Undo One AI Agent's Merge in MongoDB Without Losing Later Writes

An agent's change was approved and merged, then other writes landed. argon undo --merge reverts that one merge and skips documents changed since. A run on Argon 2.13.2, with its limits.

By Argon Labs8 min readUpdated
MongoDBAI AgentsUndo

Current capability notes: checkout materializes a physical database; native actor attribution is per branch/run; undo needs complete images and retained history. CLI sandboxes require watch and scheduled sweep. Read the capability matrix.

You reviewed an AI agent's change to a MongoDB database, approved it and merged it. A few hours later it turns out to be wrong. By then the database has moved on: the application took orders, and another agent's change was merged too. How do you take back the one bad merge without losing everything after it?

With Argon 2.13.0 or newer, argon undo --merge <plan-id> reverts the documents that one merge changed and nothing else. A document that was changed again after the merge is skipped and named, not overwritten.

This post runs that on the published release: a bad merge, later writes on top of it, the undo, and the one document it refused to touch. It also shows what the older way of undoing would have reverted, and where undo by plan does not apply.

What we ran

  • Argon 2.13.2, installed from npm as argonctl.
  • MongoDB 7.0.48 as a one-member replica set, in a container.
  • A database shop with 200 products, p-000 to p-199. Prices are in cents. It is imported into an Argon project whose main branch is checked out, so an application can write to it, with capture running.

Every command and its output is in the transcript. The report has the script that produced it and lists what the run does not cover. Below, $MAIN and $PRICING are connection strings Argon returned, and plan IDs are shortened with …

The merge that should not have been approved

A pricing agent gets a sandbox, a copy of main it can write to with any MongoDB driver. Its task: take 5.00 off five audio products, add a promo product, retire p-199. It sets the price to 500 where it should have subtracted 500.

$ mongosh "$PRICING" --eval 'db.products.updateMany({category: "audio", _id: {$lt: "p-020"}}, {$set: {price: 500}}).modifiedCount'
5

$ argon merge preview -p shop -b pricing-agent
Merging pricing-agent → main
  put    products/p-000
  put    products/p-004
  put    products/p-008
  put    products/p-012
  put    products/p-016
  delete products/p-199
  put    products/p-900
7 change(s), 0 conflict(s)

Plan 6b0fc9a6… saved (pending).

$ argon merge apply 6b0fc9a6…
Merged: 7 change(s) applied.

Seven changes to the right documents: the preview looks like the task. The mistake is in the values, and the reviewer missed it. After the merge, p-000, which cost 20.00, costs 5.00 on main.

The database keeps moving

Nobody notices yet. The application takes an order, which lowers the stock of two products. One of them, p-004, is a product the merge mispriced. Then an inventory agent restocks five other products in its own sandbox, and that merge is approved as well.

$ mongosh "$MAIN" --eval 'db.products.updateOne({_id: "p-004"}, {$inc: {stock: -1}}); db.products.updateOne({_id: "p-150"}, {$inc: {stock: -2}}); db.orders.insertOne({_id: "o-1001", …}).insertedId'
o-1001

$ argon merge list -p shop
PLAN          STATUS     SOURCE→TARGET     CHANGES  CONFLICTS CREATED
6b0fc9d3…     applied    inventory-agent→main        5          0 2026-12-02 07:33:39
6b0fc9a6…     applied    pricing-agent→main        7          0 2026-12-02 07:32:54

History on main now reads: the bad merge at LSN 211, the application's writes, then the second merge at LSN 248. An LSN is the position of a change in a branch's history.

Why undoing everything since the merge is the wrong tool

Before 2.13.0, argon undo --from-lsn was the way to undo a merge. It restores every document touched in a range of history to its state before the range. Asked to undo from the bad merge onwards, it plans this:

$ argon undo -p shop -b main --from-lsn 211 --dry-run
Undo [211, 253]: 14 document(s) to revert
  delete  orders/o-1001
  restore products/p-000
  restore products/p-004
  restore products/p-008
  restore products/p-012
  restore products/p-016
  restore products/p-100
  restore products/p-101
  restore products/p-102
  restore products/p-103
  restore products/p-104
  restore products/p-150
  restore products/p-199
  delete  products/p-900
Dry run: nothing applied.

Fourteen documents. Seven belong to the bad merge. The other seven are the customer's order, the stock it consumed and the whole of the inventory agent's merge. A restore from a backup or a point-in-time recovery to just before the merge has the same shape: it returns the data to a moment, and everything after that moment goes with it.

Undo the one merge

From 2.13.0 an applied plan records where its changes sit in history, and undo accepts the plan's ID:

$ argon undo --merge 6b0fc9a6… --dry-run
Undo [211, 253] for actor "merge plan 6b0fc9a6…": 6 document(s) to revert
  restore products/p-000
  restore products/p-008
  restore products/p-012
  restore products/p-016
  restore products/p-199
  delete  products/p-900
  CONFLICT products/p-004: modified by "ingest" at LSN 233 — skipped
Dry run: nothing applied.

$ argon undo --merge 6b0fc9a6…
…
Done: 5 restored, 1 deleted.

Four prices are restored, the retired product is back, and the promo product is gone. The order, the stock it consumed on p-150 and the inventory agent's five documents are not in the plan at all. On main afterwards:

DocumentAfter the undoWhy
p-000price 2000, stock 50Restored to its state before the merge
p-199price 21900, stock 50The merge deleted it; restored
p-900absentThe merge inserted it; deleted
p-100price 12000, stock 150The second merge's restock, untouched
p-150price 17000, stock 48The order's stock update, untouched
p-004price 500, stock 49Skipped as a conflict; still mispriced

The document it refused to touch

p-004 was changed twice: the merge set its price to 500, and then the order took one unit of stock. An undo restores a whole document as it was before the merge. For p-004 that would put the price right and also put the stock back to 50, as if the sale had not happened. Argon cannot know which of the two you want, so it skips the document and says who wrote to it and where:

CONFLICT products/p-004: modified by "ingest" at LSN 233 — skipped

ingest is the default label for writes captured on a branch, here the application's. The conflict is per document, not per field: the later write changed stock and the merge changed price, and it still counts. One line of the report is one decision for a person. Here the fix is a single update:

$ mongosh "$MAIN" --eval 'db.products.updateOne({_id: "p-004"}, {$set: {price: 2400}}).modifiedCount'
1

Running it twice, and the sandbox that is still there

The undo is itself written to main as new changes. It does not remove the merge from history. Two things follow.

Running the same undo again is harmless. Every document of the merge has now been modified since, by the undo, so all seven are conflicts and nothing is written:

$ argon undo --merge 6b0fc9a6…
Undo [211, 304] for actor "merge plan 6b0fc9a6…": 0 document(s) to revert
  CONFLICT products/p-004: modified by "ingest" at LSN 233 — skipped
  CONFLICT products/p-000: modified by "ingest" at LSN 280 — skipped
  …
Done: 0 restored, 0 deleted.

And the pricing agent's sandbox still holds the bad change. Undoing a merge does not change the branch it came from. A new preview from that sandbox offers the same change again:

$ argon merge preview -p shop -b pricing-agent
Merging pricing-agent → main
  put    products/p-000
  …
  CONFLICT products/p-004 (both sides changed since the fork)
6 change(s), 1 conflict(s)

Discard the sandbox, or have the agent correct it before anyone previews it again. A sandbox created from main after the undo reads the corrected data.

How Argon knows what a merge changed

  • When a plan is applied, Argon records the position of its changes on the plan: merge_from_lsn, merge_lsn and merge_changes, visible in argon merge list -o json.
  • When the target branch is checked out, the merge is written to its MongoDB database, and capture records those writes with the actor merge:<source> and the plan's ID. That is what lets undo tell the merge's writes from an application's writes to the same branch.
  • Undo restores each document from the image recorded before the merge changed it. It is refused while capture has not yet recorded the whole merge.

The details are in the CLI reference and the pull request. The REST API takes the same plan ID as merge_plan_id (agent guide).

Which undo to use

ApproachWhat it revertsLater writes
Restore a backup or recover to a point in timeThe whole database, to a momentLost
argon undo --from-lsn NEvery document touched since LSN NReverted with the rest (14 documents in this run)
argon undo --from-lsn N --actor AOne actor label's writes in a rangeKept; documents they touched are skipped
argon undo --merge PLANThe documents one applied merge changedKept; documents they touched are skipped (1 in this run)

Undo by actor suits a run you never merged, inside its own sandbox. Undo by plan suits a merge you approved and regret, because a plan ID names exactly one merge even when the same sandbox was merged more than once.

Limits

  • Skipped documents are yours to fix. A conflict is per document. The undo tells you which ones; it does not merge fields.
  • It needs complete, retained history. Undo restores from images that capture recorded. A document whose image capture missed is reported as unrecoverable and skipped, and history that retention has reclaimed can no longer be undone.
  • Not on a branch that follows a source. While main follows a live database it refuses undo. A write-back is reversed by writing another reviewed plan back.
  • Plans applied before 2.13.0 have no recorded position and are undone by LSN range.
  • CLI and REST only. The MCP tool argon_undo takes an LSN range and an actor, not a plan ID.
  • What this run did not test: a merge into a branch that is not checked out, access control, and MongoDB Atlas. See the current test coverage and limits.

The full list for this release is in the changelog.

Try it

npm install -g argonctl@2.13.2 --allow-scripts=argonctl
export MONGODB_URI="mongodb://…"           # a replica set
argon import database --uri "$MONGODB_URI" --database shop --project shop --source-quiesced --yes
argon sandbox create -p shop --name agent-run --ttl 1h --watch    # the agent writes here
argon merge preview -p shop -b agent-run
argon merge apply <plan-id>
argon merge list -p shop                   # find the plan again later
argon undo --merge <plan-id> --dry-run
argon undo --merge <plan-id>

Read the dry run before applying: each CONFLICT line is a document you will have to correct yourself.

Frequently asked questions

How do I undo an AI agent's change to a MongoDB database after it was merged?
With Argon 2.13.0 or newer, run argon undo --merge <plan-id>. It reverts the documents that one merge plan changed on the branch it was merged into, and leaves every other write alone. A document that something else changed after the merge is reported as a conflict and skipped. In our run on 2.13.2 it reverted six of a merge's seven documents and named the seventh.
Why not restore a backup or undo everything since the merge?
Both take the later writes with them. In our run, an undo by LSN range from the merge onwards planned to revert 14 documents: the merge's seven, an order and a stock update the application made afterwards, and five documents from another agent's merge. Undoing by plan ID planned only the first merge's documents.
What happens to a document that changed again after the merge?
Argon skips it and reports it as a conflict with the actor and LSN of the later write. An undo restores the whole document as it was before the merge, so restoring it would erase the later write. In our run the application had sold one unit of a product the merge had mispriced; the undo left that document alone, and we corrected its price by hand.
Does undoing a merge rewrite history?
No. The undo is written as new changes on the branch, so it appears in history and can be undone in turn. Running the same undo a second time changes nothing: every document is reported as modified since the merge.
Can I undo a change that was written back to the source database?
Not with argon undo. A main branch that follows a source refuses undo, because the source is the only thing that changes it. To reverse a write-back, write another reviewed plan back. Undo by plan ID applies to branches Argon owns, such as the main branch of an imported project that is not following a source.