Files
magpie/internal/gui/tests/sessions-subagents.test.cjs
T
yetone 3a0b61275f gui: the Sessions page nests subagents under their session and opens each one's conversation; Codex threads nest under their parent_session (#2261, infinitr0us)
Only main sessions sit at the top of the Sessions page now.

- A session with subagents shows an "N subagents" fold under its row. It starts folded.
  - Unfolding neither opens the session nor moves the page.
  - The rows are indented by the depth of the subagent that started them.
  - Clicking a row opens its conversation in place, via /api/sessions/transcript?sub=.
- Codex threads with a parent_session nest under that session, to any depth.
  - The parent chain is guarded against loops and capped at a depth of 8.
  - A thread whose parent isn't listed stays at the top.
  - A folder's count and its select-all include the nested threads.
  - Searching opens the folds that hold matches. Opening a nested session from elsewhere opens its ancestors' folds.
- New strings ("{n} subagent", "{n} subagents") in zh, en, zh-TW, ja and de.
- No native select and no left-border accent. The nested list is a bordered card.
- docs/subsystems/gateway-sessions.md describes the subagents field, the sub= route and the page.

What was run, at this head before the rebase (5618ade1e):

- internal/gui/tests/sessions-subagents.test.cjs (new): 40/40 pass in Chromium and WebKit, at widths 1100 and 420, in all 5 languages.
  - It checks the fold, the indentation and order, that the transcript is called with sub= and read once, and Codex nesting with a loop and a missing parent.
  - Its tidy check covers: no select, no left border, nothing wider than the page.
- Related GUI tests (sessions*, sess*, session*, i18n, routing-session, account-mask, click-scroll): 451/451 pass.
- Broken by hand, each restored afterwards:
  - Without nest(), the Codex test fails.
  - Without the fold, both tests fail.
  - Without &sub=, the Claude test fails.
- usage-agents-hide and usage-fold fail the same way on origin/main bc470c6b6, with or without this change. They are filed as #2211 and #2163.
- The full suite, go test -p 4 -timeout 40m ./... under a temp HOME at 5618ade1e with this change: green (exit 0).
- gofmt -l is clean. go vet passes for darwin, linux (-tags nogui) and windows.
- Real use: a sandbox magpie (temp HOME, ports 3486/3487) against read-only copies of real sessions.
  - Claude Code: the 5-subagent fold, and a real nested subagent indented one step whose conversation opened.
  - Codex: a real exec rollout pair, with the child's session_meta source rewritten to thread_spawn of the parent. Only the parent is at the top, and the child's details and conversation open in place.
  - Found on the way, and the same on main: a Codex Image Session Helper rollout is listed but can't be opened ("no such session"). Filed as #2273.

Rebased onto 69d7ee33a, whose new commits touch none of these files. Rerun at the rebased head:

- go test ./internal/sessions ./internal/gui: pass.
- sessions-subagents: 40/40.
- i18n-keys, sessions-* and claude-cloud-drift: 371/371.
- gofmt -l is clean.

Not tried:

- A real Codex rollout written as a subagent: none on this Mac, so the copied rollout above stands in.
- Real OpenCode children and real Kimi sessions.
- The GUI on Linux (WebKitGTK) and Windows.
2026-10-15 08:31:21 +08:00

