Skip to content

fix: properly check returnDirect in makeToolsToModelEdge to fix premature workflow termination#4429

Open
zhexueqi wants to merge 4 commits intoalibaba:mainfrom
zhexueqi:fix/reactagent-returndirect-logic
Open

fix: properly check returnDirect in makeToolsToModelEdge to fix premature workflow termination#4429
zhexueqi wants to merge 4 commits intoalibaba:mainfrom
zhexueqi:fix/reactagent-returndirect-logic

Conversation

@zhexueqi
Copy link
Copy Markdown

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

…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
@robocanic
Copy link
Copy Markdown
Collaborator

@yuluo-yx

@yuluo-yx yuluo-yx self-assigned this Mar 21, 2026
@yuluo-yx
Copy link
Copy Markdown
Collaborator

@zhexueqi ths for your pr, can you provider a simple demo?

btw, i think tests need to be added 👀

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.

[BUG] ReactAgent returnDirect错误

3 participants