Skip to content

Record template provenance in a hash chain, verifiable offline #730

Closed

Description

@webchang

Problem

Nothing in upstream records where a sandbox template came from, who changed it, or which snapshot a
sandbox ran on. The control plane's only trail is the ownership-plane audit stream sh:cp:audit
(packages/control-plane/src/ownership.ts, AUDIT_STREAM): a Redis stream trimmed to about
AUDIT_MAXLEN entries, not chained, and not exportable as a self-checking document. Templates
themselves are a fixed env list (SH_SANDBOX_TEMPLATES, sandbox-config.ts), so after the fact an
operator cannot show which image, gate record and policy produced the hands a run used, nor prove that
a record was not edited, dropped or reordered.

For benchmark runs (one template per task instance), and for any audit, that chain is what lets a
result be traced to the exact snapshot it ran on.

Proposal

An append-only provenance store in the control plane, one hash chain per stream, exportable and
verifiable offline with the Python standard library alone.

  • Store (packages/control-plane/src/provenance.ts, ProvenanceStore): a directory on the control
    plane's persistent volume. The control plane opens it from SH_PROVENANCE_DIR (absolute path) with
    the template store of Keep sandbox templates in a control-plane store, with builds, tags, readiness and a read API #736, which shares the directory; unset, nothing is recorded and nothing
    changes. One file per record under records/, named by its digest; one head per stream
    under heads/; cited documents (gate records) byte for byte under blobs/, by sha256. A record is
    written (exclusive create, fsync) before its stream's head moves (write-temp, fsync, rename), so a
    crash leaves an orphan record, never a head naming a missing one. Appends to one stream are
    serialized. The directory is checked writable at start, so a missing or read-only volume fails the
    start rather than the first write.
  • Records (moca-provenance-record/1): {stream, seq, prev, kind, subject, at, body}. A record's
    digest is the sha256 of its canonical JSON (keys sorted, no whitespace, integers only, so a second
    implementation writes the same bytes); prev names the previous record's digest. Streams are
    policy and template/<id> (later units add members). Kinds: policy, create, gate, build,
    check, activate, deactivate, tag, visibility, owner, retire, reject, use, sandbox,
    session. The units that produce those events write them (the template store, self-serve create,
    the host agent's build reports, activation, retire); this issue adds the store, the format and the
    checks.
  • Every cross-record reference is a record digest, never a time or a name: check.policy,
    check.build, retire.buildRecord, reject.buildRecord, and the lease of a sandbox or session
    record (the activate record it came from). A run then joins its build by equality alone.
  • A sandbox's span on its template's chain: a sandbox record at create (sandboxId, buildId,
    templateManifestHash, lease, expiresAt) and one at the end (state: terminated, reason: deleted
    with endedAt, or expired, written once by the janitor's sweep when it first sees the expiry).
  • Export (moca-provenance-export/1, ProvenanceStore.export()): the requested template streams,
    the policy stream they cite, every stream head with its count, and every gate record a gate record
    names.
  • Offline verification: deploy/sbx/template-onboarding/verify-provenance.py EXPORT.json [--head STREAM=sha256:…] and verifyExport() in TypeScript apply the same rules: every record's
    digest, each stream's seq/prev chain up to its head and count, every blob's sha256, each check
    citing a policy record of the export, each gate citing a blob of it. --head compares a head kept
    elsewhere, which is what catches an export that dropped its latest records. Exit 0 verified, 1 a check
    failed (each printed), 2 usage.
  • Redaction: redact-provenance.py STORE_DIR TEMPLATE_ID --pattern RE [--apply] (control plane
    stopped) rewrites the matching records of one stream and every later one, moves the head, and appends
    a note to redactions.log (counts and heads, never the matched text). --catalog-only redacts text
    only the catalog keeps. Dry run by default. It exists for data that must not be kept; an export taken
    before a redaction no longer verifies against the new head, by design.

What the PR will contain

One PR, about 1,390 added lines with tests.

  • packages/control-plane/src/provenance.ts (store, canonical JSON, export, verifyExport),
    src/index.ts (exports), the sandbox record hooks in src/sandbox-handlers.ts (createSandbox,
    sandboxEnded, recordExpiries; inert until a store is configured). Reading SH_PROVENANCE_DIR in
    src/main.ts comes with Keep sandbox templates in a control-plane store, with builds, tags, readiness and a read API #736, which opens the store and the catalog together.
  • deploy/sbx/template-onboarding/verify-provenance.py, redact-provenance.py.
  • Tests: packages/control-plane/test/provenance.test.ts (chain, concurrent appends, an edited record
    file refused, restart continuity, export, tampering cases; it runs the Python verifier on the same
    exports as verifyExport, so the two cannot drift), and the "every link of a sandbox is a record
    digest" case.
  • Docs: the provenance section (§7) of the template lifecycle design.

Depends on

Nothing. Later template issues write their records into it: #736 (store), #737 (self-serve create),
#741 (host agent), #742 (activation lease), #749 (retire), #751 (sessions), #739 (members stream),
#752 (egress digests on sandbox records).

How to verify

  • pnpm -C packages/control-plane test -- provenance
  • python3 deploy/sbx/template-onboarding/verify-provenance.py <export.json> on an export the test
    writes, then again after editing one byte of a record (exit 1), and with --head naming an older
    head (exit 1).

Limits / out of scope

  • Digests, not signatures: an export proves itself consistent; only --head against a head kept
    elsewhere catches truncation. Signing images or an SBOM are out of scope.
  • No HTTP export route yet: the export is the store's export(); an authorized
    GET /v1/provenance/export?template=<id> is planned in the design, not in this PR.
  • The verifier does not re-run each check against its policy, nor match a build record's manifest
    hash to a snapshot at hand.
  • A store written before a record kind existed links those events by time, not by digest; a verifier
    older than a kind refuses a store holding it.
  • One control-plane process per store directory.

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