Documentation
¶
Overview ¶
Package codex reads OpenAI Codex's session transcripts.
Codex is the easiest of the three agents to observe. It writes one JSONL "rollout" file per session under ~/.codex/sessions/YYYY/MM/DD/, and that file already carries everything Caprock stores: the session id and working directory, the model, per-turn token counts split by kind, tool calls with their arguments, and the plan-limit windows. Nothing has to be installed, no config is rewritten, and no process is signalled — the observation is a read of files Codex was going to write anyway.
The two things worth knowing before changing anything here were measured against 100 real transcripts, not inferred from documentation:
`token_count` carries both a running `total_token_usage` and a per-turn `last_token_usage`, and the same values are frequently emitted twice in a row. Summing `last` therefore double-counts: on one real session it gave 261,111 tokens against a true 137,739. Deltas are taken from the *cumulative* field instead, which is monotonic in every transcript checked and reproduces the final total exactly on all 92 sessions that carry one.
Codex reports tokens but never a cost. Unlike OpenCode, whose own figure Caprock carries through, Codex sessions are priced by our own table — which is why pricing.json had to grow OpenAI rows (see .ai/19-codex.md and pricing/pricing.json).
Index ¶
Constants ¶
const Agent = "codex"
Agent is the value written to sessions.agent for Codex sessions.
const EnvDir = "CAPROCK_CODEX_DIR"
EnvDir overrides the transcript root.
Variables ¶
var ErrNotASession = errors.New("codex: not a rollout transcript")
ErrNotASession is returned for a file with no session_meta record.
Functions ¶
func Available ¶
func Available() bool
Available reports whether Codex transcripts exist on this machine.
func Dir ¶
func Dir() string
Dir is the root Codex writes its rollout transcripts under.
It is the same path on every platform: Codex uses the home directory directly, with no XDG or %APPDATA% branching, so there is nothing to switch on here. CAPROCK_CODEX_DIR overrides it for tests and for anyone whose Codex lives somewhere unusual.
Types ¶
type Ingester ¶
type Ingester struct {
// contains filtered or unexported fields
}
Ingester copies Codex's rollout transcripts into Caprock's store.
It polls rather than watches. The transcripts are append-only files, so a poll that re-reads one it has already seen costs a read and produces nothing: every event carries a key derived from the record's ordinal, and `(session_id, key)` is unique in the store.
Only files whose modification time moved are re-read, which on a machine with a hundred transcripts is one stat per file per tick and no parsing at all.
func NewIngester ¶
NewIngester builds an ingester over a Codex transcript directory.
type Limits ¶
type Limits struct {
At time.Time
PrimaryPct float64
PrimaryMinutes int
PrimaryResets int64
SecondaryPct float64
SecondaryMinutes int
SecondaryResets int64
}
Limits is a plan-limit sample. Codex reports the same two windows Claude Code does, in minutes rather than by name.
type Session ¶
type Session struct {
ID string
Path string
Cwd string
Model string
CLIVersion string
// Originator is what launched the session ("Codex Desktop", "codex_cli_rs").
// Kept because a desktop session and a terminal session are different
// things to a reader looking at where their money went.
Originator string
StartedAt time.Time
Turns []Turn
Tools []ToolCall
// Limits is the most recent plan-limit sample in the transcript, when the
// session carried one.
Limits *Limits
}
Session is one Codex rollout transcript, parsed.
func Parse ¶
Parse reads a rollout transcript from r. path is used only for the returned Session's Path field.
func ParseFile ¶
ParseFile reads one rollout transcript.
A malformed line is skipped rather than failing the file: these are written by another program while it runs, so the last line of a live session is routinely a partial write, and refusing the whole transcript for it would mean a session is invisible until it ends.
type Stats ¶
type Stats struct {
Sessions int `json:"sessions"`
Events int `json:"events"`
Unpriced int `json:"unpriced,omitempty"`
LastPoll int64 `json:"last_poll_ms,omitempty"`
}
Stats is what the daemon reports about Codex ingest.
type ToolCall ¶
type ToolCall struct {
At time.Time
Key string
Name string
// Input is the raw argument payload, stored so the dashboard can show what
// a call actually did.
Input string
}
ToolCall is one tool invocation.
type Transcript ¶
Transcript is a rollout file on disk, before it is parsed.
func List ¶
func List(dir string) ([]Transcript, error)
List finds every rollout transcript under dir, newest first.
It walks rather than globbing a fixed depth: the layout is YYYY/MM/DD today, and a walk keeps working if Codex changes it. Directories it cannot read are skipped rather than failing the scan — a transcript root is someone else's directory and may contain anything.
type Turn ¶
type Turn struct {
At time.Time
// Key is stable across re-reads: the transcript is append-only, so the
// ordinal of the record that produced the turn identifies it forever.
Key string
In int64
CacheRead int64
CacheWrite int64
Out int64
// Reasoning tokens are billed as output and are already included in Out.
// Carried separately only so a reader can see the split.
Reasoning int64
}
Turn is one assistant turn's token usage, already reduced to a delta.