Skip to content

[PR] fix: keep tool_result-first google part order for Claude targets (#5392) - #5397

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-11-01_claude-google-part-order
Closed

ranxianglei wants to merge 3 commits into
masterfrom
2026-11-01_claude-google-part-order

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

kernel-change: make normalizeGoogleUserPartOrder/coreToGoogle model-aware so Claude-through-Google targets keep tool_result-first part order. The host side cannot solve this: the offending reorder happens INSIDE the kernel codec (coreToGoogle's flushUser), and the stable text-first partition is non-invertible (an interleaved [fr,text,fr,text] collapses to [text,text,fr,fr]), so no host-only workaround exists. Owner approval via decision request #5393 (option A).

Summary

Claude-family models routed through Google's v1internal / Cloud Code Assist wire (e.g. Antigravity, pi-antigravity) behind bili returned HTTP 400 on any user turn carrying BOTH a tool response and injected text (ACP tags / nudge / system-drift note):

messages.N: 'tool_use' ids were found without 'tool_result' blocks immediately after … Each 'tool_use' block must have a corresponding 'tool_result' block in the next message.

Reproduced on 0.1.209 and 0.1.210.

Root cause

WC-018 (#3159) makes bili stable-partition plain text before functionResponse parts in the Google codec (normalizeGoogleUserPartOrder, called from coreToGoogle). That text-first order is what Gemini/Vertex require. But Google's v1internal backend translates role:"user" parts into Anthropic content blocks in order, and Anthropic requires all tool_result blocks to lead the user message that follows an assistant tool_use turn — the opposite order. The unconditional reorder emitted exactly the bytes Anthropic rejects.

Fix

Make the reorder model-aware. An optional target-model is threaded through every google-wire part-order boundary; when it matches /claude/i the natural responses-first (tool_result-first) order is kept, otherwise the existing text-first WC-018 behavior is byte-identical.

  • kernel/src/wire/google.ts — normalizeGoogleUserPartOrder(parts, {model?}) returns the input untouched for a Claude target; coreToGoogle(messages, {model?}) threads it. Kernel version 0.0.175 → 0.0.176.
  • Host boundaries forward the request model (googleModel ?? v1Model on the proxy path incl. countTokens, modelVersion in the loop adapter): prepareGoogle, appendGoogleNudge, appendTrailingUserText (degenerate-retry nudge + fake-completion hint), and the outbound chain-checkpoint carrier.
  • Validation-parity fake skips WC-018 for /claude/i targets; the WC-018 ledger entry documents the carve-out (ledger only grows).

Behavior change (old → new)

Target through the Google wire Before After
non-Claude (Gemini/Vertex) text-first (WC-018) unchanged — text-first
Claude (/claude/i) text-first → upstream 400 responses-first (tool_result leads)

Only the previously-broken Claude-through-Google path changes; every other path is byte-identical. This restores availability (a hard 400) for that lane with no other behavioral drift. No new config/env/CLI surface.

Tests

  • kernel/tests/wire-google.test.ts — two new pins: normalizeGoogleUserPartOrder returns the input unchanged (same reference) for Claude models while still reordering without a model; coreToGoogle emits functionResponse-first under a Claude model vs text-first by default.
  • tests/issue5392-claude-google-part-order.test.ts (new) — real-proxy integration: a Claude-named target forwards the trailing user content with functionResponse first and zero fake violations; the same turn on a non-Claude target still normalizes text-first (guards against the carve-out overreaching / regressing Vertex rejects tool responses when Bili appends trailing nudge text #3159).
  • Existing tests/issue3159-vertex-nudge-order.test.ts and tests/wire-contract.test.ts remain green (no WC-018 regression).

Verified locally on the rebased head (origin/master 7578e8184): typecheck clean, build clean, 46/46 targeted tests pass. Full-suite verdict = remote CI.

Closes #5392. Implements approved option A from decision request #5393.

ci-check added 2 commits November 1, 2026 09:51
…argets (#5392)

Claude-family backends routed through Google's v1internal / Cloud Code Assist wire translate role:user parts into Anthropic content blocks IN ORDER and require every tool_result(functionResponse) to LEAD the user message that follows an assistant tool_use turn — the opposite of the WC-018 text-first rule (#3159). Thread an optional target-model through normalizeGoogleUserPartOrder and coreToGoogle; when it matches /claude/i the input order is returned untouched so the natural responses-first shape survives. Default (no model) behavior is byte-identical.
#5392)

Forward the request model (googleModel ?? v1Model on the proxy path incl. countTokens, modelVersion in the loop adapter) through each boundary that reorders google user parts: prepareGoogle, appendGoogleNudge, appendTrailingUserText (degenerate-retry nudge + fake-completion hint), and the outbound chain-checkpoint carrier — so Claude-through-Google targets keep tool_result-first order end-to-end instead of 400ing upstream. The validation-parity fake skips WC-018 for /claude/i targets and the WC-018 ledger entry documents the carve-out. Adds tests/issue5392-claude-google-part-order.test.ts pinning both directions (Claude keeps tool_result-first; non-Claude stays text-first).
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-01_claude-google-part-order (31763b6)

Latest build — install this exact version

npm install -g billion-context@0.0.7638-pr.5397.17f5f5e

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
31763b6 billion-context@0.0.7638-pr.5397.17f5f5e build
222a265 billion-context@0.0.7629-pr.5397.355e0eb build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5397.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix: keep tool_result-first google part order for Claude targets (#5392) [PR] fix: keep tool_result-first google part order for Claude targets (#5392) Nov 1, 2026
…ies for google

Review of this PR found a completeness gap on secondary paths. prepareGoogle
returns Prepared with the model nested under google:{system,model}, and the
google lane early-returns before handle.ts assigns top-level Prepared.model, so
prepared.model is undefined for google requests. The three outbound boundary
sites added here that read prepared.model (outbound chain-checkpoint stamp,
degenerate-terminal continuation nudge, fake-completion hint) therefore silently
fell back to the WC-018 text-first reorder for Claude-through-Google targets,
re-introducing the exact 400 #5392 fixes on those retry/stamp turns.

Fall back to prepared.google?.model (the same canonical source the loop adapter
already uses via pickAdapter) at those sites. Inert for every non-google lane
(prepared.model is defined there); activates only where prepared.model is unset.

Verified: typecheck clean; issue5392/issue3159/wire-contract targeted suites green.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重:不重复。 这是修 #5392 的 PR(#5397),落实 owner 在 #5393 拍板的方案 A;检索 open+closed 无同主题竞争条目。WC-018/#3159 是「被本 PR 打例外」的那套 Gemini text-first 归一,属机制来源而非重复项。

标签: bug · antigravity · scope/kernel · p2(缺陷;客户端=Antigravity,v1internal/Cloud Code Assist 是其专属线;领域=kernel 内 Google codec;严重度沿用 triage 的 p2——但见第 3 问,这条车道一触发就是硬 400、工具循环整体停摆,owner 若要按「可用性优先」上调 p1 我认同)。

独立审核结论

方向对、kernel 改动干净、主路径修好且有双向测试钉;但我独立审 diff 时发现并就地修掉了一个真实的完整性缺口(见下「⚠️ 缺口」)。以下判断是我自己逐段读 diff/追调用链得出的,不是照抄 issue 或上一轮 triage。

  • kernel 侧(kernel/src/wire/google.ts):新增 CLAUDE_WIRE_MODEL = /claude/i;normalizeGoogleUserPartOrder(parts,{model?}) 命中 claude 直接返回原数组(同一引用、零拷贝),否则走原 WC-018;coreToGoogle(messages,{model?}) 透传。默认(无 model)路径字节不变。版本 0.0.175→0.0.176,bump 合规。
  • host 侧模型信号(主链路都对):主转发 prepareGoogle 用局部 model(= handle.ts 的 googleModel ?? v1Model,v1internal 下回落到信封 meta);countTokens 同参;compress-loop 重建经 pickAdapter → createGoogleAdapter(google?.model)。三条主链路都拿到正确目标模型。✓
  • 测试质量高:kernel 两新 pin(claude 返回同一引用 + coreToGoogle 出 functionResponse-first,大小写都测)+ 新集成测试跑真 proxy+真 kernel,断言实际转发字节——claude 以 functionResponse 领先、gemini 仍以 text 领先(双向钉,防 carve-out 越界回归 Vertex rejects tool responses when Bili appends trailing nudge text #3159)。WC-018 账本只增(补例外说明);fake 的 validateGoogleBody 加 model 参数,VALIDATORS.google 仍是同一函数,无覆盖损失。✓

⚠️ 我发现并修掉的缺口

prepareGoogle 的返回值把 model 塞进 google:{system,model},而 google 分支在 handle.ts 提前 return,没走到给其它车道赋顶层 Prepared.model 的那行——所以 google 请求的 prepared.model 恒为 undefined。本 PR 往 relay 三个出站边界塞的都是 prepared.model:①出站 chain-checkpoint stamp(受 chainEgressStamp 门控)、②degenerate-terminal 续写重试、③fake-completion 提示重试。这三处对 google 拿到的都是 undefined → Claude 在这些重试/stamp 回合仍会被 WC-018 排成 text-first,等于把 #5392 那个 400 又引回到这几条边路上。

  • 报障的正主(普通工具轮)走 prepareGoogle 局部 model,已正确修好;缺的是这三条次要边界。
  • 修法(已推 commit 31763b6,独立标注):三处改 prepared.model ?? prepared.google?.model(与 loop 用的同一个规范源)。对非 google 车道 prepared.model 有值、?? 恒短路,字节不变;只在 google(顶层未设)时激活。刻意不碰 resolveCompress/fuse 等其它读 prepared.model 的地方(避免 visibilityMarkers 之类副作用)。
  • 验证:typecheck 干净;issue5392 / issue3159 / wire-contract 定向套件全绿(28 pass)。诚实说明:这三条重试/stamp 边界目前没有专门的行为钉(现有测试覆盖的是主转发路径),建议后续补一条 claude-google 重试/stamp 的最小 e2e;CI 真跑起来也会兜底。

结构化八问

  1. 是什么问题:claude-* 走 Google v1internal/Antigravity 线时,只要某轮 user 消息同时带「工具结果」和「注入文本」(ACP tag / nudge / system-drift note),上游就硬 400('tool_use' ids were found without 'tool_result' blocks immediately after)。
  2. 什么原因导致的:kernel Google codec 的 WC-018(Vertex rejects tool responses when Bili appends trailing nudge text #3159)无条件把纯文本排到 functionResponse 前(Gemini/Vertex 要的 text-first);但 v1internal 后端把 user parts 按顺序译成 Anthropic 内容块,而 Anthropic 要求 tool_result 必须领先——正好相反。根因是这套顺序归一缺了「目标模型」这个维度,不是某个注入点漏了。
  3. 影响范围与严重程度:窄组合(claude-* × Antigravity/v1internal × 有工具轮的会话);一旦触发该车道工具循环每轮硬 400、完全不可用(体验毁灭级),但有绕行(换原生 Anthropic 路由 / 本地 patch dist)。非数据丢失、非缓存泄露。定 p2(沿用 triage),owner 可按可用性优先上调 p1。
  4. 怎么修复的:把重排改成「按目标模型感知」——每个 google-wire part-order 边界透传请求模型,命中 /claude/i 保留自然 responses-first(tool_result 领先),否则维持 text-first。kernel 加可选 {model?};host 各边界透传;另补上我审核发现的 relay 三条次级边界(见上)。
  5. 是否有回归问题:逐项审过既有行为——非 claude(Gemini/Vertex)text-first 逐字不变(Vertex rejects tool responses when Bili appends trailing nudge text #3159 三 pin + wire-contract 仍绿);countTokens 与 generateContent 共用同一份模型信号;默认(无 model)路径 byte-identical;我的补充改动对非 google 车道字节不变。无回归。
  6. 是否破坏缓存:不影响前缀缓存锚点——非 claude 路径字节不变,claude 路径本就是修好的正确形状;nudge/anchor 逻辑未动。
  7. 是否改了设计方向:否。仍是同一个 normalize 机制,只加一个模型门控分支;架构/边界/数据流没转向。
  8. 是否引入新的配置:否。无新 env/config/CLI;判定直接用请求里已有的目标模型名。

行为变更披露(old → new)

经 Google wire 的目标 旧 新
非 Claude(Gemini/Vertex) text-first(WC-018) 不变
Claude(/claude/i) text-first → 上游 400 responses-first(tool_result 领先)

只动了原本就坏的 Claude-through-Google 路径,恢复其可用性,无其它漂移。

CI 状态(读镜像缓存)

PR head 222a265:mergeable_state=blocked,test / static-gates / e2e / dsh / codex 等 lane 全部显示 skipped,仅 mysql-contract success——目前没有全量绿判据,看着像这次 run 没真正执行测试矩阵(可能被 path-filter 或调度跳过)。需要一次干净重跑才能作为合并依据。我已 push 31763b6,请让 CI 在新 head 上真跑一遍再合。

收工判定

方向与实现成立,kernel 改动合规、主路径修好且有双向测试钉;我额外堵上了 relay 三条次级边界的同源缺口(commit 31763b6)。代码层面可合,前提:①CI 在全量 lane 上真跑绿(现显示 skipped,需重跑确认);②(建议)补一条 claude-google 重试/stamp 边界的行为钉。kernel 变更按 Kernel Boundary 走人工 review,不 auto-merge。

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: sglang-gpu0/qwen3.8-27b) 已合入 master: 本 PR 的改动(含 kernel-change 声明,owner 批准 #5393 option A)经「codex 车道小修合集第三辑」#5971 送审合入;kernel 版本按 master 现值 bump 到 0.0.189。本 PR 关闭以免重复。

@ranxianglei ranxianglei closed this Nov 8, 2026
yanhuojiyusheng pushed a commit to yanhuojiyusheng/billion-context that referenced this pull request Nov 9, 2026
…gle targets (for ranxianglei#5397, closes ranxianglei#5392)

kernel-change: make normalizeGoogleUserPartOrder/coreToGoogle model-aware so Claude-through-Google targets keep tool_result-first part order. The host side cannot solve this: the offending reorder happens INSIDE the kernel codec (coreToGoogle's flushUser), and the stable text-first partition is non-invertible, so no host-only workaround exists. Owner approval via decision request ranxianglei#5393 (option A). Kernel version 0.0.188 -> 0.0.189 (master was already at 0.0.188 past this PR's 0.0.175->0.0.176 base bump).
@github-actions github-actions Bot mentioned this pull request Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Host-side tool-result pruner mutates folded messages → permanent fold residual (614 ids) re-reported every turn, v0.1.210 plugin lane

1 participant