fix(#3849): phase allocation counts numbers held by sibling git worktrees (#4042)

* test(#3849): phase allocation must skip numbers held by sibling worktrees (failing first)

* fix(#3849): phase allocation counts numbers held by sibling git worktrees

Both allocators (cmdPhaseAdd, cmdPhaseAddBatch) chose max+1 over numbers
gathered from ONE checkout — headers, bullets (add only), on-disk dirs.
Every sibling git worktree carries its own .planning/ on its own branch,
so a phase minted there was invisible and the same number was allocated
twice (the reported incident: two Phase 441s, one with six written
plans, surfaced a day late by human memory).

New shared horizon collectSiblingWorktreePhaseNums: one
git worktree list --porcelain, then per sibling — phase-dir names (the
cheap scan that would have caught the incident) and the WHOLE sibling
ROADMAP.md headers (a row can predate its dir; milestone-scoping would
be wrong — a number used under any milestone on another branch is
taken). Widen, never refuse: unreadable sibling / no .planning / not a
git repo / git unavailable each contribute nothing and allocation is
unchanged. Reuses isSentinelPhaseId and the allocators' own patterns.

Secondary (#1229 never reached batch): cmdPhaseAddBatch now also scans
roadmap bullets — a bullet-only 'Phase N' row was invisible to batch
allocation, exactly the condition #1229 was filed for.

Also fixes the two new tests' result-key access (output.phases, not
output.results).

* fix(#3849): review fold-ins — subprocess band, bounded test git, linked-worktree fixture

- execFileSync options now match the repo's git band (10s window,
  windowsHide, 4MiB maxBuffer) — a spurious 4s timeout silently reverted
  to the pre-fix collision.
- test git helper bounded (15s) per local/no-unbounded-spawn.
- new fixture: allocation FROM a linked worktree counts the main
  checkout — the incident's actual topology direction.
- fixture-setup rmSync carries the sanctioned lint-disable (setup, not
  teardown; cleanup() still owns directory removal).

* fix(#3849): exempt the sibling-worktree scan from the enumeration-drift guard

collectSiblingWorktreePhaseNums reads a SIBLING checkout's phases dir —
a different question from the cwd-scoped listMilestonePhaseDirs the
guard routes everything to (which cannot see another worktree's
.planning at all). Function-scoped, per ADR-3180 Decision 4(a): any
other re-derivation in phase.cts is still caught. The GREEN bench
caught the omission.

* chore(#3849): changeset fragment (pr number backfilled after PR creation)

* chore(#3849): backfill changeset PR number (4042)

---------

Co-authored-by: sim <sim@local>
This commit is contained in:
Tom Boucher
2026-08-29 11:35:19 -04:00
committed by GitHub
parent 519ac23ebb
commit 0bf778c352
4 changed files with 255 additions and 1 deletions

View File

@@ -2495,6 +2495,171 @@ describe('find-phase scalar counts (#3218)', () => {
});
});
// ─────────────────────────────────────────────────────────────────────────────
// phase number allocation across sibling git worktrees (#3849)
// ─────────────────────────────────────────────────────────────────────────────
describe('phase add allocation vs sibling git worktrees (#3849)', () => {
const activeWorktrees = [];
const activeDirs = [];
function git(args, cwd) {
return execFileSync('git', args, { cwd, encoding: 'utf-8', timeout: 15_000 });
}
function initRepo(repoDir) {
fs.mkdirSync(path.join(repoDir, '.planning', 'phases'), { recursive: true });
fs.writeFileSync(
path.join(repoDir, '.planning', 'ROADMAP.md'),
['# Roadmap v1.0', '', '### Phase 440: existing thing', '**Goal:** Setup', '', '---', ''].join('\n')
);
fs.mkdirSync(path.join(repoDir, '.planning', 'phases', '440-existing-thing'));
git(['init', '-b', 'main'], repoDir);
git(['config', 'user.email', 'test@example.com'], repoDir);
git(['config', 'user.name', 'Test'], repoDir);
git(['add', '-A'], repoDir);
git(['commit', '-m', 'init'], repoDir);
}
/** Materialize the issue's repro: a sibling worktree branch holding Phase 441. */
function addSiblingHolding441(repoDir) {
const sha = git(['rev-parse', 'HEAD'], repoDir).trim();
const worktreeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-sib-'));
git(['worktree', 'add', '--detach', worktreeDir, sha], repoDir);
activeWorktrees.push({ repoDir, worktreeDir });
fs.mkdirSync(path.join(worktreeDir, '.planning', 'phases', '441-conversation-surface'), { recursive: true });
fs.writeFileSync(
path.join(worktreeDir, '.planning', 'ROADMAP.md'),
['# Roadmap v1.0', '', '### Phase 440: existing thing', '**Goal:** Setup', '', '### Phase 441: conversation surface', '**Goal:** Talk', '', '---', ''].join('\n')
);
return worktreeDir;
}
/** A sibling worktree whose checkout has no .planning at all (fail-open case). */
function addBareSibling(repoDir) {
const sha = git(['rev-parse', 'HEAD'], repoDir).trim();
const worktreeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-bare-'));
git(['worktree', 'add', '--detach', worktreeDir, sha], repoDir);
activeWorktrees.push({ repoDir, worktreeDir });
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- fixture SETUP, not teardown: strips the tracked .planning/ so this worktree has none; cleanup() owns the dir's removal with the Windows retry budget
fs.rmSync(path.join(worktreeDir, '.planning'), { recursive: true, force: true });
return worktreeDir;
}
function teardown() {
while (activeWorktrees.length) {
const { repoDir, worktreeDir } = activeWorktrees.pop();
try {
git(['worktree', 'remove', '--force', worktreeDir], repoDir);
} catch (_) { /* best-effort; cleanup() below still removes the directory */ }
cleanup(worktreeDir);
}
while (activeDirs.length) cleanup(activeDirs.pop());
}
afterEach(teardown);
test('phase add skips a number held only by a sibling worktree (dir + roadmap header)', () => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-main-'));
activeDirs.push(repoDir);
initRepo(repoDir);
addSiblingHolding441(repoDir);
const result = runGsdTools('phase add anything', repoDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(
output.phase_number,
442,
'#3849: sibling worktree holds Phase 441 — allocation must skip to 442, not collide'
);
assert.ok(
fs.existsSync(path.join(repoDir, '.planning', 'phases', '442-anything')),
'directory for the non-colliding 442 should be created'
);
});
test('phase add-batch skips a number held only by a sibling worktree', () => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-batch-'));
activeDirs.push(repoDir);
initRepo(repoDir);
addSiblingHolding441(repoDir);
const result = runGsdTools(['phase', 'add-batch', '--descriptions', '["New Thing"]'], repoDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(
output.phases[0].phase_number,
442,
'#3849: the batch allocator must widen its horizon the same way the single-add allocator does'
);
});
test('a sibling worktree without .planning/ changes nothing (fail open)', () => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-failopen-'));
activeDirs.push(repoDir);
initRepo(repoDir);
addBareSibling(repoDir);
const result = runGsdTools('phase add next thing', repoDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.phase_number, 441, 'no sibling numbers visible — max+1 from the local 440');
});
test('allocation FROM a linked worktree counts the main checkout too (the incident topology)', () => {
const repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-linked-'));
activeDirs.push(repoDir);
initRepo(repoDir);
// The real incident ran the other direction: cwd is a linked worktree and
// the MAIN checkout (scanned as a sibling here) holds the higher number.
const sha = git(['rev-parse', 'HEAD'], repoDir).trim();
const worktreeDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-3849-linked-wt-'));
git(['worktree', 'add', '--detach', worktreeDir, sha], repoDir);
activeWorktrees.push({ repoDir, worktreeDir });
// eslint-disable-next-line local/no-raw-rmsync-in-tests -- fixture SETUP, not teardown: strips the tracked .planning/ so this worktree has none; cleanup() owns the dir's removal with the Windows retry budget
fs.rmSync(path.join(worktreeDir, '.planning'), { recursive: true, force: true });
fs.mkdirSync(path.join(worktreeDir, '.planning', 'phases'), { recursive: true });
fs.writeFileSync(
path.join(worktreeDir, '.planning', 'ROADMAP.md'),
['# Roadmap v1.0', '', '### Phase 440: base', '**Goal:** Setup', '', '---', ''].join('\n')
);
const result = runGsdTools('phase add from linked', worktreeDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(
output.phase_number,
441,
'#3849: the main checkout (440) must be visible when allocating from a linked worktree'
);
});
test('phase add-batch counts bullet-only Phase N rows (#1229 reached the batch path)', () => {
const tmp = createTempProject();
try {
fs.writeFileSync(
path.join(tmp, '.planning', 'ROADMAP.md'),
['# Roadmap v1.0', '', '- [ ] **Phase 11: bullet-only phase**', '', '---', ''].join('\n')
);
const result = runGsdTools(['phase', 'add-batch', '--descriptions', '["After Bullet"]'], tmp);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(
output.phases[0].phase_number,
12,
'#3849 secondary: a bullet-only Phase 11 must not be re-allocated by add-batch'
);
} finally {
cleanup(tmp);
}
});
});
// ─────────────────────────────────────────────────────────────────────────────
// phase add-batch command (#2165)
// ─────────────────────────────────────────────────────────────────────────────