Skip to content

fix(slack): session-based sticky routing + context bloat fixes#29067

Closed
sourman wants to merge 4 commits intoopenclaw:mainfrom
sourman:fix/slack-thread-routing-and-context
Closed

fix(slack): session-based sticky routing + context bloat fixes#29067
sourman wants to merge 4 commits intoopenclaw:mainfrom
sourman:fix/slack-thread-routing-and-context

Conversation

@sourman
Copy link
Contributor

@sourman sourman commented Feb 27, 2026

  • Problem: In Slack channels with requireMention: true, follow-up replies in threads are dropped when users don't re-@mention the bot; even after the bot has already participated. Additionally, thread history was being fetched on every message, causing significant token bloat. See fix(slack): reduce token bloat by skipping thread context on existing sessions #27609 for bloat example
  • Why it matters: Users have to repeatedly @mention the bot in ongoing conversations, breaking the natural flow. Token bloat increases costs and latency.
  • What changed:
    • Session-based check for thread participation (bot replies = implicit @mention for follow-ups)
    • Thread history only fetched for NEW sessions (existing sessions already have conversation in transcript)
    • Thread-aware sender preamble helps LLM understand multi-human thread context
  • What did NOT change: Bot behavior in non-thread conversations, @mention requirement for first contact in threads

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

User-visible / Behavior Changes

Once the bot replies in a Slack thread, subsequent messages from any user (without @mention) will be routed to the bot. Thread context is no longer redundantly included in messages (reduces token usage).

Config changes: None (zero new config options)

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (Yes)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation: We now check using the slack API if the bot account replied to a thread as a fallback if we do not find an agent session matching the thread on which a new message arrived. This adds a few API calls to slack

Repro + Verification

Environment

  • OS: Linux (Amazon Linux 2023)
  • Runtime/container: Node 22+
  • Model/provider: N/A
  • Integration/channel: Slack
  • Relevant config: channels.slack.requireMention: true

Steps

  1. Start a Slack thread (user posts message, not from bot)
  2. User @mentions bot → bot replies
  3. Another user replies in thread WITHOUT @mention

Expected

Bot receives and responds to the follow-up message (session-based check passes)

Actual

Before fix: Message was silently dropped (only parent_user_id === botUserId was checked)
After fix: Bot receives message (session exists from previous reply)

Evidence

  • All 33 Slack test files pass (274 tests)
  • TypeScript type check passes
  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

  • Verified scenarios: Session-based sticky routing works after bot reply; context bloat is reduced (thread history not re-fetched)
  • Edge cases checked: DMs excluded from thread logic; non-thread messages unchanged
  • What you did not verify: I did not test it with requireMention: false

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps: N/A

Failure Recovery (if this breaks)

  • How to disable/revert this change: Revert PR; bot returns to requiring @mention on every thread message
  • Files/config to restore: src/slack/monitor/message-handler/prepare.ts
  • Known bad symptoms reviewers should watch for: Bot not receiving messages it should; or receiving messages it shouldn't

Risks and Mitigations

This whole part of the codebase needs a real hard look. Why even bother with a slack channel session if the replyToMode is all? the dichotomy between openclaw sessions and how they are presented Slack threads makes it hard for Devs and agents to work in this piece and may be unnecessary. We might consider to get rid of the channel session entirely and spawn per response sessions until replyToMode is off (no threading)

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cab8606c31

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 27, 2026

Greptile Summary

This PR successfully implements session-based sticky routing for Slack threads and optimizes thread context loading to reduce token bloat. The core changes are well-architected: (1) thread-scoped session checks detect prior bot participation without requiring re-@mention, (2) Slack API fallback with LRU caching handles edge cases where sessions don't yet exist, and (3) thread history is only fetched for new sessions, preventing redundant context in ongoing conversations.

The implementation correctly handles the key edge case mentioned in the PR description - preventing base channel sessions from causing false positives in thread mention gating. Test coverage is comprehensive, including scenarios for new vs existing sessions, API-based participation detection, and thread-scoped gating.

One minor limitation: the Slack API participation check examines up to 800 messages per thread (4 pages × 200 limit). In extremely long threads beyond this range, bot participation might not be detected and users would need to re-@mention. This is a reasonable trade-off to prevent excessive API calls and is unlikely to affect most real-world usage.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk - it's a well-tested bug fix with clear performance benefits
  • The implementation is solid with comprehensive test coverage (3 new tests covering edge cases). The logic correctly handles session-based participation checks, Slack API fallback with caching, and thread history optimization. No critical bugs identified. Score is 4 (not 5) due to the inherent complexity of distributed session state management and the pagination limit on very long threads (>800 messages), though these are reasonable architectural trade-offs rather than implementation flaws.
  • No files require special attention - both the implementation and tests are well-structured

Last reviewed commit: 5012366

@sourman sourman force-pushed the fix/slack-thread-routing-and-context branch from cab8606 to 81a882c Compare February 27, 2026 23:11
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 81a882c17e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@sourman sourman marked this pull request as draft February 28, 2026 07:32
@sourman sourman force-pushed the fix/slack-thread-routing-and-context branch from 81a882c to 87027c3 Compare February 28, 2026 18:43
sourman and others added 3 commits February 28, 2026 13:59
Only fetch thread history for new sessions. Skip for existing sessions
since the transcript already has full conversation.

Fixes token bloat where thread history was included on every message.
Once bot replies in a thread, follow-up messages without @mention reach
the bot via implicit mention.

Uses session existence check (thread session OR base channel session)
to detect bot participation. Base session check handles routing
discontinuity where first reply goes to channel session internally
but displays as thread message in Slack UI.

Sender preamble: "Slack message detected on thread: {name} speaking"
helps LLM understand multi-human thread context.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourman sourman force-pushed the fix/slack-thread-routing-and-context branch from 87027c3 to 5012366 Compare February 28, 2026 19:11
@sourman sourman marked this pull request as ready for review February 28, 2026 19:14
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5012366473

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@sourman
Copy link
Contributor Author

sourman commented Feb 28, 2026

@iamfuntime would you please take a look and test it to see if it works for you? this is a little bit more reliable than the in memory approach in #16223

@sourman
Copy link
Contributor Author

sourman commented Feb 28, 2026

@zerone0x Would you please take a look? should reduce token bloat and improve model responses related to your work on #25781

@Takhoffman Takhoffman added the close:superseded PR close reason label Mar 1, 2026
@Takhoffman
Copy link
Contributor

Thanks for the contribution and the detailed work here.

Closing this PR for now because it is superseded by a tighter Workstream 3 path that we are landing to avoid overlapping Slack threading/session changes in parallel.

What we are keeping:

  • thread participation behavior, but via a lower-risk implementation path

If you want, I can cross-reference any specific test or edge case from this PR into the active salvage PRs so your coverage is preserved.

@Takhoffman Takhoffman closed this Mar 1, 2026
@sourman
Copy link
Contributor Author

sourman commented Mar 2, 2026

Thanks @Takhoffman yeah I feel this piece needs a refactor. What exactly do you mean by workstream 3.

If you can please do grab the context bloat part of this pull request as the current implementation on threads includes a history injection that blows up the agent session on the slack thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: slack Channel integration: slack close:superseded PR close reason size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slack: implicit thread mention only checks parent_user_id, not thread participation

2 participants