fix: group agent session resume churns when messages toggle between @-mention and plain#99722
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 3, 2026, 8:43 PM ET / 00:43 UTC. Summary PR surface: Source -1, Tests +269. Total +268 across 10 files. Reproducibility: yes. at source level: current main includes per-message mention state and conditional group intro in the static CLI prompt hash path. The PR comment also gives live Telegram claude-cli canary output for the after-fix mention/plain/mention sequence. Review metrics: 1 noteworthy metric.
Stored data model Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Mantis proof suggestion Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land this PR after maintainer session-state approval, treating it as the candidate fix for #99696 while explicitly superseding or sequencing the older groupIntro work. Do we have a high-confidence way to reproduce the issue? Yes at source level: current main includes per-message mention state and conditional group intro in the static CLI prompt hash path. The PR comment also gives live Telegram claude-cli canary output for the after-fix mention/plain/mention sequence. Is this the best way to solve the issue? Yes for the mention-state axis: per-message mention facts belong in per-turn inbound context instead of the session-stable CLI prompt identity. The groupIntro change is plausible and live-proven, but maintainers should reconcile it with #91157 before merge. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against f59310a555d7. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source -1, Tests +269. Total +268 across 10 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
Live canary results (validated)Branch build (
One CLI session survived the full mention-toggle + sender-flip sequence with zero spurious invalidations. No turn failures. Marking ready for review. |
Prompt-cost numbers from the production canariesMeasured from the same production group deployment's
Spurious invalidation rate on the same group, same traffic shape, across the fix series:
Net effect: the common conversational turn now sends ~10x fewer prompt characters than an invalidated one, keeps Claude CLI's warm session state (native compaction instead of history replay), and the cascade case — where queued turns lost history entirely — no longer occurs outside genuine session-level changes. Small samples from a single deployment, but the per-turn costs are deterministic (bootstrap + replay vs delta) and the invalidation mechanism is covered by the fail-confirmed regression tests. |
|
The single red check ("Run agentic native Telegram proof") is a capture-environment limitation, not an observed regression: its The Telegram-visible behavior in question (session continuity across mention toggles) is proven above on a real Telegram group deployment (#99722 (comment)). All 70 other checks pass. Landing on that evidence. |
Closes #99696
Related: #99640, #99595, #99372
What Problem This Solves
Fixes an issue where CLI-backend agents (e.g. Claude CLI) in group chats abandon their resumed session whenever consecutive messages toggle between explicitly @-mentioning the bot and not mentioning it — the most common message pattern in groups ("hi @bot" followed by a plain follow-up). Each toggle invalidated the stored session binding (
reuse=invalidated:system-prompt) and spawned a fresh CLI session, dropping continuity and re-paying warm-up.Also fixes a related guaranteed one-shot invalidation: the group behavioral intro was injected only on a session's first turn, so turn 2 of every fresh group session hashed differently from turn 1 and invalidated once more.
Fourth member of the per-turn-facts-in-session-stable-hash family (#99372, #99595, #99640).
Why This Change Was Made
buildGroupChatContextappended a "the incoming message explicitly mentions your channel identity" line whenExplicitlyMentionedBotwas true — a per-message fact — and that context is part ofextraSystemPromptStatic, which is hashed into the CLI session reuse fingerprint. The fix moves the mention notice to the per-turn inbound user context (Conversation infopayload,explicit_bot_mention_note), which is already excluded from the static hash and stripped from replays/UI by existing metadata handling. The model still learns about the mention every time it happens; the system prompt no longer changes shape because of it.The group intro is now included unconditionally for group sessions (Claude CLI fixes the system prompt at session creation, so session-stable content must not depend on turn ordinality). Activation-mode changes still invalidate — correctly — because the intro text itself changes.
Non-goal: the
shouldInjectGroupIntro/groupActivationNeedsSystemIntroflag machinery now only maintains its own state and is a candidate for removal, but it touches command flows, session reset, and session-entry slot keys, so that cleanup is left as a follow-up rather than expanding this fix's surface.User Impact
Group-chat agents on CLI backends keep one resumed session across natural conversations that mix @-mentions and plain messages, and no longer burn an extra fresh session on the second turn of every new session. No change to what the model is told about mentions — the information moved from the system prompt to per-turn context.
Evidence
resolveCliSessionReuse):extraSystemPromptHashflipped2dba…↔5ac0…tracking exactly the mention state of each inbound message whilepromptToolNamesHash/messageToolPolicyHashstayed constant — full table in CLI session binding churns on mention-state toggles: ExplicitlyMentionedBot leaks into extraSystemPromptStatic #99696.ExplicitlyMentionedBotis true.pnpm tsgo0,pnpm check:test-types0, affected-test sweep (prompt-composition, groups, inbound-meta, strip-inbound-meta, get-reply-run.media-only, cli-runner/prepare, cli-session) all 0,prompt:snapshots:check0, oxlint 0,git diff --check0, autoreview clean.Draft until the live canary confirms stable reuse across mention toggles.