Skip to content

Commit 567a6d4

Browse files
authored
Merge pull request #1508 from lidge-jun/agent/fix-1503-google-thought-visibility
fix(google): keep thought parts out of visible text
2 parents 4e0ffb2 + e0d2df3 commit 567a6d4

4 files changed

Lines changed: 166 additions & 5 deletions

File tree

‎src/adapters/google.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,24 @@ function artifactMarkdownUrl(filePath: string): string {
304304
return artifactHttpUrl(filePath).replace(/([()])/g, "\\$1");
305305
}
306306

307+
interface GoogleResponsePart {
308+
text?: string;
309+
thought?: boolean;
310+
functionCall?: { name: string; args: unknown };
311+
}
312+
313+
/**
314+
* Google marks model-internal reasoning as a normal text-bearing part plus `thought: true`.
315+
* Keep that provider visibility bit authoritative here so the streaming and buffered parsers
316+
* cannot accidentally expose the same hidden reasoning through different event types.
317+
*/
318+
function googlePartTextEvent(part: GoogleResponsePart): AdapterEvent | undefined {
319+
if (!part.text) return undefined;
320+
return part.thought === true
321+
? { type: "reasoning_raw_delta", text: part.text }
322+
: { type: "text_delta", text: part.text };
323+
}
324+
307325
export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapter {
308326
// Per-request closure: resolveAdapter builds a fresh adapter per request (server.ts), so buildRequest
309327
// can stash the CCA model/session for parseStream's reasoning-replay observation.
@@ -602,7 +620,7 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
602620
sawTerminalSignal = true;
603621
}
604622

