功能:executor 镜像分层(基础 M + 技能系统层派生镜像)+ 启动前 skills 初始化
- executor/Dockerfile 改多阶段:base 为基础镜像 M(长期不变,系统工具 + 预烘 Python 包);skills 阶段 = M + 技能系统层依赖(构建期跑各 install.sh,apt 等系统包持久且对命令只读)。build context 改为仓库根,用 .dockerignore 只放行 executor/ 与 skills/。 - 新增 agent-executor-init 一次性服务(executor/init-skills.js,与 core ensureDeps 同 hash/marker 格式):启动前把 requirements.txt(pip --target 共享 deps)与 install.sh 资产落到宿主 ./skills;只挂 ./skills、无特权、read_only,跑完退出;executor depends_on service_completed_successfully。 - 运行期 skills 仍 :ro;M 不变、skills 可变、命令写不了共享系统层,会话隔离保持。 - 实测:构建 stage 两个 install.sh 正常;init 幂等跳过/按需重跑(install.sh 与 pip 分支各验一次);executor 重建后 ReadonlyRootfs=true、/run tmpfs、core 到 executor 健康、run_skill 端到端正常。docs/ARCHITECTURE.md 与 skills/README.md 同步。
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env node
|
||||
// agent-executor-init:executor 启动前的一次性技能准备。
|
||||
//
|
||||
// 只处理“运行期可变”的依赖:
|
||||
// - requirements.txt → python3 -m pip install --target <skills>/deps -r ...
|
||||
// - install.sh → 在技能目录执行(生成技能自带资产,如从系统字体提取 cjk.ttf)
|
||||
// 系统层依赖(apt 等)不在这里做——那部分由派生镜像 M'(executor/Dockerfile 的 skills 阶段)
|
||||
// 承载,才持久且对命令只读。
|
||||
//
|
||||
// 无特权:不做任何 mount;只挂 ./skills(不挂 ./tasks),因此看不到任何会话数据。
|
||||
// 幂等:按 requirements.txt/install.sh 的内容 hash(与 core 的 ensureDeps 同格式)比对
|
||||
// .deps_installed,命中即跳过。
|
||||
// 退出码恒为 0:单个技能失败只告警,不阻断 executor 启动(该技能暂不可用,其余照常)。
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
const ROOT = process.env.SKILLS_ROOT || '/workspace/skills';
|
||||
const DEPS = path.join(ROOT, 'deps');
|
||||
|
||||
function log() {
|
||||
const args = Array.prototype.slice.call(arguments);
|
||||
console.log('[' + new Date().toISOString() + '] [skills-init]', ...args);
|
||||
}
|
||||
|
||||
// 与 core(app/server/tools/skills.js 的 hashFile/ensureDeps)保持完全一致的 hash 与 marker 格式。
|
||||
function hashFile(p) {
|
||||
try { return crypto.createHash('sha1').update(fs.readFileSync(p)).digest('hex'); } catch (_) { return ''; }
|
||||
}
|
||||
|
||||
function run(cmd, args, cwd) {
|
||||
log('run:', cmd, args.join(' '), '(cwd=' + cwd + ')');
|
||||
const r = spawnSync(cmd, args, { cwd, stdio: 'inherit', env: process.env });
|
||||
if (r.error) { log('spawn 失败:' + r.error.message); return false; }
|
||||
return r.status === 0;
|
||||
}
|
||||
|
||||
function initSkill(name) {
|
||||
const dir = path.join(ROOT, name);
|
||||
const reqPath = path.join(dir, 'requirements.txt');
|
||||
const installPath = path.join(dir, 'install.sh');
|
||||
const hasReq = fs.existsSync(reqPath);
|
||||
const hasInstall = fs.existsSync(installPath);
|
||||
if (!hasReq && !hasInstall) return;
|
||||
|
||||
const need = 'req:' + hashFile(reqPath) + '|install:' + hashFile(installPath);
|
||||
const markerPath = path.join(dir, '.deps_installed');
|
||||
try {
|
||||
if (fs.existsSync(markerPath) && fs.readFileSync(markerPath, 'utf8') === need) {
|
||||
log('跳过(已就绪):' + name);
|
||||
return;
|
||||
}
|
||||
} catch (_) { /* marker 读失败按未就绪处理 */ }
|
||||
|
||||
log('初始化:' + name);
|
||||
let ok = true;
|
||||
try {
|
||||
if (hasReq) {
|
||||
fs.mkdirSync(DEPS, { recursive: true });
|
||||
ok = run('python3', ['-m', 'pip', 'install', '--no-cache-dir', '--target', DEPS, '-r', reqPath], dir) && ok;
|
||||
}
|
||||
if (hasInstall) {
|
||||
ok = run('bash', ['install.sh'], dir) && ok;
|
||||
}
|
||||
} catch (e) {
|
||||
log('异常:' + name + ':' + e.message);
|
||||
ok = false;
|
||||
}
|
||||
if (ok) {
|
||||
fs.writeFileSync(markerPath, need, 'utf8');
|
||||
log('就绪:' + name);
|
||||
} else {
|
||||
log('失败(不写 marker,下次启动重试):' + name);
|
||||
}
|
||||
}
|
||||
|
||||
(function main() {
|
||||
try {
|
||||
if (!fs.existsSync(ROOT)) { log('技能根目录不存在,跳过:' + ROOT); return; }
|
||||
const names = fs.readdirSync(ROOT, { withFileTypes: true })
|
||||
.filter(e => e.isDirectory() && e.name !== 'deps')
|
||||
.map(e => e.name)
|
||||
.sort();
|
||||
log('技能根=' + ROOT + ',共 ' + names.length + ' 个技能');
|
||||
for (const n of names) initSkill(n);
|
||||
log('初始化结束');
|
||||
} catch (e) {
|
||||
log('顶层异常(忽略,不阻断 executor):' + e.message);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user