Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: a Codex thread's consumption is built again at most once a minute

yetone61d656c69652
diff --git a/internal/usage/codex_effort_shared_test.go b/internal/usage/codex_effort_shared_test.go
index 9ff3817ac..743d2b925 100644
--- a/internal/usage/codex_effort_shared_test.go
+++ b/internal/usage/codex_effort_shared_test.go
@@ -105,6 +105,42 @@ 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.
+func TestCodexModelEffortHeldForAMinute(t *testing.T) {
+ now, rollout := effortFixture(t)
+ effortFresh = time.Hour
+ t.Cleanup(func() { effortFresh = 0 })
+ var built atomic.Int32
+ effortBuilt = func() { built.Add(1) }
+ t.Cleanup(func() { effortBuilt = nil })
+ CodexModelEffort(All)
+
+ writeCodexCall(t, rollout, "main-a", "main-a", `"vscode"`, "resp-1", "m", "low", now.Add(-time.Hour), 2000, 30)
+ later := now.Add(time.Minute)
+ if err := os.Chtimes(rollout, later, later); err != nil {
+ t.Fatal(err)
+ }
+ if got, _ := findModelEffort(CodexModelEffort(All)["main-a"], "m", "low"); built.Load() != 1 || got.Input != 20 {
+ t.Fatalf("inside the minute: builds %d, %+v, want 1 and the last answer", built.Load(), got)
+ }
+ codexEffort.Lock()
+ codexEffort.periods[All].at = time.Now().Add(-2 * time.Hour)
+ codexEffort.Unlock()
+ 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)
+ }
+
+ // another home's answer is never held for this one
+ effortFixture(t)
+ CodexModelEffort(All)
+ if n := built.Load(); n != 3 {
+ t.Fatalf("a new home was told the last one's answer: builds %d, want 3", n)
+ }
+}
+
// An answer is kept when there is no gateway log yet, and when the log grew
// while it was built: the next ask builds again only if the state did change.
func TestCodexModelEffortKeptWithoutLogAndAcrossAWrite(t *testing.T) {
diff --git a/internal/usage/codex_identity.go b/internal/usage/codex_identity.go
index d63cd96c6..e2937cae2 100644
--- a/internal/usage/codex_identity.go
+++ b/internal/usage/codex_identity.go
@@ -390,8 +390,18 @@ type effortEntry struct {
have bool
building chan struct{} // closed when the build under way ends
used int64
+ at time.Time // when the kept answer's build began
}
+// effortFresh is how long an answer stands before a change is built into
+// it. Codex and Claude Code write to their session files all the time, so
+// nearly every 15 s Sessions poll found the history changed and built the
+// ledger of all of it again: over a real history that was ~2.5 GB
+// allocated and ~1 s of CPU every 3.5 minutes for this answer alone. A
+// thread's consumption a minute behind is still the thread's; the page's
+// own rows and totals are read as they change.
+var effortFresh = time.Minute
+
// effortBuilt, in tests, is told each time CodexModelEffort reads the ledger.
var effortBuilt func()
@@ -400,6 +410,15 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort {
if LogCalls != nil { // synthetic logs carry no state to key on
return codexModelEffort(codexEffortRows(p, Clock(), LogCalls))
}
+ codexEffort.Lock()
+ if e := codexEffort.periods[p]; e != nil && e.have && e.root == requestRoot() && time.Since(e.at) < effortFresh {
+ codexEffort.built++
+ e.used = codexEffort.built
+ value := e.value
+ codexEffort.Unlock()
+ return cloneEffort(value) // read before the state is: that is most of an unchanged ask's cost
+ }
+ codexEffort.Unlock()
now := Clock()
st := readRequestState(now)
codexEffort.Lock()
@@ -427,6 +446,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort {
done := make(chan struct{})
e.building = done
codexEffort.Unlock()
+ began := time.Now()
var value map[string][]sessions.ModelEffort
defer func() {
codexEffort.Lock()
@@ -434,7 +454,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort {
if value != nil {
// kept under the state read before the build: a call written
// while it ran changes the state, and the next ask builds again
- e.key, e.root, e.value, e.have = st.key, st.root, value, true
+ e.key, e.root, e.value, e.have, e.at = st.key, st.root, value, true, began
}
codexEffort.Unlock()
close(done)
diff --git a/internal/usage/main_test.go b/internal/usage/main_test.go
index 9802e4ea2..be5dd7b48 100644
--- a/internal/usage/main_test.go
+++ b/internal/usage/main_test.go
@@ -11,5 +11,9 @@ import (
// 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.
-func TestMain(m *testing.M) { testenv.Main(m) }
+// 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
+ testenv.Main(m)
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index 06cd0060a..c759ad2cd 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -672,9 +672,13 @@ func readRequestState(now time.Time) requestState {
fmt.Fprintf(h, "%s:%d:%d;", s.path, s.size, s.mod.UnixNano())
}
return requestState{sources: sources, codexIdentities: codexIdentities, cfg: cfg, snapshot: snapshot, days: days, someShared: someShared,
- root: Path() + "|" + catalog.CachePath(), meta: meta, key: fmt.Sprintf("%x", h.Sum(nil))}
+ root: requestRoot(), meta: meta, key: fmt.Sprintf("%x", h.Sum(nil))}
}
+// requestRoot is the logs a request state is read from: another home's
+// answer is no answer.
+func requestRoot() string { return Path() + "|" + catalog.CachePath() }
+
func QueryPage(p Period, f Filter, offset, limit int) RequestPage {
return queryPage(p, f, offset, limit, sessions.ReadCallSource)
}