Skip to content

fix(core): normalize tool call result transcripts - #2357

Open
Zbhbb wants to merge 10 commits into
agentscope-ai:mainfrom
Zbhbb:codex/tool-transcript-recovery
Open

fix(core): normalize tool call result transcripts#2357
Zbhbb wants to merge 10 commits into
agentscope-ai:mainfrom
Zbhbb:codex/tool-transcript-recovery

Conversation

@Zbhbb

@Zbhbb Zbhbb commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

Current main branch.

Description

Fixes #2191.

Background

After an interruption, a tool-use block can remain in history while its tool result is missing or appears later in the transcript. Providers that require adjacent tool calls and results reject that payload.

Changes

  • Normalize outbound model transcripts without mutating stored agent state.
  • Move known tool results next to their owning assistant tool calls.
  • Insert a terminal error result only when no result exists, so the next model call can recover safely.
  • Apply the normalization to both normal ReAct reasoning and summary generation.

Validation

  • mvn -q -pl agentscope-core -am -Dtest=MessageUtilsToolTranscriptTest,HookStopAgentTest,ReActAgentNewLoopE2ETest test
  • mvn -q -T1 -pl agentscope-core -am verify

The full repository verify ran through its Java test phase, but the unrelated agentscope-builder step could not download npm 10.9.2 because the npm registry TLS handshake was terminated.

Checklist

  • Code has been formatted with mvn spotless:apply rules
  • Relevant tests are passing
  • Javadoc follows existing conventions
  • Documentation update is not needed
  • Code is ready for review

Copilot AI review requested due to automatic review settings July 22, 2026 10:33

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.

Pull request overview

This PR addresses provider rejections (e.g., OpenAI/Anthropic 400) caused by mismatched tool-call/tool-result ordering after interruptions by normalizing the outbound transcript (without mutating stored agent state) before sending messages to the model.

Changes:

  • Add MessageUtils.normalizeToolCallResults(...) to relocate tool results adjacent to their owning assistant tool calls and synthesize a terminal error tool result when missing.
  • Apply transcript normalization to both the main ReAct reasoning call path and the summarization call path in ReActAgent.
  • Add/extend tests to validate transcript normalization behavior and ensure agent state isn’t mutated by normalization.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
agentscope-core/src/main/java/io/agentscope/core/util/MessageUtils.java Introduces transcript normalization logic for tool results/tool calls.
agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java Applies normalization before model calls in reasoning + summary.
agentscope-core/src/test/java/io/agentscope/core/util/MessageUtilsToolTranscriptTest.java Adds unit tests covering tool transcript normalization scenarios.
agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentNewLoopE2ETest.java Adds an E2E assertion that normalization occurs before model invocation and does not mutate stored history.

Comment thread agentscope-core/src/main/java/io/agentscope/core/util/MessageUtils.java Outdated
@Zbhbb

Zbhbb commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 5510e8c. Replaced the fully-qualified IntStream expression with a simple loop to keep the test readable.

@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Looks good.


Automated review by "github-manager-bot"

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/agent Agent runtime, pipeline, hooks, plan labels Aug 11, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR adds a model-agnostic normalization layer for tool call/result message sequences before they're sent to LLM providers. It addresses a real problem: various LLM APIs reject or misbehave when tool call/result transcripts have structural issues. The normalization is inserted at both model call sites in ReActAgent (reasoning and summarizing), and is backed by 98 comprehensive tests.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR adds a model-agnostic normalization layer for tool call/result message sequences before they're sent to LLM providers. It addresses a real problem: various LLM APIs reject or misbehave when tool call/result transcripts have structural issues. The normalization is inserted at both model call sites in ReActAgent (reasoning and summarizing), and is backed by 98 comprehensive tests.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR adds a model-agnostic normalization layer for tool call/result message sequences before they're sent to LLM providers. It addresses a real problem: various LLM APIs reject or misbehave when tool call/result transcripts have structural issues. The normalization is inserted at both model call sites in ReActAgent (reasoning and summarizing), and is backed by 98 comprehensive tests.

@Zbhbb
Zbhbb force-pushed the codex/tool-transcript-recovery branch 3 times, most recently from 101a6a5 to c9dccaa Compare August 18, 2026 06:59
@Zbhbb
Zbhbb force-pushed the codex/tool-transcript-recovery branch 4 times, most recently from b3d7da9 to f0d92bc Compare August 22, 2026 08:01
@Zbhbb
Zbhbb force-pushed the codex/tool-transcript-recovery branch from f0d92bc to 46f19a6 Compare August 24, 2026 03:05
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 bug Something isn't working

Projects

None yet

4 participants