xonecas.io
64875
Mc

mini-coder / commit 4d853ca

fix: propagate abort signal to tool execute functions

The AI SDK passes ToolExecutionOptions (including abortSignal) as the
second argument to tool execute functions, but our dynamicTool wrapper
was discarding it. This meant ESC/abort had no effect on in-flight
shell processes — long-running commands like sub-agent mc calls or log
tailing would keep running after the user interrupted.

Changes:
- Add ToolExecuteOptions (with signal) to ToolDef.execute signature
- Forward abortSignal from AI SDK's ToolExecutionOptions in toCoreTool
- Forward options through withCwdDefault wrapper
- Wire abort signal in runShellCommand to SIGTERM/SIGKILL the process
- Extract shared killProc() to DRY the timeout and abort kill logic
- Add test: abort signal kills a long-running process

xonecas2026-03-22 4d853caeb2295d56fe1c1117c96114e15e526041 parent 05e92ec

5 files changed

src/agent/tools.ts+3−3
@@ -1,4 +1,4 @@
1-import type { ToolDef } from "../llm-api/types.ts";
1+import type { ToolDef, ToolExecuteOptions } from "../llm-api/types.ts";
22 import { webContentTool, webSearchTool } from "../tools/exa.ts";
33 import type { ShellOutput } from "../tools/shell.ts";
44 import { shellTool } from "../tools/shell.ts";
@@ -11,12 +11,12 @@ function withCwdDefault(tool: ToolDef, cwd: string): ToolDef {
1111 const originalExecute = tool.execute;
1212 return {
1313 ...tool,
14- execute: async (input: unknown) => {
14+ execute: async (input: unknown, options?: ToolExecuteOptions) => {
1515 const patched = (
1616 typeof input === "object" && input !== null ? input : {}
1717 ) as Record<string, unknown>;
1818 if (patched.cwd === undefined) patched.cwd = cwd;
19- return originalExecute(patched);
19+ return originalExecute(patched, options);
2020 },
2121 };
2222 }
src/llm-api/turn-execution.ts+5−2
@@ -50,9 +50,12 @@ function toCoreTool(def: ToolDef): ReturnType<typeof dynamicTool> {
5050 return dynamicTool({
5151 description: def.description,
5252 inputSchema: schema,
53- execute: async (input: unknown) => {
53+ execute: async (input: unknown, { abortSignal }) => {
5454 try {
55- return await def.execute(input);
55+ return await def.execute(
56+ input,
57+ abortSignal ? { signal: abortSignal } : undefined,
58+ );
5659 } catch (err) {
5760 throw normalizeUnknownError(err);
5861 }
src/llm-api/types.ts+5−1
@@ -76,10 +76,14 @@ export type TurnEvent =
7676
7777 // ─── Tool definition ──────────────────────────────────────────────────────────
7878
79+export interface ToolExecuteOptions {
80+ signal?: AbortSignal;
81+}
82+
7983 export interface ToolDef<TInput = unknown, TOutput = unknown> {
8084 name: string;
8185 description: string;
8286 /** Zod schema — typed as unknown here, cast at call sites */
8387 schema: unknown;
84- execute: (input: TInput) => Promise<TOutput>;
88+ execute: (input: TInput, options?: ToolExecuteOptions) => Promise<TOutput>;
8589 }
src/tools/shell.test.ts+17−0
@@ -22,3 +22,20 @@ describe("shellTool", () => {
2222 expect(result.exitCode).toBe(0);
2323 });
2424 });
25+
26+it("kills the process when abort signal fires", async () => {
27+ const ac = new AbortController();
28+
29+ // Start a long-running sleep
30+ const promise = shellTool.execute(
31+ { command: "sleep 60", timeout: null },
32+ { signal: ac.signal },
33+ );
34+
35+ // Abort after 100ms
36+ setTimeout(() => ac.abort(), 100);
37+
38+ const result = await promise;
39+ // Process should have been killed, not run for 60s
40+ expect(result.success).toBe(false);
41+});
src/tools/shell.ts+36−18
@@ -2,7 +2,7 @@ import { z } from "zod";
22 import { restoreTerminal } from "../cli/output.ts";
33 import { stripAnsi } from "../internal/ansi.ts";
44 import { buildFileEditShellPrelude } from "../internal/file-edit/command.ts";
5-import type { ToolDef } from "../llm-api/types.ts";
5+import type { ToolDef, ToolExecuteOptions } from "../llm-api/types.ts";
66
77 const ShellSchema = z.object({
88 command: z.string().describe("Shell command to execute"),
@@ -41,7 +41,10 @@ export interface ShellOutput {
4141
4242 const MAX_OUTPUT_BYTES = 10_000; // 10KB per stream
4343
44-export async function runShellCommand(input: ShellInput): Promise<ShellOutput> {
44+export async function runShellCommand(
45+ input: ShellInput,
46+ options?: ToolExecuteOptions,
47+): Promise<ShellOutput> {
4548 const cwd = input.cwd ?? process.cwd();
4649 const timeout = input.timeout ?? undefined;
4750 const inputEnv = input.env ?? undefined;
@@ -75,27 +78,41 @@ export async function runShellCommand(input: ShellInput): Promise<ShellOutput> {
7578 },
7679 );
7780
78- const timer = timeout
79- ? setTimeout(() => {
80- timedOut = true;
81+ function killProc(): void {
82+ try {
83+ proc.kill("SIGTERM");
84+ setTimeout(() => {
8185 try {
82- proc.kill("SIGTERM");
83- setTimeout(() => {
84- try {
85- proc.kill("SIGKILL");
86- } catch {
87- /* already dead */
88- }
89- }, 2000);
86+ proc.kill("SIGKILL");
9087 } catch {
91- /* already done */
92- }
93- for (const reader of readers) {
94- reader.cancel().catch(() => {});
88+ /* already dead */
9589 }
90+ }, 2000);
91+ } catch {
92+ /* already done */
93+ }
94+ for (const reader of readers) {
95+ reader.cancel().catch(() => {});
96+ }
97+ }
98+
99+ const timer = timeout
100+ ? setTimeout(() => {
101+ timedOut = true;
102+ killProc();
96103 }, timeout)
97104 : undefined;
98105
106+ const abortSignal = options?.signal;
107+ const onAbort = () => {
108+ killProc();
109+ };
110+ if (abortSignal?.aborted) {
111+ onAbort();
112+ } else {
113+ abortSignal?.addEventListener("abort", onAbort, { once: true });
114+ }
115+
99116 async function collectStream(
100117 stream: ReadableStream<Uint8Array>,
101118 ): Promise<string> {
@@ -144,6 +161,7 @@ export async function runShellCommand(input: ShellInput): Promise<ShellOutput> {
144161 exitCode = await proc.exited;
145162 } finally {
146163 if (timer) clearTimeout(timer);
164+ abortSignal?.removeEventListener("abort", onAbort);
147165 restoreTerminal();
148166 if (wasRaw) {
149167 try {
@@ -175,5 +193,5 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
175193 "Use this for reading/searching code, running tests, builds, git commands, and invoking `mc-edit` for partial file edits. " +
176194 "Prefer non-interactive commands. Avoid commands that run indefinitely.",
177195 schema: ShellSchema,
178- execute: runShellCommand,
196+ execute: (input, options) => runShellCommand(input, options),
179197 };