@antelopejs/tooling-configs 0.3.3 → 0.3.5

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,18 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
  import { a as VERIFY_KINDS, c as isVerifyKind, l as skillFolder } from "../shared-igPpEDpS.mjs";
3
- import { a as isString, c as object, f as parseJson, i as isObject, n as children, p as str, s as num, u as optStr } from "../json-CG2L7YFl.mjs";
3
+ import { a as isString, c as object, f as parseJson, i as isObject, n as children, p as str, s as num, t as child, u as optStr } from "../json-CG2L7YFl.mjs";
4
4
  import { a as exitIs, c as interrupted, i as exists, r as equal, u as refused } from "../checks-CxkSu2E6.mjs";
5
5
  import { n as runCliAsync } from "../cli-4Tv71vYk.mjs";
6
6
  import { c as repositoryRoot } from "../files-DJ0TeaTU.mjs";
7
- import { appendFileSync, chmodSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, rmdirSync, statSync, symlinkSync, utimesSync, writeFileSync } from "node:fs";
8
- import { basename, dirname, extname, join, matchesGlob, posix, relative, resolve, sep } from "node:path";
7
+ import { a as readFeatures, c as allocateRun, d as loadRun, f as openWorkspace, g as saveRun, h as removeTree, i as planFor, l as git, m as operatorTmpdir, n as SMOKE_FEATURE, o as CACHE_DIR, p as operatorEnv, r as checkMap, s as RUNS_DIR, u as isolatedEnv } from "../map-s_jKqBQv.mjs";
8
+ import { appendFileSync, closeSync, copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, realpathSync, renameSync, rmSync, rmdirSync, statSync, symlinkSync, unlinkSync, writeFileSync } from "node:fs";
9
+ import { basename, dirname, extname, isAbsolute, join, matchesGlob, posix, relative, resolve, sep } from "node:path";
9
10
  import { execFileSync, spawn, spawnSync } from "node:child_process";
10
11
  import { pathToFileURL } from "node:url";
11
12
  import ts from "typescript";
12
- import { randomBytes } from "node:crypto";
13
- import { constants as constants$1, homedir, tmpdir } from "node:os";
13
+ import { createHash, randomBytes } from "node:crypto";
14
+ import { constants as constants$1, tmpdir } from "node:os";
14
15
  import { parseArgs } from "node:util";
15
16
  import { createServer } from "node:net";
17
+ import { setTimeout as setTimeout$1 } from "node:timers/promises";
16
18
  //#region src/verify/doors.ts
