본문으로 건너뛰기
G

dsh-sleep-guard

guagua159/dsh-sleep-guard

Refuses shell commands that would block the whole turn, such as foreground sleeps of 20 seconds or more, follow-mode log readers and dev servers, and points the agent at background jobs instead.

설치

dsh plugin --profile web add github:guagua159/dsh-sleep-guard

README

dsh-sleep-guard

test

A DeepSeek Harness bundle that refuses shell commands and blocking waits that would hold the whole turn, and points the agent at background jobs instead.

The agent cannot act while a foreground command is running. sleep 300, tail -f, and npm run dev each hold the turn for minutes or forever, so the work they wait for cannot overlap with anything else. This plugin denies those calls with a ctx.tools.guard(): the decision is synchronous, it lands after the extensible pre-execute policy, and no later listener can turn the refusal back into permission — so registration order does not matter. The alternative is named in the message.

What it refuses

CommandCondition
sleep20 seconds or more, including 20s, 2m, 1h, 1d suffixes and absolute paths such as /usr/bin/sleep
Start-Sleep20 seconds or more, in the flag form (-Seconds 60, -Milliseconds:30000, abbreviable to -s / -m) and the positional form (Start-Sleep 60, Sleep 60)
tail-f, -F, --follow, --follow=name
ping, ping6runs until interrupted: on MSYS/Linux any call without -c N, on Windows only -t. A bare Windows ping host sends four echoes and returns in about 3 s, so it is allowed; -h, --help, -V, and --version print and return everywhere
watchalways, except --version / --help
journalctl-f, --follow
docker logs, docker compose logs, docker-compose logs, kubectl logs-f, --follow; compose global options (-f box.yml, -p proj) do not shift the verb out of view
viteexcept build and optimize, which exit
nodemon, webpack servealways, except --version / --help
npm/pnpm/yarn/bun start, dev, serve, watch, run <one of those>always
job_outputwait: true with timeout_ms above the wait bound (5 minutes by default)
wait_agenttimeout_ms above the wait bound (5 minutes by default)
a for/while/until loopwhose body reaches a sleep, when the loop as a whole holds the turn for 20 seconds or more

Commands are split on &&, ||, ;, |, &, and line breaks — including a lone carriage return — before each segment is checked. A segment backgrounded with & returns immediately, so it is allowed: sleep 300 & costs nothing. Leading VAR=value assignments, wrappers such as sudo, env, nohup, time, nice, stdbuf, command, exec, and busybox, and command runners (npx, bunx, pnpx, pnpm dlx, yarn exec) are stepped over together with their own options, so sudo -u root sleep 300 and npx vite are still seen. Heredoc bodies and PowerShell here-strings (@'…'@, @"…"@) are treated as file contents, not commands; << inside arithmetic (x=$((1<<n))) is a bit shift and opens nothing. Quoted text is stripped first, so echo "sleep 240" is allowed — but an unpaired quote stays literal text, so git commit -m don't care; sleep 300 no longer hides the sleep.

A PowerShell assignment prefix is stripped before the command word is read. $out = sleep 300, $out=sleep 300, $null = …, $a.b = …, $env:X = … and $script:x = … all still run the command on the right of the =, so the left side must not be taken for the command name — which is exactly what happened while only the VAR=value form was recognised, and it hid every blocking call written that way. Chained assignments ($a = $b = sleep 300) are stripped too. A $-prefixed word that is not followed by = is left alone, because $x sleep 300 is not valid PowerShell and guessing there would be inventing a rule.

A loop is checked before the per-segment pass, because splitting on ; cuts do sleep 30; done into pieces that no longer look like a loop. The hold is the whole loop, not one pass: for i in $(seq 1 30); do sleep 10; done is five minutes of a frozen turn even though no single sleep crosses the threshold. The iteration count is read from $(seq 1 N) or counted in a literal word list; while true has no exit path and is refused whenever its body sleeps. A loop whose body has no sleep is allowed, because for f in *.txt; do wc -l "$f"; done ends when the glob does.

A blocking wait spends the turn exactly like a foreground delay, so the two wait tools are bounded the same way. job_output({ wait: true }) and wait_agent return as soon as their condition is met, so their timeout_ms is a ceiling rather than a planned delay — the limit is therefore generous (5 minutes) and does not forbid waiting. Exceeding it is refused with an instruction to shorten the ceiling or to read with wait: false between steps. job_output without wait: true returns at once and is never refused. The judge sees only the call, so a wait on a job that has already settled is refused too; the retry with a shorter ceiling then returns the output immediately.

What the refusal says

Each refusal names a direction to try next, chosen by the shape of what was refused — a fixed delay, a polling loop, a follower, a server, a watch, or a wait ceiling. It deliberately does not name a replacement command. The guard sees one command and no context: a sleep 300 may be waiting for a process, waiting for a service to come up, or may be padding that does not belong, and those three need different fixes. A concrete rewrite would be wrong in two of them, and a wrong instruction is worse than a general one because the agent follows it and then fails.

