Skip to content

[Bug]: Batched tool results are counted repeatedly in compaction pruning budgets #3537

Closed

Description

@pengmoubuaixuexi

Describe the bug
ConversationCompactor.pruneToolResults charges the token count of the entire Msg once for every eligible ToolResultBlock in that message. With multiple tool results in one TOOL message, this prematurely exhausts protectTokens and multiplies the accumulated minimumTokens budget.

This affects histories supplied with batched tool-result blocks, for example imported/custom histories. The default ReAct tool-message builder emits one result per message, so I am not claiming every ordinary parallel tool call hits this. Multi-result messages are supported by the content API and the existing pruning tests.

To Reproduce
On main c2ff087cae0e7ef363ea15023f848f5ad15d7ae7, call the public ConversationCompactor.compactIfNeeded(...) with summarization disabled via triggerMessages(0) and triggerTokens(Integer.MAX_VALUE):

List<ContentBlock> blocks = IntStream.range(0, 6)
    .<ContentBlock>mapToObj(i -> ToolResultBlock.of("call-" + i, "execute",
        TextBlock.builder().text("x".repeat(1_000)).build()))
    .toList();
List<Msg> messages = List.of(Msg.builder().role(MsgRole.TOOL).content(blocks).build());
CompactionConfig config = CompactionConfig.builder()
    .triggerMessages(0).triggerTokens(Integer.MAX_VALUE)
    .prune(CompactionConfig.PruneConfig.builder()
        .protectTokens(0).minimumTokens(3_000).maxOutputChars(100)
        .excludedTools(Set.of()).build())
    .build();
var result = compactor.compactIfNeeded(null, messages, config, "agent", "session").block();
// Expected: empty (about 2,500 tokens are below minimumTokens).
// Actual: present, with all six outputs pruned.

A second case uses protectTokens(700), minimumTokens(1): splitting the same blocks across six TOOL messages preserves the last two outputs, but batching them preserves only the last one. The penultimate 1,000-character result becomes a 100-character head/tail preview.

Expected behavior
Each eligible result contributes its own estimate once. Unrelated sibling results must not inflate that result's protection or pruning budget. Both representations should make equivalent pruning decisions in these cases.

Error messages / validation
Two new regression cases exercise the public method in ConversationCompactorPruneTest. On unmodified production code: 7 tests, 2 assertion failures (the new cases), 0 errors, 0 skipped. Existing 5 tests pass. No model or external service is called.

mvn -B -T1 -pl agentscope-harness -am -Dtest=ConversationCompactorPruneTest -Dsurefire.failIfNoSpecifiedTests=false -Dspotless.skip=true test

Environment

  • AgentScope Java: 2.1.0-SNAPSHOT, main c2ff087
  • Java: 17.0.20.1
  • Windows; Maven 3.9.12

Additional context / ownership
Root cause: TokenCounterUtil.calculateToken(List.of(msg)) is inside the per-result loop. I checked the related #2971 and #2990 patches and the merged #3326 change: they address different compaction behavior, not this per-block accounting.

I am implementing a minimal fix and will submit the regression tests in a linked PR in this work session. Please assign this issue to @pengmoubuaixuexi if assignment is used for coordination. AI assistance was used for source review and regression development; the behavior was reproduced locally through the public API.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions