Commit Graph
8 Commits
Author SHA1 Message Date
yetone d4712e1312 usage: a probe's chunk among the settled days no longer drops the heatmap's disk cache
heatSettledKey gave up whenever a settled chunk had no serial. A probe's
chunk (MCP and search calls) is made again for each query, so it never
has one. When a user had no tool call in the last two days, that chunk
fell among the settled days, the settled count was never kept on disk,
and every Usage refresh paired and counted every day again.

The loop below already names such a chunk by its rows (hashRows), the
same rows give the same key, and pairSerials already allows one unnamed
chunk. So the early return goes; the doc comment says what false means
now.

Ran (mote, at this head):
- TestHeatSettledKeyNamesAProbeChunk: green; red with the early return
  put back ("a probe's chunk left the settled days unnamed").
- go test -count=1 ./internal/usage ok; go vet for darwin, linux and
  windows ok; gofmt clean.
Not tried: timing a real Usage page with no recent tool calls.

Reported-by: gua2100 (Discord #feedback)
2026-11-08 00:12:29 +08:00
yetone f2eb56c6bb usage: a first start pairs the history a few weeks at a time, not at once (#2278)
With no sums on disk (a first install, or a version that counts rows
otherwise), the page's cells, the heatmap's days and Codex's effort
before the cut were built from one pairing of the whole history. Every
gateway block was priced, every session row's message id went into one
set, and every call into one match map. On the owner's history that
was ~212 MB of the warm-up's 341 MB in use and the cold start's peak.

coldSpans builds them as a day's start advances them (pageAdvance),
14 days at a time. A span pairs the chunks with a row from heatReach
before it to heatReach after. Each chunk's oldest row is kept by its
serial. A gateway block not priced yet is priced by a pricer of the
span alone, and its rows go back to the pool after the span. The
recent days are then paired as on any kept start.

The page's cells come out in another order (by span, then chunk), so
TestSettledCountedAsVersioned now sorts them before it hashes. The
sorted hash is the same for a pairing at once and in spans, so
settledCountVersion stays. Tests that count a pairing of every chunk
use pairAtOnce (coldSpan 0).

Run on mote at the pushed head (rebased on b62fa332f): gofmt -l
clean; go vet ./internal/usage for darwin, linux and windows ok;
go test -count=1 ./internal/usage ok (20.0s); go build . ok.
Broken by hand: TestFirstBuildPairsSpans goes red when a span pairs
only its own days (lo, hi := from, to): the effort differs.
Bench, real history (a year), cold first start, against v0.1.2256,
two rounds each, on mote at load ~22:
  no GUI polls:  footprint peak 451, 407 MB (2256: 493, 484);
                 live heap max 318, 289 MB (2256: 367, 362)
  GUI polls:     footprint peak 483, 410 MB (2256: 534, 532);
                 live heap max 316, 290 MB (2256: 395, 406)
The heatmap's answers are byte-equal to 2256's. The request page's
differ only in float digits from the summation order (232 floats,
largest relative difference 6.5e-14); every other value is equal.
Not tried: a start on Windows or Linux.
2026-10-31 03:13:28 +08:00
yetone ceef9e9438 usage: the heatmap's settled-days key names the filter's zone, not its pointer (#2278)
8d4c07ffe (v0.1.2156) named the heatmap's settled days with %#v of the
Filter, and the page's filter carries Zone, a *time.Location: %#v
writes its address, another in each run. So heat-v1.json never matched
on a start and every start paired the whole history for the heatmap
again, the cost 8d4c07ffe was to take away. Its bench start matched by
chance.

The zone is now written by its name, the rest of the filter as before.

Found with a key-parts log on mote (~/r2278/full): two starts of one
binary wrote headers differing only in Zone:(*time.Location)(0x1089b81e0)
vs (0x105d8c1e0). After the fix three starts of a build with it: the
first (new binary) and second wrote heat-v1.json, the third read it,
and its heap at 18 s has no heatSplit (was 83 to 95 MB in each start
before).

TestHeatSettledKeyNamesTheZone: two FixedZone("Z", 3600) give one key,
nil or another zone a different one. Red at 8d4c07ffe's heat_disk.go
("one zone, two keys"), green here.

Run at this tree, cherry-picked onto d05482bab (mote; first
run on c28cc5c87 the same): go test -count=1 ./internal/usage ok (16.4s); gofmt -l clean; go vet -tags
nogui ./internal/usage for linux, windows, darwin ok.
2026-10-30 14:39:22 +08:00
yetone 8d4c07ffed usage: the heatmap's settled days are kept on disk, so a start pairs only the last days (#2278)
The heatmap's days before the cut (two days ago) came from heatOld, a
full pairing of the whole history kept in memory. A started magpie made
it again on its first heatmap, and again every heatOldFresh (10 min):
on the 52G Codex + 13G Claude bench history that was heatSplit's 88 MB
of the cold start's ~325 MB in-use heap at 18 s.

The days are now kept in heat-v1.json beside the sessions cache, under
a key (heatSettledKey) naming what they were counted of: the request
code and the index's root and meta (prices, names), the cut, since,
zone, filter, the recent chunks' rows before the cut, the Codex thread
identities, and each settled chunk (a session file's by path, size and
mtime, like requests-v1.bin; any other chunk by its rows). The file is
written through a temp file renamed over the last. A key that can't be
named (a chunk with no serial) pairs as before; after heatOldFresh a
same-named key keeps the days without pairing again.

Measured on mote, ~/r2278/full, cold start + the page's 10 endpoints
polled every 5 s:
- heap at 18 s: v0.1.2149 324.6 MB in use, heatSplit 88.3 MB,
  HeatmapOf 135.8 MB; this 310.1 MB, no heatSplit, HeatmapOf 44.1 MB.
- /api/usage/heatmap at 40 s: byte-for-byte the same as v0.1.2149's.
- gcpeak 120 s, 3 rounds each, alternated (mote under other load):
  live 360/393/414 vs 378/404/387 MB, gcs 148/174/168 vs 201/209/160.
  The peak moves to CodexModelEffort's own full pairing (91 MB), next.
- First start, no file yet: pairs as before and writes 7.9 KB.

Tests: TestHeatmapAfterACallPairsOnlyTheLastDays now removes the file
for its cold oracle, and past heatOldFresh with nothing changed pairs
less than all, then a new settled file pairs all and equals cold.
TestHeatmapSettledDaysFromDisk: restarted with memory cleared pairs
less than all and gives the same heatmap; another filter, a rewritten
settled file, and no file each pair all.

Run at this tree (mote): go test ./internal/usage ok (71s);
go test -tags nogui ./internal/gui -run 'Heat|Usage' ok, usageapi ok;
go vet -tags nogui ./internal/usage/... ./internal/gui/... on linux,
windows, darwin ok; gofmt -l clean. Rebased onto e8d42c02d, then cae785a37 (no
internal/usage change in either); at e8d42c02d go test ./internal/usage ok
(100s) and linux vet ok again. Not run: the full suite.
2026-10-30 14:19:41 +08:00
yetone d7e9c37454 usage: a call pairs again only the heatmap's last days, not its year (#2278)
Each call through magpie changes the gateway's newest block, so the
heatmap's 30 s reuse missed and HeatmapOf paired a year of gateway rows
against every session file again: 15% of magpie's CPU on real history
(HeatmapOf -> pairRows 5.2 s of 32.7 s sampled), ~2.3 s a refresh, and a
large share of the allocation the GC marks.

heatSplit keeps the days before cut (00:00 two days ago) from the last
full pairing, with the serials of the chunks that can't reach past cut
(newest row more than heatReach = 48 h before it). A call then pairs only
the recent chunks, from cut on (pairRowsIn), and adds their days to the
kept ones. Every chunk is paired again when:
- a settled chunk changed or went (its serials differ), or
- the recent chunks' rows before cut changed: a session file copied in or
  a rollout forked from an old one holds calls of then (heatBefore, a
  count, time and token sum of those rows), or
- cut moved (a new day), or the kept pairing is heatOldFresh (10 min) old.
heatReach covers a call paired across cut: a full gateway block whose
newest call is before cut is still paired with the session row after it.

TestHeatmapAfterACallAddsUpOnlyWhatChanged's gateway now has a call of
the last days before its first build, as a gateway in use does: with
only a call of 40 days ago its block went from settled to recent at the
call, which is a full pairing by design.

Run on mote, heat2278 at this head (based on fb5959553, rebased on
b2d005bf3, which changes internal/gateway and nothing under usage):
- GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage/...
  ./internal/usageapi/... ./internal/gui/...: ok
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...: ok,
  and again with vet after the rebase
- ./internal/gui/...: TestMainWindowCorners/macOS_26_SDK red, #3949
  (mote's macOS 27; nothing here reaches it); every other test ok
- broken by hand, TestHeatmapAfterACallPairsOnlyTheLastDays goes red:
  recent days after cut+1 dropped; heatReach 0 (the cross-cut gateway
  block); the heatBefore check removed (both the rewritten mixed file and
  the forked file, each alone)

Real history on mote (the owner's usage.jsonl and session files copied
into a sandbox HOME, GUI polled every 5 s, the gateway fed 4 calls/s,
load ~30 from other sessions), v0.1.1956 build vs this one:
- /api/usage/heatmap mean 2.35 s -> 1.53 s; all polls 0.93 s -> 0.74 s
- HeatmapOf 0.89 s of 28.35 s sampled (3.1%), heatSplit itself 0.25 s,
  from 15%
- average CPU after the feed starts 61.2% -> 59.0%; peak RSS 1103 -> 1065 MiB
What is left of the heatmap's wait is readRequestState and the pages it
shares a lock with, not its pairing.
2026-10-29 05:04:50 +08:00
yetone b69f8e96af usage: a call through magpie adds up the heatmap only for the chunks it changed (#2278)
A call through magpie changes the gateway's log, so the heatmap's kept
page (heatFresh) is no use and the Requests tab's refresh read every
row of the year again: unpacked each session file's chunk and summed its
days. While an agent worked through magpie that was every refresh.

The heatmap now keeps each chunk's day sums (chunkPart.days, in
requestIndex.heatParts, apart from the pages' cells so neither pushes
the other's out), under the same pairing signature the cells use, and
adds up again only a chunk whose rows or pairing changed. A call is
still counted at once. pairedRows.visit had no other caller and is gone.

Measured on mote at #2278's scale (synthetic HOME: 15 GB of Codex
rollouts, 1.9 GB of Claude sessions, 240k-row usage.jsonl), the GUI's
polls every 5s and 4 gateway records appended a second, against
v0.1.1920:
- CPU of a 60s profile while appending: 57.9s -> 42.1s;
  HeatmapOf 11.5s -> 6.7s, of it the visit 4.8s -> ~0
- /api/usage/heatmap mean 2.79s -> 1.69s; process CPU average
  107% -> 83%
- peak footprint unchanged (579 MB, 607 MB this run: noise; what is
  left is pairRows' pairing of the year, next)

Test: TestHeatmapAfterACallAddsUpOnlyWhatChanged (8 session files and
a gateway call: after one more call, at most 1 chunk is added up again,
the call is counted, and the sums equal a cold build). Broken by hand
(old sums never taken): red, "a call added up 9 chunks again".

Run at the pushed head (rebased on fc6f1d5e8, no internal/usage change
in between, rerun after): go test ./internal/usage ok; GOOS=linux,
windows, darwin go vet -tags nogui ./internal/usage ./internal/gui ok;
go test ./internal/gui before the rebase: ok except
TestMainWindowCorners, #3949 (macOS 27 on mote). Not run: the whole
suite.
2026-10-28 23:44:37 +08:00
yetone cf8b1e9338 usage: the heatmap works out only its days, kept 30s while only session files change (#3493)
The Requests tab asks for the year's heatmap on every ledger refresh. A
live agent writes its session file all the time, so the page's key
changed for every ask and the whole year was worked out again: groups,
shares and series the heatmap never reads, over ~289k calls on the
owner's real history.

- buildRequestBlocks returns at once for the heatmap's period with only
  HeatmapOf's days, from the rows the chart filter keeps.
- queryPage keeps a heatmap page 30s (heatFresh) while the gateway
  log's snapshot version is the same; a meta change (prices, names,
  wires) drops it, and a call through magpie shows at once.

Measured at the handler (internal/gui, sandbox HOME with the owner's
real ~/.codex and ~/.claude, 15,553 sources, 59.9 GB): heatmap steady
refresh 2.99s -> 0.15s, cold 5.5s -> 4.5s.

Run at this tree: go test -count=1 -timeout 45m ./internal/usage
./internal/usageapi ok. TestHeatmapKeptWhileOnlySessionsChange is red
with the throttle taken out (builds counted twice).
2026-10-24 22:58:26 +08:00
yetone d1d4b7ed2a usage: a heatmap of the last 53 weeks on the Requests tab, by tokens, requests or cost (#1369, nianlee-official)
What changed: the Requests tab of the Usage page shows a heatmap card like
CC Switch's: a cell per day for the last 53 weeks, Monday on top, month
names over the weeks and weekday names at the edge, a Tokens / Requests /
Cost switch (kept in localStorage), a five-shade Less…More legend, and a
tip on each day with its date, tokens, requests and cost.

How: GET /api/usage/heatmap (usage.HeatmapOf) reads QueryPage's cached
request index over a hidden period, "heatmap", which starts on the Monday 52
weeks before this week's. Its rows are summed by local calendar day in both
the compact and the ledger path. So no raw log is scanned per render, and the
days count what the chart counts:
- the page's agent/provider/model/key filters, but not the period or day
  picked;
- the gateway's own rejections left out;
- cost of priced calls only, with an unpriced count.
The days are in the local time zone and the shades use theme tokens
(.l0–.l4, as in the sessions calendar). There are no left-border accents and
no native <select>. A click never scrolls the page. A narrow window scrolls
the card sideways inside itself, with the newest week in sight (kept there
as it's laid out or resized, until the reader scrolls back), and the weekday
names stay at the card's edge. The card is hidden when no day has calls. New
strings are in zh, zh-TW, ja and de.

Run:
- go test -tags nogui ./internal/usage ./internal/gui (new TestHeatmapDays,
  TestHeatmapOf, TestUsageHeatmapRoute); gofmt -l clean; go vet darwin,
  GOOS=linux and GOOS=windows go vet -tags nogui on both packages; go build
  for darwin, linux and windows.
- New internal/gui/tests/usage-heatmap.test.cjs in Chromium and WebKit, at
  420 and 1100, in en, zh, zh-TW, ja and de, plus dark and the empty case.
  It checks:
  - the cells, labels, legend and the busiest and quiet days;
  - the newest week in sight and only the card scrolling sideways;
  - the tip's text and that it stays inside the card;
  - that a metric click moves nothing and is kept;
  - that the filters, but no period or day, are sent.
- Breaking checks: with the keep-at-end guard reverted, 5 of its 11 subtests
  fail in WebKit; with the server's heatmap dropped, TestUsageHeatmapRoute
  fails.
- i18n-keys, click-scroll, gui-de, gui-ja, gui-zh-tw, sessions-usage-open
  and all usage-*.test.cjs GUI tests pass in both engines (161 each).
- A sandboxed `magpie web` (temp HOME, 2605 fake calls over 400 days, ports
  3671/3672): the card fills 369 days at 1280 and 420 wide, and the tip
  reads "2026年10月1日周四 / 2.6M token · 23 个请求 · ≈$1.05+".
2026-10-09 11:33:47 +08:00