Files
magpie/internal/gui/tests/agent-stale-copies.test.cjs
T
yetone e611fc65d6 codex: magpie restarts the codex app-server a change left on the old model list when no codex session is on it, and the Agents page offers Restart when one is (luci on Discord)
luci (Codex CLI 0.162, magpie 0.1.1150): after Codex was wired back to
magpie, codex in a new terminal listed Codex's own models although
config.toml was magpie's.

Cause, reproduced with real codex 0.162.0 in a sandbox CODEX_HOME:
codex sessions attach to a background app-server (the managed daemon)
that is per CODEX_HOME, not per terminal (socket under
$CODEX_HOME/app-server-control). It is detached, has no idle exit and
builds its model list once, at start. A daemon started while Codex was
off magpie keeps the official list for every later session, in this
terminal or a new one, until it restarts. magpie writes only PATH to
shell rc files and doesn't unwire Codex on quit, so nothing
per-terminal is involved. luci's "a new terminal fixed it" isn't
explained by the daemon. Codex's hourly updater restarting it is a
guess, not proven.

What changed:
- provider.KeepCodexDaemonCurrent(ctx, home, changed) compares the
  daemon's start time with when magpie last changed what Codex reads at
  start. The start time is the mtime of a pid file in that home that
  names the running daemon's pid, so another home's daemon (ChatGPT.app,
  other sandboxes) is never acted on.
  - With no codex session attached, it runs
    `codex app-server daemon restart` with CODEX_HOME set to that home.
  - Otherwise it reports Behind and how many sessions are attached.
  - isCodexClient leaves out apps' and editors' own codex, exec-server,
    mcp-server and the daemon's own processes. It reads the whole ps line,
    since ps leaves a path's spaces unquoted ("Codex Computer
    Use.app/...").
- agent: Codex's Notice after a change runs this check first. It says
  either that magpie restarted the daemon, or that the daemon is behind
  and how to restart it (Agents page or the command).
- The GUI backend and `magpie serve` re-check every 15s. A session that
  was open at the change is usually closed later, and that is when the
  daemon can be restarted.
- Agents page: the stale "daemon" copy line has a Restart button that
  POSTs /api/codex/daemon/restart (timeout now 2 min, as a running turn
  is let finish). It reuses translated strings. On Windows, where
  processes aren't listed on each look, the row shows the last check.
- Restarts are serialized by one mutex.
- WSL's Codex is not handled automatically. Its row keeps the advice
  text, which names the restart command.

What was run:
- Real codex 0.162.0 in a sandbox CODEX_HOME, with a fake gateway, on
  luci's sequence: the daemon started on the official config, then
  magpie's config and catalog were written.
  - With a TUI open, /model listed GPT-6.x. The check said Behind with
    a session attached and did not restart.
  - With the TUI quit, the check restarted the daemon. A new codex in
    the same terminal listed Magpie Alpha and Beta.
- Earlier in the sandbox: a daemon restart under an idle attached 0.162
  TUI reconnects, and /model then shows magpie's models. A running turn
  is waited for up to 60s.
- New tests:
  - TestKeepCodexDaemonCurrent(Fails), TestIsCodexClient (with the
    space-in-path case from the real process list) and
    TestCodexNoticeKeepsTheDaemonCurrent. They don't build or fail on
    the old code. The space case failed before the ps-line fix.
  - The GUI test "the daemon's line restarts it" times out on the old
    app.js.
- Results:
  - agent-stale-copies.test.cjs: 60/60 in Chromium and WebKit, in en,
    zh, zh-TW, ja and de, at widths 980 and 440.
  - go test (temp HOME) for ./internal/agent, ./internal/provider,
    ./internal/gui and the root package: ok.
  - gofmt -l: clean.
  - GOOS=linux and GOOS=windows go vet -tags nogui: ok.
- The Windows path is checked only by vet and not tried on a Windows
  box.
2026-10-10 10:05:10 +08:00

