Repository navigation
feat(proxy): serve the Google/Gemini wire (#866) - #867
Conversation
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.
|
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 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. Finding 3 — your CI analysis, accepted, and my earlier statement was incomplete. You are right that The sequence is agreed: #318 merges (human-only) → CI publishes → the Commit: |
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 Finding 1 — verified.
One residual consequence worth knowing (non-blocking): in a round with no real calls, a chunk Finding 2 — verified. Finding 3 — dependency state independently re-verified. Environment failure (for ops): while building acp-kernel from #318 head ( Two minor nits (non-blocking, report-only):
Sequence: standing by for your ping when #318 merges. I'll then push 修了什么问题/为什么/能否合并:已在代码层面逐路径核对 split-chunk 双发修复(text/reasoning 两分支统一改为整块 |
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 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.
|
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 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 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 Standing by as agreed: when #318 merges I will ping you for the |
Verified
The 5 failures are not regressions from this PR — the identical set reproduces on master (
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 修了什么问题/为什么/能否合并:nit 修复与所建议的守卫逐字一致且带回归钉;已用 #318 头构建的内核完成运行时验证(google 套件 12/12、typecheck/build 干净、全量 1686 仅 5 失败且全部在 master 同样复现——4 个为内核 |
|
ci失败 ework看下 |
|
继续 |
|
ci |
CI is fixed — all 8 checks green on the new head Root cause of the red CI. The branch pinned What I did. Merged current master ( Verification.
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。 |
|
PR 队列扫描更新(issue #1020): 判定:仍有价值,待作者更新。 master 至今没有 Gemini/Google wire 支持(无 现状:
fork 分支我无法代推。请 rebase 到最新 master( |
7a780ad to
c08d99a
Compare
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.
Rebase 完成,冲突已全部解决,CI 8/8 全绿。分支我已直接推到 fork(凭据可推),作者无需再操作。 新状态
冲突解决(union of intent,共 5 文件 11 处)
备注
中文一句话:PR #867 已 rebase 到最新 master( 🤖 ework agent |
|
解决冲突 |
c08d99a to
81bd145
Compare
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.
已 rebase 到最新 master( 1. 2. 3. 4. compress-loop 段 — 保留 master 的 5. JSON rewrite 分发 — 新增 google 分支,并按 #970 的模式用 6. 验证(rebase 后本地全量):
分支现在与 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 通过,可以进入评审。 |
|
又冲突了 |
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.
…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.
81bd145 to
102ba3a
Compare
已 rebase 并推送,冲突已解决,CI 全绿。新 head: 冲突(仅 1 处)master 前进到 合并后发现的两个适配缺口(已修,commit
|
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)andgoogleModelFromPath(urlPath)classify:generateContent,:streamGenerateContentand:countTokensand resolve the model,WireProtocolgainsgoogle, andrequestModelprefers the body's model with the path as the fallback. Anonymous Gemini traffic is linked into a session by prefix affinity overcontents(conversationSignalGoogle), since the wire carries no client session header.Folding:
prepareGoogle/prepareGoogleCountTokensrun the kernel path withstrategyand tool injection (injectGoogleTool),appendGoogleNudgefolds a nudge into a trailing non-model content rather than adding one,pickAdaptergained agoogleentry, andsrc/loop/adapter-google.tsplussrc/stream-google.ts(rewriteGoogleJsonResponse) serve the loop and non-streaming paths.googleThoughtSignatureis written back on replayed reasoning and carried throughToolCallEmit.Accounting:
usageTotals/promptInputTotal/usageOutputTotalmap input =promptTokenCountminuscachedContentTokenCountand output =candidatesTokenCountplusthoughtsTokenCount; the window table gained the Gemini 3 line;outputBudgetFieldgainedgenerationConfig.maxOutputTokenswith 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
: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, thenpreflight fail-fast 502and a 300s dead-end cooldown. The body's shape now decides the parser (SSE_DATA_LINE_REplus a JSON-vs-SSE branch).functionResponse{name:"unknown"}answeringfunctionCall{name:"compress"}and Gemini rejected the whole request with400 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/completionswould be a much smaller change, but the translation dropsthoughtSignature, 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, everyfunctionResponsenamed 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 intests/preflight-sse-extraction.test.ts.Verification
npx tsc --noEmit -p tsconfig.build.jsonclean; proxy suite 1669 tests with 1661 passing, the only failures being the six environmental ones that upstream also shows (tests/launcher.test.tsspawning real launchers, and twotests/plugin-agent.test.tscases about a proxy being absent). Live againstgemini-3.8-flash: a proxy-ownedcompresscall 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 carriessystemInstructionwith valid alternatingcontents, and the response returns HTTP 200 with noINVALID_ARGUMENT.Dependency
Requires the kernel codec:
acp-kernelPR #318 (issues #317). Theacp-kerneldevDependency is pinned at0.0.74, sonpm ci && npm run buildhere 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.