@mark1russell7/collections 0.9.4 → 0.9.5
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.
- package/README.md +4 -0
- package/build/dag/index.d.ts +10 -10
- package/build/dag/index.js +12 -12
- package/build/dag/index.js.map +1 -1
- package/build/index.d.ts +80 -77
- package/build/index.d.ts.map +1 -1
- package/build/index.js +49 -41
- package/build/index.js.map +1 -1
- package/package.json +3 -1
- package/src/async/async-queue.ts +24 -10
- package/src/async/channels.ts +3 -3
- package/src/behaviors/bounded.ts +1 -1
- package/src/behaviors/evented.ts +1 -1
- package/src/behaviors/readonly.ts +1 -1
- package/src/behaviors/safe.ts +16 -16
- package/src/behaviors/synchronized.ts +1 -1
- package/src/behaviors/ttl.ts +6 -6
- package/src/core/events.ts +14 -14
- package/src/core/middleware.ts +6 -6
- package/src/core/policies.ts +3 -3
- package/src/dag/builder.ts +5 -5
- package/src/dag/executor.ts +5 -5
- package/src/dag/traversal.ts +2 -2
- package/src/fx/collectors.ts +1 -1
- package/src/impl/hash-map.ts +2 -2
- package/src/impl/hash-set.ts +1 -1
- package/src/impl/linked-hash-map.ts +1 -1
- package/src/interfaces/collection.ts +2 -2
- package/src/interfaces/list.ts +4 -4
- package/src/interfaces/map.ts +3 -3
- package/src/interfaces/queue.ts +10 -10
- package/src/interfaces/set.ts +3 -3
- package/src/utils/defaults.ts +2 -2
- package/src/utils/helpers.ts +4 -4
package/README.md
CHANGED
|
@@ -103,12 +103,16 @@ Each guide has examples, and a test runs each example:
|
|
|
103
103
|
- [Lists](https://github.com/mark1russell7/collections/blob/main/docs/guides/lists.md): `arrayList` and `linkedList`
|
|
104
104
|
- [Maps and sets](https://github.com/mark1russell7/collections/blob/main/docs/guides/maps-and-sets.md): hash maps, tree maps, linked maps, and their sets
|
|
105
105
|
- [Queues](https://github.com/mark1russell7/collections/blob/main/docs/guides/queues.md): `arrayDeque` and `priorityQueue`
|
|
106
|
+
- [Equality, hashes and order](https://github.com/mark1russell7/collections/blob/main/docs/guides/equality-and-order.md): `deepEq`, `defaultHash`, the comparators and their rules
|
|
107
|
+
- [Async queues and tasks](https://github.com/mark1russell7/collections/blob/main/docs/guides/async.md): `asyncQueue`, `semaphore` and `asSettled`
|
|
106
108
|
- [Dependency graphs](https://github.com/mark1russell7/collections/blob/main/docs/guides/dag.md): levels, the topological order, and a run with `executeDAG`
|
|
107
109
|
- [Graph orders](https://github.com/mark1russell7/collections/blob/main/docs/guides/graph.md): `hubBreadthFirstOrder`
|
|
108
110
|
- [Layers](https://github.com/mark1russell7/collections/blob/main/docs/guides/layers.md): `compose`, its types and its laws, and behaviors on a collection
|
|
109
111
|
- [Behaviors](https://github.com/mark1russell7/collections/blob/main/docs/guides/behaviors.md): readonly views, safe reads and the locks
|
|
112
|
+
- [Events and caches](https://github.com/mark1russell7/collections/blob/main/docs/guides/caches.md): `eventedMap`, `eventedList`, `lruMap`, `ttlMap` and their stacks
|
|
110
113
|
- [Iterables and collectors](https://github.com/mark1russell7/collections/blob/main/docs/guides/iterables.md): lazy functions, reducers, collectors and the names of the root
|
|
111
114
|
- [Rank keys](https://github.com/mark1russell7/collections/blob/main/docs/guides/rank-keys.md): the keys of a list that a user can reorder, and the writes of a move
|
|
115
|
+
- [Summary trees](https://github.com/mark1russell7/collections/blob/main/docs/guides/summary-tree.md): the spec, a search by a summary, positions in a text, the edits and `merge`
|
|
112
116
|
|
|
113
117
|
## Contract checks
|
|
114
118
|
|
package/build/dag/index.d.ts
CHANGED
|
@@ -90,7 +90,7 @@ interface DAGResult<TNode extends DAGNode = DAGNode> {
|
|
|
90
90
|
*/
|
|
91
91
|
type NodeProcessor<TNode extends DAGNode = DAGNode> = (node: TNode) => Promise<NodeResult<TNode>>;
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* This function builds a leveled DAG from nodes, with Kahn's algorithm.
|
|
94
94
|
*
|
|
95
95
|
* This assigns levels to each node where:
|
|
96
96
|
* - Level 0: Nodes with no dependencies (leaves)
|
|
@@ -102,7 +102,7 @@ type NodeProcessor<TNode extends DAGNode = DAGNode> = (node: TNode) => Promise<N
|
|
|
102
102
|
*/
|
|
103
103
|
export declare function buildLeveledDAG<TNode extends DAGNode>(nodes: Map<string, TNode>): DependencyDAG<TNode>;
|
|
104
104
|
/**
|
|
105
|
-
*
|
|
105
|
+
* This function gives the topological order: a flat list, leaves first.
|
|
106
106
|
*/
|
|
107
107
|
export declare function getTopologicalOrder<TNode extends DAGNode>(dag: DependencyDAG<TNode>): TNode[];
|
|
108
108
|
/**
|
|
@@ -110,7 +110,7 @@ export declare function getTopologicalOrder<TNode extends DAGNode>(dag: Dependen
|
|
|
110
110
|
*/
|
|
111
111
|
export declare function visualizeDAG<TNode extends DAGNode>(dag: DependencyDAG<TNode>): string;
|
|
112
112
|
/**
|
|
113
|
-
*
|
|
113
|
+
* This function starts a DAG level by level, with the nodes of each level in parallel.
|
|
114
114
|
*
|
|
115
115
|
* - Levels are processed sequentially (level 0 first, then level 1, and so on)
|
|
116
116
|
* - Within each level, nodes are processed in parallel up to concurrency limit
|
|
@@ -130,31 +130,31 @@ export declare function visualizeDAG<TNode extends DAGNode>(dag: DependencyDAG<T
|
|
|
130
130
|
*/
|
|
131
131
|
export declare function executeDAG<TNode extends DAGNode>(dag: DependencyDAG<TNode>, processor: (node: TNode) => Promise<NodeResult<TNode>>, options?: DAGExecutionOptions<TNode>): Promise<DAGResult<TNode>>;
|
|
132
132
|
/**
|
|
133
|
-
*
|
|
133
|
+
* This function starts a DAG one node at a time, with no nodes in parallel.
|
|
134
134
|
*/
|
|
135
135
|
export declare function executeDAGSequential<TNode extends DAGNode>(dag: DependencyDAG<TNode>, processor: (node: TNode) => Promise<NodeResult<TNode>>, options?: Omit<DAGExecutionOptions<TNode>, "concurrency">): Promise<DAGResult<TNode>>;
|
|
136
136
|
/**
|
|
137
|
-
*
|
|
137
|
+
* This function makes a simple processor that wraps an async function.
|
|
138
138
|
*/
|
|
139
139
|
export declare function createProcessor<TNode extends DAGNode>(fn: (node: TNode) => Promise<void>): (node: TNode) => Promise<NodeResult<TNode>>;
|
|
140
140
|
/**
|
|
141
|
-
*
|
|
141
|
+
* This function filters a DAG to the nodes that a root can reach.
|
|
142
142
|
*/
|
|
143
143
|
export declare function filterDAGFromRoot<TNode extends DAGNode>(nodes: Map<string, TNode>, rootId: string): Map<string, TNode>;
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
145
|
+
* This function gives all ancestors of a node: the nodes that this node depends on, transitively.
|
|
146
146
|
*/
|
|
147
147
|
export declare function getAncestors<TNode extends DAGNode>(nodes: Map<string, TNode>, id: string): Set<string>;
|
|
148
148
|
/**
|
|
149
|
-
*
|
|
149
|
+
* This function gives all descendants of a node: the nodes that depend on this node, transitively.
|
|
150
150
|
*/
|
|
151
151
|
export declare function getDescendants<TNode extends DAGNode>(nodes: Map<string, TNode>, id: string): Set<string>;
|
|
152
152
|
/**
|
|
153
|
-
*
|
|
153
|
+
* This function makes a DAG node from a minimal input.
|
|
154
154
|
*/
|
|
155
155
|
export declare function createNode<TData = unknown>(id: string, dependencies?: string[], data?: TData | undefined): DAGNode<TData>;
|
|
156
156
|
/**
|
|
157
|
-
*
|
|
157
|
+
* This function builds a Map of nodes from an array.
|
|
158
158
|
*/
|
|
159
159
|
export declare function buildNodeMap<TNode extends DAGNode>(nodes: TNode[]): Map<string, TNode>;
|
|
160
160
|
export type { DAGExecutionOptions, DAGNode, DAGResult, DependencyDAG, NodeProcessor, NodeResult };
|
package/build/dag/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/dag/traversal.ts
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* This function builds a leveled DAG from nodes, with Kahn's algorithm.
|
|
4
4
|
*
|
|
5
5
|
* This assigns levels to each node where:
|
|
6
6
|
* - Level 0: Nodes with no dependencies (leaves)
|
|
@@ -60,7 +60,7 @@ function buildLeveledDAG(nodes) {
|
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
|
-
*
|
|
63
|
+
* This function gives the topological order: a flat list, leaves first.
|
|
64
64
|
*/
|
|
65
65
|
function getTopologicalOrder(dag) {
|
|
66
66
|
return dag.levels.flat();
|
|
@@ -82,7 +82,7 @@ function visualizeDAG(dag) {
|
|
|
82
82
|
}
|
|
83
83
|
// src/dag/executor.ts
|
|
84
84
|
/**
|
|
85
|
-
*
|
|
85
|
+
* This function starts a batch of items with a limit of concurrency.
|
|
86
86
|
*/
|
|
87
87
|
async function executeWithConcurrency(items, fn, limit) {
|
|
88
88
|
const results = [];
|
|
@@ -100,7 +100,7 @@ async function executeWithConcurrency(items, fn, limit) {
|
|
|
100
100
|
return results;
|
|
101
101
|
}
|
|
102
102
|
/**
|
|
103
|
-
*
|
|
103
|
+
* This function calls a listener. A listener that throws does not stop the run: the function gives a log
|
|
104
104
|
* line for its error (deep dive DATA-21).
|
|
105
105
|
*/
|
|
106
106
|
function notify(name, listener, arg) {
|
|
@@ -113,7 +113,7 @@ function notify(name, listener, arg) {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
|
-
*
|
|
116
|
+
* This function starts a DAG level by level, with the nodes of each level in parallel.
|
|
117
117
|
*
|
|
118
118
|
* - Levels are processed sequentially (level 0 first, then level 1, and so on)
|
|
119
119
|
* - Within each level, nodes are processed in parallel up to concurrency limit
|
|
@@ -201,7 +201,7 @@ async function executeDAG(dag, processor, options = {}) {
|
|
|
201
201
|
};
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
|
-
*
|
|
204
|
+
* This function starts a DAG one node at a time, with no nodes in parallel.
|
|
205
205
|
*/
|
|
206
206
|
async function executeDAGSequential(dag, processor, options = {}) {
|
|
207
207
|
return executeDAG(dag, processor, {
|
|
@@ -210,7 +210,7 @@ async function executeDAGSequential(dag, processor, options = {}) {
|
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
|
-
*
|
|
213
|
+
* This function makes a simple processor that wraps an async function.
|
|
214
214
|
*/
|
|
215
215
|
function createProcessor(fn) {
|
|
216
216
|
return async (node) => {
|
|
@@ -241,7 +241,7 @@ function createProcessor(fn) {
|
|
|
241
241
|
}
|
|
242
242
|
// src/dag/builder.ts
|
|
243
243
|
/**
|
|
244
|
-
*
|
|
244
|
+
* This function filters a DAG to the nodes that a root can reach.
|
|
245
245
|
*/
|
|
246
246
|
function filterDAGFromRoot(nodes, rootId) {
|
|
247
247
|
const filtered = /* @__PURE__ */ new Map();
|
|
@@ -258,7 +258,7 @@ function filterDAGFromRoot(nodes, rootId) {
|
|
|
258
258
|
return filtered;
|
|
259
259
|
}
|
|
260
260
|
/**
|
|
261
|
-
*
|
|
261
|
+
* This function gives all ancestors of a node: the nodes that this node depends on, transitively.
|
|
262
262
|
*/
|
|
263
263
|
function getAncestors(nodes, id) {
|
|
264
264
|
const ancestors = /* @__PURE__ */ new Set();
|
|
@@ -277,7 +277,7 @@ function getAncestors(nodes, id) {
|
|
|
277
277
|
return ancestors;
|
|
278
278
|
}
|
|
279
279
|
/**
|
|
280
|
-
*
|
|
280
|
+
* This function gives all descendants of a node: the nodes that depend on this node, transitively.
|
|
281
281
|
*/
|
|
282
282
|
function getDescendants(nodes, id) {
|
|
283
283
|
const descendants = /* @__PURE__ */ new Set();
|
|
@@ -301,7 +301,7 @@ function getDescendants(nodes, id) {
|
|
|
301
301
|
return descendants;
|
|
302
302
|
}
|
|
303
303
|
/**
|
|
304
|
-
*
|
|
304
|
+
* This function makes a DAG node from a minimal input.
|
|
305
305
|
*/
|
|
306
306
|
function createNode(id, dependencies = [], data) {
|
|
307
307
|
const node = {
|
|
@@ -312,7 +312,7 @@ function createNode(id, dependencies = [], data) {
|
|
|
312
312
|
return node;
|
|
313
313
|
}
|
|
314
314
|
/**
|
|
315
|
-
*
|
|
315
|
+
* This function builds a Map of nodes from an array.
|
|
316
316
|
*/
|
|
317
317
|
function buildNodeMap(nodes) {
|
|
318
318
|
return new Map(nodes.map((n) => [n.id, n]));
|
package/build/dag/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/dag/traversal.ts","../../src/dag/executor.ts","../../src/dag/builder.ts"],"sourcesContent":["/**\n * DAG Traversal using Kahn's Algorithm\n *\n * This module implements topological sort with level assignment for parallel execution.\n * Level 0 = leaves (no dependencies), higher levels depend on lower levels.\n */\n\nimport type { DAGNode, DependencyDAG } from \"./types.js\";\n\n/**\n * Build a leveled DAG from nodes using Kahn's algorithm\n *\n * This assigns levels to each node where:\n * - Level 0: Nodes with no dependencies (leaves)\n * - Level N: Nodes whose dependencies are all at level < N\n *\n * Nodes at the same level can be processed in parallel.\n *\n * @throws Error if a cycle is detected\n */\nexport function buildLeveledDAG<TNode extends DAGNode>(\n nodes: Map<string, TNode>\n): DependencyDAG<TNode> {\n const inDegree = new Map<string, number>();\n const dependents = new Map<string, Set<string>>();\n\n for (const [name, node] of nodes) {\n // A set: a dependency that a node lists twice counts once. (`dependents` is a set, so it\n // takes the in-degree down once: a repeated dependency was \"Circular\": deep dive DATA-21.)\n const depsInDag = new Set(node.dependencies.filter((dep) => nodes.has(dep)));\n inDegree.set(name, depsInDag.size);\n dependents.set(name, new Set());\n }\n\n for (const [name, node] of nodes) {\n for (const dep of node.dependencies) {\n if (dependents.has(dep)) {\n dependents.get(dep)!.add(name);\n }\n }\n }\n\n let queue: string[] = [];\n for (const [name, degree] of inDegree) {\n if (degree === 0) {\n queue.push(name);\n }\n }\n\n const levels: TNode[][] = [];\n const nodeLevel = new Map<string, number>();\n let processedCount = 0;\n\n while (queue.length > 0) {\n const currentLevel = levels.length;\n const levelNodes: TNode[] = [];\n const nextQueue: string[] = [];\n\n for (const name of queue) {\n const node = nodes.get(name)!;\n node.level = currentLevel;\n nodeLevel.set(name, currentLevel);\n levelNodes.push(node);\n processedCount++;\n\n for (const dependent of dependents.get(name) ?? []) {\n const newDegree = inDegree.get(dependent)! - 1;\n inDegree.set(dependent, newDegree);\n\n if (newDegree === 0) {\n nextQueue.push(dependent);\n }\n }\n }\n\n levels.push(levelNodes);\n queue = nextQueue;\n }\n\n if (processedCount !== nodes.size) {\n const cycleNodes = [...nodes.keys()].filter((n) => !nodeLevel.has(n));\n throw new Error(\"Circular dependency detected involving: \" + cycleNodes.join(\", \"));\n }\n\n const roots = [...nodes.keys()].filter((name) => {\n const deps = dependents.get(name);\n return !deps || deps.size === 0;\n });\n\n const leaves = [...nodes.keys()].filter((name) => {\n const node = nodes.get(name)!;\n const depsInDag = node.dependencies.filter((dep) => nodes.has(dep));\n return depsInDag.length === 0;\n });\n\n return { nodes, levels, roots, leaves };\n}\n\n/**\n * Get the topological order (flat list, leaves first)\n */\nexport function getTopologicalOrder<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>\n): TNode[] {\n return dag.levels.flat();\n}\n\n/**\n * Visualize the DAG structure (for debugging)\n */\nexport function visualizeDAG<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>\n): string {\n const lines = [\"DAG Structure:\"];\n\n for (let i = 0; i < dag.levels.length; i++) {\n const level = dag.levels[i]!;\n lines.push(\" Level \" + i + \":\");\n for (const node of level) {\n const deps =\n node.dependencies.length > 0\n ? \" -> [\" + node.dependencies.join(\", \") + \"]\"\n : \" (leaf)\";\n lines.push(\" - \" + node.id + deps);\n }\n }\n\n return lines.join(\"\\n\");\n}\n","/**\n * DAG Executor with parallel level-based execution\n */\n\nimport type {\n DependencyDAG,\n DAGNode,\n DAGExecutionOptions,\n NodeResult,\n DAGResult,\n} from \"./types.js\";\n\n/**\n * Start a batch of items with concurrency limit\n */\nasync function executeWithConcurrency<T, R>(\n items: T[],\n fn: (item: T) => Promise<R>,\n limit: number\n): Promise<R[]> {\n const results: R[] = [];\n const executing = new Set<Promise<void>>();\n\n for (const item of items) {\n const promise = fn(item)\n .then((result) => {\n results.push(result);\n })\n .finally(() => {\n // Rejection-safe cleanup: always remove the settled promise from the\n // executing set, even if `fn` rejected, so the set never leaks a\n // settled (possibly rejected) promise into later `race`/`all` waits.\n executing.delete(promise);\n });\n\n executing.add(promise);\n\n if (executing.size >= limit) {\n await Promise.race(executing);\n }\n }\n\n await Promise.all(executing);\n return results;\n}\n\n/**\n * Call a listener. A listener that throws does not stop the run: the function gives a log\n * line for its error (deep dive DATA-21).\n */\nfunction notify<T>(name: string, listener: ((arg: T) => void) | undefined, arg: T): string | undefined {\n if (!listener) return undefined;\n try {\n listener(arg);\n return undefined;\n } catch (error) {\n return name + \" threw: \" + (error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Start DAG with level-based parallelization\n *\n * - Levels are processed sequentially (level 0 first, then level 1, and so on)\n * - Within each level, nodes are processed in parallel up to concurrency limit\n * - Supports fail-fast or continue-on-error modes\n *\n * A processor that throws does NOT reject the whole run. The run records the throw\n * as a failed NodeResult, so partial results are always given.\n *\n * With `skipDependents` (the default), a node whose dependency failed or was skipped does not start. Its result has\n * `skipped: true` and an error that names those dependencies. A dependency outside the DAG does not count.\n *\n * `failFast` operates on levels. When a node of a level fails, each node of that level continues to its end.\n * This includes the nodes that did not start before the failure, when `concurrency` is less than the\n * size of the level. No later level starts. The run does not cancel a node.\n *\n * A node that did not start (`skipped: true`) calls neither `onNodeStart` nor `onNodeComplete`.\n */\nexport async function executeDAG<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>,\n processor: (node: TNode) => Promise<NodeResult<TNode>>,\n options: DAGExecutionOptions<TNode> = {}\n): Promise<DAGResult<TNode>> {\n const {\n concurrency = 4,\n failFast = true,\n skipDependents = true,\n onNodeStart,\n onNodeComplete,\n } = options;\n\n const results = new Map<string, NodeResult<TNode>>();\n const failedNodes: string[] = [];\n const skippedNodes: string[] = [];\n // The ids of the nodes that failed or were skipped\n const blocked = new Set<string>();\n const startTime = Date.now();\n let shouldStop = false;\n\n for (const level of dag.levels) {\n if (shouldStop) break;\n\n const runnable: TNode[] = [];\n for (const node of level) {\n const blockers = skipDependents ? node.dependencies.filter((dep) => blocked.has(dep)) : [];\n if (blockers.length === 0) {\n runnable.push(node);\n continue;\n }\n const message = \"Skipped \" + node.id + \": the dependencies \" + blockers.join(\", \") + \" failed or were skipped\";\n results.set(node.id, { node, success: false, skipped: true, error: new Error(message), duration: 0, logs: [message] });\n skippedNodes.push(node.id);\n blocked.add(node.id);\n }\n\n const levelResults = await executeWithConcurrency(\n runnable,\n async (node) => {\n const startError = notify(\"onNodeStart\", onNodeStart, node);\n const nodeStart = Date.now();\n let result: NodeResult<TNode>;\n try {\n result = await processor(node);\n } catch (error) {\n // Convert a thrown processor error into a failed NodeResult instead\n // of rejecting the entire run and discarding partial results.\n const err =\n error instanceof Error ? error : new Error(String(error));\n result = {\n node,\n success: false,\n error: err,\n duration: Date.now() - nodeStart,\n logs: [\"Processor threw: \" + err.message],\n };\n }\n const completeError = notify(\"onNodeComplete\", onNodeComplete, result);\n const listenerErrors = [startError, completeError].filter((line): line is string => line !== undefined);\n if (listenerErrors.length > 0) {\n result = { ...result, logs: [...(result.logs ?? []), ...listenerErrors] };\n }\n return result;\n },\n concurrency\n );\n\n for (const result of levelResults) {\n results.set(result.node.id, result);\n if (!result.success) {\n failedNodes.push(result.node.id);\n blocked.add(result.node.id);\n if (failFast) {\n shouldStop = true;\n }\n }\n }\n }\n\n return {\n success: failedNodes.length === 0 && skippedNodes.length === 0,\n results,\n failedNodes,\n skippedNodes,\n totalDuration: Date.now() - startTime,\n };\n}\n\n/**\n * Start DAG sequentially (no parallelization)\n */\nexport async function executeDAGSequential<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>,\n processor: (node: TNode) => Promise<NodeResult<TNode>>,\n options: Omit<DAGExecutionOptions<TNode>, \"concurrency\"> = {}\n): Promise<DAGResult<TNode>> {\n return executeDAG(dag, processor, { ...options, concurrency: 1 });\n}\n\n/**\n * Make a simple processor that wraps an async function\n */\nexport function createProcessor<TNode extends DAGNode>(\n fn: (node: TNode) => Promise<void>\n): (node: TNode) => Promise<NodeResult<TNode>> {\n return async (node: TNode): Promise<NodeResult<TNode>> => {\n const startTime = Date.now();\n const logs: string[] = [];\n\n try {\n logs.push(\"Starting \" + node.id);\n await fn(node);\n logs.push(\"Completed \" + node.id);\n\n return {\n node,\n success: true,\n duration: Date.now() - startTime,\n logs,\n };\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n logs.push(\"Failed \" + node.id + \": \" + err.message);\n\n return {\n node,\n success: false,\n error: err,\n duration: Date.now() - startTime,\n logs,\n };\n }\n };\n}\n","/**\n * DAG Builder utilities\n */\n\nimport type { DAGNode } from \"./types.js\";\n\n/**\n * Filter DAG to only include nodes reachable from a root\n */\nexport function filterDAGFromRoot<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n rootId: string\n): Map<string, TNode> {\n const filtered = new Map<string, TNode>();\n const visited = new Set<string>();\n\n function visit(id: string): void {\n if (visited.has(id)) return;\n visited.add(id);\n\n const node = nodes.get(id);\n if (!node) return;\n\n filtered.set(id, node);\n\n for (const dep of node.dependencies) {\n visit(dep);\n }\n }\n\n visit(rootId);\n return filtered;\n}\n\n/**\n * Get all ancestors of a node (nodes that this node depends on, transitively)\n */\nexport function getAncestors<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n id: string\n): Set<string> {\n const ancestors = new Set<string>();\n const visited = new Set<string>();\n\n function visit(n: string): void {\n if (visited.has(n)) return;\n visited.add(n);\n\n const node = nodes.get(n);\n if (!node) return;\n\n for (const dep of node.dependencies) {\n if (nodes.has(dep)) {\n ancestors.add(dep);\n visit(dep);\n }\n }\n }\n\n visit(id);\n return ancestors;\n}\n\n/**\n * Get all descendants of a node (nodes that depend on this node, transitively)\n */\nexport function getDescendants<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n id: string\n): Set<string> {\n const descendants = new Set<string>();\n const visited = new Set<string>();\n\n // Build reverse dependency map\n const dependents = new Map<string, Set<string>>();\n for (const [nodeId, node] of nodes) {\n for (const dep of node.dependencies) {\n if (!dependents.has(dep)) {\n dependents.set(dep, new Set());\n }\n dependents.get(dep)!.add(nodeId);\n }\n }\n\n function visit(n: string): void {\n if (visited.has(n)) return;\n visited.add(n);\n\n const deps = dependents.get(n);\n if (!deps) return;\n\n for (const dep of deps) {\n descendants.add(dep);\n visit(dep);\n }\n }\n\n visit(id);\n return descendants;\n}\n\n/**\n * Make a DAG node from minimal input\n */\nexport function createNode<TData = unknown>(\n id: string,\n dependencies: string[] = [],\n data?: TData | undefined\n): DAGNode<TData> {\n const node: DAGNode<TData> = { id, dependencies };\n if (data !== undefined) {\n node.data = data;\n }\n return node;\n}\n\n/**\n * Build a Map of nodes from an array\n */\nexport function buildNodeMap<TNode extends DAGNode>(\n nodes: TNode[]\n): Map<string, TNode> {\n return new Map(nodes.map((n) => [n.id, n]));\n}\n"],"mappings":";;;;;;;;;;;;AAoBA,SAAgB,gBACd,OACsB;CACtB,MAAM,2BAAW,IAAI,IAAoB;CACzC,MAAM,6BAAa,IAAI,IAAyB;CAEhD,KAAK,MAAM,CAAC,MAAM,SAAS,OAAO;EAGhC,MAAM,YAAY,IAAI,IAAI,KAAK,aAAa,QAAQ,QAAQ,MAAM,IAAI,GAAG,CAAC,CAAC;EAC3E,SAAS,IAAI,MAAM,UAAU,IAAI;EACjC,WAAW,IAAI,sBAAM,IAAI,IAAI,CAAC;CAChC;CAEA,KAAK,MAAM,CAAC,MAAM,SAAS,OACzB,KAAK,MAAM,OAAO,KAAK,cACrB,IAAI,WAAW,IAAI,GAAG,GACpB,WAAW,IAAI,GAAG,CAAC,CAAE,IAAI,IAAI;CAKnC,IAAI,QAAkB,CAAC;CACvB,KAAK,MAAM,CAAC,MAAM,WAAW,UAC3B,IAAI,WAAW,GACb,MAAM,KAAK,IAAI;CAInB,MAAM,SAAoB,CAAC;CAC3B,MAAM,4BAAY,IAAI,IAAoB;CAC1C,IAAI,iBAAiB;CAErB,OAAO,MAAM,SAAS,GAAG;EACvB,MAAM,eAAe,OAAO;EAC5B,MAAM,aAAsB,CAAC;EAC7B,MAAM,YAAsB,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index.js","names":[],"sources":["../../src/dag/traversal.ts","../../src/dag/executor.ts","../../src/dag/builder.ts"],"sourcesContent":["/**\n * DAG Traversal using Kahn's Algorithm\n *\n * This module implements topological sort with level assignment for parallel execution.\n * Level 0 = leaves (no dependencies), higher levels depend on lower levels.\n */\n\nimport type { DAGNode, DependencyDAG } from \"./types.js\";\n\n/**\n * This function builds a leveled DAG from nodes, with Kahn's algorithm.\n *\n * This assigns levels to each node where:\n * - Level 0: Nodes with no dependencies (leaves)\n * - Level N: Nodes whose dependencies are all at level < N\n *\n * Nodes at the same level can be processed in parallel.\n *\n * @throws Error if a cycle is detected\n */\nexport function buildLeveledDAG<TNode extends DAGNode>(\n nodes: Map<string, TNode>\n): DependencyDAG<TNode> {\n const inDegree = new Map<string, number>();\n const dependents = new Map<string, Set<string>>();\n\n for (const [name, node] of nodes) {\n // A set: a dependency that a node lists twice counts once. (`dependents` is a set, so it\n // takes the in-degree down once: a repeated dependency was \"Circular\": deep dive DATA-21.)\n const depsInDag = new Set(node.dependencies.filter((dep) => nodes.has(dep)));\n inDegree.set(name, depsInDag.size);\n dependents.set(name, new Set());\n }\n\n for (const [name, node] of nodes) {\n for (const dep of node.dependencies) {\n if (dependents.has(dep)) {\n dependents.get(dep)!.add(name);\n }\n }\n }\n\n let queue: string[] = [];\n for (const [name, degree] of inDegree) {\n if (degree === 0) {\n queue.push(name);\n }\n }\n\n const levels: TNode[][] = [];\n const nodeLevel = new Map<string, number>();\n let processedCount = 0;\n\n while (queue.length > 0) {\n const currentLevel = levels.length;\n const levelNodes: TNode[] = [];\n const nextQueue: string[] = [];\n\n for (const name of queue) {\n const node = nodes.get(name)!;\n node.level = currentLevel;\n nodeLevel.set(name, currentLevel);\n levelNodes.push(node);\n processedCount++;\n\n for (const dependent of dependents.get(name) ?? []) {\n const newDegree = inDegree.get(dependent)! - 1;\n inDegree.set(dependent, newDegree);\n\n if (newDegree === 0) {\n nextQueue.push(dependent);\n }\n }\n }\n\n levels.push(levelNodes);\n queue = nextQueue;\n }\n\n if (processedCount !== nodes.size) {\n const cycleNodes = [...nodes.keys()].filter((n) => !nodeLevel.has(n));\n throw new Error(\"Circular dependency detected involving: \" + cycleNodes.join(\", \"));\n }\n\n const roots = [...nodes.keys()].filter((name) => {\n const deps = dependents.get(name);\n return !deps || deps.size === 0;\n });\n\n const leaves = [...nodes.keys()].filter((name) => {\n const node = nodes.get(name)!;\n const depsInDag = node.dependencies.filter((dep) => nodes.has(dep));\n return depsInDag.length === 0;\n });\n\n return { nodes, levels, roots, leaves };\n}\n\n/**\n * This function gives the topological order: a flat list, leaves first.\n */\nexport function getTopologicalOrder<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>\n): TNode[] {\n return dag.levels.flat();\n}\n\n/**\n * Visualize the DAG structure (for debugging)\n */\nexport function visualizeDAG<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>\n): string {\n const lines = [\"DAG Structure:\"];\n\n for (let i = 0; i < dag.levels.length; i++) {\n const level = dag.levels[i]!;\n lines.push(\" Level \" + i + \":\");\n for (const node of level) {\n const deps =\n node.dependencies.length > 0\n ? \" -> [\" + node.dependencies.join(\", \") + \"]\"\n : \" (leaf)\";\n lines.push(\" - \" + node.id + deps);\n }\n }\n\n return lines.join(\"\\n\");\n}\n","/**\n * DAG Executor with parallel level-based execution\n */\n\nimport type {\n DependencyDAG,\n DAGNode,\n DAGExecutionOptions,\n NodeResult,\n DAGResult,\n} from \"./types.js\";\n\n/**\n * This function starts a batch of items with a limit of concurrency.\n */\nasync function executeWithConcurrency<T, R>(\n items: T[],\n fn: (item: T) => Promise<R>,\n limit: number\n): Promise<R[]> {\n const results: R[] = [];\n const executing = new Set<Promise<void>>();\n\n for (const item of items) {\n const promise = fn(item)\n .then((result) => {\n results.push(result);\n })\n .finally(() => {\n // Rejection-safe cleanup: always remove the settled promise from the\n // executing set, even if `fn` rejected, so the set never leaks a\n // settled (possibly rejected) promise into later `race`/`all` waits.\n executing.delete(promise);\n });\n\n executing.add(promise);\n\n if (executing.size >= limit) {\n await Promise.race(executing);\n }\n }\n\n await Promise.all(executing);\n return results;\n}\n\n/**\n * This function calls a listener. A listener that throws does not stop the run: the function gives a log\n * line for its error (deep dive DATA-21).\n */\nfunction notify<T>(name: string, listener: ((arg: T) => void) | undefined, arg: T): string | undefined {\n if (!listener) return undefined;\n try {\n listener(arg);\n return undefined;\n } catch (error) {\n return name + \" threw: \" + (error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * This function starts a DAG level by level, with the nodes of each level in parallel.\n *\n * - Levels are processed sequentially (level 0 first, then level 1, and so on)\n * - Within each level, nodes are processed in parallel up to concurrency limit\n * - Supports fail-fast or continue-on-error modes\n *\n * A processor that throws does NOT reject the whole run. The run records the throw\n * as a failed NodeResult, so partial results are always given.\n *\n * With `skipDependents` (the default), a node whose dependency failed or was skipped does not start. Its result has\n * `skipped: true` and an error that names those dependencies. A dependency outside the DAG does not count.\n *\n * `failFast` operates on levels. When a node of a level fails, each node of that level continues to its end.\n * This includes the nodes that did not start before the failure, when `concurrency` is less than the\n * size of the level. No later level starts. The run does not cancel a node.\n *\n * A node that did not start (`skipped: true`) calls neither `onNodeStart` nor `onNodeComplete`.\n */\nexport async function executeDAG<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>,\n processor: (node: TNode) => Promise<NodeResult<TNode>>,\n options: DAGExecutionOptions<TNode> = {}\n): Promise<DAGResult<TNode>> {\n const {\n concurrency = 4,\n failFast = true,\n skipDependents = true,\n onNodeStart,\n onNodeComplete,\n } = options;\n\n const results = new Map<string, NodeResult<TNode>>();\n const failedNodes: string[] = [];\n const skippedNodes: string[] = [];\n // The ids of the nodes that failed or were skipped\n const blocked = new Set<string>();\n const startTime = Date.now();\n let shouldStop = false;\n\n for (const level of dag.levels) {\n if (shouldStop) break;\n\n const runnable: TNode[] = [];\n for (const node of level) {\n const blockers = skipDependents ? node.dependencies.filter((dep) => blocked.has(dep)) : [];\n if (blockers.length === 0) {\n runnable.push(node);\n continue;\n }\n const message = \"Skipped \" + node.id + \": the dependencies \" + blockers.join(\", \") + \" failed or were skipped\";\n results.set(node.id, { node, success: false, skipped: true, error: new Error(message), duration: 0, logs: [message] });\n skippedNodes.push(node.id);\n blocked.add(node.id);\n }\n\n const levelResults = await executeWithConcurrency(\n runnable,\n async (node) => {\n const startError = notify(\"onNodeStart\", onNodeStart, node);\n const nodeStart = Date.now();\n let result: NodeResult<TNode>;\n try {\n result = await processor(node);\n } catch (error) {\n // Convert a thrown processor error into a failed NodeResult instead\n // of rejecting the entire run and discarding partial results.\n const err =\n error instanceof Error ? error : new Error(String(error));\n result = {\n node,\n success: false,\n error: err,\n duration: Date.now() - nodeStart,\n logs: [\"Processor threw: \" + err.message],\n };\n }\n const completeError = notify(\"onNodeComplete\", onNodeComplete, result);\n const listenerErrors = [startError, completeError].filter((line): line is string => line !== undefined);\n if (listenerErrors.length > 0) {\n result = { ...result, logs: [...(result.logs ?? []), ...listenerErrors] };\n }\n return result;\n },\n concurrency\n );\n\n for (const result of levelResults) {\n results.set(result.node.id, result);\n if (!result.success) {\n failedNodes.push(result.node.id);\n blocked.add(result.node.id);\n if (failFast) {\n shouldStop = true;\n }\n }\n }\n }\n\n return {\n success: failedNodes.length === 0 && skippedNodes.length === 0,\n results,\n failedNodes,\n skippedNodes,\n totalDuration: Date.now() - startTime,\n };\n}\n\n/**\n * This function starts a DAG one node at a time, with no nodes in parallel.\n */\nexport async function executeDAGSequential<TNode extends DAGNode>(\n dag: DependencyDAG<TNode>,\n processor: (node: TNode) => Promise<NodeResult<TNode>>,\n options: Omit<DAGExecutionOptions<TNode>, \"concurrency\"> = {}\n): Promise<DAGResult<TNode>> {\n return executeDAG(dag, processor, { ...options, concurrency: 1 });\n}\n\n/**\n * This function makes a simple processor that wraps an async function.\n */\nexport function createProcessor<TNode extends DAGNode>(\n fn: (node: TNode) => Promise<void>\n): (node: TNode) => Promise<NodeResult<TNode>> {\n return async (node: TNode): Promise<NodeResult<TNode>> => {\n const startTime = Date.now();\n const logs: string[] = [];\n\n try {\n logs.push(\"Starting \" + node.id);\n await fn(node);\n logs.push(\"Completed \" + node.id);\n\n return {\n node,\n success: true,\n duration: Date.now() - startTime,\n logs,\n };\n } catch (error) {\n const err = error instanceof Error ? error : new Error(String(error));\n logs.push(\"Failed \" + node.id + \": \" + err.message);\n\n return {\n node,\n success: false,\n error: err,\n duration: Date.now() - startTime,\n logs,\n };\n }\n };\n}\n","/**\n * DAG Builder utilities\n */\n\nimport type { DAGNode } from \"./types.js\";\n\n/**\n * This function filters a DAG to the nodes that a root can reach.\n */\nexport function filterDAGFromRoot<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n rootId: string\n): Map<string, TNode> {\n const filtered = new Map<string, TNode>();\n const visited = new Set<string>();\n\n function visit(id: string): void {\n if (visited.has(id)) return;\n visited.add(id);\n\n const node = nodes.get(id);\n if (!node) return;\n\n filtered.set(id, node);\n\n for (const dep of node.dependencies) {\n visit(dep);\n }\n }\n\n visit(rootId);\n return filtered;\n}\n\n/**\n * This function gives all ancestors of a node: the nodes that this node depends on, transitively.\n */\nexport function getAncestors<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n id: string\n): Set<string> {\n const ancestors = new Set<string>();\n const visited = new Set<string>();\n\n function visit(n: string): void {\n if (visited.has(n)) return;\n visited.add(n);\n\n const node = nodes.get(n);\n if (!node) return;\n\n for (const dep of node.dependencies) {\n if (nodes.has(dep)) {\n ancestors.add(dep);\n visit(dep);\n }\n }\n }\n\n visit(id);\n return ancestors;\n}\n\n/**\n * This function gives all descendants of a node: the nodes that depend on this node, transitively.\n */\nexport function getDescendants<TNode extends DAGNode>(\n nodes: Map<string, TNode>,\n id: string\n): Set<string> {\n const descendants = new Set<string>();\n const visited = new Set<string>();\n\n // Build reverse dependency map\n const dependents = new Map<string, Set<string>>();\n for (const [nodeId, node] of nodes) {\n for (const dep of node.dependencies) {\n if (!dependents.has(dep)) {\n dependents.set(dep, new Set());\n }\n dependents.get(dep)!.add(nodeId);\n }\n }\n\n function visit(n: string): void {\n if (visited.has(n)) return;\n visited.add(n);\n\n const deps = dependents.get(n);\n if (!deps) return;\n\n for (const dep of deps) {\n descendants.add(dep);\n visit(dep);\n }\n }\n\n visit(id);\n return descendants;\n}\n\n/**\n * This function makes a DAG node from a minimal input.\n */\nexport function createNode<TData = unknown>(\n id: string,\n dependencies: string[] = [],\n data?: TData | undefined\n): DAGNode<TData> {\n const node: DAGNode<TData> = { id, dependencies };\n if (data !== undefined) {\n node.data = data;\n }\n return node;\n}\n\n/**\n * This function builds a Map of nodes from an array.\n */\nexport function buildNodeMap<TNode extends DAGNode>(\n nodes: TNode[]\n): Map<string, TNode> {\n return new Map(nodes.map((n) => [n.id, n]));\n}\n"],"mappings":";;;;;;;;;;;;AAoBA,SAAgB,gBACd,OACsB;CACtB,MAAM,2BAAW,IAAI,IAAoB;CACzC,MAAM,6BAAa,IAAI,IAAyB;CAEhD,KAAK,MAAM,CAAC,MAAM,SAAS,OAAO;EAGhC,MAAM,YAAY,IAAI,IAAI,KAAK,aAAa,QAAQ,QAAQ,MAAM,IAAI,GAAG,CAAC,CAAC;EAC3E,SAAS,IAAI,MAAM,UAAU,IAAI;EACjC,WAAW,IAAI,sBAAM,IAAI,IAAI,CAAC;CAChC;CAEA,KAAK,MAAM,CAAC,MAAM,SAAS,OACzB,KAAK,MAAM,OAAO,KAAK,cACrB,IAAI,WAAW,IAAI,GAAG,GACpB,WAAW,IAAI,GAAG,CAAC,CAAE,IAAI,IAAI;CAKnC,IAAI,QAAkB,CAAC;CACvB,KAAK,MAAM,CAAC,MAAM,WAAW,UAC3B,IAAI,WAAW,GACb,MAAM,KAAK,IAAI;CAInB,MAAM,SAAoB,CAAC;CAC3B,MAAM,4BAAY,IAAI,IAAoB;CAC1C,IAAI,iBAAiB;CAErB,OAAO,MAAM,SAAS,GAAG;EACvB,MAAM,eAAe,OAAO;EAC5B,MAAM,aAAsB,CAAC;EAC7B,MAAM,YAAsB,CAAC;EAE7B,KAAK,MAAM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