diff --git a/internal/usage/codex_effort_shared_test.go b/internal/usage/codex_effort_shared_test.go index 0c2f22894..4573a864c 100644 --- a/internal/usage/codex_effort_shared_test.go +++ b/internal/usage/codex_effort_shared_test.go @@ -108,11 +108,12 @@ func TestCodexModelEffortAnswersLastWhileBuilding(t *testing.T) { // 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. +// and the first ask after it is told it too while the change is built +// behind it, for the next ask. func TestCodexModelEffortHeldForAMinute(t *testing.T) { now, rollout := effortFixture(t) - effortFresh = time.Hour - t.Cleanup(func() { effortFresh = 0 }) + effortFresh, effortBehind = time.Hour, true + t.Cleanup(func() { effortFresh, effortBehind = 0, false }) var built atomic.Int32 effortBuilt = func() { built.Add(1) } t.Cleanup(func() { effortBuilt = nil }) @@ -129,13 +130,43 @@ func TestCodexModelEffortHeldForAMinute(t *testing.T) { codexEffort.Lock() codexEffort.periods[All].at = time.Now().Add(-2 * time.Hour) codexEffort.Unlock() + entered, release := make(chan struct{}), make(chan struct{}) + effortBuilt = func() { + built.Add(1) + close(entered) + <-release + } + asked := make(chan map[string][]sessions.ModelEffort, 1) + go func() { asked <- CodexModelEffort(All) }() + <-entered + select { + case got := <-asked: + if got, _ := findModelEffort(got["main-a"], "m", "low"); got.Input != 20 { + t.Fatalf("after the minute the ask got %+v, want the last answer", got) + } + case <-time.After(30 * time.Second): + close(release) + t.Fatal("after the minute the ask waited for the build") + } + codexEffort.Lock() + building := codexEffort.periods[All].building + codexEffort.Unlock() + if got, _ := findModelEffort(CodexModelEffort(All)["main-a"], "m", "low"); got.Input != 20 { + t.Fatalf("an ask during the build got %+v, want the last answer", got) + } + close(release) + <-building + effortBuilt = func() { built.Add(1) } 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) + t.Fatalf("after the build: builds %d, %+v, want 2 and the change", built.Load(), got) } - // another home's answer is never held for this one + // another home's answer is never held for this one, nor told while + // its own is built effortFixture(t) - CodexModelEffort(All) + if got := CodexModelEffort(All); len(got) == 0 || built.Load() != 3 { + t.Fatalf("a new home was told the last one's answer: builds %d, %+v", built.Load(), got) + } if n := built.Load(); n != 3 { t.Fatalf("a new home was told the last one's answer: builds %d, want 3", n) } diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go index 558ddd5ac..305bd4fdd 100644 --- a/internal/usage/codex_identity.go +++ b/internal/usage/codex_identity.go @@ -473,6 +473,14 @@ type effortEntry struct { // own rows and totals are read as they change. var effortFresh = time.Minute +// effortBehind: once an answer is past effortFresh and the history +// changed, the ask that finds it is told it while the next is built behind +// it. The build is the ledger of all history, and made inside the +// Sessions page's request it held that poll 0.8 to 1.3 s every minute a +// Codex or Claude Code was working; the next poll has the change. Only a +// period's first answer, or one from another home, is waited for. +var effortBehind = true + // effortBuilt, in tests, is told each time CodexModelEffort reads the ledger. var effortBuilt func() @@ -516,9 +524,20 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { } done := make(chan struct{}) e.building = done + if effortBehind && e.have && e.root == st.root { + value := e.value + codexEffort.Unlock() + go buildEffort(e, st, p, now, done) + return cloneEffort(value) + } codexEffort.Unlock() + return cloneEffort(buildEffort(e, st, p, now, done)) +} + +// buildEffort builds p's answer under st into e, and closes done when it +// is kept. +func buildEffort(e *effortEntry, st requestState, p Period, now time.Time, done chan struct{}) (value map[string][]sessions.ModelEffort) { began := time.Now() - var value map[string][]sessions.ModelEffort defer func() { codexEffort.Lock() e.building = nil @@ -535,8 +554,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort { if effortBuilt != nil { effortBuilt() } - value = codexEffortOfIndex(st, p, now) - return cloneEffort(value) + return codexEffortOfIndex(st, p, now) } // codexEffortRows are the rows of ledgerOfAt(p, Filter{}, now, reader) that diff --git a/internal/usage/main_test.go b/internal/usage/main_test.go index be5dd7b48..15ceb454a 100644 --- a/internal/usage/main_test.go +++ b/internal/usage/main_test.go @@ -14,6 +14,6 @@ import ( // agent (#522), so testenv clears those too. A test's answers are built // again as soon as the history changes, unless it holds them (effortFresh). func TestMain(m *testing.M) { - effortFresh = 0 + effortFresh, effortBehind = 0, false testenv.Main(m) }