Skip to content
Merged
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
42 changes: 40 additions & 2 deletions packages/cli/src/ui/AppContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3185,7 +3185,7 @@ describe('AppContainer State Management', () => {
});
});

it('clears the prompt when onCancelSubmit is called with shouldRestorePrompt=false', async () => {
it('preserves buffer when cancelling, even if empty (user is in control)', async () => {
let unmount: () => void;
await act(async () => {
const result = renderAppContainer();
Expand All @@ -3201,7 +3201,45 @@ describe('AppContainer State Management', () => {
onCancelSubmit(false);
});

expect(mockSetText).toHaveBeenCalledWith('');
// Should NOT modify buffer when cancelling - user is in control
expect(mockSetText).not.toHaveBeenCalled();

unmount!();
});

it('preserves prompt text when cancelling streaming, even if same as last message (regression test for issue #13387)', async () => {
// Mock buffer with text that user typed while streaming (same as last message)
const promptText = 'What is Python?';
mockedUseTextBuffer.mockReturnValue({
text: promptText,
setText: mockSetText,
});

// Mock input history with same message
mockedUseInputHistoryStore.mockReturnValue({
inputHistory: [promptText],
addInput: vi.fn(),
initializeFromLogger: vi.fn(),
});

let unmount: () => void;
await act(async () => {
const result = renderAppContainer();
unmount = result.unmount;
});
await waitFor(() => expect(capturedUIState).toBeTruthy());

const { onCancelSubmit } = extractUseGeminiStreamArgs(
mockedUseGeminiStream.mock.lastCall!,
);

act(() => {
// Simulate Escape key cancelling streaming (shouldRestorePrompt=false)
onCancelSubmit(false);
});

// Should NOT call setText - prompt should be preserved regardless of content
expect(mockSetText).not.toHaveBeenCalled();

unmount!();
});
Expand Down
11 changes: 9 additions & 2 deletions packages/cli/src/ui/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1218,16 +1218,23 @@ Logging in with Google... Restarting Gemini CLI to continue.
return;
}

// If cancelling (shouldRestorePrompt=false), never modify the buffer
// User is in control - preserve whatever text they typed, pasted, or restored
if (!shouldRestorePrompt) {
return;
}

// Restore the last message when shouldRestorePrompt=true
const lastUserMessage = inputHistory.at(-1);
let textToSet = shouldRestorePrompt ? lastUserMessage || '' : '';
let textToSet = lastUserMessage || '';

const queuedText = getQueuedMessagesText();
if (queuedText) {
textToSet = textToSet ? `${textToSet}\n\n${queuedText}` : queuedText;
clearQueue();
}

if (textToSet || !shouldRestorePrompt) {
if (textToSet) {
buffer.setText(textToSet);
}
},
Expand Down
Loading