Files
magpie/internal/usage/codex_identity.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

462 lines
13 KiB
Go

package usage
import (
"slices"
"strings"
"sync"
"time"
"unsafe"
"github.com/yetone/magpie/internal/sessions"
)
func codexIdentityRow(row Row, actor string, identities map[string]sessions.CodexIdentity) Row {
if !isCodexRow(row) && actor == "" {
return row
}
row.codexActor = actor
row.SessionRole = "unknown"
row.Subagent, row.ParentAgent = "", ""
identity, ok := identities[actor]
if actor == "" || !ok || identity.ID != actor || identity.Role != "main" && identity.Role != "subagent" {
return row
}
row.SessionRole = identity.Role
row.Session = identity.RootSession
if identity.Role == "subagent" {
row.Subagent = identity.ID
if parent := identities[identity.ParentSession]; parent.ID == identity.ParentSession && parent.Role == "subagent" {
row.ParentAgent = parent.ID
}
}
return row
}
func codexActor(call sessions.Call) string {
if call.Agent == "codex" {
return call.ThreadID
}
return ""
}
func codexResponseMatches(records []Record, calls []sessions.Call, duplicateCalls map[int]bool) map[int]int {
recordIDs := map[string][]int{}
for i, record := range records {
if record.ResponseID != "" {
recordIDs[record.ResponseID] = append(recordIDs[record.ResponseID], i)
}
}
callIDs := map[string][]int{}
for i, call := range calls {
if call.ResponseID != "" && !duplicateCalls[i] {
callIDs[call.ResponseID] = append(callIDs[call.ResponseID], i)
}
}
pairs := map[int]int{}
for id, recordRefs := range recordIDs {
callRefs := callIDs[id]
if len(recordRefs) != 1 || len(callRefs) != 1 {
continue
}
record, call := recordRefs[0], callRefs[0]
if records[record].Agent == "codex" && !records[record].IsRejected() && records[record].Source != SourceReported && calls[call].Agent == "codex" {
pairs[record] = call
}
}
return pairs
}
func sameCodexCall(a, b Row) bool {
return isCodexRow(a) && isCodexRow(b) && a.ResponseID == b.ResponseID && a.Time.Equal(b.Time) &&
a.Model == b.Model && a.Requested == b.Requested && a.Served == b.Served && a.Effort == b.Effort &&
a.Input == b.Input && a.Output == b.Output && a.CacheRead == b.CacheRead && a.CacheWrite == b.CacheWrite &&
a.CacheWrite1h == b.CacheWrite1h && a.Reasoning == b.Reasoning && a.Millis == b.Millis && a.TTFT == b.TTFT &&
a.Error == b.Error && a.ErrType == b.ErrType && a.RequestID == b.RequestID
}
// codexCompactionOf is what a native Codex call says about compaction: only
// a call read from a token_usage_record (it has a response_id) can say, as
// Codex writes that record and a compaction's compaction_response_id together
// (openai/codex#41912). An older rollout's call, told from token_count alone,
// never carries the marker, so it is unknown, not "no compaction".
func codexCompactionOf(call sessions.Call) *bool {
if call.Agent != "codex" || call.ResponseID == "" {
return nil
}
compacted := call.Compaction
return &compacted
}
func mergeCodexCompactions(values []*bool) *bool {
allKnown, confirmed := len(values) > 0, false
for _, value := range values {
if value == nil {
allKnown = false
continue
}
confirmed = confirmed || *value
}
if confirmed {
yes := true
return &yes
}
if allKnown {
no := false
return &no
}
return nil
}
func isCodexRow(row Row) bool {
if row.Client != "" {
return row.Client == "codex"
}
return row.Agent == "codex"
}
func duplicateCodexCalls(calls []sessions.Call) (map[int]bool, map[int]string, map[int]*bool) {
local := map[string][]int{}
for i, call := range calls {
if call.Agent == "codex" && call.ResponseID != "" {
local[call.ResponseID] = append(local[call.ResponseID], i)
}
}
skip, actors, compactions := map[int]bool{}, map[int]string{}, map[int]*bool{}
for index, call := range calls {
if call.Agent == "codex" {
compactions[index] = codexCompactionOf(call)
}
}
for _, indexes := range local {
if len(indexes) < 2 {
continue
}
rows := make([]Row, len(indexes))
actor := calls[indexes[0]].ThreadID
actorConflict := actor == ""
identical := true
for i, index := range indexes {
rows[i] = Row{Record: logRecord(calls[index])}
if calls[index].ThreadID != actor {
actorConflict = true
}
if i > 0 && !sameCodexCall(rows[0], rows[i]) {
identical = false
}
}
if !identical {
for _, index := range indexes {
actors[index] = ""
}
continue
}
for _, index := range indexes[1:] {
skip[index] = true
}
merged := make([]*bool, len(indexes))
for i, index := range indexes {
merged[i] = codexCompactionOf(calls[index])
}
compactions[indexes[0]] = mergeCodexCompactions(merged)
if actorConflict {
actors[indexes[0]] = ""
} else {
actors[indexes[0]] = actor
}
}
return skip, actors, compactions
}
func cloneCodexChunks(chunks []*rowChunk) []*rowChunk {
out := make([]*rowChunk, len(chunks))
for i, chunk := range chunks {
copy := *chunk
copy.codexActors = slices.Clone(chunk.codexActors)
copy.codexCompactions = nil
copy.codexIdentities = nil
out[i] = &copy
}
return out
}
func codexChunkResponsePairs(gateways, locals []*rowChunk, duplicateLocal map[rowRef]bool) map[rowRef]rowRef {
gatewayIDs := map[string][]rowRef{}
for _, chunk := range gateways {
for i := range chunk.Rows {
row := chunk.row(i)
if row.ResponseID != "" {
gatewayIDs[row.ResponseID] = append(gatewayIDs[row.ResponseID], rowRef{chunk, i})
}
}
}
localIDs := map[string][]rowRef{}
for _, chunk := range locals {
for i := range chunk.Rows {
ref := rowRef{chunk, i}
if duplicateLocal[ref] {
continue
}
row := chunk.row(i)
if row.ResponseID != "" {
localIDs[row.ResponseID] = append(localIDs[row.ResponseID], ref)
}
}
}
pairs := map[rowRef]rowRef{}
for id, gatewayRefs := range gatewayIDs {
localRefs := localIDs[id]
if len(gatewayRefs) != 1 || len(localRefs) != 1 {
continue
}
gateway, local := gatewayRefs[0], localRefs[0]
gatewayRow, localRow := gateway.row(), local.row()
if isCodexRow(gatewayRow) && !gatewayRow.IsRejected() && gatewayRow.Source != SourceReported && isCodexRow(localRow) && localRow.Source == "log" {
pairs[gateway] = local
}
}
return pairs
}
func foldCodexRows(locals []*rowChunk) map[rowRef]bool {
localIDs := map[string][]rowRef{}
for _, chunk := range locals {
for i := range chunk.Rows {
row := chunk.row(i)
if isCodexRow(row) && row.Source == "log" && row.ResponseID != "" {
localIDs[row.ResponseID] = append(localIDs[row.ResponseID], rowRef{chunk, i})
}
}
}
skip := map[rowRef]bool{}
for _, refs := range localIDs {
if len(refs) < 2 {
continue
}
actors := make([]string, len(refs))
rows := make([]Row, len(refs))
identical := true
actor := ""
actorConflict := false
for i, ref := range refs {
rows[i] = ref.row()
if ref.Index < len(ref.Chunk.codexActors) {
actors[i] = ref.Chunk.codexActors[ref.Index]
}
if i == 0 {
actor = actors[i]
} else {
if actors[i] != actor {
actorConflict = true
}
if !sameCodexCall(rows[0], rows[i]) {
identical = false
}
}
}
if !identical {
for _, ref := range refs {
setCodexActor(ref.Chunk, ref.Index, "")
}
continue
}
if actorConflict {
setCodexActor(refs[0].Chunk, refs[0].Index, "")
}
for _, ref := range refs[1:] {
skip[ref] = true
}
compactions := make([]*bool, len(refs))
for i := range refs {
compactions[i] = rows[i].Compaction
}
refs[0].Chunk.setCodexCompaction(refs[0].Index, mergeCodexCompactions(compactions))
}
return skip
}
func setCodexActor(chunk *rowChunk, index int, actor string) {
if len(chunk.codexActors) != len(chunk.Rows) {
actors := make([]string, len(chunk.Rows))
copy(actors, chunk.codexActors)
chunk.codexActors = actors
}
chunk.codexActors[index] = actor
}
func codexActorBytes(actors []string) int64 {
bytes := int64(cap(actors)) * int64(unsafe.Sizeof(""))
for _, actor := range actors {
bytes += int64(len(actor))
}
return bytes
}
// codexEffort keeps CodexModelEffort's last answer for each period: the
// Sessions page asks for all history every 15s, and the ledger under it is
// read again only when a call, a price or an identity changed.
//
// A gateway in use writes a call every few seconds, so the answer is often
// out of date when it is asked for. Only one ledger is built at a time, and
// while one is built for a period that has an answer already, that answer is
// what is said: each request used to build its own ledger of all history
// (#2278, with 68k session files and 75k calls a week), and the Sessions
// page's requests arriving together grew the app to 85 GB.
var codexEffort = struct {
sync.Mutex
periods map[Period]*effortEntry
built int64 // a clock for which period was asked for last
}{periods: map[Period]*effortEntry{}}
// codexEffortBuilding holds one ledger build at a time.
var codexEffortBuilding sync.Mutex
// effortPeriodsKept is how many periods' answers are kept: a picked range is
// a period of its own, so they are not kept without end.
const effortPeriodsKept = 8
type effortEntry struct {
key string
root string // the logs it was read from: another home's is no answer
value map[string][]sessions.ModelEffort
have bool
building chan struct{} // closed when the build under way ends
used int64
}
// effortBuilt, in tests, is told each time CodexModelEffort reads the ledger.
var effortBuilt func()
// CodexModelEffort reports each Codex thread's own priced ledger consumption.
func CodexModelEffort(p Period) map[string][]sessions.ModelEffort {
if LogCalls != nil { // synthetic logs carry no state to key on
return codexModelEffort(LedgerOf(p, Filter{}).Rows)
}
now := Clock()
st := readRequestState(now)
codexEffort.Lock()
e := codexEffort.periods[p]
if e == nil {
e = &effortEntry{}
codexEffort.periods[p] = e
forgetEffortPeriods(p)
}
codexEffort.built++
e.used = codexEffort.built
if e.have && (e.key == st.key || e.building != nil && e.root == st.root) {
value := e.value // current, or the last one while the next is built
codexEffort.Unlock()
return cloneEffort(value)
}
if wait := e.building; wait != nil { // the first answer is on its way
codexEffort.Unlock()
<-wait
codexEffort.Lock()
value := e.value
codexEffort.Unlock()
return cloneEffort(value)
}
done := make(chan struct{})
e.building = done
codexEffort.Unlock()
var value map[string][]sessions.ModelEffort
defer func() {
codexEffort.Lock()
e.building = nil
if value != nil {
// kept under the state read before the build: a call written
// while it ran changes the state, and the next ask builds again
e.key, e.root, e.value, e.have = st.key, st.root, value, true
}
codexEffort.Unlock()
close(done)
}()
codexEffortBuilding.Lock()
defer codexEffortBuilding.Unlock()
if effortBuilt != nil {
effortBuilt()
}
// Only Codex's own calls carry a Codex thread: a gateway call is told
// its thread by the Codex call that has its response id, so the other
// agents' session files are not read for this.
value = codexModelEffort(ledgerOfAt(p, Filter{}, now, codexCalls).Rows)
return cloneEffort(value)
}
func codexCalls(since time.Time) []sessions.Call { return sessions.AgentCalls(since, "codex") }
// forgetEffortPeriods drops the answers asked for longest ago, keeping p's
// entry; codexEffort is held.
func forgetEffortPeriods(p Period) {
for len(codexEffort.periods) > effortPeriodsKept {
var oldest Period
var at int64 = -1
for q, e := range codexEffort.periods {
if q != p && e.building == nil && (at < 0 || e.used < at) {
oldest, at = q, e.used
}
}
if at < 0 {
return
}
delete(codexEffort.periods, oldest)
}
}
func cloneEffort(values map[string][]sessions.ModelEffort) map[string][]sessions.ModelEffort {
out := make(map[string][]sessions.ModelEffort, len(values))
for actor, v := range values {
out[actor] = slices.Clone(v)
}
return out
}
func codexModelEffort(rows []Row) map[string][]sessions.ModelEffort {
type modelEffortKey struct{ model, effort string }
type total struct {
value sessions.ModelEffort
priced bool
}
groups := map[string]map[modelEffortKey]*total{}
for _, row := range rows {
if row.codexActor == "" || row.IsRejected() ||
row.Input+row.Output+row.CacheRead+row.CacheWrite == 0 {
continue
}
byModel := groups[row.codexActor]
if byModel == nil {
byModel = map[modelEffortKey]*total{}
groups[row.codexActor] = byModel
}
key := modelEffortKey{row.Model, row.Effort}
entry := byModel[key]
if entry == nil {
entry = &total{value: sessions.ModelEffort{Model: row.Model, Effort: row.Effort}, priced: true}
byModel[key] = entry
}
entry.priced = entry.priced && row.Priced
entry.value.Input += row.Input
entry.value.Output += row.Output
entry.value.CacheRead += row.CacheRead
entry.value.CacheWrite += row.CacheWrite
entry.value.CacheWrite1h += row.CacheWrite1h
entry.value.Reasoning += row.Reasoning
entry.value.Cost += row.Cost
}
out := make(map[string][]sessions.ModelEffort, len(groups))
for actor, byModel := range groups {
values := make([]sessions.ModelEffort, 0, len(byModel))
for _, entry := range byModel {
entry.value.Priced = entry.priced
values = append(values, entry.value)
}
slices.SortFunc(values, func(a, b sessions.ModelEffort) int {
if a.Model != b.Model {
return strings.Compare(a.Model, b.Model)
}
return strings.Compare(a.Effort, b.Effort)
})
out[actor] = values
}
return out
}