usage: a Codex effort past its minute is told while the next is built behind it

The Sessions page asks for every Codex thread's consumption on each 15s
poll. Once the kept answer was a minute old and any session file had
changed (a working Codex or Claude Code changes one all the time), the
ask built the ledger of all history inside the request, so /api/sessions
held for 0.8 to 1.3 s about once a minute. Now that ask is told the kept
answer and the build runs behind it, one at a time as before; the next
poll has the change. A period's first answer, and one for another home,
are still waited for.

effortBehind is off in the usage tests' TestMain, as effortFresh is, so
the tests that write a change and ask again still see it at once.
TestCodexModelEffortHeldForAMinute now holds the build and checks that
the ask after the minute, and one during the build, get the last answer,
and that the ask after the build gets the change without a third build.

Run at 5e3dd1d52 plus this change (main since gained e7173958e and
aa766289b, agent and gui only; re-run below at the rebased head):
- go test ./internal/usage/ ./internal/usageapi/ ./internal/gui/ ok
- go test -race -count=5 -run TestCodexModelEffort ./internal/usage/ ok
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage/ ok
- broken by hand (the background branch made unreachable):
  TestCodexModelEffortHeldForAMinute red, "after the minute the ask
  waited for the build"
- real history (rss-home over the live ~/.codex and ~/.claude, 120s
  CPU profile over 8 GUI-like polls, rss-bin/cpu6.sh), /api/sessions
  per poll:
    before: 1238 207 138 210 194 980 295 288 ms; 815 140 197 158 413 484 151 231 ms
    after:   348 141 330 177 263 233 135 263 ms; 449 267 203 210 161 239 317 290 ms
  pairRows CPU unchanged (0.79s vs 0.81/0.91s): the build moved, it
  wasn't added. Requests page max 260 ms after vs 404 ms before on the
  second pair.

At the rebased head (on e7173958e): go build ok; go test ./internal/usageapi/
./internal/gui/ ok; ./internal/usage/ ok except
TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp, a
known wall-clock flake taken by ggbdpq (#3511): 1 of 20 red on a clean
origin/main e7173958e worktree, 0 of 20 with this change.

Not tried: the Windows box and the Linux app; nothing here is OS-specific.
This commit is contained in:
yetone
2026-10-27 11:24:15 +08:00
parent e7173958e9
commit 9fed5dc8c1
3 changed files with 59 additions and 10 deletions
+37 -6
View File
@@ -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)
}
+21 -3
View File
@@ -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
+1 -1
View File
@@ -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)
}