context-guard
npx machina-cli add skill littlebearapps/pitchdocs/context-guard --openclawContext Guard
What It Does
Context Guard adds two PostToolUse hooks, one PreToolUse hook, and one quality rule to a project. The PostToolUse hooks detect when AI context files (CLAUDE.md, AGENTS.md, GEMINI.md, .cursorrules, copilot-instructions.md, .windsurfrules, .clinerules) are out of date and nudge the developer to update them. The PreToolUse hook prevents content filter errors by intercepting Write operations on files known to trigger Claude Code's API content filter.
Claude Code only. These hooks use Claude Code's hook system (PostToolUse events, .claude/settings.json configuration). OpenCode, Codex CLI, Cursor, Windsurf, Cline, and Gemini CLI do not support Claude Code hooks. The quality rule (.claude/rules/context-quality.md) is also Claude Code-specific.
Cross-tool features like skills (.claude/skills/) and AGENTS.md work in OpenCode and Codex CLI without Context Guard.
Components
Hook: context-drift-check.sh
- Event: PostToolUse on
Bash(filters forgit commitcommands) - Fires: After a successful git commit
- Checks:
- Compares each context file's last-modified commit vs the most recent source-code commit
- Detects broken file-path references inside context files
- Flags when
package.jsonorpyproject.tomlchanged more recently than context files
- Output: Lists stale files and recommends
/ai-context audit, or stays silent if everything is current - Throttle: Max once per hour via
.git/.context-guard-last-checktimestamp
Hook: context-structural-change.sh
- Event: PostToolUse on
Write|Edit - Fires: After creating or editing structural files (commands, skills, agents, rules, manifests, config)
- Reminds: Which context files may need updating based on the type of change
- File patterns:
commands/*.md→ AGENTS.md, CLAUDE.md, llms.txt.claude/skills/*/SKILL.md→ AGENTS.md, CLAUDE.md, llms.txt.claude/agents/*.md→ AGENTS.md.claude/rules/*.md→ CLAUDE.md, AGENTS.mdpackage.json,pyproject.toml, config files → all context files
Hook: content-filter-guard.sh
- Event: PreToolUse on
Write - Fires: Before Claude Code writes a file
- HIGH-risk files (CODE_OF_CONDUCT.md, LICENSE, SECURITY.md):
- Blocks the write (exit non-zero)
- Returns fetch commands for the canonical URL
- MEDIUM-risk files (CHANGELOG.md, CONTRIBUTING.md):
- Allows the write
- Returns advisory about chunked writing (5–10 entries at a time)
- All other files: Passes through silently
Rule: context-quality.md
Auto-loaded every session. Establishes cross-file consistency, path verification, version accuracy, command accuracy, and a sync-points table mapping project changes to context files.
Installation
The /context-guard install command:
- Copies hook scripts to
.claude/hooks/in the target project - Merges hook configuration into
.claude/settings.json - Copies the quality rule to
.claude/rules/context-quality.md - Makes hook scripts executable
Settings.json Configuration
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write",
"hooks": [{
"type": "command",
"command": ".claude/hooks/content-filter-guard.sh"
}]
}
],
"PostToolUse": [
{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": ".claude/hooks/context-drift-check.sh"
}]
},
{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": ".claude/hooks/context-structural-change.sh"
}]
}
]
}
}
Uninstallation
The /context-guard uninstall command removes all hook scripts (context-drift-check.sh, context-structural-change.sh, content-filter-guard.sh), settings.json entries, and the quality rule.
Customisation
Throttle Interval
Edit context-drift-check.sh line with 3600 (seconds) to change the check interval. Set to 0 to check on every commit.
File Patterns
Edit context-structural-change.sh case statement to add or remove structural file patterns.
Troubleshooting
| Issue | Cause | Fix |
|---|---|---|
| Hooks not firing | Scripts not executable | chmod +x .claude/hooks/*.sh |
| No output after commit | Throttle active | Delete .git/.context-guard-last-check to reset |
| "jq: command not found" | jq not installed | Install jq: apt install jq or brew install jq |
| Hooks fail silently | jq receives malformed JSON | Run .claude/hooks/context-drift-check.sh manually and check for JSON parse errors — common when settings.json has trailing commas or comments |
| Hook errors in logs | Wrong project directory | Check CLAUDE_PROJECT_DIR is set correctly |
| Hook blocks legitimate writes | content-filter-guard too aggressive | Uninstall with /context-guard uninstall, or remove just the PreToolUse entry from .claude/settings.json |
Source
git clone https://github.com/littlebearapps/pitchdocs/blob/main/.claude/skills/context-guard/SKILL.mdView on GitHub Overview
Context Guard installs Claude Code hooks to detect stale AI context files (CLAUDE.md, AGENTS.md, GEMINI.md, etc.), remind developers to update them, and prevent content filter errors when generating standard OSS files. It includes post-commit drift checks, structural-change reminders, a content filter guard, and a Claude Code-specific quality rule.
How This Skill Works
This tool adds two PostToolUse hooks, one PreToolUse hook, and a quality rule to your project. It uses context-drift-check.sh to flag out-of-date context files after commits, context-structural-change.sh to advise updates after structural edits, and content-filter-guard.sh to block risky writes and guide safe chunking. All hooks rely on Claude Code's settings (.claude/settings.json) and the setup is Claude Code-only; OpenCode, Codex CLI, or other tools do not support these hooks, while cross-tool features like .claude/skills/ remain usable.
When to Use It
- When starting a Claude Code-enabled project to ensure all context files stay current (CLAUDE.md, AGENTS.md, GEMINI.md, etc.).
- After refactoring commands, agents, rules, or manifests, to receive reminders about updating context files.
- Before writing files that could trigger Claude Code's content filter to prevent write-time errors.
- After commits to audit drift and verify context files reflect the latest source code.
- When enabling or auditing the context-quality rule to enforce cross-file consistency.
Quick Start
- Step 1: Run /context-guard install in the project to install hooks to .claude/hooks.
- Step 2: Merge the provided settings into .claude/settings.json and ensure the hooks are wired.
- Step 3: Copy the context-quality.md rule to .claude/rules/context-quality.md and make hook scripts executable.
Best Practices
- Run /context-guard install in new projects to install hooks and rules.
- Keep CLAUDE.md, AGENTS.md, GEMINI.md, and related context files in sync with code changes.
- Use context-drift-check.sh after commits to catch stale files early.
- Respect content-filter guards for high-risk files and plan chunked writes for medium-risk files.
- Enable and maintain the context-quality.md rule to enforce cross-file consistency.
Example Use Cases
- A stale CLAUDE.md is detected after dependency bumps; the drift check prompts an ai-context audit.
- Structural changes to commands/agents trigger the structural-change hook, suggesting which context files to refresh.
- Attempting to write CODE_OF_CONDUCT.md is blocked by the content-filter-guard, which provides a canonical URL fetch path.
- Package.json changes cause a drift alert explaining which context files may need updating.
- The context-quality rule surfaces a cross-file mismatch and guides a quick reconciliation across CLAUDE.md and AGENTS.md.