-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbash.ts
More file actions
378 lines (329 loc) · 11.4 KB
/
bash.ts
File metadata and controls
378 lines (329 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Bash tool - Execute shell commands with timeout and background support
*/
import { spawn, ChildProcess } from 'child_process';
import { mkdirSync, readFileSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import type { Tool, ToolContext, JSONSchema } from '../types/tools';
export interface BackgroundProcess {
pid: number;
startTime: number;
stdout: string;
stderr: string;
stdoutPath?: string;
stderrPath?: string;
stdoutTruncated: boolean;
stderrTruncated: boolean;
exitCode: number | null;
process: ChildProcess;
detached: boolean;
}
export interface BashInput {
command: string;
timeout?: number;
description?: string;
run_in_background?: boolean;
}
export interface BashOutput {
output: string;
exitCode: number;
killed?: boolean;
shellId?: string;
error?: string;
}
const parameters: JSONSchema = {
type: 'object',
properties: {
command: {
type: 'string',
description: 'Shell command to execute',
},
timeout: {
type: 'number',
description: 'Maximum execution time in milliseconds (max 600000)',
},
description: {
type: 'string',
description: 'Brief description of the command (5-10 words)',
},
run_in_background: {
type: 'boolean',
description: 'Run the command in the background',
},
},
required: ['command'],
};
// Track background processes
let backgroundProcessId = 0;
export const backgroundProcesses = new Map<string, BackgroundProcess>();
// Cap captured output to avoid OOM when commands print large streams.
const MAX_CAPTURE_CHARS = 200_000;
const TRUNCATED_NOTICE = '\n[Output truncated to avoid excessive memory usage]';
const BG_LOG_DIR = join(tmpdir(), 'open-agent-sdk-bg');
function appendCapped(
current: string,
chunk: string,
maxChars: number
): { value: string; truncated: boolean } {
if (current.length >= maxChars) {
return { value: current, truncated: true };
}
const remaining = maxChars - current.length;
if (chunk.length <= remaining) {
return { value: current + chunk, truncated: false };
}
return { value: current + chunk.slice(0, remaining), truncated: true };
}
export class BashTool implements Tool<BashInput, BashOutput> {
name = 'Bash';
description =
'Execute a shell command. Supports timeout (max 600000ms), background execution, and captures stdout/stderr.';
parameters = parameters;
handler = async (
input: BashInput,
context: ToolContext
): Promise<BashOutput> => {
const { command, timeout = 120000, run_in_background } = input;
if (!command.trim()) {
return { output: '', exitCode: 0 };
}
// Check if already aborted
if (context.abortController?.signal.aborted) {
return {
output: 'Command aborted before execution',
exitCode: -1,
killed: true,
};
}
return new Promise((resolve) => {
const shell = process.platform === 'win32' ? 'cmd.exe' : '/bin/sh';
const shellFlag = process.platform === 'win32' ? '/c' : '-c';
let shellId: string | undefined;
let stdoutPath: string | undefined;
let stderrPath: string | undefined;
let detachedBackground = false;
if (run_in_background) {
shellId = `shell_${++backgroundProcessId}`;
if (process.platform !== 'win32') {
detachedBackground = true;
mkdirSync(BG_LOG_DIR, { recursive: true });
stdoutPath = join(BG_LOG_DIR, `${shellId}.stdout.log`);
stderrPath = join(BG_LOG_DIR, `${shellId}.stderr.log`);
}
}
const normalizedCommand = run_in_background
? command.replace(/\s*&\s*$/, '').trim()
: command;
const commandToRun = run_in_background && stdoutPath && stderrPath
? `( ${normalizedCommand} ) >>"${stdoutPath}" 2>>"${stderrPath}"`
: normalizedCommand;
const child = spawn(shell, [shellFlag, commandToRun], {
cwd: context.cwd,
env: { ...process.env, ...context.env },
stdio: run_in_background && detachedBackground ? ['ignore', 'ignore', 'ignore'] : ['ignore', 'pipe', 'pipe'],
detached: run_in_background ? detachedBackground : false,
});
// Set up abort handler
let abortHandler: (() => void) | undefined;
if (context.abortController?.signal) {
abortHandler = () => {
killed = true;
child.kill('SIGTERM');
// Force kill after 5 seconds if still running
setTimeout(() => child.kill('SIGKILL'), 5000);
};
context.abortController.signal.addEventListener('abort', abortHandler);
}
// Handle background execution
if (run_in_background) {
const bgProcess: BackgroundProcess = {
pid: child.pid!,
startTime: Date.now(),
stdout: '',
stderr: '',
stdoutPath,
stderrPath,
stdoutTruncated: false,
stderrTruncated: false,
exitCode: null,
process: child,
detached: detachedBackground,
};
backgroundProcesses.set(shellId!, bgProcess);
if (!detachedBackground) {
// Capture stdout/stderr in-process when not detached.
child.stdout?.on('data', (data) => {
const next = appendCapped(bgProcess.stdout, data.toString(), MAX_CAPTURE_CHARS);
bgProcess.stdout = next.value;
if (next.truncated) bgProcess.stdoutTruncated = true;
});
child.stderr?.on('data', (data) => {
const next = appendCapped(bgProcess.stderr, data.toString(), MAX_CAPTURE_CHARS);
bgProcess.stderr = next.value;
if (next.truncated) bgProcess.stderrTruncated = true;
});
}
// Set exit code when process exits (don't delete from map)
child.on('exit', (code) => {
bgProcess.exitCode = code ?? -1;
if (bgProcess.stdoutPath) {
try {
const content = readFileSync(bgProcess.stdoutPath, 'utf8');
const next = appendCapped('', content, MAX_CAPTURE_CHARS);
bgProcess.stdout = next.value;
bgProcess.stdoutTruncated = next.truncated;
} catch {
// Ignore read errors for best-effort output capture.
}
}
if (bgProcess.stderrPath) {
try {
const content = readFileSync(bgProcess.stderrPath, 'utf8');
const next = appendCapped('', content, MAX_CAPTURE_CHARS);
bgProcess.stderr = next.value;
bgProcess.stderrTruncated = next.truncated;
} catch {
// Ignore read errors for best-effort output capture.
}
}
});
// Prevent background child handles from keeping the process alive.
child.unref();
(child.stdout as unknown as { unref?: () => void } | null)?.unref?.();
(child.stderr as unknown as { unref?: () => void } | null)?.unref?.();
// Don't wait for completion
resolve({
output: `Command running in background with ID: ${shellId!}`,
exitCode: 0,
shellId: shellId!,
});
return;
}
let stdout = '';
let stderr = '';
let killed = false;
let stdoutTruncated = false;
let stderrTruncated = false;
child.stdout?.on('data', (data) => {
const next = appendCapped(stdout, data.toString(), MAX_CAPTURE_CHARS);
stdout = next.value;
if (next.truncated) stdoutTruncated = true;
});
child.stderr?.on('data', (data) => {
const next = appendCapped(stderr, data.toString(), MAX_CAPTURE_CHARS);
stderr = next.value;
if (next.truncated) stderrTruncated = true;
});
// Set up timeout
const timeoutId = setTimeout(() => {
killed = true;
child.kill('SIGTERM');
// Force kill after 5 seconds if still running
setTimeout(() => child.kill('SIGKILL'), 5000);
}, Math.min(timeout, 600000));
child.on('exit', (code, signal) => {
clearTimeout(timeoutId);
if (signal === 'SIGTERM' || signal === 'SIGKILL') {
killed = true;
}
// Clean up abort listener
if (abortHandler && context.abortController?.signal) {
context.abortController.signal.removeEventListener('abort', abortHandler);
}
let output = stdout + (stderr ? '\n' + stderr : '');
// Normalize macOS /private prefix for pwd command
if (command.trim() === 'pwd' && output.startsWith('/private/')) {
output = output.substring('/private'.length);
}
// Check if killed due to abort
const wasAborted = context.abortController?.signal.aborted;
const outputMessage = wasAborted
? output + '\n[Command aborted]'
: killed
? output + '\n[Command timed out]'
: output;
const finalOutput = stdoutTruncated || stderrTruncated
? outputMessage + TRUNCATED_NOTICE
: outputMessage;
resolve({
output: finalOutput,
exitCode: code ?? (killed ? -1 : 0),
killed,
});
});
child.on('error', (err) => {
clearTimeout(timeoutId);
// Clean up abort listener
if (abortHandler && context.abortController?.signal) {
context.abortController.signal.removeEventListener('abort', abortHandler);
}
resolve({
output: '',
exitCode: -1,
error: `Failed to execute command: ${err.message}`,
});
});
});
};
}
// Export singleton instance
export const bashTool = new BashTool();
// Export function to get background process info (for future BashOutput tool)
export function getBackgroundProcess(shellId: string) {
return backgroundProcesses.get(shellId);
}
/**
* Best-effort cleanup for any running background bash processes.
* Intended for process shutdown paths (e.g. CLI exit) to avoid hangs.
*/
export async function cleanupBackgroundProcesses(
forceKillAfterMs = 1000
): Promise<void> {
const entries = Array.from(backgroundProcesses.values()).filter((p) => p.exitCode === null);
await Promise.all(
entries.map(
(bgProcess) =>
new Promise<void>((resolve) => {
if (bgProcess.exitCode !== null) {
resolve();
return;
}
let settled = false;
const done = () => {
if (settled) return;
settled = true;
clearTimeout(forceKillTimer);
resolve();
};
bgProcess.process.once('exit', (code) => {
bgProcess.exitCode = code ?? -1;
done();
});
try {
if (bgProcess.detached && process.platform !== 'win32') {
process.kill(-bgProcess.pid, 'SIGTERM');
} else {
bgProcess.process.kill('SIGTERM');
}
} catch {
// Ignore errors if process already exited.
}
const forceKillTimer = setTimeout(() => {
if (bgProcess.exitCode === null) {
try {
if (bgProcess.detached && process.platform !== 'win32') {
process.kill(-bgProcess.pid, 'SIGKILL');
} else {
bgProcess.process.kill('SIGKILL');
}
} catch {
// Ignore errors if process already exited.
}
}
done();
}, Math.max(1, forceKillAfterMs));
})
)
);
}