@online-tools/agent-runner 0.0.0-stage → 0.0.1-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/LICENSE +21 -0
- package/README.md +104 -2
- package/bin/gitcode-agent-runner.cjs +139 -0
- package/bin/main.mjs +675 -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/node-runtime.cjs +594 -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/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-2",
|
|
4
|
+
"description": "GitCode 在线工具的 Agent 执行机器:在自己的机器上运行 PR 描述、代码检视任务",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": { "gitcode-agent-runner": "bin/gitcode-agent-runner.cjs" },
|
|
8
|
+
"files": ["bin/", "lib/", "README.md", "LICENSE"],
|
|
9
|
+
"engines": { "node": ">=10.12.0" },
|
|
10
|
+
"os": ["linux", "darwin"],
|
|
11
|
+
"publishConfig": { "access": "public" }
|
|
12
|
+
}
|