From 012c584cc1c6da054aba4f9ae6be60db7e0a1ae7 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 20:28:05 +0800 Subject: [PATCH] usage: clear a model's usage, or all of it, from the Usage page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CrazyMouse_TT on X asked for a way to clear usage, all of it or one model's, to measure a local model fresh after deploying it. The Usage page's Models list has a "…" on each row (and a right-click menu) with "Clear usage…", and "Clear all usage…" sits under the lists below the "Counted from…" note. Each asks first, in a confirmation that says what is deleted, that it can't be undone, and that key spending limits and allowances still count the calls. A clear is a mark in settings (usageCleared: provider, model, unix ms), read at read time; usage.jsonl is never rewritten. The Overview hides calls up to the mark in summarizeFrom; the Requests page, heatmap and ledger/CSV hide them in Filter.keepsRow, their one choke point, and the request index's meta carries the marks so its cache is dropped. Key budgets, window holds and Codex reads go through usage.Visit and the raw log, so a clear never resets a key's spending limit or a quota. A later clear of the same model or of everything takes the place of an earlier one. The marks ride the settings part of a sync (sync-backup.md) and a save of the Settings page keeps them (api.go's keep list; the guard test TestSettingsSaveKeepsWhatItDoesNotSend caught it missing). gateway-sessions.md and sync-backup.md say so. Run on mote (mote-run) at this tree, rebased on origin/main c26b2992e (the Go packages and usage-clear.test.cjs; vet and the 68 GUI files ran at the earlier base f835bd713, four commits behind): - go test -tags nogui -timeout 45m ./internal/usage ./internal/settings ./internal/usageapi ./internal/gui: all ok after the keep-list fix (before it, TestSettingsSaveKeepsWhatItDoesNotSend red). - gofmt -l: clean. GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: exit 0. (Linux vet without nogui fails in wails' cgo, as on main.) - usage-clear.test.cjs: 32/32 in Chromium and WebKit, en/zh/zh-TW/ja/de, at 1200, 560 and 390 (web). With the old page.js/client.js/i18n.js it fails (TimeoutError). - 68 GUI files the change can reach (every usage-*, gui-de/ja/zh-tw, i18n-keys, click-scroll, idle-ticks, mobile-web, panel-usage, sessions-purge): 941/980. The reds are filed and red on main (usage-ledger run alone at origin/main 84a6a2c42 too): i18n-keys "masking…" (#4053), mobile-web (#3952), usage-agents-hide (#2276, #3414), usage-ledger "newest live route needs no return-to-live button" (#3104, run alone at main too). i18n-keys first also named "Clear usage…" in Go comments of this change; reworded. Broken by hand, each red: hides() always false (usage and gui tests), keepsRow's clear check removed ("Requests after the clear: 5 rows"), clearedKey out of the request meta ("after clearing everything: 3"), the api.go keep line removed (UsageCleared [] after a save). Not tried: the full go ./... suite and the whole GUI suite (mote was busy with other sessions' full suites), the real app, a sync between two computers. usage.jsonl doesn't shrink, and the Sessions tab's figures are not cleared. --- docs/subsystems/gateway-sessions.md | 22 +++ docs/subsystems/sync-backup.md | 6 + internal/gui/api.go | 2 + internal/gui/assets/app.css | 2 + internal/gui/assets/plugins/usage/client.js | 1 + internal/gui/assets/plugins/usage/i18n.js | 36 +++++ internal/gui/assets/plugins/usage/page.js | 53 ++++++- internal/gui/settings_keep_test.go | 1 + internal/gui/tests/usage-clear.test.cjs | 146 ++++++++++++++++++++ internal/gui/usage.go | 18 +++ internal/gui/usage_clear_test.go | 44 ++++++ internal/settings/settings.go | 106 ++++++++++++++ internal/settings/usage_cleared_test.go | 55 ++++++++ internal/usage/cleared.go | 110 +++++++++++++++ internal/usage/cleared_test.go | 101 ++++++++++++++ internal/usage/ledger.go | 4 + internal/usage/request_page.go | 2 +- internal/usage/usage.go | 7 + 18 files changed, 714 insertions(+), 2 deletions(-) create mode 100644 internal/gui/tests/usage-clear.test.cjs create mode 100644 internal/gui/usage_clear_test.go create mode 100644 internal/settings/usage_cleared_test.go create mode 100644 internal/usage/cleared.go create mode 100644 internal/usage/cleared_test.go diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index abd601aeb..9be8c64c5 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -74,6 +74,28 @@ Requests, the Requests ledger and model lists keep each call's own id. Verification: `TestUsageAliasCountsTwoIdsAsOne`, `TestSessionsCountAliasedModelsAsOne`, `TestAliasStatsLeavesItsInputAlone`. +The Usage page can clear its figures (CrazyMouse_TT on X): a model's row's +"…" has "Clear usage…", and "Clear all usage…" sits under the lists. Each +asks first, then `POST /api/usage/clear` adds a mark to `usageCleared` in +settings.json ([`settings.ClearUsage`](../../internal/settings/settings.go)): +a provider and model as the Models list names them (the alias, the +provider's shown id), or neither for everything, and the time in Unix +milliseconds. usage.jsonl is never rewritten. The usage pages leave out +each call at or before a mark that matches it +([`cleared.go`](../../internal/usage/cleared.go)): the Overview +(`summarizeFrom`), and the Requests page, its heatmap and the CSV through +`Filter.keepsRow`; the request index keys its meta on the marks +(`clearedKey`), so a cached page is read again. Gateway keys' spending +limits, the allowances' window holds and Codex's reads go by `usage.Visit` +and the raw log, so they still count every cleared call: a clear never +lifts a limit, and the confirmation says so. The marks ride the settings +part of a sync, so a clear holds on the computers it syncs with, over +their shared days and the agents' session-file rows too, and a later clear +of the same model or of everything takes an earlier one's place. Usage › +Sessions is not cleared. Verification: `TestClearedUsageLeavesThePages`, +`TestClearUsageKeepsOneMarkAModel`, `TestUsageClearRoute`, +`usage-clear.test.cjs`. + Claude Desktop gateway rows can have a local name even without a CLI transcript (#1675). [`DesktopTitles`](../../internal/sessions/desktop_titles.go) reads `claude-code-sessions/*/*/local_*.json` and diff --git a/docs/subsystems/sync-backup.md b/docs/subsystems/sync-backup.md index 2492fbb37..8128bed64 100644 --- a/docs/subsystems/sync-backup.md +++ b/docs/subsystems/sync-backup.md @@ -47,6 +47,12 @@ session resumes in (`SessionTerminal`) and what the menu bar shows. Launch at login is the system's own setting (`internal/autostart`) and the listen address is `MAGPIE_ADDR`, so neither is in a bundle. +The Usage page's clears (`usageCleared`, see +[gateway sessions](gateway-sessions.md)) are a preference, not a computer's +own, so they ride the settings part: a model cleared on one computer is +cleared on the others, their shared usage days included. The usage files +themselves are never rewritten by a clear. + The providers part works the same way for which account an agent is kept signed in to (`KeepLogin`, `KeepLoginAs`, `SwitchOnly`): one person keeps a personal account at home and a work account at the office, and sync set diff --git a/internal/gui/api.go b/internal/gui/api.go index 5d6ace58f..0dd5de632 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -1350,6 +1350,8 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { in.UsageAgentsHidden = cur.UsageAgentsHidden // and the names models' usage is counted under (magpie model alias) in.UsageAliases = cur.UsageAliases + // and the clears of usage made on the Usage page + in.UsageCleared = cur.UsageCleared // and the clients it counts as agents, set there too (#1958) in.ClientAgents = cur.ClientAgents // how agents' lists name models, set on its own for the agents to be told diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 573e06d91..fc040268f 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -1197,6 +1197,8 @@ body.masked :hover > .acct-eye, body.masked .pq-card:hover .acct-eye, body.maske .us-hid .list[hidden] { display: none; } .row.stat.hid > :not(.us-unhide) { opacity: .55; } .usage-note { padding: 10px 4px 2px; font-size: 11px; color: var(--faint); user-select: text; -webkit-user-select: text; } +.usage-clear { margin: 0; padding: 2px 0 6px; } +.usage-clear button { font-size: 11px; } .usage-pane { display: flex; flex-direction: column; flex: none; } .usage-pane[hidden] { display: none; } diff --git a/internal/gui/assets/plugins/usage/client.js b/internal/gui/assets/plugins/usage/client.js index 56a9ad637..31519a69c 100644 --- a/internal/gui/assets/plugins/usage/client.js +++ b/internal/gui/assets/plugins/usage/client.js @@ -50,6 +50,7 @@ magpie.plugin("usage", (ctx) => {

+