# rewound

Canonical URL: https://developers.iagenify.com/sdk/agent/events/rewound/
Language: en

`{ type: "rewound", n, removed, files, kept, credits }`

A run that was asked to edit an earlier message went back first. It carries exactly what rewind() returns — including what going back did not undo.

Status: only with run({ edit }) · before anything else · names what stays done

## When it is emitted {#when}

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.

```javascript
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");
  },
});
```

## Fields {#fields}

```javascript
{
  type: "rewound",
  n: 2,
  removed: { messages: 11, turns: 5 },
  files: { restored: [...], recreated: [...], deleted: [...], skipped: [...] },
  kept: ["send_mail", "deploy"],
  credits: { spent: 24, refunded: 0 }
}
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `n` | integer |  | The message gone back to. |
| `removed` | object |  | How many messages and assistant turns left the conversation. |
| `files` | object |  | What was restored, recreated, deleted — and `skipped`, with a reason each. |
| `kept` | array |  | **What left the machine and stays done** — a send, a deploy, a storage write, a workflow, a command. |
| `credits` | object |  | `{ spent, refunded: 0 }`. The removed turns were billed, and are not given back. |

## kept is the field to show {#kept}

> **« Rewound » describes the files, not the world**
>
> 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 {#failures}

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 {#standalone}

```javascript
const outcome = await agent.rewind(2);   // the same object, returned rather than emitted
```

- [Checkpoints](/sdk/agent/checkpoints): The whole mechanism.
- [rewind()](/sdk/ref/local-agent/rewind): The method.
- [What a rewind cannot undo](/sdk/agent/checkpoints/limits): Where kept comes from.

```python
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,
)
```

```python
{
    "type": "rewound",
    "n": 2,
    "files": {"restored": [...], "recreated": [...], "deleted": [...], "skipped": [...]},
    "kept": [],
    "credits": {"spent": 0, "refunded": 0},
}
```

```python
outcome = agent.rewind(2)   # the same object, returned rather than emitted
```
