Files
magpie/internal/tui/pages.go
T
forever e961836fb0 usage, gateway, gui, provider, cli: the tests pass when a run crosses midnight, and a usage answer reads the clock once (#1046)
* usage: the usage tests pass at any time of day, midnight included

The usage tests stamped their calls with the machine's time and then
asked for today, 7 or 30 days, or a cached answer, which the code
worked out from its own time.Now a moment later. A minute after
midnight a call made "a minute ago" was yesterday's, and when midnight
fell between the test's read and the code's, the call and the period
were of two days. magpie usage, the TUI's Usage page, the two Codex
cache-write tests and the computers' shared days failed in the first
minutes after midnight (the TUI's after midnight UTC, the zone its
package's tests run in since 1caea638), and 28 more failed when
midnight fell inside them.

usage.Clock is now the time every period is read by: the start of
today, 7 days and 30 days (LedgerOf, Summarize, Direct, QueryPage), the
hours or days of the chart (LedgerSeries, QueryPage), the day a cached
summary or page is for, the time Append gives a call that has none,
the days davsync shares and the date in an exported CSV's name. It is
time.Now, as before, and a variable for the tests, like LogCalls. Each
test that built its calls from the time now holds Clock at noon on
2026-09-30 (holdClock; holdUsageClock in gui and the CLI; three lines
in tui and davsync) and stamps its calls from that instant, among them
TestReasoningReplyTimedByItsAnswer, whose timeline rows 117b6c7f made
at time.Now. TestPeriodsReadTheClock checks that every one of these
reads goes through Clock. TestQueryPageSourceAndIdentityInvalidation is
held too; that it fails when run alone is fixed apart from this, in
#1045.

Seven held tests could not fail at midnight before, and are held so
that the tests read one clock: TestUsageSameSizeAndMtimeRewrite (All's
totals), TestDirectCountsOnlySessionFileCalls (30 days around its own
noon), TestQueryPageIOOutsideCacheLock (a file two days old), gui's
TestUsageLedgerDayRoutes (7 days hold its calls from either day),
TestLedgerSingleSnapshotAndLocalSession and
TestLedgerNamesModelAtProvider (their calls are stamped after the code
reads the time), and TestUsageRawAndPricedChunksShareBudget, which
fails only when the midnight in it starts a new week of All's chart, on
a Sunday night; at.sh sets the time of day and not the weekday, so that
one was not shown.

Verified with at.sh, a scratch script that runs a test binary under a
zone made with zic, so that the local time of day is the one asked for
as the run starts (HOME in a sandbox, Go's caches pinned). The runs were
first made on c5a54bc7, and those marked (698da36f) again on 698da36f,
with the same results. The tui package runs in UTC since 1caea638, a
zone at.sh doesn't move, so its runs on 698da36f used copies of both
trees whose TestMain keeps the zone TZ sets.

Before, at 00:00:30, 00:01:30 and 12:00:00 (698da36f):
  TestUsageShowsCallsNotThroughMagpie, 00:00:30:
    usage_cli_test.go:95: no calls today · route an agent through
    magpie and its usage shows up here
  00:01:30: "5 tokens today · 1 call · no price", without the calls
  not through magpie; TestUsagePageShowsCallsNotThroughMagpie (tui)
  fails the same two ways at usage_test.go:138
  --- FAIL: TestCodexCacheWriteNotInInput (00:00:30)
    codex_cache_write_test.go:104: rows []
  --- FAIL: TestCodexCacheWriteMatchesGatewayCall (00:01:30)
    codex_cache_write_test.go:73: filter {...}: 0 rows, 0 calls, want
    the gateway's 2: []
  --- FAIL: TestUsageSharedBetweenComputers (00:01:30)
    usage_share_test.go:134: a's days on the server:
    [...-2026-10-05 ...-2026-10-06 ...-2026-10-07]
  12:00:00: PASS

Midnight inside a test, in scratch copies of the head. Started at
23:59:59.3 with the code's first period read held 1.5s, so that it
reads after midnight what the test stamped before it, 16 usage tests,
3 gui tests and TestUsageCSV FAIL, among them
  ledger_test.go:257: today: 0
  review_test.go:69: chart counted 0
  incremental_log_test.go:202: concurrent append lost
  TestCachedSummaryRemainsCallerOwned: panic: runtime error: index out
  of range [0] with length 0
  usage_test.go:121: second page: {... Period:today Rows:[] ...
  Total:0 ...}
  usage_test.go:311: "": magpie-requests-7d-2026-10-06
Started at 23:59:55 with the test waiting past midnight between two of
its reads, 7 more FAIL:
  request_day_test.go:45: selecting a day changed the chart or its
  legend
  cache_bounds_test.go:84: uncached today summary stale
  growing_rewrite_test.go:71: today summary stale
  cache_bounds_test.go:153: page key changed instead of exercising the
  same cache
  priced_blocks_test.go:55, caller_provider_test.go:129 and :172: page
  differs
TestReasoningReplyTimedByItsAnswer, with a scratch 1.5s sleep before
its LedgerSeries, started at 23:59:59.3 (698da36f):
  usage_test.go:255: part: {Calls:0 Tokens:0 Cost:0 Timed:0 TTFT:0
  DecodeMs:0 DecodeOut:0}

After: the same at.sh runs PASS at 00:00:30, 00:01:30 and 12:00:00
(698da36f); the same scratch copies made of this change PASS, each
test taking 1.5s or 5.2s, so midnight did fall inside it (698da36f for
TestReasoningReplyTimedByItsAnswer's); and every changed test PASS at
23:59:59.9, 00:00:30 and 12:00:00 (698da36f).

Without the change (698da36f): with the reads put back on time.Now, 21
usage tests, 6 gui, 2 CLI, the tui and the davsync test fail, among
them
  --- FAIL: TestPeriodsReadTheClock
    clock_test.go:55: Direct(Today): 0 calls since 2026-10-06 00:00:00
    -0700 PDT, want 1 since 2026-09-30 00:00:00 -0700 PDT
    clock_test.go:63: Summarize(Today) the next day: 1 calls, want 0
  --- FAIL: TestReasoningReplyTimedByItsAnswer
    usage_test.go:255: part: {Calls:0 Tokens:0 ...}
Each of the eleven reads put back alone fails a test:
TestPeriodsReadTheClock for the nine in usage (LedgerSeries:
"clock_test.go:52: LedgerSeries(Today): 0 calls, want 2"; the page's
cache key: "clock_test.go:66: QueryPage(Today) the next day: 1 rows,
want 0"), TestCSVStamp for the CSV's name (usage_test.go:325: "":
magpie-requests-7d-2026-10-06) and TestUsageSharedBetweenComputers for
davsync (usage_share_test.go:224: all computers: 4 rows, 707 in; want
5, 737).

On this commit: go test -tags nogui -count=1 . ./internal/usage
./internal/tui ./internal/davsync ./internal/gui ok; -race -count=20
over the changed tests of each of these packages ok; gofmt -l prints
nothing, and go vet -tags nogui on the five packages finds nothing.

* usage: pages, CSVs and magpie usage asked at midnight show one day

An answer about usage read the time more than once. The Requests page
read it for the day it is kept for, for the period its log and session
files are read over, for the period of its rows and for its chart's
hours; the page from the ledger (QueryPage's path when LogCalls is
set, as in the tests) for its rows and for its chart; Direct for its
period and for its rows; a CSV for its rows and for its name;
magpie usage and the TUI's Usage page for Summarize and then for
Direct. Most of these reads have the log and the session files read
between them, seconds on a large log, and when midnight fell between
two of them the answer was of two days: yesterday's calls listed over
today's empty chart, Direct starting yesterday and counting none of
yesterday's calls, the calls not through magpie left out of magpie
usage and the Usage page (they were the new day's, none yet), and
yesterday's calls saved as magpie-requests-today-<the new day>.csv.

Each answer now reads Clock once and passes that moment down. queryPage
reads it for its key, its sources, its rows and its chart
(buildRequestBlocks takes it); the page from the ledger reads its rows
and its chart at it (LedgerOfAt, ledgerSeriesAt); Direct reads the
ledger at the moment of its period. usage.Summaries(p) is Summarize(p)
and Direct(p) at one moment, and magpie usage and the TUI's Usage page,
when a period is picked and when it is read again, ask for it: one
exported name, the clock left inside usage and one line changed at
each caller, where SummarizeAt and DirectAt would be two names and
each caller would read the clock itself. The CSV download and export
read their rows with the new usage.LedgerOfAt at the moment their file
is named for, which csvStamp now takes; the rows and the name are read
in two packages, so LedgerOfAt is exported. LedgerOf, LedgerSeries,
Summarize, Direct and QueryPage keep their signatures, each reading
Clock once.

The new tests set Clock to read a tenth of a second before midnight
the first time and a tenth of a second after it from then on, set
again for each answer. The midnight is the one that starts 2026-10-01,
local time: a past instant, at midnight because that is what is
tested, and the day before it is the 2026-09-30 the held tests use.
TestAnswerReadsTheClockOnce asks QueryPage(Today) from the indexed log
and from the ledger: its 3 rows, one from a session file written that
day at 23:00, are under that day's chart, and asked again after
midnight the page is the next day's, not the one kept for the day
before; Direct(Today) counts the call and starts that day.
TestUsageAskedAtMidnight (magpie usage today), TestUsagePageAtMidnight
(the TUI's Usage page, today picked and read again) and
TestUsageCSVAtMidnight (download and export) check that the calls
through magpie and those not through it, or the rows and the file's
name, are of the day asked on. TestCSVStamp passes its time.

Verified on the previous commit, at.sh as there. The runs were first
made on c5a54bc7, when the tui package's tests still ran in the local
zone, and those marked (698da36f) again on 698da36f, with the same
results.

Before: the new tests on the previous commit, at any time of day
(at.sh at 12:00:00 too) (698da36f):
  --- FAIL: TestAnswerReadsTheClockOnce
    clock_test.go:131: QueryPage(Today): 0 rows, 0 in a chart from
    2026-10-01 00:00:00 -0700 PDT; want 3 under one from 2026-09-30
    00:00:00 -0700 PDT
    clock_test.go:141: QueryPage(Today): 3 rows, 0 in a chart from
    2026-10-01 ...; want 3 under one from 2026-09-30 ...
    clock_test.go:145: Direct(Today): 0 calls since 2026-09-30
    00:00:00 -0700 PDT, want 1 since 2026-09-30 00:00:00 -0700 PDT
  --- FAIL: TestUsageAskedAtMidnight
    usage_cli_test.go:154: 5 tokens today · 1 call · no price ...,
    with no part for the calls not through magpie
  --- FAIL: TestUsagePageAtMidnight
    usage_test.go:184: today picked: ... (the same)
    usage_test.go:187: read again: ...
  --- FAIL: TestUsageCSVAtMidnight
    usage_test.go:367: download: 200, attachment;
    filename="magpie-requests-today-2026-10-01.csv" with 2 rows
    (<nil>), want magpie-requests-today-2026-09-30.csv with 2
    usage_test.go:378: export: 200
    {"path":"~/Downloads/magpie-requests-today-2026-10-01.csv",
    "rows":1}, want magpie-requests-today-2026-09-30.csv with 1 row

With the real clock, in scratch copies of both trees: scratch tests
held one of Clock's reads 1.5s after it was made, as reading a large
log does (the first; for one of them the page's third, its chart's),
and at.sh started each at 23:59:59.3. Before, all 9 FAIL, among them
  asked 23:59:59.564, answered 00:00:01.068
  Direct(Today): 0 calls since 2026-10-05 00:00:00 -0940 MID, want 1
  since 2026-10-05 00:00:00 -0940 MID
  QueryPage(Today): 2 rows, 0 in a chart from 2026-10-06 ...; want 2
  under one from 2026-10-05 ...
  download: attachment; filename="magpie-requests-today-2026-10-06.csv"
  with a call of 2026-10-05 23:59
After, all 9 PASS, answered at 00:00:00.9 to 00:00:01.0, so midnight
fell inside each, except the chart's: its held read is gone with the
change. A scratch sleep before the chart shows that one: before "2
rows, 0 in a chart from 2026-10-06", after PASS, asked 23:59:59.344
and answered 00:00:00.941. At 12:00:00 all of them PASS in both trees.

After: every changed test PASS with at.sh at 23:59:59.9, 00:00:30 and
12:00:00 (698da36f).

Without the change (698da36f): with the production put back to the
previous commit's (gui/usage.go only so far that TestCSVStamp compiles:
csvStamp takes the time, read apart from the rows as before), go test
-count=1 . ./internal/usage ./internal/tui ./internal/gui fails the four
new tests and no other, with the lines above. Each of the 16 places put
back alone fails one of them: the page's key (clock_test.go:134: "3
rows, 3 in a chart from 2026-09-30 ...; want 0 under one from 2026-10-01
..."), its sources (:131: "2 rows"), its rows (:131: "0 rows"), its
chart (:131), the ledger page's rows and chart (:141), LedgerOfAt (:141,
:145), ledgerSeriesAt (:141), Direct (:145); Summaries, indexedSummary
and magpie usage (usage_cli_test.go:154); Summaries and the TUI's period
key and reload (usage_test.go:184, :187); the CSV's download and export
(usage_test.go:367, :378).

On this commit: go test -tags nogui -count=1 . ./internal/usage
./internal/gui ./internal/tui ./internal/davsync ./internal/gateway ok;
-race -count=20 over the changed tests of each package ok; gofmt -l
prints nothing, and go vet -tags nogui finds nothing.

* gateway: the key-limit tests pass when a run crosses local midnight

A go test run that crossed local midnight could fail the gateway's
key-limit tests. keyLimited and the /v1/magpie/limit endpoint each read
time.Now() when they check a key, and access.Window turns that into a
calendar window (a day from midnight, a week from Monday, a month from
the 1st). The tests seed "yesterday" from their own time.Now() and then
make several calls, so a run that crossed midnight saw its calls split
over two windows: the second call of a 350-token day was let in, or a
restart counted nothing.

The gateway's key-limit path now reads limitClock, a variable for the
tests like ruleClock and idClock, and production behaviour is unchanged
(it is time.Now). TestKeyLimitRefusesBeforeUpstream,
TestKeyLimitHoldsParallelRequests and TestKeyLimitWindowResets pin it
at 2026-09-30 12:00 local and seed "yesterday" from that instant. Calls
are still recorded at the real time, after the pinned one, and a window
counts every call from its start, so they all count in the pinned
window; the "tomorrow's window" check in TestKeyLimitWindowResets stays
on the real now, the first window with none of those calls in it. No
other test sets a key limit with records in its window (the gateway-key
CLI test and the GUI caller-key tests count 0 calls or have no limit).

TestKeyLimitRefusesBeforeUpstream now also checks that the 429's
Retry-After and X-Magpie-Limit-Reset name the next midnight after the
check, and that the limit endpoint's week starts on the pinned
Monday.

Verified with at.sh, a scratch script that runs the test binary under
a zone made with zic, so that the local time of day is the one asked
for as the run starts:
- before, at.sh at 23:59:59 with COUNT=500/300 and FRAC 0.3/0.6/0.8:
  3 of 6 runs of each test failed with
  key_limit_test.go:217: spent key let in
  key_limit_test.go:117: a restart forgot what the key used 200
  key_limit_test.go:121: status &{Period:day Start:2026-10-06 ...
  Calls:0 Tokens:0 ...}
- after, the same 6 runs (COUNT=500, both tests) print PASS, and so do
  ^TestKeyLimit at 12:00:00 and 00:00:00 with COUNT=20.
- go test -tags nogui -count=1 ./internal/gateway: ok (79.5s);
  -race -count=20 -run '^TestKeyLimit' ./internal/gateway: ok.
- gofmt -l prints nothing; go vet -tags nogui ./internal/gateway is
  clean.

The month boundary (TestKeyLimitHoldsParallelRequests) and Monday can't
be reproduced by changing the time of day; the pinned clock covers them
the same way.

Fails without the change (tests pinned, keyLimited back on time.Now):
--- FAIL: TestKeyLimitRefusesBeforeUpstream
    key_limit_test.go:107: headers map[... Retry-After:[80228]
    X-Magpie-Limit-Reset:[2026-10-07T00:00:00-07:00] ...]
and with only the limit endpoint back on time.Now:
--- FAIL: TestKeyLimitRefusesBeforeUpstream
    key_limit_test.go:159: limit endpoint {... "period":"week",
    "start":"2026-10-05T00:00:00-07:00" ...}

* gateway: the routing history tests pass when a run crosses midnight

TestHistoryKeepsDaysAndDropsOld and TestRemoteMagpieGroupHistory took
now := time.Now() and saved routes at now and now+1s. saveRoute files a
route by its own day (r.Time.Local()), so a test started at 23:59:59 put
the +1s route in tomorrow's file and History(today) found one route
less. TestHistoryKeepsSessionAndTokenTiers raced the other clock:
saveRoute prunes by time.Now(), so when the day turned between the
test's time.Now() and the first saveRoute, the prune gzipped "today"
after the first route, the second went to a new .jsonl, and the test's
own pruneHistory gzipped that over the first .gz, losing route 1.

saveRoute now prunes by historyClock (time.Now outside the tests). The
three tests take historyNoon(t), which holds that clock at
2026-09-30 12:00 local and returns it as their now, restored with
t.Cleanup. Every route, day file and prune is then on that one past day.
The fixture can't be a fixed past day without the clock: pruning by
the real today gzips that day's file at once (and, a month on, drops
it as too old).

The other history tests keep the real clock:
TestHistoryRouteOutsideDayLimit writes its day files itself from one
now and the code under test reads no clock, and
TestHistoryRouteDayAndNoLock already uses a fixed day.

Verified with at.sh, a scratch script that sets the local time of day
through a zone made with zic as the run starts:
- before, at.sh ./internal/gateway at 23:59:59:
  --- FAIL: TestHistoryKeepsDaysAndDropsOld
      history_test.go:50: days [{2026-10-06 1} {2026-10-05 1} {2026-10-04 1}]
  --- FAIL: TestRemoteMagpieGroupHistory
      remote_group_served_test.go:144: routes [... only ID 1 ...]
  and, with a 2s sleep after now := time.Now() standing in for the turn
  of the day, --- FAIL: TestHistoryKeepsSessionAndTokenTiers
      history_test.go:22: compressed history lost accounting: [only ID 2]
  12:00:00 control: PASS.
- after: the three tests PASS at 23:59:59, 23:59:59.9 (FRAC=0.9),
  12:00:00, and 23:59:59 with the same 2s sleep; all TestHistory* and
  TestRemoteMagpieGroupHistory PASS at 23:59:59 with -test.count=3.
- with saveRoute pruning by time.Now() again (the fixtures still pinned),
  at any time of day:
  --- FAIL: TestHistoryKeepsSessionAndTokenTiers
      history_test.go:32: compressed history lost accounting: [only ID 2]
- go test -tags nogui -count=1 ./internal/gateway: ok;
  -race -count=20 on the three tests: ok; gofmt -l and
  go vet -tags nogui ./internal/gateway print nothing.

* gateway, davsync: the request archive tests pass when a run crosses UTC midnight

The archive names a call "<date>/<id>" from the call's own start, in
UTC (archiving in archive.go). TestArchiveOn read time.Now().UTC()
before posting and TestRequestArchiveToS3 read it before post(), and
each expected that read's date. A run where UTC midnight (17:00 PDT)
fell between the test's read and the request's start expected the day
before the one the archive used, and failed.

Both tests now take the date from the Time the call carries
(s.Recent()[0].Time, the same start the archive's name is made from),
in UTC, so nothing is read from the clock a second time. No production
code changes. The other archive tests already go by the call's Archive
name, and internal/gui's archive test uses a fixed name; no other test
in internal/gateway, internal/davsync or internal/gui predicts an
archive key or a UTC-dated name from its own clock.

Verified: at.sh only moves the local time of day, and this is a UTC
crossing (UTC midnight was ~16h away), so it was shown in a scratch
copy whose serve starts each request 0.2s after the next UTC midnight,
as a request does when the test read the clock just before it. There
the old tests fail:
  archive_test.go:115: call archive "2026-10-07/000000-f4690de23ee8a859",
  want "2026-10-06/000000-f4690de23ee8a859"
  archive_test.go:93: nothing at
  "team x+y/magpie/archive/2026-10-06/000000-0e74a87816ef6a56.json"
and the new ones pass. Unshifted, under at.sh at local 23:59:59,
00:00:00 and 12:00:00, both pass, as the old ones do at 23:59:59 and
00:00:00 (local time doesn't enter the name). With archiving naming by
start.Format (local) instead of start.UTC().Format, both new tests fail
under at.sh at local 23:00:00, where the local date is a day behind
UTC's:
  --- FAIL: TestArchiveOn ... archive_test.go:116: call archive
  "2026-10-05/082500-7322712bd89f71da", want
  "2026-10-06/082500-7322712bd89f71da"
  --- FAIL: TestRequestArchiveToS3 ... archive_test.go:95: nothing at
  "team x+y/magpie/archive/2026-10-06/082500-3587ab4192da2cd5.json"
and pass at 12:00:00. go test -tags nogui -count=1 ./internal/gateway
./internal/davsync passes in a sandbox HOME; -race -count=20 -run
'^(TestArchiveOn|TestRequestArchiveToS3)$' passes; gofmt -l and go vet
-tags nogui on both packages are clean.

* gui: TestWhatsNewTodayRoute passes when the run crosses local midnight

TestWhatsNewTodayRoute posts {"on":true} to /api/whatsnew/today, which
writes today() to whatsnew-quiet-day, then checks quietToday(), which
compares the file with a second today(). A run that crossed local
midnight between the two read different days and failed with
"on: 204 false". The neighbouring TestWhatsNewQuietAfterRestartToUpdate
already pins today; this test didn't.

The test now pins today to 2026-09-30 (restored with t.Cleanup), so the
post and the check read the same day. The other What's new tests start
from an empty folder, so quietToday finds no file and never reads
today().

Verified: with a temporary 1s sleep between the post and the check, the
unpinned test under at.sh (local time of day set by a zone) started at
local 23:59:59.5 failed with "whatsnew_test.go:237: on: 204 false" and
passed at 12:00:00; pinned, it passes at 23:59:59.5 (same sleep), and,
without the sleep, at 23:59:59, 00:00:00 and 12:00:00. With
setQuietToday writing time.Now().Format("2006-01-02") instead of
today(), the test fails: "--- FAIL: TestWhatsNewTodayRoute ...
whatsnew_test.go:239: on: 204 false". go test -tags nogui -count=1
./internal/gui passes in a sandbox HOME; -race -count=20 -run
'^TestWhatsNewTodayRoute$' passes; gofmt -l and go vet -tags nogui
./internal/gui are clean.

* provider: TestZCodeStartSentAsTheApp passes when the run crosses midnight

A ZCode Start Plan request opens with a context prefix that says
"Today's date is <day>" (zcodeDateReminder), the day zcodeStartRequest
took from its own time.Now(). TestZCodeStartSentAsTheApp sent the
request, then built the expected prefix from a second time.Now(). A run
that crossed midnight between the two failed on "the context prefix",
the request holding the day before.

zcodeStartRequest now takes the time from zcodeClock (a package
variable, time.Now outside tests, as gateway's ruleClock), and the test
pins it to 2026-09-30 12:00 local, restored with t.Cleanup, and expects
"Today's date is 2026-09-30." The test sets TZ=Asia/Shanghai, which
moves time.Local only when nothing has read it yet; a mid-day instant in
time.Local formats to the same day either way. The other calls to
zcodeStartBody in the test check shaping only, not the day.

No other provider test builds a day string from a second clock: the
WorkBuddy credits, daily warm-up and check-in tests pass fixed times to
code that takes now as an argument, and TestQoderCheckinOnTheCard's day
is only carried, not compared.

Verified with at.sh (local time of day set by a zone, run as
-run '^(TestDailyCredits|TestZCodeStartSentAsTheApp)$' so that
time.Local is read before the test's t.Setenv("TZ"), as in a full run;
alone, Local becomes Asia/Shanghai): with a temporary 1s sleep after the
request, the unpinned test started at local 23:59:59.5 failed with
"zcode_client_test.go:195: the context prefix: ... Today's date is
2026-10-05." and passed at 12:00:00; pinned, it passes at 23:59:59.5
(same sleep) and, without the sleep, at 23:59:59, 00:00:00 and 12:00:00.
With zcodeStartRequest back on time.Now(), the pinned test fails:
"--- FAIL: TestZCodeStartSentAsTheApp ... zcode_client_test.go:196: the
context prefix: ... Today's date is 2026-10-06." go test -tags nogui
-count=1 ./internal/provider passes in a sandbox HOME; -race -count=20
-run '^TestZCodeStartSentAsTheApp$' passes; gofmt -l and go vet -tags
nogui ./internal/provider are clean.

* accounts, quota: a window's reset reads against one time per listing; TestQuotaCell passes across midnight

quotaCell, a window in `magpie accounts` and `magpie quota`, read the
clock three times: whether the reset had passed, how long until it, and
ResetClock's label, which names the calendar day ("tomorrow 02:13" or
"02:13"). TestQuotaCell set the reset 2h13m30s from time.Now() and
built the expected label from yet another time.Now(); a run that crossed
local midnight between quotaCell's read and the test's got "tomorrow
02:13" from one and "02:13" from the other and failed.

quotaCell now takes now. accountsCmd and quotaCmd take one time.Now()
just before printing and pass it to every window's cell, so a listing's
windows agree on what "today" is. The test passes a fixed 2026-09-30
12:00 local and expects "↻2h13m 14:13", and for a reset the next day
"↻14h13m tomorrow 02:13"; no zone in tzdata 2026c changes its clock
between those instants, so both read the same in any TZ. resetsCell's
label, quotaReadingCell's expired check and quotaResetCmd's prompt
still read time.Now(); no test compares them against a second clock.
internal/tui's quotaCell and quotaLines already take now, and their
tests pass a fixed one.

Verified with at.sh (local time of day set by a zone): with a temporary
1s sleep between quotaCell and the check, the old test started at local
23:59:59.5 failed with "accounts_cli_test.go:128: \"5h 42% ↻2h13m
tomorrow 02:13\"" and passed at 12:00:00; the new one passes at
23:59:59.5 (same sleep) and, without the sleep, at 23:59:59, 00:00:00
and 12:00:00. Putting any of quotaCell's three reads back on the wall
clock fails the test ("--- FAIL: TestQuotaCell"). ResetClock's now
alone fails the next-day case: "accounts_cli_test.go:134: tomorrow:
\"7d 42% ↻14h13m 02:13\"". The passed check alone, or all three:
"accounts_cli_test.go:129: \"5h 42% · reset time passed Sep 30
14:13\"". untilShort alone: "accounts_cli_test.go:129: \"5h 42% ↻now
14:13\"". go test -tags nogui -count=1 . passes in a sandbox HOME;
-race -count=20 -run '^TestQuotaCell$' passes; gofmt -l and go vet
-tags nogui . are clean.
2026-10-07 02:14:29 +08:00

1151 lines
33 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package tui
// The pages beside the agents: providers (keys, models, balances) and
// usage — what the app's Providers and Usage views do, in the terminal.
// Routing is in routing.go, sessions in sessions.go, the library in
// library.go.
import (
"context"
"fmt"
"math"
"slices"
"strings"
"sync"
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/fx"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
type page int
const (
pageAgents page = iota
pageProviders
pageGroups
pageUsage
pageSessions
pageLibrary
)
var pageNames = []string{"agents", "providers", "routing", "usage", "sessions", "library"}
// ask is a line to type: a key, a family, a group's name.
type ask struct {
crumbs []string
hint string
input textinput.Model
empty bool // enter with nothing typed is an answer too
onEnter func(string) tea.Cmd
}
// askMsg opens a line to type, from a step that comes before it.
type askMsg struct{ a ask }
// balanceMsg is what is left on each provider's key, by id.
type balanceMsg map[string]string
// ---- providers --------------------------------------------------------------
func (m *model) reloadProviders() {
m.provs = provider.All()
m.provsErr = provider.FileError()
m.prow = clamp(m.prow, len(m.provs))
}
// balancesCmd asks every vendor it can what is left on its key.
func balancesCmd(ps []provider.Provider) tea.Cmd {
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
out := balanceMsg{}
var mu sync.Mutex
var wg sync.WaitGroup
for _, p := range ps {
wg.Add(1)
go func() {
defer wg.Done()
amount, ok, err := provider.Balance(ctx, p)
if !ok {
return
}
v := amount
if err != nil {
v = "balance: " + err.Error()
if r := []rune(v); len(r) > 60 {
v = string(r[:60]) + "…"
}
}
mu.Lock()
out[p.ID] = v
mu.Unlock()
}()
}
wg.Wait()
return out
}
}
func (m model) updateProviders(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
n := len(m.provs)
key := msg.String()
if key != "d" {
m.confirm = ""
}
switch key {
case "j", "down":
if n > 0 {
m.prow = (m.prow + 1) % n
}
return m, nil
case "k", "up":
if n > 0 {
m.prow = (m.prow + n - 1) % n
}
return m, nil
case "a":
m.openPresets()
return m, nil
case "b":
m.flash, m.flashOK = "asking the vendors for balances…", true
return m, balancesCmd(m.provs)
}
if n == 0 {
return m, nil
}
p := m.provs[m.prow]
switch key {
case "enter", " ":
m.openProviderModels(p.ID)
case "e":
if p.Account != nil {
m.flash, m.flashOK = p.Name+" is a signed-in account: its key is the agent's sign-in", false
return m, nil
}
in := newInput("the API key")
in.EchoMode = textinput.EchoPassword
in.EchoCharacter = '•'
m.openAsk(ask{crumbs: []string{"providers", p.Name, "key"}, input: in,
hint: "now " + dash(provider.Mask(p.Key)) + " · the key is kept in magpie's providers file",
onEnter: func(v string) tea.Cmd {
return saveProvider(p.ID, func(p *provider.Provider) { p.Key = v; provider.ForgetBalances() }, p.Name+" key "+provider.Mask(v))
}})
case "w":
pr := provider.Preset(p.Preset)
if pr == nil || pr.Endpoint == "" {
m.flash, m.flashOK = p.Name+" is asked at its vendor's address · magpie provider set "+p.ID+" url=… changes a custom one's", false
return m, nil
}
now := p.Chat
if now == "" {
now = p.Responses
}
if p.IsRemoteMagpie() {
now = p.Anthropic // the address as typed, without /v1
}
id, name := p.ID, p.Name
m.openAsk(endpointAsk(*pr, []string{"providers", name, "address"}, now, func(v string) tea.Cmd {
return saveProvider(id, func(p *provider.Provider) {
p.Chat, p.Responses = v, v
if p.IsRemoteMagpie() {
p.Anthropic = "" // put again from the address typed
}
provider.ForgetBalances()
}, name+" address "+v)
}))
case "f":
in := newInput("a tag, e.g. relay")
in.SetValue(p.Family)
m.openAsk(ask{crumbs: []string{"providers", p.Name, "family"}, input: in, empty: true,
hint: "agents can be shown only some families: magpie visible <agent> <family>",
onEnter: func(v string) tea.Cmd {
return saveProvider(p.ID, func(p *provider.Provider) { p.Family = v }, p.Name+" family "+dash(v))
}})
case "u":
on := !p.Unlisted
what := "its models are listed"
if on {
what = "serves only through routing groups"
}
return m, saveProvider(p.ID, func(p *provider.Provider) { p.Unlisted = on }, p.Name+" "+what)
case "o":
off := !p.Off
what := "switched on"
if off {
what = "switched off: agents are given none of its models"
}
id := p.ID
return m, reseatCmd(func() error { return provider.SetOff(id, off) }, p.Name+" "+what)
case "t":
m.flash, m.flashOK = "testing "+p.Name+"…", true
return m, testCmd(p)
case "m":
// the vendor's list asked again, as the app editor's Refresh does
m.flash, m.flashOK = "asking "+p.Name+" for its models…", true
return m, refetchCmd(p.ID)
case "d":
if m.confirm != "provider/"+p.ID {
m.confirm = "provider/" + p.ID
m.flash, m.flashOK = "press d again to remove "+p.Name, false
return m, nil
}
m.confirm = ""
id := p.ID
return m, reseatCmd(func() error { return provider.Delete(id) }, "removed "+p.Name)
}
return m, nil
}
// reseatCmd makes a change that may take models away from the agents on
// them, and says which it moved to others (agent.Reseat).
func reseatCmd(change func() error, done string) tea.Cmd {
return func() tea.Msg {
moved, err := agent.Reseat(change)
if err != nil {
return flashMsg{text: err.Error()}
}
for _, mv := range moved {
if mv.Error != "" {
done += "; " + mv.String()
continue
}
done += "; moved " + mv.String()
}
return flashMsg{text: done, ok: true}
}
}
// saveProvider changes one provider and saves it.
func saveProvider(id string, change func(*provider.Provider), done string) tea.Cmd {
return func() tea.Msg {
p, err := provider.Find(id)
if err != nil {
return flashMsg{text: err.Error()}
}
change(p)
if err := provider.Save(*p); err != nil {
return flashMsg{text: err.Error()}
}
return flashMsg{text: done, ok: true}
}
}
// refetchCmd asks the provider's vendor for its model list again and says
// how many it has and how many agents are offered, or why there is none:
// the app editor's Refresh, which the TUI had no way to do (akic404 on
// Discord: a provider added here had 0 models and nothing to fetch them).
func refetchCmd(id string) tea.Cmd {
return func() tea.Msg {
p, err := provider.Find(id)
if err != nil {
return flashMsg{text: err.Error()}
}
if p.IsPlugin() {
return flashMsg{text: p.Name + "'s models are what its plugin lists"}
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
ms, dropped, err := p.Refetch(ctx)
if err != nil {
return flashMsg{text: p.Name + ": " + fetchNote(err)}
}
text := p.Name + ": " + modelsNote(id, len(ms))
if len(dropped) > 0 {
text += " · gone from its list, so no longer picked: " + strings.Join(dropped, ", ")
}
return flashMsg{text: text, ok: len(ms) > 0}
}
}
// modelsNote says how many models the vendor's list had and how many of
// them agents are offered.
func modelsNote(id string, fetched int) string {
text := fmt.Sprintf("%d models from its list", fetched)
if p, err := provider.Find(id); err == nil {
text += fmt.Sprintf(" · %d for agents (↵ picks)", len(p.Exposed()))
}
return text
}
// fetchNote is why a model list wasn't had, short enough for the status
// line: the first URL's answer.
func fetchNote(err error) string {
e, _, _ := strings.Cut(err.Error(), "; ")
if r := []rune(e); len(r) > 140 {
e = string(r[:140]) + "…"
}
return "no model list · " + strings.TrimPrefix(e, "no model list: ") + " · m asks again"
}
func testCmd(p provider.Provider) tea.Cmd {
return func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var parts []string
ok := true
for _, r := range p.Test(ctx) {
if r.OK {
parts = append(parts, fmt.Sprintf("%s ✓ %d ms", r.Protocol, r.Millis))
continue
}
ok = false
e := r.Error
if r.Status != 0 {
e = fmt.Sprintf("%d %s", r.Status, r.Error)
}
if rs := []rune(e); len(rs) > 80 {
e = string(rs[:80]) + "…"
}
parts = append(parts, fmt.Sprintf("%s ✗ %s", r.Protocol, e))
}
if len(parts) == 0 {
return flashMsg{text: p.Name + ": nothing to test"}
}
return flashMsg{text: p.Name + ": " + strings.Join(parts, " · "), ok: ok}
}
}
// modelOptions are a provider's models, those agents are shown marked.
func modelOptions(id string) []agent.Option {
p, err := provider.Find(id)
if err != nil {
return nil
}
on := map[string]bool{}
for _, x := range p.Exposed() {
on[x.ID] = true
}
var out []agent.Option
seen := map[string]bool{}
names := p.ModelNames()
add := func(id, name string) {
if seen[id] {
return
}
seen[id] = true
note := "○ off"
if on[id] {
note = "● on"
}
if n, ok := names[id]; ok {
name = n // the user's (magpie model name)
}
if name != "" && name != id {
note += " · " + name
}
out = append(out, agent.Option{Value: id, Note: note})
}
for _, x := range p.Exposed() {
add(x.ID, x.Name)
}
for _, x := range p.Available() {
add(x.ID, x.Name)
}
return out
}
// openProviderModels lists a provider's models; enter turns one on or off
// for agents, and the list stays open.
func (m *model) openProviderModels(id string) {
p := m.provs[m.prow]
m.pk = picker{
crumbs: []string{"providers", p.Name, "models"},
input: newInput("filter models"),
items: modelOptions(id),
empty: "no models known yet · m on the providers list asks the vendor for them",
toggle: func(model string) ([]agent.Option, string, bool) {
p, err := provider.Find(id)
if err != nil {
return nil, err.Error(), false
}
ids := p.Models
if len(ids) == 0 {
for _, x := range p.Exposed() {
ids = append(ids, x.ID)
}
}
verb := "on"
if i := slices.Index(ids, model); i >= 0 {
if len(ids) == 1 {
return nil, "one model has to stay on", false
}
ids = slices.Delete(slices.Clone(ids), i, i+1)
verb = "off"
} else {
ids = append(slices.Clone(ids), model)
}
p.Models = ids
if err := provider.Save(*p); err != nil {
return nil, err.Error(), false
}
items := modelOptions(id)
if !slices.ContainsFunc(items, func(o agent.Option) bool { return o.Value == model }) {
// a vendor whose list wasn't fetched knows only those on:
// the one just turned off stays to be turned on again
items = append(items, agent.Option{Value: model, Note: "○ off"})
}
return items, model + " " + verb, true
},
}
m.pk.refilter()
m.mode = modePick
}
// openPresets picks a vendor magpie knows, then asks for its key.
func (m *model) openPresets() {
var items []agent.Option
for _, d := range provider.Presets() {
items = append(items, agent.Option{Value: d.ID, Note: d.Name + " · " + string(d.Kind)})
}
m.pk = picker{
crumbs: []string{"providers", "add"},
input: newInput("a vendor magpie knows (magpie provider add <name> url=… for another)"),
items: items,
onPick: func(id string) tea.Cmd {
return func() tea.Msg {
p, err := provider.FromPreset(id)
if err != nil {
return flashMsg{text: err.Error()}
}
// a vendor reached at the user's own address (a remote
// magpie, Azure OpenAI) has none of the preset's: it is
// asked for first, as the app's editor asks it
if pr := provider.Preset(id); pr != nil && pr.Endpoint != "" {
return askMsg{endpointAsk(*pr, []string{"providers", "add", p.Name, "address"}, "", func(addr string) tea.Cmd {
return func() tea.Msg {
p.Chat, p.Responses = addr, addr
return askMsg{addKeyAsk(p)}
}
})}
}
return askMsg{addKeyAsk(p)}
}
},
}
m.pk.refilter()
m.mode = modePick
}
// addKeyAsk asks for the key of p, a preset's provider, and adds it.
func addKeyAsk(p provider.Provider) ask {
in := newInput("the API key")
in.EchoMode = textinput.EchoPassword
in.EchoCharacter = '•'
hint := "the key is kept in magpie's providers file"
if p.KeysURL != "" {
hint = "keys: " + p.KeysURL
}
return ask{crumbs: []string{"providers", "add", p.Name}, input: in, hint: hint, empty: true,
onEnter: func(key string) tea.Cmd {
return func() tea.Msg {
p.Key = key
id, err := provider.Add(p)
if err != nil {
return flashMsg{text: err.Error()}
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
saved, err := provider.Find(id)
if err != nil {
return flashMsg{text: err.Error()}
}
// the vendor's list, and what came of asking it: a list
// that failed said so, not an "added" over 0 models
// (akic404 on Discord)
text := "added " + saved.Name
ms, err := saved.Fetch(ctx)
if err != nil && saved.Decides() {
// a System One API: its list isn't what it is for
return flashMsg{text: text, ok: true}
}
if err != nil {
return flashMsg{text: text + " · " + fetchNote(err)}
}
return flashMsg{text: text + " · " + modelsNote(id, len(ms)), ok: len(ms) > 0}
}
}}
}
// endpointAsk asks for the address of a vendor reached at the user's own
// (pr.Endpoint is its example): a remote magpie's, as the other
// computer's magpie shows it in Settings, under Share on local network.
// Nothing typed is said to be needed, as the app's editor says it.
func endpointAsk(pr provider.PresetDef, crumbs []string, now string, then func(string) tea.Cmd) ask {
in := newInput(pr.Endpoint)
in.SetValue(now)
hint := pr.EndpointHint
if hint == "" {
hint = "the address it is reached at, e.g. " + pr.Endpoint
}
return ask{crumbs: crumbs, input: in, hint: hint, empty: true,
onEnter: func(v string) tea.Cmd {
if v == "" {
need := pr.EndpointNeeded
if need == "" {
need = "Your resource's endpoint is needed"
}
return func() tea.Msg { return flashMsg{text: need} }
}
return then(v)
}}
}
// listError is provider.Provider.ListError; tests stand in for it.
var listError = provider.Provider.ListError
func (m model) viewProviders() string {
var b strings.Builder
b.WriteString(m.header())
b.WriteString("\n\n")
if m.provsErr != nil {
b.WriteString(pad + " " + sBad.Render("! "+m.provsErr.Error()) + "\n\n")
}
if len(m.provs) == 0 {
if m.provsErr == nil {
b.WriteString(pad + " " + sMuted.Render("no providers yet · a adds one"))
}
return b.String()
}
type row struct{ name, id, key, models, note, warn string }
var rows []row
var w [4]int
for _, p := range m.provs {
r := row{name: p.Name, id: p.ID}
switch {
case p.Off:
r.key = "○ switched off"
case p.Account != nil:
r.key = "● " + p.Account.User
case p.Key != "":
r.key = "● " + provider.Mask(p.Key)
case p.Ready():
r.key = "● no key needed"
default:
r.key = "○ no key"
}
r.models = fmt.Sprintf("%d models", len(p.Exposed()))
var notes []string
if v, ok := m.bal[p.ID]; ok {
notes = append(notes, v)
}
if p.Family != "" {
notes = append(notes, "family "+p.Family)
}
if p.Unlisted {
notes = append(notes, "groups only")
}
r.note = strings.Join(notes, " · ")
// a plugin's account showing its defaults alone says why, as the
// app's editor does (gnayiab on X: Cursor in WSL's TUI had Auto
// alone and nothing said)
if e := listError(p); e != "" {
r.warn = "couldn't list its models: " + e
}
for i, s := range []string{r.name, r.id, r.key, r.models} {
w[i] = max(w[i], lipgloss.Width(s))
}
rows = append(rows, r)
}
visible := max(3, m.h-7)
start := 0
if m.prow >= visible {
start = m.prow - visible + 1
}
for i := start; i < min(len(rows), start+visible); i++ {
r := rows[i]
marker, name := " ", sName.Render(padRight(r.name, w[0]))
if i == m.prow {
marker, name = sCursor.Render("▸ "), sNameOn.Render(padRight(r.name, w[0]))
}
key := sMuted.Render(padRight(r.key, w[2]))
if strings.HasPrefix(r.key, "○") {
key = sBad.Render(padRight(r.key, w[2]))
}
line := pad + marker + name + " " + sFaint.Render(padRight(r.id, w[1])) + " " + key + " " + sText.Render(padRight(r.models, w[3])) + " " + sMuted.Render(r.note)
if r.warn != "" {
if r.note != "" {
line += sMuted.Render(" · ")
}
line += sBad.Render(r.warn)
}
b.WriteString(line + "\n")
}
return strings.TrimRight(b.String(), "\n")
}
// ---- usage ------------------------------------------------------------------
var periods = []usage.Period{usage.Today, usage.Week, usage.Month, usage.All}
var periodNames = map[usage.Period]string{usage.Today: "today", usage.Week: "7 days", usage.Month: "30 days", usage.All: "all time"}
// quotaMsg is what is left of every subscription, plan bought with a key
// and key balance, as the app's Usage page shows them.
type quotaMsg []provider.SubscriptionQuota
func quotasCmd() tea.Msg {
provider.AskClaudeUsage() // the page opened, or r pressed
ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()
// a WorkBuddy (China) account's line says how its daily check-in went,
// as its card in the app does
return quotaMsg(provider.WithCheckins(provider.Quotas(ctx)))
}
// checkinMsg is what came of c, WorkBuddy's daily check-in pressed now.
type checkinMsg struct {
text string
ok bool
}
// The check-in; vars so tests can stand in for WorkBuddy and Trae CN.
var (
checkinHere = provider.CheckInWorkBuddy
hasWorkBuddy = provider.HasWorkBuddy
checkinTrae = provider.CheckInTrae
hasTrae = provider.HasTrae
checkinMM = provider.CheckInMiniMax
hasMiniMax = provider.HasMiniMax
checkinQd = provider.CheckInQoder
hasQoder = provider.HasQoder
checkinPl = func(ctx context.Context) []provider.WorkBuddyCheckin { return provider.CheckInPlugins(ctx) }
hasPlugin = provider.HasPluginCheckin
)
// checkinCmd presses WorkBuddy's daily check-in (签到) now for every
// WorkBuddy (China) account signed in here not in yet today, all at once,
// as the app's Usage card's "Check in now" and magpie accounts checkin do:
// the built-in's or the plugin's (akic404 on Discord: the TUI had no way
// to); and Trae CN's (每日签到) for each Trae CN account (#694), and
// MiniMax Code's for each MiniMax Code account (#811), and Qoder's daily
// credits for each Qoder account, and each plugin's own check-in
// (auth.checkin) for its accounts. It says
// how each account stands: the credits and streak, in
// already today, or why not. A shared magpie's accounts are checked in
// on that magpie, from its own app, TUI or CLI.
func checkinCmd() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
var parts []string
failed := 0
if hasWorkBuddy() {
for _, r := range checkinHere(ctx) {
if r.Outcome == provider.CheckinFailed {
failed++
}
parts = append(parts, checkinWords(r))
}
}
if hasTrae() {
for _, r := range checkinTrae(ctx) {
if r.Outcome == provider.CheckinFailed {
failed++
}
parts = append(parts, checkinWords(r))
}
}
if hasMiniMax() {
for _, r := range checkinMM(ctx) {
if r.Outcome == provider.CheckinFailed {
failed++
}
parts = append(parts, checkinWords(r))
}
}
if hasQoder() {
for _, r := range checkinQd(ctx) {
if r.Outcome == provider.CheckinFailed {
failed++
}
parts = append(parts, checkinWords(r))
}
}
if hasPlugin() {
for _, r := range checkinPl(ctx) {
if r.Outcome == provider.CheckinFailed {
failed++
}
parts = append(parts, checkinWords(r))
}
}
if len(parts) == 0 {
return checkinMsg{text: "no WorkBuddy (China), Trae CN, MiniMax Code, Qoder or check-in plugin account is signed in · only they have the daily check-in"}
}
return checkinMsg{text: strings.Join(parts, "; "), ok: failed == 0}
}
// checkinWords is how an account's check-in stands, in a few words.
func checkinWords(r provider.WorkBuddyCheckin) string {
who := r.User
switch {
case r.By == "trae" && who == "":
who = "Trae CN"
case r.By == "trae":
who = "Trae CN " + who
case r.By == "minimax" && who == "":
who = "MiniMax Code"
case r.By == "minimax":
who = "MiniMax Code " + who
case r.By == "qoder" && who == "":
who = "Qoder"
case r.By == "qoder":
who = "Qoder " + who
case r.Vendor != "":
who = strings.TrimSpace(r.Vendor + " " + who)
case who == "":
who = "WorkBuddy"
}
switch r.Outcome {
case provider.CheckinClaimed, provider.CheckinDone:
s := who + " checked in today"
if r.Credit > 0 {
s += fmt.Sprintf(" +%g", r.Credit)
}
if r.Streak > 0 {
s += fmt.Sprintf(" · a %d-day streak", r.Streak)
}
if r.Outcome == provider.CheckinDone || !r.Asked {
s += " · already"
}
return s
case provider.CheckinIneligible:
return who + " isn't eligible for the check-in"
case provider.CheckinInactive:
return who + ": no check-in event now"
case provider.CheckinCaptcha:
return who + " asks for a captcha: check in in its own app"
}
msg := r.Msg
if msg == "" {
msg = "no answer"
}
return who + " couldn't check in: " + msg
}
// checkinCell is a WorkBuddy (China) or Trae CN account's check-in on its
// line: today's
// done, with the credits, or not yet; empty for an account without one.
func checkinCell(q provider.SubscriptionQuota, now time.Time) string {
if !q.Checkins {
return ""
}
r := q.Checkin
if r == nil || r.Day != provider.CheckinDay(now) {
return sMuted.Render("签到 not yet today · c")
}
switch r.Outcome {
case provider.CheckinClaimed, provider.CheckinDone:
s := sOK.Render("签到 ✓")
if r.Credit > 0 {
s += sText.Render(fmt.Sprintf(" +%g", r.Credit))
}
if r.Streak > 0 {
s += sFaint.Render(fmt.Sprintf(" · %d-day streak", r.Streak))
}
return s
case provider.CheckinIneligible:
return sMuted.Render("签到 not eligible")
case provider.CheckinInactive:
return sMuted.Render("签到 no event now")
case provider.CheckinCaptcha:
return sMuted.Render("签到 needs a captcha · check in in its app")
}
return sBad.Render("签到 failed") + sMuted.Render(" · c tries again")
}
func (m model) updateUsage(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
i := slices.Index(periods, m.period)
switch msg.String() {
case "u":
m.qleft = !m.qleft
return m, nil
case "c":
// WorkBuddy's daily check-in, as the app's "Check in now"
m.flash, m.flashOK = "checking in…", true
return m, checkinCmd
case "l", "right":
m.period = periods[(i+1)%len(periods)]
case "h", "left":
m.period = periods[(i+len(periods)-1)%len(periods)]
case "t":
m.period = usage.Today
case "w":
m.period = usage.Week
case "m":
m.period = usage.Month
case "A":
m.period = usage.All
default:
return m, nil
}
m.sum, m.direct = usage.Summaries(m.period)
return m, nil
}
func fmtTokens(n int) string {
switch {
case n >= 1_000_000_000:
return fmt.Sprintf("%.2fB", float64(n)/1e9)
case n >= 10_000_000:
return fmt.Sprintf("%.0fM", float64(n)/1e6)
case n >= 1_000_000:
return fmt.Sprintf("%.1fM", float64(n)/1e6)
case n >= 100_000:
return fmt.Sprintf("%.0fK", float64(n)/1e3)
case n >= 1_000:
return fmt.Sprintf("%.1fK", float64(n)/1e3)
}
return fmt.Sprint(n)
}
// costCurrency is Settings' currency choice and, for cny, the CNY-per-USD
// rate to show costs at (internal/fx), refreshed at most once a second so
// a page redrawn on every keypress doesn't reread the settings file or
// touch the rate's own cache for each row fmtCost renders.
var (
costMu sync.Mutex
costAt time.Time
costCncy string
costRateVal float64
)
func costCurrency() (string, float64) {
costMu.Lock()
defer costMu.Unlock()
if time.Since(costAt) < time.Second {
return costCncy, costRateVal
}
costCncy = settings.Load().Currency
costRateVal = 0
if costCncy == "cny" {
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
costRateVal = fx.Get(ctx).CNYPerUSD
cancel()
}
costAt = time.Now()
return costCncy, costRateVal
}
func fmtCost(t usage.Totals) string {
if t.Cost == 0 && t.Unpriced > 0 {
return "no price"
}
cncy, rate := costCurrency()
s := usage.FormatCost(t.Cost, cncy, rate)
if t.Unpriced > 0 {
s += "+"
}
return "≈" + s
}
var bars = []rune(" ▁▂▃▄▅▆▇█")
func (m model) viewUsage() string {
s := m.sum
var b strings.Builder
b.WriteString(m.header())
b.WriteString("\n\n")
var tabs []string
for _, p := range periods {
if p == s.Period {
tabs = append(tabs, sPill.Render(periodNames[p]))
} else {
tabs = append(tabs, sMuted.Render(" "+periodNames[p]+" "))
}
}
b.WriteString(pad + " " + strings.Join(tabs, " ") + "\n\n")
ql := quotaLines(m.quotas, m.qasked, m.qleft, m.w-len(pad)-2, time.Now())
for _, l := range ql {
b.WriteString(pad + " " + l + "\n")
}
if len(ql) > 0 {
b.WriteString("\n")
}
d := m.direct
if s.Calls == 0 && d.Calls == 0 {
b.WriteString(pad + " " + sMuted.Render("no calls in this time · route an agent through magpie and its usage shows up here"))
return b.String()
}
names := map[string]string{}
for _, a := range agent.All() {
names[a.ID] = a.Name
}
room := max(2, (m.h-16-len(ql))/2)
if s.Calls > 0 && d.Calls > 0 {
room = max(2, room/2)
}
if s.Calls == 0 {
b.WriteString(pad + " " + sMuted.Render("no calls through magpie in this time") + "\n\n")
} else {
m.usageBlock(&b, s, room, names)
}
// the calls the agents made on their own, as the app's Requests tab
// counts them beside the gateway's (Kumo31 on Discord)
if d.Calls > 0 {
b.WriteString(pad + " " + sFaint.Render("not through magpie · the agents' own requests, read from their session files") + "\n")
m.usageBlock(&b, d, room, names)
}
return strings.TrimRight(b.String(), "\n")
}
// usageBlock is one summary's totals, timeline and agents' and models'
// tables, the gateway's or the agents' own calls.
func (m model) usageBlock(b *strings.Builder, s usage.Summary, room int, names map[string]string) {
line := sName.Render(fmtTokens(s.Tokens())+" tokens") + sMuted.Render(fmt.Sprintf(" · %d call%s · ", s.Calls, plural(s.Calls))) + sOK.Render(fmtCost(s.Totals))
if s.Errors > 0 {
line += sMuted.Render(" · ") + sBad.Render(fmt.Sprintf("%d error%s", s.Errors, plural(s.Errors)))
}
b.WriteString(pad + " " + line + "\n")
b.WriteString(pad + " " + sMuted.Render("in "+fmtTokens(s.Input)+" out "+fmtTokens(s.Output)+" cache read "+fmtTokens(s.CacheRead)+" cache write "+fmtTokens(s.CacheWrite)+" reasoning "+fmtTokens(s.Reasoning)) + "\n\n")
// the timeline, one bar a bucket
top := 0
for _, p := range s.Series {
top = max(top, p.Tokens())
}
if top > 0 && len(s.Series) > 0 {
var sb strings.Builder
for _, p := range s.Series {
i := 0
if p.Tokens() > 0 {
i = max(1, p.Tokens()*(len(bars)-1)/top)
}
sb.WriteRune(bars[i])
}
first, last := s.Series[0].Label, s.Series[len(s.Series)-1].Label
b.WriteString(pad + " " + sCursor.Render(sb.String()) + " " + sFaint.Render(first+" – "+last) + "\n\n")
}
table := func(head string, gs []usage.Group, name func(usage.Group) string) {
b.WriteString(pad + " " + sFaint.Render(head) + "\n")
w := 0
for _, g := range gs {
w = max(w, lipgloss.Width(name(g)))
}
for i, g := range gs {
if i == room {
b.WriteString(pad + " " + sFaint.Render(fmt.Sprintf("and %d more · magpie usage", len(gs)-room)) + "\n")
break
}
share := fmt.Sprintf("%3.0f%%", 100*float64(g.Tokens())/float64(max(1, s.Tokens())))
b.WriteString(pad + " " + sText.Render(padRight(name(g), w)) + " " + sMuted.Render(share) + " " + sText.Render(padRight(fmtTokens(g.Tokens()), 7)) + " " + sFaint.Render(padRight(fmt.Sprintf("%d call%s", g.Calls, plural(g.Calls)), 10)) + " " + sOK.Render(fmtCost(g.Totals)) + "\n")
}
b.WriteString("\n")
}
table("agents", s.Agents, func(g usage.Group) string {
if n := names[g.ID]; n != "" {
return n
}
return g.ID
})
table("models", s.Models, func(g usage.Group) string {
if g.Provider == usage.UnknownProvider {
return g.Model // a session file's call whose provider isn't known
}
return g.ID
})
}
// quotaLines is the accounts' allowances, a line each as the app's cards
// are: the subscriptions' and plans' windows as small meters — how much is
// used, or left, with the vendor's own count before it where it gives one
// — and the keys' balances; nil when there is nothing to tell.
func quotaLines(qs []provider.SubscriptionQuota, asked, left bool, width int, now time.Time) []string {
if qs == nil {
if !asked {
return nil
}
return []string{sFaint.Render("accounts"), sMuted.Render("asking the vendors what is left…")}
}
if len(qs) == 0 {
return nil
}
title := func(q provider.SubscriptionQuota) (plain, styled string) {
plain, styled = q.Name, sName.Render(q.Name)
if q.Plan != "" {
plain += " · " + q.Plan
styled += sMuted.Render(" · " + q.Plan)
}
if t := provider.PlanTerm(q.Until, q.Renew); t != "" {
plain += " · " + t
styled += sFaint.Render(" · " + t)
}
if q.User != "" {
plain += " · " + q.User
styled += sFaint.Render(" · " + q.User)
}
return
}
tw := 0
for _, q := range qs {
p, _ := title(q)
tw = max(tw, lipgloss.Width(p))
}
tw = min(tw, 44)
head := "accounts · % is how much is used"
if left {
head = "accounts · % is how much is left"
}
out := []string{sFaint.Render(head)}
for _, q := range qs {
p, t := title(q)
if lipgloss.Width(p) > tw {
t = sName.Render(trunc(p, tw))
}
line := padRight(t, tw)
// a WorkBuddy (China) account's check-in, after whatever else
ci := checkinCell(q, now)
if ci != "" {
ci = " " + ci
}
switch {
case q.Balance != "" && len(q.Windows) == 0:
out = append(out, line+" "+sText.Render(q.Balance)+sMuted.Render(" left")+ci)
continue
case q.Error != "":
out = append(out, line+" "+sMuted.Render(trunc(q.Error, max(20, width-tw-2-lipgloss.Width(ci))))+ci)
continue
case len(q.Windows) == 0:
out = append(out, line+" "+sMuted.Render("no usage reported")+ci)
continue
}
// the windows follow the name, those that don't fit on lines below
// it, then the credits a ChatGPT account holds beside them and a
// Codex account's resets
var cells []string
for _, w := range provider.PooledWindows(q.Windows) {
cells = append(cells, quotaCell(w, left, now))
}
if q.Balance != "" {
c := sText.Render(q.Balance) + sMuted.Render(" left")
if q.Provider == "codex" && q.User != "" && !provider.CodexCredits(q.User) {
c += sFaint.Render(" · not spent") // held once a window is used up
}
cells = append(cells, c)
}
if ci != "" {
cells = append(cells, ci[3:])
}
if r := q.Resets; r != nil {
c := sText.Render("↺ " + r.Words())
if r.Until != nil {
c += sFaint.Render(" until " + provider.ResetClock(*r.Until, now))
}
if provider.AutoResets(q.Provider, q.User) {
c += sFaint.Render(" · auto") // spent by itself once the week is used up
}
cells = append(cells, c)
}
at := tw
for _, c := range cells {
cw := lipgloss.Width(c)
if at > tw && width > 0 && at+3+cw > width {
out = append(out, line)
line, at = strings.Repeat(" ", tw), tw
}
line += " " + c
at += 3 + cw
}
out = append(out, line)
}
return out
}
// quotaCell is one window: its name, a meter, how much is used or left,
// and when it starts again: on the clock, and how long until then.
func quotaCell(w provider.QuotaWindow, left bool, now time.Time) string {
if w.Unlimited {
return sMuted.Render(w.Name) + " " + sText.Render("Unlimited")
}
used := int(math.Round(math.Max(0, math.Min(100, w.Used))))
n, word := used, "used"
if left {
n, word = 100-used, "left"
}
const cells = 8
on := (n*cells + 50) / 100
if n > 0 {
on = max(1, on)
}
fill := sCursor
if used >= 90 {
fill = sBad
}
pct := fmt.Sprintf("%d%% %s", n, word)
if c := w.Count(left); c != "" {
pct = c + " · " + pct
}
c := sMuted.Render(w.Name) + " " + fill.Render(strings.Repeat("█", on)) + sFaint.Render(strings.Repeat("░", cells-on)) + " " + sText.Render(pct)
var at time.Time
if w.ResetsAt != nil {
at = *w.ResetsAt
} else if w.ResetSecs > 0 {
at = now.Add(time.Duration(w.ResetSecs) * time.Second)
}
if !at.IsZero() {
c += sFaint.Render(" ↻ " + provider.ResetClock(at, now) + " (" + until(at.Sub(now)) + ")")
}
return c
}
// until is how long until then, roughly: 40m, 5h, 3d.
func until(d time.Duration) string {
mins := max(1, int(math.Round(d.Minutes())))
h := int(math.Round(float64(mins) / 60))
switch {
case mins < 60:
return fmt.Sprintf("%dm", mins)
case h < 48:
return fmt.Sprintf("%dh", h)
}
return fmt.Sprintf("%dd", int(math.Round(float64(h)/24)))
}
// ---- a line to type ---------------------------------------------------------
func (m *model) openAsk(a ask) {
m.ask = a
m.mode = modeAsk
m.back = modeList
}
func (m model) updateAsk(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc":
m.mode = m.back
return m, nil
case "enter":
v := strings.TrimSpace(m.ask.input.Value())
if v == "" && !m.ask.empty {
return m, nil
}
m.mode = m.back
return m, m.ask.onEnter(v)
}
var cmd tea.Cmd
m.ask.input, cmd = m.ask.input.Update(msg)
return m, cmd
}
func (m model) viewAsk() string {
var b strings.Builder
b.WriteString(m.header(m.ask.crumbs...))
b.WriteString("\n\n")
b.WriteString(pad + sCursor.Render("❯ ") + m.ask.input.View())
if m.ask.hint != "" {
b.WriteString("\n")
for _, line := range strings.Split(m.ask.hint, "\n") {
b.WriteString("\n" + pad + " " + sMuted.Render(line))
}
}
return b.String()
}