|
| 1 | +/* |
| 2 | + * Copyright 2024-2026 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.agentscope.harness.agent.memory.compaction; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | + |
| 23 | +import io.agentscope.core.message.ContentBlock; |
| 24 | +import io.agentscope.core.message.Msg; |
| 25 | +import io.agentscope.core.message.MsgRole; |
| 26 | +import io.agentscope.core.message.TextBlock; |
| 27 | +import io.agentscope.core.message.ToolResultBlock; |
| 28 | +import io.agentscope.core.model.Model; |
| 29 | +import io.agentscope.harness.agent.memory.MemoryFlushManager; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +class ConversationCompactorMixedBudgetTest { |
| 36 | + private final ConversationCompactor compactor = |
| 37 | + new ConversationCompactor(mock(Model.class), mock(MemoryFlushManager.class)); |
| 38 | + |
| 39 | + @Test |
| 40 | + void nonToolTextDoesNotInflatePrunableResultBudget() { |
| 41 | + assertBelowMinimum(TextBlock.builder().text("n".repeat(10_000)).build(), Set.of()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void excludedSiblingDoesNotInflatePrunableResultBudget() { |
| 46 | + assertBelowMinimum(result("excluded", "read_file", 10_000), Set.of("read_file")); |
| 47 | + } |
| 48 | + |
| 49 | + private void assertBelowMinimum(ContentBlock sibling, Set<String> excludedTools) { |
| 50 | + ToolResultBlock eligible = result("eligible", "execute", 1_000); |
| 51 | + List<ContentBlock> blocks = List.of(sibling, eligible); |
| 52 | + Msg original = |
| 53 | + Msg.builder() |
| 54 | + .id("batch") |
| 55 | + .role(MsgRole.TOOL) |
| 56 | + .content(blocks) |
| 57 | + .metadata(Map.of("source", "review")) |
| 58 | + .build(); |
| 59 | + CompactionConfig config = |
| 60 | + CompactionConfig.builder() |
| 61 | + .triggerTokens(Integer.MAX_VALUE) |
| 62 | + .triggerMessages(0) |
| 63 | + .prune( |
| 64 | + CompactionConfig.PruneConfig.builder() |
| 65 | + .maxOutputChars(100) |
| 66 | + .minimumTokens(3_000) |
| 67 | + .protectTokens(0) |
| 68 | + .excludedTools(excludedTools) |
| 69 | + .build()) |
| 70 | + .build(); |
| 71 | + |
| 72 | + assertTrue( |
| 73 | + compactor |
| 74 | + .compactIfNeeded(null, List.of(original), config, "agent", "session") |
| 75 | + .block() |
| 76 | + .isEmpty()); |
| 77 | + assertEquals("batch", original.getId()); |
| 78 | + assertEquals(Map.of("source", "review"), original.getMetadata()); |
| 79 | + assertSame(sibling, original.getContent().get(0)); |
| 80 | + assertSame(eligible, original.getContent().get(1)); |
| 81 | + assertEquals("x".repeat(1_000), ((TextBlock) eligible.getOutput().get(0)).getText()); |
| 82 | + } |
| 83 | + |
| 84 | + private static ToolResultBlock result(String id, String name, int chars) { |
| 85 | + return ToolResultBlock.of(id, name, TextBlock.builder().text("x".repeat(chars)).build()); |
| 86 | + } |
| 87 | +} |
0 commit comments