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,441 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { STAGED_WORKSPACE_PREPARATION_VERSION, STAGED_WORKSPACE_VOLUME_PREFIX, removeDockerVolume, } from "./runner-inventory.js";
3
+ export { STAGED_WORKSPACE_DEFAULT_BUDGET_BYTES, STAGED_WORKSPACE_IDLE_LIMIT_MS, STAGED_WORKSPACE_PREPARATION_VERSION, STAGED_WORKSPACE_VOLUME_PREFIX, isStagedWorkspaceVolumeName, parseDockerSize, parseDockerVolumeUsage, } from "./runner-inventory.js";
4
+ import { dockerLabel, dockerName, dockerNamePattern } from "./docker-names.js";
5
+ /**
6
+ * Pre-staged workspace volumes (fast session startup, Gate D).
7
+ *
8
+ * A staged volume holds a plain checkout of one commit, made from the host's
9
+ * repository mirror by an unprivileged, network-less container. It holds no
10
+ * credential, identity or process. The runner inventory is the runner's
11
+ * authority on which volumes are ready; the `.tiller-staged.json` marker
12
+ * inside the volume is the container's cross-check, consumed by the
13
+ * workspace bootstrap. A create adopts a ready volume by recording it as the
14
+ * slug's workspace volume; from then on it is that slug's workspace and never
15
+ * a cache again. Cleanup lives in `reconcileRunnerVolumes`.
16
+ *
17
+ * A second run then installs the project's dependencies into the checkout,
18
+ * with network but no credential, before the volume is offered for adoption.
19
+ */
20
+ export const STAGED_WORKSPACE_CAP = 3;
21
+ /** The stage container's exit code when the mirror does not hold the commit. */
22
+ export const WORKSPACE_STAGE_COMMIT_MISSING_EXIT_CODE = 3;
23
+ /** The dependency phase's exit codes: nothing to install, or a failed install that left the checkout clean. */
24
+ export const WORKSPACE_STAGE_DEPENDENCIES_SKIPPED_EXIT_CODE = 4;
25
+ export const WORKSPACE_STAGE_DEPENDENCIES_FAILED_EXIT_CODE = 5;
26
+ export const WORKSPACE_STAGE_TIMEOUT_MS = 5 * 60 * 1000;
27
+ /** The dependency install's own cap inside the container. */
28
+ export const WORKSPACE_STAGE_DEPENDENCIES_TIMEOUT_MS = 10 * 60 * 1000;
29
+ /** The runner removes a dependency run this long after the install's own cap. */
30
+ const DEPENDENCIES_CONTAINER_GRACE_MS = 60 * 1000;
31
+ const MIRROR_CONTAINER_PATH = "/var/cache/tiller/git-mirror";
32
+ const STAGE_USER = "1001:1001";
33
+ const COMMIT = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
34
+ const MIRROR_VOLUME_NAME = dockerNamePattern("mirror-[a-f0-9]{16}");
35
+ /**
36
+ * Stage and dependency runs yield CPU to environment containers: an eighth of
37
+ * the default share (1024), which only matters when the CPU is contended, and
38
+ * at most two CPUs, so a dependency install can never fill the Docker VM while
39
+ * a session is starting beside it.
40
+ * There is no IO priority: `--blkio-weight` makes `docker run` fail where the
41
+ * kernel has no `io.weight`, as in Docker Desktop's VM.
42
+ */
43
+ export const STAGE_PRIORITY_ARGS = ["--cpu-shares", "128", "--cpus", "2"];
44
+ /**
45
+ * The stage run: the sandbox image as uid 1001 with no network, no
46
+ * capabilities, the mirror read-only and the new volume at `/workspace`. Its
47
+ * only inputs are the repository URL and the commit.
48
+ */
49
+ export function buildWorkspaceStageRunArgs(options) {
50
+ return [
51
+ "run",
52
+ "--rm",
53
+ ...(options.image.startsWith("sha256:") ? ["--pull=never"] : []),
54
+ "--name",
55
+ options.containerName,
56
+ ...STAGE_PRIORITY_ARGS,
57
+ "--network",
58
+ "none",
59
+ "--user",
60
+ STAGE_USER,
61
+ "--cap-drop",
62
+ "ALL",
63
+ "--security-opt",
64
+ "no-new-privileges",
65
+ "-v",
66
+ `${options.mirrorVolume}:${MIRROR_CONTAINER_PATH}:ro`,
67
+ "-v",
68
+ `${options.stagedVolume}:/workspace`,
69
+ "-e",
70
+ "TILLER_BOOTSTRAP_MODE=workspace-stage",
71
+ "-e",
72
+ `TILLER_STAGE_REPO_URL=${options.repoUrl}`,
73
+ "-e",
74
+ `TILLER_STAGE_COMMIT=${options.commit}`,
75
+ options.image,
76
+ ];
77
+ }
78
+ /**
79
+ * The dependency run: the same image and uid, the staged volume and the
80
+ * shared package caches, with network (the default bridge) but no mirror and
81
+ * no credential. The script installs with the command the entrypoint would
82
+ * pick, under its own time cap, and cleans the dependencies away again when
83
+ * the install fails or the volume would exceed `maxBytes`.
84
+ */
85
+ export function buildWorkspaceDependenciesRunArgs(options) {
86
+ const cacheEnv = Object.assign({}, ...options.cacheMounts.map((mount) => mount.env));
87
+ return [
88
+ "run",
89
+ "--rm",
90
+ ...(options.image.startsWith("sha256:") ? ["--pull=never"] : []),
91
+ "--name",
92
+ options.containerName,
93
+ ...STAGE_PRIORITY_ARGS,
94
+ "--network",
95
+ "bridge",
96
+ "--user",
97
+ STAGE_USER,
98
+ "--cap-drop",
99
+ "ALL",
100
+ "--security-opt",
101
+ "no-new-privileges",
102
+ "-v",
103
+ `${options.stagedVolume}:/workspace`,
104
+ ...options.cacheMounts.flatMap((mount) => ["-v", `${mount.volume}:${mount.target}`]),
105
+ ...Object.entries(cacheEnv).flatMap(([name, value]) => ["-e", `${name}=${value}`]),
106
+ "-e",
107
+ "TILLER_BOOTSTRAP_MODE=workspace-stage",
108
+ "-e",
109
+ "TILLER_STAGE_PHASE=dependencies",
110
+ "-e",
111
+ "RUNNER_BACKEND=host",
112
+ "-e",
113
+ `TILLER_STAGE_REPO_URL=${options.repoUrl}`,
114
+ "-e",
115
+ `TILLER_STAGE_COMMIT=${options.commit}`,
116
+ "-e",
117
+ `TILLER_STAGE_DEPENDENCIES_TIMEOUT_MS=${options.timeoutMs}`,
118
+ "-e",
119
+ `TILLER_STAGE_DEPENDENCIES_MAX_BYTES=${options.maxBytes}`,
120
+ ...(options.command ? ["-e", `TILLER_DEPENDENCY_INSTALL_COMMAND=${options.command}`] : []),
121
+ options.image,
122
+ ];
123
+ }
124
+ function errorText(error) {
125
+ return error instanceof Error ? error.message : String(error);
126
+ }
127
+ function exitCode(error) {
128
+ const code = Number(error?.code);
129
+ return Number.isInteger(code) ? code : null;
130
+ }
131
+ /**
132
+ * Stages and hands out workspace volumes. Every decision runs on one
133
+ * runner-global promise chain over the inventory, so two creates cannot adopt
134
+ * the same volume and a stage cannot race its own bookkeeping. Docker calls
135
+ * run outside the chain, so a slow stage never delays an adoption.
136
+ */
137
+ export class StagedWorkspaces {
138
+ options;
139
+ chain = Promise.resolve();
140
+ /** Volumes this process is staging right now. */
141
+ building = new Set();
142
+ preferences = new Map();
143
+ /** Per repository, a commit asked for while another stage of it was running. */
144
+ wanted = new Map();
145
+ now;
146
+ log;
147
+ constructor(options) {
148
+ this.options = options;
149
+ this.now = options.now ?? Date.now;
150
+ this.log = options.log ?? ((message) => console.log(message));
151
+ }
152
+ exclusive(effect) {
153
+ const run = this.chain.then(effect, effect);
154
+ this.chain = run.then(() => undefined, () => undefined);
155
+ return run;
156
+ }
157
+ /** Whether this process is staging the volume right now; reconciliation leaves it alone. */
158
+ isBuilding(volume) {
159
+ return this.building.has(volume);
160
+ }
161
+ /**
162
+ * Remembers what an environment of the repository says about dependency
163
+ * installs, for later stages of that repository (a prepare request carries
164
+ * no project settings). Only `TILLER_DEPENDENCY_INSTALL` and
165
+ * `TILLER_DEPENDENCY_INSTALL_COMMAND` are kept.
166
+ */
167
+ noteDependencyPreference(repoUrl, envVars) {
168
+ const repository = this.options.canonicalRepositoryUrl(repoUrl);
169
+ if (!repository)
170
+ return;
171
+ const command = envVars.TILLER_DEPENDENCY_INSTALL_COMMAND?.trim();
172
+ this.preferences.set(repository, {
173
+ off: envVars.TILLER_DEPENDENCY_INSTALL === "off",
174
+ ...(command && !/[\0\r\n]/.test(command) ? { command } : {}),
175
+ });
176
+ }
177
+ /**
178
+ * Takes a ready staged volume for this repository: one at the base commit
179
+ * if there is one, otherwise the newest at another commit, which the
180
+ * bootstrap's delta update brings to the base. The inventory entry becomes
181
+ * the slug's workspace before `claim` records it in the slug's fence
182
+ * directory, so a crash in between leaves an orphan for reconciliation,
183
+ * never a volume that two slugs could adopt. `check` runs first and may
184
+ * throw to leave the pool untouched.
185
+ */
186
+ adopt(repoUrl, baseCommit, slug, claim, check = () => { }) {
187
+ const repository = this.options.canonicalRepositoryUrl(repoUrl);
188
+ if (!repository || !baseCommit || !COMMIT.test(baseCommit))
189
+ return Promise.resolve(null);
190
+ return this.exclusive(() => {
191
+ const candidates = Object.entries(this.options.inventory.read()).filter(([volume, entry]) => entry.kind === "staged"
192
+ && entry.state === "ready"
193
+ && entry.preparationVersion === STAGED_WORKSPACE_PREPARATION_VERSION
194
+ && entry.repoUrl === repository
195
+ && !this.building.has(volume));
196
+ if (!candidates.length)
197
+ return null;
198
+ check();
199
+ candidates.sort(([, left], [, right]) => Number(right.commit === baseCommit) - Number(left.commit === baseCommit)
200
+ || Date.parse(right.createdAt) - Date.parse(left.createdAt));
201
+ const [volume] = candidates[0];
202
+ const taken = this.options.inventory.update((entries) => {
203
+ const entry = entries[volume];
204
+ if (entry?.kind !== "staged" || entry.state !== "ready")
205
+ return null;
206
+ entries[volume] = {
207
+ ...entry,
208
+ kind: "workspace",
209
+ slug,
210
+ state: "assigned",
211
+ lastUsedAt: new Date(this.now()).toISOString(),
212
+ };
213
+ return entry;
214
+ });
215
+ if (!taken)
216
+ return null;
217
+ const recorded = claim(volume);
218
+ if (recorded !== volume) {
219
+ // The slug already had a volume. Ours is now an orphan and the next
220
+ // reconciliation removes it.
221
+ return null;
222
+ }
223
+ return {
224
+ volume,
225
+ commit: taken.commit,
226
+ exact: taken.commit === baseCommit,
227
+ ...(taken.deps ? { deps: taken.deps } : {}),
228
+ };
229
+ });
230
+ }
231
+ /**
232
+ * Starts staging a checkout of `commit` from the repository's mirror unless
233
+ * the repository already has a staged volume or the global cap is reached.
234
+ * Returns the decision at once; `done` settles when the stage run ends.
235
+ *
236
+ * With `replace`, a ready volume at another commit does not count: the new
237
+ * one is staged beside it (one over the cap at most, for the length of the
238
+ * run) and the old one is removed once the new one is ready, so the
239
+ * repository is never left without a volume. If a stage of the repository
240
+ * is running, the commit is remembered and staged when that run ends.
241
+ */
242
+ async stage(repoUrl, commit, options = {}) {
243
+ const skip = (reason) => ({
244
+ result: "skipped",
245
+ done: Promise.resolve({ kind: "skipped", reason }),
246
+ });
247
+ const repository = this.options.canonicalRepositoryUrl(repoUrl);
248
+ const mirrorVolume = repository ? this.options.mirrorVolumeName(repository) : null;
249
+ if (!repository || !mirrorVolume || !MIRROR_VOLUME_NAME.test(mirrorVolume))
250
+ return skip("not a GitHub repository");
251
+ if (!COMMIT.test(commit))
252
+ return skip("invalid commit");
253
+ // `docker run -v` would create a missing mirror as an empty volume.
254
+ try {
255
+ await this.options.runDocker(["volume", "inspect", mirrorVolume]);
256
+ }
257
+ catch {
258
+ return skip("no mirror for the repository");
259
+ }
260
+ const id = randomUUID();
261
+ const volume = `${STAGED_WORKSPACE_VOLUME_PREFIX}${id}`;
262
+ const decision = await this.exclusive(() => this.options.inventory.update((entries) => {
263
+ const staged = Object.values(entries).filter((entry) => entry.kind === "staged");
264
+ const mine = staged.filter((entry) => entry.repoUrl === repository);
265
+ if (mine.some((entry) => entry.commit === commit))
266
+ return "present";
267
+ if (mine.length && !options.replace)
268
+ return "present";
269
+ if (mine.some((entry) => entry.state === "building")) {
270
+ this.wanted.set(repository, commit);
271
+ return "present";
272
+ }
273
+ // The volumes this one replaces leave when it is ready.
274
+ if (staged.length - mine.length >= STAGED_WORKSPACE_CAP)
275
+ return "staged workspace cap reached";
276
+ const at = new Date(this.now()).toISOString();
277
+ entries[volume] = {
278
+ kind: "staged",
279
+ repoUrl: repository,
280
+ commit,
281
+ slug: null,
282
+ preparationVersion: STAGED_WORKSPACE_PREPARATION_VERSION,
283
+ state: "building",
284
+ createdAt: at,
285
+ lastUsedAt: at,
286
+ };
287
+ this.building.add(volume);
288
+ return "started";
289
+ }));
290
+ if (decision === "present") {
291
+ return { result: "present", done: Promise.resolve({ kind: "skipped", reason: "already staged" }) };
292
+ }
293
+ if (decision !== "started")
294
+ return skip(decision);
295
+ const refusal = await this.options.diskRefusal?.().catch(() => null);
296
+ if (refusal) {
297
+ await this.exclusive(() => this.options.inventory.update((entries) => {
298
+ if (entries[volume]?.state === "building")
299
+ delete entries[volume];
300
+ }));
301
+ this.building.delete(volume);
302
+ return skip(refusal);
303
+ }
304
+ const done = this.runStage({ id, volume, mirrorVolume, repository, commit });
305
+ return { result: "started", done };
306
+ }
307
+ /** Runs `docker run` and removes the container if it outlives `timeoutMs`. */
308
+ async runBounded(args, containerName, timeoutMs) {
309
+ let timedOut = false;
310
+ const timer = setTimeout(() => {
311
+ timedOut = true;
312
+ void this.options.runDocker(["rm", "-f", containerName]).catch(() => { });
313
+ }, timeoutMs);
314
+ timer.unref?.();
315
+ try {
316
+ await this.options.runDocker(args);
317
+ }
318
+ catch (error) {
319
+ throw timedOut ? new Error("The stage run timed out") : error;
320
+ }
321
+ finally {
322
+ clearTimeout(timer);
323
+ }
324
+ if (timedOut)
325
+ throw new Error("The stage run timed out");
326
+ }
327
+ /**
328
+ * The optional second phase. Exit 0 means installed, 4 nothing to install
329
+ * (or installs are off for the repository), 5 a failed or oversized install
330
+ * that the script cleaned away. Anything else, or a run the runner had to
331
+ * kill, may have left a partial tree, so the volume is discarded.
332
+ */
333
+ async installDependencies(stage) {
334
+ const dependencies = this.options.dependencies;
335
+ if (!dependencies)
336
+ return "skipped";
337
+ const preference = this.preferences.get(stage.repository);
338
+ if (preference?.off)
339
+ return "skipped";
340
+ const timeoutMs = dependencies.timeoutMs ?? WORKSPACE_STAGE_DEPENDENCIES_TIMEOUT_MS;
341
+ const containerName = dockerName(`stage-deps-${stage.id}`);
342
+ const startedAt = this.now();
343
+ try {
344
+ await this.runBounded(buildWorkspaceDependenciesRunArgs({
345
+ image: this.options.image,
346
+ containerName,
347
+ stagedVolume: stage.volume,
348
+ repoUrl: stage.repository,
349
+ commit: stage.commit,
350
+ cacheMounts: dependencies.cacheMounts,
351
+ timeoutMs,
352
+ maxBytes: dependencies.maxBytes,
353
+ command: preference?.command,
354
+ }), containerName, timeoutMs + (dependencies.containerGraceMs ?? DEPENDENCIES_CONTAINER_GRACE_MS));
355
+ this.log(`[runner] installed dependencies into ${stage.volume} in ${this.now() - startedAt} ms`);
356
+ return "ready";
357
+ }
358
+ catch (error) {
359
+ const code = exitCode(error);
360
+ if (code === WORKSPACE_STAGE_DEPENDENCIES_SKIPPED_EXIT_CODE)
361
+ return "skipped";
362
+ if (code === WORKSPACE_STAGE_DEPENDENCIES_FAILED_EXIT_CODE) {
363
+ this.log(`[runner] dependency install for ${stage.volume} failed; keeping the checkout: ${errorText(error)}`);
364
+ return "failed";
365
+ }
366
+ this.log(`[runner] dependency install for ${stage.volume} ended unexpectedly; discarding the volume: ${errorText(error)}`);
367
+ return "discard";
368
+ }
369
+ }
370
+ async runStage(stage) {
371
+ const { volume } = stage;
372
+ const forget = () => this.exclusive(() => this.options.inventory.forget(volume));
373
+ try {
374
+ await this.options.runDocker(["volume", "create", "--label", `${dockerLabel("staged-workspace")}=1`, volume]);
375
+ const startedAt = this.now();
376
+ const containerName = dockerName(`stage-${stage.id}`);
377
+ await this.runBounded(buildWorkspaceStageRunArgs({
378
+ image: this.options.image,
379
+ containerName,
380
+ mirrorVolume: stage.mirrorVolume,
381
+ stagedVolume: volume,
382
+ repoUrl: stage.repository,
383
+ commit: stage.commit,
384
+ }), containerName, this.options.stageTimeoutMs ?? WORKSPACE_STAGE_TIMEOUT_MS);
385
+ const deps = this.options.dependencies ? await this.installDependencies(stage) : undefined;
386
+ if (deps === "discard")
387
+ throw new Error("The dependency run did not finish cleanly");
388
+ // Marking this volume ready retires the repository's other ready
389
+ // volumes in the same step, so an adoption sees one or the other.
390
+ const replaced = await this.exclusive(() => this.options.inventory.update((entries) => {
391
+ const entry = entries[volume];
392
+ if (entry?.kind !== "staged" || entry.state !== "building")
393
+ return null;
394
+ const next = { ...entry, state: "ready", lastUsedAt: new Date(this.now()).toISOString() };
395
+ if (deps)
396
+ next.deps = deps;
397
+ entries[volume] = next;
398
+ const old = Object.keys(entries).filter((name) => name !== volume
399
+ && entries[name].kind === "staged"
400
+ && entries[name].state === "ready"
401
+ && entries[name].repoUrl === stage.repository
402
+ && !this.building.has(name));
403
+ for (const name of old)
404
+ delete entries[name];
405
+ return old;
406
+ }));
407
+ if (!replaced)
408
+ throw new Error("The staged workspace record disappeared while staging");
409
+ for (const old of replaced) {
410
+ this.log(`[runner] staged workspace ${volume} replaces ${old}`);
411
+ await removeDockerVolume(this.options.runDocker, old, this.log, { emptyWith: this.options.image });
412
+ }
413
+ this.log(`[runner] staged workspace ${volume} for ${stage.repository} at ${stage.commit.slice(0, 12)} in ${this.now() - startedAt} ms`
414
+ + (deps ? ` (dependencies: ${deps})` : ""));
415
+ return { kind: "ready", volume, ...(deps ? { deps } : {}) };
416
+ }
417
+ catch (error) {
418
+ const commitMissing = errorText(error) !== "The stage run timed out"
419
+ && exitCode(error) === WORKSPACE_STAGE_COMMIT_MISSING_EXIT_CODE;
420
+ await forget();
421
+ await removeDockerVolume(this.options.runDocker, volume, this.log, { emptyWith: this.options.image });
422
+ if (commitMissing) {
423
+ this.log(`[runner] not staging ${stage.repository}: the mirror does not hold ${stage.commit.slice(0, 12)}`);
424
+ return { kind: "commit-missing", volume };
425
+ }
426
+ this.log(`[runner] staging a workspace for ${stage.repository} failed: ${errorText(error)}`);
427
+ return { kind: "failed", volume, error: errorText(error) };
428
+ }
429
+ finally {
430
+ this.building.delete(volume);
431
+ // One follow-up per run, so a commit the mirror lacks is not retried in a loop.
432
+ const next = this.wanted.get(stage.repository);
433
+ this.wanted.delete(stage.repository);
434
+ if (next && next !== stage.commit) {
435
+ void this.stage(stage.repository, next, { replace: true }).catch((error) => {
436
+ this.log(`[runner] staging a workspace for ${stage.repository} failed: ${errorText(error)}`);
437
+ });
438
+ }
439
+ }
440
+ }
441
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Whether this host has room to keep staged workspace volumes.
3
+ *
4
+ * Staging is the one startup accelerator that holds disk ahead of use: up to
5
+ * the byte budget across Git mirrors and staged volumes. On a small disk, such
6
+ * as a Raspberry Pi's SD card, that would crowd out the environments
7
+ * themselves, so the runner decides from free space on the Docker data volume
8
+ * (where every named volume lives):
9
+ *
10
+ * - At runner start, staging is on only when free space covers the whole
11
+ * budget plus the headroom, so a full budget still leaves the headroom free.
12
+ * The decision is logged once and fixes the `staged-workspaces` feature the
13
+ * runner advertises.
14
+ * - Before each stage run, the run is skipped while free space is below the
15
+ * headroom alone. Staged volumes already count against free space by then,
16
+ * so the start-up threshold would turn staging off as it fills its budget.
17
+ *
18
+ * There is no switch. The Git mirror and the reuse of a workspace volume are
19
+ * small and stay on everywhere.
20
+ */
21
+ /** Free space that staging never eats into: room for environments, images and installs. */
22
+ export const STAGING_DISK_HEADROOM_BYTES = 10 * 1024 * 1024 * 1024;
23
+ const GIB = 1024 * 1024 * 1024;
24
+ function gib(bytes) {
25
+ return `${(bytes / GIB).toFixed(1)} GiB`;
26
+ }
27
+ /** Free space staging needs at runner start: the budget plus the headroom. */
28
+ export function stagingStartupThresholdBytes(budgetBytes) {
29
+ return Math.max(0, budgetBytes) + STAGING_DISK_HEADROOM_BYTES;
30
+ }
31
+ export function decideStagingAtStartup(space, budgetBytes) {
32
+ const needed = stagingStartupThresholdBytes(budgetBytes);
33
+ if (!space) {
34
+ return {
35
+ enabled: false,
36
+ reason: "staged workspaces are off: free space on the Docker data volume could not be measured",
37
+ };
38
+ }
39
+ const size = space.totalBytes ? ` of ${gib(space.totalBytes)}` : "";
40
+ if (space.freeBytes < needed) {
41
+ return {
42
+ enabled: false,
43
+ reason: `staged workspaces are off: ${gib(space.freeBytes)} free${size} on the Docker data volume, below the ${gib(needed)} they need (budget plus headroom)`,
44
+ };
45
+ }
46
+ return {
47
+ enabled: true,
48
+ reason: `staged workspaces are on, with dependencies: ${gib(space.freeBytes)} free${size} on the Docker data volume (needs ${gib(needed)})`,
49
+ };
50
+ }
51
+ /** Before a stage run: null to go ahead, else why it is skipped. */
52
+ export function stageRunDiskRefusal(space) {
53
+ if (!space)
54
+ return "free space on the Docker data volume could not be measured";
55
+ if (space.freeBytes < STAGING_DISK_HEADROOM_BYTES) {
56
+ return `${gib(space.freeBytes)} free on the Docker data volume, below the ${gib(STAGING_DISK_HEADROOM_BYTES)} headroom`;
57
+ }
58
+ return null;
59
+ }
60
+ /** Parses `df -Pk` output for one filesystem (1024-byte blocks). */
61
+ export function parseDfOutput(output) {
62
+ const lines = output.trim().split("\n").filter(Boolean);
63
+ const fields = lines.at(-1)?.trim().split(/\s+/) ?? [];
64
+ // Filesystem, 1024-blocks, Used, Available, Capacity, Mounted on
65
+ if (lines.length < 2 || fields.length < 6)
66
+ return null;
67
+ const total = Number(fields[1]);
68
+ const available = Number(fields[3]);
69
+ if (!Number.isFinite(total) || !Number.isFinite(available) || total <= 0 || available < 0)
70
+ return null;
71
+ return { freeBytes: available * 1024, totalBytes: total * 1024 };
72
+ }
73
+ const PROBE_PATH = "/tiller-disk-probe";
74
+ /**
75
+ * `docker run` arguments that report free space on the Docker data volume.
76
+ * An anonymous volume lives where named volumes live, on Linux and inside
77
+ * Docker Desktop's VM alike, and `--rm` removes it. `--pull never`: a probe
78
+ * never downloads the image.
79
+ */
80
+ export function buildDiskProbeRunArgs(image) {
81
+ return [
82
+ "run", "--rm", "--pull", "never", "--network", "none",
83
+ "--entrypoint", "df",
84
+ "-v", PROBE_PATH,
85
+ image, "-Pk", PROBE_PATH,
86
+ ];
87
+ }
88
+ export function createDockerDiskProbe(image, runDocker) {
89
+ return async () => {
90
+ try {
91
+ return parseDfOutput(await runDocker(buildDiskProbeRunArgs(image)));
92
+ }
93
+ catch {
94
+ return null;
95
+ }
96
+ };
97
+ }