AGT-22Recommended
Return an undo handle with every agent change
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.
How to do it
Log agent actions and let users undo them 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.
Examples
JavaScript example
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
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()}
How to 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.
Related rules
- SEC-100 Log agent actions and let users undo them in Adversarial testing
- AGT-21 Confirm risky actions with a token bound to the exact request in Agents as users
For agents: this playbook is free to use under CC 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.