#6016·deer-flow

[bug] loop_detection boolean thresholds can force-stop the first tool call

Author: yc2bgr8Created Sep 28, 2026Updated Sep 28, 2026
Labelsneeds-triage

Before you start

  • I searched existing issues and pull requests for loop-detection boolean thresholds, warn_threshold, hard_limit, tool_freq_warn, ToolFreqOverride, and the affected config/test files; no duplicate implementation was found.
  • I can reproduce this on main at 8a3a309d1ce8bac8418251be29d4e4297a20c5eb.

Problem summary

loop_detection silently coerces YAML true into integer threshold 1, so a configuration typo can make the first tool-call set force-stop the agent.

Affected area(s)

  • Agents / LangGraph (loop-detection middleware)
  • Config / setup (config.yaml validation)

What happened?

Every numeric field in LoopDetectionConfig and ToolFreqOverride is declared as int, but none rejects booleans before Pydantic coercion. YAML parses true as Python True, and Pydantic then turns it into integer 1:

LoopDetectionConfig(warn_threshold=True, hard_limit=True)
-> warn_threshold=1 hard_limit=1

LoopDetectionConfig(window_size=True, max_tracked_threads=True)
-> window_size=1 max_tracked_threads=1

LoopDetectionConfig(tool_freq_overrides={"bash": {"warn": True, "hard_limit": 2}})
-> bash.warn=1

The middleware checks count >= hard_limit before admitting the current tool calls. With both thresholds coerced to 1, the first tool-call set has count == 1, meets the hard limit and returns the forced-stop decision instead of executing the tool. Boolean values in window_size, max_tracked_threads, tool_freq_warn, tool_freq_hard_limit, or a per-tool override likewise collapse their runtime limits to one without any configuration error.

This is the same configuration-boundary class recently fixed for database pool settings: a YAML boolean is accepted as an integer and changes runtime control flow rather than failing at startup.

Expected behavior

All integer loop-detection thresholds should reject booleans during configuration loading with an error naming the affected field. Positive integer values and numeric strings produced by environment-variable substitution should retain their existing behavior. The enabled field remains a boolean and is not part of this change.

Steps to reproduce

  1. Check out current main at 8a3a309d1ce8bac8418251be29d4e4297a20c5eb.
  2. From backend/, run:
bash
uv run python -c "from deerflow.config.loop_detection_config import LoopDetectionConfig; print(LoopDetectionConfig(warn_threshold=True, hard_limit=True)); print(LoopDetectionConfig(tool_freq_overrides={'bash': {'warn': True, 'hard_limit': 2}}))"
  1. Observe that both values are accepted and printed as integer 1.
  2. Construct LoopDetectionMiddleware from the first config and pass one AI message containing tool calls. The middleware reaches count >= hard_limit on that first set and produces a hard-stop decision.

Relevant logs

The regression test added for the candidate fix was run against unmodified main and produced eight independent failures:

bash
$ cd backend
$ uv run pytest tests/test_loop_detection_config.py -q
...FFFFFFFF.....
8 failed, 8 passed

Failed: DID NOT RAISE <class 'ValueError'>

The failing cases cover warn_threshold, hard_limit, window_size, max_tracked_threads, tool_freq_warn, tool_freq_hard_limit, and both fields of a per-tool override.

How are you running DeerFlow?

Other — deterministic config-model and middleware unit reproduction; no live model or credentials required.

Operating system

Windows

Platform details

x86_64, PowerShell

Python version

Python 3.12.10

Node.js version

v24.14.1

pnpm version

11.19.0

uv version

uv 0.12.18

Git state

base branch: main
base commit: 8a3a309d1ce8bac8418251be29d4e4297a20c5eb
candidate branch: fix/loop-detection-boolean-thresholds

Support bundle summary

Not applicable: this is a pure configuration-model defect with a deterministic unit reproduction and no deployment, credentials, model call, or host-service dependency.

Additional context

The proposed fix is deliberately limited to rejecting bool before integer coercion. It does not enable strict integer mode globally, because DeerFlow supports numeric strings after environment-variable substitution. The candidate regression suite verifies both sides: all top-level and per-tool boolean thresholds are rejected, while numeric strings still parse to their existing positive integer values.