Skip to content
Delegate

ArchitectureADR 0011

Architecture · Decision record 0011

ADR 0011: Ship-a-PR with a hand-written Engineer loop, Vercel Sandbox and a GitHub App

On this page
  • Status: Accepted
  • Date: 2026-11-25
  • Amends: SPEC "Ship-a-PR: an Engineer sub-agent on the Claude Agent SDK inside a sandbox" and the matching exception in SPEC §Architecture and ADR 0001

Context#

Ship-a-PR (M5) takes one improvement from a Repo X-ray, implements it in an isolated copy of the repository, runs the repo's own tests and lint, and opens a pull request on a repository the owner has authorised, after a human approves the diff. Any other repository gets a downloadable patch and test report instead.

The spec planned the Engineer sub-agent on the Claude Agent SDK. Three constraints rule that out:

  1. $0, keyless (ADR 0003). The Agent SDK drives Claude models only, which need a paid key. Delegate's default chains are free open models behind our router; an Engineer that only works with a paid key would make the hero flow unusable on the default deployment.
  2. Controls live in our loop (ADR 0001). Capability checks, the policy engine, budgets and typed RunEvents sit around every tool call in runAgentLoop. The Agent SDK runs its own loop and its own tools (shell, file edits), which is exactly where those controls must be.
  3. Serverless budget. Production is Vercel Hobby: 300 s per function, resumed from the event log (ADR 0007). An external agent process holds state we cannot checkpoint into run_events.

Decision#

  1. The Engineer is our own loop. It is runAgentLoop with five sandbox tools (read_file, list_dir, search, write_file, run), the worker role from the router, and an outer verify loop: after the model stops, the runtime runs the detected test command itself; failing output goes back to the model for another round. Rounds, iterations per round, tokens (the run budget) and wall time are all capped. The prompt is packages/runtime/prompts/ship-a-pr.engineer.v1.md.
  2. Sandbox interface, two implementations. Sandbox exposes exec with argv (never a shell string), file read/write and a two-phase network policy. VercelSandbox (@vercel/sandbox, free on Hobby: 5 CPU-hours a month, 45 minute sessions, over-quota pauses rather than bills; OIDC auth on Vercel) is the production implementation: network open to GitHub and package registries only while cloning and installing, then restricted to package registries. LocalProcessSandbox (temporary directory, child_process with a scrubbed environment and hard timeouts) is for development and tests; it is not an isolation boundary and refuses to start when NODE_ENV=production. DELEGATE_SANDBOX=vercel|local selects one; the default is local in development and vercel in production. If Vercel Sandbox is unavailable, the run fails with a clear error; untrusted code never runs in the web function.
  3. The Engineer is an unprivileged agent. A coding agent must read the repository, which is untrusted content, so the quarantined-reader rule (ADR 0002) cannot apply to it. Instead the Engineer holds no privilege worth stealing: its only capabilities are sandbox.read, sandbox.write and sandbox.exec, which reach nothing outside the sandbox; it sees no secrets; and nothing it produces leaves the sandbox until a human has approved the exact diff.
  4. Confinement in code. Every path is checked lexically (no absolute paths, no .., no .git, no .github/workflows) and then resolved inside the sandbox (realpath) so a symlink cannot point a write outside the repository. run accepts only the commands detected from package.json scripts, pyproject.toml or the Makefile plus a read-only pair (git status --short, git diff --stat), each with a per-command timeout, as argv without a shell. Changes made by installing dependencies are snapshotted before the Engineer starts and excluded from the diff. The final diff is checked again before it is shown for approval: no workflow files, no symlinks, no submodules, size-capped.
  5. No credentials in the sandbox. The sandbox gets no environment secrets and no git credentials. The server reads the changed files out of the sandbox and creates the commit itself through the GitHub git data API (blobs, tree, commit, ref) and then the pull request.
  6. GitHub App authorisation. The owner creates a GitHub App (Contents and Pull requests read & write, Metadata read, no webhook) and installs it only on chosen repositories. The server signs an RS256 JWT with node:crypto, looks up the installation for the target repository, and mints an installation token scoped to that one repository with only contents: write and pull_requests: write. Tokens are never logged, persisted or emitted; they live for one request sequence.
  7. Approval is a gate in code. github.open_pr has capability repo.write, a side-effect capability the policy baseline always routes to require_approval. The mission emits ship.approval.requested with the diff and the before/after test results and waits for a human decision; a timeout is a rejection. Which installation may be used is decided outside the mission: it takes an injected findInstallation(owner, repo). The web app scopes that lookup to the visitor's own workspace (installations they connected through "Connect GitHub"), so a visitor gets real pull requests on repositories their own installation covers and a patch everywhere else, even if they paste someone else's repository URL. Owner mode is the second source: the owner's own App installation is looked up directly. Without either, the guest policy (ship-demo) denies github.open_pr outright; a connected visitor runs under ship-connected (approval required, zero spend), the owner under ship-owner. The installation token is always minted for the one input repository, so an installation that does not cover it fails closed to a patch.
  8. Durable steps. The mission is a fixed plan (prepare → baseline → implement → verify → ship) on runPlannedMission. Each step's state is an artifact in the log (workspace, test reports, diff), so a run cut at 300 s resumes: the sandbox is reattached by id or rebuilt from the recorded base commit plus the recorded patch.

Consequences#

  • The Engineer is weaker than a frontier coding agent on free models. It suits small, test-backed improvements (effort: S|M), which is what Repo X-ray proposes.
  • We own the coding loop's tools and their security tests; the red-team suite gains sandbox-escape, exfiltration and approval-bypass cases for them.
  • Owner mode is a development-only flag (DELEGATE_OWNER_MODE, ignored in production), so real pull requests are opened from the owner's own environment; the production deployment produces patches. Changing that needs real owner authentication (M7 RBAC), not a flag.
  • Vercel Sandbox is the one external service on this path. It is free on Hobby and needs no extra account beyond the Vercel project the app already deploys to; self-hosters without it can run DELEGATE_SANDBOX=local only on machines they trust.