Skip to content

feat: extend truncation continuation nudge to the anthropic wire (#1470) - #1478

Merged
ranxianglei merged 2 commits into
masterfrom
2026-09-27_anthropic-continuation-nudge
Sep 27, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-09-27_anthropic-continuation-nudge

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

改了什么

把第二道截断闸门(continuation nudge,src/loop/core.ts)从 openai-only 放宽到 openai || anthropic:anthropic 线在「已转发部分可见文本之后」被上游截断时,代理侧现在会用续写 nudge 重取一次(one-shot 预算,与盲重试预算独立),而不是直接落 error channel。同时把该闸门上方已过时的「OpenAI wire only」注释重写为当前依据。responses/google 保持排除(#440 单 created 不变式)。

为什么安全(issue 里两个线格式阻塞的实测结论)

  1. 同轮重解析的 message_start 去重——已由 fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464 的 state-keyed suppression(!messageStartForwarded)覆盖:一个逻辑响应至多一个 start 帧,跨轮次与重试流均成立;
  2. nudge 重发请求体中的 thinking/signature——dead attempt 的部分输出(含无签名 thinking)永不进入历史:提交只在 proxy-tool 轮次发生(core.ts 提交路径),nudge 重试体 = buildRequest([...coreMessages, nudge]) 不含死尝试内容;即使有签名缺失段混入,ACP loop re-request drops reasoning_content, DeepSeek returns 400 invalid_request_error #539 守卫(requiresThinkingSignature 时丢弃无签名段)兜底。zcode replay-400 测试(tests/anthropic-round2-stream.test.ts)是已修好的历史 bug,本 PR 不触碰其路径;
  3. 悬空块——fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464 的 close-at-resume 在重试流开始前关闭 dead attempt 未闭合的块,client index 跨尝试单调递增(实测恢复流为 idx0=注入的 proxy-tool 结果块 → idx1=死尝试文本 → idx2=续写文本,全部闭合、单一 terminal)。

行为变更披露(旧值 → 新值)

验证

依赖说明

本 PR 基于 #1464 分支头(其 adapter 安全网是本改动的前提)。#1464 合并后 rebase 到 master(预期无冲突:只触碰 #1464 自己改过的同一注释/闸门区域)。CI e2e 热文件触发规则对 stacked PR 的适用性以实际运行为准。

Closes #1470

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,这是带 ework-agent-pr 标记的已完成 PR(GitHub #1470),不重做。开始按 review 规范走:拉取分支核对代码、审原 issue #1470、回归清点、检查 CI,稍后给结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #1478 review 结论(按 PR Review 规范逐关走完;已在 head ef64ec40 上独立复跑验证)

① 原 issue(#1470)方向关:合理。anthropic 线在「可见文本已转发后被截断」时两道闸门都拒收(blind retry 要求零可见输出;nudge 闸门硬编码 openai-only),直接落 error channel——同类截断在 openai 线能自愈合,anthropic 不能,方向成立。owner 已在原 issue 拍板「继续」。改动打在决策点(闸门条件本身),不是症状层。

② 回归审查(diff 只碰一个闸门条件 + 注释;触碰路径承担的既有行为逐项判定):

  1. openai 线 nudge 路径:保持(新条件是旧条件的超集,代数不变);
  2. anthropic blind retry(fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464):保持(独立闸门,未触碰);
  3. responses/google 线:零变更(两闸门均排除;textProtocol 仅在 responses 置位 src/server.ts:5361,故 !ctx.textProtocol 对 anthropic 放行、对 responses 仍拦截);
  4. anthropic 可见文本截断→error channel:有意变更(PR 已披露)——旧=直接 emitError,新=先一次 continuation nudge,再次失败走同一路径同一消息形状(src/loop/core.ts:821-833)。失败语义与错误帧形状不变,仅新增自愈合前置步;
  5. degenerate-turn retry([BUG] In some cases, with Codex's auto model + DeepSeek, it will suddenly terminate automatically. #732/workbuddy报错Empty stream: upstream gateway sent only placeholder chunks without any model output (chunks=0, bytes=0) #821):保持(顺序与条件均未动);
  6. 盲重试/nudge 双预算独立性:保持(truncationRetried 与 continuationRetried 各自 one-shot,core.ts:263/269);
  7. 历史提交完整性:保持——dead attempt 输出永不进 coreMessages(commit 仅发生在 proxy-tool 轮次 core.ts:688;nudge 重试体 [...coreMessages, nudge] 结构性排除死尝试内容);无签名 thinking 段另有 ACP loop re-request drops reasoning_content, DeepSeek returns 400 invalid_request_error #539 守卫兜底(core.ts:699-703);
  8. 客户端流协议有效性:依赖 fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464 三个安全网,逐一读码确认——state-keyed message_start 抑制(adapter-anthropic.ts:315,模块级 messageStartForwarded,跨重试流至多一个 start 帧)、close-at-resume(:288-294,重试流开始前关闭 dead attempt 悬空块)、clientIndex 单调递增(:156);tool_use 块缓冲在 parseStream 局部 pending map(:247),截断时随调用消亡,不会给客户端发过 start 的块补 stop。
    两种压缩模式共用循环核心,闸门只读 ctx.protocol,无模式差异影响。

③ 代码审查:无缺陷。单行条件扩展 + 注释重写,注释新文本引用的依据(#1455/#1464/#440 单 created 不变式)与实际代码一致。一个继承性残余风险(非本 PR 引入,不拦合并):拼接流可能把「已闭合但无签名」的 thinking 块交给客户端(dead attempt 截断于 thinking 中段时 close-at-resume 补 stop),严格客户端下轮回放到 Anthropic proper 理论上可 400;但同一暴露面在已合并的 #1464 blind-retry 路径(零可见前缀场景)已存在且已被接受,本 PR 未扩大该风险类。

④ 测试有效性:两个新测试断言有牙、非恒真:

  • A(恢复路径):calls===2 钉死恰好一次 nudge;抓重试请求体验证含 nudge 文案且引用已转发 tail(并对 bodies[0] 做反向验证);单一 message_start;块索引 0/1/2 连续;idx1 的 stop 排序在 idx2 的 start 之前(close-at-resume 时序被钉住);
  • B(预算耗尽):one-shot 后不再 fetch、收尾帧为协议原生 event: error(upstream_error)、无合成完成帧、全块闭合。
    闸门回退、抑制失效、close-at-resume 乱序、预算变循环——任一回归都会红。

⑤ diff 整洁度:单 commit,2 文件,+169/-5,每行都与本 PR 目的相关;无版本字段、无 lockfile、无无关文件。干净。

验证证据(我在 head ef64ec40 本地独立复跑):

  • typecheck clean;build success;
  • 全量套件 2883 pass / 0 fail / 12 skipped——与 PR 描述声明完全一致;
  • CI:head sha 上 5 条 e2e lane(fake-codex 零 token 真客户端、native-pi/opencode/dsh、hermetic registry)全部 success;
  • ⚠️ 唯一待办:ci.yml(typecheck/test/build 矩阵,含 Windows)与 ci-e2e.yml(真实上游 e2e)都是 pull_request: branches: [master] 触发,而本 PR 创建时 base 是 fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464 分支(stacked);GitHub 在 base 重定向到 master 后不会补触发——所以这两条针对 ef64ec40 从未运行(PR 里预判了这一点,实际运行结果即未触发)。src/loop/ 属 e2e 热文件,按 AGENTS.md 合并前应有一次真实客户端 e2e。建议二选一:(a) Actions 手动 dispatch「E2E (real codex)」一次;(b) 向分支推一个空 commit 重新触发两条 workflow。代码本身未发现需修之处,我不代作者动分支。

结论:代码与测试可合(人工合并,Agent 不 merge);建议先补上真实上游 e2e 这一环再合。

一句话:把 anthropic 线的截断续写 nudge 从 openai-only 放宽到含 anthropic(单行闸门扩展,复用 #1464 安全网),本地全量套件与假客户端 e2e 全绿、新测试断言有牙,仅剩真实上游 e2e 因 stacked-base 触发语义未运行,手动 dispatch 或推空 commit 后即可合并。

The second truncation gate (continuation nudge) was hardcoded to the
openai wire, so an anthropic stream cut after visible text reached the
client had no proxy-side self-heal path and fell straight to the error
channel. Relax the gate to openai || anthropic; the two anthropic
re-fetch hazards are already neutralized by the adapter shipped in
#1464 (state-keyed message_start suppression, close-at-resume with
block indices continuing upward), and a dead attempt's partial output
is never committed to history (commit happens only on proxy-tool
rounds), with the #539 unsigned-thinking guard as backstop.
responses/google stay excluded (#440 single-created invariant).

Regression tests: tests/issue1470-anthropic-nudge.test.ts (recovery
with nudge-body/tail verification + exhaustion on the error channel).
Full suite green on the #1464 head: 2883 pass / 0 fail / 12 skipped;
typecheck clean.
@ranxianglei
ranxianglei force-pushed the 2026-09-27_anthropic-continuation-nudge branch from ef64ec4 to 34f716f Compare September 27, 2026 15:02
@ranxianglei

Copy link
Copy Markdown
Owner Author

Deep-review complete (independently verified, not a mirror of the bot review):

Rebased onto current master (head 34f716f — #1464 landed, so the PR's base moved from the #1464 branch to master as promised; clean, 2 files: src/loop/core.ts +15, new tests/issue1470-anthropic-nudge.test.ts).

Both safety claims verified in code:

  1. adapter-anthropic.ts:157 messageStartForwarded is forwarding-state-keyed (not round-keyed) — a re-fetched stream cannot emit a second message_start (incl. the :526 late-start fallback).
  2. :280-295 close-at-resume splices ALL still-open blocks with content_block_stop before the retry stream resumes; clientIndex keeps counting up — no dangling content_block_start, indices strictly increase. This is the mechanism that makes the widened gate safe for anthropic, and it's exactly why responses/google stay excluded (fix: acp-loop zero-side-effect truncation retry + well-formed anthropic error stream (#413) #440 single-response.created invariant has no dedup equivalent).

Mutation-verified: narrowing the continuation gate back to openai-only reddens both new tests (0/2); restored 2/2. The tests bite the gate, not just the fixtures.

Gates on the rebased head: typecheck ✓; full suite 2975 tests / 0 fail / 12 skipped (one first-run fail was the known tmp-dir flake family — clean on rerun; the 7 golden/#1414 reds seen mid-review were environment drift: symlinked node_modules carried acp-kernel 0.0.93 vs pin 0.0.95, reproduced on pristine master and gone after npm install; not this PR).

Behavior change (as disclosed): anthropic wire "visible text forwarded then truncated" now gets a one-shot continuation nudge retry before falling to the error channel; failure semantics and error frame shape unchanged; openai/responses/google untouched.

✅ Ready to merge — Closes #1470.

@ranxianglei
ranxianglei changed the base branch from 2026-09-27_truncation-retry-gate-error-signal to master September 27, 2026 15:08
@ranxianglei
ranxianglei merged commit daa8136 into master Sep 27, 2026
13 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-27_anthropic-continuation-nudge (8838ecc)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1457-pr.1478.82fb244

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-pr1478.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei mentioned this pull request Sep 27, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 复核你的 deep-review:代码结论不变(可合),但合并前有两个机械性阻塞,都是操作项不是代码项。

先确认你做的事(我在远端核对过,不是照抄):

  • rebase 是真实且纯机械的:34f716f0 的 PR patch 与我第一轮审的 ef64ec40 逐字节一致(core.ts 的 +/- 行完全相同),只是落点从 8929a458 移到 a1a9dba0;
  • 你的 mutation 验证(闸门收窄回 openai-only → 两个新测试 0/2 红;恢复 → 2/2)比我抓请求体的静态审查更硬,测试确实咬住闸门本身;
  • 2975/0/12 与第一轮 2883/0/12 的差值 = master 期间新增的测试,合理。golden/Copilot Gemini rejects ACP compress schema with invalid_request_body #1414 环境漂移的解释(symlinked node_modules 带旧 acp-kernel)也自洽。
    代码与测试层面我维持第一轮的判定:无缺陷,可合。

但有两个合并前必须处理的问题:

  1. PR base 还指着已死的 fix: gate truncation retry on visible output, not any forwarded byte; ride stream failures on protocol-native error frames (#1455) #1464 分支。API 实测 base.ref = 2026-09-27_truncation-retry-gate-error-signal(该分支仍在远端)。rebase 只移动了 head 的落点,没有改 PR 的 base——现在直接合并会合进那条死分支,master 什么都收不到,而 anthropic 线缺少 continuation-nudge 恢复路径:第二道截断闸门硬编码 openai-only #1470 会被自动关闭。这是 stacked PR 的经典陷阱。处置二选一:UI 里把 base 改成 master;或直接删掉那条已合并的旧分支(GitHub 会自动把 PR retarget 到 master)。

  2. head 落后当前 master 21 个 commit。rebase 落点是 a1a9dba0,之后 master 又进了 ce53d3b2(含 release v0.1.162、acp-kernel 0.0.97 bump chore: pin acp-kernel 0.0.97 — post-fold echo discrimination (kernel #463) #1505、proxy 工具参数 JSON 解析失败被降级为空对象执行,原始字符串被丢弃、lenient 容错层被绕过 #1502 等)。按 AGENTS.md §7.2「mergeable 前必须对 CURRENT master rebase」,还差最后一步。我已本地试跑:rebase 到 ce53d3b2 干净通过,零冲突,diff 仍是同样 2 文件 +169/-5——proxy 工具参数 JSON 解析失败被降级为空对象执行,原始字符串被丢弃、lenient 容错层被绕过 #1502 虽也动了 src/loop/core.ts(import / executeProxyTool / ~L706 提交路径三处 hunk),但与 nudge 闸地区域(~L527)无重叠、语义独立(工具参数宽容解析)。所以这次 rebase 是纯机械操作,push 后建议在 new head 上让 CI 过一遍(或本地再跑一次全量套件)。

连带效应(正好补上我第一轮的待办):ci.yml 和 ci-e2e.yml 都带 pull_request: branches: [master] 触发器——正因为 base 一直不是 master,这两条在本 PR 的任何 head 上都从未运行过(两个 head 上跑的只有那 5 条无 base 限制的 e2e lane,全绿)。完成上面两步(retarget + re-push)后这两条会自动触发,真实上游 e2e 这一环就补齐了。

顺序建议:retarget base → force-push rebase 后的 head → 等 ci.yml + ci-e2e.yml 绿 → 合并。合并本身是人工操作,我不会代合。

一句话:代码与测试复核通过(patch 逐字节未变、mutation 验证成立),但 PR base 仍指向已合并的 #1464 死分支且 head 落后 master 21 个 commit——先 retarget base 到 master 再 rebase 推上去(本地试跑零冲突),等 ci/e2e 触发变绿后即可人工合并。

@ranxianglei
ranxianglei deleted the 2026-09-27_anthropic-continuation-nudge branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

anthropic 线缺少 continuation-nudge 恢复路径:第二道截断闸门硬编码 openai-only

1 participant