Skip to content

Architecture

claude-p replaces claude -p by turning the interactive claude TUI into a deterministic subprocess with structured stdin/stdout. It does this without modifying ~/.claude/ and without scraping the rendered terminal to detect completion.

claude-p process (Zig 0.15.2) driver / daemon loop • startup dialog FSM • paced bracketed paste • watchdogs + FIFO poll zmux.NativeSession • openpty / forkpty • background reader thread • 8 KB ring + EventSink terminal.zig (reader thread) DA1 · DA2 · DSR · XTVERSION · 18t queues into pending_to_pty stream · transcript · emit pread Tailer + offset parser stdout: text | json | stream-json hook.zig + events.fifo $TMPDIR/claude-p-pid-rand/ SessionStart · UserPromptSubmit · Stop StopFailure · PermissionRequest child `claude` Interactive Ink TUI --settings '<inline json>' TERM=xterm-256color no FIFO env var transcript.jsonl Claude's session log appended per message writeInput PTY pane_output mutex queue (no re-entry) hook.sh O_NONBLOCK read flush JSONL pread() tailer & parseFileFromOffset() on Stop
Two OS boundaries keep claude-p deterministic: a real PTY for Ink I/O, and an out-of-band FIFO + JSONL transcript for lifecycle events and output.

1. In-process zmux.NativeSession, no daemon process

Section titled “1. In-process zmux.NativeSession, no daemon process”

Ink (Claude Code’s terminal UI) exits immediately if stdin is not a TTY. claude-p imports zmux directly as a Zig dependency and uses only zmux.NativeSession—not zmuxd. That gives one binary openpty(3)/forkpty(3), a background PTY reader thread, writeInput()/send(), and bounded termination (SIGTERM, ~200 ms wait, SIGKILL).

2. Two threads, one mutex, zero re-entrancy

Section titled “2. Two threads, one mutex, zero re-entrancy”

zmux’s reader thread invokes driver.onZmuxEvent on every chunk of PTY output. Calling session.writeInput from inside that callback would re-enter zmux’s internal locks.

Instead, the reader thread stays strictly side-effect-free on the session:

  • Feeds po.data to the stateful terminal.DecDecoder (which carries split queries across chunks) and appends reply bytes to shared.pending_to_pty under shared.write_mutex.
  • Appends raw bytes to the 8192-byte rolling buffer shared.recent and updates shared.bytes_seen and shared.last_output_ns under shared.recent_mutex.

The main loop drains pending_to_pty via flushPendingToPty() (using a 512-byte stack buffer, zero heap allocation) on every loop tick and inside every quiescence wait.

3. Out-of-band lifecycle via inline --settings + FIFO

Section titled “3. Out-of-band lifecycle via inline --settings + FIFO”

Rather than guessing when a turn starts or ends from ANSI screen diffs, src/hook.zig generates an inline --settings JSON object registering five relay hooks: SessionStart, UserPromptSubmit, Stop, StopFailure, and PermissionRequest. Each hook invokes $TMPDIR/claude-p-<pid>-<rand>/hook.sh, which embeds the FIFO path and a per-run nonce and writes <nonce>\t<event>\t<json>\n to a named pipe (events.fifo) that the parent opened O_RDONLY | O_NONBLOCK before spawning claude.

4. Output comes from the session JSONL, never the PTY

Section titled “4. Output comes from the session JSONL, never the PTY”

The PTY exists only to keep Ink alive, dismiss startup modals, and deliver keystrokes. Every emitted assistant message, token count, and stream-json line is read directly from Claude’s session transcript file (transcript_path from the hook payload) via src/stream.zig and src/transcript.zig.

File Responsibility
main.zig CLI entry point, one-shot vs daemon dispatch, exit-code mapping.
args.zig claude -p flag parser, variadic flags, boolean passthrough list, validation.
terminal.zig Streaming DecDecoder DEC/XTerm query responder (DA1, DA2, DSR, XTVERSION, 18t).
hook.zig Temp dir, mkfifo, hook.sh, inline --settings + extra_hooks / extra_settings_json.
driver.zig One-shot lifecycle, env filter, startup dialog FSM, PasteChunker, watchdog.
daemon.zig Multi-turn NDJSON daemon (user, ping/pong, interrupt, system/init, result, error frames, idle and transcript watchdogs).
transcript.zig Session JSONL parser and offset-scoped parseFileFromOffset.
stream.zig Non-blocking pread-based line-buffered transcript Tailer.
emit.zig text, json, and stream-json output formatters.
root.zig Public Zig library facade and semantic version (0.1.3).