@online-tools/agent-runner 0.0.0-stage → 0.0.1-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/LICENSE +21 -0
- package/README.md +102 -2
- package/bin/gitcode-agent-runner.mjs +679 -0
- package/lib/claude-cli.mjs +85 -0
- package/lib/config.mjs +126 -0
- package/lib/connection.mjs +191 -0
- package/lib/doctor.mjs +240 -0
- package/lib/http.mjs +198 -0
- package/lib/jobs.mjs +494 -0
- package/lib/lock.mjs +66 -0
- package/lib/log.mjs +22 -0
- package/lib/packages.mjs +74 -0
- package/lib/prompt.mjs +76 -0
- package/lib/sandbox.mjs +110 -0
- package/lib/service.mjs +187 -0
- package/lib/setup.mjs +152 -0
- package/lib/skill-text.mjs +29 -0
- package/lib/skills.mjs +211 -0
- package/package.json +10 -4
package/lib/setup.mjs
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// `setup`: installs missing dependencies after the user confirms, in a
|
|
2
|
+
// terminal only (the background service never installs anything). What can
|
|
3
|
+
// be installed and the commands are fixed in packages.mjs; the website only
|
|
4
|
+
// names tools from that list (requiredTools). Commands run without a shell,
|
|
5
|
+
// one after another; a failure does not stop the rest.
|
|
6
|
+
|
|
7
|
+
import { spawn } from 'node:child_process';
|
|
8
|
+
import fs from 'node:fs';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
|
|
11
|
+
import { CLAUDE_PACKAGE, PACKAGE_NAMES, SANDBOX_TOOLS, SKILL_TOOLS, installCommands } from './packages.mjs';
|
|
12
|
+
|
|
13
|
+
export const DEFAULT_REQUIRED_TOOLS = [...SKILL_TOOLS];
|
|
14
|
+
|
|
15
|
+
function compareVersions(a, b) {
|
|
16
|
+
const parse = (value) => (/^(\d+)\.(\d+)\.(\d+)/.exec(String(value)) || [0, 0, 0, 0]).slice(1, 4).map(Number);
|
|
17
|
+
const [x, y] = [parse(a), parse(b)];
|
|
18
|
+
for (let index = 0; index < 3; index += 1) if (x[index] !== y[index]) return x[index] < y[index] ? -1 : 1;
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Whether the npm global directory is writable by this user (else sudo). */
|
|
23
|
+
export function npmPrefixWritable(prefix) {
|
|
24
|
+
for (const dir of [path.join(prefix, 'lib', 'node_modules'), path.join(prefix, 'lib'), prefix]) {
|
|
25
|
+
try {
|
|
26
|
+
fs.accessSync(dir, fs.constants.W_OK);
|
|
27
|
+
return true;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error.code !== 'ENOENT') return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The install plan (pure; tested with fake inputs):
|
|
37
|
+
* requiredTools from the website (or DEFAULT_REQUIRED_TOOLS without a token)
|
|
38
|
+
* tools doctor's { [name]: { ok } }
|
|
39
|
+
* claudeVersion '' when missing
|
|
40
|
+
* manager 'apt' | … | 'brew' | ''
|
|
41
|
+
* isRoot, sudo absolute path of sudo or ''
|
|
42
|
+
* npm, npmWritable absolute path of npm, and whether its global dir is writable
|
|
43
|
+
* Returns { commands: [[file, ...args]], manual: [[...]], missing: [names], ignored: [names], notes: [text] }.
|
|
44
|
+
*/
|
|
45
|
+
export function planSetup({
|
|
46
|
+
platform,
|
|
47
|
+
requiredTools = DEFAULT_REQUIRED_TOOLS,
|
|
48
|
+
tools,
|
|
49
|
+
claudeVersion = '',
|
|
50
|
+
minClaudeVersion = '',
|
|
51
|
+
manager = '',
|
|
52
|
+
isRoot = false,
|
|
53
|
+
sudo = '',
|
|
54
|
+
npm = '',
|
|
55
|
+
npmWritable = false,
|
|
56
|
+
}) {
|
|
57
|
+
const ignored = requiredTools.filter((name) => !SKILL_TOOLS.includes(name));
|
|
58
|
+
const wanted = SKILL_TOOLS.filter((name) => requiredTools.includes(name));
|
|
59
|
+
if (platform === 'linux') wanted.push(...SANDBOX_TOOLS);
|
|
60
|
+
const missing = wanted.filter((name) => !tools?.[name]?.ok);
|
|
61
|
+
const notes = [];
|
|
62
|
+
if (ignored.length) notes.push(`网站要求的 ${ignored.join('、')} 不在本程序可安装的范围内,请升级程序:npm i -g @online-tools/agent-runner@latest`);
|
|
63
|
+
|
|
64
|
+
const commands = [];
|
|
65
|
+
const manual = [];
|
|
66
|
+
const names = PACKAGE_NAMES[manager] || null;
|
|
67
|
+
const packages = [];
|
|
68
|
+
for (const tool of missing) {
|
|
69
|
+
const name = names?.[tool];
|
|
70
|
+
if (name) packages.push(name);
|
|
71
|
+
else if (names && name === null) notes.push(`${tool} 应由系统自带,请检查系统环境`);
|
|
72
|
+
}
|
|
73
|
+
const unique = [...new Set(packages)];
|
|
74
|
+
if (missing.length && !manager) {
|
|
75
|
+
notes.push(
|
|
76
|
+
platform === 'darwin'
|
|
77
|
+
? '没有找到 Homebrew(https://brew.sh);git 和 python3 也可以用 xcode-select --install 安装'
|
|
78
|
+
: '没有识别出包管理器,请让管理员手动安装缺少的软件',
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const packageCommands = installCommands(manager, unique);
|
|
82
|
+
if (packageCommands.length) {
|
|
83
|
+
if (manager === 'brew') commands.push(...packageCommands); // Homebrew refuses sudo
|
|
84
|
+
else if (isRoot) commands.push(...packageCommands);
|
|
85
|
+
else if (sudo) commands.push(...packageCommands.map((command) => [sudo, ...command]));
|
|
86
|
+
else {
|
|
87
|
+
manual.push(...packageCommands.map((command) => ['sudo', ...command]));
|
|
88
|
+
notes.push('没有找到 sudo:以下命令需要管理员执行');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const needsClaude = !claudeVersion || (minClaudeVersion && compareVersions(claudeVersion, minClaudeVersion) < 0);
|
|
93
|
+
if (needsClaude) {
|
|
94
|
+
if (!npm) {
|
|
95
|
+
notes.push('没有找到 npm,无法安装 claude');
|
|
96
|
+
} else {
|
|
97
|
+
const command = [npm, 'install', '-g', CLAUDE_PACKAGE];
|
|
98
|
+
if (npmWritable || isRoot) commands.push(command);
|
|
99
|
+
else if (sudo) commands.push([sudo, ...command]);
|
|
100
|
+
else manual.push(['sudo', ...command]);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return { commands, manual, missing: needsClaude ? [...missing, 'claude'] : missing, ignored, notes };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function formatCommand(command) {
|
|
107
|
+
return command.map((part) => (/^[\w@./:=+-]+$/.test(part) ? part : JSON.stringify(part))).join(' ');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Runs one command with the terminal attached (no shell); resolves with its exit code. */
|
|
111
|
+
export function runCommand(command) {
|
|
112
|
+
return new Promise((resolve) => {
|
|
113
|
+
const child = spawn(command[0], command.slice(1), { stdio: 'inherit' });
|
|
114
|
+
child.once('error', () => resolve(127));
|
|
115
|
+
child.once('close', (code) => resolve(code ?? 1));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Shows the plan, asks (unless `yes`), runs it. `interactive` false: prints
|
|
121
|
+
* what is missing and installs nothing. Returns { ran, failed: [commands] }.
|
|
122
|
+
*/
|
|
123
|
+
export async function runSetup({ plan, interactive, yes = false, confirm, say, run = runCommand }) {
|
|
124
|
+
for (const note of plan.notes) say(` ${note}`);
|
|
125
|
+
if (plan.manual.length) {
|
|
126
|
+
say('需要管理员执行:');
|
|
127
|
+
for (const command of plan.manual) say(` ${formatCommand(command)}`);
|
|
128
|
+
}
|
|
129
|
+
if (!plan.commands.length) {
|
|
130
|
+
if (!plan.missing.length) say('依赖都已就绪');
|
|
131
|
+
return { ran: false, failed: [] };
|
|
132
|
+
}
|
|
133
|
+
if (!interactive) {
|
|
134
|
+
say('需要安装依赖:请在终端运行 gitcode-agent-runner setup');
|
|
135
|
+
return { ran: false, failed: [] };
|
|
136
|
+
}
|
|
137
|
+
say('将执行以下命令:');
|
|
138
|
+
for (const command of plan.commands) say(` ${formatCommand(command)}`);
|
|
139
|
+
if (!yes && !(await confirm('是否执行?'))) {
|
|
140
|
+
say('已跳过安装;缺少的依赖只影响用到它的功能');
|
|
141
|
+
return { ran: false, failed: [] };
|
|
142
|
+
}
|
|
143
|
+
const failed = [];
|
|
144
|
+
for (const command of plan.commands) {
|
|
145
|
+
const code = await run(command);
|
|
146
|
+
if (code !== 0) {
|
|
147
|
+
failed.push(command);
|
|
148
|
+
say(`命令失败(退出码 ${code}):${formatCommand(command)}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return { ran: true, failed };
|
|
152
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// How Skill text files are rewritten for a run, shared by the website
|
|
2
|
+
// (lib/skill-bundle.js for server runs, lib/skill-manifest.js for the files
|
|
3
|
+
// sent to execution machines) and the machine program (lib/skills.mjs).
|
|
4
|
+
//
|
|
5
|
+
// Only node: built-ins and files in this directory may be imported here, and
|
|
6
|
+
// importing must have no side effects.
|
|
7
|
+
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
|
|
10
|
+
export const TEXT_EXTENSIONS = new Set([
|
|
11
|
+
'.md', '.txt', '.sh', '.bash', '.py', '.js', '.mjs', '.cjs', '.ts',
|
|
12
|
+
'.json', '.yaml', '.yml', '.toml', '.ini', '.cfg', '.conf',
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
/** True for files whose text is rewritten (by extension, case-insensitive). */
|
|
16
|
+
export function isSkillTextFile(name) {
|
|
17
|
+
return TEXT_EXTENSIONS.has(path.extname(String(name || '')).toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Agent clients scrub variables named *TOKEN*/*KEY*/*SECRET* from the
|
|
21
|
+
// environment, so Skills read the GitCode credential as GITCODE_CRED.
|
|
22
|
+
export function renameTokenVariable(text) {
|
|
23
|
+
return String(text).split('GITCODE_TOKEN').join('GITCODE_CRED');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Points the Skills' scratch directory `/tmp/work-agent` at the run's own directory. */
|
|
27
|
+
export function rewriteWorkAgentPath(text, replacement) {
|
|
28
|
+
return String(text).split('/tmp/work-agent').join(replacement);
|
|
29
|
+
}
|
package/lib/skills.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Skill cache: the website sends a job with `skillsHash`; the files come from
|
|
2
|
+
// GET /skills/manifest?hash= and /skills/file/<sha256> and are kept as
|
|
3
|
+
// <cache>/skills/objects/<sha256> file contents (checked on download)
|
|
4
|
+
// <cache>/skills/versions/<hash>.json a version's file list
|
|
5
|
+
// A version is written only once all its files are present. The 3 most
|
|
6
|
+
// recently used versions are kept; objects nobody references are removed.
|
|
7
|
+
|
|
8
|
+
import crypto from 'node:crypto';
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
|
|
12
|
+
import { isSkillTextFile, rewriteWorkAgentPath } from './skill-text.mjs';
|
|
13
|
+
|
|
14
|
+
const HASH = /^[0-9a-f]{64}$/;
|
|
15
|
+
const MAX_FILES = 10_000;
|
|
16
|
+
const MAX_FILE_BYTES = 100 * 1024 * 1024;
|
|
17
|
+
const MAX_TOTAL_BYTES = 512 * 1024 * 1024;
|
|
18
|
+
const KEEP_VERSIONS = 3;
|
|
19
|
+
const DOWNLOAD_CONCURRENCY = 4;
|
|
20
|
+
|
|
21
|
+
function sha256(data) {
|
|
22
|
+
return crypto.createHash('sha256').update(data).digest('hex');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A safe relative path: no absolute paths, '..', '.', empty segments, backslashes or NUL. */
|
|
26
|
+
export function isSafeSkillPath(value) {
|
|
27
|
+
if (typeof value !== 'string' || !value || value.length > 1024) return false;
|
|
28
|
+
if (value.includes('\\') || value.includes('\0')) return false;
|
|
29
|
+
return value.split('/').every((segment) => segment !== '' && segment !== '.' && segment !== '..');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Checks a manifest from the website; returns its files or throws. */
|
|
33
|
+
export function validateManifest(manifest, hash) {
|
|
34
|
+
if (!manifest || manifest.hash !== hash || !Array.isArray(manifest.files)) throw new Error('Skill 清单格式不正确');
|
|
35
|
+
const { files } = manifest;
|
|
36
|
+
if (files.length > MAX_FILES) throw new Error('Skill 清单文件过多');
|
|
37
|
+
// The version hash covers the list itself.
|
|
38
|
+
if (sha256(JSON.stringify(files)) !== hash) throw new Error('Skill 清单与版本号不符');
|
|
39
|
+
const paths = new Set();
|
|
40
|
+
let total = 0;
|
|
41
|
+
for (const file of files) {
|
|
42
|
+
if (!file || !isSafeSkillPath(file.path)) throw new Error(`Skill 清单包含不合法的路径:${String(file?.path).slice(0, 80)}`);
|
|
43
|
+
if (!HASH.test(file.sha256 || '')) throw new Error(`Skill 文件校验值不正确:${file.path}`);
|
|
44
|
+
if (!Number.isInteger(file.size) || file.size < 0 || file.size > MAX_FILE_BYTES) throw new Error(`Skill 文件大小不正确:${file.path}`);
|
|
45
|
+
if (!Number.isInteger(file.mode) || file.mode < 0) throw new Error(`Skill 文件权限不正确:${file.path}`);
|
|
46
|
+
if (paths.has(file.path)) throw new Error(`Skill 清单路径重复:${file.path}`);
|
|
47
|
+
paths.add(file.path);
|
|
48
|
+
total += file.size;
|
|
49
|
+
}
|
|
50
|
+
if (total > MAX_TOTAL_BYTES) throw new Error('Skill 文件总大小超出限制');
|
|
51
|
+
for (const file of files) {
|
|
52
|
+
// A file may not also be a directory of another file.
|
|
53
|
+
const segments = file.path.split('/');
|
|
54
|
+
for (let index = 1; index < segments.length; index += 1) {
|
|
55
|
+
if (paths.has(segments.slice(0, index).join('/'))) throw new Error(`Skill 清单路径冲突:${file.path}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return files.map((file) => ({ path: file.path, sha256: file.sha256, size: file.size, mode: file.mode & 0o777 }));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function writeAtomic(file, data) {
|
|
62
|
+
const temp = `${file}.${process.pid}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
63
|
+
fs.writeFileSync(temp, data, { mode: 0o600 });
|
|
64
|
+
fs.renameSync(temp, file);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function eachLimit(items, limit, fn) {
|
|
68
|
+
let next = 0;
|
|
69
|
+
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
70
|
+
while (next < items.length) await fn(items[next++]);
|
|
71
|
+
});
|
|
72
|
+
await Promise.all(workers);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class SkillCache {
|
|
76
|
+
constructor({ dir, client }) {
|
|
77
|
+
this.dir = dir;
|
|
78
|
+
this.client = client;
|
|
79
|
+
this.objectsDir = path.join(dir, 'objects');
|
|
80
|
+
this.versionsDir = path.join(dir, 'versions');
|
|
81
|
+
this.syncing = new Map(); // hash → Promise (one sync per version at a time)
|
|
82
|
+
this.pinned = new Map(); // hash → jobs copying it right now
|
|
83
|
+
fs.mkdirSync(this.objectsDir, { recursive: true, mode: 0o700 });
|
|
84
|
+
fs.mkdirSync(this.versionsDir, { recursive: true, mode: 0o700 });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
objectPath(hash) {
|
|
88
|
+
return path.join(this.objectsDir, hash);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
versionPath(hash) {
|
|
92
|
+
return path.join(this.versionsDir, `${hash}.json`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
readVersion(hash) {
|
|
96
|
+
try {
|
|
97
|
+
const files = JSON.parse(fs.readFileSync(this.versionPath(hash), 'utf8')).files;
|
|
98
|
+
if (!Array.isArray(files)) return null;
|
|
99
|
+
for (const file of files) {
|
|
100
|
+
if (!isSafeSkillPath(file.path) || fs.statSync(this.objectPath(file.sha256)).size !== file.size) return null;
|
|
101
|
+
}
|
|
102
|
+
return files;
|
|
103
|
+
} catch {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The files of version `hash`, downloading what is missing. */
|
|
109
|
+
async ensure(hash) {
|
|
110
|
+
if (!HASH.test(String(hash))) throw new Error('Skill 版本号不正确');
|
|
111
|
+
const cached = this.readVersion(hash);
|
|
112
|
+
if (cached) {
|
|
113
|
+
const now = new Date();
|
|
114
|
+
fs.utimesSync(this.versionPath(hash), now, now); // most recently used
|
|
115
|
+
return cached;
|
|
116
|
+
}
|
|
117
|
+
if (!this.syncing.has(hash)) {
|
|
118
|
+
this.syncing.set(hash, this.sync(hash).finally(() => this.syncing.delete(hash)));
|
|
119
|
+
}
|
|
120
|
+
return this.syncing.get(hash);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async sync(hash) {
|
|
124
|
+
const files = validateManifest(await this.client.request('GET', `/skills/manifest?hash=${hash}`), hash);
|
|
125
|
+
const missing = new Map();
|
|
126
|
+
for (const file of files) {
|
|
127
|
+
let present = false;
|
|
128
|
+
try {
|
|
129
|
+
present = fs.statSync(this.objectPath(file.sha256)).size === file.size;
|
|
130
|
+
} catch {
|
|
131
|
+
// Not cached.
|
|
132
|
+
}
|
|
133
|
+
if (!present) missing.set(file.sha256, file);
|
|
134
|
+
}
|
|
135
|
+
await eachLimit([...missing.values()], DOWNLOAD_CONCURRENCY, async (file) => {
|
|
136
|
+
const data = await this.client.download(`/skills/file/${file.sha256}`);
|
|
137
|
+
if (data.length !== file.size || sha256(data) !== file.sha256) throw new Error(`Skill 文件校验失败:${file.path}`);
|
|
138
|
+
writeAtomic(this.objectPath(file.sha256), data);
|
|
139
|
+
});
|
|
140
|
+
writeAtomic(this.versionPath(hash), JSON.stringify({ hash, files }));
|
|
141
|
+
this.prune();
|
|
142
|
+
return files;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Copies version `hash` (from ensure()) into `skillsDir`; text files get
|
|
147
|
+
* /tmp/work-agent pointed at `workAgentDir`.
|
|
148
|
+
*/
|
|
149
|
+
copyInto(hash, files, skillsDir, workAgentDir) {
|
|
150
|
+
this.pinned.set(hash, (this.pinned.get(hash) || 0) + 1);
|
|
151
|
+
try {
|
|
152
|
+
const root = path.resolve(skillsDir);
|
|
153
|
+
for (const file of files) {
|
|
154
|
+
const target = path.resolve(root, ...file.path.split('/'));
|
|
155
|
+
if (!target.startsWith(`${root}${path.sep}`)) throw new Error(`Skill 路径越界:${file.path}`);
|
|
156
|
+
fs.mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
157
|
+
let data = fs.readFileSync(this.objectPath(file.sha256));
|
|
158
|
+
if (isSkillTextFile(file.path)) {
|
|
159
|
+
const text = data.toString('utf8');
|
|
160
|
+
if (text.includes('/tmp/work-agent')) data = Buffer.from(rewriteWorkAgentPath(text, workAgentDir), 'utf8');
|
|
161
|
+
}
|
|
162
|
+
fs.writeFileSync(target, data, { mode: file.mode & 0o777, flag: 'wx' });
|
|
163
|
+
fs.chmodSync(target, file.mode & 0o777);
|
|
164
|
+
}
|
|
165
|
+
} finally {
|
|
166
|
+
const left = (this.pinned.get(hash) || 1) - 1;
|
|
167
|
+
if (left > 0) this.pinned.set(hash, left);
|
|
168
|
+
else this.pinned.delete(hash);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Keeps the 3 most recently used versions (and any being copied); drops unreferenced objects. */
|
|
173
|
+
prune() {
|
|
174
|
+
let entries = [];
|
|
175
|
+
try {
|
|
176
|
+
entries = fs
|
|
177
|
+
.readdirSync(this.versionsDir)
|
|
178
|
+
.filter((name) => /^[0-9a-f]{64}\.json$/.test(name))
|
|
179
|
+
.map((name) => ({ hash: name.slice(0, 64), mtime: fs.statSync(path.join(this.versionsDir, name)).mtimeMs }))
|
|
180
|
+
.sort((a, b) => b.mtime - a.mtime);
|
|
181
|
+
} catch {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const keep = new Set(entries.slice(0, KEEP_VERSIONS).map((entry) => entry.hash));
|
|
185
|
+
for (const hash of this.pinned.keys()) keep.add(hash);
|
|
186
|
+
for (const hash of this.syncing.keys()) keep.add(hash);
|
|
187
|
+
for (const entry of entries) {
|
|
188
|
+
if (!keep.has(entry.hash)) fs.rmSync(this.versionPath(entry.hash), { force: true });
|
|
189
|
+
}
|
|
190
|
+
const referenced = new Set();
|
|
191
|
+
for (const hash of keep) {
|
|
192
|
+
try {
|
|
193
|
+
for (const file of JSON.parse(fs.readFileSync(this.versionPath(hash), 'utf8')).files) referenced.add(file.sha256);
|
|
194
|
+
} catch {
|
|
195
|
+
// A version being synced has no file yet: its objects are written as they arrive.
|
|
196
|
+
if (this.syncing.has(hash)) return; // do not delete objects of an unfinished sync
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
for (const name of fs.readdirSync(this.objectsDir)) {
|
|
200
|
+
if (HASH.test(name) && !referenced.has(name)) fs.rmSync(path.join(this.objectsDir, name), { force: true });
|
|
201
|
+
else if (name.endsWith('.tmp')) {
|
|
202
|
+
// Left by an interrupted download long ago.
|
|
203
|
+
try {
|
|
204
|
+
if (Date.now() - fs.statSync(path.join(this.objectsDir, name)).mtimeMs > 3_600_000) fs.rmSync(path.join(this.objectsDir, name), { force: true });
|
|
205
|
+
} catch {
|
|
206
|
+
// Ignore.
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@online-tools/agent-runner",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1-alpha-1",
|
|
4
|
+
"description": "GitCode 在线工具的 Agent 执行机器:在自己的机器上运行 PR 描述、代码检视任务",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": { "gitcode-agent-runner": "bin/gitcode-agent-runner.mjs" },
|
|
8
|
+
"files": ["bin/", "lib/", "README.md", "LICENSE"],
|
|
9
|
+
"engines": { "node": ">=22.21.0" },
|
|
10
|
+
"os": ["linux", "darwin"],
|
|
11
|
+
"publishConfig": { "access": "public" }
|
|
12
|
+
}
|