[PR #172] [MERGED] fix(deepagents): prevent infinite loop when read_file returns large content #177

Closed
opened 2026-02-16 06:17:21 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/172
Author: @christian-bromann
Created: 2/2/2026
Status: Merged
Merged: 2/3/2026
Merged by: @christian-bromann

Base: mainHead: cb/fs-eviction-fix


📝 Commits (3)

  • 4b8e991 fix(deepagents): prevent infinite loop when read_file returns large content (#82)
  • 2693629 export FileData from right place
  • 4ecabda add changesets

📊 Changes

4 files changed (+221 additions, -11 deletions)

View changed files

.changeset/puny-jars-buy.md (+5 -0)
📝 libs/deepagents/src/index.ts (+1 -1)
📝 libs/deepagents/src/middleware/fs.eviction.test.ts (+162 -1)
📝 libs/deepagents/src/middleware/fs.ts (+53 -9)

📄 Description

Agents can enter an infinite loop when handling large tool results with long lines.

Problem

When a tool returns a large result composed of a small number of very long lines (e.g., 5 lines totaling ~100,000 characters), the agent could enter an infinite eviction/read loop:

  1. Tool result exceeds eviction threshold (~80KB) → evicted to filesystem
  2. Agent uses read_file to read the evicted content
  3. read_file returns all content (only 5 lines, but 100K chars) because it only had a line limit, not a character limit
  4. read_file result exceeds threshold → would be evicted again (loop)

While read_file was already excluded from eviction (preventing step 4), the unbounded output could still overwhelm the context window.

Solution

Port the fix from the Python implementation:

  • Add character-based truncation to read_file: Truncates output when it exceeds ~80KB (matching the eviction threshold)
  • Provide actionable guidance: Truncation message suggests using tools like jq to reformat files with long lines
  • Reduce default line limit: Changed from 500 to 100 lines (matching Python)
  • Improve constant naming: Renamed to DEFAULT_READ_LINE_OFFSET / DEFAULT_READ_LINE_LIMIT for clarity

This PR also creates consistent behavior between JS/PY

fixes #82


🔄 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/172 **Author:** [@christian-bromann](https://github.com/christian-bromann) **Created:** 2/2/2026 **Status:** ✅ Merged **Merged:** 2/3/2026 **Merged by:** [@christian-bromann](https://github.com/christian-bromann) **Base:** `main` ← **Head:** `cb/fs-eviction-fix` --- ### 📝 Commits (3) - [`4b8e991`](https://github.com/langchain-ai/deepagentsjs/commit/4b8e99194765be9146c810af9bf0a89677a4b5b8) fix(deepagents): prevent infinite loop when read_file returns large content (#82) - [`2693629`](https://github.com/langchain-ai/deepagentsjs/commit/26936295b29c9255af5da5a23754c46877e9f960) export FileData from right place - [`4ecabda`](https://github.com/langchain-ai/deepagentsjs/commit/4ecabda78844e6e30f8c1366c100ba243c9e6b7d) add changesets ### 📊 Changes **4 files changed** (+221 additions, -11 deletions) <details> <summary>View changed files</summary> ➕ `.changeset/puny-jars-buy.md` (+5 -0) 📝 `libs/deepagents/src/index.ts` (+1 -1) 📝 `libs/deepagents/src/middleware/fs.eviction.test.ts` (+162 -1) 📝 `libs/deepagents/src/middleware/fs.ts` (+53 -9) </details> ### 📄 Description Agents can enter an infinite loop when handling large tool results with long lines. ## Problem When a tool returns a large result composed of a small number of very long lines (e.g., 5 lines totaling ~100,000 characters), the agent could enter an infinite eviction/read loop: 1. Tool result exceeds eviction threshold (~80KB) → evicted to filesystem 2. Agent uses `read_file` to read the evicted content 3. `read_file` returns all content (only 5 lines, but 100K chars) because it only had a **line limit**, not a character limit 4. `read_file` result exceeds threshold → would be evicted again (loop) While `read_file` was already excluded from eviction (preventing step 4), the unbounded output could still overwhelm the context window. ## Solution Port the fix from the Python implementation: - **Add character-based truncation to `read_file`**: Truncates output when it exceeds ~80KB (matching the eviction threshold) - **Provide actionable guidance**: Truncation message suggests using tools like `jq` to reformat files with long lines - **Reduce default line limit**: Changed from 500 to 100 lines (matching Python) - **Improve constant naming**: Renamed to `DEFAULT_READ_LINE_OFFSET` / `DEFAULT_READ_LINE_LIMIT` for clarity This PR also creates consistent behavior between JS/PY fixes #82 --- <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-02-16 06:17:21 -05:00
yindo closed this issue 2026-02-16 06:17:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#177