88b4c00ea11c8653cada22e11edf20ab1594de43
8
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
957aa79aa3 |
provider, gateway: refreshing a saved Claude account's card reads its allowance through Claude Code, without switching to it (#1630, hhh2210)
A saved (not signed-in) Claude account's Usage card read nothing on Refresh: claudeWindows only ever ran /usage for the account Claude Code itself is signed in to, so the button did nothing for the rest. Now the card's own refresh (RefreshUsage for that account, savedRefresh) runs the real Claude Code's /usage with CLAUDE_CONFIG_DIR set to the account's claudeSavedDir (readSavedClaudeUsage). magpie still never calls Anthropic with the token itself, and Claude Code's own account is not switched. The 30-second per-account floor holds, background reads keep the last reading, and asks for every card (the Usage page opened, magpie quota, magpie accounts) start no Claude Code per saved account: they keep the last reading, or the "not read yet: refresh this account's card" note. Claude Code may refresh the account's token in that directory while it runs, spending the old refresh token. So: - inSavedClaudeDir holds a per-account RWMutex (claudeDirRun) for the run, and switchSavedLogin takes it for writing first, so a switch never hands Claude Code a copy the run has just spent; - syncSavedClaude copies a rotated sign-in from the directory back into logins.json right after the run, under savedTokenMu and loginsMu. Two Claude Codes in one directory are kept from refreshing at once by Claude Code's own lock file (2.1.296's refresh lock in its config dir). A saved account's warm-up, which already ran in that directory, now goes through inSavedClaudeDir too. The plumbing (configDir through UsageClaudeVia, readClaudeUsage and the gateway's claudeUsage, and TestClaudeUsageRunsInSavedAccountDirectory) is from hhh2210's patch 043ee7d7, reviewed line by line. Its gate (any explicit read reads every saved account) was narrowed to the card's own refresh, and the lock and sync back are new. Run: go test -tags nogui ./internal/provider ./internal/gateway . (sandbox HOME, fake claude via UsageClaudeVia, no real token), gofmt -l, GOOS=linux and GOOS=windows go vet on the three packages. Broken by hand, each red: saved never read (all three new tests), any ask reads saved (TestRefreshSavedClaudeUsage), switch not held and no sync back (TestSavedClaudeUsageKeepsRotatedSignIn / SavesRotatedSignIn), run in "" instead of the account's directory. Not tried with a real Claude Code binary against a real saved account. Co-authored-by: hhh2210 <hzy2210@gmail.com> |
||
|
|
570b8fdb2a |
provider: swapping LoginUsage's or Claude /usage's stand-in waits for the readings still running through the old one (data race in CI run 37942751214)
Cause: TestClaudeAccountBackOnceItsWindowRenews (
|
||
|
|
dfde3acafc |
provider, gateway: a Claude /usage reset with no year is read within its window, so a reading a few days old no longer benches an account for a year (#1212)
* provider: a Claude reset read in the hour the clocks repeat, or the night they skip it, is the coming one, and a Dec 31 with no year read just after New Year is last year's
claudeResetTime turns /usage's reset text into the ResetsAt of the
"5 hours" and "7 days" windows, which smart routing orders accounts by and
rests a full account until.
- Where the clocks go back and read a time twice, time.Date gives either
instant: the first west of UTC, the second east of it. Each reading is
now considered (nextReading), and the one not yet past is taken.
Before, at 2026-11-01 01:20 EST (the second 01:20), "Nov 1, 1:30am
(America/New_York)" was 01:30 EDT, 50 minutes past, so a full window
read as renewed; "1:30am" was 24h10m away instead of 10m. In Berlin at
02:20 CEST on 2026-10-25, "2:30am" was 02:30 CET, 1h10m away instead of
10m (Sydney and Lord Howe alike).
- A date with no year is in the first of last year, this year and next
that is less than a day past, as the doc comment already said. Before,
only this year and next were tried, so at 2027-01-01 00:00:05
"Dec 31, 11:59pm" was 2027-12-31, 8760h away, and a 7-day window that
had just renewed read as renewing in a year.
- A time alone the clocks skip today is the next day that has it,
rebuilt from the clock read rather than from the normalized time.
Before, the evening after New York skipped 02:00-02:59, "2:30am" was
tomorrow's 1:30am; at 23:31 before Santiago skips midnight, "12:30am"
was 23:30 that night, a minute past, instead of 00:30 the day after
tomorrow.
What Claude Code prints, from the /usage formatter in the 2.1.290 binary:
every line of `claude -p /usage` carries a date, with the year only when
it isn't this one, so the New Year case needs /usage run just before
midnight and parsed after, and the repeated hour shows in the dated form.
Whether "at" or a comma joins date and time follows the platform's ICU,
and both are parsed. A time alone is the older form, which the TUI still
prints for the session within a day.
No other parser in internal/provider fills in a missing date or year:
zhipuTime and mimoTime read full dates in UTC+8, and the rest read
RFC3339 or Unix times.
Verified:
- TestClaudeResetTimeAroundClockChanges and TestClaudeResetTimeAtNewYear
are new. With upstream's claudeResetTime put back, 15 rows fail, e.g.
`"Nov 1, 1:30am (America/New_York)" at 2026-11-01 01:20:00 -0500 EST:
2026-11-01 01:30:00 -0400 EDT true (in -50m0s)` and `"Dec 31, 11:59pm
(America/New_York)" at 2027-01-01 00:00:05 -0500 EST: 2027-12-31
23:59:00 -0500 EST true (in 8759h58m55s)`; the 16 controls pass. With
the change, all pass, as does TestParseClaudeUsage, also with
-race -count=20.
- An oracle that lists every instant a zone shows a clock at was swept
over 518 zones, every transition 2025-2030, 85M calls: no deviation in
anything Claude Code can print. What remains is Feb 29 in a non-leap
year and dated times the clocks skip, both unchanged from upstream.
- Under a temporary HOME with GOPATH, GOMODCACHE and GOCACHE pinned:
go vet ./..., go build, GOOS=linux go build -tags nogui,
GOOS=windows go build, go test -tags nogui ./... all pass.
- Not run: claude -p /usage on a real account across a clock change or
New Year.
* provider: a Claude reset further ahead than its window can reach is the one already past, so a /usage reading days old no longer rests an account for a year
claudeResetTime now gets the window's length (5 hours for "Current
session", 7 days for "Current week ...") and never reads a reset further
ahead than that plus claudeResetSlack (2h, for this machine's clock being
behind Anthropic's; Claude Code prints the reset from Anthropic's instant
with seconds dropped, so never later).
- A date with no year is in the first of last year, this year and next
that is less than a day past and within the window. Before, it was the
first less than a day past: read on Oct 5, a week's "Oct 1, 3pm" was
2027-10-01, 8667h away, and routing (candidate.full, Allowance.Full)
rested a full account that long. Now it is 2026-10-01.
- Where no year fits, the latest year where it is already past is taken,
rather than dropping ResetsAt. The date read fine, and the window's
length rules out every later year, so the reset has passed and the
window has renewed, which is what elapsed() does with a past reset.
Dropping it would keep a 100% window with no time to clear it: Pooled
counts a full model window with no reset as out, so Opus on the account
stayed refused until the next read, and the card showed 100% with no
reset.
- A 5-hour window's date 6 days ahead ("Oct 11, 3pm" on Oct 5) is likewise
last year's and reads as renewed; a week's keeps Oct 11.
- A time alone (the older form) is the next one within the window, else
the latest day where it is already past: at noon, "11am" is today's for
the session (was tomorrow's, 23h on) and tomorrow's for the week.
- A fresh /usage read now goes through elapsed() as a kept one does, so a
reset it gives as past reads as renewed at once. Before, only the next
look did, and the switch (NextLogin's usedPast) and quota alerts saw the
fresh 100%.
- Dates with a year are unchanged.
Tests: TestClaudeResetTimeWithinItsWindow covers the cases above, the
inclusive bound (exactly span+slack is kept, a minute more is not, for a
date and a time alone), a week across New York's fall-back, and
claudeWindows' fresh and kept reads of a 4-day-old reset (Used 0, no
reset, Full zero). The DST rows and the other New Year rows pass
unchanged, at the week's length. Changed existing rows, each of which
asserted a reset further ahead than any window reaches: TestParseClaudeUsage's
"Current week (Opus) ... Jan 2 at 2pm" read Oct 1 (now 2026-01-02), the
#631 "Jan 2, 9:05am (UTC)" row (2026-01-02), and the New Year "Dec 30,
11pm" read Jan 1 01:00, 26h past (2026-12-30, not 2027-12-30). The #631
format rows are read on Oct 3 instead of Oct 1, so "Oct 9, 2:59pm" stays
within its week. Fixtures that gave a 5-hour session a reset the next day
(soon(1) at 3:30pm in claude_usage, claude_seats, claude_usage_switched;
claude_made_first in gateway; "Jul 20 at 10am" at midnight in
claude_scoped) now give one 3 hours on, as /usage would.
Fails without: with the bound removed and fresh reads not through
elapsed, TestClaudeResetTimeWithinItsWindow fails ("Oct 1, 3pm (UTC)" for
168h at 2026-10-05: 2027-10-01 15:00 (in 8667h), want 2026-10-01 15:00;
"Oct 11, 3pm (UTC)" for 5h: 2026-10-11 (in 147h), want 2025-10-11;
"read true: held until 2027-10-03 15:00"), as do TestParseClaudeUsage and
TestClaudeResetTimeAtNewYear on the changed rows. With the bound but
fresh reads not through elapsed, it fails with "read true: 5 hours
Used:100 ResetsAt:<4 days ago>".
Verified, under a temp HOME with GOPATH, GOMODCACHE and GOCACHE pinned:
go vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go
build, go test -tags nogui ./... (all pass), and go test -race -count=20
-run 'TestClaudeResetTime|TestParseClaudeUsage' ./internal/provider.
Not run: claude /usage on a real account read days late. Unchanged: a
week's time alone read minutes after its reset still reads as tomorrow's
(at most a day).
* gateway: a subscription account that fails with a window full rests until it renews, but no more than 8 days, so a reset time read a year off no longer benches it for that year
restAfter's default branch (a failure that isn't credit, quota, a rate
limit or verification: a 502, a sign-in error) rests a subscription
account until its fullest window renews, by c.full(now). That time is
the vendor's reading, not the failure's: Claude Code's /usage text, read
by claudeResetTime, takes a date with no year for next year when it reads
it wrong, and the account then sat out ~365 days until the user noticed
and lifted it by hand. The failQuota branch of the same function already
holds every rest, window included, to longestQuota (a week's window and a
day over); the window rest here now takes the same bound:
min(t.Sub(now), longestQuota). A window renewing within 8 days rests
exactly as before.
Why longestQuota and not the full window's own Span: Allowance.Full
returns only the renewal time, so the call site doesn't know which window
was full or how long it runs; Span is zero where a reading doesn't say
(plugin windows may not); and a failure that isn't about quota must not
sit out longer than one that is, which the quota branch already caps at
8 days. Bounding by Span would only shorten a misread 5-hour window from
8 days to 5 hours, and would have to change the quota branch with it, so
it is left for a change of its own.
Siblings: restAfterMarked is the only place that stores a rest
(restingUntil). Its other branches are bounded already: failQuota by
longestQuota (resets in the body, the noted header, the window and
Retry-After alike), failRate by longestWait and longestRateRest, credit
and verify by constants; a key never takes the default branch's window
rest (c.p.Account != nil). Every other use of a window's reset (capHeld,
Allowance.Full/Renewal/Restarts/Pace in weighing, the sink condition) is
computed again from the latest reading on each request and stores
nothing. Not changed here: cappedError's soonest renewal still becomes
the agent's Retry-After uncapped, and a stored window rest isn't lifted
when a later reading corrects the time (only OnRenewed or a manual
Unrest does), so a bad reading can still bench an account for up to 8
days. Claude's parsing is fixed in the commit before this one, and a
Claude account's window rest is lifted by a renewed reading in #1200.
docs/subsystems/gateway-routing.md says so in the runtime path; its
gateway test list already takes the new test by its TestRouting prefix.
Verified under a temporary HOME with GOPATH, GOMODCACHE and GOCACHE
pinned: go vet ./..., go build, GOOS=linux go build -tags nogui and
GOOS=windows go build pass. TestRoutingHoldsAWindowRestToLongestQuota (new): a
Claude account whose window is full with ResetsAt a year out, failing
502, rests at most longestQuota; a control renewing in 3h rests 3h.
Without the change it fails: "window read a year off: rests
8759h59m59.999682292s by window (other), want at most 192h0m0s". It
passes with -race -count=20, with TestOrderedKeepsAnAccountAt98 and
TestQuotaRestsUntilItsReset. The routing reference's gateway test set
passes.
go test -tags nogui ./... ran with the machine at a load average of
~125 on 12 cores, and three packages failed there, none on this path:
internal/davsync timed out at 10m (it passes alone in 353s);
TestSetAnswersWhileDevinAndProvidersHang fails on
|
||
|
|
5ca143bff3 | provider: Claude usage reads the reset times Claude Code 2.1.285 on prints, "Oct 9, 2:59pm (UTC)" with a comma where "at" was (#631) — claudeResetTime took only "Oct 9 at 2:59pm", so the weekly windows lost their reset time. It now takes "Jan 2, 3:04pm", "Jan 2, 3pm" and the same with a year; TestParseClaudeUsage covers them (fails without the change). go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build and go test -tags nogui -count=1 ./... under a temp HOME pass. | ||
|
|
34538b0a4b |
Preserve fresh Claude headers and shorten tray cache labels (#548)
* Preserve fresh Claude headers and shorten tray cache labels * Keep local permission errors separate from Claude account refusals |
||
|
|
e0121f71b9 |
Preserve dated Claude usage when allowance is unavailable (#511)
* fix(provider): preserve dated Claude usage after unavailable reads * fix: clarify cached Claude usage and preserve account return |
||
|
|
a2714f7506 | claude: a Claude account's usage resets are read in the zone /usage names on Windows too, and the provider tests pass whole on Windows (goal: 插件订阅 provider 跟内置的所有逻辑都要一模一样, run on Windows and Linux) — Windows has no time zone database, so "resets Oct 1 at 3:30pm (Asia/Shanghai)" was read in the machine's own zone, hours off for anyone not in that zone; magpie now carries the database (time/tzdata). The provider tests set HOME but not USERPROFILE, which Windows takes the home from, so on Windows they read the test box's own agent sign-ins (Gemini models in a list, an extra provider, a Cursor version); every test home sets both now. Tests whose fake CLI is a shell script (Claude Code's auth status, codex --version, macOS's security) skip on Windows, as the fake devin did, and file mode bits go unchecked there. TestParseClaudeUsage fails on Windows without the change. On Windows and on Linux (WSL) the provider package passes whole, three times on Windows (15 failed before), the plugin package and the move/plugin/Grok/Devin/usage tests five times over on Linux. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass. | ||
|
|
9bf6d43527 |
claude: magpie no longer asks Anthropic for a Claude account's usage, and adding a Claude account warns of a ban (the user: 不要伪造任何的 Claude 请求,能否通过 claude cli 获取 / 我害怕被封; 添加账号时要像其他的 Subscription Provider 一样显示风险提示) — the usage was read by magpie itself calling Anthropic's /api/oauth/usage with the account's token, on a timer, for every saved account, and Claude's usage-limit resets (cedar_ember) were listed, spent and auto-spent the same way. Now the usage is what Claude Code's own /usage prints (claude -p /usage, a command Claude Code answers itself without asking a model), run only when Usage is opened or Refresh pressed (GUI, magpie quota, accounts, TUI), for the account Claude Code is signed in to, at most once in 30s; a saved account shows only what Claude Code reported while using it, or the last reading kept. The Claude resets, Auto-use and their settings are gone (Codex's stay). Session jsonl files hold no usage figures, so they aren't read. Starting a Claude sign-in or import (GUI and CLI) first says Anthropic may suspend or ban the account and that it is at the user's own risk. TestClaudeWindowsAsked, TestParseClaudeUsage, TestClaudeUsageRunsClaudeCode, claude-usage-asked.test.cjs and claude-risk.test.cjs fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; routing-auto-reset, codex-auto-reset, quota-families, tray-usages, click-scroll, old-webkit, plugin-signin, usage-alert, usage-ledger pass.
|