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.
Four load-bearing design choices
Section titled “Four load-bearing design choices”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.datato the statefulterminal.DecDecoder(which carries split queries across chunks) and appends reply bytes toshared.pending_to_ptyundershared.write_mutex. - Appends raw bytes to the 8192-byte rolling buffer
shared.recentand updatesshared.bytes_seenandshared.last_output_nsundershared.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.
Module map (src/)
Section titled “Module map (src/)”| 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). |