dsh-loop-guard: Predicate Guard for Costly Loops Before Tool Execution

Preface

When running agents in DSH, the cost often lies not in “chatting a bit more,” but in the tool layer: executing the same command repeatedly, switching between two tools back and forth, hitting the same error twice and still retrying, or uncontrolled sub-agent fan-out within a single round of Q&A—all of these silently drive up the token bill. Relying on the model to consciously stop is unreliable; relying solely on user-level skill reminders cannot block the actual step of sending the call.

dsh-loop-guard is a local workflow plugin (Community Directory Category: Workflow) published by maintainer 514koishi-495. Before every tool call actually occurs, it uses pure arithmetic rules to determine “is this step just burning money again,” and intercepts it directly when necessary; it does not look at content and does not call the model. The README states that it works in tandem with the user-level skill ~/.dsh/skills/no-waste-loop-guard/: the skill handles consciousness, and the plugin handles enforcement. One of the design principles is to never permanently lock the agent—every interception point includes an escape hatch.

What is this

dsh-loop-guard (npm package name dsh-loop-guard, current version 1.1.7, MIT License, requires Node.js >= 22) hooks into the DSH tool call chain via a Cordis patch. The ledger counts “billed input”: inputTokens + cacheReadTokens + cacheWriteTokens, rather than just looking at the uncached inputTokens.

Plugin state and session counts are persisted to ~/.dsh/loop-guard-state.<profile>.json (falling back to ~/.dsh/loop-guard-state.json if the profile cannot be identified). The effective time for any switch or configuration change is the next time DSH is launched—after installation or modification, you must completely exit the process and reopen it (closing the window does not count).

Core Features: Gate Overview

The following summarizes the main gates based on the table in the README (interception returns error codes starting with LOOP_GUARD_).

Type Default Threshold (Adjustable) Interception Code Example
Round Budget 60 million billed inputs (includes all sub-agents in this round) LOOP_GUARD_BUDGET_EXHAUSTED
Session Total Budget 1 billion (counts this session only, excluding sub-agents) LOOP_GUARD_SESSION_BUDGET
Family Budget Unlimited by default LOOP_GUARD_FAMILY_BUDGET
Same Signature Repeat 4 times (read is 5 times) LOOP_GUARD_REPEAT
Same Skeleton Repeat 10 times (command line normalized) LOOP_GUARD_SKELETON
Ping-Pong Cycle 3 rounds of A-B-A-B LOOP_GUARD_CYCLE
Same Error 2 times (only intercepts the specific tool that errored) LOOP_GUARD_SAME_ERROR
Sub-agent Requests / Tool Count 150 / 250 LOOP_GUARD_SUBAGENT_REQUESTS etc.
Fan-out & Sub-agent Anomalies Concurrent sub-agents 8, agents in workflow 24, etc. LOOP_GUARD_FANOUT etc.

Round Budget resets per round: one round = one user message and all work following it; sub-agent consumption falls into the round where the spending occurred. Session Total Budget accumulates across rounds but only counts the host session itself. Family Budget counts the accumulation across rounds of itself plus all descendants; if you want “the entire session including sub-agents not to exceed 1 billion,” you can set DSH_LOOP_GUARD_FAMILY_MAX_TOKENS to 1000000000.

The “Same Error” gate only recognizes “Tool Name + Normalized Error Body”; the plugin’s own rejections and common transient network errors are skipped to avoid false locks. Tools for closing or interacting with users like todo_write and ask_user_question are on a whitelist and are never intercepted.

Escape hatches have two semantics (emphasized in README): Repeat gates and the three budget gates follow “Intercept 3 times consecutively at the same spot (default maxDenials), then allow once”; Quota gates follow “First exceed warn and allow once, subsequent exceeds intercept continuously.” Ledger failures will not cause session errors—the guard goes blind, but does not block normal conversation.

Installation and Activation

The runtime directory and source code directory on Windows are shown below (on Linux, replace %USERPROFILE% with $HOME for comparison):

%USERPROFILE%\.dsh\profiles\desktop\node_modules\dsh-loop-guard\    ← Read by runtime
%USERPROFILE%\.dsh\local-plugins\dsh-loop-guard\                    ← Source and tests

