Repository navigation
docs(spec,adr): SBX0, a minimal sandbox service on sandbox/v1 (ADR-0042) - #546
Conversation
Brings the sandbox-over-MCP prototype up as a service for a small internal cohort, on the existing sandbox/v1 gRPC API (no sandboxd, no proto or worker change): the relay behind TLS in its own namespace, a control-plane 'sandbox' route profile with explicit sandboxes and a per-sandbox Ed25519 token, and mocactl sbx (push/pull, a CLI, a local stdio MCP server). Increments 1a-1c to the cohort, egress next, hardening after. Registers the SBX track. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…-sandbox tokens Records the SBX0 decision: sandbox/v1 as it is (no sandboxd), the relay behind TLS in its own namespace, a control-plane 'sandbox' route profile minting per-sandbox Ed25519 tokens bound to one worker and one workspace key, and mocactl sbx with a local MCP server. Links it from the spec and both indexes. The spec now states that the prototype's static relay client tokens (feat/relay-client-tokens) run only in the team-only increment 1a and are never merged, and fixes a cross-reference (the token is section 4.3). Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Planning found that mocactl may depend on exactly one workspace package (ADR-0036, with ADR-0038's single exception, enforced by layering.test.ts), and that Pi's tools bring pi-tui, pi-ai and agent-core with them. The gRPC client, the seven tools (Pi's names and schemas, reimplemented over Exec without Pi) and the MCP server therefore move into @moca/sandbox-client, the second named exception, recorded in ADR-0042. Also settles O2 (the relay imports the token verifier from @moca/control-plane, as knative-server does) and O3. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…orkspace root Each microvm-worker sweeps its parent cgroup for orphans at startup (vmpool/cgroup.go), so template workers sharing one slice would kill each other's VMs on restart. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
… move to 1c run and put need the gRPC client, which lives in @moca/sandbox-client (1c). 1b's acceptance uses the updated Python example client, which lands on main. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…rker The worker queues a second Exec on a workspace behind the first and sandbox/v1 has no busy frame, so the MCP server refuses a call that would wait more than 30 s behind one of its own in-flight calls, with a readable tool error. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
…x relay sandbox/v1's End frame has no timeout flag, so the relay cannot report one; and the lines are written only when sandbox keys are configured, leaving a MOCA relay's output unchanged. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Summary
The SBX0 spec and ADR-0042 are well grounded. Most claims about existing code check out against main: relay ports and validators, the worker key regex and teardown gates, the grpc-js 1.14.6 keepalive options, the layering allow-list, routes, and the ADR-0024 references. ADR 0042 is free, and every relative link resolves. Four design gaps need fixing before this is the contract for 1b/1c: the worker dedup cache crosses tenants on a shared template worker, timeouts arrive as ExecError (not an exit code), in-flight Execs outlive token expiry and delete by up to MaxExecTimeoutS, and the base overlay still configures the unrestricted MOCA_RELAY_EXEC_TOKEN that the spec says is absent.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Docs (spec, ADR, indexes), cross-checked against main and the stacked 1a PR (#547)
Agent/IDE config (.claude/.vscode): none
Commits: 8 commits, all signed-off: yes
CI status: passing (all 14 checks)
| - Dial `SBX_ADDR` over TLS, and trust `SBX_CA_FILE` if one is given. | ||
| - Send `authorization: Bearer <sandbox token>`. | ||
| - Set `ExecRequest.sandbox_id = wrk` and `Exec.workspace_key = wsk` exactly. | ||
| - Keep `req_id` unique and below 2^53. |
There was a problem hiding this comment.
must-fix: the worker's dedup cache crosses tenants. Every user of a template shares one worker, and its dedup cache is keyed by req_id plus a fingerprint of command, stdin, timeout_s and streaming only (remote-worker/internal/session/cache.go Fingerprint, loop.go s.cache.Lookup(reqID, fp)). It does not include workspace_key. So another user's sandbox that reuses a completed req_id with the same command gets the first sandbox's cached terminal frame, and its own command never runs. A client counting from 1 hits this immediately, and "keep req_id unique" can't be met across tenants. Have the relay assign or rewrite req_ids, or enforce uniqueness per wrk, and add a §10.2/§10.6 test for two sandboxes sending the same req_id and command.
There was a problem hiding this comment.
Agreed, and confirmed: Fingerprint has no workspace key, and the relay forwards req_id as is. Fixed in dd53fb7 with new §3.6. For a sandbox caller, the relay sends the worker the first 53 bits of SHA-256(sbx ‖ 0x00 ‖ req_id) and writes the caller's own id back into every frame; Abort maps the same way. The mapping is deterministic, so a client's own retries still hit dedup for its own sandbox. §10.2 now has the two-sandboxes-same-req_id-and-command test, and §10.6 step 2 the live version.
|
|
||
| One JSON line per `Exec` on completion, to stdout: | ||
| `{ts, sub, sbx, wrk, req_id, ms, exit_code, truncated, out_bytes, outcome}`, where | ||
| `outcome` is `end | error | cancelled | refused`. There is no timeout field: `sandbox/v1`'s `End` carries only the exit code and `truncated`, so a timeout shows as the exit code the worker reports. The lines are written only when the relay has sandbox keys configured, so a MOCA relay's output is unchanged. Command text and output are never logged; an |
There was a problem hiding this comment.
must-fix: a timeout is an ExecError, not an exit code. On main, a timeout comes back as ExecError{"timeout:<n>"} (remote-worker/internal/session/loop.go:720, vmpool/errors.go:10), and the relay maps that to an error status. Under §7's rule (ExecError = machinery failure), sbx run would exit 125 on a timeout, not the 124 promised in §6.1/§10.3, and the audit would log outcome: error. Please specify mapping timeout:<n> to 124 (and a timeout audit outcome), and fix this sentence and §7.
There was a problem hiding this comment.
Fixed in dd53fb7. ExecError{"timeout:<n>"} is exit 124 in sbx run and the tools (§6.1, §6.5, §7, §10.3), and the audit gets outcome: timeout (§3.5). §7 now separates it from machinery failures.
|
|
||
| It does nothing when the control plane is unreachable, and never removes a directory whose sandbox | ||
| is `ready` or unknown to the control plane. An unknown directory is reported, not removed. The | ||
| 1-hour wait outlasts any token's remaining life (§5.4.3), so no `Exec` can still be using a tree the |
There was a problem hiding this comment.
must-fix: access outlives expiry and delete, so the janitor's safety argument doesn't hold. The relay checks exp only when an Exec is accepted, and the worker allows timeout_s up to MaxExecTimeoutS = 2 h (vmpool/pool.go:42). An Exec accepted in a token's last second (plus the 30 s leeway) can run about 2 h past expires_at, or about 2 h 15 min past a delete. That breaks §9's "access ends at expiry" and this claim that the 1 h wait means no Exec is still using the tree. Clamp timeout_s to exp - now in the relay (or cancel in-flight Execs at exp), or raise the janitor wait above TTL + leeway + MaxExecTimeoutS and correct the claims.
There was a problem hiding this comment.
Agreed. Fixed in dd53fb7 (§3.6): the relay arms a timer per accepted sandbox Exec, at the token's exp plus the 30 s leeway. When it fires, the relay aborts the Exec at the worker and ends the call with DEADLINE_EXCEEDED. I chose this over clamping timeout_s because a rewritten timeout_s changes the worker's dedup fingerprint, so a retry would read as a collision. If the relay dies, the worker kills the stream's Execs when its attach stream drops (cancelConn in loop.go), so the bound still holds. Clients mint a fresh token when one has less than timeout_s + 60 s left, and cap timeout_s at TTL − 60 s, so the worker's own timeout normally fires first (§6.5). §5.4, §8.3 and §9 are corrected, setup.sh refuses a janitor wait ≤ TTL + leeway, and the 15-minute ceiling on one command is recorded as risk 5 and in the ADR.
| The exec service's caller check (`validateExecToken` in `main.ts`) becomes a caller | ||
| classification with two kinds. Precedence, first match wins: | ||
|
|
||
| 1. **Harness:** `MOCA_RELAY_EXEC_TOKEN`, unchanged and unrestricted. Not configured in an SBX0 |
There was a problem hiding this comment.
must-fix: This says the harness token is "Not configured in an SBX0 deployment", but deploy/k8s/base/relay.yaml:44-45 wires MOCA_RELAY_EXEC_TOKEN through a non-optional secretKeyRef, and main's makeExecTokenValidator refuses to boot without it. #547's deploy/sbx/setup.sh therefore generates one. Under §3.1's precedence, that's an unrestricted caller that skips all wrk/wsk binding, held by anyone who can read Secrets in the namespace. Please state that 1b's ocp-sbx overlay drops the env entry and setup.sh stops minting it (with the validator made optional in that profile), and add an overlay test that the rendered relay has no MOCA_RELAY_EXEC_TOKEN.
There was a problem hiding this comment.
Correct. dd53fb7 states it in §3.1 and §8.1. From 1b, the ocp-sbx overlay removes the env entry, setup.sh stops minting it and deletes the key from an existing moca-relay Secret, the relay boots on the sandbox keyset alone (§3.3), and an overlay test asserts the rendered relay has no MOCA_RELAY_EXEC_TOKEN (§10.2). 1a's team-only relay keeps one, readable only by the namespace's operators.
| - These are **the disk control.** Workspace disk is not admission-controlled on the worker; only VM | ||
| memory is. `setup.sh` checks that `SH_SANDBOX_MAX_TOTAL` × the workspace image size fits the KVM | ||
| host's workspace filesystem (§8.1). | ||
| - Concurrency per sandbox is already 1: the worker serializes `Exec`s per workspace. |
There was a problem hiding this comment.
suggestion: noisy neighbour. "Concurrency per sandbox is already 1" covers execution only. Each queued Exec has already acquired a VM, charged against SH_MAX_COMMITTED_MB, before it waits on the per-workspace gate (vmpool/pool.go). It also holds one of the worker's 16 slots, which are shared by every user of the template (microvm-worker main.go:84). One user firing parallel Execs at their own sandbox can starve the template for everyone else, who then see a memory-budget refusal or the worker's busy: queue full ExecError, despite §6.4 saying there is no busy signal. Consider a relay-side cap on in-flight Execs per sbx.
There was a problem hiding this comment.
Adopted in §3.6: SH_SANDBOX_MAX_INFLIGHT (default 2: one running, one waiting at the per-workspace gate) per sbx, refused with RESOURCE_EXHAUSTED "sandbox busy". §6.4 also maps the worker's busy: queue full and memory-budget refusals to a "template busy" tool error, and §10.2 tests the cap.
| than `SH_SANDBOX_LIFETIME_SECONDS` (default 9 days against 7). `setup.sh` refuses otherwise. | ||
| Idle time can't exceed total lifetime, so the worker's idle sweep can only reach a workspace whose | ||
| sandbox has already ended. | ||
| 2. **Expiry is exact.** No token outlives its sandbox's `expires_at`. |
There was a problem hiding this comment.
nit: "Expiry is exact" contradicts the 30 s exp leeway at L148 (and the "≤ 15 min" delete bound). Reword, or apply the leeway only to iat.
There was a problem hiding this comment.
Reworded: §5.4.2 is now "access ends at expiry, within the 30 s leeway", and delete is "within one token lifetime plus the leeway".
| - **In the sandbox**, that Exec empties `/workspace`, extracts, writes a `.git/info/exclude` for | ||
| common build output (`__pycache__/`, `*.pyc`, `node_modules/`, `.venv/`, `venv/`, `.pytest_cache/`, | ||
| `.mypy_cache/`, `dist/`, `build/`, `target/`), and commits a baseline with a fixed identity. | ||
| - **The 8 MiB message ceiling** (ADR-0024) bounds one push. A larger tree is split into several |
There was a problem hiding this comment.
nit: The gRPC message ceiling is 16 MiB (MAX_EXEC_MESSAGE_BYTES, packages/k8s-sandbox/src/transport.ts:146). 8 MiB is the output cap. Chunking push at 8 MiB is fine, but don't call it ADR-0024's message ceiling.
There was a problem hiding this comment.
Fixed: push chunks at 8 MiB, half the 16 MiB MAX_EXEC_MESSAGE_BYTES. Pull is bounded by the worker's 8 MiB output cap.
| still in flight (the prototype's ownership map, keyed by `sbx` instead of a grant; §3.1). | ||
|
|
||
| The relay holds **public keys only**, and does no Redis read for authorization. Verification reuses | ||
| `token.ts`'s verify primitive once it is generalized over `typ` and `aud` (MI1 §6.1). If importing |
There was a problem hiding this comment.
nit: §3.2 still treats the verifier's home as open ("If importing @moca/control-plane into the relay is unwanted…"), but O2 records it as settled. Make §3.2 match O2.
There was a problem hiding this comment.
Fixed: §3.2 now matches O2.
|
|
||
| **Refuses** to apply if: | ||
|
|
||
| - `SH_WORKSPACE_IDLE` ≤ `SH_SANDBOX_LIFETIME_SECONDS`; |
There was a problem hiding this comment.
nit: SH_WORKSPACE_IDLE is a Go duration and SH_SANDBOX_LIFETIME_SECONDS is seconds, so state the conversion. In 1a the idle value is hard-coded (WORKSPACE_IDLE=216h in #547's install-host.sh), not a setup.sh input, so say which script owns it from 1b on.
There was a problem hiding this comment.
Fixed in §5.4.1. From 1b, setup.sh owns SH_WORKSPACE_IDLE and writes (SH_SANDBOX_LIFETIME_SECONDS / 3600 + 48)h into each worker bundle. install-host.sh takes it from there, replacing 1a's constant.
| means editing a Secret and restarting the relay. Used only in the first, team-only increment, from | ||
| the prototype branch, and never merged. | ||
| - **Embedding Pi's tools in `mocactl`** (depending on `pi-coding-agent` and `@moca/k8s-sandbox`). | ||
| One tool implementation, but it would break ADR-0036's stated core ("no pi") and bundle Pi's TUI |
There was a problem hiding this comment.
nit: ADR-0036 doesn't say "no pi". It forbids workspace:* deps. The "no pi, no server internals, no substrate assumptions" wording is ADR-0038's paraphrase, so attribute it there.
There was a problem hiding this comment.
Fixed: the ADR now names ADR-0036's rule as being against workspace:* dependencies, and attributes "no Pi, no server internals, no substrate assumptions" to ADR-0038, in both places.
Must-fix:
- The relay namespaces a sandbox caller's req_id per sandbox (the worker's
dedup fingerprint omits workspace_key, so two sandboxes on one template
worker could share a result or abort each other) (new section 3.6).
- A timeout is ExecError{"timeout:<n>"}: exit 124, audit outcome timeout.
- No Exec outlives its token: the relay cancels at exp plus the leeway, so
access really ends at expiry and the janitor's wait argument holds; the
longest command is one token lifetime (a new risk).
- MOCA_RELAY_EXEC_TOKEN is removed from the ocp-sbx relay from 1b, with an
overlay test.
Suggestions and nits: a per-sandbox in-flight cap; the 1a image branch is
named (sbx/relay-1a); the guide and examples are new in 1b; the audit test
moves to 1c and sbx create gets its command row; the janitor reads live and
terminated records and reclaims unknown directories only after
SH_WORKSPACE_IDLE; admins never mint; the leeway wording; the 16 MiB message
ceiling; section 3.2 matches O2; the SH_WORKSPACE_IDLE conversion and owner;
ADR-0038's wording attributed. API bodies are camelCase, with the id format
and storage keys the 1b plan uses.
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Re-review at dd53fb7. All 15 items from our 82bdf25 review are addressed. I verified the four must-fixes against the diff and main:
- Dedup across tenants: §3.6 namespaces
req_idpersbx(deterministic, so own-sandbox retries still dedup), with the two-sandbox test in §10.2/§10.6. - Timeouts:
ExecError{"timeout:<n>"}→ exit 124 /outcome: timeoutthroughout §3.5, §6.1, §6.5, §7 and §10.3. - Exec outliving its token: the relay cancels at
exp+ leeway. The relay-death fallback holds: loop.go:466cancelConn()kills in-flight and queued children when the attach stream drops. The janitor wait is now checked bysetup.sh. - Harness exec token: removed in the
ocp-sbxoverlay from 1b, with an overlay test.
The suggestions and nits are also resolved (in-flight cap, sbx/relay-1a, new-on-main docs, 1b/1c audit split, GET /v1/janitor/sandboxes with unknown-directory reclaim bounded by SH_WORKSPACE_IDLE, owner-only mint, wording fixes). The two new suggestions below are about the token-freshness/timeout_s interaction that §3.6 introduced. Neither blocks.
Author: pdettori (MEMBER — maintainer)
Areas reviewed: Docs (spec, ADR), incremental diff 82bdf25..dd53fb7, cross-checked against main 068711f
Agent/IDE config (.claude/.vscode): none
Commits: 9 commits, all signed-off: yes
CI status: passing (all 14 checks)
| known wire wart, documented and not fixed in SBX0. | ||
| - On `UNAUTHENTICATED` the client refreshes the token once and retries **only if the call never | ||
| started**, that is, before the first event. A started command is never re-run. | ||
| - **Token freshness.** Before each Exec, the client makes sure its token has at least `timeout_s` + |
There was a problem hiding this comment.
suggestion: the token-freshness rule can't be met at the default timeout. timeout_s defaults to TTL − 60 s, and the rule requires at least timeout_s + 60 s = a full TTL left on the token. A token minted a moment ago already has slightly less than that, so read literally, every default Exec mints, and even then the check never passes. Near the sandbox's end it is also unsatisfiable, because exp is capped at expiresAt (L288). Give it slack (e.g. require timeout_s + 30 s, or default to TTL − 120 s), and say what happens close to expiresAt (clamp timeout_s to the remaining life, or refuse with "sandbox expires in N s"). §10.3's token-freshness test should cover both cases.
| - Keep `req_id` unique within the sandbox and below 2^53. The relay namespaces it per sandbox | ||
| (§3.6), so ids cannot collide with another user's. | ||
| - Ping every 20 s. | ||
| - Run `SBX_TOKEN_CMD` for a token, and again when it is within 60 s of `exp`, on `UNAUTHENTICATED`, |
There was a problem hiding this comment.
suggestion: direct clients that omit timeout_s get the worker's 30-minute default. That default is DefaultExecTimeoutS (remote-worker/internal/vmpool/pool.go:41, applied by clampTimeoutS to 0/absent). It is longer than the 15-minute token, so a long command from sandbox_v1_client.py, sbx.py or a developer's own code ends in §3.6's DEADLINE_EXCEEDED instead of a timeout. §6.5's cap binds only mocactl. Add the cap to this list of client rules, or have the relay refuse a sandbox Exec whose timeout_s is 0 or above TTL − 60 s with INVALID_ARGUMENT. A refusal doesn't rewrite the Exec, so it leaves the dedup fingerprint alone, which was the concern with clamping. (I checked that the worker's timer also covers the wait at the per-workspace gate, pool.go execGate select on runCtx, so queued Execs are bounded too.)
| reason: "deleted" | "expired" | null, createdAt, expiresAt, terminatedAt | null, labels } | ||
| ``` | ||
|
|
||
| Times are epoch seconds. `execCa` is the exec route's CA certificate (PEM) when it is self-issued, |
There was a problem hiding this comment.
nit: Epoch seconds is fine, but the control plane's existing createdAt is milliseconds: the session view returns rec.createdAt = deps.now() = Date.now() (handlers.ts:610, main.ts:343). Token expiresAt is seconds, though. Since mocactl will render both a session's and a sandbox's createdAt, say explicitly that the units differ, or use ms for createdAt/terminatedAt to match.
|
|
||
| `ready → terminated`, with `reason ∈ { deleted, expired }`. | ||
|
|
||
| - **Expiry** is computed, not scheduled: a sandbox whose `expires_at` has passed reads as |
There was a problem hiding this comment.
nit: expires_at was left over from before the camelCase rename. It also appears at L288 and L726, while §5.1 now names the field expiresAt.
Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
Design for SBX0, a minimal sandbox service for a small internal cohort, following the exec review of the sandbox prototype. The review decided not to adopt a standardized exec API (sandboxd) while those APIs are still moving, and to offer what MOCA already supports on
sandbox/v1instead.docs/specs/2026-10-15-sbx0-internal-sandbox-service-design.md, plus a new "Sandbox Service (SBX-prefix)" track indocs/specs/README.md.sandbox/v1as it is, behind per-sandbox Ed25519 tokens from a second control-plane instance in asandboxroute profile.@moca/sandbox-clientbecomes the second named exception to ADR-0036's dependency rule.There is no proto change and no worker change. The design ships in three increments:
ocp-sbxoverlay,setup.sh, host installmocactl sbxHTTP commands@moca/sandbox-client,mocactl sbx run/put/push/pull/up/mcp, the janitor and auditAllowlisted egress is the next increment after 1c.
Stack: this PR is the bottom. The three PRs for 1a, 1b and 1c are stacked on it, each based on the branch below.
No site-specific values: hosts, images and subjects are deployment inputs.
🤖 Generated with Claude Code