// Keeps plate-check-steps.jsonl in the clone's git common directory: one line // for each step a `pnpm check` run selected, with its seconds and exit when it // ran or the pass that covered it. Run it to print what each step costs: // node tooling/scripts/check-steps.mjs [--days ] [--log ] // With no run in the log yet, it reads the passes the check cache recorded. import { spawnSync } from 'node:child_process'; import { appendFileSync, existsSync, readdirSync, readFileSync, realpathSync, statSync, } from 'node:fs'; import { basename, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; export const STEP_LOG = 'plate-check-steps.jsonl'; export const INTERRUPTED = 'interrupted'; const CACHE = join('plate-check', 'v2'); const DAY_MS = 86_400_000; const PNPM_SHIM_NODE_PATH_RE = /export NODE_PATH="([^":]+)\/node_modules\/\.pnpm\//u; const binShimsMatchRoot = (root) => { const binDir = join(root, 'node_modules', '.bin'); if (!existsSync(binDir)) return true; const expectedRoots = new Set([resolve(root), realpathSync(root)]); for (const entry of readdirSync(binDir, { withFileTypes: true })) { if (!entry.isFile() || /\.(?:cmd|ps1)$/u.test(entry.name)) continue; const shimPath = join(binDir, entry.name); if (statSync(shimPath).size > 8192) continue; const match = PNPM_SHIM_NODE_PATH_RE.exec(readFileSync(shimPath, 'utf-8')); if (!match) continue; return expectedRoots.has(match[1]); } return true; }; /** * Whether node_modules holds `root`'s pnpm-lock.yaml and its pnpm `.bin` shims * still point at `root`; null when pnpm never installed there. */ export const lockfileInstalled = (root) => { const installed = join(root, 'node_modules', '.pnpm', 'lock.yaml'); if (!existsSync(installed)) return null; if ( readFileSync(installed, 'utf-8') !== readFileSync(join(root, 'pnpm-lock.yaml'), 'utf-8') ) { return false; } return binShimsMatchRoot(root); }; /** * Installs `root`'s pnpm-lock.yaml, from the store first and then from the * network, and returns null, or pnpm's output when both installs fail. */ export const installLockfile = (root, { ignoreWorkspace = false } = {}) => { let output = ''; for (const offline of [true, false]) { const result = spawnSync( 'pnpm', [ 'install', '--frozen-lockfile', ...(ignoreWorkspace ? ['--ignore-workspace'] : []), ...(offline ? ['--offline'] : []), ], { cwd: root, encoding: 'utf-8', maxBuffer: 64 << 20 } ); if (result.status === 0) return null; output = result.error?.message ?? `${result.stdout}${result.stderr}`; if (output.includes('ERR_PNPM_OUTDATED_LOCKFILE')) break; } return output; }; const STANDALONE_SKIP = new Set([ 'apps', 'node_modules', 'packages', 'private-apps', 'templates', ]); /** * Top-level package directories outside the root workspace that carry their own * pnpm-lock.yaml and need an isolated `--ignore-workspace` install. */ export const standaloneLockfileDirs = (root) => readdirSync(root, { withFileTypes: true }) .filter( (entry) => entry.isDirectory() && !entry.name.startsWith('.') && !STANDALONE_SKIP.has(entry.name) && existsSync(join(root, entry.name, 'package.json')) && existsSync(join(root, entry.name, 'pnpm-lock.yaml')) ) .map((entry) => join(root, entry.name)) .sort((a, b) => a.localeCompare(b)); /** * Installs any missing or stale standalone lockfiles under `root`. * Returns `{ dir, output }` on the first failure, or null when all succeed. */ export const ensureStandaloneLockfiles = ( root, { dirs = standaloneLockfileDirs(root), onInstall } = {} ) => { for (const dir of dirs) { if (lockfileInstalled(dir) === true) continue; onInstall?.(dir); const output = installLockfile(dir, { ignoreWorkspace: true }); if (output !== null) return { dir, output }; } return null; }; export const commonDir = (root) => { const result = spawnSync( 'git', ['rev-parse', '--path-format=absolute', '--git-common-dir'], { cwd: root, encoding: 'utf-8' } ); return result.status === 0 ? result.stdout.trim() : null; }; /** Returns a writer that appends one line per step to the clone's step log. */ export const stepLogger = (root, fields) => { const common = commonDir(root); if (!common) return () => {}; const head = spawnSync('git', ['rev-parse', 'HEAD'], { cwd: root, encoding: 'utf-8', }); const base = { commit: head.stdout.trim(), run: process.env.PLATE_CHECK_RUN ?? null, worktree: basename(root), ...fields, }; return (line) => appendFileSync( join(common, STEP_LOG), `${JSON.stringify({ at: new Date().toISOString(), ...base, ...line })}\n` ); }; // Every run of every worktree appends to one log, so a torn line is skipped // instead of failing the check that reads it. const readLines = (file) => existsSync(file) ? readFileSync(file, 'utf-8') .split('\n') .flatMap((line) => { try { return line ? [JSON.parse(line)] : []; } catch { return []; } }) : []; const cachedPasses = (common) => { const store = join(common, CACHE); if (!existsSync(store)) return []; const passes = []; for (const key of readdirSync(store)) { if (key.startsWith('.')) continue; for (const name of readdirSync(join(store, key))) { const record = JSON.parse(readFileSync(join(store, key, name), 'utf-8')); passes.push({ at: record.at, exit: 0, seconds: record.seconds, step: record.name, }); } } return passes; }; const quantile = (sorted, share) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * share))]; /** Each step's runs, median and 90th percentile seconds, failures and covers. */ export const summarize = (lines) => { const byStep = new Map(); for (const line of lines) { if (!byStep.has(line.step)) byStep.set(line.step, []); byStep.get(line.step).push(line); } return [...byStep] .map(([step, entries]) => { const ran = entries.filter((entry) => entry.seconds !== undefined); const seconds = ran.map((entry) => entry.seconds).sort((a, b) => a - b); return { step, runs: ran.length, p50: ran.length > 0 ? quantile(seconds, 0.5) : null, p90: ran.length > 0 ? quantile(seconds, 0.9) : null, failed: ran.filter((entry) => entry.exit !== 0).length, covered: entries.length - ran.length, }; }) .sort((a, b) => (b.p90 ?? -1) - (a.p90 ?? -1)); }; /** Each logged step's median seconds over the last `days` of `root`'s clone. */ export const typicalSeconds = (root, days = 14) => { const common = commonDir(root); if (!common) return new Map(); const since = Date.now() - days * DAY_MS; const lines = readLines(join(common, STEP_LOG)).filter( (line) => Date.parse(line.at) >= since ); return new Map( summarize(lines) .filter((row) => row.p50 !== null) .map((row) => [row.step, row.p50]) ); }; const main = (argv) => { const option = (name) => { const at = argv.indexOf(name); return at === -1 ? undefined : argv[at + 1]; }; const days = Number(option('--days') ?? 14); const common = commonDir(process.cwd()); const log = option('--log') ?? (common && join(common, STEP_LOG)); if (!log || !Number.isFinite(days)) { console.error( 'Usage: node tooling/scripts/check-steps.mjs [--days ] [--log ]' ); return 1; } const since = Date.now() - days * DAY_MS; const recent = (lines) => lines.filter((line) => Date.parse(line.at) >= since); let lines = recent(readLines(log)); let source = log; if (!lines.some((line) => line.seconds !== undefined) && !option('--log')) { lines = recent(cachedPasses(common)); source = `${join(common, CACHE)} (passes only)`; } console.info(`# ${source}, last ${days} days`); console.info('step\truns\tp50s\tp90s\tfailed\tcovered'); for (const row of summarize(lines)) { console.info( [row.step, row.runs, row.p50, row.p90, row.failed, row.covered] .map((cell) => cell ?? '-') .join('\t') ); } return 0; }; if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { process.exitCode = main(process.argv.slice(2)); }