fix(3739): gap-checker now detects padded-prefix CONTEXT.md (#3764)

* test(3739): add RED tests for padded-prefix CONTEXT.md gap-checker miss

Covers bare and padded (01-CONTEXT.md, 02.1-CONTEXT.md) forms, an
uncovered-decision counter-test, and unit tests for the upcoming
findContextMdIn() helper. All 6 new tests fail before the fix.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3739): extract findContextMdIn() helper; fix gap-checker bare lookup

gap-checker.cjs:136 used a bare path.join(absPhaseDir, 'CONTEXT.md')
that silently returned '' for any phase using the padded-prefix
convention (01-CONTEXT.md, 02.1-CONTEXT.md, etc.).

Extract findContextMdIn(absDir) to planning-workspace.cjs — the module
already imported by gap-checker, init, roadmap, and core — and wire
gap-checker.cjs to call it instead of the bare lookup.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* refactor(3739): replace inline dual-form predicate with findContextMdIn() at all 4 remaining sites

The dual-form predicate `f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md'`
existed verbatim at 5 sites across init.cjs (×3), roadmap.cjs, and
core.cjs — Rule of Three mandates extraction at ≥3 sites. All 4
remaining call sites now delegate to findContextMdIn() from
planning-workspace.cjs. No behaviour change; all existing tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore(3739): update changeset to reference PR #3764

* fix(3739): findContextMdIn prefers bare CONTEXT.md over padded form (deterministic precedence)

`.find()` with `f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md'` returned
the first match in `readdirSync` order — undefined on most filesystems.
When both `CONTEXT.md` and `01-CONTEXT.md` exist the winner was arbitrary.

The old gap-checker.cjs:136 code always used bare `CONTEXT.md` first
(existsSync on the bare path). Restore that invariant: check
`files.includes('CONTEXT.md')` before falling through to the padded scan.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test(3739): dual-file precedence test — bare CONTEXT.md wins over padded form

Adds two test cases for the scenario where both CONTEXT.md and
01-CONTEXT.md exist in the same phase directory:

1. Helper level: findContextMdIn() must return 'CONTEXT.md' (not the
   padded filename) when both files are present on disk.
2. Integration level: gap-analysis must resolve decisions from the bare
   form only; D-PADDED (from 01-CONTEXT.md) must not appear when the
   bare form shadows it.

Without these tests a future change to findContextMdIn could silently
regress the precedence guarantee.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3739): bug-2798 tests skip cleanly when sdk/dist is absent (was hard-fail)

The 3 tests in bug-2798-context-window-config-key.test.cjs invoke the built
SDK CLI (sdk/dist/cli.js) and require sdk/dist/query/config-schema.js. When
dist is absent, they threw hard errors rather than observable skips.

Apply the same `if (!existsSync(...)) { t.skip(...); return; }` guard used in
bug-2767-gsd-sdk-commit-files-flag.test.cjs (c2812313). Tests 1 & 2 guard on
sdk/dist/cli.js; test 3 guards on sdk/dist/query/config-schema.js. When dist
is present all 3 run; when absent all 3 emit actionable skip lines.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3739): eliminate double readdirSync in findContextMdIn callers

findContextMdIn now accepts either a directory path or an already-read
files array, allowing callers that already hold a directory listing
(core.cjs:getPhaseFileStats, roadmap.cjs:countPhasePlansAndSummaries,
gap-checker.cjs:runGapAnalysis) to skip redundant readdirSync calls.

Test coverage added for the array-argument overload.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: redesign flaky concurrent add-blocker test with deterministic barrier

Previous design relied on OS scheduler to interleave two subprocess writes,
producing a flake under CI load. Redesigned using a file-barrier (Option A)
that forces both subprocesses to reach a ready-gate before either proceeds,
guaranteeing true concurrent lock contention and eliminating timing dependency.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-20 22:39:34 -04:00
committed by GitHub
parent aab01f43e9
commit c1c8b0d109
9 changed files with 404 additions and 24 deletions

View File

