Files
msd-core/scripts/workflow-size.cjs
Rezolv 74d7bc8239 test(#1074): add additive per-file workflow size baseline guard (PR 1/3) (#1089)
* test(#1074): add additive per-file workflow size baseline guard (PR 1/3)

Introduces a committed per-file size baseline scheme alongside (not replacing)
the existing tier anti-creep tests. Green by construction — the baseline
records current sizes, so both schemes pass side by side during migration.

- scripts/lib/allowlist-ratchet.cjs: add assertFileBaseline (third pure helper,
  same injected-fail style) — per-file growth/shrink/add/remove diff vs baseline.
- scripts/workflow-size.cjs: single source of truth for LF-normalized byte
  counting (#683) + workflow enumeration, shared by the guard and the generator
  so they can never measure differently. Lives in scripts/ root (NOT scripts/lib/)
  because it is dev/CI-only tooling — scripts/lib/ is bundled into the installed
  runtime, scripts/ root is not, so this keeps it out of the shipped payload.
- scripts/update-size-baseline.cjs + npm run size:baseline: regenerate the
  snapshot (sorted keys, trailing newline, idempotent).
- tests/workflow-size-baseline.json: generated snapshot (88 workflows).
- tests/workflow-size-budget.test.cjs: import the shared counter (drops the
  duplicated local byteCount) and add the per-file baseline describe block.
- Tests for the helper, the shared module, and the generator (incl. round-trip
  and fault-injection cases).

Refs #1074. Part 1 of 3; PR 2 swaps enforcement, PR 3 covers the agent test.

* test(#1074): regenerate workflow baseline after Update-branch merge with next

The 'Update branch' merge (652a916b) pulled in next's update.md change (#1090)
without regenerating the snapshot, leaving the per-file baseline stale by one
file. Re-ran `npm run size:baseline` so the committed baseline matches the
merged workflow files.

Refs #1074.

---------

Co-authored-by: Tom Boucher <trekkie@nomorestars.com>
2026-06-11 23:59:56 -04:00

69 lines
2.5 KiB
JavaScript

'use strict';
/**
* @file workflow-size.cjs
*
* Single source of truth for measuring workflow `.md` file sizes in bytes.
*
* Shared by `tests/workflow-size-budget.test.cjs` (the CI guard) and
* `scripts/update-size-baseline.cjs` (the baseline generator) so the two can
* never disagree on HOW a file is measured. A divergence between the generator
* and the guard would silently mis-record the baseline (issue #1074).
*/
const fs = require('fs');
const path = require('path');
const WORKFLOWS_DIR = path.join(__dirname, '..', 'gsd-core', 'workflows');
/**
* Byte size of a file, counted as on an LF (Unix) checkout.
*
* The size budget is calibrated against `wc -c` on a Unix (LF) checkout, but
* these `.md` files have no `eol=lf` in `.gitattributes`, so Windows checks
* them out as CRLF. Counting raw on-disk bytes there adds one byte per line,
* a Windows-only false positive that diverges from the LF calibration basis
* (issue #683). Stripping CR yields the same LF byte count on every platform.
* This is still a raw byte count (not a trailing-newline-stripping line count).
*
* @param {string} filePath - Absolute or relative path to the file.
* @returns {number} LF-normalized byte length.
*/
function lfByteCount(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
return Buffer.byteLength(content.replace(/\r\n/g, '\n'), 'utf-8');
}
/**
* List top-level workflow stems (filenames without the `.md` extension), sorted.
* Non-recursive by design: per-mode bodies under `workflows/<name>/modes/` and
* templates are NOT measured — only the always-loaded top-level workflows.
*
* @param {string} [dir] - Workflows directory (defaults to the canonical one).
* @returns {string[]} Sorted stems, e.g. `['autonomous', 'plan-phase', ...]`.
*/
function listWorkflowStems(dir = WORKFLOWS_DIR) {
return fs
.readdirSync(dir)
.filter((f) => f.endsWith('.md'))
.map((f) => f.replace(/\.md$/, ''))
.sort();
}
/**
* Measure every top-level workflow file, keyed by filename (`<stem>.md`).
*
* @param {string} [dir] - Workflows directory (defaults to the canonical one).
* @returns {Object<string, number>} Map of `<stem>.md` → LF byte size, with
* keys inserted in sorted order.
*/
function measureWorkflows(dir = WORKFLOWS_DIR) {
const out = {};
for (const stem of listWorkflowStems(dir)) {
out[`${stem}.md`] = lfByteCount(path.join(dir, `${stem}.md`));
}
return out;
}
module.exports = { WORKFLOWS_DIR, lfByteCount, listWorkflowStems, measureWorkflows };