From 9bd6eacaa9ebe860842b5d5cfbf2b7d2d0293ab1 Mon Sep 17 00:00:00 2001
From: soryu <soryu@soryu.co>
Date: Wed, 11 Feb 2026 00:33:36 +0000
Subject: Fix DAG ordering

---
 .../src/components/directives/DirectiveDAG.tsx     | 40 ++++++----------------
 1 file changed, 11 insertions(+), 29 deletions(-)

(limited to 'makima/frontend/src/components/directives/DirectiveDAG.tsx')

diff --git a/makima/frontend/src/components/directives/DirectiveDAG.tsx b/makima/frontend/src/components/directives/DirectiveDAG.tsx
index f288a0d..27a80ac 100644
--- a/makima/frontend/src/components/directives/DirectiveDAG.tsx
+++ b/makima/frontend/src/components/directives/DirectiveDAG.tsx
@@ -16,37 +16,19 @@ interface Layer {
 function topoSort(steps: DirectiveStep[]): Layer[] {
   if (steps.length === 0) return [];
 
-  const stepMap = new Map(steps.map((s) => [s.id, s]));
-  const assigned = new Set<string>();
-  const layers: Layer[] = [];
-
-  // Iteratively find steps whose dependencies are all assigned
-  let remaining = [...steps];
-  while (remaining.length > 0) {
-    const layer: DirectiveStep[] = [];
-    for (const step of remaining) {
-      const depsResolved = step.dependsOn.every(
-        (depId) => assigned.has(depId) || !stepMap.has(depId)
-      );
-      if (depsResolved) {
-        layer.push(step);
-      }
-    }
-
-    if (layer.length === 0) {
-      // Cycle detected or orphaned — push all remaining
-      layers.push({ steps: remaining });
-      break;
-    }
-
-    for (const s of layer) {
-      assigned.add(s.id);
-    }
-    layers.push({ steps: layer.sort((a, b) => a.orderIndex - b.orderIndex) });
-    remaining = remaining.filter((s) => !assigned.has(s.id));
+  // Group steps by orderIndex — each unique orderIndex is one execution phase
+  const byOrder = new Map<number, DirectiveStep[]>();
+  for (const step of steps) {
+    const group = byOrder.get(step.orderIndex) ?? [];
+    group.push(step);
+    byOrder.set(step.orderIndex, group);
   }
 
-  return layers;
+  // Sort groups by ascending orderIndex
+  const sortedKeys = [...byOrder.keys()].sort((a, b) => a - b);
+  return sortedKeys.map((key) => ({
+    steps: byOrder.get(key)!.sort((a, b) => a.name.localeCompare(b.name)),
+  }));
 }
 
 export function DirectiveDAG({ steps, onComplete, onFail, onSkip }: DirectiveDAGProps) {
-- 
cgit v1.2.3

