Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/agentop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -787,12 +787,13 @@ ran before you installed Cortex, or that did not go through it.
agentop import # every supported agent
agentop import --agent claude-code --since 30d
agentop import --agent bob # Bob's tasks, from ~/.bob/db/bob.db
agentop import --agent codex # Codex's rollouts, from ~/.codex/sessions
agentop import --since 2026-10-01 --dry-run # count only, write nothing
```

| Flag | Meaning |
|---|---|
| `--agent` | `claude-code`, `bob`, `codex`, `opencode` or `pi`. Only `claude-code` and `bob` are supported now: the proxy answers the others with an error that names the supported agents. #1873 to #1875 add them. `bob` needs the `sqlite3` program on `PATH` |
| `--agent` | `claude-code`, `bob`, `codex`, `opencode` or `pi`. Only `claude-code`, `bob` and `codex` are supported now: the proxy answers the others with an error that names the supported agents. #1874 and #1875 add them. `bob` needs the `sqlite3` program on `PATH` |
| `--since` | skip a transcript file last changed before this time: `YYYY-MM-DD` (local midnight) or `<N>d` (N days ago) |
| `--dry-run` | count what an import would do, and write nothing |
| `--endpoint` | the session API, as for `agentop cost` |
Expand Down
3 changes: 2 additions & 1 deletion cmd/cortex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/rossoctl/cortex/core/session/importer"
bobimport "github.com/rossoctl/cortex/core/session/importer/bob"
claudeimport "github.com/rossoctl/cortex/core/session/importer/claude"
codeximport "github.com/rossoctl/cortex/core/session/importer/codex"
"github.com/rossoctl/cortex/core/sessionapi"
"github.com/rossoctl/cortex/core/spiffe"
"github.com/rossoctl/cortex/core/tlsbridge"
Expand Down Expand Up @@ -1280,7 +1281,7 @@ func main() {
sessionapi.WithLoopbackOnly(cfg.Listener.BindLoopbackOnly),
// POST /v1/import, only with the archive on and loopback-only; else no route.
sessionapi.WithImport(sessionapi.Import{Rates: pricingRegistry,
Sources: []importer.Source{claudeimport.New(""), bobimport.New("")}}),
Sources: []importer.Source{claudeimport.New(""), bobimport.New(""), codeximport.New("")}}),
}, ingestOpts...)...,
)
sessionLn := listeners.take("session-api")
Expand Down
361 changes: 361 additions & 0 deletions core/session/importer/codex/codex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,361 @@
// Package codex is the importer source for Codex. It reads the rollout files under
// <codex home>/sessions and makes one importer.Session from the root rollout of a session
// and the rollouts of its subagents. It does not write a file.
package codex

import (
"bufio"
"cmp"
"encoding/json"
"errors"
"io"
"io/fs"
"iter"
"log/slog"
"os"
"path/filepath"
"slices"
"strings"
"time"

"github.com/klauspost/compress/zstd"

"github.com/rossoctl/cortex/core/model"
"github.com/rossoctl/cortex/core/session/importer"
)

const agentName = "codex"

type source struct{ home string }

// New returns the source. home "" resolves $CODEX_HOME, else ~/.codex, when Sessions runs.
func New(home string) importer.Source { return source{home: home} }

func (source) Agent() string { return agentName }

// PricingHost is the OpenAI host: the bundled table prices the GPT models under every host,
// and a rollout does not say if Codex used an API key or a ChatGPT login.
func (source) PricingHost() string { return "api.openai.com" }

// rollout is one rollout file, from its session_meta line.
type rollout struct {
path, thread, session string
forkedFrom string // the thread id this rollout forks, or ""
mod time.Time
size int64
}

// openRollout opens a rollout file. The listing and the parse both use it, so a test can
// make an open fail.
var openRollout = openFile

// openFile opens path, and reads it through zstd when its name ends with .zst.
func openFile(path string) (io.ReadCloser, error) {
f, err := os.Open(path)
if err != nil || !strings.HasSuffix(path, ".zst") {
return f, err
}
d, err := zstd.NewReader(f)
if err != nil {
f.Close()
return nil, err
}
return zstdFile{d, f}, nil
}

type zstdFile struct {
*zstd.Decoder
f *os.File
}

func (z zstdFile) Close() error {
z.Decoder.Close()
return z.f.Close()
}

func (s source) dir() (string, error) {
if s.home != "" {
return s.home, nil
}
if h := os.Getenv("CODEX_HOME"); h != "" {
return h, nil
}
h, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(h, ".codex"), nil
}

func (s source) Sessions(since time.Time, skip func(id string, updated time.Time) bool, progress func(done, total int)) iter.Seq2[importer.Session, error] {
return func(yield func(importer.Session, error) bool) {
dir, err := s.dir()
if err != nil {
yield(importer.Session{}, err)
return
}
files, unopened := list(filepath.Join(dir, "sessions"))
groups := group(files)
// A fork's parent can be archived. The source does not import an archived rollout, but
// the ledger can already hold its turns.
threads := map[string]rollout{}
archived, _ := list(filepath.Join(dir, "archived_sessions"))
for _, f := range archived {
threads[f.thread] = f
}
for _, files := range groups {
for _, f := range files {
threads[f.thread] = f
}
}
sids := make([]string, 0, len(groups))
newest := map[string]time.Time{}
for id, files := range groups {
sids = append(sids, id)
for _, f := range files {
if f.mod.After(newest[id]) {
newest[id] = f.mod
}
}
}
slices.SortFunc(sids, func(a, b string) int {
return cmp.Or(newest[b].Compare(newest[a]), strings.Compare(a, b))
})
total := len(unopened) + len(sids)
// A rollout that cannot be opened names no session, so it is counted on its own.
for i, mod := range unopened {
progress(i, total)
if (since.IsZero() || !mod.Before(since)) && !yield(importer.Session{Unreadable: true}, nil) {
return
}
}
home, _ := os.UserHomeDir()
for i, id := range sids {
progress(len(unopened)+i, total)
// Codex writes each line when it happens, so no line is newer than its file.
if !since.IsZero() && newest[id].Before(since) {
continue
}
if skip(id, newest[id]) {
continue
}
// The mtime is always after the newest event, so only this time lets Rule 2 skip an
// unchanged session before the slow parse.
if at, ok := newestAt(groups[id]); ok && skip(id, at) {
continue
}
if !yield(readSession(id, groups[id], threads, home), nil) {
return
}
}
progress(total, total)
}
}

// list finds the rollout files under dir at all depths and reads the first line of each.
// unopened is the mtime of each file that cannot be opened or read. A missing dir gives none.
func list(dir string) (out []rollout, unopened []time.Time) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — filepath.WalkDir does not follow symlinks: when <codex home>/sessions is itself a symlink to a directory, the import silently finds no sessions and gives no warning, and symlinked subdirectories/rollout files are skipped by the d.Type().IsRegular() check. The claude source uses os.ReadDir, which follows a symlinked root. Suggested fix: resolve the root once with filepath.EvalSymlinks before the walk.

// An error in one directory must not stop the others, so the walk ignores it.
_ = filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
if d != nil && d.IsDir() && path != dir {
return fs.SkipDir
}
return nil
}
name := d.Name()
if !d.Type().IsRegular() || !strings.HasPrefix(name, "rollout-") ||
!(strings.HasSuffix(name, ".jsonl") || strings.HasSuffix(name, ".jsonl.zst")) {
return nil
}
fi, err := d.Info()
if err != nil {
return nil
}
r, err := readMeta(path)
if err != nil {
slog.Warn("importer: cannot list a Codex rollout", "path", path, "error", err)
if !errors.Is(err, errNoMeta) && !errors.Is(err, io.EOF) {
unopened = append(unopened, fi.ModTime())
}
return nil
}
r.path, r.mod, r.size = path, fi.ModTime(), fi.Size()
out = append(out, r)
return nil
})
return out, unopened
}

// errNoMeta is the error of a rollout whose first line is not a session_meta line.
var errNoMeta = errors.New("the first line is not a session_meta line")

// readMeta gives the thread id, the session id and the forked thread id from the first line
// of a rollout.
func readMeta(path string) (rollout, error) {
f, err := openRollout(path)
if err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit — progress(total, total) is never reached when the consumer stops iterating early or on an early error return. Harmless (progress is advisory), but the done==total test assertion only holds for full reads.

return rollout{}, err
}
defer f.Close()
raw, err := bufio.NewReader(f).ReadBytes('\n')
if err != nil && len(raw) == 0 {
return rollout{}, err
}
var l struct {
Type string `json:"type"`
Payload struct {
ID string `json:"id"`
SessionID string `json:"session_id"`
ForkedFromID string `json:"forked_from_id"`
} `json:"payload"`
}
if json.Unmarshal(raw, &l) != nil || l.Type != "session_meta" || l.Payload.ID == "" {
return rollout{}, errNoMeta
}
// Codex sends session_id as the Session-Id header; when it is absent it reads it as id.
return rollout{thread: l.Payload.ID, session: cmp.Or(l.Payload.SessionID, l.Payload.ID), forkedFrom: l.Payload.ForkedFromID}, nil
}

// newestAt reads each rollout of a session fast and gives a time that is not before the At of
// any event that the parse makes: the newest token record that closes an open turn, or the
// newest model output. ok is false when a file cannot be read, so that the parse reports it.
// shortcut: it trusts that the lines of a rollout are in time order, as Codex writes them.
func newestAt(files []rollout) (newest time.Time, ok bool) {
for _, f := range files {
t, err := newestInFile(f.path)
if err != nil {
return time.Time{}, false
}
if t.After(newest) {
newest = t
}
}
return newest, true
}

func newestInFile(path string) (time.Time, error) {
f, err := openRollout(path)
if err != nil {
return time.Time{}, err
}
defer f.Close()
var newest time.Time
open := false // a model output came after the last close; see parser.file
r := bufio.NewReader(f)
for {
raw, rerr := r.ReadBytes('\n')
var l line
var it struct {
Type string `json:"type"`
Role string `json:"role"`
Info json.RawMessage `json:"info"`
}
if json.Unmarshal(raw, &l) == nil && json.Unmarshal(l.Payload, &it) == nil {
switch {
case l.Type == "response_item" && (outputTypes[it.Type] || (it.Type == "message" && it.Role == "assistant")):
open, newest = true, maxTime(newest, l.Timestamp)
case l.Type == "compacted",
// The parse skips an item that it refuses, and the turn stays open.
l.Type == "response_item" && open && !toolOutputTypes[it.Type] && validItem(l.Payload):
open = false
case open && (l.Type == "token_usage_record" ||

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — newestInFile closes an open turn on l.Type == "token_usage_record" regardless of payload validity, while the parser (turns.go) closes only if json.Unmarshal of the payload succeeds; same divergence for event_msg/token_count. The direction is safe (newestAt can only exceed the parsed Updated, making the skip more aggressive), but the sameNewest invariant the idempotency test relies on is only asserted on well-formed fixtures — a malformed payload would silently break the equality. Suggested fix: a comment noting the divergence is deliberate, or a fixture with a malformed payload.

(l.Type == "event_msg" && it.Type == "token_count" && len(it.Info) > 0 && string(it.Info) != "null")):
open, newest = false, maxTime(newest, l.Timestamp)
}
}
if rerr != nil {
if errors.Is(rerr, io.EOF) {
return newest, nil
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — newestInFile re-implements a reduced copy of the parse's turn open/close state machine (which outputs open a turn, which lines close it) alongside turns.go's version. They share the outputTypes/toolOutputTypes maps today, but the compacted and close conditions here are hand-mirrored, so a future rule change in parser.file (say, a new closing event type) can silently desync the fast-path skip from the real parse — the symptom would be sessions skipped or re-imported more than intended, not a crash. Consider a comment cross-referencing parser.file as the source of truth for these rules (or a shared table), so the coupling is explicit. Not blocking.

return time.Time{}, rerr
}
}
}

func maxTime(a, b time.Time) time.Time {
if b.After(a) {
return b
}
return a
}

// group puts the rollouts by session id. Of two rollouts with one thread id the newer wins:
// a reverted thread, or a .jsonl and its .jsonl.zst during compression.
func group(files []rollout) map[string][]rollout {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — In group(), of two rollouts with the same thread id the newer mtime wins — during compression that is the .jsonl.zst. If Codex writes the .zst in place under its final name, a mid-compression import picks the incomplete .zst, readSession gets a zstd error on the root, and the session is marked Unreadable even though the complete .jsonl is still on disk. Suggested fix: fall back to the other file of the thread when the winner fails to read, or prefer .jsonl while both exist.

byThread := map[string]rollout{}
for _, f := range files {
if prev, dup := byThread[f.thread]; dup && !f.mod.After(prev.mod) {
continue
}
byThread[f.thread] = f
}
out := map[string][]rollout{}
for _, f := range byThread {
out[f.session] = append(out[f.session], f)
}
for _, rs := range out {
// The root first, so that on a tie of At its events stay before the subagents' events.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit — the comment says "on a tie of At its events stay before the subagents' events", but the comparator ties on isChild then the path; no At is involved.

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

slices.SortFunc(rs, func(a, b rollout) int {
return cmp.Or(cmp.Compare(isChild(a), isChild(b)), strings.Compare(a.path, b.path))
})
}
return out
}

func isChild(f rollout) int {
if f.thread == f.session {
return 0
}
return 1
}

// readSession parses the rollouts of one session. threads gives a fork's parent rollout.
func readSession(id string, files []rollout, threads map[string]rollout, home string) importer.Session {
p := parser{home: home, projects: map[string]string{}, parents: map[string][]usage{}}
s := importer.Session{ID: id}
for _, f := range files {
subagent := ""
if f.thread != id {
subagent = f.thread
}
r, err := p.file(f.path, f.thread, subagent, p.copied(f, threads), false)
if err != nil {
if subagent == "" {
slog.Warn("importer: cannot read a Codex rollout", "path", f.path, "error", err)
return importer.Session{ID: id, Unreadable: true}
}
s.Skipped++
continue
}
s.Events = append(s.Events, r.events...)
s.Skipped += r.skipped
s.SizeHint += f.size
}
// Stable: on a tie the root stays first, and a request stays before its response.
slices.SortStableFunc(s.Events, func(a, b model.SessionEvent) int { return a.At.Compare(b.At) })
for _, e := range s.Events {
if e.At.After(s.Updated) {
s.Updated = e.At
}
}
return s
}

// copied gives the usages of the turns of f's parent, which a fork's rollout starts with a
// copy of. It gives none when f is not a fork or its parent rollout cannot be read.
func (p *parser) copied(f rollout, threads map[string]rollout) []usage {
parent, ok := threads[f.forkedFrom]
if f.forkedFrom == "" || !ok {
return nil
}
if u, done := p.parents[parent.path]; done {
return u
}
r, err := p.file(parent.path, parent.thread, "", nil, true)
if err != nil {
r.usages = nil
}
p.parents[parent.path] = r.usages

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion — a failed parent read is cached as an empty result, so all forks of that parent in this session lose copy-matching permanently. If the failure is transient (a .zst file mid-compression), the fork's copied turns are emitted as real turns and their usage is counted twice. Suggested fix: only cache the successful case, or log at warn when a fork's parent cannot be read so the overcount is visible in the import log.

Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

return r.usages
}
Loading
Loading