|
| 1 | +// Cross-target: the Run/Test panel's backend (`POST /executions`) auto-approves |
| 2 | +// approval-gated tools when the operator invokes them, because clicking Run in |
| 3 | +// the panel IS the human approval. |
| 4 | +// |
| 5 | +// The panel sends `autoApprove: true`. Here we drive the same HTTP endpoint the |
| 6 | +// panel uses and prove both halves of the contract against ONE tool that gates |
| 7 | +// itself: the `policies.create` core tool carries a `requiresApproval` |
| 8 | +// annotation, so with no matching policy in play the annotation is the only |
| 9 | +// thing that can pause the call. |
| 10 | +// |
| 11 | +// - without `autoApprove`: the call pauses (the panel would have dead-ended |
| 12 | +// on "This tool requires approval"), and the policy is not written. |
| 13 | +// - with `autoApprove`: the call runs to completion and the policy is written. |
| 14 | +// |
| 15 | +// The created policy is a `block` rule on a unique, non-matching pattern, so a |
| 16 | +// leak cannot gate another scenario's tools; it is removed in an `ensuring` |
| 17 | +// finalizer regardless. |
| 18 | +import { randomUUID } from "node:crypto"; |
| 19 | + |
| 20 | +import { expect } from "@effect/vitest"; |
| 21 | +import { Effect } from "effect"; |
| 22 | +import { composePluginApi } from "@executor-js/api/server"; |
| 23 | + |
| 24 | +import { scenario } from "../src/scenario"; |
| 25 | +import { Api, Target } from "../src/services"; |
| 26 | + |
| 27 | +const coreApi = composePluginApi([] as const); |
| 28 | + |
| 29 | +/** Sandbox code that creates a policy through the approval-gated core tool. The |
| 30 | + * pattern is unique-per-run and matches no real tool, so the rule is inert. */ |
| 31 | +const createPolicyCode = (pattern: string) => ` |
| 32 | +return await tools.executor.coreTools.policies.create({ |
| 33 | + owner: "user", |
| 34 | + pattern: ${JSON.stringify(pattern)}, |
| 35 | + action: "block", |
| 36 | +}); |
| 37 | +`; |
| 38 | + |
| 39 | +scenario( |
| 40 | + "Run panel · autoApprove runs an approval-gated tool that otherwise pauses", |
| 41 | + {}, |
| 42 | + Effect.gen(function* () { |
| 43 | + const target = yield* Target; |
| 44 | + const apiSurface = yield* Api; |
| 45 | + const identity = yield* target.newIdentity(); |
| 46 | + const client = yield* apiSurface.client(coreApi, identity); |
| 47 | + const pattern = `run-auto-approve-${randomUUID().slice(0, 8)}.*`; |
| 48 | + const code = createPolicyCode(pattern); |
| 49 | + |
| 50 | + const cleanup = client.policies.list().pipe( |
| 51 | + Effect.flatMap((list) => |
| 52 | + Effect.forEach( |
| 53 | + list.filter((p) => p.pattern === pattern), |
| 54 | + (p) => |
| 55 | + client.policies |
| 56 | + .remove({ params: { policyId: p.id }, payload: { owner: "user" } }) |
| 57 | + .pipe(Effect.ignore), |
| 58 | + ), |
| 59 | + ), |
| 60 | + Effect.ignore, |
| 61 | + ); |
| 62 | + |
| 63 | + yield* Effect.gen(function* () { |
| 64 | + // Baseline: without autoApprove the gated tool pauses (the panel's old |
| 65 | + // dead-end), and the side effect must not have happened. |
| 66 | + const gated = yield* client.executions.execute({ payload: { code } }); |
| 67 | + expect(gated.status, "a gated tool pauses without autoApprove").toBe("paused"); |
| 68 | + |
| 69 | + const beforeApproval = yield* client.policies.list(); |
| 70 | + expect( |
| 71 | + beforeApproval.some((p) => p.pattern === pattern), |
| 72 | + "the policy is not written while the call is paused for approval", |
| 73 | + ).toBe(false); |
| 74 | + |
| 75 | + // Release the paused fiber so it does not linger waiting on a response. |
| 76 | + if (gated.status === "paused") { |
| 77 | + const executionId = (gated.structured as { readonly executionId?: string }).executionId; |
| 78 | + if (executionId) { |
| 79 | + yield* client.executions |
| 80 | + .resume({ params: { executionId }, payload: { action: "cancel" } }) |
| 81 | + .pipe(Effect.ignore); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // With autoApprove the operator IS the approver: the same call runs to |
| 86 | + // completion and the side effect lands. |
| 87 | + const approved = yield* client.executions.execute({ |
| 88 | + payload: { code, autoApprove: true }, |
| 89 | + }); |
| 90 | + expect(approved.status, "autoApprove runs the gated tool to completion").toBe("completed"); |
| 91 | + if (approved.status !== "completed") return; // narrowing only |
| 92 | + expect(approved.isError, "the auto-approved run is not an error").toBe(false); |
| 93 | + |
| 94 | + const afterApproval = yield* client.policies.list(); |
| 95 | + expect( |
| 96 | + afterApproval.some((p) => p.pattern === pattern), |
| 97 | + "the policy is written once autoApprove runs the gated tool", |
| 98 | + ).toBe(true); |
| 99 | + }).pipe(Effect.ensuring(cleanup)); |
| 100 | + }), |
| 101 | +); |
0 commit comments