@@ -23,6 +23,7 @@ const {
withPlanningLock,
getActiveWorkstream,
setActiveWorkstream,
findContextMdIn,
} = require('./planning-workspace.cjs');
const { findProjectRoot } = require('./project-root.generated.cjs');
@@ -1810,7 +1811,7 @@ function getPhaseFileStats(phaseDir) {
plans: filterPlanFiles(files),
summaries: filterSummaryFiles(files),
hasResearch: files.some(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md'),
hasContext: files.some(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md'),
hasContext: findContextMdIn(files) !== null,
hasVerification: files.some(f => f.endsWith('-VERIFICATION.md') || f === 'VERIFICATION.md'),
hasReviews: files.some(f => f.endsWith('-REVIEWS.md') || f === 'REVIEWS.md'),
};

View File

@@ -17,7 +17,7 @@
const fs = require('fs');
const path = require('path');
const { escapeRegex, output, error } = require('./core.cjs');
const { planningPaths, planningDir } = require('./planning-workspace.cjs');
const { planningPaths, planningDir, findContextMdIn } = require('./planning-workspace.cjs');
const { parseDecisions } = require('./decisions.cjs');
/**
@@ -133,16 +133,24 @@ function runGapAnalysis(cwd, phaseDir) {
const reqMd = fs.existsSync(reqPath) ? fs.readFileSync(reqPath, 'utf-8') : '';
const reqItems = parseRequirements(reqMd).map(r => ({ ...r, source: 'REQUIREMENTS.md' }));
const ctxPath = path.join(absPhaseDir, 'CONTEXT.md');
const ctxMd = fs.existsSync(ctxPath) ? fs.readFileSync(ctxPath, 'utf-8') : '';
// Read the phase directory once; reuse the listing for both context detection
// and plan-file enumeration (avoids redundant readdirSync calls).
let phaseDirFiles = [];
try {
if (fs.existsSync(absPhaseDir)) phaseDirFiles = fs.readdirSync(absPhaseDir);
} catch { /* unreadable */ }
const ctxFile = findContextMdIn(phaseDirFiles);
const ctxPath = ctxFile ? path.join(absPhaseDir, ctxFile) : null;
const ctxMd = ctxPath ? fs.readFileSync(ctxPath, 'utf-8') : '';
const dItems = parseDecisions(ctxMd).map(d => ({ ...d, source: 'CONTEXT.md' }));
const items = [...reqItems, ...dItems];
let planText = '';
try {
if (fs.existsSync(absPhaseDir)) {
const files = fs.readdirSync(absPhaseDir).filter(f => /-PLAN\.md$/.test(f));
if (phaseDirFiles.length > 0) {
const files = phaseDirFiles.filter(f => /-PLAN\.md$/.test(f));
planText = files.map(f => {
try { return fs.readFileSync(path.join(absPhaseDir, f), 'utf-8'); }
catch { return ''; }

View File

@@ -6,7 +6,7 @@ const fs = require('fs');
const path = require('path');
const { execGit, platformWriteSync, platformReadSync } = require('./shell-command-projection.cjs');
const { loadConfig, resolveModelInternal, findPhaseInternal, getRoadmapPhaseInternal, pathExistsInternal, gitWorktreeInfoInternal, generateSlugInternal, getMilestoneInfo, getMilestonePhaseFilter, stripShippedMilestones, extractCurrentMilestone, normalizePhaseName, toPosixPath, output, error, checkAgentsInstalled, phaseTokenMatches } = require('./core.cjs');
const { planningPaths, planningDir, planningRoot } = require('./planning-workspace.cjs');
const { planningPaths, planningDir, planningRoot, findContextMdIn } = require('./planning-workspace.cjs');
const { maskIfSecret } = require('./secrets.cjs');
const scanPhasePlans = require('./plan-scan.cjs');
const { stateExtractField } = require('./state-document.cjs');
@@ -407,7 +407,7 @@ function cmdInitPlanPhase(cwd, phase, raw, options = {}) {
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const contextFile = files.find(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md');
const contextFile = findContextMdIn(phaseDirFull);
if (contextFile) {
result.context_path = toPosixPath(path.join(phaseInfo.directory, contextFile));
}
@@ -885,7 +885,7 @@ function cmdInitPhaseOp(cwd, phase, raw) {
const phaseDirFull = path.join(cwd, phaseInfo.directory);
try {
const files = fs.readdirSync(phaseDirFull);
const contextFile = files.find(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md');
const contextFile = findContextMdIn(phaseDirFull);
if (contextFile) {
result.context_path = toPosixPath(path.join(phaseInfo.directory, contextFile));
}
@@ -1198,7 +1198,7 @@ function cmdInitManager(cwd, raw) {
const phaseFiles = fs.readdirSync(fullDir);
planCount = listPhasePlanFiles(fullDir).length;
summaryCount = listPhaseSummaryFiles(fullDir).length;
hasContext = phaseFiles.some(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md');
hasContext = findContextMdIn(fullDir) !== null;
hasResearch = phaseFiles.some(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md');
if (summaryCount >= planCount && planCount > 0) diskStatus = 'complete';

View File

@@ -371,6 +371,35 @@ function setActiveWorkstream(cwd, name) {
createPlanningWorkspace(cwd).activeWorkstream.set(name);
}
/**
* Locate the CONTEXT.md file in a phase directory, handling both the bare
* form (`CONTEXT.md`) and the padded-prefix convention (`NN-CONTEXT.md`,
* `NN.N-CONTEXT.md`, etc.) used by gsd-discuss-phase output.
*
* Returns the filename (not the full path) of the first match, or null if
* no CONTEXT.md exists in the directory.
*
* Canonical dual-form predicate extracted here to eliminate the 5-site
* duplication that previously existed across init.cjs, roadmap.cjs,
* core.cjs, gap-checker.cjs (#3739).
*
* @param {string|string[]} absDirOrFiles - Absolute path to the phase directory,
* OR an already-read files array (avoids a redundant readdirSync at call sites
* that already hold a directory listing).
* @returns {string|null}
*/
function findContextMdIn(absDirOrFiles) {
try {
const files = Array.isArray(absDirOrFiles)
? absDirOrFiles
: fs.readdirSync(absDirOrFiles);
if (files.includes('CONTEXT.md')) return 'CONTEXT.md';
return files.find(f => f.endsWith('-CONTEXT.md')) ?? null;
} catch {
return null;
}
}
module.exports = {
createPlanningWorkspace,
createSharedPointerAdapter,
@@ -382,4 +411,5 @@ module.exports = {
withPlanningLock,
getActiveWorkstream,
setActiveWorkstream,
findContextMdIn,
};

View File

@@ -6,7 +6,7 @@ const fs = require('fs');
const path = require('path');
const { escapeRegex, normalizePhaseName, phaseMarkdownRegexSource, phaseMarkdownRegexSourceExact, output, error, findPhaseInternal, stripShippedMilestones, extractCurrentMilestone, replaceInCurrentMilestone, phaseTokenMatches } = require('./core.cjs');
const { platformWriteSync } = require('./shell-command-projection.cjs');
const { planningPaths, withPlanningLock } = require('./planning-workspace.cjs');
const { planningPaths, withPlanningLock, findContextMdIn } = require('./planning-workspace.cjs');
const scanPhasePlans = require('./plan-scan.cjs');
/**
@@ -41,13 +41,13 @@ function coerceTruthToString(t) {
function countPhasePlansAndSummaries(phaseDir) {
const { planCount, summaryCount } = scanPhasePlans(phaseDir);
// hasContext and hasResearch are not plan-scan concerns — read the directory
// once for the non-plan metadata that cmdRoadmapAnalyze needs.
// once and share the listing for all non-plan metadata that cmdRoadmapAnalyze needs.
let phaseFiles = [];
try { phaseFiles = fs.readdirSync(phaseDir); } catch { /* empty */ }
return {
planCount,
summaryCount,
hasContext: phaseFiles.some(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md'),
hasContext: findContextMdIn(phaseFiles) !== null,
hasResearch: phaseFiles.some(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md'),
};
}