Skip to content

docs(sandbox): templates -- the model, the API reference, provenance and session pinning - #924

Merged
pdettori merged 3 commits into
rossoctl:mainfrom
pdettori:docs/sandbox-templates
Nov 19, 2026
Merged

pdettori merged 3 commits into
rossoctl:mainfrom
pdettori:docs/sandbox-templates

Conversation

@pdettori

Copy link
Copy Markdown
Member

Summary

The template feature had a how-to (docs/sandbox/how-to/templates.md) and runbook steps, but no model page and no reference page; that material was only in specs, deploy/sbx/template-onboarding/ and openapi.yaml. This PR adds both. It draws on TEMPLATE_LIFECYCLE.md and the template parts of the client developer guide, keeping only what the published docs lacked (about 70% of the source repeats published pages and is linked instead), and corrects it against the code and openapi.yaml.

Change What
New docs/sandbox/concepts/templates.md, "Templates, builds and activation" Templates, builds, tags, the policy, host agents and activation leases. A build's onboarding status and a registered build's readiness, as tables and mermaid diagrams. How a sandbox or session picks its build (id, id:tag, id@build, default). Activation lease vs lease mode
New docs/sandbox/reference/templates-api.md, "Template API" Route tables for reads, create and builds, lifecycle changes, activation, and policy and quota: who may call each, what it does, and the matching mocactl sbx command. Also conventions, error codes, policy rule ids, and quotas with their defaults (templates 100, builds queued 10, active 8, owned 200) and refusals. Host routes get one sentence pointing to the runbook
runbook.md §5 Which policy writes need an admin named in the settings, and how gate records are trusted or self-attested
runbook.md, new unnumbered section "Provenance records: export, verify and redact". The numbered §1–§14 that scripts cite are byte-identical
how-to/templates.md "Pin a session to a build" (mocactl run --option sandboxTemplate=…), "From your own code", and a fix: a sandbox create after a retire answers 410, while a template create on a retired id queues a new build
user-guide.md, glossary.md Links to the two new pages

Corrections to the source material

These come from a review that checked about 60 claims against the code and openapi.yaml:

  • redact-provenance.py --catalog-only takes no template id and redacts every catalog entry (redact-provenance.py:4-8). The source's command would fail or redact more than intended.
  • The template store. The read routes work without one; the source said every template route answers 400 without it.
  • Missing error codes: 409 policy_changed (errors.ts:138, from PUT /v1/policy?expect=) and 403 mode_not_allowed are now in the table.
  • PUT /v1/policy now documents its ?expect= compare-and-set; sandbox create documents idleLifetimeS / --lifetime; built follows the contract.
  • Dropped because the code doesn't bear them out:
    • "a stored invalid policy fails until an admin PUTs a valid one" (PUT /v1/policy reads the stored policy first, so it fails too);
    • the git-identity rule;
    • "the manifest hash covers every field";
    • a retry claim for mocactl run.
  • Publish hygiene. No internal deployment names, and no links to specs or ADRs. None of the source repo's register ids or companion-file citations.
  • Conventions: the Preview banner and lowercase "moca", per CONTRIBUTING-DOCS.md.

The evidence list (claim → file:line) for the 17 most important facts is in the PR thread on request. The provenance export, verify and both redact forms were run end to end on a synthetic store: verify exits 0, and after a redaction an old export checked against the new head exits 1. The two kubectl copy steps are marked (unverified on a cluster).

Testing (from a clean clone of the branch)

  • make check-links: 238 files, 0 broken links.
  • scripts/check-docs-publish.sh: 85 published files, no matches; grep -iE 'sbx[0-9]|cohort|w3id' on the changed pages finds nothing.
  • scripts/check-docs-site.sh: 63 pages, no broken links or anchors.
  • Prettier 3.10.1 is clean.

Assisted-By: Claude Code

…and session pinning

The template feature had a how-to and runbook steps but no model or
reference page. From TEMPLATE_LIFECYCLE.md and the client developer
guide's template parts, kept only where the published docs had
nothing, corrected against the code and openapi.yaml:

- docs/sandbox/concepts/templates.md (new): templates, builds, tags,
  the policy, host agents and activation leases; a build's onboarding
  status and a registered build's readiness as tables and diagrams; how
  a sandbox or session picks its build (id, id:tag, id@build, default).
- docs/sandbox/reference/templates-api.md (new): the template, policy,
  quota and activation routes (who may call, what it does, the mocactl
  sbx command), conventions (the read routes work without a template
  store; everything else needs one), the error codes with 409
  policy_changed and 403 mode_not_allowed, the policy rule ids, and the
  quotas with their defaults (100 / 10 / 8 / 200) and refusals.
