Multiplayer agent harness for work.
Keep resolved credential approvals dismissed after refresh failures (#1767)
Regan Belld4b99c3d5c0c
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.tsindex c20b9a55b..d2cdf917c 100644--- a/plugins/web-ui/src/chat.ts+++ b/plugins/web-ui/src/chat.ts@@ -97,6 +97,7 @@ import {makeOpenerStreamFn,makeRunResumeStreamFn,resolveApproval,+ unresolvedApprovals,runApprovalTurn,type RunPoll,TAIL_TURNS,@@ -817,7 +818,7 @@ export function createChatSurface(if (!chatState.resolvingApprovals.has(approval.requestId)) byId.set(approval.requestId, approval);}}- return [...byId.values()];+ return unresolvedApprovals([...byId.values()]);}function hasUnresolvedApproval(): boolean {@@ -827,9 +828,7 @@ export function createChatSurface(async function syncPendingApprovals(agent: Agent, messages = agent.state.messages): Promise<void> {const id = chatState.sessionId;if (!id || agent !== chatState.agent) return;- const r = await api<{ approvals: PendingApproval[] }>(`/api/sessions/${encodeURIComponent(id)}/approvals`).catch(- () => null,- );+ const r = await fetchSessionApprovals(id);if (!r || id !== chatState.sessionId || agent !== chatState.agent) return;for (const message of messages) delete (message as AssistantWork).work?.pendingApprovals;attachPendingApprovals(messages, r.approvals ?? [], transcriptModel());@@ -1103,6 +1102,7 @@ export function createChatSurface(host.className = "custom-chat readonly-chat";let approvals: PendingApproval[] = [];const draw = () => {+ approvals = unresolvedApprovals(approvals);const shownMessages = chatState.inheritedExpanded ? [...chatState.inheritedMessages, ...messages] : messages;prepareMessageRows(shownMessages);render(@@ -1466,6 +1466,10 @@ export function createChatSurface(function drawActiveChat(agent = chatState.agent, opts: { forceScroll?: boolean } = {}): void {if (!agent || agent !== chatState.agent || !chatState.host || (!ctx.inbox && appState.currentView !== "chats"))return;+ for (const message of agent.state.messages) {+ const work = (message as AssistantWork).work;+ if (work?.pendingApprovals) work.pendingApprovals = unresolvedApprovals(work.pendingApprovals);+ }adoptActiveSessionFromList(agent);if (!ctx.visible()) {postCurrentPaneState();@@ -2544,6 +2548,7 @@ export function createChatSurface(const now = Date.now();const elapsed = goalElapsedLabel(row.startedAt, row.endedAt ?? now);const peeking = subagentUi.peekId === row.session.id;+ const approvals = unresolvedApprovals(subagentUi.approvals.get(row.session.id) ?? []);const title = row.session.title?.trim() || "Subagent";return html`<divclass="subagent-row ${row.state} ${peeking ? "peeking" : ""}"@@ -2572,8 +2577,8 @@ export function createChatSurface(Open</button>${- subagentUi.approvals.get(row.session.id)?.length- ? ctx.composer.composerApprovalPanel(subagentUi.approvals.get(row.session.id)!, (decision) =>+ approvals.length+ ? ctx.composer.composerApprovalPanel(approvals, (decision) =>resolveSubagentApproval(row.session.id, decision),): nothingdiff --git a/plugins/web-ui/src/core-bridge.ts b/plugins/web-ui/src/core-bridge.tsindex 64c813c44..ff8bd99c6 100644--- a/plugins/web-ui/src/core-bridge.ts+++ b/plugins/web-ui/src/core-bridge.ts@@ -354,8 +354,17 @@ export async function fetchTranscript(return api<TranscriptPage>(`/api/sessions/${encodeURIComponent(id)}${suffix}`);}+const resolvedCredentialApprovals = new Set<string>();++export function unresolvedApprovals(approvals: PendingApproval[]): PendingApproval[] {+ const pending = approvals.filter((approval) => !resolvedCredentialApprovals.has(approval.requestId));+ return pending.length === approvals.length ? approvals : pending;+}+export function fetchSessionApprovals(id: string): Promise<{ approvals: PendingApproval[] } | null> {- return api<{ approvals: PendingApproval[] }>(`/api/sessions/${encodeURIComponent(id)}/approvals`).catch(() => null);+ return api<{ approvals: PendingApproval[] }>(`/api/sessions/${encodeURIComponent(id)}/approvals`)+ .then((result) => ({ approvals: unresolvedApprovals(result.approvals) }))+ .catch(() => null);}export async function fetchEntry(sessionId: string, seq: number): Promise<SessionEntry> {@@ -983,13 +992,17 @@ export function makeRunResumeStreamFn(export async function resolveApproval(decision: ApprovalDecision): Promise<string | null> {if (decision.requestId.startsWith("keychain:")) {+ if (resolvedCredentialApprovals.has(decision.requestId)) return "";const id = decision.requestId.slice("keychain:".length);let choice = "deny";if (decision.approved) choice = decision.scope === "always" ? "standing" : "once";- await api(`/api/keychain/approvals/${encodeURIComponent(id)}`, {+ const result = await api<{ ask: { status: string } }>(`/api/keychain/approvals/${encodeURIComponent(id)}`, {method: "POST",body: JSON.stringify({ decision: choice }),});+ if (!["approved", "declined", "expired"].includes(result.ask?.status))+ throw new Error("The credential request is still pending. Please try again.");+ resolvedCredentialApprovals.add(decision.requestId);return "";}const submit = await api<{ runId?: string }>(`/api/approvals/${encodeURIComponent(decision.requestId)}`, {@@ -1010,6 +1023,10 @@ export async function runApprovalTurn(onWork: WorkObserver | undefined,slot?: RunSlot,): Promise<void> {+ if (decision.requestId.startsWith("keychain:")) {+ await resolveApproval(decision);+ return;+ }const stream = createAssistantMessageEventStream();await driveApproval(stream, agent.state.model, decision, onWork, slot);const outcome = await stream.result();@@ -2152,6 +2169,7 @@ export function attachPendingApprovals(approvals: PendingApproval[],model?: Model<Api>,): void {+ approvals = unresolvedApprovals(approvals);if (!approvals.length) return;const turnForCommand = (command: string): AssistantWork | undefined => {diff --git a/plugins/web-ui/test/credential-approval-resolution.test.ts b/plugins/web-ui/test/credential-approval-resolution.test.tsnew file mode 100644index 000000000..c08d1612e--- /dev/null+++ b/plugins/web-ui/test/credential-approval-resolution.test.ts@@ -0,0 +1,89 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import {+ attachPendingApprovals,+ fetchSessionApprovals,+ resolveApproval,+ runApprovalTurn,+ unresolvedApprovals,+ type PendingApproval,+} from "../src/core-bridge.ts";+import type { Agent, AgentMessage } from "@earendil-works/pi-agent-core";++const approval = (id: string): PendingApproval => ({+ requestId: `keychain:${id}`,+ command: "aws",+ reason: "Credential approval",+});++test("accepted credential decisions survive failed refreshes and late pending responses", async () => {+ const pending = approval("refresh-race");+ const other = approval("unrelated");+ const original = globalThis.fetch;+ let finishRead!: (response: Response) => void;+ let posts = 0;+ globalThis.fetch = (async (_url, init) => {+ if (init?.method === "POST") {+ posts++;+ return Response.json({ ask: { status: "approved" } });+ }+ return new Promise<Response>((resolve) => {+ finishRead = resolve;+ });+ }) as typeof fetch;+ try {+ const staleRead = fetchSessionApprovals("parent");+ await resolveApproval({ requestId: pending.requestId, approved: true, scope: "always" });+ assert.deepEqual(unresolvedApprovals([pending, other]), [other]);+ finishRead(Response.json({ approvals: [pending, other] }));+ assert.deepEqual(await staleRead, { approvals: [other] });+ globalThis.fetch = (async () => {+ throw new Error("network unavailable");+ }) as typeof fetch;+ assert.equal(await fetchSessionApprovals("child"), null);+ assert.deepEqual(unresolvedApprovals([pending, other]), [other]);+ const messages: AgentMessage[] = [];+ attachPendingApprovals(messages, [pending]);+ assert.deepEqual(messages, []);+ await resolveApproval({ requestId: pending.requestId, approved: true, scope: "always" });+ assert.equal(posts, 1);+ } finally {+ globalThis.fetch = original;+ }+});++test("failed and still-pending decisions remain retryable", async () => {+ const pending = approval("retryable");+ const original = globalThis.fetch;+ try {+ globalThis.fetch = (async () => Response.json({ error: "unavailable" }, { status: 503 })) as typeof fetch;+ await assert.rejects(resolveApproval({ requestId: pending.requestId, approved: true, scope: "once" }));+ assert.deepEqual(unresolvedApprovals([pending]), [pending]);+ globalThis.fetch = (async () => Response.json({ ask: { status: "pending" } })) as typeof fetch;+ await assert.rejects(+ resolveApproval({ requestId: pending.requestId, approved: true, scope: "once" }),+ /still pending/,+ );+ assert.deepEqual(unresolvedApprovals([pending]), [pending]);+ globalThis.fetch = (async () => Response.json({ ask: { status: "declined" } })) as typeof fetch;+ await resolveApproval({ requestId: pending.requestId, approved: false });+ assert.deepEqual(unresolvedApprovals([pending]), []);+ } finally {+ globalThis.fetch = original;+ }+});++test("read-only approval turns use the credential endpoint and preserve duration", async () => {+ const original = globalThis.fetch;+ const calls: Array<[string, unknown]> = [];+ globalThis.fetch = (async (url, init) => {+ calls.push([String(url), JSON.parse(String(init?.body))]);+ return Response.json({ ask: { status: "approved" } });+ }) as typeof fetch;+ try {+ await runApprovalTurn({} as Agent, { requestId: "keychain:readonly", approved: true, scope: "once" }, undefined);+ assert.deepEqual(calls, [["/api/keychain/approvals/readonly", { decision: "once" }]]);+ } finally {+ globalThis.fetch = original;+ }+});diff --git a/plugins/web-ui/test/keychain-flow.test.ts b/plugins/web-ui/test/keychain-flow.test.tsindex 23819dcd0..a02b9b259 100644--- a/plugins/web-ui/test/keychain-flow.test.ts+++ b/plugins/web-ui/test/keychain-flow.test.ts@@ -209,8 +209,8 @@ test("an inline credential card decides through the keychain approval route, nottry {const { resolveApproval } = await import("../src/core-bridge.ts");assert.equal(await resolveApproval({ requestId: "keychain:a1", approved: true, scope: "always" }), "");- await resolveApproval({ requestId: "keychain:a1", approved: true, scope: "once" });- await resolveApproval({ requestId: "keychain:a1", approved: false });+ await resolveApproval({ requestId: "keychain:a2", approved: true, scope: "once" });+ await resolveApproval({ requestId: "keychain:a3", approved: false });} finally {globalThis.fetch = realFetch;}@@ -218,8 +218,8 @@ test("an inline credential card decides through the keychain approval route, notcalls.map((c) => [c.url.replace(/^.*(\/api\/)/, "$1"), (c.body as { decision: string }).decision]),[["/api/keychain/approvals/a1", "standing"],- ["/api/keychain/approvals/a1", "once"],- ["/api/keychain/approvals/a1", "deny"],+ ["/api/keychain/approvals/a2", "once"],+ ["/api/keychain/approvals/a3", "deny"],],);});diff --git a/plugins/web-ui/test/swarm-approvals.test.ts b/plugins/web-ui/test/swarm-approvals.test.tsindex d0def0ea3..d0cd290a6 100644--- a/plugins/web-ui/test/swarm-approvals.test.ts+++ b/plugins/web-ui/test/swarm-approvals.test.ts@@ -58,7 +58,12 @@ test("worker transcripts allow approvals without enabling the composer", async (title: "Test",};const entries = [{ seq: 1, type: "user", createdAt: Date.now(), payload: { text: "run the command" } }];- let pending = [approval];+ const credential: PendingApproval = {+ requestId: "keychain:readonly-cache",+ command: "aws",+ reason: "credential access",+ };+ let pending = [approval, credential];const decision = deferred<Response>();const continuation = deferred<Response>();const requests: Array<{ path: string; body?: Record<string, unknown> }> = [];@@ -77,6 +82,7 @@ test("worker transcripts allow approvals without enabling the composer", async (scopeOverride: null,});}+ if (path === "/api/keychain/approvals/readonly-cache") return Response.json({ ask: { status: "approved" } });if (path.startsWith("/api/approvals/")) {return decision.promise;}@@ -97,7 +103,7 @@ test("worker transcripts allow approvals without enabling the composer", async (const { appState } = await vite.ssrLoadModule("/src/shell-state.ts");const { sessionsState } = await vite.ssrLoadModule("/src/sessions.ts");const { createConversation } = await vite.ssrLoadModule("/src/conversations.ts");- const { entriesToMessages } = await vite.ssrLoadModule("/src/core-bridge.ts");+ const { entriesToMessages, resolveApproval } = await vite.ssrLoadModule("/src/core-bridge.ts");const { transcriptModel } = await vite.ssrLoadModule("/src/model-options.ts");const { seedRuntimeConfig } = await vite.ssrLoadModule("/src/runtime-config-store.ts");seedRuntimeConfig(row.scopeId, await (await fetch("/api/runtime-config")).json());@@ -120,6 +126,12 @@ test("worker transcripts allow approvals without enabling the composer", async (chat.mountReadOnly(row, entriesToMessages(entries, transcriptModel()));await until(() => !!host.querySelector(".approval-btn"));assert.equal(host.querySelector("textarea"), null);+ const before = host.querySelectorAll(".approval-btn").length;+ await resolveApproval({ requestId: credential.requestId, approved: true, scope: "once" });+ chat.revealEntry(9999);+ assert.ok(host.querySelectorAll(".approval-btn").length < before);+ assert.equal(host.textContent?.includes("credential access"), false);+ assert.equal(host.textContent?.includes("requires approval"), true);const button = [...host.querySelectorAll<HTMLButtonElement>("button")].find((b) => b.textContent?.trim() === "Allow once",)!;