# rewind()

Canonical URL: https://developers.iagenify.com/sdk/ref/local-agent/rewind/
Language: en

`agent.rewind(n) → Promise<Outcome>`

Go back to a message: the files return to what they were, the message and everything after it leave the conversation — and the answer names what could not be undone.

Status: needs a session · stop it first · names what stays done

```javascript
const outcome = await agent.rewind(2);
// {
//   n: 2,
//   removed: { messages: 11, turns: 5 },
//   files: { restored: [...], recreated: [...], deleted: [...], skipped: [...] },
//   kept: ["send_mail", "deploy"],
//   credits: { spent: 24, refunded: 0 }
// }
```

## The workspace first {#order}

Files are restored before anything else, because that is the part that can fail. Then the conversation is cut, then the device journal, then the account's copy.

## What it reports {#returns}

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `n` | integer |  | The message gone back to. |
| `removed` | object |  | `{ messages, turns }` — what left the conversation. |
| `files` | object |  | `restored`, `recreated`, `deleted`, and `skipped` with a reason each. |
| `kept` | string[] |  | **What left the machine and stays done.** |
| `credits` | object |  | `{ spent, refunded: 0 }`. The removed turns were billed and are not given back. |

> **kept and refunded: 0 are the honest part**
>
> A message that was sent is sent, a site that was published is online, credits that were spent are spent. The answer names them rather than letting « rewind » suggest the world was put back.

## What files.skipped means {#skipped}

| Reason | Means |
| --- | --- |
| too large to have been copied | Over 25 MB — recorded, never copied, cannot be restored |
| saved content is gone | The stored content was collected |
| could not be written | Locked, in use, or refused by the file system |
| more than 5 000 files: the checkpoint is partial | The snapshot never held everything |

## Refusals {#errors}

| Code | Status | When |
| --- | --- | --- |
| session_required | 400 | No session — `memory: true` is needed. |
| agent_running | 409 | It is answering. `stop()` first. |
| invalid_checkpoint | 400 | `n` is not a whole number of at least 1. |
| checkpoints_disabled | 400 | `checkpoints: false`. |
| checkpoint_compacted | 404 | That message was summarised away. The message names what is left. |
| checkpoint_not_found | 404 | No such message. The message lists the available numbers. |

## Rewinding and running in one call {#edit}

```javascript
await agent.run({ edit: 2, message: "Actually, rename them to kebab-case." });
```

The same rewind, then the run — with a `rewound` event carrying this outcome.

- [Checkpoints](/sdk/agent/checkpoints): The mechanism.
- [What a rewind cannot undo](/sdk/agent/checkpoints/limits): The honest list.
- [rewound](/sdk/agent/events/rewound): The event.

```python
outcome = agent.rewind(2)
# {
#   "n": 2,
#   "files": {"restored": [...], "recreated": [...], "deleted": [...], "skipped": [...]},
#   "kept": [],
#   "credits": {"spent": 0, "refunded": 0}
# }
```

```python
agent.run(edit=2, message="Actually, rename them to kebab-case.")
```
