Skip to content

[PR] fix: preserve Anthropic matched stop sequences - #7228

Open
Afloat16 wants to merge 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-18_preserve-anthropic-stop-sequence
Open

Afloat16 wants to merge 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-18_preserve-anthropic-stop-sequence

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7227

Problem

When an Anthropic streaming response ends on a custom stop string, the tool loop retains stop_reason: "stop_sequence" but replaces the provider's matched stop_sequence with null. Clients cannot identify which requested boundary ended the response. The no-tools passthrough already preserves the value.

Change

Keep the matched string in the Anthropic adapter for the current upstream attempt and carry it into the synthesized terminal frame only when the final reason is stop_sequence. Reset it for each round or retry. A usage-only delta leaves the observed match intact; an explicit null clears it. Preserve literal strings without trimming or inferring a match from the request.

The existing early-stop mapping, natural completions and legacy error completions continue to report null. No generic adapter API, configuration or kernel change is needed.

Validation

  • The 17 new regressions produce 10 failures and 7 passes on the original source, then 17 passes on this patch. All nine selected regression files pass: 96 passed, 0 failed, 0 skipped.
  • Five real loopback HTTP cases exercise the production server: ASCII and Unicode matches, an actual acp_status continuation followed by a matched stop, no-tools passthrough, and natural completion with unmatched configured stops. The first three fail on the original source; both controls pass.
  • Adapter controls cover byte-by-byte UTF-8 input, missing/null values, usage-only deltas, round and same-round retry isolation, independent adapters, literal strings, forced length termination and legacy errors. The existing suites cover terminal metadata, thinking, truncation, errors and nonstreaming continuations.
  • npm run typecheck, npm run build, the test import-graph gate, CI live-file classification and git diff --check pass. Rebuilt kernel and pi-subagents artifacts match the components used by the tests byte for byte.
  • Remote full-suite CI is still running on this head.

The real Codex four-phase model E2E has not run and remains a required pre-merge check. The zero-token preflight found the CLI and built artifact, but the local upstream at 127.0.0.1:8199 was unreachable. The HTTP fixtures above use a local scripted provider; they are not a real-model E2E result.

Novelty check

Compared all 276 live Afloat16 fork heads and 475 upstream history entries, covering 64 complete versions of this adapter. Each existing terminal constructor still emitted a literal null. Also inspected the actual patches for #5202, #5530, #2691 and #7207; those address other lifecycle behavior.

