Skip to content

Development guide

This page tells you how to build boxfork, how to run it on your computer and how to find a defect. It is for a person who wants to change the code.

Read architecture.md first for the parts of the system. Read ../CONTRIBUTING.md for the rules of a change.

Tool Version Used for Source of the version
Go 1.26 or later The daemon, the helper, the CLI, all Go tests go.mod
make No special version All targets Makefile
Python 3.9 or later The Python SDK, its tests, make docs-check sdk/python/pyproject.toml
Bun No version is recorded The tests of the TypeScript SDK Makefile
sh A POSIX shell The tests of the installer scripts/install_test.sh
dash Optional A second run of the tests of the installer Makefile
git No special version make docs-check uses git ls-files Makefile
tar, awk, and sha256sum or shasum No special version make dist Makefile
A container engine Docker or Podman To run the daemon. The tests do not use it. internal/engine/docker.go

The Go code uses only the standard library. The Python SDK uses only the standard library. You install no Go module and no Python package.

The tests of the TypeScript SDK use packages from npm. sdk/typescript/bun.lock locks their versions. When sdk/typescript/node_modules does not exist, make test runs bun install --frozen-lockfile first. This first run needs the network.

The code of the helper and the Go tests use system calls of Unix. Do the work on Linux or on macOS.

  1. Clone the repository and go to its directory.
  2. Run make build.
  3. Make sure that the file bin/boxfork exists.
  4. Run ./bin/boxfork version. A build without VERSION prints dev.

make build does two things in this order:

  1. The target guest compiles the helper for linux/amd64 and for linux/arm64. It writes the two files to internal/guestbin/bin/.
  2. The target build compiles the daemon to bin/boxfork.

The order is important. internal/guestbin/guestbin.go embeds the two helper files in the daemon with go:embed. The daemon copies one of them into each sandbox as /.boxfork/guest. Thus a change to the helper needs make build, not only go build.

If you compile the daemon without the helper, the daemon starts. Then each create fails with this message:

the sandbox helper for linux/arm64 is not built into this binary: run `make guest` and rebuild

To set the version that the binary reports, give VERSION:

Terminal window
make build VERSION=0.1.0
Target What it does
make or make all The same as make build.
make guest Compiles the helper for linux/amd64 and linux/arm64 into internal/guestbin/bin/, with CGO_ENABLED=0.
make build Runs guest, then compiles the daemon for your computer to bin/boxfork.
make dist Runs guest, then makes a release in dist/. See “Make a release” below.
make test Runs test-go, test-python, test-typescript and test-install.
make test-go Runs guest, then go vet ./..., then go test -race -count=1 ./....
make testserver Compiles bin/boxfork-testserver: the real HTTP API on the test engine.
make test-python Runs testserver, then the tests of the Python SDK with python3 -W error -m unittest.
make test-typescript Runs testserver, then bun run typecheck and bun test in sdk/typescript.
make test-install Runs scripts/install_test.sh with sh, and also with dash if dash is installed.
make docs-check Runs scripts/ste_check.py on each Markdown file that git knows, but not on sdk/typescript/.
make clean Removes bin/, dist/ and the helper files in internal/guestbin/bin/.

make docs-check reports errors only. To see the warnings for one page, run the checker directly:

Terminal window
python3 -I scripts/ste_check.py --warnings docs/development.md

Caution: Use one daemon for one engine. Do not start a second daemon on an engine that a daemon already uses. At its start, a daemon removes each sandbox container that its own state file does not list. Thus the second daemon removes the sandboxes of the first daemon.

Caution: Use a development daemon only on an engine where you can lose the sandboxes. The daemon removes only containers that have the label boxfork.managed=true and a name that starts with boxfork-. It does not remove other containers.

The daemon talks to the engine through the Docker Engine API. NewDocker in internal/engine/docker.go selects the address in this order:

  1. The value of the flag --docker-host.
  2. The value of the environment variable DOCKER_HOST.
  3. The default unix:///var/run/docker.sock.

The address must start with unix:// or with tcp://. The daemon does not read the contexts of the docker command.

  1. Make sure that Docker runs. The command docker info must give an answer.
  2. Run make build.
  3. Run ./bin/boxfork serve.
  4. Copy the API key that the daemon prints. The daemon shows the key only at the first start.
  5. Keep this terminal open. The daemon writes its log there.

If the socket of your Docker is not /var/run/docker.sock, give the address:

Terminal window
./bin/boxfork serve --docker-host unix:///path/to/docker.sock

Podman gives an API that is compatible with the Docker Engine API.

  1. Start the Podman machine: podman machine start.
  2. Read the path of the socket: podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}'.
  3. Set the variable: export DOCKER_HOST=unix://<the path from step 2>.
  4. Run ./bin/boxfork serve.

