[PR #281] [MERGED] fix: add truncation to grep/glob/ls tool results #321

Closed
opened 2026-06-05 17:22:40 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/281
Author: @JadenKim-dev
Created: 3/5/2026
Status: Merged
Merged: 3/6/2026
Merged by: @christian-bromann

Base: mainHead: fix/truncate-grep-glob-ls-results


📝 Commits (4)

  • 6e4a2b8 fix: add truncation to grep/glob/ls tool results
  • f9cf00f test: add documentation for truncation test coverage
  • 13eb3d6 test: add integration tests for ls/grep/glob truncation
  • 4dfa59d Implement truncation in tool results

📊 Changes

3 files changed (+202 additions, -5 deletions)

View changed files

.changeset/loud-pigs-clap.md (+5 -0)
📝 libs/deepagents/src/middleware/fs.test.ts (+176 -2)
📝 libs/deepagents/src/middleware/fs.ts (+21 -3)

📄 Description

Summary

Fixes #272

grep, glob, and ls tools can return hundreds of KB of results (observed: 772KB from a single grep), which can exceed context limits and cause model retries or blank responses before eviction can help.

This PR adds truncation at the TOOL_RESULT_TOKEN_LIMIT (20K tokens ~80KB) threshold, consistent with read_file's existing behavior from #172 and matching the Python version's implementation.

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: ... [results truncated, try being more specific with your parameters]

Architecture: 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 follows the same pattern as the Python implementation in deepagents/middleware/filesystem.py.

Test plan

  • Existing tests pass (utils.test.ts already covers truncateIfTooLong utility at lines 290-314)
  • Implementation follows established pattern from read_file truncation (#172)
  • Matches Python version's architecture and behavior

Related

  • #82 — infinite loop from large tool results (fixed by #172 for read_file)
  • #172 — added char truncation to read_file (merged, same pattern used here)
  • #228 — context overflow from 668KB glob (attempted fix via post-summarization truncation)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/281 **Author:** [@JadenKim-dev](https://github.com/JadenKim-dev) **Created:** 3/5/2026 **Status:** ✅ Merged **Merged:** 3/6/2026 **Merged by:** [@christian-bromann](https://github.com/christian-bromann) **Base:** `main` ← **Head:** `fix/truncate-grep-glob-ls-results` --- ### 📝 Commits (4) - [`6e4a2b8`](https://github.com/langchain-ai/deepagentsjs/commit/6e4a2b8f5ce2519ce8e649ce3b6fa0d6d5cf5985) fix: add truncation to grep/glob/ls tool results - [`f9cf00f`](https://github.com/langchain-ai/deepagentsjs/commit/f9cf00f8cfcd6b61021ff73543ba34c08e462514) test: add documentation for truncation test coverage - [`13eb3d6`](https://github.com/langchain-ai/deepagentsjs/commit/13eb3d64a25bfe2930e492274bdb3574043d8f6e) test: add integration tests for ls/grep/glob truncation - [`4dfa59d`](https://github.com/langchain-ai/deepagentsjs/commit/4dfa59d0c083570e7f0686cd4483f2f4e11494ff) Implement truncation in tool results ### 📊 Changes **3 files changed** (+202 additions, -5 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/loud-pigs-clap.md` (+5 -0) 📝 `libs/deepagents/src/middleware/fs.test.ts` (+176 -2) 📝 `libs/deepagents/src/middleware/fs.ts` (+21 -3) </details> ### 📄 Description ## Summary Fixes #272 `grep`, `glob`, and `ls` tools can return hundreds of KB of results (observed: 772KB from a single grep), which can exceed context limits and cause model retries or blank responses before eviction can help. This PR adds truncation at the `TOOL_RESULT_TOKEN_LIMIT` (20K tokens ~80KB) threshold, consistent with `read_file`'s existing behavior from #172 and **matching the Python version's implementation**. ## 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: `... [results truncated, try being more specific with your parameters]` **Architecture**: 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 follows the same pattern as the Python implementation in `deepagents/middleware/filesystem.py`. ## Test plan - [x] Existing tests pass (`utils.test.ts` already covers `truncateIfTooLong` utility at lines 290-314) - [x] Implementation follows established pattern from `read_file` truncation (#172) - [x] Matches Python version's architecture and behavior ## Related - #82 — infinite loop from large tool results (fixed by #172 for `read_file`) - #172 — added char truncation to `read_file` (merged, same pattern used here) - #228 — context overflow from 668KB glob (attempted fix via post-summarization truncation) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 17:22:40 -04:00
yindo closed this issue 2026-06-05 17:22:40 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#321