feat(cli): Prevent queuing of slash and shell commands (#11094)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Jainam M
2025-10-15 22:32:50 +05:30
committed by GitHub
parent b8df8b2ab8
commit 4f17eae5cc
7 changed files with 215 additions and 2 deletions

View File

@@ -956,6 +956,109 @@ describe('AppContainer State Management', () => {
});
});
describe('Queue Error Message', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('should set and clear the queue error message after a timeout', async () => {
const { rerender } = render(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBeNull();
capturedUIActions.setQueueErrorMessage('Test error');
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBe('Test error');
vi.advanceTimersByTime(3000);
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBeNull();
});
it('should reset the timer if a new error message is set', async () => {
const { rerender } = render(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
capturedUIActions.setQueueErrorMessage('First error');
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBe('First error');
vi.advanceTimersByTime(1500);
capturedUIActions.setQueueErrorMessage('Second error');
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBe('Second error');
vi.advanceTimersByTime(2000);
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBe('Second error');
// 5. Advance time past the 3 second timeout from the second message
vi.advanceTimersByTime(1000);
rerender(
<AppContainer
config={mockConfig}
settings={mockSettings}
version="1.0.0"
initializationResult={mockInitResult}
/>,
);
expect(capturedUIState.queueErrorMessage).toBeNull();
});
});
describe('Terminal Height Calculation', () => {
const mockedMeasureElement = measureElement as Mock;
const mockedUseTerminalSize = useTerminalSize as Mock;

View File

@@ -93,6 +93,7 @@ import { useExtensionUpdates } from './hooks/useExtensionUpdates.js';
import { ShellFocusContext } from './contexts/ShellFocusContext.js';
const CTRL_EXIT_PROMPT_DURATION_MS = 1000;
const QUEUE_ERROR_DISPLAY_DURATION_MS = 3000;
function isToolExecuting(pendingHistoryItems: HistoryItemWithoutId[]) {
return pendingHistoryItems.some((item) => {
@@ -154,6 +155,10 @@ export const AppContainer = (props: AppContainerProps) => {
config.isTrustedFolder(),
);
const [queueErrorMessage, setQueueErrorMessage] = useState<string | null>(
null,
);
const extensions = config.getExtensions();
const {
extensionsUpdateState,
@@ -815,6 +820,17 @@ Logging in with Google... Please restart Gemini CLI to continue.
}
}, [ideNeedsRestart]);
useEffect(() => {
if (queueErrorMessage) {
const timer = setTimeout(() => {
setQueueErrorMessage(null);
}, QUEUE_ERROR_DISPLAY_DURATION_MS);
return () => clearTimeout(timer);
}
return undefined;
}, [queueErrorMessage, setQueueErrorMessage]);
useEffect(() => {
if (isInitialMount.current) {
isInitialMount.current = false;
@@ -1131,6 +1147,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
currentLoadingPhrase,
historyRemountKey,
messageQueue,
queueErrorMessage,
showAutoAcceptIndicator,
showWorkspaceMigrationDialog,
workspaceExtensions,
@@ -1212,6 +1229,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
currentLoadingPhrase,
historyRemountKey,
messageQueue,
queueErrorMessage,
showAutoAcceptIndicator,
showWorkspaceMigrationDialog,
workspaceExtensions,
@@ -1276,6 +1294,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
onWorkspaceMigrationDialogOpen,
onWorkspaceMigrationDialogClose,
handleProQuotaChoice,
setQueueErrorMessage,
}),
[
handleThemeSelect,
@@ -1301,6 +1320,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
onWorkspaceMigrationDialogOpen,
onWorkspaceMigrationDialogClose,
handleProQuotaChoice,
setQueueErrorMessage,
],
);

View File

@@ -89,6 +89,8 @@ export const Composer = () => {
</Text>
) : uiState.showEscapePrompt ? (
<Text color={theme.text.secondary}>Press Esc again to clear.</Text>
) : uiState.queueErrorMessage ? (
<Text color={theme.status.error}>{uiState.queueErrorMessage}</Text>
) : (
!settings.merged.ui?.hideContextSummary && (
<ContextSummaryDisplay
@@ -149,6 +151,8 @@ export const Composer = () => {
? " Press 'i' for INSERT mode and 'Esc' for NORMAL mode."
: ' Type your message or @path/to/file'
}
setQueueErrorMessage={uiActions.setQueueErrorMessage}
streamingState={uiState.streamingState}
/>
)}

View File

@@ -28,6 +28,7 @@ import { useKittyKeyboardProtocol } from '../hooks/useKittyKeyboardProtocol.js';
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
import stripAnsi from 'strip-ansi';
import chalk from 'chalk';
import { StreamingState } from '../types.js';
vi.mock('../hooks/useShellHistory.js');
vi.mock('../hooks/useCommandCompletion.js');
@@ -2167,7 +2168,58 @@ describe('InputPrompt', () => {
expect(mockBuffer.handleInput).toHaveBeenCalled();
unmount();
});
it('should prevent slash commands from being queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = '/help';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('/help');
stdin.write('\r');
await wait();
expect(props.onSubmit).not.toHaveBeenCalled();
expect(props.setQueueErrorMessage).toHaveBeenCalledWith(
'Slash commands cannot be queued',
);
unmount();
});
it('should prevent shell commands from being queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = 'ls';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
props.shellModeActive = true;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('ls');
stdin.write('\r');
await wait();
expect(props.onSubmit).not.toHaveBeenCalled();
expect(props.setQueueErrorMessage).toHaveBeenCalledWith(
'Shell commands cannot be queued',
);
unmount();
});
it('should allow regular messages to be queued while streaming', async () => {
props.onSubmit = vi.fn();
props.buffer.text = 'regular message';
props.setQueueErrorMessage = vi.fn();
props.streamingState = StreamingState.Responding;
const { stdin, unmount } = renderWithProviders(<InputPrompt {...props} />);
await wait();
stdin.write('regular message');
stdin.write('\r');
await wait();
expect(props.onSubmit).toHaveBeenCalledWith('regular message');
expect(props.setQueueErrorMessage).not.toHaveBeenCalled();
unmount();
});
});
function clean(str: string | undefined): string {
if (!str) return '';
// Remove ANSI escape codes and trim whitespace

View File

@@ -38,6 +38,8 @@ import * as path from 'node:path';
import { SCREEN_READER_USER_PREFIX } from '../textConstants.js';
import { useShellFocusState } from '../contexts/ShellFocusContext.js';
import { useUIState } from '../contexts/UIStateContext.js';
import { StreamingState } from '../types.js';
import { isSlashCommand } from '../utils/commandUtils.js';
/**
* Returns if the terminal can be trusted to handle paste events atomically
@@ -71,6 +73,8 @@ export interface InputPromptProps {
onEscapePromptChange?: (showPrompt: boolean) => void;
vimHandleInput?: (key: Key) => boolean;
isEmbeddedShellFocused?: boolean;
setQueueErrorMessage: (message: string | null) => void;
streamingState: StreamingState;
}
// The input content, input container, and input suggestions list may have different widths
@@ -107,6 +111,8 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
onEscapePromptChange,
vimHandleInput,
isEmbeddedShellFocused,
setQueueErrorMessage,
streamingState,
}) => {
const kittyProtocol = useKittyKeyboardProtocol();
const isShellFocused = useShellFocusState();
@@ -221,6 +227,31 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
],
);
const handleSubmit = useCallback(
(submittedValue: string) => {
const trimmedMessage = submittedValue.trim();
const isSlash = isSlashCommand(trimmedMessage);
const isShell = shellModeActive;
if (
(isSlash || isShell) &&
streamingState === StreamingState.Responding
) {
setQueueErrorMessage(
`${isShell ? 'Shell' : 'Slash'} commands cannot be queued`,
);
return;
}
handleSubmitAndClear(trimmedMessage);
},
[
handleSubmitAndClear,
shellModeActive,
streamingState,
setQueueErrorMessage,
],
);
const customSetTextAndResetCompletionSignal = useCallback(
(newText: string) => {
buffer.setText(newText);
@@ -514,7 +545,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
// If the command is a perfect match, pressing enter should execute it.
if (completion.isPerfectMatch && keyMatchers[Command.RETURN](key)) {
handleSubmitAndClear(buffer.text);
handleSubmit(buffer.text);
return;
}
@@ -625,7 +656,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
buffer.backspace();
buffer.newline();
} else {
handleSubmitAndClear(buffer.text);
handleSubmit(buffer.text);
}
}
return;
@@ -706,6 +737,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
onClearScreen,
inputHistory,
handleSubmitAndClear,
handleSubmit,
shellHistory,
reverseSearchCompletion,
handleClipboardImage,

View File

@@ -45,6 +45,7 @@ export interface UIActions {
onWorkspaceMigrationDialogOpen: () => void;
onWorkspaceMigrationDialogClose: () => void;
handleProQuotaChoice: (choice: 'auth' | 'continue') => void;
setQueueErrorMessage: (message: string | null) => void;
}
export const UIActionsContext = createContext<UIActions | null>(null);

View File

@@ -89,6 +89,7 @@ export interface UIState {
currentLoadingPhrase: string;
historyRemountKey: number;
messageQueue: string[];
queueErrorMessage: string | null;
showAutoAcceptIndicator: ApprovalMode;
showWorkspaceMigrationDialog: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any