The Open Framework for Building Agents
fix(eve): let replies to a just-raised request steer the held turn (#4790)
roboeve[bot]71479ce42067
diff --git a/.changeset/held-request-boundary-admission.md b/.changeset/held-request-boundary-admission.mdnew file mode 100644index 000000000..3ba6772e4--- /dev/null+++ b/.changeset/held-request-boundary-admission.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Let a message sent with `turnPolicy: "queue"` right after a tool approval or sign-in appears steer the held turn again. A message that arrived while the step raising the request was still finishing was treated as sent before the request, so it stayed queued behind a turn that could not end until the person acted.diff --git a/packages/eve/src/execution/session/entry.integration.test.ts b/packages/eve/src/execution/session/entry.integration.test.tsindex 0d864fd46..9f75d0609 100644--- a/packages/eve/src/execution/session/entry.integration.test.ts+++ b/packages/eve/src/execution/session/entry.integration.test.ts@@ -1083,6 +1083,56 @@ describe("workflowEntry integration", () => {);}, 60_000);+ it("lets a queued message sent after the approval appeared steer before the step returns", async () => {+ const executions: string[] = [];+ const approvalShown = Promise.withResolvers<void>();+ const releaseStep = Promise.withResolvers<void>();+ const holdStepOpen = {+ logicalPath: "hooks/hold-step-open.ts",+ loadNamespace: async () => ({+ default: defineHook({+ events: {+ // Keeps the step that raised the approval running after it was+ // published, the window a client that reacts at once lands in.+ async "input.requested"() {+ approvalShown.resolve();+ await releaseStep.promise;+ },+ },+ }),+ }),+ };+ await withHeldApprovalRun(+ {+ agent: { name: "workflow-entry-steer-held-before-boundary" },+ auth: ALICE,+ modules: [gatedTool("approve_change", executions), holdStepOpen],+ },+ async ({ sessionInbox, stream }) => {+ await withTimeout(approvalShown.promise, "approval shown");+ await resumeHook(sessionInbox, {+ auth: ALICE,+ kind: "send",+ payload: { message: "Never mind, just say hello." },+ turnPolicy: "queue",+ });+ // Let the session accept the message before the step returns.+ await new Promise((resolve) => setTimeout(resolve, 500));+ releaseStep.resolve();++ const held = await withTimeout(stream.nextTurn(), "approval hold");+ const request = filterEventsByType(held, "input.requested")[0]?.data.requests[0];+ expect(held.at(-1)?.type).toBe("turn.waiting");+ const steered = await withTimeout(stream.nextTurn(), "steered turn");+ expect(filterEventsByType(steered, "input.resolved")).toMatchObject([+ { data: { resolutions: [{ outcome: "ignored", requestId: request!.requestId }] } },+ ]);+ expect(steered.at(-1)?.type).toBe("session.waiting");+ expect(executions).toEqual([]);+ },+ );+ }, 60_000);+// https://github.com/vercel/eve/issues/786it("keeps a queued message sent before an approval queued instead of answering it", async () => {const executions: string[] = [];diff --git a/packages/eve/src/execution/session/turn.ts b/packages/eve/src/execution/session/turn.tsindex 9641f15a1..49e55671f 100644--- a/packages/eve/src/execution/session/turn.ts+++ b/packages/eve/src/execution/session/turn.ts@@ -192,6 +192,10 @@ export class SessionExecution {result.action === "park" ? result.pendingCoordinationCallIds : undefined;const turnCompleted = result.action === "park" && result.settled !== undefined;+ // Deliveries the boundary admits next arrived while the step ran. A step+ // that raises a request publishes it before it returns, so they may+ // reply to it.+ const stepAdmission = this.input.queue.nextAdmission;await turn.admitBoundary();await this.handleBoundaryMessages(turn.takeBoundaryMessages(), result.action === "done");@@ -218,7 +222,7 @@ export class SessionExecution {) {throw new Error(NO_INPUT_CAPABILITY_ERROR_MESSAGE);}- const woke = await this.waitForHeldRequest(turn, result);+ const woke = await this.waitForHeldRequest(turn, result, stepAdmission);if (woke === "cancelled") return await this.finishCancelledTurn(turn);nextStepInput = { delivery: woke };continue;@@ -353,8 +357,9 @@ export class SessionExecution {* delivery its next step reads: the sign-in callback, an approval answer* from any responder, or a message from the turn's own person, which steers* the turn and cancels the request. Anyone else's message queues behind it,- * as does a queued message admitted before the hold: it was sent before the- * request existed, so it must not answer it.+ * as does a queued message admitted before the step that raised the request+ * (`heldSince`): it was sent before the request existed, so it must not+ * answer it.*/private async waitForHeldRequest(turn: ActiveTurn,@@ -362,10 +367,10 @@ export class SessionExecution {readonly authorizationAttemptIds: readonly string[];readonly inputRequestIds: readonly string[];},+ heldSince: number,): Promise<DeliverHookPayload | "cancelled"> {const attemptIds = new Set(held.authorizationAttemptIds);const requestIds = new Set(held.inputRequestIds);- const heldSince = this.input.queue.nextAdmission;while (true) {const callbacks = this.input.queue.takeAuthorizations(attemptIds);if (callbacks !== undefined) return { kind: "deliver", payloads: callbacks };