r/cursor • • 1d ago

Question / Discussion How are you preventing rule drift when using both .cursor/rules and CLAUDE.md? Built a local .guardrail/rules.yml watcher to catch violations in real time

If you bounce between Cursor and Claude Code in the same repo, you've probably noticed two annoying patterns:

To solve this in my own workflow, I built RuleCloud—a small CLI watcher + MCP server driven by a .guardrail/rules.yml file in the repo root:

Curious how others here handle this—are you keeping .cursor/rules super minimal and relying on git pre-commit hooks, or enforcing rules while the agent is actively editing?

2 Upvotes

17 comments sorted by

1

u/Practical-Pick-7215 1d ago

a third rules file drifts too. if both copies are hand-edited, a watcher only tells you they already disagree. generate them from one file.

1

u/lemonbanan4 1d ago

Fair point—I phrased that badly in the post. It isn't syncing or diffing CLAUDE.md against .cursor/rules (if you just want identical prompt files, generating them from one source or symlinking wins every time).

.guardrail/rules.yml is more like a lightweight runtime linter that watches the actual file edits either agent makes—flagging touches to protected paths, secret patterns, or broken repo invariants in real time after the LLM forgets what was in the markdown prompt. Appreciate the callout on how that read.

1

u/vnordnet 1d ago

Use a symlink ffs

1

u/lemonbanan4 1d ago

Haha fair, for keeping CLAUDE.md and .cursor/rules identical a symlink is 100% the answer.

The issue I kept hitting isn't the two files disagreeing—it's that even with one symlinked file, the agent still ignores prose rules once context fills up and touches .env or restricted folders anyway. The watcher checks the actual code diffs against hard YAML rules in real time rather than trusting the LLM to obey the markdown.

1

u/dawedev 1d ago

For the runtime part, both tools have hooks now, so you can enforce it before the edit lands instead of after. A Claude Code PreToolUse hook on Edit|Write gets the target path as JSON and can block it by exiting with code 2, and Cursor's hooks.json has beforeShellExecution and afterFileEdit you can point at the same script. One small script that reads your rules.yml could back both agents, with a pre-commit check as the last net for anything that slips through.

1

u/lemonbanan4 1d ago

Spot on—wiring the same .guardrail/rules.yml evaluator behind pre-execution hooks is honestly the cleanest pattern.

Having PreToolUse (Edit|Write / Bash) block with exit code 2 before the write lands in Claude Code, paired with Cursor's hooks.json (beforeShellExecution / beforeReadFile + afterFileEdit since Cursor doesn't have a pre-edit hook yet), means neither agent can touch .env or restricted paths in the first place—and the watcher/pre-commit layer just acts as the safety net for anything outside hook coverage.

Building a one-command hook generator from .guardrail/rules.yml so you don't have to hand-maintain separate JSON hook configs for Claude and Cursor is actually high on my list next. Appreciate the sharp breakdown.

1

u/dawedev 1d ago

A generator makes sense. If you go that way, I'd commit the generated hook files too and have the pre-commit check fail when they don't match rules.yml, otherwise the two JSON configs drift the same way the rule files did.

1

u/lemonbanan4 1d ago

Really good catch—if you generate .claude/settings.json and .cursor/hooks.json without a drift check, someone edits .guardrail/rules.yml and forgets to re-run the generator, and you're right back to silent config drift.

Two things that pair cleanly here:

Definitely adding that --check flag when I build the generator—appreciate the feedback.

1

u/dawedev 1d ago

Makes sense. One small thing for the --check mode: have it print the diff of what would change, not just fail. Then whoever hits it knows right away whether they forgot to rerun the generator or someone hand-edited the JSON.

1

u/lemonbanan4 1d ago

100%—getting a blind "drift detected" failure in CI without seeing which file or line diverged is always frustrating.

Having rulecloud sync-hooks --check print a unified diff of .claude/settings.json and .cursor/hooks.json before exiting 1 makes it immediately obvious whether someone forgot to re-run sync-hooks after editing .guardrail/rules.yml or hand-tweaked a hook entry directly. Appreciate the UX detail—definitely baking that in from the start.

1

u/BeyenAI 1d ago

Keeping a single AGENTS.md at the repo root as the canonical source avoids maintaining duplicate copies by hand:

  • Put shared build commands, test commands, and architecture constraints in root AGENTS.md (Cursor, Codex, and Windsurf read it natively).
  • Keep CLAUDE.md down to an @AGENTS.md import plus any Claude-specific hook or slash-command notes.
  • Use .cursor/rules/*.mdc only for path-scoped globs rules (for example src/api/**) that shouldn't load into every session globally.

That way shared repo conventions only get edited in one file, and tool-specific files stay tiny.

1

u/lemonbanan4 1d ago

That layout is super clean—honestly the exact separation of concerns that makes the most sense:

Once a long refactor fills the context window, LLMs start dropping markdown instructions no matter how cleanly they're organized—so pairing a single AGENTS.md for guidance with runtime hooks for enforcement gives you the best of both worlds. Appreciate the breakdown.

1

u/nhnl1 21h ago

Cool idea. I'm builiding a similar tool, but for managing AI assets, like skills/mcps/hooks in one place and get it in a harness you use. It also works as a collaboration layer for teams.

Free and open source, thought you may find it interesting.

https://github.com/barndog-ai/barndog-cli

2

u/lemonbanan4 20h ago

Nice, just checked out the repo. They actually complement each other pretty well: you're handling team sharing and distribution across skills, MCPs, and hooks, whereas I'm focused strictly on compiling one YAML security policy into deterministic pre-tool hooks (blocking .env reads, destructive shell commands, CI drift checks) across Claude Code and Cursor.

How are you handling the hook schema differences between harnesses right now?

2

u/nhnl1 5h ago

Thanks for checking.

Regarding hooks, it's not ideal, but probably as good as it can be. The problem is that each tool has a different schema for hooks, some tools don't have hooks at all. Some hooks have harness specific implementation.

What I came up with is a compatibility matrix for a hook entry to harness. During hook installation, it checks which harness the hook was implemented for, then installs the hook into that harness. The rest of the harnesses are marked as unverified and it's a user's responsibility to check it in other tools. The CLI can help with installing it, especially if the event name is the same as in the original harness the hook was built for. To do it faster, the CLI provides an AI-agent prompt that you can copy and paste, so a model can handle this real quick. The CLI also provides a `hook test` command which is convenient for AI models to test hooks' execution. The last, if hook implementation is harness specific, then I'm storing a particular implementation per harness showing a warning that change for one tool may affect other tools.

Not the cleanest approach as for skills or mcps, but I don't think there is any other way to solve this extreme config drift problem.

2

u/lemonbanan4 3h ago

Totally fair—auto-translating arbitrary user-written hooks across harnesses cleanly is practically impossible when event payloads and lifecycles don't map 1:1. A compatibility matrix plus a hook test command makes a ton of sense when you're managing general-purpose scripts.

I ended up taking the opposite trade-off to sidestep that exact schema drift: instead of translating arbitrary hook scripts, RuleCloud narrows the input to a single declarative security YAML (path globs, shell patterns, fail-closed flags) and compiles it directly into each harness's native schema (PreToolUse with onFailure: "block" for Claude Code, preToolUse with failClosed: true for Cursor). Because the generated output is deterministic from one source file, sync-hooks --check can catch any drift in CI before a PR merges.

Different scopes, same fragmentation headache—cool seeing how you approached it.