Skip to content

feat(cli): declare the Incus sandbox backend of a managed deployment - #5015

Merged
yannickmonney merged 14 commits into
mainfrom
feat/cli-incus-sandbox-shape
Oct 19, 2026
Merged

yannickmonney merged 14 commits into
mainfrom
feat/cli-incus-sandbox-shape

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A managed deployment can now declare "sandbox": { "backend": "incus", "address": "<IPv4>", "egressProxy": "http://<IPv4>:<port>" }. tale deploy prepare then turns the release's own compose.yml into a runtime in which the sandbox service mounts no Docker socket and no session root, only the three Incus PEM files, and deploy --bundle checks and applies it. An environment switches from the Docker shape to the Incus shape and back in place; the outgoing spawner is drained first, and the switch is refused while it still holds sessions. A spec without the field prepares and applies as before. compose.yml's values, the workspace generator, services/sandbox/ and every workflow are untouched.

What changes

The Incus shape differs from the Docker bundle prepareRuntime writes today in three services only; the other eight are byte-identical to it.

Service Docker shape Incus shape (sandbox declared)
sandbox volumes Docker socket, /var/lib/tale-sandbox, config-data ../sandbox-incus:/run/secrets/incus:ro, config-data
sandbox environment the source's, with the three image variables pinned the same minus SANDBOX_RUNTIME, SANDBOX_EGRESS_NETWORK, SANDBOX_HUB_PORT; SANDBOX_EGRESS_PROXY is the declared proxy; plus SANDBOX_BACKEND=incus, the image alias and source, and the three credential paths
backend-api / sandbox-llm-gateway ports [] ["<address>:3005:3005"] / ["<address>:8080:8080"]
  • Image identity. The alias is tale-sandbox-runtime- plus the first 32 hex digits of the runtime image digest (53 characters); the source is the digest reference itself. runtime.json records sandbox: { backend, address, egressProxy, image: { alias, source } } and readRuntimeBundle derives the image again and compares.
  • Reader. readRuntimeBundle holds each invariant that the bundle alone can show: the Incus mount and ports exist only in a bundle that records sandbox; with it, the volumes, the literals, the removed variables, the egress literal and the ports are exactly those above.
  • Source check. prepareRuntime refuses a source revision that does not list services/sandbox/src/backend/incus/incus-config.ts (select a revision from v0.5.91 or later), and a source whose sandbox environment lacks one of the four keys it rewrites.
  • Apply. Before the first Docker call, also in a dry run, the apply requires <stateDirectory>/sandbox-incus to be a real directory owned by root or the CLI's user, mode 0700, with a trusted ancestor chain, holding client.crt, client.key and server.crt as regular, single-link, owner-only, non-empty files of at most 2 000 000 bytes. It uses lstat only and never opens or prints a file. /var/lib/tale-sandbox is created for the Docker shape only. The managed .env loses the names of the shape it leaves: for the Incus shape the ten the CLI owns, for the Docker shape every SANDBOX_INCUS_* name and SANDBOX_BACKEND=incus that the specification does not declare.
  • Custody. At admission only, a retained sandbox container in the other known shape is accepted and reported; after up and in observeReadyState the bundle's own shape is checked exactly. A mixed set, an extra mount, a drifted ready runtime and the Incus mount on any other service are refused as before.
  • Switch. When custody finds the other shape, the apply latches the outgoing spawner's drain and reads its status before any image pull or runtime write. It refuses while sessions remain after the activation's 300 s budget, naming the count and the backend and never a session id, and refuses when the spawner is not running. Nothing is deleted. A dry run logs that the backend changes and execs into nothing. First deploys, replays of the ready bundle and changes inside one shape are not gated.
  • Declaration. sandbox is a strict optional field of the deployment specification. address (unicast IPv4) and egressProxy (http:, IPv4 host, explicit port 1..65535 but not 80, nothing else) may each be an {env} reference. A specification that declares sandbox may not name any of the ten owned variables in environment. The guard that the prepared runtime and the specification agree compares sandbox in both directions.
  • Shared pieces. The ancestor check of observationFile and validateClientExportDirectory now lives in private-path.ts, and the spawner control calls of the managed runtime (control, status, the drain loop) in runtime-spawner.ts. Both keep their messages and tests.
  • Docs. docs/{en,de,fr}/self-hosted/install/cli-install.md gain a section for the sandbox field, the credential directory, the switch and its drain; the three environment-reference.md rows and tools/cli/README.md say that a managed deployment that declares sandbox sets the Incus variables. The two header comments on the sandbox service (compose.yml, create-sandbox-service.ts) now say what the CLI does.

Dormancy

A specification without sandbox prepares the same bytes. A scratch script prepared the base commit's own compose.yml and proxy source (c9d5ea8a02) into a temporary repository with the test helper's fake Docker digests, once with the base CLI and once with this branch:

