Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d93a758
feat(core): reject upstream fail-fast placeholder responses
yiliang114 Aug 11, 2026
9b6de2e
Merge remote-tracking branch 'origin/main' into HEAD
yiliang114 Aug 12, 2026
6bded47
Merge branch 'main' into feat/8916-degraded-placeholder-defense
wenshao Aug 12, 2026
c504077
Merge remote-tracking branch 'origin/main' into HEAD
yiliang114 Aug 12, 2026
9b06525
fix(core): prevent placeholder text from reaching display before retr…
yiliang114 Aug 13, 2026
aa2572c
Merge remote-tracking branch 'origin/main' into HEAD
yiliang114 Aug 13, 2026
4767a29
fix(core): block degraded continuation leaks
yiliang114 Aug 13, 2026
bf107c7
fix(core): catch split degraded placeholders
yiliang114 Aug 13, 2026
121ba97
fix(core): withhold degraded placeholder chunks
yiliang114 Aug 14, 2026
e8dc46f
fix(core): preserve current image payloads after placeholder curation
yiliang114 Aug 14, 2026
0d37451
fix(core): preserve deferred stream chunks safely
yiliang114 Aug 14, 2026
a5adb37
Merge remote-tracking branch 'origin/main' into HEAD
yiliang114 Aug 14, 2026
b967f8e
fix(core): fold held placeholder-prefix text into continuation accoun…
yiliang114 Aug 14, 2026
60891f5
fix(core): latch deferredFirstChunk so duplicate finishReason chunks …
yiliang114 Aug 14, 2026
387f918
fix(core): judge deferred chunk against accumulated text on stream error
yiliang114 Aug 14, 2026
2a2d5db
fix(core): deliver withheld placeholder-defense chunks in arrival ord…
yiliang114 Aug 14, 2026
6f42821
fix(core): propagate image eviction to durable history, harden placeh…
yiliang114 Aug 14, 2026
1330183
fix(core): preserve deferred stream metadata
yiliang114 Aug 15, 2026
a1e5b4b
Merge remote-tracking branch 'origin/main' into HEAD
yiliang114 Aug 15, 2026
5733389
fix(core): close round-12 findings on degraded-placeholder defense
yiliang114 Aug 15, 2026
250dd3e
fix(core): close round-13 findings on degraded-placeholder defense
yiliang114 Aug 15, 2026
1c0be41
test(core): pin full history in the second tool-result continuation test
yiliang114 Aug 15, 2026
56068cd
fix(core): read the converted placeholder error type in budget telemetry
yiliang114 Aug 15, 2026
e2137cd
fix(core): exempt last-referenced images from reattach cap + harden t…
yiliang114 Aug 15, 2026
aad6c11
refactor(core): dedupe placeholder-error and reattach-append sites
yiliang114 Aug 15, 2026
c6d1969
fix(core): gate placeholder conversion on !streamYieldedFunctionCall …
yiliang114 Aug 15, 2026
0c78052
test(core): attach the rejection handler before timer flush (#8938)
yiliang114 Aug 15, 2026
8cce5d2
test(core): satisfy vitest/valid-expect on the pre-timer rejection ha…
yiliang114 Aug 15, 2026
7974050
fix(core): scope placeholder run invalidation to the degraded turns (…
yiliang114 Aug 16, 2026
74bc08d
Merge remote-tracking branch 'origin/main' into feat/8916-degraded-pl…
yiliang114 Aug 18, 2026
e281211
refactor(core): narrow degraded placeholder defense
yiliang114 Aug 18, 2026
74211a0
fix(core): preserve placeholder-aware tool adjacency
yiliang114 Aug 18, 2026
f1e647a
Merge branch 'main' into feat/8916-degraded-placeholder-defense
yiliang114 Aug 19, 2026
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
329 changes: 329 additions & 0 deletions packages/core/src/core/geminiChat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2660,6 +2660,204 @@ describe('GeminiChat', async () => {
]);
});

it('excludes degraded placeholder model turns from curated history and requests', async () => {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
chat.setHistory([
{ role: 'user', parts: [{ text: 'summarize the work' }] },
{ role: 'model', parts: [{ text: '(request timeout)' }] },
]);
const response = (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: 'real summary' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(
response,
);

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'again' },
'prompt-id-placeholder-curation',
);
for await (const _ of stream) {
// consume stream
}

const request = vi.mocked(mockContentGenerator.generateContentStream).mock
.calls[0]?.[0];
expect(request?.contents).toEqual([
{
role: 'user',
parts: [{ text: 'summarize the work' }, { text: 'again' }],
},
]);
expect(chat.getHistory(true)).toEqual([
{
role: 'user',
parts: [{ text: 'summarize the work' }, { text: 'again' }],
},
{ role: 'model', parts: [{ text: 'real summary' }] },
]);
// Comprehensive history keeps the faithful record.
expect(chat.getHistory()).toEqual([
{ role: 'user', parts: [{ text: 'summarize the work' }] },
{ role: 'model', parts: [{ text: '(request timeout)' }] },
{ role: 'user', parts: [{ text: 'again' }] },
{ role: 'model', parts: [{ text: 'real summary' }] },
]);
});

