Repository navigation
fix(thread): keep the main thread across a Claude Code compaction - #519
Conversation
Claude Code 2.1.296 changed the shape of its compaction request, and v0.5.0
then moved the main thread to a new id at every compaction. The status line
followed the dead thread (ctx 98%, cache cold, a wrong rewrite hint).
- Detection: skip trailing system messages, and match "summary of " so both
prompt variants ("of the conversation so far", "of this conversation") hit.
- Thread: a compaction request that extends no thread joins the thread that
shares the longest run of leading messages, preferring one with tools.
- Succession: only a request that declares tools takes over a tools thread
after its compaction, so Claude Code's tool-less side request cannot.
Fixes #518
Assisted-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
When a compaction request ties between two threads on shared messages and tools, or two compacting threads compete for the next request, prefer the thread whose last request named the same model, then the most recent. The model is a tie-breaker only. One thread can change model (/model, Opus plan mode, a fallback on overload), so a different model never keeps a request out of a thread. Refs #518 Assisted-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
Seen on a live v0.5.0 session: the main thread moved onto the id of a tool-less side thread at two turn starts. Two things together did it. - At those turn starts Claude Code changed the last user message, or dropped the trailing system message, instead of only appending, so the request matched no thread by prefix (the provider cache still read 99.8% of it). - Claude Code's auto-mode classifier sends the session transcript in one tool-less message. That transcript quoted the compaction prompt, so the request was flagged as a compaction and its thread marked compacting, and the unmatched main request took that thread's id. A header-less request that declares tools and matches no thread now joins the tools thread it repeats except for the last one or two messages (and more than half of them). A compacting thread now passes its id only to a request of the same kind: tools to tools, tool-less to tool-less. Refs #518 Assisted-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Verdict
Request changes. One real defect: the two new rules can pull a header-less main request onto a subagent thread (a:<id>). Three test gaps. The rest holds.
Findings
-
The new parent rules also search header threads (
a:<id>).internal/thread/thread.go,compactionParentandrewrittenTailParent. Both loop over every thread of the session. A header thread that declares tools is a valid candidate. This matters for a forked subagent: it carries the parent's messages and has its own header. A header-less main request can then match the subagent as well as the main thread. With an equal shared run,preferOnTiepicks the newer thread, which is the subagent.- Failing case A (rewritten tail): main thread, 10 messages, tools. Then header
f1with the same 10 messages + 1. Then a main turn: first 9 messages, a rewritten 10th, +1 new. Result:a:f1. Expected:"". - Failing case B (compaction): main with 8 messages, fork
f1with the same 8 + 1. Then the compaction request (7 shared, 1 changed). Result:a:f1. The code then setscompactingona:f1, not on main. The post-compaction request would take the ida:f1. That is the #518 symptom again. - Before this PR, case B gave main a new
p:id and case A gave a new id withInherits. Neither moved main onto a subagent id. - Proposed fix: skip threads whose id starts with
a:in both loops. A header-less request is never a header thread's request. - I wrote 2 tests for A and B (the file is
internal/thread/review519_test.goin my review worktree, copy in/tmp/review-519-tests.go.txt). Both fail on the PR head and pass with the fix above (checked on box 1;./internal/threadall green). - Caveat: I built the fork shape by hand (parent messages + 1, with header). I did not see it on the wire. I believe Claude Code fork subagents send the parent's messages.
- Failing case A (rewritten tail): main thread, 10 messages, tools. Then header
-
Test gap: the
hasToolscondition on the rewritten-tail rule.thread.go,case hasTools && agentID == "":. Mutationcase agentID == "":leaves allinternal/threadtests green. Without it, a tool-less side request that shares all but its last message with main would join main and overwrite its tail. That is the #426 bug class. Add a test: a tool-less request sharing n-1 messages must not return"". -
Test gap: the "last 2 messages" boundary. Mutation
lcp < th.n-3(allow a 3-message rewrite) leaves all tests green. Add a test with exactly 3 rewritten messages: it must not join. -
Test gap: the
developerrole inlastTurn.proxy/agentcompaction.go. Mutationcase "system":(dropdeveloper) leaves the./proxycompaction tests green. Add a test with a trailingdevelopermessage, or dropdeveloperfrom the code.
What I verified
- Worktree at
6c9e03f; tests on box 1. go test -race ./internal/thread ./proxy: both pass (1.1 s and 398 s).- Read the full diff of all 5 files. Fail-open holds: no new panic path, nothing blocks. Index math is safe (
th.n == 0andlcp == 0are skipped). A wrong thread or detection result changes only the id or the bypass. It never alters a request. - Succession by kind: I traced the recorded flow (compaction call, tool-less 1-message side request, 4-message next turn). The side request gets its own
p:id. The next turn takes the main id. Correct. - Codex single-thread traffic: a normal turn extends its thread, so it never reaches the new rules.
- A user rewind of 3 or more messages still forks with
Inherits. A rewind of 1 or 2 messages now joins the thread. This is acceptable: same cache prefix. - Mutations that survived: findings 2, 3, 4.
Questions
- The detection phrase is now shorter ("summary of "). A user prompt that begins with it as the last user turn now bypasses the request and marks the thread
compacting. "of the " allowed the same. The risk grew a little. I think this is acceptable: the cost is a missed saving. compactionParenthas no minimum share (1 shared message is enough). The tail rule needs more than half. Do you want a floor? I found no failing case.
Reviewed by an AI reviewer (Claude)
Review of #519: compactionParent and rewrittenTailParent also searched threads the agent named with its header. A forked subagent carries its parent's messages plus its own, so it shares the same leading run as the main thread; a rewritten main turn could join the fork, and a compaction could mark the fork compacting and hand its id to the next main turn. Both searches now skip header threads. Also closes 3 test gaps the review found: a tool-less request must not use the rewritten-tail rule, a request sharing all but 3 messages is a new thread, and a trailing developer message is skipped like a system one. Refs #518 Assisted-By: Claude Opus 5.5 (1M context) <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Verdict
Approve (comment only). All 4 round 1 points are fixed. I found no new defect. The only new commit is 48312d5.
Findings
None.
What I verified
- Read the diff
6c9e03f..48312d5:headerThreadskipsa:threads incompactionParentandrewrittenTailParent. The rest of the diff is tests. go test -race ./internal/threadon box 1: pass.- Each round 1 point, re-checked by mutation on box 1:
headerThreadreturns false: failsTestARewrittenTailDoesNotJoinAHeaderThreadandTestACompactionDoesNotMarkAHeaderThread. It also fails my own two round 1 tests, which still sit in my box copy.case agentID == ""(nohasTools): failsTestAToolLessRewrittenTailDoesNotJoinTheMainThread.lcp < th.n-3: failsTestARequestSharingAllButThreeMessagesIsANewThread.- Drop
developerfromlastTurn: failsTestAgentCompactionIsBypassed(./proxy).
- Fail-open is unchanged: the change only removes candidates.
Questions
None.
Reviewed by an AI reviewer (Claude)
…hread (#519) A Claude Code request ends with a system reminder that the next turn replaces. With the tools declaration every real request carries, a growing conversation already stays on one thread (rewrittenTailParent). Pin that, and pin that two conversations sharing only their first message stay two threads, which tolerating just the last message would merge. Signed-off-by: Osher-Elhadad <[email protected]>
…hread (#519) (#559) test(thread): pin that a replaced trailing system message keeps the thread (#519) A Claude Code request ends with a system reminder that the next turn replaces. With the tools declaration every real request carries, a growing conversation already stays on one thread (rewrittenTailParent). Pin that, and pin that two conversations sharing only their first message stay two threads, which tolerating just the last message would merge. Signed-off-by: Osher-Elhadad <[email protected]>
Summary
Fixes #518. After a Claude Code compaction, v0.5.0 moved the main thread to a new id. The status line then showed the dead thread for the rest of the session: ctx 98% while
/contextshowed 7%, "cold" during an active conversation, and a wrong rewrite hint. This PR keeps the main thread across a compaction. It is the fix for v0.5.1.What changed
Detection (
proxy/agentcompaction.go): the proxy now looks for the compaction prompt in the last turn after trailingsystemmessages. Claude Code 2.1.296 ends every request with a<total_tokens>system message.Phrase: the proxy now matches "Your task is to create a detailed summary of ". This covers both prompt versions in 2.1.296 ("of the conversation so far" and "of this conversation").
Thread (
internal/thread/thread.go): a compaction request that extends no thread now joins the thread that shares the longest run of leading messages with it. If two threads tie, the one that declares tools wins. Claude Code puts the prompt inside the last user message, so the request never extends the thread it summarizes.Succession: only a request that declares tools can take a tools thread's id after its compaction. Claude Code sends a tool-less side request between the compaction and the next turn, and that request must not take the main id.
Model as a tie-breaker: when the rules above tie between 2 threads, the proxy prefers the thread whose last request used the same model, then the most recent. The model is never a hard rule, because one thread can change model (
/model, Opus plan mode, a fallback on overload).A rewritten tail at a turn start (seen live on this session after the first fix): at some turn starts Claude Code changed the last user message, or dropped the trailing
systemmessage, instead of only adding messages. The provider cache still read 99.8% of the prompt, but the request matched no thread. A header-less request that declares tools and matches no thread now joins the tools thread whose last request it repeats, except for the last 1 or 2 messages. It must also share more than half of that thread's messages, so a short thread is not joined by a fork.Succession by kind: a compacting thread now passes its id only to a request of the same kind (tools to tools, no tools to no tools). Claude Code's auto-mode classifier sends the session transcript in one tool-less message. On this session the transcript quoted the compaction prompt, so every classifier request was flagged as a compaction (
bypassed) and its thread was marked compacting. The unmatched main request then took the classifier thread's id: the main thread moved fromp:1d6e…top:72840…, and then top:3753e3….Effect on the proxy's own components: because of causes 1 and 2, the proxy ran its components on Claude Code's compaction call. Now it sends that call unchanged again, as it must.
How it was tested
go vet,gofmtandgo test -raceon./internal/threadand./proxyon box 1: all pass.go teston./proxy,./dash,./applyand./internal/thread: all pass."", and keep-alive pinged the main thread on time.claude -p: 2 turns, then/compact, then 3 turns.p:76594e9a5774(new)""(main)p:454991dcddc6(new)""(main)agent_compactiongate: 1)/compact, then 2 turns.p:3ba1d246ab56""p:3ba1d246ab56""Technical notes
The recorded shape (structure only): the compaction call has the same 8 messages as the turn before. Its
[6]is the user message with a second text block (the prompt), and its[7]is the same system message. Next comes a 1-message request without tools. Then the next turn: 4 messages, and[0]starts with "This session is being continued from a previous conversation".The known false positive is unchanged, and it is now visible on the classifier: its request is forwarded unchanged (bypassed), which is harmless, and its thread can no longer take the main id. Narrowing the detector (for example, the phrase must be near the start of a text block) changes what bypass means for every caller, so it is left for its own change. The case: a
tool_resultthat quotes the phrase as the last user turn (see the comment at the expand injection inproxy/proxy.go). It is out of scope here.🤖 Generated with Claude Code