Preface¶
In DSH Agent development, when a model continuously outputs reasoning-delta, the existing dsh-agent-loop logic can only determine termination after the stream ends, while dsh-llm-deepseek’s idleWatchdog only detects connection liveness and will not interrupt due to stagnant task progress. This causes the turn to continue indefinitely, requiring manual user abortion. dsh-thinking-guard hooks into the agent/assistant-stream event and utilizes a triple circuit-breaker mechanism to prevent the Agent from entering a degenerate state of “only thinking, zero output.”
Plugin Overview¶
- Name: dsh-thinking-guard
- Maintainer: ethanwong-hk
- License: MIT
- Core Function: Listens for
agent/assistant-streamevents and implements a triple circuit-breaker (timeout, capacity, repetition) for idle rounds of pure thinking.
Installation¶
Install via npm:
dsh plugin add @ethanwong-hk/dsh-thinking-guard
Restart DSH after installation. Verify effectiveness:
grep -n "thinking-guard" ~/.dsh/cordis.patch.yml
Core Functions¶
The plugin relies solely on the harness’s agent/assistant-stream events and agent.cancel/followup interfaces, with no other dependencies.
Triple Circuit-Breaker Mechanism¶
- thinkingOnlyMs (Timeout)
- Default Value: 45000 ms
- Trigger Condition: Only active when there is zero text and zero tool-calls.
- Logic: Anchors on “last progress” (the moment text or tool-call arrived), rather than the attempt start point.
- maxThinkingChars (Capacity)
- Default Value: 80000 characters
- Logic: Triggered if the total reasoning volume in a single attempt exceeds the limit, regardless of whether text output exists.
- degenerate-loop (Degenerate Loop)
- Default Value: 3 repetitions
- Logic: Detects repetition patterns in the tail window, including exact repeated units, sentence-level template repetition, sparse repetition, or high N-gram density.
When any condition is met, agent.cancel({ kind:'thinking-guard', reason, detail }) is triggered, and an explanation is attempted via agent.followup().
Configuration¶
Adjust parameters under the thinking-guard entry in ~/.dsh/cordis.patch.yml; changes take effect after restarting DSH.
- insert:
- id: thinking-guard
name: "dsh-thinking-guard"
config:
enabled: true
thinkingOnlyMs: 45000
maxThinkingChars: 80000
repeatThreshold: 3
notify: true
verbose: false
Environment variables can temporarily override parameters:
| Environment Variable | Effect |
|---|---|
DSH_THINKING_GUARD_DISABLED=1 |
Disables the plugin |
DSH_THINKING_ONLY_MS |
Stagnation threshold for pure thinking |
DSH_MAX_THINKING_CHARS |
Capacity threshold for thinking |
DSH_THINKING_REPEAT_THRESHOLD |
Repetition count threshold |
DSH_THINKING_GUARD_VERBOSE=1 |
Enables verbose logging |
Fixed Defects¶
The plugin fixes a cfg scope reference error (ReferenceError) in the formatNotice function.
* Cause: The module-level function referenced a local cfg variable inside apply().
* Impact: This exception directly interrupted the trip() function, causing agent.cancel to never execute, rendering the circuit breaker completely ineffective.
* Fix: Explicitly passed the cfg parameter and moved the agent.cancel logic to the front, ensuring the circuit-breaking action does not depend on the success of the notification step.
Testing & Validation¶
Run the built-in test cases covering 11 scenarios, including pure thinking timeout, degenerate repetition, capacity limits, and passing normal turns.
node ~/.dsh/plugins/dsh-thinking-guard/test/guard.test.mjs
Dependencies & Environment¶
- Dependency:
@deepseek-ai/cordis(^4.0.1) - Node Version: >= 18
- Permissions: The plugin runs with the permissions of the current DSH process. It is recommended to review the source code and license before installation.