it('keeps turns that only mention the placeholder text inside longer output', async () => {
chat.setHistory([
{ role: 'user', parts: [{ text: 'what happened?' }] },
{
role: 'model',
parts: [{ text: 'the upstream returned "(request timeout)" once' }],
},
]);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
const response = (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: 'ok' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(
response,
);

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'next' },
'prompt-id-placeholder-mention',
);
for await (const _ of stream) {
// consume stream
}

const request = vi.mocked(mockContentGenerator.generateContentStream).mock
.calls[0]?.[0];
expect(request?.contents).toEqual([
{ role: 'user', parts: [{ text: 'what happened?' }] },
{
role: 'model',
parts: [{ text: 'the upstream returned "(request timeout)" once' }],
},
{ role: 'user', parts: [{ text: 'next' }] },
]);
});

it('excludes a whitespace-padded degraded placeholder from curated history', async () => {
chat.setHistory([
{ role: 'user', parts: [{ text: 'query' }] },
{
role: 'model',
parts: [{ text: '(request timeout)\n' }],
},
]);
const response = (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: 'real' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(
response,
);

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'next' },
'prompt-id-placeholder-curation-trim',
);
for await (const _ of stream) {
// consume stream
}

const request = vi.mocked(mockContentGenerator.generateContentStream).mock
.calls[0]?.[0];
expect(request?.contents).toEqual([
{ role: 'user', parts: [{ text: 'query' }, { text: 'next' }] },
]);
expect(chat.getHistory(true)).toEqual([
{ role: 'user', parts: [{ text: 'query' }, { text: 'next' }] },
{ role: 'model', parts: [{ text: 'real' }] },
]);
});

it('keeps a placeholder turn that carries a functionCall in curated history', async () => {
chat.setHistory([
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
{ role: 'user', parts: [{ text: 'query' }] },
{
role: 'model',
parts: [
{ text: '(request timeout)' },
{ functionCall: { name: 'someTool', args: {} } },
],
},
]);
const response = (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: 'real' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(
response,
);

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'next' },
'prompt-id-placeholder-curation-tool-call',
);
for await (const _ of stream) {
// consume stream
}

const request = vi.mocked(mockContentGenerator.generateContentStream).mock
.calls[0]?.[0];
expect(request?.contents).toEqual([
{ role: 'user', parts: [{ text: 'query' }] },
{
role: 'model',
parts: [
{ text: '(request timeout)' },
{ functionCall: { name: 'someTool', args: {} } },
],
},
{ role: 'user', parts: [{ text: 'next' }] },
]);
});

it('does not deep-clone the full curated history when building request contents', async () => {
chat.setHistory([
{ role: 'user', parts: [{ text: 'prior question' }] },
Expand Down Expand Up @@ -6057,6 +6255,137 @@ describe('GeminiChat', async () => {
}
});

it('should retry an upstream fail-fast placeholder response without persisting it', async () => {
vi.useFakeTimers();
try {
let callCount = 0;
vi.mocked(
mockContentGenerator.generateContentStream,
).mockImplementation(async () => {
callCount++;
if (callCount === 1) {
return (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: '(request timeout)' }],
role: 'model',
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
}

return (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: 'Recovered response' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})();
});

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'test' },
'prompt-id-degraded-placeholder',
);
const events = await collectStreamWithFakeTimers(stream, 25_000);

expect(
mockContentGenerator.generateContentStream,
).toHaveBeenCalledTimes(2);
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
expect(mockLogContentRetry).toHaveBeenCalledWith(
mockConfig,
expect.objectContaining({
error_type: 'UPSTREAM_DEGRADED_RESPONSE',
model: 'test-model',
}),
);
expect(
events.some(
(event) =>
event.type === StreamEventType.CHUNK &&
event.value.candidates?.[0]?.content?.parts?.[0]?.text ===
'Recovered response',
),
).toBe(true);
expect(
events.some(
(event) =>
event.type === StreamEventType.CHUNK &&
event.value.candidates?.[0]?.content?.parts?.[0]?.text ===
'(request timeout)',
),
).toBe(false);
expect(chat.getHistory()).toEqual([
{ role: 'user', parts: [{ text: 'test' }] },
{ role: 'model', parts: [{ text: 'Recovered response' }] },
]);
} finally {
vi.useRealTimers();
}
});

it('should exhaust retries for upstream fail-fast placeholder responses', async () => {
vi.useFakeTimers();
try {
vi.mocked(
mockContentGenerator.generateContentStream,
).mockImplementation(async () => (async function* () {
yield {
candidates: [
{
content: {
parts: [{ text: '(request timeout)' }],
role: 'model',
},
finishReason: 'STOP',
},
],
} as unknown as GenerateContentResponse;
})());

const stream = await chat.sendMessageStream(
'test-model',
{ message: 'test' },
'prompt-id-degraded-placeholder-exhaustion',
);
await expectStreamExhaustion(stream);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

expect(
mockContentGenerator.generateContentStream,
).toHaveBeenCalledTimes(5);
expect(mockLogContentRetry).toHaveBeenCalledTimes(4);
expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
expect(mockLogContentRetryFailure).toHaveBeenCalledWith(
mockConfig,
expect.objectContaining({
total_attempts: 5,
final_error_type: 'UPSTREAM_DEGRADED_RESPONSE',
model: 'test-model',
}),
);

// History should still contain only the user message.
expect(chat.getHistory()).toEqual([
{ role: 'user', parts: [{ text: 'test' }] },
]);
} finally {
vi.useRealTimers();
}
});

it.each([
{
name: 'protocol tag leaks',
Expand Down
Loading
Loading