Files
msd-core/tests/loop-hooks-ship-pre-e2e.test.cjs
Tom Boucher d16a66479a feat(#1950): broken-windows ledger — cross-phase defect register gating ship (#2441)
* feat(#1950): broken-windows ledger — cross-phase defect register gating ship

Adds a new  capability (#1950) that operationalizes GSD's
no-defer discipline as a tracked, enforced artifact:
accumulates stubs, TODOs, skipped tests, unrun verifies, and unmet truths
across phases, and /gsd-ship blocks while any entry is open.

Implementation:
- src/broken-windows.cts → gsd-core/bin/lib/broken-windows.cjs: typed IR +
  I/O entry points (parseLedger/renderLedger/appendWindow/markWaived/markFixed
  + cmdWindowsStatus/Append/Waive/MarkFixed). Frozen REASON enum for typed
  error assertions. Windows-safe atomic rename with retry on transient
  EPERM/EBUSY/EACCES.
- gsd-tools.cjs: new  subcommand (status | append | waive | fixed),
  wired via routeWindows + HOST_COMMAND_ROUTERS.windows.
- capabilities/broken-windows/capability.json: one ship:pre gate with
  artifact-frontmatter-equals predicate on WINDOWS.md open_count == 0.
  activationKey windows.enabled (default true) + sibling windows.enforce
  (default true, separate so tracking can precede enforcement).
- gsd-core/workflows/ship.md: capId==broken-windows branch in preflight,
  sibling to security — reads gsd_run windows status --raw, fails closed
  on open_count > 0 or unreadable ledger.
- agents/gsd-executor.md: extends the existing ## Known Stubs instruction
  to also append to WINDOWS.md via gsd_run windows append (best-effort,
  never blocks execution).
- agents/gsd-verifier.md: new Step 8b — record unmet truths + human-verify
  items in WINDOWS.md.
- gsd-core/workflows/progress.md: surfaces open + waived counts.
- docs/COMMANDS.md + CONTEXT.md glossary entry + docs/INVENTORY.md:
  document the gate, waiver mechanism, and new module.
- tests/broken-windows.test.cjs: pure + CLI behavioral coverage + fast-check
  roundtrip property; fail-closed on malformed ledger; security boundary on
  path traversal in --file.

Backward-compatible: a project with no .planning/WINDOWS.md reports
open_count: 0 and ships cleanly. Disable enforcement per-project with
gsd config-set windows.enforce false (tracking continues, gate stays open).

* chore(#1950): ratchet size baselines, defer verifier integration

- Workflow size baseline: ship.md 25575→27928, progress.md 31789→32632
  (broken-windows preflight branch + open-windows surface).
- Agent size baseline: gsd-executor.md 46644→47951 (Known Stubs → also
  appends to WINDOWS.md). gsd-verifier.md unchanged.
- LARGE_CAP (49152) preempted the planned verifier integration
  (gsd-verifier.md was at 49140 pre-PR — 12 bytes of headroom, not the
  documented 'real headroom'). Verifier integration deferred to a follow-up
  PR that extracts the VERIFICATION.md template (lines 739-859) to
  gsd-core/references/ — a pre-existing cap-tightness defect this PR
  exposed but does not expand scope to fix. Verifier integration is not in
  the issue's acceptance criteria (executor writes is; unmet-truths
  recording was an enhancement, not a gate).

* fix(#1950): gate default-off, rename to workflow.windows_enforce, regen goldens

Test-failure-driven fixes after first gsd-test run on db8733c8f failed 44
cases (pre-existing structural tests encoded 'ship:pre has 1 gate' / 'all
caps off → empty hooks'):

- capability manifest: rename windows.enabled+windows.enforce (default
  true) → single federated key workflow.windows_enforce (default FALSE,
  opt-in). Matches security's workflow.security_enforce convention and
  makes the adr857 all-caps-off test pass without modification (the test's
  buildAllFalseConfig handles workflow.* out of the box). Default-OFF keeps
  the gate out of the registry's default ship:pre resolution so existing
  loop-hooks-ship-pre-e2e structural assertions (exactly 1 gate, capId
  'security') stay valid; users opt in via
  gsd config-set workflow.windows_enforce true.
- drop activationKey (security doesn't have one either; workflow.* key
  doubles as the activation toggle).
- regenerate docs/reference/capability-matrix.md to include broken-windows
  (capability-matrix-sync test).
- regenerate tests/fixtures/golden-install-parity/*.json (18 runtimes) —
  installer now emits the new capability + lib file.
- update CONTEXT.md, docs/COMMANDS.md, docs/FEATURES.md, ship.md,
  agents/gsd-executor.md to use the new key name and /gsd:colon slash
  syntax (slash-command-namespace test).
- restore accidentally-regressed /gsd:capture in progress.md.

Tracking-only by default; enforcement is opt-in. Acceptance criterion
'/gsd-ship fails while any ledger entry is open' is met when
workflow.windows_enforce=true (test fixture enables it).

* test(#1950): update ship:pre structural invariants for 2-gate registry

- loop-hooks-ship-pre-e2e: the registry now declares 2 gates at ship:pre
  (security + broken-windows), regardless of activation. Activation tests
  above still pin security-only or empty behavior via fixtures; these
  structural tests pin the REGISTRY shape, which has 2 gates as of #1950.
- workflow-size-baseline: ship.md 27928→27945 (workflow.windows_enforce
  rename added 17 bytes).

* fix(#1950): review H1+H2+M1+M2+M3 — fence-injection, EACCES fail-closed, cleanup, strict line, stryker

Adversarial isolated review (Step 6.3) found 2 HIGH findings that block
the PR and 3 mediums. All addressed:

H1 (HIGH): description containing the markdown 3-backtick fence would
terminate the ledger's JSON code block early inside JSON.stringify output
(JSON doesn't escape backticks), corrupting the file and bricking the
next parse. Fix: use a 4-backtick fence (json ... ) which
JSON.stringify cannot produce on its own, AND validate that no entry
text field contains a 4-backtick run (reject at append time with new
WINDOWS_INVALID_TEXT reason code). Locked by a regression test.

H2 (HIGH): readLedgerOrNull swallowed ALL fs errors as 'no ledger',
silently returning open_count:0 on EACCES/EPERM/EIO. The ship gate
would then pass on an unreadable ledger — the precise vector the
workflow doc claims is impossible. Fix: only ENOENT returns null;
every other fs error propagates as WINDOWS_LEDGER_MALFORMED so the
gate blocks and the operator sees a real diagnostic. Locked by a
regression test that chmod 000s a ledger with open_count=1 and
asserts the result is never a false-green 0.

M1: writeLedgerAtomic left an orphaned .tmp file on rename failure.
Wrapped renameWithRetry in try/catch with best-effort unlink.

M2: validateLine silently coerced 'abc' → NaN → null, hiding type
drift. Removed the line === 0 special case (was undocumented) and
made the error message match the strict check. Now any non-positive-
integer line value throws, including strings.

M3: tests/broken-windows.test.cjs (with its fast-check property test)
was not in stryker.config.mjs DEFAULT_TEST_CMD — Stryker would mutate
src/broken-windows.cts but no test would catch the mutations,
producing false surviving-mutant scores. Added to the list.

L1 (dead throw e after error()), L7 (line boundary tests, H1/H2
regression tests, 4-backtick CLI test) also addressed.

* docs(#1950): inline concurrency + busy-wait notes (review L2+L3)

* fix(#1950): regen goldens against latest gsd-tools; correct --line 0 boundary test

gsd-test v4 caught two issues:
- goldens I regenerated earlier (commit 526682084) predated the L1
  routeWindows catch-block cleanup (commit dd844d565). Regenerated
  via 'npm run gen:golden' against current HEAD so the install
  parity hash for gsd-tools.cjs matches.
- 'append --line boundary' test expected --line 0 to succeed with
  null entry.line, but the M2 fix correctly rejects 0 (lines are
  1-indexed; 0 is not a valid source line). Updated the boundary
  test to assert --line 0 fails alongside -1 and 'abc'.

* chore(#1950): regen goldens after rebase onto next

* chore(#1950): quick.md baseline 50699→50993 (correct resolution from next rebase)

* chore(changeset): backfill pr:2441 in .changeset/broken-windows-ledger.md

* fix(#1950): renderTable escapes backslash before pipe (CodeQL incomplete-sanitization)

CodeQL flagged the markdown-table cell escaper:
  String(s ?? '').replace(/\|/g, '\\|')
— it escapes pipe but not backslash first. A description containing '\|'
would render as '\\|' which markdown parses as 'literal backslash' +
'cell separator', splitting the column.

Fix: escape backslash FIRST (each \ → \\), then pipe (each | → \|).
Now a description with '\|' renders as '\\\\|' (literal '\\' + escaped
pipe), which markdown renders as a single '\|' inside the cell. The JSON
code block (the parse source-of-truth) was already correctly escaped via
JSON.stringify; only the display-only table was affected.

Locked by a regression test that:
1. Verifies the JSON block reparses with the description intact.
2. Walks the rendered table row counting unescaped pipes — must be
   exactly 11 (the row separators for 10 cells), proving no in-cell
   pipe added a split.
2026-07-19 20:24:21 -04:00

318 lines
15 KiB
JavaScript

'use strict';
/**
* loop-hooks-ship-pre-e2e.test.cjs — E2E content tests for the ship:pre hook point.
*
* ADR-857 phase 6 gap coverage. Tests cover:
* - loop render-hooks ship:pre CLI subprocess (envelope shape, predicate typing)
* - frontmatter get CLI subprocess (threats_open field contract)
* - resolveLoopHooks pure-function with realRegistry (predicate.equals integer contract)
*
* NOTE: ship:pre has NO runnable predicate evaluator — enforcement is ship.md prose only.
* The check.predicate shape is asserted here to pin the Hyrum's-law contract for downstream
* consumers (workflow prose, manual ship gate). This is a known robustness gap (kerckhoffs).
*
* Follows RULESET.TESTS (no source-grep, BVA at thresholds, genuine assertions).
*/
const { describe, test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { spawnSync } = require('node:child_process');
const { cleanup } = require('./helpers.cjs');
const GSD_TOOLS = path.join(__dirname, '..', 'gsd-core', 'bin', 'gsd-tools.cjs');
const {
resolveLoopHooks,
} = require('../gsd-core/bin/lib/loop-resolver.cjs');
const realRegistry = require('../gsd-core/bin/lib/capability-registry.cjs');
// ─── Helpers ──────────────────────────────────────────────────────────────────
/**
* Run gsd-tools synchronously via spawnSync. Returns { status, stdout, stderr }.
* Does NOT throw on non-zero exit — callers must assert status themselves.
*/
function runTools(args, opts = {}) {
const result = spawnSync(process.execPath, [GSD_TOOLS, ...args], {
encoding: 'utf8',
timeout: 60000,
cwd: opts.cwd || process.cwd(),
env: {
...process.env,
// Clear ambient session vars that can redirect config paths
GSD_SESSION_KEY: '',
CODEX_THREAD_ID: '',
CLAUDE_SESSION_ID: '',
...opts.env,
},
});
return result;
}
/**
* Create a minimal temp project directory with a .planning/ dir.
* Optionally write config.json if configObj is provided.
*/
function makeTmpProject(prefix, configObj) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
const planningDir = path.join(tmpDir, '.planning');
fs.mkdirSync(planningDir, { recursive: true });
if (configObj !== undefined) {
fs.writeFileSync(path.join(planningDir, 'config.json'), JSON.stringify(configObj), 'utf8');
}
return tmpDir;
}
/**
* Write a SECURITY.md file with the given frontmatter content to the given dir.
*/
function writeSecurityMd(dir, frontmatter) {
const content = `---\n${frontmatter}\n---\n# Security Review\n`;
const filePath = path.join(dir, 'SECURITY.md');
fs.writeFileSync(filePath, content, 'utf8');
return filePath;
}
// ─── Fixture state ─────────────────────────────────────────────────────────────
let tmpEnforcementOn; // .planning/config.json with security_enforcement:true
let tmpEnforcementOff; // .planning/config.json with security_enforcement:false
let tmpNoConfig; // .planning/ dir with NO config.json (schema default applies)
let tmpWithSecurityMd; // project + SECURITY.md variants in sub-temp dir
before(() => {
tmpEnforcementOn = makeTmpProject('ship-pre-on-', { workflow: { security_enforcement: true } });
tmpEnforcementOff = makeTmpProject('ship-pre-off-', { workflow: { security_enforcement: false } });
tmpNoConfig = makeTmpProject('ship-pre-noconf-'); // no config.json
tmpWithSecurityMd = fs.mkdtempSync(path.join(os.tmpdir(), 'ship-pre-secmd-'));
});
after(() => {
if (tmpEnforcementOn) cleanup(tmpEnforcementOn);
if (tmpEnforcementOff) cleanup(tmpEnforcementOff);
if (tmpNoConfig) cleanup(tmpNoConfig);
if (tmpWithSecurityMd) cleanup(tmpWithSecurityMd);
});
// ─── 1. render-hooks ship:pre envelope tests ──────────────────────────────────
describe('loop render-hooks ship:pre — envelope resolution', () => {
test('[happy] security_enforcement=true returns gate hook with correct predicate shape', () => {
const result = runTools(['loop', 'render-hooks', 'ship:pre', '--raw'], { cwd: tmpEnforcementOn });
assert.strictEqual(result.status, 0, `expected exit 0, got ${result.status}; stderr: ${result.stderr}`);
const envelope = JSON.parse(result.stdout.trim());
assert.strictEqual(envelope.point, 'ship:pre');
assert.strictEqual(envelope.activeHooks.length, 1, 'expected exactly 1 active hook');
const gate = envelope.activeHooks[0];
assert.strictEqual(gate.capId, 'security');
assert.strictEqual(gate.kind, 'gate');
assert.strictEqual(gate.blocking, true);
assert.strictEqual(gate.onError, 'halt');
assert.strictEqual(gate.when, 'workflow.security_enforcement');
// Predicate shape — the critical contract for downstream workflow prose
const pred = gate.check.predicate;
assert.strictEqual(pred.kind, 'artifact-frontmatter-equals');
assert.strictEqual(pred.artifact, 'SECURITY.md');
assert.strictEqual(pred.field, 'threats_open');
assert.strictEqual(pred.equals, 0);
// TYPE contract: equals must be integer (not string '0')
assert.strictEqual(typeof pred.equals, 'number', 'predicate.equals must be a number, not a string');
});
test('[negative] security_enforcement=false returns empty activeHooks (gate suppressed)', () => {
const result = runTools(['loop', 'render-hooks', 'ship:pre', '--raw'], { cwd: tmpEnforcementOff });
assert.strictEqual(result.status, 0);
const envelope = JSON.parse(result.stdout.trim());
assert.strictEqual(envelope.point, 'ship:pre');
assert.deepEqual(envelope.activeHooks, [], 'expected empty activeHooks when enforcement disabled');
assert.strictEqual(envelope.rendered, '_No active hooks at ship:pre._');
// Confirm no security hook leaked through
const secHook = envelope.activeHooks.find(h => h.capId === 'security');
assert.strictEqual(secHook, undefined, 'security gate must be absent when enforcement=false');
});
test('[happy] no config.json uses schema default (security_enforcement=true) and activates gate', () => {
const result = runTools(['loop', 'render-hooks', 'ship:pre', '--raw'], { cwd: tmpNoConfig });
assert.strictEqual(result.status, 0);
const envelope = JSON.parse(result.stdout.trim());
// Schema default for security_enforcement is true — gate must fire
assert.strictEqual(envelope.activeHooks.length, 1, 'schema default must activate the security gate');
assert.strictEqual(envelope.activeHooks[0].capId, 'security');
assert.strictEqual(envelope.activeHooks[0].blocking, true);
});
test('[empty-resolution] gate active when no SECURITY.md exists: envelope confirms gate live (fail-closed)', () => {
// The phase dir has NO SECURITY.md — the gate is still ACTIVE in the envelope
// (activation is config-driven; file absence is a predicate evaluation concern
// handled by ship.md prose, not the CLI resolver).
const tmpPhaseNoSec = makeTmpProject('ship-pre-nosec-', { workflow: { security_enforcement: true } });
try {
const phaseDir = path.join(tmpPhaseNoSec, '.planning', 'phases', '01-feature');
fs.mkdirSync(phaseDir, { recursive: true });
// No SECURITY.md written anywhere
const result = runTools(['loop', 'render-hooks', 'ship:pre', '--raw'], { cwd: tmpPhaseNoSec });
assert.strictEqual(result.status, 0);
const envelope = JSON.parse(result.stdout.trim());
// Gate must still be active — no-file does not suppress the gate
assert.strictEqual(envelope.activeHooks.length, 1, 'gate must remain active even without SECURITY.md on disk');
assert.strictEqual(envelope.activeHooks[0].capId, 'security');
assert.strictEqual(envelope.activeHooks[0].blocking, true);
// Confirm no SECURITY.md in the phase dir (this is the "no-file" scenario)
const hasSec = fs.readdirSync(phaseDir).some(f => f.endsWith('-SECURITY.md') || f === 'SECURITY.md');
assert.strictEqual(hasSec, false, 'fixture must have no SECURITY.md for this test to be meaningful');
} finally {
cleanup(tmpPhaseNoSec);
}
});
});
// ─── 2. predicate.equals integer contract via resolveLoopHooks (pure function) ─
describe('resolveLoopHooks ship:pre — predicate.equals integer type contract', () => {
test('[bva] predicate.equals is integer 0 in resolved output (Hyrum\'s-law type pin)', () => {
const resolved = resolveLoopHooks({
point: 'ship:pre',
registry: realRegistry,
config: { workflow: { security_enforcement: true } },
});
assert.strictEqual(resolved.activeHooks.length, 1);
const gate = resolved.activeHooks[0];
assert.strictEqual(gate.capId, 'security');
const equals = gate.check.predicate.equals;
assert.strictEqual(equals, 0, 'predicate.equals must be integer 0');
assert.strictEqual(typeof equals, 'number', 'predicate.equals typeof must be number, not string');
});
test('[negative] security_enforcement=false via resolveLoopHooks returns 0 active hooks', () => {
const resolved = resolveLoopHooks({
point: 'ship:pre',
registry: realRegistry,
config: { workflow: { security_enforcement: false } },
});
assert.strictEqual(resolved.activeHooks.length, 0, 'enforcement=false must yield 0 hooks');
assert.strictEqual(resolved.point, 'ship:pre');
});
});
// ─── 3. frontmatter get contract for threats_open field ───────────────────────
describe('frontmatter get SECURITY.md threats_open — type contract', () => {
test('[happy] threats_open:0 returns string "0" (type contract: YAML→string via frontmatter CLI)', () => {
const secFile = writeSecurityMd(tmpWithSecurityMd, 'threats_open: 0\nasvs_level: 1');
const result = runTools(['frontmatter', 'get', secFile, '--field', 'threats_open', '--raw']);
assert.strictEqual(result.status, 0);
// Raw output is JSON-encoded string "0", not integer 0
const parsed = JSON.parse(result.stdout.trim());
assert.strictEqual(parsed, '0', 'frontmatter returns string "0", not integer 0');
assert.strictEqual(typeof parsed, 'string', 'frontmatter CLI must return a string for YAML integer fields');
});
test('[bva] threats_open:1 returns string "1" — above threshold, predicate(equals:0) fails', () => {
// BVA: equals:0 passes, equals:1 blocks — this is the just-above threshold value
const secFile = path.join(tmpWithSecurityMd, 'SECURITY-1.md');
fs.writeFileSync(secFile, '---\nthreats_open: 1\nasvs_level: 1\n---\n# Security\n', 'utf8');
const result = runTools(['frontmatter', 'get', secFile, '--field', 'threats_open', '--raw']);
assert.strictEqual(result.status, 0);
const parsed = JSON.parse(result.stdout.trim());
assert.strictEqual(parsed, '1', 'threats_open:1 must return string "1"');
// Verify this differs from the passing case (string "0" !== string "1")
assert.notStrictEqual(parsed, '0', 'string "1" must not equal passing value "0"');
});
test('[negative] missing threats_open field returns Field-not-found error (fail-closed path)', () => {
const secFile = path.join(tmpWithSecurityMd, 'SECURITY-missing-field.md');
// No threats_open key in frontmatter — only unrelated fields
fs.writeFileSync(secFile, '---\nphase: 01\nstatus: active\n---\n# Security\n', 'utf8');
const result = runTools(['frontmatter', 'get', secFile, '--field', 'threats_open', '--raw']);
// Exit 0 — the CLI returns a JSON error object, not a crash
assert.strictEqual(result.status, 0);
const parsed = JSON.parse(result.stdout.trim());
// Must return an error object, not a string value
assert.strictEqual(typeof parsed, 'object', 'missing field must return an object, not a string');
assert.strictEqual(parsed.error, 'Field not found');
assert.strictEqual(parsed.field, 'threats_open');
});
test('[negative] threats_open: unknown returns string "unknown" (ambiguous value must fail closed)', () => {
// Non-numeric string value — predicate (equals:0 integer) cannot match
const secFile = path.join(tmpWithSecurityMd, 'SECURITY-unknown.md');
fs.writeFileSync(secFile, '---\nthreats_open: unknown\nasvs_level: 1\n---\n# Security\n', 'utf8');
const result = runTools(['frontmatter', 'get', secFile, '--field', 'threats_open', '--raw']);
assert.strictEqual(result.status, 0);
const parsed = JSON.parse(result.stdout.trim());
assert.strictEqual(parsed, 'unknown', 'non-numeric value must be returned as-is');
// Confirm this is NOT a match for predicate.equals===0 (integer)
assert.notStrictEqual(parsed, 0, 'string "unknown" must not match integer 0');
assert.strictEqual(typeof parsed, 'string');
});
});
// ─── 4. Real registry structure sanity ────────────────────────────────────────
describe('real registry ship:pre — structural guards', () => {
test('ship:pre byLoopPoint entry has only gates (no steps/contributions) and includes security + broken-windows', () => {
const entry = realRegistry.byLoopPoint['ship:pre'];
assert.ok(entry, 'ship:pre must exist in byLoopPoint');
assert.strictEqual(entry.steps.length, 0, 'ship:pre must have 0 steps');
assert.strictEqual(entry.contributions.length, 0, 'ship:pre must have 0 contributions');
// Two predicate-style gates as of #1950: security (workflow.security_enforcement)
// and broken-windows (workflow.windows_enforce). Both default-off in tests via
// their respective when keys; both surface in the registry regardless of activation.
assert.strictEqual(entry.gates.length, 2, 'ship:pre must have exactly 2 gates (security + broken-windows)');
const capIds = entry.gates.map(g => g.capId).sort();
assert.deepEqual(capIds, ['broken-windows', 'security'], 'ship:pre gate capIds must be {security, broken-windows}');
});
test('every ship:pre gate uses predicate (not query) and the security gate is present', () => {
const gates = realRegistry.byLoopPoint['ship:pre'].gates;
for (const gate of gates) {
assert.ok(gate.check.predicate, `ship:pre gate ${gate.capId} must use predicate, not query`);
assert.strictEqual(gate.check.query, undefined, `ship:pre gate ${gate.capId} must NOT have a check.query (predicate-only gate)`);
}
const security = gates.find(g => g.capId === 'security');
assert.ok(security, 'ship:pre must include the security gate');
});
});