@ranxianglei ranxianglei changed the title fix: preserve Anthropic matched stop sequences [PR] fix: preserve Anthropic matched stop sequences Nov 18, 2026
@Afloat16
Afloat16 marked this pull request as ready for review November 18, 2026 20:33
@ranxianglei ranxianglei added bug Something isn't working claude-code p2 review中 scope/wiring ci:full Run the FULL CI matrix (all e2e lanes + 12-leg test matrix) on this PR labels Nov 18, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷
(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 — searched open+closed issues and PRs for matched-stop_sequence preservation: #5531 is a different bug (delta:null crash), #6021 is retry-decision logic, and the auto-mode-forge path forges its own stop_sequence independently (e2e-anthropic.test.ts:458-546) and is unaffected. No parallel fix PR exists for #7227.

标签: bug + p2 + claude-code + scope/wiring + review中 (applied to both PR #7228 and issue #7227) — 功能缺陷(元数据丢失,非崩溃/非缓存泄露 → p2),Anthropic wire 协议接线层,claude-code 车道。

Review

The fix is correct and minimal. The null was produced by buildTerminal hard-coding stop_sequence: null while parseStream discarded the provider's matched string from the message_delta frame. The patch captures it at the source — a per-request closure var, reset at the top of parseStream so each round and each same-round retry starts clean — and uses it in the synthesized terminal only when the final reason is stop_sequence. Explicit null clears, usage-only deltas leave it intact, non-strings are ignored, and nothing is inferred from the request. Root-cause fix at the producer, not a mask.

Verified locally (worktree at PR head 36a8331 vs master a446a3c):

  • New suite: 17/17 pass on the PR; exactly 10 fail / 7 pass on master — the tests have teeth, and the author's claim is accurate.
  • npm run typecheck clean; check:test-import-graph clean; git diff --check clean.
  • Adjacent suites (56 tests): anthropic-round2-stream, anthropic-thinking-replay, issue5432-trailing-system-tails, loop-stream-cleanup, upstream-truncation, verif-adapter-wire-stability, issue5179-anthropic-early-stop-reason — all green.

Regression audit (existing behaviors on the touched paths):

  • Natural completion (end_turn), tool_use, length→max_tokens, legacy error completion: still stop_sequence: null — only the stop_sequence reason carries the match. ✓
  • No-tools requests and plugin mode: passthrough (compressInjected=false) — upstream terminal relayed byte-for-byte, already preserved the match; unchanged. ✓
  • Multi-round / retry: state resets per parseStream call (core.ts:968 attempt loop), so a round or retry can never inherit an earlier match; pinned by tests. ✓
  • No history pollution: the loop's saved round parts carry no stop_reason/stop_sequence; the field is response metadata only. ✓
  • Adapter state is per-request (factory at src/loop/index.ts:43); independent adapters don't share. ✓
  • Forge path (auto-mode) unaffected — it forges its own stop_sequence. ✓

Behavior change (disclosed): loop-path terminal stop_sequence changes from always null to the provider's matched string when the final reason is stop_sequence. Old → new: null → "<matched string>". This aligns the proxy with the native Anthropic API (which sends the matched string); clients branching on stop_sequence !== null will now correctly identify the boundary — that is the point of the fix.

Both compression modes reasoned: proxy mode gets the fix; plugin mode passthrough is unaffected (upstream bytes verbatim).

Code quality: clean — per-request closure var, one explanatory comment (why the reset exists), style matches the file, no new deps/API/config. Test quality: real loopback HTTP through the production server (port 0, deterministic) plus adapter-level isolation tests; assertions pin the exact client-visible terminal.

CI status (live-checked):

Verdict: review passes — correct, minimal, well-tested, no regressions found. Pre-merge: (1) the e2e run (now triggered) must be green; (2) the 3 stuck shards need cancel + re-run. Wire-shape change → human-only merge per §7.4; I will not merge.

八问

  1. 是什么问题:Anthropic 工具循环的合成终止帧把 stop_sequence 硬编码为 null,客户端无法知道是哪条自定义停止串结束了响应(无工具直传路径本来保留该值)。
  2. 什么原因导致的:buildTerminal 输出端写死 null,parseStream 解析 message_delta 时丢弃了供应商回传的 matched string——产出 null 的机制就在合成终止帧本身。
  3. 影响范围与严重程度:任何走 Anthropic wire + 工具循环 + 配置了 stop_sequences 的客户端(claude-code 等);仅响应元数据丢失,无崩溃/数据丢失/缓存影响;p2。
  4. 怎么修复的:在 parseStream 按轮捕获供应商 matched string(每轮/每次重试入口重置,显式 null 清除,usage-only delta 不动,非字符串忽略,不从请求推断),仅当最终 reason 为 stop_sequence 时带入合成终止帧——修在产出端,不是中途过滤。
  5. 是否有回归问题:审过自然完成/tool_use/length/legacy 错误四条路径(仍 null)、无工具与插件模式直传(不变)、多轮/重试隔离(测试钉住)、历史消息无污染、forge 路径不受影响;56 个邻近测试 + 17 个新测试全绿,typecheck 绿。无回归。
  6. 是否破坏缓存:不涉及——stop_sequence 是响应元数据,不进请求前缀,不影响 prefix cache/重试/游标。
  7. 是否改了设计方向:不涉及——无新 adapter API、无配置、无 kernel 改动,生产代码 6 行。
  8. 是否引入了新的配置:不涉及。

行为变更披露:loop 路径终止帧 stop_sequence 由恒 null 变为在最终 reason 为 stop_sequence 时携带供应商 matched string(对齐原生 Anthropic API,即修复目的本身;旧值 null → 新值 matched string)。

结论:审查通过;可合并条件 = e2e(ci:full 已触发,待跑)绿 + 3 个卡死 shard 重跑后 CI 全绿。wire 形状变更按 §7.4 属 human-only,合并由 owner 执行。

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

Labels

bug Something isn't working ci:full Run the FULL CI matrix (all e2e lanes + 12-leg test matrix) on this PR claude-code p2 review中 root-cause scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic tool-loop completions lose the matched custom stop sequence

2 participants