Skip to content

fix(slack): reduce token bloat by skipping thread context on existing sessions#27609

Closed
sourman wants to merge 1 commit intoopenclaw:mainfrom
sourman:fix/slack-thread-context-bloat
Closed

fix(slack): reduce token bloat by skipping thread context on existing sessions#27609
sourman wants to merge 1 commit intoopenclaw:mainfrom
sourman:fix/slack-thread-context-bloat

Conversation

@sourman
Copy link
Contributor

@sourman sourman commented Feb 26, 2026

BAD

Screenshot 2026-02-26 092554

Good

Screenshot 2026-02-26 091545

Thread history and thread starter were being included on every message in a Slack thread
This PR Only fetches ThreadHistoryBody for new thread sessions

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

  • Closes #
  • Related #

User-visible / Behavior Changes

Before: Every message in a Slack thread included [Thread starter - for context] with the thread starter text.

After: Only the first message in a thread session includes context ([Thread history - for context] with full thread history or [Thread starter - for context] with just the starter). Subsequent messages rely on the session transcript which already has the full conversation history.

Security Impact (required)

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (Yes - REDUCE API calls to Slack for thread history)
  • Command/tool execution surface changed? (No)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation:
    No additional endpoints touches just reduced calls to slack for hsitory

Repro + Verification

  1. Message your openclaw in a channel and wait for it respond in a thread
  2. Send multiple follow-up replies in the thread
  3. Check the session file in ~/.openclaw/agents/main/sessions/ or open the tui and /session to open the session
  4. Verify: First message includes [Thread history - for context] with full thread
  5. Verify: Subsequent messages do NOT include [Thread history - for context]

AI-Assisted PR

  • Mark as AI-assisted in the PR title or description - mea culpa
  • Include prompts or session logs if possible (super helpful!)
  • Confirm you understand what the code does

This PR was developed with GLM 5. I understand the changes and have verified they fix the token bloat issue.

Session file evidence: Before the fix, sessions included a Copy of every previous message in every new message in the thread. After the fix, only the first message should include this context wrapper.

… sessions

Thread history and thread starter were being included on every message
in a thread, not just the first message. This caused significant token
bloat since the session transcript already maintains the full conversation
history.

Changes:
- Only fetch ThreadHistoryBody for new thread sessions (when !threadSessionPreviousTimestamp)
- Only include ThreadStarterBody for new thread sessions (existing sessions already have it in their transcript)

This fixes the issue where:
- First message: Gets "[Thread history - for context]" with full thread (correct)
- Subsequent messages: Were getting "[Thread starter - for context]" redundantly (now fixed)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@openclaw-barnacle openclaw-barnacle bot added channel: slack Channel integration: slack size: XS labels Feb 26, 2026
@sourman sourman marked this pull request as ready for review February 26, 2026 14:43
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

This PR fixes token bloat in Slack threads by preventing redundant context from being fetched and included on every message. The fix ensures thread history and thread starter context are only included for new thread sessions - existing sessions already have this context in their transcript.

Key changes:

  • Conditionally fetch thread history only when !threadSessionPreviousTimestamp (line 543 in prepare.ts)
  • Set ThreadStarterBody to undefined for existing sessions (line 632)
  • Thread label construction and media resolution still work correctly via the thread starter fetch
  • All downstream consumers (get-reply-run.ts, inbound-context.ts, session.ts) safely handle undefined values
  • The change aligns with existing consumption logic that was already stripping these fields for non-new sessions

The implementation is clean, well-tested, and addresses the stated problem effectively.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are well-isolated to Slack thread context handling, thoroughly tested, and align with existing downstream logic. All consumers safely handle undefined values, error paths return safe defaults, and the optimization reduces unnecessary API calls and token usage without changing user-visible behavior.
  • No files require special attention

Last reviewed commit: b40d384

sourman pushed a commit to sourman/openclaw that referenced this pull request Feb 26, 2026
Further reduces token bloat by removing two redundant context elements:

1. [Thread starter - for context] wrapper - The thread starter is already
   included in the [Thread history - for context] on the first message,
   and the session transcript maintains context for subsequent messages.

2. Message preview in system events - The "Slack message in #channel
   from Sender: message preview" was duplicating the actual message body
   that follows. Removed the preview, keeping only the metadata label.

