ClaudeMods
☰
ZH-TW
● 0 人在線上 · 瀏覽 0 次
贊助提交作品
GitHub 儲存庫 · 發布者 myaji35

harness-guard

將現有 Harness PreToolUse 防護以函式掛鉤的形式進行影子執行或強制執行

myaji35@myaji35

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 完成後會重現工具後的密鑰警告。當現有的 PostToolUse shell 掛鉤仍啟用時,警告可能出現兩次。

內部錯誤會記錄為警告並放行。原有的 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 command field while the other hooks read a tool_input envelope. Sandbox fixtures use the script's top-level shape. The mod sees e.command directly, 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.read and $.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.sh for WARN+T2 before denying. The shell sandbox also calls decision-trace.sh for 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 source accepts arbitrary code in freeze files. The mod reads literal FREEZE_DIR and FREEZE_ISSUE assignments, which covers generated freeze files without executing arbitrary shell code.
  • The post-tool secret warning is reproduced after a Write/Edit tool.call completes. While the existing PostToolUse shell 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.

更多類似作品