新增 agent-executor 服务:9000 端口接收命令并以 SSE 实时回传输出(零依赖单文件;cwd 白名单/可选 token 鉴权/超时与取消杀进程组);补回 compose 顶层 external 网络定义;附冒烟测试
This commit is contained in:
@@ -19,6 +19,8 @@ services:
|
||||
- CERT_NAME=default
|
||||
# 加入executor独立服务
|
||||
- EXECUTOR_URL=http://agent-executor:9000
|
||||
# 两侧设成同一个值即启用调用鉴权(内网互访,可选):
|
||||
# - EXECUTOR_TOKEN=change-me
|
||||
networks:
|
||||
- my-app-network
|
||||
|
||||
@@ -33,8 +35,14 @@ services:
|
||||
- ./skills:/workspace/skills # 技能目录:所有任务/会话共享同一份技能
|
||||
environment:
|
||||
- PORT=9000
|
||||
# 与 agent-core 侧设成同一个值即启用调用鉴权(可选):
|
||||
# - EXECUTOR_TOKEN=change-me
|
||||
cap_drop:
|
||||
- SYS_ADMIN
|
||||
- DAC_OVERRIDE
|
||||
networks:
|
||||
- my-app-network
|
||||
|
||||
networks:
|
||||
my-app-network:
|
||||
external: true
|
||||
|
||||
@@ -26,3 +26,14 @@ RUN pip3 install --no-cache-dir --break-system-packages \
|
||||
pymupdf pdfplumber pypdf reportlab \
|
||||
Pillow python-dateutil python-magic \
|
||||
loguru rich fonttools
|
||||
|
||||
# ---- executor 服务(零依赖单文件,仅 Node 内置 http)----
|
||||
WORKDIR /app
|
||||
COPY server.js ./
|
||||
|
||||
EXPOSE 9000
|
||||
ENV PORT=9000 \
|
||||
EXECUTOR_ROOTS=/workspace \
|
||||
EXECUTOR_CWD=/workspace/tasks
|
||||
|
||||
CMD ["node", "server.js"]
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
// agent-executor:命令执行服务(独立容器,供 agent-core 调用)。单文件零依赖,仅用 Node 内置 http,默认端口 9000。
|
||||
//
|
||||
// 接口:
|
||||
// GET /health → {ok, running:[{id,pid,cwd,started_at}]}
|
||||
// POST /exec body {command, cwd?, timeout_ms?, max_output_chars?, env?}
|
||||
// 响应 SSE(text/event-stream),每条 data 为一个 JSON:
|
||||
// {"type":"start","id","pid","cwd"} 进程已启动
|
||||
// {"type":"out","chunk"} stdout 增量(实时分块下发)
|
||||
// {"type":"err","chunk"} stderr 增量
|
||||
// {"type":"exit","code","killed_by","stdout_chars","stderr_chars","truncated"} 结束汇总
|
||||
// 期间每 15s 发 ": ping" 注释行保活。
|
||||
// DELETE /exec/:id → {ok:true} 杀整个进程组(core 侧取消/超时用)
|
||||
//
|
||||
// 安全约束:
|
||||
// - 设置 EXECUTOR_TOKEN 后,除 /health 外必须带 Authorization: Bearer <token>(compose 内网互访,token 可选);
|
||||
// - cwd 必须落在 EXECUTOR_ROOTS(逗号分隔白名单,默认 /workspace)内,先解析再 realpath 校验,防穿越;
|
||||
// - timeout_ms 夹在 [1s, EXECUTOR_MAX_TIMEOUT_MS](默认 30 分钟);
|
||||
// - stdout/stderr 各自按 max_output_chars 截断(默认 EXECUTOR_MAX_OUTPUT_CHARS=200000),超限后丢弃剩余输出,exit 事件带 truncated 标记。
|
||||
//
|
||||
// 约定:core 与 executor 把同一份宿主机 ./tasks、./skills 挂到相同容器路径(/workspace/*),
|
||||
// 所以 core 下发的任务目录 cwd 在 executor 内原样可用。
|
||||
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
const PORT = parseInt(process.env.PORT, 10) || 9000;
|
||||
const TOKEN = process.env.EXECUTOR_TOKEN || '';
|
||||
const ROOTS = (process.env.EXECUTOR_ROOTS || '/workspace').split(',').map(s => s.trim()).filter(Boolean).map(p => path.resolve(p));
|
||||
const DEFAULT_CWD = process.env.EXECUTOR_CWD || path.join(ROOTS[0], 'tasks');
|
||||
const MAX_TIMEOUT = parseInt(process.env.EXECUTOR_MAX_TIMEOUT_MS, 10) || 30 * 60 * 1000;
|
||||
const DEFAULT_MAX_OUT = parseInt(process.env.EXECUTOR_MAX_OUTPUT_CHARS, 10) || 200000;
|
||||
const PING_MS = 15000;
|
||||
const BODY_LIMIT = 1024 * 1024; // /exec 请求体上限 1MB
|
||||
|
||||
const runs = new Map(); // id -> { child, killedBy, started_at, cwd }
|
||||
|
||||
// 统一日志(ISO 时间戳 + 前缀)
|
||||
function log(...args) { console.log(new Date().toISOString(), '[executor]', ...args); }
|
||||
|
||||
// 常量时间比较,避免通过响应耗时猜 token
|
||||
function tokenOk(req) {
|
||||
if (!TOKEN) return true;
|
||||
const auth = String(req.headers.authorization || '');
|
||||
const got = auth.startsWith('Bearer ') ? auth.slice(7) : '';
|
||||
const a = Buffer.from(got), b = Buffer.from(TOKEN);
|
||||
if (a.length !== b.length) return false;
|
||||
return crypto.timingSafeEqual(a, b);
|
||||
}
|
||||
|
||||
function send(res, status, obj) {
|
||||
const body = JSON.stringify(obj);
|
||||
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
res.end(body);
|
||||
}
|
||||
|
||||
function readJsonBody(req) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let size = 0; const chunks = [];
|
||||
req.on('data', c => {
|
||||
size += c.length;
|
||||
if (size > BODY_LIMIT) { reject(new Error('请求体超过 ' + BODY_LIMIT + ' 字节')); req.destroy(); return; }
|
||||
chunks.push(c);
|
||||
});
|
||||
req.on('end', () => { try { resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : {}); } catch (e) { reject(new Error('body 不是合法 JSON:' + e.message)); } });
|
||||
req.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
// cwd 白名单校验:解析为绝对路径后必须位于某个 root 的 realpath 之下(含 root 本身)。
|
||||
function checkCwd(raw) {
|
||||
let cwd;
|
||||
try { cwd = path.resolve(raw); } catch (_) { return null; }
|
||||
let real;
|
||||
try { real = fs.realpathSync(cwd); } catch (_) { return null; } // 不存在直接拒绝
|
||||
for (const root of ROOTS) {
|
||||
let realRoot;
|
||||
try { realRoot = fs.realpathSync(root); } catch (_) { continue; }
|
||||
if (real === realRoot || real.startsWith(realRoot + path.sep)) return real;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function killGroup(entry) {
|
||||
const child = entry.child;
|
||||
// detached 启动后 child 是组长:对 -pid 发信号可连子进程的子孙一起杀掉
|
||||
try { process.kill(-child.pid, 'SIGKILL'); }
|
||||
catch (_) { try { child.kill('SIGKILL'); } catch (__) { /* 已退出 */ } }
|
||||
}
|
||||
|
||||
function sseWrite(res, obj) { res.write('data: ' + JSON.stringify(obj) + '\n\n'); }
|
||||
|
||||
async function handleExec(req, res) {
|
||||
let args;
|
||||
try { args = await readJsonBody(req); } catch (e) { return send(res, 400, { error: e.message }); }
|
||||
|
||||
const command = String(args.command || '');
|
||||
if (!command.trim()) return send(res, 400, { error: 'command 必填' });
|
||||
|
||||
const rawCwd = args.cwd ? String(args.cwd) : DEFAULT_CWD;
|
||||
const cwd = checkCwd(rawCwd);
|
||||
if (!cwd) return send(res, 400, { error: 'cwd 不存在或不在白名单目录内:' + rawCwd + '(允许:' + ROOTS.join(', ') + ')' });
|
||||
|
||||
let timeout = parseInt(args.timeout_ms, 10);
|
||||
if (!isFinite(timeout) || timeout <= 0) timeout = 5 * 60 * 1000;
|
||||
timeout = Math.min(Math.max(timeout, 1000), MAX_TIMEOUT);
|
||||
|
||||
let maxChars = parseInt(args.max_output_chars, 10);
|
||||
if (!isFinite(maxChars) || maxChars <= 0) maxChars = DEFAULT_MAX_OUT;
|
||||
maxChars = Math.min(maxChars, DEFAULT_MAX_OUT);
|
||||
|
||||
const env = Object.assign({}, process.env);
|
||||
if (args.env && typeof args.env === 'object') {
|
||||
for (const [k, v] of Object.entries(args.env)) env[String(k)] = String(v);
|
||||
}
|
||||
|
||||
const id = crypto.randomUUID();
|
||||
let child;
|
||||
try {
|
||||
child = spawn('/bin/sh', ['-c', command], { cwd, env, detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
} catch (e) {
|
||||
return send(res, 500, { error: 'spawn 失败:' + e.message });
|
||||
}
|
||||
|
||||
const entry = { child, killedBy: null, started_at: Date.now(), cwd, timer: null };
|
||||
runs.set(id, entry);
|
||||
log('start', id, 'pid=' + child.pid, 'cwd=' + cwd, 'timeout=' + timeout + 'ms', JSON.stringify(command).slice(0, 200));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream; charset=utf-8',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'X-Accel-Buffering': 'no', // nginx 前置时对 SSE 关缓冲(配合 core 的 proxy_buffering off 习惯)
|
||||
});
|
||||
const ping = setInterval(() => { res.write(': ping\n\n'); }, PING_MS);
|
||||
// 先下发 start 事件:core 靠这里的 id 调 DELETE /exec/:id 取消
|
||||
sseWrite(res, { type: 'start', id, pid: child.pid, cwd });
|
||||
|
||||
// 输出计数:超限后停止下发(truncated),但仍要持续消费流,防子进程因管道满阻塞。
|
||||
const out = { stdout: 0, stderr: 0, truncated: false };
|
||||
function streamChunk(kind, chunk) {
|
||||
const key = kind === 'out' ? 'stdout' : 'stderr';
|
||||
const text = chunk.toString('utf8');
|
||||
const remain = maxChars - out[key];
|
||||
if (remain <= 0) { out.truncated = true; return; }
|
||||
if (text.length > remain) { out.truncated = true; sseWrite(res, { type: kind, chunk: text.slice(0, remain) }); out[key] = maxChars; return; }
|
||||
out[key] += text.length;
|
||||
sseWrite(res, { type: kind, chunk: text });
|
||||
}
|
||||
child.stdout.on('data', c => streamChunk('out', c));
|
||||
child.stderr.on('data', c => streamChunk('err', c));
|
||||
|
||||
entry.timer = setTimeout(() => { entry.killedBy = 'timeout'; killGroup(entry); }, timeout);
|
||||
|
||||
// core 断开连接(取消/重启):连带杀掉进程,防孤儿。
|
||||
res.on('close', () => {
|
||||
if (runs.has(id)) { entry.killedBy = entry.killedBy || 'disconnected'; killGroup(entry); }
|
||||
});
|
||||
|
||||
function finish(code) {
|
||||
if (!runs.has(id)) return;
|
||||
clearTimeout(entry.timer); clearInterval(ping); runs.delete(id);
|
||||
sseWrite(res, {
|
||||
type: 'exit', code, signal: child.signalCode, killed_by: entry.killedBy,
|
||||
stdout_chars: out.stdout, stderr_chars: out.stderr, truncated: out.truncated,
|
||||
});
|
||||
log('exit ', id, 'code=' + code, 'killed_by=' + entry.killedBy, 'out=' + out.stdout + 'B err=' + out.stderr + 'B');
|
||||
res.end();
|
||||
}
|
||||
child.on('error', err => { entry.killedBy = entry.killedBy || 'spawn_error'; log('spawn error', id, err.message); streamChunk('err', Buffer.from('spawn 失败:' + err.message + '\n')); finish(-1); });
|
||||
child.on('close', (code) => finish(entry.killedBy ? -1 : (code === null ? -1 : code)));
|
||||
}
|
||||
|
||||
function handleKill(req, res, id) {
|
||||
const entry = runs.get(id);
|
||||
if (!entry) return send(res, 404, { error: '没有该执行任务(可能已结束)' });
|
||||
entry.killedBy = 'cancelled';
|
||||
killGroup(entry);
|
||||
send(res, 200, { ok: true, id });
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url, 'http://x');
|
||||
const seg = url.pathname.split('/').filter(Boolean);
|
||||
|
||||
if (url.pathname === '/health') return send(res, 200, { ok: true, time: Date.now(), running: Array.from(runs.entries()).map(([id, e]) => ({ id, pid: e.child.pid, cwd: e.cwd, started_at: e.started_at })) });
|
||||
if (!tokenOk(req)) return send(res, 401, { error: '缺少或错误的 Authorization: Bearer token' });
|
||||
|
||||
try {
|
||||
if (req.method === 'POST' && url.pathname === '/exec') return await handleExec(req, res);
|
||||
if (req.method === 'DELETE' && seg.length === 2 && seg[0] === 'exec') return handleKill(req, res, seg[1]);
|
||||
} catch (e) {
|
||||
log('处理异常:', e.message);
|
||||
if (!res.headersSent) send(res, 500, { error: e.message }); else res.end();
|
||||
return;
|
||||
}
|
||||
send(res, 404, { error: '未知路由 ' + req.method + ' ' + url.pathname });
|
||||
});
|
||||
|
||||
server.listen(PORT, () => {
|
||||
log('listening on ' + PORT + ' roots=' + ROOTS.join(', ') + ' default_cwd=' + DEFAULT_CWD + ' auth=' + (TOKEN ? 'token' : 'off'));
|
||||
});
|
||||
|
||||
// 退出前杀掉所有在跑的进程组,防容器停止后遗留
|
||||
function shutdown(sig) {
|
||||
log('收到 ' + sig + ',停止服务(杀掉 ' + runs.size + ' 个运行中任务)');
|
||||
for (const entry of runs.values()) { entry.killedBy = 'shutdown'; killGroup(entry); }
|
||||
server.close(() => process.exit(0));
|
||||
setTimeout(() => process.exit(0), 3000).unref();
|
||||
}
|
||||
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
||||
process.on('SIGINT', () => shutdown('SIGINT'));
|
||||
@@ -0,0 +1,97 @@
|
||||
// agent-executor 冒烟测试:node smoke-test.js(在本目录执行)。
|
||||
// 覆盖:/health、/exec 流式输出与退出事件、增量下发时序、cwd 白名单拒绝、DELETE 取消、EXECUTOR_TOKEN 鉴权。
|
||||
process.env.PORT = process.env.PORT || '19000';
|
||||
process.env.EXECUTOR_ROOTS = process.env.__SELF_DIR__ || __dirname; // 宿主机测试:白名单取本目录
|
||||
process.env.EXECUTOR_CWD = process.env.__SELF_DIR__ || __dirname;
|
||||
delete process.env.EXECUTOR_TOKEN; // 主实例不带 token,鉴权单独测
|
||||
|
||||
const { spawn } = require('child_process');
|
||||
require('./server.js');
|
||||
|
||||
const BASE = 'http://127.0.0.1:' + process.env.PORT;
|
||||
let failed = 0;
|
||||
function check(name, cond, extra) {
|
||||
console.log((cond ? 'PASS' : 'FAIL') + ' ' + name + (cond || !extra ? '' : ' → ' + extra));
|
||||
if (!cond) failed++;
|
||||
}
|
||||
|
||||
// 把 SSE 响应解析成事件对象异步流
|
||||
async function* sse(res) {
|
||||
const dec = new TextDecoder();
|
||||
let buf = '';
|
||||
for await (const chunk of res.body) {
|
||||
buf += dec.decode(chunk, { stream: true });
|
||||
let i;
|
||||
while ((i = buf.indexOf('\n\n')) >= 0) {
|
||||
const msg = buf.slice(0, i); buf = buf.slice(i + 2);
|
||||
for (const line of msg.split('\n')) {
|
||||
if (line.startsWith('data: ')) yield JSON.parse(line.slice(6));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
async function collect(res) { const evs = []; for await (const e of sse(res)) evs.push(e); return evs; }
|
||||
|
||||
async function main() {
|
||||
// 1. health
|
||||
const h = await (await fetch(BASE + '/health')).json();
|
||||
check('GET /health', h.ok === true);
|
||||
|
||||
// 2. 基本执行:stdout/stderr/exit,且增量分块下发(a 与 b 之间 sleep,验证非攒到最后)
|
||||
const t0 = Date.now();
|
||||
const r2 = await fetch(BASE + '/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'echo a; sleep 0.6; echo b; echo e1 >&2; exit 3' }) });
|
||||
const evs2 = [];
|
||||
const marks = [];
|
||||
for await (const e of sse(r2)) { evs2.push(e); if (e.type === 'out') marks.push(Date.now() - t0); }
|
||||
check('POST /exec start 事件', evs2[0] && evs2[0].type === 'start' && evs2[0].pid > 0);
|
||||
check('stdout 增量两条', evs2.filter(e => e.type === 'out').length === 2, JSON.stringify(evs2));
|
||||
check('间隔体现流式(>300ms)', marks.length === 2 && marks[1] - marks[0] > 300, 'marks=' + marks);
|
||||
check('stderr 事件', evs2.some(e => e.type === 'err' && e.chunk.trim() === 'e1'));
|
||||
const exit2 = evs2[evs2.length - 1];
|
||||
check('exit 事件带退出码 3', exit2 && exit2.type === 'exit' && exit2.code === 3, JSON.stringify(exit2));
|
||||
|
||||
// 3. cwd 白名单:/etc 必须拒绝
|
||||
const r3 = await fetch(BASE + '/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'pwd', cwd: '/etc' }) });
|
||||
check('cwd 越权 400', r3.status === 400, 'status=' + r3.status);
|
||||
|
||||
// 4. 取消:sleep 30 启动后立即 DELETE
|
||||
const r4 = await fetch(BASE + '/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'sleep 30' }) });
|
||||
let id4 = null; const evs4 = [];
|
||||
for await (const e of sse(r4)) { evs4.push(e); if (e.type === 'start' && !id4) { id4 = e.id; const k = await fetch(BASE + '/exec/' + id4, { method: 'DELETE' }); check('DELETE /exec/:id 返回 ok', (await k.json()).ok === true); } }
|
||||
const exit4 = evs4[evs4.length - 1];
|
||||
check('取消后 exit killed_by=cancelled', exit4 && exit4.type === 'exit' && exit4.killed_by === 'cancelled', JSON.stringify(exit4));
|
||||
|
||||
// 5. 超时自杀
|
||||
const t5 = Date.now();
|
||||
const r5 = await fetch(BASE + '/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'sleep 30', timeout_ms: 600 }) });
|
||||
const evs5 = await collect(r5);
|
||||
const exit5 = evs5[evs5.length - 1];
|
||||
check('超时 killed_by=timeout', exit5 && exit5.killed_by === 'timeout', JSON.stringify(exit5));
|
||||
check('超时耗时接近设定值', Date.now() - t5 < 3000, (Date.now() - t5) + 'ms');
|
||||
|
||||
// 6. 输出截断
|
||||
const r6 = await fetch(BASE + '/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'printf x%.0s $(seq 1 500); echo', max_output_chars: 100 }) });
|
||||
const evs6 = await collect(r6);
|
||||
const outChars = evs6.filter(e => e.type === 'out').reduce((s, e) => s + e.chunk.length, 0);
|
||||
const exit6 = evs6[evs6.length - 1];
|
||||
check('输出按 max_output_chars 截断', outChars <= 100 && exit6.truncated === true, 'chars=' + outChars + ' exit=' + JSON.stringify(exit6));
|
||||
|
||||
// 7. 未知路由
|
||||
check('未知路由 404', (await fetch(BASE + '/nope')).status === 404);
|
||||
|
||||
// 8. token 鉴权:另起一个带 EXECUTOR_TOKEN 的实例(子进程,端口 19001)
|
||||
const child = spawn(process.execPath, [__dirname + '/server.js'], { env: Object.assign({}, process.env, { PORT: '19001', EXECUTOR_TOKEN: 't9' }), stdio: 'ignore' });
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
const noTok = await fetch('http://127.0.0.1:19001/exec', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ command: 'echo hi' }) });
|
||||
check('无 token 401', noTok.status === 401, 'status=' + noTok.status);
|
||||
const healthNoTok = await fetch('http://127.0.0.1:19001/health');
|
||||
check('带 token 时 /health 仍免鉴权', healthNoTok.status === 200);
|
||||
const withTok = await fetch('http://127.0.0.1:19001/exec', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer t9' }, body: JSON.stringify({ command: 'echo hi' }) });
|
||||
const evs8 = await collect(withTok);
|
||||
check('带 token 正常执行', evs8.some(e => e.type === 'out' && e.chunk.trim() === 'hi'), JSON.stringify(evs8));
|
||||
child.kill('SIGKILL');
|
||||
|
||||
console.log(failed ? '\n' + failed + ' 项失败' : '\n全部通过');
|
||||
process.exit(failed ? 1 : 0);
|
||||
}
|
||||
main().catch(e => { console.error('测试异常:', e); process.exit(1); });
|
||||
Reference in New Issue
Block a user