With Podman, expect these differences. They come from the engine, not from a defect in the daemon.

  • The isolation is container. Podman lists a runtime with the name runsc but runs the container with a different runtime. The daemon sees this in its trial container and does not report gVisor.
  • The daemon warns that the engine does not block traffic between sandboxes. Podman drops the network option that blocks it.
  • The firewall rules for the metadata service use the DOCKER-USER chain of Docker. With Podman the daemon cannot set them.

internals/engine.md gives the differences between the two engines.

serve in cmd/boxfork/main.go does these steps. A failure in a step with “stops” stops the daemon.

  1. It reads the flags and the TLS files. A flag with a value that is not permitted stops the daemon.
  2. It connects to the engine. No answer stops the daemon.
  3. It selects the isolation. If the engine lists a runsc runtime, it starts a trial container with that runtime.
  4. It reads the GPUs of the host with nvidia-smi. A missing nvidia-smi means no GPUs.
  5. It reads the API key, or it makes one at the first start.
  6. It loads the state file and compares the records with the containers of the engine.
  7. It makes the network boxfork and reads back if the network isolates the sandboxes.
  8. It sets the firewall rules for the metadata service, where it can.
  9. It does a trial snapshot. A failure switches off snapshot and fork, with a warning.
  10. It starts the sweep and the HTTP server.

The trial containers of steps 3 and 9 use the default image, ubuntu:24.04 unless you set --default-image. The engine pulls the image if it does not have it. Thus the first start needs access to the registry and takes more time.

The last line of a good start is boxfork is serving, with the address and the results of the steps:

time=<time> level=INFO msg="boxfork is serving" address=127.0.0.1:7070 https=false isolation=container network_isolated=true metadata_blocked=false snapshots=true gpus=0 sandboxes=0 data_dir=/home/you/.boxfork

DefaultDataDir in internal/client/client.go selects the data directory:

  1. The value of BOXFORK_DATA_DIR, if it is set.
  2. /var/lib/boxfork, if the user is root.
  3. .boxfork in the home directory of the user.

The flag --data-dir of serve replaces this value for the daemon. The directory contains two files: api-key and state.json.

When you stop the daemon with Ctrl-C, the sandboxes continue. The daemon reads them from the state file at the next start.

Open a second terminal. The daemon runs in the first terminal.

The CLI finds a daemon on the same computer without settings. It uses the address http://127.0.0.1:7070 and reads the API key from the file api-key in the data directory. For a different daemon, set BOXFORK_URL and BOXFORK_API_KEY, or use --url and --key.

  1. Show what the daemon found: ./bin/boxfork info.
  2. Create a sandbox: id=$(./bin/boxfork create --image ubuntu:24.04 --timeout 10m).
  3. Run a command through the shell of the sandbox: ./bin/boxfork exec "$id" 'echo hello && uname -a'.
  4. Run a command with separate arguments: ./bin/boxfork exec "$id" -- ls -l /etc.
  5. Copy a file into the sandbox: ./bin/boxfork cp README.md "$id":/tmp/README.md.
  6. List the sandboxes: ./bin/boxfork ls.
  7. Kill the sandbox: ./bin/boxfork kill "$id".

The exit status of exec is the exit status of the command. Add --json to create, ls or info to get the result as JSON. Run ./bin/boxfork help for the list of commands. Each command accepts -h.

reference/cli.md describes all commands. reference/http-api.md describes the API that the CLI calls.

Run all tests before you send a change:

Terminal window
make test

The tests do not use a container engine. testing.md describes each kind of test.

Terminal window
go test -race -count=1 -run 'TestAConnectionNobodyUsesIsClosed$' ./internal/sandbox/

You do not need make guest for go test. The module compiles without the helper files, and the tests give the manager a stand-in for the helper file. make test-go runs the target guest first, and then go vet.

To run the tests of one package, give only the path:

Terminal window
go test -race -count=1 ./internal/api/
Terminal window
make testserver
cd sdk/python
python3 -W error -m unittest discover -s tests -v -k test_create_and_describe

The tests start bin/boxfork-testserver. Set BOXFORK_TESTSERVER to use a different file.

Terminal window
sh scripts/install_test.sh
TEST_SH=dash sh scripts/install_test.sh

The script has no option to run one test. It prints ok or FAIL for each test.

