Files
magpie/internal/usage/codex_effort_shared_test.go
T
yetone f3c4d7b7a7 usage: Codex effort reads only the session chunks that have Codex threads
CodexModelEffort (the Usage page's model and effort per Codex thread,
read again each minute) unpacked every chunk of the ledger, Claude
Code's and every other agent's too, to sum the few rows a Codex actor
owns. codexEffortOfIndex now visits only chunks with a Codex actor
(pairedRows.visitActors), and effortSums.add clones the model and
effort it keeps, since lentRows lends its strings.

Run at this head (d5f86d991 + this): go test ./internal/usage
./internal/usageapi ./internal/gui ok; TestCodexModelEffort* -race
-count=3 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/usage clean. TestCodexModelEffortIsTheWholeLedgers
now checks a chunk was read and the Claude Code session's wasn't: red
with the skip taken out by hand, and red with the clone taken out
(the sums read the next chunk's strings).
A/B on real session files, 8 sequential polls of state, sessions,
stats, usage, usage/requests, quotas and providers, 15 s apart, the
previous commit's build vs this: CodexModelEffort 305 -> 245 MB,
codexEffortOfIndex 238 -> 176 MB; against v0.1.1661 twice, 304 -> 253
and 308 -> 245 MB. Totals moved with live sessions being written
beside the runs (the sandbox HOME links ~/.codex/sessions and
~/.claude/projects): ReadCallSource read 46 to 398 MB a run on the
same binaries, so the totals don't measure this change.
Not changed: pairRowsMemo's misses in buildRequestBlocksIn and
readRequestState's listing, the next hotspots.
Rebased on 33ccc6188 (library, not usage) and run again at the pushed
head: usage and usageapi ok.
2026-10-27 09:33:42 +08:00

211 lines
8.3 KiB
Go

package usage
import (
"fmt"
"os"
"path/filepath"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// effortFixture is one Codex thread's call in a sandbox home.
func effortFixture(t *testing.T) (now time.Time, rollout string) {
t.Helper()
pageHome(t)
now = holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC))
rollout = filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30", "rollout-2026-09-30T10-00-00-main-a.jsonl")
writeCodexCall(t, rollout, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 20, 3)
return now, rollout
}
// The Sessions page's overview and list ask for every Codex thread's
// consumption at once, each refresh. Each ask built its own ledger of all
// history, and with 68k session files the app grew to 85 GB (#2278).
// Asks arriving together are answered by one build.
func TestCodexModelEffortConcurrentAsksBuildOnce(t *testing.T) {
effortFixture(t)
var built atomic.Int32
release := make(chan struct{})
effortBuilt = func() {
if built.Add(1) == 1 {
<-release
}
}
t.Cleanup(func() { effortBuilt = nil })
const asks = 8
results := make([]map[string][]sessions.ModelEffort, asks)
var wg sync.WaitGroup
for i := range asks {
wg.Add(1)
go func() {
defer wg.Done()
results[i] = CodexModelEffort(All)
}()
}
// give the other asks time to start builds of their own, as they did
deadline := time.Now().Add(time.Second)
for built.Load() < asks && time.Now().Before(deadline) {
time.Sleep(5 * time.Millisecond)
}
close(release)
wg.Wait()
if n := built.Load(); n != 1 {
t.Fatalf("%d asks at once built the ledger %d times, want 1", asks, n)
}
for i, r := range results {
if got, ok := findModelEffort(r["main-a"], "m", "low"); !ok || got.Input != 20 {
t.Fatalf("ask %d got %+v", i, r)
}
}
}
// While the next answer is built, an ask for the same period is told the
// last one at once rather than building another ledger beside it.
func TestCodexModelEffortAnswersLastWhileBuilding(t *testing.T) {
now, rollout := effortFixture(t)
var built atomic.Int32
effortBuilt = func() { built.Add(1) }
t.Cleanup(func() { effortBuilt = nil })
CodexModelEffort(All)
writeCodexCall(t, rollout, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 2000, 30)
later := now.Add(time.Minute)
if err := os.Chtimes(rollout, later, later); err != nil {
t.Fatal(err)
}
entered, release := make(chan struct{}), make(chan struct{})
effortBuilt = func() {
if built.Add(1) == 2 {
close(entered)
<-release
}
}
done := make(chan map[string][]sessions.ModelEffort)
go func() { done <- CodexModelEffort(All) }()
<-entered
meanwhile := CodexModelEffort(All)
close(release)
next := <-done
if n := built.Load(); n != 2 {
t.Fatalf("an ask during a build built the ledger again: %d builds, want 2", n)
}
if got, _ := findModelEffort(meanwhile["main-a"], "m", "low"); got.Input != 20 {
t.Fatalf("an ask during the build got %+v, want the last answer", meanwhile)
}
if got, _ := findModelEffort(next["main-a"], "m", "low"); got.Input != 2000 {
t.Fatalf("the build got %+v", next)
}
if got, _ := findModelEffort(CodexModelEffort(All)["main-a"], "m", "low"); built.Load() != 2 || got.Input != 2000 {
t.Fatalf("the next ask: builds %d, %+v", built.Load(), got)
}
}
// A history that changes between polls, as a working Codex's does, is
// built into the answer at most once a minute: an ask inside the minute is
// told the last answer without reading the ledger or the sources' state,
// and the first ask after it builds the change in.
func TestCodexModelEffortHeldForAMinute(t *testing.T) {
now, rollout := effortFixture(t)
effortFresh = time.Hour
t.Cleanup(func() { effortFresh = 0 })
var built atomic.Int32
effortBuilt = func() { built.Add(1) }
t.Cleanup(func() { effortBuilt = nil })
CodexModelEffort(All)
writeCodexCall(t, rollout, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 2000, 30)
later := now.Add(time.Minute)
if err := os.Chtimes(rollout, later, later); err != nil {
t.Fatal(err)
}
if got, _ := findModelEffort(CodexModelEffort(All)["main-a"], "m", "low"); built.Load() != 1 || got.Input != 20 {
t.Fatalf("inside the minute: builds %d, %+v, want 1 and the last answer", built.Load(), got)
}
codexEffort.Lock()
codexEffort.periods[All].at = time.Now().Add(-2 * time.Hour)
codexEffort.Unlock()
if got, _ := findModelEffort(CodexModelEffort(All)["main-a"], "m", "low"); built.Load() != 2 || got.Input != 2000 {
t.Fatalf("after the minute: builds %d, %+v, want 2 and the change", built.Load(), got)
}
// another home's answer is never held for this one
effortFixture(t)
CodexModelEffort(All)
if n := built.Load(); n != 3 {
t.Fatalf("a new home was told the last one's answer: builds %d, want 3", n)
}
}
// An answer is kept when there is no gateway log yet, and when the log grew
// while it was built: the next ask builds again only if the state did change.
func TestCodexModelEffortKeptWithoutLogAndAcrossAWrite(t *testing.T) {
now, _ := effortFixture(t)
var built atomic.Int32
effortBuilt = func() { built.Add(1) }
t.Cleanup(func() { effortBuilt = nil })
CodexModelEffort(All)
CodexModelEffort(All)
if n := built.Load(); n != 1 {
t.Fatalf("with no usage.jsonl the ledger was built %d times, want 1", n)
}
effortBuilt = func() {
if built.Add(1) == 2 { // the gateway writes while the ledger is built
Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 5, Output: 1, Status: 200, ResponseID: "resp-2"})
}
}
Append(Record{Time: now.Add(-2 * time.Minute), Agent: "pi", Provider: "openai", Model: "m", Input: 1, Output: 1, Status: 200})
CodexModelEffort(All) // built under the state before the write
CodexModelEffort(All) // the write changed it
CodexModelEffort(All)
if n := built.Load(); n != 3 {
t.Fatalf("builds = %d, want 3", n)
}
}
// CodexModelEffort reads Codex's session files only; what it reports is what
// the whole ledger, every agent's files read, says of each Codex thread.
func TestCodexModelEffortIsTheWholeLedgers(t *testing.T) {
now, _ := effortFixture(t)
root := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30")
writeCodexCall(t, filepath.Join(root, "rollout-2026-09-30T11-00-00-main-b.jsonl"), "main-b", "main-b", `"cli"`, "resp-b", "m", "high", now.Add(-30*time.Minute), 300, 30)
Append(Record{Time: now.Add(-30 * time.Minute), Agent: "codex", Provider: "openai", Model: "m", Effort: "xhigh", Input: 300, Output: 30, Status: 200, ResponseID: "resp-b", Session: "main-b"})
Append(Record{Time: now.Add(-20 * time.Minute), Agent: "codex", Provider: "openai", Model: "m", Input: 9, Output: 9, Status: 200, ResponseID: "resp-unpaired"})
// a Claude Code session the gateway saw a call of
claude := filepath.Join(sessions.ClaudeDir(), "projects", "-work-app", "sess1.jsonl")
at := now.Add(-10 * time.Minute)
line := fmt.Sprintf(`{"type":"assistant","uuid":"u1","timestamp":%q,"message":{"id":"msg1","role":"assistant","model":"claude-opus-5-5","content":[{"type":"text","text":"hi"}],"usage":{"input_tokens":40,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":4}},"requestId":"req_1","sessionId":"sess1","cwd":"/work/app","entrypoint":"cli"}`+"\n", at.Format(time.RFC3339Nano))
if err := os.MkdirAll(filepath.Dir(claude), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(claude, []byte(line), 0o600); err != nil {
t.Fatal(err)
}
Append(Record{Time: at, Agent: "claude-code", Provider: "anthropic", Model: "claude-opus-5-5", Input: 40, Output: 4, Status: 200, RequestID: "req_1", Session: "sess1"})
whole := codexModelEffort(LedgerOfAt(All, Filter{}, now).Rows)
if len(whole) != 2 {
t.Fatalf("fixture: the whole ledger has %d Codex threads: %+v", len(whole), whole)
}
// and only the chunks a Codex thread is known for are read for it:
// the Claude Code session's isn't
var read []sessions.CallSource
visitingActors = func(c *rowChunk) { read = append(read, c.Source) }
t.Cleanup(func() { visitingActors = nil })
if got := CodexModelEffort(All); !reflect.DeepEqual(got, whole) {
t.Fatalf("Codex-only ledger:\n got %+v\nwant %+v", got, whole)
}
if len(read) == 0 {
t.Fatal("no chunk was read for Codex's effort")
}
for _, src := range read {
if src.Path == claude {
t.Fatalf("the Claude Code session's chunk was read for Codex's effort: %v", read)
}
}
}