When it is emitted
Only on run({ edit: n, message }) — editing an earlier message means going back to it and starting again with new text. The event is emitted before anything else of the run.
await agent.run({
edit: 2, // go back to just before message 2
message: "Actually, rename them to kebab-case.",
onEvent: (e) => {
if (e.type === "rewound") console.log("undid", e.removed.messages, "messages");
},
});def on_event(event):
if event["type"] == "rewound":
print("restored", len(event["files"]["restored"]), "files")
agent.run(
edit=2, # go back to just before message 2
message="Actually, rename them to kebab-case.",
on_event=on_event,
)Fields
{
type: "rewound",
n: 2,
removed: { messages: 11, turns: 5 },
files: { restored: [...], recreated: [...], deleted: [...], skipped: [...] },
kept: ["send_mail", "deploy"],
credits: { spent: 24, refunded: 0 }
}{
"type": "rewound",
"n": 2,
"files": {"restored": [...], "recreated": [...], "deleted": [...], "skipped": [...]},
"kept": [],
"credits": {"spent": 0, "refunded": 0},
}nintegerThe message gone back to.
removedobjectHow many messages and assistant turns left the conversation.
filesobjectWhat was restored, recreated, deleted — and skipped, with a reason each.
keptarrayWhat left the machine and stays done — a send, a deploy, a storage write, a workflow, a command.
creditsobject{ spent, refunded: 0 }. The removed turns were billed, and are not given back.
kept is the field to show
If kept is not empty, something happened that going back does not reverse. An interface that hides it tells the user the run was undone, which is false — and false in the direction that matters.
A rewind can fail
It throws before emitting anything: session_required, agent_running, checkpoints_disabled, checkpoint_compacted, checkpoint_not_found, invalid_checkpoint. The run does not start.
Rewinding without running
const outcome = await agent.rewind(2); // the same object, returned rather than emittedoutcome = agent.rewind(2) # the same object, returned rather than emitted