[FEATURE] Path-scoped rules should load when Write/Edit targets a matching path
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
Path-scoped rules in .claude/rules/ only load when Claude reads a file matching the paths: glob. They do not load when Claude creates a new file at a matching path via the Write tool.
This means scoped rules are blind to file creation — the exact moment when conventions matter most, because there's no existing code to learn patterns from.
Why this matters (not a nice-to-have)
We maintain a codebase entirely with AI coding agents. No human reads the code. We use scoped rules to keep our instruction files small (research shows compliance drops sharply past ~150 instructions — every rule loaded is a cost).
Example: we have performance rules scoped to src/hooks/**/*.ts:
---
paths:
- "src/hooks/**/*.ts"
---
# Performance Rules
- No React setState in mousemove/touchmove — use refs only
- Settings in hot paths: settingsRef.current, never React props
When Claude edits an existing hook file, these rules load (via the pre-edit Read). Good.
When Claude creates a new hook file, these rules never load. The agent writes the file without knowing the performance constraints exist. The resulting code violates conventions that are invisible to it.
This is the highest-risk moment — new files have no existing patterns to follow, and the rules designed to guide creation aren't present.
Reproduction
- Create
.claude/rules/test-probe.md:
---
paths:
- "src/test-guardrail-*.ts"
---
# Test Rule
Add this comment as the first line: `// SCOPED_RULE_LOADED`
- Ask Claude (or a subagent) to create
src/test-guardrail-probe.tswith a simple function. Do not read any matching files first.
- Observe: the file is created without the marker comment. The scoped rule was not loaded.
How this differs from prior issues
- #23478 (closed NOT_PLANNED) and #27861 / #36334 (closed as duplicates) requested separating read vs write triggers — a new
trigger: read | writefrontmatter field. That's a design complexity question.
- This request is simpler: don't add new frontmatter. Just expand the existing path-matching to also check the target path of Write and Edit tool calls, not only Read. When Claude is about to Write to
src/hooks/use-foo.ts, check if any scoped rule matches that path and load it before the write executes.
This is purely additive — it doesn't change behavior for rules that already load on Read. It just closes the gap where creation is unguarded.
Proposed Implementation
In the PreToolUse phase for Write and Edit tools, check the target file_path parameter against all paths: globs in .claude/rules/. If a match is found and that rule isn't already loaded, inject it into context before the tool executes.
This is analogous to how PreToolUse hooks already receive tool parameters and can inject additionalContext — the rule engine would do the same thing for scoped rules.
Priority
Medium - Important for my workflow
Feature Category
File operations
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