myaji35/GH_Harness/tree/main/mods/harness-guard
关于这个 mod
harness-guard mod
Claude Code 2.1.289 的函数钩子版本,替代五个现有的 PreToolUse shell 钩子。它注册一个 tool.call 处理器。默认的 shadow 模式会评估每次调用,将判定、原因和耗时毫秒数写入 ~/.claude/harness-core/logs/mod-guard-shadow.jsonl,然后调用 next(e)。enforce 模式在判定为拒绝时返回 { deny: message },否则调用 next(e);在该模式下,RTK 重写结果会传给 next。
| 现有钩子 | 移植的规则 |
| --- | --- |
| freeze-guard.sh | 在 Write/Edit 时,只要冻结文件存在,就阻止指向 FREEZE_DIR 以外路径的操作。注册表中带有 issue 的冻结,仅在状态为 IN_PROGRESS 或 BACKGROUND_RUNNING 时生效。 |
| sandbox-enforce.sh | Bash 拦截危险删除、破坏性设备写入、不安全的 chmod、管道到 shell 以及 sudo rm;对强制推送、硬重置、清理、SQL 删除、部署删除和发布使用警告/T2 模式。HARNESS_SANDBOX_BYPASS=1/2 保持 shell 语义。 |
| secret-guard.sh | 对包含 git commit 或 git push 的 Bash 命令,先检查暂存文件名,再检查新增的 diff 行,同时排除测试/规格路径和占位值。Write/Edit 完成后,如果编辑的文件中有密钥,则发出警告。 |
| rtk-guard.sh | 请求 rtk hook claude 重写可能受支持的命令;如果 RTK 不存在或失败,则直接放行。 |
| health-gate.sh | 带有注册表的提交会运行可用的 typecheck、tsc、lint、Rubocop 和 TODO 检查,记录分数历史;只有分数下降至少五分时才发出警告。 |
配置
使用 claude --plugin-dir mods/harness-guard 加载。默认模式是 shadow。要启用强制执行,请在用户设置(~/.claude/settings.json)中加入配置,或在 /config 中选择 Guard mode → enforce:
{"pluginConfigs":{"harness-guard":{"options":{"mode":"enforce"}}}}
比较影子日志期间,请保留现有 shell 钩子。影子健康分数和回归比较使用 .claude/knowledge-db/health-history.mod-shadow.jsonl;shell health-gate 继续使用 .claude/knowledge-db/health-history.jsonl。切换到 enforce 时,同时禁用 shell 的 health-gate.sh 钩子,因为 enforce 模式使用共享历史文件。要回滚,请禁用此 mod 或移除它的 --plugin-dir,如果之前禁用了 shell health-gate,再重新启用它。
验证
test/capture-expected.sh 会创建临时 git 仓库,使用 stdin JSON 执行每个真实的 shell 钩子,并在 test/fixtures.json(source: shell)中记录 38 个 fixture 判定。test/fixtures.ts 根据同一份捕获结果生成,供无法导入 JSON 模块的 Claude Code 测试运行器使用。测试会将每个 fixture 与 mod 对应的判定函数进行比较。测试命令执行前会忽略并删除 ._* AppleDouble 文件,因为 CLI 否则会将它们误认为测试。
已知差异
- 经典 sandbox 钩子要求顶层的
command字段,而其他钩子读取tool_input封装。Sandbox fixture 使用脚本的顶层结构。该 mod 直接读取e.command,因此能一致地检测危险命令;带封装的经典调用可能漏检。 - 为了让完整判定保持在 10 秒以内,该 mod 为健康检查使用 5 秒子预算,并将每个进程限制在 1.2 秒。shell worker 总共允许 45 秒,每项检查允许 20 秒。因此,缓慢的检查可能被跳过或超时,产生不同的分数。
- 提供的 Claude Code API 类型暴露了
$.fs.read和$.fs.write,但没有追加方法。影子日志和健康历史会通过$.fs.write读取并重写;同时运行的 Claude 工作阶段可能丢失一行追加内容。Shell 追加在文件描述符层面是原子的。影子健康历史与 shell 文件分开;只有 enforce 模式会读写 shell 文件。 - 在 enforce 模式下,mod 会在拒绝 WARN+T2 之前调用
request-user-confirm.sh。Shell sandbox 也会为阻止/警告事件调用decision-trace.sh;mod 改用自己的 JSONL 判定记录,因此不会添加旧版追踪行。当 shell 和 mod 同时启用时,T2 辅助程序可能被调用两次。 - Shell
source接受冻结文件中的任意代码。该 mod 读取字面量的FREEZE_DIR和FREEZE_ISSUE赋值,这足以覆盖生成的冻结文件,同时不会执行任意 shell 代码。 - Write/Edit 的
tool.call完成后会重现工具后的密钥警告。当现有的PostToolUseshell 钩子仍启用时,警告可能出现两次。
内部错误会被记录为警告并放行。原有的 freeze、secret 和 health 钩子在解析/检查失败时也会放行;sandbox 的 set -e 在某些主机命令失败时可能以非零状态退出。该 mod 范围收窄的文件系统/进程调用会捕获预期的缺少文件或缺少命令错误,不会因这些失败而阻止操作。
安装
请先查看作者 README 确认 marketplace 和插件名称;命令可能随仓库结构改变。
claude plugin marketplace add myaji35/GH_Harness claude plugin install harness-guard
原文 / README
harness-guard mod
Claude Code 2.1.289 function-hook version of five existing PreToolUse shell hooks. It registers one tool.call handler. The default shadow mode evaluates each call, writes its verdict, reasons, and elapsed milliseconds to ~/.claude/harness-core/logs/mod-guard-shadow.jsonl, then calls next(e). enforce returns { deny: message } for deny decisions and otherwise calls next(e); RTK rewrites are passed to next in that mode.
| Existing hook | Ported rule |
| --- | --- |
| freeze-guard.sh | On Write/Edit, block a path outside FREEZE_DIR while the freeze file exists. A freeze with an issue in the registry is active only for IN_PROGRESS or BACKGROUND_RUNNING. |
| sandbox-enforce.sh | Bash block patterns for dangerous deletion, destructive device writes, unsafe chmod, pipe-to-shell, and sudo rm; warning/T2 patterns for force push, hard reset, cleanup, SQL deletion, deployment deletion, and publishing. HARNESS_SANDBOX_BYPASS=1/2 keeps the shell semantics. |
| secret-guard.sh | On Bash commands containing git commit or git push, inspect staged filenames first, then added diff lines, excluding test/spec paths and placeholder values. After Write/Edit completes, warn about a secret in the edited file. |
| rtk-guard.sh | Ask rtk hook claude for a rewrite of likely supported commands; pass through if RTK is absent or fails. |
| health-gate.sh | On commit with a registry, run available typecheck, tsc, lint, Rubocop, and TODO checks, record score history, and warn only if the score drops at least five points. |
Configuration
Load with claude --plugin-dir mods/harness-guard. The default is shadow. To enforce, put this in user settings (~/.claude/settings.json), or choose Guard mode → enforce in /config:
{"pluginConfigs":{"harness-guard":{"options":{"mode":"enforce"}}}}
Keep the existing shell hooks installed while comparing shadow logs. Shadow health scores and regression comparisons use .claude/knowledge-db/health-history.mod-shadow.jsonl; the shell health-gate continues using .claude/knowledge-db/health-history.jsonl. When switching to enforce, disable the shell health-gate.sh hook at the same time, since enforce mode uses the shared history file. To roll back, disable this mod or remove its --plugin-dir, then re-enable the shell health-gate if it was disabled.
Verification
test/capture-expected.sh creates a temporary git repository, executes each real shell hook with stdin JSON, and records 38 fixture verdicts in test/fixtures.json (source: shell). test/fixtures.ts is generated from the same capture for the Claude Code test runner, which cannot import JSON modules. The test compares every fixture with the mod's corresponding decision function. ._* AppleDouble files are ignored and removed before test commands because the CLI otherwise mistakes them for tests.
Known differences
- The classic sandbox hook expects a top-level
commandfield while the other hooks read atool_inputenvelope. Sandbox fixtures use the script's top-level shape. The mod seese.commanddirectly, so it detects dangerous commands consistently; a classic call with an envelope can miss them. - The mod uses a 5-second sub-budget for health checks and 1.2-second per-process caps to keep the complete decision below 10 seconds. The shell worker allows 45 seconds overall and 20 seconds per check. A slow check can therefore be skipped or timed out and yield a different score.
- The supplied Claude Code API type exposes
$.fs.readand$.fs.write, but no append method. Shadow logs and health history are read and rewritten with$.fs.write; simultaneous Claude sessions can lose an appended line. Shell append is atomic at the file descriptor level. Shadow health history is separate from the shell file; only enforce mode reads and writes the shell file. - In enforce mode the mod calls
request-user-confirm.shfor WARN+T2 before denying. The shell sandbox also callsdecision-trace.shfor block/warn events; the mod uses its JSONL decision record instead, so it does not add a legacy trace line. While both shell and mod are enabled, the T2 helper can be called twice. - Shell
sourceaccepts arbitrary code in freeze files. The mod reads literalFREEZE_DIRandFREEZE_ISSUEassignments, which covers generated freeze files without executing arbitrary shell code. - The post-tool secret warning is reproduced after a Write/Edit
tool.callcompletes. While the existingPostToolUseshell hook remains enabled, the warning can appear twice.
An internal error is recorded as a warning and passed through. The original freeze, secret, and health hooks also pass on parse/check failures; sandbox's set -e can exit nonzero for some host command failures. The mod's narrow filesystem/process calls catch expected missing-file or missing-command errors and do not block on those failures.
