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,685 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { mkdirSync } from "node:fs";
3
+ import { stdout as output } from "node:process";
4
+ import { ansi } from "./ansi.js";
5
+ import { checkHttpHealth } from "./health-check.js";
6
+ import { readHostRuntimeHealth, parseHostRuntimeIdentity } from "./host-runtime-identity.js";
7
+ import { startRunnerServer } from "./runner-server.js";
8
+ import { RunnerSessionController } from "./runner-session.js";
9
+ import { RUNNER_COMMAND_PROTOCOL } from "./runner-command-fence.js";
10
+ import { CODEX_RUNTIME_AUTH_PROTOCOL } from "./codex-runtime-protocol.js";
11
+ import { parseManagedLocalRunnerImageSourceId } from "./managed-runner-image.js";
12
+ import { HostReviewerIsolationAdvertisement } from "./reviewer-isolation-capability.js";
13
+ import { HUB_URL, LOCAL_RUNNER_IMAGE, LOCAL_RUNNER_PORT, LOCAL_STATE_DIR, MACHINE_ID, MACHINE_DISPLAY_NAME, TILLER_CONFIG_DIR, hubControlHeaders, } from "./config.js";
14
+ import { createOsMachine } from "./os-machine.js";
15
+ import { dockerLabel } from "./docker-names.js";
16
+ import { CLI, envVar } from "./identity.js";
17
+ import { buildMachineCapacity, environmentLimit, readDockerResources, readHostResources, runCapacityDocker, } from "./machine-capacity.js";
18
+ const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
19
+ let inProcessRunner = null;
20
+ let runnerRegistration = null;
21
+ let announcedSignature = null;
22
+ let runnerSessionConnectedAt = null;
23
+ let runnerHealthAvailable = false;
24
+ // Docker readiness as of the last health check. Registration reads this
25
+ // instead of running `docker info` (synchronous, up to 5 s) on every flush,
26
+ // which blocked the event loop while the Hub's reply was waiting.
27
+ let dockerAvailableForRegistration = false;
28
+ /** How long the heartbeat's Docker probe may take; it runs off the event loop. */
29
+ const DOCKER_PROBE_TIMEOUT_MS = 10_000;
30
+ /**
31
+ * Whether the Docker daemon answers `docker version`, without blocking the
32
+ * event loop. `docker version` is a cheap API call; `docker info` gathers
33
+ * counts and storage status and slows to seconds while a stage run installs
34
+ * dependencies or a large volume is removed.
35
+ */
36
+ export function probeDocker(timeoutMs = DOCKER_PROBE_TIMEOUT_MS) {
37
+ return new Promise((resolvePromise) => {
38
+ let settled = false;
39
+ const finish = (ok) => {
40
+ if (settled)
41
+ return;
42
+ settled = true;
43
+ clearTimeout(timer);
44
+ resolvePromise(ok);
45
+ };
46
+ const child = spawn("docker", ["version", "--format", "{{.Server.Version}}"], { stdio: "ignore" });
47
+ const timer = setTimeout(() => {
48
+ child.kill("SIGKILL");
49
+ finish(false);
50
+ }, timeoutMs);
51
+ child.on("error", () => finish(false));
52
+ child.on("close", (code) => finish(code === 0));
53
+ });
54
+ }
55
+ /**
56
+ * The heartbeat's view of host health for the Hub advertisement. An
57
+ * advertisement without `dockerAvailable` withdraws the host at once (the Hub
58
+ * shows it as not connected until the next heartbeat, 30 s later), so one
59
+ * failed check while Docker is busy keeps the last answer; the second in a
60
+ * row reports it. A recovery is reported at once.
61
+ */
62
+ export class RegistrationHealth {
63
+ failuresToWithdraw;
64
+ failures = 0;
65
+ constructor(failuresToWithdraw = 2) {
66
+ this.failuresToWithdraw = failuresToWithdraw;
67
+ }
68
+ observe(current, checked) {
69
+ const runnerAvailable = checked.dockerOk && checked.runnerOk;
70
+ if (runnerAvailable) {
71
+ this.failures = 0;
72
+ return { dockerAvailable: true, runnerAvailable: true };
73
+ }
74
+ this.failures += 1;
75
+ if (this.failures < this.failuresToWithdraw && current.runnerAvailable)
76
+ return current;
77
+ return { dockerAvailable: checked.dockerOk, runnerAvailable: false };
78
+ }
79
+ }
80
+ const registrationHealth = new RegistrationHealth();
81
+ // Capacity is reported alongside health and never feeds it. Docker's CPUs and
82
+ // memory change only when Docker restarts, so they are read at runner start
83
+ // and again when `probeDocker` recovers (or until a first read succeeds),
84
+ // off the heartbeat; the environment count is read every heartbeat.
85
+ let dockerResources = null;
86
+ let dockerResourcesRead = null;
87
+ let lastProbeDockerOk = false;
88
+ let machineCapacity = null;
89
+ function refreshDockerResources() {
90
+ dockerResourcesRead ??= readDockerResources(runCapacityDocker)
91
+ .then((resources) => {
92
+ dockerResources = resources;
93
+ })
94
+ .finally(() => {
95
+ dockerResourcesRead = null;
96
+ });
97
+ return dockerResourcesRead;
98
+ }
99
+ function measureCapacity(runningEnvironments) {
100
+ return buildMachineCapacity({
101
+ docker: dockerResources,
102
+ runningEnvironments,
103
+ host: readHostResources(),
104
+ maxEnvironments: process.env[envVar("MAX_ENVIRONMENTS")],
105
+ now: new Date(),
106
+ jobs: inProcessRunner?.gadgetJobLoad() ?? null,
107
+ });
108
+ }
109
+ let reviewerIsolation = null;
110
+ /** Read on every registration flush, so it never runs Docker itself. */
111
+ function reviewerIsolationAdvertisement() {
112
+ const image = LOCAL_RUNNER_IMAGE.trim();
113
+ if (reviewerIsolation?.image !== image) {
114
+ reviewerIsolation = new HostReviewerIsolationAdvertisement(image, parseManagedLocalRunnerImageSourceId(image));
115
+ }
116
+ return reviewerIsolation;
117
+ }
118
+ /** Says which check withdrew the host, so a `not connected` in the Hub can be traced here. */
119
+ export function describeRegistration(signature) {
120
+ try {
121
+ const parsed = JSON.parse(signature);
122
+ const at = new Date().toISOString();
123
+ if (parsed.dockerAvailable === false)
124
+ return `Execution machine withdrawn from hub: Docker did not answer (${at})`;
125
+ if (parsed.runnerAvailable === false)
126
+ return `Execution machine withdrawn from hub: runner health check failed (${at})`;
127
+ return `Execution machine registered with hub (${at})`;
128
+ }
129
+ catch {
130
+ return "Execution machine registered with hub";
131
+ }
132
+ }
133
+ function sleep(ms) {
134
+ return new Promise((resolvePromise) => setTimeout(resolvePromise, ms));
135
+ }
136
+ function localLog(message) {
137
+ process.stderr.write(`${ansi.bold}${CLI.logTag}${ansi.reset} ${message}\n`);
138
+ }
139
+ function localRunnerHealthUrl() {
140
+ return `http://127.0.0.1:${LOCAL_RUNNER_PORT}/healthz`;
141
+ }
142
+ function serviceModeLabel(status) {
143
+ return status.mode === "stopped" ? "local" : status.mode;
144
+ }
145
+ function stackFullyHealthy(status) {
146
+ return status.dockerReady && status.hubReachable && status.runner.healthy;
147
+ }
148
+ async function runAnimatedStep(label, work) {
149
+ const prefix = `${ansi.bold}${CLI.logTag}${ansi.reset} `;
150
+ const render = (icon, text) => {
151
+ process.stderr.write(`\r${ansi.eraseLine}${prefix}${icon} ${text}`);
152
+ };
153
+ if (!process.stderr.isTTY) {
154
+ localLog(`${label}...`);
155
+ try {
156
+ const result = await work();
157
+ localLog(`${ansi.green}✓${ansi.reset} ${label}${result.detail ? ` ${ansi.dim}${result.detail}${ansi.reset}` : ""}`);
158
+ return result.value;
159
+ }
160
+ catch (error) {
161
+ localLog(`${ansi.red}✕${ansi.reset} ${label} ${error instanceof Error ? error.message : String(error)}`);
162
+ throw error;
163
+ }
164
+ }
165
+ let frame = 0;
166
+ render(`${ansi.cyan}${SPINNER_FRAMES[0]}${ansi.reset}`, label);
167
+ const timer = setInterval(() => {
168
+ frame = (frame + 1) % SPINNER_FRAMES.length;
169
+ render(`${ansi.cyan}${SPINNER_FRAMES[frame]}${ansi.reset}`, label);
170
+ }, 80);
171
+ try {
172
+ const result = await work();
173
+ clearInterval(timer);
174
+ render(`${ansi.green}✓${ansi.reset}`, `${label}${result.detail ? ` ${ansi.dim}${result.detail}${ansi.reset}` : ""}`);
175
+ process.stderr.write("\n");
176
+ return result.value;
177
+ }
178
+ catch (error) {
179
+ clearInterval(timer);
180
+ render(`${ansi.red}✕${ansi.reset}`, `${label} ${ansi.dim}${error instanceof Error ? error.message : String(error)}${ansi.reset}`);
181
+ process.stderr.write("\n");
182
+ throw error;
183
+ }
184
+ }
185
+ function commandReady(command, args) {
186
+ const result = spawnSync(command, args, {
187
+ stdio: "ignore",
188
+ timeout: 5000,
189
+ });
190
+ return result.status === 0;
191
+ }
192
+ async function checkHealth(url, headers) {
193
+ return checkHttpHealth(url, headers);
194
+ }
195
+ async function runDocker(args) {
196
+ return new Promise((resolvePromise, reject) => {
197
+ const child = spawn("docker", args, {
198
+ stdio: ["ignore", "pipe", "pipe"],
199
+ });
200
+ let stdout = "";
201
+ let stderr = "";
202
+ child.stdout.on("data", (chunk) => {
203
+ stdout += chunk.toString();
204
+ });
205
+ child.stderr.on("data", (chunk) => {
206
+ stderr += chunk.toString();
207
+ });
208
+ child.on("error", reject);
209
+ child.on("close", (code) => {
210
+ if (code === 0) {
211
+ resolvePromise(stdout.trim());
212
+ return;
213
+ }
214
+ reject(new Error(stderr.trim() || stdout.trim() || `docker ${args.join(" ")} failed`));
215
+ });
216
+ });
217
+ }
218
+ async function runDockerInherited(args) {
219
+ return new Promise((resolvePromise, reject) => {
220
+ const child = spawn("docker", args, {
221
+ stdio: "inherit",
222
+ });
223
+ child.on("error", reject);
224
+ child.on("close", (code) => {
225
+ if (code === 0) {
226
+ resolvePromise();
227
+ return;
228
+ }
229
+ reject(new Error(`docker ${args.join(" ")} failed with exit code ${code ?? "unknown"}`));
230
+ });
231
+ });
232
+ }
233
+ export function dockerInstalled() {
234
+ return commandReady("docker", ["--version"]);
235
+ }
236
+ export function dockerReady() {
237
+ return commandReady("docker", ["info"]);
238
+ }
239
+ async function ensureDocker() {
240
+ if (dockerReady())
241
+ return false;
242
+ if (process.platform !== "darwin") {
243
+ throw new Error("Docker is not running");
244
+ }
245
+ const resolveApp = spawnSync("open", ["-Ra", "Docker"], {
246
+ stdio: "ignore",
247
+ timeout: 5000,
248
+ });
249
+ if (resolveApp.status !== 0) {
250
+ throw new Error("Docker Desktop is not installed");
251
+ }
252
+ const openApp = spawnSync("open", ["-ga", "Docker"], {
253
+ stdio: "ignore",
254
+ timeout: 5000,
255
+ });
256
+ if (openApp.status !== 0) {
257
+ throw new Error("Failed to launch Docker Desktop");
258
+ }
259
+ const deadline = Date.now() + 60_000;
260
+ while (Date.now() < deadline) {
261
+ if (dockerReady())
262
+ return true;
263
+ await sleep(1_000);
264
+ }
265
+ throw new Error("Docker Desktop did not become ready");
266
+ }
267
+ export function localImageExists(image = LOCAL_RUNNER_IMAGE) {
268
+ return commandReady("docker", ["image", "inspect", image]);
269
+ }
270
+ async function ensureLocalRunnerImage() {
271
+ if (localImageExists()) {
272
+ return;
273
+ }
274
+ localLog(`Pulling sandbox image ${ansi.cyan}${LOCAL_RUNNER_IMAGE}${ansi.reset}...`);
275
+ if (output.isTTY) {
276
+ await runDockerInherited(["pull", LOCAL_RUNNER_IMAGE]);
277
+ }
278
+ else {
279
+ await runDocker(["pull", LOCAL_RUNNER_IMAGE]);
280
+ }
281
+ localLog(`${ansi.green}Sandbox image is ready.${ansi.reset}`);
282
+ }
283
+ export async function prepareLocalRunnerImage() {
284
+ await ensureLocalRunnerImage();
285
+ }
286
+ export async function pullLocalRunnerImage(image) {
287
+ localLog(`Pulling sandbox image ${ansi.cyan}${image}${ansi.reset}...`);
288
+ if (output.isTTY) {
289
+ await runDockerInherited(["pull", image]);
290
+ }
291
+ else {
292
+ await runDocker(["pull", image]);
293
+ }
294
+ localLog(`${ansi.green}Sandbox image is ready.${ansi.reset}`);
295
+ }
296
+ async function listManagedEnvContainers() {
297
+ const outputText = await runDocker(["ps", "-aq", "--filter", `label=${dockerLabel("slug")}`]);
298
+ return outputText.split(/\r?\n/).map((value) => value.trim()).filter(Boolean);
299
+ }
300
+ export async function removeManagedEnvContainers(containerIds) {
301
+ const ids = containerIds ?? await listManagedEnvContainers();
302
+ if (ids.length === 0) {
303
+ return 0;
304
+ }
305
+ await runDocker(["rm", "-f", ...ids]);
306
+ return ids.length;
307
+ }
308
+ export function runnerSessionSignature(machineId, localRunnerImage, localRunnerImageSourceId, runnerCommandProtocol, codexRuntimeAuthProtocol, reviewerIsolationProtocol, displayName = machineId, dockerAvailable = true, runnerAvailable = true, projectEnvironmentProtocol = null, portableWorkspaceRestoreProtocol = null, portableNativeRestoreProtocol = null, runtimeIdentity) {
309
+ // `runnerFeatures` is left out: every runner now names features, and a Hub
310
+ // from before them drops the field, which would leave registration
311
+ // unverified for good. A Hub that does not keep them only never asks the
312
+ // runner to prepare a project. They are fixed for the life of the runner
313
+ // process, whose start the runtime identity already names.
314
+ return JSON.stringify({
315
+ machineId,
316
+ displayName,
317
+ dockerAvailable,
318
+ runnerAvailable,
319
+ ...(runnerCommandProtocol === RUNNER_COMMAND_PROTOCOL
320
+ ? { runnerCommandProtocol: RUNNER_COMMAND_PROTOCOL }
321
+ : {}),
322
+ ...(codexRuntimeAuthProtocol === CODEX_RUNTIME_AUTH_PROTOCOL
323
+ ? { codexRuntimeAuthProtocol: CODEX_RUNTIME_AUTH_PROTOCOL }
324
+ : {}),
325
+ ...(reviewerIsolationProtocol === 1 ? { reviewerIsolationProtocol: 1 } : {}),
326
+ ...(projectEnvironmentProtocol === 1 ? { projectEnvironmentProtocol: 1 } : {}),
327
+ ...(portableWorkspaceRestoreProtocol === 1 ? { portableWorkspaceRestoreProtocol: 1 } : {}),
328
+ ...(portableNativeRestoreProtocol === 1 ? { portableNativeRestoreProtocol: 1 } : {}),
329
+ ...(runtimeIdentity ? { runtimeIdentity } : {}),
330
+ claudeSubscription: false,
331
+ localRunnerImage,
332
+ ...(localRunnerImageSourceId ? { localRunnerImageSourceId } : {}),
333
+ transport: "session",
334
+ });
335
+ }
336
+ export function parseRunnerSessionSignature(machine) {
337
+ try {
338
+ const state = JSON.parse(machine.runner_state);
339
+ if (!state.host?.machineId)
340
+ return null;
341
+ return runnerSessionSignature(state.host.machineId, state.host.localRunnerImage ?? "", state.host.localRunnerImageSourceId ?? null, state.host.runnerCommandProtocol === RUNNER_COMMAND_PROTOCOL
342
+ ? RUNNER_COMMAND_PROTOCOL
343
+ : null, state.host.codexRuntimeAuthProtocol === CODEX_RUNTIME_AUTH_PROTOCOL
344
+ ? CODEX_RUNTIME_AUTH_PROTOCOL
345
+ : null, state.host.reviewerIsolationProtocol === 1 ? 1 : null, state.host.displayName ?? state.host.machineId, state.host.dockerAvailable === true, state.host.runnerAvailable === true, state.host.projectEnvironmentProtocol === 1 ? 1 : null, state.host.portableWorkspaceRestoreProtocol === 1 ? 1 : null, state.host.portableNativeRestoreProtocol === 1 ? 1 : null, parseHostRuntimeIdentity(state.host.runtimeIdentity));
346
+ }
347
+ catch {
348
+ return null;
349
+ }
350
+ }
351
+ function buildRunnerRegistration() {
352
+ if (!runnerSessionConnectedAt) {
353
+ runnerSessionConnectedAt = new Date().toISOString();
354
+ }
355
+ const localRunnerImage = LOCAL_RUNNER_IMAGE.trim();
356
+ const localRunnerImageSourceId = parseManagedLocalRunnerImageSourceId(localRunnerImage);
357
+ const reviewerIsolationProtocol = reviewerIsolationAdvertisement().current();
358
+ const dockerAvailable = dockerAvailableForRegistration;
359
+ const runnerAvailable = dockerAvailable && inProcessRunner !== null && runnerHealthAvailable;
360
+ return {
361
+ host: {
362
+ machineId: MACHINE_ID,
363
+ displayName: MACHINE_DISPLAY_NAME,
364
+ connectedAt: runnerSessionConnectedAt,
365
+ dockerAvailable,
366
+ runnerAvailable,
367
+ ...(runnerAvailable && inProcessRunner ? { runtimeIdentity: inProcessRunner.identity } : {}),
368
+ runnerCommandProtocol: RUNNER_COMMAND_PROTOCOL,
369
+ codexRuntimeAuthProtocol: CODEX_RUNTIME_AUTH_PROTOCOL,
370
+ ...(reviewerIsolationProtocol ? { reviewerIsolationProtocol } : {}),
371
+ ...(runnerAvailable && inProcessRunner?.capabilities.projectEnvironmentProtocol === 1 ? { projectEnvironmentProtocol: 1 } : {}),
372
+ ...(runnerAvailable && inProcessRunner?.capabilities.portableWorkspaceRestoreProtocol === 1 ? { portableWorkspaceRestoreProtocol: 1 } : {}),
373
+ ...(runnerAvailable && inProcessRunner?.capabilities.portableNativeRestoreProtocol === 1 ? { portableNativeRestoreProtocol: 1 } : {}),
374
+ ...(runnerAvailable && inProcessRunner?.capabilities.features?.length
375
+ ? { runnerFeatures: [...inProcessRunner.capabilities.features] }
376
+ : {}),
377
+ claudeSubscription: false,
378
+ localRunnerImage,
379
+ ...(localRunnerImageSourceId ? { localRunnerImageSourceId } : {}),
380
+ ...(runnerAvailable && machineCapacity ? { capacity: machineCapacity } : {}),
381
+ transport: "session",
382
+ },
383
+ };
384
+ }
385
+ async function ensureRegistrationClient() {
386
+ if (!HUB_URL)
387
+ return;
388
+ runnerRegistration ??= new RunnerSessionController({
389
+ hubUrl: HUB_URL,
390
+ cfAccessHeaders: hubControlHeaders,
391
+ machineId: MACHINE_ID,
392
+ runnerPort: LOCAL_RUNNER_PORT,
393
+ runnerApiToken: () => inProcessRunner?.apiToken,
394
+ buildState: buildRunnerRegistration,
395
+ refreshState: async () => {
396
+ // Both checks are asynchronous: a synchronous `docker info` blocked the
397
+ // runner's event loop for up to 5 s per heartbeat, and its timeout
398
+ // withdrew the host whenever Docker was busy.
399
+ const [health, dockerOk, , runningEnvironments] = await Promise.all([
400
+ checkLocalRunnerHealth(),
401
+ probeDocker(),
402
+ reviewerIsolationAdvertisement().refresh(),
403
+ inProcessRunner?.countRunningEnvironments() ?? Promise.resolve(null),
404
+ ]);
405
+ const next = registrationHealth.observe({ dockerAvailable: dockerAvailableForRegistration, runnerAvailable: runnerHealthAvailable }, { dockerOk, runnerOk: inProcessRunner !== null && health.ok });
406
+ dockerAvailableForRegistration = next.dockerAvailable;
407
+ runnerHealthAvailable = inProcessRunner !== null && next.runnerAvailable;
408
+ if (dockerOk && (!lastProbeDockerOk || !dockerResources)) {
409
+ if (!lastProbeDockerOk)
410
+ dockerResources = null;
411
+ void refreshDockerResources();
412
+ }
413
+ lastProbeDockerOk = dockerOk;
414
+ machineCapacity = dockerOk ? measureCapacity(runningEnvironments) : null;
415
+ },
416
+ getStateSignature: (state) => runnerSessionSignature(state.host.machineId, state.host.localRunnerImage ?? "", state.host.localRunnerImageSourceId ?? null, state.host.runnerCommandProtocol === RUNNER_COMMAND_PROTOCOL
417
+ ? RUNNER_COMMAND_PROTOCOL
418
+ : null, state.host.codexRuntimeAuthProtocol === CODEX_RUNTIME_AUTH_PROTOCOL
419
+ ? CODEX_RUNTIME_AUTH_PROTOCOL
420
+ : null, state.host.reviewerIsolationProtocol === 1 ? 1 : null, state.host.displayName, state.host.dockerAvailable, state.host.runnerAvailable, state.host.projectEnvironmentProtocol === 1 ? 1 : null, state.host.portableWorkspaceRestoreProtocol === 1 ? 1 : null, state.host.portableNativeRestoreProtocol === 1 ? 1 : null, state.host.runtimeIdentity),
421
+ getMachineSignature: parseRunnerSessionSignature,
422
+ onRegistered: (signature) => {
423
+ if (announcedSignature !== signature) {
424
+ announcedSignature = signature;
425
+ localLog(describeRegistration(signature));
426
+ }
427
+ },
428
+ onLog: (message) => localLog(`${ansi.yellow}${message}${ansi.reset}`),
429
+ // Deploy, pull and preview on Overvues, asked for by the Hub over this connection.
430
+ os: createOsMachine({
431
+ configDir: TILLER_CONFIG_DIR,
432
+ machineId: MACHINE_ID,
433
+ hubUrl: HUB_URL,
434
+ hubHeaders: () => hubControlHeaders,
435
+ localStateDir: LOCAL_STATE_DIR,
436
+ log: localLog,
437
+ }),
438
+ });
439
+ await runnerRegistration.ensureConnected();
440
+ }
441
+ function clearRunnerRegistration() {
442
+ runnerRegistration?.close();
443
+ runnerRegistration = null;
444
+ announcedSignature = null;
445
+ runnerSessionConnectedAt = null;
446
+ machineCapacity = null;
447
+ }
448
+ async function syncRunnerRegistration(timeoutMs = 15_000) {
449
+ if (!HUB_URL) {
450
+ return;
451
+ }
452
+ await ensureRegistrationClient();
453
+ await runnerRegistration?.sync(timeoutMs);
454
+ }
455
+ async function trySyncRunnerRegistration(timeoutMs = 1_500) {
456
+ try {
457
+ await syncRunnerRegistration(timeoutMs);
458
+ return true;
459
+ }
460
+ catch (error) {
461
+ localLog(`${ansi.yellow}Runner registration unavailable: ${error instanceof Error ? error.message : String(error)}${ansi.reset}`);
462
+ return false;
463
+ }
464
+ }
465
+ function ensureStateDir() {
466
+ mkdirSync(LOCAL_STATE_DIR, { recursive: true });
467
+ }
468
+ async function stopManagedService() {
469
+ if (inProcessRunner) {
470
+ runnerHealthAvailable = false;
471
+ clearRunnerRegistration();
472
+ await inProcessRunner.close();
473
+ inProcessRunner = null;
474
+ return true;
475
+ }
476
+ runnerHealthAvailable = false;
477
+ clearRunnerRegistration();
478
+ return false;
479
+ }
480
+ export async function checkLocalRunnerHealth() {
481
+ return readHostRuntimeHealth(localRunnerHealthUrl());
482
+ }
483
+ export async function checkHubHealth() {
484
+ if (!HUB_URL) {
485
+ return { ok: false, detail: "Hub URL not configured" };
486
+ }
487
+ return checkHealth(`${HUB_URL}/api/setup/status`, hubControlHeaders);
488
+ }
489
+ async function getRunnerStatus() {
490
+ const health = await checkLocalRunnerHealth();
491
+ if (inProcessRunner) {
492
+ return {
493
+ name: "runner",
494
+ label: "runner",
495
+ mode: "managed",
496
+ running: true,
497
+ healthy: health.ok,
498
+ detail: health.detail ?? (health.ok ? undefined : "unhealthy"),
499
+ };
500
+ }
501
+ if (health.ok) {
502
+ return {
503
+ name: "runner",
504
+ label: "runner",
505
+ mode: "external",
506
+ running: true,
507
+ healthy: true,
508
+ detail: health.detail,
509
+ };
510
+ }
511
+ return {
512
+ name: "runner",
513
+ label: "runner",
514
+ mode: "stopped",
515
+ running: false,
516
+ healthy: false,
517
+ detail: health.detail,
518
+ };
519
+ }
520
+ export async function getLocalStackStatus() {
521
+ const [runner, hubReachable] = await Promise.all([
522
+ getRunnerStatus(),
523
+ checkHubHealth().then((result) => result.ok).catch(() => false),
524
+ ]);
525
+ return {
526
+ dockerReady: dockerReady(),
527
+ hubReachable,
528
+ runner,
529
+ };
530
+ }
531
+ function serviceIcon(status) {
532
+ if (status.healthy)
533
+ return `${ansi.green}ok${ansi.reset}`;
534
+ if (status.running)
535
+ return `${ansi.yellow}degraded${ansi.reset}`;
536
+ return `${ansi.dim}down${ansi.reset}`;
537
+ }
538
+ function formatServiceStatus(status) {
539
+ const mode = status.mode === "stopped" ? "managed:none" : status.mode;
540
+ const detail = status.detail ? ` ${ansi.dim}${status.detail}${ansi.reset}` : "";
541
+ return ` ${serviceIcon(status)} ${status.label} ${ansi.dim}[${mode}]${ansi.reset}${detail}`;
542
+ }
543
+ function printStatus(status, options = {}) {
544
+ const dockerLabel = status.dockerReady ? `${ansi.green}ok${ansi.reset}` : `${ansi.red}down${ansi.reset}`;
545
+ const hubLabel = status.hubReachable ? `${ansi.green}ok${ansi.reset}` : `${ansi.red}down${ansi.reset}`;
546
+ if (options.compact && stackFullyHealthy(status)) {
547
+ localLog(`stack ready ${ansi.dim}[hub ok, docker ok, runner ${serviceModeLabel(status.runner)}, direct session]${ansi.reset}`);
548
+ return;
549
+ }
550
+ localLog(`hub ${hubLabel} ${ansi.dim}${HUB_URL}${ansi.reset}`);
551
+ localLog(`docker ${dockerLabel}`);
552
+ process.stderr.write(`${formatServiceStatus(status.runner)}\n`);
553
+ process.stderr.write(`${ansi.dim}state dir: ${LOCAL_STATE_DIR}${ansi.reset}\n`);
554
+ process.stderr.write(`${ansi.dim}host transport: outbound hub session${ansi.reset}\n`);
555
+ }
556
+ async function ensureRunner(status, dockerOk) {
557
+ if (inProcessRunner)
558
+ return false;
559
+ if (status.mode === "external") {
560
+ if (!status.healthy) {
561
+ throw new Error(`Runner is externally managed but unhealthy: ${status.detail ?? "unknown error"}`);
562
+ }
563
+ return false;
564
+ }
565
+ if (status.healthy && status.mode === "managed")
566
+ return false;
567
+ if (!dockerOk) {
568
+ throw new Error("Docker is not running");
569
+ }
570
+ await ensureLocalRunnerImage();
571
+ inProcessRunner = await startRunnerServer({
572
+ port: LOCAL_RUNNER_PORT,
573
+ image: LOCAL_RUNNER_IMAGE,
574
+ ...(terminalLaneConfig() ? { terminalLane: terminalLaneConfig() } : {}),
575
+ // The same limit the capacity report sends; unknown until Docker's memory is read.
576
+ environmentLimit: () => dockerResources
577
+ ? environmentLimit(dockerResources.memoryBytes, process.env[envVar("MAX_ENVIRONMENTS")])
578
+ : null,
579
+ });
580
+ return true;
581
+ }
582
+ async function bringUpLocalStack(options = {}) {
583
+ ensureStateDir();
584
+ const before = await getLocalStackStatus();
585
+ if (before.runner.mode === "external") {
586
+ throw new Error(`A runner is already listening on port ${LOCAL_RUNNER_PORT}. Stop the other execution-machine process before starting this one; its runtime protocols and image cannot be verified by this supervisor.`);
587
+ }
588
+ if (options.announce) {
589
+ localLog(`${ansi.bold}Checking Your machine${ansi.reset}`);
590
+ }
591
+ const started = [];
592
+ const failures = [];
593
+ let dockerOk = before.dockerReady;
594
+ if (!dockerOk) {
595
+ try {
596
+ dockerOk = options.announce
597
+ ? await runAnimatedStep("docker", async () => {
598
+ const startedDocker = await ensureDocker();
599
+ return {
600
+ value: true,
601
+ detail: startedDocker ? "Docker Desktop ready" : "ok",
602
+ };
603
+ })
604
+ : await (async () => {
605
+ await ensureDocker();
606
+ return true;
607
+ })();
608
+ }
609
+ catch (error) {
610
+ failures.push(`docker: ${error instanceof Error ? error.message : String(error)}`);
611
+ }
612
+ }
613
+ if (dockerOk || before.runner.healthy) {
614
+ try {
615
+ if (!before.runner.healthy && dockerOk && !localImageExists()) {
616
+ await ensureLocalRunnerImage();
617
+ }
618
+ const runnerStarted = options.announce
619
+ ? await runAnimatedStep("runner", async () => {
620
+ if (before.runner.healthy) {
621
+ return { value: false, detail: `ok (${serviceModeLabel(before.runner)})` };
622
+ }
623
+ const startedRunner = await ensureRunner(before.runner, dockerOk);
624
+ const current = await getRunnerStatus();
625
+ return { value: startedRunner, detail: `ready (${serviceModeLabel(current)})` };
626
+ })
627
+ : await ensureRunner(before.runner, dockerOk);
628
+ if (runnerStarted) {
629
+ started.push("runner");
630
+ }
631
+ }
632
+ catch (error) {
633
+ failures.push(`runner: ${error instanceof Error ? error.message : String(error)}`);
634
+ }
635
+ }
636
+ const finalStatus = await getLocalStackStatus();
637
+ dockerAvailableForRegistration = finalStatus.dockerReady;
638
+ runnerHealthAvailable = (inProcessRunner !== null
639
+ && finalStatus.dockerReady
640
+ && finalStatus.runner.healthy);
641
+ if (finalStatus.runner.healthy) {
642
+ if (finalStatus.dockerReady && inProcessRunner) {
643
+ const [, runningEnvironments] = await Promise.all([
644
+ refreshDockerResources(),
645
+ inProcessRunner.countRunningEnvironments(),
646
+ ]);
647
+ lastProbeDockerOk = true;
648
+ machineCapacity = measureCapacity(runningEnvironments);
649
+ }
650
+ await reviewerIsolationAdvertisement().refresh();
651
+ await trySyncRunnerRegistration();
652
+ }
653
+ if (options.printSummary) {
654
+ printStatus(finalStatus, { compact: options.announce });
655
+ }
656
+ if (failures.length > 0) {
657
+ throw new Error(failures.join(" | "));
658
+ }
659
+ return {
660
+ started,
661
+ status: finalStatus,
662
+ };
663
+ }
664
+ export async function startHostRunner() {
665
+ const result = await bringUpLocalStack({
666
+ announce: true,
667
+ printSummary: true,
668
+ });
669
+ return result.status;
670
+ }
671
+ export async function stopHostRunner() {
672
+ return stopManagedService();
673
+ }
674
+ /** The lane needs the exact Hub origin for its Origin check; without a usable Hub URL there is no lane. */
675
+ function terminalLaneConfig() {
676
+ if (!HUB_URL || !MACHINE_ID)
677
+ return null;
678
+ try {
679
+ const origin = new URL(HUB_URL).origin;
680
+ return origin.startsWith("https://") || origin.startsWith("http://") ? { hubOrigin: origin, machineId: MACHINE_ID } : null;
681
+ }
682
+ catch {
683
+ return null;
684
+ }
685
+ }