agentleFS
littlebearappslittlebearapps/contextdocs/.claude/skills/context-guard/SKILL.md

context-guard

Skill5 starsChanged 6 months agoMITView on GitHub

Installs Claude Code hooks that keep AI context files fresh. Two-tier enforcement — Tier 1 nudges at session end, Tier 2 blocks commits when structural files are staged without context updates. Includes content filter, drift check, structural change reminder, SessionStart health check, and the context-updater agent.

  • Installs packages

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Default: Tier 1 only. Add Tier 2 with install strict.

    line 37
  2. Very specific:

    If your repository uses the .agents/ layout instead of .claude/, apply the same AGENTS-first update order to the equivalent skill and agent …

    line 55

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Context Guard

What It Does

Hooks and a quality rule to keep AI context files in sync with the codebase. Prevents content filter errors on standard OSS files. Two-tier enforcement for context doc freshness.

Primary platform: Claude Code (full automation, 12 hook events). Other platforms with hook support — Gemini CLI (11 events), Copilot (8, preview), Cursor (4+), Cline (3) — can use adapted hooks from platforms/ packages (coming soon). For platforms without hooks (Codex CLI, OpenCode, Windsurf), use bin/context-verify.sh as a manual or CI-based alternative.

Enforcement Tiers

TierNameMechanismBehaviour
1NudgeStop hookAdvisory — suggests updating context docs before session ends
2GuardPreToolUse on git commitBlocking — prevents commits when structural files staged without context updates (exit 2)

Default: Tier 1 only. Add Tier 2 with install strict.

Components

content-filter-guard.sh (PreToolUse → Write)

  • HIGH-risk (CODE_OF_CONDUCT.md, LICENSE, SECURITY.md): blocks write, returns fetch commands for canonical URL
  • MEDIUM-risk (CHANGELOG.md, CONTRIBUTING.md): allows write, advises chunked writing
  • All other files: passes through silently

context-structural-change.sh (PostToolUse → Write|Edit)

Fires after creating/editing structural files. Reminds which context files may need updating:

  • commands/*.md, .claude/skills/*/SKILL.md, .agents/skills/*/SKILL.md → update AGENTS.md first, then CLAUDE.md, llms.txt, and any affected bridge files
  • .claude/agents/*.md, .agents/agents/*.md → AGENTS.md, llms.txt
  • .claude/rules/*.md → AGENTS.md and CLAUDE.md if rule references changed
  • Config files (package.json, pyproject.toml, etc.) → update AGENTS.md first, then only the bridge files with tool-specific command/tooling notes

If your repository uses the .agents/ layout instead of .claude/, apply the same AGENTS-first update order to the equivalent skill and agent paths above.

context-drift-check.sh (PostToolUse → Bash)

Fires after git commit. Compares context file last-modified commit vs most recent source commit. Detects broken path references. Throttled to max once per hour via .git/.context-guard-last-check.

context-guard-stop.sh (Stop — Tier 1)

Fires on session end. Checks for uncommitted structural changes without context updates. Returns {"decision": "block"} if drift detected. Checks stop_hook_active flag to prevent infinite loops.

context-commit-guard.sh (PreToolUse → Bash — Tier 2)

Fires before git commit. Checks staging area for structural files without context files. Exit 2 blocks the commit.

context-session-start.sh (SessionStart)

Fires at session start. Quick context health check: counts context files, detects stale files (source commits ahead), warns if aggregate line count exceeds budget. Advisory only — cannot block session start.

context-forced-eval.sh (UserPromptSubmit)

Fires on every user prompt. Checks for context-related keywords and injects a skill evaluation sequence that instructs Claude to evaluate and activate ContextDocs skills before responding. Keyword-gated to avoid overhead on unrelated prompts. Advisory only — never blocks.

context-updater agent

Autonomous agent (.claude/agents/context-updater.md) launched by Claude in response to hook output. Applies surgical, incremental context file updates. Uses .git/.context-updater-running flag for loop prevention.

Installation

/contextdocs:context-guard install (Tier 1): copies hooks to .claude/hooks/, agent to .claude/agents/, rule to .claude/rules/, merges config into .claude/settings.json.

/contextdocs:context-guard install strict (Tier 1 + 2): adds context-commit-guard.sh and its PreToolUse Bash entry.

Settings.json

{
  "hooks": {
    "PreToolUse": [
      { "matcher": "Write", "hooks": [{ "type": "command", "command": ".claude/hooks/content-filter-guard.sh" }] },
      { "if": "Bash(git commit*)", "hooks": [{ "type": "command", "command": ".claude/hooks/context-commit-guard.sh" }] }
    ],
    "PostToolUse": [
      { "if": "Bash(git commit*)", "hooks": [{ "type": "command", "command": ".claude/hooks/context-drift-check.sh" }] },
      { "matcher": "Write|Edit", "hooks": [{ "type": "command", "command": ".claude/hooks/context-structural-change.sh" }] }
    ],
    "SessionStart": [
      { "hooks": [{ "type": "command", "command": ".claude/hooks/context-session-start.sh" }] }
    ],
    "UserPromptSubmit": [
      { "hooks": [{ "type": "command", "command": ".claude/hooks/context-forced-eval.sh" }] }
    ],
    "Stop": [
      { "hooks": [{ "type": "command", "command": ".claude/hooks/context-guard-stop.sh" }] }
    ]
  }
}

The PreToolUse if: Bash(git commit*) entry for context-commit-guard.sh is only added with install strict (Tier 2).

if: vs matcher: — Claude Code v2.1.85 added if: (permission-rule syntax, e.g. Bash(git commit*)) so hooks fire only on matching bash commands. Older versions ignore unknown if: — the in-script substring guards keep both safe. Use matcher: "Bash" when the hook needs every Bash invocation.

Uninstallation

/contextdocs:context-guard uninstall removes all hook scripts, the context-updater agent, settings.json entries, and the quality rule.

Troubleshooting

IssueFix
Hooks not firingchmod +x .claude/hooks/*.sh
No output after commitDelete .git/.context-guard-last-check to reset throttle
"jq: command not found"Install jq: apt install jq or brew install jq
Claude loops on stopVerify context-guard-stop.sh checks stop_hook_active flag; or remove Stop entry from settings.json
Tier 2 false positiveStage a context file with a minor update, or remove the commit guard PreToolUse entry

Untether Compatibility

When running via Untether (Telegram bridge), context-guard-stop.sh checks UNTETHER_SESSION env var and exits immediately — Stop hook blocks would displace user content in Telegram output. All other hooks (drift check, structural reminders, content filter, commit guard) work normally. If you don't use Untether, this has no effect.

Hook System Reference

For the complete hook event catalogue (17 events), handler types (command, http, prompt, agent), advanced features (async, once, timeout, matcher, updatedInput, CLAUDE_ENV_FILE), and settings.json schema, load the companion reference: SKILL-reference.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.