Startup Dialogs and Paced Paste
Two subtle terminal bugs break naive PTY wrappers: sending Enter on a modal that defaults to “No, exit”, and overflowing the kernel PTY input queue on long prompts. src/driver.zig handles both explicitly.
1. Claude Code 2.1 startup dialog state machine
Section titled “1. Claude Code 2.1 startup dialog state machine”Before SessionStart fires, Claude Code may display modal prompts (workspace folder trust, bypass-permissions confirmation, or development-channels confirmation).
In Claude Code 2.1, the workspace-trust modal lists and preselects "No, exit" above "Yes, I trust this folder", has no option numbers, ignores digit keys, and wraps arrow keys. Blindly pressing Enter exits Claude immediately.
maybeHandleStartupDialogs() runs only when the PTY has been quiet for 80 ms (dialog_quiescence_ms):
- Zero-heap snapshot & CSI strip: Copies the 8192-byte rolling buffer
shared.recentonto the stack and strips CSI/OSC/DCS sequences into stack buffers (fullandfreshsincebytes_at_key). - Latest-marker classification (
classifyDialog): Detects.trust(trust+folder),.bypass_permissions(bypass+permissions+accept), and.dev_channels(loading+development+channels). Latest marker wins; if Claude’s main prompt footer ("for shortcuts") painted afterward, returns.none. - Cursor verification (
analyzeDialog): Finds the last❯glyph (\xe2\x9d\xaf) and uses whitespace-insensitive prefix matching (looseMatch, because Ink pads words with cursor moves that strip to zero spaces on first paint). - Safe action selection (
chooseDialogAction):- Sends
\ronly when❯is verified on an affirmative option (Yes,Itrust,Yes,proceed,Yes,Iaccept,Iamusing). - When
❯is on a negative option (No,exit,Exit): sends the option digit on numbered menus (e.g.'2'for2. Yes, I accept), or falls back to Down (\x1b[B) / Up (\x1b[A) depending on whether the affirmative text appears below the cursor. - Waits for fresh repaint bytes before acting again. If zero fresh bytes arrive within
1500 ms(dialog_retry_ms), retries the dropped key. Hard cap:16keys (max_dialog_keys).
- Sends
- Intent gate (
dialogConfirmAllowed): The workspace-trust dialog is always answered. The bypass-permissions dialog is answered only when this invocation passed--dangerously-skip-permissionsor--permission-mode bypassPermissions(as a wrapper flag or in the forwarded arguments). The development-channels dialog is answered only when the forwarded arguments include--dangerously-load-development-channels. Any other dialog is never answered: no key is sent, and the run fails withRunError.UnexpectedStartupDialog(exit2; the daemon emits anunexpected_startup_dialogerror frame and exits2). A dialog raised by settings files alone is not consent.
2. Paced bracketed paste and macOS TTYHOG
Section titled “2. Paced bracketed paste and macOS TTYHOG”macOS enforces a 1024-byte TTYHOG limit on raw PTY input queues. Writing a 4 KB prompt in one writeInput() call silently drops bytes past 1024. On top of that, raw newlines inside a prompt trigger early submission, and sending \r immediately after a paste gets swallowed by Ink’s paste burst detector.
typePrompt() and PasteChunker solve all three constraints:
- Bracketed paste framing: Wraps the prompt in DECSET 2004
\x1b[200~…\x1b[201~so embedded newlines stay inside one prompt. - Chunk sizing: Emits chunks of at most
512bytes (paste_chunk_bytes, half ofTTYHOG), pausing8 ms(paste_chunk_gap_ms) between chunks so the child drains each write. - UTF-8 safety: Checks
utf8ByteSequenceLengthbefore adding a leading byte (>= 0xC0) so a multi-byte character is never split across chunk boundaries. - Injection safety: Detects any literal
\x1b[201~inside untrusted prompt text and drops its trailing~, preventing prompt content from escaping bracketed paste early. - Enter debounce: Sleeps
120 ms(ink_enter_debounce_ms) after\x1b[201~, then writes\ronce.
3. Prompt-acceptance and truncation watchdog
Section titled “3. Prompt-acceptance and truncation watchdog”After typePrompt() finishes, one-shot mode expects confirmation from either UserPromptSubmit or transcript growth past pre_prompt_transcript_offset:
- Truncation check (
isPromptTruncated): OnUserPromptSubmit, if the prompt has more than64non-whitespace bytes (prompt_truncation_min_len) and Claude’s received string does not contain a paste placeholder ([Pasted text #or<pasted_content), having fewer non-whitespace bytes than expected aborts immediately withRunError.PromptTruncated. - Re-injection (
promptWatchdogAction): If neither confirmation arrives within4000 ms(prompt_accept_timeout_ms), the driver sendsCtrl-U(\x15) to clear residual input, waits for quiescence, and re-runstypePrompt(). After4total attempts (max_prompt_injections), it fails fast withRunError.PromptNotAccepted.