# Keep your coding agent from undoing your fixes

Save the requirement as a rule. Check later edits for the bug you already fixed.

Source: https://perchscan.com/examples/plain-english-rules/
Published: 2026-09-20

![Perch flags a repeated-refund bug at 80% confidence. Adding a status guard makes the next check pass.](/demos/refund-rule.gif)

The demo uses a bug that refunds the same order twice. Perch flags the unguarded function, and the check passes once the guard is added. The requirement stays in `perch.yaml`, so you can check a later agent edit against the same rule. [Full command output](/demos/refund-capture.json).

## Add the requirement

Once you have [set up Perch](https://docs.perchscan.com/install/), save this as `refund.py` in a Git repository:

```python
def refund(order, payment):
    payment.refund(order.total)
    order.status = "refunded"
    return order
```

Add the behavior you need as a rule:

```console
$ perch rules add refund-once --where refund.py --each method --ensure "Refunded orders cannot be refunded again."
Added refund-once.
```

That saves the rule in `perch.yaml`. Now check the function against it:

```console
$ perch check refund.py::refund --rules refund-once
refund.py:1  refund
1 check, 1 broken.

  Confidence  Rule         Description
         80%  refund-once  Refunded orders cannot be refunded again.
```

This output is from the September 20 demo. The check exited with `3`, which means Perch found something.

## Add the guard

Return early, before calling the payment provider, if the order has already been refunded:

```python
def refund(order, payment):
    if order.status == "refunded":
        return order
    payment.refund(order.total)
    order.status = "refunded"
    return order
```

Run the same command again on the edited file:

```console
$ perch check refund.py::refund --rules refund-once
refund.py:1  refund
1 check, nothing to report.
```

This time it exited with `0`. `perch check` reads the file on disk, so you do not need to commit the fix first. Each check asks the model again, so your scores may differ.

## Check the payment behavior

Add a regression test that refunds the same order twice and checks that the payment provider was called only once. Also test a failed refund at the provider: the order must not end up marked as refunded.

The guard only covers calls made one after another. Two requests at the same time, or a crash after the payment succeeds, need idempotency at the provider and durable state; test those at the payment boundary.

## Run the rule in CI

Commit the rule alongside the code. With `origin/main` fetched, scan the changes on your branch:

```sh
perch scan --since origin/main --filter type=lint
```

Have your coding agent run the single-function check after its edits too. [Agent setup](https://docs.perchscan.com/skill/) adds Perch instructions to supported coding assistants, and [the same rules can fail AI slop pull requests in CI](/blog/stop-ai-slop-prs-before-code-review/).
