{"slug": "undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes", "title": "Undo One AI Agent's Merge in MongoDB Without Losing Later Writes", "summary": "Argon 2.13.0 and newer adds an `argon undo --merge <plan-id>` command that reverts only the documents a single approved AI-agent merge changed in MongoDB, skipping and naming any document modified again after that merge instead of overwriting it. In a walkthrough on Argon 2.13.2 against MongoDB 7.0.48, a pricing agent's merge of 7 changes (5 mispriced audio products set to 500 cents, a new product p-900, and deletion of p-199) was applied at LSN 211, followed by an application order that decremented stock on p-004 and p-150 and an inventory-agent merge of 5 changes at LSN 248; the undo then refused to touch the one document changed since.", "body_md": "# Undo One AI Agent's Merge in MongoDB Without Losing Later Writes\n\nAn 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.\n\nCurrent 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](https://argonlabs.tech/features#capabilities).\n\nYou 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?\n\n**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.**\n\nThis 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.\n\n## What we ran\n\n- **Argon 2.13.2** , installed from npm as`argonctl` .\n- **MongoDB 7.0.48** as a one-member replica set, in a container.\n- 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.\n\nEvery command and its output is in [the transcript](https://github.com/argon-lab/benchmarks/blob/main/reports/2026-12-02-v2.13.2-merge-undo-walkthrough/transcript.txt). [The report](https://github.com/argon-lab/benchmarks/tree/main/reports/2026-12-02-v2.13.2-merge-undo-walkthrough) 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 …\n\n## The merge that should not have been approved\n\nA 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.\n\n``` bash\n$ mongosh \"$PRICING\" --eval 'db.products.updateMany({category: \"audio\", _id: {$lt: \"p-020\"}}, {$set: {price: 500}}).modifiedCount'\n5\n\n$ argon merge preview -p shop -b pricing-agent\nMerging pricing-agent → main\n  put    products/p-000\n  put    products/p-004\n  put    products/p-008\n  put    products/p-012\n  put    products/p-016\n  delete products/p-199\n  put    products/p-900\n7 change(s), 0 conflict(s)\n\nPlan 6b0fc9a6… saved (pending).\n\n$ argon merge apply 6b0fc9a6…\nMerged: 7 change(s) applied.\n```\n\nSeven 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`.\n\n## The database keeps moving\n\nNobody 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.\n\n``` bash\n$ 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'\no-1001\n\n$ argon merge list -p shop\nPLAN          STATUS     SOURCE→TARGET     CHANGES  CONFLICTS CREATED\n6b0fc9d3…     applied    inventory-agent→main        5          0 2026-12-02 07:33:39\n6b0fc9a6…     applied    pricing-agent→main        7          0 2026-12-02 07:32:54\n```\n\nHistory 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.\n\n## Why undoing everything since the merge is the wrong tool\n\nBefore 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:\n\n``` bash\n$ argon undo -p shop -b main --from-lsn 211 --dry-run\nUndo [211, 253]: 14 document(s) to revert\n  delete  orders/o-1001\n  restore products/p-000\n  restore products/p-004\n  restore products/p-008\n  restore products/p-012\n  restore products/p-016\n  restore products/p-100\n  restore products/p-101\n  restore products/p-102\n  restore products/p-103\n  restore products/p-104\n  restore products/p-150\n  restore products/p-199\n  delete  products/p-900\nDry run: nothing applied.\n```\n\nFourteen 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](https://argonlabs.tech/blog/mongodb-time-travel-vs-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.\n\n## Undo the one merge\n\nFrom 2.13.0 an applied plan records where its changes sit in history, and undo accepts the plan's ID:\n\n``` bash\n$ argon undo --merge 6b0fc9a6… --dry-run\nUndo [211, 253] for actor \"merge plan 6b0fc9a6…\": 6 document(s) to revert\n  restore products/p-000\n  restore products/p-008\n  restore products/p-012\n  restore products/p-016\n  restore products/p-199\n  delete  products/p-900\n  CONFLICT products/p-004: modified by \"ingest\" at LSN 233 — skipped\nDry run: nothing applied.\n\n$ argon undo --merge 6b0fc9a6…\n…\nDone: 5 restored, 1 deleted.\n```\n\nFour 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:\n\n| Document | After the undo | Why | \n|---|---|---|\n| `p-000` | price 2000, stock 50 | Restored to its state before the merge | \n| `p-199` | price 21900, stock 50 | The merge deleted it; restored | \n| `p-900` | absent | The merge inserted it; deleted | \n| `p-100` | price 12000, stock 150 | The second merge's restock, untouched | \n| `p-150` | price 17000, stock 48 | The order's stock update, untouched | \n| `p-004` | price 500, stock 49 | Skipped as a conflict; still mispriced | \n\n## The document it refused to touch\n\n`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:\n\n```\nCONFLICT products/p-004: modified by \"ingest\" at LSN 233 — skipped\n```\n\n`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:\n\n``` bash\n$ mongosh \"$MAIN\" --eval 'db.products.updateOne({_id: \"p-004\"}, {$set: {price: 2400}}).modifiedCount'\n1\n```\n\n## Running it twice, and the sandbox that is still there\n\nThe undo is itself written to `main` as new changes. It does not remove the merge from history. Two things follow.\n\nRunning 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:\n\n``` bash\n$ argon undo --merge 6b0fc9a6…\nUndo [211, 304] for actor \"merge plan 6b0fc9a6…\": 0 document(s) to revert\n  CONFLICT products/p-004: modified by \"ingest\" at LSN 233 — skipped\n  CONFLICT products/p-000: modified by \"ingest\" at LSN 280 — skipped\n  …\nDone: 0 restored, 0 deleted.\n```\n\nAnd 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:\n\n``` bash\n$ argon merge preview -p shop -b pricing-agent\nMerging pricing-agent → main\n  put    products/p-000\n  …\n  CONFLICT products/p-004 (both sides changed since the fork)\n6 change(s), 1 conflict(s)\n```\n\nDiscard 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.\n\n## How Argon knows what a merge changed\n\n- 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` .\n- 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.\n- 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.\n\nThe details are in the [CLI reference](https://github.com/argon-lab/argon/blob/v2.13.2/docs/CLI.md#history-time-travel-undo-restore) and [the pull request](https://github.com/argon-lab/argon/pull/222). The REST API takes the same plan ID as `merge_plan_id` ([agent guide](https://github.com/argon-lab/argon/blob/v2.13.2/docs/AGENTS.md)).\n\n## Which undo to use\n\n| Approach | What it reverts | Later writes | \n|---|---|---|\n| Restore a backup or recover to a point in time | The whole database, to a moment | Lost | \n| `argon undo --from-lsn N` | Every document touched since LSN N | Reverted with the rest (14 documents in this run) | \n| `argon undo --from-lsn N --actor A` | One actor label's writes in a range | Kept; documents they touched are skipped | \n| `argon undo --merge PLAN` | The documents one applied merge changed | Kept; documents they touched are skipped (1 in this run) | \n\nUndo 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.\n\n## Limits\n\n- **Skipped documents are yours to fix.** A conflict is per document. The undo tells you which ones; it does not merge fields.\n- **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.\n- **Not on a branch that follows a source.** While`main` follows a live database it refuses undo. A[write-back](https://argonlabs.tech/blog/branch-live-mongodb-write-back) is reversed by writing another reviewed plan back.\n- **Plans applied before 2.13.0** have no recorded position and are undone by LSN range.\n- **CLI and REST only.** The MCP tool`argon_undo` takes an LSN range and an actor, not a plan ID.\n- **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](https://argonlabs.tech/features#capabilities) .\n\nThe full list for this release is in the [changelog](https://github.com/argon-lab/argon/blob/v2.13.2/CHANGELOG.md).\n\n## Try it\n\n```\nnpm install -g argonctl@2.13.2 --allow-scripts=argonctl\nexport MONGODB_URI=\"mongodb://…\"           # a replica set\nargon import database --uri \"$MONGODB_URI\" --database shop --project shop --source-quiesced --yes\nargon sandbox create -p shop --name agent-run --ttl 1h --watch    # the agent writes here\nargon merge preview -p shop -b agent-run\nargon merge apply <plan-id>\nargon merge list -p shop                   # find the plan again later\nargon undo --merge <plan-id> --dry-run\nargon undo --merge <plan-id>\n```\n\nRead the dry run before applying: each `CONFLICT` line is a document you will have to correct yourself.\n\n## Frequently asked questions\n\n- How do I undo an AI agent's change to a MongoDB database after it was merged?\n- 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.\n- Why not restore a backup or undo everything since the merge?\n- 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.\n- What happens to a document that changed again after the merge?\n- 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.\n- Does undoing a merge rewrite history?\n- 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.\n- Can I undo a change that was written back to the source database?\n- 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.", "url": "https://wpnews.pro/news/undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes", "canonical_source": "https://argonlabs.tech/blog/undo-ai-agent-merge-mongodb", "published_at": "2026-12-02 00:00:00+00:00", "updated_at": "2026-12-02 08:14:09.717072+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools"], "entities": ["Argon", "MongoDB", "argonctl", "Argon 2.13.2", "MongoDB 7.0.48", "pricing-agent", "inventory-agent"], "also_reported_by": [], "takeaway": "`argon undo --merge <plan-id>` in Argon 2.13.0+ reverts only the documents one merge changed and skips documents written after it, so a bad agent merge can be rolled back without clobbering later application or agent writes.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes", "markdown": "https://wpnews.pro/news/undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes.md", "text": "https://wpnews.pro/news/undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes.txt", "jsonld": "https://wpnews.pro/news/undo-one-ai-agent-s-merge-in-mongodb-without-losing-later-writes.jsonld"}}