Files
magpie/internal/usage/main_test.go
T
yetone 9fed5dc8c1 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.
2026-10-27 11:24:15 +08:00

20 lines
697 B
Go

package usage
import (
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// TestMain gives the package a home of its own (testenv): a test that
// doesn't set one reads and writes there, never in the runner's agents or,
// on Windows, its APPDATA and LOCALAPPDATA. The package's ledger reads what
// sessions finds, which asks each agent's own variable before its folder in
// the home: one left in the shell would point a test at the runner's real
// 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, effortBehind = 0, false
testenv.Main(m)
}