Cursor Agent repeatedly generates Python code that violates the same explicit, deterministic repository hard rules

Hey, answering your two questions from the last post.

About hook feedback back to the agent. The key thing is the difference between hooks. What actually blocks an edit before the agent starts building dependent changes on top of it is preToolUse on the Write tool, not afterFileEdit. preToolUse gets the proposed edit content tool_input, and if you return permission: "deny" together with an agent_message, that message goes straight to the same agent and the action is blocked. So the denial becomes feedback for the agent, not a silent hook error, and the agent has to adjust before continuing.

afterFileEdit runs after the file is written. It can also block exit code 2 = deny and return a message to the agent, but at that point the edit is already on disk. So if the goal is to stop the agent from continuing with dependent edits on top of an invalid change, put your Ruff or MODULE_SIZE audit in preToolUse on Write. Schema and output fields details: Hooks | Cursor Docs

About >95% recurrence, expected or a quality issue. That rate is not desired behavior. Rules are injected as strong guidance, and compliance is probabilistic, but first-pass compliance with clear deterministic constraints is what we track and improve. Your example with specific rule classes, request ID, and recurrence numbers was shared with the team as a well documented case. I can’t give an ETA, but if there is an update, I’ll post it here.

Also, if you notice Chat context summarized flashing during repair cycles, let me know with details like roughly how long the session was. That’s a separate thing worth tracking.

And the advice above from @AymaneWebDEV is on point and complements what I wrote. Scoping globs to specific paths like **/services/**/*.py instead of **/* makes the rule re-inject exactly when working with those files, and a short BAD or GOOD snippet in .mdc is picked up by the agent much better than abstract prose. Worth trying together with Hooks.

Waiting for your Auto vs pinned reasoning model comparison. Curious to see the difference in repair cycles and total usage.