When installing from GitHub for others, the steps given in the README are to clone to the local plugin directory and register two places in the profile’s package.json:

git clone https://github.com/514koishi-495/dsh-loop-guard "$HOME\.dsh\local-plugins\dsh-loop-guard"

The directory name must remain dsh-loop-guard. In the profile’s package.json:

  1. Add "dsh-loop-guard" to the dsh.profile.bundles array.
  2. Add "dsh-loop-guard": "file:../../local-plugins/dsh-loop-guard" to dependencies (path relative to the profile directory).

Using a file: dependency is intentional: to avoid bundle rollback caused by pulling packages from the registry or missing lockfile entries. After modifying source code, you need to sync lib, package.json, and cordis.patch.yml to the corresponding directory under node_modules, then completely exit DSH and restart.

The companion skill no-waste-loop-guard is not in this repository and needs to be placed separately at %USERPROFILE%\.dsh\skills\no-waste-loop-guard\SKILL.md.

Typical Usage: Thresholds and Environment Variables

Most thresholds do not require code changes; just set environment variables before starting DSH. Common items are as follows:

Variable Default Description
DSH_LOOP_GUARD_MAX_TOKENS 60000000 Round limit; 0 or negative means unlimited
DSH_LOOP_GUARD_SESSION_MAX_TOKENS 1000000000 Session cumulative; 0 or negative means unlimited
DSH_LOOP_GUARD_FAMILY_MAX_TOKENS Unlimited Family cumulative
DSH_LOOP_GUARD_REPEAT_LIMIT 4 Same signature repeat
DSH_LOOP_GUARD_CYCLE_LIMIT 3 Ping-pong cycle
DSH_LOOP_GUARD_SAME_ERROR_LIMIT 2 Same error
DSH_LOOP_GUARD_MAX_DENIALS 3 Consecutive interceptions for escape hatch

The warning ratio defaults to DSH_LOOP_GUARD_WARN_RATIO=0.7. For the complete list, see the repository README.

For local development, you can run the tests included in the package:

node test/smoke.mjs && node test/guards.mjs && node test/persist.mjs write && node test/persist.mjs read && node test/persist.mjs migrate

(Execute in the plugin source directory, consistent with the test script in package.json.)

Use Cases and Notes

Who it is for: Developers who run multi-tool, multi-sub-agent workflows in DSH for long periods; scenarios where you want hard rules to catch duplicate calls, loops, and single-round token limits, rather than relying on model self-constraint.

Permissions and Security: The plugin runs with the permissions of the current DSH process, reads and writes state files under ~/.dsh/, and intercepts tool calls. Before installation, you should read the source code and the MIT License to confirm that the default gate values fit your environment.

If falsely locked or when you need to stop it (none rely on the model):

  1. Preferred: Settings → Plugin Market → Installed → Turn off “Enabled” for this plugin, then Restart DSH (for this plugin, the market switch is not hot-effective).
  2. Unlock budget only: Delete or edit the corresponding session record in ~/.dsh/loop-guard-state.<profile>.json, or turn up / set DSH_LOOP_GUARD_MAX_TOKENS and DSH_LOOP_GUARD_SESSION_MAX_TOKENS to 0 and restart.
  3. Uninstall: Follow the uninstall.ps1 process provided in the repository, then completely exit and reopen.

Budget limits are sticky: once the limit is reached, it will continuously intercept, and accumulated values are restored from the archive; closing tasks can still be passed via whitelisted tools and escape hatches. The community plugin directory is maintained by sites like SkillHub and is not officially affiliated with DeepSeek / High-Flyer; installation sources are subject to the GitHub repository and directory page descriptions.

Conclusion

dsh-loop-guard turns “anti-burn” into a code gate before tool calls: duplicates, loops, sub-agent fan-out, and session-level budgets all have default criteria, and it leaves manual exits like restarts and market switches for false interceptions. To further understand parameters and change logs, you can directly consult the repository README and CHANGELOG.md.

  • Community Directory Page: https://www.skillhub.cn/plugins/514koishi-495/dsh-loop-guard
  • GitHub: https://github.com/514koishi-495/dsh-loop-guard
羽毛球分组比赛记分
小程序二维码

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

Xiaoye