修复:技能远端执行的 cwd 落到会话目录,技能看不到会话文件

- runSkill 远端分支 cwd 由技能目录改为会话任务目录(ctx.taskDir),命中 executor 的 session 隔离与虚拟视图:技能在 /workspace/tasks/<sid> 看到的即本会话数据,absolutizeArgs 产出的绝对路径因此有效。此前 cwd 在任务根外走 plain 模式,/workspace/tasks 是镜像空目录,md2pdf/img-utils 这类读写会话文件的技能实际拿不到文件。本地分支行为不变。

- 入口脚本改传绝对路径(run.py/run.sh):cwd 不再是技能目录,相对入口会找不到。

- 依赖安装 ensureDeps 仍以技能目录为 cwd(依赖是技能私有资源,skills 为 :ro)。

- remote-exec 集成测试改为模拟真实会话(拆分虚拟路径/宿主机真实会话/隔离基路径),新增文件读写回归用例;同步 skills/README 与 ARCHITECTURE。
This commit is contained in:
2026-09-11 19:58:30 +08:00
parent 2feda4a210
commit 4e8560982f
4 changed files with 62 additions and 18 deletions
+11 -5
View File
@@ -227,7 +227,8 @@ function validateArgs(fm, inputArgs, skillName) {
// 前台执行技能入口,带超时与取消支持;成功 resolve stdout,失败 reject SkillExecutionError。
// 配了 executor:入口命令拼成单条 shell 行(参数单引号转义,JSON payload 安全)交远端执行,
// cwd 指向技能目录、PYTHONPATH 指向共享 deps;core 与 executor 同路径挂载 skills,无需翻译。
// cwd 由调用方给出(run_skill 传会话目录以命中 session 隔离与虚拟视图;依赖安装仍传技能目录),
// PYTHONPATH 指向共享 deps;core 与 executor 同路径挂载 skills,无需翻译。
function runEntry(ctx, entry, args, cwd, timeoutMs) {
const cfg = ctx.cfg;
if (cfg && rexec.isRemote(cfg)) {
@@ -314,18 +315,23 @@ async function runSkill(ctx, skillName, inputArgs) {
const runSh = path.join(dir, 'run.sh');
if (fs.existsSync(runPy)) {
// PYTHONPATH 已指向共享 deps,直接走系统 python3。
// 入口一律用绝对路径:远端 cwd 是会话目录,相对的 'run.py' 会找不到。
entry = 'python3';
cmdArgs = ['run.py', payload];
cmdArgs = [runPy, payload];
} else if (fs.existsSync(runSh)) {
entry = 'bash';
cmdArgs = ['run.sh', payload];
cmdArgs = [runSh, payload];
} else {
throw new SkillExecutionError('技能 ' + skillName + ' 缺少入口 run.py 或 run.sh', 1);
}
// cwd:远端取会话目录(executor 侧命中 session 隔离,技能看到的 /workspace/tasks/<sid>
// 与 core 侧是同一份数据,absolutizeArgs 产出的绝对路径因此有效);缺 taskDir 时退回
// 技能目录(退化为旧行为)。本地无隔离诉求,仍以技能目录为 cwd。
const execCwd = rexec.isRemote(ctx.cfg) ? (ctx.taskDir || dir) : dir;
const timeoutMs = (ctx.cfg && ctx.cfg.skills && ctx.cfg.skills.timeout_ms) || 120000;
log.info('skills', '执行 ' + skillName + ':' + entry + ' ' + cmdArgs.join(' '));
return runEntry(ctx, entry, cmdArgs, dir, timeoutMs);
log.info('skills', '执行 ' + skillName + ':' + entry + ' ' + cmdArgs.join(' ') + '(cwd=' + execCwd + ')');
return runEntry(ctx, entry, cmdArgs, execCwd, timeoutMs);
}
module.exports = { listSkills, getSkillInfo, runSkill, skillsRoot, parseFrontMatter, extractDescription, formatSkillListForPrompt };
+44 -11
View File
@@ -1,22 +1,47 @@
// 集成测试:core 的 terminal 工具 / runSkill 走远端 executor(同机起 executor 于 19000)。
// 需要 Linux + CAP_SYS_ADMIN:executor 隔离用 unshare/mount,macOS 上无法运行。
//
// 拓扑模拟两个容器共享同一份宿主机任务目录:
// REAL (/ISO/<sid>) —— 宿主机真实会话数据(等价 compose 的 ./tasks/<sid>)
// VIRT (ROOTS/tasks/<sid>) —— core 与 executor ns 里看到的虚拟会话路径
// session 模式下 wrapper 会遮 ROOTS/tasks 再把 REAL bind 到 VIRT,故断言写盘要查 REAL。
const path = require('path');
const fs = require('fs');
const os = require('os');
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');
const ROOTS = path.join(TMP, 'ws'); // EXECUTOR_ROOTS
const TASKS = path.join(ROOTS, 'tasks'); // EXECUTOR_CWD(虚拟任务根)
const SKILLS = path.join(ROOTS, 'skills');
const SID = 'sess1';
const VIRT = path.join(TASKS, SID); // core 侧会话目录(虚拟路径)
// ISOLATION_BASE 与 ROOTS 无父子关系;其父目录 MNTROOT 是独立目录,遮盖不会波及 ROOTS/系统 /tmp。
const ISOROOT = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-exec-iso-'));
const ISO = path.join(ISOROOT, 'host-tasks');
const REAL = path.join(ISO, SID); // 宿主机真实会话数据
fs.rmSync(TMP, { recursive: true, force: true });
fs.mkdirSync(TASK, { recursive: true });
fs.mkdirSync(VIRT, { recursive: true });
fs.mkdirSync(REAL, { 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');
// 文件读写技能:验证技能进程在会话隔离视图里能按 absolutizeArgs 产出的绝对路径读/写会话数据。
fs.mkdirSync(path.join(SKILLS, 'fileio'), { recursive: true });
fs.writeFileSync(path.join(SKILLS, 'fileio/SKILL.md'), '---\ndescription: 文件读写\n---\n');
fs.writeFileSync(path.join(SKILLS, 'fileio/run.sh'),
'#!/bin/sh\nPAYLOAD="$1" node -e \'const fs=require("fs");const a=JSON.parse(process.env.PAYLOAD);' +
'fs.writeFileSync(a.output,fs.readFileSync(a.input,"utf8").toUpperCase());console.log("OK "+a.output);\'\n');
// 1) executor 服务进程内启动
process.env.PORT = '19000';
process.env.EXECUTOR_ROOTS = TMP;
process.env.EXECUTOR_CWD = TASK;
process.env.EXECUTOR_ROOTS = ROOTS;
process.env.EXECUTOR_CWD = TASKS;
process.env.EXECUTOR_ISOLATION_BASE = ISO;
process.env.EXECUTOR_SKILLS_DIR = SKILLS;
delete process.env.EXECUTOR_URL; delete process.env.EXECUTOR_TOKEN;
delete process.env.EXECUTOR_LOG_LEVEL;
require(path.join(ROOT, 'executor/server.js'));
// 2) core 侧模块
@@ -27,7 +52,7 @@ 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 } };
return { cfg, sessionId: SID, taskDir: VIRT, 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++; };
@@ -36,12 +61,13 @@ const sleep = ms => new Promise(r => setTimeout(r, ms));
async function main() {
await sleep(200);
// --- terminal:基本输出 / 退出码 / 文件落在共享目录 ---
// --- 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')));
check('输出含远端结果且 cwd 为虚拟会话路径', r.includes('远程ok') && r.includes(VIRT) && r.includes('[exit code: 0]'), r);
check('文件写入真实会话目录', fs.existsSync(path.join(REAL, 'out.txt')));
check('虚拟路径本身未被写穿(隔离生效)', !fs.existsSync(path.join(VIRT, 'out.txt')));
// --- 非零退出码 ---
r = await toolExec.executeTool(ctx, 'terminal', { command: 'exit 7' });
@@ -70,7 +96,7 @@ async function main() {
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 ---
// --- runSkill:入口脚本 + JSON payload 引号 + PYTHONPATH(cwd 落在会话目录)---
ctx = mkCtx('skills:\n skills_root: "' + SKILLS + '"\n');
const msg = "带 空格 和'单引号' 与\"双引号\"及中文";
let sr = await skills.runSkill(ctx, 'echo', { msg });
@@ -78,10 +104,17 @@ async function main() {
check('runSkill 远端执行成功', sr.includes(want), sr);
check('PYTHONPATH 指向共享 deps', sr.includes('pp=' + path.join(SKILLS, 'deps')), sr);
// --- runSkill 文件读写:绝对路径在会话隔离视图里可用(修复的核心回归点)---
fs.writeFileSync(path.join(REAL, 'in.txt'), 'hello');
sr = await skills.runSkill(ctx, 'fileio', { input: 'in.txt', output: 'out.txt' });
check('技能读到会话输入文件并成功执行', sr.includes('OK ' + path.join(VIRT, 'out.txt')), sr);
check('技能输出写回真实会话目录且内容正确',
fs.existsSync(path.join(REAL, 'out.txt')) && fs.readFileSync(path.join(REAL, 'out.txt'), 'utf8') === 'HELLO');
// --- 传输失败: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 } };
const ctxBad = { cfg: cfgBad, sessionId: SID, taskDir: VIRT, 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);