overvues 0.0.1 → 0.4.10

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 (78) hide show
  1. package/README.md +58 -2
  2. package/dist/ansi.js +24 -0
  3. package/dist/attach-client.js +586 -0
  4. package/dist/auth-connect-browser.js +112 -0
  5. package/dist/auth-connect.js +306 -0
  6. package/dist/browser-bootstrap.js +190 -0
  7. package/dist/browser-loopback.js +289 -0
  8. package/dist/build-identity.js +6 -0
  9. package/dist/codex-reviewer-apparmor.json +4 -0
  10. package/dist/codex-reviewer-seccomp.json +1238 -0
  11. package/dist/codex-runtime-protocol.js +1 -0
  12. package/dist/codex-subscription.js +53 -0
  13. package/dist/config.js +297 -0
  14. package/dist/connect-comparison-code.js +26 -0
  15. package/dist/development-runner.js +109 -0
  16. package/dist/dns-doctor.js +390 -0
  17. package/dist/docker-names.js +47 -0
  18. package/dist/doctor.js +114 -0
  19. package/dist/gadget-job.js +804 -0
  20. package/dist/health-check.js +112 -0
  21. package/dist/help.js +71 -0
  22. package/dist/host-auth.js +22 -0
  23. package/dist/host-diagnostics.js +20 -0
  24. package/dist/host-runtime-identity.js +39 -0
  25. package/dist/host-runtime-metadata.js +56 -0
  26. package/dist/host-service.js +377 -0
  27. package/dist/host-stack.js +484 -0
  28. package/dist/host-supervisor.js +237 -0
  29. package/dist/host-update.js +387 -0
  30. package/dist/hub-address.js +100 -0
  31. package/dist/hub-client.js +459 -0
  32. package/dist/hub-kind.js +88 -0
  33. package/dist/identity.js +73 -0
  34. package/dist/index.js +185 -0
  35. package/dist/init.js +295 -0
  36. package/dist/isolated-run-cache.js +116 -0
  37. package/dist/local-stack.js +685 -0
  38. package/dist/machine-capacity.js +128 -0
  39. package/dist/managed-runner-image.js +13 -0
  40. package/dist/os-acted-record.js +49 -0
  41. package/dist/os-adapter.js +8190 -0
  42. package/dist/os-command.js +292 -0
  43. package/dist/os-handoff.js +274 -0
  44. package/dist/os-keychain.js +247 -0
  45. package/dist/os-machine.js +818 -0
  46. package/dist/os-owner.js +61 -0
  47. package/dist/os-private-file.js +98 -0
  48. package/dist/os-protocol.js +468 -0
  49. package/dist/os-session-store.js +133 -0
  50. package/dist/os-status.js +102 -0
  51. package/dist/picker.js +863 -0
  52. package/dist/portable-workspace-capability.js +60 -0
  53. package/dist/project-environment-capability.js +62 -0
  54. package/dist/project-environment.js +28 -0
  55. package/dist/project-preparation.js +134 -0
  56. package/dist/readiness.js +28 -0
  57. package/dist/redaction.js +27 -0
  58. package/dist/reviewer-isolation-capability.js +61 -0
  59. package/dist/runner-api-auth.js +80 -0
  60. package/dist/runner-command-fence.js +536 -0
  61. package/dist/runner-inspection.js +57 -0
  62. package/dist/runner-inventory.js +587 -0
  63. package/dist/runner-server.js +2520 -0
  64. package/dist/runner-session.js +470 -0
  65. package/dist/runner-slug-folder.js +39 -0
  66. package/dist/runner-slug.js +40 -0
  67. package/dist/setup.js +139 -0
  68. package/dist/shutdown.js +102 -0
  69. package/dist/splash.js +65 -0
  70. package/dist/staged-workspaces.js +441 -0
  71. package/dist/staging-disk.js +97 -0
  72. package/dist/terminal-lane.js +339 -0
  73. package/dist/terminal-recovery.js +502 -0
  74. package/licenses/capnweb.txt +23 -0
  75. package/licenses/moby-profiles.txt +212 -0
  76. package/licenses/ws.txt +22 -0
  77. package/package.json +28 -7
  78. package/cli.js +0 -2
