usage: Codex's model and effort answers go with the chunks' caches when idle

CodexModelEffort kept each period's answer (every Codex thread's
consumption by model and effort) for the life of the process. On a real
history it was ~6.9 MB of the idle heap after one visit to the Sessions
page (t59 measured on mote, heap at 10 idle minutes:
usage.keptCodexModelEffort).

Each ask now notes itself with derivedUsed, as the pairing and heatmap
reads do, and dropDerived lets go of every period's answer but one being
built. The page polls every 15 s while open, so it keeps them; the first
ask after derivedIdle (3 minutes) builds the answer again, waiting for it
as a first ask does.

Run on mote (go1.26.8):
- go test -count=1 ./internal/usage ./internal/usageapi ./internal/gui: ok
- gofmt -l, go vet, GOOS=windows go vet, GOOS=linux go vet -tags nogui
  for internal/usage: clean.
- Broken by hand: with dropEffortPeriods left out of dropDerived,
  TestIdleCodexEffortLetGoAndBuiltAgain fails ("1 periods' answers kept
  while idle").
This commit is contained in:
yetone
2026-11-11 06:10:18 +08:00
parent 0bb991b0da
commit 8d68b5f219
3 changed files with 55 additions and 0 deletions
+1
View File
@@ -629,6 +629,7 @@ func keptCodexModelEffort(p Period) map[string][]sessions.ModelEffort {
if LogCalls != nil { // synthetic logs carry no state to key on
return codexModelEffort(codexEffortRows(p, Clock(), LogCalls))
}
derivedUsed() // the kept answers go with the chunks' caches when idle
codexEffort.Lock()
if e := codexEffort.periods[p]; e != nil && e.have && e.root == requestRoot() && time.Since(e.at) < effortFresh {
codexEffort.built++
+18
View File
@@ -77,5 +77,23 @@ func dropDerived() int {
n += len(heatBefores.by)
heatBefores.by = nil
heatBefores.Unlock()
n += dropEffortPeriods()
return n
}
// dropEffortPeriods lets go of every period's kept CodexModelEffort answer
// but one being built: each thread's consumption by model, ~7 MB of a real
// history's idle heap after one visit to the Sessions page. The next ask
// builds it again, waiting for it as a first ask does.
func dropEffortPeriods() int {
codexEffort.Lock()
defer codexEffort.Unlock()
n := 0
for p, e := range codexEffort.periods {
if e.building == nil {
delete(codexEffort.periods, p)
n++
}
}
return n
}
+36
View File
@@ -1,8 +1,11 @@
package usage
import (
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
)
// What is kept of each chunk by its serial stays while it was read within
@@ -64,3 +67,36 @@ func TestDerivedLetGoWhenIdle(t *testing.T) {
t.Fatal("a read after the let-go did not arm the check again")
}
}
// Codex's model and effort answers go with the chunks' caches once nothing
// asked for them for derivedIdle, and the next ask builds the same answer
// again; an ask keeps them.
func TestIdleCodexEffortLetGoAndBuiltAgain(t *testing.T) {
pageHome(t)
now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.UTC))
path := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30", "rollout-2026-09-30T10-00-00-main-a.jsonl")
writeCodexCall(t, path, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 20, 3)
derivedKept.off = false
t.Cleanup(func() { derivedKept.off = true })
built := 0
effortBuilt = func() { built++ }
t.Cleanup(func() { effortBuilt = nil })
CodexModelEffort(All)
derivedIdleCheck() // asked just now: kept
if CodexModelEffort(All); built != 1 {
t.Fatalf("built %d times while asked; want the answer kept", built)
}
derivedKept.last.Store(time.Now().Add(-2 * derivedIdle).UnixNano())
derivedIdleCheck()
codexEffort.Lock()
kept := len(codexEffort.periods)
codexEffort.Unlock()
if kept != 0 {
t.Fatalf("%d periods' answers kept while idle", kept)
}
again := CodexModelEffort(All)
if got, ok := findModelEffort(again["main-a"], "m", "low"); built != 2 || !ok || got.Input != 20 {
t.Fatalf("after the let-go: built %d, %+v", built, again)
}
}