fix(3266): preserve wave 0 and bucket plans by depends_on DAG in phase-plan-index (#3276)

* fix(3266): preserve wave 0 and bucket plans by depends_on DAG in phase-plan-index

Fixes two cooperating bugs in the phase-plan-index builder:

1. Wave 0 collapse: `parseInt(...) || 1` coerced parsed value `0` to `1` due to
   JS falsy default. Fixed with `Number.isNaN` guard.
2. depends_on ignored: wave-bucketing used only the `wave:` frontmatter field.
   Now replaced with Kahn's topological-level algorithm over `depends_on`:
   source nodes (no in-phase deps) → lowest level; each plan's level = max(deps'
   levels) + 1.  Declared `wave:` that disagrees with computed level emits a
   non-fatal warning on the result.  Cycle detection throws GSDError.

`PlanInfo` gains `depends_on: string[]`. `PhasePlanIndex` gains `warnings?: string[]`.
Both TS (`sdk/src/query/phase.ts`) and CJS twin (`get-shit-done/bin/lib/phase.cjs`)
fixed identically.

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

* chore: add changeset for #3276

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

* fix(phase): resolve depends_on against canonical plan id (#3276 CR)

Build a secondary `canonicalToId` index alongside `planMap` so that a
dependency declared as '03-01' resolves to a descriptive plan stored
under '03-01-auth-hardening', preventing silent wave-ordering failures.
Applied at both DAG construction sites in phase.cjs and the SDK's
phase.ts (k014 parity). Regression test added.

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-09 00:25:05 -04:00
committed by GitHub
parent 8bc255c266
commit 8d5f509edf
7 changed files with 576 additions and 79 deletions

View File

@@ -288,10 +288,20 @@ export const phasePlanIndex: QueryHandler = async (args, projectDir, workstream)
})
);
const plans: Array<Record<string, unknown>> = [];
const waves: Record<string, string[]> = {};
const incomplete: string[] = [];
let hasCheckpoints = false;
// ── Pass 1: parse each plan file ─────────────────────────────────────────
interface RawPlan {
id: string;
declaredWave: number | null;
dependsOn: string[];
autonomous: boolean;
objective: string | null;
filesModified: string[];
taskCount: number;
hasSummary: boolean;
}
const rawPlans: RawPlan[] = [];
for (const planFile of planFiles) {
// For named plans (01-01-PLAN.md): strip suffix to get '01-01'
@@ -306,8 +316,20 @@ export const phasePlanIndex: QueryHandler = async (args, projectDir, workstream)
const mdTasks = content.match(/##\s*Task\s*\d+/gi) || [];
const taskCount = xmlTasks.length || mdTasks.length;
// Parse wave as integer
const wave = parseInt(String(fm.wave), 10) || 1;
// Parse wave as integer — use nullish handling so wave: 0 is preserved.
// parseInt returns NaN for missing/non-numeric values; fall back to null
// (meaning "no declared wave") so downstream can apply the topo default.
const parsedWave = parseInt(String(fm.wave), 10);
const declaredWave = Number.isNaN(parsedWave) ? null : parsedWave;
// Parse depends_on — normalise to string[]
let dependsOn: string[] = [];
const fmDeps = fm['depends_on'] as string | string[] | undefined;
if (Array.isArray(fmDeps)) {
dependsOn = fmDeps.map(String);
} else if (typeof fmDeps === 'string' && fmDeps.trim() !== '') {
dependsOn = [fmDeps];
}
// Parse autonomous (default true if not specified)
let autonomous = true;
@@ -315,10 +337,6 @@ export const phasePlanIndex: QueryHandler = async (args, projectDir, workstream)
autonomous = fm.autonomous === 'true' || fm.autonomous === true;
}
if (!autonomous) {
hasCheckpoints = true;
}
// Parse files_modified
let filesModified: string[] = [];
const fmFiles = (fm['files_modified'] || fm['files-modified']) as string | string[] | undefined;
@@ -327,37 +345,144 @@ export const phasePlanIndex: QueryHandler = async (args, projectDir, workstream)
}
const hasSummary = completedPlanIds.has(planId) || completedPlanIds.has(extractCanonicalPlanId(planFile));
if (!hasSummary) {
incomplete.push(planId);
}
const plan = {
rawPlans.push({
id: planId,
wave,
declaredWave,
dependsOn,
autonomous,
objective: extractObjective(content) || (fm.objective as string) || null,
files_modified: filesModified,
task_count: taskCount,
has_summary: hasSummary,
filesModified,
taskCount,
hasSummary,
});
}
// ── Pass 2: topological level assignment via depends_on DAG ──────────────
// Build a map from plan ID → RawPlan for fast lookup.
// Deps that reference plans outside this phase are silently ignored (treated
// as already-satisfied external deps — the plan becomes a source node).
const planMap = new Map<string, RawPlan>(rawPlans.map(p => [p.id, 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<string, string>(rawPlans.map(p => [extractCanonicalPlanId(p.id), p.id]));
// Kahn's algorithm — compute in-degree and adjacency for plans in this phase only.
const level = new Map<string, number>();
const inDeg = new Map<string, number>();
const adj = new Map<string, string[]>(); // dep → [dependents]
for (const p of rawPlans) {
if (!inDeg.has(p.id)) inDeg.set(p.id, 0);
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);
if (!resolvedDep) continue; // external dep — ignore
if (!adj.has(resolvedDep)) adj.set(resolvedDep, []);
adj.get(resolvedDep)!.push(p.id);
inDeg.set(p.id, (inDeg.get(p.id) ?? 0) + 1);
}
}
// Start with nodes that have no in-phase dependencies.
const queue: string[] = [];
for (const p of rawPlans) {
if ((inDeg.get(p.id) ?? 0) === 0) {
queue.push(p.id);
level.set(p.id, 0);
}
}
let visited = 0;
while (queue.length > 0) {
const cur = queue.shift()!;
visited++;
const curLevel = level.get(cur)!;
for (const dep of (adj.get(cur) ?? [])) {
const newLevel = curLevel + 1;
if (newLevel > (level.get(dep) ?? -1)) {
level.set(dep, newLevel);
}
inDeg.set(dep, inDeg.get(dep)! - 1);
if (inDeg.get(dep) === 0) {
queue.push(dep);
}
}
}
// Cycle detection — any node not visited has a cycle.
if (visited < rawPlans.length) {
const cycleNodes = rawPlans.filter(p => !level.has(p.id)).map(p => p.id);
throw new GSDError(
`depends_on cycle detected in phase ${normalized} — cycle involves: ${cycleNodes.join(', ')}`,
ErrorClassification.Execution,
);
}
// ── Pass 3: determine lowest bucket key and build output ─────────────────
// If any plan has declared wave: 0, the lowest level maps to "0"; otherwise "1".
const anyWaveZero = rawPlans.some(p => p.declaredWave === 0);
const levelOffset = anyWaveZero ? 0 : 1;
const plans: Array<Record<string, unknown>> = [];
const waves: Record<string, string[]> = {};
const incomplete: string[] = [];
let hasCheckpoints = false;
const warnings: string[] = [];
for (const raw of rawPlans) {
if (!raw.autonomous) {
hasCheckpoints = true;
}
if (!raw.hasSummary) {
incomplete.push(raw.id);
}
// Computed wave = topological level + offset (so lowest level → 0 or 1).
const computedWave = (level.get(raw.id) ?? 0) + levelOffset;
// The effective wave used for bucketing is always the computed topo level.
// If the plan declared a wave that disagrees, emit a non-fatal warning.
const effectiveWave = computedWave;
if (raw.declaredWave !== null && raw.declaredWave !== computedWave) {
warnings.push(
`Plan ${raw.id}: declared wave: ${raw.declaredWave} but depends_on DAG places it in wave ${computedWave}`,
);
}
const plan: Record<string, unknown> = {
id: raw.id,
wave: effectiveWave,
depends_on: raw.dependsOn,
autonomous: raw.autonomous,
objective: raw.objective,
files_modified: raw.filesModified,
task_count: raw.taskCount,
has_summary: raw.hasSummary,
};
plans.push(plan);
// Group by wave
const waveKey = String(wave);
const waveKey = String(effectiveWave);
if (!waves[waveKey]) {
waves[waveKey] = [];
}
waves[waveKey].push(planId);
waves[waveKey].push(raw.id);
}
return {
data: {
phase: normalized,
plans,
waves,
incomplete,
has_checkpoints: hasCheckpoints,
},
const result: Record<string, unknown> = {
phase: normalized,
plans,
waves,
incomplete,
has_checkpoints: hasCheckpoints,
};
if (warnings.length > 0) {
result['warnings'] = warnings;
}
return { data: result };
};