---
title: "Return an undo handle with every agent · Agent Wranglers"
url: https://agentwranglers.se/playbook/agents-as-users/safe-actions/undo-handle
id: AGT-22
level: Recommended
updated: 2026-10-22
tokens: 976
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Return an undo handle with every agent change

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Safe actions](https://agentwranglers.se/playbook/agents-as-users/safe-actions.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: AGT-22
- Level: Recommended
- Chapter: [Safe actions](https://agentwranglers.se/playbook/agents-as-users/safe-actions.md)
- Rule: Every change an agent makes returns an undo handle that works for at least 24 hours. The person can undo the same change from the app.
- Check: A test agent renames a product and calls `undo` with the returned handle. The old name is back, and the history shows both steps with the agent's name. After a person edits the record, the undo reports a conflict and changes nothing.
- How: [Log agent actions and let users undo them](https://agentwranglers.se/playbook/adversarial-testing/hostile-agents-and-bots/reversible-agent-actions.md) sets the log and the soft delete. This rule hands the undo to the agent too, so it can fix its own mistake at once. Store the change as a reversible record, with the old values and who made it. Return its ID and its end time in the result. When a later change touches the same record, the undo says so instead of overwriting the newer value. Actions that cannot be undone, such as sending email, go through [Confirm risky actions with a token bound to the exact request](https://agentwranglers.se/playbook/agents-as-users/safe-actions/confirmation-token.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
const UNDO_WINDOW_MS = 24 * 3_600_000;
export async function recordChange(tx, { table, id, before, actor }) {
  const undoId = await tx.one('INSERT INTO changes (tbl, row_id, before, actor, undo_until) VALUES ($1,$2,$3,$4,$5) RETURNING id',
    [table, id, before, actor, new Date(Date.now() + UNDO_WINDOW_MS)]);
  return { undo: undoId.id, undoUntil: new Date(Date.now() + UNDO_WINDOW_MS).toISOString() };
}
```

### Python example

```python
UNDO_WINDOW = timedelta(hours=24)

def record_change(conn, table: str, row_id: str, before: dict, actor: dict) -> dict:
    undo_until = datetime.now(UTC) + UNDO_WINDOW
    undo_id = conn.execute("INSERT INTO changes (tbl, row_id, before, actor, undo_until) VALUES (%s,%s,%s,%s,%s) RETURNING id",
                           (table, row_id, Json(before), Json(actor), undo_until)).fetchone().id
    return {"undo": undo_id, "undoUntil": undo_until.isoformat()}
```

## Related rules

- SEC-100 [Log agent actions and let users undo them](https://agentwranglers.se/playbook/adversarial-testing/hostile-agents-and-bots/reversible-agent-actions.md) in Adversarial testing
- AGT-21 [Confirm risky actions with a token bound to the exact request](https://agentwranglers.se/playbook/agents-as-users/safe-actions/confirmation-token.md) in Agents as users

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/agents-as-users/safe-actions/undo-handle">Agent Wranglers agents as users playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Confirm risky actions with a token bound to the exact request](https://agentwranglers.se/playbook/agents-as-users/safe-actions/confirmation-token.md)
- Next: [Run agent calls through the same business rules as the app](https://agentwranglers.se/playbook/agents-as-users/safe-actions/same-rules-for-agents.md)
- [Safe actions](https://agentwranglers.se/playbook/agents-as-users/safe-actions.md)
