page-agent-console 1.1.0: QA fixes, neutral accent, own dropdown menus, self-heal after a host restart, by qingnian108 (#569)

page-agent-console 1.1.0, by qingnian108 (#569).

QA fixes B1-B21, a reworked UI, a new logo, and self-heal after a plugin host restart. In this version:
- Accent: a fixed neutral grey (charcoal in light mode, light grey in dark mode). Magpie's accent tokens are dropped on purpose. Surfaces, text, lines, fonts and semantic colours still follow magpie.
- Every native <select> (composer effort/mode/Cursor fast, Settings, Debug) is now the page's own SelectMenu, a button plus a listbox.
- The registry summary again discloses the 127.0.0.1 service and its lifecycle, that only its own page gets the token, the ~46 MB first install, and that Claude Code isn't bundled (en, zh).
- session.js is written once at bind with beat:false, and beats every 4s only after the first /open. A new host deletes a dead host's session.js at startup (owner pid in session-owner.json, outside the page folder).
- The daemon swallows console EPIPE after a hard host kill and handles a lifeline close once.

Run at 930c6e2 on macOS (mote, Node 22), in a fresh copy:
- npm ci, then npm run check (tsc daemon + web): ok
- npx vitest run: 46 files, 288 passed, 17 skipped
- npm run verify:build: app.js, app.css, index.html and daemon/dist/main.js(.map) rebuild byte-identical
- bun scripts/check.mjs agent-console: ok
- Playwright Chromium and WebKit, light and dark, sandbox HOME, page served under /_magpie/plugin/<id>/page with a stub bridge passing accent #ff0000:
  - accent: --accent and the primary button stay grey (rgb(38,38,38) light, rgb(229,229,229) dark); no <select> anywhere.
  - Settings menus: click opens the menu inside the window; ArrowDown+Enter picks, closes and refocuses the trigger; the pick persists through the daemon.
  - Esc closes the menu, not the dialog, and then closes the dialog. Clicking an option keeps the dialog open; a click outside closes the menu.
  - A host that was never opened writes beat:false and doesn't rewrite session.js. SIGKILL it, start a new host: the stale file is removed and the page reaches the new daemon.
  - After an open, session.js beats. SIGKILL the host: the page heals in ~5.3s and stays. A daemon-only crash heals in ~5.3s.
  - Daemon logs: no uncaughtException or EPIPE, one lifeline line per daemon exit.
Not tried: the composer pickers in a browser (they need a thread with an installed agent; same SelectMenu component), magpie's real sandboxed frame, Windows and Linux.
This commit is contained in:
qingnian108
2026-11-01 00:37:07 +08:00
committed by GitHub
parent 6727a1e68c
commit 08ad5e3956
77 changed files with 3541 additions and 1251 deletions
@@ -0,0 +1,62 @@
// Self-heal after the plugin host restarts (Magpie restarts its Bun plugin host from time to time; the old host's daemon exits with it
// and the gate key / port change). This page was opened by the gate with the Magpie plugin page URL in the fragment (#t=…&b=…).
// When the daemon can't be reached for a few seconds, the page goes back to that Magpie page: boot.js reads the fresh session.js and
// opens the new gate, which starts a new daemon. Only a loopback Magpie plugin page URL is ever followed (no open redirect),
// and the token is never put anywhere but this page's own fragment.
/** The same check as lib/gate.mjs safeBackUrl: http(s), loopback host, no credentials, /_magpie/plugin/<id>/[page/]index.html */
export function safeBackUrl(raw: string | null | undefined): string | null {
if (typeof raw !== "string" || !raw || raw.length > 2048) return null;
let u: URL;
try { u = new URL(raw); } catch { return null; }
if (u.protocol !== "http:" && u.protocol !== "https:") return null;
if (u.username || u.password) return null;
const host = u.hostname.toLowerCase();
if (host !== "127.0.0.1" && host !== "localhost" && host !== "[::1]") return null;
if (!/^\/_magpie\/plugin\/[A-Za-z0-9_-]{1,128}\/(?:page\/)?index\.html$/.test(u.pathname)) return null;
return `${u.protocol}//${u.host}${u.pathname}${u.search}`;
}
export function backUrlFromHash(hash: string): string | null {
const fragment = hash.startsWith("#") ? hash.slice(1) : hash;
return safeBackUrl(new URLSearchParams(fragment).get("b"));
}
let lostAt = 0;
/** The URL to go back to, with ac_lost = when the connection dropped: boot.js then ignores a session.js last refreshed before
* that (the killed host's) and waits for the new host's. */
export function backTarget(hash: string, lost: number): string | null {
const b = backUrlFromHash(hash);
if (!b) return null;
const u = new URL(b);
if (lost > 0) u.searchParams.set("ac_lost", String(Math.floor(lost)));
return u.toString();
}
/** Navigate back through the Magpie plugin page; false when this page doesn't know it (opened some other way) */
export function goBackToMagpie(): boolean {
const target = backTarget(location.hash, lostAt);
if (!target) return false;
location.replace(target);
return true;
}
/** Calls onStuck once the connection has been down for `ms` without coming back */
export function watchReconnect(
ws: { onState(fn: (s: string) => void): () => void; getState(): string },
onStuck: () => void,
ms = 5000,
): () => void {
let timer: ReturnType<typeof setTimeout> | null = null;
const off = ws.onState((s) => {
if (s === "reconnecting" || s === "down") {
if (!lostAt) lostAt = Date.now();
if (!timer) timer = setTimeout(() => { timer = null; if (ws.getState() !== "open") onStuck(); }, ms);
} else if (s === "open") {
lostAt = 0;
if (timer) { clearTimeout(timer); timer = null; }
}
});
return () => { off(); if (timer) clearTimeout(timer); };
}
+3 -3
View File
@@ -93,7 +93,7 @@ export class WsClient {
// Reject all pending requests
for (const [id, p] of this.pending) {
clearTimeout(p.timer);
p.rej(new Error("连接中断"));
p.rej(new Error(t("api.connClosed")));
this.pending.delete(id);
}
if (this.closed) return;
@@ -114,13 +114,13 @@ export class WsClient {
return new Promise((res, rej) => {
const ws = this.ws;
if (!ws || ws.readyState !== WebSocket.OPEN) {
rej(new Error("连接未建立"));
rej(new Error(t("api.notConnected")));
return;
}
const id = ++this.seq;
const timer = setTimeout(() => {
this.pending.delete(id);
rej(new Error("请求超时"));
rej(new Error(t("api.timeout")));
}, timeoutMs);
this.pending.set(id, { res, rej, timer });
ws.send(JSON.stringify({ t: "req", id, m: method, p: params ?? {} }));