Repository navigation
fix(harness): clamp model-supplied limits in session_search / session_history #3382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+138
−3
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...-harness/src/test/java/io/agentscope/harness/agent/tool/SessionSearchToolCeilingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * 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.tool; | ||
|
|
||
| import static io.agentscope.harness.agent.tool.ToolResultAssertions.assertText; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import io.agentscope.core.agent.RuntimeContext; | ||
| import io.agentscope.core.message.Msg; | ||
| import io.agentscope.core.message.MsgRole; | ||
| import io.agentscope.core.message.TextBlock; | ||
| import io.agentscope.core.message.ToolResultState; | ||
| import io.agentscope.core.session.SessionEvent; | ||
| import io.agentscope.core.session.SessionKey; | ||
| import io.agentscope.core.util.JsonUtils; | ||
| import io.agentscope.harness.agent.filesystem.local.LocalFilesystem; | ||
| import io.agentscope.harness.agent.session.WorkspaceSessionLogStore; | ||
| import java.nio.file.Path; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| /** | ||
| * Regression tests for the model-supplied result-limit clamp (#3270): {@code session_search} | ||
| * and {@code session_history} are excluded from {@code ToolResultEvictionConfig} on the | ||
| * "bounded results" rationale, so an unbounded model-supplied limit bypasses every downstream | ||
| * trim. | ||
| */ | ||
| class SessionSearchToolCeilingTest { | ||
|
|
||
| @TempDir Path directory; | ||
| private final SessionKey key = new SessionKey("u", "agent-a", "session-1"); | ||
| private final RuntimeContext rc = RuntimeContext.builder().userId("u").build(); | ||
|
|
||
| private SessionSearchTool seedMessages(int count) { | ||
| var store = new WorkspaceSessionLogStore(new LocalFilesystem(directory)); | ||
| var log = store.open(key, rc); | ||
| var writer = log.acquire("owner", Duration.ofMinutes(2)); | ||
| var events = new ArrayList<SessionEvent>(); | ||
| for (int i = 1; i <= count; i++) { | ||
| var message = | ||
| Msg.builder() | ||
| .id("m" + i) | ||
| .role(MsgRole.USER) | ||
| .content(TextBlock.builder().text("needle item " + i).build()) | ||
| .build(); | ||
| events.add( | ||
| new SessionEvent( | ||
| 1, | ||
| "m" + i, | ||
| i, | ||
| i, | ||
| "message/user", | ||
| "run", | ||
| "turn", | ||
| true, | ||
| JsonUtils.getJsonCodec().toJson(Map.of("message", message)))); | ||
| } | ||
| log.commit(writer, "messages", 0, events); | ||
| log.release(writer); | ||
| return new SessionSearchTool(new WorkspaceSessionLogStore(new LocalFilesystem(directory))); | ||
| } | ||
|
|
||
| private static int countMatches(String json) { | ||
| return (json.length() - json.replace("needle item", "").length()) / "needle item".length(); | ||
| } | ||
|
|
||
| @Test | ||
| void sessionSearchClampsModelSuppliedMaxResults() { | ||
| var tool = seedMessages(150); | ||
| var out = | ||
| assertText( | ||
| tool.sessionSearch(rc, "needle", "agent-a", 100000), | ||
| ToolResultState.SUCCESS); | ||
| assertEquals(100, countMatches(out)); | ||
| } | ||
|
|
||
| @Test | ||
| void sessionHistoryClampsModelSuppliedLastN() { | ||
| var tool = seedMessages(150); | ||
| var out = | ||
| assertText( | ||
| tool.sessionHistory(rc, "agent-a", "session-1", 100000), | ||
| ToolResultState.SUCCESS); | ||
| assertEquals(100, countMatches(out)); | ||
| } | ||
|
|
||
| @Test | ||
| void sessionHistoryBoundaryLastN99And101() { | ||
| var tool = seedMessages(150); | ||
| assertEquals( | ||
| 99, | ||
| countMatches( | ||
| assertText( | ||
| tool.sessionHistory(rc, "agent-a", "session-1", 99), | ||
| ToolResultState.SUCCESS))); | ||
| assertEquals( | ||
| 100, | ||
| countMatches( | ||
| assertText( | ||
| tool.sessionHistory(rc, "agent-a", "session-1", 101), | ||
| ToolResultState.SUCCESS))); | ||
| } | ||
|
|
||
| @Test | ||
| void defaultLimitStillApplies() { | ||
| var tool = seedMessages(15); | ||
| var out = | ||
| assertText( | ||
| tool.sessionSearch(rc, "needle", "agent-a", null), ToolResultState.SUCCESS); | ||
| assertEquals(10, countMatches(out)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stated rationale only holds for
session_search.ToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLSlistsmemory_search,memory_getandsession_search—session_historyis not in it, so oversized history results are still evictable downstream. The clamp itself is still the right fix here (bounding at the source is better than evicting after the fact), but the comment claiming "nothing downstream trims" an oversizedsession_historyresult is inaccurate and will mislead the next reader.Could you either drop
session_historyfrom that sentence, or add it toDEFAULT_EXCLUDED_TOOLSif the intent is for the clamp to be the only bound?