feat: pass invocation_state to edge condition calls - #2642
Conversation
|
Assessment: Comment Solid re-submission — the "don't serialize Review Themes
Nice work threading |
Add support for edge conditions that receive invocation_state, enabling conditional routing based on runtime context (feature flags, user roles, environment config) passed during graph invocation. Also fixes a deadlock in _compute_ready_nodes_for_resume() where conditional edges evaluating to False would block downstream nodes from ever becoming ready on interrupt/resume workflows. Resolves strands-agents#1346
- Cache inspect.signature() results via WeakKeyDictionary - Remove unused @runtime_checkable decorator - Gate serialization validation on session_manager presence - Add validation on deserialization path for symmetry - Move json import to module level - Add inline comments for short-circuit and cache behavior - Extract _make_graph() test helper, fix list->set type consistency
Cache the result of _is_context_condition() on each GraphEdge instance instead of a module-level WeakKeyDictionary. Simpler, no global state, and directly co-located with the edge that owns the condition.
invocation_state can contain non-serializable objects (callbacks, agent refs). Rather than validating or deep-copying, the caller is now responsible for passing invocation_state on every invocation including resume. Edge conditions receive whatever the caller passes each time. This fixes TypeError: Object of type Agent is not JSON serializable that occurred when the session manager persisted graph state.
…arify docstrings
- Remove None default from should_traverse's invocation_state parameter
to align with the EdgeConditionWithContext protocol contract
- Clarify _is_node_ready_for_resume docstring: distinguish pre-computed
initial resume batch from re-evaluated subsequent routing, and document
intentional AND-join vs OR-join semantics difference
- Add note to _is_context_condition about name-based parameter detection
- Update tests to pass invocation_state={} explicitly
fd5370c to
528e559
Compare
|
Rebased onto latest main (63 commits), addressed all automated review feedback, and force-pushed. Changes in response to review:
Re: integration test failures: All 20+ failures are in OpenAI model tests ( Re: |
|
Assessment: Approve Thanks for the quick turnaround @yananym — re-reviewed commit Resolution of prior comments
The only remaining item is the maintainer-side API sign-off on the new public symbols — not a code change. Nice, clean iteration on this. 🚀 |
Description
Re-submission of #2305 (which was merged then reverted due to serialization failures in CI).
Add support for edge conditions that receive
invocation_state, enabling conditional routing based on runtime context (feature flags, user roles, environment config) passed during graph invocation.Also fixes a deadlock in
_compute_ready_nodes_for_resume()where conditional edges evaluating toFalsewould block downstream nodes from ever becoming ready on interrupt/resume workflows.Change from #2305:
invocation_stateis NOT serializedThe reverted PR failed because
invocation_statewas serialized to session storage, causingTypeError: Object of type Agent is not JSON serializablewhen non-serializable objects (callbacks, agent references) were present.Fix (Option B): Skip serializing
invocation_stateentirely. On resume, the caller must passinvocation_stateagain. The edge conditions get whatever the caller passes on each invocation.This is the cleanest approach because:
Resume flow:
serialize_state()pre-computesnext_nodes_to_execute(evaluating edge conditions whileinvocation_stateis still in memory). On resume, the concrete node IDs are loaded from session — no re-evaluation needed for the initial batch. Any subsequent routing decisions use the caller's freshinvocation_state.Public API Changes
GraphBuilder.add_edge()now accepts conditions with an extended signature via theEdgeConditionWithContextProtocol:Both signatures are supported indefinitely — no deprecation, no breaking changes.
Related Issues
Resolves #1346
Partial for #2387 (enables
invocation_statefoundation for cycle/loop support)Documentation PR
strands-agents/docs#847
Type of Change
New feature (re-submission with fix)
Testing
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.