Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.
feature/Dry-merge-leaves-wrapper · L-261005-5a39fe (#1350)
Louis Choquelf355952fb248
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 65dea64ad..dc86e12b4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,10 +1,6 @@# Changelog-## [v0.74.1] - 2026-10-05--### Added--- **`PIPELEX_HOME` relocates the home configuration directory**: when set and not empty, it names the directory Pipelex uses in place of `~/.pipelex`, for the configuration layers, the inference files and their overrides, the credentials `.env`, the first-boot copy of the kit, `pipelex init`, `pipelex doctor`, `pipelex update`, `pipelex migrate` and the agent CLI's `--global`. It must be in the process environment before Pipelex is imported, a relative value is resolved against the working directory at import, and a `PIPELEX_HOME` line in a `.env` file is ignored. The first boot now fills the home directory from the kit when it exists but is empty, as it already did when it was missing, so an empty volume mount or a `mktemp -d` works as it is.+## [Unreleased]### Changed@@ -12,7 +8,13 @@### Fixed-- **A dry-run condition's output is one stuff every outcome produces**: in the graph of a dry run, every outcome of a `PipeCondition` now carries the condition's output digest, so the step reading the condition's result is wired to all of them rather than to the outcome whose pipe code sorts last, and the condition's own item takes its declared output concept when the outcomes write or declare different ones. Each outcome but the last now runs on a copy of the memory the condition received, so an outcome reading the condition's slot reads the value from before the condition rather than a sibling's output, and the default outcome runs last, so the steps after the condition read its value.+- **A dry-run condition's output is one stuff every outcome produces**: in the graph of a dry run, every outcome of a `PipeCondition` now carries the condition's output digest, so the step reading the condition's result is wired to all of them rather than to the outcome whose pipe code sorts last, and when the outcomes write or declare different outputs, the condition's declared output types that stuff on every node outside the outcomes: the condition, the controllers whose output it is, such as a sequence ending on the condition, and the steps reading it, while each outcome keeps the concept it writes. Each outcome but the last now runs on a copy of the memory the condition received, so an outcome reading the condition's slot reads the value from before the condition rather than a sibling's output, and the default outcome runs last, so the steps after the condition read its value.++## [v0.74.1] - 2026-10-05++### Added++- **`PIPELEX_HOME` relocates the home configuration directory**: when set and not empty, it names the directory Pipelex uses in place of `~/.pipelex`, for the configuration layers, the inference files and their overrides, the credentials `.env`, the first-boot copy of the kit, `pipelex init`, `pipelex doctor`, `pipelex update`, `pipelex migrate` and the agent CLI's `--global`. It must be in the process environment before Pipelex is imported, a relative value is resolved against the working directory at import, and a `PIPELEX_HOME` line in a `.env` file is ignored. The first boot now fills the home directory from the kit when it exists but is empty, as it already did when it was missing, so an empty volume mount or a `mktemp -d` works as it is.## [v0.74.0] - 2026-10-05diff --git a/docs/under-the-hood/execution-graph-tracing.md b/docs/under-the-hood/execution-graph-tracing.mdindex f85bbbb82..86b366e67 100644--- a/docs/under-the-hood/execution-graph-tracing.md+++ b/docs/under-the-hood/execution-graph-tracing.md@@ -338,7 +338,9 @@ The condition runs every outcome but the last on a copy of the working memory anOnce every outcome has run, the condition registers a `ConditionOutputMerge` (`pipelex/graph/condition_output_merge.py`) through the tracer's `register_condition_output_merge`, which the event log carries as a `ConditionOutputMergeEvent`. It names the condition's node, its output digest and the digests the other outcomes minted for the slot. Only a stuff minted inside the condition is merged: an outcome that hands back a stuff it was given keeps its digest, and nothing is merged when the last outcome resolves its slot absent. Both graph builders apply every merge to the finished graph through `apply_condition_output_merges`, which moves each merged digest onto the condition's, transitively for a condition run as another condition's outcome, across every io item and every edge's stuff digests. The last step of an outcome sequence and the output list of an outcome batch therefore move with their outcome.-Each outcome's io item keeps its own concept and multiplicity, so its node still says what that outcome writes. The condition's own output item types the shared stuff: when the merged outcomes write different concepts or multiplicities, or declare different ones, the merge carries the condition's declared output concept, a list when the declaration or the invocation's `nb_output` or `multiple_output` makes it one, and the rewrite retypes that item with it. The declarations count because an outcome's stuff can stand for several: an outcome that is itself a condition hands back only its last outcome's stuff, while its declaration covers every one of them. A list is recorded as `True`, never as an item count, as on every other io item. A condition run as a `PipeBatch`'s branch is given the branch's stuff code, which every outcome mints under, so there is no digest to merge; when its outcomes still write or declare different outputs, the condition records a merge with no merged digest and the typing alone.+Each outcome's io item keeps its own concept and multiplicity, so its node still says what that outcome writes. When the merged outcomes write different concepts or multiplicities, or declare different ones, the merge also carries the condition's declared output concept, a list when the declaration or the invocation's `nb_output` or `multiple_output` makes it one. The declarations count because an outcome's stuff can stand for several: an outcome that is itself a condition hands back only its last outcome's stuff, while its declaration covers every one of them. A list is recorded as `True`, never as an item count, as on every other io item. A condition run as a `PipeBatch`'s branch is given the branch's stuff code, which every outcome mints under, so there is no digest to merge; when its outcomes still write or declare different outputs, the condition records a merge with no merged digest and the typing alone.++The rewrite gives that typing to every io item on the shared stuff whose node is not inside the condition's outcomes, read off the finished graph's `CONTAINS` edges: the condition's own item, every controller whose output is the condition's, such as a sequence ending on the condition or a batch branch's sequence, and every step reading the stuff. A consumer typing a stuff from the first item naming its digest, which is an enclosing controller's, therefore reads the declaration rather than the concept of whichever outcome ran last. When several typed conditions are merged onto one stuff, an item takes the typing of the outermost one whose outcomes it is not inside, and a node inside another condition's outcomes takes a typing only when it is that typed condition or encloses it: an outer outcome sequence ending on an inner condition carries the inner typing, while the inner condition's sibling outcome keeps its own concept.An outcome that is a `PipeParallel` or a `PipeBatch` writes the shared stuff as a controller, combining its branches or aggregating its items into it, so it is no producer of the stuff. `GraphAnalysis` still counts it as one of the stuff's writers, so the stuff belongs to the condition all the same.diff --git a/pipelex/graph/condition_output_merge.py b/pipelex/graph/condition_output_merge.pyindex eef7c6e2d..7af6072e9 100644--- a/pipelex/graph/condition_output_merge.py+++ b/pipelex/graph/condition_output_merge.py@@ -5,7 +5,10 @@ take, and each outcome writes its own stuff into the condition's one slot. The scondition reads the last outcome's stuff, which is the condition's own output. To draw thecondition's result as one stuff that every outcome produces, the condition records which digestsits outcomes minted, and the graph builders move them onto its output digest once the graph is-built. One function for both builders (the in-process `GraphTracer` and the event-replay+built. When the outcomes disagree on what they write, the condition also records the typing that+covers them all, and every item carrying the shared stuff outside the outcomes takes it: the+condition's own item, the controllers enclosing the condition whose output it is, and the steps+reading it. One function for both builders (the in-process `GraphTracer` and the event-replay`GraphSpecAssembler`) so the two cannot drift."""@@ -13,7 +16,7 @@ from collections.abc import Sequencefrom pydantic import BaseModel, ConfigDict, Field-from pipelex.graph.graphspec import EdgeSpec, GraphSpec, IOSpec, NodeSpec+from pipelex.graph.graphspec import EdgeKind, EdgeSpec, GraphSpec, IOSpec, NodeSpecclass ConditionOutputTyping(BaseModel):@@ -37,9 +40,10 @@ class ConditionOutputMerge(BaseModel):condition_node_id: The condition's graph node.shared_digest: The condition's output digest, which is the last outcome's stuff.merged_digests: The digests the other outcomes minted for the same slot.- shared_typing: The typing of the condition's own output item, set when the outcomes write or- declare different concepts or multiplicities, so that the shared stuff carries the- declaration that covers every outcome rather than whichever outcome ran last.+ shared_typing: The typing of every item carrying the shared stuff outside the condition's+ outcomes, set when the outcomes write or declare different concepts or multiplicities,+ so that the shared stuff carries the declaration that covers every outcome rather than+ whichever outcome ran last."""model_config = ConfigDict(extra="forbid", strict=True)@@ -55,8 +59,13 @@ def apply_condition_output_merges(*, graph: GraphSpec, merges: Sequence[ConditioA merged digest is resolved transitively, so a condition run as another condition's outcomelands on the outer condition's digest. Each io item keeps its own concept, multiplicity and- data, except the condition's own output item when its merge carries a typing. Applied once the- graph is built, the rewrite does not depend on the order the merges were recorded in.+ data, except the items a typed condition covers, which take its typing: on the stuff the+ condition's output resolves to, its own item and every item on a node outside every condition+ merged onto that stuff (the controllers enclosing it, the steps reading it), and, inside+ another condition's outcomes, the items on the nodes enclosing it. An item several typed+ conditions cover takes the outermost one's typing, the earlier in node order at equal depth.+ Applied once the graph is built, the rewrite does not depend on the order the merges were+ recorded in."""if not merges:return graph@@ -66,19 +75,94 @@ def apply_condition_output_merges(*, graph: GraphSpec, merges: Sequence[Conditiofor merged_digest in merge.merged_digests:if merged_digest != merge.shared_digest:renames[merged_digest] = merge.shared_digest- # A typing names the condition's item by the digest it carried when the merge was recorded.- typings: dict[tuple[str, str], ConditionOutputTyping] = {- (merge.condition_node_id, merge.shared_digest): merge.shared_typing for merge in merges if merge.shared_typing is not None- }++ shared_typings = _SharedTypings.build(graph=graph, merges=merges, renames=renames)return graph.model_copy(update={- "nodes": [_rewrite_node(node=node, renames=renames, typings=typings) for node in graph.nodes],+ "nodes": [_rewrite_node(node=node, renames=renames, shared_typings=shared_typings) for node in graph.nodes],"edges": [_rewrite_edge(edge=edge, renames=renames) for edge in graph.edges],})+class _TypingScope(BaseModel):+ """A typed condition's place in the containment tree, and the typing it gives."""++ model_config = ConfigDict(extra="forbid", strict=True, frozen=True)++ condition_node_id: str+ condition_ancestors: frozenset[str]+ rank: tuple[int, int]+ typing: ConditionOutputTyping+++class _SharedTypings:+ """Which typed condition, if any, covers an io item, read off the finished graph's containment tree."""++ def __init__(self, *, parents: dict[str, str], conditions_by_digest: dict[str, set[str]], scopes_by_digest: dict[str, list[_TypingScope]]):+ self._parents = parents+ # Every condition merged onto a stuff, typed or not, bounds the outcomes its typing does not reach into.+ self._conditions_by_digest = conditions_by_digest+ self._scopes_by_digest = scopes_by_digest++ @classmethod+ def build(cls, *, graph: GraphSpec, merges: Sequence[ConditionOutputMerge], renames: dict[str, str]) -> "_SharedTypings":+ parents = {edge.target: edge.source for edge in graph.edges if edge.kind == EdgeKind.CONTAINS}+ node_order = {node.node_id: index for index, node in enumerate(graph.nodes)}+ conditions_by_digest: dict[str, set[str]] = {}+ scopes_by_digest: dict[str, list[_TypingScope]] = {}+ for merge in merges:+ digest = _resolve_digest(digest=merge.shared_digest, renames=renames)+ if digest is None:+ continue+ conditions_by_digest.setdefault(digest, set()).add(merge.condition_node_id)+ if merge.shared_typing is None:+ continue+ condition_ancestors = _ancestors(node_id=merge.condition_node_id, parents=parents)+ scopes_by_digest.setdefault(digest, []).append(+ _TypingScope(+ condition_node_id=merge.condition_node_id,+ condition_ancestors=frozenset(condition_ancestors),+ rank=(len(condition_ancestors), node_order.get(merge.condition_node_id, len(node_order))),+ typing=merge.shared_typing,+ )+ )+ return cls(parents=parents, conditions_by_digest=conditions_by_digest, scopes_by_digest=scopes_by_digest)++ def typing_for(self, *, node_id: str, digest: str | None) -> ConditionOutputTyping | None:+ """The typing of the outermost typed condition covering `node_id`'s item on `digest`, if any.++ A typed condition covers an item on the stuff its output resolves to when the item's node is+ not inside its outcomes. A node inside another condition's outcomes on that stuff is covered+ only when it is the typed condition or encloses it, as an outcome sequence ending on a nested+ condition does: a sibling outcome writes its own value, which no typing describes better.+ """+ if digest is None or digest not in self._scopes_by_digest:+ return None+ node_ancestors = set(_ancestors(node_id=node_id, parents=self._parents))+ inside_an_outcome = not node_ancestors.isdisjoint(self._conditions_by_digest[digest])+ covering = [+ scope+ for scope in self._scopes_by_digest[digest]+ if scope.condition_node_id not in node_ancestors+ and (not inside_an_outcome or scope.condition_node_id == node_id or node_id in scope.condition_ancestors)+ ]+ if not covering:+ return None+ return min(covering, key=lambda scope: scope.rank).typing+++def _ancestors(*, node_id: str, parents: dict[str, str]) -> list[str]:+ """The controllers containing `node_id`, innermost first, stopping on a cycle."""+ ancestors: list[str] = []+ current = parents.get(node_id)+ while current is not None and current != node_id and current not in ancestors:+ ancestors.append(current)+ current = parents.get(current)+ return ancestors++def _resolve_digest(*, digest: str | None, renames: dict[str, str]) -> str | None:"""Follow the renames from `digest` to the digest it finally lands on."""visited: set[str] = set()@@ -89,20 +173,19 @@ def _resolve_digest(*, digest: str | None, renames: dict[str, str]) -> str | Nonreturn current-def _rewrite_item(*, item: IOSpec, renames: dict[str, str], typing: ConditionOutputTyping | None) -> IOSpec:- update: dict[str, object] = {"digest": _resolve_digest(digest=item.digest, renames=renames)}+def _rewrite_item(*, item: IOSpec, node_id: str, renames: dict[str, str], shared_typings: _SharedTypings) -> IOSpec:+ digest = _resolve_digest(digest=item.digest, renames=renames)+ update: dict[str, object] = {"digest": digest}+ typing = shared_typings.typing_for(node_id=node_id, digest=digest)if typing is not None:update["concept"] = typing.conceptupdate["multiplicity"] = typing.multiplicityreturn item.model_copy(update=update)-def _rewrite_node(*, node: NodeSpec, renames: dict[str, str], typings: dict[tuple[str, str], ConditionOutputTyping]) -> NodeSpec:- inputs = [_rewrite_item(item=item, renames=renames, typing=None) for item in node.node_io.inputs]- outputs = [- _rewrite_item(item=item, renames=renames, typing=typings.get((node.node_id, item.digest)) if item.digest is not None else None)- for item in node.node_io.outputs- ]+def _rewrite_node(*, node: NodeSpec, renames: dict[str, str], shared_typings: _SharedTypings) -> NodeSpec:+ inputs = [_rewrite_item(item=item, node_id=node.node_id, renames=renames, shared_typings=shared_typings) for item in node.node_io.inputs]+ outputs = [_rewrite_item(item=item, node_id=node.node_id, renames=renames, shared_typings=shared_typings) for item in node.node_io.outputs]return node.model_copy(update={"node_io": node.node_io.model_copy(update={"inputs": inputs, "outputs": outputs})})diff --git a/tests/integration/pipelex/pipeline/dry_run_condition/graph_reading.py b/tests/integration/pipelex/pipeline/dry_run_condition/graph_reading.pyindex 1401cb408..67d4a4030 100644--- a/tests/integration/pipelex/pipeline/dry_run_condition/graph_reading.py+++ b/tests/integration/pipelex/pipeline/dry_run_condition/graph_reading.py@@ -32,3 +32,15 @@ def output_named(node: NodeSpec, name: str) -> IOSpec:matches = [output_spec for output_spec in node.node_io.outputs if output_spec.name == name]assert len(matches) == 1, f"expected one output '{name}' on '{node.pipe_code}', found {node.node_io.outputs}"return matches[0]+++def items_on(graph_spec: GraphSpec, digest: str) -> list[tuple[NodeSpec, IOSpec]]:+ """Every io item naming `digest`, in node order, each node's outputs before its inputs."""+ return [(node, io_spec) for node in graph_spec.nodes for io_spec in [*node.node_io.outputs, *node.node_io.inputs] if io_spec.digest == digest]+++def first_item_on(graph_spec: GraphSpec, digest: str) -> IOSpec:+ """The item a reader taking a stuff's typing from its first mention reads, as mthds-ui's renderer does."""+ items = items_on(graph_spec, digest)+ assert items, f"no io item names the digest '{digest}'"+ return items[0][1]diff --git a/tests/integration/pipelex/pipeline/dry_run_condition/test_data.py b/tests/integration/pipelex/pipeline/dry_run_condition/test_data.pyindex 6c3e681e8..946e8a0e8 100644--- a/tests/integration/pipelex/pipeline/dry_run_condition/test_data.py+++ b/tests/integration/pipelex/pipeline/dry_run_condition/test_data.py@@ -519,4 +519,122 @@ description = "Assemble the follow-ups"inputs = { follow_ups = "Anything[]" }output = "Text"prompt = "Assemble $follow_ups"+"""++ # The main sequence ends on the condition, so the sequence's output is the condition's shared+ # stuff, and the sequence comes before the condition in node order: a reader typing a stuff by+ # its first item would see the sequence's item first.+ WRAPPED_CONDITION_MTHDS: ClassVar[str] = """+domain = "dry_condition_wrapped"+description = "A sequence whose last step is a condition with differing outcomes"+main_pipe = "screen_candidate"++[concept.InterviewQuestion]+description = "An interview question"+refines = "Text"++[concept.Email]+description = "An email"+refines = "Text"++[pipe.screen_candidate]+type = "PipeSequence"+description = "Judge a CV, then write questions or a refusal"+inputs = { cv = "Text" }+output = "Anything"+steps = [+ { pipe = "judge", result = "verdict" },+ { pipe = "route_on_match", result = "decision_output" },+]++[pipe.judge]+type = "PipeLLM"+description = "Judge the CV"+inputs = { cv = "Text" }+output = "Text"+prompt = "Judge $cv"++[pipe.route_on_match]+type = "PipeCondition"+description = "Write questions or a refusal on the verdict"+inputs = { verdict = "Text", cv = "Text" }+output = "Anything"+expression = "verdict"+outcomes = { fit = "generate_interview_questions", no_fit = "write_refusal_email" }+default_outcome = "fail"++[pipe.generate_interview_questions]+type = "PipeLLM"+description = "Write interview questions"+inputs = { cv = "Text" }+output = "InterviewQuestion[]"+prompt = "Questions for $cv"++[pipe.write_refusal_email]+type = "PipeLLM"+description = "Write a refusal"+inputs = { cv = "Text" }+output = "Email"+prompt = "Refusal for $cv"+"""++ # A sequence step batched over a list runs a branch sequence per item, each ending on its own run+ # of the condition, so each branch sequence's output is that run's shared stuff.+ BATCH_BRANCH_SEQUENCE_MTHDS: ClassVar[str] = """+domain = "dry_condition_batch_branch_sequence"+description = "A batch whose branch is a sequence ending on a condition with differing outcomes"+main_pipe = "screen_cvs"++[concept.InterviewQuestion]+description = "An interview question"+refines = "Text"++[pipe.screen_cvs]+type = "PipeSequence"+description = "Screen every CV"+inputs = { cvs = "Text[]" }+output = "Anything[]"+steps = [+ { pipe = "screen_single_cv", batch_over = "cvs", batch_as = "cv", result = "results" },+]++[pipe.screen_single_cv]+type = "PipeSequence"+description = "Judge one CV, then write questions or a refusal"+inputs = { cv = "Text" }+output = "Anything"+steps = [+ { pipe = "judge", result = "verdict" },+ { pipe = "route_by_match", result = "output" },+]++[pipe.judge]+type = "PipeLLM"+description = "Judge the CV"+inputs = { cv = "Text" }+output = "Text"+prompt = "Judge $cv"++[pipe.route_by_match]+type = "PipeCondition"+description = "Write questions or a refusal on the verdict"+inputs = { verdict = "Text", cv = "Text" }+output = "Anything"+expression = "verdict"+outcomes = { yes = "generate_interview_questions", no = "write_refusal_email" }+default_outcome = "fail"++[pipe.generate_interview_questions]+type = "PipeLLM"+description = "Write interview questions"+inputs = { cv = "Text" }+output = "InterviewQuestion[]"+prompt = "Questions for $cv"++[pipe.write_refusal_email]+type = "PipeLLM"+description = "Write a refusal"+inputs = { cv = "Text" }+output = "Text"+prompt = "Refusal for $cv""""diff --git a/tests/integration/pipelex/pipeline/dry_run_condition/test_shared_output.py b/tests/integration/pipelex/pipeline/dry_run_condition/test_shared_output.pyindex 87d15316c..1d820cd8c 100644--- a/tests/integration/pipelex/pipeline/dry_run_condition/test_shared_output.py+++ b/tests/integration/pipelex/pipeline/dry_run_condition/test_shared_output.py@@ -19,7 +19,9 @@ from pipelex.graph.mermaidflow.mermaidflow_utils import make_stuff_idfrom pipelex.pipeline.dry_run_pipeline import dry_run_pipelinefrom pipelex.tools.mermaid.mermaid_utils import sanitize_mermaid_idfrom tests.integration.pipelex.pipeline.dry_run_condition.graph_reading import (+ first_item_on,input_named,+ items_on,node_by_code,nodes_by_code,only_output,@@ -136,6 +138,10 @@ class TestDryRunConditionSharedOutput:assert only_output(node_by_code(graph_spec, "score_cv")).concept == "Number"assert only_output(node_by_code(graph_spec, "outer")).concept == "Anything"+ assert only_output(node_by_code(graph_spec, "inner")).concept == "Anything"+ # `write_note` is the outer condition's outcome, beside `inner`, so it keeps what it writes.+ assert only_output(node_by_code(graph_spec, "write_note")).concept == "Text"+ assert only_output(node_by_code(graph_spec, "summarize_cv")).concept == "Text"analysis = GraphAnalysis.from_graphspec(graph_spec)assert analysis.shared_stuff_controllers == {shared_digest: node_by_code(graph_spec, "outer").node_id}assert analysis.stuff_registry[shared_digest].concept == "Anything"@@ -176,3 +182,34 @@ class TestDryRunConditionSharedOutput:mermaidflow = MermaidflowFactory.make_from_graphspec(graph_spec, graph_config=make_graph_config())declarations = _subgraphs_declaring(mermaid_code=mermaidflow.mermaid_code, mermaid_id=make_stuff_id(shared_digest))assert declarations == [f"sg_{sanitize_mermaid_id(route_node_id)}"]++ async def test_a_sequence_ending_on_the_condition_carries_the_declaration(self) -> None:+ graph_spec, _ = await dry_run_pipeline(mthds_contents=[DryRunConditionTestData.WRAPPED_CONDITION_MTHDS])++ shared_digest = _shared_digest(graph_spec, condition_code="route_on_match")+ outcome_typings = {"generate_interview_questions": ("InterviewQuestion", True), "write_refusal_email": ("Email", None)}+ typings_by_code = {node.pipe_code: (item.concept, item.multiplicity) for node, item in items_on(graph_spec, shared_digest)}+ assert typings_by_code == {"screen_candidate": ("Anything", None), "route_on_match": ("Anything", None), **outcome_typings}++ # The sequence comes first in node order, so a reader typing the stuff by its first item reads the declaration.+ first_item = first_item_on(graph_spec, shared_digest)+ assert (first_item.concept, first_item.multiplicity) == ("Anything", None)+ assert node_by_code(graph_spec, "screen_candidate") is graph_spec.nodes[0]++ async def test_a_batch_branch_sequence_ending_on_the_condition_carries_the_declaration(self) -> None:+ graph_spec, _ = await dry_run_pipeline(mthds_contents=[DryRunConditionTestData.BATCH_BRANCH_SEQUENCE_MTHDS])++ condition_nodes = nodes_by_code(graph_spec, "route_by_match")+ assert len(condition_nodes) > 1, "the batch runs its branch once per item"+ outcome_typings = {"generate_interview_questions": ("InterviewQuestion", True), "write_refusal_email": ("Text", None)}+ for condition_node in condition_nodes:+ shared_digest = only_output(condition_node).digest+ assert shared_digest is not None+ typings_by_code = {node.pipe_code: (item.concept, item.multiplicity) for node, item in items_on(graph_spec, shared_digest)}+ assert typings_by_code == {"screen_single_cv": ("Anything", None), "route_by_match": ("Anything", None), **outcome_typings}+ first_item = first_item_on(graph_spec, shared_digest)+ assert (first_item.concept, first_item.multiplicity) == ("Anything", None)++ # The batch's aggregate is another stuff, typed by the batched step's own output.+ main_output = only_output(node_by_code(graph_spec, "screen_cvs"))+ assert main_output.digest not in {only_output(node).digest for node in condition_nodes}diff --git a/tests/unit/pipelex/graph/test_condition_output_merge.py b/tests/unit/pipelex/graph/test_condition_output_merge.pyindex 36f8e6441..39dea808f 100644--- a/tests/unit/pipelex/graph/test_condition_output_merge.py+++ b/tests/unit/pipelex/graph/test_condition_output_merge.py@@ -53,7 +53,8 @@ class TestApplyConditionOutputMerges:node_id="assemble",inputs=[IOSpec(name="verdict", concept="Text", digest="d_verdict"),- IOSpec(name="follow_up", concept="Anything", digest="d_rejection"),+ # A reader records the stuff it received, which in a dry run is the last outcome's.+ IOSpec(name="follow_up", concept="Rejection", digest="d_rejection"),],outputs=[IOSpec(name="result", concept="Text", digest="d_result")],),@@ -86,7 +87,7 @@ class TestApplyConditionOutputMerges:# Without a typing, the condition's own item is untouched.assert outputs["route"][0].concept == "Rejection"- def test_a_typing_retypes_the_condition_item_only(self) -> None:+ def test_a_typing_retypes_the_condition_and_its_reader_not_the_outcomes(self) -> None:graph = self._report_shape()merge = ConditionOutputMerge(condition_node_id="route",@@ -104,7 +105,10 @@ class TestApplyConditionOutputMerges:assert outputs["write_questions"][0].multiplicity is Trueassert outputs["write_rejection"][0].concept == "Rejection"assert _inputs_by_node(merged)["assemble"][1].concept == "Anything"+ # Items on other stuffs are untouched.+ assert _inputs_by_node(merged)["assemble"][0].concept == "Text"assert _inputs_by_node(merged)["route"][0].concept == "Text"+ assert outputs["assemble"][0].concept == "Text"def test_a_typing_types_a_list(self) -> None:graph = self._report_shape()@@ -152,34 +156,246 @@ class TestApplyConditionOutputMerges:"fallback": ["d_fallback"],}- def test_a_nested_typing_retypes_the_inner_condition_item(self) -> None:- """A typing names the item by the digest it had when recorded, which a later merge may move."""+ def test_a_sequence_ending_on_the_condition_takes_the_typing(self) -> None:+ """`screen` runs `judge` then `route`, so `screen`'s output is `route`'s, the last outcome's stuff."""graph = self._graph(nodes=[+ _node(node_id="screen", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="decision", concept="Email", digest="d_email")]),+ _node(node_id="judge", outputs=[IOSpec(name="verdict", concept="Text", digest="d_verdict")]),+ _node(+ node_id="route",+ kind=NodeKind.CONTROLLER,+ inputs=[IOSpec(name="verdict", concept="Text", digest="d_verdict")],+ outputs=[IOSpec(name="decision", concept="Email", digest="d_email")],+ ),+ _node(node_id="write_questions", outputs=[IOSpec(name="decision", concept="Question", multiplicity=True, digest="d_questions")]),+ _node(node_id="write_email", outputs=[IOSpec(name="decision", concept="Email", digest="d_email")]),+ ],+ edges=[+ _contains(source="screen", target="judge"),+ _contains(source="screen", target="route"),+ _contains(source="route", target="write_questions"),+ _contains(source="route", target="write_email"),+ ],+ )+ merge = ConditionOutputMerge(+ condition_node_id="route",+ shared_digest="d_email",+ merged_digests=["d_questions"],+ shared_typing=ConditionOutputTyping(concept="Anything"),+ )++ merged = apply_condition_output_merges(graph=graph, merges=[merge])++ outputs = _outputs_by_node(merged)+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["screen"]] == [("d_email", "Anything", None)]+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["route"]] == [("d_email", "Anything", None)]+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["write_questions"]] == [("d_email", "Question", True)]+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["write_email"]] == [("d_email", "Email", None)]+ assert outputs["judge"][0].concept == "Text"++ def test_each_batch_branch_sequence_takes_its_own_condition_typing(self) -> None:+ """`screen_each` runs the sequence `screen_one` per item, each ending on its own run of `route`."""+ nodes = [+ _node(+ node_id="screen_each", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="results", concept="Text", multiplicity=True, digest="d_list")]+ )+ ]+ edges: list[EdgeSpec] = []+ merges: list[ConditionOutputMerge] = []+ for index in (0, 1):+ branch_digest = f"d_branch_{index}"+ nodes += [+ _node(node_id=f"screen_one_{index}", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="output", concept="Text", digest=branch_digest)]),+ _node(node_id=f"route_{index}", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="output", concept="Text", digest=branch_digest)]),+ _node(+ node_id=f"write_questions_{index}",+ outputs=[IOSpec(name="output", concept="Question", multiplicity=True, digest=f"d_questions_{index}")],+ ),+ _node(node_id=f"write_refusal_{index}", outputs=[IOSpec(name="output", concept="Text", digest=branch_digest)]),+ ]+ edges += [+ _contains(source="screen_each", target=f"screen_one_{index}"),+ _contains(source=f"screen_one_{index}", target=f"route_{index}"),+ _contains(source=f"route_{index}", target=f"write_questions_{index}"),+ _contains(source=f"route_{index}", target=f"write_refusal_{index}"),+ EdgeSpec(+ edge_id=f"aggregate_{index}",+ source=f"screen_one_{index}",+ target="screen_each",+ kind=EdgeKind.BATCH_AGGREGATE,+ source_stuff_digest=branch_digest,+ target_stuff_digest="d_list",+ ),+ ]+ merges.append(+ ConditionOutputMerge(+ condition_node_id=f"route_{index}",+ shared_digest=branch_digest,+ merged_digests=[f"d_questions_{index}"],+ shared_typing=ConditionOutputTyping(concept=f"Declared{index}"),+ )+ )++ merged = apply_condition_output_merges(graph=self._graph(nodes=nodes, edges=edges), merges=merges)++ outputs = _outputs_by_node(merged)+ for index in (0, 1):+ assert [(spec.digest, spec.concept) for spec in outputs[f"screen_one_{index}"]] == [(f"d_branch_{index}", f"Declared{index}")]+ assert [(spec.digest, spec.concept) for spec in outputs[f"route_{index}"]] == [(f"d_branch_{index}", f"Declared{index}")]+ assert [(spec.digest, spec.concept) for spec in outputs[f"write_questions_{index}"]] == [(f"d_branch_{index}", "Question")]+ assert [(spec.digest, spec.concept) for spec in outputs[f"write_refusal_{index}"]] == [(f"d_branch_{index}", "Text")]+ # The batch's aggregate is another stuff, which keeps its own typing.+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["screen_each"]] == [("d_list", "Text", True)]++ def test_an_outcome_sequence_keeps_the_outcome_typing(self) -> None:+ """`questions_flow` is an outcome: it and its last step write the slot, inside the condition's outcomes."""+ graph = self._graph(+ nodes=[+ _node(node_id="route", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="follow_up", concept="Rejection", digest="d_rejection")]),+ _node(+ node_id="questions_flow",+ kind=NodeKind.CONTROLLER,+ outputs=[IOSpec(name="follow_up", concept="Question", multiplicity=True, digest="d_questions")],+ ),+ _node(node_id="draft_questions", outputs=[IOSpec(name="draft", concept="Text", digest="d_draft")]),+ _node(+ node_id="finalize_questions",+ inputs=[IOSpec(name="draft", concept="Text", digest="d_draft")],+ outputs=[IOSpec(name="follow_up", concept="Question", multiplicity=True, digest="d_questions")],+ ),+ _node(node_id="write_rejection", outputs=[IOSpec(name="follow_up", concept="Rejection", digest="d_rejection")]),+ ],+ edges=[+ _contains(source="route", target="questions_flow"),+ _contains(source="questions_flow", target="draft_questions"),+ _contains(source="questions_flow", target="finalize_questions"),+ _contains(source="route", target="write_rejection"),+ ],+ )+ merge = ConditionOutputMerge(+ condition_node_id="route",+ shared_digest="d_rejection",+ merged_digests=["d_questions"],+ shared_typing=ConditionOutputTyping(concept="Anything"),+ )++ merged = apply_condition_output_merges(graph=graph, merges=[merge])++ outputs = _outputs_by_node(merged)+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["questions_flow"]] == [("d_rejection", "Question", True)]+ assert [(spec.digest, spec.concept, spec.multiplicity) for spec in outputs["finalize_questions"]] == [("d_rejection", "Question", True)]+ assert outputs["route"][0].concept == "Anything"++ def _nested_shape(self) -> GraphSpec:+ """`screen` ends on `outer`, which runs `wrapper` then `fallback`; `wrapper` ends on `inner`, which runs `x` then `y`."""+ return self._graph(+ nodes=[+ _node(node_id="screen", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Text", digest="d_fallback")]),_node(node_id="outer", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Text", digest="d_fallback")]),+ _node(node_id="wrapper", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Email", digest="d_y")]),_node(node_id="inner", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Email", digest="d_y")]),- _node(node_id="x", outputs=[IOSpec(name="slot", concept="Question", digest="d_x")]),+ _node(node_id="x", outputs=[IOSpec(name="slot", concept="Question", multiplicity=True, digest="d_x")]),_node(node_id="y", outputs=[IOSpec(name="slot", concept="Email", digest="d_y")]),_node(node_id="fallback", outputs=[IOSpec(name="slot", concept="Text", digest="d_fallback")]),+ _node(node_id="assemble", inputs=[IOSpec(name="slot", concept="Text", digest="d_fallback")]),+ ],+ edges=[+ _contains(source="screen", target="outer"),+ _contains(source="outer", target="wrapper"),+ _contains(source="wrapper", target="inner"),+ _contains(source="inner", target="x"),+ _contains(source="inner", target="y"),+ _contains(source="outer", target="fallback"),],- edges=[],)- merges = [++ def _nested_merges(self, *, outer_typing: ConditionOutputTyping | None) -> list[ConditionOutputMerge]:+ return [ConditionOutputMerge(condition_node_id="inner",shared_digest="d_y",merged_digests=["d_x"],- shared_typing=ConditionOutputTyping(concept="Anything", multiplicity=None),+ shared_typing=ConditionOutputTyping(concept="InnerDeclared"),+ ),+ ConditionOutputMerge(condition_node_id="outer", shared_digest="d_fallback", merged_digests=["d_y"], shared_typing=outer_typing),+ ]++ @pytest.mark.parametrize("inner_first", [True, False])+ def test_nested_typings_split_at_the_outer_condition(self, *, inner_first: bool) -> None:+ """What lies between the conditions carries the inner typing; the outer condition and beyond, the outer one."""+ merges = self._nested_merges(outer_typing=ConditionOutputTyping(concept="OuterDeclared"))+ if not inner_first:+ merges.reverse()++ merged = apply_condition_output_merges(graph=self._nested_shape(), merges=merges)++ outputs = _outputs_by_node(merged)+ assert {node_id: [(spec.digest, spec.concept) for spec in specs] for node_id, specs in outputs.items()} == {+ "screen": [("d_fallback", "OuterDeclared")],+ "outer": [("d_fallback", "OuterDeclared")],+ "wrapper": [("d_fallback", "InnerDeclared")],+ "inner": [("d_fallback", "InnerDeclared")],+ "x": [("d_fallback", "Question")],+ "y": [("d_fallback", "Email")],+ # A sibling outcome of the inner condition is the outer condition's outcome, never the inner one's.+ "fallback": [("d_fallback", "Text")],+ "assemble": [],+ }+ assert [(spec.digest, spec.concept) for spec in _inputs_by_node(merged)["assemble"]] == [("d_fallback", "OuterDeclared")]++ def test_an_untyped_outer_condition_lets_the_inner_typing_through(self) -> None:+ merged = apply_condition_output_merges(graph=self._nested_shape(), merges=self._nested_merges(outer_typing=None))++ concepts = {node_id: [spec.concept for spec in specs] for node_id, specs in _outputs_by_node(merged).items()}+ assert concepts["screen"] == ["InnerDeclared"]+ assert concepts["outer"] == ["InnerDeclared"]+ assert concepts["wrapper"] == ["InnerDeclared"]+ assert concepts["inner"] == ["InnerDeclared"]+ assert concepts["fallback"] == ["Text"]+ assert concepts["x"] == ["Question"]+ assert concepts["y"] == ["Email"]+ assert [spec.concept for spec in _inputs_by_node(merged)["assemble"]] == ["InnerDeclared"]++ def test_sibling_typed_conditions_resolve_to_the_first_in_node_order(self) -> None:+ """Two typed conditions as outcomes of an untyped one: everything outside them takes the first one's typing."""+ graph = self._graph(+ nodes=[+ _node(node_id="outer", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Text", digest="d_b")]),+ _node(node_id="inner_a", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Text", digest="d_a")]),+ _node(node_id="a_1", outputs=[IOSpec(name="slot", concept="Number", digest="d_a1")]),+ _node(node_id="a_2", outputs=[IOSpec(name="slot", concept="Text", digest="d_a")]),+ _node(node_id="inner_b", kind=NodeKind.CONTROLLER, outputs=[IOSpec(name="slot", concept="Text", digest="d_b")]),+ _node(node_id="b_1", outputs=[IOSpec(name="slot", concept="Number", digest="d_b1")]),+ _node(node_id="b_2", outputs=[IOSpec(name="slot", concept="Text", digest="d_b")]),+ ],+ edges=[+ _contains(source="outer", target="inner_a"),+ _contains(source="inner_a", target="a_1"),+ _contains(source="inner_a", target="a_2"),+ _contains(source="outer", target="inner_b"),+ _contains(source="inner_b", target="b_1"),+ _contains(source="inner_b", target="b_2"),+ ],+ )+ merges = [+ ConditionOutputMerge(+ condition_node_id="inner_b", shared_digest="d_b", merged_digests=["d_b1"], shared_typing=ConditionOutputTyping(concept="B")+ ),+ ConditionOutputMerge(+ condition_node_id="inner_a", shared_digest="d_a", merged_digests=["d_a1"], shared_typing=ConditionOutputTyping(concept="A")),- ConditionOutputMerge(condition_node_id="outer", shared_digest="d_fallback", merged_digests=["d_y"]),+ ConditionOutputMerge(condition_node_id="outer", shared_digest="d_b", merged_digests=["d_a"]),]merged = apply_condition_output_merges(graph=graph, merges=merges)- inner_output = _outputs_by_node(merged)["inner"][0]- assert inner_output.digest == "d_fallback"- assert inner_output.concept == "Anything"- assert _outputs_by_node(merged)["outer"][0].concept == "Text"+ concepts = {node_id: [spec.concept for spec in specs] for node_id, specs in _outputs_by_node(merged).items()}+ assert concepts["outer"] == ["A"]+ assert concepts["inner_a"] == ["A"]+ assert concepts["inner_b"] == ["B"]+ assert concepts["a_1"] == ["Number"]+ assert concepts["b_2"] == ["Text"]def test_stuff_to_stuff_edges_follow_the_merge(self) -> None:graph = self._graph(diff --git a/tests/unit/pipelex/tracing/test_assembler_equivalence.py b/tests/unit/pipelex/tracing/test_assembler_equivalence.pyindex 83337d929..c243e25d5 100644--- a/tests/unit/pipelex/tracing/test_assembler_equivalence.py+++ b/tests/unit/pipelex/tracing/test_assembler_equivalence.py@@ -532,7 +532,8 @@ def _scenario_dry_condition_merge(tracer: GraphTracer, context: TraceContext) ->"""Dry-run condition whose two outcomes write different concepts into the slot a consumer reads.`write_questions` is a batch whose aggregate edge names its output list; `write_rejection` runs- last, so its stuff is the condition's output and the one `assemble` reads.+ last, so its stuff is the condition's output, the output of `follow_up_flow`, the sequence+ ending on the condition, and the one `assemble` reads."""started_at = _T0@@ -543,8 +544,15 @@ def _scenario_dry_condition_merge(tracer: GraphTracer, context: TraceContext) ->node_kind=NodeKind.CONTROLLER,started_at=started_at,)- cond_id, cond_ctx = tracer.on_pipe_start(+ flow_id, flow_ctx = tracer.on_pipe_start(trace_context=seq_ctx,+ pipe_code="follow_up_flow",+ pipe_type="PipeSequence",+ node_kind=NodeKind.CONTROLLER,+ started_at=started_at + timedelta(milliseconds=500),+ )+ cond_id, cond_ctx = tracer.on_pipe_start(+ trace_context=flow_ctx,pipe_code="route",pipe_type="PipeCondition",node_kind=NodeKind.CONTROLLER,@@ -608,6 +616,11 @@ def _scenario_dry_condition_merge(tracer: GraphTracer, context: TraceContext) ->ended_at=started_at + timedelta(seconds=8),output_spec=IOSpec(name="follow_up", concept="Rejection", digest="digest_rejection"),)+ tracer.on_pipe_end_success(+ node_id=flow_id,+ ended_at=started_at + timedelta(seconds=8, milliseconds=500),+ output_spec=IOSpec(name="follow_up", concept="Rejection", digest="digest_rejection"),+ )assemble_id, _ = tracer.on_pipe_start(trace_context=seq_ctx,@@ -615,7 +628,7 @@ def _scenario_dry_condition_merge(tracer: GraphTracer, context: TraceContext) ->pipe_type="PipeLLM",node_kind=NodeKind.OPERATOR,started_at=started_at + timedelta(seconds=9),- input_specs=[IOSpec(name="follow_up", concept="Anything", digest="digest_rejection")],+ input_specs=[IOSpec(name="follow_up", concept="Rejection", digest="digest_rejection")],)tracer.on_pipe_end_success(node_id=assemble_id,@@ -673,7 +686,10 @@ class TestAssemblerEquivalence:direct_spec, assembled_spec = _run_both_paths(_scenario_dry_condition_merge)for spec in (direct_spec, assembled_spec):outputs_by_code = {node.pipe_code: node.node_io.outputs for node in spec.nodes}+ inputs_by_code = {node.pipe_code: node.node_io.inputs for node in spec.nodes}+ assert [(item.digest, item.concept) for item in outputs_by_code["follow_up_flow"]] == [("digest_rejection", "Anything")]assert [(item.digest, item.concept) for item in outputs_by_code["route"]] == [("digest_rejection", "Anything")]+ assert [(item.digest, item.concept) for item in inputs_by_code["assemble"]] == [("digest_rejection", "Anything")]assert [(item.digest, item.concept) for item in outputs_by_code["write_questions"]] == [("digest_rejection", "Question")]assert [(item.digest, item.concept) for item in outputs_by_code["write_rejection"]] == [("digest_rejection", "Rejection")]aggregate_edges = [edge for edge in spec.edges if edge.kind == EdgeKind.BATCH_AGGREGATE]