* fix(#3057): refuse the write when the duplicate scan cannot complete writeManifest documents itself as a fail-closed duplicate guard: if any existing manifest shares plan_id with a different, non-terminal job_id it must refuse, because dispatching again would duplicate the external job. It could not honour that. The scan reads every sibling manifest looking for the duplicate, and an unreadable or unparseable sibling was `continue`d past. If the corrupt file was the one holding the live duplicate, the scan found nothing and a duplicate external job dispatched. The asymmetry is what gives it away: a malformed TARGET refused with malformed_existing because clobbering is unacceptable, while a malformed SIBLING was skipped — yet siblings are the only thing the duplicate check reads. Adds a scan_incomplete verdict that refuses and names the offending file, so an operator can quarantine or repair it. Fail-closed alone would let one stale corrupt manifest wedge every dispatch for that planning dir permanently; naming the file is what makes refusing survivable. malformed_existing is untouched, so the target/sibling distinction stays visible. The docstring is updated — it previously stated a rule the function did not keep. memFs() gains an optional failReads map so these branches are reachable at all; they had zero coverage because the fake could not express a per-file read fault. The signature is additive and every existing caller is unchanged. The regression is proved by a pair, not a single test. A control writes a readable sibling holding a genuine non-terminal duplicate and asserts duplicate_plan_id, establishing the scenario is real; the regression then makes that same path unreadable and asserts scan_incomplete. A first draft of this test used a corrupt-JSON fixture containing no plan_id at all while its comment claimed otherwise — it duplicated the unparseable-sibling case and proved nothing, which is the defect class this phase exists to remove. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3057): make a guard's failure distinguishable from its benign result Wave 1 of the negative-space backfill: the branches where a guard that could not verify something reported the same value it reports when everything is fine. That indistinguishability is the defect; every fix here makes the two states tellable apart, and every test proves it with a pair — one for the failure, one for the benign case. A single test cannot establish that two states are distinguishable, which is the whole property being fixed. state.cts phaseInventoryProvider returned null for both a real disk-scan failure and a genuinely empty phases dir, so `state rebuild` could report success while phase-table reconciliation never ran. It now returns a discriminated result and the CLI surfaces phase_inventory_scan_failed plus a reason. The reason field turned out never to have been wired into the emitted JSON at all — it existed only as an internal variable — so a test could only assert on the operator-facing note. It is a real field now. state.cts treated an unreadable lock body the same as an empty one, applying the 1-second stealable floor. A lock we cannot read is not a lock we know is stale; an unreadable body is now held to the deadman ceiling like a live holder. verification.cts findStaleVerificationSummary returned null on any fs, scan or clock failure — meaning "not stale". It now returns a discriminated StaleCheckResult and the caller records that the check was indeterminate. git-base-branch resolveBaseBranch returned 'main' both when no candidate branch existed and when every git tier timed out. A diagnostics variant now reports whether the answer was verified, and the CLI writes an unverified-fallback note to stderr. The stdout contract five workflows parse is untouched. worktree-safety snapshotWorktreeInventory left exists:true when statSync threw, so a guard that could not check reported the worktree present; exists is now tri-state and a stat failure surfaces as an 'unverified' finding. planWorktreePrune reported 'no_worktrees' for a parse failure, which is not the same as an empty list — and it drives a prune. It now reports 'parse_failed'. Fixing the inventory change exposed a second fail-open in verify.cts: the validate-health consumer silently dropped findings whose kind it did not recognise, so the new kind would have vanished. That is closed too — worth noting that the survey enumerated producers of degraded verdicts, not consumers that discard them. worktree-base-ref and state-transition gain the distinguishing signal without changing what they do: headAbsenceVerified, and a phase-inventory scan meta. Whether those guards should ACT differently is a product question this change does not answer, and both are flagged rather than quietly settled. rescueSummaryArtifacts is left alone: rescuing on an uncertain cat-file is deliberate per #2556. It now has tests proving it, and a recorded negative finding — git cat-file -e returns 128 for both "absent from HEAD" and a fatal error, so "uncertain" and "certain-and-fine" are not separable at the git level. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3057): assert typed values, not rendered text Ten assertions in the rebuild CLI suite matched substrings of produced output — STATE.md body fields, a markdown table row, an audit-log heading, and JSON keys read as text. CONTRIBUTING prohibits that: if the code under test produces text, the test asserts on its structured surface instead. No production surface had to be built. Every one already existed and was already compiled into bin/lib: stateExtractField for body fields, parseMarkdownTable for the phase table, collectSection for the audit-log section, and result.data.log — already a typed RebuildLogEntry[]. The tests were matching rendered text sitting next to the structured data. One of those assertions was passing for the wrong reason. `stdout.includes ('rebuilt')` matched the JSON KEY name, not a value: the dry-run path emits `mutated` and the real path emits `rebuilt`, so it would have passed whether the value was true or false. It now asserts the value. external-job's refusal already had to name the offending file — that naming is why the fail-closed variant is survivable rather than a permanent wedge — but the tests proved it by substring of a prose message. The failure result now carries offendingPath as its own field and the tests assert it by value. The human message is unchanged; operators read it. Array membership is left alone. `phaseIds.includes('99')` and `result.updated.includes('Completed Phases')` are membership checks on real arrays, not text matching, and converting them would weaken nothing and clarify nothing. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3057): execute acquireStateLock instead of grepping its source The non-EEXIST lock test asserted on the TEXT of the built .cjs and never called acquireStateLock. It carried an allow-test-rule: architectural-invariant exemption to permit that. A source grep proves a literal is present in a file, not that the behaviour works — it is weaker than a liveness test, which at least runs the code, and it was the only coverage the fatal-errno path had. Replaced with tests that inject the errno through fs and assert what actually happens: a fatal EACCES propagates out of acquireStateLock with zero backoff sleeps, while EAGAIN/EINTR/EINVAL/EIO/ENOENT/ESTALE/EPERM/EBUSY retry once and succeed. The exemption is removed and its allowlist entry with it. One old assertion is deliberately not carried over: it checked the retryable errnos were expressed as a Set rather than an inline literal. That is a shape check with no runtime signature; the behavioural tests fail if the code reverts to the old inline check, which is the regression it was really guarding. The #3057 lock-body tests move into that same file rather than a new one, which is what lint-test-file-count asks for and puts every acquireStateLock test in one place. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3057): surface an indeterminate staleness check to its callers An isolated review caught an inconsistency inside this wave. Two of the three "add the distinguishing signal" fixes wire through to something a user sees: git base-branch writes an unverified-fallback diagnostic to stderr, and an unverifiable worktree surfaces as a W020 finding. The third set staleCheckIndeterminate on readVerificationStatus's result and nothing read it. A signal nobody consumes leaves the fail-open exactly as silent as before: the staleness check could fail and the operator saw precisely what they would see if the answer were genuinely "not stale". That is the defect this issue exists to remove, so it is not defensible as scaffolding when its two siblings in the same change already wire through. All five callers now surface it, each through the channel it already had rather than a mechanism imposed uniformly: phase complete adds it to its existing warnings array and, on the blocked path, as an additive note on the error text; init and roadmap carry it as a field on output they already emit; the UAT report carries it without ever gating passed/blockers; workstream inventory takes an injectable writeDiagnostic mirroring the git base-branch idiom, because its return shape had nowhere to hang a per-phase field without rippling the builder's types. The routing decision is unchanged everywhere. What changes is only that a caller and an operator can now tell a failed check from a completed one. That diagnostic carries structured meta rather than being asserted by regex — the default still writes only the human message to stderr, but tests assert phaseDir and reason by value. Two earlier assertions in this branch were converted the same way; this was the last raw-text assertion left. Also records a scope correction: the completePhaseCore guards now compare stateReplaceField's result to the body instead of testing truthiness, so a field whose substitution produced identical text no longer reports as updated. That is a real behaviour fix, not the signal-only change this file was described as carrying, and its tests cover both the changed and unchanged cases. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3057): bound two heavy subprocesses for a loaded bench, not an idle one The remote matrix surfaced three failures unrelated to this branch's changes. All were bad tests, and a re-run would have hidden every one of them. The reviewer-flags parse block bounded bash -> node -> a full gsd-tools cold start at 5 seconds. On a bench running thirty thousand tests in parallel that is not a hang, it is a busy machine. Raised to 30s, matching the convention sibling suites already use for script invocations, with a comment saying what the budget covers so nobody tightens it back. Two further copies of the same 5-second spawn in the same file had the identical defect and are raised too — they were not in the failure report, but they will be next time. The fragment-propagation test bounded npm run regen:derived — a full build plus eight generators, the heaviest subprocess in the suite — at five minutes, and node22 was killed near the end. The captured output proves it: every generator had written its files and gen:install-tree had emitted all fifteen runtimes before the kill. Raised to fifteen minutes. That failure read as `null !== 0`, which says nothing. status null means killed, not a non-zero exit, and the two want different responses: one is a timeout to size correctly, the other is a real build break. The assertion now distinguishes them and names the signal. Neither test's assertions were weakened and no retry was added. A retry here would suppress exactly the signal the timeout exists to produce. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3057): capture fd 1 through the mock tracker, not a raw reassignment The phase suite reported zero test results on both lanes while running for five and a half minutes and exiting 1. No assertion text, no stderr, four events for the whole file: enqueue, start, dequeue, complete. That shape is not a failing assertion — it is the runner being unable to read the child at all, because it parses its event stream from the child's stdout. The cause was the capture helper reassigning fs.writeSync directly. Proven rather than assumed: a standalone probe patched fs.writeSync and called process.stdout.write, and the interception fired only when fd 1 resolved to a FILE, not when it was a pipe. The remote runner captures the event stream to a file, so a helper that was invisible against a pipe swallowed the reporter's own output on the bench. That is also why the two sibling suites wired the same way in this change pass cleanly — they use the mock tracker, the seam io.test.cjs established for this exact function. The helper now uses t.mock.method with an explicit restore after each call, so teardown belongs to node:test rather than a second hand-rolled implementation, and the interception cannot outlive the one synchronous call it wraps even if that call throws. Ten call sites thread the test context through; three test callbacks gained the parameter they lacked. The three B3 tests are untouched — same assertions, same fault injection. Only how the context reaches the helper changed. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * test(#3057): capture phase-complete output from a subprocess, not fd 1 Two attempts to make in-process fd-1 interception safe both failed on the bench. The suite reported zero test results on either lane while exiting 1 — four events for the whole file — because the runner parses its event stream from the child's stdout, and process.stdout.write routes through fs.writeSync whenever fd 1 resolves to a file, which is how the runner captures. Patching that seam anywhere in a file can therefore destroy the file's own reporting, and tightening the window only moved the runtime from 326s to 125s without recovering a single event. So the interception is gone rather than tuned. The helper now spawns gsd-tools as a real subprocess and reads stdout the way the OS already gives it to us, which is what the rest of the suite does. It asserts the command succeeded before parsing, so a genuine failure can no longer present as a JSON parse error. The two fault-injecting tests could not survive that move as written: a subprocess cannot see a mock installed in the parent. Instead of reinstating the interception they now produce the fault on disk — the summary artifact is created as a dangling symlink, so the staleness check's real statSync throws inside the child. That is a more honest fixture than a mock in any case, since it is a condition a user's tree can actually be in. Skipped on Windows, matching the existing symlink precedent in the write-guard suite. Three further call sites turned out to depend on parent-process writeFileSync mocks the subprocess could not see. Those call the CJS function directly, which is what they always wanted — they never needed stdout at all. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(#3057): one name for one signal, one encoding for one distinction Standards review found four things this branch introduced, all of them inconsistencies with itself rather than with the repo. One upstream bit reached its consumers under three names — verification_stale_check_indeterminate in two modules, the same value with "stale" dropped in a third, and stderr only in the fourth. Standardised on the long name wherever it is a field. The workstream inventory keeps its stderr channel, since its return shape has nowhere to hang a per-phase field without rippling the builder's types, but it now says the same word for the same thing. worktree-safety encoded one three-way distinction two ways in a single file: a named union for a finding's kind, and boolean|null for an inventory entry's existence. The second is now a named union too. Two assertions matched human prose because the blocked and non-blocked completion paths carried no typed field for the signal. Both now assert typed values. The first round of this fix added the field but left the regex beside it, which is the banned pattern sitting next to its own replacement; the second removed it and added an assertion on the reason enum so nothing was lost. The remaining two were reasoned away before being fixed, and both reasons were bad. "No typed surface exists" is the condition CONTRIBUTING says to fix by adding one — it took three lines. "The file already does this dozens of times" is not licence to add instance number thirty-one; a convention that violates a documented rule is debt, not precedent. Vocabulary differing across DIFFERENT modules is left alone: CONTEXT.md rejects a single shared result envelope, so per-module shapes are precedented, and a baseline smell does not outrank a documented standard. A census of every line this branch adds to a test file now finds no regex or substring assertion on produced prose: 87 strictEqual, 25 ok (all non-empty or shape guards), 12 equal, 3 throws (all typed err.code predicates), 3 deepStrictEqual, 2 notStrictEqual. Refs #3051 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore(#3057): backfill changeset pr number to 3088 --------- Co-authored-by: sim <sim@local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
797 lines
38 KiB
TypeScript
797 lines
38 KiB
TypeScript
/**
|
|
* Workstream Inventory Module
|
|
*
|
|
* Owns discovery and read-only projection of .planning/workstreams/* state.
|
|
* Command handlers should render outputs from this inventory instead of
|
|
* rescanning workstream directories directly.
|
|
*
|
|
* Pure projection logic lives in workstream-inventory-builder.cts.
|
|
* This module handles I/O orchestration only.
|
|
*
|
|
* ADR-457 build-at-publish: the hand-written bin/lib/workstream-inventory.cjs
|
|
* collapsed to a TypeScript source of truth. Behaviour is preserved byte-for-behaviour
|
|
* from the prior hand-written .cjs; only types are added.
|
|
*/
|
|
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
import coreUtilsMod = require('./core-utils.cjs');
|
|
const { readSubdirectories } = coreUtilsMod;
|
|
// 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 planningWorkspace = require('./planning-workspace.cjs');
|
|
const { planningPaths, planningRoot, getActiveWorkstream } = planningWorkspace;
|
|
import { stateExtractField } from './state-document.cjs';
|
|
import { findTableWithColumns } from './markdown-table.cjs';
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports -- verification.cjs is an export= CommonJS module
|
|
import verificationMod = require('./verification.cjs');
|
|
const { readVerificationStatus } = verificationMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports -- phase-id.cjs is an export= CommonJS module
|
|
import phaseIdMod = require('./phase-id.cjs');
|
|
const { phaseKeyFromDir, phaseKeyFromProse, parentPhaseKey } = phaseIdMod;
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports -- roadmap-parser.cjs is an export= CommonJS module
|
|
import roadmapParserMod = require('./roadmap-parser.cjs');
|
|
const { getMilestonePhaseFilter, isMilestoneShippedInRoadmap } = roadmapParserMod;
|
|
import { buildWorkstreamInventory, isCompletedInventory, pickRollupWinners } from './workstream-inventory-builder.cjs';
|
|
import type { WorkstreamInventory, StateProjection, MilestoneShippedSignal } from './workstream-inventory-builder.cjs';
|
|
|
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
|
|
interface PhaseFileCounts {
|
|
planCount: number;
|
|
summaryCount: number;
|
|
}
|
|
|
|
interface InspectWorkstreamOptions {
|
|
active?: string | null;
|
|
/**
|
|
* #3057 B3: injectable diagnostic-write seam, mirrored from
|
|
* cmdGitBaseBranch's `writeDiagnostic` (git-base-branch.cts). Called with a
|
|
* stderr-style line when a phase's readVerificationStatus staleness check
|
|
* could not run to completion — WorkstreamInventory's own return shape
|
|
* (`phases: PhaseStatus[]`) has no per-phase verification detail today, so
|
|
* this side channel surfaces the fact without widening that aggregate type.
|
|
* The second argument carries the same facts as structured, typed data
|
|
* (CONTRIBUTING: no raw-text matching on produced diagnostics) so a caller
|
|
* — tests included — can assert on `phaseDir`/`reason` directly instead of
|
|
* pattern-matching the operator-facing `message`. The default
|
|
* implementation writes only `message` to stderr; operator output is
|
|
* unchanged. Never affects routing: the ledger / rollup computation below
|
|
* is unchanged either way (the pre-existing fail-open contract).
|
|
*/
|
|
writeDiagnostic?: (message: string, meta: { phaseDir: string; reason: string }) => void;
|
|
}
|
|
|
|
interface WorkstreamInventoryList {
|
|
mode: 'flat' | 'workstream';
|
|
active: string | null;
|
|
workstreams: WorkstreamInventory[];
|
|
count: number;
|
|
message?: string;
|
|
}
|
|
|
|
// ─── Implementation ───────────────────────────────────────────────────────────
|
|
|
|
function workstreamsRoot(cwd: string): string {
|
|
return path.join(planningRoot(cwd), 'workstreams');
|
|
}
|
|
|
|
function countRoadmapPhases(roadmapPath: string, fallbackCount: number): number {
|
|
try {
|
|
const roadmapContent = fs.readFileSync(roadmapPath, 'utf-8');
|
|
const matches = roadmapContent.match(/^#{2,4}\s+Phase\s+[\w][\w.-]*/gm);
|
|
return matches ? matches.length : fallbackCount;
|
|
} catch {
|
|
return fallbackCount;
|
|
}
|
|
}
|
|
|
|
interface RoadmapProgressRow {
|
|
/** Canonical phase key (`phaseKeyFromProse`) — the SAME key space as `phaseKeyFromDir`. */
|
|
key: string;
|
|
/** `vX.Y` when the row attributes the phase to a milestone; null when the cell is absent, blank or malformed. */
|
|
version: string | null;
|
|
}
|
|
|
|
/**
|
|
* #2562: parse the ROADMAP `## Progress` table into canonical phase keys with
|
|
* their milestone attribution, e.g. `| 30. Name | v10.0 | 1/3 | … |` →
|
|
* `{ key: '30', version: 'v10.0' }`. The table is the authoritative per-phase
|
|
* milestone attribution and — crucially — lists phases declared but never
|
|
* scaffolded (no directory), which a directory-only scan misses. `Plans
|
|
* Complete` is present in BOTH RoadmapProgress variants, so this matches the
|
|
* flat (no Milestone column → every `version` null) and milestone-grouped
|
|
* shapes alike.
|
|
*
|
|
* Row keys come from `phaseKeyFromProse`, the same owner-module derivation
|
|
* `phaseKeyFromDir` uses for directories, so a `| 01. … |` row and a `1-slug`
|
|
* directory cannot land in different key spaces (the padding-asymmetry defect).
|
|
*/
|
|
function parseRoadmapProgressRows(roadmapPath: string): RoadmapProgressRow[] {
|
|
let content: string;
|
|
try {
|
|
content = fs.readFileSync(roadmapPath, 'utf-8');
|
|
} catch {
|
|
return []; /* no roadmap */
|
|
}
|
|
// Milestone-ATTRIBUTING shape first. Both shapes carry `Plans Complete`, so
|
|
// probing that column first would pick a flat table appearing earlier in the
|
|
// document over a milestone-grouped one later — every row would come back
|
|
// unattributed and be treated as current-milestone, silently over-including.
|
|
const table = findTableWithColumns(content, ['Phase', 'Milestone'])
|
|
?? findTableWithColumns(content, ['Phase', 'Plans Complete']);
|
|
if (!table) return [];
|
|
const rows: RoadmapProgressRow[] = [];
|
|
for (const row of table.rows) {
|
|
const key = phaseKeyFromProse(row['Phase']);
|
|
if (key === null) continue;
|
|
const cell = (row['Milestone'] ?? '').trim();
|
|
rows.push({ key, version: /^v\d+(?:\.\d+)+$/.test(cell) ? cell : null });
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
/**
|
|
* #2562: the workstream's CURRENT milestone version, read from the STATE.md
|
|
* `milestone:` frontmatter field (the reliable per-workstream signal — the
|
|
* ROADMAP's own in-progress markers can be stale, e.g. a lingering 🚧 on an
|
|
* already-shipped milestone). Falls back to the ROADMAP in-progress heading
|
|
* marker only when STATE has no field.
|
|
*/
|
|
function readCurrentMilestoneVersion(statePath: string, roadmapPath: string): string | null {
|
|
try {
|
|
const m = fs.readFileSync(statePath, 'utf-8').match(/^milestone:\s*["']?(v\d+(?:\.\d+)+)["']?/m);
|
|
if (m) return m[1];
|
|
} catch {
|
|
/* no state */
|
|
}
|
|
try {
|
|
const rm = fs.readFileSync(roadmapPath, 'utf-8').match(/(?:🚧|🔄)\s*\*\*(v\d+(?:\.\d+)+)\b/);
|
|
if (rm) return rm[1];
|
|
} catch {
|
|
/* no roadmap */
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* #2562: does the CURRENT milestone's own ROADMAP heading carry a shipped
|
|
* marker? Delegated to `roadmap-parser`, the module that owns milestone-heading
|
|
* classification: heading/`<summary>` lines only (never a bullet that merely
|
|
* names the version), version-token boundary-matched so `v2.0` does not match
|
|
* inside `v2.0.1`, and in-progress markers win. Scoped to the current version,
|
|
* so a prior milestone's collapsed `<details><summary>✅ … SHIPPED</summary>`
|
|
* block can never mark the current milestone complete.
|
|
*/
|
|
function currentMilestoneHeadingShipped(roadmapPath: string, version: string): boolean {
|
|
try {
|
|
return isMilestoneShippedInRoadmap(fs.readFileSync(roadmapPath, 'utf-8'), version);
|
|
} catch {
|
|
return false; /* no roadmap */
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Legacy pre-#2562 shipped detection: ANY archived milestone snapshot OR a
|
|
* SHIPPED marker anywhere in the ROADMAP. Over-broad (project-lifetime, not
|
|
* milestone-scoped) — retained ONLY as the fallback when the current milestone
|
|
* version cannot be determined (malformed/legacy STATE.md with no `milestone:`
|
|
* field), so those projects keep #1913's stale-field protection.
|
|
*/
|
|
function legacyMilestoneShipped(roadmapPath: string, planningBase: string): boolean {
|
|
try {
|
|
const milestonesDir = path.join(planningBase, 'milestones');
|
|
for (const entry of fs.readdirSync(milestonesDir, { withFileTypes: true })) {
|
|
if (entry.isFile() && /-ROADMAP\.md$/i.test(entry.name)) return true;
|
|
}
|
|
} catch {
|
|
/* no milestones archive dir */
|
|
}
|
|
try {
|
|
if (/SHIPPED/i.test(fs.readFileSync(roadmapPath, 'utf-8'))) return true;
|
|
} catch {
|
|
/* no roadmap */
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* #2562: directory mtime, used only to break a duplicate-phase-key tie in the
|
|
* rollup (keep the more recently touched directory — the Bug #2445 rule). 0 on
|
|
* a stat failure, which loses the tie rather than throwing.
|
|
*/
|
|
function phaseDirMtime(phaseDir: string): number {
|
|
try {
|
|
return fs.statSync(phaseDir).mtimeMs;
|
|
} catch {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function countPhaseFiles(phaseDir: string): PhaseFileCounts {
|
|
const scan = planScan(phaseDir);
|
|
return { planCount: scan.planCount, summaryCount: scan.summaryCount };
|
|
}
|
|
|
|
// ─── #2645: verification-deletion ledger ───────────────────────────────────
|
|
//
|
|
// #2562's completeness gate (`FAILING_VERIFICATION_STATUSES`,
|
|
// workstream-inventory-builder.cts) reads a phase's verification verdict
|
|
// fresh from `*-VERIFICATION.md` on every call. That makes "verifier ran,
|
|
// found gaps, report later deleted" indistinguishable from "verifier never
|
|
// ran" — both collapse to the same `'missing'` sentinel
|
|
// (verification.cts's `missingResult()`), which is deliberately NOT in the
|
|
// failing set (so verifier-disabled projects can still reach 100%). Deleting
|
|
// a failing report is therefore sufficient to silently raise the reported
|
|
// completion percentage — a Goodhart hole (#2645).
|
|
//
|
|
// The fix persists the last REAL (non-'missing') verdict this module has
|
|
// ever observed per phase key, in a small ledger file living at the
|
|
// WORKSTREAM directory level — never inside the phase directory whose file
|
|
// is the thing being deleted, so the same `rm` that triggers the hole
|
|
// cannot also erase the memory of it. When a live read comes back 'missing',
|
|
// the ledger is consulted as a fallback; when a live read comes back with a
|
|
// real verdict — including a later 'passed' that supersedes an earlier
|
|
// failing one — the ledger is updated to match, so a genuinely re-verified
|
|
// phase is never permanently pinned.
|
|
//
|
|
// #2645 review: a NAIVE two-state read ("got entries, or nothing") fails
|
|
// OPEN — any read/parse failure degraded to `{}`, which is indistinguishable
|
|
// from "genuinely never verified", so corrupting the ledger (or deleting it
|
|
// alongside the report) silently reopened the exact hole this fix exists to
|
|
// close, one level up. THREE states, not two:
|
|
//
|
|
// 1. `'absent'` — no `.verification-ledger.json` for this workstream at
|
|
// all. This is the ONLY state that behaves exactly as pre-#2645 (a
|
|
// phase with no live report reads `'missing'`, ungated). Deliberate:
|
|
// on the day this ships, EVERY existing project is in this state for
|
|
// EVERY workstream, and gating here would drop them all to
|
|
// `in_progress` at once. A workstream stays here forever if the
|
|
// verifier is never actually used on it (criterion 2/3 — no entry is
|
|
// ever written for a `'missing'` live read, so the file itself is
|
|
// never created).
|
|
// 2. `'corrupt'` — the file exists but could not be read or parsed (I/O
|
|
// error, invalid JSON, wrong shape). This is NOT the same as absent:
|
|
// an unreadable file is evidence something existed. Treated identically
|
|
// to "present, no entry for this phase" below — fails CLOSED, not open.
|
|
// 3. `'ok'` — the file exists and parsed. A phase with an entry uses
|
|
// it; a phase WITHOUT one is "present, no entry" — this workstream has
|
|
// adopted the ledger (some phase in it has a real verdict on record),
|
|
// so an unobserved phase can no longer default to the pre-adoption
|
|
// "ungated" behavior, or the same evidence-erasure hole reopens for
|
|
// THIS phase specifically. Fails CLOSED: resolves to the `'unrecorded'`
|
|
// sentinel (`workstream-inventory-builder.cts`'s
|
|
// `FAILING_VERIFICATION_STATUSES`), not `'missing'`.
|
|
//
|
|
// Corrupt-ledger recovery: a corrupt ledger is NOT a permanent wedge. Any
|
|
// phase with a REAL live verdict on disk still writes/repairs the ledger on
|
|
// this same call (the corrupt content is fully overwritten, never patched),
|
|
// so re-running the verifier for even one phase heals the file. A phase with
|
|
// no live report and no way to re-verify stays `'unrecorded'` (gated) until
|
|
// someone re-verifies it — a deliberate, disclosed cost of failing closed,
|
|
// not an accidental one.
|
|
//
|
|
// Disclosed, ACCEPTED residual gap (not closed by this fix, and not closable
|
|
// by ledger design alone): deleting the ledger FILE ITSELF (not just the
|
|
// phase's report) returns a workstream to state 1 (`'absent'`) and restores
|
|
// pre-#2645 behavior for it. Any durable store that can fail open when
|
|
// absent has this property at its own root — the ledger raises the bar from
|
|
// "delete one file" to "delete two files in two different directories,
|
|
// including one this issue's own reproduction never needed to touch", but a
|
|
// deliberately absent ledger is indistinguishable from a never-adopted one
|
|
// by design (criterion 2/3 depend on that same indistinguishability). Rail B
|
|
// is PROSPECTIVE ONLY: a phase verified and its report deleted BEFORE this
|
|
// fix ships has no ledger entry to fall back on and cannot be retroactively
|
|
// recovered.
|
|
interface VerificationLedger { [phaseKey: string]: string; }
|
|
|
|
interface VerificationLedgerRead {
|
|
state: 'absent' | 'corrupt' | 'ok';
|
|
entries: VerificationLedger;
|
|
}
|
|
|
|
function verificationLedgerPath(wsDir: string): string {
|
|
return path.join(wsDir, '.verification-ledger.json');
|
|
}
|
|
|
|
function readVerificationLedger(wsDir: string): VerificationLedgerRead {
|
|
const ledgerPath = verificationLedgerPath(wsDir);
|
|
let raw: string;
|
|
try {
|
|
raw = fs.readFileSync(ledgerPath, 'utf-8');
|
|
} catch (err) {
|
|
// `fs.readFileSync` FOLLOWS symlinks, so a broken symlink at this path
|
|
// (the entry exists, its target does not) reports the EXACT SAME
|
|
// `ENOENT` as genuine absence — `code` alone cannot distinguish
|
|
// "nothing was ever here" from "something is here and cannot be read".
|
|
// `fs.lstatSync` does NOT follow symlinks, so it still finds the
|
|
// symlink entry itself even when its target is gone. Only when NEITHER
|
|
// call finds anything is this genuinely `'absent'` (pre-adoption); a
|
|
// present-but-broken symlink is evidence something existed and must
|
|
// fail CLOSED like any other unreadable ledger, not fall open.
|
|
const code = (err as NodeJS.ErrnoException)?.code;
|
|
if (code === 'ENOENT') {
|
|
try {
|
|
fs.lstatSync(ledgerPath);
|
|
return { state: 'corrupt', entries: {} }; // a symlink entry exists; its target does not
|
|
} catch (lstatErr) {
|
|
// #2645 review: every OTHER failure path in this function fails
|
|
// CLOSED — this one must too. A failed `lstatSync` is only proof of
|
|
// absence when IT ALSO reports `ENOENT`; anything else (a raced
|
|
// permission change, a path component that became inaccessible
|
|
// between the two calls, …) is not evidence the file was never
|
|
// there, and a bare `catch {}` here would silently fall OPEN exactly
|
|
// like the two-state design this fix replaced.
|
|
const lstatCode = (lstatErr as NodeJS.ErrnoException)?.code;
|
|
if (lstatCode === 'ENOENT') return { state: 'absent', entries: {} }; // truly nothing at this path
|
|
return { state: 'corrupt', entries: {} };
|
|
}
|
|
}
|
|
// Any OTHER read failure (EACCES, EISDIR, …) also means the path EXISTS
|
|
// in some form but this process cannot see its content right now — that
|
|
// is corruption from this reader's point of view, not absence, and must
|
|
// fail closed rather than silently falling back to the ungated
|
|
// pre-adoption behavior.
|
|
return { state: 'corrupt', entries: {} };
|
|
}
|
|
let parsed: unknown;
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch {
|
|
return { state: 'corrupt', entries: {} };
|
|
}
|
|
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
return { state: 'corrupt', entries: {} };
|
|
}
|
|
const out: VerificationLedger = {};
|
|
for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
if (typeof value === 'string') out[key] = value;
|
|
}
|
|
return { state: 'ok', entries: out };
|
|
}
|
|
|
|
// #2645 review: Windows can transiently hold the rename target busy (AV
|
|
// scanners, indexers) — the SAME retry shape `broken-windows.cts`'s
|
|
// `writeLedgerAtomic`/`renameWithRetry` already uses for its own ledger
|
|
// write, mirrored here rather than imported (that function is private to
|
|
// its module) so this fix does not widen its own blast radius by exporting
|
|
// a new cross-module utility.
|
|
const LEDGER_RENAME_RETRY_ERRNOS = new Set(['EPERM', 'EBUSY', 'EACCES']);
|
|
const LEDGER_RENAME_MAX_ATTEMPTS = 5;
|
|
const LEDGER_RENAME_BACKOFF_MS = 25;
|
|
|
|
function renameVerificationLedgerWithRetry(tmpPath: string, finalPath: string): void {
|
|
let lastErr: unknown;
|
|
for (let attempt = 0; attempt < LEDGER_RENAME_MAX_ATTEMPTS; attempt++) {
|
|
try {
|
|
fs.renameSync(tmpPath, finalPath);
|
|
return;
|
|
} catch (err: unknown) {
|
|
lastErr = err;
|
|
const code = (err && typeof err === 'object' && 'code' in err) ? String((err as { code?: unknown }).code) : '';
|
|
if (code && LEDGER_RENAME_RETRY_ERRNOS.has(code) && attempt < LEDGER_RENAME_MAX_ATTEMPTS - 1) {
|
|
// Exponential-ish backoff: 25ms, 50ms, 100ms, 200ms. Transient
|
|
// Windows locks usually clear well inside that window.
|
|
const delay = LEDGER_RENAME_BACKOFF_MS * Math.pow(2, attempt);
|
|
const start = Date.now();
|
|
while (Date.now() - start < delay) {
|
|
// Deliberate short busy-wait — no async/timer seam is available
|
|
// in this synchronous read path.
|
|
}
|
|
continue;
|
|
}
|
|
throw err;
|
|
}
|
|
}
|
|
throw lastErr;
|
|
}
|
|
|
|
function writeVerificationLedger(wsDir: string, ledger: VerificationLedger): void {
|
|
// #2645 review: atomic write. A crash or a concurrent read mid-write
|
|
// against `verificationLedgerPath(wsDir)` directly would leave (or briefly
|
|
// expose) TRUNCATED JSON — and now that `'corrupt'` carries real semantic
|
|
// weight (it fails CLOSED, holding every phase with no live report at
|
|
// `'unrecorded'`), producing a corrupt file ourselves is a self-inflicted
|
|
// version of the exact failure mode this fix exists to survive. Write to a
|
|
// sibling temp file in the SAME directory (same filesystem — `rename` is
|
|
// only atomic within one) and rename into place: a reader can only ever
|
|
// observe the prior complete content or the new complete content.
|
|
const finalPath = verificationLedgerPath(wsDir);
|
|
const tmpPath = `${finalPath}.${process.pid}.tmp`;
|
|
try {
|
|
fs.mkdirSync(wsDir, { recursive: true });
|
|
fs.writeFileSync(tmpPath, `${JSON.stringify(ledger, null, 2)}\n`, 'utf-8');
|
|
renameVerificationLedgerWithRetry(tmpPath, finalPath);
|
|
} catch {
|
|
// Best-effort persistence: a missing parent directory that `mkdirSync`
|
|
// itself cannot create, a read-only filesystem, a write failure, or a
|
|
// rename failure that exhausts its retries must not break inventory
|
|
// reads, which are otherwise pure. Losing this observation only
|
|
// re-opens the pre-#2645 window for THIS run; the next successful read
|
|
// while a report is on disk repairs it. Clean up a half-written temp
|
|
// file so repeated failures cannot accumulate orphaned
|
|
// `.verification-ledger.json.<pid>.tmp` files.
|
|
try { fs.unlinkSync(tmpPath); } catch { /* nothing to clean up, or cleanup itself failed — not fatal */ }
|
|
}
|
|
}
|
|
|
|
function readStateProjection(statePath: string): StateProjection {
|
|
try {
|
|
const stateContent = fs.readFileSync(statePath, 'utf-8');
|
|
return {
|
|
status: stateExtractField(stateContent, 'Status') || 'unknown',
|
|
current_phase: stateExtractField(stateContent, 'Current Phase'),
|
|
last_activity: stateExtractField(stateContent, 'Last Activity'),
|
|
};
|
|
} catch {
|
|
return {
|
|
status: 'unknown',
|
|
current_phase: null,
|
|
last_activity: null,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* #1913 + #2562: detect an authoritative shipped signal for a workstream's
|
|
* CURRENT milestone, so the inventory status is never trusted from the mutable
|
|
* STATE.md `Status` field alone (#1913) yet is never pinned to "milestone
|
|
* complete" by a PRIOR milestone's shipped marker (#2562).
|
|
*
|
|
* When the current milestone version is known, the signal is scoped to it:
|
|
* an archived snapshot `milestones/<version>-ROADMAP.md` (the canonical
|
|
* "milestone shipped" artifact) OR the current milestone's own ROADMAP line
|
|
* marked shipped. When the version cannot be determined, we fall back to the
|
|
* over-broad legacy detection to preserve #1913's protection for those
|
|
* (malformed/legacy) projects.
|
|
*
|
|
* Returns WHICH signal fired, not merely that one did. The two differ in how
|
|
* much they can be trusted and therefore in how the builder cross-validates
|
|
* them against the milestone's own artifacts — see the `shippedContradicted`
|
|
* block in `workstream-inventory-builder.cts`. Collapsing them to a boolean is
|
|
* what forced a single completeness check to serve two incompatible shapes.
|
|
*/
|
|
function workstreamShippedSignal(
|
|
roadmapPath: string,
|
|
planningBase: string,
|
|
currentVersion: string | null,
|
|
): MilestoneShippedSignal {
|
|
if (!currentVersion) {
|
|
return legacyMilestoneShipped(roadmapPath, planningBase) ? 'legacy' : null;
|
|
}
|
|
// Canonical shipped artifact: the archived ROADMAP snapshot of the CURRENT
|
|
// milestone (`vX.Y-ROADMAP.md`), written at milestone close. REQUIREMENTS
|
|
// snapshots are intentionally NOT accepted — they can be written at milestone
|
|
// START (requirements-locked), so they do not imply shipped.
|
|
const snapshot = path.join(planningBase, 'milestones', `${currentVersion}-ROADMAP.md`);
|
|
if (fs.existsSync(snapshot)) return 'snapshot';
|
|
return currentMilestoneHeadingShipped(roadmapPath, currentVersion) ? 'heading' : null;
|
|
}
|
|
|
|
function sortWorkstreamInventories(inventories: WorkstreamInventory[], activeWorkstreamName: string | null): WorkstreamInventory[] {
|
|
return [...inventories].sort((a, b) => {
|
|
const aActive = a.name === activeWorkstreamName ? 1 : 0;
|
|
const bActive = b.name === activeWorkstreamName ? 1 : 0;
|
|
if (aActive !== bActive) {
|
|
return bActive - aActive;
|
|
}
|
|
return a.name.localeCompare(b.name);
|
|
});
|
|
}
|
|
|
|
function inspectWorkstream(cwd: string, name: string, options: InspectWorkstreamOptions = {}): WorkstreamInventory | null {
|
|
const wsDir = path.join(workstreamsRoot(cwd), name);
|
|
if (!fs.existsSync(wsDir)) return null;
|
|
|
|
const activeWorkstreamName = options.active === undefined ? getActiveWorkstream(cwd) : options.active;
|
|
const writeDiagnostic = options.writeDiagnostic ?? ((message: string) => process.stderr.write(message));
|
|
const p = planningPaths(cwd, name);
|
|
const phaseDirNames = readSubdirectories(p.phases);
|
|
|
|
// #2562: scope progress to the CURRENT milestone. Membership and the
|
|
// denominator are derived in ONE key space (`phaseKeyFromDir` /
|
|
// `phaseKeyFromProse`, both from the phase-id owner module) so the two sides
|
|
// of the rollup cannot disagree.
|
|
const currentVersion = readCurrentMilestoneVersion(p.state, p.roadmap);
|
|
const progressRows = parseRoadmapProgressRows(p.roadmap);
|
|
|
|
// Phase keys the ROADMAP attributes to the current milestone. A row whose
|
|
// Milestone cell is blank or malformed is INCLUDED rather than dropped: a
|
|
// phase we cannot attribute must still be visible to the rollup. Dropping it
|
|
// from both sides was the silent-deletion defect — it let an unstarted phase
|
|
// vanish and the percentage round to 100. Over-inclusive-never-under is the
|
|
// degrade direction this codebase already commits to for unparseable roadmap
|
|
// input (see the getMilestonePhaseFilter catch in roadmap-parser.cts).
|
|
const currentMilestoneKeys = new Set<string>();
|
|
if (currentVersion) {
|
|
for (const row of progressRows) {
|
|
if (row.version === null || row.version === currentVersion) currentMilestoneKeys.add(row.key);
|
|
}
|
|
}
|
|
|
|
// Roadmap-heading membership, from the module that OWNS milestone-phase
|
|
// filtering. Consulted only when it is genuinely scoped to a single milestone
|
|
// (`versionScoped`); the unversioned whole-roadmap shape spans the project's
|
|
// lifetime and would re-admit prior-milestone phases — the very defect here.
|
|
const headingFilter = getMilestonePhaseFilter(cwd, currentVersion, null, name);
|
|
const headingScoped = headingFilter.versionScoped && headingFilter.phaseCount > 0;
|
|
|
|
// Phase keys the ROADMAP attributes to some OTHER milestone. A row carrying an
|
|
// explicit version that is not the current one is a positive claim by a prior
|
|
// (or future) milestone — the only reliable evidence that a phase does NOT
|
|
// belong to the current one.
|
|
const claimedElsewhere = new Set<string>();
|
|
for (const row of progressRows) {
|
|
if (row.version !== null && row.version !== currentVersion) claimedElsewhere.add(row.key);
|
|
}
|
|
|
|
// #2562: the current milestone is DECLARED but nothing attributes a phase to
|
|
// it yet — the window right after `/gsd-new-milestone`, where STATE.md's
|
|
// `milestone:` field updates the moment the heading lands but the Progress
|
|
// table and phase sections have not caught up.
|
|
//
|
|
// Treating that as "unscoped" was a hole in the original fix: scoping switched
|
|
// off entirely and the fallback below counted the project's ENTIRE phase
|
|
// history as both numerator and denominator, so a workstream whose current
|
|
// milestone had zero phases done reported 100% off its predecessors' work.
|
|
// That is the very symptom #2562 reports, reached by a different route.
|
|
//
|
|
// Three independent signals witness it; ANY of them is enough, and each covers
|
|
// a ROADMAP shape the others miss:
|
|
// - `versionSectionFound` — the milestone's own section exists but declares
|
|
// no phases (heading-only ROADMAPs, and the common `## v3.0` stub).
|
|
// - `missingExplicitVersion` — the ROADMAP versions its milestones but has
|
|
// no section for this one at all.
|
|
// - a Progress table that attributes every row elsewhere (`claimedElsewhere`
|
|
// non-empty while `currentMilestoneKeys` is empty).
|
|
// A ROADMAP that attributes NO versions anywhere matches none of them: its
|
|
// rows parse with `version: null`, land in `currentMilestoneKeys`, and never
|
|
// reach here. That is deliberate — for a free-form legacy project the
|
|
// whole-roadmap count IS the current milestone, and `readCurrentMilestoneVersion`
|
|
// hands back a non-null version for almost every project, so keying off
|
|
// `currentVersion` alone would regress every one of them to 0%.
|
|
const currentMilestoneDeclaredEmpty =
|
|
currentVersion !== null &&
|
|
currentMilestoneKeys.size === 0 &&
|
|
!headingScoped &&
|
|
(headingFilter.versionSectionFound || headingFilter.missingExplicitVersion || claimedElsewhere.size > 0);
|
|
|
|
// A dir-only phase joins the current milestone when the roadmap names it, or
|
|
// when it is a sub-phase (`30.1-…`) of a phase the roadmap names — sub-phases
|
|
// inserted mid-milestone rarely get a row of their own. Membership feeds BOTH
|
|
// the numerator and (via `milestoneKeys` below) the denominator, so a member
|
|
// can never exceed the denominator that counts it.
|
|
const scoped = currentMilestoneKeys.size > 0 || headingScoped || currentMilestoneDeclaredEmpty;
|
|
const isDirInCurrentMilestone = (dir: string): boolean => {
|
|
if (!scoped) return true;
|
|
const key = phaseKeyFromDir(dir);
|
|
if (currentMilestoneKeys.has(key)) return true;
|
|
const parent = parentPhaseKey(key);
|
|
if (parent !== null && currentMilestoneKeys.has(parent)) return true;
|
|
// An empty current milestone has no roadmap declarations to match against,
|
|
// so membership inverts: a directory belongs UNLESS another milestone claims
|
|
// it. A phase scaffolded before the roadmap caught up would otherwise vanish
|
|
// from both sides of the rollup — under-reporting, the direction this
|
|
// codebase never degrades in.
|
|
if (currentMilestoneDeclaredEmpty) {
|
|
const parentKey = parentPhaseKey(key);
|
|
return !claimedElsewhere.has(key) && (parentKey === null || !claimedElsewhere.has(parentKey));
|
|
}
|
|
return headingScoped && headingFilter(dir);
|
|
};
|
|
|
|
// Collect per-phase file counts (+ canonical key, milestone membership,
|
|
// verification verdict). `phaseKey` lets the builder de-duplicate stale
|
|
// same-numbered directories (Bug #2445's scenario) in the rollup.
|
|
//
|
|
// #2645 review: built from `[...phaseDirNames].sort()`, NOT the raw
|
|
// `phaseDirNames` (unsorted `readdirSync` order — `readSubdirectories`'s
|
|
// default). `buildWorkstreamInventory`'s own de-dup (`rollupDirByKey`,
|
|
// workstream-inventory-builder.cts) iterates the SAME sorted order; the
|
|
// ledger-winner tie-break below (incumbent wins unless a later entry has a
|
|
// STRICTLY newer mtime) only agrees with the builder's winner on an exact
|
|
// mtime tie if both walk entries in the same order. Iterating unsorted
|
|
// input here let the two independently pick different "winning"
|
|
// directories for one phase key on a tie — reopening the stale-duplicate-
|
|
// clobbers-live-verdict hole criterion 4 exists to close.
|
|
const rawPhaseEntries = [...phaseDirNames].sort().map(dir => {
|
|
const phaseDir = path.join(p.phases, dir);
|
|
const counts = countPhaseFiles(phaseDir);
|
|
const verificationResult = readVerificationStatus(phaseDir);
|
|
// #3057 B3: routing is UNCHANGED — `liveVerificationStatus` below is still
|
|
// `.status`, exactly as before, so the ledger/rollup logic that consumes
|
|
// it is unaffected. This only makes an indeterminate staleness check
|
|
// visible (stderr), matching cmdGitBaseBranch's own non-blocking
|
|
// unverified-fallback diagnostic (#3057 B4) — the closest existing idiom,
|
|
// since `WorkstreamInventory`'s aggregate return shape carries no
|
|
// per-phase verification detail for this to attach to.
|
|
if (verificationResult.staleCheckIndeterminate) {
|
|
writeDiagnostic(
|
|
`⚠ workstream-inventory: verification staleness check could not complete for phase directory '${dir}' in workstream '${name}' — routed as not-stale, but this was not actually verified. See #3057.\n`,
|
|
{ phaseDir: dir, reason: 'staleCheckIndeterminate' },
|
|
);
|
|
}
|
|
return {
|
|
directory: dir,
|
|
phaseKey: phaseKeyFromDir(dir),
|
|
mtimeMs: phaseDirMtime(phaseDir),
|
|
planCount: counts.planCount,
|
|
summaryCount: counts.summaryCount,
|
|
inMilestone: isDirInCurrentMilestone(dir),
|
|
liveVerificationStatus: verificationResult.status,
|
|
};
|
|
});
|
|
|
|
// #2645: only the directory Bug #2445's de-dup rollup would actually pick
|
|
// for a phase key may read or write that key's ledger entry. Letting every
|
|
// same-keyed directory (including a stale leftover) write would let a
|
|
// stale duplicate's stale verdict clobber the live directory's remembered
|
|
// one.
|
|
//
|
|
// #2645 review — CORRECTED: an earlier version of this comment claimed the
|
|
// milestone-scoping exclusion (`scoped && entry.inMilestone === false`)
|
|
// was safe to drop here as "out of scope", and hand-wrote a scoping-free
|
|
// tie-break rule. That was a real bug, not a scope call: in a SCOPED
|
|
// workstream, a stale OUT-of-milestone directory sharing a phase key with
|
|
// the live IN-milestone one can have a newer mtime (plausible after a
|
|
// checkout/rebase resets mtimes) and would then win THIS selection while
|
|
// the builder's own `rollupDirByKey` — which DOES apply the scoping filter
|
|
// — picks the live directory instead. `isLedgerWinner` would then be false
|
|
// for the live directory, so deleting ITS `*-VERIFICATION.md` would never
|
|
// consult the ledger and would reopen #2645's exact hole for the phase
|
|
// that actually counts toward `completed_phases` — reachable with a plain
|
|
// `rm`, no ledger tampering required. Fixed by calling the SAME shared
|
|
// `pickRollupWinners` the builder's `rollupDirByKey` now also calls, with
|
|
// the identical scoping filter, rather than a second hand-written copy.
|
|
const ledgerWinnerByKey = pickRollupWinners(
|
|
rawPhaseEntries,
|
|
(entry) => entry.phaseKey,
|
|
(entry) => entry.mtimeMs,
|
|
(entry) => !(scoped && entry.inMilestone === false),
|
|
);
|
|
|
|
const ledgerRead = readVerificationLedger(wsDir);
|
|
// Both `'corrupt'` and `'ok'` start from whatever entries could actually be
|
|
// trusted (empty for `'corrupt'` — nothing in an unparseable file is
|
|
// trusted) and get REPAIRED below by any real verdict this call observes;
|
|
// only `'absent'` skips the ledger mechanism entirely (pre-adoption).
|
|
const verificationLedger = ledgerRead.entries;
|
|
let ledgerDirty = false;
|
|
for (const winner of ledgerWinnerByKey.values()) {
|
|
if (winner.liveVerificationStatus === 'missing') continue;
|
|
if (verificationLedger[winner.phaseKey] !== winner.liveVerificationStatus) {
|
|
verificationLedger[winner.phaseKey] = winner.liveVerificationStatus;
|
|
ledgerDirty = true;
|
|
}
|
|
}
|
|
// A `'corrupt'` read that observes no real verdict this call has nothing to
|
|
// repair with — writing an empty `{}` would DESTROY whatever the corrupt
|
|
// file's bytes might still hold (a human could recover it by hand; this
|
|
// fix must not foreclose that). Only write when there is something real to
|
|
// persist, exactly as for `'absent'`/`'ok'`.
|
|
if (ledgerDirty) writeVerificationLedger(wsDir, verificationLedger);
|
|
|
|
const phaseFilesCounts = rawPhaseEntries.map(entry => {
|
|
const isLedgerWinner = ledgerWinnerByKey.get(entry.phaseKey) === entry;
|
|
let verificationStatus = entry.liveVerificationStatus;
|
|
if (entry.liveVerificationStatus === 'missing' && isLedgerWinner) {
|
|
if (ledgerRead.state === 'absent') {
|
|
// State 1: pre-adoption. Exactly today's behavior — 'missing' is
|
|
// NOT in FAILING_VERIFICATION_STATUSES, so this does not gate.
|
|
verificationStatus = 'missing';
|
|
} else {
|
|
// States 2/3 ('corrupt' or 'ok'): this workstream has adopted the
|
|
// ledger. A remembered entry wins; no entry fails CLOSED to the
|
|
// 'unrecorded' sentinel rather than falling open to 'missing'.
|
|
const remembered = verificationLedger[entry.phaseKey];
|
|
verificationStatus = remembered !== undefined ? remembered : 'unrecorded';
|
|
}
|
|
}
|
|
return {
|
|
directory: entry.directory,
|
|
phaseKey: entry.phaseKey,
|
|
mtimeMs: entry.mtimeMs,
|
|
planCount: entry.planCount,
|
|
summaryCount: entry.summaryCount,
|
|
inMilestone: entry.inMilestone,
|
|
verificationStatus,
|
|
};
|
|
});
|
|
|
|
// The denominator is the union of what the roadmap DECLARES for the current
|
|
// milestone (including never-scaffolded phases) and the keys of the member
|
|
// directories (including dir-only sub-phases). One key space, so
|
|
// `completed_phases <= denominator` holds by construction rather than by a
|
|
// `Math.min` cap that hid the inconsistency.
|
|
const milestoneKeys = new Set(currentMilestoneKeys);
|
|
for (const entry of phaseFilesCounts) {
|
|
if (entry.inMilestone) milestoneKeys.add(entry.phaseKey);
|
|
}
|
|
const currentMilestonePhaseCount = scoped
|
|
? Math.max(milestoneKeys.size, headingScoped ? headingFilter.phaseCount : 0)
|
|
: 0;
|
|
|
|
// Unscoped fallback: the denominator must STILL count phases the ROADMAP
|
|
// declares in its Progress table but never scaffolded — the heading-only
|
|
// count drops them, even when other headings exist. Union the declared rows
|
|
// with the phase directories so neither source can silently shrink it.
|
|
let fallbackPhaseCount = countRoadmapPhases(p.roadmap, phaseDirNames.length);
|
|
if (!scoped && progressRows.length > 0) {
|
|
const union = new Set(progressRows.map(row => row.key));
|
|
for (const entry of phaseFilesCounts) union.add(entry.phaseKey);
|
|
fallbackPhaseCount = union.size;
|
|
}
|
|
|
|
return buildWorkstreamInventory({
|
|
name,
|
|
projectDir: cwd,
|
|
workstreamDir: wsDir,
|
|
phaseDirNames,
|
|
activeWorkstreamName: activeWorkstreamName ?? '',
|
|
phaseFilesCounts,
|
|
roadmapPhaseCount: fallbackPhaseCount,
|
|
currentMilestonePhaseCount,
|
|
// Stated, not inferred from the count: a declared-but-empty current
|
|
// milestone is legitimately scoped AND legitimately zero-phase, and the
|
|
// builder cannot tell those apart from `currentMilestonePhaseCount` alone.
|
|
milestoneScoped: scoped,
|
|
stateProjection: readStateProjection(p.state),
|
|
filesExist: {
|
|
roadmap: fs.existsSync(p.roadmap),
|
|
state: fs.existsSync(p.state),
|
|
requirements: fs.existsSync(p.requirements),
|
|
},
|
|
milestoneShippedSignal: workstreamShippedSignal(p.roadmap, p.planning, currentVersion),
|
|
});
|
|
}
|
|
|
|
function listWorkstreamInventories(cwd: string): WorkstreamInventoryList {
|
|
const wsRoot = workstreamsRoot(cwd);
|
|
if (!fs.existsSync(wsRoot)) {
|
|
return {
|
|
mode: 'flat',
|
|
active: null,
|
|
workstreams: [],
|
|
count: 0,
|
|
message: 'No workstreams — operating in flat mode',
|
|
};
|
|
}
|
|
|
|
const active = getActiveWorkstream(cwd);
|
|
const entries = fs.readdirSync(wsRoot, { withFileTypes: true });
|
|
const workstreams: WorkstreamInventory[] = [];
|
|
for (const entry of entries) {
|
|
if (!entry.isDirectory()) continue;
|
|
const inventory = inspectWorkstream(cwd, entry.name, { active });
|
|
if (inventory) workstreams.push(inventory);
|
|
}
|
|
|
|
const ordered = sortWorkstreamInventories(workstreams, active);
|
|
|
|
return {
|
|
mode: 'workstream',
|
|
active,
|
|
workstreams: ordered,
|
|
count: ordered.length,
|
|
};
|
|
}
|
|
|
|
function getOtherActiveWorkstreamInventories(cwd: string, excludeWs: string): WorkstreamInventory[] {
|
|
return listWorkstreamInventories(cwd).workstreams
|
|
.filter(inventory => inventory.name !== excludeWs)
|
|
.filter(inventory => !isCompletedInventory(inventory.status));
|
|
}
|
|
|
|
export = {
|
|
countPhaseFiles,
|
|
countRoadmapPhases,
|
|
getOtherActiveWorkstreamInventories,
|
|
inspectWorkstream,
|
|
isCompletedInventory,
|
|
listWorkstreamInventories,
|
|
sortWorkstreamInventories,
|
|
workstreamsRoot,
|
|
};
|