Skip to content

fix(thread): keep the main thread across a Claude Code compaction - #519

Merged
amiddavid merged 4 commits into
mainfrom
feat/compaction-thread
Oct 14, 2026
Merged

amiddavid merged 4 commits into
mainfrom
feat/compaction-thread

Conversation

@amiddavid

@amiddavid amiddavid commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

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 /context showed 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

  1. Detection (proxy/agentcompaction.go): the proxy now looks for the compaction prompt in the last turn after trailing system messages. Claude Code 2.1.296 ends every request with a <total_tokens> system message.

  2. 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").

  3. 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.

  4. 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.

  5. 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).

  6. 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 system message, 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.

  7. 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 from p:1d6e… to p:72840…, and then to p: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, gofmt and go test -race on ./internal/thread and ./proxy on box 1: all pass.
  • New tests use the request shapes recorded from Claude Code 2.1.296. The tests before this PR assumed the prompt came as a new, last, user message, so they passed on a shape Claude Code does not send.
  • 9 mutations, each reverting one part of the fix: all 9 make a test fail.
    • the old phrase;
    • no skip of system messages;
    • no compaction-parent rule;
    • a side request can take the main id;
    • no preference for the thread with tools;
    • no model tie-breaker;
    • no rewritten-tail rule;
    • the old succession rule;
    • no "more than half" condition.
  • go test on ./proxy, ./dash, ./apply and ./internal/thread: all pass.
  • Interactive Claude Code 2.1.296 in auto mode (tmux, Sonnet 5.5), through a recorder and a build of this branch: a background-task notification, a classifier request and 3 turns. All main requests stayed on "", and keep-alive pinged the main thread on time.
  • A test checks that a model change (Opus plan mode) does not split a thread.
  • Live, on the laptop, with a darwin build of this branch, Sonnet 5.5, claude -p: 2 turns, then /compact, then 3 turns.
Released v0.5.0 This branch
Thread of the compaction call p:76594e9a5774 (new) "" (main)
Thread after the compaction p:454991dcddc6 (new) "" (main)
Compaction call sent unchanged no yes (agent_compaction gate: 1)
  • Live, both builds, same script (interactive Claude Code 2.1.296 in auto mode, Sonnet 5.5): a file read, a Bash command quoting the compaction prompt, a background-task notification, /compact, then 2 turns.
Released v0.5.0 This branch (48312d5)
Compaction call (same 13 messages, last one rewritten) new thread p:3ba1d246ab56 ""
Turns after the compaction p:3ba1d246ab56 ""
Keep-alive pings on the dead pre-compaction thread 2 0
Classifier request flagged as a compaction (not triggered in this run) yes, on its own thread; the main thread did not move

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_result that quotes the phrase as the last user turn (see the comment at the expand injection in proxy/proxy.go). It is out of scope here.

🤖 Generated with Claude Code

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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. The new parent rules also search header threads (a:<id>). internal/thread/thread.go, compactionParent and rewrittenTailParent. 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, preferOnTie picks the newer thread, which is the subagent.

    • Failing case A (rewritten tail): main thread, 10 messages, tools. Then header f1 with 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 f1 with the same 8 + 1. Then the compaction request (7 shared, 1 changed). Result: a:f1. The code then sets compacting on a:f1, not on main. The post-compaction request would take the id a: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 with Inherits. 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.go in 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/thread all 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.
  2. Test gap: the hasTools condition on the rewritten-tail rule. thread.go, case hasTools && agentID == "":. Mutation case agentID == "": leaves all internal/thread tests 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 "".

  3. 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.

  4. Test gap: the developer role in lastTurn. proxy/agentcompaction.go. Mutation case "system": (drop developer) leaves the ./proxy compaction tests green. Add a test with a trailing developer message, or drop developer from 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 == 0 and lcp == 0 are 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

  1. 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.
  2. compactionParent has 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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: headerThread skips a: threads in compactionParent and rewrittenTailParent. The rest of the diff is tests.
  • go test -race ./internal/thread on box 1: pass.
  • Each round 1 point, re-checked by mutation on box 1:
    1. headerThread returns false: fails TestARewrittenTailDoesNotJoinAHeaderThread and TestACompactionDoesNotMarkAHeaderThread. It also fails my own two round 1 tests, which still sit in my box copy.
    2. case agentID == "" (no hasTools): fails TestAToolLessRewrittenTailDoesNotJoinTheMainThread.
    3. lcp < th.n-3: fails TestARequestSharingAllButThreeMessagesIsANewThread.
    4. Drop developer from lastTurn: fails TestAgentCompactionIsBypassed (./proxy).
  • Fail-open is unchanged: the change only removes candidates.

Questions

None.

Reviewed by an AI reviewer (Claude)

@amiddavid
amiddavid merged commit 06c09ac into main Oct 14, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 14, 2026
OsherElhadad pushed a commit that referenced this pull request Oct 15, 2026
…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]>
amiddavid pushed a commit that referenced this pull request Oct 20, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(thread): a Claude Code compaction splits the main thread, so the status line freezes

1 participant