@exla/rudder 4.2.8 → 4.2.9

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.
@@ -1,26 +1,48 @@
1
1
  /**
2
2
  * Warm snapshots, overlay forks (Linux) and clone forks (macOS).
3
3
  *
4
- * A warm snapshot is one jj workspace per base commit (`rudderwarm-<key>`)
5
- * where the trusted builder installed dependencies and ran the build once
6
- * (with the shared tool cache, src/tool-cache.ts). It is never written again:
7
- * agents get a fork of it instead of a full checkout. On macOS a fork is an
8
- * APFS copy-on-write clone of the snapshot (see createCloneWorkspace). On
9
- * Linux it is an overlay fork:
4
+ * A warm snapshot holds one base commit's files plus the dependencies and
5
+ * build output the trusted builder made for it (with the shared tool cache,
6
+ * src/tool-cache.ts). It is never written again once ready: agents get a fork
7
+ * of it instead of a full checkout.
8
+ *
9
+ * Snapshots follow trunk. One detached builder per project (`startWarmBuild`
10
+ * spawns this file; launches and integrations start it) watches the base new
11
+ * workers start from (`currentBaseCommit`) and builds a snapshot for it
12
+ * whenever it moves, coalescing quick moves to the latest. A refresh starts
13
+ * from the nearest ready snapshot, so install and build reuse its outputs:
14
+ * - Linux: the new snapshot is a thin OverlayFS layer on top of the previous
15
+ * snapshot's layers. The builder mounts the stack at the fixed path
16
+ * `.warm/build`, applies the base change with one Git two-tree checkout and
17
+ * runs the warm commands there; their writes land in `.warm/<key>/`, and
18
+ * `.warm/<key>.lowerdir` lists the whole stack, top first. A refresh on a
19
+ * stack MAX_LAYERS deep copies the stack's merged view into a plain
20
+ * directory instead (compaction).
21
+ * - macOS: the new snapshot is an APFS clone of the previous one.
22
+ * A project's first snapshot is a jj workspace (`rudderwarm-<key>`). Branch
23
+ * launches never build: they fork the nearest base snapshot and patch the
24
+ * difference.
25
+ *
26
+ * On macOS a fork is an APFS copy-on-write clone of the snapshot (see
27
+ * createCloneWorkspace). On Linux it is an overlay fork:
10
28
  *
11
29
  * A fork is a jj workspace on the `rudder-overlay` working copy, added by
12
30
  * `rudder-jj rudder-fork-add`: its directory on disk holds only `.jj`, the
13
31
  * agent's private OverlayFS layers live in `.jj/working_copy/{upper,work}`,
14
- * and the warm snapshot directory is the lower layer (linked into the shared
15
- * `.warm/lowers/<tree>` cache). Snapshot reads only the upper layer (no tree
16
- * scan); check-out repoints the lower (no copy). Only rudder-jj can load such
17
- * a workspace. Next to it, `<workspace>.fork/` holds:
18
- * - `lower`: path of the warm snapshot (for pruning);
32
+ * and the warm snapshot (its top layer, linked into `.warm/lower-links/<tree>`)
33
+ * is the lower layer. Snapshot reads only the upper layer (no tree scan);
34
+ * check-out repoints the lower (no copy). Only rudder-jj can load such a
35
+ * workspace. Next to it, `<workspace>.fork/` holds:
36
+ * - `lower`: the warm snapshot it was made from (for pruning);
37
+ * - `alias`: the builder's path, where build output may point (see ENTER_SCRIPT);
19
38
  * - `bin/jj`: rudder-jj, first on PATH inside the fork;
20
39
  * - `enter`: runs a command in a private user+mount namespace with the overlay
21
40
  * mounted at the workspace path and `.jj` bound back, as the real user.
22
41
  * The native PTY layer runs every agent command whose cwd has a sibling
23
42
  * `.fork/enter` through it.
43
+ *
44
+ * Pruning keeps the newest snapshot, every layer a fork or a kept snapshot
45
+ * mounts, and deletes the rest.
24
46
  */
25
47
  import fs from "node:fs";
26
48
  import fsp from "node:fs/promises";
@@ -31,7 +53,17 @@ import { runCommand, rudderJjPath } from "./util.js";
31
53
  import { workspacePath } from "./state.js";
32
54
  import { toolCacheEnv } from "./tool-cache.js";
33
55
  export const WARM_WORKSPACE_PREFIX = "rudderwarm-";
34
- const KEEP_READY = 2;
56
+ /** Newest ready snapshots kept even when no fork or newer stack uses them. */
57
+ const KEEP_READY = 1;
58
+ /** A refresh on a stack this deep makes a flat copy instead of another layer. */
59
+ const MAX_LAYERS = 8;
60
+ /** The builder's lock and pid, one per project. */
61
+ const BUILDER_LOCK = "builder.pid";
62
+ const FOLLOW_POLL_MS = 1_000;
63
+ /** The builder exits after this long without the base moving. */
64
+ const FOLLOW_IDLE_MS = 30 * 60_000;
65
+ /** After a refresh, a small base move waits this long (or as long as the refresh took). */
66
+ const REFRESH_GAP_MS = 60_000;
35
67
  /** `.rudder-workspaces/<repo>/` (where agent workspaces live). */
