diff --git a/packages/cli/src/ui/contexts/KeypressContext.tsx b/packages/cli/src/ui/contexts/KeypressContext.tsx index 97db27563..e81747957 100644 --- a/packages/cli/src/ui/contexts/KeypressContext.tsx +++ b/packages/cli/src/ui/contexts/KeypressContext.tsx @@ -540,7 +540,16 @@ export function KeypressProvider({ } }; + // Matches terminal query responses (DA1, DA2, Kitty protocol query) + // that may arrive late from startup detection in kittyProtocolDetector. + // These are never valid user input. + // eslint-disable-next-line no-control-regex + const TERMINAL_RESPONSE_RE = /^\x1b\[[?>][\d;]*[uc]$/; + const handleKeypress = async (_: unknown, key: Key) => { + if (TERMINAL_RESPONSE_RE.test(key.sequence)) { + return; + } if (key.sequence === FOCUS_IN || key.sequence === FOCUS_OUT) { return; } diff --git a/packages/cli/src/ui/utils/kittyProtocolDetector.ts b/packages/cli/src/ui/utils/kittyProtocolDetector.ts index 3355330a6..a46390603 100644 --- a/packages/cli/src/ui/utils/kittyProtocolDetector.ts +++ b/packages/cli/src/ui/utils/kittyProtocolDetector.ts @@ -37,11 +37,20 @@ export async function detectAndEnableKittyProtocol(): Promise { const onTimeout = () => { timeoutId = undefined; process.stdin.removeListener('data', handleData); - if (!originalRawMode) { - process.stdin.setRawMode(false); - } - detectionComplete = true; - resolve(false); + + // Keep a drain handler briefly to consume any late-arriving terminal + // responses that would otherwise leak into the application input. + const drainHandler = () => {}; + process.stdin.on('data', drainHandler); + + setTimeout(() => { + process.stdin.removeListener('data', drainHandler); + if (!originalRawMode) { + process.stdin.setRawMode(false); + } + detectionComplete = true; + resolve(false); + }, 100); }; const handleData = (data: Buffer) => {