gateguard
affaan-m/ECC
強制 AI 代理在編輯或執行具破壞性的指令前先進行調查,並透過要求提供具體事實(例如匯入程式、資料結構與使用者指示)來提升程式碼品質。
...展開全部GateGuard — 強制查證的行動前關卡
一個 PreToolUse 鉤子,用以強制 Claude 在編輯前進行調查。它摒棄自我評估(「你確定嗎?」),轉而要求具體事實。調查的過程所帶來的覺察,是自我評估所無法達到的。
何時啟用
- 在任何檔案編輯會影響多個模組的程式碼庫中工作時
- 專案中的資料檔案具有特定結構或日期格式
- AI 生成的程式碼必須符合現有模式的團隊
- 任何 Claude 傾向於憑直覺推測而非進行調查的工作流程
核心概念
大型語言模型(LLM)的自我評估並不奏效。若詢問「你是否違反任何政策?」,答案總是「沒有」。這點已透過實驗驗證。
但若詢問「列出所有導入此模組的檔案」,則會迫使 LLM 執行 Grep 和 Read 操作。這項調查本身所建立的語境,會改變輸出結果。
三階段門檻:
1. DENY — block the first Edit/Write/Bash attempt
2. FORCE — tell the model exactly which facts to gather
3. ALLOW — permit retry after facts are presented
沒有任何競爭對手能同時做到這三點。多數僅止步於「否認」。
證據
兩項獨立的 A/B 測試,使用相同的代理程式,執行相同的任務:
| 任務 | 設有門檻 | 無門控 | 差距 |
|---|---|---|---|
| 分析模組 | 8.0/10 | 6.5/10 | +1.5 |
| Webhook 驗證器 | 10.0/10 | 7.0/10 | +3.0 |
| 平均 | 9.0 | 6.75 | +2.25 |
這兩款工具生成的程式碼都能執行並通過測試。兩者的差異在於設計的深度。
閘門類型
編輯/多重編輯閘門(每檔的首次編輯)
多重編輯的處理方式完全相同——批次中的每個檔案都會被個別進行閘控。
Before editing {file_path}, present these facts:
1. List ALL files that import/require this file (use Grep)
2. List the public functions/classes affected by this change
3. If this file reads/writes data files, show field names, structure,
and date format (use redacted or synthetic values, not raw production data)
4. Quote the user's current instruction verbatim
寫入閘門(首次建立新檔案)
Before creating {file_path}, present these facts:
1. Name the file(s) and line(s) that will call this new file
2. Confirm no existing file serves the same purpose (use Glob)
3. If this file reads/writes data files, show field names, structure,
and date format (use redacted or synthetic values, not raw production data)
4. Quote the user's current instruction verbatim
破壞性 Bash 閘門(每個破壞性指令)
觸發條件: rm -rf, git reset --hard, git push --force, drop table等。
1. List all files/data this command will modify or delete
2. Write a one-line rollback procedure
3. Quote the user's current instruction verbatim
例行 Bash 閘門(每場工作階段一次)
1. The current user request in one sentence
2. What this specific command verifies or produces
快速入門
選項 A:使用 ECC 掛鉤(零安裝)
位於 scripts/hooks/gateguard-fact-force.js 已包含於此外掛中。請透過 hooks.json 啟用它。
若 `GateGuard` 阻礙設定或修復作業,請以
ECC_GATEGUARD=off。若需在鉤子層級進行控制,請繼續使用
ECC_DISABLED_HOOKS 並搭配 GateGuard 掛鉤 ID 繼續操作。
在長時間的會話中,僅會輸出第一個 GATEGUARD_FACT_FORCE_FULL_DENIALS
fact-force 拒絕(預設為 3 次)會發出完整的四項事實阻擋訊息;後續的
拒絕則會濃縮為一行,僅包含拒絕序號,因此
幾乎相同的阻擋訊息不會在上下文視窗中累積,
進而加劇模型重複迴圈(#2142)。 在呈現事實後重新嘗試同一個檔案或
指令,絕不會再次觸發閘門。
選項 B:包含設定檔的完整套件
pip install gateguard-ai
gateguard init
此選項新增 .gateguard.yml 用於專案層級的設定(自訂訊息、忽略路徑、閘門切換)。
反模式
- 切勿改用自我評估。「您確定嗎?」這個問題總是會得到「是」的回答。這點已透過實驗驗證。
- 切勿跳過資料結構檢查。兩組 A/B 測試代理程式都假設實際資料採用 ISO-8601 日期格式
%Y/%m/%d %H:%M時,兩套 A/B 測試代理程式皆預設採用 ISO-8601 日期格式。檢查資料結構(並對值進行遮蔽處理)可避免這類錯誤的發生。 - 請勿對每個 Bash 指令都設置門控。常規 Bash 指令每場次僅需門控一次;具破壞性的 Bash 指令則需每次門控。這種平衡既能避免系統變慢,又能有效防範真實風險。
最佳實務
- 讓門控機制自然觸發。不要試圖預先回答門控問題——調查過程本身才是提升品質的關鍵。
- 根據您的領域自訂檢查提示訊息。若您的專案有特定規範,請將其加入檢查提示中。
- 使用
.gateguard.yml來忽略類似.venv/,node_modules/,.git/.
相關技能
safety-guard—— 執行時安全檢查(互補而非重疊)code-reviewer— 編輯後審查(GateGuard 屬於編輯前檢查)
---
name: gateguard
description: Forces AI agents to investigate before editing or running destructive commands, improving code quality by requiring concrete facts like importers, data schemas, and user instructions.
---
# GateGuard — Fact-Forcing Pre-Action Gate
A PreToolUse hook that forces Claude to investigate before editing. Instead of self-evaluation ("are you sure?"), it demands concrete facts. The act of investigation creates awareness that self-evaluation never did.
## When to Activate
- Working on any codebase where file edits affect multiple modules
- Projects with data files that have specific schemas or date formats
- Teams where AI-generated code must match existing patterns
- Any workflow where Claude tends to guess instead of investigating
## Core Concept
LLM self-evaluation doesn't work. Ask "did you violate any policies?" and the answer is always "no." This is verified experimentally.
But asking "list every file that imports this module" forces the LLM to run Grep and Read. The investigation itself creates context that changes the output.
**Three-stage gate:**
```
1. DENY — block the first Edit/Write/Bash attempt
2. FORCE — tell the model exactly which facts to gather
3. ALLOW — permit retry after facts are presented
```
No competitor does all three. Most stop at deny.
## Evidence
Two independent A/B tests, identical agents, same task:
| Task | Gated | Ungated | Gap |
| --- | --- | --- | --- |
| Analytics module | 8.0/10 | 6.5/10 | +1.5 |
| Webhook validator | 10.0/10 | 7.0/10 | +3.0 |
| **Average** | **9.0** | **6.75** | **+2.25** |
Both agents produce code that runs and passes tests. The difference is design depth.
## Gate Types
### Edit / MultiEdit Gate (first edit per file)
MultiEdit is handled identically — each file in the batch is gated individually.
```
Before editing {file_path}, present these facts:
1. List ALL files that import/require this file (use Grep)
2. List the public functions/classes affected by this change
3. If this file reads/writes data files, show field names, structure,
and date format (use redacted or synthetic values, not raw production data)
4. Quote the user's current instruction verbatim
```
### Write Gate (first new file creation)
```
Before creating {file_path}, present these facts:
1. Name the file(s) and line(s) that will call this new file
2. Confirm no existing file serves the same purpose (use Glob)
3. If this file reads/writes data files, show field names, structure,
and date format (use redacted or synthetic values, not raw production data)
4. Quote the user's current instruction verbatim
```
### Destructive Bash Gate (every destructive command)
Triggers on: `rm -rf`, `git reset --hard`, `git push --force`, `drop table`, etc.
```
1. List all files/data this command will modify or delete
2. Write a one-line rollback procedure
3. Quote the user's current instruction verbatim
```
### Routine Bash Gate (once per session)
```
1. The current user request in one sentence
2. What this specific command verifies or produces
```
## Quick Start
### Option A: Use the ECC hook (zero install)
The hook at `scripts/hooks/gateguard-fact-force.js` is included in this plugin. Enable it via hooks.json.
If GateGuard blocks setup or repair work, start the session with
`ECC_GATEGUARD=off`. For hook-level control, keep using
`ECC_DISABLED_HOOKS` with the GateGuard hook ID.
In long sessions, only the first `GATEGUARD_FACT_FORCE_FULL_DENIALS`
fact-force denials (default 3) emit the full four-fact block; later
denials are condensed to a single line carrying the denial ordinal, so
near-identical blocks cannot accumulate in the context window and
amplify model repetition loops (#2142). Retrying the same file or
command after presenting facts never re-triggers the gate.
### Option B: Full package with config
```bash
pip install gateguard-ai
gateguard init
```
This adds `.gateguard.yml` for per-project configuration (custom messages, ignore paths, gate toggles).
## Anti-Patterns
- **Don't use self-evaluation instead.** "Are you sure?" always gets "yes." This is experimentally verified.
- **Don't skip the data schema check.** Both A/B test agents assumed ISO-8601 dates when real data used `%Y/%m/%d %H:%M`. Checking data structure (with redacted values) prevents this entire class of bugs.
- **Don't gate every single Bash command.** Routine bash gates once per session. Destructive bash gates every time. This balance avoids slowdown while catching real risks.
## Best Practices
- Let the gate fire naturally. Don't try to pre-answer the gate questions — the investigation itself is what improves quality.
- Customize gate messages for your domain. If your project has specific conventions, add them to the gate prompts.
- Use `.gateguard.yml` to ignore paths like `.venv/`, `node_modules/`, `.git/`.
## Related Skills
- `safety-guard` — Runtime safety checks (complementary, not overlapping)
- `code-reviewer` — Post-edit review (GateGuard is pre-edit investigation)
所有檔案
1 個檔案安裝 gateguard
請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/affaan-m/ECC/tree/main/skills/gateguard # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
