Skip to content

fix(tools): report tool success/failure via ToolResultBlock state - #2751

Merged
jujn merged 9 commits into
agentscope-ai:mainfrom
haosong384:fix/tool-result-error-detection
Oct 10, 2026
Merged

jujn merged 9 commits into
agentscope-ai:mainfrom
haosong384:fix/tool-result-error-detection

Conversation

@haosong384

@haosong384 haosong384 commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #2745
Built-in tool calls could report failed operations as SUCCESS (#2745). Inferring failure from output text also misclassified successful content that merely starts with Error: or [ERROR].

Changes

  • Add ToolResultBlock.success(String) so successful tool calls can carry an explicit terminal state.
  • Convert the affected built-in tool methods in place to ToolResultBlock / Mono<ToolResultBlock>:
    • Harness filesystem, web, memory, session, task, team, subagent, and wait tools
    • Paw session tools
    • Core long-term-memory, RAG, and meta tools
    • Service scheduler outbound delivery
  • Map real outcomes explicitly:
    • Invalid input, unknown IDs, missing repository/session context, and synchronous failures map to ERROR
    • Cancelled or interrupted waits map to INTERRUPTED
    • Accepted background work, timeout promotion, successful lookups, and empty search results map to SUCCESS
    • task_output and wait_async_results propagate FAILED / CANCELLED task state
  • Stop swallowing local subagent task exceptions as successful error text; let the repository mark those tasks FAILED.
  • Keep ReActAgent and reflective Toolkit registration unchanged. Structured text no longer receives the JSON string quoting applied to plain String returns.
  • Update direct callers, regression coverage, and concise English/Chinese docs; the explicit-state guidance now lives under custom tools.
  • Merge current main (8eb911).

Compatibility

  • Direct Java callers now receive ToolResultBlock or Mono<ToolResultBlock>; inspect getState() and the text blocks in getOutput().
  • Existing payload text is preserved where possible.

Copilot AI lite review requested due to automatic review settings August 17, 2026 13:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

AgentScopeJavaBot

This comment was marked as duplicate.

AgentScopeJavaBot

This comment was marked as duplicate.

@jujn

jujn commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

我更倾向于重构一下:让 FilesystemTool 等工具成功/失败时返回结构化 ToolResultBlock.withState();
目前,ReActAgent # determineToolResultState 取 ToolResultBlock 携带的 TextBlock.getText,容易误判:内容返回型工具可能会误报 ERROR

@jujn jujn self-assigned this Aug 20, 2026
Built-in @tool methods signalled failure by returning a string prefixed
with "Error: ", leaving ReActAgent#determineToolResultState to infer the
state from the result text. Per review feedback on agentscope-ai#2751, that inference
is replaced by having the tools state their outcome structurally.

Text-based inference cannot be made correct: several tools return
external content on success — read_file hands back raw file content,
web_fetch an HTTP body, shell execute the command output — so content
that merely looks like an error message is indistinguishable from a real
failure. Broadening the prefix match would have turned a missed failure
into a false one.

- ToolResultBlock.success(String): new, symmetric with the existing
  error(String). Needed because an unset state is coerced to RUNNING by
  the constructor, which determineToolResultState treats as "unknown"
  and resolves via the text fallback; only an explicit SUCCESS opts out.
- 17 @tool methods across FilesystemTool, WebTools, SessionSearchTool,
  TaskTool, MemoryGetTool, MemorySaveTool and ShellExecuteTool now
  return ToolResultBlock: failures via error(msg), successes via
  success(text).
- ReActAgent is untouched (byte-identical to main). The [ERROR] text
  fallback is left in place for third-party tools that build blocks
  without a state; structured results no longer reach it.

Error text is unchanged: error(msg) already renders "Error: " + msg.
Successful output loses the JSON quotes that DefaultToolResultConverter
added around String returns, since ToolResultBlock passes through
unserialised — the model now sees the bare content.

ReActAgentToolResultErrorPrefixTest covered the removed heuristic and is
replaced by ReActAgentToolResultStateTest, which asserts both directions
of the new contract end-to-end, including a successful read whose content
begins with "Error: ". Tool-level tests assert state alongside text.

Closes agentscope-ai#2745
@haosong384
haosong384 force-pushed the fix/tool-result-error-detection branch from e36f264 to bdd4602 Compare August 20, 2026 12:35
@haosong384 haosong384 changed the title fix(core): detect Error: prefix so failing @Tool calls report ERROR state fix(tools): report tool success/failure via ToolResultBlock state Aug 20, 2026
@haosong384

haosong384 commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@jujn Thanks — you were right, and the point turned out to be sharper than a style preference. Reworked as requested.

Your concern reproduces. FilesystemTool.readFile returns raw file content on success, so reading a log file whose first line is Error: connection refused is a successful read that my previous revision classified as ERROR:

expected: <SUCCESS> but was: <ERROR>

Five tools are exposed this way — read_file, web_fetch, web_search, memory_get, shell execute — all of them returning external content on success. Widening the prefix match traded a missed failure for a fabricated one, so it's gone.

What changed

  • ReActAgent is now byte-identical to main; git diff main -- ReActAgent.java is empty. The fix lives entirely in the tools.
  • 17 @Tool methods across FilesystemTool, WebTools, SessionSearchTool, TaskTool, MemoryGetTool, MemorySaveTool, ShellExecuteTool return ToolResultBlock: failures via ToolResultBlock.error(msg), successes via a new ToolResultBlock.success(text).
  • Error text is unchanged — error(msg) already renders "Error: " + msg.

Two findings worth your attention

withState(SUCCESS) is required on success paths, not optional. An unset state is coerced to RUNNING by the constructor, and determineToolResultState treats RUNNING as "unknown" and falls through to the text fallback. So ToolResultBlock.text(...) does not opt a tool out of the heuristic. That's why I added success(String) — symmetric with the existing error(String), and documented so the next person doesn't hit the same trap.

One behaviour change to flag: successful output loses the JSON quotes that DefaultToolResultConverter added around String returns, because ToolResultBlock passes through unserialised. The model now sees bare content and real newlines instead of "\"...\"" with \n escapes. Looks like a strict improvement, but it does alter prompt content.

Scope question for you. This PR covers the tool named in #2745 plus every content-returning tool. Still returning strings: TeamTool (20 methods), AgentSpawnTool / AgentGenerateTool (Mono<String> → Mono<ToolResultBlock>), TodoTools, MemorySearchTool. I stopped here to keep the diff reviewable — want me to extend this PR, or follow up separately?

Verification: mvn -pl agentscope-core,agentscope-harness clean test → 2277 + 833 green, spotless clean. Note clean matters: incremental compilation hid a real return-type mismatch in TaskTool while still reporting BUILD SUCCESS.

@jujn

jujn commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

👍 Please continue in the current PR.

Return ToolResultBlock from team, memory search, and todo tools, and
Mono<ToolResultBlock> from subagent spawn, send, and generation. Preserve
existing payloads while reporting explicit success, failure, and remote
interruption states. Keep asynchronous acceptance and timeout promotion
successful, and leave ReActAgent's fallback unchanged.

Add state regression coverage, adapt direct test callers, and document
the return-type migration and subagent outcome semantics in English and
Chinese.

Validation: Linux core/harness clean test passed (2284 + 894 tests,
0 failures, 0 errors, 9 existing skips); Windows harness and affected
core tests passed. Spotless and git diff --check passed.
@CLAassistant

CLAassistant commented Sep 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Preserve bounded filesystem listings and upstream task status messages
while returning structured tool results. Retain web interruption handling
and cover task lookup errors, successful payloads, and interrupted waits.
@haosong384
haosong384 force-pushed the fix/tool-result-error-detection branch from 4b7508f to 6eaa5a1 Compare September 10, 2026 05:48
@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch origin
git rebase origin/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Feel free to @mention me for a re-review after conflicts are resolved.


Automated notification by github-manager-bot

@jujn
jujn force-pushed the fix/tool-result-error-detection branch from 98f5e60 to a5d8e88 Compare October 10, 2026 12:19
@jujn
jujn merged commit 20dc107 into agentscope-ai:main Oct 10, 2026
9 checks passed
helloworldtang pushed a commit to helloworldtang/agentscope-java that referenced this pull request Oct 11, 2026
Brings main (incl. agentscope-ai#2697, agentscope-ai#3324, agentscope-ai#2751, agentscope-ai#3129) into the branch. Only
conflict: ReActAgentStructuredOutputTest.java — this branch's added test
(testToolBasedExtractionPreservesMessageIdentity) sat exactly where
agentscope-ai#2697 later appended its new tests; kept both sides (our test plus the
incoming suite) and both import sets. ReActAgent.java auto-merged.

Verified: ReActAgentStructuredOutputTest 16 tests green including both
this branch's identity test and the merged-in tests.
xzxiaoshan added a commit to xzxiaoshan/agentscope-java that referenced this pull request Oct 11, 2026
- Builder.copy() now carries capabilityResolver (main's copy()-based build
  dropped it, silently disabling the grant layer)
- reset_equipped_tools keeps the RuntimeContext param while adopting
  ToolResultBlock.success from agentscope-ai#2751
- externalToolSchema and rewriteResetToolSchema carry the new deferLoading
  schema field
- grant tests observe the call-scoped rc (main isolates the caller rc) and
  assert the suspended external result
uhhgxg added a commit to uhhgxg/agentscope-java that referenced this pull request Oct 14, 2026
The takeover test can exceed its 3s sent.await budget on loaded CI
runners: a >leaseMs (1500ms) stall expires the lease mid-takeover,
the consumer backs off and re-acquires, and the reply lands after
the budget. Widen the takeover assertion to 10s and the class-level
@timeout to 60s, consistent with agentscope-ai#2751/agentscope-ai#3505. Closes agentscope-ai#3547
aMetric added a commit to aMetric/agentscope-java that referenced this pull request Oct 15, 2026
…uncation (agentscope-ai#3266)

Rebase onto main (f4dd724) resolving the String -> ToolResultBlock
return-type change from agentscope-ai#2751 and the session_search clamp from agentscope-ai#3382:
- Clamp model-supplied maxResults to a 100 hard ceiling (Math.min), default 30
- Truncate matched lines at 500 chars (surrogate-pair-safe) with a memory_get hint
- Test-before-cap scan so a match exactly at the cap is flagged, never dropped
- Keep ToolResultBlock.success/error contract and the matchMode overloads
aMetric added a commit to aMetric/agentscope-java that referenced this pull request Nov 2, 2026
…uncation (agentscope-ai#3266)

Rebase onto main (f4dd724) resolving the String -> ToolResultBlock
return-type change from agentscope-ai#2751 and the session_search clamp from agentscope-ai#3382:
- Clamp model-supplied maxResults to a 100 hard ceiling (Math.min), default 30
- Truncate matched lines at 500 chars (surrogate-pair-safe) with a memory_get hint
- Test-before-cap scan so a match exactly at the cap is flagged, never dropped
- Keep ToolResultBlock.success/error contract and the matchMode overloads
aMetric added a commit to aMetric/agentscope-java that referenced this pull request Nov 2, 2026
…uncation (agentscope-ai#3266)

Rebase onto main (f4dd724) resolving the String -> ToolResultBlock
return-type change from agentscope-ai#2751 and the session_search clamp from agentscope-ai#3382:
- Clamp model-supplied maxResults to a 100 hard ceiling (Math.min), default 30
- Truncate matched lines at 500 chars (surrogate-pair-safe) with a memory_get hint
- Test-before-cap scan so a match exactly at the cap is flagged, never dropped
- Keep ToolResultBlock.success/error contract and the matchMode overloads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core/agent Agent runtime, pipeline, hooks, plan breaking change bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: FileSystemTool 没有正确返回ToolResultState.ERROR

6 participants