- runbook: in §5, which policy writes need an admin named in the
  settings, and how gate records are trusted or self-attested; a new
  unnumbered "Provenance records: export, verify and redact" section
  (redact-provenance.py --catalog-only redacts every catalog entry and
  takes no template id). The numbered §1-§14 are unchanged.
- how-to/templates.md: "Pin a session to a build" (mocactl run --option
  sandboxTemplate=...), "From your own code", and a fix: a SANDBOX
  create after a retire answers 410; a template create on a retired id
  queues a new build. The user guide and the glossary link the pages.

Claims the code did not bear out were dropped (among them the source's
"a stored invalid policy fails until an admin PUTs a valid one": PUT
/v1/policy reads the stored policy first). The export/verify/redact
scripts were run end to end on a synthetic store; the two kubectl steps
are marked unverified on a cluster.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — docs(sandbox): templates — the model, the API reference, provenance and session pinning

Both pages fill a real gap, and the corrections the PR calls out check out. I verified about 45 claims against the code and openapi.yaml; nothing blocking, two precision nits inline.

Placement and shape

  • docs/sandbox/concepts/templates.md (sidebar_position 3) and docs/sandbox/reference/templates-api.md (position 2) slot in beside the existing concepts/reference pages with no position collision, and both are linked from the user guide and the how-to.
  • The banner matches the CONTRIBUTING-DOCS template word for word, and "Moca Pi" / lowercase moca follow existing usage.

Reference accuracy (sampled)

  • Store gating is as described: the five GET reads fall back to the declared list (sandbox-handlers.ts:2576-2619; visibleTemplates and trustedSet tolerate a missing policy store), while getPolicy/getQuota and every write go through policyStore/writable and answer 400 invalid_request without SH_PROVENANCE_DIR (:884-906, :2642, :2816).
  • Error codes and statuses match errors.ts (policy_changed 409 at :138, mode_not_allowed 403 at :141, and the rest of the table).
  • Quota defaults 100/10/8/200 (template-policy.ts:167, DEFAULT_OWNED at :119); the live cap is 3 per subject (sandbox-config.ts:272) with a deployment total.
  • Lease 60–86400 s, default the largest idleLimitSeconds of the holders, else 3600 (sandbox-handlers.ts:1604-1622); Retry-After 30 s or the start line (:2079, activation.ts:575).
  • Rule ids, the M7 bounds (128 MiB, 1–32 vCPUs, 256 MiB), M8/M9, X1.self.scope/X1.self.egress/X12.self, X10/X11/X12/X14/X15 all match template-policy.ts (:380-655).
  • Onboarding and readiness tables match buildBody/templateBody (:721-819), including the honest "built is brief" note (the built report runs registration in the same catalog update, host-handlers.ts:1075-1104), cancel → failed/cancelled (:1398-1421), a pre-registration retire answering buildId: null (:1997-1999), and reject being registered-only (:1935-1941).
  • Use path: resolveForUse's 410/400 split (:839-871), template_not_active with readiness + Retry-After and the activate: false variant (:2087-2160), and the session create's enrolled-only activation (handlers.ts:1164-1171) are all as written.

Provenance section

  • Store layout, moca-provenance-record/1, the kind list (with use defined and never written — no writer in the tree), record-before-head (provenance.ts:246-250), and export skipping a blob the store does not hold (:310-336) match the page.
  • verify-provenance.py and redact-provenance.py match their docstrings: --head STREAM=sha256:… repeatable, exit 0/1/2, --catalog-only takes no id and touches no record, and redactions.log never holds the matched text.
  • The widening and self-attest prose in §5 matches policyWidens/profileWidens (sandbox-handlers.ts:2479-2569) and the per-sha256 claim queue (:966-1050) precisely, including the 403-for-API-grant-admins rule.

Session pinning

  • Fields (sandboxTemplate, sandboxBuildId, templateManifestHash, handlers.ts:554-561), sendUnlisted (session-options.ts:110-125), --option ignored on resume (headless.ts:499-503), the seed origin (run-turn.ts:390-444) and SH_TURN_PATCH_MAX_BYTES (:522-529) all hold.

Checks

  • make check-links: 238 files, 0 broken; check-docs-publish.sh: 85 published files, no match; runbook §1–§14 headings byte-identical to main; CI 18/18, docs job and site build included.

Two nits inline: the model page's absolute "a worker runs only while someone holds an activation lease" (true for a host-built build, not for one the deployment runs), and the create body's "visibility defaults to deployment" (a self-attested create stores private).

Author: pdettori (MEMBER — maintainer). One commit, signed off.

