koode-taskgraph 0.2.0__tar.gz → 0.3.0__tar.gz

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.

Potentially problematic release.


This version of koode-taskgraph might be problematic. Click here for more details.

Files changed (32) hide show
  1. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/PKG-INFO +1 -1
  2. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/README.md +6 -0
  3. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/PKG-INFO +1 -1
  4. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/pyproject.toml +1 -1
  5. koode_taskgraph-0.3.0/taskgraph/core/executor.py +300 -0
  6. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/properties.py +30 -2
  7. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/tests/test_gui_plugins.py +31 -2
  8. koode_taskgraph-0.2.0/taskgraph/core/executor.py +0 -232
  9. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/LICENSE +0 -0
  10. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/SOURCES.txt +0 -0
  11. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/dependency_links.txt +0 -0
  12. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/entry_points.txt +0 -0
  13. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/requires.txt +0 -0
  14. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/koode_taskgraph.egg-info/top_level.txt +0 -0
  15. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/setup.cfg +0 -0
  16. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/__init__.py +0 -0
  17. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/__main__.py +0 -0
  18. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/app.py +0 -0
  19. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/cli.py +0 -0
  20. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/core/__init__.py +0 -0
  21. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/core/model.py +0 -0
  22. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/core/registry.py +0 -0
  23. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/core/serialization.py +0 -0
  24. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/nodes/__init__.py +0 -0
  25. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/nodes/builtin.py +0 -0
  26. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/__init__.py +0 -0
  27. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/items.py +0 -0
  28. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/main_window.py +0 -0
  29. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/plugins.py +0 -0
  30. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/scene.py +0 -0
  31. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/taskgraph/ui/theme.py +0 -0
  32. {koode_taskgraph-0.2.0 → koode_taskgraph-0.3.0}/tests/test_core.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: koode-taskgraph
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: An extensible process node graph built with QtPy and PySide6
5
5
  Requires-Python: >=3.10
6
6
  License-File: LICENSE
@@ -15,6 +15,7 @@ koode-taskgraph
15
15
  ```
16
16
 
17
17
  For local development from this repository, use `python -m pip install -e .`.
18
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for pull request guidelines.
18
19
 
19
20
  ## Headless CLI
20
21
 
@@ -67,6 +68,11 @@ class PrefixText(ProcessNode):
67
68
  return {"text": f"{self.prefix}{inputs.get('value', '')}"}
68
69
  ```
69
70
 
71
+ Property kinds map to generated controls in the Properties panel: `"text"` for
72
+ a line edit, `"file"` for a file path field with a Browse button, `"bool"` for
73
+ a checkbox, `"int"`/`"float"` for numeric fields, and `"choice"` for a
74
+ dropdown using `choices`.
75
+
70
76
  Modules in `taskgraph/nodes/` are discovered automatically. Custom modules can
71
77
  also live anywhere on disk:
72
78
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: koode-taskgraph
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: An extensible process node graph built with QtPy and PySide6
5
5
  Requires-Python: >=3.10
6
6
  License-File: LICENSE
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "koode-taskgraph"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "An extensible process node graph built with QtPy and PySide6"
9
9
  requires-python = ">=3.10"
