-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.py
More file actions
656 lines (592 loc) · 28.4 KB
/
Copy pathbuilder.py
File metadata and controls
656 lines (592 loc) · 28.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
"""Graph builder: mutable construction → compile to immutable `CompiledGraph`.
Compilation MUST fail if the graph has no declared entry, unreachable
nodes, dangling edges, a node with more than one outgoing edge, or a
field with more than one declared reducer.
`GraphBuilder[StateT]` is parameterized on the graph's state type. Node
functions, conditional-edge functions, and the returned `CompiledGraph[StateT]`
all carry `StateT` forward so consumers get typed `invoke()` return values and
a type-checked `state` parameter on every callback, without `cast(...)` calls.
"""
from collections.abc import Awaitable, Callable, Iterable, Mapping
from types import GenericAlias, UnionType
from typing import Any, Literal, Self, cast, get_args, get_origin
from openarmature.checkpoint.errors import CheckpointStateMigrationChainAmbiguous
from openarmature.checkpoint.migration import MigrationRegistry, StateMigration
from openarmature.checkpoint.protocol import Checkpointer
from .compiled import CompiledGraph
from .edges import ConditionalEdge, EndSentinel, StaticEdge
from .errors import (
ConflictingReducers,
DanglingEdge,
FanOutCountModeAmbiguous,
FanOutDegradedUpdateMissingCollectField,
FanOutFieldNotList,
MappingReferencesUndeclaredField,
MultipleOutgoingEdges,
NoDeclaredEntry,
ParallelBranchesNoBranches,
UnreachableNode,
)
from .fan_out import ConcurrencyResolver, CountResolver, FanOutConfig, FanOutNode
from .middleware import FailureIsolationMiddleware, Middleware
from .nodes import FunctionNode, Node
from .parallel_branches import BranchSpec, ParallelBranchesNode
from .projection import FieldNameMatching, ProjectionStrategy
from .reducers import Reducer
from .state import State, field_reducers, resolve_reducer
from .subgraph import SubgraphNode
class GraphBuilder[StateT: State]:
"""Mutable builder for a graph; call `compile()` to produce a `CompiledGraph`."""
def __init__(self, state_cls: type[StateT]) -> None:
self.state_cls: type[StateT] = state_cls
self._nodes: dict[str, Node[StateT]] = {}
self._edges: list[StaticEdge | ConditionalEdge[StateT]] = []
self._entry: str | None = None
# Per-graph middleware in registration order (outer-to-inner).
# Composed OUTSIDE per-node middleware at runtime per spec §3.
self._middleware: list[Middleware] = []
# Optional Checkpointer attached at compile time; ``None`` is
# the spec §10.1.1 default-off behavior.
self._checkpointer: Checkpointer | None = None
# State-migration registry per pipeline-utilities §10.12
# (proposal 0014). Populated by ``with_state_migration(s)``;
# passed through to the compiled graph.
self._migration_registry: MigrationRegistry = MigrationRegistry()
def add_node(
self,
name: str,
fn: Callable[[StateT], Awaitable[Mapping[str, Any]]],
*,
middleware: Iterable[Middleware] | None = None,
) -> Self:
"""Register a function node.
``fn`` is an async callable taking the current state and
returning a partial update (a mapping from declared state-field
names to new values). The merge into parent state happens
through the field reducers; ``fn`` itself does not see or touch
the merge.
``middleware`` is an optional per-node middleware tuple,
outer-to-inner. Per-graph middleware (see
:meth:`add_middleware`) wraps these.
Raises ``ValueError`` if ``name`` is already declared on this
builder.
"""
if name in self._nodes:
raise ValueError(f"node {name!r} already declared")
self._nodes[name] = FunctionNode[StateT](
name=name,
fn=fn,
middleware=tuple(middleware) if middleware is not None else (),
)
return self
def add_subgraph_node[ChildT: State](
self,
name: str,
compiled: CompiledGraph[ChildT],
projection: ProjectionStrategy[StateT, ChildT] | None = None,
*,
middleware: Iterable[Middleware] | None = None,
) -> Self:
"""Register a subgraph as a node.
``compiled`` is a child :class:`CompiledGraph` (parameterized
on its own state type). On entry the parent's state is
translated to the child's via ``projection.project_in``; on
exit the child's final state is folded back via
``projection.project_out``. The default projection is
:class:`FieldNameMatching`; pass an :class:`ExplicitMapping`
for declarative ``inputs``/``outputs`` instead.
``middleware`` wraps the whole subgraph dispatch as one atomic
call from the parent's perspective. Per-graph middleware on
the parent does NOT propagate into the child; child middleware
is configured on the child's own builder.
Raises ``ValueError`` if ``name`` is already declared.
"""
if name in self._nodes:
raise ValueError(f"node {name!r} already declared")
proj: ProjectionStrategy[StateT, ChildT] = (
projection if projection is not None else FieldNameMatching[StateT, ChildT]()
)
self._nodes[name] = SubgraphNode[StateT, ChildT](
name=name,
compiled=compiled,
projection=proj,
middleware=tuple(middleware) if middleware is not None else (),
)
return self
def add_fan_out_node[ChildT: State](
self,
name: str,
*,
subgraph: CompiledGraph[ChildT],
collect_field: str,
target_field: str,
items_field: str | None = None,
item_field: str | None = None,
count: int | CountResolver | None = None,
concurrency: int | ConcurrencyResolver | None = 10,
error_policy: str = "fail_fast",
on_empty: str = "raise",
count_field: str | None = None,
inputs: Mapping[str, str] | None = None,
extra_outputs: Mapping[str, str] | None = None,
instance_middleware: Iterable[Middleware] | None = None,
errors_field: str | None = None,
middleware: Iterable[Middleware] | None = None,
subgraph_identity: str | None = None,
) -> Self:
"""Register a fan-out node.
Validates configuration at registration time:
- Exactly one of ``items_field`` or ``count`` MUST be
specified (``fan_out_count_mode_ambiguous`` otherwise).
- ``items_field`` MUST refer to a list-typed field on the
parent state schema (``fan_out_field_not_list`` otherwise).
- ``items_field`` mode requires ``item_field``; ``count``
mode forbids ``item_field``.
- ``on_empty`` and ``error_policy`` MUST be one of the
permitted string literals (``"raise"`` / ``"noop"`` and
``"fail_fast"`` / ``"collect"`` respectively).
- ``inputs`` / ``extra_outputs`` / ``count_field`` field
references go through the existing
``mapping_references_undeclared_field`` rule.
"""
if name in self._nodes:
raise ValueError(f"node {name!r} already declared")
# Mode validation: exactly one of items_field / count.
if (items_field is None) == (count is None):
raise FanOutCountModeAmbiguous(
node_name=name,
message=(
"must specify exactly one of items_field or count "
f"(got items_field={items_field!r}, count={count!r})"
),
)
if items_field is not None and item_field is None:
raise FanOutCountModeAmbiguous(node_name=name, message="items_field mode requires item_field")
if count is not None and item_field is not None:
raise FanOutCountModeAmbiguous(node_name=name, message="count mode forbids item_field")
# items_field must be a list-typed parent field.
if items_field is not None:
parent_fields = self.state_cls.model_fields
if items_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.items_field", side="parent", field_name=items_field
)
ann = parent_fields[items_field].annotation
if not _is_list_typed(ann):
raise FanOutFieldNotList(node_name=name, field_name=items_field)
# error_policy + on_empty literal validation.
if error_policy not in {"fail_fast", "collect"}:
raise ValueError(
f"fan-out node {name!r}: error_policy must be 'fail_fast' or 'collect', got {error_policy!r}"
)
if on_empty not in {"raise", "noop"}:
raise ValueError(f"fan-out node {name!r}: on_empty must be 'raise' or 'noop', got {on_empty!r}")
# *_field references must match declared fields.
parent_fields = self.state_cls.model_fields
sub_fields = subgraph.state_cls.model_fields
if target_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.target_field", side="parent", field_name=target_field
)
if collect_field not in sub_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.collect_field", side="subgraph", field_name=collect_field
)
# NOTE: item_field is intentionally NOT validated against declared
# subgraph fields. Per fixture 023, the spec allows item_field to
# name a field the subgraph doesn't declare (treated as a
# placeholder when the subgraph doesn't read the item). The
# runtime projection in fan_out._build_instance_states skips the
# assignment if the field isn't declared, so non-declared
# item_field values are effectively no-ops.
if count_field is not None and count_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.count_field", side="parent", field_name=count_field
)
if errors_field is not None and errors_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.errors_field", side="parent", field_name=errors_field
)
for sub_f, parent_f in (inputs or {}).items():
if sub_f not in sub_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.inputs", side="subgraph", field_name=sub_f
)
if parent_f not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.inputs", side="parent", field_name=parent_f
)
for parent_f, sub_f in (extra_outputs or {}).items():
if parent_f not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.extra_outputs", side="parent", field_name=parent_f
)
if sub_f not in sub_fields:
raise MappingReferencesUndeclaredField(
direction="fan_out.extra_outputs", side="subgraph", field_name=sub_f
)
# Materialize instance_middleware once so the degraded_update check
# below and the FanOutConfig build don't both consume a one-shot
# iterable.
instance_middleware = tuple(instance_middleware or ())
# §9.8: a degraded fan-out instance contributes its degraded_update
# as the instance result, so a static (mapping) degraded_update on an
# instance FailureIsolationMiddleware must cover collect_field. A
# callable degraded_update is exempt — its output isn't knowable at
# construction; an omitted collect_field yields a runtime null slot.
for mw in instance_middleware:
if not isinstance(mw, FailureIsolationMiddleware):
continue
degraded = mw.degraded_update
if isinstance(degraded, Mapping) and collect_field not in cast("Mapping[str, Any]", degraded):
raise FanOutDegradedUpdateMissingCollectField(node_name=name, collect_field=collect_field)
cfg = FanOutConfig(
subgraph=subgraph,
collect_field=collect_field,
target_field=target_field,
items_field=items_field,
item_field=item_field,
count=count,
concurrency=concurrency,
error_policy=cast(Any, error_policy),
on_empty=cast(Any, on_empty),
count_field=count_field,
inputs=dict(inputs or {}),
extra_outputs=dict(extra_outputs or {}),
instance_middleware=tuple(instance_middleware or ()),
errors_field=errors_field,
subgraph_identity=subgraph_identity,
)
# FanOutNode satisfies the Node[StateT] structural protocol (run
# returns a partial update; name and middleware are present),
# but pyright loses the StateT correspondence through the second
# type parameter — cast restores it for the dict assignment.
fan_out: Node[StateT] = cast(
"Node[StateT]",
FanOutNode[StateT, ChildT](
name=name,
config=cfg,
middleware=tuple(middleware) if middleware is not None else (),
),
)
self._nodes[name] = fan_out
return self
def add_parallel_branches_node(
self,
name: str,
*,
branches: Mapping[str, BranchSpec[Any]],
error_policy: Literal["fail_fast", "collect"] = "fail_fast",
errors_field: str | None = None,
middleware: Iterable[Middleware] | None = None,
) -> Self:
"""Register a parallel-branches node.
``branches`` is a mapping from non-empty branch name to a
:class:`BranchSpec`. Insertion order is preserved and is
the dispatch + merge order.
Validates at registration:
- ``branches`` non-empty (raises ``ParallelBranchesNoBranches``).
- Each branch name is a non-empty string (raises ``ValueError``).
- Each branch's ``inputs`` / ``outputs`` refer only to declared
fields on the (parent, branch-subgraph) state schemas
(raises ``MappingReferencesUndeclaredField``).
- ``errors_field`` (when set) is a declared parent-state field.
"""
if name in self._nodes:
raise ValueError(f"node {name!r} already declared")
if not branches:
raise ParallelBranchesNoBranches(node_name=name)
parent_fields = self.state_cls.model_fields
if errors_field is not None and errors_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction="parallel_branches.errors_field",
side="parent",
field_name=errors_field,
)
for branch_name, spec in branches.items():
if not branch_name:
raise ValueError(f"parallel-branches node {name!r}: branch_name MUST be non-empty")
sub_fields = spec.subgraph.state_cls.model_fields
for sub_field, parent_field in spec.inputs.items():
if sub_field not in sub_fields:
raise MappingReferencesUndeclaredField(
direction=f"parallel_branches.{branch_name}.inputs",
side="subgraph",
field_name=sub_field,
)
if parent_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction=f"parallel_branches.{branch_name}.inputs",
side="parent",
field_name=parent_field,
)
for parent_field, sub_field in spec.outputs.items():
if parent_field not in parent_fields:
raise MappingReferencesUndeclaredField(
direction=f"parallel_branches.{branch_name}.outputs",
side="parent",
field_name=parent_field,
)
if sub_field not in sub_fields:
raise MappingReferencesUndeclaredField(
direction=f"parallel_branches.{branch_name}.outputs",
side="subgraph",
field_name=sub_field,
)
pb: Node[StateT] = cast(
"Node[StateT]",
ParallelBranchesNode[StateT](
name=name,
branches=dict(branches),
error_policy=error_policy,
errors_field=errors_field,
middleware=tuple(middleware) if middleware is not None else (),
),
)
self._nodes[name] = pb
return self
def with_checkpointer(self, checkpointer: Checkpointer) -> Self:
"""Register a Checkpointer for the compiled graph.
At most one Checkpointer per graph; calling
``with_checkpointer`` again replaces the previously-stored
one. Pass the result of :meth:`compile` to
:meth:`CompiledGraph.invoke` as usual; the engine fires saves
at every ``completed`` event for outermost-graph and
subgraph-internal nodes.
"""
self._checkpointer = checkpointer
return self
def with_state_migration(
self,
from_version: str,
to_version: str,
migrate: Callable[[Any], Any],
) -> Self:
"""Register one state migration.
On resume, when the saved record's ``schema_version`` does not
match the current state class's ``schema_version``, the engine
consults the registry for a chain that bridges the two and
applies it to the record's state (and to each entry in
``parent_states``) before deserialization.
Migrations MUST be pure: deterministic, no I/O, no implicit
state. The framework does not police purity, but violating it
risks non-deterministic resume.
Raises ``CheckpointStateMigrationChainAmbiguous`` at
registration if the ``(from_version, to_version)`` pair is
already registered. Also raises ``ValueError`` if
``to_version`` is the empty-string sentinel (the un-declared
marker is not a valid chain target).
"""
self._migration_registry.register(
StateMigration(
from_version=from_version,
to_version=to_version,
migrate=migrate,
)
)
return self
def with_state_migrations(self, *migrations: StateMigration) -> Self:
"""Register multiple migrations in one call. Convenience over
``with_state_migration``; each entry is registered through the
same path and obeys the same ambiguity rule.
Pre-validates the full input list against the existing
registry + against earlier entries in the same call before
mutating, so a duplicate in the third entry cannot leave
the first two half-registered. If any duplicate ``(from,
to)`` pair is detected the call raises
``CheckpointStateMigrationChainAmbiguous`` without mutating
the registry; otherwise all entries register atomically.
"""
# Pre-validation pass: collect every (from, to) we're about
# to add, check both against the existing registry and
# against earlier entries in the input. Raise before
# mutating if anything collides.
seen_in_call: set[tuple[str, str]] = set()
for m in migrations:
key = (m.from_version, m.to_version)
if key in self._migration_registry._migrations: # noqa: SLF001
raise CheckpointStateMigrationChainAmbiguous(
f"duplicate state migration {m.from_version!r}→{m.to_version!r} already registered",
from_version=m.from_version,
to_version=m.to_version,
)
if key in seen_in_call:
raise CheckpointStateMigrationChainAmbiguous(
f"duplicate state migration {m.from_version!r}→"
f"{m.to_version!r} repeated in with_state_migrations call",
from_version=m.from_version,
to_version=m.to_version,
)
seen_in_call.add(key)
# Validation passed — commit them all.
for migration in migrations:
self._migration_registry.register(migration)
return self
def add_middleware(self, middleware: Middleware) -> Self:
"""Register a per-graph middleware applied to every node in this graph.
Per-graph middleware composes OUTSIDE per-node middleware.
Calling order is preserved (outer-to-inner); earlier
``add_middleware`` calls produce outer layers in the runtime
chain.
"""
self._middleware.append(middleware)
return self
def add_edge(self, source: str, target: str | EndSentinel) -> Self:
"""Register a static edge from ``source`` to ``target``.
``target`` is either the name of another declared node or the
:data:`END` sentinel. A node may have at most one outgoing
static edge; declaring two raises
:class:`MultipleOutgoingEdges` at ``compile()``. For
state-dependent branching use :meth:`add_conditional_edge`.
"""
self._edges.append(StaticEdge(source=source, target=target))
return self
def add_conditional_edge(
self,
source: str,
fn: Callable[[StateT], str | EndSentinel],
) -> Self:
"""Register a conditional edge from ``source``.
``fn`` is a synchronous callable that receives the merged
post-node state and returns either the name of the next node
or the :data:`END` sentinel. Returning any other value raises
:class:`RoutingError` at runtime; an exception inside ``fn``
becomes an :class:`EdgeException`.
Like static edges, a node has at most one outgoing edge;
declaring both a static and a conditional edge from the same
source raises :class:`MultipleOutgoingEdges` at ``compile()``.
"""
self._edges.append(ConditionalEdge[StateT](source=source, fn=fn))
return self
def set_entry(self, name: str) -> Self:
"""Declare which registered node the graph starts at.
Calling ``set_entry`` again replaces the previously set entry.
``compile()`` raises :class:`NoDeclaredEntry` if no entry was
set, and :class:`DanglingEdge` if the entry name was never
declared with ``add_*_node``.
"""
self._entry = name
return self
def compile(self) -> CompiledGraph[StateT]:
"""Validate the builder and return an immutable
:class:`CompiledGraph`.
Runs structural checks in order:
- ``conflicting_reducers`` (state-field reducer conflicts)
- declarative projection ``validate`` hooks (e.g.
:class:`ExplicitMapping`)
- ``no_declared_entry`` / ``dangling_edge`` (entry pointer)
- ``dangling_edge`` (edge endpoints reference declared nodes)
- ``multiple_outgoing_edges`` (one edge per source)
- ``unreachable_node`` (every node reachable from entry)
The first failing check raises its specific
:class:`CompileError` subclass; passing means the returned
graph is ready for :meth:`CompiledGraph.invoke`. A previously
attached :class:`Checkpointer` is forwarded onto the compiled
graph.
"""
# 1. ConflictingReducers — state schema check.
per_field = field_reducers(self.state_cls)
for fname, declared in per_field.items():
if len(declared) > 1:
raise ConflictingReducers(fname)
resolved: dict[str, Reducer] = {
fname: resolve_reducer(declared) for fname, declared in per_field.items()
}
# 2. MappingReferencesUndeclaredField — declarative projection
# strategies (e.g. `ExplicitMapping`) expose an optional
# `validate(parent_cls, child_cls)` hook that we invoke here so
# misconfigured mappings fail compile rather than at runtime.
# The hook is duck-typed: strategies with nothing declarative to
# check (the default `FieldNameMatching`, hand-written imperative
# projections) simply omit `validate` and the engine skips it.
# ChildT is erased once SubgraphNode is stored as Node[StateT];
# the cast restores enough type info to access `compiled.state_cls`
# without pyright flagging an unknown member type.
for node in self._nodes.values():
if isinstance(node, SubgraphNode):
sub = cast(SubgraphNode[StateT, State], node)
validate = getattr(sub.projection, "validate", None)
if validate is not None:
validate(self.state_cls, sub.compiled.state_cls)
# 3. NoDeclaredEntry.
if self._entry is None:
raise NoDeclaredEntry()
# 4. Entry must point to a declared node (treat as DanglingEdge).
if self._entry not in self._nodes:
raise DanglingEdge(source="<entry>", target=self._entry)
# 5. DanglingEdge — both endpoints of every edge must be declared.
for edge in self._edges:
if edge.source not in self._nodes:
raise DanglingEdge(source=edge.source, target=edge.source)
if isinstance(edge, StaticEdge) and isinstance(edge.target, str):
if edge.target not in self._nodes:
raise DanglingEdge(source=edge.source, target=edge.target)
# 6. MultipleOutgoingEdges + index by source for the reachability pass.
edges_by_source: dict[str, StaticEdge | ConditionalEdge[StateT]] = {}
for edge in self._edges:
if edge.source in edges_by_source:
raise MultipleOutgoingEdges(edge.source)
edges_by_source[edge.source] = edge
# 7. UnreachableNode — BFS from entry. Conditional edges over-approximate
# by reaching every declared node (we cannot statically know the fn's
# range), which keeps the check sound (no false positives).
reachable = self._reachable_nodes(edges_by_source)
for node_name in self._nodes:
if node_name not in reachable:
raise UnreachableNode(node_name)
compiled = CompiledGraph[StateT](
state_cls=self.state_cls,
entry=self._entry,
nodes=dict(self._nodes),
edges=edges_by_source,
reducers=resolved,
middleware=tuple(self._middleware),
migration_registry=self._migration_registry,
)
if self._checkpointer is not None:
compiled.attach_checkpointer(self._checkpointer)
return compiled
def _reachable_nodes(
self,
edges_by_source: Mapping[str, StaticEdge | ConditionalEdge[StateT]],
) -> set[str]:
assert self._entry is not None
reachable: set[str] = {self._entry}
frontier = [self._entry]
all_names = set(self._nodes.keys())
while frontier:
current = frontier.pop()
edge = edges_by_source.get(current)
if edge is None:
continue
if isinstance(edge, StaticEdge):
if isinstance(edge.target, str) and edge.target not in reachable:
reachable.add(edge.target)
frontier.append(edge.target)
else:
for name in all_names - reachable:
reachable.add(name)
frontier.append(name)
return reachable
def _is_list_typed(annotation: Any) -> bool:
"""True if ``annotation`` resolves to a ``list[...]`` shape.
Used by ``add_fan_out_node`` to validate that ``items_field`` refers
to a list-typed parent field. Handles both bare ``list[X]`` and
``Annotated[list[X], reducer]`` forms (the latter is how state
fields commonly attach an `append` reducer).
"""
if annotation is list:
return True
origin = get_origin(annotation)
if origin is list:
return True
# Annotated[T, ...] — peel the metadata, recurse on the type.
if isinstance(annotation, GenericAlias):
return False
args = get_args(annotation)
if args and origin is None:
# Likely Annotated; first arg is the underlying type.
return _is_list_typed(args[0])
if isinstance(annotation, UnionType):
return any(_is_list_typed(a) for a in args)
return False