fix: properly check returnDirect in makeToolsToModelEdge to fix premature workflow termination#4429
Open
zhexueqi wants to merge 4 commits intoalibaba:mainfrom
Open
fix: properly check returnDirect in makeToolsToModelEdge to fix premature workflow termination#4429zhexueqi wants to merge 4 commits intoalibaba:mainfrom
zhexueqi wants to merge 4 commits intoalibaba:mainfrom
Conversation
…event residual values When a MessagesModelHook overrides canJumpTo() to return List.of(JumpTo.end), the framework registers a conditional edge that reads the jump_to field from state. Previously, if beforeModel/afterModel returned AgentCommand without a JumpTo intent, the jump_to field was not written to state, leaving any residual value from a previous execution intact. This caused the workflow to terminate prematurely even when the hook did not intend to jump. Fix: Always write jump_to to state in BeforeModelAction and AfterModelAction. When the AgentCommand has no JumpTo intent, explicitly set null to clear any stale value from prior executions. Fixes alibaba#4421
The previous code had a FIXME placeholder that always returned false, causing the returnDirect check to fail. When a tool has returnDirect=true, AgentToolNode sets the finish_reason metadata in ToolResponseMessage. Fix: Check the finish_reason metadata from ToolResponseMessage instead of trying to match tool names against a non-existent registry. Fixes alibaba#4427
Collaborator
Collaborator
|
@zhexueqi ths for your pr, can you provider a simple demo? btw, i think tests need to be added 👀 |
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.
Problem
When a tool has
eturnDirect=true, the ReactAgent should route directly to the end node after tool execution, bypassing the model. However, the routing logic in makeToolsToModelEdge had a FIXME placeholder that always returned alse, making the
eturnDirect feature completely non-functional.
Additionally, the issue reporter found a second bug: on the second call (with the same session), the agent would incorrectly terminate early because the ToolResponseMessage from the previous turn still had the inish_reason metadata in the message history.
Root Cause
In ReactAgent.makeToolsToModelEdge:
java boolean allReturnDirect = toolResponseMessage.getResponses().stream().allMatch(toolResponse -> { String toolName = toolResponse.name(); return false; // FIXME ? always false, returnDirect never works });Fix
AgentToolNode already correctly sets inish_reason metadata on ToolResponseMessage when all tools have
eturnDirect=true. The fix reads this metadata from the last ToolResponseMessage in state (which etchLastToolResponseMessage already retrieves correctly - it always gets the most recent one, not historical ones).
java Map<String, Object> metadata = toolResponseMessage.getMetadata(); if (metadata != null && FINISH_REASON_METADATA_KEY.equals(metadata.get(FINISH_REASON_METADATA_KEY))) { return endDestination; }This correctly handles both issues:
1.
eturnDirect=true now actually works
2. Historical ToolResponseMessage entries are ignored because etchLastToolResponseMessage only returns the most recent one
Fixes #4427