core 接入 agent-executor:terminal 工具与技能执行/依赖安装支持远端执行(cfg.executor.url 或 EXECUTOR_URL/TOKEN 环境变量,SSE 收流、run.procs 注册 remoteKill 句柄与取消/超时联动、engine.killRunProcs 兼容);设置页服务状态显示命令执行方式;附 remote-exec 集成测试

This commit is contained in:
2026-09-06 08:21:41 +08:00
parent 232f768783
commit 548ce651f6
10 changed files with 269 additions and 4 deletions
+9 -1
View File
@@ -40,7 +40,15 @@ workspace_root: "./data/tasks"
# 数据根目录:db(默认 .agent.db)与启动 prompt dump(prompt.txt)放这里;相对路径按启动目录展开。
# 容器部署建议指到挂载卷内(如 /workspace/data),否则重建容器会丢数据。
data_root: "./data"
# db_path: "/some/where/.agent.db" # 显式配置优先;缺省为 data_root/.agent.db
# db_path: "/workspace/data/.agent.db" # 显式配置优先;缺省为 data_root/.agent.db
# agent-executor(独立命令执行容器,见根目录 executor/):url 非空时 terminal 工具与技能命令
# 都发到该服务执行(SSE 实时回传),core 镜像不再携带 python 等重工具链;
# 留空则本地 spawn(裸机开发)。两侧需同路径挂载 ./tasks、./skills;
# compose 已用环境变量 EXECUTOR_URL/EXECUTOR_TOKEN 注入,通常无需在这里配。
# executor:
# url: "http://agent-executor:9000"
# token: "change-me" # 选填;executor 侧配同值才校验
# 日志等级(改后重启生效):debug < info < warn < error < none,只输出 ≥ 该等级的日志;none=全部静默。
# info(默认)=关键动作概述;debug 另加用户/AI 消息全文、工具调用参数/结果等排查细节。
+9
View File
@@ -10,6 +10,10 @@ const DEFAULTS = {
data_root: './data',
workspace_root: './data/tasks',
db_path: '', // 默认为 data_root/.agent.db
// agent-executor(独立命令执行容器):url 非空时 terminal 工具与技能命令全部改走远端执行
// (core 镜像不再携带 python/libreoffice 等重工具链);留空则本地 spawn(裸机开发兼容)。
// 配置留空时退到环境变量 EXECUTOR_URL / EXECUTOR_TOKEN(compose 注入)。
executor: { url: '', token: '' },
terminal: { default_timeout_ms: 30000, max_output_chars: 10000 },
agent: { temperature: 1.0, max_tool_iterations: 50, max_retries: 2, language: '中文' },
// 大图上传预处理:图片文件超过 threshold_kb 时,浏览器端询问用户后用 canvas 压缩
@@ -161,6 +165,11 @@ function parseAndValidate(rawText, configPath) {
cfg.data_root = path.resolve(cfg.data_root);
if (!cfg.db_path) cfg.db_path = path.join(cfg.data_root, '.agent.db');
// executor 远程执行:配置优先,env 兑底(compose 注入 EXECUTOR_URL/EXECUTOR_TOKEN);url 去尾部斜杠(拼 /exec 用)。
if (!cfg.executor || typeof cfg.executor !== 'object') cfg.executor = {};
cfg.executor.url = String(cfg.executor.url || process.env.EXECUTOR_URL || '').trim().replace(/\/+$/, '');
cfg.executor.token = String(cfg.executor.token || process.env.EXECUTOR_TOKEN || '').trim();
// 日志等级:只认 debug/info/warn/error/none(off 是 none 别名),非法值按 info(不影响启动)
cfg.log_level = String(cfg.log_level || 'info').trim().toLowerCase();
if (cfg.log_level === 'off') cfg.log_level = 'none';
+2
View File
@@ -371,6 +371,8 @@ function activeRuns() {
function killRunProcs(run) {
for (const proc of run.procs) {
// 远端 executor 任务句柄(remote-exec.js):发 DELETE 取消,不能本地 kill。
if (typeof proc.remoteKill === 'function') { try { proc.remoteKill(); } catch (_) {} continue; }
try { process.kill(-proc.pid, 'SIGKILL'); } catch (_) {
try { proc.kill('SIGKILL'); } catch (__) {}
}
+1
View File
@@ -2677,6 +2677,7 @@
html += kvPlain('工作区', st.workspace_root);
html += kvPlain('数据目录', st.data_root);
html += kvPlain('技能目录', st.skills_root);
html += kvPlain('命令执行', st.executor_url ? st.executor_url + '(远端)' : '本地');
if (SET.pendingKeys.size) html += kvPlain('待生效改动', Array.from(SET.pendingKeys).join(', '));
html += '<button class="primary-btn" id="restart-btn" style="margin-top:8px" onclick="sUI.restart()">重启服务</button>' +
'<div class="cfg-note">配置改动重启后生效;重启约需十几秒,期间页面暂不可用。非 Docker 部署不会自动拉起。</div></div>';
+1
View File
@@ -113,6 +113,7 @@ function svcCard(st) {
html += kvPlain('工作区', st.workspace_root);
html += kvPlain('数据目录', st.data_root);
html += kvPlain('技能目录', st.skills_root);
html += kvPlain('命令执行', st.executor_url ? st.executor_url + '(远端)' : '本地');
if (pendingKeys.size) html += kvPlain('待生效改动', Array.from(pendingKeys).join(', '));
html += '<button class="primary-btn" id="restart-btn" style="width:100%;margin-top:8px" onclick="cfgUI.restart()">重启服务</button>' +
'<div class="cfg-note">配置改动重启后生效;重启约需十几秒,期间页面暂不可用。非 Docker 部署不会自动拉起。</div></div>';
+100
View File
@@ -0,0 +1,100 @@
// agent-executor 远程执行客户端:cfg.executor.url 非空时,terminal 工具与技能命令改走远端 executor 容器执行。
// 协议见仓库根 executor/server.js(SSE:start → out/err* → exit)。
// 与本地 spawn 的行为对齐:stdout+stderr 按到达顺序合并成一个 out 字符串;超时/取消杀远端进程组;
// 通过 run.procs 注册句柄(remoteKill),使 engine 的停止/终止会话能连带取消远端任务。
'use strict';
// 单引号安全包裹(内部单引号按 '\'' 规则转义),供拼 sh -c 命令串用。
function shq(s) { return "'" + String(s).replace(/'/g, "'\\''") + "'"; }
// cmd + args 数组 → 一条 shell 命令行(技能入口、pip 安装等都走它,避免 JSON 参数里的引号被吃掉)。
function shellLine(cmd, args) { return [cmd].concat(args || []).map(shq).join(' '); }
function isRemote(cfg) { return !!(cfg && cfg.executor && cfg.executor.url); }
function reqHeaders(cfg) {
const h = { 'Content-Type': 'application/json' };
if (cfg.executor.token) h.Authorization = 'Bearer ' + cfg.executor.token;
return h;
}
// 执行一条命令。返回 { code, killed_by, out };传输层失败抛 Error(由调用方决定如何呈现)。
// opts: { command, cwd?, timeout_ms?, env?, run? }(run = engine 的 {cancelled, procs},可省略)
async function run(cfg, opts) {
const base = cfg.executor.url;
const timeoutMs = opts.timeout_ms && opts.timeout_ms > 0 ? opts.timeout_ms : 300000;
let id = null; // executor 的 start 事件里下发
let out = '';
let exit = null; // exit 事件
let localKill = null; // 'cancelled' | 'timeout'(本地判定的终止原因)
const ac = new AbortController();
function killRemote() {
if (!id) return;
fetch(base + '/exec/' + id, { method: 'DELETE', headers: reqHeaders(cfg), signal: AbortSignal.timeout(5000) }).catch(() => {});
}
const handle = {
remoteKill() {
if (!localKill) localKill = 'cancelled';
killRemote();
ac.abort();
},
};
const runCtx = opts.run;
if (runCtx && runCtx.procs) runCtx.procs.add(handle);
// 取消轮询与本地看门狗(executor 自身超时兜底,+15s 仍未见 exit 才动手)。
const cancelTimer = runCtx ? setInterval(() => { if (runCtx.cancelled) handle.remoteKill(); }, 200) : null;
const watchdog = setTimeout(() => { if (!exit) { if (!localKill) localKill = 'timeout'; handle.remoteKill(); } }, timeoutMs + 15000);
try {
const res = await fetch(base + '/exec', {
method: 'POST',
headers: reqHeaders(cfg),
body: JSON.stringify({ command: opts.command, cwd: opts.cwd, timeout_ms: timeoutMs, env: opts.env }),
signal: ac.signal,
});
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { const j = await res.json(); if (j && j.error) msg = j.error; } catch (_) {}
throw new Error('executor 拒绝请求:' + msg);
}
// SSE 帧手工解析:每帧以空行分隔,data: 行是 JSON 事件;": ping" 注释行忽略。
const dec = new TextDecoder();
let buf = '';
outer: for await (const chunk of res.body) {
buf += dec.decode(chunk, { stream: true });
let i;
while ((i = buf.indexOf('\n\n')) >= 0) {
const frame = buf.slice(0, i);
buf = buf.slice(i + 2);
for (const line of frame.split('\n')) {
if (!line.startsWith('data: ')) continue;
let ev;
try { ev = JSON.parse(line.slice(6)); } catch (_) { continue; }
if (ev.type === 'start') id = ev.id;
else if (ev.type === 'out' || ev.type === 'err') out += ev.chunk;
else if (ev.type === 'exit') { exit = ev; break outer; }
}
}
}
} catch (e) {
if (e && (e.name === 'AbortError' || localKill)) {
// 取消/看门狗路径:不算错误,合成 exit 结果返回。
} else {
throw new Error('executor 调用失败(' + base + '):' + (e.cause ? e.cause.message : e.message || String(e)));
}
} finally {
clearTimeout(watchdog);
if (cancelTimer) clearInterval(cancelTimer);
if (runCtx && runCtx.procs) runCtx.procs.delete(handle);
if (!exit && localKill) killRemote(); // 兜底再杀一次(abort 早于 start 到达时 id 可能还没拿到)
}
if (!exit) {
if (localKill) return { code: -1, killed_by: localKill, out };
throw new Error('executor 连接中断(任务 ' + (id || '?') + ' 结果未知,远端可能仍在执行)');
}
return { code: exit.code, killed_by: exit.killed_by || localKill, out, truncated: !!exit.truncated };
}
module.exports = { isRemote, run, shq, shellLine };
+1
View File
@@ -43,6 +43,7 @@ router.get('/status', wrap((req, res) => {
data_root: cfg.data_root,
skills_root: cfg.skills_root,
db_path: cfg.db_path,
executor_url: (cfg.executor && cfg.executor.url) || '',
port: cfg.port,
auth_enabled: !!(cfg.auth && cfg.auth.users && cfg.auth.users.length),
running_sessions: engine.activeRuns(),
+16
View File
@@ -7,6 +7,7 @@ const { spawn } = require('child_process');
const { Minimatch } = require('minimatch');
const mcp = require('../mcp');
const skills = require('./skills');
const rexec = require('../remote-exec');
function makeMatcher(pattern, opts) {
const mm = new Minimatch(pattern, opts || {});
@@ -549,6 +550,21 @@ async function toolTerminal(ctx, args) {
if (args.cd) cwd = resolveInTask(ctx.taskDir, args.cd);
if (!fs.existsSync(cwd)) throw new Error('工作目录不存在:' + cwd);
// 配了 executor.url(或 env EXECUTOR_URL):命令交给远端 agent-executor 执行,文本拼回与本地完全一致。
if (rexec.isRemote(cfg)) {
const r = await rexec.run(cfg, { command: args.command, cwd, timeout_ms: timeout, run: ctx.run });
let text = selectOutput(r.out.replace(/\n+$/, ''), args.head_lines, args.tail_lines);
if (r.killed_by === 'timeout') {
text += (text ? '\n' : '') + '(命令超时 ' + timeout + 'ms,已终止;这是超时不是命令本身失败)';
} else if (r.killed_by === 'cancelled') {
text += (text ? '\n' : '') + '(用户终止任务,进程被 kill)';
} else {
text += (text ? '\n' : '') + '[exit code: ' + r.code + ']';
}
if (r.truncated) text += '\n(输出超长,已在 executor 侧截断)';
return truncate(text || '(无输出)', maxChars);
}
return new Promise((resolve) => {
const child = spawn('/bin/sh', ['-c', args.command], { cwd, detached: true, env: process.env });
ctx.run.procs.add(child);
+33 -3
View File
@@ -9,6 +9,7 @@ const crypto = require('crypto');
const { spawn } = require('child_process');
const YAML = require('yaml');
const log = require('../log');
const rexec = require('../remote-exec');
class SkillNotFoundError extends Error {
constructor(msg) { super(msg); this.name = 'SkillNotFoundError'; }
@@ -112,8 +113,22 @@ function hashFile(p) {
// 在技能目录下执行一个前台命令,返回 stdout+stderr;非零退出抛 DependencyInstallError。
// 安装命令兼有硬超时(默认 10 分钟),防 pip/apt 挂死阻塞会话。
function execSyncCapture(cmd, args, cwd, timeoutMs) {
// 配了 executor:命令拼成单条 shell 行交远端执行(core 镜像已不带 python/pip)。
// 注意传 PYTHONPATH=共享 deps:远端进程原有 PYTHONPATH 会被覆盖(executor 容器不预装到 site-packages,仅影响技能依赖可见性)。
function execSyncCapture(ctx, cmd, args, cwd, timeoutMs) {
const limit = timeoutMs || 600000;
const cfg = ctx && ctx.cfg;
if (cfg && rexec.isRemote(cfg)) {
return rexec.run(cfg, { command: rexec.shellLine(cmd, args), cwd, timeout_ms: limit, env: { PYTHONPATH: depsRoot(ctx) } })
.then(r => {
if (r.killed_by === 'timeout') throw new DependencyInstallError('依赖安装超时(' + limit + 'ms)已终止:\n' + r.out.trim(), -1);
if (r.code === 0) return r.out.trim();
throw new DependencyInstallError((r.out.trim() || ('依赖命令退出码 ' + r.code)) + ' [exit: ' + r.code + ']', r.code);
}, e => {
if (e instanceof DependencyInstallError) throw e;
throw new DependencyInstallError('依赖命令远程执行失败:' + e.message, -1);
});
}
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, { cwd, env: process.env, detached: true });
let out = '';
@@ -162,11 +177,11 @@ async function ensureDeps(ctx, dir, skillName) {
const target = depsRoot(ctx);
fs.mkdirSync(target, { recursive: true });
log.debug('skills', skillName + ':pip install --target ' + target);
await execSyncCapture('python3', ['-m', 'pip', 'install', '--target', target, '-r', reqPath], dir);
await execSyncCapture(ctx, 'python3', ['-m', 'pip', 'install', '--target', target, '-r', reqPath], dir);
}
if (hasInstall) {
log.debug('skills', skillName + ':执行 install.sh');
await execSyncCapture('bash', ['install.sh'], dir);
await execSyncCapture(ctx, 'bash', ['install.sh'], dir);
}
} catch (e) {
if (e instanceof DependencyInstallError) throw e;
@@ -211,7 +226,22 @@ function validateArgs(fm, inputArgs, skillName) {
}
// 前台执行技能入口,带超时与取消支持;成功 resolve stdout,失败 reject SkillExecutionError。
// 配了 executor:入口命令拼成单条 shell 行(参数单引号转义,JSON payload 安全)交远端执行,
// cwd 指向技能目录、PYTHONPATH 指向共享 deps;core 与 executor 同路径挂载 skills,无需翻译。
function runEntry(ctx, entry, args, cwd, timeoutMs) {
const cfg = ctx.cfg;
if (cfg && rexec.isRemote(cfg)) {
return rexec.run(cfg, {
command: rexec.shellLine(entry, args), cwd, timeout_ms: timeoutMs,
env: { PYTHONPATH: depsRoot(ctx) }, run: ctx.run,
}).then(r => {
const trimmed = r.out.replace(/\n+$/, '').trim();
if (r.killed_by === 'timeout') throw new SkillExecutionError('技能执行超时(' + timeoutMs + 'ms)已终止:\n' + trimmed, -1);
if (r.killed_by === 'cancelled') throw new SkillExecutionError('技能执行被用户终止', -1);
if (r.code === 0) return trimmed || '(无输出)';
throw new SkillExecutionError((trimmed || ('技能执行失败,退出码 ' + r.code)) + '\n[exit code: ' + r.code + ']', r.code);
});
}
return new Promise((resolve, reject) => {
const child = spawn(entry, args, { cwd, env: skillEnv(ctx), detached: true });
if (ctx.run && ctx.run.procs) ctx.run.procs.add(child);
+97
View File
@@ -0,0 +1,97 @@
// 集成测试:core 的 terminal 工具 / runSkill 走远端 executor(同机起 executor 于 19000)。
const path = require('path');
const fs = require('fs');
const ROOT = path.resolve(__dirname, '..', '..'); // agent 仓库根
const TMP = path.join(ROOT, 'app/test/tmp/remote-exec');
const TASK = path.join(TMP, 'task');
const SKILLS = path.join(TMP, 'skills');
fs.rmSync(TMP, { recursive: true, force: true });
fs.mkdirSync(TASK, { recursive: true });
fs.mkdirSync(path.join(SKILLS, 'echo'), { recursive: true });
fs.writeFileSync(path.join(SKILLS, 'echo/SKILL.md'), '---\ndescription: 回显参数\n---\n回显\n');
fs.writeFileSync(path.join(SKILLS, 'echo/run.sh'), '#!/bin/sh\necho "payload=$1"\necho "pp=$PYTHONPATH"\n');
// 1) executor 服务进程内启动
process.env.PORT = '19000';
process.env.EXECUTOR_ROOTS = TMP;
process.env.EXECUTOR_CWD = TASK;
delete process.env.EXECUTOR_URL; delete process.env.EXECUTOR_TOKEN;
require(path.join(ROOT, 'executor/server.js'));
// 2) core 侧模块
const { loadConfigFromText } = require(path.join(ROOT, 'app/server/config'));
const toolExec = require(path.join(ROOT, 'app/server/tools/executor'));
const skills = require(path.join(ROOT, 'app/server/tools/skills'));
const yml = (extra) => 'platforms:\n - url: https://a.com/v1\n keys:\n - models:\n - id: m1\nexecutor:\n url: "http://127.0.0.1:19000"\n' + extra;
function mkCtx(cfgExtra) {
const cfg = loadConfigFromText(yml(cfgExtra), '');
return { cfg, taskDir: TASK, run: { cancelled: false, procs: new Set(), abortController: null, looping: false } };
}
let failed = 0;
const check = (n, c, e) => { console.log((c ? 'PASS' : 'FAIL') + ' ' + n + (c || !e ? '' : ' → ' + e)); if (!c) failed++; };
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function main() {
await sleep(200);
// --- terminal:基本输出 / 退出码 / 文件落在共享目录 ---
let ctx = mkCtx('');
check('executor.url 生效', ctx.cfg.executor.url === 'http://127.0.0.1:19000', ctx.cfg.executor.url);
let r = await toolExec.executeTool(ctx, 'terminal', { command: 'echo 远程ok; pwd; echo data > out.txt' });
check('输出含远端结果', r.includes('远程ok') && r.includes(TASK) && r.includes('[exit code: 0]'), r);
check('文件写入共享目录', fs.existsSync(path.join(TASK, 'out.txt')));
// --- 非零退出码 ---
r = await toolExec.executeTool(ctx, 'terminal', { command: 'exit 7' });
check('退出码透传', r.includes('[exit code: 7]'), r);
// --- head_lines 选段 ---
r = await toolExec.executeTool(ctx, 'terminal', { command: 'printf "l1\\nl2\\nl3\\n"', head_lines: 1 });
check('head_lines 生效', r.startsWith('l1\n[exit code: 0]'), JSON.stringify(r));
// --- 超时 ---
const t0 = Date.now();
r = await toolExec.executeTool(ctx, 'terminal', { command: 'sleep 20', timeout_ms: 800 });
check('远端超时文案', r.includes('命令超时 800ms'), r);
check('超时耗时受控(<5s)', Date.now() - t0 < 5000, (Date.now() - t0) + 'ms');
// --- 取消:模拟 engine 轮询/stop ---
ctx = mkCtx('');
const p = toolExec.executeTool(ctx, 'terminal', { command: 'sleep 20', timeout_ms: 15000 });
await sleep(400);
check('运行中注册 procs 句柄', ctx.run.procs.size === 1, ctx.run.procs.size);
// 同 engine.killRunProcs 对新句柄的处理(proc.remoteKill):
for (const proc of ctx.run.procs) if (typeof proc.remoteKill === 'function') proc.remoteKill();
r = await p;
check('取消后返回且文案一致', r.includes('用户终止任务'), r);
check('取消后 procs 清空', ctx.run.procs.size === 0);
const h = await (await fetch('http://127.0.0.1:19000/health')).json();
check('executor 无残留进程', h.running.length === 0, JSON.stringify(h.running));
// --- runSkill:入口脚本 + JSON payload 引号 + PYTHONPATH ---
ctx = mkCtx('skills:\n skills_root: "' + SKILLS + '"\n');
const msg = "带 空格 和'单引号' 与\"双引号\"及中文";
let sr = await skills.runSkill(ctx, 'echo', { msg });
const want = 'payload=' + JSON.stringify({ msg });
check('runSkill 远端执行成功', sr.includes(want), sr);
check('PYTHONPATH 指向共享 deps', sr.includes('pp=' + path.join(SKILLS, 'deps')), sr);
// --- 传输失败:executor 不可达 ---
const ymlBad = 'platforms:\n - url: https://a.com/v1\n keys:\n - models:\n - id: m1\nexecutor:\n url: "http://127.0.0.1:19999"\n';
const cfgBad = loadConfigFromText(ymlBad, '');
const ctxBad = { cfg: cfgBad, taskDir: TASK, run: { cancelled: false, procs: new Set(), abortController: null, looping: false } };
let threw = '';
try { await toolExec.executeTool(ctxBad, 'terminal', { command: 'echo x' }); } catch (e) { threw = e.message; }
check('不可达时报错而非静默', threw.includes('executor 调用失败'), threw);
// --- env 兜底:YAML 不配 url,靠 EXECUTOR_URL ---
process.env.EXECUTOR_URL = 'http://127.0.0.1:19000/';
const cfgEnv = loadConfigFromText('platforms:\n - url: https://a.com/v1\n keys:\n - models:\n - id: m1\n', '');
check('EXECUTOR_URL env 兜底+去尾斜杠', cfgEnv.executor.url === 'http://127.0.0.1:19000', cfgEnv.executor.url);
console.log(failed ? '\n' + failed + ' 项失败' : '\n全部通过');
process.exit(failed ? 1 : 0);
}
main().catch(e => { console.error('测试异常:', e); process.exit(1); });