|
| 1 | +// Browser approval of a gated MCP action, end to end through the real console. |
| 2 | +// |
| 3 | +// A `require_approval` policy turns a built-in tool into an action that pauses |
| 4 | +// for a human. The MCP session runs in `elicitation_mode=browser`, so the gated |
| 5 | +// `execute` does not let the model resume inline — it pauses and hands back an |
| 6 | +// `approvalUrl`. A real browser (signed in as the same identity) opens that |
| 7 | +// console page and clicks Approve / Decline; meanwhile `resume` long-polls for |
| 8 | +// the decision. Approve lets the tool run and return its result; Decline blocks |
| 9 | +// it. This is the leg unit tests structurally cannot cover: a human clicking the |
| 10 | +// button in the rendered ResumeApprovalPage. |
| 11 | +// |
| 12 | +// The policy is removed in an `ensuring` finalizer — a leaked require_approval |
| 13 | +// gate on a shared built-in tool would pause unrelated scenarios. |
| 14 | +// |
| 15 | +// Lives under cloud/ for now because cloud is the only host wired for browser |
| 16 | +// approval; it moves to scenarios/ (cross-target) as self-host and Cloudflare |
| 17 | +// gain the feature. |
| 18 | +import { expect } from "@effect/vitest"; |
| 19 | +import { Effect } from "effect"; |
| 20 | +import { composePluginApi } from "@executor-js/api/server"; |
| 21 | + |
| 22 | +import { scenario } from "../src/scenario"; |
| 23 | +import { Api, Browser, Mcp, Target } from "../src/services"; |
| 24 | +import { type McpBrowserApproval, parseBrowserApproval } from "../src/surfaces/mcp"; |
| 25 | +import type { BrowserSurface } from "../src/surfaces/browser"; |
| 26 | +import type { Identity } from "../src/target"; |
| 27 | + |
| 28 | +const coreApi = composePluginApi([] as const); |
| 29 | + |
| 30 | +// Gating a built-in read tool keeps the scenario hermetic — no external server |
| 31 | +// to host a destructive tool. The gate, not the tool, is what's under test: any |
| 32 | +// action the engine pauses on flows through the same approval path. |
| 33 | +const GATE_TOOL = "executor.coreTools.policies.list"; |
| 34 | + |
| 35 | +// The gated call returns the policy listing, which includes the policy we just |
| 36 | +// created — so the created policy's id appears in the result iff the tool |
| 37 | +// actually ran (i.e. the human approved). |
| 38 | +const GATED_CODE = ` |
| 39 | +const result = await tools.executor.coreTools.policies.list({}); |
| 40 | +return JSON.stringify(result); |
| 41 | +`; |
| 42 | + |
| 43 | +/** Open the console approval page as `identity` and click Approve or Decline. */ |
| 44 | +const decideInBrowser = ( |
| 45 | + browser: BrowserSurface, |
| 46 | + identity: Identity, |
| 47 | + approval: McpBrowserApproval, |
| 48 | + decision: "Approve" | "Decline", |
| 49 | +): Effect.Effect<void> => |
| 50 | + browser.session(identity, async ({ page, step }) => { |
| 51 | + await step( |
| 52 | + `Open the approval page and ${decision.toLowerCase()} the paused action`, |
| 53 | + async () => { |
| 54 | + await page.goto(approval.approvalUrl, { waitUntil: "networkidle" }); |
| 55 | + await page.getByRole("button", { name: decision }).click(); |
| 56 | + // The page confirms the decision was recorded ("Approve sent" / "Decline sent"). |
| 57 | + await page.getByText(`${decision} sent`).waitFor(); |
| 58 | + }, |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | +scenario( |
| 63 | + "MCP · a gated action approved in the browser runs to completion", |
| 64 | + { timeout: 180_000 }, |
| 65 | + Effect.gen(function* () { |
| 66 | + const target = yield* Target; |
| 67 | + const api = yield* Api; |
| 68 | + const browser = yield* Browser; |
| 69 | + const mcp = yield* Mcp; |
| 70 | + const identity = yield* target.newIdentity(); |
| 71 | + const client = yield* api.client(coreApi, identity); |
| 72 | + |
| 73 | + const policy = yield* client.policies.create({ |
| 74 | + payload: { owner: "org", pattern: GATE_TOOL, action: "require_approval" }, |
| 75 | + }); |
| 76 | + |
| 77 | + yield* Effect.gen(function* () { |
| 78 | + const session = mcp.session(identity, { elicitationMode: "browser" }); |
| 79 | + const tools = yield* session.listTools(); |
| 80 | + expect(tools).toContain("execute"); |
| 81 | + |
| 82 | + const paused = yield* session.call("execute", { code: GATED_CODE }); |
| 83 | + const approval = parseBrowserApproval(paused); |
| 84 | + expect(approval.approvalUrl, "approval URL targets the resume page").toContain( |
| 85 | + `/resume/${approval.executionId}`, |
| 86 | + ); |
| 87 | + |
| 88 | + // `resume` blocks for the human's decision; approve it in the browser |
| 89 | + // concurrently, then the resumed call returns the gated tool's result. |
| 90 | + const [resumed] = yield* Effect.all( |
| 91 | + [ |
| 92 | + session.awaitResume(approval.executionId), |
| 93 | + decideInBrowser(browser, identity, approval, "Approve"), |
| 94 | + ], |
| 95 | + { concurrency: "unbounded" }, |
| 96 | + ); |
| 97 | + |
| 98 | + expect(resumed.ok, "the approved execution completed without error").toBe(true); |
| 99 | + expect(resumed.text, "the gated tool ran and returned the policy listing").toContain( |
| 100 | + policy.id, |
| 101 | + ); |
| 102 | + }).pipe( |
| 103 | + Effect.ensuring( |
| 104 | + client.policies |
| 105 | + .remove({ params: { policyId: policy.id }, payload: { owner: "org" } }) |
| 106 | + .pipe(Effect.ignore), |
| 107 | + ), |
| 108 | + ); |
| 109 | + }), |
| 110 | +); |
| 111 | + |
| 112 | +scenario( |
| 113 | + "MCP · a gated action declined in the browser is blocked", |
| 114 | + { timeout: 180_000 }, |
| 115 | + Effect.gen(function* () { |
| 116 | + const target = yield* Target; |
| 117 | + const api = yield* Api; |
| 118 | + const browser = yield* Browser; |
| 119 | + const mcp = yield* Mcp; |
| 120 | + const identity = yield* target.newIdentity(); |
| 121 | + const client = yield* api.client(coreApi, identity); |
| 122 | + |
| 123 | + const policy = yield* client.policies.create({ |
| 124 | + payload: { owner: "org", pattern: GATE_TOOL, action: "require_approval" }, |
| 125 | + }); |
| 126 | + |
| 127 | + yield* Effect.gen(function* () { |
| 128 | + const session = mcp.session(identity, { elicitationMode: "browser" }); |
| 129 | + yield* session.listTools(); |
| 130 | + |
| 131 | + const paused = yield* session.call("execute", { code: GATED_CODE }); |
| 132 | + const approval = parseBrowserApproval(paused); |
| 133 | + |
| 134 | + const [resumed] = yield* Effect.all( |
| 135 | + [ |
| 136 | + session.awaitResume(approval.executionId), |
| 137 | + decideInBrowser(browser, identity, approval, "Decline"), |
| 138 | + ], |
| 139 | + { concurrency: "unbounded" }, |
| 140 | + ); |
| 141 | + |
| 142 | + // The decision propagated (resume returned rather than hanging) and the |
| 143 | + // gated tool never ran — its output (the policy id) is absent. |
| 144 | + expect(resumed.text, "the gated tool did not run after a decline").not.toContain(policy.id); |
| 145 | + }).pipe( |
| 146 | + Effect.ensuring( |
| 147 | + client.policies |
| 148 | + .remove({ params: { policyId: policy.id }, payload: { owner: "org" } }) |
| 149 | + .pipe(Effect.ignore), |
| 150 | + ), |
| 151 | + ); |
| 152 | + }), |
| 153 | +); |
0 commit comments