Skip to content

Model workflow edge direction by condition presence - #938

Merged
PaulAsjes merged 3 commits into
mainfrom
pma/workflow-edge-model-fix
Nov 18, 2026
Merged

PaulAsjes merged 3 commits into
mainfrom
pma/workflow-edge-model-fix

Conversation

@PaulAsjes

Copy link
Copy Markdown
Collaborator

Summary

Fixes a set of edge-direction bugs in the hand-written workflows-as-code builder, bringing it in line with the six new-language SDKs (Rust/Ruby/PHP/.NET already landed). A loaded workflow edge can carry only a forward condition, only a backward one, or both, and a transition exists only in the direction whose condition is present — the builder previously assumed a forward transition always exists.

Details
  • Validation (validate.py): the forward routing checks (edge_into_start, end-has-outgoing, tool/result) now run only when a forward_condition is present, and the same checks run in the reverse direction when a backward_condition is present. Previously a backward condition could point into the start node, leave an end node, or leave a tool node without a result condition, all unflagged.
  • Reachability (validate.py depths_from): an edge is followed forward only when it has a forward_condition (the reverse pass already required a backward one), so a loaded backward-only edge no longer invents a forward transition or suppresses unreachable_node.
  • Serialization (workflow.py _to_wire): an edge joins its source's edge_order only when it has a forward_condition (its target's only when backward_condition), so a loaded backward-only edge isn't re-attached forward on save.
  • insert_between on a bidirectional edge can't preserve the original target's reverse route, so it now rejects with a new cannot_insert_bidirectional issue instead of silently dropping/misattaching it.
  • position is accepted for node kinds with no dedicated field map (graph.py), e.g. generated say/update_state, instead of recording invalid_option.

These are behaviour fixes to the shipped builder; no generated code changes. The same fixes are in the Rust/Ruby/PHP/.NET PRs. Follow-up: lock these behaviours into the shared golden fixtures so all SDKs stay identical.

Testing

  • pytest tests/agent_workflows/ — 59 passed (8 shared fixtures + hand-written suite + 4 new TestEdgeDirectionModel cases covering bidirectional-insert rejection, backward-out-of-end, position on an unmapped node kind, and a loaded backward-only edge's edge_order).
  • ruff format --check clean on the changed files.

🤖 Generated with Claude Code

…onal insert

Brings the workflows-as-code builder's edge model in line with the six new
SDKs. A loaded edge can carry only a forward or only a backward condition,
and a transition exists only in the direction whose condition is present:

- validate(): the forward routing checks (edge_into_start, end outgoing,
  tool/result) run only when a forward condition is present, and the same
  checks now also run in the reverse direction for a backward condition
  (previously a backward condition could leave an end node, point into
  start, or leave a tool without a result unflagged).
- depths_from(): follow an edge forward only when it has a forward
  condition.
- _to_wire(): add an edge to its source's outgoing edge_order only when it
  has a forward condition (to the target's only when backward), so a loaded
  backward-only edge is not re-attached forward.

insert_between() on a bidirectional edge can't preserve the original
target's reverse route, so reject it (cannot_insert_bidirectional) instead
of dropping it. And position is accepted for node kinds without a dedicated
field map (e.g. generated say/update_state).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PaulAsjes
PaulAsjes marked this pull request as ready for review November 18, 2026 11:19
@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes core workflow graph validation, layout, and serialization semantics for loaded or bidirectional edges; incorrect behavior could alter reachability or saved edge order but is confined to the workflows-as-code builder with new tests.

Overview
Treats workflow transitions as directional: a move exists only when the corresponding forward_condition or backward_condition is set on an edge, instead of assuming every edge has a forward transition.

Validation runs start/end/tool routing rules on the forward leg only when forward_condition is present, and applies the same rules to the reverse leg (target → source) when backward_condition is present. Reachability layout follows an edge forward only if it has a forward_condition, so backward-only loaded edges no longer fake forward paths or hide unreachable nodes. Wire serialization puts edges in a node’s edge_order only for outgoing transitions that actually exist (source for forward, target for backward).

insert_between now fails with cannot_insert_bidirectional when splitting an edge that still has a backward condition, rather than dropping the reverse route. Node updates accept position for kinds without a dedicated field map (e.g. say), avoiding spurious invalid_option errors.

Reviewed by Cursor Bugbot for commit 8b13a16. Bugbot is set up for automated code reviews on this repo. Configure here.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new cannot_insert_bidirectional issue code is missing from the exported WorkflowIssueCode literal, creating a type mismatch for callers and leaving exhaustive handlers incomplete.

1 open issue
What changed in this PR

This PR updates the hand-written workflow builder so transitions exist only in directions with a condition, aligning validation and serialization with the other SDKs.

Changes:

  • Makes routing validation, reachability, and outgoing edge order respect condition presence.
  • Rejects insertion into edges with backward conditions and accepts positions on unmapped node kinds.
  • Adds regression tests and shared fixtures for edge-direction behavior.
File Description
tests/​agent_workflows/​test_workflow.py Adds four edge-direction and position regression tests.
tests/​agent_workflows/​fixtures/​10-backward-condition-rules.json Covers rejection of reverse transitions leaving end nodes.
tests/​agent_workflows/​fixtures/​09-insert-into-bidirectional.json Covers bidirectional insertion rejection.
src/​elevenlabs/​agent_workflows/​workflow.py Guards insertion and makes outgoing edge order direction-aware.
src/​elevenlabs/​agent_workflows/​validate.py Validates both transition directions and corrects reachability.
src/​elevenlabs/​agent_workflows/​graph.py Allows position updates for node kinds without field maps.

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The serialization test would still pass without the new guard because it lacks a backward-only edge case asserting that an otherwise isolated target remains unreachable.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Add test for backward-only edge reachability

tests/​agent_workflows/​test_workflow.py:648

This test checks serialization, but all its nodes are already reachable through forward edges, so removing the new depths_from condition guard still passes it. Add a case with a reachable source and an otherwise isolated target connected by a backward-only edge, and assert unreachable_node for the target. That locks down the false-reachability bug this PR fixes.

Review effort: Balanced

@PaulAsjes
PaulAsjes merged commit 1e800fe into main Nov 18, 2026
7 checks passed
@PaulAsjes
PaulAsjes deleted the pma/workflow-edge-model-fix branch November 18, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants