Guide · AI coding guardrails
How to stop AI coding agents from breaking working code
AI coding agents like Claude Code, Cursor, Codex and GitHub Copilot ship features fast. The cost is regressions: a login flow, an export or an API field that worked yesterday quietly breaks while the agent was doing something else.
Why it happens
- No boundary. The agent doesn't know which files the task should touch, so a "small fix" spreads into shared utilities and unrelated modules.
- No memory of what works. Nothing tells the agent which behaviours are load-bearing. It sees code, not the customers depending on it.
- Green at any cost. When a test fails, the shortest path to "done" is sometimes editing the test, not the code.
- Rules it can ignore. AGENTS.md, CLAUDE.md and Cursor rules are instructions, not checks. Nothing verifies the agent followed them.
- Lost context. New chat, new session: past decisions get reopened and old bugs come back.
Five guardrails that work
1. Scope every task
Before the agent starts, write down the task and the files it may change, for example src/theme/** for a dark-mode task. After it finishes, list every changed file and flag anything outside that list. This catches scope creep, the most common source of surprise regressions.
2. Lock what already works
For each behaviour that must not break, record one command that proves it: a focused test, or a snapshot of an output such as an export or a route list. Run all of them after every AI change. Small and specific beats "the whole suite passed".
3. Never let the agent weaken a check
Make the rule explicit: if a check fails, fix the change, not the check. Flag edits to test files or approved snapshots during the task so a human decides.
4. One task, one commit
Commit each finished task before starting the next. Small diffs are easy to review and easy to revert, and the next task starts from a known-good state.
5. Enforce it where the agent can't reach
Run the scope check and the locked behaviours in a git pre-commit hook and in CI, so a skipped step is still caught.
Doing it in one tool: Stet
Stet is a free, open-source CLI and MCP server that automates these five guardrails for any AI coding tool.
npx stet-cli init # locks your test suite as L1
stet lock add "Login works" --cmd "npm test -- auth" --protect "src/auth/**"
stet scope set "Add dark mode" --allow "src/theme/**"
# ...the agent works...
stet guard # files changed outside the task?
stet check # do locked behaviours still pass?
stet scope done --commit
Claude Code and Cursor connect over MCP; Codex, Copilot, Windsurf and Gemini CLI read the AGENTS.md Stet keeps current; for ChatGPT or Claude.ai, stet context builds a paste-ready context pack. It runs locally with no account and no telemetry.
FAQ
Are tests enough?
Tests are the foundation, but agents can edit them, they don't encode which files a task may touch, and most codebases have gaps. Add an explicit scope and a "never weaken a check" rule.
Does this slow the agent down?
Setting a scope takes one line. Guard is instant; check costs as long as your chosen tests take, which is why focused locks beat the full suite.
Does it work with ChatGPT and other chats?
Yes. Paste the context pack, apply the AI's changes, then run stet guard and stet check as usual.
Try Stet in your repo
npx stet-cli init · Setup guide · GitHub