deerflow2/backend/tests
virtaava d18a9ae5aa
feat: add LoopDetectionMiddleware to break repetitive tool call loops (#1056)
* feat: add LoopDetectionMiddleware to break repetitive tool call loops

Adds a new AgentMiddleware that detects when the agent is stuck calling
the same tools with the same arguments repeatedly, which currently runs
until the recursion limit kills the run.

Detection: per-thread sliding window of tool call hashes (name + args).
- Warn threshold (default 3): injects a "wrap up" system message
- Hard limit (default 5): strips tool_calls, forcing final text output

Includes 13 unit tests covering hashing, thresholds, window sliding,
reset, and edge cases.

Closes #1055

* fix: address PR #1056 review feedback for LoopDetectionMiddleware

- Remove unused imports (Awaitable, Callable, ModelCallResult,
  ModelRequest, ModelResponse, AIMessage) from loop_detection_middleware
- Remove unused pytest import from test file
- Fix _hash_tool_calls sort key: sort by (name, serialized args) for
  deterministic hashing when multiple calls share the same tool name
- Revert subagent_enabled default to False in agent.py to match
  DeerFlowClient and channel defaults
- Remove unrelated SearxNG tools and Next.js rewrite changes from PR

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: address 2nd round review feedback on PR #1056

- Inject loop warning only once per thread (prevents context bloat)
- Add threading.Lock for thread-safe history mutations
- Use runtime.context thread_id instead of workspace_path
- Add LRU eviction for per-thread history (max 100 threads)
- Add 5 new tests covering warn-once, LRU eviction, thread isolation,
  fallback thread_id, and lock presence

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: resolve lint errors in loop detection middleware tests

Sort imports (I001) and remove unused _WARNING_MSG import (F401)
to fix ruff lint failures in CI.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-14 22:17:54 +08:00
..
conftest.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_channel_file_attachments.py feat(channels): upload file attachments via IM channels (Slack, Telegram, Feishu) (#1040) 2026-03-10 09:11:57 +08:00
test_channels.py fix: preserve conversation context in Telegram private chats (#1105) 2026-03-14 09:47:24 +08:00
test_checkpointer.py fix(checkpointer): return InMemorySaver instead of None when not configured (#1016) (#1019) 2026-03-09 15:48:27 +08:00
test_checkpointer_none_fix.py fix(checkpointer): return InMemorySaver instead of None when not configured (#1016) (#1019) 2026-03-09 15:48:27 +08:00
test_client.py fix(client): Harden upload validation and conversion flow (#989) 2026-03-11 15:17:31 +08:00
test_client_live.py Support langgraph checkpointer (#1005) 2026-03-07 21:07:21 +08:00
test_custom_agent.py fix(backend): upgrade langgraph-api to 0.7 and stabilize memory path tests (#984) 2026-03-06 09:44:40 +08:00
test_docker_sandbox_mode_detection.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_infoquest_client.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_lead_agent_model_resolution.py feat(agent):Supports custom agent and chat experience with refactoring (#957) 2026-03-03 21:32:01 +08:00
test_loop_detection_middleware.py feat: add LoopDetectionMiddleware to break repetitive tool call loops (#1056) 2026-03-14 22:17:54 +08:00
test_mcp_client_config.py test(backend): add core logic unit tests for task/title/mcp (#936) 2026-03-01 12:36:09 +08:00
test_mcp_oauth.py feat(mcp): add OAuth support for HTTP/SSE MCP servers (#908) 2026-03-01 22:38:58 +08:00
test_memory_prompt_injection.py fix(memory): inject stored facts into system prompt memory context (#1083) 2026-03-13 14:37:40 +08:00
test_memory_upload_filtering.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_model_factory.py Add MiniMax as an OpenAI-compatible model provider (#1120) 2026-03-14 22:05:18 +08:00
test_present_file_tool_core_logic.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_provisioner_kubeconfig.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_readability.py fix(backend): Fix readability extraction crash when Node parser fails (#937) 2026-03-01 22:24:02 +08:00
test_reflection_resolvers.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00
test_sandbox_tools_security.py feat(sandbox): harden local file access and mask host paths (#983) 2026-03-13 22:38:32 +08:00
test_skills_archive_root.py fix(gateway): ignore archive metadata wrappers (#1108) 2026-03-13 21:27:54 +08:00
test_skills_loader.py feat(skills): support recursive nested skill loading (#950) 2026-03-02 21:02:03 +08:00
test_skills_router.py fix(gateway): allow standard skill frontmatter metadata (#1103) 2026-03-13 21:23:35 +08:00
test_subagent_executor.py fix(subagents): cleanup background tasks after completion to prevent memory leak (#1030) 2026-03-10 07:41:48 +08:00
test_subagent_timeout_config.py feat(subagents): make subagent timeout configurable via config.yaml (#897) 2026-02-25 08:39:29 +08:00
test_suggestions_router.py fix(gateway): normalize suggestion response content (#1098) 2026-03-13 21:20:15 +08:00
test_task_tool_core_logic.py fix(subagents): cleanup background tasks after completion to prevent memory leak (#1030) 2026-03-10 07:41:48 +08:00
test_title_generation.py feat: add AIO sandbox provider and auto title generation (#1) 2026-01-14 23:29:18 +08:00
test_title_middleware_core_logic.py Enhance chat UI and compatible anthropic thinking messages (#1018) 2026-03-08 20:19:31 +08:00
test_tool_error_handling_middleware.py fix(middleware): degrade tool-call exceptions to error tool messages (#1110) 2026-03-13 09:41:59 +08:00
test_tracing_config.py fix(tracing): support LANGCHAIN_* env fallback for LangSmith config (#1065) 2026-03-11 10:26:56 +08:00
test_uploads_middleware_core_logic.py fix(chat): handle empty uploaded files case and improve artifact selection logic (#979) 2026-03-05 17:45:25 +08:00
test_uploads_router.py feat: add IM channels for Feishu, Slack, and Telegram (#1010) 2026-03-08 15:21:18 +08:00