Skip to content

fix(harness): preserve tool result metadata and state when pruning - #3326

Merged
Buktal merged 1 commit into
agentscope-ai:mainfrom
birdie7761:fix/prune-tool-result-metadata
Oct 13, 2026
Merged

Buktal merged 1 commit into
agentscope-ai:mainfrom
birdie7761:fix/prune-tool-result-metadata

Conversation

@birdie7761

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.4-SNAPSHOT (based on main @ 9c8b10d4e)

Description

ConversationCompactor.pruneToolResults trims old oversized tool results to a head+tail preview. When rebuilding the trimmed block it copies only id and name, which silently discards the rest of the block's identity:

  • metadata — wiped. Markers that downstream code branches on are lost: the eviction marker agentscope.tool_result_evicted (written by ToolResultEvictionMiddleware, fix: preserve skill isolation and tool result history #2319) and the suspension marker agentscope_suspended (consumed by ToolResultBlock.isSuspended() and ReActAgent's suspend/resume matching), plus any integrator-supplied metadata.
  • state — reset to RUNNING (the ToolResultBlock constructor default for null), so a pruned ERROR result is rewritten as RUNNING. MessageUtils result-state maps and event-side state reporting observe the corrupted value.
  • Msg.usage — dropped by the message rebuild.

The eviction middleware already follows a lossless rebuild discipline (withPlaceholder preserves metadata and adds its own marker). This PR applies the same discipline to prune: the rebuilt block now copies metadata and state from the original block, and the rebuilt Msg keeps usage. Only the output text is replaced with the preview.

Context size is strictly reduced, never grown — the preserved fields were already in the conversation before pruning, and formatters do not render block metadata into the model prompt. This matters once lightweight pruning results reach the working AgentState (e.g. once #2970/#2971 is addressed): a persisted-and-replayed pruned block currently loses every field downstream consumers branch on.

Compatibility

  • No serialization/format change: metadata, state, usage are existing @JsonProperty fields of ToolResultBlock/Msg.
  • No context growth: only the output text shrinks relative to the pre-prune message.
  • Sessions persisted by older builds read back unchanged (those blocks already carry the default values).

Tests

New ConversationCompactorPruneTest (5 cases). The preservation case was red-verified against pre-fix code — verbatim failure output:

org.opentest4j.AssertionFailedError: expected: <{agentscope.tool_result_evicted=true, agentscope_suspended=true, custom_key=custom_value}> but was: <{}>
Test Asserts
pruneToolResults_preservesMetadataStateAndUsageWhenTrimming metadata map, isSuspended(), state=ERROR, block id/name and Msg id/metadata/timestamp/usage all survive; preview actually trimmed
pruneToolResults_multiBlockMessagePrunesOnlyOversizedBlock a message carrying several results is rebuilt with only the oversized block replaced; untouched blocks keep their original reference (pinned via assertSame, so the rebuild is surgical rather than destructive)
pruneToolResults_skipsExcludedTools excluded tool is never pruned (original list reference returned)
pruneToolResults_returnsOriginalWhenBelowMinimum below-threshold conversation returned unchanged
pruneToolResults_handlesBlockWithoutMetadataOrState metadata-less block prunes without failure, defaults intact

Regression: ConversationCompactorTest 7/7, ToolResultEvictionMiddlewareTest green, full agentscope-harness suite green, spotless:check clean.

Out of scope

The truncateArgs rebuilds in the same class keep even less (truncateToolUseArgs drops the message id/metadata/usage; truncateToolUseBlock drops ToolUseBlock metadata). They are masked by the same #2970 discard path and left for a follow-up to keep this change single-purpose.

One pre-existing interaction edge is worth recording: an eviction placeholder larger than prune's maxOutputChars is itself prunable, and the head+tail preview may cut the embedded read_file pointer text. This PR preserves the eviction marker through that rewrite, but cannot recover the pointer text itself; in practice the default placeholder (~4K chars) only hits this under a custom maxOutputChars at or above that size.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (PruneConfig has no user-facing docs today; the method Javadoc documents the preservation contract)
  • Code is ready for review

@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.

Summary

pruneToolResults rebuilt the pruned ToolResultBlock from id/name/output only, so the eviction/suspension markers in metadata and the execution state were silently dropped, and the rebuilt Msg dropped usage. The fix restores all three and the new ConversationCompactorPruneTest pins each preserved field plus the below-minimum / excluded-tool / multi-block paths. Correctness-wise I have no objection to the change itself.

The only blocker is CI: build (ubuntu-latest) fails in service-dataplane on a test unrelated to this diff (windows cancelled). Rebase or re-run CI and this is good to go.


Automated review by github-manager-bot

…ning

pruneToolResults rebuilt each pruned ToolResultBlock with only id and
name, silently discarding metadata and resetting the execution state to
RUNNING (the constructor default). Eviction markers
(agentscope.tool_result_evicted), suspension markers
(agentscope_suspended) and error states were wiped, so a pruned block no
longer matched the consumers that branch on them
(ToolResultEvictionMiddleware.isEvicted, ToolResultBlock.isSuspended).
The carrying message also lost its usage.

Copy metadata and state from the original block and keep usage on the
rebuilt Msg, mirroring the lossless rebuild discipline of
ToolResultEvictionMiddleware (agentscope-ai#2319). Only the output text is replaced
with the head+tail preview.
@birdie7761
birdie7761 force-pushed the fix/prune-tool-result-metadata branch from d616466 to cfcd421 Compare September 28, 2026 05:35
@codecov

codecov Bot commented Sep 28, 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.

Summary

Re-review at cfcd4214. I diffed this head against the head I reviewed before (d6164667): the only change is the javadoc note that a preserved marker describes the pre-prune payload, so consumers must not assume the block text is the full payload — exactly the caveat I would have asked for, and it makes the agentscope.tool_result_evicted pointer semantics explicit instead of leaving them to be discovered.

The code itself is unchanged and still correct: ToolResultBlock's constructor defensive-copies metadata (Map.copyOf) and defaults a null state, so sharing tr.getMetadata() / tr.getState() with the pruned copy is safe, and Msg now carries usage through the rebuild. ConversationCompactorPruneTest pins every preserved field plus the excluded-tool, below-minimum and multi-block paths.

CI is green at this head (build (ubuntu-latest), build (windows-latest), codecov/patch, license/cla), which was the only open blocker in my previous pass. Approving.

Findings

None.


Automated review by github-manager-bot

@Buktal Buktal 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

@Buktal
Buktal merged commit d87c96d into agentscope-ai:main Oct 13, 2026
6 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…3326)

Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit d87c96d)
Backport-review-patch: 7dc81b5b90e0d06917f913aa09dbcaeea70dd88f
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
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.

3 participants