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
45 changes: 45 additions & 0 deletions packages/core/src/core/geminiChat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
THINKING_ONLY_NUDGE_MESSAGE,
NO_RESPONSE_TEXT_NUDGE_MESSAGE,
applyRetryNudge,
INTERRUPTED_RESPONSE_PLACEHOLDER,
} from './geminiChat.js';
import {
type CompletedToolCall,
Expand Down Expand Up @@ -3834,6 +3835,50 @@ describe('GeminiChat', () => {
});
});

describe('interruption handling in getHistoryTurns', () => {
it('should replace INTERRUPTED_RESPONSE_PLACEHOLDER with a benign continuing message in curated history to prevent poisoning while preventing turn fusion', () => {
vi.mocked(mockConfig.isContextManagementEnabled).mockReturnValue(false);
vi.mocked(mockConfig.getModel).mockReturnValue('gemini-2.5-pro');

chat.setHistory([
{ role: 'user', parts: [{ text: 'search for files' }] },
{
role: 'model',
parts: [{ functionCall: { name: 'glob', args: {} } }],
},
{
role: 'user',
parts: [{ functionResponse: { name: 'glob', response: { files: [] } } }],
},
{
role: 'model',
parts: [{ text: INTERRUPTED_RESPONSE_PLACEHOLDER }],
},
{ role: 'user', parts: [{ text: 'continue search' }] },
]);

const turns = chat.getHistoryTurns(true);

// The interrupted response turn should be mapped to "Continuing.".
// The turns should NOT be coalesced, and their roles must alternate perfectly.
expect(turns).toHaveLength(5);
expect(turns[0].content.role).toBe('user');
expect(turns[0].content.parts![0].text).toBe('search for files');

expect(turns[1].content.role).toBe('model');
expect(turns[1].content.parts![0].functionCall?.name).toBe('glob');

expect(turns[2].content.role).toBe('user');
expect(turns[2].content.parts![0].functionResponse?.name).toBe('glob');

expect(turns[3].content.role).toBe('model');
expect(turns[3].content.parts![0].text).toBe('Continuing.');

expect(turns[4].content.role).toBe('user');
expect(turns[4].content.parts![0].text).toBe('continue search');
});
});

describe('ensureActiveLoopHasThoughtSignatures', () => {
it('should add thoughtSignature to the first functionCall in each model turn of the active loop', () => {
const chat = new GeminiChat(mockConfig, '', [], []);
Expand Down
24 changes: 22 additions & 2 deletions packages/core/src/core/geminiChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,28 @@ function extractCuratedHistory(
const modelOutput: HistoryTurn[] = [];
let isValid = true;
while (i < length && comprehensiveHistory[i].content.role === 'model') {
modelOutput.push(comprehensiveHistory[i]);
if (isValid && !isValidContent(comprehensiveHistory[i].content)) {
let turn = comprehensiveHistory[i];
if (
turn.content.parts?.some(
(part) => part?.text === INTERRUPTED_RESPONSE_PLACEHOLDER,
)
) {
const newParts = turn.content.parts.map((part) => {
if (part?.text === INTERRUPTED_RESPONSE_PLACEHOLDER) {
return { ...part, text: 'Continuing.' };
}
return part;
});
Comment thread
rahuldevlab marked this conversation as resolved.
turn = {
...turn,
content: {
...turn.content,
parts: newParts,
},
};
}
modelOutput.push(turn);
if (isValid && !isValidContent(turn.content)) {
isValid = false;
}
i++;
Expand Down