Skip to content

[PR] fix: threads-lane reforge — model-reply capture, client-shaped side-call, rejected-create cooldown - #3746

Open
ranxianglei wants to merge 4 commits into
masterfrom
2026-10-19_threads-reforge-assistant-capture
Open

ranxianglei wants to merge 4 commits into
masterfrom
2026-10-19_threads-reforge-assistant-capture

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #3744

Three defects kept the opt-in threads reforge (BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1) unusable on Claude Desktop (cert-MITM, embedded CC 2.1.299, subscription OAuth). All three were verified at code level before fixing; the reporter's local variant-B experiment is the evidence base for fix 1.

Fixes

1. Summary side-call always 429. It was hand-built: four whitelisted headers plus a bili-authored system block — the upstream rejected that shape under subscription OAuth while the client's own requests passed. The side-call is now rebuilt from the client's own request: every client header passes through except hop-by-hop / host / content-length / accept-encoding / x-bili-* / one-shot ids (x-client-request-id, anthropic-dispatch-id, x-claude-code-prompt-id); the captured original system rides verbatim (attribution + identity intact, prefix-cache friendly); summarizer instructions move to the end of the user message. Old shape remains as fallback when no system was captured.

2. Reforged create 400 — messages.N: role 'system' must precede an 'assistant' message or end the array. The mirror only stored client-sent deltas (user + system). CC 2.1.299 sends a mid-conversation system each turn, and model replies live server-side in the Thread — so rebuilt arrays were user system user system …, which violates the placement rule (a system is only legal when followed by an assistant message or at array end). Assistant replies are now captured into the mirror from the response stream bili already forwards: text + tool_use reassembled from SSE content_block_* deltas or parsed from the JSON body; thinking/redacted_thinking blocks are excluded (signature-verified, #1960 family). Side benefit: fold summaries now reflect what the model actually did, not just what the client sent.

3. Summarize→rebuild→400 loop. When the swapped create is rejected upstream, the mirror rolls back to its pre-reforge view (the server-side thread never changed) and arms the existing 10-min side-call cooldown; subsequent over-window turns forward plain until it expires. Previously only side-call failures armed it, so a summary-success/create-rejected pair looped every turn (20–40s + a big Opus call per round).

Behavior changes (disclosed)

All inside the opt-in rollout feature — no change for anyone without BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1:

  • mirror size estimates rise (model replies are now counted) → reforge triggers earlier and reflects true thread growth;
  • summary side-call wire shape changed as in fix 1;
  • new cooldown trigger on rejected reforged create (fix 3).

No config/env surface added. No kernel/ changes.

Tests

tests/threads-lane.test.ts: the fake upstream now enforces the production placement rule on full-history creates (validator pinned both directions), records side-call headers, and serves SSE text/tool_use plus configurable non-stream replies. New tests: SSE reply reassembly (split deltas → text + tool_use input), non-stream capture with thinking filtered out, variant-B side-call shape/header assertions (pass-through vs stripped), CC-style mid-conversation-system placement regression (the pre-fix rebuild 400s inside the fake), rejected-create rollback + cooldown (no second side-call, fail-open continue). Existing mirror-count asserts updated for captured replies. tests/issue2701-threads-continue.test.ts unchanged and green. Local targeted runs green; full-suite verdict = CI.

Docs

README en/zh: desktop threads enablement conditions appended to the #2290 known-limitation paragraph (field report from #3744, paraphrase of CC 2.1.299 minified code).

@ranxianglei ranxianglei changed the title fix: threads-lane reforge — model-reply capture, client-shaped side-call, rejected-create cooldown [PR] fix: threads-lane reforge — model-reply capture, client-shaped side-call, rejected-create cooldown Oct 18, 2026
@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-19_threads-reforge-assistant-capture (6311c31)

Latest build — install this exact version

npm install -g billion-context@0.0.6249-pr.3746.eac6e14

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

Recent builds (commit → version)

commit version build
6311c31 billion-context@0.0.6249-pr.3746.eac6e14 build

Download artifact

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

Install instructions are refreshed on each push.

ranxianglei pushed a commit that referenced this pull request Oct 18, 2026
…es, decoder flush at stream end (#3744)

Review of #3746 (Closes #3744): a completed reply carrying only signature-verified thinking blocks (or aborted before any content event) left NO mirror entry, so every later reforge rebuild hit the system-placement 400 again after each cooldown cycle. Such replies now mirror a self-describing placeholder assistant slot (SSE + JSON paths); the stream reader also flushes the TextDecoder at EOF so a trailing multi-byte character can't drop the final frame. The lane-local placement validator moves to wire-contract-fakes.ts as the shared firstAnthropicThreadsPlacementViolation export.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重结论: 不重复 — open 条目里提及 threads reforge 的只有本 PR、原始 #3744、以及无关的 #3559;上一个相关修复 #3569 已合入 master。本 PR 是 #3744 的实现线程,无并行工作。

标签: bug / claude-code / scope/kernel / p1(缺陷类;涉及 Claude Desktop 内嵌 CC 2.1.299;属上下文管理/压缩引擎域;开关打开时功能完全不可用 → p1)。

独立重审了本 PR(逐文件读 diff + 对既有行为逐项审计,未复用 issue 侧的分析)。三个修复均在代码层面验证成立;审查中另发现 一个真实缺陷、一个小缺陷、一个账本缺口,按「小问题直接修」已直接在分支上修掉(两个标注 commit,原作者署名保留):

结构化八问

  1. 是什么问题: opt-in 的 threads reforge(BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1)在 Claude Desktop 上完全不可用([Bug][threads lane] reforge still unavailable after #3569: summary side-call 429 (with viable fix experiment) + mirror m | [Bug][threads lane] #3569 之后 reforge 仍不可用:摘要 side-call 429(附可行修法实验)+ mirror 缺 assistant 回复致 create 400 循环;附 Claude Desktop 难以适配的结论 #3744):summary side-call 恒 429、reforged create 恒 400、且 400 后每轮循环。本 PR 修这三个缺陷;审查对象 = 修复是否正确、完整、无回归。
  2. 什么原因导致的(三个根因,均已在代码层面复核): ① side-call 是手搓的(白名单 header + bili 自撰 system),subscription OAuth 下被上游拒绝,而客户端自己的请求形态能过;② mirror 只存 client 侧 delta(user+system),模型回复活在 Thread 服务端从未被记录,而 CC 2.1.299 每轮都在会话中途发 system → 重建数组变成 user system user system …,违反 placement 规则(system 必须后面跟着 assistant 或位于数组末尾);③ cooldown 只在 side-call 失败时 arm,create 被拒不 arm → summarize→rebuild→400 每轮循环(20–40s + 一次大 Opus 调用)。
  3. 影响范围与严重程度: 只影响显式打开该 opt-in flag 的用户;对他们而言功能从「完全不可用」变为可用(功能范围内 p1);功能范围外零影响(rollout-off 路径不变)。
  4. 怎么修复的: PR 的三个修复——独立验证成立: variant-B header 透传/剥离清单(含三个 one-shot id)正确;响应流捕获挂在 relay.ts:1336-1346(仅 2xx-with-body 可达);noteReforgeCreateRejected 确认位于 !upstream.ok 错误分支内部,成功路径不可能误触发;回滚保留 entries、恢复 meta。我的补充修复: A(真实缺陷) thinking-only 或在首个内容事件前中断的回复原来完全不进 mirror → 留下永久 placement 缺口 → 每次 cooldown 到期后 reforge 再次 400 死循环 → 现在记一条自描述的占位 assistant 条目(SSE+JSON 两条路径,+2 个测试);B 流读取结束缺 TextDecoder flush,可能丢最后一个跨块多字节字符 → 一行修复; C 排查了异步 capture 与下一轮 sync capture 的竞争 → 验证不是 bug(每次 mirror RMW 在 load/persist 之间无 await,Node 微任务顺序 + 客户端串行性保证不交错,不加投机性的队列); D 生产 placement 规则按 [CI/testing] wire-contract anti-regression system: validation-parity fake upstreams + golden schema snapshots + constraint ledger + staged 4-protocol x 3-mode client smoke matrix #1304 提升为 ledger WC-022(lane 范围),校验器去重为共享导出。
  5. 是否有回归问题: 逐项审过既有行为: rollout-off 路径不变; legacy-storage passthrough 不变; fail-open 路径(side-call 失败/cooldown/create 被拒)全部保持 fail-open;thinking/redacted_thinking 排除在 SSE+JSON 两条路径都保持([acp-loop] In-turn compress re-request edits the latest assistant message → Anthropic 400 "thinking blocks … cannot be modified" (session stays broken) #1960 族不变量完好);typecheck clean;定向套件 43/43(threads-lane + wire-contract + issue2701);全量 CI green。无无意回归。
  6. 是否破坏缓存: 否。side-call 现在原样携带客户端自己的 system(attribution+identity 完整,prefix-cache friendly——比旧的 bili 自撰 system 更好);mirror 存储布局不变(threads:mirror:* 键);message-ref/id 逻辑未触碰。
  7. 是否改了设计方向: 否。仍是同一套 mirror→budget→reforge 架构、同一个 opt-in rollout 门;只是补上了缺失的数据(assistant 回复)并加固了失败路径(rejected-create cooldown)。
  8. 是否引入了新的配置: 否。无新 env var / config 字段 / CLI flag。

行为变更披露(全部限定在 flag 之内,旧值 → 新值)

行为 旧 新 原因
mirror 大小估算 只计 client delta 计入捕获的模型回复 反映真实 thread 增长(有意)
side-call wire 形态 bili 手搓 header + system 客户端 header 透传(除 hop-by-hop/host/content-length/accept-encoding/x-bili-*/one-shot ids)+ 客户端原 system 原样 + summarizer 指令移到 user 消息尾部 旧形态被 subscription OAuth 拒绝(即修复①本身)
cooldown 触发 仅 side-call 失败 增加 rejected reforged create 打破每轮循环(修复③)
thinking-only 回复捕获 (本 PR 新增行为)不入 mirror 记一条自描述占位 assistant 条目 防永久 placement 缺口(我的修复 A)
流读取收尾 无 decoder flush EOF flush 尾字节 防丢尾部多字节字符(我的修复 B)

CI

新 head 8e11fa00c: 30/30 lanes green(test shards × ubuntu/windows × node 22/24 + static gates + kernel/pi-subagents/version/lane guards)。push 前本地定向验证已通过(typecheck + 43/43)。

判定: 可合并 — 三个修复方向正确、修法到位(含我的两处补充),回归审计干净,CI 全绿。合并留给你: #3746

@ranxianglei
ranxianglei force-pushed the 2026-10-19_threads-reforge-assistant-capture branch from 8e11fa0 to e7c12bb Compare October 20, 2026 11:43
ranxianglei pushed a commit that referenced this pull request Oct 20, 2026
…es, decoder flush at stream end (#3744)

Review of #3746 (Closes #3744): a completed reply carrying only signature-verified thinking blocks (or aborted before any content event) left NO mirror entry, so every later reforge rebuild hit the system-placement 400 again after each cooldown cycle. Such replies now mirror a self-describing placeholder assistant slot (SSE + JSON paths); the stream reader also flushes the TextDecoder at EOF so a trailing multi-byte character can't drop the final frame. The lane-local placement validator moves to wire-contract-fakes.ts as the shared firstAnthropicThreadsPlacementViolation export.
awork added 3 commits October 20, 2026 19:45
…ary side-call, cooldown on rejected create (#3744)

Three defects kept the opt-in threads reforge unusable on Claude Desktop
(cert-MITM, embedded CC 2.1.299, subscription OAuth):

1. Summary side-call always 429: it was hand-built with four whitelisted
   headers and a bili-authored system block. Rebuild it from the client's own
   request instead — pass every client header through except hop-by-hop /
   host / content-length / accept-encoding / x-bili-* / one-shot ids
   (x-client-request-id, anthropic-dispatch-id, x-claude-code-prompt-id),
   ride the captured original system verbatim (attribution + identity
   intact), and append the summarizer instructions to the user message.
   Shape proven by the local variant-B experiment in #3744.

2. Reforged create 400 "role 'system' must precede an 'assistant'": the
   mirror only stored client-sent deltas (user+system); mid-conversation
   systems were never followed by an assistant because model replies live
   server-side in the Thread. Capture assistant replies (text + tool_use,
   reassembled from SSE deltas or the JSON body; signature-verified thinking
   blocks excluded per #1960) so rebuilt creates satisfy the placement rule.

3. No cooldown when the swapped create is rejected: the mirror now rolls back
   to its pre-reforge view and arms the existing 10-min side-call cooldown,
   ending the summarize→rebuild→400 loop that cost 20–40s per turn.

Behavior changes (opt-in feature behind BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE):
- mirror size estimates rise (replies counted) → reforge triggers earlier and
  reflects true thread growth;
- summary side-call wire shape as above;
- new cooldown trigger on rejected reforged create.

Docs: README en/zh note the desktop threads enablement conditions (field
report, #3744). Tests: threads-lane suite extended (SSE/JSON reply capture,
side-call shape+headers, placement regression both directions, rejected-create
rollback+cooldown); issue2701 suite still green.

Closes #3744
…es, decoder flush at stream end (#3744)

Review of #3746 (Closes #3744): a completed reply carrying only signature-verified thinking blocks (or aborted before any content event) left NO mirror entry, so every later reforge rebuild hit the system-placement 400 again after each cooldown cycle. Such replies now mirror a self-describing placeholder assistant slot (SSE + JSON paths); the stream reader also flushes the TextDecoder at EOF so a trailing multi-byte character can't drop the final frame. The lane-local placement validator moves to wire-contract-fakes.ts as the shared firstAnthropicThreadsPlacementViolation export.
…t rule gate (#3744)

Per #1304 the production constraint learned in bili #3744 becomes a permanent WIRE_RULES entry: WC-022 (wire anthropic, lane anthropic-threads) — a full-history Threads create 400s when any system message is not followed later by an assistant message and does not end the array. Lane-scoped enforcement stays in the lane's own fake upstream (tests/threads-lane.test.ts pins the validator both directions), with the probe row added to the laneProbes gate table.
@ranxianglei
ranxianglei force-pushed the 2026-10-19_threads-reforge-assistant-capture branch from e7c12bb to c7cfc32 Compare October 20, 2026 11:47
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

上游冲突已解决:PR 分支已 rebase 到最新 master(77e2eb4b1),新 head c7cfc323a(3 个提交原样保留,作者署名不变)。GitHub 当前 mergeable: true,mergeable_state: blocked = 仅剩 CI checks 未跑完,冲突状态已清除。

冲突实况:整个窗口只有一处真实文本冲突——README en/zh 的 claude 原生姿态条目同一行:master 侧(#1821)加了「MCP shell 会话内守护代理、死亡时自行重生(~90s 恢复)」一句,本 PR 侧在句尾追加了 #3744「桌面端 Threads 暴露面」段落。逐字 diff 确认两侧除这两处增量外完全一致 → 按并集解决(两句都保留),无取舍。其余 7 个文件(src/server.ts、relay.ts、handle.ts、threads-lane.ts、三个测试文件)全部自动合并。rebase 过程中发现并修掉一个排版瑕疵(并集拼接丢了 . ** 之间的一个空格),已 fixup 进第 1 个提交,diff 净零影响。PR 相对 base 的 diff 与 rebase 前逐字节一致(9 files, +574/−32)。

结构化八问

  1. 是什么问题:系统检测到本 PR 与 master 冲突(dirty),分支无法合并;需要把 master 并进 PR 分支解冲突。
  2. 什么原因导致的:分支点之后 master 合入了约 50 个提交(web 会话浏览器 [PR] feat(web): add session filters and pagination #3566、Dockerfile [PR] feat: add Dockerfile + docs for container deployments (#3389) #3392、autofold 确定性 [PR] test: use saturation barrier to make queue-drop test case deterministic (fixes #3708) | test: 用饱和屏障让 queue-drop 用例确定性(修 #3708) #3709、Hook-model clients (claude/zcode) have no mid-session self-heal when the proxy dies — ECONNREFUSED until next session #1821 MCP shell 守护文档等),其中 Hook-model clients (claude/zcode) have no mid-session self-heal when the proxy dies — ECONNREFUSED until next session #1821 的文档句与本 PR 的 [Bug][threads lane] reforge still unavailable after #3569: summary side-call 429 (with viable fix experiment) + mirror m | [Bug][threads lane] #3569 之后 reforge 仍不可用:摘要 side-call 429(附可行修法实验)+ mirror 缺 assistant 回复致 create 400 循环;附 Claude Desktop 难以适配的结论 #3744 段落落在 README 同一条 bullet 上 → 单行文本冲突;纯时序竞争,无任何语义对立。
  3. 影响范围与严重程度:仅影响本 PR 的可合并性(流程层面);对任何用户零运行时影响。
  4. 怎么修复的:rebase 到最新 master(期间 master 又前进了两次,最终钉在 77e2eb4b1);唯一冲突按「两侧增量都是有效内容」取并集解决;代码零改动、零取舍。
  5. 是否有回归问题:无。rebase 不改变 PR 内容(diff stat 前后一致);新 master 提交与本 PR 的 9 个文件零重叠(web/admin/docs/Dockerfile/advisory 域);typecheck clean;定向套件 43/43 绿(threads-lane + wire-contract + issue2701)。全量判定以 CI 为准。
  6. 是否破坏缓存:不涉及——无线条/存储/重试策略变更。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

判定:冲突已清除,可进入 CI→人工合并。合并仍留给你:#3746

# Conflicts:
#	README.md
#	README.zh-CN.md
#	src/server/relay.ts

This branch has not been deployed

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

Projects

None yet

1 participant