@online-tools/work-agent-runner 0.0.2-alpha-1 → 0.0.2-alpha-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -11
- package/bin/main.mjs +30 -17
- package/lib/config.mjs +15 -7
- package/lib/jobs.mjs +6 -1
- package/lib/sandbox.mjs +42 -22
- package/lib/work-agent.mjs +65 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @online-tools/work-agent-runner
|
|
2
2
|
|
|
3
|
-
GitCode 在线工具的 Agent 执行机器:在你自己的机器上运行「在线 Agent」里的 work-agent Skill——**PR 描述 / Issue**、**代码检视(全量 /
|
|
3
|
+
GitCode 在线工具的 Agent 执行机器:在你自己的机器上运行「在线 Agent」里的 work-agent Skill——**PR 描述 / Issue**、**代码检视(全量 / 快速)**、**CANN 知识问答**,不占网站服务器的资源。Skill 由本程序自己从 GitCode 克隆 work-agent 仓库取得,网站只下发任务数据。
|
|
4
4
|
|
|
5
5
|
程序只**主动连出**到网站(HTTPS),适用于没有公网 IP、在 NAT 或公司代理后面的机器;网站从不主动连接你的机器。
|
|
6
6
|
|
|
@@ -30,7 +30,7 @@ work-agent-runner service install # 可选:注册为后台服务,开机
|
|
|
30
30
|
|
|
31
31
|
拒绝安装或安装失败时程序照样接入,缺少的依赖只让用到它的功能不可用,网页上会显示原因。之后可以随时运行 `work-agent-runner setup`。
|
|
32
32
|
|
|
33
|
-
`setup` 最后会问是否下载
|
|
33
|
+
`setup` 最后会问是否下载 **知识仓**(work-agent 的必要仓清单,见下)。不下载也能用。
|
|
34
34
|
|
|
35
35
|
在网页「PR 描述 / Issue」「代码检视」页的「执行位置」里选择这台机器(或在「模型配置」里设为默认),任务就会在这里运行。
|
|
36
36
|
|
|
@@ -60,21 +60,29 @@ work-agent-runner service install # 可选:注册为后台服务,开机
|
|
|
60
60
|
├── run.lock 单实例锁
|
|
61
61
|
├── work-agent/ work-agent 仓库的克隆 = Skill 来源(跟随 main)
|
|
62
62
|
├── work-agent.lock 克隆的锁(终端 update vs 后台服务)
|
|
63
|
-
├── .work-agent
|
|
64
|
-
│ ├── logs/ Skill 日志(任务跑完还在,可以查历史)
|
|
65
|
-
│ ├── tmp/ docs/ Skill 的中间产物和交付件
|
|
66
|
-
│ └── ascend-repos ─→ ~/.work-agent/ascend-repos 知识仓(只读)
|
|
63
|
+
├── .work-agent ─→ ~/.work-agent 软链,指向 work-agent 自己的家
|
|
67
64
|
├── node/ 只有系统 Node 过旧时才有,程序专用
|
|
68
65
|
└── runs/r7x9k2m/ 一个任务一个,结束即删
|
|
69
66
|
├── work-agent/ ← claude 的工作目录:上面那个克隆的完整副本
|
|
70
|
-
├── .home/ 任务的 HOME(.work-agent
|
|
67
|
+
├── .home/ 任务的 HOME(.work-agent 软链到 ~/.work-agent)
|
|
71
68
|
├── .tmp/ TMPDIR
|
|
72
69
|
├── claude-home/ CLAUDE_CONFIG_DIR
|
|
73
70
|
└── settings.json 沙箱设置(任务改不了)
|
|
74
71
|
```
|
|
75
72
|
|
|
76
|
-
-
|
|
77
|
-
|
|
73
|
+
work-agent 自己的家是独立的真目录,和你手工使用 work-agent 时完全共用:
|
|
74
|
+
|
|
75
|
+
```text
|
|
76
|
+
~/.work-agent/ WORK_AGENT_HOME:每个任务都注入这个值
|
|
77
|
+
├── ascend-repos/ 知识仓(任务只读)
|
|
78
|
+
├── essential-repos.txt 初始化下载哪些仓,可自己编辑
|
|
79
|
+
├── logs/ Skill 日志(任务跑完还在,可以查历史)
|
|
80
|
+
├── tmp/ docs/ Skill 的中间产物和交付件
|
|
81
|
+
└── work-agent.py init 建的软链,指向克隆里的脚本
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- **两个仓库的位置都写在 `config.json` 里**(`workAgentRepoDir`、`ascendReposDir`,首次运行自动填上默认值)。要放到别的盘就直接改这两个值(绝对路径),程序按配置文件去找和更新,不用别的开关;知识仓换盘时 `~/.work-agent/ascend-repos` 会变成指向新位置的软链。
|
|
85
|
+
- **沙箱只能写** `~/.work-agent/{logs,tmp,docs}` 和本次任务的目录。`~/.work-agent` 其余部分和知识仓都是只读的 —— 任务删不掉知识仓,也改不了 `essential-repos.txt`。
|
|
78
86
|
- **任务目录**默认在 `~/.work-agent-runner/runs/`,用 `--work-dir <目录>` 可以换到别的盘(代码检视会克隆被检视的仓,可能占几百 MB),其余结构不变。
|
|
79
87
|
- Linux 上程序目录(或 `--work-dir`)的路径不能超过 54 字节:沙箱的通信 socket 建在每个任务的 `.tmp` 里,而 socket 路径最多 107 字节。超了程序会直接报错。
|
|
80
88
|
- 每个任务的工作目录是克隆的**副本**(不含 `.git`):任务在一个 work-agent 仓库里干活,但改不到真仓库,也影响不到下一个任务,结束时整个目录删掉。
|
|
@@ -93,9 +101,16 @@ Skill **不来自网站**,而是本程序自己从 GitCode 克隆的 work-agen
|
|
|
93
101
|
- 本机当前的 commit 会上报给网站,显示在「模型配置 → 执行机器 → 详情」里。没有仓库时所有功能不可用(网页上显示「缺少 work-agent 仓库」)。
|
|
94
102
|
- 仓库被改坏、或上游 force-push 导致无法快进时,程序重新克隆一份,不会用 `reset --hard` 悄悄接受改写过的历史。
|
|
95
103
|
|
|
96
|
-
###
|
|
104
|
+
### 知识仓(可选)
|
|
105
|
+
|
|
106
|
+
「代码检视」和「CANN 知识问答」要查 CANN / AscendC 的权威文档(**CANN 知识问答完全靠它作答,没有仓池的机器不能跑这个功能**)。下载的是 work-agent 的**必要仓清单** `~/.work-agent/essential-repos.txt`(首次 `init` 时按 work-agent 的内置默认生成,之后**可以自己编辑**再运行 `setup`)。默认是 CANN 组织下这些仓:
|
|
107
|
+
|
|
108
|
+
```text
|
|
109
|
+
asc-devkit cann-samples docs ops-math ops-nn ops-transformer
|
|
110
|
+
runtime hccl ge asc-tools cann-learning-hub
|
|
111
|
+
```
|
|
97
112
|
|
|
98
|
-
|
|
113
|
+
仓比较多、合计体积不小(而且会随上游变化),下载可能要很久;程序在下载前会检查磁盘至少有 10 GB 可用,并把当前可用空间打出来。由 work-agent 自己的脚本下载到 `~/.work-agent/ascend-repos`:
|
|
99
114
|
|
|
100
115
|
```bash
|
|
101
116
|
work-agent-runner setup # 在终端里询问后下载
|
package/bin/main.mjs
CHANGED
|
@@ -53,7 +53,7 @@ import {
|
|
|
53
53
|
import { DEFAULT_REQUIRED_TOOLS, npmPrefixWritable, planSetup, runSetup } from '../lib/setup.mjs';
|
|
54
54
|
import { prepareWorkAgentHome } from '../lib/sandbox.mjs';
|
|
55
55
|
import { collectSystemInfo, describeSystem } from '../lib/system-info.mjs';
|
|
56
|
-
import {
|
|
56
|
+
import { WorkAgent, freeBytes } from '../lib/work-agent.mjs';
|
|
57
57
|
|
|
58
58
|
// The command's entry point (it checks the Node version, then loads this file).
|
|
59
59
|
const ENTRY = fileURLToPath(new URL('./work-agent-runner.cjs', import.meta.url));
|
|
@@ -69,6 +69,7 @@ const FEATURE_TOOLS = {
|
|
|
69
69
|
'pr-desc-issue-gen': ['git', 'curl', 'python3'],
|
|
70
70
|
'ascend-code-review': ['git', 'python3', 'curl', 'jq', 'sed'],
|
|
71
71
|
'code-review-flash': ['python3', 'git', 'base64'],
|
|
72
|
+
'cann-knowledge': ['rg'],
|
|
72
73
|
};
|
|
73
74
|
|
|
74
75
|
const USAGE = `GitCode Agent 执行机器 ${VERSION}
|
|
@@ -82,7 +83,7 @@ const USAGE = `GitCode Agent 执行机器 ${VERSION}
|
|
|
82
83
|
logout 清除本机保存的令牌(机器标识保留)
|
|
83
84
|
setup [--yes] [--repos <仓名>|--no-repos]
|
|
84
85
|
检查并安装缺少的依赖、下载 work-agent 仓库,并询问是否
|
|
85
|
-
|
|
86
|
+
下载知识仓(work-agent 的必要仓清单;需在终端里运行)
|
|
86
87
|
update 立即更新 work-agent 仓库和知识仓(平时由程序自己定时更新)
|
|
87
88
|
doctor [--json] 检查依赖、沙箱和各功能是否可用
|
|
88
89
|
status 显示网站、令牌、机器标识、工作目录、服务状态,并测试连接
|
|
@@ -235,7 +236,9 @@ function loadContext(values, { workRoot: needWorkRoot = true } = {}) {
|
|
|
235
236
|
});
|
|
236
237
|
// The Skills' logs, scratch and deliverables, and the pool symlink when there
|
|
237
238
|
// is a pool; a job's HOME links to this directory.
|
|
238
|
-
if (needWorkRoot)
|
|
239
|
+
if (needWorkRoot) {
|
|
240
|
+
prepareWorkAgentHome(paths.workAgentHome, { poolDir: workAgent.mountablePool(), link: paths.workAgentLink });
|
|
241
|
+
}
|
|
239
242
|
return { paths, config, server, workRoot, realHome: realHome(), workAgent };
|
|
240
243
|
}
|
|
241
244
|
|
|
@@ -417,7 +420,7 @@ function printWorkAgent(state) {
|
|
|
417
420
|
const what = state.ascend ? `${state.repos} 个仓(含 AscendC 两仓)` : `${state.repos} 个仓,缺 AscendC 两仓(代码检视的 AscendC 规范域不检查)`;
|
|
418
421
|
say(` 知识仓:${what}${poolAge(state.poolFetchedAt)}`);
|
|
419
422
|
} else {
|
|
420
|
-
say('
|
|
423
|
+
say(' 知识仓:未初始化(可选;运行 work-agent-runner setup 下载)');
|
|
421
424
|
}
|
|
422
425
|
}
|
|
423
426
|
|
|
@@ -434,31 +437,40 @@ function splitRepos(value) {
|
|
|
434
437
|
}
|
|
435
438
|
|
|
436
439
|
/**
|
|
437
|
-
* Offers the
|
|
438
|
-
* downloads anything without a yes: `--repos`
|
|
439
|
-
* skips the question.
|
|
440
|
+
* Offers the knowledge pool — the repositories of work-agent's own essential
|
|
441
|
+
* list — in a terminal only. Never downloads anything without a yes: `--repos`
|
|
442
|
+
* picks the repositories instead, `--no-repos` skips the question.
|
|
440
443
|
*/
|
|
441
444
|
async function offerPool(ctx, values) {
|
|
442
445
|
if (values['no-repos']) return;
|
|
443
|
-
const wanted = values.repos === undefined ?
|
|
446
|
+
const wanted = values.repos === undefined ? null : splitRepos(values.repos);
|
|
447
|
+
const { poolDir, workAgentHome } = ctx.paths;
|
|
444
448
|
const pool = ctx.workAgent.poolState();
|
|
445
449
|
if (pool.incomplete.length) {
|
|
446
|
-
say(`知识仓 ${pool.incomplete.join('、')} 不完整:请先删除 ${
|
|
450
|
+
say(`知识仓 ${pool.incomplete.join('、')} 不完整:请先删除 ${poolDir} 下这些目录`);
|
|
447
451
|
return;
|
|
448
452
|
}
|
|
449
|
-
if (pool.
|
|
450
|
-
say(`知识仓已就绪(${
|
|
453
|
+
if (pool.repos && values.repos === undefined) {
|
|
454
|
+
say(`知识仓已就绪(${poolDir},${pool.repos} 个仓)`);
|
|
451
455
|
return;
|
|
452
456
|
}
|
|
453
|
-
say(
|
|
454
|
-
|
|
457
|
+
if (wanted) say(`将下载到 ${poolDir}:${wanted.join('、')}`);
|
|
458
|
+
else {
|
|
459
|
+
say(`知识仓用于「代码检视」查 CANN / AscendC 的权威文档,下载到 ${poolDir}`);
|
|
460
|
+
// The list file only exists once work-agent.py has run init at least once.
|
|
461
|
+
const listed = ctx.workAgent.essentialRepos();
|
|
462
|
+
if (listed.length) say(`要下载的仓(${ctx.workAgent.essentialFile}):${listed.join('、')}`);
|
|
463
|
+
else say(`要下载的是 work-agent 的必要仓清单,首次初始化时会生成 ${ctx.workAgent.essentialFile},之后可以自己编辑再运行 setup`);
|
|
464
|
+
const free = freeBytes(poolDir);
|
|
465
|
+
say(`仓比较多,下载可能要很久;该磁盘当前可用 ${(free / 1073741824).toFixed(1)} GB。不下载也能用,只是相关规范不做检查。`);
|
|
466
|
+
}
|
|
455
467
|
if (!values.yes && !(await askYesNo('是否现在下载?'))) {
|
|
456
468
|
say('已跳过;以后随时可以再运行 work-agent-runner setup');
|
|
457
469
|
return;
|
|
458
470
|
}
|
|
459
471
|
try {
|
|
460
472
|
await ctx.workAgent.initPool({ repos: wanted, say });
|
|
461
|
-
prepareWorkAgentHome(
|
|
473
|
+
prepareWorkAgentHome(workAgentHome, { poolDir: ctx.workAgent.mountablePool(), link: ctx.paths.workAgentLink });
|
|
462
474
|
} catch (error) {
|
|
463
475
|
say(`下载知识仓失败:${error.message}`);
|
|
464
476
|
}
|
|
@@ -466,9 +478,9 @@ async function offerPool(ctx, values) {
|
|
|
466
478
|
|
|
467
479
|
function neededBy(tool) {
|
|
468
480
|
if (tool === 'bwrap' || tool === 'socat') return '沙箱需要(所有功能)';
|
|
469
|
-
if (tool === 'rg') return '检索 AscendC 知识仓需要';
|
|
470
481
|
const features = Object.entries(FEATURE_TOOLS).filter(([, tools]) => tools.includes(tool)).map(([feature]) => FEATURE_LABELS[feature]);
|
|
471
|
-
|
|
482
|
+
if (!features.length) return '暂时没有功能需要';
|
|
483
|
+
return features.length === Object.keys(FEATURE_TOOLS).length ? '所有功能需要' : `${features.join('、')}需要`;
|
|
472
484
|
}
|
|
473
485
|
|
|
474
486
|
function printDoctor(result) {
|
|
@@ -840,7 +852,8 @@ async function status(values) {
|
|
|
840
852
|
const repo = ctx.workAgent.state();
|
|
841
853
|
say(`work-agent 仓库:${repo.ok ? `${repo.commit.slice(0, 7)}(${ctx.workAgent.repoDir})` : repo.reason}`);
|
|
842
854
|
say(`WORK_AGENT_HOME:${ctx.paths.workAgentHome}`);
|
|
843
|
-
|
|
855
|
+
const poolRepos = ctx.workAgent.poolRepos();
|
|
856
|
+
say(`知识仓:${poolRepos.length ? `${poolRepos.length} 个仓(${poolRepos.join('、')})` : '未初始化'}(${ctx.paths.poolDir})`);
|
|
844
857
|
say(`系统:${describeSystem(systemInfo(ctx))}`);
|
|
845
858
|
say(`配置文件:${ctx.paths.configFile}`);
|
|
846
859
|
const service = serviceStatus();
|
package/lib/config.mjs
CHANGED
|
@@ -7,8 +7,10 @@
|
|
|
7
7
|
// run.lock one running instance per user
|
|
8
8
|
// work-agent/ the work-agent clone: the Skill source (lib/work-agent.mjs)
|
|
9
9
|
// work-agent.lock the clone's lock (a terminal `update` vs the service)
|
|
10
|
-
// .work-agent
|
|
11
|
-
//
|
|
10
|
+
// .work-agent symlink to ~/.work-agent, work-agent's own home and the
|
|
11
|
+
// WORK_AGENT_HOME of every job (logs/ tmp/ docs/
|
|
12
|
+
// ascend-repos/ essential-repos.txt live there, shared
|
|
13
|
+
// with the user's own use of work-agent)
|
|
12
14
|
// node/ a downloaded Node, when the system one is too old
|
|
13
15
|
// runs/<7 chars>/ one directory per job, deleted when it ends (--work-dir
|
|
14
16
|
// moves just this one elsewhere, e.g. a bigger disk)
|
|
@@ -28,8 +30,10 @@ export const REPO_UPDATE_HOUR = 8;
|
|
|
28
30
|
/** The knowledge pool is updated this often (every repository it holds). */
|
|
29
31
|
export const POOL_UPDATE_MS = 60 * 60_000;
|
|
30
32
|
export const PROGRAM_DIR = '.work-agent-runner';
|
|
31
|
-
/**
|
|
32
|
-
export const
|
|
33
|
+
/** work-agent's own home, shared with the user's own use of it. */
|
|
34
|
+
export const WORK_AGENT_DIR = '.work-agent';
|
|
35
|
+
/** Its repository pool, which the program may be pointed at another disk. */
|
|
36
|
+
export const POOL_DIR = path.join(WORK_AGENT_DIR, 'ascend-repos');
|
|
33
37
|
|
|
34
38
|
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
35
39
|
|
|
@@ -53,9 +57,13 @@ export function configPaths(env = process.env, home = os.homedir()) {
|
|
|
53
57
|
lockFile: path.join(base, 'run.lock'),
|
|
54
58
|
repoDir: path.join(base, 'work-agent'),
|
|
55
59
|
repoLockFile: path.join(base, 'work-agent.lock'),
|
|
56
|
-
// WORK_AGENT_HOME handed to every job
|
|
57
|
-
//
|
|
58
|
-
|
|
60
|
+
// WORK_AGENT_HOME handed to every job: work-agent's own home, a real
|
|
61
|
+
// directory outside the program's, so the user's own work-agent use sees
|
|
62
|
+
// the same logs, scratch and repository pool.
|
|
63
|
+
workAgentHome: path.join(home, WORK_AGENT_DIR),
|
|
64
|
+
// A symlink to it inside the program's home, so everything the program
|
|
65
|
+
// touches is reachable from one place.
|
|
66
|
+
workAgentLink: path.join(base, WORK_AGENT_DIR),
|
|
59
67
|
nodeDir: path.join(base, 'node'),
|
|
60
68
|
runsRoot: base,
|
|
61
69
|
poolDir: path.join(home, POOL_DIR),
|
package/lib/jobs.mjs
CHANGED
|
@@ -31,6 +31,7 @@ export const FEATURE_LABELS = {
|
|
|
31
31
|
'pr-desc-issue-gen': 'PR 描述 / Issue',
|
|
32
32
|
'ascend-code-review': '代码检视(全量)',
|
|
33
33
|
'code-review-flash': '代码检视(快速)',
|
|
34
|
+
'cann-knowledge': 'CANN 知识问答',
|
|
34
35
|
};
|
|
35
36
|
|
|
36
37
|
const RUN_ID = /^(skill|review)_\d+_[0-9a-f]{8}$/;
|
|
@@ -396,6 +397,10 @@ export class JobManager {
|
|
|
396
397
|
try {
|
|
397
398
|
const { commit } = await this.workAgent.copyRepoInto(layout.workDir, this.paths.workAgentHome);
|
|
398
399
|
const pool = this.workAgent.mountablePool();
|
|
400
|
+
// Claude Code limits its file tools to the working directory and whatever
|
|
401
|
+
// --add-dir names; the pool is outside it (the sandbox still decides what
|
|
402
|
+
// may be written).
|
|
403
|
+
entry.addDirs = pool ? [this.paths.workAgentHome] : [];
|
|
399
404
|
writeSandboxSettings(layout.settingsFile, {
|
|
400
405
|
workDir: fs.realpathSync(layout.workDir),
|
|
401
406
|
realHome: this.realHome,
|
|
@@ -443,7 +448,7 @@ export class JobManager {
|
|
|
443
448
|
}
|
|
444
449
|
let child;
|
|
445
450
|
try {
|
|
446
|
-
child = spawn(this.claudePath, claudeArgs({ settingsFile: layout.settingsFile }), {
|
|
451
|
+
child = spawn(this.claudePath, claudeArgs({ settingsFile: layout.settingsFile, addDirs: entry.addDirs || [] }), {
|
|
447
452
|
cwd: layout.workDir,
|
|
448
453
|
detached: true,
|
|
449
454
|
stdio: ['pipe', 'pipe', 'pipe'],
|
package/lib/sandbox.mjs
CHANGED
|
@@ -102,9 +102,16 @@ export function sandboxSettings({ workDir, realHome, readAllow = [], writeAllow
|
|
|
102
102
|
export function runAllowances(layout, { realHome, pathEnv = '', caPaths = [], pool = '', paths = null }) {
|
|
103
103
|
const runDirs = [layout.workDir, layout.homeDir, layout.tmpDir, layout.configDir].map((dir) => realpathOr(dir));
|
|
104
104
|
const workAgentHome = layout.workAgentHome ? realpathOr(layout.workAgentHome) : '';
|
|
105
|
-
|
|
105
|
+
// Writable: this run, and the three directories the Skills write into. Not
|
|
106
|
+
// the work-agent home itself and not the pool — a job must not be able to
|
|
107
|
+
// delete the repositories it shares with the user's own work-agent.
|
|
108
|
+
const writeAllow = [
|
|
109
|
+
...runDirs,
|
|
110
|
+
...(workAgentHome ? ['logs', 'tmp', 'docs'].map((name) => path.join(workAgentHome, name)) : []),
|
|
111
|
+
];
|
|
106
112
|
const readAllow = [
|
|
107
113
|
...writeAllow,
|
|
114
|
+
...(workAgentHome ? [workAgentHome] : []),
|
|
108
115
|
...(pool ? [realpathOr(pool)] : []),
|
|
109
116
|
...homeReadAllow({ realHome, pathEnv, caPaths }),
|
|
110
117
|
];
|
|
@@ -209,17 +216,41 @@ export function createRunLayout(runDir, { workAgentHome = '' } = {}) {
|
|
|
209
216
|
return layout;
|
|
210
217
|
}
|
|
211
218
|
|
|
219
|
+
/** Points `link` at `target`, repairing a stale link; leaves a real directory alone. */
|
|
220
|
+
function relink(link, target) {
|
|
221
|
+
let current = '';
|
|
222
|
+
try {
|
|
223
|
+
current = fs.readlinkSync(link);
|
|
224
|
+
} catch {
|
|
225
|
+
// Not a symlink, or missing.
|
|
226
|
+
}
|
|
227
|
+
if (current === target) return true;
|
|
228
|
+
if (current) fs.rmSync(link, { force: true });
|
|
229
|
+
try {
|
|
230
|
+
fs.symlinkSync(target, link);
|
|
231
|
+
return true;
|
|
232
|
+
} catch (error) {
|
|
233
|
+
// A real directory is already there: leave whatever the user put there.
|
|
234
|
+
if (error.code === 'EEXIST') return false;
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
212
239
|
/**
|
|
213
|
-
*
|
|
214
|
-
* Skills to write,
|
|
215
|
-
*
|
|
216
|
-
*
|
|
240
|
+
* Prepares work-agent's own home (`~/.work-agent`, shared with the user's own
|
|
241
|
+
* use of it): `logs/ tmp/ docs/` for the Skills to write, `ascend-repos`
|
|
242
|
+
* pointing at the pool when it was configured onto another disk, and a symlink
|
|
243
|
+
* to the whole thing inside the program's home, so everything the program
|
|
244
|
+
* touches is reachable from one place. Returns the pool's real path, or ''.
|
|
217
245
|
*/
|
|
218
|
-
export function prepareWorkAgentHome(workAgentHome, poolDir = '') {
|
|
246
|
+
export function prepareWorkAgentHome(workAgentHome, { poolDir = '', link = '' } = {}) {
|
|
219
247
|
for (const name of ['logs', 'tmp', 'docs']) {
|
|
220
248
|
fs.mkdirSync(path.join(workAgentHome, name), { recursive: true, mode: 0o700 });
|
|
221
249
|
}
|
|
222
|
-
|
|
250
|
+
if (link) {
|
|
251
|
+
fs.mkdirSync(path.dirname(link), { recursive: true, mode: 0o700 });
|
|
252
|
+
relink(link, path.resolve(workAgentHome));
|
|
253
|
+
}
|
|
223
254
|
if (!poolDir) return '';
|
|
224
255
|
let real;
|
|
225
256
|
try {
|
|
@@ -227,21 +258,10 @@ export function prepareWorkAgentHome(workAgentHome, poolDir = '') {
|
|
|
227
258
|
} catch {
|
|
228
259
|
return '';
|
|
229
260
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
// Not a symlink, or missing.
|
|
235
|
-
}
|
|
236
|
-
if (current !== real) {
|
|
237
|
-
if (current) fs.rmSync(link, { force: true });
|
|
238
|
-
try {
|
|
239
|
-
fs.symlinkSync(real, link);
|
|
240
|
-
} catch (error) {
|
|
241
|
-
// A real directory there (someone cloned into it): leave it alone.
|
|
242
|
-
if (error.code !== 'EEXIST') throw error;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
261
|
+
// Only a pool on another disk needs a link; the default is a real directory
|
|
262
|
+
// inside the home (work-agent's own layout).
|
|
263
|
+
const inside = path.join(workAgentHome, 'ascend-repos');
|
|
264
|
+
if (real !== fs.realpathSync(workAgentHome) + path.sep + 'ascend-repos') relink(inside, real);
|
|
245
265
|
return real;
|
|
246
266
|
}
|
|
247
267
|
|
package/lib/work-agent.mjs
CHANGED
|
@@ -3,14 +3,16 @@
|
|
|
3
3
|
//
|
|
4
4
|
// ~/.work-agent-runner/work-agent/ the clone, tracking origin/main
|
|
5
5
|
// ~/.work-agent-runner/work-agent.lock lock: a terminal `update` vs the service
|
|
6
|
-
// ~/.work-agent
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
6
|
+
// ~/.work-agent/ work-agent's own home = the
|
|
7
|
+
// WORK_AGENT_HOME of every job
|
|
8
|
+
// ├ ascend-repos/ the repository pool, built by
|
|
9
|
+
// │ scripts/work-agent.py
|
|
10
|
+
// ├ essential-repos.txt which repositories init clones
|
|
11
|
+
// └ logs/ tmp/ docs/ the only part a job may write
|
|
12
|
+
// ~/.work-agent-runner/.work-agent symlink to it
|
|
10
13
|
//
|
|
11
|
-
// The
|
|
12
|
-
// the user's own work-agent use sees
|
|
13
|
-
// write — a job cannot delete it.
|
|
14
|
+
// The home is work-agent's own default location, so it is the same workspace
|
|
15
|
+
// and the same repositories the user's own work-agent use sees.
|
|
14
16
|
//
|
|
15
17
|
// The website never names the repository, a branch or a commit — a website that
|
|
16
18
|
// could would be choosing which code runs on the user's machine. The URL is
|
|
@@ -33,7 +35,11 @@ import { copyTreeDetached, rewriteSkillPaths } from './skill-tree.mjs';
|
|
|
33
35
|
|
|
34
36
|
export const REPO_URL = 'https://gitcode.com/pingchuantang/work-agent.git';
|
|
35
37
|
export const REPO_BRANCH = 'main';
|
|
36
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* The two repositories the AscendC domain of 代码检视 reads. They are part of
|
|
40
|
+
* the essential list; this is only how the website is told whether that domain
|
|
41
|
+
* can be covered.
|
|
42
|
+
*/
|
|
37
43
|
export const ASCEND_REPOS = ['asc-devkit', 'cann-samples'];
|
|
38
44
|
// A file that only exists in a complete clone: work-agent.py leaves a half
|
|
39
45
|
// clone behind when it is killed, and then skips it forever as "already there".
|
|
@@ -46,9 +52,12 @@ const ASCEND_ENTRY = {
|
|
|
46
52
|
// free to rename.
|
|
47
53
|
const SKILLS_SUBDIR = path.join('.claude', 'skills');
|
|
48
54
|
const GIT_TIMEOUT_MS = 10 * 60_000;
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
55
|
+
// The essential list is large and grows with upstream, cloned 8 at a time;
|
|
56
|
+
// work-agent.py gives each git operation 1800 s, so never be the one that kills
|
|
57
|
+
// it first. The free-space figure is a floor, not a claim about the list's size.
|
|
58
|
+
const POOL_TIMEOUT_MS = 240 * 60_000;
|
|
59
|
+
export const POOL_FREE_BYTES = 10 * 1024 * 1024 * 1024;
|
|
60
|
+
const ESSENTIAL_FILE = 'essential-repos.txt';
|
|
52
61
|
const KILL_GRACE_MS = 5000;
|
|
53
62
|
// work-agent.py cleanup: tmp/ is emptied and logs older than this are dropped.
|
|
54
63
|
const LOG_DAYS = 30;
|
|
@@ -215,16 +224,25 @@ export class WorkAgent {
|
|
|
215
224
|
}
|
|
216
225
|
}
|
|
217
226
|
|
|
218
|
-
/**
|
|
219
|
-
|
|
227
|
+
/** The repositories the pool holds, by name. */
|
|
228
|
+
poolRepos() {
|
|
220
229
|
const { poolDir } = this.paths;
|
|
221
230
|
let names = [];
|
|
222
231
|
try {
|
|
223
|
-
names = fs
|
|
232
|
+
names = fs
|
|
233
|
+
.readdirSync(poolDir, { withFileTypes: true })
|
|
234
|
+
.filter((entry) => entry.isDirectory() || entry.isSymbolicLink())
|
|
235
|
+
.map((entry) => entry.name);
|
|
224
236
|
} catch {
|
|
225
|
-
return
|
|
237
|
+
return [];
|
|
226
238
|
}
|
|
227
|
-
|
|
239
|
+
return names.filter((name) => !name.startsWith('.') && exists(path.join(poolDir, name, '.git'))).sort();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** { repos, ascend, incomplete }: how many repos the pool holds and whether the AscendC pair is complete. */
|
|
243
|
+
poolState() {
|
|
244
|
+
const { poolDir } = this.paths;
|
|
245
|
+
const repos = this.poolRepos();
|
|
228
246
|
const incomplete = ASCEND_REPOS.filter(
|
|
229
247
|
(name) => repos.includes(name) && !exists(path.join(poolDir, name, ASCEND_ENTRY[name])),
|
|
230
248
|
);
|
|
@@ -407,12 +425,32 @@ export class WorkAgent {
|
|
|
407
425
|
});
|
|
408
426
|
}
|
|
409
427
|
|
|
428
|
+
/** The repositories named in work-agent's essential list ([] when it has none yet). */
|
|
429
|
+
essentialRepos() {
|
|
430
|
+
try {
|
|
431
|
+
return fs
|
|
432
|
+
.readFileSync(path.join(this.paths.workAgentHome, ESSENTIAL_FILE), 'utf8')
|
|
433
|
+
.split('\n')
|
|
434
|
+
.map((line) => line.split('#')[0].trim())
|
|
435
|
+
.filter(Boolean);
|
|
436
|
+
} catch {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** Where that list lives, for messages that have to point at it. */
|
|
442
|
+
get essentialFile() {
|
|
443
|
+
return path.join(this.paths.workAgentHome, ESSENTIAL_FILE);
|
|
444
|
+
}
|
|
445
|
+
|
|
410
446
|
/**
|
|
411
|
-
* Clones the
|
|
412
|
-
*
|
|
413
|
-
*
|
|
447
|
+
* Clones the repositories of work-agent's own essential list
|
|
448
|
+
* ($WORK_AGENT_HOME/essential-repos.txt, which init creates and the user may
|
|
449
|
+
* edit) into the pool, or the ones `repos` names. Only ever from a terminal:
|
|
450
|
+
* the list is large. `work-agent.py` skips repos that are already there, so
|
|
451
|
+
* this is safe to re-run.
|
|
414
452
|
*/
|
|
415
|
-
initPool({ repos =
|
|
453
|
+
initPool({ repos = null, say = () => {}, requireFreeBytes = POOL_FREE_BYTES } = {}) {
|
|
416
454
|
return this.run(async () => {
|
|
417
455
|
const state = this.state();
|
|
418
456
|
if (!state.ok) throw new Error(state.reason);
|
|
@@ -423,18 +461,19 @@ export class WorkAgent {
|
|
|
423
461
|
`知识仓需要 ${Math.round(requireFreeBytes / 1073741824)} GB 以上`,
|
|
424
462
|
);
|
|
425
463
|
}
|
|
426
|
-
say(`正在下载知识仓到 ${this.paths.poolDir}(${repos.join('、')
|
|
464
|
+
say(`正在下载知识仓到 ${this.paths.poolDir}(${repos ? repos.join('、') : '必要仓清单里的仓'}),可能要很久`);
|
|
427
465
|
say('如果你自己也在运行 work-agent 的 init/update,这里会先等它结束(它们共用一把锁)');
|
|
428
|
-
// work-agent.py clones into $WORK_AGENT_HOME/ascend-repos
|
|
429
|
-
//
|
|
466
|
+
// work-agent.py clones into $WORK_AGENT_HOME/ascend-repos (a symlink when
|
|
467
|
+
// the pool was put on another disk).
|
|
430
468
|
fs.mkdirSync(this.paths.poolDir, { recursive: true, mode: 0o700 });
|
|
431
|
-
prepareWorkAgentHome(this.paths.workAgentHome, this.paths.poolDir);
|
|
432
|
-
await this.python(['init', '--repos', repos.join(',')], { say });
|
|
469
|
+
prepareWorkAgentHome(this.paths.workAgentHome, { poolDir: this.paths.poolDir, link: this.paths.workAgentLink });
|
|
470
|
+
await this.python(repos ? ['init', '--repos', repos.join(',')] : ['init', '--essential'], { say });
|
|
433
471
|
const after = this.poolState();
|
|
434
472
|
if (after.incomplete.length) {
|
|
435
473
|
throw new Error(`${after.incomplete.join('、')} 下载不完整:请删除 ${this.paths.poolDir} 下这些目录后重试`);
|
|
436
474
|
}
|
|
437
|
-
|
|
475
|
+
const names = this.poolRepos();
|
|
476
|
+
say(`知识仓就绪:${this.paths.poolDir}(${after.repos} 个仓:${names.join('、')})`);
|
|
438
477
|
return after;
|
|
439
478
|
});
|
|
440
479
|
}
|