@decocms/parity 0.35.34 → 0.35.36

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.
package/AGENTS.md CHANGED
@@ -25,6 +25,7 @@ Do **not** run parity for:
25
25
  | "is everything still working?" | `parity run --preset smoke` then `--preset full` if smoke is clean |
26
26
  | "compare these two URLs in CI" | `parity journey --prod ... --cand ... --junit ... --github` |
27
27
  | "find sections missing in the migration" | `parity run --visual-pages 5` then read `report.json` `.visualDiff` |
28
+ | "did my change just break a page that was fine?" (before each commit) | `parity gate --cand ... --dir <target>/.parity --only <pages>` (baseline once with `--update-baseline`; exit 1 = do not commit, 2 = could not judge; see `docs/cli.md`) |
28
29
  | "did web vitals regress?" | `parity vitals --prod ... --cand ... --limit 20` |
29
30
  | "is the new site missing cache?" | `parity cache --cand ... --limit 30` |
30
31
  | "I want an LLM prompt with all the issues" | `parity prompt <runId>` (writes Markdown to stdout) |
package/CHANGELOG.md CHANGED
@@ -9,6 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
9
9
 
10
10
  ### Added
11
11
 
12
+ * **`parity gate`: a fast pre-commit regression gate.** Loads each page x viewport once (one browser,
13
+ sequential, primed like `run`) and compares status, server HTML size, new section-crash / hydration /
14
+ uncaught errors, page height after the scroll, interactive element count, server-rendered
15
+ first-viewport sections and click-blocking overlays against `<dir>/gate-baseline.json`, a baseline of
16
+ the candidate itself. Exit 0 no regression with at least one page compared, 1 regression, 2 could not
17
+ judge (no baseline, cold or degraded candidate, nothing compared). `--update-baseline` accepts the
18
+ current state and refuses when a page is unjudgeable. `capturePage` gains an additive `lean` option
19
+ (skips vitals) and `PageCapture.gateProbe`; `fixer` and `porter` run the gate before every commit.
12
20
  * **`parity run` notices a local candidate that degrades mid-run.** Priming records each URL's
13
21
  status, document bytes and time; after the passes the same URLs are re-requested once, in order,
14
22
  and compared (tolerances in `TOLERANCES.candHealth`). When the server is found degraded, a
@@ -22,6 +30,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
22
30
  `favicon-parity`, `plp-sorting` and `plp-pagination` wait 60s on a local URL instead of aborting
23
31
  after 8-15s, since an aborted request is what wedges a dev server's loader de-duplication.
24
32
 
33
+ * **`parity capacity`** answers "may one more heavy agent start on this machine now, and how many
34
+ more?" so the orchestrator admits parallel fixers/porters by what the machine can carry instead
35
+ of a fixed 5. Measures cores, 1-minute load, available memory (macOS `vm_stat`, because
36
+ `os.freemem()` reads 2.0 GB where 5.3 GB is available; Linux `MemAvailable`, cgroup-capped),
37
+ swap and free disk, then `slots = min(max, memory, cpu, swap, disk)`. An unreadable input caps at
38
+ 1 and is named, never read as headroom. Read-only; exit 0 admit, 1 no headroom, 2 could not
39
+ measure enough. The orchestrator checks it before each additional agent and removes a fixer's
40
+ worktree once its PR is merged or closed (disk was the binding constraint).
41
+
25
42
  * **`first-viewport-ssr` check (html module)** answers "is what the user sees in the first viewport
26
43
  already in the server response, or does it arrive later?". A deferred header (a skeleton in the
27
44
  server HTML that client JS fills in) passed `ssr-no-js` (the rest of the page carried the text)
package/dist/cli.js CHANGED
@@ -75779,7 +75779,7 @@ import {
75779
75779
  registerCommands as registerCatalogCommands
75780
75780
  } from "@decocms/catalog";
75781
75781
  import { DESCRIPTION, registerCommands } from "@decocms/vtex-cms";
75782
- import chalk49 from "chalk";
75782
+ import chalk51 from "chalk";
75783
75783
  import { Command, InvalidArgumentError as InvalidArgumentError2 } from "commander";
75784
75784
 
75785
75785
  // src/commands/ab-detect.ts
@@ -83921,11 +83921,11 @@ ${compacted}
83921
83921
  }
83922
83922
 
83923
83923
  // src/engine/click-probe.ts
