Skip to content

Keep sandbox templates in a control-plane store, with builds, tags, readiness and a read API #736

Closed

Description

@webchang

Problem

Upstream's templates are a fixed JSON list in SH_SANDBOX_TEMPLATES
(packages/control-plane/src/sandbox-config.ts), served by GET /v1/templates on the sandbox route
profile only (src/routes.ts), each id mapped straight to one worker. So:

  • adding, changing or removing a template is a redeploy (deploy/sbx/setup.sh, RUNBOOK §5);
  • a template has no versions: a new snapshot of the same environment is a new id or an in-place swap
    of the worker under live sandboxes;
  • a client cannot see whether a template can serve now (is its worker attached?): a template with no
    worker fails only when it is used;
  • a Moca Pi session (the harness profile) cannot read the templates at all.

Proposal

Templates live in a store the control plane owns, imported once from the env list, and are served with
their builds as resources.

  • Store (packages/control-plane/src/template-catalog.ts, FileTemplateCatalog): a directory on a
    persistent volume, SH_PROVENANCE_DIR, shared with the provenance store of Record template provenance in a hash chain, verifiable offline #730 (each catalog change
    writes its record there). SH_SANDBOX_TEMPLATES is imported once into an empty store (a create
    record per template) and ignored after that, with a warning when it differs. Unset, nothing changes:
    templates come from the env list at every start, as today.
  • Templates, builds, tags: a template is a name over immutable builds. A build's id is its
    snapshot's manifest hash (sha256:…); an imported entry is one registered build imported:<12 hex>
    (stable per worker, never the worker id, which clients never see). Tags map to builds; default is
    what a bare id names.
  • References: a sandbox create names id, id:tag or id@sha256:…; the reference is looked up
    to one build at create and kept for the sandbox's life; an unknown one is 400 unknown_template at create, as today; the read routes answer 404 template_not_found.
  • Readiness from facts: for a registered build, active (the relay holds an attached record for
    its worker, the same record the harness routes on, with its capacity), inactive, or unknown when
    presence cannot be read; later issues add activating, deactivating, unavailable and retired.
  • Routes, on both route profiles (BOTH), so a Moca Pi brain and a stock agent read the same
    templates:
    • GET /v1/templates (today's first fields unchanged, plus owner, visibility, onboarding,
      readiness, tags, builds, and a hosts block that later host issues fill);
    • GET /v1/templates/{id}, GET /v1/templates/{id}/builds[/{build}] (each build with its own
      spec, seed included, so a client that runs the seed itself reads the build it was served, never
      the newest), GET /v1/templates/{id}/tags (each tag's build, when and by whom).
    • private templates are visible only to their owner and admins (others get 404).
  • Deploy: an overlay deploy/k8s/overlays/ocp-sbx/template-store (a PVC mounted as
    SH_PROVENANCE_DIR) and setup.sh SBX_TEMPLATE_STORE=1.
  • mocactl (the client part of this unit): sbx templates shows readiness and tags;
    sbx templates show ID [--build BUILD]; sbx create --template ID[@BUILD] [--seed], where --seed
    runs the served build's own seed and checks /workspace is a clean tree, recording its HEAD as the
    sbx baseline so pull returns only later changes and rm needs no --force.

What the PR will contain

One PR, about 1,760 added lines with tests on the server side, plus this unit's part of the mocactl
client (sized when the branch is cut; the whole client is about 4,800 lines, filed with the server
issues it drives).

  • control-plane: src/template-catalog.ts, src/sandbox-store.ts, src/sandbox-redis.ts;
    src/sandbox-handlers.ts (visibleTemplate(s), readinessFacts, templateBody, buildBody,
    resolveForUse, the read handlers, createSandbox's lookup); src/routes.ts, src/index.ts,
    src/main.ts (templateCatalogFromEnv, which also opens Record template provenance in a hash chain, verifiable offline #730's store);
    test/template-catalog.test.ts, test/sandbox-handlers.test.ts (routing across a worker change).
  • deploy: deploy/k8s/overlays/ocp-sbx/template-store/{kustomization,patch-control-plane,pvc}.yaml;
    deploy/sbx/setup.sh, tests/setup.test.sh, tests/overlay.test.sh.
  • mocactl: src/sbx/api.ts (the read calls), src/sbx/templates.ts (show, create --seed),
    src/sbx/push.ts (the baseline), src/sbx/cli.ts; their cases in test/sbx-templates.test.ts,
    test/contract.test.ts.
  • docs: docs/api/openapi.yaml (templates, builds, readiness; a minor API version bump),
    docs/api/CHANGELOG.md, docs/reference/http-api.md, docs/reference/configuration.md
    (SH_PROVENANCE_DIR); the template lifecycle design
    (docs/specs/2026-11-03-template-lifecycle-and-provenance-design.md) §1-§4.

Depends on

#730 (the provenance store: the catalog's records).

How to verify

  • pnpm -C packages/control-plane test -- template-catalog sandbox-handlers
  • bash deploy/sbx/tests/overlay.test.sh; bash deploy/sbx/tests/setup.test.sh
  • pnpm -C packages/mocactl test -- sbx-templates contract
  • On a namespace with SBX_TEMPLATE_STORE=1: the env list's templates are listed with readiness: active while their workers are attached; the next setup.sh run with a changed list warns and keeps
    the store; sbx create --template ID@sha256:… is served by that build.

Limits / out of scope

Assisted-By: Claude Code

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions