* 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>
1365 lines
58 KiB
TypeScript
1365 lines
58 KiB
TypeScript
/**
|
|
* Planning Inspect Module — the schema-v1 canonical planning snapshot (#2790).
|
|
*
|
|
* `planning.inspect` is READ-ONLY: it opens `.planning/` documents and writes
|
|
* nothing, anywhere, ever. Downstream harness UIs (gsd-code Phase 12, plan
|
|
* mission-control) consume it instead of parsing ROADMAP/REQUIREMENTS/PLAN/
|
|
* SUMMARY markdown a second time — gsd-core is the single source of `.planning/`
|
|
* truth.
|
|
*
|
|
* COMPOSED, NOT RE-DERIVED. Every ADR-3180 §7 derivation arrives from its
|
|
* declared owner: milestone identity and windowing from `getMilestoneInfo`
|
|
* (§7.2) and phase enumeration from `listMilestonePhaseDirs` (§7.3), both via
|
|
* `buildPlanningSnapshot`; phase completion from `isPhaseComplete` (§7.4,
|
|
* disk-strict); live-plan counting from `scanPhasePlans` (§7.5); the
|
|
* fraction→percent arithmetic from `clampPercent` (§7.6). Plan bodies come from
|
|
* `parsePlanDocument` (`src/plan-document.cts`), requirement IDs from
|
|
* `parseRequirements` (`src/gap-checker.cts`), UAT items from `parseUatItems`
|
|
* (`src/uat.cts`). This module introduces no second answer to any of those
|
|
* questions.
|
|
*
|
|
* WHY THIS DOES NOT SERIALIZE `PlanningSnapshot` DIRECTLY. `PlanningSnapshot`
|
|
* is the §8.1 *diagnostic-rule subject* — explicitly additive and still growing
|
|
* (4 fields at Phase 10, 20+ by Phase 12). schema-v1 is a frozen EXTERNAL
|
|
* contract. Handing an internal, churning shape to external consumers is a
|
|
* Hyrum's-Law break waiting to happen, so this module declares its own flat
|
|
* schema and maps into it. Adding a field to `PlanningSnapshot` must never
|
|
* change what `planning.inspect` emits.
|
|
*
|
|
* NEVER INFERS. Where evidence is absent or two sources disagree, the value is
|
|
* `null` / `unknown` and a diagnostic names why. It is never reconciled, never
|
|
* guessed, and never filled from a plausible default. Keys are ALWAYS present —
|
|
* omitting a key on a non-answer is itself an observable a consumer would bind
|
|
* to.
|
|
*
|
|
* NOT a diagnostic rule, and deliberately NOT registered in
|
|
* `scripts/lint-planning-snapshot-bypass-drift.cjs`: that guard is
|
|
* `DIAGNOSTIC_RULE_FUNCTIONS`-scoped and must be prunable to zero when #3309
|
|
* lands. This is a query command.
|
|
*
|
|
* ADR-457 build-at-publish: source in src/planning-inspect.cts, compiled to
|
|
* gsd-core/bin/lib/planning-inspect.cjs (gitignored).
|
|
*/
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import planningSnapshotMod = require('./planning-snapshot.cjs');
|
|
const { buildPlanningSnapshot, worstScope } = planningSnapshotMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import planningWorkspaceMod = require('./planning-workspace.cjs');
|
|
const { planningPaths } = planningWorkspaceMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import planScan = require('./plan-scan.cjs');
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import planDocumentMod = require('./plan-document.cjs');
|
|
const { parsePlanDocument, TASK_KIND, planIdFromFile } = planDocumentMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import gapCheckerMod = require('./gap-checker.cjs');
|
|
const { parseRequirements } = gapCheckerMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import uatMod = require('./uat.cjs');
|
|
const { parseUatItemsWithStats, selectPhaseUatFiles } = uatMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import phaseLifecycleMod = require('./phase-lifecycle.cjs');
|
|
const { clampPercent } = phaseLifecycleMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import planningScopeMod = require('./planning-scope.cjs');
|
|
const { SCOPE } = planningScopeMod;
|
|
type Scope = planningScopeMod.Scope;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import verificationMod = require('./verification.cjs');
|
|
const { readVerificationStatus } = verificationMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import phaseIdMod = require('./phase-id.cjs');
|
|
const { phaseKeyFromDir, phaseKeyFromToken, phaseMarkdownRegexSource } = phaseIdMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import roadmapParserMod = require('./roadmap-parser.cjs');
|
|
const { extractCurrentMilestone } = roadmapParserMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import io = require('./io.cjs');
|
|
const { output } = io;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import frontmatterMod = require('./frontmatter.cjs');
|
|
const { extractFrontmatter, stripFrontmatter } = frontmatterMod;
|
|
import { stateFieldValue, stateCurrentPositionSlice } from './state-document.cjs';
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import markdownSectionizer = require('./markdown-sectionizer.cjs');
|
|
const { collectSection, iterateBullets } = markdownSectionizer;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import markdownTable = require('./markdown-table.cjs');
|
|
const { parseMarkdownTable, matchTableSchema } = markdownTable;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import coreUtilsMod = require('./core-utils.cjs');
|
|
const { normalizeLineEndings } = coreUtilsMod;
|
|
|
|
/**
|
|
* The wire schema version. A consumer MUST reject any value other than this
|
|
* one rather than best-effort-parsing an unknown shape.
|
|
*/
|
|
const PLANNING_INSPECT_SCHEMA_VERSION = 1;
|
|
|
|
/**
|
|
* Frozen diagnostic vocabulary. Adding a member is the repo's standard three
|
|
* coordinated changes: this enum, the emitting site, and the test that locks
|
|
* `Object.keys(INSPECT_DIAGNOSTIC).sort()`.
|
|
*/
|
|
const INSPECT_DIAGNOSTIC = Object.freeze({
|
|
PLANNING_ROOT_ABSENT: 'planning_root_absent',
|
|
ROADMAP_UNSCOPED: 'roadmap_unscoped',
|
|
REQUIREMENTS_ABSENT: 'requirements_absent',
|
|
REQUIREMENTS_UNREADABLE: 'requirements_unreadable',
|
|
REQUIREMENT_DUPLICATE: 'requirement_duplicate',
|
|
REQUIREMENT_UNMAPPED: 'requirement_unmapped',
|
|
REQUIREMENT_PHASE_UNKNOWN: 'requirement_phase_unknown',
|
|
REQUIREMENT_COMPLETION_UNKNOWN: 'requirement_completion_unknown',
|
|
ORPHAN_PHASE_DIR: 'orphan_phase_dir',
|
|
PHASE_SCOPE_DEGRADED: 'phase_scope_degraded',
|
|
PLAN_UNREADABLE: 'plan_unreadable',
|
|
SUMMARY_UNREADABLE: 'summary_unreadable',
|
|
TASK_SHAPE_CHECKPOINT: 'task_shape_checkpoint',
|
|
TASK_CHANGED_FILES_PLAN_SCOPED: 'task_changed_files_plan_scoped',
|
|
TASK_CHANGED_FILES_CONFLICTING: 'task_changed_files_conflicting',
|
|
UAT_ABSENT: 'uat_absent',
|
|
UAT_UNREADABLE: 'uat_unreadable',
|
|
PERCENT_WITHHELD: 'percent_withheld',
|
|
});
|
|
|
|
type InspectDiagnosticCode = (typeof INSPECT_DIAGNOSTIC)[keyof typeof INSPECT_DIAGNOSTIC];
|
|
|
|
/** Whether a task has a completion record. `unknown` is a first-class answer. */
|
|
const TASK_STATUS = Object.freeze({
|
|
DONE: 'done',
|
|
PENDING: 'pending',
|
|
UNKNOWN: 'unknown',
|
|
});
|
|
|
|
type TaskStatus = (typeof TASK_STATUS)[keyof typeof TASK_STATUS];
|
|
|
|
/**
|
|
* How precisely a changed-file list is attributed. `plan_scoped` is the common
|
|
* case and is NOT an error — SUMMARY.md's `## Files Created/Modified` is a
|
|
* plan-level section, so spreading it across a plan's tasks would be inference.
|
|
*/
|
|
const PROVENANCE = Object.freeze({
|
|
TASK_SCOPED: 'task_scoped',
|
|
PLAN_SCOPED: 'plan_scoped',
|
|
ABSENT: 'absent',
|
|
});
|
|
|
|
type Provenance = (typeof PROVENANCE)[keyof typeof PROVENANCE];
|
|
|
|
/** Whether planned and changed file sets agree. Never reconciled. */
|
|
const AGREEMENT = Object.freeze({
|
|
AGREED: 'agreed',
|
|
CONFLICTING: 'conflicting',
|
|
UNKNOWN: 'unknown',
|
|
});
|
|
|
|
type Agreement = (typeof AGREEMENT)[keyof typeof AGREEMENT];
|
|
|
|
interface Diagnostic {
|
|
code: InspectDiagnosticCode;
|
|
subject: string;
|
|
detail: string;
|
|
}
|
|
|
|
interface Fraction {
|
|
completed: number;
|
|
total: number;
|
|
/** `null` whenever `scope !== complete` — ADR-3180 §7.6 rule 4. */
|
|
percent: number | null;
|
|
scope: Scope;
|
|
}
|
|
|
|
// ─── Small shared helpers ─────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Path separators are normalised UNCONDITIONALLY, never via `path.sep` — a
|
|
* backslash-bearing path can arrive on Linux too, so a platform-gated
|
|
* normaliser leaves the non-Windows case untested and broken.
|
|
*/
|
|
function toPosix(value: string): string {
|
|
return value.replace(/\\/g, '/');
|
|
}
|
|
|
|
/**
|
|
* Read a UTF-8 file, distinguishing absent from unreadable.
|
|
*
|
|
* `root` is a containment boundary, not a convenience default: every
|
|
* document this module reads arrives as UNTRUSTED input — a clone, a PR
|
|
* branch, a teammate's working tree — and the assembled payload is handed
|
|
* to downstream tooling verbatim (see module doc). A `*-PLAN.md` (or any
|
|
* other document under `.planning/`) that is a SYMLINK resolving outside
|
|
* `root` is therefore an exfiltration path, not a convenience: reading it
|
|
* would let a planted symlink smuggle arbitrary readable file content into
|
|
* the emitted payload. Both `filePath` and `root` are resolved with
|
|
* `fs.realpathSync` — never a raw string prefix check on the unresolved
|
|
* path — so a project that legitimately symlinks its whole `.planning/`
|
|
* directory, or a single phase directory, elsewhere on disk keeps working;
|
|
* only a resolved target that ends up OUTSIDE the resolved root is
|
|
* rejected. An escape is reported as unreadable (same shape as any other
|
|
* unreadable document) so existing per-document degradation and
|
|
* diagnostics apply unchanged.
|
|
*/
|
|
/**
|
|
* Path-boundary-safe comparison of two ALREADY-RESOLVED absolute paths — the
|
|
* ONE containment comparison every containment check in this module shares
|
|
* (`readDocument`'s file-level guard, `isPathContained` below for
|
|
* directories and the verification-status `fs` seam), so none of them can
|
|
* drift apart. `target` must equal `root`, or begin with `root` plus a path
|
|
* separator; a bare `startsWith(root)` would wrongly accept a sibling
|
|
* directory like `.planning-evil/` that merely shares a string prefix. Pure
|
|
* string comparison, no I/O — callers own their own `fs.realpathSync` call
|
|
* (and its own not-found/broken-symlink handling).
|
|
*/
|
|
function isWithinRoot(resolvedTarget: string, resolvedRoot: string): boolean {
|
|
return resolvedTarget === resolvedRoot || resolvedTarget.startsWith(resolvedRoot + path.sep);
|
|
}
|
|
|
|
/**
|
|
* Containment check for a path (file OR directory) that resolves its own
|
|
* `fs.realpathSync`, then delegates the actual boundary comparison to
|
|
* `isWithinRoot`. Used where the caller does not need to distinguish "target
|
|
* vanished / broken symlink" from "target resolved but escapes root" — both
|
|
* degrade the same way at every call site that uses this (an escaped or
|
|
* unresolvable phase directory is treated identically to an unreadable one).
|
|
* `readDocument` below needs that distinction for its own exists/readable
|
|
* tri-state, so it keeps its own inline `realpathSync` calls and calls
|
|
* `isWithinRoot` directly instead of this wrapper.
|
|
*/
|
|
function isPathContained(target: string, root: string): boolean {
|
|
let realTarget: string;
|
|
let realRoot: string;
|
|
try {
|
|
realTarget = fs.realpathSync(target);
|
|
realRoot = fs.realpathSync(root);
|
|
} catch {
|
|
return false;
|
|
}
|
|
return isWithinRoot(realTarget, realRoot);
|
|
}
|
|
|
|
function readDocument(filePath: string, root: string): { text: string | null; exists: boolean; readable: boolean } {
|
|
let stat: fs.Stats;
|
|
try {
|
|
stat = fs.statSync(filePath);
|
|
} catch {
|
|
return { text: null, exists: false, readable: false };
|
|
}
|
|
// A directory, socket, or symlink resolving to a device in a document
|
|
// position is not a document. Reject before any open (cf. #2378/#2383).
|
|
if (!stat.isFile()) return { text: null, exists: true, readable: false };
|
|
|
|
let realTarget: string;
|
|
let realRoot: string;
|
|
try {
|
|
realTarget = fs.realpathSync(filePath);
|
|
realRoot = fs.realpathSync(root);
|
|
} catch {
|
|
// A broken symlink, or the path vanished between the stat above and
|
|
// here — the same non-answer `readDocument` already gives "not exists".
|
|
return { text: null, exists: false, readable: false };
|
|
}
|
|
if (!isWithinRoot(realTarget, realRoot)) {
|
|
return { text: null, exists: true, readable: false };
|
|
}
|
|
|
|
try {
|
|
// #3707-CR follow-up MAJOR: normalize line endings HERE, at this module's
|
|
// one shared document-read seam, so a lone-CR document (CommonMark line
|
|
// ending; a document using it renders as separate lines to a human
|
|
// reader) is normalized by construction for every current and future
|
|
// caller of `readDocument` (`buildRequirements`, `buildUatRows`, the
|
|
// ROADMAP.md read above) — not only the ones a parser author remembered
|
|
// to fix individually. Mirrors `src/uat.cts`'s `readNormalizedDocument`,
|
|
// the equivalent boundary for `cmdAuditUat`; both now delegate to the
|
|
// same `normalizeLineEndings` in `core-utils.cts`.
|
|
return { text: normalizeLineEndings(fs.readFileSync(filePath, 'utf-8')), exists: true, readable: true };
|
|
} catch {
|
|
return { text: null, exists: true, readable: false };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Containment-enforcing `fs` seam for `readVerificationStatus`
|
|
* (`src/verification.cts`), passed through that function's existing
|
|
* `opts.fs` injection point — GAP 2 of the #2790 follow-up security review.
|
|
*
|
|
* `readVerificationStatus` is a SHARED owner consumed by many commands
|
|
* (init, roadmap, phase, ship, …), so it must not gain a containment
|
|
* parameter of its own; the boundary is enforced HERE, at this consumer's
|
|
* call site, instead. Each method below delegates to the real `node:fs`
|
|
* ONLY after the given path passes `isPathContained` against
|
|
* `planningRoot` — otherwise it throws. `readVerificationStatus` already
|
|
* treats every one of these calls' failures as its no-throw "missing"
|
|
* degradation (see its own try/catch around `readdirSync`/`readFileSync`,
|
|
* and `findStaleVerificationSummary`'s around `readdirSync`/`statSync`), so
|
|
* a `*-VERIFICATION.md` symlinked outside the planning root — or a phase
|
|
* directory that is itself such a symlink — now degrades to the ordinary
|
|
* 'missing' status instead of leaking frontmatter content (or the escaped
|
|
* directory's filenames) into the payload.
|
|
*/
|
|
function containmentEnforcingVerificationFs(planningRoot: string): {
|
|
readdirSync(dir: string): string[];
|
|
readFileSync(filePath: string, encoding: 'utf-8'): string;
|
|
statSync(filePath: string): { mtimeMs: number };
|
|
} {
|
|
function assertContained(target: string): void {
|
|
if (!isPathContained(target, planningRoot)) {
|
|
throw new Error(`planning-inspect: path escapes planning root: ${toPosix(target)}`);
|
|
}
|
|
}
|
|
return {
|
|
readdirSync(dir: string): string[] {
|
|
assertContained(dir);
|
|
return fs.readdirSync(dir);
|
|
},
|
|
readFileSync(filePath: string, encoding: 'utf-8'): string {
|
|
assertContained(filePath);
|
|
return fs.readFileSync(filePath, encoding);
|
|
},
|
|
statSync(filePath: string): { mtimeMs: number } {
|
|
assertContained(filePath);
|
|
return fs.statSync(filePath);
|
|
},
|
|
};
|
|
}
|
|
|
|
function sortedUnique(values: string[]): string[] {
|
|
return [...new Set(values)].sort();
|
|
}
|
|
|
|
// ─── Requirements ─────────────────────────────────────────────────────────────
|
|
|
|
interface RequirementRow {
|
|
id: string;
|
|
text: string | null;
|
|
/** `true` / `false` from the checkbox, or `unknown` when no checkbox exists. */
|
|
complete: boolean | 'unknown';
|
|
/** Phase tokens the Traceability table maps this requirement to. */
|
|
mappedPhases: string[];
|
|
/**
|
|
* Correlation convenience: the CODES (never prose) of the diagnostics this
|
|
* module raised for THIS requirement. The global `diagnostics[]` array
|
|
* remains the sole authoritative record — this is a lookup shortcut so a
|
|
* consumer does not have to string-parse `diagnostics[].subject` (e.g.
|
|
* `"AUTH-02->9"`) to correlate a diagnostic back to a row.
|
|
*/
|
|
diagnostics: InspectDiagnosticCode[];
|
|
scope: Scope;
|
|
}
|
|
|
|
/**
|
|
* Prefix-agnostic requirement-ID format shared with `parseRequirements`
|
|
* (`src/gap-checker.cts`) — REQ-01, TST-01, BACK-07, INSP-04, etc. Bold
|
|
* markers (`**ID**`) are optional decoration, matching how `gap-checker.cts`
|
|
* pulls the ID out of a checkbox bullet's text.
|
|
*/
|
|
const ID_PATTERN = '[A-Z][A-Z0-9]*-[A-Za-z0-9_-]+';
|
|
|
|
/**
|
|
* A Traceability table's `Requirement` cell holds ONLY the ID (mod optional
|
|
* bold + surrounding whitespace) — full-match, mirroring the pipe-bounded
|
|
* anchoring the prior hand-rolled `ID_CELL` regex enforced.
|
|
*/
|
|
const CELL_ID_RE = new RegExp(`^\\*{0,2}(${ID_PATTERN})\\*{0,2}$`);
|
|
|
|
/**
|
|
* A checkbox bullet's leading `**ID**` — prefix match only (no end anchor):
|
|
* trailing prose (`: description`, ` some text`) is not required to match,
|
|
* mirroring the prior hand-rolled `BULLET` regex.
|
|
*/
|
|
const BULLET_ID_RE = new RegExp(`^\\*{0,2}(${ID_PATTERN})\\*{0,2}`);
|
|
|
|
/**
|
|
* Parse the `## Traceability` table's `Requirement | Phase | Status` rows via
|
|
* the canonical `markdown-table` seam (ADR-2143) — never a hand-rolled
|
|
* table/row regex. A missing/malformed section or table, or a header that
|
|
* doesn't match the `RequirementsTraceability` schema, yields an EMPTY map: a
|
|
* malformed table is a non-answer, and every requirement then falls through
|
|
* to the caller's own `REQUIREMENT_UNMAPPED` diagnostic.
|
|
*/
|
|
function parseTraceability(reqMd: string): Map<string, string[]> {
|
|
const byId = new Map<string, string[]>();
|
|
const section = collectSection(reqMd, (h) => /^traceability$/i.test(h.text.trim()));
|
|
if (!section) return byId;
|
|
|
|
const parsed = parseMarkdownTable(section.body);
|
|
if (!parsed.ok) return byId;
|
|
|
|
const schema = matchTableSchema(parsed.value.columns);
|
|
if (!schema || schema.id !== 'RequirementsTraceability') return byId;
|
|
|
|
for (const row of parsed.value.rows) {
|
|
const idMatch = CELL_ID_RE.exec((row.Requirement ?? '').trim());
|
|
if (!idMatch) continue;
|
|
const id = idMatch[1];
|
|
// `Phase 1`, `1`, `Phase 1, Phase 2` — the token is what a consumer can
|
|
// match against a phase id; the surrounding word is decoration. This is
|
|
// value-level parsing of ONE already-addressed cell, not markdown parsing.
|
|
const tokens = [...(row.Phase ?? '').matchAll(/\d+(?:\.\d+)*/g)].map((t) => t[0]);
|
|
const existing = byId.get(id);
|
|
if (existing) existing.push(...tokens);
|
|
else byId.set(id, tokens);
|
|
}
|
|
return byId;
|
|
}
|
|
|
|
/**
|
|
* Checkbox completion state per requirement ID, from the `- [x] **ID**`
|
|
* bullets — driven by the canonical `iterateBullets` seam, same bold-ID
|
|
* extraction style as `parseRequirements` (`src/gap-checker.cts`). A
|
|
* requirement with no bullet has no checkbox answer — reported as `unknown`,
|
|
* never defaulted to `false`.
|
|
*/
|
|
function parseCheckboxStates(reqMd: string): Map<string, boolean> {
|
|
const states = new Map<string, boolean>();
|
|
for (const bullet of iterateBullets(reqMd)) {
|
|
if (bullet.marker !== 'checkbox-checked' && bullet.marker !== 'checkbox-unchecked') continue;
|
|
const m = BULLET_ID_RE.exec(bullet.text);
|
|
if (!m) continue;
|
|
// First occurrence wins, matching parseRequirements' own `seen` set.
|
|
if (!states.has(m[1])) states.set(m[1], bullet.marker === 'checkbox-checked');
|
|
}
|
|
return states;
|
|
}
|
|
|
|
/** IDs appearing more than once in the checkbox bullets, in document order. */
|
|
function findDuplicateIds(reqMd: string): string[] {
|
|
const seen = new Set<string>();
|
|
const dupes: string[] = [];
|
|
for (const bullet of iterateBullets(reqMd)) {
|
|
if (bullet.marker !== 'checkbox-checked' && bullet.marker !== 'checkbox-unchecked') continue;
|
|
const m = BULLET_ID_RE.exec(bullet.text);
|
|
if (!m) continue;
|
|
if (seen.has(m[1])) dupes.push(m[1]);
|
|
else seen.add(m[1]);
|
|
}
|
|
return dupes;
|
|
}
|
|
|
|
function buildRequirements(
|
|
requirementsPath: string,
|
|
knownPhaseKeys: Set<string>,
|
|
diagnostics: Diagnostic[],
|
|
planningRoot: string,
|
|
): { rows: RequirementRow[]; scope: Scope } {
|
|
const doc = readDocument(requirementsPath, planningRoot);
|
|
if (!doc.exists) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENTS_ABSENT,
|
|
subject: toPosix(requirementsPath),
|
|
detail: 'REQUIREMENTS.md does not exist; no requirement rows are available.',
|
|
});
|
|
return { rows: [], scope: SCOPE.UNSCOPED };
|
|
}
|
|
if (!doc.readable || doc.text === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENTS_UNREADABLE,
|
|
subject: toPosix(requirementsPath),
|
|
detail: 'REQUIREMENTS.md exists but could not be read; zero rows is not a reliable answer.',
|
|
});
|
|
return { rows: [], scope: SCOPE.UNREADABLE };
|
|
}
|
|
|
|
const reqMd = doc.text;
|
|
const items = parseRequirements(reqMd);
|
|
const traceability = parseTraceability(reqMd);
|
|
const checkboxes = parseCheckboxStates(reqMd);
|
|
|
|
const dupeIds = findDuplicateIds(reqMd);
|
|
for (const dupe of dupeIds) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENT_DUPLICATE,
|
|
subject: dupe,
|
|
detail: 'Requirement ID appears more than once; the first occurrence is authoritative.',
|
|
});
|
|
}
|
|
const duplicateIdSet = new Set(dupeIds);
|
|
|
|
const rows: RequirementRow[] = items.map((item: { id: string; text: string }) => {
|
|
const mappedPhases = sortedUnique(traceability.get(item.id) ?? []);
|
|
const hasCheckbox = checkboxes.has(item.id);
|
|
const complete: boolean | 'unknown' = hasCheckbox ? (checkboxes.get(item.id) as boolean) : 'unknown';
|
|
const rowDiagnostics: InspectDiagnosticCode[] = [];
|
|
|
|
if (duplicateIdSet.has(item.id)) {
|
|
rowDiagnostics.push(INSPECT_DIAGNOSTIC.REQUIREMENT_DUPLICATE);
|
|
}
|
|
if (mappedPhases.length === 0) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENT_UNMAPPED,
|
|
subject: item.id,
|
|
detail: 'No Traceability row maps this requirement to a phase.',
|
|
});
|
|
rowDiagnostics.push(INSPECT_DIAGNOSTIC.REQUIREMENT_UNMAPPED);
|
|
}
|
|
for (const token of mappedPhases) {
|
|
// `phaseKeyFromDir` and `phaseKeyFromToken` are the canonical pair for
|
|
// phase-IDENTITY equality: both map a directory name and a document
|
|
// token onto the same canonical zero-padded key, so `01-auth` /
|
|
// `1-auth` / `Phase 1` / `Phase 01` are recognized as one phase, and
|
|
// decimal phases (`1.1`) compare correctly. A raw string compare gets
|
|
// the padding case wrong; `phaseTokenMatches` is the wrong primitive
|
|
// here too — it is a FILE-MEMBERSHIP predicate (#3511) for aggregate
|
|
// `*-UAT.md` / `*-VERIFICATION.md` scans, not a phase-identity equality
|
|
// test, and is unreliable for decimal phases.
|
|
if (!knownPhaseKeys.has(phaseKeyFromToken(token))) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENT_PHASE_UNKNOWN,
|
|
subject: `${item.id}->${token}`,
|
|
detail: 'Traceability maps this requirement to a phase that is not present on disk.',
|
|
});
|
|
rowDiagnostics.push(INSPECT_DIAGNOSTIC.REQUIREMENT_PHASE_UNKNOWN);
|
|
}
|
|
}
|
|
if (complete === 'unknown') {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.REQUIREMENT_COMPLETION_UNKNOWN,
|
|
subject: item.id,
|
|
detail: 'Requirement has no checkbox bullet; completion is unknown, not incomplete.',
|
|
});
|
|
rowDiagnostics.push(INSPECT_DIAGNOSTIC.REQUIREMENT_COMPLETION_UNKNOWN);
|
|
}
|
|
|
|
return {
|
|
id: item.id,
|
|
text: item.text && item.text.length > 0 ? item.text : null,
|
|
complete,
|
|
mappedPhases,
|
|
diagnostics: rowDiagnostics,
|
|
scope: SCOPE.COMPLETE,
|
|
};
|
|
});
|
|
|
|
return { rows, scope: SCOPE.COMPLETE };
|
|
}
|
|
|
|
// ─── Summary provenance ───────────────────────────────────────────────────────
|
|
|
|
interface SummaryProvenance {
|
|
/** `## Files Created/Modified` — a PLAN-level list. Never attributed to a task. */
|
|
planFiles: string[];
|
|
/** Task index -> files, from a deviation block naming `Found during: Task N`. */
|
|
byTask: Map<number, string[]>;
|
|
}
|
|
|
|
/**
|
|
* Parse a SUMMARY.md body for file provenance.
|
|
*
|
|
* Two DIFFERENT scopes live in this document and conflating them is the whole
|
|
* hazard: `## Files Created/Modified` describes the PLAN, while a deviation
|
|
* block's `**Files modified:**` is attributed to the task its `**Found during:**
|
|
* Task N` line names. Only the latter is task-scoped.
|
|
*/
|
|
function parseSummaryProvenance(content: string): SummaryProvenance {
|
|
const planFiles: string[] = [];
|
|
const byTask = new Map<number, string[]>();
|
|
|
|
// `## Files Created/Modified` — a PLAN-level bullet list. Bounded to the
|
|
// section body via collectSection + iterateBullets; absent is not an error.
|
|
const filesSection = collectSection(content, (h) => /^files\s+created\/modified\s*$/i.test(h.text.trim()));
|
|
if (filesSection) {
|
|
for (const bullet of iterateBullets(filesSection.body)) {
|
|
// `- \`path/to/file.ts\` - What it does`
|
|
const m = /^`([^`]+)`/.exec(bullet.text);
|
|
if (m) planFiles.push(m[1].trim());
|
|
}
|
|
}
|
|
|
|
// `## Deviations from Plan` (also matches the `(Auto-fixed)` variant) — the
|
|
// `**Found during:** Task N` / `**Files modified:**` scan is a regex
|
|
// CONFINED to this already-collected section body (ADR-2143 §4 sanctioned
|
|
// pattern), never a document-wide heading walk.
|
|
const deviationsSection = collectSection(content, (h) => /^deviations\s+from\s+plan/i.test(h.text.trim()));
|
|
if (deviationsSection) {
|
|
let currentTask: number | null = null;
|
|
for (const line of deviationsSection.body.split(/\r?\n/)) {
|
|
const foundDuring = /\*\*Found during:\*\*\s*Task\s*(\d+)/i.exec(line);
|
|
if (foundDuring) {
|
|
currentTask = parseInt(foundDuring[1], 10);
|
|
continue;
|
|
}
|
|
|
|
const filesModified = /\*\*Files modified:\*\*\s*(.+)$/i.exec(line);
|
|
if (filesModified && currentTask !== null) {
|
|
const files = filesModified[1]
|
|
.split(',')
|
|
.map((f) => f.trim().replace(/^`|`$/g, ''))
|
|
.filter((f) => f.length > 0);
|
|
const existing = byTask.get(currentTask);
|
|
if (existing) existing.push(...files);
|
|
else byTask.set(currentTask, files);
|
|
}
|
|
}
|
|
}
|
|
|
|
return { planFiles: sortedUnique(planFiles), byTask };
|
|
}
|
|
|
|
// ─── Plans and tasks ──────────────────────────────────────────────────────────
|
|
|
|
interface TaskRow {
|
|
index: number;
|
|
kind: string;
|
|
type: string | null;
|
|
name: string | null;
|
|
plannedFiles: string[];
|
|
acceptanceCriteria: string[];
|
|
done: string | null;
|
|
/** `null` unless the SUMMARY attributes files to THIS task. */
|
|
changedFiles: string[] | null;
|
|
provenance: Provenance;
|
|
agreement: Agreement;
|
|
status: TaskStatus;
|
|
}
|
|
|
|
interface PlanRow {
|
|
id: string;
|
|
file: string;
|
|
superseded: boolean;
|
|
objective: string | null;
|
|
wave: number | null;
|
|
dependsOn: string[];
|
|
autonomous: boolean;
|
|
agentHint: string | null;
|
|
/** From the plan's own frontmatter `files_modified`. */
|
|
plannedFiles: string[];
|
|
/** Plan-scoped `## Files Created/Modified`. `null` when no SUMMARY exists. */
|
|
changedFiles: string[] | null;
|
|
hasSummary: boolean;
|
|
tasks: TaskRow[];
|
|
scope: Scope;
|
|
}
|
|
|
|
/** Pair a plan file with its SUMMARY by the canonical id embedded in the name. */
|
|
function summaryForPlan(planFile: string, summaryFiles: string[]): string | null {
|
|
const base = path.basename(planFile);
|
|
const key = base.replace(/-?PLAN/i, '').replace(/\.md$/i, '');
|
|
const dir = planFile.includes('/') ? planFile.slice(0, planFile.lastIndexOf('/') + 1) : '';
|
|
for (const candidate of summaryFiles) {
|
|
if (!candidate.startsWith(dir)) continue;
|
|
const candidateKey = path.basename(candidate).replace(/-?SUMMARY/i, '').replace(/\.md$/i, '');
|
|
if (candidateKey === key) return candidate;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function buildTaskRows(
|
|
planFile: string,
|
|
parsed: ReturnType<typeof parsePlanDocument>,
|
|
provenance: SummaryProvenance | null,
|
|
diagnostics: Diagnostic[],
|
|
): TaskRow[] {
|
|
return parsed.tasks.map((task) => {
|
|
if (task.kind === TASK_KIND.CHECKPOINT) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.TASK_SHAPE_CHECKPOINT,
|
|
subject: `${toPosix(planFile)}#${task.index}`,
|
|
detail: 'Checkpoint task: the grammar carries no name/files/acceptance elements.',
|
|
});
|
|
}
|
|
|
|
if (provenance === null) {
|
|
return {
|
|
index: task.index,
|
|
kind: task.kind,
|
|
type: task.type,
|
|
name: task.name,
|
|
plannedFiles: task.plannedFiles,
|
|
acceptanceCriteria: task.acceptanceCriteria,
|
|
done: task.done,
|
|
changedFiles: null,
|
|
provenance: PROVENANCE.ABSENT,
|
|
agreement: AGREEMENT.UNKNOWN,
|
|
status: TASK_STATUS.PENDING,
|
|
};
|
|
}
|
|
|
|
const attributed = provenance.byTask.get(task.index);
|
|
if (attributed === undefined) {
|
|
// A SUMMARY exists but says nothing about THIS task. The plan-level file
|
|
// list is not evidence about a task — attributing it would be inference.
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.TASK_CHANGED_FILES_PLAN_SCOPED,
|
|
subject: `${toPosix(planFile)}#${task.index}`,
|
|
detail: 'SUMMARY carries only a plan-level file list; task-scoped changed files are unknown.',
|
|
});
|
|
return {
|
|
index: task.index,
|
|
kind: task.kind,
|
|
type: task.type,
|
|
name: task.name,
|
|
plannedFiles: task.plannedFiles,
|
|
acceptanceCriteria: task.acceptanceCriteria,
|
|
done: task.done,
|
|
changedFiles: null,
|
|
provenance: PROVENANCE.PLAN_SCOPED,
|
|
agreement: AGREEMENT.UNKNOWN,
|
|
status: TASK_STATUS.UNKNOWN,
|
|
};
|
|
}
|
|
|
|
const changed = sortedUnique(attributed);
|
|
const planned = sortedUnique(task.plannedFiles);
|
|
let agreement: Agreement = AGREEMENT.UNKNOWN;
|
|
if (planned.length > 0) {
|
|
const same = planned.length === changed.length && planned.every((f, i) => f === changed[i]);
|
|
agreement = same ? AGREEMENT.AGREED : AGREEMENT.CONFLICTING;
|
|
if (!same) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.TASK_CHANGED_FILES_CONFLICTING,
|
|
subject: `${toPosix(planFile)}#${task.index}`,
|
|
detail: 'Planned and changed file sets disagree; both are reported verbatim, unreconciled.',
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
index: task.index,
|
|
kind: task.kind,
|
|
type: task.type,
|
|
name: task.name,
|
|
plannedFiles: task.plannedFiles,
|
|
acceptanceCriteria: task.acceptanceCriteria,
|
|
done: task.done,
|
|
changedFiles: changed,
|
|
provenance: PROVENANCE.TASK_SCOPED,
|
|
agreement,
|
|
status: TASK_STATUS.DONE,
|
|
};
|
|
});
|
|
}
|
|
|
|
function buildPlanRows(phaseDir: string, diagnostics: Diagnostic[], planningRoot: string): { rows: PlanRow[]; scope: Scope } {
|
|
// GAP 1 (#2790 follow-up security review): `scanPhasePlans` (`plan-scan.cjs`)
|
|
// does its own `readdirSync(phaseDir)`, which FOLLOWS a directory symlink —
|
|
// so a phase directory that is itself a symlink escaping `planningRoot`
|
|
// would have its EXTERNAL filenames enumerated and surfaced via `file:
|
|
// toPosix(planFile)` below, even though the per-file `readDocument` guard
|
|
// already rejects the CONTENT. Contained before any enumeration happens, so
|
|
// an escaped phase directory contributes zero rows and zero filenames — the
|
|
// same degraded shape (`scope: unreadable`, empty `rows`) `scanPhasePlans`
|
|
// already returns when the directory cannot be listed at all, via the same
|
|
// `isPathContained` comparison `readDocument` uses for files (never a
|
|
// second, hand-rolled boundary check).
|
|
if (!isPathContained(phaseDir, planningRoot)) {
|
|
return { rows: [], scope: SCOPE.UNREADABLE };
|
|
}
|
|
const scan = planScan(phaseDir);
|
|
const supersededSet = new Set(
|
|
scan.allPlanFiles.filter((f: string) => !scan.planFiles.includes(f)),
|
|
);
|
|
|
|
const rows: PlanRow[] = scan.allPlanFiles.map((planFile: string) => {
|
|
const doc = readDocument(path.join(phaseDir, planFile), planningRoot);
|
|
if (doc.text === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.PLAN_UNREADABLE,
|
|
subject: toPosix(planFile),
|
|
detail: 'Plan file could not be read; its body is unknown. Sibling plans are unaffected.',
|
|
});
|
|
return {
|
|
id: planIdFromFile(planFile),
|
|
file: toPosix(planFile),
|
|
superseded: supersededSet.has(planFile),
|
|
objective: null,
|
|
wave: null,
|
|
dependsOn: [],
|
|
autonomous: true,
|
|
agentHint: null,
|
|
plannedFiles: [],
|
|
changedFiles: null,
|
|
hasSummary: false,
|
|
tasks: [],
|
|
scope: SCOPE.UNREADABLE,
|
|
};
|
|
}
|
|
|
|
const parsed = parsePlanDocument(doc.text);
|
|
const summaryFile = summaryForPlan(planFile, scan.summaryFiles);
|
|
let provenance: SummaryProvenance | null = null;
|
|
if (summaryFile !== null) {
|
|
const summaryDoc = readDocument(path.join(phaseDir, summaryFile), planningRoot);
|
|
if (summaryDoc.text === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.SUMMARY_UNREADABLE,
|
|
subject: toPosix(summaryFile),
|
|
detail: 'Summary file could not be read; file provenance for this plan is unknown.',
|
|
});
|
|
} else {
|
|
provenance = parseSummaryProvenance(summaryDoc.text);
|
|
}
|
|
}
|
|
|
|
return {
|
|
id: planIdFromFile(planFile),
|
|
file: toPosix(planFile),
|
|
superseded: supersededSet.has(planFile),
|
|
objective: parsed.objective,
|
|
wave: parsed.declaredWave,
|
|
dependsOn: parsed.dependsOn,
|
|
autonomous: parsed.autonomous,
|
|
agentHint: parsed.agentHint,
|
|
plannedFiles: parsed.filesModified,
|
|
changedFiles: provenance === null ? null : provenance.planFiles,
|
|
hasSummary: summaryFile !== null,
|
|
tasks: buildTaskRows(planFile, parsed, provenance, diagnostics),
|
|
scope: SCOPE.COMPLETE,
|
|
};
|
|
});
|
|
|
|
return { rows, scope: scan.scope };
|
|
}
|
|
|
|
// ─── UAT ──────────────────────────────────────────────────────────────────────
|
|
|
|
// `scope` and `foldScope` are, by decision, identical at every return site in
|
|
// this function as of #3078 round-8 — they are not accidentally in sync. The
|
|
// two-field shape is kept anyway because it lets `scope` (the row's own
|
|
// honest answer) and `foldScope` (what the caller folds into the milestone)
|
|
// diverge again later without a signature change, should some future gap
|
|
// class need to be reported on the row but exempted from the fold, or vice
|
|
// versa. If you find yourself "simplifying" this to one field, don't.
|
|
function buildUatRows(
|
|
phasesDir: string,
|
|
phaseDirName: string,
|
|
diagnostics: Diagnostic[],
|
|
planningRoot: string,
|
|
): { items: unknown[]; scope: Scope; foldScope: Scope } {
|
|
const phaseDir = path.join(phasesDir, phaseDirName);
|
|
// GAP 1 (#2790 follow-up security review): same rationale as
|
|
// `buildPlanRows` above — `readdirSync` below FOLLOWS a directory symlink,
|
|
// so an escaped phase directory must be rejected before enumeration, not
|
|
// after. Reuses the existing `UAT_UNREADABLE` diagnostic and degraded
|
|
// shape (the pre-existing "directory could not be listed" path below),
|
|
// rather than inventing a new diagnostic code for what is, from a
|
|
// consumer's perspective, the same non-answer.
|
|
if (!isPathContained(phaseDir, planningRoot)) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.UAT_UNREADABLE,
|
|
subject: phaseDirName,
|
|
detail: 'Phase directory could not be listed; UAT presence is unknown.',
|
|
});
|
|
return { items: [], scope: SCOPE.UNREADABLE, foldScope: SCOPE.UNREADABLE };
|
|
}
|
|
let entries: string[];
|
|
try {
|
|
entries = fs.readdirSync(phaseDir);
|
|
} catch {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.UAT_UNREADABLE,
|
|
subject: phaseDirName,
|
|
detail: 'Phase directory could not be listed; UAT presence is unknown.',
|
|
});
|
|
return { items: [], scope: SCOPE.UNREADABLE, foldScope: SCOPE.UNREADABLE };
|
|
}
|
|
|
|
const uatFiles = selectPhaseUatFiles(entries, phaseDirName);
|
|
if (uatFiles.length === 0) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.UAT_ABSENT,
|
|
subject: phaseDirName,
|
|
detail: 'No UAT document for this phase. This does not affect phase acceptance.',
|
|
});
|
|
return { items: [], scope: SCOPE.COMPLETE, foldScope: SCOPE.COMPLETE };
|
|
}
|
|
|
|
const items: unknown[] = [];
|
|
let scope: Scope = SCOPE.COMPLETE;
|
|
let foldScope: Scope = SCOPE.COMPLETE;
|
|
for (const file of uatFiles) {
|
|
const doc = readDocument(path.join(phaseDir, file), planningRoot);
|
|
if (doc.text === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.UAT_UNREADABLE,
|
|
subject: `${phaseDirName}/${file}`,
|
|
detail: 'UAT document exists but could not be read.',
|
|
});
|
|
scope = SCOPE.TRUNCATED;
|
|
foldScope = SCOPE.TRUNCATED;
|
|
continue;
|
|
}
|
|
// #3707-class false-clean, second surface (security review finding 1):
|
|
// `parseUatItemsWithStats`'s `headingsSeen` counts `### N.` test blocks
|
|
// that yielded ZERO items (a row missing its `result:` line, or otherwise
|
|
// unrecognised) — the audit-uat side (`cmdAuditUat`, above) already flags
|
|
// this as `parse_gap`. Reading the file successfully is not the same as
|
|
// deriving every row from it, so the gap is ALWAYS REPORTED.
|
|
//
|
|
// #3078 round-8 HIGH — NO FRONTMATTER KILL SWITCH. There is deliberately
|
|
// no `status !== 'complete'` term here. `cmdAuditUat` dropped its own such
|
|
// guard (see the long rationale at src/uat.cts, above `headingsSeen > 0`):
|
|
// a terminal status is an ASSERTION BY THE AUTHOR, and an assertion must
|
|
// not be able to switch off the detector that would contradict it. A guard
|
|
// here let one word of frontmatter turn a fence-straddled `result: blocked`
|
|
// into an affirmative `scope: "complete"` with ZERO diagnostics. The
|
|
// condition is `headingsSeen > 0` alone, matching src/uat.cts's own check
|
|
// line for line. Do not re-add a status term to "align the surfaces" — the
|
|
// surfaces are aligned precisely BY its absence.
|
|
//
|
|
// #3078 round-8 — REPORTING THE GAP AND WITHHOLDING THE PERCENTAGE ARE TWO
|
|
// DIFFERENT DECISIONS, so they get two different fields. `scope` is what
|
|
// this phase's own `uat.scope` reports: it stays honest and goes TRUNCATED
|
|
// for every gap, because a document that did not yield all its rows must
|
|
// never carry an affirmative `"complete"`. `foldScope` is what is handed to
|
|
// `worstScope` in the caller, and it is the one with teeth — a non-COMPLETE
|
|
// fold raises `phase_scope_degraded` AND, via `phaseScope`/`makeFraction`,
|
|
// withholds BOTH progress percentages for the WHOLE milestone.
|
|
//
|
|
// The two now agree: EVERY gap class degrades both, including the
|
|
// fence-suppression shortfall. `shortfallBlocks` is not exempted here
|
|
// because it is a single tally incremented at ONE site in the scan and
|
|
// spans BOTH a harmless closed-fence documentation sample AND a genuinely
|
|
// fence-straddled `result: blocked` row — exempting the tally cannot
|
|
// exempt only the harmless case, it also publishes a milestone percentage
|
|
// over a real unread outstanding row. `SCOPE.TRUNCATED` means the scan
|
|
// could not SEE part of the evidence (src/planning-scope.cts), which is
|
|
// exactly the fence-straddled case. The accepted over-report itself is
|
|
// unchanged and still documented at src/uat.cts; what changed is only
|
|
// that it no longer buys an exemption from the fold.
|
|
const { items: fileItems, headingsSeen } = parseUatItemsWithStats(doc.text);
|
|
items.push(...fileItems);
|
|
if (headingsSeen > 0) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.UAT_UNREADABLE,
|
|
subject: `${phaseDirName}/${file}`,
|
|
detail: `UAT document has ${headingsSeen} test block(s) with no parseable result; unresolved is not a complete answer.`,
|
|
});
|
|
scope = SCOPE.TRUNCATED;
|
|
foldScope = SCOPE.TRUNCATED;
|
|
}
|
|
}
|
|
return { items, scope, foldScope };
|
|
}
|
|
|
|
// ─── Progress ─────────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* ADR-3180 §7.6: the arithmetic is `clampPercent`'s alone (rule 1), a
|
|
* non-positive denominator is 0 not 100 (rule 2), numerator and denominator
|
|
* come from one scoped set (rule 3), and a scope other than COMPLETE renders NO
|
|
* percentage at all (rule 4).
|
|
*/
|
|
function makeFraction(completed: number, total: number, scope: Scope, subject: string, diagnostics: Diagnostic[]): Fraction {
|
|
if (scope !== SCOPE.COMPLETE) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.PERCENT_WITHHELD,
|
|
subject,
|
|
detail: `Scope is "${scope}"; a percentage derived from an incomplete read would be a confident wrong answer.`,
|
|
});
|
|
return { completed, total, percent: null, scope };
|
|
}
|
|
return { completed, total, percent: clampPercent(completed, total), scope };
|
|
}
|
|
|
|
/**
|
|
* The active plan position, from STATE.md's `## Current Position` block.
|
|
*
|
|
* ADR-3180 §7.7: `stateFieldValue` owns the #1760 frontmatter-then-body
|
|
* fallback ladder — this composes it, it does not re-derive it. The section
|
|
* slice is load-bearing: `Plan:` canonically lives under `## Current Position`,
|
|
* and a whole-document search would match a historical `Plan:` line in an
|
|
* archive section instead (#2956). A missing section is therefore reported as
|
|
* UNSCOPED rather than silently widened to the whole body.
|
|
*/
|
|
function buildActivePlan(statePath: string, planningRoot: string): { value: string | null; scope: Scope } {
|
|
const doc = readDocument(statePath, planningRoot);
|
|
if (!doc.exists) return { value: null, scope: SCOPE.UNSCOPED };
|
|
if (!doc.readable || doc.text === null) return { value: null, scope: SCOPE.UNREADABLE };
|
|
const fm = extractFrontmatter(doc.text, statePath) as Record<string, unknown>;
|
|
const body = stripFrontmatter(doc.text);
|
|
const slice = stateCurrentPositionSlice(body);
|
|
return stateFieldValue(fm, slice ?? body, 'plan', 'Plan', {
|
|
scope: slice === null ? SCOPE.UNSCOPED : SCOPE.COMPLETE,
|
|
});
|
|
}
|
|
|
|
// ─── Phase ROADMAP section (goal / dependencies) ──────────────────────────────
|
|
|
|
interface ScopedText {
|
|
value: string | null;
|
|
scope: Scope;
|
|
}
|
|
|
|
interface ScopedTextList {
|
|
value: string[];
|
|
scope: Scope;
|
|
}
|
|
|
|
/**
|
|
* The same `**Depends on:**` line `src/phase.cts`'s phase-insert path writes
|
|
* (`\n**Depends on:** Phase ${afterPhase}`) and `init.cts`'s own
|
|
* phase-listing scan reads (`init.cts:2359`) — mirrored verbatim here rather
|
|
* than re-derived.
|
|
*/
|
|
const DEPENDS_ON_LINE_RE = /\*\*Depends on(?::\*\*|\*\*:)\s*([^\n]+)/i;
|
|
|
|
/**
|
|
* A bold-annotation line — `**Label:** …` (colon inside the bold run) or
|
|
* `**Label**: …` (colon immediately after it). Matches `**Depends on:**`,
|
|
* `**Plans**:`, `**Goal:**`, and `**Cross-cutting constraints:**` alike.
|
|
*/
|
|
const BOLD_ANNOTATION_LINE_RE = /^\*\*(?:[^*\n]*:[^*\n]*\*\*|[^*\n]*\*\*:)/;
|
|
|
|
/** The bare `Plans:` checklist header `cmdRoadmapAnnotateDependencies` emits. */
|
|
const PLANS_CHECKLIST_HEADER_RE = /^plans:$/i;
|
|
|
|
/**
|
|
* The prose immediately under a `### Phase N: Name` heading, stopping at the
|
|
* first line that is METADATA rather than prose — issue #2790's own
|
|
* definition of per-phase "goal". The boundary matters because this payload
|
|
* already surfaces every one of those metadata lines in its own typed field
|
|
* (`**Depends on:**` -> `dependencies`, `**Plans**:` / `Plans:` + `- [ ]`
|
|
* rows -> `plans`, wave headers and `**Cross-cutting constraints:**` -> the
|
|
* per-plan rows) — folding them into `goal` too would both duplicate the
|
|
* data and hand a consumer raw Markdown to render. A sub-heading boundary is
|
|
* belt-and-braces: `collectSection` already bounds the body at the next
|
|
* heading.
|
|
*
|
|
* `null` when the section carries no such prose before hitting metadata (a
|
|
* real "no goal", not a failure).
|
|
*/
|
|
function extractGoalProse(sectionBody: string): string | null {
|
|
const proseLines: string[] = [];
|
|
for (const line of sectionBody.split(/\r?\n/)) {
|
|
const trimmed = line.trim();
|
|
if (/^\s{0,3}#{1,6}\s/.test(line)) break;
|
|
if (/^\s*(?:[-*+]|\d+[.)])\s/.test(line)) break;
|
|
if (BOLD_ANNOTATION_LINE_RE.test(trimmed)) break;
|
|
if (PLANS_CHECKLIST_HEADER_RE.test(trimmed)) break;
|
|
proseLines.push(line);
|
|
}
|
|
const text = proseLines.join('\n').trim();
|
|
return text.length > 0 ? text : null;
|
|
}
|
|
|
|
/**
|
|
* Phase tokens off a `**Depends on:**` line — value-level token extraction
|
|
* of ONE already-addressed line (mirrors `parseTraceability`'s Phase-cell
|
|
* token scan above), never a document-wide scan. `[]` when the line is
|
|
* absent.
|
|
*/
|
|
function extractDependencyTokens(sectionBody: string): string[] {
|
|
const m = DEPENDS_ON_LINE_RE.exec(sectionBody);
|
|
if (!m) return [];
|
|
return sortedUnique([...m[1].matchAll(/\d+(?:\.\d+)*/g)].map((t) => t[0]));
|
|
}
|
|
|
|
/**
|
|
* This phase's own ROADMAP.md section body — milestone-scoped via the SAME
|
|
* `extractCurrentMilestone` seam `planning-snapshot.cts`'s own ROADMAP
|
|
* consumers use (`planning-snapshot.cts:918`), never a document-wide walk.
|
|
* `collectSection` (ADR-2143) owns the heading walk; the predicate is built
|
|
* from the canonical `phaseMarkdownRegexSource` (`phase-id.cts`) anchor —
|
|
* the same start-of-heading anchor `roadmap-parser.cts`'s own phase-section
|
|
* lookups (`withPhaseSection`, `findRoadmapPhaseInContent`) use, so a
|
|
* sibling phase whose TITLE merely mentions this phase's number is never
|
|
* hijacked.
|
|
*/
|
|
function findPhaseRoadmapSection(cwd: string, roadmapText: string, phaseId: string): string | null {
|
|
const scoped = extractCurrentMilestone(roadmapText, cwd);
|
|
const headingRe = new RegExp(
|
|
`^(?:\\[[^\\]]{1,200}\\]\\s*)?Phase\\s+${phaseMarkdownRegexSource(phaseId)}(?=[\\s:(]|$)`,
|
|
'i',
|
|
);
|
|
const section = collectSection(scoped, (h) => headingRe.test(h.text));
|
|
return section ? section.body : null;
|
|
}
|
|
|
|
/**
|
|
* Issue #2790's Summary names "per-phase goal/dependency" as spec elements
|
|
* distinct from the STATUS evidence (`verification` / `roadmap_acceptance` /
|
|
* `uat`) phase rows already carry. Both fields fold the SAME three-way scope
|
|
* every other value in this module carries: `complete` when the section was
|
|
* found and parsed, `unscoped` when ROADMAP.md has no section for this
|
|
* phase, `unreadable` when ROADMAP.md itself could not be read. Never
|
|
* inferred — an absent `**Depends on:**` line is `[]`, not a degraded scope.
|
|
*
|
|
* `getRoadmapPhaseWithFallback` (`roadmap.cts`) and `findRoadmapPhaseInContent`
|
|
* (`roadmap-parser.cts`) were considered first (per this module's own
|
|
* "COMPOSED, NOT RE-DERIVED" rule) but both perform their OWN internal file
|
|
* read, bypassing this module's `readDocument` tri-state (exists/readable/
|
|
* text) — the exact distinction `unreadable` vs `unscoped` needs here, and
|
|
* the one every other section of this module gets via the same seam. Their
|
|
* `goal` extraction also targets an explicit `**Goal:**` bold line, not the
|
|
* free prose immediately under the heading the issue's own fixture expects.
|
|
* `collectSection` + `extractCurrentMilestone`, fed by this module's own
|
|
* `readDocument(paths.roadmap)` read, keeps both the read path and the
|
|
* "found vs missing vs unreadable" distinction singular.
|
|
*/
|
|
function buildPhaseGoalAndDependencies(
|
|
cwd: string,
|
|
roadmapDoc: { text: string | null; readable: boolean },
|
|
phaseId: string | null,
|
|
phaseDirLabel: string,
|
|
diagnostics: Diagnostic[],
|
|
): { goal: ScopedText; dependencies: ScopedTextList } {
|
|
if (!roadmapDoc.readable || roadmapDoc.text === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.ROADMAP_UNSCOPED,
|
|
subject: phaseDirLabel,
|
|
detail: 'ROADMAP.md could not be read; phase goal and dependencies are unknown, not empty.',
|
|
});
|
|
return {
|
|
goal: { value: null, scope: SCOPE.UNREADABLE },
|
|
dependencies: { value: [], scope: SCOPE.UNREADABLE },
|
|
};
|
|
}
|
|
if (phaseId === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.ROADMAP_UNSCOPED,
|
|
subject: phaseDirLabel,
|
|
detail: 'Phase directory name carries no recognizable phase number; ROADMAP section cannot be located.',
|
|
});
|
|
return {
|
|
goal: { value: null, scope: SCOPE.UNSCOPED },
|
|
dependencies: { value: [], scope: SCOPE.UNSCOPED },
|
|
};
|
|
}
|
|
const sectionBody = findPhaseRoadmapSection(cwd, roadmapDoc.text, phaseId);
|
|
if (sectionBody === null) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.ROADMAP_UNSCOPED,
|
|
subject: phaseDirLabel,
|
|
detail: 'ROADMAP.md has no section for this phase; goal and dependencies are non-answers, not empty.',
|
|
});
|
|
return {
|
|
goal: { value: null, scope: SCOPE.UNSCOPED },
|
|
dependencies: { value: [], scope: SCOPE.UNSCOPED },
|
|
};
|
|
}
|
|
return {
|
|
goal: { value: extractGoalProse(sectionBody), scope: SCOPE.COMPLETE },
|
|
dependencies: { value: extractDependencyTokens(sectionBody), scope: SCOPE.COMPLETE },
|
|
};
|
|
}
|
|
|
|
// ─── Entry points ─────────────────────────────────────────────────────────────
|
|
|
|
function buildPlanningInspect(cwd: string): Record<string, unknown> {
|
|
const diagnostics: Diagnostic[] = [];
|
|
const paths = planningPaths(cwd);
|
|
const planningExists = fs.existsSync(paths.planning);
|
|
if (!planningExists) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.PLANNING_ROOT_ABSENT,
|
|
subject: toPosix(paths.planning),
|
|
detail: 'No .planning/ directory; every section below is an empty non-answer, not an empty project.',
|
|
});
|
|
}
|
|
|
|
const snapshot = buildPlanningSnapshot(cwd);
|
|
|
|
if (snapshot.milestone.scope !== SCOPE.COMPLETE) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.ROADMAP_UNSCOPED,
|
|
subject: toPosix(paths.roadmap),
|
|
detail: `Milestone identity scope is "${snapshot.milestone.scope}"; no version is invented to stand in for it.`,
|
|
});
|
|
}
|
|
|
|
const milestoneValue = snapshot.milestone.value as { version?: string; name?: string | null } | null;
|
|
|
|
// Phase rows come from the WINDOWED set (this milestone's phases). A dir on
|
|
// disk that the roadmap never declares is an orphan, reported separately —
|
|
// it is not silently promoted into `phases`, and it is not silently dropped.
|
|
const windowed: string[] = snapshot.phaseDirs.value;
|
|
const windowedSet = new Set(windowed);
|
|
const orphans = snapshot.allPhaseDirNames.value
|
|
.filter((dir) => !windowedSet.has(dir))
|
|
.sort();
|
|
for (const orphan of orphans) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.ORPHAN_PHASE_DIR,
|
|
subject: orphan,
|
|
detail: 'Phase directory exists on disk but is not declared in the current milestone window.',
|
|
});
|
|
}
|
|
|
|
const checkboxes = snapshot.roadmapPhaseCheckboxes.value;
|
|
// ROADMAP checkboxes arrive keyed by the BARE phase token the ROADMAP prose
|
|
// carries ("1"), while phase rows are keyed by on-disk directory name
|
|
// ("01-auth"). Comparing them raw makes this field null for every
|
|
// real-world directory — an evidence channel that silently never fires.
|
|
// Both sides go through the phase-id owners so "1", "01" and "01-auth" are
|
|
// one phase.
|
|
const checkboxByPhaseKey = new Map<string, boolean>();
|
|
for (const [token, ticked] of Object.entries(checkboxes)) {
|
|
checkboxByPhaseKey.set(phaseKeyFromToken(token), ticked);
|
|
}
|
|
const phaseSnapshots = snapshot.phases.value as {
|
|
dir: string;
|
|
complete: boolean;
|
|
verificationStatus: string;
|
|
planCount: number;
|
|
summaryCount: number;
|
|
scope: Scope;
|
|
}[];
|
|
|
|
// Canonical per-directory identity keys (`phase-id.cts`'s
|
|
// `phaseKeyFromDir`), not a raw regex scrape — this is the set
|
|
// `buildRequirements` below matches Traceability tokens against via
|
|
// `phaseKeyFromToken`.
|
|
const knownPhaseKeys: Set<string> = new Set(windowed.map((dir) => phaseKeyFromDir(dir)));
|
|
|
|
// Read once, shared across every phase row's goal/dependencies lookup —
|
|
// the same `readDocument` seam every other document read in this module
|
|
// uses, never a second file-reading path.
|
|
const roadmapDoc = readDocument(paths.roadmap, paths.planning);
|
|
|
|
const phaseRows = phaseSnapshots.map((phase) => {
|
|
const phaseDir = path.join(paths.phases, phase.dir);
|
|
const plans = buildPlanRows(phaseDir, diagnostics, paths.planning);
|
|
const uat = buildUatRows(paths.phases, phase.dir, diagnostics, paths.planning);
|
|
// GAP 2 (#2790 follow-up security review): `readVerificationStatus`
|
|
// (`src/verification.cts`) is a shared owner with its own unguarded
|
|
// `readFileSync` — a `*-VERIFICATION.md` symlinked outside the planning
|
|
// root would leak an unrecognized `status:` value verbatim via its
|
|
// "Unexpected verification status '<value>'" `next_action` string. Fixed
|
|
// from THIS consumer's side via the injectable `opts.fs` seam that
|
|
// function already exposes, never by touching its signature — see
|
|
// `containmentEnforcingVerificationFs`'s doc comment. This same seam's
|
|
// `readdirSync(phaseDir)` guard also independently covers the
|
|
// escaped-phase-DIRECTORY case for this call site (GAP 1 above only
|
|
// gates `buildPlanRows`/`buildUatRows`, not this one).
|
|
//
|
|
// `src/plan-scan.cts`'s `isPlanSuperseded` similarly reads
|
|
// symlink-followed content with no containment of its own, but it is
|
|
// reached only via `scanPhasePlans(phaseDir)` inside `buildPlanRows`
|
|
// above (never touched here), leaks only a derived boolean
|
|
// (`superseded`) rather than document text, and GAP 1's directory
|
|
// containment check already covers the escaped-DIRECTORY case for it —
|
|
// so it needs no fix of its own.
|
|
const verification = readVerificationStatus(phaseDir, {
|
|
fs: containmentEnforcingVerificationFs(paths.planning),
|
|
});
|
|
|
|
const token = /^(\d+(?:\.\d+)*)/.exec(phase.dir);
|
|
const phaseId = token ? token[1] : null;
|
|
const { goal, dependencies } = buildPhaseGoalAndDependencies(cwd, roadmapDoc, phaseId, phase.dir, diagnostics);
|
|
|
|
// `uat.foldScope`, NOT `uat.scope` (#3078 round-8). The two currently
|
|
// agree at every call site — see `buildUatRows` for why the field is
|
|
// still kept separate — so folding either one here produces the same
|
|
// result today. `foldScope` is used because it is the field with teeth:
|
|
// it is what `worstScope` folds into the phase's overall scope, and a
|
|
// non-COMPLETE result here raises `phase_scope_degraded` and, via
|
|
// `makeFraction`, withholds the milestone's percentages. A phase whose
|
|
// UAT document could not be fully read must not contribute an
|
|
// affirmative completion to the milestone.
|
|
const folded = worstScope(phase.scope, plans.scope, uat.foldScope, goal.scope, dependencies.scope);
|
|
if (folded !== SCOPE.COMPLETE) {
|
|
diagnostics.push({
|
|
code: INSPECT_DIAGNOSTIC.PHASE_SCOPE_DEGRADED,
|
|
subject: phase.dir,
|
|
detail: `Phase evidence is incomplete (scope "${folded}").`,
|
|
});
|
|
}
|
|
|
|
return {
|
|
dir: phase.dir,
|
|
phase_id: phaseId,
|
|
complete: phase.complete,
|
|
goal,
|
|
dependencies,
|
|
// The three evidence sources are reported SIDE BY SIDE and never folded
|
|
// into one verdict — folding them is precisely the confidently-wrong
|
|
// composite ADR-3180 exists to remove.
|
|
verification: {
|
|
status: verification.status,
|
|
next_action: verification.next_action ?? null,
|
|
},
|
|
roadmap_acceptance: {
|
|
checkbox: checkboxByPhaseKey.has(phaseKeyFromDir(phase.dir))
|
|
? (checkboxByPhaseKey.get(phaseKeyFromDir(phase.dir)) as boolean)
|
|
: null,
|
|
// ADR-3180 §7.4: a ticked checkbox is a human annotation with no
|
|
// machine authority. Stated in the payload so a consumer cannot
|
|
// mistake it for a completion signal.
|
|
authoritative: false,
|
|
},
|
|
uat: { unresolved: uat.items, scope: uat.scope },
|
|
plan_count: phase.planCount,
|
|
summary_count: phase.summaryCount,
|
|
plans: plans.rows,
|
|
scope: folded,
|
|
};
|
|
});
|
|
|
|
const requirements = buildRequirements(paths.requirements, knownPhaseKeys, diagnostics, paths.planning);
|
|
|
|
const phaseScope = worstScope(
|
|
snapshot.phaseDirs.scope,
|
|
snapshot.phases.scope,
|
|
...phaseRows.map((p) => p.scope),
|
|
);
|
|
const acceptedPhases = makeFraction(
|
|
phaseRows.filter((p) => p.complete).length,
|
|
phaseRows.length,
|
|
phaseScope,
|
|
'progress.accepted_phases',
|
|
diagnostics,
|
|
);
|
|
const completedPlans = makeFraction(
|
|
phaseRows.reduce((sum, p) => sum + p.summary_count, 0),
|
|
phaseRows.reduce((sum, p) => sum + p.plan_count, 0),
|
|
phaseScope,
|
|
'progress.completed_plans',
|
|
diagnostics,
|
|
);
|
|
|
|
return {
|
|
schema_version: PLANNING_INSPECT_SCHEMA_VERSION,
|
|
generated_from: {
|
|
cwd: toPosix(cwd),
|
|
planning_root: planningExists ? toPosix(paths.planning) : null,
|
|
},
|
|
milestone: {
|
|
version: milestoneValue && milestoneValue.version ? milestoneValue.version : null,
|
|
name: milestoneValue && milestoneValue.name ? milestoneValue.name : null,
|
|
scope: snapshot.milestone.scope,
|
|
},
|
|
// Three DISTINCT STATE.md facts, each from its own source. Collapsing any
|
|
// two of them is the confidently-wrong composite this schema exists to
|
|
// avoid: `Status:` is a lifecycle label, `Plan:` is a position.
|
|
active: {
|
|
phase: { value: snapshot.currentPhaseLabel.value, scope: snapshot.currentPhaseLabel.scope },
|
|
plan: buildActivePlan(paths.state, paths.planning),
|
|
status: { value: snapshot.stateStatus.value, scope: snapshot.stateStatus.scope },
|
|
},
|
|
phases: phaseRows,
|
|
orphan_phase_dirs: orphans,
|
|
requirements: requirements.rows,
|
|
progress: {
|
|
accepted_phases: acceptedPhases,
|
|
completed_plans: completedPlans,
|
|
},
|
|
diagnostics,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* `planning inspect` — emit the schema-v1 snapshot.
|
|
*
|
|
* `output()` is the spill seam: a payload over 50 KB is written to a tmpfile and
|
|
* returned as `@file:<path>`, which `gsd-tools`' `resolveAtFileOutput` resolves
|
|
* transparently on stdout. Bypassing `output()` would lose that for free.
|
|
*/
|
|
function cmdPlanningInspect(cwd: string, raw: boolean): void {
|
|
output(buildPlanningInspect(cwd), raw);
|
|
}
|
|
|
|
const planningInspect = {
|
|
PLANNING_INSPECT_SCHEMA_VERSION,
|
|
INSPECT_DIAGNOSTIC,
|
|
TASK_STATUS,
|
|
PROVENANCE,
|
|
AGREEMENT,
|
|
buildPlanningInspect,
|
|
cmdPlanningInspect,
|
|
};
|
|
|
|
export = planningInspect;
|