perf(#307): use head-index queue for phase dependency BFS (O(V^2) -> O(V+E)) (#383)

The Pass-2 topological level assignment in cmdPhasePlanIndex dequeued its
Kahn's-algorithm queue with Array.shift(), which is O(n) per call in V8, so the
BFS was O(V^2) and slowed superlinearly on deep queues (wide fan-in plan
graphs). Extract the traversal into a pure, exported computeDependencyLevels
(rawPlans, planMap, canonicalToId) and dequeue via a head index (queue[head++])
-> O(V+E). Behavior is identical: same FIFO order, same longest-path levels,
same visited-count cycle detection. A complexity-contract comment above the loop
documents why shift() must not be reintroduced.

Adds tests/phase-dependency-levels.test.cjs with deterministic behavior and
edge-case coverage (linear chain, diamond longest-path, independent set, cycle,
canonical-prefix resolution, empty, self-loop, duplicate edge, external dep). A
timing-based complexity guard was intentionally omitted: the O(V+E) Map-build
constant dilutes the O(V^2) signal until impractical N (~1e6), so an empirical
guard is inherently flaky on contended CI — the contract is enforced by the
inline comment and correctness tests instead.

Fixes #307

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-27 20:21:12 -04:00
committed by GitHub
parent d17bb4ea1e
commit 51aac62e9a
3 changed files with 219 additions and 45 deletions

View File

@@ -353,6 +353,64 @@ function extractObjective(content) {
return m ? m[1].trim() : null;
}
// O(V + E). Assigns each in-phase plan its longest-path topological level over the
// in-phase dependsOn DAG (Kahn's algorithm). Returns { level: Map<id,number>, visited: number }.
// visited < rawPlans.length signals a dependency cycle.
function computeDependencyLevels(rawPlans, planMap, canonicalToId) {
// Kahn's algorithm — compute in-degree and adjacency for in-phase deps only.
const level = new Map();
const inDeg = new Map();
const adj = new Map();
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.
// 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);
inDeg.set(p.id, (inDeg.get(p.id) ?? 0) + 1);
}
}
// Start with nodes that have no in-phase dependencies.
const queue = [];
for (const p of rawPlans) {
if ((inDeg.get(p.id) ?? 0) === 0) {
queue.push(p.id);
level.set(p.id, 0);
}
}
// Dequeue by head index (queue[head++]), NOT Array.shift(): shift() is O(n) per
// call in V8 (it re-indexes the backing store), which would make this Kahn's BFS
// O(V^2) on deep queues (e.g. wide fan-in graphs). Head-index dequeue is O(1)
// amortized -> O(V+E) overall. Do not "simplify" this back to queue.shift(). (#307)
let head = 0;
let visited = 0;
while (head < queue.length) {
const cur = queue[head++];
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);
}
}
}
return { level, visited };
}
function cmdPhasePlanIndex(cwd, phase, raw) {
if (!phase) {
error('phase required for phase-plan-index');
@@ -496,51 +554,7 @@ function cmdPhasePlanIndex(cwd, phase, raw) {
// 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();
const inDeg = new Map();
const adj = new Map();
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.
// 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);
inDeg.set(p.id, (inDeg.get(p.id) ?? 0) + 1);
}
}
// Start with nodes that have no in-phase dependencies.
const queue = [];
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);
}
}
}
const { level, visited } = computeDependencyLevels(rawPlans, planMap, canonicalToId);
// Cycle detection — any node not visited has a cycle.
if (visited < rawPlans.length) {
@@ -1555,4 +1569,5 @@ module.exports = {
cmdPhaseInsert,
cmdPhaseRemove,
cmdPhaseComplete,
computeDependencyLevels,
};