The third refusal in a mount adds one sentence saying so, because by then the agent is usually rephrasing the same intent rather than changing the approach. The count only advances on refusals: allowed calls, run_in_background calls and the other bounded waits do not consume it.

Measured on 88 blocked commands replayed from a session archive, the longer message costs about 55 extra tokens per refusal (52 → 107), or roughly 4.8k tokens over the whole corpus. That is below the noise floor of a real session, which is why the trade was taken; the counter sentence adds 130 more tokens each time it appears.

What it leaves alone

  • run_in_background: true — the command is meant to outlive the step.
  • A non-empty script_path — the contents are not part of this call and are not read.
  • Tools other than bash, pwsh, pwsh_persistent, shell, and ops_bash, apart from the two bounded wait tools. bash and pwsh also cover the persistent variants, which register under the same names and take no run_in_background. ops_bash comes from the separate dsh-ops package; pwsh_persistent and shell are not registered by any shipped package. All three names are kept for profiles that do install a matching tool and never match otherwise.
  • Anything the detector throws on. Every path fails open: a guard bug must never block an unrelated command. The failure is logged through ctx.logger.warn so a broken detector does not look like a working guard. Refusal logging is bounded: at most 100 lines per mount and at most one line per second, with the count of suppressed lines reported on the next line that is written. A guard firing in a loop cannot flood the session log.

It is hygiene, not a security boundary. Destructive-command analysis stays with a dedicated tool such as cc-safety-net, which parses shell structure far more thoroughly than this package tries to.

Install

dsh plugin --profile <name> add dsh-sleep-guard

dsh plugin forwards to pnpm, so pnpm has to be on PATH. Add dsh-sleep-guard to dsh.profile.bundles in the profile's package.json if the command does not do it for you, then restart DeepSeek Harness — an installed package only loads a fresh JavaScript module generation on restart.

To work on the source instead, clone it next to the profile and link it:

git clone https://github.com/guagua159/dsh-sleep-guard ~/.dsh/local-plugins/dsh-sleep-guard
{ "dependencies": { "dsh-sleep-guard": "link:../../local-plugins/dsh-sleep-guard" } }

The path in link: is relative to the profile directory; adjust the number of .. segments if you cloned elsewhere. Link rather than copy: a link: dependency points at the one checkout, so edits and git pull are picked up, while file: would install a second copy that nothing keeps in sync.

Configure

Three values are adjustable from the patch layer; each keeps its shipped default when omitted, and a malformed value falls back to it rather than disabling the guard.

- insert:
    - id: sleep-guard
      name: dsh-sleep-guard
      config:
        minSeconds: 60                                    # default 20
        maxWaitMs: 600000                                 # default 300000
        shellTools: ['bash', 'pwsh', 'shell']             # default: bash, pwsh, pwsh_persistent, shell, ops_bash

minSeconds is the shortest foreground delay worth refusing. maxWaitMs is the longest single blocking wait allowed, in milliseconds, for job_output({ wait: true }) and wait_agent. shellTools replaces — not extends — the built-in list, so name every tool that carries a command argument in your profile. The settings are read once when the row mounts, so a change takes effect after a restart.

Limits

Detection is pattern-based, not a shell parser. These forms are not refused:

bash -c "sleep 300"
sh -c "tail -f x"
eval "sleep 300"
xargs sleep 300
find . -exec sleep 300 \;
(sleep 300)
if true; then sleep 300; fi
$(sleep 300)
`sleep 300`
timeout 10 sleep 300

Nested quoting, command substitution, subshells, and if/then blocks all hide the command from a per-segment regex. Closing that gap needs an AST walker over a real shell parser; this package stays dependency-free, so the gap is documented instead of papered over.

A for/while/until loop is covered, because its delay is spelled out in the loop header and body even when the ; separators split it. A loop is only measured by the sleep calls inside it: a loop whose body runs a slow external program (for i in 1 2 3 4 5 6; do ./bench.exe; done) is not refused, since nothing in the text says how long that program takes.

Two further bounds apply, both failing open. A command longer than 64 KiB is analyzed only up to that cut, since real calls average a few hundred characters and scanning a megabyte costs milliseconds per tool call. A heredoc with no terminator swallows the lines after it in real shells too, so echo hi << x followed by sleep 300 is allowed: bash treats the sleep as file content.

tail -f --pid=N is refused on purpose: it ends only when the watched process exits, which may be never or an hour from now.

Test

node test/test.mjs

301 cases across detection, guard exemptions, config handling, blocked-wait bounds, loop bounds, refusal guidance, logging bounds, and a false-positive watch list; exit code 0 means all pass. test/ab.mjs measures the foreground-versus-background wall-clock difference and the guard's own per-call cost.

License

MIT

관련 플러그인