Repository navigation
fix(thread): a replaced trailing system message no longer starts a new thread every turn - #499
OsherElhadad wants to merge 1 commit into
Conversation
…w thread every turn Signed-off-by: Osher-Elhadad <[email protected]>
|
Reviewed. |
OsherElhadad
left a comment
There was a problem hiding this comment.
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/threadand the proxyThreadtests pass on a local merge with current main,go vetis 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
left a comment
There was a problem hiding this comment.
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 ininternal/thread/thread_test.go.thread.gomerges cleanly. - I ran the PR's two tests on
main(eval box 1,-race).TestAReplacedTrailingSystemReminderDoesNotSplitTheThread: fails on main. Its requests have notools.TestATrailingMessageTolerance_DoesNotMergeADifferentConversation: passes on main.
- I made a copy of the first test with one
toolsentry 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 > nguard. Any thread withn >= 2is joined by a request that shares only its first message. - No
toolscheck, noheaderThreadcheck, no model tie-break.
- No
- 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 plainmainit 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
toolsvariant ofTestAReplacedTrailingSystemReminderDoesNotSplitTheThreadtomain. It passes there and guards #519's behaviour.
|
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: |
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_idwas 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 byextract_llm_sweep,cache_aware_summarizerandextract_llm_ride; the per-thread keep-alive candidate) found nothing: the ride loggedride_no_prefixon 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) andTestATrailingMessageTolerance_DoesNotMergeADifferentConversation../proxytests pass.