17
19
  const RELEASE_FILES = [
18
20
  "package.json",
@@ -332,146 +334,13 @@ function emitFingerprint(repo, sha, files) {
332
334
  return fingerprint;
333
335
  }
334
336
  //#endregion
335
- //#region src/verify/workspace.ts
336
- /**
337
- * Caches shared by every run: the warm pnpm store, the npm cache and the
338
- * published packages. Nothing a journey asserts on lives here.
339
- */
340
- const CACHE_DIR = join(process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "antelopejs-verify");
341
- /** The folder that holds every run, one subfolder per repository. */
342
- const RUNS_DIR = join(tmpdir(), "antelopejs-verify");
343
- function makeWritable(folder) {
344
- chmodSync(folder, lstatSync(folder).mode | 448);
345
- for (const entry of readdirSync(folder, { withFileTypes: true })) if (entry.isDirectory()) makeWritable(join(folder, entry.name));
346
- }
347
- /** Deletes a folder this run wrote, including folders an install left read-only. */
348
- function removeTree(folder) {
349
- try {
350
- rmSync(folder, {
351
- recursive: true,
352
- force: true
353
- });
354
- } catch {
355
- makeWritable(folder);
356
- rmSync(folder, {
357
- recursive: true,
358
- force: true
359
- });
360
- }
361
- }
362
- /** Runs git in `cwd` and returns its trimmed output. */
363
- function git(cwd, args) {
364
- return execFileSync("git", args, {
365
- cwd,
366
- encoding: "utf8",
367
- stdio: [
368
- "ignore",
369
- "pipe",
370
- "pipe"
371
- ]
372
- }).trim();
373
- }
374
- function readManifest$1(root) {
375
- const file = join(root, "package.json");
376
- const value = existsSync(file) ? parseJson(readFileSync(file, "utf8")) : {};
377
- return isObject(value) ? value : {};
378
- }
379
- /** Opens the repository at `root` for `kind`, loading the repo-owned verify.config.ts. */
380
- async function openWorkspace(root, kind) {
381
- const skill = join(root, skillFolder(kind));
382
- const file = join(skill, "verify.config.ts");
383
- if (!existsSync(file)) throw new Error(`${file} is missing. Write the repo-owned layer (verify.config.ts and features/) described in ${skillFolder(kind)}/SKILL.md.`);
384
- const config = (await import(pathToFileURL(file).href)).default;
385
- if (config?.doors === void 0) throw new Error(`${file} must export default a VerifyConfig with a doors block.`);
386
- const manifest = readManifest$1(root);
387
- return {
388
- root,
389
- kind,
390
- skill,
391
- config,
392
- manifest,
393
- name: String(manifest.name ?? basename(root)).replace(/^@[^/]+\//, "").replace(/[^a-z0-9-]+/gi, "-")
394
- };
395
- }
396
- function stateFile(dir) {
397
- return join(dir, "run.json");
398
- }
399
- /** Reads a run created by `new`, failing with the reason when it is missing, and marks it active by touching run.json. */
400
- function loadRun(dir) {
401
- const file = stateFile(resolve(dir));
402
- if (!existsSync(file)) throw new Error(`No run at ${dir}: run.json is missing. Create one with \`new\`.`);
403
- const now = /* @__PURE__ */ new Date();
404
- utimesSync(file, now, now);
405
- return JSON.parse(readFileSync(file, "utf8"));
406
- }
407
- /** Persists the run state. */
408
- function saveRun(state) {
409
- writeFileSync(stateFile(state.dir), `${JSON.stringify(state, null, 2)}\n`);
410
- }
411
- /** Allocates the folders of a new run under the system temp folder. */
412
- function allocateRun(workspace, baseRef, title) {
413
- const id = `${(/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").slice(0, 15)}-${randomBytes(3).toString("hex")}`;
414
- const dir = join(RUNS_DIR, workspace.name, id);
415
- const evidence = join(dir, "evidence");
416
- mkdirSync(evidence, { recursive: true });
417
- return {
418
- id,
419
- kind: workspace.kind,
420
- repo: workspace.root,
421
- dir,
422
- evidence,
423
- createdAt: (/* @__PURE__ */ new Date()).toISOString(),
424
- headSha: git(workspace.root, ["rev-parse", "HEAD"]),
425
- baseRef,
426
- title,
427
- processes: [],
428
- containers: [],
429
- pack: "",
430
- manifestUrl: "",
431
- templateName: "",
432
- missingKeys: (workspace.config.saas?.env ?? []).filter((key) => !process.env[key])
433
- };
434
- }
435
- const INHERITED_PREFIXES = [
436
- "ANTELOPEJS_",
437
- "npm_",
438
- "PNPM_",
439
- "XDG_"
440
- ];
441
- /**
442
- * The environment every driven command gets: the journey's HOME, CI=1, no
443
- * update check, no colors and the shared caches. Variables that could leak
444
- * the operator's own settings into the run are dropped.
445
- */
446
- function isolatedEnv(workspace, home, extra = {}) {
447
- const kept = Object.entries(process.env).filter(([key]) => !INHERITED_PREFIXES.some((prefix) => key.startsWith(prefix)));
448
- return {
449
- ...Object.fromEntries(kept),
450
- HOME: home,
451
- USERPROFILE: home,
452
- CI: "1",
453
- AJS_NO_UPDATE_CHECK: "1",
454
- NO_COLOR: "1",
455
- COREPACK_HOME: process.env.COREPACK_HOME ?? join(homedir(), ".cache", "node", "corepack"),
456
- COREPACK_ENABLE_DOWNLOAD_PROMPT: "0",
457
- XDG_DATA_HOME: join(CACHE_DIR, "xdg-data"),
458
- XDG_CACHE_HOME: join(home, ".cache"),
459
- XDG_CONFIG_HOME: join(home, ".config"),
460
- npm_config_cache: join(CACHE_DIR, "npm"),
461
- npm_config_prefer_offline: "true",
462
- GIT_CONFIG_NOSYSTEM: "1",
463
- ...workspace.config.env,
464
- ...extra
465
- };
466
- }
467
- //#endregion
468
337
  //#region src/verify/behaviour.ts
469
338
  const DETAIL_LIMIT = 5;
470
339
  const DETAIL_LENGTH = 160;
471
340
  const OUTPUT_LIMIT$1 = 67108864;
472
341
  /** Runs `action` in a throwaway worktree of `sha` whose `node_modules` links the checkout's, then removes it. */
473
342
  function inWorktree(repo, sha, action) {
474
- const dir = mkdtempSync(join(tmpdir(), "antelopejs-verify-behaviour-"));
343
+ const dir = mkdtempSync(join(operatorTmpdir(), "antelopejs-verify-behaviour-"));
475
344
  git(repo, [
476
345
  "worktree",
477
346
  "add",
@@ -502,6 +371,7 @@ function probeFingerprint(repo, command, sha) {
502
371
  return inWorktree(repo, sha, (dir) => {
503
372
  const run = spawnSync(program, [...args, dir], {
504
373
  cwd: repo,
374
+ env: operatorEnv(),
505
375
  encoding: "utf8",
506
376
  maxBuffer: OUTPUT_LIMIT$1
507
377
  });
@@ -583,6 +453,61 @@ function behaviourChange(repo, check, commits) {
583
453
  };
584
454
  }
585
455
  //#endregion
456
+ //#region src/verify/timing.ts
457
+ /** Starts a clock; the returned function gives the whole milliseconds elapsed since. */
458
+ function stopwatch() {
459
+ const start = performance.now();
460
+ return () => Math.round(performance.now() - start);
461
+ }
462
+ /** Runs `phase` and records how long it took on the run, also when it throws, so a run whose `new` failed still shows the time it spent; a failed phase is labelled as such. */
463
+ async function timed(state, phase, run) {
464
+ const elapsed = stopwatch();
465
+ let failed = true;
466
+ try {
467
+ const value = await run();
468
+ failed = false;
469
+ return value;
470
+ } finally {
471
+ state.timings.push({
472
+ phase: failed ? `${phase} (failed)` : phase,
473
+ ms: elapsed()
474
+ });
475
+ }
476
+ }
477
+ /**
478
+ * Times the step folders of one journey: a step lasts from the `start` call
479
+ * that names it to the next `start` call, or to `finish` for the last one.
480
+ */
481
+ var StepClock = class {
482
+ times = [];
483
+ open = null;
484
+ /** Ends the running step, if any, and starts timing `step`. */
485
+ start(step) {
486
+ this.close();
487
+ this.open = {
488
+ step,
489
+ elapsed: stopwatch()
490
+ };
491
+ }
492
+ /** Ends the running step and returns how long every step lasted, in the order they started. */
493
+ finish() {
494
+ this.close();
495
+ return this.times;
496
+ }
497
+ close() {
498
+ if (this.open === null) return;
499
+ this.times.push({
500
+ step: this.open.step,
501
+ ms: this.open.elapsed()
502
+ });
503
+ this.open = null;
504
+ }
505
+ };
506
+ /** Milliseconds as seconds with one decimal, e.g. `12.3 s`; `not measured` when there is no measurement. */
507
+ function seconds(ms) {
508
+ return ms === null || ms === void 0 ? "not measured" : `${(ms / 1e3).toFixed(1)} s`;
509
+ }
510
+ //#endregion
586
511
  //#region src/verify/formal.ts
587
512
  const OUTPUT_LIMIT = 67108864;
588
513
  function changesCheckFiles(check, changed) {
@@ -611,6 +536,7 @@ function runCommand(command, cwd) {
611
536
  const [program, ...args] = command;
612
537
  return spawnSync(program, args, {
613
538
  cwd,
539
+ env: operatorEnv(),
614
540
  encoding: "utf8",
615
541
  maxBuffer: OUTPUT_LIMIT
616
542
  });
@@ -657,6 +583,7 @@ function gateFields(workspace, check, diff, folder, head) {
657
583
  * the rules the base broke that HEAD reports neither failing nor passing.
658
584
  */
659
585
  function runFormalCheck(workspace, state, check, diff) {
586
+ const elapsed = stopwatch();
660
587
  const folder = join(state.evidence, "formal", check.name);
661
588
  mkdirSync(folder, { recursive: true });
662
589
  const run = runCommand(check.command, workspace.root);
@@ -673,6 +600,7 @@ function runFormalCheck(workspace, state, check, diff) {
673
600
  baseSha: diff.baseSha,
674
601
  headSha: diff.headSha
675
602
  } }, gateFields(workspace, check, diff, folder, head));
603
+ result.ms = elapsed();
676
604
  writeFileSync(join(folder, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
677
605
  return result;
678
606
  }
@@ -800,6 +728,8 @@ function readJudgement(file) {
800
728
  //#endregion
801
729
  //#region src/verify/docker.ts
802
730
  const READY_TIMEOUT_MS$1 = 9e4;
731
+ /** The open-file limit of every service container, MongoDB's recommended value: Docker's default soft limit of 1024 runs out when mongod holds dropped tables open until its next checkpoint. */
732
+ const OPEN_FILES = 64e3;
803
733
  function docker(args) {
804
734
  const run = spawnSync("docker", args, { encoding: "utf8" });
805
735
  return {
@@ -850,7 +780,8 @@ function waitReady(container, ready, folder) {
850
780
  /**
851
781
  * Starts every service of verify.config.ts in its own container, named
852
782
  * `<prefix>-<repo>-<service>-<run id>`, publishes its port on a free
853
- * loopback port chosen by Docker, and waits for its readiness command. Each
783
+ * loopback port chosen by Docker, raises its open-file limit to 64000, and
784
+ * waits for its readiness command. Each
854
785
  * container is recorded before it is probed, so `clean` removes it even when
855
786
  * it never became ready.
856
787
  */
@@ -865,24 +796,36 @@ function startServices(workspace, state, prefix) {
865
796
  name,
866
797
  "-p",
867
798
  `127.0.0.1::${definition.port}`,
799
+ "--ulimit",
800
+ `nofile=${OPEN_FILES}:${OPEN_FILES}`,
868
801
  ...env,
869
802
  definition.image
870
803
  ];
871
804
  const folder = join(state.evidence, "services", service);
872
- const started = docker(args);
873
- record(join(folder, "01-run"), args, started);
874
- if (started.status !== 0) throw new Error(`docker ${args.join(" ")} failed: ${started.stderr}`);
875
- const container = {
876
- service,
877
- name,
878
- host: "127.0.0.1",
879
- port: 0
880
- };
881
- state.containers.push(container);
882
- saveRun(state);
883
- container.port = publishedPort(name, definition.port);
884
- saveRun(state);
885
- waitReady(name, definition.ready, join(folder, "02-ready"));
805
+ const elapsed = stopwatch();
806
+ let failed = true;
807
+ try {
808
+ const started = docker(args);
809
+ record(join(folder, "01-run"), args, started);
810
+ if (started.status !== 0) throw new Error(`docker ${args.join(" ")} failed: ${started.stderr}`);
811
+ const container = {
812
+ service,
813
+ name,
814
+ host: "127.0.0.1",
815
+ port: 0
816
+ };
817
+ state.containers.push(container);
818
+ saveRun(state);
819
+ container.port = publishedPort(name, definition.port);
820
+ saveRun(state);
821
+ waitReady(name, definition.ready, join(folder, "02-ready"));
822
+ failed = false;
823
+ } finally {
824
+ state.timings.push({
825
+ phase: `service ${service} (start and ready)${failed ? " (failed)" : ""}`,
826
+ ms: elapsed()
827
+ });
828
+ }
886
829
  }
887
830
  }
888
831
  /** Runs a service's readiness command once in its container, recorded in `folder`: the stderr when it fails, `null` when it passes. */
@@ -2205,10 +2148,25 @@ function seedSources(workspace, state) {
2205
2148
  function containerPrefix() {
2206
2149
  return process.env.VERIFY_CONTAINER_PREFIX ?? "verify";
2207
2150
  }
2151
+ /** The `prepack` values that do nothing but run the package's build script. */
2152
+ const BUILD_ONLY_PREPACKS = /* @__PURE__ */ new Set([
2153
+ "pnpm run build",
2154
+ "pnpm build",
2155
+ "npm run build",
2156
+ "yarn build",
2157
+ "yarn run build"
2158
+ ]);
2159
+ function script(workspace, name) {
2160
+ return optStr(child(workspace.manifest, "scripts"), name);
2161
+ }
2162
+ /** Whether `pnpm pack` builds the package itself, so a setup build before it would run the same build twice. */
2163
+ function packBuilds(workspace) {
2164
+ const prepack = script(workspace, "prepack");
2165
+ return workspace.kind === "interface" && prepack !== void 0 && BUILD_ONLY_PREPACKS.has(prepack.trim());
2166
+ }
2208
2167
  function setupCommands(workspace) {
2209
2168
  if (workspace.config.setup) return workspace.config.setup;
2210
- const scripts = workspace.manifest.scripts;
2211
- return scripts !== void 0 && isObject(scripts) && scripts.build !== void 0 && workspace.kind !== "template" ? [[
2169
+ return script(workspace, "build") !== void 0 && workspace.kind !== "template" && !packBuilds(workspace) ? [[
2212
2170
  "pnpm",
2213
2171
  "run",
2214
2172
  "build"
@@ -2217,19 +2175,29 @@ function setupCommands(workspace) {
2217
2175
  async function runSetup(workspace, state) {
2218
2176
  const commands = setupCommands(workspace);
2219
2177
  for (const [index, [program, ...args]] of commands.entries()) {
2220
- const folder = join(state.evidence, "setup", `${String(index + 1).padStart(2, "0")}-${program}`);
2178
+ const name = `${String(index + 1).padStart(2, "0")}-${program}`;
2179
+ const folder = join(state.evidence, "setup", name);
2180
+ const elapsed = stopwatch();
2221
2181
  const result = await runRecorded(folder, {
2222
2182
  program,
2223
2183
  args,
2224
2184
  cwd: workspace.root,
2225
2185
  env: {
2226
- ...process.env,
2186
+ ...operatorEnv(),
2227
2187
  ...workspace.config.env
2228
2188
  }
2229
2189
  });
2190
+ state.timings.push({
2191
+ phase: `setup/${name}${result.exitCode === 0 ? "" : " (failed)"}`,
2192
+ ms: elapsed()
2193
+ });
2230
2194
  if (result.exitCode !== 0) throw new Error(`setup \`${result.command}\` exited ${result.exitCode}; see ${folder}`);
2231
2195
  }
2232
2196
  }
2197
+ /** The last 300 characters of each non-empty stream of a command, since a tool may print its message on either. */
2198
+ function outputTail(result) {
2199
+ return [["stdout", result.stdout], ["stderr", result.stderr]].filter(([, text]) => text.trim() !== "").map(([name, text]) => `${name}: ${text.trim().slice(-300)}`).join("\n");
2200
+ }
2233
2201
  async function packInterface(workspace, state) {
2234
2202
  const folder = join(state.dir, "pack");
2235
2203
  mkdirSync(folder, { recursive: true });
@@ -2245,7 +2213,7 @@ async function packInterface(workspace, state) {
2245
2213
  env
2246
2214
  });
2247
2215
  const tarball = readdirSync(folder).find((file) => file.endsWith(".tgz"));
2248
- if (packed.exitCode !== 0 || tarball === void 0) throw new Error(`pnpm pack failed: ${packed.stderr.slice(-300)}`);
2216
+ if (packed.exitCode !== 0 || tarball === void 0) throw new Error(`pnpm pack failed; see ${packed.folder}\n${outputTail(packed)}`);
2249
2217
  const extract = await runRecorded(join(state.evidence, "setup", "extract"), {
2250
2218
  program: "tar",
2251
2219
  args: [
@@ -2283,7 +2251,7 @@ function recorded(dir) {
2283
2251
  };
2284
2252
  }
2285
2253
  }
2286
- function entries(folder) {
2254
+ function entries$1(folder) {
2287
2255
  try {
2288
2256
  return readdirSync(folder, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join(folder, entry.name));
2289
2257
  } catch {
@@ -2310,7 +2278,7 @@ function pruneRun(dir, now) {
2310
2278
  */
2311
2279
  function pruneRuns() {
2312
2280
  const now = Date.now();
2313
- for (const repo of entries(RUNS_DIR)) for (const dir of entries(repo)) pruneRun(dir, now);
2281
+ for (const repo of entries$1(RUNS_DIR)) for (const dir of entries$1(repo)) pruneRun(dir, now);
2314
2282
  }
2315
2283
  /**
2316
2284
  * Creates an isolated run: runs the setup (the build by default), then
@@ -2318,18 +2286,27 @@ function pruneRuns() {
2318
2286
  * it, a template is served from an offline manifest repository, and every
2319
2287
  * kind's backing `services` start in Docker on free ports.
2320
2288
  */
2321
- async function createRun(workspace, baseRef, title) {
2289
+ async function createRun(workspace, baseRef, title, all) {
2290
+ const pruned = stopwatch();
2322
2291
  pruneRuns();
2323
- const state = allocateRun(workspace, baseRef, title);
2292
+ const pruneMs = pruned();
2293
+ const state = allocateRun(workspace, baseRef, title, all);
2294
+ state.timings.push({
2295
+ phase: "prune old runs",
2296
+ ms: pruneMs
2297
+ });
2324
2298
  saveRun(state);
2325
- await runSetup(workspace, state);
2326
- if (workspace.kind === "interface") state.pack = await packInterface(workspace, state);
2327
- startServices(workspace, state, containerPrefix());
2328
- if (workspace.kind === "template") {
2329
- state.manifestUrl = seedSources(workspace, state);
2330
- state.templateName = templateName(workspace);
2299
+ try {
2300
+ await runSetup(workspace, state);
2301
+ if (workspace.kind === "interface") state.pack = await timed(state, "pack interface", () => packInterface(workspace, state));
2302
+ startServices(workspace, state, containerPrefix());
2303
+ if (workspace.kind === "template") await timed(state, "seed template sources", () => {
2304
+ state.manifestUrl = seedSources(workspace, state);
2305
+ state.templateName = templateName(workspace);
2306
+ });
2307
+ } finally {
2308
+ saveRun(state);
2331
2309
  }
2332
- saveRun(state);
2333
2310
  return state;
2334
2311
  }
2335
2312
  function alive(pid) {
@@ -2429,164 +2406,6 @@ function clean(state) {
2429
2406
  return lines;
2430
2407
  }
2431
2408
  //#endregion
2432
- //#region src/verify/map.ts
2433
- /** The feature that always runs, whatever the diff touches. */
2434
- const SMOKE_FEATURE = "smoke";
2435
- const DEFAULT_USER_FACING = {
2436
- interface: ["src/**"],
2437
- module: ["src/**"],
2438
- template: ["**"]
2439
- };
2440
- const VERIFICATION_LAYER = [
2441
- ".agents/**",
2442
- ".claude/**",
2443
- ".github/**"
2444
- ];
2445
- /** Tests are user-facing in a template, since `ajs` copies its whole tree except `.agents/` and `.claude/skills/`. */
2446
- const NOT_USER_FACING = {
2447
- interface: [
2448
- "**/*.test.ts",
2449
- "**/test/**",
2450
- "**/tests/**",
2451
- ...VERIFICATION_LAYER
2452
- ],
2453
- module: [
2454
- "**/*.test.ts",
2455
- "**/test/**",
2456
- "**/tests/**",
2457
- ...VERIFICATION_LAYER
2458
- ],
2459
- template: VERIFICATION_LAYER
2460
- };
2461
- function frontmatterLists(text) {
2462
- const lists = /* @__PURE__ */ new Map();
2463
- const match = /^---\n([\s\S]*?)\n---\n/.exec(text);
2464
- let current;
2465
- for (const line of (match?.[1] ?? "").split("\n")) {
2466
- const key = /^(\w+):\s*$/.exec(line);
2467
- const item = /^\s+-\s+(.+)$/.exec(line);
2468
- if (key) {
2469
- current = [];
2470
- lists.set(key[1], current);
2471
- } else if (item && current) current.push(item[1].replace(/^"|"$/g, ""));
2472
- }
2473
- return lists;
2474
- }
2475
- /** Reads every feature file under features/, README.md excluded. */
2476
- function readFeatures(workspace) {
2477
- const folder = join(workspace.skill, "features");
2478
- if (!existsSync(folder)) return [];
2479
- return readdirSync(folder).filter((file) => file.endsWith(".md") && file !== "README.md").sort().map((file) => {
2480
- const body = readFileSync(join(folder, file), "utf8");
2481
- const lists = frontmatterLists(body);
2482
- return {
2483
- name: file.replace(/\.md$/, ""),
2484
- file: relative(workspace.root, join(folder, file)),
2485
- covers: lists.get("covers") ?? [],
2486
- docs: lists.get("docs") ?? [],
2487
- journeys: lists.get("journeys") ?? [],
2488
- body
2489
- };
2490
- });
2491
- }
2492
- function matches(file, globs) {
2493
- return globs.some((glob) => matchesGlob(file, glob));
2494
- }
2495
- /** Whether a user can observe a change to `file`. */
2496
- function userFacing(workspace, file) {
2497
- return matches(file, workspace.config.userFacing ?? DEFAULT_USER_FACING[workspace.kind]) && !matches(file, NOT_USER_FACING[workspace.kind]);
2498
- }
2499
- function trackedFiles(workspace) {
2500
- return git(workspace.root, ["ls-files"]).split("\n").filter(Boolean);
2501
- }
2502
- /** The features whose globs cover `file`. */
2503
- function featuresOf(file, features) {
2504
- return features.filter((feature) => matches(file, feature.covers)).map((feature) => feature.name);
2505
- }
2506
- function featureProblems(workspace, feature, known, tracked) {
2507
- const problems = [];
2508
- if (feature.journeys.length === 0) problems.push(`${feature.file}: lists no journeys`);
2509
- for (const journey of feature.journeys) {
2510
- if (!known.has(journey)) problems.push(`${feature.file}: journey ${journey} is neither built in nor in verify.config.ts journeys`);
2511
- if (!feature.body.includes(`\`${journey}\``)) problems.push(`${feature.file}: journey \`${journey}\` is not described`);
2512
- }
2513
- if (feature.name !== "smoke" && feature.covers.length === 0) problems.push(`${feature.file}: lists no covers globs`);
2514
- for (const glob of feature.covers) if (!tracked.some((file) => matchesGlob(file, glob))) problems.push(`${feature.file}: glob ${glob} matches no tracked file`);
2515
- if (feature.docs.length === 0) problems.push(`${feature.file}: lists no docs pages`);
2516
- for (const doc of feature.docs) if (!existsSync(join(workspace.root, doc))) problems.push(`${feature.file}: docs path ${doc} does not exist`);
2517
- return problems;
2518
- }
2519
- /**
2520
- * Checks the repo-owned layer against the repository: a smoke feature
2521
- * exists, every user-facing file is covered, every feature names known
2522
- * journeys and docs pages that exist, and every config journey is used. One
2523
- * line per problem.
2524
- */
2525
- function checkMap(workspace, builtins) {
2526
- const features = readFeatures(workspace);
2527
- const tracked = trackedFiles(workspace);
2528
- const configJourneys = Object.keys(workspace.config.journeys ?? {});
2529
- const known = /* @__PURE__ */ new Set([...builtins, ...configJourneys]);
2530
- const problems = [];
2531
- if (!features.some((feature) => feature.name === "smoke")) problems.push(`${skillFolder(workspace.kind)}/features/${SMOKE_FEATURE}.md is missing: every verdict runs the smoke feature`);
2532
- for (const file of tracked.filter((path) => userFacing(workspace, path))) if (featuresOf(file, features).length === 0) problems.push(`unmapped: ${file} matches no feature glob`);
2533
- for (const feature of features) problems.push(...featureProblems(workspace, feature, known, tracked));
2534
- for (const journey of configJourneys) if (!features.some((feature) => feature.journeys.includes(journey))) problems.push(`verify.config.ts journey ${journey} is in no feature file`);
2535
- return problems;
2536
- }
2537
- /** The files a change touches: committed since the merge base, staged, unstaged and untracked. */
2538
- function changedFiles(workspace, baseSha) {
2539
- const committed = git(workspace.root, [
2540
- "diff",
2541
- "--name-only",
2542
- baseSha
2543
- ]).split("\n");
2544
- const untracked = git(workspace.root, [
2545
- "ls-files",
2546
- "--others",
2547
- "--exclude-standard"
2548
- ]).split("\n");
2549
- return [...new Set([...committed, ...untracked].filter(Boolean))].sort();
2550
- }
2551
- /**
2552
- * Maps the change from the merge base of `baseRef` onto features: every
2553
- * touched feature plus the smoke feature runs, and a user-facing file outside
2554
- * every glob is an unmapped surface.
2555
- */
2556
- function planFor(workspace, baseRef) {
2557
- const features = readFeatures(workspace);
2558
- const baseSha = git(workspace.root, [
2559
- "merge-base",
2560
- baseRef,
2561
- "HEAD"
2562
- ]);
2563
- const changed = changedFiles(workspace, baseSha);
2564
- const touched = features.map((feature) => ({
2565
- feature: feature.name,
2566
- files: changed.filter((file) => matches(file, feature.covers))
2567
- })).filter((entry) => entry.files.length > 0);
2568
- const unmapped = changed.filter((file) => userFacing(workspace, file) && featuresOf(file, features).length === 0);
2569
- const touchedNames = new Set(touched.map((entry) => entry.feature));
2570
- const docs = [...new Set(features.filter((feature) => touchedNames.has(feature.name)).flatMap((feature) => feature.docs))];
2571
- const run = [SMOKE_FEATURE, ...[...touchedNames].filter((name) => name !== SMOKE_FEATURE)];
2572
- const dirty = git(workspace.root, [
2573
- "status",
2574
- "--porcelain",
2575
- "--untracked-files=no"
2576
- ]).split("\n").filter(Boolean);
2577
- return {
2578
- baseRef,
2579
- baseSha,
2580
- headSha: git(workspace.root, ["rev-parse", "HEAD"]),
2581
- changed,
2582
- touched,
2583
- unmapped,
2584
- docs,
2585
- run,
2586
- dirty
2587
- };
2588
- }
2589
- //#endregion
2590
2409
  //#region src/verify/publish.ts
2591
2410
  const EVIDENCE_BRANCH = "verify-evidence";
2592
2411
  const SCRATCH_BRANCH = "verify-evidence-scratch";
@@ -2796,6 +2615,478 @@ const BUILTIN_JOURNEYS = {
2796
2615
  }
2797
2616
  };
2798
2617
  //#endregion
2618
+ //#region src/verify/app-fault.ts
2619
+ /** The signals a process outside the run sends to end an app. Any other signal, such as SIGABRT or SIGSEGV, is the product crashing. */
2620
+ const OUTSIDE_KILLS = /* @__PURE__ */ new Set([
2621
+ "SIGKILL",
2622
+ "SIGTERM",
2623
+ "SIGHUP",
2624
+ "SIGINT"
2625
+ ]);
2626
+ /**
2627
+ * Why one of the apps a journey started points away from the product, or
2628
+ * `null`: an app killed by SIGKILL, SIGTERM, SIGHUP or SIGINT that the harness
2629
+ * did not send, or an app that could not bind the port the harness gave it.
2630
+ */
2631
+ function appFault(started) {
2632
+ for (const { app, sent, exit } of started) {
2633
+ const label = `${app.name} (pid ${app.pid})`;
2634
+ if (exit?.signal && OUTSIDE_KILLS.has(exit.signal) && !sent.includes(exit.signal)) return `${label} was killed by ${exit.signal}, a signal the harness did not send`;
2635
+ const output = readOutput(app.folder);
2636
+ const port = new RegExp(`(?<![0-9])${app.port}(?![0-9])`);
2637
+ const taken = `${output.stdout}\n${output.stderr}`.split("\n").find((line) => line.includes("EADDRINUSE") && port.test(line));
2638
+ if (taken !== void 0) return `${label} could not bind port ${app.port}, which another process holds: ${taken.trim().slice(0, 300)}`;
2639
+ }
2640
+ return null;
2641
+ }
2642
+ //#endregion
2643
+ //#region src/verify/caches.ts
2644
+ /** Where `ajs` installs the modules of a project that names no other `cacheFolder`. */
2645
+ const PROJECT_CACHE = join(".antelope", "cache");
2646
+ const MANIFEST = "manifest.json";
2647
+ /** Where `ajs` keeps a project's lock records, one folder per `<package>@<version>`. */
2648
+ const LOCK_FOLDER = "antelope-lock";
2649
+ /** How many milliseconds the harness waits at most for the file system clock to pass an install. */
2650
+ const CLOCK_ATTEMPTS = 100;
2651
+ /** A start time later than every command's, for a module the harness first sees outside a command: any lock record it has counts as one `ajs` installed it from. */
2652
+ const UNKNOWN_START = 2n ** 63n;
2653
+ /** A version `ajs` records for a registry package; a git module records `git:<branch>:<commit>`. */
2654
+ const REGISTRY_VERSION = /^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.+-]*)?$/;
2655
+ /** When the folder at `path` was created, which tells apart the folders created one after another there even when the file system reuses the inode; `0n` when the file system does not record it. */
2656
+ function bornAt(path) {
2657
+ try {
2658
+ return lstatSync(path, { bigint: true }).birthtimeNs;
2659
+ } catch {
2660
+ return 0n;
2661
+ }
2662
+ }
2663
+ /** Whether `project` held the lock record of `name@version` when a command that started at `start` began; `ajs` writes a record only after an install that used none. */
2664
+ function pinned(project, name, version, start) {
2665
+ try {
2666
+ const record = join(project, LOCK_FOLDER, `${name}@${version}`);
2667
+ return lstatSync(record, { bigint: true }).mtimeNs <= start;
2668
+ } catch {
2669
+ return false;
2670
+ }
2671
+ }
2672
+ function names(folder) {
2673
+ try {
2674
+ return readdirSync(folder).filter((name) => !name.startsWith("."));
2675
+ } catch {
2676
+ return [];
2677
+ }
2678
+ }
2679
+ /** Every entry under `root`, by its path relative to `root`, with its modification time; symbolic links are listed, not followed. */
2680
+ function* entries(root, prefix = "") {
2681
+ for (const entry of readdirSync(join(root, prefix), { withFileTypes: true })) {
2682
+ const path = join(prefix, entry.name);
2683
+ const directory = entry.isDirectory();
2684
+ yield {
2685
+ path,
2686
+ mtime: lstatSync(join(root, path), { bigint: true }).mtimeNs,
2687
+ directory
2688
+ };
2689
+ if (directory) yield* entries(root, path);
2690
+ }
2691
+ }
2692
+ /** Whether a project's antelope config (antelope.json or antelope.config.*) mentions `cacheFolder`, so `ajs` may install somewhere other than `.antelope/cache`. */
2693
+ function namesCacheFolder(project) {
2694
+ try {
2695
+ return readdirSync(project).filter((name) => /^antelope(\.config)?\.[cm]?[jt]s(on)?$/.test(name)).some((name) => /\bcacheFolder\b/.test(readFileSync(join(project, name), "utf8")));
2696
+ } catch {
2697
+ return false;
2698
+ }
2699
+ }
2700
+ /**
2701
+ * Copies `source` into a staging entry next to `target` and renames it onto
2702
+ * `target`, so a reader sees all of the copy or none of it. A failed copy, a
2703
+ * deleted source or a `target` another writer filled first leaves `target`
2704
+ * as it was.
2705
+ */
2706
+ function publish(source, target) {
2707
+ const staging = join(dirname(target), `.staging-${randomBytes(6).toString("hex")}`);
2708
+ try {
2709
+ cpSync(source, staging, {
2710
+ recursive: true,
2711
+ verbatimSymlinks: true,
2712
+ preserveTimestamps: true
2713
+ });
2714
+ renameSync(staging, target);
2715
+ } catch {
2716
+ return;
2717
+ } finally {
2718
+ removeTree(staging);
2719
+ }
2720
+ }
2721
+ /** The manifest `ajs` writes each time it finishes installing a module: its modification time and each module's version, sorted by module. */
2722
+ function manifestOf(cache) {
2723
+ try {
2724
+ const file = join(cache, MANIFEST);
2725
+ const manifest = parseJson(readFileSync(file, "utf8"));
2726
+ if (!isObject(manifest)) return void 0;
2727
+ const modules = Object.entries(manifest).map(([name, version]) => [name, String(version)]).sort(([left], [right]) => left < right ? -1 : 1);
2728
+ return {
2729
+ mtime: lstatSync(file, { bigint: true }).mtimeNs,
2730
+ modules
2731
+ };
2732
+ } catch {
2733
+ return;
2734
+ }
2735
+ }
2736
+ /**
2737
+ * Why `cache` is not exactly what `ajs` installed, or `undefined` when it is.
2738
+ * Each file of a module must be no newer than the module's bound. Outside the
2739
+ * modules, `ajs` writes only the manifest and the folders that hold the
2740
+ * modules, which must be no newer than the manifest.
2741
+ */
2742
+ function changeIn(cache, installed, manifest) {
2743
+ const modules = [...installed].map(([name, module]) => ({
2744
+ folder: join(name),
2745
+ label: `${name}@${module.version}`,
2746
+ bound: module.bound
2747
+ }));
2748
+ try {
2749
+ for (const { path, mtime, directory } of entries(cache)) {
2750
+ if (path === MANIFEST) continue;
2751
+ const owner = modules.find(({ folder }) => path === folder || path.startsWith(`${folder}${sep}`));
2752
+ if (owner === void 0) {
2753
+ if (!directory) return `${path} belongs to no installed module`;
2754
+ if (mtime > manifest) return `${path} changed after ajs wrote ${MANIFEST}`;
2755
+ } else if (mtime > owner.bound) return `${path} changed after ajs installed ${owner.label}`;
2756
+ }
2757
+ } catch (error) {
2758
+ return `it could not be read: ${error instanceof Error ? error.message : String(error)}`;
2759
+ }
2760
+ }
2761
+ /**
2762
+ * The module caches of the projects one journey creates, and the run's kept
2763
+ * copies of them in `store`. Kept copies are never written, so journeys of
2764
+ * one run can copy them at once.
2765
+ *
2766
+ * Only a cache exactly as a fresh `ajs` install leaves it is kept. `ajs`
2767
+ * writes a registry module's files, then records its version in
2768
+ * `manifest.json`, so the manifest's modification time when the harness first
2769
+ * sees the module bounds every file of it. A file modified later, a module
2770
+ * whose version or folder changes after that, a module `ajs` installed from a
2771
+ * lock record the project held before the command, and a git module, which
2772
+ * `ajs` updates in place and builds with the project's own install command,
2773
+ * each keep the cache out of the store.
2774
+ */
2775
+ var ProjectCaches = class {
2776
+ store;
2777
+ projects = /* @__PURE__ */ new Map();
2778
+ constructor(store) {
2779
+ this.store = store;
2780
+ }
2781
+ /** Gives a project that has installed no module yet a copy of the largest kept cache, so `ajs` reuses the packages an earlier journey installed instead of packing and installing them again. A project whose antelope config names its own `cacheFolder` gets no copy, since `ajs` would not read `.antelope/cache`. */
2782
+ seed(project) {
2783
+ const state = { installed: /* @__PURE__ */ new Map() };
2784
+ this.projects.set(project, state);
2785
+ if (namesCacheFolder(project)) return;
2786
+ const cache = join(project, PROJECT_CACHE);
2787
+ if ((manifestOf(cache)?.modules.length ?? 0) > 0) {
2788
+ this.observe();
2789
+ return;
2790
+ }
2791
+ let largest;
2792
+ for (const name of names(this.store)) {
2793
+ const folder = join(this.store, name);
2794
+ const size = manifestOf(folder)?.modules.length ?? 0;
2795
+ if (size > (largest?.size ?? 0)) largest = {
2796
+ folder,
2797
+ size
2798
+ };
2799
+ }
2800
+ if (largest === void 0) return;
2801
+ mkdirSync(dirname(cache), { recursive: true });
2802
+ publish(largest.folder, cache);
2803
+ const copied = manifestOf(cache);
2804
+ if (copied === void 0) return;
2805
+ let bound = copied.mtime;
2806
+ try {
2807
+ for (const { mtime } of entries(cache)) if (mtime > bound) bound = mtime;
2808
+ } catch {
2809
+ return;
2810
+ }
2811
+ for (const [name, version] of copied.modules) state.installed.set(name, {
2812
+ version,
2813
+ born: bornAt(join(cache, name)),
2814
+ bound
2815
+ });
2816
+ this.passClock(bound);
2817
+ }
2818
+ /**
2819
+ * Records the bound of each module that appeared in a project's manifest
2820
+ * since the last call, and refuses the cache of a project whose recorded
2821
+ * module left the manifest, changed version or sits in a new folder. A
2822
+ * module that appeared with a lock record no newer than `start`, the start
2823
+ * of the command that just ended, was installed from that record.
2824
+ */
2825
+ observe(start = UNKNOWN_START) {
2826
+ let latest = -1n;
2827
+ for (const [project, state] of this.projects) {
2828
+ const cache = join(project, PROJECT_CACHE);
2829
+ const manifest = manifestOf(cache);
2830
+ if (manifest === void 0) continue;
2831
+ const listed = new Map(manifest.modules);
2832
+ for (const [name, known] of state.installed) if (!listed.has(name)) state.refused ??= `${name} left ${MANIFEST} after ajs installed ${name}@${known.version}`;
2833
+ for (const [name, version] of manifest.modules) {
2834
+ const born = bornAt(join(cache, name));
2835
+ const known = state.installed.get(name);
2836
+ if (known === void 0) {
2837
+ state.installed.set(name, {
2838
+ version,
2839
+ born,
2840
+ bound: manifest.mtime
2841
+ });
2842
+ if (manifest.mtime > latest) latest = manifest.mtime;
2843
+ if (pinned(project, name, version, start)) state.refused ??= `ajs installed ${name}@${version} from the project's lock record`;
2844
+ } else if (known.version !== version) state.refused ??= `${name} changed from ${known.version} to ${version} after ajs installed it`;
2845
+ else if (born !== known.born) state.refused ??= `${name} was created again after ajs installed ${name}@${version}`;
2846
+ }
2847
+ }
2848
+ if (latest >= 0n) this.passClock(latest);
2849
+ }
2850
+ /** Observes the project caches once a command or an app start has ended, and returns its result. Its `folder` is the step folder the harness created as it began, so a lock record no newer than that folder was there before the command. */
2851
+ observed(result) {
2852
+ this.observe(bornAt(result.folder) || UNKNOWN_START);
2853
+ return result;
2854
+ }
2855
+ /**
2856
+ * Returns once the file system clock has moved past `time`, so a file the
2857
+ * journey writes from now on is newer than a bound of `time`. The clock
2858
+ * advances in ticks of a few milliseconds, and a write within the tick of
2859
+ * the bound would carry the bound's time.
2860
+ */
2861
+ passClock(time) {
2862
+ const probe = join(this.store, `.clock-${randomBytes(6).toString("hex")}`);
2863
+ const pause = new Int32Array(new SharedArrayBuffer(4));
2864
+ try {
2865
+ mkdirSync(this.store, { recursive: true });
2866
+ for (let attempt = 0; attempt < CLOCK_ATTEMPTS; attempt += 1) {
2867
+ writeFileSync(probe, "");
2868
+ if (lstatSync(probe, { bigint: true }).mtimeNs > time) return;
2869
+ Atomics.wait(pause, 0, 0, 1);
2870
+ }
2871
+ } catch {
2872
+ return;
2873
+ } finally {
2874
+ rmSync(probe, { force: true });
2875
+ }
2876
+ }
2877
+ /**
2878
+ * Keeps a copy of each project's module cache for the rest of the run, one per set of installed versions; a set already kept stays as it is. Call it once the journey passed and every app stopped.
2879
+ * Returns why each cache that changed after `ajs` installed it was not kept.
2880
+ */
2881
+ keep() {
2882
+ this.observe();
2883
+ const skipped = [];
2884
+ for (const [project, state] of this.projects) {
2885
+ const cache = join(project, PROJECT_CACHE);
2886
+ const manifest = manifestOf(cache);
2887
+ if (manifest === void 0 || manifest.modules.length === 0) continue;
2888
+ const target = join(this.store, createHash("sha256").update(manifest.modules.map((entry) => entry.join("@")).join("\n")).digest("hex").slice(0, 16));
2889
+ if (existsSync(target)) continue;
2890
+ const unregistered = manifest.modules.find(([, version]) => !REGISTRY_VERSION.test(version));
2891
+ const change = state.refused ?? (unregistered === void 0 ? changeIn(cache, state.installed, manifest.mtime) : `${unregistered.join("@")} is not a registry version, and ajs updates a git module in place`);
2892
+ if (change !== void 0) {
2893
+ skipped.push(`${cache}: ${change}`);
2894
+ continue;
2895
+ }
2896
+ mkdirSync(this.store, { recursive: true });
2897
+ publish(cache, target);
2898
+ }
2899
+ return skipped;
2900
+ }
2901
+ };
2902
+ //#endregion
2903
+ //#region src/verify/prepared.ts
2904
+ const KEY = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
2905
+ /** The path of `path` inside `root`, or `null` when it lies outside. */
2906
+ function inside(root, path) {
2907
+ const rest = relative(root, path);
2908
+ if (rest === ".." || rest.startsWith(`..${sep}`) || isAbsolute(rest)) return null;
2909
+ return rest;
2910
+ }
2911
+ /** Surveys `folder` in one walk that descends only into real directories, so it never follows a symlink; a target may name the folder by its real path. */
2912
+ function survey(folder) {
2913
+ const real = realpathSync(folder);
2914
+ const found = {
2915
+ links: [],
2916
+ special: null
2917
+ };
2918
+ const walk = (at) => {
2919
+ for (const entry of readdirSync(join(folder, at), { withFileTypes: true })) {
2920
+ const path = join(at, entry.name);
2921
+ if (entry.isDirectory()) walk(path);
2922
+ else if (entry.isSymbolicLink()) {
2923
+ const pointed = readlinkSync(join(folder, path));
2924
+ const target = isAbsolute(pointed) ? inside(folder, pointed) ?? inside(real, pointed) : null;
2925
+ if (target !== null) found.links.push({
2926
+ path,
2927
+ target
2928
+ });
2929
+ } else if (!entry.isFile()) found.special ??= path;
2930
+ }
2931
+ };
2932
+ walk("");
2933
+ return found;
2934
+ }
2935
+ /**
2936
+ * Copies `source` to `target`, which ends up at `root`. Each of `links`
2937
+ * points to its target inside `root`, so no copy reaches another journey's
2938
+ * folder. Every other symlink keeps its target, so pnpm's relative links in a
2939
+ * copy never point back into the source.
2940
+ */
2941
+ function copyTree(source, target, links = [], root = target) {
2942
+ cpSync(source, target, {
2943
+ recursive: true,
2944
+ verbatimSymlinks: true,
2945
+ preserveTimestamps: true
2946
+ });
2947
+ for (const link of links) {
2948
+ unlinkSync(join(target, link.path));
2949
+ symlinkSync(join(root, link.target), join(target, link.path));
2950
+ }
2951
+ }
2952
+ /** Judges the build of `folder` by the steps it recorded. */
2953
+ function judge$1(steps, folder) {
2954
+ const checks = steps.flatMap((step) => step.checks);
2955
+ if (checks.length === 0) return {
2956
+ kept: false,
2957
+ reason: "the build recorded no check"
2958
+ };
2959
+ if (!checks.every((check) => check.pass)) return {
2960
+ kept: false,
2961
+ reason: "a check of the build failed"
2962
+ };
2963
+ const found = survey(folder);
2964
+ return found.special === null ? {
2965
+ kept: true,
2966
+ links: found.links
2967
+ } : {
2968
+ kept: false,
2969
+ reason: `${found.special} is not a file, directory or symlink, which a copy would drop`
2970
+ };
2971
+ }
2972
+ /** The links `keep` recorded, each a path then its target, joined by NUL, the one character no path holds. */
2973
+ function readLinks(kept) {
2974
+ const fields = readFileSync(join(kept, "links"), "utf8").split("\0");
2975
+ const links = [];
2976
+ for (let at = 0; at + 1 < fields.length; at += 2) links.push({
2977
+ path: fields[at],
2978
+ target: fields[at + 1]
2979
+ });
2980
+ return links;
2981
+ }
2982
+ /** The journey that built what the run kept, as its path under the evidence. */
2983
+ function builder(kept) {
2984
+ return readFileSync(join(kept, "from.txt"), "utf8").trim();
2985
+ }
2986
+ /**
2987
+ * Copies what the run kept into the journey, with the evidence of that build
2988
+ * under the step `marker`, and returns `null`. When the copy fails, or what
2989
+ * the run kept is gone or replaced once it ends, as when the journey that
2990
+ * built reruns meanwhile, the partial copy is removed and this returns why.
2991
+ */
2992
+ function reuse$1({ marker, key, kept, folder }) {
2993
+ const evidence = join(marker, "build");
2994
+ try {
2995
+ const memo = statSync(kept).ino;
2996
+ copyTree(join(kept, "evidence"), evidence);
2997
+ copyTree(join(kept, "folder"), folder, readLinks(kept));
2998
+ const from = builder(kept);
2999
+ if (statSync(kept).ino !== memo) throw new Error(`${kept} was replaced while it was copied`);
3000
+ writeFileSync(join(marker, "prepared.txt"), `${key}: ${folder} is a copy of what ${from} prepared; build/ holds the evidence of that build\n`);
3001
+ return null;
3002
+ } catch (error) {
3003
+ removeTree(evidence);
3004
+ removeTree(folder);
3005
+ return error instanceof Error ? error.message : String(error);
3006
+ }
3007
+ }
3008
+ /** Removes what the run kept by first renaming it away, so a journey copying it meanwhile fails instead of copying a half-deleted tree. */
3009
+ function drop(kept) {
3010
+ const trash = mkdtempSync(`${kept}.`);
3011
+ renameSync(kept, join(trash, "dropped"));
3012
+ removeTree(trash);
3013
+ }
3014
+ /** Keeps the built folder and the build's step evidence for the rest of the run, published by one rename; when another journey kept the key first, its copy stays and this returns `false`. */
3015
+ function keep(context, kept, folder, built) {
3016
+ mkdirSync(dirname(kept), { recursive: true });
3017
+ const staging = mkdtempSync(`${kept}.`);
3018
+ try {
3019
+ mkdirSync(join(staging, "evidence"));
3020
+ for (const step of built.steps) copyTree(join(context.folder, step), join(staging, "evidence", step));
3021
+ copyTree(folder, join(staging, "folder"), built.links, join(kept, "folder"));
3022
+ writeFileSync(join(staging, "links"), built.links.flatMap((link) => [link.path, link.target]).join("\0"));
3023
+ writeFileSync(join(staging, "from.txt"), `${relative(context.state.evidence, context.folder)}\n`);
3024
+ renameSync(staging, kept);
3025
+ return true;
3026
+ } catch (error) {
3027
+ if (!existsSync(kept)) throw error;
3028
+ return false;
3029
+ } finally {
3030
+ removeTree(staging);
3031
+ }
3032
+ }
3033
+ /** Runs `build` with a HOME of its own for its commands, deleted when it ends. */
3034
+ async function buildAlone(context, key, folder, build) {
3035
+ const outer = context.buildHome;
3036
+ const home = join(context.home, `.prepared-${key}`);
3037
+ mkdirSync(home, { recursive: true });
3038
+ context.buildHome = home;
3039
+ try {
3040
+ await build(folder);
3041
+ } finally {
3042
+ context.buildHome = outer;
3043
+ removeTree(home);
3044
+ }
3045
+ }
3046
+ /** Builds `key` in this journey after the step `marker`, which marks the steps after it as the build and says, after `before`, whether the run kept it. */
3047
+ async function buildHere(context, where, build, before = "") {
3048
+ const { marker, key, kept, folder } = where;
3049
+ const note = (outcome) => writeFileSync(join(marker, "prepared.txt"), `${key}: ${before}${folder} was built here by the steps after this one; ${outcome}\n`);
3050
+ const earlier = new Set(readdirSync(context.folder));
3051
+ const checked = context.steps.length;
3052
+ mkdirSync(folder, { recursive: true });
3053
+ try {
3054
+ await buildAlone(context, key, folder, build);
3055
+ } catch (error) {
3056
+ note("not kept: the build threw, so the next journey builds again");
3057
+ throw error;
3058
+ }
3059
+ const verdict = judge$1(context.steps.slice(checked), folder);
3060
+ if (!verdict.kept) {
3061
+ note(`not kept: ${verdict.reason}, so the next journey builds again`);
3062
+ return;
3063
+ }
3064
+ note(keep(context, kept, folder, {
3065
+ steps: readdirSync(context.folder).filter((entry) => !earlier.has(entry)),
3066
+ links: verdict.links
3067
+ }) ? "kept, so later journeys of the run copy it" : `not kept: ${builder(kept)} kept ${key} first, so later journeys copy that build`);
3068
+ }
3069
+ /** `JourneyContext.prepared` for one journey of the run. */
3070
+ async function prepare(context, key, build) {
3071
+ if (!KEY.test(key)) throw new Error(`prepared key ${JSON.stringify(key)} must be lowercase letters and digits joined by dashes`);
3072
+ const folder = join(context.work, key);
3073
+ if (existsSync(folder)) throw new Error(`${folder} already exists: a journey prepares ${key} once`);
3074
+ const kept = join(context.state.dir, "prepared", key);
3075
+ if (existsSync(kept) && builder(kept) === relative(context.state.evidence, context.folder)) drop(kept);
3076
+ const where = {
3077
+ marker: context.step(`prepared ${key}`),
3078
+ key,
3079
+ kept,
3080
+ folder
3081
+ };
3082
+ if (!existsSync(kept)) await buildHere(context, where, build);
3083
+ else {
3084
+ const failed = reuse$1(where);
3085
+ if (failed !== null) await buildHere(context, where, build, `copying what the run kept failed (${failed}), so `);
3086
+ }
3087
+ return folder;
3088
+ }
3089
+ //#endregion
2799
3090
  //#region src/verify/context.ts
2800
3091
  var JourneyAbort = class extends Error {};
2801
3092
  var NotVerified = class extends Error {};
@@ -2809,19 +3100,9 @@ const READINESS_RETRY_MS = [
2809
3100
  1e3,
2810
3101
  2e3
2811
3102
  ];
2812
- /** The signals a process outside the run sends to end an app. Any other signal, such as SIGABRT or SIGSEGV, is the product crashing. */
2813
- const OUTSIDE_KILLS = /* @__PURE__ */ new Set([
2814
- "SIGKILL",
2815
- "SIGTERM",
2816
- "SIGHUP",
2817
- "SIGINT"
2818
- ]);
2819
3103
  function slug(text) {
2820
3104
  return text.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
2821
3105
  }
2822
- function sleep(ms) {
2823
- return new Promise((resolve) => setTimeout(resolve, ms));
2824
- }
2825
3106
  /** The harness side of a journey: every driving call writes its evidence into a new numbered step folder. */
2826
3107
  var VerifyContext = class {
2827
3108
  repo;
@@ -2829,12 +3110,17 @@ var VerifyContext = class {
2829
3110
  work;
2830
3111
  home;
2831
3112
  steps = [];
3113
+ /** The HOME every command gets while `prepared` runs a build; the journey's HOME when `null`. */
3114
+ buildHome = null;
2832
3115
  running = [];
2833
3116
  started = [];
2834
3117
  fixtures = /* @__PURE__ */ new Map();
2835
3118
  index = 0;
3119
+ /** Times every step folder of the journey. */
3120
+ clock = new StepClock();
2836
3121
  workspace;
2837
3122
  state;
3123
+ caches;
2838
3124
  constructor(workspace, state, feature, journey) {
2839
3125
  this.workspace = workspace;
2840
3126
  this.state = state;
@@ -2842,6 +3128,7 @@ var VerifyContext = class {
2842
3128
  this.folder = join(state.evidence, feature, journey);
2843
3129
  this.work = join(state.dir, "work", feature, journey);
2844
3130
  this.home = join(state.dir, "homes", feature, journey);
3131
+ this.caches = new ProjectCaches(join(state.dir, "ajs-cache"));
2845
3132
  for (const folder of [
2846
3133
  this.folder,
2847
3134
  this.work,
@@ -2854,16 +3141,18 @@ var VerifyContext = class {
2854
3141
  mkdirSync(folder, { recursive: true });
2855
3142
  }
2856
3143
  }
2857
- /** A fresh numbered step folder. */
3144
+ /** A fresh numbered step folder; it also ends the clock of the previous step and starts its own. */
2858
3145
  step(name) {
2859
3146
  this.index += 1;
2860
3147
  const folder = join(this.folder, `${String(this.index).padStart(2, "0")}-${slug(name)}`);
2861
3148
  mkdirSync(folder, { recursive: true });
3149
+ this.clock.start(basename(folder));
2862
3150
  return folder;
2863
3151
  }
2864
3152
  env(extra = {}) {
2865
- return isolatedEnv(this.workspace, this.home, extra);
3153
+ return isolatedEnv(this.workspace, this.buildHome ?? this.home, extra);
2866
3154
  }
3155
+ prepared = (key, build) => prepare(this, key, build);
2867
3156
  command(name, program, args, options = {}) {
2868
3157
  return runRecorded(this.step(name), {
2869
3158
  program,
@@ -2871,7 +3160,7 @@ var VerifyContext = class {
2871
3160
  cwd: options.cwd ?? this.work,
2872
3161
  env: this.env(options.env),
2873
3162
  timeoutMs: options.timeoutMs
2874
- });
3163
+ }).then((result) => this.caches.observed(result));
2875
3164
  }
2876
3165
  /** Runs `ajs <args>` of the repository's `@antelopejs/core`. */
2877
3166
  ajs(name, args, options = {}) {
@@ -2935,6 +3224,7 @@ var VerifyContext = class {
2935
3224
  const fixture = writeFixture(this.workspace, this.state, folder, config);
2936
3225
  this.fixtures.set(fixture.project, fixture);
2937
3226
  this.tree("fixture project", fixture.project);
3227
+ this.caches.seed(fixture.project);
2938
3228
  return fixture.project;
2939
3229
  }
2940
3230
  /** The log the stub provider wrote, one call per line: the provider's view of what core delivered. */
@@ -2998,7 +3288,7 @@ var VerifyContext = class {
2998
3288
  });
2999
3289
  saveRun(this.state);
3000
3290
  await this.waitReady(app, handle, fixture !== void 0);
3001
- return app;
3291
+ return this.caches.observed(app);
3002
3292
  }
3003
3293
  async probe(app, fixture, folder) {
3004
3294
  const probe = this.workspace.config.template?.probe;
@@ -3029,11 +3319,11 @@ var VerifyContext = class {
3029
3319
  });
3030
3320
  let check = await this.probe(app, fixture, folder);
3031
3321
  while (!check.pass && !exited && Date.now() < deadline) {
3032
- await sleep(POLL_MS);
3322
+ await setTimeout$1(POLL_MS);
3033
3323
  check = await this.probe(app, fixture, folder);
3034
3324
  }
3035
3325
  const settle = this.workspace.config.template?.probe.log ? this.workspace.config.template.probe.settleMs ?? SETTLE_MS : 0;
3036
- if (check.pass && settle > 0) await sleep(settle);
3326
+ if (check.pass && settle > 0) await setTimeout$1(settle);
3037
3327
  const alive = {
3038
3328
  name: "still running after readiness",
3039
3329
  pass: !exited,
@@ -3124,7 +3414,7 @@ var VerifyContext = class {
3124
3414
  pass: set.exitCode === 0,
3125
3415
  detail: set.stderr.slice(-300)
3126
3416
  }], { critical: true });
3127
- return this.ajs(name, [
3417
+ const init = await this.ajs(name, [
3128
3418
  "project",
3129
3419
  "init",
3130
3420
  relative(this.work, project),
@@ -3132,6 +3422,8 @@ var VerifyContext = class {
3132
3422
  this.state.templateName,
3133
3423
  ...flags
3134
3424
  ]);
3425
+ if (init.exitCode === 0) this.caches.seed(project);
3426
+ return init;
3135
3427
  }
3136
3428
  /** Stops every app the journey left running. */
3137
3429
  async cleanup() {
@@ -3151,21 +3443,13 @@ var VerifyContext = class {
3151
3443
  const failure = await this.unready(name, service.ready);
3152
3444
  if (failure !== null) return `the service ${name} no longer passes its readiness command: ${failure}`;
3153
3445
  }
3154
- for (const { app, sent, exit } of this.started) {
3155
- const label = `${app.name} (pid ${app.pid})`;
3156
- if (exit?.signal && OUTSIDE_KILLS.has(exit.signal) && !sent.includes(exit.signal)) return `${label} was killed by ${exit.signal}, a signal the harness did not send`;
3157
- const output = readOutput(app.folder);
3158
- const port = new RegExp(`(?<![0-9])${app.port}(?![0-9])`);
3159
- const taken = `${output.stdout}\n${output.stderr}`.split("\n").find((line) => line.includes("EADDRINUSE") && port.test(line));
3160
- if (taken !== void 0) return `${label} could not bind port ${app.port}, which another process holds: ${taken.trim().slice(0, 300)}`;
3161
- }
3162
- return null;
3446
+ return appFault(this.started);
3163
3447
  }
3164
3448
  /** The last readiness failure of a service after the reruns, or `null` once one rerun passes. */
3165
3449
  async unready(name, ready) {
3166
3450
  let failure = null;
3167
3451
  for (const wait of [0, ...READINESS_RETRY_MS]) {
3168
- await sleep(wait);
3452
+ await setTimeout$1(wait);
3169
3453
  failure = readinessFailure(this.service(name).container, ready, this.step(`${name} readiness after the failure`));
3170
3454
  if (failure === null) return null;
3171
3455
  }
@@ -3201,12 +3485,12 @@ function resetServices(context) {
3201
3485
  for (const [name, service] of Object.entries(context.workspace.config.services ?? {})) {
3202
3486
  if (!service.reset) continue;
3203
3487
  const container = context.service(name).container;
3488
+ const folder = context.step(`reset ${name}`);
3204
3489
  const run = spawnSync("docker", [
3205
3490
  "exec",
3206
3491
  container,
3207
3492
  ...service.reset
3208
3493
  ], { encoding: "utf8" });
3209
- const folder = context.step(`reset ${name}`);
3210
3494
  writeFileSync(join(folder, "command.txt"), `docker exec ${container} ${service.reset.join(" ")}\n`);
3211
3495
  writeFileSync(join(folder, "stdout.txt"), run.stdout ?? "");
3212
3496
  writeFileSync(join(folder, "stderr.txt"), run.stderr ?? "");
@@ -3249,6 +3533,7 @@ async function runOne(workspace, state, feature, id, journey) {
3249
3533
  }
3250
3534
  async function driveJourney(context, feature, id, journey) {
3251
3535
  const { state } = context;
3536
+ const elapsed = stopwatch();
3252
3537
  let failure = {
3253
3538
  error: null,
3254
3539
  notVerified: null
@@ -3268,15 +3553,20 @@ async function driveJourney(context, feature, id, journey) {
3268
3553
  notVerified: fault
3269
3554
  };
3270
3555
  }
3556
+ const status = statusOf(context, failure);
3557
+ const skipped = status === "PASS" ? context.caches.keep() : [];
3558
+ if (skipped.length > 0) writeFileSync(join(context.step("module cache not kept"), "note.txt"), `${skipped.map((reason) => `${reason}; a later journey installs its modules again`).join("\n")}\n`);
3271
3559
  const result = {
3272
3560
  feature,
3273
3561
  journey: id,
3274
3562
  title: journey.title,
3275
3563
  entryPoint: journey.entryPoint,
3276
- status: statusOf(context, failure),
3564
+ status,
3277
3565
  steps: context.steps,
3278
3566
  ...failure,
3279
- folder: relative(state.evidence, context.folder)
3567
+ folder: relative(state.evidence, context.folder),
3568
+ stepMs: context.clock.finish(),
3569
+ ms: elapsed()
3280
3570
  };
3281
3571
  writeFileSync(join(context.folder, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
3282
3572
  return result;
@@ -3287,9 +3577,108 @@ function readResults(state, feature) {
3287
3577
  if (!existsSync(file)) return void 0;
3288
3578
  return JSON.parse(readFileSync(file, "utf8"));
3289
3579
  }
3580
+ /** NUL-separated paths git prints, read without the default output limit, since a large worktree can list megabytes. */
3581
+ function gitPaths(root, args) {
3582
+ const listed = spawnSync("git", args, {
3583
+ cwd: root,
3584
+ encoding: "utf8",
3585
+ maxBuffer: 1073741824
3586
+ });
3587
+ if (listed.status !== 0) throw new Error(`git ${args.join(" ")} failed`);
3588
+ return listed.stdout.split("\0").filter(Boolean);
3589
+ }
3590
+ /**
3591
+ * A fingerprint of the tree a journey runs against: HEAD, and the path, type,
3592
+ * size and modification time of every changed tracked file and every
3593
+ * untracked, non-ignored entry. It never reads file contents, so symlinks,
3594
+ * nested repositories and huge files cost one `lstat` each; an edit always
3595
+ * moves the modification time. A path it cannot fingerprint, an untracked
3596
+ * nested repository (whose inner edits leave its folder's time alone) or a
3597
+ * name that is not UTF-8, disables reuse. The run's own folder is left out when it sits
3598
+ * inside the repository, since its evidence changes with every journey.
3599
+ * `undefined` when git cannot answer, which disables reuse instead of failing
3600
+ * the journey.
3601
+ */
3602
+ function treeState(root, runDir) {
3603
+ try {
3604
+ const own = relative(root, runDir);
3605
+ const inside = own !== "" && !own.startsWith("..") && !isAbsolute(own) ? `${own}/` : null;
3606
+ const hash = createHash("sha256");
3607
+ hash.update(git(root, ["rev-parse", "HEAD"]));
3608
+ const paths = [...gitPaths(root, [
3609
+ "diff",
3610
+ "HEAD",
3611
+ "--name-only",
3612
+ "-z",
3613
+ "--no-renames"
3614
+ ]), ...gitPaths(root, [
3615
+ "ls-files",
3616
+ "-z",
3617
+ "--others",
3618
+ "--exclude-standard"
3619
+ ])].filter((path) => inside === null || !`${path}/`.startsWith(inside)).sort();
3620
+ if (paths.some((path) => path.endsWith("/") || path.includes("�"))) return void 0;
3621
+ for (const path of paths) {
3622
+ const stat = lstatSync(join(root, path), {
3623
+ bigint: true,
3624
+ throwIfNoEntry: false
3625
+ });
3626
+ hash.update(`\0${path}\0${stat === void 0 ? "gone" : `${stat.mode}:${stat.size}:${stat.mtimeNs}`}`);
3627
+ }
3628
+ return hash.digest("hex");
3629
+ } catch {
3630
+ return;
3631
+ }
3632
+ }
3633
+ /** The PASS of journey `id` that another feature of this run recorded by running it against the same tree, if any. */
3634
+ function passElsewhere(state, feature, id, tree) {
3635
+ const features = readdirSync(state.evidence, { withFileTypes: true }).filter((entry) => entry.isDirectory() && entry.name !== feature).map((entry) => entry.name).sort();
3636
+ for (const other of features) {
3637
+ const source = readResults(state, other)?.find((result) => result.journey === id && result.status === "PASS" && result.reusedFrom === void 0 && result.tree === tree);
3638
+ if (source !== void 0) return source;
3639
+ }
3640
+ }
3641
+ /** Records the tree a journey ran against in its result.json. */
3642
+ function stamp(state, result, tree) {
3643
+ if (tree === void 0) return result;
3644
+ const stamped = {
3645
+ ...result,
3646
+ tree
3647
+ };
3648
+ writeFileSync(join(state.evidence, result.folder, "result.json"), `${JSON.stringify(stamped, null, 2)}\n`);
3649
+ return stamped;
3650
+ }
3651
+ /** Records `source` as the result of `id` under `feature` without running the journey: its own result.json names the folder it reuses, and its time is zero so a run's total counts the journey once. */
3652
+ function reuse(state, feature, id, source) {
3653
+ const folder = join(state.evidence, feature, id);
3654
+ rmSync(folder, {
3655
+ recursive: true,
3656
+ force: true
3657
+ });
3658
+ mkdirSync(folder, { recursive: true });
3659
+ const result = {
3660
+ ...source,
3661
+ feature,
3662
+ folder: relative(state.evidence, folder),
3663
+ reusedFrom: source.folder,
3664
+ ms: 0,
3665
+ stepMs: []
3666
+ };
3667
+ writeFileSync(join(folder, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
3668
+ return result;
3669
+ }
3290
3670
  /**
3291
3671
  * Runs the named journeys of a feature, or all of them, and merges their
3292
3672
  * results into feature.json so partial runs add up instead of overwriting.
3673
+ *
3674
+ * A journey id is the same code whichever feature lists it. When no ids are
3675
+ * named, a journey that another feature of this run already ran and passed
3676
+ * is not run again: the feature records a result that reuses that PASS and
3677
+ * names its evidence folder. Only a PASS is reused, only for the same tree
3678
+ * (HEAD, uncommitted diff and untracked files) and while HEAD is still the
3679
+ * commit the run was created at, never across runs, and never for a
3680
+ * journey this feature already has a result for. Naming ids runs them even
3681
+ * when a PASS exists.
3293
3682
  */
3294
3683
  async function runFeature(workspace, state, feature, ids) {
3295
3684
  const registered = featureJourneys(workspace, feature);
@@ -3297,18 +3686,72 @@ async function runFeature(workspace, state, feature, ids) {
3297
3686
  const unknown = ids.filter((id) => !registered.includes(id));
3298
3687
  if (unknown.length > 0) throw new Error(`${feature} registers no journey ${unknown.join(", ")}; it registers ${registered.join(", ")}`);
3299
3688
  const known = knownJourneys(workspace);
3689
+ const earlier = readResults(state, feature) ?? [];
3690
+ const tree = treeState(workspace.root, state.dir);
3691
+ const mayReuse = ids.length === 0 && git(workspace.root, ["rev-parse", "HEAD"]) === state.headSha;
3300
3692
  const results = [];
3301
3693
  for (const id of ids.length > 0 ? ids : registered) {
3302
3694
  const journey = known.get(id);
3303
3695
  if (journey === void 0) throw new Error(`${feature} names journey ${id}, which is neither built in nor in verify.config.ts journeys`);
3304
- results.push(await runOne(workspace, state, feature, id, journey));
3696
+ const source = mayReuse && tree !== void 0 && !earlier.some((result) => result.journey === id) ? passElsewhere(state, feature, id, tree) : void 0;
3697
+ results.push(source === void 0 ? stamp(state, await runOne(workspace, state, feature, id, journey), tree) : reuse(state, feature, id, source));
3305
3698
  }
3306
- const earlier = readResults(state, feature) ?? [];
3307
3699
  const merged = registered.flatMap((id) => results.filter((result) => result.journey === id).concat(results.some((result) => result.journey === id) ? [] : earlier.filter((result) => result.journey === id)));
3308
3700
  writeFileSync(join(state.evidence, feature, "feature.json"), `${JSON.stringify(merged, null, 2)}\n`);
3309
3701
  return results;
3310
3702
  }
3311
3703
  //#endregion
3704
+ //#region src/verify/timing-report.ts
3705
+ /** How many of a journey's steps the verdict names. */
3706
+ const SLOWEST_STEPS = 3;
3707
+ /** The label of the verdict's last timing row; it states what the sum leaves out. */
3708
+ const TOTAL_LABEL = "**Total measured** (commands only; agent time between commands is not included)";
3709
+ function sum(values) {
3710
+ return values.every((ms) => ms !== null) ? values.reduce((total, ms) => total + (ms ?? 0), 0) : null;
3711
+ }
3712
+ function slowest(steps) {
3713
+ return steps.toSorted((a, b) => b.ms - a.ms).slice(0, SLOWEST_STEPS);
3714
+ }
3715
+ /** Collects what a run measured: its `new` phases, each journey's total and slowest steps, each formal check. */
3716
+ function timingReport(setup, features, formal) {
3717
+ const journeys = features.flatMap(({ feature, results }) => results.map((result) => ({
3718
+ feature,
3719
+ journey: result.journey,
3720
+ ms: result.ms ?? null,
3721
+ slowestSteps: slowest(result.stepMs ?? [])
3722
+ })));
3723
+ const checks = formal.map((result) => ({
3724
+ name: result.name,
3725
+ ms: result.ms ?? null
3726
+ }));
3727
+ return {
3728
+ setup,
3729
+ journeys,
3730
+ formal: checks,
3731
+ totalMs: setup.length === 0 ? null : sum([
3732
+ ...setup.map((entry) => entry.ms),
3733
+ ...journeys.map((entry) => entry.ms),
3734
+ ...checks.map((entry) => entry.ms)
3735
+ ])
3736
+ };
3737
+ }
3738
+ function journeyCell(entry) {
3739
+ if (entry.ms === null || entry.slowestSteps.length === 0) return seconds(entry.ms);
3740
+ const steps = entry.slowestSteps.map((step) => `\`${step.step}\` ${seconds(step.ms)}`).join(", ");
3741
+ return `${seconds(entry.ms)} (slowest steps: ${steps})`;
3742
+ }
3743
+ /** The `### Timing` table of a verdict: one row per measured phase, journey and formal check, then the sum. */
3744
+ function timingTable(report) {
3745
+ return [
3746
+ "| Phase | Time |",
3747
+ "|---|---|",
3748
+ ...report.setup.length > 0 ? report.setup.map((entry) => `| ${entry.phase} | ${seconds(entry.ms)} |`) : [`| new (setup, pack, services, sources) | ${seconds(null)} |`],
3749
+ ...report.journeys.map((entry) => `| journey ${entry.feature}/${entry.journey} | ${journeyCell(entry)} |`),
3750
+ ...report.formal.map((entry) => `| formal ${entry.name} | ${seconds(entry.ms)} |`),
3751
+ `| ${TOTAL_LABEL} | ${seconds(report.totalMs)} |`
3752
+ ];
3753
+ }
3754
+ //#endregion
3312
3755
  //#region src/verify/verdict.ts
3313
3756
  /** The GitHub `owner/repo` of the origin remote. */
3314
3757
  function originRepository(root) {
@@ -3329,7 +3772,16 @@ function bullets(entries, empty) {
3329
3772
  }
3330
3773
  function journeyRow(label, result, link) {
3331
3774
  const outcome = result.status === "PASS" ? "pass" : result.status === "FAIL" ? "**fail**" : "**not verified**";
3332
- return `| ${label} | \`${result.journey}\`: ${result.entryPoint} | ${outcome}: ${result.title} | [${result.folder}](${link(result.folder)}) |`;
3775
+ const reused = result.reusedFrom === void 0 ? "" : ` (not run again: reuses [${result.reusedFrom}](${link(result.reusedFrom)}))`;
3776
+ return `| ${label} | \`${result.journey}\`: ${result.entryPoint} | ${outcome}: ${result.title}${reused} | [${result.folder}](${link(result.folder)}) |`;
3777
+ }
3778
+ /** For a reused result, the status of its source when that is no longer PASS (`missing` when gone); otherwise null. */
3779
+ function staleReuse(state, result) {
3780
+ if (result.reusedFrom === void 0) return null;
3781
+ const file = join(state.evidence, result.reusedFrom, "result.json");
3782
+ if (!existsSync(file)) return "missing";
3783
+ const source = JSON.parse(readFileSync(file, "utf8"));
3784
+ return source.status === "PASS" ? null : source.status;
3333
3785
  }
3334
3786
  /** The features outside the plan that have journey results in the run's evidence, in name order. */
3335
3787
  function featuresOutsidePlan(state, plan) {
@@ -3340,27 +3792,44 @@ function journeyRows(workspace, state, plan, link) {
3340
3792
  const reasons = [];
3341
3793
  const notVerified = [];
3342
3794
  const outsideNotVerified = [];
3795
+ const listed = [];
3343
3796
  for (const feature of plan.run) {
3344
3797
  const results = readResults(state, feature) ?? [];
3798
+ listed.push({
3799
+ feature,
3800
+ results
3801
+ });
3345
3802
  const registered = featureJourneys(workspace, feature) ?? [];
3346
3803
  for (const id of registered.filter((journey) => !results.some((result) => result.journey === journey))) reasons.push(`${feature}/${id} was not run`);
3347
3804
  for (const result of results) {
3348
3805
  if (!registered.includes(result.journey)) reasons.push(`${feature}/${result.journey} is not a registered journey`);
3349
3806
  if (result.status === "FAIL") reasons.push(`${feature}/${result.journey} failed`);
3350
3807
  if (result.status === "NOT VERIFIED") notVerified.push(`${feature}/${result.journey}: ${result.notVerified}`);
3808
+ const stale = staleReuse(state, result);
3809
+ if (stale !== null) reasons.push(`${feature}/${result.journey} reuses ${result.reusedFrom}, which is now ${stale}`);
3351
3810
  rows.push(journeyRow(feature, result, link));
3352
3811
  }
3353
3812
  }
3354
- for (const feature of featuresOutsidePlan(state, plan)) for (const result of readResults(state, feature) ?? []) {
3355
- if (result.status === "FAIL") reasons.push(`${feature}/${result.journey} failed; ${feature} is outside the diff's features, so check its covers globs`);
3356
- if (result.status === "NOT VERIFIED") outsideNotVerified.push(`${feature}/${result.journey}: ${result.notVerified}`);
3357
- rows.push(journeyRow(`${feature} (outside the diff's features)`, result, link));
3813
+ for (const feature of featuresOutsidePlan(state, plan)) {
3814
+ const results = readResults(state, feature) ?? [];
3815
+ listed.push({
3816
+ feature,
3817
+ results
3818
+ });
3819
+ for (const result of results) {
3820
+ if (result.status === "FAIL") reasons.push(`${feature}/${result.journey} failed; ${feature} is outside the diff's features, so check its covers globs`);
3821
+ const stale = staleReuse(state, result);
3822
+ if (stale !== null) reasons.push(`${feature}/${result.journey} reuses ${result.reusedFrom}, which is now ${stale}`);
3823
+ if (result.status === "NOT VERIFIED") outsideNotVerified.push(`${feature}/${result.journey}: ${result.notVerified}`);
3824
+ rows.push(journeyRow(`${feature} (outside the diff's features)`, result, link));
3825
+ }
3358
3826
  }
3359
3827
  return {
3360
3828
  rows,
3361
3829
  reasons,
3362
3830
  notVerified,
3363
- outsideNotVerified
3831
+ outsideNotVerified,
3832
+ listed
3364
3833
  };
3365
3834
  }
3366
3835
  function doorReasons(workspace, judgement, plan) {
@@ -3385,6 +3854,7 @@ function doorsSection(judgement) {
3385
3854
  function formalSection(workspace, state, plan, link) {
3386
3855
  const reasons = [];
3387
3856
  const lines = [];
3857
+ const ran = [];
3388
3858
  const touched = touchedFormalChecks(workspace, plan.changed).map((check) => check.name);
3389
3859
  for (const check of workspace.config.formal ?? []) {
3390
3860
  const result = readFormalResult(state, check.name);
@@ -3393,6 +3863,7 @@ function formalSection(workspace, state, plan, link) {
3393
3863
  if (gate) reasons.push(`formal check ${check.name} covers the diff but was not run`);
3394
3864
  continue;
3395
3865
  }
3866
+ ran.push(result);
3396
3867
  if (gate && result.behaviour === void 0 && result.exitCode === 0) reasons.push(`formal check ${check.name} covers the diff but its run did not compare the covered behaviour with the base`);
3397
3868
  const weight = formalGate(result);
3398
3869
  if (gate && weight.fails) reasons.push(`formal check ${check.name}: ${weight.outcome}`);
@@ -3401,28 +3872,41 @@ function formalSection(workspace, state, plan, link) {
3401
3872
  }
3402
3873
  return {
3403
3874
  reasons,
3404
- lines
3875
+ lines,
3876
+ ran
3405
3877
  };
3406
3878
  }
3407
3879
  function docsReasons(judgement, plan) {
3408
3880
  return plan.docs.flatMap((page) => {
3409
3881
  const docs = judgement.docs.find((entry) => entry.page === page);
3410
- if (!docs) return [`docs page ${page} of a touched feature has no judgement`];
3882
+ if (!docs) return [`docs page ${page} of a feature the plan runs has no judgement`];
3411
3883
  return docs.matches ? [] : [`${page} no longer matches the behaviour`];
3412
3884
  });
3413
3885
  }
3886
+ function fullLine(full) {
3887
+ return full.reason === "all" ? "Every feature must run: the run was created with `--all`, before a release." : `Every feature must run: the diff changes shared files ${full.files.map((file) => `\`${file}\``).join(", ")}.`;
3888
+ }
3414
3889
  function resultOf(reasons, notVerified, notes) {
3415
3890
  if (reasons.length > 0) return "FAIL";
3416
3891
  if (notVerified.length > 0) return "NOT VERIFIED";
3417
3892
  return notes > 0 ? "PASS+NOTES" : "PASS";
3418
3893
  }
3894
+ /** The verdict's Unmapped surfaces section, empty when every changed surface has a feature. */
3895
+ function unmappedSection(unmapped) {
3896
+ if (unmapped.length === 0) return [];
3897
+ return [
3898
+ "### Unmapped surfaces",
3899
+ ...unmapped.map((file) => `- \`${file}\` matches no feature glob; add it to a feature file.`),
3900
+ ""
3901
+ ];
3902
+ }
3419
3903
  /**
3420
3904
  * Computes the verdict of a run from its journey results, the diff plan and
3421
3905
  * the verifier's judgement, and writes verdict.md, verdict.json and plan.json
3422
3906
  * into the evidence folder.
3423
3907
  */
3424
3908
  function renderVerdict(workspace, state, pr, judgement) {
3425
- const plan = planFor(workspace, state.baseRef);
3909
+ const plan = planFor(workspace, state.baseRef, state.all);
3426
3910
  const repository = originRepository(workspace.root);
3427
3911
  const skillSha = git(workspace.root, [
3428
3912
  "log",
@@ -3443,6 +3927,7 @@ function renderVerdict(workspace, state, pr, judgement) {
3443
3927
  ...judgement.notVerified
3444
3928
  ];
3445
3929
  const result = resultOf(reasons, journeys.notVerified, notVerified.length + judgement.attention.length);
3930
+ const timings = timingReport(state.timings, journeys.listed, formal.ran);
3446
3931
  const touched = plan.touched.map((entry) => `\`${entry.feature}\` (diff: ${entry.files.map((file) => `\`${file}\``).join(", ")})`);
3447
3932
  const markdown = [
3448
3933
  `## Verify: ${result}`,
@@ -3455,12 +3940,9 @@ function renderVerdict(workspace, state, pr, judgement) {
3455
3940
  "### Touched features",
3456
3941
  ...bullets(touched, "none"),
3457
3942
  `- ${SMOKE_FEATURE}: runs on every verdict`,
3943
+ ...plan.full ? [`- ${fullLine(plan.full)}`] : [],
3458
3944
  "",
3459
- ...plan.unmapped.length > 0 ? [
3460
- "### Unmapped surfaces",
3461
- ...plan.unmapped.map((file) => `- \`${file}\` matches no feature glob; add it to a feature file.`),
3462
- ""
3463
- ] : [],
3945
+ ...unmappedSection(plan.unmapped),
3464
3946
  ...reasons.length > 0 ? [
3465
3947
  "### Why it fails",
3466
3948
  ...reasons.map((reason) => `- ${reason}`),
@@ -3475,7 +3957,7 @@ function renderVerdict(workspace, state, pr, judgement) {
3475
3957
  ...bullets(formal.lines, "None configured."),
3476
3958
  "",
3477
3959
  "### Docs",
3478
- ...bullets(judgement.docs.map((docs) => `\`${docs.page}\`: ${docs.matches ? "matches" : "**stale**"}${docs.note ? `. ${docs.note}` : ""}`), "No touched feature links a page."),
3960
+ ...bullets(judgement.docs.map((docs) => `\`${docs.page}\`: ${docs.matches ? "matches" : "**stale**"}${docs.note ? `. ${docs.note}` : ""}`), "The plan lists no docs page to judge."),
3479
3961
  "",
3480
3962
  "### Not verified",
3481
3963
  ...bullets(notVerified, "Nothing."),
@@ -3483,6 +3965,9 @@ function renderVerdict(workspace, state, pr, judgement) {
3483
3965
  "### Attention",
3484
3966
  ...bullets(judgement.attention, "Nothing."),
3485
3967
  "",
3968
+ "### Timing",
3969
+ ...timingTable(timings),
3970
+ "",
3486
3971
  `Evidence: \`verify-evidence\` branch, [\`pr-${pr}/${plan.headSha}/\`](${link("")})`,
3487
3972
  ""
3488
3973
  ].join("\n");
@@ -3493,7 +3978,8 @@ function renderVerdict(workspace, state, pr, judgement) {
3493
3978
  headSha: plan.headSha,
3494
3979
  skillSha,
3495
3980
  pr,
3496
- reasons
3981
+ reasons,
3982
+ timings
3497
3983
  }, null, 2)}\n`);
3498
3984
  return {
3499
3985
  result,
@@ -3510,8 +3996,9 @@ function renderVerdict(workspace, state, pr, judgement) {
3510
3996
  const USAGE = `Usage: antelopejs-verify <command>
3511
3997
 
3512
3998
  map check every user-facing file is covered and every feature is complete
3513
- plan [--base <ref>] map the diff to features; list unmapped surfaces
3514
- new [--base <ref>] [--title <t>] build, prepare an isolated run, print its folder
3999
+ plan [--base <ref>] [--all] map the diff to features; list unmapped surfaces; every feature runs when a shared file changed or with --all
4000
+ new [--base <ref>] [--title <t>] [--all]
4001
+ build, prepare an isolated run, print its folder; --all runs every feature, before a release
3515
4002
  doctor <run> check the run is worth driving
3516
4003
  journey <run> <feature> [ids...] run a feature's journeys and record evidence
3517
4004
  formal <run> [--all] run the formal checks that cover the diff and gate them on changed behaviour
@@ -3540,7 +4027,7 @@ function printMap(workspace) {
3540
4027
  async function runJourneys(workspace, dir, feature, ids) {
3541
4028
  const results = await runFeature(workspace, loadRun(dir), feature, ids);
3542
4029
  for (const result of results) {
3543
- print(`${result.status} ${feature}/${result.journey}`);
4030
+ print(`${result.status} ${feature}/${result.journey}${result.reusedFrom === void 0 ? ` (${seconds(result.ms)})` : ` (reuses ${result.reusedFrom}, not run again)`}`);
3544
4031
  for (const step of result.steps) for (const check of step.checks.filter((entry) => !entry.pass)) print(` ${step.name}: ${check.name} (${check.detail})`);
3545
4032
  if (result.error) print(` ${result.error.split("\n")[0]}`);
3546
4033
  if (result.notVerified) print(` ${result.notVerified}`);
@@ -3612,10 +4099,10 @@ async function main(argv) {
3612
4099
  }
3613
4100
  const root = repositoryRoot();
3614
4101
  const workspace = await openWorkspace(root, detectKind(root, values.kind));
3615
- if (command === "new") print((await createRun(workspace, values.base, values.title)).dir);
4102
+ if (command === "new") print((await createRun(workspace, values.base, values.title, values.all === true)).dir);
3616
4103
  else if (command === "map") return printMap(workspace);
3617
4104
  else if (command === "plan") {
3618
- const plan = planFor(workspace, values.base);
4105
+ const plan = planFor(workspace, values.base, values.all === true);
3619
4106
  print(JSON.stringify(plan, null, 2));
3620
4107
  return plan.unmapped.length === 0 ? 0 : 1;
3621
4108
  } else if (command === "doctor") {