Agent skill
directory-structure-enforcement-hooks
Use when Claude Code creates files in wrong locations despite CLAUDE.md rules. PreToolUse + Stop hook 3-layer defense.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/directory-structure-enforcement-hooks
Metadata
Additional technical details for this skill
- author
- shimo4228
- version
- 1.0
- extracted
- 2026-02-13
SKILL.md
Directory Structure Enforcement via Hooks
Extracted: 2026-02-13 Context: Claude Code hooks を使ってディレクトリ構造ルールを自動強制する
Problem
Claude が docs/ に間違った場所・命名でファイルを作るドリフトを防止したい。 CLAUDE.md にルールを書いても守られないことがある。
Solution
3層防御:
-
PreToolUse hook (
docs-prewrite.sh): Write/Edit 時にリアルタイムでバリデーション- exit 0 = 許可、exit 2 = ブロック、stderr = 警告
- stdin から JSON を読み、
tool_input.file_pathを検査
-
Stop hook (
driftcheck.sh): セッション終了時に全体スキャン- mv や外部プロセスによる変更も捕捉
- 常に exit 0(セッション終了をブロックしない)
-
hooks.json matcher 除外: 既存の汎用ブロッカーと競合しないよう除外条件を追加
Key Design Decisions
- ブロック (exit 2) vs 警告 (stderr + exit 0) を違反の深刻度で使い分ける
- 許可リストはスクリプト内にハードコード(設定ファイルを増やさない)
- driftcheck は常に exit 0 でセッション終了を妨げない
When to Use
- プロジェクトにディレクトリ構造ルールがあるとき
- Claude が勝手にファイルを作る問題が繰り返されるとき
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?