Skip to content

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

usage: read today's usage ahead at startup

yetone1f589e3f4f51
diff --git a/internal/gui/backend.go b/internal/gui/backend.go
index 02a5831b1..d5911ed31 100644
--- a/internal/gui/backend.go
+++ b/internal/gui/backend.go
@@ -12,6 +12,7 @@ import (
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/update"
+ "github.com/yetone/magpie/internal/usage"
)
// served is the gateway this process serves, nil while another magpie
@@ -40,6 +41,9 @@ func startBackend() (gw *gateway.Server) {
gateway.Window = true // the routing this process serves is shown on its page
gw = serveGateway()
go watchGateway(backendCtx, gatewayWatch)
+ // the usage log parsed now, not when the tray panel first asks for
+ // today's calls (CyberCaius)
+ go usage.ReadAhead()
// Codex's background app-server, restarted when it has the list from
// before a change and no codex session is on it (luci)
go agent.KeepCodexDaemonCurrent(backendCtx)
diff --git a/internal/usage/read_ahead_test.go b/internal/usage/read_ahead_test.go
new file mode 100644
index 000000000..3c4cd5619
--- /dev/null
+++ b/internal/usage/read_ahead_test.go
@@ -0,0 +1,34 @@
+package usage
+
+import (
+ "testing"
+ "time"
+)
+
+// What magpie reads ahead as it starts leaves the tray panel's first ask,
+// today's summary in this computer's zone as /api/usage?period=today asks
+// for it, nothing to parse or work out: the cold parse of the whole log
+// was what its numbers waited for on the first open (CyberCaius).
+func TestReadAheadLeavesTodayNothingToRead(t *testing.T) {
+ pageHome(t)
+ holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ settleLogClock(t)
+ historyLog(t, 2000)
+ counts := func() (int, int) {
+ logIndex.Lock()
+ reads := logIndex.reads
+ logIndex.Unlock()
+ summaries.Lock()
+ defer summaries.Unlock()
+ return reads, summaries.worked
+ }
+ ReadAhead()
+ reads, worked := counts()
+ got := SummarizeTopIn(Today, 100, nil)
+ if r, w := counts(); r != reads || w != worked {
+ t.Fatalf("today's summary after the read ahead parsed the log %d more times and was worked out %d more", r-reads, w-worked)
+ }
+ if got.Totals.Calls != 1 {
+ t.Fatalf("today's calls: %+v", got.Totals)
+ }
+}
diff --git a/internal/usage/summary_cache.go b/internal/usage/summary_cache.go
index 4422bc273..67251442f 100644
--- a/internal/usage/summary_cache.go
+++ b/internal/usage/summary_cache.go
@@ -38,6 +38,15 @@ var summaries struct {
func indexedSummary(p Period, now time.Time) Summary { return topSummary(p, now, -1) }
+// ReadAhead parses the log into its index and works out today's summary in
+// this computer's zone, for magpie to run as it starts: the first reader,
+// the tray panel's line of today's calls, finds both done. Parsing the log
+// cold took 0.33 s at 2 MB and 0.77 s at 40 MB on one core, and the
+// panel's numbers waited for it on the first open after a start
+// (CyberCaius on Discord). A reader that comes while it runs waits for the
+// same parse, never a second.
+func ReadAhead() { SummarizeTopIn(Today, summaryCacheSessions, nil) }
+
// SummarizeTop is Summarize with only the top n sessions by tokens (all of
// them for n < 0); SessionCount says how many the period has. It is what a page that
// shows a few of them or none asks for: a summary of more than