The package internal/engine/enginetest is an engine that exists only in memory. It implements the Engine interface from internal/engine/engine.go. Most Go tests use it, and so does bin/boxfork-testserver.

  • A container of the test engine is a record in a map. It has no isolation.
  • When the manager runs the helper in a container, the test engine calls guest.Main in the test process. Thus a test uses the real protocol between the daemon and the helper.
  • A command “in” a sandbox is a real process on your computer. A path in a file operation is a path on your computer.
  • The test engine answers some helper commands itself: gpus, procs, dns and own. The real commands read or change the system that they run on.
  • A test can make an operation of the engine fail. A test can also replace the helper for one call.

Caution: Do not send a request to bin/boxfork-testserver from a program that is not a test. Its commands run on your computer as your user.

testing.md gives the controls of the test engine.

cmd/
boxfork/ the daemon, the CLI, the MCP command
boxfork-guest/ main function of the helper
boxfork-testserver/ the real API on the test engine
internal/
api/ HTTP API
apitest/ a daemon on the test engine, for tests of clients
client/ Go client of the HTTP API
engine/ Engine interface, client of the Docker Engine API
enginetest/ the test engine
gpu/ GPU allocator
guest/ the helper and its protocol
guestbin/ the helper programs, embedded in the daemon
hostfw/ firewall rules for the metadata service
mcp/ MCP server and session rules
sandbox/ the manager
tools/ the tools for a model
scripts/
install.sh the installer
cloud-startup.sh the first-boot script for a cloud machine
install_test.sh tests of the two scripts above
proof/ proof scripts for a real engine
ste_check.py the checker for the documentation
sdk/
python/ the Python SDK and its tests
typescript/ the TypeScript SDK and its tests
docs/ the documentation
Dockerfile the daemon as a container image
Makefile

The files in internal/sandbox/ divide the manager by topic:

File Topic Page
manager.go Create, kill, pause, resume, timeouts, sweep, the start of the daemon internals/manager.md
run.go, files.go Commands and file operations internals/commands-and-files.md
kept.go The kept connection internals/kept-connection.md
snapshot.go Snapshots and forks internals/snapshots-and-forks.md
pool.go Pools internals/pools.md
suspend.go Suspend and wake internals/suspend.md
volume.go Volumes internals/volumes.md
state.go The state file internals/manager.md

These directories are not in git: bin/, dist/, the helper files in internal/guestbin/bin/, node_modules/.

A release is a directory with the daemon for Linux, the installer and the checksums.

  1. Run make dist VERSION=0.1.0.
  2. Read the list of files that the target prints at its end.

dist/ then contains these files:

File Content
boxfork-linux-amd64.tar.gz, boxfork-linux-arm64.tar.gz The daemon and LICENSE, for each chip type.
install.sh A copy of scripts/install.sh.
cloud-startup.sh A copy of scripts/cloud-startup.sh with the three checksums of this release.
*.sha256 The SHA-256 of each archive and of install.sh.
LICENSE The licence.

make dist deletes the old dist/ first. It compiles with CGO_ENABLED=0, thus you can make a Linux release on a Mac. The archives have root as the owner of each file and no Mac attributes.

If you know the https address where you put the release, give RELEASE:

Terminal window
make dist VERSION=0.1.0 RELEASE=https://example.com/boxfork/0.1.0

The target then writes the address into dist/cloud-startup.sh. cloud-startup.sh has no checksum file. A person usually edits it before use.

install.md tells an operator how to use the release.

scripts/install.sh changes a machine: it installs Docker, gVisor and a service. Do not run it on your computer to see what it does.

Use its tests:

Terminal window
make test-install

scripts/install_test.sh installs nothing. It works in this way:

  • For each test it makes an empty “machine” in a temporary directory.
  • The installer gets BOXFORK_INSTALL_ROOT. It puts this directory before each path that it writes.
  • The commands docker, systemctl, runsc, nvidia-smi, apt-get, curl and uname are stand-ins. Each stand-in writes its arguments to a file with the name calls.
  • The PATH of the installer contains only the stand-ins and a fixed list of basic tools.
  • A test then reads the output, the exit status, the files below the root and the file calls.

To add a test, copy a test that is near to your case. A test has this form:

Terminal window
t "an unknown option"
usual
install --frm "$M/boxfork"
refused; says "unknown option --frm"
done_t

usual makes a machine with Docker, systemd, gVisor and an boxfork binary. install runs the installer. The lines after it are the checks.

For a run of the real installer, use a virtual machine that you can delete.

The daemon writes its log to standard error, one line for each event, as text with keys and values. The service that the installer makes sends the log to the journal: journalctl -u boxfork.

The level of the log is Info. The daemon has no flag to show the Debug lines.

