Skip to main content

The Data Rollback Problem: Undoing What Your AI Agent Wrote to Production

· 10 min read
Tian Pan
Software Engineer

During a live executive demo, an AI coding agent deleted an entire production database. The fix wasn't a clever rollback script — it was a four-hour database restore from backups. The company had given an AI agent unrestricted SQL execution in production, and when the agent "panicked" (its word, not a metaphor), it executed DROP TABLE with no confirmation gate. Over 1,200 executives and 1,190 companies lost data. That incident wasn't an edge case. It was a preview.

As AI agents take on more write-heavy operations — updating records, processing transactions, modifying customer state — the question of how to undo their mistakes becomes load-bearing infrastructure. The problem is that "rollback" as engineers understand it from relational databases does not translate cleanly to agentic systems. The standard tools break in three specific ways that are worth understanding before your first agent incident.

Why Traditional Database Rollbacks Don't Work

Database rollbacks work because they operate within a transaction boundary. The commit hasn't happened yet, the locks are held, and the undo log has everything the engine needs. The moment your transaction commits, the safety net is gone — but for human-authored writes, that's usually fine because the scope is narrow and deliberate.

AI agents violate every assumption that makes rollbacks tractable.

First, downstream systems consume agent-written data immediately. A single agent write might update a record, publish an event, invalidate a cache entry, and update a vector embedding — all in one operation, across four systems. You can't hold distributed transactions open across systems that have no shared transaction coordinator. By the time you detect a problem, the downstream effects have already propagated.

Second, agents write at machine speed. A human developer might make 50 deliberate writes to production in a day. An agent processing a queue might make 50,000. When anomaly detection finally surfaces a problem — the median detection time for AI data incidents is around four and a half days — the window for surgical rollback has long passed. You're dealing with contaminated state across hundreds of millions of rows.

Third, agent intent is not preserved in the write. When a human writes a bug, you can look at the git commit, the PR description, and the Jira ticket to understand what they meant to do. When an agent writes bad data, the model's reasoning at that moment is gone. Compensation must be algorithmic, not context-aware. You can't ask the model to explain itself — it doesn't remember.

The Fan-Out Problem

The real rollback nightmare is not a single bad write. It's a bad write that spawns more bad writes.

Modern agentic systems are orchestrators. An orchestrator agent decides what to do, then dispatches to specialist subagents for execution. If the orchestrator's decision is wrong, every downstream agent faithfully executes the wrong plan. OWASP has formalized this as a distinct security category — cascading failures in agentic AI — because the blast radius is fundamentally different from a single-system error.

Consider an orchestrator that decides a customer deserves a $1,000 refund based on a misread conversation. It dispatches simultaneously to a payment agent, an inventory agent, an analytics agent, a notification agent, and a ledger agent. The payment agent processes the refund. The notification agent emails the customer. The ledger agent files an accounting entry. The analytics agent updates revenue reports. Four days later, a human notices something wrong.

You don't have one problem to undo. You have five, each touching different systems with different consistency models and different rollback mechanisms — if they have any at all. The notification is already sent. The customer has already been told. The accounting entry is already in a compliance report.

This is why the architecture decision that matters is not "how do we roll back" but "how do we write so that rollback remains possible."

Writing for Reversibility

The patterns that make agent writes reversible are borrowed from distributed systems and event-driven architecture. None of them are new. What's new is that agentic systems make them mandatory rather than optional.

Soft deletes are the simplest starting point. Instead of DELETE FROM users WHERE id = 123, you UPDATE users SET deleted_at = NOW(), deleted_by_agent = 'agent-7', deletion_reason = 'concluded_duplicate' WHERE id = 123. The record becomes logically invisible to normal queries but physically present. Undo is a one-liner. The audit trail — which agent, when, why — is preserved in the schema.

The Replit incident is a perfect illustration of the difference. An agent with a soft-delete architecture would have produced a recoverable state in seconds. With hard deletes and no audit trail, recovery required restoring from backup and four hours of downtime.

Tombstone records extend this pattern for distributed systems where replication lag is a factor. A tombstone is a lightweight marker that says "this record was deleted at timestamp T." Without tombstones, deleted records can resurrect on out-of-sync nodes that haven't received the delete yet. For AI systems that write to multiple datastores — relational databases, vector stores, caches — tombstones keep deletions consistent across eventual-consistency boundaries.

Write-ahead logs with agent provenance record intent before execution. The log entry includes the agent ID, agent version, idempotency key, reasoning, and downstream systems to notify. If the system crashes mid-write, recovery replays from the log. More importantly, the log becomes a complete record of what the agent did and why — something you'll need during incident investigation.

