Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

Fix stale approval restored after cancelled denial (#3415)

roboeve[bot]20c1317b388f
diff --git a/.changeset/fix-denied-approval-cancellation.md b/.changeset/fix-denied-approval-cancellation.md
new file mode 100644
index 000000000..3f03c1262
--- /dev/null
+++ b/.changeset/fix-denied-approval-cancellation.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Keep a tool approval answered before a cancelled response resolved, instead of restoring it and resolving it again on the next message. An approved call the cancellation interrupts while it runs gets an interrupted result and is not run again, and a message sent with the answer is kept once.
diff --git a/packages/eve/src/execution/cancelled-model-call-batch.ts b/packages/eve/src/execution/cancelled-model-call-batch.ts
index 6084bf13d..c155364ce 100644
--- a/packages/eve/src/execution/cancelled-model-call-batch.ts
+++ b/packages/eve/src/execution/cancelled-model-call-batch.ts
@@ -6,7 +6,8 @@ import { preserveCancelledTurnMessage } from "#execution/cancelled-turn-message.
import { createDurableSessionValues } from "#execution/durable-session-store.js";
import type { DurableStepResult } from "#execution/session/turn-step-types.js";
import type { HarnessSession, StepInput, StepResult } from "#harness/types.js";
-import { saveSessionProjection } from "#harness/session-machine/current.js";
+import { appliedSession, saveSessionProjection } from "#harness/session-machine/current.js";
+import { takeDeferredMessage } from "#harness/hitl/index.js";
import { preserveSerializedInstrumentationState } from "#instrumentation/state.js";
import { preserveSerializedAgentTraceState } from "#tracing/agent-trace-context-store.js";
@@ -15,6 +16,28 @@ export interface CompletedModelCallCheckpoint {
readonly serializedContext: Record<string, unknown>;
}
+/**
+ * The session the cut call applied, with the delivery's message kept in history. The applied queue
+ * already reflects what the call consumed, so answers it resolved don't come back. A message it
+ * deferred behind those answers moves to history, as an ordinary cancelled turn's message does,
+ * and leaves the queue so it doesn't reach the model again; the rest of the queue stays.
+ */
+async function cancelledWithoutCheckpoint(
+ ctx: ContextContainer,
+ initial: HarnessSession,
+ stepInput: StepInput | undefined,
+): Promise<HarnessSession> {
+ const applied = appliedSession<HarnessSession>(ctx);
+ let session = applied ?? initial;
+ let preserved = stepInput;
+ if (applied !== undefined) {
+ const deferred = takeDeferredMessage(applied);
+ session = deferred.session;
+ if (deferred.message !== undefined) preserved = { ...stepInput, message: deferred.message };
+ }
+ return await contextStorage.run(ctx, () => preserveCancelledTurnMessage(session, preserved));
+}
+
/** Builds the successful step result that commits a cancelled batch's completed model calls. */
export async function createCancelledModelCallBatchResult(input: {
readonly beforeBatchContext: Record<string, unknown>;
@@ -24,13 +47,11 @@ export async function createCancelledModelCallBatchResult(input: {
readonly stepInput: StepInput | undefined;
}): Promise<DurableStepResult> {
const interruptedContext = serializeContext(input.ctx);
- const checkpointSession = input.checkpoint?.result.session ?? input.initialSession;
+ // Without a completed call, the batch keeps what its last call applied before the cut, such as
+ // answers it resolved and published, so the session never rolls back past its own events.
const cancelledSession =
- input.checkpoint === undefined
- ? await contextStorage.run(input.ctx, () =>
- preserveCancelledTurnMessage(checkpointSession, input.stepInput),
- )
- : checkpointSession;
+ input.checkpoint?.result.session ??
+ (await cancelledWithoutCheckpoint(input.ctx, input.initialSession, input.stepInput));
// The stream keeps every event the cancelled batch published, so the projection does too.
const checkpointContext = {
...(input.checkpoint?.serializedContext ?? input.beforeBatchContext),
diff --git a/packages/eve/src/execution/session/turn-step.test.ts b/packages/eve/src/execution/session/turn-step.test.ts
index 692bf8962..0ef2a8511 100644
--- a/packages/eve/src/execution/session/turn-step.test.ts
+++ b/packages/eve/src/execution/session/turn-step.test.ts
@@ -1,5 +1,6 @@
import { createTestSessionState } from "#internal/testing/session-state.js";
-import type { ModelMessage } from "ai";
+import { jsonSchema, type ModelMessage } from "ai";
+import { MockLanguageModelV4 } from "ai/test";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ChannelAdapter, ChannelAdapterContext } from "#channel/adapter.js";
import type {
@@ -33,9 +34,16 @@ import { BundleKey, ChannelKey } from "#runtime/sessions/runtime-context-keys.js
import { deserializeContext, serializeContext } from "#context/serialize.js";
import { startWorkflowTask } from "#execution/tools/workflow/start.js";
import { TurnCancelledError } from "#harness/turn-cancellation.js";
+import { createToolLoopHarness } from "#harness/tool-loop.js";
+import { queuedInput, storedProjection } from "#harness/session-machine/view.js";
+import { APPROVED_CALL_INTERRUPTED_MESSAGE } from "#harness/hitl/approved-calls.js";
+import { textStreamResult } from "#internal/testing/approval-resume.js";
+import { openInputs } from "#protocol/session-projection.js";
+import type { InputRequest } from "#shared/input.js";
import { getPendingAuthorization, setPendingAuthorization } from "#harness/authorization.js";
import { upsertProxyInputRequests } from "#harness/proxy-input-requests.js";
import {
+ parkedSteps,
positionOf,
positionState,
withOpenTurn,
@@ -43,7 +51,7 @@ import {
withPublished,
withQueuedInput,
} from "#internal/testing/session-machine.js";
-import type { HarnessSession, StepFn, StepResult } from "#harness/types.js";
+import type { HarnessSession, StepFn, StepInput, StepResult } from "#harness/types.js";
import { createRuntimeHookRegistry } from "#runtime/hooks/registry.js";
import {
createInputRequestedEvent,
@@ -1196,6 +1204,268 @@ describe("turnStep", () => {
});
});
+ describe("an approval answered before a cancelled model call", () => {
+ const approvalRequest: InputRequest = {
+ action: {
+ callId: "approval-call",
+ input: { command: "pwd" },
+ kind: "tool-call",
+ toolName: "bash",
+ },
+ allowFreeform: false,
+ display: "confirmation",
+ kind: "tool-approval",
+ options: [
+ { id: "approve", label: "Yes" },
+ { id: "cancel", label: "No" },
+ ],
+ prompt: "Approve tool call: bash",
+ requestId: "approval-1",
+ };
+
+ /**
+ * Answers the parked approval, cancels the model call that follows (or, with `cutTool`, the
+ * approved call while it runs), then sends a message.
+ */
+ async function answerThenCancel(
+ optionId: "approve" | "cancel",
+ options: {
+ readonly cutTool?: boolean;
+ readonly message?: string;
+ /** The answer, or earlier input, waits in the queue instead of arriving with the delivery. */
+ readonly queued?: StepInput;
+ } = {},
+ ) {
+ const { cutTool = false, message, queued } = options;
+ const answered = queued?.inputResponses !== undefined;
+ const parked = withParkedStep(
+ createStubSession({ history: [{ content: "Run pwd.", kind: "user", role: "user" }] }),
+ {
+ event: { sequence: 0, stepIndex: 0, turnId: "turn_0" },
+ messages: [
+ {
+ content: [
+ {
+ input: { command: "pwd" },
+ toolCallId: "approval-call",
+ toolName: "bash",
+ type: "tool-call",
+ },
+ {
+ approvalId: "approval-1",
+ toolCallId: "approval-call",
+ type: "tool-approval-request",
+ },
+ ],
+ role: "assistant",
+ },
+ ],
+ requests: [approvalRequest],
+ },
+ );
+ const start = queued === undefined ? parked : withQueuedInput(parked, queued);
+ const controller = new AbortController();
+ const events: UnstampedMessageStreamEvent[] = [];
+ const execute = vi.fn(async () => {
+ // The tool has started its side effect when the turn is cancelled.
+ if (cutTool && execute.mock.calls.length === 1) {
+ controller.abort(new TurnCancelledError());
+ throw controller.signal.reason;
+ }
+ return "/workspace";
+ });
+ const model = new MockLanguageModelV4({
+ doStream: async () => {
+ if (cutTool || model.doStreamCalls.length > 1) return textStreamResult("Done.");
+ return {
+ stream: new ReadableStream({
+ start(stream) {
+ stream.enqueue({ type: "stream-start", warnings: [] });
+ stream.enqueue({ id: "answer", type: "text-start" });
+ stream.enqueue({ delta: "Interrupted.", id: "answer", type: "text-delta" });
+ controller.abort(new TurnCancelledError());
+ },
+ }),
+ };
+ },
+ });
+ vi.mocked(createExecutionNodeStep).mockImplementation((input) =>
+ createToolLoopHarness({
+ abortSignal: input.abortSignal,
+ handleEvent: async (event, messages) => {
+ events.push(event);
+ await input.handleEvent?.(event, messages);
+ },
+ resolveModel: async () => model,
+ tools: new Map([
+ [
+ "bash",
+ {
+ description: "Run a shell command.",
+ execute,
+ inputSchema: jsonSchema({ type: "object" }),
+ name: "bash",
+ },
+ ],
+ ]),
+ }),
+ );
+ // An adapter without `deliver` passes the answer through as an answer, not a message.
+ const adapter: ChannelAdapter = { kind: "answers" };
+ const bundle = Object.assign({}, createTurnStepTestBundle() as object, {
+ adapterRegistry: { adaptersByKind: new Map([[adapter.kind, adapter]]) },
+ }) as never;
+ vi.mocked(getCompiledRuntimeAgentBundle).mockResolvedValue(bundle);
+ const ctx = new ContextContainer();
+ ctx.set(AuthKey, null);
+ ctx.set(BundleKey, bundle);
+ ctx.set(ChannelKey, adapter);
+ ctx.set(ContinuationTokenKey, "answers");
+ ctx.set(SessionIdKey, "sess-test");
+ installSessionStoreMocks([start]);
+
+ const cancelled = await turnStep({
+ abortSignal: controller.signal,
+ history: parked.history,
+ input: {
+ kind: "deliver",
+ payloads: [
+ {
+ inputResponses: answered ? [] : [{ optionId, requestId: "approval-1" }],
+ message,
+ },
+ ],
+ },
+ sessionWritable: createTestWritable(),
+ serializedContext: serializeContext(ctx),
+ sessionState: createStubSessionState(),
+ });
+ const cancelledEvents = [...events];
+ const session = cancelled.sessionState.snapshot.session;
+
+ events.length = 0;
+ installSessionStoreMocks([session]);
+ const next = await turnStep({
+ history: cancelled.history,
+ input: { kind: "deliver", payloads: [{ message: "next" }] },
+ sessionWritable: createTestWritable(),
+ serializedContext: cancelled.serializedContext,
+ sessionState: cancelled.sessionState,
+ });
+ return { cancelled, cancelledEvents, execute, next, nextEvents: [...events], session };
+ }
+
+ const resolutions = (events: readonly UnstampedMessageStreamEvent[]) =>
+ events.filter((event) => event.type === "input.resolved");
+
+ it("keeps a denial the cancelled step published", async () => {
+ const { cancelled, cancelledEvents, nextEvents, session } = await answerThenCancel("cancel");
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(resolutions(cancelledEvents)).toHaveLength(1);
+ expect(openInputs(storedProjection(session.state))).toEqual([]);
+ expect(parkedSteps(session)).toEqual([]);
+ expect(cancelled.history).toContainEqual({
+ content: [
+ {
+ output: { reason: "Tool execution was denied.", type: "execution-denied" },
+ toolCallId: "approval-call",
+ toolName: "bash",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ });
+ // The next message neither resolves the denied approval again nor asks it again.
+ expect(resolutions(nextEvents)).toEqual([]);
+ expect(nextEvents.map((event) => event.type)).not.toContain("input.requested");
+ });
+
+ it("keeps a message sent with the denial once, and doesn't queue it again", async () => {
+ const { cancelled, next, session } = await answerThenCancel("cancel", {
+ message: "do something else",
+ });
+ const sent = (history: readonly ModelMessage[]) =>
+ history.filter((entry) => entry.role === "user" && entry.content === "do something else");
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(sent(cancelled.history)).toHaveLength(1);
+ expect(queuedInput(session.state)).toBeUndefined();
+ // The next message doesn't consume the preserved one a second time.
+ expect(sent(next.history)).toHaveLength(1);
+ expect(next.history).toContainEqual(
+ expect.objectContaining({ content: "next", role: "user" }),
+ );
+ });
+
+ it("doesn't restore a queued answer the cancelled step consumed", async () => {
+ const { cancelled, next, nextEvents, session } = await answerThenCancel("cancel", {
+ queued: { inputResponses: [{ optionId: "cancel", requestId: "approval-1" }] },
+ });
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(queuedInput(session.state)).toBeUndefined();
+ expect(parkedSteps(session)).toEqual([]);
+ // The next message doesn't carry the answer again as a response to an earlier prompt.
+ expect(resolutions(nextEvents)).toEqual([]);
+ expect(JSON.stringify(next.history)).not.toContain("earlier interactive prompt");
+ });
+
+ it("keeps earlier queued input once, with the denial's message", async () => {
+ const { cancelled, next, session } = await answerThenCancel("cancel", {
+ message: "and this",
+ queued: { message: "earlier" },
+ });
+ const userText = (history: readonly ModelMessage[]) =>
+ JSON.stringify(history.filter((entry) => entry.role === "user"));
+ const count = (text: string, value: string) => text.split(value).length - 1;
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(queuedInput(session.state)).toBeUndefined();
+ expect(count(userText(cancelled.history), "earlier")).toBe(1);
+ expect(count(userText(cancelled.history), "and this")).toBe(1);
+ expect(count(userText(next.history), "earlier")).toBe(1);
+ expect(count(userText(next.history), "and this")).toBe(1);
+ });
+
+ it("doesn't run an approved call again after the cancellation cut it short", async () => {
+ const { cancelled, execute, nextEvents, session } = await answerThenCancel("approve", {
+ cutTool: true,
+ });
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(execute).toHaveBeenCalledOnce();
+ expect(parkedSteps(session)).toEqual([]);
+ expect(cancelled.history).toContainEqual({
+ content: [
+ {
+ output: { type: "error-text", value: APPROVED_CALL_INTERRUPTED_MESSAGE },
+ toolCallId: "approval-call",
+ toolName: "bash",
+ type: "tool-result",
+ },
+ ],
+ role: "tool",
+ });
+ expect(resolutions(nextEvents)).toEqual([]);
+ });
+
+ it("keeps the result of an approved call that ran before the cut", async () => {
+ const { cancelled, execute, nextEvents, session } = await answerThenCancel("approve");
+
+ expect(cancelled.action).toBe("cancelled");
+ expect(parkedSteps(session)).toEqual([]);
+ expect(cancelled.history).toContainEqual(
+ expect.objectContaining({
+ content: [expect.objectContaining({ toolCallId: "approval-call", type: "tool-result" })],
+ role: "tool",
+ }),
+ );
+ expect(resolutions(nextEvents)).toEqual([]);
+ expect(execute).toHaveBeenCalledOnce();
+ });
+ });
+
it("keeps one model call per Workflow step by default", async () => {
const bundle = createTurnStepTestBundle();
vi.mocked(getCompiledRuntimeAgentBundle).mockResolvedValue(bundle);
diff --git a/packages/eve/src/harness/hitl/approved-calls.test.ts b/packages/eve/src/harness/hitl/approved-calls.test.ts
new file mode 100644
index 000000000..0d5574a9d
--- /dev/null
+++ b/packages/eve/src/harness/hitl/approved-calls.test.ts
@@ -0,0 +1,82 @@
+import { jsonSchema } from "ai";
+import { expect, it, vi } from "vitest";
+
+import {
+ APPROVED_CALL_INTERRUPTED_MESSAGE,
+ runApprovedCalls,
+} from "#harness/hitl/approved-calls.js";
+import { TurnCancelledError } from "#harness/turn-cancellation.js";
+import type { HarnessToolDefinition } from "#harness/execute-tool.js";
+import type { InputRequest } from "#shared/input.js";
+
+function approval(callId: string, toolName: string): InputRequest {
+ return {
+ action: { callId, input: {}, kind: "tool-call", toolName },
+ kind: "tool-approval",
+ options: [
+ { id: "approve", label: "Yes" },
+ { id: "cancel", label: "No" },
+ ],
+ prompt: `Approve tool call: ${toolName}`,
+ requestId: `approval-${callId}`,
+ };
+}
+
+function tool(name: string, execute: () => Promise<unknown>): HarnessToolDefinition {
+ return {
+ description: name,
+ execute,
+ inputSchema: jsonSchema({ type: "object" }),
+ name,
+ } as HarnessToolDefinition;
+}
+
+it("settles calls a cancellation cut short, and keeps their siblings' results", async () => {
+ const controller = new AbortController();
+ const finished = tool("finished", async () => "done");
+ const cut = tool("cut", async () => {
+ // The sibling has its result before the turn is cancelled.
+ await Promise.resolve();
+ controller.abort(new TurnCancelledError());
+ throw controller.signal.reason;
+ });
+ const execute = { cut: vi.spyOn(cut, "execute"), finished: vi.spyOn(finished, "execute") };
+
+ const result = await runApprovedCalls({
+ abortSignal: controller.signal,
+ messages: [],
+ position: { sequence: 0, stepIndex: 0, turnId: "turn_0" },
+ publish: async () => {},
+ requests: [approval("call-finished", "finished"), approval("call-cut", "cut")],
+ tools: new Map([
+ ["finished", finished],
+ ["cut", cut],
+ ]),
+ });
+
+ expect(execute.finished).toHaveBeenCalledOnce();
+ expect(execute.cut).toHaveBeenCalledOnce();
+ expect(result.settled.map(({ part }) => [part.toolCallId, part.output])).toEqual([
+ ["call-finished", { type: "text", value: "done" }],
+ ["call-cut", { type: "error-text", value: APPROVED_CALL_INTERRUPTED_MESSAGE }],
+ ]);
+});
+
+it("leaves a call the cancellation stopped before it started without a result", async () => {
+ const controller = new AbortController();
+ controller.abort(new TurnCancelledError());
+ const unstarted = tool("unstarted", async () => "never");
+ const execute = vi.spyOn(unstarted, "execute");
+
+ const result = await runApprovedCalls({
+ abortSignal: controller.signal,
+ messages: [],
+ position: { sequence: 0, stepIndex: 0, turnId: "turn_0" },
+ publish: async () => {},
+ requests: [approval("call-unstarted", "unstarted")],
+ tools: new Map([["unstarted", unstarted]]),
+ });
+
+ expect(execute).not.toHaveBeenCalled();
+ expect(result.settled).toEqual([]);
+});
diff --git a/packages/eve/src/harness/hitl/approved-calls.ts b/packages/eve/src/harness/hitl/approved-calls.ts
index a0cf075e5..bf931eea8 100644
--- a/packages/eve/src/harness/hitl/approved-calls.ts
+++ b/packages/eve/src/harness/hitl/approved-calls.ts
@@ -30,6 +30,10 @@ export interface ApprovedCallResult {
const log = createLogger("harness.tool-loop");
+/** What the model reads for an approved call the turn's cancellation cut short. */
+export const APPROVED_CALL_INTERRUPTED_MESSAGE =
+ "The turn was cancelled while this approved tool call was running. It may have partially run; it was not retried.";
+
/**
* Runs approved local calls before the model reads their results. Like the calls a model step
* runs inline, each streams its progress and result as it goes. The lifecycle owner places the
@@ -79,6 +83,10 @@ export async function runApprovedCalls(input: {
const settled: ApprovedCallResult[] = [];
const toolResults: TypedToolResult<ToolSet>[] = [];
const { callId, toolName, input: args } = request.action;
+ // Once the tool runs, the call settles even if the turn is cancelled: a result reaches the
+ // session, so it never runs a call with side effects a second time.
+ let started = false;
+ let completed: ApprovedCallResult | undefined;
try {
throwIfTurnAborted(input.abortSignal);
// As in the AI SDK: the stored input revalidates against the tool's own schema and runs
@@ -158,6 +166,7 @@ export async function runApprovedCalls(input: {
await telemetry.execute(async () => {
const startedAt = performance.now();
try {
+ started = true;
const executed = tool.execute!(args, {
abortSignal: input.abortSignal,
context: undefined,
@@ -218,7 +227,7 @@ export async function runApprovedCalls(input: {
if (isInlineAuthorizationToolResult(result)) return { settled, toolResults };
// Calls the tool made on the model's behalf report before its result.
await emitNestedToolActions(input.publish, input.position, callId);
- settled.push({
+ completed = {
part: {
output: failed
? { type: "error-text", value: String(output) }
@@ -227,7 +236,8 @@ export async function runApprovedCalls(input: {
toolName,
type: "tool-result",
},
- });
+ };
+ settled.push(completed);
await input.publish(
createActionResultEvent({
...at,
@@ -239,7 +249,11 @@ export async function runApprovedCalls(input: {
);
return { settled, toolResults };
} catch (error) {
- throwIfTurnAborted(input.abortSignal);
+ if (input.abortSignal?.aborted === true) {
+ if (!started) throwIfTurnAborted(input.abortSignal);
+ if (completed !== undefined) return { settled: [completed], toolResults };
+ return { settled: [interruptedResult(callId, toolName)], toolResults };
+ }
const message = toError(error).message;
logError(log, "approved tool failed", error, { toolName, toolCallId: callId });
await input.publish(
@@ -270,7 +284,10 @@ export async function runApprovedCalls(input: {
}),
);
const failed = executed.find((call) => call.status === "rejected");
- if (failed?.status === "rejected") throw failed.reason;
+ // Only a cancellation rejects a call, and only one that hadn't started: it stays approved. The
+ // calls that settled keep their results, and the caller ends the cancelled turn after it
+ // records them.
+ if (failed?.status === "rejected" && input.abortSignal?.aborted !== true) throw failed.reason;
const completed = executed.flatMap((call) => (call.status === "fulfilled" ? [call.value] : []));
return {
settled: completed.flatMap((call) => call.settled),
@@ -278,6 +295,17 @@ export async function runApprovedCalls(input: {
};
}
+function interruptedResult(callId: string, toolName: string): ApprovedCallResult {
+ return {
+ part: {
+ output: { type: "error-text", value: APPROVED_CALL_INTERRUPTED_MESSAGE },
+ toolCallId: callId,
+ toolName,
+ type: "tool-result",
+ },
+ };
+}
+
type ToolExecutionStart = Parameters<NonNullable<Telemetry["onToolExecutionStart"]>>[0];
type ToolExecutionEnd = Parameters<NonNullable<Telemetry["onToolExecutionEnd"]>>[0];
diff --git a/packages/eve/src/harness/hitl/index.ts b/packages/eve/src/harness/hitl/index.ts
index 1915b713b..2945d3ef9 100644
--- a/packages/eve/src/harness/hitl/index.ts
+++ b/packages/eve/src/harness/hitl/index.ts
@@ -7,9 +7,10 @@ import { clearPendingAuthorization } from "#harness/authorization.js";
import type { resolveInlineAuthorizationInterrupt } from "#harness/inline-tool-authorization.js";
import { validateHarnessModelMessages } from "#harness/messages.js";
import { fail } from "#harness/session-machine/transitions.js";
+import { readTurnState, writeTurnState } from "#harness/session-machine/state.js";
import type { StepCoordinates } from "#harness/session-machine/view.js";
import type { Step } from "#harness/step/context.js";
-import type { HarnessSessionBase, HarnessToolMap, StepResult } from "#harness/types.js";
+import type { HarnessSessionBase, HarnessToolMap, StepInput, StepResult } from "#harness/types.js";
import type { RuntimeWorkflowTaskRequest } from "#shared/action-types.js";
import type { InputRequest } from "#shared/input.js";
import { renderPendingApprovalsInstruction } from "./approval-prompt.js";
@@ -22,6 +23,7 @@ import {
} from "./approvals.js";
import { checkSessionUsageLimit } from "./budget.js";
import { retireActiveCandidates } from "./candidates.js";
+import { isEmptyInput } from "./delivery.js";
import { holdForInput } from "./intake.js";
// The session's human-in-the-loop lifecycle, behind the few points where the rest of the harness
@@ -189,3 +191,19 @@ export function retireCancelledCandidates<T extends HarnessSessionBase>(session:
state: retireActiveCandidates(session.state, { completedAt: Date.now(), reason: "Cancelled." }),
};
}
+
+/**
+ * Takes the message a delivery's answers deferred into the turn queue out of it, so a cancelled
+ * turn can keep it in history once instead of also leaving it queued. The rest of the queue stays.
+ */
+export function takeDeferredMessage<T extends HarnessSessionBase>(
+ session: T,
+): { readonly message?: StepInput["message"]; readonly session: T } {
+ const { queued, ...turn } = readTurnState(session.state);
+ if (queued?.message === undefined) return { session };
+ const { message, ...rest } = queued;
+ return {
+ message,
+ session: writeTurnState(session, isEmptyInput(rest) ? turn : { ...turn, queued: rest }),
+ };
+}
diff --git a/packages/eve/src/harness/hitl/intake.ts b/packages/eve/src/harness/hitl/intake.ts
index 70c235d19..2d9d4ad1a 100644
--- a/packages/eve/src/harness/hitl/intake.ts
+++ b/packages/eve/src/harness/hitl/intake.ts
@@ -13,7 +13,7 @@ import { validateHarnessModelMessages } from "#harness/messages.js";
import { openTurn, type Step } from "#harness/step/context.js";
import { prepareTurnInput } from "#harness/step/intake.js";
import { placeTurnInput } from "#harness/step/prompt.js";
-import { SessionLimitDeclinedError } from "#harness/turn-cancellation.js";
+import { SessionLimitDeclinedError, throwIfTurnAborted } from "#harness/turn-cancellation.js";
import { bumpSessionRuntimeUsageLimits } from "#harness/turn-tag-state.js";
import type { HarnessToolMap, StepInput, StepResult } from "#harness/types.js";
import type { RuntimeWorkflowTaskRequest } from "#shared/action-types.js";
@@ -331,6 +331,8 @@ export async function runApprovedLocalCalls(
}),
);
await step.apply(settle(step.view(), { results: executed.flatMap((run) => run.settled) }));
+ // A cancellation ends the turn once the calls it cut short have their results.
+ throwIfTurnAborted(step.config.abortSignal);
const signIn = resolveInlineAuthorizationInterrupt({
messages: [],
toolResults: executed.flatMap((run) => run.toolResults),
diff --git a/packages/eve/src/harness/session-machine/current.ts b/packages/eve/src/harness/session-machine/current.ts
index 4f982db27..2f3eb0997 100644
--- a/packages/eve/src/harness/session-machine/current.ts
+++ b/packages/eve/src/harness/session-machine/current.ts
@@ -63,6 +63,21 @@ export function recordPublishedEvent(
live.projection = event.type === "session.waiting" ? pruneSessionProjection(folded) : folded;
}
+// The session as the step's last applied transition left it. Each transition publishes its events
+// before the step writes its state, so a step cut short (a cancelled model call) keeps the state
+// that matches what it published instead of rolling back past it.
+const appliedSessions = new WeakMap<ContextReader, HarnessSessionBase>();
+
+/** Records the session a transition the step published produced. */
+export function recordAppliedSession(ctx: ContextReader, session: HarnessSessionBase): void {
+ appliedSessions.set(ctx, session);
+}
+
+/** The session the step's last applied transition produced, if it applied any. */
+export function appliedSession<T extends HarnessSessionBase>(ctx: ContextReader): T | undefined {
+ return appliedSessions.get(ctx) as T | undefined;
+}
+
export function saveSessionProjection<T extends HarnessSessionBase>(
session: T,
ctx: ContextReader,
diff --git a/packages/eve/src/harness/step/context.ts b/packages/eve/src/harness/step/context.ts
index 57ae9faf5..c4c1b2bbd 100644
--- a/packages/eve/src/harness/step/context.ts
+++ b/packages/eve/src/harness/step/context.ts
@@ -16,7 +16,7 @@ import {
sessionView,
type Transition,
} from "#harness/session-machine/commit.js";
-import type { StepProjection } from "#harness/session-machine/current.js";
+import { recordAppliedSession, type StepProjection } from "#harness/session-machine/current.js";
import { fail, receive } from "#harness/session-machine/transitions.js";
import {
activeTurnId,
@@ -97,6 +97,7 @@ export function createStep(input: {
position: () => turnPosition(live.read()),
async apply(transition, messages) {
step.session = await applyTransition(step.session, transition, input.publish, messages);
+ if (ctx !== undefined) recordAppliedSession(ctx, step.session);
},
projectHistory: (messages, state = step.session.state) =>
input.prepareHistory(messages, state).messages,