# An agent checking its own change

> A coding agent records the program before and after its change and reads ormaos diff to see what the change did in the execution.

A coding agent that changes code can see whether the tests pass and what the program printed. With Ormaos it can also see what the change did in the execution: which calls now run, which no longer do, which exceptions, I/O and log records appeared or went away. The example is [agent-change](/examples/agent-change.md): a billing tool in a git repository, and a change that an agent made to it (`change.patch`).

## Before the change

```bash
ormaos run -- python run.py
```

```console
$ ormaos run -- python run.py
12 invoices, 3 refunds written
ormaos: r1 exit 0 in …: 1 Python process, 75 calls, 0 exceptions, 6 io
ormaos: not collected: io: writes to the 2 files opened at run.py:18: opened for writing, and no `write` call by Python code was seen on a file of that name: if it was written, that was inside C code (`print(..., file=f)`, `csv.writer`)
ormaos: saved in .ormaos/r1, next: ormaos tree r1:c1
```

## The change

```bash
git apply change.patch
git diff --stat
```

```console
$ git diff --stat
 billing.py | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
```

## After the change, the same command

The program prints the same line as before; the summary has more calls and more I/O:

```console
$ ormaos run -- python run.py
12 invoices, 3 refunds written
ormaos: r2 exit 0 in …: 1 Python process, 113 calls, 0 exceptions, 82 io
ormaos: not collected: io: writes to the 2 files opened at run.py:18: opened for writing, and no `write` call by Python code was seen on a file of that name: if it was written, that was inside C code (`print(..., file=f)`, `csv.writer`)
ormaos: saved in .ormaos/r2, next: ormaos tree r2:c1
```

## What does the execution do differently?

```console
$ ormaos diff r1 r2
r1 -> r2, matched by place in the code and order, not by address
  r1: exit 0, 75 calls, 0 exceptions, 6 io events
  r2: exit 0, 113 calls, 0 exceptions, 82 io events
code: the two code versions differ: the same git commit 5b23406de1, and the uncommitted changes are not the same (none in r1, 1 file changed in r2)
calls: 1 of 11 places differ
  discount_percent at billing.py:10: none in r1, 38 in r2  [r2:c9]
exceptions: the same in both (0 groups)
io: 2 of 8 groups differ
  file open tiers.json (read)  at billing.py:11 in discount_percent: none in r1, 38 in r2  [r2:i5]
  file read tiers.json  at billing.py:12 in discount_percent: none in r1, 38 in r2  [r2:i6]
logs: the same in both (0 groups)
data: the same in both (0 places)
not compared: values, shapes of arguments and results, durations, cpu and memory, the lines of `path`, which process or thread an event was in
next: ormaos tree r2:c9 | ormaos io r2:i5 | ormaos io r2:i6 | ormaos show r1 | ormaos show r2
```

- `code:` the two recordings ran different versions of the code: the same commit, and different uncommitted changes.
- `calls:` `discount_percent` did not run in `r1` and ran 38 times in `r2`.
- `io:` each of those calls opened and read `tiers.json`: 38 opens and 38 reads in `r2`.

Whether reading the file 38 times is what the change should do is for the agent, or the developer, to decide. The diff gives the places and the counts.

## Did the new function run at all? How often?

```console
$ ormaos tree r2 --function discount_percent --limit 3
r2: 38 of the 113 calls match --function discount_percent (38 discount_percent; 0 raised)
r2:c9 discount_percent(customer: str[8]) -> int  billing.py:10  …  io 2
r2:c11 discount_percent(customer: str[8]) -> int  billing.py:10  …  io 2
r2:c15 discount_percent(customer: str[5]) -> int  billing.py:10  …  io 2
... 35 more: --offset 3
next: ormaos tree r2:c9 | ormaos tree r2:c11 | ormaos tree r2 --offset 3 --function discount_percent | ormaos tree r2 | ormaos exceptions r2
```

```console
$ ormaos tree r2:c9
inside: r2:c1 <module> > r2:c3 main > r2:c6 invoice_total > r2:c7 invoice_total.<locals>.<genexpr> > r2:c8 line_price
r2:c9 discount_percent(customer: str[8]) -> int  billing.py:10  …  io 2
next: ormaos tree r2:c8 | ormaos exceptions r2:c9
```

```console
$ ormaos io r2:i5
r2:i5 file open tiers.json (read)
  caused at   billing.py:11 in discount_percent  [r2:c9]
  inside      r2:c1 <module> > r2:c3 main > r2:c6 invoice_total > r2:c7 invoice_total.<locals>.<genexpr> > r2:c8 line_price
next: ormaos io r2:c9 | ormaos tree r2:c9 | ormaos io r2
```

## Does the new code catch exceptions that are never shown?

```console
$ ormaos exceptions r1
r1: 0 exceptions (0 swallowed, 0 uncaught)
next: ormaos tree r1
```

None in `r1`, and the diff says the exceptions are the same in both.

## The loop

1. `ormaos guide` once, to learn the installed version.
2. `ormaos run -- <command>` before the change.
3. Change the code.
4. `ormaos run -- <command>` again, same command, same input.
5. `ormaos diff r1 r2`, then follow `next:` for each difference.

See [The verification loop](/python/agents.md).

---

Source: https://docs.ormaos.com/python/guides/agent-change/ · ormaos 0.0.1 · ormaos-python 968f69776c · ormaos-examples 6e27be5922
