-
-
Notifications
You must be signed in to change notification settings - Fork 54.6k
Description
Summary
Slack thread sessions include full thread history on every message in a thread, not just the first. This causes unnecessary token bloat and increased API costs - the session transcript already contains the full conversation, so re-fetching and re-injecting thread history on each turn is redundant.
Steps to Reproduce
- Start a Slack thread with the bot (e.g., @mention in a channel)
- Send multiple follow-up replies in the thread
- Check the session file in
~/.openclaw/agents/main/sessions/*.jsonl - Observe
[Thread history - for context]wrapper on every message, growing larger each time
Expected Behavior
- First message in thread session: Include
[Thread history - for context]with full thread history - Subsequent messages: Rely on session transcript (already has full context), skip redundant thread history fetch
Actual Behavior
Thread history is fetched and included on every single message in a thread session, causing exponential token growth:
Session Evidence (2026-03-02)
Message 2 (user: "checking in"):
[Thread history - for context]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:45 EST] <@U0AF3PBGARJ> ping
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] Pong. What do you need?
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] :warning: :email: Message: `with message 1772480714.721979, emoji 👍` failed
Message 3 (user: "what is the fifth letter in four"):
[Thread history - for context]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:45 EST] <@U0AF3PBGARJ> ping
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] Pong. What do you need?
[Slack comply (assistant) Mon 2026-03-02 14:45 EST] :warning: :email: Message: `with message 1772480714.721979, emoji 👍` failed
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:46 EST] checking in
[Slack comply (assistant) Mon 2026-03-02 14:46 EST] All clear here. No pending items, no anomalies. Anything you need?
Message 4 (user: "are you sure"):
[Thread history - for context]
[... entire conversation history repeated again ...]
[Slack Ahmed Mansour (user) Mon 2026-03-02 14:46 EST] what is the fifth letter in four
[Slack comply (assistant) Mon 2026-03-02 14:46 EST] "Four" has 4 letters. No fifth letter exists.
Input token counts showing bloat:
- Message 1 (ping): 12,379 tokens
- Message 2: 3,533 tokens
- Message 3: 4,161 tokens
- Message 4: 4,892 tokens
The session transcript already contains this full context - re-injecting it on every message is wasteful.
Root Cause
In src/slack/monitor/message-handler/prepare.ts, the resolveSlackThreadHistory function fetches thread history unconditionally. The ThreadStarterBody is then injected on every message via [Thread history - for context] wrapper.
Related
- Related [Bug]: Slack DM thread sessions lose context after first message — thread history only injected on isNewSession #23037 (discusses thread history behavior but focuses on context loss, not bloat)
- PR fix(slack): reduce token bloat by skipping thread context on existing sessions #27609 (attempted fix, superseded)
- PR fix(slack): session-based sticky routing + context bloat fixes #29067 (comprehensive fix including this issue, superseded)
Environment
- OpenClaw: main (2026-03-02)
- Channel: Slack (socket mode)
- Config:
channels.slack.thread.historyScope: "thread"
Impact
- Cost: Unnecessary API calls to fetch thread history on every message
- Latency: Additional Slack API round-trips per message
- Tokens: Redundant context inclusion increases prompt size
Suggested Fix
Only fetch and inject thread history for isNewSession (first message in thread session). Subsequent messages should rely on the session transcript which already contains the full conversation history.
Condition should be:
const shouldFetchHistory = !threadSessionPreviousTimestamp;