
书名:《Agent Runtime 工程化:从工具调用循环到可恢复执行系统》
作者:陈堂会
章节:第二章 TypeScript / Node Runtime 基础 / 2.4 子进程执行器:Agent 的危险地带
电子版系列:查看完整目录
第二章 TypeScript / Node Runtime 基础:2.4 子进程执行器:Agent 的危险地带
coding agent 迟早会执行 shell 命令。读目录、跑测试、安装依赖、生成文件、调用 CLI,都会经过子进程。这里必须谨慎,因为 shell 是副作用入口。
一个最低限度的命令执行器应当具备:
- 明确的工作目录;
- 环境变量白名单或脱敏策略;
- stdout/stderr 流式输出;
- 超时;
- 取消;
- 退出码记录;
- 最大输出限制;
- 风险分类与审批前置。
示例骨架:
import { spawn } from "node:child_process";
export function runCommand(
command: string,
args: string[],
options: { cwd: string; signal: AbortSignal; timeoutMs: number }
): AsyncIterable<{ stream: "stdout" | "stderr"; text: string }> {
const child = spawn(command, args, {
cwd: options.cwd,
shell: ,
: [, , ],
});
timer = ( {
child.();
}, options.);
options..(, {
child.();
});
(child, timer);
}

