Skip to content

feat(proxy): serve the Google/Gemini wire (#866) - #867

Merged
ranxianglei merged 9 commits into
ranxianglei:masterfrom
joshwapohlmann:2026-09-16_866-google-wire
Sep 20, 2026
Merged

ranxianglei merged 9 commits into
ranxianglei:masterfrom
joshwapohlmann:2026-09-16_866-google-wire

Conversation

@joshwapohlmann

Copy link
Copy Markdown
Contributor

Solution

Native support for the Google/Gemini wire in the proxy, so a Gemini conversation is tracked, folded and re-emitted instead of passing through verbatim.

Detection and identity: googlePathKind(urlPath) and googleModelFromPath(urlPath) classify :generateContent, :streamGenerateContent and :countTokens and resolve the model, WireProtocol gains google, and requestModel prefers the body's model with the path as the fallback. Anonymous Gemini traffic is linked into a session by prefix affinity over contents (conversationSignalGoogle), since the wire carries no client session header.

Folding: prepareGoogle / prepareGoogleCountTokens run the kernel path with strategy and tool injection (injectGoogleTool), appendGoogleNudge folds a nudge into a trailing non-model content rather than adding one, pickAdapter gained a google entry, and src/loop/adapter-google.ts plus src/stream-google.ts (rewriteGoogleJsonResponse) serve the loop and non-streaming paths. googleThoughtSignature is written back on replayed reasoning and carried through ToolCallEmit.

Accounting: usageTotals / promptInputTotal / usageOutputTotal map input = promptTokenCount minus cachedContentTokenCount and output = candidatesTokenCount plus thoughtsTokenCount; the window table gained the Gemini 3 line; outputBudgetField gained generationConfig.maxOutputTokens with matching read/write helpers; overflow detection understands the Gemini token phrasing.

Auxiliary paths: tag stripping, image estimation (inlineData / fileData), strip-images, in-band truncation frames, the fake-completion hint, the preflight summary payload and the plugin's usage sniffing all learned the wire.

Two defects fixed on the way, both Gemini-only in practice

  • The preflight summary was posted to the client's own URL. That endpoint is :streamGenerateContent, which answers SSE whatever the request asks for, while the summary parser only engaged for a streaming client, so the reply read as empty: carried 41 SSE event(s) but no summary text, eight attempts, then preflight fail-fast 502 and a 300s dead-end cooldown. The body's shape now decides the parser (SSE_DATA_LINE_RE plus a JSON-vs-SSE branch).
  • The proxy-synthesized tool result carried no tool name, so the wire emitted functionResponse{name:"unknown"} answering functionCall{name:"compress"} and Gemini rejected the whole request with 400 INVALID_ARGUMENT. Unlike the other wires, this one pairs a response to its call by name.

Why native rather than the OpenAI-compatibility endpoint

/v1beta/openai/chat/completions would be a much smaller change, but the translation drops thoughtSignature, which Gemini 3 requires on every replay of a function-call part, so a folded conversation is rejected as soon as the client does tool calls.

Tests

tests/adapter-google.test.ts (9, request building and SSE rewriting), tests/e2e-google-compress.test.ts (end-to-end fold through the loop with a strict Gemini shape validator: role alternation, one payload per part, signatures only on model parts, every functionResponse named and answering a call the conversation already sent), tests/preflight-google-summary.test.ts (2, the SSE and JSON summary paths), and four google cases in tests/preflight-sse-extraction.test.ts.

Verification

npx tsc --noEmit -p tsconfig.build.json clean; proxy suite 1669 tests with 1661 passing, the only failures being the six environmental ones that upstream also shows (tests/launcher.test.ts spawning real launchers, and two tests/plugin-agent.test.ts cases about a proxy being absent). Live against gemini-3.8-flash: a proxy-owned compress call folds an over-window conversation (context 15914 tokens reached preflight target 10000 … preflight compressed 2 range(s), ~7399 tokens saved … rebuilding payload), the forwarded request carries systemInstruction with valid alternating contents, and the response returns HTTP 200 with no INVALID_ARGUMENT.

Dependency

Requires the kernel codec: acp-kernel PR #318 (issues #317). The acp-kernel devDependency is pinned at 0.0.74, so npm ci && npm run build here cannot resolve the Google exports until a kernel release carries them. This repository's convention is a separate bump commit after a kernel release (733e919 deps: bump acp-kernel 0.0.73 -> 0.0.74, 2f43f26), which is the missing piece for this branch's CI.

joshwapohlmann pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 16, 2026
joshwapohlmann pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 16, 2026
A chunk carrying a text part AND a functionCall part is replayed whole at
settle, so its text must not also go out immediately. The guard counted only the
calls seen before the text part in iteration order, so `[text, functionCall]` —
the shape the reviewer named — forwarded the chunk as the text frame and the
settle replay forwarded it again, handing the client the text and the call
twice. The reasoning branch had no guard at all and had the same problem.

The edited copy the tag filter leaves behind had a second hole: it kept every
sibling part, so a PROXY call (which settle drops, because the proxy executes it
itself) rode out to the client inside the filtered text frame. The copy now
drops sibling call parts; settle replays them when they are real.

Covered by two tests: a split chunk delivering its text and its call once each,
and an edited frame carrying no proxy call. Reported by the maintainer's review
agent on PR ranxianglei#867 (finding 1); the cosmetic role on finish-only clones is fixed
in the same pass.
@joshwapohlmann

Copy link
Copy Markdown
Contributor Author

Both findings are fixed on the branch, and your CI analysis is accepted with one correction of mine.

Finding 1 — the split chunk. Confirmed in the code, and it was worse than one branch. The guard tested callIndexes.length === 0, which only counts calls seen before the text part in iteration order, so [text, functionCall] forwarded the chunk as the text frame and the settle replay forwarded it again. The reasoning branch had no guard at all and did the same. Both now consult one hasCallPart computed over the whole chunk before any part is examined, so a chunk that settle will replay never goes out immediately.

The second half of your finding was the sharper one: the edited copy an emptied tag leaves behind kept every sibling part, so a proxy call — which settle drops precisely because the proxy executes it itself — rode out to the client inside the filtered text frame. The copy now drops sibling call parts; real ones are replayed by settle anyway. So your suggested fix is what shipped, extended to the reasoning path.

Two tests pin it: a split chunk whose text and call each reach the client exactly once, and an edited frame that carries no proxy call (asserted per frame, because a proxy call's own structured event legitimately carries a frame).

Finding 2 — cosmetic: fixed. cloneChunk now defaults content.role to model when it rewrites parts, which is what the file's own partsOf helper asserts anyway.

Finding 3 — your CI analysis, accepted, and my earlier statement was incomplete. You are right that GoogleRequestBody is only one of ~22 errors: the wire exports (coreToGoogle, googleToCore, googleSystemText, injectGoogleSystem, conversationSignalGoogle, the five type exports) and the six *_TOOL_GOOGLE constants are all missing from the pinned kernel, plus the cascading TS7006 at src/server.ts:2269. And your key point stands: there is no release to bump to yet — acp-kernel@latest is 0.0.75 and contains none of it, #318 is unmerged, and there is no prerelease tag. My earlier note that only one export was missing came from the first error line, not from a full local run; thanks for running it.

The sequence is agreed: #318 merges (human-only) → CI publishes → the deps: bump acp-kernel commit lands here. Yes please to your offer to push that bump commit to joshwapohlmann:2026-09-16_866-google-wire once the version is live, with the exact pin and a lockfile refresh; a PR-specific prerelease pin would be wrong, since the proxy pins released versions. I will ping you the moment #318 merges — I am watching that thread and will see it.

Commit: fix(loop/google): never forward a split text+functionCall chunk twice, on top of e377eb9.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Verdict: both fixes verified at the code level — LGTM pending the agreed dependency sequence. The branch is current: merge-base is the present master head d650db7, 6 commits ahead / 0 behind, so no rebase is needed right now. Runtime execution of the suite is still impossible against the pinned kernel (as established), and my local attempt this round to unblock that hit an environment failure — details below.

Finding 1 — verified. src/loop/adapter-google.ts:435 computes hasCallPart once over the whole chunk before any part is examined, and both the text branch (:475) and the reasoning branch (:457) consult it, so neither can forward a chunk that settle will replay. I traced every chunk shape through settleToolCalls + filterRealCallChunk:

  • [text, realCall]: immediate frame suppressed; settle returns "keep" and replays the original JSON verbatim → text and call each reach the client exactly once (your new test pins it).
  • [thought, realCall]: same via the new reasoning guard — previously unguarded.
  • [editedText, proxyCall]: the edited copy now drops sibling call parts (the .filter at :480), so the immediate frame carries only prose; settle drops proxy-only chunks → no leak (second test pins it).

One residual consequence worth knowing (non-blocking): in a round with no real calls, a chunk [uneditedText, proxyCall] now loses its preamble text on the wire — the immediate frame is suppressed by hasCallPart, settle drops the all-proxy chunk, and the loop core forwards no raw-less round-1 text event (src/loop/core.ts:299-305 only re-synthesizes for round > 1). Pre-fix, the [text, call] order leaked the proxy call into the immediate frame while the [call, text] order already lost the text; the fix makes both orders consistently lose the preamble instead of leaking. Since executing the proxy call supersedes the turn (marker frame + re-requested response), I accept that trade — flagging it as known behavior.

Finding 2 — verified. cloneChunk (:85) spreads { role: "model" } before the original content, so the default applies only when role is absent. Correct and minimal.

Finding 3 — dependency state independently re-verified. acp-kernel@latest is 0.0.75; the newest prerelease tag is pr-316 (0.0.75-pr.316.181); no published build carries the #318 codec. CI stays red until merge → publish → bump, exactly as you laid out. I also reproduced the import-time failure locally: SyntaxError: The requested module 'acp-kernel' does not provide an export named 'ABSORB_TOOL_GOOGLE' — expected until the bump lands.

Environment failure (for ops): while building acp-kernel from #318 head (006f91a) locally to actually run the suite, the sandbox disk filled up mid-install (ENOSPC: no space left on device, write); every subsequent command and file write — including cleanup to reclaim space — is blocked. Not a security issue; reporting per policy. Net effect: this round's verification is inspection-level (full trace above), not runtime. I will run the full suite + typecheck + build on the bumped head before declaring mergeable — that stays the gate, alongside the two new tests passing there.

Two minor nits (non-blocking, report-only):

  1. adapter-google.ts:480 — the new filter (p) => p.functionCall === undefined dereferences parts that could be null, while the rest of the loop guards non-object parts defensively (:435, :439). A malformed upstream chunk with a null sibling part plus tag-edited text would throw mid-stream. Suggested: (p) => !p || typeof p !== "object" || p.functionCall === undefined.
  2. Tag-echo + real call in the same chunk: the immediate frame is clean, but settle's "keep" replays the original JSON verbatim (:370-371), so model-emulated render tags still reach the client through the replay. That's a pre-existing tension between the tag filters and verbatim replay (requires model misbehavior to trigger); noted for the record, not blocking.

Sequence: standing by for your ping when #318 merges. I'll then push deps: bump acp-kernel (exact pin + lockfile refresh) to joshwapohlmann:2026-09-16_866-google-wire and re-run full local verification on the bumped head.

修了什么问题/为什么/能否合并:已在代码层面逐路径核对 split-chunk 双发修复(text/reasoning 两分支统一改为整块 hasCallPart 守卫、编辑副本剥离兄弟 call 部件)与 cloneChunk role 默认值修复,逻辑正确、分支与当前 master 同步;本地运行时验证因沙箱磁盘写满(ENOSPC,已报运维)受阻,待 #318 人合→CI 发布→bump 提交落地后跑全量测试+typecheck 转绿即可合并。

joshwapohlmann pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 16, 2026
The guard that keeps a proxy call out of a tag-edited frame dereferenced every
sibling part, while the rest of the loop treats non-object parts defensively. A
malformed upstream chunk carrying a null part alongside tag-edited text therefore
threw mid-stream and took the turn with it.

Found in review of ranxianglei#867; the pin fails without this change.
joshwapohlmann pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 16, 2026
The guard that keeps a proxy call out of a tag-edited frame dereferenced every
sibling part, while the rest of the loop treats non-object parts defensively. A
malformed upstream chunk carrying a null part alongside tag-edited text therefore
threw mid-stream and took the turn with it.

Found in review of ranxianglei#867; the pin fails without this change.
@joshwapohlmann

Copy link
Copy Markdown
Contributor Author

Thanks for the path-by-path trace — that is the level the two fixes needed, since each is a guard that only shows its value in one chunk order.

Finding 1's residual, agreed. In a round with no real calls, a chunk [uneditedText, proxyCall] now loses its preamble instead of leaking the call. I would take that trade for the reason you give and one more: the round is superseded when the proxy call executes, so the host discards the preamble either way — what it does not discard is a call it then executes twice, which is what the old [text, call] order sent. Consistently losing the preamble is the better half of an asymmetric choice.

Nit 1 is fixed, not just noted. You are right that a null sibling part would have thrown: the filter dereferenced every part while the rest of the loop guards non-object parts at :435 and :439. It is now (p) => !p || typeof p !== "object" || p.functionCall === undefined, matching the defensive style around it, and there is a pin for it — a chunk of [tag-edited text, null] must still deliver its prose. The reproduction is on the record in my run: with the guard stashed the new test fails and the adapter suite is 11/12; with the guard, 12/12 and typecheck clean. Branch head is 1f4785c, current with master, one commit ahead of what you reviewed.

Nit 2, agreed as pre-existing. A model-emulated render tag riding along in a chunk that also carries a real call still reaches the client through settle's verbatim replay. That replay exists to protect the call's own integrity (#463), so scrubbing it is not a local change, and the shape needs model misbehavior to trigger. The plugin pipe closes the case where the whole turn is an echo — swallow what is unparseable, and re-ask for a turn whose only visible output was the tag. A tag inside a call-bearing chunk is the remaining corner, and I would rather leave it visible than risk the call.

On the dependency and your environment. Your reproduction is the expected failure: the pinned 0.0.75 kernel has no ABSORB_TOOL_GOOGLE, which is exactly what the bump after the release fixes. The ENOSPC that stopped the local build is worth fixing on your side before the bump lands, since the gate you committed to — full suite plus typecheck and build on the bumped head — needs that disk. Nothing to do about it from here.

Standing by as agreed: when #318 merges I will ping you for the deps: bump acp-kernel commit, and I will not touch the branch in between apart from keeping it current with master.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Verified 1f4785c, and the runtime gate is now done — the sandbox disk recovered, so I built acp-kernel directly from #318 head (006f91a) and swapped it into place locally; everything below runs against the post-bump dependency rather than the pinned one.

  • Nit-1 diff reviewed: the filter is exactly the suggested guard, and the pin test ([tag-edited text, null] still delivers its prose) reproduces the pre-fix throw as documented. Good.
  • Google adapter suite: 12/12 — matches your numbers.
  • Typecheck: clean. Build: success.
  • Full suite: 1686 tests, 1679 pass, 5 fail, 2 skip (the skips are the #679 real win32 tests, platform-gated on Linux).

The 5 failures are not regressions from this PR — the identical set reproduces on master (d650db7) against the same kernel build. Breakdown:

  • Four are acp_cache drift. The tool-list assertions (tests/codex-official.test.ts, tests/launcher-plugin-mode.test.ts ×2, tests/plugin-protocol.test.ts) expect lists without acp_cache; the newer kernel injects it. acp_cache landed in kernel commit 0bf90ba ("cache reconciliation core") and already shipped in v0.0.75, so every post-[Bug] Sessions with lastCompaction=0 fail to resume (empty response, exit 0) once totalPruneTokens grows #318 release carries it. Consequence for our sequence: the bump commit must update those four expectations alongside the pin + lockfile or CI goes red at bump time. I'll fold that reconciliation into the bump commit so it lands green in one push.
  • One is pre-existing and kernel-independent: #857 D: upward self-heal trusts usage baselines only (tests/issue857-image-baseline.test.ts:276 — confirmedOf returns undefined instead of {deepseek-flash: 140000}). It fails identically on master and on this branch, on both pinned 0.0.74 and the [Bug] Sessions with lastCompaction=0 fail to resume (empty response, exit 0) once totalPruneTokens grows #318 build, deterministically across repeated runs and with proxy env cleared. Not caused by this PR and not unblocked by the bump; I found no existing issue tracking it. Report-only per convention — whether it earns its own thread is your call.

One small correction to your comment: the branch pins 0.0.74 (devDependencies), not 0.0.75 — 0.0.75 is simply npm's latest. Neither carries the codec, so your conclusion stands unchanged.

Sequence confirmed: ping me when #318 merges. I'll push deps: bump acp-kernel = exact pin + lockfile refresh + the four acp_cache expectation updates, then re-run the full gate (suite + typecheck + build) on the bumped head before calling it mergeable.

修了什么问题/为什么/能否合并:nit 修复与所建议的守卫逐字一致且带回归钉;已用 #318 头构建的内核完成运行时验证(google 套件 12/12、typecheck/build 干净、全量 1686 仅 5 失败且全部在 master 同样复现——4 个为内核 acp_cache 版本漂移、1 个为 master 既有问题),本 PR 零回归;待 #318 人合→CI 发布后由我推 bump 提交(pin+lockfile+4 处期望更新)并复跑全绿即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner

ci失败 ework看下

@ranxianglei

Copy link
Copy Markdown
Owner

继续

@ranxianglei

Copy link
Copy Markdown
Owner

ci
失败 ework看看 另外这个有issue吗 没有帮忙补充一个

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

CI is fixed — all 8 checks green on the new head 7a780ad. Ready for you to merge.

Root cause of the red CI. The branch pinned acp-kernel@0.0.74, which predates the Gemini wire codec, so typecheck failed with ~22 missing-export errors (GoogleRequestBody, coreToGoogle, googleToCore, ABSORB_TOOL_GOOGLE, …) and every test/build job went down with them. The blocker upstream — kernel PR #318 — has since been merged and released (acp-kernel v0.0.76/v0.0.77 carry the codec), and master already picked up the pin via PR #915 (cdda2ea fix: bump acp-kernel 0.0.74 -> 0.0.77).

What I did. Merged current master (adf6d88) into joshwapohlmann:2026-09-16_866-google-wire → head 7a780ad, pushed to the PR fork. No conflicts. This brings the 0.0.77 pin plus #915's acp_cache expectation reconciliation, which also clears the four drift failures flagged in the earlier review round.

Verification.

  • Local on the merged tree: npm ci clean, npx tsc --noEmit -p tsconfig.build.json clean, full suite npm test = 1763 tests, 1761 pass, 0 fail, 2 skip (platform-gated win32). The previously noted pre-existing #857 image-baseline failure does not reproduce on the merged head.
  • CI on 7a780ad: build-artifact ✅, version-guard ✅, real-codex fake-upstream ✅, codex CODEX_HOME ✅, test ubuntu-latest 22/24 ✅, test windows-latest 22/24 ✅ — 8/8 success.

On the issue question. Yes, one exists: #866 "Google Gemini is untracked: no conversation id, so nothing is folded or compressed" is exactly this work's issue (the PR title references it). It is already closed, so there is nothing to file — no new issue created.

Remaining step is yours: the merge.


中文摘要:CI 红是因为分支钉在 acp-kernel 0.0.74(缺 Gemini codec,约 22 个导出缺失导致 typecheck/测试全挂);内核 #318 已合并并发布 0.0.77、master 已通过 #915 完成版本升级,我已把最新 master 合入 PR 分支并推送(新头 7a780ad),本地全量 1763 测试 0 失败、CI 8/8 全绿,可以合并;issue 方面已有 #866(本 PR 对应的 issue,已关闭),无需补开新 issue。

@ranxianglei

Copy link
Copy Markdown
Owner

PR 队列扫描更新(issue #1020):

判定:仍有价值,待作者更新。 master 至今没有 Gemini/Google wire 支持(无 generativelanguage 协议路径),本 PR 是该方向唯一实现;对应 issue #866 / #318。

现状:

  • CI 8 项全绿(2026-09-17 head 7a780ad)✅
  • 分支落后 master 且冲突(dirty)❌

fork 分支我无法代推。请 rebase 到最新 master(git fetch origin && git rebase origin/master)解决冲突后我们继续评审。如果短期内没有时间维护,也可以说明,由维护者 repo 侧接手重做。

@ranxianglei
ranxianglei force-pushed the 2026-09-16_866-google-wire branch from 7a780ad to c08d99a Compare September 19, 2026 17:26
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 19, 2026
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 19, 2026
A chunk carrying a text part AND a functionCall part is replayed whole at
settle, so its text must not also go out immediately. The guard counted only the
calls seen before the text part in iteration order, so `[text, functionCall]` —
the shape the reviewer named — forwarded the chunk as the text frame and the
settle replay forwarded it again, handing the client the text and the call
twice. The reasoning branch had no guard at all and had the same problem.

The edited copy the tag filter leaves behind had a second hole: it kept every
sibling part, so a PROXY call (which settle drops, because the proxy executes it
itself) rode out to the client inside the filtered text frame. The copy now
drops sibling call parts; settle replays them when they are real.

Covered by two tests: a split chunk delivering its text and its call once each,
and an edited frame carrying no proxy call. Reported by the maintainer's review
agent on PR ranxianglei#867 (finding 1); the cosmetic role on finish-only clones is fixed
in the same pass.
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 19, 2026
The guard that keeps a proxy call out of a tag-edited frame dereferenced every
sibling part, while the rest of the loop treats non-object parts defensively. A
malformed upstream chunk carrying a null part alongside tag-edited text therefore
threw mid-stream and took the turn with it.

Found in review of ranxianglei#867; the pin fails without this change.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

Rebase 完成,冲突已全部解决,CI 8/8 全绿。分支我已直接推到 fork(凭据可推),作者无需再操作。

新状态

  • head: c08d99a(force-push 到 joshwapohlmann:2026-09-16_866-google-wire,旧 head 7a780ad)
  • base: master 5f7c00b(rebase 后线性 8 个 commit,无 merge commit)
  • acp-kernel: 继承 master 的 0.0.79 pin(PR 本身不动 package.json)
  • CI: 8/8 success(build-artifact、version-guard、ubuntu/windows × node22/24 test matrix、real codex vs deterministic fake upstream、codex CODEX_HOME overlay)
  • 本地验证: tsc --noEmit ✅ · 全量测试 2006 tests / 2004 pass / 0 fail / 2 skip(win32-gated)· tsup build ✅

冲突解决(union of intent,共 5 文件 11 处)

  1. src/server.ts(7 hunks)
  2. src/compress-tool.ts(feat: 支持 search_context 显式查询历史 session #841 对齐,请 reviewer 确认):master feat: 支持 search_context 显式查询历史 session #841 要求所有 wire-mode 注入点用 BILI_ 数组(search_context 带 conversation_id)。PR 原代码在 google 注入点用的是裸 ACP_TOOLS_GOOGLE,我补了 BILI_SEARCH_CONTEXT_TOOL_GOOGLE + BILI_ACP_TOOLS_GOOGLE(google 形状用 parameters 而非 input_schema),injectGoogleTool 改走 BILI_ 版本——这样 Gemini 客户端也能跨会话 search_context,与其他三条 wire 行为一致。若你倾向保持 PR 原样(裸数组),告诉我即可回退这一处。
  3. src/plugin.ts(2 hunks):google syntheticTail 适配 master 新的 PipeStream 签名(finishReason/modelVersion 透传逻辑不变);stream dispatch 加 processGoogle 分支,同时保留 master 的 turnTerminal/empty-turn retry 门(对 google 恒为 undefined → 不生效,与 PR 行为一致)。
  4. src/loop/core.ts:proxyResults 保留 PR 的 signature 携带,外层套 master [Question] zocde调用子智能体的时候,子智能体有概率出错,导致子智能体停止工作 #862 的 visibilityMarkers !== false 门。
  5. src/fake-completion.ts + src/wire-body.ts:master 把 per-wire hint 注入重构成了 appendTrailingUserText(protocol, body, text) helper,我把 PR 的 google 分支移植进该 helper(contents 末尾 user parts 追加 / 新建 user part)。
  6. src/server/side-request.ts:guard 内输出预算读取取 PR 的 readOutputBudget(支持嵌套 generationConfig.maxOutputTokens)+ master 的 headroomCap 第三参。

备注

中文一句话:PR #867 已 rebase 到最新 master(5f7c00b),解决了 11 处冲突——核心是丢弃已被 #987 删除的窗口自学习代码、按 #841 给 google wire 补齐 BILI_ 工具数组、保留 PR 全部 Gemini 功能分支;本地 typecheck/2006 测试/build 全过,CI 8/8 绿,可以进入评审。

🤖 ework agent

@ranxianglei

Copy link
Copy Markdown
Owner

解决冲突

@ranxianglei
ranxianglei force-pushed the 2026-09-16_866-google-wire branch from c08d99a to 81bd145 Compare September 20, 2026 01:58
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 20, 2026
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 20, 2026
A chunk carrying a text part AND a functionCall part is replayed whole at
settle, so its text must not also go out immediately. The guard counted only the
calls seen before the text part in iteration order, so `[text, functionCall]` —
the shape the reviewer named — forwarded the chunk as the text frame and the
settle replay forwarded it again, handing the client the text and the call
twice. The reasoning branch had no guard at all and had the same problem.

The edited copy the tag filter leaves behind had a second hole: it kept every
sibling part, so a PROXY call (which settle drops, because the proxy executes it
itself) rode out to the client inside the filtered text frame. The copy now
drops sibling call parts; settle replays them when they are real.

Covered by two tests: a split chunk delivering its text and its call once each,
and an edited frame carrying no proxy call. Reported by the maintainer's review
agent on PR ranxianglei#867 (finding 1); the cosmetic role on finish-only clones is fixed
in the same pass.
ranxianglei pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 20, 2026
The guard that keeps a proxy call out of a tag-edited frame dereferenced every
sibling part, while the rest of the loop treats non-object parts defensively. A
malformed upstream chunk carrying a null part alongside tag-edited text therefore
threw mid-stream and took the turn with it.

Found in review of ranxianglei#867; the pin fails without this change.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

已 rebase 到最新 master(1a3b1ec,比上次基线 5f7c00b 又新了 28 个 commit)并强推,新 head:81bd145。冲突只有 5 处 hunk + 1 个文件删除,全部按"保留 master 行为、嫁接 google 分支"的合并意图处理:

1. src/strip-images.ts(modify/delete) — master 侧 #617 已把 stripHistoricalImages 整体搬进内核(acp-kernel/wire,kernel #215/#331),本地文件删除。查证过:acp-kernel@0.0.79 的内核实现已包含 google 分支(inlineData/fileData part 判定 + 裸 {text} 占位),且内核测试 tests/wire-google.test.ts:368("strip-images: google drops inlineData/fileData from old contents only")覆盖它——本 PR 原来加在本地文件的 google 剥离逻辑与内核逐行等价,接受删除、不重复实现。

2. src/server.ts import 区 — 取并集:保留 master 新增(#811 自动重启的 detectStaleInstall、PACKAGE_NAME/VERSION、totalInFlight、#888 的 withSummaryBudgetNote)+ 本 PR 的 acp-kernel/wire google codec 块 + ABSORB_TOOL_GOOGLE/BILI_ACP_TOOLS_GOOGLE。

3. preflightCompressIfNeeded 调用点 — 传 requestModel(= body.model ?? googleModel,覆盖路径里的 Gemini 模型名)+ master 新增的 resolvedNativeWindow/windowShrinkReason 两个参数(#987/#737)。

4. compress-loop 段 — 保留 master 的 withSummaryBudgetNote(#888)和 #970 的 async refreshFolded(带 withSessionLock);给 pickAdapter 补上本 PR 的 prepared.google 第 8 参。

5. JSON rewrite 分发 — 新增 google 分支,并按 #970 的模式用 await withSessionLock(...) 包裹(与同段 openai/responses 一致)。

6. src/loop/core.ts — proxy tool 执行保留 master 的 await withSessionLock(#970)+ 本 PR 的 signature 透传。

验证(rebase 后本地全量):

  • tsc --noEmit ✅
  • 全量测试 2025 tests / 2023 pass / 0 fail / 2 skip(skip 均为 win32-gated)✅
  • npm run build ✅
  • GitHub CI:8/8 success(build-artifact、version-guard、ubuntu/windows × node22/24、real codex fake upstream、codex CODEX_HOME overlay)✅

分支现在与 master 零落后、零冲突,可以继续评审。


一句话中文摘要:把 Google/Gemini wire 分支 rebase 到最新 master(含 #617 图像剥离下沉内核、#970 会话锁收窄、#987 window clamp、#888 预算提示等 28 个新 commit),解决了 5 处冲突+1 处文件删除(google 剥离逻辑确认已由内核 acp-kernel@0.0.79 等价承载,直接接受删除),本地 typecheck/2025 项测试/build 全绿、CI 8/8 通过,可以进入评审。

@ranxianglei

Copy link
Copy Markdown
Owner

又冲突了

Joshwa Pohlmann added 2 commits September 20, 2026 12:22
A Gemini request already reached the proxy (the MITM domain is configured) but
was forwarded verbatim: /v1beta/models/<model>:streamGenerateContent matched no
protocol, so the turn never entered a session and had no identity, folding,
compression or usage accounting.

- detection: googlePathKind()/googleModelFromPath() take the model from the URL
  PATH (Gemini carries it there, not in the body), protocol "google" is in
  KNOWN_PROTOCOLS with /bili/google/... as an explicit upstream prefix, and
  :countTokens is routed to a prune-only path.
- identity: conversationSignalGoogle, plus prefix affinity over `contents` for
  the anonymous case — omp stamps no session header for its google provider.
- prepare: prepareGoogle writes the system into body.systemInstruction (a Gemini
  request carries no system message in contents), injects the Gemini-shaped ACP
  tool declarations from the kernel, and merges a nudge into a trailing user
  content because the wire rejects non-alternating roles and a request must not
  end on the model side. Replayed reasoning keeps its thoughtSignature: unlike
  the other wires, a reasoning part is never dropped here, because Gemini 3
  validates the signature on every replayed part.
- loop: createGoogleAdapter + rewriteGoogleJsonResponse carry the compress loop
  on the streaming and non-streaming paths, including the tool-call signature
  and the googleThoughtSignature sidecar on replayed reasoning.
- aux pipes: preflight summary requests, in-band error frames, fake-completion
  detection, image-token counting, historical-image stripping and the plugin
  passthrough (usage sniffed from usageMetadata, finishReason-aware terminal
  handling, per-part tag-echo stripping with the same drop-and-warn behaviour
  the other two wires have).
- usage: promptTokenCount/cachedContentTokenCount in, candidatesTokenCount +
  thoughtsTokenCount out; gemini-3 added to the context-limit table.

Covered by tests/e2e-google-compress.test.ts (session-identified growth that
compresses and stays bounded, an anonymous omp-shaped client that attaches by
prefix affinity, plugin mode keeping the agent's own call) and
tests/adapter-google.test.ts.
…rdless

A preflight on the google wire could never compress. summarizeRange opens the
summary call with stream=false (the ranxianglei#626 default for upstreams that answer a
stream call with JSON), but a Gemini summary goes to the client's own URL —
`…:streamGenerateContent?alt=sse` — which answers SSE whatever the request
asks for: the Gemini payload has no `stream` field to turn it off. requestSummary
parsed SSE only when that flag was set, so the text of every summary reply was
discarded, the reply read as empty, and the whole per-request call budget (8)
was spent before the turn fail-fasted with "context ~N tokens exceeds the model
window … preflight compression could not bring it under: the preflight
summarization budget (8 calls per protection regime) was exhausted … the upstream
stream carried 41 SSE event(s) but no summary text" (ranxianglei#829).

The body's shape now decides how a summary reply is read, covering both
directions of the mismatch: a stream:true call answered with plain JSON (the
ranxianglei#626 case, unchanged) and a non-stream call answered with SSE (this one).

The Gemini extraction contract had no test coverage at all, which is what let
this reach a live session: tests/preflight-sse-extraction.test.ts gains the four
google cases (thinking parts skipped, lenient framing, mid-frame truncation,
in-stream error), and tests/preflight-google-summary.test.ts drives a real
over-window Gemini turn through the proxy against a mock upstream that answers
the way Google does — SSE for a streaming client, JSON for a `:generateContent`
client — asserting the summary replaces a raw range in the forwarded payload.
Joshwa Pohlmann and others added 7 commits September 20, 2026 12:22
…he call

A proxy-owned tool call (compress, decompress, search_context, acp_status, absorb)
is answered by the proxy itself: it synthesizes the tool-call core AND the
tool-result core, then re-requests so the model sees the outcome. The call core
carried `toolName`, the result core only `toolCallId` — harmless on OpenAI and
Anthropic, which pair a result by id, but Gemini answers a call by NAME:
the wire emitted `functionResponse{name:"unknown"}` for a `functionCall` named
`compress`, and the API rejects the whole request with a generic
`400 INVALID_ARGUMENT` ("Request contains an invalid argument."), so an
anonymous Gemini session died on the first proxy tool call it made (ranxianglei#829).

The mock upstream in tests/e2e-google-compress.test.ts is now the strict one the
real API is: it validates every rebuilt request against Gemini's contract
(alternating roles, non-empty parts, exactly one payload per part, signatures
only on model parts, functionCall named, functionResponse named AND answering a
call seen earlier, response an object) and fails the test on any violation.
Reverting this one line reproduces nine `functionResponse name=unknown answers
no call (calls seen: compress)` violations; with it the suite is clean.
A chunk carrying a text part AND a functionCall part is replayed whole at
settle, so its text must not also go out immediately. The guard counted only the
calls seen before the text part in iteration order, so `[text, functionCall]` —
the shape the reviewer named — forwarded the chunk as the text frame and the
settle replay forwarded it again, handing the client the text and the call
twice. The reasoning branch had no guard at all and had the same problem.

The edited copy the tag filter leaves behind had a second hole: it kept every
sibling part, so a PROXY call (which settle drops, because the proxy executes it
itself) rode out to the client inside the filtered text frame. The copy now
drops sibling call parts; settle replays them when they are real.

Covered by two tests: a split chunk delivering its text and its call once each,
and an edited frame carrying no proxy call. Reported by the maintainer's review
agent on PR ranxianglei#867 (finding 1); the cosmetic role on finish-only clones is fixed
in the same pass.
The guard that keeps a proxy call out of a tag-edited frame dereferenced every
sibling part, while the rest of the loop treats non-object parts defensively. A
malformed upstream chunk carrying a null part alongside tag-edited text therefore
threw mid-stream and took the turn with it.

Found in review of ranxianglei#867; the pin fails without this change.
The BILI_SEARCH_CONTEXT_TOOL_GOOGLE / BILI_ACP_TOOLS_GOOGLE wrappers added
during the master rebase reference SEARCH_CONTEXT_TOOL_GOOGLE and
ACP_TOOLS_GOOGLE in local scope; they were only re-exported, not imported.
Master gained two injection-point features after this branch diverged:
ranxianglei#750 (acp_rule persistent reminders) and ranxianglei#816 (per-wire tool prompt
overrides). The anthropic/openai/responses wires both honor them at
their injection points; the Google wire silently dropped both.

- synthesize RULE_TOOL_GOOGLE in compress-tool.ts (the kernel ships
  RULE_TOOL_NAME only, same situation as the other three shapes) and
  inject it when rules are enabled, mirroring the other three sites;
- pass surface?.toolPrompts into injectGoogleTool so ranxianglei#816 overrides
  apply on the fourth wire as well.
@ranxianglei
ranxianglei force-pushed the 2026-09-16_866-google-wire branch from 81bd145 to 102ba3a Compare September 20, 2026 04:30
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

已 rebase 并推送,冲突已解决,CI 全绿。新 head:102ba3a(fork 分支 2026-09-16_866-google-wire,force-update 81bd145 → 102ba3a)。

冲突(仅 1 处)

master 前进到 2b027f1(新增 #750 acp_rule、#816 per-wire tool prompts 等),唯一冲突在 src/server.ts 顶部 import 块:master #750 给 compress-tool import 加了 RULE_TOOL/RULE_TOOL_OPENAI/RULE_TOOL_RESPONSES,本 PR 加了 Google codec 常量与 ABSORB_TOOL_GOOGLE/BILI_ACP_TOOLS_GOOGLE。按 union-of-intent 解决:两边都保留。其余 7 个 commit 全部干净落上。

合并后发现的两个适配缺口(已修,commit 102ba3a)

master 的两个注入点特性在本分支分叉后才加入,其他三条 wire 的注入点都遵守,唯独 Google wire 静默缺失:

  1. feat(rules): acp_rule persistent reminders (compress.rules) #750 acp_rule:prepareGoogle 只注入 absorb,不注入 rule 工具;且 kernel 只提供 RULE_TOOL_NAME(三条 wire 的工具对象都在 compress-tool.ts 本地合成)。已补 RULE_TOOL_GOOGLE(google 扁平 shape:{name, description, parameters}),并在 rulesActive 时随 absorb 一起注入——与 anthropic/openai/responses 三处完全对齐。
  2. compactEntry summary clamp (slice(0,199)+'…') can strand a lone high surrogate → same deterministic UTF-8 gateway 500 family #816 per-wire tool prompts:google 调用点没传 surface?.toolPrompts,导致 prompt override 在第四条 wire 上不生效。已补传,并把 injectGoogleTool 的第二参从单个工具改为数组(与其他三处签名一致)。

这两处是机械性的第四 wire 补全,若 reviewer 认为应拆出单独 PR 请说一声。

验证

  • 本地:tsc --noEmit ✅;全量 2042 tests / 2040 pass / 0 fail / 2 skip(win32-gated);npm run build ✅
  • GitHub CI @ 102ba3a:8/8 success(build-artifact、version-guard、ubuntu/windows × node22/24、real codex fake upstream、codex CODEX_HOME overlay)

中文一句话:把分支 rebase 到最新 master(唯一 import 冲突按两边保留解决),补齐了合并后 Google wire 缺失的 acp_rule 注入和 per-wire prompt 覆盖两处适配,本地与 CI 全绿,可以进入评审/合并。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants