Repository navigation
[PR] kernel: heal system positions on the final Anthropic wire array (#4570) - #4715
Conversation
…ianglei#4570) Anthropic's mid-conversation system rule has two halves: a content-bearing system must follow a user turn (or an assistant ending in a server tool result) and must precede an assistant turn or end the array. - healIllegalSystemPositions now enforces the predecessor half too: a system after an assistant (or at index 0) moves past the next user turn, keeping tool deltas, or is re-voiced to user when no user turn follows. - New healAnthropicSystemPositions re-heals the array actually sent after prepare-anthropic appends host notes, nudge directives and the retrieval note, on the decision side request, and on compress-loop re-requests. - WC-023 records the rule in the wire-contract ledger.
The kernel version bump in the parent commit left both package-lock.json files at 0.0.163, so the lock-sync CI job failed and every test lane was skipped. Regenerated with npm install --package-lock-only exactly as the lock-sync job does; only the acp-kernel version lines change.
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) 查重 (dedup): 不重复。同家族已关闭的 #4292/#4396/#4457/#4465 修的是这一机制的 fold-summary 半边(内核内 heal 的时机与降级);本 PR 是另一半——在最终出站数组上强制完整的位置规则,包含此前从未检查过的 "must follow" 半边。按失败签名(两条不同的 400 报文)检索 open+closed issues/PRs:无在途重复。 标签: 已打 Review(PR #4715,head
|
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) CI 跟进(新 head 状态):
|
…help-merge sweep)
…help-merge sweep)
…help-merge sweep)
… master 0.0.167 -> 0.0.168)
|
I reported #4570 and tested this PR against our failing shapes through the real request path ( Shapes:
The folded view is injected at the request boundary; the fold itself is not driven.
Your own tests on the same trees: One behaviour worth a line in the description: a sidecar From our side this fixes #4570; we will drop our local patch once a release with it is out. |
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) Thanks for the A/B through the real request path — asserting on the body the fake upstream actually receives is exactly the evidence bar this fix needed, and it lines up with my review read (both shapes 400 on master, legal on the PR, legal layouts byte-identical). The demote-vs-move behaviour you flagged is intentional, and the two halves deliberately take different paths:
Your local workaround (keep as system, move past the following users to just before the next assistant / array end) is a valid alternative default — it keeps the delta effective at the cost of moving more bytes. Flipping the default from demote-to-move is a behaviour decision (delta effectiveness vs byte-minimality / cache stability), not a defect in this fix, so I'd rather track it as an owner decision in its own issue than quietly change semantics inside this PR — say the word and I'll file it with your examples as the spec. Paste-ready line for the description, since you asked for one:
Status after your master sync: head is now Verdict: review stands as posted — direction correct, no regressions found, tests have teeth. Kernel territory stays human-merge per the Kernel Boundary; ready whenever you are. 中文小结
|
(ai: vllm-qwen/qwen3.8-27b) 这是一条重复/过期的告警,无需新动作。 它指向的是 run 刚在当前 head 结论: |
(ai: vllm-qwen/qwen3.8-27b) Merge-guard sync done: merged |
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 4: nothing to sync — origin/master (9563c1b) is already fully contained in this branch from the previous push (verified 0 commits behind), so no new commit was made; state after last push (head |
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 5 done: merged master |
…ll master 58e61a3 + kernel 0.0.171
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 6 done: synced master into the branch (it advanced twice mid-round, so both |
[bot] 🏷 merge-guard 第 6 轮同步完成:master 本轮推进了两次,已把 |
(ai: vllm-qwen/qwen3.8-27b) 本轮 CI 红(head 证据链
结构化八问
状态:rerun 已触发、进行中;若重跑再次命中同一签名,即为 floor 486 所述「待 #5144 侧处置」的同一 master flake,届时交由哨兵/admin,不在本 PR 重复排查。收工判定:本 PR 无自身缺陷,等重跑转绿即可合并(合并不由我执行)。 |
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 7 done: synced master |
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 8: no new sync needed. Master is still at |
|
(ai: vllm-qwen/qwen3.8-27b) Merge-guard round 9: same state as round 8 — master is still |
[bot] 🏷 Merge-guard round 10 done — this time the What came in (all 14 commits = the #5188 fork/snapshot sidecar-recovery line):
Sync details:
Live API after push: head Nothing else pending on the guard side; next firing will re-verify drift. Kernel territory stays human-merge per the Kernel Boundary. |
…i#5438 two-sided heal; keep final-array backstop; kernel 0.0.181) Conflict resolution: master shipped the sibling fix for this family (ranxianglei#5438 via PR ranxianglei#5522: two-sided heal inside coreToAnthropic, demote-only) plus the insertBeforeTrailingSystemRun helper (host wiring pending in ranxianglei#5441). Take master's approved heal semantics wholesale; keep ranxianglei#4715's residual contribution: the healAnthropicSystemPositions standalone export + the three host final-array call sites + WC-023 pins (ledger entry kept from master's side, one entry per id). Branch move-past-next-user design superseded by the owner-approved ranxianglei#5438 approach. Kernel bumped 0.0.180 -> 0.0.181 (kernel-change-vs-base rule).
|
(ai: vllm-qwen/qwen3.8-27b) @Mako-L — the branch conflicted with master because master absorbed a sibling fix for this same family while this PR was in review: #5438 (owner-approved kernel authorization) shipped via PR #5522 a two-sided heal inside I resolved the conflict against current master (new head
Local verification on the merged tree: typecheck clean, kernel targeted 46/46, root targeted 26/26. CI is running on the new head. One residual gap worth an owner decision (not fixed here): under demote-only semantics an orphaned tool-delta sidecar loses its |
[bot] 🏷 Merge-guard outcome: no action needed — the conflict was already resolved by @Mako-L before this round ran, and I verified that resolution independently rather than re-doing it. State after their push: head |
ranxianglei
left a comment
There was a problem hiding this comment.
(ai: vllm-qwen/qwen3.8-27b)
Findings-only review of the final head 15adc9b44 (net diff vs master 23a826d37: 8 files, +283/−8). This is the first review pass on the post-conflict-resolution tree — the earlier in-thread reviews covered superseded heads that carried the move-based engine. After #5438 landed on master via #5522, the branch converged to: master's approved two-sided heal engine inside coreToAnthropic + this PR's residual = the healAnthropicSystemPositions final-array backstop (kernel export + three host call sites) + tests adapted to demote semantics. I reviewed the net diff independently.
① Direction (original issue #4570) — correct, and unchanged by the convergence. Both production 400s stem from the position rule being enforced before host appends (notes / nudges / retrieval note / decision prompt) and before fold-orphaned sidecars land on loop re-requests. Re-running the same predicate on the final array fixes it at the send point; the single source of truth stays in the kernel (the #5438 engine, already owner-approved).
② Regression audit (existing behaviour on touched paths)
- Legal layouts: byte-identical — the heal is a no-op when both halves hold; pinned by the "legal layouts round-trip verbatim" negative gate (six layouts incl. server-tool-result predecessor, directive-only exemption, trailing run at array end) and the host no-notes negative. ✅
- Directive-only systems: never healed, never legitimize neighbours (master contract preserved). ✅
- Index-0 systems: untouched (deliberate #5438 contract, pinned by test). ✅
- Thinking/signature blocks (KDD #10): the heal touches system messages only;
warnAnthropicThinkingPairsmatches by stable tool_use id over assistant messages, so index shifts/drops from the heal cannot misfire it. ✅ - Prefix cache: the heal fires only on layouts that previously 400'd the whole request → zero churn on healthy sessions; deterministic healing keeps client-resent bytes stable across turns. ✅
- Call-site coverage: all three post-conversion append sites are healed (
prepare-anthropic.tsfinalrebuiltMessages+ nudge-decisionsideBody;adapter-anthropic.tsloop notes). Every othercoreToAnthropicsite audited:src/loop/core.ts:740(thread body — sends the converted array verbatim),src/server.ts:3052(count_tokens — verbatim),src/plugin-render.ts:156(diagnostic render, not model egress),src/incremental-lane.ts:356/362(fragment splice into client-resent bodies — separate mechanism/lane, see observation 2 below). No unhealed append remains on the full-resend path. ✅ - Behaviour change disclosure (intended — the fix itself): illegal mid-conversation system layouts — old value: sent as-is → upstream 400 → session death; new value: healed in place (demote to user,
tool_addition/tool_removalstripped per WC-022, empty entries dropped). Previously-fatal arrays only; nothing that worked changes.
③ Code — the kernel addition is a 12-line additive wrapper (copy + run the existing engine; fresh objects only, input never mutated); host changes are three one-line wraps. No new dependencies, no config surface, no env vars. Kernel Boundary discipline on the final diff: kernel-change: declaration present, version 0.0.180 → 0.0.181 (CI-enforced), kernel: commit prefix. ✅
④ Tests — real assertions with teeth: both production shapes reproduced through the real entry points (adapter buildRequest checked against the WC-023 ledger validator; kernel direct calls with an independent oracle mirror); both-direction gates present (an over-heal would red on the verbatim-legal rows; dropping the follow half would red on the must-follow rows). Two observations, neither blocking:
- The kernel-test oracle encodes the positional halves only, not the tool_use↔tool_result adjacency constraint — so the "a system after an assistant is re-voiced in place" row pins
[A(tool_use), U(demoted), U(tool_result)], a shape the PR description itself live-verified as a 400. That shape cannot be produced by bili's own pipeline (the turn-integrity carve forbids folds splitting a tool pair; sidecars land at turn boundaries), and raw client input carrying it already 400'd on master — no practical exposure, but the oracle's boundary is worth knowing. It feeds the #5031-family discussion rather than this PR. - The dsh incremental lane splices
coreToAnthropicfragments into client-resent bodies; a fold-orphaned sidecar could in principle create an illegal position at a splice boundary that the final-array backstop does not see (different mechanism/lane — #4570's production hits were full-resend shapes). Awareness note only, no action requested here.
⑤ Diff cleanliness — clean: 8 files, every line serves the PR purpose (wrapper + 3 call sites + lockfile syncs + 2 test files). One process item: the PR body is stale in four places after adopting #5438 — it describes the dropped move engine, claims WC-023 as new (it shipped with #5438/#5522), cites kernel 0.0.163 → 0.0.164 instead of 0.0.180 → 0.0.181, and lists fixture edits that are not in the net diff. Recommend updating the body before merge so history matches code — author-owned, non-blocking.
结构化八问
- 是什么问题:Anthropic 在 mid-conversation
system消息位置非法时拒绝整个请求;bili 的 heal 跑在coreToAnthropic内部,即 bili 自己追加 notes/nudges/retrieval note 之前、以及 fold 孤儿 sidecar 在 loop 重请求上显形之前——nudge 轮与 compress-loop 重请求持续发出非法数组,整个会话 400。 - 什么原因导致的:位置规则在转换时刻执行、而非发送时刻执行;转换后追加(host notes/nudge/检索注记/decision prompt)与 fold 孤儿重新制造了内层 heal 已清掉的位置违规。
- 影响范围与严重程度:p1 可用性——anthropic 线路上重建历史含非法位置 system 且该轮有追加注记、或 fold 把 sidecar 前的 user 轮折掉的会话(Claude Code / omp / pi 车道;报告者 v0.1.206 单会话一天命中两次)。触发即该请求硬 400,需本地补丁/新会话恢复。
- 怎么修复的:owner 已批准的 #5438 两半位置 heal(现已在
coreToAnthropic内)作为独立 pass 在最终出站数组上重跑——kernel 导出healAnthropicSystemPositions,三个 post-conversion 追加点各调用一次。 - 是否有回归问题:未发现。合法布局字节不变(负向测试钉死);directive-only 与 index-0 契约保留;thinking 哨兵不受影响(按 id 匹配);无缓存影响;仅此前必 400 的数组行为改变(即修复本身)。已知已跟踪残留:demote 会丢孤儿 sidecar 的 tool delta(#5031 家族,owner 决策中)。
- 是否破坏缓存:否——heal 只在请求此前必死的布局上触发;健康请求字节稳定,确定性 heal 保证客户端逐轮重发得到相同结果。
- 是否改了设计方向:没有——规则单一事实源留在 kernel,host 只是更晚调用它。
- 是否引入新配置:无。
判定:最终 head 无阻塞性代码发现,CI 于 15adc9b44 全绿(镜像缓存核对:全部 executed check success,skip 均为设计内门控 e2e 车道)。因 PR 触碰 kernel/(Kernel Boundary:人工评审档,禁止 auto-merge),agent 权限下不能 approve——授权请求见下方 🔶 评论;建议合并前更新 PR 正文以匹配收敛后的设计。
|
🔶 需要决策 PR #4715 (head Gated diff (net vs master
Host-side call sites (not gated; reviewed clean): Context: the #5438 heal engine itself was already owner-approved (shipped via #5522); this PR's residual kernel change is only the additive wrapper plus the version bump. Agent review conclusion: mechanism correct, call-site coverage complete, tests real, no blocking findings (full analysis in the review above). The residual demote-vs-move delta-loss semantics are tracked separately as the #5031-family owner decision. Options: A — authorize the Recommendation: A. |
(ai: vllm-qwen/qwen3.8-27b) Review lane 收工:最终 head |
|
I prepared an additive merge candidate for this PR against current master. Candidate commit: Maziar123@3740f16 It resolves the conflicts in I cannot push directly to this PR's branch, so the candidate is on my fork: https://github.com/Maziar123/billion-context/tree/local-repair/pr-4715-20261109. Please cherry-pick or use it as a reference. The remaining demote-versus-move choice for tool deltas is still the open #5031 decision. |
(ai: vllm-qwen/qwen3.8-27b) Conflict resolved and pushed — I adopted your merge candidate verbatim, after verifying it independently instead of trusting it blindly. What I did this run:
Still open, unchanged by this run:
中文结构化小结(八问)
收工判定:冲突已闭环并推送( |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.1134-master.4446301 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Fixes #4570
kernel-change:
healIllegalSystemPositionsonly enforced half of Anthropic's mid-conversation system rule and ran before the host appended messages; the heal needs the predecessor half and a public entry point for the final wire array.Errors seen in production (Claude Code → bili → Anthropic)
The second came from a compress-loop re-request (
[acp-loop] upstream error 400). No open issue covered it.Root cause
healIllegalSystemPositions(kernel/src/wire/anthropic.ts, called insidecoreToAnthropic) keeps a trailing system because it is legal at the end. Then the host appends user messages after it:src/server/prepare-anthropic.ts: host notes (sysNotes), nudge directives, and the retrieval note go ontorebuiltMessagesaftercoreToAnthropic, andrebuiltis built from that array unhealed. The decision side request (sideBody) appendsbuildDecisionPromptthe same way.src/loop/adapter-anthropic.tsbuildRequest: compress-loop notes are appended aftercoreToAnthropictoo.Rule (checked against the live API)
A content-bearing
role: "system"message must:usermessage, or anassistantmessage whose last block is a server tool result (*_tool_result, nottool_result). Index 0 is illegal. A directive-only system does not count as a predecessor.assistantmessage or end the array.Consecutive systems are judged together. The directive-only form (
content: []withoutput_config) is legal anywhere.A/B results:
[U,S,A,U]200,[U,A,U,S]200,[U,S,S,A,U]200,[U,Sdir,S,A,U]200,[U,A,Sdir,A,U]200.[U,A,S,A,U],[U,A,S],[U,A,Sdir,S,A,U],[U,A(tool_use),S,U(tool_result)]400 "must follow".[S,A,U]400 at messages.0 ("use the top-level 'system' parameter").[U,S,U],[U,S,S,U]400 "must precede". Re-voicing a system that sits betweentool_useand itstool_resultas a user message is itself a 400 (tool_useids withouttool_resultimmediately after), so that case must move, not demote.Fix
kernel/src/wire/anthropic.ts:healIllegalSystemPositionsenforces both halves. A system with an illegal predecessor moves, whole and in run order, to just after the next user turn (this keepstool_use/tool_resultadjacent and keeps tool deltas as system). If no user turn follows, it is re-voiced to user, as the [Bug][anthropic] 0.1.205: 400 "role 'system' must precede an 'assistant' message" still occurs with #4396 present (mid-conversation system + tool_addition, cf. #4400) #4457 heal already does. The successor half is unchanged (re-voice, striptool_addition/tool_removal, drop if empty). New exporthealAnthropicSystemPositions(messages)returns a healed copy.src/server/prepare-anthropic.ts: healrebuiltMessagesright beforerebuilt, and heal thesideBodymessages.src/loop/adapter-anthropic.ts: heal the compress-loop request when notes are appended.tests/wire-contract-fakes.ts/tests/wire-contract.test.ts: new ledger entry WC-023 (both halves), enforced in the Anthropic validation-parity fake, with a gate test and a ledger self-check probe.kernel/package.json: 0.0.163 → 0.0.164.No user-visible content is dropped. Legal layouts stay byte-identical.
Tests
kernel/tests/issue4570-system-position-final-wire.test.ts(7): the [Bug][anthropic] 0.1.206: system 400 still fires on nudge turns: the heal keeps a trailing system, then prepare-anthropic appends user notes after it #4570 note-after-trailing-system shape, a trailing tool-delta run before a note, a sidecar between two assistants, a system betweentool_useandtool_result, a run after an assistant, trailing-after-assistant and index-0 systems, and a legal-layouts-verbatim negative. Against master source, 6 of 7 fail (the negative passes).tests/issue4570-anthropic-final-wire-system-position.test.ts(3): compress-loopbuildRequestwith notes after a trailing system, a post-fold sidecar after an assistant, and a no-notes negative, all checked with the WC-023 validator. Against master source, 2 of 3 fail.kernel/tests/issue4292-orphan-system-guard.test.tsandkernel/tests/anthropic-system-message-passthrough.test.ts.Local:
npm run typecheckclean.npm --prefix kernel test1480/1480. Targeted root suites (wire-contract, anthropic lanes, #4292/#4159 system tests) pass. The full-suite verdict is the CItestlane.