dsh-block-hanging-commands:在 Shell 執行前攔截會掛起的命令

前言

在 DeepSeek Harness(DSH)裏讓智能體跑 Shell 時,常見一類失敗:模型發出 git commit 卻沒帶 -m,或執行 npm run dev、python3 後沒有後續參數。命令在等鍵盤輸入,而無人輸入;工具往往等到超時,這一輪幾乎學不到有效反饋,下一輪還可能再試同樣會掛起的調用。

手工在提示詞裏強調「不要交互式命令」能緩解一部分,但無法覆蓋管道、&& 鏈、包裝命令等組合。下面介紹社區插件 dsh-block-hanging-commands(維護者 acus9345),它在命令真正執行前做靜態檢查,對會等待輸入或長期不退出的調用直接拒絕,並把原因與可替代寫法返回給模型。

這是什麼

dsh-block-hanging-commands 是 DeepSeek Harness 的插件,分類爲 admin-security。它在 tools/pre-execute 階段讀取即將執行的 Shell 命令,對匹配規則的調用返回 deny 及說明文字,不改寫命令、不啓動 Shell;模型可在同一步根據提示改用非交互方式。

源碼與說明見 GitHub 倉庫,社區目錄頁:skillhub.cn/plugins/acus9345/dsh-block-hanging-commands。該目錄爲獨立社區站點,與 DeepSeek / 幻方無官方從屬關係。

核心功能

規則覆蓋範圍

插件對命令做詞法級解析(不 spawn Shell),並識別常見包裝(sudo、env、nohup、npx、bash -c 等)以及管道、&&、heredoc、重定向。README 中列出的規則與示例如下。

規則 典型命令 建議替代
editor vim、nano、code --wait 使用編輯工具或 sed -i
git-editor 無 -m 的 git commit、git tag -a git commit -m "..." 或 --no-edit
git-interactive git rebase -i、git add -p 非交互 rebase / git add <path>
repl python、node、psql mydb -c "..."、腳本文件或 heredoc
server npm run dev、vite、next dev run_in_background: true 或 timeout
watch jest --watch、tsc -w 去掉 watch,單次執行
follow tail -f、kubectl logs -f tail -n 200、--since 等
remote-shell 無遠程命令的 ssh host 附帶遠程命令;-o BatchMode=yes
container-tty docker run -it、kubectl exec -it 去掉 -it
tui htop、lazygit 批處理形式(如 top -bn1)
prompt / login 無 -y 的 npm init、gh auth login 非交互標誌或向用戶確認
pager、tty-prompt less、sudo 要密碼 僅在 PTY Shell 下啓用相關規則

攔截時,模型會收到類似下面的反饋(摘自 README):

dsh-block-hanging-commands stopped `git commit` before it ran, because `git commit`
without a message opens an editor, so the tool call would just sit there until it timed out.
Instead: pass the message: `git commit -m "<message>"` (or `--no-edit` when amending).
(rule: git-editor)

放行與例外

爲降低誤攔,以下情況會放行:psql -c "select 1"、node script.js、git commit -F msg.txt、ssh host uptime、--help / --version 等。echo q | python3、python3 < script.py 因輸入已就緒而不攔。timeout 30 npm run dev、帶 run_in_background: true 的 dev server,或 Shell 工具自身 timeoutMs 能界定時長的調用也會放行。Windows 上通過 PowerShell(pwsh)執行時,插件同樣處理 npm.cmd、路徑形式的可執行文件等。

安裝與啓用

DSH 按 profile 維護插件列表:web(dsh web 瀏覽器 UI)、headless(腳本與 CI)、acp(編輯器集成)。在你會用到的每個 profile 上單獨安裝。

以 web profile 爲例:

dsh plugin --profile web add dsh-block-hanging-commands

插件自帶 patch 層,安裝後即生效,無需額外開關。卸載:

dsh plugin --profile web remove dsh-block-hanging-commands

配置

可在 DSH 側邊欄 Plugins 中打開 dsh-block-hanging-commands,修改後 Save,下一次工具調用即生效,無需重啓。

也可在 ~/.dsh/profiles/<profile>/cordis.patch.yml,或全局 ~/.dsh/cordis.patch.yml 中寫入:

- id: block-hanging-commands
  config:
    mode: deny             # 或 ask:彈出確認而非直接拒絕
    tools: [bash, pwsh]    # 要檢查的 Shell 工具名(Windows 爲 pwsh)
    pty: false             # Shell 是否在 PTY 上運行(如 dsh-tool-bash-persistent)
    disabledRules: []      # 例如 [server] 以允許啓動 dev server
    allowPatterns: []      # 正則白名單,如 ['^make serve$']

未填寫的鍵保持默認。pty 爲 false 時,less、sudo 等依賴終端的規則不會啓用,以避免在無 PTY 環境下的誤報。

適用場景與注意

適合在 DSH 中頻繁讓智能體執行 Shell、又希望減少「整輪掛死」的團隊或個人。插件以當前 dsh 進程權限 運行,安裝前建議閱讀 源碼 與 MIT 許可證,確認行爲符合你的安全策略。

若你確實需要模型啓動 dev server 或 watch 模式,可通過 disabledRules 或 allowPatterns 放寬;mode: ask 則在攔截前走人工確認。

結尾

dsh-block-hanging-commands 把「會掛起的 Shell」從執行階段前移到可解釋的拒絕與替代建議,減輕超時空轉。更多信息見 社區目錄頁 與 GitHub。

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

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

小夜