Compensating Transactions: The Actual Undo Mechanism

When soft deletes aren't enough — when the agent has already sent an email, processed a payment, or pushed an event to an external system — compensation is the only mechanism available.

A compensating transaction is an operation that logically reverses a previous operation. "Reverse a 1,000refund"isthecompensationfor"processa1,000 refund" is the compensation for "process a 1,000 refund." The Saga pattern formalizes this: every operation in a distributed transaction has a corresponding compensation, and if any step fails, the system executes compensations in reverse order.

The critical insight is that compensating transactions are not perfect inverses. You can reverse a refund, but you can't un-send an email. You can issue a correction notice, but the customer already saw the original. This is why compensation design happens at architecture time, not at incident time. The question to ask for every agent action is: "If this turns out to be wrong, what is the correction path?" If the answer is "there isn't one," you need an approval gate before that action executes, not after.

For AI agents specifically, compensation design has to account for the fact that the agent might batch thousands of similar operations before a problem surfaces. Compensation should be idempotent — running the compensating operation twice should not create a worse state than running it once. It should be atomic — compensation of step 3 should not require manual coordination with step 2's system. And it should produce an audit entry — the compensation is itself a write that needs to be traceable.

Event Sourcing as the Backbone

The architectural pattern that makes all of this tractable at scale is event sourcing. Instead of storing current state, store every state change as an immutable event.

A conventional write says: "user 123's balance is now 900." An event-sourced write says: "at 14:30:00, agent-finance-7 decremented user 123's balance by 100, reason: customer_refund, idempotency_key: run-999-step-3." The current balance of 900 is a projection derived by replaying events. The event itself is immutable.

This matters for rollback because you can reconstruct state at any point in time by replaying events up to a specific timestamp. More practically, compensation becomes a first-class event — you append a new event to the log rather than mutating the past. The audit trail is complete. Any agent that reads state can see not just what the current value is but the full history of how it got there.

Event sourcing is expensive — it requires more storage, and queries must aggregate events rather than reading a single row. But the cost is front-loaded and predictable. The cost of not having it shows up as an unpredictable incident requiring manual data archaeology.

The Incident Playbook

When an AI data incident is detected, the organizational response needs to differ from a traditional production incident in a few specific ways.

Detection is harder and slower. AI errors are judgment failures — the wrong amount refunded, the wrong record modified, the wrong email sent. They don't throw exceptions. They don't trigger alerting. An anomaly might be a subtle percentage shift in a metric that only looks wrong to a human analyst. The four-and-a-half day average detection lag isn't laziness; it reflects how invisible these errors are.

Scope assessment is non-trivial. You need to know: which agent instance, which version, which run IDs, which records were touched. This is why agent provenance metadata in writes is infrastructure, not a nice-to-have. Without it, scope assessment turns into a data forensics exercise.

Compensation must be planned before execution. Unlike a database rollback that is either all-or-nothing, agentic compensation must account for downstream systems that may have partially processed the contaminated data. The compensation plan should be simulated before execution, reviewed by a human, and executed idempotently.

Downstream notifications are part of the fix. If the agent wrote bad data that a downstream partner consumed, the fix isn't complete until that partner has received and acknowledged the correction. Treating the write-side fix as sufficient while downstream systems continue operating on bad data is a common failure mode.

What to Change Before You Ship

The architectural changes that prevent rollback nightmares fall into three layers.

At the data layer: adopt soft deletes for all agent-written records, instrument every write with agent ID and run ID, use event sourcing for high-stakes state, and enforce idempotency keys to prevent duplicate effects on retry. Agents retry tool calls 15–30% of the time due to timeouts and validation errors — without idempotency, every retry is a potential duplicate write.

At the operational layer: maintain hard separation between production and non-production access. An agent that can write to production should not be the same agent you experiment with in staging. Scope agent capabilities to the minimum required. An agent that sends emails should not also have database write access. Build approval gates for destructive operations — delete, drop, truncate — that require human confirmation before execution.

At the incident layer: define compensation plans before agents go live, not after an incident. For every agent action that touches external state, the question "what is the correction path if this is wrong?" must have an answer in the runbook. Run tabletop exercises specifically for AI data incidents. The failure modes are different enough from traditional outages that teams encountering them for the first time will be disoriented.

The hardest part isn't the architecture. The hardest part is that most teams don't discover these gaps until the demo where the agent confidently says "I panicked" and the database is gone. Design for that moment before it arrives.

References:Let's stay in touch and Follow me for more thoughts and updates