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:
@@ -491,6 +491,11 @@ export class AgentManager {
|
||||
rt.interruptTimer = null;
|
||||
}
|
||||
if (ev.turnId && ev.turnId !== "unknown") rt.endedTurnIds.add(ev.turnId);
|
||||
// A request the Agent left open when its turn ended (Cursor: the model stream died with "[canceled] http/2 stream
|
||||
// closed", the request was never answered or withdrawn) can no longer be answered: cancel it before the turn end
|
||||
// is written. Otherwise it stays in rt.pending, threads.get hands it to the page, and the top bar says 等待授权
|
||||
// with no card to click (the card itself shows 已取消 because the thread is idle).
|
||||
await this.cancelPending(threadId, rt, { tellSession: true });
|
||||
rt.coalescer.dispose();
|
||||
rt.coalescer = this.newCoalescer(threadId);
|
||||
await this.appendRaw(threadId, ev);
|
||||
@@ -578,16 +583,28 @@ export class AgentManager {
|
||||
return n;
|
||||
}
|
||||
|
||||
/** Cancel every open permission/question: persisted as permission_resolved{cancelled} (the page marks the card
|
||||
* 已取消 and stops waiting). tellSession: also answer the provider's own waiter (an Agent that ended its turn without
|
||||
* withdrawing the request would otherwise keep a dangling promise); its echo is dropped because rt.pending is empty. */
|
||||
private async cancelPending(threadId: string, rt: Runtime, opts: { tellSession: boolean }): Promise<void> {
|
||||
if (!rt.pending.size) return;
|
||||
const open = [...rt.pending.values()];
|
||||
rt.pending.clear();
|
||||
for (const p of open) {
|
||||
await this.appendRaw(threadId, { type: "permission_resolved", requestId: p.requestId, cancelled: true });
|
||||
p.resolve({ cancelled: true });
|
||||
if (opts.tellSession) {
|
||||
try { rt.session?.respond(p.requestId, { cancelled: true }); } catch { /* the session may be gone */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---- interrupt ----
|
||||
async interrupt(threadId: string): Promise<void> {
|
||||
const rt = this.rt(threadId);
|
||||
if (!rt.session || rt.status !== "running") return;
|
||||
// Invalidate all pending requests
|
||||
for (const [id, p] of rt.pending) {
|
||||
await this.appendRaw(threadId, { type: "permission_resolved", requestId: id, cancelled: true });
|
||||
p.resolve({ cancelled: true });
|
||||
}
|
||||
rt.pending.clear();
|
||||
await this.cancelPending(threadId, rt, { tellSession: false });
|
||||
await rt.session.interrupt();
|
||||
const turnId = rt.currentTurnId;
|
||||
// No turn_ended within 1 second → append interrupted
|
||||
|
||||
@@ -7,7 +7,8 @@ import { tr } from "../../util/i18n.js";
|
||||
|
||||
export type PermBridge = {
|
||||
push(ev: ProviderEvent): void;
|
||||
waitFor(requestId: string): Promise<RespondAnswer>;
|
||||
/** signal: aborted when the Agent withdraws the request ($/cancel_request) or the connection closes */
|
||||
waitFor(requestId: string, signal?: AbortSignal): Promise<RespondAnswer>;
|
||||
/** Cursor Auto-review: no permission card, allow is chosen automatically */
|
||||
autoAcceptPermissions?: () => boolean;
|
||||
};
|
||||
@@ -42,7 +43,7 @@ export function registerAcpClientHandlers(app: ClientApp, bridge: PermBridge): v
|
||||
detail: tc.rawInput ?? undefined,
|
||||
options,
|
||||
});
|
||||
const answer = await bridge.waitFor(requestId);
|
||||
const answer = await bridge.waitFor(requestId, ctx.signal);
|
||||
if (answer.cancelled) return { outcome: { outcome: "cancelled" } };
|
||||
const optionId = answer.optionId ?? options[0]?.id;
|
||||
if (!optionId) return { outcome: { outcome: "cancelled" } };
|
||||
|
||||
@@ -113,7 +113,7 @@ export class AcpSession implements ProviderSession {
|
||||
const app = client({ name: "magpie-agent-console" });
|
||||
registerAcpClientHandlers(app, {
|
||||
push: (ev) => this.q.push(ev),
|
||||
waitFor: (id) => this.waitPerm(id),
|
||||
waitFor: (id, signal) => this.waitPerm(id, signal),
|
||||
autoAcceptPermissions: () => this.cursorAutoReview,
|
||||
});
|
||||
const stream = acpStreamFromChild(this.child);
|
||||
@@ -356,12 +356,24 @@ export class AcpSession implements ProviderSession {
|
||||
await this.applyCursorAutoReviewConfig(false);
|
||||
}
|
||||
|
||||
private waitPerm(requestId: string): Promise<RespondAnswer> {
|
||||
private waitPerm(requestId: string, signal?: AbortSignal): Promise<RespondAnswer> {
|
||||
return new Promise((resolve) => {
|
||||
this.permPending.set(requestId, resolve);
|
||||
// The Agent withdrew the request ($/cancel_request) or the connection closed: the card must not stay clickable
|
||||
if (signal?.aborted) this.cancelPerm(requestId);
|
||||
else signal?.addEventListener("abort", () => this.cancelPerm(requestId), { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
/** Close an open permission request as cancelled (answer the Agent's request and tell the manager) */
|
||||
private cancelPerm(requestId: string): void {
|
||||
const r = this.permPending.get(requestId);
|
||||
if (!r) return;
|
||||
this.permPending.delete(requestId);
|
||||
r({ cancelled: true });
|
||||
this.q.push({ type: "permission_resolved", requestId, cancelled: true });
|
||||
}
|
||||
|
||||
respond(requestId: string, answer: RespondAnswer): void {
|
||||
const r = this.permPending.get(requestId);
|
||||
if (r) {
|
||||
@@ -425,6 +437,10 @@ export class AcpSession implements ProviderSession {
|
||||
}
|
||||
|
||||
private endTurn(ev: ProviderEvent): void {
|
||||
// The prompt turn is over (end_turn, refusal, error, cancel): any request still open can't be answered any more.
|
||||
// Cursor ends a turn this way when its model stream dies ("[canceled] http/2 stream closed … CANCEL") right after
|
||||
// asking. Cancel those before the turn end so the timeline never keeps a request open past its turn.
|
||||
for (const id of [...this.permPending.keys()]) this.cancelPerm(id);
|
||||
this.q.push(ev);
|
||||
this.activeTurnId = null;
|
||||
this.running = false;
|
||||
|
||||
@@ -277,6 +277,25 @@ async function scriptPerm(ctx: Ctx): Promise<void> {
|
||||
emit({ type: "assistant_message", itemId: "pm", text: "测试通过了。" });
|
||||
}
|
||||
|
||||
// Like Cursor when its model stream dies ("http/2 stream closed … CANCEL"): it asks for permission, then ends the turn
|
||||
// itself without waiting for (or withdrawing) the answer. The manager must not keep the request pending afterwards.
|
||||
async function scriptPermAbandon(ctx: Ctx): Promise<void> {
|
||||
const { emit } = ctx;
|
||||
emit({
|
||||
type: "permission_request",
|
||||
requestId: "perm-" + rid(),
|
||||
title: "运行 git status",
|
||||
kind: "execute",
|
||||
detail: { command: "git status" },
|
||||
options: [
|
||||
{ id: "allow-once", label: tr("perm.allow"), kind: "allow_once" },
|
||||
{ id: "reject-once", label: tr("perm.deny"), kind: "reject_once" },
|
||||
],
|
||||
});
|
||||
await sleepAbortable(200, ctx);
|
||||
emit({ type: "assistant_message", itemId: "pa", text: "Error: RetriableError: [canceled] http/2 stream closed with error code CANCEL (0x8)" });
|
||||
}
|
||||
|
||||
async function scriptPlan(ctx: Ctx): Promise<void> {
|
||||
const { emit } = ctx;
|
||||
emit({
|
||||
@@ -372,6 +391,7 @@ async function scriptExit(_ctx: Ctx): Promise<void> {
|
||||
|
||||
function pickScript(text: string): (ctx: Ctx) => Promise<void> {
|
||||
const t = text.trim();
|
||||
if (t.startsWith("/perm-abandon")) return scriptPermAbandon;
|
||||
if (t.startsWith("/perm")) return scriptPerm;
|
||||
if (t.startsWith("/plan")) return scriptPlan;
|
||||
if (t.startsWith("/ask")) return scriptAsk;
|
||||
|
||||
Reference in New Issue
Block a user