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
1 change: 1 addition & 0 deletions packages/cli/src/gemini.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ describe('startInteractiveUI', () => {

vi.mock('./ui/utils/kittyProtocolDetector.js', () => ({
detectAndEnableKittyProtocol: vi.fn(() => Promise.resolve(true)),
disableKittyProtocol: vi.fn(),
}));

vi.mock('./ui/utils/updateCheck.js', () => ({
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/gemini.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import { VimModeProvider } from './ui/contexts/VimModeContext.js';
import { AgentViewProvider } from './ui/contexts/AgentViewContext.js';
import { useKittyKeyboardProtocol } from './ui/hooks/useKittyKeyboardProtocol.js';
import { themeManager, AUTO_THEME_NAME } from './ui/themes/theme-manager.js';
import { detectAndEnableKittyProtocol } from './ui/utils/kittyProtocolDetector.js';
import {
detectAndEnableKittyProtocol,
disableKittyProtocol,
} from './ui/utils/kittyProtocolDetector.js';
import { checkForUpdates } from './ui/utils/updateCheck.js';
import {
cleanupCheckpoints,
Expand Down Expand Up @@ -289,6 +292,10 @@ export async function startInteractiveUI(
registerCleanup(async () => {
remoteInputWatcher?.shutdown();
await dualOutputBridge?.shutdown();
// Explicitly disable the Kitty keyboard protocol before unmounting Ink so
// that the disable escape sequence is written while stdout is still fully
// operational, preventing garbled terminal output after the app exits.
disableKittyProtocol();
instance.unmount();
restoreTerminalRedrawOptimizer();
});
Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/ui/utils/kittyProtocolDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ export async function detectAndEnableKittyProtocol(): Promise<boolean> {
protocolSupported = true;
protocolEnabled = true;

// Set up cleanup on exit
// Set up cleanup on exit (exit covers process.exit() calls,
// SIGTERM/SIGINT cover signal-based terminations).
process.on('exit', disableProtocol);
process.on('SIGTERM', disableProtocol);
process.on('SIGINT', disableProtocol);
}

detectionComplete = true;
Expand Down Expand Up @@ -116,6 +118,15 @@ function disableProtocol() {
}
}

/**
* Explicitly disables the Kitty keyboard protocol. Should be called during
* application cleanup before process.exit() to ensure the terminal is restored
* even if the 'exit' event handler does not fire in time (e.g. on SIGKILL).
*/
export function disableKittyProtocol(): void {
disableProtocol();
}

export function isKittyProtocolEnabled(): boolean {
return protocolEnabled;
}
Expand Down
Loading