DSH (DeepSeek Harness) is a modular plugin platform. In real-world development, models sometimes exhibit “degenerative repetition”: the reasoning chain or main text repeatedly outputs the same small fragment (such as “OK”, “OK, execute”, or “fine”), without producing a useful result for a long time, causing tokens to be wasted.

Existing community solutions (such as dsh-thinking-loop-guard) are usually attached to agent/turn-stopping and only inspect the complete output at turn boundaries. This has a clear limitation: turn boundary = the tokens for that turn have already been burned. It can prevent the loop from spreading into the next turn, but it cannot rescue the turn that is currently burning.

dsh-loop-breaker is attached to DSH’s llm/stream waterfall event. By tracking reasoning and text block by block, it can detect repetition patterns in real time during streaming output. After a hit, it immediately cancels the underlying HTTP request (stopping billing), injects a recovery prompt so the model can retry, and uses an L0 sanitization mechanism to prevent the repeated original text from polluting the conversation history.

Core Features

  • Streaming interception: Attached to the llm/stream event, detecting issues at the streaming output layer.
  • Multi-pattern detection: Supports detection of fragment-cycle loops (such as “Do.→Execute.→OK.”), character collapse, sentence recycling, 16-gram recycling, and short-sentence redundancy.
  • Billing circuit-breaker: After a rule is triggered, the adapter generator执行 consumer.abort(), the underlying HTTP request is immediately canceled, and token billing stops.
  • Recovery mechanism: Injects a recovery prompt to guide the model to answer again.
  • L0 sanitization: When writing back to conversation history, retains the beginning portion and replaces the rest with a placeholder, preventing the repeated original text from polluting subsequent generation.
  • Tool-block deferral: If an unclosed non-text/reasoning tool-call block is detected, the circuit-breaker is deferred to avoid false positives.

Installation

Run the following commands in your terminal:

git clone https://github.com/CHIP-PHILO-GH/dsh-loop-breaker
cd dsh-loop-breaker
pwsh -File install.ps1

install.ps1 is idempotent and can be run repeatedly. The default installation path is $env:DSH_HOME\profiles\node_modules\dsh-loop-breaker. After making configuration changes, restart dsh web for them to take effect.

Note: Running install.ps1 with Windows PowerShell 5.1 may fail due to Chinese encoding issues. PowerShell 7 (pwsh) is recommended.

Configuration

Add the following snippet to your DSH configuration file (example):

- id: loop-breaker
  name: 'dsh-loop-breaker'
  config:
    enabled: true                # 总开关
    recover: true                # 命中后是否注入补救提示
    maxRecoveriesPerTurn: 2      # 同一轮最多补救次数
    nudge: ''                    # 补救提示文本,留空使用内置文案

    # --- L0 消毒 ---
    sanitize: true               # 是否改写正文
    sanitizeHeadChars: 240       # 保留原文开头的字数(0=全丢)
    degradePlaceholder: ''       # 替换复读原文的占位符

    # --- 检测阈值 ---
    windowChars: 1600            # 滚动统计窗口
    periodMax: 48                # 短周期检测最大周期
    periodMatch: 0.92            # 短周期自吻合率阈值
    periodMaxPieces: 6           # 窗口内片段种类上限
    collapseUniqueRatio: 0.10    # 去重字符占比阈值
    sentRecycle: 0.80            # 句子回收率阈值
    gramRecycle16: 0.80          # 16-gram 回收率阈值
    tightRedundancy: 0.90        # 窗口字面冗余度阈值
    maxChars: 200000             # 单次输出硬上限
    maxReasoningChars: 120000    # 单次思考链硬上限

Testing

The plugin provides regression tests and integration test scripts.

  • Regression test: Validates the core detection logic.
    node test.mjs
  • Integration test: Runs in an environment with DSH installed, validating stream protocol validity, circuit-breaking, and sanitization logic.
    node tests/guard.test.mjs

Applicable Scenarios and Notes

  • Primary target: Adjacent short-cycle repetition (such as “Do.→Execute.→OK.”). This kind of repetition is usually built from a small number of short fragments repeated consecutively.
  • Known boundaries: Long-interval repetition, shorter English cycles, or samples that are too short may be missed; normal long technical reports, code containing many similar functions, and other long legitimate outputs may be falsely triggered due to structural repetition.
  • Environment dependencies:
    • Node.js >= 18 is required.
    • The host environment must provide @deepseek-ai/dsh-llm and @deepseek-ai/schemastery.
    • Review the source code and license before use (MIT).

By promptly cutting off invalid generation during streaming, the plugin avoids out-of-control runs such as the 31M-token example reported in Ollama, ensuring each interaction remains within controllable bounds.