agent-console 1.3.3: reconnect on return, sidebar dots, permission-wait, chat icon (#760)

qingnian108's follow-up to #747 and #758, on top of 1.3.2.

- A permission request still open when its turn ends is cancelled by the
  daemon (manager and ACP session both), and an ACP request the Agent
  withdraws ($/cancel_request, through the handler's AbortSignal) closes
  as cancelled. The page waits only on cards that still show buttons, so
  a finished turn no longer leaves 等待授权 with nothing to click. The
  等待授权 pill scrolls to the waiting card.
- 添加到对话 uses a speech-bubble icon.
- Sidebar dot: blinks while running, green after a completed turn, red
  on failure, amber while waiting.
- Wrapper: every disconnect / invalid-link end state offers Reconnect;
  the last safe back URL is kept in sessionStorage (safeBack re-checked
  on read). Coming back to the page (visible / focus) probes the child
  for 4 s instead of waiting out timers pushed back while hidden.

Review (head 0c8ee32b): every changed source line read. 1.3.2's Codex
model_catalog_json change is kept (codex/provider.ts untouched, rebuilt
dist still carries it). No new network in tests, page text set through
textContent/React, no new timers in the page or daemon; the wrapper's
sessionStorage back passes the same loopback check (tested with an evil
stored value).

Run (mote, macOS), standalone copy of packages/agent-console:
- npm ci; npm run check OK.
- vitest: 65 files, 489 passed / 17 skipped.
- npm run build, then verify:build: committed app/ and daemon/dist
  identical to the rebuild.
On the PR merged onto main 5026f636: root `bun test
packages/agent-console` 6/6, `bun scripts/check.mjs` exit 0.

Not tried: scripts/e2e-v1.mjs (Playwright, scenario 28), the page with a
live Cursor/Claude/Codex, Windows/Linux.
This commit is contained in:
qingnian108
2026-11-05 18:14:53 +08:00
committed by GitHub
parent 5026f6362e
commit 41e90a18d3
30 changed files with 870 additions and 131 deletions
+82 -8
View File
@@ -10,6 +10,15 @@
// fresh session.js and opens the gate, which restarts the daemon and sends the frame here again with the new port.
// - at most 3 automatic tries in 2 minutes (counted in ?ac_try=<start>.<n>, carried through b → boot.js → gate → here);
// then a button, so a daemon that can't start doesn't loop forever.
// - coming back to the page (visibilitychange → visible, or window focus) checks at once instead of waiting out the timers
// that were pushed back while hidden: if the child never said alive within bootMs of loading, or its last real alive is
// older than silentMs, it gets probeMs (two heartbeats) to speak up, then recover(); a child that is still alive is
// never refreshed. If an end state with Reconnect is showing, coming back counts as the user clicking it (also when
// the 3 automatic tries are used up), so the user never returns to a dead button.
// - every disconnect / invalid-link end state shows a Reconnect button. A missing #b= is filled from sessionStorage
// (last safe Magpie back URL written when this page first saw a valid b; read/write re-run safeBack). With a stored
// back, recover navigates via go(false). With none, the button reloads and the copy tells the user to flip Magpie's
// top-bar page and open Agent Console again if reload is not enough.
//
// Security: this page holds no secret of its own. The daemon token arrives only in its # fragment (as before, never sent to a
// server, not in Referer) and is handed to the child in the child's fragment; the gate key is never here. The child is always
@@ -35,7 +44,7 @@ export function parseTry(raw) {
return m ? { start: Number(m[1]), n: Number(m[2]) } : null;
}
export const WRAP_TIMING = { bootMs: 12000, afterLoadMs: 6000, silentMs: 15000, maxTries: 3, triesWindowMs: 120000 };
export const WRAP_TIMING = { bootMs: 12000, afterLoadMs: 6000, silentMs: 15000, maxTries: 3, triesWindowMs: 120000, probeMs: 4000 };
const SCRIPT = String.raw`(function () {
"use strict";
@@ -44,7 +53,16 @@ const SCRIPT = String.raw`(function () {
var frag = new URLSearchParams(location.hash.slice(1));
var token = frag.get("t") || "";
var port = Number(frag.get("p"));
var BACK_KEY = "ac_wrap_back";
function readStoredBack() {
try { return safeBack(sessionStorage.getItem(BACK_KEY)); } catch (e) { return null; }
}
function writeStoredBack(v) {
if (!v) return;
try { sessionStorage.setItem(BACK_KEY, v); } catch (e) {}
}
var back = safeBack(frag.get("b"));
if (back) writeStoredBack(back);
var q = new URLSearchParams(location.search);
var lang = (q.get("lang") || "").toLowerCase();
var zh = !lang || lang.indexOf("zh") === 0;
@@ -53,13 +71,34 @@ const SCRIPT = String.raw`(function () {
var box = document.getElementById("m");
var said = document.getElementById("s");
var btn = document.getElementById("r");
var RECONNECT = ["重新连接", "Reconnect"];
function onReturn(fn) {
document.addEventListener("visibilitychange", function () { if (document.visibilityState === "visible") fn(); });
window.addEventListener("focus", function () { if (document.visibilityState !== "hidden") fn(); });
}
function msg(zhText, enText, button) {
said.textContent = zh ? zhText : enText;
box.hidden = false;
if (button) { btn.textContent = zh ? button[0] : button[1]; btn.hidden = false; }
}
// The Reconnect action of the end state on screen (null while none is): coming back to the page runs it once
var ended = null;
function wireReconnect(targetBack) {
ended = btn.onclick = function () {
if (btn.disabled) return;
btn.disabled = true;
if (targetBack) location.replace(targetBack);
else location.reload();
};
}
if (!/^[0-9a-f]{32,128}$/i.test(token) || !(port > 0 && port < 65536 && Math.floor(port) === port)) {
msg("链接已失效,请在 Magpie 里重新打开插件页。", "The link is no longer valid. Please reopen the Agent Console page in Magpie.");
msg(
"链接已失效。请点重新连接;若仍不行,可在 Magpie 顶栏先切到别的页再点回「Agent 控制台」。",
"This link is no longer valid. Click Reconnect; if that fails, switch to another Magpie top-bar page, then open Agent Console again.",
RECONNECT
);
wireReconnect(back || readStoredBack());
onReturn(function () { if (ended) ended(); });
return;
}
@@ -71,10 +110,15 @@ const SCRIPT = String.raw`(function () {
var everAlive = false, lastAlive = 0, recovering = false;
var loadedAt = Date.now(), lastTick = Date.now();
// Unlike lastAlive / loadedAt these are never pushed back while hidden or asleep: what the return check compares against
var heardAt = 0, bootAt = loadedAt, probeFrom = 0, probeUntil = 0;
msg("正在连接本地服务…", "Connecting to the local service…");
// A dead port shows the browser's error page, which also fires load: once loaded, the child gets T.afterLoadMs to say alive
f.addEventListener("load", function () {
if (!everAlive) loadedAt = Math.min(loadedAt, Date.now() - (T.bootMs - T.afterLoadMs));
if (!everAlive) {
loadedAt = Math.min(loadedAt, Date.now() - (T.bootMs - T.afterLoadMs));
bootAt = Math.min(bootAt, Date.now() - (T.bootMs - T.afterLoadMs));
}
});
function recover(at) {
@@ -84,8 +128,22 @@ const SCRIPT = String.raw`(function () {
var now = Date.now();
var t = (function (raw) { var m = /^(\d{10,15})\.([0-9])$/.exec(raw || ""); return m ? { start: +m[1], n: +m[2] } : null; })(q.get("ac_try"));
if (!t || now - t.start > T.triesWindowMs || t.start > now) t = { start: now, n: 0 };
// #b= missing (or unsafe): reuse the last safe Magpie back URL from this origin, then go(false) — not an auto try
if (!back) {
msg("本地服务已断开。请在 Magpie 里重新打开插件页。", "The local service disconnected. Please reopen the Agent Console page in Magpie.");
back = readStoredBack();
if (back) {
msg("本地服务已断开,正在重新连接…", "The local service disconnected. Reconnecting…");
var uStored = new URL(back);
if (at > 0) uStored.searchParams.set("ac_lost", String(Math.floor(at)));
location.replace(uStored.toString());
return;
}
msg(
"本地服务已断开。请点重新连接;若仍不行,可在 Magpie 顶栏先切到别的页再点回「Agent 控制台」。",
"The local service disconnected. Click Reconnect; if that fails, switch to another Magpie top-bar page, then open Agent Console again.",
RECONNECT
);
wireReconnect(null);
return;
}
var go = function (auto) {
@@ -95,8 +153,8 @@ const SCRIPT = String.raw`(function () {
location.replace(u.toString());
};
if (t.n >= T.maxTries) {
msg("本地服务多次重连失败。", "Reconnecting to the local service failed several times.", ["重新连接", "Reconnect"]);
btn.onclick = function () { btn.disabled = true; go(false); };
msg("本地服务多次重连失败。", "Reconnecting to the local service failed several times.", RECONNECT);
ended = btn.onclick = function () { if (btn.disabled) return; btn.disabled = true; go(false); };
return;
}
msg("本地服务已断开,正在重新连接…", "The local service disconnected. Reconnecting…");
@@ -108,7 +166,8 @@ const SCRIPT = String.raw`(function () {
if (!d || typeof d !== "object") return;
if (e.source === f.contentWindow) {
if (d.ac_console === "alive") {
lastAlive = Date.now();
lastAlive = heardAt = Date.now();
probeUntil = 0;
if (!everAlive) { everAlive = true; box.hidden = true; f.classList.add("on"); }
return;
}
@@ -126,10 +185,25 @@ const SCRIPT = String.raw`(function () {
}
});
// The page is visible again: don't wait for the timers that were pushed back while hidden
function checkOnReturn() {
if (ended) { ended(); return; } // an end state is showing: same as clicking Reconnect (even with the auto tries used up)
if (recovering || probeUntil) return;
var now = Date.now();
var stale = everAlive ? now - heardAt > T.silentMs : now - bootAt > T.bootMs;
if (stale) { probeFrom = now; probeUntil = now + T.probeMs; } // a child that is still there answers with its next heartbeat
}
onReturn(checkOnReturn);
setInterval(function () {
var now = Date.now();
var gap = now - lastTick;
var prevTick = lastTick, gap = now - lastTick;
lastTick = now;
if (probeUntil) {
// Hidden or suspended again after the probe started: drop it (a gap that ended before it, e.g. the wake itself, is fine)
if (document.visibilityState === "hidden" || (gap > 5000 && prevTick >= probeFrom)) probeUntil = 0;
else if (now >= probeUntil) { probeUntil = 0; recover(everAlive ? heardAt : 0); return; }
}
// This page's own timers were held (sleep, a frozen or heavily throttled frame): give the child a fresh window
if (gap > 5000 || document.visibilityState === "hidden") {
if (everAlive) lastAlive = Math.max(lastAlive, now);