@procedures/dag 0.47.1 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/CHANGELOG.md +25 -1
  2. package/README.md +2 -0
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/graph/index.d.ts +3 -3
  5. package/dist/graph/index.d.ts.map +1 -1
  6. package/dist/graph/index.js +3 -3
  7. package/dist/graph/index.js.map +1 -1
  8. package/dist/index.d.ts +3 -3
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +2 -2
  11. package/dist/index.js.map +1 -1
  12. package/dist/procedures/traverse.js +1 -1
  13. package/dist/procedures/traverse.js.map +1 -1
  14. package/dist/procedures/types.d.ts +1 -1
  15. package/dist/procedures/types.d.ts.map +1 -1
  16. package/package.json +4 -3
  17. package/src/graph/index.ts +3 -3
  18. package/src/index.ts +3 -3
  19. package/src/procedures/traverse.ts +1 -1
  20. package/src/procedures/types.ts +1 -1
  21. package/dist/dag/builder.d.ts +0 -25
  22. package/dist/dag/builder.d.ts.map +0 -1
  23. package/dist/dag/builder.js +0 -95
  24. package/dist/dag/builder.js.map +0 -1
  25. package/dist/dag/executor.d.ts +0 -28
  26. package/dist/dag/executor.d.ts.map +0 -1
  27. package/dist/dag/executor.js +0 -147
  28. package/dist/dag/executor.js.map +0 -1
  29. package/dist/dag/index.d.ts +0 -7
  30. package/dist/dag/index.d.ts.map +0 -1
  31. package/dist/dag/index.js +0 -7
  32. package/dist/dag/index.js.map +0 -1
  33. package/dist/dag/traversal.d.ts +0 -28
  34. package/dist/dag/traversal.d.ts.map +0 -1
  35. package/dist/dag/traversal.js +0 -103
  36. package/dist/dag/traversal.js.map +0 -1
  37. package/dist/graph/hub-order.d.ts +0 -19
  38. package/dist/graph/hub-order.d.ts.map +0 -1
  39. package/dist/graph/hub-order.js +0 -52
  40. package/dist/graph/hub-order.js.map +0 -1
  41. package/dist/types.d.ts +0 -80
  42. package/dist/types.d.ts.map +0 -1
  43. package/dist/types.js +0 -8
  44. package/dist/types.js.map +0 -1
  45. package/src/dag/builder.ts +0 -124
  46. package/src/dag/executor.ts +0 -190
  47. package/src/dag/index.ts +0 -23
  48. package/src/dag/traversal.ts +0 -129
  49. package/src/graph/hub-order.ts +0 -53
  50. package/src/types.ts +0 -87
