fix: add truncation to grep/glob/ls tool results

Fixes #272

grep, glob, and ls tools can return hundreds of KB of results, which
can exceed context limits and cause model retries or blank responses.
This adds truncation at the TOOL_RESULT_TOKEN_LIMIT (20K tokens ~80KB)
threshold, consistent with read_file's existing behavior.

Changes:
- Import truncateIfTooLong utility in fs.ts middleware
- Apply truncation to ls, glob, and grep tool results
- Truncate arrays before joining to preserve complete lines
- Add guidance message when results are truncated

The truncation happens in the middleware layer (matching Python's
architecture), not in the backend, so backends return full results
and middleware applies the limit.
This commit is contained in:
JadenKim-dev
2026-03-06 13:36:38 +09:00
parent c860bbefa7
commit 6e4a2b8f5c
+21 -3
View File
@@ -31,6 +31,7 @@ import { StateBackend } from "../backends/state.js";
import {
sanitizeToolCallId,
formatContentWithLineNumbers,
truncateIfTooLong,
} from "../backends/utils.js";
/**
@@ -394,7 +395,13 @@ function createLsTool(
lines.push(`${info.path}${size}`);
}
}
return lines.join("\n");
const result = truncateIfTooLong(lines);
if (Array.isArray(result)) {
return result.join("\n");
}
return result;
},
{
name: "ls",
@@ -616,7 +623,13 @@ function createGlobTool(
return `No files found matching pattern '${pattern}'`;
}
return infos.map((info) => info.path).join("\n");
const paths = infos.map((info) => info.path);
const result = truncateIfTooLong(paths);
if (Array.isArray(result)) {
return result.join("\n");
}
return result;
},
{
name: "glob",
@@ -671,7 +684,12 @@ function createGrepTool(
lines.push(` ${match.line}: ${match.text}`);
}
return lines.join("\n");
const truncated = truncateIfTooLong(lines);
if (Array.isArray(truncated)) {
return truncated.join("\n");
}
return truncated;
},
{
name: "grep",