Prerequisites
Start with:
- a clean committed baseline;
- a branch or isolated worktree;
- one observable outcome;
- a known test command; and
- no unrelated user changes in the target files.
Git's worktree documentation describes multiple working trees attached to one repository. A branch alone is also sufficient if it protects the baseline and the operator understands where uncommitted work lives.
Stage 1: write the task contract
Give the agent:
Outcome:
Allowed paths:
Forbidden boundaries:
Acceptance command:
Diff budget:
Stop and report when:
The diff budget can name files and change classes rather than line count. For example: one model file, one test file, no dependencies, no schema changes, no authentication changes.
Stage 2: checkpoint before generation
Record the baseline commit and confirm the worktree is clean. Never use a destructive cleanup command when the directory may contain another person's changes.
Create the agent attempt on a disposable branch or worktree. This makes discard a branch decision instead of a manual attempt to recognize generated edits.
Stage 3: inspect before correcting
Use Git's diff tools to inspect changed paths and content. Stop immediately if the patch crosses a forbidden boundary, rewrites unrelated code, adds unexplained dependencies, or cannot state how its behavior will be tested.
Do not first ask the same context to “clean it up.” Preserve the failure as evidence, note the mistaken assumption, and decide whether a narrower fresh attempt is cheaper to review.
Stage 4: run the named gate
Run formatting or static checks, the narrow behavior test, then the relevant broader suite. Record exact commands, environment, and result.
A passing generated test is weak evidence if the agent changed both production logic and the assertion to encode the same misunderstanding. Review the expected behavior independently.
Stage 5: accept or discard
Accept the attempt only when:
- it stays inside the declared boundary;
- the human reviewer understands the diff;
- the independent acceptance behavior passes;
- existing relevant behavior still passes; and
- recovery is clear.
Discard when the approach violates an assumption, expands beyond the budget, or requires layers of corrective prompts that make provenance unclear. Start the next attempt from the recorded baseline with a revised task contract.
Stage 6: preserve learning, not contamination
Record:
- failed assumption;
- observed failure signal;
- constraint added to the next task;
- accepted commit, if any; and
- untested environment-specific behavior.
Code rollback and context reset are separate. Reverting files while continuing with a conversation built around a false architecture may reproduce the same mistake. A fresh task should carry the verified constraint, not the entire failed narrative.
This loop does not prove an agent is reliable. It limits the cost of discovering that one proposed change is not.