[Bug]: ConversationCompactor#truncateToolUseArgs drops Msg id/metadata/timestamp/usage and ToolUseBlock content/metadata/state
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:
The rebuilt message gets a new random id,return Msg.builder().role(msg.getRole()).name(msg.getName()).content(newBlocks).build();timestampbecomes "now", andmetadataandusageare lost. - ConversationCompactor.java#L726:
The rebuilt block losesreturn ToolUseBlock.builder().id(tu.getId()).name(tu.getName()).input(newInput).build();content(the raw argument JSON),metadata(for example Gemini'sthoughtSignature, see ToolUseBlock.java#L37) andstate.
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
- Enable compaction with tool-argument truncation.
- Create an assistant message whose
ToolUseBlockhas a large argument, and setmetadataon both the message and the block. - 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
我在 main 40a4a57ad 上独立复现了该问题:8 项本地 JUnit 检查中,消息身份/metadata/usage 保留和工具 metadata/state 保留两项失败,其余截断、保护窗口、原输入不变及参数 JSON 一致性对照通过。
计划只修复两个字段复制位置:使用 Msg.withContent 保留消息字段,重建 ToolUseBlock 时保留 metadata/state;维持清空旧 raw content,由现有参数解析器从截断后的 input 生成 JSON,避免旧 JSON 覆盖截断结果。
如果没有其他人在处理,我愿意跟进这个修复和回归测试。
感谢复现!方案我看没问题:Msg.withContent 会原样保留 id / metadata / timestamp / usage,而从截断后的 input 重新生成原始参数 JSON,正是我想要的一致性。欢迎跟进,PR 提了我可以帮忙 review。