@boxes-dev/dvb 1.0.42 → 1.0.44

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 (87) hide show
  1. package/dist/bin/dvb.cjs +4160 -3307
  2. package/dist/bin/dvb.cjs.map +1 -1
  3. package/dist/bin/dvbd.cjs +5 -5
  4. package/dist/devbox/cli.d.ts.map +1 -1
  5. package/dist/devbox/cli.js +2 -2
  6. package/dist/devbox/cli.js.map +1 -1
  7. package/dist/devbox/commands/agent.d.ts.map +1 -1
  8. package/dist/devbox/commands/agent.js +5 -1
  9. package/dist/devbox/commands/agent.js.map +1 -1
  10. package/dist/devbox/commands/boxSelect.d.ts.map +1 -1
  11. package/dist/devbox/commands/boxSelect.js +7 -7
  12. package/dist/devbox/commands/boxSelect.js.map +1 -1
  13. package/dist/devbox/commands/connect.d.ts.map +1 -1
  14. package/dist/devbox/commands/connect.js +6 -5
  15. package/dist/devbox/commands/connect.js.map +1 -1
  16. package/dist/devbox/commands/destroy.d.ts.map +1 -1
  17. package/dist/devbox/commands/destroy.js +4 -1
  18. package/dist/devbox/commands/destroy.js.map +1 -1
  19. package/dist/devbox/commands/init/args.d.ts +0 -2
  20. package/dist/devbox/commands/init/args.d.ts.map +1 -1
  21. package/dist/devbox/commands/init/args.js +0 -12
  22. package/dist/devbox/commands/init/args.js.map +1 -1
  23. package/dist/devbox/commands/init/codex/artifacts.d.ts +25 -2
  24. package/dist/devbox/commands/init/codex/artifacts.d.ts.map +1 -1
  25. package/dist/devbox/commands/init/codex/artifacts.js +134 -1
  26. package/dist/devbox/commands/init/codex/artifacts.js.map +1 -1
  27. package/dist/devbox/commands/init/codex/index.d.ts +3 -1
  28. package/dist/devbox/commands/init/codex/index.d.ts.map +1 -1
  29. package/dist/devbox/commands/init/codex/index.js +149 -11
  30. package/dist/devbox/commands/init/codex/index.js.map +1 -1
  31. package/dist/devbox/commands/init/codex/local.d.ts +16 -8
  32. package/dist/devbox/commands/init/codex/local.d.ts.map +1 -1
  33. package/dist/devbox/commands/init/codex/local.js +79 -19
  34. package/dist/devbox/commands/init/codex/local.js.map +1 -1
  35. package/dist/devbox/commands/init/finalizeFlow.d.ts +56 -0
  36. package/dist/devbox/commands/init/finalizeFlow.d.ts.map +1 -0
  37. package/dist/devbox/commands/init/finalizeFlow.js +601 -0
  38. package/dist/devbox/commands/init/finalizeFlow.js.map +1 -0
  39. package/dist/devbox/commands/init/index.d.ts.map +1 -1
  40. package/dist/devbox/commands/init/index.js +154 -2006
  41. package/dist/devbox/commands/init/index.js.map +1 -1
  42. package/dist/devbox/commands/init/provisionFlow.d.ts +34 -0
  43. package/dist/devbox/commands/init/provisionFlow.d.ts.map +1 -0
  44. package/dist/devbox/commands/init/provisionFlow.js +319 -0
  45. package/dist/devbox/commands/init/provisionFlow.js.map +1 -0
  46. package/dist/devbox/commands/init/session.d.ts +56 -0
  47. package/dist/devbox/commands/init/session.d.ts.map +1 -0
  48. package/dist/devbox/commands/init/session.js +150 -0
  49. package/dist/devbox/commands/init/session.js.map +1 -0
  50. package/dist/devbox/commands/init/setupArtifactsValidation.d.ts +28 -0
  51. package/dist/devbox/commands/init/setupArtifactsValidation.d.ts.map +1 -0
  52. package/dist/devbox/commands/init/setupArtifactsValidation.js +113 -0
  53. package/dist/devbox/commands/init/setupArtifactsValidation.js.map +1 -0
  54. package/dist/devbox/commands/init/setupPlanFlow.d.ts +28 -0
  55. package/dist/devbox/commands/init/setupPlanFlow.d.ts.map +1 -0
  56. package/dist/devbox/commands/init/setupPlanFlow.js +840 -0
  57. package/dist/devbox/commands/init/setupPlanFlow.js.map +1 -0
  58. package/dist/devbox/commands/init/statusFlow.d.ts +26 -0
  59. package/dist/devbox/commands/init/statusFlow.d.ts.map +1 -0
  60. package/dist/devbox/commands/init/statusFlow.js +152 -0
  61. package/dist/devbox/commands/init/statusFlow.js.map +1 -0
  62. package/dist/devbox/commands/list.js +1 -1
  63. package/dist/devbox/commands/list.js.map +1 -1
  64. package/dist/devbox/commands/mount.d.ts.map +1 -1
  65. package/dist/devbox/commands/mount.js +5 -1
  66. package/dist/devbox/commands/mount.js.map +1 -1
  67. package/dist/devbox/commands/mountSsh.js +2 -2
  68. package/dist/devbox/commands/mountSsh.js.map +1 -1
  69. package/dist/devbox/commands/ports.d.ts.map +1 -1
  70. package/dist/devbox/commands/ports.js +9 -2
  71. package/dist/devbox/commands/ports.js.map +1 -1
  72. package/dist/devbox/commands/services.d.ts.map +1 -1
  73. package/dist/devbox/commands/services.js +5 -1
  74. package/dist/devbox/commands/services.js.map +1 -1
  75. package/dist/devbox/commands/sessions.d.ts.map +1 -1
  76. package/dist/devbox/commands/sessions.js +12 -3
  77. package/dist/devbox/commands/sessions.js.map +1 -1
  78. package/dist/devbox/commands/wezterm.d.ts.map +1 -1
  79. package/dist/devbox/commands/wezterm.js +5 -1
  80. package/dist/devbox/commands/wezterm.js.map +1 -1
  81. package/dist/devbox/completions/index.d.ts.map +1 -1
  82. package/dist/devbox/completions/index.js +0 -1
  83. package/dist/devbox/completions/index.js.map +1 -1
  84. package/dist/prompts/local-scan-env-secrets.md +2 -0
  85. package/dist/prompts/local-scan-external.md +2 -0
  86. package/dist/prompts/local-scan-extra-artifacts.md +2 -0
  87. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import path from "node:path";
2
2
  import fs from "node:fs/promises";
3
3
  import os from "node:os";
4
- import { cancel as clackCancel, confirm as clackConfirm, isCancel, log as clackLog, note as clackNote, select as clackSelect, taskLog as clackTaskLog, } from "@clack/prompts";
4
+ import { cancel as clackCancel, confirm as clackConfirm, isCancel, taskLog as clackTaskLog, } from "@clack/prompts";
5
5
  import { resolveSocketInfo } from "@boxes-dev/core";
6
6
  import { createSecretStore, loadConfig, resolveCodexAuthMode, createSpritesClient, normalizeGitRemoteUrl, resolveSpritesApiUrl, resolveDevboxProjectDir, SpritesApiError, slugify, } from "@boxes-dev/core";
7
7
  import { DAEMON_TIMEOUT_MS, ensureDaemonRunning, requestJson, requireDaemonFeatures, } from "../../daemonClient.js";
@@ -9,19 +9,20 @@ import { ensureSpritesToken } from "../../auth.js";
9
9
  import { fetchSpriteDaemonRelease, getConvexUrl, issueSpriteDaemonToken, } from "../../controlPlane.js";
10
10
  import { logger } from "../../logger.js";
11
11
  import { parseInitArgs } from "./args.js";
12
- import { confirmCopyWorktree, findRepoRoot, ensureRepoProjectId, mapGlobalGitConfigDestinations, readGlobalGitConfigFiles, readHeadState, readRepoOrigin, readWorktreeState, resolveGitCommonDir, runCommand, } from "./repo.js";
13
- import { createFileListArchive, createGitMetaArchive, writePatch, } from "./packaging.js";
14
- import { bootstrapDevbox, ensureSpriteDaemonService, expandHome, ensureWeztermMuxService, installSpriteDaemon, shellQuote, stageRemoteSetupArtifacts, } from "./remote.js";
12
+ import { runInitStatusFlow } from "./statusFlow.js";
13
+ import { createCodexCheckpointRecorder, createInitStateUpdater, prepareInitSessionState, } from "./session.js";
14
+ import { findRepoRoot, ensureRepoProjectId, readRepoOrigin } from "./repo.js";
15
+ import { bootstrapDevbox, ensureSpriteDaemonService, expandHome, ensureWeztermMuxService, installSpriteDaemon, shellQuote, } from "./remote.js";
15
16
  import { ensureWeztermMuxInstalled } from "../../../wezterm/ensureMux.js";
16
- import { readRepoMarker, writeRepoMarker } from "./registry.js";
17
- import { showCopyableUrl } from "../../ui/copyableUrl.js";
18
- import { INIT_STEP_KEYS, readInitState, writeInitState, } from "./state.js";
19
- import { ensureSshConfig, ensureSshKey, copyToClipboard, openBrowser, parseGitRemote, readRemoteOrigin, setRemoteOrigin, verifySshAuth, } from "./ssh.js";
17
+ import { readRepoMarker } from "./registry.js";
18
+ import { readInitState, writeInitState } from "./state.js";
20
19
  import { ensureKnownHostsFile, ensureLocalMountKey, ensureRemoteMountAccess, ensureSshdService, readLocalMountPublicKey, } from "../mountSsh.js";
21
- import { createSetupArtifacts, promptForPlanApproval, promptForServicesApproval, readSetupPlan, readSetupEnvSecretsPlan, readSetupExternalPlan, readSetupExtraArtifactsPlan, readServicesPlan, runLocalServicesScan, runLocalSetupEnvSecretsScan, runLocalSetupExternalScan, runLocalSetupExtraArtifactsScan, runRemoteCodexSetup, ensureRemoteCodexInstalled, uploadSetupPlan, mergeSetupScans, writeSetupPlan, writeSetupEnvSecretsSchema, writeSetupExternalSchema, writeSetupExtraArtifactsSchema, writeServicesSchema, } from "./codex/index.js";
22
- import { mergeServicesToml, splitShellCommand, } from "../servicesToml.js";
20
+ import { ensureRemoteCodexInstalled } from "./codex/index.js";
23
21
  import { checkSpriteExists, destroySpriteAndClearState, } from "../destroyShared.js";
24
22
  import { runInitStep } from "./progress.js";
23
+ import { runSetupPlanFlow } from "./setupPlanFlow.js";
24
+ import { runProvisionFlow } from "./provisionFlow.js";
25
+ import { runFinalizeFlow } from "./finalizeFlow.js";
25
26
  const requireDaemonJsonOk = (response, label) => {
26
27
  if (response.status >= 200 && response.status < 300) {
27
28
  return response.body;
@@ -61,7 +62,21 @@ const DEFAULT_INIT_STEP_RETRIES = 3;
61
62
  const INIT_STEP_RETRYABLE_STATUSES = new Set([
62
63
  408, 409, 425, 429, 500, 502, 503, 504,
63
64
  ]);
65
+ const ALIAS_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
66
+ const RESERVED_ALIAS_PATTERN = /^dvb-[a-f0-9]{12}-/;
64
67
  const delay = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));
68
+ const normalizeAlias = (value) => value.trim().toLowerCase();
69
+ const validateAlias = (alias) => {
70
+ if (!alias) {
71
+ throw new Error("Alias is required.");
72
+ }
73
+ if (RESERVED_ALIAS_PATTERN.test(alias)) {
74
+ throw new Error(`Alias "${alias}" is reserved. Choose an alias that does not start with dvb-<12 hex>-.`);
75
+ }
76
+ if (!ALIAS_PATTERN.test(alias)) {
77
+ throw new Error(`Invalid alias "${alias}". Use lowercase letters, numbers, and dashes.`);
78
+ }
79
+ };
65
80
  const computeRetryDelayMs = (retryIndex) => {
66
81
  // retryIndex is 1-based (1..N)
67
82
  const base = 500;
@@ -161,6 +176,8 @@ const migrateLegacyRepoDevboxDir = async ({ repoRoot, projectDir, }) => {
161
176
  "setup.json",
162
177
  "setup-artifacts.tgz",
163
178
  "setup-artifacts.json",
179
+ "setup-artifacts.parts",
180
+ "setup-artifacts.parts.json",
164
181
  "scans",
165
182
  "logs",
166
183
  ];
@@ -182,27 +199,6 @@ const migrateLegacyRepoDevboxDir = async ({ repoRoot, projectDir, }) => {
182
199
  }
183
200
  }
184
201
  };
