Skip to content

fix(#2701): threads continue-turns survive the compress loop - #2718

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-10_threads-continue-loop-fix
Oct 11, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-10_threads-continue-loop-fix

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

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 in buildRequest):

400 The requested \previous_message_id` has already been continued. Each response `id` may be continued at most once`

Turn dead, fold lost, ~210k input tokens wasted per hit.

The same rebuild also re-stamps system with bytes the steady prepare path never sends (appendSystemText(systemPrompt, originalSystem) + stamp vs injectSystem + #1085-anchored stamp), poisoning the per-thread fingerprint — after every fold the next client continue mismatched with differing 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)

  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, the three nudge retries, the degraded thinking-strip retry) is rebuilt delta-shaped: 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 #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's 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). Covers both client id-beliefs (round-1 message_start id and tail id from message_delta).
  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" — 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 → create rewrite (variant B), tracked for the storage proposal in #2671.

Tests

tests/issue2701-threads-continue.test.ts (5, all passing):

  • re-request continues from msg_r1 with a single-message tool_result delta and byte-identical system/tools; ids persisted as {tail: msg_r2, consumed: [msg_r1]}; terminal message_delta carries the tail id
  • blind retry legal before any id observed (byte-identical re-send); barred after (no re-fetch, client-visible error)
  • create keeps the full-rebuild path
  • thread-tracking unit tests (roll, cap, rewrite-to-tail, unknown-id passthrough, create reset)

Targeted 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.ts reproduce identically on vanilla master (exit=124, 37 failures) — environmental (port/spawn/net), unrelated to this change.

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-10_threads-continue-loop-fix (38c1a89)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2960-pr.2718.4ac22eb

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2718.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2718 review — 可合并(当前 head 1d9b53e68 CI 29/29 全绿)

先说结论:CI 唯一红项 unused-exports 的根因我已定位并本地复现;作者随后已推入逐字节相同的一行修复(1d9b53e68)。我这边准备的同款修复因会与作者提交撞车(推上去是 non-fast-forward、需 force-push 覆盖他人提交——禁止)而未推送,以作者的为准。

