@antelopejs/tooling-configs 0.3.5 → 0.3.6

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.
@@ -4,7 +4,7 @@ import { a as isString, c as object, f as parseJson, i as isObject, n as childre
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 { 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";
7
+ import { _ as withLocalBins, 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-CiFEGFRz.mjs";
8
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
9
  import { basename, dirname, extname, isAbsolute, join, matchesGlob, posix, relative, resolve, sep } from "node:path";
10
10
  import { execFileSync, spawn, spawnSync } from "node:child_process";
@@ -525,11 +525,27 @@ function passingRules(check, exitCode, output) {
525
525
  if (check.passing) return [...new Set([...output.matchAll(check.passing)].map((match) => match[1]))];
526
526
  return !check.violation && exitCode === 0 ? ["property"] : [];
527
527
  }
528
+ function withIndices(pattern) {
529
+ return pattern.hasIndices ? pattern : new RegExp(pattern.source, `${pattern.flags}d`);
530
+ }
531
+ function counterexampleLengths(check, output) {
532
+ if (!check.violation) return {};
533
+ const seen = /* @__PURE__ */ new Map();
534
+ for (const match of output.matchAll(withIndices(check.violation))) {
535
+ const states = match.groups?.states;
536
+ const rule = match[1];
537
+ const statesIsRule = match.indices?.[1]?.[0] === match.indices?.groups?.states?.[0];
538
+ const length = states === void 0 || !/^\d+$/.test(states) || statesIsRule ? null : Number(states);
539
+ seen.set(rule, seen.has(rule) && seen.get(rule) !== length ? null : length);
540
+ }
541
+ return Object.fromEntries([...seen].filter((entry) => entry[1] !== null && Number.isSafeInteger(entry[1])));
542
+ }
528
543
  function rulesOf(check, run) {
529
544
  return {
530
545
  exitCode: run.status,
531
546
  violated: violatedRules(check, run.status, run.stdout ?? ""),
532
- passing: passingRules(check, run.status, run.stdout ?? "")
547
+ passing: passingRules(check, run.status, run.stdout ?? ""),
548
+ lengths: counterexampleLengths(check, run.stdout ?? "")
533
549
  };
534
550
  }
535
551
  function runCommand(command, cwd) {
@@ -575,7 +591,8 @@ function gateFields(workspace, check, diff, folder, head) {
575
591
  }
576
592
  /**
577
593
  * Runs one formal check from the repository root and writes its command,
578
- * output, exit code and violated rules under `evidence/formal/<name>/`.
594
+ * output, exit code, violated rules and their counterexample lengths under
595
+ * `evidence/formal/<name>/`.
579
596
  * When the check covers the diff (`diff` given), it also records whether the
580
597
  * diff changed the covered code's behaviour. When it did, or when the diff
581
598
  * changed the check's own `files`, it runs the check on the base: `fixed`
@@ -594,6 +611,7 @@ function runFormalCheck(workspace, state, check, diff) {
594
611
  exitCode: run.status,
595
612
  violated: head.violated,
596
613
  passing: head.passing,
614
+ lengths: head.lengths,
597
615
  folder: relative(state.evidence, folder)
598
616
  };
599
617
  if (diff !== null) Object.assign(result, { commits: {
@@ -604,13 +622,31 @@ function runFormalCheck(workspace, state, check, diff) {
604
622
  writeFileSync(join(folder, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
605
623
  return result;
606
624
  }
625
+ function lengthOf(lengths, rule) {
626
+ return lengths !== void 0 && Object.hasOwn(lengths, rule) ? lengths[rule] : void 0;
627
+ }
628
+ function knownFindingsOfBase(result) {
629
+ const { base } = result;
630
+ if (!base || base.exitCode !== 0) return false;
631
+ const atHead = new Set(result.violated);
632
+ const fixed = new Set(result.fixed ?? []);
633
+ return base.violated.every((rule) => atHead.has(rule) || fixed.has(rule)) && result.violated.every((rule) => {
634
+ const length = lengthOf(result.lengths, rule);
635
+ return base.violated.includes(rule) && length !== void 0 && length === lengthOf(base.lengths, rule);
636
+ });
637
+ }
607
638
  /**
608
639
  * How a formal check's result weighs on a verdict whose diff it covers. A
609
640
  * non-zero exit (a tool error, or a check that no longer matches its record)
610
- * fails. Recorded findings fail only when the diff changed the covered code's
611
- * behaviour; otherwise they are reported as known. A rule the base broke and
612
- * HEAD reports passing is reported as fixed; one HEAD does not report at all
613
- * fails.
641
+ * fails, so every violated rule of a result that reaches the rest of the gate
642
+ * is a recorded finding. A rule the base broke that HEAD does not report at
643
+ * all fails. Recorded findings are known when the diff left the covered
644
+ * code's behaviour unchanged. When it changed them, they are known only when
645
+ * the base run exited 0 and broke every one of them, each with the same
646
+ * counterexample length at HEAD as on the base, and every other rule the
647
+ * base broke is fixed at HEAD; a new rule, a changed length, or a length
648
+ * either run did not report fails. A rule the base broke and HEAD reports
649
+ * passing is reported as fixed.
614
650
  */
615
651
  function formalGate(result) {
616
652
  if (result.exitCode !== 0) return {
@@ -635,6 +671,10 @@ function formalGate(result) {
635
671
  fails: false,
636
672
  outcome: `covered behaviour changed (${how}), no violated rule${fixed}`
637
673
  };
674
+ if (knownFindingsOfBase(result)) return {
675
+ fails: false,
676
+ outcome: `covered behaviour changed (${how}), same known findings as base: ${result.violated.join(", ")}${fixed}`
677
+ };
638
678
  return {
639
679
  fails: true,
640
680
  outcome: `covered behaviour changed (${how}), violated ${result.violated.join(", ")}${fixed}`
@@ -1564,6 +1604,263 @@ function minorRule(input) {
1564
1604
  };
1565
1605
  }
1566
1606
  //#endregion
1607
+ //#region src/verify/record.ts
1608
+ const DEFAULT_TIMEOUT_MS = 6e5;
1609
+ /** Quotes an argument the way a shell user would type it. */
1610
+ function shellQuote(value) {
1611
+ return /^[\w@%+=:,./-]+$/.test(value) ? value : `'${value.replace(/'/g, "'\\''")}'`;
1612
+ }
1613
+ function describe(spec) {
1614
+ return [spec.program, ...spec.args].map(shellQuote).join(" ");
1615
+ }
1616
+ function writeCommand(folder, spec) {
1617
+ mkdirSync(folder, { recursive: true });
1618
+ writeFileSync(join(folder, "command.txt"), `cd ${shellQuote(spec.cwd)}\n${describe(spec)}\n`);
1619
+ }
1620
+ const foreground = /* @__PURE__ */ new Set();
1621
+ const releases = /* @__PURE__ */ new Set();
1622
+ const INTERRUPTS = [
1623
+ "SIGINT",
1624
+ "SIGTERM",
1625
+ "SIGHUP",
1626
+ "SIGQUIT"
1627
+ ];
1628
+ let listening = false;
1629
+ /** On the first interrupt, kills the process group of every foreground command, runs every registered release (one that throws is reported and the others still run), and exits with the shell's code for the signal. */
1630
+ function listenForInterrupts() {
1631
+ if (listening) return;
1632
+ listening = true;
1633
+ for (const signal of INTERRUPTS) process.once(signal, () => {
1634
+ for (const child of foreground) if (child.pid !== void 0) killOwnGroup(child.pid);
1635
+ for (const release of releases) try {
1636
+ release();
1637
+ } catch (error) {
1638
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
1639
+ }
1640
+ process.exit(128 + constants$1.signals[signal]);
1641
+ });
1642
+ }
1643
+ /**
1644
+ * Until the returned function is called, an interrupt calls `release` after
1645
+ * killing the process group of every command still running in the
1646
+ * foreground, the processes it started included.
1647
+ */
1648
+ function onInterrupt(release) {
1649
+ listenForInterrupts();
1650
+ releases.add(release);
1651
+ return () => releases.delete(release);
1652
+ }
1653
+ /** Runs a command to completion and writes command.txt, stdout.txt, stderr.txt and exit-code.txt into `folder`. */
1654
+ function runRecorded(folder, spec) {
1655
+ writeCommand(folder, spec);
1656
+ listenForInterrupts();
1657
+ return new Promise((resolve) => {
1658
+ const child = spawn(spec.program, spec.args, {
1659
+ cwd: spec.cwd,
1660
+ env: spec.env,
1661
+ detached: true,
1662
+ stdio: [
1663
+ "ignore",
1664
+ "pipe",
1665
+ "pipe"
1666
+ ]
1667
+ });
1668
+ foreground.add(child);
1669
+ const out = [];
1670
+ const err = [];
1671
+ child.stdout.on("data", (chunk) => out.push(chunk));
1672
+ child.stderr.on("data", (chunk) => err.push(chunk));
1673
+ const timer = setTimeout(() => {
1674
+ if (child.pid !== void 0) killOwnGroup(child.pid);
1675
+ }, spec.timeoutMs ?? DEFAULT_TIMEOUT_MS);
1676
+ const finish = (code, signal, failure = "") => {
1677
+ clearTimeout(timer);
1678
+ foreground.delete(child);
1679
+ const result = {
1680
+ folder,
1681
+ command: describe(spec),
1682
+ exitCode: code,
1683
+ signal,
1684
+ stdout: Buffer.concat(out).toString("utf8"),
1685
+ stderr: `${Buffer.concat(err).toString("utf8")}${failure}`
1686
+ };
1687
+ writeFileSync(join(folder, "stdout.txt"), result.stdout);
1688
+ writeFileSync(join(folder, "stderr.txt"), result.stderr);
1689
+ writeFileSync(join(folder, "exit-code.txt"), `${code ?? `signal ${signal}`}\n`);
1690
+ resolve(result);
1691
+ };
1692
+ child.on("error", (error) => finish(null, null, `${error.message}\n`));
1693
+ child.on("close", (code, signal) => finish(code, signal));
1694
+ });
1695
+ }
1696
+ /** Starts a long-running command in its own process group, writing stdout and stderr straight to files. */
1697
+ function startRecorded(folder, spec) {
1698
+ writeCommand(folder, spec);
1699
+ const stdout = openSync(join(folder, "stdout.txt"), "w");
1700
+ const stderr = openSync(join(folder, "stderr.txt"), "w");
1701
+ const child = spawn(spec.program, spec.args, {
1702
+ cwd: spec.cwd,
1703
+ env: spec.env,
1704
+ detached: true,
1705
+ stdio: [
1706
+ "ignore",
1707
+ stdout,
1708
+ stderr
1709
+ ]
1710
+ });
1711
+ closeSync(stdout);
1712
+ closeSync(stderr);
1713
+ const exited = new Promise((resolve) => {
1714
+ child.on("exit", (exitCode, signal) => {
1715
+ writeFileSync(join(folder, "exit-code.txt"), `${exitCode ?? `signal ${signal}`}\n`);
1716
+ resolve({
1717
+ exitCode,
1718
+ signal
1719
+ });
1720
+ });
1721
+ });
1722
+ if (child.pid === void 0) throw new Error(`Could not start ${describe(spec)}`);
1723
+ writeFileSync(join(folder, "pid.txt"), `${child.pid}\n`);
1724
+ return {
1725
+ folder,
1726
+ child,
1727
+ pid: child.pid,
1728
+ exited
1729
+ };
1730
+ }
1731
+ /** Kills the process group a detached command leads, never a lone PID the system may have reused since the command exited. */
1732
+ function killOwnGroup(pid) {
1733
+ try {
1734
+ process.kill(-pid, "SIGKILL");
1735
+ } catch {
1736
+ return;
1737
+ }
1738
+ }
1739
+ /** Kills the process group of `pid`, or `pid` alone, ignoring a process already gone. */
1740
+ function killGroup(pid) {
1741
+ try {
1742
+ process.kill(-pid, "SIGKILL");
1743
+ } catch {
1744
+ try {
1745
+ process.kill(pid, "SIGKILL");
1746
+ } catch {
1747
+ return;
1748
+ }
1749
+ }
1750
+ }
1751
+ /** Reads what a background command has written so far. */
1752
+ function readOutput(folder) {
1753
+ return {
1754
+ stdout: readFileSync(join(folder, "stdout.txt"), "utf8"),
1755
+ stderr: readFileSync(join(folder, "stderr.txt"), "utf8")
1756
+ };
1757
+ }
1758
+ /** Where `freePort` looks first: below the kernel's ephemeral range, which outgoing connections keep drawing from, and below the 20000-29999 ports antelopejs-lease hands out. */
1759
+ const PORT_RANGE = {
1760
+ low: 1e4,
1761
+ high: 2e4
1762
+ };
1763
+ const PORT_ATTEMPTS = 50;
1764
+ function bindable(port) {
1765
+ return new Promise((resolve) => {
1766
+ const server = createServer();
1767
+ server.once("error", () => resolve(null));
1768
+ server.listen(port, "127.0.0.1", () => {
1769
+ const address = server.address();
1770
+ const bound = address !== null && address instanceof Object ? address.port : null;
1771
+ server.close(() => resolve(bound));
1772
+ });
1773
+ });
1774
+ }
1775
+ /**
1776
+ * A free TCP port on the loopback interface for a process the harness starts
1777
+ * next. It tries random ports below the kernel's ephemeral range first, where
1778
+ * no outgoing connection on a busy host takes it between this check and the
1779
+ * app's own bind, and falls back to a port the kernel picks.
1780
+ */
1781
+ async function freePort() {
1782
+ for (let attempt = 0; attempt < PORT_ATTEMPTS; attempt++) {
1783
+ const bound = await bindable(PORT_RANGE.low + Math.floor(Math.random() * (PORT_RANGE.high - PORT_RANGE.low)));
1784
+ if (bound !== null) return bound;
1785
+ }
1786
+ const picked = await bindable(0);
1787
+ if (picked === null) throw new Error("no free loopback port");
1788
+ return picked;
1789
+ }
1790
+ function reason(error) {
1791
+ return error.cause instanceof Error ? error.cause.message : error.message;
1792
+ }
1793
+ /** Sends one request and writes http.json with the status and body. */
1794
+ async function httpRecorded(folder, url, init = {}) {
1795
+ mkdirSync(folder, { recursive: true });
1796
+ let result;
1797
+ try {
1798
+ const response = await fetch(url, {
1799
+ method: init.method ?? "GET",
1800
+ body: init.body,
1801
+ headers: init.body ? { "content-type": "application/json" } : void 0,
1802
+ signal: AbortSignal.timeout(init.timeoutMs ?? 5e3)
1803
+ });
1804
+ const body = await response.text();
1805
+ let json;
1806
+ try {
1807
+ json = parseJson(body);
1808
+ } catch {
1809
+ json = void 0;
1810
+ }
1811
+ result = {
1812
+ folder,
1813
+ url,
1814
+ status: response.status,
1815
+ body,
1816
+ json,
1817
+ error: null
1818
+ };
1819
+ } catch (error) {
1820
+ result = {
1821
+ folder,
1822
+ url,
1823
+ status: null,
1824
+ body: "",
1825
+ json: void 0,
1826
+ error: error instanceof Error ? reason(error) : String(error)
1827
+ };
1828
+ }
1829
+ writeFileSync(join(folder, "http.json"), `${JSON.stringify({
1830
+ request: {
1831
+ url,
1832
+ ...init
1833
+ },
1834
+ response: result
1835
+ }, null, 2)}\n`);
1836
+ return result;
1837
+ }
1838
+ const TREE_SKIP = /* @__PURE__ */ new Set([
1839
+ "node_modules",
1840
+ ".git",
1841
+ ".pnpm-store"
1842
+ ]);
1843
+ function listTree(root, folder = root) {
1844
+ return readdirSync(folder, { withFileTypes: true }).filter((entry) => !TREE_SKIP.has(entry.name)).sort((left, right) => left.name.localeCompare(right.name)).flatMap((entry) => {
1845
+ const path = join(folder, entry.name);
1846
+ const name = relative(root, path);
1847
+ if (entry.isDirectory()) return [`${name}/`, ...listTree(root, path)];
1848
+ return [`${name} ${statSync(path).size}`];
1849
+ });
1850
+ }
1851
+ /** Writes `tree.txt`: every path under `root` with its size, node_modules and .git left out. */
1852
+ function recordTree(folder, root) {
1853
+ mkdirSync(folder, { recursive: true });
1854
+ let lines;
1855
+ try {
1856
+ lines = listTree(root);
1857
+ } catch {
1858
+ lines = [];
1859
+ }
1860
+ writeFileSync(join(folder, "tree.txt"), `${root}\n${lines.join("\n")}\n`);
1861
+ return lines;
1862
+ }
1863
+ //#endregion
1567
1864
  //#region src/verify/interface.ts
1568
1865
  const CALL_ARGS = ["verify-argument", 7];
1569
1866
  function runtimeMembers(value) {
@@ -1716,32 +2013,99 @@ const everyFunction = {
1716
2013
  context.expect(stopped.folder, [interrupted(stopped)]);
1717
2014
  }
1718
2015
  };
1719
- function publishedVersion(output) {
2016
+ /** Reads the latest version and its sha512 integrity from `npm view <name> version dist.integrity --json`. */
2017
+ function publishedView(output) {
1720
2018
  const text = output.trim();
1721
- return text === "" ? null : text.replace(/^"|"$/g, "");
2019
+ if (text === "") return {
2020
+ version: null,
2021
+ integrity: null
2022
+ };
2023
+ const parsed = parseJson(text);
2024
+ if (isString(parsed)) return {
2025
+ version: parsed,
2026
+ integrity: null
2027
+ };
2028
+ if (!isObject(parsed)) return {
2029
+ version: null,
2030
+ integrity: null
2031
+ };
2032
+ return {
2033
+ version: optStr(parsed, "version") ?? null,
2034
+ integrity: sha512Token(optStr(parsed, "dist.integrity"))
2035
+ };
1722
2036
  }
1723
- async function fetchPublished(context, name, version) {
1724
- const folder = join(CACHE_DIR, "published", name.replace("/", "+"), version);
1725
- mkdirSync(folder, { recursive: true });
1726
- const packed = await context.command(`npm pack ${name}@${version}`, "npm", [
1727
- "pack",
1728
- `${name}@${version}`,
1729
- "--pack-destination",
1730
- folder
1731
- ], { env: {
1732
- npm_config_offline: "false",
1733
- npm_config_prefer_offline: "false"
1734
- } });
1735
- const tarball = readdirSync(folder).find((file) => file.endsWith(".tgz"));
1736
- if (packed.exitCode !== 0 || tarball === void 0) {
1737
- context.expect(packed.folder, [exitIs(packed, 0)], { critical: true });
1738
- return;
2037
+ /** The `sha512-` token of a Subresource Integrity value, which may list several hashes; `null` when it has none, which npm also checks but this cache cannot. */
2038
+ function sha512Token(integrity) {
2039
+ return (integrity ?? "").split(/\s+/).find((token) => token.startsWith("sha512-")) ?? null;
2040
+ }
2041
+ const ONLINE = {
2042
+ npm_config_offline: "false",
2043
+ npm_config_prefer_offline: "false"
2044
+ };
2045
+ function tarballsIn(folder) {
2046
+ if (!existsSync(folder)) return [];
2047
+ return readdirSync(folder).filter((entry) => entry.endsWith(".tgz")).map((entry) => join(folder, entry));
2048
+ }
2049
+ /** The registry's integrity string of a file: `sha512-` and the base64 of its SHA-512. */
2050
+ function integrityOf(file) {
2051
+ return `sha512-${createHash("sha512").update(readFileSync(file)).digest("base64")}`;
2052
+ }
2053
+ /** Packs into a staging folder next to the cache, so the cache only ever receives a complete tarball that matches the published integrity. */
2054
+ async function packPublished(context, published, folder) {
2055
+ const { name, version, integrity } = published;
2056
+ mkdirSync(dirname(folder), { recursive: true });
2057
+ const staging = mkdtempSync(`${folder}.pack-`);
2058
+ const release = onInterrupt(() => rmSync(staging, {
2059
+ recursive: true,
2060
+ force: true
2061
+ }));
2062
+ try {
2063
+ const packed = await context.command(`npm pack ${name}@${version}`, "npm", [
2064
+ "pack",
2065
+ `${name}@${version}`,
2066
+ "--pack-destination",
2067
+ staging
2068
+ ], { env: ONLINE });
2069
+ const [staged] = tarballsIn(staging);
2070
+ if (packed.exitCode !== 0 || staged === void 0) {
2071
+ context.expect(packed.folder, [exitIs(packed, 0)], { critical: true });
2072
+ return;
2073
+ }
2074
+ if (integrity !== null) {
2075
+ const actual = integrityOf(staged);
2076
+ context.expect(context.step(`check packed ${name}@${version}`), [{
2077
+ name: `the packed tarball of ${name}@${version} has the integrity the registry publishes`,
2078
+ pass: actual === integrity,
2079
+ detail: `packed ${actual}, published ${integrity}`
2080
+ }], { critical: true });
2081
+ }
2082
+ if (integrity === null) {
2083
+ const uncached = join(context.work, basename(staged));
2084
+ copyFileSync(staged, uncached);
2085
+ return uncached;
2086
+ }
2087
+ mkdirSync(folder, { recursive: true });
2088
+ const cached = join(folder, basename(staged));
2089
+ renameSync(staged, cached);
2090
+ return cached;
2091
+ } finally {
2092
+ release();
2093
+ rmSync(staging, {
2094
+ recursive: true,
2095
+ force: true
2096
+ });
1739
2097
  }
2098
+ }
2099
+ async function extractPublished(context, tarball) {
1740
2100
  const extracted = join(context.work, "published");
2101
+ rmSync(extracted, {
2102
+ recursive: true,
2103
+ force: true
2104
+ });
1741
2105
  mkdirSync(extracted, { recursive: true });
1742
2106
  const untar = await context.command("extract published", "tar", [
1743
2107
  "-xzf",
1744
- join(folder, tarball),
2108
+ tarball,
1745
2109
  "-C",
1746
2110
  extracted
1747
2111
  ]);
@@ -1750,6 +2114,54 @@ async function fetchPublished(context, name, version) {
1750
2114
  linkDependencies(root, context.repo);
1751
2115
  return root;
1752
2116
  }
2117
+ /**
2118
+ * A private copy, under the journey's work folder, of the cached tarball of a
2119
+ * version, only when it is the folder's single tarball and the copy's
2120
+ * integrity is the published one. Hashing and extracting the same copy keeps
2121
+ * another run that replaces the cached file in between from changing what
2122
+ * this journey reads.
2123
+ */
2124
+ function trustedTarball(files, integrity, work) {
2125
+ const [only] = files;
2126
+ if (files.length !== 1 || integrity === null) return void 0;
2127
+ const copy = join(work, basename(only));
2128
+ try {
2129
+ copyFileSync(only, copy);
2130
+ } catch {
2131
+ return;
2132
+ }
2133
+ return integrityOf(copy) === integrity ? copy : void 0;
2134
+ }
2135
+ /** Extracts a published version from the cache when its one tarball has the published integrity, and packs it again otherwise. */
2136
+ async function fetchPublished(context, name, published) {
2137
+ const { version, integrity } = published;
2138
+ const folder = join(CACHE_DIR, "published", name.replace("/", "+"), version);
2139
+ const files = tarballsIn(folder);
2140
+ mkdirSync(context.work, { recursive: true });
2141
+ const trusted = trustedTarball(files, integrity, context.work);
2142
+ if (trusted !== void 0) {
2143
+ context.expect(context.step(`reuse cached ${name}@${version}`), [{
2144
+ name: `the cached tarball of ${name}@${version} has the published integrity, so it is reused and no npm pack ran`,
2145
+ pass: true,
2146
+ detail: `${files[0]}: ${integrity}`
2147
+ }]);
2148
+ return extractPublished(context, trusted);
2149
+ }
2150
+ if (files.length > 0) {
2151
+ for (const file of files) rmSync(file, { force: true });
2152
+ context.expect(context.step(`discard cached ${name}@${version}`), [{
2153
+ name: `the cached tarballs of ${name}@${version} are deleted and packed again, as ${files.length === 1 ? "the one" : `none of the ${files.length}`} cannot be trusted`,
2154
+ pass: tarballsIn(folder).length === 0,
2155
+ detail: `${files.join(", ")} against ${integrity ?? "no published integrity"}`
2156
+ }]);
2157
+ }
2158
+ const packed = await packPublished(context, {
2159
+ name,
2160
+ version,
2161
+ integrity
2162
+ }, folder);
2163
+ return packed === void 0 ? void 0 : extractPublished(context, packed);
2164
+ }
1753
2165
  function changeTitles(context) {
1754
2166
  if (context.state.title !== "") return [context.state.title];
1755
2167
  const base = git(context.repo, [
@@ -1776,16 +2188,20 @@ const INTERFACE_JOURNEYS = {
1776
2188
  "view",
1777
2189
  name,
1778
2190
  "version",
2191
+ "dist.integrity",
1779
2192
  "--json"
1780
- ], { env: {
1781
- npm_config_offline: "false",
1782
- npm_config_prefer_offline: "false"
1783
- } });
2193
+ ], { env: ONLINE });
1784
2194
  const missing = view.exitCode !== 0 && /E404/.test(view.stderr);
1785
2195
  if (view.exitCode !== 0 && !missing) context.notVerified(`npm view ${name} failed: ${view.stderr.trim().slice(-200)}`);
1786
- const published = missing ? null : publishedVersion(view.stdout);
2196
+ const { version: published, integrity } = missing ? {
2197
+ version: null,
2198
+ integrity: null
2199
+ } : publishedView(view.stdout);
1787
2200
  const head = readSurface(context, context.state.pack, "head");
1788
- const root = published === null ? void 0 : await fetchPublished(context, name, published);
2201
+ const root = published === null ? void 0 : await fetchPublished(context, name, {
2202
+ version: published,
2203
+ integrity
2204
+ });
1789
2205
  const diff = diffSurface((root === void 0 ? void 0 : readSurface(context, root, `published ${published}`)) ?? /* @__PURE__ */ new Map(), head ?? /* @__PURE__ */ new Map());
1790
2206
  const titles = changeTitles(context);
1791
2207
  const verdict = minorRule({
@@ -1811,243 +2227,6 @@ const INTERFACE_JOURNEYS = {
1811
2227
  }
1812
2228
  };
1813
2229
  //#endregion
1814
- //#region src/verify/record.ts
1815
- const DEFAULT_TIMEOUT_MS = 6e5;
1816
- /** Quotes an argument the way a shell user would type it. */
1817
- function shellQuote(value) {
1818
- return /^[\w@%+=:,./-]+$/.test(value) ? value : `'${value.replace(/'/g, "'\\''")}'`;
1819
- }
1820
- function describe(spec) {
1821
- return [spec.program, ...spec.args].map(shellQuote).join(" ");
1822
- }
1823
- function writeCommand(folder, spec) {
1824
- mkdirSync(folder, { recursive: true });
1825
- writeFileSync(join(folder, "command.txt"), `cd ${shellQuote(spec.cwd)}\n${describe(spec)}\n`);
1826
- }
1827
- const foreground = /* @__PURE__ */ new Set();
1828
- const releases = /* @__PURE__ */ new Set();
1829
- const INTERRUPTS = [
1830
- "SIGINT",
1831
- "SIGTERM",
1832
- "SIGHUP",
1833
- "SIGQUIT"
1834
- ];
1835
- let listening = false;
1836
- /** On the first interrupt, kills the process group of every foreground command, runs every registered release (one that throws is reported and the others still run), and exits with the shell's code for the signal. */
1837
- function listenForInterrupts() {
1838
- if (listening) return;
1839
- listening = true;
1840
- for (const signal of INTERRUPTS) process.once(signal, () => {
1841
- for (const child of foreground) if (child.pid !== void 0) killOwnGroup(child.pid);
1842
- for (const release of releases) try {
1843
- release();
1844
- } catch (error) {
1845
- process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
1846
- }
1847
- process.exit(128 + constants$1.signals[signal]);
1848
- });
1849
- }
1850
- /**
1851
- * Until the returned function is called, an interrupt calls `release` after
1852
- * killing the process group of every command still running in the
1853
- * foreground, the processes it started included.
1854
- */
1855
- function onInterrupt(release) {
1856
- listenForInterrupts();
1857
- releases.add(release);
1858
- return () => releases.delete(release);
1859
- }
1860
- /** Runs a command to completion and writes command.txt, stdout.txt, stderr.txt and exit-code.txt into `folder`. */
1861
- function runRecorded(folder, spec) {
1862
- writeCommand(folder, spec);
1863
- listenForInterrupts();
1864
- return new Promise((resolve) => {
1865
- const child = spawn(spec.program, spec.args, {
1866
- cwd: spec.cwd,
1867
- env: spec.env,
1868
- detached: true,
1869
- stdio: [
1870
- "ignore",
1871
- "pipe",
1872
- "pipe"
1873
- ]
1874
- });
1875
- foreground.add(child);
1876
- const out = [];
1877
- const err = [];
1878
- child.stdout.on("data", (chunk) => out.push(chunk));
1879
- child.stderr.on("data", (chunk) => err.push(chunk));
1880
- const timer = setTimeout(() => {
1881
- if (child.pid !== void 0) killOwnGroup(child.pid);
1882
- }, spec.timeoutMs ?? DEFAULT_TIMEOUT_MS);
1883
- const finish = (code, signal, failure = "") => {
1884
- clearTimeout(timer);
1885
- foreground.delete(child);
1886
- const result = {
1887
- folder,
1888
- command: describe(spec),
1889
- exitCode: code,
1890
- signal,
1891
- stdout: Buffer.concat(out).toString("utf8"),
1892
- stderr: `${Buffer.concat(err).toString("utf8")}${failure}`
1893
- };
1894
- writeFileSync(join(folder, "stdout.txt"), result.stdout);
1895
- writeFileSync(join(folder, "stderr.txt"), result.stderr);
1896
- writeFileSync(join(folder, "exit-code.txt"), `${code ?? `signal ${signal}`}\n`);
1897
- resolve(result);
1898
- };
1899
- child.on("error", (error) => finish(null, null, `${error.message}\n`));
1900
- child.on("close", (code, signal) => finish(code, signal));
1901
- });
1902
- }
1903
- /** Starts a long-running command in its own process group, writing stdout and stderr straight to files. */
1904
- function startRecorded(folder, spec) {
1905
- writeCommand(folder, spec);
1906
- const stdout = openSync(join(folder, "stdout.txt"), "w");
1907
- const stderr = openSync(join(folder, "stderr.txt"), "w");
1908
- const child = spawn(spec.program, spec.args, {
1909
- cwd: spec.cwd,
1910
- env: spec.env,
1911
- detached: true,
1912
- stdio: [
1913
- "ignore",
1914
- stdout,
1915
- stderr
1916
- ]
1917
- });
1918
- closeSync(stdout);
1919
- closeSync(stderr);
1920
- const exited = new Promise((resolve) => {
1921
- child.on("exit", (exitCode, signal) => {
1922
- writeFileSync(join(folder, "exit-code.txt"), `${exitCode ?? `signal ${signal}`}\n`);
1923
- resolve({
1924
- exitCode,
1925
- signal
1926
- });
1927
- });
1928
- });
1929
- if (child.pid === void 0) throw new Error(`Could not start ${describe(spec)}`);
1930
- writeFileSync(join(folder, "pid.txt"), `${child.pid}\n`);
1931
- return {
1932
- folder,
1933
- child,
1934
- pid: child.pid,
1935
- exited
1936
- };
1937
- }
1938
- /** Kills the process group a detached command leads, never a lone PID the system may have reused since the command exited. */
1939
- function killOwnGroup(pid) {
1940
- try {
1941
- process.kill(-pid, "SIGKILL");
1942
- } catch {
1943
- return;
1944
- }
1945
- }
1946
- /** Kills the process group of `pid`, or `pid` alone, ignoring a process already gone. */
1947
- function killGroup(pid) {
1948
- try {
1949
- process.kill(-pid, "SIGKILL");
1950
- } catch {
1951
- try {
1952
- process.kill(pid, "SIGKILL");
1953
- } catch {
1954
- return;
1955
- }
1956
- }
1957
- }
1958
- /** Reads what a background command has written so far. */
1959
- function readOutput(folder) {
1960
- return {
1961
- stdout: readFileSync(join(folder, "stdout.txt"), "utf8"),
1962
- stderr: readFileSync(join(folder, "stderr.txt"), "utf8")
1963
- };
1964
- }
1965
- /** Asks the kernel for a free TCP port on the loopback interface. */
1966
- function freePort() {
1967
- return new Promise((resolve, reject) => {
1968
- const server = createServer();
1969
- server.once("error", reject);
1970
- server.listen(0, "127.0.0.1", () => {
1971
- const address = server.address();
1972
- const port = address !== null && address instanceof Object ? address.port : 0;
1973
- server.close(() => resolve(port));
1974
- });
1975
- });
1976
- }
1977
- function reason(error) {
1978
- return error.cause instanceof Error ? error.cause.message : error.message;
1979
- }
1980
- /** Sends one request and writes http.json with the status and body. */
1981
- async function httpRecorded(folder, url, init = {}) {
1982
- mkdirSync(folder, { recursive: true });
1983
- let result;
1984
- try {
1985
- const response = await fetch(url, {
1986
- method: init.method ?? "GET",
1987
- body: init.body,
1988
- headers: init.body ? { "content-type": "application/json" } : void 0,
1989
- signal: AbortSignal.timeout(init.timeoutMs ?? 5e3)
1990
- });
1991
- const body = await response.text();
1992
- let json;
1993
- try {
1994
- json = parseJson(body);
1995
- } catch {
1996
- json = void 0;
1997
- }
1998
- result = {
1999
- folder,
2000
- url,
2001
- status: response.status,
2002
- body,
2003
- json,
2004
- error: null
2005
- };
2006
- } catch (error) {
2007
- result = {
2008
- folder,
2009
- url,
2010
- status: null,
2011
- body: "",
2012
- json: void 0,
2013
- error: error instanceof Error ? reason(error) : String(error)
2014
- };
2015
- }
2016
- writeFileSync(join(folder, "http.json"), `${JSON.stringify({
2017
- request: {
2018
- url,
2019
- ...init
2020
- },
2021
- response: result
2022
- }, null, 2)}\n`);
2023
- return result;
2024
- }
2025
- const TREE_SKIP = /* @__PURE__ */ new Set([
2026
- "node_modules",
2027
- ".git",
2028
- ".pnpm-store"
2029
- ]);
2030
- function listTree(root, folder = root) {
2031
- return readdirSync(folder, { withFileTypes: true }).filter((entry) => !TREE_SKIP.has(entry.name)).sort((left, right) => left.name.localeCompare(right.name)).flatMap((entry) => {
2032
- const path = join(folder, entry.name);
2033
- const name = relative(root, path);
2034
- if (entry.isDirectory()) return [`${name}/`, ...listTree(root, path)];
2035
- return [`${name} ${statSync(path).size}`];
2036
- });
2037
- }
2038
- /** Writes `tree.txt`: every path under `root` with its size, node_modules and .git left out. */
2039
- function recordTree(folder, root) {
2040
- mkdirSync(folder, { recursive: true });
2041
- let lines;
2042
- try {
2043
- lines = listTree(root);
2044
- } catch {
2045
- lines = [];
2046
- }
2047
- writeFileSync(join(folder, "tree.txt"), `${root}\n${lines.join("\n")}\n`);
2048
- return lines;
2049
- }
2050
- //#endregion
2051
2230
  //#region src/verify/sources.ts
2052
2231
  const IDENTITY = [
2053
2232
  "-c",
@@ -3092,7 +3271,12 @@ var JourneyAbort = class extends Error {};
3092
3271
  var NotVerified = class extends Error {};
3093
3272
  const READY_TIMEOUT_MS = 12e4;
3094
3273
  const STOP_TIMEOUT_MS = 3e4;
3274
+ /** The longest wait between readiness probes. */
3095
3275
  const POLL_MS = 250;
3276
+ /** The wait between readiness probes in the first `FAST_POLL_WINDOW_MS`, where most apps come up: an app ready in 300 ms is seen within 25 ms of it instead of at the 500 ms tick. */
3277
+ const FAST_POLL_MS = 25;
3278
+ /** How long readiness is probed every `FAST_POLL_MS` before falling back to `POLL_MS`. */
3279
+ const FAST_POLL_WINDOW_MS = 3e3;
3096
3280
  const SETTLE_MS = 3e3;
3097
3281
  /** The waits between the readiness reruns after a failed journey: one miss under load does not hide a catch. */
3098
3282
  const READINESS_RETRY_MS = [
@@ -3317,9 +3501,10 @@ var VerifyContext = class {
3317
3501
  handle.exited.then(() => {
3318
3502
  exited = true;
3319
3503
  });
3504
+ const fastUntil = Date.now() + FAST_POLL_WINDOW_MS;
3320
3505
  let check = await this.probe(app, fixture, folder);
3321
3506
  while (!check.pass && !exited && Date.now() < deadline) {
3322
- await setTimeout$1(POLL_MS);
3507
+ await setTimeout$1(Date.now() < fastUntil ? FAST_POLL_MS : POLL_MS);
3323
3508
  check = await this.probe(app, fixture, folder);
3324
3509
  }
3325
3510
  const settle = this.workspace.config.template?.probe.log ? this.workspace.config.template.probe.settleMs ?? SETTLE_MS : 0;
@@ -4001,6 +4186,8 @@ const USAGE = `Usage: antelopejs-verify <command>
4001
4186
  build, prepare an isolated run, print its folder; --all runs every feature, before a release
4002
4187
  doctor <run> check the run is worth driving
4003
4188
  journey <run> <feature> [ids...] run a feature's journeys and record evidence
4189
+ journey <run> --plan run every feature the run's plan lists, smoke first
4190
+ journey <run> --features <a,b> run the named features, each whole, in that order
4004
4191
  formal <run> [--all] run the formal checks that cover the diff and gate them on changed behaviour
4005
4192
  doors [patch] [--base <ref>] print the One-way-door line for a patch, or for the diff
4006
4193
  clean <run> stop what the run started, keep only the evidence
@@ -4024,15 +4211,98 @@ function printMap(workspace) {
4024
4211
  for (const problem of problems) print(problem);
4025
4212
  return problems.length === 0 ? 0 : 1;
4026
4213
  }
4027
- async function runJourneys(workspace, dir, feature, ids) {
4028
- const results = await runFeature(workspace, loadRun(dir), feature, ids);
4214
+ /**
4215
+ * The features a `journey` call runs, in order.
4216
+ *
4217
+ * `journey <run> <feature> [ids...]` runs one feature, or the named journeys
4218
+ * of it. `journey <run> --plan` runs every feature `plan` lists as `run` for
4219
+ * the run's base and `--all`, `smoke` first. `journey <run> --features a,b`
4220
+ * runs the named features whole, in that order. Several features take a flag
4221
+ * rather than more positionals because a positional after the feature is a
4222
+ * journey id: reading it as a feature whenever a feature file has that name
4223
+ * would change what an existing call runs as soon as someone adds a feature
4224
+ * named like one of its journeys.
4225
+ */
4226
+ function featureRuns(workspace, rest, flags) {
4227
+ const [dir, feature, ...ids] = rest;
4228
+ if (flags.plan !== true && flags.features === void 0) return [{
4229
+ feature,
4230
+ ids
4231
+ }];
4232
+ if (flags.plan === true && flags.features !== void 0) throw new Error("journey takes --plan or --features, not both");
4233
+ if (feature !== void 0) throw new Error("--plan and --features run whole features; name journey ids after one feature: journey <run> <feature> [ids...]");
4234
+ if (flags.plan === true) {
4235
+ const state = loadRun(dir);
4236
+ return planFor(workspace, state.baseRef, state.all).run.map((name) => ({
4237
+ feature: name,
4238
+ ids: []
4239
+ }));
4240
+ }
4241
+ const names = (flags.features ?? []).flatMap((list) => list.split(",")).map((name) => name.trim()).filter(Boolean);
4242
+ if (names.length === 0) throw new Error("--features names no feature");
4243
+ return names.map((name) => ({
4244
+ feature: name,
4245
+ ids: []
4246
+ }));
4247
+ }
4248
+ /**
4249
+ * Runs one feature's journeys as its own `journey` call would, and prints its
4250
+ * lines once it ends. A feature that cannot run, an unknown feature or
4251
+ * journey for one, prints its error on stderr instead.
4252
+ * @returns whether every journey it ran passed.
4253
+ */
4254
+ async function journeyFeature(workspace, dir, { feature, ids }) {
4255
+ let results;
4256
+ try {
4257
+ results = await runFeature(workspace, loadRun(dir), feature, ids);
4258
+ } catch (error) {
4259
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
4260
+ return false;
4261
+ }
4029
4262
  for (const result of results) {
4030
4263
  print(`${result.status} ${feature}/${result.journey}${result.reusedFrom === void 0 ? ` (${seconds(result.ms)})` : ` (reuses ${result.reusedFrom}, not run again)`}`);
4031
4264
  for (const step of result.steps) for (const check of step.checks.filter((entry) => !entry.pass)) print(` ${step.name}: ${check.name} (${check.detail})`);
4032
4265
  if (result.error) print(` ${result.error.split("\n")[0]}`);
4033
4266
  if (result.notVerified) print(` ${result.notVerified}`);
4034
4267
  }
4035
- return results.every((result) => result.status === "PASS") ? 0 : 1;
4268
+ return results.every((result) => result.status === "PASS");
4269
+ }
4270
+ /**
4271
+ * Runs the features in order, each one as if by its own call: a feature
4272
+ * whose journeys fail does not stop the next, a feature reuses the PASS of a
4273
+ * journey an earlier one ran, and `process.env` is restored after each
4274
+ * feature. An uncaught error a journey leaves behind (a timer that throws, a
4275
+ * rejection nobody awaits) ends the call, as it would end a single call, and
4276
+ * names the features that did not run. Module-level state a repository's
4277
+ * verify.config.ts keeps is shared by the features of one call. This loop is
4278
+ * the one place a capped number of features would run at once.
4279
+ * @returns 1 when any feature had a journey that did not pass or could not run, else 0.
4280
+ */
4281
+ async function runJourneys(workspace, dir, runs) {
4282
+ let current = 0;
4283
+ const abort = (message) => {
4284
+ const left = runs.slice(current + 1).map((run) => run.feature);
4285
+ process.stderr.write(`${runs[current]?.feature ?? "journey"}: a journey left an uncaught error: ${message}\n${left.length > 0 ? `not run: ${left.join(", ")}\n` : ""}`);
4286
+ process.exit(1);
4287
+ };
4288
+ const onError = (error) => abort(error.message);
4289
+ const onRejection = (reason) => abort(reason instanceof Error ? reason.message : String(reason));
4290
+ process.on("uncaughtException", onError);
4291
+ process.on("unhandledRejection", onRejection);
4292
+ const env = { ...process.env };
4293
+ let passed = true;
4294
+ for (; current < runs.length; current++) {
4295
+ passed = await journeyFeature(workspace, dir, runs[current]) && passed;
4296
+ restoreEnv(env);
4297
+ }
4298
+ process.off("uncaughtException", onError);
4299
+ process.off("unhandledRejection", onRejection);
4300
+ return passed ? 0 : 1;
4301
+ }
4302
+ /** Puts `process.env` back to `saved`, so a journey that set a variable leaves the next feature the environment its own call would have. */
4303
+ function restoreEnv(saved) {
4304
+ for (const key of Object.keys(process.env)) if (!Object.hasOwn(saved, key)) delete process.env[key];
4305
+ Object.assign(process.env, saved);
4036
4306
  }
4037
4307
  function runFormal(workspace, dir, all) {
4038
4308
  const state = loadRun(dir);
@@ -4089,7 +4359,12 @@ async function main(argv) {
4089
4359
  },
4090
4360
  pr: { type: "string" },
4091
4361
  all: { type: "boolean" },
4092
- kind: { type: "string" }
4362
+ kind: { type: "string" },
4363
+ plan: { type: "boolean" },
4364
+ features: {
4365
+ type: "string",
4366
+ multiple: true
4367
+ }
4093
4368
  }
4094
4369
  });
4095
4370
  const [command, ...rest] = positionals;
@@ -4098,6 +4373,8 @@ async function main(argv) {
4098
4373
  return 0;
4099
4374
  }
4100
4375
  const root = repositoryRoot();
4376
+ const path = withLocalBins(root, process.env.PATH);
4377
+ if (path !== void 0) process.env.PATH = path;
4101
4378
  const workspace = await openWorkspace(root, detectKind(root, values.kind));
4102
4379
  if (command === "new") print((await createRun(workspace, values.base, values.title, values.all === true)).dir);
4103
4380
  else if (command === "map") return printMap(workspace);
@@ -4109,7 +4386,7 @@ async function main(argv) {
4109
4386
  const lines = doctor(workspace, loadRun(rest[0]));
4110
4387
  for (const line of lines) print(`${line.ok ? "ok " : "FAIL"} ${line.check}: ${line.detail}`);
4111
4388
  return lines.every((line) => line.ok) ? 0 : 1;
4112
- } else if (command === "journey") return runJourneys(workspace, rest[0], rest[1], rest.slice(2));
4389
+ } else if (command === "journey") return runJourneys(workspace, rest[0], featureRuns(workspace, rest, values));
4113
4390
  else if (command === "formal") return runFormal(workspace, rest[0], values.all === true);
4114
4391
  else if (command === "doors") return printDoors(workspace, rest[0] ? patchFiles(rest[0]) : planFor(workspace, values.base).changed);
4115
4392
  else if (command === "clean") for (const line of clean(loadRun(rest[0]))) print(line);