83924
+ var INTERACTIVE_SELECTOR = "a[href],button,input:not([type=hidden]),select,textarea,summary,[role=button],[role=link],[role=menuitem],[role=tab],[onclick]";
83924
83925
  var CLICK_PROBE_SOURCE = `(function (cols, rows, minPaintAlpha) {
83925
83926
  var vw = window.innerWidth;
83926
83927
  var vh = window.innerHeight;
83927
- var INTERACTIVE =
83928
- 'a[href],button,input:not([type=hidden]),select,textarea,summary,[role=button],[role=link],[role=menuitem],[role=tab],[onclick]';
83928
+ var INTERACTIVE = '${INTERACTIVE_SELECTOR}';
83929
83929
 
83930
83930
  function describe(el) {
83931
83931
  var s = el.tagName.toLowerCase();
@@ -84811,20 +84811,23 @@ async function reloadIfOptionalFonts(page, waitMs = 0) {
84811
84811
  function formatSettleCaveat(side, settle) {
84812
84812
  return settle.stable ? null : `${side}: layout still changing after ${settle.waitedMs}ms (height ${settle.heightPx}px) — measured anyway, so a large rect delta may be a timing artifact`;
84813
84813
  }
84814
- async function scrollFullPage(page, budgetMs = 45000) {
84814
+ async function scrollFullPage(page, budgetMs = 45000, quick = false) {
84815
84815
  const start = Date.now();
84816
- const result = await page.evaluate(async (budget) => {
84816
+ const W = quick ? { step: 250, stableN: 2, stable: 300, bottom: 300, top: 100 } : { step: 400, stableN: 3, stable: 500, bottom: 1500, top: 700 };
84817
+ const result = await page.evaluate(async (a) => {
84818
+ const budget = a.budget;
84819
+ const w = a.w;
84817
84820
  const SK = "[aria-busy='true'],[data-skeleton],[data-loading='true'],.skeleton,[class*='skeleton' i],[class*='Skeleton'],[class*='shimmer' i],.animate-pulse,.placeholder-shimmer,.react-loading-skeleton";
84818
84821
  const innerStart = Date.now();
84819
84822
  let y = 0;
84820
84823
  let prevHeight = 0;
84821
84824
  let stableTicks = 0;
84822
84825
  let steps = 0;
84823
- const STABLE_THRESHOLD = 3;
84826
+ const STABLE_THRESHOLD = w.stableN;
84824
84827
  while (budget - (Date.now() - innerStart) > 2000) {
84825
84828
  window.scrollTo(0, y);
84826
84829
  steps++;
84827
- await new Promise((r) => setTimeout(r, 400));
84830
+ await new Promise((r) => setTimeout(r, w.step));
84828
84831
  const stepWaitStart = Date.now();
84829
84832
  while (document.querySelectorAll(SK).length > 0 && Date.now() - stepWaitStart < 1500 && budget - (Date.now() - innerStart) > 1000) {
84830
84833
  await new Promise((r) => setTimeout(r, 200));
@@ -84840,18 +84843,18 @@ async function scrollFullPage(page, budgetMs = 45000) {
84840
84843
  stableTicks = 0;
84841
84844
  prevHeight = height;
84842
84845
  }
84843
- await new Promise((r) => setTimeout(r, 500));
84846
+ await new Promise((r) => setTimeout(r, w.stable));
84844
84847
  } else {
84845
84848
  y = Math.min(y + Math.max(viewport * 0.8, 600), height);
84846
84849
  }
84847
84850
  }
84848
84851
  const finalHeight = document.documentElement.scrollHeight;
84849
84852
  window.scrollTo(0, finalHeight);
84850
- await new Promise((r) => setTimeout(r, 1500));
84853
+ await new Promise((r) => setTimeout(r, w.bottom));
84851
84854
  window.scrollTo(0, 0);
84852
- await new Promise((r) => setTimeout(r, 700));
84855
+ await new Promise((r) => setTimeout(r, w.top));
84853
84856
  return { steps, finalHeight, stableAtEnd: stableTicks >= STABLE_THRESHOLD };
84854
- }, budgetMs);
84857
+ }, { budget: budgetMs, w: W });
84855
84858
  return {
84856
84859
  steps: result.steps,
84857
84860
  finalHeight: result.finalHeight,
@@ -84918,6 +84921,7 @@ async function capturePage(page, opts) {
84918
84921
  pairKey: opts.pairKey,
84919
84922
  ...clickProbe ? { clickProbe } : {},
84920
84923
  ...firstViewport ? { firstViewport } : {},
84924
+ ...gateProbe ? { gateProbe } : {},
84921
84925
  ...response ? { variant: fingerprintVariant(html, response.headers(), cookieNames) } : {}
84922
84926
  });
84923
84927
  let cookieNames = [];
@@ -84929,8 +84933,11 @@ async function capturePage(page, opts) {
84929
84933
  let vitalsFullPage = null;
84930
84934
  let clickProbe;
84931
84935
  let firstViewport;
84936
+ let gateProbe;
84932
84937
  let html = "";
84933
84938
  const readVitals = async () => {
84939
+ if (opts.lean)
84940
+ return null;
84934
84941
  dlog(opts.side, opts.viewport, ` capturePage: vitals evaluate (cap=${Math.min(5000, remaining())}ms)`);
84935
84942
  return await Promise.race([
84936
84943
  page.evaluate(() => window.__parity_vitals).catch(() => null),
@@ -85003,7 +85010,7 @@ async function capturePage(page, opts) {
85003
85010
  const scrollBudget = Math.min(45000, remaining());
85004
85011
  dlog(opts.side, opts.viewport, ` capturePage: scrollFullPage start (budget=${scrollBudget}ms, remaining=${remaining()}ms)`);
85005
85012
  const scrollResult = await Promise.race([
85006
- scrollFullPage(page, scrollBudget).catch((err) => {
85013
+ scrollFullPage(page, scrollBudget, opts.lean).catch((err) => {
85007
85014
  dlog(opts.side, opts.viewport, ` capturePage: scrollFullPage threw: ${err.message}`);
85008
85015
  return;
85009
85016
  }),
@@ -85020,7 +85027,7 @@ async function capturePage(page, opts) {
85020
85027
  return;
85021
85028
  });
85022
85029
  }
85023
- await page.waitForTimeout(Math.min(800, remaining()));
85030
+ await page.waitForTimeout(Math.min(opts.lean ? 200 : 800, remaining()));
85024
85031
  vitalsFullPage = await readVitals();
85025
85032
  }
85026
85033
  }
@@ -85033,6 +85040,14 @@ async function capturePage(page, opts) {
85033
85040
  if (vitals === null) {
85034
85041
  vitals = await readVitals();
85035
85042
  }
85043
+ if (opts.lean && response) {
85044
+ gateProbe = await Promise.race([
85045
+ page.evaluate(`({ pageHeight: document.documentElement.scrollHeight, interactive: document.querySelectorAll(${JSON.stringify(INTERACTIVE_SELECTOR)}).length })`).then((r) => r).catch(() => {
85046
+ return;
85047
+ }),
85048
+ new Promise((resolve) => setTimeout(() => resolve(undefined), Math.min(3000, remaining())))
85049
+ ]);
85050
+ }
85036
85051
  if (!opts.skipScreenshot) {
85037
85052
  await Promise.race([
85038
85053
  stabilizeCarousels(page).catch(() => {
@@ -91566,7 +91581,8 @@ var PageCapture = z2.object({
91566
91581
  retried: z2.boolean().optional(),
91567
91582
  candDegraded: z2.string().optional(),
91568
91583
  clickProbe: ClickProbe.optional(),
91569
- firstViewport: FirstViewportProbe.optional()
91584
+ firstViewport: FirstViewportProbe.optional(),
91585
+ gateProbe: z2.object({ pageHeight: z2.number(), interactive: z2.number() }).optional()
91570
91586
  });
91571
91587
  var StepCapture = z2.object({
91572
91588
  step: z2.number(),
@@ -119873,6 +119889,9 @@ function markDegradedCaptures(candPages, baselines, recheck) {
119873
119889
  }
119874
119890
  return { degraded: true, marked };
119875
119891
  }
119892
+ function watchesCandHealth(candBase) {
119893
+ return isLocalhost(candBase);
119894
+ }
119876
119895
  async function recheckCand(urls, opts = {}) {
119877
119896
  const out = new Map;
119878
119897
  for (const url of urls) {
@@ -125542,11 +125561,336 @@ async function studioCommentCommand(opts, call = callTool2) {
125542
125561
  }
125543
125562
  }
125544
125563
 
125545
- // src/commands/drift.ts
125546
- import { spawnSync as spawnSync3 } from "node:child_process";
125547
- import { existsSync as existsSync41, readFileSync as readFileSync46 } from "node:fs";
125564
+ // src/commands/capacity.ts
125565
+ import { execFileSync as execFileSync7 } from "node:child_process";
125566
+ import { readFileSync as readFileSync46, statfsSync } from "node:fs";
125567
+ import os from "node:os";
125548
125568
  import { resolve as resolve24 } from "node:path";
125549
125569
  import chalk46 from "chalk";
125570
+
125571
+ // src/commands/capacity-tolerances.ts
125572
+ var CAPACITY_DEFAULTS = {
125573
+ perAgentGB: {
125574
+ value: 1.5,
125575
+ unit: "GB",
125576
+ basis: "assumed",
125577
+ note: "Resident memory of one agent's dev server + headless Chromium + a gate run. Not measured per agent: derived from 3 agents tipping a 16 GB machine into swap."
125578
+ },
125579
+ reserveGB: {
125580
+ value: 2,
125581
+ unit: "GB",
125582
+ basis: "assumed",
125583
+ note: "Memory left for the OS, the editor and the orchestrator itself; never handed to an agent."
125584
+ },
125585
+ perAgentCores: {
125586
+ value: 2,
125587
+ unit: "cores",
125588
+ basis: "assumed",
125589
+ note: "A dev server plus a build or test run keep about two cores busy. Load 11.8 on 10 cores with three agents is ~4 per agent at the peak; 2 is the sustained figure."
125590
+ },
125591
+ reserveCores: {
125592
+ value: 0,
125593
+ unit: "cores",
125594
+ basis: "assumed",
125595
+ note: "The 1-minute load average already contains whatever else runs on the machine, so no extra reserve."
125596
+ },
125597
+ swapThreshold: {
125598
+ value: 0.75,
125599
+ unit: "ratio (0-1)",
125600
+ basis: "assumed",
125601
+ note: "Swap used / swap total above this means the machine is already paging: free RAM figures stop meaning what they say."
125602
+ },
125603
+ swapCap: {
125604
+ value: 2,
125605
+ unit: "agents",
125606
+ basis: "assumed",
125607
+ note: "Slots allowed while swap is above the threshold. At 90% swap the measured case still ran three agents, slowly."
125608
+ },
125609
+ perAgentDiskGB: {
125610
+ value: 1,
125611
+ unit: "GB",
125612
+ basis: "assumed",
125613
+ note: "One worktree (~540 MB measured) plus its node_modules churn and a capture (~180 MB measured)."
125614
+ },
125615
+ reserveDiskGB: {
125616
+ value: 3,
125617
+ unit: "GB",
125618
+ basis: "assumed",
125619
+ note: "Headroom so a full disk does not corrupt a git operation or a capture mid-write."
125620
+ },
125621
+ maxParallel: {
125622
+ value: 5,
125623
+ unit: "agents",
125624
+ basis: "assumed",
125625
+ note: "The orchestrator's ceiling ('up to 5 per round'); `budget.maxParallel` in the state file overrides it via --max."
125626
+ },
125627
+ unknownCap: {
125628
+ value: 1,
125629
+ unit: "agents",
125630
+ basis: "assumed",
125631
+ note: "What an input that could not be read contributes. Never headroom: the caller runs one agent at a time."
125632
+ }
125633
+ };
125634
+
125635
+ // src/commands/capacity.ts
125636
+ var GB = 2 ** 30;
125637
+ var defaultParams = () => ({
125638
+ perAgentGB: CAPACITY_DEFAULTS.perAgentGB.value,
125639
+ reserveGB: CAPACITY_DEFAULTS.reserveGB.value,
125640
+ perAgentCores: CAPACITY_DEFAULTS.perAgentCores.value,
125641
+ reserveCores: CAPACITY_DEFAULTS.reserveCores.value,
125642
+ swapThreshold: CAPACITY_DEFAULTS.swapThreshold.value,
125643
+ swapCap: CAPACITY_DEFAULTS.swapCap.value,
125644
+ perAgentDiskGB: CAPACITY_DEFAULTS.perAgentDiskGB.value,
125645
+ reserveDiskGB: CAPACITY_DEFAULTS.reserveDiskGB.value,
125646
+ maxParallel: CAPACITY_DEFAULTS.maxParallel.value
125647
+ });
125648
+ function decideCapacity(inputs, params = defaultParams(), running) {
125649
+ const p = params;
125650
+ const unknown = [];
125651
+ const cap = CAPACITY_DEFAULTS.unknownCap.value;
125652
+ const term = (names, f) => {
125653
+ const missing = names.filter((n) => inputs[n] === null);
125654
+ unknown.push(...missing);
125655
+ return missing.length ? cap : Math.floor(f());
125656
+ };
125657
+ const memSlots = term(["availableGB"], () => (inputs.availableGB - p.reserveGB) / p.perAgentGB);
125658
+ const cpuSlots = term(["cores", "load1"], () => (inputs.cores - inputs.load1 - p.reserveCores) / p.perAgentCores);
125659
+ const diskSlots = term(["freeDiskGB"], () => (inputs.freeDiskGB - p.reserveDiskGB) / p.perAgentDiskGB);
125660
+ let swapCap = null;
125661
+ if (inputs.swapUsedGB === null || inputs.swapTotalGB === null) {
125662
+ unknown.push(...["swapUsedGB", "swapTotalGB"].filter((n) => inputs[n] === null));
125663
+ swapCap = cap;
125664
+ } else if (inputs.swapTotalGB > 0 && inputs.swapUsedGB / inputs.swapTotalGB > p.swapThreshold) {
125665
+ swapCap = p.swapCap;
125666
+ }
125667
+ const swapU = inputs.swapUsedGB === null || inputs.swapTotalGB === null;
125668
+ const cands = [
125669
+ ["swap", swapCap ?? Number.POSITIVE_INFINITY, swapU],
125670
+ ["memory", memSlots, inputs.availableGB === null],
125671
+ ["cpu", cpuSlots, inputs.cores === null || inputs.load1 === null],
125672
+ ["disk", diskSlots, inputs.freeDiskGB === null],
125673
+ ["max", p.maxParallel, false]
125674
+ ];
125675
+ let limitedBy = "max";
125676
+ let best = Number.POSITIVE_INFINITY;
125677
+ let bestUnknown = false;
125678
+ for (const [name, v, isUnknown] of cands) {
125679
+ if (v < best || v === best && isUnknown && !bestUnknown) {
125680
+ [limitedBy, best, bestUnknown] = [isUnknown ? "unknown" : name, v, isUnknown];
125681
+ }
125682
+ }
125683
+ const slots = Math.max(0, best);
125684
+ const report = {
125685
+ slots,
125686
+ admit: slots >= 1,
125687
+ limitedBy,
125688
+ inputs,
125689
+ terms: { memSlots, cpuSlots, swapCap, diskSlots },
125690
+ unknown,
125691
+ advice: ""
125692
+ };
125693
+ if (running !== undefined)
125694
+ report.running = running;
125695
+ report.advice = adviceFor(report);
125696
+ return report;
125697
+ }
125698
+ function adviceFor(r) {
125699
+ if (r.slots === 0) {
125700
+ return `no headroom (${r.limitedBy}): wait for a running agent to finish, then re-check`;
125701
+ }
125702
+ if (r.unknown.length) {
125703
+ return `could not read ${r.unknown.join(", ")}: run one agent at a time`;
125704
+ }
125705
+ return `room for ${r.slots} more agent(s) now (limited by ${r.limitedBy}); re-check before each start`;
125706
+ }
125707
+ function capacityExitCode(r) {
125708
+ if (r.slots < 1)
125709
+ return 1;
125710
+ return r.unknown.length ? 2 : 0;
125711
+ }
125712
+ function parseVmStat(text) {
125713
+ const size = /page size of (\d+) bytes/.exec(text)?.[1];
125714
+ const pages = (label) => {
125715
+ const m = new RegExp(`^${label}:\\s+(\\d+)\\.`, "m").exec(text);
125716
+ return m ? Number(m[1]) : null;
125717
+ };
125718
+ const free = pages("Pages free");
125719
+ if (!size || free === null)
125720
+ return null;
125721
+ const purgeable = pages("Pages purgeable") ?? 0;
125722
+ const fileBacked = pages("File-backed pages");
125723
+ const reclaimable = fileBacked !== null ? fileBacked : (pages("Pages speculative") ?? 0) + (pages("Pages inactive") ?? 0);
125724
+ return (free + reclaimable + purgeable) * Number(size);
125725
+ }
125726
+ function parseSwapUsage(text) {
125727
+ const grab = (k) => {
125728
+ const m = new RegExp(`${k} = ([\\d.]+)([KMGT])`).exec(text);
125729
+ if (!m)
125730
+ return null;
125731
+ return Number(m[1]) * 1024 ** ("KMGT".indexOf(m[2]) + 1);
125732
+ };
125733
+ const total = grab("total");
125734
+ const used = grab("used");
125735
+ return total === null || used === null ? null : { usedBytes: used, totalBytes: total };
125736
+ }
125737
+ var kb = (text, key) => {
125738
+ const m = new RegExp(`^${key}:\\s+(\\d+) kB`, "m").exec(text);
125739
+ return m ? Number(m[1]) * 1024 : null;
125740
+ };
125741
+ function parseMeminfo(text, cgroup) {
125742
+ let available = kb(text, "MemAvailable");
125743
+ if (available !== null && cgroup?.limit != null && cgroup.current != null) {
125744
+ available = Math.min(available, Math.max(0, cgroup.limit - cgroup.current));
125745
+ }
125746
+ const total = kb(text, "SwapTotal");
125747
+ const free = kb(text, "SwapFree");
125748
+ return {
125749
+ availableBytes: available,
125750
+ swapTotalBytes: total,
125751
+ swapUsedBytes: total !== null && free !== null ? total - free : null
125752
+ };
125753
+ }
125754
+ var realProbe = {
125755
+ platform: process.platform,
125756
+ readFile: (p) => {
125757
+ try {
125758
+ return readFileSync46(p, "utf8");
125759
+ } catch {
125760
+ return null;
125761
+ }
125762
+ },
125763
+ run: (cmd, args) => {
125764
+ try {
125765
+ return execFileSync7(cmd, args, {
125766
+ encoding: "utf8",
125767
+ timeout: 5000,
125768
+ stdio: ["ignore", "pipe", "ignore"]
125769
+ });
125770
+ } catch {
125771
+ return null;
125772
+ }
125773
+ },
125774
+ freeDiskBytes: (dir) => {
125775
+ try {
125776
+ const s = statfsSync(dir);
125777
+ return s.bavail * s.bsize;
125778
+ } catch {
125779
+ return null;
125780
+ }
125781
+ },
125782
+ cores: () => os.availableParallelism(),
125783
+ loadavg: () => os.loadavg(),
125784
+ freemem: () => os.freemem()
125785
+ };
125786
+ var toGB = (b) => b === null ? null : b / GB;
125787
+ function measure(dir, probe = realProbe) {
125788
+ let availableBytes = null;
125789
+ let swapUsed = null;
125790
+ let swapTotal = null;
125791
+ if (probe.platform === "darwin") {
125792
+ const vm = probe.run("vm_stat", []);
125793
+ availableBytes = vm ? parseVmStat(vm) : null;
125794
+ const sw = probe.run("sysctl", ["vm.swapusage"]);
125795
+ const s = sw ? parseSwapUsage(sw) : null;
125796
+ swapUsed = s?.usedBytes ?? null;
125797
+ swapTotal = s?.totalBytes ?? null;
125798
+ } else if (probe.platform === "linux") {
125799
+ const mi = probe.readFile("/proc/meminfo");
125800
+ if (mi) {
125801
+ const num = (p) => {
125802
+ const t = probe.readFile(p)?.trim();
125803
+ return t && /^\d+$/.test(t) ? Number(t) : null;
125804
+ };
125805
+ const m = parseMeminfo(mi, {
125806
+ limit: num("/sys/fs/cgroup/memory.max"),
125807
+ current: num("/sys/fs/cgroup/memory.current")
125808
+ });
125809
+ availableBytes = m.availableBytes;
125810
+ swapUsed = m.swapUsedBytes;
125811
+ swapTotal = m.swapTotalBytes;
125812
+ }
125813
+ } else if (probe.platform === "win32") {
125814
+ availableBytes = probe.freemem();
125815
+ }
125816
+ const load1 = probe.platform === "win32" ? null : probe.loadavg()[0] ?? null;
125817
+ const disk = probe.freeDiskBytes(dir);
125818
+ return {
125819
+ cores: probe.cores() || null,
125820
+ load1,
125821
+ availableGB: toGB(availableBytes),
125822
+ swapUsedGB: toGB(swapUsed),
125823
+ swapTotalGB: toGB(swapTotal),
125824
+ freeDiskGB: toGB(disk)
125825
+ };
125826
+ }
125827
+ var FLAG_TO_PARAM = [
125828
+ ["max", "maxParallel", 0],
125829
+ ["perAgentGb", "perAgentGB", Number.MIN_VALUE],
125830
+ ["reserveGb", "reserveGB", 0],
125831
+ ["perAgentCores", "perAgentCores", Number.MIN_VALUE],
125832
+ ["reserveCores", "reserveCores", 0],
125833
+ ["swapThreshold", "swapThreshold", 0],
125834
+ ["swapCap", "swapCap", 0],
125835
+ ["perAgentDiskGb", "perAgentDiskGB", Number.MIN_VALUE],
125836
+ ["reserveDiskGb", "reserveDiskGB", 0]
125837
+ ];
125838
+ function paramsFromFlags(flags) {
125839
+ const params = defaultParams();
125840
+ for (const [flag, key, min] of FLAG_TO_PARAM) {
125841
+ const raw = flags[flag];
125842
+ if (raw === undefined)
125843
+ continue;
125844
+ const n = Number(raw);
125845
+ if (raw.trim() === "" || !Number.isFinite(n) || n < min) {
125846
+ return {
125847
+ error: `--${flag.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)} "${raw}" is not a valid number`
125848
+ };
125849
+ }
125850
+ params[key] = n;
125851
+ }
125852
+ return params;
125853
+ }
125854
+ function capacityCommand(flags, probe = realProbe) {
125855
+ const params = paramsFromFlags(flags);
125856
+ let running;
125857
+ if (flags.running !== undefined) {
125858
+ const n = Number(flags.running);
125859
+ if (!Number.isInteger(n) || n < 0) {
125860
+ return fail3(flags, `--running "${flags.running}" is not a non-negative integer`);
125861
+ }
125862
+ running = n;
125863
+ }
125864
+ if ("error" in params)
125865
+ return fail3(flags, params.error);
125866
+ const r = decideCapacity(measure(resolve24(flags.dir ?? "."), probe), params, running);
125867
+ const code = capacityExitCode(r);
125868
+ if (flags.json) {
125869
+ console.log(JSON.stringify(r));
125870
+ } else {
125871
+ const f = (n, u) => n === null ? chalk46.yellow("unknown") : `${n.toFixed(1)} ${u}`.trim();
125872
+ const i = r.inputs;
125873
+ console.log(` machine ${f(i.cores, "cores")}, load1 ${f(i.load1, "")}, memory ${f(i.availableGB, "GB")} available, swap ${f(i.swapUsedGB, "")}/${f(i.swapTotalGB, "GB")}, disk ${f(i.freeDiskGB, "GB")} free`);
125874
+ console.log(` slots ${r.slots} (memory ${r.terms.memSlots}, cpu ${r.terms.cpuSlots}, swap ${r.terms.swapCap ?? "-"}, disk ${r.terms.diskSlots}, max ${params.maxParallel}; limited by ${r.limitedBy})`);
125875
+ if (r.running !== undefined)
125876
+ console.log(` running ${r.running} (already in the load; not subtracted)`);
125877
+ console.log(` ${code === 0 ? chalk46.green(r.advice) : code === 1 ? chalk46.red(r.advice) : chalk46.yellow(r.advice)}`);
125878
+ }
125879
+ return code;
125880
+ }
125881
+ function fail3(flags, message) {
125882
+ if (flags.json)
125883
+ console.log(JSON.stringify({ error: { code: "bad_flag", message } }));
125884
+ else
125885
+ console.error(chalk46.red(` ${message}`));
125886
+ return 2;
125887
+ }
125888
+
125889
+ // src/commands/drift.ts
125890
+ import { spawnSync as spawnSync3 } from "node:child_process";
125891
+ import { existsSync as existsSync41, readFileSync as readFileSync47 } from "node:fs";
125892
+ import { resolve as resolve25 } from "node:path";
125893
+ import chalk47 from "chalk";
125550
125894
  var DRIFT_EXCLUDES = [":!.deco"];
125551
125895
  var SHA_RE = /^[0-9a-f]{7,64}$/i;
125552
125896
  function resolveDriftInputs(state, flags) {
@@ -125735,7 +126079,7 @@ function loadState(path) {
125735
126079
  if (!existsSync41(path))
125736
126080
  return null;
125737
126081
  try {
125738
- return JSON.parse(readFileSync46(path, "utf8"));
126082
+ return JSON.parse(readFileSync47(path, "utf8"));
125739
126083
  } catch (err) {
125740
126084
  return { error: { code: "state_unreadable", message: `${path}: ${err.message}` } };
125741
126085
  }
@@ -125743,7 +126087,7 @@ function loadState(path) {
125743
126087
  function runDrift(flags) {
125744
126088
  let state = null;
125745
126089
  if (!(flags.source && flags.base)) {
125746
- const loaded = loadState(resolve24(flags.state ?? ".parity/migration.json"));
126090
+ const loaded = loadState(resolve25(flags.state ?? ".parity/migration.json"));
125747
126091
  if (loaded && "error" in loaded)
125748
126092
  return loaded;
125749
126093
  state = loaded;
@@ -125759,7 +126103,7 @@ function driftCommand(flags) {
125759
126103
  if (flags.json)
125760
126104
  console.log(JSON.stringify({ error: r.error }));
125761
126105
  else
125762
- console.error(chalk46.red(` could not measure drift [${r.error.code}]: ${r.error.message}`));
126106
+ console.error(chalk47.red(` could not measure drift [${r.error.code}]: ${r.error.message}`));
125763
126107
  return 2;
125764
126108
  }
125765
126109
  if (flags.json) {
@@ -125777,8 +126121,569 @@ function driftCommand(flags) {
125777
126121
  return r.commits > 0 ? 1 : 0;
125778
126122
  }
125779
126123
 
126124
+ // src/commands/gate.ts
126125
+ import { existsSync as existsSync42, mkdirSync as mkdirSync26, readFileSync as readFileSync48, renameSync as renameSync4, writeFileSync as writeFileSync39 } from "node:fs";
126126
+ import { join as join55, resolve as resolve26 } from "node:path";
126127
+ import chalk48 from "chalk";
126128
+ import { z as z7 } from "zod";
126129
+
126130
+ // src/commands/gate-tolerances.ts
126131
+ var GATE_TOLERANCES = {
126132
+ heightRel: {
126133
+ value: 0.05,
126134
+ unit: "ratio (0-1) of the baseline page height",
126135
+ basis: "assumed",
126136
+ note: "A page whose scroll height after the autoscroll moved by more than this share of its baseline height is changed. Lazy images and webfonts settle a page by a percent or two between two loads of the same code; a missing section or a crashed list moves it by tens of percent."
126137
+ },
126138
+ heightMinPx: {
126139
+ value: 100,
126140
+ unit: "px",
126141
+ basis: "assumed",
126142
+ note: "A height change under this is never reported, whatever the ratio: on a short page 5% is a few pixels, which a font swap or a carousel slide alone can produce. A section is rarely shorter than this."
126143
+ },
126144
+ interactiveDropRel: {
126145
+ value: 0.1,
126146
+ unit: "ratio (0-1) of the baseline element count",
126147
+ basis: "assumed",
126148
+ note: "The interactive element count (links, buttons, fields) falling by at least this share is a regression. Only a DROP counts: a page that gains elements (a new section, a hydrated shelf) is not broken."
126149
+ },
126150
+ interactiveDropMin: {
126151
+ value: 3,
126152
+ unit: "elements",
126153
+ basis: "assumed",
126154
+ note: "A drop smaller than this is never reported: on a small page one removed link is 10%. A section that disappears takes more than three links or buttons with it."
126155
+ },
126156
+ htmlCollapseRatio: {
126157
+ value: 0.5,
126158
+ unit: "ratio (0-1) of the baseline server HTML bytes",
126159
+ basis: "assumed",
126160
+ note: "A server document smaller than this share of the baseline's is a regression (a loader that now throws and renders its empty branch). Looser than candHealth.bytesCollapseRatio on purpose: that one says a SERVER is sick, this one says the CODE changed what it serves, and a changed page can legitimately shrink by a third."
126161
+ }
126162
+ };
126163
+
126164
+ // src/commands/gate.ts
126165
+ var gateKey = (path, viewport) => `${path}::${viewport}`;
126166
+ var is2xx = (s) => s !== null && s >= 200 && s < 300;
126167
+ var FORMAT_VERSION2 = 1;
126168
+ var BASELINE_FILE = "gate-baseline.json";
126169
+ var EntrySchema = z7.object({
126170
+ status: z7.number().nullable().default(null),
126171
+ htmlBytes: z7.number().nullable().default(null),
126172
+ errors: z7.array(z7.object({
126173
+ cls: z7.enum(["section-crash", "hydration", "uncaught"]),
126174
+ key: z7.string(),
126175
+ text: z7.string().default("")
126176
+ })).default([]),
126177
+ pageHeight: z7.number().nullable().default(null),
126178
+ interactive: z7.number().nullable().default(null),
126179
+ ssrSections: z7.array(z7.string()).nullable().default(null),
126180
+ deferredSections: z7.array(z7.string()).nullable().default(null),
126181
+ blockedShare: z7.number().nullable().default(null)
126182
+ });
126183
+ var BaselineSchema = z7.object({
126184
+ formatVersion: z7.number().default(FORMAT_VERSION2),
126185
+ recordedAt: z7.string().optional(),
126186
+ cand: z7.string().optional(),
126187
+ entries: z7.record(z7.string(), EntrySchema).default({})
126188
+ });
126189
+ function readBaseline(dir) {
126190
+ const file = join55(dir, BASELINE_FILE);
126191
+ if (!existsSync42(file))
126192
+ return { missing: true };
126193
+ try {
126194
+ const raw = JSON.parse(readFileSync48(file, "utf8"));
126195
+ if (typeof raw.formatVersion === "number" && raw.formatVersion > FORMAT_VERSION2) {
126196
+ return {
126197
+ error: `${file}: formatVersion ${raw.formatVersion} is newer than this parity reads (${FORMAT_VERSION2})`
126198
+ };
126199
+ }
126200
+ return { baseline: BaselineSchema.parse(raw) };
126201
+ } catch (err) {
126202
+ return { error: `${file}: ${err.message}` };
126203
+ }
126204
+ }
126205
+ function writeBaseline(dir, cand, entries, prior) {
126206
+ mkdirSync26(dir, { recursive: true });
126207
+ const file = join55(dir, BASELINE_FILE);
126208
+ const next = {
126209
+ formatVersion: FORMAT_VERSION2,
126210
+ recordedAt: new Date().toISOString(),
126211
+ cand,
126212
+ entries: { ...prior?.entries ?? {}, ...entries }
126213
+ };
126214
+ const tmp = `${file}.${process.pid}.tmp`;
126215
+ writeFileSync39(tmp, `${JSON.stringify(next, null, 2)}
126216
+ `, "utf8");
126217
+ renameSync4(tmp, file);
126218
+ return file;
126219
+ }
126220
+ var MEASUREMENT_FAILURE2 = /^\[(capture-timeout|browser-disconnected|navigation-error)\]/;
126221
+ function urlOf(e) {
126222
+ const m = /https?:\/\/[^\s)]+/.exec(e.location ?? "");
126223
+ return m ? m[0] : null;
126224
+ }
126225
+ function isThirdParty3(e, pageUrl) {
126226
+ const u = urlOf(e);
126227
+ if (!u)
126228
+ return false;
126229
+ try {
126230
+ return registrableDomain(new URL(u).hostname) !== registrableDomain(new URL(pageUrl).hostname);
126231
+ } catch {
126232
+ return false;
126233
+ }
126234
+ }
126235
+ function gateErrors(entries, pageUrl, ignorePatterns = []) {
126236
+ const own = entries.filter((e) => !MEASUREMENT_FAILURE2.test(e.text) && !isThirdParty3(e, pageUrl));
126237
+ const out = new Map;
126238
+ for (const c of diffConsole(own, [], { ignorePatterns, errorsOnly: true }).prodErrors) {
126239
+ let cls = null;
126240
+ if (c.cls === "section-crash" || c.cls === "hydration")
126241
+ cls = c.cls;
126242
+ else if (c.cls === "generic" && (/^at\s/.test(c.entry.location ?? "") || /^Uncaught\b/.test(c.entry.text)))
126243
+ cls = "uncaught";
126244
+ if (!cls)
126245
+ continue;
126246
+ const section = cls === "section-crash" ? parseSectionCrash(c.entry.text).section : undefined;
126247
+ const key = `${cls}:${section ?? c.key.slice(0, 160)}`;
126248
+ if (!out.has(key))
126249
+ out.set(key, { cls, key, text: c.entry.text.slice(0, 200) });
126250
+ }
126251
+ return [...out.values()];
126252
+ }
126253
+ function docBytes(cap) {
126254
+ const docs = cap.network.filter((n) => n.resourceType === "document" && n.method === "GET");
126255
+ return (docs.find((n) => n.url === cap.url || n.url === cap.finalUrl) ?? docs[0])?.bytes ?? null;
126256
+ }
126257
+ function observe(path, viewport, cap, ctx) {
126258
+ const base = { key: gateKey(path, viewport), path, viewport };
126259
+ const no = (unjudged) => ({ ...base, entry: null, unjudged });
126260
+ if (ctx.cold)
126261
+ return no(ctx.cold);
126262
+ if (ctx.primeFailed)
126263
+ return no("the priming load never answered: the candidate is cold or down");
126264
+ if (!cap)
126265
+ return no("not captured");
126266
+ if (cap.candDegraded)
126267
+ return no(`candidate degraded: ${cap.candDegraded}`);
126268
+ if (cap.status === 0 && !ctx.primedOk) {
126269
+ return no("never answered, and nothing shows the server was up (a remote candidate is not primed)");
126270
+ }
126271
+ const fv = cap.firstViewport;
126272
+ const view = fv && fv.serverChars > 0 ? fv.sections.filter(inViewport) : null;
126273
+ const names = (ss) => ss ? [...new Set(ss.map((s) => s.name))] : null;
126274
+ const pts = cap.clickProbe?.points ?? [];
126275
+ return {
126276
+ ...base,
126277
+ ms: cap.durationMs,
126278
+ entry: {
126279
+ status: cap.status,
126280
+ htmlBytes: docBytes(cap),
126281
+ errors: gateErrors(cap.console, cap.finalUrl || cap.url, ctx.ignorePatterns),
126282
+ pageHeight: cap.gateProbe?.pageHeight ?? null,
126283
+ interactive: cap.gateProbe?.interactive ?? null,
126284
+ ssrSections: names(view?.filter((s) => !isDeferred(s)) ?? null),
126285
+ deferredSections: names(view?.filter(isDeferred) ?? null),
126286
+ blockedShare: pts.length > 0 ? pts.filter(isBlocked).length / pts.length : null
126287
+ }
126288
+ };
126289
+ }
126290
+ var R = TOLERANCES.clickProbe.blockedShareMin.value;
126291
+ function judgeEntry(cur, b, prodHeight) {
126292
+ const bad = [];
126293
+ const good = [];
126294
+ const on = [];
126295
+ if (b.status !== null && cur.status !== null) {
126296
+ on.push("status");
126297
+ if (is2xx(b.status) && !is2xx(cur.status)) {
126298
+ bad.push(cur.status === 0 ? `no answer where the baseline answered ${b.status}` : `status ${b.status} -> ${cur.status}`);
126299
+ } else if (!is2xx(b.status) && is2xx(cur.status))
126300
+ good.push(`status ${b.status} -> ${cur.status}`);
126301
+ }
126302
+ on.push("errors");
126303
+ const had = new Set(b.errors.map((e) => e.key));
126304
+ const has = new Set(cur.errors.map((e) => e.key));
126305
+ for (const e of cur.errors) {
126306
+ if (!had.has(e.key))
126307
+ bad.push(`new ${e.cls}: ${e.key.slice(e.cls.length + 1) || e.text}`);
126308
+ }
126309
+ for (const e of b.errors)
126310
+ if (!has.has(e.key))
126311
+ good.push(`${e.cls} gone: ${e.key}`);
126312
+ if (b.htmlBytes && cur.htmlBytes !== null) {
126313
+ on.push("html-size");
126314
+ if (cur.htmlBytes < b.htmlBytes * GATE_TOLERANCES.htmlCollapseRatio.value) {
126315
+ bad.push(`server HTML ${kb2(b.htmlBytes)} -> ${kb2(cur.htmlBytes)}`);
126316
+ }
126317
+ }
126318
+ if (b.blockedShare !== null && cur.blockedShare !== null) {
126319
+ on.push("click-blockers");
126320
+ if (b.blockedShare < R && cur.blockedShare >= R) {
126321
+ bad.push(`an overlay now blocks ${Math.round(cur.blockedShare * 100)}% of the viewport's click points`);
126322
+ }
126323
+ }
126324
+ if (b.ssrSections && cur.deferredSections) {
126325
+ on.push("first-viewport");
126326
+ const lost = b.ssrSections.filter((n) => cur.deferredSections?.includes(n));
126327
+ if (lost.length)
126328
+ bad.push(`first-viewport section ${lost.join(", ")} was server-rendered, now deferred`);
126329
+ const won = (b.deferredSections ?? []).filter((n) => cur.ssrSections?.includes(n));
126330
+ if (won.length)
126331
+ good.push(`first-viewport section ${won.join(", ")} now server-rendered`);
126332
+ }
126333
+ if (b.interactive && cur.interactive !== null) {
126334
+ on.push("interactive");
126335
+ const drop = b.interactive - cur.interactive;
126336
+ if (drop >= GATE_TOLERANCES.interactiveDropMin.value && drop / b.interactive >= GATE_TOLERANCES.interactiveDropRel.value) {
126337
+ bad.push(`interactive elements ${b.interactive} -> ${cur.interactive}`);
126338
+ }
126339
+ }
126340
+ if (b.pageHeight && cur.pageHeight !== null) {
126341
+ on.push("height");
126342
+ const delta = cur.pageHeight - b.pageHeight;
126343
+ if (Math.abs(delta) >= GATE_TOLERANCES.heightMinPx.value && Math.abs(delta) / b.pageHeight > GATE_TOLERANCES.heightRel.value) {
126344
+ const text = `page height ${Math.round(b.pageHeight)} -> ${Math.round(cur.pageHeight)} px (${delta > 0 ? "+" : ""}${Math.round(delta)})`;
126345
+ if (prodHeight === undefined)
126346
+ bad.push(text);
126347
+ else if (Math.abs(cur.pageHeight - prodHeight) > Math.abs(b.pageHeight - prodHeight))
126348
+ bad.push(`${text}, away from prod's ${Math.round(prodHeight)}`);
126349
+ else
126350
+ good.push(`${text}, toward prod's ${Math.round(prodHeight)}`);
126351
+ }
126352
+ }
126353
+ return { bad, good, judgedOn: on };
126354
+ }
126355
+ var kb2 = (n) => `${Math.round(n / 1024)} KB`;
126356
+ function judgeGate(input) {
126357
+ const { baseline } = input;
126358
+ const cells = input.observations.map((o) => {
126359
+ const cell = {
126360
+ key: o.key,
126361
+ page: o.path,
126362
+ viewport: o.viewport,
126363
+ ...o.ms !== undefined ? { ms: o.ms } : {}
126364
+ };
126365
+ if (!o.entry) {
126366
+ const reason = o.unjudged ?? "not judged";
126367
+ return {
126368
+ ...cell,
126369
+ verdict: "inconclusive",
126370
+ reason,
126371
+ reasons: [reason],
126372
+ baselined: false,
126373
+ judgedOn: []
126374
+ };
126375
+ }
126376
+ const b = baseline?.entries[o.key];
126377
+ if (!b) {
126378
+ const bad = [];
126379
+ if (!is2xx(o.entry.status))
126380
+ bad.push(`new page answers ${o.entry.status} (no baseline)`);
126381
+ for (const e of o.entry.errors.filter((x) => x.cls === "section-crash"))
126382
+ bad.push(`new page: section-crash ${e.key.slice("section-crash:".length)} (no baseline)`);
126383
+ return bad.length ? {
126384
+ ...cell,
126385
+ verdict: "regressed",
126386
+ reason: bad[0],
126387
+ reasons: bad,
126388
+ baselined: false,
126389
+ judgedOn: ["status", "section-crash"]
126390
+ } : {
126391
+ ...cell,
126392
+ verdict: "new",
126393
+ reason: "no baseline entry for this page",
126394
+ reasons: ["no baseline entry for this page"],
126395
+ baselined: false,
126396
+ judgedOn: ["status", "section-crash"]
126397
+ };
126398
+ }
126399
+ const { bad, good, judgedOn } = judgeEntry(o.entry, b, input.goldenHeights?.[o.key]);
126400
+ if (bad.length)
126401
+ return {
126402
+ ...cell,
126403
+ verdict: "regressed",
126404
+ reason: bad[0],
126405
+ reasons: bad,
126406
+ baselined: true,
126407
+ judgedOn
126408
+ };
126409
+ if (good.length)
126410
+ return {
126411
+ ...cell,
126412
+ verdict: "improved",
126413
+ reason: good[0],
126414
+ reasons: good,
126415
+ baselined: true,
126416
+ judgedOn
126417
+ };
126418
+ return {
126419
+ ...cell,
126420
+ verdict: "unchanged",
126421
+ reason: "same as baseline",
126422
+ reasons: [],
126423
+ baselined: true,
126424
+ judgedOn
126425
+ };
126426
+ });
126427
+ const n = (v) => cells.filter((c) => c.verdict === v).length;
126428
+ const counts = {
126429
+ compared: cells.filter((c) => c.baselined).length,
126430
+ unchanged: n("unchanged"),
126431
+ improved: n("improved"),
126432
+ regressed: n("regressed"),
126433
+ new: n("new"),
126434
+ inconclusive: n("inconclusive")
126435
+ };
126436
+ const parts = [`${counts.compared} compared`, `${counts.unchanged} unchanged`];
126437
+ if (counts.improved)
126438
+ parts.push(`${counts.improved} improved`);
126439
+ parts.push(`${counts.regressed} regressed`);
126440
+ if (counts.new)
126441
+ parts.push(`${counts.new} new (no baseline)`);
126442
+ if (counts.inconclusive)
126443
+ parts.push(`${counts.inconclusive} inconclusive`);
126444
+ let exit;
126445
+ let summary = parts.join(", ");
126446
+ if (counts.regressed > 0)
126447
+ exit = 1;
126448
+ else if (counts.compared === 0) {
126449
+ exit = 2;
126450
+ summary = `nothing compared: ${cells.length === 0 ? "no pages" : baseline === null ? "no baseline" : counts.inconclusive === cells.length ? "every page was inconclusive" : "no page has a baseline entry"} (${parts.join(", ")})`;
126451
+ } else if (counts.inconclusive > 0) {
126452
+ exit = 2;
126453
+ summary += `: ${counts.inconclusive} page(s) could not be judged, treat them as unverified`;
126454
+ } else
126455
+ exit = 0;
126456
+ return { cells, counts, summary, exit };
126457
+ }
126458
+ function updateRefusal(observations) {
126459
+ if (observations.length === 0)
126460
+ return ["no pages were observed"];
126461
+ return observations.filter((o) => !o.entry).map((o) => `${o.key}: ${o.unjudged ?? "not judged"}`);
126462
+ }
126463
+ var pathAndQuery2 = (url) => {
126464
+ try {
126465
+ const u = new URL(url);
126466
+ return (u.pathname || "/") + u.search;
126467
+ } catch {
126468
+ return url;
126469
+ }
126470
+ };
126471
+ function goldenPages(dir) {
126472
+ const g = loadGolden(dir);
126473
+ if ("error" in g)
126474
+ return g;
126475
+ const { prodPages, candPages } = g.rec.ctx;
126476
+ const idOf = (p) => p.pairKey ?? pathAndQuery2(p.url);
126477
+ const pages = [];
126478
+ const heights = {};
126479
+ for (const c of candPages.filter((p) => p.pass === "visual")) {
126480
+ const path = pathAndQuery2(c.url);
126481
+ if (!pages.includes(path))
126482
+ pages.push(path);
126483
+ const prod = prodPages.find((p) => p.pass === "visual" && p.viewport === c.viewport && idOf(p) === idOf(c));
126484
+ const px = prod ? readPngSize(prod.screenshotPath)?.height : undefined;
126485
+ if (px)
126486
+ heights[gateKey(path, c.viewport)] = px / (VIEWPORT_PRESETS[c.viewport].deviceScaleFactor ?? 1);
126487
+ }
126488
+ return { pages, heights };
126489
+ }
126490
+ async function captureGate(cand, paths, viewports) {
126491
+ const browser = await launchBrowser({ headless: true });
126492
+ const ignorePatterns = loadParityIgnore().ignoreConsolePatterns;
126493
+ const urlOf = (p) => new URL(p, cand).toString();
126494
+ try {
126495
+ const t0 = Date.now();
126496
+ const baselines = new Map;
126497
+ const primeUrls = planCandPrime(cand, paths.map(urlOf), new Set);
126498
+ const primed = new Set;
126499
+ const prime = await primeCandUrls(browser, primeUrls, primed, { viewport: viewports[0] }, baselines);
126500
+ const primeFailed = new Set(prime.failed);
126501
+ const cold = isColdCandidate({ total: prime.attempted, failed: prime.failed.length }) ? `the candidate is cold: ${prime.failed.length} of ${prime.attempted} priming loads never answered` : undefined;
126502
+ const primeMs = Date.now() - t0;
126503
+ const caps = new Map;
126504
+ const t1 = Date.now();
126505
+ if (!cold) {
126506
+ for (const viewport of viewports) {
126507
+ const ctx = await newContext(browser, { viewport, cohortCookieValue: "control" });
126508
+ try {
126509
+ for (const path of paths) {
126510
+ const url = urlOf(path);
126511
+ if (primeFailed.has(url))
126512
+ continue;
126513
+ const page = await ctx.newPage();
126514
+ try {
126515
+ const cap = await capturePage(page, {
126516
+ url,
126517
+ side: "cand",
126518
+ viewport,
126519
+ screenshotPath: "",
126520
+ settleMs: 800,
126521
+ timeoutMs: 20000,
126522
+ skipScreenshot: true,
126523
+ noNetworkIdle: true,
126524
+ lean: true
126525
+ });
126526
+ caps.set(gateKey(path, viewport), cap);
126527
+ } catch {} finally {
126528
+ await page.close().catch(() => {
126529
+ return;
126530
+ });
126531
+ }
126532
+ }
126533
+ } finally {
126534
+ await ctx.close().catch(() => {
126535
+ return;
126536
+ });
126537
+ }
126538
+ }
126539
+ }
126540
+ const captureMs = Date.now() - t1;
126541
+ let degraded = false;
126542
+ if (!cold && watchesCandHealth(cand)) {
126543
+ const after = await recheckCand([...baselines.keys()]);
126544
+ degraded = markDegradedCaptures([...caps.values()], baselines, judgeRecheck(baselines, after)).degraded;
126545
+ }
126546
+ const observations = viewports.flatMap((viewport) => paths.map((path) => observe(path, viewport, caps.get(gateKey(path, viewport)), {
126547
+ candUrl: cand,
126548
+ cold,
126549
+ primeFailed: primeFailed.has(urlOf(path)),
126550
+ primedOk: baselines.has(urlOf(path)),
126551
+ ignorePatterns
126552
+ })));
126553
+ return { observations, primeMs, captureMs, degraded };
126554
+ } finally {
126555
+ await browser.close().catch(() => {
126556
+ return;
126557
+ });
126558
+ }
126559
+ }
126560
+ function fail4(flags, code, message) {
126561
+ if (flags.json)
126562
+ console.log(JSON.stringify({ exit: 2, error: { code, message } }));
126563
+ else
126564
+ console.error(chalk48.red(` gate: ${message}`));
126565
+ return 2;
126566
+ }
126567
+ var normalize4 = (p) => p.startsWith("/") ? p : `/${p}`;
126568
+ async function gateCommand(flags, deps = { capture: captureGate }) {
126569
+ if (!flags.cand)
126570
+ return fail4(flags, "no_cand", "--cand <url> is required");
126571
+ try {
126572
+ new URL(flags.cand);
126573
+ } catch {
126574
+ return fail4(flags, "bad_cand", `--cand "${flags.cand}" is not a URL`);
126575
+ }
126576
+ const viewports = [];
126577
+ for (const v of (flags.viewports ?? "mobile,desktop").split(",").map((s) => s.trim()).filter(Boolean)) {
126578
+ const r = Viewport.safeParse(v);
126579
+ if (!r.success)
126580
+ return fail4(flags, "bad_viewport", `--viewports: "${v}" is not mobile, tablet or desktop`);
126581
+ if (!viewports.includes(r.data))
126582
+ viewports.push(r.data);
126583
+ }
126584
+ if (viewports.length === 0)
126585
+ return fail4(flags, "bad_viewport", "--viewports is empty");
126586
+ const dir = resolve26(flags.dir ?? ".parity");
126587
+ const read = readBaseline(dir);
126588
+ if ("error" in read)
126589
+ return fail4(flags, "baseline_unreadable", read.error);
126590
+ const baseline = "baseline" in read ? read.baseline : null;
126591
+ if (!baseline && !flags.updateBaseline) {
126592
+ return fail4(flags, "no_baseline", `nothing compared: no baseline at ${join55(dir, BASELINE_FILE)}. Run once with --update-baseline on a state known to be good.`);
126593
+ }
126594
+ let paths = null;
126595
+ let heights;
126596
+ try {
126597
+ const explicit = resolveExplicitPages(flags.pagesFile, flags.pages);
126598
+ if (explicit)
126599
+ paths = explicit.map((p) => normalize4(p.cand));
126600
+ } catch (err) {
126601
+ return fail4(flags, "bad_pages", err.message);
126602
+ }
126603
+ if (flags.golden) {
126604
+ const g = goldenPages(flags.golden);
126605
+ if ("error" in g)
126606
+ return fail4(flags, "bad_golden", g.error);
126607
+ paths ??= g.pages;
126608
+ heights = g.heights;
126609
+ }
126610
+ const only = flags.only ? parsePageList(flags.only, { fallback: [] }).map(normalize4) : null;
126611
+ if (!paths && baseline) {
126612
+ paths = [...new Set(Object.keys(baseline.entries).map((k) => k.slice(0, k.lastIndexOf("::"))))];
126613
+ if (only)
126614
+ paths = only;
126615
+ } else if (paths && only) {
126616
+ paths = paths.filter((p) => only.includes(p));
126617
+ }
126618
+ if (!paths || paths.length === 0) {
126619
+ return fail4(flags, "no_pages", only ? "--only matched none of the pages" : "no pages: pass --pages, --pages-file or --golden");
126620
+ }
126621
+ paths = [...new Set(paths)];
126622
+ const cap = await deps.capture(flags.cand, paths, viewports).catch((err) => err);
126623
+ if (cap instanceof Error)
126624
+ return fail4(flags, "capture_failed", cap.message);
126625
+ if (flags.updateBaseline) {
126626
+ const why = updateRefusal(cap.observations);
126627
+ if (why.length > 0) {
126628
+ if (flags.json)
126629
+ console.log(JSON.stringify({ exit: 2, baselineWritten: false, refused: why }));
126630
+ else {
126631
+ console.error(chalk48.red(" gate: baseline NOT written, these pages could not be judged:"));
126632
+ for (const w of why)
126633
+ console.error(chalk48.red(` ${w}`));
126634
+ }
126635
+ return 2;
126636
+ }
126637
+ const entries = Object.fromEntries(cap.observations.map((o) => [o.key, o.entry]));
126638
+ const file = writeBaseline(dir, flags.cand, entries, baseline);
126639
+ const broken = cap.observations.filter((o) => o.entry && (!is2xx(o.entry.status) || o.entry.errors.length > 0));
126640
+ if (flags.json) {
126641
+ console.log(JSON.stringify({
126642
+ exit: 0,
126643
+ baselineWritten: true,
126644
+ file,
126645
+ pages: cap.observations.length,
126646
+ withDefects: broken.map((o) => o.key)
126647
+ }));
126648
+ } else {
126649
+ console.log(chalk48.green(` baseline written: ${cap.observations.length} page x viewport -> ${file}`));
126650
+ for (const o of broken) {
126651
+ console.log(chalk48.yellow(` ! ${o.key} was recorded WITH a defect (status ${o.entry?.status}, ${o.entry?.errors.length} error(s)): future runs will treat it as accepted`));
126652
+ }
126653
+ }
126654
+ return 0;
126655
+ }
126656
+ const report = judgeGate({ observations: cap.observations, baseline, goldenHeights: heights });
126657
+ if (flags.json) {
126658
+ console.log(JSON.stringify({
126659
+ exit: report.exit,
126660
+ summary: report.summary,
126661
+ counts: report.counts,
126662
+ cells: report.cells,
126663
+ degraded: cap.degraded,
126664
+ timing: { primeMs: cap.primeMs, captureMs: cap.captureMs }
126665
+ }));
126666
+ } else {
126667
+ for (const c of report.cells) {
126668
+ const mark = {
126669
+ regressed: chalk48.red("REGRESSED "),
126670
+ improved: chalk48.green("improved "),
126671
+ unchanged: chalk48.dim("unchanged "),
126672
+ new: chalk48.yellow("new "),
126673
+ inconclusive: chalk48.yellow("inconclusive")
126674
+ }[c.verdict];
126675
+ const ms = c.ms !== undefined ? chalk48.dim(` ${(c.ms / 1000).toFixed(1)}s`) : "";
126676
+ console.log(` ${mark} ${c.page} [${c.viewport}] ${c.verdict === "unchanged" ? "" : c.reason}${ms}`);
126677
+ }
126678
+ const color = report.exit === 0 ? chalk48.green : report.exit === 1 ? chalk48.red : chalk48.yellow;
126679
+ console.log(color(` ${report.summary}`));
126680
+ console.log(chalk48.dim(` prime ${(cap.primeMs / 1000).toFixed(1)}s, capture ${(cap.captureMs / 1000).toFixed(1)}s`));
126681
+ }
126682
+ return report.exit;
126683
+ }
126684
+
125780
126685
  // src/commands/variance.ts
