bitssly on X (10-24) asked why, with a group set 按顺序 (In order), an
account whose quota isn't known "can't be used", quoting the Routing
page: "opencode:剩余额度未知,排在已知的之后。" and "腾讯-国际:剩余额度未知,
排在已知的之后。"
Cause: the gateway does what the mode says. planGroup/planLevel try an
In order group's members in the order given and never by what each has
left; each member's own accounts go as that member's provider routes
them (Smart by default, where an unknown allowance still comes before
any account at 90% or more and is tried when the known ones fail). The
explanation was wrong. weighed() gives an ordered group's rows their
member's routing (""), so the page's smart() judged each row against
every known row of the whole request: an In order group's later
members read as ranked behind a known allowance, and the first got a
Smart reason ("its allowance renews soonest ...") instead of In order.
A By weight group's first had the same fault.
Change:
- client.js: placeRouting/wholeMembers/weighedWith scope what a row is
weighed against to its own member when the level that placed it takes
members whole (order, weight). firstWhy says "In order: {who} is
first ..." for such a group, and "By weight: it's {who}'s share ..."
for a weighted one; the unknown, learns, spent and low asides are said
only of an account weighed against another of its own member. Rebased
onto 6d707af08 (Creat.wu, memberSplit): its member line now asks the
scoped someKnown(x), so an unknown account alone in its member gets no
line at all, and one beside a known account of its own member gets
6d707af08's one line for the member.
- i18n.js: the By weight line in zh, zh-TW, ja, de.
- routing-order-unknown.test.cjs: In order, Smart and By weight groups
over Codex (known), OpenCode Zen Free "opencode" (unknown) and two
WorkBuddy AI accounts (one known, "腾讯-国际" unknown); chromium and
webkit, en/zh/zh-TW/ja/de, 1100 and 420 wide.
- docs/reference.md: Smart's unknown account is still tried when the
known ones fail; an In order group keeps its order whatever is known.
Why the quotas are unknown: OpenCode Zen Free is a free tier with no
allowance to report. Tencent Cloud Token Plan and Volcengine access-key
plans are read for the Usage page but not for routing; that class is
#3251. No plugin change.
Run:
- old client.js with the new test: red on the why line (chromium en/zh
1100: "c@x.test goes first: of those with quota to spare, its allowance
renews soonest ..." where "In order: c@x.test is first ..." was due).
- at the rebased head (origin/main 1f8f80e4a plus this):
routing-order-unknown, routing-weight-member-low and i18n-* 64/64.
- go test -timeout 45m ./internal/gui/... at the rebased head: ok but
TestMainWindowCorners (killed measuring windows at load ~80; red alone
too; #1936, #2645, AppKit, not this change). Before the rebase: ok
(112s).
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./...: clean. No Go file changed, so gofmt has nothing to check.
- whole GUI suite at ab287a839 (before the rebase), concurrency 4, load
50 to 80: files a* to roughly o* ran; reds, all outside routing and
filed: api-key-theme chromium system (#3307, also 1 of 3 alone),
fonts webkit narrow (#3227), library-mcp-health-wsl ja (#3350),
menu-input-scroll chromium en/zh (#3402). The rest of that run is void:
my own rebase put conflict markers in client.js mid-run. The files from
n* on are run again at the pushed head after this push.
Broken by hand: someKnown made global again (the old aside scoping):
red, the opencode aside is back under the In order group.
Not tried: the reporter's own trace and accounts (not shared); a Go
test for the gateway's order (unchanged here, covered by the existing
fallback tests); routing reading the access-key plans (#3251).
124 lines
8.5 KiB
JavaScript
124 lines
8.5 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// A routing group set In order tries its members in the order the user
|
|
// gave, each over its own accounts as its provider routes them
|
|
// (planLevel): an account whose allowance isn't known doesn't go after
|
|
// another member's known one. The Routing page said it did (bitssly on
|
|
// X: "opencode:剩余额度未知,排在已知的之后。" under an In order group),
|
|
// as if magpie had moved the member the user put there. Now an In order
|
|
// group's first says In order, and the unknown-allowance line is said
|
|
// only of an account weighed against a known one of its own member, as
|
|
// a Smart group still says it of each; a group By weight says the
|
|
// round picked its first. English, Chinese (both),
|
|
// Japanese and German; 1100 and 420 wide; no backend, the API is faked
|
|
// here.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const now = new Date();
|
|
const ago = (s) => new Date(now.getTime() - s * 1e3).toISOString();
|
|
const ahead = (h) => new Date(Date.now() + h * 3600e3).toISOString();
|
|
const acct = (p, name, user, model, extra = {}) => ({ id: `${p}@${user}`, provider: p, name, who: user, kind: "account", agent: p, model, routing: "", ...extra });
|
|
|
|
const codex = acct("codex", "Codex", "c@x.test", "gpt-5.5", { plan: "Plus", known: true, used: 30, renews: [ahead(50)] });
|
|
const zen = acct("opencode-zen-free", "OpenCode Zen Free", "opencode", "big-pickle");
|
|
const wbKnown = acct("workbuddy-ai", "WorkBuddy AI", "w2@x.test", "glm-5", { known: true, used: 20, renews: [ahead(100)] });
|
|
const wbUnknown = acct("workbuddy-ai", "WorkBuddy AI", "腾讯-国际", "glm-5");
|
|
|
|
const members = ["codex/gpt-5.5", "opencode-zen-free/big-pickle", "workbuddy-ai/glm-5"];
|
|
const routeOf = (id, group, order) => ({
|
|
id, seq: id, time: ago(60 - id), agent: "codex", model: group.id, provider: "codex", done: true, status: 200, ms: 900, tokens: 1200, group, order,
|
|
tries: [{ id: order[0].id, model: order[0].model, start: ago(60 - id), done: true, status: 200, ms: 900 }],
|
|
});
|
|
// In order: Codex first as the user put it; WorkBuddy AI's two accounts,
|
|
// one member, Smart between themselves: the known one first
|
|
const ordered = routeOf(1, { id: "coding", name: "coding", routing: "order", members }, [codex, zen, wbKnown, wbUnknown]);
|
|
// Smart: every member's accounts weighed as one, the unknown after
|
|
const smart = routeOf(2, { id: "mix", name: "mix", routing: "", members }, [codex, wbKnown, zen, wbUnknown]);
|
|
// By weight: the round's pick first, each member whole after it
|
|
const weighted = routeOf(3, { id: "spread", name: "spread", routing: "weight", members }, [zen, codex, wbKnown, wbUnknown]);
|
|
|
|
function serve(lang) {
|
|
return async (request) => {
|
|
const url = new URL(request.request().url());
|
|
const json = (data) => request.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return request.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return request.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } });
|
|
if (url.pathname === "/api/gateway/trace") {
|
|
if (url.searchParams.get("wait")) { await new Promise((r) => setTimeout(r, 20e3)); return json({}); }
|
|
return json({ mine: true, now: now.toISOString(), seq: 3, totals: { requests: 3, rerouted: 0, errors: 0 }, routes: [ordered, smart, weighted] });
|
|
}
|
|
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
|
|
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], excluded: [], gateway: { running: true, window: true } });
|
|
if (url.pathname.startsWith("/api/")) return json({});
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
await request.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
for (const width of [1100, 420]) {
|
|
test(`${engine} ${lang} ${width}: an In order group's later members aren't said to go after a known allowance`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const page = await browser.newPage({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
|
|
page.setDefaultTimeout(5000);
|
|
const errors = [];
|
|
page.on("pageerror", (error) => errors.push(error.message));
|
|
await page.route("**/*", serve(lang));
|
|
await page.goto("http://magpie.test/?view=routing");
|
|
const say = (key, vars = {}) => page.evaluate(([lang, key, vars]) => {
|
|
const s = (lang !== "en" && I18N[lang]?.[key]) || key;
|
|
return s.replace(/\{(\w+)\}/g, (_, k) => vars[k] ?? `{${k}}`);
|
|
}, [lang, key, vars]);
|
|
const unknown = (who) => say("{who}: what it has left isn't known yet, so it goes after those known.", { who });
|
|
// the story of the request with the group named
|
|
const story = async (name) => {
|
|
const row = page.locator(".rt-req", { hasText: name }).first();
|
|
await row.waitFor();
|
|
await row.dispatchEvent("click"); // the list may run past a narrow window
|
|
await page.locator(".rt-steps li").first().waitFor();
|
|
await page.waitForFunction((name) => document.querySelector(".rt-steps")?.textContent.includes(name), name);
|
|
return page.locator(".rt-steps li").evaluateAll((ls) => ls.map((l) => [l.className, l.textContent.trim()]));
|
|
};
|
|
|
|
let got = await story("coding");
|
|
const why = got.filter(([c]) => c === "why").map(([, s]) => s);
|
|
assert.deepEqual(why, [await say("In order: {who} is first, and answers everything while it can.", { who: "c@x.test" })], JSON.stringify(got));
|
|
const asides = got.filter(([c]) => c === "aside").map(([, s]) => s);
|
|
// OpenCode Zen Free is its own member, after Codex by the order given
|
|
assert(!asides.includes(await unknown("opencode")), JSON.stringify(asides));
|
|
// WorkBuddy AI's unknown account, weighed against its own known one,
|
|
// is told with its member: after Codex by the order given
|
|
assert(asides.includes(await say("{member} isn't passed over for being near its limit: it comes after {first} in {group}'s order.", { member: "WorkBuddy AI", first: "Codex", group: "coding" })), JSON.stringify(asides));
|
|
assert(!asides.some((s) => s.startsWith("OpenCode Zen Free")), JSON.stringify(asides));
|
|
|
|
got = await story("mix");
|
|
const smartAsides = got.filter(([c]) => c === "aside").map(([, s]) => s);
|
|
assert(smartAsides.includes(await unknown("opencode")), JSON.stringify(smartAsides));
|
|
assert(smartAsides.includes(await unknown("腾讯-国际")), JSON.stringify(smartAsides));
|
|
|
|
// by weight: the first is the round's pick, not one weighed by
|
|
// its allowance against Codex's
|
|
got = await story("spread");
|
|
assert.deepEqual(got.filter(([c]) => c === "why").map(([, s]) => s), [await say("By weight: it's {who}'s share — each model takes requests as the weight set beside it says.", { who: "opencode" })], JSON.stringify(got));
|
|
const wAsides = got.filter(([c]) => c === "aside").map(([, s]) => s);
|
|
assert(!wAsides.some((s) => s.startsWith("c@x.test")), JSON.stringify(wAsides));
|
|
assert(!wAsides.includes(await unknown("opencode")), JSON.stringify(wAsides));
|
|
assert(wAsides.includes(await unknown("腾讯-国际")), JSON.stringify(wAsides));
|
|
|
|
const spills = await page.locator(".rt-steps li").evaluateAll((els) => els.filter((e) => e.getBoundingClientRect().right > document.documentElement.clientWidth + 0.5).map((e) => e.textContent.slice(0, 40)));
|
|
assert.deepEqual(spills, []);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|
|
}
|