Skip to content

fix(affinity): project responses/google wire renders into the neutral identity domain (#2791) - #2839

Merged
ranxianglei merged 1 commit into
masterfrom
fix/2791-neutral-responses-google
Oct 12, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
fix/2791-neutral-responses-google

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 11, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to #2791 / #2812 / #2819 (gap scan asked for in #2791 discussion): scanning the remaining protocol matrix surfaced a REAL bug, not just a gap.

The bug

blockText() in src/affinity-neutral.ts only handled text | thinking | redacted_thinking content types. A responses-wire render (input_text/output_text blocks — the OpenAI Responses API shape used by codex /v1/responses proxy mode and dsh openai-responses) projected each turn to a canon-JSON blob instead of its text:

openai == responses  →  FALSE   (canon blob vs plain text)
openai == google     →  TRUE

resolve() and chainFingerprint hash neutralTurns() over the RAW wire input (src/prefix-affinity.ts:553, :923) with no pre-conversion. So a responses-wire client that switches protocol mid-session (or any cross-wire continuation) missed prefix-resolve and forked a pfa-* session — the exact #2791 silent-fold-loss class: fold ledger orphaned, history re-arrived unfolded, upstream re-folded from scratch, cache reset.

Red-green: stashing the src/affinity-neutral.ts hunk fails the new proxy e2e with got pfa-da71ba97cbee4154, pfa-f61ed9a2914da1ba.

The fix (src/affinity-neutral.ts)

  1. blockText(): accept input_text / output_text.
  2. Google merge boundary: gemini splits commentary and function call across contents[] items while openai merges across messages — model-role items now merge into the pending assistant run (mirror of the openai block-list branch).
  3. Responses merge boundary: function_call sibling items merge into a pending assistant run when one exists (codex splits commentary message + function_call items).
  4. Google functionResponse: unwrap transport wrappers ({output:}/{result:}/{content:} single-key-string conventions) before canon.

Coverage matrix (this PR's additions)

layer openai⇄anthropic openai⇄responses openai⇄google
unit (4-wire identity equality + prefix resolve) ✅ (existing) ✅ new ✅ new
proxy e2e (fold carried, pfas===1) ✅ (existing #2819) ✅ new ✅ new
real pi client ✅ (#2819) + reverse leg new ✅ new (fakeresp provider, openai-responses API) n/a (no google client)
real dsh client ✅ (#2819) n/a (persona rig is openai/anthropic; responses path exercised at proxy+pi level) n/a

Real-client notes: the pi plugin normalizes messages before resolve, so the pi lane is behavioral coverage (conv/session/fold/persistence across the switch), while the proxy-level test is the regression catcher for the projection itself (it fails on the pre-fix src).

Upgrade note

Responses: effectively ALL anonymous responses-wire sessions shift — every text-bearing turn (canon fix) plus the function_call merge boundaries.

Google: not just split renders — the merge also shifts the COMMON single-item shape (one model contents[] item carrying [text, functionCall] parts), and the functionResponse unwrap shifts every tool result whose response is a string or single-key-string object. Net: effectively all tool-using anonymous google sessions.

Per affected ACTIVE session crossing the upgrade boundary: one prefix-resolve miss → mint a new chain → one re-fold. No history loss (the client holds it); self-heals after the first fold round. Same migration class as the hv1→hv2 bridge (#2812). openai/anthropic projections are byte-identical pre/post (pinned by the existing hash tests), and plugin-headered clients (dsh/pi/codex-overlay) don't key on the hash at all. The single-key unwrap deliberately drops the wrapper key name ({output:"x"} ≡ {result:"x"}); a cross-convention collision requires a whole-chain match — acceptable heuristic, documented in the code comment.

Test plan

  • npx tsc --noEmit clean
  • unit: affinity-neutral (5), affinity-crosswire-fold (2) ✔
  • fork/adoption/resume/chain suite: 36 ✔
  • real pi e2e (ACP_TEST_E2E_NATIVE=1): cross-protocol switch + new openai→responses test, green twice consecutively; red-green at proxy layer

… identity domain (#2791)

blockText() only knew text|thinking|redacted_thinking content types, so a
responses-wire render (input_text/output_text blocks) hashed to canon-JSON
blobs instead of turns: openai==responses identity equality was FALSE while
openai==google was TRUE. resolve()/chainFingerprint hash neutralTurns(raw
wire input) (src/prefix-affinity.ts:553, :923) with no pre-conversion, so
any responses-wire client (codex /v1/responses proxy mode, dsh
openai-responses) forked a pfa-* session on a cross-wire model switch —
the #2791 silent-fold-loss class.

Also align the merge boundaries: gemini splits commentary and function
call across contents[] items while openai merges across messages — model
items now merge into the pending assistant run (mirroring the openai
block-list branch); responses function_call sibling items merge into a
pending run when one exists; google functionResponse unwraps its transport
wrapper ({output:}/{result:}/{content:} single-key-string conventions).

Tests:
- unit: all four wire renders (openai/anthropic/responses/google) project
  to IDENTICAL neutral turns; mid-session switch onto responses AND google
  wires resolves via:"prefix" with the same sessionId in both directions.
- proxy e2e: cross-wire switch onto the responses and google lanes keeps
  the session AND its fold (pfas===1, carrier attached, no re-fold) — red
  green proven against the pre-fix src (pfa fork fails the test).
- real-client pi e2e: openai→responses switch (fakeresp provider on the
  openai-responses API) keeps conv/session/fold; the anthropic-switch test
  gains the reverse (anthropic→openai) leg.

Note: responses sessions' neutral hashes shift (text-turn canon fix +
merge boundaries) — a live responses-wire anonymous session will miss
prefix-resolve ONCE on upgrade and re-fold, same migration class as the
hv1→hv2 bridge, smaller population.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: fix/2791-neutral-responses-google (916d24c)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3142-pr.2839.15a321c

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

Option B — Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到。这是 #2791/#2812/#2819 的 gap-scan 后续 PR(responses/google wire 的 neutral identity 投影 bug)。开始按 PR review 规范审查:

计划

  1. 方向关:读原 issue 链(dsh anthropic-messages lane bypasses native-intercept fetch patch: anonymous pfa-* session, fold ledger orphaned, silent ~182s re-fold on cross-protocol switch #2791 → fix(affinity): append-continuation rung + hv2 ladder preference (#2807 regressions) #2812 → test(e2e): real-client cross-protocol switch coverage for pi and dsh (#2791) #2819),核对 blockText() 只认 text|thinking|redacted_thinking 是否属实、修法是否打在根因层;
  2. 回归关:清点 src/affinity-neutral.ts + src/prefix-affinity.ts 触碰路径的既有行为(openai⇄anthropic 既有 lane、hv1→hv2 bridge、chain fingerprint 语义);
  3. 代码/测试关:通读 diff、核查 red-green 证据与测试断言有效性;
  4. CI 状态 + 分支 freshness(当前 behind master)。

初判后给结构化八问结论。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review 完成 — 结论:可合(mergeable)。以下为结构化八问。

验证证据(先放硬证据)

  • Red-green 本地复现成功,与 PR 声称逐字一致:把 src/affinity-neutral.ts 单独回退到 merge-base(47386e6f)版本后跑新增 proxy e2e → openai 相位建立 pfa-da71ba97cbee4154(depth 1→23),responses 切换处 new session pfa-f61ed9a2914da1ba at depth 25,测试 not ok;恢复修复文件 → ok。回归捕获器有牙,且红态 ID 与 issue 正文完全吻合。
  • Rebase 到当前 master(cd02d2dc5)干净(master 仅领先 release v0.1.194 + 无关的 better-sqlite3 移除,与本 PR 零文件重叠);rebased head 上 tsc --noEmit 干净;定向套件 8 个文件 65/65 绿(affinity-neutral、affinity-crosswire-fold、prefix-affinity、prefix-affinity-simhash、prefix-affinity-orphan、fork-adoption、fork-adoption-wires、resume-adoption-wires)。全量回归按纪律交给 CI。
  • CI 在 head 916d24c 全绿:ubuntu/windows 22+24 双矩阵、real pi/opencode/dsh/codex 各 lane、hermetic registry、config-surface-guard/version-guard/kernel-guard 等全部 success——含 real pi lane(新 openai→responses 列测试所在文件)。

八问

1. 是什么问题:#2791 建立的 wire-neutral identity 投影对 responses/google 两条 wire 不完整——responses 文本轮被投影成 canon-JSON blob 而非纯文本、两侧 assistant run 的合并边界不一致,导致同一会话换协议重放时哈希对不上,prefix-resolve 落空,fork 出 pfa-* 孤儿会话(fold ledger 孤立、历史未折叠重到达、从零 re-fold、缓存归零)。是 #2791 静默折叠丢失类的真实残留 bug,不只是 gap。

2. 什么原因导致的:根因在投影函数本身(生产者层):① blockText() 不认 input_text/output_text → responses message items 落入 foreign/canon 兜底;② responses function_call 兄弟项与 google model 项都不并入 pending assistant run → 跨 wire turn 结构分叉(openai 一个消息 vs 两条 wire 拆成多项);③ google functionResponse.response 被 canon 包裹而 openai tool content 是裸字符串。哈希点在 src/prefix-affinity.ts:553/:923 对 RAW wire input 跑 neutralTurns(),投影不全=必然分叉。修法打在源头,不是堵症状。

3. 影响范围与严重程度:触发条件明确——匿名(无身份头)会话 + 会话中切协议:codex /v1/responses proxy 模式、dsh openai-responses、gemini lane 的跨 wire 续接。后果严重但限于该条件面:会话级状态静默丢失(整段历史重付+re-fold,#2791 实测 263% window/~182s)。有身份头的客户端不受影响。

4. 怎么修复的:补全 neutralTurns() 纯投影(4 个 hunk,全在 src/affinity-neutral.ts):responses 文本类型并入 blockText();google model 项与 responses function_call 项在存在 pending run 时并入(镜像 openai 既有合并语义);google functionResponse 先解包传输包装(string / 单键 string 对象)再 canon。无状态、同 wire 字节稳定、neutralTurnCanonical 的 v1 形状保真(#2265 simhash 标定)均保持。

5. 是否有回归问题:未发现无意回归。逐项清点触碰路径的既有行为:openai/anthropic 投影逐字节不变(新分支只响应 responses 专属 type 名与 google parts 形状;65/65 定向绿佐证);hv1/hv2 域种子分离("v2"/"v2i\0" vs raw)保证升级后旧条目不可能跨域误配,只会按披露机制一次性失配后重新 mint;400 no-identity 路径、上游转发字节、持久化格式、kernel 均未触碰;#1115/#629 反收养纪律未被削弱(byte-exact 阶梯 + user-word 锚点原样)。有意行为变更(已披露类):responses/google 匿名会话的 neutral 哈希整体平移 → 升级后每个受影响活跃会话 miss 一次 prefix-resolve、mint 新链、re-fold(与 #2812 hv1→hv2 桥同类)。⚠️ 一处披露措辞不准(minor,建议改 PR 正文):"Google shifts only at split-render boundaries (rare)" 低估了面——google 单个 model 项同时带 [text, functionCall] parts 的常见单条渲染同样会因合并而平移,加上所有 response 为 string/单键 string 对象的 tool result 解包平移;实际面≈所有带工具的匿名 google 会话。机制与严重度不变,只是人口比"rare"大。另注:单键解包使 {output:"x"} 与 {result:"x"} 同哈希(键名从哈希中丢弃)——需整链匹配才可能误配,属可接受启发式,代码注释已说明。

6. 是否破坏缓存:不影响上游 prefix-cache 机制本身;本修复恰恰移除一个缓存归零源(fork→re-fold)。升级期一次性 re-fold 是已披露成本。

7. 是否改了设计方向:没有转向——把 #2791/#2807/#2812 的 neutral-domain 设计延伸到剩余两条 wire 家族,架构、边界、数据流不变。

8. 是否引入了新的配置:无。(测试里用到的 promptCache.routing 是既有字段,config-surface-guard 绿。)

其他

  • diff 整洁:5 文件全部目的相关,单一线性提交,无空行/锁文件噪音。分支名 fix/2791-neutral-responses-google 不符合 YYYY-MM-DD_short-title 惯例——cosmetic,owner 自开,不拦。
  • behind master 6 commits 非阻塞(house rule),且我已验证当前 master 上 rebase 干净、定向全绿;如需我可以把 rebased head 推上去,默认不动你的分支。
  • 小问题处置:唯一发现(第 5 问的披露措辞)属 PR 正文表述而非代码缺陷,不在我直接改的范围——建议你修正 upgrade note 后再合;代码侧无需任何 follow-up commit。

判定:可合。 唯一建议动作:修正 PR 正文中 google 平移人口的措辞(可选但建议),然后直接 merge。

@ranxianglei ranxianglei added the p1 label Oct 12, 2026
@ranxianglei
ranxianglei merged commit 011bf5e into master Oct 12, 2026
27 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.295-master.011bf5e (rolling master tag).

npm install -g billion-context@0.0.295-master.011bf5e  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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.

1 participant