10
10
  dependencies = [
@@ -0,0 +1,300 @@
1
+ from __future__ import annotations
2
+
3
+ from collections import defaultdict, deque
4
+ from concurrent.futures import FIRST_COMPLETED, Future, ThreadPoolExecutor, wait
5
+ from dataclasses import dataclass
6
+ from threading import Event
7
+ from typing import Any, Callable
8
+
9
+ from taskgraph.core.model import Connection, Graph, NodeCancelled
10
+
11
+ VALID_WORKER_COUNTS = (1, *range(2, 33, 2))
12
+
13
+
14
+ class GraphExecutionError(RuntimeError):
15
+ pass
16
+
17
+
18
+ class GraphExecutionCancelled(GraphExecutionError):
19
+ pass
20
+
21
+
22
+ @dataclass
23
+ class ExecutionResult:
24
+ order: list[str]
25
+ outputs: dict[str, dict[str, Any]]
26
+
27
+
28
+ def execute_graph(
29
+ graph: Graph,
30
+ on_event: Callable[[str], None] | None = None,
31
+ max_workers: int = 4,
32
+ on_node_state: Callable[[str, str], None] | None = None,
33
+ cancel_event: Event | None = None,
34
+ ) -> ExecutionResult:
35
+ return GraphExecutor(
36
+ graph,
37
+ on_event=on_event,
38
+ on_node_state=on_node_state,
39
+ cancel_event=cancel_event,
40
+ ).execute(max_workers=max_workers)
41
+
42
+
43
+ class GraphExecutor:
44
+ def __init__(
45
+ self,
46
+ graph: Graph,
47
+ on_event: Callable[[str], None] | None = None,
48
+ on_node_state: Callable[[str, str], None] | None = None,
49
+ cancel_event: Event | None = None,
50
+ ):
51
+ self.graph = graph
52
+ self.emit = on_event or (lambda _message: None)
53
+ self.set_state = on_node_state or (lambda _node_id, _state: None)
54
+ self.cancel_event = cancel_event or Event()
55
+ self.indegree = {node_id: 0 for node_id in graph.nodes}
56
+ self.downstream: dict[str, list[str]] = defaultdict(list)
57
+ self.incoming: dict[str, list[Connection]] = defaultdict(list)
58
+ self.attribute_edges: list[Connection] = []
59
+ self.queue: deque[str] = deque()
60
+ self.order: list[str] = []
61
+ self.outputs: dict[str, dict[str, Any]] = {}
62
+ self.terminal: set[str] = set()
63
+ self.failures: list[str] = []
64
+ self.running: dict[Future[dict[str, Any]], str] = {}
65
+ self.cancellation_applied = False
66
+
67
+ def execute(self, max_workers: int = 4) -> ExecutionResult:
68
+ self._validate_worker_count(max_workers)
69
+ self._prepare_connections()
70
+ self._validate_attribute_dependencies()
71
+ self.queue = deque(
72
+ node_id for node_id, degree in self.indegree.items()
73
+ if degree == 0
74
+ )
75
+ self._run(max_workers)
76
+ self._raise_if_cancelled()
77
+ self._raise_if_unresolved_or_failed()
78
+ return ExecutionResult(self.order, self.outputs)
79
+
80
+ @staticmethod
81
+ def _validate_worker_count(max_workers: int) -> None:
82
+ if max_workers not in VALID_WORKER_COUNTS:
83
+ raise ValueError(
84
+ "max_workers must be 1 or an even number between 2 and 32"
85
+ )
86
+
87
+ def _prepare_connections(self) -> None:
88
+ for edge in self.graph.connections:
89
+ if (
90
+ edge.source_node not in self.graph.nodes
91
+ or edge.target_node not in self.graph.nodes
92
+ ):
93
+ raise GraphExecutionError("A connection references a missing node")
94
+ if edge.kind == "dependency":
95
+ self.indegree[edge.target_node] += 1
96
+ self.downstream[edge.source_node].append(edge.target_node)
97
+ else:
98
+ self.attribute_edges.append(edge)
99
+ self.incoming[edge.target_node].append(edge)
100
+
101
+ def _validate_attribute_dependencies(self) -> None:
102
+ for edge in self.attribute_edges:
103
+ if not self._has_dependency_path(edge.source_node, edge.target_node):
104
+ source = self.graph.nodes[edge.source_node]
105
+ target = self.graph.nodes[edge.target_node]
106
+ raise GraphExecutionError(
107
+ f"Attribute {source.display_name}.{edge.source_port} -> "
108
+ f"{target.display_name}.{edge.target_port} requires a dependency path"
109
+ )
110
+
111
+ def _has_dependency_path(self, source: str, target: str) -> bool:
112
+ pending = [source]
113
+ visited = set()
114
+ while pending:
115
+ current = pending.pop()
116
+ if current == target:
117
+ return True
118
+ if current not in visited:
119
+ visited.add(current)
120
+ pending.extend(self.downstream[current])
121
+ return False
122
+
123
+ def _run(self, max_workers: int) -> None:
124
+ with ThreadPoolExecutor(
125
+ max_workers=max_workers,
126
+ thread_name_prefix="taskgraph",
127
+ ) as pool:
128
+ while self.queue or self.running:
129
+ self._apply_cancellation_if_requested()
130
+ self._submit_ready_nodes(pool, max_workers)
131
+ if not self.running:
132
+ continue
133
+ finished, _pending = wait(
134
+ self.running,
135
+ return_when=FIRST_COMPLETED,
136
+ )
137
+ self._handle_finished_futures(finished)
138
+
139
+ def _apply_cancellation_if_requested(self) -> None:
140
+ if not self.cancel_event.is_set() or self.cancellation_applied:
141
+ return
142
+ self.cancellation_applied = True
143
+ self.emit("Cancellation requested...")
144
+ running_ids = set(self.running.values())
145
+ for future in self.running:
146
+ future.cancel()
147
+ for node_id in self.graph.nodes:
148
+ if node_id not in self.terminal and node_id not in running_ids:
149
+ self.terminal.add(node_id)
150
+ self.set_state(node_id, "cancelled")
151
+ self.queue.clear()
152
+
153
+ def _submit_ready_nodes(
154
+ self,
155
+ pool: ThreadPoolExecutor,
156
+ max_workers: int,
157
+ ) -> None:
158
+ while self.queue and len(self.running) < max_workers:
159
+ node_id = self.queue.popleft()
160
+ if node_id in self.terminal:
161
+ continue
162
+ node = self.graph.nodes[node_id]
163
+ if node.disabled:
164
+ self.emit(f"Skipped disabled node: {node.display_name}")
165
+ self.set_state(node_id, "skipped")
166
+ self._complete(node_id, {})
167
+ continue
168
+ try:
169
+ node_inputs = self._inputs_for(node_id)
170
+ except GraphExecutionError as exc:
171
+ self._fail_branch(node_id, str(exc))
172
+ continue
173
+ self.emit(f"Running {node.display_name}")
174
+ self.set_state(node_id, "running")
175
+ node._cancel_event = self.cancel_event
176
+ self.running[pool.submit(node.process, node_inputs)] = node_id
177
+
178
+ def _inputs_for(self, node_id: str) -> dict[str, Any]:
179
+ node = self.graph.nodes[node_id]
180
+ node_inputs: dict[str, Any] = {}
181
+ for edge in self.incoming[node_id]:
182
+ source_outputs = self.outputs.get(edge.source_node, {})
183
+ if edge.source_port not in source_outputs:
184
+ source = self.graph.nodes[edge.source_node]
185
+ raise GraphExecutionError(
186
+ f"{node.display_name}: input '{edge.target_port}' is unavailable "
187
+ f"because {source.display_name} did not produce '{edge.source_port}'"
188
+ )
189
+ value = source_outputs[edge.source_port]
190
+ port = next(
191
+ spec for spec in node.inputs
192
+ if spec.name == edge.target_port
193
+ )
194
+ if port.multiple:
195
+ node_inputs.setdefault(edge.target_port, []).append(value)
196
+ else:
197
+ node_inputs[edge.target_port] = value
198
+
199
+ missing = [
200
+ port.name for port in node.inputs
201
+ if port.required and port.name not in node_inputs
202
+ ]
203
+ if missing:
204
+ raise GraphExecutionError(
205
+ f"{node.display_name}: missing required input(s): {', '.join(missing)}"
206
+ )
207
+ return node_inputs
208
+
209
+ def _handle_finished_futures(
210
+ self,
211
+ finished: set[Future[dict[str, Any]]],
212
+ ) -> None:
213
+ for future in finished:
214
+ node_id = self.running.pop(future)
215
+ node = self.graph.nodes[node_id]
216
+ try:
217
+ if self.cancel_event.is_set():
218
+ self._discard_cancelled_future(future)
219
+ self._mark_cancelled(node_id)
220
+ continue
221
+ result = future.result()
222
+ except NodeCancelled:
223
+ self._mark_cancelled(node_id)
224
+ continue
225
+ except Exception as exc:
226
+ self._fail_branch(node_id, f"{node.display_name} failed: {exc}")
227
+ continue
228
+ finally:
229
+ node._cancel_event = None
230
+
231
+ if not isinstance(result, dict):
232
+ self._fail_branch(
233
+ node_id,
234
+ f"{node.display_name} must return a dictionary",
235
+ )
236
+ continue
237
+ self._complete(node_id, result)
238
+ self.set_state(node_id, "finished")
239
+ self.emit(f"Finished {node.display_name}: {result}")
240
+
241
+ @staticmethod
242
+ def _discard_cancelled_future(future: Future[dict[str, Any]]) -> None:
243
+ try:
244
+ future.result()
245
+ except Exception:
246
+ pass
247
+
248
+ def _mark_cancelled(self, node_id: str) -> None:
249
+ node = self.graph.nodes[node_id]
250
+ self.terminal.add(node_id)
251
+ self.set_state(node_id, "cancelled")
252
+ self.emit(f"Cancelled {node.display_name}")
253
+
254
+ def _complete(self, node_id: str, result: dict[str, Any]) -> None:
255
+ self.outputs[node_id] = result
256
+ self.order.append(node_id)
257
+ self.terminal.add(node_id)
258
+ for target in self.downstream[node_id]:
259
+ self.indegree[target] -= 1
260
+ if self.indegree[target] == 0:
261
+ self.queue.append(target)
262
+
263
+ def _fail_branch(self, node_id: str, message: str) -> None:
264
+ """Fail one node and block only nodes that depend on its output."""
265
+ if node_id in self.terminal:
266
+ return
267
+ self.terminal.add(node_id)
268
+ self.failures.append(message)
269
+ self.set_state(node_id, "failed")
270
+ self.emit(f"Failure: {message}")
271
+ pending = deque(self.downstream[node_id])
272
+ while pending:
273
+ blocked_id = pending.popleft()
274
+ if blocked_id in self.terminal:
275
+ continue
276
+ self.terminal.add(blocked_id)
277
+ self.set_state(blocked_id, "blocked")
278
+ self.emit(
279
+ f"Blocked {self.graph.nodes[blocked_id].display_name}: "
280
+ "an upstream dependency failed"
281
+ )
282
+ pending.extend(self.downstream[blocked_id])
283
+
284
+ def _raise_if_cancelled(self) -> None:
285
+ if not self.cancel_event.is_set():
286
+ return
287
+ for node_id in self.graph.nodes:
288
+ if node_id not in self.terminal:
289
+ self.terminal.add(node_id)
290
+ self.set_state(node_id, "cancelled")
291
+ raise GraphExecutionCancelled("Graph execution cancelled")
292
+
293
+ def _raise_if_unresolved_or_failed(self) -> None:
294
+ unresolved = set(self.graph.nodes) - self.terminal
295
+ if unresolved:
296
+ self.failures.append("The graph contains a cycle and cannot be executed")
297
+ for node_id in unresolved:
298
+ self.set_state(node_id, "blocked")
299
+ if self.failures:
300
+ raise GraphExecutionError("\n".join(self.failures))
@@ -3,8 +3,9 @@ from __future__ import annotations
3
3
  from qtpy.QtCore import Signal
4
4
  from qtpy.QtGui import QColor
5
5
  from qtpy.QtWidgets import (
6
- QCheckBox, QComboBox, QDoubleSpinBox, QFormLayout, QLabel,
7
- QLineEdit, QScrollArea, QSizePolicy, QSpinBox, QTextEdit, QWidget,
6
+ QCheckBox, QComboBox, QDoubleSpinBox, QFileDialog, QFormLayout,
7
+ QHBoxLayout, QLabel, QLineEdit, QPushButton, QScrollArea, QSizePolicy,
8
+ QSpinBox, QTextEdit, QWidget,
8
9
  )
9
10
 
10
11
  from taskgraph.core.model import Backdrop
@@ -128,11 +129,38 @@ class PropertyEditor(QScrollArea):
128
129
  widget.currentIndexChanged.connect(
129
130
  lambda index, s=spec: self._set(s.name, s.choices[index])
130
131
  )
132
+ elif spec.kind == "file":
133
+ widget = self._make_file_editor(spec, value)
131
134
  else:
132
135
  widget = QLineEdit("" if value is None else str(value))
133
136
  widget.editingFinished.connect(lambda w=widget, s=spec: self._set(s.name, w.text()))
134
137
  return widget
135
138
 
139
+ def _make_file_editor(self, spec, value) -> QWidget:
140
+ widget = QWidget()
141
+ layout = QHBoxLayout(widget)
142
+ layout.setContentsMargins(0, 0, 0, 0)
143
+ layout.setSpacing(6)
144
+ path = QLineEdit("" if value is None else str(value))
145
+ browse = QPushButton("Browse...")
146
+ browse.clicked.connect(
147
+ lambda checked=False, editor=path, s=spec: self._browse_file(editor, s)
148
+ )
149
+ path.editingFinished.connect(
150
+ lambda editor=path, s=spec: self._set(s.name, editor.text())
151
+ )
152
+ layout.addWidget(path, 1)
153
+ layout.addWidget(browse)
154
+ return widget
155
+
156
+ def _browse_file(self, editor: QLineEdit, spec) -> None:
157
+ filename, _ = QFileDialog.getOpenFileName(
158
+ self, f"Select {spec.label}", editor.text()
159
+ )
160
+ if filename:
161
+ editor.setText(filename)
162
+ self._set(spec.name, filename)
163
+
136
164
  def _set(self, name, value) -> None:
137
165
  if self._node:
138
166
  self._node.values[name] = value
@@ -5,11 +5,12 @@ import unittest
5
5
 
6
6
  os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
7
7
 
8
- from qtpy.QtWidgets import QApplication
8
+ from qtpy.QtWidgets import QApplication, QLineEdit, QWidget
9
9
 
10
10
  from taskgraph.nodes import load_builtin_nodes
11
- from taskgraph.core.model import Graph
11
+ from taskgraph.core.model import Graph, NodeProperty, ProcessNode
12
12
  from taskgraph.ui.main_window import MainWindow
13
+ from taskgraph.ui.properties import PropertyEditor
13
14
  from taskgraph.ui.plugins import load_gui_plugin_directory
14
15
 
15
16
  PROJECT_ROOT = Path(__file__).resolve().parents[1]
@@ -126,5 +127,33 @@ class GuiPluginTests(unittest.TestCase):
126
127
  )