File SHA-256, base and branch
compose.yml b05835711e5b87c4812091c27c10cd8e6e26d4b79be2c8e6164cfc000bde7c7f
Caddyfile.production 098ccb12bd0f2987c9ee086653127941cd77cadb7733155d35d553e632be0f38
runtime.json 9a258a58f4863a53401801601fd5c435f62384a1d791500e0b562744465d5f06
.env (no SANDBOX_INCUS_*, no SANDBOX_BACKEND=incus before) fcb3591f854cda4d3567653eed9d2cdd58793cbce21bf6ae8dfafb7f23ca6527

The one effect on a specification without sandbox is the .env cleanup above. With SANDBOX_INCUS_ENDPOINT left in the previous .env, the base keeps it (d9ca2c3a25eb…) and this branch drops it (fcb3591f854c…). No existing declaration names either variable. Tests hold the boundary: SANDBOX_BACKEND=docker, SANDBOX_RUNTIME=sysbox and a declared SANDBOX_INCUS_ENDPOINT are kept byte for byte.

Checklist

  • bun run check passes (format, lint, typecheck, all tests), 49 of 49 turbo tasks on the branch tip. Details under Test plan.
  • bun run lint:sast passes (Opengrep): 476 rules on 5870 files, 0 findings.
  • Translations: N/A for message catalogs (no key added). The docs are translated, see below.
  • Docs updated in docs/{en,de,fr}/.
  • README.md / README.de.md / README.fr.md: N/A, no root README changes. tools/cli/README.md has one paragraph.

Test plan

Local, on the branch tip rebased on origin/main c9d5ea8a02 (no rebase was needed), bun 1.4.2:

  • bun run check: format, lint:conflicts, lint:links, then turbo run lint typecheck test test:ui over 49 tasks, all green. @tale/cli lint 0 warnings and 0 errors, typecheck clean, test 3489 pass, 35 skip, 0 fail (3524 tests in 192 files). @tale/docs 716 pass, @tale/platform test 90253 pass, test:ui 8377 pass. Under this Mac's load the first runs timed out or lost races in a few tests of @tale/sandbox, @tale/sandbox-runtime-daemon and @tale/platform; the diff has no path under services/ or packages/, each of those files passes alone, and the last whole run, with fewer parallel tasks, is green.
  • bunx knip exit 0, bun run lint:manual ok, commitlint over every commit and this title ok.
  • docker compose config (v5.3.0, no pull, no build) over the prepared Incus bundle laid out as the managed apply lays out a state directory: the sandbox bind source is <state>/sandbox-incus, read-only at /run/secrets/incus; backend-api publishes <address>:3005 and sandbox-llm-gateway <address>:8080. The Docker bundle keeps the socket, /var/lib/tale-sandbox and no published port.
  • A suite prepares the Incus bundle from this repository's own compose.yml, resolves its variables as Compose does, and runs the spawner's real loadConfig over it in a child process: the Incus backend boots with the declared proxy, the pinned alias and registry source and the three credential paths, with no ignored-setting warning and no runtime tier; dropping one owned variable shows the spawner's own refusal naming it. tools/cli/turbo.json and platform-docs.test.ts list the spawner modules it reads.
  • New suites: runtime-sandbox, runtime-sandbox-credentials, runtime-sandbox-spawner, runtime-apply-sandbox, runtime-spawner, private-path, plus cases in the prepare, apply, env, configuration, inputs, declaration-text and deployment apply tests. Tests that depend on a file mode, an owner or a child process run behind describe.skipIf(process.platform === 'win32') (private-path, runtime-sandbox-credentials, runtime-apply-sandbox, runtime-sandbox-spawner, parts of apply, inputs and runtime-configuration); the prepare, reader and specification tests have no platform assumption.

Not run, and not claimed:

  • The merge queue's jobs (E2E, Build, CLI, UI, Browser, backend integration) and the Windows CLI job, which run in the queue only. This PR's own checks (Format, Lint, Type check, Knip, Unit, Commitlint, Opengrep and the scope jobs) were all green on bc021cc3e4 when this was written.
  • That the spawner container starts with no socket and no session root on a real Docker host, that Docker publishes on the Docker host's address at boot, the node's reachability from the Docker host, that Incus pulls the digest reference through the node's registry configuration, a replaced PEM (the spawner reads it once at boot), and the switch with real sessions on both backends. These need a Docker host and an Incus node.

Ops follow-up

Nothing in this PR is used until a deployment declares sandbox. After a release contains it, the ops repository has to place the three PEM files in <stateDirectory>/sandbox-incus/ (mode 0600 in a 0700 directory, by the deployment automation and never by the CLI), give the Docker host an address the node can reach, open the node's API and the two published ports between the Docker host and the sandbox bridge, pin the new CLI, and add the sandbox block to the tale.json of the environments that move. Switching an environment is a later, deliberate step.