185
- const buildServicesTomlUpdates = (services) => {
186
- const updates = {};
187
- for (const service of services) {
188
- const name = service.name.trim();
189
- if (!name) {
190
- throw new Error("Service name is required in setup.json services.");
191
- }
192
- const parts = splitShellCommand(service.command ?? "");
193
- const [cmd, ...args] = parts;
194
- if (!cmd) {
195
- throw new Error(`Service "${name}" is missing a command.`);
196
- }
197
- updates[name] = {
198
- name,
199
- cmd,
200
- ...(args.length > 0 ? { args } : {}),
201
- ...(service.httpPort !== null ? { httpPort: service.httpPort } : {}),
202
- };
203
- }
204
- return updates;
205
- };
206
202
  const extractCheckpointId = (events) => {
207
203
  const idPattern = /\bv\d+\b/;
208
204
  const matchId = (value) => {
@@ -229,195 +225,10 @@ const extractCheckpointId = (events) => {
229
225
  }
230
226
  return null;
231
227
  };
232
- const writeRemoteServicesToml = async ({ client, canonical, workdir, services, }) => {
233
- if (services.length === 0)
234
- return;
235
- let baseContent = "";
236
- try {
237
- const bytes = await client.readFile(canonical, {
238
- path: "devbox.toml",
239
- workingDir: workdir,
240
- });
241
- baseContent = Buffer.from(bytes).toString("utf8");
242
- }
243
- catch (error) {
244
- if (!(error instanceof SpritesApiError && error.status === 404)) {
245
- throw error;
246
- }
247
- }
248
- const updates = buildServicesTomlUpdates(services);
249
- if (Object.keys(updates).length === 0)
250
- return;
251
- const merged = mergeServicesToml(baseContent, updates);
252
- await client.writeFile(canonical, path.posix.join(workdir.replace(/\/$/, ""), "devbox.toml"), Buffer.from(merged));
253
- };
254
- const enableRemoteServices = async ({ client, canonical, services, status, }) => {
255
- if (services.length === 0)
256
- return;
257
- logger.info("init_services_enable_start", {
258
- box: canonical,
259
- serviceCount: services.length,
260
- });
261
- for (const service of services) {
262
- const name = service.name.trim();
263
- if (!name) {
264
- throw new Error("Service name is required in setup.json services.");
265
- }
266
- status?.stage(`Enabling service: ${name}`);
267
- const parts = splitShellCommand(service.command ?? "");
268
- const [cmd, ...args] = parts;
269
- if (!cmd) {
270
- throw new Error(`Service "${name}" is missing a command.`);
271
- }
272
- const input = {
273
- cmd,
274
- ...(args.length > 0 ? { args } : {}),
275
- ...(service.httpPort !== null ? { httpPort: service.httpPort } : {}),
276
- };
277
- await client.createService(canonical, name, input);
278
- }
279
- logger.info("init_services_enable_complete", { box: canonical });
280
- };
281
228
  const throwInitCanceled = () => {
282
229
  clackCancel("Init canceled.");
283
230
  throw new Error("Init canceled.");
284
231
  };
285
- const promptBeforeOpenBrowser = async ({ url, title, consequence, }) => {
286
- if (!process.stdin.isTTY)
287
- return false;
288
- showCopyableUrl(url, title);
289
- const choice = await clackSelect({
290
- message: `Open ${title} in your browser?`,
291
- options: [
292
- { value: "open", label: "Open in browser" },
293
- { value: "skip", label: "Skip" },
294
- ],
295
- initialValue: "open",
296
- });
297
- if (isCancel(choice)) {
298
- throwInitCanceled();
299
- }
300
- if (choice === "skip") {
301
- clackLog.warn(consequence);
302
- return false;
303
- }
304
- return true;
305
- };
306
- const toPosixPath = (value) => value.split(path.sep).join(path.posix.sep);
307
- const toRepoRelativePath = (repoRoot, filePath) => {
308
- const resolved = path.isAbsolute(filePath)
309
- ? filePath
310
- : path.resolve(repoRoot, filePath);
311
- const relative = path.relative(repoRoot, resolved) || filePath;
312
- return toPosixPath(relative);
313
- };
314
- const isOutsideRepoPath = (relativePath) => relativePath === ".." || relativePath.startsWith(`..${path.posix.sep}`);
315
- const ensureWorkdirOwnership = async ({ client, canonical, workdir, status, }) => {
316
- const checkResult = await client.exec(canonical, [
317
- "/bin/bash",
318
- "-lc",
319
- [
320
- "set -euo pipefail",
321
- `dir=${shellQuote(workdir)}`,
322
- 'if [ ! -d "$dir" ]; then',
323
- ' echo "Missing workdir: $dir" >&2',
324
- " exit 1",
325
- "fi",
326
- 'stat -c %U "$dir"',
327
- ].join("\n"),
328
- ]);
329
- if (checkResult.exitCode !== 0) {
330
- const details = checkResult.stderr || checkResult.stdout || "";
331
- throw new Error(details
332
- ? `Failed to check workdir ownership: ${details.trim()}`
333
- : `Failed to check workdir ownership (exit ${checkResult.exitCode})`);
334
- }
335
- const owner = checkResult.stdout.trim();
336
- if (!owner || owner === "sprite")
337
- return;
338
- status.stage("Fixing workdir ownership");
339
- const chownResult = await client.exec(canonical, [
340
- "/bin/bash",
341
- "-lc",
342
- `sudo -n chown -R sprite:sprite ${shellQuote(workdir)}`,
343
- ]);
344
- if (chownResult.exitCode !== 0) {
345
- const details = chownResult.stderr || chownResult.stdout || "";
346
- throw new Error(details
347
- ? `Failed to update workdir ownership: ${details.trim()}`
348
- : `Failed to update workdir ownership (exit ${chownResult.exitCode})`);
349
- }
350
- };
351
- const ensureGitSafeDirectory = async ({ client, canonical, workdir, status, }) => {
352
- status.stage("Configuring git safe.directory");
353
- logger.info("init_git_safe_directory_configure_start", {
354
- box: canonical,
355
- workdir,
356
- });
357
- const script = [
358
- "set -euo pipefail",
359
- `repo=${shellQuote(workdir)}`,
360
- 'if [ ! -d "$repo" ]; then',
361
- ' echo "Missing repo workdir: $repo" >&2',
362
- " exit 1",
363
- "fi",
364
- 'if [ ! -d "$repo/.git" ]; then',
365
- " exit 0",
366
- "fi",
367
- "if ! command -v git >/dev/null 2>&1; then",
368
- " exit 0",
369
- "fi",
370
- "",
371
- "ensure_safe_in_file() {",
372
- ' cfg="$1"',
373
- ' existing="$(git config --file "$cfg" --get-all safe.directory 2>/dev/null || true)"',
374
- ' if printf \'%s\\n\' "$existing" | grep -Fxq "$repo"; then',
375
- " return 0",
376
- " fi",
377
- ' git config --file "$cfg" --add safe.directory "$repo"',
378
- "}",
379
- "",
380
- // Ensure the repo is trusted for the sprite user (most devbox flows).
381
- 'ensure_safe_in_file "/home/sprite/.gitconfig"',
382
- // If we created/modified the file as root, best-effort fix the owner.
383
- 'if [ "$(id -u)" -eq 0 ]; then',
384
- " chown sprite:sprite /home/sprite/.gitconfig >/dev/null 2>&1 || true",
385
- "elif command -v sudo >/dev/null 2>&1; then",
386
- " sudo -n chown sprite:sprite /home/sprite/.gitconfig >/dev/null 2>&1 || true",
387
- "fi",
388
- "",
389
- // Best-effort: also trust the repo for root, in case tools run git as root.
390
- 'if [ "$(id -u)" -eq 0 ]; then',
391
- ' ensure_safe_in_file "/root/.gitconfig"',
392
- "elif command -v sudo >/dev/null 2>&1; then",
393
- ' root_existing="$(sudo -n git config --file /root/.gitconfig --get-all safe.directory 2>/dev/null || true)"',
394
- ' if ! printf \'%s\\n\' "$root_existing" | grep -Fxq "$repo"; then',
395
- ' sudo -n git config --file /root/.gitconfig --add safe.directory "$repo" >/dev/null 2>&1 || true',
396
- " fi",
397
- "fi",
398
- ].join("\n");
399
- const result = await client.exec(canonical, [
400
- "/bin/bash",
401
- "--noprofile",
402
- "--norc",
403
- "-e",
404
- "-u",
405
- "-o",
406
- "pipefail",
407
- "-c",
408
- script,
409
- ]);
410
- if (result.exitCode !== 0) {
411
- const details = result.stderr || result.stdout || "";
412
- throw new Error(details
413
- ? `Failed to configure git safe.directory: ${details.trim()}`
414
- : `Failed to configure git safe.directory (exit ${result.exitCode})`);
415
- }
416
- logger.info("init_git_safe_directory_configure_complete", {
417
- box: canonical,
418
- workdir,
419
- });
420
- };
421
232
  export const runInit = async (args) => {
422
233
  const parsed = parseInitArgs(args);
423
234
  const progressEnabled = process.stdout.isTTY && !parsed.json;
@@ -474,536 +285,62 @@ export const runInit = async (args) => {
474
285
  const { repoRoot, repoName, slug, localHomeDir, projectId, projectDir, repoMarker, origin, normalizedOrigin, fingerprint, } = detected;
475
286
  let initState = detected.initState;
476
287
  const initFingerprintMismatch = detected.initFingerprintMismatch;
477
- if (parsed.status) {
478
- if (parsed.resume ||
479
- parsed.force ||
480
- parsed.yes ||
481
- parsed.codexSetupOnly ||
482
- parsed.alias ||
483
- parsed.name) {
484
- throw new Error("`dvb init --status` cannot be combined with other init flags (except --json).");
485
- }
486
- const socketInfo = resolveSocketInfo();
487
- let daemonError = null;
488
- let registryProject = null;
489
- try {
490
- await ensureDaemonRunning(socketInfo.socketPath);
491
- await requireDaemonFeatures(socketInfo.socketPath, ["registry"]);
492
- const existingProject = await requestJson(socketInfo.socketPath, "GET", `/registry/project?fingerprint=${encodeURIComponent(fingerprint)}`, DAEMON_TIMEOUT_MS.registry);
493
- registryProject =
494
- requireDaemonJsonOk(existingProject, "Loading registry project (/registry/project)").project ?? null;
495
- }
496
- catch (error) {
497
- daemonError = error instanceof Error ? error.message : String(error);
498
- }
499
- const localStatus = initState && !initFingerprintMismatch
500
- ? resolveInitStatus(initState.steps, initState.complete)
501
- : null;
502
- const recommendsResume = Boolean(initState && !initFingerprintMismatch && !initState.complete);
503
- const recommendsInit = !registryProject && (!initState || initFingerprintMismatch);
504
- const recommendsForce = !recommendsResume &&
505
- (!initState || initFingerprintMismatch) &&
506
- Boolean(registryProject?.initStatus &&
507
- registryProject.initStatus !== "complete");
508
- const lines = [];
509
- const markerPath = path.join(projectDir, "box.json");
510
- const checkpointPath = path.join(projectDir, "init-state.json");
511
- lines.push("INIT STATUS");
512
- lines.push("");
513
- lines.push("Repo");
514
- lines.push(` root: ${repoRoot}`);
515
- lines.push(` origin: ${normalizedOrigin ?? origin ?? "(none)"}`);
516
- lines.push(` projectId (git config devbox.projectId): ${projectId}`);
517
- lines.push(` local state dir: ${projectDir}`);
518
- lines.push("");
519
- lines.push("Local state");
520
- if (repoMarker?.canonical) {
521
- lines.push(` box marker (${markerPath}): present (alias: ${repoMarker.alias ?? "(none)"}, box: ${repoMarker.canonical})`);
522
- }
523
- else {
524
- lines.push(` box marker (${markerPath}): missing`);
525
- }
526
- if (initState) {
527
- const completeText = initFingerprintMismatch
528
- ? `${String(Boolean(initState.complete))} (projectId mismatch)`
529
- : String(Boolean(initState.complete));
530
- lines.push(` init checkpoint (${checkpointPath}): present (updated: ${initState.updatedAt}, complete: ${completeText})`);
531
- if (initState.canonical || initState.alias || initState.workdir) {
532
- lines.push(` box: ${initState.canonical ?? "(unknown)"} (alias: ${initState.alias ?? "(unknown)"})`);
533
- lines.push(` workdir: ${initState.workdir ?? "(unknown)"}`);
534
- }
535
- if (initState.checkpoints?.preCodexSetup ||
536
- initState.checkpoints?.postCodexSetup) {
537
- const pre = initState.checkpoints?.preCodexSetup;
538
- const post = initState.checkpoints?.postCodexSetup;
539
- lines.push(" snapshots:");
540
- if (pre) {
541
- lines.push(` pre-codex-setup: ${pre.id} (created: ${pre.createdAt})`);
542
- }
543
- if (post) {
544
- lines.push(` post-codex-setup: ${post.id} (created: ${post.createdAt})`);
545
- }
546
- }
547
- if (localStatus) {
548
- lines.push(` inferred init status: ${localStatus}`);
549
- }
550
- lines.push(" steps:");
551
- const steps = initState.steps ?? {};
552
- for (const key of INIT_STEP_KEYS) {
553
- lines.push(` ${key}: ${steps[key] ? "yes" : "no"}`);
554
- }
555
- }
556
- else {
557
- lines.push(` init checkpoint (${checkpointPath}): missing`);
558
- }
559
- lines.push("");
560
- lines.push("Registry");
561
- lines.push(` daemon socket: ${socketInfo.socketPath}`);
562
- if (daemonError) {
563
- lines.push(` daemon: unavailable (${daemonError})`);
564
- }
565
- else if (registryProject) {
566
- lines.push(" project: present");
567
- lines.push(` box: ${registryProject.canonical}`);
568
- lines.push(` alias: ${registryProject.alias ?? "(none)"}`);
569
- lines.push(` initStatus: ${registryProject.initStatus ?? "(none)"}`);
570
- lines.push(` initUpdatedAt: ${registryProject.initUpdatedAt ?? "(none)"}`);
571
- if (registryProject.localPaths &&
572
- registryProject.localPaths.length > 0) {
573
- lines.push(` localPaths: ${registryProject.localPaths.length} path(s)`);
574
- }
575
- }
576
- else {
577
- lines.push(" project: not found");
578
- }
579
- lines.push("");
580
- lines.push("Recommended");
581
- if (recommendsResume) {
582
- lines.push(" dvb init --resume");
583
- }
584
- else if (recommendsInit) {
585
- lines.push(" dvb init");
586
- }
587
- else if (recommendsForce) {
588
- lines.push(" Resume is not available from this clone (no init checkpoint).");
589
- lines.push(" If init was started elsewhere, re-run `dvb init --resume` from that repo.");
590
- lines.push(" Otherwise, restart init with `dvb init --force` (destroys and recreates the existing devbox).");
591
- }
592
- else {
593
- lines.push(" (none)");
594
- }
595
- lines.push("");
596
- if (parsed.json) {
597
- console.log(JSON.stringify({
598
- ok: true,
599
- repo: {
600
- root: repoRoot,
601
- origin: normalizedOrigin ?? origin ?? null,
602
- fingerprint,
603
- },
604
- local: {
605
- marker: repoMarker ?? null,
606
- initState: initState ?? null,
607
- initFingerprintMismatch,
608
- inferredStatus: localStatus,
609
- },
610
- registry: {
611
- socketPath: socketInfo.socketPath,
612
- error: daemonError,
613
- project: registryProject,
614
- },
615
- recommended: recommendsResume
616
- ? "dvb init --resume"
617
- : recommendsInit
618
- ? "dvb init"
619
- : recommendsForce
620
- ? "dvb init --force"
621
- : null,
622
- }, null, 2));
623
- return;
624
- }
625
- console.log(lines.join("\n"));
288
+ if (await runInitStatusFlow({
289
+ parsed,
290
+ repoRoot,
291
+ origin,
292
+ normalizedOrigin,
293
+ projectId,
294
+ projectDir,
295
+ fingerprint,
296
+ repoMarker,
297
+ initState,
298
+ initFingerprintMismatch,
299
+ requireDaemonJsonOk,
300
+ resolveInitStatus,
301
+ })) {
626
302
  return;
627
303
  }
628
- if (parsed.resume) {
629
- if (initFingerprintMismatch) {
630
- throw new Error(`Init state does not match this repo. Remove ${path.join(projectDir, "init-state.json")} and run \`dvb init\` again.`);
631
- }
632
- if (!initState) {
633
- throw new Error("No init state to resume. Run `dvb init` to start a new init.");
634
- }
635
- if (initState.complete) {
636
- throw new Error("Init already completed. Run `dvb init` to start a new init.");
637
- }
638
- }
639
- if (initFingerprintMismatch) {
640
- initState = null;
641
- }
642
- const wantsResume = Boolean(parsed.resume);
643
- if (!parsed.codexSetupOnly &&
644
- !wantsResume &&
645
- initState &&
646
- !initState.complete &&
647
- !parsed.force) {
648
- throw new Error("Previous init is incomplete. Run `dvb init --resume` to finish the previous init or `dvb init --force` to restart.");
649
- }
650
- if (parsed.force && !wantsResume) {
651
- // Forced fresh starts must reset checkpoint state so stale `complete`/step
652
- // values from prior runs cannot block `dvb init --resume` after failures.
653
- const previousState = initState;
654
- const freshState = {
655
- version: 1,
656
- fingerprint,
657
- ...(previousState?.canonical !== undefined
658
- ? { canonical: previousState.canonical }
659
- : {}),
660
- ...(previousState?.alias !== undefined
661
- ? { alias: previousState.alias }
662
- : {}),
663
- ...(previousState?.workdir !== undefined
664
- ? { workdir: previousState.workdir }
665
- : {}),
666
- steps: {},
667
- updatedAt: new Date().toISOString(),
668
- complete: false,
669
- };
670
- initState = freshState;
671
- await writeInitState(projectDir, freshState);
672
- }
673
- const shouldResume = Boolean(wantsResume && initState && !initState.complete);
674
- const ensureInitState = () => {
675
- if (!initState || initState.fingerprint !== fingerprint) {
676
- initState = {
677
- version: 1,
678
- fingerprint,
679
- steps: {},
680
- updatedAt: new Date().toISOString(),
681
- };
682
- }
683
- return initState;
684
- };
685
- const updateInitState = async (update) => {
686
- const base = ensureInitState();
687
- initState = {
688
- ...base,
689
- ...update,
690
- steps: {
691
- ...base.steps,
692
- ...(update.steps ?? {}),
693
- },
694
- updatedAt: new Date().toISOString(),
695
- };
696
- await writeInitState(projectDir, initState);
697
- };
698
- const recordCodexCheckpoint = async ({ client, canonical, phase, }) => {
699
- const createdAt = new Date().toISOString();
700
- const label = phase === "preCodexSetup" ? "pre-codex-setup" : "post-codex-setup";
701
- const comment = `dvb init: ${label} repo=${repoName} fingerprint=${fingerprint} at=${createdAt}`;
702
- const events = await client.createCheckpoint(canonical, { comment });
703
- const id = extractCheckpointId(events);
704
- if (!id) {
304
+ const preparedSession = await prepareInitSessionState({
305
+ parsed,
306
+ initState,
307
+ initFingerprintMismatch,
308
+ projectDir,
309
+ fingerprint,
310
+ });
311
+ initState = preparedSession.initState;
312
+ const { shouldResume, nonInteractive, skipCodexApply, skipCodexCliEnsure } = preparedSession;
313
+ const getInitState = () => initState;
314
+ const { updateInitState } = createInitStateUpdater({
315
+ fingerprint,
316
+ projectDir,
317
+ getInitState,
318
+ setInitState: (next) => {
319
+ initState = next;
320
+ },
321
+ });
322
+ const recordCodexCheckpoint = createCodexCheckpointRecorder({
323
+ repoName,
324
+ fingerprint,
325
+ getInitState,
326
+ updateInitState,
327
+ extractCheckpointId,
328
+ onCheckpointIdMissing: ({ canonical, phaseLabel }) => {
705
329
  logger.warn("init_checkpoint_id_missing", {
706
330
  box: canonical,
707
331
  fingerprint,
708
- phase: label,
709
- });
710
- throw new Error("Checkpoint ID missing.");
711
- }
712
- const record = { id, comment, createdAt };
713
- if (initState) {
714
- await updateInitState({
715
- checkpoints: { ...(initState.checkpoints ?? {}), [phase]: record },
332
+ phase: phaseLabel,
716
333
  });
717
- }
718
- const metaPath = `/home/sprite/.devbox/projects/${fingerprint}.json`;
719
- try {
720
- let meta = {};
721
- try {
722
- const raw = await client.readFile(canonical, { path: metaPath });
723
- const parsed = JSON.parse(Buffer.from(raw).toString("utf8"));
724
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
725
- meta = parsed;
726
- }
727
- }
728
- catch (error) {
729
- if (!(error instanceof SpritesApiError && error.status === 404)) {
730
- throw error;
731
- }
732
- }
733
- const existingCheckpoints = meta.checkpoints &&
734
- typeof meta.checkpoints === "object" &&
735
- !Array.isArray(meta.checkpoints)
736
- ? meta.checkpoints
737
- : {};
738
- const updated = {
739
- ...meta,
740
- checkpoints: {
741
- ...existingCheckpoints,
742
- [phase]: record,
743
- },
744
- };
745
- await client.writeFile(canonical, metaPath, Buffer.from(JSON.stringify(updated, null, 2)));
746
- }
747
- catch (error) {
334
+ },
335
+ onCheckpointMetaUpdateFailed: ({ canonical, phaseLabel, error }) => {
748
336
  logger.warn("init_checkpoint_meta_update_failed", {
749
337
  box: canonical,
750
338
  fingerprint,
751
- phase: label,
752
- error: error instanceof Error ? error.message : String(error),
753
- });
754
- }
755
- return { id, comment, createdAt };
756
- };
757
- if (parsed.codexSetupOnly) {
758
- if (!repoMarker?.canonical) {
759
- throw new Error("Repo is not initialized. Run `dvb init` first.");
760
- }
761
- const setupDir = projectDir;
762
- const setupPath = path.join(setupDir, "setup.json");
763
- let setupPlan;
764
- try {
765
- setupPlan = await readSetupPlan(setupPath);
766
- }
767
- catch {
768
- throw new Error(`Missing or invalid setup plan (${setupPath}). Run \`dvb init\` first.`);
769
- }
770
- const localArtifactsBundlePath = path.join(setupDir, "setup-artifacts.tgz");
771
- const localArtifactsManifestPath = path.join(setupDir, "setup-artifacts.json");
772
- let artifactsBundlePath = null;
773
- let artifactsManifestPath = null;
774
- try {
775
- await fs.access(localArtifactsBundlePath);
776
- await fs.access(localArtifactsManifestPath);
777
- artifactsBundlePath = localArtifactsBundlePath;
778
- artifactsManifestPath = localArtifactsManifestPath;
779
- }
780
- catch {
781
- // artifacts are optional in setup-only flow
782
- }
783
- const socketInfo = resolveSocketInfo();
784
- await runInitStep({
785
- enabled: progressEnabled,
786
- title: "Starting dvbd",
787
- fn: async () => {
788
- await ensureDaemonRunning(socketInfo.socketPath);
789
- await requireDaemonFeatures(socketInfo.socketPath, ["ports"]);
790
- },
791
- });
792
- const { config, client, controlPlaneToken } = await runInitStep({
793
- enabled: progressEnabled,
794
- title: "Loading devbox config",
795
- fn: async () => {
796
- const config = await loadConfig(process.env.HOME ? { homeDir: process.env.HOME } : undefined);
797
- const store = await createSecretStore(config?.tokenStore, process.env.HOME ? { homeDir: process.env.HOME } : undefined);
798
- const apiBaseUrl = resolveSpritesApiUrl(config);
799
- const { token, controlPlaneToken } = await ensureSpritesToken(store, undefined, {
800
- apiBaseUrl,
801
- });
802
- const client = createSpritesClient({
803
- apiBaseUrl,
804
- token,
805
- });
806
- return { config, client, controlPlaneToken };
807
- },
808
- });
809
- const setupOnlyCodexAuthMode = resolveCodexAuthMode(config);
810
- const setupOnlyCodexProxyOptions = setupOnlyCodexAuthMode === "proxy"
811
- ? {
812
- gatewayBaseUrl: resolveSpritesApiUrl(config),
813
- controlPlaneToken: (controlPlaneToken ?? "").trim(),
814
- }
815
- : undefined;
816
- if (setupOnlyCodexAuthMode === "proxy" &&
817
- !setupOnlyCodexProxyOptions?.controlPlaneToken) {
818
- throw new Error("Control plane session required for init Codex proxy mode.");
819
- }
820
- const canonical = repoMarker.canonical;
821
- let expandedWorkdir = expandHome(`~/${slug}`);
822
- try {
823
- const metaRaw = await client.readFile(canonical, {
824
- path: `/home/sprite/.devbox/projects/${fingerprint}.json`,
339
+ phase: phaseLabel,
340
+ error,
825
341
  });
826
- const meta = JSON.parse(Buffer.from(metaRaw).toString("utf8"));
827
- if (meta.workdir) {
828
- expandedWorkdir = expandHome(meta.workdir);
829
- }
830
- }
831
- catch {
832
- // ignore missing project metadata
833
- }
834
- const remoteSetupPath = path.posix.join(expandedWorkdir, ".devbox", "setup.json");
835
- const remoteArtifactsBundlePath = path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.tgz");
836
- const remoteArtifactsManifestPath = path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.json");
837
- const pathSetup = 'export PATH="$(npm bin -g 2>/dev/null):$PATH"';
838
- await runInitStep({
839
- enabled: progressEnabled,
840
- title: "Configuring git safe.directory",
841
- fn: async ({ status }) => {
842
- await ensureGitSafeDirectory({
843
- client,
844
- canonical,
845
- workdir: expandedWorkdir,
846
- status,
847
- });
848
- },
849
- });
850
- await runInitStep({
851
- enabled: progressEnabled,
852
- title: "Uploading setup plan",
853
- fn: async ({ status }) => {
854
- await uploadSetupPlan({
855
- client,
856
- canonical,
857
- localSetupPath: setupPath,
858
- remoteSetupPath,
859
- localArtifactsBundlePath: artifactsBundlePath,
860
- localArtifactsManifestPath: artifactsManifestPath,
861
- remoteArtifactsBundlePath: artifactsBundlePath
862
- ? remoteArtifactsBundlePath
863
- : null,
864
- remoteArtifactsManifestPath: artifactsBundlePath
865
- ? remoteArtifactsManifestPath
866
- : null,
867
- status,
868
- });
869
- },
870
- });
871
- await runInitStep({
872
- enabled: progressEnabled,
873
- title: "Staging setup artifacts",
874
- fn: async ({ status }) => {
875
- status.stage("Copying repo artifacts and staging external files");
876
- await stageRemoteSetupArtifacts({
877
- client,
878
- canonical,
879
- workdir: expandedWorkdir,
880
- artifactsBundlePath: remoteArtifactsBundlePath,
881
- artifactsManifestPath: remoteArtifactsManifestPath,
882
- });
883
- },
884
- });
885
- await runInitStep({
886
- enabled: progressEnabled,
887
- title: "Snapshotting filesystem (pre-setup)",
888
- fn: async ({ status, fail, ok }) => {
889
- try {
890
- const checkpoint = await retryInitStep({
891
- status,
892
- title: "Snapshotting filesystem (pre-setup)",
893
- fn: async () => await recordCodexCheckpoint({
894
- client,
895
- canonical,
896
- phase: "preCodexSetup",
897
- }),
898
- });
899
- if (checkpoint.id) {
900
- ok(`Snapshot created: ${checkpoint.id}`);
901
- }
902
- }
903
- catch (error) {
904
- logger.warn("init_checkpoint_create_failed", {
905
- box: canonical,
906
- fingerprint,
907
- phase: "pre-codex-setup",
908
- error: error instanceof Error ? error.message : String(error),
909
- });
910
- fail("Snapshotting filesystem (pre-setup) (failed)");
911
- throw error;
912
- }
913
- },
914
- });
915
- await runInitStep({
916
- enabled: progressEnabled,
917
- title: "Enabling devbox services",
918
- fn: async ({ status }) => {
919
- await enableRemoteServices({
920
- client,
921
- canonical,
922
- services: setupPlan.services.backgroundServices,
923
- status,
924
- });
925
- },
926
- });
927
- await runInitStep({
928
- enabled: progressEnabled,
929
- title: "Ensuring Codex CLI",
930
- fn: async ({ status, fail }) => {
931
- try {
932
- await retryInitStep({
933
- status,
934
- title: "Ensuring Codex CLI",
935
- fn: async () => await ensureRemoteCodexInstalled(client, canonical),
936
- });
937
- }
938
- catch (error) {
939
- logger.warn("codex_cli_ensure_failed", {
940
- box: canonical,
941
- error: error instanceof Error ? error.message : String(error),
942
- });
943
- fail("Ensuring Codex CLI (failed)");
944
- throw error;
945
- }
946
- },
947
- });
948
- await runInitStep({
949
- enabled: progressEnabled,
950
- title: "Applying setup plan",
951
- fn: async ({ status }) => {
952
- await runRemoteCodexSetup({
953
- client,
954
- canonical,
955
- expandedWorkdir,
956
- remoteSetupPath,
957
- remoteArtifactsBundlePath,
958
- remoteArtifactsManifestPath,
959
- socketInfo,
960
- status,
961
- pathSetup,
962
- entrypoints: setupPlan.services.appEntrypoints,
963
- emitCodexOutput: !parsed.json,
964
- ...(setupOnlyCodexProxyOptions
965
- ? { proxyOptions: setupOnlyCodexProxyOptions }
966
- : {}),
967
- });
968
- },
969
- });
970
- await runInitStep({
971
- enabled: progressEnabled,
972
- title: "Snapshotting filesystem (post-setup)",
973
- fn: async ({ status, fail, ok }) => {
974
- try {
975
- const checkpoint = await retryInitStep({
976
- status,
977
- title: "Snapshotting filesystem (post-setup)",
978
- fn: async () => await recordCodexCheckpoint({
979
- client,
980
- canonical,
981
- phase: "postCodexSetup",
982
- }),
983
- });
984
- if (checkpoint.id) {
985
- ok(`Snapshot created: ${checkpoint.id}`);
986
- }
987
- }
988
- catch (error) {
989
- logger.warn("init_checkpoint_create_failed", {
990
- box: canonical,
991
- fingerprint,
992
- phase: "post-codex-setup",
993
- error: error instanceof Error ? error.message : String(error),
994
- });
995
- fail("Snapshotting filesystem (post-setup) (failed)");
996
- throw error;
997
- }
998
- },
999
- });
1000
- if (parsed.json) {
1001
- console.log(JSON.stringify({ ok: true }, null, 2));
1002
- return;
1003
- }
1004
- console.log("Codex setup complete.");
1005
- return;
1006
- }
342
+ },
343
+ });
1007
344
  const socketInfo = resolveSocketInfo();
1008
345
  await runInitStep({
1009
346
  enabled: progressEnabled,
@@ -1036,13 +373,15 @@ export const runInit = async (args) => {
1036
373
  throw new Error(`Repo already initialized (box: ${existingEntry.canonical}).`);
1037
374
  }
1038
375
  }
1039
- const alias = parsed.alias ?? initState?.alias ?? existingEntry?.alias ?? slug;
1040
- const canonicalHint = parsed.name ?? initState?.canonical ?? existingEntry?.canonical;
376
+ const alias = normalizeAlias(parsed.alias ?? initState?.alias ?? existingEntry?.alias ?? slug);
377
+ validateAlias(alias);
378
+ const canonicalHint = initState?.canonical ?? existingEntry?.canonical;
1041
379
  const workdir = `~/${slug}`;
1042
380
  const expandedWorkdir = expandHome(workdir);
1043
381
  const remoteSetupPath = path.posix.join(expandedWorkdir, ".devbox", "setup.json");
1044
382
  const remoteArtifactsBundlePath = path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.tgz");
1045
383
  const remoteArtifactsManifestPath = path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.json");
384
+ const remoteArtifactsPartsDescriptorPath = path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.parts.json");
1046
385
  const pathSetup = 'export PATH="$(npm bin -g 2>/dev/null):$PATH"';
1047
386
  const { config, client, controlPlaneToken } = await runInitStep({
1048
387
  enabled: progressEnabled,
@@ -1121,15 +460,17 @@ export const runInit = async (args) => {
1121
460
  });
1122
461
  existingEntry = null;
1123
462
  }
1124
- const username = os.userInfo().username;
1125
463
  let canonical = (shouldResume && initState?.canonical ? initState.canonical : null) ??
1126
464
  canonicalHint ??
1127
- `${username}-${slug}`;
465
+ alias;
1128
466
  const knownAssociatedCanonicals = new Set([existingEntry?.canonical, initState?.canonical].filter((value) => typeof value === "string" && value.length > 0));
1129
467
  const createSprite = async (name) => {
1130
468
  try {
1131
- await client.createSprite(name);
1132
- return "created";
469
+ const created = await client.createSprite(name);
470
+ const canonical = typeof created.name === "string" && created.name.trim()
471
+ ? created.name.trim()
472
+ : name;
473
+ return { kind: "created", canonical };
1133
474
  }
1134
475
  catch (error) {
1135
476
  if (error instanceof SpritesApiError) {
@@ -1138,7 +479,7 @@ export const runInit = async (args) => {
1138
479
  error.status === 409 ||
1139
480
  body.includes("exists") ||
1140
481
  body.includes("already")) {
1141
- return "exists";
482
+ return { kind: "exists" };
1142
483
  }
1143
484
  }
1144
485
  throw error;
@@ -1163,7 +504,7 @@ export const runInit = async (args) => {
1163
504
  fn: async ({ status }) => {
1164
505
  let nextCanonical = canonical;
1165
506
  const createResult = await createSprite(nextCanonical);
1166
- if (createResult === "exists") {
507
+ if (createResult.kind === "exists") {
1167
508
  const associatedCanonical = knownAssociatedCanonicals.has(nextCanonical);
1168
509
  if (parsed.force) {
1169
510
  if (!associatedCanonical) {
@@ -1181,7 +522,7 @@ export const runInit = async (args) => {
1181
522
  throwInitCanceled();
1182
523
  }
1183
524
  if (!confirmedReuse) {
1184
- throw new Error(`Sprite reuse canceled: ${nextCanonical}. Choose a different --name.`);
525
+ throw new Error(`Sprite reuse canceled: ${nextCanonical}. Choose a different alias.`);
1185
526
  }
1186
527
  status.stage("Creating devbox");
1187
528
  }
@@ -1194,11 +535,15 @@ export const runInit = async (args) => {
1194
535
  nextCanonical = `${nextCanonical}-${suffix}`;
1195
536
  status.stage("Resolving devbox name");
1196
537
  const second = await createSprite(nextCanonical);
1197
- if (second === "exists") {
538
+ if (second.kind === "exists") {
1198
539
  throw new Error(`Sprite already exists: ${nextCanonical}`);
1199
540
  }
541
+ nextCanonical = second.canonical;
1200
542
  }
1201
543
  }
544
+ else {
545
+ nextCanonical = createResult.canonical;
546
+ }
1202
547
  await updateInitState({
1203
548
  canonical: nextCanonical,
1204
549
  alias,
@@ -1386,916 +731,6 @@ export const runInit = async (args) => {
1386
731
  }
1387
732
  },
1388
733
  });
1389
- const setupDir = projectDir;
1390
- const setupPath = path.join(setupDir, "setup.json");
1391
- const scansDir = path.join(setupDir, "scans");
1392
- const logDir = path.join(setupDir, "logs");
1393
- const setupEnvSecretsScanPath = path.join(scansDir, "setup-env-secrets.json");
1394
- const setupExternalScanPath = path.join(scansDir, "setup-external.json");
1395
- const setupExtraArtifactsScanPath = path.join(scansDir, "setup-extra-artifacts.json");
1396
- const servicesScanPath = path.join(scansDir, "services.json");
1397
- let setupArtifacts = null;
1398
- const nonInteractive = !process.stdin.isTTY || parsed.json;
1399
- const skipSetupPlan = shouldResume && initState?.steps.setupPlanWritten;
1400
- const skipServicesConfig = shouldResume && initState?.steps.servicesConfigWritten;
1401
- const skipServicesEnable = shouldResume && initState?.steps.servicesEnabled;
1402
- const skipSetupUpload = nonInteractive || (shouldResume && initState?.steps.setupUploaded);
1403
- const skipCodexApply = nonInteractive || (shouldResume && initState?.steps.codexApplied);
1404
- const skipCodexCliEnsure = skipCodexApply || (shouldResume && initState?.steps.codexCliEnsured);
1405
- let approvedPlan = null;
1406
- const setupTempDir = await fs.mkdtemp(path.join(os.tmpdir(), "devbox-setup-"));
1407
- try {
1408
- await fs.mkdir(setupDir, { recursive: true, mode: 0o700 });
1409
- try {
1410
- await fs.chmod(setupDir, 0o700);
1411
- }
1412
- catch {
1413
- // best effort on filesystems that do not support chmod
1414
- }
1415
- const tryReadSetupPlan = async () => {
1416
- try {
1417
- return await readSetupPlan(setupPath);
1418
- }
1419
- catch {
1420
- return null;
1421
- }
1422
- };
1423
- const tryReadServicesPlan = async () => {
1424
- try {
1425
- return await readServicesPlan(servicesScanPath);
1426
- }
1427
- catch {
1428
- return null;
1429
- }
1430
- };
1431
- const tryReadEnvSecretsScan = async () => {
1432
- try {
1433
- return await readSetupEnvSecretsPlan(setupEnvSecretsScanPath);
1434
- }
1435
- catch {
1436
- return null;
1437
- }
1438
- };
1439
- const tryReadExternalScan = async () => {
1440
- try {
1441
- return await readSetupExternalPlan(setupExternalScanPath);
1442
- }
1443
- catch {
1444
- return null;
1445
- }
1446
- };
1447
- const tryReadExtraArtifactsScan = async () => {
1448
- try {
1449
- return await readSetupExtraArtifactsPlan(setupExtraArtifactsScanPath);
1450
- }
1451
- catch {
1452
- return null;
1453
- }
1454
- };
1455
- const shouldRetryCodexScan = (scanFullyCompleted, ...arrays) => !scanFullyCompleted && arrays.every((array) => array.length === 0);
1456
- let setupPlan = skipSetupPlan || !shouldResume ? null : await tryReadSetupPlan();
1457
- const needsSetupScan = !skipSetupPlan && !setupPlan;
1458
- let servicesPlan = !needsSetupScan || !shouldResume ? null : await tryReadServicesPlan();
1459
- let envSecretsScan = !needsSetupScan || !shouldResume ? null : await tryReadEnvSecretsScan();
1460
- let externalScan = !needsSetupScan || !shouldResume ? null : await tryReadExternalScan();
1461
- let extraArtifactsScan = !needsSetupScan || !shouldResume
1462
- ? null
1463
- : await tryReadExtraArtifactsScan();
1464
- if (servicesPlan &&
1465
- shouldRetryCodexScan(servicesPlan.scanFullyCompleted, servicesPlan.appEntrypoints, servicesPlan.backgroundServices)) {
1466
- servicesPlan = null;
1467
- }
1468
- if (envSecretsScan &&
1469
- shouldRetryCodexScan(envSecretsScan.scanFullyCompleted, envSecretsScan.envFiles, envSecretsScan.secretFiles)) {
1470
- envSecretsScan = null;
1471
- }
1472
- if (externalScan &&
1473
- shouldRetryCodexScan(externalScan.scanFullyCompleted, externalScan.externalDependencies, externalScan.externalConfigs)) {
1474
- externalScan = null;
1475
- }
1476
- if (extraArtifactsScan &&
1477
- shouldRetryCodexScan(extraArtifactsScan.scanFullyCompleted, extraArtifactsScan.extraArtifacts)) {
1478
- extraArtifactsScan = null;
1479
- }
1480
- const needsServicesScan = needsSetupScan && !servicesPlan;
1481
- const needsEnvSecretsScan = needsSetupScan && !envSecretsScan;
1482
- const needsExternalScan = needsSetupScan && !externalScan;
1483
- const needsExtraArtifactsScan = needsSetupScan && !extraArtifactsScan;
1484
- if (needsSetupScan || needsServicesScan) {
1485
- const runLocalEnvironmentAnalysis = async ({ updateEnvSecrets, updateExternal, updateExtraArtifacts, updateServices, }) => {
1486
- let envSecretsSchemaPath = null;
1487
- let externalSchemaPath = null;
1488
- let extraArtifactsSchemaPath = null;
1489
- let servicesSchemaPath = null;
1490
- if (needsEnvSecretsScan) {
1491
- envSecretsSchemaPath =
1492
- await writeSetupEnvSecretsSchema(setupTempDir);
1493
- }
1494
- if (needsExternalScan) {
1495
- externalSchemaPath = await writeSetupExternalSchema(setupTempDir);
1496
- }
1497
- if (needsExtraArtifactsScan) {
1498
- extraArtifactsSchemaPath =
1499
- await writeSetupExtraArtifactsSchema(setupTempDir);
1500
- }
1501
- if (needsServicesScan) {
1502
- servicesSchemaPath = await writeServicesSchema(setupTempDir);
1503
- }
1504
- if (needsEnvSecretsScan && !envSecretsSchemaPath) {
1505
- throw new Error("Env/secrets schema path missing.");
1506
- }
1507
- if (needsExternalScan && !externalSchemaPath) {
1508
- throw new Error("External schema path missing.");
1509
- }
1510
- if (needsExtraArtifactsScan && !extraArtifactsSchemaPath) {
1511
- throw new Error("Extra artifacts schema path missing.");
1512
- }
1513
- if (needsServicesScan && !servicesSchemaPath) {
1514
- throw new Error("Services schema path missing.");
1515
- }
1516
- if (needsSetupScan) {
1517
- await fs.mkdir(scansDir, { recursive: true });
1518
- }
1519
- const runCodexScanWithImmediateRetry = async ({ run, read, outputPath, update, shouldRetry, }) => {
1520
- try {
1521
- await run();
1522
- let out = await read();
1523
- if (shouldRetry(out)) {
1524
- update("retrying");
1525
- await fs.rm(outputPath, { force: true });
1526
- await run();
1527
- out = await read();
1528
- }
1529
- update("done");
1530
- return out;
1531
- }
1532
- catch (error) {
1533
- update("failed");
1534
- throw error;
1535
- }
1536
- };
1537
- const envSecretsPromise = !needsSetupScan
1538
- ? Promise.resolve(null)
1539
- : !needsEnvSecretsScan
1540
- ? Promise.resolve(envSecretsScan)
1541
- : runCodexScanWithImmediateRetry({
1542
- run: async () => await runLocalSetupEnvSecretsScan({
1543
- cwd: repoRoot,
1544
- logDir,
1545
- schemaPath: envSecretsSchemaPath,
1546
- outputPath: setupEnvSecretsScanPath,
1547
- ...(initCodexProxyOptions
1548
- ? { proxyOptions: initCodexProxyOptions }
1549
- : {}),
1550
- onProgress: updateEnvSecrets,
1551
- }),
1552
- read: async () => await readSetupEnvSecretsPlan(setupEnvSecretsScanPath),
1553
- outputPath: setupEnvSecretsScanPath,
1554
- update: updateEnvSecrets,
1555
- shouldRetry: (plan) => shouldRetryCodexScan(plan.scanFullyCompleted, plan.envFiles, plan.secretFiles),
1556
- });
1557
- const externalPromise = !needsSetupScan
1558
- ? Promise.resolve(null)
1559
- : !needsExternalScan
1560
- ? Promise.resolve(externalScan)
1561
- : runCodexScanWithImmediateRetry({
1562
- run: async () => await runLocalSetupExternalScan({
1563
- cwd: repoRoot,
1564
- logDir,
1565
- schemaPath: externalSchemaPath,
1566
- outputPath: setupExternalScanPath,
1567
- homeDir: localHomeDir,
1568
- ...(initCodexProxyOptions
1569
- ? { proxyOptions: initCodexProxyOptions }
1570
- : {}),
1571
- onProgress: updateExternal,
1572
- }),
1573
- read: async () => await readSetupExternalPlan(setupExternalScanPath),
1574
- outputPath: setupExternalScanPath,
1575
- update: updateExternal,
1576
- shouldRetry: (plan) => shouldRetryCodexScan(plan.scanFullyCompleted, plan.externalDependencies, plan.externalConfigs),
1577
- });
1578
- const extraArtifactsPromise = !needsSetupScan
1579
- ? Promise.resolve(null)
1580
- : !needsExtraArtifactsScan
1581
- ? Promise.resolve(extraArtifactsScan)
1582
- : runCodexScanWithImmediateRetry({
1583
- run: async () => await runLocalSetupExtraArtifactsScan({
1584
- cwd: repoRoot,
1585
- logDir,
1586
- schemaPath: extraArtifactsSchemaPath,
1587
- outputPath: setupExtraArtifactsScanPath,
1588
- ...(initCodexProxyOptions
1589
- ? { proxyOptions: initCodexProxyOptions }
1590
- : {}),
1591
- onProgress: updateExtraArtifacts,
1592
- }),
1593
- read: async () => await readSetupExtraArtifactsPlan(setupExtraArtifactsScanPath),
1594
- outputPath: setupExtraArtifactsScanPath,
1595
- update: updateExtraArtifacts,
1596
- shouldRetry: (plan) => shouldRetryCodexScan(plan.scanFullyCompleted, plan.extraArtifacts),
1597
- });
1598
- const servicesPromise = !needsServicesScan
1599
- ? Promise.resolve(servicesPlan)
1600
- : runCodexScanWithImmediateRetry({
1601
- run: async () => await runLocalServicesScan({
1602
- cwd: repoRoot,
1603
- logDir,
1604
- schemaPath: servicesSchemaPath,
1605
- outputPath: servicesScanPath,
1606
- homeDir: localHomeDir,
1607
- ...(initCodexProxyOptions
1608
- ? { proxyOptions: initCodexProxyOptions }
1609
- : {}),
1610
- onProgress: updateServices,
1611
- }),
1612
- read: async () => await readServicesPlan(servicesScanPath),
1613
- outputPath: servicesScanPath,
1614
- update: updateServices,
1615
- shouldRetry: (plan) => shouldRetryCodexScan(plan.scanFullyCompleted, plan.appEntrypoints, plan.backgroundServices),
1616
- });
1617
- const [envSecrets, external, extraArtifacts, services] = await Promise.all([
1618
- envSecretsPromise,
1619
- externalPromise,
1620
- extraArtifactsPromise,
1621
- servicesPromise,
1622
- ]);
1623
- if (needsServicesScan) {
1624
- if (!services) {
1625
- throw new Error("Services scan missing.");
1626
- }
1627
- servicesPlan = services;
1628
- }
1629
- if (needsSetupScan) {
1630
- if (!envSecrets) {
1631
- throw new Error("Env/secrets scan missing.");
1632
- }
1633
- if (!external) {
1634
- throw new Error("External scan missing.");
1635
- }
1636
- if (!extraArtifacts) {
1637
- throw new Error("Extra artifacts scan missing.");
1638
- }
1639
- if (!servicesPlan) {
1640
- throw new Error("Services scan missing.");
1641
- }
1642
- updateEnvSecrets("merging setup plan");
1643
- const merged = mergeSetupScans({
1644
- envSecrets,
1645
- external,
1646
- extraArtifacts,
1647
- services: {
1648
- appEntrypoints: servicesPlan.appEntrypoints,
1649
- backgroundServices: servicesPlan.backgroundServices,
1650
- },
1651
- });
1652
- await writeSetupPlan(setupPath, merged);
1653
- setupPlan = merged;
1654
- }
1655
- };
1656
- if (!progressEnabled) {
1657
- await runLocalEnvironmentAnalysis({
1658
- updateEnvSecrets: () => { },
1659
- updateExternal: () => { },
1660
- updateExtraArtifacts: () => { },
1661
- updateServices: () => { },
1662
- });
1663
- }
1664
- else {
1665
- const log = clackTaskLog({
1666
- title: "Analyzing local environment",
1667
- limit: 1,
1668
- spacing: 0,
1669
- });
1670
- let active = true;
1671
- const colorCategory = (label) => {
1672
- if (!process.stdout.hasColors?.())
1673
- return label;
1674
- const undim = "\u001b[22m";
1675
- const dim = "\u001b[2m";
1676
- const bold = "\u001b[1m";
1677
- const teal = "\u001b[36m";
1678
- const resetColor = "\u001b[39m";
1679
- return `${undim}${teal}${bold}${label}${resetColor}${undim}${dim}`;
1680
- };
1681
- const formatRow = (label, message) => {
1682
- const normalized = message.replace(/\r?\n/g, " ").trim();
1683
- return `${colorCategory(label)}: ${normalized}`;
1684
- };
1685
- const envSecretsRow = log.group("");
1686
- const externalRow = log.group("");
1687
- const extraArtifactsRow = log.group("");
1688
- const servicesRow = log.group("");
1689
- const makeUpdater = (row, label) => (message) => {
1690
- if (!active)
1691
- return;
1692
- row.message(formatRow(label, message));
1693
- };
1694
- const updateEnvSecrets = makeUpdater(envSecretsRow, "env/secrets");
1695
- const updateExternal = makeUpdater(externalRow, "external");
1696
- const updateExtraArtifacts = makeUpdater(extraArtifactsRow, "extra artifacts");
1697
- const updateServices = makeUpdater(servicesRow, "services");
1698
- updateEnvSecrets(needsSetupScan
1699
- ? needsEnvSecretsScan
1700
- ? "starting"
1701
- : "cached"
1702
- : "skipped");
1703
- updateExternal(needsSetupScan
1704
- ? needsExternalScan
1705
- ? "starting"
1706
- : "cached"
1707
- : "skipped");
1708
- updateExtraArtifacts(needsSetupScan
1709
- ? needsExtraArtifactsScan
1710
- ? "starting"
1711
- : "cached"
1712
- : "skipped");
1713
- updateServices(needsServicesScan ? "starting" : "cached");
1714
- try {
1715
- await runLocalEnvironmentAnalysis({
1716
- updateEnvSecrets,
1717
- updateExternal,
1718
- updateExtraArtifacts,
1719
- updateServices,
1720
- });
1721
- active = false;
1722
- log.success("Analyzing local environment");
1723
- }
1724
- catch (error) {
1725
- active = false;
1726
- log.error("Analyzing local environment");
1727
- throw error;
1728
- }
1729
- }
1730
- }
1731
- if (!skipSetupPlan && !setupPlan) {
1732
- setupPlan = await readSetupPlan(setupPath);
1733
- }
1734
- if (!skipSetupPlan && !setupPlan) {
1735
- throw new Error("Setup plan missing.");
1736
- }
1737
- const scanStepUpdate = {};
1738
- if (!skipSetupPlan && setupPlan) {
1739
- if (!initState?.steps.setupEnvSecretsScanned) {
1740
- scanStepUpdate.setupEnvSecretsScanned = true;
1741
- }
1742
- if (!initState?.steps.setupExternalScanned) {
1743
- scanStepUpdate.setupExternalScanned = true;
1744
- }
1745
- if (!initState?.steps.setupExtraArtifactsScanned) {
1746
- scanStepUpdate.setupExtraArtifactsScanned = true;
1747
- }
1748
- if (!initState?.steps.setupPlanScanned) {
1749
- scanStepUpdate.setupPlanScanned = true;
1750
- }
1751
- if (!initState?.steps.servicesPlanScanned) {
1752
- scanStepUpdate.servicesPlanScanned = true;
1753
- }
1754
- }
1755
- if (Object.keys(scanStepUpdate).length > 0) {
1756
- await updateInitState({ steps: scanStepUpdate });
1757
- }
1758
- if (skipSetupPlan) {
1759
- approvedPlan = await readSetupPlan(setupPath);
1760
- }
1761
- if (shouldResume && approvedPlan) {
1762
- const backfillStepUpdate = {};
1763
- if (!initState?.steps.setupEnvSecretsScanned) {
1764
- backfillStepUpdate.setupEnvSecretsScanned = true;
1765
- }
1766
- if (!initState?.steps.setupExternalScanned) {
1767
- backfillStepUpdate.setupExternalScanned = true;
1768
- }
1769
- if (!initState?.steps.setupExtraArtifactsScanned) {
1770
- backfillStepUpdate.setupExtraArtifactsScanned = true;
1771
- }
1772
- if (!initState?.steps.setupPlanScanned) {
1773
- backfillStepUpdate.setupPlanScanned = true;
1774
- }
1775
- if (!initState?.steps.servicesPlanScanned) {
1776
- backfillStepUpdate.servicesPlanScanned = true;
1777
- }
1778
- if (Object.keys(backfillStepUpdate).length > 0) {
1779
- await updateInitState({ steps: backfillStepUpdate });
1780
- }
1781
- }
1782
- if (nonInteractive) {
1783
- if (setupPlan)
1784
- approvedPlan = setupPlan;
1785
- }
1786
- else if (!skipSetupPlan && setupPlan) {
1787
- if (!process.stdin.isTTY || parsed.json) {
1788
- throw new Error("Interactive terminal required to approve setup.");
1789
- }
1790
- const statCache = new Map();
1791
- const readPathInfo = async (candidatePath) => {
1792
- const cached = statCache.get(candidatePath);
1793
- if (cached)
1794
- return cached;
1795
- const resolved = path.isAbsolute(candidatePath)
1796
- ? candidatePath
1797
- : path.resolve(repoRoot, candidatePath);
1798
- const relative = toRepoRelativePath(repoRoot, candidatePath);
1799
- const outsideRepo = isOutsideRepoPath(relative);
1800
- let isDirectory = false;
1801
- try {
1802
- const stat = await fs.stat(resolved);
1803
- isDirectory = stat.isDirectory();
1804
- }
1805
- catch {
1806
- isDirectory = false;
1807
- }
1808
- const info = { relative, outsideRepo, isDirectory };
1809
- statCache.set(candidatePath, info);
1810
- return info;
1811
- };
1812
- const buildApprovalSummary = async (plan) => {
1813
- const lines = [];
1814
- lines.push("Setup");
1815
- lines.push(`- .env files: ${plan.envFiles.length}`);
1816
- for (const entry of plan.envFiles) {
1817
- lines.push(` - ${toRepoRelativePath(repoRoot, entry.path)}`);
1818
- }
1819
- lines.push(`- Secret/config files: ${plan.secretFiles.length}`);
1820
- for (const entry of plan.secretFiles) {
1821
- lines.push(` - ${toRepoRelativePath(repoRoot, entry.path)}`);
1822
- }
1823
- lines.push(`- Other artifacts: ${plan.extraArtifacts.length}`);
1824
- for (const entry of plan.extraArtifacts) {
1825
- lines.push(` - ${toRepoRelativePath(repoRoot, entry.path)}`);
1826
- }
1827
- lines.push(`- External dependencies: ${plan.externalDependencies.length}`);
1828
- for (const entry of plan.externalDependencies) {
1829
- lines.push(` - ${entry.version ? `${entry.name}@${entry.version}` : entry.name}`);
1830
- }
1831
- lines.push(`- External config/secret files: ${plan.externalConfigs.length}`);
1832
- for (const entry of plan.externalConfigs) {
1833
- lines.push(` - ${toRepoRelativePath(repoRoot, entry.path)}`);
1834
- }
1835
- lines.push("");
1836
- lines.push("Services");
1837
- lines.push(`- App entrypoints: ${plan.services.appEntrypoints.length}`);
1838
- for (const entry of plan.services.appEntrypoints) {
1839
- lines.push(` - ${entry.command}`);
1840
- }
1841
- lines.push(`- Background services: ${plan.services.backgroundServices.length}`);
1842
- for (const entry of plan.services.backgroundServices) {
1843
- lines.push(` - ${entry.name}`);
1844
- }
1845
- const candidatePaths = new Set();
1846
- for (const entry of [
1847
- ...plan.envFiles,
1848
- ...plan.secretFiles,
1849
- ...plan.extraArtifacts,
1850
- ...plan.externalConfigs,
1851
- ]) {
1852
- candidatePaths.add(entry.path);
1853
- }
1854
- const outsideRepoPaths = [];
1855
- const directoryPaths = [];
1856
- for (const candidatePath of candidatePaths) {
1857
- const info = await readPathInfo(candidatePath);
1858
- if (info.outsideRepo)
1859
- outsideRepoPaths.push(info.relative);
1860
- if (info.isDirectory)
1861
- directoryPaths.push(info.relative);
1862
- }
1863
- outsideRepoPaths.sort();
1864
- directoryPaths.sort();
1865
- const hasRisk = outsideRepoPaths.length > 0 || directoryPaths.length > 0;
1866
- const warningLines = [];
1867
- if (outsideRepoPaths.length > 0) {
1868
- warningLines.push("Outside repo:");
1869
- for (const entry of outsideRepoPaths) {
1870
- warningLines.push(`- ${entry}`);
1871
- }
1872
- warningLines.push("");
1873
- }
1874
- if (directoryPaths.length > 0) {
1875
- warningLines.push("Directories:");
1876
- for (const entry of directoryPaths) {
1877
- warningLines.push(`- ${entry}`);
1878
- }
1879
- }
1880
- return {
1881
- summary: lines.join("\n"),
1882
- warning: warningLines.length > 0 ? warningLines.join("\n") : null,
1883
- hasRisk,
1884
- };
1885
- };
1886
- let draftSetup = setupPlan;
1887
- while (true) {
1888
- const nextSetup = await promptForPlanApproval({
1889
- plan: setupPlan,
1890
- repoRoot,
1891
- initialPlan: draftSetup,
1892
- });
1893
- const nextServices = await promptForServicesApproval({
1894
- plan: setupPlan.services,
1895
- initialSelection: draftSetup?.services ?? null,
1896
- });
1897
- const nextPlan = { ...nextSetup, services: nextServices };
1898
- const { summary, warning, hasRisk } = await buildApprovalSummary(nextPlan);
1899
- clackNote(summary, "Selected setup requirements");
1900
- if (warning) {
1901
- clackNote(warning, "Special attention");
1902
- }
1903
- const decision = await clackSelect({
1904
- message: "Proceed with these selections?",
1905
- options: [
1906
- { value: "proceed", label: "Proceed" },
1907
- { value: "edit", label: "Edit selections" },
1908
- { value: "cancel", label: "Cancel" },
1909
- ],
1910
- initialValue: "proceed",
1911
- });
1912
- if (isCancel(decision) || decision === "cancel") {
1913
- throwInitCanceled();
1914
- }
1915
- if (decision === "edit") {
1916
- draftSetup = nextPlan;
1917
- continue;
1918
- }
1919
- if (hasRisk) {
1920
- const confirmed = await clackConfirm({
1921
- message: "You selected items outside the repo and/or directories. Continue?",
1922
- active: "Continue",
1923
- inactive: "Edit selections",
1924
- initialValue: true,
1925
- });
1926
- if (isCancel(confirmed)) {
1927
- throwInitCanceled();
1928
- }
1929
- if (!confirmed) {
1930
- draftSetup = nextPlan;
1931
- continue;
1932
- }
1933
- }
1934
- approvedPlan = nextPlan;
1935
- break;
1936
- }
1937
- }
1938
- if (!approvedPlan) {
1939
- throw new Error("Setup plan missing.");
1940
- }
1941
- const ensuredPlan = approvedPlan;
1942
- if (!skipSetupPlan) {
1943
- await writeSetupPlan(setupPath, ensuredPlan);
1944
- await updateInitState({ steps: { setupPlanWritten: true } });
1945
- }
1946
- if (!skipSetupUpload) {
1947
- setupArtifacts = await runInitStep({
1948
- enabled: progressEnabled,
1949
- title: "Packaging setup artifacts",
1950
- fn: async () => await createSetupArtifacts({
1951
- repoRoot,
1952
- plan: ensuredPlan,
1953
- outputDir: setupDir,
1954
- tempDir: setupTempDir,
1955
- homeDir: localHomeDir,
1956
- }),
1957
- });
1958
- }
1959
- }
1960
- finally {
1961
- await fs.rm(setupTempDir, { recursive: true, force: true });
1962
- }
1963
- if (!approvedPlan) {
1964
- throw new Error("Setup plan missing.");
1965
- }
1966
- const skipProvision = shouldResume && initState?.steps.workdirProvisioned;
1967
- if (skipProvision && !skipSetupUpload) {
1968
- await runInitStep({
1969
- enabled: progressEnabled,
1970
- title: "Uploading setup plan",
1971
- fn: async ({ status }) => {
1972
- await uploadSetupPlan({
1973
- client,
1974
- canonical,
1975
- localSetupPath: setupPath,
1976
- remoteSetupPath,
1977
- localArtifactsBundlePath: setupArtifacts?.bundlePath ?? null,
1978
- localArtifactsManifestPath: setupArtifacts?.manifestPath ?? null,
1979
- remoteArtifactsBundlePath: setupArtifacts
1980
- ? path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.tgz")
1981
- : null,
1982
- remoteArtifactsManifestPath: setupArtifacts
1983
- ? path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.json")
1984
- : null,
1985
- status,
1986
- });
1987
- },
1988
- });
1989
- await updateInitState({ steps: { setupUploaded: true } });
1990
- await updateRegistryProjectStatus(resolveInitStatus(initState?.steps, initState?.complete));
1991
- }
1992
- else if (!skipProvision || !skipSetupUpload) {
1993
- const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "devbox-init-"));
1994
- const bundlePath = path.join(tempDir, "repo.bundle");
1995
- const gitMetaPath = path.join(tempDir, "git-meta.tgz");
1996
- const gitMetaListPath = path.join(tempDir, "git-meta.list");
1997
- const stagedPatchPath = path.join(tempDir, "staged.patch");
1998
- const unstagedPatchPath = path.join(tempDir, "unstaged.patch");
1999
- const untrackedPath = path.join(tempDir, "untracked.tgz");
2000
- const untrackedListPath = path.join(tempDir, "untracked.list");
2001
- const globalGitConfigSources = await readGlobalGitConfigFiles(repoRoot);
2002
- const globalGitConfigMappings = mapGlobalGitConfigDestinations(globalGitConfigSources, localHomeDir);
2003
- try {
2004
- const remoteBundlePath = "/home/sprite/.devbox/upload.bundle";
2005
- const remoteGitMetaPath = "/home/sprite/.devbox/git-meta.tgz";
2006
- const remoteStagedPatchPath = "/home/sprite/.devbox/staged.patch";
2007
- const remoteUnstagedPatchPath = "/home/sprite/.devbox/unstaged.patch";
2008
- const remoteUntrackedPath = "/home/sprite/.devbox/untracked.tgz";
2009
- await runInitStep({
2010
- enabled: progressEnabled,
2011
- title: "Preparing remote directories",
2012
- fn: async ({ status }) => {
2013
- status.stage("Checking remote git");
2014
- const gitCheck = await client.exec(canonical, [
2015
- "/bin/bash",
2016
- "-lc",
2017
- "git --version",
2018
- ]);
2019
- if (gitCheck.exitCode !== 0) {
2020
- const details = gitCheck.stderr || gitCheck.stdout || "";
2021
- throw new Error(details
2022
- ? `Remote git unavailable: ${details.trim()}`
2023
- : "Remote git unavailable");
2024
- }
2025
- const remoteDirs = new Set();
2026
- remoteDirs.add(path.posix.dirname(remoteBundlePath));
2027
- remoteDirs.add(path.posix.dirname(remoteGitMetaPath));
2028
- remoteDirs.add(path.posix.dirname(remoteStagedPatchPath));
2029
- remoteDirs.add(path.posix.dirname(remoteUnstagedPatchPath));
2030
- remoteDirs.add(path.posix.dirname(remoteUntrackedPath));
2031
- for (const mapping of globalGitConfigMappings) {
2032
- remoteDirs.add(path.posix.dirname(mapping.dest));
2033
- }
2034
- if (remoteDirs.size > 0) {
2035
- status.stage("Preparing remote directories");
2036
- const prepResult = await client.exec(canonical, [
2037
- "/bin/bash",
2038
- "-lc",
2039
- `mkdir -p ${[...remoteDirs].map(shellQuote).join(" ")}`,
2040
- ]);
2041
- if (prepResult.exitCode !== 0) {
2042
- throw new Error(prepResult.stderr || "Failed to prepare remote dirs");
2043
- }
2044
- }
2045
- },
2046
- });
2047
- const { headState, gitCommonDir, worktreeState, copyWorktree } = await runInitStep({
2048
- enabled: progressEnabled,
2049
- title: "Inspecting repo state",
2050
- fn: async ({ status }) => {
2051
- const headState = await readHeadState(repoRoot);
2052
- const resolved = await resolveGitCommonDir(repoRoot);
2053
- const worktreeState = await readWorktreeState(repoRoot);
2054
- const hasWorktreeChanges = worktreeState.staged.length > 0 ||
2055
- worktreeState.unstaged.length > 0 ||
2056
- worktreeState.untracked.length > 0;
2057
- let copyWorktree = false;
2058
- if (hasWorktreeChanges && process.stdin.isTTY && !parsed.json) {
2059
- status.stop();
2060
- copyWorktree = await confirmCopyWorktree(worktreeState);
2061
- status.stage("Inspecting repo state");
2062
- }
2063
- else if (hasWorktreeChanges) {
2064
- copyWorktree = true;
2065
- }
2066
- return {
2067
- headState,
2068
- gitCommonDir: resolved.commonDir,
2069
- worktreeState,
2070
- copyWorktree,
2071
- };
2072
- },
2073
- });
2074
- const packaged = await runInitStep({
2075
- enabled: progressEnabled,
2076
- title: "Packaging repo",
2077
- fn: async ({ status }) => {
2078
- let gitMetaCreated = false;
2079
- let stagedPatchCreated = false;
2080
- let unstagedPatchCreated = false;
2081
- let untrackedCreated = false;
2082
- status.stage("Packaging repo bundle");
2083
- await runCommand(repoRoot, "git", [
2084
- "bundle",
2085
- "create",
2086
- bundlePath,
2087
- "--all",
2088
- ]);
2089
- status.stage("Packaging git metadata");
2090
- gitMetaCreated = await createGitMetaArchive(gitCommonDir, gitMetaPath, gitMetaListPath);
2091
- if (copyWorktree) {
2092
- status.stage("Packaging repo changes");
2093
- stagedPatchCreated = await writePatch(repoRoot, ["diff", "--binary", "--cached"], stagedPatchPath);
2094
- unstagedPatchCreated = await writePatch(repoRoot, ["diff", "--binary"], unstagedPatchPath);
2095
- untrackedCreated = await createFileListArchive(repoRoot, worktreeState.untracked, untrackedPath, untrackedListPath);
2096
- }
2097
- return {
2098
- gitMetaCreated,
2099
- stagedPatchCreated,
2100
- unstagedPatchCreated,
2101
- untrackedCreated,
2102
- };
2103
- },
2104
- });
2105
- await runInitStep({
2106
- enabled: progressEnabled,
2107
- title: "Uploading repo",
2108
- fn: async ({ status }) => {
2109
- const uploadItems = [
2110
- {
2111
- label: "repo bundle",
2112
- localPath: bundlePath,
2113
- remotePath: remoteBundlePath,
2114
- },
2115
- ];
2116
- if (packaged.gitMetaCreated) {
2117
- uploadItems.push({
2118
- label: "git metadata",
2119
- localPath: gitMetaPath,
2120
- remotePath: remoteGitMetaPath,
2121
- });
2122
- }
2123
- if (packaged.stagedPatchCreated) {
2124
- uploadItems.push({
2125
- label: "staged changes",
2126
- localPath: stagedPatchPath,
2127
- remotePath: remoteStagedPatchPath,
2128
- });
2129
- }
2130
- if (packaged.unstagedPatchCreated) {
2131
- uploadItems.push({
2132
- label: "unstaged changes",
2133
- localPath: unstagedPatchPath,
2134
- remotePath: remoteUnstagedPatchPath,
2135
- });
2136
- }
2137
- if (packaged.untrackedCreated) {
2138
- uploadItems.push({
2139
- label: "untracked files",
2140
- localPath: untrackedPath,
2141
- remotePath: remoteUntrackedPath,
2142
- });
2143
- }
2144
- for (const mapping of globalGitConfigMappings) {
2145
- uploadItems.push({
2146
- label: `git config (${path.basename(mapping.source)})`,
2147
- localPath: mapping.source,
2148
- remotePath: mapping.dest,
2149
- });
2150
- }
2151
- const plannedUploads = await Promise.all(uploadItems.map(async (item) => {
2152
- const stats = await fs.stat(item.localPath);
2153
- return { ...item, size: stats.size };
2154
- }));
2155
- const totalBytes = plannedUploads.reduce((sum, item) => sum + item.size, 0);
2156
- let uploadedBytes = 0;
2157
- const updateProgress = (currentFileBytes, detail) => status.byteProgress({
2158
- title: "Uploading repo",
2159
- uploadedBytes: uploadedBytes + currentFileBytes,
2160
- totalBytes,
2161
- detail,
2162
- });
2163
- for (const [index, upload] of plannedUploads.entries()) {
2164
- const detail = `${upload.label} (${index + 1}/${plannedUploads.length})`;
2165
- const fileData = await fs.readFile(upload.localPath);
2166
- updateProgress(0, detail);
2167
- await client.writeFile(canonical, upload.remotePath, fileData, {
2168
- onProgress: (fileUploadedBytes, fileTotalBytes) => {
2169
- const bounded = Math.min(fileUploadedBytes, Math.max(fileData.length, fileTotalBytes));
2170
- updateProgress(bounded, detail);
2171
- },
2172
- });
2173
- uploadedBytes += fileData.length;
2174
- updateProgress(fileData.length, detail);
2175
- }
2176
- status.byteProgress({
2177
- title: "Uploading repo",
2178
- uploadedBytes: totalBytes,
2179
- totalBytes,
2180
- detail: "completed",
2181
- });
2182
- },
2183
- });
2184
- await runInitStep({
2185
- enabled: progressEnabled,
2186
- title: "Provisioning workdir",
2187
- fn: async () => {
2188
- const backup = `${expandedWorkdir}.bak-${Date.now()}`;
2189
- const checkoutCommand = headState.branch
2190
- ? `git checkout -B ${shellQuote(headState.branch)} ${shellQuote(headState.commit)}`
2191
- : `git checkout --detach ${shellQuote(headState.commit)}`;
2192
- const remoteCommand = [
2193
- "set -euo pipefail",
2194
- "unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE",
2195
- `if [ -d ${shellQuote(expandedWorkdir)} ]; then`,
2196
- parsed.force
2197
- ? ` mv ${shellQuote(expandedWorkdir)} ${shellQuote(backup)}`
2198
- : ` echo "Target exists: ${expandedWorkdir}" >&2; exit 1`,
2199
- "fi",
2200
- `mkdir -p ${shellQuote(path.dirname(expandedWorkdir))}`,
2201
- `git init -b devbox-init ${shellQuote(expandedWorkdir)}`,
2202
- `cd ${shellQuote(expandedWorkdir)}`,
2203
- `git fetch ${shellQuote(remoteBundlePath)} 'refs/*:refs/*'`,
2204
- `if [ -f ${shellQuote(remoteGitMetaPath)} ]; then`,
2205
- ` tar -xzf ${shellQuote(remoteGitMetaPath)} -C .git`,
2206
- "fi",
2207
- checkoutCommand,
2208
- `if [ -f ${shellQuote(remoteStagedPatchPath)} ]; then`,
2209
- ` git apply --index ${shellQuote(remoteStagedPatchPath)}`,
2210
- "fi",
2211
- `if [ -f ${shellQuote(remoteUnstagedPatchPath)} ]; then`,
2212
- ` git apply ${shellQuote(remoteUnstagedPatchPath)}`,
2213
- "fi",
2214
- `if [ -f ${shellQuote(remoteUntrackedPath)} ]; then`,
2215
- ` tar -xzf ${shellQuote(remoteUntrackedPath)} -C .`,
2216
- "fi",
2217
- ].join("\n");
2218
- const execResult = await client.exec(canonical, [
2219
- "/bin/bash",
2220
- "--noprofile",
2221
- "--norc",
2222
- "-e",
2223
- "-u",
2224
- "-o",
2225
- "pipefail",
2226
- "-c",
2227
- remoteCommand,
2228
- ]);
2229
- if (execResult.exitCode !== 0) {
2230
- throw new Error(execResult.stderr || "Remote init failed");
2231
- }
2232
- await updateInitState({ steps: { workdirProvisioned: true } });
2233
- },
2234
- });
2235
- if (!skipSetupUpload) {
2236
- await runInitStep({
2237
- enabled: progressEnabled,
2238
- title: "Uploading setup plan",
2239
- fn: async ({ status }) => {
2240
- await uploadSetupPlan({
2241
- client,
2242
- canonical,
2243
- localSetupPath: setupPath,
2244
- remoteSetupPath,
2245
- localArtifactsBundlePath: setupArtifacts?.bundlePath ?? null,
2246
- localArtifactsManifestPath: setupArtifacts?.manifestPath ?? null,
2247
- remoteArtifactsBundlePath: setupArtifacts
2248
- ? path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.tgz")
2249
- : null,
2250
- remoteArtifactsManifestPath: setupArtifacts
2251
- ? path.posix.join(expandedWorkdir, ".devbox", "setup-artifacts.json")
2252
- : null,
2253
- status,
2254
- });
2255
- },
2256
- });
2257
- await updateInitState({ steps: { setupUploaded: true } });
2258
- await updateRegistryProjectStatus(resolveInitStatus(initState?.steps, initState?.complete));
2259
- }
2260
- }
2261
- finally {
2262
- await fs.rm(tempDir, { recursive: true, force: true });
2263
- }
2264
- }
2265
- await runInitStep({
2266
- enabled: progressEnabled,
2267
- title: "Ensuring workdir ownership",
2268
- fn: async ({ status }) => {
2269
- await retryInitStep({
2270
- status,
2271
- title: "Ensuring workdir ownership",
2272
- fn: async () => await ensureWorkdirOwnership({
2273
- client,
2274
- canonical,
2275
- workdir: expandedWorkdir,
2276
- status,
2277
- }),
2278
- });
2279
- },
2280
- });
2281
- const skipGitSafeDirectory = shouldResume && initState?.steps.gitSafeDirectoryConfigured;
2282
- if (!skipGitSafeDirectory) {
2283
- await runInitStep({
2284
- enabled: progressEnabled,
2285
- title: "Configuring git safe.directory",
2286
- fn: async ({ status }) => {
2287
- await ensureGitSafeDirectory({
2288
- client,
2289
- canonical,
2290
- workdir: expandedWorkdir,
2291
- status,
2292
- });
2293
- await updateInitState({
2294
- steps: { gitSafeDirectoryConfigured: true },
2295
- });
2296
- },
2297
- });
2298
- }
2299
734
  const skipSshdConfig = shouldResume && initState?.steps.sshdConfigured;
2300
735
  if (!skipSshdConfig) {
2301
736
  await runInitStep({
@@ -2342,143 +777,6 @@ export const runInit = async (args) => {
2342
777
  },
2343
778
  });
2344
779
  }
2345
- const skipSshAuth = nonInteractive || (shouldResume && initState?.steps.sshAuthConfigured);
2346
- if (!skipSshAuth) {
2347
- const { remoteOrigin, remoteInfo } = await runInitStep({
2348
- enabled: progressEnabled,
2349
- title: "Checking git remote for SSH auth",
2350
- fn: async () => {
2351
- const remoteOrigin = await readRemoteOrigin(client, canonical, expandedWorkdir);
2352
- const remoteInfo = remoteOrigin ? parseGitRemote(remoteOrigin) : null;
2353
- return { remoteOrigin, remoteInfo };
2354
- },
2355
- });
2356
- if (!remoteOrigin) {
2357
- if (!parsed.json) {
2358
- console.warn("Warning: unable to detect remote origin on sprite. Skipping SSH setup.");
2359
- }
2360
- }
2361
- else if (!remoteInfo) {
2362
- if (!parsed.json) {
2363
- console.warn(`Warning: unrecognized git remote format (${remoteOrigin}). Skipping SSH setup.`);
2364
- }
2365
- }
2366
- else {
2367
- let activeOrigin = remoteOrigin;
2368
- if (remoteInfo.protocol === "https") {
2369
- if (!parsed.json) {
2370
- clackNote([
2371
- `Origin is HTTPS (${remoteOrigin}).`,
2372
- "",
2373
- "This only changes the remote devbox checkout (your local checkout is unchanged).",
2374
- "SSH key auth on the remote devbox will not work unless this remote uses SSH:",
2375
- remoteInfo.sshUrl,
2376
- ].join("\n"), "Git remote");
2377
- }
2378
- const shouldSwitch = await clackSelect({
2379
- message: `Use SSH auth for ${remoteInfo.host} on the remote devbox checkout?`,
2380
- options: [
2381
- {
2382
- value: "switch",
2383
- label: "Use SSH on remote devbox (Recommended)",
2384
- },
2385
- { value: "keep", label: "Keep HTTPS (Skip SSH auth setup)" },
2386
- { value: "cancel", label: "Cancel init" },
2387
- ],
2388
- initialValue: "switch",
2389
- });
2390
- if (isCancel(shouldSwitch) || shouldSwitch === "cancel") {
2391
- throwInitCanceled();
2392
- }
2393
- if (shouldSwitch === "keep") {
2394
- if (!parsed.json) {
2395
- console.warn("Skipping SSH auth setup. Configure git credentials for this repo manually before pulling or pushing.");
2396
- }
2397
- await updateInitState({
2398
- steps: { sshAuthConfigured: true },
2399
- });
2400
- activeOrigin = "";
2401
- }
2402
- else {
2403
- await runInitStep({
2404
- enabled: progressEnabled,
2405
- title: "Updating remote devbox checkout to SSH git remote",
2406
- fn: async () => {
2407
- await setRemoteOrigin(client, canonical, expandedWorkdir, remoteInfo.sshUrl);
2408
- },
2409
- });
2410
- activeOrigin = remoteInfo.sshUrl;
2411
- }
2412
- }
2413
- if (activeOrigin) {
2414
- const publicKey = await runInitStep({
2415
- enabled: progressEnabled,
2416
- title: "Generating SSH key",
2417
- fn: async () => await ensureSshKey(client, canonical, `${alias}@devbox`),
2418
- });
2419
- await runInitStep({
2420
- enabled: progressEnabled,
2421
- title: "Updating SSH config",
2422
- fn: async () => await ensureSshConfig(client, canonical, remoteInfo.host),
2423
- });
2424
- if (!parsed.json) {
2425
- clackNote(publicKey, `Add this SSH public key to ${remoteInfo.host}`);
2426
- const copied = await copyToClipboard(publicKey);
2427
- if (copied) {
2428
- clackLog.success("Copied SSH public key to clipboard.");
2429
- }
2430
- else {
2431
- clackLog.warn("Could not copy the SSH key automatically.");
2432
- }
2433
- const shouldOpen = await promptBeforeOpenBrowser({
2434
- url: remoteInfo.settingsUrl,
2435
- title: `${remoteInfo.host} SSH key page`,
2436
- consequence: [
2437
- "Skipping browser open.",
2438
- "You must add the SSH key before git pull/push will work via SSH.",
2439
- ].join(" "),
2440
- });
2441
- if (shouldOpen) {
2442
- const opened = openBrowser(remoteInfo.settingsUrl);
2443
- if (!opened) {
2444
- clackLog.warn("Unable to open the browser automatically.");
2445
- }
2446
- }
2447
- }
2448
- const added = await clackConfirm({
2449
- message: "Have you added the SSH key?",
2450
- active: "Yes, verify now",
2451
- inactive: "Not yet",
2452
- initialValue: true,
2453
- });
2454
- if (isCancel(added)) {
2455
- throwInitCanceled();
2456
- }
2457
- if (!added) {
2458
- if (!parsed.json) {
2459
- console.warn("Skipping SSH verification. Add the key and re-run `dvb init --resume` to verify.");
2460
- }
2461
- }
2462
- else {
2463
- const verified = await runInitStep({
2464
- enabled: progressEnabled,
2465
- title: "Verifying git SSH auth",
2466
- fn: async () => await verifySshAuth(client, canonical, remoteInfo.host, expandedWorkdir),
2467
- });
2468
- if (!verified) {
2469
- if (!parsed.json) {
2470
- console.warn("SSH auth verification failed. Confirm the key is added and that the repo access is granted.");
2471
- }
2472
- }
2473
- else {
2474
- await updateInitState({
2475
- steps: { sshAuthConfigured: true },
2476
- });
2477
- }
2478
- }
2479
- }
2480
- }
2481
- }
2482
780
  const weztermMuxPresent = await runInitStep({
2483
781
  enabled: progressEnabled,
2484
782
  title: "Ensuring WezTerm mux server (optional)",
@@ -2557,88 +855,6 @@ export const runInit = async (args) => {
2557
855
  },
2558
856
  });
2559
857
  }
2560
- if (!skipServicesConfig) {
2561
- await runInitStep({
2562
- enabled: progressEnabled,
2563
- title: "Writing devbox.toml services",
2564
- fn: async () => {
2565
- await writeRemoteServicesToml({
2566
- client,
2567
- canonical,
2568
- workdir: expandedWorkdir,
2569
- services: approvedPlan.services.backgroundServices,
2570
- });
2571
- await updateInitState({ steps: { servicesConfigWritten: true } });
2572
- },
2573
- });
2574
- }
2575
- const projectMeta = {
2576
- fingerprint,
2577
- canonical,
2578
- alias,
2579
- workdir,
2580
- origin: projectOrigin,
2581
- createdAt: projectCreatedAt,
2582
- };
2583
- const projectMetaPath = `/home/sprite/.devbox/projects/${fingerprint}.json`;
2584
- await runInitStep({
2585
- enabled: progressEnabled,
2586
- title: "Registering project",
2587
- fn: async () => {
2588
- let existing = {};
2589
- try {
2590
- const raw = await client.readFile(canonical, {
2591
- path: projectMetaPath,
2592
- });
2593
- const parsed = JSON.parse(Buffer.from(raw).toString("utf8"));
2594
- if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
2595
- existing = parsed;
2596
- }
2597
- }
2598
- catch (error) {
2599
- if (!(error instanceof SpritesApiError && error.status === 404)) {
2600
- throw error;
2601
- }
2602
- }
2603
- const existingCheckpoints = existing.checkpoints &&
2604
- typeof existing.checkpoints === "object" &&
2605
- !Array.isArray(existing.checkpoints)
2606
- ? existing.checkpoints
2607
- : {};
2608
- const stateCheckpoints = initState?.checkpoints ?? {};
2609
- const merged = {
2610
- ...existing,
2611
- ...projectMeta,
2612
- checkpoints: { ...existingCheckpoints, ...stateCheckpoints },
2613
- };
2614
- await client.writeFile(canonical, projectMetaPath, Buffer.from(JSON.stringify(merged, null, 2)));
2615
- },
2616
- });
2617
- await runInitStep({
2618
- enabled: progressEnabled,
2619
- title: "Writing local metadata",
2620
- fn: async () => {
2621
- await writeRepoMarker(projectDir, { fingerprint, canonical, alias });
2622
- },
2623
- });
2624
- const skipSetupArtifactsStage = skipCodexApply || (shouldResume && initState?.steps.setupArtifactsStaged);
2625
- if (!skipSetupArtifactsStage) {
2626
- await runInitStep({
2627
- enabled: progressEnabled,
2628
- title: "Staging setup artifacts",
2629
- fn: async ({ status }) => {
2630
- status.stage("Copying repo artifacts and staging external files");
2631
- await stageRemoteSetupArtifacts({
2632
- client,
2633
- canonical,
2634
- workdir: expandedWorkdir,
2635
- artifactsBundlePath: remoteArtifactsBundlePath,
2636
- artifactsManifestPath: remoteArtifactsManifestPath,
2637
- });
2638
- },
2639
- });
2640
- await updateInitState({ steps: { setupArtifactsStaged: true } });
2641
- }
2642
858
  if (!skipCodexCliEnsure) {
2643
859
  await runInitStep({
2644
860
  enabled: progressEnabled,
@@ -2664,138 +880,70 @@ export const runInit = async (args) => {
2664
880
  },
2665
881
  });
2666
882
  }
2667
- if (!skipCodexApply) {
2668
- await runInitStep({
2669
- enabled: progressEnabled,
2670
- title: "Snapshotting filesystem (pre-setup)",
2671
- fn: async ({ status, fail, ok }) => {
2672
- try {
2673
- const checkpoint = await retryInitStep({
2674
- status,
2675
- title: "Snapshotting filesystem (pre-setup)",
2676
- fn: async () => await recordCodexCheckpoint({
2677
- client,
2678
- canonical,
2679
- phase: "preCodexSetup",
2680
- }),
2681
- });
2682
- if (checkpoint.id) {
2683
- ok(`Snapshot created: ${checkpoint.id}`);
2684
- }
2685
- }
2686
- catch (error) {
2687
- logger.warn("init_checkpoint_create_failed", {
2688
- box: canonical,
2689
- fingerprint,
2690
- phase: "pre-codex-setup",
2691
- error: error instanceof Error ? error.message : String(error),
2692
- });
2693
- fail("Snapshotting filesystem (pre-setup) (failed)");
2694
- throw error;
2695
- }
2696
- },
2697
- });
2698
- if (!skipServicesEnable) {
2699
- await runInitStep({
2700
- enabled: progressEnabled,
2701
- title: "Enabling devbox services",
2702
- fn: async ({ status }) => {
2703
- await enableRemoteServices({
2704
- client,
2705
- canonical,
2706
- services: approvedPlan.services.backgroundServices,
2707
- status,
2708
- });
2709
- await updateInitState({ steps: { servicesEnabled: true } });
2710
- },
2711
- });
2712
- }
2713
- await runInitStep({
2714
- enabled: progressEnabled,
2715
- title: "Running Codex setup",
2716
- fn: async ({ status }) => {
2717
- await runRemoteCodexSetup({
2718
- client,
2719
- canonical,
2720
- expandedWorkdir,
2721
- remoteSetupPath,
2722
- remoteArtifactsBundlePath,
2723
- remoteArtifactsManifestPath,
2724
- socketInfo,
2725
- status,
2726
- pathSetup,
2727
- entrypoints: approvedPlan.services.appEntrypoints,
2728
- emitCodexOutput: !parsed.json,
2729
- ...(initCodexProxyOptions
2730
- ? { proxyOptions: initCodexProxyOptions }
2731
- : {}),
2732
- });
2733
- },
2734
- });
2735
- await updateInitState({ steps: { codexApplied: true } });
2736
- await updateRegistryProjectStatus(resolveInitStatus(initState?.steps, initState?.complete));
2737
- await runInitStep({
2738
- enabled: progressEnabled,
2739
- title: "Snapshotting filesystem (post-setup)",
2740
- fn: async ({ status, fail, ok }) => {
2741
- try {
2742
- const checkpoint = await retryInitStep({
2743
- status,
2744
- title: "Snapshotting filesystem (post-setup)",
2745
- fn: async () => await recordCodexCheckpoint({
2746
- client,
2747
- canonical,
2748
- phase: "postCodexSetup",
2749
- }),
2750
- });
2751
- if (checkpoint.id) {
2752
- ok(`Snapshot created: ${checkpoint.id}`);
2753
- }
2754
- }
2755
- catch (error) {
2756
- logger.warn("init_checkpoint_create_failed", {
2757
- box: canonical,
2758
- fingerprint,
2759
- phase: "post-codex-setup",
2760
- error: error instanceof Error ? error.message : String(error),
2761
- });
2762
- fail("Snapshotting filesystem (post-setup) (failed)");
2763
- throw error;
2764
- }
2765
- },
2766
- });
2767
- }
2768
- const finalStatus = resolveInitStatus(initState?.steps, initState?.complete);
2769
- const isComplete = finalStatus === "complete";
2770
- await runInitStep({
2771
- enabled: progressEnabled,
2772
- title: "Finalizing init",
2773
- fn: async () => {
2774
- await updateInitState({ complete: isComplete });
2775
- await updateRegistryProjectStatus(finalStatus);
2776
- },
883
+ const { setupPath, finalApprovedPlan, setupArtifacts, skipServicesConfig, skipServicesEnable, skipSetupUpload, } = await runSetupPlanFlow({
884
+ projectDir,
885
+ repoRoot,
886
+ localHomeDir,
887
+ shouldResume,
888
+ nonInteractive,
889
+ progressEnabled,
890
+ parsed,
891
+ getInitState,
892
+ updateInitState,
893
+ ...(initCodexProxyOptions ? { initCodexProxyOptions } : {}),
894
+ throwInitCanceled,
895
+ });
896
+ await runProvisionFlow({
897
+ client,
898
+ canonical,
899
+ expandedWorkdir,
900
+ repoRoot,
901
+ localHomeDir,
902
+ shouldResume,
903
+ parsed,
904
+ progressEnabled,
905
+ setupPath,
906
+ remoteSetupPath,
907
+ setupArtifacts,
908
+ skipSetupUpload,
909
+ remoteArtifactsBundlePath,
910
+ remoteArtifactsManifestPath,
911
+ remoteArtifactsPartsDescriptorPath,
912
+ getInitState,
913
+ updateInitState,
914
+ refreshRegistryProjectStatus: async () => await updateRegistryProjectStatus(resolveInitStatus(initState?.steps, initState?.complete)),
915
+ });
916
+ await runFinalizeFlow({
917
+ client,
918
+ canonical,
919
+ alias,
920
+ workdir,
921
+ projectDir,
922
+ fingerprint,
923
+ projectCreatedAt,
924
+ ...(projectOrigin !== undefined ? { projectOrigin } : {}),
925
+ finalApprovedPlan,
926
+ shouldResume,
927
+ nonInteractive,
928
+ progressEnabled,
929
+ parsed,
930
+ skipCodexApply,
931
+ skipServicesConfig,
932
+ skipServicesEnable,
933
+ remoteSetupPath,
934
+ remoteArtifactsBundlePath,
935
+ remoteArtifactsManifestPath,
936
+ socketInfo,
937
+ pathSetup,
938
+ ...(initCodexProxyOptions ? { initCodexProxyOptions } : {}),
939
+ throwInitCanceled,
940
+ getInitState,
941
+ updateInitState,
942
+ updateRegistryProjectStatus,
943
+ resolveInitStatus,
944
+ retryInitStep,
945
+ recordCodexCheckpoint,
2777
946
  });
2778
- if (parsed.json) {
2779
- console.log(JSON.stringify({
2780
- ok: true,
2781
- status: finalStatus,
2782
- canonical,
2783
- alias,
2784
- workdir,
2785
- fingerprint,
2786
- }, null, 2));
2787
- return;
2788
- }
2789
- if (!isComplete) {
2790
- console.log(`devbox initialized (setup incomplete): ${alias} -> ${canonical}`);
2791
- console.log(`workdir: ${workdir}`);
2792
- console.log("next: run `dvb init --resume` from this repo to finish setup");
2793
- console.log("sprites: synced to control plane");
2794
- return;
2795
- }
2796
- console.log(`devbox initialized: ${alias} -> ${canonical}`);
2797
- console.log(`workdir: ${workdir}`);
2798
- console.log("sprites: synced to control plane");
2799
947
  };
2800
948
  await run();
2801
949
  };