Repository navigation
[PR] kernel: keep mid-conversation anthropic system messages verbatim (#3991) - #3992
Conversation
|
(ai: anthropic/claude-opus-5-5) Pushed f8221c1. The first CI run caught a Claude Code lane regression in Now only control-plane system messages (non-text blocks or message-level fields) are unrecoverable. Plain-text ones fold, keep their legacy id, and have their mark harvested. The PR body is updated with the details. The other red jobs ( |
…xianglei#3991) Pi declares tools activated mid-session as tool_addition blocks inside a mid-conversation role:"system" message (and emits empty system messages carrying message-level output_config). anthropicToCore flattened those into the user/assistant block stream and coreToAnthropic re-emitted them as role:"user": on 0.1.202 the tool_addition blocks were dropped (tools never reached the model); on master, with ranxianglei#3410's unknown-block passthrough, upstream 400s ("'tool_addition'/'tool_removal' blocks are only permitted within `role: "system"` messages"). Each wire system message now maps to exactly one core message carrying the original on a rawAnthropicMessage sidecar; coreToAnthropic re-emits it verbatim without merging adjacent user turns. Kernel-synthesized system cores (no sidecar) still map to user. protected.ts classifies the sidecar as unrecoverable so a fold can never destroy a tool declaration. Records the upstream rejection as WC-022 in the wire-contract ledger. kernel-change: wire保真 — anthropic mid-conversation system messages (ranxianglei#3991)
…ible (ranxianglei#3991) CI caught a Claude Code lane regression: Claude Code (2.1.300, Vertex fixture) appends a plain-text environment note as a trailing role:"system" message, and on that fixture it carries the request's only message-level cache_control. - Only system messages with non-text blocks or message-level extra fields (Pi's tool_addition deltas, output_config) are unrecoverable. Plain-text ones keep a numeric ref, so the recent-zone window no longer slides back one message (tests/e2e-vertex-anthropic.test.ts). - The system message's cache_control is harvested into cacheControls like any text block's, so the host's client-managed caching detection (ranxianglei#1637) and WC-010's breakpoint budget still see it. - The rebuild uses m.text when the kernel rewrote a plain-text system message (e.g. emergency truncation), and puts a host-stamped mark on the last block when the original has none. - A single-text-block system message keeps the legacy per-block id, so upgraded sessions see no ref churn. kernel-change: wire保真 — anthropic mid-conversation system messages (ranxianglei#3991)
master took 0.0.147 while this branch was open. kernel-change: wire保真 — anthropic mid-conversation system messages (ranxianglei#3991)
f8221c1 to
6eafb2a
Compare
|
(ai: anthropic/claude-opus-5-5) Rebased onto |
|
ework接单 |
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) 查重:不重复——检索 open+closed issue 与 PR,无其他条目针对同一缺陷(Anthropic wire 的会话中段 role:"system" 消息);相关条目:#3991(本 PR 源 issue)、#3997(master CI 红,解释本 PR 的红分片)。 独立验证(代码审查 + 本地定向 + CI 取证)验证基准: 代码审查:diff 干净(9 文件),机制核对无误——每条 wire role:"system" 映射为恰好一条 core(内容哈希 id、保留 wireIndex)、原始字节经 本地定向跑(全绿):kernel 钉死测试 13/13(含与 #3953 straddle-truncate 的交互用例);wire-contract 22/22(WC-022 正反例);e2e-anthropic 11/11、e2e-vertex-anthropic 2/2、pi-native 17/17、vertex-wire-fixture 8/8、incremental-lane / invariant-judge{,-e2e} / issue1470 / issue2499 全过;typecheck 干净。 CI 取证(最新状态):
行为变更披露(旧 → 新)
结构化八问
判定本 PR 代码与行为通过独立验证,未引入任何回归;CI 仅剩的红是 master 既有的 #3769 flake(#3997-B,#4015 修复后负载下仍未收敛,同等阻塞当前所有 PR)。技术面 ready——kernel 变更属 human-only review 层,需 owner 批准后人工合并;可接受 known-red 分片先行合并,或待 #3997-B 收敛后再合。我不执行合并。 |
[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) 已接单并完成独立验证,完整报告见本线程上一条(含八问、行为变更披露、CI 取证)。当前状态无变化,收敛为待你拍板的三件事: 现状(刚重新核实)
需要你拍板
一句话:代码可以合,等你点头;红的不是它的锅。 |
…y fixes (Windows EBUSY index.db close, storage-mode pin) for red shards (ranxianglei#3992)
(ai: vllm-qwen/qwen3.8-27b) 查重: 非新缺陷——本轮红与 #3997 子缺陷 B(#3769 分片负载下 hydration flake)签名完全一致;#3997 已由 owner 于 10-21 关闭(A/C 修复合并、master 绿)。不新立项。 结论:红与本 PR 无关,已通过同步 master 解决。新 head 本轮动作(auto-fix lane,第 1 次尝试):
八问:
附带一句:本轮结果同时佐证了 #3997 的关闭判断(子缺陷 B 随那两个测试 hermeticity 修复收敛),无需重开。 剩余待办(等你拍板,详见 floor 17):① kernel 变更批准(Kernel Boundary 人工层,本 PR 动 收工判定:CI 障碍已清除,PR 技术面就绪,等人工评审与合并决定。 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.723-master.5469a16 # this exact build
npm install -g billion-context@master # always the newest merged build
|
) openaiToCore keeps the raw wire message on mid-history system/developer cores (rawOpenaiMessage); coreToOpenai re-emits it byte-exact, re-laying plain text when kernel ops rewrote it. hasUnrecoverableMediaPayload: structured (non-plain-text) mid system never folds — DSH tool_addition style declarations survive instead of being stringContent-flattened.
…only bili's carriers re-voice (#4166, #4159, #4165) kernel (mirrors #3992 on the remaining two wires): - openai: mid-history system parses with a rawOpenaiMessage verbatim sidecar; coreToOpenai re-emits it byte-exact (re-blocked only when the kernel itself edited the text); non-plain-text system is unfoldable - responses: mid-history system/developer become positioned sidecar cores (refs + fold coverage + layout slots); head prefix still hoists (#377); coreToResponses re-emits raw verbatim; kernel 0.0.149 host: - systemToUser/revoiceMidSystemDevelopers take a predicate; both codecs re-voice ONLY isRenderedSummaryMessage carriers — client-origin mid system/developer is data, never re-voiced (#4159) - prepare-responses: #1638 re-typing hack retired (codec-native now, mode-invariant projection — the plugin->proxy degrade no longer re-projects or re-bills, #2867); #3713 masking retired (real cores) - compress-loop-responses: visibility markers + retrieval injections are born user; no blanket re-voice on the re-request - loop/adapter-openai: same carrier-only narrowing (loop/prepare parity)
…only bili's carriers re-voice (#4166, #4159, #4165) kernel (mirrors #3992 on the remaining two wires): - openai: mid-history system parses with a rawOpenaiMessage verbatim sidecar; coreToOpenai re-emits it byte-exact (re-blocked only when the kernel itself edited the text); non-plain-text system is unfoldable - responses: mid-history system/developer become positioned sidecar cores (refs + fold coverage + layout slots); head prefix still hoists (#377); coreToResponses re-emits raw verbatim; kernel 0.0.149 host: - systemToUser/revoiceMidSystemDevelopers take a predicate; both codecs re-voice ONLY isRenderedSummaryMessage carriers — client-origin mid system/developer is data, never re-voiced (#4159) - prepare-responses: #1638 re-typing hack retired (codec-native now, mode-invariant projection — the plugin->proxy degrade no longer re-projects or re-bills, #2867); #3713 masking retired (real cores) - compress-loop-responses: visibility markers + retrieval injections are born user; no blanket re-voice on the re-request - loop/adapter-openai: same carrier-only narrowing (loop/prepare parity)
… stored summary (ranxianglei#5620) The ranxianglei#5620 usage wall: in image-heavy workflows every message carrying remote-URL images / DeepSeek Files API file refs was permanently pinned by hasUnrecoverableMediaPayload, turn integrity pinned its partners, and the context floor climbed to ~70% of the window while folds kept pace with growth but could never lower it. Split the class (conservative enumeration): - new kernel/src/media-refs.ts: extractMediaRefs pulls known re-access handles (non-data: URLs, file ids, fileUris) from all five wire sidecar families; hasRefRecoverableMediaPayload = at least one handle; unknown opaque shapes keep pinning until their semantics are known - hasUnrecoverableMediaPayload media branches now return !hasRefRecoverableMediaPayload(msg); control-plane structured system branches (ranxianglei#3992/ranxianglei#4166) and refless shapes are untouched - apply path appends a deterministic MEDIA REFS section to the stored block summary (applySingleRange union incl. consumed-block carry; applyRefolds per-block carry), so handles survive folds/merges/ distillations mechanically — never model discretion (on the wire a URL part is often invisible to the summarizer); decompress restores the original losslessly either way - compression-rules: MEDIA REFS preservation added to T1/T2/T3 rules; golden regenerated (justification: ranxianglei#5620 root fix, three prompt fields extended additively, COMPRESS_PHILOSOPHY unchanged) - cosmetic: normalize one stray leading space on the tier-2 Open-objectives bullet (pre-existing, touched while inserting the sibling line) Legacy sessions self-heal without migration code: pre-ranxianglei#5620 BLOCKED refs on now-foldable media migrate to fresh numbers via the existing ranxianglei#3805 deferred-render path (carrier render deferred to next fold boundary). kernel version 0.0.179 -> 0.0.180
Fixes #3991
kernel-change: wire保真 — anthropic mid-conversation
role:"system"messages must round-trip verbatim. The codec inkernel/src/wire/anthropic.tsis the only layer that rebuildsmessages[]; the host side only sees the rebuilt body (justification in #3991).Problem
pi (models with
supportsMidConvoToolChanges) declares tools activated mid-session astool_additionblocks inside a mid-conversationrole:"system"message, and also emits empty system messages carrying message-leveloutput_config.anthropicToCoreflattened these into the user/assistant stream, andcoreToAnthropicre-emitted them asrole:"user":subagents_enablesucceeds, butsubagentis never callable.user, and upstream answers400 messages.2.content: 'tool_addition'/'tool_removal' blocks are only permitted within role: "system" messages.Change
kernel/src/wire/bili-message.ts: newrawAnthropicMessage?: unknownsidecar.kernel/src/wire/anthropic.ts:anthropicToCoremaps each wire system message to exactly one core message (rolesystem,wireIndexkept, deterministic id viaderiveCanonicalMessageId). Its text is the joined text blocks, with non-text blocks as[type], or[system]when empty.coreToAnthropicflushes the pending turn and re-emits the sidecar verbatim, so the message is never merged into adjacent user turns.user. This is pinned by a parity test.kernel/src/protected.ts:hasUnrecoverableMediaPayloadreturns true only for control-plane system messages: ones with non-text blocks (tool_addition/tool_removal) or message-level extra fields (output_config). Folding one would un-declare tools, and the host archives these nowhere. Plain-text system messages (isPlainTextSystemMessage) fold like any text message.role:"system"message (# Environment …), and there it carries the request's only message-levelcache_control. So:cacheControls, so the host's client-managed caching detection ([Bug] Anthropic lane never emits cache_control breakpoints — prompt cache can only ever cover the system head (~11K), the whole history re-bills at full price every request #1637) and WC-010's budget still see it;m.textwhen the kernel rewrote the text (e.g. emergency truncation), so the original can't undo it;On this lane the message used to be merged into the preceding user turn; it now goes upstream as the
systemmessage the client sent.validateAnthropicBody, a gate test, and a live-enforcement probe.kernel/package.jsonbumped 0.0.147 → 0.0.148, with both lockfiles updated to match.Modes
The codec is shared by plugin and proxy mode, and both rebuild through
coreToAnthropic. Verified live in proxy mode (BILI_PROVIDER_REWRITES→ patched proxy → capture → api.anthropic.com):subagentsuccessfully aftersubagents_enable.I1 violation: mid-history-drop.Tests
New
kernel/tests/anthropic-system-message-passthrough.test.ts(11 tests):wireIndexkeptWithout the fix, 7 of the original 8 fail and 1 passes (the parity pin).
Kernel suite:
npm testinkernel/, 1408/1408 pass (after rebase ontoe98ccda9).Bili suite (subset): wire-contract*, pi-native, anthropic-, e2e-anthropic, e2e-vertex-anthropic, vertex-wire-fixture, kernel-, invariant-judge*, incremental-*, issue2499, issue1470: 170 pass, including 任何一轮没有拿到 usage 报告的请求,都会把上下文分母放大 12–20×(nudge 读到 >1000% usage、假 EMERGENCY、基线冻结数分钟) #1839 (now green on master).
Real-data replay: the captured pi body and the Claude Code Vertex fixture through
anthropicToCore→coreToAnthropic. All system messages are byte-exact. The only diff is thetool_resultis_error:falsedrop, which was already on master.CI failures not from this PR: the only red rows are
#3769 upgraded pre-fold-gate state keeps rotation-hidden orphans hiddenand the SMT# SKIProw. The master run fore98ccda9(39447024068) fails#3769identically on every test shard.npm run typecheckand kerneltsc --noEmitare clean.Known gaps (not addressed, by design)
tool_additiondefinitions. This is the same trade-off anthropic wire silently drops unknown top-level blocks (incl. PDF document) when rebuilding history #3410 made for unknown blocks.kernel/src/wire/anthropic.tsandkernel/src/protected.tsalready fail prettier on master, so I left them unformatted to keep the diff minimal.