195 lines
12 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// miaopasi on Discord: the Codex row stayed "已接入 · 重开 Codex 后生效"
// after Codex was restarted. On macOS closing the Codex app's window keeps
// the app running, and an editor's Codex or the CLI's daemon runs on
// through a reopened app. Opened, the row says which copy is left on the
// old list, since when, and how that one is reopened: ⌘Q for the app, the
// daemon's restart command, the app whose own Codex it is. Every language, wide and narrow; "Got it"
// takes it away. 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 days = (n) => new Date(Date.now() - n * 86400e3).toISOString();
const codex = {
id: "codex", name: "Codex", icon: "generic", path: "/fixture/codex", wired: true,
fields: [{ key: "model", label: "model", value: "gpt-6.1-sol", options: [{ value: "gpt-6.1-sol", label: "GPT-6.1 Sol", ref: "group/auto-gpt-6-1-sol", note: "Group · via magpie" }] }],
stale: 3, staleCopies: [{ kind: "app", since: days(3) }, { kind: "daemon", since: days(1) }, { kind: "embedded", app: "Agents Anywhere", since: days(1) }],
};
function serve(lang, agent = 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: [agent], profiles: [], settings: { lang, theme: "light" } });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/agents/cli") return json({ agents: {}, pending: false });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], 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 route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const row = '.row.agent[data-id="codex"]';
// as each engine's Intl says "3 days ago"
const ago3 = { en: /3 days ago/, zh: /3 ?天前/, "zh-TW": /3 ?天前/, ja: /3 ?日前/, de: /vor 3 Tagen/ };
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 [980, 440]) {
test(`${engine} ${lang} ${width}px: the opened row says which Codex is left and how to reopen it`, 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: 800 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang));
await page.goto("http://magpie.test/?view=agents");
await page.locator(`${row} .ag-conn`).waitFor();
await page.locator(`${row} .ag-link`).click();
const copies = page.locator(`${row} .ag-stale-copy`);
await copies.first().waitFor();
assert.equal(await copies.count(), 3);
const [app, daemon, embedded] = await copies.allTextContents();
// miaopasi: Agents Anywhere's own Codex, named by its app
assert.equal(embedded.split("Agents Anywhere").length - 1, 2, embedded);
assert.equal(await copies.nth(0).locator("code").textContent(), "⌘Q");
assert.equal(await copies.nth(1).locator("code").textContent(), "codex app-server daemon restart");
assert.match(app, ago3[lang], "the app's start");
for (const s of [app, daemon, embedded]) {
assert.ok(s.includes("Codex"), s);
assert.doesNotMatch(s, /\{\w+\}/, "a placeholder left: " + s);
}
if (lang !== "en") assert.doesNotMatch(app, /closing its window/, "not translated: " + app);
// nothing runs past the row
const over = await page.evaluate((r) => [...document.querySelectorAll(r + " .ag-stale-copy, " + r + " .ag-stale-copy *")].filter((e) => e.scrollWidth > e.clientWidth + 1 || e.getBoundingClientRect().right > document.documentElement.clientWidth).map((e) => e.className || e.tagName), row);
assert.deepEqual(over, []);
// told: the lines go with the warning
await page.locator(`${row} .ag-warn button`).click();
await page.waitForFunction((r) => !document.querySelector(r + " .ag-stale-copy"), row);
assert.deepEqual(errors, []);
});
}
}
}
// #1374 (SivanCola): folded, the row said only "reopen Codex", so the app
// was reopened while VS Code's Codex was the copy left. Folded, it names
// the one kind left and how it is reopened, counts several, and its hover
// has each copy whole. The reporter's case first: one editor's Codex.
const folded = {
ide: { copies: [{ kind: "ide", since: days(1) }],
say: { en: "reload the editor's window", zh: "重新加载编辑器窗口", "zh-TW": "重新載入編輯器視窗", ja: "エディタのウィンドウを再読み込み", de: "Editorfenster neu laden" } },
app: { copies: [{ kind: "app", since: days(1) }],
say: { en: "quit and reopen the Codex app", zh: "退出并重开 Codex 应用", "zh-TW": "結束並重開 Codex 應用程式", ja: "Codex アプリを終了", de: "Codex-App beenden" } },
embedded: { copies: [{ kind: "embedded", app: "Agents Anywhere", since: days(1) }],
say: { en: "reopen Agents Anywhere", zh: "重开 Agents Anywhere", "zh-TW": "重開 Agents Anywhere", ja: "Agents Anywhere を開き直す", de: "Agents Anywhere neu öffnen" } },
several: { copies: codex.staleCopies,
say: { en: "3 copies of Codex", zh: "3 个运行中的 Codex", "zh-TW": "3 個執行中的 Codex", ja: "Codex 3 件", de: "3 Codex-Instanzen" } },
};
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 [980, 440]) {
test(`${engine} ${lang} ${width}px: the folded row says which Codex to reopen`, 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: 800 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
for (const [name, c] of Object.entries(folded)) {
await page.unrouteAll();
await page.route("**/*", serve(lang, { ...codex, stale: c.copies.length, staleCopies: c.copies }));
await page.goto("http://magpie.test/?view=agents");
const words = page.locator(`${row} .ag-st-t`);
await page.locator(`${row} .ag-conn`).waitFor();
const said = await words.textContent();
assert.ok(said.includes(c.say[lang]), `${name}: ${said}`);
assert.doesNotMatch(said, /\{\w+\}/, `${name}: a placeholder left`);
// the hover has every copy whole, with its command
const title = await words.getAttribute("title");
assert.equal(title.split("\n").length, c.copies.length, `${name}: ${title}`);
if (name === "app") assert.ok(title.includes("⌘Q"), title);
assert.doesNotMatch(title, /\{\w+\}/, `${name}: a placeholder left in ${title}`);
// one line, nothing past the row
const box = await words.evaluate((e) => ({ right: e.getBoundingClientRect().right, lines: Math.round(e.getBoundingClientRect().height / parseFloat(getComputedStyle(e).lineHeight)) }));
assert.ok(box.right <= width, `${name}: past the window`);
assert.equal(box.lines, 1, `${name}: one line`);
if (process.env.ARTIFACT_DIR) await page.locator(row).screenshot({ path: path.join(process.env.ARTIFACT_DIR, `stale-${engine}-${lang}-${width}-${name}.png`) });
}
// a copy of no kind magpie knows keeps the plain words
await page.unrouteAll();
await page.route("**/*", serve(lang, { ...codex, stale: 1, staleCopies: [{ since: days(1) }] }));
await page.goto("http://magpie.test/?view=agents");
await page.locator(`${row} .ag-conn`).waitFor();
assert.equal(await page.locator(`${row} .ag-st-t`).getAttribute("title"), null);
assert.deepEqual(errors, []);
});
}
}
}
// luci on Discord (Codex 0.162): every new codex showed Codex's own models
// after Codex was wired in, since the daemon they attach to had the list
// from before. magpie restarts it itself while no codex session is on it;
// with one on, the daemon's line has a Restart, which restarts it, and
// the row as it is after takes the line away. Only the daemon's line has
// it. Every language, both widths.
const restarted = { en: "Codex's background service restarted", zh: "Codex 的后台服务已重启", "zh-TW": "Codex 的後臺服務已重啟", ja: "Codex のバックグラウンドサービスを再起動しました", de: "Hintergrunddienst von Codex neu gestartet" };
const restartSays = { en: "Restart", zh: "重启", "zh-TW": "重啟", ja: "再起動", de: "Neu starten" };
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 [980, 440]) {
test(`${engine} ${lang} ${width}px: the daemon's line restarts it`, 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: 800 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
let agent = { ...codex, stale: 2, staleCopies: [{ kind: "app", since: days(3) }, { kind: "daemon", since: days(1) }] };
const asked = [];
const fake = serve(lang);
await page.route("**/*", async (route) => {
const url = new URL(route.request().url());
if (url.pathname === "/api/codex/daemon/restart") {
asked.push(route.request().method());
agent = { ...agent, stale: 1, staleCopies: [agent.staleCopies[0]] };
return route.fulfill({ json: { providers: [], presets: [], excluded: [], gateway: { running: true, window: true } } });
}
if (url.pathname === "/api/state") return route.fulfill({ json: { agents: [agent], profiles: [], settings: { lang, theme: "light" } } });
return fake(route);
});
await page.goto("http://magpie.test/?view=agents");
await page.locator(`${row} .ag-conn`).waitFor();
await page.locator(`${row} .ag-link`).click();
const copies = page.locator(`${row} .ag-stale-copy`);
await copies.first().waitFor();
assert.equal(await copies.nth(0).locator("button").count(), 0, "the app's line has no Restart");
const go = copies.nth(1).locator("button");
assert.equal((await go.textContent()).trim(), restartSays[lang]);
// in the line, inside the window
const box = await go.boundingBox();
assert.ok(box.x + box.width <= width, "past the window");
await go.click();
await page.waitForFunction((r) => document.querySelectorAll(r + " .ag-stale-copy").length === 1, row);
assert.deepEqual(asked, ["POST"]);
assert.equal(await page.locator("#status").textContent(), restarted[lang]);
assert.equal(await page.locator(`${row} .ag-stale-copy button`).count(), 0);
if (process.env.ARTIFACT_DIR) await page.locator(row).screenshot({ path: path.join(process.env.ARTIFACT_DIR, `daemon-restart-${engine}-${lang}-${width}.png`) });
assert.deepEqual(errors, []);
});
}
}
}