mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-15 22:03:45 +00:00
fix(coding-agent): improve bash tool error handling (#479)
- Validate working directory exists before spawning to provide clear error message - Add spawn error handler to prevent uncaught exceptions when shell not found or cwd invalid - Add tests for both error scenarios Without these fixes, spawn errors (e.g., ENOENT from missing cwd or shell) would cause uncaught exceptions that crash the entire agent session instead of being returned as clean tool errors. Co-authored-by: robinwander <robinwander@users.noreply.github.com>
This commit is contained in:
parent
c6aa0407e7
commit
1432fd91d2
2 changed files with 41 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { randomBytes } from "node:crypto";
|
||||
import { createWriteStream } from "node:fs";
|
||||
import { createWriteStream, existsSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import type { AgentTool } from "@mariozechner/pi-agent-core";
|
||||
|
|
@ -40,6 +40,11 @@ export function createBashTool(cwd: string): AgentTool<typeof bashSchema> {
|
|||
) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { shell, args } = getShellConfig();
|
||||
|
||||
if (!existsSync(cwd)) {
|
||||
throw new Error(`Working directory does not exist: ${cwd}\nCannot execute bash commands.`);
|
||||
}
|
||||
|
||||
const child = spawn(shell, [...args, command], {
|
||||
cwd,
|
||||
detached: true,
|
||||
|
|
@ -119,6 +124,17 @@ export function createBashTool(cwd: string): AgentTool<typeof bashSchema> {
|
|||
child.stderr.on("data", handleData);
|
||||
}
|
||||
|
||||
// Handle shell spawn errors to prevent session from crashing
|
||||
child.on("error", (err) => {
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle);
|
||||
}
|
||||
if (signal) {
|
||||
signal.removeEventListener("abort", onAbort);
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
|
||||
// Handle process exit
|
||||
child.on("close", (code) => {
|
||||
if (timeoutHandle) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue