跪拜 Guibai
← All articles
Backend · Architecture · Design Patterns

Codex's Architecture: From a Single Model Call to a Recoverable Runtime System

By HarmonSir ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Coding agent builders get a concrete map of where naive agents break down and the exact boundaries Codex adds, which separates a demo from a runtime that survives restarts and grows experience. The patterns, from context projection to privilege-limited background agents, transfer directly to any agent architecture.

Summary

Codex's source shows a chain that begins with a user's input and ends with a recoverable storage layer. The execution path goes through session admission, context projection, model request, tool execution, and result write-back, but a second lifecycle path feeds results into rollout files, background memory consolidation, and restart recovery. Each step exists because a naive agent would break at that point.

Key boundaries include thread/session/turn resource layering with explicit admission states, a ContextManager that projects canonical history into model input, a unified ToolInvocation for native/MCP/Code Mode tools, and a persistence split where JSONL is the source of truth and SQLite is just an index. A two-phase memory pipeline extracts from rollout, then runs a heavily restricted consolidation agent that cannot read or generate memory, access the network, or use plugins.

Takeaways
— Codex has three entry points: CLI, TUI, and app-server, with app-server exposing a JSON-RPC semantic boundary that keeps protocol behavior consistent.
— Conversations are split into thread, session, and turn, with turn admission states Idle, Pending, and Admitted to manage concurrency and interruption.
— ContextManager::for_prompt projects canonical history into model input, normalizing media, stripping unsupported modalities, and replacing missing files with placeholders.
— Tool calls from native, MCP, Code Mode, and dynamic sources all flow through ToolInvocation, giving approvals and permission checks a single enforcement point.
— ExecutedToolCalls::attach_to_prompt writes tool request metadata back into the prompt so the model knows which call produced each result.
— RolloutRecorder writes JSONL as the canonical history; SQLite only stores queryable metadata, so query efficiency never corrupts the fact source.
— Memory runs in two phases: phase 1 extracts raw memory and summaries from rollout items; phase 2 consolidates them, but its agent is restricted to be ephemeral, non-memory-reading, non-memory-writing, network-restricted, and plugin-free.
— Recovery starts from an SQLite-selected rollout path, replays the JSONL lines, and reconstructs the thread id from the first SessionMeta entry.
— Each of the eight steps addresses a specific naive-agent failure: multi-entry drift, conversation continuity, context fidelity, tool side effects, result provenance, restart loss, experience accumulation, and recovery semantics.
Conclusions

Codex's architecture is reactive, not feature-driven: every layer exists because a naive agent would collapse at that point, which gives the design a strong sense of necessity.

Separating the source of truth (JSONL) from the index (SQLite) protects audit correctness from performance pressure, a pattern worth copying for any system that must both record and query.

The memory consolidation agent is deliberately crippled to prevent recursive capability amplification, showing that self-improvement must be bounded by explicit privilege limits.

Treating conversations as resources with lifecycles, rather than message lists, is what makes concurrency, interruption, and shutdown manageable in a local coding agent.

The model never sees raw history; it sees a projection. That boundary prevents unsupported media and stale metadata from polluting the prompt, and it keeps the fact source clean.

Concepts & terms
JSON-RPC
A remote procedure call protocol encoded in JSON, used here as the semantic boundary between Codex's client entries (CLI, TUI, IDE) and the app-server, ensuring consistent request handling across carriers.
Canonical history
The authoritative, append-only record of everything that happened in a conversation, stored as JSONL rollout files. It is the source of truth, separate from any indexes or projections.
Turn admission state
A state machine (Idle, Pending, Admitted) that gates whether a turn can execute, preventing overlapping or interrupted turns in a concurrent agent runtime.
Context projection
The process by which ContextManager transforms canonical history into what the model actually sees, stripping unsupported media, replacing missing files with placeholders, and applying per-model input policies.
Rollout
A JSONL file that records canonical history items in append-only form. It is the replayable ledger used to recover session state after a restart, with SQLite serving only as an index.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