@saturn-universe/runtime-host 0.0.0-stage → 0.1.0

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.
Files changed (116) hide show
  1. package/LICENSE +661 -0
  2. package/dist/builtin-plugins/saturn-plugin-ctf/README.md +13 -0
  3. package/dist/builtin-plugins/saturn-plugin-ctf/index.mjs +1473 -0
  4. package/dist/builtin-plugins/saturn-plugin-ctf/package.json +10 -0
  5. package/dist/builtin-plugins/saturn-plugin-ctf/src/board.mjs +321 -0
  6. package/dist/builtin-plugins/saturn-plugin-ctf/src/ledger.mjs +1143 -0
  7. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/ctfd.mjs +154 -0
  8. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/ctfplus.mjs +769 -0
  9. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/gzctf.mjs +130 -0
  10. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/http.mjs +336 -0
  11. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/index.mjs +133 -0
  12. package/dist/builtin-plugins/saturn-plugin-ctf/src/platform/lease.mjs +153 -0
  13. package/dist/builtin-plugins/saturn-plugin-ctf/src/server.mjs +336 -0
  14. package/dist/builtin-plugins/saturn-plugin-ctf/src/view.mjs +714 -0
  15. package/dist/command-dispatch.d.ts +10 -0
  16. package/dist/command-dispatch.js +65 -0
  17. package/dist/completion-judge.d.ts +10 -0
  18. package/dist/completion-judge.js +156 -0
  19. package/dist/composition.d.ts +108 -0
  20. package/dist/composition.js +1180 -0
  21. package/dist/configuration.d.ts +49 -0
  22. package/dist/configuration.js +51 -0
  23. package/dist/context-extensions.d.ts +681 -0
  24. package/dist/context-extensions.js +289 -0
  25. package/dist/cordis/context-core-plugin.d.ts +35 -0
  26. package/dist/cordis/context-core-plugin.js +296 -0
  27. package/dist/cordis/env-probe-plugin.d.ts +27 -0
  28. package/dist/cordis/env-probe-plugin.js +114 -0
  29. package/dist/cordis/host-context.d.ts +65 -0
  30. package/dist/cordis/host-context.js +199 -0
  31. package/dist/cordis/model-responses-plugin.d.ts +81 -0
  32. package/dist/cordis/model-responses-plugin.js +209 -0
  33. package/dist/cordis/plugin-contributions.d.ts +47 -0
  34. package/dist/cordis/plugin-contributions.js +119 -0
  35. package/dist/cordis/plugin-lifecycle.d.ts +57 -0
  36. package/dist/cordis/plugin-lifecycle.js +222 -0
  37. package/dist/cordis/plugin-loader.d.ts +18 -0
  38. package/dist/cordis/plugin-loader.js +310 -0
  39. package/dist/cordis/plugin-sdk-adapter.d.ts +17 -0
  40. package/dist/cordis/plugin-sdk-adapter.js +283 -0
  41. package/dist/cordis/required-host-plugin.d.ts +6 -0
  42. package/dist/cordis/required-host-plugin.js +1 -0
  43. package/dist/cordis/runtime-hooks-plugin.d.ts +23 -0
  44. package/dist/cordis/runtime-hooks-plugin.js +103 -0
  45. package/dist/cordis/sandbox-plugin.d.ts +18 -0
  46. package/dist/cordis/sandbox-plugin.js +21 -0
  47. package/dist/cordis/scoped-layers.d.ts +63 -0
  48. package/dist/cordis/scoped-layers.js +170 -0
  49. package/dist/cordis/tools/builtin-agent-tools-plugin.d.ts +11 -0
  50. package/dist/cordis/tools/builtin-agent-tools-plugin.js +171 -0
  51. package/dist/cordis/tools/index.d.ts +17 -0
  52. package/dist/cordis/tools/index.js +40 -0
  53. package/dist/cordis/tools/mcp-tools-plugin.d.ts +7 -0
  54. package/dist/cordis/tools/mcp-tools-plugin.js +116 -0
  55. package/dist/cordis/tools/process-manager-plugin.d.ts +11 -0
  56. package/dist/cordis/tools/process-manager-plugin.js +39 -0
  57. package/dist/cordis/tools/sandbox-target-tools-plugin.d.ts +13 -0
  58. package/dist/cordis/tools/sandbox-target-tools-plugin.js +255 -0
  59. package/dist/cordis/tools/tool-guidance.d.ts +4 -0
  60. package/dist/cordis/tools/tool-guidance.js +30 -0
  61. package/dist/cordis/tools/tools-core-plugin.d.ts +128 -0
  62. package/dist/cordis/tools/tools-core-plugin.js +486 -0
  63. package/dist/cordis/tools/web-fetch.d.ts +17 -0
  64. package/dist/cordis/tools/web-fetch.js +529 -0
  65. package/dist/cordis/tools/web-search-tools-plugin.d.ts +10 -0
  66. package/dist/cordis/tools/web-search-tools-plugin.js +191 -0
  67. package/dist/cordis/tools/workspace-tools-plugin.d.ts +8 -0
  68. package/dist/cordis/tools/workspace-tools-plugin.js +145 -0
  69. package/dist/env-probe.d.ts +8 -0
  70. package/dist/env-probe.js +172 -0
  71. package/dist/host-configuration.d.ts +27 -0
  72. package/dist/host-configuration.js +185 -0
  73. package/dist/host-tools.d.ts +19 -0
  74. package/dist/host-tools.js +30 -0
  75. package/dist/index.d.ts +2 -0
  76. package/dist/index.js +140 -0
  77. package/dist/ipc.d.ts +24 -0
  78. package/dist/ipc.js +162 -0
  79. package/dist/logger.d.ts +11 -0
  80. package/dist/logger.js +84 -0
  81. package/dist/mcp-configuration.d.ts +17 -0
  82. package/dist/mcp-configuration.js +50 -0
  83. package/dist/modes.d.ts +27 -0
  84. package/dist/modes.js +472 -0
  85. package/dist/ownership.d.ts +7 -0
  86. package/dist/ownership.js +100 -0
  87. package/dist/plugin-configuration.d.ts +9 -0
  88. package/dist/plugin-configuration.js +21 -0
  89. package/dist/plugin-tool-grants.d.ts +18 -0
  90. package/dist/plugin-tool-grants.js +21 -0
  91. package/dist/process-workspaces.d.ts +4 -0
  92. package/dist/process-workspaces.js +28 -0
  93. package/dist/progress.d.ts +30 -0
  94. package/dist/progress.js +699 -0
  95. package/dist/provider-catalog.d.ts +754 -0
  96. package/dist/provider-catalog.js +471 -0
  97. package/dist/run-budget.d.ts +24 -0
  98. package/dist/run-budget.js +47 -0
  99. package/dist/run-checkpoint.d.ts +2 -0
  100. package/dist/run-checkpoint.js +52 -0
  101. package/dist/sandbox-manager.d.ts +111 -0
  102. package/dist/sandbox-manager.js +1519 -0
  103. package/dist/session-paths.d.ts +15 -0
  104. package/dist/session-paths.js +40 -0
  105. package/dist/side-question.d.ts +14 -0
  106. package/dist/side-question.js +89 -0
  107. package/dist/skills-runtime.d.ts +13 -0
  108. package/dist/skills-runtime.js +243 -0
  109. package/dist/token-estimator.d.ts +3 -0
  110. package/dist/token-estimator.js +73 -0
  111. package/dist/tool-result.d.ts +2 -0
  112. package/dist/tool-result.js +91 -0
  113. package/dist/user-config.d.ts +23 -0
  114. package/dist/user-config.js +36 -0
  115. package/package.json +28 -3
  116. package/README.md +0 -3
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "saturn-plugin-ctf",
3
+ "version": "0.1.0",
4
+ "license": "AGPL-3.0-only",
5
+ "type": "module",
6
+ "main": "./index.mjs",
7
+ "engines": {
8
+ "node": ">=26.0.0"
9
+ }
10
+ }
@@ -0,0 +1,321 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { lstat, mkdir, open, readdir, realpath, rename, rm, writeFile } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+
5
+ export const STATUSES = ['queued', 'running', 'solved', 'blocked'];
6
+ const transitions = {
7
+ queued: ['running', 'blocked', 'solved'],
8
+ running: ['queued', 'blocked', 'solved'],
9
+ blocked: ['queued', 'running', 'solved'],
10
+ solved: ['queued'],
11
+ };
12
+ const descriptionNames = ['README.md', 'description.md', 'challenge.md', 'description.txt', 'README.txt'];
13
+
14
+ export function fail(code, message) {
15
+ return Object.assign(new Error(message), { code });
16
+ }
17
+
18
+ export function safeSegment(segment) {
19
+ if (
20
+ typeof segment !== 'string' ||
21
+ !segment ||
22
+ segment.length > 180 ||
23
+ /[<>:"/\\|?*\u0000-\u001f]/u.test(segment) ||
24
+ /^[.]|[ .]$/u.test(segment) ||
25
+ /^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu.test(segment)
26
+ ) {
27
+ throw fail('INVALID_PATH', 'Expected a portable file or directory name.');
28
+ }
29
+ return segment;
30
+ }
31
+
32
+ async function inspect(filename) {
33
+ return lstat(filename, { throwIfNoEntry: false }).catch((error) => {
34
+ if (error.code !== 'ENOENT') throw error;
35
+ });
36
+ }
37
+
38
+ export async function checkedDirectory(root, segments, create = false) {
39
+ let child = root;
40
+ for (const segment of segments) {
41
+ if (segment !== '.saturn') safeSegment(segment);
42
+ child = path.join(child, segment);
43
+ if (create)
44
+ await mkdir(child).catch((error) => {
45
+ if (error.code !== 'EEXIST') throw error;
46
+ });
47
+ if (!(await inspect(child))?.isDirectory()) throw fail('INVALID_PATH', 'Expected a real directory.');
48
+ }
49
+ return child;
50
+ }
51
+
52
+ export async function checkFile(filename) {
53
+ const entry = await inspect(filename);
54
+ if (entry && (!entry.isFile() || entry.nlink !== 1))
55
+ throw fail('INVALID_PATH', 'Expected a regular file with a single link.');
56
+ return entry;
57
+ }
58
+
59
+ export async function atomicWrite(filename, data) {
60
+ await checkFile(filename);
61
+ const temporary = path.join(path.dirname(filename), `.${randomUUID()}.tmp`);
62
+ try {
63
+ await writeFile(temporary, data, { flag: 'wx', mode: 0o600 });
64
+ await rename(temporary, filename);
65
+ } finally {
66
+ await rm(temporary, { force: true });
67
+ }
68
+ }
69
+
70
+ async function readDescription(filename) {
71
+ await checkFile(filename);
72
+ const file = await open(filename, 'r');
73
+ try {
74
+ const buffer = Buffer.alloc(32_768);
75
+ const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
76
+ return buffer.subarray(0, bytesRead).toString('utf8');
77
+ } finally {
78
+ await file.close();
79
+ }
80
+ }
81
+
82
+ async function ensureContainedDir(root, candidate) {
83
+ const realRoot = await realpath(root);
84
+ const resolved = path.resolve(candidate);
85
+ const rel = path.relative(realRoot, resolved);
86
+ if (rel && (rel.startsWith('..') || path.isAbsolute(rel))) {
87
+ throw fail('INVALID_PATH', 'Directory escapes workspace root.');
88
+ }
89
+ const parts = rel ? rel.split(path.sep).filter(Boolean) : [];
90
+ let current = realRoot;
91
+ for (const segment of parts) {
92
+ current = path.join(current, segment);
93
+ await mkdir(current).catch((error) => {
94
+ if (error.code !== 'EEXIST') throw error;
95
+ });
96
+ if (!(await inspect(current))?.isDirectory()) throw fail('INVALID_PATH', 'Expected a real directory.');
97
+ }
98
+ return current;
99
+ }
100
+
101
+ export async function scanWorkspace(root, extraUserRoot) {
102
+ const directories = await readdir(root, { withFileTypes: true }).catch((error) => {
103
+ if (error?.code === 'ENOENT') return [];
104
+ throw error;
105
+ });
106
+ const items = [];
107
+ const seenIds = new Set();
108
+ const scanDir = async (baseDir, skipNames) => {
109
+ const entries = await readdir(baseDir, { withFileTypes: true }).catch((error) => {
110
+ if (error?.code === 'ENOENT') return [];
111
+ throw error;
112
+ });
113
+ for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
114
+ if (!entry.isDirectory() || entry.name.startsWith('.') || entry.name === 'node_modules') continue;
115
+ if (skipNames?.has(entry.name)) continue;
116
+ if (seenIds.has(entry.name)) continue;
117
+ try {
118
+ safeSegment(entry.name);
119
+ } catch {
120
+ continue; // Skip directories that cannot be represented by a portable challenge ID.
121
+ }
122
+ const directory = await checkedDirectory(baseDir, [entry.name]);
123
+ const files = (await readdir(directory, { withFileTypes: true }))
124
+ .filter((file) => file.isFile() && !file.name.startsWith('.'))
125
+ .map((file) => file.name)
126
+ .sort();
127
+ const descriptionFile = descriptionNames.find((name) => files.includes(name));
128
+ seenIds.add(entry.name);
129
+ items.push({
130
+ id: entry.name,
131
+ title: entry.name,
132
+ workSubpath: entry.name,
133
+ ...(descriptionFile
134
+ ? {
135
+ descriptionFile,
136
+ description: await readDescription(path.join(directory, descriptionFile)),
137
+ }
138
+ : {}),
139
+ attachments: files.filter((file) => file !== descriptionFile),
140
+ });
141
+ }
142
+ };
143
+ await scanDir(root, directories.length ? undefined : undefined);
144
+ if (extraUserRoot && path.resolve(extraUserRoot) !== path.resolve(root)) {
145
+ await scanDir(extraUserRoot, new Set(['sessions']));
146
+ }
147
+ return items;
148
+ }
149
+
150
+ export async function createBoard(workspaceRoot, settings, redact, options = {}) {
151
+ const root = await realpath(workspaceRoot);
152
+ const customStateDir = options.stateDir ? path.resolve(options.stateDir) : undefined;
153
+ if (customStateDir) {
154
+ const rel = path.relative(root, customStateDir);
155
+ if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) {
156
+ throw fail('INVALID_PATH', 'Board stateDir must stay inside workspace root.');
157
+ }
158
+ }
159
+ const customAssetsRoot = options.assetsRoot ? path.resolve(options.assetsRoot) : undefined;
160
+ if (customAssetsRoot) {
161
+ const rel = path.relative(root, customAssetsRoot);
162
+ if (rel && (rel.startsWith('..') || path.isAbsolute(rel))) {
163
+ throw fail('INVALID_PATH', 'Board assetsRoot must stay inside workspace root.');
164
+ }
165
+ }
166
+ // Create state on first use so starting a Host does not write plugin files.
167
+ const parts = ['.saturn', 'plugins', 'ctf'];
168
+ const stateDirectoryPath = customStateDir ?? path.join(root, ...parts);
169
+ const assetsRoot = customAssetsRoot ?? root;
170
+ const filename = path.join(stateDirectoryPath, 'board.json');
171
+ const ledgersDirectoryPath = path.join(stateDirectoryPath, 'ledgers');
172
+ let tail = Promise.resolve();
173
+
174
+ async function ensureStateDir(create = false) {
175
+ if (!customStateDir) {
176
+ for (let length = 1; length <= parts.length; length++) {
177
+ const current = path.join(root, ...parts.slice(0, length));
178
+ if (create) {
179
+ await mkdir(current).catch((error) => {
180
+ if (error.code !== 'EEXIST') throw error;
181
+ });
182
+ }
183
+ const entry = await inspect(current);
184
+ if (!entry) return undefined;
185
+ if (!entry.isDirectory()) throw fail('INVALID_PATH', 'Expected a real board directory.');
186
+ }
187
+ return stateDirectoryPath;
188
+ }
189
+ if (create) return ensureContainedDir(root, stateDirectoryPath);
190
+ const rel = path.relative(root, stateDirectoryPath);
191
+ const segments = rel.split(path.sep).filter(Boolean);
192
+ let current = root;
193
+ for (const seg of segments) {
194
+ current = path.join(current, seg);
195
+ const entry = await inspect(current);
196
+ if (!entry) return undefined;
197
+ if (!entry.isDirectory()) throw fail('INVALID_PATH', 'Expected a real board directory.');
198
+ }
199
+ return stateDirectoryPath;
200
+ }
201
+
202
+ async function snapshot() {
203
+ try {
204
+ if (!(await ensureStateDir(false))) return empty();
205
+ if (!(await checkFile(filename))) return empty();
206
+ const file = await open(filename, 'r');
207
+ try {
208
+ const value = JSON.parse(await file.readFile('utf8'));
209
+ if (value.version !== 1 || !Array.isArray(value.challenges) || !value.metadata) {
210
+ throw fail('INVALID_BOARD', 'Unsupported format in board.json.');
211
+ }
212
+ return { ...value, settings };
213
+ } finally {
214
+ await file.close();
215
+ }
216
+ } catch (error) {
217
+ if (error instanceof SyntaxError) throw fail('INVALID_BOARD', 'Invalid JSON in board.json.');
218
+ throw error;
219
+ }
220
+ }
221
+
222
+ function empty() {
223
+ return {
224
+ version: 1,
225
+ revision: 0,
226
+ metadata: { title: path.basename(root) || 'Saturn' },
227
+ settings,
228
+ challenges: [],
229
+ };
230
+ }
231
+
232
+ function change(mutate) {
233
+ const pending = tail.then(async () => {
234
+ const board = await snapshot();
235
+ await mutate(board);
236
+ const next = redact({ ...board, revision: board.revision + 1 });
237
+ await ensureStateDir(true);
238
+ await atomicWrite(filename, `${JSON.stringify(next, null, 2)}\n`);
239
+ return next;
240
+ });
241
+ tail = pending.catch(() => {});
242
+ return pending;
243
+ }
244
+
245
+ function merge(items) {
246
+ return change((board) => {
247
+ const byId = new Map(board.challenges.map((item) => [item.id, item]));
248
+ for (const item of items) {
249
+ const existing = byId.get(item.id);
250
+ if (existing?.platform && !item.platform) {
251
+ continue;
252
+ }
253
+ const merged = {
254
+ ...existing,
255
+ ...item,
256
+ title: item.platform ? item.title : (existing?.title ?? item.title),
257
+ status: item.status === 'solved' ? 'solved' : (existing?.status ?? 'queued'),
258
+ };
259
+ if (!item.descriptionFile) {
260
+ delete merged.descriptionFile;
261
+ delete merged.description;
262
+ }
263
+ byId.set(item.id, merged);
264
+ }
265
+ board.challenges = [...byId.values()];
266
+ });
267
+ }
268
+
269
+ return {
270
+ root,
271
+ assetsRoot,
272
+ filename,
273
+ change,
274
+ merge,
275
+ directory: () => ensureStateDir(true),
276
+ ensureAssetsRoot: () => ensureContainedDir(root, assetsRoot),
277
+ ledgerDirectory: () => ensureContainedDir(root, ledgersDirectoryPath),
278
+ snapshot: async () => redact(await snapshot()),
279
+ update(challengeId, patch) {
280
+ return change((board) => {
281
+ const challenge = board.challenges.find((item) => item.id === challengeId);
282
+ if (!challenge) throw fail('CHALLENGE_NOT_FOUND', 'Unknown challenge.');
283
+ if (patch.status && patch.status !== challenge.status) {
284
+ if (!transitions[challenge.status]?.includes(patch.status))
285
+ throw fail('INVALID_TRANSITION', 'Invalid challenge status transition.');
286
+ if (patch.status === 'running') {
287
+ if (
288
+ board.challenges.filter((item) => item.status === 'running').length >=
289
+ settings.maxActiveChallenges
290
+ )
291
+ throw fail('BOARD_SLOTS_FULL', 'All active challenge slots are in use.');
292
+ challenge.startedAt = new Date().toISOString();
293
+ if (patch.elapsedMs === undefined) challenge.elapsedMs = 0;
294
+ } else if (challenge.status === 'running' && challenge.startedAt && patch.elapsedMs === undefined) {
295
+ challenge.elapsedMs = Date.now() - Date.parse(challenge.startedAt);
296
+ }
297
+ }
298
+ if (patch.helpers && patch.helpers.length > settings.maxHelpersPerChallenge)
299
+ throw fail('TOO_MANY_HELPERS', 'Helper count exceeds the configured limit.');
300
+ if (patch.status === 'running' || (typeof patch.worker === 'string' && patch.worker.trim())) {
301
+ const history = Array.isArray(challenge.workerHistory) ? [...challenge.workerHistory] : [];
302
+ const nextWorker =
303
+ typeof patch.worker === 'string' && patch.worker.trim() ? patch.worker.trim() : undefined;
304
+ let count = typeof challenge.dispatchCount === 'number' ? challenge.dispatchCount : history.length;
305
+ if (nextWorker) {
306
+ if (!history.includes(nextWorker)) {
307
+ history.push(nextWorker);
308
+ count = Math.max(count + 1, history.length);
309
+ }
310
+ } else if (patch.status === 'running' && challenge.status !== 'running') {
311
+ count += 1;
312
+ }
313
+ if (history.length > 0) challenge.workerHistory = history;
314
+ if (count > 0) challenge.dispatchCount = count;
315
+ }
316
+ Object.assign(challenge, patch);
317
+ });
318
+ },
319
+ dispose: () => tail,
320
+ };
321
+ }