Skip to content

Seed a pinned session from its build, return the workspace's patch, and carry templates on async turns #751

Closed

Description

@webchang

Problem

With session templates (#731) and a combined control plane (#750):

  1. A session pinned to a store build gets no seed. The session exchange (exchange.ts) reads the
    seed from SH_SANDBOX_TEMPLATES only, so a self-serve template (Let users create sandbox templates self-serve, gated by a machine-checked policy #737), which is in no env list,
    starts in an empty workspace even though its build's spec carries a seed.
  2. A client cannot read back what a session changed. Only /v1/runs solve leaves return a patch;
    a session's client has no exec on its workspace (the relay's exec route takes sandbox tokens only),
    and tool results are clipped. A benchmark that grades a session's work has no way to get it.
  3. No record ties a session to its build. A sandbox has sandbox records on its build's chain (Record template provenance in a hash chain, verifiable offline #730);
    a session has none.
  4. Async turns lose the template (with feat(control-plane): async turn submission and turn records (#387) #704). The dequeue-time exchange
    (turn-exchange.ts, exchangeTurnCredential, feat(control-plane): resolve an async turn's credential at dequeue (#388 M1) #713/fix(control-plane): #713 review follow-ups — correct the refusal docs, bound the dequeue turn id #714) returns sandboxTier from the live session
    but none of sandboxTemplate, sandboxSeed, sandboxSeedSource, sandboxBuildId, which the
    session exchange returns. When the async consumer (Coding-agent subagents B4: async leaf worker on P6 (deploy/vm, compose) #389) drains a templated session's turn, it would
    run on an untemplated worker, unseeded, with no patch.

Proposal

  • The seed from the build: for a session pinned to a sha256: build, the exchange takes the seed
    from that build's own spec.seed and sends sandboxSeedSource: build | declared; the turn result's
    sandbox.seed reports {source, buildId}, so a client can check its workspace was seeded from the
    build it pinned. A build with no seed sends none.
  • The baseline: right after the seed, the harness writes git rev-parse HEAD to .moca-baseline in
    the workspace (best effort).
  • The patch, after every turn of a templated session that leased its sandbox, before the lease is
    returned: one exec in the workspace, a throwaway index (GIT_INDEX_FILE), git read-tree $BASE,
    git add -A excluding .moca-* and .sh-config, git diff --cached --binary $BASE (the agent's own
    commits do not hide its work). TurnResult.workspace {baseline, patch | patchBase64, patchSha256, patchBytes}, also on the terminal done and error frames. Over SH_TURN_PATCH_MAX_BYTES (default
    4 MiB, at most the transport's output cap) the patch is left out, never cut: patchRefused: too_large with its sha256 and size. A capture that fails is {error} and never fails the turn.
  • session records on template/<id>: at create {sessionId, buildId, templateManifestHash, lease, state: created} and at delete {state: terminated, reason: deleted, endedAt}.
  • Async turns on a templated session (not in the branch yet; to add in this PR, the maintainers'
    choice of the two):
    • preferred: pin the build on the turn record at submission and return sandboxTemplate,
      sandboxSeed, sandboxSeedSource and sandboxBuildId from the dequeue exchange, as feat(control-plane): async turn submission and turn records (#387) #704's own
      note on live-versus-record fields suggests;
    • or, until then, refuse POST /v1/sessions/{id}/turns on a templated session with a clear 409, so
      no turn runs on the wrong hands.
  • mocactl (the client part, filed here): mocactl run --json prints workspace and sandbox.seed
    from the result and the terminal frames; src/api/frames.ts, harness.ts, types.ts,
    headless.ts, render/blocks.ts carry the fields.

What the PR will contain

One PR, about 900 added lines with tests, plus the async-turn part (sized when built) and the client part.

  • control-plane: src/exchange.ts (sessionSeed, the build seed), src/handlers.ts (the session
    records), src/provenance.ts (the session kind, if Record template provenance in a hash chain, verifiable offline #730 has not carried it), src/turn-exchange.ts
    (the async part).
  • harness: src/run-turn.ts (seedOrigin, SEED_BASELINE, captureTurnPatch, withWorkspacePatch),
    src/turn-stream.ts; test/turn-seed.test.ts, test/turn-stream.test.ts.
  • packages/k8s-sandbox/src/index.ts (exports DEFAULT_OUTPUT_CAP).
  • knative-server: src/turn-auth.ts, src/server.ts (sandboxSeedSource); test/server-process.test.ts.
  • mocactl: the files above; test/harness.test.ts, test/headless.test.ts.
  • docs: docs/api/harness-openapi.yaml (TurnResult, FrameDone, FrameError), the session templates
    design §5-§5.1, docs/reference/configuration.md (SH_TURN_PATCH_MAX_BYTES).

Depends on

#731, #730, #750. Related: #704 (async turns), #713/#714 (the dequeue exchange), #389 (the consumer).

How to verify

  • pnpm -C harness test -- turn-seed turn-stream
  • pnpm -C packages/control-plane test -- session-templates activation provenance
  • pnpm -C packages/knative-server test -- server-process
  • pnpm -C packages/mocactl test -- harness headless
  • On a combined deployment: a session pinned to a self-serve build whose spec seeds a repository; the
    first turn's sandbox.seed is {source: build, buildId}; an agent edit and a commit in that turn
    both appear in workspace.patch, which applies cleanly to the seed's commit; deleting the session
    writes its terminated record, and the template's export verifies offline.

Limits / out of scope

  • A session has no defined end: one never deleted gets no terminated record (and keeps its build in
    use for a retire).
  • The patch is per turn; a client takes the last turn's.
  • An injected sandbox (a leaf's) is its caller's; leaves take their own patch, as today.
  • A seed that leaves no git repository leaves no baseline, and the capture answers {error}.

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