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.
Required tools
Section titled “Required tools”| 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.
- Clone the repository and go to its directory.
- Run
make build. - Make sure that the file
bin/boxforkexists. - Run
./bin/boxfork version. A build withoutVERSIONprintsdev.
make build does two things in this order:
- The target
guestcompiles the helper forlinux/amd64and forlinux/arm64. It writes the two files tointernal/guestbin/bin/. - The target
buildcompiles the daemon tobin/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 rebuildTo set the version that the binary reports, give VERSION:
make build VERSION=0.1.0Makefile targets
Section titled “Makefile targets”| 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:
python3 -I scripts/ste_check.py --warnings docs/development.mdRun the daemon on your computer
Section titled “Run the daemon on your computer”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.
How the daemon finds the engine
Section titled “How the daemon finds the engine”The daemon talks to the engine through the Docker Engine API.
NewDocker in internal/engine/docker.go selects the address in this order:
- The value of the flag
--docker-host. - The value of the environment variable
DOCKER_HOST. - 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.
With Docker
Section titled “With Docker”- Make sure that Docker runs. The command
docker infomust give an answer. - Run
make build. - Run
./bin/boxfork serve. - Copy the API key that the daemon prints. The daemon shows the key only at the first start.
- 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:
./bin/boxfork serve --docker-host unix:///path/to/docker.sockWith Podman
Section titled “With Podman”Podman gives an API that is compatible with the Docker Engine API.
- Start the Podman machine:
podman machine start. - Read the path of the socket:
podman machine inspect --format '{{.ConnectionInfo.PodmanSocket.Path}}'. - Set the variable:
export DOCKER_HOST=unix://<the path from step 2>. - 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 namerunscbut 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-USERchain of Docker. With Podman the daemon cannot set them.
internals/engine.md gives the differences between the two engines.
What the daemon does at its start
Section titled “What the daemon does at its start”serve in cmd/boxfork/main.go does these steps. A failure in a step with “stops” stops the daemon.
- It reads the flags and the TLS files. A flag with a value that is not permitted stops the daemon.
- It connects to the engine. No answer stops the daemon.
- It selects the isolation. If the engine lists a
runscruntime, it starts a trial container with that runtime. - It reads the GPUs of the host with
nvidia-smi. A missingnvidia-smimeans no GPUs. - It reads the API key, or it makes one at the first start.
- It loads the state file and compares the records with the containers of the engine.
- It makes the network
boxforkand reads back if the network isolates the sandboxes. - It sets the firewall rules for the metadata service, where it can.
- It does a trial snapshot. A failure switches off snapshot and fork, with a warning.
- 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/.boxforkWhere the daemon keeps its data
Section titled “Where the daemon keeps its data”DefaultDataDir in internal/client/client.go selects the data directory:
- The value of
BOXFORK_DATA_DIR, if it is set. /var/lib/boxfork, if the user is root..boxforkin 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.
A first session with the CLI
Section titled “A first session with the CLI”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.
- Show what the daemon found:
./bin/boxfork info. - Create a sandbox:
id=$(./bin/boxfork create --image ubuntu:24.04 --timeout 10m). - Run a command through the shell of the sandbox:
./bin/boxfork exec "$id" 'echo hello && uname -a'. - Run a command with separate arguments:
./bin/boxfork exec "$id" -- ls -l /etc. - Copy a file into the sandbox:
./bin/boxfork cp README.md "$id":/tmp/README.md. - List the sandboxes:
./bin/boxfork ls. - 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 the tests
Section titled “Run the tests”Run all tests before you send a change:
make testThe tests do not use a container engine. testing.md describes each kind of test.
Run one Go test
Section titled “Run one Go test”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:
go test -race -count=1 ./internal/api/Run one test of the Python SDK
Section titled “Run one test of the Python SDK”make testservercd sdk/pythonpython3 -W error -m unittest discover -s tests -v -k test_create_and_describeThe tests start bin/boxfork-testserver.
Set BOXFORK_TESTSERVER to use a different file.
Run the tests of the installer
Section titled “Run the tests of the installer”sh scripts/install_test.shTEST_SH=dash sh scripts/install_test.shThe script has no option to run one test. It prints ok or FAIL for each test.
The test engine
Section titled “The test engine”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.Mainin 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,dnsandown. 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.
Layout of the repository
Section titled “Layout of the repository”cmd/ boxfork/ the daemon, the CLI, the MCP command boxfork-guest/ main function of the helper boxfork-testserver/ the real API on the test engineinternal/ 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 modelscripts/ 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 documentationsdk/ python/ the Python SDK and its tests typescript/ the TypeScript SDK and its testsdocs/ the documentationDockerfile the daemon as a container imageMakefileThe 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/.
Make a release
Section titled “Make a release”A release is a directory with the daemon for Linux, the installer and the checksums.
- Run
make dist VERSION=0.1.0. - 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:
make dist VERSION=0.1.0 RELEASE=https://example.com/boxfork/0.1.0The 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.
Try the installer safely
Section titled “Try the installer safely”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:
make test-installscripts/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,curlandunameare stand-ins. Each stand-in writes its arguments to a file with the namecalls. - The
PATHof 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:
t "an unknown option"usualinstall --frm "$M/boxfork"refused; says "unknown option --frm"done_tusual 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 log of the daemon
Section titled “The log of the daemon”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
Section titled “The state file”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.
What the daemon makes on the engine
Section titled “What the daemon makes on the engine”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:
docker ps -a --filter label=boxfork.managed=truedocker ps -q --filter label=boxfork.id=<sandbox id>docker volume ls --filter label=boxfork.managed=truedocker images boxfork-snapshotdocker network inspect boxforksudo iptables -S BOXFORK-METADATALook into a sandbox from the host
Section titled “Look into a sandbox from the host”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.
docker exec <container> /.boxfork/guest versiondocker top <container>The first command prints the version of the helper. Version in internal/guest/main.go is the value.
A procedure for a defect
Section titled “A procedure for a defect”- Write the smallest sequence of API calls that shows the defect.
- Write the sequence as a Go test on the test engine, if the test engine can show it.
- If only a real engine shows the defect, write the steps as a shell script. Use scripts/proof as a model.
- Read the log of the daemon for the id of the sandbox.
- Compare the record in the state file with the container on the engine.
- Correct the defect. Keep the test.
Common problems
Section titled “Common problems”| 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.
Not proven
Section titled “Not proven”- 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 teston a local machine. - This page does not record a Bun version, because the repository does not record one.