The observation file reader and the client export directory check each
walked a path's ancestors with their own copy of the rules. One helper,
untrustedAncestor, now holds them and both call it, keeping their own
messages.

The two walks differed in two places and the helper takes the stricter
of each. A sticky directory is trusted only when root owns it, where the
export check also accepted one the CLI's own account owned; the
filesystem root is examined, where the observation reader skipped it.
Every real layout is unaffected: the system temporary directory is a
root-owned sticky directory and the root is root-owned.
The calls that ask the spawner container to drain, read its status and
wait for its sessions to end lived inside the configuration activation.
A second caller needs the same calls, so they move to runtime-spawner.ts
without a change of behaviour. They keep the strict status shape, the
latch, and the wait of up to 60 reads within 300 seconds in steps of 5
seconds. The activation calls them and keeps its own messages and tests.
prepareRuntime takes a sandbox option, an address and an egress proxy,
and turns the release's own compose.yml into a bundle whose sandbox
service runs against an Incus node. The spawner mounts no Docker socket
and no session root, only the credentials directory ../sandbox-incus
read-only at /run/secrets/incus; it loses the three variables the
Docker shape sets, takes the declared egress proxy, and gets the six
literals that name the backend, the image alias and source, and the
three PEM paths. The backend API and the LLM gateway publish their
ports on the declared address. The other eight services are byte for
byte what the Docker bundle holds, and a call without the option writes
the same files as before.

The image alias is the first 32 digits of the runtime image digest, so
one alias names one image, and both alias and source are recorded in
runtime.json beside the address and the proxy. readRuntimeBundle derives
them again and holds the compose file to the record, using only what the
bundle holds. The Incus mount and the two port lists are accepted only
for a bundle that records the shape. Preparation refuses a revision that
lacks the spawner's Incus backend and a source whose sandbox service
lacks a variable the shape replaces.

Everything the shape needs to agree on is one module, runtime-sandbox.ts.
Nothing declares the shape yet.
Applying a bundle that records the Incus sandbox first holds the state
directory's sandbox-incus directory to the CLI's custody rules, before
the first Docker call and so also in a dry run: a real directory that
root or the CLI's account owns and only that account can enter, below
ancestors nobody else can replace, holding client.crt, client.key and
server.crt as private, single-link, non-empty regular files within the
runtime input limit. Only metadata is read and no message carries
content. A host without POSIX account ids is refused. The directory is
the one entry besides src and .tale that a fresh runtime of this shape
accepts, and the host workspace of sessions is created only for the
Docker shape.

Container custody at admission of a changing apply lets the sandbox
container still hold exactly the mounts of the other shape, the state a
switch between the Docker and the Incus shape starts from, and reports
that shape. The check after the start, the ready-state check, every
other service and every extra mount keep the exact rule.

The managed .env drops what the other shape left in it: the Incus shape
removes the ten variables it owns, the Docker shape removes the Incus
variables and a backend of incus, unless the declaration being applied
names them. An environment that names an owned variable is refused when
the bundle declares the Incus sandbox.
A session outlives its spawner process: the next process adopts it, but
only the backend that created it sees it, and the replaced container
leaves it running. When container custody finds the sandbox container in
the other shape, the apply now latches the outgoing spawner's drain and
reads its status through the managed path's own spawner control before
the runtime's images are pulled or any runtime file is written, and
refuses while sessions remain after the activation's budget. The one
earlier pull is the platform image of a release whose writer protocol is 2.

The refusal names the outgoing backend and the number of sessions, never
a session id. It says the spawner stops sessions itself after its linger
(SANDBOX_SESSION_MAX_LINGER_MS, 30 minutes by default), that the same
bundle can be applied again after that, and that restarting the spawner
lifts the drain. A spawner that is not running cannot say whether it
holds sessions, so that is refused too, with the way to abandon them.
Nothing is deleted. The gate runs before the receipt write of a pending
takeover as well, so a refusal leaves the state as it was.

A dry run, which every real apply makes first, logs one line that the
backend changes and that the apply will drain, and execs into nothing.
First deploys, replays of the ready bundle and changes inside one shape
are not gated.
A deployment specification may declare sandbox with backend incus, the
IPv4 address of the Docker host that the node's sessions dial for the
backend API and the LLM gateway, and the egress proxy. Address and proxy
are checked by the same rules the prepared runtime holds them to and may
each be an environment reference, resolved when the bundle is prepared
like the origin. A specification without the field accepts exactly what
it accepted before; with it, the environment may not name any variable
the shape sets itself.