217 lines
13 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// infinitr0us (#2261): a session's subagents sit under it, not beside it.
// A Claude Code session's subagents (s.subagents, as Claude Code, Qoder,
// OpenCode and Kimi Code list them) are folded under its row, "2
// subagents"; unfolded, each is a row of what it was asked and the agent
// it ran as, a subagent another one started set in under it, and a click
// shows what was said in it, read from sessions/transcript with the
// session and the subagent's id (sub=), once. A Codex thread whose
// parent_session is listed is drawn under that session, a session of its
// own still (its details open where it is), and only main sessions are on
// top; a loop of parents, or a parent not listed, leaves a thread on top.
// No click moves the page, nothing has a left-border accent, no <select>,
// nothing wider than the window. In every language, Chromium and WebKit,
// at 1100px and 420px; no backend, the API is faked.
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 at = (min) => new Date(Date.now() - min * 60e3).toISOString();
const sess = (agent, id, title, min, extra) => ({
agent, id, cwd: "/work/app", title, start: at(min + 5), last: at(min), models: [], resume: `${agent} --resume ${id}`,
path: `/x/${id}.jsonl`, size: 4096, messages: 6, files: 1, deletable: true, transcript: true, ...extra,
});
const CLAUDE = [
sess("claude", "c-1", "Refactor the parser", 5, {
files: 3,
subagents: [
{ id: "a1", title: "Inventory the parser's callers", kind: "Explore", start: at(9), last: at(8) },
{ id: "ac1d8e7a80d42178d", title: "Research MCP config", kind: "general-purpose", parent: "a1", start: at(8), last: at(7) },
],
}),
sess("claude", "c-2", "an older one", 50),
];
const CODEX = [
sess("codex", "x-1", "Lead the port", 5),
sess("codex", "x-2", "Port the lexer", 6, { role: "subagent", parent_session: "x-1", nickname: "Lexer" }),
sess("codex", "x-3", "Port the tokens", 7, { role: "subagent", parent_session: "x-2" }),
sess("codex", "x-4", "one of a loop", 20, { parent_session: "x-5" }),
sess("codex", "x-5", "the other of the loop", 21, { parent_session: "x-4" }),
sess("codex", "x-6", "its parent is gone", 30, { role: "subagent", parent_session: "x-gone" }),
];
const PARTS = [
{ role: "user", kind: "text", text: "Research task only" },
{ role: "assistant", kind: "text", text: "Kimi reads ~/.kimi/mcp.json" },
];
function serve(lang, calls) {
const store = { claude: CLAUDE, codex: CODEX };
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:false};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === "/api/sessions/manage") {
const agent = url.searchParams.get("agent") === "codex" ? "codex" : "claude";
return json({
agents: [
{ agent: "claude", count: CLAUDE.length, deletable: true, name: "Claude Code", icon: "claudecode-color" },
{ agent: "codex", count: CODEX.length, deletable: true, name: "Codex", icon: "codex" },
],
agent, sessions: store[agent], terminal: false, trash: [], trashDir: "~/trash",
});
}
if (url.pathname === "/api/sessions/transcript") {
const c = { agent: url.searchParams.get("agent"), id: url.searchParams.get("id") };
if (url.searchParams.has("sub")) c.sub = url.searchParams.get("sub");
calls.push(c);
return json({ parts: PARTS });
}
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
async function open(engine, lang, width, agent, t) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 760 }, reducedMotion: "reduce" })).newPage();
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-${agent}-subagents.png`), fullPage: true });
}
await browser.close();
});
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
page.on("dialog", (d) => { errors.push("a browser dialog: " + d.message()); d.dismiss(); });
const calls = [];
await page.addInitScript((a) => { localStorage.setItem("magpie.sessionsAgent", a); }, agent);
await page.route("**/*", serve(lang, calls));
await page.goto("http://magpie.test/?view=providers");
await page.locator('#nav button[data-view="sessions"]').click();
const view = page.locator("#view-sessions");
const top = (l) => l.evaluate((e) => e.getBoundingClientRect().top);
const still = async (l, what, click) => {
const before = await top(l);
await click();
await page.waitForTimeout(250);
assert.equal(await top(l), before, `${what} moved the page`);
};
const tr = (k, v) => page.evaluate(([k, v]) => t(k, v), [k, v]);
const tidy = async () => {
assert.equal(await page.locator("select").count(), 0, "no native select");
const border = await page.evaluate(() => [...document.querySelectorAll("#view-sessions, #view-sessions *")]
.filter((e) => parseFloat(getComputedStyle(e).borderLeftWidth) > 1 && getComputedStyle(e).borderLeftColor !== getComputedStyle(e).borderRightColor).map((e) => e.className));
assert.deepEqual(border, [], "no left-border accent");
const wide = await page.evaluate(() => {
const box = document.querySelector("#view-sessions").getBoundingClientRect();
return [...document.querySelectorAll("#view-sessions .sm-subs *")].filter((e) => e.getBoundingClientRect().right > box.right + 1).map((e) => e.className);
});
assert.deepEqual(wide, [], "nothing under a session is wider than the page");
assert(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), "the page doesn't scroll sideways");
};
return { page, view, calls, errors, still, tr, tidy };
}
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}: a Claude Code session's subagents are folded under it and open to what was said`, async (t) => {
const { page, view, calls, errors, still, tr, tidy } = await open(engine, lang, width, "claude", t);
if (lang !== "en") {
const missing = await page.evaluate((lang) => ["{n} subagent", "{n} subagents"].filter((k) => !I18N[lang]?.[k]), lang);
assert.deepEqual(missing, [], `every new string has its ${lang}`);
}
const row = (id) => view.locator(`.row.sm-sess[data-id="${id}"]`);
await row("c-1").waitFor();
const item = row("c-1").locator("xpath=..");
const fold = item.locator(":scope > .sm-subs > .sm-subs-fold");
assert.equal(await fold.count(), 1);
assert.equal((await fold.textContent()).trim(), await tr("{n} subagents", { n: 2 }));
assert.equal(await fold.getAttribute("aria-expanded"), "false");
assert.equal(await view.locator(".sm-sub").count(), 0, "folded at first");
assert.equal(await row("c-2").locator("xpath=..").locator(".sm-subs").count(), 0, "a session with none has no fold");
await still(row("c-1"), "unfolding the subagents", () => fold.click());
assert.equal(await fold.getAttribute("aria-expanded"), "true");
assert.equal(await view.locator(".sess-detail").count(), 0, "the fold doesn't open the session");
const subs = item.locator(".sm-sub");
assert.deepEqual(await subs.locator(".sm-sub-row").evaluateAll((rs) => rs.map((r) => r.dataset.sub)), ["a1", "ac1d8e7a80d42178d"], "each under the one that started it");
assert.equal((await subs.nth(0).locator(".name").textContent()).trim(), "Inventory the parser's callers");
assert((await subs.nth(0).locator(".sub").textContent()).startsWith("Explore · "));
const inset = (i) => subs.nth(i).locator(".sm-sub-row .who").evaluate((e) => e.getBoundingClientRect().left);
assert(await inset(1) > await inset(0), "a subagent's own subagent is set in under it");
await tidy();
const a1 = subs.nth(1).locator(".sm-sub-row");
await still(a1, "opening a subagent", () => a1.click());
assert.equal(await a1.getAttribute("aria-expanded"), "true");
const talk = subs.nth(1).locator(".sess-talk");
await talk.locator(".cx-part").first().waitFor();
assert.deepEqual(calls, [{ agent: "claude", id: "c-1", sub: "ac1d8e7a80d42178d" }]);
assert.equal(await talk.locator(".cx-part").count(), PARTS.length);
assert((await talk.textContent()).includes("Kimi reads ~/.kimi/mcp.json"));
await tidy();
await a1.click();
assert.equal(await talk.isVisible(), false);
await a1.click();
await talk.locator(".cx-part").first().waitFor();
assert.equal(calls.length, 1, "read once");
// the session's own conversation is still its own
await row("c-1").locator(".who").click();
await view.locator(".sess-detail .sess-talk-btn").click();
await view.locator(".sess-detail .sess-talk .cx-part").first().waitFor();
assert.deepEqual(calls[1], { agent: "claude", id: "c-1" });
// folded again, it stays folded when the row is drawn again
await item.locator(".sm-subs-fold").first().click();
assert.equal(await view.locator(".sm-sub").count(), 0);
assert.deepEqual(errors, []);
});
test(`${engine} ${lang} ${width}: Codex threads sit under the session that started them`, async (t) => {
const { view, errors, still, tr, tidy } = await open(engine, lang, width, "codex", t);
const row = (id) => view.locator(`.row.sm-sess[data-id="${id}"]`);
await row("x-1").waitFor();
const tops = await view.locator(".sm-group > .sm-item > .row.sm-sess").evaluateAll((rs) => rs.map((r) => r.dataset.id));
assert.deepEqual(tops, ["x-1", "x-4", "x-5", "x-6"], "only main sessions on top; a loop or a parent not listed stays on top");
assert.equal((await view.locator(".sm-folder .note").textContent()).trim(), await tr("{n} sessions", { n: 6 }), "the folder counts the ones under others");
const fold = (id) => row(id).locator("xpath=..").locator(":scope > .sm-subs > .sm-subs-fold");
assert.equal((await fold("x-1").textContent()).trim(), await tr("{n} subagent", { n: 1 }));
for (const id of ["x-4", "x-5", "x-6"]) assert.equal(await fold(id).count(), 0, `${id} has none under it`);
await still(row("x-1"), "unfolding a Codex session", () => fold("x-1").click());
await row("x-2").waitFor();
await still(row("x-1"), "unfolding a thread", () => fold("x-2").click());
await row("x-3").waitFor();
const left = (id) => row(id).locator(".who").evaluate((e) => e.getBoundingClientRect().left);
assert(await left("x-2") > await left("x-1") && await left("x-3") > await left("x-2"), "each set in under its parent");
// a thread is a session of its own: its details open where it is
await still(row("x-3"), "opening a thread", () => row("x-3").locator(".who").click());
const d = row("x-3").locator("xpath=..").locator(":scope > .sess-detail");
await d.waitFor();
assert((await d.textContent()).includes("x-3"));
assert.equal(await row("x-3").isVisible(), true, "still under its parent");
await tidy();
// opening another closes it, and both stay where they are
await row("x-2").locator(".who").click();
await row("x-2").locator("xpath=..").locator(":scope > .sess-detail").waitFor();
assert.equal(await d.count(), 0);
assert.equal(await row("x-3").isVisible(), true);
assert.deepEqual(errors, []);
});
}
}
}