Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/loop/adapter-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,16 @@ export function createGoogleAdapter(
});
continue;
}
const clean = tagFilter.push(part.text);
// A signature may belong to ordinary text, without thought:true.
// Preserve its bytes and part boundary on both replay paths.
const signed = typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0;
const clean = signed ? part.text : tagFilter.push(part.text);
if (clean !== part.text) {
editedParts ??= [...parts];
editedParts[i] = { ...part, text: clean };
}
if (clean.length === 0) continue;
partEvents.push({ kind: "text", delta: clean, rawHandled: true });
partEvents.push({ kind: "text", delta: clean, rawHandled: true, ...(signed ? { rawGooglePart: part } : {}) });
}
// Decide wire output only after every prose sibling has been
// cleaned. Each complete frame belongs to one event, regardless
Expand Down
14 changes: 9 additions & 5 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { maybeJudgeContinuity } from "../invariant-judge.js";
import type { SeparatorSlot } from "../server/responses-ordering.js";
import { countImagesInParsedBody, countImagesInRawBody } from "../image-tokens.js";
import { diagnoseSuccessWithoutUsage, lastCompressSuffix, releaseNudgeHold, withSessionLock, type Session } from "../session.js";
import type { BiliMessage } from "acp-kernel/wire";
import type { BiliMessage, GooglePart } from "acp-kernel/wire";
import { coreToAnthropic } from "acp-kernel/wire";
import {
parseCompressInput,
Expand Down Expand Up @@ -322,7 +322,7 @@ export interface RequestOptions {
// arrive instead of being re-laid out as [reasoning…, text, tool…].
type RoundPart =
| { kind: "reasoning"; text: string; signature: string }
| { kind: "text"; text: string }
| { kind: "text"; text: string; rawGooglePart?: GooglePart }
| { kind: "redacted"; data: string }
// #3158: a Gemini part whose only payload is a thoughtSignature (no text,
// no functionCall). Recorded in stream order so a proxy-tool rebuild puts
Expand All @@ -333,7 +333,7 @@ export type ParsedStreamEvent =
// rawHandled: the adapter carries this delta in another event's complete
// frame (or buffers that frame until tool settlement). Keep accounting it,
// but do not synthesize a second client frame in continuation rounds.
| { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean }
| { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean; rawGooglePart?: GooglePart }
// round2-hold: withheld marks a reasoning PAYLOAD the adapter kept off the
// client wire (bili-owned round ≥ 2 on a signing upstream — see
// ParseStreamOpts.upstreamSignsThinking). It still feeds the loop's
Expand Down Expand Up @@ -963,8 +963,8 @@ export async function* runCompressLoop(
if (ev.kind === "text") {
assistantText += ev.delta;
let tp = roundParts[roundParts.length - 1];
if (!tp || tp.kind !== "text") {
tp = { kind: "text", text: "" };
if (!tp || tp.kind !== "text" || tp.rawGooglePart !== undefined || ev.rawGooglePart !== undefined) {
tp = { kind: "text", text: "", ...(ev.rawGooglePart ? { rawGooglePart: ev.rawGooglePart } : {}) };
roundParts.push(tp);
}
tp.text += ev.delta;
Expand Down Expand Up @@ -1531,6 +1531,10 @@ export async function* runCompressLoop(
role: "assistant",
contentType: "text",
text: part.text,
...(part.rawGooglePart ? {
googleThoughtSignature: part.rawGooglePart.thoughtSignature,
rawGoogleParts: [part.rawGooglePart],
} : {}),
});
} else if (part.kind === "standalone_signature") {
// #3158: put the standalone thoughtSignature back at its
Expand Down
19 changes: 12 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3737,10 +3737,11 @@ export async function pipePluginChatWithStrip(
// field, so an interleaved thought/text pair in one frame never
// shares held-back state.
const field = p["thought"] === true ? "thinking" : "text";
const signed = typeof p["thoughtSignature"] === "string" && p["thoughtSignature"].length > 0;
// Whole-field tool-call emission on an absorb-instructed
// request (m00885) — see processOpenai. Degenerate-close
// residue (#2190) rides the same routing.
if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) {
if (signed || (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending())) {
if (raw.length > 0) {
keptText = true;
appendReply(field, index, raw);
Expand Down Expand Up @@ -5178,12 +5179,16 @@ function stripGoogleChunk<T>(obj: T, drop: boolean = false, requestText?: string
...cand,
content: {
...cont,
parts: (cont["parts"] as unknown[]).map((p) =>
// #1960/KDD#10: thought parts carry a thoughtSignature — leave them byte-for-byte.
p && typeof p === "object" && (p as Record<string, unknown>)["thought"] !== true && typeof (p as Record<string, unknown>)["text"] === "string"
? { ...(p as Record<string, unknown>), text: stripAcpTags((p as Record<string, unknown>)["text"] as string, drop, requestText) }
: p,
),
parts: (cont["parts"] as unknown[]).map((p) => {
if (!p || typeof p !== "object") return p;
const part = p as Record<string, unknown>;
// #1960/KDD#10: ordinary text can carry a signature too.
return part["thought"] !== true
&& !(typeof part["thoughtSignature"] === "string" && part["thoughtSignature"].length > 0)
&& typeof part["text"] === "string"
? { ...part, text: stripAcpTags(part["text"], drop, requestText) }
: p;
}),
},
};
});
Expand Down
14 changes: 9 additions & 5 deletions src/stream-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json
const containsEcho = (text: string): boolean => containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text) || (absorbArmed && containsToolCallEmissionText(text));
// Capture request evidence before a yielded proxy tool can change the context.
const requestText = absorbArmed && parts.some(part => part && typeof part === "object"
&& (part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text))
&& (part as { thought?: boolean }).thought !== true
&& !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0)
&& typeof part.text === "string" && containsEcho(part.text))
? JSON.stringify(ctx.messages) : undefined;
for (const part of parts) {
if (!part || typeof part !== "object") continue;
Expand All @@ -46,10 +48,12 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json
continue;
}
if (fc) sawReal = true;
// #1960/KDD#10: a thought part carries a thoughtSignature the client
// echoes back verbatim — rewriting its text desyncs the signature and
// bricks replay. Strip prose only from non-thought parts.
if ((part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text)) {
// #1960/KDD#10: thought parts and signed ordinary text are replayed
// verbatim. Rewriting their text desyncs the signed payload; strip
// prose only from unsigned, non-thought parts.
if ((part as { thought?: boolean }).thought !== true
&& !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0)
&& typeof part.text === "string" && containsEcho(part.text)) {
ctx.log(`[tag-echo] stripped: non-stream google output ACP echo (render tags/markers/internal artifacts): ${part.text.slice(0, 120).replace(/\n/g, " ")}`);
part.text = stripAcpTags(part.text, absorbArmed, requestText);
}
Expand Down
47 changes: 42 additions & 5 deletions tests/google-complete-frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ async function close(server: http.Server): Promise<void> {
await new Promise<void>(resolve => server.close(() => resolve()));
}

async function throughProxy(rounds: Array<Array<Record<string, unknown>>>): Promise<{ calls: Array<Record<string, unknown>>; wire: string }> {
async function throughProxy(rounds: Array<Array<Record<string, unknown>>>, options: { plugin?: boolean; stream?: boolean } = {}): Promise<{ calls: Array<Record<string, unknown>>; wire: string }> {
const calls: Array<Record<string, unknown>> = [];
const upstream = http.createServer(async (req, res) => {
const buffers: Buffer[] = [];
for await (const chunk of req) buffers.push(Buffer.from(chunk));
calls.push(JSON.parse(Buffer.concat(buffers).toString()) as Record<string, unknown>);
res.writeHead(200, { "content-type": "text/event-stream" });
res.end((rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")]).map(sse).join(""));
const reply = rounds[calls.length - 1] ?? [candidate([{ text: "Unexpected retry." }], "STOP")];
res.writeHead(200, { "content-type": options.stream === false ? "application/json" : "text/event-stream" });
res.end(options.stream === false ? JSON.stringify(reply[0]) : reply.map(sse).join(""));
});
upstream.listen(0, "127.0.0.1");
await once(upstream, "listening");
Expand All @@ -169,8 +170,9 @@ async function throughProxy(rounds: Array<Array<Record<string, unknown>>>): Prom
try {
const upstreamPort = (upstream.address() as import("node:net").AddressInfo).port;
const proxyPort = (proxy.address() as import("node:net").AddressInfo).port;
const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse`, {
method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}` },
const method = options.stream === false ? "generateContent" : "streamGenerateContent?alt=sse";
const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1beta/models/gemini-2.5-pro:${method}`, {
method: "POST", headers: { "content-type": "application/json", "x-acp-session": `complete-frame-${proxyPort}`, ...(options.plugin ? { "x-bili-plugin": "pi", "x-bili-plugin-conversation": `complete-frame-${proxyPort}` } : {}) },
body: JSON.stringify({ contents: [{ role: "user", parts: [{ text: "Report status then answer." }] }] }),
});
const wire = await response.text();
Expand Down Expand Up @@ -225,3 +227,38 @@ test("Google HTTP control: single-part replies remain single-part", async () =>
assert.equal(calls.length, 1);
assert.deepEqual(partsOf(framesOf(wire)), [{ text: "Control answer." }]);
});

for (const mode of ["sse", "array"] as const) {
test(`Google ${mode}: signed ordinary text bypasses prose filtering`, async () => {
const signed = { text: thought, thoughtSignature: signature };
const events = await collect([candidate([signed]), candidate([], "STOP")], mode);
assert.deepEqual(partsOf(framesOf(forwarded(events))), [signed]);
assert.deepEqual(events.filter(event => event.kind === "text").map(event => event.delta), [thought]);
});
}

for (const plugin of [false, true]) {
for (const stream of [false, true]) {
test(`Google HTTP ${plugin ? "plugin" : "proxy"} ${stream ? "SSE" : "JSON"}: signed text stays byte-exact`, async () => {
const signed = { text: thought, thoughtSignature: signature };
const { calls, wire } = await throughProxy([[candidate([signed], "STOP")]], { plugin, stream });
assert.equal(calls.length, 1);
assert.deepEqual(partsOf(stream ? framesOf(wire) : [JSON.parse(wire) as Record<string, unknown>]), [signed]);
});
}
}

test("Google HTTP proxy: signed text boundaries and signatures survive a tool continuation", async () => {
const signedA = { text: " Signed ordinary answer A. ", thoughtSignature: "c2lnLWE=" };
const signedB = { text: "Signed ordinary answer B.\n", thoughtSignature: "c2lnLWI=" };
const parts = [{ text: "Unsigned prefix." }, signedA, signedB, { text: "Unsigned suffix." }];
const { calls, wire } = await throughProxy([
[candidate(parts), candidate([{ functionCall: { id: "status", name: "acp_status", args: {} }, thoughtSignature: "c3RhdHVzLXNpZw==" }], "STOP")],
[candidate([{ text: "Final answer." }], "STOP")],
]);
assert.equal(calls.length, 2);
const visible = partsOf(framesOf(wire));
assert.deepEqual(visible.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]);
const replay = (calls[1]!.contents as Array<{ parts: Part[] }>).flatMap(content => content.parts);
assert.deepEqual(replay.filter(part => part.thoughtSignature === signedA.thoughtSignature || part.thoughtSignature === signedB.thoughtSignature), [signedA, signedB]);
});
Loading