Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- knowledge
last_checked: "2026-11-26T01:12:45Z"
last_checked: "2026-12-04T20:52:37Z"
-->
# Architecture

Expand Down Expand Up @@ -264,9 +264,9 @@ segment types through snapshots and completion. Converting a variable with
`to_segment()` removes its source identity so the parent pool binds completed
outputs to the container's selectors. Storing an existing variable under a new
selector also assigns a new identity; see [variable pool tests](tests/runtime/test_variable_pool.py).
Iteration keeps matching array types when flattening outputs and uses
`array[any]` for mixed array types. See [workflow tests](tests/workflows/test_full_engine_events.py)
for empty, serial, and parallel container behavior.
When Iteration flattens arrays, it ignores empty `array[any]` outputs and keeps the type shared by the remaining arrays.
If their types differ or no arrays remain, the result is `array[any]`.
See the [workflow tests](tests/workflows/test_full_engine_events.py) for serial and parallel Iteration and for inputs to a downstream List Operator.

Stored outputs remain typed; container node-result inputs and Loop/Iteration
presentation events expose plain values. The
Expand Down
5 changes: 5 additions & 0 deletions src/graphon/engine/container_handler/builtin/iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ def _collect_iteration_outputs(
isinstance(output, (ArraySegment, NoneSegment)) for output in outputs
)
):
arrays = [
output
for output in arrays
if not isinstance(output, ArrayAnySegment) or output.value
]
flattened = [item for output in arrays for item in output.value]
if len({output.value_type for output in arrays}) == 1:
return build_container_value(arrays[0]).model_copy(
Expand Down
69 changes: 69 additions & 0 deletions tests/workflows/test_full_engine_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,26 @@ def test_loop_secret_can_be_assigned_and_cleared(
"array[any]",
["first"],
),
(
[
ArrayStringSegment(value=["first"]),
ArrayAnySegment(value=[]),
ArrayNumberSegment(value=[1]),
],
True,
"array[any]",
["first", 1],
),
(
[
ArrayAnySegment(value=[]),
ArrayStringSegment(value=["ok"]),
ArrayAnySegment(value=[1]),
],
True,
"array[any]",
["ok", 1],
),
(
[ArrayFileSegment(value=[]), ArrayFileSegment(value=[])],
False,
Expand All @@ -1451,6 +1471,8 @@ def test_loop_secret_can_be_assigned_and_cleared(
"declared-any",
"missing-output",
"mixed-types",
"mixed-types-with-untyped-empty",
"nonempty-any-with-string",
"unflattened",
"tuple-array",
"opaque-elements",
Expand Down Expand Up @@ -1491,6 +1513,53 @@ def test_iteration_preserves_array_output_types(
assert succeeded.outputs == {"output": expected_value}


@pytest.mark.parametrize(
("items", "expected_type", "expected_items"),
[
([[], ["ok"]], "array[string]", ["ok"]),
([["ok"], []], "array[string]", ["ok"]),
([[], [2, 1]], "array[number]", [2, 1]),
([[2, 1], []], "array[number]", [2, 1]),
([[], []], "array[any]", []),
],
)
def test_list_operator_accepts_flattened_output_from_iteration_with_empty_arrays(
items: list[list[object]], expected_type: str, expected_items: list[object]
) -> None:
iteration_nodes = _iteration_nodes("iteration")
iteration_nodes[0]["data"]["flatten_output"] = True
dsl = _graph_dsl(
nodes=[
_start_node(),
*iteration_nodes,
{
"id": "list",
"data": {
"type": "list-operator",
"variable": ["iteration", "output"],
"filter_by": {},
"order_by": {},
"limit": {},
},
},
_end_node([{"variable": "items", "value_selector": ["list", "result"]}]),
],
edges=[
_edge("start", "iteration"),
_edge("iteration", "list"),
_edge("list", "end"),
],
)
engine = loads(dsl, start_inputs={"items": items})

events = list(engine.run())

assert final_outputs(events) == {"items": expected_items}
output = engine.runtime_state.variable_pool.get(("iteration", "output"))
assert output is not None
assert output.value_type == expected_type


@pytest.mark.parametrize("flatten_output", [False, True])
def test_iteration_any_array_files_survive_snapshot(flatten_output: bool) -> None:
file = File(
Expand Down
Loading