Skip to content

Commit 9d79a97

Browse files
authored
feat(workbot): run desktop computers on Fast Sandbox microVMs (#6726)
Workbot computers placed through a sandbox gateway can now run as OpenSandbox Fast Sandbox (Firecracker) microVMs. Dockerfile.desktop-fast-sandbox layers what Fast Sandbox's guest init and snapshot restores need onto the desktop image, and fast-sandbox-guest-init.sh, the template's entrypoint, fixes /run's mode, keeps the clock with chrony and starts the desktop as the runner does, so the template's snapshot captures it running. A microVM pause writes a full checkpoint of disk and memory, about three minutes for a desktop, so computer pauses get five minutes instead of 30 seconds. Signed-off-by: Jalil <reachjalil@gmail.com>
1 parent 3d7961f commit 9d79a97

5 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎ee/apps/headless-runner/README.md‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,47 @@ which side made it (the `computer_sides` table):
460460

461461
Gateway computers share the runner's scope, pause, keep-days, persistence and
462462
desktop settings, and ask for Daytona's size (2 CPUs, 4 GiB, 8 GiB disk). They
463-
are paused when idle only where every enabled gateway pool can pause or stop
464-
(pools that can only stop are stopped, keeping the disk), which the adapter
465-
reads from Differently as it creates them. Elsewhere (for
463+
are paused when idle only where every pool the gateway lets the runner's key
464+
use can pause or stop (pools that can only stop are stopped, keeping the disk),
465+
which the adapter reads from Differently as it creates them. Elsewhere (for
466466
example OpenSandbox on Kubernetes without a snapshot registry) an idle computer
467467
runs until Differently's TTL removes it. With only a gateway configured,
468468
sessions without the setting get no computer. The desktop viewer needs a gateway pool that publishes port
469-
URLs (Freestyle or Daytona). Differently's
469+
URLs: Freestyle, Daytona, or OpenSandbox behind its ingress gateway, Fast
470+
Sandbox microVMs included. Differently's
470471
[OpenWork guide](https://github.com/different-ai/differently/blob/pre-release/docs/guides/openwork.md)
471472
covers the server side.
472473

474+
#### Fast Sandbox microVMs
475+
476+
OpenSandbox's Fast Sandbox runs each computer as a Firecracker microVM restored
477+
from a template's memory snapshot. The template is built from
478+
`Dockerfile.desktop-fast-sandbox`, a layer on the desktop image:
479+
480+
```sh
481+
docker build -f ee/packages/headless-computer/Dockerfile.desktop -t <registry>/workbot-desktop:<tag> .
482+
docker build -f ee/packages/headless-computer/Dockerfile.desktop-fast-sandbox \
483+
--build-arg DESKTOP_IMAGE=<registry>/workbot-desktop:<tag> -t <registry>/workbot-desktop-fast-sandbox:<tag> ee/packages/headless-computer
484+
```
485+
486+
Create the template (OpenSandbox `POST /v1/templates`) with 2 CPUs, `4Gi` memory
487+
and `8Gi` disk, `entrypoint: ["/usr/local/sbin/fast-sandbox-guest-init", "tail",
488+
"-f", "/dev/null"]`, and a readiness probe that waits for the desktop:
489+
`cmd://printf '{}' | /usr/local/sbin/openwork-desktop-bridge call /health`. The
490+
entrypoint starts the desktop as the runner does, so the snapshot captures it
491+
running, as the Freestyle snapshot does; it also fixes `/run`'s mode and keeps
492+
the clock with chrony. Point the Differently template's image for that pool at the
493+
template's id.
494+
495+
The guest kernel must have nftables' `inet` family, `reject` and the owner match
496+
built in, or the desktop's egress guard cannot load and the desktop refuses to
497+
start; OpenSandbox 1.2.0's stock kernel has none of them (Differently's
498+
OpenSandbox pools guide lists the options). Measured on Azure
499+
(`Standard_D16s_v6`, nested KVM): a computer is running in about 2 s once the
500+
template is on the node; pause, which writes a full checkpoint of disk and
501+
memory, takes about three minutes and resume about a second, with processes and
502+
open windows kept.
503+
473504
Opt-in verification, creating and cleaning one real computer:
474505

475506
```sh
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Workbot's desktop as an OpenSandbox Fast Sandbox (Firecracker) template, for
2+
# computers placed through a sandbox gateway (Differently). Build and push
3+
# Dockerfile.desktop first, then this on top of it:
4+
# docker build -f ee/packages/headless-computer/Dockerfile.desktop-fast-sandbox \
5+
# --build-arg DESKTOP_IMAGE=<registry>/workbot-desktop:<tag> ee/packages/headless-computer
6+
# The template is created from this image with scripts/fast-sandbox-guest-init.sh
7+
# as its entrypoint; see the runner README's sandbox gateway section.
8+
ARG DESKTOP_IMAGE
9+
FROM ${DESKTOP_IMAGE}
10+
USER root
11+
# Fast Sandbox's guest init needs nc; procps, tini and python3 complete
12+
# OpenSandbox's template image set, and chrony keeps the clock after restores.
13+
RUN apt-get update && apt-get install -y --no-install-recommends \
14+
netcat-openbsd procps curl ca-certificates tini python3 chrony \
15+
&& rm -rf /var/lib/apt/lists/*
16+
# Every restore jumps the clock, so step on any offset over a second.
17+
RUN printf 'server time.cloudflare.com iburst minpoll 4 maxpoll 5\nserver time.google.com iburst minpoll 4 maxpoll 5\nmakestep 1 -1\nrtcsync\ndriftfile /var/lib/chrony/drift\n' > /etc/chrony/fast-sandbox.conf
18+
COPY scripts/fast-sandbox-guest-init.sh /usr/local/sbin/fast-sandbox-guest-init
19+
RUN chmod 0755 /usr/local/sbin/fast-sandbox-guest-init
20+
USER ubuntu
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/sh
2+
# Entrypoint of Workbot's Fast Sandbox template. It runs as root while the
3+
# template is built, before its memory snapshot, so what it starts is already
4+
# running in every computer restored from the template.
5+
# Fast Sandbox's guest init mounts /run world-writable, which the desktop's
6+
# start script refuses.
7+
chmod 0755 /run
8+
# The guest is named "sandbox" with no hosts entry, so sudo cannot resolve it.
9+
grep -q '[[:space:]]sandbox$' /etc/hosts || echo '127.0.1.1 sandbox' >> /etc/hosts
10+
# A restored snapshot keeps the time it was written; chrony steps it from NTP.
11+
mkdir -p /run/chrony /var/lib/chrony
12+
chronyd -f /etc/chrony/fast-sandbox.conf || echo "chronyd did not start" >&2
13+
# Start the desktop exactly as the runner does, so the snapshot captures it
14+
# running, as the Freestyle desktop snapshot does.
15+
desktop=/usr/local/share/openwork-desktop
16+
/usr/local/sbin/openwork-desktop-bridge start "$(cat "$desktop/image.version")" "$(cat "$desktop/guest.version")" \
17+
> /var/log/fast-sandbox-desktop-start.log 2>&1 &
18+
exec "$@"

‎ee/packages/headless-computer/scripts/prove-computer.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** Opt-in real computer proof. One disposable conversation, commands + Files + look + wake, cleanup in finally. */
22
import assert from "node:assert/strict"
33
import { randomUUID } from "node:crypto"
4-
import { Computers } from "../src/computer.js"
4+
import { Computers, PAUSE_TIMEOUT_MS } from "../src/computer.js"
55
import { createComputerProvider, createGatewayProvider } from "../src/providers.js"
66
import type { ComputerFile, ComputerFiles } from "../src/types.js"
77

@@ -51,8 +51,8 @@ try {
5151
const box = await computers.vm(session)
5252
// Pools that can neither pause nor stop keep idle computers running, so there is no resume to prove.
5353
const idles = provider.describe().stopResume
54-
if (idles && provider.blocks?.pause) await provider.blocks.pause(box, { timeoutMs: 30000 })
55-
else if (idles) await provider.stop(box, { timeoutMs: 30000 })
54+
if (idles && provider.blocks?.pause) await provider.blocks.pause(box, { timeoutMs: PAUSE_TIMEOUT_MS })
55+
else if (idles) await provider.stop(box, { timeoutMs: PAUSE_TIMEOUT_MS })
5656
const resumed = await computers.bash(session, "cat /workspace/out/proof.txt", 30)
5757
assert.equal(resumed.isError, false, resumed.output)
5858
assert.match(resumed.output, /hello from shared computer/)

‎ee/packages/headless-computer/src/computer.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ function computerBlocks(provider: SandboxProvider) { return withBlocks(provider,
4040
const GUEST_PATH = "/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
4141
/** The longest a single command may run; Freestyle's exec limit. Longer work goes through `background`. */
4242
const MAX_COMMAND_SECONDS = 300
43+
/** A microVM pause writes a full checkpoint of disk and memory: about three minutes for a desktop on Fast Sandbox. */
44+
export const PAUSE_TIMEOUT_MS = 5 * 60_000
4345
const DEFAULT_COMMAND_SECONDS = 120
4446
/** Each of stdout and stderr is cut to its last this-many characters: errors and results are at the end. */
4547
const OUTPUT_TAIL_CHARS = 30_000
@@ -204,7 +206,7 @@ export class Computers {
204206
const box = existing ?? await this.vm(sessionId)
205207
if (action === "pause") {
206208
if (!this.provider.blocks?.pause) throw new Error("This computer does not support pause.")
207-
await this.provider.blocks.pause(box, { timeoutMs: 30_000 })
209+
await this.provider.blocks.pause(box, { timeoutMs: PAUSE_TIMEOUT_MS })
208210
} else if (action === "continue") {
209211
await this.desktop.call(box, "/continue", {})
210212
} else await this.startDesktop(sessionId, box)
@@ -353,7 +355,7 @@ export class Computers {
353355
if (!this.working.has(sessionId) && await this.staleImage(sessionId, vm)) vm = await this.replace(sessionId, vm, { required: false }) ?? vm
354356
// A provider that can neither pause nor stop keeps the computer running until its own expiry.
355357
if (!this.provider.describe().stopResume) return this.options.pauses?.clear(sessionId)
356-
await (this.provider.blocks?.pause ? this.provider.blocks.pause(vm, { timeoutMs: 30_000 }) : this.provider.stop(vm, { timeoutMs: 30_000 }))
358+
await (this.provider.blocks?.pause ? this.provider.blocks.pause(vm, { timeoutMs: PAUSE_TIMEOUT_MS }) : this.provider.stop(vm, { timeoutMs: PAUSE_TIMEOUT_MS }))
357359
this.options.pauses?.clear(sessionId)
358360
} catch {
359361
// Not fatal: tried again after another idle period, and Freestyle's own idle timeout pauses it later.

0 commit comments

Comments
 (0)