[Bug]: memory_search returns unbounded results — no maxResults, no per-line truncation, while excluded from tool-result eviction
Describe the bug
memory_search returns every matching line of every memory file in full, with no result-count cap and no per-line truncation. Two code paths that were written assuming this tool returns bounded output are therefore left unprotected:
-
MemorySearchTool.keywordSearch(agentscope-harness/src/main/java/io/agentscope/harness/agent/tool/MemorySearchTool.java:64-89) iterates allMEMORY.md+memory/*.mdfiles, appends each matching line verbatim (Source: <file>#<line>: <full line>), and only stops when the files run out. There is nomaxResultsequivalent — compareSessionSearchTool.sessionSearch, which caps atmaxResults(default 10) and truncates each preview to 200 chars (SessionSearchTool.java:77, 289-290). -
ToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLS(agentscope-harness/.../memory/compaction/ToolResultEvictionConfig.java:66-73) excludesmemory_searchfrom tool-result eviction, with the stated rationale "small/paginated results" (:61). Since the eviction layer is bypassed, an oversizedmemory_searchresult is passed straight into the next model call.
Additionally, the docs promise a cap that the code does not implement: docs/v2/en/docs/harness/memory.md:236 and docs/v2/zh/docs/harness/memory.md:235 both say memory_search returns "up to 30 hits" — there is no 30 (or any) limit in the implementation.
Impact
A long-lived agent accumulates many memory/YYYY-MM-DD.md files. A broad query (e.g. a keyword that appears in a header or boilerplate line repeated across daily ledgers) can return hundreds of untruncated lines, blowing up the tool-result content and overflowing the model context window. Because the tool is on the eviction exclusion list, nothing downstream trims it.
To Reproduce
- Create a workspace with
memory/2026-09-01.md…memory/2026-09-20.md(20 files), each containing a line## Memory Flush — 2026-09-XXplus a long content line (~1000 chars) mentioning the same keyword. - Let the agent call
memory_search(query="<keyword>")— e.g. any keyword present in the repeated header line. - Result: every header line and every long content line of all 20 files is concatenated verbatim into one tool result (observed on
main@63a6f616).
Expected behavior
- A default result cap (the docs already promise 30) and/or a
maxResultsparameter likesession_search. - Per-line preview truncation (like
session_search's 200-char preview), withmemory_getremaining the tool for fetching full context around a hit. - Either implement the cap, or — if unbounded output is intended — remove
memory_searchfromDEFAULT_EXCLUDED_TOOLS/ fix the docs, so the eviction layer can actually bound it.
Environment
- AgentScope-Java Version: 2.0.3-SNAPSHOT (main @ 63a6f616)
- Java Version: 21
- OS: macOS
Additional context
Happy to submit a PR — proposed shape: maxResults tool parameter (default 30, aligning with the docs) + per-line truncation at a fixed char budget, plus a regression test. The eviction exclusion list would then match the documented behavior.
Issue Evaluation
Category: bug (area/harness) | Status: Confirmed | Severity: medium-high
Verified against main @ 63a6f6168. All four claims in the report hold — the line references are accurate as filed.
Claim-by-claim check
| Claim | Result | Evidence |
|---|---|---|
keywordSearch emits every matching line verbatim, no cap |
TRUE | MemorySearchTool.java:64-89 — for (relativePath : memoryPaths) → for (i < lines.length) → results.add("Source: %s#%d: %s", relativePath, i + 1, lines[i]); maxResults does not appear anywhere in the file |
session_search bounds itself, memory_search does not |
TRUE | SessionSearchTool.java:77 limit = maxResults > 0 ? maxResults : 10; :85-86 stops at the limit; :289-290 truncates the preview to 200 chars + ... |
memory_search is excluded from tool-result eviction |
TRUE | ToolResultEvictionConfig.java:66-73; the javadoc at :61 justifies it as "small/paginated results" |
| Docs promise a cap that is not implemented | TRUE | docs/v2/en/docs/harness/memory.md:236 "up to 30 hits"; docs/v2/zh/docs/harness/memory.md:235 "最多返回 30 条命中" |
Root cause. MemorySearchTool.keywordSearch is the unbounded source; the eviction layer that would otherwise cap an oversized tool result is deliberately switched off for this tool name, so nothing downstream trims it. The "small/paginated results" premise in ToolResultEvictionConfig is simply not true for this tool — it is the only one of the three search tools with neither a count cap nor a per-line budget.
Impact. Grows with agent lifetime: a broad keyword repeated across memory/YYYY-MM-DD.md ledgers (headers, boilerplate flush lines) returns every occurrence in full, in one tool result, straight into the next model call — context overflow on exactly the long-running agents the harness targets. Not a security or data-loss issue.
Fix shape (as proposed in the report, and workable). Add maxResults (default 30, matching the docs), break out of both loops once it is reached, truncate each line to a fixed preview budget like session_search's 200 chars, leaving memory_get as the way to pull full context around a hit.
One design call belongs to maintainers, not to a bot PR. "Either implement the cap, or drop memory_search from DEFAULT_EXCLUDED_TOOLS" are not equivalent: the first keeps eviction disabled for the tool, the second re-enables eviction and changes what the model sees on a follow-up turn. Both would also want ToolResultEvictionConfig.java:61's javadoc corrected, since it currently asserts boundedness that no other part of the change guarantees.
No PR from us. The reporter already offered to submit one against this exact shape, so a competing automated PR would duplicate community work. Flagging the doc-vs-code mismatch and the eviction-list question above is the useful output here.
Testability: yes, no external services needed — stub WorkspaceManager.listMemoryFilePaths / readManagedWorkspaceFileUtf8 with several files containing repeated and ~1000-char matching lines, then assert matches <= maxResults, maxResults is honoured when set explicitly, and each emitted preview is within the char budget.
Automated evaluation by github-manager-bot
PR submitted: #3267 — adds the maxResults parameter (default 30, aligning with the documented cap), per-line truncation at 500 chars with a memory_get pointer, and a truncation note when results are capped. 8 new regression tests + full agentscope-harness module suite (1051 tests) pass locally.