前言¶
在 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 目录页。