diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go index 9479da359..0429662cb 100644 --- a/internal/usage/codex_identity.go +++ b/internal/usage/codex_identity.go @@ -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++ diff --git a/internal/usage/derived_idle.go b/internal/usage/derived_idle.go index 7930b5330..10a860d52 100644 --- a/internal/usage/derived_idle.go +++ b/internal/usage/derived_idle.go @@ -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 } diff --git a/internal/usage/derived_idle_test.go b/internal/usage/derived_idle_test.go index 6c8e0c4c4..9d79be014 100644 --- a/internal/usage/derived_idle_test.go +++ b/internal/usage/derived_idle_test.go @@ -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) + } +}