tjhhhh runs several agent sessions at once. The Routing page's context
window card followed whichever request came last, so it jumped from one
session's prompt to another's. The card still tells of the request it
picked. Under it, while two or more sessions are live, a strip now
shows one small block per session (renderCtxSessions):
- keyed as the card's line of prompts is (sessionKey, else the
conversation);
- named by the agent's title for the session (sessionName), else its
folder's last level, never a bare id;
- holding that session's newest reading through context.js's stack bar,
exported as window.ctxStack.
A session is live while a request of it is under way, or while its
newest request is within 15 minutes of the newest overall. At most six
blocks show, newest session first. A block keeps its place while the
sessions take turns, because blocks are ordered by each session's first
request. The block the card tells of is lit (aria-pressed), and a click
on a block picks that session's newest request. The card's footer names
the session by its title too. New words are in every locale in
plugins/routing/i18n.js. The strip reuses the card's tokens: no select,
no coloured border, no gradient.
The strip sits under the card, not over it. Over it, the strip's first
appearance moved the card, and routing-ctx-still ("the cache kept its
place") went red in both engines. It is green at base ccc791231, and
green again with the strip under.
What was run (mote, through mote-run):
- New routing-ctx-sessions.test.cjs: two engines, en/zh/zh-TW/ja/de at
1100 and 390px, plus one session alone and one with no title. Red on
origin/main, since there is no strip there. Broken by hand: ordering
blocks by newest request instead of first request turns the
"keeps its place" check red. Green, 4 of 4 tests, at the final tree.
- routing-ctx-still: red with the strip over the card, green with it
under (tests 391/392, both engines).
- Whole GUI suite, at the first version (strip over the card), under
load 25 to 30 from other sessions: 16892 tests, 16595 pass, 297 fail
in 49 files. That version's own red was routing-ctx-still (fixed
above). Every other red file has an issue:
- Filed before: agent-panel-name-width #4203, context-map #3488,
gui-ja #4580, i18n-keys "Unmarked" #4524, header-centre #3479,
key-replace #4126, menu-input-scroll #3402, mobile-web #3952,
model-chip-steady #4388, models-works-only #4391, page-width-steady
#4541, phone-connect-menu #3524, plugin-card-update #3079,
plugin-move-unserved #4392, provider-fetch-again #3292,
provider-levels-scroll #4394, provider-models-rest #4396,
provider-picks-stay #4397, provider-typed #4461, recovered-file
#4320, remote-magpie #4398, rest-try-now #2564, routing-row-swap
#3799, routing-steady #3412, the routing rule-drag, rule-remove,
rules-line and upstream files #4585 and #4494, s3-sync #3413,
searcher-pick #4274, segs-press-redraw #4189 and #4566,
usage-agents-hide #3414, warm-now #4322.
- Filed with this commit: editor-advanced-align #4615,
gateway-key-models #4616, group-found-skip #4617, images-codex-one
#4618, import-row #4619, market-have #4620, model-copy-id #4621,
nav-timeout #4622, number-units #4623, omarchy #4624, tencent-cloud
#4625, usage-ledger #4626, routing-order-unknown #4627,
routing-steps-cap #4628, routing-story-shrinks #4629.
- The files this change can reach (routing-*, context-*, i18n-keys,
gui-ja), alone at concurrency 2 on the first version against base
ccc791231: 54 fail on the head and 34 at the base. The only red that
was on the head and not the base was routing-ctx-still. The rest were
routing reds under load that show on both.
- The same files rerun with the strip under the card: 2331 tests,
35 fail. routing-ctx-sessions and routing-ctx-still are green. Each
red has an issue: context-map webkit 420px "two tiles overlap"
(#3488, the same at base), gui-ja (#4580), i18n-keys "Unmarked"
(#4524), routing-many-keys (#4082), routing-row-swap (#3799),
routing-steady (#3412), routing-order-unknown (#4627),
routing-steps-cap (#4628), routing-story-shrinks (#4629), and
rule-drag, rule-remove, rules-line and upstream (#4585).
- TestCoreScriptBudget went red after the rebase: the core was over by
266 bytes, which is the new stackBar helper in context.js. The budget
was not raised. stackBar was written tighter and the comments around
it cut, and context.js is now 1 byte under its size at origin/main.
- Rebased on origin/main 2e9df9b1b, on mote:
- `go test ./internal/gui`: TestCoreScriptBudget passes. Two reds are
already filed: TestSettingsSaveKeepsWhatItDoesNotSend (#4514, red
on main) and TestMainWindowCorners (#3949, macOS 27).
- routing-ctx-*, context-*, i18n-keys and gui-ja: 337 tests, 8 fail.
routing-ctx-sessions is 4/4, routing-ctx-still 2/2, and every
context-* other than context-map is green. The reds are context-map
"two tiles overlap" (webkit 420px in every language and chromium
zh 420px, #3488), gui-ja (#4580) and i18n-keys "Unmarked" (#4524).
- Rebased again on origin/main e8347401f, which changes the core
budget and fixes #4524. This is the pushed tree. TestCoreScriptBudget
is ok, and routing-ctx-sessions, routing-ctx-still and i18n-keys pass
44/44.
Not tried: a real gateway with several live agent sessions sending at
once (only the page's fixture feed was used), the Linux WebKitGTK app,
and the whole GUI suite at the final tree (only the files above,
which the change can reach).
177 lines
11 KiB
JavaScript
177 lines
11 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// tjhhhh on Discord: with several sessions running through magpie at once,
|
|
// the Routing page's context window went from one session's context to the
|
|
// other's, and which was which couldn't be told. Each session sending now
|
|
// has a small block of its own under the card, named as its agent named it
|
|
// (not its id), with its own newest reading; the newest session comes
|
|
// first and the blocks keep their places while the sessions take turns.
|
|
// Two Codex conversations' requests interleave here; every frame's blocks
|
|
// are watched. A block clicked tells of that session's newest request. One
|
|
// session alone shows none. Every language, both engines, a phone's width.
|
|
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 iso = (ms) => new Date(ms).toISOString();
|
|
const t0 = Date.now();
|
|
const seat = { id: "codex@me", provider: "codex", name: "ChatGPT", who: "me@example.com", kind: "account", model: "gpt-6.1-sol", known: true, plan: "Pro" };
|
|
const SESS = {
|
|
a: { id: "019a7c2e-aaaa-7000-8000-000000000001", title: "Fix the login redirect", folder: "/Users/me/work/web", base: 40000, window: 272000 },
|
|
b: { id: "019a7c2e-bbbb-7000-8000-000000000002", title: "Write the release notes", folder: "/Users/me/work/docs", base: 150000, window: 272000 },
|
|
};
|
|
// a request of session s, its prompt growing by n
|
|
const prompt = (s, n) => ({
|
|
window: SESS[s].window, tokens: SESS[s].base + n * 1000, counted: true, turns: n,
|
|
parts: [
|
|
{ kind: "system", tokens: 9000, items: [{ name: "prompt", tokens: 9000 }] },
|
|
{ kind: "chat", tokens: SESS[s].base - 9000 + n * 1000, items: [{ name: "turn", tag: "turns", n, tokens: SESS[s].base - 9000 + n * 1000 }] },
|
|
],
|
|
});
|
|
let seq = 10;
|
|
function req(id, s, done = true, titled = true) {
|
|
const at = t0 - (200 - id) * 1000;
|
|
return {
|
|
id, seq: ++seq, time: iso(at), agent: "codex", session: SESS[s].id, folder: SESS[s].folder, model: "gpt-6.1-sol", provider: "codex",
|
|
...(titled ? { sessionTitle: SESS[s].title } : {}),
|
|
order: [seat], tries: [{ id: seat.id, model: seat.model, start: iso(at), done, ...(done ? { status: 200, ms: 3000 } : {}) }],
|
|
done, ...(done ? { status: 200, ms: 3000, tokens: 900 } : {}), prompt: prompt(s, id - 100),
|
|
};
|
|
}
|
|
|
|
function serve(lang, first, feed) {
|
|
const state = { agents: [{ id: "codex", name: "Codex", path: "/test/codex", fields: [] }], profiles: [], settings: { lang, theme: "light" } };
|
|
return async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const json = (data) => route.fulfill({ json: data });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json(state);
|
|
if (url.pathname === "/api/gateway/trace") {
|
|
const trace = (routes) => json({ mine: true, now: new Date().toISOString(), seq: routes.length ? routes[routes.length - 1].seq : Number(url.searchParams.get("after")), totals: { requests: 0, rerouted: 0, errors: 0 }, routes });
|
|
if (!url.searchParams.get("wait")) return trace(first);
|
|
const rs = await new Promise((res) => { feed.next = res; });
|
|
return trace(rs);
|
|
}
|
|
if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
|
|
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 route.fulfill({ body: await fs.readFile(file).catch(() => ""), contentType });
|
|
};
|
|
}
|
|
|
|
async function open(browser, lang, width, first) {
|
|
const context = await browser.newContext({ viewport: { width, height: 900 } });
|
|
const page = await context.newPage();
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const feed = { next: null };
|
|
await page.route("**/*", serve(lang, first, feed));
|
|
await page.goto("http://magpie.test/?view=routing");
|
|
await page.locator(".rt-req").first().waitFor();
|
|
await page.locator(".rt-ctx .ctx-card").waitFor();
|
|
const send = async (...rs) => {
|
|
for (let i = 0; i < 100 && !feed.next; i++) await page.waitForTimeout(50);
|
|
const next = feed.next;
|
|
assert(next, "the page asks for the next trace");
|
|
feed.next = null;
|
|
next(rs);
|
|
await page.waitForTimeout(300);
|
|
};
|
|
return { context, page, errors, send };
|
|
}
|
|
|
|
const blocks = (page) => page.$$eval(".rt-ctx-sess:not([hidden]) .rt-ctx-s", (bs) => bs.map((b) => ({
|
|
name: b.querySelector(".nm").textContent, sub: b.querySelector(".sub").textContent, on: b.getAttribute("aria-pressed") === "true",
|
|
})));
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(`${engine}: each session sending at once has its own context window, by its name`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
for (const width of [1100, 390]) {
|
|
await t.test(`${lang} ${width}px`, async () => {
|
|
// session a began first, then b: b is the newer session
|
|
const { context, page, errors, send } = await open(browser, lang, width, [req(101, "a"), req(102, "b")]);
|
|
t.after(() => context.close());
|
|
await page.locator(".rt-ctx-sess:not([hidden]) .rt-ctx-s").nth(1).waitFor();
|
|
const label = await page.evaluate(([l]) => (l === "en" ? "Each session's context window" : I18N[l]?.["Each session's context window"]), [lang]);
|
|
assert.ok(label, `the strip's label has its ${lang}`);
|
|
assert.equal(await page.locator(".rt-ctx-sess").getAttribute("aria-label"), label);
|
|
|
|
// watch every frame's blocks from here on
|
|
await page.evaluate(() => {
|
|
window.__frames = [];
|
|
window.__on = true;
|
|
const tick = () => {
|
|
window.__frames.push([...document.querySelectorAll(".rt-ctx-sess:not([hidden]) .rt-ctx-s")].map((b) => [b.querySelector(".nm").textContent, b.querySelector(".sub").textContent]));
|
|
if (window.__on) requestAnimationFrame(tick);
|
|
};
|
|
requestAnimationFrame(tick);
|
|
});
|
|
// the sessions take turns, a's and b's requests interleaved
|
|
await send(req(103, "a", false));
|
|
await send(req(103, "a"), req(104, "b", false));
|
|
await send(req(104, "b"), req(105, "a"));
|
|
await send(req(106, "b"));
|
|
const frames = await page.evaluate(() => { window.__on = false; return window.__frames; });
|
|
assert(frames.length > 10, `frames were watched: ${frames.length}`);
|
|
const k = (n) => Math.round(n / 1000) + "K";
|
|
for (const f of frames) {
|
|
// the newest session first, each in its place, by its title
|
|
assert.deepEqual(f.map((x) => x[0]), [SESS.b.title, SESS.a.title], `the blocks kept their places: ${JSON.stringify(f)}`);
|
|
// each block says its own session's reading, never the other's
|
|
assert.match(f[0][1], new RegExp(`^Codex · (${k(SESS.b.base + 2000)}|${k(SESS.b.base + 4000)}|${k(SESS.b.base + 6000)}) / 272K`), JSON.stringify(f));
|
|
assert.match(f[1][1], new RegExp(`^Codex · (${k(SESS.a.base + 1000)}|${k(SESS.a.base + 3000)}|${k(SESS.a.base + 5000)}) / 272K`), JSON.stringify(f));
|
|
}
|
|
let bs = await blocks(page);
|
|
assert.deepEqual(bs.map((b) => b.sub), [`Codex · ${k(SESS.b.base + 6000)} / 272K · ${Math.round((SESS.b.base + 6000) / 2720)}%`, `Codex · ${k(SESS.a.base + 5000)} / 272K · ${Math.round((SESS.a.base + 5000) / 2720)}%`]);
|
|
// the card tells of b's 106; its block is the one lit, and the
|
|
// card names the session by its title, not its id
|
|
assert.deepEqual(bs.map((b) => b.on), [true, false]);
|
|
const crumbs = await page.locator(".rt-ctx .ctx-crumbs").textContent();
|
|
assert.ok(crumbs.includes(SESS.b.title.slice(0, 20)), crumbs);
|
|
assert.ok(!crumbs.includes(SESS.b.id.slice(0, 8)), crumbs);
|
|
|
|
// a's block clicked: the card tells of a's newest, 105
|
|
await page.locator(".rt-ctx-s").nth(1).click();
|
|
await page.waitForFunction(() => document.querySelectorAll(".rt-ctx-s")[1]?.getAttribute("aria-pressed") === "true");
|
|
assert.ok((await page.locator(".rt-ctx .ctx-crumbs").textContent()).includes("#105"));
|
|
assert.ok((await page.locator(".rt-ctx .ctx-crumbs").textContent()).includes(SESS.a.title.slice(0, 20)));
|
|
bs = await blocks(page);
|
|
assert.deepEqual(bs.map((b) => b.name), [SESS.b.title, SESS.a.title], "a click doesn't move the blocks");
|
|
|
|
// at a phone's width the blocks fit the page: nothing runs off it
|
|
const over = await page.evaluate(() => {
|
|
const s = document.querySelector(".rt-ctx-sess").getBoundingClientRect();
|
|
return [...document.querySelectorAll(".rt-ctx-s")].some((b) => b.getBoundingClientRect().right > s.right + 0.5) || document.documentElement.scrollWidth > innerWidth;
|
|
});
|
|
assert.equal(over, false, "the blocks stay inside the page");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
test(`${engine}: one session alone shows no blocks, and one with no title is named by its folder`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
const { context, page, errors, send } = await open(browser, "en", 1100, [req(101, "a"), req(102, "a")]);
|
|
t.after(() => context.close());
|
|
await page.waitForTimeout(300);
|
|
assert.equal(await page.locator(".rt-ctx-sess").isHidden(), true, "one session: the card is its own");
|
|
// b comes in without a title yet: named by its folder, never its id
|
|
await send(req(103, "b", true, false));
|
|
await page.locator(".rt-ctx-sess:not([hidden]) .rt-ctx-s").nth(1).waitFor();
|
|
assert.deepEqual((await blocks(page)).map((b) => b.name), ["docs", SESS.a.title]);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|