Communitygithub.com

GateGuard — 強制事實核查的執行前閘門

一個強制事實核查的閘門,會阻擋編輯/寫入/Bash(包含多重編輯)動作,並要求具體的調查(導入者、資料結構、使用者指令),然後才允許執行。與未設閘門的代理相比,可測量地將輸出品質提升 +2.25 分。

GateGuard — 強制事實核查的執行前閘門 是什麼?

GateGuard 是一個預先執行的鉤子(PreToolUse hook),專門設計用來強制 AI 代理在執行編輯、寫入或 Bash 指令之前,先進行具體的事實調查。與傳統的自我評估(例如「你確定嗎?」)不同,GateGuard 明確要求代理提供如導入檔案列表、受影響的函數、資料結構,以及使用者原始指令等具體事實。這種強制調查的機制能顯著提升模型意識,避免 AI 單純猜測,進而產出更準確、更符合專案慣例的程式碼。根據兩項獨立 A/B 測試,使用 GateGuard 的代理在分析模組和 Webhook 驗證器任務上,平均得分達到 9.0/10,相較於未使用的 6.75/10,有著 +2.25 分的顯著提升。它支援編輯/寫入、破壞性 Bash 和例行性 Bash 等不同閘門類型,並可透過環境變數進行細緻控制,例如關閉閘門、豁免特定檔案,或調整完整拒絕訊息的次數。

相容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/gateguard

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

GateGuard — 強制事實核查的執行前閘門 是做什麼的?

一個 PreToolUse 鉤子,強制 Claude 在編輯之前進行調查。不是自我評估(「你確定嗎?」),而是要求具體的事實。調查這一行為本身所創造的覺察,是自我評估永遠無法達到的。

何時啟用

  • 在檔案編輯會影響多個模組的任何程式碼庫上工作
  • 專案中包含具有特定結構或日期格式的資料檔案
  • 團隊要求 AI 生成的程式碼必須符合現有模式
  • 任何 Claude 傾向於猜測而非調查的工作流程

核心概念

LLM 的自我評估無效。問「你是否違反了任何策略?」答案永遠是「沒有。」這已經過實驗驗證。

但問「列出所有導入此模組的檔案」會強迫 LLM 執行 Grep 和 Read。調查本身創造了改變輸出的上下文。

三階段閘門:

1. DENY  — 阻擋第一次 Edit/Write/Bash 嘗試
2. FORCE — 明確告知模型需要收集哪些事實
3. ALLOW — 在事實呈現後允許重試

沒有競爭對手能做到全部三點。大多數只停留在拒絕。

證據

兩個獨立的 A/B 測試,相同的代理,相同的任務:

任務有閘門無閘門差距
分析模組8.0/106.5/10+1.5
Webhook 驗證器10.0/107.0/10+3.0
平均9.06.75+2.25

兩個代理產生的程式碼都能執行並通過測試。差別在於設計深度。

閘門類型

編輯 / 多重編輯閘門(每個檔案的第一次編輯)

多重編輯的處理方式相同 — 批次中的每個檔案都會被獨立設閘。

在編輯 {file_path} 之前,請提供以下事實:

1. 列出所有導入/引用此檔案的全部檔案(搜尋整個樹狀結構 — 透過 Glob/Grep,或經由 Bash 使用 find/grep)
2. 列出受此變更影響的公開函數/類別
3. 如果此檔案讀取/寫入資料檔案,請顯示欄位名稱、結構
   和日期格式(使用編輯過或合成的值,而非原始生產資料)
4. 逐字引用使用者當前的指令

寫入閘門(首次建立新檔案)

在建立 {file_path} 之前,請提供以下事實:

1. 指出會呼叫此新檔案的檔案名稱和行號
2. 確認沒有現有檔案能達到相同目的(搜尋整個樹狀結構 — 透過 Glob/Grep,或經由 Bash 使用 find/grep)
3. 如果此檔案讀取/寫入資料檔案,請顯示欄位名稱、結構
   和日期格式(使用編輯過或合成的值,而非原始生產資料)
4. 逐字引用使用者當前的指令

破壞性 Bash 閘門(每次破壞性指令)

觸發條件:rm -rf, git reset --hard, git push --force, drop table 等。

1. 列出此指令將修改或刪除的所有檔案/資料
2. 寫出一行的復原程序
3. 逐字引用使用者當前的指令

例行性 Bash 閘門(每個會話一次)

1. 用一句話描述目前的使用者請求
2. 這個特定指令驗證或產生了什麼

平行批次與部分套用

首次觸碰閘門會獨立評估每個工具呼叫。當對一個尚未被觸及的檔案發送多個編輯指令到同一個平行批次時,第一個呼叫會被拒絕,而拒絕會將該檔案標記為已檢查,因此該批次中的同級編輯仍會被套用。沒有什麼會回滾:檔案最終可能包含同級編輯,但不包含被拒絕的那個。

拒絕訊息會指名該檔案,並警告批次同級編輯可能已經被套用。請嚴格遵循以下規則:

  • 將對尚未觸及檔案的相依編輯,以序列方式而非平行批次方式發送。定義與其首次使用,或導入與其呼叫點,不得在同一批次中。
  • 在首次觸碰被拒絕後,提供事實,重試被拒絕的編輯,並在基於批次中其他任何內容繼續建構之前,重新讀取該檔案。

批次層級鎖定是不可能的:鉤子一次只能看到一個工具呼叫,因此閘門無法知道哪些呼叫是同時到來的。

快速開始

選項 A:使用 ECC 鉤子(零安裝)