@@ -0,0 +1,587 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { dockerName, dockerNamePattern } from "./docker-names.js";
5
+ /**
6
+ * The runner's single record of the Docker volumes it manages (fast session
7
+ * startup hardening): repository mirrors, pre-staged workspaces and the
8
+ * workspace volumes slugs have recorded. It replaces `mirrors.json`,
9
+ * `staged.json` and the scan of every slug's `workspace-volume.json`.
10
+ *
11
+ * A slug's `workspace-volume.json` in its fence directory stays the authority
12
+ * for which volume that slug mounts, because the fence protocol needs a
13
+ * create-exclusive per-slug record. The inventory mirrors those records
14
+ * (`kind: "workspace"`); reconciliation re-derives them from the fence store,
15
+ * so a record and its mirror can never disagree for long.
16
+ *
17
+ * Every change is a synchronous read-modify-write of one file, replaced
18
+ * atomically, so changes made by this process never interleave.
19
+ */
20
+ export const RUNNER_INVENTORY_FILE = "runner-inventory.json";
21
+ const RUNNER_INVENTORY_VERSION = 1;
22
+ const LEGACY_MIRROR_STATE_FILE = "mirrors.json";
23
+ const LEGACY_STAGED_STATE_FILE = "staged.json";
24
+ // This runner's own volumes, named from its Docker prefix (docker-names.ts): `tiller-mirror-…`
25
+ // for the default `tiller` runner, `overvues-mirror-…` for `overvues`.
26
+ export const MIRROR_VOLUME_NAME = dockerNamePattern("mirror-[a-f0-9]{16}");
27
+ export const STAGED_VOLUME_NAME = dockerNamePattern("ws-staged-[a-f0-9-]{36}");
28
+ const WORKSPACE_VOLUME_NAME = dockerNamePattern("ws-[A-Za-z0-9_.-]{1,200}");
29
+ const COMMIT = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
30
+ export const STAGED_WORKSPACE_VOLUME_PREFIX = dockerName("ws-staged-");
31
+ /** Bumped whenever the staged layout changes, so older volumes are never adopted. */
32
+ export const STAGED_WORKSPACE_PREPARATION_VERSION = 1;
33
+ export const STAGED_WORKSPACE_IDLE_LIMIT_MS = 24 * 60 * 60 * 1000;
34
+ export const STAGED_WORKSPACE_DEFAULT_BUDGET_BYTES = 5 * 1024 * 1024 * 1024;
35
+ export const GIT_MIRROR_IDLE_LIMIT_MS = 30 * 24 * 60 * 60 * 1000;
36
+ export function isStagedWorkspaceVolumeName(value) {
37
+ return STAGED_VOLUME_NAME.test(value);
38
+ }
39
+ function validName(kind, volume) {
40
+ if (kind === "mirror")
41
+ return MIRROR_VOLUME_NAME.test(volume);
42
+ if (kind === "staged")
43
+ return STAGED_VOLUME_NAME.test(volume);
44
+ return WORKSPACE_VOLUME_NAME.test(volume);
45
+ }
46
+ function validTime(value) {
47
+ return typeof value === "string" && Number.isFinite(Date.parse(value));
48
+ }
49
+ function normalizeEntry(volume, value) {
50
+ const entry = (value && typeof value === "object" ? value : {});
51
+ const kind = entry.kind;
52
+ if (kind !== "mirror" && kind !== "staged" && kind !== "workspace")
53
+ return null;
54
+ if (!validName(kind, volume))
55
+ return null;
56
+ if (entry.repoUrl !== null && typeof entry.repoUrl !== "string")
57
+ return null;
58
+ if (entry.commit !== null && !(typeof entry.commit === "string" && COMMIT.test(entry.commit)))
59
+ return null;
60
+ if (entry.slug !== null && typeof entry.slug !== "string")
61
+ return null;
62
+ if (entry.preparationVersion !== null && !Number.isSafeInteger(entry.preparationVersion))
63
+ return null;
64
+ if (!validTime(entry.createdAt) || !validTime(entry.lastUsedAt))
65
+ return null;
66
+ const state = entry.state;
67
+ if (kind === "mirror" && (state !== "ready" || !entry.repoUrl))
68
+ return null;
69
+ if (kind === "staged" && ((state !== "building" && state !== "ready")
70
+ || !entry.repoUrl || !entry.commit || entry.preparationVersion === null))
71
+ return null;
72
+ if (kind === "workspace" && (state !== "assigned" || !entry.slug))
73
+ return null;
74
+ const deps = entry.deps;
75
+ if (deps !== undefined && deps !== "ready" && deps !== "failed" && deps !== "skipped")
76
+ return null;
77
+ return {
78
+ kind,
79
+ repoUrl: entry.repoUrl ?? null,
80
+ commit: entry.commit ?? null,
81
+ slug: entry.slug ?? null,
82
+ preparationVersion: entry.preparationVersion ?? null,
83
+ state: state,
84
+ ...(deps ? { deps } : {}),
85
+ createdAt: entry.createdAt,
86
+ lastUsedAt: entry.lastUsedAt,
87
+ };
88
+ }
89
+ function inventoryPath(localStateDir) {
90
+ return join(localStateDir, RUNNER_INVENTORY_FILE);
91
+ }
92
+ /** The inventory's valid entries; a missing or unreadable file reads as empty. */
93
+ export function readRunnerInventory(localStateDir) {
94
+ let parsed;
95
+ try {
96
+ parsed = JSON.parse(readFileSync(inventoryPath(localStateDir), "utf8"));
97
+ }
98
+ catch {
99
+ return {};
100
+ }
101
+ const volumes = parsed?.volumes;
102
+ const entries = {};
103
+ if (parsed.version !== RUNNER_INVENTORY_VERSION
104
+ || !volumes || typeof volumes !== "object" || Array.isArray(volumes))
105
+ return entries;
106
+ for (const [volume, value] of Object.entries(volumes)) {
107
+ const entry = normalizeEntry(volume, value);
108
+ if (entry)
109
+ entries[volume] = entry;
110
+ }
111
+ return entries;
112
+ }
113
+ function writeJsonAtomic(path, value) {
114
+ const temporary = `${path}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
115
+ try {
116
+ writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { encoding: "utf8", mode: 0o600, flag: "wx" });
117
+ renameSync(temporary, path);
118
+ }
119
+ finally {
120
+ rmSync(temporary, { force: true });
121
+ }
122
+ }
123
+ function readLegacy(path) {
124
+ let text;
125
+ try {
126
+ text = readFileSync(path, "utf8");
127
+ }
128
+ catch (error) {
129
+ if (error.code === "ENOENT")
130
+ return null;
131
+ throw error;
132
+ }
133
+ try {
134
+ const parsed = JSON.parse(text);
135
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : {};
136
+ }
137
+ catch {
138
+ return {};
139
+ }
140
+ }
141
+ export class RunnerInventory {
142
+ localStateDir;
143
+ now;
144
+ constructor(localStateDir, now = Date.now) {
145
+ this.localStateDir = localStateDir;
146
+ this.now = now;
147
+ }
148
+ read() {
149
+ return readRunnerInventory(this.localStateDir);
150
+ }
151
+ /**
152
+ * Applies `change` to the current entries and writes them back if they
153
+ * changed. Synchronous, so no other change of this process can interleave.
154
+ */
155
+ update(change) {
156
+ const entries = this.read();
157
+ const before = JSON.stringify(entries);
158
+ const result = change(entries);
159
+ if (JSON.stringify(entries) !== before) {
160
+ mkdirSync(this.localStateDir, { recursive: true, mode: 0o700 });
161
+ writeJsonAtomic(inventoryPath(this.localStateDir), { version: RUNNER_INVENTORY_VERSION, volumes: entries });
162
+ }
163
+ return result;
164
+ }
165
+ timestamp() {
166
+ return new Date(this.now()).toISOString();
167
+ }
168
+ recordMirrorUse(volume, repoUrl) {
169
+ if (!MIRROR_VOLUME_NAME.test(volume))
170
+ return;
171
+ const at = this.timestamp();
172
+ this.update((entries) => {
173
+ const existing = entries[volume]?.kind === "mirror" ? entries[volume] : null;
174
+ entries[volume] = {
175
+ kind: "mirror",
176
+ repoUrl,
177
+ commit: null,
178
+ slug: null,
179
+ preparationVersion: null,
180
+ state: "ready",
181
+ createdAt: existing?.createdAt ?? at,
182
+ lastUsedAt: at,
183
+ };
184
+ });
185
+ }
186
+ forget(volume) {
187
+ this.update((entries) => {
188
+ delete entries[volume];
189
+ });
190
+ }
191
+ /**
192
+ * Mirrors a slug's `workspace-volume.json` record: the volume becomes, or
193
+ * stays, that slug's workspace. A staged entry being adopted keeps its
194
+ * repository, commit and dependency state.
195
+ */
196
+ recordWorkspace(slug, volume) {
197
+ if (!WORKSPACE_VOLUME_NAME.test(volume))
198
+ return;
199
+ const at = this.timestamp();
200
+ this.update((entries) => {
201
+ const existing = entries[volume];
202
+ entries[volume] = {
203
+ kind: "workspace",
204
+ repoUrl: existing?.repoUrl ?? null,
205
+ commit: existing?.commit ?? null,
206
+ slug,
207
+ preparationVersion: existing?.preparationVersion ?? null,
208
+ state: "assigned",
209
+ ...(existing?.deps ? { deps: existing.deps } : {}),
210
+ createdAt: existing?.createdAt ?? at,
211
+ lastUsedAt: at,
212
+ };
213
+ });
214
+ }
215
+ /** Drops the slug's workspace entries after its record and volumes are gone. */
216
+ forgetWorkspace(slug) {
217
+ this.update((entries) => {
218
+ for (const [volume, entry] of Object.entries(entries)) {
219
+ if (entry.kind === "workspace" && entry.slug === slug)
220
+ delete entries[volume];
221
+ }
222
+ });
223
+ }
224
+ /**
225
+ * One-time move from `mirrors.json`, `staged.json` and the per-slug records
226
+ * into the inventory. Entries already in the inventory win (a downgrade and
227
+ * upgrade may leave both); the legacy files are deleted once their entries
228
+ * are written. Returns the legacy files it removed.
229
+ */
230
+ migrate(workspaces) {
231
+ const mirrorPath = join(this.localStateDir, LEGACY_MIRROR_STATE_FILE);
232
+ const stagedPath = join(this.localStateDir, LEGACY_STAGED_STATE_FILE);
233
+ const mirrors = readLegacy(mirrorPath);
234
+ const staged = readLegacy(stagedPath);
235
+ const now = this.timestamp();
236
+ this.update((entries) => {
237
+ for (const [volume, value] of Object.entries(mirrors ?? {})) {
238
+ const legacy = value;
239
+ if (entries[volume] || !MIRROR_VOLUME_NAME.test(volume)
240
+ || typeof legacy?.repoUrl !== "string" || !validTime(legacy.lastUsedAt))
241
+ continue;
242
+ entries[volume] = {
243
+ kind: "mirror",
244
+ repoUrl: legacy.repoUrl,
245
+ commit: null,
246
+ slug: null,
247
+ preparationVersion: null,
248
+ state: "ready",
249
+ createdAt: legacy.lastUsedAt,
250
+ lastUsedAt: legacy.lastUsedAt,
251
+ };
252
+ }
253
+ for (const [volume, value] of Object.entries(staged ?? {})) {
254
+ const legacy = value;
255
+ if (entries[volume] || !STAGED_VOLUME_NAME.test(volume)
256
+ || typeof legacy?.repoUrl !== "string"
257
+ || typeof legacy.commit !== "string" || !COMMIT.test(legacy.commit)
258
+ || !Number.isSafeInteger(legacy.preparationVersion)
259
+ || !validTime(legacy.createdAt)
260
+ || (legacy.state !== "building" && legacy.state !== "ready"))
261
+ continue;
262
+ entries[volume] = {
263
+ kind: "staged",
264
+ repoUrl: legacy.repoUrl,
265
+ commit: legacy.commit,
266
+ slug: null,
267
+ preparationVersion: legacy.preparationVersion,
268
+ state: legacy.state,
269
+ createdAt: legacy.createdAt,
270
+ lastUsedAt: legacy.createdAt,
271
+ };
272
+ }
273
+ for (const record of workspaces.records) {
274
+ if (entries[record.volume]?.kind === "workspace" && entries[record.volume].slug === record.slug)
275
+ continue;
276
+ const existing = entries[record.volume];
277
+ entries[record.volume] = {
278
+ kind: "workspace",
279
+ repoUrl: existing?.repoUrl ?? null,
280
+ commit: existing?.commit ?? null,
281
+ slug: record.slug,
282
+ preparationVersion: existing?.preparationVersion ?? null,
283
+ state: "assigned",
284
+ createdAt: validTime(record.recordedAt) ? record.recordedAt : now,
285
+ lastUsedAt: now,
286
+ };
287
+ }
288
+ });
289
+ const removed = [];
290
+ for (const [path, legacy] of [[mirrorPath, mirrors], [stagedPath, staged]]) {
291
+ if (legacy === null)
292
+ continue;
293
+ rmSync(path, { force: true });
294
+ removed.push(path);
295
+ }
296
+ return removed;
297
+ }
298
+ }
299
+ /** Bytes from Docker's human-readable size ("1.217GB", "12.5kB", "0B"). */
300
+ export function parseDockerSize(value) {
301
+ if (typeof value !== "string")
302
+ return null;
303
+ const match = /^([0-9]+(?:\.[0-9]+)?)\s*([kKMGTPE]?)(i?)B$/.exec(value.trim());
304
+ if (!match)
305
+ return null;
306
+ const power = ["", "k", "m", "g", "t", "p", "e"].indexOf(match[2].toLowerCase());
307
+ return Math.round(Number(match[1]) * (match[3] ? 1024 : 1000) ** power);
308
+ }
309
+ /** Parses `docker system df -v --format '{{json .Volumes}}'`. */
310
+ export function parseDockerVolumeUsage(output) {
311
+ const parsed = JSON.parse(output);
312
+ if (!Array.isArray(parsed))
313
+ throw new Error("Docker did not report volume usage");
314
+ const volumes = [];
315
+ for (const value of parsed) {
316
+ const entry = value;
317
+ const bytes = parseDockerSize(entry?.Size);
318
+ const links = Number(entry?.Links);
319
+ if (typeof entry?.Name === "string" && bytes !== null) {
320
+ volumes.push({ name: entry.Name, links: Number.isFinite(links) ? links : 1, bytes });
321
+ }
322
+ }
323
+ return volumes;
324
+ }
325
+ /** Measures every volume's size, or null (with a log line) when Docker cannot. */
326
+ export async function measureVolumes(runDocker, log) {
327
+ try {
328
+ return parseDockerVolumeUsage(await runDocker(["system", "df", "-v", "--format", "{{json .Volumes}}"]));
329
+ }
330
+ catch (error) {
331
+ log(`[runner] could not measure volumes: ${errorText(error)}`);
332
+ return null;
333
+ }
334
+ }
335
+ function errorText(error) {
336
+ return error instanceof Error ? error.message : String(error);
337
+ }
338
+ /**
339
+ * A container that deletes everything in a volume: no network, an eighth of
340
+ * the default CPU share, root only to delete files the sandbox user owns.
341
+ */
342
+ export function buildVolumeEmptyRunArgs(image, volume) {
343
+ return [
344
+ "run",
345
+ "--rm",
346
+ ...(image.startsWith("sha256:") ? ["--pull=never"] : []),
347
+ "--network",
348
+ "none",
349
+ "--cpu-shares",
350
+ "128",
351
+ "--user",
352
+ "0:0",
353
+ "--cap-drop",
354
+ "ALL",
355
+ "--cap-add",
356
+ "DAC_OVERRIDE",
357
+ "--cap-add",
358
+ "FOWNER",
359
+ "--security-opt",
360
+ "no-new-privileges",
361
+ "-v",
362
+ `${volume}:/volume`,
363
+ "--entrypoint",
364
+ "find",
365
+ image,
366
+ "/volume",
367
+ "-mindepth",
368
+ "1",
369
+ "-delete",
370
+ ];
371
+ }
372
+ /**
373
+ * Deletes a volume's files from inside a container, so that the
374
+ * `docker volume rm` after it is quick. Docker's local volume driver holds
375
+ * one lock for the whole of a removal, and every create that mounts a volume,
376
+ * every `docker volume inspect` and every untyped `docker inspect` waits on
377
+ * it: removing a workspace with its dependencies (hundreds of thousands of
378
+ * files) held session creates for 10 to 20 s. A volume that is missing or
379
+ * that any container references is left alone, so the removal reports it as
380
+ * before; any failure here only means the plain removal does the work.
381
+ */
382
+ export async function emptyDockerVolume(runDocker, image, volume) {
383
+ try {
384
+ await runDocker(["volume", "inspect", "--format", "{{.Name}}", volume]);
385
+ }
386
+ catch {
387
+ return;
388
+ }
389
+ try {
390
+ if ((await runDocker(["ps", "-a", "-q", "--filter", `volume=${volume}`])).trim())
391
+ return;
392
+ await runDocker(buildVolumeEmptyRunArgs(image, volume));
393
+ }
394
+ catch {
395
+ // The removal below still runs.
396
+ }
397
+ }
398
+ export async function removeDockerVolume(runDocker, volume, log, options = {}) {
399
+ if (options.emptyWith)
400
+ await emptyDockerVolume(runDocker, options.emptyWith, volume);
401
+ try {
402
+ await runDocker(["volume", "rm", volume]);
403
+ return "removed";
404
+ }
405
+ catch (error) {
406
+ const message = errorText(error).toLowerCase();
407
+ if (message.includes("no such volume"))
408
+ return "missing";
409
+ if (message.includes("in use"))
410
+ return "in-use";
411
+ log(`[runner] could not remove volume ${volume}: ${errorText(error)}`);
412
+ return "failed";
413
+ }
414
+ }
415
+ /**
416
+ * The runner's one cleanup pass over the volumes it manages, at startup and
417
+ * then daily. In order:
418
+ *
419
+ * 1. Workspaces (staging on). Every slug record is mirrored into the
420
+ * inventory. A workspace entry whose slug no longer records it (the fence
421
+ * directory is gone, or the record names another volume) is dropped; its
422
+ * volume is removed unless it is the slug's default name, which a later
423
+ * create of the slug would mount again as an existing workspace. A slug
424
+ * whose record cannot be read keeps everything.
425
+ * 2. Staged volumes (staging on). Removes staged volumes with no ready entry
426
+ * (half-built ones included), entries idle for 24 hours, of an older
427
+ * preparation version or whose volume is gone. Never a volume a slug
428
+ * records or one being staged now.
429
+ * 3. Mirrors (mirrors on). Removes mirrors no container has mounted for 30
430
+ * days; one a container still references is in use and stays.
431
+ * 4. Byte budget (staging on). Across mirrors and unadopted staged volumes,
432
+ * evicts unused ones oldest first until the total fits.
433
+ *
434
+ * A removal that fails with "in use" keeps the volume for the next pass.
435
+ */
436
+ export async function reconcileRunnerVolumes(options) {
437
+ const now = options.now ?? Date.now;
438
+ const log = options.log ?? ((message) => console.log(message));
439
+ const { inventory, runDocker } = options;
440
+ const removed = [];
441
+ const remove = async (volume) => {
442
+ const outcome = await removeDockerVolume(runDocker, volume, log);
443
+ if (outcome === "removed" || outcome === "missing")
444
+ removed.push(volume);
445
+ return outcome;
446
+ };
447
+ if (options.staged) {
448
+ const { isBuilding } = options.staged;
449
+ const listed = (await runDocker([
450
+ "volume", "ls", "--filter", `name=${STAGED_WORKSPACE_VOLUME_PREFIX}`, "--format", "{{.Name}}",
451
+ ])).split(/\r?\n/).map((name) => name.trim()).filter(isStagedWorkspaceVolumeName);
452
+ const workspaces = options.workspaceRecords();
453
+ const doomed = inventory.update((entries) => {
454
+ const recordedBy = new Map(workspaces.records.map((record) => [record.volume, record.slug]));
455
+ const protectedVolumes = new Set(recordedBy.keys());
456
+ const doomedVolumes = new Set();
457
+ const at = new Date(now()).toISOString();
458
+ for (const record of workspaces.records) {
459
+ const existing = entries[record.volume];
460
+ if (existing?.kind === "workspace" && existing.slug === record.slug)
461
+ continue;
462
+ entries[record.volume] = {
463
+ kind: "workspace",
464
+ repoUrl: existing?.repoUrl ?? null,
465
+ commit: existing?.commit ?? null,
466
+ slug: record.slug,
467
+ preparationVersion: existing?.preparationVersion ?? null,
468
+ state: "assigned",
469
+ ...(existing?.deps ? { deps: existing.deps } : {}),
470
+ createdAt: existing?.createdAt ?? at,
471
+ lastUsedAt: existing?.lastUsedAt ?? at,
472
+ };
473
+ }
474
+ for (const [volume, entry] of Object.entries(entries)) {
475
+ if (entry.kind !== "workspace")
476
+ continue;
477
+ if (recordedBy.get(volume) === entry.slug)
478
+ continue;
479
+ if (workspaces.unreadableSlugs.has(entry.slug)) {
480
+ protectedVolumes.add(volume);
481
+ continue;
482
+ }
483
+ delete entries[volume];
484
+ if (recordedBy.has(volume))
485
+ continue;
486
+ if (volume === options.defaultWorkspaceVolume(entry.slug)) {
487
+ log(`[runner] ${entry.slug} no longer records ${volume}; it stays for a later create of that slug`);
488
+ continue;
489
+ }
490
+ doomedVolumes.add(volume);
491
+ }
492
+ const existing = new Set(listed);
493
+ for (const [volume, entry] of Object.entries(entries)) {
494
+ if (entry.kind !== "staged" || isBuilding(volume))
495
+ continue;
496
+ const stale = entry.state !== "ready"
497
+ || entry.preparationVersion !== STAGED_WORKSPACE_PREPARATION_VERSION
498
+ || now() - Date.parse(entry.lastUsedAt) > STAGED_WORKSPACE_IDLE_LIMIT_MS
499
+ || !existing.has(volume)
500
+ || protectedVolumes.has(volume);
501
+ if (!stale)
502
+ continue;
503
+ delete entries[volume];
504
+ if (existing.has(volume) && !protectedVolumes.has(volume))
505
+ doomedVolumes.add(volume);
506
+ }
507
+ for (const volume of listed) {
508
+ if (!entries[volume] && !isBuilding(volume) && !protectedVolumes.has(volume))
509
+ doomedVolumes.add(volume);
510
+ }
511
+ return [...doomedVolumes];
512
+ });
513
+ for (const volume of doomed)
514
+ await remove(volume);
515
+ }
516
+ if (options.mirrors) {
517
+ const isIdle = (entry) => entry?.kind === "mirror" && now() - Date.parse(entry.lastUsedAt) > GIT_MIRROR_IDLE_LIMIT_MS;
518
+ for (const [volume, entry] of Object.entries(inventory.read())) {
519
+ if (!isIdle(entry))
520
+ continue;
521
+ const outcome = await remove(volume);
522
+ if (outcome !== "removed" && outcome !== "missing")
523
+ continue;
524
+ // A create may have mounted the mirror again while Docker was removing it.
525
+ inventory.update((entries) => {
526
+ if (isIdle(entries[volume]))
527
+ delete entries[volume];
528
+ });
529
+ }
530
+ }
531
+ if (options.staged && options.staged.budgetBytes > 0) {
532
+ removed.push(...await enforceBudget(options, log));
533
+ }
534
+ return { removed };
535
+ }
536
+ async function enforceBudget(options, log) {
537
+ const { inventory, runDocker } = options;
538
+ const { isBuilding, budgetBytes } = options.staged;
539
+ const usage = await measureVolumes(runDocker, log);
540
+ if (!usage) {
541
+ log("[runner] the byte budget was not enforced");
542
+ return [];
543
+ }
544
+ const recorded = new Set(options.workspaceRecords().records.map((record) => record.volume));
545
+ const entries = inventory.read();
546
+ const caches = usage.filter((volume) => MIRROR_VOLUME_NAME.test(volume.name)
547
+ || (isStagedWorkspaceVolumeName(volume.name) && !recorded.has(volume.name) && entries[volume.name]?.kind !== "workspace"));
548
+ let total = caches.reduce((sum, volume) => sum + volume.bytes, 0);
549
+ if (total <= budgetBytes)
550
+ return [];
551
+ const candidates = caches.flatMap((volume) => {
552
+ if (volume.links > 0 || isBuilding(volume.name))
553
+ return [];
554
+ const entry = entries[volume.name];
555
+ if (isStagedWorkspaceVolumeName(volume.name)) {
556
+ return entry?.kind === "staged" && entry.state === "ready" ? [{ ...volume, age: Date.parse(entry.lastUsedAt) }] : [];
557
+ }
558
+ // A mirror with no entry counts as the oldest.
559
+ return [{ ...volume, age: entry?.kind === "mirror" ? Date.parse(entry.lastUsedAt) : 0 }];
560
+ }).sort((left, right) => left.age - right.age);
561
+ const removed = [];
562
+ for (const candidate of candidates) {
563
+ if (total <= budgetBytes)
564
+ break;
565
+ if (isStagedWorkspaceVolumeName(candidate.name)) {
566
+ const taken = inventory.update((current) => {
567
+ if (current[candidate.name]?.kind !== "staged" || current[candidate.name].state !== "ready")
568
+ return false;
569
+ delete current[candidate.name];
570
+ return true;
571
+ });
572
+ if (!taken)
573
+ continue;
574
+ }
575
+ const outcome = await removeDockerVolume(runDocker, candidate.name, log);
576
+ if (outcome !== "removed" && outcome !== "missing")
577
+ continue;
578
+ if (MIRROR_VOLUME_NAME.test(candidate.name))
579
+ inventory.forget(candidate.name);
580
+ total -= candidate.bytes;
581
+ removed.push(candidate.name);
582
+ }
583
+ if (total > budgetBytes) {
584
+ log(`[runner] cache volumes still use ${total} bytes, over the ${budgetBytes} byte budget; the rest are in use`);
585
+ }
586
+ return removed;
587
+ }