@@ -1,103 +0,0 @@
1
- /**
2
- * DAG Traversal using Kahn's Algorithm
3
- *
4
- * Implements topological sort with level assignment for parallel execution.
5
- * Level 0 = leaves (no dependencies), higher levels depend on lower levels.
6
- */
7
- /**
8
- * Build a leveled DAG from nodes using Kahn's algorithm
9
- *
10
- * This assigns levels to each node where:
11
- * - Level 0: Nodes with no dependencies (leaves)
12
- * - Level N: Nodes whose dependencies are all at level < N
13
- *
14
- * Nodes at the same level can be processed in parallel.
15
- *
16
- * @throws Error if a cycle is detected
17
- */
18
- export function buildLeveledDAG(nodes) {
19
- const inDegree = new Map();
20
- const dependents = new Map();
21
- for (const [name, node] of nodes) {
22
- // A set: a dependency that a node lists twice counts once. (`dependents` is a set, so it
23
- // takes the in-degree down once: a repeated dependency was "Circular": deep dive DATA-21.)
24
- const depsInDag = new Set(node.dependencies.filter((dep) => nodes.has(dep)));
25
- inDegree.set(name, depsInDag.size);
26
- dependents.set(name, new Set());
27
- }
28
- for (const [name, node] of nodes) {
29
- for (const dep of node.dependencies) {
30
- if (dependents.has(dep)) {
31
- dependents.get(dep).add(name);
32
- }
33
- }
34
- }
35
- let queue = [];
36
- for (const [name, degree] of inDegree) {
37
- if (degree === 0) {
38
- queue.push(name);
39
- }
40
- }
41
- const levels = [];
42
- const nodeLevel = new Map();
43
- let processedCount = 0;
44
- while (queue.length > 0) {
45
- const currentLevel = levels.length;
46
- const levelNodes = [];
47
- const nextQueue = [];
48
- for (const name of queue) {
49
- const node = nodes.get(name);
50
- node.level = currentLevel;
51
- nodeLevel.set(name, currentLevel);
52
- levelNodes.push(node);
53
- processedCount++;
54
- for (const dependent of dependents.get(name) ?? []) {
55
- const newDegree = inDegree.get(dependent) - 1;
56
- inDegree.set(dependent, newDegree);
57
- if (newDegree === 0) {
58
- nextQueue.push(dependent);
59
- }
60
- }
61
- }
62
- levels.push(levelNodes);
63
- queue = nextQueue;
64
- }
65
- if (processedCount !== nodes.size) {
66
- const cycleNodes = [...nodes.keys()].filter((n) => !nodeLevel.has(n));
67
- throw new Error("Circular dependency detected involving: " + cycleNodes.join(", "));
68
- }
69
- const roots = [...nodes.keys()].filter((name) => {
70
- const deps = dependents.get(name);
71
- return !deps || deps.size === 0;
72
- });
73
- const leaves = [...nodes.keys()].filter((name) => {
74
- const node = nodes.get(name);
75
- const depsInDag = node.dependencies.filter((dep) => nodes.has(dep));
76
- return depsInDag.length === 0;
77
- });
78
- return { nodes, levels, roots, leaves };
79
- }
80
- /**
81
- * Get the topological order (flat list, leaves first)
82
- */
83
- export function getTopologicalOrder(dag) {
84
- return dag.levels.flat();
85
- }
86
- /**
87
- * Visualize the DAG structure (for debugging)
88
- */
89
- export function visualizeDAG(dag) {
90
- const lines = ["DAG Structure:"];
91
- for (let i = 0; i < dag.levels.length; i++) {
92
- const level = dag.levels[i];
93
- lines.push(" Level " + i + ":");
94
- for (const node of level) {
95
- const deps = node.dependencies.length > 0
96
- ? " -> [" + node.dependencies.join(", ") + "]"
97
- : " (leaf)";
98
- lines.push(" - " + node.id + deps);
99
- }
100
- }
101
- return lines.join("\n");
102
- }
103
- //# sourceMappingURL=traversal.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"traversal.js","sourceRoot":"","sources":["../../src/dag/traversal.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAyB;IAEzB,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC3C,MAAM,UAAU,GAAG,IAAI,GAAG,EAAuB,CAAC;IAElD,KAAK,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,E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AAC,SAAS,CAAC;YAChB,KAAK,CAAC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC;QACxC,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
@@ -1,19 +0,0 @@
1
- /** An undirected edge between the nodes `a` and `b`. The default weight is 1. */
2
- export interface WeightedEdge {
3
- readonly a: number;
4
- readonly b: number;
5
- readonly weight?: number | undefined;
6
- }
7
- /**
8
- * This function gives a breadth-first order of the nodes `0` to `n - 1` of an undirected weighted graph.
9
- *
10
- * - The hub of a component is the node with the largest sum of edge weights. `compare`, then the lower index, breaks a tie.
11
- * - The order starts at the hub of each component, and goes breadth-first. `compare`, then the lower index, orders the neighbours of a node.
12
- * - Without edges, each node is a component, so the order is the order of `compare`.
13
- * - An edge from a node to itself adds no weight.
14
- * - A bad `n`, an index outside the nodes, or a weight that is not a finite number above 0, throws a `RangeError`.
15
- *
16
- * The function is pure. It keeps no state, and it does not change its arguments.
17
- */
18
- export declare function hubBreadthFirstOrder(n: number, edges?: readonly WeightedEdge[], compare?: (i: number, j: number) => number): number[];
19
- //# sourceMappingURL=hub-order.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hub-order.d.ts","sourceRoot":"","sources":["../../src/graph/hub-order.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACtC;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,GAAE,SAAS,YAAY,EAAO,EAAE,OAAO,GAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,KAAK,MAAgB,GAAG,MAAM,EAAE,CAkClJ"}
@@ -1,52 +0,0 @@
1
- /**
2
- * This function gives a breadth-first order of the nodes `0` to `n - 1` of an undirected weighted graph.
3
- *
4
- * - The hub of a component is the node with the largest sum of edge weights. `compare`, then the lower index, breaks a tie.
5
- * - The order starts at the hub of each component, and goes breadth-first. `compare`, then the lower index, orders the neighbours of a node.
6
- * - Without edges, each node is a component, so the order is the order of `compare`.
7
- * - An edge from a node to itself adds no weight.
8
- * - A bad `n`, an index outside the nodes, or a weight that is not a finite number above 0, throws a `RangeError`.
9
- *
10
- * The function is pure. It keeps no state, and it does not change its arguments.
11
- */
12
- export function hubBreadthFirstOrder(n, edges = [], compare = () => 0) {
13
- if (!Number.isInteger(n) || n < 0)
14
- throw new RangeError(`The count of the nodes is a whole number from 0, not ${n}.`);
15
- const degree = new Float64Array(n);
16
- const next = Array.from({ length: n }, () => []);
17
- for (const { a, b, weight = 1 } of edges) {
18
- if (!Number.isInteger(a) || !Number.isInteger(b) || a < 0 || b < 0 || a >= n || b >= n) {
19
- throw new RangeError(`An edge joins the nodes ${a} and ${b}, but the indexes go from 0 to ${n - 1}.`);
20
- }
21
- if (!Number.isFinite(weight) || weight <= 0)
22
- throw new RangeError(`The weight of an edge is a finite number above 0, not ${weight}.`);
23
- if (a === b)
24
- continue;
25
- degree[a] = degree[a] + weight;
26
- degree[b] = degree[b] + weight;
27
- next[a].push(b);
28
- next[b].push(a);
29
- }
30
- const order = (i, j) => compare(i, j) || i - j;
31
- const hubs = Array.from({ length: n }, (_, i) => i).sort((i, j) => degree[j] - degree[i] || order(i, j));
32
- const seen = new Uint8Array(n);
33
- const out = [];
34
- for (const hub of hubs) {
35
- if (seen[hub] === 1)
36
- continue;
37
- seen[hub] = 1;
38
- const queue = [hub];
39
- for (let k = 0; k < queue.length; k++) {
40
- const i = queue[k];
41
- out.push(i);
42
- for (const j of next[i].toSorted(order)) {
43
- if (seen[j] === 1)
44
- continue;
45
- seen[j] = 1;
46
- queue.push(j);
47
- }
48
- }
49
- }
50
- return out;
51
- }
52
- //# sourceMappingURL=hub-order.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"hub-order.js","sourceRoot":"","sources":["../../src/graph/hub-order.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAAC,CAAS,EAAE,KAAK,GAA4B,EAAE,EAAE,OAAO,GAAqC,GAAG,EAAE,CAAC,CAAC;IACtI,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,MAAM,IAAI,UAAU,CAAC,wDAAwD,CAAC,GAAG,CAAC,CAAC;IACtH,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,CAAC,CAAC,CAAC;IACnC,MAAM,IAAI,GAAe,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;IAC7D,KAAK,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,IAAI,KAAK,EAAE,CAAC;QACzC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACvF,MAAM,IAAI,UAAU,CAAC,2BAA2B,CAAC,QAAQ,CAAC,kCAAkC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACxG,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC;YAAE,MAAM,IAAI,UAAU,CAAC,yDAAyD,MAAM,GAAG,CAAC,CAAC;QACtI,IAAI,CAAC,KAAK,CAAC;YAAE,SAAS;QACtB,MAAM,CAAC,CAAC,C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,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,CAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;gBACzC,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;oBAAE,SAAS;gBAC5B,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;gBACZ,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAChB,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
package/dist/types.d.ts DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * Generic DAG Types
3
- *
4
- * These types are designed to be reusable across different use cases.
5
- * For ecosystem-specific DAG building, see client-lib.
6
- */
7
- /**
8
- * A node in a dependency DAG
9
- */
10
- export interface DAGNode<TData = unknown> {
11
- /** Unique identifier for this node */
12
- id: string;
13
- /** Names/IDs of nodes this node depends on */
14
- dependencies: string[];
15
- /** Topological level (0 = leaves, computed by Kahn's algorithm) */
16
- level?: number;
17
- /** Optional user data associated with this node */
18
- data?: TData | undefined;
19
- }
20
- /**
21
- * A dependency DAG with level information for parallel execution
22
- */
23
- export interface DependencyDAG<TNode extends DAGNode = DAGNode> {
24
- /** All nodes in the DAG */
25
- nodes: Map<string, TNode>;
26
- /** Nodes grouped by level for parallel execution */
27
- levels: TNode[][];
28
- /** Root nodes (no dependents in the DAG) */
29
- roots: string[];
30
- /** Leaf nodes (no dependencies in the DAG) */
31
- leaves: string[];
32
- }
33
- /**
34
- * Options for DAG execution
35
- */
36
- export interface DAGExecutionOptions<TNode extends DAGNode = DAGNode> {
37
- /** Max parallel operations per level (default: 4) */
38
- concurrency?: number;
39
- /** Stop on first error vs continue (default: true) */
40
- failFast?: boolean;
41
- /** Callback for progress reporting */
42
- onNodeStart?: (node: TNode) => void;
43
- /** Callback when a node completes */
44
- onNodeComplete?: (result: NodeResult<TNode>) => void;
45
- }
46
- /**
47
- * Result of processing a single node
48
- */
49
- export interface NodeResult<TNode extends DAGNode = DAGNode> {
50
- /** The node that was processed */
51
- node: TNode;
52
- /** Whether processing succeeded */
53
- success: boolean;
54
- /** Error if failed */
55
- error?: Error | undefined;
56
- /** Duration in milliseconds */
57
- duration: number;
58
- /** Logs from processing */
59
- logs: string[];
60
- /** Optional output from the processor */
61
- output?: unknown | undefined;
62
- }
63
- /**
64
- * Result of executing an entire DAG
65
- */
66
- export interface DAGResult<TNode extends DAGNode = DAGNode> {
67
- /** Overall success */
68
- success: boolean;
69
- /** Results for each node */
70
- results: Map<string, NodeResult<TNode>>;
71
- /** Names of failed nodes */
72
- failedNodes: string[];
73
- /** Total duration in milliseconds */
74
- totalDuration: number;
75
- }
76
- /**
77
- * Processor function type
78
- */
79
- export type NodeProcessor<TNode extends DAGNode = DAGNode> = (node: TNode) => Promise<NodeResult<TNode>>;
80
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,WAAW,OAAO,CAAC,KAAK,GAAG,OAAO;IACtC,sCAAsC;IACtC,EAAE,EAAE,MAAM,CAAC;IACX,8CAA8C;IAC9C,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,mDAAmD;IACnD,IAAI,CAAC,EAAE,KAAK,GAAG,SAAS,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,KAAK,SAAS,OAAO,GAAG,OAAO;IAC5D,2BAA2B;IAC3B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC1B,oDAAoD;IACpD,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;IAClB,4CAA4C;IAC5C,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,8CAA8C;IAC9C,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB;AAED;;GAE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1,8 +0,0 @@
1
- /**
2
- * Generic DAG Types
3
- *
4
- * These types are designed to be reusable across different use cases.
5
- * For ecosystem-specific DAG building, see client-lib.
6
- */
7
- export {};
8
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
@@ -1,124 +0,0 @@
1
- /**
2
- * DAG Builder utilities
3
- */
4
-
5
- import type { DAGNode } from "../types.js";
6
-
7
- /**
8
- * Filter DAG to only include nodes reachable from a root
9
- */
10
- export function filterDAGFromRoot<TNode extends DAGNode>(
11
- nodes: Map<string, TNode>,
12
- rootId: string
13
- ): Map<string, TNode> {
14
- const filtered = new Map<string, TNode>();
15
- const visited = new Set<string>();
16
-
17
- function visit(id: string): void {
18
- if (visited.has(id)) return;
19
- visited.add(id);
20
-
21
- const node = nodes.get(id);
22
- if (!node) return;
23
-
24
- filtered.set(id, node);
25
-
26
- for (const dep of node.dependencies) {
27
- visit(dep);
28
- }
29
- }
30
-
31
- visit(rootId);
32
- return filtered;
33
- }
34
-
35
- /**
36
- * Get all ancestors of a node (nodes that this node depends on, transitively)
37
- */
38
- export function getAncestors<TNode extends DAGNode>(
39
- nodes: Map<string, TNode>,
40
- id: string
41
- ): Set<string> {
42
- const ancestors = new Set<string>();
43
- const visited = new Set<string>();
44
-
45
- function visit(n: string): void {
46
- if (visited.has(n)) return;
47
- visited.add(n);
48
-
49
- const node = nodes.get(n);
50
- if (!node) return;
51
-
52
- for (const dep of node.dependencies) {
53
- if (nodes.has(dep)) {
54
- ancestors.add(dep);
55
- visit(dep);
56
- }
57
- }
58
- }
59
-
60
- visit(id);
61
- return ancestors;
62
- }
63
-
64
- /**
65
- * Get all descendants of a node (nodes that depend on this node, transitively)
66
- */
67
- export function getDescendants<TNode extends DAGNode>(
68
- nodes: Map<string, TNode>,
69
- id: string
70
- ): Set<string> {
71
- const descendants = new Set<string>();
72
- const visited = new Set<string>();
73
-
74
- // Build reverse dependency map
75
- const dependents = new Map<string, Set<string>>();
76
- for (const [nodeId, node] of nodes) {
77
- for (const dep of node.dependencies) {
78
- if (!dependents.has(dep)) {
79
- dependents.set(dep, new Set());
80
- }
81
- dependents.get(dep)!.add(nodeId);
82
- }
83
- }
84
-
85
- function visit(n: string): void {
86
- if (visited.has(n)) return;
87
- visited.add(n);
88
-
89
- const deps = dependents.get(n);
90
- if (!deps) return;
91
-
92
- for (const dep of deps) {
93
- descendants.add(dep);
94
- visit(dep);
95
- }
96
- }
97
-
98
- visit(id);
99
- return descendants;
100
- }
101
-
102
- /**
103
- * Create a DAG node from minimal input
104
- */
105
- export function createNode<TData = unknown>(
106
- id: string,
107
- dependencies: string[] = [],
108
- data?: TData | undefined
109
- ): DAGNode<TData> {
110
- const node: DAGNode<TData> = { id, dependencies };
111
- if (data !== undefined) {
112
- node.data = data;
113
- }
114
- return node;
115
- }
116
-
117
- /**
118
- * Build a Map of nodes from an array
119
- */
120
- export function buildNodeMap<TNode extends DAGNode>(
121
- nodes: TNode[]
122
- ): Map<string, TNode> {
123
- return new Map(nodes.map((n) => [n.id, n]));
124
- }
@@ -1,190 +0,0 @@
1
- /**
2
- * DAG Executor with parallel level-based execution
3
- */
4
-
5
- import type {
6
- DependencyDAG,
7
- DAGNode,
8
- DAGExecutionOptions,
9
- NodeResult,
10
- DAGResult,
11
- } from "../types.js";
12
-
13
- /**
14
- * Execute a batch of items with concurrency limit
15
- */
16
- async function executeWithConcurrency<T, R>(
17
- items: T[],
18
- fn: (item: T) => Promise<R>,
19
- limit: number
20
- ): Promise<R[]> {
21
- const results: R[] = [];
22
- const executing = new Set<Promise<void>>();
23
-
24
- for (const item of items) {
25
- const promise = fn(item)
26
- .then((result) => {
27
- results.push(result);
28
- })
29
- .finally(() => {
30
- // Rejection-safe cleanup: always remove the settled promise from the
31
- // executing set, even if `fn` rejected, so the set never leaks a
32
- // settled (possibly rejected) promise into later `race`/`all` waits.
33
- executing.delete(promise);
34
- });
35
-
36
- executing.add(promise);
37
-
38
- if (executing.size >= limit) {
39
- await Promise.race(executing);
40
- }
41
- }
42
-
43
- await Promise.all(executing);
44
- return results;
45
- }
46
-
47
- /**
48
- * Call a listener. A listener that throws does not stop the run: the function returns a log
49
- * line for its error (deep dive DATA-21).
50
- */
51
- function notify<T>(name: string, listener: ((arg: T) => void) | undefined, arg: T): string | undefined {
52
- if (!listener) return undefined;
53
- try {
54
- listener(arg);
55
- return undefined;
56
- } catch (error) {
57
- return name + " threw: " + (error instanceof Error ? error.message : String(error));
58
- }
59
- }
60
-
61
- /**
62
- * Execute DAG with level-based parallelization
63
- *
64
- * - Levels are processed sequentially (level 0 first, then 1, etc.)
65
- * - Within each level, nodes are processed in parallel up to concurrency limit
66
- * - Supports fail-fast or continue-on-error modes
67
- *
68
- * A processor that throws does NOT reject the whole run: the throw is captured
69
- * and recorded as a failed NodeResult, so partial results are always returned.
70
- *
71
- * `failFast` operates at level granularity. When a node in a level fails, every
72
- * node already in flight in that same level still runs to completion, but no
73
- * later level is started. (There is no mid-level cancellation.)
74
- */
75
- export async function executeDAG<TNode extends DAGNode>(
76
- dag: DependencyDAG<TNode>,
77
- processor: (node: TNode) => Promise<NodeResult<TNode>>,
78
- options: DAGExecutionOptions<TNode> = {}
79
- ): Promise<DAGResult<TNode>> {
80
- const {
81
- concurrency = 4,
82
- failFast = true,
83
- onNodeStart,
84
- onNodeComplete,
85
- } = options;
86
-
87
- const results = new Map<string, NodeResult<TNode>>();
88
- const failedNodes: string[] = [];
89
- const startTime = Date.now();
90
- let shouldStop = false;
91
-
92
- for (const level of dag.levels) {
93
- if (shouldStop) break;
94
-
95
- const levelResults = await executeWithConcurrency(
96
- level,
97
- async (node) => {
98
- const startError = notify("onNodeStart", onNodeStart, node);
99
- const nodeStart = Date.now();
100
- let result: NodeResult<TNode>;
101
- try {
102
- result = await processor(node);
103
- } catch (error) {
104
- // Convert a thrown processor error into a failed NodeResult instead
105
- // of rejecting the entire run and discarding partial results.
106
- const err =
107
- error instanceof Error ? error : new Error(String(error));
108
- result = {
109
- node,
110
- success: false,
111
- error: err,
112
- duration: Date.now() - nodeStart,
113
- logs: ["Processor threw: " + err.message],
114
- };
115
- }
116
- const completeError = notify("onNodeComplete", onNodeComplete, result);
117
- const listenerErrors = [startError, completeError].filter((line): line is string => line !== undefined);
118
- if (listenerErrors.length > 0) {
119
- result = { ...result, logs: [...(result.logs ?? []), ...listenerErrors] };
120
- }
121
- return result;
122
- },
123
- concurrency
124
- );
125
-
126
- for (const result of levelResults) {
127
- results.set(result.node.id, result);
128
- if (!result.success) {
129
- failedNodes.push(result.node.id);
130
- if (failFast) {
131
- shouldStop = true;
132
- }
133
- }
134
- }
135
- }
136
-
137
- return {
138
- success: failedNodes.length === 0,
139
- results,
140
- failedNodes,
141
- totalDuration: Date.now() - startTime,
142
- };
143
- }
144
-
145
- /**
146
- * Execute DAG sequentially (no parallelization)
147
- */
148
- export async function executeDAGSequential<TNode extends DAGNode>(
149
- dag: DependencyDAG<TNode>,
150
- processor: (node: TNode) => Promise<NodeResult<TNode>>,
151
- options: Omit<DAGExecutionOptions<TNode>, "concurrency"> = {}
152
- ): Promise<DAGResult<TNode>> {
153
- return executeDAG(dag, processor, { ...options, concurrency: 1 });
154
- }
155
-
156
- /**
157
- * Create a simple processor that wraps an async function
158
- */
159
- export function createProcessor<TNode extends DAGNode>(
160
- fn: (node: TNode) => Promise<void>
161
- ): (node: TNode) => Promise<NodeResult<TNode>> {
162
- return async (node: TNode): Promise<NodeResult<TNode>> => {
163
- const startTime = Date.now();
164
- const logs: string[] = [];
165
-
166
- try {
167
- logs.push("Starting " + node.id);
168
- await fn(node);
169
- logs.push("Completed " + node.id);
170
-
171
- return {
172
- node,
173
- success: true,
174
- duration: Date.now() - startTime,
175
- logs,
176
- };
177
- } catch (error) {
178
- const err = error instanceof Error ? error : new Error(String(error));
179
- logs.push("Failed " + node.id + ": " + err.message);
180
-
181
- return {
182
- node,
183
- success: false,
184
- error: err,
185
- duration: Date.now() - startTime,
186
- logs,
187
- };
188
- }
189
- };
190
- }
package/src/dag/index.ts DELETED
@@ -1,23 +0,0 @@
1
- /**
2
- * DAG utilities
3
- */
4
-
5
- export {
6
- buildLeveledDAG,
7
- getTopologicalOrder,
8
- visualizeDAG,
9
- } from "./traversal.js";
10
-
11
- export {
12
- executeDAG,
13
- executeDAGSequential,
14
- createProcessor,
15
- } from "./executor.js";
16
-
17
- export {
18
- filterDAGFromRoot,
19
- getAncestors,
20
- getDescendants,
21
- createNode,
22
- buildNodeMap,
23
- } from "./builder.js";