prepare passes the declaration to the runtime preparation and logs one
step. The guard that the prepared runtime and the specification agree
now compares the backend, the address and the egress proxy as well as
the revision, platform and container prefix, in both directions, so a
runtime prepared with a sandbox is refused for a specification without
one and the reverse.
The sandbox environment of a prepared Incus bundle is only right if the
spawner accepts it. A suite now prepares the bundle from this
repository's own compose.yml, resolves its variables as Compose does
beside a representative specification, and runs the spawner's real
loadConfig over the result in a process of its own, so that nothing from
services/sandbox joins this workspace's type program.

It asserts that the Incus backend boots with the declared egress proxy,
the pinned image alias and registry source, and the three credential
paths under the mount point, and that the spawner reports no setting as
ignored and no runtime tier. It also shows the check can fail: a bundle
without the image, a credential path or the egress proxy is refused by
the spawner naming the variable, and one without the backend variable
runs the Docker backend and reports its Incus settings as ignored.

The spawner modules loadConfig imports are test inputs of the CLI in
turbo, and a test holds that list to the real imports, so an edit to one
of them re-runs the suites instead of replaying a cached verdict.
The install guide gains a section beside the additional-origins and
organization-creators ones for the sandbox field of a managed
deployment, in English, German and French: the three keys and the
values that are refused, the credentials directory with its three files
and the custody the apply holds them to, what the CLI sets, removes and
refuses in the sandbox service, the two ports published on the declared
address, what does not apply under Incus, and the switch between the
Docker and the Incus backend in both directions with its drain, its
refusal while sessions remain and the way to retry or to abandon them.

The environment reference rows for the backend, the image alias and
source, and the three credential paths say that a managed deployment
that declares sandbox sets them. The CLI readme gets one paragraph. The
two header comments that called the Incus shape an override of the
service now say what the CLI does. A test resolves the guide's example
declaration in every locale.
The post-start check of the sandbox mounts rejected a start that left the
Docker mounts in place, but the test kept only part of each mount, so it
could not tell the shape from a missing volume name. It now restores the
container's mounts exactly, shows that the retry of the same bundle
admits them and completes the switch, and adds the passing case beside
each refusal of an extra mount: the same change is admitted once the
extra mount is gone.
A release whose writer protocol is 2 makes the runtime pull its platform
image, up to 1800 s, before the backend switch drain ran. A refused
switch therefore pulled that image first, and the comment on the drain
claimed otherwise. The drain now runs directly after admission and the
planning of the environment files, which write nothing, and before
that pull and every write of the runtime.

The test prepares a protocol 2 release, runs it as a Docker runtime and
refuses the switch to Incus with a session left: no image is pulled and
no runtime file is written. It fails on the previous order.
A switch of the sandbox backend that the drain refuses has already
taken the deployment's recovery snapshot and recorded its bundle as
pending, because the drain runs inside the runtime apply. Two cases now
hold that state at the deployment level, against a ready Docker
deployment that still has two sessions:

The refusal leaves the ready receipt unchanged and the pending record
with its snapshot. The bundle that is ready is refused as a different
bundle, the switch bundle retries on the same snapshot, and once the
sessions end it completes without taking another.

Giving up the switch works as the install guide describes: the spawner
restarts, and a bundle of the backend kept that names the pending
bundle in supersedesPendingBundle takes it over with its snapshot.
The proxy URL is refused when the port is missing or is 80, because a
URL parser drops the default port of http and the written value would
not round-trip. The refusal said only "with an explicit port", which a
proxy URL ending in :80 satisfies. The schema and preparation messages
and the install guide in all three languages now say the port is 1 to
65535 and not 80, and a test holds the schema message.
A refused backend switch leaves the runtime untouched, but the
deployment has already taken its recovery snapshot and recorded the
bundle as pending. The install guide in all three languages now says
that only the same bundle applies again and takes no second snapshot,
that any other bundle is refused until it names the pending bundle in
supersedesPendingBundle, and that giving up the switch means restarting
the spawner and applying such a bundle of the backend kept.

It also says that Compose replaces the sandbox, backend API and model
gateway containers, and that only the drain stops and deletes nothing.

Every apply of a Docker-backend deployment removes SANDBOX_BACKEND=incus
and the SANDBOX_INCUS_ variables the specification does not declare from
the managed environment file, switch or not. The guide and the CLI
README no longer promise that a deployment without a sandbox declaration
is applied exactly as before.
Giving up a sandbox backend switch ends with applying a bundle that declares
the pending bundle as supersedesPendingBundle. The paragraph stopped there,
so an operator who followed only it kept the declaration. Every later apply
then warned that no bundle is pending, and configuration-only apply stayed
refused. The paragraph now says to remove the declaration and prepare again
once that apply is ready, in English, German and French.
@yannickmonney
yannickmonney added this pull request to the merge queue Oct 19, 2026
Merged via the queue into main with commit b7b18ea Oct 19, 2026
@yannickmonney
yannickmonney deleted the feat/cli-incus-sandbox-shape branch October 19, 2026 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant