Codex App Server Persisted
[Created by Codex: 019b74f5-a7d5-7121-ac48-8e66cd2bdc92]
Purpose
Provide a repeatable workflow to keep a single Codex process running in memory via codex app-server, and drive multi-turn interactions by sending thread/start once and turn/start repeatedly on the same threadId.
When To Use
- •Need a warm, long-lived Codex PID to minimize turn-to-turn latency
- •Need programmatic control over turns (JSON-RPC over stdio) instead of a TUI
- •Need built-in observability via
sse_lines.jsonl(works withapp-server, not withmcp-server)
Workflow
1) Confirm requirements
- •Identify whether the goal is: single-controller (one process drives Codex) or multi-client daemon/broker.
- •Identify whether approvals must be supported or bypassed (risk/permissions).
- •Identify desired observability target: centralized
~/centralized-logs/codex/sse_lines.jsonlor isolated--log-dir.
2) Start a long-lived app-server process
- •Spawn
codex app-serveras a subprocess withstdin=PIPEand keep stdin open for the lifetime of the controller. - •Read stdout line-by-line and parse each line as JSON (JSON-RPC-like).
- •Treat stderr as inherited or actively drained to avoid deadlocks.
3) Create a thread once, then reuse it
- •Send
initialize. - •Send
thread/startand storethreadId. - •For each user prompt, send
turn/startwith the samethreadId.
4) Stream events and detect completion
- •Expect notifications interleaved with responses on stdout.
- •Print/collect assistant output using
codex/event/agent_message_content_delta. - •Consider a turn complete when either:
- •notification method
codex/event/task_completearrives, or - •notification method
turn/completedarrives.
- •notification method
5) Handle approvals if enabled
- •Detect server→client JSON-RPC requests (messages that include both
methodandidbut noresult). - •For V2 approval requests, handle at least:
- •
item/commandExecution/requestApproval - •
item/fileChange/requestApproval
- •
- •Reply with a JSON-RPC response
{ "id": <same>, "result": { "decision": "accept" | "decline" } }.
6) Validate observability logging
- •Prefer to run
codexwith--log-dirwhen isolation is needed (createssse_lines.jsonlunder that directory). - •Otherwise, verify the centralized
~/centralized-logs/codex/sse_lines.jsonlchanges while the app-server PID stays alive.
Bundled Resources
- •Reference guide:
references/codex-app-server-persisted-process.md - •Starter script:
scripts/app_server_persisted_repl.py