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

[Bug]: session_search maxResults is unbounded — model-supplied limit bypasses result bounding while session_search is excluded from tool-result eviction

Open aMetric opened this issue 2 months ago • 3 comments

Describe the bug

session_search accepts a model-controlled maxResults with no upper bound — the same hole #3266 described for memory_search and that PR #3267 just closed for that tool (hard ceiling MAX_RESULTS_CEILING = 100, clamped via Math.min).

SessionSearchTool.java:77:

int limit = maxResults != null && maxResults > 0 ? maxResults : 10;

A model that passes maxResults=100000 scans every session entry and collects matches with only a 200-char preview cap per entry — the result count itself is unbounded.

Two aggravating factors:

  1. session_search is in ToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLS (ToolResultEvictionConfig.java:66-73) with the rationale "bounded results", so ToolResultEvictionMiddleware.maybeEvict() returns early for it regardless of result size — nothing downstream trims an oversized result.
  2. SessionSearchTool.searchInSessionFile (:269-308) loads and parses the full SessionTree of every session file while collecting matches, so a huge maxResults also amplifies I/O, not just output size.

Impact

Long-running agents with many accumulated sessions (agents/<id>/sessions/*.log.jsonl) can get a single oversized session_search tool result injected straight into the next model call — context overflow on exactly the long-lived agents the harness targets. Medium severity; no data corruption.

Expected behavior

Clamp the model-supplied maxResults to a hard ceiling the way #3267 does for memory_search (e.g. Math.min(maxResults, 100)), and state the bound in the @ToolParam description ("default: 10, max: 100") so the model is not surprised by a silent clamp.

Environment

  • AgentScope-Java Version: 2.0.4-SNAPSHOT (main)
  • Java Version: 21
  • OS: macOS

Additional context

Raised in the review of #3267 (inline comment on ToolResultEvictionConfig.java:61): the eviction-exclusion rationale "bounded results" is only honest if both search tools bound their model-supplied limits. Happy to submit a PR mirroring the #3267 fix (clamp + @ToolParam description + regression test).

aMetric avatar Sep 23 '26 23:09 aMetric

Cross-linking: this was flagged during the review of #3267 (PR for #3266) — the eviction-exclusion rationale in ToolResultEvictionConfig.java:61 reads "bounded results", which only stays honest if both search tools clamp their model-supplied limits. I can pick this up after #3267 lands, mirroring its fix: Math.min clamp + (default: 10, max: 100) in the @ToolParam description + a regression test.

aMetric avatar Sep 23 '26 23:09 aMetric

Issue Evaluation

Category: bug (area/harness) | Status: Confirmed | Severity: medium

Verified against main @ 6e6406610 (engine-assisted read of agentscope-harness). All four claims hold; line references in the report are accurate as filed.

Claim-by-claim check

Claim Result Evidence
session_search accepts a model-supplied maxResults with no upper bound TRUE SessionSearchTool.java:71 (required = false), :77 int limit = maxResults != null && maxResults > 0 ? maxResults : 10; — only a > 0 sanity check, no clamp
session_search is excluded from tool-result eviction, so nothing downstream trims an oversized result TRUE ToolResultEvictionConfig.java:66-73 contains "session_search"; ToolResultEvictionMiddleware.maybeEvict() (middleware/ToolResultEvictionMiddleware.java:188-192) returns early on the excluded-name check before the size check at :194
Huge maxResults amplifies I/O, not just output size TRUE SessionSearchTool.java:279-283 constructs a SessionTree per session file and calls tree.load(), which reads and JSON-parses every line (SessionTree.java:245-266, parseJsonlEntries 659-667); the outer loop :84-89 only breaks once results.size() >= limit
The ceiling cited from #3267 (MAX_RESULTS_CEILING = 100, Math.min) is not on main yet TRUE gh pr view 3267 → state OPEN, mergedAt: null; MemorySearchTool.java:64-89 on main still has no maxResults parameter at all

Root cause. Same shape as #3266, different tool: the model controls the result count, the tool trusts it verbatim, and the eviction layer that would otherwise cap an oversized tool result is deliberately switched off for this tool name — the javadoc at ToolResultEvictionConfig.java:61 claims "bounded previews" for the excluded search tools, which the session_search code path does not guarantee.

Impact. A single session_search call with e.g. maxResults=100000 on a long-running agent can inject an arbitrarily large tool result straight into the next model call — context overflow on exactly the long-lived agents the harness targets. Medium severity; no data corruption, no security boundary crossed.

Fix shape (as proposed, and workable). Mirror #3267 exactly: shared hard ceiling (Math.min(maxResults, 100)) + per-file/total early break so SessionTree.load() is skipped once the limit is reached + @ToolParam description changed to "default: 10, max: 100" so a clamp is not silent to the model.

Two notes for whoever implements it.

  1. session_history has the identical unbounded pattern at SessionSearchTool.java:186 (lastN != null && lastN > 0 ? lastN : 20). Fixing only session_search leaves the same hole one method down in the same file.
  2. If #3267 and this land separately, the ceiling constant should be shared rather than duplicated, so the eviction-list javadoc has one truth to point at.

No PR from us. The reporter explicitly offered to submit one against this exact shape, so a competing automated PR would duplicate community work.

Testability: yes, no external services needed — write several *.log.jsonl + context files with repeated matches, call sessionSearch(..., maxResults=100000) and assert (a) results <= 100, (b) the description text advertises the max, (c) session files beyond the limit are never parsed. No test in the repo today invokes sessionSearch/maxResults at all, so nothing currently guards this.


Automated evaluation by github-manager-bot

oss-maintainer avatar Sep 24 '26 00:09 oss-maintainer

Hi, I'd like to work on this. Following the #3267/#3267 pattern (MAX_RESULTS_CEILING = 100, Math.min clamp), I'll add the same ceiling to SessionSearchTool's model-supplied maxResults, plus regression tests for the clamp and the excluded-from-eviction interplay. PR coming shortly.

aiyili avatar Oct 01 '26 15:10 aiyili