Files
msd-core/src/uat-predicate.cts
Tom Boucher 4e8927b0b9 fix(#3707): degrade the fold for every UAT gap class, and stop line endings hiding rows from the audit and the acceptance gate (#3903)
* test(#3707): failing-first coverage for reverting the fence-shortfall fold shield

Pins the post-revert contract: a phase whose only gap is a fence shortfall must
degrade the fold and withhold the milestone percentages, like every other gap class.

Five of the eight rows are CONTROLS that pass before the change, and they carry more
weight than the failing row. The failure mode of this revert is degrading TOO MUCH:
a revert that sets foldScope outside the headingsSeen > 0 branch would withhold every
percentage in the project, and only the no-gap control catches that. Another control
catches a revert that collapses the two scopes into one and loses the distinction
between what a phase reports and what the fold folds -- uat.scope must stay TRUNCATED
for every gap either way, which it already is.

The row that pinned the shielded behavior is rewritten rather than deleted. Deleting
a test because the behavior it asserts is being reversed leaves the reversal
unguarded.

* fix(#3707): degrade the fold for every UAT gap class, reverting the fence-shortfall shield

Maintainer decision. The two orthogonal engines split on this during #3707 and
neither filed it as blocking, so it shipped in the shape the engine that raised the
objection endorsed after verifying seven fixtures. The call has now gone the other
way, restoring the fail-safe direction chosen twice already on this issue.

The shield exempted one gap class from the fold's teeth. It could not do that
safely: shortfallBlocks is a single tally incremented at exactly one site and spans
BOTH a harmless fenced documentation sample AND a genuinely fence-straddled
result: blocked row. Exempting it therefore could not exempt only the harmless case
-- it also published a milestone percentage over a real, unread outstanding row.
SCOPE.TRUNCATED means the scan could not SEE part of the evidence, which is exactly
that case.

scope and foldScope now agree: every gap class degrades both. The accepted
over-report documented in uat.cts is unchanged and still documented there; what
changed is only that it no longer buys an exemption from the fold.

The comment block above it argued FOR the shield and is rewritten, because a
comment defending behavior the code no longer has is worse than no comment.
shortfallBlocks leaves this function's destructure but is untouched upstream, where
audit-uat still consumes it.

* fix(#3707): correct the caller comment, add the changeset, and name what the order tests guard

Review found a SECOND comment still documenting the removed shield -- the caller's,
beside the worstScope fold, stating that foldScope differs from scope for exactly
one case which must not raise phase_scope_degraded or withhold the milestone's
percentages. That is now the opposite of what the code does. I rewrote the
buildUatRows comment in the previous commit and asserted in its message that a
comment defending behavior the code no longer has is worse than no comment, then
left exactly that one standing a few hundred lines away.

The change had no changeset. It is user-visible: a milestone's percentage goes from
published to withheld whenever any phase has a fence-shortfall-only gap. PR gates
hard-fail a user-facing code diff without one.

The two scopes are now identical at every return site. They are NOT collapsed --
that would change the return shape and the caller on what is meant to be a
one-condition revert, and the seam is worth keeping if the distinction is ever
wanted again -- but the declaration now says plainly that they agree by decision
rather than by accident, so a reader does not have to re-derive it.

The two order-independence tests were renamed. foldScope is monotonic with no reset
path, so file order is structurally irrelevant and those rows could never have
failed for the ordering reason their names promised. They do guard something real --
a multi-file phase degrading when any one file has a shortfall-only gap -- so they
now say that instead.

* test(#3707): failing-first coverage for the lone-CR UAT false-clean

The parser splits on newline only, and the heading tokenizer agrees with it, so a
lone carriage return is not a line boundary anywhere in it. CommonMark treats a lone
CR as a line ending, so such a row renders to a human reader while being invisible
to BOTH sides of the parser's symmetry invariant: no item, no shortfall, no
headingsSeen. A phase hiding a result: blocked row this way reports 100 percent with
zero diagnostics.

Found by the security review of the fold-shield revert. It is the one false-clean
class that revert does not reach, and it is the same bug class this issue exists to
fix -- an unreadable row reported as clean.

Nine rows. The LF control is what proves this is a separator defect rather than a
content defect: identical bodies, one separator apart, and only one of them hides
the row. CRLF and CR-inside-a-fence controls guard the coming normalization against
double-counting or tearing content that legitimately contains a carriage return.
Two further manifestations turned up while writing them: a leading CR breaks
column-0 anchoring of the first heading, and an all-CR document flags a shortfall it
cannot attribute to any row.

* fix(#3707): treat a lone carriage return as a line ending in the UAT parser

A lone CR was not a line boundary anywhere in the parser -- it split on newline
only, and the heading tokenizer agreed with it. CommonMark treats a lone CR as a
line ending, so such a row rendered to a human reader while being invisible to BOTH
sides of the parser's symmetry invariant: no item, no shortfall, no headingsSeen. A
phase hiding a result: blocked row that way reported 100 percent with zero
diagnostics.

Line endings are now normalized once at document ingress -- CRLF and lone CR both to
newline -- at the two independent entry points, rather than teaching each split site
about CR. Every downstream scan, offset and span therefore reads one convention.
That single-frame property is deliberate: this issue already cost a HIGH when two
scans read the same document through different frames.

MY OWN END-TO-END TEST WAS WRONG and is replaced rather than weakened. It asserted
that a lone-CR document must withhold its percentage, which reasons from the
pre-fix symptom: after the fix the row is not hidden, it is surfaced, and this
module deliberately keeps visible outstanding UAT work separate from completion
percentages -- only unreadable evidence degrades scope. The success of the fix is
what made the assertion false. The implementing agent refused to satisfy both it and
the architecture and asked instead of bending either; it was right.

What replaces it is a stronger contract: a lone-CR document and its LF twin, built
from one source, must produce identical audit output -- scope, percent, every
unresolved row by identity, and the diagnostic set. That is what 'a line-ending
convention must not change what the audit reports' actually means, and it carries a
non-vacuity check so it cannot pass with both sides empty.

shortfallBlocks keeps being returned, now documented as currently unconsumed. An
earlier reviewer told me audit-uat still consumed it and I passed that on as an
instruction; it was wrong, and it was caught by checking rather than by me.

* fix(#3707): normalize at the document read boundary, not at two call sites

The lone-CR fix was half-applied and both review engines caught it independently.
cmdAuditUat has four document ingresses, not the two I normalized: VERIFICATION.md
and deferred-items.md still handed raw text to newline-only splitters, and the
frontmatter extract in the UAT loop read raw content while its parser read
normalized -- one audit entry mixing the two frames the fix exists to unify.
Measured: a phase written twice from one source gave total_files 2 / total_items 4
under LF and results [] / total_items 0 under lone CR, with zero diagnostics.

Normalizing two call sites and declaring it done is exactly why two were missed, so
this moves it to the read boundary: every document now enters through a helper that
normalizes, in audit-uat, in planning-inspect's readDocument, and in the shared
verification-status read. Future parsers downstream get normalized text by
construction rather than because someone remembered.

That last seam also fixes an under-reporting case of the same root: a lone-CR
VERIFICATION.md saying status: passed was read as missing, telling the user a verify
step that had completed never ran.

The parity test's load-bearing assertion is now marked as such. Four of its five
equality checks still pass with the bug present -- only the unresolved-row identity
differs -- so trimming that one as redundant would make the row vacuous.

Second changeset added: the CR fix is user-visible independently of the fold revert,
and one fragment covering both would have described neither.

* test(#3707): failing-first coverage for the U+2028 and duplicate-result false-cleans

Two more of the same class, both found by the security review of this branch and
both reproduced before writing a line.

normalizeLineEndings folds only carriage returns, but a JS /m anchor also treats
U+2028 and U+2029 as line terminators while split on newline does not. That is the
identical asymmetry the carriage-return bug exploited, one separator over, and worse
in one respect: these are not CommonMark line endings, so a reader still sees the
column-0 result: blocked that the tool discards. Measured: a scalar-internal
result: pass placed after U+2028 wins over the real blocked line and the row
disappears with no gap raised.

Separately, and independent of any separator, a block with two column-0 result:
lines resolves to the first with no ambiguity signalled. Prepending result: pass to
a block therefore deletes an outstanding row silently; reversing the order surfaces
it. Order deciding meaning is the defect, so the pair of rows pins the contract as
ambiguity-is-a-gap rather than last-one-wins, leaving the fix room to implement the
gap sensibly.

Four controls: an ordinary marker in the same position (proving separator not
content), legitimate U+2028 inside prose that must not be torn, a single result line,
and a result line inside a fence that must not count as a second occurrence.

* fix(#3707): scan result lines by split, not by a multiline anchor

Two more false-cleans from the security review, both closed by the same change.

A JS /m anchor treats U+2028 and U+2029 as line terminators while split on newline
does not. A scalar-internal result: pass placed after one of those separators
therefore matched as a line start and beat the real column-0 result: blocked, and
the row vanished at 100 percent with no gap. Worse than the carriage-return case in
one respect: these are not CommonMark line endings, so a reader still saw the
blocked row the tool discarded.

Separately, the non-global match returned the leftmost hit, so a block with two
column-0 result: lines silently resolved to the first. Prepending result: pass
deleted an outstanding row; reversing the order surfaced it. Order deciding meaning
was the defect.

Both close by scanning lines produced by split rather than by anchoring a regex
inside the whole document: each line is tested on its own, and a count other than
exactly one is reported as a parse gap instead of resolved to either candidate.

I asked for U+2028 to be folded in normalizeLineEndings and that was wrong. Folding
is length-preserving, so it would have made the U+2028 fixture byte-identical to the
genuine two-result-line fixture -- while one requires a confident item and the other
requires an ambiguity gap. No implementation can satisfy both once the distinguishing
character is erased. The agent proved that and deviated rather than forcing it, which
is why normalizeLineEndings still folds only carriage returns, now with a comment
saying why.

* fix(#3707): bound the ambiguity scan at the next heading-shaped line

The split-based result scan regressed four pre-existing #3078/#3707 guards, each
off by exactly one gap.

My diagnosis was wrong. I read the off-by-one as double counting -- zero-result
blocks taking both the new path and the pre-existing one -- and said to change the
ambiguity condition from not-equal-one to greater-than-one. The agent checked and
refused: the zero path was never duplicated. The real cause is double ATTRIBUTION.
A block is sliced to the next TOKENIZED heading, so when the next row is untokenized
-- hidden by a straddling fence, or indented and already counted by the shortfall
scan -- that row's own result: line is absorbed into the previous block. The scan
then saw two result lines across what are really two rows and raised a second,
redundant gap on top of the one already counted elsewhere.

Had the greater-than-one change gone in, the counts would have matched while the
double attribution stayed. That is the compensating-adjustment failure I had asked
it to refuse, and it did.

The scan is now bounded at the first following heading-shaped line, either indent
class, so a genuine same-block ambiguity is untouched while spillover from a row
counted elsewhere is excluded.

* fix(#3707): keep the U+2028 immunity, revert the ambiguity detection

The ambiguity half of this change regressed the suite twice and is coming out.

Attempt one double-attributed: a block is sliced to the next TOKENIZED heading, so
when the real next row is untokenized its result: line was absorbed into the
previous block and raised a second gap on a row already counted elsewhere. Four
guards broke.

Attempt two bounded the scan at the next heading-shaped line and broke thirty. An
indented ### N. inside a block scalar is legitimate scalar CONTENT, not a heading,
and truncating there defeats every #3078 guard that exists to stop scalar bodies
being read as rows. Telling a genuinely hidden indented row apart from indented
scalar text is a classification countUnattributedIndentedRows already owns; a raw
regex does not have that information.

What survives is the half that is sound and was never implicated in either
regression: the result scan tests each line produced by split rather than anchoring
a regex with the multiline flag over the whole block. split never treats U+2028 or
U+2029 as a delimiter, so those separators can no longer manufacture a line start
and steal a row. Everything else returns to first-match-wins, byte-identical to
origin/next.

The two tests pinning ambiguity-as-a-gap are removed with it, since the contract is
no longer implemented here. The defect they described is real, pre-existing and
independent of any separator -- result: pass before result: blocked silently deletes
an outstanding row -- and it needs its own change with a scalar-aware counter rather
than being wedged into a branch already carrying three fixes.

* fix(#3707): correct the shared-seam rationale and restore U+2028 trailing text

The revert left a stale rationale in core-utils, justifying the decision not to fold
U+2028 by claiming uat.cts must tell a fake line start apart from a real second
column-0 result: declaration that gets flagged as ambiguous. Nothing flags ambiguity
any more; that behavior was reverted and the same file says so a few lines away. The
decision is still right, the stated reason was false.

This is the third stale comment this branch has shipped and had to fix, and the worst
placed of them: core-utils is a shared leaf that every future document consumer will
read for guidance. Rewritten to the true reason -- the scan tests each split line
individually rather than anchoring over the block, so an exotic separator cannot
manufacture a line start and folding is unnecessary.

Also a real behavior delta I had not noticed. Dropping the multiline flag left the
pattern's trailing .*$ in place, and dot never matches U+2028, so a genuine column-0
result: blocked whose TRAILING text contained one stopped parsing entirely -- a
visible parse gap rather than a false clean, so fail-safe, but a regression against
origin/next that nothing pinned. The trailing portion now matches any character and
a test pins it by identity against its plain-LF twin.

Plus the JSDoc orphaned when normalizeLineEndings moved to core-utils, and the
changeset, which described neither the separator fix nor planning-inspect surfacing
lone-CR rows.

* fix(#3707): harden the acceptance gate, which had both halves of the same bug

uat-predicate is a SECOND, independent UAT parser, and it is the one that decides
phase uat-passed. It read raw bytes and anchored a multiline regex over unsplit
text -- exactly the two defects this branch closed one module away in uat.cts.

The consequence is worse than the audit surface it mirrors. Measured on identical
bytes: a U+2028 scalar injection made the gate return passed true while planning
inspect reported the same row as blocked and outstanding. The hardened surface and
the gate disagreed, and the gate was the permissive one -- so a phase could be
accepted over a row the audit could see and the gate could not.

Both raw reads now go through the shared normalize seam and both scans test lines
produced by split rather than anchoring over the document. First-match-wins,
matching uat.cts; no ambiguity counting is reintroduced. Tests assert the AGREEMENT
between the two surfaces rather than each separately, because divergence is the
defect.

Also finishes the same root cause one module over: phase complete's advisory
pre-scan read raw bytes, so a lone-CR VERIFICATION.md lost its human_needed or
gaps_found warning -- the fix verification.cts already got on this branch.

And narrows the core-utils rationale I reworded last commit, which claimed consumers
already avoid multiline anchors. uat.cts still has five over unsplit text. That is
the fourth comment on this branch to assert something the code does not do, so it
now states only what is true of core-utils itself.

* fix(#3707): give structure and attribution different line frames, normalize the close audit

Two more from review, and the first was a regression I introduced one commit
earlier.

Converting the gate's heading scan to split-then-match removed a detection
origin/next had: a ### N. heading delimited by U+2028 was found by the old multiline
scan and was not found after. So hardening the result scan quietly weakened the
heading scan, and the gate stopped blocking on rows origin/next blocked -- the
permissive direction, on the surface that decides acceptance.

The insight I had missed is that the two scans need DIFFERENT frames. Heading
detection is structure: there is no distinction to preserve, so it splits on newline
or either exotic separator and finds a heading however it is delimited. The result
scan is attribution: the newline-only frame is exactly what stops a scalar-internal
result: from being read as a column-0 line, so it stays. One frame applied uniformly
was the error.

Second, a THIRD unnormalized parser family: the milestone-close audit read every
artifact raw. A lone-CR VERIFICATION.md degraded to status unknown and was skipped,
and deferred entries vanished outright -- measured as three items requiring
decisions under LF and one under CR, on identical bytes. All nine scanner reads now
normalize; six of them had the identical defect beyond the three review named. The
acknowledge path stays deliberately raw, since it splices by byte offset, and now
says so.

Also pins the cross-newline result: divergence, and replaces three raw U+2028
literals in test source with escapes. A raw separator in a fixture is one formatter
away from becoming an ordinary-character control that still passes -- vacuous in the
only test pinning the separator fix.

* fix(#3707): share one frame between the acknowledge writer and the audit reader

Normalizing the audit scanners left the writer and the reader on different frames.
cmdAuditAcknowledge derives its stored snapshot values from raw content -- correct
for the SPLICE, which rewrites by byte offset -- but scanUatGaps and
scanContextQuestions now recompute those same values from normalized content. For a
lone-CR artifact the two can never match, so an acknowledgement never suppresses its
item and it resurfaces on every audit: acknowledge became a silent no-op.

Fail-safe in direction, since the item stays visible rather than being wrongly
suppressed, but it is the writer and reader disagreeing about what a line is -- the
exact class this branch exists to eliminate, and the fourth instance of it here.

The derive functions now read a normalized copy while the splice keeps raw bytes and
raw offsets, so both sides share one frame and the byte-offset rewrite is untouched.
Round trip pinned for lone-CR and LF, with an existing LF marker asserted still
recognised so the change cannot silently invalidate acknowledgements already in
users' files.

Also tightens an assertion that pinned this branch's own heading fix with a proxy:
notStrictEqual against 'passed' also passes on 'pass', which IS a passing token, so
it could not have caught a regression attributing a passing result to the recovered
heading. It now pins the exact token.

* chore(#3707): backfill changeset pr numbers

Both fragments still carried the pr: 0 placeholder, which failed changeset-lint and
docs-lint on PR 3903. The review had flagged the backfill as pending and I opened
the PR without doing it.

---------

Co-authored-by: sim <sim@local>
2026-08-26 20:17:35 -04:00

422 lines
18 KiB
TypeScript

/**
* UAT Predicate — Pure-computation UAT pass/fail evaluation
*
* Evaluates all *-UAT.md and *-VERIFICATION.md files in a phase directory and
* returns a typed report. Used by `phase uat-passed` to harden against the
* naive whole-file regex in cmdPhaseComplete which false-matches `result:` lines
* inside frontmatter, fenced code blocks, blockquotes, and HTML comments.
*
* Issue #247 — phase uat-passed predicate
*
* ADR-457 build-at-publish: compiled by tsc to gsd-core/bin/lib/uat-predicate.cjs.
*/
import fs from 'node:fs';
import path from 'node:path';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import frontmatter = require('./frontmatter.cjs');
const { extractFrontmatter } = frontmatter;
// eslint-disable-next-line @typescript-eslint/no-require-imports
import markdownSectionizer = require('./markdown-sectionizer.cjs');
const { stripFencedCode } = markdownSectionizer;
// eslint-disable-next-line @typescript-eslint/no-require-imports
import verification = require('./verification.cjs');
const { readVerificationStatus } = verification;
// eslint-disable-next-line @typescript-eslint/no-require-imports
import phaseIdMod = require('./phase-id.cjs');
const { scopeToPhase } = phaseIdMod;
// eslint-disable-next-line @typescript-eslint/no-require-imports
import coreUtils = require('./core-utils.cjs');
const { normalizeLineEndings } = coreUtils;
// ─── Types ────────────────────────────────────────────────────────────────────
interface UatCheckItem {
file: string;
test: number;
name: string;
result: string;
passing: boolean;
}
interface UatPassedReport {
passed: boolean;
uat_files: string[];
verification_files: string[];
checks: UatCheckItem[];
blockers: string[];
no_uat_artifacts: boolean;
policy: {
require_verification: boolean;
};
/**
* #3057 B3: true when the `requireVerification` policy check's own
* readVerificationStatus call could not complete its internal staleness
* check (fs / scanPhasePlans / clock failure). `passed`/`blockers` above are
* UNCHANGED by this — the pre-existing fail-open routing (indeterminate
* treated as not-stale) is preserved — this only makes the fact visible to
* cmdPhaseUatPassed's JSON output (which spreads this whole report), rather
* than silently indistinguishable from "checked; nothing is stale". Always
* false when `requireVerification` is not set (the check is never reached).
*/
verification_stale_check_indeterminate: boolean;
}
// ─── Blocking state sets (documented for maintainability) ─────────────────────
// UAT file frontmatter `status` values that indicate the file is not fully done
const BLOCKING_UAT_FM_STATUSES = new Set([
'partial', 'diagnosed', 'pending', 'blocked', 'in_progress', 'failed',
]);
// UAT file frontmatter `result` values that indicate failure
const BLOCKING_UAT_FM_RESULTS = new Set(['pending', 'blocked', 'failed']);
// Canonical VERIFICATION frontmatter `status` value that indicates passing.
const PASSING_VERIFICATION_STATUSES = new Set(['passed']);
// VERIFICATION file frontmatter `status` values that explicitly block
const BLOCKING_VERIFICATION_FM_STATUSES = new Set([
'human_needed', 'gaps_found', 'pending', 'blocked', 'partial',
'failed', 'in_progress',
]);
// UAT test-item `result` values that count as passing
const PASSING_RESULTS = new Set(['passed', 'pass']);
// ─── stripFalsePositiveContexts ───────────────────────────────────────────────
/**
* Remove contexts that can contain `result: ...` lines that are NOT real test results:
* (a) leading frontmatter block at byte 0
* (b) HTML comments (unterminated comments swallow to EOF — fail-closed)
* (c) fenced code blocks (backtick and tilde, indented too) via CommonMark state machine
* (d) blockquote lines
*
* Each step is a composable function (Kernighan's Law — independently testable).
* Returns surviving lines joined by '\n'. Robust to CRLF input.
*/
function stripFalsePositiveContexts(content: string): string {
// Step (a): strip leading frontmatter block only at byte 0
let stripped = content.replace(/^---\r?\n[\s\S]*?\r?\n---[ \t]*(\r?\n|$)/, '');
// Step (b): remove HTML comments anywhere; unterminated comment swallows to EOF
stripped = stripped.replace(/<!--[\s\S]*?(?:-->|$)/g, '');
// Step (c): remove fenced code blocks via the canonical seam (ADR-1372 T5)
stripped = stripFencedCode(stripped).text;
// Step (d): remove blockquote lines
stripped = stripped
.split('\n')
.filter(line => !/^\s*>/.test(line))
.join('\n');
return stripped;
}
/**
* Analyse raw markdown for structural anomalies (unterminated fence / comment).
* Exported for unit-testability and used by evaluateUatPassed for per-file malformed detection.
*
* FIX C: properly balanced comments are stripped before checking for a dangling <!--,
* so an earlier closed comment does not mask a later unterminated one.
*/
function analyzeMarkdown(raw: string): { unterminatedFence: boolean; unterminatedComment: boolean } {
// Detect an unterminated HTML comment via a paired scan: every `<!--` must
// have a following `-->`. Using indexOf (not a regex .replace of the comment
// token) avoids the js/incomplete-multi-character-sanitization pattern — and
// is exact: a closed earlier comment never masks a later unterminated one.
let unterminatedComment = false;
for (let i = 0; ; ) {
const open = raw.indexOf('<!--', i);
if (open === -1) break;
const close = raw.indexOf('-->', open + 4);
if (close === -1) { unterminatedComment = true; break; }
i = close + 3;
}
// Fence state machine gives the accurate unterminated-fence signal (seam, ADR-1372 T5).
const { unterminatedFence } = stripFencedCode(raw);
return { unterminatedFence, unterminatedComment };
}
// ─── parseUatResultItems ──────────────────────────────────────────────────────
/**
* HEADING-BLOCK parser: scan the CLEANED body (after stripFalsePositiveContexts)
* for UAT test blocks.
*
* For each ### N. Name heading, the block spans until the next ### heading or EOF.
* Within each block, find a column-0 anchored result line (rejects indented YAML
* block-scalar bodies and inline/quoted fakes).
*
* - If a heading block has NO column-0 result line → emit result:'missing' (blocker).
* - Support bracketed [passed] and bare passed (#2273).
* - Returns ALL items (both passing and non-passing).
*/
function parseUatResultItems(cleanContent: string): Array<{ test: number; name: string; result: string }> {
const items: Array<{ test: number; name: string; result: string }> = [];
// Find all ### N. Name headings.
// #3078-CR MEDIUM (security review follow-up): STRUCTURE and ATTRIBUTION
// need different split frames. This is a STRUCTURE scan — finding where a
// heading block begins — and there is no attribution distinction to
// preserve, so split on any of `\n`, U+2028, U+2029: a heading delimited by
// an exotic line separator (origin/next's `/m`-anchored scan found these;
// a naive `split('\n')`-only port silently stopped finding them, making the
// gate MORE permissive than origin/next) is found exactly like a
// `\n`-delimited one. Contrast the `result:` scan below, which is an
// ATTRIBUTION scan and must NOT do this.
const HEADING_LINE_RE = /^###\s*(\d+)\.\s*(.+)$/;
const headings: Array<{ index: number; lineStart: number; test: number; name: string }> = [];
{
// All three separators are exactly one UTF-16 code unit, so the
// `line.length + 1` offset arithmetic below stays valid regardless of
// which separator terminated a given line.
const lines = cleanContent.split(/[\n\u2028\u2029]/);
let offset = 0;
for (const line of lines) {
const hMatch = line.match(HEADING_LINE_RE);
if (hMatch) {
headings.push({
index: offset + hMatch[0].length,
lineStart: offset,
test: parseInt(hMatch[1], 10),
name: hMatch[2].trim(),
});
}
offset += line.length + 1; // +1 for the separator consumed by split
}
}
for (let i = 0; i < headings.length; i++) {
const h = headings[i];
const blockStart = h.index;
// A block spans until the START of the next heading's line (tracked
// directly from the same split-frame scan above), not a re-search for a
// literal '\n###' over unsplit text -- the latter would silently miss a
// next heading delimited by U+2028/U+2029 instead of '\n' and swallow
// every subsequent block into this one.
const blockContent = i + 1 < headings.length
? cleanContent.slice(blockStart, headings[i + 1].lineStart)
: cleanContent.slice(blockStart);
// Column-0 result line, split-then-match (#3078-CR MEDIUM — same fix as
// the heading scan above): test each already-split line individually
// against a single-line (no `/m` anchor) pattern instead of anchoring
// over unsplit `blockContent`, so a `result:`-shaped line reachable only
// via a U+2028/U+2029 separator inside an `expected: |` scalar body can
// never register as a fake column-0 match. FIRST MATCH WINS — no
// ambiguity counting, matching src/uat.cts's contract.
// Uses [ \t]* (not \s*) so the captured value must sit on the SAME line as result:.
// A result: key with the value on a subsequent line yields no match → 'missing' (blocker).
const RESULT_LINE_RE = /^result:[ \t]*\[?([\w-]+)\]?/i;
const resultMatch = blockContent
.split('\n')
.map((line) => line.match(RESULT_LINE_RE))
.find((m): m is RegExpMatchArray => m !== null) ?? null;
if (resultMatch) {
items.push({
test: h.test,
name: h.name,
result: resultMatch[1].toLowerCase(),
});
} else {
// No column-0 result line → emit 'missing' (a non-passing state)
items.push({
test: h.test,
name: h.name,
result: 'missing',
});
}
}
return items;
}
// ─── evaluateUatPassed ────────────────────────────────────────────────────────
/**
* Evaluate all UAT/VERIFICATION files in a phase directory.
* Returns a UatPassedReport with the locked, stable shape defined by the interface.
*
* FAIL-CLOSED: any absence/ambiguity/malformed input → NOT passed.
* Pass requires at least one real passing check AND no blockers.
*/
function evaluateUatPassed(
phaseFullDir: string,
opts?: { policy?: { requireVerification?: boolean } },
): UatPassedReport {
const requireVerification = opts?.policy?.requireVerification === true;
const blockers: string[] = [];
const checks: UatCheckItem[] = [];
const uatFiles: string[] = [];
const verificationFiles: string[] = [];
// Read the directory — if unreadable, treat as no files (fail-closed: no artifacts → not passed)
let dirEntries: string[] = [];
try {
dirEntries = fs.readdirSync(phaseFullDir);
} catch {
// Unreadable dir — no_uat_artifacts:true, passed:false
const no_uat_artifacts = true;
if (requireVerification) {
blockers.push('policy: verification required but no passing *-VERIFICATION.md found');
}
return {
passed: false,
uat_files: [],
verification_files: [],
checks: [],
blockers,
no_uat_artifacts,
policy: { require_verification: requireVerification },
// readVerificationStatus was never reached on this early-return path.
verification_stale_check_indeterminate: false,
};
}
// Filter UAT and VERIFICATION files using the same filter as cmdPhaseComplete,
// scoped to THIS phase's own token (#3511) — a stray, cross-phase, or ad-hoc
// file can no longer contribute a blocker to a phase it does not belong to.
const phaseDirBaseName = path.basename(phaseFullDir);
const uatFileNames = scopeToPhase(
dirEntries.filter(f => f.includes('-UAT') && f.endsWith('.md')),
phaseDirBaseName,
);
const verFileNames = scopeToPhase(
dirEntries.filter(f => f.includes('-VERIFICATION') && f.endsWith('.md')),
phaseDirBaseName,
);
// ── Process UAT files ──────────────────────────────────────────────────────
for (const file of uatFileNames) {
uatFiles.push(file);
const uatFilePath = path.join(phaseFullDir, file);
let raw = '';
try {
// #3078-CR MEDIUM: normalize line endings at the read boundary — the
// same seam src/uat.cts and src/verification.cts route through — so a
// lone-CR *-UAT.md is not read as one unbroken line by the column-0
// scans below.
raw = normalizeLineEndings(fs.readFileSync(uatFilePath, 'utf-8'));
} catch {
blockers.push(`${file}: could not read file`);
continue;
}
// ── Per-file malformed markdown guard ──────────────────────────────────
// FIX D: use accurate signals from analyzeMarkdown instead of heuristics.
// unterminatedFence: CommonMark state machine detects a genuinely unclosed fence.
// unterminatedComment: strips balanced comments first, then checks for leftover <!--.
const { unterminatedFence, unterminatedComment } = analyzeMarkdown(raw);
if (unterminatedFence || unterminatedComment) {
blockers.push(`${file}: malformed markdown (unterminated fence or comment)`);
}
const fm = extractFrontmatter(raw, uatFilePath) as Record<string, unknown>;
// File-level frontmatter status check
if (fm['status'] && BLOCKING_UAT_FM_STATUSES.has(fm['status'] as string)) {
blockers.push(`${file}: frontmatter status=${fm['status'] as string}`);
}
// File-level frontmatter result check
if (fm['result'] && BLOCKING_UAT_FM_RESULTS.has(fm['result'] as string)) {
blockers.push(`${file}: frontmatter result=${fm['result'] as string}`);
}
// Parse test items from the cleaned body (hardened against false positives)
const cleanContent = stripFalsePositiveContexts(raw);
const items = parseUatResultItems(cleanContent);
for (const item of items) {
const passing = PASSING_RESULTS.has(item.result);
checks.push({
file,
test: item.test,
name: item.name,
result: item.result,
passing,
});
if (!passing) {
blockers.push(`${file}: test ${item.test} (${item.result})`);
}
}
}
// ── Process VERIFICATION files ─────────────────────────────────────────────
let hasPassingVerification = false;
for (const file of verFileNames) {
verificationFiles.push(file);
const verificationFilePath = path.join(phaseFullDir, file);
let raw = '';
try {
// #3078-CR MEDIUM: same read-boundary normalization as the UAT loop above.
raw = normalizeLineEndings(fs.readFileSync(verificationFilePath, 'utf-8'));
} catch {
blockers.push(`${file}: could not read verification file`);
continue;
}
const vfm = extractFrontmatter(raw, verificationFilePath) as Record<string, unknown>;
const vStatus = vfm['status'] as string | undefined;
if (vStatus && BLOCKING_VERIFICATION_FM_STATUSES.has(vStatus)) {
blockers.push(`${file}: verification status=${vStatus}`);
} else if (vStatus && PASSING_VERIFICATION_STATUSES.has(vStatus)) {
// Allowlist: only explicitly-passing statuses count
hasPassingVerification = true;
}
// Missing or unknown status: does NOT count as passing, does NOT push a blocker
// (handled by the requireVerification policy check below if needed)
}
// ── Policy: requireVerification ───────────────────────────────────────────
// #3057 B3: routing here is UNCHANGED — an indeterminate staleness check
// still falls through to the same `verificationStatus !== 'passed'` branch
// it always did (the pre-existing fail-open contract). `verificationStaleCheckIndeterminate`
// only records the fact for the report below; it never itself gates `blockers`.
let verificationStaleCheckIndeterminate = false;
if (requireVerification) {
const verificationResult = readVerificationStatus(phaseFullDir);
const verificationStatus = verificationResult.status;
verificationStaleCheckIndeterminate = verificationResult.staleCheckIndeterminate === true;
if (verificationStatus === 'stale') {
blockers.push('policy: verification status=stale');
} else if (verificationStatus !== 'passed' || !hasPassingVerification) {
blockers.push('policy: verification required but no passing *-VERIFICATION.md found');
}
}
// ── Determine no_uat_artifacts and passed ─────────────────────────────────
// no_uat_artifacts: true when no real UAT test items were parsed from any file
const no_uat_artifacts = checks.length === 0;
// FIX 1: require positive passing evidence; no vacuous pass
// passed = no blockers AND at least one check AND all checks passing
const passed = blockers.length === 0 && checks.length > 0 && checks.every(c => c.passing);
return {
passed,
uat_files: uatFiles,
verification_files: verificationFiles,
checks,
blockers,
no_uat_artifacts,
policy: {
require_verification: requireVerification,
},
verification_stale_check_indeterminate: verificationStaleCheckIndeterminate,
};
}
export = {
stripFalsePositiveContexts,
parseUatResultItems,
analyzeMarkdown,
evaluateUatPassed,
};