Result: Messages now go directly to the LLM without redundant wrappers:
- Before: "[Thread starter - for context]\n<@U0AF3PBGARJ> hello\n\nSystem: [timestamp] Slack message...\n\nactual message"
- After: "actual message"

The session transcript and initial thread history provide sufficient context
for the LLM to understand the conversation.

Related to openclaw#27609 (initial fix for thread history bloat)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sourman pushed a commit to sourman/openclaw that referenced this pull request Feb 26, 2026
Further reduces token bloat by removing two redundant context elements:

1. [Thread starter - for context] wrapper - The thread starter is already
   included in the [Thread history - for context] on the first message,
   and the session transcript maintains context for subsequent messages.

2. Message preview in system events - The "Slack message in #channel
   from Sender: message preview" was duplicating the actual message body
   that follows. Removed the preview, keeping only the metadata label.

Result: Messages now go directly to the LLM without redundant wrappers:
- Before: "[Thread starter - for context]\n<@U0AF3PBGARJ> hello\n\nSystem: [timestamp] Slack message...\n\nactual message"
- After: "actual message"

The session transcript and initial thread history provide sufficient context
for the LLM to understand the conversation.

Related to openclaw#27609 (initial fix for thread history bloat)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sourman pushed a commit to sourman/openclaw that referenced this pull request Feb 26, 2026
Further reduces token bloat while preserving context for multi-human threads:

1. [Thread starter - for context] wrapper - The thread starter is already
   included in the [Thread history - for context] on the first message,
   and the session transcript maintains context for subsequent messages.

2. Verbose system event reduced to simple sender preamble - Changed from
   "System: [timestamp] Slack message in #channel from Sender: preview" to
   just "Slack message from Sender" for clean sender identification in
   multi-human threads.

Result: Messages now show clean sender context without redundant metadata:
- Before: "[Thread starter - for context]\nstarter\n\nSystem: [timestamp] Slack message in #channel from Sender: preview\n\nactual message"
- After: "Slack message from Sender\n\nactual message"

For the first message in a new thread session, full thread history is provided.

Related to openclaw#27609 (initial fix for thread history bloat)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourman sourman marked this pull request as draft February 26, 2026 19:22
@sourman
Copy link
Contributor Author

sourman commented Feb 26, 2026

This PR works well for requireMention = false, but for requireMention = true it actually breaks the context passing. need to rethink it

sourman pushed a commit to sourman/openclaw that referenced this pull request Feb 27, 2026
Further reduces token bloat while preserving context for multi-human threads:

1. [Thread starter - for context] wrapper - The thread starter is already
   included in the [Thread history - for context] on the first message,
   and the session transcript maintains context for subsequent messages.

2. Verbose system event reduced to simple sender preamble - Changed from
   "System: [timestamp] Slack message in #channel from Sender: preview" to
   just "Slack message from Sender" for clean sender identification in
   multi-human threads.

Result: Messages now show clean sender context without redundant metadata:
- Before: "[Thread starter - for context]\nstarter\n\nSystem: [timestamp] Slack message in #channel from Sender: preview\n\nactual message"
- After: "Slack message from Sender\n\nactual message"

For the first message in a new thread session, full thread history is provided.

Related to openclaw#27609 (initial fix for thread history bloat)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sourman added a commit to sourman/openclaw that referenced this pull request Feb 27, 2026
Further reduces token bloat while preserving context for multi-human threads:

1. [Thread starter - for context] wrapper - The thread starter is already
   included in the [Thread history - for context] on the first message,
   and the session transcript maintains context for subsequent messages.

2. Verbose system event reduced to simple sender preamble - Changed from
   "System: [timestamp] Slack message in #channel from Sender: preview" to
   just "Slack message from Sender" for clean sender identification in
   multi-human threads.

Result: Messages now show clean sender context without redundant metadata:
- Before: "[Thread starter - for context]\nstarter\n\nSystem: [timestamp] Slack message in #channel from Sender: preview\n\nactual message"
- After: "Slack message from Sender\n\nactual message"

For the first message in a new thread session, full thread history is provided.

Related to openclaw#27609 (initial fix for thread history bloat)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sourman
Copy link
Contributor Author

sourman commented Feb 27, 2026

Addressed in #29067 - session-based sticky routing + context bloat fixes

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

Labels

channel: slack Channel integration: slack size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant