# Review an agent's changes as a pull request

> Let an agent write on its own branch, preview what merging it would change with MERGE DATABASE … DRY RUN and DRY RUN ROWS, settle conflicts with RESOLVE OURS or THEIRS, and merge only what was reviewed with REVIEWED.

Source: https://stolondb.com/guides/review-agent-changes/ · Page title: Review an agent's database changes as a pull request — Stolon

Give the agent a branch, preview the merge, read it row by row, then land exactly what you reviewed.

## 1. Give the agent its own branch

```sh
psql "postgresql://app:<password>@db.stolondb.com/acme.main" -c 'CREATE DATABASE "agent-7"'
```

The agent writes on `agent-7` while `main` keeps taking writes:

```sh
psql "postgresql://app:<password>@db.stolondb.com/acme.agent-7" \
  -c "UPDATE accounts SET balance = 175.00 WHERE id = 1" \
  -c "DELETE FROM accounts WHERE id = 3" \
  -c "INSERT INTO accounts VALUES (5, 'noor', 42.00)"
psql "postgresql://app:<password>@db.stolondb.com/acme.main" -c "UPDATE accounts SET balance = 130.00 WHERE id = 1"
```

## 2. Preview the merge

```sh
psql "postgresql://app:<password>@db.stolondb.com/acme.main" -c 'MERGE DATABASE "agent-7" INTO main DRY RUN'
```

```text
       kind       |                              detail
------------------+------------------------------------------------------------------
 conflict         | accounts[1].balance: changed here and on agent-7
 review           | b7a889ecaec843516fb0a1d811e19813bf68076e60c6efbd8a4efcf3eae507f8
 rows to apply    | 1
 deletes to apply | 1
(4 rows)
```

## 3. Read it row by row

```sh
psql "postgresql://app:<password>@db.stolondb.com/acme.main" -c 'MERGE DATABASE "agent-7" INTO main DRY RUN ROWS 10'
```

```text
 row              | {"after":null,"before":[3,"sam","75.50",null],"change":"removed","changed":[],"key":"3","table":"accounts"}
 row              | {"after":[5,"noor","42.00",null],"before":null,"change":"added","changed":[],"key":"5","table":"accounts"}
```

The console shows the same pull request, with both sides of each conflict.

## 4. Settle conflicts

```sh
psql "postgresql://app:<password>@db.stolondb.com/acme.main" -c 'MERGE DATABASE "agent-7" INTO main'
```

```text
ERROR:  merge of "agent-7" has 1 conflict(s), nothing applied: accounts[1].balance: changed here and on agent-7
```

Choose a side with `RESOLVE OURS` (keep `main`'s) or `RESOLVE THEIRS` (take the branch's).

## 5. Merge exactly what you reviewed

Pass the preview's digest; if either branch moved since, nothing lands.

```sh
MAIN="postgresql://app:<password>@db.stolondb.com/acme.main"
DIGEST=$(psql "$MAIN" -tA -c 'MERGE DATABASE "agent-7" INTO main RESOLVE THEIRS DRY RUN' | awk -F'|' '$1=="review"{print $2}')
psql "$MAIN" -c "MERGE DATABASE \"agent-7\" INTO main RESOLVE THEIRS REVIEWED '$DIGEST'"
```

```text
       action        | count
---------------------+-------
 rows applied        |     2
 deletes applied     |     1
 tables created      |     0
 tables dropped      |     0
 conflicts resolved  |     1
 keys renumbered     |     0
 tables full-scanned |     0
(7 rows)
```

Then `DROP DATABASE "agent-7"`. More in [the docs](https://stolondb.com/docs/).
