Repository navigation
[PR] fix(kernel): heal the follow side of Anthropic's mid-conversation system rule (#5438) - #5522
Conversation
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.7865-pr.5522.be209b3This exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr5522.tgz
npm install -g packageInstall instructions are refreshed on each push. |
🔶 需要决策 查重:不重复。镜像检索(open+closed,按机制关键词 "healIllegalSystemPositions follow side" / "mid-conversation system follow heal")零命中;相关线程 #5432(母 issue)、#5438(kernel 授权申请,owner 已批 option A)、#5441(host 侧止血 PR,未合入 master)——本 PR 是 #5438 批准 diff 的交付,与三者机制或范围均不同。 标签:bug, claude-code, scope/kernel, p1, needs-decision —— 缺陷类;现场客户端为 claude-code v2.1.316(mid-conversation-tool-changes 契约);修复位置在 kernel wire 编码器;受影响配置下整请求 400、会话级不可用 → p1(可用性缺陷类)。 背景一句话:本 PR 实现的是你已在 #5438 拍板(option A,2026-11-02)的 kernel diff,但 PR 正文缺 待决问题:由谁在 PR #5522 正文补上 kernel-change 声明? 选项:
不决定的默认行为:PR 停在 kernel-guard 红,test 车道持续 skip,无法合并。代码审查不受此阻塞——我已独立通读 diff 并核完版本/lockfile,八问结论随后单独给出。 初审状态:diff 已通读(6 文件全部在 kernel/ 内 + 两个 lockfile,无 host 侧改动);kernel 版本 0.0.177→0.0.178 与两个 lockfile 一致性已核对;正在本地跑定向测试(新增回归文件 + 4457 双侧 checker),完整八问结论随后给出。 |
(ai: vllm-qwen/qwen3.8-27b) PR #5522 审核结论(结构化八问)我独立做了什么:通读全部 6 文件 diff(逐行读了
测试有效性:真测试——fold 驱动回归走真实压缩链并断言精确内容(delta text 以 user content 存活、tool_addition 绝不泄漏出 system)、双向门纪律齐全(正例 heal + 负例 verbatim)、链式最小修复有 pin、空 additions 同引用有 assert.equal。bug 复发时抓得住。 diff 整洁度:6 文件全部服务于目的,lockfile 只有版本行,无空行/重排噪音。代码整洁硬标准逐项过:命名达意、无死代码、无魔法数、注释讲为什么(带 issue 锚点)、风格与周边一致、零新依赖。 合并判定:代码本身通过独立审核,可合的条件是两个前置:① owner 在 PR 正文补 |
…llow side (#5438) Review follow-up to the approved heal: legitimatesFollowSide matched ONLY the literal 'tool_result' as an assistant's last block, but on the real Anthropic wire server-executed tool results ride INSIDE assistant messages as the <name>_tool_result family (web_search_tool_result, code_execution_tool_result, ...; plain tool_result is user-role only). The parser rides those blocks on the #3410 unknown-block sidecar, so a legal system-after-server-tool-result-assistant hit the over-demotion path: re-voiced to user on every request — byte drift vs verbatim = one-time prefix-cache break plus a system->user authority shift on previously-legal traffic. Fix: match the whole family by suffix (endsWith 'tool_result') — every member shares the convention, future server tools inherit it, and no non-result block type ends in 'tool_result'. Tests: mirrored orphanSystems checker updated to the same family rule; new regression pins BOTH directions — the real wire shape (assistant ending in web_search_tool_result keeps the following sidecar system verbatim) and its precede-side-violating twin (still demoted, proving the two legs stay independent).
…nversation system rule (#5438) The #5432 production 400s documented a permanent upstream constraint (system messages must FOLLOW a user or a tool-result-terminated assistant AND PRECEDE an assistant or end the array; directive-only form exempt anywhere) that had no WIRE_RULES ledger entry — #1304 requires one inside the fixing PR. Adds WC-023 with provenance citing both live error strings, and midConversationSystemViolations() mirroring kernel healIllegalSystemPositions exactly (per-run verdict at the first non-directive member; missing predecessor legal; <name>_tool_result family on the follow side), enforced on BOTH Anthropic lanes (main validator + vertex-anthropic lane validator share the clause — same Messages API behind both). Gate test asserts both directions plus the four legal shapes; probe row feeds the ledger self-check.
(ai: vllm-qwen/qwen3.8-27b) PR #5522 审核结论(findings-only,不 approve —— kernel 人工门禁)我独立做了什么(不复读前一轮结论):逐分支推演 核心判定:代码正确、测试扎实,可合——但有一处我在前一轮之外新发现的流程缺口(见第 5 问),需 owner 定夺。 结构化八问
测试有效性:真测试。fold 驱动回归走真实压缩链并断言精确内容(delta text 以 user content 存活、 CI 状态(镜像缓存 @ ca532d4,06:40Z):kernel-guard ✅、全部 guard/gate 车道绿(version/lock-sync/config-surface/lane/unused-exports/docs/pi-subagents/lifecycle-ledger),0 failed / 0 pending。注意:本快照里 diff 整洁度:6 文件全部服务目的,lockfile 仅版本行,无噪音。版本纪律正确(acp-kernel 0.0.177→0.0.178 随 kernel src 变更同 commit,CI 强制;host 小 nit(不处理):PR 正文写「10 tests」,实际新文件 11 个顶层 合并判定:代码本身通过独立审核,可合。但本 PR 触及 |
🔶 需要决策 PR #5522(gated diff: 待决事项(1 项,不阻塞代码正确性):WIRE_RULES 台账缺口。#5438 open-item(d) 承诺把「mid-conversation system 位置规则」作为 WIRE_RULES 永久条目并入本 PR(§7.3/#1304),但本 diff 未加——台账仍只有 WC-001..WC-022。回归已被新增 kernel 测试钉住,所以两条都可接受,请你选:
合并前请顺手确认:镜像缓存本快照里 其余:代码正确、测试扎实、行为披露完整、diff 干净,无其他阻塞项。 |
(ai: vllm-qwen/qwen3.8-27b) review-worker-r1 完成:findings-only 审核(不 approve,kernel 人工门禁)+ 🔶 需要决策 + needs-decision 标签均已落地;代码判定可合,待 owner 人工合并授权并就 WIRE_RULES 台账缺口(选项 A/B)拍板。 |
…23 (#5522) The WC-023 ledger commit (f862e4a) accidentally deleted three permanent WIRE_RULES entries (WC-010 cache-breakpoint cap, WC-013 reasoning.summary, WC-014 Jinja head-system) and overwrote WC-011's #1733 provenance with unrelated #4882 text - violating the ledger-grows-only rule (#1304). Their enforcement clauses and gate tests were never removed, so this is a faithful byte-for-byte restore of the known-good records from ca532d4. Verified locally: tests/wire-contract.test.ts 23/23 green (ledger self-check now sees all 25 rules) and tsc --noEmit clean.
(ai: vllm-qwen/qwen3.8-27b) 第二轮审核(新 head 作者在我第一轮审核后推了两个 commit。我独立重审了增量(未照抄 issue/上一轮结论): ①
②
③ 我另发现并当场修掉一个阻塞级缺陷(#1304 违规) 结构化八问
结论:代码正确、测试有效、台账已补齐并恢复,可审层面全部收敛。唯一挡合并的是 kernel 人工审批门槛(Kernel Boundary §1 + 本节 HARD RULE)——agent 不自批 kernel 变更,转 🔶 需要决策,等 owner 授权后再由人合并。 |
🔶 需要决策 (ai: vllm-qwen/qwen3.8-27b) PR #5522 触及 请 owner 就以下被门禁的 kernel diff 授权:
背景:owner 已于 2026-11-02 在 #5438 确认 option A(「你直接把他修复到闭环吧」),本 PR 即其实现;host 侧配套为 #5441。此授权是合并前的最后人工关卡。我这边可审层面已全部收敛(含当场修掉的台账误删,见上一条 review)。 |
(ai: vllm-qwen/qwen3.8-27b) ✅ 第二轮审核完成(head |
…r-convergence asserts The windows/node22 shard-2 flake behind #5522 run 41407086184: settle() trusted one fixed sleep of debounceMs+60 (80ms for these fixtures) before asserting all 11 clustered-duplicate rows had settled — under Windows load the debounced disk flush outran that budget (9/11 rows). Both affected assert sites now poll a bounded 10s window (waitUntil) before the SAME assertions; no behavior change shipped, test file only.
|
红因定位 + 修复(test-only,已 rebase 在分支上): 唯一红的 job: windows/node22 测试矩阵分片 2(run 41407086184,job 125151570147)。TAP 2512 个用例只挂 1 个:「#5155 integration: duplicate-content messages (clustered 根因是时序 flake,与本 PR diff 无交集:该测试的 settle() 是固定睡眠 debounceMs+60(fixture debounceMs=20 → 共 80ms),睡完就断言 11 条同内容消息的持久化行全部落盘并匹配指针。Windows 分片高负载下(同文件相邻用例单跑 3.9s),防抖磁盘写入没赶上 80ms 预算 → 只匹配到 9 行。这条链路全是宿主持久层(SessionStore 防抖 / inc:canonical-ptrs 扩展字段),与本 PR 的改动(kernel/src/wire/anthropic.ts follow-side heal + kernel 测试 + 版本号)零重叠;同一份代码在 05:43Z、06:48Z 两轮完整 CI 都是全绿,本地 linux 全量也绿。 修复(commit 335afa0):把两个指针收敛断言点前的固定睡眠换成有界轮询 waitUntil(≤10s),断言语义完全不变,仅测试文件,+22 行;本地跑该文件 7/7 通过。 另告知:本分支在两次推送之间被另一会话(ci-check@local)追加了 3 个 commit——f06619ff(kernel 侧放行真 server-tool-result 块族)、f862e4a9(WC-023 ledger 条目+tripwire)、3ee3ff65(恢复其误覆盖的 ledger 条目);本次修复已 rebase 在其上,本轮 CI 一并覆盖它们(此前 06:48Z 批次已单独验证过全绿)。 |
(ai: vllm-qwen/qwen3.8-27b) 独立复核了你的 flake 修复 commit
状态:CI 红已消解并验证。整个 PR 现在只剩一道门槛——按 Kernel Boundary §1 / 🔶 需要决策,等你对 kernel diff 的显式授权后由人合并;我这边代码面没有其它待办。 |
…c-heal-follow-side
(ai: vllm-qwen/qwen3.8-27b) 第四轮(新 head 这个新头是把
结论不变:代码正确、可审层面全部收敛。仍只差一道门槛——按 Kernel Boundary §1 / 我的 🔶 需要决策,等你对 kernel diff 的显式授权后由人合并。本轮无新增待修项、无新发现。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.998-master.d5a84f1 # this exact build
npm install -g billion-context@master # always the newest merged build
|
…low-side heal Kernel gains the follow-side half of the mid-conversation system rule (ranxianglei#5438) plus two wire-contract ledger entries and a bounded-poll fix for issue5155. Zero overlap with this lane's 16 files.
…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).
kernel-change: heal gains the FOLLOW half of Anthropic's two-sided mid-conversation system-position rule + single-source insertBeforeTrailingSystemRun export; owner approved this exact diff as option A in #5438 on 2026-11-02 (approval recorded in issue comment), closing #5432's remaining exposure.
#5438 approved diff — the follow half of Anthropic's two-sided system-position rule
Closes the remaining exposure of #5432:
healIllegalSystemPositionsenforcedonly ONE half of the API contract. A mid-conversation
systemmust followa user message (or an assistant ending in a server
tool_result) ANDprecede an assistant or end the array. The precede half was healed
(#4292/#4457); the follow half sailed through the backstop and 400ed the whole
request —
messages.N: role 'system' must follow a 'user' message— until theclient self-clears its mid-conversation systems. Field repro: claude-code
v2.1.316 + mid-conversation-tool-changes contract, bili v0.1.210 /
acp-kernel 0.0.175 era (#5438).
Changes (all inside
kernel/, per the approved diff)healIllegalSystemPositionsgains the predecessor leg. The nearestnon-system predecessor of each non-directive system must now be a
user, oran
assistantwhose last content block istool_result; a system with nopredecessor (array start) stays legal by existing contract. Demotion
reuses the EXACT existing pipeline (re-voice to user, strip
tool_addition/tool_removalper WC-022, drop when nothing remains;directive-only exempt anywhere and never legitimizing a neighbour).
The single left-to-right mutating pass means an early demotion
re-legitimizes a later run member — minimal repair, pinned by test.
insertBeforeTrailingSystemRun(messages, additions)—single source of truth for inserting ephemeral tail messages BEFORE any
trailing system run instead of after it (the append-after shape is exactly
what 400s case-2). No trailing run → byte-identical to plain append;
empty additions → same array reference (zero prefix-cache churn).
Behaviour-change disclosure
Only requests that previously 400ed are affected: they ship healed (same
semantics users already get from the precede-half heal). Every LEGAL layout
round-trips byte-identical — negative tests pin it, so stock cache breakpoints
are untouched. No config surface change; no new surface.
Out of scope (small follow-up PR)
Host takeover: delete the interim
insertBeforeTrailingSystemRuncopy insrc/util.ts(#5441) and route the steady-path tail-injection sites throughthe kernel export, so the rule lives in exactly one place. Deliberately NOT
here — it would collide with the open #5441 branch; do it once BOTH merge.
Then #5432 can close for good.
Tests & local verification
kernel/tests/issue5432-follow-side-heal.test.ts(10 tests): theFOLD-DRIVEN field regression through the real chain (
applyCompressioncovering the turns AFTER a never-fold sidecar leaves it behind an exposed
assistant → healed, delta text survives as user content,
tool_additionnever leaks outside system), client-side case-1 shapes (trailing /
mid-array behind assistants), verbatim guards ([user, system, assistant],
server-tool-result terminator, directive-only exemption), the chain
minimal-repair pin, and
insertBeforeTrailingSystemRununits.issue4457-sidecar-system-folds.test.ts: localorphanSystemscheckerupgraded to the two-sided rule (stronger assertion, same fixtures — still
green). All [Bug][anthropic] fold summary rendered as user right after a mid-conversation system message → 400 "role 'system' must precede an 'assistant' message" (0.1.204, since #3992) #4292 verbatim/heal pins pass UNCHANGED.
system-to-user, e2e-vertex-anthropic) 33/33 green; full local suite run
shows failures ONLY in pre-existing environment-dependent categories
(update-advisory feed, opencode/omp/pi native-host detection, opencode-v2
setup) — verified byte-identical failure set against a pristine-master
baseline (same 5 files, same fail counts: 1 + 6).
Owner decision recorded in #5438 (option A, 2026-11-02). Gate: kernel unit
suite + bili e2e green; human-only review tier unchanged.