[PR #1327] feat(cli): Add stateless loop detection middleware with tests #1310

Open
opened 2026-02-16 09:18:42 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagents/pull/1327
Author: @vtrivedy
Created: 2/13/2026
Status: 🔄 Open

Base: mainHead: vivek/loop-detection


📝 Commits (1)

  • 4c76cb9 Add stateless loop detection middleware with tests

📊 Changes

3 files changed (+763 additions, -0 deletions)

View changed files

libs/cli/deepagents_cli/loop_detection.py (+241 -0)
📝 libs/cli/pyproject.toml (+3 -0)
libs/cli/tests/unit_tests/test_loop_detection.py (+519 -0)

📄 Description

  • Rewrites the loop detection middleware proposed in #1295 to be fully stateless, addressing review feedback from @eyurtsev and @ccurme
  • Edit counts and warning dedup are derived by scanning state["messages"] on each hook invocation — no mutable instance state
  • Thread-safe, sub-agent safe (shared instance, different state dicts), and summarization-aware (old messages compress → counts naturally reset)

How it works

  • wrap_tool_call: after each edit_file/write_file, counts edits from messages. At soft threshold (8), appends a nudge to the ToolMessage content. Agent can acknowledge or ignore.
  • after_model: counts edits from messages. At hard threshold (12) with no pending tool calls, injects a HumanMessage telling the agent to stop and ask the user, then forces a new model turn via jump_to: "model".
  • Warning dedup: checks if marker text already exists in message history. No sets or dicts on the instance.
  • Path resolution: uses Path.resolve() so relative and absolute paths to the same file are counted together.
  • Only instance attrs are soft_threshold and hard_threshold (immutable config).

Not yet wired into agent.py — kept as a standalone proposal with tests.


🔄 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/deepagents/pull/1327 **Author:** [@vtrivedy](https://github.com/vtrivedy) **Created:** 2/13/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `vivek/loop-detection` --- ### 📝 Commits (1) - [`4c76cb9`](https://github.com/langchain-ai/deepagents/commit/4c76cb93b71722f79048c1645696b2af09d834bb) Add stateless loop detection middleware with tests ### 📊 Changes **3 files changed** (+763 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `libs/cli/deepagents_cli/loop_detection.py` (+241 -0) 📝 `libs/cli/pyproject.toml` (+3 -0) ➕ `libs/cli/tests/unit_tests/test_loop_detection.py` (+519 -0) </details> ### 📄 Description - Rewrites the loop detection middleware proposed in #1295 to be fully stateless, addressing review feedback from @eyurtsev and @ccurme - Edit counts and warning dedup are derived by scanning `state["messages"]` on each hook invocation — no mutable instance state - Thread-safe, sub-agent safe (shared instance, different state dicts), and summarization-aware (old messages compress → counts naturally reset) ## How it works - `wrap_tool_call`: after each `edit_file`/`write_file`, counts edits from messages. At soft threshold (8), appends a nudge to the ToolMessage content. Agent can acknowledge or ignore. - `after_model`: counts edits from messages. At hard threshold (12) with no pending tool calls, injects a HumanMessage telling the agent to stop and ask the user, then forces a new model turn via `jump_to: "model"`. - Warning dedup: checks if marker text already exists in message history. No sets or dicts on the instance. - Path resolution: uses `Path.resolve()` so relative and absolute paths to the same file are counted together. - Only instance attrs are `soft_threshold` and `hard_threshold` (immutable config). Not yet wired into `agent.py` — kept as a standalone proposal with tests. --- <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 09:18:42 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagents#1310