[Bug] Custom prompt commands (/cmp etc.) always land in the wrong session due to state-corruption in mismatch recycle
Summary
cc-connect's getOrCreateInteractiveStateWith (core/engine.go:3909-3932) decides to recycle the live agent process whenever disk session.AgentSessionID and live agentSession.CurrentSessionID() disagree. This causes every prompt-typed custom command to land in the wrong (often empty) session when disk state goes stale.
A /cmp custom command (prompt=/compress) is the reproducer in this report, but the bug affects every prompt path: regular user messages, custom commands, skill execution, cron/timer (existing-session), and heartbeat.
Affected code paths (all converge on getOrCreateInteractiveStateWith)
| File:Line | Trigger |
|---|---|
engine.go:2983 |
User message |
engine.go:14398 |
Custom command (executeCustomCommand) |
engine.go:14626 |
Skill execution |
engine.go:1552 |
Cron (existing session) |
engine.go:1743 |
Timer (existing session) |
engine.go:2201 |
Heartbeat |
Yes, all prompt-typed custom commands (prompt = "...") are affected. The bug is engine-level, not agent-specific. Claude Code, Codex, and pi are all equally vulnerable — the engine compares disk vs. live, not agent-specific code.
Reproduction (pi agent, 372K-token context)
- Real conversation with pi on session
019f17d7. Context 35% (~372K tokens). - User sends
/cmp(custom commandprompt="/compress"). - cc-connect mismatch-recycling:
want=019f17d3, have=019f17d7→ recycle, spawn pi--session-id 019f17d3(wrong, empty session).
- pi compaction runs on the empty session →
"❌ Compression failed: Nothing to compact (session too small)". - The /cmp's
EventResultthen overwrites disk viaengine.go:5303(session.SetAgentSessionID(state.agentSession.CurrentSessionID())) — locking in the wrong id019f17d3. - Next user message triggers another mismatch, killing the actually-correct live session and resuming
019f17d3again. Ping-pong forever.
Server log excerpt:
19:29:17 /cmp: want=019f17d3, have=019f17d7 → recycle to 019f17d3 (wrong)
19:29:37 user reply: want=019f17d7, have=019f17d3 → recycle to 019f17d7 (right)
19:32:07 /cmp again: want=019f17d3, have=019f17d7 → recycle to 019f17d3 (wrong again)Root cause (two-part)
(A) Mismatch decision logic at core/engine.go:3909-3932:
wantID := session.GetAgentSessionID() // disk (may be stale)
currentID := state.agentSession.CurrentSessionID() // live (authoritative)
needRecycle := currentID != "" && (wantID == "" || wantID != currentID)Disk is treated as truth. When disk is stale (different id, possibly empty/wrong from a prior botched turn), the live process is killed. This destroys user context as collateral damage.
(B) Disk-corruption source: processInteractiveEvents EventResult handler at engine.go:5299-5310 writes live CurrentSessionID() back to disk even when the live session is itself wrong (because of a preceding mismatch recycle). This makes the corruption self-perpetuating.
Proposed fix (one-liner, conservative)
Change the mismatch decision at engine.go:3918:
// Before
needRecycle := currentID != "" && (wantID == "" || wantID != currentID)
// After
needRecycle := currentID == "" && wantID != ""Semantics: trust the live process whenever it reports a session id. Only recycle when live is dead but disk has an id (cold-start with persisted session).
For explicit user-initiated session changes (/new, /switch, /dir, /model, /provider, /reasoning, /cancel, /workspace), cleanupInteractiveState already kills live before clearing disk (/new at line 6608, /dir at 8014, /reasoning at 9651, /cancel at 9884, etc.), so this change does NOT break those flows — by the time the next message arrives, live is already empty.
Why I'm not opening a PR
The mismatch logic is the engine's central session-state decision and is crossed by every user-facing code path. The fix itself is small but the failure modes are subtle:
- Edge case:
HandleRelayresume failure atengine.go:15611clears disk without killing live — Option A correctly handles this without recycle. - Cross-project session invalidation via
SessionIDValidator(engine.go:4006-4014) deserves review before commit. - Existing tests (
TestSessionMismatch_*,TestSessionIDWriteback_*,TestResumeFallback_*incore/engine_test.go) all guard correct behavior in their specific scenarios; none cover "stale disk + alive live" as a regression target. - A new CUJ test in
core/cuj_test.gowould be appropriate:TestCUJ_CustomCommandKeepsActiveSession— reproduce the user's scenario and assert /cmp lands in the correct session.
I want the maintainer to decide:
- Whether Option A is the right intent for this codebase's session-state philosophy
- Whether a
stateGuardflag (e.g. set when /new clears disk, cleared on first live spawn) would be a more conservative approach than relying oncleanupInteractiveStatealways killing live first - What test coverage is needed in
core/cuj_test.goto lock this down
I am happy to submit a PR with appropriate tests once the approach is agreed.
Temporary workaround (immediate, no code change)
Edit ~/.cc-connect/sessions/project-one_ws_<hash>.json and set the corrupted session's agent_session_id to the correct live id. After cc-connect restart, the next message resumes the right session.
Verified working on pi agent in my environment; the unblock was changing s91.agent_session_id from 019f17d3-… to 019f17d7-… (the actual live id).
Source: chenhg5/cc-connect