The Open Framework for Building Agents
fix(eve): approvals - keep pending prompts independent (#2923)
Andrew Barba884cba8279ad
diff --git a/.changeset/safe-pending-approval-grants.md b/.changeset/safe-pending-approval-grants.mdnew file mode 100644index 000000000..ed9e1f6a4--- /dev/null+++ b/.changeset/safe-pending-approval-grants.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Prevent approving one tool call from auto-authorizing other already-pending calls with the same approval key. Existing approval prompts now remain independent, while `once()` still auto-allows calls proposed after the pending requests are resolved.diff --git a/docs/tools/human-in-the-loop.md b/docs/tools/human-in-the-loop.mdindex c2876b4a5..ad4dd0792 100644--- a/docs/tools/human-in-the-loop.md+++ b/docs/tools/human-in-the-loop.md@@ -38,6 +38,8 @@ export default defineTool({By default, omitted `approval` behaves like `never()`, so tool calls may execute without human approval. Require human approval or other safeguards for sensitive, irreversible, regulated, financial, healthcare, employment, housing, legal, safety-impacting, user-impacting, or external side-effecting actions.+A reusable approval grant applies only after every matching request that is already pending has been resolved. If several calls to a `once()`-gated tool have each produced an approval prompt, approving one does not authorize the others; each visible prompt remains an independent decision. After those pending requests are resolved, later calls in the session are allowed automatically.+When the decision depends on the input, pass your own policy instead of a helper. It receives the same session context as tool execution, plus `{ toolName, toolInput, approvedTools, callId }`, and returns an AI SDK 7 approval status synchronously or as a promise. Use `ctx.session.auth.current` to guard by the caller of the current turn and `ctx.session.auth.initiator` to guard by the caller that created the session. Return `"user-approval"` to pause for a person or `"not-applicable"` to continue without a prompt. `toolInput` can be undefined, so guard the access. This policy denies cross-tenant calls, then requires approval only when an amount crosses a threshold:```tsdiff --git a/packages/eve/src/harness/hitl/approval-input-requests.ts b/packages/eve/src/harness/hitl/approval-input-requests.tsindex c7fd76579..ccfa397ef 100644--- a/packages/eve/src/harness/hitl/approval-input-requests.ts+++ b/packages/eve/src/harness/hitl/approval-input-requests.ts@@ -10,6 +10,7 @@ import {import { isApprovalRequest } from "#harness/input-request-class.js";import type { PendingInputBatch } from "#harness/pending-input-batches.js";import {+ getPendingInputBatches,queueDeferredStepInput,removePendingInputBatches,} from "#harness/pending-input-batches.js";@@ -139,8 +140,22 @@ export function resolveApprovalInputBatches(});}-/** Returns tool approval keys recorded during this session. */-export function getApprovedTools(session: HarnessSession): ReadonlySet<string> {+/** Returns recorded approval keys that have no matching request still pending. */+export function getApprovedTools(+ session: HarnessSession,+ resolveApprovalKey?: (request: InputRequest) => string | undefined,+): ReadonlySet<string> {+ const approvedTools = readRecordedApprovedTools(session);+ for (const batch of getPendingInputBatches(session.state)) {+ for (const request of batch.requests) {+ if (!isApprovalRequest(request)) continue;+ approvedTools.delete(resolveApprovalKey?.(request) ?? request.action.toolName);+ }+ }+ return approvedTools;+}++function readRecordedApprovedTools(session: HarnessSession): Set<string> {const value = session.state?.[APPROVED_TOOLS_KEY];return Array.isArray(value) ? new Set(value as string[]) : new Set();}@@ -191,7 +206,9 @@ function recordApprovedTools(input: {if (newKeys.length === 0) return input.session;const state = { ...input.session.state };- state[APPROVED_TOOLS_KEY] = [...new Set([...getApprovedTools(input.session), ...newKeys])];+ state[APPROVED_TOOLS_KEY] = [+ ...new Set([...readRecordedApprovedTools(input.session), ...newKeys]),+ ];return { ...input.session, state };}diff --git a/packages/eve/src/harness/input-requests.test.ts b/packages/eve/src/harness/input-requests.test.tsindex a576a6a52..e703e5294 100644--- a/packages/eve/src/harness/input-requests.test.ts+++ b/packages/eve/src/harness/input-requests.test.ts@@ -1155,6 +1155,99 @@ describe("pending input batch collection", () => {expect(hasPendingInputBatch(second.session.state)).toBe(false);});+ it("does not apply an approval grant to already-pending calls with the same key", () => {+ let session = createHarnessSession();+ for (const index of [1, 2, 3]) {+ session = appendPendingInputBatch({+ requests: [approvalRequest(`approval-${index}`, `call-${index}`)],+ responseMessages: [batchOutput(`call-${index}`, "bash")],+ session,+ });+ }++ const first = resolvePendingInput({+ session,+ stepInput: { inputResponses: [{ requestId: "approval-1", optionId: "approve" }] },+ });++ expect(first.outcome).toBe("resolved");+ expect(getApprovedTools(first.session)).toEqual(new Set());+ expect(getPendingInputRequestIds(first.session.state)).toEqual(+ new Set(["approval-2", "approval-3"]),+ );++ const second = resolvePendingInput({+ session: first.session,+ stepInput: { inputResponses: [{ requestId: "approval-2", optionId: "cancel" }] },+ });+ expect(getApprovedTools(second.session)).toEqual(new Set());+ expect(getPendingInputRequestIds(second.session.state)).toEqual(new Set(["approval-3"]));++ const third = resolvePendingInput({+ session: second.session,+ stepInput: { inputResponses: [{ requestId: "approval-3", optionId: "cancel" }] },+ });+ expect(getApprovedTools(third.session)).toEqual(new Set(["bash"]));+ expect(hasPendingInputBatch(third.session.state)).toBe(false);+ });++ it("keeps approval grants independent across compound keys", () => {+ const scopedApproval = (+ requestId: string,+ callId: string,+ workspace: string,+ ): InputRequest => ({+ ...approvalRequest(requestId, callId),+ action: {+ callId,+ input: { workspace },+ kind: "tool-call",+ toolName: "notion__notion-update-page",+ },+ });+ const resolveApprovalKey = (request: InputRequest) =>+ `${request.action.toolName}:${String(request.action.input.workspace)}`;+ let session = appendPendingInputBatch({+ requests: [scopedApproval("approval-1", "call-1", "workspace-a")],+ responseMessages: [batchOutput("call-1", "notion__notion-update-page")],+ session: createHarnessSession(),+ });+ session = appendPendingInputBatch({+ requests: [scopedApproval("approval-2", "call-2", "workspace-b")],+ responseMessages: [batchOutput("call-2", "notion__notion-update-page")],+ session,+ });+ session = appendPendingInputBatch({+ requests: [scopedApproval("approval-3", "call-3", "workspace-a")],+ responseMessages: [batchOutput("call-3", "notion__notion-update-page")],+ session,+ });++ const first = resolvePendingInput({+ resolveApprovalKey,+ session,+ stepInput: { inputResponses: [{ requestId: "approval-1", optionId: "approve" }] },+ });++ // approval-3 shares the workspace-a key, so the grant stays masked; the+ // bare tool name would not mask it, which is what makes the resolver matter.+ expect(getApprovedTools(first.session, resolveApprovalKey)).toEqual(new Set());+ expect(getPendingInputRequestIds(first.session.state)).toEqual(+ new Set(["approval-2", "approval-3"]),+ );++ const second = resolvePendingInput({+ resolveApprovalKey,+ session: first.session,+ stepInput: { inputResponses: [{ requestId: "approval-3", optionId: "cancel" }] },+ });++ expect(getApprovedTools(second.session, resolveApprovalKey)).toEqual(+ new Set(["notion__notion-update-page:workspace-a"]),+ );+ expect(getPendingInputRequestIds(second.session.state)).toEqual(new Set(["approval-2"]));+ });+it("leaves every batch open when a message arrives with several batches pending", () => {let session = appendPendingInputBatch({requests: [approvalRequest("approval-1", "call-1")],diff --git a/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts b/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.tsindex 625c1eb38..05d6838b1 100644--- a/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts+++ b/packages/eve/src/harness/tool-loop-generate-approval-resume.integration.test.ts@@ -16,11 +16,14 @@ import {TurnTaskStateKey,} from "#context/keys.js";import { setHarnessEmissionState } from "#harness/emission.js";+import type { HarnessToolDefinition } from "#harness/execute-tool.js";import type { InputRequest } from "#shared/input.js";import { appendPendingInputBatch } from "#harness/input-requests.js";+import { getPendingInputBatches } from "#harness/pending-input-batches.js";import { createToolLoopHarness } from "#harness/tool-loop.js";import { setTurnUsageState } from "#harness/turn-tag-state.js";import type { HarnessSession, ToolLoopHarnessConfig } from "#harness/types.js";+import { once } from "#tools/approval/policies.js";import { defineTool } from "#tools/definition.js";import { stampDurableDynamicCallback } from "#tools/durable-callbacks.js";@@ -59,6 +62,26 @@ function textStreamResult(text: string): StreamResult {};}+function toolCallStreamResult(call: {+ readonly input: string;+ readonly toolCallId: string;+ readonly toolName: string;+}): StreamResult {+ return {+ stream: simulateReadableStream({+ chunks: [+ { type: "stream-start", warnings: [] },+ { ...call, type: "tool-call" },+ {+ finishReason: { raw: undefined, unified: "tool-calls" },+ type: "finish",+ usage,+ },+ ] satisfies StreamPart[],+ }),+ };+}+const toolCall = {input: { command: "pwd" },toolCallId: "call-1",@@ -173,6 +196,24 @@ function createTwoPendingApprovalSession(): HarnessSession {});}+function createApprovalContext(): ContextContainer {+ const responder = {+ attributes: {},+ authenticator: "test",+ issuer: "test",+ principalId: "user-1",+ principalType: "user" as const,+ };+ const ctx = new ContextContainer();+ ctx.set(AuthKey, responder);+ ctx.set(SessionKey, {+ auth: { current: responder, initiator: null },+ sessionId: "generate-approval-resume-session",+ turn: { id: "turn-1", sequence: 1 },+ });+ return ctx;+}+function createModel(): MockLanguageModelV4 {return new MockLanguageModelV4({doGenerate: {@@ -189,11 +230,13 @@ function createModel(): MockLanguageModelV4 {function createConfig(model: MockLanguageModelV4,execute: (input: unknown, options: unknown) => Promise<string>,+ approval?: HarnessToolDefinition["approval"],): ToolLoopHarnessConfig {const tools: ToolLoopHarnessConfig["tools"] = new Map([[toolCall.toolName,{+ approval,description: "Run a shell command.",execute,inputSchema: jsonSchema({ type: "object" }),@@ -503,6 +546,112 @@ describe("tool loop generate approval resume (real AI SDK)", () => {expect(toolResultCallIds).toEqual([toolCall.toolCallId, secondToolCall.toolCallId]);});+ // Regression: approving one once()-gated call recorded the grant immediately,+ // so a new same-tool call proposed on the resume step executed without a+ // prompt while the other parked card was still visible to the user.+ it("keeps prompting a once() tool while a sibling approval is still pending", async () => {+ const thirdToolCall = {+ input: JSON.stringify({ command: "ls" }),+ toolCallId: "call-3",+ toolName: "bash",+ };+ const execute = vi.fn(async () => "/workspace");+ const model = new MockLanguageModelV4({+ doStream: async () => toolCallStreamResult(thirdToolCall),+ modelId: "generate-approval-resume-model",+ provider: "eve-integration-mock",+ });+ const requested: InputRequest[] = [];+ const config = {+ ...createConfig(model, execute, once()),+ handleEvent: async (event) => {+ if (event.type === "input.requested") requested.push(...event.data.requests);+ },+ } satisfies ToolLoopHarnessConfig;++ const result = await contextStorage.run(createApprovalContext(), () =>+ createToolLoopHarness(config)(createTwoPendingApprovalSession(), {+ inputResponses: [{ optionId: "approve", requestId: approvalRequest.approvalId }],+ }),+ );++ expect(execute).toHaveBeenCalledExactlyOnceWith(+ toolCall.input,+ expect.objectContaining({ toolCallId: toolCall.toolCallId }),+ );+ expect(requested.map((request) => request.action.callId)).toEqual([thirdToolCall.toolCallId]);+ expect(result.next).toBeNull();+ const pendingCallIds = getPendingInputBatches(result.session.state).flatMap((batch) =>+ batch.requests.map((request) => request.action.callId),+ );+ expect(pendingCallIds).toEqual([secondToolCall.toolCallId, thirdToolCall.toolCallId]);+ });++ it("auto-allows a once() tool after every pending approval with its key is settled", async () => {+ const thirdToolCall = {+ input: JSON.stringify({ command: "ls" }),+ toolCallId: "call-3",+ toolName: "bash",+ };+ const execute = vi.fn(async () => "/workspace");+ // Batches settle one per step, so the model is called between them. It+ // proposes the third call only once both pending prompts are gone.+ const responses = [+ textStreamResult("Ran pwd."),+ toolCallStreamResult(thirdToolCall),+ textStreamResult("All done."),+ ];+ const model = new MockLanguageModelV4({+ doStream: async () => {+ const next = responses.shift();+ if (next === undefined) throw new Error("Unexpected extra model call.");+ return next;+ },+ modelId: "generate-approval-resume-model",+ provider: "eve-integration-mock",+ });+ const requested: InputRequest[] = [];+ const config = {+ ...createConfig(model, execute, once()),+ handleEvent: async (event) => {+ if (event.type === "input.requested") requested.push(...event.data.requests);+ },+ } satisfies ToolLoopHarnessConfig;+ const ctx = createApprovalContext();+ const runStep = createToolLoopHarness(config);++ const first = await contextStorage.run(ctx, () =>+ runStep(createTwoPendingApprovalSession(), {+ inputResponses: [+ { optionId: "approve", requestId: approvalRequest.approvalId },+ { optionId: "cancel", requestId: secondApprovalRequest.approvalId },+ ],+ }),+ );+ // The second batch's denial replays on a deferred step; drain until the turn settles.+ if (typeof first.next !== "function") {+ throw new TypeError("Expected the deferred approval response to schedule another step.");+ }+ let result = first;+ while (typeof result.next === "function") {+ const { next, session } = result;+ result = await contextStorage.run(ctx, () => next(session));+ }++ expect(requested).toEqual([]);+ expect(execute).toHaveBeenCalledTimes(2);+ expect(execute).toHaveBeenNthCalledWith(+ 2,+ { command: "ls" },+ expect.objectContaining({ toolCallId: thirdToolCall.toolCallId }),+ );+ expect(responses).toEqual([]);+ expect(result.session.history.at(-1)).toMatchObject({+ content: [{ text: "All done.", type: "text" }],+ role: "assistant",+ });+ });+it("executes an approval before replaying its message after a limit continuation", async () => {const execute = vi.fn(async () => "/workspace");const model = new MockLanguageModelV4({diff --git a/packages/eve/src/harness/tool-loop.ts b/packages/eve/src/harness/tool-loop.tsindex f087c810d..085492edc 100644--- a/packages/eve/src/harness/tool-loop.ts+++ b/packages/eve/src/harness/tool-loop.ts@@ -458,6 +458,7 @@ function hasDispatchTarget(tools: HarnessToolMap, kind: PreparedDispatchTarget["export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {const baseEmit = config.handleEvent;+ const resolveApprovalKey = resolveApprovalKeyFromTools(config.tools);async function runStep(initialSession: Readonly<Parameters<StepFn>[0]>,@@ -819,7 +820,7 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {const pending = resolvePendingInput({deferMessagesWhileApprovalsPending: config.mode !== "conversation",history: resolvedRuntimeActions.messages,- resolveApprovalKey: resolveApprovalKeyFromTools(config.tools),+ resolveApprovalKey,session,stepInput: coordinated.stepInput,});@@ -1145,7 +1146,7 @@ export function createToolLoopHarness(config: ToolLoopHarnessConfig): StepFn {projectedMessages,);}- const approvedTools = getApprovedTools(session);+ const approvedTools = getApprovedTools(session, resolveApprovalKey);const isFirstTurn = emissionState.sequence === 0;const hasScheduleProvenance = isFirstTurn && ctx?.get(ScheduleIdKey) !== undefined;