@parcha/agentrun-serve 0.1.0-beta.48 → 0.1.0-beta.50
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/CHANGELOG.md +37 -0
- package/README.md +236 -26
- package/dist/agent-delete.d.ts +36 -12
- package/dist/agent-delete.js +213 -30
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +18 -5
- package/dist/agents.d.ts +8 -1
- package/dist/agents.js +14 -4
- package/dist/answer-json.d.ts +2 -2
- package/dist/answer-json.js +3 -4
- package/dist/api-only.d.ts +7 -10
- package/dist/api-only.js +9 -11
- package/dist/archil-routes.d.ts +1 -1
- package/dist/archil-routes.js +4 -3
- package/dist/browser-sessions.d.ts +56 -0
- package/dist/browser-sessions.js +117 -0
- package/dist/compute-cost.d.ts +101 -0
- package/dist/compute-cost.js +250 -0
- package/dist/computer-routes.d.ts +4 -15
- package/dist/computer-routes.js +6 -10
- package/dist/console/assets/index-D0mr9wfZ.css +1 -0
- package/dist/console/assets/index-D7G6o-1E.js +720 -0
- package/dist/console/index.html +2 -2
- package/dist/errors.d.ts +4 -1
- package/dist/errors.js +15 -5
- package/dist/eval-previous.d.ts +52 -0
- package/dist/eval-previous.js +164 -0
- package/dist/file-types.d.ts +2 -0
- package/dist/file-types.js +3 -1
- package/dist/harness.js +2 -1
- package/dist/import.js +4 -3
- package/dist/learn/check.d.ts +16 -4
- package/dist/learn/check.js +30 -26
- package/dist/learn/promote.d.ts +7 -1
- package/dist/learn/promote.js +47 -0
- package/dist/learn/repick.d.ts +43 -0
- package/dist/learn/repick.js +202 -0
- package/dist/learn/workflow-draft.d.ts +4 -0
- package/dist/learn/workflow-draft.js +10 -7
- package/dist/learned-workflow.d.ts +2 -0
- package/dist/learned-workflow.js +19 -2
- package/dist/learning-setting.d.ts +13 -0
- package/dist/learning-setting.js +23 -1
- package/dist/llms.txt +1 -1
- package/dist/openapi.js +180 -14
- package/dist/paid-calls.d.ts +17 -7
- package/dist/paid-calls.js +15 -9
- package/dist/run-launch.d.ts +2 -2
- package/dist/run-launch.js +2 -1
- package/dist/run-learning-served.d.ts +1 -1
- package/dist/run-timeline-served.js +5 -3
- package/dist/run-usage.d.ts +125 -0
- package/dist/run-usage.js +394 -0
- package/dist/runner/adapter-loop.d.ts +4 -0
- package/dist/runner/adapter-loop.js +66 -21
- package/dist/runner/adapter-run-spec.d.ts +8 -3
- package/dist/runner/adapter-run-spec.js +13 -6
- package/dist/runner/big-writes.d.ts +33 -0
- package/dist/runner/big-writes.js +105 -0
- package/dist/runner/cli-run-spec.js +14 -6
- package/dist/runner/conformance/harness.d.ts +2 -0
- package/dist/runner/conformance/harness.js +18 -4
- package/dist/runner/feedback-round.d.ts +29 -1
- package/dist/runner/feedback-round.js +54 -3
- package/dist/runner/held.d.ts +4 -1
- package/dist/runner/journal-kinds.d.ts +3 -1
- package/dist/runner/learning-end.js +54 -18
- package/dist/runner/loop-contract.d.ts +3 -0
- package/dist/runner/metrics.d.ts +4 -0
- package/dist/runner/metrics.js +9 -0
- package/dist/runner/monid-run.js +2 -2
- package/dist/runner/promotion.js +8 -2
- package/dist/runner/reflect-hook.js +3 -2
- package/dist/runner/run-child-host.d.ts +15 -2
- package/dist/runner/run-child-host.js +24 -8
- package/dist/runner/run-child.js +3 -0
- package/dist/runner/save-slots.d.ts +9 -0
- package/dist/runner/save-slots.js +91 -0
- package/dist/runs-over-store.d.ts +3 -2
- package/dist/runs-over-store.js +9 -7
- package/dist/sdk/runs.d.ts +3 -4
- package/dist/sdk/runs.js +10 -7
- package/dist/sdk/runtimes.d.ts +19 -0
- package/dist/sdk/runtimes.js +23 -0
- package/dist/sdk/schema.d.ts +14 -8
- package/dist/sdk/schema.js +18 -6
- package/dist/server.d.ts +7 -3
- package/dist/server.js +223 -100
- package/dist/store/agent-body.d.ts +2 -3
- package/dist/store/agent-body.js +3 -2
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/journal/storage.js +1 -1
- package/dist/store/open.d.ts +1 -1
- package/dist/store/open.js +1 -1
- package/dist/store/postgres/migrations/0021_purged.sql +13 -0
- package/dist/store/postgres/migrations/0022_computer_intervals.sql +27 -0
- package/dist/store/postgres/migrations/0023_computer_interval_resources.sql +14 -0
- package/dist/store/schema.d.ts +20 -1
- package/dist/store/schema.js +60 -5
- package/dist/store/sqlite/connection.d.ts +4 -1
- package/dist/store/sqlite/connection.js +5 -0
- package/dist/store/sqlite/run-patch.d.ts +8 -3
- package/dist/store/sqlite/run-patch.js +15 -7
- package/dist/store/sqlite/store.d.ts +2 -1
- package/dist/store/sqlite/store.js +279 -68
- package/dist/store/sqlite/worker.d.ts +9 -0
- package/dist/store/sqlite/worker.js +18 -1
- package/dist/store/store.d.ts +149 -9
- package/dist/store/store.js +63 -0
- package/dist/store-reads/analytics.js +2 -1
- package/dist/store-reads/cap.d.ts +17 -7
- package/dist/store-reads/cap.js +54 -4
- package/dist/store-reads/decisions.d.ts +21 -0
- package/dist/store-reads/decisions.js +22 -0
- package/dist/store-reads/journal.d.ts +8 -2
- package/dist/store-reads/journal.js +27 -8
- package/dist/store-reads/learning.d.ts +1 -1
- package/dist/store-reads/learning.js +5 -2
- package/dist/store-reads/runs.js +2 -0
- package/dist/store-reads/stats.js +4 -2
- package/dist/store-view.d.ts +12 -1
- package/dist/store-view.js +98 -38
- package/dist/template-eval.d.ts +15 -1
- package/dist/template-eval.js +92 -13
- package/dist/templates.d.ts +1 -1
- package/dist/templates.js +4 -3
- package/dist/timeline/labels.js +17 -0
- package/dist/tool-list.js +1 -0
- package/dist/tools.d.ts +6 -2
- package/dist/tools.js +22 -1
- package/dist/trigger-targets.d.ts +28 -9
- package/dist/trigger-targets.js +19 -16
- package/dist/triggers.d.ts +2 -1
- package/dist/triggers.js +5 -2
- package/dist/validate.d.ts +4 -2
- package/dist/validate.js +13 -3
- package/dist/viewer-credential.d.ts +3 -4
- package/dist/viewer-credential.js +6 -4
- package/dist/web-fake.d.ts +25 -0
- package/dist/web-fake.js +38 -1
- package/dist/web-save.d.ts +75 -0
- package/dist/web-save.js +235 -0
- package/dist/workspace/archil-claim.d.ts +5 -2
- package/dist/workspace/archil-claim.js +5 -2
- package/dist/workspace/archil-disk.d.ts +10 -1
- package/dist/workspace/archil-disk.js +19 -1
- package/dist/workspace/archil.d.ts +3 -0
- package/dist/workspace/archil.js +101 -16
- package/dist/workspace/computer-for-claim.d.ts +19 -1
- package/dist/workspace/computer-for-claim.js +88 -20
- package/dist/workspace/computer-intervals.d.ts +26 -0
- package/dist/workspace/computer-intervals.js +58 -0
- package/dist/workspace/computer-janitor.d.ts +7 -3
- package/dist/workspace/computer-janitor.js +14 -6
- package/dist/workspace/computer-kept.d.ts +7 -2
- package/dist/workspace/computer-kept.js +9 -2
- package/dist/workspace/pieced.d.ts +14 -0
- package/dist/workspace/pieced.js +23 -0
- package/dist/workspace/resolve-computer.d.ts +7 -4
- package/dist/workspace/resolve-computer.js +21 -32
- package/dist/workspace/run-disk.js +32 -12
- package/dist/workspace/run-end-computers.d.ts +1 -0
- package/dist/workspace/run-end-computers.js +4 -1
- package/dist/workspace/run-workspace.d.ts +1 -1
- package/dist/workspace/run-workspace.js +11 -6
- package/package.json +10 -10
- package/templates/README.md +36 -7
- package/dist/console/assets/index-CTGegcKt.css +0 -1
- package/dist/console/assets/index-D1Se6mgG.js +0 -715
package/dist/workspace/archil.js
CHANGED
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
// through the file API as the runner keeps it, its files and `dir/` markers, and its modes and links in sync.json beside
|
|
51
51
|
// the root (syncMetaOf), and attach() with check.sync reads back a synced folder under the claim's epoch instead.
|
|
52
52
|
import { folderPathProblem } from "@parcha/agentrun-computer";
|
|
53
|
-
import { DENY_EGRESS, pinnedImage } from "./archil-disk.js";
|
|
53
|
+
import { ArchilDiskMissing, DENY_EGRESS, pinnedImage } from "./archil-disk.js";
|
|
54
54
|
import { ArchilEgressRefused, archilExec, COMMAND_MAX_MS, DEFAULT_QUEUE_MS, MAX_EXEC_MS, spillKeys, spillName } from "./archil-exec.js";
|
|
55
55
|
import { attachedWorkspace, checkReadBack, noProcesses } from "./attach.js";
|
|
56
56
|
import { globRegex } from "./glob.js";
|
|
@@ -73,14 +73,28 @@ const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
73
73
|
* unhandled rejection. */
|
|
74
74
|
const quiet = (promise) => { promise.catch(() => undefined); return promise; };
|
|
75
75
|
const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
76
|
-
/** A
|
|
76
|
+
/** A put Archil refused (404) for a folder above it that was not there, a folder that then could not be made (`why`):
|
|
77
|
+
* nothing of the put landed. Kept apart from the folder's own failure so an append can say so. */
|
|
78
|
+
class FolderNotMade extends Error {
|
|
79
|
+
why;
|
|
80
|
+
constructor(why) {
|
|
81
|
+
super(why.message, { cause: why });
|
|
82
|
+
this.why = why;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/** A disk API failure as a WorkspaceError, Archil's words kept. A disk that is not there is UNAVAILABLE, never a file or
|
|
86
|
+
* folder that is not there: no caller takes it for an empty folder. */
|
|
77
87
|
function failed(error, path, what) {
|
|
78
88
|
if (error instanceof WorkspaceError)
|
|
79
89
|
return error;
|
|
90
|
+
if (error instanceof FolderNotMade)
|
|
91
|
+
return error.why;
|
|
80
92
|
const status = statusOf(error);
|
|
81
93
|
const at = { path, cause: error };
|
|
94
|
+
if (error instanceof ArchilDiskMissing)
|
|
95
|
+
return new WorkspaceError("UNAVAILABLE", `${what} ${show(path)}: ${error.message}`, at);
|
|
82
96
|
if (status === 404)
|
|
83
|
-
return new WorkspaceError("NOT_FOUND", `${show(path)} is not there`, at);
|
|
97
|
+
return new WorkspaceError("NOT_FOUND", `${show(path)} is not there (${what}; Archil: ${describe(error)})`, at);
|
|
84
98
|
if (status === 413 || error?.code === "EntityTooLarge")
|
|
85
99
|
return new WorkspaceError("TOO_LARGE", `${what} ${show(path)}: more than the disk takes in one call`, at);
|
|
86
100
|
if (status === 401 || status === 403)
|
|
@@ -172,6 +186,46 @@ class ArchilWorkspace {
|
|
|
172
186
|
await this.#execs;
|
|
173
187
|
return this.#settle(rel, call);
|
|
174
188
|
}
|
|
189
|
+
/** Relative path of disk key `key` under the root ("" for the root, or for a folder above or beside it). */
|
|
190
|
+
#relOf(key) {
|
|
191
|
+
return key.startsWith(`${this.#root}/`) ? key.slice(this.#root.length + 1) : "";
|
|
192
|
+
}
|
|
193
|
+
/** Whether folder `key` (a disk key, no trailing "/") is on the disk. */
|
|
194
|
+
async #folderThere(key) {
|
|
195
|
+
for await (const page of this.#pages(key, false)) {
|
|
196
|
+
if (page.commonPrefixes.includes(`${key}/`) || page.objects.some((o) => o.key === `${key}/`))
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
/** `put`, which puts disk key `key` (a file, or a folder ending in "/"), after this workspace's execs (`inExec`: made
|
|
202
|
+
* from inside the exec queue, so not after it). Archil may make no folder above a put (staging's answered 404 for an
|
|
203
|
+
* expert run's `.agents/skills/` on a fresh run folder), so on a 404 each folder above `key` that is not there is
|
|
204
|
+
* made, from the highest down, and `put` is made once more. A folder already there is left as it is. */
|
|
205
|
+
async #put(key, put, inExec = false) {
|
|
206
|
+
const call = (rel, fn) => (inExec ? this.#settle(rel, fn) : this.#write(rel, fn));
|
|
207
|
+
try {
|
|
208
|
+
return await call(this.#relOf(key), put);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (statusOf(error) !== 404 || error instanceof ArchilDiskMissing)
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
const above = key.replace(/\/$/, "").split("/").slice(0, -1);
|
|
215
|
+
let there = above.length;
|
|
216
|
+
while (there > 0 && !(await this.#folderThere(above.slice(0, there).join("/"))))
|
|
217
|
+
there--;
|
|
218
|
+
for (let depth = there + 1; depth <= above.length; depth++) {
|
|
219
|
+
const folder = above.slice(0, depth).join("/");
|
|
220
|
+
try {
|
|
221
|
+
await call(this.#relOf(folder), () => this.#disk.putObject(`${folder}/`, new Uint8Array(0)));
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
throw new FolderNotMade(failed(error, this.#relOf(folder), "mkdir"));
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return call(this.#relOf(key), put);
|
|
228
|
+
}
|
|
175
229
|
/** Every page of the listing of `prefix`. */
|
|
176
230
|
async *#pages(prefix, recursive) {
|
|
177
231
|
let token;
|
|
@@ -246,9 +300,17 @@ class ArchilWorkspace {
|
|
|
246
300
|
answer = await this.#disk.getRange(this.#key(rel), `bytes=${offset}-${last}`);
|
|
247
301
|
}
|
|
248
302
|
catch (error) {
|
|
249
|
-
|
|
303
|
+
// A ranged read is no SDK call, so a disk that is not there answers it 404 as a missing file would: the look at what
|
|
304
|
+
// is there (an SDK call) is what tells them apart, and its ArchilDiskMissing is the read's failure.
|
|
305
|
+
const kind = statusOf(error) === 404
|
|
306
|
+
? await this.#kind(rel).catch((lookup) => {
|
|
307
|
+
if (lookup instanceof ArchilDiskMissing)
|
|
308
|
+
throw failed(lookup, rel, "read");
|
|
309
|
+
return undefined;
|
|
310
|
+
})
|
|
311
|
+
: undefined;
|
|
312
|
+
if (kind === "dir")
|
|
250
313
|
throw new WorkspaceError("REFUSED", `${show(rel)} is a directory`, { path: rel, cause: error });
|
|
251
|
-
}
|
|
252
314
|
if (statusOf(error) === 501) {
|
|
253
315
|
throw new WorkspaceError("UNAVAILABLE", `${show(rel)} is a symbolic link (an exec made it): Archil's file API does not read one`, { path: rel, cause: error });
|
|
254
316
|
}
|
|
@@ -269,7 +331,7 @@ class ArchilWorkspace {
|
|
|
269
331
|
const bytes = bytesOf(data);
|
|
270
332
|
const rel = ArchilWorkspace.#below(path, "a file").join("/");
|
|
271
333
|
try {
|
|
272
|
-
await this.#
|
|
334
|
+
await this.#put(this.#key(rel), () => this.#disk.putObject(this.#key(rel), bytes));
|
|
273
335
|
}
|
|
274
336
|
catch (error) {
|
|
275
337
|
throw await this.#writeFailed(error, rel, "file", "write");
|
|
@@ -290,26 +352,31 @@ class ArchilWorkspace {
|
|
|
290
352
|
// land in order.
|
|
291
353
|
for (let at = 0; at === 0 || at < bytes.length; at += APPEND_MAX) {
|
|
292
354
|
const part = bytes.subarray(at, at + APPEND_MAX);
|
|
293
|
-
await this.#
|
|
355
|
+
await this.#put(this.#key(rel), () => this.#disk.appendObject(this.#key(rel), part));
|
|
294
356
|
landed += part.length;
|
|
295
357
|
}
|
|
296
358
|
}
|
|
297
359
|
catch (error) {
|
|
298
360
|
const why = await this.#writeFailed(error, rel, "file", "append");
|
|
299
361
|
const status = statusOf(error);
|
|
300
|
-
|
|
362
|
+
// A disk that is not there took nothing either.
|
|
363
|
+
// So did a put refused for a folder that could not be made.
|
|
364
|
+
const refused = error instanceof ArchilDiskMissing || error instanceof FolderNotMade || (status !== undefined && status >= 400 && status < 500);
|
|
301
365
|
if (chunks === 1) {
|
|
302
366
|
// A refusal (4xx) applied nothing; any other failure may have come after the bytes were committed.
|
|
303
367
|
if (refused)
|
|
304
368
|
throw why;
|
|
305
369
|
throw new WorkspaceError(why.code, `the append to ${show(rel)} may also have landed: unknown, re-read the file before retrying: ${why.message}`, { path: rel, cause: error });
|
|
306
370
|
}
|
|
307
|
-
// Part of it may be on the disk. Only what is known is said: the size read back, or else what was acknowledged,
|
|
308
|
-
//
|
|
371
|
+
// Part of it may be on the disk. Only what is known is said: the size read back, or else what was acknowledged, with
|
|
372
|
+
// the failed chunk's outcome unknown unless it was refused (as one chunk's refusal applied nothing). A chunk may
|
|
373
|
+
// commit and lose its answer, so a count is never a guess.
|
|
309
374
|
const after = before === undefined ? undefined : await this.#sizeOf(rel).catch(() => undefined);
|
|
310
375
|
const known = before !== undefined && after !== undefined && after - before >= landed && after - before <= bytes.length
|
|
311
376
|
? `${after - before} of ${bytes.length} bytes were appended (read back from the file: ${after} bytes now, ${before} before)`
|
|
312
|
-
:
|
|
377
|
+
: refused
|
|
378
|
+
? `${landed} of ${bytes.length} bytes were appended (the chunk that failed was refused, so none of it landed)`
|
|
379
|
+
: `at least ${landed} of ${bytes.length} bytes were appended; the failed chunk may also have landed: unknown, re-read the file before retrying`;
|
|
313
380
|
throw new WorkspaceError(why.code, `${known} to ${show(rel)} before it failed: ${why.message}`, { path: rel, cause: error });
|
|
314
381
|
}
|
|
315
382
|
}
|
|
@@ -385,13 +452,29 @@ class ArchilWorkspace {
|
|
|
385
452
|
async mkdir(dir) {
|
|
386
453
|
const rel = relativeSegments(dir).join("/");
|
|
387
454
|
try {
|
|
388
|
-
// A directory is a key ending in "/";
|
|
389
|
-
|
|
455
|
+
// A directory is a key ending in "/"; a parent that is not there is made first (#put), and one already there is
|
|
456
|
+
// left as it is.
|
|
457
|
+
await this.#put(`${this.#key(rel)}/`, () => this.#disk.putObject(`${this.#key(rel)}/`, new Uint8Array(0)));
|
|
390
458
|
}
|
|
391
459
|
catch (error) {
|
|
392
460
|
throw await this.#writeFailed(error, rel, "dir", "mkdir");
|
|
393
461
|
}
|
|
394
462
|
}
|
|
463
|
+
/** The root made on the disk when it is not there yet; one already there is not put again, so a computer whose mount
|
|
464
|
+
* holds it (Archil's write delegation refuses a put there) never stands in the way. A failure throws, in Archil's words. */
|
|
465
|
+
async ensureRoot() {
|
|
466
|
+
const marker = `${this.#root}/`;
|
|
467
|
+
try {
|
|
468
|
+
for await (const page of this.#pages(this.#root, false)) {
|
|
469
|
+
if (page.commonPrefixes.includes(marker) || page.objects.some((o) => o.key === marker))
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
await this.#put(marker, () => this.#disk.putObject(marker, new Uint8Array(0)));
|
|
473
|
+
}
|
|
474
|
+
catch (error) {
|
|
475
|
+
throw failed(error, "", "mkdir");
|
|
476
|
+
}
|
|
477
|
+
}
|
|
395
478
|
async grep(pattern, options = {}) {
|
|
396
479
|
try {
|
|
397
480
|
new RegExp(pattern);
|
|
@@ -662,7 +745,7 @@ class ArchilWorkspace {
|
|
|
662
745
|
* exec queue, so it does not wait for it. */
|
|
663
746
|
async #makeRoot() {
|
|
664
747
|
for (const folder of [this.#root, this.#spill]) {
|
|
665
|
-
await this.#
|
|
748
|
+
await this.#put(`${folder}/`, () => this.#disk.putObject(`${folder}/`, new Uint8Array(0)), true).catch((error) => { throw failed(error, "", "mkdir"); });
|
|
666
749
|
}
|
|
667
750
|
this.#rooted = true;
|
|
668
751
|
}
|
|
@@ -753,7 +836,9 @@ class ArchilWorkspace {
|
|
|
753
836
|
const wrote = async (path, what, call) => {
|
|
754
837
|
try {
|
|
755
838
|
const rel = relativeSegments(path).join("/");
|
|
756
|
-
|
|
839
|
+
const key = this.#key(rel);
|
|
840
|
+
// A put or a folder makes the folders above it that are not there; a remove makes none.
|
|
841
|
+
await (what === "remove" ? this.#write(rel, () => call(key)) : this.#put(what === "mkdir" ? `${key}/` : key, () => call(key)));
|
|
757
842
|
return { ok: true, value: undefined };
|
|
758
843
|
}
|
|
759
844
|
catch (error) {
|
|
@@ -767,7 +852,7 @@ class ArchilWorkspace {
|
|
|
767
852
|
remove: (path, type) => wrote(path, "remove", (key) => this.#disk.deleteObject(type === "dir" ? `${key}/` : key)),
|
|
768
853
|
meta: async (folder) => {
|
|
769
854
|
try {
|
|
770
|
-
await this.#
|
|
855
|
+
await this.#put(meta, () => this.#disk.putObject(meta, Buffer.from(JSON.stringify(folder))));
|
|
771
856
|
return { ok: true, value: undefined };
|
|
772
857
|
}
|
|
773
858
|
catch (error) {
|
|
@@ -6,6 +6,7 @@ import type { Store } from "../store/index.js";
|
|
|
6
6
|
import type { MountCheck } from "./attach.js";
|
|
7
7
|
import { type AttachedComputers } from "./attached-computers.js";
|
|
8
8
|
import { type HeldComputer } from "./computer-hold.js";
|
|
9
|
+
import { type ComputeLedger, type ComputeMeterOptions } from "../compute-cost.js";
|
|
9
10
|
import { type Kept, type ParkCause, type ParkedEnd } from "./computer-kept.js";
|
|
10
11
|
import { type ProviderLedger } from "./computer-ledger.js";
|
|
11
12
|
import { type ClaimBrowser, type ClaimSide, type ComputerSetting, type ComputerSettingKind, type KindRegistry, type ProviderEntry } from "./resolve-computer.js";
|
|
@@ -168,8 +169,20 @@ export interface ComputerForClaimDeps {
|
|
|
168
169
|
* claim's Archil mount credential (Mode 2): the credential is this claim's to remove now (archil-claim.ts inherit),
|
|
169
170
|
* since this claim mints none. Absent: nothing was minted (Mode 1). */
|
|
170
171
|
readonly inheritMount?: () => void;
|
|
171
|
-
/** The clock a kept computer's time is read by (computer-kept.ts)
|
|
172
|
+
/** The clock a kept computer's time is read by (computer-kept.ts), and its computers' intervals are written on
|
|
173
|
+
* (computer-intervals.ts). Default Date.now. */
|
|
172
174
|
readonly now?: () => number;
|
|
175
|
+
/** Where a computer interval that could not be written is said (computer-intervals.ts). Default the runner's stderr. */
|
|
176
|
+
readonly log?: (line: string) => void;
|
|
177
|
+
/** The run's one ledger and the server's environment, for its computers' time (compute-cost.ts): each computer the
|
|
178
|
+
* claim makes or takes back is metered there, priced by the operator's per-resource settings, its next stretch held
|
|
179
|
+
* under the run's cap. Absent (a runner of its own with no ledger here), nothing is held or booked: its time is on the
|
|
180
|
+
* usage read alone. */
|
|
181
|
+
readonly compute?: {
|
|
182
|
+
readonly ledger: ComputeLedger;
|
|
183
|
+
readonly env: NodeJS.ProcessEnv;
|
|
184
|
+
readonly every?: ComputeMeterOptions["every"];
|
|
185
|
+
};
|
|
173
186
|
readonly signal?: AbortSignal;
|
|
174
187
|
}
|
|
175
188
|
/** The run's computer at its claim: what runTools takes, the person's hold on a desktop's or phone's ports, the hold
|
|
@@ -191,6 +204,10 @@ export interface ClaimComputer {
|
|
|
191
204
|
/** The kind of computer a park of this claim keeps (`park`): a desktop, a phone or a sandbox the claim made through its
|
|
192
205
|
* provider, or took back from an earlier claim's park. Absent: a park lets the computer go as the run's end does. */
|
|
193
206
|
readonly keeps?: KeptKind;
|
|
207
|
+
/** Books the time of every computer this claim holds up to now on the run's ledger, its holds let go, its meter
|
|
208
|
+
* stopped (compute-cost.ts): run before the run's end or park is written (the spec's beforeRelease), so the end's
|
|
209
|
+
* cost holds it. Absent where nothing is metered. */
|
|
210
|
+
settle?(): Promise<void>;
|
|
194
211
|
release(why?: ViewEnd): Promise<void>;
|
|
195
212
|
/** The claim's release at the run's park, for `cause`: a computer it `keeps` is kept for KEEP_MS (computer-kept.ts),
|
|
196
213
|
* its live view closed with the park's cause and the keep's time (ParkedEnd) and its hold ended, but the machine left
|
|
@@ -241,4 +258,5 @@ export declare function claimBrowser(provider: ComputerProvider, spec: ComputerS
|
|
|
241
258
|
export declare function computerForClaim(launch: ClaimLaunch, store: Pick<Store, "agents" | "sealedKeys" | "computerLedger"> & {
|
|
242
259
|
readonly settings?: Store["settings"];
|
|
243
260
|
readonly runs?: Pick<Store["runs"], "get">;
|
|
261
|
+
readonly computerIntervals?: Store["computerIntervals"];
|
|
244
262
|
}, deps: ComputerForClaimDeps): Promise<ClaimComputer>;
|
|
@@ -48,6 +48,8 @@ import { getSealedKey } from "../server-keys.js";
|
|
|
48
48
|
import { agentComputerOf, forgetAgentComputer, holdAgentComputer, keepAgentComputer, liveHolderOf, reconcileAgentComputer, serverIdOf } from "./agent-computer.js";
|
|
49
49
|
import { pairedProvider, withPaired } from "./attached-computers.js";
|
|
50
50
|
import { computerHold, heldHands } from "./computer-hold.js";
|
|
51
|
+
import { endingDeletes, figuresOf, intervalWrites } from "./computer-intervals.js";
|
|
52
|
+
import { computeMeter } from "../compute-cost.js";
|
|
51
53
|
import { deleteKept, endKept, KEEP_MS, readKept, takeKept, writeKept } from "./computer-kept.js";
|
|
52
54
|
import { storeLedger } from "./computer-ledger.js";
|
|
53
55
|
import { providerKeys, registryHere, resolveComputer } from "./resolve-computer.js";
|
|
@@ -64,10 +66,13 @@ const claimRequestId = (claim) => `${claim.runId}#${claim.epoch}`;
|
|
|
64
66
|
* lost, a crash). A run's first claim is epoch 1 (a queued run is epoch 0; each claim and release moves it up one): no
|
|
65
67
|
* claim came before it to leave a computer, so the provider is not asked (its list is a vendor round trip before the
|
|
66
68
|
* create). */
|
|
67
|
-
async function releaseOlderClaims(provider, claim, signal) {
|
|
69
|
+
async function releaseOlderClaims(provider, claim, signal, ending) {
|
|
68
70
|
if (claim.epoch <= 1)
|
|
69
71
|
return false;
|
|
70
|
-
|
|
72
|
+
// Each delete that answers ok ends its interval as it answers (computer-intervals.ts endingDeletes), so one deleted
|
|
73
|
+
// before another failed, or before the call was cut, keeps its end; one detached or not let go is left open.
|
|
74
|
+
const releasing = ending ? endingDeletes(provider, ending.entry, ending.writes) : provider;
|
|
75
|
+
const older = await releaseOlder(releasing, claim.runId, claim.epoch, signal ? { signal } : {});
|
|
71
76
|
if (!older.ok)
|
|
72
77
|
throw new Error(older.error.message);
|
|
73
78
|
return older.value.released.length > 0;
|
|
@@ -191,6 +196,9 @@ export function claimBrowser(provider, spec) {
|
|
|
191
196
|
},
|
|
192
197
|
url: (id) => cdpUrlOf(provider, { id, name: id }),
|
|
193
198
|
close: release,
|
|
199
|
+
...(isBrowserProvider(provider) && typeof provider.sessionUsage === "function"
|
|
200
|
+
? { usage: (id, signal) => valueOf(provider.sessionUsage({ id, name: id }, signal ? { signal } : {})) }
|
|
201
|
+
: {}),
|
|
194
202
|
...(kept && features.downloads
|
|
195
203
|
? {
|
|
196
204
|
downloads: {
|
|
@@ -206,8 +214,9 @@ export function claimBrowser(provider, spec) {
|
|
|
206
214
|
},
|
|
207
215
|
};
|
|
208
216
|
}
|
|
209
|
-
/** The run's computer `ref` of `provider`, connected (`connection`), as the claim holds it (ProviderComputer).
|
|
210
|
-
|
|
217
|
+
/** The run's computer `ref` of `provider`, connected (`connection`), as the claim holds it (ProviderComputer). `ended`:
|
|
218
|
+
* told once its delete answered ok (its interval's end, computer-intervals.ts). */
|
|
219
|
+
function providerComputer(provider, ref, connection, ended) {
|
|
211
220
|
const { client, hello } = connection;
|
|
212
221
|
const watch = watchOf(provider, ref);
|
|
213
222
|
const close = async () => {
|
|
@@ -235,25 +244,40 @@ function providerComputer(provider, ref, connection) {
|
|
|
235
244
|
const released = await provider.release(ref, "delete");
|
|
236
245
|
if (!released.ok)
|
|
237
246
|
throw new Error(released.error.message);
|
|
247
|
+
await ended?.();
|
|
238
248
|
},
|
|
239
249
|
};
|
|
240
250
|
}
|
|
241
251
|
/** `entry`'s computer started through its provider: the run's computers of a lower claim epoch released first
|
|
242
252
|
* (releaseOlderClaims; none at a run's first claim; `releasedOlder`, whether it deleted one), then made with the run's
|
|
243
|
-
* labels (and the run's folder, when it mounts), connected (providerComputer); its release deletes it.
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
* labels (and the run's folder, when it mounts), connected (providerComputer); its release deletes it. Its interval
|
|
254
|
+
* (computer-intervals.ts) is opened under `entry` (the provider's registry id) once the create answers, of `kind`, and
|
|
255
|
+
* ended by each delete that answers ok. */
|
|
256
|
+
async function providerLaunch(provider, claim, spec, signal, interval) {
|
|
257
|
+
const { writes, entry } = interval;
|
|
258
|
+
const releasedOlder = await releaseOlderClaims(provider, claim, signal, { entry, writes });
|
|
259
|
+
// Its first stretch held under the run's cap at what the create asks for, before anything is made: refused, nothing is.
|
|
260
|
+
const ahead = await interval.meter?.before(entry, figuresOf(spec));
|
|
246
261
|
// The claim is the request: a retry of this launch under the same epoch gets the computer it made, never a second.
|
|
247
262
|
const created = await provider.create(spec, { requestId: claimRequestId(claim), ...(signal ? { signal } : {}) });
|
|
248
|
-
if (!created.ok)
|
|
263
|
+
if (!created.ok) {
|
|
264
|
+
void ahead?.drop();
|
|
249
265
|
throw new Error(created.error.message);
|
|
266
|
+
}
|
|
250
267
|
const ref = { id: created.value.id, name: created.value.name };
|
|
268
|
+
// What it was made with: its provider's figures where it said them, else what was asked. Metered from here.
|
|
269
|
+
const figures = figuresOf(spec, created.value.resources);
|
|
270
|
+
await writes.opened(entry, ref.id, claim.runId, interval.kind, figures);
|
|
271
|
+
interval.meter?.start(entry, ref.id, figures, ahead);
|
|
272
|
+
const ended = () => writes.ended(entry, ref.id);
|
|
251
273
|
const connected = await provider.connect(ref);
|
|
252
274
|
if (!connected.ok) {
|
|
253
|
-
await provider.release(ref, "delete").catch(() => undefined);
|
|
275
|
+
const released = await provider.release(ref, "delete").catch(() => undefined);
|
|
276
|
+
if (released?.ok)
|
|
277
|
+
await ended();
|
|
254
278
|
throw new Error(connected.error.message);
|
|
255
279
|
}
|
|
256
|
-
return { ...providerComputer(provider, ref, connected.value), releasedOlder };
|
|
280
|
+
return { ...providerComputer(provider, ref, connected.value, ended), releasedOlder };
|
|
257
281
|
}
|
|
258
282
|
/** The words a hosted provider's connection answers every call in once its vendor deleted the machine (the computer
|
|
259
283
|
* package's hosted deletedSaid: "the computer <name> (<id>) was deleted: <vendor> no longer has it"). */
|
|
@@ -415,17 +439,19 @@ function lostAware(first, renewal) {
|
|
|
415
439
|
}
|
|
416
440
|
/** The computer an earlier claim of the run kept at its park (`kept`), attached again through `provider` and connected,
|
|
417
441
|
* or undefined when its provider no longer has it or it does not connect (one that does not is deleted). */
|
|
418
|
-
async function keptLaunch(provider, kept, signal) {
|
|
442
|
+
async function keptLaunch(provider, kept, signal, ended) {
|
|
419
443
|
const ref = { id: kept.id, name: kept.name };
|
|
420
444
|
const found = await provider.attach({ id: ref.id }, signal ? { signal } : {});
|
|
421
445
|
if (!found.ok)
|
|
422
446
|
return undefined;
|
|
423
447
|
const connected = await provider.connect(ref);
|
|
424
448
|
if (!connected.ok) {
|
|
425
|
-
await provider.release(ref, "delete").catch(() => undefined);
|
|
449
|
+
const released = await provider.release(ref, "delete").catch(() => undefined);
|
|
450
|
+
if (released?.ok)
|
|
451
|
+
await ended();
|
|
426
452
|
return undefined;
|
|
427
453
|
}
|
|
428
|
-
return providerComputer(provider, ref, connected.value);
|
|
454
|
+
return providerComputer(provider, ref, connected.value, ended);
|
|
429
455
|
}
|
|
430
456
|
/** A run state in which the run still holds what it claimed. */
|
|
431
457
|
const LIVE_RUN = new Set(["queued", "running", "parked"]);
|
|
@@ -570,6 +596,8 @@ async function ownedStart(provider, claim, spec, context, hold, signal) {
|
|
|
570
596
|
export async function computerForClaim(launch,
|
|
571
597
|
// `settings` and `runs`: the agent's own computer's record and its holder's run row, for a persistent setting; and the
|
|
572
598
|
// run's own row, whose epoch and runner fence every write of the run's folder to its disk.
|
|
599
|
+
// `computerIntervals`: where each computer the claim makes is written, from its create to its proven delete
|
|
600
|
+
// (computer-intervals.ts); absent, none is.
|
|
573
601
|
store, deps) {
|
|
574
602
|
// The run's disk as this claim writes the folder on it: each write refused once a later claim has taken the run (Q1).
|
|
575
603
|
const runs = store.runs;
|
|
@@ -597,6 +625,24 @@ store, deps) {
|
|
|
597
625
|
/** The kind of the computer this claim made the run fresh (ClaimComputer.made). */
|
|
598
626
|
let ownKind;
|
|
599
627
|
const now = deps.now ?? Date.now;
|
|
628
|
+
/** Each computer this claim makes through its provider, from its create to its proven delete. */
|
|
629
|
+
const written = intervalWrites(store, deps.log, now);
|
|
630
|
+
/** Its time on the run's ledger (compute-cost.ts), while this claim holds it; none with no ledger here. */
|
|
631
|
+
const meter = deps.compute ? computeMeter({ env: deps.compute.env, ledger: deps.compute.ledger, booked: written.booked, now, ...(deps.compute.every ? { every: deps.compute.every } : {}), ...(deps.log ? { log: deps.log } : {}) }) : undefined;
|
|
632
|
+
/** The writes, each computer's meter stopped (its time booked up to now) before it is kept or its end written. */
|
|
633
|
+
const writes = meter
|
|
634
|
+
? {
|
|
635
|
+
...written,
|
|
636
|
+
kept: async (provider, providerId) => {
|
|
637
|
+
await meter.stop(provider, providerId);
|
|
638
|
+
await written.kept(provider, providerId);
|
|
639
|
+
},
|
|
640
|
+
ended: async (provider, providerId) => {
|
|
641
|
+
await meter.stop(provider, providerId);
|
|
642
|
+
await written.ended(provider, providerId);
|
|
643
|
+
},
|
|
644
|
+
}
|
|
645
|
+
: written;
|
|
600
646
|
/** The computer an earlier claim of the run kept at its park (computer-kept.ts), read once: attached again when it is
|
|
601
647
|
* the setting's and its time is not over (reattach), else left to the launch's releaseOlderClaims. A read that fails
|
|
602
648
|
* is none. A run's first claim follows no park. */
|
|
@@ -665,16 +711,25 @@ store, deps) {
|
|
|
665
711
|
mount = await deps.workspace.mountSpec();
|
|
666
712
|
}
|
|
667
713
|
const call = deps.signal ? { signal: deps.signal } : {};
|
|
714
|
+
const ahead = await meter?.before(entry.id, figuresOf(spec));
|
|
668
715
|
const created = await provider.create({ ...spec, ...(mount ? { mount } : {}) }, { requestId: `${claimRequestId(launch)}#lost-${times}`, ...call });
|
|
669
|
-
if (!created.ok)
|
|
716
|
+
if (!created.ok) {
|
|
717
|
+
void ahead?.drop();
|
|
670
718
|
throw new Error(created.error.message);
|
|
719
|
+
}
|
|
671
720
|
const ref = { id: created.value.id, name: created.value.name };
|
|
721
|
+
const figures = figuresOf(spec, created.value.resources);
|
|
722
|
+
await writes.opened(entry.id, ref.id, launch.runId, "sandbox", figures);
|
|
723
|
+
meter?.start(entry.id, ref.id, figures, ahead);
|
|
724
|
+
const ended = () => writes.ended(entry.id, ref.id);
|
|
672
725
|
const connected = await provider.connect(ref);
|
|
673
726
|
if (!connected.ok) {
|
|
674
|
-
await provider.release(ref, "delete").catch(() => undefined);
|
|
727
|
+
const released = await provider.release(ref, "delete").catch(() => undefined);
|
|
728
|
+
if (released?.ok)
|
|
729
|
+
await ended();
|
|
675
730
|
throw new Error(connected.error.message);
|
|
676
731
|
}
|
|
677
|
-
const again = providerComputer(provider, ref, connected.value);
|
|
732
|
+
const again = providerComputer(provider, ref, connected.value, ended);
|
|
678
733
|
if (mount) {
|
|
679
734
|
try {
|
|
680
735
|
if (store && stored)
|
|
@@ -721,9 +776,13 @@ store, deps) {
|
|
|
721
776
|
return undefined;
|
|
722
777
|
}
|
|
723
778
|
const provider = await vendorProvider(entry, keys, configuration);
|
|
724
|
-
const taken = await keptLaunch(provider, record, signal);
|
|
779
|
+
const taken = await keptLaunch(provider, record, signal, () => writes.ended(entry.id, record.id));
|
|
725
780
|
if (!taken)
|
|
726
781
|
return undefined;
|
|
782
|
+
// Metered again from here, with what its interval says it was made with: the time it was kept is no claim's.
|
|
783
|
+
const row = meter ? (await store.computerIntervals?.forRun(launch.runId).catch(() => undefined))?.find((one) => one.provider === entry.id && one.providerId === record.id) : undefined;
|
|
784
|
+
if (row)
|
|
785
|
+
meter?.start(entry.id, record.id, row);
|
|
727
786
|
deps.inheritMount?.();
|
|
728
787
|
// A sandbox taken back is started again when it is lost as one this claim made (lostAware): the new one is this
|
|
729
788
|
// claim's, made from this claim's spec, its folder mounted.
|
|
@@ -762,7 +821,7 @@ store, deps) {
|
|
|
762
821
|
// booked) only while the tool holds it.
|
|
763
822
|
if (kind === "browser") {
|
|
764
823
|
// The run's sessions of a lower claim epoch go first, as a computer's do (the janitor's releaseOlder).
|
|
765
|
-
await releaseOlderClaims(provider, launch, signal);
|
|
824
|
+
await releaseOlderClaims(provider, launch, signal, { entry: entry.id, writes });
|
|
766
825
|
const opened = claimBrowser(provider, spec);
|
|
767
826
|
madeBy = entry;
|
|
768
827
|
return { id: entry.id, groups: ["cdp"], browser: opened.browser, release: opened.release };
|
|
@@ -791,7 +850,8 @@ store, deps) {
|
|
|
791
850
|
// end: a server's stop). The journal is read before anything is rented, so a read that fails leaves nothing to let
|
|
792
851
|
// go. A paired computer is the same one at every claim, and is no computer of the run's own.
|
|
793
852
|
const journalSays = !pairedRow && launch.epoch > 1 && (await deps.hadComputer?.()) === true;
|
|
794
|
-
|
|
853
|
+
// A paired computer is no computer of the run's own: it has no interval.
|
|
854
|
+
const fresh = await providerLaunch(provider, launch, spec, signal, pairedRow ? { writes: intervalWrites(undefined), entry: entry.id, kind } : { writes, entry: entry.id, kind, ...(meter ? { meter } : {}) });
|
|
795
855
|
// A sandbox the claim made is started again when its provider loses it mid-run (lostAware): never a paired
|
|
796
856
|
// computer, nor a desktop or a phone (a person may hold or watch it), nor the agent's own (persistent, above).
|
|
797
857
|
const { releasedOlder } = fresh;
|
|
@@ -834,6 +894,8 @@ store, deps) {
|
|
|
834
894
|
started = await resolved.start({ workspace: deps.workspace, check: { ...deps.check, epoch: deps.check.epoch ?? launch.epoch }, ...(disk ? { store: disk } : {}), held: folderHeld, ...(deps.signal ? { signal: deps.signal } : {}) });
|
|
835
895
|
}
|
|
836
896
|
catch (error) {
|
|
897
|
+
// A computer made before the start failed is the claim's no more: its time booked up to now.
|
|
898
|
+
await meter?.stopAll().catch(() => undefined);
|
|
837
899
|
await revoke().catch(() => undefined);
|
|
838
900
|
throw error;
|
|
839
901
|
}
|
|
@@ -863,6 +925,7 @@ store, deps) {
|
|
|
863
925
|
const opened = await deps.openComputerView({ store, sealKey: deps.sealKey, claim: launch.claim ?? { runId: launch.runId, epoch: launch.epoch }, kind: computer.kind, input, tag: made.id, watch, hold: theHold, ...(made.gone ? { gone: made.gone } : {}) });
|
|
864
926
|
if (!opened.ok) {
|
|
865
927
|
await started.release().catch(() => undefined);
|
|
928
|
+
await meter?.stopAll().catch(() => undefined);
|
|
866
929
|
await revoke().catch(() => undefined);
|
|
867
930
|
if (opened.refused === "fenced")
|
|
868
931
|
throw claimLost(`run ${launch.runId}'s claim (epoch ${launch.epoch}) is no longer this runner's, so its ${computer.kind} was released: ${opened.message}`);
|
|
@@ -887,6 +950,8 @@ store, deps) {
|
|
|
887
950
|
await started.release();
|
|
888
951
|
}
|
|
889
952
|
finally {
|
|
953
|
+
// A computer whose delete failed is held by no claim from here: its time is booked up to now, and no more.
|
|
954
|
+
await meter?.stopAll();
|
|
890
955
|
await revoke();
|
|
891
956
|
}
|
|
892
957
|
})());
|
|
@@ -901,6 +966,7 @@ store, deps) {
|
|
|
901
966
|
...(replaced ? { replaced } : {}),
|
|
902
967
|
...(ownKind ? { made: ownKind } : {}),
|
|
903
968
|
...(keeps ? { keeps: keeps.kind } : {}),
|
|
969
|
+
...(meter ? { settle: () => meter.stopAll() } : {}),
|
|
904
970
|
release,
|
|
905
971
|
async park(cause) {
|
|
906
972
|
if (!keeps || !settings || released) {
|
|
@@ -924,6 +990,7 @@ store, deps) {
|
|
|
924
990
|
await release();
|
|
925
991
|
return false;
|
|
926
992
|
}
|
|
993
|
+
await writes.kept(keeps.entry, ref.id);
|
|
927
994
|
// The live view ends (its frames and any take-over), saying what the run waits for and until when the computer is
|
|
928
995
|
// kept, never that it ended; the hold with it; the machine and its mount stay.
|
|
929
996
|
released = (async () => {
|
|
@@ -938,7 +1005,8 @@ store, deps) {
|
|
|
938
1005
|
// deletes it, a synced folder with no read-back.
|
|
939
1006
|
const timer = setTimeout(() => {
|
|
940
1007
|
void endKept(settings, launch.runId, row, async () => {
|
|
941
|
-
await deleteKept(keeps.provider, ref)
|
|
1008
|
+
if (await deleteKept(keeps.provider, ref))
|
|
1009
|
+
await writes.ended(keeps.entry, ref.id);
|
|
942
1010
|
await revoke().catch(() => undefined);
|
|
943
1011
|
}).catch(() => undefined);
|
|
944
1012
|
}, Math.max(0, row.until - now()));
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ComputerProvider, ComputerResources, ComputerSpec } from "@parcha/agentrun-computer";
|
|
2
|
+
import type { ComputeFigures } from "../compute-cost.js";
|
|
3
|
+
import type { ComputerIntervals } from "../store/index.js";
|
|
4
|
+
/** The store as these writes need it: its intervals, where it keeps them (a test's store or an older caller's may not). */
|
|
5
|
+
export type IntervalsStore = {
|
|
6
|
+
readonly computerIntervals?: ComputerIntervals;
|
|
7
|
+
};
|
|
8
|
+
/** The writes of one process, each safe: a failure is said on `log` and never thrown. */
|
|
9
|
+
export interface IntervalWrites {
|
|
10
|
+
/** Run `runId`'s computer `providerId` on `provider` was made with `figures` (figuresOf), of `kind`. */
|
|
11
|
+
opened(provider: string, providerId: string, runId: string, kind: string | null, figures: ComputeFigures): Promise<void>;
|
|
12
|
+
/** The run's park kept it past its claim. */
|
|
13
|
+
kept(provider: string, providerId: string): Promise<void>;
|
|
14
|
+
/** Its provider's delete answered ok, now. */
|
|
15
|
+
ended(provider: string, providerId: string): Promise<void>;
|
|
16
|
+
/** A claim booked `ms` more of its time on the run's ledger (compute-cost.ts). */
|
|
17
|
+
booked(provider: string, providerId: string, ms: number): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
/** What a computer was made with: each of `given`'s figures (its provider's answer), else `spec`'s (what its create
|
|
20
|
+
* asked), else null. A GPU's type and count are taken together, from one or the other. */
|
|
21
|
+
export declare function figuresOf(spec: Pick<ComputerSpec, "gpu" | "cpus" | "memoryMb">, given?: ComputerResources): ComputeFigures;
|
|
22
|
+
/** The interval writes over `store` (none written where it keeps none), on the clock `now`, failures said on `log`. */
|
|
23
|
+
export declare function intervalWrites(store: IntervalsStore | undefined, log?: (line: string) => void, now?: () => number): IntervalWrites;
|
|
24
|
+
/** `provider` with each delete that answers ok ended in `writes` under `id` (the provider's registry id): the janitor's
|
|
25
|
+
* sweeps and a run's end let computers go through it. A detach, or a delete that fails, writes nothing. */
|
|
26
|
+
export declare function endingDeletes(provider: ComputerProvider, id: string, writes: IntervalWrites): ComputerProvider;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** The default log: the runner's stderr, as its other lines. */
|
|
2
|
+
const stderrLog = (line) => void process.stderr.write(`agentrun-serve runner: ${line}\n`);
|
|
3
|
+
const count = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
4
|
+
const wholeMb = (mb) => (mb === null ? null : Math.round(mb));
|
|
5
|
+
/** What a computer was made with: each of `given`'s figures (its provider's answer), else `spec`'s (what its create
|
|
6
|
+
* asked), else null. A GPU's type and count are taken together, from one or the other. */
|
|
7
|
+
export function figuresOf(spec, given) {
|
|
8
|
+
const gpu = given?.gpu ?? spec.gpu;
|
|
9
|
+
return {
|
|
10
|
+
cpus: count(given?.cpus) ?? count(spec.cpus),
|
|
11
|
+
// Whole MB, as the store keeps it: a provider that counts memory in GiB may give a fraction of one.
|
|
12
|
+
memoryMb: wholeMb(count(given?.memoryMb) ?? count(spec.memoryMb)),
|
|
13
|
+
gpuType: typeof gpu?.type === "string" ? gpu.type : null,
|
|
14
|
+
gpuCount: gpu ? count(gpu.count) : null,
|
|
15
|
+
diskGb: count(given?.diskGb),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** The interval writes over `store` (none written where it keeps none), on the clock `now`, failures said on `log`. */
|
|
19
|
+
export function intervalWrites(store, log = stderrLog, now = Date.now) {
|
|
20
|
+
const intervals = store?.computerIntervals;
|
|
21
|
+
const safely = async (what, write) => {
|
|
22
|
+
if (!intervals)
|
|
23
|
+
return;
|
|
24
|
+
try {
|
|
25
|
+
await write(intervals);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
log(`the computer interval of ${what} was not written: ${error instanceof Error ? error.message : String(error)}`);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
opened: (provider, providerId, runId, kind, figures) => safely(`${provider} ${providerId} (run ${runId})`, (to) => to.opened({ runId, provider, providerId, kind, ...figures, startedAt: now() })),
|
|
33
|
+
kept: (provider, providerId) => safely(`${provider} ${providerId}`, (to) => to.kept(provider, providerId)),
|
|
34
|
+
ended: (provider, providerId) => safely(`${provider} ${providerId}`, (to) => to.ended(provider, providerId, now())),
|
|
35
|
+
// A store with no booked writes (an older test's) keeps none.
|
|
36
|
+
booked: (provider, providerId, ms) => safely(`${provider} ${providerId}'s booked time`, async (to) => void (await to.booked?.(provider, providerId, ms))),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** `provider` with each delete that answers ok ended in `writes` under `id` (the provider's registry id): the janitor's
|
|
40
|
+
* sweeps and a run's end let computers go through it. A detach, or a delete that fails, writes nothing. */
|
|
41
|
+
export function endingDeletes(provider, id, writes) {
|
|
42
|
+
return new Proxy(provider, {
|
|
43
|
+
// Every other member is the provider's own, called on the provider itself (its private fields are its own).
|
|
44
|
+
get(target, key) {
|
|
45
|
+
if (key !== "release") {
|
|
46
|
+
const value = Reflect.get(target, key, target);
|
|
47
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
48
|
+
}
|
|
49
|
+
return async (...args) => {
|
|
50
|
+
const released = await target.release(...args);
|
|
51
|
+
const [ref, how] = args;
|
|
52
|
+
if (released.ok && how === "delete")
|
|
53
|
+
await writes.ended(id, ref.id);
|
|
54
|
+
return released;
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -13,7 +13,7 @@ export declare const JANITOR_PROVIDER_MS: number;
|
|
|
13
13
|
export interface JanitorDeps {
|
|
14
14
|
/** The store: whose runs a computer's run label names, and the phone provider's ledger; with `settings` and `agents`,
|
|
15
15
|
* this server's id and its agents, whose own computers it then sweeps by their owner. */
|
|
16
|
-
readonly store: Pick<Store, "runs" | "computerLedger"> & Partial<Pick<Store, "settings" | "agents">>;
|
|
16
|
+
readonly store: Pick<Store, "runs" | "computerLedger"> & Partial<Pick<Store, "settings" | "agents" | "computerIntervals">>;
|
|
17
17
|
/** The run's computers as the runner composed them (computer-deps.ts computerDepsFrom): the registry, the provider
|
|
18
18
|
* makers and the key reader. */
|
|
19
19
|
readonly computers: Pick<ComputerDeps, "kinds" | "providers" | "keyOf"> & {
|
|
@@ -40,11 +40,15 @@ export type ProviderSweep = {
|
|
|
40
40
|
/** One sweep of every provider this server can list: each provider whose keys it has and whose computers the janitor
|
|
41
41
|
* ends, its computers past their deadline that are this server's and unheld ended. A provider that fails is answered
|
|
42
42
|
* in its words; nothing throws. */
|
|
43
|
-
export declare function sweepServerComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "providerMs"
|
|
43
|
+
export declare function sweepServerComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now" | "providerMs"> & {
|
|
44
|
+
readonly log?: (line: string) => void;
|
|
45
|
+
}, signal?: AbortSignal): Promise<ProviderSweep[]>;
|
|
44
46
|
/** The pass over the desktops and phones kept at a run's park (computer-kept.ts sweepKept): one past its time, or whose
|
|
45
47
|
* run ended, is ended through its provider, made with this server's keys, whatever its lifetime (a vendor's own end is
|
|
46
48
|
* hours away). Undefined without the store's settings, where none is kept. Never throws. */
|
|
47
|
-
export declare function sweepKeptComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now">
|
|
49
|
+
export declare function sweepKeptComputers(deps: Pick<JanitorDeps, "store" | "computers" | "now"> & {
|
|
50
|
+
readonly log?: (line: string) => void;
|
|
51
|
+
}): Promise<KeptSweep | undefined>;
|
|
48
52
|
/** The janitor: one sweep now, then one every `everyMs`, never two at once, and a kept pass at the earliest time a kept
|
|
49
53
|
* computer is due (sweepKeptComputers; each delete bounded, KEPT_DELETE_MS), so a park's keep ends on time though the
|
|
50
54
|
* process that parked it is gone. A wake that comes while a sweep runs runs the kept pass as soon as that sweep is done.
|