Each range statsFor keeps holds an Active for every day, agent and
folder, and each had a 24-int64 Hours (192 bytes), mostly zeros: 4.5 MB
of statsOfRange's 13.5 MB in the idle heap on the owner's real cache
(18,331 files). Active.Hours is now the same sparse hours a day keeps
(hour<<58|seconds), and the act sums in statsOfRange use it too. Its JSON
is the 24 numbers of seconds it was, which the Usage page's heatmap reads
(a.hours.forEach).
Run on mote at this head:
- go test ./internal/sessions: ok
- GOOS=darwin/linux/windows go vet ./internal/sessions: ok; gofmt clean
- TestStatsHoursAndSessions now reads the hours through JSON, as the
page does. Broken by hand (ms not turned into seconds): red, "the 20th
by hour: [... 180000]".
- Real-account copy (~/r2278/acct), idle heap at 180s: inuse 110.65 MB
(v0.1.3402's tree) -> 100.21 MB at this head (sampled; 4.5 MB of it
was the hours' own lists).
statsRead grew out.Sessions by append, so 34k sessions' list (Summary
is ~480 bytes) held up to a quarter more than its rows, 19.4 MB in the
heap after a cold start at #2278's scale, and copied itself through
every size on the way. Each range statsFor keeps holds its list. It is
now made with room for the range's sessions (shares holds only those
with a day in range), and Keys with room for the list.
TestStatsHoursAndSessions checks the list and keys hold their rows and
no more. Broken by hand (the make removed) it failed with "3 sessions
held in 4".
Run at this change (mote): go vet -tags nogui of internal/usage and
internal/sessions for linux, windows and darwin ok; go test -tags nogui
./internal/usage ./internal/sessions ok.
Rebased onto 60561129f and run again at the pushed tree: the same
three packages (usage, sessions, codexcat) ok.
A kept parse holds a model or two, a day or two and a few tools, and Go's
map of one entry is a whole group of eight: ~500 bytes for one model's
Tokens, where a sorted slice entry is 56. Over a long history (15.9k kept
parses) state.Models, Days, day.Models, Tools and Skills held 18.4MB as
maps and 4.2MB as named[V], a slice kept in name order. It is written as
the same JSON object a map writes, byte for byte, so the parse cache
saved before reads unchanged; names are interned with unique.Make, and
plain integers in ints and Tokens are read without reflection.
Measured with the real ~/.codex and ~/.claude histories (sandbox HOME,
warm cache, 40s after start), base 3393e61d0 vs this change, two
alternating runs each:
live heap 102 / 105MB -> 92 / 91MB
footprint 164 / 167MB -> 156 / 150MB
Cache decode at start: ~58ms -> ~61ms (best of 7).
Side by side on the same data, /api/sessions/overview?agent=codex and
/api/stats?days=30 are identical; the other overview queries differed
only in the two Claude sessions being written during the run.
TestNamedIsAMapInJSON: marshal is byte-equal to a map's (HTML-escaped
key included), round trip, a repeated name keeps its last value, an
unknown Tokens field is ignored, omitempty leaves none out, and 1.5,
"1", [1], 1e3 and a too-big int fail as they fail into a map. Broken by
hand (the int fast path accepting 1.5) it went red.
Run at this head (rebased on 2beed3b04): gofmt clean; go build ./...;
GOOS=linux go vet -tags nogui sessions, gui, usage; GOOS=windows vet
sessions; go test -timeout 45m ./internal/sessions ./internal/usage
./internal/gui ./internal/gateway all ok.
Not run: the full ./... suite and the GUI node tests (no GUI change).
title() keeps 160 characters and an ellipsis, 161 in all, and the
overview cut every such title again to 160. So each range statsFor
keeps (all days, and 30) held a new copy of nearly every title, and
clip made a []rune of each title to count it. On a 15,921-file
history, 12,220 of the 12,289 titles are 161 long.
The overview now clips at titleKept (161), and clip counts runes
without converting, returning s itself when it fits.
Run at this head (d4699c00a + this change), in a sandbox HOME with
the real ~/.codex and ~/.claude sessions, warm start (KEEP=1, the
parse cache kept), two alternating runs each, at 40 s after settling:
before: footprint 191 / 193 MB, live heap 112 / 112 MB
after: footprint 185 / 179 MB, live heap 107 / 102 MB
TestStatsTitleIsTheSessionsOwn is red with titleKept set back to 160,
green with it. go test ./internal/sessions ./internal/gui
./internal/usage ok; gofmt clean; GOOS=linux and GOOS=windows go vet
-tags nogui ./internal/sessions ok.
Not run: the full suite (two packages' worth of change, the GUI
package passes).
go test -race -count=3 ./internal/sessions/ failed in TestOpenCodeSubagents:
testenv.Zone (via inZone) wrote time.Local while saveCache's goroutine,
started by List() in an earlier iteration, read it in os.MkdirAll ->
time.Unix (sessions.go:717 -> writeCache :761).
inZone, the only place this package's tests change the zone, now drains
the writer before the swap and again before it is put back. The same drain
written by hand in TestHermesStatsIncludesRecentUsageForOldTranscript is
dropped: inZone does it for every caller.
Run: go test -race -run 'TestOpenCodeSubagents|TestHermes' -count=20
./internal/sessions/ ok (184s); before, -count=3 failed with the race.
The Sessions tab listed every session whose last line fell in the range, and
counted only those stats found at work there. A session whose file goes on
being written after its last call (a setting, a fork's seed) was listed but
not counted, so the note beside the list said more than the KPI above it.
statsAt now returns keys (agent:id) for the sessions it counted, in the
list's order. app.js filters the list by them in every range and falls back
to the old last-line guess only when keys are missing.
The PR also made days with prompts or replies count as at work, so a
session that spoke but spent nothing (one user line, a Cursor chat) is not
dropped. Main's 1cd814004 (#1743) already has the same block, so the PR's
copy was left out.
Run on origin/main with the change:
- go test -tags nogui ./internal/sessions: ok.
- gofmt -l is clean; go vet passes on darwin, linux -tags nogui and windows.
- GUI: sessions-*, usage-sessions-agents-fit, gui-ja and gui-de, chromium
and webkit, en/zh/ja/de: 236/236.
Broken by hand:
- With app.js reverted, 8 sessions-in-range / spoke-not-spent cases fail.
- With the keys loop removed, TestStatsCountsSessionsThatSpentNothing,
TestStatsCountsCursorChats and TestStatsHoursAndSessions fail.
Not tried: a real history with sessions whose last line is a setting.
testenv.StandIns (06420a71) starts each stand-in once in the background on
macOS and records it with os.Stat, which reads time.Local. internal/
sessions' inZone, internal/usage's TestDayFilterDST and the root package's
sessions CLI setup assigned time.Local while those runs could still be
under way. `go test -race -run '^TestDsh$' ./internal/sessions` reported
the race 6 of 6 runs on main (stats_test.go:14 against program.go:140).
testenv.Zone(t, loc) waits on the same WaitGroup RemovePrograms waits on,
sets time.Local and restores it in a Cleanup; the three helpers use it.
The root setup registers Zone before its own Cleanup, so the zone still
goes back after sessions.Reset's index write, as before.
After: the same command 0 races in 6 runs; -race -count=20 of
Dsh|DST over sessions and usage, -race -count=3 of the root Sessions
tests, -race of sessions/testenv/usage/root, go vet, gofmt pass. Tests only.
OpenAI bills gpt-6-astra, gpt-6.1-sol and gpt-6-luna over 272K input at 2× input and cache and 1.5× output, for the whole request. A price now has tiers: `magpie model price <m> 10,50,1,12.5 --tier 272k 20,75,2,25` (--tier can repeat). A tier applies when input + cache reads + cache writes is over its size, so 272,000 is not over and 272,001 is. The built-in prices take tiers only where models.dev lists one (cost.tiers of type "context"), and it does list one for those three. The ledger, each request and OTLP are costed at the tier the call reached. Session and day totals sum many calls, so they stay at the base price.
Anthropic bills a cache write kept for an hour at 2× input and one kept for 5 minutes at 1.25×. Its usage reports which is which in cache_creation.ephemeral_5m/1h_input_tokens. The gateway (API and Claude subscription) and Claude Code's transcripts now keep that split. A price's fifth part is the 1-hour write. When it isn't given, it is 2× input: that is Anthropic's rule, and only Anthropic reports 1-hour writes. Claude list prices carry the 2× too. cache_write_tokens is still the total, and the usage CSV adds cache_write_5m_tokens and cache_write_1h_tokens at the end. The app shows the split on a request's Cache and on the usage tooltips. Old prices and old records, which have no 1-hour part, read as before. The sessions cache (18) and the call cache (calls-v8) are re-read once.
In the GUI, the price boxes were number inputs and dropped what they didn't parse ("0,25"). They are now decimal text boxes that take both "0.25" and "0,25". A model's price row has a 1-hour box (empty means 2× input) and a quiet Long-context price row. That row shows the list's tier greyed in, or sits behind a link when the list has none. A bad size is refused by name. en/zh/ja/de.
Tests:
- New: TestPriceTiers, TestCostSplit, TestPriceJSON, TestModelPriceTiers, TestModelPriceTierCmd, TestAnthropicCacheWriteSplit and TestClaudeCacheWriteSplit. The ledger CSV test expects the new columns.
- model-price-tiers.test.cjs is new; model-price-edit and provider-price-rate are updated. All of them fail against the old assets and pass in WebKit. Chromium couldn't be launched here.
- go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
The index: session files are read in parallel and each line told from its first bytes (a Claude line's type, message head and usage; a Codex line's type and payload), so tool results, attachments and compactions are never decoded; a cold index of this machine's sessions takes about 32 s, then the page opens from ~/.cache/magpie/sessions.json (version 4, which re-indexes once) and only changed files are read again. The page keeps showing the last answer while a fresh one is computed, and GET /api/sessions/progress says how far a first run is, which the page shows in an animated hero instead of a spinner (reduced motion respected).
All models and All folders are searchable combo boxes that draw only the rows in sight, so 13k folders open at once.
Each day now counts the prompts typed, replies, tool calls by name and skills called (a subagent's calls and tokens count, its messages don't). The overview adds:
- Session shape (会话形态): sessions by messages (1–5 … 121+), minutes at work, or autonomy (tool calls a prompt, <1 … 21+), toggled and remembered;
- Tool use (Tool 使用): calls and sessions, the top tools with their kind's dot, calls, sessions and share, the kinds (Bash, Edit, Read, Write, Grep, Glob, Task, MCP tools, other) as a stacked bar and by week;
- Top skills (常用 Skills): each skill's calls, sessions, last use, agents' share and projects;
- the activity heatmap by Output tokens (输出 Token) and Messages (消息) beside Tokens, Sessions, Cost and Active.
No border stripes; a toggle leaves the page where it was.
Tested with TestStatsToolsAndSkills (Claude reply blocks laid out both ways, a Skill call, an MCP tool, /clear, an interrupt, a subagent; a Codex rollout's function and custom tool calls) and the stats tests, and internal/gui/tests/sessions-overview.test.cjs in Chromium and WebKit, en and zh: the new metrics and their tips, the shape's buckets and toggles (the page not moved), the tools' rows, kinds and weeks, the skills' lines, nothing overflowing at 1100 and 560 px. go vet, go build -tags nogui (and GOOS=linux, GOOS=windows), go test -tags nogui ./... and the GUI suite pass (panel-fold flaked once in the parallel run, passes alone).