Sub topic 12
Turning Repeated Corrections into Project Instructions
This chapter assumes you can use Git branches and diffs, run your project's tests and linter from the command line, and have already given a coding agent a corrective message at least once. It covers what to do when you notice you are giving the same correction in run after run.
Why one-off corrections do not last
A corrective message, like the one in the previous chapter, steers a single run. The agent does not carry it into the next run. A new session starts without memory of your last review, so the same defect can come back.
Suppose you review three agent runs in a week. In each one, the agent adds # type: ignore to silence a type error, and each time you ask it to fix the error properly. Each correction cost you a review round. The fix is to write the rule down once, where every future run will read it.
Most coding agents load a project instruction file at the start of a session. The name depends on the tool. Common examples are AGENTS.md, CLAUDE.md, a rules directory, or a steering file. Check your tool's documentation for the exact name and location. The idea is the same everywhere: a plain text file in the repository that is added to the agent's context before it reads your task.
Deciding what belongs in the file
Not every correction deserves a rule. Use a simple test before you write one.
- Did you give this correction at least twice, or is it likely to recur?
- Is the rule specific to this project, rather than something any competent developer or the agent already knows?
- Can you state it so that a reviewer could check it in a diff?
If the answer to all three is yes, write the rule. If the correction was about one particular task, keep it in the task message.
Good candidates come from the failure modes you have already learned to look for:
- Commands the agent should run to verify its work, with the exact flags.
- Boundaries on scope, such as directories it must not touch.
- Protection for tests and checks, such as a ban on skipping tests to make them pass.
- Local conventions that are not visible from one file, such as where new modules go.
- Facts the agent tends to invent, such as the real name of an internal helper.
Poor candidates are general advice ("write clean code"), rules that the linter already enforces, and long style guides. The agent cannot act on vague advice, and a tool that enforces a rule automatically is more reliable than a sentence asking for it.
Writing rules the agent can follow
A rule works when it names a concrete behavior and, where possible, the reason. Compare these.
| Weak rule | Stronger rule |
|---|---|
| Be careful with tests. | Never add `skip`, `xfail`, or delete a test to make the suite pass. If a test seems wrong, stop and explain why. |
| Keep changes small. | Edit only files needed for the task. If another file needs a change, list it in your report and ask first. |
| Run checks before you finish. | Before reporting done, run `pytest -q`, `ruff check .`, and `mypy src`. Report each command and its result. |
| Don't make up APIs. | Before calling a function from this repository, open its definition and confirm the signature. |
Each strong rule gives the agent an action it can take and gives you something to check afterward. The reason matters because it helps the agent apply the rule to cases you did not foresee. "Do not use # type: ignore, because it hides real type errors from our CI check" generalizes better than the bare prohibition.
Here is a short example file for a Python project. It is about a dozen lines, which is the right order of magnitude.
# Project instructions
## Verify before reporting done
- Run `pytest -q`, `ruff check .`, and `mypy src`. Report each command and its exact result.
- If a command fails, say so. Do not report success without running it.
## Scope
- Change only files required by the task. Do not edit `src/billing/` unless the task names it.
- If you find a problem outside the task, mention it in your report instead of fixing it.
## Tests and checks
- Never skip, delete, or loosen a test to make it pass. Fix the code, or stop and explain.
- Do not add `# noqa` or `# type: ignore` without a comment giving the reason.
## Project facts
- Configuration loading lives in `src/config/loader.py`. New settings go in `Settings`, not in module-level globals.Keeping the file short and current
The instruction file competes with your task and the code for the agent's attention. A long file dilutes every rule in it. Keep it short enough that you can read it in a minute.
Instruction files also go stale. A rule that names a directory you have since renamed will mislead the agent. Treat the file like code:
- Store it in the repository so changes appear in diffs and pull requests.
- Review changes to it as carefully as changes to source code.
- Remove rules that no longer fire. If a rule has not mattered in months, it is probably noise.
- When a rule conflicts with the task, the agent has to guess which wins. Resolve conflicts in the file instead of leaving them.
An agent can edit this file too. Include it in your diff triage. A run that quietly rewrites AGENTS.md to permit what it just did is a form of weakened checks, and you should treat it the same way as a skipped test.
Checking that a rule works
Writing a rule does not prove the agent follows it. Test it the way you test code.
- Write the rule.
- Re-run a task that previously triggered the problem, on a clean branch.
- Check the diff for the old defect, using the same search you used before.
- If the defect returns, make the rule more specific or move enforcement elsewhere.
The search from the test integrity chapter is a useful check:
git diff -U0 | grep -nE '^\+.*(@pytest\.mark\.(skip|xfail)|# noqa|# type: ignore)'If a rule keeps failing, ask whether a sentence is the right tool. Some rules are better enforced mechanically:
- A pre-commit hook or CI job that fails on
# type: ignorewithout a reason. - A path restriction in your tool's permission settings that blocks edits to
src/billing/. - A required test command in CI, so a missing run shows up as a failed check.
Instructions ask the agent to behave. Automated checks make misbehavior visible whether or not the agent complied. Use instructions for guidance and automation for anything that must never happen.
Common mistakes
- Rules written in anger. A rule like "NEVER touch anything outside the file" may cause the agent to refuse necessary edits. State the boundary and the exit: "ask before editing other files."
- Duplicating the linter. If
ruffalready enforces import order, a rule about it adds length and no value. - Trusting the file as a guarantee. The agent reads the file, but it can still ignore or misapply a rule. Keep verifying with your own checks.
- Recording a one-time fix. "The loader was renamed on Monday" is history, not guidance. It belongs in a commit message.
- Letting the file grow without review. Add a quick pass over the file to your regular review loop.
Applying it to the earlier example
In the rename of the config loader, you sent a message that asked the agent to revert src/billing/invoice.py and restore a skipped test. If that happens again on the next task, convert the two problems into rules:
- A scope rule: "Do not edit files outside the paths named in the task. Report other needed changes instead."
- A test rule: "Never skip or remove a test to make it pass."
Commit those lines to AGENTS.md on their own commit, separate from any agent work, so the history shows when and why each rule was added:
git add AGENTS.md
git commit -m "Add scope and test-integrity rules for coding agent"Then run the next task from a clean baseline, and check whether the corrections were still needed.
Exercise
Over two weeks, you give the agent the same correction three times: "Run pytest -q and report the result before saying you are done." The agent also sometimes ignores it and reports success without running tests. Which two actions address this, and why is the second needed?
Show answer
First, add a rule to the project instruction file that names the exact command and requires the agent to report the command and its result, so the correction applies to every future run. Second, add an automated check, such as a required CI test job, because the agent can still skip an instruction. The automated check makes a missing test run visible regardless of whether the agent complied.
Exercise
A teammate proposes adding "Write high-quality, well-structured code" to the instruction file. Using the three-question test from this chapter, explain whether it belongs there and how you would improve it.
Show answer
It fails the tests. It is general advice, not specific to this project, and a reviewer cannot check it in a diff. Replace it with concrete, checkable rules drawn from real corrections, such as "New settings go in Settings, not module-level globals" or "Run ruff check . and fix all findings without adding # noqa."