fix(3785): case-insensitive depends_on resolution in phase resolver (#88)

* fix(3785): case-insensitive depends_on resolution in phase resolver

planMap, canonicalToId, and shortFormToId in phasePlanIndex used strict
Map.has() with no case normalization. A depends_on ref in mixed/lowercase
against an uppercase-suffix plan ID (e.g. '20-01-auth' → '20-01-Auth')
dropped the DAG edge, assigning the dependent plan to wave 1 instead of
wave 2. Fix: normalize all keys and lookup values to lowercase so the
three-tier resolution is case-insensitive. Adds regression test (#3785).

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

* chore(changeset): add PR 3798 changelog fragment

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

* fix(3785): detect case-fold collisions; apply lowercase-both to CJS path

- Add collision guard in both sdk/src/query/phase.ts (phasePlanIndex)
  and get-shit-done/bin/lib/phase.cjs (cmdPhasePlanIndex): when two plan
  IDs in the same phase are identical after toLowerCase(), throw/error
  immediately with a clear message naming both files instead of silently
  overwriting one in planMap and misrouting depends_on edges.
- Apply the same lowercase-both normalization (#3785) to cmdPhasePlanIndex
  in phase.cjs, which was missing from the original PR — planMap and
  canonicalToId keys are now lowercased on write; dep strings are
  lowercased before lookup.
- Add regression tests to tests/phase.test.cjs: case-insensitive
  resolution test (runs on all platforms) and collision-detection test
  (skipped on macOS/Windows where FS is case-insensitive).
- Update changeset to describe both the SDK and CJS fixes.

Identified via Codex adversarial review of PR #3798.

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

* fix(3785): address review — KNOWN GAP comment for CJS shortFormToId, canonical-casing tests, depends_on output normalization

- F1: Reword changeset for accuracy (plannerID drift trigger; two-tier CJS gap honest).
  Add KNOWN GAP comment in phase.cjs before Kahn's loop noting CJS lacks shortFormToId
  (tracked as follow-up parity gap, out of scope for #3785).
- F2: Add strict planA.id === '20-01-Auth' assertions in both SDK (vitest) and CJS (node --test)
  tests — a future regression that silently lowercases stored IDs would now fail the test.
- F3: Normalize depends_on output to canonical plan IDs in both SDK phase.ts and CJS phase.cjs.
  User-typed '20-01-auth' in depends_on resolves to '20-01-Auth' in output via planMap lookup.
  Add planB.depends_on === ['20-01-Auth'] assertions in both test suites.
- F5: Add seenLower guard-scope comment (full-ID collisions only; shared-prefix collisions
  handled by first-write-wins from sorted planFiles).
- F6: Add ASCII-safe toLowerCase comment at first call site in both SDK and CJS.
- F7: Add intentional-separation comment on seenLower vs planMap in both SDK and CJS.

Reviewers: gsd-code-reviewer (MN-01/NT-1) + sonnet adversarial.

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

* test(3785): cover case-insensitive depends_on resolution branches

Add 4 focused test cases exercising branches introduced by #3785:
- All-uppercase depends_on ref resolving to lowercase plan ID via planMap
- External cross-phase dep preserved as-is in Pass 3 output (planMap miss)
- Mixed-case short canonical prefix resolving via canonicalToId
- Plans with undefined/empty depends_on emit empty array correctly

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

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-22 16:54:10 -04:00
committed by GitHub
parent 334a64168e
commit 8b6ffca51f
5 changed files with 391 additions and 14 deletions

View File

@@ -444,12 +444,44 @@ function cmdPhasePlanIndex(cwd, phase, raw) {
// ── Pass 2: topological level assignment via depends_on DAG ──────────────
// Guard: detect case-insensitive key collisions before building dependency
// maps. Two plan IDs that differ only by case would silently overwrite each
// other in planMap, routing depends_on edges to whichever plan survived last.
// This is a configuration error — fail fast with the conflicting IDs. (#3785)
//
// This guard catches case-fold collisions on full plan IDs.
// Shared-numeric-prefix collisions (e.g. '20-01-Auth' and '20-01' both
// producing canonical '20-01') are resolved by first-write-wins ordering
// from sorted planFiles — not explicitly guarded here.
// seenLower is intentionally separate from planMap — it exists only to detect
// collisions before planMap is built, so the error fires before any Map
// entry silently overwrites another.
const seenLower = new Map(); // lowercase key → original id
for (const p of rawPlans) {
// ASCII plan IDs only — toLowerCase() is correct and locale-safe here.
const lower = p.id.toLowerCase();
const existing = seenLower.get(lower);
if (existing !== undefined) {
error(`depends_on index collision in phase ${normalized}: plan IDs '${existing}' and '${p.id}' are identical when case-folded. Rename one file to avoid ambiguous dependency resolution.`);
return;
}
seenLower.set(lower, p.id);
}
// Build a map from plan ID → raw plan for fast lookup.
// Deps that reference plans outside this phase are treated as external and ignored.
const planMap = new Map(rawPlans.map(p => [p.id, p]));
// Keys are lowercased so that depends_on refs with different casing still
// resolve to the correct plan (#3785: case-insensitive identifier resolution).
const planMap = new Map(rawPlans.map(p => [p.id.toLowerCase(), p]));
// Secondary index: canonical prefix → full plan ID, so depends_on: ['03-01'] resolves
// to '03-01-auth-hardening-PLAN.md'-derived ID '03-01-auth-hardening' (k015).
const canonicalToId = new Map(rawPlans.map(p => [extractCanonicalPlanId(p.id), p.id]));
// Keyed lowercase for the same case-insensitive reason (#3785).
const canonicalToId = new Map(rawPlans.map(p => [extractCanonicalPlanId(p.id).toLowerCase(), p.id]));
// KNOWN GAP: CJS resolver has only two tiers (planMap + canonicalToId);
// the SDK has an additional shortFormToId for same-phase short-form refs
// like '01' or '01A'. Adding the third tier here is tracked as a parity
// gap and is out of scope for #3785 / PR #3798.
// Kahn's algorithm — compute in-degree and adjacency for in-phase deps only.
const level = new Map();
@@ -461,7 +493,9 @@ function cmdPhasePlanIndex(cwd, phase, raw) {
if (!adj.has(p.id)) adj.set(p.id, []);
for (const dep of p.dependsOn) {
// Accept both full-stem ('03-01-auth-hardening') and canonical-prefix ('03-01') forms.
const resolvedDep = planMap.has(dep) ? dep : canonicalToId.get(dep);
// All lookups are lowercased so mixed-case depends_on refs resolve correctly (#3785).
const depLower = dep.toLowerCase();
const resolvedDep = planMap.has(depLower) ? planMap.get(depLower).id : canonicalToId.get(depLower);
if (!resolvedDep) continue; // external dep — ignore
if (!adj.has(resolvedDep)) adj.set(resolvedDep, []);
adj.get(resolvedDep).push(p.id);
@@ -537,7 +571,13 @@ function cmdPhasePlanIndex(cwd, phase, raw) {
const plan = {
id: raw.id,
wave: effectiveWave,
depends_on: raw.dependsOn,
// Resolve each user-typed dep to its canonical plan ID (preserving on-disk casing)
// so the output never reflects the user's case typo. Unresolved deps (external
// phase refs) are kept as-is since planMap only contains plans in this phase.
depends_on: raw.dependsOn.map(dep => {
const lower = String(dep).toLowerCase();
return planMap.has(lower) ? planMap.get(lower).id : dep;
}),
autonomous: raw.autonomous,
objective: raw.objective,
files_modified: raw.filesModified,