> ## Documentation Index
> Fetch the complete documentation index at: https://axe-engine.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Snapshots and restore

> What survives a sandbox snapshot, how clients reconnect, how axed detects resumes, and how to use Freeze and Configure around a snapshot.

axed does not take snapshots. The platform does, usually without telling the guest: gVisor's `runsc checkpoint`, Firecracker snapshots, or Agent Substrate's suspend.

## What survives

A memory snapshot captures the whole sandbox, so everything axed holds comes back:

* every process, running or finished, with its id, name, labels and exit status,
* every output log, with the same offsets,
* every stdin offset,
* the process table's `instance_id`,
* the listeners inside the sandbox.

What does not survive is every client connection. Streams such as `Attach`, `Run`, `Watch` and `Files.Write` break, and some platforms (Agent Substrate) cancel them before the snapshot is taken. A replace or create-only upload that had not committed leaves nothing behind. Clients must start watches again.

> [!WARNING]
>
> A broken `Run` stream kills its process unless you set `detach`, and a broken `Exec` call without a `request_id` kills its process too. Start long-running work with `Start`, or with `Run` and `detach: true`, if it must survive a snapshot.

## Reconnect a client

**1. Remember where you were**

While attached, keep the process id and the offset one past the last output byte you received (`Output.offset` plus the length of `data`).

**2. Check the instance**

After reconnecting, call `System.Info` and compare its `instance_id` with the one you saw before. If it changed, the sandbox cold-booted. The old processes are gone and their ids return `NOT_FOUND`.

**3. Reattach from your offset**

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl attach --from 1180 build
```

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
grpcurl -plaintext -d '{"process": {"id": "44f52f99bfa0cf0048f2b8753f0c3648"}, "from_offset": 1180}' \
  localhost:9090 axe.v1.Processes/Attach
```

You get exactly the bytes after the ones you saw, or a `gap` event if some were evicted in the meantime. See [The output log](https://axe-engine.mintlify.site/processes/output.md).

**4. Retry writes safely**

Retry a start or an `Exec` with the same `request_id` and a stdin write with the same `offset`, and axed acknowledges work that already happened instead of repeating it. A process id you kept from before the snapshot can carry the `instance_id` you saw: if the sandbox cold-booted instead of restoring, the call fails with `NOT_FOUND` and reason `INSTANCE_CHANGED`. See [Start processes](https://axe-engine.mintlify.site/processes/run.md#retry-a-start-safely) and [Terminals and input](https://axe-engine.mintlify.site/processes/terminals.md#make-retries-safe-with-offsets).

## How axed detects a resume

Because platforms snapshot without calling into the guest, axed detects resumes itself. A background thread samples three clocks every second: `CLOCK_MONOTONIC`, `CLOCK_BOOTTIME` and `CLOCK_REALTIME`. Each pattern below counts as a resume:

| Pattern | Typical platform | What axed sees |
| - | - | - |
| Boot time jumped more than 5 s past monotonic time | gVisor, which hides suspended time from `CLOCK_MONOTONIC` but not from `CLOCK_BOOTTIME` | The suspended time, which timers never saw |
| Monotonic time jumped more than 5 s past the one-second tick | A VMM that catches the guest's clocks up on restore | The sampler appears to have overslept; the excess is recorded as frozen time |
| Wall time and monotonic time diverged by more than 30 s, in either direction | An old VMM whose clock stays frozen until NTP steps it | A wall-clock step with no matching running time |

Each detected resume increments `generation` and records its time in `resumed_at` (the most recent 16). `System.Info` reports both; `axectl info` shows `generation`:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
instance       d556af249746e7d135975019779baf38
generation     1
```

A scheduler stall longer than 5 seconds looks like a resume too. The cost is one extra generation and a timeout extended by the stall.

## Timeouts count running time

A process timeout measures the time the sandbox ran: monotonic time minus the frozen time axed detected. When a timer fires, axed samples the clocks again before killing, so a timer woken by a VM resume does not kill on the spot. A process started with a 20 s timeout and suspended for an hour still gets 20 s of running time. See [Signals, exits and limits](https://axe-engine.mintlify.site/processes/signals.md#set-a-timeout).

Time spent stopped by `System.Freeze` is not suspended time. The sandbox keeps running, so it counts toward timeouts.

## Before a snapshot: Freeze

`System.Freeze` stops every process tree through its cgroup (`cgroup.freeze`), so nothing changes state while the platform captures memory. That includes background jobs a finished command left running, as an Agent Sandbox command does. It waits for every tree to change state, up to `timeout` (0 means 5 seconds), and returns `changed` and `total`, the number of trees that had tasks in them.

> [tab] axectl
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> axectl freeze           # 1 of 1 process trees frozen
> # take the snapshot
> axectl freeze --thaw    # 1 of 1 process trees thawed
> ```

> [tab] gRPC
>
> ```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
> grpcurl -plaintext -d '{"frozen": true, "timeout": "10s"}' localhost:9090 axe.v1.System/Freeze
> grpcurl -plaintext -d '{"frozen": false}' localhost:9090 axe.v1.System/Freeze
> ```

* Freeze needs cgroups. Without them it fails with `FAILED_PRECONDITION`.
* A `changed` lower than `total` means some trees did not finish changing state within the timeout.
* Until the thaw, axed refuses to start processes, on every API, with `FAILED_PRECONDITION` (`FROZEN`), so nothing runs while the rest are stopped. Starts already under way finish first and are frozen with the rest. `System.Health` reports `frozen`.
* The frozen state is part of the snapshot. Thaw after you restore, with `Freeze` or with `thaw` in `Configure`, or the processes stay stopped and starts stay refused.

Freezing is optional. A memory snapshot is consistent without it; Freeze gives you a quiet point, for example to snapshot the disk at the same moment.

## After a restore: Configure

`System.Configure` hands axed state that only the host knows. Call it after every restore on platforms whose clocks do not reveal the resume.

| Field | Effect |
| - | - |
| `resumed` | Records a resume and increments `generation`, for platforms whose clocks show nothing. |
| `wall_time` | Steps `CLOCK_REALTIME` to this time if it is off by more than 50 ms. Needs `CAP_SYS_TIME` when a step is needed. The step itself does not count as a resume. |
| `entropy` | Credits these bytes to the kernel entropy pool, so clones of one snapshot stop sharing random state. Needs `CAP_SYS_ADMIN`; without it the bytes are dropped and the call still succeeds. |
| `thaw` | Thaws processes frozen before the snapshot, after the clock is set and the resume recorded, so they wake to the right time. Does nothing without cgroups. |

The response carries `clock_correction` (positive when the clock moved forward), the new `generation`, `entropy_added`, which says whether the kernel took the entropy, and `thawed`, the trees `thaw` thawed.

`axectl resumed` sends the local wall time, 64 bytes of fresh entropy, and `resumed: true`:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
axectl resumed                  # generation 1, clock stepped 0.000s, entropy added
axectl resumed --no-entropy     # generation 2, clock stepped 0.000s
axectl resumed --thaw           # generation 3, clock stepped 0.000s, entropy added, 1 process trees thawed
```

> [!WARNING]
>
> axed checks the whole request first, then steps the wall clock, then adds entropy, then records the resume, and thaws last. A request that fails changes nothing that comes after the failure, and in particular does not count as a resume, so retrying it never inflates the generation. A clock step without `CAP_SYS_TIME` fails with `PERMISSION_DENIED: setting the clock needs CAP_SYS_TIME`. Entropy is best effort: without `CAP_SYS_ADMIN` it is skipped, `entropy_added` is false and `axectl resumed` prints `entropy not added`. The default image runs axed without either capability, so make sure the clock is within 50 ms or grant `CAP_SYS_TIME`.

`axectl resumed` uses the clock of the machine where `axectl` runs, so run it on the host or wherever the time is right.

## Platform notes

> [accordion] gVisor
>
> gVisor's clocks reveal the suspend on their own. Boot time includes it and monotonic time does not, so detection is automatic and timers are unaffected. gVisor offers no delegated cgroup, so `Freeze` is not available. After a checkpoint and restore, a detached process keeps running, replay from offset 0 is intact, generation becomes 1 with a gap about as long as the suspension, and a timeout fires after its full running time. See [gVisor](https://axe-engine.mintlify.site/deploy/gvisor.md).

> [accordion] Firecracker
>
> Firecracker freezes the guest's clocks across a snapshot, so timeouts stay right without help, but the guest's wall clock falls behind and nothing in the clocks reveals the resume. Call `System.Configure` (`axectl resumed`) after every restore: it steps the clock, adds entropy and records the resume. Clients reconnect through the surviving vsock listener. axed manages the guest's cgroup v2 hierarchy there, so `Freeze` is available. See [Firecracker](https://axe-engine.mintlify.site/deploy/firecracker.md).

> [accordion] Cold boot fallback
>
> Some platforms, Agent Substrate among them, restore from the persisted disk instead of memory when a memory snapshot is not available. That is a cold boot. axed starts fresh with a new `instance_id` and generation 0, and process ids from before return `NOT_FOUND`. Files on the persisted disk remain. Compare `instance_id` on every reconnect to tell the two cases apart. See [Agent Substrate](https://axe-engine.mintlify.site/deploy/agent-substrate.md).

> [accordion] Eviction
>
> A platform that sends SIGTERM before suspending would normally make axed shut down. Start axed with `--on-sigterm=stay` to keep it running instead; `System.Health` then reports `eviction_pending: true`, and `axectl info` shows `pending` on its `eviction` line. SIGINT always shuts down.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.