The built-in sign-ins that come back to a port on this machine (Codex,
ChatGPT API, Devin, Kiro, Zed, Gemini, Antigravity) already took the
pasted address. Their plugins didn't: a subscription moved onto its plugin
(Devin, Kiro, Zed) or a plugin's own browser sign-in (Trae) showed only
"the account shows up here as soon as you're done", and a browser on
another computer than magpie's ended on a page that won't load with no way
to finish.
Now a plugin's "auto" sign-in whose page names a port on this machine to
come back to (redirect_uri, auth_callback_url, callback on localhost, or
Zed's native_app_port: loopbackPorts) sets pasteCallback, so the GUI shows
the same Callback URL field as the built-ins, and `magpie plugin login` /
`magpie accounts add` ask for it too. SubmitSignInCallback opens the pasted
address on 127.0.0.1 at that port, never through a proxy, so the plugin's
own listener finishes the sign-in as it would for the browser; an address
on another port or host is refused, a page that sends the browser on to a
page that comes back to the same port (Kiro's AWS sign-in) becomes the next
page to open. Command Code's page posts its key from script, which leaves
no address to paste, so it keeps offering the plugin's API key way.
Verified: TestPluginSignInTakesAPastedCallback (fake plugin with a loopback
sign-in, $FAKE_LOOPBACK) fails without the wiring in pluginBegin ("a
sign-in that comes back to a port here takes no pasted address:
{... PasteCallback:false ...}") and passes with it, -race -count=10;
TestLoopbackPorts covers the community plugins' page addresses.
go test -race ./internal/provider ./internal/plugin and the root package's
Plugin|Login|SignIn tests pass; vet, gofmt, darwin/linux nogui/windows
builds pass. With the real community Devin, Zed and Kiro plugins in a
sandbox HOME, each sign-in offered the field and a pasted address reached
the plugin's listener and the vendor (bogus code refused by Devin 401, Zed
"couldn't be decrypted", Kiro 400). signin-paste-plugin.test.cjs (new),
signin-callback and signin-paste-codex pass in Chromium and WebKit, en and
zh, at 360px; no new strings. Not tried: a real successful sign-in from a
browser on another computer.
11 KiB
11 KiB
Providers and accounts
The gateway can send a request to three kinds of upstream:
- A provider in
providers.json: an endpoint with one or more API keys. - A subscription account: an agent's own sign-in (Codex, Claude Code, Copilot and others) that magpie reads from the agent's store, or an account signed in from magpie.
- A plugin's account: a subscription served by an OpenCode plugin.
Package internal/provider holds all three, the routing groups built over
them, and what is left of each one's allowance.
Plugin ownership of the deprecated built-in subscriptions is in Provider and plugin ownership. How the gateway orders keys and accounts per request is in Gateway routing and fallback.
Responsibilities and sources of truth
| Part | Responsibility | Source |
|---|---|---|
| Provider | Provider holds the endpoint bases for each Protocol (Chat, Responses, Anthropic, Gemini), the keys, Fallback, Routing, Sink, Affinity, concurrency and queue limits, Headers, Proxy, models, and balance settings. Account is set only for a subscription and is never saved (json:"-"). |
provider.go |
| providers.json | Path is providers.json in magpie's config folder. A read that fails returns ErrUnreadable and is never treated as empty. store writes atomically through a rename and drops the cached catalog (catalog.Touched). |
provider.go |
| The list | All merges stored providers with Accounts(). An entry with no URL stores the user's picks for an account. The order is file.Order. Reads within one request share one build (Hold, #746). |
provider.go |
| Keys | Several keys per provider. Requests go to the first key that is on; the gateway moves on when it is out or rate limited. Key stays the first, so other code reads it as before. A key can be made for one protocol (KeyProtocol). |
keys.go |
| Subscription accounts | Accounts reads the credentials each agent keeps on disk or in the macOS Keychain; nothing is stored twice. A built-in that has moved to its plugin is dropped (Moved), and plugin accounts are placed in its slot (placeMoved). Sign adds the account's own auth, then the user's Headers. |
account.go |
| Token refresh | Each account refreshes through its vendor. For example, codexToken refreshes within 5 minutes of expiry and writes the rotated tokens back into ~/.codex/auth.json for Codex CLI. A 400 or 401 from the vendor's refresh endpoint means it was refused (refreshFailed). |
account.go |
| Saved accounts | logins.json remembers every Codex and Claude Code account seen signed in. A switch moves the credentials into the agent's own store and saves the ones it replaces. A saved account that is on (SetLoginOn) stands behind the agent's own. Its tokens stay in logins.json. |
logins.go, logins_on.go |
| Sign-in in magpie | StartSignIn runs the vendor's own browser OAuth with its client and takes the tokens at a callback on this machine. A browser that can't reach that callback (magpie on a server or in Docker) ends on a page that won't load; its address, pasted (SubmitSignInCallback), goes through the same callback handler (pastedCallback). A plugin's browser sign-in (auto) whose page names a port on this machine to come back to takes its pasted address too: magpie opens it there on 127.0.0.1, for the plugin's own listener (pluginCallback, loopbackPorts); Command Code's page posts its key from script, so it offers the API key way instead. A Claude account is signed in by Claude Code itself. |
signin.go, claude_signin.go, pluginsignin_paste.go |
| Used-up switch | When the account Codex or Claude Code is signed in to has used its allowance, the agent is signed in to the next account that is on and has room (#208). | codex_switch.go |
| Routing groups | User-made groups from the Routing view, and auto-found groups of the same model across providers (derived, never stored). Members are provider/model[:effort]. A group can nest another (group/<id>); SaveGroup refuses loops. Match takes a glob or re: pattern (#766). |
group.go |
| Allowance | Quotas asks every vendor at once: subscription windows (SubscriptionUsage), key-bought plans (PlanQuotas) and key balances (KeyBalances). The Usage page, magpie quota --json and GET /v1/magpie/quotas read it. A single card can be read again on its own (#840). A remote magpie's cards come from what it has kept (CachedCards, GET /v1/magpie/quotas/cards) and are never read there for this computer except by a card's refresh (POST /v1/magpie/quotas/refresh, throttled per card). |
quotas.go, subscription_usage.go, planquota.go, balance.go, quota_refresh.go, remote_quotas.go |
| A key's own windows | A sub2api key given a spending limit per 5 hours, day or 7 days answers its /v1/usage with rate_limits and no remaining (readSub2APIKeyLimits). With the Balance URL set to it, the key's card shows them as the windows 5 hours, 1 day, 7 days, in USD, beside a balance field the user wrote; Check balance and magpie provider show give what its fullest window has left (Balance). KeyAllowance gives routing the same windows: never waited for, read in the background at most once a minute, a failed read keeping the last, and seeded from the card's last reading (quotas.json) after a restart. A key bought as a plan (Kimi Code, GLM Coding Plan and Z.ai, MiniMax Coding Plan, OpenCode Go, Command Code: planQuotaSourceOf) gives routing its card's windows the same way (planKeyWindows, Zhipu's team quota behind a key with none of its own); the Usage page's read of its card is taken at once (noteKeyAllowance), and a key there that turns out pay-as-you-go, with no windows, is asked again only every 30 minutes (keyNoWindowsAge). StepFun's Step Plan is read through a sign-in, not the key, and isn't one of them. |
sub2api_usage.go, key_allowance.go |
| Last served | served.json records when each account, plan or key last answered, shown as lastServedAt (#570). |
served.go |
| Protocol detection | Works out which APIs a base URL speaks when a provider is added. | detect.go |
Runtime path
- Add. The Providers page adds a key provider through
AddorSave, or an account throughStartSignIn. An agent signed in on its own is found byAccountswith no step from the user. - Resolve. The gateway calls
provider.ResolveorGroupForwith the model name, then plans candidates over the provider's keys or accounts. - Sign. For each try,
Signgives the request its auth. A token about to expire is refreshed first, and the rotated tokens go back to their one holder: the agent's store, orlogins.jsonfor an account standing behind it. - Allowance. Usage endpoints are rate limited, so
SubscriptionUsagecaches its results: a cached copy comes back at once and a stale one is refreshed in the background. When an account's windows start again,OnRenewedtells the gateway so a resting account can come back. A key's reading that finds a window it was full in (atSpentShareOfits routing) full no more, or full till sooner, tells it too, as agent""and the key'sKeyAllowanceID, so a key out of its limit is back once the limit is raised or its usage reset; a first reading, a failed one, or one that finds it fuller tells nothing.
Constraints and failure behavior
- Each refresh token has exactly one holder. Vendors rotate tokens on refresh, so two copies of one token would sign each other out.
savedTokenMustops two requests refreshing one saved account at once. - A Claude subscription account is used only for Claude Code, through the genuine Claude Code binary (see Claude subscription bridge).
- An unreadable
providers.jsonis an error, never an empty list. Saving over an empty list would lose every provider. - When
logins.jsoncannot be decoded as saved accounts, reads keep the accounts read before. Before saving a change,keepUnreadLoginscopies the original file aside, including valid JSON with invalid account field types, so its credentials can be recovered. If the existing file cannot be read or the backup fails, saving returns the error and leaves the original file unchanged. A missing file can be created normally. - A switched-off provider (
Off, #163) stays saved. The gateway says it is off instead of saying the model is unknown. - A provider removed or switched off takes its models out of the catalog. Agent wiring's
Reseatmoves agents off them. - A gateway key held to some accounts (#905) is held to those of the providers its list names: a provider it names no account of, the key uses as it always did. The list keeps an account by its stable id —
logins.json'sid, set once written and kept through renames; the agents that keep no logins (and plugins) are known by a hash of the account's name, so an account renamed there drops out of a key's list, holding it closer, never wider. Storing an id for every agent is a larger change, not this one's. - A sub2api key's windows are fixed: each starts at the key's first request after it was empty, aligned to the relay's day, and a window not started yet says
used 0with noreset_at, so it is taken to renew a span from now. They count this key's spend only, not the relay's accounts behind it.ForgetBalances(a key or Balance URL changed) has every key's windows read again at its next ask, a read begun before dropped; until then the last reading stands, fresher than the card's, which routing's reads never write. A key changed is another key (keyID), with windows of its own. - Whether a Codex account spends its credits (
CodexNoCreditsin settings, opt-out, by lower-cased email) is the gateway's routing only. It never changes which account Codex is signed in to:NextLoginmoves off an account at its routing's spent share, 100% at most, credits or not.
Verification
go test -tags nogui ./internal/provider -run 'TestAccount|TestCodex|TestGroup|TestQuota|TestLogin|TestSignIn|TestSeveralKeys|TestKeyProtocol|TestSetKeyWeight|TestReadSub2APIKeyLimits|TestSub2APIKeyLimitsOnItsCard|TestKeyAllowance|TestPlanKeyAllowance|TestPayAsYouGoKeyAskedSeldom'
go test -tags nogui ./internal/provider
The package's tests use a home of their own (testenv). Never point them at
real ~/.codex, ~/.claude or ~/.config/magpie files. Check a change
against real accounts in a sandbox HOME, as Code standards describes.