dsh-pwsh-guard:在 Shell 工具上拦截文本读写,引导智能体用 read / grep / glob

前言

在 DeepSeek Harness(DSH)里,智能体往往通过 pwsh、bash、ops_bash 执行命令。常见问题是:用 Get-Content、cat、rg、Select-String 等做文件读取或搜索,输出混在终端里,难分页、难定位,也和 Harness 提供的 read、grep、glob 工具重复。

dsh-pwsh-guard 在命令真正执行前挂一层 ctx.tools.guard:若命令行属于「用 Shell 做文本 I/O」这一类,直接拒绝,并在拒绝原因里写明应改用哪类工具。下文介绍它的规则、配置与安装方式。资料来源为 GitHub 仓库 与 SkillHub 目录页;社区目录为独立站点,与 DeepSeek / 幻方无官方从属关系。

这是什么

dsh-pwsh-guard 是维护者 Steven-Qiang 发布的 DSH 插件(npm 包名 dsh-pwsh-guard,当前资料中版本为 0.1.0,MIT 许可证)。一句话定位:读文件用 read,搜索用 grep,找文件用 glob——不要用 Shell 文本命令替代。

它覆盖的工具通道为 pwsh、bash、ops_bash。拒绝提示会跟随宿主语言(英文或中文)。

核心功能:内置规则与误拦控制

插件按规则组拦截命令,并在拒绝信息中给出替代工具。内置规则摘要如下(完整表格见仓库 README)。

规则组 拦截示例 应改用
textSearch Select-String、sls、findstr、grep、rg grep
textRead Get-Content、gc、cat、type、more、head、tail read
runtimeBypass node -e、python -c、php -r、以及通过 [IO.File]::ReadAllText 读文件 read、grep
sleep Start-Sleep、sleep job_output 等待、schedule_create
stderrMerge 2>&1 后接管道 $LASTEXITCODE、2>err.txt

为降低误拦,有两条匹配策略:

  1. 短别名只在「命令位置」匹配(行首或 ;、|、&、(、{、$( 之后),因此 Get-ChildItem -Type f、git gc、git log --grep=x 可通过。
  2. 引号内的片段不参与匹配,因此 git commit -m "stop using Select-String" 可通过。检查脚本体的规则(如 node -e ...)仍读取原始字符串。

README 说明:本插件不是沙箱;破坏性命令与密钥访问应使用专用 guard(如 cc-safety-net)。后者规则模式难以表达「禁止某命令的一切用法」,因此需要本插件这类按命令名拦截的方案。

配置

配置字段均为可选,在 profile 中形如:

- id: pwsh-guard
  name: 'dsh-pwsh-guard'
  config:
    enabled: true
    language: auto          # zh | en | auto
    tools: [pwsh, bash, ops_bash]
    disabledGroups: []      # textSearch | textRead | runtimeBypass | sleep | stderrMerge
    allowPatterns: []       # 对原始命令行做正则匹配,命中则放行
    rules: []               # 额外规则,先于内置规则评估

language: auto 时依次参考 DSH_LOCALE、宿主 locale、LANG / LC_ALL。

可追加自定义规则,例如禁止强制推送:

    rules:
      - id: no-force-push
        pattern: 'git\s+push\s+--force'
        title: 'Force push is blocked'
        hint: 'Use --force-with-lease instead'

安装与启用

从 npm 安装:

plugin_manager action: install_bundle target: dsh-pwsh-guard

从 GitHub 安装:

plugin_manager action: install_bundle target: https://github.com/Steven-Qiang/dsh-pwsh-guard

本地开发目录(link:)安装时,README 给出 git clone 与 install_bundle 指向本地路径的示例;正文任务要求不展开 clone 流程,需要时直接查阅仓库 Installation 小节。

修改代码或配置后,可不重启进程而重载插件:

plugin_manager action: set_plugin target: include:pwsh-guard enabled: false
plugin_manager action: set_plugin target: include:pwsh-guard enabled: true

卸载:

plugin_manager action: remove_bundle target: dsh-pwsh-guard

若 install_bundle 报 failedAt: registry,可能是 profile 里其他依赖失败;README 建议先检查 node_modules/dsh-pwsh-guard/index.js 与 id: pwsh-guard 配置行是否已写入,再重试。

典型行为说明

启用后,智能体在 pwsh / bash / ops_bash 里发起上述类别的命令时,会在执行前收到拒绝,并看到应使用的工具名。未列出的能力(例如用 Get-ChildItem、ls、dir 列目录)不在本插件范围内。

适用场景与限制

适合谁: 希望统一智能体工作流、减少「Shell 里读全文 / 全库 grep」的团队或个人;分类上属于 DSH 生态中的「工作流」类插件。

已知限制(来自 README):

  • 引号内的命令名(如 & "Select-String"、powershell -Command "Get-Content x")无法检测。
  • 不解码 -EncodedCommand。
  • ops_bash --script_path 仅按路径检查,不检查脚本内容。
  • 目录列举不在拦截范围。

安全提示: 插件以当前 DSH 进程权限运行,安装前请自行查看源码与许可证(MIT)。它不是隔离环境,不能替代专门的安全 guard。

结尾

dsh-pwsh-guard 用工具 guard 把「Shell 文本 I/O」挡在门外,并把智能体指回 read、grep、glob,适合作为 DSH profile 里的一层工作流约束。

  • 目录页:https://www.skillhub.cn/plugins/Steven-Qiang/dsh-pwsh-guard
  • 源码与文档:https://github.com/Steven-Qiang/dsh-pwsh-guard
羽毛球分组比赛记分
小程序二维码

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

Xiaoye