Line Meaning
sandbox created A create finished. The line gives the id, the image, the number of GPUs and the isolation. It also gives the pool, if the sandbox came from a pool.
sandbox removed A client killed a sandbox.
sandbox timed out The sweep removed a sandbox after its timeout.
sandbox ended on its own The container stopped without a request. oom_killed=true means that the kernel stopped it for memory.
removing a leftover sandbox container At its start the daemon found a container that the state file does not list.
dropping a sandbox whose container is gone At its start the daemon found a record without a container that runs.
could not start a sandbox for a pool; it is tried again every half minute A pool cannot fill. The line gives the error.
request failed A request ended with HTTP status 500. The line gives the method, the path and the error.

The state file is state.json in the data directory. It is JSON with indentation, thus you can read it directly. stateFile in internal/sandbox/state.go is its format.

Key Content
version The number 1.
sandboxes One record for each sandbox, also for each waiting sandbox and each suspended sandbox.
snapshots One record for each snapshot, also for the temporary snapshots of forks and of suspended sandboxes.
pools One record for each pool.
discarded The ids of snapshots whose files the daemon must delete from the engine.

A sandbox record has the public fields of the API and these internal fields:

Key Content
container_id The id of the container. It is empty for a suspended sandbox.
pool The id of the pool, for a waiting sandbox.
fork The id of the fork, while the fork makes the copy.
setup The mark of the daemon settings under which a waiting sandbox started.
remaining_ns The time that a paused sandbox has left.
disk The temporary snapshot that contains the files of a suspended sandbox.
gpu_request The GPUs that the sandbox gets when it wakes.
layers The number of image layers below the container.

The state file does not contain environment variables. It does not contain the volumes: the engine keeps them.

Caution: Do not edit the state file while the daemon runs. The daemon keeps the records in memory and writes the file again at the next change.

Caution: Do not start the daemon with an empty data directory on an engine that has sandboxes. The daemon removes their containers.

All names are constants in internal/sandbox and internal/hostfw.

Object Name Labels
Container of a sandbox boxfork-<sandbox id> boxfork.managed=true, boxfork.id=<sandbox id>, boxfork.gpus=<UUIDs of its GPUs>
Trial container for gVisor boxfork-probe-<random> boxfork.managed=true, boxfork.id=probe
Trial container for snapshots boxfork-check-<random> boxfork.managed=true, boxfork.id=check
Network boxfork boxfork.managed=true
Volume boxfork-vol-<volume name> boxfork.managed=true, boxfork.volume=<volume name>
Image of a snapshot boxfork-snapshot:<snapshot id without "snap_"> The labels of the container that it came from
Firewall chain BOXFORK-METADATA None

These commands show the objects on Docker:

Terminal window
docker ps -a --filter label=boxfork.managed=true
docker ps -q --filter label=boxfork.id=<sandbox id>
docker volume ls --filter label=boxfork.managed=true
docker images boxfork-snapshot
docker network inspect boxfork
sudo iptables -S BOXFORK-METADATA

The helper is the file /.boxfork/guest in each sandbox. The first process is /.boxfork/guest init. While a kept connection is open, a second process /.boxfork/guest serve runs.

Terminal window
docker exec <container> /.boxfork/guest version
docker top <container>

The first command prints the version of the helper. Version in internal/guest/main.go is the value.

  1. Write the smallest sequence of API calls that shows the defect.
  2. Write the sequence as a Go test on the test engine, if the test engine can show it.
  3. If only a real engine shows the defect, write the steps as a shell script. Use scripts/proof as a model.
  4. Read the log of the daemon for the id of the sandbox.
  5. Compare the record in the state file with the container on the engine.
  6. Correct the defect. Keep the test.
Problem Cause Solution
A create fails with run make guest and rebuild in the message You compiled the daemon without the helper. Run make build.
make test-python cannot start the test server bin/boxfork-testserver is missing or old. Run make testserver. The target test-python does this for you.
make test-typescript fails before the first test The packages are missing or old. make test installs them only when node_modules does not exist. Run cd sdk/typescript && bun install --frozen-lockfile.
The daemon stops with cannot reach the container engine The engine does not run, or the socket has a different path. See “How the daemon finds the engine” above.
The CLI says no API key The daemon uses a different data directory, or the daemon is not local. Set BOXFORK_API_KEY, or set BOXFORK_DATA_DIR to the directory of the daemon.
Your sandboxes went when you started a second daemon Two daemons used one engine. Use one daemon for one engine.
A change to the helper has no effect The sandbox has the helper from the time of its create. Run make build, start the daemon again and create a new sandbox.

troubleshooting.md has the problems of a daemon that runs, with the text of each error.

  • Nobody ran the daemon on a machine with an NVIDIA GPU or on x86_64.
  • The runs on a real engine were on Docker with gVisor on arm64, in a nested virtual machine, and on Podman.
  • No CI service runs the tests. Each person runs make test on a local machine.
  • This page does not record a Bun version, because the repository does not record one.