dsh-context-guard:DSH 工具輸出預算、落盤與風險命令確認

前言

在 DSH 里長時間開着 bash、persistent-bash 做排查時,常見問題是上下文被大段命令輸出佔滿:ls -la、整文件 cat、journalctl、反覆 curl 輪詢都會按字符進上下文並反覆計費。官方 persistent-bash 默認還會在約 16,000 字符處截斷,落盤證據也可能不完整。

dsh-context-guard 是 DSH 的 Host-only 插件(維護者 person-clouds),在分類上屬於社區目錄的 admin-security,側重上下文 token 優化與安全相關的執行前確認。下面介紹它做什麼、如何安裝,以及使用時的邊界。

這是什麼

dsh-context-guard(npm 包名 @fenghoer/dsh-context-guard,當前資料中版本 0.3.1)在工具結果進入模型上下文之前做預算、落盤、去重與歸檔,並對高風險命令在 DSH 權限策略之後追加一層 ask 警示。它不替代 DSH 自帶的權限與審批,也不做沙箱。

核心功能

資料中歸納的五項能力如下。

有界摘要與完整落盤

輸出超過預算時,完整文本寫入磁盤,上下文只保留 head/tail、命令類型和文件路徑。寫盤失敗時回退原始結果,避免丟證據。

默認預算相關參數:thresholdChars=3000、headChars=1200、tailChars=800,硬頂 12000。不同命令類型有不同預算,例如普通 bash 3000,有界日誌類 4000,構建測試類 8000;非零退出時至少保留 4000。無界命令(如裸 cat、無界的 journalctl、find /)會被壓到 2500 並提示改用有界命令。首行可寫 # dsh-budget=8000 聲明更高預算(最高 12000)。

去重與行級 diff

緩存鍵爲 session + tool + args,TTL 10 分鐘,最近使用會續期。只處理 ≥800 字符的非錯誤結果。命令每次仍會真實執行(不做執行緩存)。相同結果返回 [context-guard:dedup] 與上次 call 指針;變化結果返回 [context-guard:diff],只展示變化塊。緩存僅在進程內內存,重啓後失效。

只讀診斷 inspect.sh

scripts/inspect.sh 用固定子命令替代零散 shell,原則是不使用 sudo、不啓停服務,每段輸出有行數/字節上限。本機可擴展 ~/.dsh/context-guard/inspect.local.sh。

風險命令確認

tools/pre-execute 在 DSH 權限策略通過後再檢查。命中規則時返回 ask,由 DSH 審批端確認;用戶不確認則不執行。默認覆蓋 bash / persistent-bash / terminal-bash 與 web_fetch,包括刪除與 git 破壞、curl | bash、白名單外或明文 HTTP 的下載、系統與數據庫破壞性操作、提權與清歷史等。默認鏈接白名單含 localhost、github.com、gitee.com 等,可用 riskAllowHosts 覆蓋。規則爲正則啓發式,可能誤報或漏報。

壓縮歸檔、指標與清理

壓縮前後寫 STATUS.md,JSONL 記錄節省量(token 按 chars/4 估算並在說明中標註)。按保留期與配額清理磁盤:默認保留 7 天,單會話 50MB / 300 文件,全局 500MB 等;目錄 0700、文件 0600。

安裝與啓用

推薦從 GitHub Release 安裝(無需構建):

dsh plugin --profile web add https://github.com/person-clouds/dsh-context-guard/releases/latest/download/dsh-context-guard.tgz

也可從 npm 或本地源碼安裝:

dsh plugin --profile web add @fenghoer/dsh-context-guard
# 或
dsh plugin --profile web add link:/home/zh/www/personalCloud/task/dsh-context-guard

安裝後執行宿主機遷移腳本(會備份 profile):

task/dsh-context-guard/scripts/apply-profile.sh

腳本會把舊的 compaction-archive 插入項改爲 context-guard,給 persistent-bash 設置 maxOutputChars: 65536(否則插件只能拿到被截斷的 16K),併爲官方 tool-result-pruner 設置 8192/4096/1024。

驗證配置是否生效:

dsh --profile web --dump-config | grep -E "context-guard|maxOutputChars|tool-result-pruner"

插件以當前 DSH 進程的用戶權限運行,安裝前應自行查看 GitHub 倉庫 源碼與 LICENSE;社區目錄 SkillHub 插件頁 與倉庫無 DeepSeek / 幻方官方從屬關係。

典型用法

配置片段

在 profile 中可參照 README 示例調整預算、去重、風險檢查與磁盤配額:

- insert:
    - id: context-guard
      name: '@fenghoer/dsh-context-guard'
      config:
        thresholdChars: 3000
        headChars: 1200
        tailChars: 800
        dedupeMinChars: 800
        dedupTtlMs: 600000
        riskCheckEnabled: true
        retentionDays: 7
        maxSessionBytes: 52428800
        maxTotalBytes: 524288000
        maxFilesPerSession: 300

inspect.sh

task/dsh-context-guard/scripts/inspect.sh overview
task/dsh-context-guard/scripts/inspect.sh service nginx
task/dsh-context-guard/scripts/inspect.sh logs /var/log/syslog 80
task/dsh-context-guard/scripts/inspect.sh ports
task/dsh-context-guard/scripts/inspect.sh docker
task/dsh-context-guard/scripts/inspect.sh report

未安裝 linuxops 時會回退 systemctl、journalctl、ss 等;可選通過 scripts/install-linuxops.sh 安裝插件自帶的只讀兼容層,或用 --binary 指向自建二進制。

輸出與指標路徑

完整日誌與指標默認落在:

${DSH_HOME:-$HOME/.dsh}/tool-output/<session-id>/<timestamp>-<call-id>.log
${DSH_HOME:-$HOME/.dsh}/context-guard/metrics/<session-id>.jsonl
${DSH_HOME:-$HOME/.dsh}/context-guard/metrics/STATUS.md
${DSH_HOME:-$HOME/.dsh}/context-guard/archive/<session-id>/STATUS.md

適用場景與注意

適合需要長時間在 DSH 裏跑 shell 診斷、又不想用大段重複輸出撐爆上下文、同時希望高風險操作多一道審批確認的智能體或運維 profile。默認主要處理 bash 系工具,其他工具可通過 toolNames 配置。

務必注意:去重不跨進程持久化;風險檢查是啓發式保底,不是安全沙箱;無審批端時按 DSH 規則拒絕 ask;插件不做 kill、重啓、備份恢復等寫操作診斷。運行 npm test 需在插件目錄執行 cd task/dsh-context-guard && npm test(資料要求 Node >=22.5.0)。

結尾

dsh-context-guard 把長工具輸出擋在上下文之外並保留落盤證據,配合去重、指標與風險命令 ask,面向 DSH 長會話下的 token 與操作安全兩類痛點。更多說明見 GitHub README 與 SkillHub 目錄頁。

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

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

小夜