agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]: ConversationCompactor#truncateToolUseArgs drops Msg id/metadata/timestamp/usage and ToolUseBlock content/metadata/state

Open littlebird69 opened this issue 1 month ago • 2 comments

Describe the bug

When tool-use arguments are truncated during compaction, both the message and the block are rebuilt from scratch, and most of their fields are lost.

  • ConversationCompactor.java#L701:
    return Msg.builder().role(msg.getRole()).name(msg.getName()).content(newBlocks).build();
    
    The rebuilt message gets a new random id, timestamp becomes "now", and metadata and usage are lost.
  • ConversationCompactor.java#L726:
    return ToolUseBlock.builder().id(tu.getId()).name(tu.getName()).input(newInput).build();
    
    The rebuilt block loses content (the raw argument JSON), metadata (for example Gemini's thoughtSignature, see ToolUseBlock.java#L37) and state.

Impact

  • Anything keyed on message id breaks once argument truncation runs. That includes UI deduplication, mapping persisted messages back to rendered ones, and snapshot/rollback comparisons. Application-level markers stored in message metadata disappear as well.
  • Gemini may reject replayed tool calls that no longer carry their thought signature.

To Reproduce

  1. Enable compaction with tool-argument truncation.
  2. Create an assistant message whose ToolUseBlock has a large argument, and set metadata on both the message and the block.
  3. Trigger compaction so that the message lands in the truncation range. The resulting message has a different id, a new timestamp, and no metadata.

Expected behavior

Truncation only shortens the argument values. Identity and metadata are preserved.

Suggested fix

Apply the same pattern as #3326, which did this for pruneToolResults:

Msg.builder().id(msg.getId()).role(msg.getRole()).name(msg.getName())
    .metadata(msg.getMetadata()).timestamp(msg.getTimestamp()).usage(msg.getUsage())
    .content(newBlocks).build();

ToolUseBlock.builder().id(tu.getId()).name(tu.getName()).input(newInput)
    .metadata(tu.getMetadata()).state(tu.getState())
    /* content: regenerate from newInput, or drop, so that it stays consistent with the truncated input */
    .build();

Environment

  • AgentScope-Java Version: 2.0.4 (unchanged since 2.0.3; also on main @035487e)
  • Java Version: 25
  • OS: any

littlebird69 avatar Oct 17 '26 11:10 littlebird69

我在 main 40a4a57ad 上独立复现了该问题:8 项本地 JUnit 检查中,消息身份/metadata/usage 保留和工具 metadata/state 保留两项失败,其余截断、保护窗口、原输入不变及参数 JSON 一致性对照通过。

计划只修复两个字段复制位置:使用 Msg.withContent 保留消息字段,重建 ToolUseBlock 时保留 metadata/state;维持清空旧 raw content,由现有参数解析器从截断后的 input 生成 JSON,避免旧 JSON 覆盖截断结果。

如果没有其他人在处理,我愿意跟进这个修复和回归测试。

xihongshichaojidan8 avatar Oct 19 '26 03:10 xihongshichaojidan8

感谢复现!方案我看没问题:Msg.withContent 会原样保留 id / metadata / timestamp / usage,而从截断后的 input 重新生成原始参数 JSON,正是我想要的一致性。欢迎跟进,PR 提了我可以帮忙 review。

littlebird69 avatar Oct 19 '26 03:10 littlebird69