provider: routed Until used up, Codex's sign-in stays on the account the requests went on to

WalterAIBuilder on X (11-04): two Codex accounts, A (Team, first) and B
(Plus), routed Until used up, affinity auto, stop at cap on. A used up
its 5h window (98% cap). Codex was signed in to B, then signed back in
to A once A had room, while B's week kept being spent until it was at
100%.

Cause: returnsToFirst (internal/provider/codex_switch.go) sent Codex's
sign-in back to the first account for every routing but Claude's and a
switch-only Codex's. Routed Until used up, the gateway keeps requests on
the account it moved to (routes.Drain, internal/routes/strategies.go),
and auto affinity keeps a conversation on whoever answered last while
the cache is warm (chatGPTCold, internal/gateway/affinity.go). So Codex
and the accounts list showed A in use, with B marked to go back from,
while requests through magpie stayed on B. The GUI note under Routing
also promised "back to the first" for drain. This is the hxy920830 rule
("not use one up, then the next") missing its Drain sibling.

Now, routed Until used up, Codex's sign-in stays on the account it moved
to till that one is used up too, the same as the requests. In order and
Smart still go back to the first. The Routing note has its own drain
text in en, zh, zh-TW, ja and de. The subsystem page says so.

Old data: a user signed back in to the first stays there. A user with a
return still pending stays on the next account until it is used up, and
the "First again" mark goes away.

Not changed (filing is the reviewer's call): a provider's own accounts
have no back-to-first under auto affinity (backToFirst and
backFromStandIn apply to groups only, inOrder := isGroup && ... in
internal/gateway/gateway.go). Routed In order, a conversation that moved
to the second account stays there after the first renews, for as long as
its cache is warm.

Run on mote (mote-run), at base 5957223d8 plus this change:
- go test ./internal/provider -count=1 -timeout 45m: ok (193.7s)
- GOOS=linux and GOOS=windows go vet ./internal/provider: ok; gofmt -l:
  clean
- node --test --test-concurrency=1 on the GUI tests account-return,
  keep-login, i18n-keys and routing-drain: 64 of 64 pass
Broken by hand: with "&& p.Routing != Drain" removed from
returnsToFirst, TestCodexDrainSignInStaysOnTheNext/drain fails ("first
marked to go back to: true, want false") and /order passes.
Not tried: real accounts; the whole GUI suite; Windows and Linux GUI; an
end-to-end gateway drain run together with a sign-in switch.
origin/main has since moved to d3c544ddf (3 commits); none of them
touch these files.
This commit is contained in:
yetone
2026-11-04 14:28:02 +08:00
parent d3c544ddf4
commit f177c36a9b
6 changed files with 141 additions and 8 deletions
File diff suppressed because one or more lines are too long
+5 -1
View File
@@ -11999,7 +11999,11 @@ function renderRouting(p) {
? t("Routing picks the account for each request through magpie, in the accounts' order; {agent} on its own stays signed in to {user}, whatever it has left.", { agent: a.agentName, user: keptLogin(p) })
: p.keepLogin && !p.switchOnly
? t("Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.", { agent: a.agentName })
: routing === "order" || routing === "drain"
: routing === "drain"
// Until used up keeps the requests on the next, and the sign-in
// with them (provider.returnsToFirst; WalterAIBuilder on X)
? t("Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.", { agent: a.agentName })
: routing === "order"
? t("Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.", { agent: a.agentName })
: a.agent === "codex" && !p.switchOnly || p.keepLogin
? t("Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.", { agent: a.agentName })
+4
View File
@@ -1818,6 +1818,7 @@ const I18N = {
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号用到 98% 时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,首选账号恢复后切回。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号用到 98% 时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,并一直用它,直到它也用到 98%。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号额度用完时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,首选账号恢复后切回。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号额度用完时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,并一直用它,直到它的额度也用完。",
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "经 magpie 的请求由路由挑选账号;{agent} 直连时始终登录首选账号,无论剩余额度多少。",
"Keep {agent} signed in to the first account": "{agent} 始终登录首选账号",
"Keep {agent} signed in to": "{agent} 始终登录",
@@ -6377,6 +6378,7 @@ const I18N = {
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號用到 98% 時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,首選帳號恢復後切回。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號用到 98% 時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,並一直用它,直到它也用到 98%。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號額度用完時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,首選帳號恢復後切回。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號額度用完時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,並一直用它,直到它的額度也用完。",
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時始終登入首選帳號,無論剩餘額度多少。",
"Keep {agent} signed in to the first account": "{agent} 始終登入首選帳號",
"Keep {agent} signed in to": "{agent} 始終登入",
@@ -10912,6 +10914,7 @@ const I18N = {
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントが 98% 使用されると magpie が残量のある次のチェック済みアカウントに切り替え、最初のアカウントに残量が戻ると元に戻します。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントが 98% 使用されると magpie が残量のある次のチェック済みアカウントに切り替え、そのアカウントも 98% 使用されるまでそのまま使います。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントを使い切ると magpie が残量のある次のチェック済みアカウントに切り替え、最初のアカウントに残量が戻ると元に戻します。",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントを使い切ると magpie が残量のある次のチェック済みアカウントに切り替え、そのアカウントも使い切るまでそのまま使います。",
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体では残量にかかわらず最初のアカウントにサインインしたままです。",
"Keep {agent} signed in to the first account": "{agent} を最初のアカウントにサインインしたままにする",
"Keep {agent} signed in to": "{agent} のサインイン先を固定:",
@@ -15478,6 +15481,7 @@ const I18N = {
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle zu 98 % verbraucht ist, und wieder beim ersten, sobald dort wieder Kontingent frei ist.",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle zu 98 % verbraucht ist, und bleibt dort, bis auch dieses zu 98 % verbraucht ist.",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle aufgebraucht ist, und wieder beim ersten, sobald dort wieder Kontingent frei ist.",
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle aufgebraucht ist, und bleibt dort, bis auch dieses aufgebraucht ist.",
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst bleibt beim ersten Konto angemeldet, egal wie viel dort noch übrig ist.",
"Keep {agent} signed in to the first account": "{agent} beim ersten Konto angemeldet lassen",
"Keep {agent} signed in to": "{agent} angemeldet lassen bei",
+18 -2
View File
@@ -38,6 +38,10 @@ const claude = () => ({
],
},
});
// WalterAIBuilder on X: routed Until used up, the gateway keeps the
// requests on the account it went on to, and Codex's or Claude Code's
// sign-in stays there with them (provider.returnsToFirst)
const drains = "Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.";
const stays = "Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.";
function serve(lang, list) {
@@ -60,8 +64,8 @@ function serve(lang, list) {
}
const words = {
en: { first: "First", now: "First for now", again: "First again once it has room", note: /and back to the first once that has room again/, why: /work@example\.com was nearly used up/, stays: /keeps it there till that one is 98% used too/ },
zh: { first: "首选", now: "暂为首选", again: "额度恢复后切回首选", note: /首选账号恢复后切回/, why: /work@example\.com 额度将尽/, stays: /直到它也用到 98%/ },
en: { first: "First", now: "First for now", again: "First again once it has room", note: /and back to the first once that has room again/, why: /work@example\.com was nearly used up/, stays: /keeps it there till that one is 98% used too/, drains: /once it is used up, and keeps it there till that one is used up too/ },
zh: { first: "首选", now: "暂为首选", again: "额度恢复后切回首选", note: /首选账号恢复后切回/, why: /work@example\.com 额度将尽/, stays: /直到它也用到 98%/, drains: /该账号额度用完时.*并一直用它,直到它的额度也用完/ },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
@@ -116,6 +120,18 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium",
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: routed Until used up, the note says the sign-in stays on the next`, async (t) => {
for (const p of [{ ...codex(false), routing: "drain" }, { ...claude(), routing: "drain" }]) {
const { page, errors } = await open(t, false, p);
const note = await page.locator(".editor").textContent();
assert.match(note, w.drains, p.id);
assert.doesNotMatch(note, w.note, p.id);
const missing = await page.evaluate((k) => ["zh", "zh-TW", "ja", "de"].filter((l) => !I18N[l][k]), drains);
assert.deepEqual(missing, [], "every language has the note");
assert.deepEqual(errors, []);
}
});
test(`${engine} ${lang}: with nothing to go back to, the account Codex is on is first`, async (t) => {
const { page, errors } = await open(t, false);
const spare = page.locator(".editor .acc", { hasText: "spare@example.com" });
+9 -4
View File
@@ -23,8 +23,9 @@ package provider
// sign-in follows the gateway it stays that: once it is no longer low
// (backShare) the agent is signed back in to it, as Smart gives it
// requests again then (#408). Where the sign-in alone spends the accounts
// (returnsToFirst), the agent stays on the one it moved to till that is
// spent too. A switch the user makes meanwhile ends that. With KeepLogin on the subscription, the agent
// (returnsToFirst), or the gateway itself stays on the next (Until used
// up), the agent stays on the one it moved to till that is spent too. A
// switch the user makes meanwhile ends that. With KeepLogin on the subscription, the agent
// stays signed in to the first, whatever it has left (#524); with
// KeepLoginAs, to that account, wherever it stands in the order. Not while
// magpie only switches the sign-in (SwitchOnly): moving it is then all
@@ -149,13 +150,17 @@ func loginSwitching(agent string) (share float64, keep bool) {
// alone magpie switches (SwitchOnly) — going back at the first's reset
// left the account moved to part used and started the first's next
// window early, which is not "use one up, then the next" (hxy920830 on
// X): there it stays, unless the user said the first comes first.
// X): there it stays, unless the user said the first comes first. Nor
// does Codex's go back when it is routed Until used up: the gateway then
// keeps its requests on the account it moved to (routes.Drain), so Codex
// signed back in to the first showed that one in use while the other's
// allowance was spent (WalterAIBuilder on X); it stays with the requests.
func returnsToFirst(agent string) bool {
for _, p := range storedFile().Providers {
if p.ID == agent {
// kept on the first (KeepLogin), set aside under SwitchOnly,
// is still the user's word that the first comes first
return p.Routing == Ordered || p.KeepLogin || agent == "codex" && !p.SwitchOnly
return p.Routing == Ordered || p.KeepLogin || agent == "codex" && !p.SwitchOnly && p.Routing != Drain
}
}
return agent == "codex"
+104
View File
@@ -0,0 +1,104 @@
package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
)
// WalterAIBuilder on X: Codex's subscription routed Until used up, two
// accounts. The first used up, magpie signed Codex in to the second, and
// signed it back in to the first once that had room again, while the
// gateway, routed Until used up, kept requests on the second as that
// routing says: Codex and the accounts list showed the first in use while
// the second's week was spent. Routed so, Codex's sign-in stays on the
// account it moved to till that is used up too, as the requests do; In
// order still signs it back in to the first.
func TestCodexDrainSignInStaysOnTheNext(t *testing.T) {
for _, c := range []struct {
routing string
back bool // signed back in to the first once it has room
}{
{Drain, false},
{Ordered, true},
} {
t.Run(c.routing, func(t *testing.T) {
home := signIn(t) // me@example.com, acct-1
rememberLogins(true)
codexSignIn(t, home, "spare@example.com", "r-spare")
rememberLogins(true)
codexSignIn(t, home, "me@example.com", "r-1")
rememberLogins(true)
used := map[string]float64{"acct-me@example.com": 100, "acct-spare@example.com": 20}
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{"plan_type": "plus", "rate_limit": map[string]any{
"primary_window": map[string]any{"used_percent": used[r.Header.Get("chatgpt-account-id")], "limit_window_seconds": 18000}}})
}))
defer fake.Close()
old := CodexBase
CodexBase = fake.URL + "/backend-api/codex"
t.Cleanup(func() { CodexBase = old })
switched := func() string {
t.Helper()
loginUsageCache.Lock()
loginUsageCache.m = nil
loginUsageCache.Unlock()
to, err := SwitchWhenSpent(context.Background(), "codex")
if err != nil {
t.Fatal(err)
}
return to
}
signedIn := func() string {
t.Helper()
var live codexAuth
readJSON(filepath.Join(home, ".codex", "auth.json"), &live)
return live.Tokens.RefreshToken
}
returns := func() bool {
for _, l := range Logins("codex") {
if l.Returns {
return true
}
}
return false
}
if err := SetLoginOn("codex", "spare@example.com", true); err != nil {
t.Fatal(err)
}
if err := SetRouting("codex", c.routing); err != nil {
t.Fatal(err)
}
// the first used up: on to the spare
if to := switched(); to != "spare@example.com" || signedIn() != "r-spare" {
t.Fatalf("switched to %q, Codex signed in with %q", to, signedIn())
}
// the accounts list marks the first as the one gone back to
// only where magpie goes back to it
if returns() != c.back {
t.Fatalf("first marked to go back to: %v, want %v", returns(), c.back)
}
// the first has room again
used["acct-me@example.com"] = 10
to := switched()
if c.back {
if !strings.HasPrefix(to, "me@example.com") || signedIn() != "r-1" {
t.Fatalf("In order: back to %q, Codex signed in with %q; want the first", to, signedIn())
}
return
}
if to != "" || signedIn() != "r-spare" {
t.Fatalf("Until used up: moved off the spare to %q, Codex signed in with %q", to, signedIn())
}
// the spare used up in turn: on to the first
used["acct-spare@example.com"] = 100
if to := switched(); !strings.HasPrefix(to, "me@example.com") || signedIn() != "r-1" {
t.Fatalf("spare used up: on to %q, Codex signed in with %q", to, signedIn())
}
})
}
}