Repository navigation
[PR] fix(loop): hold a bili-owned round's thinking until its signature, so a cut inside round ≥2 stays re-fetchable - #4681
Conversation
… a cut inside round >=2 stays re-fetchable (ranxianglei#4679) A proxy tool (compress …) makes bili re-request on the client's behalf (round >=2) after it has already streamed its own receipt as assistant TEXT, so the client cannot replay that turn at all. When the upstream cut landed inside a thinking block, the block's start + deltas were already on the client's wire unsigned, ranxianglei#2501's unsignedThinkingBarsRefetch refused both re-fetch shapes, and the !sawDone branch ended the turn with `event: error` — unrecoverable, and the fold the user paid for was already committed. Round >=2 is bili's OWN round, so its truncated attempt must leave no client state. On an upstream evidenced to sign (signed thinking in history, or an earlier round of this request), the adapter now buffers a round-2 thinking payload off the wire and reports it to the loop as accounting only (`withheld: true`, still feeding the re-request rebuild); `signature_delta` releases start + payload + signature together as ONE block, and a `content_block_stop` with no signature discards the buffer (on an evidenced signing upstream a signature-less block is invalid history by definition). The adapter self-arms the hold the moment a parse sees a signature. Death before the signature therefore leaves nothing behind, so the existing ranxianglei#413 blind re-fetch of the same round fires and heals the turn. Never-signing relays (GLM/DeepSeek anthropic-compat) have no evidence and keep streaming live; ranxianglei#2501's round-1 behaviour and its gate are untouched. tests/anthropic-round2-thinking-hold.test.ts: T1/T4 fail on d1b46fc (one fetch call, turn ends in error / payload leaked) and pass here; T2 (relay keeps streaming, hold must NOT arm) and T3 (one signed block) pass both ways.
(ai: vllm-qwen/qwen3.8-27b) 查重:不重复。 检索了 open+closed 的 issue 与 PR,没有针对「bili 自有轮(round≥2)thinking 截断后不可恢复」这一具体缺陷的既有项。PR 依赖的 #2501( 一句话结论:代码本身正确、测试有牙、方向对;唯一拦路的是一个几行级 CI 红( 结构化八问1. 是什么问题 2. 什么原因导致 3. 影响范围与严重程度 4. 怎么修的 5. 是否有回归问题(逐条过触碰路径的既有行为)
6. 是否破坏缓存 7. 是否改了设计方向 8. 是否引入新配置
|
(ai: vllm-qwen/qwen3.8-27b) 状态收尾(详细八问审核见本线程我上一条评论,这里只报结论与剩余一步):
等作者处理那一行后即可收工。 |
…ate flags it INTERNAL_ONLY Adapters receive the opts object contextually through CompressLoopAdapter.parseStream; no module imports the name, so the export adds API surface the gate (ranxianglei#1440) rejects. Type-only change, zero behavior impact. (CI fix for ranxianglei#4681)
(ai: vllm-qwen/qwen3.8-27b) 查重: 不重复 —— open/closed issue 与近期 PR 中均无针对本 CI 失败( CI 自动修复(第 1 次尝试)已完成并推送: 结构化八问
判定:CI 修复为单行类型可见性调整,不改变 PR 的任何行为;待新 head 的 CI 全绿后即可进入正常 review。 |
|
Merged master On the merged head:
Description updated per the review: the drafting line is removed, and the two-thinking-block edge case is listed under Risk. On #4712: as noted in its thread, it fixes the same #4679 path. Happy for whichever lands to close the other. |
e428e46 to
a220df9
Compare
(ai: vllm-qwen/qwen3.8-27b) 合并后的新 head
关于 #4712 与 #4681 的去重(两者都修 #4679、同为「签名到达前压住 round≥2 thinking」方案):保留哪个、关闭哪个由 owner 决定(按政策 agent 不关闭任何 PR/issue)。供决策的现状快照:
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard sync done: merged |
…-hold # Conflicts: # CHANGELOG.md
|
Merged master |
(ai: vllm-qwen/qwen3.8-27b) Verified independently against the live tree: master tip is |
# Conflicts: # CHANGELOG.md
(ai: vllm-qwen/qwen3.8-27b) Synced origin/master (tip |
…1-merge-guard-r1 # Conflicts: # CHANGELOG.md
ranxianglei
left a comment
There was a problem hiding this comment.
(ai: vllm-qwen/qwen3.8-27b)
Verdict: APPROVE. Correct root-cause fix, real regression teeth, clean scope, behavior change disclosed. No kernel/ changes, no kernel-change declaration.
Independently verified (re-derived from the diff, not taken from the PR text)
- Mechanism, end-to-end. Round ≥2 thinking payload deltas are buffered server-side (
heldThinkingDeltas, per-parse) only when evidence says this upstream signs — signed thinking in the client's history OR a signature already seen this turn (src/loop/core.ts:873). Atsignature_deltathe held start plus the concatenated remapped deltas flush as one burst; if the block closes WITHOUT a signature the whole block is discarded — the client saw zero bytes of it, and the withheld text stays out of replays because rebuild drops unsigned reasoning segments for signing protocols (src/loop/core.ts:1363-1370). - The heal path really fires post-fix. A held block leaves
unsignedThinkingOnWirenet-false (set + clear happen atomically at signature time), so the #2501 gate no longer bars recovery, and the dead attempt forwarded zero visible bytes — exactly the #413 blind re-fetch precondition. A cut after a completed signed block also recovers (blind shape b) at the documented cost of re-streaming block 1. - Teeth proven, not assumed. Ran the new suite against pristine master in a throwaway worktree: T1 and T4 fail there (dead round-2 attempt not re-fetched —
callsexpected 2, actual 1 — turn ends in error), T2/T3 pass. On the PR head everything passes. - Adjacent behavior audited: round 1 byte-identical (hold requires
round > 1) · relay/no-evidence lanes byte-identical (T2 pins the negative direction) ·redacted_thinkingnever held (arrives whole in its start frame) · multiple blocks per round OK (per-index maps) · #2501 / #413 / #1455 resume semantics preserved (held-start map cleared at every parse start; dead attempt's open blocks closed by synthetic stops). - Local evidence: extended suite 6/6, adjacent truncation/thinking suites 38/38, typecheck clean. CI at head
a68014017: 0 failed / 0 pending (mirror check cache).
Findings (non-blocking)
- Duplicate PR — owner decision needed before merging either. #4712 is open with a near-identical fix for this same issue (created 2026-10-25T13:12Z vs this PR 11:08Z; the issue triage comment already cites #4712). Loop guard: one issue ≤ one PR. Agents can't close PRs — please close the later one (#4712) if this merges.
- Doc drift: the PR body says the evidence expression includes
ctx.protocol === "anthropic" &&; the code omits the protocol check. Harmless in practice (only the anthropic adapter implements holding), but body and code should match. - Pre-existing write-only
lastThinkingIndexinparseStream(src/loop/adapter-anthropic.ts:373, never read — dead on master too); this PR adds one more write in the flush path (L626). Left untouched deliberately to keep the diff auto-merge-clean; optional follow-up cleanup.
Added during review (pushed to the PR branch, 4b8c4c64f)
Two regression paths the original four tests did not cover:
- T5 (self-arm): no evidence in history or round 1 — the first round-2 block streams live, its signature arms the hold mid-parse, and a cut inside the second block must still leave zero unsigned bytes for the blind re-fetch. Fails on master.
- T6 (Opus 5.5): start → signature_delta with no payload deltas — the held start must release on the signature alone, or the signature frame would precede its own start on strict clients.
Behavior change (re-confirming the PR's own disclosure)
OLD: on an evidenced-signing upstream, round ≥2 thinking streamed live from the first payload byte. NEW: buffered server-side, released as one burst at signature. Cost bounded by max_tokens per block; relay lanes and round 1 unchanged. Disclosed in the PR body + CHANGELOG ✓.
结构化八问
- 是什么问题:bili 自己发起的第 ≥2 轮请求(执行 compress 后的重发)在流式返回中途被切断时,客户端已持有未签名的 thinking 块,#2501 门禁正确地拒绝重取,回合以 error 终结且不可恢复。
- 什么原因导致的:第 ≥2 轮的 thinking 载荷逐帧实时转发给客户端(只有 start 帧被延迟),而签名帧永远在最后——切点落在载荷中段时,客户端落盘的就是无签名块,重取路径对这种状态按设计封死。
- 影响范围与严重程度:仅代理模式 + 确实会签 thinking 的 anthropic 上游 + 切断恰好发生在 bili 自发起回合(round ≥2)的 thinking 载荷内。触发面窄但后果重:该回合彻底丢失、用户可见 error(p2 定级合理)。中继/不签名上游不受影响。
- 怎么修复的:有签名证据时,round ≥2 的 thinking 载荷先缓冲在服务端(每个 parse 独立缓冲,死尝试的缓冲随其消失),签名帧到达时一次性重映射释放;块未签名即关闭则整块丢弃(客户端零字节),被丢文本由重建层既有规则排除、不回放。
- 是否回归问题:审过的既有行为——round 1 流式、中继车道、redacted thinking、多块响应、#2501/#413/#1455 语义——全部保持;唯一有意变更即上面披露项,T2 负向钉住中继不变。
- 是否破坏缓存:不影响前缀缓存策略;盲重取是既有恢复机制,本修复只是让它在被错误封死的场景下可用。
- 是否改了设计方向:没有。沿用既有 hold/release、blind re-fetch、rebuild-drop 三层机制,把 round ≥2 纳入证据门控的 hold。
- 是否引入了新的配置:没有。无新 env/config/CLI。
Mergeable (human merges). Please resolve the #4712 duplicate first.
[bot] 🏷 Review complete → APPROVE posted (review link). What was done:
Verdict: mergeable (human merges), after resolving the #4712 duplicate. |
|
Restored the two missing regression cases in
Verification on the previous PR head plus this test-only commit:
This push triggers a fresh CI matrix; the earlier cancelled matrix is not being treated as a green run. |
[bot] 🏷 Confirmed and verified against the new head
Reminder before merge: #4712 remains a near-duplicate PR for the same issue (#4679) — owner needs to close one of them (agents can't close PRs). |
Fixes #4679
What
Round ≥ 2 is bili's own re-request (a proxy tool already executed and bili re-sends on the client's behalf). Its truncated attempt must therefore stay invisible, or the client is stuck: round 1 has already committed visible text (the
📦 [ACP] …receipt) to the client's stream, so the client cannot replay the turn itself.Before: an upstream cut inside a round-2 thinking block streamed the block's start + deltas to the client, the signature never arrived,
unsignedThinkingBarsRefetch(#2501) — correctly — refused both re-fetch shapes, and the loop ended the turn withevent: error. Unrecoverable.After: on an upstream that is evidenced to sign thinking, a bili-owned round keeps its thinking payload off the wire until its
signature_delta:historyHasSignedThinking(existing) or a new turn-levelsawSignedThinkingThisTurn(a fresh session whose round 1 signed its thinking — the client's own history had nothing) arms the hold; the adapter additionally self-arms the moment it sees a signature inside the parse.heldThinkingDeltas) and reported to the loop only as accounting (withheld: true), so the re-request rebuild still sees them (it drops unsigned reasoning for signing protocols anyway).signature_deltareleases start + payload + signature together, in onemetaframe per held event, inside the SAME single block — never one block per delta (the 上游截断在 compress 内部重发路径无法恢复:重试闸门将 ping 计为已转发输出,且 emitError 合成 end_turn 伪完成 #1455 / zcode duplicate-thinking trap).content_block_stopwith no signature discards the buffer: on an evidenced signing upstream a signature-less block is invalid history by definition. The loop's reasoning segment is sealed so the next block cannot inherit it.unsignedThinkingOnWireis never set → the existing 启动横幅硬编码 raw-HTTP-capture(on) 而真开关是 ACP_DUMP_BODY(默认关):日志永远撒谎,取证被带偏过 #413 blind re-fetch of the same round fires and heals the turn.Nothing in #2501 changes: its round-1 hold and its gate are untouched. The round-≥2 case simply never reaches the gate with an unsigned block on the wire.
Why not the alternatives
thinking.signature: Field required→ Claude Code strips ALL prior thinking).Tests
tests/anthropic-round2-thinking-hold.test.ts(new, 4 cases, hermetic — mockedfetch, no network):calls === 1, turn ends in error)Commands (this clone, node 24 / linux):
Local full
npm testin this checkout is dominated by pre-existing environment failures: pristined1b46fc0reports the same ~20 (storage engine/golden state digests,storage-orphan-recovery, native-host module evaluation,opencode-native, v2 setup, HTTP fork, #2443/#3278/#3304) — none of them in the anthropic loop path. Between the pristine run and the run on this branch the ONLY differences are my new T1/T4 flipping fail → pass, plus one flakymodule evaluation marks the process as a native {pi,opencode} hosttest whose identity alternates between runs. Full-suite verdict is CI's anyway (AGENTS.md §7).Branch head:
a220df9confix/round2-thinking-hold= the reviewed30c70efaplus a merge of masterbcbd1a3d(no conflicts). Against master the diff is 4 files and touches nothing underkernel/or the lockfiles, sokernel-guardhas nothing to gate. On the merged head:tsc --noEmitexit 0,node scripts/check-unused-exports.mjsclean (6 waived, none added).Risk
max_tokensper block; no cap added.withheldis accounting-only, no wire bytes).parseStreamgained an optional third parameter (ParseStreamOpts, module-private sincef87e96a7); the other adapters are unaffected (fewer params stay assignable) and no call site outside the loop changed.