Files
msd-core/scripts/lint-phase-enumeration-drift.cjs
Tom Boucher abf3cf7c25 fix(#3458): scan archived milestone phases, and make [A] Acknowledge actually suppress (#3555)
* fix(#3458): scan archived milestone phases in the four audit-open scanners

`query audit-open` resolved exactly one phase root, `.planning/phases/`. When a
milestone closes its phase directories move to
`.planning/milestones/v<X.Y>-phases/`, so an item still unresolved at that
moment — the `[R]/[A]/[C]` prompt accepts "accept" and "carry forward", not only
"resolve" — became invisible to the v1.1 pre-close audit and every audit after
it. The window in which an unresolved item is visible to this gate was exactly
one milestone wide, and nothing announced when it closed.

Reproduced before fixing, with byte-identical artifacts in the two layouts and
the active layout as the control:

  active   → has_open_items=true   deferred=1  uat_gaps=1  total=2
  archived → has_open_items=false  deferred=0  uat_gaps=0  total=0

`scanDeferredItems`' own doc comment names this as the thing it was built to
prevent — "phase directories archive to `milestones/vX.Y-phases/` (#1871) and
the entry leaves the live tree having never been triaged" — while the
implementation eleven lines below cannot read that path. It catches an entry at
its own milestone close and goes blind at precisely the transition the comment
describes.

This is not cosmetic under-reporting. `auditOpenArtifacts` sums all nine
category counts into `counts.total` and returns `has_open_items: counts.total >
0`, so four blind scanners can flip the gate's headline boolean and let
`/gsd-complete-milestone` assert a clean close it never verified. In a
fully-archived project `.planning/phases/` may not exist at all, and the
scanners' `if (!fs.existsSync(phasesDir)) return []` produced a value
indistinguishable from "nothing is open".

## One enumeration, not four

The four scanners each hand-rolled the same active-only walk. They now share
`listAuditPhaseTargets(planDir, cwd)`, which yields both roots — the shape of
fix epic #3473's B2 asks for, and the reason the fix is one seam rather than
four edits.

Three properties are load-bearing:

  * the ACTIVE enumeration is unchanged — still a raw `readdirSync`, NOT
    `listMilestonePhaseDirs`. These scanners are deliberately not
    milestone-filtered today, and switching would silently add window and
    sentinel filtering: a behavior change belonging to #3372, not here.
  * a missing or unreadable active root skips that half instead of returning
    early. That early return WAS the bug in a fully-archived project.
  * archived dirs are deliberately NOT milestone-filtered, per the comment
    `src/uat.cts` already carries: archived phases belong to past milestones by
    definition, so applying the current-milestone filter discards every one and
    silently reinstates this bug.

Each item now carries `archived_milestone` when it comes from a closed
milestone, matching how the sibling module already labels archived results —
without it an operator triaging `[R]/[A]/[C]` cannot tell a live item from one
carried over. Additive: no existing test or doc asserted an exact key set.

`scripts/lint-phase-enumeration-drift.cjs`'s exemption list for this file drops
from the four scanner names to the single helper, since that is now the only
place the enumeration lives.

## Tests

Written failing-first and confirmed red for the right reason before the fix, all
four driven through the real `audit-open` CLI rather than private functions:
archived-only (was 0/0/0/0 with `has_open_items=false`, now 1/1/1/1 true),
mixed active+archived (was 1/1/1/1 — the archived half dropped — now 2/2/2/2),
active-only unchanged, and an all-resolved archived phase contributing 0.

That last one passed vacuously before the fix, because the archived path was not
reached at all; it was re-verified as genuinely discriminating afterward by
flipping one archived item to unresolved and watching the count rise.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#3458): restore the scan_error sentinel and show archive provenance

Adversarial review found one BLOCKER that the previous revision introduced,
which a green remote-runner suite did not catch because nothing in the tree
asserts `scan_error` at all.

## The regression

Consolidating four hand-rolled walks into `listAuditPhaseTargets` swallowed the
active-root `readdirSync` throw in a bare `catch {}`. Pre-fix each scanner
returned `[{scan_error: true, …}]`; after, each returned `[]`. Measured with
`.planning/phases` created as a FILE (so `existsSync` passes and `readdirSync`
throws ENOTDIR):

  before this fix: uat_gaps/verification_gaps/context_questions/deferred_items
                   each `[{"scan_error":true,…}]`
  the regression:  each `[]`

`complete-milestone.md` re-runs `audit-open --json` and reads those counts, so a
machine consumer could no longer tell "I/O failed" from "verified clean" — the
exact conflation this issue exists to remove, reintroduced on the failure path.
`listAuditPhaseTargets` now reports `activeUnreadable` and each scanner pushes
the sentinel shape recovered verbatim from `origin/next`, not reinvented.

The docstring claiming the active enumeration was "UNCHANGED" was false while
that sentinel was missing, and is corrected to state what is actually preserved.

An unreadable ARCHIVED root deliberately gets NO sentinel: there was no archived
read before, so there is no consumer contract to preserve, and adding one would
conflate the ordinary "no milestones archived yet" state with a real I/O failure.

## The operator could not see the archive

`formatAuditReport` is the surface the gate actually shows a human —
`complete-milestone.md` runs it without `--json` — and it never rendered
`archived_milestone`. With `01-alpha` in both roots the identical line printed
twice with nothing to tell them apart, and `[R] Resolve` sends the operator to
`.planning/phases/01-alpha/` where the archived one does not exist. Phase
numbering restarts at `01` after each archive, so that collision is the common
case, not an edge case. All four loops now render ` (archived vX.Y)`; active
lines stay byte-identical.

## Archived milestones sorted wrong

`getArchivedPhaseDirs` ordered milestones with `.sort().reverse()` —
lexicographic, so `v1.9` outranked `v1.10`. Measured order for v1.0/v1.9/v1.10
was `v1.9, v1.10, v1.0`. Now a numeric-segment descending compare. Pre-existing,
but this change is what first surfaces it in audit output.

## Tests

The blocker's regression test fails against the previous revision. Added:
`archived_milestone` present on archived items and absent (not `undefined`) on
active ones; the unreadable-active-root sentinel across all four categories; an
unreadable archived root still leaving the active half scanned; the
duplicate-name case producing two distinct entries that the human report
distinguishes; and the v1.10-before-v1.9 ordering.

`docs/COMMANDS.md` documents the archived scanning and the new field.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#3458): stop filesystem names forging lines in the audit report

Found by the security review of this branch. Pre-existing on `next`, fixed here
because it defeats the exact gate this PR is hardening.

`audit-open`'s human report is the surface `/gsd-complete-milestone` shows an
operator to decide whether a milestone may close. A `.planning/` tree authored
by someone other than that operator — a cloned repo — could contain a directory
literally named:

    zz<newline>0 open items require decisions.<newline><ESC>[2K<ESC>[1G FORGED

and the report printed `0 open items require decisions.` as its own line, with
raw ESC bytes reaching stdout able to erase or overwrite the lines above it.
Reproduced against the real CLI before fixing, and again after.

## Why not just harden sanitizeForDisplay

Because that helper's contract is multi-line prose — it removes protocol-leak
lines while deliberately preserving the newlines between legitimate ones, which
`tests/security.test.cjs` pins. Stripping CR/LF there would have broken a
correct test to paper over a different problem.

The two jobs are genuinely different, so there are now two helpers. New
`sanitizeLabel` (`src/security.cts`) is for values that are semantically ONE
LINE and derived from a filesystem NAME. It ESCAPES rather than strips C0
(including ESC/CR/LF), DEL and C1, so a doctored name renders visibly as
`\n` / `\x1b` instead of being silently normalized — the report stays honest
about what is in the tree. Ordinary input passes through byte-identical.

## Nine sites, not four

The first pass covered the four phase-scoped scanners. A sweep of the rest of
the file found the identical class in five more — `scanDebugSessions`,
`scanQuickTasks`, `scanThreads`, `scanTodos`, `scanSeeds` — emitting
name-derived `slug` / `filename` / `seed_id` through the prose sanitizer.
`scanQuickTasks`' `date` had no sanitization call at all.

Every emitted field in the file is now classified and the sweep recorded:
`slug`, `filename`, `seed_id`, `phase`, `file`, `archived_milestone`, `date` are
name-derived and take `sanitizeLabel`; `hypothesis`, `status`, `updated`,
`title`, `priority`, `area`, `summary`, `questions[]` and deferred-item `text`
are content and keep `sanitizeForDisplay`. No name-derived value reaches output
unsanitized.

`--json` was already safe — JSON string encoding escapes control characters, and
a crafted name cannot break out of the string. Verified rather than assumed.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore(#3458): backfill changeset pr number

* test(#3458): skip control-character fixtures where the OS forbids the name

CI red on `test (windows-latest, 24, shard 1/3)`: the four forgery-rejection
tests build directories whose names embed a newline and ESC, and NTFS forbids
control characters in path components, so `mkdir` threw ENOENT.

The remote runner is Linux-only, so it could not have caught this class.

Semantically the skip is honest rather than a workaround: on Windows the
directory-name forgery vector does not exist, because the OS refuses to create
the name. The sanitizer's own behavior stays covered there by the
`sanitizeLabel` unit tests, which are pure string tests with no filesystem
calls — verified.

Uses the repo's established capability-probe convention
(`tests/adr-index-gate.test.cjs`'s `trySymlink`), which `t.skip()`s on the real
errno rather than branching on `process.platform`, and whose comment gives the
reason: a bare `return` "would silently report a PASS ... and hide the gap this
guard exists to close". A skipped test is visibly skipped.

Swept every test added on this branch for names Windows would reject or POSIX
path assumptions; these four were the only ones.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* feat(#3458): make [A] Acknowledge actually suppress, without overwriting a verdict

Making archived phases visible exposed the other half of the problem: an item
unresolved at a milestone close now resurfaces at every later close forever,
because `[A] Acknowledge` wrote a prose block to STATE.md that
`auditOpenArtifacts` never reads. `verified_closeout` became unreachable and the
gate degraded to a mandatory `[A]` every time.

## The prompt does not change

`[A] Acknowledge all` already promises "document as deferred and proceed with
close". It documented but never deferred. This makes `[A]` do what it says.
`[R]` and `[C]` stay abort paths. No "carry forward" option is invented — an
item that is not acknowledged simply keeps surfacing, which is the default.

## The marker lives inside the artifact

Not a ledger. The audit mints no ids and has no stable identity — `phase` is a
token that collides across directories, `file` for deferred items is a constant,
and identity otherwise degrades to the item's own prose after a lossy sanitizer.
Any ledger must re-derive that key every close, so a reworded item silently
un-suppresses or, worse, mis-suppresses a different one. Storing the
acknowledgment next to the thing it suppresses makes that class of bug
structurally impossible, and it is the pattern `src/uat.cts` already argues for
with `deferred-items.md`'s in-place `status: resolved`.

## The marker is verdict-preserving and self-invalidating

`status:` is never overwritten — writing `resolved` into an unresolved UAT would
be a lie in the artifact of record, and the disclosure has to be additive.

    audit_acknowledged:
      milestone: v1.0
      at: 2026-08-15
      status: gaps_found      # snapshot of what was true when acknowledged

Suppression applies ONLY while the snapshot still matches reality: `status` for
seven categories, `question_count` for context questions, and for deferred items
a new per-entry `status: acknowledged` distinct from `resolved`, which keeps
meaning "actually fixed". Change the artifact and the acknowledgment stops
applying, so the item comes back on its own.

That is what makes re-opening answer itself with no extra state, and it fails in
the safe direction: a stale acknowledgment can never hide a NEW problem. A
malformed marker is treated as absent — a bad marker must never silence an item.

The check is ONE shared `isAuditItemAcknowledged`, not nine copies. This file has
already been through that defect family twice in this PR.

## Observable, not silent

`audit-open --json` now reports an `acknowledged` count beside `counts`, so a
reviewer can tell a close that is clean because things were fixed from one that
is clean because things were silenced.

## Writer

New `audit-open acknowledge` verb snapshots current state itself, so the marker
is never hand-authored from workflow prose — the gap that left the STATE.md
block with no writer, no schema and two conflicting formats. Writes route
through the existing path-confinement seam.

## Two deliberate limits, failing closed

Heading-delimited deferred entries (#3457) are REFUSED with
`unsupported_heading_shape` rather than edited, because mapping a heading entry
back to its exact source span is not safely derivable when headless and heading
entries interleave in one file. A loud refusal beats a mis-targeted write.

A quick task with no summary gets one created to carry the marker, since there
is otherwise nowhere to put it.

## Tests

Self-invalidation is the important one and is covered per category: acknowledge,
then change the status or question count, and the item resurfaces. Also
malformed markers not suppressing, `status:` byte-unchanged after acknowledging,
the writer refusing a path outside the project, and the four original #3458
scenarios unchanged.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* feat(#3458): wire [A] to the acknowledge verb and converge the disclosure table

Consumer side of the suppression seam.

## The workflow stops hand-authoring the mechanism

`[A]` now calls `audit-open acknowledge` once per open item, then writes the
STATE.md `## Deferred Items` table as before. The table stays as a
human-readable disclosure; it is no longer the mechanism. That closes the gap
where the block had no writer, no schema and no reader — the marker is now
written by the tool, which snapshots current state itself.

The `[R]` / `[A]` / `[C]` prompt is unchanged, `[C]` still means "Cancel — exit
without closing", and no carry-forward option is invented.

The all-clear branch now distinguishes a close that is clean because items were
FIXED from one that is clean because they were ACKNOWLEDGED, using the
`acknowledged.total` count, and carries that into the MILESTONES.md disclosure
line beside the existing override count. A clean close that was bought with
acknowledgments should say so.

## Format drift resolved

Two incompatible `## Deferred Items` shapes shipped simultaneously — 3 columns
in the workflow, 4 in the template, with different body lines. Converged on one
5-column shape carrying the source Milestone, since archived items now appear
and the archived-milestone disambiguator was previously discarded at write time.
The workflow enumerates the categories instead of trailing off in `...`.

## Ack fragment bookkeeping

`complete-milestone.md` grows 6,764 bytes (31,228 → 37,992; cap 61,440), covered
by a new `tests/emitted-drift-acks/3458-*.json`.

`2962-zsh-nomatch-for-glob-portability.json`'s `complete-milestone.md` entry is
REMOVED — the no-duplicate-path rule hard-blocks two sources naming one path.
That entry is spent: the nullglob shim it acknowledges is present in both
`origin/next` and the CI emitted baseline `fd2b97a5`, so its ripple is already
absorbed and it can never clear anything again — verified directly, not assumed,
and the gate's own message directs deleting spent entries. Its other three files'
entries are untouched.

`scripts/sync-runtime-launcher.cjs` wanted to rewrite `explore.md` as well —
pre-existing drift unrelated to this change, reverted. `complete-milestone.md`
still carries exactly one canonical preamble.

Docs cover the verb's real flag surface, the marker's verdict-preserving and
self-invalidating behavior, and the new `acknowledged` count. A second `Added`
changeset covers the verb, since the existing `Fixed` fragment describes only
the archived-phase scanning.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#3458): close three blockers in the acknowledgment seam

Adversarial review of the seam. Three BLOCKERs, one of which disproves a safety
claim I published in the PR body, the changeset and the docs.

## The claim was false; the code is fixed rather than the claim softened

I wrote that "a stale acknowledgment can never hide a NEW problem". It could.
`context_questions` snapshotted only the question COUNT, so replacing two
acknowledged questions with two brand-new blockers kept the item suppressed.
`uat_gaps` snapshotted only `status`, so adding five more pending scenarios
(`open_scenario_count` 1→6) kept it suppressed.

The snapshot now identifies CONTENT, not size: a digest of the whole question
set, and a status + open-scenario-count composite. Any edit invalidates. The
other seven categories were checked and their single tracked dimension is
already the whole story. Both disproofs now resurface the item.

## Writing to the wrong line, and reporting success

`acknowledgeDeferredItem` built an unanchored regex and exec'd it over the whole
file while match-selection and the ambiguity guard ran over the section body
only, so the write landed at the first match ANYWHERE. A file with `# Notes`
holding `- Fix the parser` above a `## Deferred Items` section holding the same
bullet: the CLI exited 0 saying `acknowledged: true`, injected `status:
acknowledged` under `# Notes`, and re-audit still reported the entry open. It
corrupted unrelated content, suppressed nothing, and claimed success — and since
`--file` is unconstrained the same path could inject into a UAT or VERIFICATION
body.

Matching is now anchored to the selected section, and the matched span is
re-verified against the selected entry before any write; a mismatch refuses with
`match_verification_failed` rather than writing.

## Acknowledging todos hid the ones never shown

`scanTodos` capped at five files and then checked acknowledgment. With seven
todos, acknowledging the five that were LISTED drove `todos: 0`,
`has_open_items: false`, and items six and seven never appeared in any later
scan. The workflow's own "repeat until no todos items" remedy terminates after
one pass. Pre-feature this was unreachable because the count was pinned at five.

That is silent over-suppression — the exact direction this PR exists to remove.
Acknowledged items are now filtered BEFORE the display cap, so unacknowledged
todos beyond it still drive the count.

## The [A] branch could not fail closed

Every acknowledge call sat in a `cmd | while read` pipeline with no status
accumulation, so any refusal was discarded and the close proceeded as
`override_closeout`. Separately, `io.output` swaps payloads over 50000 chars for
an `@file:<path>` sentinel — every `jq` would then fail, every loop body run
zero times, nothing be suppressed, and the close happen anyway.

Both closed: failures accumulate across all invocations and halt before close,
and the sentinel is dereferenced using the same pattern `verify_readiness`
already uses for `INIT_MANAGER`. Quoting was verified sound by the review and is
left alone.

## Also

Suppression is now visible in the human report, not only `--json` — the
"clean because fixed vs clean because silenced" distinction was promised for the
surface an operator actually reads.

The CRLF-preservation branches in the writer were dead: every `.md` write goes
through `_normalizeMd`, which normalizes line endings and blank lines whatever
the writer does. Deleted and documented rather than left as code that cannot run.

## Why these shipped

The review named it exactly: there was no coverage for
`unsupported_heading_shape`, `ambiguous`, `not_found`, duplicate-text
mis-targeting, todos beyond the cap, or CRLF. All are now tested, alongside both
snapshot disproofs and the mixed-section fixture.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(#3458): align the items-open footer wording with its assertion

Remote runner red on one test: the items-open footer must match
`/previously acknowledged item/i`.

The disclosure was NOT missing — the items-open branch already printed
"N additional items previously acknowledged and still suppressed." The word
order simply did not match the regex the test in the same change asserts. A
wording mismatch between my own test and my own implementation, not a behavior
gap.

Reworded to "N previously acknowledged items also suppressed above the M open
items", which satisfies the assertion and states the relationship between the
two counts more plainly than the original did.

Swept `formatAuditReport` for other branches that could skip the tally: the only
early return is the all-clear path, which already discloses it. `scan_error`
sentinels are filtered per category and excluded from `counts.total`, so an
all-error project falls through to that same branch. No inconsistency remains.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#3458): splice by carried span, digest the untruncated question set

Security review of the writer. Both findings are the same shape, and both are
cases where an earlier fix of mine was incomplete in the same direction: a value
derived for DISPLAY was reused for an IDENTITY or LOCATION decision.

## Writing to the wrong entry, again

The previous fix anchored matching to the `## Deferred Items` SECTION but still
re-found the entry inside it with an unanchored regex, so the write landed at
the first SUBSTRING occurrence rather than the entry's own span. The
`match_verification_failed` guard could not catch it, because the mis-targeted
span is byte-identical to the target.

Probe-confirmed, in a cloned repo's own artifact:

    - CRITICAL unfixed auth bypass
      see also: - minor typo
    - minor typo

Acknowledging "minor typo" appended `status: acknowledged` into the CRITICAL
entry, suppressing it at every future close, while the typo stayed open — exit
0, `"acknowledged": true`. A variant where the target text appears inside
unrelated prose split that line mid-sentence, acknowledged nothing, and still
exited 0, so the workflow's `ACK_FAILURES` halt never fired.

Fixed structurally rather than with a better regex: `splitGapsEntriesWithSpans`
carries each entry's own character span out of the splitter, and the write
splices by that recorded span. The location is already known at selection time —
re-deriving it by searching was the entire defect class. Added as a sibling so
`splitGapsEntries`' three existing callers are untouched. With index-splicing,
`match_verification_failed` becomes a genuine independent cross-check instead of
a guard that could never fire.

## The digest was blind past the third question

`deriveOpenQuestions` truncated to three questions, and clamped each to 200
chars, BEFORE the digest hashed it — so the snapshot could not see the fourth
and later. Ship three innocuous questions, acknowledge, then add real blockers,
and they are permanently invisible: measured `open=0, acknowledged=1`, report
"All artifact types clear."

That is the same self-invalidation property this digest was added to guarantee
one revision ago. The digest now covers the untruncated list; truncation is
display-only.

Found while fixing it: the previous digest joined on a literal raw NUL byte
embedded in the source — collisions are constructible, and reachable through
attacker-controlled YAML `\x00` escapes. Verified both ways. Replaced with a
length-prefixed encoding so no two question sets can collide by concatenation.

## Sweep

Because this is the third incomplete fix on this seam, every identity and
location derivation was swept for the display-vs-identity confusion: uat_gaps
uses status plus a full-content count, the other seven categories use a scalar
status or presence, the deferred `--text` identity is never truncated, and all
five flat categories resolve their file by path rather than by content search.
No further instances.

Closes #3458

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(#3458): correct two assertions that over-reached the measured behavior

Remote runner red on two of the F1 tests. The source is correct — reproduced
both fixtures against the built CLI — and both failures were bugs in the
assertions I wrote. `src/` is untouched by this commit.

The first is worth recording. It computed the CRITICAL entry's block as

    content.slice(content.indexOf('- CRITICAL'), content.indexOf('- minor typo'))

and `indexOf` found the FIRST SUBSTRING occurrence, which lives inside that
entry's own continuation line `  see also: - minor typo`. The block was
truncated mid-line, so the assertion could never match. The test committed the
exact first-substring-match mistake it exists to catch, one revision after that
mistake was fixed in the source.

The second asserted `deferred_items === 0` after acknowledging the typo entry,
but the decoy `- Note: reference - minor typo elsewhere, ignore` is itself an
open entry and was never acknowledged, so the correct count is 1. It now also
asserts WHICH item remains open — that is what actually proves the right entry
was suppressed, and the original assertion would have passed even if both had
been silenced.

Both now derive their expectations from measured CLI output. A comment records
that the write seam normalizes markdown (`_normalizeMd` inserts a blank line
before a list item following a non-list line) so the inserted line is not later
mistaken for a regression; that is repo-wide behavior for every `.md` write
through the single write projection, not something this change should diverge
from.

Root cause of both: the previous two dispatches verified behavior with direct
CLI probes but never executed the test file, so assertions could over-reach what
had actually been measured. Every other assertion added in those two commits has
since been re-derived from real output; no further mismatches.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 17:00:52 -04:00

476 lines
26 KiB
JavaScript

#!/usr/bin/env node
'use strict';
/**
* Anti-divergence drift guard for the PHASE-ENUMERATION seam (epic #3180,
* issue #3185, ADR-3180 Decision 1 row "Phase enumeration").
*
* `src/phase-locator.cts` is the SINGLE canonical owner of "which phase
* directories belong to the current milestone" — `listMilestonePhaseDirs`.
* `src/phase-id.cts` is the SINGLE canonical owner of "is this phase id a
* reserved sentinel (Phase 0 / Phase 999.x)" — `isSentinelPhaseId` /
* `SENTINEL_RANGES`. Before these existed the derivation had four
* independent implementations, and the sentinel rule had five copies across
* three different regexes that disagreed about Phase 0 (see
* `listMilestonePhaseDirs`'s own doc comment). Every other module that
* hand-rolls a `readdirSync` over the phases directory, or hand-rolls a
* `999`-shaped sentinel test, is a re-derivation that can silently drift
* from one of these two owners — the same generative-fix-divergence class
* `lint-plan-count-drift.cjs` and `lint-milestone-window-drift.cjs` exist to
* remove, now applied to the phase-enumeration seam.
*
* Per ADR-3180 Decision 4(a) this guard discovers call sites by SCANNING THE
* WHOLE `src/` TREE, not by consulting an allowlist of known files — an
* allowlist only measures re-derivations in files someone remembered to
* list. Exemptions below are FUNCTION-SCOPED with a written reason, never a
* bare file allowlist, mirroring `lint-plan-count-drift.cjs`'s and
* `lint-milestone-window-drift.cjs`'s precedent.
*
* TWO INDEPENDENT DETECTORS. A line matching EITHER is a violation:
*
* DETECTOR 1 (enumeration): a single source line carrying BOTH
* (a) `readdirSync`, AND
* (b) a phases-directory token — the identifier `phasesDir`, or a
* quoted/backticked `'phases'` string.
* This is the shape every consumer used before routing through the owner.
*
* DETECTOR 2 (sentinel): a single source line carrying a sentinel-range
* literal outside the owner —
* - a bare `999` inside a regex literal or a quoted/backticked string
* (e.g. `/^999\b/`, `/^999(?:\.|$)/`, `'999'`), OR
* - a numeric comparison against 999 (`=== 999`, `== 999`, `!== 999`),
* or a bare reference to `SENTINEL_RANGES` outside its owner.
* Deliberately NARROW, mirroring the sibling guards' token discipline: the
* `999` must be a STANDALONE digit run (no digit immediately before or
* after it, inside the literal or right after the comparison operator), so
* unrelated arithmetic (`total + 1999`, `=== 9990`) never fires — only a
* line that actually spells the reserved sentinel value fires.
*
* Owner files (exempt by construction — each not only DEFINES its half of
* the grammar but composes it at internal call sites that are the canonical
* implementation, not copies of it):
* - `src/phase-locator.cts` — defines `listMilestonePhaseDirs`, the
* enumeration owner, and legitimately calls `readdirSync` on the phases
* dir inside it.
* - `src/phase-id.cts` — defines `isSentinelPhaseId` and
* `SENTINEL_RANGES`, the sentinel owner.
*
* FUNCTION-SCOPED EXEMPTIONS (per ADR-3180 Decision 4(a) — a written reason,
* never a bare file allowlist, so an unrelated re-derivation added anywhere
* ELSE in these same files is still caught). Generalizing #3183's rule ("a
* diagnostic about file NAMING wants the physical set; only a question about
* outstanding WORK wants the live set"): a LOOKUP, DIAGNOSTIC or ARCHIVAL
* pass wants the physical set; only "which phases belong to this milestone"
* wants the scoped set.
* - `src/roadmap.cts` `cmdRoadmapAnalyze`: builds `_phaseDirNames` as a
* heading->directory LOOKUP INDEX, not a milestone enumeration. It must
* see the PHYSICAL set so a heading already scoped by
* `extractCurrentMilestoneScoped` can find its directory; filtering it
* through the owner would scope the same set twice.
* - `src/verify.cts` `cmdValidateHealth`: a project-wide HEALTH-CHECK sweep
* (config drift, phase-directory naming, duplicate-directory collisions,
* unsummarized-plan detection) — same "sweep everything, report gaps"
* shape as `collectDiskPhases` and the `audit.cts` scanners; it must see
* every phase directory regardless of milestone window to catch a
* naming/duplicate defect wherever it lives.
* - `src/verify.cts` `cmdVerifySchemaDrift`: resolves ONE caller-supplied
* `phase` argument to its directory (falling back to an exact-name
* match) — a single-phase LOOKUP, not a current-milestone enumeration.
* - `src/init.cts` `detectHasPriorPhases`: answers "has this project EVER
* completed a phase", explicitly excluding the current one. A history
* probe across all milestones, not a current-milestone enumeration.
* - `src/init.cts` `detectUiPhaseActive`: its `readdirSync` targets a
* SINGLE already-resolved phase directory's own FILES
* (`phases/<dirName>`) to check for a `*-UI-SPEC.md` file — not the
* phases directory itself. It never enumerates which phases exist at
* all, so it is not this derivation, only shaped like it textually.
* - `src/init.cts` `cmdInitMilestoneOp`: `diskPhaseDirs` is a heading->
* directory LOOKUP INDEX (same role as `cmdRoadmapAnalyze`'s
* `_phaseDirNames` below) — the ROADMAP heading scan just above it
* already scopes `roadmapPhaseNumbers` to the current milestone, so this
* map must see the PHYSICAL set to resolve each heading's phase number
* to its actual directory name; scoping it again would look up inside
* an already-scoped set for no benefit. Its sibling readdirSync (the
* no-ROADMAP-headings-found fallback, where there is no heading scope
* to look inside) is a real current-milestone enumeration and is routed
* through the owner, not exempted.
* - `src/milestone.cts` `archivePhaseDirectories`: archival MOVES the
* physical set. Scoping it would silently leave out-of-window
* directories behind in the live tree.
* - `src/milestone.cts` `cmdMilestoneComplete`: its one remaining
* unrouted readdirSync (`phaseDirEntries`, the unstarted-phase guard)
* is a token-match LOOKUP against ROADMAP headings already scoped by
* `sliceMilestoneWindow`/`extractCurrentMilestone` above it — same
* "physical set feeds an already-scoped lookup" shape as
* `cmdRoadmapAnalyze`'s `_phaseDirNames`. Its three OTHER former
* readdirSync call sites (the stats aggregation, the dry-run archive
* preview, and the real archive-move loop) all genuinely asked "which
* phases belong to the current milestone" and are routed through the
* owner with the resolved `version` as `versionOverride`.
* - `src/milestone.cts` `cmdPhasesClear`: a destructive CLEAR that must
* remove every live phase directory except sentinels, regardless of
* milestone window — `new-milestone` runs this to wipe the ENTIRE
* phases tree before starting fresh, not just the outgoing milestone's
* slice. Scoping it to one milestone's window would silently leave
* out-of-window directories behind instead of clearing/archiving them.
* - `src/phase.cts` `cmdPhasesList`: its `--phase <n>` lookup and
* `--include-archived` merge are phase LOCATION and archive
* enumeration, not current-milestone enumeration; both legitimately
* read the physical set. Its ENUMERATION path routes through the owner.
* - `src/roadmap-parser.cts` `getMilestonePhaseFilter` and its #3262-extracted
* set-building owner `scanMilestonePhaseIds` (the same two heading/
* bullet scans, lifted verbatim so the `roadmap milestone-scope` probe
* reads the identical derivation): both deliberately use the local
* `999`-only literal, NOT `isSentinelPhaseId`. That canonical predicate
* additionally treats a leading `0` as sentinel milestone 0 (via its
* `/^0*(\d+)/` backtrack), which would swallow #2554's decimal phase ids
* ("00.1" is a real phase, not milestone 0). This scan asks a narrower
* question — "which phase ids does this milestone's window declare" —
* where only the 999 icebox range is excluded.
* - `src/state.cts` `cmdStateValidate` ("Gate 1: Validate STATE.md against
* filesystem"): resolves ONE directory — the disk match for STATE.md's
* own `Current Phase` field — by prefix, a single-phase LOOKUP, not an
* enumeration of the current milestone's phase set.
* - `src/state.cts` `cmdStateSync` ("Gate 2: Sync STATE.md from filesystem
* ground truth"): a ground-truth RECONCILIATION pass, same family as
* `collectDiskPhases` below — it deliberately scans every phase
* directory on disk (minus #1514 retired-phase exclusion) so STATE.md's
* rewritten counters reflect the true disk state, not a re-derivation of
* "which phases belong to the current milestone" the way its sibling
* `buildStateFrontmatter` computes (that one IS routed through the
* owner, scoped to the stored milestone, because it exists specifically
* to answer the milestone-scoped question at STATE.md construction
* time).
* - `src/state.cts` `cmdStateRebuild`: its nested `phaseInventoryProvider`
* deliberately does NOT route through `listMilestonePhaseDirs`. `state
* rebuild` is a RECONCILIATION pass against ground truth — it must see
* every phase directory on disk so an orphan STATE.md row for a phase
* that no longer exists (or sits outside the current milestone window) is
* dropped. Scoping this enumeration would make the rebuild silently
* preserve stale rows instead of dropping them, and a non-`readdirSync`-
* shaped owner call also cannot propagate the original fault message the
* #3057 B1 contract requires to surface verbatim.
* - `src/phase.cts` `cmdPhaseNextDecimal`: computes the next free decimal
* sub-phase id (e.g. `2.3`) by scanning EVERY on-disk directory and the
* WHOLE ROADMAP (not milestone-scoped) for existing `2.N` ids — an id
* collision it must avoid can come from any milestone, so it needs the
* physical set, matching the CREATE-adjacent exemption category.
* - `src/phase.cts` `cmdPhasePlanIndex`: resolves ONE caller-supplied
* `phase` id to its directory — a single-phase LOCATION lookup, not an
* enumeration of the current milestone's phase set.
* - `src/phase.cts` `cmdPhaseInsert`: the same next-free-decimal-id scan as
* `cmdPhaseNextDecimal` (id collisions can come from any milestone),
* immediately followed by creating the new phase directory — a CREATE
* operation, physical set by definition.
* - `src/phase.cts` `renameDecimalPhases`, `renameIntegerPhases`: RENAME
* mutations. Each `readdirSync` targets a SINGLE just-renamed phase
* directory's own FILES (`phasesDir/newDirName`) to rename the files
* inside it to match — not an enumeration of the phases directory at
* all; only shaped like one because `phasesDir` is a substring of the
* joined path.
* - `src/audit.cts` `listAuditPhaseTargets` (#3458): the shared active-root
* enumeration for the pre-milestone-close audit gate (`gsd-tools.cjs
* audit-open`, called by `/gsd:complete-milestone`'s pre-close gate).
* `scanUatGaps`, `scanVerificationGaps`, `scanContextQuestions`, and
* `scanDeferredItems` used to each hand-roll this same readdirSync
* independently (four copies of one re-derivation — the very drift class
* this guard exists to catch); #3458 consolidated all four into this one
* function, so the exemption moved with the call site instead of
* multiplying. It deliberately SWEEPS EVERY phase directory on disk to
* report open UAT/VERIFICATION/CONTEXT/deferred-item gaps — the audit's
* whole purpose is catching stragglers before a milestone closes, so
* scoping it to the current milestone's window would hide exactly the
* drift (e.g. a still-open item in a phase that somehow fell outside the
* window) it exists to surface.
* - `src/roadmap-upgrade.cts` `computeMigrationPlan`: a legacy-id-to-
* milestone-prefixed-id MIGRATION. It must see and rename EVERY existing
* phase directory across every milestone in one pass (a legacy phase
* number can legitimately collide across milestones — that ambiguity is
* exactly what the migration resolves) — the physical set by definition.
* - `src/smart-entry.cts` `detectVerifyFailed`: resolves ONE phase — the
* current phase from STATE.md, falling back to the highest-numbered
* directory when STATE.md has none — to check its own verify/UAT
* artifacts. A single-phase LOOKUP (with an explicit fallback rule of
* its own), not a current-milestone enumeration.
* - `src/commands.cts` `cmdHistoryDigest`: explicitly builds `allPhaseDirs`
* as archived-milestone dirs (via `getArchivedPhaseDirs`) PLUS every
* live phase directory, to produce a project-wide historical digest
* spanning every milestone ever shipped — the union is a strict
* superset of any one milestone's window by design; scoping the live
* half would silently drop history the digest exists to preserve.
* - `src/planning-snapshot.cts` `buildAllPhaseDirNamesField` (Phase 11,
* #3309): the un-windowed twin of `phaseDirs`/`listMilestonePhaseDirs` —
* every directory actually present under the active `phases/` root,
* UNFILTERED by current-milestone-window membership. Backs the migrated
* `cmdValidateHealth`'s W007 rule ("an on-disk phase directory has no
* matching ROADMAP entry"): sourcing that check from the WINDOWED owner
* would make it structurally unable to fire on the exact orphan
* directory it exists to find (an orphan-by-definition can never be a
* member of a set defined as "directories the roadmap already
* declares") — see that field's own doc comment on `PlanningSnapshot`
* for the full, empirically-verified rationale. Same "must see the
* physical set by definition" shape as `collectDiskPhases`/
* `cmdValidateHealth` above, generalized from a raw `readdirSync` call
* site to a dedicated snapshot-builder function.
*
* The tree-walk / root-confinement / regex-literal-tokenizer / sanitizer
* machinery is SHARED with the sibling drift guards via
* `scripts/lib/drift-scan.cjs` (ADR-3180 Decision 4) — see that module for
* the `isInsideRoot` case-sensitivity note, the `walk` symlink-confinement
* rationale, and the `readRegexLiteralAt` ReDoS-avoidance rationale.
* `readStringLiteralAt` below is the same style, written locally for
* quoted/backticked strings, mirroring `lint-milestone-window-drift.cjs`'s
* own local copy (not shared — each guard's literal-bearing shape differs).
*
* KNOWN, ACCEPTED limits of a per-line textual scan (same tradeoff the
* sibling drift guards document): a re-derivation whose detector tokens are
* split across two DIFFERENT lines with no single line carrying both is not
* caught by this narrow shape. That is left to code review and the design's
* identity tests, not this regex.
*/
const path = require('node:path');
const driftScan = require('./lib/drift-scan.cjs');
const { readRegexLiteralAt, MAX_REGEX_LITERAL_LEN, sanitizeForReport, scanTree } = driftScan;
// (1a) The enumeration primitive itself.
const READDIR_SYNC_RE = /readdirSync/;
// (1b1) The phases-directory identifier every routed call site used to bind
// its `readdirSync` target to.
const PHASES_DIR_ID_RE = /\bphasesDir\b/;
// (1b2) A quoted/backticked `'phases'` string — the other shape a phases-dir
// path segment takes at a call site that builds the path inline instead of
// through a `phasesDir` local.
const PHASES_STRING_RE = /['"`]phases['"`]/;
// (2b) A numeric comparison against the sentinel value, or a bare reference
// to the owner's exported range constant used outside the owner. The digit
// run is anchored on both sides (`\b`, and no digit can precede it inside
// the `\s*` gap immediately after the operator) so `=== 9990` / `=== 19999`
// never fire — only the standalone value `999` does.
const SENTINEL_COMPARISON_RE = /(?:===|==|!==)\s*999\b|\bSENTINEL_RANGES\b/;
// A standalone `999` inside an already-located literal's text: no digit
// immediately before or after, so `1999`/`9990`/`19999` inside a string or
// regex literal never fire — only the literal spelling of the reserved
// sentinel value does.
const STANDALONE_999_RE = /(?<!\d)999(?!\d)/;
// Authored TypeScript source only (the generated bin/lib/*.cjs mirror it).
const SCAN_DIRS = ['src'];
const SCAN_EXT = new Set(['.cts', '.ts', '.mts']);
// The two canonical owners; each defines and internally composes its half
// of the grammar and is exempt by construction (see header comment).
const OWNER_FILES = new Set([
path.join('src', 'phase-locator.cts'),
path.join('src', 'phase-id.cts'),
]);
// Per ADR-3180 Decision 4(a): NOT a bare file allowlist — each entry below
// is scoped to the SPECIFIC function asking a documented, DIFFERENT
// question, so an unrelated re-derivation added anywhere else in these same
// files is still caught. Mirrors `lint-plan-count-drift.cjs`'s and
// `lint-milestone-window-drift.cjs`'s FUNCTION_SCOPED_EXEMPTIONS mechanism.
// See the header comment for the full written reason behind each entry.
const FUNCTION_SCOPED_EXEMPTIONS = new Map([
[path.join('src', 'roadmap.cts'), new Set(['cmdRoadmapAnalyze'])],
[path.join('src', 'verify.cts'), new Set(['cmdValidateHealth', 'cmdVerifySchemaDrift'])],
[path.join('src', 'init.cts'), new Set(['detectHasPriorPhases', 'detectUiPhaseActive', 'cmdInitMilestoneOp'])],
[path.join('src', 'milestone.cts'), new Set(['archivePhaseDirectories', 'cmdMilestoneComplete', 'cmdPhasesClear'])],
[path.join('src', 'phase.cts'), new Set(['cmdPhasesList', 'cmdPhaseNextDecimal', 'cmdPhasePlanIndex', 'cmdPhaseInsert', 'renameDecimalPhases', 'renameIntegerPhases'])],
[path.join('src', 'audit.cts'), new Set(['listAuditPhaseTargets'])],
[path.join('src', 'commands.cts'), new Set(['cmdHistoryDigest'])],
[path.join('src', 'state.cts'), new Set(['cmdStateValidate', 'cmdStateSync', 'cmdStateRebuild'])],
[path.join('src', 'roadmap-upgrade.cts'), new Set(['computeMigrationPlan'])],
[path.join('src', 'smart-entry.cts'), new Set(['detectVerifyFailed'])],
[path.join('src', 'roadmap-parser.cts'), new Set(['getMilestonePhaseFilter', 'scanMilestonePhaseIds'])],
[path.join('src', 'planning-snapshot.cts'), new Set(['buildAllPhaseDirNamesField'])],
]);
// Optional `export ` modifier, mirroring the sibling guards' function
// tracker — only a column-0 top-level `function` declaration updates the
// current-function tracker; a nested/arrow function does not reset it,
// matching every FUNCTION_SCOPED_EXEMPTIONS entry above (all top-level
// `function` declarations).
const TOP_LEVEL_FUNCTION_RE = /^(?:export\s+)?function\s+([A-Za-z0-9_]+)\s*\(/;
/**
* Read the quoted or backtick-delimited string/template literal starting at
* `line[start]` (which must be `'`, `"`, or `` ` ``). Returns `{ text, end }`
* — `text` includes both delimiters, `end` is the index one past the literal
* — or null if no matching close quote is found within MAX_REGEX_LITERAL_LEN
* characters. Same single left-to-right, no-backtracking, escape-aware style
* as the shared `readRegexLiteralAt` (`\x` escapes consume both characters,
* so an escaped quote never terminates the literal early) — written locally,
* mirroring `lint-milestone-window-drift.cjs`'s own copy (not shared — each
* guard's literal-bearing shape differs).
*/
function readStringLiteralAt(line, start) {
const quote = line[start];
if (quote !== "'" && quote !== '"' && quote !== '`') return null;
const limit = Math.min(line.length, start + MAX_REGEX_LITERAL_LEN);
for (let i = start + 1; i < limit; i++) {
const ch = line[i];
if (ch === '\\') {
i++; // escape consumes the next character, whatever it is
continue;
}
if (ch === '\r' || ch === '\n') return null; // a literal cannot span lines in this per-line scan
if (ch === quote) return { text: line.slice(start, i + 1), end: i + 1 };
}
return null;
}
/**
* True when `line` contains a regex-literal or quoted/backtick-string
* literal whose text carries a STANDALONE `999` (see STANDALONE_999_RE).
*/
function hasSentinelLiteral(line) {
for (let i = 0; i < line.length; i++) {
const ch = line[i];
let literal = null;
if (ch === '/') literal = readRegexLiteralAt(line, i);
else if (ch === "'" || ch === '"' || ch === '`') literal = readStringLiteralAt(line, i);
if (!literal) continue;
if (STANDALONE_999_RE.test(literal.text)) return true;
i = literal.end - 1; // resume scanning just past this literal
}
return false;
}
/**
* The first literal (regex OR quoted/backtick string) on `line` whose text
* contains a STANDALONE `999` or the exact `'phases'` token — the "smoking
* gun" fragment worth reporting, mirroring `extractFragment`'s role in
* `lint-milestone-window-drift.cjs`. Falls back to a bounded, trimmed slice
* of the raw line when neither is inside a located literal (the
* `phasesDir` identifier / `SENTINEL_RANGES` / bare numeric-comparison
* shapes never are).
*/
function extractFragment(line) {
for (let i = 0; i < line.length; i++) {
const ch = line[i];
let literal = null;
if (ch === '/') literal = readRegexLiteralAt(line, i);
else if (ch === "'" || ch === '"' || ch === '`') literal = readStringLiteralAt(line, i);
if (!literal) continue;
if (STANDALONE_999_RE.test(literal.text) || /^['"`]phases['"`]$/.test(literal.text)) return literal.text;
i = literal.end - 1; // resume scanning just past this literal
}
return line.trim().slice(0, MAX_REGEX_LITERAL_LEN);
}
/**
* Strip comment text from a line before detection. A guard that fires on a
* COMMENT — including a comment documenting that the code below uses the
* canonical owner — reports prose as drift and trains readers to add
* exemptions for documentation. Handles the three shapes that appear in this
* codebase: a whole-line block-comment continuation (`*` or `/*` leading), a
* `//` line comment, and a trailing `//` after code.
*
* Deliberately simple and conservative: it does not attempt full block-comment
* state tracking across lines (this is a per-line scan, same tradeoff the
* sibling guards document). A `//` inside a string literal would be stripped
* early — accepted, because the effect is to UNDER-report on a pathological
* line, never to over-report prose as drift.
*/
function stripComments(line) {
const trimmed = line.trim();
// Whole-line block comment or JSDoc continuation.
if (trimmed.startsWith('*') || trimmed.startsWith('/*') || trimmed.startsWith('//')) return '';
// Trailing line comment after code.
const idx = line.indexOf('//');
return idx === -1 ? line : line.slice(0, idx);
}
/**
* Pure: find every unsanctioned phase-enumeration re-derivation in `text`.
* `relPath` is the repo-relative path, used both to report file:line and to
* apply the narrow, function-scoped exemptions above.
* Returns [{ line, found }].
*/
function findPhaseEnumerationDrift(text, relPath) {
const out = [];
const lines = text.split('\n');
const exemptFunctions = FUNCTION_SCOPED_EXEMPTIONS.get(relPath) || null;
let currentFunction = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const fnMatch = TOP_LEVEL_FUNCTION_RE.exec(line);
if (fnMatch) currentFunction = fnMatch[1];
const code = stripComments(line);
if (!code.trim()) continue;
const isEnumerationDrift = READDIR_SYNC_RE.test(code) && (PHASES_DIR_ID_RE.test(code) || PHASES_STRING_RE.test(code));
const isSentinelDrift = SENTINEL_COMPARISON_RE.test(code) || hasSentinelLiteral(code);
if (!isEnumerationDrift && !isSentinelDrift) continue;
if (exemptFunctions && exemptFunctions.has(currentFunction)) continue;
out.push({ line: i + 1, found: extractFragment(line) });
}
return out;
}
/**
* Scan the authored source tree and return every unsanctioned re-derivation,
* each annotated with the repo-relative file path.
*/
function scanRepo(root) {
return scanTree({
root,
scanDirs: SCAN_DIRS,
scanExt: SCAN_EXT,
onFile(rel, text) {
// `rel` is already the REAL (canonical) path (scanTree resolves
// symlinks before calling onFile), so this comparison — and
// FUNCTION_SCOPED_EXEMPTIONS above, also keyed on `rel` — match
// consistently regardless of which symlink reached the file.
if (OWNER_FILES.has(rel)) return [];
return findPhaseEnumerationDrift(text, rel).map((d) => ({ file: rel, ...d }));
},
});
}
function main() {
const root = path.join(__dirname, '..');
const violations = scanRepo(root);
if (violations.length === 0) {
process.stdout.write('ok phase-enumeration-drift: no unsanctioned phase-enumeration re-derivations outside phase-locator.cts / phase-id.cts\n');
return;
}
process.stderr.write('phase-enumeration-drift: independent re-derivation(s) of phase enumeration found.\n');
process.stderr.write('Use src/phase-locator.cjs `listMilestonePhaseDirs` instead of re-deriving a phases-directory\n');
process.stderr.write('readdirSync, and src/phase-id.cjs `isSentinelPhaseId` instead of re-deriving a `999` sentinel test:\n');
for (const d of violations) {
// `d.file` is exactly as attacker-controlled as `d.found`: a repo can
// legally track a filename containing control bytes / bidi overrides,
// and it is a fork-PR-authored value reaching a CI log the same way the
// matched literal does — sanitize it at the same reporting boundary.
process.stderr.write(` ${sanitizeForReport(d.file)}:${d.line} ${sanitizeForReport(d.found)}\n`);
}
process.exitCode = 1;
}
if (require.main === module) main();
module.exports = {
findPhaseEnumerationDrift,
scanRepo,
READDIR_SYNC_RE,
PHASES_DIR_ID_RE,
PHASES_STRING_RE,
SENTINEL_COMPARISON_RE,
STANDALONE_999_RE,
OWNER_FILES,
FUNCTION_SCOPED_EXEMPTIONS,
readStringLiteralAt,
hasSentinelLiteral,
extractFragment,
stripComments,
};