605-
const parts = candidate.content?.parts as { text?: string; functionCall?: { name: string; args: unknown } }[] | undefined;
623+
const parts = candidate.content?.parts as GoogleResponsePart[] | undefined;
606624
// Record Gemini thought signatures for the next stateless tool-result turn. Vertex and
607625
// Antigravity use separate model namespaces so opaque provider state cannot cross routes.
608626
const replayModel = provider.googleMode === "cloud-code-assist" ? antigravityModel : vertexReplayModel;
@@ -613,9 +631,10 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
613631
}
614632
if (parts) {
615633
for (const part of parts) {
616-
if (part.text) {
634+
const textEvent = googlePartTextEvent(part);
635+
if (textEvent) {
617636
emittedContentEvent = true;
618-
yield { type: "text_delta", text: part.text };
637+
yield textEvent;
619638
}
620639
const inline = (part as { inlineData?: { mimeType?: string; data?: string } }).inlineData;
621640
if (inline && typeof inline.data === "string") {
@@ -817,7 +836,7 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
817836
}
818837
const events: AdapterEvent[] = [];
819838

820-
const candidates = json.candidates as { content?: { parts?: { text?: string; functionCall?: { name: string; args: unknown } }[] }; finishReason?: string }[] | undefined;
839+
const candidates = json.candidates as { content?: { parts?: GoogleResponsePart[] }; finishReason?: string }[] | undefined;
821840
if (!candidates?.length) {
822841
return finish([{ type: "error", message: "google response contained no candidates" }]);
823842
}
@@ -833,7 +852,8 @@ export function createGoogleAdapter(provider: OcxProviderConfig): ProviderAdapte
833852
observeAntigravityReplay(replayModel, replaySession, candidates[0].content.parts as unknown[]);
834853
}
835854
for (const part of candidates[0].content.parts) {
836-
if (part.text) events.push({ type: "text_delta", text: part.text });
855+
const textEvent = googlePartTextEvent(part);
856+
if (textEvent) events.push(textEvent);
837857
const inline = (part as { inlineData?: { mimeType?: string; data?: string } }).inlineData;
838858
if (inline && typeof inline.data === "string") {
839859
if (inline.data.length > MAX_ENCODED_BYTES_PER_IMAGE) {

‎structure/04_transports-and-sidecars.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,23 @@ pre-compaction checkpoint is not persisted for later carry-forward.
437437
- 장점, 단점 및 영향: Active-context reporting stays monotonic within an uncompacted Cursor conversation; no-checkpoint turns remain estimated; a process restart loses the numeric cache, and when neither a checkpoint nor a carry-forward is available the turn reports a request-local estimate derived from the same pruned payload sent to Cursor (#373 — reporting output-only usage made Codex read the context as nearly empty). Estimates are never persisted or promoted into checkpoint carry-forward; only live checkpoint frames update the cache.
438438
```
439439

440+
## Google thought-text visibility boundary
441+
442+
Google-family responses may represent model-internal reasoning as a text-bearing part with
443+
`thought: true`. The Google adapter maps that text to the internal `reasoning_raw_delta` event;
444+
only text without the marker becomes visible `text_delta`. Streaming SSE and buffered JSON share
445+
one classifier so transport selection cannot change whether provider-declared reasoning is shown
446+
as assistant output. Thought-signature observation still runs on the original parts before text
447+
classification, preserving the opaque continuation state independently of display semantics.
448+
449+
[Decision Log]
450+
- 목적과 의도: Prevent provider-marked internal reasoning from appearing as ordinary assistant text while preserving reasoning and tool-call continuation.
451+
- 기존 구현 및 제약 조건: Both Google response paths emitted every non-empty `Part.text` as visible text; function calls, inline images, and Antigravity/Vertex thought-signature replay already depended on the original part ordering.
452+
- 검토한 주요 대안: Drop thought text; classify it separately in each parser; remove the marker and keep visible text; use one shared classifier without mutating the provider parts.
453+
- 선택한 방식: Map `thought: true` text to `reasoning_raw_delta` through one helper used by streaming and buffered parsing, leaving part order and signature observation unchanged.
454+
- 다른 대안 대신 이 방식을 선택한 이유: Dropping the text loses reasoning replay/display policy input, while duplicated parser rules can drift and exposing marked thoughts violates the provider's visibility boundary.
455+
- 장점, 단점 및 영향: Internal reasoning no longer leaks into normal answers and both transports stay consistent; downstream reasoning policy still decides whether raw reasoning is rendered or only preserved, and malformed non-boolean markers remain ordinary text rather than broadening hidden-content inference.
456+
440457
## Google tool-call thought-signature replay
441458

442459
Gemini may attach an opaque `thoughtSignature` to a `functionCall` and requires that exact value on

‎tests/google-antigravity-wire.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,39 @@ describe("antigravity parseResponse unwraps response (non-streaming)", () => {
505505
applyAntigravityReplay("gemini-3-pro", antigravitySessionId(followup), contents);
506506
expect((contents[0].parts[0] as { thoughtSignature?: string }).thoughtSignature).toBe("sig-nonstream0000000");
507507
});
508+
509+
// Guard for #1503: routing `thought: true` text to the reasoning channel must not disturb
510+
// signature observation. Gemini 3 rejects a follow-up turn whose first function-call part
511+
// lost its signature, so a classification change that also dropped replay would trade a
512+
// visible-text bug for a hard 400. Asserting the signature survives a payload that mixes a
513+
// thought part with a signed function call is the direct proof, rather than inferring it
514+
// from unrelated fixtures that happen to still pass.
515+
test("a thought part alongside a signed function call does not disturb replay", async () => {
516+
const { __resetAntigravityReplayCache, applyAntigravityReplay } = await import("../src/adapters/google-antigravity-replay");
517+
__resetAntigravityReplayCache();
518+
const adapter = createGoogleAdapter(provider);
519+
await adapter.buildRequest(parsed("hello world"));
520+
const body = JSON.stringify({
521+
response: {
522+
candidates: [{
523+
content: {
524+
parts: [
525+
{ thought: true, text: "deciding which tool to call" },
526+
{ functionCall: { name: "do_x", args: { a: 1 } }, thoughtSignature: "sig-withthought00000" },
527+
],
528+
},
529+
}],
530+
},
531+
});
532+
const events = await adapter.parseResponse!(new Response(body, { status: 200 }));
533+
534+
expect(events).not.toContainEqual({ type: "text_delta", text: "deciding which tool to call" });
535+
536+
const followup = parsed("hello world");
537+
const contents = [{ role: "model", parts: [{ functionCall: { name: "do_x", args: { a: 1 } } }] }];
538+
applyAntigravityReplay("gemini-3-pro", antigravitySessionId(followup), contents);
539+
expect((contents[0].parts[0] as { thoughtSignature?: string }).thoughtSignature).toBe("sig-withthought00000");
540+
});
508541
});
509542

510543
describe("antigravity history preserves tool-call thoughtSignature", () => {

‎tests/google-hardening.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,97 @@ describe("google provider hardening", () => {
293293
expect(events.some(e => e.type === "error")).toBe(false);
294294
});
295295

296+
test("thought text stays hidden reasoning in streaming and non-streaming responses", async () => {
297+
const body = {
298+
candidates: [{
299+
content: { parts: [{ thought: true, text: "private analysis" }] },
300+
finishReason: "STOP",
301+
}],
302+
};
303+
304+
const streamEvents = await collect(createGoogleAdapter(provider()).parseStream(sseResponse([body])));
305+
const responseEvents = await createGoogleAdapter(provider()).parseResponse!(
306+
new Response(JSON.stringify(body), { status: 200 }),
307+
);
308+
309+
for (const events of [streamEvents, responseEvents]) {
310+
expect(events).toContainEqual({ type: "reasoning_raw_delta", text: "private analysis" });
311+
expect(events).not.toContainEqual({ type: "text_delta", text: "private analysis" });
312+
}
313+
});
314+
315+
test("thought text preserves ordering before function calls in both response modes", async () => {
316+
const body = {
317+
candidates: [{
318+
content: {
319+
parts: [
320+
{ thought: true, text: "choose the tool" },
321+
{ functionCall: { name: "lookup", args: { id: 7 } } },
322+
],
323+
},
324+
finishReason: "STOP",
325+
}],
326+
};
327+
328+
const streamEvents = await collect(createGoogleAdapter(provider()).parseStream(sseResponse([body])));
329+
const responseEvents = await createGoogleAdapter(provider()).parseResponse!(
330+
new Response(JSON.stringify(body), { status: 200 }),
331+
);
332+
333+
for (const events of [streamEvents, responseEvents]) {
334+
expect(events.slice(0, 4)).toEqual([
335+
{ type: "reasoning_raw_delta", text: "choose the tool" },
336+
{ type: "tool_call_start", id: expect.stringMatching(/^call_/), name: "lookup" },
337+
{ type: "tool_call_delta", arguments: '{"id":7}' },
338+
{ type: "tool_call_end" },
339+
]);
340+
expect(events).not.toContainEqual({ type: "text_delta", text: "choose the tool" });
341+
}
342+
});
343+
344+
test("ordinary Google text remains visible in both response modes", async () => {
345+
const body = {
346+
candidates: [{ content: { parts: [{ text: "visible answer" }] }, finishReason: "STOP" }],
347+
};
348+
349+
const streamEvents = await collect(createGoogleAdapter(provider()).parseStream(sseResponse([body])));
350+
const responseEvents = await createGoogleAdapter(provider()).parseResponse!(
351+
new Response(JSON.stringify(body), { status: 200 }),
352+
);
353+
354+
for (const events of [streamEvents, responseEvents]) {
355+
expect(events).toContainEqual({ type: "text_delta", text: "visible answer" });
356+
expect(events).not.toContainEqual({ type: "reasoning_raw_delta", text: "visible answer" });
357+
}
358+
});
359+
360+
// `emittedContentEvent` decides `"content"` vs `"continue"`, and its only consumer is the
361+
// synthetic-heartbeat suppression in the read loop. A thought delta is real upstream
362+
// activity, so it must count as content: emitting a heartbeat alongside it would claim the
363+
// stream was idle while the model was demonstrably working. Pinning that here keeps the
364+
// classification a decision rather than a side effect of routing thought text elsewhere.
365+
test("a thought-only frame counts as content, so no synthetic heartbeat is emitted", async () => {
366+
const thoughtOnly = {
367+
candidates: [{ content: { parts: [{ thought: true, text: "private analysis" }] } }],
368+
};
369+
const events = await collect(createGoogleAdapter(provider()).parseStream(sseResponse([thoughtOnly])));
370+
371+
expect(events).toContainEqual({ type: "reasoning_raw_delta", text: "private analysis" });
372+
expect(events.some(e => e.type === "heartbeat")).toBe(false);
373+
});
374+
375+
// The visible-text control for the assertion above: an ordinary text frame has always
376+
// suppressed the heartbeat, so a divergence here would mean thought parts are classified
377+
// differently from the text they replaced.
378+
test("a visible-text frame also suppresses the synthetic heartbeat", async () => {
379+
const textOnly = {
380+
candidates: [{ content: { parts: [{ text: "visible answer" }] } }],
381+
};
382+
const events = await collect(createGoogleAdapter(provider()).parseStream(sseResponse([textOnly])));
383+
384+
expect(events).toContainEqual({ type: "text_delta", text: "visible answer" });
385+
expect(events.some(e => e.type === "heartbeat")).toBe(false);
386+
});
296387
test("sends Gemini Flash thinkingLevel only for direct AI Studio requests", async () => {
297388
const direct = createGoogleAdapter(provider({
298389
modelReasoningEfforts: {

0 commit comments

Comments
 (0)