位於 scripts/hooks/gateguard-fact-force.js 的鉤子已包含在此插件中。透過 hooks.json 啟用它。

如果 GateGuard 阻擋了設定或修復工作,請以 ECC_GATEGUARD=off 啟動會話。如需鉤子層級的控制,請繼續搭配 GateGuard 鉤子 ID 使用 ECC_DISABLED_HOOKS。

在長時間會話中,只有前 GATEGUARD_FACT_FORCE_FULL_DENIALS 次(預設 3 次)的強制事實拒絕會發出完整的四個事實區塊;後續的拒絕會濃縮為一行,攜帶拒絕序號,這樣一來,近乎相同的區塊就不會在對話視窗中累積,進而放大模型重複的循環(#2142)。在呈現事實後重試同一個檔案或指令,永遠不會再次觸發閘門。

漸進式控制

ECC_GATEGUARD=off(或 GATEGUARD_DISABLED=1)會完全關閉閘門。下表中的變數不會 — 每個變數只會縮窄一個行為,而承載負載的破壞性 Bash 檢查仍會持續執行:

變數預設值效果
GATEGUARD_BASH_ROUTINE_DISABLED未設定(閘門開啟)僅停用例行性 Bash 閘門。破壞性 Bash 閘門(rm -rf, git reset --hard, drop table, dd if=, …)不受影響。
GATEGUARD_EXEMPT_GLOBS未設定(無豁免)逗號分隔的 glob 模式;符合條件的 Edit/Write/MultiEdit 目標會跳過首次觸碰的事實強制。適用於導入價值低的樹狀結構(測試、生成產物、暫存目錄),在這些地方「誰導入此檔案 / 什麼結構」沒有意義。
GATEGUARD_FACT_FORCE_FULL_DENIALS3多少次拒絕會發出完整的四個事實區塊,之後的拒絕會濃縮為一行。0 表示從第一次拒絕就開始濃縮。
GATEGUARD_BASH_EXTRA_DESTRUCTIVE未設定額外的破壞性指令模式,作為正規表達式來源,加入內建集合中。格式錯誤的正規表達式會被視為未設定(內建仍會套用)並記錄一次到 stderr。
GATEGUARD_STATE_DIR~/.gateguard每個會話的閘門狀態存放位置。如果無法持久化狀態,閘門會允許操作以避免循環,並在警告中指名此變數。

GATEGUARD_BASH_ROUTINE_DISABLED 接受 1, true, on, enabled, enable, 或 yes(不區分大小寫與空白);任何其他值將保持閘門開啟。

完全關閉閘門

變數效果
ECC_GATEGUARD=off停用該會話的 GateGuard。接受 0, false, off, disabled, 或 disable。
GATEGUARD_DISABLED=1效果相同。僅辨識 1 — 上述的拼寫不適用於此。

如需鉤子層級的控制,請繼續搭配 GateGuard 鉤子 ID 使用 ECC_DISABLED_HOOKS。

GATEGUARD_EXEMPT_GLOBS 的 Glob 語意

模式比對整個相對於專案的目標路徑。專案根目錄為 CLAUDE_PROJECT_DIR,若無則回退到鉤子 payload 的 cwd,再回退到鉤子程序的工作目錄。相對 glob 從不豁免該根目錄之外的目標。明確的絕對 glob 比對整個絕對目標路徑,並可能刻意豁免專案之外的路徑。

模式和路徑都使用 / 分隔符號並進行小寫比對。* 比對一個區段內的字元,** 跨區段比對,? 比對一個非分隔符號字元。**/ 包含零個目錄,因此 **/tests/** 也會比對到 tests/foo.js。格式錯誤的模式會被丟棄,不會授予豁免。

自 2.2.1 版起,services/** 僅涵蓋專案根目錄的 services 樹狀結構,*.md 僅涵蓋其根目錄的 Markdown 檔案。對於專案內的所有 Markdown 檔案,請使用 **/*.md。現有的未錨定豁免可能需要調整:

{
  "env": {
    "GATEGUARD_BASH_ROUTINE_DISABLED": "1",
    "GATEGUARD_EXEMPT_GLOBS": "**/tests/**,tests/**,**/*.test.*,**/docs/**,**/dist/**"
  }
}

選項 B:包含設定的完整套件

pip install gateguard-ai
gateguard init

這會新增 .gateguard.yml 用於每個專案的設定(自訂訊息、忽略路徑、閘門開關)。

反模式

  • 不要使用自我評估來代替。 「你確定嗎?」永遠會得到「確定。」這已經過實驗驗證。
  • 不要跳過資料結構檢查。 兩個 A/B 測試代理都假設日期是 ISO-8601 格式,但實際資料使用的是 %Y/%m/%d %H:%M。檢查資料結構(使用編輯過的值)可以預防這整個類別的錯誤。
  • 不要對每個 Bash 指令都設閘。 例行性 Bash 每個會話只設閘一次。破壞性 Bash 則每次都設閘。這種平衡可以在避免減速的同時,捕捉真正的風險。

最佳實務

  • 讓閘門自然觸發。不要試圖預先回答閘門的問題 — 調查本身才是提升品質的關鍵。
  • 根據您的領域自訂閘門訊息。如果您的專案有特定的慣例,請將它們加入閘門提示中。
  • 使用 .gateguard.yml 忽略如 .venv/, node_modules/, .git/ 等路徑。

相關技能

  • safety-guard — 執行時安全檢查(互補,不重疊)
  • code-reviewer — 編輯後審查(GateGuard 是編輯前調查)

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

相關技能