Repository navigation
fix(#2701): threads continue-turns survive the compress loop - #2718
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2960-pr.2718.4ac22ebThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2718.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 PR #2718 review — 可合并(当前 head 先说结论:CI 唯一红项 结构化八问:
关于那个 CI 红项:失败的 run 里 判定:代码方向正确、测试兜底到位、CI 全绿、干净 auto-merge(落后 master 31 个提交但无冲突)。可合并。 我无权合并 PR,请自行 merge:#2718 |
With anthropic Threads (`thread:{type:"continue",previous_message_id}`,
Claude Code 2.1.295), the conversation lives server-side and the request
body carries only the turn's 1-2 delta messages. The loop's internal
round-2 fetch consumes the client's continue target, and the legacy
re-request re-sent exactly that target via the `{...requestBody}` spread
in buildRequest: upstream 400 "The requested `previous_message_id` has
already been continued", turn dead, fold lost. The same rebuild also
re-stamps system with different bytes than the steady prepare path ever
sends, poisoning the per-thread fingerprint — after every fold the next
client continue mismatched with "differing field(s): cache_control,
system, tools" (corrects #2648's P3 note: folds ARE inherited once a
create delivers them; the gap was fold-only-deliverable-at-create plus
this drift).
Four coordinated changes (anthropic-only, no new knobs):
1. adapter-anthropic.ts: record EVERY round's message_start id
(threadTailId + threadResponseIds, optional CompressLoopAdapter
hooks); the terminal message_delta reports the tail id so the next
client continue targets a live response.
2. core.ts: on a threads-continue turn, every re-request (main
re-request, three nudge retries, degraded thinking-strip retry) is
rebuilt delta-shaped — thread.previous_message_id = latest round id,
messages = coreToAnthropic(unsent delta) — where the delta carries
only the loop's client-invisible additions since the last accepted
send (proxy tool_results + retrieval injections; assistant output
already lives server-side; visibility markers excluded — a server
thread is append-only and would carry the client noise forever).
system/tools/all other bytes stay identical to round 1 (fingerprint
stable). The #413 blind truncation retry is barred once the sent
body's target is provably consumed (an id was observed since the
send); pre-start truncations keep it.
3. thread-tracking.ts: persist observed ids into
session.metadata.anthropicThreadTail {tail, consumed[]} (capped 20,
recorded in the loop finally so consumed ids survive turns that die
mid-round); prepareAnthropic rewrites an inbound continue whose
target sits in the consumed set to the tracked tail (unknown ids
pass through untouched — restart/foreign-route safe; create resets
tracking).
4. create turns keep the legacy full rebuild — a create re-sends the
whole (possibly folded) history, the only channel that can deliver a
fold to the server-side thread.
Known limitation (documented in code): a fold executed during a
continue turn is not reflected server-side until the next create
("silent lie"); emergency truncation stays impossible on continues.
tests/issue2701-threads-continue.test.ts: 5 tests — re-request
continues from msg_r1 with a single-message tool_result delta and
byte-identical system/tools; {tail: msg_r2, consumed: [msg_r1]}
persisted; terminal message_delta carries the tail id; blind retry
legal before any id (byte-identical re-send), barred after; create
keeps full-rebuild; thread-tracking unit tests (roll, cap, rewrite,
reset). Targeted regression: 268/268 across loop/anthropic/relay/
prepare/session test files. The claude-native/dsh-native plugin-mode
failures in this environment reproduce identically on vanilla master
(exit=124, 37 failures) — environmental, unrelated.
…ype, unused-exports gate)
1d9b53e to
38c1a89
Compare
|
Rebased onto current master 76143a8 (was based on a24f6b6, pre-#2715/#2722). Clean rebase — no conflicts. Re-verified on the rebased head: typecheck clean; branch suite tests/issue2701-threads-continue.test.ts 5/5; adjacent anthropic/loop/wire/claude-mitm 35/35; anthropic lane + cache-proof 37/37; build OK. Head is now 38c1a89. |
[bot] 🏷 Rebase 复核通过 ✅ Rebased head
回归判定(Q5):rebase 引入回归 = 无。 唯一共享文件的改动落在不相交区域,既有行为不变。注:master 又前进到 判定:可合并(等那 4 条 test 腿跑完即全绿)。我不合并 PR,请自行 merge:#2718 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.256-master.9e95c32 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2701.
Root cause (two stacked bugs)
With anthropic Threads (
thread:{type:"continue",previous_message_id}, Claude Code 2.1.295), the conversation lives server-side; the request body carries only the turn's 1–2 delta messages. The compress loop's internal round-2 fetch consumes the client's continue target, and the legacy re-request re-sent exactly that target ({...requestBody}spread inbuildRequest):Turn dead, fold lost, ~210k input tokens wasted per hit.
The same rebuild also re-stamps
systemwith bytes the steady prepare path never sends (appendSystemText(systemPrompt, originalSystem)+ stamp vsinjectSystem+#1085-anchored stamp), poisoning the per-thread fingerprint — after every fold the next client continue mismatched withdiffering field(s): cache_control, system, tools. This also corrects #2648's P3 note: folds are inherited by the server thread once a create delivers them; the limitation was fold-only-deliverable-at-create plus this drift.Fix (variant A — thread-aware loop, anthropic-only, no new knobs)
message_startid (threadTailId/threadResponseIds, optionalCompressLoopAdapterhooks); the terminalmessage_deltareports the tail id, so the next client continue targets a live response.thread.previous_message_id = latest round id,messages = coreToAnthropic(unsent delta). The delta carries only the loop's client-invisible additions since the last accepted send (proxy tool_results + retrieval injections; assistant output already lives server-side in r_k; visibility markers deliberately excluded — a server thread is append-only and would carry the client noise forever).system/tools/all other bytes stay identical to round 1 → fingerprint stable. The#413blind truncation retry is barred once the sent body's target is provably consumed (an id was observed since the send); pre-start truncations keep it.session.metadata.anthropicThreadTail {tail, consumed[]}(capped 20, recorded in the loop'sfinallyso consumed ids survive turns that die mid-round);prepareAnthropicrewrites an inbound continue whose target sits in the consumed set to the tracked tail (unknown ids pass through untouched — restart/foreign-route safe;createresets tracking). Covers both client id-beliefs (round-1message_startid and tail id frommessage_delta).Known limitation (documented in code)
A fold executed during a continue turn is not reflected server-side until the next create ("silent lie" — the loop's local view is folded, the thread keeps the pre-fold text). Emergency truncation stays impossible on continues for the same reason: the real context lives server-side. Full server-side folding on continue turns needs the request-path mirror +
continue → createrewrite (variant B), tracked for the storage proposal in #2671.Tests
tests/issue2701-threads-continue.test.ts(5, all passing):msg_r1with a single-messagetool_resultdelta and byte-identical system/tools; ids persisted as{tail: msg_r2, consumed: [msg_r1]}; terminalmessage_deltacarries the tail idTargeted regression: 268/268 across all loop-/anthropic/relay/prepare/session test files. The plugin-mode failures this environment shows in
claude-native.test.ts/dsh-native.test.tsreproduce identically on vanilla master (exit=124, 37 failures) — environmental (port/spawn/net), unrelated to this change.