36
68
  function workspacesDir(repoRoot) {
37
69
  return path.dirname(workspacePath(repoRoot, "warm-probe"));
@@ -39,6 +71,18 @@ function workspacesDir(repoRoot) {
39
71
  export function warmDir(repoRoot) {
40
72
  return path.join(workspacesDir(repoRoot), ".warm");
41
73
  }
74
+ /** Where the Linux builder mounts the snapshot it builds, and forks alias themselves. */
75
+ function buildPath(repoRoot) {
76
+ return path.join(warmDir(repoRoot), "build");
77
+ }
78
+ /**
79
+ * Tree links for forks made by this version. Forks from before stacked
80
+ * snapshots use `.warm/lowers` and an enter script that mounts one layer, so
81
+ * they never resolve a tree to a stacked snapshot's top layer.
82
+ */
83
+ function forkLowersRoot(repoRoot) {
84
+ return path.join(warmDir(repoRoot), "lower-links");
85
+ }
42
86
  export function forkStateDir(workspace) {
43
87
  return `${workspace}.fork`;
44
88
  }
@@ -80,6 +124,26 @@ export async function currentBaseCommit(repoRoot) {
80
124
  const id = merge.stdout.trim();
81
125
  return merge.code === 0 && id ? id : null;
82
126
  }
127
+ /** A snapshot directory's layers, top first: the list in `<dir>.lowerdir`, else the directory itself. */
128
+ function layersOf(dir) {
129
+ try {
130
+ const layers = fs.readFileSync(`${dir}.lowerdir`, "utf8").split(/[:\n]/).filter(Boolean);
131
+ if (layers.length > 0)
132
+ return layers;
133
+ }
134
+ catch {
135
+ /* a flat snapshot */
136
+ }
137
+ return [dir];
138
+ }
139
+ function realPath(file) {
140
+ try {
141
+ return fs.realpathSync(file);
142
+ }
143
+ catch {
144
+ return path.resolve(file);
145
+ }
146
+ }
83
147
  export function readySnapshots(repoRoot) {
84
148
  const dir = warmDir(repoRoot);
85
149
  let names = [];
@@ -93,7 +157,7 @@ export function readySnapshots(repoRoot) {
93
157
  for (const name of names.filter((n) => n.endsWith(".ready"))) {
94
158
  try {
95
159
  const snap = JSON.parse(fs.readFileSync(path.join(dir, name), "utf8"));
96
- if (fs.existsSync(path.join(snap.dir, ".jj")))
160
+ if (layersOf(snap.dir).every((layer) => fs.existsSync(layer)))
97
161
  out.push(snap);
98
162
  }
99
163
  catch {
@@ -104,10 +168,30 @@ export function readySnapshots(repoRoot) {
104
168
  }
105
169
  /** Commands from rudder.toml `[warm] commands = [...]`, else detected from lockfiles. */
106
170
  export function warmCommands(dir) {
107
- const configured = readConfiguredCommands(path.join(dir, "rudder.toml"));
171
+ return commandsFor((file) => fs.existsSync(path.join(dir, file)), (file) => {
172
+ try {
173
+ return fs.readFileSync(path.join(dir, file), "utf8");
174
+ }
175
+ catch {
176
+ return null;
177
+ }
178
+ });
179
+ }
180
+ /** warmCommands for a commit's tree, read from the Git store (a stacked build has no full checkout). */
181
+ function warmCommandsAt(gitDir, commit) {
182
+ const listed = spawnSync("git", ["--git-dir", gitDir, "ls-tree", "--name-only", "-z", commit], { encoding: "utf8" });
183
+ const names = new Set(listed.status === 0 ? listed.stdout.split("\0") : []);
184
+ return commandsFor((file) => names.has(file), (file) => {
185
+ if (!names.has(file))
186
+ return null;
187
+ const shown = spawnSync("git", ["--git-dir", gitDir, "show", `${commit}:${file}`], { encoding: "utf8" });
188
+ return shown.status === 0 ? shown.stdout : null;
189
+ });
190
+ }
191
+ function commandsFor(has, read) {
192
+ const configured = readConfiguredCommands(read("rudder.toml"));
108
193
  if (configured)
109
194
  return configured;
110
- const has = (file) => fs.existsSync(path.join(dir, file));
111
195
  const commands = [];
112
196
  if (has("pnpm-lock.yaml"))
113
197
  commands.push("pnpm install --frozen-lockfile");
@@ -119,7 +203,7 @@ export function warmCommands(dir) {
119
203
  commands.push("npm install");
120
204
  if (has("package.json")) {
121
205
  try {
122
- const pkg = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf8"));
206
+ const pkg = JSON.parse(read("package.json") ?? "");
123
207
  if (pkg.scripts?.build)
124
208
  commands.push(has("pnpm-lock.yaml") ? "pnpm run build" : has("yarn.lock") ? "yarn build" : "npm run build");
125
209
  }
@@ -135,26 +219,98 @@ export function warmCommands(dir) {
135
219
  commands.push("python3 -m venv .venv && .venv/bin/pip install -r requirements.txt");
136
220
  return commands;
137
221
  }
138
- function readConfiguredCommands(file) {
139
- let text;
140
- try {
141
- text = fs.readFileSync(file, "utf8");
142
- }
143
- catch {
222
+ function readConfiguredCommands(text) {
223
+ if (text === null)
144
224
  return null;
145
- }
146
225
  const section = text.split(/^\s*\[warm\]\s*$/m)[1]?.split(/^\s*\[/m)[0];
147
226
  const match = section?.match(/^\s*commands\s*=\s*\[([\s\S]*?)\]/m);
148
227
  if (!match)
149
228
  return null;
150
229
  return [...match[1].matchAll(/"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`));
151
230
  }
231
+ /** Git's empty tree. As the attribute source it turns off in-tree .gitattributes. */
232
+ const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
233
+ // Runs in the directory to update. $1 = git dir, $2 = throwaway index file,
234
+ // $3 = commit whose files are there, $4 = commit to move to. jj commits are
235
+ // Git commits in the colocated repo, so Git's two-tree checkout (what
236
+ // `git checkout` does to switch branches) writes exactly the paths that
237
+ // differ, in one process: removals (whiteouts in an overlay's upper layer),
238
+ // executable bits, symlinks and file/directory swaps. It uses its own index
239
+ // file; the repo's index and refs are never written. The overrides keep
240
+ // line-ending, filter and attribute settings, hooks, sparse checkout and
241
+ // fsmonitor from changing what is written: the bytes must be the commit's, as
242
+ // jj writes them. Git without --attr-source (before 2.42) fails here.
243
+ const CHECKOUT_DIFF = `
244
+ G="$1"; I="$2"; S="$3"; B="$4"
245
+ g() {
246
+ GIT_INDEX_FILE="$I" GIT_ATTR_NOSYSTEM=1 git --git-dir="$G" --work-tree=. --attr-source=${EMPTY_TREE} \\
247
+ -c core.autocrlf=false -c core.symlinks=true -c core.attributesFile=/dev/null -c core.hooksPath=/dev/null \\
248
+ -c core.fsmonitor=false -c core.splitIndex=false "$@"
249
+ }
250
+ g read-tree "$S"
251
+ g read-tree --reset -u --no-sparse-checkout "$S" "$B"
252
+ `;
253
+ /** A fork moves its files, then its change, onto the base (the launch makes a full checkout if this fails). */
254
+ const MOVE_TO_BASE = `${CHECKOUT_DIFF}jj rebase -r @ -d "$B"\n`;
255
+ // Runs "$@" as the real user in a private user+mount namespace where the
256
+ // fixed build path $B shows the snapshot being built:
257
+ // - overlay: the layers $L (lowerdir list) under the new layer $UP (work dir $WK);
258
+ // - stack: the layers $L, read-only (needs two or more);
259
+ // - bind: the plain directory $L.
260
+ // Every step mounts afresh; nothing outside the namespace sees $B change.
261
+ const BUILD_ENTER = `
262
+ B="$1"; MODE="$2"; L="$3"; UP="$4"; WK="$5"; U="$6"; G="$7"; shift 7
263
+ case "$MODE" in
264
+ overlay) mount -t overlay overlay -o "lowerdir=$L,upperdir=$UP,workdir=$WK,userxattr" "$B" ;;
265
+ stack) mount -t overlay overlay -o "lowerdir=$L,userxattr" "$B" ;;
266
+ *) mount --bind "$L" "$B" ;;
267
+ esac
268
+ cd "$B"
269
+ exec unshare -U --map-user="$U" --map-group="$G" -- "$@"
270
+ `;
271
+ let namespacesChecked;
272
+ /** Rootless user+mount namespaces (what forks and stacked builds need). */
273
+ function namespacesWork() {
274
+ namespacesChecked ??= process.platform === "linux"
275
+ && spawnSync("unshare", ["-rm", "--propagation", "private", "true"], { stdio: "ignore" }).status === 0;
276
+ return namespacesChecked;
277
+ }
278
+ /** One build step: `command` in `view` (or in `cwd` without one), output to the log. */
279
+ function runStep(log, env, view, cwd, command, args) {
280
+ const [program, argv] = view
281
+ ? ["unshare", [
282
+ "-rm", "--propagation", "private", "sh", "-ec", BUILD_ENTER, "sh",
283
+ view.mount, view.mode, view.lowers, view.upper ?? "", view.work ?? "",
284
+ String(process.getuid?.() ?? 0), String(process.getgid?.() ?? 0), command, ...args,
285
+ ]]
286
+ : [command, args];
287
+ return spawnSync(program, argv, { cwd, env, stdio: ["ignore", log, log] }).status === 0;
288
+ }
289
+ /** Removes a directory tree, including read-only directories a build left behind. */
290
+ async function removeTree(dir) {
291
+ try {
292
+ await fsp.rm(dir, { recursive: true, force: true });
293
+ }
294
+ catch {
295
+ spawnSync("chmod", ["-R", "u+rwX", dir], { stdio: "ignore" });
296
+ await fsp.rm(dir, { recursive: true, force: true }).catch(() => undefined);
297
+ }
298
+ }
299
+ /** Is `commit` conflicted? Its Git tree then holds jj's conflict layout, not files. */
300
+ async function conflicted(repoRoot, revset) {
301
+ const result = await runCommand("jj", ["log", "--no-graph", "--ignore-working-copy", "-r", revset, "-T", 'if(conflict, "conflict\\n")'], {
302
+ cwd: repoRoot,
303
+ allowFailure: true,
304
+ });
305
+ return result.code !== 0 || result.stdout.trim() !== "";
306
+ }
152
307
  /**
153
- * Builds the warm snapshot for `commit` (blocking). Returns it, or null when
154
- * another builder holds the key or the build failed (a failed key is not
155
- * retried; a new base commit gets a new key).
308
+ * Builds the warm snapshot for `commit` (blocking), on top of the nearest
309
+ * ready snapshot when there is one. Returns it (or the ready snapshot with the
310
+ * same files), or null when another builder holds the key or the build failed
311
+ * (a failed key is not retried; a new base commit gets a new key).
156
312
  */
157
- export async function buildWarmSnapshot(repoRoot, commit) {
313
+ export async function buildWarmSnapshot(repoRoot, commit, options = {}) {
158
314
  const key = commit.slice(0, 12);
159
315
  const root = warmDir(repoRoot);
160
316
  await fsp.mkdir(root, { recursive: true });
@@ -162,6 +318,8 @@ export async function buildWarmSnapshot(repoRoot, commit) {
162
318
  if (fs.existsSync(marker("ready")) || fs.existsSync(marker("failed"))) {
163
319
  return readySnapshots(repoRoot).find((snap) => snap.key === key) ?? null;
164
320
  }
321
+ if (fs.existsSync(marker("building")) && !lockAlive(marker("building")))
322
+ await fsp.rm(marker("building"), { force: true });
165
323
  let lock;
166
324
  try {
167
325
  lock = await fsp.open(marker("building"), "wx");
@@ -170,41 +328,178 @@ export async function buildWarmSnapshot(repoRoot, commit) {
170
328
  return null;
171
329
  }
172
330
  const dir = path.join(root, key);
173
- const name = `${WARM_WORKSPACE_PREFIX}${key}`;
331
+ const log = fs.openSync(marker("log"), "a");
332
+ const ctx = {
333
+ repoRoot, key, commit, dir, log,
334
+ say: (line) => fs.writeSync(log, `[rudder] warm snapshot ${key}: ${line}\n`),
335
+ };
174
336
  try {
175
337
  await lock.write(String(process.pid));
176
- await runCommand("jj", ["workspace", "forget", name], { cwd: repoRoot, allowFailure: true });
177
- await fsp.rm(dir, { recursive: true, force: true });
178
- const added = await runCommand("jj", ["workspace", "add", dir, "--name", name, "-r", commit], { cwd: repoRoot, allowFailure: true });
179
- if (added.code !== 0)
180
- throw new Error(added.stderr.trim() || "jj workspace add failed");
181
- const previous = readySnapshots(repoRoot).find((snap) => snap.key !== key);
182
- if (previous)
183
- await seedFromPrevious(previous.dir, dir);
184
- const env = { ...process.env, ...toolCacheEnv("builder", { workspace: dir }) };
185
- for (const command of warmCommands(dir)) {
186
- process.stderr.write(`[rudder] warm snapshot ${key}: ${command}\n`);
187
- const ran = spawnSync("sh", ["-c", command], { cwd: dir, env, stdio: ["ignore", "inherit", "inherit"] });
188
- if (ran.status !== 0)
189
- throw new Error(`${command} exited ${ran.status ?? ran.signal}`);
338
+ if (await conflicted(repoRoot, commit))
339
+ throw new Error(`${key} has conflicts`);
340
+ const gitDir = await gitDirOf(repoRoot);
341
+ const snaps = readySnapshots(repoRoot);
342
+ const nearest = snaps.length > 0 ? await nearestSnapshot(gitDir, snaps, commit) : null;
343
+ if (nearest?.files === 0) {
344
+ ctx.say(`same files as ${nearest.snap.key}`);
345
+ return nearest.snap;
190
346
  }
191
- // Record file stats for the whole tree once; forks reuse this state.
192
- await runCommand("jj", ["status"], { cwd: dir, allowFailure: true });
193
- const snap = { key, commit, dir, builtAt: new Date().toISOString() };
347
+ const started = Date.now();
348
+ let snap = null;
349
+ if (nearest && gitDir && namespacesWork()) {
350
+ snap = await buildOnLayers(ctx, gitDir, nearest.snap, options.maxLayers ?? MAX_LAYERS);
351
+ }
352
+ else if (nearest && process.platform === "darwin") {
353
+ snap = await buildFromClone(ctx, nearest.snap);
354
+ }
355
+ snap ??= await buildFromScratch(ctx, nearest?.snap ?? null);
356
+ ctx.say(`ready in ${((Date.now() - started) / 1000).toFixed(1)} s (${snap.layers?.length ?? 1} layer(s))`);
194
357
  await fsp.writeFile(marker("ready"), JSON.stringify(snap));
195
358
  await pruneWarmSnapshots(repoRoot);
196
359
  return snap;
197
360
  }
198
361
  catch (error) {
199
- process.stderr.write(`[rudder] warm snapshot ${key} failed: ${error instanceof Error ? error.message : String(error)}\n`);
200
- await fsp.writeFile(marker("failed"), String(error));
362
+ ctx.say(`failed: ${error instanceof Error ? error.message : String(error)}`);
363
+ await removeSnapshotFiles(repoRoot, key, dir);
364
+ await fsp.writeFile(marker("failed"), String(error)).catch(() => undefined);
201
365
  return null;
202
366
  }
203
367
  finally {
368
+ fs.closeSync(log);
204
369
  await lock.close();
205
370
  await fsp.rm(marker("building"), { force: true });
206
371
  }
207
372
  }
373
+ /**
374
+ * Linux refresh on `previous`: a new OverlayFS layer over its layers, or, when
375
+ * its stack is `maxLayers` deep, a plain copy of its merged view (compaction).
376
+ * The base change and the warm commands run in that view at the fixed build
377
+ * path, so they see the previous outputs at the paths they were made at.
378
+ * Returns null (after cleaning up) when the view cannot be set up; the caller
379
+ * then builds from scratch. A failing warm command throws.
380
+ */
381
+ async function buildOnLayers(ctx, gitDir, previous, maxLayers) {
382
+ const { repoRoot, key, commit, dir, log, say } = ctx;
383
+ const layers = layersOf(previous.dir);
384
+ const mount = buildPath(repoRoot);
385
+ const work = `${dir}.work`;
386
+ const env = { ...process.env, ...toolCacheEnv("builder", { workspace: mount }) };
387
+ await fsp.mkdir(mount, { recursive: true });
388
+ await removeSnapshotFiles(repoRoot, key, dir);
389
+ await fsp.mkdir(dir);
390
+ const flat = layers.length >= maxLayers;
391
+ let view;
392
+ if (flat) {
393
+ const started = Date.now();
394
+ const source = { mount, mode: layers.length > 1 ? "stack" : "bind", lowers: layers.join(":") };
395
+ // The first snapshot's .jj belongs to its jj workspace. Forks need only an
396
+ // empty .jj to mount their own over.
397
+ const copy = 'find . -mindepth 1 -maxdepth 1 ! -name .jj -exec cp -a -t "$1" {} +; mkdir "$1/.jj"';
398
+ const copied = runStep(log, env, source, dir, "sh", ["-ec", copy, "sh", dir]);
399
+ if (!copied) {
400
+ say(`could not copy ${previous.key}'s ${layers.length} layers; building from scratch`);
401
+ await removeSnapshotFiles(repoRoot, key, dir);
402
+ return null;
403
+ }
404
+ say(`compacted ${previous.key}'s ${layers.length} layers into a plain copy in ${((Date.now() - started) / 1000).toFixed(1)} s`);
405
+ view = { mount, mode: "bind", lowers: dir };
406
+ }
407
+ else {
408
+ await fsp.mkdir(work);
409
+ view = { mount, mode: "overlay", lowers: layers.join(":"), upper: dir, work };
410
+ }
411
+ const index = `${dir}.index`;
412
+ say(`files of ${commit.slice(0, 12)} on top of ${previous.key}`);
413
+ const applied = runStep(log, env, view, dir, "sh", ["-ec", CHECKOUT_DIFF, "sh", gitDir, index, previous.commit, commit]);
414
+ await fsp.rm(index, { force: true });
415
+ if (!applied) {
416
+ say(`could not apply the base change on ${previous.key}; building from scratch`);
417
+ await removeSnapshotFiles(repoRoot, key, dir);
418
+ return null;
419
+ }
420
+ for (const command of warmCommandsAt(gitDir, commit)) {
421
+ say(command);
422
+ if (!runStep(log, env, view, dir, "sh", ["-c", command]))
423
+ throw new Error(`${command} failed`);
424
+ }
425
+ await removeWorkDir(work);
426
+ if (flat)
427
+ return { key, commit, dir, builtAt: new Date().toISOString() };
428
+ const stack = [dir, ...layers];
429
+ await fsp.writeFile(`${dir}.lowerdir`, stack.join(":"));
430
+ return { key, commit, dir, builtAt: new Date().toISOString(), layers: stack };
431
+ }
432
+ /**
433
+ * macOS refresh: the new snapshot is an APFS clone of `previous` checked out at
434
+ * the commit (`rudder-jj rudder-clone-add` writes only the files that differ),
435
+ * so the warm commands start from its outputs. Null when the clone fails.
436
+ */
437
+ async function buildFromClone(ctx, previous) {
438
+ const { repoRoot, key, commit, dir, log, say } = ctx;
439
+ const name = `${WARM_WORKSPACE_PREFIX}${key}`;
440
+ await runCommand("jj", ["workspace", "forget", name], { cwd: repoRoot, allowFailure: true });
441
+ await removeTree(dir);
442
+ const added = await runCommand("jj", ["rudder-clone-add", dir, "--name", name, "-r", commit, "--snapshot", previous.dir, "-m", ""], { cwd: repoRoot, allowFailure: true });
443
+ if (added.code !== 0) {
444
+ say(`clone of ${previous.key} failed (${added.stderr.trim()}); building from scratch`);
445
+ await removeTree(dir);
446
+ return null;
447
+ }
448
+ const env = { ...process.env, ...toolCacheEnv("builder", { workspace: dir }) };
449
+ for (const command of warmCommands(dir)) {
450
+ say(command);
451
+ if (!runStep(log, env, null, dir, "sh", ["-c", command]))
452
+ throw new Error(`${command} failed`);
453
+ }
454
+ await runCommand("jj", ["status"], { cwd: dir, allowFailure: true });
455
+ return { key, commit, dir, builtAt: new Date().toISOString() };
456
+ }
457
+ /**
458
+ * A full checkout of the commit as a jj workspace, seeded with a flat
459
+ * previous snapshot's untracked outputs. On Linux the warm commands run at the
460
+ * fixed build path, as for layers built later on top of it.
461
+ */
462
+ async function buildFromScratch(ctx, previous) {
463
+ const { repoRoot, key, commit, dir, log, say } = ctx;
464
+ const name = `${WARM_WORKSPACE_PREFIX}${key}`;
465
+ await runCommand("jj", ["workspace", "forget", name], { cwd: repoRoot, allowFailure: true });
466
+ await removeSnapshotFiles(repoRoot, key, dir);
467
+ const added = await runCommand("jj", ["workspace", "add", dir, "--name", name, "-r", commit], { cwd: repoRoot, allowFailure: true });
468
+ if (added.code !== 0)
469
+ throw new Error(added.stderr.trim() || "jj workspace add failed");
470
+ if (previous && layersOf(previous.dir).length === 1)
471
+ await seedFromPrevious(previous.dir, dir);
472
+ let view = null;
473
+ if (namespacesWork()) {
474
+ view = { mount: buildPath(repoRoot), mode: "bind", lowers: dir };
475
+ await fsp.mkdir(view.mount, { recursive: true });
476
+ }
477
+ const env = { ...process.env, ...toolCacheEnv("builder", { workspace: view?.mount ?? dir }) };
478
+ for (const command of warmCommands(dir)) {
479
+ say(command);
480
+ if (!runStep(log, env, view, dir, "sh", ["-c", command]))
481
+ throw new Error(`${command} failed`);
482
+ }
483
+ // Record file stats for the whole tree once; macOS clones reuse this state.
484
+ if (process.platform === "darwin")
485
+ await runCommand("jj", ["status"], { cwd: dir, allowFailure: true });
486
+ return { key, commit, dir, builtAt: new Date().toISOString() };
487
+ }
488
+ /** OverlayFS leaves a mode-000 `work/work` in its work directory. */
489
+ async function removeWorkDir(work) {
490
+ await fsp.chmod(path.join(work, "work"), 0o700).catch(() => undefined);
491
+ await removeTree(work);
492
+ }
493
+ /** Removes a snapshot's directory and side files (not its markers or log). */
494
+ async function removeSnapshotFiles(repoRoot, key, dir) {
495
+ if (fs.existsSync(path.join(dir, ".jj", "repo"))) {
496
+ await runCommand("jj", ["workspace", "forget", `${WARM_WORKSPACE_PREFIX}${key}`], { cwd: repoRoot, allowFailure: true });
497
+ }
498
+ await removeWorkDir(`${dir}.work`);
499
+ await removeTree(dir);
500
+ for (const side of [`${dir}.lowerdir`, `${dir}.index`])
501
+ await fsp.rm(side, { force: true });
502
+ }
208
503
  /**
209
504
  * Incremental warm build: copies the previous snapshot's untracked top-level
210
505
  * entries (dependencies and build outputs such as node_modules, target, .venv)
@@ -260,20 +555,22 @@ export function warmBuildSkipReason(dir, minFree = MIN_FREE_FOR_WARM_BUILD) {
260
555
  return `no warm snapshot: ${Math.floor(free / 1024 ** 3)} GB free, a warm build waits for ${Math.ceil(minFree / 1024 ** 3)} GB`;
261
556
  }
262
557
  /**
263
- * Starts a detached builder for `commit` unless one is ready, failed or
264
- * running. Returns why it did not start one when the disk lacks room.
558
+ * Makes sure the project's builder is following the base: starts the detached
559
+ * builder unless one is running or there is nothing to install or build.
560
+ * Never waits. Returns why it did not start one when the disk lacks room.
265
561
  */
266
- export function startWarmBuild(repoRoot, commit) {
267
- const key = commit.slice(0, 12);
268
- const root = warmDir(repoRoot);
269
- if (["ready", "failed", "building"].some((s) => fs.existsSync(path.join(root, `${key}.${s}`))))
562
+ export function startWarmBuild(repoRoot) {
563
+ if (warmCommands(repoRoot).length === 0)
270
564
  return null;
565
+ const root = warmDir(repoRoot);
271
566
  fs.mkdirSync(root, { recursive: true });
567
+ if (lockAlive(path.join(root, BUILDER_LOCK)))
568
+ return null;
272
569
  const skipped = warmBuildSkipReason(root);
273
570
  if (skipped)
274
571
  return skipped;
275
- const log = fs.openSync(path.join(root, `${key}.log`), "a");
276
- const child = spawn(process.execPath, [fileURLToPath(import.meta.url), repoRoot, commit], {
572
+ const log = fs.openSync(path.join(root, "builder.log"), "w");
573
+ const child = spawn(process.execPath, [fileURLToPath(import.meta.url), repoRoot], {
277
574
  detached: true,
278
575
  stdio: ["ignore", log, log],
279
576
  });
@@ -281,30 +578,180 @@ export function startWarmBuild(repoRoot, commit) {
281
578
  fs.closeSync(log);
282
579
  return null;
283
580
  }
284
- /** Deletes ready snapshots beyond the newest few that no fork still uses. */
285
- export async function pruneWarmSnapshots(repoRoot) {
286
- const used = new Set();
287
- const parent = workspacesDir(repoRoot);
288
- for (const name of fs.readdirSync(parent).filter((n) => n.endsWith(".fork"))) {
581
+ /** Takes the project's builder lock (pid written atomically). False when a live builder holds it. */
582
+ function takeBuilderLock(lock) {
583
+ const tmp = `${lock}.${process.pid}`;
584
+ for (let attempt = 0; attempt < 2; attempt += 1) {
585
+ fs.writeFileSync(tmp, String(process.pid));
289
586
  try {
290
- used.add(fs.readFileSync(path.join(parent, name, "lower"), "utf8").trim());
587
+ fs.linkSync(tmp, lock);
588
+ return true;
291
589
  }
292
590
  catch {
293
- /* not a fork */
591
+ if (lockAlive(lock))
592
+ return false;
593
+ fs.rmSync(lock, { force: true });
594
+ }
595
+ finally {
596
+ fs.rmSync(tmp, { force: true });
597
+ }
598
+ }
599
+ return false;
600
+ }
601
+ function releaseBuilderLock(lock) {
602
+ try {
603
+ if (fs.readFileSync(lock, "utf8").trim() === String(process.pid))
604
+ fs.rmSync(lock, { force: true });
605
+ }
606
+ catch {
607
+ /* already gone */
608
+ }
609
+ }
610
+ /** Names of the repo's operation heads: they change with every jj operation. */
611
+ function opHeads(repoRoot) {
612
+ try {
613
+ return fs.readdirSync(path.join(repoRoot, ".jj", "repo", "op_heads", "heads")).sort().join(",");
614
+ }
615
+ catch {
616
+ return String(Math.floor(Date.now() / 10_000));
617
+ }
618
+ }
619
+ /**
620
+ * The builder's loop: whenever the operation log moves, read the base; once
621
+ * the same new base is seen on two polls in a row, build its snapshot. Moves
622
+ * during a build coalesce: the next check sees only the latest base. A small
623
+ * move right after a refresh waits REFRESH_GAP_MS (or as long as that refresh
624
+ * took); a move far from every snapshot builds at once. Exits after
625
+ * FOLLOW_IDLE_MS without a move, or when the repo is gone.
626
+ */
627
+ async function followBase(repoRoot) {
628
+ const root = warmDir(repoRoot);
629
+ await fsp.mkdir(root, { recursive: true });
630
+ const lock = path.join(root, BUILDER_LOCK);
631
+ if (!takeBuilderLock(lock))
632
+ return;
633
+ let heads = "";
634
+ let pending = null;
635
+ let done = null;
636
+ let nextRefresh = 0;
637
+ let active = Date.now();
638
+ try {
639
+ while (Date.now() - active < FOLLOW_IDLE_MS && fs.existsSync(path.join(repoRoot, ".jj"))) {
640
+ const now = opHeads(repoRoot);
641
+ if (now !== heads || pending) {
642
+ // The base only moves with the operation log.
643
+ const base = now !== heads ? await currentBaseCommit(repoRoot) : pending.base;
644
+ heads = now;
645
+ if (!base || base === done) {
646
+ pending = null;
647
+ }
648
+ else if (base !== pending?.base) {
649
+ active = Date.now();
650
+ const files = await baseDistance(repoRoot, base);
651
+ if (files === 0)
652
+ done = base;
653
+ pending = files === 0 ? null : { base, urgent: files > NEAR_SNAPSHOT_FILES };
654
+ }
655
+ else if (pending.urgent || Date.now() >= nextRefresh) {
656
+ pending = null;
657
+ done = base;
658
+ if (warmCommands(repoRoot).length > 0 && !warmBuildSkipReason(root)) {
659
+ const started = Date.now();
660
+ process.stdout.write(`[rudder] warm builder: base moved to ${base.slice(0, 12)}\n`);
661
+ await buildWarmSnapshot(repoRoot, base);
662
+ nextRefresh = Date.now() + Math.max(REFRESH_GAP_MS, Date.now() - started);
663
+ }
664
+ active = Date.now();
665
+ continue;
666
+ }
667
+ }
668
+ await new Promise((resolve) => setTimeout(resolve, FOLLOW_POLL_MS));
294
669
  }
295
670
  }
296
- for (const snap of readySnapshots(repoRoot).slice(KEEP_READY)) {
297
- if (used.has(snap.dir))
671
+ finally {
672
+ releaseBuilderLock(lock);
673
+ }
674
+ }
675
+ /** Files between `base` and the nearest ready snapshot or build attempt (0 when it has one). */
676
+ async function baseDistance(repoRoot, base) {
677
+ const key = base.slice(0, 12);
678
+ if (["ready", "failed"].some((s) => fs.existsSync(path.join(warmDir(repoRoot), `${key}.${s}`))))
679
+ return 0;
680
+ const snaps = readySnapshots(repoRoot);
681
+ if (snaps.length === 0)
682
+ return Infinity;
683
+ return (await nearestSnapshot(await gitDirOf(repoRoot), snaps, base)).files;
684
+ }
685
+ /**
686
+ * Deletes ready snapshots beyond the newest `keep`, unless a fork or a kept
687
+ * snapshot mounts them: a stacked snapshot keeps every layer under it.
688
+ */
689
+ export async function pruneWarmSnapshots(repoRoot, keep = KEEP_READY) {
690
+ const snaps = readySnapshots(repoRoot);
691
+ const kept = snaps.slice(0, keep);
692
+ for (const snap of snaps.slice(keep)) {
693
+ // Asked again for each one: a launch may claim a snapshot meanwhile.
694
+ if (layersInUse(repoRoot, kept).has(realPath(snap.dir)))
298
695
  continue;
299
696
  await fsp.rm(path.join(warmDir(repoRoot), `${snap.key}.ready`), { force: true });
300
- await removeLowerLinks(repoRoot, snap.dir);
301
- await runCommand("jj", ["workspace", "forget", `${WARM_WORKSPACE_PREFIX}${snap.key}`], { cwd: repoRoot, allowFailure: true });
302
- await fsp.rm(snap.dir, { recursive: true, force: true });
697
+ for (const links of [forkLowersRoot(repoRoot), path.join(warmDir(repoRoot), "lowers")])
698
+ await removeLowerLinks(links, snap.dir);
699
+ await removeSnapshotFiles(repoRoot, snap.key, snap.dir);
700
+ await fsp.rm(path.join(warmDir(repoRoot), `${snap.key}.log`), { force: true });
303
701
  }
702
+ await pruneOldMarkers(repoRoot);
304
703
  }
305
- /** Drops `.warm/lowers/<tree>` links into a snapshot that is being deleted. */
306
- async function removeLowerLinks(repoRoot, target) {
307
- const root = path.join(warmDir(repoRoot), "lowers");
704
+ /**
705
+ * Every layer (real path) a kept snapshot or a fork mounts. A fork counts both
706
+ * the snapshot it was made from (`.fork/lower`: its agent's namespace may still
707
+ * hold that mount) and what its working copy points at now.
708
+ */
709
+ function layersInUse(repoRoot, kept) {
710
+ const used = new Set();
711
+ const add = (dir) => {
712
+ for (const layer of layersOf(realPath(dir)))
713
+ used.add(realPath(layer));
714
+ };
715
+ for (const snap of kept)
716
+ add(snap.dir);
717
+ const parent = workspacesDir(repoRoot);
718
+ let names = [];
719
+ try {
720
+ names = fs.readdirSync(parent).filter((n) => n.endsWith(".fork"));
721
+ }
722
+ catch {
723
+ /* no workspaces */
724
+ }
725
+ for (const name of names) {
726
+ for (const file of [path.join(parent, name, "lower"), path.join(parent, name.slice(0, -".fork".length), ".jj", "working_copy", "lower")]) {
727
+ try {
728
+ const dir = fs.readFileSync(file, "utf8").trim();
729
+ if (dir)
730
+ add(dir);
731
+ }
732
+ catch {
733
+ /* not written (yet) */
734
+ }
735
+ }
736
+ }
737
+ return used;
738
+ }
739
+ /** Drops logs and failure markers of builds that left no snapshot, a day on. */
740
+ async function pruneOldMarkers(repoRoot) {
741
+ const root = warmDir(repoRoot);
742
+ const names = await fsp.readdir(root).catch(() => []);
743
+ const cutoff = Date.now() - 24 * 3600_000;
744
+ for (const name of names) {
745
+ const match = /^(.+)\.(log|failed)$/.exec(name);
746
+ if (!match || match[1] === "builder" || names.includes(`${match[1]}.ready`))
747
+ continue;
748
+ const stat = await fsp.stat(path.join(root, name)).catch(() => null);
749
+ if (stat && stat.mtimeMs < cutoff)
750
+ await fsp.rm(path.join(root, name), { force: true });
751
+ }
752
+ }
753
+ /** Drops tree links into a snapshot that is being deleted. */
754
+ async function removeLowerLinks(root, target) {
308
755
  for (const name of await fsp.readdir(root).catch(() => [])) {
309
756
  const link = path.join(root, name);
310
757
  if ((await fsp.readlink(link).catch(() => null)) === target)
@@ -319,8 +766,14 @@ exec unshare -rm --propagation private sh -ec '
319
766
  F="$1"; M="$2"; U="$3"; G="$4"; shift 4
320
767
  mount --bind "$M/.jj" "$F/jjmnt"
321
768
  W="$F/jjmnt/working_copy"
322
- mount -t overlay overlay -o "lowerdir=$(readlink -f "$(cat "$W/lower")"),upperdir=$W/upper,workdir=$W/work,userxattr" "$M"
769
+ L="$(readlink -f "$(cat "$W/lower")")"
770
+ # A stacked warm snapshot lists all its layers next to its top layer.
771
+ if [ -f "$L.lowerdir" ]; then L="$(cat "$L.lowerdir")"; fi
772
+ mount -t overlay overlay -o "lowerdir=$L,upperdir=$W/upper,workdir=$W/work,userxattr" "$M"
323
773
  mount --bind "$F/jjmnt" "$M/.jj"
774
+ # Build output can name the path the snapshot was built at; show this fork there.
775
+ A="$(cat "$F/alias" 2>/dev/null || true)"
776
+ if [ -n "$A" ] && [ -d "$A" ]; then mount --rbind "$M" "$A"; fi
324
777
  # Login shells rebuild PATH; make every jj on it this fork-aware rudder-jj.
325
778
  J="$(readlink -f "$F/bin/jj")"
326
779
  for d in $(printf %s "$PATH" | tr : " ") /usr/local/bin /usr/bin "$HOME/.local/bin"; do
@@ -330,29 +783,6 @@ cd "$M"
330
783
  PATH="$F/bin:$PATH"; export PATH
331
784
  exec unshare -U --map-user="$U" --map-group="$G" -- "$@"' sh "$F" "$M" "$U" "$G" "$@"
332
785
  `;
333
- /** Git's empty tree. As the attribute source it turns off in-tree .gitattributes. */
334
- const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
335
- // Runs inside the mounted fork. $1 = git dir, $2 = throwaway index file,
336
- // $3 = snapshot commit, $4 = base. jj commits are Git commits in the colocated
337
- // repo, so Git's two-tree checkout (what `git checkout` does to switch
338
- // branches) writes exactly the paths that differ, in one process: removals
339
- // (whiteouts in the upper layer), executable bits, symlinks and file/directory
340
- // swaps. It uses its own index file; the repo's index and refs are never
341
- // written. The overrides keep line-ending, filter and attribute settings,
342
- // hooks, sparse checkout and fsmonitor from changing what is written: the
343
- // bytes must be the commit's, as jj writes them. Git without --attr-source
344
- // (before 2.42) fails here, and the launch makes a full checkout.
345
- const MOVE_TO_BASE = `
346
- G="$1"; I="$2"; S="$3"; B="$4"
347
- g() {
348
- GIT_INDEX_FILE="$I" GIT_ATTR_NOSYSTEM=1 git --git-dir="$G" --work-tree=. --attr-source=${EMPTY_TREE} \\
349
- -c core.autocrlf=false -c core.symlinks=true -c core.attributesFile=/dev/null -c core.hooksPath=/dev/null \\
350
- -c core.fsmonitor=false -c core.splitIndex=false "$@"
351
- }
352
- g read-tree "$S"
353
- g read-tree --reset -u --no-sparse-checkout "$S" "$B"
354
- jj rebase -r @ -d "$B"
355
- `;
356
786
  /** Ready snapshots further than this many files from the base wait for the base's own build. */
357
787
  const NEAR_SNAPSHOT_FILES = 500;
358
788
  const WAIT_FOR_BUILD_MS = 90_000;
@@ -377,35 +807,55 @@ async function nearestSnapshot(gitDir, snaps, base) {
377
807
  return { snap: snaps[best], files: files[best] };
378
808
  }
379
809
  /**
380
- * Waits up to `timeoutMs` for the warm build of `commit` to become ready.
381
- * Returns null when it fails, or no live builder holds its lock.
810
+ * Waits up to `timeoutMs` for the builder to make a snapshot of `base`, or one
811
+ * within NEAR_SNAPSHOT_FILES of it (the base may move on meanwhile). Returns
812
+ * null when its build fails or no live builder is left to make it.
382
813
  */
383
- async function waitForWarmBuild(repoRoot, commit, timeoutMs) {
384
- const key = commit.slice(0, 12);
385
- const marker = (suffix) => path.join(warmDir(repoRoot), `${key}.${suffix}`);
814
+ async function waitForNearSnapshot(repoRoot, gitDir, base, timeoutMs) {
815
+ const root = warmDir(repoRoot);
816
+ const key = base.slice(0, 12);
386
817
  const started = Date.now();
818
+ let seen = "";
387
819
  while (Date.now() - started < timeoutMs) {
388
- if (fs.existsSync(marker("ready")))
389
- return readySnapshots(repoRoot).find((snap) => snap.key === key) ?? null;
390
- if (fs.existsSync(marker("failed")))
820
+ const snaps = readySnapshots(repoRoot);
821
+ const own = snaps.find((snap) => snap.commit === base);
822
+ if (own)
823
+ return own;
824
+ if (fs.existsSync(path.join(root, `${key}.failed`)))
391
825
  return null;
392
- // A detached builder takes the lock right after it starts.
393
- if (Date.now() - started > 5_000 && !builderAlive(marker("building")))
826
+ const keys = snaps.map((snap) => snap.key).join();
827
+ if (snaps.length > 0 && keys !== seen) {
828
+ seen = keys;
829
+ gitDir ??= await gitDirOf(repoRoot);
830
+ const nearest = await nearestSnapshot(gitDir, snaps, base);
831
+ if (nearest.files <= NEAR_SNAPSHOT_FILES)
832
+ return nearest.snap;
833
+ }
834
+ // A detached builder takes its lock right after it starts.
835
+ if (Date.now() - started > 5_000 && !lockAlive(path.join(root, BUILDER_LOCK)) && !lockAlive(path.join(root, `${key}.building`)))
394
836
  return null;
395
837
  await new Promise((resolve) => setTimeout(resolve, 250));
396
838
  }
397
839
  return null;
398
840
  }
399
- function builderAlive(lock) {
400
- let pid;
841
+ /** Does a live process hold this pid lock? An empty lock counts as live for 10 s. */
842
+ function lockAlive(lock) {
843
+ let text;
401
844
  try {
402
- pid = Number(fs.readFileSync(lock, "utf8").trim());
845
+ text = fs.readFileSync(lock, "utf8").trim();
403
846
  }
404
847
  catch {
405
848
  return false;
406
849
  }
407
- if (!pid)
408
- return true; // lock taken, pid not written yet
850
+ const pid = Number(text);
851
+ if (!pid) {
852
+ try {
853
+ return Date.now() - fs.statSync(lock).mtimeMs < 10_000;
854
+ }
855
+ catch {
856
+ return false;
857
+ }
858
+ }
409
859
  try {
410
860
  process.kill(pid, 0);
411
861
  return true;
@@ -416,44 +866,51 @@ function builderAlive(lock) {
416
866
  }
417
867
  /**
418
868
  * Creates `targetPath` as a fork of the warm snapshot nearest its base: an
419
- * overlay fork on Linux, an APFS clone on macOS. A base without its own
420
- * snapshot starts a build for it; when every ready snapshot is far away, the
421
- * launch waits a while for that build. Returns a reason (after cleaning up) when
422
- * there is no snapshot or the fork cannot be made here; the caller then makes
423
- * a full jj checkout.
869
+ * overlay fork on Linux, an APFS clone on macOS. A launch never builds; it
870
+ * makes sure the project's builder runs. When every ready snapshot is far from
871
+ * the base (or there is none yet), a launch from the base waits a while for
872
+ * the builder; a branch launch forks the nearest base snapshot and patches the
873
+ * difference. Returns a reason (after cleaning up) when there is no snapshot
874
+ * or the fork cannot be made here; the caller then makes a full jj checkout.
424
875
  */
425
876
  export async function createForkWorkspace(params) {
426
877
  if (process.platform !== "linux" && process.platform !== "darwin")
427
878
  return { reason: "not Linux or macOS" };
879
+ // Without these no fork can be made, so do not build or wait for a snapshot.
880
+ const rudderJj = rudderJjPath();
881
+ if (!rudderJj)
882
+ return { reason: "rudder-jj is not available" };
883
+ if (process.platform === "linux" && !namespacesWork())
884
+ return { reason: "rootless user namespaces unavailable" };
428
885
  const base = params.atChangeId
429
886
  ? await commitOf(params.repoRoot, params.atChangeId)
430
887
  : await currentBaseCommit(params.repoRoot);
431
888
  if (params.atChangeId && !base)
432
889
  return { reason: `change ${params.atChangeId} not found` };
433
- const snaps = readySnapshots(params.repoRoot);
434
890
  // Projects with nothing to install or build keep the plain checkout.
435
891
  const warmable = warmCommands(params.repoRoot).length > 0;
436
- let skipped = null;
892
+ const skipped = warmable ? startWarmBuild(params.repoRoot) : null;
893
+ const snaps = readySnapshots(params.repoRoot);
894
+ const canWait = Boolean(base && warmable && !skipped && !params.atChangeId);
437
895
  let snapshot = snaps.find((snap) => snap.commit === base);
438
- if (base && !snapshot && warmable)
439
- skipped = startWarmBuild(params.repoRoot, base);
440
896
  let gitDir = null;
441
897
  if (base && !snapshot && snaps.length > 0) {
442
898
  gitDir = await gitDirOf(params.repoRoot);
443
899
  const nearest = await nearestSnapshot(gitDir, snaps, base);
444
900
  snapshot = nearest.snap;
445
- // Far from every ready snapshot: the base's own build (seeded from the
446
- // previous one) is usually quicker than patching thousands of files.
447
- if (nearest.files > NEAR_SNAPSHOT_FILES && warmable && !skipped) {
448
- snapshot = (await waitForWarmBuild(params.repoRoot, base, WAIT_FOR_BUILD_MS)) ?? snapshot;
901
+ // Far from every ready snapshot: the base's own build (on top of the
902
+ // nearest one) is usually quicker than patching thousands of files.
903
+ if (nearest.files > NEAR_SNAPSHOT_FILES && canWait) {
904
+ snapshot = (await waitForNearSnapshot(params.repoRoot, gitDir, base, WAIT_FOR_BUILD_MS)) ?? snapshot;
449
905
  }
450
906
  }
907
+ else if (base && !snapshot && canWait) {
908
+ // A project's first launch: its first snapshot is often ready soon.
909
+ snapshot = (await waitForNearSnapshot(params.repoRoot, gitDir, base, WAIT_FOR_BUILD_MS)) ?? undefined;
910
+ }
451
911
  snapshot ??= snaps[0];
452
912
  if (!snapshot)
453
913
  return { reason: skipped ?? (warmable ? "no warm snapshot yet (building)" : "nothing to install or build") };
454
- const rudderJj = rudderJjPath();
455
- if (!rudderJj)
456
- return { reason: "rudder-jj is not available" };
457
914
  if (process.platform === "darwin")
458
915
  return await createCloneWorkspace(params, snapshot, base ?? snapshot.commit);
459
916
  const move = base && base !== snapshot.commit ? base : null;
@@ -461,20 +918,22 @@ export async function createForkWorkspace(params) {
461
918
  gitDir ??= await gitDirOf(params.repoRoot);
462
919
  if (!gitDir)
463
920
  return { reason: "no Git store behind the jj repo" };
464
- // A conflicted commit's Git tree holds jj's conflict layout, not files.
465
- const conflicted = await runCommand("jj", ["log", "--no-graph", "--ignore-working-copy", "-r", `${snapshot.commit}|${move}`, "-T", 'if(conflict, "conflict\\n")'], { cwd: params.repoRoot, allowFailure: true });
466
- if (conflicted.code !== 0 || conflicted.stdout.trim())
921
+ if (await conflicted(params.repoRoot, `${snapshot.commit}|${move}`))
467
922
  return { reason: `${move.slice(0, 12)} has conflicts` };
468
923
  }
469
- // Claim the snapshot before adding the fork: a warm build that finishes
470
- // meanwhile prunes snapshots no `.fork/lower` names, and the nearest one is
471
- // often not among the newest.
924
+ // Claim the snapshot before adding the fork: a refresh that finishes
925
+ // meanwhile prunes snapshots no `.fork/lower` names. One that was already
926
+ // pruning may have taken it; then make a full checkout.
472
927
  const state = forkStateDir(params.targetPath);
473
928
  await fsp.mkdir(state, { recursive: true });
474
929
  await fsp.writeFile(path.join(state, "lower"), snapshot.dir);
930
+ if (!layersOf(snapshot.dir).every((layer) => fs.existsSync(layer))) {
931
+ await removeForkState(params.targetPath);
932
+ return { reason: `warm snapshot ${snapshot.key} was pruned` };
933
+ }
475
934
  const added = await runCommand("jj", [
476
935
  "rudder-fork-add", params.targetPath, "--name", params.workspaceName, "-r", snapshot.commit,
477
- "--lower", snapshot.dir, "--lowers-root", path.join(warmDir(params.repoRoot), "lowers"),
936
+ "--lower", snapshot.dir, "--lowers-root", forkLowersRoot(params.repoRoot),
478
937
  "-m", params.description ?? "",
479
938
  ], { cwd: params.repoRoot, allowFailure: true });
480
939
  if (added.code !== 0) {
@@ -486,6 +945,7 @@ export async function createForkWorkspace(params) {
486
945
  for (const sub of ["jjmnt", "bin"])
487
946
  await fsp.mkdir(path.join(state, sub), { recursive: true });
488
947
  await fsp.symlink(rudderJj, path.join(state, "bin", "jj"));
948
+ await fsp.writeFile(path.join(state, "alias"), buildPath(params.repoRoot));
489
949
  await fsp.writeFile(path.join(state, "enter"), ENTER_SCRIPT, { mode: 0o755 });
490
950
  const probe = spawnSync(path.join(state, "enter"), ["true"], { cwd: params.targetPath, stdio: ["ignore", "ignore", "pipe"] });
491
951
  if (probe.status !== 0)
@@ -547,10 +1007,13 @@ export async function removeForkState(workspace) {
547
1007
  await fsp.chmod(path.join(state, "work", "work"), 0o700).catch(() => undefined);
548
1008
  await fsp.rm(state, { recursive: true, force: true });
549
1009
  }
550
- // Detached builder entry: node warm-snapshot.js <repoRoot> <commit>
1010
+ // Detached builder entry: node warm-snapshot.js <repoRoot> follows the base;
1011
+ // with a commit it builds that one snapshot.
551
1012
  if (process.argv[1] && fileURLToPath(import.meta.url) === path.resolve(process.argv[1])) {
552
1013
  const [repoRoot, commit] = process.argv.slice(2);
553
1014
  if (repoRoot && commit)
554
1015
  await buildWarmSnapshot(repoRoot, commit);
1016
+ else if (repoRoot)
1017
+ await followBase(repoRoot);
555
1018
  }
556
1019
  //# sourceMappingURL=warm-snapshot.js.map