@online-tools/work-agent-runner 0.0.1-alpha-5 → 0.0.2-alpha-2
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 +91 -16
- package/bin/main.mjs +271 -44
- package/lib/config.mjs +136 -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 +174 -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 +525 -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 { WorkAgent, freeBytes } 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
|
+
下载知识仓(work-agent 的必要仓清单;需在终端里运行)
|
|
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,37 @@ 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) {
|
|
239
|
+
prepareWorkAgentHome(paths.workAgentHome, { poolDir: workAgent.mountablePool(), link: paths.workAgentLink });
|
|
240
|
+
}
|
|
241
|
+
return { paths, config, server, workRoot, realHome: realHome(), workAgent };
|
|
198
242
|
}
|
|
199
243
|
|
|
200
244
|
function save(ctx) {
|
|
@@ -217,7 +261,7 @@ function connect(ctx, client, capabilities) {
|
|
|
217
261
|
}
|
|
218
262
|
|
|
219
263
|
function systemInfo(ctx) {
|
|
220
|
-
return collectSystemInfo({ workRoot: ctx.workRoot || ctx.config.workRoot ||
|
|
264
|
+
return collectSystemInfo({ workRoot: ctx.workRoot || ctx.config.workRoot || ctx.paths.runsRoot });
|
|
221
265
|
}
|
|
222
266
|
|
|
223
267
|
/** After /connect: what the machine is called there, and whether its owner confirmed it. */
|
|
@@ -256,7 +300,12 @@ function networkReason(error) {
|
|
|
256
300
|
|
|
257
301
|
/** doctor + remembering a claude found outside PATH. */
|
|
258
302
|
async function doctorFor(ctx) {
|
|
259
|
-
const result = await runDoctor({
|
|
303
|
+
const result = await runDoctor({
|
|
304
|
+
configuredClaude: ctx.config.claudePath || '',
|
|
305
|
+
workRoot: ctx.workRoot,
|
|
306
|
+
realHome: ctx.realHome,
|
|
307
|
+
workAgent: ctx.workAgent,
|
|
308
|
+
});
|
|
260
309
|
const remembered = result.claude.viaNpmPrefix || result.claude.path === ctx.config.claudePath ? result.claude.path : '';
|
|
261
310
|
if ((ctx.config.claudePath || '') !== remembered) {
|
|
262
311
|
ctx.config.claudePath = remembered;
|
|
@@ -335,8 +384,100 @@ function toolLabel(name) {
|
|
|
335
384
|
return name === 'bwrap' ? 'bubblewrap' : name;
|
|
336
385
|
}
|
|
337
386
|
|
|
387
|
+
/** Updates every repository in the knowledge pool. Never fatal. */
|
|
388
|
+
async function updatePool(ctx) {
|
|
389
|
+
if (!ctx.workAgent.mountablePool()) return;
|
|
390
|
+
await ctx.workAgent
|
|
391
|
+
.updatePool({ say: (line) => log.info(line) })
|
|
392
|
+
.catch((error) => log.warn(`知识仓更新失败:${error.message}`));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Brings the clone up to date. Never fatal: the website shows the reason instead. */
|
|
396
|
+
async function refreshWorkAgent(ctx, { say: out = say } = {}) {
|
|
397
|
+
try {
|
|
398
|
+
return await ctx.workAgent.refresh();
|
|
399
|
+
} catch (error) {
|
|
400
|
+
out(`work-agent 仓库检查失败:${error.message}`);
|
|
401
|
+
return ctx.workAgent.state();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** ",3 天前更新" for the knowledge pool line ('' when it was never fetched). */
|
|
406
|
+
function poolAge(fetchedAt) {
|
|
407
|
+
if (!fetchedAt) return '';
|
|
408
|
+
const days = Math.floor((Date.now() - fetchedAt) / 86_400_000);
|
|
409
|
+
return `,${days <= 0 ? '今天' : `${days} 天前`}更新`;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/** The Skill source and the knowledge pool, in two lines. */
|
|
413
|
+
function printWorkAgent(state) {
|
|
414
|
+
if (state.ok) say(`✓ work-agent ${state.commit.slice(0, 7)}${state.committedAt ? `(${state.committedAt.slice(0, 10)})` : ''}`);
|
|
415
|
+
else say(`✗ work-agent 仓库不可用:${state.reason}`);
|
|
416
|
+
if (state.incomplete?.length) {
|
|
417
|
+
say(` 知识仓 ${state.incomplete.join('、')} 不完整:请删除后重新运行 work-agent-runner setup`);
|
|
418
|
+
} else if (state.repos) {
|
|
419
|
+
const what = state.ascend ? `${state.repos} 个仓(含 AscendC 两仓)` : `${state.repos} 个仓,缺 AscendC 两仓(代码检视的 AscendC 规范域不检查)`;
|
|
420
|
+
say(` 知识仓:${what}${poolAge(state.poolFetchedAt)}`);
|
|
421
|
+
} else {
|
|
422
|
+
say(' 知识仓:未初始化(可选;运行 work-agent-runner setup 下载)');
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const REPO_NAME = /^[A-Za-z0-9._-]{1,80}$/;
|
|
427
|
+
|
|
428
|
+
/** `--repos a,b` → names, validated the way work-agent.py validates them. */
|
|
429
|
+
function splitRepos(value) {
|
|
430
|
+
const names = String(value).split(/[,\s]+/).filter(Boolean);
|
|
431
|
+
for (const name of names) {
|
|
432
|
+
if (!REPO_NAME.test(name) || name.startsWith('.')) throw new ExitError(1, `仓名不正确:${name}`);
|
|
433
|
+
}
|
|
434
|
+
if (!names.length) throw new ExitError(1, '--repos 需要至少一个仓名');
|
|
435
|
+
return names;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Offers the knowledge pool — the repositories of work-agent's own essential
|
|
440
|
+
* list — in a terminal only. Never downloads anything without a yes: `--repos`
|
|
441
|
+
* picks the repositories instead, `--no-repos` skips the question.
|
|
442
|
+
*/
|
|
443
|
+
async function offerPool(ctx, values) {
|
|
444
|
+
if (values['no-repos']) return;
|
|
445
|
+
const wanted = values.repos === undefined ? null : splitRepos(values.repos);
|
|
446
|
+
const { poolDir, workAgentHome } = ctx.paths;
|
|
447
|
+
const pool = ctx.workAgent.poolState();
|
|
448
|
+
if (pool.incomplete.length) {
|
|
449
|
+
say(`知识仓 ${pool.incomplete.join('、')} 不完整:请先删除 ${poolDir} 下这些目录`);
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
if (pool.repos && values.repos === undefined) {
|
|
453
|
+
say(`知识仓已就绪(${poolDir},${pool.repos} 个仓)`);
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
if (wanted) say(`将下载到 ${poolDir}:${wanted.join('、')}`);
|
|
457
|
+
else {
|
|
458
|
+
say(`知识仓用于「代码检视」查 CANN / AscendC 的权威文档,下载到 ${poolDir}`);
|
|
459
|
+
// The list file only exists once work-agent.py has run init at least once.
|
|
460
|
+
const listed = ctx.workAgent.essentialRepos();
|
|
461
|
+
if (listed.length) say(`要下载的仓(${ctx.workAgent.essentialFile}):${listed.join('、')}`);
|
|
462
|
+
else say(`要下载的是 work-agent 的必要仓清单,首次初始化时会生成 ${ctx.workAgent.essentialFile},之后可以自己编辑再运行 setup`);
|
|
463
|
+
const free = freeBytes(poolDir);
|
|
464
|
+
say(`仓比较多,下载可能要很久;该磁盘当前可用 ${(free / 1073741824).toFixed(1)} GB。不下载也能用,只是相关规范不做检查。`);
|
|
465
|
+
}
|
|
466
|
+
if (!values.yes && !(await askYesNo('是否现在下载?'))) {
|
|
467
|
+
say('已跳过;以后随时可以再运行 work-agent-runner setup');
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
try {
|
|
471
|
+
await ctx.workAgent.initPool({ repos: wanted, say });
|
|
472
|
+
prepareWorkAgentHome(workAgentHome, { poolDir: ctx.workAgent.mountablePool(), link: ctx.paths.workAgentLink });
|
|
473
|
+
} catch (error) {
|
|
474
|
+
say(`下载知识仓失败:${error.message}`);
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
338
478
|
function neededBy(tool) {
|
|
339
479
|
if (tool === 'bwrap' || tool === 'socat') return '沙箱需要(所有功能)';
|
|
480
|
+
if (tool === 'rg') return '检索 AscendC 知识仓需要';
|
|
340
481
|
const features = Object.entries(FEATURE_TOOLS).filter(([, tools]) => tools.includes(tool)).map(([feature]) => FEATURE_LABELS[feature]);
|
|
341
482
|
return features.length === 3 ? '所有功能需要' : `${features.join('、')}需要`;
|
|
342
483
|
}
|
|
@@ -371,7 +512,7 @@ function printFeatures(reply) {
|
|
|
371
512
|
if (reply.outdated) say(`程序版本过低,请运行 ${UPDATE_COMMAND}`);
|
|
372
513
|
}
|
|
373
514
|
|
|
374
|
-
function setupPlan(result, reply) {
|
|
515
|
+
function setupPlan(result, reply, { poolWanted = false } = {}) {
|
|
375
516
|
const npm = which('npm');
|
|
376
517
|
let npmWritable = false;
|
|
377
518
|
if (npm) {
|
|
@@ -381,6 +522,8 @@ function setupPlan(result, reply) {
|
|
|
381
522
|
return planSetup({
|
|
382
523
|
platform: process.platform,
|
|
383
524
|
requiredTools: Array.isArray(reply?.requiredTools) ? reply.requiredTools : DEFAULT_REQUIRED_TOOLS,
|
|
525
|
+
// The website does not ask for rg; the knowledge pool needs it.
|
|
526
|
+
extraTools: poolWanted ? ['rg'] : [],
|
|
384
527
|
tools: result.tools,
|
|
385
528
|
claudeVersion: result.claudeVersion,
|
|
386
529
|
minClaudeVersion: reply?.minClaudeVersion || '',
|
|
@@ -415,15 +558,19 @@ async function start(values) {
|
|
|
415
558
|
try {
|
|
416
559
|
say(`GitCode Agent 执行机器 ${VERSION}`);
|
|
417
560
|
if (legacyServiceInstalled()) log.warn(LEGACY_SERVICE_HINT);
|
|
418
|
-
// Whatever a previous run left behind.
|
|
561
|
+
// Whatever a previous run left behind (probe directories are runs/p******).
|
|
419
562
|
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
563
|
const interactive = isInteractive() && !process.env.GC_RUNNER_SERVICE;
|
|
424
564
|
if (!ctx.config.token && !interactive) {
|
|
425
565
|
throw new ExitError(ATTENTION, '还没有接入令牌:请先在终端运行 work-agent-runner login');
|
|
426
566
|
}
|
|
567
|
+
say('检查 work-agent 仓库……');
|
|
568
|
+
printWorkAgent(await refreshWorkAgent(ctx));
|
|
569
|
+
// No job is running yet, so it is safe to empty $WORK_AGENT_HOME/tmp, drop
|
|
570
|
+
// old logs (work-agent's own cleanup) and pull the knowledge pool — a
|
|
571
|
+
// `git pull` must never replace files a running review is reading.
|
|
572
|
+
await ctx.workAgent.cleanup({ say: (line) => log.info(line) }).catch(() => {});
|
|
573
|
+
await updatePool(ctx);
|
|
427
574
|
say('检查依赖和沙箱……');
|
|
428
575
|
let result = await doctorFor(ctx);
|
|
429
576
|
let client;
|
|
@@ -436,9 +583,10 @@ async function start(values) {
|
|
|
436
583
|
sayRegistered(ctx, reply);
|
|
437
584
|
}
|
|
438
585
|
printDoctor(result);
|
|
439
|
-
|
|
586
|
+
const firstRun = !ctx.config.setupDone && interactive;
|
|
587
|
+
if (firstRun) {
|
|
440
588
|
const outcome = await runSetup({
|
|
441
|
-
plan: setupPlan(result, reply),
|
|
589
|
+
plan: setupPlan(result, reply, { poolWanted: !values['no-repos'] }),
|
|
442
590
|
interactive: true,
|
|
443
591
|
confirm: (question) => askYesNo(question),
|
|
444
592
|
say,
|
|
@@ -449,9 +597,12 @@ async function start(values) {
|
|
|
449
597
|
if (outcome.ran) {
|
|
450
598
|
say('重新检查……');
|
|
451
599
|
result = await doctorFor(ctx);
|
|
452
|
-
reply = await connectSaved(ctx, client, result.capabilities);
|
|
453
600
|
printDoctor(result);
|
|
454
601
|
}
|
|
602
|
+
// The same question `setup` asks, so a first run settles everything.
|
|
603
|
+
await offerPool(ctx, values);
|
|
604
|
+
result = await doctorFor(ctx);
|
|
605
|
+
reply = await connectSaved(ctx, client, result.capabilities);
|
|
455
606
|
}
|
|
456
607
|
printFeatures(reply);
|
|
457
608
|
say('等待任务……(Ctrl+C 退出;后台运行:work-agent-runner service install)');
|
|
@@ -473,7 +624,7 @@ async function serve(ctx, client, initial, reply) {
|
|
|
473
624
|
};
|
|
474
625
|
const jobs = new JobManager({
|
|
475
626
|
client,
|
|
476
|
-
|
|
627
|
+
workAgent: ctx.workAgent,
|
|
477
628
|
workRoot: ctx.workRoot,
|
|
478
629
|
claudePath: initial.claude.path,
|
|
479
630
|
realHome: ctx.realHome,
|
|
@@ -481,16 +632,44 @@ async function serve(ctx, client, initial, reply) {
|
|
|
481
632
|
onFatal,
|
|
482
633
|
});
|
|
483
634
|
let capabilities = initial.capabilities;
|
|
635
|
+
let checking = false;
|
|
636
|
+
// Every hour: the knowledge pool, then the dependency and sandbox check.
|
|
484
637
|
const doctorTimer = setInterval(async () => {
|
|
638
|
+
// A hung pull must not stack up another round.
|
|
639
|
+
if (checking) return;
|
|
640
|
+
checking = true;
|
|
485
641
|
try {
|
|
642
|
+
// Only between jobs: a pull would replace files under a running review.
|
|
643
|
+
if (!jobs.runningIds().length) await updatePool(ctx);
|
|
486
644
|
const fresh = await doctorFor(ctx);
|
|
487
645
|
capabilities = fresh.capabilities;
|
|
488
646
|
if (fresh.claude.path) jobs.claudePath = fresh.claude.path;
|
|
489
647
|
} catch (error) {
|
|
490
648
|
log.warn(`定期检查失败:${error.message}`);
|
|
649
|
+
} finally {
|
|
650
|
+
checking = false;
|
|
491
651
|
}
|
|
492
|
-
},
|
|
652
|
+
}, POOL_UPDATE_MS);
|
|
493
653
|
doctorTimer.unref();
|
|
654
|
+
// Every day at 08:00 local time: the work-agent clone (the Skill source).
|
|
655
|
+
let repoTimer = null;
|
|
656
|
+
const scheduleRepoUpdate = () => {
|
|
657
|
+
const next = new Date();
|
|
658
|
+
next.setHours(REPO_UPDATE_HOUR, 0, 0, 0);
|
|
659
|
+
if (next.getTime() <= Date.now()) next.setDate(next.getDate() + 1);
|
|
660
|
+
repoTimer = setTimeout(async () => {
|
|
661
|
+
printWorkAgent(await refreshWorkAgent(ctx, { say: (line) => log.info(line) }));
|
|
662
|
+
try {
|
|
663
|
+
capabilities = (await doctorFor(ctx)).capabilities;
|
|
664
|
+
} catch (error) {
|
|
665
|
+
log.warn(`定期检查失败:${error.message}`);
|
|
666
|
+
}
|
|
667
|
+
// Re-armed each time, so it stays on 08:00 across a change of day length.
|
|
668
|
+
scheduleRepoUpdate();
|
|
669
|
+
}, next.getTime() - Date.now());
|
|
670
|
+
repoTimer.unref();
|
|
671
|
+
};
|
|
672
|
+
scheduleRepoUpdate();
|
|
494
673
|
let stopping = false;
|
|
495
674
|
const onSignal = (signal) => {
|
|
496
675
|
if (stopping) return;
|
|
@@ -513,7 +692,9 @@ async function serve(ctx, client, initial, reply) {
|
|
|
513
692
|
signal: controller.signal,
|
|
514
693
|
});
|
|
515
694
|
clearInterval(doctorTimer);
|
|
695
|
+
clearTimeout(repoTimer);
|
|
516
696
|
const outcome = fatal || result;
|
|
697
|
+
ctx.workAgent.stop();
|
|
517
698
|
await jobs.stopAll(5000);
|
|
518
699
|
if (outcome.clearToken) {
|
|
519
700
|
ctx.config.token = '';
|
|
@@ -558,6 +739,8 @@ async function setup(values) {
|
|
|
558
739
|
if (restarted !== null) return restarted;
|
|
559
740
|
if (!isInteractive()) throw new ExitError(ATTENTION, 'setup 需要在终端里运行(安装前会请你确认)');
|
|
560
741
|
const ctx = loadContext(values);
|
|
742
|
+
say('检查 work-agent 仓库……');
|
|
743
|
+
printWorkAgent(await refreshWorkAgent(ctx));
|
|
561
744
|
say('检查依赖和沙箱……');
|
|
562
745
|
let result = await doctorFor(ctx);
|
|
563
746
|
let client = null;
|
|
@@ -572,8 +755,9 @@ async function setup(values) {
|
|
|
572
755
|
}
|
|
573
756
|
}
|
|
574
757
|
printDoctor(result);
|
|
758
|
+
const poolWanted = !values['no-repos'];
|
|
575
759
|
const outcome = await runSetup({
|
|
576
|
-
plan: setupPlan(result, reply),
|
|
760
|
+
plan: setupPlan(result, reply, { poolWanted }),
|
|
577
761
|
interactive: true,
|
|
578
762
|
yes: Boolean(values.yes),
|
|
579
763
|
confirm: (question) => askYesNo(question),
|
|
@@ -584,13 +768,42 @@ async function setup(values) {
|
|
|
584
768
|
if (outcome.ran) {
|
|
585
769
|
say('重新检查……');
|
|
586
770
|
result = await doctorFor(ctx);
|
|
587
|
-
if (client) reply = await connect(ctx, client, result.capabilities).catch(() => reply);
|
|
588
771
|
printDoctor(result);
|
|
589
772
|
}
|
|
773
|
+
// After the tools: the pool is searched with rg.
|
|
774
|
+
await offerPool(ctx, values);
|
|
775
|
+
if (client) reply = await connect(ctx, client, (await doctorFor(ctx)).capabilities).catch(() => reply);
|
|
590
776
|
printFeatures(reply);
|
|
591
777
|
return outcome.failed.length ? 1 : 0;
|
|
592
778
|
}
|
|
593
779
|
|
|
780
|
+
/** `update`: both repositories, now. */
|
|
781
|
+
async function update(values) {
|
|
782
|
+
checkSystem();
|
|
783
|
+
const restarted = await applyProxy();
|
|
784
|
+
if (restarted !== null) return restarted;
|
|
785
|
+
const ctx = loadContext(values);
|
|
786
|
+
let failed = false;
|
|
787
|
+
try {
|
|
788
|
+
const state = await ctx.workAgent.refresh();
|
|
789
|
+
printWorkAgent(state);
|
|
790
|
+
failed = !state.ok;
|
|
791
|
+
if (ctx.workAgent.mountablePool()) await ctx.workAgent.updatePool({ say });
|
|
792
|
+
} catch (error) {
|
|
793
|
+
say(error.message);
|
|
794
|
+
failed = true;
|
|
795
|
+
}
|
|
796
|
+
if (ctx.config.token) {
|
|
797
|
+
// So the website shows the new commit without waiting for the hourly check.
|
|
798
|
+
try {
|
|
799
|
+
printFeatures(await connect(ctx, clientFor(ctx), (await doctorFor(ctx)).capabilities));
|
|
800
|
+
} catch {
|
|
801
|
+
say('(没能把新状态报给网站,程序下次连接时会报)');
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
return failed ? 1 : 0;
|
|
805
|
+
}
|
|
806
|
+
|
|
594
807
|
async function doctor(values) {
|
|
595
808
|
checkSystem();
|
|
596
809
|
const restarted = await applyProxy();
|
|
@@ -608,12 +821,14 @@ async function doctor(values) {
|
|
|
608
821
|
}
|
|
609
822
|
} else note = '还没有接入令牌,无法判断各功能(运行 work-agent-runner login)';
|
|
610
823
|
const system = systemInfo(ctx);
|
|
824
|
+
const repo = ctx.workAgent.state();
|
|
611
825
|
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));
|
|
826
|
+
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
827
|
return 0;
|
|
614
828
|
}
|
|
615
829
|
say(`GitCode Agent 执行机器 ${VERSION} · Node ${process.versions.node} · ${process.platform}/${process.arch}`);
|
|
616
830
|
say(`系统:${describeSystem(system)}`);
|
|
831
|
+
printWorkAgent(repo);
|
|
617
832
|
printDoctor(result);
|
|
618
833
|
printFeatures(reply);
|
|
619
834
|
if (reply?.confirmed === false) say('这台机器还没有在网页上确认,确认后才会收到任务(在线 Agent → 模型配置 → 执行机器)');
|
|
@@ -631,7 +846,13 @@ async function status(values) {
|
|
|
631
846
|
say(`令牌:${config.token ? `${config.token.slice(0, 12)}…` : '未登录(运行 work-agent-runner login)'}`);
|
|
632
847
|
say(`机器标识:${config.clientId}`);
|
|
633
848
|
say(`登记名称:${config.machineName || `${os.hostname()}(主机名)`}`);
|
|
634
|
-
say(
|
|
849
|
+
say(`程序目录:${ctx.paths.baseDir}`);
|
|
850
|
+
say(`任务目录:${path.join(config.workRoot || ctx.paths.runsRoot, 'runs')}`);
|
|
851
|
+
const repo = ctx.workAgent.state();
|
|
852
|
+
say(`work-agent 仓库:${repo.ok ? `${repo.commit.slice(0, 7)}(${ctx.workAgent.repoDir})` : repo.reason}`);
|
|
853
|
+
say(`WORK_AGENT_HOME:${ctx.paths.workAgentHome}`);
|
|
854
|
+
const poolRepos = ctx.workAgent.poolRepos();
|
|
855
|
+
say(`知识仓:${poolRepos.length ? `${poolRepos.length} 个仓(${poolRepos.join('、')})` : '未初始化'}(${ctx.paths.poolDir})`);
|
|
635
856
|
say(`系统:${describeSystem(systemInfo(ctx))}`);
|
|
636
857
|
say(`配置文件:${ctx.paths.configFile}`);
|
|
637
858
|
const service = serviceStatus();
|
|
@@ -641,13 +862,14 @@ async function status(values) {
|
|
|
641
862
|
say(`运行中的实例:${isRunnerAlive(holder) ? `pid ${holder}` : '无'}`);
|
|
642
863
|
if (!config.token) return 0;
|
|
643
864
|
try {
|
|
644
|
-
// A request with no effect
|
|
645
|
-
|
|
865
|
+
// A request with no effect (it must not be /heartbeat: reporting no running
|
|
866
|
+
// jobs from a second process would make the website fail this machine's jobs).
|
|
867
|
+
await clientFor(ctx).request('GET', '/ping');
|
|
646
868
|
say('连接测试:正常');
|
|
647
869
|
} catch (error) {
|
|
648
870
|
if (error instanceof ApiError) {
|
|
649
871
|
const text = {
|
|
650
|
-
404: '
|
|
872
|
+
404: '网站版本过旧(没有 /ping),请联系管理员部署新版网站',
|
|
651
873
|
401: '令牌无效或已删除(运行 work-agent-runner login)',
|
|
652
874
|
403: '这台机器已被封禁',
|
|
653
875
|
409: '令牌有效,这台机器尚未登记(运行 work-agent-runner 会自动登记,之后在网页上确认)',
|
|
@@ -711,6 +933,9 @@ async function main(argv) {
|
|
|
711
933
|
'allow-root': { type: 'boolean' },
|
|
712
934
|
'new-id': { type: 'boolean' },
|
|
713
935
|
'token-stdin': { type: 'boolean' },
|
|
936
|
+
repos: { type: 'string' },
|
|
937
|
+
'no-repos': { type: 'boolean' },
|
|
938
|
+
|
|
714
939
|
yes: { type: 'boolean', short: 'y' },
|
|
715
940
|
json: { type: 'boolean' },
|
|
716
941
|
version: { type: 'boolean', short: 'v' },
|
|
@@ -733,6 +958,8 @@ async function main(argv) {
|
|
|
733
958
|
return logout(values);
|
|
734
959
|
case 'setup':
|
|
735
960
|
return setup(values);
|
|
961
|
+
case 'update':
|
|
962
|
+
return update(values);
|
|
736
963
|
case 'doctor':
|
|
737
964
|
return doctor(values);
|
|
738
965
|
case 'status':
|