Comment thread docs/sandbox/concepts/templates.md Outdated
listens. It reports what the host holds, claims queued builds, builds and probes them, reports
the result, and starts or stops the workers the control plane wants on that host
([The template host agent](../../operate/sandbox-service/runbook.md#the-template-host-agent)).
- **A worker runs only while someone holds an activation lease.** An **activation lease** is a

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: true for a build a host agent runs, but not for one the deployment runs itself: a build declared in SH_SANDBOX_TEMPLATES (id imported:<12 hex>) is never host-managed — hostManaged() returns false for it (activation.ts:200), so requireActive returns before any lease exists (sandbox-handlers.ts:2099-2106), and activate refuses it with 400 invalid_request (:1536-1548). The API reference says this under Activation; one clause here ("…for a build a host agent runs") would keep the model page from reading as absolute.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c986909. The bullet now opens "A host-built build's worker runs only while someone holds an activation lease", and adds that a build the deployment declares itself (SH_SANDBOX_TEMPLATES, an imported: id) isn't host-managed: it needs no lease, and activating it is refused with 400 invalid_request (hostManaged(), activation.ts:200).

Comment thread docs/sandbox/reference/templates-api.md Outdated
`moca-sbx-template-spec/1` document; its guest size is `guest_ram_mb` or its alias `guest_mb`,
never both, and an `id` or `image` inside it must equal the create's. `gateRecord` is the gate
record file, byte for byte. The whole body is at most 1 MiB. `visibility` defaults to
`deployment`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: with one exception worth a clause: a self-attested create (an X1 profile with selfAttest, and a gate record the policy does not trust) stores private when visibility is omitted — and an explicit deployment there is refused 409 self_attested (sandbox-handlers.ts:1105-1111, :1262-1270). As written, a self-attesting client reads the default as deployment and is surprised by the refusal.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c986909: "visibility defaults to deployment, except on a self-attested create (a profile with selfAttest and a gate record the policy does not trust): that one is stored private, and an explicit deployment is refused 409 self_attested" (sandbox-handlers.ts:1105-1111, :1262-1270). That's consistent with the page's existing self_attested rows.

…e; self-attested visibility)

- concepts/templates.md: "a worker runs only while someone holds an
  activation lease" is true for a host-built build; a build the
  deployment declares itself (SH_SANDBOX_TEMPLATES, an imported: id) is
  not host-managed, needs no lease, and activating it is refused
  (400 invalid_request) -- activation.ts:200, sandbox-handlers.ts.
- reference/templates-api.md: visibility defaults to deployment except
  on a self-attested create, which is stored private, an explicit
  deployment there being refused 409 self_attested
  (sandbox-handlers.ts:1105-1111, :1262-1270).

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>

@webchang webchang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for carrying the template material into the new layout. I checked the routes, readiness reasons, lease range, quotas, CLI flags and the provenance scripts against the code, and they match; c986909's lease and visibility fixes read right. Two small fixes left:

  1. docs/sandbox/concepts/templates.md:21 (the tags bullet): please add the default rule. Only a template's first registered build takes default; a later build does not move it, and the owner moves it with mocactl sbx templates tag ID default --build B (host-handlers.ts:1107-1109). Without that sentence a reader expects a bare id to name the newest build, which is what #909 reports.
  2. concepts/templates.md:137 (activation refusals): 409 template_unavailable is also an activation refusal (no host holds the build, or memfile_mismatch); the reference page lists it. Add it, or link to the reference's list.

…navailable on activation)

- concepts/templates.md, tags: only a template's FIRST registered build
  takes `default`; a later build does not move it, so a bare id keeps
  naming the first build until the owner moves the tag with
  `mocactl sbx templates tag ID default --build BUILD`
  (host-handlers.ts:1107-1109) -- the expectation rossoctl#909 reports.
- concepts/templates.md, activation: 409 template_unavailable
  (readiness: unavailable) when the reference names no registered build,
  no host holds the snapshot, or every holder reports a memory file of
  the wrong size (reason: memfile_mismatch, which only a rebuild clears)
  -- sandbox-handlers.ts:1529-1535, :1683-1697; with a link to the
  reference's error codes.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori

Copy link
Copy Markdown
Member Author

@webchang Both points are fixed in 51a8463.

  1. The default rule. The tags bullet now says a template's first registered build takes default, and a later build doesn't move it. So a bare id keeps naming the first build until the owner moves the tag with mocactl sbx templates tag ID default --build BUILD (host-handlers.ts:1107-1109), which addresses templates: a bare template id resolves to a retired default build (410) while another build is active #909's surprise.
  2. 409 template_unavailable on activation. It's now a bullet among the activation refusals, with readiness: unavailable. The three cases are: the reference names no registered build, no host holds the snapshot, or every holder reports a memory file of the wrong size. The last one carries reason: memfile_mismatch, and only a rebuild clears it (sandbox-handlers.ts:1529-1535, :1683-1697). The bullet links the reference page's error codes.

From a clean clone: 238 files, 0 broken links; site build, no broken links.

@pdettori
pdettori merged commit d393cbf into rossoctl:main Nov 19, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants