From 730a6b4e36e79f555778ef9469c51c4b1351b647 Mon Sep 17 00:00:00 2001 From: guoyuchen <2273345925@qq.com> Date: Sun, 29 Nov 2026 14:05:24 +0800 Subject: [PATCH] fix(harness): count tool call arguments once in context estimates --- .../agent/context/ContextTokenEstimator.java | 3 +- .../context/ContextTokenEstimatorTest.java | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 agentscope-harness/src/test/java/io/agentscope/harness/agent/context/ContextTokenEstimatorTest.java diff --git a/agentscope-harness/src/main/java/io/agentscope/harness/agent/context/ContextTokenEstimator.java b/agentscope-harness/src/main/java/io/agentscope/harness/agent/context/ContextTokenEstimator.java index d2983b384a..6462fb18a4 100644 --- a/agentscope-harness/src/main/java/io/agentscope/harness/agent/context/ContextTokenEstimator.java +++ b/agentscope-harness/src/main/java/io/agentscope/harness/agent/context/ContextTokenEstimator.java @@ -60,8 +60,7 @@ private static long block(ContentBlock block) { return 12 + text(use.getId()) + text(use.getName()) - + text(JsonUtils.getJsonCodec().toJson(use.getInput())) - + text(use.getContent()); + + text(JsonUtils.resolveToolCallArgsJson(use)); } if (block instanceof ToolResultBlock result) { long tokens = 12 + text(result.getId()) + text(result.getName()); diff --git a/agentscope-harness/src/test/java/io/agentscope/harness/agent/context/ContextTokenEstimatorTest.java b/agentscope-harness/src/test/java/io/agentscope/harness/agent/context/ContextTokenEstimatorTest.java new file mode 100644 index 0000000000..374840bb36 --- /dev/null +++ b/agentscope-harness/src/test/java/io/agentscope/harness/agent/context/ContextTokenEstimatorTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2024-2026 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.agentscope.harness.agent.context; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import io.agentscope.core.message.Msg; +import io.agentscope.core.message.MsgRole; +import io.agentscope.core.message.ThinkingBlock; +import io.agentscope.core.message.ToolUseBlock; +import io.agentscope.core.middleware.ModelCallInput; +import io.agentscope.core.model.Model; +import io.agentscope.core.util.JsonUtils; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.NullAndEmptySource; +import org.junit.jupiter.params.provider.ValueSource; + +class ContextTokenEstimatorTest { + private static final Map INPUT = Map.of("content", "".repeat(4000)); + + @Test + void streamedArgumentsAreCountedOnce() { + String raw = JsonUtils.getJsonCodec().toJson(INPUT); + long expected = 35 + (long) Math.ceil(raw.length() / 2.5); + assertEquals(expected, estimate(tool(INPUT, null))); + assertEquals(expected, estimate(tool(INPUT, raw))); + assertEquals(expected, estimate(tool(Map.of(), raw))); + } + + @ParameterizedTest + @NullAndEmptySource + @ValueSource(strings = {"{}", " { } ", "{broken", "[]"}) + void missingEmptyOrInvalidRawArgumentsFallBackToInput(String raw) { + assertEquals(estimate(tool(INPUT, null)), estimate(tool(INPUT, raw))); + } + + @Test + void validRawArgumentsTakePrecedenceOverParsedInput() { + String raw = "{\"path\":\"actual.html\"}"; + assertEquals(estimate(tool(Map.of(), raw)), estimate(tool(INPUT, raw))); + } + + @Test + void malformedArgumentsAreSanitized() { + var malformed = + ToolUseBlock.builder() + .id("tool-1") + .name("write_file") + .input(INPUT) + .content("{broken") + .metadata(Map.of(ToolUseBlock.METADATA_MALFORMED_ARGUMENTS, true)) + .build(); + assertEquals(estimate(tool(Map.of(), "{}")), estimate(malformed)); + } + + @Test + void thinkingRemainsInTheEstimate() { + var tool = tool(INPUT, null); + String thinking = "think".repeat(1000); + var msg = + Msg.builder() + .role(MsgRole.ASSISTANT) + .name("assistant") + .content(ThinkingBlock.builder().thinking(thinking).build(), tool) + .build(); + assertEquals(estimate(tool) + 2000, estimate(msg)); + } + + private ToolUseBlock tool(Map input, String content) { + return ToolUseBlock.builder() + .id("tool-1") + .name("write_file") + .input(input) + .content(content) + .build(); + } + + private long estimate(ToolUseBlock tool) { + return estimate( + Msg.builder().role(MsgRole.ASSISTANT).name("assistant").content(tool).build()); + } + + private long estimate(Msg msg) { + var input = new ModelCallInput(List.of(msg), List.of(), null, (Model) null); + return ContextTokenEstimator.approximate().estimate(input).tokens(); + } +}