Merge pull request #32 from nwparker/cloud/core-sync

chore(cloud): bring core sandbox-control #29-#31 into integration
This commit is contained in:
Neil
2026-11-09 04:25:51 -08:00
committed by GitHub
8 changed files with 166 additions and 67 deletions
@@ -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
File diff suppressed because one or more lines are too long
@@ -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
+2 -2
View File
@@ -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
+28 -4
View File
@@ -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' } })
}
@@ -386,6 +399,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:<sub>
// 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) {
@@ -445,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.
@@ -462,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))
+116 -50
View File
@@ -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<string, number> }
/** 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<string, number> = {}
return {
timings,
async time<T>(phase: string, work: () => Promise<T>): Promise<T> {
const started = now()
try {
return await work()
} finally {
timings[phase] = now() - started
}
}
}
}
type BootPrep = {
secrets: { env: Record<string, string>; 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<BootPrep> {
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<Ready> {
async function boot(
deps: LifecycleDeps,
row: SandboxRow,
state: ProviderSandboxState,
prep: BootPrep,
options: { repo?: { url: string; ref?: string }; policyApplied: boolean; timer: ReturnType<typeof phaseTimer> }
): Promise<Ready> {
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<Ready> {
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)
@@ -34,7 +34,7 @@ afterEach(() => {
async function setup(overrides: Record<string, string> = {}, deps: Parameters<typeof createHarness>[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 })
+2 -2
View File
@@ -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:<id>`), personal = `user:<sub>`, repo =
`<account>#github.com/<owner>/<repo>`. Org secrets and an org account's repo secrets need `org_role` owner/admin on
`<account>#github.com/<owner>/<repo>`. 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)