125781
- import chalk47 from "chalk";
126686
+ import chalk49 from "chalk";
125782
126687
 
125783
126688
  // src/diff/variance.ts
125784
126689
  var uniq2 = (xs) => [...new Set(xs)];
@@ -125882,7 +126787,7 @@ function formatVariance(report, opts = {}) {
125882
126787
  // src/commands/variance.ts
125883
126788
  function varianceCommand(opts) {
125884
126789
  if (opts.runIds.length < 2) {
125885
- console.error(chalk47.red(" informe pelo menos 2 runs: parity variance <runIdA> <runIdB> [...]"));
126790
+ console.error(chalk49.red(" informe pelo menos 2 runs: parity variance <runIdA> <runIdB> [...]"));
125886
126791
  return 2;
125887
126792
  }
125888
126793
  const budget = Number.parseFloat(opts.scoreBudget ?? "1");
@@ -125891,7 +126796,7 @@ function varianceCommand(opts) {
125891
126796
  try {
125892
126797
  runs.push(loadRun(opts.output, id));
125893
126798
  } catch (err) {
125894
- console.error(chalk47.red(` ✖ ${err.message}`));
126799
+ console.error(chalk49.red(` ✖ ${err.message}`));
125895
126800
  return 2;
125896
126801
  }
125897
126802
  }
@@ -125900,7 +126805,7 @@ function varianceCommand(opts) {
125900
126805
  console.log(JSON.stringify({ ...report, stable: isStable(report, budget), scoreBudget: budget }));
125901
126806
  } else {
125902
126807
  console.log("");
125903
- console.log(chalk47.bold(" Score variance"));
126808
+ console.log(chalk49.bold(" Score variance"));
125904
126809
  for (const line of formatVariance(report, { scoreBudget: budget }).split(`
125905
126810
  `)) {
125906
126811
  console.log(` ${line}`);
@@ -125912,10 +126817,10 @@ function varianceCommand(opts) {
125912
126817
  }
125913
126818
 
125914
126819
  // src/commands/vitals.ts
125915
- import { existsSync as existsSync42, readFileSync as readFileSync47, writeFileSync as writeFileSync39 } from "node:fs";
126820
+ import { existsSync as existsSync43, readFileSync as readFileSync49, writeFileSync as writeFileSync40 } from "node:fs";
125916
126821
  import { availableParallelism } from "node:os";
125917
- import { join as join55 } from "node:path";
125918
- import chalk48 from "chalk";
126822
+ import { join as join56 } from "node:path";
126823
+ import chalk50 from "chalk";
125919
126824
  import ora9 from "ora";
125920
126825
 
125921
126826
  // src/checks/agentic-nav.ts
@@ -126223,10 +127128,10 @@ async function vitalsCommand(opts) {
126223
127128
  const runId = newRunId();
126224
127129
  const paths = createRunDir(opts.output, runId);
126225
127130
  const t0 = Date.now();
126226
- console.log(chalk48.bold(`
127131
+ console.log(chalk50.bold(`
126227
127132
  parity vitals ${runId}`));
126228
- console.log(chalk48.dim(` prod: ${opts.prod}`));
126229
- console.log(chalk48.dim(` cand: ${opts.cand}`));
127133
+ console.log(chalk50.dim(` prod: ${opts.prod}`));
127134
+ console.log(chalk50.dim(` cand: ${opts.cand}`));
126230
127135
  const discoverSpinner = ora9("Descobrindo páginas…").start();
126231
127136
  const pagePaths = await discoverPagePaths2(opts.prod, opts);
126232
127137
  if (pagePaths.length === 0) {
@@ -126258,12 +127163,12 @@ async function vitalsCommand(opts) {
126258
127163
  completed++;
126259
127164
  const elapsed = ((Date.now() - t0) / 1000).toFixed(0);
126260
127165
  const etaSec = completed > 0 ? Math.round((Date.now() - t0) / completed * (total - completed) / 1000) : 0;
126261
- progress.text = `${completed}/${total} · ${elapsed}s · ETA ${etaSec}s · ${task.side === "prod" ? chalk48.cyan("prod") : chalk48.magenta("cand")} ${task.path} (${task.viewport})`;
127166
+ progress.text = `${completed}/${total} · ${elapsed}s · ETA ${etaSec}s · ${task.side === "prod" ? chalk50.cyan("prod") : chalk50.magenta("cand")} ${task.path} (${task.viewport})`;
126262
127167
  }
126263
127168
  });
126264
127169
  progress.succeed(`${total} capture(s) em ${((Date.now() - t0) / 1000).toFixed(1)}s`);
126265
127170
  if (opts.lighthouse !== false) {
126266
- const lhDir = join55(paths.runDir, "lighthouse");
127171
+ const lhDir = join56(paths.runDir, "lighthouse");
126267
127172
  const lhCap = opts.lighthouseConcurrency ?? Math.max(1, Math.min(2, Math.floor(availableParallelism() / 2)));
126268
127173
  const done = tasks.filter((t) => !!t.capture);
126269
127174
  const lhT0 = Date.now();
@@ -126287,7 +127192,7 @@ async function vitalsCommand(opts) {
126287
127192
  }
126288
127193
  lhCompleted++;
126289
127194
  const elapsed = ((Date.now() - lhT0) / 1000).toFixed(0);
126290
- lhProgress.text = `Lighthouse ${lhCompleted}/${done.length} · ${elapsed}s · ${task.side === "prod" ? chalk48.cyan("prod") : chalk48.magenta("cand")} ${task.path} (${task.viewport})`;
127195
+ lhProgress.text = `Lighthouse ${lhCompleted}/${done.length} · ${elapsed}s · ${task.side === "prod" ? chalk50.cyan("prod") : chalk50.magenta("cand")} ${task.path} (${task.viewport})`;
126291
127196
  });
126292
127197
  lhProgress.succeed(`Lighthouse ${done.length} página(s) em ${((Date.now() - lhT0) / 1000).toFixed(1)}s`);
126293
127198
  }
@@ -126352,7 +127257,7 @@ async function vitalsCommand(opts) {
126352
127257
  writeRunReportJson(paths.runDir, run);
126353
127258
  const html = renderHtmlReport(run, paths.runDir);
126354
127259
  writeRunReportHtml(paths.runDir, html);
126355
- writeFileSync39(join55(paths.runDir, "vitals.json"), `${JSON.stringify({
127260
+ writeFileSync40(join56(paths.runDir, "vitals.json"), `${JSON.stringify({
126356
127261
  schemaVersion: "0.1",
126357
127262
  prodUrl: opts.prod,
126358
127263
  candUrl: opts.cand,
@@ -126365,8 +127270,8 @@ async function vitalsCommand(opts) {
126365
127270
  `, "utf8");
126366
127271
  printSummary9(vitalsResult, cacheResult, scoresResult, oppsResult, agenticResult);
126367
127272
  console.log("");
126368
- console.log(chalk48.dim(` → ${paths.reportHtml}`));
126369
- console.log(chalk48.dim(` \uD83D\uDCA1 use 'parity serve ${runId}' pra preview iframe`));
127273
+ console.log(chalk50.dim(` → ${paths.reportHtml}`));
127274
+ console.log(chalk50.dim(` \uD83D\uDCA1 use 'parity serve ${runId}' pra preview iframe`));
126370
127275
  console.log("");
126371
127276
  if (opts.open) {
126372
127277
  const { default: open } = await import("open");
@@ -126458,15 +127363,15 @@ async function discoverPagePaths2(prodUrl, opts) {
126458
127363
  function parseUrlList(input) {
126459
127364
  if (input.startsWith("file:") || input.endsWith(".txt") || input.endsWith(".list")) {
126460
127365
  const path = input.replace(/^file:\/\//, "");
126461
- if (existsSync42(path)) {
126462
- return readFileSync47(path, "utf8").split(/\r?\n/).map((s) => s.trim()).filter((s) => s && !s.startsWith("#")).map((s) => s.startsWith("/") ? s : `/${s}`);
127366
+ if (existsSync43(path)) {
127367
+ return readFileSync49(path, "utf8").split(/\r?\n/).map((s) => s.trim()).filter((s) => s && !s.startsWith("#")).map((s) => s.startsWith("/") ? s : `/${s}`);
126463
127368
  }
126464
127369
  }
126465
127370
  return input.split(",").map((s) => s.trim()).filter(Boolean).map((s) => s.startsWith("/") || s.startsWith("http") ? s : `/${s}`);
126466
127371
  }
126467
127372
  function printSummary9(vitals, cache, scores, opportunities, agentic) {
126468
127373
  console.log("");
126469
- console.log(chalk48.bold(" Summary:"));
127374
+ console.log(chalk50.bold(" Summary:"));
126470
127375
  console.log(` ${vitals.summary}`);
126471
127376
  console.log(` ${cache.summary}`);
126472
127377
  if (scores.status !== "skipped")
@@ -126530,10 +127435,10 @@ sweep.addCommand(new Command("regress").description("Run only the store's tests/
126530
127435
  if (opts.json)
126531
127436
  console.log(JSON.stringify({ url: opts.url, issues }));
126532
127437
  else if (!issues.length)
126533
- console.log(chalk49.green(" regression suite: all passed"));
127438
+ console.log(chalk51.green(" regression suite: all passed"));
126534
127439
  else
126535
127440
  for (const i of issues)
126536
- console.log((i.inconclusive ? chalk49.yellow : chalk49.red)(` ${i.summary}`));
127441
+ console.log((i.inconclusive ? chalk51.yellow : chalk51.red)(` ${i.summary}`));
126537
127442
  process.exit(regressed ? 1 : unverified ? 2 : 0);
126538
127443
  }));
126539
127444
  sweep.addCommand(new Command("file").description("File an existing sweep run's findings on the Studio board (default: the newest sweep run). Merges <run>/sweeper-issues.json written by the `sweeper` agent. Dry run unless --yes.").option("--run <runId>", "Sweep run id (default: newest sweep in --output)").option("--output <dir>", "Output directory", "./parity-output").option("--board <name>", "Board: studio", "studio").option("--yes", "Actually create/update cards", false).option("--allow-automation", "File even if the board's triage column starts an agent", false).option("--min-severity <sev>", "critical|high|medium|low", "medium").option("--repo <owner/name>", "Repository to put on the cards").option("--artifacts-url <url>", "Public base URL for this run's artifacts").option("--json", "Emit JSON", false).action(async (opts) => {
@@ -126887,6 +127792,12 @@ program.command("drift").description("How far has the SOURCE repo moved since th
126887
127792
  json: opts.json
126888
127793
  }));
126889
127794
  });
127795
+ program.command("capacity").description("May one more heavy agent (dev server + headless Chromium + a gate run) start on this machine now, and how many more? Measures cores, 1-minute load, available memory, swap and free disk, then slots = min(--max, memory, cpu, swap, disk) floored at 0. The load already includes running agents, so --running is echoed, never subtracted: re-run before each start. Read-only. Exits 0 admit, 1 no headroom, 2 could not measure enough to decide (an unreadable input caps at 1, never counts as headroom).").option("--dir <path>", "Directory whose volume's free disk is measured", ".").option("--running <n>", "Agents the caller already has running (echoed back; not subtracted)").option("--max <n>", "Ceiling on parallel agents (default 5)").option("--per-agent-gb <n>", "Memory one agent needs, GB (default 1.5)").option("--reserve-gb <n>", "Memory never handed to agents, GB (default 2)").option("--per-agent-cores <n>", "Cores one agent keeps busy (default 2)").option("--reserve-cores <n>", "Cores never handed to agents (default 0)").option("--swap-threshold <ratio>", "Swap used/total above which the swap cap applies (default 0.75)").option("--swap-cap <n>", "Slots allowed while swap is above the threshold (default 2)").option("--per-agent-disk-gb <n>", "Disk one agent needs, GB (default 1)").option("--reserve-disk-gb <n>", "Disk never handed to agents, GB (default 3)").option("--json", "Emit one-line JSON instead of text", false).action((opts) => {
127796
+ process.exit(capacityCommand(opts));
127797
+ });
127798
+ program.command("gate").description("Fast pre-commit regression gate: did this change break a page that was fine at the last accepted state? Loads each page x viewport once (one browser, sequential, primed) and compares status, server HTML size, NEW section-crash / hydration / uncaught errors, page height after the scroll, interactive element count, server-rendered first-viewport sections and click-blocking overlays against `<dir>/gate-baseline.json`, a baseline of the candidate itself (NOT prod: that is `parity run`). No flows, no pixel diff, no vitals: it judges a page as it loads, so an interaction that breaks (a drawer that opens and ignores clicks) is not caught. Exits 0 no regression with at least one page compared, 1 a regression, 2 could not judge (cold or degraded candidate, no pages, no baseline, nothing compared) -- never 0 on an empty comparison. --update-baseline accepts the current state, and refuses when any page is unjudgeable.").requiredOption("--cand <url>", "Candidate base URL").option("--pages <list>", "Comma-separated paths (default: --golden's pages, else the baseline's)").option("--pages-file <path>", "File of paths, one per line").option("--golden <dir>", "A --record recording: its pages, and prod's page height so a change toward prod is an improvement").option("--viewports <list>", "mobile,tablet,desktop", "mobile,desktop").option("--dir <path>", "Directory holding gate-baseline.json (the target's .parity)", ".parity").option("--only <list>", "Restrict to these paths (the pages a change touched)").option("--update-baseline", "Accept the current state as the baseline (merges; refuses if any page is unjudgeable)", false).option("--json", "Emit one-line JSON instead of text", false).action(async (opts) => {
127799
+ process.exit(await gateCommand(opts));
127800
+ });
126890
127801
  program.command("variance").description("Measure whether a score change is attributable to the code. Run `parity run` twice against UNCHANGED code, then pass both run ids: this reports the score spread and, more usefully, WHICH checks reported differently. A check whose status flips between two runs of the same code is not measuring the code, and every score it contributes to is unfalsifiable until it is named. Exits 1 when unstable, 2 when the runs are not comparable.").argument("<runIds...>", "Two or more saved run ids of the same host pair").option("--score-budget <points>", "Score spread tolerated before calling it unstable", "1").option("--output <dir>", "Output directory holding the runs", "./parity-output").option("--json", "Emit one-line JSON instead of pretty text", false).action((runIds, opts) => {
126891
127802
  process.exit(varianceCommand({
126892
127803
  runIds,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/parity",
3
- "version": "0.35.34",
3
+ "version": "0.35.36",
4
4
  "description": "E2E parity validator for site migrations. Compares prod vs cand and reports UI, functional, SEO, visual, and Web Vitals deltas with a ranked HTML report.",
5
5
  "type": "module",
6
6
  "license": "MIT",