Skip to content

Per-sandbox egress records through the API, digested into the sandbox's provenance #752

Closed

Description

@webchang

Problem

On each KVM host one moca-egress proxy serves every template worker's guests and writes one audit
event per connection or request (remote-worker/internal/egress/audit.go, Event: time, vm_id,
sid, host, dial_addr, method, path, status, bytes, credential by name, decision,
drop counts) as a JSON line into the host's journal. For a sandbox, sid is its workspace key, which is
the sandbox id. So the facts are recorded per sandbox, but only on the host: a sandbox's owner, or a
benchmark grader who must void a run for a download its rules forbid, cannot ask Moca what a sandbox
reached, and nothing ties those events to the sandbox's provenance.

Proposal

  • Collection, on the host (host-agent.sh, in its --wanted-only runs, about every 15 s): read the
    proxy's journal after a cursor and post the events unchanged, in batches of up to 1000, at most 10
    per run, within a 120 s budget. It never acts on the proxy and is off unless moca-egress.service
    is active. Only this host's sandboxes' events leave the host: the wanted answer lists egressSids;
    other workloads' events (Moca's P6 sessions on the same proxy) stay, counted only. A failed post
    keeps the batch and cursor; a batch refused as malformed is kept on the host in egress.rejected/
    and the cursor moves past it; a cursor the journal no longer holds restarts from this boot, skipping
    entries already sent by a position mark. All files 0600 under $SBX_SRV.
  • POST /v1/hosts/{host}/egress {events, cursor, oversized?} (host bearer), idempotent by (host,
    cursor) → {accepted, refused, capped, invalid, dropped, cursor}. An event is stored only when its
    sid is a sandbox on a worker of a build this host holds, ended at most 600 s ago; an event naming
    a field the proxy does not write is refused (its JSON is the wire format); an event over 8 KiB is
    sent as oversized (its sid only).
  • Store (src/egress-log.ts, Redis beside the sandbox records): a stream per sandbox (one
    canonical line per event, capped at 10,000 per sandbox and 200,000 across all logs, the excess counted
    as dropped), drop counts, and a per-host cursor; kept as long as the
    sandbox record (its end plus SH_SANDBOX_RETENTION_SECONDS). The per-sandbox index drops entries
    whose sandbox record has expired.
  • Read (the sandbox's owner, or an admin; else 404 sandbox_not_found):
    GET /v1/sandboxes/{id}/egress?after=<next>&limit=N → {events, next, summary}; summary is
    byHost (count, decisions, bytes per destination), total, dropped, droppedBy (proxy, cap,
    budget, invalid), complete (dropped == 0), sealed, sha256.
    ?format=ndjson returns the whole log (application/x-ndjson).
  • Digest and provenance: the log's digest is the sha256 of its NDJSON (each event's canonical JSON
    and a newline, in arrival order). The sandbox's terminated record (Record template provenance in a hash chain, verifiable offline #730) gains egress: {events, sha256, dropped, droppedBy, complete}. Late events are accepted for 600 s after the end; the janitor's
    sweep then seals the log and, if it changed since the end record, appends one sandbox record with
    state: egress naming the final log. deploy/sbx/template-onboarding/verify-provenance.py EXPORT --egress <sandbox>=<file> checks a
    saved copy offline against the newest record naming it.
  • Drops are never silent: a proxy audit_dropped report is charged to every sandbox on that host's
    workers within 60 s before it, so a log may read incomplete when it lost nothing, never complete when
    it did.
  • mocactl (the client part, filed here): mocactl sbx egress [ID] [--json | --ndjson | --verify]
    (per host, and whether the log is complete; --ndjson prints the log whose sha256 the end record
    names; --verify checks its sha256 and count against that record, exit 0 only when both match).

What the PR will contain

Split in two (about 2,820 lines in all, with tests):

  • PR-29a, the control plane (about 2,030): src/egress-log.ts, src/egress-handlers.ts,
    src/host-handlers.ts (the route, egressSids), src/sandbox-handlers.ts (the end record's digest,
    the seal), src/routes.ts, src/handlers.ts, src/main.ts; test/sandbox-egress.test.ts,
    test/egress-redis.test.ts (Redis), the egress cases of test/provenance.test.ts; a design
    docs/specs/…-sandbox-egress-records-design.md, docs/api/openapi.yaml (a minor API version bump), CHANGELOG.
  • PR-29b, the host agent, the verifier and the client (about 790): deploy/sbx/host-agent.sh's
    egress step and its cases in tests/host-agent.test.sh; new cases in remote-worker/internal/egress/audit_test.go
    (RedactPath and the event fields as the wire format); verify-provenance.py --egress; mocactl
    sbx egress with test/sbx-egress.test.ts. Depends on PR-29a.

Depends on

#730 (provenance), #741 (host routes and the agent), #742 (the wanted state). Related: #623 (guest
egress).

How to verify

  • pnpm -C packages/control-plane test -- sandbox-egress egress-redis provenance (Redis)
  • bash deploy/sbx/tests/host-agent.test.sh
  • go test ./remote-worker/internal/egress/...
  • pnpm -C packages/mocactl test -- sbx-egress
  • On a KVM host with moca-egress active and an egress template: a sandbox's curl to an allowed host
    and to a denied one both appear in mocactl sbx egress <id> within a wanted run or two; after
    sbx rm, mocactl sbx egress <id> --verify exits 0 and verify-provenance.py --egress passes on a
    saved copy.

Limits / out of scope

  • A secret a guest puts in a URL path segment is logged as sent. Paths are logged without query,
    fragment or userinfo, and an injected credential appears by name only, but the proxy does not redact
    path segments; this PR leaves the proxy unchanged and documents it.
  • Shipping events has been exercised by tests, not yet live with events: on deployments run so far
    the proxy was off, so the logs were empty, complete, and digested as the sha256 of no bytes.
  • After a control-plane rollout, the sweep may seal older sandboxes' empty logs once (append-only,
    0 events); expected, no guard proposed.
  • Moca Pi sessions' egress, request bodies and packet capture are out of scope; what the proxy allows is
    the host operator's allowlist.
  • Deploy the control plane first: an older one answers 404 on the route, and the agent keeps its batch
    and retries.

Assisted-By: Claude Code

Activity

  1. added 20 commits that reference this issue on Nov 13, 2026
  2. added this to the v0.11.0 milestone on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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