Skip to content

fix(thread): a replaced trailing system message no longer starts a new thread every turn - #499

Closed
OsherElhadad wants to merge 1 commit into
mainfrom
fix/thread-volatile-tail
Closed

OsherElhadad wants to merge 1 commit into
mainfrom
fix/thread-volatile-tail

Conversation

@OsherElhadad

@OsherElhadad OsherElhadad commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

What

Thread resolution matched a request to a thread only when the thread's whole previous message list, including its last message, was a prefix of the new request. Claude Code ends a request with a mid-conversation system message and resends it in a different form (a plain string rather than the marked block it was when it was last) in the next request, so its fingerprint does not repeat: on captured SWE-bench traffic every turn of a session resolved to a new thread (p:<hash>), and everything keyed by thread lost its history.

Effect

In SWE-bench Claude Code runs, requests.thread_id was a different value on nearly every request of a session (reproduced by replaying a captured session through the proxy against a mock upstream: the first 12 requests of one session got 12 different threads before; 29 requests got 1 thread after). Features that use the previous forwarded body of a thread (the prefix ask used by extract_llm_sweep, cache_aware_summarizer and extract_llm_ride; the per-thread keep-alive candidate) found nothing: the ride logged ride_no_prefix on most turns and made no asks.

Change

If no thread continues exactly, accept the thread whose messages except the last are a prefix of the request, when at least one message remains to compare. An exact match still wins. A conversation sharing no messages with another still gets its own thread.

Tests

TestAReplacedTrailingSystemReminderDoesNotSplitTheThread (fails without the change) and TestATrailingMessageTolerance_DoesNotMergeADifferentConversation. ./proxy tests pass.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Reviewed. sums is the cumulative fingerprint, so the single comparison at index n-1 checks the whole prefix except the trailing message. An exact match still wins, and the negative test covers two unrelated conversations. The one behavioural edge: an edited or regenerated last message now stays in the same thread, which is the intended continuity. LGTM; it needs the second-account approval to merge.

@OsherElhadad OsherElhadad 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.

Review: APPROVE at 910ccda. (Posted as a comment: GitHub does not let the author account approve.)

  • The fallback compares the cumulative fingerprint of all but the thread's last message, so a match means that whole prefix is identical. An exact match still wins, and a conversation sharing no prefix still gets its own thread.
  • With the fallback disabled, TestAReplacedTrailingSystemReminderDoesNotSplitTheThread fails.
  • go test ./internal/thread and the proxy Thread tests pass on a local merge with current main, go vet is clean, and CI is green. The commit is signed off with no AI attribution.

Non-blocking: the fingerprint covers messages only (no system or tools), and a two-message thread is matched on its first message alone. A side request that starts with the same first user message as a two-message main thread would therefore join that thread. a: agent threads are unaffected because their ids come from the header. Requiring at least two compared messages (n >= 2) would remove that case at no cost to the Claude Code reminder fix. The body's replay request counts are a reproduction description rather than results; trimming them is optional.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review, round 1

Verdict: redundant after #519. Do not merge.

@OsherElhadad: please close this PR, or rebase it onto main and keep only the tests (see below).

What I checked

  • Merge onto origin/main: conflict in internal/thread/thread_test.go. thread.go merges cleanly.
  • I ran the PR's two tests on main (eval box 1, -race).
    • TestAReplacedTrailingSystemReminderDoesNotSplitTheThread: fails on main. Its requests have no tools.
    • TestATrailingMessageTolerance_DoesNotMergeADifferentConversation: passes on main.
  • I made a copy of the first test with one tools entry in the body. This is what a real Claude Code main-agent request has. It passes on main without this PR.

Why it is redundant

  • #519 added rewrittenTailParent. It already joins a request to a tools thread when only the last 1 or 2 messages differ.
  • The real defect (a replaced trailing system message splits the thread) is therefore fixed on main.
  • The PR's test only fails because its body has no tools. A tool-less main conversation does not exist in practice. Tool-less requests are side requests (title, quota check) with their own rules.

Why the fix is also harmful on top of #519

  • It adds a fallback in longestPrefix. That runs before #519's guards, so it skips them:
    • No 2*lcp > n guard. Any thread with n >= 2 is joined by a request that shares only its first message.
    • No tools check, no headerThread check, no model tie-break.
  • I showed this with a test on main + this patch: thread 1 = [hello, hi] with tools. A second tools request [hello, other, x] joins thread 1. On plain main it gets its own thread. The PR breaks this.
  • A tool-less request that shares n-1 messages with a main thread would also become a side thread instead of a new thread. This changes #426 behaviour.

Mutations

  • Not run on the PR logic. The fix is redundant, and I found the regression above directly.

Non-blocking

  • If Osher wants to keep test coverage: add the tools variant of TestAReplacedTrailingSystemReminderDoesNotSplitTheThread to main. It passes there and guards #519's behaviour.

@OsherElhadad

Copy link
Copy Markdown
Collaborator Author

Superseded by #519, which already keeps the thread when Claude Code replaces its trailing system message (with the tools declaration every real request carries). Tolerating only the replaced last message, as this PR does, would also merge separate conversations: [hello, hi] and [hello, other, x] would join into one thread, where main keeps them apart. The test from this PR is kept as a regression test for #519 in #559.

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.

2 participants