结构化八问:

  1. 是什么问题:Anthropic Threads(Claude Code,thread:{type:"continue"})会话里,bili 压缩循环用内部 round-2 抓取把客户端的 continue 目标 id 消费掉了,旧的整包重建重发把这个"已被消费一次"的 id 又发了回去 → 上游 400「previous_message_id has already been continued」;同时 system/tools 被以不同方式重打 → 线程指纹漂移,客户端下一次 continue 再 400「differing field(s): system, tools」。PR 报告单次命中烧掉约 210k input token,会话直接断。
  2. 什么原因导致的:round-2 重建路径默认"请求体可从 kernel 视图自由重建"({...requestBody} 再展开 + 对 coreMessages 调 buildRequest)。这个假设在 threads 上不成立——会话在服务端、按 previous_message_id 键控而非消息数组,且每个 response id 只能被 continue 一次;于是重发已消费目标必然非法,任何 system/tools 字节漂移都会毒化指纹。修法是改"发什么"(疏),不是拦症状。
  3. 影响范围与严重程度:走 Anthropic 协议且用服务端 Threads 的客户端(复现于 Claude Code 2.1.295+),一旦会话中途触发压缩即中招。严重度=会话级中断(turn 直接 400)。OpenAI/Responses/Gemini wire 无此形状,不受影响。
  4. 怎么修复的(variant A,仅 anthropic,零新增配置):① adapter 记录每轮 message_start id,终帧上报 tail id;② 每次循环重发(主发 + nudge 重试 + thinking-strip 重试)都改造成 delta 形状——从最新观测到的上游 id 继续、只带循环自己新增的内容(tool_result / 检索注入;visibility marker 排除)、system/tools 及其余字节与 round 1 完全一致;③ 观测到的 id 落 session.metadata,入站 continue 若指向被代理消费过的 id 就改写到当前 tail;④ 一旦本轮已见 response id,启动横幅硬编码 raw-HTTP-capture(on) 而真开关是 ACP_DUMP_BODY(默认关):日志永远撒谎,取证被带偏过 #413 盲截断重发被禁;⑤ CREATE 轮保留旧整包重建(它是把 fold 送达服务端的唯一通道)。
  5. 是否有回归问题:审过。master 侧自 merge-base 以来的改动只碰 prepare-anthropic.ts([Bug][Responses] acp_turn_sep 合成分隔符进入持久压缩覆盖集合,导致重复历史漂移误报 #2627 Responses turn-separators,不同 wire 区域/不同关注点),auto-merge 干净;两种压缩模式都过了一遍——本改动以 anthropic thread.continue 为门,非 threads 的 anthropic 轮次与其它协议走原路径不变;无 fail-fast 变吞错、无阈值/时序/输出格式漂移。有意行为变更(即修复本身):anthropic threads continue 轮的 round-2 重发由「整包重建 + 重发已消费目标」→「delta 形状 + 从最新 id 继续 + 指纹稳定」,入站被消费的 continue 目标会被改写到 tail。CI 那一个红项是纯卫生问题(unused export),行为中性,已修。
  6. 是否破坏缓存:不破坏,反而保护——跨重发保持线程指纹(system/tools)字节稳定,正是 threads 场景下维持前缀缓存连续性的关键。
  7. 是否改了设计方向:无架构转向。新增一个小记账模块 thread-tracking + 一条针对单协议特性的 wire 分支。已知限制(作者已注明):CONTINUE 轮内发生的 fold 在下一次 create 前不会反映到服务端;continue 上无法做紧急截断。完整服务端折叠留待 variant B(Storage layer: four concrete defects (filename coupling, multi-writer heuristic, non-atomic content-store write, immortal compressed sessions) #2671 存储提案)。
  8. 是否引入新配置:无。显式零新增 config/env/CLI 面(variant A 约束)。

关于那个 CI 红项:失败的 run 里 test job 显示 skipped 不是配置选择——它 needs: [version-guard, kernel-guard, pi-subagents-guard, lane-guard, docs-sync, docs-coverage, unused-exports],gate 一挂整个 test(typecheck + kernel/pi-subagents 套件 + 全量 npm test + build)就被级联跳过,所以那次失败 run 里其实没有任何单测真正跑过。现在 1d9b53e68 这版 29/29 全绿,含 unused-exports 和 4 条 test 矩阵腿(ubuntu/windows × node22/24),新加的那组 threads 回归测试也是在这次才真正执行并通过。测试质量我看过了,是有牙的(byte-equality 断言钉住重发体 / 线程目标 / tracking 状态 / 终帧 id / 盲重试合法与否 / create 全量重建 / roll-cap-rewrite-reset 单元)。

判定:代码方向正确、测试兜底到位、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.
@ranxianglei
ranxianglei force-pushed the 2026-10-10_threads-continue-loop-fix branch from 1d9b53e to 38c1a89 Compare October 11, 2026 03:02
@ranxianglei

Copy link
Copy Markdown
Owner Author

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.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Rebase 复核通过 ✅

Rebased head 38c1a891 我独立核了一遍:

回归判定(Q5):rebase 引入回归 = 无。 唯一共享文件的改动落在不相交区域,既有行为不变。注:master 又前进到 0fa5fd508(#2720),PR 现落后其 2 个提交,但如上所述无冲突文件,应可干净合并(GitHub 的 mergeable 状态在 master 移动后还在异步计算中)。

判定:可合并(等那 4 条 test 腿跑完即全绿)。我不合并 PR,请自行 merge:#2718

@ranxianglei
ranxianglei merged commit 9e95c32 into master Oct 11, 2026
29 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.256-master.9e95c32 (rolling master tag).

npm install -g billion-context@0.0.256-master.9e95c32  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant