codex

package
v0.59.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Agent = "codex"

Agent is the value written to sessions.agent for Codex sessions.

View Source
const EnvDir = "CAPROCK_CODEX_DIR"

EnvDir overrides the transcript root.

Variables

View Source
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.

func ReadThreadNames added in v0.59.0

func ReadThreadNames(ctx context.Context, path string) (map[string]ThreadName, error)

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.

func StateDB added in v0.59.0

func StateDB(sessionsDir string) string

StateDB returns the newest `state_<N>.sqlite` in the Codex home that holds the sessions directory, or "" when there is none. The number is Codex's own schema generation; the newest one is the one it writes.

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(dir string, rec *rollup.Recorder, log *slog.Logger, every time.Duration) *Ingester

NewIngester builds an ingester over a Codex transcript directory.

func (*Ingester) Run

func (in *Ingester) Run(ctx context.Context) error

Run polls until the context is cancelled. It imports once immediately so a fresh daemon shows history straight away rather than after the first tick.

func (*Ingester) Stats

func (in *Ingester) Stats() Stats

Stats returns a snapshot of what has been imported.

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

func Parse(r io.Reader, path string) (*Session, error)

Parse reads a rollout transcript from r. path is used only for the returned Session's Path field.

func ParseFile

func ParseFile(path string) (*Session, error)

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

type ThreadName struct {
	Name         string
	FirstMessage string
}

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

type Transcript struct {
	Path     string
	Modified time.Time
	Size     int64
}

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
	// 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL