> ## Documentation Index
> Fetch the complete documentation index at: https://docs.microsandbox.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Start warm workers from a snapshot

> Install a toolchain once and launch clean workers from the captured disk state

<Tooltip tip="This workflow creates and restores local disk snapshots, which are not available on microsandbox cloud."><span className="msb-badge-local">Local-only <Icon icon="circle-info" size={11} /></span></Tooltip>

Package installation often costs more than sandbox boot. This example installs OpenCode once, records the stopped sandbox as an integrity-checked snapshot, and launches fresh workers from that prepared filesystem.

## Start a warm worker

<Steps>
  <Step title="Prepare the baseline">
    Create the worker verification script that the snapshot will carry into every worker:

    ```sh verify-worker.sh theme={null}
    #!/bin/sh
    set -eu

    test "$(id -u)" -ne 0
    git init -q
    git add .
    git -c user.name=microsandbox \
      -c user.email=worker@microsandbox.local \
      commit -qm "sandbox baseline"
    test -z "$(git remote)"
    opencode --version
    ```

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb run --name agent-base --replace \
        --cpus 2 --memory 2G --root-disk 4G \
        --script-path verify-worker:./verify-worker.sh \
        node:24-bookworm-slim -- sh -lc '
          apt-get update &&
          apt-get install -y --no-install-recommends ca-certificates git &&
          rm -rf /var/lib/apt/lists/* &&
          npm install -g opencode-ai@1.18.4 &&
          mkdir -p /workspace/project &&
          chown -R node:node /workspace &&
          opencode --version
        '
      ```

      ```powershell Windows theme={null}
      msb run --name agent-base --replace `
        --cpus 2 --memory 2G --root-disk 4G `
        --script-path verify-worker:./verify-worker.sh `
        node:24-bookworm-slim -- sh -lc '
          apt-get update &&
          apt-get install -y --no-install-recommends ca-certificates git &&
          rm -rf /var/lib/apt/lists/* &&
          npm install -g opencode-ai@1.18.4 &&
          mkdir -p /workspace/project &&
          chown -R node:node /workspace &&
          opencode --version
        '
      ```
    </CodeGroup>

    When the command exits, `agent-base` is stopped and ready to snapshot.
  </Step>

  <Step title="Create and verify the snapshot">
    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb snapshot create coding-agent-base \
        --from agent-base \
        --integrity \
        --force
      ```

      ```powershell Windows theme={null}
      msb snapshot create coding-agent-base `
        --from agent-base `
        --integrity `
        --force
      ```
    </CodeGroup>

    Verify the captured snapshot before using it:

    ```sh theme={null}
    msb snapshot verify coding-agent-base
    ```

    The snapshot captures the writable disk changes and pins the source image. It does not capture memory, running processes, network state, environment variables, or named volumes. Integrity verification detects later changes to those captured bytes; it does not attest who built the snapshot or whether its packages are trustworthy.

    <Warning>
      Snapshots preserve every file written to the guest disk, including shell history, tool configuration, and cached credentials. Build the baseline in a trusted workflow, and never authenticate OpenCode or place registry tokens, source code, or API keys in `agent-base`.
    </Warning>
  </Step>

  <Step title="Launch a clean worker">
    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb run -d --name coding-worker-1 --replace \
        --from-snapshot coding-agent-base \
        --cpus 2 --memory 2G \
        --user node --security restricted \
        --max-duration 1h \
        --workdir /workspace/project \
        -- sh -lc 'exec sleep 1h'
      ```

      ```powershell Windows theme={null}
      msb run -d --name coding-worker-1 --replace `
        --from-snapshot coding-agent-base `
        --cpus 2 --memory 2G `
        --user node --security restricted `
        --max-duration 1h `
        --workdir /workspace/project `
        -- sh -lc 'exec sleep 1h'
      ```
    </CodeGroup>

    Transfer the committed project tree into the worker:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      git -C ./my-project archive --format=tar HEAD | \
        msb exec --stream --user node \
          --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \
          coding-worker-1 -- tar -x -C /workspace/project
      ```

      ```powershell Windows theme={null}
      git -C ./my-project archive --format=tar --output=project.tar HEAD
      msb cp ./project.tar coding-worker-1:/tmp/project.tar
      msb exec --user node `
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
        coding-worker-1 -- tar -x -f /tmp/project.tar -C /workspace/project
      Remove-Item ./project.tar
      ```
    </CodeGroup>

    Create a credential-free Git baseline and verify the worker boundary:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb exec --user node --workdir /workspace/project \
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \
        coding-worker-1 -- verify-worker
      ```

      ```powershell Windows theme={null}
      msb exec --user node --workdir /workspace/project `
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
        coding-worker-1 -- verify-worker
      ```
    </CodeGroup>

    Start OpenCode after those checks pass:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb exec -t --user node --workdir /workspace/project \
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \
        coding-worker-1 -- opencode
      ```

      ```powershell Windows theme={null}
      msb exec -t --user node --workdir /workspace/project `
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
        coding-worker-1 -- opencode
      ```
    </CodeGroup>

    Rootfs patches such as `--copy-dir` cannot be combined with `--from-snapshot`, so the worker boots first and receives the committed tree afterward. `git archive` excludes `.git`, checkout credentials, and untracked files such as a local `.env`; review the committed tree for secrets before sending it. Initializing a new repository inside the worker preserves useful diff workflows without copying host remotes or credentials. Each transferred or interactive workload sets its own process, file-descriptor, and per-file limits.

    Each launch receives its own writable layer. Changes made by one worker do not modify the snapshot, the host project, or another worker.

    <Note>
      This worker uses microsandbox's default public-internet profile so OpenCode can reach a configured provider. For sensitive projects, replace it with a deny-by-default allowlist for the provider and source hosts you need, and use [host-held secrets](/sandboxes/secrets) instead of copying credentials into the worker.
    </Note>

    Create more workers by changing the sandbox name:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb run --name coding-worker-2 --replace \
        --from-snapshot coding-agent-base \
        --user node --security restricted \
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 \
        --max-duration 1m \
        -- opencode --version
      ```

      ```powershell Windows theme={null}
      msb run --name coding-worker-2 --replace `
        --from-snapshot coding-agent-base `
        --user node --security restricted `
        --rlimit nproc=256 --rlimit nofile=1024 --rlimit fsize=268435456 `
        --max-duration 1m `
        -- opencode --version
      ```
    </CodeGroup>
  </Step>

  <Step title="Clean up">
    Remove the prepared sandbox and workers:

    <CodeGroup>
      ```sh macOS & Linux theme={null}
      msb rm -f agent-base coding-worker-1 coding-worker-2
      rm -f verify-worker.sh
      ```

      ```powershell Windows theme={null}
      msb rm -f agent-base coding-worker-1 coding-worker-2
      Remove-Item verify-worker.sh
      ```
    </CodeGroup>

    Remove the reusable snapshot only when you no longer need it:

    ```sh theme={null}
    msb snapshot rm coding-agent-base
    ```
  </Step>
</Steps>

## Refresh the baseline

Snapshots are immutable. To update packages, recreate `agent-base`, then overwrite the named snapshot intentionally:

```sh theme={null}
msb snapshot create coding-agent-base --from agent-base --integrity --force
```

See [Snapshots](/sandboxes/snapshots) for archive, integrity, and portability details.
