Commit Graph
5 Commits
Author SHA1 Message Date
yetone 41b5c0f19a accounts: Claude Code stays on the account it moved to till that is spent too (hxy920830 on X)
hxy920830 on X wants Claude Code to use one account to 98%, move to the
next and use that one to 98%, not jump back the moment the first resets.
No setting gave that. NextLogin (#408) signed the agent back in to the
first account as soon as it was under 90% again, under every routing.
KeepLogin pins one account, In order goes back too, and switch only
went back as well.

Where the sign-in is what spends the accounts (Claude Code, whose own
requests never pass the gateway, and Codex under switch only), the
sign-in now stays on the account it moved to until that one is spent
or fails, then moves on to the next ticked account with room. It goes
back to the first only where the user said the first comes first:
routing In order, or KeepLogin. Codex through the gateway keeps going
back, because Smart routing sends its requests to the first account
(#209) and the sign-in follows it. This is returnsToFirst in
codex_switch.go. The return record is still written, so turning In
order on later goes back as before. An old record is ignored otherwise,
and its "First again" label (logins.go) is hidden. No user setting is
overridden: In order and KeepLogin keep their meaning.

GUI: the Routing note for Claude Code (and Codex under switch only) now
says it stays on the account till that one is 98% used too. The two
switch-only tooltips no longer promise going back. New and changed
strings are in zh, zh-TW, ja and de. app.js lost a 3-line comment so
the core script budget holds without a raise (core 1,531,108 of
1,533,105).

Run on mote at 94cd5edde (base d307aa648):
- go test -tags nogui -count=1 ./internal/provider/: ok. New test
  TestSignInStaysOnTheAccountMovedTo (claude, claude switch only, codex
  switch only stay; claude in order goes back).
- TestCoreScriptBudget and the gui package's budget tests: ok.
- go vet -tags nogui ./internal/provider/ ./internal/gui/ with GOOS
  darwin, linux and windows: clean. gofmt -l: clean.
- GUI, Chromium and WebKit: account-return, switch-only,
  claude-switch-only, i18n-keys, keep-login, routing-signin, gui-zh-tw,
  gui-de, gui-ja. 116/117 and 64/65 passed. The one red is gui-ja's
  "{agent} three times" string, red on origin/main dd7442f28 too (#4580).
- Full suite at the earlier base 76c130b29, -timeout 45m: provider ok.
  Reds, all already filed: TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514), TestCodexListTagNotKeptOverAFreshFile (#4216),
  TestUnroutedRequestShownThenAnswered (#4200). TestCoreScriptBudget
  failed by 41 bytes, fixed above. internal/gui hit its 45m timeout in
  TestUpdateInATerminalRestartsTheAppWithoutCuttingIt.

Broken by hand: with returnsToFirst forced to true,
TestSignInStaysOnTheAccountMovedTo (all three staying cases) and
TestCodexSwitchOnlyStillSwitchesTheSignIn went red. With the note forced
back to the "back to the first" text, the new account-return GUI test
went red in Chromium en and zh.

Not tried: a real Claude account moving and its first renewing (needs
two real windows to run out), runs on the Windows and Linux boxes, and
the full GUI suite at this head.
2026-11-01 05:23:58 +08:00
yetone d31710c71d provider: the switch off a spent Claude account reads saved accounts it has no reading of (cn_Vincentzyx on X)
cn_Vincentzyx on X: Claude Code not connected to magpie, only its
sign-in switched ("Only switch Claude Code's sign-in"), the accounts in
the order Settings' Make first gave them. The account Claude Code was on
ran out and it was never moved. ef2b2d1e3 (#1907) let switch-only move
a sign-in kept on one account; this is the other half.

Cause: NextLogin moves on only to a spare whose allowance is known and
not spent. A saved (not signed-in) Claude account's reading comes from
claudeWindows(ctx, user, false), which runs Claude Code's /usage for it
only on a card refresh (savedRefresh) and otherwise returns what was
kept, or errClaudeSaved when nothing was. Under switch-only no request
of a saved account passes the gateway (KeptToAgent), so
NoteClaudeLimits never hears of it either. A spare Claude Code wasn't
signed in to since (and with no reading on disk in last_quotas) was
"not read yet" on every look, and the switch never found one with
room.

Now, once the account Claude Code is on is spent, NextLogin reads such
a spare (no reading, or a read that failed for a reason other than the
account's own) through readSpare: the existing saved-account path,
Claude Code's own /usage run in the account's directory
(readSavedClaudeUsage), the same run a card refresh makes. Spares are
taken in order, one at a time, and reading stops at the first with
room. claudeWindows lets this read through (spareRead) only while it
has no windows for the account: once, then again only after
claudeUsageWait (5-15 min) while its read keeps failing, and never
under the 30s floor or while allowances are read only when asked
(holding). Nothing about how Claude Code is run, or the requests'
entrypoint, is changed.

Codex: its saved spares are read through their saved tokens
(savedLoginToken + wham/usage) in the same someLoginUsage call, with no
seen-in-use condition, so it had no such gap
(TestCodexSwitchOnlyStillSwitchesTheSignIn covers a spare magpie never
read before).

Run (mote, at this tree, rebased on c941e0877):
- TestClaudeSwitchOnlyReadsUnreadSpares (new) green, -count=3.
- go test ./internal/provider -count=1 -timeout 45m: ok (129s)
- gofmt -l clean; go vet ./internal/provider for darwin, linux, windows.

Broken by hand:
- NextLogin's readSpare call disabled: the new test is red
  (got ""→"" ok=false, the old behaviour).
- claudeWindows' spare gate reduced to spareRead(ctx) alone (no
  ws/wait check): red, "a spare run again on the next look".

Not tried: a real account switch with the reporter's accounts; this was
run in sandbox HOMEs with a stand-in for Claude Code's /usage only. With
Settings > Usage > Allowance reads at "When I ask", nothing is read in
the background, the switch included, as before: an unread spare stays
unknown there.
2026-10-30 13:11:36 +08:00
yetone ef2b2d1e3b provider, gui: switching only Codex's or Claude Code's sign-in moves it even when it is kept on one account (#1907, foobra; yifengz88611436 on X)
foobra's screenshot on #1907 (v0.1.1224 on) had both "Keep Codex
signed in to [the first account]" and "Only switch Codex's sign-in"
ticked. The account Codex was on had 0% of its five hours left and the
other had 100%. Codex stayed on the spent account, through a restart of
Codex Desktop and the CLI. With KeepLogin on, NextLogin never moves the
sign-in (#524), and switch-only sends nothing through the gateway. So the
pair meant that no account change ever happened, while the switch-only
tick says moving the sign-in is the one thing magpie does. Picking "the
first account" in the keep picker ticks KeepLogin too, so it is easy to
land in this state without meaning to.

Now Provider.keepsLogin() is KeepLogin && !SwitchOnly. loginSwitching,
keptAs (KeepLoginAs) and LoginRanks go by it, so under switch-only the
sign-in moves at the spent share and back once the first has room. That
holds whether the user kept the first account or one of their choosing,
and for Codex and Claude Code alike (switchedAgents). The saved
KeepLogin/KeepLoginAs is left as the user wrote it, and it holds again
once switch-only is unticked. SetKeepLogin(true) and SetKeepLoginAs
refuse while switch-only is on, because there they would do nothing.
GUI: the keep tick and picker are hidden while switch-only is on, and
keptLogin() and the Routing note treat KeepLogin as off then. The
Accounts hint for a switched sign-in said "the gateway moves on to the
next", which isn't true under switch-only. It now says "magpie moves on
to the next", which holds in both modes, in en, zh, zh-TW, ja and de.
app.js ends up 29 bytes smaller than origin/main, so TestCoreScriptBudget
holds without a raise (a first version had been 661 over).

Run (on mote unless said otherwise):
- TestSwitchOnlyMovesASignInKeptOnOne (Codex, kept on the first and on a
  chosen account) and TestClaudeSwitchOnlyMovesASignInKeptOnOne are new.
  With keepsLogin put back to plain KeepLogin by hand, all three cases
  are red ("switched to \"\""). With the change they are green.
- go test -run 'SwitchOnly|KeepLogin|KeptLogin|CodexLoginOrderedAndKept'
  on internal/provider, agent, gateway and davsync: ok at this head
  (rebased on accb951ab).
- keep-login.test.cjs has 4 new cases (switch-only with keep on the
  first or on spare, en and zh). With app.js put back to origin/main,
  4 of 12 are red in Chromium. The GUI files keep-login, switch-only,
  claude-switch-only, i18n-keys, agent-switch-accounts, pool-ends,
  account-return, routing-account-order and account-arrange gave
  185 of 186 in Chromium and WebKit, at base e863f520c. The one red was
  claude-switch-only "chromium 420 ja", where the element was detached
  before the test clicked anything, under load ~90. That file passed
  32 of 32 twice alone. Filed as #4344.
- After the hint and budget change: go test -run 'Budget|I18n'
  ./internal/gui ok; keep-login, i18n-keys and switch-only .test.cjs
  60 of 60 (Chromium and WebKit).
- gofmt -l is clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui
  and GOOS=windows go build are ok (at base e863f520c).
- The full go test ./... -timeout 45m follows this push, once mote is
  free; its result goes on #1907.

Not tried: a real ChatGPT or Claude account, and Codex Desktop itself.
Also not covered here: Claude Code not connected and switched by Make
first (cn_Vincentzyx on X). There, a saved Claude account has no usage
reading unless magpie has heard of it, so the switch can find no spare
with known room. That is a separate cause.
2026-10-30 11:44:40 +08:00
yetone ec3dad75ae provider, gateway, gui, cli: Claude's accounts can be kept to Claude Code's own sign-in, magpie serving nothing on them (myduso)
myduso on Discord asked for an exclusive mode on Claude's subscription:
switch the credentials locally and proxy no traffic, since Claude Code
is used on its own and the SDK run magpie's bridge makes spends more of
the allowance than the interactive client.

The switch-only setting Codex has (#1907) now also takes claude. On it,
Provider.KeptToAgent is true and On() false, so the gateway, catalog,
groups, fallbacks, search, draw and embeddings serve nothing on the
Claude accounts, and a request naming them is refused 404 saying they
are kept to Claude Code's own sign-in and how to undo it (switchedOff).
Switching Claude Code's sign-in when an account is used up, and reading
usage, go on: both are gated by leftAlone on the raw Off, which this
doesn't set. Groups magpie found keep their Claude members
(heldOffEntries). Nothing is sent as Claude Code: magpie only writes and
restores Claude Code's own credentials as it already did.

GUI: the tick under Claude's accounts, shown with one account too, drawn
by the block Codex's tick had (one block now, its strings picked by
agent). app.js stays under its budget with no raise (1,311,711 of
1,311,735). CLI: magpie accounts switch-only <codex|claude> [on|off].
Docs: reference.md, subsystems/providers-accounts.md.

Run on mote at this tree on 06950fbbb; then rebased on aeacc6358 (two
app.js comments shorter) and go test -run TestCoreScriptBudget ./internal/gui
run again at the pushed head: ok.
- gofmt -l internal: clean. GOOS=linux go vet -tags nogui ./internal/...
  and GOOS=windows go vet ./internal/...: ok.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/gateway ./internal/cli ./internal/gui: all ok.
- node --test (Chromium + WebKit, every language the file covers):
  claude-switch-only (new), switch-only, keep-login, i18n-keys, gui-ja,
  gui-de, gui-zh-tw: 85 of 93 in one run beside other sessions' full GUI
  suites (load 23-73); the 8 reds (claude-switch-only chromium en/zh,
  gui-de and gui-ja in both engines) were not captured, and those files
  then passed alone twice (35 of 35 each run).
- Earlier, at ecbd694a2 (on ddf31ca30): internal/agent ok; add-sheet,
  api-key-usage pass; account-models and account-proxy 40/40 alone after
  8 browserType.launch timeouts at load 40-70 from other sessions.

Broken by hand, watched red:
- On() without the KeptToAgent term: TestClaudeSwitchOnlyServesNothing-
  AndStillSwitches and TestClaudeKeptToClaudeCodeServesNothing fail (the
  gateway ran Claude Code through the bridge, 200).
- SetSwitchOnly refusing claude: both fail.
- app.js without the claude branch: claude-switch-only 16/16 red in
  Chromium, switch-only (Codex) 16/16 green.

Not tried: real Claude accounts or a real Claude Code (the switch itself
is the existing path, tested with a fake claude in a sandbox HOME); the
whole GUI suite (only the files that open Claude's or a provider's
editor, plus the i18n ones); Linux and Windows test runs (vet only).
2026-10-29 04:15:56 +08:00
yetone d6fd9b3a36 provider, agent, gui, cli: Codex can switch only its own sign-in between accounts, never through the gateway (#1907, foobra)
foobra wants Codex Desktop and the CLI kept on their own ChatGPT
subscription, moved to the next saved account when the signed-in one is
used up and back when it resets, without magpie's gateway in the way.
magpie already did the switch (codex_switch.go: 98%, 100% In order, back
under 90%, unknown usage never switches), but with more than one Codex
account on it also wrote openai_base_url into config.toml so a refused
request fails over through the gateway (codexFailover). There was no way
to have the first without the second.

Now Codex's subscription has SwitchOnly (providers.json "switchOnly",
opt-in, off by default so nothing changes for anyone who doesn't tick it):
- provider.SetSwitchOnly / provider.SwitchOnly; only Codex's own
  subscription takes it. An unreadable providers.json reads as on, so
  magpie's own failover is never written on a guess.
- agent.codexFailover is false when it is on: no openai_base_url, no
  failover note, no magpie models in Codex's list. A base URL the user
  wrote stays.
- The account Save copy keeps it.
- GUI: "Only switch Codex's sign-in" tick under Providers > Codex's
  accounts (with more than one on), posting provider/switchonly; the
  Agents row's failover title points at it. zh, zh-TW, ja, de.
- CLI: magpie accounts switch-only codex [on|off].
- docs/reference.md and providers-accounts.md say what it does and that
  an open Codex app or app-server stays on its account until reopened.

Run (darwin, sandbox HOME, -tags nogui):
- go test ./internal/provider -run 'TestCodexSwitchOnly|TestCodexSwitchedWhenUsedUp|KeepLogin'
- go test ./internal/agent -run 'TestCodexSwitchOnly|TestCodexFailover|TestCodexOwnModelThroughMagpie|TestCodexHandWritten'
- Broken by hand, each red: codexFailover guard removed (agent test),
  SwitchOnly dropped from the Save copy, SwitchOnly reading false on an
  unreadable file (provider test), the tick drawn unticked (GUI, 16/16).
- GUI: switch-only (new), keep-login, agent-failover, gui-ja, gui-de,
  gui-zh-tw in Chromium and WebKit, en/zh/ja/de at 1100 and 420: 77 pass.
- The built binary's `accounts switch-only` in an empty sandbox HOME:
  reads off, refuses on with no Codex provider, refuses claude.
- gofmt -l clean; darwin go vet.

Reviewed and rerun after rebasing onto eba718736 (darwin, sandbox HOME).
The rebase met #1899's withPicks; SwitchOnly is copied there now.
- gofmt -l clean; go vet -tags nogui on darwin, GOOS=linux and
  GOOS=windows for internal/provider, agent, cli, gui.
- go test -tags nogui -timeout 40m ./internal/provider ./internal/agent
  ./internal/cli ./internal/gui: all ok. (On the base before, provider's
  TestCopilotSecretToolUsername timed out once under load: #1627,
  untouched code, 3 of 3 alone.)
- Broken by hand, each red: the codexFailover guard removed
  (TestCodexSwitchOnlyKeepsCodexOffTheGateway), SwitchOnly dropped from
  the Save copy, from withPicks, and an unreadable providers.json
  reading off (TestCodexSwitchOnlyStillSwitchesTheSignIn).
- GUI: switch-only, keep-login, agent-failover, gui-ja, gui-de, gui-zh-tw
  in Chromium and WebKit at 1100 and 420: 77 of 77 pass.

Rebased again onto 81f587d7b (clean; it touches provider, cli, gui but
not agent): vet on all three OSes again, go test -timeout 40m
./internal/provider ./internal/cli ./internal/gui all ok, -run TestCodex
./internal/agent ok, and the 77 GUI cases above pass again.

Rebased once more onto d45b20757 (clean; no lines of this change touched):
vet on all three OSes, -run 'TestCodex|KeepLogin|SwitchOnly'
./internal/provider ./internal/agent ok, switch-only and agent-failover
in Chromium and WebKit pass.

Not run: not tried with real ChatGPT accounts or the Codex app.
2026-10-13 14:35:08 +08:00