self-maintaining-skill-enforcement-triad

Self-maintaining skill enforcement triad pattern

Making a skill truly self-enforcing (so the model cannot drift away from it over sessions) requires three cooperating mechanisms at different lifecycle points. Implementing only one leaves gaps.

The triad

LayerTypeFires onRole
1. Auto-loaderHookify UserPromptSubmitKeyword detection in user promptsForces model to invoke the governing skill BEFORE acting — routing, format contracts, anti-bloat filters loaded
2. Write-guardNative PreToolUse on Write|EditAny edit to a governed fileInjects reminder if skill wasn’t invoked — advisory, not blocking, so trivial edits proceed
3. Nightly sweepPython cronScheduled (e.g., 03:00 IST daily)Catches drift between sessions. Logs [DRIFT] entries to session-learnings for next session to pick up on startup

Plus a PostToolUse marker hook that watches for Skill invocations and writes a per-session marker to /tmp/.{skill}-invoked-<session_id>. The guard checks the marker to know whether to warn.

Why all three are needed

  • Layer 1 alone: Model still forgets if the user prompt doesn’t contain trigger keywords
  • Layer 2 alone: Only catches edits, not additions from memory or vault
  • Layer 3 alone: Drift accumulates up to 24h before detection

Self-verification

The sweep script should check its OWN dependencies first (check_enforcement_integrity()) — verify the skill file, all hook scripts, the cron script, hookify rules, AND that the hooks are still registered in settings.json. If any artifact is missing or the hooks got unregistered, the enforcement is silently broken and the sweep must flag it.

Real example

setup-curator skill at ~/.claude/skills/setup-curator/SKILL.md governs all Claude Code setup/memory files. Implemented with:

  • ~/.claude/projects/-root-*/\.claude/hookify.setup-curator-autoloader.local.md (x2 workspaces)
  • /root/.claude/hooks/setup-curator-guard.py (PreToolUse Write|Edit)
  • /root/.claude/hooks/setup-curator-marker.py (PostToolUse Skill)
  • /root/.claude/crons/setup-pristine-sweep.py (nightly 03:00 IST)
  • settings.json registrations for both Python hooks

Applies to

Any skill that governs a set of files (style guides, format contracts, API conventions). The triad turns a “strong recommendation” into an “enforced discipline” without LLM cost at enforcement time — hooks and cron are deterministic Python.