diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 618448996..f5123a37c 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -41,6 +41,19 @@ three. ## Constraints and failure behavior +### First opening of an agent's Details + +In the main window, `expandLink` in +[`app.js`](../../internal/gui/assets/app.js) reads an agent's own settings +and loads its Long conversations controls before drawing and measuring its +opened row. While these are loading, Details shows Loading and is disabled. +A failed read keeps the row closed, reports the error and allows retry. +A later agent click or leaving Agents supersedes the pending opening. +Once prepared, the row slides to its full height; reduced motion opens it +at once. Later openings reuse the settings and controls already loaded. +`agent-details-ready.test.cjs` holds both responses and checks the final +animation frame against the natural height in both engines and five languages. + ### Allowance card refreshes A card refresh applies only the requested account from the API's full diff --git a/internal/gui/assets/agentcompact.js b/internal/gui/assets/agentcompact.js index 9a1ea6ebf..0c6f68a17 100644 --- a/internal/gui/assets/agentcompact.js +++ b/internal/gui/assets/agentcompact.js @@ -46,6 +46,7 @@ window.agentCompactFill = function (list, a) { above ? above.after(row) : list.append(row); // every agent's size, in its label, changes when the row above is drawn const seen = new MutationObserver(() => row.isConnected ? draw() : seen.disconnect()); - const segsBox = document.getElementById("fullContextSegs"); + // Details can build this list before attaching it to the page. + const segsBox = list.querySelector("#fullContextSegs"); if (segsBox) seen.observe(segsBox, { childList: true }); }; diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index c8151b55e..78bab6981 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -1290,13 +1290,10 @@ function fillMenuButton(b, a) { // what turning one on costs, said in its opened row const CONNECT_COST = { gemini: "Gemini CLI's requests all go through magpie; its Google sign-in is turned off first and comes back when you disconnect", - // Desktop's third-party mode runs in a userData of its own (Claude-3p), - // so the signed-in profile's chats and projects aren't shown there; magpie - // leaves them alone (xgddm88 on X: 之前的工程和会话记录全没了) + // its third-party mode has a userData of its own (xgddm88 on X) "claude-desktop": "Chats and projects from your Anthropic sign-in stay in Desktop's own profile, not shown on magpie; nothing is deleted, and turning this off brings them back.", }; -// what an agent not yet connected says turning it on does to what the user -// sees in it, before they turn it on +// what turning it on changes in it, said before const CONNECT_BEFORE = { "claude-desktop": "Not connected · on magpie, Claude Desktop opens a separate profile: chats and projects from your Anthropic sign-in stay, and are back when you turn it off", }; @@ -1305,6 +1302,7 @@ const CONNECT_BEFORE = { const STARTS_WITH = new Set(["codex", "claude"]); let agentExpanded = null; // the connected agent opened in the window +let agentExpandRequest = 0; // a later click supersedes a settings read or closing animation const staleSeen = {}; // agent id → the running copies the reader was told of const previews = {}; // agent id → its disconnect preview, as last read @@ -1340,9 +1338,7 @@ function newModels(a) { } const staleNow = (a) => a.wired && a.stale > 0 && staleSeen[a.id] !== a.stale; -// a running copy left on the old list (agent.StaleCopy's kind): what it -// is, since when, and how it is reopened — the words, and the command or -// key in them as code +// a copy left on the old list (agent.StaleCopy's kind), and its reopen const STALE_HOW = { app: ["The {agent} app, started {when}: closing its window keeps it running. Quit it with {cmd} and open it again.", "⌘Q"], ide: ["{agent} in an editor (VS Code, Cursor…), started {when}: reload that editor's window."], @@ -1352,10 +1348,7 @@ const STALE_HOW = { cli: ["A {agent} in a terminal, started {when}: quit it and start it again."], }; -// the folded row's words for the copies left on the old list (#1374): a -// reopen of one kind doesn't end another, so "reopen Codex" sent the reader -// to the app while the editor's Codex was the one left. One kind is named -// with how it is reopened; more are counted, the row opened says which. +// the folded row's words for one kind of copy left; more are counted (#1374) const STALE_SHORT = { app: "Connected · quit and reopen the {agent} app", ide: "Connected · reload the editor's window", @@ -1429,8 +1422,7 @@ function connectLine(a, kind) { return line; } line.classList.add("on"); - // Codex on magpie while its ChatGPT account is used up: the line says - // until when, the hover (and the row opened) what is off till then + // its ChatGPT account used up: until when, and what is off till then if (a.appOut) { const o = a.appOut; if (o.stuck) say(t("On magpie · quit and reopen the {agent} app to send", { agent: a.name }), "wait"); @@ -1484,14 +1476,27 @@ function expandLink(a) { if (!open) { b.append(el("span", "chev", svg(CHEV_R, 10, 1.6))); } - b.onclick = (e) => { + b.onclick = async (e) => { e.stopPropagation(); + const request = ++agentExpandRequest; + const want = open ? null : a.id; + const own = want && agentPrefsOf(a); + const compact = own && own.includes("fullContextRow") && !window.agentCompactFill; + // sheet measures once: its settings are read first (#5360, chiperman) + if (own && (!prefs || compact)) { + const label = [...b.childNodes]; + b.disabled = true; + saving(b, t("Loading…")); + try { await Promise.all([!prefs && loadSettings(), compact && import("./agentcompact.js")]); } + catch (e) { if (request === agentExpandRequest && b.isConnected) statusErr(e); return; } + finally { b.disabled = false; doneSaving(b); b.replaceChildren(...label); } + if (request !== agentExpandRequest || !b.isConnected || view !== "agents") return; + } const shut = document.querySelector("#agents .row.agent.expanded > .ag-exp"); - agentExpanded = open ? null : a.id; - const want = agentExpanded; + agentExpanded = want; // the one open slides shut first, then the list is drawn again sheet(shut, false, () => { - if (agentExpanded !== want) return; + if (request !== agentExpandRequest || agentExpanded !== want) return; renderAgents(); if (want) sheet(document.querySelector(`#agents .row.agent[data-id="${CSS.escape(want)}"] > .ag-exp`), true); }); @@ -1546,22 +1551,15 @@ function connectRow(a, row, who, { fields, extras, fieldBtn, sw, kind }) { row.classList.toggle("unplugged", kind === "ok" && !a.wired); who.classList.add("with-models"); who.append(connectLine(a, kind)); - // a hidden one's Show stands in the row, centred as its picker and switch - // are, not up on the name's line above its middle (Hu9956, #842: 其余里 - // 的显示歪了偏上了) + // a hidden one's Show centred in the row, as its switch is (#842) const back = who.querySelector(":scope > .ag-show"); if (back) row.append(back); - // the model it starts on, one click away beside the switch (the owner: - // both ways at once): picked here, it is connected first if it wasn't + // the model it starts on, beside the switch; picked, it connects (the owner) const start = kind === "ok" && !listsMenu(a) ? startField(a) : null; if (start) fields.querySelector(`:scope > [data-key="${CSS.escape(start.key)}"]`)?.remove(); if (kind === "ok") { if (a.wired) { - // a click anywhere on the row that isn't one of its controls opens or - // closes it, as a disclosure row does on the Mac (NG_shiyu on X: the - // space between the name and the model did nothing, only the small - // link did); the link stays the control the keyboard and a screen - // reader use, with its aria-expanded + // a click on the row off its controls opens it, as on the Mac (NG_shiyu) const link = expandLink(a); row.append(link); row.onclick = (ev) => { @@ -1592,9 +1590,7 @@ function connectRow(a, row, who, { fields, extras, fieldBtn, sw, kind }) { off.append(el("i")); row.append(add, off); } else if (kind === "link") { - // its link where the others' model stands, and nothing to switch: the - // switch's room kept, so the button lines up with the pickers (the - // owner: the old row's wide button sat apart from every other) + // its link where a model stands, the switch's room kept (the owner) const b = importButton(a); b.classList.add("ag-start"); row.append(b, el("span", "ag-conn-gap")); diff --git a/internal/gui/tests/README.md b/internal/gui/tests/README.md index 7c2d2a047..c61063634 100644 --- a/internal/gui/tests/README.md +++ b/internal/gui/tests/README.md @@ -1,5 +1,13 @@ # Dropdown browser regression +`agent-details-ready.test.cjs` holds the first settings read and the lazy +Long conversations module on the main window's Agents → Details path. It +checks a complete first and warm opening without a final height jump in all +five languages, wide and narrow, Chromium and WebKit. It also covers Claude +Code and Grok Build, failed-read retry, a superseding agent click, navigation +away and reduced motion. `MAGPIE_ASSETS` selects older assets for the failing +regression comparison; `ARTIFACT_DIR` retains Chinese desktop screenshots. + `modal-backdrop.test.cjs` checks the modal's computed backdrop filter using the real stylesheet and platform marker: Windows desktop windows and panels keep the dimming veil without filtering, while browser pages, macOS and Linux diff --git a/internal/gui/tests/agent-details-ready.test.cjs b/internal/gui/tests/agent-details-ready.test.cjs new file mode 100644 index 000000000..881ebf611 --- /dev/null +++ b/internal/gui/tests/agent-details-ready.test.cjs @@ -0,0 +1,210 @@ +// First Details must measure the settings it will show, including the lazy +// Long conversations row. Hold each read rather than racing a wall-clock +// delay; sample the sheet's last animation frame and its natural height. +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 { agentRow } = require("./reader.cjs"); + +const assets = process.env.MAGPIE_ASSETS || path.resolve(__dirname, "../assets"); +const languages = ["en", "zh", "zh-TW", "ja", "de"]; +const row = (id) => `.row.agent[data-id="${id}"]`; +const link = (page, id) => page.locator(row(id) + " .ag-link"); +function held() { + let release; + const promise = new Promise((r) => { release = r; }); + return { promise, release }; +} + +async function openPage(t, engine, lang = "en", width = 1180, reducedMotion = "no-preference") { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + const page = await browser.newPage({ viewport: { width, height: 1000 }, reducedMotion }); + page.setDefaultTimeout(8000); + const errors = [], settingsAsked = held(), settings = held(), compactAsked = held(), compact = held(); + t.after(async () => { settings.release(); compact.release(); await browser.close(); }); + let reads = 0, fail = false; + page.on("pageerror", (e) => errors.push(e.message)); + await page.addInitScript(() => { + const animate = Element.prototype.animate; + window.detailSlides = []; + Element.prototype.animate = function (...args) { + const run = animate.apply(this, args); + if (this.classList.contains("ag-exp")) { + run.pause(); + window.detailSlides.push(run); + } + return run; + }; + }); + await page.route("**/*", async (route) => { + const url = new URL(route.request().url()), json = (data) => route.fulfill({ json: data }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs={lang:${JSON.stringify(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: [agentRow("codex", "Codex"), agentRow("claude", "Claude Code"), agentRow("grok", "Grok Build"), agentRow("goose", "Goose")], profiles: [], settings: { lang, theme: "light" } }); + if (url.pathname === "/api/settings") { + reads++; + settingsAsked.release(); + await settings.promise; + if (fail) return route.fulfill({ status: 500, body: "settings unavailable" }); + return json({ lang, theme: "light", tray: "panel", compactAt: 200000, codexAgentsV1: true, visionModels: [], imageGenModels: [], workbuddyCheckins: [], lanURLs: [], titleModels: [], redactWords: [] }); + } + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname === "/api/usage/quotas") return json([]); + if (url.pathname === "/api/groups") return json({ groups: [], models: [] }); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname.startsWith("/api/")) return json({}); + if (url.pathname === "/agentcompact.js") { compactAsked.release(); await compact.promise; } + 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: "" }); } + }); + await page.goto("http://magpie.test/?view=agents"); + await link(page, "codex").waitFor(); + return { page, errors, settingsAsked, settings, compactAsked, compact, reads: () => reads, fail: (v) => { fail = v; } }; +} + +async function slideEndsWhole(page, id) { + await page.waitForFunction(() => detailSlides.length > 0); + const heights = await page.evaluate(async (selector) => { + const run = detailSlides.at(-1), box = document.querySelector(selector); + await run.ready; + run.currentTime = run.effect.getTiming().duration - 1; + const before = box.getBoundingClientRect().height; + run.finish(); + await run.finished; + await new Promise(requestAnimationFrame); + return [before, box.getBoundingClientRect().height]; + }, row(id)); + assert(Math.abs(heights[1] - heights[0]) < 1, `Details must end without a height jump: ${heights.join(" -> ")}`); + return heights[1]; +} + +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of languages) for (const width of [1180, 560]) { + test(`${engine} ${lang} ${width}: first Details waits for settings and ends at its full height`, async (t) => { + const f = await openPage(t, engine, lang, width), { page } = f; + const shut = await page.locator(row("codex")).boundingBox(); + await link(page, "codex").click(); + await f.settingsAsked.promise; + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0, "no partial Details while settings are unread"); + assert.equal(await link(page, "codex").getAttribute("aria-busy"), "true", "the read has immediate feedback"); + f.settings.release(); + await f.compactAsked.promise; + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0, "no partial Details while its last settings row loads"); + f.compact.release(); + await page.locator(row("codex") + " .ag-prefs .agent-compact").waitFor(); + const full = await slideEndsWhole(page, "codex"); + assert(full > shut.height + 100, "the whole settings list is present"); + assert.equal(await page.locator("#view-agents").evaluate((v) => v.scrollTop), 0, "opening never scrolls the page"); + assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), "no horizontal overflow"); + await link(page, "codex").click(); + await page.evaluate(async () => { const run = detailSlides.at(-1); run.finish(); await run.finished; }); + await page.locator(row("codex") + " .ag-exp").waitFor({ state: "detached" }); + await link(page, "codex").click(); + assert.equal(await slideEndsWhole(page, "codex"), full, "warm Details opens to the same full height"); + assert.equal(f.reads(), 1, "warm opening reuses the settings"); + assert.deepEqual(f.errors, []); + if (process.env.ARTIFACT_DIR && lang === "zh" && width === 1180) { + await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true }); + await page.locator(row("codex") + " .ag-exp").evaluate((b) => b.getAnimations({ subtree: true }).forEach((a) => a.finish())); + await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `details-${engine}.png`) }); + } + }); + } + + for (const id of ["claude", "grok"]) test(`${engine}: ${id} first Details prepares its settings too`, async (t) => { + const f = await openPage(t, engine), { page } = f; + await link(page, id).click(); + await f.settingsAsked.promise; + assert.equal(await page.locator(row(id) + " .ag-exp").count(), 0); + f.settings.release(); f.compact.release(); + await page.locator(row(id) + " .agent-compact").waitFor(); + await slideEndsWhole(page, id); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: the first animation ends at the natural height after both reads answer`, async (t) => { + const f = await openPage(t, engine), { page } = f; + await link(page, "codex").click(); + await f.settingsAsked.promise; + f.settings.release(); f.compact.release(); + await page.locator(row("codex") + " .ag-prefs .agent-compact").waitFor(); + await slideEndsWhole(page, "codex"); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: settings already read still waits for the first controls import`, async (t) => { + const f = await openPage(t, engine), { page } = f; + f.settings.release(); + await page.locator("#prefs").click(); + await page.locator("#view-settings").waitFor(); + await page.waitForFunction(() => document.querySelector("#fullContextSegs .opt")); + await page.locator('#nav button[data-view="agents"]').click(); + await link(page, "codex").click(); + await f.compactAsked.promise; + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0, "a cold controls module still delays measurement"); + f.compact.release(); + await page.locator(row("codex") + " .agent-compact").waitFor(); + await slideEndsWhole(page, "codex"); + assert.equal(f.reads(), 1); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: a failed settings read leaves Details closed and retryable`, async (t) => { + const f = await openPage(t, engine), { page } = f; + f.fail(true); f.compact.release(); + await link(page, "codex").click(); + await f.settingsAsked.promise; + f.settings.release(); + await page.waitForFunction(() => !document.querySelector('.row.agent[data-id="codex"] .ag-link').disabled); + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0, "failure never shows guessed defaults"); + assert.match(await page.locator("#status").textContent(), /settings unavailable/); + f.fail(false); + await link(page, "codex").click(); + await page.locator(row("codex") + " .agent-compact").waitFor(); + await slideEndsWhole(page, "codex"); + assert.equal(f.reads(), 2); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: a late settings read cannot replace the agent clicked next`, async (t) => { + const f = await openPage(t, engine), { page } = f; + await link(page, "codex").click(); + await f.settingsAsked.promise; + await link(page, "goose").click(); + await slideEndsWhole(page, "goose"); + const answered = page.waitForResponse((r) => new URL(r.url()).pathname === "/api/settings"); + f.settings.release(); f.compact.release(); + await answered; + await page.waitForFunction(() => !document.querySelector('.row.agent[data-id="codex"] .ag-link').hasAttribute("aria-busy")); + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0); + assert.equal(await link(page, "goose").getAttribute("aria-expanded"), "true"); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: reduced motion waits for real settings, then opens at once`, async (t) => { + const f = await openPage(t, engine, "en", 1180, "reduce"), { page } = f; + await link(page, "codex").click(); + await f.settingsAsked.promise; + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0); + f.settings.release(); f.compact.release(); + await page.locator(row("codex") + " .agent-compact").waitFor(); + assert.equal(await page.evaluate(() => detailSlides.length), 0, "no sheet animation with reduced motion"); + assert.deepEqual(f.errors, []); + }); + + test(`${engine}: leaving Agents while settings load does not open a hidden row`, async (t) => { + const f = await openPage(t, engine), { page } = f; + await link(page, "codex").click(); + await f.settingsAsked.promise; + await page.locator('#nav button[data-view="providers"]').click(); + await page.locator("#view-providers").waitFor(); + f.settings.release(); f.compact.release(); + await page.waitForFunction(() => !document.querySelector('.row.agent[data-id="codex"] .ag-link').hasAttribute("aria-busy")); + assert.equal(await page.locator(row("codex") + " .ag-exp").count(), 0); + assert.deepEqual(f.errors, []); + }); +} diff --git a/internal/gui/tests/agent-expand-steady.test.cjs b/internal/gui/tests/agent-expand-steady.test.cjs index 393fe63c8..63d0bdef7 100644 --- a/internal/gui/tests/agent-expand-steady.test.cjs +++ b/internal/gui/tests/agent-expand-steady.test.cjs @@ -61,13 +61,14 @@ const where = (page) => page.evaluate((r) => { // the animation on the opened part can const slide = (page, ms) => page.evaluate(([r, ms]) => new Promise((done) => { const hs = [], t0 = performance.now(); - const f = () => { hs.push(Math.round(document.querySelector(r).getBoundingClientRect().height)); if (performance.now() - t0 < ms) requestAnimationFrame(f); else done({ hs, animated }); }; + // the opened part carries a height animation (none comes when motion is + // reduced, and none is wanted there); a first opening reads the settings + // before it slides (#5360), so each frame looks + let animated = false; + const look = () => { const exp = document.querySelector(r + " > .ag-exp"); animated ||= !!exp && exp.getAnimations().some((a) => a.effect.getKeyframes().some((k) => "height" in k)); }; + const f = () => { look(); hs.push(Math.round(document.querySelector(r).getBoundingClientRect().height)); if (performance.now() - t0 < ms) requestAnimationFrame(f); else done({ hs, animated }); }; document.querySelector(r + " .ag-link").click(); - // the click drove the slide before it returned: the opened part carries a - // height animation (none comes when motion is reduced, and none is wanted - // there) - const exp = document.querySelector(r + " > .ag-exp"); - const animated = !!exp && exp.getAnimations().some((a) => a.effect.getKeyframes().some((k) => "height" in k)); + look(); requestAnimationFrame(f); }), [row, ms]);