# Fork, checkpoints and rewind

A sandbox is a whole running machine, so you can save it, go back to it and copy it, memory and processes included. This is what lets an agent try something risky, or an RL loop branch a rollout, without setting anything up again.

## Checkpoint and rewind

**CLI**

```
ID=$(kosa8 cloud sandbox create --template python --ttl 15m --json | jq -r .sandbox.id)
kosa8 cloud sandbox exec $ID -- sh -c 'echo v1 > /workspace/state'

CK=$(kosa8 cloud checkpoint create $ID --label "before the risky part" --json | jq -r .checkpoint.id)
kosa8 cloud sandbox exec $ID -- sh -c 'echo broken > /workspace/state'

kosa8 cloud checkpoint rewind $ID $CK              # memory, processes and disk
kosa8 cloud sandbox exec $ID -- cat /workspace/state   # v1

kosa8 cloud sandbox rm $ID
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
JSON='Content-Type: application/json'
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H "$JSON" -d '{"template":"python","ttl_s":900}' | jq -r .sandbox.id)
run() { curl -s $API/sandboxes/$SBX/exec -H "$AUTH" -H "$JSON" -d "{\"cmd\":[\"sh\",\"-c\",\"$1\"]}" | jq -r .stdout; }

run 'echo v1 > /workspace/state'
CK=$(curl -s $API/sandboxes/$SBX/checkpoints -H "$AUTH" -H "$JSON" \
  -d '{"label":"before the risky part"}' | jq -r .checkpoint.id)
run 'echo broken > /workspace/state'

curl -s $API/sandboxes/$SBX/rewind -H "$AUTH" -H "$JSON" -d "{\"checkpoint\":\"$CK\"}" > /dev/null
run 'cat /workspace/state'     # v1

curl -s -X DELETE $API/sandboxes/$SBX -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

with cloud.sandboxes.create(template="python", ttl_s=900) as box:
    box.exec("echo v1 > /workspace/state")
    ck = box.checkpoint("before the risky part")   # memory, processes and disk

    box.exec("echo broken > /workspace/state")
    box.rewind(ck["id"])                            # the old state is saved first, as a backup

    print(box.exec("cat /workspace/state").stdout)  # v1
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();
const box = await cloud.sandboxes.create({ template: 'python', ttlS: 900 });
try {
  await box.exec('echo v1 > /workspace/state');
  const ck = await box.checkpoint('before the risky part'); // memory, processes and disk

  await box.exec('echo broken > /workspace/state');
  await box.rewind(ck);                                      // the old state is saved first

  console.log((await box.exec('cat /workspace/state')).stdout); // v1
} finally {
  await box.delete();
}
```

- **A checkpoint** saves memory, processes and disk. Take one with `POST /v1/sandboxes/{id}/checkpoints`, with an optional `label`. The newest 10 are kept; older ones are dropped and listed in `evicted`.

- **Rewind** (`POST /v1/sandboxes/{id}/rewind` with `checkpoint_id`) puts the sandbox back. How it was just before is saved as an automatic checkpoint first, so a rewind can itself be undone.

- Checkpoints belong to their sandbox and go when it is deleted. To keep a state for good, or start other sandboxes from it, take a [snapshot](https://docs.kosa8.com/concepts/snapshots/).

## Fork

**CLI**

```
ID=$(kosa8 cloud sandbox create --template python --ttl 15m --json | jq -r .sandbox.id)
kosa8 cloud sandbox exec $ID -- sh -c 'echo "set up once" > /workspace/ready'

# Three live copies, memory and processes included
kosa8 cloud sandbox fork $ID -n 3 --prefix try --json | jq -r '.sandboxes[].id' > forks.txt
for F in $(cat forks.txt); do
  kosa8 cloud sandbox exec $F -- cat /workspace/ready
done

kosa8 cloud sandbox rm $ID $(cat forks.txt)
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
JSON='Content-Type: application/json'
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H "$JSON" -d '{"template":"python","ttl_s":900}' | jq -r .sandbox.id)
curl -s $API/sandboxes/$SBX/exec -H "$AUTH" -H "$JSON" \
  -d '{"cmd":["sh","-c","echo set up once > /workspace/ready"]}' > /dev/null

# Three live copies of the sandbox as it is now
FORKS=$(curl -s $API/sandboxes/$SBX/fork -H "$AUTH" -H "$JSON" -d '{"n":3}' | jq -r '.sandboxes[].id')
for F in $FORKS; do
  curl -s $API/sandboxes/$F/exec -H "$AUTH" -H "$JSON" -d '{"cmd":["cat","/workspace/ready"]}' | jq -r .stdout
done

for S in $SBX $FORKS; do curl -s -X DELETE $API/sandboxes/$S -H "$AUTH" > /dev/null; done
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

with cloud.sandboxes.create(template="python", ttl_s=900) as box:
    box.exec("echo 'set up once' > /workspace/ready")

    forks = box.fork(3)                  # three live copies, memory and processes included
    for f in forks:
        print(f.id, f.exec("cat /workspace/ready").stdout.strip())
        f.delete()
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();
const box = await cloud.sandboxes.create({ template: 'python', ttlS: 900 });
try {
  await box.exec("echo 'set up once' > /workspace/ready");

  const forks = await box.fork(3);       // three live copies, memory and processes included
  const outs = await Promise.all(forks.map((f) => f.exec('cat /workspace/ready')));
  outs.forEach((r, i) => console.log(forks[i].id, r.stdout.trim()));
  await Promise.all(forks.map((f) => f.delete()));
} finally {
  await box.delete();
}
```

`POST /v1/sandboxes/{id}/fork` with `n` makes that many running copies of the sandbox as it is now. Each copy is a full sandbox of its own, with its own id, billed and limited like a create. Copies land on the parent's host first, as copy-on-write restores, so they start fast. Up to 1,000 per call, made 100 at a time; when the parent's host is full the rest are restored on other hosts.

The answer has `requested`, `created` and a `failed` list, so a partial fork tells you exactly what you got. Secrets are never copied into forks.

## Patterns

[**Branching rollouts**Checkpoint at a decision point, fork n ways, score each, keep the best.](https://docs.kosa8.com/guides/rl-training/) [**Fan out a test suite**Install once, snapshot, then shard across copies.](https://docs.kosa8.com/guides/cloud-fan-out/) [**Undo for agents**Checkpoint before every risky tool call and rewind if it goes wrong.](https://docs.kosa8.com/guides/agent-hooks/)

---
Source: https://docs.kosa8.com/concepts/fork-rewind/
