Commit Graph
11 Commits
Author SHA1 Message Date
nrenvo 7b151cf549 provider tests: the stale copy's prompt return is asserted by a held refresh, not a 40ms bound (#1586)
TestSubscriptionUsageServesStale said "a stale copy comes back at once while it
refreshes" by timing the call: a 50ms ctx and a 40ms bound on time.Since(start).
On a loaded machine the bound is what fails -- the report has "waited
311.332917ms for a stale copy" -- so the test said nothing either way about the
guarantee it guards. Which step of the call took those 311ms I did not
establish; the assertion no longer needs to know, because nothing is timed.

The refresh is now held at the vendor instead: CopilotUserURL points at an
httptest server whose handler blocks until released, so the refresh cannot
finish. The copy is asserted to come back while SubscriptionUsageReading still
reports a refresh in flight, which is the guarantee itself rather than a proxy
for it, and <-reached proves a refresh really went out, so the test cannot pass
by never starting one.

The test also resets c.asked, the one field of the cache it primes that it had
left alone. It is not set when this test runs -- AskUsage is the only setter and
quota_asked_test.go's own restart clears it -- so this is not the reported
failure's cause; it completes what the test already does to c.data, c.at and
c.pending, so the test does not depend on another file's cleanup.

What was run:
  gofmt -l internal/provider/subscription_usage_test.go   (no output)
  go test -tags nogui -count=1 -run TestSubscriptionUsageServesStale ./internal/provider
  go test -race -count=20 -run TestSubscriptionUsageServesStale ./internal/provider
  go test -tags nogui -count=1 ./internal/provider        (whole package)
  go vet ./... ; GOOS=linux go vet -tags nogui ./... ; GOOS=windows go vet ./...
  go build ; GOOS=linux go build -tags nogui ; GOOS=windows go build
  go test -tags nogui ./... (53 of 54 packages ok; internal/gateway reds only in
    the parallel run, which this change cannot reach -- it does not compile this
    file -- and which is filed already as #1543/#1544)

Without the change: with SubscriptionUsage's "if !have && pending != nil" broken
to "if have && pending != nil" -- a copy that has data waiting for the pending
refresh -- the new test fails with

  subscription_usage_test.go:97: got [], want only claude: a copy that waited
  for the held refresh comes back with nothing
  --- FAIL: TestSubscriptionUsageServesStale (12.51s)

Reviewed and landed from nrenvo/magpie fix/1586-stale-copy-test (b5bda612),
posted on #1586 because PRs are collaborators-only. Run at the cherry-pick on
origin/main 398f8469b, temp HOME, caches pinned, load average ~120-135:
  go test -tags nogui -race -count=50 -run 'TestSubscriptionUsageServesStale$' ./internal/provider   ok
  go test -tags nogui -race -count=20 -cpu 1,2 -run TestSubscriptionUsage ./internal/provider        ok
  go test -tags nogui -count=1 ./internal/provider                                                   ok 149.7s
  go vet, GOOS=linux and GOOS=windows go vet -tags nogui ./internal/provider; gofmt -l               clean
Broken by hand in SubscriptionUsage, two ways, each red:
  "if !have && pending != nil" -> "if pending != nil" (a stale read waits for
    the refresh, up to ctx): FAIL at :96, got [] (11.6s)
  an unbounded <-pending for every read: FAIL at :96, got the Copilot card read
    after the held refresh gave up (21s)
2026-10-11 21:22:18 +08:00
yetone 655cdd0abd provider: the hook a landed usage refresh tells is held atomically and told before the refresh is done (#1023) — OnSubscriptionUsage was a plain package variable that the background refresh SubscriptionUsage starts read after closing its pending channel, unsynchronised, while the tray sets it as it starts (watchTrayUsage) and a test swapped it: go test -race ./internal/provider failed in TestSubscriptionUsageServesStale when TestSubscriptionUsageReadingWhileStale's refresh, waited for by its pending, was still reading the hook after it (subscription_usage.go:185 against subscription_usage_test.go:84). OnSubscriptionUsage is now a setter over an atomic.Pointer, so setting it while a refresh is under way is safe in the app too, and the refresh tells it before closing pending, so whoever waits for a refresh has it finished, hook and all, and may change what it reads. The tray's hook only signals a buffered channel and never waits, so telling it first holds no one; the test's hook no longer blocks either. With -race -count=20 the two tests failed on main with the DATA RACE and pass now; go test -race -tags nogui ./internal/provider passes in full; go vet, gofmt -l, darwin, linux and windows (nogui) and windows builds pass. 2026-10-06 19:59:29 +08:00
yetone 37f2a2851f gui: a Usage card has a refresh of its own, left of its "Updated" time, that reads that card alone (Hu9956, #840: 在更新时间左侧加一个刷新按钮,鼠标悬停在套餐卡片上时显示,可以单独刷新某个套餐的用量,不用刷新全部) — only the page's Refresh read the allowances again, every card at once. Each card now has a ↻ that opens left of "Updated 3 min ago" (or of a failed reading's reason, or under a balance) as the card is hovered; at rest the line starts where the card's others do. A click POSTs /api/usage/quotas/refresh?provider=&user=: provider.RefreshUsage reads that subscription account, plan key or balance key again (a Claude account by running Claude Code's own /usage, as the page's Refresh does) and puts it in place in the caches, the other cards not asked and kept as they were read; the button turns while it reads. zh, ja, de strings. TestRefreshOneCard (two keys of one relay and another provider: a refresh asks that key alone, the others keep their figures) and the plugin case in TestPluginUsage (one account of three read) fail on the old code; usage-card-refresh.test.cjs (WebKit, en and zh: aligned at rest, left of the time when hovered, a click reads that card alone, turns meanwhile, doesn't scroll; a failed card's own button) fails on the old code. usage-quota-head, usage-checkin, click-scroll, quota-read-at, balance-parts, claude-usage-asked, gui-ja and gui-de pass on WebKit. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass. 2026-10-05 08:59:47 +08:00
yetone 9f94b2156b copilot: an account on GitHub Enterprise Cloud with data residency (*.ghe.com) signs in, is read from copilot login --host and is served at its enterprise (#723, guitaoliu: magpie skipped a Copilot CLI sign-in whose host wasn't github.com and its own sign-in was fixed to github.com) — a Copilot account now remembers its host (copilotApp.Host, "host" in its logins.json auth; none for github.com) and every URL it uses follows it, as VS Code's Copilot Chat, copilot.lua and avante do (checked against the issue's references and GitHub's docs): the device flow at https://<name>.ghe.com/login/device/code and /login/oauth/access_token with the same Copilot OAuth app, the device page there, whose account at https://api.<name>.ghe.com/user, its plan and usage at api.<name>.ghe.com/copilot_internal/user, its session token at api.<name>.ghe.com/copilot_internal/v2/token, and its requests, model list, Auto and terms at the session's endpoints.api, or https://copilot-api.<name>.ghe.com when it names none (the provider's base too). A github.com account has no host and goes where it always did, the same vars and the same bytes. CopilotHost (copilot_ghe.go) takes only github.com or one <name>.ghe.com label (a scheme and a trailing slash are dropped; a port, path, user, deeper subdomain or any other host is refused), in the sign-in, the CLI and a host read back from logins.json, so a GitHub token is never sent to a host of the user's choosing. The Copilot CLI's sign-in: a github.com account first, as before; then one copilot login --host <name>.ghe.com signed in, its token under "https://<name>.ghe.com:<login>" in copilotTokens or the keychain; a GitHub Enterprise Server host is still left out. An editor's apps.json entry keyed "<name>.ghe.com:…" is read after the editors' github.com one and the CLI's. GUI: a Copilot device code's panel has "Account on GHE.com?" (zh 账号在 GHE.com 上?), which ends that sign-in and asks "Your enterprise on GHE.com" (你的企业在 GHE.com 上的地址) in a field (acme.ghe.com; Sign in stays off until it reads as <name>.ghe.com), then signs in there; that sign-in's panel offers "Sign in on github.com instead" (改为在 github.com 登录), as does a failed one. CLI: magpie accounts add copilot [--host <name>.ghe.com]. TestCopilotHost, TestCopilotGHESignIn (an httptest server standing in for acme.ghe.com, api.acme.ghe.com and copilot-api.acme.ghe.com through copilotGHEOrigin: sign-in, what is kept, models, a signed request, usage, nothing sent to github.com, a bad host refused) and TestCopilotCLIGHELogin are new and fail on the old code; copilot-ghe.test.cjs (webkit and chromium, en and zh: the link, the field refusing evil.com, a.b.ghe.com, ports and the like, the posted site, the way back, cancels, no native select, no left-border accent) fails on the old code. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass; signin-link, signin-close, signin-again and routing-signin pass in webkit, signin-callback fails before and after the change alike. Not tried with a real GHE.com account: the URLs follow the references, and the CLI's token key for a ghe.com host is taken to be the github.com one's form. Accounts are still told apart by login alone; an EMU login carries its enterprise's suffix. 2026-10-04 06:56:04 +08:00
Zhaofeng 5fd503c789 fix(copilot): unify plan and entitlement identification (#510)
* fix(copilot): show subscription plans in account labels

* fix(copilot): unify plan and entitlement identification

* fix(copilot): prioritize limited quotas and Free access SKU

* fix(cursor): wait for complete CLI sign-in parameters
2026-10-02 19:34:33 +08:00
Bo Zou f56d105495 provider: a Copilot account signed in from magpie gets its allowance card, the editors' sign-in or not (#386)
The Usage page's Copilot card was asked only when copilotLogin(cfg) found the
editors' or the standalone CLI's own GitHub sign-in
(~/.config/github-copilot/apps.json or hosts.json, else ~/.copilot/config.json).
An account signed in from magpie itself is kept in logins.json, and
copilotLogin never looks there: with no editor and no Copilot CLI signed in on
the machine, no Copilot allowance was fetched at all. The Usage page, the menu
bar's usage picker and the gateway's GET /v1/magpie/quotas had no Copilot card,
while `magpie accounts` did show one (that path reads copilotLoginList through
builtinLogins, login_usage.go) and quotas.json kept a fresh reading written by
it — the account was known and readable, only never asked where the page looks.

copilotLoginList() covers both the editors'/CLI's own account and the accounts
magpie signed in (copilot_accounts.go), and loginQuota resolves each account's
own token by user, so the fetch is scheduled from that list now, as Kiro's,
ZCode's, Qoder's, Zed's and WorkBuddy's already are. The single-account branch
this replaces called copilotSubscriptionUsage with the editors' token for the
same account, which perLogin reaches the same way.

TestCopilotQuotaWithoutEditorsSignIn fails without the change ("no Copilot
card: an account signed in from magpie alone was left out") and passes with it.
go vet ./internal/provider/ and the internal/provider package's tests pass.
2026-10-01 14:43:13 +08:00
yetone 8a30a938d6 provider: Antigravity's usage shows only the models enabled again (antigravity 为什么又显示了没有启用的模型的用量?) — since #150 (2baad53) magpie offers a family of Antigravity's levels as one model (gemini-3.8-flash), but its quota still names each level's id (gemini-3.8-flash-high), so none of the windows matched what's enabled and chosenWindows, finding none, kept all 27. A window's id is now taken to the model magpie offers for it (AntigravityBase) before it's matched; on this machine, with Gemini 3.8 Flash enabled, 3 of 27 are kept: its high, low and medium 2026-09-29 00:21:18 +08:00
yetone 91aacfba1c tray: the menu bar's usage says what the panel does — a subscription's quotas are served stale while they refresh behind, and the menu bar, reading them every few minutes, showed each refresh only a tick later (53% beside a panel's 57%); it reads them again the moment a refresh lands 2026-09-28 14:16:42 +08:00
yetone a890311767 provider: a Gemini or Antigravity account's usage shows only the models chosen for it — Google reports a quota for every model the account can reach, and the card listed them all; it keeps the windows of the models the provider exposes (the ones picked, else its preset's), and all of them when none of those has one 2026-09-25 18:05:37 +08:00
yetone 2a90ed1030 usage: the page no longer waits on subscription quotas — they load on their own, in parallel, stale-while-revalidate (#6) 2026-09-24 12:49:51 +08:00
yetone 702477525a Claude subscription as a provider; model lists fetched, never compiled in
A signed-in Claude Code is now offered as a provider (claude/…) like Codex
and Copilot. Anthropic classifies another agent's system prompt as
third-party traffic even over Claude Code's OAuth, so magpie drives the
real local claude binary for every generation: the caller's tools are
bridged in over MCP (magpie claude-mcp-helper), and tool results resume
the same Claude Code turn. Token counting is estimated locally so the
OAuth token takes no side path.

No model list is compiled in any more. The built-in catalog, preset
defaults and Claude Code's alias list are gone; the picker shows the
models.dev catalog (refreshed in the background when stale) layered with
each vendor's own /models answer, and signed-in accounts fetch theirs at
start-up. Whether a model rejects temperature/top_p now comes from
models.dev rather than a name prefix.

The gateway retries a Chat request on the Responses endpoint when the
model is Responses-only, and remembers that. Recent calls keep the first
256 KB of request and response bodies, viewable in the call list. The
Usage tab shows subscription allowances (Claude, Codex, Copilot) as the
vendors report them.
2026-09-24 00:20:59 +08:00