127
128
 
128
129
 
130
+ class PropertyEditorTests(unittest.TestCase):
131
+ @classmethod
132
+ def setUpClass(cls):
133
+ cls.app = QApplication.instance() or QApplication([])
134
+
135
+ def test_file_property_uses_browse_widget_and_stores_text_path(self):
136
+ class FileNode(ProcessNode):
137
+ title = "File Node"
138
+ type_id = "tests.file_node"
139
+ properties = (
140
+ NodeProperty("source_file", "Source File", "file", ""),
141
+ )
142
+
143
+ node = FileNode()
144
+ editor = PropertyEditor()
145
+ editor.set_node(node)
146
+
147
+ file_widgets = [
148
+ widget for widget in editor.findChildren(QWidget)
149
+ if widget.findChild(QLineEdit)
150
+ ]
151
+ path_editor = file_widgets[-1].findChild(QLineEdit)
152
+ path_editor.setText("/tmp/example.txt")
153
+ path_editor.editingFinished.emit()
154
+
155
+ self.assertEqual(node.values["source_file"], "/tmp/example.txt")
156
+
157
+
129
158
  if __name__ == "__main__":
130
159
  unittest.main()
@@ -1,232 +0,0 @@
1
- from __future__ import annotations
2
-
3
- from collections import defaultdict, deque
4
- from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
5
- from dataclasses import dataclass
6
- from threading import Event
7
- from typing import Any, Callable
8
-
9
- from taskgraph.core.model import Graph, NodeCancelled
10
-
11
- VALID_WORKER_COUNTS = (1, *range(2, 33, 2))
12
-
13
-
14
- class GraphExecutionError(RuntimeError):
15
- pass
16
-
17
-
18
- class GraphExecutionCancelled(GraphExecutionError):
19
- pass
20
-
21
-
22
- @dataclass
23
- class ExecutionResult:
24
- order: list[str]
25
- outputs: dict[str, dict[str, Any]]
26
-
27
-
28
- def execute_graph(
29
- graph: Graph,
30
- on_event: Callable[[str], None] | None = None,
31
- max_workers: int = 4,
32
- on_node_state: Callable[[str, str], None] | None = None,
33
- cancel_event: Event | None = None,
34
- ) -> ExecutionResult:
35
- if max_workers not in VALID_WORKER_COUNTS:
36
- raise ValueError(
37
- "max_workers must be 1 or an even number between 2 and 32"
38
- )
39
- emit = on_event or (lambda _message: None)
40
- set_state = on_node_state or (lambda _node_id, _state: None)
41
- cancel_event = cancel_event or Event()
42
- indegree = {node_id: 0 for node_id in graph.nodes}
43
- downstream: dict[str, list[str]] = defaultdict(list)
44
- incoming = defaultdict(list)
45
-
46
- attribute_edges = []
47
- for edge in graph.connections:
48
- if edge.source_node not in graph.nodes or edge.target_node not in graph.nodes:
49
- raise GraphExecutionError("A connection references a missing node")
50
- if edge.kind == "dependency":
51
- indegree[edge.target_node] += 1
52
- downstream[edge.source_node].append(edge.target_node)
53
- else:
54
- attribute_edges.append(edge)
55
- incoming[edge.target_node].append(edge)
56
-
57
- def has_dependency_path(source: str, target: str) -> bool:
58
- pending = [source]
59
- visited = set()
60
- while pending:
61
- current = pending.pop()
62
- if current == target:
63
- return True
64
- if current not in visited:
65
- visited.add(current)
66
- pending.extend(downstream[current])
67
- return False
68
-
69
- for edge in attribute_edges:
70
- if not has_dependency_path(edge.source_node, edge.target_node):
71
- source = graph.nodes[edge.source_node]
72
- target = graph.nodes[edge.target_node]
73
- raise GraphExecutionError(
74
- f"Attribute {source.display_name}.{edge.source_port} → "
75
- f"{target.display_name}.{edge.target_port} requires a dependency path"
76
- )
77
-
78
- queue = deque(node_id for node_id, degree in indegree.items() if degree == 0)
79
- order: list[str] = []
80
- outputs: dict[str, dict[str, Any]] = {}
81
- terminal: set[str] = set()
82
- failures: list[str] = []
83
-
84
- def inputs_for(node_id: str) -> dict[str, Any]:
85
- node = graph.nodes[node_id]
86
- node_inputs: dict[str, Any] = {}
87
- for edge in incoming[node_id]:
88
- source_outputs = outputs.get(edge.source_node, {})
89
- if edge.source_port not in source_outputs:
90
- source = graph.nodes[edge.source_node]
91
- raise GraphExecutionError(
92
- f"{node.display_name}: input '{edge.target_port}' is unavailable "
93
- f"because {source.display_name} did not produce '{edge.source_port}'"
94
- )
95
- value = source_outputs[edge.source_port]
96
- port = next(spec for spec in node.inputs if spec.name == edge.target_port)
97
- if port.multiple:
98
- node_inputs.setdefault(edge.target_port, []).append(value)
99
- else:
100
- node_inputs[edge.target_port] = value
101
-
102
- missing = [port.name for port in node.inputs if port.required and port.name not in node_inputs]
103
- if missing:
104
- raise GraphExecutionError(
105
- f"{node.display_name}: missing required input(s): {', '.join(missing)}"
106
- )
107
- return node_inputs
108
-
109
- def complete(node_id: str, result: dict[str, Any]) -> None:
110
- outputs[node_id] = result
111
- order.append(node_id)
112
- terminal.add(node_id)
113
- for target in downstream[node_id]:
114
- indegree[target] -= 1
115
- if indegree[target] == 0:
116
- queue.append(target)
117
-
118
- def fail_branch(node_id: str, message: str) -> None:
119
- """Fail one node and block only nodes that depend on its output."""
120
- if node_id in terminal:
121
- return
122
- terminal.add(node_id)
123
- failures.append(message)
124
- set_state(node_id, "failed")
125
- emit(f"Failure: {message}")
126
- pending = deque(downstream[node_id])
127
- while pending:
128
- blocked_id = pending.popleft()
129
- if blocked_id in terminal:
130
- continue
131
- terminal.add(blocked_id)
132
- set_state(blocked_id, "blocked")
133
- emit(
134
- f"Blocked {graph.nodes[blocked_id].display_name}: "
135
- "an upstream dependency failed"
136
- )
137
- pending.extend(downstream[blocked_id])
138
-
139
- running = {}
140
- cancellation_applied = False
141
- with ThreadPoolExecutor(max_workers=max_workers, thread_name_prefix="taskgraph") as pool:
142
- while queue or running:
143
- if cancel_event.is_set() and not cancellation_applied:
144
- cancellation_applied = True
145
- emit("Cancellation requested…")
146
- running_ids = set(running.values())
147
- for future in running:
148
- future.cancel()
149
- for node_id in graph.nodes:
150
- if node_id not in terminal and node_id not in running_ids:
151
- terminal.add(node_id)
152
- set_state(node_id, "cancelled")
153
- queue.clear()
154
-
155
- while queue and len(running) < max_workers:
156
- node_id = queue.popleft()
157
- if node_id in terminal:
158
- continue
159
- node = graph.nodes[node_id]
160
- if node.disabled:
161
- emit(f"Skipped disabled node: {node.display_name}")
162
- set_state(node_id, "skipped")
163
- complete(node_id, {})
164
- continue
165
- try:
166
- node_inputs = inputs_for(node_id)
167
- except GraphExecutionError as exc:
168
- fail_branch(node_id, str(exc))
169
- continue
170
- emit(f"Running {node.display_name}")
171
- set_state(node_id, "running")
172
- node._cancel_event = cancel_event
173
- running[pool.submit(node.process, node_inputs)] = node_id
174
-
175
- if not running:
176
- continue
177
-
178
- finished, _pending = wait(running, return_when=FIRST_COMPLETED)
179
- for future in finished:
180
- node_id = running.pop(future)
181
- node = graph.nodes[node_id]
182
- if cancel_event.is_set():
183
- try:
184
- future.result()
185
- except Exception:
186
- pass
187
- terminal.add(node_id)
188
- set_state(node_id, "cancelled")
189
- emit(f"Cancelled {node.display_name}")
190
- node._cancel_event = None
191
- continue
192
- try:
193
- result = future.result()
194
- except NodeCancelled:
195
- terminal.add(node_id)
196
- set_state(node_id, "cancelled")
197
- emit(f"Cancelled {node.display_name}")
198
- node._cancel_event = None
199
- continue
200
- except Exception as exc:
201
- fail_branch(node_id, f"{node.display_name} failed: {exc}")
202
- node._cancel_event = None
203
- continue
204
- if not isinstance(result, dict):
205
- fail_branch(
206
- node_id,
207
- f"{node.display_name} must return a dictionary",
208
- )
209
- node._cancel_event = None
210
- continue
211
- complete(node_id, result)
212
- set_state(node_id, "finished")
213
- emit(f"Finished {node.display_name}: {result}")
214
- node._cancel_event = None
215
-
216
- if cancel_event.is_set():
217
- # A running cooperative node may notice cancellation and finish before
218
- # the scheduler reaches the next loop iteration. Ensure descendants
219
- # that never entered the ready queue still receive a cancelled state.
220
- for node_id in graph.nodes:
221
- if node_id not in terminal:
222
- terminal.add(node_id)
223
- set_state(node_id, "cancelled")
224
- raise GraphExecutionCancelled("Graph execution cancelled")
225
- unresolved = set(graph.nodes) - terminal
226
- if unresolved:
227
- failures.append("The graph contains a cycle and cannot be executed")
228
- for node_id in unresolved:
229
- set_state(node_id, "blocked")
230
- if failures:
231
- raise GraphExecutionError("\n".join(failures))
232
- return ExecutionResult(order, outputs)
File without changes