Files
magpie/internal/sessions/calls.go
T
yetone 8a1b5916f3 sessions, usage, gui: one read of every call at a time, the Codex effort built once and kept, and the Sessions page asks for an overview once, so the app no longer grows to 85 GB (#2278)
On a long history (the reporter's: 68k call files in calls-v15, 415 MB;
42 MB usage.jsonl; 75k calls in 7 days), the app's main process grew to
85 GB in about 90 minutes and the gateway froze. `magpie serve` alone held
83-153 MB for 20 minutes (the reporter's follow-up), so the growth is in
the app's Sessions path:
/api/sessions/overview -> usage.CodexModelEffort -> LedgerOfAt ->
sessions.Calls(all history).

Why it grew:
- CodexModelEffort's cache key changes on every gateway write, and it
  never kept an answer when usage.jsonl was missing or grew during the
  build. So every /api/sessions, overview, /one and /manage ask with a Codex
  session rebuilt the all-history ledger, gob-decoding every shard (only
  64 stay in memory).
- Nothing joined concurrent asks.
- The page's 15s reload asked for an overview again while the last one
  was still under way, so slow asks piled up.

What changes:
- sessions.Calls and the new AgentCalls go through sharedCalls. An ask
  while the same read is under way waits for it and gets a copy, and
  reads run one at a time.
- AgentCalls(since, "codex") reads Codex's files only. pruneCalls still
  sees every file.
- CodexModelEffort is built from Codex's calls plus the gateway's records.
  Only those name a Codex actor, and TestCodexModelEffortIsTheWholeLedgers
  checks the answer equals the whole ledger's on a mixed Claude + Codex +
  gateway fixture.
  - One build per period at a time. An ask during a build gets the last
    answer for the same home (root), or waits if there is none.
  - The answer is kept when usage.jsonl is missing or grows during the
    build.
  - At most 8 periods are kept.
- page.js: sessOverview shares one promise per query, so the filter's ask
  and the 15s reload's ask for the same overview send one request.
- docs/subsystems/gateway-sessions.md says this.

Reproduced (temporary test, not committed): a sandbox HOME with 62k
Claude Code session files, 5.9k Codex rollouts and 100k gateway records.
Sessions routes in-process, 4 rounds x 6 concurrent asks
(overview?days=0 and /api/sessions), a usage record appended every 300ms:
  before (2b0037f41): 7m39s total, slowest ask 2m01s; 66264 MB allocated
    (2761 MB per ask); peak heap in use 4620 MB, peak Go sys 6274 MB,
    peak RSS 5325 MB
  after:              20.4s total, slowest ask 7.1s; 20728 MB allocated
    (863 MB per ask); peak heap in use 1168 MB, peak Go sys 1487 MB,
    peak RSS 1415 MB
Cold warm-up (index plus every shard) was 21m before and 18.5s after on the
same home. The after run reused the shards on disk, so it is not a
comparable number.

Tests, each red on the old code:
- TestConcurrentCallsShareOneRead: 8 reads, not 1. It also goes red when
  the join is broken by hand.
- TestCodexModelEffortConcurrentAsksBuildOnce: 8 builds.
- TestCodexModelEffortAnswersLastWhileBuilding: 3 builds.
- TestCodexModelEffortKeptWithoutLogAndAcrossAWrite: 2 builds.
- sessions-overview-once.test.cjs: 2 overview requests, not 1. That is
  chromium and webkit; en, zh, zh-TW and ja at 1100/900px, de at 420px.
  It was red with the old page.js served.
TestAgentCallsReadsOneAgent and TestCodexModelEffortIsTheWholeLedgers pass
on both versions.

Run:
- gofmt -l: clean.
- go vet -tags nogui for darwin, linux and windows: sessions, usage,
  gui. Before that, the whole tree for linux and windows.
- go test -race on ./internal/sessions/ and ./internal/usage/.
- go test of internal/sessions, internal/usage and internal/gui at the
  rebased head (on 95e7c6bcc): green. gui was red once, in a run whose
  output was cut, at load ~100; it was green on two reruns.
- Full suite: SUITE_LINE
- GUI: the 13 files that ask for sessions/overview, at the rebased head,
  in chromium and webkit: 198/198. Before the rebase, those plus every
  usage*.test.cjs,
  in chromium and webkit: 749 pass. The 24 failures are usage-fold
  (#2163) and usage-agents-hide (#2276). The same 24 fail with the old
  page.js on 2b0037f41, and both are filed.

Not tried: the real app on the reporter's data; an hour-long run.
2026-10-15 23:08:00 +08:00

1081 lines
35 KiB
Go

package sessions
import (
"bytes"
"cmp"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"maps"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/desktopdir"
)
// Call is one model call as an agent's own session file records it.
type Call struct {
ResponseID string `json:"response_id,omitempty"`
Time time.Time `json:"t"`
Agent string `json:"agent"` // claude (Claude Code's CLI), claude-desktop (Desktop's Code tab and Cowork), codex
Session string `json:"session"` // the id the agent sends the gateway as its session header
ThreadID string `json:"thread_id,omitempty"`
Model string `json:"model"` // the model the file names: for Claude, the one the vendor answered
// Requested: the model Claude Code was running as (its identity note) when it made
// the call, as it was asked for: claude-opus-5[1m] for claude-opus-5 with the long context
Requested string `json:"requested,omitempty"`
Tokens
Reasoning int `json:"reasoning,omitempty"` // Codex: inside Output already
Effort string `json:"effort,omitempty"` // the thinking effort: Codex's, of the turn; Claude's, of the line
RequestID string `json:"request_id,omitempty"` // Claude's
// Codex's session metadata identifies its configured provider and creator.
// These identify the historical session, not whichever account is signed in now.
Upstream string `json:"upstream,omitempty"`
AccountID string `json:"account_id,omitempty"`
UserID string `json:"user_id,omitempty"`
Error string `json:"error,omitempty"` // Claude: the API error's kind (rate_limit, server_error …); such a call has no tokens
ErrorText string `json:"error_text,omitempty"` // its message, the first 400 characters
// Codex: the turn's own figures, set only when the turn made just this call
TTFT int64 `json:"ttft_ms,omitempty"`
// Compaction is Codex's explicit marker that this response compacted the context.
Compaction bool `json:"compaction,omitempty"`
// Millis: how long the call took. Codex's, of a turn that made this call alone, is what
// it says; the rest are told from the file's times, the call's last line less the line
// that asked for it (a prompt, a tool's result), and so a little more or less than it was
Millis int64 `json:"ms,omitempty"`
// Compact: how long a compaction of the context kept the agent waiting, in ms.
// Codex's is on the compaction's own call (its Millis); Claude Code's is the
// durationMs its compact_boundary line says, on the first call after it
Compact int64 `json:"compact_ms,omitempty"`
Cwd string `json:"cwd,omitempty"`
// Where the call is in its file, for reading what was said in it (ContentOf): the file,
// from where what it was asked began — after the reply before it — to where it ended,
// and for Claude Code the id of its message
File string `json:"file,omitempty"`
From int64 `json:"from,omitempty"`
To int64 `json:"to,omitempty"`
Msg string `json:"msg,omitempty"`
}
// callFile is what one file's read has come to: the calls found in it, and
// what it takes to read on from off.
type callFile struct {
Claude *claudeUsageState
ContentHash string
dirty map[int]bool
Size int64
Mod int64 // unix nanoseconds
Off int64 // after the last whole line read
Head string // hash of the prefix, to tell one replaced from one grown
HeadSize int
Path string
Calls []Call
Agent string
// where the line being handled begins and ends, and where the last reply's did
At, End, AsstEnd int64
Strs map[string]string
// Claude Code: the session by the file's name, and each message's place in
// calls, as its blocks are lines of their own repeating its usage
Session string
Msgs map[string]int
// and the model it runs as, when the last user line and the last reply
// line were, and where each message's asking began: a call took from there
// to its last line
Requested string
LastUser, LastAsst time.Time
Began map[string]time.Time
// how long a compaction its file told of took, for the call after it
CompactMs int64
// Codex
CX *cxRun
}
// cxRun is where a rollout's read stands: what its lines have said so far.
type cxRun struct {
Meta bool // session_meta seen
Session, ThreadID, Cwd, Model, Effort, Turn string
Upstream, AccountID, UserID string
Usage *codexUsageState
Contexts map[string]cxContext
Turns map[string]cxOpen
LastIn, LastCall time.Time // the last thing the model was given, and the last call's end
// Answering is set from a call's first output item until its usage line:
// AskedAt is what the model had last been given when the call began to
// answer, Answered its last output item. An older rollout writes a call's
// usage after its own tool's output, so LastIn alone would start the call
// at that output, and the usage line's time would end it after the tool ran.
Answering bool
AskedAt, Answered time.Time
AskedTurn string
LastEnd int64 // where the last call's line ended
Timings []cxCallTiming
LastIndex int
}
// cxOpen is a turn still open: how many calls it has made, and where the first is.
type cxOpen struct {
N, First int
Duration, FirstToken int64
Complete bool
}
type cxCallTiming struct {
Input time.Time
Previous int
End time.Time // the call's last output item when its tool's output came before its usage line; else zero, and the line's time ends it
}
type cxContext struct {
Model, Effort, Cwd, Upstream string
LastIn time.Time
}
type cxCallUsage = cxUsage
// callDesktopDirs are Claude Desktop's data folders. Tests swap it.
var callDesktopDirs = desktopDataDirs
// DesktopDataDirs are the folders whose session metadata belongs to this index.
func DesktopDataDirs() []string { return callDesktopDirs() }
// Calls reads per-file request shards, separately from the session summaries.
// Only changed files are scanned, and only a bounded number of parses stay in
// memory. Conversation text is read by ContentOf when a detail is opened.
//
// Over a long history a read decodes every shard (#2278: 68k of them, 415
// MB), so callers asking at once share one read of the same files, and only
// one read of every file runs at a time: the Sessions page's requests
// arriving together each held a whole read's calls, and the app grew to
// 85 GB.
func Calls(since time.Time) []Call { return sharedCalls(since, "") }
// AgentCalls is Calls of one agent's session files only: Codex's model and
// effort per thread need Codex's calls, not a read of every agent's.
func AgentCalls(since time.Time, agent string) []Call { return sharedCalls(since, agent) }
type callsKey struct {
since int64
zero bool
agent string
}
type callsFlight struct {
done chan struct{}
calls []Call
waiters int
}
var (
callsFlights = struct {
sync.Mutex
on map[callsKey]*callsFlight
}{on: map[callsKey]*callsFlight{}}
// callsReading holds one read of every file at a time.
callsReading sync.Mutex
// readingCalls, in tests, is told each time Calls reads the files.
readingCalls func()
)
func sharedCalls(since time.Time, agent string) []Call {
key := callsKey{since.UnixNano(), since.IsZero(), agent}
callsFlights.Lock()
if fl := callsFlights.on[key]; fl != nil {
fl.waiters++
callsFlights.Unlock()
<-fl.done
return slices.Clone(fl.calls)
}
fl := &callsFlight{done: make(chan struct{})}
callsFlights.on[key] = fl
callsFlights.Unlock()
// Whatever happens to the read, the flight ends and its waiters go on.
defer func() {
callsFlights.Lock()
if callsFlights.on[key] == fl {
delete(callsFlights.on, key)
}
callsFlights.Unlock()
close(fl.done)
}()
func() {
callsReading.Lock()
defer callsReading.Unlock()
if readingCalls != nil {
readingCalls()
}
fl.calls = callsFor(since, "", agent)
}()
// none joins once the flight is out of the map, so the count read with
// that is every waiter
callsFlights.Lock()
if callsFlights.on[key] == fl {
delete(callsFlights.on, key)
}
shared := fl.waiters > 0
callsFlights.Unlock()
if shared { // the waiters copy fl.calls, so the caller's own is another
return slices.Clone(fl.calls)
}
return fl.calls
}
func callsFor(since time.Time, session, agent string) []Call {
all := callSources()
pruneCalls(all)
files := all
if agent != "" {
files = slices.DeleteFunc(slices.Clone(all), func(f file) bool { return f.agent != agent })
}
// Earlier files own messages copied into a resumed Claude session.
sort.Slice(files, func(i, j int) bool {
if !files[i].mod.Equal(files[j].mod) {
return files[i].mod.Before(files[j].mod)
}
return files[i].path < files[j].path
})
seen := map[string]bool{}
capacity := 0
callsMu.Lock()
for _, f := range files {
if since.IsZero() || !f.mod.Before(since) {
capacity += callCounts[f.path]
}
}
callsMu.Unlock()
if session != "" {
capacity = 0
}
out := make([]Call, 0, capacity)
for _, f := range files {
if !since.IsZero() && f.mod.Before(since) {
continue
}
st := readCalls(f)
for _, c := range st.Calls {
if c.Msg != "" {
if seen[c.Msg] {
continue
}
seen[c.Msg] = true
}
if (since.IsZero() || !c.Time.Before(since)) && (session == "" || c.Session == session) {
out = append(out, c)
}
}
}
sort.SliceStable(out, func(i, j int) bool {
if !out[i].Time.Equal(out[j].Time) {
return out[i].Time.After(out[j].Time)
}
if out[i].File != out[j].File {
return out[i].File > out[j].File
}
return out[i].To > out[j].To
})
return out
}
// callFiles are the session files that hold calls: Claude Code's, Cowork's
// and Desktop's own, and Codex's.
func callFiles() []file {
var out []file
seen := map[string]bool{}
add := func(fs []file) {
for _, f := range fs {
if !seen[f.path] {
seen[f.path] = true
out = append(out, f)
}
}
}
add(ccFiles("claude", ClaudeDir()))
for _, d := range callDesktopDirs() {
// Cowork keeps a Claude Code folder of its own for each session
homes, _ := SessionGlob(filepath.Join(d, "local-agent-mode-sessions", "*", "*", "local_*", ".claude"))
for _, h := range homes {
add(ccFiles("claude-desktop", h))
}
}
add(codexFiles())
add(wslFiles("claude", "codex"))
return out
}
// callSources are callFiles and the agents whose calls are read whole
// rather than line by line: OpenCode's (#680) and ZCode's, whose calls are
// rows of their database or their JSON files; DeepSeek Harness's, whose
// session file is packed in frames and so cannot be read on from the middle
// of one; and WorkBuddy's, whose usage lines repeat a reply's id as it goes
// on, as CodeBuddy Code's do (#1779); and MiniMax Code's, whose messages
// file a later generation of its history may replace (#1605); and Kimi
// Code's, whose forks copy their parent's records (#1607). Every agent here
// needs an entry in wholeCallReaders.
func callSources() []file {
out := callFiles()
out = append(out, openCodeCallFiles()...)
out = append(out, zcodeCallFiles()...)
out = append(out, dshCallFiles()...)
out = append(out, minimaxCallFiles()...)
out = append(out, kimiCallFiles()...)
out = append(out, wslFiles("kimi")...)
out = append(out, piCallFiles()...)
out = append(out, codebuddyCallFiles()...)
out = append(out, agyCallFiles()...) // #2097
return append(out, workbuddyCallFiles()...)
}
// desktopDataDirs are Claude Desktop's folders on this computer that
// hold sessions: its own (%APPDATA%\Claude on Windows, or the MSIX
// package's, and the real ones beside a package's), the Claude folder
// magpie writes and Claude-3p, as desktopdir finds them.
func desktopDataDirs() []string { return desktopdir.Everywhere() }
// headLen is how much of a file's start is kept to know it again.
const headLen = 256
// headOf is the start of a file.
func headOf(path string) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
b := make([]byte, headLen)
n, _ := io.ReadFull(f, b)
return string(b[:n])
}
// prepareCalls clones the continuation so a published index is never changed.
func prepareCalls(f file, old *callFile) *callFile {
// The disk shard stores final calls, not a second copy of every usage
// observation. Rebuild continuation only when a cold source grows.
if f.agent == "codex" && old != nil && (old.CX == nil || old.CX.Usage == nil) {
old = nil
}
// Disk shards keep final calls, not message block/tool maps. A changed
// Claude source reconstructs its transient revision index after restart.
if (f.agent == "claude" || f.agent == "claude-desktop") && old != nil && old.Claude == nil {
old = nil
}
head := headOf(f.path)
var st *callFile
if old != nil && !packed(f.path) && f.size >= old.Size && old.Off <= f.size && sameHead(head, old.Head, old.HeadSize) && old.ContentHash != "" && prefixHash(f.path, old.Size) == old.ContentHash {
st = old.clone()
} else {
st = &callFile{Agent: f.agent}
if f.agent == "codex" {
st.CX = &cxRun{}
if m := rolloutName.FindStringSubmatch(filepath.Base(f.path)); m != nil {
st.CX.Session, st.CX.ThreadID = m[1], m[1]
}
} else {
st.Session = sessionOfPath(f.path)
st.Msgs = map[string]int{}
}
}
st.dirty = map[int]bool{}
st.Head, st.HeadSize, st.Path = hashHead(head), len(head), f.path
return st
}
func (st *callFile) clone() *callFile {
c := *st
c.Calls = append([]Call(nil), st.Calls...)
c.Claude = st.Claude.clone()
c.Msgs, c.Began, c.Strs = maps.Clone(st.Msgs), maps.Clone(st.Began), maps.Clone(st.Strs)
if st.CX != nil {
r := *st.CX
r.Turns = maps.Clone(r.Turns)
r.Usage = r.Usage.clone()
r.Contexts = maps.Clone(r.Contexts)
r.Timings = append([]cxCallTiming(nil), r.Timings...)
c.CX = &r
}
return &c
}
// cxAnswerItem is a rollout line that is the model's own output: a call has
// begun to answer. Its head is enough.
func cxAnswerItem(b []byte) bool {
h := b[:min(len(b), 1024)]
if typeAfter(h, cxType) != "response_item" {
return false
}
switch typeAfter(h, cxPayload) {
case "reasoning", "function_call", "custom_tool_call", "web_search_call", "local_shell_call", "tool_search_call", "image_generation_call":
return true
case "message":
return typeAfter(h, cxRole) == "assistant"
}
return false
}
// Codex tool results need only their timestamp. Compaction metadata must reach
// the usage reader even when its replacement history contains user messages.
func callHead(st *callFile, b []byte) bool {
if st.Agent != "codex" {
return true
}
if cxAnswerItem(b) {
codexCallLine(st, b)
return false
}
compacted := typeAfter(b[:min(len(b), 1024)], cxType) == "compacted"
if !compacted && (bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) || bytes.Contains(b, cxToolOut) || bytes.Contains(b, cxCustomOut)) {
codexCallLine(st, b)
return false
}
return bytes.Contains(b, cxMeta) || bytes.Contains(b, cxTurn) || bytes.Contains(b, cxRecord) || bytes.Contains(b, cxCompacted) || bytes.Contains(b, cxCount) || bytes.Contains(b, cxSettings) || bytes.Contains(b, cxStarted) || bytes.Contains(b, cxDone)
}
// sessionOfPath is the session a Claude Code file belongs to by its name:
// <id>.jsonl, <id>/subagents/<agent>.jsonl, or a workflow's
// <id>/subagents/workflows/<run>/<agent>.jsonl.
func sessionOfPath(p string) string {
d := filepath.Dir(p)
if w := filepath.Dir(d); filepath.Base(w) == "workflows" && filepath.Base(filepath.Dir(w)) == "subagents" {
return filepath.Base(filepath.Dir(filepath.Dir(w)))
}
if filepath.Base(d) == "subagents" {
return filepath.Base(filepath.Dir(d))
}
return strings.TrimSuffix(filepath.Base(p), ".jsonl")
}
// SubagentOf is the subagent a Claude Code file is the conversation of, by
// its name: <agent> of <id>/subagents/agent-<agent>.jsonl or a workflow's
// <id>/subagents/workflows/<run>/agent-<agent>.jsonl — the agentId its
// lines carry, and the x-claude-code-agent-id Claude Code sends the
// gateway for it. "" for a session's own file, and any other agent's.
func SubagentOf(p string) string {
d := filepath.Dir(p)
if filepath.Base(d) != "subagents" && (filepath.Base(filepath.Dir(d)) != "workflows" || filepath.Base(filepath.Dir(filepath.Dir(d))) != "subagents") {
return ""
}
name := strings.TrimSuffix(filepath.Base(p), ".jsonl")
if name == filepath.Base(p) || !strings.HasPrefix(name, "agent-") {
return ""
}
return strings.TrimPrefix(name, "agent-")
}
// SubagentParent is the subagent that started the one whose Claude Code
// file p is: the parentAgentId of its agent-<agent>.meta.json beside it,
// which Claude Code writes for a subagent a subagent started (spawnDepth
// 2 and on), the x-claude-code-parent-agent-id it sends the gateway. ""
// for one the conversation itself started, and for a file that is no
// subagent's. A meta file once read is kept; one not read is read again.
func SubagentParent(p string) string {
if SubagentOf(p) == "" {
return ""
}
return ccMeta(p).ParentAgentID
}
// str is s kept once per file, as the calls of a file repeat a few.
func (st *callFile) str(s string) string {
if s == "" {
return ""
}
if v, ok := st.Strs[s]; ok {
return v
}
if st.Strs == nil {
st.Strs = map[string]string{}
}
st.Strs[s] = s
return s
}
// ccStr is a string field that reads as "" when a file holds something else there.
type ccStr string
func (s *ccStr) UnmarshalJSON(b []byte) error {
var v string
if json.Unmarshal(b, &v) == nil {
*s = ccStr(v)
}
return nil
}
// ccCall is an assistant line of Claude Code's, less its content.
type ccCall struct {
UUID string `json:"uuid"`
Type string `json:"type"`
Time string `json:"timestamp"`
SessionID ccStr `json:"sessionId"`
RequestID ccStr `json:"requestId"`
Cwd ccStr `json:"cwd"`
Entrypoint ccStr `json:"entrypoint"`
APIError bool `json:"isApiErrorMessage"`
Error ccStr `json:"error"`
// the session's thinking effort, and the turn's own when it asked for
// another (null when it did not)
Effort ccStr `json:"effort"`
PerTurn ccStr `json:"perTurnEffort"`
Message struct {
ID string `json:"id"`
Model string `json:"model"`
Usage *struct {
Input int `json:"input_tokens"`
Output int `json:"output_tokens"`
CacheRead int `json:"cache_read_input_tokens"`
CacheWrite int `json:"cache_creation_input_tokens"`
Creation *ccCacheCreation `json:"cache_creation"`
} `json:"usage"`
} `json:"message"`
}
// claudeCallLine notes the call an assistant line is (a block of) in the
// file: the latest of a message's lines stands for it, as it has the final
// usage. A synthetic line is a call only as an API error.
func claudeCallLine(st *callFile, b []byte) {
if !bytes.Contains(b, ccAssistant) {
switch {
case bytes.Contains(b, ccIdentity):
// a note of the model it runs as, on the first turn and when it is changed
var n struct {
Attachment struct {
Identity struct {
ModelID string `json:"modelId"`
} `json:"identity"`
} `json:"attachment"`
}
if json.Unmarshal(b, &n) == nil && n.Attachment.Identity.ModelID != "" {
st.Requested = st.str(n.Attachment.Identity.ModelID)
}
case bytes.Contains(b, ccCompact):
// {"type":"system","subtype":"compact_boundary","compactMetadata":{"durationMs":48913,…}}
var n struct {
Meta struct {
DurationMs int64 `json:"durationMs"`
} `json:"compactMetadata"`
}
if json.Unmarshal(b, &n) == nil && n.Meta.DurationMs > 0 {
st.CompactMs = n.Meta.DurationMs
}
case bytes.Contains(b, ccUserLine):
// what a reply is asked by: a prompt, or a tool's result
if at := tsAt(b, true); !at.IsZero() {
st.LastUser = at
}
}
return
}
var l ccCall
if json.Unmarshal(b, &l) != nil || l.Type != "assistant" {
return
}
at, _ := time.Parse(time.RFC3339Nano, l.Time)
model := l.Message.Model
if at.IsZero() || model == "<synthetic>" && !l.APIError {
return
}
c := Call{Time: at, Agent: st.Agent, Session: st.str(string(l.SessionID)), Cwd: st.str(string(l.Cwd)), RequestID: string(l.RequestID)}
c.Requested = st.Requested
m := ccUsageState(&st.Claude).message(l.Message.ID, string(l.RequestID))
defer st.Claude.finish(m)
// Materialized rows already carry the last known usage beyond the recent
// replay window. Reuse the original row lookup when that history expires.
if m.Call < 0 && l.Message.ID != "" && len(st.Claude.Messages[m.key]) == 1 {
if i, ok := st.Msgs[l.Message.ID]; ok {
old := st.Calls[i]
if m.Request == "" || old.RequestID == "" || m.Request == old.RequestID {
if m.Request == "" && old.RequestID != "" {
m = st.Claude.message(l.Message.ID, old.RequestID)
}
m.Call, m.Began = i, st.Began[l.Message.ID]
m.Usage = &claudeUsageVersion{At: old.Time, Model: old.Model, Tokens: old.Tokens}
m.Updated = old.Time
}
}
}
c.RequestID = m.Request
blockID := "uuid:" + l.UUID
if l.UUID == "" {
// Older writers lack a block UUID. Content distinguishes a new
// text/tool block from a replay without treating its timestamp as ID.
var content struct {
Message struct{ Content json.RawMessage }
}
if json.Unmarshal(b, &content) == nil {
blockID = fmt.Sprintf("content:%x", sha256.Sum256(content.Message.Content))
}
}
newBlock := !m.Blocks[blockID]
if blockID != "" {
if m.Blocks == nil {
m.Blocks = map[string]bool{}
}
m.Blocks[blockID] = true
}
// it was asked when the last user line or, with none since, the last reply ended
asked, seen := st.LastUser, false
if m.Call >= 0 {
asked, seen = m.Began, true
}
if !seen && st.LastAsst.After(asked) {
asked = st.LastAsst
}
// where its asking began: after the last reply's line, its own if it goes on
c.File, c.From, c.To, c.Msg = st.Path, st.AsstEnd, st.End, l.Message.ID
defer func() {
if at.After(st.LastAsst) {
st.LastAsst = at
}
st.AsstEnd = st.End
}()
if e := cmp.Or(string(l.PerTurn), string(l.Effort)); e != "" {
c.Effort = st.str(e)
}
if c.Session == "" {
c.Session = st.Session
}
if st.Agent == "claude" && l.Entrypoint == "claude-desktop" {
c.Agent = "claude-desktop"
}
if l.APIError {
c.Error = st.str(string(l.Error))
if c.Error == "" {
c.Error = "unknown"
}
var m struct {
Message struct {
Content json.RawMessage `json:"content"`
} `json:"message"`
}
json.Unmarshal(b, &m)
c.ErrorText = firstChars(ccText(m.Message.Content), 400)
} else {
u := l.Message.Usage
if u == nil {
return
}
c.Model = st.str(model)
c.Tokens = ccTokens(u.Input, u.Output, u.CacheRead, u.CacheWrite, u.Creation)
if c.Tokens.zero() && m.Usage == nil {
return
}
if !asked.IsZero() && at.After(asked) {
c.Millis = at.Sub(asked).Milliseconds()
}
}
if _, changed := m.accept(at, c.Model, c.Tokens); !changed {
// A missing request ID can become known without changing usage.
if i := m.Call; i >= 0 {
previous := &st.Calls[i]
changed := previous.RequestID != m.Request
previous.RequestID = m.Request
// A new block can finish a reply with unchanged usage. Retain
// its duration/content boundary without moving usage across days.
if newBlock && m.Usage.Model == c.Model && m.Usage.Tokens == c.Tokens &&
at.After(previous.Time) && dateOf(at) == dateOf(previous.Time) {
previous.Time, previous.Millis, previous.To = at, c.Millis, c.To
changed = true
}
if changed && st.dirty != nil {
st.dirty[i] = true
}
}
return
}
if i := m.Call; i >= 0 {
c.From = st.Calls[i].From // its asking began with its first line
st.Calls[i] = c
if st.dirty != nil {
st.dirty[i] = true
}
return
}
m.Call, m.Began = len(st.Calls), asked
if c.Msg != "" {
if st.Msgs == nil {
st.Msgs = map[string]int{}
}
if st.Began == nil {
st.Began = map[string]time.Time{}
}
st.Msgs[c.Msg], st.Began[c.Msg] = m.Call, asked
}
if c.Error == "" {
c.Compact, st.CompactMs = st.CompactMs, 0
}
st.Calls = append(st.Calls, c)
}
// firstChars is the first n characters of s.
func firstChars(s string, n int) string {
if r := []rune(s); len(r) > n {
return string(r[:n])
}
return s
}
var (
ccIdentity = []byte(`"identity":{"modelId":"`)
ccUserLine = []byte(`"type":"user"`)
ccCompact = []byte(`"subtype":"compact_boundary"`)
cxToolOut = []byte(`"type":"function_call_output"`)
cxCustomOut = []byte(`"type":"custom_tool_call_output"`)
cxStarted = []byte(`"type":"task_started"`)
cxDone = []byte(`"type":"task_complete"`)
)
// cxCall is a line of a Codex rollout that says something about calls.
type cxCall struct {
Type string `json:"type"`
Payload struct {
cxCompaction
cxHistorySnapshot
Type string `json:"type"`
ID string `json:"id"`
SessionID string `json:"session_id"`
ThreadID string `json:"thread_id"`
Upstream string `json:"model_provider"`
AccountID string `json:"creator_account_id"`
UserID string `json:"creator_user_id"`
Cwd string `json:"cwd"`
Model string `json:"model"`
Effort ccStr `json:"effort"`
TurnID string `json:"turn_id"`
ResponseID string `json:"response_id"`
Usage *cxUsage `json:"usage"`
ThreadUsage *cxUsage `json:"thread_token_usage"`
Duration int64 `json:"duration_ms"`
FirstToken int64 `json:"time_to_first_token_ms"`
Settings struct {
Model string `json:"model"`
Upstream string `json:"model_provider"`
} `json:"thread_settings"`
Info *struct {
Total *cxCallUsage `json:"total_token_usage"`
Last *cxCallUsage `json:"last_token_usage"`
} `json:"info"`
} `json:"payload"`
}
// codexCallLine follows a rollout: the model, effort and turn in force, and
// a call wherever the running total grew.
func codexCallLine(st *callFile, b []byte) {
r := st.CX
if r.Usage == nil {
r.Usage = &codexUsageState{}
}
if cxAnswerItem(b) {
if !r.Answering {
r.Answering, r.AskedAt, r.AskedTurn = true, r.LastIn, r.Turn
}
if at := tsAt(b, false); !at.IsZero() {
r.Answered = at
}
return
}
// what the model is given: the prompt, a tool's output
compacted := typeAfter(b[:min(len(b), 1024)], cxType) == "compacted"
if compacted {
st.codexChanged(r.Usage.compacted(tsAt(b, false), r.Model, cxCompactionIn(b)))
return
}
if bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) || bytes.Contains(b, cxToolOut) || bytes.Contains(b, cxCustomOut) {
if at := tsAt(b, false); !at.IsZero() {
r.LastIn = at
if bytes.Contains(b, cxUserMsg) || bytes.Contains(b, cxUserRole) {
r.Usage.Pending = 0
}
r.saveContext()
}
return
}
if !(!r.Meta && bytes.Contains(b, cxMeta) || bytes.Contains(b, cxTurn) || bytes.Contains(b, cxRecord) || bytes.Contains(b, cxCompacted) || bytes.Contains(b, cxCount) ||
bytes.Contains(b, cxSettings) || bytes.Contains(b, cxStarted) || bytes.Contains(b, cxDone)) {
return
}
var l cxCall
if json.Unmarshal(b, &l) != nil {
return
}
p := &l.Payload
switch {
case l.Type == "session_meta":
if r.Meta {
return
}
r.Meta = true
if p.ID != "" {
r.ThreadID = st.str(p.ID)
}
if p.SessionID != "" {
r.Session = p.SessionID
} else if p.ID != "" {
r.Session = p.ID
}
r.Session, r.Cwd = st.str(r.Session), st.str(p.Cwd)
r.Upstream, r.AccountID, r.UserID = st.str(p.Upstream), st.str(p.AccountID), st.str(p.UserID)
r.Usage.context(r.Session, "", "")
r.Usage.beginSnapshot(tsAt(b, false), p.ID, p.cxHistorySnapshot)
case l.Type == "turn_context":
if p.Upstream != "" {
r.Upstream = st.str(p.Upstream)
}
if p.Model != "" {
r.Model = st.str(p.Model)
}
r.Effort = st.str(string(p.Effort))
if p.Cwd != "" {
r.Cwd = st.str(p.Cwd)
}
if p.TurnID != "" {
r.Turn = p.TurnID
}
r.saveContext()
case l.Type == "event_msg" && p.Type == "task_started":
if p.TurnID != "" {
r.Turn = p.TurnID
}
// a turn's first call was asked for as it began, whatever else was seen
if at := tsAt(b, false); !at.IsZero() {
r.LastIn = at
}
r.Answering = false
r.saveContext()
case l.Type == "event_msg" && p.Type == "thread_settings_applied":
if p.Settings.Upstream != "" {
r.Upstream = st.str(p.Settings.Upstream)
}
if p.Settings.Model != "" {
r.Model = st.str(p.Settings.Model)
}
r.saveContext()
case l.Type == "event_msg" && p.Type == "task_complete":
if r.Turns == nil {
r.Turns = map[string]cxOpen{}
}
turn := r.Turns[p.TurnID]
turn.Complete, turn.Duration, turn.FirstToken = true, p.Duration, p.FirstToken
r.Turns[p.TurnID] = turn
if turn.N == 1 {
st.Calls[turn.First].TTFT, st.Calls[turn.First].Millis = p.FirstToken, p.Duration
st.codexCompact(turn.First)
st.markCall(turn.First)
}
case l.Type == "compacted":
r.Answering = false
st.codexChanged(r.Usage.compacted(tsAt(b, false), r.Model, p.cxCompaction))
case l.Type == "token_usage_record":
if p.Usage != nil {
st.codexChanged(r.Usage.record(codexContribution{ResponseID: p.ResponseID, Session: p.SessionID, ThreadID: p.ThreadID, Turn: p.TurnID, Model: r.Model, At: tsAt(b, false), Usage: *p.Usage, Total: p.ThreadUsage}))
}
case l.Type == "event_msg" && p.Type == "token_count":
if p.Info != nil {
st.codexChanged(r.Usage.count(tsAt(b, false), r.Model, p.Info.Total, p.Info.Last))
}
}
}
func (r *cxRun) saveContext() {
if r.Contexts == nil {
r.Contexts = map[string]cxContext{}
}
r.Contexts[r.Turn] = cxContext{r.Model, r.Effort, r.Cwd, r.Upstream, r.LastIn}
r.Usage.context(r.Session, r.Turn, r.Model)
}
func (st *callFile) markCall(i int) {
if st.dirty != nil {
st.dirty[i] = true
}
}
func (st *callFile) codexChanged(change *codexChange) {
if change == nil {
return
}
r, v, i := st.CX, change.Value, change.Index
context, ok := r.Contexts[v.Turn]
if !ok {
context = cxContext{r.Model, r.Effort, r.Cwd, r.Upstream, r.LastIn}
}
threadID := v.ThreadID
if threadID == "" && !v.ThreadIDConflict {
threadID = r.ThreadID
}
c := Call{Time: v.At, Agent: "codex", Session: r.Session, ThreadID: threadID, Model: v.Model, Tokens: spent(v.Usage.raw()), ResponseID: v.ResponseID,
Upstream: context.Upstream, AccountID: r.AccountID, UserID: r.UserID, Reasoning: v.Usage.Reasoning, Effort: context.Effort, Compaction: v.Compaction, Cwd: context.Cwd, File: st.Path, From: r.LastEnd, To: st.End}
if c.Session == "" {
c.Session = r.Session
}
if change.Old != nil {
// The response may arrive after the next call's content. Upgrading its
// identity/time must not expand its physical content range into that call.
c.From, c.To = st.Calls[i].From, min(st.Calls[i].To, st.End)
st.Calls[i] = c
} else {
prev := -1
if i > 0 && !r.LastCall.After(v.At) {
prev = r.LastIndex
} else {
for j, old := range st.Calls {
if old.Time.Before(v.At) && (prev < 0 || old.Time.After(st.Calls[prev].Time)) {
prev = j
}
}
}
input, end := context.LastIn, time.Time{}
if r.Answering && r.AskedTurn == v.Turn && !r.AskedAt.IsZero() {
input = r.AskedAt
// its own tool's output came between its answer and this line
if r.LastIn.After(r.Answered) && !r.Answered.After(v.At) {
end = r.Answered
}
}
if input.After(v.At) {
input = time.Time{}
}
r.Answering = false
r.Timings = append(r.Timings, cxCallTiming{input, prev, end})
st.Calls = append(st.Calls, c)
if r.Turns == nil {
r.Turns = map[string]cxOpen{}
}
turn := r.Turns[v.Turn]
if turn.N == 0 {
turn.First = i
}
turn.N++
r.Turns[v.Turn] = turn
if turn.N == 2 {
st.codexTiming(turn.First)
}
if st.End > r.LastEnd {
r.LastEnd = st.End
}
}
if i == r.LastIndex || v.At.After(r.LastCall) {
r.LastCall, r.LastIndex = v.At, i
}
st.codexTiming(i)
if change.Old != nil && !change.Old.At.Equal(v.At) {
for j, timing := range r.Timings {
if timing.Previous == i {
st.codexTiming(j)
}
}
}
}
func (st *callFile) codexTiming(i int) {
r := st.CX
c, timing := &st.Calls[i], r.Timings[i]
asked := timing.Input
if timing.Previous >= 0 && st.Calls[timing.Previous].Time.After(asked) {
asked = st.Calls[timing.Previous].Time
}
end := c.Time
if !timing.End.IsZero() {
end = timing.End
}
c.Millis, c.TTFT = 0, 0
if !asked.IsZero() && end.After(asked) && end.Sub(asked) < 2*time.Hour {
c.Millis = end.Sub(asked).Milliseconds()
}
turn := r.Turns[r.Usage.Entries[i].Turn]
if turn.N == 1 && turn.Complete {
c.Millis, c.TTFT = turn.Duration, turn.FirstToken
}
st.codexCompact(i)
st.markCall(i)
}
// codexCompact notes a compaction's call's time as the wait it made: Codex
// compacts by a call of its own to the model.
func (st *callFile) codexCompact(i int) {
c := &st.Calls[i]
c.Compact = 0
if e := st.CX.Usage.Entries; i < len(e) && e[i].Compaction {
c.Compact = c.Millis
}
}
func hashHead(head string) string { return fmt.Sprintf("%x", sha256.Sum256([]byte(head))) }
func sameHead(head, hash string, size int) bool {
return size >= 0 && len(head) >= size && hashHead(head[:size]) == hash
}
// prefixHash checks distributed windows of the indexed prefix. Small files
// are checked in full. The version tag invalidates earlier full-file hashes.
// Rewrites outside the sampled windows can go undetected; appends cost at most
// 64 KiB of reads per fingerprint regardless of the session's length.
func prefixHash(path string, n int64) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
h := sha256.New()
const window, samples = int64(4096), int64(16)
if n <= window*samples {
if _, err = io.CopyN(h, f, n); err != nil {
return ""
}
} else {
for i := int64(0); i < samples; i++ {
off := (n - window) * i / (samples - 1)
if _, err = io.CopyN(h, io.NewSectionReader(f, off, window), window); err != nil {
return ""
}
}
}
return fmt.Sprintf("sample-v1:%x", h.Sum(nil))
}
// CallSource identifies a raw local request log without reading its contents.
type CallSource struct {
Path, Key, Agent string
Size int64
Modified time.Time
}
func CallSources() []CallSource {
fs := callSources()
pruneCalls(fs)
out := make([]CallSource, 0, len(fs))
for _, f := range fs {
out = append(out, CallSource{f.path, f.key, f.agent, f.size, f.mod})
}
return out
}
// ReadCallSource returns immutable calls for one source. Independent files can
// be read concurrently; disk IO never holds callsMu.
func ReadCallSource(s CallSource) []Call {
st := readCalls(file{path: s.Path, key: s.Key, agent: s.Agent, size: s.Size, mod: s.Modified})
// Requests retains its own compact snapshot. Keeping the expanded parser's
// calls as well doubles residency, especially for long active sessions. The
// append-frame shard still carries the continuation for the next read.
callsMu.Lock()
keepCallContinuation(st)
// Index eviction can replace the cached snapshot with a shallow copy.
// Release its rows too, but never evict a newer parse from another reader.
cached := callCache[s.Path]
if cached != nil && cached.Size == st.Size && cached.Mod == st.Mod && cached.Off == st.Off && cached.ContentHash == st.ContentHash {
delete(callCache, s.Path)
for i, p := range callOrder {
if p == s.Path {
callOrder = append(callOrder[:i], callOrder[i+1:]...)
break
}
}
}
callsMu.Unlock()
return st.Calls
}