Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): keep queued messages sent before a held request from answering it (#4749)

roboeve[bot]0dea0ded869b
diff --git a/.changeset/queued-message-before-approval.md b/.changeset/queued-message-before-approval.md
new file mode 100644
index 000000000..ab4847a5b
--- /dev/null
+++ b/.changeset/queued-message-before-approval.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Keep a message sent with `turnPolicy: "queue"` before a tool approval or sign-in was requested in the queue, so it can no longer answer a prompt the person had not seen. Messages sent after the prompt still steer the held turn.
diff --git a/packages/eve/src/execution/session/entry.integration.test.ts b/packages/eve/src/execution/session/entry.integration.test.ts
index 4d6eb57cc..0d864fd46 100644
--- a/packages/eve/src/execution/session/entry.integration.test.ts
+++ b/packages/eve/src/execution/session/entry.integration.test.ts
@@ -1083,6 +1083,86 @@ describe("workflowEntry integration", () => {
);
}, 60_000);
+ // https://github.com/vercel/eve/issues/786
+ it("keeps a queued message sent before an approval queued instead of answering it", async () => {
+ const executions: string[] = [];
+ const lookupStarted = Promise.withResolvers<void>();
+ const releaseLookup = Promise.withResolvers<void>();
+ const lookup = {
+ loadNamespace: async () => ({
+ default: defineTool({
+ description: "Look up the change to apply.",
+ execute: async () => {
+ lookupStarted.resolve();
+ await releaseLookup.promise;
+ return { stepKey: "change-1" };
+ },
+ inputSchema: {},
+ }),
+ }),
+ logicalPath: "tools/lookup_change.ts",
+ };
+ await withHeldApprovalRun(
+ {
+ agent: { name: "workflow-entry-stale-queued-approval" },
+ auth: ALICE,
+ message: "Use lookup_change, then approve_change.",
+ modules: [
+ lookup,
+ gatedTool("approve_change", executions, {
+ request: always(),
+ response: async () => ({ status: "allowed" as const }),
+ }),
+ ],
+ },
+ async ({ commandInbox, sessionInbox, stream }) => {
+ await withTimeout(lookupStarted.promise, "lookup started");
+ // Alice types while the turn runs, before any approval exists.
+ await resumeHook(sessionInbox, {
+ auth: ALICE,
+ kind: "send",
+ payload: { message: "approve" },
+ turnPolicy: "queue",
+ });
+ // Let the session admit the message before the step ends.
+ await new Promise((resolve) => setTimeout(resolve, 500));
+ releaseLookup.resolve();
+
+ const held = await withTimeout(stream.nextTurn(), "approval hold");
+ const request = filterEventsByType(held, "input.requested")[0]?.data.requests[0];
+ expect(request?.action.toolName).toBe("approve_change");
+ expect(held.at(-1)).toMatchObject({ data: { on: "input" }, type: "turn.waiting" });
+ expect(filterEventsByType(held, "approval.settled")).toHaveLength(0);
+ expect(executions).toEqual([]);
+
+ // Bob approves, so the settlement shows who answered.
+ await resumeHook(commandInbox, {
+ auth: BOB,
+ kind: "send",
+ payload: { inputResponses: [{ optionId: "approve", requestId: request!.requestId }] },
+ });
+ // The approval settles first; the queued message then runs as an ordinary message.
+ const answered = await withTimeout(
+ stream.nextUntil((event) => event.type === "message.received"),
+ "queued message",
+ );
+ expect(filterEventsByType(answered, "approval.settled")).toMatchObject([
+ {
+ data: {
+ outcome: "approved",
+ requestId: request!.requestId,
+ responderPrincipalId: expect.stringContaining("bob"),
+ },
+ },
+ ]);
+ expect(filterEventsByType(answered, "message.received").at(-1)?.data.message).toBe(
+ "approve",
+ );
+ expect(executions).toEqual(["approve_change"]);
+ },
+ );
+ }, 60_000);
+
it("runs a steering message after the budget prompt that followed its cancelled approval", async () => {
await withHeldApprovalRun(
{
diff --git a/packages/eve/src/execution/session/input-queue.test.ts b/packages/eve/src/execution/session/input-queue.test.ts
index b6e259e67..e5e690942 100644
--- a/packages/eve/src/execution/session/input-queue.test.ts
+++ b/packages/eve/src/execution/session/input-queue.test.ts
@@ -27,4 +27,24 @@ describe("SessionInputQueue", () => {
]);
expect(queue.takeAuthorizations(held)).toBeUndefined();
});
+
+ it("lets a queued message steer a held turn only when it arrived after the hold", () => {
+ const queue = new SessionInputQueue();
+ const turn = { callerCallId: undefined, principal: "user:test:alice" };
+ const send = (message: string) =>
+ queue.enqueueDelivery({
+ kind: "deliver",
+ payloads: [{ message }],
+ turnPolicy: "queue",
+ }).sequence;
+
+ const before = send("approve");
+ const heldSince = queue.nextAdmission;
+ const after = send("never mind");
+ const admitted = new Set([before, after]);
+
+ expect(queue.takeSteering(admitted, turn)).toBeUndefined();
+ expect(queue.takeSteering(admitted, turn, { heldSince })?.sequences).toEqual([after]);
+ expect(queue.takeNext()).toMatchObject({ sequences: [before] });
+ });
});
diff --git a/packages/eve/src/execution/session/input-queue.ts b/packages/eve/src/execution/session/input-queue.ts
index 17440ae6a..6d5d75ecd 100644
--- a/packages/eve/src/execution/session/input-queue.ts
+++ b/packages/eve/src/execution/session/input-queue.ts
@@ -58,6 +58,11 @@ export class SessionInputQueue {
return this.entries.length;
}
+ /** The sequence the next admission receives; every earlier admission is below it. */
+ get nextAdmission(): number {
+ return this.nextSequence;
+ }
+
enqueueDelivery(delivery: DeliverHookPayload): DeliveryAdmission {
const admission = { delivery, sequence: this.nextSequence++ };
this.entries.push({ ...admission, kind: "delivery" });
@@ -136,7 +141,9 @@ export class SessionInputQueue {
(entry): entry is QueuedDelivery =>
entry.kind === "delivery" &&
admitted.has(entry.sequence) &&
- isSteeringDelivery(entry.delivery, turn, options),
+ isSteeringDelivery(entry.delivery, turn, {
+ heldOnPerson: options?.heldSince !== undefined && entry.sequence >= options.heldSince,
+ }),
);
if (steering.length === 0) return undefined;
this.retain((entry) => entry.kind !== "delivery" || !steering.includes(entry));
@@ -219,22 +226,25 @@ export interface SteeringTurn {
export interface SteeringOptions {
/**
- * The turn waits on its own person for a sign-in or approval. Their message
- * steers even with `turnPolicy: "queue"`: queued, it would wait for a turn
- * that cannot end until they act.
+ * The turn waits on its own person for a sign-in or approval, held since
+ * this admission sequence. Their later messages steer even with
+ * `turnPolicy: "queue"`: queued, they would wait for a turn that cannot end
+ * until they act. Earlier ones were sent before the request existed, so
+ * they stay queued and never answer it.
*/
- readonly heldOnPerson?: boolean;
+ readonly heldSince?: number;
}
/**
* Only the turn's own principal steers it; another principal's message waits
* for the turn to end. A delivery without auth acts as the session's current
- * identity, which is the turn's.
+ * identity, which is the turn's. `heldOnPerson` lets a queued message steer a
+ * turn that waits on its person.
*/
export function isSteeringDelivery(
delivery: DeliverHookPayload,
turn: SteeringTurn,
- options?: SteeringOptions,
+ options?: { readonly heldOnPerson?: boolean },
): boolean {
return (
(options?.heldOnPerson === true || (delivery.turnPolicy ?? "steer") === "steer") &&
diff --git a/packages/eve/src/execution/session/turn.ts b/packages/eve/src/execution/session/turn.ts
index f77b69c01..9641f15a1 100644
--- a/packages/eve/src/execution/session/turn.ts
+++ b/packages/eve/src/execution/session/turn.ts
@@ -352,7 +352,9 @@ export class SessionExecution {
* The turn waits on a sign-in or tool approval it raised. It wakes with the
* 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.
+ * 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.
*/
private async waitForHeldRequest(
turn: ActiveTurn,
@@ -363,12 +365,13 @@ export class SessionExecution {
): 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 };
const answer = await turn.takeInputResponses(requestIds);
if (answer !== undefined) return answer;
- const steering = await turn.takeSteering({ heldOnPerson: true });
+ const steering = await turn.takeSteering({ heldSince });
if (steering !== undefined) return steering;
const next = await turn.nextRuntimeEvent([]);
if (next === "cancelled") return next;