fix(graph): handle cancel_node gracefully without raising exception#1524
Draft
fix(graph): handle cancel_node gracefully without raising exception#1524
Conversation
Align Graph cancel_node behavior with Swarm by not raising a RuntimeError when a node is cancelled. Instead, handle cancellation gracefully by: - Creating a NodeResult with status=FAILED for the cancelled node - Adding the node to failed_nodes set - Yielding MultiAgentNodeStopEvent for the cancelled node - Returning without exception to allow GraphResult to be yielded Also add a failed_nodes check in _execute_graph to stop execution after a cancellation, preventing downstream nodes from executing. This makes the Graph behavior consistent with Swarm cancel_node handling, where users can cleanly exit without catching RuntimeError.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Member
Author
|
/strands Please address lint failures |
NodeResult.result expects AgentResult | MultiAgentResult | Exception, not a string. Wrap the cancel message in an Exception to fix the mypy type error.
|
Fixed the mypy lint error by wrapping Commit: 924a1b3 |
Member
Author
|
/strands Do we need to construct a NodeResult for a cancelled node? Also, do we need to yield MultiAgentNodeStopEvent? |
Simplify the cancel_node handling to match Swarm's approach: - Yield MultiAgentNodeCancelEvent - Set self.state.status = Status.FAILED - Return early (no exception) Remove unnecessary NodeResult construction and MultiAgentNodeStopEvent for cancelled nodes, as Swarm doesn't emit these either. Update the _execute_graph check to use Status.FAILED instead of failed_nodes to stop downstream execution.
|
Simplified the implementation to match Swarm's pattern exactly: Before (over-engineered):
After (matches Swarm): yield MultiAgentNodeCancelEvent(node.node_id, cancel_message)
self.state.status = Status.FAILED
returnThe key insight is that Swarm (lines 750-759) simply:
No Commit: 21d1fa3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, users can cancel a Graph node execution by setting
event.cancel_node = <STR_MSG|True>within aBeforeNodeCallEventhook. Unlike Swarm, which handles this gracefully by settingstatus = FAILEDand breaking out of the loop, Graph raises aRuntimeErrorthat stops the entire graph execution. This inconsistency forces users to wrap Graph execution in try/catch blocks when intentionally cancelling nodes, which is unnecessary and differs from the Swarm behavior.This change aligns Graph
cancel_nodebehavior with Swarm to provide a clean exit path without exception handling.Resolves #1500
Public API Changes
No public API changes. The behavior change is internal -
cancel_nodestill works the same way, but now exits gracefully:Breaking Changes
This is a breaking change for any code that catches
RuntimeErrorduring graph node cancellation. The existing behavior was inconsistent with Swarm and is considered a bug.Migration