@@ -1,6 +1,6 @@
11 import type { Database } from "bun:sqlite";
22 import { afterEach, beforeEach, describe, expect, test } from "bun:test";
3-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
44 import { tmpdir } from "node:os";
55 import { join } from "node:path";
66 import type {
@@ -975,7 +975,8 @@ describe("agent loop", () => {
975975 { type: "text", text: "Shell error: This operation was aborted" },
976976 ]);
977977
978- expect(events.some((event) => event.type === "aborted")).toBe(false);
978+ const abortedEvent = expectLastEvent(events, "aborted");
979+ expect(abortedEvent.message.stopReason).toBe("aborted");
979980 expect(
980981 events.filter((event) => event.type === "assistant_message"),
981982 ).toHaveLength(1);
@@ -991,6 +992,73 @@ describe("agent loop", () => {
991992 ]);
992993 });
993994
995+ test("interrupt before a non-signal-aware tool runs leaves the file unchanged", async () => {
996+ faux.setResponses([
997+ fauxAssistantMessage(
998+ [
999+ fauxToolCall("edit", {
1000+ path: "note.txt",
1001+ oldText: "before",
1002+ newText: "after",
1003+ }),
1004+ ],
1005+ { stopReason: "toolUse" },
1006+ ),
1007+ ]);
1008+ writeFileSync(join(tmp, "note.txt"), "before", "utf-8");
1009+
1010+ const controller = new AbortController();
1011+ const events: AgentEvent[] = [];
1012+ const toolPath = join(tmp, "note.txt");
1013+
1014+ const session = createSession(db, { cwd: tmp });
1015+ const userMsg = makeUser("abort before edit");
1016+ const turn = appendMessage(db, session.id, userMsg);
1017+
1018+ const result = await runAgentLoop({
1019+ db,
1020+ sessionId: session.id,
1021+ turn,
1022+ model: faux.getModel(),
1023+ systemPrompt: "Test",
1024+ tools: builtinToolDefs(),
1025+ toolHandlers: builtinToolHandlers(),
1026+ messages: [userMsg],
1027+ cwd: tmp,
1028+ signal: controller.signal,
1029+ onEvent: (event) => {
1030+ events.push(event);
1031+ if (event.type === "tool_start") {
1032+ controller.abort();
1033+ }
1034+ },
1035+ });
1036+
1037+ expect(result.stopReason).toBe("aborted");
1038+ expect(readFileSync(toolPath, "utf-8")).toBe("before");
1039+
1040+ const toolResult = result.messages[2];
1041+ expect(toolResult?.role).toBe("toolResult");
1042+ if (!toolResult || toolResult.role !== "toolResult") {
1043+ throw new Error("Expected tool result message");
1044+ }
1045+ expect(toolResult.isError).toBe(true);
1046+ expect(toolResult.content).toEqual([
1047+ { type: "text", text: "Tool edit failed: This operation was aborted" },
1048+ ]);
1049+
1050+ const startIndex = events.findIndex((event) => event.type === "tool_start");
1051+ const endIndex = events.findIndex((event) => event.type === "tool_end");
1052+ const resultIndex = events.findIndex(
1053+ (event) => event.type === "tool_result",
1054+ );
1055+ const abortedIndex = events.findIndex((event) => event.type === "aborted");
1056+ expect(startIndex).toBeGreaterThan(-1);
1057+ expect(startIndex).toBeLessThan(endIndex);
1058+ expect(endIndex).toBeLessThan(resultIndex);
1059+ expect(resultIndex).toBeLessThan(abortedIndex);
1060+ });
1061+
9941062 test("uses an explicit stream end result when no terminal event was emitted", async () => {
9951063 const api = "agent-stream-end-result-test";
9961064 const sourceId = "agent-stream-end-result-test-source";