Python
Prevent resolved approvals from reappearing after delayed updates
Rohit C Prasadb9a30e7978e3
diff --git a/surfaces/gui/e2e/worker-approval.spec.ts b/surfaces/gui/e2e/worker-approval.spec.tsindex 7cbed644..ef1dbbd1 100644--- a/surfaces/gui/e2e/worker-approval.spec.ts+++ b/surfaces/gui/e2e/worker-approval.spec.ts@@ -1,6 +1,74 @@import { expect } from "@playwright/test";import { test, sendSessionEvent } from "./fixtures";+for (const tool of ["run_shell", "decide_worker_call"]) {+ for (const resolution of ["allow", "deny"]) {+ test(`${tool}: ${resolution} completion cannot be resurrected by a delayed pending poll`, async ({ page }) => {+ let staleSnapshot = false;+ let servedStale = 0;+ const args = tool === "run_shell" ? { command: "printf verified" } : {+ worker: "Maya", call_id: "worker-prompt", decision: "allow", note: "Check the assigned task.",+ };+ const workerCall = tool === "decide_worker_call" ? {+ worker: "Maya", tool: "run_shell", arguments: { command: "printf verified" }, state: "pending",+ } : undefined;+ await page.route("**/v1/inbox?**", async route => {+ if (staleSnapshot) {+ if (new URL(route.request().url()).searchParams.has("session_id")) servedStale++;+ await route.fulfill({ json: { items: [{ id: "answered-gate", kind: "approval", state: "pending",+ tool_call_id: "answered-call", title: "Approve action?", data: { tool, arguments: args,+ worker_call: workerCall, worker_prompt_id: workerCall ? "worker-prompt" : undefined } }] } });+ } else await route.fulfill({ json: { items: [] } });+ });+ await page.goto("/");+ await sendSessionEvent(page, { type: "permission_required", data: {+ name: tool, tool_call_id: "answered-call", arguments: args, worker_call: workerCall,+ } });+ const button = page.getByRole("button", { name: tool === "run_shell" ? "Allow once" : "Allow it, as the lead suggests", exact: true });+ await expect(button).toBeVisible();+ await sendSessionEvent(page, { type: "tool_finished", data: {+ name: tool, tool_call_id: "answered-call", status: resolution === "allow" ? "ok" : "denied",+ } });+ await expect(button).toHaveCount(0);+ staleSnapshot = true;+ await expect.poll(() => servedStale, { timeout: 6000 }).toBeGreaterThan(0);+ await page.waitForTimeout(300);+ await expect(button).toHaveCount(0);+ await sendSessionEvent(page, { type: "permission_required", data: {+ name: tool, tool_call_id: "answered-call", arguments: args, worker_call: workerCall,+ } });+ await expect(button).toHaveCount(0);+ // An identical-looking NEW request is still actionable.+ await sendSessionEvent(page, { type: "permission_required", data: {+ name: tool, tool_call_id: "next-call", arguments: args, worker_call: workerCall,+ } });+ await expect(button).toBeVisible();+ });+ }+}++test("worker decision polling retires an externally answered proxy without a completion event", async ({ page }) => {+ let resolved = false;+ await page.route("**/v1/inbox?**", route => route.fulfill({ json: { items: resolved ? [{+ id: "lead-proxy", tool_call_id: "proxy-call", state: "resolved", kind: "approval", resolution: "allow",+ }] : [] } }));+ await page.goto("/");+ await sendSessionEvent(page, { type: "permission_required", data: {+ name: "decide_worker_call", tool_call_id: "proxy-call",+ arguments: { worker: "Maya", call_id: "worker-prompt", decision: "allow" },+ worker_call: { worker: "Maya", tool: "run_shell", arguments: { command: "printf verified" }, state: "pending" },+ } });+ await expect(page.getByTestId("workerdec-card")).toBeVisible();+ resolved = true;+ await expect(page.getByTestId("workerdec-card")).toHaveCount(0, { timeout: 7000 });+ await sendSessionEvent(page, { type: "permission_required", data: {+ name: "decide_worker_call", tool_call_id: "proxy-call",+ arguments: { worker: "Maya", call_id: "worker-prompt", decision: "allow" },+ worker_call: { worker: "Maya", tool: "run_shell", arguments: { command: "printf verified" }, state: "pending" },+ } });+ await expect(page.getByTestId("workerdec-card")).toHaveCount(0);+});+test("ordinary approval answered elsewhere retires on exact tool completion", async ({ page }) => {await page.goto("/");await sendSessionEvent(page, { type: "permission_required", data: {diff --git a/surfaces/gui/src/App.tsx b/surfaces/gui/src/App.tsxindex f7957e92..27507a85 100644--- a/surfaces/gui/src/App.tsx+++ b/surfaces/gui/src/App.tsx@@ -944,6 +944,7 @@ export function App() {]);break;case "permission_required":+ if (d.tool_call_id && finishedGateCalls.current.get(gateScope)?.has(d.tool_call_id)) break;// Unattended → the backend parked it in the Inbox; don't also surface a live card.if (unattendedRef.current) break;setItems((p) => [...p, approvalItemFromPayload(d)]);@@ -963,6 +964,7 @@ export function App() {case "team_proposed":// The staffing gate (agent teams) — approval pre-spawns the worker sessions.if (unattendedRef.current) break;+ if (d.tool_call_id && finishedGateCalls.current.get(gateScope)?.has(d.tool_call_id)) break;setItems((p) => [...p, teamItemFromPayload(d)]);break;case "connector_requested":@@ -973,6 +975,7 @@ export function App() {case "items_proposed":// The decomposition gate — approval creates the items on the board.if (unattendedRef.current) break;+ if (d.tool_call_id && finishedGateCalls.current.get(gateScope)?.has(d.tool_call_id)) break;setItems((p) => [...p, workItemsItemFromPayload(d)]);break;case "question_requested":@@ -980,7 +983,7 @@ export function App() {setItems((p) => [...p, questionItemFromPayload(d)]);break;case "tool_finished":- if (d.tool_call_id && (d.name === "propose_team" || d.name === "propose_work_items")) {+ if (d.tool_call_id) {const calls = finishedGateCalls.current.get(gateScope) || new Set<string>();calls.add(d.tool_call_id);finishedGateCalls.current.set(gateScope, calls);@@ -1292,6 +1295,13 @@ export function App() {const current = ++request;getInbox(sessionId).then(inbox => {if (canceled || current !== request) return;+ // Persist authoritative resolutions too: another in-flight pending-only+ // fetch or a late gate event must not resurrect an already answered call.+ const calls = finishedGateCalls.current.get(gateScope) || new Set<string>();+ for (const item of inbox) {+ if (item.state === "resolved" && item.tool_call_id) calls.add(item.tool_call_id);+ }+ finishedGateCalls.current.set(gateScope, calls);setSessionInbox(pendingInbox(inbox));setItems(items => reconcileResolvedGates(items, inbox));}).catch(() => {});@@ -1302,7 +1312,7 @@ export function App() {load();const t = setInterval(load, 4000);return () => { canceled = true; clearInterval(t); };- }, [surface, sessionId, browserRefreshKey, markUnattended, pendingInbox]);+ }, [surface, sessionId, browserRefreshKey, markUnattended, pendingInbox, gateScope]);const send = (text: string, attachments?: Attachment[], skill?: string) => {// UX-029: folder enforcement AT SEND. A code-family session with no folder has no