Skip to content

fix(anthropic): preserve native thinking and parallel tool history - #3740

Open
Buktal wants to merge 8 commits into
agentscope-ai:mainfrom
Buktal:codex/anthropic-native-replay
Open

Buktal wants to merge 8 commits into
agentscope-ai:mainfrom
Buktal:codex/anthropic-native-replay

Conversation

@Buktal

@Buktal Buktal commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

AgentScope-Java Version

2.1.0-SNAPSHOT

Description

Summary

Anthropic thinking signatures can be lost or attached to the wrong merged block, while parallel tool calls are rewritten as serial exchanges. Carry native content-block indices/completion and stop reasons through parsing and accumulation, then replay signed thinking blocks and parallel tool batches in their native shape.

- assistant(call A), user(result A), assistant(call B), user(result B)

+ assistant(call A, call B), user(result A, result B)

- thinking A + thinking B, signature B

+ thinking A/signature A, text, thinking B/signature B

Reuse existing accumulators and metadata. Keep real-time event aggregation and text middleware replacements/deletions/additions working, and remove transient block bookkeeping from stored message metadata. Completed native tool blocks remain distinguishable from empty starts cut off by the output limit.

Parent issue: #3737. Depends on #3739 (core tool integrity, merged).

This Draft has been refreshed after #3739 merged and now includes merge commit 2ae4cae26729b75abc36d5428c9e3a4b8b81a797 with the current main. Its diff relative to current main contains only the native replay changes. The native-only incremental comparison remains available here. EN/ZH context documentation is included.

Evidence

Tests cover SDK-shaped streaming/nonstream responses through real ReAct accumulation, handler execution, and the next Anthropic request. Two thinking blocks retain their own signatures and positions; complete and incomplete tool starts in the same truncated response are handled separately. Formatter tests verify one assistant tool batch and one user result batch.

Dependency-plus-native replay validation passes: 126 tests, 0 failures/errors, 1 existing skip; Spotless passed. After resolving the conflict against current main, mvn -B -T1 -DforkCount=1 clean verify passes: 11,377 tests, 0 failures, 0 errors, 242 skipped, across 75 Maven test-module summaries. The serial flags avoid the Windows fork-start resource failure seen with the default parallel run; they do not change production code.

mvn -B -T1C -pl agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic -am test spotless:check '-Dtest=AnthropicMessageConverterTest,AnthropicResponseParserTest,AnthropicReplayIntegrityTest,ReasoningContextTest,TruncatedToolCallIntegrityTest,ToolCallsAccumulatorTest,JsonUtilsTest' -Dsurefire.failIfNoSpecifiedTests=false

ZCode anthropic-reasoning-metadata.ts preserves per-block provider metadata and reasoning-history-normalization.ts checks signature compatibility. DeepSeek Harness replay.ts aligns replay metadata to content-block indices. SDK event fixtures and local integration tests support ASJ's specific index/completion implementation.

Merge Danger

Door: reversible code change. Blast radius: Anthropic request projection and indexed response history; providers without native indices retain the existing aggregation path. Signed thinking uses native replay; foreign thinking without an Anthropic signature retains the existing text fallback. No persistent schema migration. Review together with the core integrity contract, while keeping both diffs separate.

Checklist

  • Code formatting verified with Spotless.

  • Relevant tests pass on this PR's isolated source snapshot and declared dependency.

  • Javadoc comments follow project conventions.

  • Related EN/ZH documentation updated.

  • Code is ready for review.

@mintlify

mintlify Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 14ba3fd.

Approve preview

…put cutoff

Capture redacted_thinking payloads in the response parser and replay them as
native redacted blocks instead of dropping them from history.

Track every observed native block index, including metadata-only start/stop
events, so a completed call before an empty block at the output cutoff stays
executable instead of being rejected as malformed.
Keep native parallel tool-result batches while preserving is_error for failed, denied, and interrupted tool calls. Add coverage for mixed success and failure result batches.
Resolve conflicts with agentscope-ai#3852 in AnthropicResponseParser(.Test):
keep this branch's native thinking signature/redacted-thinking replay
and content-block metadata, and adopt main's finishReason handling
(StopReason::asString wire value) in parseMessage and message_delta.
@jujn jujn added the P2 重要不紧急 label Nov 3, 2026
jujn pushed a commit that referenced this pull request Nov 8, 2026
## Problem

Parallel client tool calls are rewritten into alternating assistant/user
exchanges, and individual tool results are emitted as separate user
messages. This changes the original tool batch structure and does not
follow Anthropic's documented parallel result format. DeepSeek's
official endpoint rejects separate result messages with HTTP 400 in the
direct test below; the gateway-specific 502 reported in #4018 has not
been reproduced.

## Changes

- Preserve all tool uses in their original assistant message.
- Group consecutive client tool results into one user message, placing
results before accompanying text or media.
- Keep server tool blocks inline and stop result grouping at
conversation boundaries.
- Update formatter/model comments and regression tests for
separate/combined results, mixed text, multiple turns, and assistant
messages carrying client tool results.
- Use an explicit batch end index without rewinding the loop cursor;
name accompanying content `trailingContent`.

Fixes #4018.

Related: #3740 includes broader native thinking/history replay changes;
this PR focuses on parallel tool batch formatting.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants