dsh-loop-guard: DeepSeek Harness Infinite Loop Circuit Breaker

The DeepSeek Harness (DSH) plugin ecosystem emphasizes that everything is a plugin. However, in actual use, agents often fall into certain forms of dead loops, such as models repeatedly outputting fixed text fragments, causing tasks to spin infinitely in the background without substantive progress. This article introduces dsh-loop-guard, which detects and blocks such repetitive degradation within a single message.

Problem Background

The deepseek-v4.1-flash model from DeepSeek has a specific failure scenario in “thinking” mode: the model may repeatedly output very small amounts of fixed text (such as “好。写。执行。”), without producing any tool calls. This is typically caused by a combination of the following factors:

  1. Thinking mode enabled: The chain of thought is at its largest volume, and the repetitive trajectory is at its longest.
  2. Full history transmission in tools scenarios: When a request includes tools, the reasoning_content of all historical turns must be fully transmitted back, causing the repetitive trajectory to be continuously reinforced.
  3. KV Cache compression: The core selling point of this model, it compresses low-entropy repetitive content almost losslessly, but erases the edge signals that could break the loop.
  4. Context compression failure: The token count per message in the loop is extremely small, the context does not grow, causing the compression mechanism triggered by total volume to remain a bystander throughout.

Any traditional guard based on “comparing whether two consecutive messages are the same” cannot reach it. The loop essentially occurs within a single assistant message.

Core Features

The plugin uses “window repetition density” as the primary criterion, covering two common loop patterns:

  • Single-line spam: A single line of text repeated an extremely high number of times (e.g., </parameter> repeated 1729 times).
  • Multi-marker alternating scrolling: Different markers (output, generate, tool call, 好。) appear alternately, with a low single-line repetition ratio but high overall density.

When detecting degradation, the plugin injects a loop-breaking reminder into the next step to prevent the agent from looping.

Installation & Enablement

Before installation, ensure the environment meets Node >= 20.

1. Install the plugin via command

Use the DSH plugin command to add the package to profile dependencies:

dsh plugin --profile <your profile> add github:GRUST303/dsh-loop-guard

2. Configure Bundle (Critical Step)

Executing the above command only writes the package into dependencies. DSH relies on the dsh.profile.bundles field to determine which plugins to load. You must explicitly declare it in the profile’s package.json:

{
  "dsh": {
    "profile": {
      "bundles": [
        "@deepseek-ai/dsh-base",
        "@deepseek-ai/dsh-web-app",
        "dsh-loop-guard"  // must be listed here
      ]
    }
  }
}

3. Restart Profile

The Bundle layer is read and synthesized into cordis.yml at startup. A full profile restart is required for it to take effect; HMR is not sufficient.

Configuration

Configuration is provided through the config section of the profile plugin line. Below is a typical configuration example:

- id: loop-guard
  name: dsh-loop-guard
  config:
    preset: balanced
    repeatDensity: 0.35     # 主判据:窗口重复密度阈值
    window: 12              # 滑动窗口行数
    lineRepeatThreshold: 6  # 单行重复次数阈值(需同时满足占比 ≥ 50%)
    contextLineRepeatThreshold: 20   # 历史污染阈值
    minChars: 120           # 短于此长度的消息不参与判定
    crossStepThreshold: 3   # 跨步重复次数
    debug: false            # 写入调试日志

After configuration changes, a profile restart is required for the changes to take effect.

Preset Description

The plugin provides three tiers for selection:

  • conservative: density threshold 0.5. Suitable for scenarios with large amounts of table or code output, with the lowest false positive rate.
  • balanced: density threshold 0.35 (default). The measured false positive rate is extremely low, covering all observed loop patterns.
  • aggressive: density threshold 0.25. Intervenes earliest, reminding even for mild repetition, suitable for unattended long-running tasks.

Notes

  • Does not rewrite LLM request parameters: The plugin does not modify parameters such as temperature, presence_penalty, because these parameters are not supported or are invalid in DeepSeek’s thinking mode.
  • Does not block, does not reset: The plugin only injects reminders, does not veto, block, or reset the session. If the upstream rejects that step, it passes through as-is.
  • Settings page rendering issue: The plugin declares a settings page slot settings.plugins.tab, but it is not rendered in DSH version 0.1.7-rc.2. Please configure directly through the config section; core functionality is not affected.
  • Dependency requirements: Requires Node.js >= 20. Depends on @deepseek-ai/schemastery for configuration validation; if missing, the settings page will be unavailable, but the core loop prevention functionality still works normally.

Summary

dsh-loop-guard targets a subtle and hard-to-detect failure mode in the DSH ecosystem. By performing sliding window density detection within a single message, it can effectively identify and block the infinite repetitive spinning caused by KV Cache compression in the model’s thinking mode, ensuring stable agent operation.

Project URL: https://github.com/GRUST303/dsh-loop-guard

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

Xiaoye