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_HOME/sessions/YYYY/MM/DD/ (moved to $CODEX_HOME/archived_sessions/ when the thread is archived), 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
- Variables
- func Available() bool
- func Dirs() []string
- func Home() string
- func ReadThreadNames(ctx context.Context, path string) (map[string]ThreadName, error)
- func StateDB(sessionsDir string) string
- type Ingester
- type LimitWindow
- type Limits
- type Session
- type Stats
- type ThreadName
- type ToolCall
- type Transcript
- type Turn
Constants ¶
const Agent = "codex"
Agent is the value written to sessions.agent for Codex sessions.
const EnvDir = "CAPROCK_CODEX_DIR"
EnvDir overrides the transcript roots with one directory.
const EnvHome = "CODEX_HOME"
EnvHome is Codex's own override of its state directory.
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 Dirs ¶ added in v0.63.0
func Dirs() []string
Dirs are the roots Codex writes rollout transcripts under: `sessions/` (YYYY/MM/DD/rollout-*.jsonl) and `archived_sessions/`, where archiving a thread moves its rollout — a rename into one flat directory, the file itself unchanged (codex-rs thread-store, archive_thread.rs). An archived session is still work that was done and paid for; reading only `sessions/` never counted one archived before Caprock was installed, and lost it from any total rebuilt from the transcripts afterwards.
CAPROCK_CODEX_DIR overrides both with a single root, for tests and for anyone whose transcripts live somewhere unusual.
func Home ¶ added in v0.63.0
func Home() string
Home is Codex's state directory: `$CODEX_HOME` when it is set, otherwise `~/.codex`. Codex documents CODEX_HOME as the override for everything it keeps locally, transcripts included, so a user who set it has no `~/.codex/sessions` for us to find — reading only the default path missed every one of their sessions.
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.
func ReadThreadNames ¶ added in v0.59.0
ReadThreadNames reads every thread's name and first message. A database whose `threads` table lacks a column is a Codex release this was not built against, and is reported as an error rather than half-read.
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 ¶
func NewIngester(dirs []string, rec *rollup.Recorder, log *slog.Logger, every time.Duration) *Ingester
NewIngester builds an ingester over Codex's transcript roots (see Dirs). The first root is the one whose parent holds Codex's state index (§ Names).
type LimitWindow ¶ added in v0.63.0
type LimitWindow struct {
Minutes int
UsedPercent float64
// ResetsAt is unix seconds; 0 where Codex wrote null (CLI 0.4x did).
ResetsAt int64
}
LimitWindow is one window as Codex reports it.
type Limits ¶
type Limits struct {
// At is when Codex wrote the sample, not when Caprock read it — which can
// be days later for a transcript imported on first run.
At time.Time
Windows []LimitWindow
}
Limits is the plan-limit sample a transcript last recorded.
The windows are a list, not a fixed primary/secondary pair, because which window sits in which slot depends on the plan. Measured on the 72 of 176 transcripts that carry limits on the owner's machine: a `plus` account writes `primary` = 300 minutes and `secondary` = 10080; a `prolite` account writes `primary` = 10080 and `secondary` = null — it has no five-hour window at all. Reading `primary` as "the 5-hour window" would have put a weekly figure under a five-hour label.
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"`
// Repriced counts turns that were stored before the model could be read
// and have since been given one from their transcript.
Repriced int `json:"repriced,omitempty"`
LastPoll int64 `json:"last_poll_ms,omitempty"`
}
Stats is what the daemon reports about Codex ingest.
type ThreadName ¶ added in v0.59.0
ThreadName is Codex's name for one thread.
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.
func ListAll ¶ added in v0.63.0
func ListAll(dirs []string) ([]Transcript, error)
ListAll lists the transcripts under every root, newest first. A root that does not exist contributes nothing: most machines have never archived a Codex thread, so `archived_sessions/` is usually absent.
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
// TotalOnly marks a turn whose transcript reported a total with no
// breakdown, so `In` holds that total rather than a measured input figure.
// Its cost is therefore an upper bound: any part of it that was really a
// cached read would have been billed at a tenth.
TotalOnly bool
}
Turn is one assistant turn's token usage, already reduced to a delta.