From ba7db6d2191aa12b92c2ba96f611a740f081301b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:07:04 -0800 Subject: [PATCH 1/3] fix(sandbox-control): read the orgRole claim that Orca Cloud auth mints (#29) Auth mints the live org role as orgRole on sandbox tokens (same claim as billing tokens, stablyai/orca-cloud#695); the verifier read org_role, so admins would never have been recognized. --- cloud/apps/sandbox-control/src/auth.ts | 4 ++-- cloud/apps/sandbox-control/test/secrets.test.ts | 2 +- cloud/docs/vercel-driver-design.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cloud/apps/sandbox-control/src/auth.ts b/cloud/apps/sandbox-control/src/auth.ts index 1f98caae46c..9b8f61c9d2b 100644 --- a/cloud/apps/sandbox-control/src/auth.ts +++ b/cloud/apps/sandbox-control/src/auth.ts @@ -20,7 +20,7 @@ const ClaimsSchema = z.object({ sub: ID, org: ID.optional(), /** The caller's role in `org`, minted by Orca Cloud auth; absent means no admin rights. */ - org_role: z.enum(['owner', 'admin', 'member']).optional(), + orgRole: z.enum(['owner', 'admin', 'member']).optional(), purpose: z.literal('sandbox') }) @@ -81,7 +81,7 @@ async function verify( return { userId: claims.sub, accountId: accountIdFor(claims), - ...(claims.org ? { orgId: claims.org, ...(claims.org_role ? { orgRole: claims.org_role } : {}) } : {}) + ...(claims.org ? { orgId: claims.org, ...(claims.orgRole ? { orgRole: claims.orgRole } : {}) } : {}) } } catch { return null diff --git a/cloud/apps/sandbox-control/test/secrets.test.ts b/cloud/apps/sandbox-control/test/secrets.test.ts index 7e5b9ad975b..2cf8169b8bf 100644 --- a/cloud/apps/sandbox-control/test/secrets.test.ts +++ b/cloud/apps/sandbox-control/test/secrets.test.ts @@ -34,7 +34,7 @@ afterEach(() => { async function setup(overrides: Record = {}, deps: Parameters[1] = {}) { const h = await createHarness({ SECRETS_KEK: KEK, ...overrides }, deps) const as = async (sub: string, org?: string, role?: string) => - h.token(sub, org, 'sandbox', role ? { org_role: role } : {}) + h.token(sub, org, 'sandbox', role ? { orgRole: role } : {}) async function put(scope: string, name: string, value: string, bearer: string, repo?: string) { const query = repo ? `?repo=${encodeURIComponent(repo)}` : '' return h.call('PUT', `/v1/secrets/${scope}/${name}${query}`, { body: { value }, bearer }) diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index afd769c86df..9211dca6ad8 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -215,7 +215,7 @@ Product decision: "API-key injection" in the Conductor parity matrix (form 2 = t - **API** (desktop JWT): `GET /v1/secrets?scope=org|personal|repo[&repo=owner/name]`, `PUT /v1/secrets/:scope/:NAME` `{value}`, `DELETE …`. Write-only values: responses carry name, scope, kind, updatedAt, updatedBy and a last-4 hint for values ≥16 chars. Owners: org = the org account (`org:`), personal = `user:`, repo = - `#github.com//`. Org secrets and an org account's repo secrets need `org_role` owner/admin on + `#github.com//`. Org secrets and an org account's repo secrets need `orgRole` owner/admin on the token (any member lists names); personal secrets are the user's alone; dev tokens stay under `dev:`. - **Validation**: `^[A-Z_][A-Z0-9_]*$` ≤128; reserved names (PATH, HOME, NODE_OPTIONS, proxy/CA vars, GIT_SSH*, GH_TOKEN/GITHUB_TOKEN, …) and prefixes (LD_, DYLD_, ORCA_, VERCEL_, BASH_FUNC_, GIT_CONFIG). Values ≤8 KiB, no NUL; @@ -237,7 +237,7 @@ Product decision: "API-key injection" in the Conductor parity matrix (form 2 = t because (1) SSE model streams through Vercel forwardURL → Worker are not yet tested live, and (2) with a key routed, api.anthropic.com denies everything else, including a subscription OAuth Bearer or a plain `ANTHROPIC_AUTH_TOKEN` in the same VM. Enable on dev after a live streaming test with the rebuilt image. -- Needs: golden image rebuild (new `orca-sandbox-start`), and Orca Cloud auth minting `org_role` on sandbox tokens. +- Needs: golden image rebuild (new `orca-sandbox-start`), and Orca Cloud auth minting `orgRole` on sandbox tokens (stablyai/orca-cloud#695). ## GitHub installation binding, guardrails, retention (2026-11-09) From 04a3fd27b41e1c3096799d5bdf242e5cd764685f Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:09:43 -0800 Subject: [PATCH 2/3] feat(sandbox-control): dev-only shareable GitHub connect link (#30) --- cloud/apps/sandbox-control/src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 6477e77bbbc..61f94e173e6 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -386,6 +386,17 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) })) ?? fail('not_found', 404) ) } + if (url.pathname === '/v1/github/dev-connect' && env.SANDBOX_ENV !== 'prod') { + // Dev-only, link-shareable start of the user flow: mints a fresh 10-min state per click for a dev: + // account (desktop builds call POST /v1/github/connect instead). + const connect = githubConnect(env, deps) + const sub = url.searchParams.get('sub') ?? '' + if (!connect || !/^[A-Za-z0-9_.-]{1,64}$/.test(sub)) { + return fail('not_found', 404) + } + const state = await createConnectState(env.SANDBOX_DB, connect.stateSecret, { accountId: `dev:${sub}`, userId: sub }, (deps.now ?? Date.now)()) + return Response.redirect(authorizeUrl(connect, url.origin, state), 302) + } if (url.pathname === '/v1/github/connect' || url.pathname === '/v1/github/installations') { const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)()) if (!caller) { From 148567dbd36796736937dc2090631a96548005c8 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 04:20:09 -0800 Subject: [PATCH 3/3] perf(cloud): parallel boot prep, policy at create, clone alongside orcad, warmed V8 compile cache; Server-Timing phases (#31) * feat(sandbox-control): dev-only shareable GitHub connect link * perf(cloud): parallel boot prep, policy at create, clone alongside orcad, warmed V8 compile cache; phase timings (Server-Timing) --- .../sandbox-control/image/provision-golden.sh | 5 +- .../image/vm-scripts.generated.ts | 4 +- .../image/vm/orca-sandbox-start | 16 +- cloud/apps/sandbox-control/src/index.ts | 21 ++- cloud/apps/sandbox-control/src/lifecycle.ts | 166 ++++++++++++------ 5 files changed, 150 insertions(+), 62 deletions(-) diff --git a/cloud/apps/sandbox-control/image/provision-golden.sh b/cloud/apps/sandbox-control/image/provision-golden.sh index a5496de3d0e..604325c41ec 100755 --- a/cloud/apps/sandbox-control/image/provision-golden.sh +++ b/cloud/apps/sandbox-control/image/provision-golden.sh @@ -68,9 +68,10 @@ done printf 'export PATH=%s/tools/bin:%s/bin:$PATH\n' "$ORCA_HOME" "$ORCA_HOME" | sudo tee /etc/profile.d/orca.sh >/dev/null sudo chown -R root:root "$ORCA_HOME" -log 'smoke: orcad reaches ready on loopback' +log 'smoke: orcad reaches ready on loopback (and warms the V8 compile cache: ~1.5 s off every boot)' +sudo install -d -o "$(id -u)" -g "$(id -g)" "$ORCA_HOME/compile-cache" smoke_data="$(mktemp -d)" -ORCA_USER_DATA="$smoke_data" timeout 60 "$node" "$ORCA_HOME/runtime/orcad/orcad-server.js" \ +NODE_COMPILE_CACHE="$ORCA_HOME/compile-cache" ORCA_USER_DATA="$smoke_data" timeout 60 "$node" "$ORCA_HOME/runtime/orcad/orcad-server.js" \ --port 6799 --json >/tmp/orcad-smoke.out 2>/tmp/orcad-smoke.err & smoke_pid=$! ready=0 diff --git a/cloud/apps/sandbox-control/image/vm-scripts.generated.ts b/cloud/apps/sandbox-control/image/vm-scripts.generated.ts index 7f2c0fa3182..3a418347a75 100644 --- a/cloud/apps/sandbox-control/image/vm-scripts.generated.ts +++ b/cloud/apps/sandbox-control/image/vm-scripts.generated.ts @@ -2,5 +2,5 @@ import type { BuildInputs, GoldenImageManifest } from './golden-image' export const MANIFEST: GoldenImageManifest = {"imageVersion":"2026-11-08.1","runtime":"node24","region":"iad1","vcpus":4,"orcad":{"bundleUrl":null,"sha256":null},"gh":{"version":"2.103.3","sha256":"4c806978ffce1cec70853302a56d70ac6da4f19bae7adca4e1a8ab00633dd91d"},"agentCliPackages":["@anthropic-ai/claude-code@2.1.319","@openai/codex@0.166.0"]} -export const PROVISION_SCRIPT: string = "#!/usr/bin/env bash\n# Runs once inside the build sandbox (as the sandbox user, with sudo). Installs the pinned orcad\n# bundle, agent CLIs and Orca's VM entry points into /opt/orca, then smoke-tests orcad.\n# Inputs (env, from image/golden-image.manifest.json via build-golden-image.ts):\n# ORCAD_BUNDLE_URL ORCAD_BUNDLE_SHA256 [GH_VERSION GH_SHA256] AGENT_NPM_PACKAGES (space-separated\n# name@version) and ORCA_VM_SCRIPT_ (base64) for each /opt/orca/bin entry point.\nset -euo pipefail\nORCA_HOME=/opt/orca\nlog() { printf '[golden] %s\\n' \"$*\" >&2; }\n\nsudo mkdir -p \"$ORCA_HOME/bin\" \"$ORCA_HOME/runtime\" \"$ORCA_HOME/tools\"\nsudo chown -R \"$(id -u):$(id -g)\" \"$ORCA_HOME\"\n\nlog 'system packages'\nif command -v apt-get >/dev/null; then\n sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq\n sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \\\n ca-certificates curl git jq ripgrep build-essential python3 unzip util-linux procps >/dev/null\nelif command -v dnf >/dev/null; then\n sudo dnf install -y -q ca-certificates curl-minimal git jq gcc gcc-c++ make python3 unzip util-linux procps-ng >/dev/null\nelse\n log 'no supported package manager'; exit 1\nfi\n\nfetch_verified() { # url sha256 dest\n curl -fsSL --retry 3 -o \"$3\" \"$1\"\n echo \"$2 $3\" | sha256sum -c --quiet -\n}\n\nlog 'orcad bundle'\nbundle=\"$(mktemp -d)/orcad.tgz\"\nfetch_verified \"$ORCAD_BUNDLE_URL\" \"$ORCAD_BUNDLE_SHA256\" \"$bundle\"\ntar -xzf \"$bundle\" -C \"$ORCA_HOME/runtime\"\nrm -rf \"$(dirname \"$bundle\")\"\n# Release tarballs (orca-server-linux-*) may nest the server; normalize to runtime/orcad + runtime/runtimes.\nif [ ! -s \"$ORCA_HOME/runtime/orcad/orcad-server.js\" ]; then\n server=\"$(find \"$ORCA_HOME/runtime\" -name orcad-server.js -type f | head -n 1)\"\n [ -n \"$server\" ] || { log 'bundle has no orcad-server.js'; exit 1; }\n root=\"$(dirname \"$(dirname \"$server\")\")\"\n ln -sfn \"$(dirname \"$server\")\" \"$ORCA_HOME/runtime/orcad\"\n [ -d \"$ORCA_HOME/runtime/runtimes\" ] || ln -sfn \"$root/runtimes\" \"$ORCA_HOME/runtime/runtimes\"\nfi\ntest -s \"$ORCA_HOME/runtime/orcad/orcad-server.js\"\nnode=\"$(find \"$ORCA_HOME/runtime/runtimes\" -type f -name node | head -n 1)\"\ntest -x \"$node\"\n\nif [ -n \"${GH_VERSION:-}\" ]; then\n log \"gh $GH_VERSION\"\n fetch_verified \"https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz\" \\\n \"$GH_SHA256\" /tmp/gh.tgz\n tar -xzf /tmp/gh.tgz -C /tmp\n mkdir -p \"$ORCA_HOME/tools/bin\"\n install -m 0755 \"/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh\" \"$ORCA_HOME/tools/bin/gh\"\n rm -rf /tmp/gh.tgz \"/tmp/gh_${GH_VERSION}_linux_amd64\"\nfi\n\nlog 'agent CLIs'\n# The image's system npm installs them; agents later run on whatever node orcad puts on PATH.\n# shellcheck disable=SC2086 # word splitting is the package list\nnpm install --global --no-fund --no-audit --prefix \"$ORCA_HOME/tools\" $AGENT_NPM_PACKAGES\n\nlog 'Orca entry points'\nfor name in orca-sandbox-start orca-sandbox-drain orca-egress-probe orca-image-wipe; do\n var=\"ORCA_VM_SCRIPT_${name//-/_}\"\n printf '%s' \"${!var}\" | base64 -d >\"$ORCA_HOME/bin/$name\"\n chmod 0755 \"$ORCA_HOME/bin/$name\"\ndone\nprintf 'export PATH=%s/tools/bin:%s/bin:$PATH\\n' \"$ORCA_HOME\" \"$ORCA_HOME\" | sudo tee /etc/profile.d/orca.sh >/dev/null\nsudo chown -R root:root \"$ORCA_HOME\"\n\nlog 'smoke: orcad reaches ready on loopback'\nsmoke_data=\"$(mktemp -d)\"\nORCA_USER_DATA=\"$smoke_data\" timeout 60 \"$node\" \"$ORCA_HOME/runtime/orcad/orcad-server.js\" \\\n --port 6799 --json >/tmp/orcad-smoke.out 2>/tmp/orcad-smoke.err &\nsmoke_pid=$!\nready=0\nfor _ in $(seq 1 600); do\n if grep -q '\"orca_server_ready\"' /tmp/orcad-smoke.out 2>/dev/null; then ready=1; break; fi\n sleep 0.1\ndone\nkill \"$smoke_pid\" 2>/dev/null || true\nwait \"$smoke_pid\" 2>/dev/null || true\nrm -rf \"$smoke_data\" /tmp/orcad-smoke.out\nif [ \"$ready\" != 1 ]; then\n tail -n 40 /tmp/orcad-smoke.err >&2; exit 1\nfi\n\nlog 'versions'\nversions=\"{}\"\nfor bin in claude codex opencode gemini gh; do\n if [ -x \"$ORCA_HOME/tools/bin/$bin\" ]; then\n v=\"$(\"$ORCA_HOME/tools/bin/$bin\" --version 2>/dev/null | head -n 1 || echo unknown)\"\n versions=\"$(jq -c --arg k \"$bin\" --arg v \"$v\" '. + {($k): $v}' <<<\"$versions\")\"\n fi\ndone\njq -c --arg node \"$(\"$node\" --version)\" '{node: $node, tools: .}' <<<\"$versions\"\n" -export const VM_SCRIPT_SOURCES: BuildInputs['vmScripts'] = {"orca-sandbox-start":"#!/usr/bin/env bash\n# Starts orcad for this boot (idempotent) and prints its recipe line:\n# {\"schemaVersion\":1,\"pairingCode\":\"orca://pair?...\",\"projectRoot\":\"/vercel/sandbox/project\"}\n# Env from the control plane: ORCA_SANDBOX_ID, ORCA_PUBLIC_WSS, optional ORCA_REPO_URL/ORCA_REPO_REF,\n# ORCA_GITHUB_PLACEHOLDER, ORCA_SECRET_ENV (`NAME=` lines: org/personal/repo secrets for\n# orcad and its agents). Never prints anything else on stdout's last line.\nset -euo pipefail\numask 077\nORCA_HOME=/opt/orca\nSTATE=\"$HOME/.orca-sandbox\"\nPROJECT=/vercel/sandbox/project\nmkdir -p \"$STATE\"\nexec 9>\"$STATE/start.lock\"\nflock -w 60 9\n\nboot_id=\"$(cat /proc/sys/kernel/random/boot_id)\"\nif [ \"$(cat \"$STATE/boot-id\" 2>/dev/null || true)\" = \"$boot_id\" ] && [ -s \"$STATE/ready.json\" ] \\\n && kill -0 \"$(cat \"$STATE/orcad.pid\" 2>/dev/null || echo 0)\" 2>/dev/null; then\n tail -n 1 \"$STATE/ready.json\"\n exit 0\nfi\n# Resume = reboot from disk: locks and pids from the previous session name dead processes.\nrm -f \"$STATE/ready.json\" \"$STATE/orcad.pid\"\nprintf '%s' \"$boot_id\" >\"$STATE/boot-id\"\n\nif [ -n \"${ORCA_GIT_EXTRAHEADER:-}\" ]; then\n # Every github.com request carries the placeholder; the egress policy swaps in a 1h repo token.\n git config --global http.https://github.com/.extraheader \"$ORCA_GIT_EXTRAHEADER\"\nfi\nif [ ! -d \"$PROJECT\" ]; then\n if [ -n \"${ORCA_REPO_URL:-}\" ]; then\n git clone --quiet ${ORCA_REPO_REF:+--branch \"$ORCA_REPO_REF\"} -- \"$ORCA_REPO_URL\" \"$PROJECT\"\n else\n mkdir -p \"$PROJECT\"\n fi\nfi\n\nagent_env=()\nif [ -n \"${ORCA_GITHUB_PLACEHOLDER:-}\" ]; then\n # The egress layer swaps the placeholder for a short-lived GitHub token; none lives in the VM.\n agent_env+=(\"GH_TOKEN=$ORCA_GITHUB_PLACEHOLDER\" \"GITHUB_TOKEN=$ORCA_GITHUB_PLACEHOLDER\")\nfi\n\nif [ -n \"${ORCA_SECRET_ENV:-}\" ]; then\n # Secrets live only in orcad's process env (memory): nothing under $HOME or the project, so the\n # stop snapshot never carries them. Resolved per start by the control plane: no image rebuild.\n while IFS= read -r line; do\n name=\"${line%%=*}\" b64=\"${line#*=}\" # not IFS='=': read would strip base64 padding\n case \"$name\" in '' | *[!A-Z0-9_]* | [0-9]* | ORCA_* | PATH | HOME | LD_* | DYLD_*) continue ;; esac\n agent_env+=(\"$name=$(printf '%s' \"$b64\" | base64 -d)\")\n done < <(printf '%s\\n' \"$ORCA_SECRET_ENV\") # a pipe, not a here-string temp file\n unset ORCA_SECRET_ENV\nfi\n\nnode=\"$(find \"$ORCA_HOME/runtime/runtimes\" -type f -name node | head -n 1)\"\nenv -i HOME=\"$HOME\" USER=\"$(id -un)\" LANG=C.UTF-8 TERM=xterm-256color \\\n PATH=\"$ORCA_HOME/tools/bin:$ORCA_HOME/bin:/usr/local/bin:/usr/bin:/bin\" \\\n ORCA_USER_DATA=\"$HOME/.orcad-data\" ORCA_SANDBOX_ID=\"$ORCA_SANDBOX_ID\" \"${agent_env[@]}\" \\\n setsid nohup \"$node\" \"$ORCA_HOME/runtime/orcad/orcad-server.js\" \\\n --port 6768 --bind 0.0.0.0 --pairing-address \"$ORCA_PUBLIC_WSS\" \\\n --recipe-json --project-root \"$PROJECT\" \\\n >\"$STATE/ready.json\" 2>\"$STATE/orcad.log\" \"$STATE/orcad.pid\"\n\nfor _ in $(seq 1 1200); do\n if grep -q '\"pairingCode\"' \"$STATE/ready.json\" 2>/dev/null; then\n tail -n 1 \"$STATE/ready.json\"\n exit 0\n fi\n if ! kill -0 \"$(cat \"$STATE/orcad.pid\")\" 2>/dev/null; then\n echo 'orcad exited before ready' >&2\n tail -n 40 \"$STATE/orcad.log\" | sed -E 's#orca://[^[:space:]]+#[REDACTED_PAIRING]#g' >&2\n exit 1\n fi\n sleep 0.05\ndone\necho 'orcad start timed out' >&2\nexit 1\n","orca-sandbox-drain":"#!/usr/bin/env bash\n# Asks orcad to quit before the VM stops: its shutdown turns working agent chats into durable\n# restart offers and flushes terminal restore hints, which the desktop continues after resume.\nset -euo pipefail\nSTATE=\"$HOME/.orca-sandbox\"\nWAIT_SECONDS=\"${1:-280}\"\npid=\"$(cat \"$STATE/orcad.pid\" 2>/dev/null || true)\"\nif [ -z \"$pid\" ] || ! kill -0 \"$pid\" 2>/dev/null; then\n exit 0\nfi\nkill -TERM \"$pid\"\nfor _ in $(seq 1 $((WAIT_SECONDS * 10))); do\n if ! kill -0 \"$pid\" 2>/dev/null; then\n rm -f \"$STATE/orcad.pid\" \"$STATE/ready.json\"\n exit 0\n fi\n sleep 0.1\ndone\n# Leave it running: the provider stop ends it, and the drain already had its window.\necho 'orcad still draining at deadline' >&2\nexit 0\n","orca-egress-probe":"#!/usr/bin/env bash\n# Bounded Node fetch against each credential host. Vercel transform rules have made undici hang on\n# response bodies (no content-length / transfer-encoding); report it instead of letting agents hang.\n# Prints one JSON line: {\"host\":\"api.github.com\",\"status\":\"ok\"|\"egress_framing_hang\"|\"error\"}.\nset -euo pipefail\nnode=\"$(find /opt/orca/runtime/runtimes -type f -name node | head -n 1)\"\nfor host in \"$@\"; do\n \"$node\" -e '\n const host = process.argv[1]\n const timer = setTimeout(() => { console.log(JSON.stringify({ host, status: \"egress_framing_hang\" })); process.exit(0) }, 8000)\n fetch(`https://${host}/`, { headers: { authorization: process.env.ORCA_PROBE_AUTHORIZATION ?? \"\" } })\n .then((r) => r.arrayBuffer())\n .then(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: \"ok\" })) })\n .catch(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: \"error\" })) })\n ' \"$host\"\ndone\n","orca-image-wipe":"#!/usr/bin/env bash\n# Runs last before the golden snapshot. Every sandbox cloned from the image must start without an\n# orcad identity, device registry, agent sign-in or shell history (clone-safe identity, risk #1).\nset -euo pipefail\npkill -f orcad-server.js 2>/dev/null || true\nfor path in \\\n \"$HOME/.orcad-data\" \"$HOME/.orca-sandbox\" \"$HOME/.config/orca\" \"$HOME/.config/orca-dev\" \\\n \"$HOME/.claude\" \"$HOME/.claude.json\" \"$HOME/.codex\" \"$HOME/.config/gh\" \"$HOME/.gemini\" \\\n \"$HOME/.local/share/opencode\" \"$HOME/.npm\" \"$HOME/.cache\" \"$HOME/.bash_history\" \\\n /vercel/sandbox/project; do\n rm -rf -- \"$path\"\ndone\nsudo rm -rf /tmp/* /var/tmp/* 2>/dev/null || true\nfor leftover in \"$HOME/.orcad-data\" \"$HOME/.claude\" \"$HOME/.codex\" \"$HOME/.config/gh\"; do\n if [ -e \"$leftover\" ]; then\n echo \"identity survived wipe: $leftover\" >&2\n exit 1\n fi\ndone\necho '{\"wiped\":true}'\n"} +export const PROVISION_SCRIPT: string = "#!/usr/bin/env bash\n# Runs once inside the build sandbox (as the sandbox user, with sudo). Installs the pinned orcad\n# bundle, agent CLIs and Orca's VM entry points into /opt/orca, then smoke-tests orcad.\n# Inputs (env, from image/golden-image.manifest.json via build-golden-image.ts):\n# ORCAD_BUNDLE_URL ORCAD_BUNDLE_SHA256 [GH_VERSION GH_SHA256] AGENT_NPM_PACKAGES (space-separated\n# name@version) and ORCA_VM_SCRIPT_ (base64) for each /opt/orca/bin entry point.\nset -euo pipefail\nORCA_HOME=/opt/orca\nlog() { printf '[golden] %s\\n' \"$*\" >&2; }\n\nsudo mkdir -p \"$ORCA_HOME/bin\" \"$ORCA_HOME/runtime\" \"$ORCA_HOME/tools\"\nsudo chown -R \"$(id -u):$(id -g)\" \"$ORCA_HOME\"\n\nlog 'system packages'\nif command -v apt-get >/dev/null; then\n sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq\n sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \\\n ca-certificates curl git jq ripgrep build-essential python3 unzip util-linux procps >/dev/null\nelif command -v dnf >/dev/null; then\n sudo dnf install -y -q ca-certificates curl-minimal git jq gcc gcc-c++ make python3 unzip util-linux procps-ng >/dev/null\nelse\n log 'no supported package manager'; exit 1\nfi\n\nfetch_verified() { # url sha256 dest\n curl -fsSL --retry 3 -o \"$3\" \"$1\"\n echo \"$2 $3\" | sha256sum -c --quiet -\n}\n\nlog 'orcad bundle'\nbundle=\"$(mktemp -d)/orcad.tgz\"\nfetch_verified \"$ORCAD_BUNDLE_URL\" \"$ORCAD_BUNDLE_SHA256\" \"$bundle\"\ntar -xzf \"$bundle\" -C \"$ORCA_HOME/runtime\"\nrm -rf \"$(dirname \"$bundle\")\"\n# Release tarballs (orca-server-linux-*) may nest the server; normalize to runtime/orcad + runtime/runtimes.\nif [ ! -s \"$ORCA_HOME/runtime/orcad/orcad-server.js\" ]; then\n server=\"$(find \"$ORCA_HOME/runtime\" -name orcad-server.js -type f | head -n 1)\"\n [ -n \"$server\" ] || { log 'bundle has no orcad-server.js'; exit 1; }\n root=\"$(dirname \"$(dirname \"$server\")\")\"\n ln -sfn \"$(dirname \"$server\")\" \"$ORCA_HOME/runtime/orcad\"\n [ -d \"$ORCA_HOME/runtime/runtimes\" ] || ln -sfn \"$root/runtimes\" \"$ORCA_HOME/runtime/runtimes\"\nfi\ntest -s \"$ORCA_HOME/runtime/orcad/orcad-server.js\"\nnode=\"$(find \"$ORCA_HOME/runtime/runtimes\" -type f -name node | head -n 1)\"\ntest -x \"$node\"\n\nif [ -n \"${GH_VERSION:-}\" ]; then\n log \"gh $GH_VERSION\"\n fetch_verified \"https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz\" \\\n \"$GH_SHA256\" /tmp/gh.tgz\n tar -xzf /tmp/gh.tgz -C /tmp\n mkdir -p \"$ORCA_HOME/tools/bin\"\n install -m 0755 \"/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh\" \"$ORCA_HOME/tools/bin/gh\"\n rm -rf /tmp/gh.tgz \"/tmp/gh_${GH_VERSION}_linux_amd64\"\nfi\n\nlog 'agent CLIs'\n# The image's system npm installs them; agents later run on whatever node orcad puts on PATH.\n# shellcheck disable=SC2086 # word splitting is the package list\nnpm install --global --no-fund --no-audit --prefix \"$ORCA_HOME/tools\" $AGENT_NPM_PACKAGES\n\nlog 'Orca entry points'\nfor name in orca-sandbox-start orca-sandbox-drain orca-egress-probe orca-image-wipe; do\n var=\"ORCA_VM_SCRIPT_${name//-/_}\"\n printf '%s' \"${!var}\" | base64 -d >\"$ORCA_HOME/bin/$name\"\n chmod 0755 \"$ORCA_HOME/bin/$name\"\ndone\nprintf 'export PATH=%s/tools/bin:%s/bin:$PATH\\n' \"$ORCA_HOME\" \"$ORCA_HOME\" | sudo tee /etc/profile.d/orca.sh >/dev/null\nsudo chown -R root:root \"$ORCA_HOME\"\n\nlog 'smoke: orcad reaches ready on loopback (and warms the V8 compile cache: ~1.5 s off every boot)'\nsudo install -d -o \"$(id -u)\" -g \"$(id -g)\" \"$ORCA_HOME/compile-cache\"\nsmoke_data=\"$(mktemp -d)\"\nNODE_COMPILE_CACHE=\"$ORCA_HOME/compile-cache\" ORCA_USER_DATA=\"$smoke_data\" timeout 60 \"$node\" \"$ORCA_HOME/runtime/orcad/orcad-server.js\" \\\n --port 6799 --json >/tmp/orcad-smoke.out 2>/tmp/orcad-smoke.err &\nsmoke_pid=$!\nready=0\nfor _ in $(seq 1 600); do\n if grep -q '\"orca_server_ready\"' /tmp/orcad-smoke.out 2>/dev/null; then ready=1; break; fi\n sleep 0.1\ndone\nkill \"$smoke_pid\" 2>/dev/null || true\nwait \"$smoke_pid\" 2>/dev/null || true\nrm -rf \"$smoke_data\" /tmp/orcad-smoke.out\nif [ \"$ready\" != 1 ]; then\n tail -n 40 /tmp/orcad-smoke.err >&2; exit 1\nfi\n\nlog 'versions'\nversions=\"{}\"\nfor bin in claude codex opencode gemini gh; do\n if [ -x \"$ORCA_HOME/tools/bin/$bin\" ]; then\n v=\"$(\"$ORCA_HOME/tools/bin/$bin\" --version 2>/dev/null | head -n 1 || echo unknown)\"\n versions=\"$(jq -c --arg k \"$bin\" --arg v \"$v\" '. + {($k): $v}' <<<\"$versions\")\"\n fi\ndone\njq -c --arg node \"$(\"$node\" --version)\" '{node: $node, tools: .}' <<<\"$versions\"\n" +export const VM_SCRIPT_SOURCES: BuildInputs['vmScripts'] = {"orca-sandbox-start":"#!/usr/bin/env bash\n# Starts orcad for this boot (idempotent) and prints its recipe line:\n# {\"schemaVersion\":1,\"pairingCode\":\"orca://pair?...\",\"projectRoot\":\"/vercel/sandbox/project\"}\n# Env from the control plane: ORCA_SANDBOX_ID, ORCA_PUBLIC_WSS, optional ORCA_REPO_URL/ORCA_REPO_REF,\n# ORCA_GITHUB_PLACEHOLDER, ORCA_SECRET_ENV (`NAME=` lines: org/personal/repo secrets for\n# orcad and its agents). Never prints anything else on stdout's last line.\nset -euo pipefail\numask 077\nORCA_HOME=/opt/orca\nSTATE=\"$HOME/.orca-sandbox\"\nPROJECT=/vercel/sandbox/project\nmkdir -p \"$STATE\"\nexec 9>\"$STATE/start.lock\"\nflock -w 60 9\n\nboot_id=\"$(cat /proc/sys/kernel/random/boot_id)\"\nif [ \"$(cat \"$STATE/boot-id\" 2>/dev/null || true)\" = \"$boot_id\" ] && [ -s \"$STATE/ready.json\" ] \\\n && kill -0 \"$(cat \"$STATE/orcad.pid\" 2>/dev/null || echo 0)\" 2>/dev/null; then\n tail -n 1 \"$STATE/ready.json\"\n exit 0\nfi\n# Resume = reboot from disk: locks and pids from the previous session name dead processes.\nrm -f \"$STATE/ready.json\" \"$STATE/orcad.pid\"\nprintf '%s' \"$boot_id\" >\"$STATE/boot-id\"\n\nif [ -n \"${ORCA_GIT_EXTRAHEADER:-}\" ]; then\n # Every github.com request carries the placeholder; the egress policy swaps in a 1h repo token.\n git config --global http.https://github.com/.extraheader \"$ORCA_GIT_EXTRAHEADER\"\nfi\n# First boot clones while orcad starts (orcad only needs the directory to exist); ready waits for both.\nclone_pid=''\nif [ ! -d \"$PROJECT\" ]; then\n mkdir -p \"$PROJECT\"\n if [ -n \"${ORCA_REPO_URL:-}\" ]; then\n git clone --quiet ${ORCA_REPO_REF:+--branch \"$ORCA_REPO_REF\"} -- \"$ORCA_REPO_URL\" \"$PROJECT\" 2>\"$STATE/clone.log\" &\n clone_pid=$!\n fi\nfi\n\nagent_env=()\nif [ -n \"${ORCA_GITHUB_PLACEHOLDER:-}\" ]; then\n # The egress layer swaps the placeholder for a short-lived GitHub token; none lives in the VM.\n agent_env+=(\"GH_TOKEN=$ORCA_GITHUB_PLACEHOLDER\" \"GITHUB_TOKEN=$ORCA_GITHUB_PLACEHOLDER\")\nfi\n\nif [ -n \"${ORCA_SECRET_ENV:-}\" ]; then\n # Secrets live only in orcad's process env (memory): nothing under $HOME or the project, so the\n # stop snapshot never carries them. Resolved per start by the control plane: no image rebuild.\n while IFS= read -r line; do\n name=\"${line%%=*}\" b64=\"${line#*=}\" # not IFS='=': read would strip base64 padding\n case \"$name\" in '' | *[!A-Z0-9_]* | [0-9]* | ORCA_* | PATH | HOME | LD_* | DYLD_*) continue ;; esac\n agent_env+=(\"$name=$(printf '%s' \"$b64\" | base64 -d)\")\n done < <(printf '%s\\n' \"$ORCA_SECRET_ENV\") # a pipe, not a here-string temp file\n unset ORCA_SECRET_ENV\nfi\n\nnode=\"$(find \"$ORCA_HOME/runtime/runtimes\" -type f -name node | head -n 1)\"\nenv -i HOME=\"$HOME\" USER=\"$(id -un)\" LANG=C.UTF-8 TERM=xterm-256color \\\n PATH=\"$ORCA_HOME/tools/bin:$ORCA_HOME/bin:/usr/local/bin:/usr/bin:/bin\" \\\n ORCA_USER_DATA=\"$HOME/.orcad-data\" ORCA_SANDBOX_ID=\"$ORCA_SANDBOX_ID\" \\\n NODE_COMPILE_CACHE=\"$ORCA_HOME/compile-cache\" \"${agent_env[@]}\" \\\n setsid nohup \"$node\" \"$ORCA_HOME/runtime/orcad/orcad-server.js\" \\\n --port 6768 --bind 0.0.0.0 --pairing-address \"$ORCA_PUBLIC_WSS\" \\\n --recipe-json --project-root \"$PROJECT\" \\\n >\"$STATE/ready.json\" 2>\"$STATE/orcad.log\" \"$STATE/orcad.pid\"\n\nfor _ in $(seq 1 1200); do\n if grep -q '\"pairingCode\"' \"$STATE/ready.json\" 2>/dev/null; then\n if [ -n \"$clone_pid\" ] && ! wait \"$clone_pid\"; then\n echo 'repository clone failed' >&2\n tail -n 5 \"$STATE/clone.log\" >&2\n exit 1\n fi\n tail -n 1 \"$STATE/ready.json\"\n exit 0\n fi\n if ! kill -0 \"$(cat \"$STATE/orcad.pid\")\" 2>/dev/null; then\n echo 'orcad exited before ready' >&2\n tail -n 40 \"$STATE/orcad.log\" | sed -E 's#orca://[^[:space:]]+#[REDACTED_PAIRING]#g' >&2\n exit 1\n fi\n sleep 0.05\ndone\necho 'orcad start timed out' >&2\nexit 1\n","orca-sandbox-drain":"#!/usr/bin/env bash\n# Asks orcad to quit before the VM stops: its shutdown turns working agent chats into durable\n# restart offers and flushes terminal restore hints, which the desktop continues after resume.\nset -euo pipefail\nSTATE=\"$HOME/.orca-sandbox\"\nWAIT_SECONDS=\"${1:-280}\"\npid=\"$(cat \"$STATE/orcad.pid\" 2>/dev/null || true)\"\nif [ -z \"$pid\" ] || ! kill -0 \"$pid\" 2>/dev/null; then\n exit 0\nfi\nkill -TERM \"$pid\"\nfor _ in $(seq 1 $((WAIT_SECONDS * 10))); do\n if ! kill -0 \"$pid\" 2>/dev/null; then\n rm -f \"$STATE/orcad.pid\" \"$STATE/ready.json\"\n exit 0\n fi\n sleep 0.1\ndone\n# Leave it running: the provider stop ends it, and the drain already had its window.\necho 'orcad still draining at deadline' >&2\nexit 0\n","orca-egress-probe":"#!/usr/bin/env bash\n# Bounded Node fetch against each credential host. Vercel transform rules have made undici hang on\n# response bodies (no content-length / transfer-encoding); report it instead of letting agents hang.\n# Prints one JSON line: {\"host\":\"api.github.com\",\"status\":\"ok\"|\"egress_framing_hang\"|\"error\"}.\nset -euo pipefail\nnode=\"$(find /opt/orca/runtime/runtimes -type f -name node | head -n 1)\"\nfor host in \"$@\"; do\n \"$node\" -e '\n const host = process.argv[1]\n const timer = setTimeout(() => { console.log(JSON.stringify({ host, status: \"egress_framing_hang\" })); process.exit(0) }, 8000)\n fetch(`https://${host}/`, { headers: { authorization: process.env.ORCA_PROBE_AUTHORIZATION ?? \"\" } })\n .then((r) => r.arrayBuffer())\n .then(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: \"ok\" })) })\n .catch(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: \"error\" })) })\n ' \"$host\"\ndone\n","orca-image-wipe":"#!/usr/bin/env bash\n# Runs last before the golden snapshot. Every sandbox cloned from the image must start without an\n# orcad identity, device registry, agent sign-in or shell history (clone-safe identity, risk #1).\nset -euo pipefail\npkill -f orcad-server.js 2>/dev/null || true\nfor path in \\\n \"$HOME/.orcad-data\" \"$HOME/.orca-sandbox\" \"$HOME/.config/orca\" \"$HOME/.config/orca-dev\" \\\n \"$HOME/.claude\" \"$HOME/.claude.json\" \"$HOME/.codex\" \"$HOME/.config/gh\" \"$HOME/.gemini\" \\\n \"$HOME/.local/share/opencode\" \"$HOME/.npm\" \"$HOME/.cache\" \"$HOME/.bash_history\" \\\n /vercel/sandbox/project; do\n rm -rf -- \"$path\"\ndone\nsudo rm -rf /tmp/* /var/tmp/* 2>/dev/null || true\nfor leftover in \"$HOME/.orcad-data\" \"$HOME/.claude\" \"$HOME/.codex\" \"$HOME/.config/gh\"; do\n if [ -e \"$leftover\" ]; then\n echo \"identity survived wipe: $leftover\" >&2\n exit 1\n fi\ndone\necho '{\"wiped\":true}'\n"} diff --git a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start index 17b7e5f2202..86c283169fc 100755 --- a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start +++ b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start @@ -27,11 +27,13 @@ if [ -n "${ORCA_GIT_EXTRAHEADER:-}" ]; then # Every github.com request carries the placeholder; the egress policy swaps in a 1h repo token. git config --global http.https://github.com/.extraheader "$ORCA_GIT_EXTRAHEADER" fi +# First boot clones while orcad starts (orcad only needs the directory to exist); ready waits for both. +clone_pid='' if [ ! -d "$PROJECT" ]; then + mkdir -p "$PROJECT" if [ -n "${ORCA_REPO_URL:-}" ]; then - git clone --quiet ${ORCA_REPO_REF:+--branch "$ORCA_REPO_REF"} -- "$ORCA_REPO_URL" "$PROJECT" - else - mkdir -p "$PROJECT" + git clone --quiet ${ORCA_REPO_REF:+--branch "$ORCA_REPO_REF"} -- "$ORCA_REPO_URL" "$PROJECT" 2>"$STATE/clone.log" & + clone_pid=$! fi fi @@ -55,7 +57,8 @@ fi node="$(find "$ORCA_HOME/runtime/runtimes" -type f -name node | head -n 1)" env -i HOME="$HOME" USER="$(id -un)" LANG=C.UTF-8 TERM=xterm-256color \ PATH="$ORCA_HOME/tools/bin:$ORCA_HOME/bin:/usr/local/bin:/usr/bin:/bin" \ - ORCA_USER_DATA="$HOME/.orcad-data" ORCA_SANDBOX_ID="$ORCA_SANDBOX_ID" "${agent_env[@]}" \ + ORCA_USER_DATA="$HOME/.orcad-data" ORCA_SANDBOX_ID="$ORCA_SANDBOX_ID" \ + NODE_COMPILE_CACHE="$ORCA_HOME/compile-cache" "${agent_env[@]}" \ setsid nohup "$node" "$ORCA_HOME/runtime/orcad/orcad-server.js" \ --port 6768 --bind 0.0.0.0 --pairing-address "$ORCA_PUBLIC_WSS" \ --recipe-json --project-root "$PROJECT" \ @@ -64,6 +67,11 @@ echo $! >"$STATE/orcad.pid" for _ in $(seq 1 1200); do if grep -q '"pairingCode"' "$STATE/ready.json" 2>/dev/null; then + if [ -n "$clone_pid" ] && ! wait "$clone_pid"; then + echo 'repository clone failed' >&2 + tail -n 5 "$STATE/clone.log" >&2 + exit 1 + fi tail -n 1 "$STATE/ready.json" exit 0 fi diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 61f94e173e6..ee32525873c 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -31,7 +31,8 @@ import { resumeSandbox, rolloverSandbox, suspendSandbox, - type LifecycleDeps + type LifecycleDeps, + type Ready } from './lifecycle' import { createGitHubAppMinter, type GitHubAppMinter } from './github-app' import { @@ -245,6 +246,18 @@ function devCredentials(env: Env): CredentialBroker { return { authorizationFor: async (_row, host) => (host === 'api.github.com' ? value : null) } } +/** Ready answers carry phase timings as Server-Timing so clients and dashboards can see the slow step. */ +function readyResponse(ready: Ready): Response { + const response = json(readyView(ready.row, ready.connection)) + const timing = Object.entries(ready.timings ?? {}) + .map(([phase, ms]) => `${phase};dur=${ms}`) + .join(', ') + if (timing) { + response.headers.set('server-timing', timing) + } + return response +} + function json(body: unknown, status = 200): Response { return Response.json(body, { status, headers: { 'cache-control': 'no-store' } }) } @@ -456,7 +469,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) return fail('invalid_request', 400) } const ready = await createSandbox(life, caller, body.data) - return json(readyView(ready.row, ready.connection)) + return readyResponse(ready) } const row = await getSandbox(env.SANDBOX_DB, id) // Another account's sandbox is indistinguishable from a missing one. @@ -473,11 +486,11 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) return json({ sandbox: sandboxView(await suspendSandbox(life, row)) }) case 'POST resume': { const ready = await resumeSandbox(life, row) - return json(readyView(ready.row, ready.connection)) + return readyResponse(ready) } case 'POST rollover': { const ready = await rolloverSandbox(life, row) - return json(readyView(ready.row, ready.connection)) + return readyResponse(ready) } case 'POST lease': { const body = LeaseRequestSchema.safeParse(await readJson(request)) diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 58ecbedabbf..0c588ad6d00 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -76,7 +76,41 @@ export class LifecycleError extends Error { } } -export type Ready = { row: SandboxRow; connection: OrcadReady } +export type Ready = { row: SandboxRow; connection: OrcadReady; timings?: Record } + +/** Phase durations for one lifecycle call (logged + Server-Timing); counts only, no ids or secrets. */ +export function phaseTimer(now: () => number = Date.now) { + const timings: Record = {} + return { + timings, + async time(phase: string, work: () => Promise): Promise { + const started = now() + try { + return await work() + } finally { + timings[phase] = now() - started + } + } + } +} + +type BootPrep = { + secrets: { env: Record; hosts: string[] } + minted: { token: string; expiresAt: number } | null +} + +/** Everything a boot needs that does not depend on the VM: resolved concurrently with the VM work. */ +async function prepareBoot(deps: LifecycleDeps, row: SandboxRow): Promise { + const [secrets, minted] = await Promise.all([ + sessionSecrets(deps, row), + deps.github && row.repo_url ? deps.github.tokenForRepo(row.repo_url, row.account_id) : Promise.resolve(null) + ]) + return { secrets, minted } +} + +function routedRow(row: SandboxRow, prep: BootPrep): SandboxRow { + return { ...row, egress_secret_hosts: prep.secrets.hosts.join(',') || null } +} function randomId(): string { const bytes = crypto.getRandomValues(new Uint8Array(12)) @@ -158,9 +192,11 @@ export function guardrails(deps: LifecycleDeps): GuardrailConfig { } async function requireStart(deps: LifecycleDeps, accountId: string, userId: string, machineSize: MachineSize) { - await assertStartsOpen(deps.db, guardrails(deps)) - const runningSandboxes = await countActiveSandboxes(deps.db, accountId) - const runningSandboxesForUser = await countActiveSandboxesForUser(deps.db, accountId, userId) + const [, runningSandboxes, runningSandboxesForUser] = await Promise.all([ + assertStartsOpen(deps.db, guardrails(deps)), + countActiveSandboxes(deps.db, accountId), + countActiveSandboxesForUser(deps.db, accountId, userId) + ]) const decision = await deps.entitlements.checkStart({ accountId, machineSize, @@ -180,24 +216,37 @@ async function requireStart(deps: LifecycleDeps, accountId: string, userId: stri }) } -async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandboxState, repo?: { url: string; ref?: string }): Promise { +async function boot( + deps: LifecycleDeps, + row: SandboxRow, + state: ProviderSandboxState, + prep: BootPrep, + options: { repo?: { url: string; ref?: string }; policyApplied: boolean; timer: ReturnType } +): Promise { if (state.sandbox.tags.orcaSandbox !== row.id) { throw new LifecycleError('conflict', 'ownership_mismatch') } - const secrets = await sessionSecrets(deps, row) - const routed: SandboxRow = { ...row, egress_secret_hosts: secrets.hosts.join(',') || null } - const git = await refreshGitCredentials(deps, routed, state.session.id) - if (!git && (routed.egress_secret_hosts || row.egress_secret_hosts)) { - // Model-key routes must be live before any agent starts; the git refresh already wrote them. - await deps.provider.updateNetworkPolicy(state.session.id, egressPolicy(deps, routed)) - } - const connection = await startOrcad(deps.provider, state, { - sandboxId: row.id, - repo, - githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderToken(row) : undefined, - gitExtraHeader: git ? `Authorization: ${gitBasicAuthorization(githubPlaceholderToken(row))}` : undefined, - secretEnv: secrets.env - }) + const routed = routedRow(row, prep) + const git = prep.minted ? { expiresAt: prep.minted.expiresAt } : null + // A resumed session keeps its last policy (an expired repo token); swap it in while orcad boots. + // Nothing in the VM uses git or model keys before orcad answers and the desktop attaches. + const needsPolicy = !options.policyApplied && (git || routed.egress_secret_hosts || row.egress_secret_hosts) + const [, connection] = await Promise.all([ + needsPolicy + ? options.timer.time('policy', () => + deps.provider.updateNetworkPolicy(state.session.id, egressPolicy(deps, routed, prep.minted?.token)) + ) + : Promise.resolve(), + options.timer.time('orcad', () => + startOrcad(deps.provider, state, { + sandboxId: row.id, + repo: options.repo, + githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderToken(row) : undefined, + gitExtraHeader: git ? `Authorization: ${gitBasicAuthorization(githubPlaceholderToken(row))}` : undefined, + secretEnv: prep.secrets.env + }) + ) + ]) const running = await claim(deps, row, { status: 'running', last_error: null, @@ -206,7 +255,7 @@ async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandbox egress_secret_hosts: routed.egress_secret_hosts, ...sessionFields(deps, state) }) - return { row: running, connection } + return { row: running, connection, timings: options.timer.timings } } /** @@ -260,42 +309,52 @@ export async function createSandbox( } return { row: existing, connection: await startOrcad(deps.provider, state, { sandboxId: existing.id }) } } - await requireStart(deps, caller.accountId, caller.userId, request.machineSize) + const timer = phaseTimer(deps.now) + await timer.time('entitlement', () => requireStart(deps, caller.accountId, caller.userId, request.machineSize)) const id = randomId() - const row = await insertSandbox( - deps.db, - { - id, - account_id: caller.accountId, - user_id: caller.userId, - workspace_id: request.workspaceId, - provider: 'vercel', - provider_name: `orca-${deps.config.environment}-${id}`, - machine_size: request.machineSize, - region: request.region ?? deps.config.defaultRegion, - repo_url: request.repo?.url ?? null - }, - deps.now() - ) + // Write-ahead row and the VM-independent prep (secrets, repo token) run together. + const [row, prep] = await timer.time('prepare', async () => { + const inserted = await insertSandbox( + deps.db, + { + id, + account_id: caller.accountId, + user_id: caller.userId, + workspace_id: request.workspaceId, + provider: 'vercel', + provider_name: `orca-${deps.config.environment}-${id}`, + machine_size: request.machineSize, + region: request.region ?? deps.config.defaultRegion, + repo_url: request.repo?.url ?? null + }, + deps.now() + ) + return [inserted, await prepareBoot(deps, inserted)] as const + }) let state: ProviderSandboxState try { - state = await deps.provider.createSandbox({ - name: row.provider_name, - snapshotId: deps.config.goldenSnapshotId, - vcpus: VCPUS[row.machine_size], - region: row.region, - timeoutMs: LEASE_MS, - ports: [ORCAD_PORT], - networkPolicy: egressPolicy(deps, row), - snapshotExpirationMs: deps.config.snapshotExpirationMs ?? DEFAULT_SNAPSHOT_EXPIRATION_MS, - tags: { orcaSandbox: row.id, orcaEnv: deps.config.environment } - }) + // The full policy (repo token, model-key routes) goes in at create: no second policy round trip. + state = await timer.time('vm', () => + deps.provider.createSandbox({ + name: row.provider_name, + snapshotId: deps.config.goldenSnapshotId, + vcpus: VCPUS[row.machine_size], + region: row.region, + timeoutMs: LEASE_MS, + ports: [ORCAD_PORT], + networkPolicy: egressPolicy(deps, routedRow(row, prep), prep.minted?.token), + snapshotExpirationMs: deps.config.snapshotExpirationMs ?? DEFAULT_SNAPSHOT_EXPIRATION_MS, + tags: { orcaSandbox: row.id, orcaEnv: deps.config.environment } + }) + ) } catch (error) { await failAndRelease(deps, row) throw error } try { - return await boot(deps, row, state, request.repo) + const ready = await boot(deps, row, state, prep, { repo: request.repo, policyApplied: true, timer }) + console.log(JSON.stringify({ event: 'lifecycle_timing', op: 'create', ms: timer.timings })) + return ready } catch (error) { await failAndRelease(deps, (await getSandbox(deps.db, row.id)) ?? row) throw error @@ -379,13 +438,20 @@ export async function resumeSandbox(deps: LifecycleDeps, row: SandboxRow): Promi } async function wake(deps: LifecycleDeps, row: SandboxRow): Promise { - const state = await deps.provider.getSandbox(row.provider_name, { resume: true }) + const timer = phaseTimer(deps.now) + // The provider boot is the long pole; secrets and the repo token resolve alongside it. + const [state, prep] = await Promise.all([ + timer.time('vm', () => deps.provider.getSandbox(row.provider_name, { resume: true })), + timer.time('prepare', () => prepareBoot(deps, row)) + ]) if (!state) { await claim(deps, row, { status: 'failed', last_error: 'provider_sandbox_missing' }) throw new LifecycleError('conflict', 'environment_missing') } try { - return await boot(deps, row, state) + const ready = await boot(deps, row, state, prep, { policyApplied: false, timer }) + console.log(JSON.stringify({ event: 'lifecycle_timing', op: 'wake', ms: timer.timings })) + return ready } catch (error) { // Leave it resumable: the disk is intact, only this boot failed. const current = await getSandbox(deps.db, row.id)