@online-tools/work-agent-runner 0.0.1-alpha-5 → 0.0.2-alpha-1
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 +77 -17
- package/bin/main.mjs +259 -44
- package/lib/config.mjs +128 -47
- package/lib/doctor.mjs +36 -9
- package/lib/jobs.mjs +48 -26
- package/lib/node-runtime.cjs +3 -2
- package/lib/packages.mjs +6 -2
- package/lib/sandbox.mjs +154 -25
- package/lib/service.mjs +7 -1
- package/lib/setup.mjs +4 -1
- package/lib/skill-text.mjs +2 -2
- package/lib/skill-tree.mjs +100 -0
- package/lib/work-agent.mjs +486 -0
- package/package.json +1 -1
- package/lib/skills.mjs +0 -211
package/bin/main.mjs
CHANGED
|
@@ -20,15 +20,18 @@ import {
|
|
|
20
20
|
ConfigError,
|
|
21
21
|
DEFAULT_SERVER,
|
|
22
22
|
LEGACY_NAME,
|
|
23
|
+
POOL_UPDATE_MS,
|
|
24
|
+
REPO_UPDATE_HOUR,
|
|
23
25
|
WorkRootError,
|
|
24
|
-
|
|
26
|
+
adoptLegacyState,
|
|
25
27
|
configPaths,
|
|
26
|
-
defaultWorkRoot,
|
|
27
28
|
legacyConfigPending,
|
|
29
|
+
legacyLockFile,
|
|
28
30
|
loadConfig,
|
|
29
31
|
newClientId,
|
|
30
32
|
prepareWorkRoot,
|
|
31
33
|
saveConfig,
|
|
34
|
+
withRepoPaths,
|
|
32
35
|
} from '../lib/config.mjs';
|
|
33
36
|
import { UPDATE_COMMAND, fatalFor, runConnection } from '../lib/connection.mjs';
|
|
34
37
|
import { SANDBOX_DOCS, runDoctor, which } from '../lib/doctor.mjs';
|
|
@@ -48,8 +51,9 @@ import {
|
|
|
48
51
|
uninstallService,
|
|
49
52
|
} from '../lib/service.mjs';
|
|
50
53
|
import { DEFAULT_REQUIRED_TOOLS, npmPrefixWritable, planSetup, runSetup } from '../lib/setup.mjs';
|
|
51
|
-
import {
|
|
54
|
+
import { prepareWorkAgentHome } from '../lib/sandbox.mjs';
|
|
52
55
|
import { collectSystemInfo, describeSystem } from '../lib/system-info.mjs';
|
|
56
|
+
import { ASCEND_REPOS, WorkAgent } from '../lib/work-agent.mjs';
|
|
53
57
|
|
|
54
58
|
// The command's entry point (it checks the Node version, then loads this file).
|
|
55
59
|
const ENTRY = fileURLToPath(new URL('./work-agent-runner.cjs', import.meta.url));
|
|
@@ -76,7 +80,10 @@ const USAGE = `GitCode Agent 执行机器 ${VERSION}
|
|
|
76
80
|
login [--new-id] [--token-stdin] [--name <名称>]
|
|
77
81
|
输入接入令牌(校验通过才保存);--new-id 重新生成机器标识
|
|
78
82
|
logout 清除本机保存的令牌(机器标识保留)
|
|
79
|
-
setup [--yes]
|
|
83
|
+
setup [--yes] [--repos <仓名>|--no-repos]
|
|
84
|
+
检查并安装缺少的依赖、下载 work-agent 仓库,并询问是否
|
|
85
|
+
下载 AscendC 知识仓(需在终端里运行)
|
|
86
|
+
update 立即更新 work-agent 仓库和知识仓(平时由程序自己定时更新)
|
|
80
87
|
doctor [--json] 检查依赖、沙箱和各功能是否可用
|
|
81
88
|
status 显示网站、令牌、机器标识、工作目录、服务状态,并测试连接
|
|
82
89
|
service install|uninstall|status
|
|
@@ -87,7 +94,9 @@ const USAGE = `GitCode Agent 执行机器 ${VERSION}
|
|
|
87
94
|
--server <url> 网站地址(默认 ${DEFAULT_SERVER},会保存)
|
|
88
95
|
--work-dir <dir> 工作目录(不能在主目录之内,会保存)
|
|
89
96
|
--name <名称> 这台机器登记时用的名称(默认主机名,会保存;登记后在网页上修改)
|
|
90
|
-
--allow-root 允许以 root
|
|
97
|
+
--allow-root 允许以 root 运行(仅限容器等确有需要的场合)
|
|
98
|
+
|
|
99
|
+
work-agent 仓库(Skill 来源)由本程序自己克隆并跟随 main,网站不下发 Skill。`;
|
|
91
100
|
|
|
92
101
|
class ExitError extends Error {
|
|
93
102
|
constructor(code, message = '') {
|
|
@@ -137,31 +146,38 @@ function machineName(value) {
|
|
|
137
146
|
}
|
|
138
147
|
|
|
139
148
|
/**
|
|
140
|
-
* First run after
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
149
|
+
* First run after an older layout (0.0.1 kept the config under ~/.config and
|
|
150
|
+
* ran jobs in /var/tmp; before 0.0.1-alpha-5 everything was named
|
|
151
|
+
* gitcode-agent-runner): take over the config, so the token and the machine id
|
|
152
|
+
* stay — never while a program still runs with it. Before anything else writes
|
|
153
|
+
* a config here, which would get a new machine id.
|
|
144
154
|
*/
|
|
145
155
|
function adoptLegacy(paths) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
156
|
+
const configFile = legacyConfigPending(paths);
|
|
157
|
+
if (configFile) {
|
|
158
|
+
const holder = lockHolder(legacyLockFile(configFile));
|
|
159
|
+
for (const name of ['work-agent-runner', LEGACY_NAME]) {
|
|
160
|
+
if (!isRunnerAlive(holder, name)) continue;
|
|
161
|
+
throw new ExitError(
|
|
162
|
+
ATTENTION,
|
|
163
|
+
`旧版程序(${name},pid ${holder})正在运行,停止后才能沿用它的令牌和机器标识。` +
|
|
164
|
+
'它是后台服务的话,运行 work-agent-runner service install 会停止并替换它(service uninstall 只删除);在前台运行的请按 Ctrl+C',
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (!configFile && fs.existsSync(paths.configFile)) return;
|
|
169
|
+
adoptLegacyState(paths, configFile);
|
|
170
|
+
if (configFile) {
|
|
171
|
+
// stderr: doctor --json prints JSON on stdout.
|
|
172
|
+
process.stderr.write(`已沿用旧版本的配置(接入令牌和机器标识不变):${configFile} → ${paths.configFile}\n`);
|
|
154
173
|
}
|
|
155
|
-
adoptLegacyConfig(paths);
|
|
156
|
-
// stderr: doctor --json prints JSON on stdout.
|
|
157
|
-
process.stderr.write(`已沿用旧版程序 ${LEGACY_NAME} 的配置(接入令牌和机器标识不变):${paths.configFile}\n`);
|
|
158
174
|
}
|
|
159
175
|
|
|
160
176
|
/** Config, server, client id and work root; --server / --work-dir / --name are saved. */
|
|
161
177
|
function loadContext(values, { workRoot: needWorkRoot = true } = {}) {
|
|
162
|
-
const
|
|
163
|
-
adoptLegacy(
|
|
164
|
-
const config = loadConfig(
|
|
178
|
+
const defaults = configPaths();
|
|
179
|
+
adoptLegacy(defaults);
|
|
180
|
+
const config = loadConfig(defaults);
|
|
165
181
|
let changed = false;
|
|
166
182
|
if (!config.clientId) {
|
|
167
183
|
config.clientId = newClientId();
|
|
@@ -192,9 +208,35 @@ function loadContext(values, { workRoot: needWorkRoot = true } = {}) {
|
|
|
192
208
|
changed = true;
|
|
193
209
|
}
|
|
194
210
|
}
|
|
195
|
-
|
|
211
|
+
// The work-agent clone and the knowledge pool: wherever the config file says.
|
|
212
|
+
let paths;
|
|
213
|
+
try {
|
|
214
|
+
const resolved = withRepoPaths(defaults, config);
|
|
215
|
+
paths = resolved.paths;
|
|
216
|
+
if (resolved.missing) {
|
|
217
|
+
// Write both into the config, so the file always shows where they are.
|
|
218
|
+
config.workAgentRepoDir = paths.repoDir;
|
|
219
|
+
config.ascendReposDir = paths.poolDir;
|
|
220
|
+
changed = true;
|
|
221
|
+
}
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new ExitError(ATTENTION, error.message);
|
|
224
|
+
}
|
|
225
|
+
// runs/ lives in the program's home unless --work-dir moves it.
|
|
226
|
+
const workRoot = needWorkRoot ? prepareWorkRoot(config.workRoot || paths.runsRoot) : '';
|
|
196
227
|
if (changed) saveConfig(paths, config);
|
|
197
|
-
|
|
228
|
+
const workAgent = new WorkAgent({
|
|
229
|
+
paths,
|
|
230
|
+
log,
|
|
231
|
+
// A mirror of the work-agent repository: from this machine's own config, or
|
|
232
|
+
// from WORK_AGENT_REPO for development and tests (any scheme, so a local
|
|
233
|
+
// path works). Never from the website, and clone() logs where it went.
|
|
234
|
+
repoUrl: process.env.WORK_AGENT_REPO || config.workAgentRepo,
|
|
235
|
+
});
|
|
236
|
+
// The Skills' logs, scratch and deliverables, and the pool symlink when there
|
|
237
|
+
// is a pool; a job's HOME links to this directory.
|
|
238
|
+
if (needWorkRoot) prepareWorkAgentHome(paths.workAgentHome, workAgent.mountablePool());
|
|
239
|
+
return { paths, config, server, workRoot, realHome: realHome(), workAgent };
|
|
198
240
|
}
|
|
199
241
|
|
|
200
242
|
function save(ctx) {
|
|
@@ -217,7 +259,7 @@ function connect(ctx, client, capabilities) {
|
|
|
217
259
|
}
|
|
218
260
|
|
|
219
261
|
function systemInfo(ctx) {
|
|
220
|
-
return collectSystemInfo({ workRoot: ctx.workRoot || ctx.config.workRoot ||
|
|
262
|
+
return collectSystemInfo({ workRoot: ctx.workRoot || ctx.config.workRoot || ctx.paths.runsRoot });
|
|
221
263
|
}
|
|
222
264
|
|
|
223
265
|
/** After /connect: what the machine is called there, and whether its owner confirmed it. */
|
|
@@ -256,7 +298,12 @@ function networkReason(error) {
|
|
|
256
298
|
|
|
257
299
|
/** doctor + remembering a claude found outside PATH. */
|
|
258
300
|
async function doctorFor(ctx) {
|
|
259
|
-
const result = await runDoctor({
|
|
301
|
+
const result = await runDoctor({
|
|
302
|
+
configuredClaude: ctx.config.claudePath || '',
|
|
303
|
+
workRoot: ctx.workRoot,
|
|
304
|
+
realHome: ctx.realHome,
|
|
305
|
+
workAgent: ctx.workAgent,
|
|
306
|
+
});
|
|
260
307
|
const remembered = result.claude.viaNpmPrefix || result.claude.path === ctx.config.claudePath ? result.claude.path : '';
|
|
261
308
|
if ((ctx.config.claudePath || '') !== remembered) {
|
|
262
309
|
ctx.config.claudePath = remembered;
|
|
@@ -335,8 +382,91 @@ function toolLabel(name) {
|
|
|
335
382
|
return name === 'bwrap' ? 'bubblewrap' : name;
|
|
336
383
|
}
|
|
337
384
|
|
|
385
|
+
/** Updates every repository in the knowledge pool. Never fatal. */
|
|
386
|
+
async function updatePool(ctx) {
|
|
387
|
+
if (!ctx.workAgent.mountablePool()) return;
|
|
388
|
+
await ctx.workAgent
|
|
389
|
+
.updatePool({ say: (line) => log.info(line) })
|
|
390
|
+
.catch((error) => log.warn(`知识仓更新失败:${error.message}`));
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Brings the clone up to date. Never fatal: the website shows the reason instead. */
|
|
394
|
+
async function refreshWorkAgent(ctx, { say: out = say } = {}) {
|
|
395
|
+
try {
|
|
396
|
+
return await ctx.workAgent.refresh();
|
|
397
|
+
} catch (error) {
|
|
398
|
+
out(`work-agent 仓库检查失败:${error.message}`);
|
|
399
|
+
return ctx.workAgent.state();
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** ",3 天前更新" for the knowledge pool line ('' when it was never fetched). */
|
|
404
|
+
function poolAge(fetchedAt) {
|
|
405
|
+
if (!fetchedAt) return '';
|
|
406
|
+
const days = Math.floor((Date.now() - fetchedAt) / 86_400_000);
|
|
407
|
+
return `,${days <= 0 ? '今天' : `${days} 天前`}更新`;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** The Skill source and the knowledge pool, in two lines. */
|
|
411
|
+
function printWorkAgent(state) {
|
|
412
|
+
if (state.ok) say(`✓ work-agent ${state.commit.slice(0, 7)}${state.committedAt ? `(${state.committedAt.slice(0, 10)})` : ''}`);
|
|
413
|
+
else say(`✗ work-agent 仓库不可用:${state.reason}`);
|
|
414
|
+
if (state.incomplete?.length) {
|
|
415
|
+
say(` 知识仓 ${state.incomplete.join('、')} 不完整:请删除后重新运行 work-agent-runner setup`);
|
|
416
|
+
} else if (state.repos) {
|
|
417
|
+
const what = state.ascend ? `${state.repos} 个仓(含 AscendC 两仓)` : `${state.repos} 个仓,缺 AscendC 两仓(代码检视的 AscendC 规范域不检查)`;
|
|
418
|
+
say(` 知识仓:${what}${poolAge(state.poolFetchedAt)}`);
|
|
419
|
+
} else {
|
|
420
|
+
say(' 知识仓:未初始化(可选,约 1.1 GB;运行 work-agent-runner setup 下载)');
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const REPO_NAME = /^[A-Za-z0-9._-]{1,80}$/;
|
|
425
|
+
|
|
426
|
+
/** `--repos a,b` → names, validated the way work-agent.py validates them. */
|
|
427
|
+
function splitRepos(value) {
|
|
428
|
+
const names = String(value).split(/[,\s]+/).filter(Boolean);
|
|
429
|
+
for (const name of names) {
|
|
430
|
+
if (!REPO_NAME.test(name) || name.startsWith('.')) throw new ExitError(1, `仓名不正确:${name}`);
|
|
431
|
+
}
|
|
432
|
+
if (!names.length) throw new ExitError(1, '--repos 需要至少一个仓名');
|
|
433
|
+
return names;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Offers the AscendC knowledge pool (≈1.1 GB), in a terminal only. Never
|
|
438
|
+
* downloads anything without a yes: `--repos` picks the repos, `--no-repos`
|
|
439
|
+
* skips the question.
|
|
440
|
+
*/
|
|
441
|
+
async function offerPool(ctx, values) {
|
|
442
|
+
if (values['no-repos']) return;
|
|
443
|
+
const wanted = values.repos === undefined ? ASCEND_REPOS : splitRepos(values.repos);
|
|
444
|
+
const pool = ctx.workAgent.poolState();
|
|
445
|
+
if (pool.incomplete.length) {
|
|
446
|
+
say(`知识仓 ${pool.incomplete.join('、')} 不完整:请先删除 ${ctx.workAgent.paths.poolDir} 下这些目录`);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (pool.ascend && values.repos === undefined) {
|
|
450
|
+
say(`知识仓已就绪(${ctx.workAgent.paths.poolDir},${pool.repos} 个仓)`);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
say(`AscendC 知识仓用于「代码检视」的 AscendC 规范域:${wanted.join('、')},约 1.1 GB,下载到 ${ctx.workAgent.paths.poolDir}`);
|
|
454
|
+
say('不下载也能用,只是那部分规范不检查。');
|
|
455
|
+
if (!values.yes && !(await askYesNo('是否现在下载?'))) {
|
|
456
|
+
say('已跳过;以后随时可以再运行 work-agent-runner setup');
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
try {
|
|
460
|
+
await ctx.workAgent.initPool({ repos: wanted, say });
|
|
461
|
+
prepareWorkAgentHome(ctx.paths.workAgentHome, ctx.workAgent.mountablePool());
|
|
462
|
+
} catch (error) {
|
|
463
|
+
say(`下载知识仓失败:${error.message}`);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
338
467
|
function neededBy(tool) {
|
|
339
468
|
if (tool === 'bwrap' || tool === 'socat') return '沙箱需要(所有功能)';
|
|
469
|
+
if (tool === 'rg') return '检索 AscendC 知识仓需要';
|
|
340
470
|
const features = Object.entries(FEATURE_TOOLS).filter(([, tools]) => tools.includes(tool)).map(([feature]) => FEATURE_LABELS[feature]);
|
|
341
471
|
return features.length === 3 ? '所有功能需要' : `${features.join('、')}需要`;
|
|
342
472
|
}
|
|
@@ -371,7 +501,7 @@ function printFeatures(reply) {
|
|
|
371
501
|
if (reply.outdated) say(`程序版本过低,请运行 ${UPDATE_COMMAND}`);
|
|
372
502
|
}
|
|
373
503
|
|
|
374
|
-
function setupPlan(result, reply) {
|
|
504
|
+
function setupPlan(result, reply, { poolWanted = false } = {}) {
|
|
375
505
|
const npm = which('npm');
|
|
376
506
|
let npmWritable = false;
|
|
377
507
|
if (npm) {
|
|
@@ -381,6 +511,8 @@ function setupPlan(result, reply) {
|
|
|
381
511
|
return planSetup({
|
|
382
512
|
platform: process.platform,
|
|
383
513
|
requiredTools: Array.isArray(reply?.requiredTools) ? reply.requiredTools : DEFAULT_REQUIRED_TOOLS,
|
|
514
|
+
// The website does not ask for rg; the knowledge pool needs it.
|
|
515
|
+
extraTools: poolWanted ? ['rg'] : [],
|
|
384
516
|
tools: result.tools,
|
|
385
517
|
claudeVersion: result.claudeVersion,
|
|
386
518
|
minClaudeVersion: reply?.minClaudeVersion || '',
|
|
@@ -415,15 +547,19 @@ async function start(values) {
|
|
|
415
547
|
try {
|
|
416
548
|
say(`GitCode Agent 执行机器 ${VERSION}`);
|
|
417
549
|
if (legacyServiceInstalled()) log.warn(LEGACY_SERVICE_HINT);
|
|
418
|
-
// Whatever a previous run left behind.
|
|
550
|
+
// Whatever a previous run left behind (probe directories are runs/p******).
|
|
419
551
|
fs.rmSync(path.join(ctx.workRoot, 'runs'), { recursive: true, force: true });
|
|
420
|
-
for (const name of fs.readdirSync(ctx.workRoot)) {
|
|
421
|
-
if (name.startsWith('probe-')) fs.rmSync(path.join(ctx.workRoot, name), { recursive: true, force: true });
|
|
422
|
-
}
|
|
423
552
|
const interactive = isInteractive() && !process.env.GC_RUNNER_SERVICE;
|
|
424
553
|
if (!ctx.config.token && !interactive) {
|
|
425
554
|
throw new ExitError(ATTENTION, '还没有接入令牌:请先在终端运行 work-agent-runner login');
|
|
426
555
|
}
|
|
556
|
+
say('检查 work-agent 仓库……');
|
|
557
|
+
printWorkAgent(await refreshWorkAgent(ctx));
|
|
558
|
+
// No job is running yet, so it is safe to empty $WORK_AGENT_HOME/tmp, drop
|
|
559
|
+
// old logs (work-agent's own cleanup) and pull the knowledge pool — a
|
|
560
|
+
// `git pull` must never replace files a running review is reading.
|
|
561
|
+
await ctx.workAgent.cleanup({ say: (line) => log.info(line) }).catch(() => {});
|
|
562
|
+
await updatePool(ctx);
|
|
427
563
|
say('检查依赖和沙箱……');
|
|
428
564
|
let result = await doctorFor(ctx);
|
|
429
565
|
let client;
|
|
@@ -436,9 +572,10 @@ async function start(values) {
|
|
|
436
572
|
sayRegistered(ctx, reply);
|
|
437
573
|
}
|
|
438
574
|
printDoctor(result);
|
|
439
|
-
|
|
575
|
+
const firstRun = !ctx.config.setupDone && interactive;
|
|
576
|
+
if (firstRun) {
|
|
440
577
|
const outcome = await runSetup({
|
|
441
|
-
plan: setupPlan(result, reply),
|
|
578
|
+
plan: setupPlan(result, reply, { poolWanted: !values['no-repos'] }),
|
|
442
579
|
interactive: true,
|
|
443
580
|
confirm: (question) => askYesNo(question),
|
|
444
581
|
say,
|
|
@@ -449,9 +586,12 @@ async function start(values) {
|
|
|
449
586
|
if (outcome.ran) {
|
|
450
587
|
say('重新检查……');
|
|
451
588
|
result = await doctorFor(ctx);
|
|
452
|
-
reply = await connectSaved(ctx, client, result.capabilities);
|
|
453
589
|
printDoctor(result);
|
|
454
590
|
}
|
|
591
|
+
// The same question `setup` asks, so a first run settles everything.
|
|
592
|
+
await offerPool(ctx, values);
|
|
593
|
+
result = await doctorFor(ctx);
|
|
594
|
+
reply = await connectSaved(ctx, client, result.capabilities);
|
|
455
595
|
}
|
|
456
596
|
printFeatures(reply);
|
|
457
597
|
say('等待任务……(Ctrl+C 退出;后台运行:work-agent-runner service install)');
|
|
@@ -473,7 +613,7 @@ async function serve(ctx, client, initial, reply) {
|
|
|
473
613
|
};
|
|
474
614
|
const jobs = new JobManager({
|
|
475
615
|
client,
|
|
476
|
-
|
|
616
|
+
workAgent: ctx.workAgent,
|
|
477
617
|
workRoot: ctx.workRoot,
|
|
478
618
|
claudePath: initial.claude.path,
|
|
479
619
|
realHome: ctx.realHome,
|
|
@@ -481,16 +621,44 @@ async function serve(ctx, client, initial, reply) {
|
|
|
481
621
|
onFatal,
|
|
482
622
|
});
|
|
483
623
|
let capabilities = initial.capabilities;
|
|
624
|
+
let checking = false;
|
|
625
|
+
// Every hour: the knowledge pool, then the dependency and sandbox check.
|
|
484
626
|
const doctorTimer = setInterval(async () => {
|
|
627
|
+
// A hung pull must not stack up another round.
|
|
628
|
+
if (checking) return;
|
|
629
|
+
checking = true;
|
|
485
630
|
try {
|
|
631
|
+
// Only between jobs: a pull would replace files under a running review.
|
|
632
|
+
if (!jobs.runningIds().length) await updatePool(ctx);
|
|
486
633
|
const fresh = await doctorFor(ctx);
|
|
487
634
|
capabilities = fresh.capabilities;
|
|
488
635
|
if (fresh.claude.path) jobs.claudePath = fresh.claude.path;
|
|
489
636
|
} catch (error) {
|
|
490
637
|
log.warn(`定期检查失败:${error.message}`);
|
|
638
|
+
} finally {
|
|
639
|
+
checking = false;
|
|
491
640
|
}
|
|
492
|
-
},
|
|
641
|
+
}, POOL_UPDATE_MS);
|
|
493
642
|
doctorTimer.unref();
|
|
643
|
+
// Every day at 08:00 local time: the work-agent clone (the Skill source).
|
|
644
|
+
let repoTimer = null;
|
|
645
|
+
const scheduleRepoUpdate = () => {
|
|
646
|
+
const next = new Date();
|
|
647
|
+
next.setHours(REPO_UPDATE_HOUR, 0, 0, 0);
|
|
648
|
+
if (next.getTime() <= Date.now()) next.setDate(next.getDate() + 1);
|
|
649
|
+
repoTimer = setTimeout(async () => {
|
|
650
|
+
printWorkAgent(await refreshWorkAgent(ctx, { say: (line) => log.info(line) }));
|
|
651
|
+
try {
|
|
652
|
+
capabilities = (await doctorFor(ctx)).capabilities;
|
|
653
|
+
} catch (error) {
|
|
654
|
+
log.warn(`定期检查失败:${error.message}`);
|
|
655
|
+
}
|
|
656
|
+
// Re-armed each time, so it stays on 08:00 across a change of day length.
|
|
657
|
+
scheduleRepoUpdate();
|
|
658
|
+
}, next.getTime() - Date.now());
|
|
659
|
+
repoTimer.unref();
|
|
660
|
+
};
|
|
661
|
+
scheduleRepoUpdate();
|
|
494
662
|
let stopping = false;
|
|
495
663
|
const onSignal = (signal) => {
|
|
496
664
|
if (stopping) return;
|
|
@@ -513,7 +681,9 @@ async function serve(ctx, client, initial, reply) {
|
|
|
513
681
|
signal: controller.signal,
|
|
514
682
|
});
|
|
515
683
|
clearInterval(doctorTimer);
|
|
684
|
+
clearTimeout(repoTimer);
|
|
516
685
|
const outcome = fatal || result;
|
|
686
|
+
ctx.workAgent.stop();
|
|
517
687
|
await jobs.stopAll(5000);
|
|
518
688
|
if (outcome.clearToken) {
|
|
519
689
|
ctx.config.token = '';
|
|
@@ -558,6 +728,8 @@ async function setup(values) {
|
|
|
558
728
|
if (restarted !== null) return restarted;
|
|
559
729
|
if (!isInteractive()) throw new ExitError(ATTENTION, 'setup 需要在终端里运行(安装前会请你确认)');
|
|
560
730
|
const ctx = loadContext(values);
|
|
731
|
+
say('检查 work-agent 仓库……');
|
|
732
|
+
printWorkAgent(await refreshWorkAgent(ctx));
|
|
561
733
|
say('检查依赖和沙箱……');
|
|
562
734
|
let result = await doctorFor(ctx);
|
|
563
735
|
let client = null;
|
|
@@ -572,8 +744,9 @@ async function setup(values) {
|
|
|
572
744
|
}
|
|
573
745
|
}
|
|
574
746
|
printDoctor(result);
|
|
747
|
+
const poolWanted = !values['no-repos'];
|
|
575
748
|
const outcome = await runSetup({
|
|
576
|
-
plan: setupPlan(result, reply),
|
|
749
|
+
plan: setupPlan(result, reply, { poolWanted }),
|
|
577
750
|
interactive: true,
|
|
578
751
|
yes: Boolean(values.yes),
|
|
579
752
|
confirm: (question) => askYesNo(question),
|
|
@@ -584,13 +757,42 @@ async function setup(values) {
|
|
|
584
757
|
if (outcome.ran) {
|
|
585
758
|
say('重新检查……');
|
|
586
759
|
result = await doctorFor(ctx);
|
|
587
|
-
if (client) reply = await connect(ctx, client, result.capabilities).catch(() => reply);
|
|
588
760
|
printDoctor(result);
|
|
589
761
|
}
|
|
762
|
+
// After the tools: the pool is searched with rg.
|
|
763
|
+
await offerPool(ctx, values);
|
|
764
|
+
if (client) reply = await connect(ctx, client, (await doctorFor(ctx)).capabilities).catch(() => reply);
|
|
590
765
|
printFeatures(reply);
|
|
591
766
|
return outcome.failed.length ? 1 : 0;
|
|
592
767
|
}
|
|
593
768
|
|
|
769
|
+
/** `update`: both repositories, now. */
|
|
770
|
+
async function update(values) {
|
|
771
|
+
checkSystem();
|
|
772
|
+
const restarted = await applyProxy();
|
|
773
|
+
if (restarted !== null) return restarted;
|
|
774
|
+
const ctx = loadContext(values);
|
|
775
|
+
let failed = false;
|
|
776
|
+
try {
|
|
777
|
+
const state = await ctx.workAgent.refresh();
|
|
778
|
+
printWorkAgent(state);
|
|
779
|
+
failed = !state.ok;
|
|
780
|
+
if (ctx.workAgent.mountablePool()) await ctx.workAgent.updatePool({ say });
|
|
781
|
+
} catch (error) {
|
|
782
|
+
say(error.message);
|
|
783
|
+
failed = true;
|
|
784
|
+
}
|
|
785
|
+
if (ctx.config.token) {
|
|
786
|
+
// So the website shows the new commit without waiting for the hourly check.
|
|
787
|
+
try {
|
|
788
|
+
printFeatures(await connect(ctx, clientFor(ctx), (await doctorFor(ctx)).capabilities));
|
|
789
|
+
} catch {
|
|
790
|
+
say('(没能把新状态报给网站,程序下次连接时会报)');
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return failed ? 1 : 0;
|
|
794
|
+
}
|
|
795
|
+
|
|
594
796
|
async function doctor(values) {
|
|
595
797
|
checkSystem();
|
|
596
798
|
const restarted = await applyProxy();
|
|
@@ -608,12 +810,14 @@ async function doctor(values) {
|
|
|
608
810
|
}
|
|
609
811
|
} else note = '还没有接入令牌,无法判断各功能(运行 work-agent-runner login)';
|
|
610
812
|
const system = systemInfo(ctx);
|
|
813
|
+
const repo = ctx.workAgent.state();
|
|
611
814
|
if (values.json) {
|
|
612
|
-
say(JSON.stringify({ version: VERSION, capabilities: result.capabilities, system, claudePath: result.claude.path, features: reply?.features || null, confirmed: reply?.confirmed ?? null, note }, null, 2));
|
|
815
|
+
say(JSON.stringify({ version: VERSION, capabilities: result.capabilities, workAgent: repo, system, claudePath: result.claude.path, features: reply?.features || null, confirmed: reply?.confirmed ?? null, note }, null, 2));
|
|
613
816
|
return 0;
|
|
614
817
|
}
|
|
615
818
|
say(`GitCode Agent 执行机器 ${VERSION} · Node ${process.versions.node} · ${process.platform}/${process.arch}`);
|
|
616
819
|
say(`系统:${describeSystem(system)}`);
|
|
820
|
+
printWorkAgent(repo);
|
|
617
821
|
printDoctor(result);
|
|
618
822
|
printFeatures(reply);
|
|
619
823
|
if (reply?.confirmed === false) say('这台机器还没有在网页上确认,确认后才会收到任务(在线 Agent → 模型配置 → 执行机器)');
|
|
@@ -631,7 +835,12 @@ async function status(values) {
|
|
|
631
835
|
say(`令牌:${config.token ? `${config.token.slice(0, 12)}…` : '未登录(运行 work-agent-runner login)'}`);
|
|
632
836
|
say(`机器标识:${config.clientId}`);
|
|
633
837
|
say(`登记名称:${config.machineName || `${os.hostname()}(主机名)`}`);
|
|
634
|
-
say(
|
|
838
|
+
say(`程序目录:${ctx.paths.baseDir}`);
|
|
839
|
+
say(`任务目录:${path.join(config.workRoot || ctx.paths.runsRoot, 'runs')}`);
|
|
840
|
+
const repo = ctx.workAgent.state();
|
|
841
|
+
say(`work-agent 仓库:${repo.ok ? `${repo.commit.slice(0, 7)}(${ctx.workAgent.repoDir})` : repo.reason}`);
|
|
842
|
+
say(`WORK_AGENT_HOME:${ctx.paths.workAgentHome}`);
|
|
843
|
+
say(`知识仓:${repo.ascend ? `${repo.repos} 个仓,含 AscendC 两仓` : repo.repos ? `${repo.repos} 个仓,缺 AscendC 两仓` : '未初始化'}(${ctx.paths.poolDir})`);
|
|
635
844
|
say(`系统:${describeSystem(systemInfo(ctx))}`);
|
|
636
845
|
say(`配置文件:${ctx.paths.configFile}`);
|
|
637
846
|
const service = serviceStatus();
|
|
@@ -641,13 +850,14 @@ async function status(values) {
|
|
|
641
850
|
say(`运行中的实例:${isRunnerAlive(holder) ? `pid ${holder}` : '无'}`);
|
|
642
851
|
if (!config.token) return 0;
|
|
643
852
|
try {
|
|
644
|
-
// A request with no effect
|
|
645
|
-
|
|
853
|
+
// A request with no effect (it must not be /heartbeat: reporting no running
|
|
854
|
+
// jobs from a second process would make the website fail this machine's jobs).
|
|
855
|
+
await clientFor(ctx).request('GET', '/ping');
|
|
646
856
|
say('连接测试:正常');
|
|
647
857
|
} catch (error) {
|
|
648
858
|
if (error instanceof ApiError) {
|
|
649
859
|
const text = {
|
|
650
|
-
404: '
|
|
860
|
+
404: '网站版本过旧(没有 /ping),请联系管理员部署新版网站',
|
|
651
861
|
401: '令牌无效或已删除(运行 work-agent-runner login)',
|
|
652
862
|
403: '这台机器已被封禁',
|
|
653
863
|
409: '令牌有效,这台机器尚未登记(运行 work-agent-runner 会自动登记,之后在网页上确认)',
|
|
@@ -711,6 +921,9 @@ async function main(argv) {
|
|
|
711
921
|
'allow-root': { type: 'boolean' },
|
|
712
922
|
'new-id': { type: 'boolean' },
|
|
713
923
|
'token-stdin': { type: 'boolean' },
|
|
924
|
+
repos: { type: 'string' },
|
|
925
|
+
'no-repos': { type: 'boolean' },
|
|
926
|
+
|
|
714
927
|
yes: { type: 'boolean', short: 'y' },
|
|
715
928
|
json: { type: 'boolean' },
|
|
716
929
|
version: { type: 'boolean', short: 'v' },
|
|
@@ -733,6 +946,8 @@ async function main(argv) {
|
|
|
733
946
|
return logout(values);
|
|
734
947
|
case 'setup':
|
|
735
948
|
return setup(values);
|
|
949
|
+
case 'update':
|
|
950
|
+
return update(values);
|
|
736
951
|
case 'doctor':
|
|
737
952
|
return doctor(values);
|
|
738
953
|
case 'status':
|