Skip to content

CLI session binding churns on mention-state toggles: ExplicitlyMentionedBot leaks into extraSystemPromptStatic #99696

Description

@obviyus

What happened

CLI-backend session resume (claude-cli) invalidates with reuse=invalidated:system-prompt whenever consecutive group messages toggle between "explicitly @-mentions the bot" and "doesn't mention the bot". Each invalidation abandons the resumed CLI session and spawns a fresh one, dropping continuity and re-paying warm-up.

This is the same disease as #99372 / #99595 / #99633 (per-turn facts leaking into the session-stable reuse fingerprint), on a new axis: per-message mention state. With the senderIsOwner axes fixed, this becomes the dominant remaining churn source — "hi @bot" followed by an un-mentioned follow-up is the most common two-message pattern in group chats.

Root cause

buildGroupChatContext bakes a per-message fact into the group context block:

  • src/auto-reply/reply/groups.ts (buildGroupChatContext): when sessionCtx.ExplicitlyMentionedBot === true, it appends "The incoming message explicitly mentions your channel identity @...". ExplicitlyMentionedBot is a property of the current inbound message, not the session.
  • src/auto-reply/reply/get-reply-run.ts (extraSystemPromptStatic): groupChatContext is one of the parts joined into extraSystemPromptStatic, which feeds cliSessionBindingFacts and is hashed into extraSystemPromptHash.
  • src/agents/cli-session.ts (resolveCliSessionReuse): a changed extraSystemPromptHash returns invalidatedReason: "system-prompt", which deletes the stored binding and forces a fresh CLI session.

So the "static" prompt hash flips on every mention↔no-mention transition.

Related, same file: groupIntro is conditionally included in extraSystemPromptStatic (shouldInjectGroupIntro = isFirstTurnInSession || sessionEntry?.groupActivationNeedsSystemIntro, get-reply-run.ts). A fresh session's first turn hashes with the intro and the second turn without, guaranteeing one extra invalidation early in every new session. Same class: turn-varying input inside the session-stable hash.

Production evidence

Observed 2026-07-03 on a live Telegram group deployment running fix/cli-binding-tool-scope (abc6ee5) with temporary per-hash attribution logging in resolveCliSessionReuse:

UTC message ExplicitlyMentionedBot extraSystemPromptHash outcome
23:09:42 owner: "hi @HamVerBot" true 2dba… reuse=reusable
23:09:59 third-party bot: 2-char message (emoji placeholder; bot-to-bot ingress) false 5ac0… invalidated: extraSystemPrompt (attribution log: stored 2dba… vs current 5ac0…)
23:10:05 owner: "bong bong" false 5ac0… reuse=none (cascade: binding deleted by prior invalidation, next binding not yet persisted)

promptToolNamesHash and messageToolPolicyHash stayed constant across all three turns, isolating the mention line as the changing content. The hash tracked mention state, not sender: the owner→non-owner flip at 23:10:05 did not flip the hash back.

Expected

The reuse fingerprint should only include session-stable facts. Per-message facts (mention state) belong in per-turn context — inboundMetaPrompt already exists for exactly this and is correctly excluded from extraSystemPromptStatic.

Fix shape

  • Move the explicit-mention notice out of buildGroupChatContext into the per-turn inbound context (buildInboundMetaSystemPrompt path) so extraSystemPromptStatic stops depending on ExplicitlyMentionedBot.
  • Decide groupIntro's contract: either include it unconditionally in the binding facts (stable content) or exclude it from the static hash; today's conditional inclusion churns turn 2 of every fresh session.
  • Regression tests mirroring the production pattern: mention → no-mention → mention with a stable extraSystemPromptHash throughout.

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1High-priority user-facing bug, regression, or broken workflow.agentsAgent runtime and toolingbugSomething isn't workingclawsweeper:needs-maintainer-reviewClawSweeper marked this issue as needing maintainer review before automation.clawsweeper:no-new-fix-prClawSweeper does not recommend queueing a new automated fix PR for this issue.clawsweeper:source-reproClawSweeper found a high-confidence source-level issue reproduction.impact:session-stateSession, memory, transcript, context, or agent state can drift or corrupt.issue-rating: 🦞 diamond lobsterVery strong issue quality with high-confidence source-level or clear reproduction.maintainerMaintainer-authored PR

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions