From 42570c32adf6ea16b7d8bb817c66a0fe2b86f150 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:00:48 -0800 Subject: [PATCH 01/18] docs(cloud): white-label Vercel sandbox driver design (#8) --- cloud/docs/vercel-driver-design.md | 158 +++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 cloud/docs/vercel-driver-design.md diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md new file mode 100644 index 00000000000..f05078eee4d --- /dev/null +++ b/cloud/docs/vercel-driver-design.md @@ -0,0 +1,158 @@ +# Orca Cloud sandboxes on Vercel — white-label driver design (v0) + +Status: design + mock-tested code in `nwparker/orca-private` (integration branch `nwparker/vercel-driver`). +Nothing here has touched a real Vercel or Cloudflare resource. Inputs: bake-off (`reports/provider-bakeoff.md`), +`conductor-parity-matrix.html` (Vercel-first, 24h cap), `vm-just-works.html`, `reports/credentials.md`, +stablyai/orca#20777 (recipe that talks to Vercel with the user's own account), billing design +(`cloud/docs/accounts-billing-design.md`). + +## Goal and non-goals + +- One click "Orca Cloud" workspace. Orca's Vercel Pro team owns every sandbox. Users never see a Vercel account, + token, dashboard or brand. They pay Orca (billing Worker), and Orca pays Vercel. +- Not in v0: memory pause (Vercel has none), BYO Vercel account (that is #20777's recipe path), live fork, Drives. + +## Shape + +``` +desktop (Electron main) Orca sandbox control plane (CF Worker) Vercel (Orca's team) + HostedSandboxDriver ──HTTPS, Orca JWT────────▶ /v1/sandboxes/* ──Vercel token (secret)──▶ /v2/sandboxes/* + implements EnvironmentProviderDriver D1: sandboxes, sessions, usage cursor persistent sandbox + └ recipe adapter → existing runtime record billing: /v1/internal/entitlements/check, per workspace, + / pairing / resume pipeline /v1/internal/usage (CONTROL_PLANE_TOKEN) golden snapshot + desktop ◀───────── wss pairing (orcad, E2EE) ─────────────────────────────────────────────▶ orcad :6768 +``` + +- **Control plane = Cloudflare Worker + D1** (`cloud/apps/sandbox-control`). Same stack as billing and relay-edge, + so one toolchain, JWKS auth pattern and deploy story. It is the only holder of `VERCEL_TOKEN`/team/project. + Workers can't run `@vercel/sandbox` (undici, fs, xdg paths), so the Worker calls Vercel's REST API (`/v2`, `/v3` + endpoints read from SDK 3.9.1's api-client) through a `SandboxProviderApi` interface. Tests use an in-memory fake; + the REST client is tested against a fake `fetch`. A Durable Object per sandbox is the upgrade path if D1 + conditional updates prove too weak for concurrent lifecycle calls (v0 uses a `generation` compare-and-set). +- **Desktop** holds only a short-lived Orca JWT (`aud=orca-sandbox`, minted by Orca Cloud auth like the billing + token; until that endpoint exists the token provider is injected). It never sees a Vercel credential. +- **Integration with existing VM code**: the hosted driver returns the same `orca-server` recipe result + (`pairingCode`, `projectRoot`, `userData`) a shell recipe prints, so write-ahead records (#39241), timeouts + (#39238), pairing reuse on resume (#39225), host health and sleep/wake work unchanged. The adapter lives in + `src/main/environment-providers/environment-provider-recipe-adapter.ts`; recipe catalog/UI wiring is follow-up. + +## Desktop interface (`src/main/environment-providers/`) + +`EnvironmentProviderDriver`: `create(spec)`, `resume(handle)`, `suspend(handle)`, `destroy(handle)`, +`status(handle)`, optional `renewLease(handle, ms)` and `rollover(handle)`; every call takes +`{signal, onProgress(phase)}`. Handles are `{driverId, environmentId, driverState}` (opaque JSON persisted in the +runtime record's `userData`). Capability flags drive UX and schedulers: + +| flag | Vercel hosted | +|---|---| +| `memoryPause` | false (stop = reboot, PTYs lost; measured) | +| `maxSessionMs` | 24h (Pro). Scheduler forces rollover at 23h50m | +| `fsPersistOnStop` | true (auto snapshot on stop, keepLastSnapshots 2) | +| `egressInjection` | true | +| `idleStopMs` | 20 min default (Orca-owned, not Vercel's timeout) | + +Errors are `EnvironmentProviderError{code}` with a closed code set (`entitlement_denied:`, +`not_found`, `conflict`, `unavailable`, `timeout`, `unauthorized`, `invalid_response`). A driver contract kit +(`environment-provider-driver-contract.ts`) runs the lifecycle against any driver; the hosted driver runs it +against a fake control plane. + +## Control-plane API (Worker) + +All desktop routes require the Orca JWT; the account is `org:` or `user:` (same rule as billing). + +| route | does | +|---|---| +| `POST /v1/sandboxes {workspaceId, machineSize, region?, repo?}` | entitlement check → write-ahead row → create from golden snapshot → start orcad → `{sandbox, connection, session}` | +| `GET /v1/sandboxes[/:id]` | status from D1 + provider reconcile | +| `POST /v1/sandboxes/:id/suspend` | drain orcad → stop session (auto snapshot) → settle usage | +| `POST /v1/sandboxes/:id/resume` | entitlement check → resume → start orcad → connection (same pairing; identity on disk) | +| `POST /v1/sandboxes/:id/lease {extendMs}` | `extend-timeout`, clamped to the session deadline | +| `POST /v1/sandboxes/:id/rollover` | drain → stop → resume → start orcad (24h cap path) | +| `DELETE /v1/sandboxes/:id` | delete with `deleteOrphanSnapshots`, then verify every recorded snapshot is gone | +| `POST /v1/egress/proxy` | Vercel `forwardURL` target (see egress) | +| cron (5 min) | settle usage, stop sandboxes past idle/deadline or whose account lost entitlement, sweep stuck `creating` rows | + +Vercel resource naming: `orca-` where `sandboxId` is our random id (never user text). Tags: `orcaSandbox`, +`orcaEnv` (prod/dev). Ownership is checked against D1 + the name; nothing is ever deleted by prefix. +Create params: `source: {type:'snapshot', snapshotId: GOLDEN_SNAPSHOT_ID}`, `persistent: true`, +`ports: [6768]`, `resources.vcpus` from machine size (standard 4, large 8; xlarge 16 needs Enterprise), +`timeout` = Orca lease (30 min, renewed), `keepLastSnapshots: {count: 2}`, `snapshotExpiration: 0` while live +(finite TTL after archive), `region` default `iad1`, `networkPolicy` from the policy compiler. + +**Entitlements**: `POST billing /v1/internal/entitlements/check {accountId, machineSize, runningSandboxes}` before +create/resume. If billing is unreachable or unconfigured the Worker refuses (fail closed); a stub client is used only +when `ENTITLEMENTS_MODE=stub` in local dev. **Usage**: per-session segments `[lastSettledAt, now]` (≤24h) sent to +`/v1/internal/usage` with `usageId = ::` (idempotent). If the answer says new +starts are no longer allowed for `included_hours_exhausted`/`overage_cap_reached`, running sandboxes are drained and +stopped; `payment_past_due` keeps running (grace is an open billing decision). + +**Pairing material**: the Worker runs `orca-sandbox-start` in the VM and returns orcad's ready JSON +(`pairingCode`, `projectRoot`). Pairing codes are never logged or stored in D1. v1 hardening: the desktop sends a +device public key and the VM pre-authorizes it, so the control plane never carries a usable pairing secret. +Endpoint branding: v0 dials the `*.vercel.run` route internally (not shown in UI); v1 fronts it with +`*.sandbox.onorca.dev` if white-label needs to extend to network inspection. + +## Golden image (`cloud/apps/sandbox-control/image/`) + +Operator-only script (`build-golden-image.ts`, dry-run by default). Steps: create build sandbox (`node24`, 4 vCPU, +non-persistent, 40 min) → upload pinned orcad linux-x64 bundle (sha256-checked) → `provision-golden.sh`: apt +basics, `gh`, pinned agent CLIs (Claude Code, Codex, OpenCode, Gemini), git credential helper + `orca-sandbox-start`, +`orca-sandbox-drain`, `orca-egress-probe` → smoke (orcad reaches ready, CLIs `--version`) → **wipe orcad identity and +any auth homes** (clone-safe, risk #1) → snapshot with `expiration: 0` → delete build sandbox → print manifest +(`snapshotId`, versions, sha256) for `GOLDEN_SNAPSHOT_ID`. Snapshot → orcad ready measured at 3.3–4.3s. + +## Network policy (fail closed) + +Compiled per sandbox by `compileNetworkPolicy()`: +- Allowlist record form, **no `"*"` entry** (unlisted domains don't resolve). Default list: package registries + (npm, pypi, crates, go proxy, rubygems), github.com + codeload + objects, agent APIs (Anthropic, OpenAI/ChatGPT, + Google), and Orca endpoints. Workspaces can opt into `"open"` (adds `"*": []`), never silently. +- Credential hosts get `[...inject rules, {response: 403 orca-egress-denied}]`: a request without our placeholder is + refused, so the real token can't be paired with an attacker-chosen request shape. +- GitHub: VM sets `http.https://github.com/.extraheader = Authorization: Basic b64(x-access-token:)` so + every git request carries the placeholder (git otherwise sends an unauthenticated probe first, which the trailing + deny would 403). The control plane mints a 1h GitHub App installation token and rewrites the policy before expiry. +- **Node fetch hang**: with a `transform` rule, the Vercel proxy answers HTTP/1.1 with no `content-length` or + `transfer-encoding`; undici waits forever. curl/git (libcurl) and Go (`gh`) are fine. Handling: (1) `transform` is + used only for hosts whose clients are curl/Go (github.com git traffic); (2) hosts Node clients use + (api.github.com) get a `forwardURL` rule to `POST /v1/egress/proxy` on the control plane instead, which verifies + Vercel's sandbox OIDC token (`oidc.vercel.com`, aud = forward URL), maps `sandbox_name` → our row, injects the + header and returns a normally framed response — this also keeps users' GitHub tokens out of Vercel's policy store; + (3) the image ships `orca-egress-probe` that does a bounded Node `fetch` per credential host at boot and reports + `egress_framing_hang` instead of letting agents hang. File the framing bug with Vercel. Needs a live test. +- Agent sign-in is native inside the VM (unmodified CLI, Anthropic-compliant); model hosts are allowlisted, not + injected. Auth homes live on the sandbox disk and persist across stop/resume. + +## Lifecycle, idle stop and the 24h cap + +- One persistent sandbox per workspace. Orca owns the timer: Vercel `timeout` is a 30 min lease that the desktop + renews while orcad reports busy; if the desktop disappears the lease lapses and Vercel stops the session (files kept). +- Idle stop: 20 min without clients, agents or PTY input → suspend. +- Opportunistic rollover after ~12h when all agents idle and PTYs quiet ≥2 min. Forced rollover at 23h50m: warn at + T−60 and T−15, stop admitting turns at T−10, drain (orcad SIGTERM → durable restart offers, scrollback/cwd hints) + up to 5 min, stop, resume, start orcad. One state machine covers idle stop, cap, crash and "Restart". +- After reconnect the desktop auto-continues the durable offers (`agentSession.restartResumable` → + `agentSession.restartContinue`) for sessions the rollover interrupted, so agents carry on without user action. + +## Failure / leak handling + +- Write-ahead D1 row before any Vercel call; the deterministic name makes create retries idempotent (`GET` by name + first). Cron reconciles rows stuck in `creating`/`destroying`. +- Destroy deletes the sandbox with `deleteOrphanSnapshots=true`, then lists snapshots by name and deletes any left + (bake-off: ~325 MB auto snapshots survive delete otherwise). +- Vercel errors map to catalogued codes; bodies are never echoed to the desktop. + +## Security + +- Secrets via `wrangler secret put` only: `VERCEL_TOKEN` (team-scoped, Sandbox only), `CONTROL_PLANE_TOKEN`, + `GITHUB_APP_PRIVATE_KEY`. Worker refuses to start lifecycle routes if `VERCEL_TEAM_ID`/`VERCEL_PROJECT_ID` are + missing. No routes / `workers_dev:false` in the checked-in wrangler file. +- Per-account authorization on every route (row `account_id` must equal the caller's); ids are random. +- Strict zod bodies, 16 KiB limit, no redirects on provider calls, provider error bodies dropped. + +## What needs live access (not done) + +Vercel Pro team id + project id + a Sandbox-scoped token (as Worker secrets), wrangler auth for a dev D1, a +billing Worker dev deployment + `CONTROL_PLANE_TOKEN`, and an Orca Cloud auth change to mint `aud=orca-sandbox` +tokens. Live checks then: golden image build, snapshot→ready timing on Pro, forwardURL + OIDC proxy, Basic-auth +inject for git, 24h rollover rehearsal with a shortened cap. From a3d0bed52e9f6938319805b31fe128cc6bf65ba6 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:08:10 -0800 Subject: [PATCH 02/18] feat(cloud): EnvironmentProviderDriver + HostedSandboxDriver with contract kit and fake control plane (#10) --- .../environment-provider-recipe-adapter.ts | 54 +++++ .../fake-hosted-control-plane.test-support.ts | 175 ++++++++++++++ .../hosted-sandbox-control-plane-client.ts | 222 ++++++++++++++++++ .../hosted-sandbox-driver.test.ts | 100 ++++++++ .../hosted-sandbox/hosted-sandbox-driver.ts | 160 +++++++++++++ .../hosted-sandbox/hosted-sandbox-wire.ts | 120 ++++++++++ 6 files changed, 831 insertions(+) create mode 100644 src/main/environment-providers/environment-provider-recipe-adapter.ts create mode 100644 src/main/hosted-sandbox/fake-hosted-control-plane.test-support.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-driver.test.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-driver.ts create mode 100644 src/shared/hosted-sandbox/hosted-sandbox-wire.ts diff --git a/src/main/environment-providers/environment-provider-recipe-adapter.ts b/src/main/environment-providers/environment-provider-recipe-adapter.ts new file mode 100644 index 00000000000..4963bde6b8a --- /dev/null +++ b/src/main/environment-providers/environment-provider-recipe-adapter.ts @@ -0,0 +1,54 @@ +// Drivers answer in the shape a shell recipe prints, so runtime records, write-ahead, pairing reuse +// on resume and host health keep one code path for every provider. +import { z } from 'zod' +import { + getEphemeralVmRecipeResultConnection, + type EphemeralVmRecipeResult, + type JsonValue +} from '../../shared/ephemeral-vm-recipes' +import type { EphemeralVmCheckoutMode } from '../../shared/orca-yaml-hook-types' +import type { EnvironmentHandle, EnvironmentReady } from './environment-provider-driver' + +const USER_DATA_KEY = 'environmentDriver' + +const HandleSchema = z + .object({ + driverId: z.string().min(1).max(64), + environmentId: z.string().min(1).max(128), + driverState: z.record(z.string(), z.json()) + }) + .strict() + +export function environmentReadyToRecipeResult( + ready: EnvironmentReady, + checkoutMode: EphemeralVmCheckoutMode +): EphemeralVmRecipeResult { + const userData: Record = { + [USER_DATA_KEY]: { + driverId: ready.handle.driverId, + environmentId: ready.handle.environmentId, + driverState: ready.handle.driverState + } + } + const connection = { ...ready.connection } + return checkoutMode === 'provisioned-root' + ? { schemaVersion: 2, checkoutMode: 'provisioned-root', connection, userData } + : { schemaVersion: 1, connection, userData } +} + +/** Null when the record came from a shell recipe or was written by another driver. */ +export function environmentHandleFromRecipeResult( + result: EphemeralVmRecipeResult, + driverId: string +): EnvironmentHandle | null { + if (getEphemeralVmRecipeResultConnection(result).type !== 'orca-server') { + return null + } + const parsed = HandleSchema.safeParse(result.userData?.[USER_DATA_KEY]) + if (!parsed.success || parsed.data.driverId !== driverId) { + return null + } + // SAFETY: z.json() validated every value as JSON; its inferred type is structurally JsonValue. + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: see above. + return parsed.data as EnvironmentHandle +} diff --git a/src/main/hosted-sandbox/fake-hosted-control-plane.test-support.ts b/src/main/hosted-sandbox/fake-hosted-control-plane.test-support.ts new file mode 100644 index 00000000000..8d2455d3770 --- /dev/null +++ b/src/main/hosted-sandbox/fake-hosted-control-plane.test-support.ts @@ -0,0 +1,175 @@ +// In-memory Orca sandbox control plane behind `fetch`, speaking the real wire schemas. +import { encodePairingOffer } from '../../shared/remote-runtime/pairing/pairing' +import { + HostedSandboxCreateRequestSchema, + HostedSandboxLeaseRequestSchema, + type HostedSandboxView +} from '../../shared/hosted-sandbox/hosted-sandbox-wire' + +const SESSION_CAP_MS = 24 * 60 * 60_000 +const LEASE_MS = 30 * 60_000 + +export type FakeHostedControlPlane = { + fetch: typeof fetch + token: string + sandboxes: Map + /** Next lifecycle start is refused by billing with this reason. */ + denyNextStart: (reason: string) => void + requests: { method: string; path: string; authorization: string | null }[] +} + +export function createFakeHostedControlPlane( + options: { now?: () => number } = {} +): FakeHostedControlPlane { + let clock = 1_700_000_000_000 + const now = options.now ?? (() => (clock += 1000)) + const sandboxes = new Map() + const requests: FakeHostedControlPlane['requests'] = [] + let pendingDeny: string | null = null + let nextId = 1 + const token = 'orca-sandbox-test-token' + + const json = (body: unknown, status = 200): Response => Response.json(body, { status }) + const fail = (error: string, status: number, reason?: string): Response => + json(reason ? { error, reason } : { error }, status) + const session = (): NonNullable => { + const startedAt = now() + return { + id: `sess_${nextId++}`, + startedAt, + deadlineAt: startedAt + SESSION_CAP_MS - 10 * 60_000, + leaseExpiresAt: startedAt + LEASE_MS + } + } + const ready = (sandbox: HostedSandboxView): Response => + json({ + sandbox, + connection: { + type: 'orca-server', + pairingCode: encodePairingOffer({ + v: 2, + endpoint: `wss://${sandbox.id}.sandbox.test`, + deviceToken: `device-${sandbox.id}`, + publicKeyB64: Buffer.alloc(32, 7).toString('base64') + }), + projectRoot: '/vercel/project' + } + }) + const takeDeny = (): string | null => { + const reason = pendingDeny + pendingDeny = null + return reason + } + + async function handle(method: string, path: string, body: unknown): Promise { + if (method === 'POST' && path === '/v1/sandboxes') { + const parsed = HostedSandboxCreateRequestSchema.safeParse(body) + if (!parsed.success) { + return fail('invalid_request', 400) + } + const denied = takeDeny() + if (denied) { + return fail('entitlement_denied', 403, denied) + } + const existing = [...sandboxes.values()].find( + (sandbox) => sandbox.workspaceId === parsed.data.workspaceId && sandbox.status === 'running' + ) + if (existing) { + return ready(existing) + } + const sandbox: HostedSandboxView = { + id: `sbx_${nextId++}`, + workspaceId: parsed.data.workspaceId, + status: 'running', + machineSize: parsed.data.machineSize, + region: parsed.data.region ?? 'iad1', + session: session(), + createdAt: now() + } + sandboxes.set(sandbox.id, sandbox) + return ready(sandbox) + } + if (method === 'GET' && path === '/v1/sandboxes') { + return json({ sandboxes: [...sandboxes.values()] }) + } + const match = /^\/v1\/sandboxes\/([^/]+)(\/[a-z]+)?$/.exec(path) + const sandbox = match ? sandboxes.get(decodeURIComponent(match[1]!)) : undefined + if (!match || !sandbox) { + return fail('not_found', 404) + } + const action = `${method} ${match[2] ?? ''}` + if (action === 'GET ') { + return json({ sandbox }) + } + if (action === 'DELETE ') { + Object.assign(sandbox, { status: 'destroyed', session: null }) + return json({ sandbox }) + } + if (sandbox.status === 'destroyed') { + return fail('conflict', 409) + } + switch (action) { + case 'POST /suspend': + Object.assign(sandbox, { status: 'suspended', session: null }) + return json({ sandbox }) + case 'POST /resume': { + if (sandbox.status === 'running') { + return ready(sandbox) + } + const denied = takeDeny() + if (denied) { + return fail('entitlement_denied', 403, denied) + } + Object.assign(sandbox, { status: 'running', session: session() }) + return ready(sandbox) + } + case 'POST /lease': { + const parsed = HostedSandboxLeaseRequestSchema.safeParse(body) + if (!parsed.success) { + return fail('invalid_request', 400) + } + if (sandbox.status !== 'running' || !sandbox.session) { + return fail('conflict', 409) + } + sandbox.session.leaseExpiresAt = Math.min( + sandbox.session.deadlineAt, + now() + parsed.data.extendMs + ) + return json({ sandbox }) + } + case 'POST /rollover': + if (sandbox.status !== 'running') { + return fail('conflict', 409) + } + Object.assign(sandbox, { session: session() }) + return ready(sandbox) + default: + return fail('not_found', 404) + } + } + + const fakeFetch: typeof fetch = async (input, init) => { + if (init?.signal?.aborted) { + throw new DOMException('The operation was aborted.', 'AbortError') + } + const url = new URL(input instanceof Request ? input.url : String(input)) + const method = init?.method ?? 'GET' + const authorization = new Headers(init?.headers).get('authorization') + requests.push({ method, path: url.pathname, authorization }) + if (authorization !== `Bearer ${token}`) { + return fail('unauthorized', 401) + } + const body = typeof init?.body === 'string' ? JSON.parse(init.body) : undefined + return handle(method, url.pathname, body) + } + + return { + fetch: fakeFetch, + token, + sandboxes, + denyNextStart: (reason) => { + pendingDeny = reason + }, + requests + } +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts b/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts new file mode 100644 index 00000000000..065d7474761 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts @@ -0,0 +1,222 @@ +// HTTPS client for the Orca sandbox control plane. The desktop holds only a short-lived Orca token; +// the provider credential never leaves the control plane. +import type { z } from 'zod' +import { + HostedSandboxErrorResponseSchema, + HostedSandboxListResponseSchema, + HostedSandboxReadyResponseSchema, + HostedSandboxStatusResponseSchema, + type HostedSandboxCreateRequest, + type HostedSandboxReadyResponse, + type HostedSandboxView +} from '../../shared/hosted-sandbox/hosted-sandbox-wire' +import { + EnvironmentProviderError, + type EnvironmentProviderErrorCode +} from '../environment-providers/environment-provider-driver' + +export type HostedSandboxControlPlaneClient = { + create( + body: HostedSandboxCreateRequest, + signal?: AbortSignal + ): Promise + get(sandboxId: string, signal?: AbortSignal): Promise + list(signal?: AbortSignal): Promise + suspend(sandboxId: string, signal?: AbortSignal): Promise + resume(sandboxId: string, signal?: AbortSignal): Promise + renewLease(sandboxId: string, extendMs: number, signal?: AbortSignal): Promise + rollover(sandboxId: string, signal?: AbortSignal): Promise + destroy(sandboxId: string, signal?: AbortSignal): Promise +} + +export type HostedSandboxControlPlaneClientOptions = { + baseUrl: string + /** Mints an Orca Cloud token with aud=orca-sandbox; never a provider credential. */ + getAccessToken: (signal?: AbortSignal) => Promise + fetch?: typeof fetch + /** Per-call ceilings; create/resume/rollover include a VM boot and orcad start. */ + timeouts?: Partial> +} + +const DEFAULT_TIMEOUTS = { boot: 180_000, control: 60_000 } +const MAX_RESPONSE_BYTES = 256 * 1024 + +export function createHostedSandboxControlPlaneClient( + options: HostedSandboxControlPlaneClientOptions +): HostedSandboxControlPlaneClient { + const base = parseBaseUrl(options.baseUrl) + const doFetch = options.fetch ?? fetch + const timeouts = { ...DEFAULT_TIMEOUTS, ...options.timeouts } + + async function call( + method: 'GET' | 'POST' | 'DELETE', + path: string, + schema: z.ZodType, + init: { body?: unknown; kind: 'boot' | 'control'; signal?: AbortSignal } + ): Promise { + const timeoutSignal = AbortSignal.timeout(timeouts[init.kind]) + const signal = init.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal + let response: Response + try { + const token = await options.getAccessToken(signal) + response = await doFetch(new URL(path, base), { + method, + headers: { + authorization: `Bearer ${token}`, + accept: 'application/json', + ...(init.body === undefined ? {} : { 'content-type': 'application/json' }) + }, + body: init.body === undefined ? undefined : JSON.stringify(init.body), + // Why: a redirect could forward the bearer token to another origin. + redirect: 'manual', + signal + }) + } catch (error) { + throw transportError(error, init.signal, timeoutSignal) + } + const payload = await readJson(response) + if (!response.ok) { + throw errorFromResponse(response.status, payload) + } + const parsed = schema.safeParse(payload) + if (!parsed.success) { + throw new EnvironmentProviderError( + 'invalid_response', + 'Orca Cloud returned an unexpected response.' + ) + } + return parsed.data + } + + const sandboxPath = (id: string, suffix = ''): string => + `v1/sandboxes/${encodeURIComponent(id)}${suffix}` + + return { + create: (body, signal) => + call('POST', 'v1/sandboxes', HostedSandboxReadyResponseSchema, { + body, + kind: 'boot', + signal + }), + get: async (id, signal) => + ( + await call('GET', sandboxPath(id), HostedSandboxStatusResponseSchema, { + kind: 'control', + signal + }) + ).sandbox, + list: async (signal) => + ( + await call('GET', 'v1/sandboxes', HostedSandboxListResponseSchema, { + kind: 'control', + signal + }) + ).sandboxes, + suspend: async (id, signal) => + ( + await call('POST', sandboxPath(id, '/suspend'), HostedSandboxStatusResponseSchema, { + kind: 'control', + signal + }) + ).sandbox, + resume: (id, signal) => + call('POST', sandboxPath(id, '/resume'), HostedSandboxReadyResponseSchema, { + kind: 'boot', + signal + }), + renewLease: async (id, extendMs, signal) => + ( + await call('POST', sandboxPath(id, '/lease'), HostedSandboxStatusResponseSchema, { + body: { extendMs }, + kind: 'control', + signal + }) + ).sandbox, + rollover: (id, signal) => + call('POST', sandboxPath(id, '/rollover'), HostedSandboxReadyResponseSchema, { + kind: 'boot', + signal + }), + destroy: async (id, signal) => + ( + await call('DELETE', sandboxPath(id), HostedSandboxStatusResponseSchema, { + kind: 'control', + signal + }) + ).sandbox + } +} + +function parseBaseUrl(value: string): URL { + const url = new URL(value.endsWith('/') ? value : `${value}/`) + const loopback = url.hostname === '127.0.0.1' || url.hostname === 'localhost' + if (url.protocol !== 'https:' && !(url.protocol === 'http:' && loopback)) { + throw new Error('Orca Cloud control plane must be HTTPS.') + } + if (url.username || url.password || url.search || url.hash) { + throw new Error('Orca Cloud control plane URL must not carry credentials, query or fragment.') + } + return url +} + +async function readJson(response: Response): Promise { + const text = await response.text() + if (text.length > MAX_RESPONSE_BYTES) { + return null + } + try { + return text ? JSON.parse(text) : null + } catch { + return null + } +} + +const STATUS_CODES: Record = { + unauthorized: 'unauthorized', + forbidden: 'unauthorized', + not_found: 'not_found', + conflict: 'conflict', + entitlement_denied: 'entitlement_denied', + invalid_request: 'invalid_request', + invalid_json: 'invalid_request', + billing_unavailable: 'unavailable', + provider_unavailable: 'unavailable', + not_configured: 'unavailable', + internal_error: 'unavailable' +} + +function errorFromResponse(status: number, payload: unknown): EnvironmentProviderError { + const parsed = HostedSandboxErrorResponseSchema.safeParse(payload) + if (parsed.success) { + const code = STATUS_CODES[parsed.data.error] ?? 'unavailable' + return new EnvironmentProviderError( + code, + `Orca Cloud refused the request (${parsed.data.error}).`, + parsed.data.reason ?? null + ) + } + if (status >= 300 && status < 400) { + return new EnvironmentProviderError('invalid_response', 'Orca Cloud answered with a redirect.') + } + return new EnvironmentProviderError( + status === 401 || status === 403 ? 'unauthorized' : 'unavailable', + `Orca Cloud request failed (HTTP ${status}).` + ) +} + +function transportError( + error: unknown, + callerSignal: AbortSignal | undefined, + timeoutSignal: AbortSignal +): EnvironmentProviderError { + if (error instanceof EnvironmentProviderError) { + return error + } + if (callerSignal?.aborted) { + return new EnvironmentProviderError('aborted', 'Orca Cloud request was cancelled.') + } + if (timeoutSignal.aborted) { + return new EnvironmentProviderError('timeout', 'Orca Cloud did not answer in time.') + } + return new EnvironmentProviderError('unavailable', 'Could not reach Orca Cloud.') +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts b/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts new file mode 100644 index 00000000000..35c71b03055 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts @@ -0,0 +1,100 @@ +import { describe, expect, it } from 'vitest' +import { parseEphemeralVmRecipeResult } from '../../shared/ephemeral-vm-recipes' +import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver' +import { runEnvironmentProviderDriverContract } from '../environment-providers/environment-provider-driver-contract.test-support' +import { + environmentHandleFromRecipeResult, + environmentReadyToRecipeResult +} from '../environment-providers/environment-provider-recipe-adapter' +import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' +import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' +import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support' + +function setup(options: { token?: string; fetch?: typeof fetch } = {}) { + const plane = createFakeHostedControlPlane() + const client = createHostedSandboxControlPlaneClient({ + baseUrl: 'https://sandbox.cloud.test', + getAccessToken: async () => options.token ?? plane.token, + fetch: options.fetch ?? plane.fetch + }) + return { plane, driver: createHostedSandboxDriver(client) } +} + +async function rejection(promise: Promise): Promise { + return promise.then( + () => null, + (error: unknown) => error + ) +} + +runEnvironmentProviderDriverContract('HostedSandboxDriver', async () => { + const { plane, driver } = setup() + return { + driver, + liveEnvironmentIds: async () => + [...plane.sandboxes.values()].filter((s) => s.status !== 'destroyed').map((s) => s.id) + } +}) + +describe('HostedSandboxDriver', () => { + it('surfaces the billing deny reason when Orca Cloud refuses a start', async () => { + const { plane, driver } = setup() + plane.denyNextStart('included_hours_exhausted') + const error = await rejection(driver.create({ workspaceId: 'ws-1' })) + expect(isEnvironmentProviderError(error, 'entitlement_denied')).toBe(true) + expect(isEnvironmentProviderError(error) && error.reason).toBe('included_hours_exhausted') + }) + + it('sends only the Orca token and maps a rejected token to unauthorized', async () => { + const { plane, driver } = setup({ token: 'expired' }) + const error = await rejection(driver.create({ workspaceId: 'ws-1' })) + expect(isEnvironmentProviderError(error, 'unauthorized')).toBe(true) + expect(plane.requests.map((r) => r.authorization)).toEqual(['Bearer expired']) + }) + + it('does not follow redirects, so the bearer token cannot be forwarded off-origin', async () => { + const seen: RequestInit[] = [] + const { driver } = setup({ + fetch: async (_input, init) => { + seen.push(init ?? {}) + return new Response(null, { status: 302, headers: { location: 'https://evil.test/' } }) + } + }) + const error = await rejection(driver.create({ workspaceId: 'ws-1' })) + expect(isEnvironmentProviderError(error, 'invalid_response')).toBe(true) + expect(seen[0]?.redirect).toBe('manual') + }) + + it('rejects a success payload that does not match the wire contract', async () => { + const { driver } = setup({ fetch: async () => Response.json({ sandbox: { id: 'x' } }) }) + const error = await rejection(driver.create({ workspaceId: 'ws-1' })) + expect(isEnvironmentProviderError(error, 'invalid_response')).toBe(true) + }) + + it('refuses a plain-HTTP control plane outside loopback', () => { + expect(() => + createHostedSandboxControlPlaneClient({ + baseUrl: 'http://sandbox.cloud.test', + getAccessToken: async () => 't' + }) + ).toThrow(/HTTPS/) + }) + + it('round-trips through the recipe result the runtime record stores', async () => { + const { driver } = setup() + const ready = await driver.create({ workspaceId: 'ws-1' }) + const recipeResult = environmentReadyToRecipeResult(ready, 'provisioned-root') + const parsed = parseEphemeralVmRecipeResult(JSON.stringify(recipeResult)) + expect(parsed.ok).toBe(true) + if (!parsed.ok) { + return + } + expect(environmentHandleFromRecipeResult(parsed.result, HOSTED_SANDBOX_DRIVER_ID)).toEqual( + ready.handle + ) + expect(environmentHandleFromRecipeResult(parsed.result, 'shell-recipe')).toBeNull() + expect((await driver.resume(ready.handle)).handle.environmentId).toBe( + ready.handle.environmentId + ) + }) +}) diff --git a/src/main/hosted-sandbox/hosted-sandbox-driver.ts b/src/main/hosted-sandbox/hosted-sandbox-driver.ts new file mode 100644 index 00000000000..244625e4489 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-driver.ts @@ -0,0 +1,160 @@ +// Orca Cloud workspaces: an EnvironmentProviderDriver that talks only to Orca's control plane. +// Which provider runs the VM (Vercel today) is a control-plane detail the desktop never sees. +import { + HOSTED_SANDBOX_MACHINE_SIZES, + type HostedSandboxMachineSize, + type HostedSandboxReadyResponse, + type HostedSandboxView +} from '../../shared/hosted-sandbox/hosted-sandbox-wire' +import { + EnvironmentProviderError, + isEnvironmentProviderError, + type EnvironmentHandle, + type EnvironmentProviderCapabilities, + type EnvironmentProviderDriver, + type EnvironmentReady, + type EnvironmentSessionInfo, + type EnvironmentStatus, + type EnvironmentStatusSnapshot +} from '../environment-providers/environment-provider-driver' +import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' + +export const HOSTED_SANDBOX_DRIVER_ID = 'orca-cloud' + +/** Measured on Vercel: stop is a reboot (PTYs lost), disk persists, Pro caps a session at 24h. */ +export const HOSTED_SANDBOX_CAPABILITIES: EnvironmentProviderCapabilities = Object.freeze({ + memoryPause: false, + maxSessionMs: 24 * 60 * 60_000, + fsPersistOnStop: true, + egressInjection: true, + idleStopMs: 20 * 60_000 +}) + +const STATUS: Record = { + creating: 'provisioning', + running: 'running', + suspending: 'suspending', + suspended: 'suspended', + resuming: 'resuming', + destroying: 'destroying', + destroyed: 'destroyed', + failed: 'failed' +} + +export function createHostedSandboxDriver( + client: HostedSandboxControlPlaneClient +): EnvironmentProviderDriver { + return { + id: HOSTED_SANDBOX_DRIVER_ID, + capabilities: HOSTED_SANDBOX_CAPABILITIES, + + async create(spec, ctx) { + ctx?.onProgress?.({ phase: 'checking-entitlement' }) + const machineSize = toMachineSize(spec.machineSize) + ctx?.onProgress?.({ phase: 'provisioning' }) + const ready = await client.create( + { + workspaceId: spec.workspaceId, + machineSize, + ...(spec.region ? { region: spec.region } : {}), + ...(spec.repo ? { repo: spec.repo } : {}) + }, + ctx?.signal + ) + ctx?.onProgress?.({ phase: 'connecting' }) + return toReady(ready) + }, + + async resume(handle, ctx) { + ctx?.onProgress?.({ phase: 'resuming' }) + const ready = await client.resume(sandboxIdOf(handle), ctx?.signal) + ctx?.onProgress?.({ phase: 'connecting' }) + return toReady(ready) + }, + + async suspend(handle, ctx) { + ctx?.onProgress?.({ phase: 'suspending' }) + return toSnapshot(await client.suspend(sandboxIdOf(handle), ctx?.signal)) + }, + + async destroy(handle, ctx) { + ctx?.onProgress?.({ phase: 'destroying' }) + try { + await client.destroy(sandboxIdOf(handle), ctx?.signal) + } catch (error) { + // Why: destroy is the cleanup of last resort, so an environment that is already gone is done. + if (!isEnvironmentProviderError(error, 'not_found')) { + throw error + } + } + }, + + async status(handle, ctx) { + return toSnapshot(await client.get(sandboxIdOf(handle), ctx?.signal)) + }, + + async renewLease(handle, extendMs, ctx) { + ctx?.onProgress?.({ phase: 'renewing' }) + return toSnapshot(await client.renewLease(sandboxIdOf(handle), extendMs, ctx?.signal)) + }, + + async rollover(handle, ctx) { + ctx?.onProgress?.({ phase: 'rolling-over' }) + const ready = await client.rollover(sandboxIdOf(handle), ctx?.signal) + ctx?.onProgress?.({ phase: 'connecting' }) + return toReady(ready) + } + } +} + +function toMachineSize(value: string | undefined): HostedSandboxMachineSize { + if (value === undefined) { + return 'standard' + } + const size = HOSTED_SANDBOX_MACHINE_SIZES.find((candidate) => candidate === value) + if (!size) { + throw new EnvironmentProviderError('invalid_request', `Unknown machine size: ${value}`) + } + return size +} + +function sandboxIdOf(handle: EnvironmentHandle): string { + const id = handle.driverState.sandboxId + if (handle.driverId !== HOSTED_SANDBOX_DRIVER_ID || typeof id !== 'string' || !id) { + throw new EnvironmentProviderError('invalid_request', 'Not an Orca Cloud environment handle.') + } + return id +} + +function toSession(view: HostedSandboxView): EnvironmentSessionInfo | null { + return view.session + ? { + startedAt: view.session.startedAt, + deadlineAt: view.session.deadlineAt, + leaseExpiresAt: view.session.leaseExpiresAt + } + : null +} + +function toSnapshot(view: HostedSandboxView): EnvironmentStatusSnapshot { + return { status: STATUS[view.status], session: toSession(view) } +} + +function toReady(ready: HostedSandboxReadyResponse): EnvironmentReady { + const session = toSession(ready.sandbox) + if (ready.sandbox.status !== 'running' || !session) { + throw new EnvironmentProviderError( + 'invalid_response', + 'Orca Cloud reported a connection for an environment that is not running.' + ) + } + return { + handle: { + driverId: HOSTED_SANDBOX_DRIVER_ID, + environmentId: ready.sandbox.id, + driverState: { sandboxId: ready.sandbox.id } + }, + connection: ready.connection, + session + } +} diff --git a/src/shared/hosted-sandbox/hosted-sandbox-wire.ts b/src/shared/hosted-sandbox/hosted-sandbox-wire.ts new file mode 100644 index 00000000000..203d9a7443d --- /dev/null +++ b/src/shared/hosted-sandbox/hosted-sandbox-wire.ts @@ -0,0 +1,120 @@ +// Wire contract between the desktop and the Orca sandbox control plane (cloud/apps/sandbox-control). +// The Worker's tests parse its responses with these schemas, so either side drifting fails a test. +import { z } from 'zod' + +export const HOSTED_SANDBOX_MACHINE_SIZES = ['standard', 'large', 'xlarge'] as const +export type HostedSandboxMachineSize = (typeof HOSTED_SANDBOX_MACHINE_SIZES)[number] + +export const HOSTED_SANDBOX_STATUSES = [ + 'creating', + 'running', + 'suspending', + 'suspended', + 'resuming', + 'destroying', + 'destroyed', + 'failed' +] as const +export type HostedSandboxStatus = (typeof HOSTED_SANDBOX_STATUSES)[number] + +export const HOSTED_SANDBOX_ERROR_CODES = [ + 'unauthorized', + 'forbidden', + 'not_found', + 'conflict', + 'entitlement_denied', + 'invalid_request', + 'invalid_json', + 'billing_unavailable', + 'provider_unavailable', + 'not_configured', + 'internal_error' +] as const +export type HostedSandboxErrorCode = (typeof HOSTED_SANDBOX_ERROR_CODES)[number] + +const Id = z + .string() + .min(1) + .max(128) + .regex(/^[A-Za-z0-9_.:-]+$/) +const Millis = z.number().int().nonnegative() + +export const HostedSandboxSessionSchema = z + .object({ + id: Id, + startedAt: Millis, + deadlineAt: Millis, + leaseExpiresAt: Millis + }) + .strict() + +export const HostedSandboxViewSchema = z + .object({ + id: Id, + workspaceId: Id, + status: z.enum(HOSTED_SANDBOX_STATUSES), + machineSize: z.enum(HOSTED_SANDBOX_MACHINE_SIZES), + region: z.string().min(1).max(32), + session: HostedSandboxSessionSchema.nullable(), + createdAt: Millis + }) + .strict() +export type HostedSandboxView = z.infer + +export const HostedSandboxConnectionSchema = z + .object({ + type: z.literal('orca-server'), + pairingCode: z.string().min(1).max(8192), + projectRoot: z.string().startsWith('/').max(4096) + }) + .strict() + +export const HostedSandboxCreateRequestSchema = z + .object({ + workspaceId: Id, + machineSize: z.enum(HOSTED_SANDBOX_MACHINE_SIZES), + region: z + .string() + .regex(/^[a-z]{3}[0-9]$/) + .optional(), + repo: z + .object({ + url: z.url({ protocol: /^https$/ }).max(2048), + ref: z.string().min(1).max(255).optional() + }) + .strict() + .optional() + }) + .strict() +export type HostedSandboxCreateRequest = z.infer + +export const HostedSandboxLeaseRequestSchema = z + .object({ + extendMs: z + .number() + .int() + .min(60_000) + .max(60 * 60_000) + }) + .strict() + +/** create, resume and rollover: the environment is up and orcad is ready to pair. */ +export const HostedSandboxReadyResponseSchema = z + .object({ sandbox: HostedSandboxViewSchema, connection: HostedSandboxConnectionSchema }) + .strict() +export type HostedSandboxReadyResponse = z.infer + +export const HostedSandboxStatusResponseSchema = z + .object({ sandbox: HostedSandboxViewSchema }) + .strict() + +export const HostedSandboxListResponseSchema = z + .object({ sandboxes: z.array(HostedSandboxViewSchema).max(1000) }) + .strict() + +export const HostedSandboxErrorResponseSchema = z + .object({ + error: z.enum(HOSTED_SANDBOX_ERROR_CODES), + reason: z.string().max(64).optional() + }) + .strict() From c502c47750ec2c8613bddc850b220cb1095e8a29 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:22:19 -0800 Subject: [PATCH 03/18] feat(cloud): sandbox control-plane Worker (white-label Vercel) (#11) * feat(cloud): sandbox control-plane Worker (Vercel behind an interface, entitlements, usage, egress proxy) * ci: collect sandbox-control tests --- cloud/apps/sandbox-control/.gitignore | 3 + .../sandbox-control/migrations/0001_init.sql | 52 +++ cloud/apps/sandbox-control/package.json | 24 ++ cloud/apps/sandbox-control/src/auth.ts | 73 +++++ .../apps/sandbox-control/src/egress-proxy.ts | 127 ++++++++ .../apps/sandbox-control/src/entitlements.ts | 89 ++++++ cloud/apps/sandbox-control/src/env.ts | 33 ++ cloud/apps/sandbox-control/src/index.ts | 194 +++++++++++ cloud/apps/sandbox-control/src/lifecycle.ts | 301 ++++++++++++++++++ .../sandbox-control/src/network-policy.ts | 110 +++++++ cloud/apps/sandbox-control/src/reconcile.ts | 98 ++++++ .../sandbox-control/src/sandbox-provider.ts | 90 ++++++ .../src/sandbox-vm-commands.ts | 89 ++++++ cloud/apps/sandbox-control/src/store.ts | 173 ++++++++++ cloud/apps/sandbox-control/src/usage.ts | 75 +++++ .../sandbox-control/src/vercel-sandbox-api.ts | 255 +++++++++++++++ cloud/apps/sandbox-control/src/wire.ts | 70 ++++ cloud/apps/sandbox-control/test/d1-fake.ts | 64 ++++ .../sandbox-control/test/egress-proxy.test.ts | 81 +++++ .../apps/sandbox-control/test/fake-vercel.ts | 161 ++++++++++ cloud/apps/sandbox-control/test/harness.ts | 98 ++++++ .../test/lifecycle-flow.test.ts | 190 +++++++++++ .../test/network-policy.test.ts | 63 ++++ .../test/vercel-sandbox-api.test.ts | 95 ++++++ cloud/apps/sandbox-control/tsconfig.json | 14 + cloud/apps/sandbox-control/vitest.config.ts | 7 + cloud/apps/sandbox-control/wrangler.jsonc | 28 ++ cloud/pnpm-lock.yaml | 28 ++ .../check-test-file-runners.mjs | 9 + 29 files changed, 2694 insertions(+) create mode 100644 cloud/apps/sandbox-control/.gitignore create mode 100644 cloud/apps/sandbox-control/migrations/0001_init.sql create mode 100644 cloud/apps/sandbox-control/package.json create mode 100644 cloud/apps/sandbox-control/src/auth.ts create mode 100644 cloud/apps/sandbox-control/src/egress-proxy.ts create mode 100644 cloud/apps/sandbox-control/src/entitlements.ts create mode 100644 cloud/apps/sandbox-control/src/env.ts create mode 100644 cloud/apps/sandbox-control/src/index.ts create mode 100644 cloud/apps/sandbox-control/src/lifecycle.ts create mode 100644 cloud/apps/sandbox-control/src/network-policy.ts create mode 100644 cloud/apps/sandbox-control/src/reconcile.ts create mode 100644 cloud/apps/sandbox-control/src/sandbox-provider.ts create mode 100644 cloud/apps/sandbox-control/src/sandbox-vm-commands.ts create mode 100644 cloud/apps/sandbox-control/src/store.ts create mode 100644 cloud/apps/sandbox-control/src/usage.ts create mode 100644 cloud/apps/sandbox-control/src/vercel-sandbox-api.ts create mode 100644 cloud/apps/sandbox-control/src/wire.ts create mode 100644 cloud/apps/sandbox-control/test/d1-fake.ts create mode 100644 cloud/apps/sandbox-control/test/egress-proxy.test.ts create mode 100644 cloud/apps/sandbox-control/test/fake-vercel.ts create mode 100644 cloud/apps/sandbox-control/test/harness.ts create mode 100644 cloud/apps/sandbox-control/test/lifecycle-flow.test.ts create mode 100644 cloud/apps/sandbox-control/test/network-policy.test.ts create mode 100644 cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts create mode 100644 cloud/apps/sandbox-control/tsconfig.json create mode 100644 cloud/apps/sandbox-control/vitest.config.ts create mode 100644 cloud/apps/sandbox-control/wrangler.jsonc diff --git a/cloud/apps/sandbox-control/.gitignore b/cloud/apps/sandbox-control/.gitignore new file mode 100644 index 00000000000..18433e3fe1e --- /dev/null +++ b/cloud/apps/sandbox-control/.gitignore @@ -0,0 +1,3 @@ +.dev.vars +.wrangler/ +node_modules/ diff --git a/cloud/apps/sandbox-control/migrations/0001_init.sql b/cloud/apps/sandbox-control/migrations/0001_init.sql new file mode 100644 index 00000000000..077d6a181c2 --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0001_init.sql @@ -0,0 +1,52 @@ +-- One row per Orca Cloud workspace sandbox. Written before any provider call (write-ahead), so a +-- crash mid-create leaves a row the cron reconciles instead of an untracked VM. +CREATE TABLE sandboxes ( + id TEXT PRIMARY KEY, + account_id TEXT NOT NULL, + user_id TEXT NOT NULL, + workspace_id TEXT NOT NULL, + provider TEXT NOT NULL, + provider_name TEXT NOT NULL UNIQUE, + status TEXT NOT NULL, + machine_size TEXT NOT NULL, + region TEXT NOT NULL, + -- Bumped on every transition; lifecycle writes compare-and-set on it. + generation INTEGER NOT NULL DEFAULT 0, + session_id TEXT, + session_started_at INTEGER, + session_deadline_at INTEGER, + lease_expires_at INTEGER, + -- Start of the not-yet-reported usage segment of the current session. + usage_settled_at INTEGER, + last_error TEXT, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + destroyed_at INTEGER +); + +CREATE UNIQUE INDEX sandboxes_live_workspace + ON sandboxes (account_id, workspace_id) WHERE status <> 'destroyed'; +CREATE INDEX sandboxes_status ON sandboxes (status, updated_at); + +-- Every snapshot id seen for a sandbox, so destroy can prove none outlive it. +CREATE TABLE sandbox_snapshots ( + snapshot_id TEXT PRIMARY KEY, + sandbox_id TEXT NOT NULL REFERENCES sandboxes (id), + recorded_at INTEGER NOT NULL, + deleted_at INTEGER +); +CREATE INDEX sandbox_snapshots_sandbox ON sandbox_snapshots (sandbox_id); + +-- Usage segments owed to billing. Written before the row's settle cursor advances, so a billing +-- outage delays reports instead of dropping them; usage_id makes resends idempotent upstream. +CREATE TABLE usage_outbox ( + usage_id TEXT PRIMARY KEY, + sandbox_id TEXT NOT NULL REFERENCES sandboxes (id), + account_id TEXT NOT NULL, + machine_size TEXT NOT NULL, + started_at INTEGER NOT NULL, + ended_at INTEGER NOT NULL, + attempts INTEGER NOT NULL DEFAULT 0, + sent_at INTEGER +); +CREATE INDEX usage_outbox_pending ON usage_outbox (sent_at, started_at); diff --git a/cloud/apps/sandbox-control/package.json b/cloud/apps/sandbox-control/package.json new file mode 100644 index 00000000000..1da9a6dd95b --- /dev/null +++ b/cloud/apps/sandbox-control/package.json @@ -0,0 +1,24 @@ +{ + "name": "@orca-cloud/sandbox-control", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "wrangler dev --port 8791 --ip 127.0.0.1", + "db:migrate:local": "wrangler d1 migrations apply SANDBOX_DB --local", + "typecheck": "tsc --noEmit", + "test": "vitest run" + }, + "dependencies": { + "jose": "^6.2.13", + "zod": "^4.6.7" + }, + "devDependencies": { + "@cloudflare/workers-types": "^5.20261103.1", + "@types/node": "^24.19.2", + "tsx": "^4.23.15", + "typescript": "^5.9.3", + "vitest": "^5.0.5", + "wrangler": "^4.157.0" + } +} diff --git a/cloud/apps/sandbox-control/src/auth.ts b/cloud/apps/sandbox-control/src/auth.ts new file mode 100644 index 00000000000..46aaa83409f --- /dev/null +++ b/cloud/apps/sandbox-control/src/auth.ts @@ -0,0 +1,73 @@ +// Desktop callers present a short-lived ES256 JWT minted by Orca Cloud auth (aud=orca-sandbox, +// purpose=sandbox), verified against the auth JWKS — the billing Worker's pattern. +import { + createLocalJWKSet, + createRemoteJWKSet, + jwtVerify, + type JSONWebKeySet, + type JWTVerifyGetKey +} from 'jose' +import { z } from 'zod' +import type { Env } from './env' + +const ID = z + .string() + .min(1) + .max(128) + .regex(/^[A-Za-z0-9_.:-]+$/) + +const ClaimsSchema = z.object({ + sub: ID, + org: ID.optional(), + purpose: z.literal('sandbox') +}) + +export type SandboxCaller = { userId: string; accountId: string } + +const keySets = new Map() + +function keysFor(env: Env): JWTVerifyGetKey { + const source = env.SANDBOX_JWKS_URL ?? `inline:${env.SANDBOX_JWKS ?? ''}` + let keys = keySets.get(source) + if (!keys) { + keys = env.SANDBOX_JWKS_URL + ? createRemoteJWKSet(new URL(env.SANDBOX_JWKS_URL)) + : createLocalJWKSet(JSON.parse(env.SANDBOX_JWKS ?? '{"keys":[]}') as JSONWebKeySet) + keySets.set(source, keys) + } + return keys +} + +export function readBearer(value: string | null): string | null { + return /^Bearer ([^\s]+)$/.exec(value ?? '')?.[1] ?? null +} + +/** Same account rule as billing: an org pays for its members, otherwise the user is the account. */ +export function accountIdFor(claims: { sub: string; org?: string }): string { + return claims.org ? `org:${claims.org}` : `user:${claims.sub}` +} + +export async function authenticateDesktop( + env: Env, + request: Request, + now: number = Date.now() +): Promise { + const token = readBearer(request.headers.get('authorization')) + if (!token) { + return null + } + try { + const { payload } = await jwtVerify(token, keysFor(env), { + audience: env.SANDBOX_JWT_AUDIENCE, + algorithms: ['ES256'], + maxTokenAge: '15m', + requiredClaims: ['exp', 'iat'], + currentDate: new Date(now), + ...(env.SANDBOX_AUTH_ISSUER ? { issuer: env.SANDBOX_AUTH_ISSUER } : {}) + }) + const claims = ClaimsSchema.parse(payload) + return { userId: claims.sub, accountId: accountIdFor(claims) } + } catch { + return null + } +} diff --git a/cloud/apps/sandbox-control/src/egress-proxy.ts b/cloud/apps/sandbox-control/src/egress-proxy.ts new file mode 100644 index 00000000000..d5169f0bfdc --- /dev/null +++ b/cloud/apps/sandbox-control/src/egress-proxy.ts @@ -0,0 +1,127 @@ +// Target of Vercel network-policy `forwardURL` rules. Vercel proves which sandbox sent the request +// with an OIDC token; we swap the VM's placeholder for the real credential and return a normally +// framed response (works around undici hanging on transform-rule responses). Real tokens never +// enter the VM or Vercel's policy store. +import { createRemoteJWKSet, decodeJwt, jwtVerify, type JWTVerifyGetKey } from 'jose' +import type { SandboxRow } from './store' + +const VERCEL_OIDC_HOSTNAME = 'oidc.vercel.com' +/** Hosts whose clients are Node (undici); git/curl hosts use transform rules instead. */ +export const PROXIED_HOSTS: ReadonlySet = new Set(['api.github.com']) +const DROP_HEADERS = [ + 'vercel-forwarded-host', + 'vercel-forwarded-scheme', + 'vercel-forwarded-port', + 'vercel-forwarded-path', + 'vercel-sandbox-oidc-token', + 'host', + 'connection', + 'keep-alive', + 'proxy-authorization', + 'transfer-encoding', + 'upgrade' +] + +export type CredentialBroker = { + /** Authorization header value for `host` on behalf of this sandbox's workspace, or null. */ + authorizationFor(row: SandboxRow, host: string): Promise +} + +/** Until the GitHub App installation-token minter exists, proxied hosts are refused. */ +export const NO_CREDENTIALS: CredentialBroker = { authorizationFor: async () => null } + +export type EgressProxyDeps = { + teamId: string + projectId: string + findSandbox: (providerName: string) => Promise + placeholderFor: (row: SandboxRow) => string + credentials: CredentialBroker + fetch?: typeof fetch + /** Tests inject a local key set; production fetches `/.well-known/jwks`. */ + keysForIssuer?: (issuer: string) => JWTVerifyGetKey +} + +const remoteKeys = new Map() +function defaultKeys(issuer: string): JWTVerifyGetKey { + let keys = remoteKeys.get(issuer) + if (!keys) { + keys = createRemoteJWKSet(new URL(`${issuer.replace(/\/$/, '')}/.well-known/jwks`)) + remoteKeys.set(issuer, keys) + } + return keys +} + +function deny(reason: string): Response { + return new Response(reason, { status: 403, headers: { 'content-type': 'text/plain' } }) +} + +/** The audience Vercel signs: the forwardURL, i.e. this request's URL minus the forwarded path. */ +function forwardAudience(requestUrl: URL, forwardedPath: string): string { + const strip = (path: string) => path.replace(/\/+$/, '') || '/' + const suffix = strip(new URL(forwardedPath, requestUrl.origin).pathname) + let pathname = strip(requestUrl.pathname) + if (suffix !== '/' && pathname.endsWith(suffix)) { + pathname = strip(pathname.slice(0, -suffix.length)) + } + return pathname === '/' ? requestUrl.origin : requestUrl.origin + pathname +} + +export async function handleEgressProxy(request: Request, deps: EgressProxyDeps): Promise { + const host = request.headers.get('vercel-forwarded-host')?.toLowerCase() + const scheme = request.headers.get('vercel-forwarded-scheme') + const port = request.headers.get('vercel-forwarded-port') + const path = request.headers.get('vercel-forwarded-path') + const token = request.headers.get('vercel-sandbox-oidc-token') + if (!host || !scheme || !port || !path || !token || !path.startsWith('/')) { + return deny('missing_proxy_headers') + } + if (!PROXIED_HOSTS.has(host) || scheme !== 'https' || port !== '443') { + return deny('host_not_proxied') + } + let claims: Record + try { + const issuer = String(decodeJwt(token).iss ?? '') + const issuerUrl = new URL(issuer) + if (issuerUrl.protocol !== 'https:' || issuerUrl.hostname !== VERCEL_OIDC_HOSTNAME) { + return deny('bad_issuer') + } + const verified = await jwtVerify(token, (deps.keysForIssuer ?? defaultKeys)(issuer), { + issuer, + audience: forwardAudience(new URL(request.url), path), + algorithms: ['RS256'], + clockTolerance: 60 + }) + claims = verified.payload + } catch { + return deny('bad_oidc_token') + } + if (claims.team_id !== deps.teamId || claims.project_id !== deps.projectId) { + return deny('foreign_sandbox') + } + const name = typeof claims.sandbox_name === 'string' ? claims.sandbox_name : null + const row = name ? await deps.findSandbox(name) : null + if (!row || row.status !== 'running') { + return deny('sandbox_not_running') + } + if (request.headers.get('authorization') !== deps.placeholderFor(row)) { + return deny('placeholder_missing') + } + const authorization = await deps.credentials.authorizationFor(row, host) + if (!authorization) { + return deny('credential_unavailable') + } + const headers = new Headers(request.headers) + for (const name of DROP_HEADERS) { + headers.delete(name) + } + headers.set('authorization', authorization) + const hasBody = request.method !== 'GET' && request.method !== 'HEAD' + const upstream = await (deps.fetch ?? fetch)(`https://${host}${path}`, { + method: request.method, + headers, + body: hasBody ? await request.arrayBuffer() : undefined, + redirect: 'manual' + }) + // Re-wrapping lets the runtime frame the body (content-length or chunked) for the VM's client. + return new Response(upstream.body, { status: upstream.status, headers: upstream.headers }) +} diff --git a/cloud/apps/sandbox-control/src/entitlements.ts b/cloud/apps/sandbox-control/src/entitlements.ts new file mode 100644 index 00000000000..ae24d148c36 --- /dev/null +++ b/cloud/apps/sandbox-control/src/entitlements.ts @@ -0,0 +1,89 @@ +// Billing Worker client (cloud/apps/billing on nwparker/billing-worker): start checks and usage. +// Fails closed: without a configured billing URL no sandbox starts, except the local-dev stub. +import { z } from 'zod' +import type { Env } from './env' + +export type MachineSize = 'standard' | 'large' | 'xlarge' + +export type StartDecision = { allow: boolean; reason: string | null } + +export type UsageSegment = { + usageId: string + accountId: string + sandboxId: string + machineSize: MachineSize + startedAt: number + endedAt: number +} + +export type UsageAnswer = { canStartSandbox: boolean; reason: string | null } + +export type EntitlementClient = { + checkStart(request: { + accountId: string + machineSize: MachineSize + runningSandboxes: number + }): Promise + reportUsage(segment: UsageSegment): Promise +} + +export class BillingUnavailableError extends Error { + constructor(message: string) { + super(message) + this.name = 'BillingUnavailableError' + } +} + +const StartAnswer = z.looseObject({ allow: z.boolean(), reason: z.string().nullable() }) +const UsageResponse = z.looseObject({ canStartSandbox: z.boolean(), reason: z.string().nullable() }) + +/** Reasons that mean running sandboxes must stop, not just that new starts wait. */ +export const STOP_RUNNING_REASONS: ReadonlySet = new Set([ + 'included_hours_exhausted', + 'overage_cap_reached', + 'subscription_inactive', + 'no_subscription' +]) + +export function createEntitlementClient(env: Env, doFetch: typeof fetch = fetch): EntitlementClient { + if (env.ENTITLEMENTS_MODE === 'stub') { + if (env.SANDBOX_ENV === 'prod') { + throw new BillingUnavailableError('stub entitlements refused in prod') + } + return { + checkStart: async () => ({ allow: true, reason: null }), + reportUsage: async () => ({ canStartSandbox: true, reason: null }) + } + } + const base = env.BILLING_URL + const token = env.CONTROL_PLANE_TOKEN + async function post(path: string, body: unknown, schema: z.ZodType): Promise { + if (!base || !token) { + throw new BillingUnavailableError('billing not configured') + } + let response: Response + try { + response = await doFetch(new URL(path, base), { + method: 'POST', + headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify(body), + redirect: 'manual', + signal: AbortSignal.timeout(10_000) + }) + } catch { + throw new BillingUnavailableError('billing unreachable') + } + if (!response.ok) { + throw new BillingUnavailableError(`billing answered ${response.status}`) + } + const parsed = schema.safeParse(await response.json().catch(() => null)) + if (!parsed.success) { + throw new BillingUnavailableError('billing answered an unexpected body') + } + return parsed.data + } + return { + checkStart: (request) => post('/v1/internal/entitlements/check', request, StartAnswer), + reportUsage: (segment) => post('/v1/internal/usage', segment, UsageResponse) + } +} diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts new file mode 100644 index 00000000000..2ce0646fd46 --- /dev/null +++ b/cloud/apps/sandbox-control/src/env.ts @@ -0,0 +1,33 @@ +// Sandbox control-plane bindings. Secrets are set with `wrangler secret put` (stdin), never in +// wrangler.jsonc or git. Deployed environments live in the private deploy config. +export type Env = { + SANDBOX_DB: D1Database + /** `prod` | `dev` | `local`; tagged on every provider resource so environments never mix. */ + SANDBOX_ENV: string + + // Vercel: Orca's own Pro team. A team-scoped token limited to Sandbox; users never see it. + VERCEL_TOKEN?: string + VERCEL_TEAM_ID?: string + VERCEL_PROJECT_ID?: string + VERCEL_REGION?: string + /** Overrides the Vercel API origin (tests only). */ + VERCEL_API_BASE?: string + /** Golden image (orcad + agent CLIs) every new sandbox boots from; see image/. */ + GOLDEN_SNAPSHOT_ID?: string + + // Desktop tokens: short-lived ES256 JWTs from Orca Cloud auth, aud=SANDBOX_JWT_AUDIENCE. + // SANDBOX_JWKS (inline set) is for local dev and tests only. + SANDBOX_JWKS_URL?: string + SANDBOX_JWKS?: string + SANDBOX_AUTH_ISSUER?: string + SANDBOX_JWT_AUDIENCE: string + + // Billing Worker (entitlement checks + usage). CONTROL_PLANE_TOKEN is shared with it. + BILLING_URL?: string + CONTROL_PLANE_TOKEN?: string + /** `stub` allows every start and records nothing: local dev only, refused when SANDBOX_ENV=prod. */ + ENTITLEMENTS_MODE?: string + + /** Public URL of this Worker's /v1/egress/proxy; Vercel forwardURL rules point here. */ + EGRESS_PROXY_URL?: string +} diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts new file mode 100644 index 00000000000..0420b2fcd69 --- /dev/null +++ b/cloud/apps/sandbox-control/src/index.ts @@ -0,0 +1,194 @@ +// Orca sandbox control plane: owns Orca's Vercel credentials and runs one persistent sandbox per +// Orca Cloud workspace for the signed-in account. Desktop routes (Orca JWT, aud=orca-sandbox): +// POST /v1/sandboxes {workspaceId, machineSize, region?, repo?} -> {sandbox, connection} +// GET /v1/sandboxes -> {sandboxes} +// GET /v1/sandboxes/:id -> {sandbox} +// POST /v1/sandboxes/:id/suspend | /resume | /rollover -> {sandbox[, connection]} +// POST /v1/sandboxes/:id/lease {extendMs} -> {sandbox} +// DELETE /v1/sandboxes/:id -> {sandbox} +// Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy +import { authenticateDesktop } from './auth' +import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker } from './egress-proxy' +import { BillingUnavailableError, createEntitlementClient, type EntitlementClient } from './entitlements' +import type { Env } from './env' +import { + LifecycleError, + createSandbox, + destroySandbox, + githubPlaceholderHeader, + renewLease, + resumeSandbox, + rolloverSandbox, + suspendSandbox, + type LifecycleDeps +} from './lifecycle' +import { reconcile } from './reconcile' +import { ProviderError, type SandboxProviderApi } from './sandbox-provider' +import { VmCommandError } from './sandbox-vm-commands' +import { getSandbox, getSandboxByProviderName, listAccountSandboxes } from './store' +import { createVercelSandboxApi } from './vercel-sandbox-api' +import { CreateRequestSchema, LeaseRequestSchema, readyView, sandboxView } from './wire' + +const MAX_JSON_BYTES = 16 * 1024 + +export type Deps = { + provider?: SandboxProviderApi + entitlements?: EntitlementClient + credentials?: CredentialBroker + now?: () => number + egressFetch?: typeof fetch + egressKeysForIssuer?: Parameters[1]['keysForIssuer'] +} + +function json(body: unknown, status = 200): Response { + return Response.json(body, { status, headers: { 'cache-control': 'no-store' } }) +} + +function fail(error: string, status: number, reason?: string): Response { + return json(reason ? { error, reason } : { error }, status) +} + +async function readJson(request: Request): Promise { + const text = await request.text() + if (text.length > MAX_JSON_BYTES) { + throw new SyntaxError('too_large') + } + return text ? JSON.parse(text) : {} +} + +/** Lifecycle routes refuse to run half-configured rather than guess at Orca's provider account. */ +function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { + if (!env.GOLDEN_SNAPSHOT_ID || (!deps.provider && (!env.VERCEL_TOKEN || !env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID))) { + return null + } + return { + db: env.SANDBOX_DB, + provider: + deps.provider ?? + createVercelSandboxApi({ + token: env.VERCEL_TOKEN!, + teamId: env.VERCEL_TEAM_ID!, + projectId: env.VERCEL_PROJECT_ID!, + baseUrl: env.VERCEL_API_BASE + }), + entitlements: deps.entitlements ?? createEntitlementClient(env), + now: deps.now ?? Date.now, + config: { + environment: env.SANDBOX_ENV, + goldenSnapshotId: env.GOLDEN_SNAPSHOT_ID, + defaultRegion: env.VERCEL_REGION ?? 'iad1', + egressProxyUrl: env.EGRESS_PROXY_URL + } + } +} + +export async function handleRequest(request: Request, env: Env, deps: Deps = {}): Promise { + const url = new URL(request.url) + try { + if (url.pathname === '/v1/egress/proxy' || url.pathname.startsWith('/v1/egress/proxy/')) { + if (!env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID) { + return fail('not_configured', 503) + } + return await handleEgressProxy(request, { + teamId: env.VERCEL_TEAM_ID, + projectId: env.VERCEL_PROJECT_ID, + findSandbox: (name) => getSandboxByProviderName(env.SANDBOX_DB, name), + placeholderFor: githubPlaceholderHeader, + credentials: deps.credentials ?? NO_CREDENTIALS, + fetch: deps.egressFetch, + keysForIssuer: deps.egressKeysForIssuer + }) + } + const match = /^\/v1\/sandboxes(?:\/([A-Za-z0-9_]+)(?:\/(suspend|resume|lease|rollover))?)?$/.exec(url.pathname) + if (!match) { + return fail('not_found', 404) + } + const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)()) + if (!caller) { + return fail('unauthorized', 401) + } + const life = lifecycleDeps(env, deps) + if (!life) { + return fail('not_configured', 503) + } + const [, id, action] = match + if (!id) { + if (request.method === 'GET') { + const rows = await listAccountSandboxes(env.SANDBOX_DB, caller.accountId) + return json({ sandboxes: rows.map(sandboxView) }) + } + if (request.method !== 'POST') { + return fail('not_found', 404) + } + const body = CreateRequestSchema.safeParse(await readJson(request)) + if (!body.success) { + return fail('invalid_request', 400) + } + const ready = await createSandbox(life, caller, body.data) + return json(readyView(ready.row, ready.connection)) + } + const row = await getSandbox(env.SANDBOX_DB, id) + // Another account's sandbox is indistinguishable from a missing one. + if (!row || row.account_id !== caller.accountId) { + return fail('not_found', 404) + } + const route = `${request.method} ${action ?? ''}` + switch (route) { + case 'GET ': + return json({ sandbox: sandboxView(row) }) + case 'DELETE ': + return json({ sandbox: sandboxView(await destroySandbox(life, row)) }) + case 'POST suspend': + return json({ sandbox: sandboxView(await suspendSandbox(life, row)) }) + case 'POST resume': { + const ready = await resumeSandbox(life, row) + return json(readyView(ready.row, ready.connection)) + } + case 'POST rollover': { + const ready = await rolloverSandbox(life, row) + return json(readyView(ready.row, ready.connection)) + } + case 'POST lease': { + const body = LeaseRequestSchema.safeParse(await readJson(request)) + if (!body.success) { + return fail('invalid_request', 400) + } + return json({ sandbox: sandboxView(await renewLease(life, row, body.data.extendMs)) }) + } + default: + return fail('not_found', 404) + } + } catch (error) { + return errorResponse(error) + } +} + +function errorResponse(error: unknown): Response { + if (error instanceof SyntaxError) { + return fail('invalid_json', 400) + } + if (error instanceof LifecycleError) { + const status = { not_found: 404, conflict: 409, entitlement_denied: 403, provider_unavailable: 502 }[error.code] + return fail(error.code, status, error.reason) + } + if (error instanceof BillingUnavailableError) { + return fail('billing_unavailable', 503) + } + // Provider and VM failures are summarized; their bodies never reach the desktop. + if (error instanceof ProviderError || error instanceof VmCommandError) { + return fail('provider_unavailable', 502, error instanceof VmCommandError ? error.code : undefined) + } + return fail('internal_error', 500) +} + +export default { + fetch(request: Request, env: Env): Promise { + return handleRequest(request, env) + }, + async scheduled(_controller: ScheduledController, env: Env, ctx: ExecutionContext): Promise { + const life = lifecycleDeps(env, {}) + if (life) { + ctx.waitUntil(reconcile(life).then(() => undefined)) + } + } +} satisfies ExportedHandler diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts new file mode 100644 index 00000000000..1ff07ff58a9 --- /dev/null +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -0,0 +1,301 @@ +// Sandbox lifecycle: one persistent provider sandbox per Orca workspace, with an Orca-owned lease, +// a forced stop before the provider's 24h cap, usage metering and write-ahead rows. +import type { EntitlementClient, MachineSize } from './entitlements' +import { compileNetworkPolicy, type CredentialRoute } from './network-policy' +import type { ProviderSandboxState, SandboxProviderApi } from './sandbox-provider' +import { ORCAD_PORT, drainOrcad, startOrcad, type OrcadReady } from './sandbox-vm-commands' +import { + NO_SESSION, + countActiveSandboxes, + getSandbox, + insertSandbox, + liveSandboxForWorkspace, + markSnapshotsDeleted, + recordSnapshots, + recordedSnapshotIds, + transition, + type SandboxRow, + type SessionFields +} from './store' +import { flushUsage, queueUsage } from './usage' + +export const LEASE_MS = 30 * 60_000 +const PROVIDER_SESSION_CAP_MS = 24 * 60 * 60_000 +/** Orca stops 10 min early so it, not the provider, ends the session (drain first). */ +export const SESSION_DEADLINE_MS = PROVIDER_SESSION_CAP_MS - 10 * 60_000 +const VCPUS: Record = { standard: 4, large: 8, xlarge: 16 } + +export type LifecycleDeps = { + db: D1Database + provider: SandboxProviderApi + entitlements: EntitlementClient + now: () => number + config: { + environment: string + goldenSnapshotId: string + defaultRegion: string + egressProxyUrl?: string + } +} + +export class LifecycleError extends Error { + constructor( + readonly code: 'not_found' | 'conflict' | 'entitlement_denied' | 'provider_unavailable', + readonly reason?: string + ) { + super(reason ? `${code}:${reason}` : code) + this.name = 'LifecycleError' + } +} + +export type Ready = { row: SandboxRow; connection: OrcadReady } + +function randomId(): string { + const bytes = crypto.getRandomValues(new Uint8Array(12)) + return `sbx_${Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')}` +} + +/** What the VM sends as its GitHub API credential; the egress proxy swaps it for the real token. */ +export function githubPlaceholderHeader(row: SandboxRow): string { + return `Bearer orca-ph-${row.id}` +} + +function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { + const credentials: CredentialRoute[] = deps.config.egressProxyUrl + ? [ + { + host: 'api.github.com', + header: 'authorization', + placeholder: githubPlaceholderHeader(row), + via: 'forward', + forwardURL: deps.config.egressProxyUrl + } + ] + : [] + return compileNetworkPolicy({ mode: 'allowlist', credentials }) +} + +export function sessionFields(state: ProviderSandboxState, now: number): SessionFields { + const startedAt = state.session.startedAt ?? now + return { + session_id: state.session.id, + session_started_at: startedAt, + session_deadline_at: startedAt + SESSION_DEADLINE_MS, + lease_expires_at: startedAt + state.session.timeoutMs, + usage_settled_at: startedAt + } +} + +async function claim(deps: LifecycleDeps, row: SandboxRow, patch: Partial): Promise { + const next = await transition(deps.db, row, patch, deps.now()) + if (!next) { + throw new LifecycleError('conflict', 'concurrent_transition') + } + return next +} + +async function requireStart(deps: LifecycleDeps, accountId: string, machineSize: MachineSize) { + const runningSandboxes = await countActiveSandboxes(deps.db, accountId) + const decision = await deps.entitlements.checkStart({ accountId, machineSize, runningSandboxes }) + if (!decision.allow) { + throw new LifecycleError('entitlement_denied', decision.reason ?? 'denied') + } +} + +async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandboxState, repo?: { url: string; ref?: string }): Promise { + if (state.sandbox.tags.orcaSandbox !== row.id) { + throw new LifecycleError('conflict', 'ownership_mismatch') + } + const connection = await startOrcad(deps.provider, state, { + sandboxId: row.id, + repo, + githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderHeader(row) : undefined + }) + const running = await claim(deps, row, { status: 'running', last_error: null, ...sessionFields(state, deps.now()) }) + return { row: running, connection } +} + +export async function createSandbox( + deps: LifecycleDeps, + caller: { accountId: string; userId: string }, + request: { workspaceId: string; machineSize: MachineSize; region?: string; repo?: { url: string; ref?: string } } +): Promise { + const existing = await liveSandboxForWorkspace(deps.db, caller.accountId, request.workspaceId) + if (existing) { + if (existing.status !== 'running' || !existing.session_id) { + throw new LifecycleError('conflict', `sandbox_${existing.status}`) + } + // Retried create after a lost response: hand back the same environment. + const state = await deps.provider.getSandbox(existing.provider_name, { resume: false }) + if (!state || state.session.status !== 'running') { + throw new LifecycleError('conflict', 'sandbox_not_running') + } + return { row: existing, connection: await startOrcad(deps.provider, state, { sandboxId: existing.id }) } + } + await requireStart(deps, caller.accountId, 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 + }, + deps.now() + ) + 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), + 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) + } catch (error) { + await failAndRelease(deps, (await getSandbox(deps.db, row.id)) ?? row) + throw error + } +} + +/** A create that never became usable is destroyed now; the cron retries if this also fails. */ +export async function failAndRelease(deps: LifecycleDeps, row: SandboxRow): Promise { + const failed = await transition(deps.db, row, { status: 'failed', last_error: 'create_failed' }, deps.now()) + if (failed) { + await destroySandbox(deps, failed).catch(() => undefined) + } +} + +async function stopForSuspend(deps: LifecycleDeps, row: SandboxRow): Promise { + if (row.session_id) { + await drainOrcad(deps.provider, row.session_id) + const stopped = await deps.provider.stopSession(row.session_id) + if (stopped.snapshotId) { + await recordSnapshots(deps.db, row.id, [stopped.snapshotId], deps.now()) + } + } + await queueUsage(deps.db, row, deps.now()) + return claim(deps, row, { status: 'suspended', ...NO_SESSION }) +} + +/** The provider ended the session on its own (lease lapsed): bill up to when it stopped. */ +export async function markProviderStopped(deps: LifecycleDeps, row: SandboxRow): Promise { + await queueUsage(deps.db, row, Math.min(deps.now(), row.lease_expires_at ?? deps.now())) + return claim(deps, row, { status: 'suspended', ...NO_SESSION }) +} + +export async function suspendSandbox(deps: LifecycleDeps, row: SandboxRow): Promise { + if (row.status === 'suspended') { + return row + } + if (row.status !== 'running') { + throw new LifecycleError('conflict', `sandbox_${row.status}`) + } + const suspending = await claim(deps, row, { status: 'suspending' }) + const suspended = await stopForSuspend(deps, suspending) + await flushUsage(deps.db, deps.entitlements, deps.now(), row.id) + return suspended +} + +export async function resumeSandbox(deps: LifecycleDeps, row: SandboxRow): Promise { + if (row.status === 'running') { + const state = await deps.provider.getSandbox(row.provider_name, { resume: false }) + if (state?.session.status === 'running') { + return { row, connection: await startOrcad(deps.provider, state, { sandboxId: row.id }) } + } + // The provider ended the session (lease lapsed): fall through to a real resume. + row = await markProviderStopped(deps, row) + } + if (row.status !== 'suspended') { + throw new LifecycleError('conflict', `sandbox_${row.status}`) + } + await requireStart(deps, row.account_id, row.machine_size) + const resuming = await claim(deps, row, { status: 'resuming' }) + return wake(deps, resuming) +} + +async function wake(deps: LifecycleDeps, row: SandboxRow): Promise { + const state = await deps.provider.getSandbox(row.provider_name, { resume: true }) + 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) + } catch (error) { + // Leave it resumable: the disk is intact, only this boot failed. + const current = await getSandbox(deps.db, row.id) + if (current?.status === 'resuming') { + await transition(deps.db, current, { status: 'suspended', ...NO_SESSION, last_error: 'resume_failed' }, deps.now()) + } + throw error + } +} + +/** 24h cap or opportunistic refresh: drain, stop (snapshot), boot the same disk again. */ +export async function rolloverSandbox(deps: LifecycleDeps, row: SandboxRow): Promise { + if (row.status !== 'running') { + throw new LifecycleError('conflict', `sandbox_${row.status}`) + } + const suspending = await claim(deps, row, { status: 'suspending' }) + const stopped = await stopForSuspend(deps, suspending) + await flushUsage(deps.db, deps.entitlements, deps.now(), row.id) + return wake(deps, await claim(deps, stopped, { status: 'resuming' })) +} + +export async function renewLease(deps: LifecycleDeps, row: SandboxRow, extendMs: number): Promise { + if (row.status !== 'running' || !row.session_id || row.lease_expires_at === null || row.session_deadline_at === null) { + throw new LifecycleError('conflict', `sandbox_${row.status}`) + } + const target = Math.min(row.session_deadline_at, deps.now() + extendMs) + if (target <= row.lease_expires_at) { + return row + } + const session = await deps.provider.extendTimeout(row.session_id, target - row.lease_expires_at) + const startedAt = row.session_started_at ?? deps.now() + return claim(deps, row, { lease_expires_at: startedAt + session.timeoutMs }) +} + +export async function destroySandbox(deps: LifecycleDeps, row: SandboxRow): Promise { + if (row.status === 'destroyed') { + return row + } + let destroying = row.status === 'destroying' ? row : await claim(deps, row, { status: 'destroying' }) + const settled = await queueUsage(deps.db, destroying, deps.now()) + if (settled !== destroying.usage_settled_at) { + destroying = await claim(deps, destroying, { usage_settled_at: settled }) + } + await deps.provider.deleteSandbox(destroying.provider_name) + // Persistent sandboxes leave automatic snapshots behind delete; prove none survive. + const listed = await deps.provider.listSnapshots(destroying.provider_name) + await recordSnapshots(deps.db, row.id, listed.map((s) => s.id), deps.now()) + for (const id of await recordedSnapshotIds(deps.db, row.id)) { + if (id !== deps.config.goldenSnapshotId) { + await deps.provider.deleteSnapshot(id) + } + } + const remaining = (await deps.provider.listSnapshots(destroying.provider_name)).filter( + (s) => s.status !== 'deleted' && s.id !== deps.config.goldenSnapshotId + ) + if (remaining.length > 0) { + throw new LifecycleError('provider_unavailable', 'snapshot_cleanup_incomplete') + } + await markSnapshotsDeleted(deps.db, row.id, deps.now()) + const destroyed = await claim(deps, destroying, { status: 'destroyed', ...NO_SESSION, destroyed_at: deps.now() }) + await flushUsage(deps.db, deps.entitlements, deps.now(), row.id) + return destroyed +} diff --git a/cloud/apps/sandbox-control/src/network-policy.ts b/cloud/apps/sandbox-control/src/network-policy.ts new file mode 100644 index 00000000000..1d19a9ae02d --- /dev/null +++ b/cloud/apps/sandbox-control/src/network-policy.ts @@ -0,0 +1,110 @@ +// Fail-closed egress for Orca Cloud sandboxes (Vercel v2 record-form network policy). +// - No "*" entry unless a workspace opts into open egress: unlisted domains do not resolve. +// - Credential hosts end with a 403 rule, so only requests carrying our placeholder get through. +// - Hosts Node clients use are forwarded to our egress proxy instead of transformed: with a +// transform rule Vercel's proxy answers HTTP/1.1 without content-length or transfer-encoding and +// undici fetch hangs on the body (bake-off). curl/git and Go clients are unaffected. + +type Matcher = { exact?: string; startsWith?: string } +export type NetworkPolicyRule = + | { + match?: { headers?: { key: Matcher; value: Matcher }[] } + transform: { headers: Record }[] + } + | { match?: { headers?: { key: Matcher; value: Matcher }[] }; forwardURL: string } + | { response: { statusCode: number; body: string; contentType: string } } + +export type NetworkPolicy = { allow: Record } + +export const EGRESS_DENIED_BODY = 'orca-egress-denied' + +/** Registries, git hosting and agent model/sign-in hosts a coding VM needs. */ +export const DEFAULT_EGRESS_ALLOWLIST: readonly string[] = [ + 'registry.npmjs.org', + 'registry.yarnpkg.com', + 'pypi.org', + 'files.pythonhosted.org', + 'crates.io', + 'index.crates.io', + 'static.crates.io', + 'proxy.golang.org', + 'sum.golang.org', + 'rubygems.org', + 'nodejs.org', + 'archive.ubuntu.com', + 'security.ubuntu.com', + 'codeload.github.com', + 'objects.githubusercontent.com', + 'raw.githubusercontent.com', + 'api.anthropic.com', + 'claude.ai', + 'console.anthropic.com', + 'api.openai.com', + 'auth.openai.com', + 'chatgpt.com', + 'generativelanguage.googleapis.com', + 'oauth2.googleapis.com', + 'accounts.google.com', + '*.onorca.dev' +] + +export type CredentialRoute = { + host: string + /** Header the VM sends with the placeholder value, matched exactly. */ + header: string + placeholder: string +} & ( + | { via: 'transform'; value: string } + | { via: 'forward'; forwardURL: string } +) + +export type EgressPolicyInput = { + mode: 'allowlist' | 'open' + allowlist?: readonly string[] + credentials?: readonly CredentialRoute[] +} + +const HOST = /^(\*\.)?([a-z0-9-]+\.)+[a-z]{2,63}$/ + +export function compileNetworkPolicy(input: EgressPolicyInput): NetworkPolicy { + const allow: Record = {} + for (const host of input.allowlist ?? DEFAULT_EGRESS_ALLOWLIST) { + allow[checkedHost(host)] = [] + } + for (const route of input.credentials ?? []) { + const host = checkedHost(route.host) + if (host.startsWith('*.')) { + throw new Error('credential routes need an exact host') + } + const match = { + headers: [{ key: { exact: route.header.toLowerCase() }, value: { exact: route.placeholder } }] + } + const claim: NetworkPolicyRule = + route.via === 'transform' + ? { match, transform: [{ headers: { [route.header.toLowerCase()]: route.value } }] } + : { match, forwardURL: route.forwardURL } + const rules = allow[host]?.filter((rule) => !('response' in rule)) ?? [] + allow[host] = [...rules, claim, denyRule()] + } + if (input.mode === 'open') { + allow['*'] = [] + } + return { allow } +} + +function denyRule(): NetworkPolicyRule { + return { response: { statusCode: 403, body: EGRESS_DENIED_BODY, contentType: 'text/plain' } } +} + +function checkedHost(host: string): string { + const normalized = host.trim().toLowerCase() + if (!HOST.test(normalized)) { + throw new Error(`invalid egress host: ${host}`) + } + return normalized +} + +/** Basic credential git sends when `http..extraheader` carries the placeholder token. */ +export function gitBasicAuthorization(token: string): string { + return `Basic ${btoa(`x-access-token:${token}`)}` +} diff --git a/cloud/apps/sandbox-control/src/reconcile.ts b/cloud/apps/sandbox-control/src/reconcile.ts new file mode 100644 index 00000000000..a4303f59978 --- /dev/null +++ b/cloud/apps/sandbox-control/src/reconcile.ts @@ -0,0 +1,98 @@ +// Cron pass: the control plane, not the desktop, guarantees metering, the session deadline and cleanup, +// so a closed laptop or a crash mid-call can never leave a sandbox billed-but-untracked. +import { STOP_RUNNING_REASONS } from './entitlements' +import { + destroySandbox, + failAndRelease, + markProviderStopped, + sessionFields, + suspendSandbox, + type LifecycleDeps +} from './lifecycle' +import { listByStatus, transition, type SandboxRow } from './store' +import { flushUsage, queueUsage } from './usage' + +const STUCK_MS = 15 * 60_000 + +export type ReconcileSummary = Record< + 'settled' | 'suspendedForDeadline' | 'suspendedForBilling' | 'leaseLapsed' | 'released' | 'repaired', + number +> + +export async function reconcile(deps: LifecycleDeps): Promise { + const summary: ReconcileSummary = { + settled: 0, + suspendedForDeadline: 0, + suspendedForBilling: 0, + leaseLapsed: 0, + released: 0, + repaired: 0 + } + for (const row of await listByStatus(deps.db, ['running'])) { + await guard(() => reconcileRunning(deps, row, summary)) + } + const now = deps.now() + for (const row of await listByStatus(deps.db, ['creating', 'failed', 'destroying', 'suspending', 'resuming'])) { + if (now - row.updated_at < STUCK_MS) { + continue + } + await guard(async () => { + if (row.status === 'creating') { + await failAndRelease(deps, row) + summary.released++ + } else if (row.status === 'failed' || row.status === 'destroying') { + await destroySandbox(deps, row) + summary.released++ + } else { + await repairTransition(deps, row) + summary.repaired++ + } + }) + } + await flushUsage(deps.db, deps.entitlements, deps.now()) + return summary +} + +async function reconcileRunning(deps: LifecycleDeps, row: SandboxRow, summary: ReconcileSummary): Promise { + const now = deps.now() + if (row.session_deadline_at !== null && now >= row.session_deadline_at) { + // The desktop should have rolled over; stop before the provider's hard cap kills it undrained. + await suspendSandbox(deps, row) + summary.suspendedForDeadline++ + return + } + if (row.lease_expires_at !== null && now >= row.lease_expires_at) { + const state = await deps.provider.getSandbox(row.provider_name, { resume: false }) + if (!state || state.session.status !== 'running') { + await markProviderStopped(deps, row) + summary.leaseLapsed++ + return + } + } + const settled = await queueUsage(deps.db, row, now) + const current = settled === row.usage_settled_at ? row : await transition(deps.db, row, { usage_settled_at: settled }, now) + summary.settled++ + const answer = await flushUsage(deps.db, deps.entitlements, now, row.id) + if (current && answer && !answer.canStartSandbox && STOP_RUNNING_REASONS.has(answer.reason ?? '')) { + await suspendSandbox(deps, current) + summary.suspendedForBilling++ + } +} + +/** A suspend/resume call died mid-way: adopt whatever the provider says is true. */ +async function repairTransition(deps: LifecycleDeps, row: SandboxRow): Promise { + const state = await deps.provider.getSandbox(row.provider_name, { resume: false }) + if (state?.session.status === 'running') { + await transition(deps.db, row, { status: 'running', ...sessionFields(state, deps.now()) }, deps.now()) + return + } + await markProviderStopped(deps, row) +} + +async function guard(step: () => Promise): Promise { + try { + await step() + } catch { + // One bad row must not stall the pass; it is retried next tick. + } +} diff --git a/cloud/apps/sandbox-control/src/sandbox-provider.ts b/cloud/apps/sandbox-control/src/sandbox-provider.ts new file mode 100644 index 00000000000..270fefca426 --- /dev/null +++ b/cloud/apps/sandbox-control/src/sandbox-provider.ts @@ -0,0 +1,90 @@ +// The provider calls the control plane needs, shaped after Vercel Sandbox's persistent-sandbox API. +// Lifecycle code depends only on this, so a second provider (Freestyle, E2B) is one more adapter. +import type { NetworkPolicy } from './network-policy' + +export type ProviderSessionStatus = + | 'pending' + | 'running' + | 'stopping' + | 'stopped' + | 'failed' + | 'aborted' + | 'snapshotting' + +export type ProviderSandbox = { + name: string + status: ProviderSessionStatus + currentSessionId: string + currentSnapshotId: string | null + tags: Record +} + +export type ProviderSession = { + id: string + status: ProviderSessionStatus + startedAt: number | null + /** Session timeout in ms, counted from start. */ + timeoutMs: number +} + +export type ProviderRoute = { port: number; url: string } + +export type ProviderSandboxState = { + sandbox: ProviderSandbox + session: ProviderSession + routes: ProviderRoute[] +} + +export type ProviderSnapshot = { + id: string + status: 'created' | 'deleted' | 'failed' + sourceSessionId: string +} + +export type CreateSandboxParams = { + name: string + snapshotId: string + vcpus: number + region: string + timeoutMs: number + ports: number[] + networkPolicy: NetworkPolicy + tags: Record +} + +export type RunCommandParams = { + command: string + args?: string[] + cwd?: string + env?: Record + sudo?: boolean + timeoutMs: number +} + +export type CommandResult = { exitCode: number; stdout: string; stderr: string } + +export type SandboxProviderApi = { + createSandbox(params: CreateSandboxParams): Promise + /** `resume: true` boots a stopped persistent sandbox from its latest snapshot. Null when absent. */ + getSandbox(name: string, options: { resume: boolean }): Promise + stopSession(sessionId: string): Promise<{ snapshotId: string | null }> + extendTimeout(sessionId: string, durationMs: number): Promise + updateNetworkPolicy(sessionId: string, policy: NetworkPolicy): Promise + runCommand(sessionId: string, params: RunCommandParams): Promise + /** Resolves when already gone. */ + deleteSandbox(name: string): Promise + listSnapshots(name: string): Promise + /** Resolves when already gone. */ + deleteSnapshot(snapshotId: string): Promise +} + +/** Provider failed or answered unexpectedly; the body is never surfaced to callers. */ +export class ProviderError extends Error { + constructor( + readonly status: number, + readonly code: string | null + ) { + super(`provider request failed (${status}${code ? ` ${code}` : ''})`) + this.name = 'ProviderError' + } +} diff --git a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts new file mode 100644 index 00000000000..fc216504f79 --- /dev/null +++ b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts @@ -0,0 +1,89 @@ +// Commands the golden image installs in /opt/orca/bin (see image/provision-golden.sh). The control +// plane only ever runs these fixed entry points in a VM, never user-supplied command lines. +import { z } from 'zod' +import { ProviderError, type ProviderSandboxState, type SandboxProviderApi } from './sandbox-provider' + +export const ORCAD_PORT = 6768 +export const START_COMMAND = '/opt/orca/bin/orca-sandbox-start' +export const DRAIN_COMMAND = '/opt/orca/bin/orca-sandbox-drain' +const START_TIMEOUT_MS = 120_000 +/** Matches the parity plan: in-flight turns get up to 5 min to become durable restart offers. */ +export const DRAIN_TIMEOUT_MS = 5 * 60_000 + +const ReadySchema = z + .object({ + pairingCode: z.string().min(1).max(8192), + projectRoot: z.string().startsWith('/').max(4096) + }) + .strict() + +export type OrcadReady = z.infer + +export class VmCommandError extends Error { + constructor(readonly code: 'orcad_start_failed' | 'orcad_ready_invalid' | 'route_missing') { + super(code) + this.name = 'VmCommandError' + } +} + +export type StartInputs = { + sandboxId: string + repo?: { url: string; ref?: string } + /** Value the VM sends for credential hosts; the egress layer swaps it for the real credential. */ + githubPlaceholder?: string +} + +/** Starts orcad (idempotent per boot) and returns its pairing offer; pairing codes are never logged. */ +export async function startOrcad( + provider: SandboxProviderApi, + state: ProviderSandboxState, + inputs: StartInputs +): Promise { + const route = state.routes.find((candidate) => candidate.port === ORCAD_PORT) + if (!route) { + throw new VmCommandError('route_missing') + } + const env: Record = { + ORCA_SANDBOX_ID: inputs.sandboxId, + ORCA_PUBLIC_WSS: route.url.replace(/^https:/, 'wss:') + } + if (inputs.repo) { + env.ORCA_REPO_URL = inputs.repo.url + if (inputs.repo.ref) { + env.ORCA_REPO_REF = inputs.repo.ref + } + } + if (inputs.githubPlaceholder) { + env.ORCA_GITHUB_PLACEHOLDER = inputs.githubPlaceholder + } + const result = await provider.runCommand(state.session.id, { + command: START_COMMAND, + env, + timeoutMs: START_TIMEOUT_MS + }) + if (result.exitCode !== 0) { + throw new VmCommandError('orcad_start_failed') + } + const lastLine = result.stdout.trim().split('\n').at(-1) ?? '' + try { + return ReadySchema.parse(JSON.parse(lastLine)) + } catch { + throw new VmCommandError('orcad_ready_invalid') + } +} + +/** Best effort: orcad turns working chats into durable restart offers before the VM stops. */ +export async function drainOrcad(provider: SandboxProviderApi, sessionId: string): Promise { + try { + const result = await provider.runCommand(sessionId, { + command: DRAIN_COMMAND, + timeoutMs: DRAIN_TIMEOUT_MS + }) + return result.exitCode === 0 + } catch (error) { + if (error instanceof ProviderError) { + return false + } + throw error + } +} diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts new file mode 100644 index 00000000000..b8ff00b8766 --- /dev/null +++ b/cloud/apps/sandbox-control/src/store.ts @@ -0,0 +1,173 @@ +// D1 access for sandbox rows. Every transition is a compare-and-set on `generation`, so two +// concurrent lifecycle calls for one sandbox cannot both act on the provider. +import type { MachineSize } from './entitlements' + +export type SandboxStatus = + | 'creating' + | 'running' + | 'suspending' + | 'suspended' + | 'resuming' + | 'destroying' + | 'destroyed' + | 'failed' + +export type SandboxRow = { + id: string + account_id: string + user_id: string + workspace_id: string + provider: string + provider_name: string + status: SandboxStatus + machine_size: MachineSize + region: string + generation: number + session_id: string | null + session_started_at: number | null + session_deadline_at: number | null + lease_expires_at: number | null + usage_settled_at: number | null + last_error: string | null + created_at: number + updated_at: number + destroyed_at: number | null +} + +export type SessionFields = Pick< + SandboxRow, + 'session_id' | 'session_started_at' | 'session_deadline_at' | 'lease_expires_at' | 'usage_settled_at' +> + +export const NO_SESSION: SessionFields = { + session_id: null, + session_started_at: null, + session_deadline_at: null, + lease_expires_at: null, + usage_settled_at: null +} + +/** Counted against the plan's concurrency limit. */ +export const ACTIVE_STATUSES: readonly SandboxStatus[] = ['creating', 'running', 'resuming', 'suspending'] + +export async function insertSandbox( + db: D1Database, + row: Pick< + SandboxRow, + 'id' | 'account_id' | 'user_id' | 'workspace_id' | 'provider' | 'provider_name' | 'machine_size' | 'region' + >, + now: number +): Promise { + await db + .prepare( + `INSERT INTO sandboxes (id, account_id, user_id, workspace_id, provider, provider_name, status, + machine_size, region, generation, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, 'creating', ?, ?, 0, ?, ?)` + ) + .bind(row.id, row.account_id, row.user_id, row.workspace_id, row.provider, row.provider_name, + row.machine_size, row.region, now, now) + .run() + const inserted = await getSandbox(db, row.id) + if (!inserted) { + throw new Error('sandbox insert lost') + } + return inserted +} + +export function getSandbox(db: D1Database, id: string): Promise { + return db.prepare('SELECT * FROM sandboxes WHERE id = ?').bind(id).first() +} + +export function getSandboxByProviderName(db: D1Database, name: string): Promise { + return db.prepare('SELECT * FROM sandboxes WHERE provider_name = ?').bind(name).first() +} + +export function liveSandboxForWorkspace( + db: D1Database, + accountId: string, + workspaceId: string +): Promise { + return db + .prepare(`SELECT * FROM sandboxes WHERE account_id = ? AND workspace_id = ? AND status <> 'destroyed'`) + .bind(accountId, workspaceId) + .first() +} + +export async function listAccountSandboxes(db: D1Database, accountId: string): Promise { + const { results } = await db + .prepare(`SELECT * FROM sandboxes WHERE account_id = ? AND status <> 'destroyed' ORDER BY created_at`) + .bind(accountId) + .all() + return results +} + +export async function countActiveSandboxes(db: D1Database, accountId: string): Promise { + const placeholders = ACTIVE_STATUSES.map(() => '?').join(', ') + const row = await db + .prepare(`SELECT COUNT(*) AS n FROM sandboxes WHERE account_id = ? AND status IN (${placeholders})`) + .bind(accountId, ...ACTIVE_STATUSES) + .first<{ n: number }>() + return row?.n ?? 0 +} + +export async function listByStatus(db: D1Database, statuses: readonly SandboxStatus[]): Promise { + const placeholders = statuses.map(() => '?').join(', ') + const { results } = await db + .prepare(`SELECT * FROM sandboxes WHERE status IN (${placeholders}) ORDER BY updated_at LIMIT 200`) + .bind(...statuses) + .all() + return results +} + +type Patch = Partial> +const PATCHABLE = new Set([ + 'status', 'session_id', 'session_started_at', 'session_deadline_at', 'lease_expires_at', + 'usage_settled_at', 'last_error', 'destroyed_at' +]) + +/** Applies `patch` only if nobody moved the row since `expected` was read; null on a lost race. */ +export async function transition( + db: D1Database, + expected: SandboxRow, + patch: Patch, + now: number +): Promise { + const entries = Object.entries(patch).filter(([key]) => PATCHABLE.has(key)) + const assignments = entries.map(([key]) => `${key} = ?`).join(', ') + const result = await db + .prepare( + `UPDATE sandboxes SET ${assignments}${assignments ? ', ' : ''}generation = generation + 1, updated_at = ? + WHERE id = ? AND generation = ?` + ) + .bind(...entries.map(([, value]) => value ?? null), now, expected.id, expected.generation) + .run() + return result.meta.changes === 1 ? getSandbox(db, expected.id) : null +} + +export async function recordSnapshots(db: D1Database, sandboxId: string, ids: readonly string[], now: number) { + if (ids.length === 0) { + return + } + await db.batch( + ids.map((id) => + db + .prepare('INSERT OR IGNORE INTO sandbox_snapshots (snapshot_id, sandbox_id, recorded_at) VALUES (?, ?, ?)') + .bind(id, sandboxId, now) + ) + ) +} + +export async function markSnapshotsDeleted(db: D1Database, sandboxId: string, now: number) { + await db + .prepare('UPDATE sandbox_snapshots SET deleted_at = ? WHERE sandbox_id = ? AND deleted_at IS NULL') + .bind(now, sandboxId) + .run() +} + +export async function recordedSnapshotIds(db: D1Database, sandboxId: string): Promise { + const { results } = await db + .prepare('SELECT snapshot_id FROM sandbox_snapshots WHERE sandbox_id = ?') + .bind(sandboxId) + .all<{ snapshot_id: string }>() + return results.map((row) => row.snapshot_id) +} diff --git a/cloud/apps/sandbox-control/src/usage.ts b/cloud/apps/sandbox-control/src/usage.ts new file mode 100644 index 00000000000..381c21c8d42 --- /dev/null +++ b/cloud/apps/sandbox-control/src/usage.ts @@ -0,0 +1,75 @@ +// Metering: running time becomes outbox segments, then billing usage reports (overage → Stripe there). +import type { EntitlementClient, MachineSize, UsageAnswer } from './entitlements' +import type { SandboxRow } from './store' + +const MAX_SEGMENT_MS = 24 * 60 * 60_000 +const MAX_ATTEMPTS = 50 + +type OutboxRow = { + usage_id: string + sandbox_id: string + account_id: string + machine_size: MachineSize + started_at: number + ended_at: number + attempts: number +} + +/** Queues the row's unsettled time up to `until`; returns the new settle cursor (unchanged if none). */ +export async function queueUsage(db: D1Database, row: SandboxRow, until: number): Promise { + const from = row.usage_settled_at + if (from === null || row.session_id === null || until <= from) { + return from + } + const statements = [] + for (let start = from; start < until; start += MAX_SEGMENT_MS) { + const end = Math.min(until, start + MAX_SEGMENT_MS) + statements.push( + db + .prepare( + `INSERT OR IGNORE INTO usage_outbox (usage_id, sandbox_id, account_id, machine_size, started_at, ended_at) + VALUES (?, ?, ?, ?, ?, ?)` + ) + .bind(`${row.id}:${row.session_id}:${start}`, row.id, row.account_id, row.machine_size, start, end) + ) + } + await db.batch(statements) + return until +} + +/** Sends pending segments (one sandbox, or the oldest for the cron). Returns billing's last answer. */ +export async function flushUsage( + db: D1Database, + entitlements: EntitlementClient, + now: number, + sandboxId?: string +): Promise { + const { results } = await db + .prepare( + `SELECT * FROM usage_outbox WHERE sent_at IS NULL AND attempts < ? ${sandboxId ? 'AND sandbox_id = ?' : ''} + ORDER BY started_at LIMIT 100` + ) + .bind(...(sandboxId ? [MAX_ATTEMPTS, sandboxId] : [MAX_ATTEMPTS])) + .all() + let answer: UsageAnswer | null = null + for (const segment of results) { + try { + answer = await entitlements.reportUsage({ + usageId: segment.usage_id, + accountId: segment.account_id, + sandboxId: segment.sandbox_id, + machineSize: segment.machine_size, + startedAt: segment.started_at, + endedAt: segment.ended_at + }) + await db.prepare('UPDATE usage_outbox SET sent_at = ? WHERE usage_id = ?').bind(now, segment.usage_id).run() + } catch { + await db + .prepare('UPDATE usage_outbox SET attempts = attempts + 1 WHERE usage_id = ?') + .bind(segment.usage_id) + .run() + break + } + } + return answer +} diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts new file mode 100644 index 00000000000..f99b48673b0 --- /dev/null +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -0,0 +1,255 @@ +// Vercel Sandbox REST client for Workers. `@vercel/sandbox` needs undici/fs/xdg paths, so this +// calls the same /v2 + /v3 endpoints its api-client uses (SDK 3.9.1), with Orca's team token. +import { z } from 'zod' +import type { NetworkPolicy } from './network-policy' +import { + ProviderError, + type CommandResult, + type ProviderSandboxState, + type ProviderSession, + type SandboxProviderApi +} from './sandbox-provider' + +export type VercelSandboxApiConfig = { + token: string + teamId: string + projectId: string + baseUrl?: string + fetch?: typeof fetch +} + +const MAX_COMMAND_OUTPUT = 256 * 1024 + +const Status = z.enum(['pending', 'running', 'stopping', 'stopped', 'failed', 'aborted', 'snapshotting']) +const Session = z.looseObject({ + id: z.string(), + status: Status, + startedAt: z.number().optional(), + timeout: z.number() +}) +const Sandbox = z.looseObject({ + name: z.string(), + status: Status, + currentSessionId: z.string(), + currentSnapshotId: z.string().optional(), + tags: z.record(z.string(), z.string()).optional() +}) +const Route = z.looseObject({ url: z.string(), port: z.number() }) +const SandboxAndSession = z.looseObject({ sandbox: Sandbox, session: Session, routes: z.array(Route) }) +const Snapshot = z.looseObject({ + id: z.string(), + status: z.enum(['created', 'deleted', 'failed']), + sourceSessionId: z.string() +}) +const ErrorBody = z.looseObject({ error: z.looseObject({ code: z.string().optional() }).optional() }) +const CommandLine = z.looseObject({ command: z.looseObject({ exitCode: z.number().nullable() }) }) +const OutputLine = z.looseObject({ stream: z.enum(['stdout', 'stderr']), data: z.string() }) +const FailureLine = z.looseObject({ stream: z.literal('error'), data: z.looseObject({ code: z.string() }) }) + +export function createVercelSandboxApi(config: VercelSandboxApiConfig): SandboxProviderApi { + const base = config.baseUrl ?? 'https://vercel.com/api' + const doFetch = config.fetch ?? fetch + + async function request( + method: string, + path: string, + options: { query?: Record; body?: unknown } = {} + ): Promise { + const url = new URL(`${base}${path}`) + url.searchParams.set('teamId', config.teamId) + for (const [key, value] of Object.entries(options.query ?? {})) { + if (value !== undefined) { + url.searchParams.set(key, value) + } + } + const response = await doFetch(url, { + method, + headers: { + authorization: `Bearer ${config.token}`, + 'content-type': 'application/json' + }, + body: options.body === undefined ? undefined : JSON.stringify(options.body), + redirect: 'manual' + }) + if (!response.ok) { + const parsed = ErrorBody.safeParse(await response.json().catch(() => null)) + throw new ProviderError(response.status, parsed.success ? (parsed.data.error?.code ?? null) : null) + } + return response + } + + async function json(schema: z.ZodType, response: Response): Promise { + const parsed = schema.safeParse(await response.json().catch(() => null)) + if (!parsed.success) { + throw new ProviderError(response.status, 'invalid_response') + } + return parsed.data + } + + const toSession = (session: z.infer): ProviderSession => ({ + id: session.id, + status: session.status, + startedAt: session.startedAt ?? null, + timeoutMs: session.timeout + }) + const toState = (body: z.infer): ProviderSandboxState => ({ + sandbox: { + name: body.sandbox.name, + status: body.sandbox.status, + currentSessionId: body.sandbox.currentSessionId, + currentSnapshotId: body.sandbox.currentSnapshotId ?? null, + tags: body.sandbox.tags ?? {} + }, + session: toSession(body.session), + routes: body.routes.map((route) => ({ port: route.port, url: route.url })) + }) + const missingOk = async (operation: () => Promise): Promise => { + try { + await operation() + } catch (error) { + if (!(error instanceof ProviderError && error.status === 404)) { + throw error + } + } + } + + return { + async createSandbox(params) { + // No `runtime` selects /v3, which boots from `source` (the golden snapshot). + const response = await request('POST', '/v3/sandboxes', { + body: { + projectId: config.projectId, + name: params.name, + source: { type: 'snapshot', snapshotId: params.snapshotId }, + resources: { vcpus: params.vcpus }, + region: params.region, + timeout: params.timeoutMs, + ports: params.ports, + persistent: true, + keepLastSnapshots: { count: 2 }, + snapshotExpiration: 0, + networkPolicy: params.networkPolicy, + tags: params.tags + } + }) + return toState(await json(SandboxAndSession, response)) + }, + + async getSandbox(name, options) { + try { + const response = await request('GET', `/v2/sandboxes/${encodeURIComponent(name)}`, { + query: { projectId: config.projectId, resume: String(options.resume) } + }) + return toState(await json(SandboxAndSession, response)) + } catch (error) { + if (error instanceof ProviderError && error.status === 404) { + return null + } + throw error + } + }, + + async stopSession(sessionId) { + const response = await request('POST', `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/stop`) + const body = await json(z.looseObject({ snapshot: Snapshot.optional() }), response) + return { snapshotId: body.snapshot?.id ?? null } + }, + + async extendTimeout(sessionId, durationMs) { + const response = await request( + 'POST', + `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/extend-timeout`, + { body: { duration: durationMs } } + ) + return toSession((await json(z.looseObject({ session: Session }), response)).session) + }, + + async updateNetworkPolicy(sessionId, policy: NetworkPolicy) { + await request('POST', `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/network-policy`, { + body: policy + }) + }, + + async runCommand(sessionId, params) { + const response = await request('POST', `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/cmd`, { + body: { + command: params.command, + args: params.args ?? [], + cwd: params.cwd, + env: params.env ?? {}, + sudo: params.sudo ?? false, + wait: true, + logs: true, + timeout: params.timeoutMs + } + }) + return readCommandStream(response) + }, + + deleteSandbox: (name) => + missingOk(() => + request('DELETE', `/v2/sandboxes/${encodeURIComponent(name)}`, { + query: { projectId: config.projectId, deleteOrphanSnapshots: 'true' } + }) + ), + + async listSnapshots(name) { + const snapshots: z.infer[] = [] + let cursor: string | undefined + do { + const response = await request('GET', '/v2/sandboxes/snapshots', { + query: { project: config.projectId, name, limit: '50', cursor } + }) + const page = await json( + z.looseObject({ + snapshots: z.array(Snapshot), + pagination: z.looseObject({ next: z.string().nullable() }) + }), + response + ) + snapshots.push(...page.snapshots) + cursor = page.pagination.next ?? undefined + } while (cursor && snapshots.length < 1000) + return snapshots.map((s) => ({ id: s.id, status: s.status, sourceSessionId: s.sourceSessionId })) + }, + + deleteSnapshot: (snapshotId) => + missingOk(() => request('DELETE', `/v2/sandboxes/snapshots/${encodeURIComponent(snapshotId)}`)) + } +} + +/** `wait: true` answers ndjson: the command, its log lines, then the finished command. */ +async function readCommandStream(response: Response): Promise { + const text = await response.text() + const output = { stdout: '', stderr: '' } + let exitCode: number | null = null + for (const line of text.split('\n')) { + if (!line.trim()) { + continue + } + let value: unknown + try { + value = JSON.parse(line) + } catch { + throw new ProviderError(response.status, 'invalid_response') + } + const command = CommandLine.safeParse(value) + const log = OutputLine.safeParse(value) + const failure = FailureLine.safeParse(value) + if (command.success) { + exitCode = command.data.command.exitCode ?? exitCode + } else if (log.success) { + if (output[log.data.stream].length < MAX_COMMAND_OUTPUT) { + output[log.data.stream] += log.data.data + } + } else if (failure.success) { + throw new ProviderError(response.status, failure.data.data.code) + } else { + throw new ProviderError(response.status, 'invalid_response') + } + } + if (exitCode === null) { + throw new ProviderError(response.status, 'command_unfinished') + } + return { exitCode, ...output } +} diff --git a/cloud/apps/sandbox-control/src/wire.ts b/cloud/apps/sandbox-control/src/wire.ts new file mode 100644 index 00000000000..2028482af22 --- /dev/null +++ b/cloud/apps/sandbox-control/src/wire.ts @@ -0,0 +1,70 @@ +// Request schemas and response views. Mirrors src/shared/hosted-sandbox/hosted-sandbox-wire.ts on +// the desktop; test/wire-contract.test.ts parses every response with the desktop schemas. +import { z } from 'zod' +import type { OrcadReady } from './sandbox-vm-commands' +import type { SandboxRow } from './store' + +const Id = z + .string() + .min(1) + .max(128) + .regex(/^[A-Za-z0-9_.:-]+$/) + +export const CreateRequestSchema = z + .object({ + workspaceId: Id, + machineSize: z.enum(['standard', 'large', 'xlarge']), + region: z + .string() + .regex(/^[a-z]{3}[0-9]$/) + .optional(), + repo: z + .object({ + url: z.url({ protocol: /^https$/ }).max(2048), + ref: z.string().min(1).max(255).optional() + }) + .strict() + .optional() + }) + .strict() + +export const LeaseRequestSchema = z + .object({ + extendMs: z + .number() + .int() + .min(60_000) + .max(60 * 60_000) + }) + .strict() + +export function sandboxView(row: SandboxRow) { + const session = + row.session_id !== null && + row.session_started_at !== null && + row.session_deadline_at !== null && + row.lease_expires_at !== null + ? { + id: row.session_id, + startedAt: row.session_started_at, + deadlineAt: row.session_deadline_at, + leaseExpiresAt: row.lease_expires_at + } + : null + return { + id: row.id, + workspaceId: row.workspace_id, + status: row.status, + machineSize: row.machine_size, + region: row.region, + session, + createdAt: row.created_at + } +} + +export function readyView(row: SandboxRow, connection: OrcadReady) { + return { + sandbox: sandboxView(row), + connection: { type: 'orca-server' as const, ...connection } + } +} diff --git a/cloud/apps/sandbox-control/test/d1-fake.ts b/cloud/apps/sandbox-control/test/d1-fake.ts new file mode 100644 index 00000000000..906896cc54f --- /dev/null +++ b/cloud/apps/sandbox-control/test/d1-fake.ts @@ -0,0 +1,64 @@ +// A D1Database over node:sqlite for tests: same SQL dialect (SQLite), same prepare/bind/first/ +// all/run/batch surface, batch() in one transaction like D1. +import { readFileSync, readdirSync } from 'node:fs' +import { DatabaseSync } from 'node:sqlite' +import { fileURLToPath } from 'node:url' + +type Param = string | number | null + +class FakeStatement { + constructor( + private readonly db: DatabaseSync, + readonly sql: string, + readonly params: Param[] = [] + ) {} + + bind(...params: unknown[]): FakeStatement { + for (const param of params) { + if (param === undefined || typeof param === 'boolean' || typeof param === 'object' && param !== null) { + throw new Error(`D1 cannot bind ${typeof param}`) + } + } + return new FakeStatement(this.db, this.sql, params as Param[]) + } + + async first(): Promise { + return (this.db.prepare(this.sql).get(...this.params) as T | undefined) ?? null + } + + async all(): Promise<{ results: T[]; success: true; meta: { changes: number } }> { + return { results: this.db.prepare(this.sql).all(...this.params) as T[], success: true, meta: { changes: 0 } } + } + + runSync(): { success: true; meta: { changes: number } } { + const result = this.db.prepare(this.sql).run(...this.params) + return { success: true, meta: { changes: Number(result.changes) } } + } + + async run() { + return this.runSync() + } +} + +export function createFakeD1(): { db: D1Database; raw: DatabaseSync } { + const raw = new DatabaseSync(':memory:') + const dir = fileURLToPath(new URL('../migrations/', import.meta.url).href) + for (const file of readdirSync(dir).filter((name) => name.endsWith('.sql')).sort()) { + raw.exec(readFileSync(`${dir}${file}`, 'utf8')) + } + const db = { + prepare: (sql: string) => new FakeStatement(raw, sql), + async batch(statements: FakeStatement[]) { + raw.exec('BEGIN') + try { + const results = statements.map((statement) => statement.runSync()) + raw.exec('COMMIT') + return results + } catch (error) { + raw.exec('ROLLBACK') + throw error + } + } + } + return { db: db as unknown as D1Database, raw } +} diff --git a/cloud/apps/sandbox-control/test/egress-proxy.test.ts b/cloud/apps/sandbox-control/test/egress-proxy.test.ts new file mode 100644 index 00000000000..2b5d06c18b3 --- /dev/null +++ b/cloud/apps/sandbox-control/test/egress-proxy.test.ts @@ -0,0 +1,81 @@ +import { SignJWT, createLocalJWKSet, exportJWK, generateKeyPair } from 'jose' +import { describe, expect, it } from 'vitest' +import { createHarness } from './harness' + +const PROXY_URL = 'https://sandbox.test/v1/egress/proxy' +const ISSUER = 'https://oidc.vercel.com/team_orca' + +async function setup() { + const { privateKey, publicKey } = await generateKeyPair('RS256') + const jwks = { keys: [{ ...(await exportJWK(publicKey)), kid: 'v1', alg: 'RS256' }] } + const upstream: { url: string; authorization: string | null }[] = [] + const h = await createHarness( + { EGRESS_PROXY_URL: PROXY_URL }, + { + egressKeysForIssuer: () => createLocalJWKSet(jwks), + credentials: { authorizationFor: async (row, host) => (host === 'api.github.com' ? `Bearer ghs_for_${row.workspace_id}` : null) }, + egressFetch: async (input, init) => { + upstream.push({ url: String(input), authorization: new Headers(init?.headers).get('authorization') }) + return new Response('{"login":"orca"}', { status: 200, headers: { 'content-type': 'application/json' } }) + } + } + ) + const created = await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' } }) + const sandboxId: string = created.body.sandbox.id + async function oidc(claims: Record, audience = PROXY_URL) { + return new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_id: 'sbx-v', sandbox_name: `orca-test-${sandboxId}`, ...claims }) + .setProtectedHeader({ alg: 'RS256', kid: 'v1' }) + .setIssuer(ISSUER) + .setAudience(audience) + .setIssuedAt() + .setExpirationTime('5m') + .sign(privateKey) + } + async function proxied(init: { token: string; authorization?: string; host?: string }) { + const response = await h.send( + new Request(`${PROXY_URL}/user`, { + headers: { + 'vercel-forwarded-host': init.host ?? 'api.github.com', + 'vercel-forwarded-scheme': 'https', + 'vercel-forwarded-port': '443', + 'vercel-forwarded-path': '/user', + 'vercel-sandbox-oidc-token': init.token, + ...(init.authorization ? { authorization: init.authorization } : {}) + } + }) + ) + return { status: response.status, body: await response.text() } + } + return { h, sandboxId, oidc, proxied, upstream } +} + +describe('egress proxy (Vercel forwardURL target)', () => { + it('swaps the placeholder for the workspace credential on a verified sandbox request', async () => { + const { sandboxId, oidc, proxied, upstream } = await setup() + const res = await proxied({ token: await oidc({}), authorization: `Bearer orca-ph-${sandboxId}` }) + expect(res).toEqual({ status: 200, body: '{"login":"orca"}' }) + expect(upstream).toEqual([{ url: 'https://api.github.com/user', authorization: 'Bearer ghs_for_ws-1' }]) + }) + + it('refuses requests without the placeholder, from other teams, or for unproxied hosts', async () => { + const { sandboxId, oidc, proxied, upstream } = await setup() + const placeholder = `Bearer orca-ph-${sandboxId}` + expect(await proxied({ token: await oidc({}), authorization: 'Bearer attacker' })).toEqual({ status: 403, body: 'placeholder_missing' }) + expect(await proxied({ token: await oidc({ team_id: 'team_other' }), authorization: placeholder })).toEqual({ status: 403, body: 'foreign_sandbox' }) + expect(await proxied({ token: await oidc({}, 'https://elsewhere.test'), authorization: placeholder })).toEqual({ status: 403, body: 'bad_oidc_token' }) + expect(await proxied({ token: await oidc({}), authorization: placeholder, host: 'example.com' })).toEqual({ status: 403, body: 'host_not_proxied' }) + expect(upstream).toEqual([]) + }) + + it('forwards the placeholder route for api.github.com in the sandbox network policy', async () => { + const { h, sandboxId } = await setup() + const [box] = [...h.vercel.boxes.values()] + expect(box!.policy.allow['api.github.com']).toEqual([ + { + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: `Bearer orca-ph-${sandboxId}` } }] }, + forwardURL: PROXY_URL + }, + { response: { statusCode: 403, body: 'orca-egress-denied', contentType: 'text/plain' } } + ]) + }) +}) diff --git a/cloud/apps/sandbox-control/test/fake-vercel.ts b/cloud/apps/sandbox-control/test/fake-vercel.ts new file mode 100644 index 00000000000..bca4b79500a --- /dev/null +++ b/cloud/apps/sandbox-control/test/fake-vercel.ts @@ -0,0 +1,161 @@ +// In-memory Vercel Sandbox with the behaviours the bake-off measured: stop = reboot from an automatic +// snapshot, resume keeps the name and route, and delete leaves automatic snapshots behind. +import type { NetworkPolicy } from '../src/network-policy' +import { + ProviderError, + type CreateSandboxParams, + type ProviderSandboxState, + type ProviderSnapshot, + type RunCommandParams, + type SandboxProviderApi +} from '../src/sandbox-provider' +import { DRAIN_COMMAND, START_COMMAND } from '../src/sandbox-vm-commands' + +type FakeBox = { + params: CreateSandboxParams + status: 'running' | 'stopped' + sessionId: string + startedAt: number + timeoutMs: number + route: string + policy: NetworkPolicy +} + +export type FakeVercel = SandboxProviderApi & { + boxes: Map + snapshots: Map + commands: { sessionId: string; command: string; env: Record }[] + failStart: boolean + failCreate: boolean + /** Simulates the provider ending a session at its timeout. */ + expire(name: string): void +} + +export function createFakeVercel(now: () => number): FakeVercel { + const boxes = new Map() + const snapshots: FakeVercel['snapshots'] = new Map() + const commands: FakeVercel['commands'] = [] + let counter = 0 + const next = (prefix: string) => `${prefix}_${++counter}` + const state = (box: FakeBox): ProviderSandboxState => ({ + sandbox: { + name: box.params.name, + status: box.status, + currentSessionId: box.sessionId, + currentSnapshotId: null, + tags: box.params.tags + }, + session: { id: box.sessionId, status: box.status, startedAt: box.startedAt, timeoutMs: box.timeoutMs }, + routes: [{ port: 6768, url: box.route }] + }) + const bySession = (sessionId: string): FakeBox => { + const box = [...boxes.values()].find((candidate) => candidate.sessionId === sessionId) + if (!box) { + throw new ProviderError(404, 'not_found') + } + return box + } + const stop = (box: FakeBox): string => { + box.status = 'stopped' + const id = next('snap') + snapshots.set(id, { id, status: 'created', sourceSessionId: box.sessionId, sandboxName: box.params.name }) + return id + } + + const fake: FakeVercel = { + boxes, + snapshots, + commands, + failStart: false, + failCreate: false, + expire(name) { + const box = boxes.get(name) + if (box?.status === 'running') { + stop(box) + } + }, + async createSandbox(params) { + if (fake.failCreate) { + throw new ProviderError(500, 'internal_error') + } + if (boxes.has(params.name)) { + throw new ProviderError(409, 'conflict') + } + const box: FakeBox = { + params, + status: 'running', + sessionId: next('sess'), + startedAt: now(), + timeoutMs: params.timeoutMs, + route: `https://sb-${counter}.vercel.run`, + policy: params.networkPolicy + } + boxes.set(params.name, box) + return state(box) + }, + async getSandbox(name, options) { + const box = boxes.get(name) + if (!box) { + return null + } + if (box.status === 'stopped' && options.resume) { + Object.assign(box, { + status: 'running', + sessionId: next('sess'), + startedAt: now(), + timeoutMs: box.params.timeoutMs + }) + } + return state(box) + }, + async stopSession(sessionId) { + const box = bySession(sessionId) + return { snapshotId: box.status === 'running' ? stop(box) : null } + }, + async extendTimeout(sessionId, durationMs) { + const box = bySession(sessionId) + box.timeoutMs += durationMs + return state(box).session + }, + async updateNetworkPolicy(sessionId, policy) { + bySession(sessionId).policy = policy + }, + async runCommand(sessionId, params: RunCommandParams) { + const box = bySession(sessionId) + if (box.status !== 'running') { + throw new ProviderError(410, 'sandbox_stopped') + } + commands.push({ sessionId, command: params.command, env: params.env ?? {} }) + if (params.command === START_COMMAND) { + if (fake.failStart) { + return { exitCode: 1, stdout: '', stderr: 'orcad crashed' } + } + const ready = { pairingCode: `orca://pair?code=${box.params.name}`, projectRoot: '/vercel/sandbox/project' } + return { exitCode: 0, stdout: `booting\n${JSON.stringify(ready)}\n`, stderr: '' } + } + if (params.command === DRAIN_COMMAND) { + return { exitCode: 0, stdout: '', stderr: '' } + } + return { exitCode: 127, stdout: '', stderr: 'not found' } + }, + async deleteSandbox(name) { + const box = boxes.get(name) + if (box?.status === 'running') { + stop(box) + } + boxes.delete(name) + }, + async listSnapshots(name) { + return [...snapshots.values()] + .filter((snapshot) => snapshot.sandboxName === name) + .map(({ id, status, sourceSessionId }) => ({ id, status, sourceSessionId })) + }, + async deleteSnapshot(snapshotId) { + const snapshot = snapshots.get(snapshotId) + if (snapshot) { + snapshot.status = 'deleted' + } + } + } + return fake +} diff --git a/cloud/apps/sandbox-control/test/harness.ts b/cloud/apps/sandbox-control/test/harness.ts new file mode 100644 index 00000000000..d27e7e5ee8c --- /dev/null +++ b/cloud/apps/sandbox-control/test/harness.ts @@ -0,0 +1,98 @@ +import { SignJWT, exportJWK, generateKeyPair } from 'jose' +import type { EntitlementClient, StartDecision, UsageAnswer, UsageSegment } from '../src/entitlements' +import type { Env } from '../src/env' +import { handleRequest, type Deps } from '../src/index' +import { reconcile } from '../src/reconcile' +import { createFakeD1 } from './d1-fake' +import { createFakeVercel } from './fake-vercel' + +export const GOLDEN = 'snap_golden' + +export async function createHarness(overrides: Partial = {}, extraDeps: Partial = {}) { + const { privateKey, publicKey } = await generateKeyPair('ES256') + const jwk = { ...(await exportJWK(publicKey)), kid: 'k1', alg: 'ES256', use: 'sig' } + const { db, raw } = createFakeD1() + let now = 1_800_000_000_000 + const clock = () => now + const vercel = createFakeVercel(clock) + const billing = { + decision: { allow: true, reason: null } as StartDecision, + usageAnswer: { canStartSandbox: true, reason: null } as UsageAnswer, + down: false, + checks: [] as { accountId: string; runningSandboxes: number }[], + usage: [] as UsageSegment[] + } + const entitlements: EntitlementClient = { + async checkStart(request) { + billing.checks.push({ accountId: request.accountId, runningSandboxes: request.runningSandboxes }) + return billing.decision + }, + async reportUsage(segment) { + if (billing.down) { + throw new Error('billing down') + } + billing.usage.push(segment) + return billing.usageAnswer + } + } + const env: Env = { + SANDBOX_DB: db, + SANDBOX_ENV: 'test', + SANDBOX_JWKS: JSON.stringify({ keys: [jwk] }), + SANDBOX_JWT_AUDIENCE: 'orca-sandbox', + SANDBOX_AUTH_ISSUER: 'https://login.onorca.dev', + GOLDEN_SNAPSHOT_ID: GOLDEN, + VERCEL_TEAM_ID: 'team_orca', + VERCEL_PROJECT_ID: 'prj_orca', + ...overrides + } + const deps: Deps = { provider: vercel, entitlements, now: clock, ...extraDeps } + + async function token(sub: string, org?: string, purpose = 'sandbox') { + const iat = Math.floor(now / 1000) + return new SignJWT({ sub, ...(org ? { org } : {}), purpose }) + .setProtectedHeader({ alg: 'ES256', kid: 'k1' }) + .setIssuer('https://login.onorca.dev') + .setAudience('orca-sandbox') + .setIssuedAt(iat) + .setExpirationTime(iat + 300) + .sign(privateKey) + } + + async function send(request: Request): Promise { + return handleRequest(request, env, deps) + } + + async function call(method: string, path: string, init: { body?: unknown; as?: string; bearer?: string } = {}) { + const bearer = init.bearer ?? (await token(init.as ?? 'alice')) + const request = new Request(`https://sandbox.test${path}`, { + method, + headers: { authorization: `Bearer ${bearer}`, 'content-type': 'application/json' }, + body: init.body === undefined ? undefined : JSON.stringify(init.body) + }) + const response = await handleRequest(request, env, deps) + return { status: response.status, body: (await response.json()) as any } + } + + return { + env, + raw, + vercel, + billing, + call, + send, + token, + advance(ms: number) { + now += ms + }, + now: clock, + reconcile: () => + reconcile({ + db, + provider: vercel, + entitlements, + now: clock, + config: { environment: 'test', goldenSnapshotId: GOLDEN, defaultRegion: 'iad1' } + }) + } +} diff --git a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts new file mode 100644 index 00000000000..bcbf176d895 --- /dev/null +++ b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts @@ -0,0 +1,190 @@ +import { describe, expect, it } from 'vitest' +import { + HostedSandboxListResponseSchema, + HostedSandboxReadyResponseSchema, + HostedSandboxStatusResponseSchema +} from '../../../../src/shared/hosted-sandbox/hosted-sandbox-wire' +import { DRAIN_COMMAND, START_COMMAND } from '../src/sandbox-vm-commands' +import { GOLDEN, createHarness } from './harness' + +const MIN = 60_000 +const create = { workspaceId: 'ws-1', machineSize: 'standard' } + +describe('sandbox control plane lifecycle', () => { + it('creates a white-labelled sandbox from the golden image and returns orcad pairing', async () => { + const h = await createHarness() + const res = await h.call('POST', '/v1/sandboxes', { body: create }) + expect(res.status).toBe(200) + const ready = HostedSandboxReadyResponseSchema.parse(res.body) + expect(ready.sandbox.status).toBe('running') + expect(ready.connection.projectRoot).toBe('/vercel/sandbox/project') + expect(ready.sandbox.session!.deadlineAt - ready.sandbox.session!.startedAt).toBe(23 * 60 * MIN + 50 * MIN) + expect(ready.sandbox.session!.leaseExpiresAt - ready.sandbox.session!.startedAt).toBe(30 * MIN) + + const [box] = [...h.vercel.boxes.values()] + expect(box!.params).toMatchObject({ + name: `orca-test-${ready.sandbox.id}`, + snapshotId: GOLDEN, + vcpus: 4, + ports: [6768], + tags: { orcaSandbox: ready.sandbox.id, orcaEnv: 'test' } + }) + expect(Object.keys(box!.policy.allow)).not.toContain('*') + const start = h.vercel.commands.find((c) => c.command === START_COMMAND) + expect(start!.env.ORCA_PUBLIC_WSS).toBe(box!.route.replace('https:', 'wss:')) + expect(h.billing.checks).toEqual([{ accountId: 'user:alice', runningSandboxes: 0 }]) + }) + + it('refuses a start billing denies without touching the provider', async () => { + const h = await createHarness() + h.billing.decision = { allow: false, reason: 'concurrency_limit' } + const res = await h.call('POST', '/v1/sandboxes', { body: create }) + expect(res).toEqual({ status: 403, body: { error: 'entitlement_denied', reason: 'concurrency_limit' } }) + expect(h.vercel.boxes.size).toBe(0) + expect(h.raw.prepare('SELECT COUNT(*) AS n FROM sandboxes').get()).toEqual({ n: 0 }) + }) + + it('hides one account’s sandbox from another', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + expect((await h.call('GET', `/v1/sandboxes/${body.sandbox.id}`, { as: 'bob' })).status).toBe(404) + expect((await h.call('DELETE', `/v1/sandboxes/${body.sandbox.id}`, { as: 'bob' })).status).toBe(404) + expect(HostedSandboxListResponseSchema.parse((await h.call('GET', '/v1/sandboxes', { as: 'bob' })).body).sandboxes).toEqual([]) + expect(h.vercel.boxes.size).toBe(1) + }) + + it('returns the same environment when a create is retried for the workspace', async () => { + const h = await createHarness() + const first = await h.call('POST', '/v1/sandboxes', { body: create }) + const second = await h.call('POST', '/v1/sandboxes', { body: create }) + expect(second.body.sandbox.id).toBe(first.body.sandbox.id) + expect(h.vercel.boxes.size).toBe(1) + }) + + it('drains before stopping, meters the session, and resumes the same disk', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + const id = body.sandbox.id + h.advance(20 * MIN) + const suspended = await h.call('POST', `/v1/sandboxes/${id}/suspend`) + expect(HostedSandboxStatusResponseSchema.parse(suspended.body).sandbox).toMatchObject({ status: 'suspended', session: null }) + expect(h.vercel.commands.map((c) => c.command)).toEqual([START_COMMAND, DRAIN_COMMAND]) + expect(h.billing.usage).toEqual([ + expect.objectContaining({ sandboxId: id, accountId: 'user:alice', machineSize: 'standard', endedAt: h.now(), startedAt: h.now() - 20 * MIN }) + ]) + + const resumed = HostedSandboxReadyResponseSchema.parse((await h.call('POST', `/v1/sandboxes/${id}/resume`)).body) + expect(resumed.sandbox.status).toBe('running') + expect(resumed.sandbox.session!.id).not.toBe(body.sandbox.session.id) + expect(h.vercel.boxes.size).toBe(1) + expect(h.billing.checks).toHaveLength(2) + }) + + it('renews the Orca lease but never past the forced-rollover deadline', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + const startedAt = body.sandbox.session.startedAt + const renewed = await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/lease`, { body: { extendMs: 60 * MIN } }) + expect(renewed.body.sandbox.session.leaseExpiresAt).toBe(h.now() + 60 * MIN) + h.advance(23 * 60 * MIN + 30 * MIN) + const clamped = await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/lease`, { body: { extendMs: 60 * MIN } }) + expect(clamped.body.sandbox.session.leaseExpiresAt).toBe(startedAt + 23 * 60 * MIN + 50 * MIN) + }) + + it('rolls a session over onto a fresh one from the same disk', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + h.advance(12 * 60 * MIN) + const rolled = HostedSandboxReadyResponseSchema.parse((await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/rollover`)).body) + expect(rolled.sandbox.id).toBe(body.sandbox.id) + expect(rolled.sandbox.session!.startedAt).toBe(h.now()) + expect(h.vercel.commands.map((c) => c.command)).toEqual([START_COMMAND, DRAIN_COMMAND, START_COMMAND]) + expect(h.billing.usage.map((u) => u.endedAt - u.startedAt)).toEqual([12 * 60 * MIN]) + }) + + it('destroys the sandbox and every snapshot it left behind, idempotently', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/suspend`) + await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/resume`) + expect([...h.vercel.snapshots.values()].filter((s) => s.status === 'created')).toHaveLength(1) + + const destroyed = await h.call('DELETE', `/v1/sandboxes/${body.sandbox.id}`) + expect(destroyed.body.sandbox.status).toBe('destroyed') + expect(h.vercel.boxes.size).toBe(0) + expect([...h.vercel.snapshots.values()].every((s) => s.status === 'deleted')).toBe(true) + expect((await h.call('DELETE', `/v1/sandboxes/${body.sandbox.id}`)).body.sandbox.status).toBe('destroyed') + expect((await h.call('GET', '/v1/sandboxes')).body.sandboxes).toEqual([]) + }) + + it('cleans up a create whose orcad never came up, so the workspace can retry', async () => { + const h = await createHarness() + h.vercel.failStart = true + const res = await h.call('POST', '/v1/sandboxes', { body: create }) + expect(res).toEqual({ status: 502, body: { error: 'provider_unavailable', reason: 'orcad_start_failed' } }) + expect(h.vercel.boxes.size).toBe(0) + h.vercel.failStart = false + expect((await h.call('POST', '/v1/sandboxes', { body: create })).status).toBe(200) + }) + + it('refuses lifecycle calls until Orca’s provider account is configured', async () => { + const h = await createHarness({ GOLDEN_SNAPSHOT_ID: undefined }) + expect(await h.call('POST', '/v1/sandboxes', { body: create })).toEqual({ status: 503, body: { error: 'not_configured' } }) + }) + + it('rejects tokens minted for another purpose', async () => { + const h = await createHarness() + const res = await h.call('GET', '/v1/sandboxes', { bearer: await h.token('alice', undefined, 'billing') }) + expect(res.status).toBe(401) + }) +}) + +describe('sandbox control plane cron', () => { + it('stops a sandbox at the forced-rollover deadline when no desktop rolled it over', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/lease`, { body: { extendMs: 60 * MIN } }) + h.advance(23 * 60 * MIN + 50 * MIN) + expect((await h.reconcile()).suspendedForDeadline).toBe(1) + expect((await h.call('GET', `/v1/sandboxes/${body.sandbox.id}`)).body.sandbox.status).toBe('suspended') + }) + + it('bills a lapsed lease only up to when the provider stopped it', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + h.advance(45 * MIN) + h.vercel.expire(`orca-test-${body.sandbox.id}`) + expect((await h.reconcile()).leaseLapsed).toBe(1) + expect(h.billing.usage.map((u) => u.endedAt - u.startedAt)).toEqual([30 * MIN]) + }) + + it('stops running sandboxes once billing says the included hours are gone', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + h.billing.usageAnswer = { canStartSandbox: false, reason: 'included_hours_exhausted' } + h.advance(10 * MIN) + expect((await h.reconcile()).suspendedForBilling).toBe(1) + expect((await h.call('GET', `/v1/sandboxes/${body.sandbox.id}`)).body.sandbox.status).toBe('suspended') + }) + + it('keeps usage owed while billing is down and reports it once billing is back', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + h.billing.down = true + h.advance(5 * MIN) + expect((await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/suspend`)).status).toBe(200) + expect(h.billing.usage).toEqual([]) + h.billing.down = false + await h.reconcile() + expect(h.billing.usage.map((u) => u.endedAt - u.startedAt)).toEqual([5 * MIN]) + }) + + it('releases a sandbox whose create died mid-way', async () => { + const h = await createHarness() + const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) + h.raw.prepare(`UPDATE sandboxes SET status = 'creating' WHERE id = ?`).run(body.sandbox.id) + h.advance(16 * MIN) + expect((await h.reconcile()).released).toBe(1) + expect(h.vercel.boxes.size).toBe(0) + }) +}) diff --git a/cloud/apps/sandbox-control/test/network-policy.test.ts b/cloud/apps/sandbox-control/test/network-policy.test.ts new file mode 100644 index 00000000000..5545a374a87 --- /dev/null +++ b/cloud/apps/sandbox-control/test/network-policy.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it } from 'vitest' +import { compileNetworkPolicy, gitBasicAuthorization } from '../src/network-policy' + +const deny = { response: { statusCode: 403, body: 'orca-egress-denied', contentType: 'text/plain' } } + +describe('compileNetworkPolicy', () => { + it('fails closed: no wildcard, and credential hosts end with a deny rule', () => { + const policy = compileNetworkPolicy({ + mode: 'allowlist', + allowlist: ['registry.npmjs.org', 'github.com'], + credentials: [ + { + host: 'github.com', + header: 'Authorization', + placeholder: gitBasicAuthorization('orca-ph-1'), + via: 'transform', + value: gitBasicAuthorization('ghs_real') + }, + { + host: 'api.github.com', + header: 'authorization', + placeholder: 'Bearer orca-ph-1', + via: 'forward', + forwardURL: 'https://sandbox.onorca.dev/v1/egress/proxy' + } + ] + }) + expect(policy).toEqual({ + allow: { + 'registry.npmjs.org': [], + 'github.com': [ + { + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: 'Basic eC1hY2Nlc3MtdG9rZW46b3JjYS1waC0x' } }] }, + transform: [{ headers: { authorization: 'Basic eC1hY2Nlc3MtdG9rZW46Z2hzX3JlYWw=' } }] + }, + deny + ], + 'api.github.com': [ + { + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: 'Bearer orca-ph-1' } }] }, + forwardURL: 'https://sandbox.onorca.dev/v1/egress/proxy' + }, + deny + ] + } + }) + }) + + it('adds the wildcard only when a workspace opts into open egress', () => { + expect(compileNetworkPolicy({ mode: 'open', allowlist: [] })).toEqual({ allow: { '*': [] } }) + }) + + it('rejects malformed hosts and wildcard credential hosts', () => { + expect(() => compileNetworkPolicy({ mode: 'allowlist', allowlist: ['https://x.com'] })).toThrow() + expect(() => + compileNetworkPolicy({ + mode: 'allowlist', + allowlist: [], + credentials: [{ host: '*.github.com', header: 'authorization', placeholder: 'p', via: 'transform', value: 'v' }] + }) + ).toThrow(/exact host/) + }) +}) diff --git a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts new file mode 100644 index 00000000000..d6d7a094b53 --- /dev/null +++ b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts @@ -0,0 +1,95 @@ +import { describe, expect, it } from 'vitest' +import { ProviderError } from '../src/sandbox-provider' +import { createVercelSandboxApi } from '../src/vercel-sandbox-api' + +type Seen = { method: string; url: URL; headers: Headers; body: any } + +function api(answer: (seen: Seen) => Response) { + const seen: Seen[] = [] + const client = createVercelSandboxApi({ + token: 'vercel_test_token', + teamId: 'team_orca', + projectId: 'prj_orca', + fetch: async (input, init) => { + const entry = { + method: init?.method ?? 'GET', + url: new URL(String(input)), + headers: new Headers(init?.headers), + body: typeof init?.body === 'string' ? JSON.parse(init.body) : undefined + } + seen.push(entry) + return answer(entry) + } + }) + return { client, seen } +} + +const sandboxBody = { + sandbox: { name: 'orca-prod-sbx_1', status: 'running', currentSessionId: 'sess_1', persistent: true, tags: { orcaSandbox: 'sbx_1' } }, + session: { id: 'sess_1', status: 'running', startedAt: 1000, timeout: 1_800_000, memory: 8192, vcpus: 4 }, + routes: [{ port: 6768, url: 'https://sb-1.vercel.run', subdomain: 'sb-1' }] +} + +describe('Vercel Sandbox REST client', () => { + it('creates a persistent sandbox from the golden snapshot on the v3 endpoint', async () => { + const { client, seen } = api(() => Response.json(sandboxBody)) + const state = await client.createSandbox({ + name: 'orca-prod-sbx_1', + snapshotId: 'snap_golden', + vcpus: 4, + region: 'iad1', + timeoutMs: 1_800_000, + ports: [6768], + networkPolicy: { allow: { 'registry.npmjs.org': [] } }, + tags: { orcaSandbox: 'sbx_1' } + }) + expect(seen[0]!.method).toBe('POST') + expect(`${seen[0]!.url.origin}${seen[0]!.url.pathname}${seen[0]!.url.search}`).toBe( + 'https://vercel.com/api/v3/sandboxes?teamId=team_orca' + ) + expect(seen[0]!.headers.get('authorization')).toBe('Bearer vercel_test_token') + expect(seen[0]!.body).toMatchObject({ + projectId: 'prj_orca', + source: { type: 'snapshot', snapshotId: 'snap_golden' }, + resources: { vcpus: 4 }, + persistent: true, + keepLastSnapshots: { count: 2 }, + snapshotExpiration: 0 + }) + expect(state.routes).toEqual([{ port: 6768, url: 'https://sb-1.vercel.run' }]) + expect(state.session.timeoutMs).toBe(1_800_000) + }) + + it('reads the command ndjson stream to the finished exit code', async () => { + const lines = [ + { command: { id: 'cmd_1', exitCode: null } }, + { stream: 'stdout', data: 'boot\n' }, + { stream: 'stderr', data: 'warn\n' }, + { stream: 'stdout', data: '{"ok":1}\n' }, + { command: { id: 'cmd_1', exitCode: 0 } } + ] + const { client, seen } = api(() => new Response(lines.map((l) => JSON.stringify(l)).join('\n'), { headers: { 'content-type': 'application/x-ndjson' } })) + const result = await client.runCommand('sess_1', { command: '/opt/orca/bin/orca-sandbox-start', timeoutMs: 1000 }) + expect(result).toEqual({ exitCode: 0, stdout: 'boot\n{"ok":1}\n', stderr: 'warn\n' }) + expect(seen[0]!.url.pathname).toBe('/api/v2/sandboxes/sessions/sess_1/cmd') + expect(seen[0]!.body).toMatchObject({ wait: true, logs: true }) + }) + + it('treats a missing sandbox as absent and deletes with orphan snapshots', async () => { + const { client, seen } = api((s) => + s.method === 'GET' ? Response.json({ error: { code: 'not_found' } }, { status: 404 }) : Response.json({ error: { code: 'not_found' } }, { status: 404 }) + ) + expect(await client.getSandbox('orca-prod-sbx_9', { resume: true })).toBeNull() + await client.deleteSandbox('orca-prod-sbx_9') + expect(seen[0]!.url.searchParams.get('resume')).toBe('true') + expect(seen[1]!.method).toBe('DELETE') + expect(seen[1]!.url.searchParams.get('deleteOrphanSnapshots')).toBe('true') + }) + + it('surfaces provider failures as codes without their bodies', async () => { + const { client } = api(() => Response.json({ error: { code: 'rate_limited', message: 'secret detail' } }, { status: 429 })) + const error = await client.stopSession('sess_1').catch((caught: unknown) => caught) + expect(error).toBeInstanceOf(ProviderError) + expect((error as ProviderError).message).toBe('provider request failed (429 rate_limited)') + }) +}) diff --git a/cloud/apps/sandbox-control/tsconfig.json b/cloud/apps/sandbox-control/tsconfig.json new file mode 100644 index 00000000000..043cf347090 --- /dev/null +++ b/cloud/apps/sandbox-control/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es2022", + "module": "es2022", + "moduleResolution": "bundler", + "lib": ["es2022"], + "types": ["@cloudflare/workers-types", "@types/node"], + "strict": true, + "noUncheckedIndexedAccess": true, + "noEmit": true, + "skipLibCheck": true + }, + "include": ["src", "test", "image"] +} diff --git a/cloud/apps/sandbox-control/vitest.config.ts b/cloud/apps/sandbox-control/vitest.config.ts new file mode 100644 index 00000000000..64447de6145 --- /dev/null +++ b/cloud/apps/sandbox-control/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + // A string tsconfig skips the lookup, which otherwise climbs to the desktop app's tsconfig. + oxc: { tsconfig: false }, + test: { include: ['test/**/*.test.ts', 'image/**/*.test.ts'] } +}) diff --git a/cloud/apps/sandbox-control/wrangler.jsonc b/cloud/apps/sandbox-control/wrangler.jsonc new file mode 100644 index 00000000000..677c810b07c --- /dev/null +++ b/cloud/apps/sandbox-control/wrangler.jsonc @@ -0,0 +1,28 @@ +{ + "$schema": "node_modules/wrangler/config-schema.json", + // Local `wrangler dev` only. No routes and workers.dev is off, so a deploy from this file + // publishes nothing reachable. Deployed environments live in the private deploy config. + // Secrets (stdin only, never here): VERCEL_TOKEN (Orca's team, Sandbox scope) and + // CONTROL_PLANE_TOKEN (shared with the billing Worker). Local dev: an untracked .dev.vars. + "name": "orca-sandbox-control-local", + "main": "src/index.ts", + "compatibility_date": "2026-11-01", + "workers_dev": false, + "d1_databases": [ + { + "binding": "SANDBOX_DB", + "database_name": "orca-sandbox-control-local", + "database_id": "00000000-0000-0000-0000-000000000000", + "migrations_dir": "migrations" + } + ], + // Usage settlement, lease/deadline enforcement and stuck-row reconciliation. + "triggers": { "crons": ["*/5 * * * *"] }, + "vars": { + "SANDBOX_ENV": "local", + "SANDBOX_JWT_AUDIENCE": "orca-sandbox", + "VERCEL_REGION": "iad1", + // Local dev never calls billing; deployed configs set BILLING_URL and drop this. + "ENTITLEMENTS_MODE": "stub" + } +} diff --git a/cloud/pnpm-lock.yaml b/cloud/pnpm-lock.yaml index 4faad933bb0..5e22a6427ca 100644 --- a/cloud/pnpm-lock.yaml +++ b/cloud/pnpm-lock.yaml @@ -348,6 +348,34 @@ importers: specifier: ^5.0.5 version: 5.0.5(@types/node@24.19.2)(vite@8.3.4(@types/node@24.19.2)(esbuild@0.28.2)(tsx@4.23.15)) + apps/sandbox-control: + dependencies: + jose: + specifier: ^6.2.13 + version: 6.2.13 + zod: + specifier: ^4.6.7 + version: 4.6.7 + devDependencies: + '@cloudflare/workers-types': + specifier: ^5.20261103.1 + version: 5.20261104.1 + '@types/node': + specifier: ^24.19.2 + version: 24.19.2 + tsx: + specifier: ^4.23.15 + version: 4.23.15 + typescript: + specifier: ^5.9.3 + version: 5.9.3 + vitest: + specifier: ^5.0.5 + version: 5.0.5(@types/node@24.19.2)(vite@8.3.4(@types/node@24.19.2)(esbuild@0.28.2)(tsx@4.23.15)) + wrangler: + specifier: ^4.157.0 + version: 4.157.0(@cloudflare/workers-types@5.20261104.1)(@types/node@24.19.2) + packages/cloud-database: dependencies: '@orca-cloud/postgres-schema': diff --git a/config/scripts/ci/unit-selection/check-test-file-runners.mjs b/config/scripts/ci/unit-selection/check-test-file-runners.mjs index 09f620398b7..778d6bd6804 100644 --- a/config/scripts/ci/unit-selection/check-test-file-runners.mjs +++ b/config/scripts/ci/unit-selection/check-test-file-runners.mjs @@ -55,6 +55,15 @@ const NON_UNIT_TEST_RUNNERS = [ sources: ['cloud/apps/relay-edge/vitest.config.ts'], mirrors: ["include: ['test/**/*.test.ts']"] }, + { + name: 'cloud sandbox-control', + include: [ + 'cloud/apps/sandbox-control/test/**/*.test.ts', + 'cloud/apps/sandbox-control/image/**/*.test.ts' + ], + sources: ['cloud/apps/sandbox-control/vitest.config.ts'], + mirrors: ["include: ['test/**/*.test.ts', 'image/**/*.test.ts']"] + }, ...[ 'cloud/apps/relay-fence-broker', 'cloud/packages/cloud-database', From 2a19f0191b9f9b062bb2e9381f94bc73bb3b9f21 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:28:31 -0800 Subject: [PATCH 04/18] feat(cloud): golden image build (orcad + agent CLIs, identity wipe, kept snapshot) (#12) --- .../image/build-golden-image.ts | 59 +++++++++ .../image/golden-image.manifest.json | 15 +++ .../image/golden-image.test.ts | 93 ++++++++++++++ .../sandbox-control/image/golden-image.ts | 118 ++++++++++++++++++ .../sandbox-control/image/provision-golden.sh | 87 +++++++++++++ .../image/vm/orca-egress-probe | 16 +++ .../sandbox-control/image/vm/orca-image-wipe | 20 +++ .../image/vm/orca-sandbox-drain | 21 ++++ .../image/vm/orca-sandbox-start | 62 +++++++++ cloud/apps/sandbox-control/package.json | 1 + .../apps/sandbox-control/src/egress-proxy.ts | 4 +- .../src/golden-image-provider.ts | 17 +++ cloud/apps/sandbox-control/src/index.ts | 4 +- cloud/apps/sandbox-control/src/lifecycle.ts | 15 ++- .../sandbox-control/src/network-policy.ts | 18 +-- .../src/sandbox-vm-commands.ts | 4 +- .../sandbox-control/src/vercel-sandbox-api.ts | 36 +++++- cloud/apps/sandbox-control/src/wire.ts | 6 +- .../sandbox-control/test/egress-proxy.test.ts | 11 +- .../apps/sandbox-control/test/fake-vercel.ts | 2 +- .../test/network-policy.test.ts | 10 +- 21 files changed, 593 insertions(+), 26 deletions(-) create mode 100644 cloud/apps/sandbox-control/image/build-golden-image.ts create mode 100644 cloud/apps/sandbox-control/image/golden-image.manifest.json create mode 100644 cloud/apps/sandbox-control/image/golden-image.test.ts create mode 100644 cloud/apps/sandbox-control/image/golden-image.ts create mode 100755 cloud/apps/sandbox-control/image/provision-golden.sh create mode 100755 cloud/apps/sandbox-control/image/vm/orca-egress-probe create mode 100755 cloud/apps/sandbox-control/image/vm/orca-image-wipe create mode 100755 cloud/apps/sandbox-control/image/vm/orca-sandbox-drain create mode 100755 cloud/apps/sandbox-control/image/vm/orca-sandbox-start create mode 100644 cloud/apps/sandbox-control/src/golden-image-provider.ts diff --git a/cloud/apps/sandbox-control/image/build-golden-image.ts b/cloud/apps/sandbox-control/image/build-golden-image.ts new file mode 100644 index 00000000000..9851eb7c496 --- /dev/null +++ b/cloud/apps/sandbox-control/image/build-golden-image.ts @@ -0,0 +1,59 @@ +// Operator CLI. Dry run by default (prints the plan, touches nothing). With --execute it builds in +// Orca's Vercel team and prints {imageVersion, snapshotId, report}; set GOLDEN_SNAPSHOT_ID from it. +// pnpm --filter @orca-cloud/sandbox-control exec tsx image/build-golden-image.ts [--execute] +// --execute needs VERCEL_TOKEN, VERCEL_TEAM_ID, VERCEL_PROJECT_ID in the environment (never in git). +import { randomBytes } from 'node:crypto' +import { readFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' +import { createVercelSandboxApi } from '../src/vercel-sandbox-api' +import { ManifestSchema, VM_SCRIPTS, buildGoldenImage, manifestGaps, provisionEnv, type BuildInputs } from './golden-image' + +const here = (path: string): string => fileURLToPath(new URL(path, import.meta.url).href) + +function loadInputs(): BuildInputs { + const manifest = ManifestSchema.parse(JSON.parse(readFileSync(here('./golden-image.manifest.json'), 'utf8'))) + const vmScripts = Object.fromEntries( + VM_SCRIPTS.map((name) => [name, readFileSync(here(`./vm/${name}`), 'utf8')]) + ) as BuildInputs['vmScripts'] + return { + manifest, + provisionScript: readFileSync(here('./provision-golden.sh'), 'utf8'), + vmScripts, + buildId: randomBytes(4).toString('hex') + } +} + +async function main(): Promise { + const inputs = loadInputs() + const execute = process.argv.includes('--execute') + const gaps = manifestGaps(inputs.manifest) + if (!execute) { + const env = provisionEnv(inputs) + console.log( + JSON.stringify( + { + dryRun: true, + manifest: inputs.manifest, + provisionEnvKeys: Object.keys(env), + steps: ['createBuildSandbox', 'provision-golden.sh', 'orca-image-wipe', 'createSnapshot(expiration=0)', 'deleteBuildSandbox'], + gaps + }, + null, + 2 + ) + ) + return + } + const { VERCEL_TOKEN, VERCEL_TEAM_ID, VERCEL_PROJECT_ID } = process.env + if (!VERCEL_TOKEN || !VERCEL_TEAM_ID || !VERCEL_PROJECT_ID) { + throw new Error('--execute needs VERCEL_TOKEN, VERCEL_TEAM_ID and VERCEL_PROJECT_ID') + } + const provider = createVercelSandboxApi({ token: VERCEL_TOKEN, teamId: VERCEL_TEAM_ID, projectId: VERCEL_PROJECT_ID }) + const result = await buildGoldenImage(provider, inputs, (line) => console.error(`[golden] ${line}`)) + console.log(JSON.stringify(result)) +} + +main().catch((error: unknown) => { + console.error(error instanceof Error ? error.message : String(error)) + process.exitCode = 1 +}) diff --git a/cloud/apps/sandbox-control/image/golden-image.manifest.json b/cloud/apps/sandbox-control/image/golden-image.manifest.json new file mode 100644 index 00000000000..94d061fdc39 --- /dev/null +++ b/cloud/apps/sandbox-control/image/golden-image.manifest.json @@ -0,0 +1,15 @@ +{ + "imageVersion": "2026-11-08.1", + "runtime": "node24", + "region": "iad1", + "vcpus": 4, + "orcad": { + "bundleUrl": null, + "sha256": null + }, + "gh": { + "version": null, + "sha256": null + }, + "agentCliPackages": ["@anthropic-ai/claude-code@2.1.319", "@openai/codex@0.166.0"] +} diff --git a/cloud/apps/sandbox-control/image/golden-image.test.ts b/cloud/apps/sandbox-control/image/golden-image.test.ts new file mode 100644 index 00000000000..c523ce93526 --- /dev/null +++ b/cloud/apps/sandbox-control/image/golden-image.test.ts @@ -0,0 +1,93 @@ +import { spawnSync } from 'node:child_process' +import { fileURLToPath } from 'node:url' +import { describe, expect, it } from 'vitest' +import { VM_SCRIPTS, buildGoldenImage, type BuildInputs } from './golden-image' + +const manifest: BuildInputs['manifest'] = { + imageVersion: '2026-11-08.1', + runtime: 'node24', + region: 'iad1', + vcpus: 4, + orcad: { bundleUrl: 'https://artifacts.onorca.dev/orcad-linux-x64.tgz', sha256: 'a'.repeat(64) }, + gh: { version: null, sha256: null }, + agentCliPackages: ['@anthropic-ai/claude-code@2.1.319', '@openai/codex@0.166.0'] +} + +function inputs(overrides: Partial = {}): BuildInputs { + return { + manifest, + provisionScript: 'echo provision', + vmScripts: Object.fromEntries(VM_SCRIPTS.map((name) => [name, `#!/bin/sh\n# ${name}\n`])) as BuildInputs['vmScripts'], + buildId: 'b1', + ...overrides + } +} + +function fakeProvider(answers: { provisionExit?: number; wipeStdout?: string } = {}) { + const calls: string[] = [] + const provider = { + async createBuildSandbox(params: { name: string; runtime: string }) { + calls.push(`create ${params.name} ${params.runtime}`) + return { + sandbox: { name: params.name, status: 'running' as const, currentSessionId: 's1', currentSnapshotId: null, tags: {} }, + session: { id: 's1', status: 'running' as const, startedAt: 0, timeoutMs: 0 }, + routes: [] + } + }, + async runCommand(_session: string, params: { command: string; env?: Record }) { + calls.push(`run ${params.command}`) + if (params.command === 'bash') { + expect(params.env?.ORCAD_BUNDLE_SHA256).toBe('a'.repeat(64)) + expect(Buffer.from(params.env!.ORCA_VM_SCRIPT_orca_sandbox_start!, 'base64').toString()).toContain('orca-sandbox-start') + return { exitCode: answers.provisionExit ?? 0, stdout: '{"node":"v24.11.0","tools":{}}\n', stderr: 'boom' } + } + return { exitCode: 0, stdout: answers.wipeStdout ?? '{"wiped":true}\n', stderr: '' } + }, + async createSnapshot(_session: string, expirationMs: number) { + calls.push(`snapshot expiration=${expirationMs}`) + return 'snap_golden_1' + }, + async deleteBuildSandbox(name: string) { + calls.push(`delete ${name}`) + } + } + return { provider, calls } +} + +describe('golden image build', () => { + it('wipes identity before snapshotting, keeps the snapshot and deletes the build sandbox', async () => { + const { provider, calls } = fakeProvider() + const result = await buildGoldenImage(provider, inputs()) + expect(result).toEqual({ imageVersion: '2026-11-08.1', snapshotId: 'snap_golden_1', report: { node: 'v24.11.0', tools: {} } }) + expect(calls).toEqual([ + 'create orca-golden-2026-11-08-1-b1 node24', + 'run bash', + 'run /opt/orca/bin/orca-image-wipe', + 'snapshot expiration=0', + 'delete orca-golden-2026-11-08-1-b1' + ]) + }) + + it('never snapshots a failed provision or an unconfirmed wipe, and still removes the build sandbox', async () => { + for (const answers of [{ provisionExit: 1 }, { wipeStdout: '' }]) { + const { provider, calls } = fakeProvider(answers) + await expect(buildGoldenImage(provider, inputs())).rejects.toThrow() + expect(calls.some((call) => call.startsWith('snapshot'))).toBe(false) + expect(calls.at(-1)).toBe('delete orca-golden-2026-11-08-1-b1') + } + }) + + it('refuses to build without a pinned orcad bundle', async () => { + const { provider, calls } = fakeProvider() + const unpinned = inputs({ manifest: { ...manifest, orcad: { bundleUrl: null, sha256: null } } }) + await expect(buildGoldenImage(provider, unpinned)).rejects.toThrow(/orcad.bundleUrl/) + expect(calls).toEqual([]) + }) + + it('ships VM scripts that parse', () => { + for (const script of ['provision-golden.sh', ...VM_SCRIPTS.map((name) => `vm/${name}`)]) { + const path = fileURLToPath(new URL(`./${script}`, import.meta.url).href) + expect(spawnSync('bash', ['-n', path]).status, script).toBe(0) + } + }) +}) diff --git a/cloud/apps/sandbox-control/image/golden-image.ts b/cloud/apps/sandbox-control/image/golden-image.ts new file mode 100644 index 00000000000..b0f87d4a4d4 --- /dev/null +++ b/cloud/apps/sandbox-control/image/golden-image.ts @@ -0,0 +1,118 @@ +// Golden image build: one throwaway build sandbox → provision → wipe identity → snapshot (kept until +// deleted) → delete the build sandbox. Every Orca Cloud sandbox boots from the resulting snapshot. +import { z } from 'zod' +import type { GoldenImageProviderApi } from '../src/golden-image-provider' +import type { SandboxProviderApi } from '../src/sandbox-provider' + +export const VM_SCRIPTS = ['orca-sandbox-start', 'orca-sandbox-drain', 'orca-egress-probe', 'orca-image-wipe'] as const + +const Sha256 = z.string().regex(/^[a-f0-9]{64}$/) +const Package = z.string().regex(/^(@[a-z0-9-]+\/)?[a-z0-9.-]+@\d+\.\d+\.\d+[A-Za-z0-9.-]*$/) + +export const ManifestSchema = z + .object({ + imageVersion: z.string().regex(/^[0-9]{4}-[0-9]{2}-[0-9]{2}\.[0-9]+$/), + runtime: z.string().min(1), + region: z.string().regex(/^[a-z]{3}[0-9]$/), + vcpus: z.number().int().min(2).max(8), + orcad: z.object({ bundleUrl: z.url({ protocol: /^https$/ }).nullable(), sha256: Sha256.nullable() }).strict(), + gh: z.object({ version: z.string().regex(/^\d+\.\d+\.\d+$/).nullable(), sha256: Sha256.nullable() }).strict(), + agentCliPackages: z.array(Package).min(1) + }) + .strict() +export type GoldenImageManifest = z.infer + +/** Execution needs every pin; a dry run may show a manifest that is still being filled in. */ +export function manifestGaps(manifest: GoldenImageManifest): string[] { + const gaps: string[] = [] + if (!manifest.orcad.bundleUrl || !manifest.orcad.sha256) { + gaps.push('orcad.bundleUrl and orcad.sha256 (linux-x64 bundle from `pnpm build:orcad`)') + } + if (Boolean(manifest.gh.version) !== Boolean(manifest.gh.sha256)) { + gaps.push('gh.version and gh.sha256 must be set together') + } + return gaps +} + +export type BuildInputs = { + manifest: GoldenImageManifest + provisionScript: string + vmScripts: Record<(typeof VM_SCRIPTS)[number], string> + buildId: string +} + +const BUILD_TIMEOUT_MS = 40 * 60_000 +const PROVISION_TIMEOUT_MS = 30 * 60_000 + +export function provisionEnv(inputs: BuildInputs): Record { + const { manifest } = inputs + const env: Record = { + ORCAD_BUNDLE_URL: manifest.orcad.bundleUrl ?? '', + ORCAD_BUNDLE_SHA256: manifest.orcad.sha256 ?? '', + AGENT_NPM_PACKAGES: manifest.agentCliPackages.join(' ') + } + if (manifest.gh.version && manifest.gh.sha256) { + env.GH_VERSION = manifest.gh.version + env.GH_SHA256 = manifest.gh.sha256 + } + for (const name of VM_SCRIPTS) { + env[`ORCA_VM_SCRIPT_${name.replaceAll('-', '_')}`] = Buffer.from(inputs.vmScripts[name]).toString('base64') + } + return env +} + +export type BuildResult = { + imageVersion: string + snapshotId: string + report: unknown +} + +type Provider = Pick & GoldenImageProviderApi + +export async function buildGoldenImage( + provider: Provider, + inputs: BuildInputs, + log: (line: string) => void = () => {} +): Promise { + const gaps = manifestGaps(inputs.manifest) + if (gaps.length > 0) { + throw new Error(`manifest incomplete: ${gaps.join('; ')}`) + } + const name = `orca-golden-${inputs.manifest.imageVersion.replace('.', '-')}-${inputs.buildId}` + log(`creating build sandbox ${name}`) + const state = await provider.createBuildSandbox({ + name, + runtime: inputs.manifest.runtime, + vcpus: inputs.manifest.vcpus, + region: inputs.manifest.region, + timeoutMs: BUILD_TIMEOUT_MS, + tags: { orcaGoldenImage: inputs.manifest.imageVersion } + }) + try { + log('provisioning') + const provision = await provider.runCommand(state.session.id, { + command: 'bash', + args: ['-c', inputs.provisionScript], + env: provisionEnv(inputs), + timeoutMs: PROVISION_TIMEOUT_MS + }) + if (provision.exitCode !== 0) { + throw new Error(`provision failed (exit ${provision.exitCode}): ${provision.stderr.slice(-2000)}`) + } + const report: unknown = JSON.parse(provision.stdout.trim().split('\n').at(-1) ?? 'null') + log('wiping identity and auth homes') + const wipe = await provider.runCommand(state.session.id, { + command: '/opt/orca/bin/orca-image-wipe', + timeoutMs: 60_000 + }) + if (wipe.exitCode !== 0 || !wipe.stdout.includes('"wiped":true')) { + throw new Error(`identity wipe failed: ${wipe.stderr.slice(-500)}`) + } + log('snapshotting') + const snapshotId = await provider.createSnapshot(state.session.id, 0) + return { imageVersion: inputs.manifest.imageVersion, snapshotId, report } + } finally { + log(`deleting build sandbox ${name}`) + await provider.deleteBuildSandbox(name) + } +} diff --git a/cloud/apps/sandbox-control/image/provision-golden.sh b/cloud/apps/sandbox-control/image/provision-golden.sh new file mode 100755 index 00000000000..c5cfa6e6a26 --- /dev/null +++ b/cloud/apps/sandbox-control/image/provision-golden.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# Runs once inside the build sandbox (as the sandbox user, with sudo). Installs the pinned orcad +# bundle, agent CLIs and Orca's VM entry points into /opt/orca, then smoke-tests orcad. +# Inputs (env, from image/golden-image.manifest.json via build-golden-image.ts): +# ORCAD_BUNDLE_URL ORCAD_BUNDLE_SHA256 [GH_VERSION GH_SHA256] AGENT_NPM_PACKAGES (space-separated +# name@version) and ORCA_VM_SCRIPT_ (base64) for each /opt/orca/bin entry point. +set -euo pipefail +ORCA_HOME=/opt/orca +log() { printf '[golden] %s\n' "$*" >&2; } + +sudo mkdir -p "$ORCA_HOME/bin" "$ORCA_HOME/runtime" "$ORCA_HOME/tools" +sudo chown -R "$(id -u):$(id -g)" "$ORCA_HOME" + +log 'system packages' +if command -v apt-get >/dev/null; then + sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \ + ca-certificates curl git jq ripgrep build-essential python3 unzip util-linux procps >/dev/null +elif command -v dnf >/dev/null; then + sudo dnf install -y -q ca-certificates curl-minimal git jq gcc gcc-c++ make python3 unzip util-linux procps-ng >/dev/null +else + log 'no supported package manager'; exit 1 +fi + +fetch_verified() { # url sha256 dest + curl -fsSL --retry 3 -o "$3" "$1" + echo "$2 $3" | sha256sum -c --quiet - +} + +log 'orcad bundle' +fetch_verified "$ORCAD_BUNDLE_URL" "$ORCAD_BUNDLE_SHA256" /tmp/orcad.tgz +tar -xzf /tmp/orcad.tgz -C "$ORCA_HOME/runtime" +rm -f /tmp/orcad.tgz +test -s "$ORCA_HOME/runtime/orcad/orcad-server.js" +node="$(find "$ORCA_HOME/runtime/runtimes" -type f -name node | head -n 1)" +test -x "$node" + +if [ -n "${GH_VERSION:-}" ]; then + log "gh $GH_VERSION" + fetch_verified "https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_amd64.tar.gz" \ + "$GH_SHA256" /tmp/gh.tgz + tar -xzf /tmp/gh.tgz -C /tmp + mkdir -p "$ORCA_HOME/tools/bin" + install -m 0755 "/tmp/gh_${GH_VERSION}_linux_amd64/bin/gh" "$ORCA_HOME/tools/bin/gh" + rm -rf /tmp/gh.tgz "/tmp/gh_${GH_VERSION}_linux_amd64" +fi + +log 'agent CLIs' +# The image's system npm installs them; agents later run on whatever node orcad puts on PATH. +# shellcheck disable=SC2086 # word splitting is the package list +npm install --global --no-fund --no-audit --prefix "$ORCA_HOME/tools" $AGENT_NPM_PACKAGES + +log 'Orca entry points' +for name in orca-sandbox-start orca-sandbox-drain orca-egress-probe orca-image-wipe; do + var="ORCA_VM_SCRIPT_${name//-/_}" + printf '%s' "${!var}" | base64 -d >"$ORCA_HOME/bin/$name" + chmod 0755 "$ORCA_HOME/bin/$name" +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' +smoke_data="$(mktemp -d)" +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 +for _ in $(seq 1 600); do + if grep -q '"orca_server_ready"' /tmp/orcad-smoke.out 2>/dev/null; then ready=1; break; fi + sleep 0.1 +done +kill "$smoke_pid" 2>/dev/null || true +wait "$smoke_pid" 2>/dev/null || true +rm -rf "$smoke_data" /tmp/orcad-smoke.out +if [ "$ready" != 1 ]; then + tail -n 40 /tmp/orcad-smoke.err >&2; exit 1 +fi + +log 'versions' +versions="{}" +for bin in claude codex opencode gemini gh; do + if [ -x "$ORCA_HOME/tools/bin/$bin" ]; then + v="$("$ORCA_HOME/tools/bin/$bin" --version 2>/dev/null | head -n 1 || echo unknown)" + versions="$(jq -c --arg k "$bin" --arg v "$v" '. + {($k): $v}' <<<"$versions")" + fi +done +jq -c --arg node "$("$node" --version)" '{node: $node, tools: .}' <<<"$versions" diff --git a/cloud/apps/sandbox-control/image/vm/orca-egress-probe b/cloud/apps/sandbox-control/image/vm/orca-egress-probe new file mode 100755 index 00000000000..74737ad16cf --- /dev/null +++ b/cloud/apps/sandbox-control/image/vm/orca-egress-probe @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Bounded Node fetch against each credential host. Vercel transform rules have made undici hang on +# response bodies (no content-length / transfer-encoding); report it instead of letting agents hang. +# Prints one JSON line: {"host":"api.github.com","status":"ok"|"egress_framing_hang"|"error"}. +set -euo pipefail +node="$(find /opt/orca/runtime/runtimes -type f -name node | head -n 1)" +for host in "$@"; do + "$node" -e ' + const host = process.argv[1] + const timer = setTimeout(() => { console.log(JSON.stringify({ host, status: "egress_framing_hang" })); process.exit(0) }, 8000) + fetch(`https://${host}/`, { headers: { authorization: process.env.ORCA_PROBE_AUTHORIZATION ?? "" } }) + .then((r) => r.arrayBuffer()) + .then(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: "ok" })) }) + .catch(() => { clearTimeout(timer); console.log(JSON.stringify({ host, status: "error" })) }) + ' "$host" +done diff --git a/cloud/apps/sandbox-control/image/vm/orca-image-wipe b/cloud/apps/sandbox-control/image/vm/orca-image-wipe new file mode 100755 index 00000000000..c0c634d5654 --- /dev/null +++ b/cloud/apps/sandbox-control/image/vm/orca-image-wipe @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# Runs last before the golden snapshot. Every sandbox cloned from the image must start without an +# orcad identity, device registry, agent sign-in or shell history (clone-safe identity, risk #1). +set -euo pipefail +pkill -f orcad-server.js 2>/dev/null || true +for path in \ + "$HOME/.orcad-data" "$HOME/.orca-sandbox" "$HOME/.config/orca" "$HOME/.config/orca-dev" \ + "$HOME/.claude" "$HOME/.claude.json" "$HOME/.codex" "$HOME/.config/gh" "$HOME/.gemini" \ + "$HOME/.local/share/opencode" "$HOME/.npm" "$HOME/.cache" "$HOME/.bash_history" \ + /vercel/sandbox/project; do + rm -rf -- "$path" +done +sudo rm -rf /tmp/* /var/tmp/* 2>/dev/null || true +for leftover in "$HOME/.orcad-data" "$HOME/.claude" "$HOME/.codex" "$HOME/.config/gh"; do + if [ -e "$leftover" ]; then + echo "identity survived wipe: $leftover" >&2 + exit 1 + fi +done +echo '{"wiped":true}' diff --git a/cloud/apps/sandbox-control/image/vm/orca-sandbox-drain b/cloud/apps/sandbox-control/image/vm/orca-sandbox-drain new file mode 100755 index 00000000000..8fbdbe82508 --- /dev/null +++ b/cloud/apps/sandbox-control/image/vm/orca-sandbox-drain @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +# Asks orcad to quit before the VM stops: its shutdown turns working agent chats into durable +# restart offers and flushes terminal restore hints, which the desktop continues after resume. +set -euo pipefail +STATE="$HOME/.orca-sandbox" +WAIT_SECONDS="${1:-280}" +pid="$(cat "$STATE/orcad.pid" 2>/dev/null || true)" +if [ -z "$pid" ] || ! kill -0 "$pid" 2>/dev/null; then + exit 0 +fi +kill -TERM "$pid" +for _ in $(seq 1 $((WAIT_SECONDS * 10))); do + if ! kill -0 "$pid" 2>/dev/null; then + rm -f "$STATE/orcad.pid" "$STATE/ready.json" + exit 0 + fi + sleep 0.1 +done +# Leave it running: the provider stop ends it, and the drain already had its window. +echo 'orcad still draining at deadline' >&2 +exit 0 diff --git a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start new file mode 100755 index 00000000000..af3fbf961b8 --- /dev/null +++ b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start @@ -0,0 +1,62 @@ +#!/usr/bin/env bash +# Starts orcad for this boot (idempotent) and prints its recipe line: +# {"schemaVersion":1,"pairingCode":"orca://pair?...","projectRoot":"/vercel/sandbox/project"} +# Env from the control plane: ORCA_SANDBOX_ID, ORCA_PUBLIC_WSS, optional ORCA_REPO_URL/ORCA_REPO_REF, +# ORCA_GITHUB_PLACEHOLDER. Never prints anything else on stdout's last line. +set -euo pipefail +umask 077 +ORCA_HOME=/opt/orca +STATE="$HOME/.orca-sandbox" +PROJECT=/vercel/sandbox/project +mkdir -p "$STATE" +exec 9>"$STATE/start.lock" +flock -w 60 9 + +boot_id="$(cat /proc/sys/kernel/random/boot_id)" +if [ "$(cat "$STATE/boot-id" 2>/dev/null || true)" = "$boot_id" ] && [ -s "$STATE/ready.json" ] \ + && kill -0 "$(cat "$STATE/orcad.pid" 2>/dev/null || echo 0)" 2>/dev/null; then + tail -n 1 "$STATE/ready.json" + exit 0 +fi +# Resume = reboot from disk: locks and pids from the previous session name dead processes. +rm -f "$STATE/ready.json" "$STATE/orcad.pid" +printf '%s' "$boot_id" >"$STATE/boot-id" + +if [ ! -d "$PROJECT" ]; then + if [ -n "${ORCA_REPO_URL:-}" ]; then + git clone --quiet ${ORCA_REPO_REF:+--branch "$ORCA_REPO_REF"} -- "$ORCA_REPO_URL" "$PROJECT" + else + mkdir -p "$PROJECT" + fi +fi + +agent_env=() +if [ -n "${ORCA_GITHUB_PLACEHOLDER:-}" ]; then + # The egress layer swaps the placeholder for a short-lived GitHub token; none lives in the VM. + agent_env+=("GH_TOKEN=$ORCA_GITHUB_PLACEHOLDER" "GITHUB_TOKEN=$ORCA_GITHUB_PLACEHOLDER") +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[@]}" \ + 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" \ + >"$STATE/ready.json" 2>"$STATE/orcad.log" "$STATE/orcad.pid" + +for _ in $(seq 1 1200); do + if grep -q '"pairingCode"' "$STATE/ready.json" 2>/dev/null; then + tail -n 1 "$STATE/ready.json" + exit 0 + fi + if ! kill -0 "$(cat "$STATE/orcad.pid")" 2>/dev/null; then + echo 'orcad exited before ready' >&2 + tail -n 40 "$STATE/orcad.log" | sed -E 's#orca://[^[:space:]]+#[REDACTED_PAIRING]#g' >&2 + exit 1 + fi + sleep 0.05 +done +echo 'orcad start timed out' >&2 +exit 1 diff --git a/cloud/apps/sandbox-control/package.json b/cloud/apps/sandbox-control/package.json index 1da9a6dd95b..bf63884437f 100644 --- a/cloud/apps/sandbox-control/package.json +++ b/cloud/apps/sandbox-control/package.json @@ -6,6 +6,7 @@ "scripts": { "dev": "wrangler dev --port 8791 --ip 127.0.0.1", "db:migrate:local": "wrangler d1 migrations apply SANDBOX_DB --local", + "golden-image": "tsx image/build-golden-image.ts", "typecheck": "tsc --noEmit", "test": "vitest run" }, diff --git a/cloud/apps/sandbox-control/src/egress-proxy.ts b/cloud/apps/sandbox-control/src/egress-proxy.ts index d5169f0bfdc..273ac04f750 100644 --- a/cloud/apps/sandbox-control/src/egress-proxy.ts +++ b/cloud/apps/sandbox-control/src/egress-proxy.ts @@ -34,7 +34,7 @@ export type EgressProxyDeps = { teamId: string projectId: string findSandbox: (providerName: string) => Promise - placeholderFor: (row: SandboxRow) => string + placeholdersFor: (row: SandboxRow) => readonly string[] credentials: CredentialBroker fetch?: typeof fetch /** Tests inject a local key set; production fetches `/.well-known/jwks`. */ @@ -103,7 +103,7 @@ export async function handleEgressProxy(request: Request, deps: EgressProxyDeps) if (!row || row.status !== 'running') { return deny('sandbox_not_running') } - if (request.headers.get('authorization') !== deps.placeholderFor(row)) { + if (!deps.placeholdersFor(row).includes(request.headers.get('authorization') ?? '')) { return deny('placeholder_missing') } const authorization = await deps.credentials.authorizationFor(row, host) diff --git a/cloud/apps/sandbox-control/src/golden-image-provider.ts b/cloud/apps/sandbox-control/src/golden-image-provider.ts new file mode 100644 index 00000000000..12a6d0286c8 --- /dev/null +++ b/cloud/apps/sandbox-control/src/golden-image-provider.ts @@ -0,0 +1,17 @@ +// Provider calls only the operator-run golden image build needs (image/build-golden-image.ts). +import type { ProviderSandboxState } from './sandbox-provider' + +export type GoldenImageProviderApi = { + createBuildSandbox(params: { + name: string + runtime: string + vcpus: number + region: string + timeoutMs: number + tags: Record + }): Promise + /** Stops the session; `expirationMs: 0` keeps the snapshot until deleted. */ + createSnapshot(sessionId: string, expirationMs: number): Promise + /** Deletes the sandbox but keeps snapshots it produced. */ + deleteBuildSandbox(name: string): Promise +} diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 0420b2fcd69..9506b2a6981 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -15,7 +15,7 @@ import { LifecycleError, createSandbox, destroySandbox, - githubPlaceholderHeader, + githubPlaceholderHeaders, renewLease, resumeSandbox, rolloverSandbox, @@ -93,7 +93,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) teamId: env.VERCEL_TEAM_ID, projectId: env.VERCEL_PROJECT_ID, findSandbox: (name) => getSandboxByProviderName(env.SANDBOX_DB, name), - placeholderFor: githubPlaceholderHeader, + placeholdersFor: githubPlaceholderHeaders, credentials: deps.credentials ?? NO_CREDENTIALS, fetch: deps.egressFetch, keysForIssuer: deps.egressKeysForIssuer diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 1ff07ff58a9..c360f450e05 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -55,9 +55,14 @@ function randomId(): string { return `sbx_${Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')}` } -/** What the VM sends as its GitHub API credential; the egress proxy swaps it for the real token. */ -export function githubPlaceholderHeader(row: SandboxRow): string { - return `Bearer orca-ph-${row.id}` +export function githubPlaceholderToken(row: SandboxRow): string { + return `orca-ph-${row.id}` +} + +/** Authorization values the VM's GitHub clients send (Octokit/fetch use Bearer, gh uses token). */ +export function githubPlaceholderHeaders(row: SandboxRow): string[] { + const token = githubPlaceholderToken(row) + return [`Bearer ${token}`, `token ${token}`] } function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { @@ -66,7 +71,7 @@ function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { { host: 'api.github.com', header: 'authorization', - placeholder: githubPlaceholderHeader(row), + placeholders: githubPlaceholderHeaders(row), via: 'forward', forwardURL: deps.config.egressProxyUrl } @@ -109,7 +114,7 @@ async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandbox const connection = await startOrcad(deps.provider, state, { sandboxId: row.id, repo, - githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderHeader(row) : undefined + githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderToken(row) : undefined }) const running = await claim(deps, row, { status: 'running', last_error: null, ...sessionFields(state, deps.now()) }) return { row: running, connection } diff --git a/cloud/apps/sandbox-control/src/network-policy.ts b/cloud/apps/sandbox-control/src/network-policy.ts index 1d19a9ae02d..70980538fb3 100644 --- a/cloud/apps/sandbox-control/src/network-policy.ts +++ b/cloud/apps/sandbox-control/src/network-policy.ts @@ -50,9 +50,9 @@ export const DEFAULT_EGRESS_ALLOWLIST: readonly string[] = [ export type CredentialRoute = { host: string - /** Header the VM sends with the placeholder value, matched exactly. */ + /** Header the VM sends with a placeholder value, matched exactly (one claim rule per value). */ header: string - placeholder: string + placeholders: readonly string[] } & ( | { via: 'transform'; value: string } | { via: 'forward'; forwardURL: string } @@ -76,15 +76,15 @@ export function compileNetworkPolicy(input: EgressPolicyInput): NetworkPolicy { if (host.startsWith('*.')) { throw new Error('credential routes need an exact host') } - const match = { - headers: [{ key: { exact: route.header.toLowerCase() }, value: { exact: route.placeholder } }] - } - const claim: NetworkPolicyRule = - route.via === 'transform' - ? { match, transform: [{ headers: { [route.header.toLowerCase()]: route.value } }] } + const header = route.header.toLowerCase() + const claims = route.placeholders.map((placeholder): NetworkPolicyRule => { + const match = { headers: [{ key: { exact: header }, value: { exact: placeholder } }] } + return route.via === 'transform' + ? { match, transform: [{ headers: { [header]: route.value } }] } : { match, forwardURL: route.forwardURL } + }) const rules = allow[host]?.filter((rule) => !('response' in rule)) ?? [] - allow[host] = [...rules, claim, denyRule()] + allow[host] = [...rules, ...claims, denyRule()] } if (input.mode === 'open') { allow['*'] = [] diff --git a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts index fc216504f79..8487b38b690 100644 --- a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts +++ b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts @@ -12,6 +12,8 @@ export const DRAIN_TIMEOUT_MS = 5 * 60_000 const ReadySchema = z .object({ + // `orcad --recipe-json` prints the recipe line schemaVersion 1. + schemaVersion: z.literal(1).optional(), pairingCode: z.string().min(1).max(8192), projectRoot: z.string().startsWith('/').max(4096) }) @@ -29,7 +31,7 @@ export class VmCommandError extends Error { export type StartInputs = { sandboxId: string repo?: { url: string; ref?: string } - /** Value the VM sends for credential hosts; the egress layer swaps it for the real credential. */ + /** Token the VM's GitHub clients send; the egress layer swaps it for a real short-lived one. */ githubPlaceholder?: string } diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts index f99b48673b0..c7c7b181928 100644 --- a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -2,6 +2,7 @@ // calls the same /v2 + /v3 endpoints its api-client uses (SDK 3.9.1), with Orca's team token. import { z } from 'zod' import type { NetworkPolicy } from './network-policy' +import type { GoldenImageProviderApi } from './golden-image-provider' import { ProviderError, type CommandResult, @@ -46,7 +47,9 @@ const CommandLine = z.looseObject({ command: z.looseObject({ exitCode: z.number( const OutputLine = z.looseObject({ stream: z.enum(['stdout', 'stderr']), data: z.string() }) const FailureLine = z.looseObject({ stream: z.literal('error'), data: z.looseObject({ code: z.string() }) }) -export function createVercelSandboxApi(config: VercelSandboxApiConfig): SandboxProviderApi { +export function createVercelSandboxApi( + config: VercelSandboxApiConfig +): SandboxProviderApi & GoldenImageProviderApi { const base = config.baseUrl ?? 'https://vercel.com/api' const doFetch = config.fetch ?? fetch @@ -193,6 +196,37 @@ export function createVercelSandboxApi(config: VercelSandboxApiConfig): SandboxP }) ), + async createBuildSandbox(params) { + // A `runtime` selects /v2: a fresh managed image, not a snapshot. + const response = await request('POST', '/v2/sandboxes', { + body: { + projectId: config.projectId, + name: params.name, + runtime: params.runtime, + resources: { vcpus: params.vcpus }, + region: params.region, + timeout: params.timeoutMs, + persistent: false, + networkPolicy: { mode: 'allow-all' }, + tags: params.tags + } + }) + return toState(await json(SandboxAndSession, response)) + }, + + async createSnapshot(sessionId, expirationMs) { + const response = await request('POST', `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/snapshot`, { + body: { expiration: expirationMs } + }) + return (await json(z.looseObject({ snapshot: Snapshot }), response)).snapshot.id + }, + + // Without deleteOrphanSnapshots: the build sandbox goes, the golden snapshot it produced stays. + deleteBuildSandbox: (name) => + missingOk(() => + request('DELETE', `/v2/sandboxes/${encodeURIComponent(name)}`, { query: { projectId: config.projectId } }) + ), + async listSnapshots(name) { const snapshots: z.infer[] = [] let cursor: string | undefined diff --git a/cloud/apps/sandbox-control/src/wire.ts b/cloud/apps/sandbox-control/src/wire.ts index 2028482af22..aaee92b168f 100644 --- a/cloud/apps/sandbox-control/src/wire.ts +++ b/cloud/apps/sandbox-control/src/wire.ts @@ -65,6 +65,10 @@ export function sandboxView(row: SandboxRow) { export function readyView(row: SandboxRow, connection: OrcadReady) { return { sandbox: sandboxView(row), - connection: { type: 'orca-server' as const, ...connection } + connection: { + type: 'orca-server' as const, + pairingCode: connection.pairingCode, + projectRoot: connection.projectRoot + } } } diff --git a/cloud/apps/sandbox-control/test/egress-proxy.test.ts b/cloud/apps/sandbox-control/test/egress-proxy.test.ts index 2b5d06c18b3..e3d0b08079e 100644 --- a/cloud/apps/sandbox-control/test/egress-proxy.test.ts +++ b/cloud/apps/sandbox-control/test/egress-proxy.test.ts @@ -54,7 +54,12 @@ describe('egress proxy (Vercel forwardURL target)', () => { const { sandboxId, oidc, proxied, upstream } = await setup() const res = await proxied({ token: await oidc({}), authorization: `Bearer orca-ph-${sandboxId}` }) expect(res).toEqual({ status: 200, body: '{"login":"orca"}' }) - expect(upstream).toEqual([{ url: 'https://api.github.com/user', authorization: 'Bearer ghs_for_ws-1' }]) + // gh sends the `token` scheme; it reaches the same credential. + expect((await proxied({ token: await oidc({}), authorization: `token orca-ph-${sandboxId}` })).status).toBe(200) + expect(upstream).toEqual([ + { url: 'https://api.github.com/user', authorization: 'Bearer ghs_for_ws-1' }, + { url: 'https://api.github.com/user', authorization: 'Bearer ghs_for_ws-1' } + ]) }) it('refuses requests without the placeholder, from other teams, or for unproxied hosts', async () => { @@ -75,6 +80,10 @@ describe('egress proxy (Vercel forwardURL target)', () => { match: { headers: [{ key: { exact: 'authorization' }, value: { exact: `Bearer orca-ph-${sandboxId}` } }] }, forwardURL: PROXY_URL }, + { + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: `token orca-ph-${sandboxId}` } }] }, + forwardURL: PROXY_URL + }, { response: { statusCode: 403, body: 'orca-egress-denied', contentType: 'text/plain' } } ]) }) diff --git a/cloud/apps/sandbox-control/test/fake-vercel.ts b/cloud/apps/sandbox-control/test/fake-vercel.ts index bca4b79500a..583b2ffda37 100644 --- a/cloud/apps/sandbox-control/test/fake-vercel.ts +++ b/cloud/apps/sandbox-control/test/fake-vercel.ts @@ -130,7 +130,7 @@ export function createFakeVercel(now: () => number): FakeVercel { if (fake.failStart) { return { exitCode: 1, stdout: '', stderr: 'orcad crashed' } } - const ready = { pairingCode: `orca://pair?code=${box.params.name}`, projectRoot: '/vercel/sandbox/project' } + const ready = { schemaVersion: 1, pairingCode: `orca://pair?code=${box.params.name}`, projectRoot: '/vercel/sandbox/project' } return { exitCode: 0, stdout: `booting\n${JSON.stringify(ready)}\n`, stderr: '' } } if (params.command === DRAIN_COMMAND) { diff --git a/cloud/apps/sandbox-control/test/network-policy.test.ts b/cloud/apps/sandbox-control/test/network-policy.test.ts index 5545a374a87..3f252e6cb1e 100644 --- a/cloud/apps/sandbox-control/test/network-policy.test.ts +++ b/cloud/apps/sandbox-control/test/network-policy.test.ts @@ -12,14 +12,14 @@ describe('compileNetworkPolicy', () => { { host: 'github.com', header: 'Authorization', - placeholder: gitBasicAuthorization('orca-ph-1'), + placeholders: [gitBasicAuthorization('orca-ph-1')], via: 'transform', value: gitBasicAuthorization('ghs_real') }, { host: 'api.github.com', header: 'authorization', - placeholder: 'Bearer orca-ph-1', + placeholders: ['Bearer orca-ph-1', 'token orca-ph-1'], via: 'forward', forwardURL: 'https://sandbox.onorca.dev/v1/egress/proxy' } @@ -40,6 +40,10 @@ describe('compileNetworkPolicy', () => { match: { headers: [{ key: { exact: 'authorization' }, value: { exact: 'Bearer orca-ph-1' } }] }, forwardURL: 'https://sandbox.onorca.dev/v1/egress/proxy' }, + { + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: 'token orca-ph-1' } }] }, + forwardURL: 'https://sandbox.onorca.dev/v1/egress/proxy' + }, deny ] } @@ -56,7 +60,7 @@ describe('compileNetworkPolicy', () => { compileNetworkPolicy({ mode: 'allowlist', allowlist: [], - credentials: [{ host: '*.github.com', header: 'authorization', placeholder: 'p', via: 'transform', value: 'v' }] + credentials: [{ host: '*.github.com', header: 'authorization', placeholders: ['p'], via: 'transform', value: 'v' }] }) ).toThrow(/exact host/) }) From bc01829481f14b02bd939473cca89d06a32432be Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 20:34:11 -0800 Subject: [PATCH 05/18] feat(cloud): 24h rollover, idle stop and agent auto-resume for Orca Cloud sandboxes (#13) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(cloud): hosted session lease scheduler — 24h rollover, idle stop, agent auto-resume * refactor: parse restart offers without casts --- cloud/apps/sandbox-control/src/reconcile.ts | 6 +- .../test/lifecycle-flow.test.ts | 2 + cloud/docs/vercel-driver-design.md | 12 ++ .../hosted-agent-auto-resume.ts | 62 ++++++++ .../hosted-runtime-environment-call.ts | 22 +++ .../hosted-session-lease-policy.ts | 78 +++++++++ .../hosted-session-lease-scheduler.ts | 148 ++++++++++++++++++ .../hosted-session-lease.test.ts | 142 +++++++++++++++++ 8 files changed, 470 insertions(+), 2 deletions(-) create mode 100644 src/main/hosted-sandbox/hosted-agent-auto-resume.ts create mode 100644 src/main/hosted-sandbox/hosted-runtime-environment-call.ts create mode 100644 src/main/hosted-sandbox/hosted-session-lease-policy.ts create mode 100644 src/main/hosted-sandbox/hosted-session-lease-scheduler.ts create mode 100644 src/main/hosted-sandbox/hosted-session-lease.test.ts diff --git a/cloud/apps/sandbox-control/src/reconcile.ts b/cloud/apps/sandbox-control/src/reconcile.ts index a4303f59978..88299ed3a21 100644 --- a/cloud/apps/sandbox-control/src/reconcile.ts +++ b/cloud/apps/sandbox-control/src/reconcile.ts @@ -13,6 +13,8 @@ import { listByStatus, transition, type SandboxRow } from './store' import { flushUsage, queueUsage } from './usage' const STUCK_MS = 15 * 60_000 +/** The desktop rolls over at the deadline; the cron steps in only if it did not (provider cap is +10 min). */ +const FORCED_STOP_GRACE_MS = 5 * 60_000 export type ReconcileSummary = Record< 'settled' | 'suspendedForDeadline' | 'suspendedForBilling' | 'leaseLapsed' | 'released' | 'repaired', @@ -55,8 +57,8 @@ export async function reconcile(deps: LifecycleDeps): Promise async function reconcileRunning(deps: LifecycleDeps, row: SandboxRow, summary: ReconcileSummary): Promise { const now = deps.now() - if (row.session_deadline_at !== null && now >= row.session_deadline_at) { - // The desktop should have rolled over; stop before the provider's hard cap kills it undrained. + if (row.session_deadline_at !== null && now >= row.session_deadline_at + FORCED_STOP_GRACE_MS) { + // No desktop rolled it over; stop before the provider's hard cap kills it undrained. await suspendSandbox(deps, row) summary.suspendedForDeadline++ return diff --git a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts index bcbf176d895..bf7dc94d6aa 100644 --- a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts +++ b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts @@ -145,6 +145,8 @@ describe('sandbox control plane cron', () => { const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) await h.call('POST', `/v1/sandboxes/${body.sandbox.id}/lease`, { body: { extendMs: 60 * MIN } }) h.advance(23 * 60 * MIN + 50 * MIN) + expect((await h.reconcile()).suspendedForDeadline).toBe(0) + h.advance(5 * MIN) expect((await h.reconcile()).suspendedForDeadline).toBe(1) expect((await h.call('GET', `/v1/sandboxes/${body.sandbox.id}`)).body.sandbox.status).toBe('suspended') }) diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index f05078eee4d..36fa3286751 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -150,6 +150,18 @@ Compiled per sandbox by `compileNetworkPolicy()`: - Per-account authorization on every route (row `account_id` must equal the caller's); ids are random. - Strict zod bodies, 16 KiB limit, no redirects on provider calls, provider error bodies dropped. +## Built (mock-tested) and next wiring + +- (b) `src/main/environment-providers/*`, `src/main/hosted-sandbox/*` — driver, client, contract kit, recipe adapter. +- (c) `cloud/apps/sandbox-control` — Worker, D1 schema, Vercel REST client, entitlements/usage, egress proxy, cron. +- (d) `cloud/apps/sandbox-control/image` — golden image builder + VM entry points. +- (e) `hosted-session-lease-policy.ts` / `-scheduler.ts` / `hosted-agent-auto-resume.ts` — lease renew, T−60/T−15 + warnings, idle suspend, opportunistic + forced rollover, then `restartResumable` → `restartContinue` for offers + recorded since the drain (older offers stay with the user). The cron only force-stops 5 min after the deadline. +- Next: a built-in "Orca Cloud" recipe source whose create/resume/suspend/destroy call the driver through the + recipe adapter; start one scheduler per ready hosted runtime (stop on suspend/destroy); an orcad RPC exposing + the idle-probe verdict + last PTY input for `readActivity`; header warning + mobile push for `warn`. + ## What needs live access (not done) Vercel Pro team id + project id + a Sandbox-scoped token (as Worker secrets), wrangler auth for a dev D1, a diff --git a/src/main/hosted-sandbox/hosted-agent-auto-resume.ts b/src/main/hosted-sandbox/hosted-agent-auto-resume.ts new file mode 100644 index 00000000000..2c1a1fd8e47 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-agent-auto-resume.ts @@ -0,0 +1,62 @@ +// After a rollover, carry on the agents the drain interrupted, through the same host restart offers +// the "Resume chats" prompt uses (agentSession.restartResumable → agentSession.restartContinue). +// Only offers recorded by this drain are continued; older offers stay for the user to decide. +import { z } from 'zod' +import { MAX_RESTART_RESUME_SESSIONS } from '../../shared/rpc-contract/structured-agent-session-params' + +export type RuntimeCall = (method: string, params?: Record) => Promise + +export type HostedAgentAutoResumeOutcome = { + offered: number + continued: number + skipped: number + /** True when the host has no restart-offer support; nothing was sent. */ + unsupported: boolean +} + +// The host is the single writer of these shapes; rows this side cannot read are left alone. +const OfferList = z.looseObject({ + sessions: z.array(z.unknown()) +}) +const Offer = z.looseObject({ sessionId: z.string(), recordedAt: z.number() }) +const ContinueAnswer = z.looseObject({ + continued: z.array(z.unknown()).optional(), + skipped: z.array(z.unknown()).optional() +}) + +function hasCode(error: unknown, code: string): boolean { + return typeof error === 'object' && error !== null && 'code' in error && error.code === code +} + +export async function autoResumeAgentsAfterRollover( + call: RuntimeCall, + drainStartedAt: number +): Promise { + let listed: unknown + try { + listed = await call('agentSession.restartResumable') + } catch (error) { + if (hasCode(error, 'method_not_found')) { + return { offered: 0, continued: 0, skipped: 0, unsupported: true } + } + throw error + } + const list = OfferList.safeParse(listed) + if (!list.success) { + throw new Error('agent_session_restart_offer_invalid') + } + const ids = list.data.sessions.flatMap((row) => { + const offer = Offer.safeParse(row) + return offer.success && offer.data.recordedAt >= drainStartedAt ? [offer.data.sessionId] : [] + }) + const outcome = { offered: ids.length, continued: 0, skipped: 0, unsupported: false } + for (let offset = 0; offset < ids.length; offset += MAX_RESTART_RESUME_SESSIONS) { + const answer = await call('agentSession.restartContinue', { + sessionIds: ids.slice(offset, offset + MAX_RESTART_RESUME_SESSIONS) + }) + const counted = ContinueAnswer.safeParse(answer) + outcome.continued += counted.success ? (counted.data.continued?.length ?? 0) : 0 + outcome.skipped += counted.success ? (counted.data.skipped?.length ?? 0) : 0 + } + return outcome +} diff --git a/src/main/hosted-sandbox/hosted-runtime-environment-call.ts b/src/main/hosted-sandbox/hosted-runtime-environment-call.ts new file mode 100644 index 00000000000..b91be290240 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-runtime-environment-call.ts @@ -0,0 +1,22 @@ +import { callRuntimeEnvironment } from '../ipc/runtime-environments/runtime-environment-transport-routing' +import type { RuntimeCall } from './hosted-agent-auto-resume' + +/** Agent resume after a rollover talks to the environment's orcad over its existing pairing. */ +export function createHostedRuntimeEnvironmentCall( + userDataPath: string, + environmentId: string +): RuntimeCall { + return async (method, params = {}) => { + const response = await callRuntimeEnvironment( + userDataPath, + environmentId, + method, + params, + 600_000 + ) + if (response.ok === true) { + return response.result + } + throw Object.assign(new Error(response.error.message), { code: response.error.code }) + } +} diff --git a/src/main/hosted-sandbox/hosted-session-lease-policy.ts b/src/main/hosted-sandbox/hosted-session-lease-policy.ts new file mode 100644 index 00000000000..e99c69de030 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-session-lease-policy.ts @@ -0,0 +1,78 @@ +// What to do next with a session-capped, no-memory-pause environment (Vercel: stop = reboot, 24h cap). +// Idle stop, the opportunistic refresh and the forced cap rollover are one decision so they never race. +import type { + EnvironmentProviderCapabilities, + EnvironmentSessionInfo +} from '../environment-providers/environment-provider-driver' + +export type HostedActivitySample = { + /** orcad's idle probes: clients, terminals, agents, automations. Unverifiable counts as busy. */ + verdict: 'idle' | 'busy' | 'unverifiable' + /** Since when every probe has read idle; null while busy. */ + idleSince: number | null + /** Last PTY input anywhere in the environment; null if none this session. */ + lastPtyInputAt: number | null +} + +export type HostedLeaseWarning = 60 | 15 + +export type HostedLeaseAction = + | { kind: 'none' } + | { kind: 'renew'; extendMs: number } + | { kind: 'warn'; minutesLeft: HostedLeaseWarning } + | { kind: 'idle-suspend' } + | { kind: 'rollover'; reason: 'opportunistic' | 'forced' } + +export const HOSTED_LEASE_POLICY = { + /** Renew when this much lease is left, by this much. */ + renewWhenRemainingMs: 10 * 60_000, + renewByMs: 30 * 60_000, + /** After this session age, roll over quietly the first time nothing is running. */ + opportunisticAfterMs: 12 * 60 * 60_000, + ptyQuietMs: 2 * 60_000, + warnings: [60, 15] as const +} + +export function decideHostedLeaseAction(input: { + now: number + capabilities: EnvironmentProviderCapabilities + session: EnvironmentSessionInfo + activity: HostedActivitySample + warned: ReadonlySet +}): HostedLeaseAction { + const { now, capabilities, session, activity } = input + const idleFor = + activity.verdict === 'idle' && activity.idleSince !== null ? now - activity.idleSince : 0 + const ptyQuiet = + activity.lastPtyInputAt === null || + now - activity.lastPtyInputAt >= HOSTED_LEASE_POLICY.ptyQuietMs + const quiet = activity.verdict === 'idle' && ptyQuiet + + if (capabilities.idleStopMs !== null && quiet && idleFor >= capabilities.idleStopMs) { + return { kind: 'idle-suspend' } + } + const deadlineAt = session.deadlineAt + if (capabilities.maxSessionMs !== null && deadlineAt !== null) { + if (now >= deadlineAt) { + // Nothing running: a plain suspend costs less than a reboot nobody is waiting for. + return quiet ? { kind: 'idle-suspend' } : { kind: 'rollover', reason: 'forced' } + } + const minutesLeft = (deadlineAt - now) / 60_000 + for (const warning of [...HOSTED_LEASE_POLICY.warnings].sort((a, b) => a - b)) { + if (minutesLeft <= warning && !input.warned.has(warning) && !quiet) { + return { kind: 'warn', minutesLeft: warning } + } + } + if (now - session.startedAt >= HOSTED_LEASE_POLICY.opportunisticAfterMs && quiet) { + return { kind: 'rollover', reason: 'opportunistic' } + } + } + if ( + session.leaseExpiresAt !== null && + session.leaseExpiresAt - now <= HOSTED_LEASE_POLICY.renewWhenRemainingMs && + (deadlineAt === null || session.leaseExpiresAt < deadlineAt) + ) { + return { kind: 'renew', extendMs: HOSTED_LEASE_POLICY.renewByMs } + } + return { kind: 'none' } +} diff --git a/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts new file mode 100644 index 00000000000..083e6ca60b6 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts @@ -0,0 +1,148 @@ +// Drives one Orca Cloud environment's session: renews Orca's lease while work runs, warns before the +// cap, suspends when idle, and rolls the session over (drain → stop → boot → re-pair → continue agents). +import type { + EnvironmentHandle, + EnvironmentProviderDriver, + EnvironmentReady, + EnvironmentSessionInfo +} from '../environment-providers/environment-provider-driver' +import type { HostedAgentAutoResumeOutcome } from './hosted-agent-auto-resume' +import { + HOSTED_LEASE_POLICY, + decideHostedLeaseAction, + type HostedActivitySample, + type HostedLeaseAction, + type HostedLeaseWarning +} from './hosted-session-lease-policy' + +export type HostedSessionLeasePorts = { + driver: EnvironmentProviderDriver + handle: EnvironmentHandle + session: EnvironmentSessionInfo + readActivity: () => Promise + warn: (minutesLeft: HostedLeaseWarning) => void + /** Re-pairs with the rebooted orcad; resolves once the runtime answers again. */ + reconnect: (ready: EnvironmentReady) => Promise + resumeAgents: (drainStartedAt: number) => Promise + onSuspended: () => void + log?: (line: string) => void + now?: () => number +} + +const UNVERIFIABLE: HostedActivitySample = { + verdict: 'unverifiable', + idleSince: null, + lastPtyInputAt: null +} + +export class HostedSessionLeaseScheduler { + private session: EnvironmentSessionInfo + private readonly warned = new Set() + private timer: ReturnType | null = null + private running: Promise | null = null + private stopped = false + private readonly now: () => number + private readonly log: (line: string) => void + + constructor(private readonly ports: HostedSessionLeasePorts) { + this.session = ports.session + this.now = ports.now ?? Date.now + this.log = ports.log ?? (() => {}) + } + + start(pollMs = 30_000): void { + this.stopped = false + this.schedule(pollMs) + } + + stop(): void { + this.stopped = true + if (this.timer) { + clearTimeout(this.timer) + this.timer = null + } + } + + /** One decision + action; concurrent calls share the in-flight one. */ + tick(): Promise { + this.running ??= this.runOnce().finally(() => { + this.running = null + }) + return this.running + } + + private async runOnce(): Promise { + const activity = await this.ports.readActivity().catch(() => UNVERIFIABLE) + const action = decideHostedLeaseAction({ + now: this.now(), + capabilities: this.ports.driver.capabilities, + session: this.session, + activity, + warned: this.warned + }) + switch (action.kind) { + case 'renew': { + const renewed = await this.ports.driver.renewLease?.(this.ports.handle, action.extendMs) + if (renewed?.session) { + this.session = renewed.session + } + break + } + case 'warn': + // A later, shorter warning supersedes every longer one still pending. + for (const warning of HOSTED_LEASE_POLICY.warnings) { + if (warning >= action.minutesLeft) { + this.warned.add(warning) + } + } + this.ports.warn(action.minutesLeft) + break + case 'idle-suspend': + await this.ports.driver.suspend(this.ports.handle) + this.stop() + this.ports.onSuspended() + break + case 'rollover': + await this.rollover(action.reason) + break + case 'none': + break + } + return action + } + + private async rollover(reason: 'opportunistic' | 'forced'): Promise { + const { driver, handle } = this.ports + if (!driver.rollover) { + return + } + const drainStartedAt = this.now() + this.log(`[orca-cloud] ${reason} rollover`) + const ready = await driver.rollover(handle) + this.session = ready.session + this.warned.clear() + await this.ports.reconnect(ready) + try { + const outcome = await this.ports.resumeAgents(drainStartedAt) + this.log( + `[orca-cloud] continued ${outcome.continued}/${outcome.offered} agents after rollover` + ) + } catch (error) { + // The offers stay on the host, so the ordinary resume prompt still covers them. + this.log(`[orca-cloud] agent auto-resume failed: ${String(error)}`) + } + } + + private schedule(pollMs: number): void { + if (this.stopped) { + return + } + this.timer = setTimeout(() => { + this.timer = null + void this.tick() + .catch((error: unknown) => this.log(`[orca-cloud] lease tick failed: ${String(error)}`)) + .finally(() => this.schedule(pollMs)) + }, pollMs) + this.timer.unref?.() + } +} diff --git a/src/main/hosted-sandbox/hosted-session-lease.test.ts b/src/main/hosted-sandbox/hosted-session-lease.test.ts new file mode 100644 index 00000000000..e381b24d8b5 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-session-lease.test.ts @@ -0,0 +1,142 @@ +import { describe, expect, it, vi } from 'vitest' +import type { EnvironmentReady } from '../environment-providers/environment-provider-driver' +import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume' +import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' +import { createHostedSandboxDriver, HOSTED_SANDBOX_CAPABILITIES } from './hosted-sandbox-driver' +import { decideHostedLeaseAction, type HostedActivitySample } from './hosted-session-lease-policy' +import { HostedSessionLeaseScheduler } from './hosted-session-lease-scheduler' +import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support' + +const MIN = 60_000 +const HOUR = 60 * MIN +const T0 = 1_800_000_000_000 +const session = { + startedAt: T0, + deadlineAt: T0 + 23 * HOUR + 50 * MIN, + leaseExpiresAt: T0 + 30 * MIN +} +const busy: HostedActivitySample = { verdict: 'busy', idleSince: null, lastPtyInputAt: null } +const idleSince = (at: number): HostedActivitySample => ({ + verdict: 'idle', + idleSince: at, + lastPtyInputAt: null +}) + +function decide(now: number, activity: HostedActivitySample, warned: (60 | 15)[] = []) { + return decideHostedLeaseAction({ + now, + capabilities: HOSTED_SANDBOX_CAPABILITIES, + session, + activity, + warned: new Set(warned) + }) +} + +describe('hosted session lease policy', () => { + it('renews the lease while work runs and leaves it alone with time to spare', () => { + expect(decide(T0 + 5 * MIN, busy)).toEqual({ kind: 'none' }) + expect(decide(T0 + 21 * MIN, busy)).toEqual({ kind: 'renew', extendMs: 30 * MIN }) + }) + + it('suspends after 20 quiet minutes, but not while a PTY was just typed in', () => { + expect(decide(T0 + 25 * MIN, idleSince(T0 + 4 * MIN))).toEqual({ kind: 'idle-suspend' }) + const typing = { ...idleSince(T0 + 4 * MIN), lastPtyInputAt: T0 + 24 * MIN } + expect(decide(T0 + 25 * MIN, typing)).toEqual({ kind: 'renew', extendMs: 30 * MIN }) + }) + + it('rolls over quietly after 12h only when nothing is running', () => { + const at = T0 + 12 * HOUR + MIN + expect(decide(at, idleSince(at - MIN))).toEqual({ kind: 'rollover', reason: 'opportunistic' }) + expect(decide(at, busy).kind).not.toBe('rollover') + }) + + it('warns at one hour and 15 minutes, then forces the rollover at the deadline', () => { + expect(decide(session.deadlineAt - 59 * MIN, busy)).toEqual({ kind: 'warn', minutesLeft: 60 }) + expect(decide(session.deadlineAt - 59 * MIN, busy, [60]).kind).not.toBe('warn') + expect(decide(session.deadlineAt - 14 * MIN, busy, [60])).toEqual({ + kind: 'warn', + minutesLeft: 15 + }) + expect(decide(session.deadlineAt, busy)).toEqual({ kind: 'rollover', reason: 'forced' }) + expect(decide(session.deadlineAt, idleSince(session.deadlineAt - 3 * MIN))).toEqual({ + kind: 'idle-suspend' + }) + }) +}) + +describe('hosted session lease scheduler', () => { + it('rolls a busy session over at the deadline, re-pairs, then continues the drained agents', async () => { + let now = T0 + const plane = createFakeHostedControlPlane({ now: () => now }) + const driver = createHostedSandboxDriver( + createHostedSandboxControlPlaneClient({ + baseUrl: 'https://sandbox.cloud.test', + getAccessToken: async () => plane.token, + fetch: plane.fetch + }) + ) + const created = await driver.create({ workspaceId: 'ws-1' }) + const reconnect = vi.fn(async (_ready: EnvironmentReady) => {}) + const resumeAgents = vi.fn(async () => ({ + offered: 2, + continued: 2, + skipped: 0, + unsupported: false + })) + const scheduler = new HostedSessionLeaseScheduler({ + driver, + handle: created.handle, + session: created.session, + readActivity: async () => busy, + warn: () => {}, + reconnect, + resumeAgents, + onSuspended: () => {}, + now: () => now + }) + now = created.session.deadlineAt! + expect(await scheduler.tick()).toEqual({ kind: 'rollover', reason: 'forced' }) + expect(reconnect).toHaveBeenCalledTimes(1) + expect(reconnect.mock.calls[0]?.[0].session.startedAt).toBe(now) + expect(resumeAgents).toHaveBeenCalledWith(created.session.deadlineAt) + // The new session is far from its cap, so the next tick does nothing drastic. + expect((await scheduler.tick()).kind).not.toBe('rollover') + }) +}) + +describe('agent auto-resume after rollover', () => { + it('continues only the offers this drain recorded, through the host restart-offer RPCs', async () => { + const calls: { method: string; params?: Record }[] = [] + const outcome = await autoResumeAgentsAfterRollover(async (method, params) => { + calls.push({ method, params }) + if (method === 'agentSession.restartResumable') { + return { + sessions: [ + { sessionId: 'old', recordedAt: 900 }, + { sessionId: 'a', recordedAt: 1000 }, + { sessionId: 'b', recordedAt: 1500 } + ] + } + } + return { continued: [{ sessionId: 'a' }], skipped: ['b'] } + }, 1000) + expect(calls).toEqual([ + { method: 'agentSession.restartResumable', params: undefined }, + { method: 'agentSession.restartContinue', params: { sessionIds: ['a', 'b'] } } + ]) + expect(outcome).toEqual({ offered: 2, continued: 1, skipped: 1, unsupported: false }) + }) + + it('sends nothing to a host without restart offers', async () => { + const call = vi.fn(async () => { + throw Object.assign(new Error('nope'), { code: 'method_not_found' }) + }) + expect(await autoResumeAgentsAfterRollover(call, 0)).toEqual({ + offered: 0, + continued: 0, + skipped: 0, + unsupported: true + }) + expect(call).toHaveBeenCalledTimes(1) + }) +}) From e9d778b054dd08e2e6e7bb1b143e85d6193c2ced Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:26:16 -0800 Subject: [PATCH 06/18] fix(cloud): live Vercel Pro findings (bundle download, rehearsal cap) + results (#16) * fix(cloud): provision bundle download no longer overwrites its source; shortened session deadline for staging rehearsals * docs(cloud): live Vercel Pro results --- .../sandbox-control/image/provision-golden.sh | 7 ++++--- cloud/apps/sandbox-control/src/env.ts | 2 ++ cloud/apps/sandbox-control/src/index.ts | 4 +++- cloud/apps/sandbox-control/src/lifecycle.ts | 9 ++++++--- cloud/apps/sandbox-control/src/reconcile.ts | 2 +- cloud/docs/vercel-driver-design.md | 18 ++++++++++++++++++ 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cloud/apps/sandbox-control/image/provision-golden.sh b/cloud/apps/sandbox-control/image/provision-golden.sh index c5cfa6e6a26..9d54d8c9ec7 100755 --- a/cloud/apps/sandbox-control/image/provision-golden.sh +++ b/cloud/apps/sandbox-control/image/provision-golden.sh @@ -28,9 +28,10 @@ fetch_verified() { # url sha256 dest } log 'orcad bundle' -fetch_verified "$ORCAD_BUNDLE_URL" "$ORCAD_BUNDLE_SHA256" /tmp/orcad.tgz -tar -xzf /tmp/orcad.tgz -C "$ORCA_HOME/runtime" -rm -f /tmp/orcad.tgz +bundle="$(mktemp -d)/orcad.tgz" +fetch_verified "$ORCAD_BUNDLE_URL" "$ORCAD_BUNDLE_SHA256" "$bundle" +tar -xzf "$bundle" -C "$ORCA_HOME/runtime" +rm -rf "$(dirname "$bundle")" test -s "$ORCA_HOME/runtime/orcad/orcad-server.js" node="$(find "$ORCA_HOME/runtime/runtimes" -type f -name node | head -n 1)" test -x "$node" diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 2ce0646fd46..12ba480162e 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -30,4 +30,6 @@ export type Env = { /** Public URL of this Worker's /v1/egress/proxy; Vercel forwardURL rules point here. */ EGRESS_PROXY_URL?: string + /** Shortened session deadline (ms) for staging rollover rehearsals; refused when SANDBOX_ENV=prod. */ + SESSION_DEADLINE_MS?: string } diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 9506b2a6981..27882421a65 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -77,7 +77,9 @@ function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { environment: env.SANDBOX_ENV, goldenSnapshotId: env.GOLDEN_SNAPSHOT_ID, defaultRegion: env.VERCEL_REGION ?? 'iad1', - egressProxyUrl: env.EGRESS_PROXY_URL + egressProxyUrl: env.EGRESS_PROXY_URL, + sessionDeadlineMs: + env.SESSION_DEADLINE_MS && env.SANDBOX_ENV !== 'prod' ? Number(env.SESSION_DEADLINE_MS) : undefined } } } diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index c360f450e05..9abed5636b9 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -35,6 +35,8 @@ export type LifecycleDeps = { goldenSnapshotId: string defaultRegion: string egressProxyUrl?: string + /** Staging rehearsals shorten the cap; production uses SESSION_DEADLINE_MS. */ + sessionDeadlineMs?: number } } @@ -80,12 +82,13 @@ function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { return compileNetworkPolicy({ mode: 'allowlist', credentials }) } -export function sessionFields(state: ProviderSandboxState, now: number): SessionFields { +export function sessionFields(deps: LifecycleDeps, state: ProviderSandboxState): SessionFields { + const now = deps.now() const startedAt = state.session.startedAt ?? now return { session_id: state.session.id, session_started_at: startedAt, - session_deadline_at: startedAt + SESSION_DEADLINE_MS, + session_deadline_at: startedAt + (deps.config.sessionDeadlineMs ?? SESSION_DEADLINE_MS), lease_expires_at: startedAt + state.session.timeoutMs, usage_settled_at: startedAt } @@ -116,7 +119,7 @@ async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandbox repo, githubPlaceholder: deps.config.egressProxyUrl ? githubPlaceholderToken(row) : undefined }) - const running = await claim(deps, row, { status: 'running', last_error: null, ...sessionFields(state, deps.now()) }) + const running = await claim(deps, row, { status: 'running', last_error: null, ...sessionFields(deps, state) }) return { row: running, connection } } diff --git a/cloud/apps/sandbox-control/src/reconcile.ts b/cloud/apps/sandbox-control/src/reconcile.ts index 88299ed3a21..81fde8737b7 100644 --- a/cloud/apps/sandbox-control/src/reconcile.ts +++ b/cloud/apps/sandbox-control/src/reconcile.ts @@ -85,7 +85,7 @@ async function reconcileRunning(deps: LifecycleDeps, row: SandboxRow, summary: R async function repairTransition(deps: LifecycleDeps, row: SandboxRow): Promise { const state = await deps.provider.getSandbox(row.provider_name, { resume: false }) if (state?.session.status === 'running') { - await transition(deps.db, row, { status: 'running', ...sessionFields(state, deps.now()) }, deps.now()) + await transition(deps.db, row, { status: 'running', ...sessionFields(deps, state) }, deps.now()) return } await markProviderStopped(deps, row) diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index 36fa3286751..911d92ab376 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -162,6 +162,24 @@ Compiled per sandbox by `compileNetworkPolicy()`: recipe adapter; start one scheduler per ready hosted runtime (stop on suspend/destroy); an orcad RPC exposing the idle-probe verdict + last PTY input for `readActivity`; header warning + mobile push for `warn`. +## Live results (2026-11-09, Vercel Pro team `lovecast`, project `orca-sandbox-control`, iad1) + +Driven by the real Worker handler in-process (real Vercel REST, node:sqlite D1, stub billing) and the real desktop +driver/scheduler. Base OS is Amazon Linux 2023 (dnf), not Ubuntu. +- Golden image: orcad built from public stablyai/orca@7c14832 in a 8 vCPU build sandbox in 98s (67 MB tgz); + provision 14.3s (gh 2.103.3, Claude Code 2.1.319, Codex 0.166.0; orcad loopback smoke passed); wipe ok; + snapshot 8.2s. Bug found and fixed: provision downloaded the bundle over its own source path. +- Snapshot → orcad ready (`POST /v1/sandboxes`, incl. Vercel create + orca-sandbox-start): 5.4 / 4.1 / 4.9 s; WS open. +- Suspend (drain + stop) 7.6s; resume → ready 6.0s; same endpoint and same orcad host key (no re-pair). +- Shortened cap (6 min): scheduler warned T−15, then forced rollover at the deadline in 17.3s (drain, stop, boot, + orcad ready), reconnect WS open, new deadline set. Destroy 0.4–0.7s; zero sandboxes/snapshots left. +- Egress: allowlist resolves npm (200), `example.com` does not resolve (curl rc 6). Policy update 0.25s. + Placeholder → injected fake GitHub credential reaches GitHub (curl/gh `token` scheme: 401 Bad credentials); + attacker token and placeholder-less git → 403 `orca-egress-denied`. Node fetch under a transform rule still hangs + on Pro; `orca-egress-probe` reports `egress_framing_hang` (npm, untransformed: ok). No real GitHub token was used, + so a successful authenticated push is still unverified. +- Not covered live: forwardURL egress proxy (needs a deployed Worker URL), agent auto-resume with a real chat. + ## What needs live access (not done) Vercel Pro team id + project id + a Sandbox-scoped token (as Worker secrets), wrangler auth for a dev D1, a From 0306a56a535d3a5d2f69609f541b21012041d330 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:47:57 -0800 Subject: [PATCH 07/18] feat(cloud): dev deployment of the sandbox control plane (dev operator keys, dev egress credential) (#17) --- cloud/apps/sandbox-control/src/auth.ts | 52 ++++++++++++++----- cloud/apps/sandbox-control/src/env.ts | 4 ++ cloud/apps/sandbox-control/src/index.ts | 10 +++- cloud/apps/sandbox-control/wrangler.dev.jsonc | 32 ++++++++++++ cloud/docs/vercel-driver-design.md | 8 ++- 5 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 cloud/apps/sandbox-control/wrangler.dev.jsonc diff --git a/cloud/apps/sandbox-control/src/auth.ts b/cloud/apps/sandbox-control/src/auth.ts index 46aaa83409f..31f88782afe 100644 --- a/cloud/apps/sandbox-control/src/auth.ts +++ b/cloud/apps/sandbox-control/src/auth.ts @@ -47,6 +47,33 @@ export function accountIdFor(claims: { sub: string; org?: string }): string { return claims.org ? `org:${claims.org}` : `user:${claims.sub}` } +/** Dev-only operator keys (issuer DEV_TEST_ISSUER) so live tests run before Orca auth mints sandbox tokens. */ +export const DEV_TEST_ISSUER = 'orca-sandbox-dev-test' +let devKeys: { source: string; keys: JWTVerifyGetKey } | null = null + +async function verify( + token: string, + keys: JWTVerifyGetKey, + env: Env, + issuer: string | undefined, + now: number +): Promise { + try { + const { payload } = await jwtVerify(token, keys, { + audience: env.SANDBOX_JWT_AUDIENCE, + algorithms: ['ES256'], + maxTokenAge: '15m', + requiredClaims: ['exp', 'iat'], + currentDate: new Date(now), + ...(issuer ? { issuer } : {}) + }) + const claims = ClaimsSchema.parse(payload) + return { userId: claims.sub, accountId: accountIdFor(claims) } + } catch { + return null + } +} + export async function authenticateDesktop( env: Env, request: Request, @@ -56,18 +83,17 @@ export async function authenticateDesktop( if (!token) { return null } - try { - const { payload } = await jwtVerify(token, keysFor(env), { - audience: env.SANDBOX_JWT_AUDIENCE, - algorithms: ['ES256'], - maxTokenAge: '15m', - requiredClaims: ['exp', 'iat'], - currentDate: new Date(now), - ...(env.SANDBOX_AUTH_ISSUER ? { issuer: env.SANDBOX_AUTH_ISSUER } : {}) - }) - const claims = ClaimsSchema.parse(payload) - return { userId: claims.sub, accountId: accountIdFor(claims) } - } catch { - return null + const caller = await verify(token, keysFor(env), env, env.SANDBOX_AUTH_ISSUER, now) + if (caller || !env.SANDBOX_DEV_JWKS || env.SANDBOX_ENV === 'prod') { + return caller } + if (devKeys?.source !== env.SANDBOX_DEV_JWKS) { + devKeys = { + source: env.SANDBOX_DEV_JWKS, + keys: createLocalJWKSet(JSON.parse(env.SANDBOX_DEV_JWKS) as JSONWebKeySet) + } + } + // Dev callers land in their own account namespace so they never touch a real user's sandboxes. + const dev = await verify(token, devKeys.keys, env, DEV_TEST_ISSUER, now) + return dev ? { userId: dev.userId, accountId: `dev:${dev.userId}` } : null } diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 12ba480162e..fb404195be8 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -21,6 +21,8 @@ export type Env = { SANDBOX_JWKS?: string SANDBOX_AUTH_ISSUER?: string SANDBOX_JWT_AUDIENCE: string + /** Dev-only operator key set (issuer `orca-sandbox-dev-test`); ignored when SANDBOX_ENV=prod. */ + SANDBOX_DEV_JWKS?: string // Billing Worker (entitlement checks + usage). CONTROL_PLANE_TOKEN is shared with it. BILLING_URL?: string @@ -32,4 +34,6 @@ export type Env = { EGRESS_PROXY_URL?: string /** Shortened session deadline (ms) for staging rollover rehearsals; refused when SANDBOX_ENV=prod. */ SESSION_DEADLINE_MS?: string + /** Dev-only: Authorization the egress proxy injects for api.github.com until the GitHub App minter exists. */ + EGRESS_DEV_STATIC_AUTHORIZATION?: string } diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 27882421a65..f1b9936d464 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -40,6 +40,14 @@ export type Deps = { egressKeysForIssuer?: Parameters[1]['keysForIssuer'] } +function devCredentials(env: Env): CredentialBroker { + const value = env.EGRESS_DEV_STATIC_AUTHORIZATION + if (!value || env.SANDBOX_ENV === 'prod') { + return NO_CREDENTIALS + } + return { authorizationFor: async (_row, host) => (host === 'api.github.com' ? value : null) } +} + function json(body: unknown, status = 200): Response { return Response.json(body, { status, headers: { 'cache-control': 'no-store' } }) } @@ -96,7 +104,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) projectId: env.VERCEL_PROJECT_ID, findSandbox: (name) => getSandboxByProviderName(env.SANDBOX_DB, name), placeholdersFor: githubPlaceholderHeaders, - credentials: deps.credentials ?? NO_CREDENTIALS, + credentials: deps.credentials ?? devCredentials(env), fetch: deps.egressFetch, keysForIssuer: deps.egressKeysForIssuer }) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc new file mode 100644 index 00000000000..7d4b1523d21 --- /dev/null +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -0,0 +1,32 @@ +{ + "$schema": "node_modules/wrangler/config-schema.json", + // DEV deployment only (workers.dev, no custom domain). Deploy: + // wrangler deploy -c wrangler.dev.jsonc + // Secrets (wrangler secret put -c wrangler.dev.jsonc, never here): VERCEL_TOKEN, SANDBOX_DEV_JWKS, + // EGRESS_DEV_STATIC_AUTHORIZATION, and CONTROL_PLANE_TOKEN once BILLING_URL points at billing dev. + "name": "orca-sandbox-control-dev", + "main": "src/index.ts", + "compatibility_date": "2026-11-01", + "workers_dev": true, + "d1_databases": [ + { + "binding": "SANDBOX_DB", + "database_name": "orca-sandbox-control-dev", + "database_id": "e9f259e9-94b3-4e18-a132-2d807693a498", + "migrations_dir": "migrations" + } + ], + "triggers": { "crons": ["*/5 * * * *"] }, + "vars": { + "SANDBOX_ENV": "dev", + "SANDBOX_JWT_AUDIENCE": "orca-sandbox", + "SANDBOX_JWKS_URL": "https://login.onorca.dev/.well-known/jwks.json", + "SANDBOX_AUTH_ISSUER": "https://login.onorca.dev", + "VERCEL_TEAM_ID": "team_c5IO9DfEYAxKEUgMxrbEtO0a", + "VERCEL_PROJECT_ID": "prj_SkVtv6zJurF9dUHpfTRy4v9avwPq", + "VERCEL_REGION": "iad1", + "ENTITLEMENTS_MODE": "stub", + // GOLDEN_SNAPSHOT_ID: unset until an image is built (image/build-golden-image.ts); lifecycle routes answer 503 meanwhile. + "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" + } +} diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index 911d92ab376..06e6a0bb5da 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -178,7 +178,13 @@ driver/scheduler. Base OS is Amazon Linux 2023 (dnf), not Ubuntu. attacker token and placeholder-less git → 403 `orca-egress-denied`. Node fetch under a transform rule still hangs on Pro; `orca-egress-probe` reports `egress_framing_hang` (npm, untransformed: ok). No real GitHub token was used, so a successful authenticated push is still unverified. -- Not covered live: forwardURL egress proxy (needs a deployed Worker URL), agent auto-resume with a real chat. +- Not covered live: agent auto-resume with a real chat (needs a real Claude/Codex sign-in). + +Dev deployment (2026-11-09): Worker https://orca-sandbox-control-dev.lovecast.workers.dev, D1 +`orca-sandbox-control-dev`, config `wrangler.dev.jsonc`, billing stubbed (no billing dev Worker yet). Through it: +create 5.1s, rollover 12.2s (same host key), destroy 1.0s, no snapshots left. Egress proxy end to end from a real +sandbox: Bearer and gh `token` placeholders forwarded and re-injected (GitHub answers the injected fake: 401), Node +fetch completes in 450 ms with chunked framing (the transform-rule hang is gone), attacker token / no auth → 403. ## What needs live access (not done) From 8fb42f939ae46b6e534ad53eed4dfdcc3eed8225 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:42:26 -0800 Subject: [PATCH 08/18] feat(cloud): GitHub App repo tokens + billing dev wiring for the sandbox control plane (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(cloud): GitHub App installation tokens for sandbox git (manifest flow, minter, policy refresh) * feat(cloud): billing dev wiring — shared control-plane token via service binding, grace-expired suspensions, standard/large only --- .../image/vm/orca-sandbox-start | 4 + .../migrations/0002_github_repo.sql | 3 + .../apps/sandbox-control/src/entitlements.ts | 50 +++++++--- cloud/apps/sandbox-control/src/env.ts | 6 ++ cloud/apps/sandbox-control/src/github-app.ts | 94 ++++++++++++++++++ cloud/apps/sandbox-control/src/index.ts | 96 ++++++++++++++++++- cloud/apps/sandbox-control/src/lifecycle.ts | 78 +++++++++++++-- cloud/apps/sandbox-control/src/reconcile.ts | 34 ++++++- .../src/sandbox-vm-commands.ts | 5 + cloud/apps/sandbox-control/src/store.ts | 12 ++- cloud/apps/sandbox-control/src/wire.ts | 2 +- .../test/entitlements-client.test.ts | 39 ++++++++ .../sandbox-control/test/github-app.test.ts | 73 ++++++++++++++ cloud/apps/sandbox-control/test/harness.ts | 9 +- .../test/lifecycle-flow.test.ts | 40 +++++++- cloud/apps/sandbox-control/wrangler.dev.jsonc | 9 +- .../hosted-sandbox/hosted-sandbox-wire.ts | 3 +- 17 files changed, 516 insertions(+), 41 deletions(-) create mode 100644 cloud/apps/sandbox-control/migrations/0002_github_repo.sql create mode 100644 cloud/apps/sandbox-control/src/github-app.ts create mode 100644 cloud/apps/sandbox-control/test/entitlements-client.test.ts create mode 100644 cloud/apps/sandbox-control/test/github-app.test.ts diff --git a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start index af3fbf961b8..2c448768c89 100755 --- a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start +++ b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start @@ -22,6 +22,10 @@ fi rm -f "$STATE/ready.json" "$STATE/orcad.pid" printf '%s' "$boot_id" >"$STATE/boot-id" +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 if [ ! -d "$PROJECT" ]; then if [ -n "${ORCA_REPO_URL:-}" ]; then git clone --quiet ${ORCA_REPO_REF:+--branch "$ORCA_REPO_REF"} -- "$ORCA_REPO_URL" "$PROJECT" diff --git a/cloud/apps/sandbox-control/migrations/0002_github_repo.sql b/cloud/apps/sandbox-control/migrations/0002_github_repo.sql new file mode 100644 index 00000000000..7b4242eeb91 --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0002_github_repo.sql @@ -0,0 +1,3 @@ +-- The workspace repository (for GitHub App installation tokens) and when the injected token expires. +ALTER TABLE sandboxes ADD COLUMN repo_url TEXT; +ALTER TABLE sandboxes ADD COLUMN github_token_expires_at INTEGER; diff --git a/cloud/apps/sandbox-control/src/entitlements.ts b/cloud/apps/sandbox-control/src/entitlements.ts index ae24d148c36..5eb71ae2664 100644 --- a/cloud/apps/sandbox-control/src/entitlements.ts +++ b/cloud/apps/sandbox-control/src/entitlements.ts @@ -3,9 +3,11 @@ import { z } from 'zod' import type { Env } from './env' -export type MachineSize = 'standard' | 'large' | 'xlarge' +/** What billing sells; anything else is refused there. */ +export type MachineSize = 'standard' | 'large' -export type StartDecision = { allow: boolean; reason: string | null } +/** `suspendRunning`: payment grace ran out, so the account's running sandboxes must stop too. */ +export type StartDecision = { allow: boolean; reason: string | null; suspendRunning: boolean } export type UsageSegment = { usageId: string @@ -16,7 +18,7 @@ export type UsageSegment = { endedAt: number } -export type UsageAnswer = { canStartSandbox: boolean; reason: string | null } +export type UsageAnswer = { canStartSandbox: boolean; reason: string | null; suspendRunning: boolean } export type EntitlementClient = { checkStart(request: { @@ -25,6 +27,8 @@ export type EntitlementClient = { runningSandboxes: number }): Promise reportUsage(segment: UsageSegment): Promise + /** Accounts whose payment grace expired; polled by the cron. */ + listSuspensions(): Promise } export class BillingUnavailableError extends Error { @@ -34,39 +38,55 @@ export class BillingUnavailableError extends Error { } } -const StartAnswer = z.looseObject({ allow: z.boolean(), reason: z.string().nullable() }) -const UsageResponse = z.looseObject({ canStartSandbox: z.boolean(), reason: z.string().nullable() }) +const StartAnswer = z.looseObject({ + allow: z.boolean(), + reason: z.string().nullable(), + suspendRunning: z.boolean().default(false) +}) +const UsageResponse = z.looseObject({ + canStartSandbox: z.boolean(), + reason: z.string().nullable(), + suspendRunning: z.boolean().default(false) +}) +const SuspensionsResponse = z.looseObject({ + accounts: z.array(z.looseObject({ accountId: z.string() })).max(10_000) +}) /** Reasons that mean running sandboxes must stop, not just that new starts wait. */ export const STOP_RUNNING_REASONS: ReadonlySet = new Set([ 'included_hours_exhausted', 'overage_cap_reached', 'subscription_inactive', - 'no_subscription' + 'no_subscription', + 'payment_grace_expired' ]) -export function createEntitlementClient(env: Env, doFetch: typeof fetch = fetch): EntitlementClient { +export function createEntitlementClient( + env: Env, + doFetch: typeof fetch = env.BILLING ? (input, init) => env.BILLING!.fetch(input, init) : fetch +): EntitlementClient { if (env.ENTITLEMENTS_MODE === 'stub') { if (env.SANDBOX_ENV === 'prod') { throw new BillingUnavailableError('stub entitlements refused in prod') } return { - checkStart: async () => ({ allow: true, reason: null }), - reportUsage: async () => ({ canStartSandbox: true, reason: null }) + checkStart: async () => ({ allow: true, reason: null, suspendRunning: false }), + reportUsage: async () => ({ canStartSandbox: true, reason: null, suspendRunning: false }), + listSuspensions: async () => [] } } const base = env.BILLING_URL const token = env.CONTROL_PLANE_TOKEN - async function post(path: string, body: unknown, schema: z.ZodType): Promise { + async function send(method: 'GET' | 'POST', path: string, body: unknown, schema: z.ZodType): Promise { if (!base || !token) { throw new BillingUnavailableError('billing not configured') } let response: Response try { response = await doFetch(new URL(path, base), { - method: 'POST', + method, headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, - body: JSON.stringify(body), + body: body === undefined ? undefined : JSON.stringify(body), redirect: 'manual', signal: AbortSignal.timeout(10_000) }) @@ -83,7 +103,9 @@ export function createEntitlementClient(env: Env, doFetch: typeof fetch = fetch) return parsed.data } return { - checkStart: (request) => post('/v1/internal/entitlements/check', request, StartAnswer), - reportUsage: (segment) => post('/v1/internal/usage', segment, UsageResponse) + checkStart: (request) => send('POST', '/v1/internal/entitlements/check', request, StartAnswer), + reportUsage: (segment) => send('POST', '/v1/internal/usage', segment, UsageResponse), + listSuspensions: async () => + (await send('GET', '/v1/internal/suspensions', undefined, SuspensionsResponse)).accounts.map((a) => a.accountId) } } diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index fb404195be8..9138af9e9e5 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -26,6 +26,8 @@ export type Env = { // Billing Worker (entitlement checks + usage). CONTROL_PLANE_TOKEN is shared with it. BILLING_URL?: string + /** Service binding to the billing Worker; Workers on one account cannot reach each other over workers.dev. */ + BILLING?: Fetcher CONTROL_PLANE_TOKEN?: string /** `stub` allows every start and records nothing: local dev only, refused when SANDBOX_ENV=prod. */ ENTITLEMENTS_MODE?: string @@ -36,4 +38,8 @@ export type Env = { SESSION_DEADLINE_MS?: string /** Dev-only: Authorization the egress proxy injects for api.github.com until the GitHub App minter exists. */ EGRESS_DEV_STATIC_AUTHORIZATION?: string + + // GitHub App (contents/pull_requests write, metadata read). The PKCS#8 key is a secret. + GITHUB_APP_ID?: string + GITHUB_APP_PRIVATE_KEY?: string } diff --git a/cloud/apps/sandbox-control/src/github-app.ts b/cloud/apps/sandbox-control/src/github-app.ts new file mode 100644 index 00000000000..ebea80124dc --- /dev/null +++ b/cloud/apps/sandbox-control/src/github-app.ts @@ -0,0 +1,94 @@ +// GitHub App installation tokens for sandbox git: 1h, scoped to the workspace's one repository with +// contents + pull_requests write. Minted here, injected at the egress layer, never stored in the VM. +import { SignJWT, importPKCS8 } from 'jose' + +export type GitHubRepoToken = { token: string; expiresAt: number } + +export type GitHubAppMinter = { + /** Null when the repo is not on github.com or the app is not installed on it. */ + tokenForRepo(repoUrl: string): Promise +} + +const REFRESH_MARGIN_MS = 5 * 60_000 + +export function parseGitHubRepo(repoUrl: string): { owner: string; repo: string } | null { + try { + const url = new URL(repoUrl) + const match = /^\/([A-Za-z0-9-]+)\/([A-Za-z0-9._-]+?)(?:\.git)?\/?$/.exec(url.pathname) + return url.protocol === 'https:' && url.hostname === 'github.com' && match + ? { owner: match[1]!, repo: match[2]! } + : null + } catch { + return null + } +} + +export function createGitHubAppMinter(config: { + appId: string + privateKeyPkcs8: string + fetch?: typeof fetch + now?: () => number +}): GitHubAppMinter { + const doFetch = config.fetch ?? fetch + const now = config.now ?? Date.now + const cache = new Map() + let signingKey: CryptoKey | null = null + + async function appJwt(): Promise { + signingKey ??= await importPKCS8(config.privateKeyPkcs8, 'RS256') + const iat = Math.floor(now() / 1000) - 60 + return new SignJWT({}) + .setProtectedHeader({ alg: 'RS256' }) + .setIssuer(config.appId) + .setIssuedAt(iat) + .setExpirationTime(iat + 9 * 60) + .sign(signingKey) + } + + async function github(method: string, path: string, body?: unknown): Promise { + return doFetch(`https://api.github.com${path}`, { + method, + headers: { + authorization: `Bearer ${await appJwt()}`, + accept: 'application/vnd.github+json', + 'x-github-api-version': '2022-11-28', + 'user-agent': 'orca-sandbox-control' + }, + body: body === undefined ? undefined : JSON.stringify(body), + redirect: 'manual' + }) + } + + return { + async tokenForRepo(repoUrl) { + const parsed = parseGitHubRepo(repoUrl) + if (!parsed) { + return null + } + const key = `${parsed.owner}/${parsed.repo}`.toLowerCase() + const cached = cache.get(key) + if (cached && cached.expiresAt - now() > REFRESH_MARGIN_MS) { + return cached + } + const installation = await github('GET', `/repos/${parsed.owner}/${parsed.repo}/installation`) + if (installation.status === 404) { + return null + } + if (!installation.ok) { + throw new Error(`github installation lookup failed (${installation.status})`) + } + const { id } = (await installation.json()) as { id: number } + const minted = await github('POST', `/app/installations/${id}/access_tokens`, { + repositories: [parsed.repo], + permissions: { contents: 'write', pull_requests: 'write', metadata: 'read' } + }) + if (!minted.ok) { + throw new Error(`github token mint failed (${minted.status})`) + } + const body = (await minted.json()) as { token: string; expires_at: string } + const token = { token: body.token, expiresAt: Date.parse(body.expires_at) } + cache.set(key, token) + return token + } + } +} diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index f1b9936d464..f34e9f270a9 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -22,6 +22,7 @@ import { suspendSandbox, type LifecycleDeps } from './lifecycle' +import { createGitHubAppMinter, type GitHubAppMinter } from './github-app' import { reconcile } from './reconcile' import { ProviderError, type SandboxProviderApi } from './sandbox-provider' import { VmCommandError } from './sandbox-vm-commands' @@ -35,11 +36,91 @@ export type Deps = { provider?: SandboxProviderApi entitlements?: EntitlementClient credentials?: CredentialBroker + github?: GitHubAppMinter now?: () => number egressFetch?: typeof fetch egressKeysForIssuer?: Parameters[1]['keysForIssuer'] } +const minters = new Map() +function githubMinter(env: Env): GitHubAppMinter | undefined { + if (!env.GITHUB_APP_ID || !env.GITHUB_APP_PRIVATE_KEY) { + return undefined + } + let minter = minters.get(env.GITHUB_APP_ID) + if (!minter) { + minter = createGitHubAppMinter({ appId: env.GITHUB_APP_ID, privateKeyPkcs8: env.GITHUB_APP_PRIVATE_KEY }) + minters.set(env.GITHUB_APP_ID, minter) + } + return minter +} + +function egressCredentials(env: Env): CredentialBroker { + const minter = githubMinter(env) + if (!minter) { + return devCredentials(env) + } + return { + authorizationFor: async (row, host) => { + if (host !== 'api.github.com' || !row.repo_url) { + return null + } + const minted = await minter.tokenForRepo(row.repo_url) + return minted ? `Bearer ${minted.token}` : null + } + } +} + +function html(body: string): Response { + return new Response(`${body}`, { + headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' } + }) +} + +/** + * One-click GitHub App creation (manifest flow). Permissions are the minimum for sandbox git: + * contents write (clone, fetch, push), pull_requests write (open/update PRs from the workspace), + * metadata read (required by GitHub for any repo access). No webhook: tokens are minted on demand. + */ +function githubManifestStart(url: URL): Response { + const origin = url.origin + const manifest = { + name: 'Orca Cloud (dev)', + url: origin, + redirect_url: `${origin}/v1/github/manifest-callback`, + setup_url: `${origin}/v1/github/setup`, + setup_on_update: false, + // Installable on personal accounts too; installing grants nothing until a repo is selected. + public: true, + hook_attributes: { url: `${origin}/v1/github/webhook-unused`, active: false }, + default_permissions: { contents: 'write', metadata: 'read', pull_requests: 'write' }, + default_events: [], + request_oauth_on_install: false + } + const state = crypto.randomUUID() + const escaped = JSON.stringify(manifest).replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<') + return html( + `
` + + `
` + + '' + ) +} + +/** GitHub App manifest flow redirect: the operator's exchange script reads the one-time code from logs. */ +function githubManifestCallback(env: Env, url: URL): Response { + if (env.SANDBOX_ENV === 'prod') { + return fail('not_found', 404) + } + const code = url.searchParams.get('code') ?? '' + if (!/^[A-Za-z0-9_-]{8,128}$/.test(code)) { + return html('Missing code.') + } + console.log(JSON.stringify({ event: 'github_manifest_code', code, state: url.searchParams.get('state') })) + return html( + 'GitHub App created. The Orca agent is finishing setup.

Next: install it on nwparker → Only select repositories → orca-sandbox-push-test.' + ) +} + function devCredentials(env: Env): CredentialBroker { const value = env.EGRESS_DEV_STATIC_AUTHORIZATION if (!value || env.SANDBOX_ENV === 'prod') { @@ -80,6 +161,7 @@ function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { baseUrl: env.VERCEL_API_BASE }), entitlements: deps.entitlements ?? createEntitlementClient(env), + github: deps.github ?? githubMinter(env), now: deps.now ?? Date.now, config: { environment: env.SANDBOX_ENV, @@ -104,11 +186,20 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) projectId: env.VERCEL_PROJECT_ID, findSandbox: (name) => getSandboxByProviderName(env.SANDBOX_DB, name), placeholdersFor: githubPlaceholderHeaders, - credentials: deps.credentials ?? devCredentials(env), + credentials: deps.credentials ?? egressCredentials(env), fetch: deps.egressFetch, keysForIssuer: deps.egressKeysForIssuer }) } + if (url.pathname === '/v1/github/manifest-callback') { + return githubManifestCallback(env, url) + } + if (url.pathname === '/v1/github/manifest-start' && env.SANDBOX_ENV !== 'prod') { + return githubManifestStart(url) + } + if (url.pathname === '/v1/github/setup') { + return html('Orca Cloud (dev) is installed. You can close this tab.') + } const match = /^\/v1\/sandboxes(?:\/([A-Za-z0-9_]+)(?:\/(suspend|resume|lease|rollover))?)?$/.exec(url.pathname) if (!match) { return fail('not_found', 404) @@ -198,7 +289,8 @@ export default { async scheduled(_controller: ScheduledController, env: Env, ctx: ExecutionContext): Promise { const life = lifecycleDeps(env, {}) if (life) { - ctx.waitUntil(reconcile(life).then(() => undefined)) + // Counts only; no ids, tokens or pairing material. + ctx.waitUntil(reconcile(life).then((summary) => console.log(JSON.stringify({ event: 'reconcile', ...summary })))) } } } satisfies ExportedHandler diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 9abed5636b9..82ae764e05b 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -1,7 +1,8 @@ // Sandbox lifecycle: one persistent provider sandbox per Orca workspace, with an Orca-owned lease, // a forced stop before the provider's 24h cap, usage metering and write-ahead rows. import type { EntitlementClient, MachineSize } from './entitlements' -import { compileNetworkPolicy, type CredentialRoute } from './network-policy' +import type { GitHubAppMinter } from './github-app' +import { compileNetworkPolicy, gitBasicAuthorization, type CredentialRoute } from './network-policy' import type { ProviderSandboxState, SandboxProviderApi } from './sandbox-provider' import { ORCAD_PORT, drainOrcad, startOrcad, type OrcadReady } from './sandbox-vm-commands' import { @@ -9,6 +10,7 @@ import { countActiveSandboxes, getSandbox, insertSandbox, + listAccountSandboxes, liveSandboxForWorkspace, markSnapshotsDeleted, recordSnapshots, @@ -23,12 +25,14 @@ export const LEASE_MS = 30 * 60_000 const PROVIDER_SESSION_CAP_MS = 24 * 60 * 60_000 /** Orca stops 10 min early so it, not the provider, ends the session (drain first). */ export const SESSION_DEADLINE_MS = PROVIDER_SESSION_CAP_MS - 10 * 60_000 -const VCPUS: Record = { standard: 4, large: 8, xlarge: 16 } +const VCPUS: Record = { standard: 4, large: 8 } export type LifecycleDeps = { db: D1Database provider: SandboxProviderApi entitlements: EntitlementClient + /** GitHub App installation tokens for the workspace repo; absent until the app is configured. */ + github?: GitHubAppMinter now: () => number config: { environment: string @@ -67,7 +71,19 @@ export function githubPlaceholderHeaders(row: SandboxRow): string[] { return [`Bearer ${token}`, `token ${token}`] } -function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { +export function egressPolicy(deps: LifecycleDeps, row: SandboxRow, gitToken?: string) { + const git: CredentialRoute[] = gitToken + ? [ + { + // git (libcurl) is unaffected by the transform-rule framing hang, so it is injected inline. + host: 'github.com', + header: 'authorization', + placeholders: [gitBasicAuthorization(githubPlaceholderToken(row))], + via: 'transform', + value: gitBasicAuthorization(gitToken) + } + ] + : [] const credentials: CredentialRoute[] = deps.config.egressProxyUrl ? [ { @@ -79,7 +95,7 @@ function egressPolicy(deps: LifecycleDeps, row: SandboxRow) { } ] : [] - return compileNetworkPolicy({ mode: 'allowlist', credentials }) + return compileNetworkPolicy({ mode: 'allowlist', credentials: [...git, ...credentials] }) } export function sessionFields(deps: LifecycleDeps, state: ProviderSandboxState): SessionFields { @@ -105,6 +121,9 @@ async function claim(deps: LifecycleDeps, row: SandboxRow, patch: Partial { + if (!deps.github || !row.repo_url) { + return null + } + const minted = await deps.github.tokenForRepo(row.repo_url) + if (!minted) { + return null + } + await deps.provider.updateNetworkPolicy(sessionId, egressPolicy(deps, row, minted.token)) + return { expiresAt: minted.expiresAt } +} + export async function createSandbox( deps: LifecycleDeps, caller: { accountId: string; userId: string }, @@ -152,7 +195,8 @@ export async function createSandbox( provider: 'vercel', provider_name: `orca-${deps.config.environment}-${id}`, machine_size: request.machineSize, - region: request.region ?? deps.config.defaultRegion + region: request.region ?? deps.config.defaultRegion, + repo_url: request.repo?.url ?? null }, deps.now() ) @@ -180,6 +224,26 @@ export async function createSandbox( } } +/** + * Billing says the account's payment grace ran out: stop (snapshot-preserving, never destroy) every + * running sandbox. Resume stays refused by the start check until billing allows starts again. + */ +export async function suspendAccountRunning(deps: LifecycleDeps, accountId: string): Promise { + let suspended = 0 + for (const row of await listAccountSandboxes(deps.db, accountId)) { + if (row.status !== 'running') { + continue + } + try { + await suspendSandbox(deps, row) + suspended++ + } catch { + // A concurrent transition or provider hiccup; the next cron pass retries. + } + } + return suspended +} + /** A create that never became usable is destroyed now; the cron retries if this also fails. */ export async function failAndRelease(deps: LifecycleDeps, row: SandboxRow): Promise { const failed = await transition(deps.db, row, { status: 'failed', last_error: 'create_failed' }, deps.now()) diff --git a/cloud/apps/sandbox-control/src/reconcile.ts b/cloud/apps/sandbox-control/src/reconcile.ts index 81fde8737b7..2f0613a354d 100644 --- a/cloud/apps/sandbox-control/src/reconcile.ts +++ b/cloud/apps/sandbox-control/src/reconcile.ts @@ -5,6 +5,8 @@ import { destroySandbox, failAndRelease, markProviderStopped, + refreshGitCredentials, + suspendAccountRunning, sessionFields, suspendSandbox, type LifecycleDeps @@ -17,7 +19,13 @@ const STUCK_MS = 15 * 60_000 const FORCED_STOP_GRACE_MS = 5 * 60_000 export type ReconcileSummary = Record< - 'settled' | 'suspendedForDeadline' | 'suspendedForBilling' | 'leaseLapsed' | 'released' | 'repaired', + | 'settled' + | 'suspendedForDeadline' + | 'suspendedForBilling' + | 'leaseLapsed' + | 'released' + | 'repaired' + | 'billingErrors', number > @@ -28,7 +36,16 @@ export async function reconcile(deps: LifecycleDeps): Promise suspendedForBilling: 0, leaseLapsed: 0, released: 0, - repaired: 0 + repaired: 0, + billingErrors: 0 + } + const polled = await guard(async () => { + for (const accountId of await deps.entitlements.listSuspensions()) { + summary.suspendedForBilling += await suspendAccountRunning(deps, accountId) + } + }) + if (!polled) { + summary.billingErrors++ } for (const row of await listByStatus(deps.db, ['running'])) { await guard(() => reconcileRunning(deps, row, summary)) @@ -71,11 +88,18 @@ async function reconcileRunning(deps: LifecycleDeps, row: SandboxRow, summary: R return } } + if (row.github_token_expires_at !== null && row.session_id && row.github_token_expires_at - now < 15 * 60_000) { + const git = await refreshGitCredentials(deps, row, row.session_id) + const refreshed = await transition(deps.db, row, { github_token_expires_at: git?.expiresAt ?? null }, now) + row = refreshed ?? row + } const settled = await queueUsage(deps.db, row, now) const current = settled === row.usage_settled_at ? row : await transition(deps.db, row, { usage_settled_at: settled }, now) summary.settled++ const answer = await flushUsage(deps.db, deps.entitlements, now, row.id) - if (current && answer && !answer.canStartSandbox && STOP_RUNNING_REASONS.has(answer.reason ?? '')) { + if (answer?.suspendRunning) { + summary.suspendedForBilling += await suspendAccountRunning(deps, row.account_id) + } else if (current && answer && !answer.canStartSandbox && STOP_RUNNING_REASONS.has(answer.reason ?? '')) { await suspendSandbox(deps, current) summary.suspendedForBilling++ } @@ -91,10 +115,12 @@ async function repairTransition(deps: LifecycleDeps, row: SandboxRow): Promise Promise): Promise { +async function guard(step: () => Promise): Promise { try { await step() + return true } catch { // One bad row must not stall the pass; it is retried next tick. + return false } } diff --git a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts index 8487b38b690..240af46c6a5 100644 --- a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts +++ b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts @@ -33,6 +33,8 @@ export type StartInputs = { repo?: { url: string; ref?: string } /** Token the VM's GitHub clients send; the egress layer swaps it for a real short-lived one. */ githubPlaceholder?: string + /** git `http.https://github.com/.extraheader` carrying the placeholder, so every git request is claimable. */ + gitExtraHeader?: string } /** Starts orcad (idempotent per boot) and returns its pairing offer; pairing codes are never logged. */ @@ -55,6 +57,9 @@ export async function startOrcad( env.ORCA_REPO_REF = inputs.repo.ref } } + if (inputs.gitExtraHeader) { + env.ORCA_GIT_EXTRAHEADER = inputs.gitExtraHeader + } if (inputs.githubPlaceholder) { env.ORCA_GITHUB_PLACEHOLDER = inputs.githubPlaceholder } diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index b8ff00b8766..e982da936f1 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -32,6 +32,8 @@ export type SandboxRow = { created_at: number updated_at: number destroyed_at: number | null + repo_url: string | null + github_token_expires_at: number | null } export type SessionFields = Pick< @@ -54,18 +56,18 @@ export async function insertSandbox( db: D1Database, row: Pick< SandboxRow, - 'id' | 'account_id' | 'user_id' | 'workspace_id' | 'provider' | 'provider_name' | 'machine_size' | 'region' + 'id' | 'account_id' | 'user_id' | 'workspace_id' | 'provider' | 'provider_name' | 'machine_size' | 'region' | 'repo_url' >, now: number ): Promise { await db .prepare( `INSERT INTO sandboxes (id, account_id, user_id, workspace_id, provider, provider_name, status, - machine_size, region, generation, created_at, updated_at) - VALUES (?, ?, ?, ?, ?, ?, 'creating', ?, ?, 0, ?, ?)` + machine_size, region, generation, created_at, updated_at, repo_url) + VALUES (?, ?, ?, ?, ?, ?, 'creating', ?, ?, 0, ?, ?, ?)` ) .bind(row.id, row.account_id, row.user_id, row.workspace_id, row.provider, row.provider_name, - row.machine_size, row.region, now, now) + row.machine_size, row.region, now, now, row.repo_url) .run() const inserted = await getSandbox(db, row.id) if (!inserted) { @@ -122,7 +124,7 @@ export async function listByStatus(db: D1Database, statuses: readonly SandboxSta type Patch = Partial> const PATCHABLE = new Set([ 'status', 'session_id', 'session_started_at', 'session_deadline_at', 'lease_expires_at', - 'usage_settled_at', 'last_error', 'destroyed_at' + 'usage_settled_at', 'last_error', 'destroyed_at', 'github_token_expires_at' ]) /** Applies `patch` only if nobody moved the row since `expected` was read; null on a lost race. */ diff --git a/cloud/apps/sandbox-control/src/wire.ts b/cloud/apps/sandbox-control/src/wire.ts index aaee92b168f..1d7a323c477 100644 --- a/cloud/apps/sandbox-control/src/wire.ts +++ b/cloud/apps/sandbox-control/src/wire.ts @@ -13,7 +13,7 @@ const Id = z export const CreateRequestSchema = z .object({ workspaceId: Id, - machineSize: z.enum(['standard', 'large', 'xlarge']), + machineSize: z.enum(['standard', 'large']), region: z .string() .regex(/^[a-z]{3}[0-9]$/) diff --git a/cloud/apps/sandbox-control/test/entitlements-client.test.ts b/cloud/apps/sandbox-control/test/entitlements-client.test.ts new file mode 100644 index 00000000000..69603b012f8 --- /dev/null +++ b/cloud/apps/sandbox-control/test/entitlements-client.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest' +import { BillingUnavailableError, createEntitlementClient } from '../src/entitlements' +import type { Env } from '../src/env' + +const env = { BILLING_URL: 'https://billing.test', CONTROL_PLANE_TOKEN: 'cp_'.padEnd(40, 'x'), SANDBOX_ENV: 'dev' } as Env + +describe('billing client', () => { + it('reads billing dev answers, including grace-expired suspensions', async () => { + const seen: string[] = [] + const client = createEntitlementClient(env, async (input, init) => { + const url = new URL(String(input)) + seen.push(`${init?.method} ${url.pathname} ${new Headers(init?.headers).get('authorization')}`) + if (url.pathname === '/v1/internal/suspensions') { + return Response.json({ accounts: [{ accountId: 'org:o1', pastDueSince: 1, graceEndsAt: 2 }] }) + } + return Response.json({ allow: false, reason: 'payment_grace_expired', suspendRunning: true, entitlement: {} }) + }) + expect(await client.listSuspensions()).toEqual(['org:o1']) + expect(await client.checkStart({ accountId: 'org:o1', machineSize: 'standard', runningSandboxes: 0 })).toEqual({ + allow: false, + reason: 'payment_grace_expired', + suspendRunning: true, + entitlement: {} + }) + expect(seen[0]).toBe(`GET /v1/internal/suspensions Bearer ${env.CONTROL_PLANE_TOKEN}`) + }) + + it('fails closed when billing is unreachable and refuses the stub in prod', async () => { + const down = createEntitlementClient(env, async () => { + throw new Error('offline') + }) + await expect(down.checkStart({ accountId: 'a', machineSize: 'large', runningSandboxes: 0 })).rejects.toBeInstanceOf( + BillingUnavailableError + ) + expect(() => createEntitlementClient({ ...env, ENTITLEMENTS_MODE: 'stub', SANDBOX_ENV: 'prod' })).toThrow( + BillingUnavailableError + ) + }) +}) diff --git a/cloud/apps/sandbox-control/test/github-app.test.ts b/cloud/apps/sandbox-control/test/github-app.test.ts new file mode 100644 index 00000000000..ff41cd639f8 --- /dev/null +++ b/cloud/apps/sandbox-control/test/github-app.test.ts @@ -0,0 +1,73 @@ +import { exportPKCS8, generateKeyPair, decodeJwt } from 'jose' +import { describe, expect, it } from 'vitest' +import { createGitHubAppMinter } from '../src/github-app' +import { gitBasicAuthorization } from '../src/network-policy' +import { START_COMMAND } from '../src/sandbox-vm-commands' +import { createHarness } from './harness' + +const MIN = 60_000 + +describe('GitHub App installation tokens', () => { + it('mints a token scoped to the one workspace repo and reuses it until near expiry', async () => { + const { privateKey } = await generateKeyPair('RS256', { extractable: true }) + const calls: { method: string; path: string; body: unknown; iss: unknown }[] = [] + let now = Date.parse('2026-11-09T00:00:00Z') + const minter = createGitHubAppMinter({ + appId: '123', + privateKeyPkcs8: await exportPKCS8(privateKey), + now: () => now, + fetch: async (input, init) => { + const path = new URL(String(input)).pathname + const jwt = new Headers(init?.headers).get('authorization')!.replace('Bearer ', '') + calls.push({ method: init?.method ?? 'GET', path, body: init?.body ? JSON.parse(String(init.body)) : undefined, iss: decodeJwt(jwt).iss }) + return path.endsWith('/installation') + ? Response.json({ id: 77 }) + : Response.json({ token: 'ghs_minted', expires_at: new Date(now + 60 * MIN).toISOString() }) + } + }) + expect(await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test.git')).toEqual({ token: 'ghs_minted', expiresAt: now + 60 * MIN }) + expect(calls).toEqual([ + { method: 'GET', path: '/repos/nwparker/orca-sandbox-push-test/installation', body: undefined, iss: '123' }, + { + method: 'POST', + path: '/app/installations/77/access_tokens', + body: { repositories: ['orca-sandbox-push-test'], permissions: { contents: 'write', pull_requests: 'write', metadata: 'read' } }, + iss: '123' + } + ]) + now += 30 * MIN + await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test') + expect(calls).toHaveLength(2) + now += 26 * MIN + await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test') + expect(calls).toHaveLength(4) + expect(await minter.tokenForRepo('https://gitlab.com/a/b')).toBeNull() + }) + + it('injects the repo token for git and refreshes it before it expires', async () => { + let expiresAt = 0 + const h = await createHarness({}, { + github: { tokenForRepo: async () => ({ token: `ghs_${expiresAt}`, expiresAt }) } + }) + expiresAt = h.now() + 60 * MIN + const { body } = await h.call('POST', '/v1/sandboxes', { + body: { workspaceId: 'ws-git', machineSize: 'standard', repo: { url: 'https://github.com/nwparker/orca-sandbox-push-test.git' } } + }) + const placeholder = gitBasicAuthorization(`orca-ph-${body.sandbox.id}`) + const [box] = [...h.vercel.boxes.values()] + expect(box!.policy.allow['github.com']![0]).toEqual({ + match: { headers: [{ key: { exact: 'authorization' }, value: { exact: placeholder } }] }, + transform: [{ headers: { authorization: gitBasicAuthorization(`ghs_${expiresAt}`) } }] + }) + const start = h.vercel.commands.find((c) => c.command === START_COMMAND)! + expect(start.env.ORCA_GIT_EXTRAHEADER).toBe(`Authorization: ${placeholder}`) + expect(start.env.ORCA_REPO_URL).toBe('https://github.com/nwparker/orca-sandbox-push-test.git') + + const first = expiresAt + h.advance(50 * MIN) + expiresAt = h.now() + 60 * MIN + await h.reconcile() + expect(box!.policy.allow['github.com']![0]).toMatchObject({ transform: [{ headers: { authorization: gitBasicAuthorization(`ghs_${expiresAt}`) } }] }) + expect(expiresAt).not.toBe(first) + }) +}) diff --git a/cloud/apps/sandbox-control/test/harness.ts b/cloud/apps/sandbox-control/test/harness.ts index d27e7e5ee8c..8911feecb27 100644 --- a/cloud/apps/sandbox-control/test/harness.ts +++ b/cloud/apps/sandbox-control/test/harness.ts @@ -16,8 +16,9 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par const clock = () => now const vercel = createFakeVercel(clock) const billing = { - decision: { allow: true, reason: null } as StartDecision, - usageAnswer: { canStartSandbox: true, reason: null } as UsageAnswer, + decision: { allow: true, reason: null, suspendRunning: false } as StartDecision, + usageAnswer: { canStartSandbox: true, reason: null, suspendRunning: false } as UsageAnswer, + suspensions: [] as string[], down: false, checks: [] as { accountId: string; runningSandboxes: number }[], usage: [] as UsageSegment[] @@ -33,6 +34,9 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par } billing.usage.push(segment) return billing.usageAnswer + }, + async listSuspensions() { + return billing.suspensions } } const env: Env = { @@ -91,6 +95,7 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par db, provider: vercel, entitlements, + github: extraDeps.github, now: clock, config: { environment: 'test', goldenSnapshotId: GOLDEN, defaultRegion: 'iad1' } }) diff --git a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts index bf7dc94d6aa..50af6ad9350 100644 --- a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts +++ b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts @@ -37,7 +37,7 @@ describe('sandbox control plane lifecycle', () => { it('refuses a start billing denies without touching the provider', async () => { const h = await createHarness() - h.billing.decision = { allow: false, reason: 'concurrency_limit' } + h.billing.decision = { allow: false, reason: 'concurrency_limit', suspendRunning: false } const res = await h.call('POST', '/v1/sandboxes', { body: create }) expect(res).toEqual({ status: 403, body: { error: 'entitlement_denied', reason: 'concurrency_limit' } }) expect(h.vercel.boxes.size).toBe(0) @@ -163,7 +163,7 @@ describe('sandbox control plane cron', () => { it('stops running sandboxes once billing says the included hours are gone', async () => { const h = await createHarness() const { body } = await h.call('POST', '/v1/sandboxes', { body: create }) - h.billing.usageAnswer = { canStartSandbox: false, reason: 'included_hours_exhausted' } + h.billing.usageAnswer = { canStartSandbox: false, reason: 'included_hours_exhausted', suspendRunning: false } h.advance(10 * MIN) expect((await h.reconcile()).suspendedForBilling).toBe(1) expect((await h.call('GET', `/v1/sandboxes/${body.sandbox.id}`)).body.sandbox.status).toBe('suspended') @@ -189,4 +189,40 @@ describe('sandbox control plane cron', () => { expect((await h.reconcile()).released).toBe(1) expect(h.vercel.boxes.size).toBe(0) }) + + it('suspends an account whose payment grace expired, keeps its disk, and resumes only after payment', async () => { + const h = await createHarness() + const a = (await h.call('POST', '/v1/sandboxes', { body: create })).body.sandbox.id + const b = (await h.call('POST', '/v1/sandboxes', { body: { ...create, workspaceId: 'ws-2' } })).body.sandbox.id + const other = (await h.call('POST', '/v1/sandboxes', { body: create, as: 'bob' })).body.sandbox.id + h.billing.suspensions = ['user:alice'] + expect((await h.reconcile()).suspendedForBilling).toBe(2) + expect((await h.call('GET', `/v1/sandboxes/${a}`)).body.sandbox.status).toBe('suspended') + expect((await h.call('GET', `/v1/sandboxes/${b}`)).body.sandbox.status).toBe('suspended') + expect((await h.call('GET', `/v1/sandboxes/${other}`, { as: 'bob' })).body.sandbox.status).toBe('running') + expect(h.vercel.boxes.size).toBe(3) + + h.billing.decision = { allow: false, reason: 'payment_grace_expired', suspendRunning: true } + expect(await h.call('POST', `/v1/sandboxes/${a}/resume`)).toEqual({ + status: 403, + body: { error: 'entitlement_denied', reason: 'payment_grace_expired' } + }) + h.billing.suspensions = [] + h.billing.decision = { allow: true, reason: null, suspendRunning: false } + expect((await h.call('POST', `/v1/sandboxes/${a}/resume`)).body.sandbox.status).toBe('running') + }) + + it('suspends the whole account when a usage report says grace expired', async () => { + const h = await createHarness() + const a = (await h.call('POST', '/v1/sandboxes', { body: create })).body.sandbox.id + h.billing.usageAnswer = { canStartSandbox: false, reason: 'payment_grace_expired', suspendRunning: true } + h.advance(10 * MIN) + expect((await h.reconcile()).suspendedForBilling).toBe(1) + expect((await h.call('GET', `/v1/sandboxes/${a}`)).body.sandbox.status).toBe('suspended') + }) + + it('refuses machine sizes billing does not sell', async () => { + const h = await createHarness() + expect((await h.call('POST', '/v1/sandboxes', { body: { ...create, machineSize: 'xlarge' } })).status).toBe(400) + }) }) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index 7d4b1523d21..b6334b814dd 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -3,7 +3,8 @@ // DEV deployment only (workers.dev, no custom domain). Deploy: // wrangler deploy -c wrangler.dev.jsonc // Secrets (wrangler secret put -c wrangler.dev.jsonc, never here): VERCEL_TOKEN, SANDBOX_DEV_JWKS, - // EGRESS_DEV_STATIC_AUTHORIZATION, and CONTROL_PLANE_TOKEN once BILLING_URL points at billing dev. + // EGRESS_DEV_STATIC_AUTHORIZATION, CONTROL_PLANE_TOKEN (same value as on + // orca-billing-dev), GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY (PKCS#8). "name": "orca-sandbox-control-dev", "main": "src/index.ts", "compatibility_date": "2026-11-01", @@ -16,6 +17,8 @@ "migrations_dir": "migrations" } ], + // Same-account Workers cannot call each other over workers.dev (error 1042); BILLING_URL names the origin. + "services": [{ "binding": "BILLING", "service": "orca-billing-dev" }], "triggers": { "crons": ["*/5 * * * *"] }, "vars": { "SANDBOX_ENV": "dev", @@ -25,8 +28,8 @@ "VERCEL_TEAM_ID": "team_c5IO9DfEYAxKEUgMxrbEtO0a", "VERCEL_PROJECT_ID": "prj_SkVtv6zJurF9dUHpfTRy4v9avwPq", "VERCEL_REGION": "iad1", - "ENTITLEMENTS_MODE": "stub", - // GOLDEN_SNAPSHOT_ID: unset until an image is built (image/build-golden-image.ts); lifecycle routes answer 503 meanwhile. + "BILLING_URL": "https://orca-billing-dev.lovecast.workers.dev", + "GOLDEN_SNAPSHOT_ID": "snap_aaJmS6etH8vyrTSxOQxPU7lx02QZ", "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" } } diff --git a/src/shared/hosted-sandbox/hosted-sandbox-wire.ts b/src/shared/hosted-sandbox/hosted-sandbox-wire.ts index 203d9a7443d..3a3d64026e8 100644 --- a/src/shared/hosted-sandbox/hosted-sandbox-wire.ts +++ b/src/shared/hosted-sandbox/hosted-sandbox-wire.ts @@ -2,7 +2,8 @@ // The Worker's tests parse its responses with these schemas, so either side drifting fails a test. import { z } from 'zod' -export const HOSTED_SANDBOX_MACHINE_SIZES = ['standard', 'large', 'xlarge'] as const +// Billing sells exactly these; larger boxes need a Vercel Enterprise plan. +export const HOSTED_SANDBOX_MACHINE_SIZES = ['standard', 'large'] as const export type HostedSandboxMachineSize = (typeof HOSTED_SANDBOX_MACHINE_SIZES)[number] export const HOSTED_SANDBOX_STATUSES = [ From 7be2308c33f2aa734c2ea6782ecf2b303cdbef4b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 00:09:15 -0800 Subject: [PATCH 09/18] fix(cloud): GitHub App setup page, dev-account billing bypass, live push results (#21) * fix(cloud): GitHub App manifest page uses a visible submit button * feat(cloud): dev operator accounts bypass billing outside prod; GitHub App push results --- .../apps/sandbox-control/src/entitlements.ts | 18 ++++++++++++++++++ cloud/apps/sandbox-control/src/index.ts | 19 ++++++++++++++----- cloud/apps/sandbox-control/wrangler.dev.jsonc | 2 +- cloud/docs/vercel-driver-design.md | 8 ++++++++ 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/cloud/apps/sandbox-control/src/entitlements.ts b/cloud/apps/sandbox-control/src/entitlements.ts index 5eb71ae2664..fe4624e0300 100644 --- a/cloud/apps/sandbox-control/src/entitlements.ts +++ b/cloud/apps/sandbox-control/src/entitlements.ts @@ -61,6 +61,24 @@ export const STOP_RUNNING_REASONS: ReadonlySet = new Set([ 'payment_grace_expired' ]) +/** Dev operator test accounts (`dev:*`, see auth.ts) never reach billing outside prod. */ +export function withDevAccountBypass(env: Env, client: EntitlementClient): EntitlementClient { + if (env.SANDBOX_ENV === 'prod') { + return client + } + return { + checkStart: (request) => + request.accountId.startsWith('dev:') + ? Promise.resolve({ allow: true, reason: null, suspendRunning: false }) + : client.checkStart(request), + reportUsage: (segment) => + segment.accountId.startsWith('dev:') + ? Promise.resolve({ canStartSandbox: true, reason: null, suspendRunning: false }) + : client.reportUsage(segment), + listSuspensions: () => client.listSuspensions() + } +} + export function createEntitlementClient( env: Env, doFetch: typeof fetch = env.BILLING ? (input, init) => env.BILLING!.fetch(input, init) : fetch diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index f34e9f270a9..b467fd39730 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -9,7 +9,12 @@ // Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy import { authenticateDesktop } from './auth' import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker } from './egress-proxy' -import { BillingUnavailableError, createEntitlementClient, type EntitlementClient } from './entitlements' +import { + BillingUnavailableError, + createEntitlementClient, + withDevAccountBypass, + type EntitlementClient +} from './entitlements' import type { Env } from './env' import { LifecycleError, @@ -99,10 +104,14 @@ function githubManifestStart(url: URL): Response { } const state = crypto.randomUUID() const escaped = JSON.stringify(manifest).replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<') + console.log(JSON.stringify({ event: 'github_manifest_start' })) + // No auto-submit: some browsers drop scripted cross-site POSTs, and GitHub still needs its own click. return html( - `

` + - `
` + - '' + '

Create the Orca Cloud (dev) GitHub App

' + + '

1. Click the button. 2. On GitHub, scroll down and click Create GitHub App for stablyai.

' + + `
` + + `` + + '
' ) } @@ -160,7 +169,7 @@ function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { projectId: env.VERCEL_PROJECT_ID!, baseUrl: env.VERCEL_API_BASE }), - entitlements: deps.entitlements ?? createEntitlementClient(env), + entitlements: deps.entitlements ?? withDevAccountBypass(env, createEntitlementClient(env)), github: deps.github ?? githubMinter(env), now: deps.now ?? Date.now, config: { diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index b6334b814dd..4a33ce81af8 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -29,7 +29,7 @@ "VERCEL_PROJECT_ID": "prj_SkVtv6zJurF9dUHpfTRy4v9avwPq", "VERCEL_REGION": "iad1", "BILLING_URL": "https://orca-billing-dev.lovecast.workers.dev", - "GOLDEN_SNAPSHOT_ID": "snap_aaJmS6etH8vyrTSxOQxPU7lx02QZ", + // GOLDEN_SNAPSHOT_ID: unset until an image is built (image/build-golden-image.ts); lifecycle routes answer 503 meanwhile. "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" } } diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index 06e6a0bb5da..9bec21d5468 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -150,6 +150,14 @@ Compiled per sandbox by `compileNetworkPolicy()`: - Per-account authorization on every route (row `account_id` must equal the caller's); ids are random. - Strict zod bodies, 16 KiB limit, no redirects on provider calls, provider error bodies dropped. +GitHub App + billing dev (2026-11-09): app "Orca Cloud (dev)" (stablyai, id 5677658, +https://github.com/apps/orca-cloud-dev; contents/pull_requests write, metadata read, no webhook) created by the +manifest flow; key only as a Worker secret. From a real sandbox via the dev Worker: private repo cloned at create +(6.6s total), real `git push` of a new branch in 1.0s with the transform-injected repo token, `gh` and Node fetch to +api.github.com through the forwardURL proxy with a minted token (200, ~1s), no token on the VM disk. Billing dev is +reached over a service binding (same-account workers.dev fetches fail with 1042): an account without a subscription +gets 403 `no_subscription`; the cron's suspensions poll runs clean. `dev:*` operator accounts bypass billing outside prod. + ## Built (mock-tested) and next wiring - (b) `src/main/environment-providers/*`, `src/main/hosted-sandbox/*` — driver, client, contract kit, recipe adapter. From 04bc4b06e718273781c27cac3d8f7fa5e464069f Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:08:28 -0800 Subject: [PATCH 10/18] feat(cloud): org/personal/repo secret store with envelope encryption and session-start injection (#23) D1 secrets table (migration 0003), AES-256-GCM per-secret DEK wrapped by SECRETS_KEK with (scope, owner, name) AAD and key-id rotation; write-only /v1/secrets CRUD with org owner/admin checks; precedence-resolved injection into orcad's env at every boot (no disk, no rebuild); optional egress placeholder swap for api.anthropic.com / api.openai.com via the forwardURL proxy. --- .../image/vm/orca-sandbox-start | 14 +- .../migrations/0003_secrets.sql | 21 + cloud/apps/sandbox-control/src/auth.ts | 24 +- .../apps/sandbox-control/src/egress-proxy.ts | 30 +- cloud/apps/sandbox-control/src/env.ts | 13 + cloud/apps/sandbox-control/src/index.ts | 102 ++++- cloud/apps/sandbox-control/src/lifecycle.ts | 53 ++- .../src/sandbox-vm-commands.ts | 18 + .../apps/sandbox-control/src/secret-egress.ts | 59 +++ .../sandbox-control/src/secrets-crypto.ts | 104 +++++ cloud/apps/sandbox-control/src/secrets.ts | 405 ++++++++++++++++++ cloud/apps/sandbox-control/src/store.ts | 5 +- cloud/apps/sandbox-control/test/harness.ts | 4 +- .../apps/sandbox-control/test/secrets.test.ts | 348 +++++++++++++++ cloud/apps/sandbox-control/wrangler.dev.jsonc | 2 +- cloud/docs/vercel-driver-design.md | 36 ++ 16 files changed, 1207 insertions(+), 31 deletions(-) create mode 100644 cloud/apps/sandbox-control/migrations/0003_secrets.sql create mode 100644 cloud/apps/sandbox-control/src/secret-egress.ts create mode 100644 cloud/apps/sandbox-control/src/secrets-crypto.ts create mode 100644 cloud/apps/sandbox-control/src/secrets.ts create mode 100644 cloud/apps/sandbox-control/test/secrets.test.ts diff --git a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start index 2c448768c89..17b7e5f2202 100755 --- a/cloud/apps/sandbox-control/image/vm/orca-sandbox-start +++ b/cloud/apps/sandbox-control/image/vm/orca-sandbox-start @@ -2,7 +2,8 @@ # Starts orcad for this boot (idempotent) and prints its recipe line: # {"schemaVersion":1,"pairingCode":"orca://pair?...","projectRoot":"/vercel/sandbox/project"} # Env from the control plane: ORCA_SANDBOX_ID, ORCA_PUBLIC_WSS, optional ORCA_REPO_URL/ORCA_REPO_REF, -# ORCA_GITHUB_PLACEHOLDER. Never prints anything else on stdout's last line. +# ORCA_GITHUB_PLACEHOLDER, ORCA_SECRET_ENV (`NAME=` lines: org/personal/repo secrets for +# orcad and its agents). Never prints anything else on stdout's last line. set -euo pipefail umask 077 ORCA_HOME=/opt/orca @@ -40,6 +41,17 @@ if [ -n "${ORCA_GITHUB_PLACEHOLDER:-}" ]; then agent_env+=("GH_TOKEN=$ORCA_GITHUB_PLACEHOLDER" "GITHUB_TOKEN=$ORCA_GITHUB_PLACEHOLDER") fi +if [ -n "${ORCA_SECRET_ENV:-}" ]; then + # Secrets live only in orcad's process env (memory): nothing under $HOME or the project, so the + # stop snapshot never carries them. Resolved per start by the control plane: no image rebuild. + while IFS= read -r line; do + name="${line%%=*}" b64="${line#*=}" # not IFS='=': read would strip base64 padding + case "$name" in '' | *[!A-Z0-9_]* | [0-9]* | ORCA_* | PATH | HOME | LD_* | DYLD_*) continue ;; esac + agent_env+=("$name=$(printf '%s' "$b64" | base64 -d)") + done < <(printf '%s\n' "$ORCA_SECRET_ENV") # a pipe, not a here-string temp file + unset ORCA_SECRET_ENV +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" \ diff --git a/cloud/apps/sandbox-control/migrations/0003_secrets.sql b/cloud/apps/sandbox-control/migrations/0003_secrets.sql new file mode 100644 index 00000000000..6d15b4064cd --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0003_secrets.sql @@ -0,0 +1,21 @@ +-- Org / personal / repo secrets, envelope-encrypted (src/secrets-crypto.ts). Plaintext never lands +-- here: `ciphertext` is AES-256-GCM under a per-row DEK, `wrapped_dek` is that DEK under the KEK +-- `kek_id` (a Worker secret). Both are bound to (scope, owner_id, name) as AAD. +CREATE TABLE secrets ( + scope TEXT NOT NULL CHECK (scope IN ('org', 'personal', 'repo')), + -- org: the org account id; personal: user:; repo: #. + owner_id TEXT NOT NULL, + name TEXT NOT NULL, + kek_id TEXT NOT NULL, + wrapped_dek TEXT NOT NULL, + ciphertext TEXT NOT NULL, + -- Last 4 characters of long values, for recognising a key in the UI; null for short values. + hint TEXT, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + updated_by TEXT NOT NULL, + PRIMARY KEY (scope, owner_id, name) +); + +-- Credential hosts whose real key the egress proxy swaps in for this session (comma list). +ALTER TABLE sandboxes ADD COLUMN egress_secret_hosts TEXT; diff --git a/cloud/apps/sandbox-control/src/auth.ts b/cloud/apps/sandbox-control/src/auth.ts index 31f88782afe..1f98caae46c 100644 --- a/cloud/apps/sandbox-control/src/auth.ts +++ b/cloud/apps/sandbox-control/src/auth.ts @@ -19,10 +19,20 @@ const ID = z 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(), purpose: z.literal('sandbox') }) -export type SandboxCaller = { userId: string; accountId: string } +export type OrgRole = 'owner' | 'admin' | 'member' +export type SandboxCaller = { + userId: string + accountId: string + orgId?: string + orgRole?: OrgRole + /** Dev operator token: every id lives under the `dev:` namespace. */ + dev?: boolean +} const keySets = new Map() @@ -68,7 +78,11 @@ async function verify( ...(issuer ? { issuer } : {}) }) const claims = ClaimsSchema.parse(payload) - return { userId: claims.sub, accountId: accountIdFor(claims) } + return { + userId: claims.sub, + accountId: accountIdFor(claims), + ...(claims.org ? { orgId: claims.org, ...(claims.org_role ? { orgRole: claims.org_role } : {}) } : {}) + } } catch { return null } @@ -95,5 +109,9 @@ export async function authenticateDesktop( } // Dev callers land in their own account namespace so they never touch a real user's sandboxes. const dev = await verify(token, devKeys.keys, env, DEV_TEST_ISSUER, now) - return dev ? { userId: dev.userId, accountId: `dev:${dev.userId}` } : null + if (!dev) { + return null + } + // Org-less dev tokens keep their historical `dev:` account; org dev tokens get `dev:org:`. + return { ...dev, accountId: dev.orgId ? `dev:org:${dev.orgId}` : `dev:${dev.userId}`, dev: true } } diff --git a/cloud/apps/sandbox-control/src/egress-proxy.ts b/cloud/apps/sandbox-control/src/egress-proxy.ts index 273ac04f750..915997e13f9 100644 --- a/cloud/apps/sandbox-control/src/egress-proxy.ts +++ b/cloud/apps/sandbox-control/src/egress-proxy.ts @@ -7,7 +7,7 @@ import type { SandboxRow } from './store' const VERCEL_OIDC_HOSTNAME = 'oidc.vercel.com' /** Hosts whose clients are Node (undici); git/curl hosts use transform rules instead. */ -export const PROXIED_HOSTS: ReadonlySet = new Set(['api.github.com']) +export const PROXIED_HOSTS: ReadonlySet = new Set(['api.github.com', 'api.anthropic.com', 'api.openai.com']) const DROP_HEADERS = [ 'vercel-forwarded-host', 'vercel-forwarded-scheme', @@ -30,12 +30,19 @@ export type CredentialBroker = { /** Until the GitHub App installation-token minter exists, proxied hosts are refused. */ export const NO_CREDENTIALS: CredentialBroker = { authorizationFor: async () => null } +/** One header the VM may send a placeholder in, and how to get the real value for it. */ +export type ProxiedRoute = { + header: string + placeholders: readonly string[] + credential: () => Promise +} + export type EgressProxyDeps = { teamId: string projectId: string findSandbox: (providerName: string) => Promise - placeholdersFor: (row: SandboxRow) => readonly string[] - credentials: CredentialBroker + /** Placeholder routes for `host` on this sandbox; empty means every request to it is refused. */ + routesFor: (row: SandboxRow, host: string) => readonly ProxiedRoute[] fetch?: typeof fetch /** Tests inject a local key set; production fetches `/.well-known/jwks`. */ keysForIssuer?: (issuer: string) => JWTVerifyGetKey @@ -103,18 +110,27 @@ export async function handleEgressProxy(request: Request, deps: EgressProxyDeps) if (!row || row.status !== 'running') { return deny('sandbox_not_running') } - if (!deps.placeholdersFor(row).includes(request.headers.get('authorization') ?? '')) { + const routes = deps.routesFor(row, host) + const route = routes + .find((candidate) => candidate.placeholders.includes(request.headers.get(candidate.header) ?? '')) + if (!route) { return deny('placeholder_missing') } - const authorization = await deps.credentials.authorizationFor(row, host) - if (!authorization) { + const credential = await route.credential() + if (!credential) { return deny('credential_unavailable') } const headers = new Headers(request.headers) for (const name of DROP_HEADERS) { headers.delete(name) } - headers.set('authorization', authorization) + for (const other of routes) { + // A second placeholder header would reach the provider as a bogus credential; drop it. + if (other !== route && other.header !== route.header && other.placeholders.includes(headers.get(other.header) ?? '')) { + headers.delete(other.header) + } + } + headers.set(route.header, credential) const hasBody = request.method !== 'GET' && request.method !== 'HEAD' const upstream = await (deps.fetch ?? fetch)(`https://${host}${path}`, { method: request.method, diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 9138af9e9e5..cfa34aec477 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -42,4 +42,17 @@ export type Env = { // GitHub App (contents/pull_requests write, metadata read). The PKCS#8 key is a secret. GITHUB_APP_ID?: string GITHUB_APP_PRIVATE_KEY?: string + + // Secret store (src/secrets.ts). SECRETS_KEK is a base64 32-byte AES key (secret); SECRETS_KEK_ID + // names it (default `k1`). After a rotation, SECRETS_KEK_RETIRED (secret, JSON {"":""}) + // keeps older rows readable. Without SECRETS_KEK the /v1/secrets routes answer 503 and sessions + // start without injected secrets. + SECRETS_KEK?: string + SECRETS_KEK_ID?: string + SECRETS_KEK_RETIRED?: string + /** + * `on`: Anthropic/OpenAI API keys reach the VM as placeholders and the egress proxy swaps the real + * key in (forwardURL path). Anything else: plain env injection. Needs EGRESS_PROXY_URL. + */ + SECRETS_EGRESS_PLACEHOLDERS?: string } diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index b467fd39730..6f013a32480 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -6,9 +6,10 @@ // POST /v1/sandboxes/:id/suspend | /resume | /rollover -> {sandbox[, connection]} // POST /v1/sandboxes/:id/lease {extendMs} -> {sandbox} // DELETE /v1/sandboxes/:id -> {sandbox} +// GET /v1/secrets?scope= | PUT, DELETE /v1/secrets/:scope/:NAME (see src/secrets.ts) // Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy import { authenticateDesktop } from './auth' -import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker } from './egress-proxy' +import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker, type ProxiedRoute } from './egress-proxy' import { BillingUnavailableError, createEntitlementClient, @@ -29,9 +30,12 @@ import { } from './lifecycle' import { createGitHubAppMinter, type GitHubAppMinter } from './github-app' import { reconcile } from './reconcile' +import { egressSecretHosts, secretPlaceholder } from './secret-egress' +import { SecretError, handleSecretsRoute, keyringFor, resolveSandboxSecrets } from './secrets' +import type { Keyring } from './secrets-crypto' import { ProviderError, type SandboxProviderApi } from './sandbox-provider' import { VmCommandError } from './sandbox-vm-commands' -import { getSandbox, getSandboxByProviderName, listAccountSandboxes } from './store' +import { getSandbox, getSandboxByProviderName, listAccountSandboxes, type SandboxRow } from './store' import { createVercelSandboxApi } from './vercel-sandbox-api' import { CreateRequestSchema, LeaseRequestSchema, readyView, sandboxView } from './wire' @@ -47,6 +51,50 @@ export type Deps = { egressKeysForIssuer?: Parameters[1]['keysForIssuer'] } +// Decrypted model keys for the egress proxy, per sandbox session, for a minute: one D1 read and +// a decrypt per session-minute instead of per model request. Isolate memory only. +const SECRET_CACHE_MS = 60_000 +const secretCache = new Map> }>() + +function sessionSecretEnv(db: D1Database, keyring: Keyring, row: SandboxRow, now: number): Promise> { + const key = `${row.id}:${row.session_id ?? ''}` + const hit = secretCache.get(key) + if (hit && now - hit.at < SECRET_CACHE_MS) { + return hit.env + } + for (const [cached, entry] of secretCache) { + if (now - entry.at >= SECRET_CACHE_MS) { + secretCache.delete(cached) + } + } + const env = resolveSandboxSecrets(db, keyring, row).then((resolved) => resolved.env) + secretCache.set(key, { at: now, env }) + env.catch(() => secretCache.delete(key)) + return env +} + +function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) => ProxiedRoute[] { + const broker = deps.credentials ?? egressCredentials(env) + return (row, host) => { + if (host === 'api.github.com') { + return [{ header: 'authorization', placeholders: githubPlaceholderHeaders(row), credential: () => broker.authorizationFor(row, host) }] + } + const entry = egressSecretHosts(row).find((candidate) => candidate.host === host) + const keyring = keyringFor(env) + if (!entry || !keyring) { + return [] + } + return Object.entries(entry.headers).map(([header, format]) => ({ + header, + placeholders: [format(secretPlaceholder(row, entry.slug))], + credential: async () => { + const value = (await sessionSecretEnv(env.SANDBOX_DB, await keyring, row, (deps.now ?? Date.now)()))[entry.secret] + return value ? format(value) : null + } + })) + } +} + const minters = new Map() function githubMinter(env: Env): GitHubAppMinter | undefined { if (!env.GITHUB_APP_ID || !env.GITHUB_APP_PRIVATE_KEY) { @@ -155,7 +203,7 @@ async function readJson(request: Request): Promise { } /** Lifecycle routes refuse to run half-configured rather than guess at Orca's provider account. */ -function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { +async function lifecycleDeps(env: Env, deps: Deps): Promise { if (!env.GOLDEN_SNAPSHOT_ID || (!deps.provider && (!env.VERCEL_TOKEN || !env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID))) { return null } @@ -171,6 +219,7 @@ function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { }), entitlements: deps.entitlements ?? withDevAccountBypass(env, createEntitlementClient(env)), github: deps.github ?? githubMinter(env), + secrets: await secretsDeps(env), now: deps.now ?? Date.now, config: { environment: env.SANDBOX_ENV, @@ -183,6 +232,20 @@ function lifecycleDeps(env: Env, deps: Deps): LifecycleDeps | null { } } +async function secretsDeps(env: Env): Promise { + const keyring = keyringFor(env) + if (!keyring) { + return undefined + } + try { + return { keyring: await keyring, egressPlaceholders: env.SECRETS_EGRESS_PLACEHOLDERS === 'on' } + } catch { + // A malformed KEK must not block sandbox starts; the secrets routes surface it as 503. + console.log(JSON.stringify({ event: 'secrets_kek_invalid' })) + return undefined + } +} + export async function handleRequest(request: Request, env: Env, deps: Deps = {}): Promise { const url = new URL(request.url) try { @@ -194,8 +257,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) teamId: env.VERCEL_TEAM_ID, projectId: env.VERCEL_PROJECT_ID, findSandbox: (name) => getSandboxByProviderName(env.SANDBOX_DB, name), - placeholdersFor: githubPlaceholderHeaders, - credentials: deps.credentials ?? egressCredentials(env), + routesFor: egressRoutes(env, deps), fetch: deps.egressFetch, keysForIssuer: deps.egressKeysForIssuer }) @@ -209,6 +271,17 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) if (url.pathname === '/v1/github/setup') { return html('Orca Cloud (dev) is installed. You can close this tab.') } + if (url.pathname === '/v1/secrets' || url.pathname.startsWith('/v1/secrets/')) { + const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)()) + if (!caller) { + return fail('unauthorized', 401) + } + const secrets = await secretsDeps(env) + if (!secrets) { + return fail('not_configured', 503) + } + return await handleSecretsRoute(request, url, env.SANDBOX_DB, secrets.keyring, caller, (deps.now ?? Date.now)(), readJson) + } const match = /^\/v1\/sandboxes(?:\/([A-Za-z0-9_]+)(?:\/(suspend|resume|lease|rollover))?)?$/.exec(url.pathname) if (!match) { return fail('not_found', 404) @@ -217,7 +290,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) if (!caller) { return fail('unauthorized', 401) } - const life = lifecycleDeps(env, deps) + const life = await lifecycleDeps(env, deps) if (!life) { return fail('not_configured', 503) } @@ -277,6 +350,9 @@ function errorResponse(error: unknown): Response { if (error instanceof SyntaxError) { return fail('invalid_json', 400) } + if (error instanceof SecretError) { + return fail(error.code, error.status, error.reason) + } if (error instanceof LifecycleError) { const status = { not_found: 404, conflict: 409, entitlement_denied: 403, provider_unavailable: 502 }[error.code] return fail(error.code, status, error.reason) @@ -296,10 +372,14 @@ export default { return handleRequest(request, env) }, async scheduled(_controller: ScheduledController, env: Env, ctx: ExecutionContext): Promise { - const life = lifecycleDeps(env, {}) - if (life) { - // Counts only; no ids, tokens or pairing material. - ctx.waitUntil(reconcile(life).then((summary) => console.log(JSON.stringify({ event: 'reconcile', ...summary })))) - } + ctx.waitUntil( + lifecycleDeps(env, {}).then(async (life) => { + if (life) { + // Counts only; no ids, tokens or pairing material. + const summary = await reconcile(life) + console.log(JSON.stringify({ event: 'reconcile', ...summary })) + } + }) + ) } } satisfies ExportedHandler diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 82ae764e05b..1754da3a6af 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -4,6 +4,9 @@ import type { EntitlementClient, MachineSize } from './entitlements' import type { GitHubAppMinter } from './github-app' import { compileNetworkPolicy, gitBasicAuthorization, type CredentialRoute } from './network-policy' import type { ProviderSandboxState, SandboxProviderApi } from './sandbox-provider' +import { applySecretPlaceholders, egressSecretHosts, secretPlaceholder } from './secret-egress' +import { resolveSandboxSecrets } from './secrets' +import type { Keyring } from './secrets-crypto' import { ORCAD_PORT, drainOrcad, startOrcad, type OrcadReady } from './sandbox-vm-commands' import { NO_SESSION, @@ -33,6 +36,8 @@ export type LifecycleDeps = { entitlements: EntitlementClient /** GitHub App installation tokens for the workspace repo; absent until the app is configured. */ github?: GitHubAppMinter + /** Secret store; absent (no SECRETS_KEK) means sessions start without injected secrets. */ + secrets?: { keyring: Keyring; egressPlaceholders: boolean } now: () => number config: { environment: string @@ -84,15 +89,28 @@ export function egressPolicy(deps: LifecycleDeps, row: SandboxRow, gitToken?: st } ] : [] - const credentials: CredentialRoute[] = deps.config.egressProxyUrl + const proxyUrl = deps.config.egressProxyUrl + const credentials: CredentialRoute[] = proxyUrl ? [ { host: 'api.github.com', header: 'authorization', placeholders: githubPlaceholderHeaders(row), via: 'forward', - forwardURL: deps.config.egressProxyUrl - } + forwardURL: proxyUrl + }, + // Model API keys swapped at egress (phase 2); each host also ends in the 403 deny rule. + ...egressSecretHosts(row).flatMap((entry) => + Object.entries(entry.headers).map( + ([header, format]): CredentialRoute => ({ + host: entry.host, + header, + placeholders: [format(secretPlaceholder(row, entry.slug))], + via: 'forward', + forwardURL: proxyUrl + }) + ) + ) ] : [] return compileNetworkPolicy({ mode: 'allowlist', credentials: [...git, ...credentials] }) @@ -133,22 +151,47 @@ async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandbox if (state.sandbox.tags.orcaSandbox !== row.id) { throw new LifecycleError('conflict', 'ownership_mismatch') } - const git = await refreshGitCredentials(deps, row, state.session.id) + 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 + gitExtraHeader: git ? `Authorization: ${gitBasicAuthorization(githubPlaceholderToken(row))}` : undefined, + secretEnv: secrets.env }) const running = await claim(deps, row, { status: 'running', last_error: null, github_token_expires_at: git?.expiresAt ?? null, + egress_secret_hosts: routed.egress_secret_hosts, ...sessionFields(deps, state) }) return { row: running, connection } } +/** + * Secrets this session starts with (resolved now, so edits apply on the next start or resume with + * no image rebuild). With egress placeholders on, model API keys become placeholders. + */ +async function sessionSecrets(deps: LifecycleDeps, row: SandboxRow): Promise<{ env: Record; hosts: string[] }> { + if (!deps.secrets) { + return { env: {}, hosts: [] } + } + const { env, skipped } = await resolveSandboxSecrets(deps.db, deps.secrets.keyring, row) + // Counts only. + console.log(JSON.stringify({ event: 'session_secrets', sandboxId: row.id, injected: Object.keys(env).length, skipped })) + if (!deps.secrets.egressPlaceholders || !deps.config.egressProxyUrl) { + return { env, hosts: [] } + } + return applySecretPlaceholders(row, env) +} + /** Mints a fresh repo token and swaps it into the live policy; null when the repo has no app install. */ export async function refreshGitCredentials( deps: LifecycleDeps, diff --git a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts index 240af46c6a5..adf8cebfdb4 100644 --- a/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts +++ b/cloud/apps/sandbox-control/src/sandbox-vm-commands.ts @@ -35,6 +35,21 @@ export type StartInputs = { githubPlaceholder?: string /** git `http.https://github.com/.extraheader` carrying the placeholder, so every git request is claimable. */ gitExtraHeader?: string + /** Secrets for orcad's (and so every agent's) process env. Never written to disk in the VM. */ + secretEnv?: Record +} + +/** `NAME=` lines, so values with newlines or quotes survive bash intact. */ +export function encodeSecretEnv(env: Record): string { + return Object.entries(env) + .map(([name, value]) => { + let binary = '' + for (const byte of new TextEncoder().encode(value)) { + binary += String.fromCharCode(byte) + } + return `${name}=${btoa(binary)}` + }) + .join('\n') } /** Starts orcad (idempotent per boot) and returns its pairing offer; pairing codes are never logged. */ @@ -63,6 +78,9 @@ export async function startOrcad( if (inputs.githubPlaceholder) { env.ORCA_GITHUB_PLACEHOLDER = inputs.githubPlaceholder } + if (inputs.secretEnv && Object.keys(inputs.secretEnv).length > 0) { + env.ORCA_SECRET_ENV = encodeSecretEnv(inputs.secretEnv) + } const result = await provider.runCommand(state.session.id, { command: START_COMMAND, env, diff --git a/cloud/apps/sandbox-control/src/secret-egress.ts b/cloud/apps/sandbox-control/src/secret-egress.ts new file mode 100644 index 00000000000..5a4e95a6da7 --- /dev/null +++ b/cloud/apps/sandbox-control/src/secret-egress.ts @@ -0,0 +1,59 @@ +// Phase-2 model API keys: the VM's env carries a placeholder, and the egress proxy swaps in the real +// key only on the provider's own API host. Node clients (Claude Code, Codex) hang under Vercel +// transform rules, so these hosts always take the forwardURL path through /v1/egress/proxy, the +// same path api.github.com uses. Placeholders are not secrets: the proxy authenticates the +// sandbox by Vercel's OIDC token, the placeholder only marks which header to swap. +import type { SandboxRow } from './store' + +export type SecretEgressHost = { + host: string + /** Secret whose value is swapped in. */ + secret: string + slug: string + /** Header shapes the provider's clients send the key in, keyed by lowercase header name. */ + headers: Record string> +} + +export const SECRET_EGRESS_HOSTS: readonly SecretEgressHost[] = [ + { + host: 'api.anthropic.com', + secret: 'ANTHROPIC_API_KEY', + slug: 'anthropic', + headers: { 'x-api-key': (value) => value, authorization: (value) => `Bearer ${value}` } + }, + { + host: 'api.openai.com', + secret: 'OPENAI_API_KEY', + slug: 'openai', + headers: { authorization: (value) => `Bearer ${value}` } + } +] + +export function secretPlaceholder(row: Pick, slug: string): string { + return `orca-ph-${slug}-${row.id}` +} + +/** Hosts recorded on the row at boot as placeholder-routed for this session. */ +export function egressSecretHosts(row: Pick): SecretEgressHost[] { + const hosts = new Set((row.egress_secret_hosts ?? '').split(',').filter(Boolean)) + return SECRET_EGRESS_HOSTS.filter((entry) => hosts.has(entry.host)) +} + +/** + * Replaces model API keys with placeholders in a session's env. Returns the hosts the policy and + * proxy must now route (and deny without the placeholder). + */ +export function applySecretPlaceholders( + row: Pick, + env: Record +): { env: Record; hosts: string[] } { + const next = { ...env } + const hosts: string[] = [] + for (const entry of SECRET_EGRESS_HOSTS) { + if (next[entry.secret]) { + next[entry.secret] = secretPlaceholder(row, entry.slug) + hosts.push(entry.host) + } + } + return { env: next, hosts } +} diff --git a/cloud/apps/sandbox-control/src/secrets-crypto.ts b/cloud/apps/sandbox-control/src/secrets-crypto.ts new file mode 100644 index 00000000000..8c3c7fdc671 --- /dev/null +++ b/cloud/apps/sandbox-control/src/secrets-crypto.ts @@ -0,0 +1,104 @@ +// Envelope encryption for the secret store. Each value gets a fresh AES-256-GCM data key (DEK); +// the DEK is wrapped by the key-encryption key (KEK), which exists only as a Worker secret. Both +// layers authenticate the row's (scope, owner, name) as AAD, so a ciphertext copied onto another +// row (another org, another name) fails to open instead of decrypting under the wrong identity. + +export type SecretBinding = { scope: string; ownerId: string; name: string } +export type SealedSecret = { kekId: string; wrappedDek: string; ciphertext: string } + +export type Keyring = { + activeId: string + seal(binding: SecretBinding, value: string): Promise + /** Throws on a tampered row, a swapped binding or an unknown key id. */ + open(binding: SecretBinding, sealed: SealedSecret): Promise +} + +const IV_BYTES = 12 +const KEY_ID = /^[A-Za-z0-9_-]{1,32}$/ + +function toBase64(bytes: Uint8Array): string { + let binary = '' + for (const byte of bytes) { + binary += String.fromCharCode(byte) + } + return btoa(binary) +} + +function fromBase64(value: string): Uint8Array { + const binary = atob(value) + const bytes = new Uint8Array(binary.length) + for (let i = 0; i < binary.length; i++) { + bytes[i] = binary.charCodeAt(i) + } + return bytes +} + +function utf8(value: string): Uint8Array { + return new Uint8Array(new TextEncoder().encode(value)) +} + +function aad(binding: SecretBinding, layer: 'dek' | 'value', kekId: string): Uint8Array { + // JSON keeps field boundaries unambiguous (a name can't bleed into the owner id). + return utf8(JSON.stringify(['orca-secret-v1', layer, kekId, binding.scope, binding.ownerId, binding.name])) +} + +async function encrypt(key: CryptoKey, plaintext: Uint8Array, additionalData: Uint8Array) { + const iv = crypto.getRandomValues(new Uint8Array(IV_BYTES)) + const ct = new Uint8Array(await crypto.subtle.encrypt({ name: 'AES-GCM', iv, additionalData }, key, plaintext)) + const out = new Uint8Array(IV_BYTES + ct.length) + out.set(iv) + out.set(ct, IV_BYTES) + return toBase64(out) +} + +async function decrypt(key: CryptoKey, sealed: string, additionalData: Uint8Array) { + const bytes = fromBase64(sealed) + const iv = bytes.slice(0, IV_BYTES) + return new Uint8Array(await crypto.subtle.decrypt({ name: 'AES-GCM', iv, additionalData }, key, bytes.slice(IV_BYTES))) +} + +function importAes(raw: Uint8Array): Promise { + return crypto.subtle.importKey('raw', raw, { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']) +} + +/** + * `active` is the base64 32-byte KEK new rows are sealed with; `retired` maps older key ids to + * their KEKs so rows written before a rotation still open (re-sealed on their next write). + */ +export async function createKeyring(active: { id: string; key: string }, retired: Record = {}): Promise { + const keys = new Map() + for (const [id, b64] of [[active.id, active.key] as const, ...Object.entries(retired)]) { + if (!KEY_ID.test(id)) { + throw new Error('invalid KEK id') + } + const raw = fromBase64(b64) + if (raw.length !== 32) { + throw new Error('KEK must be 32 bytes') + } + keys.set(id, await importAes(raw)) + } + const kek = (id: string) => { + const key = keys.get(id) + if (!key) { + throw new Error('unknown KEK id') + } + return key + } + return { + activeId: active.id, + async seal(binding, value) { + const dekRaw = crypto.getRandomValues(new Uint8Array(32)) + const dek = await importAes(dekRaw) + const ciphertext = await encrypt(dek, utf8(value), aad(binding, 'value', active.id)) + const wrappedDek = await encrypt(kek(active.id), dekRaw, aad(binding, 'dek', active.id)) + dekRaw.fill(0) + return { kekId: active.id, wrappedDek, ciphertext } + }, + async open(binding, sealed) { + const dekRaw = await decrypt(kek(sealed.kekId), sealed.wrappedDek, aad(binding, 'dek', sealed.kekId)) + const dek = await importAes(dekRaw) + dekRaw.fill(0) + return new TextDecoder().decode(await decrypt(dek, sealed.ciphertext, aad(binding, 'value', sealed.kekId))) + } + } +} diff --git a/cloud/apps/sandbox-control/src/secrets.ts b/cloud/apps/sandbox-control/src/secrets.ts new file mode 100644 index 00000000000..ef491cd2198 --- /dev/null +++ b/cloud/apps/sandbox-control/src/secrets.ts @@ -0,0 +1,405 @@ +// Org / personal / repo secret store and its injection into sandbox sessions. +// GET /v1/secrets?scope=org|personal|repo[&repo=owner/name] -> {secrets: [metadata]} +// PUT /v1/secrets/:scope/:NAME[?repo=owner/name] {value} -> {secret: metadata} +// DELETE /v1/secrets/:scope/:NAME[?repo=owner/name] -> {deleted: true} +// Values are write-only: no route returns them, only a last-4 hint for long values. Org secrets +// (and repo secrets of an org account) need owner/admin of that org to write; any member may list +// names. Personal secrets belong to the user alone. Values are never logged. +import { z } from 'zod' +import type { SandboxCaller } from './auth' +import type { Env } from './env' +import { createKeyring, type Keyring, type SecretBinding } from './secrets-crypto' +import type { SandboxRow } from './store' + +export type SecretScope = 'org' | 'personal' | 'repo' +export type SecretKind = 'env' | 'agent_key' + +const NAME = /^[A-Z_][A-Z0-9_]*$/ +const MAX_NAME_LENGTH = 128 +export const MAX_VALUE_BYTES = 8 * 1024 +export const MAX_SECRETS_PER_OWNER = 100 +/** Upper bound on what one session start carries (it rides the VM command's env). */ +export const MAX_INJECTED_BYTES = 128 * 1024 + +/** Keys agent CLIs read. For these an org-shared value beats a personal one (Conductor parity). */ +export const AGENT_KEY_NAMES: ReadonlySet = new Set([ + 'ANTHROPIC_API_KEY', + 'ANTHROPIC_AUTH_TOKEN', + 'OPENAI_API_KEY', + 'GEMINI_API_KEY', + 'GOOGLE_API_KEY', + 'CURSOR_API_KEY', + 'AWS_ACCESS_KEY_ID', + 'AWS_SECRET_ACCESS_KEY', + 'AWS_SESSION_TOKEN', + 'AWS_REGION', + 'AWS_BEARER_TOKEN_BEDROCK', + 'CLAUDE_CODE_USE_BEDROCK', + 'CLAUDE_CODE_USE_VERTEX', + 'ANTHROPIC_VERTEX_PROJECT_ID', + 'CLOUD_ML_REGION', + 'GOOGLE_CLOUD_PROJECT' +]) + +/** Names that would break or hijack the VM's own processes, or that Orca manages itself. */ +const RESERVED_NAMES: ReadonlySet = new Set([ + 'PATH', 'HOME', 'USER', 'LOGNAME', 'SHELL', 'PWD', 'OLDPWD', 'LANG', 'TERM', 'TMPDIR', 'IFS', 'ENV', + 'BASH_ENV', 'PS4', 'PROMPT_COMMAND', 'SHELLOPTS', 'BASHOPTS', 'NODE_OPTIONS', 'NODE_PATH', + 'NODE_EXTRA_CA_CERTS', 'NODE_TLS_REJECT_UNAUTHORIZED', 'SSL_CERT_FILE', 'SSL_CERT_DIR', 'CURL_CA_BUNDLE', + 'REQUESTS_CA_BUNDLE', 'HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY', 'PYTHONPATH', 'PYTHONSTARTUP', + 'PERL5OPT', 'RUBYOPT', 'JAVA_TOOL_OPTIONS', 'GIT_SSH', 'GIT_SSH_COMMAND', 'GIT_ASKPASS', 'GIT_EXEC_PATH', + 'GIT_PROXY_COMMAND', 'SSH_AUTH_SOCK', 'GH_TOKEN', 'GITHUB_TOKEN', + // Subscription credentials are never stored (Anthropic policy forbids third parties holding them). + 'CLAUDE_CODE_OAUTH_TOKEN' +]) +const RESERVED_PREFIXES = ['LD_', 'DYLD_', 'ORCA_', 'VERCEL_', 'BASH_FUNC_', 'GIT_CONFIG'] + +export class SecretError extends Error { + constructor( + readonly status: 400 | 403 | 404 | 409, + readonly code: string, + readonly reason?: string + ) { + super(reason ? `${code}:${reason}` : code) + this.name = 'SecretError' + } +} + +export function kindOf(name: string): SecretKind { + return AGENT_KEY_NAMES.has(name) ? 'agent_key' : 'env' +} + +export function checkName(name: string): void { + if (name.length > MAX_NAME_LENGTH || !NAME.test(name)) { + throw new SecretError(400, 'invalid_request', 'invalid_name') + } + if (name === 'CLAUDE_CODE_OAUTH_TOKEN') { + throw new SecretError(400, 'invalid_request', 'subscription_token_refused') + } + if (RESERVED_NAMES.has(name) || RESERVED_PREFIXES.some((prefix) => name.startsWith(prefix))) { + throw new SecretError(400, 'invalid_request', 'reserved_name') + } +} + +const JWT_SHAPE = /^[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}$/ + +/** Refuses Claude.ai / ChatGPT subscription credentials; API keys and ordinary env values pass. */ +export function checkValue(name: string, value: string): void { + if (value.length === 0 || value.includes('\0')) { + throw new SecretError(400, 'invalid_request', 'invalid_value') + } + if (new TextEncoder().encode(value).length > MAX_VALUE_BYTES) { + throw new SecretError(400, 'invalid_request', 'value_too_large') + } + const trimmed = value.trim() + if (trimmed.startsWith('sk-ant-oat')) { + throw new SecretError(400, 'invalid_request', 'subscription_token_refused') + } + // A ChatGPT/Claude.ai session is a JWT; real Anthropic/OpenAI API keys never are. + if ((name === 'ANTHROPIC_API_KEY' || name === 'OPENAI_API_KEY') && JWT_SHAPE.test(trimmed)) { + throw new SecretError(400, 'invalid_request', 'subscription_token_refused') + } +} + +function hintFor(value: string): string | null { + return value.length >= 16 ? value.slice(-4) : null +} + +const REPO_PART = /^[A-Za-z0-9_.-]{1,100}$/ + +/** `owner/name`, `github.com/owner/name` or an https GitHub URL -> `github.com/owner/name` (lowercase). */ +export function normalizeRepo(input: string | null | undefined): string | null { + if (!input) { + return null + } + const stripped = input + .trim() + .replace(/^https:\/\//i, '') + .replace(/^github\.com\//i, '') + .replace(/\.git$/i, '') + .replace(/\/+$/, '') + const [owner, name, ...rest] = stripped.split('/') + if (rest.length > 0 || !owner || !name || !REPO_PART.test(owner) || !REPO_PART.test(name)) { + return null + } + return `github.com/${owner}/${name}`.toLowerCase() +} + +// Owner ids. Dev operator callers stay inside the `dev:` namespace like their sandboxes do. +const ORG_ACCOUNT = /^(dev:)?org:/ +function personalOwner(userId: string, dev: boolean): string { + return dev ? `dev:user:${userId}` : `user:${userId}` +} +function repoOwner(accountId: string, repo: string): string { + return `${accountId}#${repo}` +} + +/** The owners whose secrets a sandbox session receives. */ +export function ownersForSandbox(row: Pick) { + const repo = normalizeRepo(row.repo_url) + return { + org: ORG_ACCOUNT.test(row.account_id) ? row.account_id : null, + personal: personalOwner(row.user_id, row.account_id.startsWith('dev:')), + repo: repo ? repoOwner(row.account_id, repo) : null + } +} + +function isOrgAdmin(caller: SandboxCaller): boolean { + return caller.orgRole === 'owner' || caller.orgRole === 'admin' +} + +/** Resolves the owner id for `scope` and checks the caller may read (or write) it. */ +export function ownerFor(caller: SandboxCaller, scope: SecretScope, repoParam: string | null, write: boolean): string { + switch (scope) { + case 'personal': + return personalOwner(caller.userId, caller.dev === true) + case 'org': + if (!caller.orgId) { + throw new SecretError(403, 'forbidden', 'no_org') + } + if (write && !isOrgAdmin(caller)) { + throw new SecretError(403, 'forbidden', 'org_admin_required') + } + return caller.accountId + case 'repo': { + const repo = normalizeRepo(repoParam) + if (!repo) { + throw new SecretError(400, 'invalid_request', 'repo_required') + } + // Repo secrets belong to the account that owns the workspace: the org (admins write) or the user. + if (write && caller.orgId && !isOrgAdmin(caller)) { + throw new SecretError(403, 'forbidden', 'org_admin_required') + } + return repoOwner(caller.accountId, repo) + } + } +} + +type SecretRecord = { + scope: SecretScope + owner_id: string + name: string + kek_id: string + wrapped_dek: string + ciphertext: string + hint: string | null + created_at: number + updated_at: number + updated_by: string +} + +export type SecretView = { + scope: SecretScope + name: string + kind: SecretKind + hint: string | null + updatedAt: number + updatedBy: string + repo?: string +} + +function view(record: Pick): SecretView { + const repo = record.scope === 'repo' ? record.owner_id.slice(record.owner_id.indexOf('#') + 1) : undefined + return { + scope: record.scope, + name: record.name, + kind: kindOf(record.name), + hint: record.hint, + updatedAt: record.updated_at, + updatedBy: record.updated_by, + ...(repo ? { repo } : {}) + } +} + +export async function listSecrets(db: D1Database, scope: SecretScope, ownerId: string): Promise { + const { results } = await db + .prepare( + 'SELECT scope, owner_id, name, hint, updated_at, updated_by FROM secrets WHERE scope = ? AND owner_id = ? ORDER BY name' + ) + .bind(scope, ownerId) + .all() + return results.map(view) +} + +export async function putSecret( + db: D1Database, + keyring: Keyring, + input: { scope: SecretScope; ownerId: string; name: string; value: string; updatedBy: string }, + now: number +): Promise { + checkName(input.name) + checkValue(input.name, input.value) + const existing = await db + .prepare('SELECT name FROM secrets WHERE scope = ? AND owner_id = ? AND name = ?') + .bind(input.scope, input.ownerId, input.name) + .first() + if (!existing) { + const count = await db + .prepare('SELECT COUNT(*) AS n FROM secrets WHERE scope = ? AND owner_id = ?') + .bind(input.scope, input.ownerId) + .first<{ n: number }>() + if ((count?.n ?? 0) >= MAX_SECRETS_PER_OWNER) { + throw new SecretError(409, 'conflict', 'secret_limit') + } + } + const sealed = await keyring.seal({ scope: input.scope, ownerId: input.ownerId, name: input.name }, input.value) + const hint = hintFor(input.value) + await db + .prepare( + `INSERT INTO secrets (scope, owner_id, name, kek_id, wrapped_dek, ciphertext, hint, created_at, updated_at, updated_by) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT (scope, owner_id, name) DO UPDATE SET kek_id = excluded.kek_id, wrapped_dek = excluded.wrapped_dek, + ciphertext = excluded.ciphertext, hint = excluded.hint, updated_at = excluded.updated_at, + updated_by = excluded.updated_by` + ) + .bind(input.scope, input.ownerId, input.name, sealed.kekId, sealed.wrappedDek, sealed.ciphertext, hint, now, now, input.updatedBy) + .run() + return view({ scope: input.scope, owner_id: input.ownerId, name: input.name, hint, updated_at: now, updated_by: input.updatedBy }) +} + +export async function deleteSecret(db: D1Database, scope: SecretScope, ownerId: string, name: string): Promise { + const result = await db + .prepare('DELETE FROM secrets WHERE scope = ? AND owner_id = ? AND name = ?') + .bind(scope, ownerId, name) + .run() + return result.meta.changes === 1 +} + +/** Higher wins. Env: repo and personal beat org. Agent keys: org-shared beats repo beats personal. */ +const RANK: Record> = { + env: { repo: 3, personal: 2, org: 1 }, + agent_key: { org: 3, repo: 2, personal: 1 } +} + +export type ResolvedSecrets = { env: Record; skipped: number } + +/** Decrypts every secret the session should see and applies precedence; unreadable rows are skipped. */ +export async function resolveSandboxSecrets( + db: D1Database, + keyring: Keyring, + row: Pick +): Promise { + const owners = ownersForSandbox(row) + const pairs: [SecretScope, string][] = [['personal', owners.personal]] + if (owners.org) { + pairs.push(['org', owners.org]) + } + if (owners.repo) { + pairs.push(['repo', owners.repo]) + } + const where = pairs.map(() => '(scope = ? AND owner_id = ?)').join(' OR ') + const { results } = await db + .prepare(`SELECT * FROM secrets WHERE ${where}`) + .bind(...pairs.flat()) + .all() + const winners = new Map() + for (const record of results) { + const current = winners.get(record.name) + const rank = RANK[kindOf(record.name)] + if (!current || rank[record.scope] > rank[current.scope]) { + winners.set(record.name, record) + } + } + const env: Record = {} + let bytes = 0 + let skipped = 0 + for (const record of [...winners.values()].sort((a, b) => a.name.localeCompare(b.name))) { + const binding: SecretBinding = { scope: record.scope, ownerId: record.owner_id, name: record.name } + let value: string + try { + value = await keyring.open(binding, { + kekId: record.kek_id, + wrappedDek: record.wrapped_dek, + ciphertext: record.ciphertext + }) + } catch { + skipped++ + // Name and scope only: never the value or ciphertext. + console.log(JSON.stringify({ event: 'secret_unreadable', sandboxId: row.id, scope: record.scope, name: record.name })) + continue + } + bytes += record.name.length + value.length + if (bytes > MAX_INJECTED_BYTES) { + skipped++ + console.log(JSON.stringify({ event: 'secret_injection_capped', sandboxId: row.id, name: record.name })) + continue + } + env[record.name] = value + } + return { env, skipped } +} + +let cachedKeyring: { source: string; keyring: Promise } | null = null + +/** The Worker's keyring, or null when SECRETS_KEK is not configured. */ +export function keyringFor(env: Env): Promise | null { + if (!env.SECRETS_KEK) { + return null + } + const source = `${env.SECRETS_KEK_ID ?? 'k1'}\n${env.SECRETS_KEK}\n${env.SECRETS_KEK_RETIRED ?? ''}` + if (cachedKeyring?.source !== source) { + const retired = env.SECRETS_KEK_RETIRED ? (JSON.parse(env.SECRETS_KEK_RETIRED) as Record) : {} + cachedKeyring = { + source, + keyring: createKeyring({ id: env.SECRETS_KEK_ID ?? 'k1', key: env.SECRETS_KEK }, retired) + } + } + return cachedKeyring.keyring +} + +const ScopeSchema = z.enum(['org', 'personal', 'repo']) +const PutSchema = z.object({ value: z.string() }).strict() + +/** Routes under /v1/secrets for an authenticated desktop caller. */ +export async function handleSecretsRoute( + request: Request, + url: URL, + db: D1Database, + keyring: Keyring, + caller: SandboxCaller, + now: number, + readJson: (request: Request) => Promise +): Promise { + const json = (body: unknown, status = 200) => Response.json(body, { status, headers: { 'cache-control': 'no-store' } }) + const match = /^\/v1\/secrets(?:\/([a-z]+)\/([^/]+))?$/.exec(url.pathname) + if (!match) { + return json({ error: 'not_found' }, 404) + } + const [, scopeSegment, name] = match + const repoParam = url.searchParams.get('repo') + if (!scopeSegment) { + if (request.method !== 'GET') { + return json({ error: 'not_found' }, 404) + } + const scope = ScopeSchema.safeParse(url.searchParams.get('scope') ?? 'personal') + if (!scope.success) { + return json({ error: 'invalid_request', reason: 'invalid_scope' }, 400) + } + const owner = ownerFor(caller, scope.data, repoParam, false) + return json({ secrets: await listSecrets(db, scope.data, owner) }) + } + const scope = ScopeSchema.safeParse(scopeSegment) + if (!scope.success || !name) { + return json({ error: 'not_found' }, 404) + } + if (request.method === 'PUT') { + const owner = ownerFor(caller, scope.data, repoParam, true) + const body = PutSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + const secret = await putSecret(db, keyring, { scope: scope.data, ownerId: owner, name, value: body.data.value, updatedBy: caller.userId }, now) + console.log(JSON.stringify({ event: 'secret_written', scope: scope.data, name, by: caller.userId })) + return json({ secret }) + } + if (request.method === 'DELETE') { + const owner = ownerFor(caller, scope.data, repoParam, true) + checkName(name) + if (!(await deleteSecret(db, scope.data, owner, name))) { + return json({ error: 'not_found' }, 404) + } + console.log(JSON.stringify({ event: 'secret_deleted', scope: scope.data, name, by: caller.userId })) + return json({ deleted: true }) + } + return json({ error: 'not_found' }, 404) +} diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index e982da936f1..7e13b8eed64 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -34,6 +34,8 @@ export type SandboxRow = { destroyed_at: number | null repo_url: string | null github_token_expires_at: number | null + /** Comma list of model API hosts whose key the egress proxy swaps in this session. */ + egress_secret_hosts: string | null } export type SessionFields = Pick< @@ -124,7 +126,8 @@ export async function listByStatus(db: D1Database, statuses: readonly SandboxSta type Patch = Partial> const PATCHABLE = new Set([ 'status', 'session_id', 'session_started_at', 'session_deadline_at', 'lease_expires_at', - 'usage_settled_at', 'last_error', 'destroyed_at', 'github_token_expires_at' + 'usage_settled_at', 'last_error', 'destroyed_at', 'github_token_expires_at', + 'egress_secret_hosts' ]) /** Applies `patch` only if nobody moved the row since `expected` was read; null on a lost race. */ diff --git a/cloud/apps/sandbox-control/test/harness.ts b/cloud/apps/sandbox-control/test/harness.ts index 8911feecb27..a5752b0f16d 100644 --- a/cloud/apps/sandbox-control/test/harness.ts +++ b/cloud/apps/sandbox-control/test/harness.ts @@ -52,9 +52,9 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par } const deps: Deps = { provider: vercel, entitlements, now: clock, ...extraDeps } - async function token(sub: string, org?: string, purpose = 'sandbox') { + async function token(sub: string, org?: string, purpose = 'sandbox', extra: Record = {}) { const iat = Math.floor(now / 1000) - return new SignJWT({ sub, ...(org ? { org } : {}), purpose }) + return new SignJWT({ sub, ...(org ? { org } : {}), purpose, ...extra }) .setProtectedHeader({ alg: 'ES256', kid: 'k1' }) .setIssuer('https://login.onorca.dev') .setAudience('orca-sandbox') diff --git a/cloud/apps/sandbox-control/test/secrets.test.ts b/cloud/apps/sandbox-control/test/secrets.test.ts new file mode 100644 index 00000000000..7e5b9ad975b --- /dev/null +++ b/cloud/apps/sandbox-control/test/secrets.test.ts @@ -0,0 +1,348 @@ +import { SignJWT, createLocalJWKSet, exportJWK, generateKeyPair } from 'jose' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createKeyring } from '../src/secrets-crypto' +import { MAX_SECRETS_PER_OWNER, MAX_VALUE_BYTES } from '../src/secrets' +import { START_COMMAND } from '../src/sandbox-vm-commands' +import { createHarness } from './harness' + +const KEK = Buffer.alloc(32, 7).toString('base64') +const OLD_KEK = Buffer.alloc(32, 3).toString('base64') +const PROXY_URL = 'https://sandbox.test/v1/egress/proxy' +// Fake values only; the log-redaction check looks for every one of them. +const ORG_KEY = 'sk-ant-api03-ORGSHARED-fake-0000-aaaa' +const PERSONAL_KEY = 'sk-ant-api03-PERSONAL-fake-1111-bbbb' +const OPENAI_KEY = 'sk-proj-fake-openai-2222-cccc' +const ALL_VALUES = [ORG_KEY, PERSONAL_KEY, OPENAI_KEY, 'org-env-value', 'personal-env-value', 'repo-env-value'] + +let logs: string[] = [] +beforeEach(() => { + logs = [] + for (const method of ['log', 'info', 'warn', 'error', 'debug'] as const) { + vi.spyOn(console, method).mockImplementation((...args: unknown[]) => { + logs.push(args.map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))).join(' ')) + }) + } +}) +afterEach(() => { + // No secret value ever reaches a log line, whatever the test did. + for (const value of ALL_VALUES) { + expect(logs.join('\n')).not.toContain(value) + } + vi.restoreAllMocks() +}) + +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 } : {}) + 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 }) + } + return { h, as, put } +} + +function startEnv(h: Awaited>, index = -1): Record { + const starts = h.vercel.commands.filter((c) => c.command === START_COMMAND) + const encoded = starts.at(index)!.env.ORCA_SECRET_ENV ?? '' + return Object.fromEntries( + encoded + .split('\n') + .filter(Boolean) + .map((line) => { + const at = line.indexOf('=') + return [line.slice(0, at), Buffer.from(line.slice(at + 1), 'base64').toString('utf8')] + }) + ) +} + +describe('secret envelope encryption', () => { + const binding = { scope: 'org', ownerId: 'org:acme', name: 'ANTHROPIC_API_KEY' } + + it('round-trips under a per-secret DEK and refuses tampering or a swapped row identity', async () => { + const keyring = await createKeyring({ id: 'k1', key: KEK }) + const sealed = await keyring.seal(binding, ORG_KEY) + expect(sealed.ciphertext).not.toContain('ORGSHARED') + expect(await keyring.open(binding, sealed)).toBe(ORG_KEY) + // Fresh DEK and IV each time. + expect((await keyring.seal(binding, ORG_KEY)).wrappedDek).not.toBe(sealed.wrappedDek) + + const flip = (b64: string) => { + const bytes = Buffer.from(b64, 'base64') + bytes[bytes.length - 1]! ^= 1 + return bytes.toString('base64') + } + await expect(keyring.open(binding, { ...sealed, ciphertext: flip(sealed.ciphertext) })).rejects.toThrow() + await expect(keyring.open(binding, { ...sealed, wrappedDek: flip(sealed.wrappedDek) })).rejects.toThrow() + await expect(keyring.open({ ...binding, ownerId: 'org:evil' }, sealed)).rejects.toThrow() + await expect(keyring.open({ ...binding, name: 'OPENAI_API_KEY' }, sealed)).rejects.toThrow() + await expect(keyring.open({ ...binding, scope: 'personal' }, sealed)).rejects.toThrow() + }) + + it('opens rows sealed under a retired KEK after rotation, and rejects unknown key ids', async () => { + const old = await createKeyring({ id: 'k0', key: OLD_KEK }) + const sealed = await old.seal(binding, ORG_KEY) + const rotated = await createKeyring({ id: 'k1', key: KEK }, { k0: OLD_KEK }) + expect(await rotated.open(binding, sealed)).toBe(ORG_KEY) + expect((await rotated.seal(binding, ORG_KEY)).kekId).toBe('k1') + const fresh = await createKeyring({ id: 'k1', key: KEK }) + await expect(fresh.open(binding, sealed)).rejects.toThrow('unknown KEK id') + await expect(createKeyring({ id: 'k1', key: Buffer.alloc(16).toString('base64') })).rejects.toThrow() + }) +}) + +describe('secrets API', () => { + it('stores write-only values: responses and D1 carry metadata and a last-4 hint, never the value', async () => { + const { h, as, put } = await setup() + const alice = await as('alice') + const res = await put('personal', 'ANTHROPIC_API_KEY', PERSONAL_KEY, alice) + expect(res).toEqual({ + status: 200, + body: { secret: { scope: 'personal', name: 'ANTHROPIC_API_KEY', kind: 'agent_key', hint: 'bbbb', updatedAt: h.now(), updatedBy: 'alice' } } + }) + await put('personal', 'SHORT', 'abc', alice) + const listed = await h.call('GET', '/v1/secrets?scope=personal', { bearer: alice }) + expect(listed.body.secrets.map((s: { name: string; hint: string | null; kind: string }) => [s.name, s.hint, s.kind])).toEqual([ + ['ANTHROPIC_API_KEY', 'bbbb', 'agent_key'], + ['SHORT', null, 'env'] + ]) + expect(JSON.stringify(listed.body)).not.toContain(PERSONAL_KEY) + const rows = JSON.stringify(h.raw.prepare('SELECT * FROM secrets').all()) + expect(rows).not.toContain(PERSONAL_KEY) + expect(rows).not.toContain('PERSONAL-fake') + // Another user sees none of it. + expect((await h.call('GET', '/v1/secrets?scope=personal', { as: 'bob' })).body.secrets).toEqual([]) + expect((await h.call('DELETE', '/v1/secrets/personal/SHORT', { as: 'bob' })).status).toBe(404) + expect((await h.call('DELETE', '/v1/secrets/personal/SHORT', { bearer: alice })).body).toEqual({ deleted: true }) + }) + + it('lets only org owners/admins write org secrets; members can list names', async () => { + const { h, as, put } = await setup() + const member = await as('mia', 'acme', 'member') + const noRole = await as('nora', 'acme') + const admin = await as('ada', 'acme', 'admin') + const owner = await as('otto', 'acme', 'owner') + expect(await put('org', 'ANTHROPIC_API_KEY', ORG_KEY, member)).toEqual({ + status: 403, + body: { error: 'forbidden', reason: 'org_admin_required' } + }) + expect((await put('org', 'ANTHROPIC_API_KEY', ORG_KEY, noRole)).status).toBe(403) + expect((await put('org', 'ANTHROPIC_API_KEY', ORG_KEY, admin)).status).toBe(200) + expect((await put('org', 'FOO', 'org-env-value', owner)).status).toBe(200) + expect((await h.call('DELETE', '/v1/secrets/org/FOO', { bearer: member })).status).toBe(403) + const listed = await h.call('GET', '/v1/secrets?scope=org', { bearer: member }) + expect(listed.body.secrets.map((s: { name: string }) => s.name)).toEqual(['ANTHROPIC_API_KEY', 'FOO']) + // No org on the token: no org scope at all; another org sees nothing of acme's. + expect((await h.call('GET', '/v1/secrets?scope=org', { as: 'alice' })).body).toEqual({ error: 'forbidden', reason: 'no_org' }) + expect((await h.call('GET', '/v1/secrets?scope=org', { bearer: await as('eve', 'other', 'owner') })).body.secrets).toEqual([]) + }) + + it('authorizes repo secrets by the owning account: org admins, or the user for a personal account', async () => { + const { as, put } = await setup() + expect((await put('repo', 'FOO', 'repo-env-value', await as('mia', 'acme', 'member'), 'acme/app')).status).toBe(403) + expect((await put('repo', 'FOO', 'repo-env-value', await as('ada', 'acme', 'admin'), 'acme/app')).status).toBe(200) + expect((await put('repo', 'FOO', 'repo-env-value', await as('alice'), 'https://github.com/alice/site.git')).body.secret.repo).toBe( + 'github.com/alice/site' + ) + expect((await put('repo', 'FOO', 'x', await as('alice'))).body).toEqual({ error: 'invalid_request', reason: 'repo_required' }) + }) + + it('validates names and refuses subscription credentials', async () => { + const { h, as, put } = await setup() + const alice = await as('alice') + const reason = async (name: string, value = 'value') => (await put('personal', name, value, alice)).body.reason + expect(await reason('lower_case')).toBe('invalid_name') + expect(await reason('1ABC')).toBe('invalid_name') + for (const name of ['PATH', 'HOME', 'LD_PRELOAD', 'DYLD_INSERT_LIBRARIES', 'NODE_OPTIONS', 'ORCA_SANDBOX_ID', 'GH_TOKEN']) { + expect(await reason(name)).toBe('reserved_name') + } + expect(await reason('CLAUDE_CODE_OAUTH_TOKEN', 'x')).toBe('subscription_token_refused') + expect(await reason('ANTHROPIC_API_KEY', 'sk-ant-oat01-abcdefghijklmnop')).toBe('subscription_token_refused') + expect(await reason('ANY_NAME', ' sk-ant-oat01-abc')).toBe('subscription_token_refused') + const jwt = 'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ1c2VyLTEyMyJ9.c2lnbmF0dXJlLXZhbHVlLWhlcmU' + expect(await reason('OPENAI_API_KEY', jwt)).toBe('subscription_token_refused') + expect(await reason('ANTHROPIC_API_KEY', jwt)).toBe('subscription_token_refused') + expect((await put('personal', 'SOME_SERVICE_JWT', jwt, alice)).status).toBe(200) + expect((await put('personal', 'AWS_SECRET_ACCESS_KEY', 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY', alice)).status).toBe(200) + expect(await reason('BIG', 'x'.repeat(MAX_VALUE_BYTES + 1))).toBe('value_too_large') + expect(await reason('EMPTY', '')).toBe('invalid_value') + for (let i = h.raw.prepare('SELECT COUNT(*) AS n FROM secrets').get()!.n as number; i < MAX_SECRETS_PER_OWNER; i++) { + await put('personal', `K${i}`, 'v', alice) + } + expect((await put('personal', 'ONE_MORE', 'v', alice)).body).toEqual({ error: 'conflict', reason: 'secret_limit' }) + // Overwriting an existing name is still allowed at the cap. + expect((await put('personal', 'K10', 'v2', alice)).status).toBe(200) + }) + + it('answers 503 until the KEK is configured', async () => { + const h = await createHarness() + expect((await h.call('GET', '/v1/secrets?scope=personal')).status).toBe(503) + expect((await h.call('PUT', '/v1/secrets/personal/FOO', { body: { value: 'x' } })).status).toBe(503) + expect((await h.call('GET', '/v1/secrets', { bearer: 'nope' })).status).toBe(401) + }) +}) + +describe('secret injection at session start', () => { + async function seeded(overrides: Record = {}, deps: Parameters[1] = {}) { + const ctx = await setup(overrides, deps) + const admin = await ctx.as('ada', 'acme', 'admin') + const mia = await ctx.as('mia', 'acme', 'member') + await ctx.put('org', 'ANTHROPIC_API_KEY', ORG_KEY, admin) + await ctx.put('org', 'FOO', 'org-env-value', admin) + await ctx.put('org', 'ORG_ONLY', 'o', admin) + await ctx.put('personal', 'ANTHROPIC_API_KEY', PERSONAL_KEY, mia) + await ctx.put('personal', 'FOO', 'personal-env-value', mia) + await ctx.put('personal', 'OPENAI_API_KEY', OPENAI_KEY, mia) + await ctx.put('repo', 'BAR', 'repo-env-value', admin, 'acme/app') + await ctx.put('personal', 'BAR', 'personal-bar', mia) + return { ...ctx, mia, admin } + } + + it('applies precedence (env: repo > personal > org; agent keys: org > personal) and re-resolves on resume', async () => { + const { h, mia, put } = await seeded() + const created = await h.call('POST', '/v1/sandboxes', { + body: { workspaceId: 'ws-1', machineSize: 'standard', repo: { url: 'https://github.com/acme/app' } }, + bearer: mia + }) + expect(created.status).toBe(200) + expect(startEnv(h)).toEqual({ + ANTHROPIC_API_KEY: ORG_KEY, + BAR: 'repo-env-value', + FOO: 'personal-env-value', + OPENAI_API_KEY: OPENAI_KEY, + ORG_ONLY: 'o' + }) + // Other start env never carries a value. + const start = h.vercel.commands.find((c) => c.command === START_COMMAND)! + expect(JSON.stringify({ ...start.env, ORCA_SECRET_ENV: '' })).not.toContain('fake') + + // Edit, then suspend/resume: the new value lands with no image rebuild. + await put('personal', 'FOO', 'personal-env-value-2', mia) + const id = created.body.sandbox.id + await h.call('POST', `/v1/sandboxes/${id}/suspend`, { bearer: mia }) + await h.call('POST', `/v1/sandboxes/${id}/resume`, { bearer: mia }) + expect(startEnv(h).FOO).toBe('personal-env-value-2') + }) + + it('injects only the sandbox owner’s personal secrets and skips a row whose ciphertext was swapped', async () => { + const { h, put, as } = await seeded() + const bob = await as('bob', 'acme', 'member') + // Copy mia's sealed OPENAI_API_KEY onto bob's personal row: AAD binds it to mia, so it won't open. + await put('personal', 'OPENAI_API_KEY', 'bob-placeholder-value', bob) + h.raw.exec(`UPDATE secrets SET (kek_id, wrapped_dek, ciphertext) = + (SELECT kek_id, wrapped_dek, ciphertext FROM secrets WHERE owner_id = 'user:mia' AND name = 'OPENAI_API_KEY') + WHERE owner_id = 'user:bob' AND name = 'OPENAI_API_KEY'`) + expect((await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-b', machineSize: 'standard' }, bearer: bob })).status).toBe(200) + const env = startEnv(h) + expect(env).toEqual({ ANTHROPIC_API_KEY: ORG_KEY, FOO: 'org-env-value', ORG_ONLY: 'o' }) + expect(logs.some((line) => line.includes('secret_unreadable') && line.includes('OPENAI_API_KEY'))).toBe(true) + }) + + it('starts without secrets when no KEK is configured', async () => { + const h = await createHarness() + await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' } }) + expect(h.vercel.commands.find((c) => c.command === START_COMMAND)!.env.ORCA_SECRET_ENV).toBeUndefined() + }) +}) + +describe('model API key egress placeholders (phase 2)', () => { + async function placeholderSetup() { + const { privateKey, publicKey } = await generateKeyPair('RS256') + const jwks = { keys: [{ ...(await exportJWK(publicKey)), kid: 'v1', alg: 'RS256' }] } + const upstream: { url: string; headers: Headers }[] = [] + const ctx = await setup( + { SECRETS_EGRESS_PLACEHOLDERS: 'on', EGRESS_PROXY_URL: PROXY_URL }, + { + egressKeysForIssuer: () => createLocalJWKSet(jwks), + credentials: { authorizationFor: async () => 'Bearer ghs_fake' }, + egressFetch: async (input, init) => { + upstream.push({ url: String(input), headers: new Headers(init?.headers) }) + return new Response('data: {"type":"message_stop"}\n\n', { status: 200, headers: { 'content-type': 'text/event-stream' } }) + } + } + ) + const mia = await ctx.as('mia', 'acme', 'member') + await ctx.put('org', 'ANTHROPIC_API_KEY', ORG_KEY, await ctx.as('ada', 'acme', 'admin')) + await ctx.put('personal', 'OPENAI_API_KEY', OPENAI_KEY, mia) + await ctx.put('personal', 'GEMINI_API_KEY', 'gemini-plain-value', mia) + const created = await ctx.h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' }, bearer: mia }) + const sandboxId: string = created.body.sandbox.id + const oidc = () => + new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_name: `orca-test-${sandboxId}` }) + .setProtectedHeader({ alg: 'RS256', kid: 'v1' }) + .setIssuer('https://oidc.vercel.com/team_orca') + .setAudience(PROXY_URL) + .setIssuedAt() + .setExpirationTime('5m') + .sign(privateKey) + async function proxied(host: string, headers: Record) { + const response = await ctx.h.send( + new Request(`${PROXY_URL}/v1/messages`, { + method: 'POST', + body: '{"model":"x"}', + headers: { + 'vercel-forwarded-host': host, + 'vercel-forwarded-scheme': 'https', + 'vercel-forwarded-port': '443', + 'vercel-forwarded-path': '/v1/messages', + 'vercel-sandbox-oidc-token': await oidc(), + ...headers + } + }) + ) + return { status: response.status, body: await response.text() } + } + return { ...ctx, sandboxId, proxied, upstream } + } + + it('gives the VM placeholders, routes only the key hosts, and swaps the real key at egress', async () => { + const { h, sandboxId, proxied, upstream } = await placeholderSetup() + const anthropicPh = `orca-ph-anthropic-${sandboxId}` + const openaiPh = `orca-ph-openai-${sandboxId}` + expect(startEnv(h)).toEqual({ ANTHROPIC_API_KEY: anthropicPh, OPENAI_API_KEY: openaiPh, GEMINI_API_KEY: 'gemini-plain-value' }) + const [box] = [...h.vercel.boxes.values()] + const forward = (header: string, value: string) => ({ + match: { headers: [{ key: { exact: header }, value: { exact: value } }] }, + forwardURL: PROXY_URL + }) + const deny = { response: { statusCode: 403, body: 'orca-egress-denied', contentType: 'text/plain' } } + expect(box!.policy.allow['api.anthropic.com']).toEqual([ + forward('x-api-key', anthropicPh), + forward('authorization', `Bearer ${anthropicPh}`), + deny + ]) + expect(box!.policy.allow['api.openai.com']).toEqual([forward('authorization', `Bearer ${openaiPh}`), deny]) + expect(box!.policy.allow['generativelanguage.googleapis.com']).toEqual([]) + + expect(await proxied('api.anthropic.com', { 'x-api-key': anthropicPh })).toEqual({ status: 200, body: 'data: {"type":"message_stop"}\n\n' }) + expect(upstream.at(-1)!.headers.get('x-api-key')).toBe(ORG_KEY) + expect((await proxied('api.anthropic.com', { authorization: `Bearer ${anthropicPh}` })).status).toBe(200) + expect(upstream.at(-1)!.headers.get('authorization')).toBe(`Bearer ${ORG_KEY}`) + expect((await proxied('api.openai.com', { authorization: `Bearer ${openaiPh}` })).status).toBe(200) + expect(upstream.at(-1)).toMatchObject({ url: 'https://api.openai.com/v1/messages' }) + expect(upstream.at(-1)!.headers.get('authorization')).toBe(`Bearer ${OPENAI_KEY}`) + expect(upstream).toHaveLength(3) + }) + + it('denies key-host requests without the placeholder, with another sandbox’s placeholder, or a key it lacks', async () => { + const { proxied, upstream, sandboxId } = await placeholderSetup() + expect(await proxied('api.anthropic.com', { 'x-api-key': 'sk-ant-api03-attacker' })).toEqual({ status: 403, body: 'placeholder_missing' }) + expect(await proxied('api.anthropic.com', { authorization: 'Bearer sk-ant-oat01-subscription' })).toEqual({ + status: 403, + body: 'placeholder_missing' + }) + expect((await proxied('api.anthropic.com', { 'x-api-key': 'orca-ph-anthropic-sbx_other' })).status).toBe(403) + // The openai placeholder is not valid on the anthropic host. + expect((await proxied('api.anthropic.com', { authorization: `Bearer orca-ph-openai-${sandboxId}` })).status).toBe(403) + expect(upstream).toEqual([]) + }) + + it('keeps plain env injection and no model routes when the flag is off', async () => { + const { h, as, put } = await setup({ EGRESS_PROXY_URL: PROXY_URL }) + const mia = await as('mia', 'acme', 'member') + await put('personal', 'ANTHROPIC_API_KEY', PERSONAL_KEY, mia) + await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' }, bearer: mia }) + expect(startEnv(h).ANTHROPIC_API_KEY).toBe(PERSONAL_KEY) + expect([...h.vercel.boxes.values()][0]!.policy.allow['api.anthropic.com']).toEqual([]) + }) +}) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index 4a33ce81af8..0a05668e44d 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -4,7 +4,7 @@ // wrangler deploy -c wrangler.dev.jsonc // Secrets (wrangler secret put -c wrangler.dev.jsonc, never here): VERCEL_TOKEN, SANDBOX_DEV_JWKS, // EGRESS_DEV_STATIC_AUTHORIZATION, CONTROL_PLANE_TOKEN (same value as on - // orca-billing-dev), GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY (PKCS#8). + // orca-billing-dev), GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY (PKCS#8), SECRETS_KEK (base64 32 bytes). "name": "orca-sandbox-control-dev", "main": "src/index.ts", "compatibility_date": "2026-11-01", diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index 9bec21d5468..f2434cddcdd 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -194,6 +194,42 @@ create 5.1s, rollover 12.2s (same host key), destroy 1.0s, no snapshots left. Eg sandbox: Bearer and gh `token` placeholders forwarded and re-injected (GitHub answers the injected fake: 401), Node fetch completes in 450 ms with chunked framing (the transform-rule hang is gone), attacker token / no auth → 403. +## Secret store and injection (org / personal / repo) + +Product decision: "API-key injection" in the Conductor parity matrix (form 2 = this store; form 3 = egress swap). +- **Store** (`src/secrets.ts`, migration 0003): D1 `secrets` keyed (scope, owner_id, name). Envelope encryption + (`src/secrets-crypto.ts`): per-secret random AES-256-GCM DEK; DEK wrapped by the KEK (`SECRETS_KEK`, base64 32 + bytes, Worker secret only; id `SECRETS_KEK_ID`, default `k1`). Both layers take `["orca-secret-v1", layer, kekId, + scope, ownerId, name]` as AAD, so a row copied onto another owner/name fails to open (skipped at injection, + logged by name). Rotation: set the new KEK + id, move the old one into `SECRETS_KEK_RETIRED` (`{"k1":"…"}`); rows + re-seal on their next write. +- **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 + 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; + 100 secrets per owner; 128 KiB injected per start. **Never stored**: `CLAUDE_CODE_OAUTH_TOKEN`, any value starting + `sk-ant-oat`, JWT-shaped `ANTHROPIC_API_KEY`/`OPENAI_API_KEY` (ChatGPT/Claude.ai sessions). +- **Precedence**: env secrets repo > personal > org; agent keys (ANTHROPIC/OPENAI/GEMINI/GOOGLE/CURSOR keys, AWS + Bedrock and Vertex vars) org-shared > repo > personal. Per name, so a partial org Bedrock set mixes with personal. +- **Injection**: every boot (create, resume, rollover) resolves secrets and passes them to `orca-sandbox-start` as + `ORCA_SECRET_ENV` (`NAME=` lines). The script decodes them into orcad's `env -i` launch env (agents inherit + it) through a pipe, unsets the variable, and writes nothing to disk, so the stop snapshot never holds them and + `orca-image-wipe` has nothing to wipe. Edits apply on the next start/resume, no image rebuild. Values transit + Vercel's command API (as the VM's env would anyway); logs carry counts and names only. +- **Phase 2 egress swap** (`src/secret-egress.ts`, `SECRETS_EGRESS_PLACEHOLDERS=on`, off by default): VM env gets + `orca-ph-anthropic-` / `orca-ph-openai-`; boot records the hosts on the row (`egress_secret_hosts`) and + the policy gets forwardURL claims (`api.anthropic.com`: `x-api-key: ` and `Authorization: Bearer `; + `api.openai.com`: `Authorization: Bearer `) followed by the 403 deny. The egress proxy (OIDC-verified, as for + api.github.com) swaps in the real key, cached per session for 60 s. Transform rules are not used: they hang Node + clients (Claude Code, Codex), the forwardURL path is the one proven live for api.github.com. Not on by default + 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. + ## What needs live access (not done) Vercel Pro team id + project id + a Sandbox-scoped token (as Worker secrets), wrangler auth for a dev D1, a From 62abb9cd845e1d42fceb8e22e0baa7bbd23f8cd1 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:16:07 -0800 Subject: [PATCH 11/18] fix(cloud): bind GitHub App installations to Orca accounts; kill switch, caps, rate limit; orphan sweep and retention (#24) Security (B1): repo tokens were minted for any repo.url where the app was installed. Now an installation must be bound to the caller's account (signed single-use connect state + OAuth proof that the installer owns the account or is an org admin), tokens are scoped to the one repo and the binding is re-checked on cached tokens. Operator binding route for support/dev. Guardrails (B3/G4): D1 kill switch + static flag refuse create/resume/rollover and drain running sandboxes; global running cap; 3/seat per-account backstop; per-account create rate limit. Retention (B4/B6): hourly orphan sweep (stop, then delete after grace; partial listings delete nothing), 30-day deletion after cancel/unpaid with a T-7 warning hook, 90-day idle retention, 104-day provider snapshot expiry backstop, repo_url tombstoned on destroy. --- .../0004_github_bindings_guardrails.sql | 64 +++ .../apps/sandbox-control/src/entitlements.ts | 18 +- cloud/apps/sandbox-control/src/env.ts | 29 +- cloud/apps/sandbox-control/src/github-app.ts | 98 ++++- .../sandbox-control/src/github-connect.ts | 305 +++++++++++++ cloud/apps/sandbox-control/src/guardrails.ts | 129 ++++++ cloud/apps/sandbox-control/src/index.ts | 165 +++++++- cloud/apps/sandbox-control/src/lifecycle.ts | 37 +- cloud/apps/sandbox-control/src/operator.ts | 117 +++++ cloud/apps/sandbox-control/src/reconcile.ts | 21 +- cloud/apps/sandbox-control/src/retention.ts | 399 ++++++++++++++++++ .../sandbox-control/src/sandbox-provider.ts | 22 + cloud/apps/sandbox-control/src/store.ts | 3 +- .../sandbox-control/src/vercel-sandbox-api.ts | 52 ++- .../apps/sandbox-control/test/fake-vercel.ts | 21 +- .../sandbox-control/test/github-app.test.ts | 316 ++++++++++++-- .../sandbox-control/test/guardrails.test.ts | 85 ++++ .../sandbox-control/test/retention.test.ts | 180 ++++++++ .../test/vercel-sandbox-api.test.ts | 7 +- cloud/apps/sandbox-control/wrangler.dev.jsonc | 8 +- cloud/docs/vercel-driver-design.md | 23 + 21 files changed, 2028 insertions(+), 71 deletions(-) create mode 100644 cloud/apps/sandbox-control/migrations/0004_github_bindings_guardrails.sql create mode 100644 cloud/apps/sandbox-control/src/github-connect.ts create mode 100644 cloud/apps/sandbox-control/src/guardrails.ts create mode 100644 cloud/apps/sandbox-control/src/operator.ts create mode 100644 cloud/apps/sandbox-control/src/retention.ts create mode 100644 cloud/apps/sandbox-control/test/guardrails.test.ts create mode 100644 cloud/apps/sandbox-control/test/retention.test.ts diff --git a/cloud/apps/sandbox-control/migrations/0004_github_bindings_guardrails.sql b/cloud/apps/sandbox-control/migrations/0004_github_bindings_guardrails.sql new file mode 100644 index 00000000000..658fddf980e --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0004_github_bindings_guardrails.sql @@ -0,0 +1,64 @@ +-- GitHub App installations bound to Orca accounts. Repo tokens are minted only for a caller whose +-- account has a live binding to the installation that covers the repo (deny by default). +CREATE TABLE github_installations ( + installation_id INTEGER NOT NULL, + account_id TEXT NOT NULL, + github_account_id INTEGER NOT NULL, + github_account_login TEXT NOT NULL, + github_account_type TEXT NOT NULL, + -- Orca user who bound it, and the GitHub user proven (OAuth) to own/admin the installation account. + bound_by_user_id TEXT NOT NULL, + bound_by_github_user_id INTEGER, + -- `oauth` (owner/org admin verified with a user-to-server token) | `operator` (support tool). + method TEXT NOT NULL, + created_at INTEGER NOT NULL, + revoked_at INTEGER, + PRIMARY KEY (installation_id, account_id) +); +CREATE INDEX github_installations_account ON github_installations (account_id); + +-- Single-use install/connect state nonces (signed state carries the account; this stops replay). +CREATE TABLE github_connect_states ( + nonce TEXT PRIMARY KEY, + account_id TEXT NOT NULL, + expires_at INTEGER NOT NULL, + consumed_at INTEGER +); + +-- Operator flags (kill switch) and cron bookkeeping; D1 so flipping needs no redeploy. +CREATE TABLE control_state ( + name TEXT PRIMARY KEY, + value TEXT NOT NULL, + updated_at INTEGER NOT NULL +); + +-- Fixed-window create counters per account. +CREATE TABLE rate_windows ( + bucket TEXT PRIMARY KEY, + window_start INTEGER NOT NULL, + count INTEGER NOT NULL +); + +-- Provider resources with no live D1 record: suspended on first sight, deleted after a grace period. +CREATE TABLE provider_orphans ( + kind TEXT NOT NULL, + provider_id TEXT NOT NULL, + first_seen_at INTEGER NOT NULL, + last_seen_at INTEGER NOT NULL, + suspended_at INTEGER, + deleted_at INTEGER, + PRIMARY KEY (kind, provider_id) +); + +-- Accounts whose subscription is canceled/unpaid: data is deleted DATA_RETENTION_DAYS after `since`. +CREATE TABLE account_holds ( + account_id TEXT PRIMARY KEY, + reason TEXT NOT NULL, + since INTEGER NOT NULL, + checked_at INTEGER NOT NULL, + warned_at INTEGER, + purged_at INTEGER +); + +-- Idle-retention warning marker for long-suspended sandboxes. +ALTER TABLE sandboxes ADD COLUMN retention_warned_at INTEGER; diff --git a/cloud/apps/sandbox-control/src/entitlements.ts b/cloud/apps/sandbox-control/src/entitlements.ts index fe4624e0300..e3692fa4259 100644 --- a/cloud/apps/sandbox-control/src/entitlements.ts +++ b/cloud/apps/sandbox-control/src/entitlements.ts @@ -7,7 +7,13 @@ import type { Env } from './env' export type MachineSize = 'standard' | 'large' /** `suspendRunning`: payment grace ran out, so the account's running sandboxes must stop too. */ -export type StartDecision = { allow: boolean; reason: string | null; suspendRunning: boolean } +export type StartDecision = { + allow: boolean + reason: string | null + suspendRunning: boolean + /** Paid seats, when billing reports them; scales the control plane's per-account backstop. */ + seats?: number +} export type UsageSegment = { usageId: string @@ -41,7 +47,8 @@ export class BillingUnavailableError extends Error { const StartAnswer = z.looseObject({ allow: z.boolean(), reason: z.string().nullable(), - suspendRunning: z.boolean().default(false) + suspendRunning: z.boolean().default(false), + seats: z.number().int().positive().max(100_000).optional() }) const UsageResponse = z.looseObject({ canStartSandbox: z.boolean(), @@ -52,6 +59,13 @@ const SuspensionsResponse = z.looseObject({ accounts: z.array(z.looseObject({ accountId: z.string() })).max(10_000) }) +/** Reasons that start the data-retention clock (canceled / unpaid). */ +export const RETENTION_HOLD_REASONS: ReadonlySet = new Set([ + 'subscription_inactive', + 'no_subscription', + 'payment_grace_expired' +]) + /** Reasons that mean running sandboxes must stop, not just that new starts wait. */ export const STOP_RUNNING_REASONS: ReadonlySet = new Set([ 'included_hours_exhausted', diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index cfa34aec477..3e0c3692546 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -39,7 +39,7 @@ export type Env = { /** Dev-only: Authorization the egress proxy injects for api.github.com until the GitHub App minter exists. */ EGRESS_DEV_STATIC_AUTHORIZATION?: string - // GitHub App (contents/pull_requests write, metadata read). The PKCS#8 key is a secret. + // GitHub App (contents/pull_requests write, metadata read, org members read). The PKCS#8 key is a secret. GITHUB_APP_ID?: string GITHUB_APP_PRIVATE_KEY?: string @@ -55,4 +55,31 @@ export type Env = { * key in (forwardURL path). Anything else: plain env injection. Needs EGRESS_PROXY_URL. */ SECRETS_EGRESS_PLACEHOLDERS?: string + + /** App slug for the install URL (github.com/apps/). */ + GITHUB_APP_SLUG?: string + /** OAuth credentials of the same app: prove who installed it before binding (secret: CLIENT_SECRET). */ + GITHUB_APP_CLIENT_ID?: string + GITHUB_APP_CLIENT_SECRET?: string + /** HMAC key for the signed connect state (secret, >= 32 chars). */ + GITHUB_STATE_SECRET?: string + + /** Operator routes (/v1/internal/*): kill switch, drain, bindings. Secret, >= 32 chars; unset = no routes. */ + OPERATOR_TOKEN?: string + /** `true` refuses every create/resume/rollover (static kill switch; the D1 flag is the runtime one). */ + SANDBOX_CREATES_DISABLED?: string + /** Global cap on active sandboxes across all accounts (default 50). */ + SANDBOX_MAX_RUNNING?: string + /** Per-account active cap per paid seat (default 3); billing enforces the plan limit as well. */ + ACCOUNT_MAX_ACTIVE_PER_SEAT?: string + /** POST /v1/sandboxes per account per minute (default 10). */ + CREATE_RATE_PER_MINUTE?: string + /** Days data is kept after cancel/unpaid before deletion (default 30). */ + DATA_RETENTION_DAYS?: string + /** Days a suspended sandbox may sit untouched before deletion (default 90). */ + IDLE_RETENTION_DAYS?: string + /** Hours an orphan provider resource is kept (suspended) before deletion (default 24). */ + ORPHAN_GRACE_HOURS?: string + /** Provider snapshot expiry after last use, days (default 104, backstop behind idle retention). */ + SNAPSHOT_EXPIRATION_DAYS?: string } diff --git a/cloud/apps/sandbox-control/src/github-app.ts b/cloud/apps/sandbox-control/src/github-app.ts index ebea80124dc..26bb8258c21 100644 --- a/cloud/apps/sandbox-control/src/github-app.ts +++ b/cloud/apps/sandbox-control/src/github-app.ts @@ -1,21 +1,50 @@ // GitHub App installation tokens for sandbox git: 1h, scoped to the workspace's one repository with // contents + pull_requests write. Minted here, injected at the egress layer, never stored in the VM. +// A token is minted only when the caller's Orca account is bound to the installation that covers the +// repo (github-connect.ts records bindings); an app installed on a repo is not by itself a grant. import { SignJWT, importPKCS8 } from 'jose' +import { z } from 'zod' export type GitHubRepoToken = { token: string; expiresAt: number } -export type GitHubAppMinter = { - /** Null when the repo is not on github.com or the app is not installed on it. */ - tokenForRepo(repoUrl: string): Promise +export type GitHubInstallation = { + id: number + account: { id: number; login: string; type: 'User' | 'Organization' } } +export type GitHubAppMinter = { + /** + * Null when the repo is not on github.com, the app is not installed on it, or the installation is + * not bound to `accountId`. Deny by default. + */ + tokenForRepo(repoUrl: string, accountId: string): Promise + /** App-level installation lookup (operator binding). Null when absent. */ + getInstallation(installationId: number): Promise + /** The installation covering a github.com repo URL, or null. */ + getRepoInstallation(repoUrl: string): Promise +} + +/** Whether `accountId` holds a live binding to the installation. */ +export type InstallationBindingCheck = (installationId: number, accountId: string) => Promise + const REFRESH_MARGIN_MS = 5 * 60_000 +const REPO_PERMISSIONS = { contents: 'write', pull_requests: 'write', metadata: 'read' } as const + +const Installation = z.looseObject({ + id: z.number().int().positive(), + account: z.looseObject({ id: z.number().int(), login: z.string(), type: z.enum(['User', 'Organization']) }) +}) +const MintedToken = z.looseObject({ + token: z.string().min(1), + expires_at: z.string(), + repositories: z.array(z.looseObject({ id: z.number(), name: z.string() })).optional() +}) export function parseGitHubRepo(repoUrl: string): { owner: string; repo: string } | null { try { const url = new URL(repoUrl) const match = /^\/([A-Za-z0-9-]+)\/([A-Za-z0-9._-]+?)(?:\.git)?\/?$/.exec(url.pathname) - return url.protocol === 'https:' && url.hostname === 'github.com' && match + return url.protocol === 'https:' && url.hostname === 'github.com' && !url.username && !url.password && match ? { owner: match[1]!, repo: match[2]! } : null } catch { @@ -26,12 +55,15 @@ export function parseGitHubRepo(repoUrl: string): { owner: string; repo: string export function createGitHubAppMinter(config: { appId: string privateKeyPkcs8: string + isBound: InstallationBindingCheck fetch?: typeof fetch now?: () => number + log?: (event: Record) => void }): GitHubAppMinter { const doFetch = config.fetch ?? fetch const now = config.now ?? Date.now - const cache = new Map() + const log = config.log ?? ((event) => console.log(JSON.stringify(event))) + const cache = new Map() let signingKey: CryptoKey | null = null async function appJwt(): Promise { @@ -59,8 +91,30 @@ export function createGitHubAppMinter(config: { }) } + async function installationFor(path: string): Promise { + const response = await github('GET', path) + if (response.status === 404) { + return null + } + if (!response.ok) { + throw new Error(`github installation lookup failed (${response.status})`) + } + const parsed = Installation.safeParse(await response.json().catch(() => null)) + if (!parsed.success) { + throw new Error('github installation lookup answered an unexpected body') + } + return parsed.data + } + return { - async tokenForRepo(repoUrl) { + getInstallation: (installationId) => installationFor(`/app/installations/${installationId}`), + + async getRepoInstallation(repoUrl) { + const parsed = parseGitHubRepo(repoUrl) + return parsed ? installationFor(`/repos/${parsed.owner}/${parsed.repo}/installation`) : null + }, + + async tokenForRepo(repoUrl, accountId) { const parsed = parseGitHubRepo(repoUrl) if (!parsed) { return null @@ -68,26 +122,36 @@ export function createGitHubAppMinter(config: { const key = `${parsed.owner}/${parsed.repo}`.toLowerCase() const cached = cache.get(key) if (cached && cached.expiresAt - now() > REFRESH_MARGIN_MS) { - return cached + // The binding is re-checked on every use: a revoked binding stops a cached token too. + return (await config.isBound(cached.installationId, accountId)) ? { token: cached.token, expiresAt: cached.expiresAt } : null } - const installation = await github('GET', `/repos/${parsed.owner}/${parsed.repo}/installation`) - if (installation.status === 404) { + const installation = await installationFor(`/repos/${parsed.owner}/${parsed.repo}/installation`) + if (!installation) { return null } - if (!installation.ok) { - throw new Error(`github installation lookup failed (${installation.status})`) + if (!(await config.isBound(installation.id, accountId))) { + log({ event: 'github_token_denied', reason: 'installation_not_bound', installationId: installation.id }) + return null } - const { id } = (await installation.json()) as { id: number } - const minted = await github('POST', `/app/installations/${id}/access_tokens`, { + const minted = await github('POST', `/app/installations/${installation.id}/access_tokens`, { repositories: [parsed.repo], - permissions: { contents: 'write', pull_requests: 'write', metadata: 'read' } + permissions: REPO_PERMISSIONS }) if (!minted.ok) { throw new Error(`github token mint failed (${minted.status})`) } - const body = (await minted.json()) as { token: string; expires_at: string } - const token = { token: body.token, expiresAt: Date.parse(body.expires_at) } - cache.set(key, token) + const body = MintedToken.safeParse(await minted.json().catch(() => null)) + // GitHub narrows to the named repo; refuse anything wider than that one repository. + if ( + !body.success || + !body.data.repositories || + body.data.repositories.length !== 1 || + body.data.repositories[0]!.name.toLowerCase() !== parsed.repo.toLowerCase() + ) { + throw new Error('github token mint answered an unexpected scope') + } + const token = { token: body.data.token, expiresAt: Date.parse(body.data.expires_at) } + cache.set(key, { ...token, installationId: installation.id }) return token } } diff --git a/cloud/apps/sandbox-control/src/github-connect.ts b/cloud/apps/sandbox-control/src/github-connect.ts new file mode 100644 index 00000000000..8610acbb13a --- /dev/null +++ b/cloud/apps/sandbox-control/src/github-connect.ts @@ -0,0 +1,305 @@ +// Binds GitHub App installations to Orca accounts. The desktop asks for a connect URL (signed, +// single-use state carrying its Orca account); GitHub sends the browser back to the setup/callback +// URL; we exchange the OAuth code for a user-to-server token and bind an installation only when that +// GitHub user owns the installation's account (personal) or is an active admin of the org. +// Installation ids in a callback URL are never trusted on their own. +import { z } from 'zod' +import type { GitHubInstallation } from './github-app' + +const STATE_TTL_MS = 10 * 60_000 + +export type GitHubConnectConfig = { + appSlug: string + clientId: string + clientSecret: string + stateSecret: string + fetch?: typeof fetch +} + +export type InstallationBinding = { + installation_id: number + account_id: string + github_account_id: number + github_account_login: string + github_account_type: string + bound_by_user_id: string + bound_by_github_user_id: number | null + method: 'oauth' | 'operator' + created_at: number + revoked_at: number | null +} + +// ---- D1 ---- + +export async function isInstallationBound(db: D1Database, installationId: number, accountId: string): Promise { + const row = await db + .prepare('SELECT 1 AS ok FROM github_installations WHERE installation_id = ? AND account_id = ? AND revoked_at IS NULL') + .bind(installationId, accountId) + .first<{ ok: number }>() + return row !== null +} + +export async function bindInstallation( + db: D1Database, + binding: Omit, + now: number +): Promise { + await db + .prepare( + `INSERT INTO github_installations (installation_id, account_id, github_account_id, github_account_login, + github_account_type, bound_by_user_id, bound_by_github_user_id, method, created_at, revoked_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NULL) + ON CONFLICT (installation_id, account_id) DO UPDATE SET github_account_id = excluded.github_account_id, + github_account_login = excluded.github_account_login, github_account_type = excluded.github_account_type, + bound_by_user_id = excluded.bound_by_user_id, bound_by_github_user_id = excluded.bound_by_github_user_id, + method = excluded.method, created_at = excluded.created_at, revoked_at = NULL` + ) + .bind(binding.installation_id, binding.account_id, binding.github_account_id, binding.github_account_login, + binding.github_account_type, binding.bound_by_user_id, binding.bound_by_github_user_id, binding.method, now) + .run() +} + +export async function revokeInstallation(db: D1Database, installationId: number, accountId: string | null, now: number) { + const result = await db + .prepare( + `UPDATE github_installations SET revoked_at = ? WHERE installation_id = ? AND revoked_at IS NULL + ${accountId ? 'AND account_id = ?' : ''}` + ) + .bind(...(accountId ? [now, installationId, accountId] : [now, installationId])) + .run() + return result.meta.changes +} + +export async function listAccountInstallations(db: D1Database, accountId: string) { + const { results } = await db + .prepare( + `SELECT installation_id, github_account_login, github_account_type, method, created_at + FROM github_installations WHERE account_id = ? AND revoked_at IS NULL ORDER BY created_at` + ) + .bind(accountId) + .all<{ installation_id: number; github_account_login: string; github_account_type: string; method: string; created_at: number }>() + return results +} + +// ---- signed state ---- + +const StatePayload = z.object({ + n: z.string().regex(/^[a-f0-9]{32}$/), + a: z.string().min(1).max(160), + u: z.string().min(1).max(160), + e: z.number().int(), + i: z.number().int().positive().optional() +}) +export type ConnectState = z.infer + +const b64url = (bytes: Uint8Array) => btoa(String.fromCharCode(...bytes)).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '') +const fromB64url = (text: string) => Uint8Array.from(atob(text.replaceAll('-', '+').replaceAll('_', '/')), (c) => c.charCodeAt(0)) + +async function hmac(secret: string, data: string): Promise { + const key = await crypto.subtle.importKey('raw', new TextEncoder().encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']) + return new Uint8Array(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(data))) +} + +export async function signState(secret: string, payload: ConnectState): Promise { + const body = b64url(new TextEncoder().encode(JSON.stringify(payload))) + return `${body}.${b64url(await hmac(secret, body))}` +} + +/** Null on a bad signature, malformed payload or expiry. Does not consume the nonce. */ +export async function verifyState(secret: string, state: string, now: number): Promise { + const [body, mac, extra] = state.split('.') + if (!body || !mac || extra !== undefined || state.length > 2048) { + return null + } + try { + const expected = await hmac(secret, body) + const given = fromB64url(mac) + if (given.length !== expected.length || !timingSafeEqual(given, expected)) { + return null + } + const parsed = StatePayload.safeParse(JSON.parse(new TextDecoder().decode(fromB64url(body)))) + return parsed.success && parsed.data.e > now ? parsed.data : null + } catch { + return null + } +} + +export function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean { + let diff = a.length ^ b.length + for (let i = 0; i < Math.max(a.length, b.length); i++) { + diff |= (a[i] ?? 0) ^ (b[i] ?? 0) + } + return diff === 0 +} + +export async function createConnectState( + db: D1Database, + secret: string, + caller: { accountId: string; userId: string }, + now: number +): Promise { + const nonce = Array.from(crypto.getRandomValues(new Uint8Array(16)), (b) => b.toString(16).padStart(2, '0')).join('') + await db + .prepare('INSERT INTO github_connect_states (nonce, account_id, expires_at) VALUES (?, ?, ?)') + .bind(nonce, caller.accountId, now + STATE_TTL_MS) + .run() + return signState(secret, { n: nonce, a: caller.accountId, u: caller.userId, e: now + STATE_TTL_MS }) +} + +/** Marks the nonce used; false when it was already used, unknown, or issued to another account. */ +async function consumeState(db: D1Database, state: ConnectState, now: number): Promise { + const result = await db + .prepare( + `UPDATE github_connect_states SET consumed_at = ? WHERE nonce = ? AND account_id = ? AND consumed_at IS NULL AND expires_at > ?` + ) + .bind(now, state.n, state.a, now) + .run() + return result.meta.changes === 1 +} + +// ---- GitHub user-to-server verification ---- + +const UserBody = z.looseObject({ id: z.number().int(), login: z.string() }) +const UserInstallations = z.looseObject({ + installations: z.array( + z.looseObject({ + id: z.number().int(), + account: z.looseObject({ id: z.number().int(), login: z.string(), type: z.enum(['User', 'Organization']) }) + }) + ) +}) +const Membership = z.looseObject({ state: z.string(), role: z.string() }) + +export type VerifiedInstallation = GitHubInstallation & { githubUserId: number } + +/** + * Exchanges the OAuth code and returns the installations this GitHub user may bind: ones on their + * own account, or on an org where they are an active admin. `only` narrows to one installation id. + */ +export async function verifiedInstallations( + config: GitHubConnectConfig, + code: string, + only?: number +): Promise<{ githubUser: { id: number; login: string }; allowed: VerifiedInstallation[]; denied: number[] }> { + const doFetch = config.fetch ?? fetch + const exchanged = await doFetch('https://github.com/login/oauth/access_token', { + method: 'POST', + headers: { accept: 'application/json', 'content-type': 'application/json', 'user-agent': 'orca-sandbox-control' }, + body: JSON.stringify({ client_id: config.clientId, client_secret: config.clientSecret, code }), + redirect: 'manual' + }) + const token = z.looseObject({ access_token: z.string().min(1) }).safeParse(await exchanged.json().catch(() => null)) + if (!exchanged.ok || !token.success) { + throw new GitHubConnectError('oauth_exchange_failed') + } + const userApi = async (path: string) => + doFetch(`https://api.github.com${path}`, { + headers: { + authorization: `Bearer ${token.data.access_token}`, + accept: 'application/vnd.github+json', + 'x-github-api-version': '2022-11-28', + 'user-agent': 'orca-sandbox-control' + }, + redirect: 'manual' + }) + const userResponse = await userApi('/user') + const user = UserBody.safeParse(await userResponse.json().catch(() => null)) + if (!userResponse.ok || !user.success) { + throw new GitHubConnectError('github_user_unavailable') + } + const listed = await userApi('/user/installations?per_page=100') + const installations = UserInstallations.safeParse(await listed.json().catch(() => null)) + if (!listed.ok || !installations.success) { + throw new GitHubConnectError('github_installations_unavailable') + } + const candidates = installations.data.installations.filter((i) => only === undefined || i.id === only) + const allowed: VerifiedInstallation[] = [] + const denied: number[] = [] + for (const installation of candidates) { + let ok = false + if (installation.account.type === 'User') { + ok = installation.account.id === user.data.id + } else { + // Org: only an active org admin may bind it (needs the app's Members:read org permission). + const membership = await userApi(`/user/memberships/orgs/${encodeURIComponent(installation.account.login)}`) + const body = Membership.safeParse(await membership.json().catch(() => null)) + ok = membership.ok && body.success && body.data.state === 'active' && body.data.role === 'admin' + } + if (ok) { + allowed.push({ id: installation.id, account: installation.account, githubUserId: user.data.id }) + } else { + denied.push(installation.id) + } + } + if (only !== undefined && candidates.length === 0) { + denied.push(only) + } + return { githubUser: user.data, allowed, denied } +} + +export class GitHubConnectError extends Error { + constructor(readonly code: string) { + super(code) + this.name = 'GitHubConnectError' + } +} + +export function authorizeUrl(config: GitHubConnectConfig, origin: string, state: string): string { + const url = new URL('https://github.com/login/oauth/authorize') + url.searchParams.set('client_id', config.clientId) + url.searchParams.set('redirect_uri', `${origin}/v1/github/callback`) + url.searchParams.set('state', state) + url.searchParams.set('allow_signup', 'false') + return url.toString() +} + +export function installUrl(config: GitHubConnectConfig, state: string): string { + return `https://github.com/apps/${encodeURIComponent(config.appSlug)}/installations/new?state=${encodeURIComponent(state)}` +} + +/** + * Setup URL (after install) and OAuth callback. Without a code we send the browser through OAuth so + * GitHub proves who the installer is; the installation id is carried inside a re-signed state. + */ +export async function handleGitHubReturn( + db: D1Database, + config: GitHubConnectConfig, + url: URL, + now: number +): Promise<{ redirect: string } | { bound: string[]; denied: number } | { error: string }> { + const rawState = url.searchParams.get('state') ?? '' + const state = rawState ? await verifyState(config.stateSecret, rawState, now) : null + if (!state) { + return { error: 'invalid_state' } + } + const queryInstallation = Number(url.searchParams.get('installation_id') ?? NaN) + const installationId = state.i ?? (Number.isSafeInteger(queryInstallation) && queryInstallation > 0 ? queryInstallation : undefined) + const code = url.searchParams.get('code') ?? '' + if (!/^[A-Za-z0-9_-]{8,128}$/.test(code)) { + // Carry the (still untrusted) installation id through OAuth; verification happens on return. + const carried = await signState(config.stateSecret, { ...state, ...(installationId ? { i: installationId } : {}) }) + return { redirect: authorizeUrl(config, url.origin, carried) } + } + if (!(await consumeState(db, state, now))) { + return { error: 'state_used' } + } + const result = await verifiedInstallations(config, code, installationId) + for (const installation of result.allowed) { + await bindInstallation( + db, + { + installation_id: installation.id, + account_id: state.a, + github_account_id: installation.account.id, + github_account_login: installation.account.login, + github_account_type: installation.account.type, + bound_by_user_id: state.u, + bound_by_github_user_id: installation.githubUserId, + method: 'oauth' + }, + now + ) + } + console.log(JSON.stringify({ event: 'github_bind', bound: result.allowed.length, denied: result.denied.length })) + return { bound: result.allowed.map((i) => i.account.login), denied: result.denied.length } +} diff --git a/cloud/apps/sandbox-control/src/guardrails.ts b/cloud/apps/sandbox-control/src/guardrails.ts new file mode 100644 index 00000000000..dbb16908908 --- /dev/null +++ b/cloud/apps/sandbox-control/src/guardrails.ts @@ -0,0 +1,129 @@ +// Cost guardrails in front of every start (create, resume, rollover): the operator kill switch, a +// global running cap, a per-account concurrency backstop (billing enforces the plan limit too) and a +// per-account create rate limit. All are control-plane local, so a billing outage cannot lift them. +import { ACTIVE_STATUSES } from './store' + +export type GuardrailConfig = { + /** Static kill switch from the deploy config (`SANDBOX_CREATES_DISABLED=true`). */ + startsDisabled: boolean + maxRunning: number + maxActivePerSeat: number + createsPerMinute: number +} + +export const DEFAULT_GUARDRAILS: GuardrailConfig = { + startsDisabled: false, + maxRunning: 50, + maxActivePerSeat: 3, + createsPerMinute: 10 +} + +export class GuardrailError extends Error { + constructor( + readonly code: 'maintenance' | 'capacity' | 'rate_limited' | 'concurrency_limit', + readonly retryAfterSeconds?: number + ) { + super(code) + this.name = 'GuardrailError' + } +} + +const KILL_SWITCH = 'kill_switch' + +export async function getControlState(db: D1Database, name: string): Promise { + const row = await db.prepare('SELECT value FROM control_state WHERE name = ?').bind(name).first<{ value: string }>() + return row?.value ?? null +} + +export async function setControlState(db: D1Database, name: string, value: string, now: number): Promise { + await db + .prepare( + `INSERT INTO control_state (name, value, updated_at) VALUES (?, ?, ?) + ON CONFLICT (name) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at` + ) + .bind(name, value, now) + .run() +} + +export async function killSwitchOn(db: D1Database, config: GuardrailConfig): Promise { + return config.startsDisabled || (await getControlState(db, KILL_SWITCH)) === 'on' +} + +export function setKillSwitch(db: D1Database, on: boolean, now: number): Promise { + return setControlState(db, KILL_SWITCH, on ? 'on' : 'off', now) +} + +async function countActive(db: D1Database, accountId?: string): Promise { + const placeholders = ACTIVE_STATUSES.map(() => '?').join(', ') + const row = await db + .prepare( + `SELECT COUNT(*) AS n FROM sandboxes WHERE status IN (${placeholders}) ${accountId ? 'AND account_id = ?' : ''}` + ) + .bind(...ACTIVE_STATUSES, ...(accountId ? [accountId] : [])) + .first<{ n: number }>() + return row?.n ?? 0 +} + +export function countAllActive(db: D1Database): Promise { + return countActive(db) +} + +/** Kill switch + global running cap; checked before billing is asked anything. */ +export async function assertStartsOpen(db: D1Database, config: GuardrailConfig): Promise { + if (await killSwitchOn(db, config)) { + throw new GuardrailError('maintenance') + } + if ((await countAllActive(db)) >= config.maxRunning) { + console.log(JSON.stringify({ event: 'global_cap_reached', maxRunning: config.maxRunning })) + throw new GuardrailError('capacity', 60) + } +} + +/** Per-account backstop (3/seat); `seats` comes from billing when it reports one, else 1. */ +export async function assertAccountCapacity( + db: D1Database, + config: GuardrailConfig, + accountId: string, + seats = 1 +): Promise { + if ((await countActive(db, accountId)) >= config.maxActivePerSeat * Math.max(1, Math.floor(seats))) { + throw new GuardrailError('concurrency_limit') + } +} + +/** Fixed one-minute window per account; counts every create request, retries included. */ +export async function consumeCreateToken(db: D1Database, config: GuardrailConfig, accountId: string, now: number) { + const windowStart = now - (now % 60_000) + const bucket = `create:${accountId}` + await db + .prepare( + `INSERT INTO rate_windows (bucket, window_start, count) VALUES (?, ?, 1) + ON CONFLICT (bucket) DO UPDATE SET + count = CASE WHEN rate_windows.window_start = excluded.window_start THEN rate_windows.count + 1 ELSE 1 END, + window_start = excluded.window_start` + ) + .bind(bucket, windowStart) + .run() + const row = await db.prepare('SELECT count FROM rate_windows WHERE bucket = ?').bind(bucket).first<{ count: number }>() + if ((row?.count ?? 0) > config.createsPerMinute) { + throw new GuardrailError('rate_limited', Math.ceil((windowStart + 60_000 - now) / 1000)) + } +} + +export function guardrailsFromEnv(env: { + SANDBOX_CREATES_DISABLED?: string + SANDBOX_MAX_RUNNING?: string + ACCOUNT_MAX_ACTIVE_PER_SEAT?: string + CREATE_RATE_PER_MINUTE?: string +}): GuardrailConfig { + const int = (value: string | undefined, fallback: number) => { + const parsed = Number(value) + return value !== undefined && Number.isSafeInteger(parsed) && parsed >= 0 ? parsed : fallback + } + return { + startsDisabled: env.SANDBOX_CREATES_DISABLED === 'true', + maxRunning: int(env.SANDBOX_MAX_RUNNING, DEFAULT_GUARDRAILS.maxRunning), + maxActivePerSeat: int(env.ACCOUNT_MAX_ACTIVE_PER_SEAT, DEFAULT_GUARDRAILS.maxActivePerSeat), + createsPerMinute: int(env.CREATE_RATE_PER_MINUTE, DEFAULT_GUARDRAILS.createsPerMinute) + } +} diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 6f013a32480..87815c21114 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -7,6 +7,10 @@ // POST /v1/sandboxes/:id/lease {extendMs} -> {sandbox} // DELETE /v1/sandboxes/:id -> {sandbox} // GET /v1/secrets?scope= | PUT, DELETE /v1/secrets/:scope/:NAME (see src/secrets.ts) +// POST /v1/github/connect -> {installUrl, authorizeUrl} (bind a GitHub App installation to the account) +// GET /v1/github/installations -> {installations} +// Browser (GitHub redirects): GET /v1/github/setup, GET /v1/github/callback +// Operator (OPERATOR_TOKEN): /v1/internal/* (see operator.ts) // Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy import { authenticateDesktop } from './auth' import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker, type ProxiedRoute } from './egress-proxy' @@ -29,6 +33,19 @@ import { type LifecycleDeps } from './lifecycle' import { createGitHubAppMinter, type GitHubAppMinter } from './github-app' +import { + GitHubConnectError, + authorizeUrl, + createConnectState, + handleGitHubReturn, + installUrl, + isInstallationBound, + listAccountInstallations, + type GitHubConnectConfig +} from './github-connect' +import { GuardrailError, consumeCreateToken, guardrailsFromEnv } from './guardrails' +import { handleOperator, isOperator } from './operator' +import { DEFAULT_RETENTION, type RetentionConfig } from './retention' import { reconcile } from './reconcile' import { egressSecretHosts, secretPlaceholder } from './secret-egress' import { SecretError, handleSecretsRoute, keyringFor, resolveSandboxSecrets } from './secrets' @@ -49,6 +66,9 @@ export type Deps = { now?: () => number egressFetch?: typeof fetch egressKeysForIssuer?: Parameters[1]['keysForIssuer'] + /** GitHub OAuth/user API fetch (tests). */ + githubFetch?: typeof fetch + retention?: RetentionConfig } // Decrypted model keys for the egress proxy, per sandbox session, for a minute: one D1 read and @@ -95,17 +115,52 @@ function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) => } } -const minters = new Map() +const minters = new Map() function githubMinter(env: Env): GitHubAppMinter | undefined { if (!env.GITHUB_APP_ID || !env.GITHUB_APP_PRIVATE_KEY) { return undefined } - let minter = minters.get(env.GITHUB_APP_ID) - if (!minter) { - minter = createGitHubAppMinter({ appId: env.GITHUB_APP_ID, privateKeyPkcs8: env.GITHUB_APP_PRIVATE_KEY }) - minters.set(env.GITHUB_APP_ID, minter) + let entry = minters.get(env.GITHUB_APP_ID) + if (!entry) { + const db = { current: env.SANDBOX_DB } + entry = { + db, + minter: createGitHubAppMinter({ + appId: env.GITHUB_APP_ID, + privateKeyPkcs8: env.GITHUB_APP_PRIVATE_KEY, + isBound: (installationId, accountId) => isInstallationBound(db.current, installationId, accountId) + }) + } + minters.set(env.GITHUB_APP_ID, entry) + } + entry.db.current = env.SANDBOX_DB + return entry.minter +} + +function githubConnect(env: Env, deps: Deps): GitHubConnectConfig | null { + if (!env.GITHUB_APP_SLUG || !env.GITHUB_APP_CLIENT_ID || !env.GITHUB_APP_CLIENT_SECRET || (env.GITHUB_STATE_SECRET?.length ?? 0) < 32) { + return null + } + return { + appSlug: env.GITHUB_APP_SLUG, + clientId: env.GITHUB_APP_CLIENT_ID, + clientSecret: env.GITHUB_APP_CLIENT_SECRET, + stateSecret: env.GITHUB_STATE_SECRET!, + fetch: deps.githubFetch + } +} + +function retentionFromEnv(env: Env): RetentionConfig { + const num = (value: string | undefined, fallback: number, unitMs: number) => { + const parsed = Number(value) + return value !== undefined && Number.isFinite(parsed) && parsed > 0 ? parsed * unitMs : fallback + } + return { + ...DEFAULT_RETENTION, + dataRetentionMs: num(env.DATA_RETENTION_DAYS, DEFAULT_RETENTION.dataRetentionMs, 86_400_000), + idleRetentionMs: num(env.IDLE_RETENTION_DAYS, DEFAULT_RETENTION.idleRetentionMs, 86_400_000), + orphanGraceMs: num(env.ORPHAN_GRACE_HOURS, DEFAULT_RETENTION.orphanGraceMs, 3_600_000) } - return minter } function egressCredentials(env: Env): CredentialBroker { @@ -118,7 +173,7 @@ function egressCredentials(env: Env): CredentialBroker { if (host !== 'api.github.com' || !row.repo_url) { return null } - const minted = await minter.tokenForRepo(row.repo_url) + const minted = await minter.tokenForRepo(row.repo_url, row.account_id) return minted ? `Bearer ${minted.token}` : null } } @@ -146,8 +201,11 @@ function githubManifestStart(url: URL): Response { // Installable on personal accounts too; installing grants nothing until a repo is selected. public: true, hook_attributes: { url: `${origin}/v1/github/webhook-unused`, active: false }, - default_permissions: { contents: 'write', metadata: 'read', pull_requests: 'write' }, + // members:read lets the bind step confirm an org installer is an org admin (user-to-server token). + default_permissions: { contents: 'write', metadata: 'read', pull_requests: 'write', members: 'read' }, default_events: [], + // The setup URL sends the browser through OAuth itself, so existing installs can be bound too. + callback_urls: [`${origin}/v1/github/callback`], request_oauth_on_install: false } const state = crypto.randomUUID() @@ -221,7 +279,13 @@ async function lifecycleDeps(env: Env, deps: Deps): Promise= 1 + ? Math.floor(Number(env.SNAPSHOT_EXPIRATION_DAYS)) * 86_400_000 + : undefined, environment: env.SANDBOX_ENV, goldenSnapshotId: env.GOLDEN_SNAPSHOT_ID, defaultRegion: env.VERCEL_REGION ?? 'iad1', @@ -268,8 +332,45 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) if (url.pathname === '/v1/github/manifest-start' && env.SANDBOX_ENV !== 'prod') { return githubManifestStart(url) } - if (url.pathname === '/v1/github/setup') { - return html('Orca Cloud (dev) is installed. You can close this tab.') + if (url.pathname === '/v1/github/setup' || url.pathname === '/v1/github/callback') { + return await githubReturn(env, deps, url) + } + if (url.pathname.startsWith('/v1/internal/')) { + if (!(await isOperator(request, env.OPERATOR_TOKEN))) { + return fail('not_found', 404) + } + const life = await lifecycleDeps(env, deps) + if (!life) { + return fail('not_configured', 503) + } + return ( + (await handleOperator(request, url.pathname, { + life, + github: deps.github ?? githubMinter(env), + readJson: () => readJson(request) + })) ?? fail('not_found', 404) + ) + } + if (url.pathname === '/v1/github/connect' || url.pathname === '/v1/github/installations') { + const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)()) + if (!caller) { + return fail('unauthorized', 401) + } + if (url.pathname === '/v1/github/installations' && request.method === 'GET') { + const rows = await listAccountInstallations(env.SANDBOX_DB, caller.accountId) + return json({ + installations: rows.map((r) => ({ installationId: r.installation_id, account: r.github_account_login, accountType: r.github_account_type, boundAt: r.created_at })) + }) + } + const connect = githubConnect(env, deps) + if (url.pathname !== '/v1/github/connect' || request.method !== 'POST') { + return fail('not_found', 404) + } + if (!connect) { + return fail('not_configured', 503) + } + const state = await createConnectState(env.SANDBOX_DB, connect.stateSecret, caller, (deps.now ?? Date.now)()) + return json({ installUrl: installUrl(connect, state), authorizeUrl: authorizeUrl(connect, url.origin, state) }) } if (url.pathname === '/v1/secrets' || url.pathname.startsWith('/v1/secrets/')) { const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)()) @@ -303,6 +404,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) if (request.method !== 'POST') { return fail('not_found', 404) } + await consumeCreateToken(life.db, life.config.guardrails!, caller.accountId, life.now()) const body = CreateRequestSchema.safeParse(await readJson(request)) if (!body.success) { return fail('invalid_request', 400) @@ -346,6 +448,38 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {}) } } +async function githubReturn(env: Env, deps: Deps, url: URL): Promise { + const connect = githubConnect(env, deps) + if (!connect) { + // Not configured for binding: an install alone grants nothing. + return html('Orca Cloud is installed on GitHub. Connect it from Orca to use it with your workspaces.') + } + try { + const result = await handleGitHubReturn(env.SANDBOX_DB, connect, url, (deps.now ?? Date.now)()) + if ('redirect' in result) { + return new Response(null, { status: 302, headers: { location: result.redirect, 'cache-control': 'no-store' } }) + } + if ('error' in result) { + return html( + result.error === 'state_used' + ? 'This link was already used. Start again from Orca.' + : 'GitHub is installed, but this page was not opened from Orca. Start from Orca to connect it to your account.' + ) + } + const esc = (value: string) => value.replace(/[^A-Za-z0-9_.-]/g, '') + return html( + result.bound.length > 0 + ? `Connected GitHub ${result.bound.map((login) => `${esc(login)}`).join(', ')} to Orca. You can close this tab.` + : 'Nothing was connected: only the owner of a personal account or an organization admin can connect an installation.' + ) + } catch (error) { + if (error instanceof GitHubConnectError) { + return html('GitHub did not confirm the sign-in. Start again from Orca.') + } + throw error + } +} + function errorResponse(error: unknown): Response { if (error instanceof SyntaxError) { return fail('invalid_json', 400) @@ -353,6 +487,17 @@ function errorResponse(error: unknown): Response { if (error instanceof SecretError) { return fail(error.code, error.status, error.reason) } + if (error instanceof GuardrailError) { + if (error.code === 'concurrency_limit') { + return fail('entitlement_denied', 403, 'concurrency_limit') + } + const status = { maintenance: 503, capacity: 503, rate_limited: 429 }[error.code] + const response = fail(error.code, status) + if (error.retryAfterSeconds) { + response.headers.set('retry-after', String(error.retryAfterSeconds)) + } + return response + } if (error instanceof LifecycleError) { const status = { not_found: 404, conflict: 409, entitlement_denied: 403, provider_unavailable: 502 }[error.code] return fail(error.code, status, error.reason) diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 1754da3a6af..8d70a55fae0 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -2,7 +2,16 @@ // a forced stop before the provider's 24h cap, usage metering and write-ahead rows. import type { EntitlementClient, MachineSize } from './entitlements' import type { GitHubAppMinter } from './github-app' +import { + DEFAULT_GUARDRAILS, + GuardrailError, + assertAccountCapacity, + assertStartsOpen, + killSwitchOn, + type GuardrailConfig +} from './guardrails' import { compileNetworkPolicy, gitBasicAuthorization, type CredentialRoute } from './network-policy' +import type { RetentionConfig } from './retention' import type { ProviderSandboxState, SandboxProviderApi } from './sandbox-provider' import { applySecretPlaceholders, egressSecretHosts, secretPlaceholder } from './secret-egress' import { resolveSandboxSecrets } from './secrets' @@ -29,6 +38,9 @@ const PROVIDER_SESSION_CAP_MS = 24 * 60 * 60_000 /** Orca stops 10 min early so it, not the provider, ends the session (drain first). */ export const SESSION_DEADLINE_MS = PROVIDER_SESSION_CAP_MS - 10 * 60_000 const VCPUS: Record = { standard: 4, large: 8 } +const DAY_MS = 24 * 60 * 60_000 +/** Provider-side backstop: longer than idle retention (90d) + its warning, so Orca deletes first. */ +export const DEFAULT_SNAPSHOT_EXPIRATION_MS = 104 * DAY_MS export type LifecycleDeps = { db: D1Database @@ -39,6 +51,8 @@ export type LifecycleDeps = { /** Secret store; absent (no SECRETS_KEK) means sessions start without injected secrets. */ secrets?: { keyring: Keyring; egressPlaceholders: boolean } now: () => number + /** Orphan/retention policy for the cron; defaults in retention.ts. */ + retention?: RetentionConfig config: { environment: string goldenSnapshotId: string @@ -46,6 +60,8 @@ export type LifecycleDeps = { egressProxyUrl?: string /** Staging rehearsals shorten the cap; production uses SESSION_DEADLINE_MS. */ sessionDeadlineMs?: number + guardrails?: GuardrailConfig + snapshotExpirationMs?: number } } @@ -136,7 +152,12 @@ async function claim(deps: LifecycleDeps, row: SandboxRow, patch: Partial { @@ -168,6 +190,7 @@ async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandbox const running = await claim(deps, row, { status: 'running', last_error: null, + retention_warned_at: null, github_token_expires_at: git?.expiresAt ?? null, egress_secret_hosts: routed.egress_secret_hosts, ...sessionFields(deps, state) @@ -201,7 +224,7 @@ export async function refreshGitCredentials( if (!deps.github || !row.repo_url) { return null } - const minted = await deps.github.tokenForRepo(row.repo_url) + const minted = await deps.github.tokenForRepo(row.repo_url, row.account_id) if (!minted) { return null } @@ -253,6 +276,7 @@ export async function createSandbox( 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 } }) } catch (error) { @@ -366,6 +390,9 @@ export async function rolloverSandbox(deps: LifecycleDeps, row: SandboxRow): Pro if (row.status !== 'running') { throw new LifecycleError('conflict', `sandbox_${row.status}`) } + if (await killSwitchOn(deps.db, guardrails(deps))) { + throw new GuardrailError('maintenance') + } const suspending = await claim(deps, row, { status: 'suspending' }) const stopped = await stopForSuspend(deps, suspending) await flushUsage(deps.db, deps.entitlements, deps.now(), row.id) @@ -410,7 +437,13 @@ export async function destroySandbox(deps: LifecycleDeps, row: SandboxRow): Prom throw new LifecycleError('provider_unavailable', 'snapshot_cleanup_incomplete') } await markSnapshotsDeleted(deps.db, row.id, deps.now()) - const destroyed = await claim(deps, destroying, { status: 'destroyed', ...NO_SESSION, destroyed_at: deps.now() }) + const destroyed = await claim(deps, destroying, { + status: 'destroyed', + ...NO_SESSION, + destroyed_at: deps.now(), + // Tombstone: the row stays for usage/audit, the workspace's repository does not. + repo_url: null + }) await flushUsage(deps.db, deps.entitlements, deps.now(), row.id) return destroyed } diff --git a/cloud/apps/sandbox-control/src/operator.ts b/cloud/apps/sandbox-control/src/operator.ts new file mode 100644 index 00000000000..b23767bc5e9 --- /dev/null +++ b/cloud/apps/sandbox-control/src/operator.ts @@ -0,0 +1,117 @@ +// Operator-only routes (Bearer OPERATOR_TOKEN, a Worker secret; absent = routes do not exist): +// GET /v1/internal/status +// POST /v1/internal/kill-switch {enabled, drain?} refuse creates/resumes; drain suspends (snapshot kept) +// POST /v1/internal/drain-all +// POST /v1/internal/housekeeping orphan sweep + retention now +// POST /v1/internal/github/bindings {installationId, accountId} support binding (audited as `operator`) +// DELETE /v1/internal/github/bindings {installationId, accountId?} +import { z } from 'zod' +import { readBearer } from './auth' +import type { GitHubAppMinter } from './github-app' +import { bindInstallation, revokeInstallation, timingSafeEqual } from './github-connect' +import { countAllActive, getControlState, killSwitchOn, setKillSwitch } from './guardrails' +import { guardrails, type LifecycleDeps } from './lifecycle' +import { drainAll, housekeeping } from './retention' +import { listByStatus } from './store' + +const KillSwitchBody = z.object({ enabled: z.boolean(), drain: z.boolean().optional() }).strict() +const AccountId = z.string().min(1).max(160).regex(/^(user|org|dev):[A-Za-z0-9_.:-]+$/) +const BindingBody = z + .object({ + installationId: z.number().int().positive().optional(), + /** Alternative to installationId: the installation covering this github.com repo. */ + repoUrl: z.url({ protocol: /^https$/ }).max(2048).optional(), + accountId: AccountId + }) + .strict() + .refine((body) => (body.installationId === undefined) !== (body.repoUrl === undefined)) +const UnbindBody = z.object({ installationId: z.number().int().positive(), accountId: AccountId.optional() }).strict() + +async function digest(value: string): Promise { + return new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value))) +} + +export async function isOperator(request: Request, operatorToken: string | undefined): Promise { + const presented = readBearer(request.headers.get('authorization')) + if (!operatorToken || operatorToken.length < 32 || !presented) { + return false + } + return timingSafeEqual(await digest(presented), await digest(operatorToken)) +} + +export async function handleOperator( + request: Request, + path: string, + deps: { life: LifecycleDeps; github?: GitHubAppMinter; readJson: () => Promise } +): Promise { + const { life } = deps + const now = life.now() + const route = `${request.method} ${path}` + switch (route) { + case 'GET /v1/internal/status': + return Response.json({ + killSwitch: await killSwitchOn(life.db, guardrails(life)), + staticDisabled: guardrails(life).startsDisabled, + active: await countAllActive(life.db), + running: (await listByStatus(life.db, ['running'])).length, + guardrails: guardrails(life), + lastHousekeepingAt: Number((await getControlState(life.db, 'housekeeping_at')) ?? 0) || null + }) + case 'POST /v1/internal/kill-switch': { + const body = KillSwitchBody.safeParse(await deps.readJson()) + if (!body.success) { + return Response.json({ error: 'invalid_request' }, { status: 400 }) + } + await setKillSwitch(life.db, body.data.enabled, now) + console.log(JSON.stringify({ event: 'kill_switch', enabled: body.data.enabled })) + const drained = body.data.enabled && body.data.drain !== false ? await drainAll(life) : null + return Response.json({ killSwitch: body.data.enabled, drained }) + } + case 'POST /v1/internal/drain-all': + return Response.json({ drained: await drainAll(life) }) + case 'POST /v1/internal/housekeeping': + return Response.json({ housekeeping: await housekeeping(life, { force: true }) }) + case 'POST /v1/internal/github/bindings': { + const body = BindingBody.safeParse(await deps.readJson()) + if (!body.success) { + return Response.json({ error: 'invalid_request' }, { status: 400 }) + } + if (!deps.github) { + return Response.json({ error: 'not_configured' }, { status: 503 }) + } + const installation = body.data.installationId + ? await deps.github.getInstallation(body.data.installationId) + : await deps.github.getRepoInstallation(body.data.repoUrl!) + if (!installation) { + return Response.json({ error: 'not_found' }, { status: 404 }) + } + await bindInstallation( + life.db, + { + installation_id: installation.id, + account_id: body.data.accountId, + github_account_id: installation.account.id, + github_account_login: installation.account.login, + github_account_type: installation.account.type, + bound_by_user_id: 'operator', + bound_by_github_user_id: null, + method: 'operator' + }, + now + ) + console.log(JSON.stringify({ event: 'github_bind_operator', installationId: installation.id, accountId: body.data.accountId })) + return Response.json({ bound: { installationId: installation.id, accountId: body.data.accountId, githubAccount: installation.account.login } }) + } + case 'DELETE /v1/internal/github/bindings': { + const body = UnbindBody.safeParse(await deps.readJson()) + if (!body.success) { + return Response.json({ error: 'invalid_request' }, { status: 400 }) + } + const revoked = await revokeInstallation(life.db, body.data.installationId, body.data.accountId ?? null, now) + console.log(JSON.stringify({ event: 'github_unbind_operator', installationId: body.data.installationId, revoked })) + return Response.json({ revoked }) + } + default: + return null + } +} diff --git a/cloud/apps/sandbox-control/src/reconcile.ts b/cloud/apps/sandbox-control/src/reconcile.ts index 2f0613a354d..8dc08b323e7 100644 --- a/cloud/apps/sandbox-control/src/reconcile.ts +++ b/cloud/apps/sandbox-control/src/reconcile.ts @@ -11,6 +11,9 @@ import { suspendSandbox, type LifecycleDeps } from './lifecycle' +import { guardrails } from './lifecycle' +import { killSwitchOn } from './guardrails' +import { drainAll, housekeeping, openHold, type HousekeepingSummary } from './retention' import { listByStatus, transition, type SandboxRow } from './store' import { flushUsage, queueUsage } from './usage' @@ -19,6 +22,7 @@ const STUCK_MS = 15 * 60_000 const FORCED_STOP_GRACE_MS = 5 * 60_000 export type ReconcileSummary = Record< + | 'drained' | 'settled' | 'suspendedForDeadline' | 'suspendedForBilling' @@ -27,10 +31,11 @@ export type ReconcileSummary = Record< | 'repaired' | 'billingErrors', number -> +> & { housekeeping?: HousekeepingSummary } export async function reconcile(deps: LifecycleDeps): Promise { const summary: ReconcileSummary = { + drained: 0, settled: 0, suspendedForDeadline: 0, suspendedForBilling: 0, @@ -42,11 +47,19 @@ export async function reconcile(deps: LifecycleDeps): Promise const polled = await guard(async () => { for (const accountId of await deps.entitlements.listSuspensions()) { summary.suspendedForBilling += await suspendAccountRunning(deps, accountId) + // Payment grace expired: the data-retention clock starts (no-op if already running). + await openHold(deps.db, accountId, 'payment_grace_expired', deps.now()) } }) if (!polled) { summary.billingErrors++ } + if (await killSwitchOn(deps.db, guardrails(deps))) { + // Kill switch on: keep draining until nothing runs (a drain call may have timed out mid-way). + await guard(async () => { + summary.drained += (await drainAll(deps)).suspended + }) + } for (const row of await listByStatus(deps.db, ['running'])) { await guard(() => reconcileRunning(deps, row, summary)) } @@ -69,6 +82,12 @@ export async function reconcile(deps: LifecycleDeps): Promise }) } await flushUsage(deps.db, deps.entitlements, deps.now()) + await guard(async () => { + const housekept = await housekeeping(deps) + if (housekept.swept) { + summary.housekeeping = housekept + } + }) return summary } diff --git a/cloud/apps/sandbox-control/src/retention.ts b/cloud/apps/sandbox-control/src/retention.ts new file mode 100644 index 00000000000..1b3f01ebd02 --- /dev/null +++ b/cloud/apps/sandbox-control/src/retention.ts @@ -0,0 +1,399 @@ +// Hourly cron housekeeping: provider-vs-D1 orphan sweep, data deletion after cancel/unpaid, and idle +// retention for long-suspended sandboxes. Every destructive step is two-phase (seen, then acted on +// after a grace period) and logged; partial provider listings never delete anything. +import { RETENTION_HOLD_REASONS } from './entitlements' +import { destroySandbox, suspendSandbox, type LifecycleDeps } from './lifecycle' +import { getControlState, setControlState } from './guardrails' +import { ACTIVE_STATUSES, getSandboxByProviderName, listByStatus, type SandboxRow } from './store' + +const HOUR_MS = 60 * 60_000 +const DAY_MS = 24 * HOUR_MS + +export type RetentionConfig = { + /** Data kept this long after an account goes canceled/unpaid, then destroyed. */ + dataRetentionMs: number + /** Suspended sandboxes untouched this long are destroyed (active subscriptions too). */ + idleRetentionMs: number + /** Warning hook fires this long before either deletion. */ + warningLeadMs: number + /** Orphans are suspended on first sight and deleted once seen for this long. */ + orphanGraceMs: number + /** Housekeeping cadence inside the 5-minute cron. */ + sweepIntervalMs: number + /** Re-ask billing about an account's standing at most this often. */ + holdProbeIntervalMs: number +} + +export const DEFAULT_RETENTION: RetentionConfig = { + dataRetentionMs: 30 * DAY_MS, + idleRetentionMs: 90 * DAY_MS, + warningLeadMs: 7 * DAY_MS, + orphanGraceMs: 24 * HOUR_MS, + sweepIntervalMs: HOUR_MS, + holdProbeIntervalMs: DAY_MS +} + +export type RetentionWarning = { + kind: 'data_deletion' | 'idle_deletion' + accountId: string + sandboxIds: string[] + deleteAt: number + reason: string +} + +/** Email hook. Stubbed: logs a structured event the alerting Worker / mailer can pick up. */ +export type RetentionNotifier = { warn(warning: RetentionWarning): Promise } +export const LOG_NOTIFIER: RetentionNotifier = { + async warn(warning) { + console.log(JSON.stringify({ event: 'retention_warning', ...warning })) + } +} + +export type HousekeepingSummary = { + swept: boolean + orphanSandboxesSuspended: number + orphanSandboxesDeleted: number + orphanSnapshotsSeen: number + orphanSnapshotsDeleted: number + rowsMissingProvider: number + holdsOpened: number + holdsCleared: number + retentionWarnings: number + retentionDestroyed: number + idleWarnings: number + idleDestroyed: number +} + +function log(event: string, fields: Record) { + console.log(JSON.stringify({ event, ...fields })) +} + +function retention(deps: LifecycleDeps): RetentionConfig { + return deps.retention ?? DEFAULT_RETENTION +} + +// ---- account holds (canceled / unpaid) ---- + +type HoldRow = { account_id: string; reason: string; since: number; checked_at: number; warned_at: number | null; purged_at: number | null } + +/** Records that billing says this account is canceled/unpaid; the retention clock starts at first sight. */ +export async function openHold(db: D1Database, accountId: string, reason: string, now: number): Promise { + const existing = await db.prepare('SELECT * FROM account_holds WHERE account_id = ?').bind(accountId).first() + if (existing && existing.reason !== 'active') { + await db.prepare('UPDATE account_holds SET checked_at = ? WHERE account_id = ?').bind(now, accountId).run() + return false + } + await db + .prepare( + `INSERT INTO account_holds (account_id, reason, since, checked_at) VALUES (?, ?, ?, ?) + ON CONFLICT (account_id) DO UPDATE SET reason = excluded.reason, since = excluded.since, + checked_at = excluded.checked_at, warned_at = NULL, purged_at = NULL` + ) + .bind(accountId, reason, now, now) + .run() + log('retention_hold_opened', { accountId, reason }) + return true +} + +/** Billing allows starts again (resubscribed / paid): the hold and its clock are dropped. */ +async function clearHold(db: D1Database, accountId: string, now: number): Promise { + const existing = await db.prepare('SELECT * FROM account_holds WHERE account_id = ?').bind(accountId).first() + await db + .prepare( + `INSERT INTO account_holds (account_id, reason, since, checked_at) VALUES (?, 'active', ?, ?) + ON CONFLICT (account_id) DO UPDATE SET reason = 'active', since = excluded.since, checked_at = excluded.checked_at, + warned_at = NULL, purged_at = NULL` + ) + .bind(accountId, now, now) + .run() + return existing !== null && existing.reason !== 'active' +} + +/** Accounts with stored sandboxes whose standing has not been checked recently. */ +async function accountsToProbe(db: D1Database, before: number): Promise<{ account_id: string; machine_size: SandboxRow['machine_size'] }[]> { + const { results } = await db + .prepare( + `SELECT s.account_id, MIN(s.machine_size) AS machine_size FROM sandboxes s + LEFT JOIN account_holds h ON h.account_id = s.account_id + WHERE s.status <> 'destroyed' AND (h.checked_at IS NULL OR h.checked_at < ?) + GROUP BY s.account_id LIMIT 50` + ) + .bind(before) + .all<{ account_id: string; machine_size: SandboxRow['machine_size'] }>() + return results +} + +async function probeHolds(deps: LifecycleDeps, summary: HousekeepingSummary): Promise { + const now = deps.now() + for (const account of await accountsToProbe(deps.db, now - retention(deps).holdProbeIntervalMs)) { + try { + // runningSandboxes 0: we ask about the account's standing, not concurrency. + const decision = await deps.entitlements.checkStart({ accountId: account.account_id, machineSize: account.machine_size, runningSandboxes: 0 }) + if (!decision.allow && RETENTION_HOLD_REASONS.has(decision.reason ?? '')) { + summary.holdsOpened += (await openHold(deps.db, account.account_id, decision.reason!, now)) ? 1 : 0 + } else if (decision.allow) { + summary.holdsCleared += (await clearHold(deps.db, account.account_id, now)) ? 1 : 0 + } else { + // Other denials (hours exhausted, cap): not a cancellation; just remember we looked. + await deps.db.prepare('UPDATE account_holds SET checked_at = ? WHERE account_id = ?').bind(now, account.account_id).run() + } + } catch { + // Billing unavailable: never start a deletion clock on an outage. + } + } +} + +async function enforceHolds(deps: LifecycleDeps, notifier: RetentionNotifier, summary: HousekeepingSummary) { + const config = retention(deps) + const now = deps.now() + const { results } = await deps.db + .prepare(`SELECT * FROM account_holds WHERE reason <> 'active' AND purged_at IS NULL AND since <= ? LIMIT 100`) + .bind(now - (config.dataRetentionMs - config.warningLeadMs)) + .all() + for (const hold of results) { + const deleteAt = hold.since + config.dataRetentionMs + const rows = await liveRows(deps.db, hold.account_id) + if (hold.warned_at === null) { + await notifier.warn({ kind: 'data_deletion', accountId: hold.account_id, sandboxIds: rows.map((r) => r.id), deleteAt, reason: hold.reason }) + await deps.db.prepare('UPDATE account_holds SET warned_at = ? WHERE account_id = ?').bind(now, hold.account_id).run() + summary.retentionWarnings++ + continue + } + // Deletion waits for both the deadline and a full warning lead after the warning went out. + if (now < deleteAt || now < hold.warned_at + config.warningLeadMs) { + continue + } + let remaining = 0 + for (const row of rows) { + try { + await destroySandbox(deps, row) + summary.retentionDestroyed++ + } catch { + remaining++ + } + } + if (remaining === 0) { + await deps.db.prepare('UPDATE account_holds SET purged_at = ? WHERE account_id = ?').bind(now, hold.account_id).run() + } + log('retention_purge', { accountId: hold.account_id, destroyed: rows.length - remaining, remaining, reason: hold.reason }) + } +} + +async function liveRows(db: D1Database, accountId: string): Promise { + const { results } = await db + .prepare(`SELECT * FROM sandboxes WHERE account_id = ? AND status <> 'destroyed'`) + .bind(accountId) + .all() + return results +} + +// ---- idle retention ---- + +async function enforceIdle(deps: LifecycleDeps, notifier: RetentionNotifier, summary: HousekeepingSummary) { + const config = retention(deps) + const now = deps.now() + const { results } = await deps.db + .prepare(`SELECT * FROM sandboxes WHERE status = 'suspended' AND updated_at <= ? ORDER BY updated_at LIMIT 100`) + .bind(now - (config.idleRetentionMs - config.warningLeadMs)) + .all() + for (const row of results) { + if (row.retention_warned_at === null) { + await notifier.warn({ + kind: 'idle_deletion', + accountId: row.account_id, + sandboxIds: [row.id], + deleteAt: Math.max(row.updated_at + config.idleRetentionMs, now + config.warningLeadMs), + reason: 'idle' + }) + // Not a transition: the idle clock (updated_at) must not move. + await deps.db.prepare('UPDATE sandboxes SET retention_warned_at = ? WHERE id = ? AND generation = ?').bind(now, row.id, row.generation).run() + summary.idleWarnings++ + } else if (now >= row.updated_at + config.idleRetentionMs && now >= row.retention_warned_at + config.warningLeadMs) { + try { + await destroySandbox(deps, row) + summary.idleDestroyed++ + log('idle_retention_destroy', { sandboxId: row.id, accountId: row.account_id, idleDays: Math.floor((now - row.updated_at) / DAY_MS) }) + } catch { + // Retried next sweep. + } + } + } +} + +// ---- orphan sweep ---- + +async function upsertOrphan(db: D1Database, kind: 'sandbox' | 'snapshot', id: string, now: number) { + await db + .prepare( + `INSERT INTO provider_orphans (kind, provider_id, first_seen_at, last_seen_at) VALUES (?, ?, ?, ?) + ON CONFLICT (kind, provider_id) DO UPDATE SET last_seen_at = excluded.last_seen_at` + ) + .bind(kind, id, now, now) + .run() + return (await db + .prepare('SELECT * FROM provider_orphans WHERE kind = ? AND provider_id = ?') + .bind(kind, id) + .first<{ first_seen_at: number; suspended_at: number | null; deleted_at: number | null }>())! +} + +async function clearOrphan(db: D1Database, kind: string, id: string) { + await db.prepare('DELETE FROM provider_orphans WHERE kind = ? AND provider_id = ? AND deleted_at IS NULL').bind(kind, id).run() +} + +async function sweepOrphans(deps: LifecycleDeps, summary: HousekeepingSummary): Promise { + const now = deps.now() + const grace = retention(deps).orphanGraceMs + const env = deps.config.environment + const listing = await deps.provider.listSandboxes() + const providerNames = new Set(listing.sandboxes.map((s) => s.name)) + for (const box of listing.sandboxes) { + // Only this environment's sandboxes; golden-image build boxes and other envs are not ours to judge. + if (box.tags.orcaEnv !== env || !box.tags.orcaSandbox) { + continue + } + const row = await getSandboxByProviderName(deps.db, box.name) + if (row && row.status !== 'destroyed') { + await clearOrphan(deps.db, 'sandbox', box.name) + continue + } + const orphan = await upsertOrphan(deps.db, 'sandbox', box.name, now) + if (orphan.deleted_at !== null) { + continue + } + if (now - orphan.first_seen_at >= grace) { + await deps.provider.deleteSandbox(box.name) + for (const snapshot of await deps.provider.listSnapshots(box.name)) { + if (snapshot.status === 'created' && snapshot.id !== deps.config.goldenSnapshotId) { + await deps.provider.deleteSnapshot(snapshot.id) + } + } + await deps.db.prepare(`UPDATE provider_orphans SET deleted_at = ? WHERE kind = 'sandbox' AND provider_id = ?`).bind(now, box.name).run() + summary.orphanSandboxesDeleted++ + log('orphan_sandbox_deleted', { name: box.name, d1: row ? row.status : 'missing', firstSeenAt: orphan.first_seen_at }) + } else if (box.status === 'running' && orphan.suspended_at === null) { + // Stop the meter now; the automatic snapshot keeps the disk until the grace period ends. + await deps.provider.stopSession(box.currentSessionId) + await deps.db.prepare(`UPDATE provider_orphans SET suspended_at = ? WHERE kind = 'sandbox' AND provider_id = ?`).bind(now, box.name).run() + summary.orphanSandboxesSuspended++ + log('orphan_sandbox_suspended', { name: box.name, d1: row ? row.status : 'missing' }) + } + } + if (!listing.complete) { + log('orphan_sweep_partial', { listed: listing.sandboxes.length }) + return + } + // D1 says it exists, the provider does not: reported, never auto-repaired (wake fails cleanly). + for (const row of await listByStatus(deps.db, ['running', 'suspended'])) { + if (row.provider_name.startsWith(`orca-${env}-`) && !providerNames.has(row.provider_name) && now - row.updated_at > grace) { + summary.rowsMissingProvider++ + } + } + await sweepSnapshots(deps, listing.sandboxes.map((s) => s.name), summary) +} + +/** Automatic snapshots whose sandbox no longer exists anywhere in the project. Golden (manual) ones are never touched. */ +async function sweepSnapshots(deps: LifecycleDeps, sandboxNames: string[], summary: HousekeepingSummary) { + const now = deps.now() + const grace = retention(deps).orphanGraceMs + if (sandboxNames.length > 500) { + log('orphan_snapshot_sweep_skipped', { sandboxes: sandboxNames.length }) + return + } + const listed = await deps.provider.listProjectSnapshots() + if (!listed.complete) { + log('orphan_snapshot_sweep_partial', { listed: listed.snapshots.length }) + return + } + const owned = new Set([deps.config.goldenSnapshotId]) + for (const name of sandboxNames) { + for (const snapshot of await deps.provider.listSnapshots(name)) { + owned.add(snapshot.id) + } + } + const { results } = await deps.db + .prepare( + `SELECT ss.snapshot_id FROM sandbox_snapshots ss JOIN sandboxes s ON s.id = ss.sandbox_id + WHERE ss.deleted_at IS NULL AND s.status <> 'destroyed'` + ) + .all<{ snapshot_id: string }>() + for (const r of results) { + owned.add(r.snapshot_id) + } + for (const snapshot of listed.snapshots) { + if (snapshot.status !== 'created' || snapshot.creationMethod !== 'automatic' || owned.has(snapshot.id)) { + if (owned.has(snapshot.id)) { + await clearOrphan(deps.db, 'snapshot', snapshot.id) + } + continue + } + summary.orphanSnapshotsSeen++ + const orphan = await upsertOrphan(deps.db, 'snapshot', snapshot.id, now) + if (orphan.deleted_at === null && now - orphan.first_seen_at >= grace) { + await deps.provider.deleteSnapshot(snapshot.id) + await deps.db.prepare(`UPDATE provider_orphans SET deleted_at = ? WHERE kind = 'snapshot' AND provider_id = ?`).bind(now, snapshot.id).run() + summary.orphanSnapshotsDeleted++ + log('orphan_snapshot_deleted', { id: snapshot.id, createdAt: snapshot.createdAt, firstSeenAt: orphan.first_seen_at }) + } + } +} + +// ---- entry points ---- + +/** Kill switch drain: snapshot-preserving suspend of every running sandbox (one batch per call). */ +export async function drainAll(deps: LifecycleDeps): Promise<{ suspended: number; failed: number; remaining: number }> { + let suspended = 0 + let failed = 0 + for (const row of await listByStatus(deps.db, ['running'])) { + try { + await suspendSandbox(deps, row) + suspended++ + } catch { + failed++ + } + } + const remaining = (await listByStatus(deps.db, ACTIVE_STATUSES)).length + log('drain_all', { suspended, failed, remaining }) + return { suspended, failed, remaining } +} + +/** Runs at most once per sweep interval; each part is isolated so one failure does not stop the rest. */ +export async function housekeeping( + deps: LifecycleDeps, + options: { force?: boolean; notifier?: RetentionNotifier } = {} +): Promise { + const summary: HousekeepingSummary = { + swept: false, + orphanSandboxesSuspended: 0, + orphanSandboxesDeleted: 0, + orphanSnapshotsSeen: 0, + orphanSnapshotsDeleted: 0, + rowsMissingProvider: 0, + holdsOpened: 0, + holdsCleared: 0, + retentionWarnings: 0, + retentionDestroyed: 0, + idleWarnings: 0, + idleDestroyed: 0 + } + const now = deps.now() + const last = Number((await getControlState(deps.db, 'housekeeping_at')) ?? 0) + if (!options.force && now - last < retention(deps).sweepIntervalMs) { + return summary + } + await setControlState(deps.db, 'housekeeping_at', String(now), now) + summary.swept = true + const notifier = options.notifier ?? LOG_NOTIFIER + for (const step of [ + () => sweepOrphans(deps, summary), + () => probeHolds(deps, summary), + () => enforceHolds(deps, notifier, summary), + () => enforceIdle(deps, notifier, summary) + ]) { + try { + await step() + } catch (error) { + log('housekeeping_step_failed', { message: error instanceof Error ? error.message : 'unknown' }) + } + } + return summary +} diff --git a/cloud/apps/sandbox-control/src/sandbox-provider.ts b/cloud/apps/sandbox-control/src/sandbox-provider.ts index 270fefca426..49e720b0543 100644 --- a/cloud/apps/sandbox-control/src/sandbox-provider.ts +++ b/cloud/apps/sandbox-control/src/sandbox-provider.ts @@ -41,6 +41,22 @@ export type ProviderSnapshot = { sourceSessionId: string } +/** Project-wide listing entries for the orphan sweep. */ +export type ProviderSandboxSummary = { + name: string + status: ProviderSessionStatus + currentSessionId: string + tags: Record +} + +export type ProviderProjectSnapshot = { + id: string + status: 'created' | 'deleted' | 'failed' + /** `automatic` (session stop) | `manual` (golden images); absent on old snapshots. */ + creationMethod: string | null + createdAt: number +} + export type CreateSandboxParams = { name: string snapshotId: string @@ -50,6 +66,8 @@ export type CreateSandboxParams = { ports: number[] networkPolicy: NetworkPolicy tags: Record + /** Provider-side snapshot expiry after last use (backstop behind Orca's own retention); 0 = never. */ + snapshotExpirationMs: number } export type RunCommandParams = { @@ -76,6 +94,10 @@ export type SandboxProviderApi = { listSnapshots(name: string): Promise /** Resolves when already gone. */ deleteSnapshot(snapshotId: string): Promise + /** Every sandbox in Orca's project (all environments). `complete` is false when the listing was cut short. */ + listSandboxes(): Promise<{ sandboxes: ProviderSandboxSummary[]; complete: boolean }> + /** Every snapshot in Orca's project. */ + listProjectSnapshots(): Promise<{ snapshots: ProviderProjectSnapshot[]; complete: boolean }> } /** Provider failed or answered unexpectedly; the body is never surfaced to callers. */ diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index 7e13b8eed64..a052a109754 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -36,6 +36,7 @@ export type SandboxRow = { github_token_expires_at: number | null /** Comma list of model API hosts whose key the egress proxy swaps in this session. */ egress_secret_hosts: string | null + retention_warned_at: number | null } export type SessionFields = Pick< @@ -127,7 +128,7 @@ type Patch = Partial([ 'status', 'session_id', 'session_started_at', 'session_deadline_at', 'lease_expires_at', 'usage_settled_at', 'last_error', 'destroyed_at', 'github_token_expires_at', - 'egress_secret_hosts' + 'egress_secret_hosts', 'repo_url', 'retention_warned_at' ]) /** Applies `patch` only if nobody moved the row since `expected` was read; null on a lost race. */ diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts index c7c7b181928..ac0835a1f17 100644 --- a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -42,6 +42,14 @@ const Snapshot = z.looseObject({ status: z.enum(['created', 'deleted', 'failed']), sourceSessionId: z.string() }) +const ProjectSnapshot = Snapshot.extend({ creationMethod: z.string().optional(), createdAt: z.number() }) +const SandboxSummary = z.looseObject({ + name: z.string(), + status: Status, + currentSessionId: z.string(), + tags: z.record(z.string(), z.string()).optional() +}) +const MAX_LISTED = 5000 const ErrorBody = z.looseObject({ error: z.looseObject({ code: z.string().optional() }).optional() }) const CommandLine = z.looseObject({ command: z.looseObject({ exitCode: z.number().nullable() }) }) const OutputLine = z.looseObject({ stream: z.enum(['stdout', 'stderr']), data: z.string() }) @@ -129,8 +137,8 @@ export function createVercelSandboxApi( timeout: params.timeoutMs, ports: params.ports, persistent: true, - keepLastSnapshots: { count: 2 }, - snapshotExpiration: 0, + keepLastSnapshots: { count: 2, expiration: params.snapshotExpirationMs }, + snapshotExpiration: params.snapshotExpirationMs, networkPolicy: params.networkPolicy, tags: params.tags } @@ -247,6 +255,46 @@ export function createVercelSandboxApi( return snapshots.map((s) => ({ id: s.id, status: s.status, sourceSessionId: s.sourceSessionId })) }, + async listSandboxes() { + const sandboxes: z.infer[] = [] + let cursor: string | undefined + do { + const response = await request('GET', '/v2/sandboxes', { + query: { project: config.projectId, limit: '50', cursor } + }) + const page = await json( + z.looseObject({ sandboxes: z.array(SandboxSummary), pagination: z.looseObject({ next: z.string().nullable() }) }), + response + ) + sandboxes.push(...page.sandboxes) + cursor = page.pagination.next ?? undefined + } while (cursor && sandboxes.length < MAX_LISTED) + return { + complete: cursor === undefined, + sandboxes: sandboxes.map((s) => ({ name: s.name, status: s.status, currentSessionId: s.currentSessionId, tags: s.tags ?? {} })) + } + }, + + async listProjectSnapshots() { + const snapshots: z.infer[] = [] + let cursor: string | undefined + do { + const response = await request('GET', '/v2/sandboxes/snapshots', { + query: { project: config.projectId, limit: '50', cursor } + }) + const page = await json( + z.looseObject({ snapshots: z.array(ProjectSnapshot), pagination: z.looseObject({ next: z.string().nullable() }) }), + response + ) + snapshots.push(...page.snapshots) + cursor = page.pagination.next ?? undefined + } while (cursor && snapshots.length < MAX_LISTED) + return { + complete: cursor === undefined, + snapshots: snapshots.map((s) => ({ id: s.id, status: s.status, creationMethod: s.creationMethod ?? null, createdAt: s.createdAt })) + } + }, + deleteSnapshot: (snapshotId) => missingOk(() => request('DELETE', `/v2/sandboxes/snapshots/${encodeURIComponent(snapshotId)}`)) } diff --git a/cloud/apps/sandbox-control/test/fake-vercel.ts b/cloud/apps/sandbox-control/test/fake-vercel.ts index 583b2ffda37..43b974b5082 100644 --- a/cloud/apps/sandbox-control/test/fake-vercel.ts +++ b/cloud/apps/sandbox-control/test/fake-vercel.ts @@ -23,7 +23,7 @@ type FakeBox = { export type FakeVercel = SandboxProviderApi & { boxes: Map - snapshots: Map + snapshots: Map commands: { sessionId: string; command: string; env: Record }[] failStart: boolean failCreate: boolean @@ -58,7 +58,7 @@ export function createFakeVercel(now: () => number): FakeVercel { const stop = (box: FakeBox): string => { box.status = 'stopped' const id = next('snap') - snapshots.set(id, { id, status: 'created', sourceSessionId: box.sessionId, sandboxName: box.params.name }) + snapshots.set(id, { id, status: 'created', sourceSessionId: box.sessionId, sandboxName: box.params.name, creationMethod: 'automatic', createdAt: now() }) return id } @@ -150,6 +150,23 @@ export function createFakeVercel(now: () => number): FakeVercel { .filter((snapshot) => snapshot.sandboxName === name) .map(({ id, status, sourceSessionId }) => ({ id, status, sourceSessionId })) }, + async listSandboxes() { + return { + complete: true, + sandboxes: [...boxes.values()].map((box) => ({ + name: box.params.name, + status: box.status, + currentSessionId: box.sessionId, + tags: box.params.tags + })) + } + }, + async listProjectSnapshots() { + return { + complete: true, + snapshots: [...snapshots.values()].map(({ id, status, creationMethod, createdAt }) => ({ id, status, creationMethod, createdAt })) + } + }, async deleteSnapshot(snapshotId) { const snapshot = snapshots.get(snapshotId) if (snapshot) { diff --git a/cloud/apps/sandbox-control/test/github-app.test.ts b/cloud/apps/sandbox-control/test/github-app.test.ts index ff41cd639f8..e56e8e24beb 100644 --- a/cloud/apps/sandbox-control/test/github-app.test.ts +++ b/cloud/apps/sandbox-control/test/github-app.test.ts @@ -1,53 +1,313 @@ import { exportPKCS8, generateKeyPair, decodeJwt } from 'jose' import { describe, expect, it } from 'vitest' import { createGitHubAppMinter } from '../src/github-app' +import { bindInstallation, isInstallationBound, signState, verifyState } from '../src/github-connect' import { gitBasicAuthorization } from '../src/network-policy' import { START_COMMAND } from '../src/sandbox-vm-commands' import { createHarness } from './harness' const MIN = 60_000 +const REPO = 'https://github.com/alice-gh/private-repo.git' +const STATE_SECRET = 's'.repeat(40) +const OPERATOR = 'o'.repeat(40) + +type Call = { method: string; path: string; body: unknown; auth: string | null } + +/** Fake api.github.com + github.com OAuth: alice-gh (id 1) owns installation 77 on her personal account. */ +function fakeGitHub(options: { now: () => number; users?: Record }> }) { + const calls: Call[] = [] + const users = options.users ?? { + code_alice: { id: 1, login: 'alice-gh', installations: [{ id: 77, account: { id: 1, login: 'alice-gh', type: 'User' } }] }, + code_mallory: { id: 666, login: 'mallory-gh', installations: [] } + } + const tokens = new Map() + const fakeFetch = (async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(String(input)) + const auth = new Headers(init?.headers).get('authorization') + const body = init?.body ? JSON.parse(String(init.body)) : undefined + calls.push({ method: init?.method ?? 'GET', path: url.pathname, body, auth }) + if (url.hostname === 'github.com' && url.pathname === '/login/oauth/access_token') { + const user = users[body.code] + if (!user) { + return Response.json({ error: 'bad_verification_code' }) + } + const token = `gho_${body.code}` + tokens.set(token, body.code) + return Response.json({ access_token: token }) + } + const userToken = auth?.startsWith('Bearer gho_') ? users[tokens.get(auth.slice(7)) ?? ''] : undefined + if (url.pathname === '/user') { + return userToken ? Response.json({ id: userToken.id, login: userToken.login }) : new Response(null, { status: 401 }) + } + if (url.pathname === '/user/installations') { + return Response.json({ total_count: userToken!.installations.length, installations: userToken!.installations }) + } + if (url.pathname.startsWith('/user/memberships/orgs/')) { + const membership = userToken?.memberships?.[url.pathname.split('/').pop()!] + return membership ? Response.json(membership) : new Response(null, { status: 404 }) + } + // App-JWT routes: the repo is installed under installation 77 regardless of who asks. + if (url.pathname === '/repos/alice-gh/private-repo/installation' || url.pathname === '/app/installations/77') { + return Response.json({ id: 77, account: { id: 1, login: 'alice-gh', type: 'User' } }) + } + if (url.pathname === '/app/installations/77/access_tokens') { + return Response.json({ + token: `ghs_${calls.length}`, + expires_at: new Date(options.now() + 60 * MIN).toISOString(), + repositories: body.repositories.map((name: string) => ({ id: 9, name })) + }) + } + return new Response(null, { status: 404 }) + }) as typeof globalThis.fetch + return { calls, fetch: fakeFetch } +} + +async function appKey() { + const { privateKey } = await generateKeyPair('RS256', { extractable: true }) + return exportPKCS8(privateKey) +} + +async function harnessWithApp() { + let clock = () => 0 + const github = fakeGitHub({ now: () => clock() }) + const pem = await appKey() + let dbRef: D1Database | null = null + const minter = createGitHubAppMinter({ + appId: '123', + privateKeyPkcs8: pem, + fetch: github.fetch, + now: () => clock(), + isBound: (id, account) => isInstallationBound(dbRef!, id, account), + log: () => undefined + }) + const h = await createHarness( + { + GITHUB_APP_SLUG: 'orca-cloud-test', + GITHUB_APP_CLIENT_ID: 'Iv1.test', + GITHUB_APP_CLIENT_SECRET: 'client-secret', + GITHUB_STATE_SECRET: STATE_SECRET, + OPERATOR_TOKEN: OPERATOR, + EGRESS_PROXY_URL: 'https://sandbox.test/v1/egress/proxy' + }, + { github: minter, githubFetch: github.fetch } + ) + clock = h.now + dbRef = h.env.SANDBOX_DB + return { h, github, minter } +} + +async function browser(h: Awaited>, url: string) { + const response = await h.send(new Request(url)) + return { status: response.status, location: response.headers.get('location'), text: await response.text() } +} + +/** Desktop asks for a connect URL, the browser installs + signs in on GitHub as `code`'s user. */ +async function connect(h: Awaited>, as: string, code: string, installationId = 77) { + const { body } = await h.call('POST', '/v1/github/connect', { as }) + const state = new URL(body.installUrl).searchParams.get('state')! + const setup = await browser(h, `https://sandbox.test/v1/github/setup?installation_id=${installationId}&setup_action=install&state=${encodeURIComponent(state)}`) + expect(setup.status).toBe(302) + const authorize = new URL(setup.location!) + expect(authorize.origin + authorize.pathname).toBe('https://github.com/login/oauth/authorize') + expect(authorize.searchParams.get('redirect_uri')).toBe('https://sandbox.test/v1/github/callback') + const carried = authorize.searchParams.get('state')! + const callback = await browser(h, `https://sandbox.test/v1/github/callback?code=${code}&state=${encodeURIComponent(carried)}`) + return { state, carried, callback, body } +} describe('GitHub App installation tokens', () => { - it('mints a token scoped to the one workspace repo and reuses it until near expiry', async () => { - const { privateKey } = await generateKeyPair('RS256', { extractable: true }) - const calls: { method: string; path: string; body: unknown; iss: unknown }[] = [] + it('mints a single-repo token only for a bound account, and re-checks the binding on cached tokens', async () => { let now = Date.parse('2026-11-09T00:00:00Z') + const github = fakeGitHub({ now: () => now }) + const bound = new Set(['77:user:alice']) const minter = createGitHubAppMinter({ appId: '123', - privateKeyPkcs8: await exportPKCS8(privateKey), + privateKeyPkcs8: await appKey(), now: () => now, - fetch: async (input, init) => { - const path = new URL(String(input)).pathname - const jwt = new Headers(init?.headers).get('authorization')!.replace('Bearer ', '') - calls.push({ method: init?.method ?? 'GET', path, body: init?.body ? JSON.parse(String(init.body)) : undefined, iss: decodeJwt(jwt).iss }) - return path.endsWith('/installation') - ? Response.json({ id: 77 }) - : Response.json({ token: 'ghs_minted', expires_at: new Date(now + 60 * MIN).toISOString() }) + fetch: github.fetch, + isBound: async (id, account) => bound.has(`${id}:${account}`), + log: () => undefined + }) + const token = await minter.tokenForRepo(REPO, 'user:alice') + expect(token).toMatchObject({ expiresAt: now + 60 * MIN }) + expect(github.calls.map((c) => [c.method, c.path, c.body])).toEqual([ + ['GET', '/repos/alice-gh/private-repo/installation', undefined], + [ + 'POST', + '/app/installations/77/access_tokens', + { repositories: ['private-repo'], permissions: { contents: 'write', pull_requests: 'write', metadata: 'read' } } + ] + ]) + expect(decodeJwt(github.calls[0]!.auth!.slice(7)).iss).toBe('123') + + // Cross-tenant: another account naming the same repo gets nothing, and no token is minted. + expect(await minter.tokenForRepo(REPO, 'user:bob')).toBeNull() + expect(github.calls.filter((c) => c.path.endsWith('/access_tokens'))).toHaveLength(1) + + now += 30 * MIN + expect(await minter.tokenForRepo('https://github.com/alice-gh/private-repo', 'user:alice')).toEqual(token) + bound.delete('77:user:alice') + expect(await minter.tokenForRepo(REPO, 'user:alice')).toBeNull() + + expect(await minter.tokenForRepo('https://gitlab.com/a/b', 'user:alice')).toBeNull() + expect(await minter.tokenForRepo('https://x:y@github.com/alice-gh/private-repo', 'user:alice')).toBeNull() + }) + + it('refuses a minted token that covers more than the one repository', async () => { + const minter = createGitHubAppMinter({ + appId: '123', + privateKeyPkcs8: await appKey(), + isBound: async () => true, + fetch: (async (input: RequestInfo | URL) => + String(input).endsWith('/installation') + ? Response.json({ id: 77, account: { id: 1, login: 'alice-gh', type: 'User' } }) + : Response.json({ token: 'ghs_wide', expires_at: new Date().toISOString(), repositories: [{ id: 1, name: 'private-repo' }, { id: 2, name: 'other' }] })) as typeof fetch + }) + await expect(minter.tokenForRepo(REPO, 'user:alice')).rejects.toThrow('unexpected scope') + }) + + it('cross-tenant attack: bob names alice’s repo and gets no credential and no token is minted', async () => { + const { h, github } = await harnessWithApp() + await bindInstallation( + h.env.SANDBOX_DB, + { + installation_id: 77, + account_id: 'user:alice', + github_account_id: 1, + github_account_login: 'alice-gh', + github_account_type: 'User', + bound_by_user_id: 'alice', + bound_by_github_user_id: 1, + method: 'oauth' + }, + h.now() + ) + const res = await h.call('POST', '/v1/sandboxes', { as: 'bob', body: { workspaceId: 'ws-steal', machineSize: 'standard', repo: { url: REPO } } }) + expect(res.status).toBe(200) + const [box] = [...h.vercel.boxes.values()] + expect(box!.policy.allow['github.com'] ?? []).toEqual([]) + expect(h.vercel.commands.find((c) => c.command === START_COMMAND)!.env.ORCA_GIT_EXTRAHEADER).toBeUndefined() + expect(github.calls.filter((c) => c.path.endsWith('/access_tokens'))).toHaveLength(0) + + // Alice, bound, gets her token. + const mine = await h.call('POST', '/v1/sandboxes', { as: 'alice', body: { workspaceId: 'ws-mine', machineSize: 'standard', repo: { url: REPO } } }) + const aliceBox = h.vercel.boxes.get(`orca-test-${mine.body.sandbox.id}`)! + expect(aliceBox.policy.allow['github.com']![0]).toMatchObject({ + transform: [{ headers: { authorization: expect.stringMatching(/^Basic /) } }] + }) + expect(github.calls.filter((c) => c.path.endsWith('/access_tokens'))).toHaveLength(1) + }) + + it('binds an installation only after GitHub proves the installer owns it, single use', async () => { + const { h, minter } = await harnessWithApp() + const { state, carried, callback, body } = await connect(h, 'alice', 'code_alice') + expect(body.installUrl).toMatch(/^https:\/\/github\.com\/apps\/orca-cloud-test\/installations\/new\?state=/) + expect(callback.text).toContain('Connected GitHub alice-gh') + expect(await minter.tokenForRepo(REPO, 'user:alice')).not.toBeNull() + expect(await minter.tokenForRepo(REPO, 'user:bob')).toBeNull() + expect((await h.call('GET', '/v1/github/installations', { as: 'alice' })).body.installations).toMatchObject([ + { installationId: 77, account: 'alice-gh', accountType: 'User' } + ]) + expect((await h.call('GET', '/v1/github/installations', { as: 'bob' })).body.installations).toEqual([]) + + // Replaying the callback (or the original state) binds nothing more. + const replay = await browser(h, `https://sandbox.test/v1/github/callback?code=code_alice&state=${encodeURIComponent(carried)}`) + expect(replay.text).toContain('already used') + expect(state).not.toBe(carried) + }) + + it('attack: mallory starts her own connect and points the setup URL at alice’s installation id', async () => { + const { h, minter } = await harnessWithApp() + const { callback } = await connect(h, 'mallory', 'code_mallory', 77) + expect(callback.text).toContain('Nothing was connected') + expect(await minter.tokenForRepo(REPO, 'user:mallory')).toBeNull() + expect(h.raw.prepare('SELECT COUNT(*) AS n FROM github_installations').get()).toEqual({ n: 0 }) + }) + + it('attack: a forged or tampered state (another account, no signature) is refused', async () => { + const { h } = await harnessWithApp() + const { body } = await h.call('POST', '/v1/github/connect', { as: 'mallory' }) + const state = new URL(body.installUrl).searchParams.get('state')! + const payload = await verifyState(STATE_SECRET, state, h.now()) + const [bodyPart, mac] = state.split('.') + const forged = `${btoa(JSON.stringify({ ...payload, a: 'user:alice' })).replace(/=+$/, '')}.${mac}` + expect(bodyPart).not.toBe(forged.split('.')[0]) + for (const bad of [forged, await signState('x'.repeat(40), payload!), 'garbage']) { + const res = await browser(h, `https://sandbox.test/v1/github/callback?code=code_alice&state=${encodeURIComponent(bad)}`) + expect(res.text).toContain('not opened from Orca') + } + // A validly signed state whose nonce belongs to another account is refused too. + const crossed = await signState(STATE_SECRET, { ...payload!, a: 'user:alice' }) + const res = await browser(h, `https://sandbox.test/v1/github/callback?code=code_alice&state=${encodeURIComponent(crossed)}`) + expect(res.text).toContain('already used') + expect(h.raw.prepare('SELECT COUNT(*) AS n FROM github_installations').get()).toEqual({ n: 0 }) + h.advance(11 * MIN) + const expired = await browser(h, `https://sandbox.test/v1/github/callback?code=code_mallory&state=${encodeURIComponent(state)}`) + expect(expired.text).toContain('not opened from Orca') + }) + + it('org installations bind only for an active org admin', async () => { + let now = 0 + const org = { id: 77, account: { id: 50, login: 'acme', type: 'Organization' } } + const github = fakeGitHub({ + now: () => now, + users: { + code_member: { id: 2, login: 'member', installations: [org], memberships: { acme: { state: 'active', role: 'member' } } }, + code_admin: { id: 3, login: 'admin', installations: [org], memberships: { acme: { state: 'active', role: 'admin' } } }, + code_pending: { id: 4, login: 'pending', installations: [org], memberships: { acme: { state: 'pending', role: 'admin' } } } } }) - expect(await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test.git')).toEqual({ token: 'ghs_minted', expiresAt: now + 60 * MIN }) - expect(calls).toEqual([ - { method: 'GET', path: '/repos/nwparker/orca-sandbox-push-test/installation', body: undefined, iss: '123' }, - { + const h = await createHarness( + { GITHUB_APP_SLUG: 'orca-cloud-test', GITHUB_APP_CLIENT_ID: 'Iv1.test', GITHUB_APP_CLIENT_SECRET: 'x', GITHUB_STATE_SECRET: STATE_SECRET }, + { githubFetch: github.fetch } + ) + now = h.now() + for (const [code, expected] of [['code_member', 0], ['code_pending', 0], ['code_admin', 1]] as const) { + const { callback } = await connect(h, 'carol', code) + expect(callback.status).toBe(200) + expect(h.raw.prepare(`SELECT COUNT(*) AS n FROM github_installations WHERE account_id = 'user:carol'`).get()).toEqual({ n: expected }) + } + }) + + it('operator binding needs the operator token and is recorded as such', async () => { + const { h, minter } = await harnessWithApp() + const bind = (token: string) => + h.send( + new Request('https://sandbox.test/v1/internal/github/bindings', { + method: 'POST', + headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ installationId: 77, accountId: 'dev:push' }) + }) + ) + expect((await bind(await h.token('alice'))).status).toBe(404) + expect((await bind('o'.repeat(39) + 'x')).status).toBe(404) + const ok = await bind(OPERATOR) + expect(await ok.json()).toEqual({ bound: { installationId: 77, accountId: 'dev:push', githubAccount: 'alice-gh' } }) + expect(h.raw.prepare('SELECT method, bound_by_user_id FROM github_installations').get()).toEqual({ method: 'operator', bound_by_user_id: 'operator' }) + expect(await minter.tokenForRepo(REPO, 'dev:push')).not.toBeNull() + const byRepo = await h.send( + new Request('https://sandbox.test/v1/internal/github/bindings', { method: 'POST', - path: '/app/installations/77/access_tokens', - body: { repositories: ['orca-sandbox-push-test'], permissions: { contents: 'write', pull_requests: 'write', metadata: 'read' } }, - iss: '123' - } - ]) - now += 30 * MIN - await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test') - expect(calls).toHaveLength(2) - now += 26 * MIN - await minter.tokenForRepo('https://github.com/nwparker/orca-sandbox-push-test') - expect(calls).toHaveLength(4) - expect(await minter.tokenForRepo('https://gitlab.com/a/b')).toBeNull() + headers: { authorization: `Bearer ${OPERATOR}` }, + body: JSON.stringify({ repoUrl: REPO, accountId: 'dev:other' }) + }) + ) + expect(await byRepo.json()).toEqual({ bound: { installationId: 77, accountId: 'dev:other', githubAccount: 'alice-gh' } }) + const unbind = await h.send( + new Request('https://sandbox.test/v1/internal/github/bindings', { + method: 'DELETE', + headers: { authorization: `Bearer ${OPERATOR}` }, + body: JSON.stringify({ installationId: 77, accountId: 'dev:push' }) + }) + ) + expect(await unbind.json()).toEqual({ revoked: 1 }) + expect(await minter.tokenForRepo(REPO, 'dev:push')).toBeNull() }) it('injects the repo token for git and refreshes it before it expires', async () => { let expiresAt = 0 const h = await createHarness({}, { - github: { tokenForRepo: async () => ({ token: `ghs_${expiresAt}`, expiresAt }) } + github: { tokenForRepo: async () => ({ token: `ghs_${expiresAt}`, expiresAt }), getInstallation: async () => null, getRepoInstallation: async () => null } }) expiresAt = h.now() + 60 * MIN const { body } = await h.call('POST', '/v1/sandboxes', { diff --git a/cloud/apps/sandbox-control/test/guardrails.test.ts b/cloud/apps/sandbox-control/test/guardrails.test.ts new file mode 100644 index 00000000000..9fa0ec95268 --- /dev/null +++ b/cloud/apps/sandbox-control/test/guardrails.test.ts @@ -0,0 +1,85 @@ +import { describe, expect, it } from 'vitest' +import { createHarness } from './harness' + +const OPERATOR = 'o'.repeat(40) +const create = (workspaceId: string) => ({ body: { workspaceId, machineSize: 'standard' } }) + +async function operator(h: Awaited>, method: string, path: string, body?: unknown) { + const response = await h.send( + new Request(`https://sandbox.test${path}`, { + method, + headers: { authorization: `Bearer ${OPERATOR}`, 'content-type': 'application/json' }, + body: body === undefined ? undefined : JSON.stringify(body) + }) + ) + return { status: response.status, body: (await response.json()) as any } +} + +describe('kill switch and caps', () => { + it('kill switch refuses creates and resumes, drains running sandboxes keeping their disk, and lifts', async () => { + const h = await createHarness({ OPERATOR_TOKEN: OPERATOR }) + const a = await h.call('POST', '/v1/sandboxes', create('ws-a')) + const b = await h.call('POST', '/v1/sandboxes', { ...create('ws-b'), as: 'bob' }) + + expect((await h.call('GET', '/v1/internal/status')).status).toBe(404) + const on = await operator(h, 'POST', '/v1/internal/kill-switch', { enabled: true }) + expect(on.body).toMatchObject({ killSwitch: true, drained: { suspended: 2, failed: 0, remaining: 0 } }) + for (const box of h.vercel.boxes.values()) { + expect(box.status).toBe('stopped') + } + expect([...h.vercel.snapshots.values()].filter((s) => s.status === 'created')).toHaveLength(2) + + expect(await h.call('POST', '/v1/sandboxes', create('ws-c'))).toEqual({ status: 503, body: { error: 'maintenance' } }) + expect((await h.call('POST', `/v1/sandboxes/${a.body.sandbox.id}/resume`)).body).toEqual({ error: 'maintenance' }) + expect(h.billing.checks).toHaveLength(2) + expect((await operator(h, 'GET', '/v1/internal/status')).body).toMatchObject({ killSwitch: true, active: 0 }) + + await operator(h, 'POST', '/v1/internal/kill-switch', { enabled: false }) + expect((await h.call('POST', `/v1/sandboxes/${b.body.sandbox.id}/resume`, { as: 'bob' })).status).toBe(200) + }) + + it('the cron keeps draining while the kill switch is on; the static env switch also refuses', async () => { + const h = await createHarness({ OPERATOR_TOKEN: OPERATOR }) + await operator(h, 'POST', '/v1/internal/kill-switch', { enabled: true, drain: false }) + expect((await h.call('POST', '/v1/sandboxes', create('ws-a'))).status).toBe(503) + await operator(h, 'POST', '/v1/internal/kill-switch', { enabled: false }) + await h.call('POST', '/v1/sandboxes', create('ws-a')) + await operator(h, 'POST', '/v1/internal/kill-switch', { enabled: true, drain: false }) + expect((await h.reconcile()).drained).toBe(1) + + const fixed = await createHarness({ SANDBOX_CREATES_DISABLED: 'true' }) + expect((await fixed.call('POST', '/v1/sandboxes', create('ws-a'))).body).toEqual({ error: 'maintenance' }) + }) + + it('global running cap refuses the next start across accounts', async () => { + const h = await createHarness({ SANDBOX_MAX_RUNNING: '1' }) + expect((await h.call('POST', '/v1/sandboxes', create('ws-a'))).status).toBe(200) + const res = await h.call('POST', '/v1/sandboxes', { ...create('ws-b'), as: 'bob' }) + expect(res).toEqual({ status: 503, body: { error: 'capacity' } }) + expect(h.vercel.boxes.size).toBe(1) + }) + + it('per-account backstop is 3 per seat even when billing would allow more', async () => { + const h = await createHarness() + for (const ws of ['ws-1', 'ws-2', 'ws-3']) { + expect((await h.call('POST', '/v1/sandboxes', create(ws))).status).toBe(200) + } + expect(await h.call('POST', '/v1/sandboxes', create('ws-4'))).toEqual({ + status: 403, + body: { error: 'entitlement_denied', reason: 'concurrency_limit' } + }) + h.billing.decision = { allow: true, reason: null, suspendRunning: false, seats: 2 } + expect((await h.call('POST', '/v1/sandboxes', create('ws-4'))).status).toBe(200) + expect(h.vercel.boxes.size).toBe(4) + }) + + it('rate limits creates per account per minute', async () => { + const h = await createHarness({ CREATE_RATE_PER_MINUTE: '2' }) + expect((await h.call('POST', '/v1/sandboxes', create('ws-1'))).status).toBe(200) + expect((await h.call('POST', '/v1/sandboxes', create('ws-1'))).status).toBe(200) + expect(await h.call('POST', '/v1/sandboxes', create('ws-2'))).toEqual({ status: 429, body: { error: 'rate_limited' } }) + expect((await h.call('POST', '/v1/sandboxes', { ...create('ws-2'), as: 'bob' })).status).toBe(200) + h.advance(60_000) + expect((await h.call('POST', '/v1/sandboxes', create('ws-2'))).status).toBe(200) + }) +}) diff --git a/cloud/apps/sandbox-control/test/retention.test.ts b/cloud/apps/sandbox-control/test/retention.test.ts new file mode 100644 index 00000000000..1120711eafe --- /dev/null +++ b/cloud/apps/sandbox-control/test/retention.test.ts @@ -0,0 +1,180 @@ +import { describe, expect, it } from 'vitest' +import { DEFAULT_RETENTION, housekeeping, type RetentionWarning } from '../src/retention' +import type { LifecycleDeps } from '../src/lifecycle' +import { GOLDEN, createHarness } from './harness' + +const HOUR = 60 * 60_000 +const DAY = 24 * HOUR +const create = (workspaceId: string) => ({ body: { workspaceId, machineSize: 'standard' } }) + +async function setup() { + const h = await createHarness() + const warnings: RetentionWarning[] = [] + const deps: LifecycleDeps = { + db: h.env.SANDBOX_DB, + provider: h.vercel, + entitlements: { + checkStart: async (request) => { + h.billing.checks.push({ accountId: request.accountId, runningSandboxes: request.runningSandboxes }) + return h.billing.decision + }, + reportUsage: async () => h.billing.usageAnswer, + listSuspensions: async () => h.billing.suspensions + }, + now: h.now, + retention: DEFAULT_RETENTION, + config: { environment: 'test', goldenSnapshotId: GOLDEN, defaultRegion: 'iad1' } + } + const sweep = () => housekeeping(deps, { force: true, notifier: { warn: async (w) => void warnings.push(w) } }) + return { h, deps, warnings, sweep } +} + +describe('orphan sweep', () => { + it('suspends a provider sandbox with no live D1 row, deletes it after the grace period, leaves tracked ones alone', async () => { + const { h, sweep } = await setup() + const tracked = await h.call('POST', '/v1/sandboxes', create('ws-1')) + await h.vercel.createSandbox({ + name: 'orca-test-sbx_ghost', + snapshotId: GOLDEN, + vcpus: 4, + region: 'iad1', + timeoutMs: 30 * 60_000, + ports: [], + networkPolicy: { allow: {} }, + tags: { orcaSandbox: 'sbx_ghost', orcaEnv: 'test' }, + snapshotExpirationMs: 0 + }) + // Another environment's sandbox is never judged by this one. + await h.vercel.createSandbox({ + name: 'orca-prod-sbx_other', + snapshotId: GOLDEN, + vcpus: 4, + region: 'iad1', + timeoutMs: 30 * 60_000, + ports: [], + networkPolicy: { allow: {} }, + tags: { orcaSandbox: 'sbx_other', orcaEnv: 'prod' }, + snapshotExpirationMs: 0 + }) + const first = await sweep() + expect(first).toMatchObject({ orphanSandboxesSuspended: 1, orphanSandboxesDeleted: 0 }) + expect(h.vercel.boxes.get('orca-test-sbx_ghost')!.status).toBe('stopped') + expect(h.vercel.boxes.get('orca-prod-sbx_other')!.status).toBe('running') + expect(h.vercel.boxes.get(`orca-test-${tracked.body.sandbox.id}`)!.status).toBe('running') + + h.advance(23 * HOUR) + expect((await sweep()).orphanSandboxesDeleted).toBe(0) + h.advance(2 * HOUR) + expect((await sweep()).orphanSandboxesDeleted).toBe(1) + expect(h.vercel.boxes.has('orca-test-sbx_ghost')).toBe(false) + const ghostSnaps = [...h.vercel.snapshots.values()].filter((s) => s.sandboxName === 'orca-test-sbx_ghost') + expect(ghostSnaps.every((s) => s.status === 'deleted')).toBe(true) + expect(h.vercel.boxes.has('orca-prod-sbx_other')).toBe(true) + expect(h.vercel.boxes.has(`orca-test-${tracked.body.sandbox.id}`)).toBe(true) + }) + + it('deletes automatic snapshots whose sandbox is gone after the grace period, never manual or owned ones', async () => { + const { h, sweep } = await setup() + const tracked = await h.call('POST', '/v1/sandboxes', create('ws-1')) + await h.call('POST', `/v1/sandboxes/${tracked.body.sandbox.id}/suspend`) + const add = (id: string, creationMethod: string, sandboxName: string) => + h.vercel.snapshots.set(id, { id, status: 'created', sourceSessionId: 's', sandboxName, creationMethod, createdAt: h.now() }) + add('snap_leftover', 'automatic', 'orca-test-sbx_deleted_long_ago') + add('snap_old_golden', 'manual', 'golden-build') + const owned = [...h.vercel.snapshots.values()].find((s) => s.sandboxName === `orca-test-${tracked.body.sandbox.id}`)! + + expect((await sweep()).orphanSnapshotsSeen).toBe(1) + h.advance(25 * HOUR) + expect((await sweep()).orphanSnapshotsDeleted).toBe(1) + expect(h.vercel.snapshots.get('snap_leftover')!.status).toBe('deleted') + expect(h.vercel.snapshots.get('snap_old_golden')!.status).toBe('created') + expect(h.vercel.snapshots.get(owned.id)!.status).toBe('created') + expect((await h.call('POST', `/v1/sandboxes/${tracked.body.sandbox.id}/resume`)).status).toBe(200) + }) + + it('a partial provider listing deletes nothing', async () => { + const { h, sweep } = await setup() + h.vercel.snapshots.set('snap_leftover', { id: 'snap_leftover', status: 'created', sourceSessionId: 's', sandboxName: 'gone', creationMethod: 'automatic', createdAt: 0 }) + const list = h.vercel.listSandboxes + h.vercel.listSandboxes = async () => ({ ...(await list()), complete: false }) + await sweep() + h.advance(48 * HOUR) + expect((await sweep()).orphanSnapshotsDeleted).toBe(0) + expect(h.vercel.snapshots.get('snap_leftover')!.status).toBe('created') + }) +}) + +describe('data retention', () => { + it('destroys a canceled account’s sandboxes 30 days after cancel, warning 7 days ahead', async () => { + const { h, sweep, warnings } = await setup() + const box = await h.call('POST', '/v1/sandboxes', create('ws-1')) + h.billing.suspensions = ['user:alice'] + h.billing.decision = { allow: false, reason: 'payment_grace_expired', suspendRunning: true } + await h.reconcile() + expect(h.raw.prepare('SELECT reason FROM account_holds WHERE account_id = ?').get('user:alice')).toEqual({ reason: 'payment_grace_expired' }) + expect((await h.call('GET', `/v1/sandboxes/${box.body.sandbox.id}`)).body.sandbox.status).toBe('suspended') + + h.advance(22 * DAY) + expect((await sweep()).retentionWarnings).toBe(0) + h.advance(1 * DAY) + expect((await sweep()).retentionWarnings).toBe(1) + expect(warnings[0]).toMatchObject({ kind: 'data_deletion', accountId: 'user:alice', sandboxIds: [box.body.sandbox.id] }) + h.advance(6 * DAY) + expect((await sweep()).retentionDestroyed).toBe(0) + h.advance(1 * DAY + HOUR) + expect((await sweep()).retentionDestroyed).toBe(1) + expect(h.vercel.boxes.size).toBe(0) + expect([...h.vercel.snapshots.values()].every((s) => s.status === 'deleted')).toBe(true) + expect(h.raw.prepare('SELECT repo_url, status FROM sandboxes').get()).toEqual({ repo_url: null, status: 'destroyed' }) + }) + + it('a canceled subscription found by the standing probe starts the clock; resubscribing clears it', async () => { + const { h, sweep } = await setup() + await h.call('POST', '/v1/sandboxes', create('ws-1')) + h.billing.decision = { allow: false, reason: 'subscription_inactive', suspendRunning: false } + expect((await sweep()).holdsOpened).toBe(1) + h.advance(2 * DAY) + h.billing.decision = { allow: true, reason: null, suspendRunning: false } + expect((await sweep()).holdsCleared).toBe(1) + h.advance(40 * DAY) + expect((await sweep()).retentionDestroyed).toBe(0) + expect(h.vercel.boxes.size).toBe(1) + }) + + it('an exhausted-hours denial or a billing outage never starts a deletion clock', async () => { + const { h, sweep, deps } = await setup() + await h.call('POST', '/v1/sandboxes', create('ws-1')) + h.billing.decision = { allow: false, reason: 'included_hours_exhausted', suspendRunning: false } + expect((await sweep()).holdsOpened).toBe(0) + deps.entitlements.checkStart = async () => { + throw new Error('billing down') + } + h.advance(2 * DAY) + expect((await sweep()).holdsOpened).toBe(0) + expect(h.raw.prepare(`SELECT COUNT(*) AS n FROM account_holds WHERE reason <> 'active'`).get()).toEqual({ n: 0 }) + }) + + it('idle retention: a suspended sandbox untouched 90 days is warned at 83 and destroyed at 90; a resume resets it', async () => { + const { h, sweep, warnings } = await setup() + const idle = await h.call('POST', '/v1/sandboxes', create('ws-idle')) + const used = await h.call('POST', '/v1/sandboxes', create('ws-used')) + await h.call('POST', `/v1/sandboxes/${idle.body.sandbox.id}/suspend`) + await h.call('POST', `/v1/sandboxes/${used.body.sandbox.id}/suspend`) + h.advance(84 * DAY) + expect((await sweep()).idleWarnings).toBe(2) + expect(warnings.map((w) => w.kind)).toEqual(['idle_deletion', 'idle_deletion']) + await h.call('POST', `/v1/sandboxes/${used.body.sandbox.id}/resume`) + await h.call('POST', `/v1/sandboxes/${used.body.sandbox.id}/suspend`) + h.advance(7 * DAY) + expect((await sweep()).idleDestroyed).toBe(1) + expect((await h.call('GET', `/v1/sandboxes/${idle.body.sandbox.id}`)).status).toBe(200) + expect((await h.call('GET', `/v1/sandboxes/${idle.body.sandbox.id}`)).body.sandbox.status).toBe('destroyed') + expect((await h.call('GET', `/v1/sandboxes/${used.body.sandbox.id}`)).body.sandbox.status).toBe('suspended') + }) + + it('new sandboxes ask the provider for a snapshot expiry beyond idle retention', async () => { + const { h } = await setup() + await h.call('POST', '/v1/sandboxes', create('ws-1')) + expect([...h.vercel.boxes.values()][0]!.params.snapshotExpirationMs).toBe(104 * DAY) + }) +}) diff --git a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts index d6d7a094b53..b960ad57b8b 100644 --- a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts +++ b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts @@ -41,7 +41,8 @@ describe('Vercel Sandbox REST client', () => { timeoutMs: 1_800_000, ports: [6768], networkPolicy: { allow: { 'registry.npmjs.org': [] } }, - tags: { orcaSandbox: 'sbx_1' } + tags: { orcaSandbox: 'sbx_1' }, + snapshotExpirationMs: 104 * 86_400_000 }) expect(seen[0]!.method).toBe('POST') expect(`${seen[0]!.url.origin}${seen[0]!.url.pathname}${seen[0]!.url.search}`).toBe( @@ -53,8 +54,8 @@ describe('Vercel Sandbox REST client', () => { source: { type: 'snapshot', snapshotId: 'snap_golden' }, resources: { vcpus: 4 }, persistent: true, - keepLastSnapshots: { count: 2 }, - snapshotExpiration: 0 + keepLastSnapshots: { count: 2, expiration: 104 * 86_400_000 }, + snapshotExpiration: 104 * 86_400_000 }) expect(state.routes).toEqual([{ port: 6768, url: 'https://sb-1.vercel.run' }]) expect(state.session.timeoutMs).toBe(1_800_000) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index 0a05668e44d..f246c2d13ee 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -4,7 +4,9 @@ // wrangler deploy -c wrangler.dev.jsonc // Secrets (wrangler secret put -c wrangler.dev.jsonc, never here): VERCEL_TOKEN, SANDBOX_DEV_JWKS, // EGRESS_DEV_STATIC_AUTHORIZATION, CONTROL_PLANE_TOKEN (same value as on - // orca-billing-dev), GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY (PKCS#8), SECRETS_KEK (base64 32 bytes). + // orca-billing-dev), GITHUB_APP_ID, GITHUB_APP_PRIVATE_KEY (PKCS#8), SECRETS_KEK (base64 32 bytes), + // OPERATOR_TOKEN (/v1/internal/*), GITHUB_STATE_SECRET; for the GitHub connect flow also + // GITHUB_APP_CLIENT_ID + GITHUB_APP_CLIENT_SECRET (app settings → generate a client secret). "name": "orca-sandbox-control-dev", "main": "src/index.ts", "compatibility_date": "2026-11-01", @@ -29,7 +31,9 @@ "VERCEL_PROJECT_ID": "prj_SkVtv6zJurF9dUHpfTRy4v9avwPq", "VERCEL_REGION": "iad1", "BILLING_URL": "https://orca-billing-dev.lovecast.workers.dev", - // GOLDEN_SNAPSHOT_ID: unset until an image is built (image/build-golden-image.ts); lifecycle routes answer 503 meanwhile. + // Golden image from image/build-golden-image.ts; lifecycle routes answer 503 without one. + "GOLDEN_SNAPSHOT_ID": "snap_0fYmcpIUlc4OUC9viFN0Y4dVCDuD", + "GITHUB_APP_SLUG": "orca-cloud-dev", "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" } } diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index f2434cddcdd..6c237f40a87 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -230,6 +230,29 @@ Product decision: "API-key injection" in the Conductor parity matrix (form 2 = t 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. +## GitHub installation binding, guardrails, retention (2026-11-09) + +- Repo tokens need a binding: `github_installations(installation_id, account_id)`. `tokenForRepo(url, accountId)` + looks up the repo's installation, refuses unless the caller's account holds a live binding (re-checked on cached + tokens too), mints for that one repo (contents/pull_requests write, metadata read) and rejects a wider response. + Binding: desktop `POST /v1/github/connect` → signed (HMAC), single-use, 10-min state carrying the Orca account → + GitHub install / OAuth → `/v1/github/setup` + `/v1/github/callback` exchange the code for a user-to-server token and + bind only installations on the GitHub user's own account or on an org where they are an active admin + (`/user/memberships/orgs`, needs the app's Members:read). Installation ids in URLs are never trusted. + Operator binding for support/dev: `POST /v1/internal/github/bindings {installationId|repoUrl, accountId}`. +- Guardrails before billing: kill switch (D1 flag via `POST /v1/internal/kill-switch`, or `SANDBOX_CREATES_DISABLED`) + refuses create/resume/rollover with 503 `maintenance` and drains running sandboxes (suspend, snapshot kept; cron + keeps draining while on); global `SANDBOX_MAX_RUNNING` (50, 503 `capacity`); per-account 3 × seats backstop + (403 `concurrency_limit`); `CREATE_RATE_PER_MINUTE` (10, 429). +- Hourly housekeeping: provider sandboxes tagged with this env and no live row are stopped on first sight and + deleted after `ORPHAN_GRACE_HOURS` (24); automatic snapshots whose sandbox no longer exists anywhere in the project + are deleted after the same grace (manual/golden never; partial listings delete nothing). Canceled/unpaid accounts + (suspensions list + daily standing probe; outages never start a clock) get a warning hook at T−7 days and are + destroyed `DATA_RETENTION_DAYS` (30) after; suspended sandboxes idle `IDLE_RETENTION_DAYS` (90) likewise. New + sandboxes set `snapshotExpiration` to 104 days (backstop). Destroyed rows drop `repo_url`. +- Live on dev: unbound account → private clone refused (no token minted); after operator binding → clone + push + + gh/Node via proxy OK; kill switch drill drained 1, refused create/resume, resume OK after lifting. + ## What needs live access (not done) Vercel Pro team id + project id + a Sandbox-scoped token (as Worker secrets), wrangler auth for a dev D1, a From 1da14d87a51ad840a0b2f9853ad1495462bae1de Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 02:59:02 -0800 Subject: [PATCH 12/18] feat(cloud): Orca Cloud in the desktop end to end (provider, lease scheduler, upsell, dogfood) (#26) * feat(sidebar): replace footer Settings and Help icons with an account menu * fix: keep account menu header on primitive spacing; move focus-read allowance * fix(sidebar): address account menu review findings * feat(billing): Cloudflare Worker for Stripe checkout, portal, webhooks, entitlements and usage (#7) Test-mode-only foundation for paid Orca plans: hosted Checkout/Portal, signed + idempotent Stripe webhooks that re-read subscriptions, a D1 ledger for sandbox usage with overage-only Billing meter events, and entitlement checks for the desktop and the sandbox control plane. * feat(billing): Plan & billing in the Orca account settings and account menu (#9) Shows the signed-in account's plan, sandbox hours used and left, and opens hosted Stripe Checkout (Upgrade) or the Customer Portal (Manage billing) in the browser. Main mints a short-lived billing token from Orca Cloud auth and only opens checkout.stripe.com / billing.stripe.com URLs. Hidden until ORCA_BILLING_API_URL is set; the account menu item is gated on the 'billing' cloud capability and registers through a local adapter that mirrors the upcoming account menu API. * feat(billing): plans and prices from config, opt-in Stripe Tax, real account menu - Worker: BILLING_PLANS JSON overrides the plan catalog (labels, display price, hours, limits); entitlements return the plan list so the desktop shows no hardcoded prices. STRIPE_AUTOMATIC_TAX=true adds automatic_tax and tax id collection to Checkout (default off). Setup script takes prices from env. - Desktop: register Plan & billing through the sidebar account menu (merged nwparker/sidebar-account-menu) and drop the local adapter. - Register the billing Worker's tests with the test-runner check. * feat(billing): Team pricing, opt-in overage, Stripe test-mode e2e, dev deploy (#18) * feat(billing): Team-only pricing, opt-in overage with spend cap, live Stripe test-mode e2e - Catalog: Team $50/seat/mo or $480/seat/yr, 60 Standard h/seat/month pooled (monthly windows on annual plans), Large 2x, 3 running/seat, 15-min idle sleep; Enterprise is contact-only. All numbers/labels in BILLING_PLANS defaults. - Overage is opt-in (adds the metered item), $0.75/h, monthly spend cap (default $25, admin-raisable) enforced in the meter outbox; 80%/100% usage warnings. - Checkout: plan/interval/seats with adjustable quantity; portal configuration var. - Workers reject fetch redirect 'error': use 'manual'. Log route errors. - scripts/stripe-test-setup.ts creates the test catalog + portal configuration; scripts/e2e-test-mode.ts drives Checkout (headless, test card), overage, meter, cap, annual, test-clock renewal, payment failure and cancel. - Desktop: Team/Enterprise cards with interval + seats, usage warnings, overage toggle + cap; split into copy/offers/usage modules. * feat(billing): dev deployment config, dev operator keys, return page - wrangler.dev.jsonc for orca-billing-dev (workers.dev, dev D1, test-mode prices). - BILLING_DEV_JWKS (issuer orca-billing-dev-test) accepted only when BILLING_ENV is not prod, for e2e runs until auth mints billing tokens. - GET /return: static 'return to Orca' page for Checkout/Portal redirects. - Design doc: final pricing, dev URL and resources for the control plane. * feat(billing): team seats follow membership, payment grace + suspend signal, Team UI Worker - Seats = org members from Orca Cloud auth (single source of truth; pending invites never count; min 1): auth pushes POST /v1/internal/seats, billing pulls /v1/internal/billing/org-members at Checkout and in a reconcile cron, fails closed (503) if auth is configured but unreachable. No token claim carries a count. - Proration: adds always_invoice, removals create_prorations (Stripe default). - Billing admin actions require a token minted in the last 6 minutes. - Payment failure: past_due_since (migration 0003); new starts stop at once; after 96 h (paymentGraceHours) reason payment_grace_expired + suspendRunning on check/usage, and GET /v1/internal/suspensions for the control plane. - Portal config: monthly<->annual + cancel only (seats follow membership). - e2e: seat add/remove proration, grace expiry -> suspend -> pay -> resume. Desktop - Team section + account menu item: members/roles, invite, change role, remove with confirm, revoke invites (owners/admins, auth role rules mirrored). - Payment issue banner with suspension date and Update payment method. * feat(billing): in-app plans page (#22) A pricing/plans page reachable from the account menu ("Plan & billing"), the Settings plan section ("Choose a plan" / "Compare plans"), and any upsell via openPlansPage(). Everything comes from the billing service's offers: Team monthly/annual (with the computed annual saving), pooled hours, Large weight, concurrency, idle sleep, opt-in overage with spend cap, and Enterprise contact. - States: choose (admin), locked (member), current plan with usage, payment failure (status-first), checkout in progress, loading and failure with retry. - Seats are read-only: one per team member (billing re-reads the count). - Shared card shell keeps both cards' footers level; sliding interval toggle and price fade respect reduced motion; dialog opens without a pre-focused button. - Worker offers gain per-seat cents and the Large weight for totals. - Copy matches the marketing /cloud promises: agents keep running, wake in seconds, secrets never stored in the VM. * wip(cloud): Orca Cloud built-in provider wiring * feat(cloud): no-plan upsell opens the plans page * feat(cloud): Orca Cloud dogfood fixes (provider persistence, picker label, any-input activity, idle override) * fix(cloud): live dogfood fixes (snapshot already-deleted, agent egress wildcards, golden image id) + results * fix(cloud): i18n keys, typed dev JWK, verdict typing * feat(cloud): billing caps from billing (maxConcurrentSandboxes, per-member), userId on usage; concurrency copy * feat(cloud): golden image can pin a released orca-server tarball (--release), source build stays the fallback * fix(cloud): async dev key read, runtime localization catalog --- cloud/apps/billing/.gitignore | 3 + cloud/apps/billing/migrations/0001_init.sql | 69 ++++ .../billing/migrations/0002_team_overage.sql | 10 + .../billing/migrations/0003_payment_grace.sql | 4 + cloud/apps/billing/package.json | 23 ++ cloud/apps/billing/scripts/e2e-test-mode.ts | 334 ++++++++++++++++ .../apps/billing/scripts/stripe-test-setup.ts | 136 +++++++ cloud/apps/billing/src/auth.ts | 146 +++++++ cloud/apps/billing/src/checkout.ts | 106 ++++++ cloud/apps/billing/src/entitlements.ts | 179 +++++++++ cloud/apps/billing/src/env.ts | 60 +++ cloud/apps/billing/src/index.ts | 227 +++++++++++ cloud/apps/billing/src/membership.ts | 52 +++ cloud/apps/billing/src/overage.ts | 52 +++ cloud/apps/billing/src/plans.ts | 184 +++++++++ cloud/apps/billing/src/seats.ts | 101 +++++ cloud/apps/billing/src/store.ts | 329 ++++++++++++++++ cloud/apps/billing/src/stripe.ts | 193 ++++++++++ cloud/apps/billing/src/usage.ts | 124 ++++++ cloud/apps/billing/src/webhook-signature.ts | 84 +++++ cloud/apps/billing/src/webhook.ts | 167 ++++++++ cloud/apps/billing/test/auth-dev-keys.test.ts | 46 +++ .../apps/billing/test/billing-config.test.ts | 86 +++++ cloud/apps/billing/test/billing-flow.test.ts | 356 ++++++++++++++++++ cloud/apps/billing/test/d1-fake.ts | 64 ++++ cloud/apps/billing/test/harness.ts | 85 +++++ cloud/apps/billing/test/seats-grace.test.ts | 183 +++++++++ cloud/apps/billing/test/stripe-client.test.ts | 47 +++ cloud/apps/billing/test/stripe-fake.ts | 192 ++++++++++ .../billing/test/webhook-signature.test.ts | 43 +++ cloud/apps/billing/tsconfig.json | 14 + cloud/apps/billing/vitest.config.ts | 7 + cloud/apps/billing/wrangler.dev.jsonc | 37 ++ cloud/apps/billing/wrangler.jsonc | 30 ++ .../image/build-golden-image.ts | 18 +- .../image/golden-image.test.ts | 12 +- .../sandbox-control/image/golden-image.ts | 26 ++ .../sandbox-control/image/provision-golden.sh | 8 + .../apps/sandbox-control/src/entitlements.ts | 13 +- cloud/apps/sandbox-control/src/env.ts | 2 + cloud/apps/sandbox-control/src/guardrails.ts | 7 +- cloud/apps/sandbox-control/src/lifecycle.ts | 21 +- .../sandbox-control/src/network-policy.ts | 11 +- cloud/apps/sandbox-control/src/store.ts | 9 + cloud/apps/sandbox-control/src/usage.ts | 9 +- .../sandbox-control/src/vercel-sandbox-api.ts | 17 +- .../test/entitlements-client.test.ts | 5 +- cloud/apps/sandbox-control/test/harness.ts | 2 + .../test/lifecycle-flow.test.ts | 13 + .../test/vercel-sandbox-api.test.ts | 10 + cloud/docs/accounts-billing-design.md | 152 ++++++++ cloud/docs/vercel-driver-design.md | 9 + cloud/pnpm-lock.yaml | 25 ++ config/focus-setting-read-baseline.txt | 2 +- .../check-test-file-runners.mjs | 6 + src/main/billing/billing-client.test.ts | 206 ++++++++++ src/main/billing/billing-client.ts | 251 ++++++++++++ src/main/billing/billing-config.test.ts | 67 ++++ src/main/billing/billing-config.ts | 23 ++ src/main/billing/billing-service.ts | 149 ++++++++ src/main/ephemeral-vm/workspace-resume.ts | 2 + .../hosted-sandbox/hosted-sandbox-config.ts | 147 ++++++++ .../hosted-sandbox/hosted-sandbox-install.ts | 68 ++++ .../hosted-sandbox-recipe.test.ts | 73 ++++ .../hosted-sandbox/hosted-sandbox-recipe.ts | 159 ++++++++ .../hosted-session-lease-scheduler.ts | 6 +- .../hosted-sandbox/hosted-session-registry.ts | 171 +++++++++ .../hosted-sandbox/hosted-session-sync.ts | 11 + src/main/ipc/ephemeral-vm-recipe-context.ts | 14 +- src/main/ipc/ephemeral-vm.ts | 11 +- .../profiles/orca-profile-billing-handlers.ts | 72 ++++ src/main/ipc/profiles/orca-profiles.ts | 2 + src/main/orcad/orcad-entry.ts | 7 + src/main/orcad/orcad-work-verdict.ts | 42 +++ .../orcad/orcad-work-verdict-method.ts | 16 + .../rpc/methods/rpc-method-registry.ts | 2 + .../terminal/lifecycle/terminal-run-facts.ts | 10 + src/preload/api/ephemeral-vm-api.ts | 8 + src/preload/api/ephemeral-vm-bridge.ts | 18 + src/preload/api/orca-profile-api.ts | 15 + src/preload/api/orca-profiles-bridge.ts | 6 +- src/renderer/src/App.tsx | 2 + .../components/billing/PaymentIssueNotice.tsx | 73 ++++ .../billing/PlanBillingSection.fixture.tsx | 128 +++++++ .../components/billing/PlanBillingSection.tsx | 228 +++++++++++ .../components/billing/PlanBillingUsage.tsx | 159 ++++++++ .../components/billing/plan-billing-copy.ts | 104 +++++ .../billing/plan-billing-fixtures.ts | 47 +++ .../billing/plan-billing-format.test.ts | 39 ++ .../components/billing/plan-billing-format.ts | 33 ++ .../billing/plans/EnterprisePlanCard.tsx | 49 +++ .../billing/plans/PlanCardShell.tsx | 64 ++++ .../billing/plans/PlansDialogContent.tsx | 45 +++ .../billing/plans/PlansDialogHost.tsx | 73 ++++ .../billing/plans/PlansIntervalToggle.tsx | 70 ++++ .../billing/plans/PlansView.fixture.tsx | 118 ++++++ .../components/billing/plans/PlansView.tsx | 273 ++++++++++++++ .../components/billing/plans/TeamPlanCard.tsx | 219 +++++++++++ .../billing/plans/plans-page-store.ts | 38 ++ .../billing/plans/plans-pricing.test.ts | 56 +++ .../components/billing/plans/plans-pricing.ts | 56 +++ .../billing/plans/use-org-seat-count.ts | 32 ++ .../register-plan-billing-menu-item.test.ts | 44 +++ .../register-plan-billing-menu-item.ts | 38 ++ .../billing/team/TeamMembersSection.tsx | 113 ++++++ .../billing/team/TeamMembersView.fixture.tsx | 56 +++ .../billing/team/TeamMembersView.tsx | 265 +++++++++++++ .../billing/team/team-roles.test.ts | 19 + .../src/components/billing/team/team-roles.ts | 67 ++++ .../components/billing/team/use-org-roster.ts | 35 ++ .../billing/use-orca-billing-status.ts | 54 +++ .../new-workspace/RunTargetCombobox.tsx | 4 +- .../new-workspace/run-target-options.ts | 4 + .../accounts/orca/OrcaAccountSettingsPane.tsx | 10 + .../sidebar/SidebarSettingsHelpMenu.tsx | 330 ---------------- .../sidebar/SidebarToolbar.fixture.tsx | 80 ++++ .../src/components/sidebar/SidebarToolbar.tsx | 4 +- .../account-menu/SidebarAccountMenu.test.tsx | 138 +++++++ .../account-menu/SidebarAccountMenu.tsx | 221 +++++++++++ .../account-menu/SidebarHelpSubmenu.tsx | 232 ++++++++++++ .../account-menu/account-menu-identity.ts | 18 + .../account-menu/account-menu-items.test.ts | 43 +++ .../account-menu/account-menu-items.ts | 63 ++++ .../src/i18n/en-runtime-required.json | 4 + src/renderer/src/i18n/locales/de.json | 4 + src/renderer/src/i18n/locales/en.json | 156 +++++++- src/renderer/src/i18n/locales/es.json | 4 + src/renderer/src/i18n/locales/fr.json | 4 + src/renderer/src/i18n/locales/ja.json | 4 + src/renderer/src/i18n/locales/ko.json | 4 + src/renderer/src/i18n/locales/pt.json | 4 + src/renderer/src/i18n/locales/ru.json | 4 + src/renderer/src/i18n/locales/tr.json | 4 + src/renderer/src/i18n/locales/zh-Hant.json | 4 + src/renderer/src/i18n/locales/zh.json | 4 + .../src/lib/ephemeral-vm-error-copy.ts | 8 +- .../src/lib/ephemeral-vm-workspace-target.ts | 9 +- .../src/lib/ephemeral-vm-worktree-creation.ts | 5 +- .../src/lib/orca-cloud-session-events.ts | 56 +++ src/renderer/src/main.tsx | 6 + .../web/preload-api/web-orca-profiles-api.ts | 6 +- src/shared/ephemeral-vm-failure-copy.ts | 8 +- .../ephemeral-vm-recipe-step-providers.ts | 42 +++ src/shared/ephemeral-vm-recipe-step.ts | 58 ++- .../ephemeral-vm-runtime-feature-store.ts | 20 +- ...phemeral-vm-runtime-rollback-projection.ts | 4 +- src/shared/ephemeral-vm-runtime-store.test.ts | 19 +- src/shared/ephemeral-vm-runtimes.ts | 3 +- .../hosted-sandbox/hosted-session-events.ts | 22 ++ src/shared/orca-billing.ts | 114 ++++++ src/shared/orca-yaml-hook-types.ts | 4 + .../rpc-params-catalog.generated.ts | 2 + tests/e2e/agent-auto-permissions.spec.ts | 3 +- tests/e2e/onboarding.spec.ts | 3 +- 154 files changed, 9660 insertions(+), 382 deletions(-) create mode 100644 cloud/apps/billing/.gitignore create mode 100644 cloud/apps/billing/migrations/0001_init.sql create mode 100644 cloud/apps/billing/migrations/0002_team_overage.sql create mode 100644 cloud/apps/billing/migrations/0003_payment_grace.sql create mode 100644 cloud/apps/billing/package.json create mode 100644 cloud/apps/billing/scripts/e2e-test-mode.ts create mode 100644 cloud/apps/billing/scripts/stripe-test-setup.ts create mode 100644 cloud/apps/billing/src/auth.ts create mode 100644 cloud/apps/billing/src/checkout.ts create mode 100644 cloud/apps/billing/src/entitlements.ts create mode 100644 cloud/apps/billing/src/env.ts create mode 100644 cloud/apps/billing/src/index.ts create mode 100644 cloud/apps/billing/src/membership.ts create mode 100644 cloud/apps/billing/src/overage.ts create mode 100644 cloud/apps/billing/src/plans.ts create mode 100644 cloud/apps/billing/src/seats.ts create mode 100644 cloud/apps/billing/src/store.ts create mode 100644 cloud/apps/billing/src/stripe.ts create mode 100644 cloud/apps/billing/src/usage.ts create mode 100644 cloud/apps/billing/src/webhook-signature.ts create mode 100644 cloud/apps/billing/src/webhook.ts create mode 100644 cloud/apps/billing/test/auth-dev-keys.test.ts create mode 100644 cloud/apps/billing/test/billing-config.test.ts create mode 100644 cloud/apps/billing/test/billing-flow.test.ts create mode 100644 cloud/apps/billing/test/d1-fake.ts create mode 100644 cloud/apps/billing/test/harness.ts create mode 100644 cloud/apps/billing/test/seats-grace.test.ts create mode 100644 cloud/apps/billing/test/stripe-client.test.ts create mode 100644 cloud/apps/billing/test/stripe-fake.ts create mode 100644 cloud/apps/billing/test/webhook-signature.test.ts create mode 100644 cloud/apps/billing/tsconfig.json create mode 100644 cloud/apps/billing/vitest.config.ts create mode 100644 cloud/apps/billing/wrangler.dev.jsonc create mode 100644 cloud/apps/billing/wrangler.jsonc create mode 100644 cloud/docs/accounts-billing-design.md create mode 100644 src/main/billing/billing-client.test.ts create mode 100644 src/main/billing/billing-client.ts create mode 100644 src/main/billing/billing-config.test.ts create mode 100644 src/main/billing/billing-config.ts create mode 100644 src/main/billing/billing-service.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-config.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-install.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-recipe.test.ts create mode 100644 src/main/hosted-sandbox/hosted-sandbox-recipe.ts create mode 100644 src/main/hosted-sandbox/hosted-session-registry.ts create mode 100644 src/main/hosted-sandbox/hosted-session-sync.ts create mode 100644 src/main/ipc/profiles/orca-profile-billing-handlers.ts create mode 100644 src/main/orcad/orcad-work-verdict.ts create mode 100644 src/main/runtime/rpc/methods/orcad/orcad-work-verdict-method.ts create mode 100644 src/renderer/src/components/billing/PaymentIssueNotice.tsx create mode 100644 src/renderer/src/components/billing/PlanBillingSection.fixture.tsx create mode 100644 src/renderer/src/components/billing/PlanBillingSection.tsx create mode 100644 src/renderer/src/components/billing/PlanBillingUsage.tsx create mode 100644 src/renderer/src/components/billing/plan-billing-copy.ts create mode 100644 src/renderer/src/components/billing/plan-billing-fixtures.ts create mode 100644 src/renderer/src/components/billing/plan-billing-format.test.ts create mode 100644 src/renderer/src/components/billing/plan-billing-format.ts create mode 100644 src/renderer/src/components/billing/plans/EnterprisePlanCard.tsx create mode 100644 src/renderer/src/components/billing/plans/PlanCardShell.tsx create mode 100644 src/renderer/src/components/billing/plans/PlansDialogContent.tsx create mode 100644 src/renderer/src/components/billing/plans/PlansDialogHost.tsx create mode 100644 src/renderer/src/components/billing/plans/PlansIntervalToggle.tsx create mode 100644 src/renderer/src/components/billing/plans/PlansView.fixture.tsx create mode 100644 src/renderer/src/components/billing/plans/PlansView.tsx create mode 100644 src/renderer/src/components/billing/plans/TeamPlanCard.tsx create mode 100644 src/renderer/src/components/billing/plans/plans-page-store.ts create mode 100644 src/renderer/src/components/billing/plans/plans-pricing.test.ts create mode 100644 src/renderer/src/components/billing/plans/plans-pricing.ts create mode 100644 src/renderer/src/components/billing/plans/use-org-seat-count.ts create mode 100644 src/renderer/src/components/billing/register-plan-billing-menu-item.test.ts create mode 100644 src/renderer/src/components/billing/register-plan-billing-menu-item.ts create mode 100644 src/renderer/src/components/billing/team/TeamMembersSection.tsx create mode 100644 src/renderer/src/components/billing/team/TeamMembersView.fixture.tsx create mode 100644 src/renderer/src/components/billing/team/TeamMembersView.tsx create mode 100644 src/renderer/src/components/billing/team/team-roles.test.ts create mode 100644 src/renderer/src/components/billing/team/team-roles.ts create mode 100644 src/renderer/src/components/billing/team/use-org-roster.ts create mode 100644 src/renderer/src/components/billing/use-orca-billing-status.ts delete mode 100644 src/renderer/src/components/sidebar/SidebarSettingsHelpMenu.tsx create mode 100644 src/renderer/src/components/sidebar/SidebarToolbar.fixture.tsx create mode 100644 src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.test.tsx create mode 100644 src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx create mode 100644 src/renderer/src/components/sidebar/account-menu/SidebarHelpSubmenu.tsx create mode 100644 src/renderer/src/components/sidebar/account-menu/account-menu-identity.ts create mode 100644 src/renderer/src/components/sidebar/account-menu/account-menu-items.test.ts create mode 100644 src/renderer/src/components/sidebar/account-menu/account-menu-items.ts create mode 100644 src/renderer/src/lib/orca-cloud-session-events.ts create mode 100644 src/shared/ephemeral-vm-recipe-step-providers.ts create mode 100644 src/shared/hosted-sandbox/hosted-session-events.ts create mode 100644 src/shared/orca-billing.ts diff --git a/cloud/apps/billing/.gitignore b/cloud/apps/billing/.gitignore new file mode 100644 index 00000000000..a933f108fe0 --- /dev/null +++ b/cloud/apps/billing/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +.wrangler/ +.dev.vars diff --git a/cloud/apps/billing/migrations/0001_init.sql b/cloud/apps/billing/migrations/0001_init.sql new file mode 100644 index 00000000000..6eb0998cc06 --- /dev/null +++ b/cloud/apps/billing/migrations/0001_init.sql @@ -0,0 +1,69 @@ +-- Billing ledger (D1). Identity (users, orgs, membership, roles) stays in Orca Cloud auth; +-- billing only stores the account id the auth token names. Times are epoch milliseconds. + +-- One row per billable account: an org ('org:') or a signed-in user without an org +-- ('user:'). +CREATE TABLE billing_accounts ( + account_id TEXT PRIMARY KEY, + stripe_customer_id TEXT UNIQUE, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL +); + +-- Mirror of the Stripe subscription, rewritten from a fresh API read on every webhook so +-- out-of-order deliveries cannot regress it. +CREATE TABLE subscriptions ( + stripe_subscription_id TEXT PRIMARY KEY, + account_id TEXT NOT NULL REFERENCES billing_accounts(account_id), + stripe_customer_id TEXT NOT NULL, + status TEXT NOT NULL, + plan TEXT NOT NULL, + seats INTEGER NOT NULL, + overage_enabled INTEGER NOT NULL, + current_period_start INTEGER NOT NULL, + current_period_end INTEGER NOT NULL, + cancel_at_period_end INTEGER NOT NULL, + updated_at INTEGER NOT NULL +); +CREATE INDEX subscriptions_account ON subscriptions(account_id, current_period_end); + +-- Webhook idempotency: an event id is processed at most once (processed_at set on success). +CREATE TABLE stripe_events ( + event_id TEXT PRIMARY KEY, + type TEXT NOT NULL, + received_at INTEGER NOT NULL, + processed_at INTEGER, + last_error TEXT +); + +-- Sandbox usage segments reported by the control plane. usage_id is the caller's idempotency +-- key. weighted_seconds = wall seconds x machine size weight. overage_seconds is the part of +-- this segment past the period's included allowance, fixed when the row is inserted. +CREATE TABLE usage_events ( + usage_id TEXT PRIMARY KEY, + account_id TEXT NOT NULL, + sandbox_id TEXT NOT NULL, + machine_size TEXT NOT NULL, + started_at INTEGER NOT NULL, + ended_at INTEGER NOT NULL, + weighted_seconds INTEGER NOT NULL, + period_start INTEGER, + overage_seconds INTEGER NOT NULL, + created_at INTEGER NOT NULL +); +CREATE INDEX usage_events_period ON usage_events(account_id, period_start); + +-- Outbox for Stripe meter events (overage only). Sent inline, retried by the cron trigger. +-- usage_id doubles as the Stripe meter event identifier, so a retry is deduplicated by Stripe. +CREATE TABLE meter_reports ( + usage_id TEXT PRIMARY KEY REFERENCES usage_events(usage_id), + stripe_customer_id TEXT NOT NULL, + seconds INTEGER NOT NULL, + event_timestamp INTEGER NOT NULL, + status TEXT NOT NULL, + attempts INTEGER NOT NULL DEFAULT 0, + last_error TEXT, + created_at INTEGER NOT NULL, + sent_at INTEGER +); +CREATE INDEX meter_reports_pending ON meter_reports(status, created_at); diff --git a/cloud/apps/billing/migrations/0002_team_overage.sql b/cloud/apps/billing/migrations/0002_team_overage.sql new file mode 100644 index 00000000000..be5b5dc29f3 --- /dev/null +++ b/cloud/apps/billing/migrations/0002_team_overage.sql @@ -0,0 +1,10 @@ +-- Team pricing: overage is opt-in per account with an admin-set monthly spend cap, and annual +-- plans keep a monthly allowance window (usage_events.period_start is that window's start). + +-- 1 once an admin opts in to overage; spend cap in cents (NULL = the plan default). +ALTER TABLE billing_accounts ADD COLUMN overage_opt_in INTEGER NOT NULL DEFAULT 0; +ALTER TABLE billing_accounts ADD COLUMN spend_cap_cents INTEGER; + +-- 'month' | 'year' (the seat price's interval) and the metered overage item, once added. +ALTER TABLE subscriptions ADD COLUMN billing_interval TEXT NOT NULL DEFAULT 'month'; +ALTER TABLE subscriptions ADD COLUMN overage_item_id TEXT; diff --git a/cloud/apps/billing/migrations/0003_payment_grace.sql b/cloud/apps/billing/migrations/0003_payment_grace.sql new file mode 100644 index 00000000000..0a2c3f2ec86 --- /dev/null +++ b/cloud/apps/billing/migrations/0003_payment_grace.sql @@ -0,0 +1,4 @@ +-- Payment failure grace: when billing first saw the subscription past_due/unpaid (epoch ms). +-- Cleared when it is paid again. New starts stop immediately; running sandboxes are suspended +-- once the plan's grace period has passed (entitlements.ts). +ALTER TABLE subscriptions ADD COLUMN past_due_since INTEGER; diff --git a/cloud/apps/billing/package.json b/cloud/apps/billing/package.json new file mode 100644 index 00000000000..453c1a82ecc --- /dev/null +++ b/cloud/apps/billing/package.json @@ -0,0 +1,23 @@ +{ + "name": "@orca-cloud/billing", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "wrangler dev --port 8790 --ip 127.0.0.1", + "db:migrate:local": "wrangler d1 migrations apply BILLING_DB --local", + "typecheck": "tsc --noEmit", + "test": "vitest run" + }, + "dependencies": { + "jose": "^6.2.13", + "zod": "^4.6.7" + }, + "devDependencies": { + "@cloudflare/workers-types": "^5.20261103.1", + "@types/node": "^24.19.2", + "typescript": "^5.9.3", + "vitest": "^5.0.5", + "wrangler": "^4.157.0" + } +} diff --git a/cloud/apps/billing/scripts/e2e-test-mode.ts b/cloud/apps/billing/scripts/e2e-test-mode.ts new file mode 100644 index 00000000000..19722f3c439 --- /dev/null +++ b/cloud/apps/billing/scripts/e2e-test-mode.ts @@ -0,0 +1,334 @@ +// End-to-end billing run against a running Worker (local `wrangler dev` or a dev deploy) and +// Stripe TEST MODE, with real webhooks (via `stripe listen` locally, or a test webhook endpoint). +// +// tsx scripts/e2e-test-mode.ts keys # local-only ES256 key + JWKS (stands in for auth) +// tsx scripts/e2e-test-mode.ts run # drive the flows below +// +// Env for `run`: BILLING_URL, STRIPE_SECRET_KEY (sk_test_/rk_test_ only), CONTROL_PLANE_TOKEN, +// E2E_JWK_PRIVATE (path), E2E_CATALOG (path to the stripe-test-setup.ts JSON), E2E_ISSUER, +// E2E_D1_ARGS (wrangler d1 execute target, e.g. "BILLING_DB --local"). +// +// Flows: Checkout (2 seats, test card, headless browser) -> overage opt-in -> usage past the +// included hours -> meter events in Stripe -> spend cap blocks starts -> annual plan + overage -> +// test-clock renewal -> invoice.payment_failed -> cancel. Prints PASS/FAIL per check and the ids. +import { execFileSync } from 'node:child_process' +import { readFileSync, writeFileSync } from 'node:fs' +import { SignJWT, exportJWK, generateKeyPair, importJWK } from 'jose' +import { encodeForm } from '../src/stripe' + +const HOUR = 3600 +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +if (process.argv[2] === 'keys') { + const dir = process.argv[3]! + const { privateKey, publicKey } = await generateKeyPair('ES256', { extractable: true }) + writeFileSync(`${dir}/jwk-private.json`, JSON.stringify({ ...(await exportJWK(privateKey)), kid: 'e2e', alg: 'ES256' }), { + mode: 0o600 + }) + writeFileSync(`${dir}/jwks.json`, JSON.stringify({ keys: [{ ...(await exportJWK(publicKey)), kid: 'e2e', alg: 'ES256', use: 'sig' }] })) + console.log(`wrote ${dir}/jwk-private.json and ${dir}/jwks.json`) + process.exit(0) +} + +const env = (name: string): string => { + const value = process.env[name] + if (!value) { + throw new Error(`missing ${name}`) + } + return value +} +const BILLING_URL = env('BILLING_URL') +const STRIPE_KEY = env('STRIPE_SECRET_KEY') +if (!STRIPE_KEY.startsWith('sk_test_') && !STRIPE_KEY.startsWith('rk_test_')) { + throw new Error('Refusing: test-mode Stripe key required') +} +const CONTROL = env('CONTROL_PLANE_TOKEN') +const catalog = JSON.parse(readFileSync(env('E2E_CATALOG'), 'utf8')) as Record & { meter: string } +const jwk = JSON.parse(readFileSync(env('E2E_JWK_PRIVATE'), 'utf8')) as { kid: string } +const signingKey = await importJWK(jwk, 'ES256') +const issuer = env('E2E_ISSUER') +const d1Args = env('E2E_D1_ARGS').split(' ') +const runId = Date.now().toString(36) + +const results: Array<{ ok: boolean; name: string; detail: string }> = [] +function check(name: string, ok: boolean, detail: unknown = ''): boolean { + results.push({ ok, name, detail: typeof detail === 'string' ? detail : JSON.stringify(detail) }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${ok ? '' : ` :: ${typeof detail === 'string' ? detail : JSON.stringify(detail)}`}`) + return ok +} +const ids: Record = {} + +async function token(org: string): Promise { + const now = Math.floor(Date.now() / 1000) + return new SignJWT({ sub: `user_${org}`, org, orgRole: 'owner', email: `e2e+${org}@example.com`, purpose: 'billing' }) + .setProtectedHeader({ alg: 'ES256', kid: jwk.kid }) + .setIssuer(issuer) + .setAudience('orca-billing') + .setIssuedAt(now) + .setExpirationTime(now + 300) + .sign(signingKey) +} + +async function billing(method: string, path: string, org: string | null, body?: unknown) { + const headers: Record = { 'content-type': 'application/json' } + headers.authorization = `Bearer ${org ? await token(org) : CONTROL}` + const response = await fetch(`${BILLING_URL}${path}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) }) + return { status: response.status, body: (await response.json().catch(() => ({}))) as Record } +} + +async function stripe>(method: 'GET' | 'POST' | 'DELETE', path: string, params?: Record): Promise { + const url = new URL(`https://api.stripe.com${path}`) + if (method !== 'POST' && params) { + url.search = encodeForm(params as never) + } + const response = await fetch(url, { + method, + headers: { authorization: `Bearer ${STRIPE_KEY}`, 'content-type': 'application/x-www-form-urlencoded', 'stripe-version': '2025-09-30.clover' }, + body: method === 'POST' ? encodeForm((params ?? {}) as never) : undefined + }) + const json = (await response.json()) as T & { livemode?: boolean; error?: { message: string } } + if (!response.ok) { + throw new Error(`stripe ${method} ${path}: ${json.error?.message}`) + } + if (json.livemode) { + throw new Error('live-mode object returned; aborting') + } + return json +} + +async function until(what: string, read: () => Promise, done: (value: T) => boolean, timeoutMs = 90_000): Promise { + const deadline = Date.now() + timeoutMs + let last = await read() + while (!done(last) && Date.now() < deadline) { + await sleep(2000) + last = await read() + } + if (!done(last)) { + console.log(` (timed out waiting for ${what})`) + } + return last +} + +const entitlement = (org: string) => billing('GET', '/v1/billing/entitlements', org).then((r) => r.body) +const startCheck = (accountId: string) => + billing('POST', '/v1/internal/entitlements/check', null, { accountId, machineSize: 'standard', runningSandboxes: 0 }).then((r) => r.body) +let usageCounter = 0 +async function reportUsage(accountId: string, hours: number) { + const endedAt = Date.now() - 60_000 - usageCounter * 1000 + usageCounter++ + return billing('POST', '/v1/internal/usage', null, { + usageId: `e2e-${runId}-${usageCounter}`, + accountId, + sandboxId: `sbx-${runId}`, + machineSize: 'standard', + startedAt: endedAt - hours * HOUR * 1000, + endedAt + }).then((r) => r.body) +} + +function linkAccount(accountId: string, customer: string) { + if (!/^[A-Za-z0-9_:.-]+$/.test(accountId + customer)) { + throw new Error('unsafe id') + } + const now = Date.now() + execFileSync( + 'npx', + ['wrangler', 'd1', 'execute', ...d1Args, '--command', + `INSERT INTO billing_accounts (account_id, stripe_customer_id, created_at, updated_at) VALUES ('${accountId}', '${customer}', ${now}, ${now})`], + { stdio: 'ignore', env: { ...process.env, CI: '1' } } + ) +} + +async function completeCheckout(url: string) { + const { chromium } = await import('playwright-core') + const browser = await chromium.launch({ headless: true }) + try { + const page = await browser.newPage() + await page.goto(url, { waitUntil: 'domcontentloaded' }) + await page.locator('#cardNumber').waitFor({ timeout: 60_000 }) + await page.fill('#cardNumber', '4242424242424242') + await page.fill('#cardExpiry', '12/34') + await page.fill('#cardCvc', '123') + if (await page.locator('#billingName').count()) { + await page.fill('#billingName', 'Orca E2E') + } + if (await page.locator('#billingPostalCode').count()) { + await page.fill('#billingPostalCode', '94107') + } + // Opting out of "Save my information with Link" (it would require a phone number). + const link = page.locator('#enableStripePass') + if ((await link.count()) && (await link.isChecked())) { + await link.uncheck({ force: true }) + } + await page.locator('[data-testid="hosted-payment-submit-button"], .SubmitButton').first().click() + try { + await page.waitForURL((current) => current.searchParams.get('checkout') === 'success', { timeout: 90_000 }) + } catch (error) { + if (process.env.E2E_SCREENSHOT_DIR) { + await page.screenshot({ path: `${process.env.E2E_SCREENSHOT_DIR}/checkout-failure.png`, fullPage: true }) + } + throw error + } + } finally { + await browser.close() + } +} + +async function advanceClock(clock: string, to: number) { + await stripe('POST', `/v1/test_helpers/test_clocks/${clock}/advance`, { frozen_time: to }) + await until('test clock ready', () => stripe('GET', `/v1/test_helpers/test_clocks/${clock}`), (c) => c.status === 'ready', 180_000) +} + +// ---- 1. Checkout, 2 seats, test card ---------------------------------------------------------- +const orgA = `e2e-${runId}-a` +const accountA = `org:${orgA}` +const checkout = await billing('POST', '/v1/billing/checkout', orgA, { plan: 'team', seats: 2, interval: 'month' }) +check('checkout session created (hosted Stripe URL)', checkout.status === 200 && /^https:\/\/checkout\.stripe\.com\//.test(checkout.body.url), checkout) +await completeCheckout(checkout.body.url) +const afterCheckout = await until('active subscription', () => entitlement(orgA), (e) => e.status === 'active') +check('webhook -> 2-seat Team active, 120 h included, 6 concurrent', afterCheckout.status === 'active' && afterCheckout.seats === 2 && + afterCheckout.includedSeconds === 120 * HOUR && afterCheckout.limits?.maxConcurrentSandboxes === 6, afterCheckout) +// List-by-email is consistent (search is not); the email is unique to this run. +const customerA = (await stripe<{ data: Array<{ id: string; metadata: Record }> }>('GET', '/v1/customers', { + email: `e2e+${orgA}@example.com` +})).data.find((customer) => customer.metadata.account_id === accountA)?.id ?? '' +const subsA = await stripe<{ data: Array<{ id: string; items: { data: Array<{ price: { id: string } }> } }> }>('GET', '/v1/subscriptions', { customer: customerA }) +ids.checkoutCustomer = customerA +ids.checkoutSubscription = subsA.data[0]?.id ?? '' + +// ---- 2. Opt in to overage -------------------------------------------------------------------- +const optIn = await billing('POST', '/v1/billing/overage', orgA, { enabled: true }) +const subA = await stripe<{ items: { data: Array<{ price: { id: string } }> } }>('GET', `/v1/subscriptions/${ids.checkoutSubscription}`) +check('overage opt-in adds the metered item ($25 default cap)', optIn.status === 200 && optIn.body.overage?.active === true && + optIn.body.overage?.spendCapCents === 2500 && subA.items.data.some((item) => item.price.id === catalog.STRIPE_PRICE_OVERAGE), optIn.body) + +// ---- 3. Usage past included hours -> meter events -------------------------------------------- +const meterStart = Math.floor(Date.now() / 1000 / 60) * 60 - 2 * 24 * HOUR +for (let i = 0; i < 5; i++) { + await reportUsage(accountA, 24) +} +const crossing = await reportUsage(accountA, 4) +check('usage past 120 h: 4 h overage recorded, starts still allowed', crossing.overageSeconds === 4 * HOUR && crossing.canStartSandbox === true, crossing) +const warned = await entitlement(orgA) +check('100% included-hours warning', warned.usageWarningPercent === 100, warned.usageWarningPercent) + +// ---- 4. Spend cap ---------------------------------------------------------------------------- +await reportUsage(accountA, 20) +await reportUsage(accountA, 20) +const capped = await startCheck(accountA) +check('spend cap ($25 = 33.3 overage h) reached -> new starts blocked', capped.allow === false && capped.reason === 'overage_cap_reached', capped) +const spent = await entitlement(orgA) +check('metered spend stops at the cap', spent.overage?.spentCents <= 2500 && spent.overage?.spentCents >= 2490, spent.overage) + +const meterEnd = Math.ceil(Date.now() / 1000 / 60) * 60 + 60 +const summaries = await until( + 'meter event summaries', + () => stripe<{ data: Array<{ aggregated_value: number }> }>('GET', `/v1/billing/meters/${catalog.meter}/event_summaries`, { + customer: customerA, start_time: meterStart, end_time: meterEnd + }), + (s) => s.data.reduce((sum, row) => sum + row.aggregated_value, 0) >= Math.floor((2500 / 75) * HOUR), + 240_000 +) +const meteredTotal = summaries.data.reduce((sum, row) => sum + row.aggregated_value, 0) +check('Stripe meter received exactly the capped overage seconds', meteredTotal === Math.floor((2500 / 75) * HOUR), { meteredTotal }) + +// ---- 5. Portal ------------------------------------------------------------------------------ +// ---- 5a. Seats follow membership (auth pushes member counts) ------------------------------------ +const membershipToken = process.env.E2E_MEMBERSHIP_TOKEN +if (membershipToken) { + const push = async (members: number) => { + const response = await fetch(`${BILLING_URL}/v1/internal/seats`, { + method: 'POST', + headers: { authorization: `Bearer ${membershipToken}`, 'content-type': 'application/json' }, + body: JSON.stringify({ accountId: accountA, members }) + }) + return (await response.json()) as Record + } + const seatItem = async () => + (await stripe<{ items: { data: Array<{ quantity: number; price: { id: string } }> } }>('GET', `/v1/subscriptions/${ids.checkoutSubscription}`)) + .items.data.find((item) => item.price.id === catalog.STRIPE_PRICE_TEAM_MONTHLY)?.quantity + const added = await push(3) + const invoices = await stripe<{ data: Array<{ billing_reason: string; status: string; amount_due: number }> }>('GET', '/v1/invoices', { + subscription: ids.checkoutSubscription, limit: 1 + }) + check('member added -> 3 seats, prorated invoice charged now', added.status === 'updated' && (await seatItem()) === 3 && + invoices.data[0]?.billing_reason === 'subscription_update' && invoices.data[0]?.status === 'paid', { added, invoice: invoices.data[0] }) + const removed = await push(2) + const afterRemove = await entitlement(orgA) + check('member removed -> 2 seats (credit on next invoice), 120 h included', removed.status === 'updated' && (await seatItem()) === 2 && + afterRemove.seats === 2 && afterRemove.includedSeconds === 120 * HOUR, { removed, seats: afterRemove.seats }) + check('pending invites do not change seats (same member count is a no-op)', (await push(2)).status === 'unchanged') +} + +const portal = await billing('POST', '/v1/billing/portal', orgA) +check('customer portal session (seats, monthly<->annual, cancel config)', /^https:\/\/billing\.stripe\.com\//.test(portal.body.url ?? ''), portal.body) + +// ---- 6. Annual plan + overage (mixed intervals) ----------------------------------------------- +const orgB = `e2e-${runId}-b` +const customerB = await stripe<{ id: string }>('POST', '/v1/customers', { email: `e2e+${orgB}@example.com`, metadata: { account_id: `org:${orgB}` } }) +ids.annualCustomer = customerB.id +linkAccount(`org:${orgB}`, customerB.id) +await stripe('POST', '/v1/payment_methods/pm_card_visa/attach', { customer: customerB.id }) + .then((pm: any) => stripe('POST', `/v1/customers/${customerB.id}`, { invoice_settings: { default_payment_method: pm.id } })) +const annualSub = await stripe<{ id: string }>('POST', '/v1/subscriptions', { + customer: customerB.id, items: [{ price: catalog.STRIPE_PRICE_TEAM_ANNUAL, quantity: 3 }], metadata: { account_id: `org:${orgB}` } +}) +ids.annualSubscription = annualSub.id +const annualEnt = await until('annual active', () => entitlement(orgB), (e) => e.status === 'active') +check('annual Team: 3 seats, 180 h monthly allowance window', annualEnt.interval === 'year' && annualEnt.includedSeconds === 180 * HOUR && + annualEnt.usageWindow && annualEnt.usageWindow.end - annualEnt.usageWindow.start <= 31 * 24 * HOUR * 1000, annualEnt) +const annualOptIn = await billing('POST', '/v1/billing/overage', orgB, { enabled: true }) +check('annual plan accepts the monthly metered overage item', annualOptIn.status === 200 && annualOptIn.body.overage?.active === true, annualOptIn) + +// ---- 7. Test clock: renewal, then invoice.payment_failed -------------------------------------- +const orgC = `e2e-${runId}-c` +const start = Math.floor(Date.now() / 1000) +const clock = await stripe<{ id: string }>('POST', '/v1/test_helpers/test_clocks', { frozen_time: start, name: `orca-e2e-${runId}` }) +ids.testClock = clock.id +const customerC = await stripe<{ id: string }>('POST', '/v1/customers', { email: `e2e+${orgC}@example.com`, test_clock: clock.id, metadata: { account_id: `org:${orgC}` } }) +ids.clockCustomer = customerC.id +linkAccount(`org:${orgC}`, customerC.id) +const visa = await stripe<{ id: string }>('POST', '/v1/payment_methods/pm_card_visa/attach', { customer: customerC.id }) +await stripe('POST', `/v1/customers/${customerC.id}`, { invoice_settings: { default_payment_method: visa.id } }) +const clockSub = await stripe<{ id: string }>('POST', '/v1/subscriptions', { + customer: customerC.id, items: [{ price: catalog.STRIPE_PRICE_TEAM_MONTHLY, quantity: 2 }], metadata: { account_id: `org:${orgC}` } +}) +ids.clockSubscription = clockSub.id +const before = await until('clock sub active', () => entitlement(orgC), (e) => e.status === 'active') +await advanceClock(clock.id, before.period.end / 1000 + HOUR) +const renewed = await until('renewal synced', () => entitlement(orgC), (e) => e.period?.start === before.period.end, 120_000) +check('test-clock renewal rolls the period (invoice.paid -> new window)', renewed.period?.start === before.period.end && renewed.status === 'active', { + before: before.period, after: renewed.period +}) +const failing = await stripe<{ id: string }>('POST', '/v1/payment_methods/pm_card_chargeCustomerFail/attach', { customer: customerC.id }) +await stripe('POST', `/v1/customers/${customerC.id}`, { invoice_settings: { default_payment_method: failing.id } }) +await advanceClock(clock.id, renewed.period.end / 1000 + HOUR) +const pastDue = await until('past_due synced', () => entitlement(orgC), (e) => e.status === 'past_due', 120_000) +check('invoice.payment_failed -> past_due -> starts blocked', pastDue.status === 'past_due' && pastDue.reason === 'payment_past_due', pastDue.status) +const graceSeconds = Number(process.env.E2E_GRACE_SECONDS ?? 4 * 24 * HOUR) +check('grace window recorded (suspend running after the grace)', pastDue.payment?.suspendRunning === false && + pastDue.payment?.graceEndsAt - pastDue.payment?.pastDueSince === graceSeconds * 1000, pastDue.payment) +if (graceSeconds <= 120) { + // Short grace configured for this run (BILLING_PLANS team.paymentGraceHours): watch it expire. + const suspended = await until('grace expiry', () => startCheck(`org:${orgC}`), (c) => c.suspendRunning === true, (graceSeconds + 60) * 1000) + check('grace expired -> suspendRunning, reason payment_grace_expired', suspended.suspendRunning === true && suspended.reason === 'payment_grace_expired', suspended) + const listed = await billing('GET', '/v1/internal/suspensions', null).then((r) => r.body) + check('/v1/internal/suspensions lists the account', listed.accounts?.some((a: any) => a.accountId === `org:${orgC}`), listed) + // Pay the open invoice with a good card -> active -> resume allowed. + const goodCard = await stripe<{ id: string }>('POST', '/v1/payment_methods/pm_card_visa/attach', { customer: customerC.id }) + await stripe('POST', `/v1/customers/${customerC.id}`, { invoice_settings: { default_payment_method: goodCard.id } }) + const live = await stripe<{ latest_invoice: string }>('GET', `/v1/subscriptions/${clockSub.id}`) + await stripe('POST', `/v1/invoices/${live.latest_invoice}/pay`, { payment_method: goodCard.id }) + const resumed = await until('paid', () => startCheck(`org:${orgC}`), (c) => c.allow === true, 120_000) + check('paid -> resume allowed, suspendRunning cleared', resumed.allow === true && resumed.suspendRunning === false, resumed) +} + +// ---- 8. Cancel ------------------------------------------------------------------------------- +await stripe('DELETE', `/v1/subscriptions/${ids.checkoutSubscription}`) +const canceled = await until('cancel synced', () => entitlement(orgA), (e) => e.status === 'canceled') +check('cancel -> subscription_inactive, starts blocked', canceled.status === 'canceled' && canceled.canStartSandbox === false, canceled.status) + +console.log(JSON.stringify({ runId, ids }, null, 2)) +const failed = results.filter((r) => !r.ok) +console.log(`${results.length - failed.length}/${results.length} checks passed`) +process.exit(failed.length === 0 ? 0 : 1) diff --git a/cloud/apps/billing/scripts/stripe-test-setup.ts b/cloud/apps/billing/scripts/stripe-test-setup.ts new file mode 100644 index 00000000000..02df0148a71 --- /dev/null +++ b/cloud/apps/billing/scripts/stripe-test-setup.ts @@ -0,0 +1,136 @@ +// One-time TEST MODE catalog setup: the Team product with monthly + annual seat prices, the +// sandbox-seconds meter with its metered overage price, and a Customer Portal configuration +// (monthly <-> annual switch, cancel; seats follow membership). Refuses any key that is not +// sk_test_/rk_test_ and stops if Stripe ever reports a live-mode object. Prints the Worker vars. +// +// STRIPE_SECRET_KEY=sk_test_... BILLING_RETURN_URL=https://... tsx scripts/stripe-test-setup.ts +// +// Prices (cents) default to the agreed Team pricing; override with +// STRIPE_SETUP_PRICES='{"teamMonthly":5000,"teamAnnual":48000,"overagePerHour":75}' +// Idempotency keys include the prices, so a re-run within 24h returns the same objects. +import { encodeForm } from '../src/stripe' + +const key = process.env.STRIPE_SECRET_KEY ?? '' +if (!key.startsWith('sk_test_') && !key.startsWith('rk_test_')) { + console.error('Refusing: STRIPE_SECRET_KEY must be a test-mode key (sk_test_ or rk_test_).') + process.exit(1) +} +const returnUrl = process.env.BILLING_RETURN_URL ?? 'https://onorca.dev/billing/return' + +type StripeObject = { id: string; livemode?: boolean; error?: { message: string } } + +async function post(path: string, params: Record, idempotencyKey: string): Promise { + const response = await fetch(`https://api.stripe.com${path}`, { + method: 'POST', + headers: { + authorization: `Bearer ${key}`, + 'content-type': 'application/x-www-form-urlencoded', + 'stripe-version': '2025-09-30.clover', + 'idempotency-key': `orca-billing-setup:${idempotencyKey}` + }, + body: encodeForm(params as never) + }) + const json = (await response.json()) as T + if (!response.ok) { + throw new Error(`${path}: ${json.error?.message}`) + } + if (json.livemode) { + throw new Error('Stripe reported a live-mode object; stopping.') + } + return json +} + +const prices = { teamMonthly: 5000, teamAnnual: 48000, overagePerHour: 75, ...JSON.parse(process.env.STRIPE_SETUP_PRICES ?? '{}') } +const suffix = `${prices.teamMonthly}-${prices.teamAnnual}-${prices.overagePerHour}` + +const team = await post('/v1/products', { name: 'Orca Team', unit_label: 'seat', metadata: { orca_plan: 'team' } }, 'product:team') +const monthly = await post( + '/v1/prices', + { + product: team.id, + currency: 'usd', + unit_amount: prices.teamMonthly, + recurring: { interval: 'month' }, + lookup_key: 'orca_team_seat_monthly', + transfer_lookup_key: true + }, + `price:team:month:${suffix}` +) +const annual = await post( + '/v1/prices', + { + product: team.id, + currency: 'usd', + unit_amount: prices.teamAnnual, + recurring: { interval: 'year' }, + lookup_key: 'orca_team_seat_annual', + transfer_lookup_key: true + }, + `price:team:year:${suffix}` +) + +const meter = await post( + '/v1/billing/meters', + { + display_name: 'Orca sandbox overage (standard seconds)', + event_name: 'orca_sandbox_seconds', + default_aggregation: { formula: 'sum' }, + customer_mapping: { type: 'by_id', event_payload_key: 'stripe_customer_id' }, + value_settings: { event_payload_key: 'value' } + }, + 'meter' +) +const overageProduct = await post('/v1/products', { name: 'Orca sandbox overage', unit_label: 'std hour' }, 'product:overage') +// Per Standard hour (Large burns 2x through the meter weight); the meter counts seconds and the +// invoice divides by 3600, rounding up. +const overage = await post( + '/v1/prices', + { + product: overageProduct.id, + currency: 'usd', + unit_amount: prices.overagePerHour, + recurring: { interval: 'month', usage_type: 'metered', meter: meter.id }, + transform_quantity: { divide_by: 3600, round: 'up' }, + lookup_key: 'orca_sandbox_overage_hourly', + transfer_lookup_key: true + }, + `price:overage:${suffix}` +) + +const portal = await post( + '/v1/billing_portal/configurations', + { + business_profile: { headline: 'Orca Team billing' }, + default_return_url: returnUrl, + features: { + customer_update: { enabled: true, allowed_updates: ['email', 'address', 'tax_id', 'name'] }, + invoice_history: { enabled: true }, + payment_method_update: { enabled: true }, + subscription_cancel: { enabled: true, mode: 'at_period_end', cancellation_reason: { enabled: true, options: ['too_expensive', 'missing_features', 'switched_service', 'unused', 'other'] } }, + subscription_update: { + enabled: true, + // Seats follow org membership (seats.ts); the portal only switches monthly <-> annual. + default_allowed_updates: ['price'], + proration_behavior: 'create_prorations', + products: [{ product: team.id, prices: [monthly.id, annual.id] }] + } + } + }, + `portal:${team.id}:${monthly.id}:${annual.id}` +) + +console.log( + JSON.stringify( + { + STRIPE_PRICE_TEAM_MONTHLY: monthly.id, + STRIPE_PRICE_TEAM_ANNUAL: annual.id, + STRIPE_PRICE_OVERAGE: overage.id, + STRIPE_METER_EVENT_NAME: meter.event_name, + STRIPE_PORTAL_CONFIGURATION: portal.id, + products: { team: team.id, overage: overageProduct.id }, + meter: meter.id + }, + null, + 2 + ) +) diff --git a/cloud/apps/billing/src/auth.ts b/cloud/apps/billing/src/auth.ts new file mode 100644 index 00000000000..1c80efbb520 --- /dev/null +++ b/cloud/apps/billing/src/auth.ts @@ -0,0 +1,146 @@ +// Caller authentication. +// - Desktop: a short-lived ES256 JWT minted by Orca Cloud auth for the signed-in user +// (aud = BILLING_JWT_AUDIENCE, purpose = 'billing'), verified against the auth JWKS — the same +// pattern the relay uses, so billing never sees the user's refresh or access token. +// - Control plane: a static bearer secret (rotation: comma-separated), compared in constant time. +import { createLocalJWKSet, createRemoteJWKSet, jwtVerify, type JSONWebKeySet, type JWTVerifyGetKey } from 'jose' +import { z } from 'zod' +import type { Env } from './env' +import { constantTimeEqual } from './webhook-signature' + +const ID = z.string().min(1).max(128).regex(/^[A-Za-z0-9_.:-]+$/) + +const ClaimsSchema = z.object({ + sub: ID, + org: ID.optional(), + // Role in `org`. Only owners and admins may buy or manage the org's plan. + orgRole: z.enum(['owner', 'admin', 'member']).optional(), + email: z.string().email().max(320).optional(), + purpose: z.literal('billing') +}) + +export type BillingCaller = { + userId: string + accountId: string + canManage: boolean + email?: string +} + +const keySets = new Map() + +function keysFor(env: Env): JWTVerifyGetKey { + const source = env.BILLING_JWKS_URL ?? `inline:${env.BILLING_JWKS ?? ''}` + let keys = keySets.get(source) + if (!keys) { + keys = env.BILLING_JWKS_URL + ? createRemoteJWKSet(new URL(env.BILLING_JWKS_URL)) + : createLocalJWKSet(JSON.parse(env.BILLING_JWKS ?? '{"keys":[]}') as JSONWebKeySet) + keySets.set(source, keys) + } + return keys +} + +export const DEV_ISSUER = 'orca-billing-dev-test' +let devKeys: { source: string; keys: JWTVerifyGetKey } | null = null + +/** Dev operator keys; never honored when BILLING_ENV is 'prod'. */ +function devKeysFor(env: Env): JWTVerifyGetKey | null { + if (env.BILLING_ENV === 'prod' || !env.BILLING_DEV_JWKS) { + return null + } + if (devKeys?.source !== env.BILLING_DEV_JWKS) { + devKeys = { + source: env.BILLING_DEV_JWKS, + keys: createLocalJWKSet(JSON.parse(env.BILLING_DEV_JWKS) as JSONWebKeySet) + } + } + return devKeys.keys +} + +export function readBearer(value: string | null): string | null { + return /^Bearer ([^\s]+)$/.exec(value ?? '')?.[1] ?? null +} + +/** An org pays for its members; a user without an org is their own account. */ +export function accountIdFor(claims: { sub: string; org?: string }): string { + return claims.org ? `org:${claims.org}` : `user:${claims.sub}` +} + +// Billing tokens re-resolve org and role from auth's org_memberships rows when minted and live 5 +// minutes. Actions that spend money or change billing take only tokens minted in the last few +// minutes, so a demotion or removal stops them within that window (auth backdates iat by 30 s). +export const ADMIN_ACTION_MAX_TOKEN_AGE = '6m' + +export async function authenticateDesktop( + env: Env, + request: Request, + options: { maxTokenAge?: string } = {} +): Promise { + const token = readBearer(request.headers.get('authorization')) + if (!token) { + return null + } + const verify = (keys: JWTVerifyGetKey, issuer: string | undefined) => + jwtVerify(token, keys, { + audience: env.BILLING_JWT_AUDIENCE, + algorithms: ['ES256'], + // Billing tokens are minted per call; refuse anything long-lived. + maxTokenAge: options.maxTokenAge ?? '15m', + requiredClaims: ['exp', 'iat'], + ...(issuer ? { issuer } : {}) + }) + try { + let verified + try { + verified = await verify(keysFor(env), env.BILLING_AUTH_ISSUER) + } catch (error) { + // Dev only: an operator key set (its own issuer) until auth mints billing tokens. + const dev = devKeysFor(env) + if (!dev) { + throw error + } + verified = await verify(dev, DEV_ISSUER) + } + const claims = ClaimsSchema.parse(verified.payload) + return { + userId: claims.sub, + accountId: accountIdFor(claims), + canManage: !claims.org || claims.orgRole === 'owner' || claims.orgRole === 'admin', + email: claims.email + } + } catch { + return null + } +} + +async function digest(value: string): Promise { + const bytes = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(value))) + return Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join('') +} + +export function authenticateControlPlane(env: Env, request: Request): Promise { + return authenticateSharedSecret(env.CONTROL_PLANE_TOKEN, request) +} + +/** Orca Cloud auth -> billing membership pushes; a separate secret from the control plane's. */ +export function authenticateMembershipSync(env: Env, request: Request): Promise { + return authenticateSharedSecret(env.MEMBERSHIP_SYNC_TOKEN, request) +} + +async function authenticateSharedSecret(secret: string | undefined, request: Request): Promise { + const token = readBearer(request.headers.get('authorization')) + const accepted = (secret ?? '') + .split(',') + .map((value) => value.trim()) + .filter((value) => value.length >= 32) + if (!token || accepted.length === 0) { + return false + } + // Hash both sides so the comparison is fixed-length and constant time. + const presented = await digest(token) + let matched = false + for (const candidate of accepted) { + matched = constantTimeEqual(presented, await digest(candidate)) || matched + } + return matched +} diff --git a/cloud/apps/billing/src/checkout.ts b/cloud/apps/billing/src/checkout.ts new file mode 100644 index 00000000000..ec5f766d321 --- /dev/null +++ b/cloud/apps/billing/src/checkout.ts @@ -0,0 +1,106 @@ +// Hosted Checkout and Customer Portal sessions. Card data only ever touches Stripe's pages. +import { z } from 'zod' +import type { BillingCaller } from './auth' +import type { Env } from './env' +import { BILLING_INTERVALS, PLAN_IDS, priceForPlan, type BillingInterval, type PlanId } from './plans' +import type { StripeClient } from './stripe' +import { currentSubscription, customerFor, ensureAccount, linkCustomer } from './store' +import { orgIdOf, type MembershipSource } from './membership' + +export const MAX_TEAM_SEATS = 500 + +export const CheckoutRequestSchema = z + .object({ + plan: z.enum(PLAN_IDS), + interval: z.enum(BILLING_INTERVALS).default('month'), + seats: z.number().int().min(1).max(MAX_TEAM_SEATS).default(1) + }) + .strict() + +export const OPEN_STATUSES = new Set(['active', 'trialing', 'past_due', 'incomplete', 'unpaid', 'paused']) + +export type CheckoutOutcome = { ok: true; url: string } | { ok: false; status: number; error: string } + +function returnUrl(env: Env, params: Record): string { + const url = new URL(env.BILLING_RETURN_URL) + for (const [key, value] of Object.entries(params)) { + url.searchParams.set(key, value) + } + // Stripe substitutes this literal placeholder; URLSearchParams would percent-encode the braces. + return url.toString().replace('CHECKOUT_SESSION_ID_PLACEHOLDER', '{CHECKOUT_SESSION_ID}') +} + +async function ensureCustomer(env: Env, stripe: StripeClient, caller: BillingCaller, now: number): Promise { + await ensureAccount(env.BILLING_DB, caller.accountId, now) + const existing = await customerFor(env.BILLING_DB, caller.accountId) + if (existing) { + return existing + } + const created = await stripe.createCustomer({ accountId: caller.accountId, email: caller.email }) + return linkCustomer(env.BILLING_DB, caller.accountId, created.id, now) +} + +export async function createCheckout( + env: Env, + stripe: StripeClient, + caller: BillingCaller, + request: { plan: PlanId; interval: BillingInterval; seats: number }, + now: number, + membership: MembershipSource +): Promise { + if (!caller.canManage) { + return { ok: false, status: 403, error: 'billing_admin_required' } + } + const existing = await currentSubscription(env.BILLING_DB, caller.accountId) + if (existing && OPEN_STATUSES.has(existing.status)) { + // Seat and interval changes go through the portal, which prorates; a second subscription + // would double-bill. + return { ok: false, status: 409, error: 'already_subscribed' } + } + // Seats = org members from auth when it is reachable; never fewer than 1. Throws (-> 503) if + // auth is configured but unavailable rather than guessing a seat count. + const orgId = orgIdOf(caller.accountId) + const live = orgId ? await membership(orgId) : null + const seats = Math.max(1, live ?? request.seats) + const customer = await ensureCustomer(env, stripe, caller, now) + const metadata = { account_id: caller.accountId, plan: request.plan, interval: request.interval } + const session = await stripe.createCheckoutSession( + { + mode: 'subscription', + customer, + client_reference_id: caller.accountId, + // Seats only. Overage is opt-in later; its metered item is added then (overage.ts). + line_items: [ + // Seats then follow org membership automatically (seats.ts); not editable on the page. + { price: priceForPlan(env, request.interval), quantity: seats } + ], + payment_method_collection: 'always', + allow_promotion_codes: true, + customer_update: { address: 'auto', name: 'auto' }, + success_url: returnUrl(env, { checkout: 'success', session_id: 'CHECKOUT_SESSION_ID_PLACEHOLDER' }), + cancel_url: returnUrl(env, { checkout: 'cancel' }), + metadata, + ...(env.STRIPE_AUTOMATIC_TAX === 'true' + ? { automatic_tax: { enabled: true }, tax_id_collection: { enabled: true } } + : {}), + subscription_data: { metadata } + }, + `checkout:${caller.accountId}:${crypto.randomUUID()}` + ) + return { ok: true, url: session.url } +} + +export async function createPortal(env: Env, stripe: StripeClient, caller: BillingCaller): Promise { + if (!caller.canManage) { + return { ok: false, status: 403, error: 'billing_admin_required' } + } + const customer = await customerFor(env.BILLING_DB, caller.accountId) + if (!customer) { + return { ok: false, status: 404, error: 'no_billing_account' } + } + const session = await stripe.createPortalSession( + { customer, returnUrl: returnUrl(env, { portal: 'return' }), configuration: env.STRIPE_PORTAL_CONFIGURATION }, + `portal:${caller.accountId}:${crypto.randomUUID()}` + ) + return { ok: true, url: session.url } +} diff --git a/cloud/apps/billing/src/entitlements.ts b/cloud/apps/billing/src/entitlements.ts new file mode 100644 index 00000000000..24ea3cc3a3b --- /dev/null +++ b/cloud/apps/billing/src/entitlements.ts @@ -0,0 +1,179 @@ +// "Can this account start a sandbox, and how many hours are left?" One computation shared by the +// desktop endpoint and the control plane's start check, so the two can never disagree. +import type { Env } from './env' +import { + MACHINE_SIZES, + allowanceWindow, + capSeconds, + includedSeconds, + planCatalog, + type BillingInterval, + type MachineSize, + type PlanId +} from './plans' +import { accountSettings, currentSubscription, meteredSeconds, periodUsage, type SubscriptionRow } from './store' +import { PAYMENT_FAILED_STATUSES } from './webhook' + +export type DenyReason = + | 'no_subscription' + | 'subscription_inactive' + | 'payment_past_due' + | 'payment_grace_expired' + | 'included_hours_exhausted' + | 'overage_cap_reached' + | 'machine_size_not_in_plan' + | 'concurrency_limit' + +export type Entitlement = { + accountId: string + plan: PlanId | null + planLabel: string | null + status: string + interval: BillingInterval | null + seats: number + /** Billing period (a year for annual plans). */ + period: { start: number; end: number } | null + /** Monthly allowance window the hours below count against. */ + usageWindow: { start: number; end: number } | null + cancelAtPeriodEnd: boolean + includedSeconds: number + usedSeconds: number + remainingIncludedSeconds: number + /** Highest configured warning level reached (e.g. 80, 100), or null. */ + usageWarningPercent: number | null + overageSeconds: number + overage: { + optedIn: boolean + /** Opted in and the metered item is on the subscription: overage is being billed. */ + active: boolean + spendCapCents: number + spentCents: number + centsPerHour: number + } + limits: { maxConcurrentSandboxes: number; machineSizes: readonly MachineSize[]; idleSleepMinutes: number } + /** Failed payment: new starts stop at once; running sandboxes must be suspended after the grace. */ + payment: { pastDueSince: number | null; graceEndsAt: number | null; suspendRunning: boolean } + canStartSandbox: boolean + reason: DenyReason | null +} + +const LIVE_STATUSES = new Set(['active', 'trialing']) + +export function subscriptionWindow(subscription: SubscriptionRow, at: number): { start: number; end: number } { + return allowanceWindow({ start: subscription.current_period_start, end: subscription.current_period_end }, at) +} + +export async function computeEntitlement( + env: Pick, + accountId: string, + now: number = Date.now() +): Promise { + const db = env.BILLING_DB + const team = planCatalog(env).team + const [subscription, settings] = await Promise.all([currentSubscription(db, accountId), accountSettings(db, accountId)]) + const spendCapCents = settings.spend_cap_cents ?? team.defaultSpendCapCents + const overageBase = { optedIn: settings.overage_opt_in === 1, spendCapCents, centsPerHour: team.overageCentsPerHour } + if (!subscription) { + return { + accountId, + plan: null, + planLabel: null, + status: 'none', + interval: null, + seats: 0, + period: null, + usageWindow: null, + cancelAtPeriodEnd: false, + includedSeconds: 0, + usedSeconds: 0, + remainingIncludedSeconds: 0, + usageWarningPercent: null, + overageSeconds: 0, + overage: { ...overageBase, active: false, spentCents: 0 }, + limits: { maxConcurrentSandboxes: 0, machineSizes: [], idleSleepMinutes: team.idleSleepMinutes }, + payment: { pastDueSince: null, graceEndsAt: null, suspendRunning: false }, + canStartSandbox: false, + reason: 'no_subscription' + } + } + const window = subscriptionWindow(subscription, now) + const included = includedSeconds(team, subscription.seats) + const [usage, metered] = await Promise.all([ + periodUsage(db, accountId, window.start), + meteredSeconds(db, accountId, window.start) + ]) + const remaining = Math.max(0, included - usage.usedSeconds) + const overageActive = overageBase.optedIn && subscription.overage_item_id !== null + const usedPercent = included > 0 ? (usage.usedSeconds / included) * 100 : 0 + const warnings = team.warnAtPercent.filter((level) => usedPercent >= level) + + const pastDueSince = PAYMENT_FAILED_STATUSES.has(subscription.status) ? (subscription.past_due_since ?? now) : null + const graceEndsAt = pastDueSince === null ? null : pastDueSince + team.paymentGraceHours * 3600_000 + const graceExpired = graceEndsAt !== null && now >= graceEndsAt + + let reason: DenyReason | null = null + if (graceExpired) { + reason = 'payment_grace_expired' + } else if (pastDueSince !== null) { + // Stripe is retrying the card (Smart Retries + dunning emails): no new starts, running ones keep going. + reason = 'payment_past_due' + } else if (!LIVE_STATUSES.has(subscription.status)) { + reason = 'subscription_inactive' + } else if (remaining === 0 && !overageActive) { + reason = 'included_hours_exhausted' + } else if (remaining === 0 && metered >= capSeconds(team, spendCapCents)) { + reason = 'overage_cap_reached' + } + + return { + accountId, + plan: subscription.plan, + planLabel: team.label, + status: subscription.status, + interval: subscription.billing_interval, + seats: subscription.seats, + period: { start: subscription.current_period_start, end: subscription.current_period_end }, + usageWindow: window, + cancelAtPeriodEnd: subscription.cancel_at_period_end === 1, + includedSeconds: included, + usedSeconds: usage.usedSeconds, + remainingIncludedSeconds: remaining, + usageWarningPercent: warnings.length > 0 ? Math.max(...warnings) : null, + overageSeconds: usage.overageSeconds, + overage: { + ...overageBase, + active: overageActive, + spentCents: Math.round((metered / 3600) * team.overageCentsPerHour) + }, + limits: { + maxConcurrentSandboxes: team.concurrentSandboxesPerSeat * Math.max(1, subscription.seats), + machineSizes: team.machineSizes, + idleSleepMinutes: team.idleSleepMinutes + }, + payment: { pastDueSince, graceEndsAt, suspendRunning: graceExpired }, + canStartSandbox: reason === null, + reason + } +} + +export function isMachineSize(value: unknown): value is MachineSize { + return typeof value === 'string' && (MACHINE_SIZES as readonly string[]).includes(value) +} + +/** The control plane's start gate: the account-level answer plus size and concurrency. */ +export function checkStart( + entitlement: Entitlement, + request: { machineSize: MachineSize; runningSandboxes: number } +): { allow: boolean; reason: DenyReason | null; suspendRunning: boolean } { + const suspendRunning = entitlement.payment.suspendRunning + if (!entitlement.canStartSandbox) { + return { allow: false, reason: entitlement.reason, suspendRunning } + } + if (!entitlement.limits.machineSizes.includes(request.machineSize)) { + return { allow: false, reason: 'machine_size_not_in_plan', suspendRunning } + } + if (request.runningSandboxes >= entitlement.limits.maxConcurrentSandboxes) { + return { allow: false, reason: 'concurrency_limit', suspendRunning } + } + return { allow: true, reason: null, suspendRunning } +} diff --git a/cloud/apps/billing/src/env.ts b/cloud/apps/billing/src/env.ts new file mode 100644 index 00000000000..59bd0f02764 --- /dev/null +++ b/cloud/apps/billing/src/env.ts @@ -0,0 +1,60 @@ +// Billing Worker bindings. Secrets are set with `wrangler secret put` (stdin), never in +// wrangler.jsonc or git. Deployed environments live in the private deploy config. +export type Env = { + BILLING_DB: D1Database + + // Stripe. Use a RESTRICTED key (rk_test_...) scoped to: Customers write, Checkout Sessions + // write, Customer portal write, Subscriptions read, Billing meter events write. Nothing else. + STRIPE_SECRET_KEY: string + // Webhook endpoint signing secret(s), comma-separated during rotation (whsec_...). + STRIPE_WEBHOOK_SECRET: string + // Pinned so a dashboard default-version bump cannot change payload shapes under us. + STRIPE_API_VERSION?: string + // Must be exactly 'true' to accept live-mode keys or events. Unset everywhere until launch. + STRIPE_ALLOW_LIVEMODE?: string + // Overrides the Stripe API origin (tests and stripe-mock only). + STRIPE_API_BASE?: string + + // Team seat prices (price_...), quantity = seats. + STRIPE_PRICE_TEAM_MONTHLY: string + STRIPE_PRICE_TEAM_ANNUAL: string + // Monthly metered price on the sandbox-seconds meter, added to a subscription when an admin + // opts in to overage (see overage.ts, usage.ts). + STRIPE_PRICE_OVERAGE: string + // Billing meter event_name the overage price aggregates. + STRIPE_METER_EVENT_NAME: string + // Customer portal configuration (bpc_...) allowing seat changes, monthly<->annual and cancel. + STRIPE_PORTAL_CONFIGURATION?: string + // 'true' turns on Stripe Tax in Checkout (automatic_tax + tax id collection). Requires Stripe Tax + // to be activated with an origin address and registrations in the dashboard. Default off. + STRIPE_AUTOMATIC_TAX?: string + + // Optional JSON overriding any field of the default plan catalog (src/plans.ts), e.g. + // {"pro":{"priceLabel":"$20/mo","includedHoursPerSeat":40}}. Malformed JSON fails closed (503). + BILLING_PLANS?: string + + // Where Checkout and the portal send the browser back (a page that deep-links into Orca). + BILLING_RETURN_URL: string + + // Desktop tokens: short-lived ES256 JWTs minted by Orca Cloud auth with aud=BILLING_JWT_AUDIENCE. + // BILLING_JWKS (inline set) is for local dev and tests only. + BILLING_JWKS_URL?: string + BILLING_JWKS?: string + BILLING_AUTH_ISSUER?: string + BILLING_JWT_AUDIENCE: string + // 'dev' | 'prod'. Dev may also accept BILLING_DEV_JWKS (issuer 'orca-billing-dev-test', a secret + // operator key set for e2e runs before auth mints billing tokens); prod never does. + BILLING_ENV?: string + BILLING_DEV_JWKS?: string + + // Sandbox control plane -> billing (usage reports, start checks). Secret; >= 32 chars. + // Comma-separated during rotation. Replace with a service binding once the control plane is a Worker. + CONTROL_PLANE_TOKEN: string + // Shared secret between Orca Cloud auth and billing for membership: auth pushes member counts to + // POST /v1/internal/seats, billing pulls them from AUTH_INTERNAL_URL. >= 32 chars; comma-separated + // during rotation (billing sends the first). + MEMBERSHIP_SYNC_TOKEN?: string + // Auth origin serving POST /v1/internal/billing/org-members. Unset: seats come from pushes and the + // Checkout request only (local dev). + AUTH_INTERNAL_URL?: string +} diff --git a/cloud/apps/billing/src/index.ts b/cloud/apps/billing/src/index.ts new file mode 100644 index 00000000000..fa18c3af2dc --- /dev/null +++ b/cloud/apps/billing/src/index.ts @@ -0,0 +1,227 @@ +// Orca billing Worker: Stripe Checkout/Portal, Stripe webhooks, entitlements and usage. +// +// Desktop (Orca Cloud billing JWT): +// GET /v1/billing/entitlements current plan, hours used/remaining, can-start +// POST /v1/billing/checkout {plan,interval,seats} -> { url } hosted Stripe Checkout +// POST /v1/billing/overage {enabled, spendCapCents?} opt in/out of overage, set the cap +// POST /v1/billing/portal -> { url } Stripe Customer Portal +// Stripe: +// POST /v1/stripe/webhook signed events +// Sandbox control plane (CONTROL_PLANE_TOKEN): +// POST /v1/internal/entitlements/check {accountId, machineSize, runningSandboxes} +// POST /v1/internal/usage {usageId, accountId, sandboxId, machineSize, startedAt, endedAt} +// GET /v1/internal/suspensions accounts whose payment grace expired (suspend running) +// Orca Cloud auth (MEMBERSHIP_SYNC_TOKEN): +// POST /v1/internal/seats {accountId, members} +import { z } from 'zod' +import { + ADMIN_ACTION_MAX_TOKEN_AGE, + authenticateControlPlane, + authenticateDesktop, + authenticateMembershipSync +} from './auth' +import { SeatSyncSchema, reconcileSeats, syncSeats } from './seats' +import { MembershipUnavailableError, authMembershipSource, type MembershipSource } from './membership' +import { graceExpiredAccounts } from './store' +import { CheckoutRequestSchema, createCheckout, createPortal } from './checkout' +import { checkStart, computeEntitlement } from './entitlements' +import type { Env } from './env' +import { MACHINE_SIZES, PlanConfigError, planCatalog, publicOffers } from './plans' +import { OverageRequestSchema, updateOverage } from './overage' +import { LiveModeRefusedError, StripeError, createStripeClient, type StripeClient } from './stripe' +import { UsageReportSchema, flushMeterReports, recordUsage, validateSegment } from './usage' +import { handleStripeWebhook } from './webhook' + +const MAX_JSON_BYTES = 16 * 1024 + +const StartCheckSchema = z + .object({ + accountId: z.string().min(1).max(128), + machineSize: z.enum(MACHINE_SIZES), + runningSandboxes: z.number().int().min(0).max(10_000) + }) + .strict() + +function json(body: unknown, status = 200): Response { + return Response.json(body, { status, headers: { 'cache-control': 'no-store' } }) +} + +async function readJson(request: Request): Promise { + const text = await request.text() + if (text.length > MAX_JSON_BYTES) { + throw new SyntaxError('too_large') + } + return text ? JSON.parse(text) : {} +} + +export type Deps = { stripe?: StripeClient; now?: () => number; membership?: MembershipSource } + +export async function handleRequest(request: Request, env: Env, ctx: ExecutionContext, deps: Deps = {}): Promise { + const url = new URL(request.url) + const now = deps.now?.() ?? Date.now() + const stripe = deps.stripe ?? createStripeClient(env) + const membership = deps.membership ?? authMembershipSource(env) + const route = `${request.method} ${url.pathname}` + try { + switch (route) { + case 'GET /return': + // Where Checkout and the Portal send the browser back. Static; reveals nothing. + return new Response( + 'Orca' + + '' + + '

You can close this tab and return to Orca.

', + { headers: { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' } } + ) + + case 'POST /v1/stripe/webhook': + return await handleStripeWebhook(env, stripe, request, now) + + case 'GET /v1/billing/entitlements': { + const caller = await authenticateDesktop(env, request) + if (!caller) { + return json({ error: 'unauthorized' }, 401) + } + const entitlement = await computeEntitlement(env, caller.accountId, now) + return json({ ...entitlement, canManageBilling: caller.canManage, offers: publicOffers(planCatalog(env)) }) + } + + case 'POST /v1/billing/checkout': { + const caller = await authenticateDesktop(env, request, { maxTokenAge: ADMIN_ACTION_MAX_TOKEN_AGE }) + if (!caller) { + return json({ error: 'unauthorized' }, 401) + } + const body = CheckoutRequestSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + const outcome = await createCheckout(env, stripe, caller, body.data, now, membership) + return outcome.ok ? json({ url: outcome.url }) : json({ error: outcome.error }, outcome.status) + } + + case 'POST /v1/billing/overage': { + const caller = await authenticateDesktop(env, request, { maxTokenAge: ADMIN_ACTION_MAX_TOKEN_AGE }) + if (!caller) { + return json({ error: 'unauthorized' }, 401) + } + const body = OverageRequestSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + const outcome = await updateOverage(env, stripe, caller, body.data, now) + if (!outcome.ok) { + return json({ error: outcome.error }, outcome.status) + } + const entitlement = await computeEntitlement(env, caller.accountId, now) + return json({ ...entitlement, canManageBilling: caller.canManage, offers: publicOffers(planCatalog(env)) }) + } + + case 'POST /v1/internal/seats': { + if (!(await authenticateMembershipSync(env, request))) { + return json({ error: 'unauthorized' }, 401) + } + const body = SeatSyncSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + return json(await syncSeats(env, stripe, body.data.accountId, body.data.members, now)) + } + + case 'GET /v1/internal/suspensions': { + // Accounts whose payment grace ran out: suspend their running sandboxes (poll from a cron). + if (!(await authenticateControlPlane(env, request))) { + return json({ error: 'unauthorized' }, 401) + } + const graceMs = planCatalog(env).team.paymentGraceHours * 3600_000 + const accounts = await graceExpiredAccounts(env.BILLING_DB, now - graceMs) + return json({ + accounts: accounts.map((row) => ({ + accountId: row.account_id, + pastDueSince: row.past_due_since, + graceEndsAt: row.past_due_since + graceMs + })) + }) + } + + case 'POST /v1/billing/portal': { + const caller = await authenticateDesktop(env, request, { maxTokenAge: ADMIN_ACTION_MAX_TOKEN_AGE }) + if (!caller) { + return json({ error: 'unauthorized' }, 401) + } + const outcome = await createPortal(env, stripe, caller) + return outcome.ok ? json({ url: outcome.url }) : json({ error: outcome.error }, outcome.status) + } + + case 'POST /v1/internal/entitlements/check': { + if (!(await authenticateControlPlane(env, request))) { + return json({ error: 'unauthorized' }, 401) + } + const body = StartCheckSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + const entitlement = await computeEntitlement(env, body.data.accountId, now) + return json({ ...checkStart(entitlement, body.data), entitlement }) + } + + case 'POST /v1/internal/usage': { + if (!(await authenticateControlPlane(env, request))) { + return json({ error: 'unauthorized' }, 401) + } + const body = UsageReportSchema.safeParse(await readJson(request)) + if (!body.success) { + return json({ error: 'invalid_request' }, 400) + } + const invalid = validateSegment(body.data, now) + if (invalid) { + return json({ error: invalid }, 400) + } + const result = await recordUsage(env, body.data, now) + if (result.overageSeconds > 0) { + // Best effort inline; the cron trigger retries whatever is still pending. + ctx.waitUntil(flushMeterReports(env, stripe, now, { usageId: body.data.usageId }).catch(() => undefined)) + } + const entitlement = await computeEntitlement(env, body.data.accountId, now) + return json({ + ...result, + canStartSandbox: entitlement.canStartSandbox, + reason: entitlement.reason, + suspendRunning: entitlement.payment.suspendRunning + }) + } + + default: + return json({ error: 'not_found' }, 404) + } + } catch (error) { + if (error instanceof SyntaxError) { + return json({ error: 'invalid_json' }, 400) + } + if (error instanceof LiveModeRefusedError || error instanceof PlanConfigError) { + return json({ error: 'billing_misconfigured' }, 503) + } + if (error instanceof MembershipUnavailableError) { + // Never guess a seat count: fail the action until auth answers. + console.error(JSON.stringify({ route, error: 'membership', message: error.message })) + return json({ error: 'membership_unavailable' }, 503) + } + if (error instanceof StripeError) { + // Stripe's message never contains the key or card data. + console.error(JSON.stringify({ route, error: 'stripe', status: error.status, code: error.code, message: error.message })) + return json({ error: 'stripe_unavailable' }, 502) + } + console.error(JSON.stringify({ route, error: error instanceof Error ? error.name : 'unknown', message: error instanceof Error ? error.message.slice(0, 200) : '' })) + return json({ error: 'internal_error' }, 500) + } +} + +export default { + fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { + return handleRequest(request, env, ctx) + }, + async scheduled(_controller: ScheduledController, env: Env, ctx: ExecutionContext): Promise { + const stripe = createStripeClient(env) + ctx.waitUntil(flushMeterReports(env, stripe, Date.now()).then(() => undefined)) + // Seat drift from a missed membership push heals here (needs AUTH_INTERNAL_URL). + ctx.waitUntil(reconcileSeats(env, stripe, authMembershipSource(env), Date.now()).then(() => undefined)) + } +} satisfies ExportedHandler diff --git a/cloud/apps/billing/src/membership.ts b/cloud/apps/billing/src/membership.ts new file mode 100644 index 00000000000..d83d1066fe1 --- /dev/null +++ b/cloud/apps/billing/src/membership.ts @@ -0,0 +1,52 @@ +// Seat count source of truth: Orca Cloud auth's org membership (org_memberships rows; pending +// invites excluded). Auth pushes changes to POST /v1/internal/seats; billing also pulls the live +// count when it needs one (Checkout, the reconcile cron) so a missed push self-heals. +import { z } from 'zod' +import type { Env } from './env' + +const MemberCountResponse = z.object({ orgId: z.string(), members: z.number().int().min(0) }) + +export class MembershipUnavailableError extends Error { + constructor(message: string) { + super(message) + this.name = 'MembershipUnavailableError' + } +} + +/** Live member count from auth, or null when no auth URL is configured (local dev and tests). */ +export type MembershipSource = (orgId: string) => Promise + +export function authMembershipSource(env: Env, fetchImpl: typeof fetch = fetch): MembershipSource { + return async (orgId) => { + if (!env.AUTH_INTERNAL_URL) { + return null + } + if (!env.MEMBERSHIP_SYNC_TOKEN) { + throw new MembershipUnavailableError('membership token not configured') + } + let response: Response + try { + response = await fetchImpl(new URL('/v1/internal/billing/org-members', env.AUTH_INTERNAL_URL), { + method: 'POST', + headers: { authorization: `Bearer ${env.MEMBERSHIP_SYNC_TOKEN.split(',')[0]!.trim()}`, 'content-type': 'application/json' }, + body: JSON.stringify({ orgId }), + redirect: 'manual', + signal: AbortSignal.timeout(10_000) + }) + } catch { + throw new MembershipUnavailableError('auth unreachable') + } + if (!response.ok) { + throw new MembershipUnavailableError(`auth answered ${response.status}`) + } + const parsed = MemberCountResponse.safeParse(await response.json().catch(() => null)) + if (!parsed.success || parsed.data.orgId !== orgId) { + throw new MembershipUnavailableError('auth answered an unexpected body') + } + return parsed.data.members + } +} + +export function orgIdOf(accountId: string): string | null { + return accountId.startsWith('org:') ? accountId.slice(4) : null +} diff --git a/cloud/apps/billing/src/overage.ts b/cloud/apps/billing/src/overage.ts new file mode 100644 index 00000000000..69c03b17de5 --- /dev/null +++ b/cloud/apps/billing/src/overage.ts @@ -0,0 +1,52 @@ +// Overage is opt-in per account (off by default) with a monthly spend cap admins can raise. +// Opting in adds the metered overage price to the Stripe subscription (once); opting out stops +// metering immediately but leaves the item, so usage already reported is still invoiced. +import { z } from 'zod' +import type { BillingCaller } from './auth' +import { OPEN_STATUSES } from './checkout' +import type { Env } from './env' +import { planCatalog } from './plans' +import type { StripeClient } from './stripe' +import { accountSettings, currentSubscription, setOverageSettings, upsertSubscription } from './store' + +export const OverageRequestSchema = z + .object({ + enabled: z.boolean(), + // Whole cents; null restores the plan default. + spendCapCents: z.number().int().min(0).nullable().optional() + }) + .strict() + +export type OverageOutcome = { ok: true } | { ok: false; status: number; error: string } + +export async function updateOverage( + env: Env, + stripe: StripeClient, + caller: BillingCaller, + request: z.infer, + now: number +): Promise { + if (!caller.canManage) { + return { ok: false, status: 403, error: 'billing_admin_required' } + } + const team = planCatalog(env).team + if (request.spendCapCents != null && request.spendCapCents > team.maxSpendCapCents) { + return { ok: false, status: 400, error: 'spend_cap_too_high' } + } + const subscription = await currentSubscription(env.BILLING_DB, caller.accountId) + if (!subscription || !OPEN_STATUSES.has(subscription.status)) { + return { ok: false, status: 409, error: 'no_subscription' } + } + if (request.enabled && !subscription.overage_item_id) { + // Idempotent per subscription+price, so a retried opt-in cannot add a second item. + const item = await stripe.addSubscriptionItem({ subscription: subscription.stripe_subscription_id, price: env.STRIPE_PRICE_OVERAGE }) + await upsertSubscription(env.BILLING_DB, { ...subscription, overage_enabled: 1, overage_item_id: item.id, updated_at: now }) + } + // An omitted cap keeps the current one; null restores the plan default. + const spendCapCents = + request.spendCapCents === undefined + ? (await accountSettings(env.BILLING_DB, caller.accountId)).spend_cap_cents + : request.spendCapCents + await setOverageSettings(env.BILLING_DB, caller.accountId, { optIn: request.enabled, spendCapCents }, now) + return { ok: true } +} diff --git a/cloud/apps/billing/src/plans.ts b/cloud/apps/billing/src/plans.ts new file mode 100644 index 00000000000..959a25681c9 --- /dev/null +++ b/cloud/apps/billing/src/plans.ts @@ -0,0 +1,184 @@ +// Plan catalog. One self-serve plan (Team, per seat, monthly or annual) plus Enterprise (contact +// sales, no Stripe price). Stripe price ids are Worker vars; every number and label below is a +// default that BILLING_PLANS (JSON) can override, so pricing changes need no code change. +// +// Usage is metered in "standard seconds": wall-clock seconds x the machine size weight, so one +// included hour is one hour on the Standard 4 vCPU / 8 GB box and a Large box burns 2x. +// Included hours are a MONTHLY per-seat allowance pooled across the org, for annual plans too. +import { z } from 'zod' +import type { Env } from './env' + +export const PLAN_IDS = ['team'] as const +export type PlanId = (typeof PLAN_IDS)[number] +export const BILLING_INTERVALS = ['month', 'year'] as const +export type BillingInterval = (typeof BILLING_INTERVALS)[number] + +export const MACHINE_SIZES = ['standard', 'large'] as const +export type MachineSize = (typeof MACHINE_SIZES)[number] +export const MACHINE_SIZE_WEIGHT: Record = { standard: 1, large: 2 } + +const TeamSchema = z + .object({ + label: z.string().min(1).max(40), + // Display only; Stripe's prices are what is charged. + monthlyPriceLabel: z.string().max(40), + annualPriceLabel: z.string().max(40), + // Display only, for totals and the annual saving on the plans page; must match Stripe's prices. + monthlyPriceCentsPerSeat: z.number().int().nonnegative(), + annualPriceCentsPerSeat: z.number().int().nonnegative(), + includedHoursPerSeat: z.number().nonnegative(), + concurrentSandboxesPerSeat: z.number().int().nonnegative(), + machineSizes: z.array(z.enum(MACHINE_SIZES)).min(1), + idleSleepMinutes: z.number().int().positive(), + // Overage: opt-in per account. The Stripe overage price must match overageCentsPerHour. + overageCentsPerHour: z.number().int().positive(), + defaultSpendCapCents: z.number().int().nonnegative(), + maxSpendCapCents: z.number().int().positive(), + // Included-hours usage levels (percent) the desktop warns at. + warnAtPercent: z.array(z.number().int().min(1).max(100)).max(4), + // After a failed payment: new starts stop at once; running sandboxes are suspended after this. + paymentGraceHours: z.number().nonnegative() + }) + .strict() + +const EnterpriseSchema = z + .object({ + label: z.string().min(1).max(40), + priceLabel: z.string().max(40), + contactUrl: z.string().url().startsWith('https://') + }) + .strict() + +export type TeamPlan = z.infer +export type EnterpriseOffer = z.infer +export type PlanCatalog = { team: TeamPlan; enterprise: EnterpriseOffer } + +export const DEFAULT_PLANS: PlanCatalog = { + team: { + label: 'Team', + monthlyPriceLabel: '$50/seat/mo', + annualPriceLabel: '$480/seat/yr', + monthlyPriceCentsPerSeat: 5000, + annualPriceCentsPerSeat: 48000, + includedHoursPerSeat: 60, + concurrentSandboxesPerSeat: 3, + machineSizes: ['standard', 'large'], + idleSleepMinutes: 15, + overageCentsPerHour: 75, + defaultSpendCapCents: 2500, + maxSpendCapCents: 500_000, + warnAtPercent: [80, 100], + paymentGraceHours: 96 + }, + enterprise: { + label: 'Enterprise', + priceLabel: 'Custom', + contactUrl: 'https://onorca.dev/contact-sales' + } +} + +const OverridesSchema = z + .object({ team: TeamSchema.partial().optional(), enterprise: EnterpriseSchema.partial().optional() }) + .strict() + +export class PlanConfigError extends Error { + constructor() { + super('billing_plans_invalid') + this.name = 'PlanConfigError' + } +} + +const catalogs = new Map() + +/** The effective catalog. Throws PlanConfigError (-> 503) on malformed BILLING_PLANS. */ +export function planCatalog(env: Pick): PlanCatalog { + const raw = env.BILLING_PLANS?.trim() ?? '' + const cached = catalogs.get(raw) + if (cached) { + return cached + } + let catalog: PlanCatalog + try { + const overrides = raw ? OverridesSchema.parse(JSON.parse(raw)) : {} + catalog = { + team: TeamSchema.parse({ ...DEFAULT_PLANS.team, ...overrides.team }), + enterprise: EnterpriseSchema.parse({ ...DEFAULT_PLANS.enterprise, ...overrides.enterprise }) + } + } catch { + throw new PlanConfigError() + } + catalogs.set(raw, catalog) + return catalog +} + +/** What the desktop shows before Checkout. */ +export function publicOffers(catalog: PlanCatalog) { + const team = catalog.team + return { + team: { + label: team.label, + monthlyPriceLabel: team.monthlyPriceLabel, + annualPriceLabel: team.annualPriceLabel, + monthlyPriceCentsPerSeat: team.monthlyPriceCentsPerSeat, + annualPriceCentsPerSeat: team.annualPriceCentsPerSeat, + largeMachineWeight: MACHINE_SIZE_WEIGHT.large, + includedHoursPerSeat: team.includedHoursPerSeat, + concurrentSandboxesPerSeat: team.concurrentSandboxesPerSeat, + idleSleepMinutes: team.idleSleepMinutes, + overageCentsPerHour: team.overageCentsPerHour, + defaultSpendCapCents: team.defaultSpendCapCents + }, + enterprise: catalog.enterprise + } +} + +export function priceForPlan(env: Env, interval: BillingInterval): string { + return interval === 'year' ? env.STRIPE_PRICE_TEAM_ANNUAL : env.STRIPE_PRICE_TEAM_MONTHLY +} + +/** The plan and interval a subscription's seat price maps to, or null for a price we did not configure. */ +export function planForPrice(env: Env, priceId: string): { plan: PlanId; interval: BillingInterval } | null { + if (priceId === env.STRIPE_PRICE_TEAM_MONTHLY) { + return { plan: 'team', interval: 'month' } + } + if (priceId === env.STRIPE_PRICE_TEAM_ANNUAL) { + return { plan: 'team', interval: 'year' } + } + return null +} + +export function includedSeconds(team: TeamPlan, seats: number): number { + return Math.round(team.includedHoursPerSeat * 3600) * Math.max(1, seats) +} + +/** The spend cap expressed as overage seconds (what the meter counts). */ +export function capSeconds(team: TeamPlan, spendCapCents: number): number { + return Math.floor((spendCapCents / team.overageCentsPerHour) * 3600) +} + +function addUtcMonths(ms: number, months: number): number { + const date = new Date(ms) + const day = date.getUTCDate() + const target = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + months, 1, date.getUTCHours(), + date.getUTCMinutes(), date.getUTCSeconds(), date.getUTCMilliseconds())) + // Clamp to the target month's last day (Jan 31 + 1 month = Feb 28/29), like Stripe's anchors. + const lastDay = new Date(Date.UTC(target.getUTCFullYear(), target.getUTCMonth() + 1, 0)).getUTCDate() + target.setUTCDate(Math.min(day, lastDay)) + return target.getTime() +} + +/** + * The monthly allowance window containing `at`: the billing period itself for monthly plans, + * one calendar-month slice of the year (anchored on the period start) for annual plans. + */ +export function allowanceWindow(period: { start: number; end: number }, at: number): { start: number; end: number } { + let start = period.start + for (let month = 1; month <= 12; month++) { + const next = Math.min(addUtcMonths(period.start, month), period.end) + if (at < next || next >= period.end) { + return { start, end: next } + } + start = next + } + return { start, end: period.end } +} diff --git a/cloud/apps/billing/src/seats.ts b/cloud/apps/billing/src/seats.ts new file mode 100644 index 00000000000..f0ba30f4d0c --- /dev/null +++ b/cloud/apps/billing/src/seats.ts @@ -0,0 +1,101 @@ +// Team seats follow org membership: seats = members (pending invites excluded), minimum 1. +// Membership is owned by Orca Cloud auth, which reports it either by pushing +// (POST /v1/internal/seats) or in the billing token's `orgMembers` claim. +// +// Proration (Stripe's model, documented in cloud/docs/accounts-billing-design.md): +// - Adding seats: `always_invoice` - the prorated amount for the rest of the period is invoiced and +// charged now (on annual plans this avoids a seat being free until renewal). +// - Removing seats: `create_prorations` - Stripe's default; the unused time becomes a credit on +// the next invoice. Nothing is refunded to the card. +import { z } from 'zod' +import { OPEN_STATUSES } from './checkout' +import type { Env } from './env' +import { MAX_TEAM_SEATS } from './checkout' +import { planForPrice } from './plans' +import type { StripeClient } from './stripe' +import { currentSubscription, openOrgAccounts } from './store' +import { orgIdOf, type MembershipSource } from './membership' +import { syncSubscription } from './webhook' + +export const SeatSyncSchema = z + .object({ + accountId: z.string().min(1).max(128).regex(/^org:[A-Za-z0-9_.:-]+$/), + members: z.number().int().min(0).max(100_000) + }) + .strict() + +export type SeatSyncOutcome = + | { status: 'unchanged'; seats: number } + | { status: 'updated'; seats: number; previous: number } + | { status: 'no_subscription' } + | { status: 'too_many_seats' } + +export async function syncSeats( + env: Env, + stripe: StripeClient, + accountId: string, + members: number, + now: number +): Promise { + const subscription = await currentSubscription(env.BILLING_DB, accountId) + if (!subscription || !OPEN_STATUSES.has(subscription.status)) { + return { status: 'no_subscription' } + } + const desired = Math.max(1, members) + if (desired > MAX_TEAM_SEATS) { + return { status: 'too_many_seats' } + } + if (desired === subscription.seats) { + return { status: 'unchanged', seats: desired } + } + // Read the live item: the mirror can lag a webhook, and the item id is not stored. + const live = await stripe.retrieveSubscription(subscription.stripe_subscription_id) + const seatItem = live.items.data.find((item) => planForPrice(env, item.price.id)) + if (!seatItem) { + return { status: 'no_subscription' } + } + const current = seatItem.quantity ?? 1 + if (current !== desired) { + await stripe.setSubscriptionItemQuantity({ + item: seatItem.id, + quantity: desired, + prorationBehavior: desired > current ? 'always_invoice' : 'create_prorations', + // Same change retried within Stripe's idempotency window is applied once. + idempotencyKey: `seats:${seatItem.id}:${current}->${desired}` + }) + } + await syncSubscription(env, stripe, subscription.stripe_subscription_id, now) + return { status: 'updated', seats: desired, previous: current } +} + +/** Cron: re-read every open org's member count from auth and fix any drift (a missed push). */ +export async function reconcileSeats( + env: Env, + stripe: StripeClient, + membership: MembershipSource, + now: number +): Promise<{ checked: number; updated: number; failed: number }> { + let checked = 0 + let updated = 0 + let failed = 0 + for (const accountId of await openOrgAccounts(env.BILLING_DB, 200)) { + const orgId = orgIdOf(accountId) + if (!orgId) { + continue + } + try { + const members = await membership(orgId) + if (members === null) { + return { checked, updated, failed } + } + checked++ + const outcome = await syncSeats(env, stripe, accountId, members, now) + if (outcome.status === 'updated') { + updated++ + } + } catch { + failed++ + } + } + return { checked, updated, failed } +} diff --git a/cloud/apps/billing/src/store.ts b/cloud/apps/billing/src/store.ts new file mode 100644 index 00000000000..aea3fa322f2 --- /dev/null +++ b/cloud/apps/billing/src/store.ts @@ -0,0 +1,329 @@ +// D1 access. Every multi-statement write goes through db.batch() (one transaction), and every +// insert that can be retried is INSERT OR IGNORE on a caller-supplied key. +import type { BillingInterval, PlanId } from './plans' + +export type SubscriptionRow = { + stripe_subscription_id: string + account_id: string + stripe_customer_id: string + status: string + plan: PlanId + seats: number + overage_enabled: number + current_period_start: number + current_period_end: number + cancel_at_period_end: number + updated_at: number + billing_interval: BillingInterval + overage_item_id: string | null + past_due_since: number | null +} + +export type AccountSettings = { overage_opt_in: number; spend_cap_cents: number | null } + +export type MeterReportRow = { + usage_id: string + stripe_customer_id: string + seconds: number + event_timestamp: number + attempts: number +} + +export async function ensureAccount(db: D1Database, accountId: string, now: number): Promise { + await db + .prepare('INSERT OR IGNORE INTO billing_accounts (account_id, created_at, updated_at) VALUES (?1, ?2, ?2)') + .bind(accountId, now) + .run() +} + +export async function customerFor(db: D1Database, accountId: string): Promise { + const row = await db + .prepare('SELECT stripe_customer_id FROM billing_accounts WHERE account_id = ?1') + .bind(accountId) + .first<{ stripe_customer_id: string | null }>() + return row?.stripe_customer_id ?? null +} + +export async function accountForCustomer(db: D1Database, customerId: string): Promise { + const row = await db + .prepare('SELECT account_id FROM billing_accounts WHERE stripe_customer_id = ?1') + .bind(customerId) + .first<{ account_id: string }>() + return row?.account_id ?? null +} + +/** Links only an unlinked account, so a race between two first checkouts keeps the first. */ +export async function linkCustomer(db: D1Database, accountId: string, customerId: string, now: number): Promise { + await db + .prepare( + 'UPDATE billing_accounts SET stripe_customer_id = ?2, updated_at = ?3 WHERE account_id = ?1 AND stripe_customer_id IS NULL' + ) + .bind(accountId, customerId, now) + .run() + return (await customerFor(db, accountId)) ?? customerId +} + +// Live statuses sort first, then the latest period: the row entitlements are computed from. +export async function currentSubscription(db: D1Database, accountId: string): Promise { + return db + .prepare( + `SELECT * FROM subscriptions WHERE account_id = ?1 + ORDER BY CASE status WHEN 'active' THEN 0 WHEN 'trialing' THEN 0 WHEN 'past_due' THEN 1 ELSE 2 END, + current_period_end DESC + LIMIT 1` + ) + .bind(accountId) + .first() +} + +export async function upsertSubscription(db: D1Database, row: SubscriptionRow): Promise { + await db + .prepare( + `INSERT INTO subscriptions (stripe_subscription_id, account_id, stripe_customer_id, status, plan, seats, + overage_enabled, current_period_start, current_period_end, cancel_at_period_end, updated_at, + billing_interval, overage_item_id, past_due_since) + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14) + ON CONFLICT (stripe_subscription_id) DO UPDATE SET + status = excluded.status, plan = excluded.plan, seats = excluded.seats, + overage_enabled = excluded.overage_enabled, current_period_start = excluded.current_period_start, + current_period_end = excluded.current_period_end, cancel_at_period_end = excluded.cancel_at_period_end, + updated_at = excluded.updated_at, billing_interval = excluded.billing_interval, + overage_item_id = excluded.overage_item_id, + -- Keep the first time it went past due; clear it once paid. + past_due_since = CASE WHEN excluded.past_due_since IS NULL THEN NULL + ELSE COALESCE(subscriptions.past_due_since, excluded.past_due_since) END` + ) + .bind( + row.stripe_subscription_id, + row.account_id, + row.stripe_customer_id, + row.status, + row.plan, + row.seats, + row.overage_enabled, + row.current_period_start, + row.current_period_end, + row.cancel_at_period_end, + row.updated_at, + row.billing_interval, + row.overage_item_id, + row.past_due_since + ) + .run() +} + +export async function accountSettings(db: D1Database, accountId: string): Promise { + const row = await db + .prepare('SELECT overage_opt_in, spend_cap_cents FROM billing_accounts WHERE account_id = ?1') + .bind(accountId) + .first() + return row ?? { overage_opt_in: 0, spend_cap_cents: null } +} + +export async function setOverageSettings( + db: D1Database, + accountId: string, + settings: { optIn: boolean; spendCapCents: number | null }, + now: number +): Promise { + await db + .prepare('UPDATE billing_accounts SET overage_opt_in = ?2, spend_cap_cents = ?3, updated_at = ?4 WHERE account_id = ?1') + .bind(accountId, settings.optIn ? 1 : 0, settings.spendCapCents, now) + .run() +} + +export async function periodUsage( + db: D1Database, + accountId: string, + periodStart: number +): Promise<{ usedSeconds: number; overageSeconds: number }> { + const row = await db + .prepare( + `SELECT COALESCE(SUM(weighted_seconds), 0) AS used, COALESCE(SUM(overage_seconds), 0) AS overage + FROM usage_events WHERE account_id = ?1 AND period_start = ?2` + ) + .bind(accountId, periodStart) + .first<{ used: number; overage: number }>() + return { usedSeconds: row?.used ?? 0, overageSeconds: row?.overage ?? 0 } +} + +export type UsageInsert = { + usageId: string + accountId: string + sandboxId: string + machineSize: string + startedAt: number + endedAt: number + weightedSeconds: number + periodStart: number | null + includedSeconds: number + now: number + // Set when overage is billable: queues a meter report in the same transaction, for at most + // what is left of the window's spend cap (meterCapSeconds). + meterCustomer: string | null + meterCapSeconds: number +} + +/** + * Records one usage segment and, atomically, the overage it adds. The overage is computed inside + * the INSERT from the period's running total, so concurrent reports cannot both claim the same + * included seconds. Returns false when usageId was already recorded (a retry). + */ +export async function insertUsage(db: D1Database, usage: UsageInsert): Promise { + const insert = db + .prepare( + `INSERT OR IGNORE INTO usage_events (usage_id, account_id, sandbox_id, machine_size, started_at, ended_at, + weighted_seconds, period_start, overage_seconds, created_at) + SELECT ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, + CASE WHEN ?8 IS NULL THEN 0 + ELSE MAX(0, MIN(?7, COALESCE(SUM(weighted_seconds), 0) + ?7 - ?9)) END, + ?10 + FROM usage_events WHERE account_id = ?2 AND period_start IS ?8` + ) + .bind( + usage.usageId, + usage.accountId, + usage.sandboxId, + usage.machineSize, + usage.startedAt, + usage.endedAt, + usage.weightedSeconds, + usage.periodStart, + usage.includedSeconds, + usage.now + ) + const statements = [insert] + if (usage.meterCustomer) { + statements.push( + db + .prepare( + `INSERT OR IGNORE INTO meter_reports (usage_id, stripe_customer_id, seconds, event_timestamp, status, created_at) + SELECT usage_id, ?2, billable, ended_at, 'pending', ?3 FROM ( + SELECT u.usage_id, u.ended_at, + MIN(u.overage_seconds, MAX(0, ?4 - ( + SELECT COALESCE(SUM(m.seconds), 0) FROM meter_reports m + JOIN usage_events p ON p.usage_id = m.usage_id + WHERE p.account_id = u.account_id AND p.period_start IS u.period_start))) AS billable + FROM usage_events u WHERE u.usage_id = ?1 AND u.overage_seconds > 0 + ) WHERE billable > 0` + ) + .bind(usage.usageId, usage.meterCustomer, usage.now, usage.meterCapSeconds) + ) + } + const [result] = await db.batch(statements) + return (result?.meta.changes ?? 0) > 0 +} + +/** Overage seconds already metered (billed) in an allowance window. */ +export async function meteredSeconds(db: D1Database, accountId: string, windowStart: number): Promise { + const row = await db + .prepare( + `SELECT COALESCE(SUM(m.seconds), 0) AS metered FROM meter_reports m + JOIN usage_events p ON p.usage_id = m.usage_id + WHERE p.account_id = ?1 AND p.period_start = ?2` + ) + .bind(accountId, windowStart) + .first<{ metered: number }>() + return row?.metered ?? 0 +} + +/** Accounts whose payment grace has run out: the control plane suspends their running sandboxes. */ +export async function graceExpiredAccounts(db: D1Database, cutoff: number): Promise> { + return ( + await db + .prepare( + `SELECT account_id, MIN(past_due_since) AS past_due_since FROM subscriptions + WHERE status IN ('past_due', 'unpaid') AND past_due_since IS NOT NULL AND past_due_since <= ?1 + GROUP BY account_id ORDER BY past_due_since LIMIT 1000` + ) + .bind(cutoff) + .all<{ account_id: string; past_due_since: number }>() + ).results +} + +/** Org accounts with an open subscription, for the seat reconcile cron. */ +export async function openOrgAccounts(db: D1Database, limit: number): Promise { + return ( + await db + .prepare( + `SELECT DISTINCT account_id FROM subscriptions + WHERE account_id LIKE 'org:%' AND status IN ('active', 'trialing', 'past_due', 'unpaid') + ORDER BY updated_at LIMIT ?1` + ) + .bind(limit) + .all<{ account_id: string }>() + ).results.map((row) => row.account_id) +} + +export async function usageOverage(db: D1Database, usageId: string): Promise { + const row = await db + .prepare('SELECT overage_seconds FROM usage_events WHERE usage_id = ?1') + .bind(usageId) + .first<{ overage_seconds: number }>() + return row?.overage_seconds ?? 0 +} + +export async function pendingMeterReports(db: D1Database, limit: number, usageId?: string): Promise { + const query = usageId + ? db + .prepare( + "SELECT usage_id, stripe_customer_id, seconds, event_timestamp, attempts FROM meter_reports WHERE usage_id = ?1 AND status = 'pending'" + ) + .bind(usageId) + : db + .prepare( + "SELECT usage_id, stripe_customer_id, seconds, event_timestamp, attempts FROM meter_reports WHERE status = 'pending' ORDER BY created_at LIMIT ?1" + ) + .bind(limit) + return (await query.all()).results +} + +export async function markMeterReport( + db: D1Database, + usageId: string, + outcome: { sent: true; now: number } | { sent: false; error: string; giveUp: boolean } +): Promise { + if (outcome.sent) { + await db + .prepare("UPDATE meter_reports SET status = 'sent', sent_at = ?2, attempts = attempts + 1 WHERE usage_id = ?1") + .bind(usageId, outcome.now) + .run() + return + } + await db + .prepare('UPDATE meter_reports SET status = ?2, last_error = ?3, attempts = attempts + 1 WHERE usage_id = ?1') + .bind(usageId, outcome.giveUp ? 'failed' : 'pending', outcome.error.slice(0, 200)) + .run() +} + +/** Claims an event id. Returns 'duplicate' when it was already processed successfully. */ +export async function claimEvent(db: D1Database, eventId: string, type: string, now: number): Promise<'new' | 'duplicate'> { + await db + .prepare('INSERT OR IGNORE INTO stripe_events (event_id, type, received_at) VALUES (?1, ?2, ?3)') + .bind(eventId, type, now) + .run() + const row = await db + .prepare('SELECT processed_at FROM stripe_events WHERE event_id = ?1') + .bind(eventId) + .first<{ processed_at: number | null }>() + return row?.processed_at ? 'duplicate' : 'new' +} + +/** `now` marks the event done (with an optional note, e.g. why it was ignored); `error` leaves it retryable. */ +export async function finishEvent( + db: D1Database, + eventId: string, + outcome: { now: number; note?: string } | { error: string } +): Promise { + if ('now' in outcome) { + await db + .prepare('UPDATE stripe_events SET processed_at = ?2, last_error = ?3 WHERE event_id = ?1') + .bind(eventId, outcome.now, outcome.note ?? null) + .run() + return + } + await db + .prepare('UPDATE stripe_events SET last_error = ?2 WHERE event_id = ?1') + .bind(eventId, outcome.error.slice(0, 200)) + .run() +} diff --git a/cloud/apps/billing/src/stripe.ts b/cloud/apps/billing/src/stripe.ts new file mode 100644 index 00000000000..c468ffd9bc5 --- /dev/null +++ b/cloud/apps/billing/src/stripe.ts @@ -0,0 +1,193 @@ +// Minimal Stripe REST client over fetch: only the calls billing makes. Form-encoded bodies, +// pinned API version, an Idempotency-Key on every POST, and a hard refusal of live-mode keys +// unless STRIPE_ALLOW_LIVEMODE is exactly 'true'. +import type { Env } from './env' + +export const DEFAULT_STRIPE_API_VERSION = '2025-09-30.clover' +const STRIPE_TIMEOUT_MS = 15_000 + +export class StripeError extends Error { + constructor( + public readonly status: number, + public readonly code: string | undefined, + message: string + ) { + super(message) + this.name = 'StripeError' + } +} + +export class LiveModeRefusedError extends Error { + constructor() { + super('stripe_livemode_refused') + this.name = 'LiveModeRefusedError' + } +} + +export function allowsLiveMode(env: Env): boolean { + return env.STRIPE_ALLOW_LIVEMODE === 'true' +} + +/** Throws unless the key is a test-mode key (or live mode was explicitly enabled). */ +export function assertKeyMode(env: Env): void { + const key = env.STRIPE_SECRET_KEY ?? '' + const isTest = key.startsWith('sk_test_') || key.startsWith('rk_test_') + const isLive = key.startsWith('sk_live_') || key.startsWith('rk_live_') + if (isLive && !allowsLiveMode(env)) { + throw new LiveModeRefusedError() + } + if (!isTest && !isLive) { + throw new Error('stripe_key_missing') + } +} + +type FormValue = string | number | boolean | null | undefined | FormRecord | FormValue[] +type FormRecord = { [key: string]: FormValue } + +/** Stripe's bracket encoding: a[b][0][c]=v. Undefined/null values are omitted. */ +export function encodeForm(params: FormRecord): string { + const pairs: string[] = [] + const walk = (prefix: string, value: FormValue): void => { + if (value === undefined || value === null) { + return + } + if (Array.isArray(value)) { + value.forEach((item, index) => walk(`${prefix}[${index}]`, item)) + return + } + if (typeof value === 'object') { + for (const [key, inner] of Object.entries(value)) { + walk(prefix ? `${prefix}[${key}]` : key, inner) + } + return + } + pairs.push(`${encodeURIComponent(prefix)}=${encodeURIComponent(String(value))}`) + } + walk('', params) + return pairs.join('&') +} + +export type StripeClient = ReturnType + +export function createStripeClient(env: Env, fetchImpl: typeof fetch = fetch) { + const base = env.STRIPE_API_BASE ?? 'https://api.stripe.com' + const version = env.STRIPE_API_VERSION ?? DEFAULT_STRIPE_API_VERSION + + async function request(method: 'GET' | 'POST', path: string, params?: FormRecord, idempotencyKey?: string) { + assertKeyMode(env) + const headers: Record = { + authorization: `Bearer ${env.STRIPE_SECRET_KEY}`, + 'stripe-version': version + } + let url = `${base}${path}` + let body: string | undefined + if (method === 'POST') { + headers['content-type'] = 'application/x-www-form-urlencoded' + body = encodeForm(params ?? {}) + if (idempotencyKey) { + headers['idempotency-key'] = idempotencyKey + } + } else if (params) { + url += `?${encodeForm(params)}` + } + const response = await fetchImpl(url, { + method, + headers, + body, + // Never follow a redirect with the key attached. Workers reject redirect: 'error', so take + // the 3xx manually and fail it below (a 3xx is never ok). + redirect: 'manual', + signal: AbortSignal.timeout(STRIPE_TIMEOUT_MS) + }) + const json = (await response.json().catch(() => ({}))) as { error?: { code?: string; message?: string } } + if (!response.ok) { + // Stripe's message never contains the key; still keep it short for logs. + throw new StripeError(response.status, json.error?.code, (json.error?.message ?? 'stripe_error').slice(0, 200)) + } + return json as T + } + + return { + createCustomer(args: { accountId: string; email?: string; name?: string }) { + return request<{ id: string }>( + 'POST', + '/v1/customers', + { email: args.email, name: args.name, metadata: { account_id: args.accountId } }, + // Stable per account: a retried first checkout cannot create a second customer. + `customer-create:${args.accountId}` + ) + }, + createCheckoutSession(params: FormRecord, idempotencyKey: string) { + return request<{ id: string; url: string }>('POST', '/v1/checkout/sessions', params, idempotencyKey) + }, + createPortalSession(args: { customer: string; returnUrl: string; configuration?: string }, idempotencyKey: string) { + return request<{ id: string; url: string }>( + 'POST', + '/v1/billing_portal/sessions', + { customer: args.customer, return_url: args.returnUrl, configuration: args.configuration }, + idempotencyKey + ) + }, + addSubscriptionItem(args: { subscription: string; price: string }) { + return request<{ id: string }>( + 'POST', + '/v1/subscription_items', + // Metered: nothing to prorate; usage is billed in arrears. + { subscription: args.subscription, price: args.price, proration_behavior: 'none' }, + `overage-item:${args.subscription}:${args.price}` + ) + }, + setSubscriptionItemQuantity(args: { + item: string + quantity: number + prorationBehavior: 'always_invoice' | 'create_prorations' + idempotencyKey: string + }) { + return request<{ id: string; quantity: number }>( + 'POST', + `/v1/subscription_items/${encodeURIComponent(args.item)}`, + { quantity: args.quantity, proration_behavior: args.prorationBehavior }, + args.idempotencyKey + ) + }, + retrieveSubscription(id: string) { + return request('GET', `/v1/subscriptions/${encodeURIComponent(id)}`) + }, + createMeterEvent(args: { eventName: string; customer: string; value: number; identifier: string; timestamp: number }) { + return request<{ identifier: string }>( + 'POST', + '/v1/billing/meter_events', + { + event_name: args.eventName, + identifier: args.identifier, + timestamp: Math.floor(args.timestamp / 1000), + payload: { stripe_customer_id: args.customer, value: String(args.value) } + }, + `meter:${args.identifier}` + ) + } + } +} + +// Fields billing reads. Since API 2025-03-31 the period lives on each item; older payloads keep it +// on the subscription, so both are read. +export type StripeSubscription = { + id: string + object: 'subscription' + customer: string | { id: string } + status: string + cancel_at_period_end: boolean + livemode: boolean + metadata?: Record + current_period_start?: number + current_period_end?: number + items: { + data: Array<{ + id: string + quantity?: number + current_period_start?: number + current_period_end?: number + price: { id: string } + }> + } +} diff --git a/cloud/apps/billing/src/usage.ts b/cloud/apps/billing/src/usage.ts new file mode 100644 index 00000000000..eccb5a40b9d --- /dev/null +++ b/cloud/apps/billing/src/usage.ts @@ -0,0 +1,124 @@ +// Sandbox usage ingestion (control plane -> billing) and the Stripe meter outbox. +// Only overage reaches Stripe, and only for accounts that opted in, up to their monthly spend cap: +// included hours are tracked here (pooled across seats, monthly even on annual plans, which tiered +// Stripe prices cannot express); the overage price is a flat per-hour metered price on a +// "standard seconds" meter with transform_quantity divide_by=3600. +import { z } from 'zod' +import type { Env } from './env' +import { MACHINE_SIZE_WEIGHT, MACHINE_SIZES, capSeconds, includedSeconds, planCatalog } from './plans' +import { subscriptionWindow } from './entitlements' +import type { StripeClient } from './stripe' +import { + accountSettings, + currentSubscription, + customerFor, + insertUsage, + markMeterReport, + pendingMeterReports, + usageOverage +} from './store' + +// One segment is at most a day: the control plane settles running sandboxes periodically. +const MAX_SEGMENT_MS = 24 * 3600 * 1000 +// Stripe accepts meter events up to 35 days old and 5 minutes in the future. +const MAX_AGE_MS = 30 * 24 * 3600 * 1000 +const FUTURE_SKEW_MS = 5 * 60 * 1000 +export const MAX_METER_ATTEMPTS = 10 + +const ID = z.string().min(1).max(128).regex(/^[A-Za-z0-9_.:-]+$/) + +export const UsageReportSchema = z + .object({ + usageId: ID, + accountId: ID, + sandboxId: ID, + machineSize: z.enum(MACHINE_SIZES), + startedAt: z.number().int().positive(), + endedAt: z.number().int().positive() + }) + .strict() + +export type UsageReport = z.infer + +export function validateSegment(report: UsageReport, now: number): string | null { + if (report.endedAt <= report.startedAt) { + return 'empty_segment' + } + if (report.endedAt - report.startedAt > MAX_SEGMENT_MS) { + return 'segment_too_long' + } + if (report.endedAt > now + FUTURE_SKEW_MS) { + return 'segment_in_future' + } + if (report.endedAt < now - MAX_AGE_MS) { + return 'segment_too_old' + } + return null +} + +export async function recordUsage( + env: Env, + report: UsageReport, + now: number +): Promise<{ recorded: boolean; weightedSeconds: number; overageSeconds: number }> { + const db = env.BILLING_DB + const team = planCatalog(env).team + const [subscription, settings] = await Promise.all([ + currentSubscription(db, report.accountId), + accountSettings(db, report.accountId) + ]) + const wallSeconds = Math.ceil((report.endedAt - report.startedAt) / 1000) + const weightedSeconds = wallSeconds * MACHINE_SIZE_WEIGHT[report.machineSize] + // A segment counts in the allowance window it ended in. + const window = subscription ? subscriptionWindow(subscription, report.endedAt) : null + const billable = subscription !== null && settings.overage_opt_in === 1 && subscription.overage_item_id !== null + const customer = billable ? await customerFor(db, report.accountId) : null + const recorded = await insertUsage(db, { + usageId: report.usageId, + accountId: report.accountId, + sandboxId: report.sandboxId, + machineSize: report.machineSize, + startedAt: report.startedAt, + endedAt: report.endedAt, + weightedSeconds, + periodStart: window?.start ?? null, + includedSeconds: subscription ? includedSeconds(team, subscription.seats) : 0, + now, + meterCustomer: customer, + meterCapSeconds: capSeconds(team, settings.spend_cap_cents ?? team.defaultSpendCapCents) + }) + return { recorded, weightedSeconds, overageSeconds: await usageOverage(db, report.usageId) } +} + +/** Sends pending meter reports (one usage id, or the oldest batch for the cron). */ +export async function flushMeterReports( + env: Env, + stripe: StripeClient, + now: number, + options: { usageId?: string; limit?: number } = {} +): Promise<{ sent: number; failed: number }> { + const pending = await pendingMeterReports(env.BILLING_DB, options.limit ?? 100, options.usageId) + let sent = 0 + let failed = 0 + for (const report of pending) { + try { + await stripe.createMeterEvent({ + eventName: env.STRIPE_METER_EVENT_NAME, + customer: report.stripe_customer_id, + value: report.seconds, + identifier: report.usage_id, + timestamp: report.event_timestamp + }) + await markMeterReport(env.BILLING_DB, report.usage_id, { sent: true, now }) + sent++ + } catch (error) { + failed++ + await markMeterReport(env.BILLING_DB, report.usage_id, { + sent: false, + error: error instanceof Error ? error.message : 'unknown', + giveUp: report.attempts + 1 >= MAX_METER_ATTEMPTS + }) + } + } + return { sent, failed } +} diff --git a/cloud/apps/billing/src/webhook-signature.ts b/cloud/apps/billing/src/webhook-signature.ts new file mode 100644 index 00000000000..9c3d8043f3b --- /dev/null +++ b/cloud/apps/billing/src/webhook-signature.ts @@ -0,0 +1,84 @@ +// Stripe webhook signature verification (https://docs.stripe.com/webhooks#verify-manually): +// header `Stripe-Signature: t=,v1=[,v1=...]`, HMAC-SHA256 over `${t}.${rawBody}` +// keyed by the endpoint secret. Implemented on WebCrypto so the Worker carries no Stripe SDK. +export const DEFAULT_TOLERANCE_SECONDS = 300 + +export type SignatureResult = { ok: true } | { ok: false; reason: 'malformed' | 'stale' | 'mismatch' } + +const encoder = new TextEncoder() + +async function hmacHex(secret: string, payload: string): Promise { + const key = await crypto.subtle.importKey('raw', encoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, [ + 'sign' + ]) + const mac = new Uint8Array(await crypto.subtle.sign('HMAC', key, encoder.encode(payload))) + return Array.from(mac, (byte) => byte.toString(16).padStart(2, '0')).join('') +} + +/** Constant-time over equal-length strings; length itself is not secret (always 64 hex chars). */ +export function constantTimeEqual(a: string, b: string): boolean { + if (a.length !== b.length) { + return false + } + let diff = 0 + for (let i = 0; i < a.length; i++) { + diff |= a.charCodeAt(i) ^ b.charCodeAt(i) + } + return diff === 0 +} + +function parseHeader(header: string): { timestamp: number; signatures: string[] } | null { + let timestamp: number | null = null + const signatures: string[] = [] + for (const part of header.split(',')) { + const index = part.indexOf('=') + if (index <= 0) { + continue + } + const key = part.slice(0, index).trim() + const value = part.slice(index + 1).trim() + if (key === 't' && /^\d{1,12}$/.test(value)) { + timestamp = Number(value) + } else if (key === 'v1' && /^[0-9a-f]{64}$/.test(value)) { + signatures.push(value) + } + } + return timestamp === null || signatures.length === 0 ? null : { timestamp, signatures } +} + +export async function verifyStripeSignature( + rawBody: string, + header: string | null, + secrets: readonly string[], + nowMs: number = Date.now(), + toleranceSeconds: number = DEFAULT_TOLERANCE_SECONDS +): Promise { + const parsed = header ? parseHeader(header) : null + const usable = secrets.filter((secret) => secret.length > 0) + if (!parsed || usable.length === 0) { + return { ok: false, reason: 'malformed' } + } + if (Math.abs(nowMs / 1000 - parsed.timestamp) > toleranceSeconds) { + return { ok: false, reason: 'stale' } + } + const signedPayload = `${parsed.timestamp}.${rawBody}` + for (const secret of usable) { + const expected = await hmacHex(secret, signedPayload) + // Check every candidate so timing does not reveal which one matched. + let matched = false + for (const signature of parsed.signatures) { + matched = constantTimeEqual(expected, signature) || matched + } + if (matched) { + return { ok: true } + } + } + return { ok: false, reason: 'mismatch' } +} + +export function splitSecrets(value: string | undefined): string[] { + return (value ?? '') + .split(',') + .map((secret) => secret.trim()) + .filter(Boolean) +} diff --git a/cloud/apps/billing/src/webhook.ts b/cloud/apps/billing/src/webhook.ts new file mode 100644 index 00000000000..c11056ac5b9 --- /dev/null +++ b/cloud/apps/billing/src/webhook.ts @@ -0,0 +1,167 @@ +// Stripe webhook: verify the signature over the raw body, dedupe by event id, then re-read the +// subscription from the API instead of trusting the (possibly stale, possibly out-of-order) payload. +import type { Env } from './env' +import { planForPrice } from './plans' +import { allowsLiveMode, type StripeClient, type StripeSubscription } from './stripe' +import { accountForCustomer, claimEvent, customerFor, finishEvent, upsertSubscription } from './store' +import { splitSecrets, verifyStripeSignature } from './webhook-signature' + +const MAX_BODY_BYTES = 512 * 1024 + +type StripeEvent = { + id: string + type: string + livemode: boolean + data: { object: Record } +} + +const SUBSCRIPTION_EVENTS = new Set([ + 'customer.subscription.created', + 'customer.subscription.updated', + 'customer.subscription.deleted', + 'customer.subscription.paused', + 'customer.subscription.resumed' +]) +const INVOICE_EVENTS = new Set(['invoice.paid', 'invoice.payment_failed']) + +function idOf(value: unknown): string | null { + if (typeof value === 'string' && value) { + return value + } + if (value && typeof value === 'object' && typeof (value as { id?: unknown }).id === 'string') { + return (value as { id: string }).id + } + return null +} + +/** The subscription an event concerns, across the payload shapes of old and new API versions. */ +export function subscriptionIdFor(event: StripeEvent): string | null { + const object = event.data.object + if (SUBSCRIPTION_EVENTS.has(event.type)) { + return idOf(object.id) + } + if (event.type === 'checkout.session.completed') { + return object.mode === 'subscription' ? idOf(object.subscription) : null + } + if (INVOICE_EVENTS.has(event.type)) { + const parent = object.parent as { subscription_details?: { subscription?: unknown } } | undefined + return idOf(object.subscription) ?? idOf(parent?.subscription_details?.subscription) + } + return null +} + +class IgnoredSubscription extends Error {} + +/** Stripe statuses after a failed renewal: past_due while retrying, unpaid once retries are exhausted. */ +export const PAYMENT_FAILED_STATUSES = new Set(['past_due', 'unpaid']) + +export async function syncSubscription(env: Env, stripe: StripeClient, subscriptionId: string, now: number) { + const subscription: StripeSubscription = await stripe.retrieveSubscription(subscriptionId) + if (subscription.livemode && !allowsLiveMode(env)) { + throw new IgnoredSubscription('livemode_subscription') + } + const customer = idOf(subscription.customer) + if (!customer) { + throw new IgnoredSubscription('subscription_without_customer') + } + const db = env.BILLING_DB + // Metadata is ours (customers cannot edit it), but the customer link is the authority: an + // account is only credited with a subscription billed to the customer we created for it. + const claimed = subscription.metadata?.account_id + const accountId = claimed && (await customerFor(db, claimed)) === customer ? claimed : await accountForCustomer(db, customer) + if (!accountId) { + throw new IgnoredSubscription('unknown_customer') + } + let match: ReturnType = null + let seats = 1 + let overageItemId: string | null = null + let periodStart = subscription.current_period_start + let periodEnd = subscription.current_period_end + for (const item of subscription.items.data) { + if (item.price.id === env.STRIPE_PRICE_OVERAGE) { + overageItemId = item.id + continue + } + const itemPlan = planForPrice(env, item.price.id) + if (itemPlan) { + match = itemPlan + seats = Math.max(1, item.quantity ?? 1) + periodStart = item.current_period_start ?? periodStart + periodEnd = item.current_period_end ?? periodEnd + } + } + if (!match || !periodStart || !periodEnd) { + throw new IgnoredSubscription('unrecognized_subscription_items') + } + await upsertSubscription(db, { + stripe_subscription_id: subscription.id, + account_id: accountId, + stripe_customer_id: customer, + status: subscription.status, + plan: match.plan, + seats, + overage_enabled: overageItemId ? 1 : 0, + current_period_start: periodStart * 1000, + current_period_end: periodEnd * 1000, + cancel_at_period_end: subscription.cancel_at_period_end ? 1 : 0, + updated_at: now, + billing_interval: match.interval, + overage_item_id: overageItemId, + // The upsert keeps the earliest value while it stays unpaid and clears it once paid. + past_due_since: PAYMENT_FAILED_STATUSES.has(subscription.status) ? now : null + }) +} + +export async function handleStripeWebhook(env: Env, stripe: StripeClient, request: Request, now: number): Promise { + const length = Number(request.headers.get('content-length') ?? '0') + if (length > MAX_BODY_BYTES) { + return new Response(null, { status: 413 }) + } + const rawBody = await request.text() + if (rawBody.length > MAX_BODY_BYTES) { + return new Response(null, { status: 413 }) + } + const verified = await verifyStripeSignature( + rawBody, + request.headers.get('stripe-signature'), + splitSecrets(env.STRIPE_WEBHOOK_SECRET), + now + ) + if (!verified.ok) { + return Response.json({ error: `signature_${verified.reason}` }, { status: 400 }) + } + let event: StripeEvent + try { + event = JSON.parse(rawBody) as StripeEvent + } catch { + return Response.json({ error: 'invalid_json' }, { status: 400 }) + } + if (typeof event.id !== 'string' || typeof event.type !== 'string' || !event.data?.object) { + return Response.json({ error: 'invalid_event' }, { status: 400 }) + } + // Acknowledged (so Stripe stops retrying) but never applied. + if (event.livemode && !allowsLiveMode(env)) { + return Response.json({ ignored: 'livemode' }) + } + const subscriptionId = subscriptionIdFor(event) + if (!subscriptionId) { + return Response.json({ ignored: 'event_type' }) + } + if ((await claimEvent(env.BILLING_DB, event.id, event.type, now)) === 'duplicate') { + return Response.json({ duplicate: true }) + } + try { + await syncSubscription(env, stripe, subscriptionId, now) + await finishEvent(env.BILLING_DB, event.id, { now }) + return Response.json({ received: true }) + } catch (error) { + if (error instanceof IgnoredSubscription) { + // Not ours or not recognizable: retrying will not help. Recorded for the audit script. + await finishEvent(env.BILLING_DB, event.id, { now, note: error.message }) + return Response.json({ ignored: error.message }) + } + await finishEvent(env.BILLING_DB, event.id, { error: error instanceof Error ? error.message : 'unknown' }) + // Non-2xx: Stripe retries with backoff for up to three days. + return Response.json({ error: 'processing_failed' }, { status: 500 }) + } +} diff --git a/cloud/apps/billing/test/auth-dev-keys.test.ts b/cloud/apps/billing/test/auth-dev-keys.test.ts new file mode 100644 index 00000000000..6bdbe1d3187 --- /dev/null +++ b/cloud/apps/billing/test/auth-dev-keys.test.ts @@ -0,0 +1,46 @@ +import { SignJWT, exportJWK, generateKeyPair } from 'jose' +import { describe, expect, it } from 'vitest' +import { DEV_ISSUER } from '../src/auth' +import { createHarness } from './harness' + +async function devKeyPair() { + const { privateKey, publicKey } = await generateKeyPair('ES256') + const jwks = JSON.stringify({ keys: [{ ...(await exportJWK(publicKey)), kid: 'dev', alg: 'ES256' }] }) + const sign = (issuer: string) => { + const now = Math.floor(Date.now() / 1000) + return new SignJWT({ sub: 'u1', purpose: 'billing' }) + .setProtectedHeader({ alg: 'ES256', kid: 'dev' }) + .setIssuer(issuer) + .setAudience('orca-billing') + .setIssuedAt(now) + .setExpirationTime(now + 300) + .sign(privateKey) + } + return { jwks, sign } +} + +describe('dev operator keys', () => { + it('accepts dev-issuer tokens outside prod only, and only with the dev issuer', async () => { + const dev = await devKeyPair() + const call = async (env: Record, token: string) => { + const h = await createHarness(env) + return (await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${token}` } })).status + } + expect(await call({ BILLING_ENV: 'dev', BILLING_DEV_JWKS: dev.jwks }, await dev.sign(DEV_ISSUER))).toBe(200) + expect(await call({ BILLING_ENV: 'prod', BILLING_DEV_JWKS: dev.jwks }, await dev.sign(DEV_ISSUER))).toBe(401) + // A dev key cannot impersonate the real issuer. + expect(await call({ BILLING_ENV: 'dev', BILLING_DEV_JWKS: dev.jwks }, await dev.sign('https://login.onorca.dev'))).toBe(401) + expect(await call({ BILLING_ENV: 'dev' }, await dev.sign(DEV_ISSUER))).toBe(401) + }) + + it('serves a static return page for Checkout and the Portal', async () => { + const h = await createHarness() + const { handleRequest } = await import('../src/index') + const response = await handleRequest(new Request('https://billing.test/return?checkout=success'), h.env, { + waitUntil() {}, + passThroughOnException() {} + } as unknown as ExecutionContext) + expect(response.status).toBe(200) + expect(await response.text()).toContain('return to Orca') + }) +}) diff --git a/cloud/apps/billing/test/billing-config.test.ts b/cloud/apps/billing/test/billing-config.test.ts new file mode 100644 index 00000000000..e8ac62c56df --- /dev/null +++ b/cloud/apps/billing/test/billing-config.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, it } from 'vitest' +import { allowanceWindow, capSeconds, DEFAULT_PLANS } from '../src/plans' +import { createHarness } from './harness' + +const HOUR = 3600 + +describe('config: tax and plan catalog', () => { + it('leaves Stripe Tax off by default and turns it on with STRIPE_AUTOMATIC_TAX=true', async () => { + for (const [flag, expected] of [ + [undefined, null], + ['true', 'true'] + ] as const) { + const h = await createHarness(flag ? { STRIPE_AUTOMATIC_TAX: flag } : {}) + const token = await h.desktopToken({ sub: 'u1' }) + await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: { authorization: `Bearer ${token}` } }) + const session = h.stripe.calls.find((call) => call.path === '/v1/checkout/sessions')! + expect(session.form.get('automatic_tax[enabled]')).toBe(expected) + expect(session.form.get('tax_id_collection[enabled]')).toBe(expected) + // Stripe Tax needs the customer's address; Checkout saves it back to the customer. + expect(session.form.get('customer_update[address]')).toBe('auto') + } + }) + + it('takes Team numbers and labels and the Enterprise contact from BILLING_PLANS', async () => { + const h = await createHarness({ + BILLING_PLANS: JSON.stringify({ + team: { includedHoursPerSeat: 40, monthlyPriceLabel: '$45/seat/mo', defaultSpendCapCents: 5000 }, + enterprise: { contactUrl: 'https://example.test/sales' } + }) + }) + const token = await h.desktopToken({ sub: 'u1' }) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${token}` } }) + expect(entitlement.body.offers).toEqual({ + team: { + label: 'Team', + monthlyPriceLabel: '$45/seat/mo', + annualPriceLabel: '$480/seat/yr', + monthlyPriceCentsPerSeat: 5000, + annualPriceCentsPerSeat: 48000, + largeMachineWeight: 2, + includedHoursPerSeat: 40, + concurrentSandboxesPerSeat: 3, + idleSleepMinutes: 15, + overageCentsPerHour: 75, + defaultSpendCapCents: 5000 + }, + enterprise: { label: 'Enterprise', priceLabel: 'Custom', contactUrl: 'https://example.test/sales' } + }) + expect(entitlement.body.overage.spendCapCents).toBe(5000) + }) + + it('fails closed on a malformed catalog', async () => { + for (const bad of [ + '{"team":{"includedHoursPerSeat":"lots"}}', + '{"pro":{}}', + '{"enterprise":{"contactUrl":"http://insecure.test"}}', + 'not json' + ]) { + const h = await createHarness({ BILLING_PLANS: bad }) + const token = await h.desktopToken({ sub: 'u1' }) + expect(await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${token}` } })).toEqual({ + status: 503, + body: { error: 'billing_misconfigured' } + }) + } + }) +}) + +describe('allowance windows and caps', () => { + const day = 24 * HOUR * 1000 + it('uses the billing period for monthly plans', () => { + const period = { start: Date.UTC(2026, 0, 15), end: Date.UTC(2026, 1, 15) } + expect(allowanceWindow(period, Date.UTC(2026, 1, 1))).toEqual(period) + }) + + it('slices annual plans into monthly windows anchored on the start, clamping short months', () => { + const period = { start: Date.UTC(2026, 0, 31), end: Date.UTC(2027, 0, 31) } + expect(allowanceWindow(period, Date.UTC(2026, 1, 10))).toEqual({ start: Date.UTC(2026, 0, 31), end: Date.UTC(2026, 1, 28) }) + expect(allowanceWindow(period, Date.UTC(2026, 2, 1))).toEqual({ start: Date.UTC(2026, 1, 28), end: Date.UTC(2026, 2, 31) }) + expect(allowanceWindow(period, period.end - day)).toEqual({ start: Date.UTC(2026, 11, 31), end: period.end }) + }) + + it('converts the $25 default cap to 33.3 overage hours at $0.75', () => { + expect(capSeconds(DEFAULT_PLANS.team, 2500)).toBe(Math.floor((2500 / 75) * HOUR)) + }) +}) diff --git a/cloud/apps/billing/test/billing-flow.test.ts b/cloud/apps/billing/test/billing-flow.test.ts new file mode 100644 index 00000000000..9eb20fde985 --- /dev/null +++ b/cloud/apps/billing/test/billing-flow.test.ts @@ -0,0 +1,356 @@ +import { describe, expect, it } from 'vitest' +import { checkoutCompleted, invoiceEvent, stripeEvent } from './stripe-fake' +import { createHarness } from './harness' + +const HOUR = 3600 +const PERIOD_START = Math.floor(Date.now() / 1000) - 5 * 24 * HOUR +const PERIOD_END = PERIOD_START + 30 * 24 * HOUR + +/** A 2-seat monthly Team org (120 included hours, 6 concurrent), optionally opted in to overage. */ +async function subscribedTeam(options: { seats?: number; overage?: boolean; spendCapCents?: number } = {}) { + const h = await createHarness() + const token = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner', email: 'a@example.com' }) + const auth = { authorization: `Bearer ${token}` } + const seats = options.seats ?? 2 + const checkout = await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team', seats }, headers: auth }) + expect(checkout.status).toBe(200) + const customer = [...h.stripe.customers.keys()][0]! + const subscription = h.stripe.putSubscription({ + customer, + accountId: 'org:o1', + basePrice: 'price_team_month', + quantity: seats, + periodStart: PERIOD_START, + periodEnd: PERIOD_END + }) + expect((await h.webhook(checkoutCompleted({ customer, subscription: subscription.id, accountId: 'org:o1' }))).body).toEqual({ + received: true + }) + if (options.overage) { + const body = options.spendCapCents === undefined ? { enabled: true } : { enabled: true, spendCapCents: options.spendCapCents } + expect((await h.call('POST', '/v1/billing/overage', { body, headers: auth })).status).toBe(200) + } + return { h, token, auth, customer, subscription } +} + +function usage(id: string, hours: number, extra: Record = {}) { + const endedAt = Date.now() - 60_000 + return { usageId: id, accountId: 'org:o1', sandboxId: 'sbx_1', machineSize: 'standard', startedAt: endedAt - hours * HOUR * 1000, endedAt, ...extra } +} + +describe('checkout and portal', () => { + it('creates one customer per account and a seat-quantity Checkout without overage', async () => { + const h = await createHarness() + const token = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'admin', email: 'a@example.com' }) + const auth = { authorization: `Bearer ${token}` } + const first = await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team', seats: 4, interval: 'year' }, headers: auth }) + expect(first.status).toBe(200) + expect(first.body.url).toMatch(/^https:\/\/checkout\.stripe\.com\//) + await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team', seats: 4 }, headers: auth }) + expect(h.stripe.customers.size).toBe(1) + + const [annual, monthly] = h.stripe.calls.filter((call) => call.path === '/v1/checkout/sessions') + expect(annual!.form.get('mode')).toBe('subscription') + expect(annual!.form.get('line_items[0][price]')).toBe('price_team_year') + expect(monthly!.form.get('line_items[0][price]')).toBe('price_team_month') + expect(annual!.form.get('line_items[0][quantity]')).toBe('4') + expect(annual!.form.has('line_items[0][adjustable_quantity][enabled]')).toBe(false) + expect(annual!.form.has('line_items[1][price]')).toBe(false) + expect(annual!.form.get('subscription_data[metadata][account_id]')).toBe('org:o1') + expect(annual!.form.has('subscription_data[trial_period_days]')).toBe(false) + expect(annual!.form.get('payment_method_collection')).toBe('always') + expect(annual!.form.get('success_url')).toContain('session_id={CHECKOUT_SESSION_ID}') + expect(annual!.headers.get('idempotency-key')).toMatch(/^checkout:org:o1:/) + }) + + it('lets only org owners and admins buy or manage the plan; personal accounts manage themselves', async () => { + const h = await createHarness() + const member = await h.desktopToken({ sub: 'u2', org: 'o1', orgRole: 'member' }) + const auth = { authorization: `Bearer ${member}` } + expect(await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: auth })).toEqual({ + status: 403, + body: { error: 'billing_admin_required' } + }) + expect((await h.call('POST', '/v1/billing/portal', { headers: auth })).status).toBe(403) + expect((await h.call('POST', '/v1/billing/overage', { body: { enabled: true }, headers: auth })).status).toBe(403) + + const solo = await h.desktopToken({ sub: 'u3' }) + expect((await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: { authorization: `Bearer ${solo}` } })).status).toBe(200) + expect(h.stripe.customers.values().next().value?.metadata.account_id).toBe('user:u3') + }) + + it('rejects bad tokens, unknown plans and a second subscription', async () => { + const { h, auth } = await subscribedTeam() + const wrongAudience = await h.desktopToken({ sub: 'u1' }, { audience: 'orca-relay' }) + const wrongPurpose = await h.desktopToken({ sub: 'u1' }, { purpose: 'host-control' }) + const tooOld = await h.desktopToken({ sub: 'u1' }, { ageSeconds: 20 * 60 }) + for (const bad of [wrongAudience, wrongPurpose, tooOld, 'garbage']) { + expect((await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${bad}` } })).status).toBe(401) + } + expect((await h.call('GET', '/v1/billing/entitlements')).status).toBe(401) + for (const body of [{ plan: 'pro' }, { plan: 'enterprise' }, { plan: 'team', interval: 'week' }, { plan: 'team', seats: 0 }]) { + expect((await h.call('POST', '/v1/billing/checkout', { body, headers: auth })).status).toBe(400) + } + expect(await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: auth })).toEqual({ + status: 409, + body: { error: 'already_subscribed' } + }) + }) + + it('opens the portal (with the configured portal configuration) for the linked customer only', async () => { + const { h, auth, customer } = await subscribedTeam() + h.env.STRIPE_PORTAL_CONFIGURATION = 'bpc_test_1' + const portal = await h.call('POST', '/v1/billing/portal', { headers: auth }) + expect(portal.body.url).toMatch(/^https:\/\/billing\.stripe\.com\//) + const call = h.stripe.calls.find((entry) => entry.path === '/v1/billing_portal/sessions')! + expect(call.form.get('customer')).toBe(customer) + expect(call.form.get('configuration')).toBe('bpc_test_1') + const stranger = await h.desktopToken({ sub: 'u9' }) + expect(await h.call('POST', '/v1/billing/portal', { headers: { authorization: `Bearer ${stranger}` } })).toEqual({ + status: 404, + body: { error: 'no_billing_account' } + }) + }) +}) + +describe('webhooks', () => { + it('rejects unsigned or mis-signed deliveries without touching state', async () => { + const { h, subscription, customer } = await subscribedTeam() + const event = invoiceEvent('invoice.paid', subscription.id, customer) + expect((await h.call('POST', '/v1/stripe/webhook', { rawBody: JSON.stringify(event) })).status).toBe(400) + expect((await h.webhook(event, 'whsec_wrong')).body).toEqual({ error: 'signature_mismatch' }) + expect(h.raw.prepare('SELECT COUNT(*) AS n FROM stripe_events WHERE event_id = ?').get(event.id)).toEqual({ n: 0 }) + }) + + it('processes an event id once', async () => { + const { h, subscription, customer } = await subscribedTeam() + const event = invoiceEvent('invoice.paid', subscription.id, customer) + expect((await h.webhook(event)).body).toEqual({ received: true }) + const reads = h.stripe.calls.filter((call) => call.method === 'GET').length + expect((await h.webhook(event)).body).toEqual({ duplicate: true }) + expect(h.stripe.calls.filter((call) => call.method === 'GET').length).toBe(reads) + }) + + it('applies Stripe current state, so out-of-order deliveries cannot resurrect a canceled plan', async () => { + const { h, auth, subscription } = await subscribedTeam() + const staleUpdate = stripeEvent('customer.subscription.updated', { ...subscription, status: 'active' }) + subscription.status = 'canceled' + await h.webhook(stripeEvent('customer.subscription.deleted', { ...subscription })) + await h.webhook(staleUpdate) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body).toMatchObject({ status: 'canceled', canStartSandbox: false, reason: 'subscription_inactive' }) + }) + + it('tracks seat and interval changes made in the portal', async () => { + const { h, auth, subscription } = await subscribedTeam() + subscription.items.data[0]!.quantity = 5 + subscription.items.data[0]!.price = { id: 'price_team_year' } + subscription.items.data[0]!.current_period_end = PERIOD_START + 365 * 24 * HOUR + await h.webhook(stripeEvent('customer.subscription.updated', subscription)) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body).toMatchObject({ seats: 5, interval: 'year', includedSeconds: 300 * HOUR }) + expect(entitlement.body.limits).toMatchObject({ maxConcurrentSandboxes: 15, idleSleepMinutes: 15 }) + // Annual plans still get a monthly allowance window. + const window = entitlement.body.usageWindow + expect(window.end - window.start).toBeLessThanOrEqual(31 * 24 * HOUR * 1000) + }) + + it('marks past_due from invoice.payment_failed (new invoice.parent shape)', async () => { + const { h, auth, subscription, customer } = await subscribedTeam() + subscription.status = 'past_due' + expect((await h.webhook(invoiceEvent('invoice.payment_failed', subscription.id, customer))).body).toEqual({ received: true }) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body).toMatchObject({ status: 'past_due', canStartSandbox: false, reason: 'payment_past_due' }) + }) + + it('acknowledges but ignores live-mode events and subscriptions on customers it did not create', async () => { + const { h, subscription } = await subscribedTeam() + expect((await h.webhook(stripeEvent('customer.subscription.updated', subscription, { livemode: true }))).body).toEqual({ + ignored: 'livemode' + }) + const foreign = h.stripe.putSubscription({ + customer: 'cus_someone_else', + accountId: 'org:o1', + basePrice: 'price_team_year', + quantity: 50, + periodStart: PERIOD_START, + periodEnd: PERIOD_END + }) + // Metadata naming org:o1 is not enough: the customer must be the one billing created for it. + expect((await h.webhook(stripeEvent('customer.subscription.created', foreign))).body).toEqual({ ignored: 'unknown_customer' }) + expect(h.raw.prepare('SELECT seats FROM subscriptions WHERE account_id = ?').all('org:o1')).toEqual([{ seats: 2 }]) + }) + + it('returns 500 (so Stripe retries) when Stripe cannot be read, and succeeds on redelivery', async () => { + const { h, subscription, customer } = await subscribedTeam() + const event = invoiceEvent('invoice.paid', 'sub_missing', customer) + expect((await h.webhook(event)).status).toBe(500) + h.stripe.subscriptions.set('sub_missing', { ...subscription, id: 'sub_missing' }) + expect((await h.webhook(event)).body).toEqual({ received: true }) + }) + + it('ignores event types it does not handle', async () => { + const h = await createHarness() + expect((await h.webhook(stripeEvent('customer.created', { id: 'cus_1' }))).body).toEqual({ ignored: 'event_type' }) + }) +}) + +describe('entitlements, overage and usage', () => { + it('reports pooled hours, warnings and limits for the desktop', async () => { + const { h, auth } = await subscribedTeam() + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 10))).status).toBe(200) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body).toMatchObject({ + accountId: 'org:o1', + plan: 'team', + planLabel: 'Team', + status: 'active', + interval: 'month', + seats: 2, + includedSeconds: 120 * HOUR, + usedSeconds: 10 * HOUR, + remainingIncludedSeconds: 110 * HOUR, + usageWarningPercent: null, + overage: { optedIn: false, active: false, spendCapCents: 2500, spentCents: 0, centsPerHour: 75 }, + limits: { maxConcurrentSandboxes: 6, machineSizes: ['standard', 'large'], idleSleepMinutes: 15 }, + canStartSandbox: true, + canManageBilling: true, + period: { start: PERIOD_START * 1000, end: PERIOD_END * 1000 } + }) + expect(entitlement.body.offers.team).toMatchObject({ monthlyPriceLabel: '$50/seat/mo', annualPriceLabel: '$480/seat/yr' }) + expect(entitlement.body.offers.enterprise).toMatchObject({ label: 'Enterprise', priceLabel: 'Custom' }) + + for (let i = 0; i < 4; i++) { + await h.controlPlane('/v1/internal/usage', usage(`w-${i}`, 22)) + } + expect((await h.call('GET', '/v1/billing/entitlements', { headers: auth })).body.usageWarningPercent).toBe(80) + }) + + it('is idempotent per usageId and burns Large at 2x', async () => { + const { h } = await subscribedTeam() + const first = await h.controlPlane('/v1/internal/usage', usage('u-1', 1, { machineSize: 'large' })) + const again = await h.controlPlane('/v1/internal/usage', usage('u-1', 1, { machineSize: 'large' })) + expect(first.body).toMatchObject({ recorded: true, weightedSeconds: 2 * HOUR }) + expect(again.body).toMatchObject({ recorded: false }) + expect(h.raw.prepare('SELECT SUM(weighted_seconds) AS s FROM usage_events').get()).toEqual({ s: 2 * HOUR }) + }) + + it('without opt-in, stops new starts at the included hours and meters nothing', async () => { + const { h, auth } = await subscribedTeam() + for (let i = 0; i < 5; i++) { + await h.controlPlane('/v1/internal/usage', usage(`u-${i}`, 24)) + } + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body).toMatchObject({ usageWarningPercent: 100, canStartSandbox: false, reason: 'included_hours_exhausted' }) + expect(h.stripe.meterEvents).toHaveLength(0) + expect(h.stripe.calls.some((call) => call.path === '/v1/subscription_items')).toBe(false) + }) + + it('opting in adds the metered item once, then meters only the overage past included hours', async () => { + const { h, auth, customer, subscription } = await subscribedTeam({ overage: true }) + expect(subscription.items.data.map((item) => item.price.id)).toEqual(['price_team_month', 'price_overage']) + await h.call('POST', '/v1/billing/overage', { body: { enabled: true }, headers: auth }) + expect(h.stripe.calls.filter((call) => call.path === '/v1/subscription_items')).toHaveLength(1) + // The item's webhook must not drop the item id. + await h.webhook(stripeEvent('customer.subscription.updated', subscription)) + + for (let i = 0; i < 5; i++) { + await h.controlPlane('/v1/internal/usage', usage(`u-${i}`, 24)) + } + // Exactly the 120 included hours: nothing billable yet. + expect(h.stripe.meterEvents).toHaveLength(0) + const crossing = await h.controlPlane('/v1/internal/usage', usage('u-5', 4)) + expect(crossing.body).toMatchObject({ overageSeconds: 4 * HOUR, canStartSandbox: true }) + await h.controlPlane('/v1/internal/usage', usage('u-5', 4)) + expect(h.stripe.meterEvents).toEqual([ + expect.objectContaining({ identifier: 'u-5', 'payload[stripe_customer_id]': customer, 'payload[value]': String(4 * HOUR) }) + ]) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: auth }) + expect(entitlement.body.overage).toMatchObject({ optedIn: true, active: true, spentCents: 300 }) + }) + + it('stops metering and new starts at the spend cap; admins can raise it', async () => { + // $3 cap = 4 overage hours at $0.75. + const { h, auth } = await subscribedTeam({ overage: true, spendCapCents: 300 }) + for (let i = 0; i < 5; i++) { + await h.controlPlane('/v1/internal/usage', usage(`u-${i}`, 24)) + } + await h.controlPlane('/v1/internal/usage', usage('o-1', 6)) + expect(h.stripe.meterEvents.map((event) => event['payload[value]'])).toEqual([String(4 * HOUR)]) + const check = () => h.controlPlane('/v1/internal/entitlements/check', { accountId: 'org:o1', machineSize: 'standard', runningSandboxes: 0 }) + expect((await check()).body).toMatchObject({ allow: false, reason: 'overage_cap_reached' }) + + expect((await h.call('POST', '/v1/billing/overage', { body: { enabled: true, spendCapCents: 10_000_000 }, headers: auth })).body).toEqual({ + error: 'spend_cap_too_high' + }) + await h.call('POST', '/v1/billing/overage', { body: { enabled: true, spendCapCents: 2500 }, headers: auth }) + expect((await check()).body).toMatchObject({ allow: true }) + }) + + it('opting out stops metering immediately and blocks starts past included hours', async () => { + const { h, auth } = await subscribedTeam({ overage: true }) + await h.call('POST', '/v1/billing/overage', { body: { enabled: false }, headers: auth }) + for (let i = 0; i < 6; i++) { + await h.controlPlane('/v1/internal/usage', usage(`u-${i}`, 24)) + } + expect(h.stripe.meterEvents).toHaveLength(0) + expect((await h.call('GET', '/v1/billing/entitlements', { headers: auth })).body).toMatchObject({ reason: 'included_hours_exhausted' }) + }) + + it('retries failed meter reports from the outbox', async () => { + const { h } = await subscribedTeam({ overage: true }) + for (let i = 0; i < 5; i++) { + await h.controlPlane('/v1/internal/usage', usage(`u-${i}`, 24)) + } + h.stripe.failNextMeterEvents(1) + await h.controlPlane('/v1/internal/usage', usage('u-9', 2)) + expect(h.stripe.meterEvents).toHaveLength(0) + expect(h.raw.prepare('SELECT status, attempts FROM meter_reports').get()).toEqual({ status: 'pending', attempts: 1 }) + const { flushMeterReports } = await import('../src/usage') + const { createStripeClient } = await import('../src/stripe') + expect(await flushMeterReports(h.env, createStripeClient(h.env, h.stripe.fetchImpl), Date.now())).toEqual({ sent: 1, failed: 0 }) + expect(h.stripe.meterEvents[0]?.['payload[value]']).toBe(String(2 * HOUR)) + }) + + it('gates starts by machine size and per-seat concurrency for the control plane', async () => { + const { h } = await subscribedTeam() + const check = (body: Record) => h.controlPlane('/v1/internal/entitlements/check', { accountId: 'org:o1', ...body }) + expect((await check({ machineSize: 'large', runningSandboxes: 5 })).body).toMatchObject({ allow: true, reason: null }) + expect((await check({ machineSize: 'standard', runningSandboxes: 6 })).body).toMatchObject({ allow: false, reason: 'concurrency_limit' }) + expect((await check({ machineSize: 'xlarge', runningSandboxes: 0 })).status).toBe(400) + const unknown = await h.controlPlane('/v1/internal/entitlements/check', { accountId: 'org:nobody', machineSize: 'standard', runningSandboxes: 0 }) + expect(unknown.body).toMatchObject({ allow: false, reason: 'no_subscription' }) + }) + + it('refuses overage opt-in without an open subscription', async () => { + const h = await createHarness() + const token = await h.desktopToken({ sub: 'u1' }) + expect(await h.call('POST', '/v1/billing/overage', { body: { enabled: true }, headers: { authorization: `Bearer ${token}` } })).toEqual({ + status: 409, + body: { error: 'no_subscription' } + }) + }) + + it('authenticates the control plane and validates segments', async () => { + const { h } = await subscribedTeam() + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 1), 'cp_test_wrong_but_long_enough_000000000')).status).toBe(401) + const desktop = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 1), desktop)).status).toBe(401) + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 25))).body).toEqual({ error: 'segment_too_long' }) + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 0))).body).toEqual({ error: 'empty_segment' }) + const future = Date.now() + 3600_000 + expect((await h.controlPlane('/v1/internal/usage', usage('u-1', 1, { startedAt: future - 1000, endedAt: future }))).body).toEqual({ + error: 'segment_in_future' + }) + expect((await h.controlPlane('/v1/internal/usage', { ...usage('u-1', 1), accountId: 'org:o1; DROP' })).status).toBe(400) + }) + + it('refuses to run with a live Stripe key', async () => { + const h = await createHarness({ STRIPE_SECRET_KEY: 'sk_live_nope' }) + const token = await h.desktopToken({ sub: 'u1' }) + expect(await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: { authorization: `Bearer ${token}` } })).toEqual({ + status: 503, + body: { error: 'billing_misconfigured' } + }) + }) +}) diff --git a/cloud/apps/billing/test/d1-fake.ts b/cloud/apps/billing/test/d1-fake.ts new file mode 100644 index 00000000000..906896cc54f --- /dev/null +++ b/cloud/apps/billing/test/d1-fake.ts @@ -0,0 +1,64 @@ +// A D1Database over node:sqlite for tests: same SQL dialect (SQLite), same prepare/bind/first/ +// all/run/batch surface, batch() in one transaction like D1. +import { readFileSync, readdirSync } from 'node:fs' +import { DatabaseSync } from 'node:sqlite' +import { fileURLToPath } from 'node:url' + +type Param = string | number | null + +class FakeStatement { + constructor( + private readonly db: DatabaseSync, + readonly sql: string, + readonly params: Param[] = [] + ) {} + + bind(...params: unknown[]): FakeStatement { + for (const param of params) { + if (param === undefined || typeof param === 'boolean' || typeof param === 'object' && param !== null) { + throw new Error(`D1 cannot bind ${typeof param}`) + } + } + return new FakeStatement(this.db, this.sql, params as Param[]) + } + + async first(): Promise { + return (this.db.prepare(this.sql).get(...this.params) as T | undefined) ?? null + } + + async all(): Promise<{ results: T[]; success: true; meta: { changes: number } }> { + return { results: this.db.prepare(this.sql).all(...this.params) as T[], success: true, meta: { changes: 0 } } + } + + runSync(): { success: true; meta: { changes: number } } { + const result = this.db.prepare(this.sql).run(...this.params) + return { success: true, meta: { changes: Number(result.changes) } } + } + + async run() { + return this.runSync() + } +} + +export function createFakeD1(): { db: D1Database; raw: DatabaseSync } { + const raw = new DatabaseSync(':memory:') + const dir = fileURLToPath(new URL('../migrations/', import.meta.url).href) + for (const file of readdirSync(dir).filter((name) => name.endsWith('.sql')).sort()) { + raw.exec(readFileSync(`${dir}${file}`, 'utf8')) + } + const db = { + prepare: (sql: string) => new FakeStatement(raw, sql), + async batch(statements: FakeStatement[]) { + raw.exec('BEGIN') + try { + const results = statements.map((statement) => statement.runSync()) + raw.exec('COMMIT') + return results + } catch (error) { + raw.exec('ROLLBACK') + throw error + } + } + } + return { db: db as unknown as D1Database, raw } +} diff --git a/cloud/apps/billing/test/harness.ts b/cloud/apps/billing/test/harness.ts new file mode 100644 index 00000000000..1572e95117b --- /dev/null +++ b/cloud/apps/billing/test/harness.ts @@ -0,0 +1,85 @@ +import { SignJWT, exportJWK, generateKeyPair } from 'jose' +import type { Env } from '../src/env' +import { handleRequest } from '../src/index' +import { createStripeClient } from '../src/stripe' +import { createFakeD1 } from './d1-fake' +import { createFakeStripe, signStripePayload } from './stripe-fake' + +export const WEBHOOK_SECRET = 'whsec_test_local_fixture_secret' +export const CONTROL_TOKEN = 'cp_test_0123456789abcdef0123456789abcdef' + +export async function createHarness(overrides: Partial = {}) { + const { privateKey, publicKey } = await generateKeyPair('ES256') + const jwk = { ...(await exportJWK(publicKey)), kid: `k${Math.random()}`, alg: 'ES256', use: 'sig' } + const { db, raw } = createFakeD1() + const stripe = createFakeStripe() + const env: Env = { + BILLING_DB: db, + STRIPE_SECRET_KEY: 'rk_test_fixture', + STRIPE_WEBHOOK_SECRET: WEBHOOK_SECRET, + STRIPE_API_BASE: 'https://stripe.test', + STRIPE_PRICE_TEAM_MONTHLY: 'price_team_month', + STRIPE_PRICE_TEAM_ANNUAL: 'price_team_year', + STRIPE_PRICE_OVERAGE: 'price_overage', + STRIPE_METER_EVENT_NAME: 'orca_sandbox_seconds', + BILLING_RETURN_URL: 'https://onorca.dev/billing/return', + BILLING_JWKS: JSON.stringify({ keys: [jwk] }), + BILLING_JWT_AUDIENCE: 'orca-billing', + BILLING_AUTH_ISSUER: 'https://login.onorca.dev', + CONTROL_PLANE_TOKEN: CONTROL_TOKEN, + ...overrides + } + let now = Date.now() + // Live org member counts from auth; null = not configured (the default). + let membership: (orgId: string) => Promise = async () => null + const waits: Promise[] = [] + const ctx = { waitUntil: (promise: Promise) => waits.push(promise), passThroughOnException() {} } as unknown as ExecutionContext + + async function call(method: string, path: string, init: { body?: unknown; rawBody?: string; headers?: Record } = {}) { + const request = new Request(`https://billing.test${path}`, { + method, + headers: { 'content-type': 'application/json', ...init.headers }, + body: init.rawBody ?? (init.body === undefined ? undefined : JSON.stringify(init.body)) + }) + const response = await handleRequest(request, env, ctx, { + stripe: createStripeClient(env, stripe.fetchImpl), + now: () => now, + membership: (orgId) => membership(orgId) + }) + await Promise.all(waits.splice(0)) + return { status: response.status, body: (await response.json()) as Record } + } + + return { + env, + raw, + stripe, + call, + setNow(value: number) { + now = value + }, + setMembership(source: (orgId: string) => Promise) { + membership = source + }, + async desktopToken(claims: { sub: string; org?: string; orgRole?: string; email?: string; orgMembers?: number }, options: { audience?: string; purpose?: string; ageSeconds?: number } = {}) { + const iat = Math.floor(now / 1000) - (options.ageSeconds ?? 0) + return new SignJWT({ ...claims, purpose: options.purpose ?? 'billing' }) + .setProtectedHeader({ alg: 'ES256', kid: jwk.kid }) + .setIssuer('https://login.onorca.dev') + .setAudience(options.audience ?? 'orca-billing') + .setIssuedAt(iat) + .setExpirationTime(iat + 900) + .sign(privateKey) + }, + async webhook(event: unknown, secret = WEBHOOK_SECRET) { + const rawBody = JSON.stringify(event) + return call('POST', '/v1/stripe/webhook', { + rawBody, + headers: { 'stripe-signature': signStripePayload(rawBody, secret, Math.floor(now / 1000)) } + }) + }, + controlPlane(path: string, body: unknown, token = CONTROL_TOKEN) { + return call('POST', path, { body, headers: { authorization: `Bearer ${token}` } }) + } + } +} diff --git a/cloud/apps/billing/test/seats-grace.test.ts b/cloud/apps/billing/test/seats-grace.test.ts new file mode 100644 index 00000000000..91415bab932 --- /dev/null +++ b/cloud/apps/billing/test/seats-grace.test.ts @@ -0,0 +1,183 @@ +import { describe, expect, it } from 'vitest' +import { checkoutCompleted, invoiceEvent, stripeEvent } from './stripe-fake' +import { createHarness, CONTROL_TOKEN } from './harness' + +const HOUR = 3600 +const PERIOD_START = Math.floor(Date.now() / 1000) - 5 * 24 * HOUR +const PERIOD_END = PERIOD_START + 30 * 24 * HOUR +const MEMBERSHIP_TOKEN = 'ms_test_0123456789abcdef0123456789abcdef' + +async function team(seats = 2) { + const h = await createHarness({ MEMBERSHIP_SYNC_TOKEN: MEMBERSHIP_TOKEN }) + const owner = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team', seats }, headers: { authorization: `Bearer ${owner}` } }) + const customer = [...h.stripe.customers.keys()][0]! + const subscription = h.stripe.putSubscription({ + customer, + accountId: 'org:o1', + basePrice: 'price_team_month', + quantity: seats, + periodStart: PERIOD_START, + periodEnd: PERIOD_END + }) + await h.webhook(checkoutCompleted({ customer, subscription: subscription.id, accountId: 'org:o1' })) + return { h, customer, subscription } +} + +const seatUpdates = (h: Awaited>['h']) => + h.stripe.calls + .filter((call) => call.method === 'POST' && call.path.startsWith('/v1/subscription_items/')) + .map((call) => ({ quantity: call.form.get('quantity'), proration: call.form.get('proration_behavior') })) + +describe('seats follow membership', () => { + it('auth pushes member counts: adds invoice now, removals credit next invoice, min 1 seat', async () => { + const { h } = await team(2) + const push = (members: number, token = MEMBERSHIP_TOKEN) => + h.call('POST', '/v1/internal/seats', { body: { accountId: 'org:o1', members }, headers: { authorization: `Bearer ${token}` } }) + + expect((await push(3, CONTROL_TOKEN)).status).toBe(401) + expect((await push(3)).body).toEqual({ status: 'updated', seats: 3, previous: 2 }) + expect((await push(3)).body).toEqual({ status: 'unchanged', seats: 3 }) + expect((await push(1)).body).toEqual({ status: 'updated', seats: 1, previous: 3 }) + expect((await push(0)).body).toEqual({ status: 'unchanged', seats: 1 }) + expect(seatUpdates(h)).toEqual([ + { quantity: '3', proration: 'always_invoice' }, + { quantity: '1', proration: 'create_prorations' } + ]) + const owner = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${owner}` } }) + expect(entitlement.body).toMatchObject({ seats: 1, includedSeconds: 60 * HOUR, limits: { maxConcurrentSandboxes: 3 } }) + }) + + it('Checkout bills the live member count from auth, never fewer than 1 seat', async () => { + const h = await createHarness() + const owner = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + const seen: string[] = [] + h.setMembership(async (orgId) => { + seen.push(orgId) + return 0 + }) + await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team', seats: 9 }, headers: { authorization: `Bearer ${owner}` } }) + expect(seen).toEqual(['o1']) + expect(h.stripe.calls.find((call) => call.path === '/v1/checkout/sessions')?.form.get('line_items[0][quantity]')).toBe('1') + }) + + it('fails closed when auth is configured but cannot answer', async () => { + const h = await createHarness() + const owner = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + const { MembershipUnavailableError } = await import('../src/membership') + h.setMembership(async () => { + throw new MembershipUnavailableError('auth answered 500') + }) + expect(await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: { authorization: `Bearer ${owner}` } })).toEqual({ + status: 503, + body: { error: 'membership_unavailable' } + }) + expect(h.stripe.calls).toHaveLength(0) + }) + + it('the reconcile cron heals a missed membership push', async () => { + const { h } = await team(2) + const { reconcileSeats } = await import('../src/seats') + const { createStripeClient } = await import('../src/stripe') + const stripe = createStripeClient(h.env, h.stripe.fetchImpl) + expect(await reconcileSeats(h.env, stripe, async () => 5, Date.now())).toEqual({ checked: 1, updated: 1, failed: 0 }) + expect(await reconcileSeats(h.env, stripe, async () => 5, Date.now())).toEqual({ checked: 1, updated: 0, failed: 0 }) + expect(seatUpdates(h)).toEqual([{ quantity: '5', proration: 'always_invoice' }]) + }) + + it('billing admin actions need a freshly minted token (role re-read by auth at mint)', async () => { + const { h } = await team(2) + const stale = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }, { ageSeconds: 7 * 60 }) + const auth = { authorization: `Bearer ${stale}` } + expect((await h.call('GET', '/v1/billing/entitlements', { headers: auth })).status).toBe(200) + expect((await h.call('POST', '/v1/billing/portal', { headers: auth })).status).toBe(401) + expect((await h.call('POST', '/v1/billing/overage', { body: { enabled: true }, headers: auth })).status).toBe(401) + expect((await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: auth })).status).toBe(401) + }) + + it('rejects malformed or missing member counts instead of billing 0 seats', async () => { + const { h } = await team(2) + for (const body of [{ accountId: 'org:o1' }, { accountId: 'org:o1', members: -1 }, { accountId: 'org:o1', members: 'many' }, { accountId: 'user:u1', members: 3 }]) { + const response = await h.call('POST', '/v1/internal/seats', { body, headers: { authorization: `Bearer ${MEMBERSHIP_TOKEN}` } }) + expect(response.status).toBe(400) + } + expect(seatUpdates(h)).toEqual([]) + }) + + it('does nothing without an open subscription', async () => { + const h = await createHarness({ MEMBERSHIP_SYNC_TOKEN: MEMBERSHIP_TOKEN }) + const pushed = await h.call('POST', '/v1/internal/seats', { + body: { accountId: 'org:nobody', members: 5 }, + headers: { authorization: `Bearer ${MEMBERSHIP_TOKEN}` } + }) + expect(pushed.body).toEqual({ status: 'no_subscription' }) + expect(h.stripe.calls.filter((call) => call.method === 'POST')).toHaveLength(0) + }) +}) + +describe('payment failure grace', () => { + it('blocks new starts at once, suspends running after 4 days, resumes when paid', async () => { + const { h, customer, subscription } = await team(2) + const failedAt = Date.now() + h.setNow(failedAt) + subscription.status = 'past_due' + await h.webhook(invoiceEvent('invoice.payment_failed', subscription.id, customer)) + const check = () => + h.controlPlane('/v1/internal/entitlements/check', { accountId: 'org:o1', machineSize: 'standard', runningSandboxes: 0 }) + const suspensions = () => h.call('GET', '/v1/internal/suspensions', { headers: { authorization: `Bearer ${CONTROL_TOKEN}` } }) + + expect((await check()).body).toMatchObject({ allow: false, reason: 'payment_past_due', suspendRunning: false }) + expect((await check()).body.entitlement.payment).toEqual({ + pastDueSince: failedAt, + graceEndsAt: failedAt + 96 * HOUR * 1000, + suspendRunning: false + }) + expect((await suspensions()).body).toEqual({ accounts: [] }) + + // A retry failing again later must not restart the grace clock. + h.setNow(failedAt + 50 * HOUR * 1000) + await h.webhook(invoiceEvent('invoice.payment_failed', subscription.id, customer)) + h.setNow(failedAt + 97 * HOUR * 1000) + expect((await check()).body).toMatchObject({ allow: false, reason: 'payment_grace_expired', suspendRunning: true }) + expect((await suspensions()).body.accounts).toEqual([ + { accountId: 'org:o1', pastDueSince: failedAt, graceEndsAt: failedAt + 96 * HOUR * 1000 } + ]) + const usage = await h.controlPlane('/v1/internal/usage', { + usageId: 'u-1', + accountId: 'org:o1', + sandboxId: 's', + machineSize: 'standard', + startedAt: failedAt + 96 * HOUR * 1000 - 60_000, + endedAt: failedAt + 96 * HOUR * 1000 + }) + expect(usage.body).toMatchObject({ canStartSandbox: false, suspendRunning: true }) + + // Retries exhausted (unpaid) stays suspended; paying restores everything. + subscription.status = 'unpaid' + await h.webhook(stripeEvent('customer.subscription.updated', subscription)) + expect((await check()).body).toMatchObject({ reason: 'payment_grace_expired' }) + subscription.status = 'active' + await h.webhook(invoiceEvent('invoice.paid', subscription.id, customer)) + expect((await check()).body).toMatchObject({ allow: true, suspendRunning: false }) + expect((await suspensions()).body).toEqual({ accounts: [] }) + }) + + it('takes the grace length from config', async () => { + const h = await createHarness({ BILLING_PLANS: JSON.stringify({ team: { paymentGraceHours: 0 } }) }) + const owner = await h.desktopToken({ sub: 'u1', org: 'o1', orgRole: 'owner' }) + await h.call('POST', '/v1/billing/checkout', { body: { plan: 'team' }, headers: { authorization: `Bearer ${owner}` } }) + const customer = [...h.stripe.customers.keys()][0]! + const subscription = h.stripe.putSubscription({ + customer, + accountId: 'org:o1', + basePrice: 'price_team_month', + status: 'past_due', + periodStart: PERIOD_START, + periodEnd: PERIOD_END + }) + await h.webhook(stripeEvent('customer.subscription.updated', subscription)) + const entitlement = await h.call('GET', '/v1/billing/entitlements', { headers: { authorization: `Bearer ${owner}` } }) + expect(entitlement.body).toMatchObject({ reason: 'payment_grace_expired', payment: { suspendRunning: true } }) + }) +}) diff --git a/cloud/apps/billing/test/stripe-client.test.ts b/cloud/apps/billing/test/stripe-client.test.ts new file mode 100644 index 00000000000..29b13759bec --- /dev/null +++ b/cloud/apps/billing/test/stripe-client.test.ts @@ -0,0 +1,47 @@ +import { describe, expect, it } from 'vitest' +import type { Env } from '../src/env' +import { LiveModeRefusedError, assertKeyMode, createStripeClient, encodeForm } from '../src/stripe' + +const env = (key: string, extra: Partial = {}) => ({ STRIPE_SECRET_KEY: key, ...extra }) as Env + +describe('stripe client', () => { + it('encodes nested params with Stripe bracket syntax and drops undefined', () => { + expect( + decodeURIComponent( + encodeForm({ mode: 'subscription', line_items: [{ price: 'p', quantity: 2 }, { price: 'o' }], metadata: { a: 'b' }, x: undefined }) + ) + ).toBe('mode=subscription&line_items[0][price]=p&line_items[0][quantity]=2&line_items[1][price]=o&metadata[a]=b') + }) + + it('refuses live-mode keys unless live mode is explicitly enabled', () => { + expect(() => assertKeyMode(env('sk_live_x'))).toThrow(LiveModeRefusedError) + expect(() => assertKeyMode(env('rk_live_x'))).toThrow(LiveModeRefusedError) + expect(() => assertKeyMode(env('rk_live_x', { STRIPE_ALLOW_LIVEMODE: '1' }))).toThrow(LiveModeRefusedError) + expect(() => assertKeyMode(env('rk_live_x', { STRIPE_ALLOW_LIVEMODE: 'true' }))).not.toThrow() + expect(() => assertKeyMode(env('rk_test_x'))).not.toThrow() + expect(() => assertKeyMode(env(''))).toThrow('stripe_key_missing') + }) + + it('never sends a request with a live key', async () => { + let called = false + const client = createStripeClient(env('sk_live_x'), async () => { + called = true + return Response.json({}) + }) + await expect(client.retrieveSubscription('sub_1')).rejects.toThrow(LiveModeRefusedError) + expect(called).toBe(false) + }) + + it('pins the API version, sends an idempotency key and refuses redirects', async () => { + let seen: RequestInit | undefined + const client = createStripeClient(env('rk_test_x'), async (_url, init) => { + seen = init + return Response.json({ id: 'cus_1' }) + }) + await client.createCustomer({ accountId: 'org:o1' }) + const headers = new Headers(seen?.headers) + expect(headers.get('stripe-version')).toBe('2025-09-30.clover') + expect(headers.get('idempotency-key')).toBe('customer-create:org:o1') + expect(seen?.redirect).toBe('manual') + }) +}) diff --git a/cloud/apps/billing/test/stripe-fake.ts b/cloud/apps/billing/test/stripe-fake.ts new file mode 100644 index 00000000000..3a2568907a5 --- /dev/null +++ b/cloud/apps/billing/test/stripe-fake.ts @@ -0,0 +1,192 @@ +// In-memory Stripe API behind a fetch function, plus event fixtures shaped like Stripe's +// (API version 2025-09-30.clover: periods on subscription items, invoice.parent.subscription_details). +// The real client in src/stripe.ts talks to it, so form encoding and headers are exercised too. +import { createHmac } from 'node:crypto' + +export type FakeSubscription = { + id: string + object: 'subscription' + customer: string + status: string + cancel_at_period_end: boolean + livemode: boolean + metadata: Record + items: { object: 'list'; data: Array<{ id: string; quantity?: number; current_period_start: number; current_period_end: number; price: { id: string } }> } +} + +export type RecordedCall = { method: string; path: string; form: URLSearchParams; headers: Headers } + +export function createFakeStripe() { + const calls: RecordedCall[] = [] + const customers = new Map }>() + const subscriptions = new Map() + const idempotent = new Map() + const meterEvents: Array> = [] + let nextId = 1 + let failMeterEvents = 0 + + const fetchImpl: typeof fetch = async (input, init) => { + const url = new URL(typeof input === 'string' ? input : input instanceof URL ? input.href : input.url) + const headers = new Headers(init?.headers) + const form = new URLSearchParams(typeof init?.body === 'string' ? init.body : '') + const method = init?.method ?? 'GET' + calls.push({ method, path: url.pathname, form, headers }) + if (!headers.get('authorization')?.startsWith('Bearer rk_test_')) { + return Response.json({ error: { message: 'Invalid API Key provided' } }, { status: 401 }) + } + const key = headers.get('idempotency-key') + if (method === 'POST' && key && idempotent.has(key)) { + return Response.json(idempotent.get(key)) + } + const respond = (body: unknown) => { + if (method === 'POST' && key) { + idempotent.set(key, body) + } + return Response.json(body) + } + if (method === 'POST' && url.pathname === '/v1/customers') { + const id = `cus_test_${nextId++}` + const customer = { id, metadata: { account_id: form.get('metadata[account_id]') ?? '' } } + customers.set(id, customer) + return respond(customer) + } + if (method === 'POST' && url.pathname === '/v1/checkout/sessions') { + const id = `cs_test_${nextId++}` + return respond({ id, object: 'checkout.session', url: `https://checkout.stripe.com/c/pay/${id}` }) + } + if (method === 'POST' && url.pathname === '/v1/billing_portal/sessions') { + const id = `bps_test_${nextId++}` + return respond({ id, url: `https://billing.stripe.com/p/session/${id}` }) + } + if (method === 'GET' && url.pathname.startsWith('/v1/subscriptions/')) { + const subscription = subscriptions.get(decodeURIComponent(url.pathname.slice('/v1/subscriptions/'.length))) + return subscription + ? Response.json(subscription) + : Response.json({ error: { code: 'resource_missing', message: 'No such subscription' } }, { status: 404 }) + } + if (method === 'POST' && url.pathname.startsWith('/v1/subscription_items/')) { + const itemId = decodeURIComponent(url.pathname.slice('/v1/subscription_items/'.length)) + for (const subscription of subscriptions.values()) { + const item = subscription.items.data.find((entry) => entry.id === itemId) + if (item) { + item.quantity = Number(form.get('quantity')) + return respond({ id: item.id, quantity: item.quantity }) + } + } + return Response.json({ error: { code: 'resource_missing', message: 'No such item' } }, { status: 404 }) + } + if (method === 'POST' && url.pathname === '/v1/subscription_items') { + const subscription = subscriptions.get(form.get('subscription') ?? '') + if (!subscription) { + return Response.json({ error: { code: 'resource_missing', message: 'No such subscription' } }, { status: 404 }) + } + const base = subscription.items.data[0]! + const item = { + id: `si_test_${nextId++}`, + price: { id: form.get('price') ?? '' }, + current_period_start: base.current_period_start, + current_period_end: base.current_period_end + } + subscription.items.data.push(item) + return respond(item) + } + if (method === 'POST' && url.pathname === '/v1/billing/meter_events') { + if (failMeterEvents > 0) { + failMeterEvents-- + return Response.json({ error: { message: 'temporarily unavailable' } }, { status: 503 }) + } + meterEvents.push(Object.fromEntries(form)) + return respond({ object: 'billing.meter_event', identifier: form.get('identifier') }) + } + return Response.json({ error: { message: `unhandled ${method} ${url.pathname}` } }, { status: 404 }) + } + + return { + fetchImpl, + calls, + customers, + subscriptions, + meterEvents, + failNextMeterEvents(count: number) { + failMeterEvents = count + }, + /** What Stripe holds after Checkout completes for `customer` (the test plays the browser). */ + putSubscription(args: { + customer: string + accountId: string + basePrice: string + overagePrice?: string + quantity?: number + status?: string + periodStart: number + periodEnd: number + livemode?: boolean + }): FakeSubscription { + const id = `sub_test_${nextId++}` + const period = { current_period_start: args.periodStart, current_period_end: args.periodEnd } + const data: FakeSubscription['items']['data'] = [ + { id: `si_test_${nextId++}`, quantity: args.quantity ?? 1, price: { id: args.basePrice }, ...period } + ] + if (args.overagePrice) { + data.push({ id: `si_test_${nextId++}`, price: { id: args.overagePrice }, ...period }) + } + const subscription: FakeSubscription = { + id, + object: 'subscription', + customer: args.customer, + status: args.status ?? 'active', + cancel_at_period_end: false, + livemode: args.livemode ?? false, + metadata: { account_id: args.accountId }, + items: { object: 'list', data } + } + subscriptions.set(id, subscription) + return subscription + } + } +} + +let eventCounter = 1 + +export function stripeEvent(type: string, object: Record, options: { livemode?: boolean; id?: string } = {}) { + return { + id: options.id ?? `evt_test_${eventCounter++}`, + object: 'event', + api_version: '2025-09-30.clover', + created: Math.floor(Date.now() / 1000), + livemode: options.livemode ?? false, + pending_webhooks: 1, + request: { id: null, idempotency_key: null }, + type, + data: { object } + } +} + +export function checkoutCompleted(args: { customer: string; subscription: string; accountId: string }) { + return stripeEvent('checkout.session.completed', { + id: `cs_test_${eventCounter++}`, + object: 'checkout.session', + mode: 'subscription', + status: 'complete', + payment_status: 'paid', + customer: args.customer, + subscription: args.subscription, + client_reference_id: args.accountId, + metadata: { account_id: args.accountId } + }) +} + +export function invoiceEvent(type: 'invoice.paid' | 'invoice.payment_failed', subscription: string, customer: string) { + return stripeEvent(type, { + id: `in_test_${eventCounter++}`, + object: 'invoice', + customer, + parent: { type: 'subscription_details', subscription_details: { subscription } } + }) +} + +/** Signs exactly like Stripe (and stripe-node's generateTestHeaderString), via node:crypto. */ +export function signStripePayload(payload: string, secret: string, timestampSeconds = Math.floor(Date.now() / 1000)): string { + const signature = createHmac('sha256', secret).update(`${timestampSeconds}.${payload}`, 'utf8').digest('hex') + return `t=${timestampSeconds},v1=${signature}` +} diff --git a/cloud/apps/billing/test/webhook-signature.test.ts b/cloud/apps/billing/test/webhook-signature.test.ts new file mode 100644 index 00000000000..e977332e83e --- /dev/null +++ b/cloud/apps/billing/test/webhook-signature.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from 'vitest' +import { constantTimeEqual, splitSecrets, verifyStripeSignature } from '../src/webhook-signature' +import { signStripePayload } from './stripe-fake' + +const SECRET = 'whsec_test_signature' +const BODY = '{"id":"evt_test_1","object":"event"}' +const NOW = 1_762_000_000_000 +const T = NOW / 1000 + +describe('verifyStripeSignature', () => { + it('accepts a header signed the way Stripe signs', async () => { + expect(await verifyStripeSignature(BODY, signStripePayload(BODY, SECRET, T), [SECRET], NOW)).toEqual({ ok: true }) + }) + + it('accepts any of several v1 signatures and either secret during rotation', async () => { + const good = signStripePayload(BODY, 'whsec_new', T).split(',')[1] + const header = `t=${T},v1=${'0'.repeat(64)},${good},v0=ignored` + expect(await verifyStripeSignature(BODY, header, ['whsec_old', 'whsec_new'], NOW)).toEqual({ ok: true }) + }) + + it('rejects a tampered body, a wrong secret and a replayed old timestamp', async () => { + const header = signStripePayload(BODY, SECRET, T) + expect(await verifyStripeSignature(`${BODY} `, header, [SECRET], NOW)).toEqual({ ok: false, reason: 'mismatch' }) + expect(await verifyStripeSignature(BODY, header, ['whsec_other'], NOW)).toEqual({ ok: false, reason: 'mismatch' }) + expect(await verifyStripeSignature(BODY, header, [SECRET], NOW + 301_000)).toEqual({ ok: false, reason: 'stale' }) + }) + + it('rejects missing or malformed headers and an empty secret list', async () => { + expect(await verifyStripeSignature(BODY, null, [SECRET], NOW)).toEqual({ ok: false, reason: 'malformed' }) + expect(await verifyStripeSignature(BODY, 't=abc,v1=zz', [SECRET], NOW)).toEqual({ ok: false, reason: 'malformed' }) + expect(await verifyStripeSignature(BODY, signStripePayload(BODY, SECRET, T), [], NOW)).toEqual({ + ok: false, + reason: 'malformed' + }) + }) + + it('splits rotation secrets and compares in constant time', () => { + expect(splitSecrets(' whsec_a , ,whsec_b')).toEqual(['whsec_a', 'whsec_b']) + expect(constantTimeEqual('abc', 'abc')).toBe(true) + expect(constantTimeEqual('abc', 'abd')).toBe(false) + expect(constantTimeEqual('abc', 'abcd')).toBe(false) + }) +}) diff --git a/cloud/apps/billing/tsconfig.json b/cloud/apps/billing/tsconfig.json new file mode 100644 index 00000000000..69d420342f1 --- /dev/null +++ b/cloud/apps/billing/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "es2022", + "module": "es2022", + "moduleResolution": "bundler", + "lib": ["es2022"], + "types": ["@cloudflare/workers-types", "@types/node"], + "strict": true, + "noUncheckedIndexedAccess": true, + "noEmit": true, + "skipLibCheck": true + }, + "include": ["src", "test", "scripts"] +} diff --git a/cloud/apps/billing/vitest.config.ts b/cloud/apps/billing/vitest.config.ts new file mode 100644 index 00000000000..9f469145c99 --- /dev/null +++ b/cloud/apps/billing/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + // A string tsconfig skips the lookup, which otherwise climbs to the desktop app's tsconfig. + oxc: { tsconfig: false }, + test: { include: ['test/**/*.test.ts'] } +}) diff --git a/cloud/apps/billing/wrangler.dev.jsonc b/cloud/apps/billing/wrangler.dev.jsonc new file mode 100644 index 00000000000..b741e073b20 --- /dev/null +++ b/cloud/apps/billing/wrangler.dev.jsonc @@ -0,0 +1,37 @@ +{ + "$schema": "node_modules/wrangler/config-schema.json", + // DEV deployment only (workers.dev, no custom domain, Stripe TEST MODE). Deploy: + // wrangler d1 migrations apply BILLING_DB --remote -c wrangler.dev.jsonc + // wrangler deploy -c wrangler.dev.jsonc + // Secrets (wrangler secret put -c wrangler.dev.jsonc, never here): STRIPE_SECRET_KEY (sk_test_/rk_test_), + // STRIPE_WEBHOOK_SECRET (the test-mode endpoint's whsec_), CONTROL_PLANE_TOKEN (shared with + // orca-sandbox-control-dev), BILLING_DEV_JWKS (operator key set for e2e until auth mints billing tokens). + "name": "orca-billing-dev", + "main": "src/index.ts", + "compatibility_date": "2026-11-01", + "workers_dev": true, + "d1_databases": [ + { + "binding": "BILLING_DB", + "database_name": "orca-billing-dev", + "database_id": "12aa0820-642a-4174-8504-f42d32043959", + "migrations_dir": "migrations" + } + ], + // Retries Stripe meter reports that failed inline. + "triggers": { "crons": ["*/10 * * * *"] }, + "vars": { + "BILLING_ENV": "dev", + "STRIPE_API_VERSION": "2025-09-30.clover", + "STRIPE_METER_EVENT_NAME": "orca_sandbox_seconds", + // Stripe TEST MODE objects in acct_1Or7xeKb73TzRkjk (scripts/stripe-test-setup.ts). + "STRIPE_PRICE_TEAM_MONTHLY": "price_1UZmKpKb73TzRkjk33aiB2th", + "STRIPE_PRICE_TEAM_ANNUAL": "price_1UZmKpKb73TzRkjkS7qv85NI", + "STRIPE_PRICE_OVERAGE": "price_1UZmKvKb73TzRkjkycTvfq0o", + "STRIPE_PORTAL_CONFIGURATION": "bpc_1UZmKvKb73TzRkjk59rIuHbx", + "BILLING_RETURN_URL": "https://orca-billing-dev.lovecast.workers.dev/return", + "BILLING_JWT_AUDIENCE": "orca-billing", + "BILLING_JWKS_URL": "https://login.onorca.dev/.well-known/jwks.json", + "BILLING_AUTH_ISSUER": "https://login.onorca.dev" + } +} diff --git a/cloud/apps/billing/wrangler.jsonc b/cloud/apps/billing/wrangler.jsonc new file mode 100644 index 00000000000..d969ce6b18a --- /dev/null +++ b/cloud/apps/billing/wrangler.jsonc @@ -0,0 +1,30 @@ +{ + "$schema": "node_modules/wrangler/config-schema.json", + // Local `wrangler dev` only. No routes and workers.dev is off, so a deploy from this file + // publishes nothing reachable. Deployed environments live in the private deploy config. + // Secrets (stdin only, never here): STRIPE_SECRET_KEY (rk_test_...), STRIPE_WEBHOOK_SECRET, + // CONTROL_PLANE_TOKEN. For local dev put them in an untracked .dev.vars. + "name": "orca-billing-local", + "main": "src/index.ts", + "compatibility_date": "2026-11-01", + "workers_dev": false, + "d1_databases": [ + { + "binding": "BILLING_DB", + "database_name": "orca-billing-local", + "database_id": "00000000-0000-0000-0000-000000000000", + "migrations_dir": "migrations" + } + ], + // Retries Stripe meter reports that failed inline. + "triggers": { "crons": ["*/10 * * * *"] }, + "vars": { + "STRIPE_API_VERSION": "2025-09-30.clover", + "STRIPE_METER_EVENT_NAME": "orca_sandbox_seconds", + "STRIPE_PRICE_TEAM_MONTHLY": "price_local_team_month", + "STRIPE_PRICE_TEAM_ANNUAL": "price_local_team_year", + "STRIPE_PRICE_OVERAGE": "price_local_overage", + "BILLING_RETURN_URL": "http://127.0.0.1:8790/return", + "BILLING_JWT_AUDIENCE": "orca-billing" + } +} diff --git a/cloud/apps/sandbox-control/image/build-golden-image.ts b/cloud/apps/sandbox-control/image/build-golden-image.ts index 9851eb7c496..28dec1a8f29 100644 --- a/cloud/apps/sandbox-control/image/build-golden-image.ts +++ b/cloud/apps/sandbox-control/image/build-golden-image.ts @@ -1,12 +1,21 @@ // Operator CLI. Dry run by default (prints the plan, touches nothing). With --execute it builds in // Orca's Vercel team and prints {imageVersion, snapshotId, report}; set GOLDEN_SNAPSHOT_ID from it. -// pnpm --filter @orca-cloud/sandbox-control exec tsx image/build-golden-image.ts [--execute] +// pnpm --filter @orca-cloud/sandbox-control exec tsx image/build-golden-image.ts [--release v1.4.256] [--execute] +// --release pins the released orca-server tarball + checksum; without it the manifest's bundleUrl is used. // --execute needs VERCEL_TOKEN, VERCEL_TEAM_ID, VERCEL_PROJECT_ID in the environment (never in git). import { randomBytes } from 'node:crypto' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { createVercelSandboxApi } from '../src/vercel-sandbox-api' -import { ManifestSchema, VM_SCRIPTS, buildGoldenImage, manifestGaps, provisionEnv, type BuildInputs } from './golden-image' +import { + ManifestSchema, + VM_SCRIPTS, + buildGoldenImage, + manifestGaps, + provisionEnv, + resolveReleaseBundle, + type BuildInputs +} from './golden-image' const here = (path: string): string => fileURLToPath(new URL(path, import.meta.url).href) @@ -25,6 +34,11 @@ function loadInputs(): BuildInputs { async function main(): Promise { const inputs = loadInputs() + const releaseFlag = process.argv.indexOf('--release') + if (releaseFlag !== -1) { + const pinned = await resolveReleaseBundle(process.argv[releaseFlag + 1] ?? '') + inputs.manifest = { ...inputs.manifest, orcad: { bundleUrl: pinned.bundleUrl, sha256: pinned.sha256 } } + } const execute = process.argv.includes('--execute') const gaps = manifestGaps(inputs.manifest) if (!execute) { diff --git a/cloud/apps/sandbox-control/image/golden-image.test.ts b/cloud/apps/sandbox-control/image/golden-image.test.ts index c523ce93526..e68ef00d1cc 100644 --- a/cloud/apps/sandbox-control/image/golden-image.test.ts +++ b/cloud/apps/sandbox-control/image/golden-image.test.ts @@ -1,7 +1,7 @@ import { spawnSync } from 'node:child_process' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' -import { VM_SCRIPTS, buildGoldenImage, type BuildInputs } from './golden-image' +import { VM_SCRIPTS, buildGoldenImage, resolveReleaseBundle, type BuildInputs } from './golden-image' const manifest: BuildInputs['manifest'] = { imageVersion: '2026-11-08.1', @@ -90,4 +90,14 @@ describe('golden image build', () => { expect(spawnSync('bash', ['-n', path]).status, script).toBe(0) } }) + + it('pins a released orca-server tarball by its published checksum', async () => { + const sums = `${'b'.repeat(64)} orca-server-linux-arm64.tar.gz\n${'c'.repeat(64)} orca-server-linux-x64.tar.gz\n` + const pinned = await resolveReleaseBundle('v1.4.256', 'x64', async () => new Response(sums)) + expect(pinned).toEqual({ + bundleUrl: 'https://github.com/stablyai/orca/releases/download/v1.4.256/orca-server-linux-x64.tar.gz', + sha256: 'c'.repeat(64) + }) + await expect(resolveReleaseBundle('v1.4.254', 'x64', async () => new Response('', { status: 404 }))).rejects.toThrow(/source build/) + }) }) diff --git a/cloud/apps/sandbox-control/image/golden-image.ts b/cloud/apps/sandbox-control/image/golden-image.ts index b0f87d4a4d4..f12fcfcf18f 100644 --- a/cloud/apps/sandbox-control/image/golden-image.ts +++ b/cloud/apps/sandbox-control/image/golden-image.ts @@ -34,6 +34,32 @@ export function manifestGaps(manifest: GoldenImageManifest): string[] { return gaps } +/** + * Orca releases ship `orca-server-linux-.tar.gz` plus `orca-server-sha256sums.txt` (stablyai/orca#40280). + * Pinning a release tag replaces the ~100 s in-sandbox source build; a manual bundleUrl/sha256 stays the fallback. + */ +export async function resolveReleaseBundle( + tag: string, + arch: 'x64' | 'arm64' = 'x64', + doFetch: typeof fetch = fetch +): Promise<{ bundleUrl: string; sha256: string }> { + if (!/^v\d+\.\d+\.\d+$/.test(tag)) { + throw new Error(`release tag must look like v1.2.3: ${tag}`) + } + const base = `https://github.com/stablyai/orca/releases/download/${tag}` + const asset = `orca-server-linux-${arch}.tar.gz` + const response = await doFetch(`${base}/orca-server-sha256sums.txt`) + if (!response.ok) { + throw new Error(`${tag} has no orca-server-sha256sums.txt (HTTP ${response.status}); use the source build`) + } + const line = (await response.text()).split('\n').find((l) => l.trim().endsWith(asset)) + const sha256 = line?.trim().split(/\s+/)[0] + if (!sha256 || !/^[a-f0-9]{64}$/.test(sha256)) { + throw new Error(`${tag} lists no checksum for ${asset}`) + } + return { bundleUrl: `${base}/${asset}`, sha256 } +} + export type BuildInputs = { manifest: GoldenImageManifest provisionScript: string diff --git a/cloud/apps/sandbox-control/image/provision-golden.sh b/cloud/apps/sandbox-control/image/provision-golden.sh index 9d54d8c9ec7..a5496de3d0e 100755 --- a/cloud/apps/sandbox-control/image/provision-golden.sh +++ b/cloud/apps/sandbox-control/image/provision-golden.sh @@ -32,6 +32,14 @@ bundle="$(mktemp -d)/orcad.tgz" fetch_verified "$ORCAD_BUNDLE_URL" "$ORCAD_BUNDLE_SHA256" "$bundle" tar -xzf "$bundle" -C "$ORCA_HOME/runtime" rm -rf "$(dirname "$bundle")" +# Release tarballs (orca-server-linux-*) may nest the server; normalize to runtime/orcad + runtime/runtimes. +if [ ! -s "$ORCA_HOME/runtime/orcad/orcad-server.js" ]; then + server="$(find "$ORCA_HOME/runtime" -name orcad-server.js -type f | head -n 1)" + [ -n "$server" ] || { log 'bundle has no orcad-server.js'; exit 1; } + root="$(dirname "$(dirname "$server")")" + ln -sfn "$(dirname "$server")" "$ORCA_HOME/runtime/orcad" + [ -d "$ORCA_HOME/runtime/runtimes" ] || ln -sfn "$root/runtimes" "$ORCA_HOME/runtime/runtimes" +fi test -s "$ORCA_HOME/runtime/orcad/orcad-server.js" node="$(find "$ORCA_HOME/runtime/runtimes" -type f -name node | head -n 1)" test -x "$node" diff --git a/cloud/apps/sandbox-control/src/entitlements.ts b/cloud/apps/sandbox-control/src/entitlements.ts index e3692fa4259..6d596180b65 100644 --- a/cloud/apps/sandbox-control/src/entitlements.ts +++ b/cloud/apps/sandbox-control/src/entitlements.ts @@ -13,6 +13,8 @@ export type StartDecision = { suspendRunning: boolean /** Paid seats, when billing reports them; scales the control plane's per-account backstop. */ seats?: number + /** Billing's account-wide cap (3 × billed seats); replaces the seat-derived backstop when present. */ + maxConcurrentSandboxes?: number } export type UsageSegment = { @@ -22,6 +24,8 @@ export type UsageSegment = { machineSize: MachineSize startedAt: number endedAt: number + /** The member who ran the sandbox, for per-member attribution in billing. */ + userId?: string } export type UsageAnswer = { canStartSandbox: boolean; reason: string | null; suspendRunning: boolean } @@ -31,6 +35,9 @@ export type EntitlementClient = { accountId: string machineSize: MachineSize runningSandboxes: number + /** The member starting it; billing caps each member (member_concurrency_limit). */ + userId?: string + runningSandboxesForUser?: number }): Promise reportUsage(segment: UsageSegment): Promise /** Accounts whose payment grace expired; polled by the cron. */ @@ -48,7 +55,8 @@ const StartAnswer = z.looseObject({ allow: z.boolean(), reason: z.string().nullable(), suspendRunning: z.boolean().default(false), - seats: z.number().int().positive().max(100_000).optional() + seats: z.number().int().nonnegative().max(100_000).optional(), + maxConcurrentSandboxes: z.number().int().nonnegative().max(1_000_000).optional() }) const UsageResponse = z.looseObject({ canStartSandbox: z.boolean(), @@ -77,7 +85,8 @@ export const STOP_RUNNING_REASONS: ReadonlySet = new Set([ /** Dev operator test accounts (`dev:*`, see auth.ts) never reach billing outside prod. */ export function withDevAccountBypass(env: Env, client: EntitlementClient): EntitlementClient { - if (env.SANDBOX_ENV === 'prod') { + // SANDBOX_DEV_BILLING_PASSTHROUGH=true sends dev accounts to billing too (live smoke of the internal routes). + if (env.SANDBOX_ENV === 'prod' || env.SANDBOX_DEV_BILLING_PASSTHROUGH === 'true') { return client } return { diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 3e0c3692546..478ee7648fe 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -31,6 +31,8 @@ export type Env = { CONTROL_PLANE_TOKEN?: string /** `stub` allows every start and records nothing: local dev only, refused when SANDBOX_ENV=prod. */ ENTITLEMENTS_MODE?: string + /** Dev only: route `dev:*` accounts through billing instead of bypassing it. */ + SANDBOX_DEV_BILLING_PASSTHROUGH?: string /** Public URL of this Worker's /v1/egress/proxy; Vercel forwardURL rules point here. */ EGRESS_PROXY_URL?: string diff --git a/cloud/apps/sandbox-control/src/guardrails.ts b/cloud/apps/sandbox-control/src/guardrails.ts index dbb16908908..53af0905f2e 100644 --- a/cloud/apps/sandbox-control/src/guardrails.ts +++ b/cloud/apps/sandbox-control/src/guardrails.ts @@ -79,14 +79,15 @@ export async function assertStartsOpen(db: D1Database, config: GuardrailConfig): } } -/** Per-account backstop (3/seat); `seats` comes from billing when it reports one, else 1. */ +/** Per-account backstop: billing's `maxConcurrentSandboxes` when it sends one, else 3 per seat (seats default 1). */ export async function assertAccountCapacity( db: D1Database, config: GuardrailConfig, accountId: string, - seats = 1 + limits: { seats?: number; maxConcurrentSandboxes?: number } = {} ): Promise { - if ((await countActive(db, accountId)) >= config.maxActivePerSeat * Math.max(1, Math.floor(seats))) { + const cap = limits.maxConcurrentSandboxes ?? config.maxActivePerSeat * Math.max(1, Math.floor(limits.seats ?? 1)) + if ((await countActive(db, accountId)) >= cap) { throw new GuardrailError('concurrency_limit') } } diff --git a/cloud/apps/sandbox-control/src/lifecycle.ts b/cloud/apps/sandbox-control/src/lifecycle.ts index 8d70a55fae0..58ecbedabbf 100644 --- a/cloud/apps/sandbox-control/src/lifecycle.ts +++ b/cloud/apps/sandbox-control/src/lifecycle.ts @@ -20,6 +20,7 @@ import { ORCAD_PORT, drainOrcad, startOrcad, type OrcadReady } from './sandbox-v import { NO_SESSION, countActiveSandboxes, + countActiveSandboxesForUser, getSandbox, insertSandbox, listAccountSandboxes, @@ -156,17 +157,27 @@ export function guardrails(deps: LifecycleDeps): GuardrailConfig { return deps.config.guardrails ?? DEFAULT_GUARDRAILS } -async function requireStart(deps: LifecycleDeps, accountId: string, machineSize: MachineSize) { +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 decision = await deps.entitlements.checkStart({ accountId, machineSize, runningSandboxes }) + const runningSandboxesForUser = await countActiveSandboxesForUser(deps.db, accountId, userId) + const decision = await deps.entitlements.checkStart({ + accountId, + machineSize, + runningSandboxes, + userId, + runningSandboxesForUser + }) if (decision.suspendRunning) { await suspendAccountRunning(deps, accountId) } if (!decision.allow) { throw new LifecycleError('entitlement_denied', decision.reason ?? 'denied') } - await assertAccountCapacity(deps.db, guardrails(deps), accountId, decision.seats) + await assertAccountCapacity(deps.db, guardrails(deps), accountId, { + seats: decision.seats, + maxConcurrentSandboxes: decision.maxConcurrentSandboxes + }) } async function boot(deps: LifecycleDeps, row: SandboxRow, state: ProviderSandboxState, repo?: { url: string; ref?: string }): Promise { @@ -249,7 +260,7 @@ export async function createSandbox( } return { row: existing, connection: await startOrcad(deps.provider, state, { sandboxId: existing.id }) } } - await requireStart(deps, caller.accountId, request.machineSize) + await requireStart(deps, caller.accountId, caller.userId, request.machineSize) const id = randomId() const row = await insertSandbox( deps.db, @@ -362,7 +373,7 @@ export async function resumeSandbox(deps: LifecycleDeps, row: SandboxRow): Promi if (row.status !== 'suspended') { throw new LifecycleError('conflict', `sandbox_${row.status}`) } - await requireStart(deps, row.account_id, row.machine_size) + await requireStart(deps, row.account_id, row.user_id, row.machine_size) const resuming = await claim(deps, row, { status: 'resuming' }) return wake(deps, resuming) } diff --git a/cloud/apps/sandbox-control/src/network-policy.ts b/cloud/apps/sandbox-control/src/network-policy.ts index 70980538fb3..763e24c3700 100644 --- a/cloud/apps/sandbox-control/src/network-policy.ts +++ b/cloud/apps/sandbox-control/src/network-policy.ts @@ -36,12 +36,19 @@ export const DEFAULT_EGRESS_ALLOWLIST: readonly string[] = [ 'codeload.github.com', 'objects.githubusercontent.com', 'raw.githubusercontent.com', + // Agent CLIs reach several vendor hosts (API, sign-in, telemetry, updates); found live: platform.claude.com. + 'anthropic.com', 'api.anthropic.com', + '*.anthropic.com', 'claude.ai', - 'console.anthropic.com', + '*.claude.ai', + 'claude.com', + '*.claude.com', + 'openai.com', 'api.openai.com', - 'auth.openai.com', + '*.openai.com', 'chatgpt.com', + '*.chatgpt.com', 'generativelanguage.googleapis.com', 'oauth2.googleapis.com', 'accounts.google.com', diff --git a/cloud/apps/sandbox-control/src/store.ts b/cloud/apps/sandbox-control/src/store.ts index a052a109754..e2c2e1766ac 100644 --- a/cloud/apps/sandbox-control/src/store.ts +++ b/cloud/apps/sandbox-control/src/store.ts @@ -115,6 +115,15 @@ export async function countActiveSandboxes(db: D1Database, accountId: string): P return row?.n ?? 0 } +export async function countActiveSandboxesForUser(db: D1Database, accountId: string, userId: string): Promise { + const placeholders = ACTIVE_STATUSES.map(() => '?').join(', ') + const row = await db + .prepare(`SELECT COUNT(*) AS n FROM sandboxes WHERE account_id = ? AND user_id = ? AND status IN (${placeholders})`) + .bind(accountId, userId, ...ACTIVE_STATUSES) + .first<{ n: number }>() + return row?.n ?? 0 +} + export async function listByStatus(db: D1Database, statuses: readonly SandboxStatus[]): Promise { const placeholders = statuses.map(() => '?').join(', ') const { results } = await db diff --git a/cloud/apps/sandbox-control/src/usage.ts b/cloud/apps/sandbox-control/src/usage.ts index 381c21c8d42..1e8642fd921 100644 --- a/cloud/apps/sandbox-control/src/usage.ts +++ b/cloud/apps/sandbox-control/src/usage.ts @@ -13,6 +13,7 @@ type OutboxRow = { started_at: number ended_at: number attempts: number + user_id: string | null } /** Queues the row's unsettled time up to `until`; returns the new settle cursor (unchanged if none). */ @@ -46,8 +47,9 @@ export async function flushUsage( ): Promise { const { results } = await db .prepare( - `SELECT * FROM usage_outbox WHERE sent_at IS NULL AND attempts < ? ${sandboxId ? 'AND sandbox_id = ?' : ''} - ORDER BY started_at LIMIT 100` + `SELECT o.*, s.user_id FROM usage_outbox o LEFT JOIN sandboxes s ON s.id = o.sandbox_id + WHERE o.sent_at IS NULL AND o.attempts < ? ${sandboxId ? 'AND o.sandbox_id = ?' : ''} + ORDER BY o.started_at LIMIT 100` ) .bind(...(sandboxId ? [MAX_ATTEMPTS, sandboxId] : [MAX_ATTEMPTS])) .all() @@ -60,7 +62,8 @@ export async function flushUsage( sandboxId: segment.sandbox_id, machineSize: segment.machine_size, startedAt: segment.started_at, - endedAt: segment.ended_at + endedAt: segment.ended_at, + ...(segment.user_id ? { userId: segment.user_id } : {}) }) await db.prepare('UPDATE usage_outbox SET sent_at = ? WHERE usage_id = ?').bind(now, segment.usage_id).run() } catch { diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts index ac0835a1f17..b30591fd7a0 100644 --- a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -295,8 +295,21 @@ export function createVercelSandboxApi( } }, - deleteSnapshot: (snapshotId) => - missingOk(() => request('DELETE', `/v2/sandboxes/snapshots/${encodeURIComponent(snapshotId)}`)) + async deleteSnapshot(snapshotId) { + const path = `/v2/sandboxes/snapshots/${encodeURIComponent(snapshotId)}` + try { + await missingOk(() => request('DELETE', path)) + } catch (error) { + // Vercel answers 400 "Snapshot expired or deleted" for one already gone (found live); confirm, then accept. + if (!(error instanceof ProviderError && error.status === 400)) { + throw error + } + const current = await json(z.looseObject({ snapshot: Snapshot }), await request('GET', path)) + if (current.snapshot.status !== 'deleted') { + throw error + } + } + } } } diff --git a/cloud/apps/sandbox-control/test/entitlements-client.test.ts b/cloud/apps/sandbox-control/test/entitlements-client.test.ts index 69603b012f8..b5afa20f386 100644 --- a/cloud/apps/sandbox-control/test/entitlements-client.test.ts +++ b/cloud/apps/sandbox-control/test/entitlements-client.test.ts @@ -13,13 +13,16 @@ describe('billing client', () => { if (url.pathname === '/v1/internal/suspensions') { return Response.json({ accounts: [{ accountId: 'org:o1', pastDueSince: 1, graceEndsAt: 2 }] }) } - return Response.json({ allow: false, reason: 'payment_grace_expired', suspendRunning: true, entitlement: {} }) + // Live billing dev answers seats 0 / cap 0 for an account without a subscription. + return Response.json({ allow: false, reason: 'payment_grace_expired', suspendRunning: true, seats: 0, maxConcurrentSandboxes: 0, entitlement: {} }) }) expect(await client.listSuspensions()).toEqual(['org:o1']) expect(await client.checkStart({ accountId: 'org:o1', machineSize: 'standard', runningSandboxes: 0 })).toEqual({ allow: false, reason: 'payment_grace_expired', suspendRunning: true, + seats: 0, + maxConcurrentSandboxes: 0, entitlement: {} }) expect(seen[0]).toBe(`GET /v1/internal/suspensions Bearer ${env.CONTROL_PLANE_TOKEN}`) diff --git a/cloud/apps/sandbox-control/test/harness.ts b/cloud/apps/sandbox-control/test/harness.ts index a5752b0f16d..1609ddbb23c 100644 --- a/cloud/apps/sandbox-control/test/harness.ts +++ b/cloud/apps/sandbox-control/test/harness.ts @@ -21,11 +21,13 @@ export async function createHarness(overrides: Partial = {}, extraDeps: Par suspensions: [] as string[], down: false, checks: [] as { accountId: string; runningSandboxes: number }[], + requests: [] as Parameters[0][], usage: [] as UsageSegment[] } const entitlements: EntitlementClient = { async checkStart(request) { billing.checks.push({ accountId: request.accountId, runningSandboxes: request.runningSandboxes }) + billing.requests.push(request) return billing.decision }, async reportUsage(segment) { diff --git a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts index 50af6ad9350..ec4f44aefc8 100644 --- a/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts +++ b/cloud/apps/sandbox-control/test/lifecycle-flow.test.ts @@ -225,4 +225,17 @@ describe('sandbox control plane cron', () => { const h = await createHarness() expect((await h.call('POST', '/v1/sandboxes', { body: { ...create, machineSize: 'xlarge' } })).status).toBe(400) }) + + it('applies billing\'s account cap and tells billing which member starts and ran each segment', async () => { + const h = await createHarness() + h.billing.decision = { allow: true, reason: null, suspendRunning: false, maxConcurrentSandboxes: 1 } + const first = await h.call('POST', '/v1/sandboxes', { body: create }) + expect(first.status).toBe(200) + const second = await h.call('POST', '/v1/sandboxes', { body: { ...create, workspaceId: 'ws-2' }, as: 'alice' }) + expect(second.status).toBe(403) + expect(h.billing.requests[1]).toMatchObject({ userId: 'alice', runningSandboxes: 1, runningSandboxesForUser: 1 }) + h.advance(5 * MIN) + await h.call('POST', `/v1/sandboxes/${first.body.sandbox.id}/suspend`) + expect(h.billing.usage[0]).toMatchObject({ userId: 'alice', sandboxId: first.body.sandbox.id }) + }) }) diff --git a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts index b960ad57b8b..429db260c30 100644 --- a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts +++ b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts @@ -87,6 +87,16 @@ describe('Vercel Sandbox REST client', () => { expect(seen[1]!.url.searchParams.get('deleteOrphanSnapshots')).toBe('true') }) + it('accepts deleting a snapshot Vercel already deleted (400 "expired or deleted")', async () => { + const { client, seen } = api((s) => + s.method === 'DELETE' + ? Response.json({ error: { code: 'bad_request', message: 'Snapshot expired or deleted.' } }, { status: 400 }) + : Response.json({ snapshot: { id: 'snap_1', status: 'deleted', sourceSessionId: 'sess_1' } }) + ) + await client.deleteSnapshot('snap_1') + expect(seen.map((s) => s.method)).toEqual(['DELETE', 'GET']) + }) + it('surfaces provider failures as codes without their bodies', async () => { const { client } = api(() => Response.json({ error: { code: 'rate_limited', message: 'secret detail' } }, { status: 429 })) const error = await client.stopSession('sess_1').catch((caught: unknown) => caught) diff --git a/cloud/docs/accounts-billing-design.md b/cloud/docs/accounts-billing-design.md new file mode 100644 index 00000000000..5f367d16aa5 --- /dev/null +++ b/cloud/docs/accounts-billing-design.md @@ -0,0 +1,152 @@ +# Orca accounts + billing — design (v1) + +Status: built in `nwparker/orca-private` (base branch `nwparker/accounts-billing`). Stripe TEST MODE only. +Code: `cloud/apps/billing` (Worker), desktop `src/main/billing`, `src/renderer/src/components/billing`. + +## Pricing (final, all config) + +| Plan | Price | Includes | +|---|---|---| +| Team | $50/seat/mo or $480/seat/yr | 60 Standard (4 vCPU / 8 GB) sandbox hours per seat per month, pooled across the org; Large (8/16) burns 2x; 3 running sandboxes per seat; 15-min idle sleep | +| Overage | opt-in, off by default | $0.75/h Standard ($1.50/h Large); monthly spend cap, default $25, admins can raise (max $5,000); warnings at 80% / 100% of included hours | +| Enterprise | Custom | "Contact us" (no Stripe price) | + +No free plan, no trial, no other tiers. Numbers and labels live in `src/plans.ts` defaults and can be overridden +without a code change by `BILLING_PLANS` (JSON, validated; malformed config fails closed with 503). Stripe price +ids are Worker vars. Annual plans still get a **monthly** allowance window (a calendar-month slice of the year, +anchored on the subscription start). Stripe Tax: `STRIPE_AUTOMATIC_TAX=true` adds `automatic_tax` + +`tax_id_collection` to Checkout; default off (activate Stripe Tax with the California origin address, SaaS tax code +and registrations first). + +## Identity — reuse Orca Cloud sign-in + +- Desktop PKCE login against `login.onorca.dev` already mints audience-scoped short-lived ES256 JWTs for other + services (relay, connector). Billing uses the same: `POST /v1/desktop/auth/billing-token` (**to add in the auth + service**) returns a ≤15-min JWT, `aud=orca-billing`, `purpose=billing`, `sub`, `org?`, `orgRole?`, `email?`. +- Billing account = `org:`, else `user:`. Only owners/admins buy, manage billing, or change overage. +- Users/orgs/membership/invites stay owned by auth (provider roster: PropelAuth). Billing stores only account ids. +- **Live role:** the billing-token route re-reads the caller's role from the live provider roster + (stablyai/orca-cloud#690; before it, the role came from a mirror that rebuilds only at sign-in). Billing also + requires checkout/portal/overage to use a token minted in the last 6 minutes, so a demotion or removal stops + billing actions within that window. + +## Team seats follow membership + +- **Source of truth:** auth's provider roster. Seats = members; pending invites never count; never fewer than 1. +- **Pull:** `POST {AUTH_INTERNAL_URL}/v1/internal/billing/org-members {orgId}` → `{orgId, members}` (shared secret + `MEMBERSHIP_SYNC_TOKEN`). Billing pulls at Checkout (the quantity ignores the desktop's request) and on the + 10-minute cron (`reconcileSeats`), which also picks up invites accepted at the provider. +- **Push:** auth calls `POST /v1/internal/seats {accountId, members}` after Orca-side removals (best effort). +- **Fail closed:** if auth is configured but unreachable, Checkout answers 503 `membership_unavailable`; malformed or + missing counts are 400; nothing ever bills 0 seats. No token claim carries a count (tokens can be stale). +- **Proration:** adding seats uses `always_invoice` (the prorated rest-of-period amount is invoiced and charged now, + so annual plans don't get free seats until renewal); removing seats uses Stripe's default `create_prorations` + (unused time becomes a credit on the next invoice; no card refund). The Customer Portal no longer edits + quantity; it only switches monthly↔annual and cancels. +- Desktop: Settings → Orca account → Team (also "Team" in the account menu): members with roles, invite by email + (accepted at the sign-in provider), change role, remove (with confirm), revoke invites; owners/admins only, + mirroring auth's role rules. + +## Payment failure + +- On `invoice.payment_failed` (status `past_due`, later `unpaid` when retries are exhausted) billing records + `past_due_since` (first time seen; a later failed retry does not restart it). +- New sandbox starts stop immediately (`payment_past_due`). After `paymentGraceHours` (96 h = 4 days, config) + running sandboxes must be suspended: reason `payment_grace_expired`, `suspendRunning: true` on + `/v1/internal/entitlements/check` and `/v1/internal/usage`, and `GET /v1/internal/suspensions` lists every + account past its grace (for a control-plane cron). Once the invoice is paid the status returns to `active`, + `past_due_since` clears, and starts/resume are allowed again. +- Desktop shows a banner with the suspension date and an "Update payment method" button (Portal). +- Stripe sends the dunning emails and retries (Smart Retries) — dashboard settings, see "What the user sets". + +## Why Cloudflare Worker + D1 + +Extends the existing Workers setup (relay-edge); a separate D1 keeps the Stripe key and billing ledger isolated +from auth/relay data (least privilege); load is small; Cron Trigger retries the meter outbox. A Durable Object per +account is the upgrade path for strict per-account start locking. + +## Data model (D1 migrations 0001, 0002) + +| Table | Purpose | +|---|---| +| `billing_accounts` | account → Stripe customer; `overage_opt_in`, `spend_cap_cents` | +| `subscriptions` | Stripe mirror: status, seats, interval, period, overage item id | +| `stripe_events` | webhook idempotency | +| `usage_events` | usage segments keyed by caller `usageId`; weighted seconds; allowance-window start; overage fixed at insert | +| `meter_reports` | outbox of billable overage → Stripe meter events, capped at the spend cap in SQL | + +## Stripe integration + +- **Checkout** (`POST /v1/billing/checkout {plan:'team', interval, seats}`): hosted, `mode=subscription`, seat price + (quantity adjustable on the page), `payment_method_collection=always`, `subscription_data.metadata.account_id`. + One customer per account. 409 if a subscription is open (changes go through the Portal). +- **Customer Portal**: configuration `STRIPE_PORTAL_CONFIGURATION` allows seat quantity changes, monthly↔annual + switch (prorated), cancel at period end, payment method, invoices. +- **Overage opt-in** (`POST /v1/billing/overage {enabled, spendCapCents?}`): first opt-in adds the monthly metered + overage price to the subscription (idempotent); opting out stops metering immediately (the item stays so + already-reported usage still invoices). Annual subscriptions accept the monthly metered item (verified). +- **Webhooks**: HMAC-SHA256 over the raw body (5-min tolerance, rotation), event-id dedupe, then re-read the + subscription from the API (order/staleness-proof). Handles `checkout.session.completed`, `customer.subscription.*`, + `invoice.paid`, `invoice.payment_failed` (old and new payload shapes). Credited only to the customer billing created + for that account. Live-mode events are acked and ignored. Failures → 500 so Stripe retries. +- **Meter**: `orca_sandbox_seconds` (sum by customer); overage price $0.75/h via `transform_quantity.divide_by=3600`. + Only billable overage is sent, at most the remaining spend cap; `identifier=usageId` + Idempotency-Key. + +## Entitlements + +- Desktop `GET /v1/billing/entitlements`: plan, status, interval, seats, period, monthly usage window, + included/used/remaining, `usageWarningPercent`, overage {optedIn, active, cap, spent, rate}, limits, + `canStartSandbox`, `reason`, `canManageBilling`, and `offers` (Team + Enterprise, for the plan cards). +- Control plane (bearer `CONTROL_PLANE_TOKEN`): + - `POST /v1/internal/entitlements/check {accountId, machineSize: standard|large, runningSandboxes}` → `{allow, reason}`. + - `POST /v1/internal/usage {usageId, accountId, sandboxId, machineSize, startedAt, endedAt}` (segments ≤24 h). + - `GET /v1/internal/suspensions` → `{accounts: [{accountId, pastDueSince, graceEndsAt}]}`. + - Reasons: `no_subscription | subscription_inactive | payment_past_due | payment_grace_expired | + included_hours_exhausted | overage_cap_reached | machine_size_not_in_plan | concurrency_limit`. + Stop running sandboxes when `suspendRunning` is true (and for the reasons the control plane already stops on). + +## Security + +No card data touches us (hosted pages; desktop opens only `checkout.stripe.com` / `billing.stripe.com`, and an https +Enterprise contact URL). Secrets only via `wrangler secret put` / untracked `.dev.vars`. Worker refuses live keys and +live events unless `STRIPE_ALLOW_LIVEMODE=true`. Pinned `Stripe-Version`; strict zod bodies; size limits; redirects +never followed with the key; JWT max age 15 min. + +## Tests + +- Unit/flow: `cd cloud && pnpm --filter @orca-cloud/billing test` (D1 fake over node:sqlite with real migrations, + in-memory Stripe behind fetch, Stripe-shaped fixtures, Stripe-identical signatures). +- Live TEST-mode e2e: `scripts/stripe-test-setup.ts` (catalog + portal) and `scripts/e2e-test-mode.ts run` against + `wrangler dev` + `stripe listen --project-name orca`: Checkout with the test card for 2 seats, overage opt-in, + usage past included hours → meter events (checked against Stripe's meter summaries), spend cap blocks starts, + annual + overage, seat add/remove proration, test-clock renewal, `invoice.payment_failed` → past_due → grace + expiry → suspend → pay → resume (36 s grace via `BILLING_PLANS` locally), cancel. 21/21 passed locally. + +## Dev deployment (TEST MODE, for the sandbox control plane) + +- **Billing dev URL:** `https://orca-billing-dev.lovecast.workers.dev` (Cloudflare account "Stably", + `wrangler.dev.jsonc`, `BILLING_ENV=dev`). Control plane config: `BILLING_URL=https://orca-billing-dev.lovecast.workers.dev`, + `CONTROL_PLANE_TOKEN` = the same secret set on both Workers, and send `machineSize` `standard|large` only. +- D1 `orca-billing-dev` (`12aa0820-642a-4174-8504-f42d32043959`), migrations 0001–0002 applied. +- Stripe test webhook endpoint `we_1UZmcXKb73TzRkjkJMKg0jso` → `/v1/stripe/webhook` (API `2025-09-30.clover`). +- Secrets (stdin only): `STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET`, `CONTROL_PLANE_TOKEN`, `BILLING_DEV_JWKS`. +- Desktop auth: JWKS `https://login.onorca.dev/.well-known/jwks.json`, issuer `https://login.onorca.dev`. Until auth mints + billing tokens, dev also accepts the operator key set `BILLING_DEV_JWKS` (issuer `orca-billing-dev-test`; refused + when `BILLING_ENV=prod`). +- Same e2e run against dev: 18/18 passed (grace expiry is exercised locally; dev keeps the 96 h grace). + +## What the user sets + +| Where | Name | Value | +|---|---|---| +| billing Worker (dev/prod) secret | `MEMBERSHIP_SYNC_TOKEN` | random ≥32 chars; same value as auth's `ORCA_CLOUD_BILLING_MEMBERSHIP_TOKEN` | +| billing Worker var | `AUTH_INTERNAL_URL` | auth origin, e.g. `https://login.onorca.dev` (after orca-cloud#690 deploys) | +| auth (Cloud Run) secret | `ORCA_CLOUD_BILLING_MEMBERSHIP_TOKEN` | same value as above | +| auth (Cloud Run) env | `ORCA_CLOUD_BILLING_INTERNAL_URL` | billing URL, e.g. `https://orca-billing-dev.lovecast.workers.dev` | +| sandbox-control-dev secret | `CONTROL_PLANE_TOKEN` | same value as billing dev's `CONTROL_PLANE_TOKEN` | +| sandbox-control-dev var | `BILLING_URL` | `https://orca-billing-dev.lovecast.workers.dev` (and drop `ENTITLEMENTS_MODE=stub`) | +| Stripe dashboard (test, then live) | Billing → Revenue recovery | Smart Retries on; "Send emails when card payments fail" on; after retries → mark subscription unpaid | + +## Open decisions + +1. Real Enterprise contact URL (placeholder `https://onorca.dev/contact-sales`). 2. When to enable Stripe Tax. diff --git a/cloud/docs/vercel-driver-design.md b/cloud/docs/vercel-driver-design.md index 6c237f40a87..afd769c86df 100644 --- a/cloud/docs/vercel-driver-design.md +++ b/cloud/docs/vercel-driver-design.md @@ -158,6 +158,15 @@ api.github.com through the forwardURL proxy with a minted token (200, ~1s), no t reached over a service binding (same-account workers.dev fetches fail with 1042): an account without a subscription gets 403 `no_subscription`; the cron's suspensions poll runs clean. `dev:*` operator accounts bypass billing outside prod. +Desktop dogfood (2026-11-09, dev app → dev Worker → Vercel Pro): "Orca Cloud" is a built-in provider in Run on +(reserved id `cloud`); create → orcad ready in the app 8–12s with the private repo cloned via the GitHub App; shell +work + real `git push` from the cloud workspace; 15-min cap warning toast; forced rollover at the (shortened) cap +with reconnect and files kept; idle sleep through the ordinary Sleep flow after a (shortened) 2-min idle; wake on +click; destroy through runtime cleanup leaves no sandbox or snapshot. Found live and fixed: agent hosts must be +wildcard-allowlisted (platform.claude.com), the runtime store dropped `provider` (now in the feature store), remote +keystrokes are not 'driving' input (verdict reads all input), Vercel answers 400 when deleting an +already-deleted snapshot. Not run: agent auto-resume with a real chat (needs a Claude/Codex sign-in). + ## Built (mock-tested) and next wiring - (b) `src/main/environment-providers/*`, `src/main/hosted-sandbox/*` — driver, client, contract kit, recipe adapter. diff --git a/cloud/pnpm-lock.yaml b/cloud/pnpm-lock.yaml index 5e22a6427ca..40def6043a6 100644 --- a/cloud/pnpm-lock.yaml +++ b/cloud/pnpm-lock.yaml @@ -185,6 +185,31 @@ importers: specifier: ^5.0.5 version: 5.0.5(@types/node@24.19.2)(vite@8.3.4(@types/node@24.19.2)(esbuild@0.28.2)(tsx@4.23.15)) + apps/billing: + dependencies: + jose: + specifier: ^6.2.13 + version: 6.2.13 + zod: + specifier: ^4.6.7 + version: 4.6.7 + devDependencies: + '@cloudflare/workers-types': + specifier: ^5.20261103.1 + version: 5.20261104.1 + '@types/node': + specifier: ^24.19.2 + version: 24.19.2 + typescript: + specifier: ^5.9.3 + version: 5.9.3 + vitest: + specifier: ^5.0.5 + version: 5.0.5(@types/node@24.19.2)(vite@8.3.4(@types/node@24.19.2)(esbuild@0.28.2)(tsx@4.23.15)) + wrangler: + specifier: ^4.157.0 + version: 4.157.0(@cloudflare/workers-types@5.20261104.1)(@types/node@24.19.2) + apps/push: dependencies: '@hono/node-server': diff --git a/config/focus-setting-read-baseline.txt b/config/focus-setting-read-baseline.txt index 4b3f4c25346..c516a2d6722 100644 --- a/config/focus-setting-read-baseline.txt +++ b/config/focus-setting-read-baseline.txt @@ -100,8 +100,8 @@ 1 src/renderer/src/components/sidebar/SetupGuideSidebarEntry.tsx 1 src/renderer/src/components/sidebar/SetupScriptPromptCard.tsx 1 src/renderer/src/components/sidebar/SidebarAgentsList.tsx -1 src/renderer/src/components/sidebar/SidebarSettingsHelpMenu.tsx 1 src/renderer/src/components/sidebar/SidebarTaskNavButton.tsx +1 src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx 1 src/renderer/src/components/sidebar/linear-agent-skill-runtime.ts 1 src/renderer/src/components/sidebar/project-admission/drop/sidebar-project-drop.ts 2 src/renderer/src/components/sidebar/project-admission/drop/useSidebarProjectDrop.ts diff --git a/config/scripts/ci/unit-selection/check-test-file-runners.mjs b/config/scripts/ci/unit-selection/check-test-file-runners.mjs index 778d6bd6804..db9bfa6de79 100644 --- a/config/scripts/ci/unit-selection/check-test-file-runners.mjs +++ b/config/scripts/ci/unit-selection/check-test-file-runners.mjs @@ -64,6 +64,12 @@ const NON_UNIT_TEST_RUNNERS = [ sources: ['cloud/apps/sandbox-control/vitest.config.ts'], mirrors: ["include: ['test/**/*.test.ts', 'image/**/*.test.ts']"] }, + { + name: 'cloud billing', + include: ['cloud/apps/billing/test/**/*.test.ts'], + sources: ['cloud/apps/billing/vitest.config.ts'], + mirrors: ["include: ['test/**/*.test.ts']"] + }, ...[ 'cloud/apps/relay-fence-broker', 'cloud/packages/cloud-database', diff --git a/src/main/billing/billing-client.test.ts b/src/main/billing/billing-client.test.ts new file mode 100644 index 00000000000..1bf5c759c24 --- /dev/null +++ b/src/main/billing/billing-client.test.ts @@ -0,0 +1,206 @@ +import { describe, expect, it } from 'vitest' +import { OrcaCloudRequestError } from '../orca-profiles/profile-cloud-client' +import { + BillingServiceError, + assertStripeHostedUrl, + createCheckoutUrl, + createPortalUrl, + fetchEntitlement, + mintBillingToken +} from './billing-client' + +const endpoints = { + billingApiUrl: 'https://billing.example.test', + billingTokenEndpoint: 'https://login.example.test/v1/desktop/auth/billing-token' +} + +type Seen = { url: string; init: RequestInit | undefined } + +function fakeFetch(responses: Record, seen: Seen[] = []): typeof fetch { + return async (input, init) => { + const url = String(input) + seen.push({ url, init }) + const response = responses[url] + if (!response) { + throw new Error(`unexpected ${url}`) + } + return response + } +} + +describe('billing client', () => { + it('mints a billing token with the Orca access token and refuses redirects', async () => { + const seen: Seen[] = [] + const token = await mintBillingToken( + endpoints, + 'access-1', + fakeFetch( + { + [endpoints.billingTokenEndpoint]: Response.json({ + billingToken: 'bt', + expiresAt: 1 + }) + }, + seen + ) + ) + expect(token).toBe('bt') + expect(new Headers(seen[0]?.init?.headers).get('authorization')).toBe('Bearer access-1') + expect(seen[0]?.init?.redirect).toBe('error') + }) + + it('surfaces an auth rejection as an Orca session failure so the session refreshes', async () => { + const call = mintBillingToken( + endpoints, + 'stale', + fakeFetch({ + [endpoints.billingTokenEndpoint]: new Response(null, { status: 401 }) + }) + ) + await expect(call).rejects.toBeInstanceOf(OrcaCloudRequestError) + }) + + it('reports billing refusals as BillingServiceError, never as a session failure', async () => { + const call = createCheckoutUrl( + endpoints, + 'bt', + { plan: 'team', interval: 'month', seats: 1 }, + fakeFetch({ + 'https://billing.example.test/v1/billing/checkout': Response.json( + { error: 'already_subscribed' }, + { status: 409 } + ) + }) + ) + await expect(call).rejects.toMatchObject({ + statusCode: 409, + errorCode: 'already_subscribed' + }) + await expect(call).rejects.toBeInstanceOf(BillingServiceError) + }) + + it('sends plan, interval and seats and returns the hosted Checkout URL', async () => { + const seen: Seen[] = [] + const url = await createCheckoutUrl( + endpoints, + 'bt', + { plan: 'team', interval: 'year', seats: 3 }, + fakeFetch( + { + 'https://billing.example.test/v1/billing/checkout': Response.json({ + url: 'https://checkout.stripe.com/c/pay/cs_test_1' + }) + }, + seen + ) + ) + expect(url).toBe('https://checkout.stripe.com/c/pay/cs_test_1') + expect(JSON.parse(String(seen[0]?.init?.body))).toEqual({ + plan: 'team', + interval: 'year', + seats: 3 + }) + }) + + it('only opens Stripe-hosted pages', async () => { + expect(assertStripeHostedUrl('https://billing.stripe.com/p/session/x')).toBe( + 'https://billing.stripe.com/p/session/x' + ) + for (const bad of [ + 'http://checkout.stripe.com/x', + 'https://checkout.stripe.com.evil.test/x', + 'https://evil.test/x', + 'https://user@billing.stripe.com/x', + 'javascript:alert(1)', + 42 + ]) { + expect(() => assertStripeHostedUrl(bad)).toThrow() + } + const portal = createPortalUrl( + endpoints, + 'bt', + fakeFetch({ + 'https://billing.example.test/v1/billing/portal': Response.json({ + url: 'https://evil.test/' + }) + }) + ) + await expect(portal).rejects.toThrow('invalid_billing_url') + }) + + it('normalizes the entitlement and drops unknown or unsafe fields', async () => { + const offers = { + team: { + label: 'Team', + monthlyPriceLabel: '$50/seat/mo', + annualPriceLabel: '$480/seat/yr', + includedHoursPerSeat: 60, + concurrentSandboxesPerSeat: 3, + idleSleepMinutes: 15, + overageCentsPerHour: 75, + defaultSpendCapCents: 2500 + }, + enterprise: { + label: 'Enterprise', + priceLabel: 'Custom', + contactUrl: 'https://onorca.dev/sales' + } + } + const response = (extra: Record) => + fakeFetch({ + 'https://billing.example.test/v1/billing/entitlements': Response.json({ + plan: 'team', + planLabel: 'Team', + status: 'active', + interval: 'year', + seats: 2, + period: { start: 1, end: 2 }, + usageWindow: { start: 1, end: 2 }, + includedSeconds: 432000, + usedSeconds: 36000, + remainingIncludedSeconds: 396000, + usageWarningPercent: null, + overageSeconds: 0, + overage: { + optedIn: true, + active: true, + spendCapCents: 2500, + spentCents: 75, + centsPerHour: 75 + }, + canStartSandbox: true, + reason: 'something_new', + canManageBilling: true, + accountId: 'org:o1', + offers, + ...extra + }) + }) + const entitlement = await fetchEntitlement(endpoints, 'bt', response({})) + expect(entitlement).toMatchObject({ + plan: 'team', + interval: 'year', + usedSeconds: 36000, + reason: null, + canManageBilling: true, + overage: { optedIn: true, spentCents: 75 }, + offers + }) + expect(entitlement).not.toHaveProperty('accountId') + // A non-https contact link is never handed to the renderer. + const unsafe = await fetchEntitlement( + endpoints, + 'bt', + response({ + offers: { + ...offers, + enterprise: { ...offers.enterprise, contactUrl: 'javascript:alert(1)' } + } + }) + ) + expect(unsafe.offers).toBeNull() + expect( + (await fetchEntitlement(endpoints, 'bt', response({ plan: 'pro', offers: undefined }))).plan + ).toBeNull() + }) +}) diff --git a/src/main/billing/billing-client.ts b/src/main/billing/billing-client.ts new file mode 100644 index 00000000000..249be8a5e65 --- /dev/null +++ b/src/main/billing/billing-client.ts @@ -0,0 +1,251 @@ +// HTTP calls behind "Plan & billing": mint a short-lived billing token from Orca Cloud auth, +// then call the billing service with it. Pure (fetch injected) so it is testable without Electron. +import { z } from 'zod' +import type { + OrcaBillingCheckoutArgs, + OrcaBillingEntitlement, + OrcaBillingOverageArgs +} from '../../shared/orca-billing' +import { ORCA_BILLING_INTERVALS, ORCA_BILLING_PLAN_IDS } from '../../shared/orca-billing' +import { OrcaCloudRequestError } from '../orca-profiles/profile-cloud-client' + +const REQUEST_TIMEOUT_MS = 20_000 +const MAX_TOKEN_LENGTH = 8 * 1024 + +export type BillingEndpoints = { + billingApiUrl: string + billingTokenEndpoint: string +} + +/** A billing-service refusal (never an Orca session failure: it must not sign the user out). */ +export class BillingServiceError extends Error { + constructor( + public readonly statusCode: number, + public readonly errorCode: string | null + ) { + super(errorCode ?? `billing_request_failed_${statusCode}`) + this.name = 'BillingServiceError' + } +} + +const ErrorBody = z.object({ error: z.string().max(64) }) +const TokenBody = z.object({ + billingToken: z.string().min(1).max(MAX_TOKEN_LENGTH) +}) +const UrlBody = z.object({ url: z.string() }) +const JsonObject = z.record(z.string(), z.unknown()) + +async function readErrorCode(response: Response): Promise { + const parsed = ErrorBody.safeParse(await response.json().catch(() => null)) + return parsed.success ? parsed.data.error : null +} + +/** POST to Orca Cloud auth. A 401/403 is surfaced as OrcaCloudRequestError so the caller's + * session-refresh wrapper refreshes the Orca session once and retries. */ +export async function mintBillingToken( + endpoints: BillingEndpoints, + accessToken: string, + fetchImpl: typeof fetch = fetch +): Promise { + const response = await fetchImpl(endpoints.billingTokenEndpoint, { + method: 'POST', + headers: { + 'content-type': 'application/json', + authorization: `Bearer ${accessToken}` + }, + body: '{}', + redirect: 'error', + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) + }) + if (!response.ok) { + throw new OrcaCloudRequestError(response.status, (await readErrorCode(response)) ?? undefined) + } + const body = TokenBody.safeParse(await response.json()) + if (!body.success) { + throw new Error('invalid_billing_token') + } + return body.data.billingToken +} + +async function billingRequest( + endpoints: BillingEndpoints, + token: string, + method: 'GET' | 'POST', + path: string, + body: unknown, + fetchImpl: typeof fetch +): Promise> { + const response = await fetchImpl(new URL(path, `${endpoints.billingApiUrl}/`).toString(), { + method, + headers: { + authorization: `Bearer ${token}`, + ...(method === 'POST' ? { 'content-type': 'application/json' } : {}) + }, + body: method === 'POST' ? JSON.stringify(body ?? {}) : undefined, + redirect: 'error', + signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) + }) + if (!response.ok) { + throw new BillingServiceError(response.status, await readErrorCode(response)) + } + const json = JsonObject.safeParse(await response.json()) + if (!json.success) { + throw new Error('invalid_billing_response') + } + return json.data +} + +const seconds = z.number().catch(0) +const window = z.object({ start: z.number(), end: z.number() }).nullable().catch(null) + +const OffersSchema = z + .object({ + team: z.object({ + label: z.string().max(40), + monthlyPriceLabel: z.string().max(40), + annualPriceLabel: z.string().max(40), + monthlyPriceCentsPerSeat: z.number().int().nonnegative().nullable().catch(null), + annualPriceCentsPerSeat: z.number().int().nonnegative().nullable().catch(null), + largeMachineWeight: z.number().positive().catch(2), + includedHoursPerSeat: z.number().nonnegative(), + concurrentSandboxesPerSeat: z.number().int().nonnegative(), + idleSleepMinutes: z.number().int().positive(), + overageCentsPerHour: z.number().int().positive(), + defaultSpendCapCents: z.number().int().nonnegative() + }), + enterprise: z.object({ + label: z.string().max(40), + priceLabel: z.string().max(40), + // Opened in the browser: https only. + contactUrl: z.string().url().startsWith('https://') + }) + }) + .nullable() + .catch(null) + +// Lenient by design: an unknown plan/reason or a missing field degrades to "none"/0, never an error, +// so a newer billing service cannot break an older desktop. +const EntitlementSchema = z.object({ + plan: z.enum(ORCA_BILLING_PLAN_IDS).nullable().catch(null), + planLabel: z.string().nullable().catch(null), + status: z.string().catch('none'), + interval: z.enum(ORCA_BILLING_INTERVALS).nullable().catch(null), + seats: seconds, + period: window, + usageWindow: window, + cancelAtPeriodEnd: z.boolean().catch(false), + includedSeconds: seconds, + usedSeconds: seconds, + remainingIncludedSeconds: seconds, + usageWarningPercent: z.number().nullable().catch(null), + overageSeconds: seconds, + overage: z + .object({ + optedIn: z.boolean().catch(false), + active: z.boolean().catch(false), + spendCapCents: seconds, + spentCents: seconds, + centsPerHour: seconds + }) + .catch({ optedIn: false, active: false, spendCapCents: 0, spentCents: 0, centsPerHour: 0 }), + payment: z + .object({ + pastDueSince: z.number().nullable().catch(null), + graceEndsAt: z.number().nullable().catch(null), + suspendRunning: z.boolean().catch(false) + }) + .catch({ pastDueSince: null, graceEndsAt: null, suspendRunning: false }), + canStartSandbox: z.boolean().catch(false), + reason: z + .enum([ + 'no_subscription', + 'subscription_inactive', + 'payment_past_due', + 'payment_grace_expired', + 'included_hours_exhausted', + 'overage_cap_reached' + ]) + .nullable() + .catch(null), + canManageBilling: z.boolean().catch(false), + offers: OffersSchema.optional().transform((value) => value ?? null) +}) + +export function normalizeEntitlement(value: Record): OrcaBillingEntitlement { + return EntitlementSchema.parse(value) +} + +// Only Stripe's hosted pages may be opened from a billing response. +const STRIPE_HOSTS = new Set(['checkout.stripe.com', 'billing.stripe.com']) + +export function assertStripeHostedUrl(value: unknown): string { + if (typeof value !== 'string' || !URL.canParse(value)) { + throw new Error('invalid_billing_url') + } + const url = new URL(value) + if ( + url.protocol !== 'https:' || + !STRIPE_HOSTS.has(url.hostname) || + url.username || + url.password + ) { + throw new Error('invalid_billing_url') + } + return url.toString() +} + +export async function fetchEntitlement( + endpoints: BillingEndpoints, + token: string, + fetchImpl: typeof fetch = fetch +): Promise { + return normalizeEntitlement( + await billingRequest(endpoints, token, 'GET', 'v1/billing/entitlements', undefined, fetchImpl) + ) +} + +export async function createCheckoutUrl( + endpoints: BillingEndpoints, + token: string, + args: OrcaBillingCheckoutArgs, + fetchImpl: typeof fetch = fetch +): Promise { + const body = await billingRequest( + endpoints, + token, + 'POST', + 'v1/billing/checkout', + { plan: args.plan, interval: args.interval, seats: args.seats }, + fetchImpl + ) + return assertStripeHostedUrl(UrlBody.safeParse(body).data?.url) +} + +export async function createPortalUrl( + endpoints: BillingEndpoints, + token: string, + fetchImpl: typeof fetch = fetch +): Promise { + const body = await billingRequest(endpoints, token, 'POST', 'v1/billing/portal', {}, fetchImpl) + return assertStripeHostedUrl(UrlBody.safeParse(body).data?.url) +} + +export async function updateOverage( + endpoints: BillingEndpoints, + token: string, + args: OrcaBillingOverageArgs, + fetchImpl: typeof fetch = fetch +): Promise { + return normalizeEntitlement( + await billingRequest( + endpoints, + token, + 'POST', + 'v1/billing/overage', + args.spendCapCents === undefined + ? { enabled: args.enabled } + : { enabled: args.enabled, spendCapCents: args.spendCapCents }, + fetchImpl + ) + ) +} diff --git a/src/main/billing/billing-config.test.ts b/src/main/billing/billing-config.test.ts new file mode 100644 index 00000000000..108572d2ec7 --- /dev/null +++ b/src/main/billing/billing-config.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it, vi } from 'vitest' + +vi.mock('electron', () => ({ app: { isPackaged: false } })) + +import { getBillingEndpoints } from './billing-config' +import { + billingCheckoutArgsFromUnknown, + billingOverageArgsFromUnknown +} from '../ipc/profiles/orca-profile-billing-handlers' + +const authConfig = { apiBaseUrl: 'https://login.onorca.dev' } + +describe('billing config', () => { + it('is unconfigured until a billing URL is set', () => { + expect(getBillingEndpoints(authConfig, {}, true)).toBeNull() + }) + + it('derives the token endpoint from Orca Cloud auth', () => { + expect( + getBillingEndpoints(authConfig, { ORCA_BILLING_API_URL: 'https://billing.onorca.dev' }, true) + ).toEqual({ + billingApiUrl: 'https://billing.onorca.dev', + billingTokenEndpoint: 'https://login.onorca.dev/v1/desktop/auth/billing-token' + }) + }) + + it('allows loopback http only in development builds', () => { + const env = { ORCA_BILLING_API_URL: 'http://127.0.0.1:8790' } + expect(getBillingEndpoints(authConfig, env, false)?.billingApiUrl).toBe('http://127.0.0.1:8790') + expect(getBillingEndpoints(authConfig, env, true)).toBeNull() + }) +}) + +describe('billing IPC args', () => { + it('accepts Team checkout with an interval and 1..500 seats', () => { + expect(billingCheckoutArgsFromUnknown({ plan: 'team', interval: 'year', seats: 5 })).toEqual({ + plan: 'team', + interval: 'year', + seats: 5 + }) + for (const bad of [ + { plan: 'pro', interval: 'month', seats: 1 }, + { plan: 'team', interval: 'week', seats: 1 }, + { plan: 'team', interval: 'month', seats: 0 }, + { plan: 'team', interval: 'month', seats: 501 }, + { plan: 'team', interval: 'month', seats: 1, extra: true }, + null + ]) { + expect(() => billingCheckoutArgsFromUnknown(bad)).toThrow('invalid_orca_billing_checkout') + } + }) + + it('accepts overage toggles with an optional whole-cent cap', () => { + expect(billingOverageArgsFromUnknown({ enabled: false })).toEqual({ enabled: false }) + expect(billingOverageArgsFromUnknown({ enabled: true, spendCapCents: 5000 })).toEqual({ + enabled: true, + spendCapCents: 5000 + }) + for (const bad of [ + { enabled: 'yes' }, + { enabled: true, spendCapCents: -1 }, + { enabled: true, spendCapCents: 1.5 } + ]) { + expect(() => billingOverageArgsFromUnknown(bad)).toThrow('invalid_orca_billing_overage') + } + }) +}) diff --git a/src/main/billing/billing-config.ts b/src/main/billing/billing-config.ts new file mode 100644 index 00000000000..c2ce29d02d2 --- /dev/null +++ b/src/main/billing/billing-config.ts @@ -0,0 +1,23 @@ +import { cleanCloudServiceUrl } from '../../shared/cloud-service-url' +import type { OrcaCloudAuthConfig } from '../orca-profiles/profile-cloud-auth-config' +import type { BillingEndpoints } from './billing-client' + +/** + * Billing is opt-in per build until the service is deployed: no production default yet, so + * packaged builds report 'unconfigured' and the menu item stays hidden behind its capability flag. + */ +export function getBillingEndpoints( + authConfig: Pick, + env: NodeJS.ProcessEnv, + packaged: boolean +): BillingEndpoints | null { + const allowLoopbackHttp = !packaged + const billingApiUrl = cleanCloudServiceUrl(env.ORCA_BILLING_API_URL, allowLoopbackHttp) + if (!billingApiUrl) { + return null + } + const billingTokenEndpoint = + cleanCloudServiceUrl(env.ORCA_CLOUD_BILLING_TOKEN_URL, allowLoopbackHttp) ?? + new URL('/v1/desktop/auth/billing-token', `${authConfig.apiBaseUrl}/`).toString() + return { billingApiUrl, billingTokenEndpoint } +} diff --git a/src/main/billing/billing-service.ts b/src/main/billing/billing-service.ts new file mode 100644 index 00000000000..aac2bf8bbe4 --- /dev/null +++ b/src/main/billing/billing-service.ts @@ -0,0 +1,149 @@ +// Plan & billing for the active Orca profile's signed-in account. Results are values, never +// throws, so the renderer can show a precise state. A billing-service refusal never signs the +// user out; only Orca Cloud auth rejecting the session does (via runWithFreshOrcaCloudSession). +import { app, shell } from 'electron' +import type { + OrcaBillingCheckoutArgs, + OrcaBillingOpenResult, + OrcaBillingOverageArgs, + OrcaBillingOverageResult, + OrcaBillingStatusResult +} from '../../shared/orca-billing' +import { getOrcaCloudAuthConfig } from '../orca-profiles/profile-cloud-auth-config' +import { runWithFreshOrcaCloudSession } from '../orca-profiles/profile-cloud-session-refresh' +import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store' +import { + BillingServiceError, + createCheckoutUrl, + createPortalUrl, + fetchEntitlement, + mintBillingToken, + updateOverage, + type BillingEndpoints +} from './billing-client' +import { getBillingEndpoints } from './billing-config' + +type Ready = + | { + ok: true + run: ( + call: (token: string, endpoints: BillingEndpoints) => Promise + ) => Promise + } + | { ok: false; status: 'unconfigured' | 'signed-out' } + +function prepare(userDataPath: string): Ready { + const auth = getOrcaCloudAuthConfig() + if (!auth.configured) { + return { ok: false, status: 'unconfigured' } + } + const endpoints = getBillingEndpoints(auth.config, process.env, app.isPackaged) + if (!endpoints) { + return { ok: false, status: 'unconfigured' } + } + const active = ensureActiveOrcaProfile(userDataPath) + if (!active.profile.cloud) { + return { ok: false, status: 'signed-out' } + } + return { + ok: true, + run: async (call) => { + const result = await runWithFreshOrcaCloudSession( + auth.config, + active, + userDataPath, + async (session) => call(await mintBillingToken(endpoints, session.accessToken), endpoints) + ) + return result.status === 'ok' ? result.value : 'signed-out' + } + } +} + +const message = (error: unknown): string => (error instanceof Error ? error.message : String(error)) + +export async function getOrcaBillingStatus(userDataPath: string): Promise { + const ready = prepare(userDataPath) + if (!ready.ok) { + return { status: ready.status } + } + try { + const entitlement = await ready.run((token, endpoints) => fetchEntitlement(endpoints, token)) + return entitlement === 'signed-out' ? { status: 'signed-out' } : { status: 'ok', entitlement } + } catch (error) { + return { status: 'failed', error: message(error) } + } +} + +function openResultForError(error: unknown): OrcaBillingOpenResult { + if (error instanceof BillingServiceError) { + if (error.statusCode === 403) { + return { status: 'forbidden' } + } + if (error.errorCode === 'already_subscribed') { + return { status: 'already-subscribed' } + } + if (error.errorCode === 'no_billing_account') { + return { status: 'no-billing-account' } + } + } + return { status: 'failed', error: message(error) } +} + +async function openHosted( + userDataPath: string, + createUrl: (token: string, endpoints: BillingEndpoints) => Promise +): Promise { + const ready = prepare(userDataPath) + if (!ready.ok) { + return { status: ready.status } + } + try { + const url = await ready.run(createUrl) + if (url === 'signed-out') { + return { status: 'signed-out' } + } + // createUrl already restricted this to Stripe's hosted Checkout/Portal hosts. + await shell.openExternal(url) + return { status: 'opened' } + } catch (error) { + return openResultForError(error) + } +} + +export function openOrcaBillingCheckout( + userDataPath: string, + args: OrcaBillingCheckoutArgs +): Promise { + return openHosted(userDataPath, (token, endpoints) => createCheckoutUrl(endpoints, token, args)) +} + +export function openOrcaBillingPortal(userDataPath: string): Promise { + return openHosted(userDataPath, (token, endpoints) => createPortalUrl(endpoints, token)) +} + +export async function setOrcaBillingOverage( + userDataPath: string, + args: OrcaBillingOverageArgs +): Promise { + const ready = prepare(userDataPath) + if (!ready.ok) { + return { status: ready.status } + } + try { + const entitlement = await ready.run((token, endpoints) => updateOverage(endpoints, token, args)) + return entitlement === 'signed-out' ? { status: 'signed-out' } : { status: 'ok', entitlement } + } catch (error) { + if (error instanceof BillingServiceError) { + if (error.statusCode === 403) { + return { status: 'forbidden' } + } + if (error.errorCode === 'no_subscription') { + return { status: 'no-subscription' } + } + if (error.errorCode === 'spend_cap_too_high') { + return { status: 'cap-too-high' } + } + } + return { status: 'failed', error: message(error) } + } +} diff --git a/src/main/ephemeral-vm/workspace-resume.ts b/src/main/ephemeral-vm/workspace-resume.ts index 4572fe00cf5..c27ee466741 100644 --- a/src/main/ephemeral-vm/workspace-resume.ts +++ b/src/main/ephemeral-vm/workspace-resume.ts @@ -9,6 +9,7 @@ import { } from '../../shared/ephemeral-vm-recipes' import { resolveEnvironment } from '../../shared/remote-runtime/host-store/runtime-environment-store' import { resumeEphemeralVmRuntime } from './runtime-service' +import { syncHostedSessions } from '../hosted-sandbox/hosted-session-sync' import { connectRuntimeOwnedSshTarget, refreshRuntimeOwnedSshTarget } from './runtime-ssh' import { noteRecipeVmWakePairing } from './recipe-vm-wake-pairings' import { applyResumedRuntimePairing } from './runtime-resume-pairing' @@ -149,6 +150,7 @@ export async function resumeEphemeralVmWorkspace( throw error } } + syncHostedSessions() return result.runtime } diff --git a/src/main/hosted-sandbox/hosted-sandbox-config.ts b/src/main/hosted-sandbox/hosted-sandbox-config.ts new file mode 100644 index 00000000000..3bd965dc481 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-config.ts @@ -0,0 +1,147 @@ +// Where Orca Cloud sandboxes are managed and how the desktop authenticates there. Off by default: +// no packaged control-plane URL ships yet, so only builds that set ORCA_CLOUD_SANDBOX_URL see it. +import { createPrivateKey, sign } from 'node:crypto' +import { readFile } from 'node:fs/promises' +import { z } from 'zod' +import { cleanCloudServiceUrl } from '../../shared/cloud-service-url' +import { getOrcaCloudAuthConfig } from '../orca-profiles/profile-cloud-auth-config' +import { OrcaCloudRequestError } from '../orca-profiles/profile-cloud-client' +import { runWithFreshOrcaCloudSession } from '../orca-profiles/profile-cloud-session-refresh' +import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store' +import { EnvironmentProviderError } from '../environment-providers/environment-provider-driver' + +export type HostedSandboxConfig = { + controlPlaneUrl: string + getAccessToken: (signal?: AbortSignal) => Promise +} + +const SandboxTokenBody = z.object({ + sandboxToken: z.string().min(1).max(8192), + expiresAt: z.number().int().positive() +}) + +const DevKeyFile = z.object({ + private: z.looseObject({ + kty: z.literal('EC'), + crv: z.literal('P-256'), + x: z.string(), + y: z.string(), + d: z.string(), + kid: z.string().optional() + }) +}) + +/** Sandbox tokens are minted per call by Orca Cloud auth; cached until a minute before expiry. */ +export function getHostedSandboxConfig( + userDataPath: string, + env: NodeJS.ProcessEnv = process.env, + packaged = false, + fetchImpl: typeof fetch = fetch +): HostedSandboxConfig | null { + const controlPlaneUrl = cleanCloudServiceUrl(env.ORCA_CLOUD_SANDBOX_URL, !packaged) + if (!controlPlaneUrl) { + return null + } + const devKeyFile = packaged ? undefined : env.ORCA_CLOUD_SANDBOX_DEV_KEY_FILE?.trim() + let cached: { token: string; expiresAt: number } | null = null + return { + controlPlaneUrl, + getAccessToken: async () => { + if (cached && cached.expiresAt - Date.now() > 60_000) { + return cached.token + } + cached = devKeyFile + ? await mintDevSandboxToken( + devKeyFile, + ensureActiveOrcaProfile(userDataPath).profile.cloud?.userId + ) + : await mintSandboxToken(userDataPath, env, packaged, fetchImpl) + return cached.token + } + } +} + +async function mintSandboxToken( + userDataPath: string, + env: NodeJS.ProcessEnv, + packaged: boolean, + fetchImpl: typeof fetch +): Promise<{ token: string; expiresAt: number }> { + const auth = getOrcaCloudAuthConfig(env, packaged) + const active = ensureActiveOrcaProfile(userDataPath) + if (!auth.configured || !active.profile.cloud) { + throw new EnvironmentProviderError('unauthorized', 'Sign in to Orca to use Orca Cloud.') + } + const endpoint = + cleanCloudServiceUrl(env.ORCA_CLOUD_SANDBOX_TOKEN_URL, !packaged) ?? + new URL('/v1/desktop/auth/sandbox-token', `${auth.config.apiBaseUrl}/`).toString() + const result = await runWithFreshOrcaCloudSession( + auth.config, + active, + userDataPath, + async (session) => { + const response = await fetchImpl(endpoint, { + method: 'POST', + headers: { + 'content-type': 'application/json', + authorization: `Bearer ${session.accessToken}` + }, + body: '{}', + redirect: 'error', + signal: AbortSignal.timeout(15_000) + }) + if (response.status === 401 || response.status === 403) { + throw new OrcaCloudRequestError(response.status) + } + if (!response.ok) { + throw new EnvironmentProviderError( + 'unavailable', + `Orca Cloud auth answered ${response.status}.` + ) + } + const body = SandboxTokenBody.safeParse(await response.json()) + if (!body.success) { + throw new EnvironmentProviderError( + 'invalid_response', + 'Orca Cloud auth sent an invalid sandbox token.' + ) + } + return { token: body.data.sandboxToken, expiresAt: body.data.expiresAt } + } + ) + if (result.status !== 'ok') { + throw new EnvironmentProviderError('unauthorized', 'Sign in to Orca to use Orca Cloud.') + } + return result.value +} + +/** Dev builds only: sign with the dev operator key the dev control plane trusts (issuer orca-sandbox-dev-test). */ +async function mintDevSandboxToken( + keyFile: string, + userId: string | undefined +): Promise<{ token: string; expiresAt: number }> { + const parsed = DevKeyFile.parse(JSON.parse(await readFile(keyFile, 'utf8'))) + const key = createPrivateKey({ key: parsed.private, format: 'jwk' }) + const now = Math.floor(Date.now() / 1000) + const encode = (value: unknown) => Buffer.from(JSON.stringify(value)).toString('base64url') + const header = encode({ + alg: 'ES256', + kid: parsed.private.kid ?? 'dev1' + }) + const payload = encode({ + sub: userId ?? 'desktop-dogfood', + purpose: 'sandbox', + iss: 'orca-sandbox-dev-test', + aud: 'orca-sandbox', + iat: now, + exp: now + 300 + }) + const signature = sign('sha256', Buffer.from(`${header}.${payload}`), { + key, + dsaEncoding: 'ieee-p1363' + }) + return { + token: `${header}.${payload}.${signature.toString('base64url')}`, + expiresAt: (now + 300) * 1000 + } +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-install.ts b/src/main/hosted-sandbox/hosted-sandbox-install.ts new file mode 100644 index 00000000000..a9fb2755506 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-install.ts @@ -0,0 +1,68 @@ +// Wires Orca Cloud into the desktop when this build has a control plane configured (off by default). +import { app, BrowserWindow } from 'electron' +import type { OrcaVmRecipe } from '../../shared/orca-yaml-hook-types' +import { getApprovedPluginVmRecipes } from '../plugins/plugin-approved-vm-recipes' +import type { PluginService } from '../plugins/plugin-service' +import { getHostedSandboxConfig } from './hosted-sandbox-config' +import { + ORCA_CLOUD_RECIPE, + createHostedSandboxRecipeDriver, + registerHostedSandboxRecipe +} from './hosted-sandbox-recipe' +import { createHostedSessionRegistry, type HostedSessionRegistry } from './hosted-session-registry' +import { setHostedSessionSync } from './hosted-session-sync' + +let installed: { registry: HostedSessionRegistry } | null = null + +export function installHostedSandbox(): void { + if (installed) { + return + } + const userDataPath = app.getPath('userData') + const config = getHostedSandboxConfig(userDataPath, process.env, app.isPackaged) + if (!config) { + return + } + const base = createHostedSandboxRecipeDriver(config) + // Dev rehearsals shorten idle sleep; packaged builds always use the provider's default. + const idleOverride = app.isPackaged + ? Number.NaN + : Number(process.env.ORCA_CLOUD_SANDBOX_IDLE_STOP_MS) + const driver = Number.isFinite(idleOverride) + ? { ...base, capabilities: { ...base.capabilities, idleStopMs: idleOverride } } + : base + const registry = createHostedSessionRegistry({ + userDataPath, + driver, + windows: () => BrowserWindow.getAllWindows() + }) + // Records land after the step answers and wakes can come from any path, so reconcile on a slow beat too. + let beat: ReturnType | null = null + const schedule = (ms: number): void => { + beat = setTimeout(() => { + void registry.sync().finally(() => schedule(30_000)) + }, ms) + beat.unref?.() + } + registerHostedSandboxRecipe(driver, { + onReady: () => setTimeout(() => void registry.sync(), 3_000).unref?.(), + onStopped: (environmentId) => registry.stopEnvironment(environmentId) + }) + installed = { registry } + setHostedSessionSync(() => void registry.sync()) + schedule(0) + app.once('will-quit', () => { + if (beat) { + clearTimeout(beat) + } + registry.dispose() + }) +} + +/** Built-in providers (Orca Cloud) ride along with approved plugin recipes and lead every list. */ +export async function withProviderRecipes(pluginService?: PluginService): Promise { + return [ + ...(installed ? [ORCA_CLOUD_RECIPE] : []), + ...(await getApprovedPluginVmRecipes(pluginService)) + ] +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-recipe.test.ts b/src/main/hosted-sandbox/hosted-sandbox-recipe.test.ts new file mode 100644 index 00000000000..df48338a287 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-recipe.test.ts @@ -0,0 +1,73 @@ +import { mkdtempSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { parseEphemeralVmRecipeResult } from '../../shared/ephemeral-vm-recipes' +import { buildEphemeralVmRecipeLifecyclePayload } from '../../shared/ephemeral-vm-recipe-lifecycle-payload' +import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' +import { createHostedSandboxDriver } from './hosted-sandbox-driver' +import { ORCA_CLOUD_RECIPE, createHostedSandboxStepProvider } from './hosted-sandbox-recipe' +import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support' + +function setup() { + const plane = createFakeHostedControlPlane() + const driver = createHostedSandboxDriver( + createHostedSandboxControlPlaneClient({ + baseUrl: 'https://sandbox.cloud.test', + getAccessToken: async () => plane.token, + fetch: plane.fetch + }) + ) + return { plane, step: createHostedSandboxStepProvider(driver) } +} + +const repoPath = mkdtempSync(join(tmpdir(), 'orca-cloud-recipe-')) +const context = { recipeId: 'cloud', repoPath, instanceId: 'orca-inst-1' } + +describe('Orca Cloud built-in recipe steps', () => { + it('answers create and resume with recipe results the runtime pipeline accepts', async () => { + const { plane, step } = setup() + const created = await step({ mode: 'create', repoPath, context }) + expect(created.exitCode).toBe(0) + const parsed = parseEphemeralVmRecipeResult(created.stdout) + expect(parsed.ok).toBe(true) + expect([...plane.sandboxes.values()].map((s) => s.workspaceId)).toEqual(['orca-inst-1']) + if (!parsed.ok) { + return + } + const payload = (mode: 'suspend' | 'resume' | 'destroy') => + JSON.stringify( + buildEphemeralVmRecipeLifecyclePayload({ + mode, + recipe: ORCA_CLOUD_RECIPE, + context, + recipeResult: parsed.result + }) + ) + expect( + (await step({ mode: 'suspend', repoPath, context, stdin: payload('suspend') })).exitCode + ).toBe(0) + const resumed = await step({ mode: 'resume', repoPath, context, stdin: payload('resume') }) + expect(parseEphemeralVmRecipeResult(resumed.stdout).ok).toBe(true) + expect( + (await step({ mode: 'destroy', repoPath, context, stdin: payload('destroy') })).exitCode + ).toBe(0) + expect([...plane.sandboxes.values()].map((s) => s.status)).toEqual(['destroyed']) + }) + + it('turns a billing refusal into the plan upsell failure', async () => { + const { plane, step } = setup() + plane.denyNextStart('no_subscription') + const result = await step({ mode: 'create', repoPath, context }) + expect(result.exitCode).toBe(1) + expect(result.failure?.errorCode).toBe('cloudPlanRequired') + }) + + it('tells the user to free a slot, not to buy a plan, when the member is at their cap', async () => { + const { plane, step } = setup() + plane.denyNextStart('member_concurrency_limit') + expect((await step({ mode: 'create', repoPath, context })).failure?.errorCode).toBe( + 'cloudConcurrencyLimit' + ) + }) +}) diff --git a/src/main/hosted-sandbox/hosted-sandbox-recipe.ts b/src/main/hosted-sandbox/hosted-sandbox-recipe.ts new file mode 100644 index 00000000000..de1508ace00 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-sandbox-recipe.ts @@ -0,0 +1,159 @@ +// "Orca Cloud" as a built-in per-workspace environment: listed first wherever recipes are, run +// in-process through the HostedSandboxDriver, and answering like a recipe process would. +import { z } from 'zod' +import { EphemeralVmRecipeResultSchema } from '../../shared/ephemeral-vm-recipes' +import { ephemeralVmFailure } from '../../shared/ephemeral-vm-failure-copy' +import { normalizeGitRemoteUrl } from '../../shared/git/remotes/git-remote-identity' +import type { OrcaVmRecipe } from '../../shared/orca-yaml-hook-types' +import { + registerEphemeralVmRecipeStepProvider, + type RecipeStepProvider, + type RecipeStepProviderResult +} from '../../shared/ephemeral-vm-recipe-step-providers' +import { getRemoteUrl } from '../git/repo' +import { + isEnvironmentProviderError, + type EnvironmentProviderDriver, + type EnvironmentReady +} from '../environment-providers/environment-provider-driver' +import { + environmentHandleFromRecipeResult, + environmentReadyToRecipeResult +} from '../environment-providers/environment-provider-recipe-adapter' +import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' +import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' +import type { HostedSandboxConfig } from './hosted-sandbox-config' + +/** Reserved id: `--on cloud` in the CLI design, first in every provider list. */ +export const ORCA_CLOUD_RECIPE_ID = 'cloud' + +export const ORCA_CLOUD_RECIPE: OrcaVmRecipe = { + id: ORCA_CLOUD_RECIPE_ID, + name: 'Orca Cloud', + description: 'A cloud VM run by Orca. Sleeps when idle; files persist.', + provider: 'orca-cloud', + create: 'orca-cloud create', + suspend: 'orca-cloud suspend', + resume: 'orca-cloud resume', + destroy: 'orca-cloud destroy', + timeoutSeconds: { create: 300, resume: 180, suspend: 420, destroy: 120 } +} + +const LifecyclePayload = z.looseObject({ recipeResult: EphemeralVmRecipeResultSchema.optional() }) + +export function createHostedSandboxRecipeDriver( + config: HostedSandboxConfig +): EnvironmentProviderDriver { + return createHostedSandboxDriver( + createHostedSandboxControlPlaneClient({ + baseUrl: config.controlPlaneUrl, + getAccessToken: config.getAccessToken + }) + ) +} + +/** https clone URL the control plane can mint a GitHub App token for; null for non-GitHub remotes. */ +export async function hostedSandboxRepoUrl(repoPath: string): Promise { + const remote = await getRemoteUrl(repoPath) + const normalized = remote ? normalizeGitRemoteUrl(remote) : null + return normalized?.startsWith('github.com/') ? `https://${normalized}.git` : null +} + +function ok(stdout: string): RecipeStepProviderResult { + return { stdout, stderr: '', exitCode: 0, signal: null } +} + +function failed(error: unknown): RecipeStepProviderResult { + const base = { stdout: '', exitCode: 1, signal: null } + if ( + isEnvironmentProviderError(error, 'entitlement_denied') && + (error.reason === 'concurrency_limit' || error.reason === 'member_concurrency_limit') + ) { + return { + ...base, + stderr: `Orca Cloud: ${error.reason}\n`, + failure: ephemeralVmFailure('cloudConcurrencyLimit') + } + } + if (isEnvironmentProviderError(error, 'entitlement_denied')) { + return { + ...base, + stderr: `Orca Cloud: ${error.reason ?? 'no plan'}\n`, + failure: ephemeralVmFailure('cloudPlanRequired') + } + } + if (isEnvironmentProviderError(error, 'unauthorized')) { + return { ...base, stderr: '', failure: ephemeralVmFailure('cloudSignInRequired') } + } + if ( + isEnvironmentProviderError(error, 'unavailable') || + isEnvironmentProviderError(error, 'timeout') + ) { + return { + ...base, + stderr: `${error.message}\n`, + failure: ephemeralVmFailure('cloudUnavailable') + } + } + return { ...base, stderr: `${error instanceof Error ? error.message : String(error)}\n` } +} + +export function createHostedSandboxStepProvider( + driver: EnvironmentProviderDriver, + hooks: { + onReady?: (ready: EnvironmentReady) => void + onStopped?: (environmentId: string) => void + } = {} +): RecipeStepProvider { + return async ({ mode, repoPath, context, stdin, signal, onStderr }) => { + const ctx = { + signal, + onProgress: ({ phase }: { phase: string }) => onStderr?.(`[orca-cloud] ${phase}\n`) + } + try { + if (mode === 'create') { + const repoUrl = await hostedSandboxRepoUrl(repoPath) + const ready = await driver.create( + { + // One create attempt = one sandbox: a retried attempt reuses the instance id. + workspaceId: context.instanceId ?? context.workspaceId ?? 'workspace', + machineSize: 'standard', + ...(repoUrl + ? { repo: { url: repoUrl, ...(context.branch ? { ref: context.branch } : {}) } } + : {}) + }, + ctx + ) + hooks.onReady?.(ready) + return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree'))) + } + const payload = LifecyclePayload.parse(JSON.parse(stdin ?? '{}')) + const handle = payload.recipeResult + ? environmentHandleFromRecipeResult(payload.recipeResult, HOSTED_SANDBOX_DRIVER_ID) + : null + if (!handle) { + return { stdout: '', stderr: 'Not an Orca Cloud environment.\n', exitCode: 1, signal: null } + } + if (mode === 'resume') { + const ready = await driver.resume(handle, ctx) + hooks.onReady?.(ready) + return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree'))) + } + await (mode === 'suspend' ? driver.suspend(handle, ctx) : driver.destroy(handle, ctx)) + hooks.onStopped?.(handle.environmentId) + return ok('') + } catch (error) { + return failed(error) + } + } +} + +export function registerHostedSandboxRecipe( + driver: EnvironmentProviderDriver, + hooks: Parameters[1] = {} +): () => void { + return registerEphemeralVmRecipeStepProvider( + 'orca-cloud', + createHostedSandboxStepProvider(driver, hooks) + ) +} diff --git a/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts index 083e6ca60b6..caf06448193 100644 --- a/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts +++ b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts @@ -25,6 +25,8 @@ export type HostedSessionLeasePorts = { reconnect: (ready: EnvironmentReady) => Promise resumeAgents: (drainStartedAt: number) => Promise onSuspended: () => void + /** Replaces the driver suspend, e.g. to run the window's ordinary Sleep for the workspace. */ + suspend?: () => Promise log?: (line: string) => void now?: () => number } @@ -98,7 +100,9 @@ export class HostedSessionLeaseScheduler { this.ports.warn(action.minutesLeft) break case 'idle-suspend': - await this.ports.driver.suspend(this.ports.handle) + await (this.ports.suspend + ? this.ports.suspend() + : this.ports.driver.suspend(this.ports.handle)) this.stop() this.ports.onSuspended() break diff --git a/src/main/hosted-sandbox/hosted-session-registry.ts b/src/main/hosted-sandbox/hosted-session-registry.ts new file mode 100644 index 00000000000..fb0153f2731 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-session-registry.ts @@ -0,0 +1,171 @@ +// One lease scheduler per running Orca Cloud workspace: renews Orca's lease, warns before the 24h +// cap, asks the window to sleep an idle workspace, and rolls the session over with agent auto-resume. +import type { BrowserWindow } from 'electron' +import { z } from 'zod' +import { + listEphemeralVmRuntimes, + updateEphemeralVmRuntimeStatus +} from '../../shared/ephemeral-vm-runtime-store' +import type { EphemeralVmRuntimeRecord } from '../../shared/ephemeral-vm-runtimes' +import { + ORCAD_WORK_VERDICT_METHOD, + type HostedSessionWarningEvent +} from '../../shared/hosted-sandbox/hosted-session-events' +import { callRuntimeEnvironment } from '../ipc/runtime-environments/runtime-environment-transport-routing' +import { invalidateRuntimeEnvironmentTransport } from '../ipc/runtime-environments/runtime-environments' +import { applyResumedRuntimePairing } from '../ephemeral-vm/runtime-resume-pairing' +import { noteRecipeVmWakePairing } from '../ephemeral-vm/recipe-vm-wake-pairings' +import type { EnvironmentProviderDriver } from '../environment-providers/environment-provider-driver' +import { + environmentHandleFromRecipeResult, + environmentReadyToRecipeResult +} from '../environment-providers/environment-provider-recipe-adapter' +import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume' +import { HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' +import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call' +import type { HostedActivitySample } from './hosted-session-lease-policy' +import { HostedSessionLeaseScheduler } from './hosted-session-lease-scheduler' + +const WorkVerdict = z.object({ + verdict: z.enum(['idle', 'busy', 'unverifiable']), + idleSince: z.number().nullable(), + lastPtyInputAt: z.number().nullable() +}) + +type Running = { scheduler: HostedSessionLeaseScheduler; runtimeId: string } + +export type HostedSessionRegistry = { + /** Start schedulers for running Orca Cloud runtimes, stop the rest. Cheap; call after any lifecycle change. */ + sync(): Promise + stopEnvironment(environmentId: string): void + dispose(): void +} + +export function createHostedSessionRegistry(deps: { + userDataPath: string + driver: EnvironmentProviderDriver + windows: () => readonly BrowserWindow[] + log?: (line: string) => void +}): HostedSessionRegistry { + const running = new Map() + const log = deps.log ?? ((line: string) => console.error(line)) + const broadcast = (channel: string, payload: unknown): void => { + for (const window of deps.windows()) { + if (!window.isDestroyed()) { + window.webContents.send(channel, payload) + } + } + } + + async function start(runtime: EphemeralVmRuntimeRecord, environmentId: string): Promise { + const handle = runtime.recipeResult + ? environmentHandleFromRecipeResult(runtime.recipeResult, HOSTED_SANDBOX_DRIVER_ID) + : null + const runtimeEnvironmentId = runtime.runtimeEnvironmentId + if (!handle || !runtimeEnvironmentId) { + return + } + const status = await deps.driver.status(handle) + if (status.status !== 'running' || !status.session || running.has(environmentId)) { + return + } + const call = createHostedRuntimeEnvironmentCall(deps.userDataPath, runtimeEnvironmentId) + const scheduler = new HostedSessionLeaseScheduler({ + driver: deps.driver, + handle, + session: status.session, + log, + readActivity: async (): Promise => { + const response = await callRuntimeEnvironment( + deps.userDataPath, + runtimeEnvironmentId, + ORCAD_WORK_VERDICT_METHOD, + {}, + 10_000 + ) + return response.ok + ? WorkVerdict.parse(response.result) + : { verdict: 'unverifiable', idleSince: null, lastPtyInputAt: null } + }, + warn: (minutesLeft) => { + log( + `[orca-cloud] ${runtime.workspaceName ?? runtime.id}: session restarts in ${minutesLeft} min` + ) + const event: HostedSessionWarningEvent = { + workspaceId: runtime.workspaceId ?? null, + workspaceName: runtime.workspaceName ?? null, + minutesLeft + } + broadcast('orcaCloud:sessionWarning', event) + }, + reconnect: async (ready) => { + updateEphemeralVmRuntimeStatus(deps.userDataPath, runtime.id, { + recipeResult: environmentReadyToRecipeResult(ready, 'orca-worktree') + }) + const { environment } = await applyResumedRuntimePairing({ + userDataPath: deps.userDataPath, + environmentId: runtimeEnvironmentId, + pairingCode: ready.connection.pairingCode, + invalidateTransport: invalidateRuntimeEnvironmentTransport + }) + noteRecipeVmWakePairing( + environment.id, + environment.pairingRevision ?? environment.createdAt, + true + ) + }, + resumeAgents: (drainStartedAt) => autoResumeAgentsAfterRollover(call, drainStartedAt), + // The window runs its ordinary Sleep, so the sidebar, chats and VM suspend stay one path. + onSuspended: () => + log(`[orca-cloud] idle sleep requested for ${runtime.workspaceName ?? runtime.id}`), + suspend: async () => { + broadcast('orcaCloud:idleSleepRequested', { workspaceId: runtime.workspaceId ?? null }) + } + }) + running.set(environmentId, { scheduler, runtimeId: runtime.id }) + scheduler.start() + log(`[orca-cloud] lease scheduler started for ${runtime.workspaceName ?? runtime.id}`) + } + + function stopEnvironment(environmentId: string): void { + running.get(environmentId)?.scheduler.stop() + running.delete(environmentId) + } + + return { + async sync() { + const live = new Set() + for (const runtime of listEphemeralVmRuntimes(deps.userDataPath)) { + if ( + runtime.recipe?.provider !== 'orca-cloud' || + runtime.status !== 'running' || + !runtime.recipeResult + ) { + continue + } + const handle = environmentHandleFromRecipeResult( + runtime.recipeResult, + HOSTED_SANDBOX_DRIVER_ID + ) + if (!handle) { + continue + } + live.add(handle.environmentId) + await start(runtime, handle.environmentId).catch((error: unknown) => + log(`[orca-cloud] could not start lease scheduler: ${String(error)}`) + ) + } + for (const environmentId of running.keys()) { + if (!live.has(environmentId)) { + stopEnvironment(environmentId) + } + } + }, + stopEnvironment, + dispose() { + for (const environmentId of running.keys()) { + stopEnvironment(environmentId) + } + } + } +} diff --git a/src/main/hosted-sandbox/hosted-session-sync.ts b/src/main/hosted-sandbox/hosted-session-sync.ts new file mode 100644 index 00000000000..a66d3e8face --- /dev/null +++ b/src/main/hosted-sandbox/hosted-session-sync.ts @@ -0,0 +1,11 @@ +// Lifecycle code nudges Orca Cloud's lease schedulers through this seam, so it never imports the driver. +let sync: (() => void) | null = null + +export function setHostedSessionSync(next: (() => void) | null): void { + sync = next +} + +/** Start or stop lease schedulers after a VM lifecycle change; a no-op when Orca Cloud is off. */ +export function syncHostedSessions(): void { + sync?.() +} diff --git a/src/main/ipc/ephemeral-vm-recipe-context.ts b/src/main/ipc/ephemeral-vm-recipe-context.ts index 3990f9827bf..f699fea87cb 100644 --- a/src/main/ipc/ephemeral-vm-recipe-context.ts +++ b/src/main/ipc/ephemeral-vm-recipe-context.ts @@ -154,13 +154,21 @@ export function resolveRecipeForRepo( } /** Project-owned recipes are authoritative for their repository and shadow - * same-id global plugin recipes without disabling the rest of the pack. */ + * same-id global plugin recipes without disabling the rest of the pack. Built-in providers keep + * their reserved ids and lead the list. */ export function combineEphemeralVmRecipes( repoRecipes: readonly OrcaVmRecipe[], pluginRecipes: readonly OrcaVmRecipe[] ): OrcaVmRecipe[] { - const repoIds = new Set(repoRecipes.map((recipe) => recipe.id)) - return [...repoRecipes, ...pluginRecipes.filter((recipe) => !repoIds.has(recipe.id))] + const builtIns = pluginRecipes.filter((recipe) => recipe.provider) + const reserved = new Set(builtIns.map((recipe) => recipe.id)) + const repo = repoRecipes.filter((recipe) => !reserved.has(recipe.id)) + const repoIds = new Set(repo.map((recipe) => recipe.id)) + return [ + ...builtIns, + ...repo, + ...pluginRecipes.filter((recipe) => !recipe.provider && !repoIds.has(recipe.id)) + ] } function failedRecipeRepo(repoPath: string | null, failure: EphemeralVmFailure): RecipeRepoResult { diff --git a/src/main/ipc/ephemeral-vm.ts b/src/main/ipc/ephemeral-vm.ts index e322668b02e..5a5dd302a17 100644 --- a/src/main/ipc/ephemeral-vm.ts +++ b/src/main/ipc/ephemeral-vm.ts @@ -37,7 +37,7 @@ import { } from './ephemeral-vm-recipe-context' import { registerEphemeralVmRuntimeHandlers } from './ephemeral-vm-runtime-handlers' import type { PluginService } from '../plugins/plugin-service' -import { getApprovedPluginVmRecipes } from '../plugins/plugin-approved-vm-recipes' +import { installHostedSandbox, withProviderRecipes } from '../hosted-sandbox/hosted-sandbox-install' import { resolveProvisionedRootSource } from '../ephemeral-vm/provisioned-root-source' import { getRuntimeEnvironmentRegistryRoot } from './runtime-environments/runtime-environment-registry-root' import { @@ -75,6 +75,7 @@ export type EphemeralVmProvisionIpcResult = } & EphemeralVmFailure) export function registerEphemeralVmHandlers(store: Store, pluginService?: PluginService): void { + installHostedSandbox() ipcMain.removeHandler('ephemeralVm:listRecipes') ipcMain.removeHandler('ephemeralVm:listRecipeCatalog') ipcMain.removeHandler('ephemeralVm:doctor') @@ -84,13 +85,13 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin reconcileInterruptedEphemeralVmProvisionsOnStartup(app.getPath('userData')) ipcMain.handle('ephemeralVm:listRecipes', async (_event, args: { repoId: string }) => { - return listRecipes(store, args.repoId, await getApprovedPluginVmRecipes(pluginService)) + return listRecipes(store, args.repoId, await withProviderRecipes(pluginService)) }) ipcMain.handle( 'ephemeralVm:listRecipeCatalog', async (): Promise => { - return listRecipeCatalog(store, await getApprovedPluginVmRecipes(pluginService)) + return listRecipeCatalog(store, await withProviderRecipes(pluginService)) } ) @@ -104,7 +105,7 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin if (!repo.ok) { return repo.doctor(args.recipeId) } - const pluginRecipes = await getApprovedPluginVmRecipes(pluginService) + const pluginRecipes = await withProviderRecipes(pluginService) return doctorEphemeralVmRecipe({ repoPath: repo.repo.path, recipeId: args.recipeId, @@ -136,7 +137,7 @@ export function registerEphemeralVmHandlers(store: Store, pluginService?: Plugin const recipe = resolveRecipeForRepo( repo.repo.path, args.recipeId, - await getApprovedPluginVmRecipes(pluginService) + await withProviderRecipes(pluginService) ) if (!recipe) { return provisionFailure(ephemeralVmFailure('recipeNotFound', { recipeId: args.recipeId })) diff --git a/src/main/ipc/profiles/orca-profile-billing-handlers.ts b/src/main/ipc/profiles/orca-profile-billing-handlers.ts new file mode 100644 index 00000000000..19f5487b322 --- /dev/null +++ b/src/main/ipc/profiles/orca-profile-billing-handlers.ts @@ -0,0 +1,72 @@ +import { ipcMain } from 'electron' +import { z } from 'zod' +import type { + OrcaBillingCheckoutArgs, + OrcaBillingOpenResult, + OrcaBillingOverageArgs, + OrcaBillingOverageResult, + OrcaBillingStatusResult +} from '../../../shared/orca-billing' +import { + ORCA_BILLING_INTERVALS, + ORCA_BILLING_MAX_SEATS, + ORCA_BILLING_PLAN_IDS +} from '../../../shared/orca-billing' +import { + getOrcaBillingStatus, + openOrcaBillingCheckout, + openOrcaBillingPortal, + setOrcaBillingOverage +} from '../../billing/billing-service' +import { getProfileUserDataPath } from '../../orca-profiles/profile-storage-paths' + +const CheckoutArgs = z + .object({ + plan: z.enum(ORCA_BILLING_PLAN_IDS), + interval: z.enum(ORCA_BILLING_INTERVALS), + seats: z.number().int().min(1).max(ORCA_BILLING_MAX_SEATS) + }) + .strict() + +// Cents; the billing service enforces the plan's maximum. +const OverageArgs = z + .object({ + enabled: z.boolean(), + spendCapCents: z.number().int().min(0).max(100_000_000).optional() + }) + .strict() + +export function billingCheckoutArgsFromUnknown(args: unknown): OrcaBillingCheckoutArgs { + const parsed = CheckoutArgs.safeParse(args) + if (!parsed.success) { + throw new Error('invalid_orca_billing_checkout') + } + return parsed.data +} + +export function billingOverageArgsFromUnknown(args: unknown): OrcaBillingOverageArgs { + const parsed = OverageArgs.safeParse(args) + if (!parsed.success) { + throw new Error('invalid_orca_billing_overage') + } + return parsed.data +} + +export function registerOrcaProfileBillingHandlers(): void { + ipcMain.handle('orcaProfiles:billingStatus', async (): Promise => + getOrcaBillingStatus(getProfileUserDataPath()) + ) + ipcMain.handle( + 'orcaProfiles:billingOpenCheckout', + async (_event, rawArgs: unknown): Promise => + openOrcaBillingCheckout(getProfileUserDataPath(), billingCheckoutArgsFromUnknown(rawArgs)) + ) + ipcMain.handle( + 'orcaProfiles:billingSetOverage', + async (_event, rawArgs: unknown): Promise => + setOrcaBillingOverage(getProfileUserDataPath(), billingOverageArgsFromUnknown(rawArgs)) + ) + ipcMain.handle('orcaProfiles:billingOpenPortal', async (): Promise => + openOrcaBillingPortal(getProfileUserDataPath()) + ) +} diff --git a/src/main/ipc/profiles/orca-profiles.ts b/src/main/ipc/profiles/orca-profiles.ts index 4c77c741c56..24b35961726 100644 --- a/src/main/ipc/profiles/orca-profiles.ts +++ b/src/main/ipc/profiles/orca-profiles.ts @@ -47,6 +47,7 @@ import { } from '../../orca-profiles/profile-cloud-service' import { refreshCurrentOrcaProfileAuth } from '../../orca-profiles/profile-cloud-capability-refresh' import { registerOrcaProfileOrgMemberHandlers } from './orca-profile-org-members-handlers' +import { registerOrcaProfileBillingHandlers } from './orca-profile-billing-handlers' import { onOrcaCloudSessionInvalidated } from '../../orca-profiles/profile-cloud-session-invalidation' import { broadcastOrcaProfileAuthStatusChanged, @@ -319,4 +320,5 @@ export function registerOrcaProfileHandlers( ) registerOrcaProfileOrgMemberHandlers() + registerOrcaProfileBillingHandlers() } diff --git a/src/main/orcad/orcad-entry.ts b/src/main/orcad/orcad-entry.ts index 0f463081e63..b68a8e53732 100644 --- a/src/main/orcad/orcad-entry.ts +++ b/src/main/orcad/orcad-entry.ts @@ -31,6 +31,7 @@ import { installOrcadFeatureReadiness } from '../runtime/rpc/orcad-feature-readi import { ORCA_SERVE_SUPERVISED_ENV } from '../../shared/serve-supervised-terminal-durability' import { startOrcadEventLoopWatchdog } from './orcad-event-loop-watchdog' import { isAgentStatusHooksEnabled } from '../../shared/agent-hook-listener/configuration/agent-status-hooks-setting' +import { installOrcadWorkProbes } from './orcad-work-verdict' let closeOrcadObservability = (): Promise => Promise.resolve() @@ -350,6 +351,12 @@ async function startOrcadRuntime( reportStartupTiming() managedAgentHooks.readinessPublished() + installOrcadWorkProbes({ + agentStates: () => agentHookServer.getStatusSnapshot(), + automationsBusy: () => orcadAutomationsKeepHostBusy(profileStore), + emulatorsBusy: async () => + (await runtime.getEmulatorBridge()?.hasLiveManagedSessions()) ?? false + }) await idleExitStartup.start({ rpc, agentStates: () => agentHookServer.getStatusSnapshot(), diff --git a/src/main/orcad/orcad-work-verdict.ts b/src/main/orcad/orcad-work-verdict.ts new file mode 100644 index 00000000000..cfba6ca75a5 --- /dev/null +++ b/src/main/orcad/orcad-work-verdict.ts @@ -0,0 +1,42 @@ +// The verdict a session-capped host (Orca Cloud) polls to decide renew, idle sleep or rollover. +// Unlike idle exit it ignores open clients and live shells: a connected desktop is not work. +import type { OrcadWorkVerdict } from '../../shared/hosted-sandbox/hosted-session-events' +import type { OrcadIdleProbe } from './orcad-idle-exit-monitor' + +let probes: readonly OrcadIdleProbe[] | null = null +let idleSince: number | null = null + +/** orcad installs its work probes once serving; other runtimes answer unverifiable. */ +export function installOrcadWorkProbes(ports: { + agentStates: () => readonly { state: string }[] + automationsBusy: () => boolean + emulatorsBusy: () => Promise +}): void { + const verdict = (busy: boolean): 'busy' | 'idle' => (busy ? 'busy' : 'idle') + probes = [ + { name: 'agents', read: () => verdict(ports.agentStates().some((e) => e.state === 'working')) }, + { name: 'automations', read: () => verdict(ports.automationsBusy()) }, + { name: 'emulators', read: async () => verdict(await ports.emulatorsBusy()) } + ] + idleSince = null +} + +export async function readOrcadWorkVerdict( + lastPtyInputAt: number | null, + now = Date.now() +): Promise { + if (!probes) { + return { verdict: 'unverifiable', idleSince: null, lastPtyInputAt } + } + for (const probe of probes) { + const verdict = await Promise.resolve() + .then(probe.read) + .catch(() => 'unverifiable' as const) + if (verdict !== 'idle') { + idleSince = null + return { verdict, idleSince: null, lastPtyInputAt } + } + } + idleSince ??= now + return { verdict: 'idle', idleSince, lastPtyInputAt } +} diff --git a/src/main/runtime/rpc/methods/orcad/orcad-work-verdict-method.ts b/src/main/runtime/rpc/methods/orcad/orcad-work-verdict-method.ts new file mode 100644 index 00000000000..f96d71706d3 --- /dev/null +++ b/src/main/runtime/rpc/methods/orcad/orcad-work-verdict-method.ts @@ -0,0 +1,16 @@ +import { defineMethod } from '../../core' +import { + ORCAD_WORK_VERDICT_METHOD, + OrcadWorkVerdictParamsSchema +} from '../../../../../shared/hosted-sandbox/hosted-session-events' +import { readOrcadWorkVerdict } from '../../../../orcad/orcad-work-verdict' + +export const ORCAD_WORK_VERDICT_METHODS = [ + defineMethod({ + name: ORCAD_WORK_VERDICT_METHOD, + permission: 'host-admin', + params: OrcadWorkVerdictParamsSchema, + handler: async (_params, { runtime }) => + readOrcadWorkVerdict(runtime.terminalRunFacts.readLatestInputAt()) + }) +] diff --git a/src/main/runtime/rpc/methods/rpc-method-registry.ts b/src/main/runtime/rpc/methods/rpc-method-registry.ts index 5b693508c20..155e2f36886 100644 --- a/src/main/runtime/rpc/methods/rpc-method-registry.ts +++ b/src/main/runtime/rpc/methods/rpc-method-registry.ts @@ -1,6 +1,7 @@ import { STATUS_METHODS } from './host-management/status' import { SIDEBAR_INPUT_METHODS } from './sidebar-inputs/sidebar-inputs' import { ORCAD_TERMINAL_CENSUS_METHODS } from './orcad/orcad-terminal-census' +import { ORCAD_WORK_VERDICT_METHODS } from './orcad/orcad-work-verdict-method' import { ORCAD_MIGRATION_METHODS } from './orcad/orcad-migration' import { SESSION_TERMINAL_SCROLLBACK_METHODS } from './terminal/session-terminal-scrollback' import { AI_VAULT_METHODS } from './history/ai-vault' @@ -86,6 +87,7 @@ import { ORCA_SIDEBAR_INPUTS_IS_MAIN } from '../../../../shared/sidebar-inputs-s export const ALL_RPC_METHODS = [ ...STATUS_METHODS, ...ORCAD_TERMINAL_CENSUS_METHODS, + ...ORCAD_WORK_VERDICT_METHODS, ...ORCAD_MIGRATION_METHODS, ...SESSION_TERMINAL_SCROLLBACK_METHODS, ...AGENT_HOOK_METHODS, diff --git a/src/main/runtime/terminal/lifecycle/terminal-run-facts.ts b/src/main/runtime/terminal/lifecycle/terminal-run-facts.ts index 44a555d6d63..95ee3713ee4 100644 --- a/src/main/runtime/terminal/lifecycle/terminal-run-facts.ts +++ b/src/main/runtime/terminal/lifecycle/terminal-run-facts.ts @@ -156,6 +156,16 @@ export class TerminalRunFactsRegister { } /** Like readLastInputAt, but only `driving` input, so Orca's own launch and orchestration writes don't count. */ + /** Latest input (not terminal replies) across every PTY; remote clients' keystrokes arrive here + * without the 'driving' kind, so a session-capped host reads all input. Null if none this process. */ + readLatestInputAt(): number | null { + let latest: number | null = null + for (const at of this.lastInputAtByPtyId.values()) { + latest = latest === null || at > latest ? at : latest + } + return latest + } + readLastDrivingInputAt(ptyId: string): number | null { return this.lastDrivingInputAtByPtyId.get(ptyId) ?? null } diff --git a/src/preload/api/ephemeral-vm-api.ts b/src/preload/api/ephemeral-vm-api.ts index 0b83d3f7157..4fdc1fc8932 100644 --- a/src/preload/api/ephemeral-vm-api.ts +++ b/src/preload/api/ephemeral-vm-api.ts @@ -1,4 +1,8 @@ import type { OrcaHooks } from '../../shared/orca-yaml-hook-types' +import type { + HostedSessionIdleSleepEvent, + HostedSessionWarningEvent +} from '../../shared/hosted-sandbox/hosted-session-events' import type { PublicKnownRuntimeEnvironment } from '../../shared/remote-runtime/host-catalog/runtime-environments' import type { EphemeralVmRecipeDoctorResult } from '../../shared/ephemeral-vm-recipes' import type { EphemeralVmRecipeResultWarning } from '../../shared/ephemeral-vm-recipe-diagnostics' @@ -63,6 +67,10 @@ export type EphemeralVmApi = { onProvisionEvent: ( callback: (event: { provisionId: string; stream: 'stdout' | 'stderr'; chunk: string }) => void ) => () => void + /** Orca Cloud: the session nears its cap (60/15 min left); running agents resume after the restart. */ + onCloudSessionWarning: (callback: (event: HostedSessionWarningEvent) => void) => () => void + /** Orca Cloud: the workspace has been idle long enough to sleep through the ordinary Sleep flow. */ + onCloudIdleSleepRequested: (callback: (event: HostedSessionIdleSleepEvent) => void) => () => void listRuntimes: () => Promise attachWorkspace: (args: { runtimeId: string diff --git a/src/preload/api/ephemeral-vm-bridge.ts b/src/preload/api/ephemeral-vm-bridge.ts index ee9748e499d..675f56e7766 100644 --- a/src/preload/api/ephemeral-vm-bridge.ts +++ b/src/preload/api/ephemeral-vm-bridge.ts @@ -1,5 +1,9 @@ import { ipcRenderer } from 'electron' import type { PreloadApi } from '../api-types' +import type { + HostedSessionIdleSleepEvent, + HostedSessionWarningEvent +} from '../../shared/hosted-sandbox/hosted-session-events' export const ephemeralVmApi = { listRecipes: (args) => ipcRenderer.invoke('ephemeralVm:listRecipes', args), @@ -15,6 +19,20 @@ export const ephemeralVmApi = { ipcRenderer.on('ephemeralVm:provisionEvent', listener) return () => ipcRenderer.removeListener('ephemeralVm:provisionEvent', listener) }, + onCloudSessionWarning: (callback) => { + const listener = (_event: Electron.IpcRendererEvent, event: HostedSessionWarningEvent): void => + callback(event) + ipcRenderer.on('orcaCloud:sessionWarning', listener) + return () => ipcRenderer.removeListener('orcaCloud:sessionWarning', listener) + }, + onCloudIdleSleepRequested: (callback) => { + const listener = ( + _event: Electron.IpcRendererEvent, + event: HostedSessionIdleSleepEvent + ): void => callback(event) + ipcRenderer.on('orcaCloud:idleSleepRequested', listener) + return () => ipcRenderer.removeListener('orcaCloud:idleSleepRequested', listener) + }, listRuntimes: () => ipcRenderer.invoke('ephemeralVm:listRuntimes'), attachWorkspace: (args) => ipcRenderer.invoke('ephemeralVm:attachWorkspace', args), resumeWorkspace: (args) => ipcRenderer.invoke('ephemeralVm:resumeWorkspace', args), diff --git a/src/preload/api/orca-profile-api.ts b/src/preload/api/orca-profile-api.ts index 8fe63dd6375..69b76af50a1 100644 --- a/src/preload/api/orca-profile-api.ts +++ b/src/preload/api/orca-profile-api.ts @@ -25,6 +25,13 @@ import type { TransferOrcaProfileProjectArgs, TransferOrcaProfileProjectResult } from '../../shared/orca-profiles' +import type { + OrcaBillingCheckoutArgs, + OrcaBillingOpenResult, + OrcaBillingOverageArgs, + OrcaBillingOverageResult, + OrcaBillingStatusResult +} from '../../shared/orca-billing' export type OrcaProfileApi = { list: () => Promise @@ -64,4 +71,12 @@ export type OrcaProfileApi = { orgMemberRemove: ( args: OrcaProfileOrgMemberRemoveArgs ) => Promise + /** Plan & billing for the signed-in account (Orca Cloud billing service). */ + billingStatus: () => Promise + /** Opens hosted Stripe Checkout in the browser. */ + billingOpenCheckout: (args: OrcaBillingCheckoutArgs) => Promise + /** Opens the Stripe Customer Portal in the browser. */ + billingOpenPortal: () => Promise + /** Opt in/out of sandbox overage and set the monthly spend cap (org admins). */ + billingSetOverage: (args: OrcaBillingOverageArgs) => Promise } diff --git a/src/preload/api/orca-profiles-bridge.ts b/src/preload/api/orca-profiles-bridge.ts index f144c9d7697..7063a998329 100644 --- a/src/preload/api/orca-profiles-bridge.ts +++ b/src/preload/api/orca-profiles-bridge.ts @@ -64,5 +64,9 @@ export const orcaProfilesApi = { orgMemberInvite: (args) => ipcRenderer.invoke('orcaProfiles:orgMemberInvite', args), orgInviteRevoke: (args) => ipcRenderer.invoke('orcaProfiles:orgInviteRevoke', args), orgMemberChangeRole: (args) => ipcRenderer.invoke('orcaProfiles:orgMemberChangeRole', args), - orgMemberRemove: (args) => ipcRenderer.invoke('orcaProfiles:orgMemberRemove', args) + orgMemberRemove: (args) => ipcRenderer.invoke('orcaProfiles:orgMemberRemove', args), + billingStatus: () => ipcRenderer.invoke('orcaProfiles:billingStatus'), + billingOpenCheckout: (args) => ipcRenderer.invoke('orcaProfiles:billingOpenCheckout', args), + billingOpenPortal: () => ipcRenderer.invoke('orcaProfiles:billingOpenPortal'), + billingSetOverage: (args) => ipcRenderer.invoke('orcaProfiles:billingSetOverage', args) } satisfies PreloadApi['orcaProfiles'] diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index b3eaaea1d6e..7f2e61cae7b 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -5,6 +5,7 @@ import { TooltipProvider } from '@/components/ui/tooltip' import { ConfirmationDialogProvider } from './components/confirmation-dialog' import { BrowserWebAuthnAccountDialog } from './components/browser-webauthn-account-dialog' import { BrowserExtensionInstallDialog } from './components/browser-extension-install-dialog' +import { PlansDialogHost } from './components/billing/plans/PlansDialogHost' import { BrowserPagePrompts } from './components/browser-pane/browser-page-prompts' import { LinkRoutingPreferenceDialogProvider } from './components/link-routing-preference-dialog' import { SkillFreshnessNudge } from './components/skills/SkillFreshnessNudge' @@ -117,6 +118,7 @@ function App(): React.JSX.Element { /> + {createPortal( diff --git a/src/renderer/src/components/billing/PaymentIssueNotice.tsx b/src/renderer/src/components/billing/PaymentIssueNotice.tsx new file mode 100644 index 00000000000..a7672fd1435 --- /dev/null +++ b/src/renderer/src/components/billing/PaymentIssueNotice.tsx @@ -0,0 +1,73 @@ +import { AlertTriangle, ExternalLink } from 'lucide-react' +import { Button } from '@/components/ui/button' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import type { OrcaBillingEntitlement } from '../../../../shared/orca-billing' + +/** + * Failed renewal: new sandboxes are paused at once; running ones are suspended when the grace + * ends. Stripe keeps retrying the card and emails the billing contact meanwhile. + */ +export function PaymentIssueNotice({ + entitlement, + busy, + onUpdatePayment +}: { + entitlement: OrcaBillingEntitlement + busy: boolean + onUpdatePayment: () => void +}): React.JSX.Element | null { + const t = useTranslate() + const locale = useIntlLocale() + const { payment } = entitlement + if (payment.pastDueSince === null) { + return null + } + const when = (ms: number): string => + new Intl.DateTimeFormat(locale, { + month: 'short', + day: 'numeric', + year: 'numeric', + hour: 'numeric', + minute: '2-digit', + timeZoneName: 'short' + }).format(ms) + return ( +
+ +
+

+ {payment.suspendRunning + ? t('auto.components.billing.paymentSuspended', 'Sandboxes suspended: payment overdue') + : t('auto.components.billing.paymentFailed', 'Payment failed')} +

+

+ {payment.suspendRunning || payment.graceEndsAt === null + ? t( + 'auto.components.billing.paymentSuspendedBody', + 'Running sandboxes were suspended and new ones cannot start. Update your payment method; everything resumes once the invoice is paid.' + ) + : t( + 'auto.components.billing.paymentGraceBody', + 'New sandboxes are paused. Running sandboxes will be suspended on {{value0}} unless the payment goes through. Stripe will retry the card and email your billing contact.', + { value0: when(payment.graceEndsAt) } + )} +

+ {entitlement.canManageBilling ? ( + + ) : null} +
+
+ ) +} diff --git a/src/renderer/src/components/billing/PlanBillingSection.fixture.tsx b/src/renderer/src/components/billing/PlanBillingSection.fixture.tsx new file mode 100644 index 00000000000..e97031b8cbf --- /dev/null +++ b/src/renderer/src/components/billing/PlanBillingSection.fixture.tsx @@ -0,0 +1,128 @@ +import { defineFixture } from '../../component-fixtures/component-fixture' +import { PlanBillingView } from './PlanBillingSection' +import { sampleTeam } from './plan-billing-fixtures' + +const HOUR = 3600 + +const handlers = { + loading: false, + busy: false, + notice: null, + onRefresh: () => {}, + onPortal: () => {}, + onSetOverage: () => {}, + onSeePlans: () => {} +} + +export const activeTeam = defineFixture({ + width: 600, + render: () => +}) + +export const noPlan = defineFixture({ + width: 680, + render: () => ( + + ) +}) + +export const overageCapReached = defineFixture({ + width: 600, + render: () => ( + + ) +}) + +export const pastDueMember = defineFixture({ + width: 600, + render: () => ( + + ) +}) + +export const suspendedAdmin = defineFixture({ + width: 600, + render: () => ( + + ) +}) diff --git a/src/renderer/src/components/billing/PlanBillingSection.tsx b/src/renderer/src/components/billing/PlanBillingSection.tsx new file mode 100644 index 00000000000..00a67976c03 --- /dev/null +++ b/src/renderer/src/components/billing/PlanBillingSection.tsx @@ -0,0 +1,228 @@ +import { useState } from 'react' +import { CreditCard, ExternalLink, RefreshCw, Sparkles } from 'lucide-react' +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import { formatCents } from './plans/plans-pricing' +import { cn } from '@/lib/utils' +import type { + OrcaBillingOpenResult, + OrcaBillingOverageArgs, + OrcaBillingOverageResult, + OrcaBillingStatusResult +} from '../../../../shared/orca-billing' +import { openPlansPage } from './plans/plans-page-store' +import { PaymentIssueNotice } from './PaymentIssueNotice' +import { OverageControls, UsageSummary } from './PlanBillingUsage' +import { + denyReasonCopy, + openFailureCopy, + overageFailureCopy, + statusLabel +} from './plan-billing-copy' +import { planBillingTone } from './plan-billing-format' +import { useOrcaBillingStatus } from './use-orca-billing-status' + +export function PlanBillingSection(): React.JSX.Element | null { + const t = useTranslate() + const { loading, result, refresh } = useOrcaBillingStatus(true) + const [busy, setBusy] = useState(false) + const [notice, setNotice] = useState(null) + + const run = async (open: () => Promise): Promise => { + setBusy(true) + setNotice(null) + const outcome = await open().catch((error: unknown): OrcaBillingOpenResult => ({ + status: 'failed', + error: String(error) + })) + setBusy(false) + setNotice( + outcome.status === 'opened' + ? t( + 'auto.components.billing.continueInBrowser', + 'Continue in your browser. This updates when you come back.' + ) + : openFailureCopy(outcome, t) + ) + } + + const setOverage = async (args: OrcaBillingOverageArgs): Promise => { + setBusy(true) + setNotice(null) + const outcome = await window.api.orcaProfiles + .billingSetOverage(args) + .catch((error: unknown): OrcaBillingOverageResult => ({ + status: 'failed', + error: String(error) + })) + setBusy(false) + if (outcome.status === 'ok') { + refresh() + } else { + setNotice(overageFailureCopy(outcome, t)) + } + } + + return ( + void run(() => window.api.orcaProfiles.billingOpenPortal())} + onSetOverage={(args) => void setOverage(args)} + onSeePlans={() => openPlansPage('settings')} + /> + ) +} + +export type PlanBillingViewProps = { + result: OrcaBillingStatusResult | null + loading: boolean + busy: boolean + notice: string | null + onRefresh: () => void + onPortal: () => void + onSetOverage: (args: OrcaBillingOverageArgs) => void + /** Opens the plans page (pricing, Checkout, Enterprise contact). */ + onSeePlans: () => void +} + +export function PlanBillingView({ + result, + loading, + busy, + notice, + onRefresh, + onPortal, + onSetOverage, + onSeePlans +}: PlanBillingViewProps): React.JSX.Element | null { + const t = useTranslate() + const locale = useIntlLocale() + if (!result || result.status === 'unconfigured' || result.status === 'signed-out') { + // The pane above already handles sign-in; billing stays out of builds without a service. + return null + } + + const header = ( +
+

+ {t('auto.components.billing.title', 'Plan & billing')} +

+ +
+ ) + + if (result.status === 'failed') { + return ( +
+ {header} +

+ {t('auto.components.billing.loadFailed', 'Could not load your plan. Try again.')} +

+
+ ) + } + + const entitlement = result.entitlement + const tone = planBillingTone(entitlement) + const hasPlan = entitlement.plan !== null && entitlement.status !== 'canceled' + const annualCents = entitlement.offers?.team.annualPriceCentsPerSeat ?? null + const teamFrom = annualCents === null ? null : formatCents(Math.round(annualCents / 12), locale) + + return ( +
+ {header} + {hasPlan ? ( +
+
+ +

{entitlement.planLabel}

+ + {t('auto.components.billing.seatsInterval', '{{value0}} seats · {{value1}}', { + value0: String(entitlement.seats), + value1: + entitlement.interval === 'year' + ? t('auto.components.billing.annual', 'Annual') + : t('auto.components.billing.monthly', 'Monthly') + })} + + + {statusLabel(entitlement.status, t)} + +
+ + +
+ ) : null} + + + + {entitlement.reason && entitlement.payment.pastDueSince === null ? ( +

{denyReasonCopy(entitlement.reason, t)}

+ ) : null} + + {!hasPlan && teamFrom ? ( +

+ {t( + 'auto.components.billing.teamFrom', + 'Team starts at {{value0}} per seat per month, billed annually.', + { + value0: teamFrom + } + )} +

+ ) : null} + +
+ {hasPlan ? null : ( + + )} + {hasPlan && entitlement.canManageBilling && entitlement.payment.pastDueSince === null ? ( + + ) : null} + {hasPlan ? ( + + ) : null} +
+ + {!entitlement.canManageBilling ? ( +

+ {t('auto.components.billing.askAdmin', 'Ask an org owner or admin to change the plan.')} +

+ ) : null} + + {notice ?

{notice}

: null} +
+ ) +} diff --git a/src/renderer/src/components/billing/PlanBillingUsage.tsx b/src/renderer/src/components/billing/PlanBillingUsage.tsx new file mode 100644 index 00000000000..e83bf716a91 --- /dev/null +++ b/src/renderer/src/components/billing/PlanBillingUsage.tsx @@ -0,0 +1,159 @@ +import { useState } from 'react' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { Progress } from '@/components/ui/progress' +import { Switch } from '@/components/ui/switch' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import type { + OrcaBillingEntitlement, + OrcaBillingOverageArgs +} from '../../../../shared/orca-billing' +import { formatDollars, formatHours, includedUsagePercent } from './plan-billing-format' + +export function UsageSummary({ + entitlement +}: { + entitlement: OrcaBillingEntitlement +}): React.JSX.Element { + const t = useTranslate() + const locale = useIntlLocale() + const date = (ms: number): string => + new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(ms) + return ( +
+
+ + {t( + 'auto.components.billing.hoursUsedMonth', + '{{value0}} of {{value1}} hours used this month', + { + value0: formatHours(entitlement.usedSeconds), + value1: formatHours(entitlement.includedSeconds) + } + )} + + + {t('auto.components.billing.hoursLeft', '{{value0}} hours left', { + value0: formatHours(entitlement.remainingIncludedSeconds) + })} + +
+ + {entitlement.usageWarningPercent !== null ? ( +

+ {entitlement.usageWarningPercent >= 100 + ? t('auto.components.billing.warn100', 'All included hours for this month are used.') + : t( + 'auto.components.billing.warnPercent', + 'You have used {{value0}}% of this month’s included hours.', + { + value0: String(entitlement.usageWarningPercent) + } + )} +

+ ) : null} +

+ {entitlement.usageWindow + ? t('auto.components.billing.resets', 'Hours reset {{value0}}', { + value0: date(entitlement.usageWindow.end) + }) + : null} + {entitlement.period + ? ` · ${ + entitlement.cancelAtPeriodEnd + ? t('auto.components.billing.endsOn', 'Plan ends {{value0}}', { + value0: date(entitlement.period.end) + }) + : t('auto.components.billing.renewsOn', 'Renews {{value0}}', { + value0: date(entitlement.period.end) + }) + }` + : null} +

+
+ ) +} + +export function OverageControls({ + entitlement, + busy, + onSetOverage +}: { + entitlement: OrcaBillingEntitlement + busy: boolean + onSetOverage: (args: OrcaBillingOverageArgs) => void +}): React.JSX.Element { + const t = useTranslate() + const { overage } = entitlement + const [capDollars, setCapDollars] = useState(String(Math.round(overage.spendCapCents / 100))) + const parsedCap = Number(capDollars) + const capValid = capDollars.trim() !== '' && Number.isInteger(parsedCap) && parsedCap >= 0 + const rate = formatDollars(overage.centsPerHour) + return ( +
+
+
+

+ {t('auto.components.billing.overageTitle', 'Overage')} +

+

+ {t( + 'auto.components.billing.overageDescription', + 'Keep starting sandboxes after included hours at {{value0}}/hr Standard ({{value1}}/hr Large).', + { value0: rate, value1: formatDollars(overage.centsPerHour * 2) } + )} +

+
+ onSetOverage({ enabled })} + aria-label={t('auto.components.billing.overageToggle', 'Allow overage')} + /> +
+ {overage.optedIn ? ( +
+

+ {t( + 'auto.components.billing.overageSpent', + '{{value0}} of {{value1}} monthly cap spent', + { + value0: formatDollars(overage.spentCents), + value1: formatDollars(overage.spendCapCents) + } + )} +

+ {entitlement.canManageBilling ? ( +
{ + event.preventDefault() + if (capValid) { + onSetOverage({ enabled: true, spendCapCents: parsedCap * 100 }) + } + }} + > + + {t('auto.components.billing.capLabel', 'Monthly cap ($)')} + +
+ setCapDollars(event.target.value)} + aria-label={t('auto.components.billing.capLabel', 'Monthly cap ($)')} + /> +
+ +
+ ) : null} +
+ ) : null} +
+ ) +} diff --git a/src/renderer/src/components/billing/plan-billing-copy.ts b/src/renderer/src/components/billing/plan-billing-copy.ts new file mode 100644 index 00000000000..5c86a00aa36 --- /dev/null +++ b/src/renderer/src/components/billing/plan-billing-copy.ts @@ -0,0 +1,104 @@ +// User-facing copy for billing states, kept apart from the components that render it. +import type { Translate } from '@/i18n/use-translate' +import type { + OrcaBillingDenyReason, + OrcaBillingOpenResult, + OrcaBillingOverageResult +} from '../../../../shared/orca-billing' + +export function denyReasonCopy(reason: OrcaBillingDenyReason, t: Translate): string { + switch (reason) { + case 'no_subscription': + return t('auto.components.billing.reasonNoPlan', 'Hosted sandboxes need a paid plan.') + case 'subscription_inactive': + return t( + 'auto.components.billing.reasonInactive', + 'Your plan is not active. Choose a plan to start sandboxes.' + ) + case 'payment_grace_expired': + return t( + 'auto.components.billing.reasonGraceExpired', + 'Your payment is overdue, so sandboxes are suspended. Update your payment method to resume.' + ) + case 'payment_past_due': + return t( + 'auto.components.billing.reasonPastDue', + 'Your last payment failed. Update your payment method to start new sandboxes.' + ) + case 'included_hours_exhausted': + return t( + 'auto.components.billing.reasonExhaustedOptIn', + 'You have used all included hours this month. Turn on overage to keep starting sandboxes.' + ) + case 'overage_cap_reached': + return t( + 'auto.components.billing.reasonCapRaise', + 'You reached this month’s overage spend cap. An admin can raise it.' + ) + } +} + +export function statusLabel(status: string, t: Translate): string { + switch (status) { + case 'active': + return t('auto.components.billing.statusActive', 'Active') + case 'trialing': + return t('auto.components.billing.statusTrial', 'Trial') + case 'past_due': + return t('auto.components.billing.statusPastDue', 'Payment due') + case 'canceled': + return t('auto.components.billing.statusCanceled', 'Canceled') + default: + return t('auto.components.billing.statusInactive', 'Inactive') + } +} + +export function openFailureCopy(result: OrcaBillingOpenResult, t: Translate): string | null { + switch (result.status) { + case 'opened': + return null + case 'forbidden': + return t( + 'auto.components.billing.openForbidden', + 'Only an org owner or admin can manage billing.' + ) + case 'already-subscribed': + return t( + 'auto.components.billing.openAlreadySubscribed', + 'This account already has a plan. Use Manage billing to change it.' + ) + case 'no-billing-account': + return t( + 'auto.components.billing.openNoAccount', + 'There is no billing account yet. Choose a plan first.' + ) + case 'signed-out': + return t('auto.components.billing.openSignedOut', 'Sign in to Orca again to manage billing.') + case 'unconfigured': + return t('auto.components.billing.openUnconfigured', 'Billing is unavailable in this build.') + case 'failed': + return t('auto.components.billing.openFailed', 'Could not reach billing. Try again.') + } +} + +export function overageFailureCopy(result: OrcaBillingOverageResult, t: Translate): string | null { + switch (result.status) { + case 'ok': + return null + case 'cap-too-high': + return t('auto.components.billing.capTooHigh', 'That spend cap is above the allowed maximum.') + case 'no-subscription': + return t('auto.components.billing.overageNoPlan', 'Overage needs an active plan.') + case 'forbidden': + return t( + 'auto.components.billing.openForbidden', + 'Only an org owner or admin can manage billing.' + ) + case 'signed-out': + return t('auto.components.billing.openSignedOut', 'Sign in to Orca again to manage billing.') + case 'unconfigured': + return t('auto.components.billing.openUnconfigured', 'Billing is unavailable in this build.') + case 'failed': + return t('auto.components.billing.openFailed', 'Could not reach billing. Try again.') + } +} diff --git a/src/renderer/src/components/billing/plan-billing-fixtures.ts b/src/renderer/src/components/billing/plan-billing-fixtures.ts new file mode 100644 index 00000000000..f54f24bb722 --- /dev/null +++ b/src/renderer/src/components/billing/plan-billing-fixtures.ts @@ -0,0 +1,47 @@ +// Shared sample entitlements for billing tests and component fixtures. +import type { OrcaBillingEntitlement, OrcaBillingOffers } from '../../../../shared/orca-billing' + +const HOUR = 3600 + +export const sampleOffers: OrcaBillingOffers = { + team: { + label: 'Team', + monthlyPriceLabel: '$50/seat/mo', + annualPriceLabel: '$480/seat/yr', + monthlyPriceCentsPerSeat: 5000, + annualPriceCentsPerSeat: 48000, + largeMachineWeight: 2, + includedHoursPerSeat: 60, + concurrentSandboxesPerSeat: 3, + idleSleepMinutes: 15, + overageCentsPerHour: 75, + defaultSpendCapCents: 2500 + }, + enterprise: { + label: 'Enterprise', + priceLabel: 'Custom', + contactUrl: 'https://onorca.dev/contact-sales' + } +} + +export const sampleTeam: OrcaBillingEntitlement = { + plan: 'team', + planLabel: 'Team', + status: 'active', + interval: 'month', + seats: 2, + period: { start: Date.UTC(2026, 10, 3), end: Date.UTC(2026, 11, 3) }, + usageWindow: { start: Date.UTC(2026, 10, 3), end: Date.UTC(2026, 11, 3) }, + cancelAtPeriodEnd: false, + includedSeconds: 120 * HOUR, + usedSeconds: 41.5 * HOUR, + remainingIncludedSeconds: 78.5 * HOUR, + usageWarningPercent: null, + overageSeconds: 0, + overage: { optedIn: false, active: false, spendCapCents: 2500, spentCents: 0, centsPerHour: 75 }, + payment: { pastDueSince: null, graceEndsAt: null, suspendRunning: false }, + canStartSandbox: true, + reason: null, + canManageBilling: true, + offers: sampleOffers +} diff --git a/src/renderer/src/components/billing/plan-billing-format.test.ts b/src/renderer/src/components/billing/plan-billing-format.test.ts new file mode 100644 index 00000000000..37ba055838b --- /dev/null +++ b/src/renderer/src/components/billing/plan-billing-format.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest' +import { + formatDollars, + formatHours, + includedUsagePercent, + planBillingTone +} from './plan-billing-format' +import { sampleTeam as base } from './plan-billing-fixtures' + +describe('plan billing format', () => { + it('formats hours with at most one decimal', () => { + expect(formatHours(0)).toBe('0') + expect(formatHours(3600)).toBe('1') + expect(formatHours(5400)).toBe('1.5') + expect(formatHours(3600 + 1)).toBe('1') + expect(formatHours(-5)).toBe('0') + }) + + it('formats dollars, dropping cents for whole amounts', () => { + expect(formatDollars(75)).toBe('$0.75') + expect(formatDollars(150)).toBe('$1.50') + expect(formatDollars(2500)).toBe('$25') + }) + + it('clamps the usage bar', () => { + expect(includedUsagePercent({ ...base, usedSeconds: 24 * 3600 })).toBe(20) + expect(includedUsagePercent({ ...base, usedSeconds: 200 * 3600 })).toBe(100) + expect(includedUsagePercent({ ...base, includedSeconds: 0 })).toBe(0) + }) + + it('flags exhausted, ending and blocked plans', () => { + expect(planBillingTone(base)).toBe('ok') + expect(planBillingTone({ ...base, remainingIncludedSeconds: 0 })).toBe('warning') + expect(planBillingTone({ ...base, cancelAtPeriodEnd: true })).toBe('warning') + expect(planBillingTone({ ...base, canStartSandbox: false, reason: 'payment_past_due' })).toBe( + 'blocked' + ) + }) +}) diff --git a/src/renderer/src/components/billing/plan-billing-format.ts b/src/renderer/src/components/billing/plan-billing-format.ts new file mode 100644 index 00000000000..d839ccc4908 --- /dev/null +++ b/src/renderer/src/components/billing/plan-billing-format.ts @@ -0,0 +1,33 @@ +import type { OrcaBillingEntitlement } from '../../../../shared/orca-billing' + +/** Hours with at most one decimal; whole numbers drop the decimal. */ +export function formatHours(seconds: number): string { + const hours = Math.max(0, seconds) / 3600 + const rounded = Math.round(hours * 10) / 10 + return Number.isInteger(rounded) ? String(rounded) : rounded.toFixed(1) +} + +/** Percent of included hours used, clamped to 0..100 (overage shows as full). */ +export function includedUsagePercent(entitlement: OrcaBillingEntitlement): number { + if (entitlement.includedSeconds <= 0) { + return 0 + } + return Math.min(100, Math.round((entitlement.usedSeconds / entitlement.includedSeconds) * 100)) +} + +export type PlanBillingTone = 'ok' | 'warning' | 'blocked' + +export function planBillingTone(entitlement: OrcaBillingEntitlement): PlanBillingTone { + if (!entitlement.canStartSandbox) { + return 'blocked' + } + return entitlement.remainingIncludedSeconds === 0 || entitlement.cancelAtPeriodEnd + ? 'warning' + : 'ok' +} + +/** "$0.75", "$25" — whole dollars drop the cents. */ +export function formatDollars(cents: number): string { + const dollars = Math.max(0, cents) / 100 + return Number.isInteger(dollars) ? `$${dollars}` : `$${dollars.toFixed(2)}` +} diff --git a/src/renderer/src/components/billing/plans/EnterprisePlanCard.tsx b/src/renderer/src/components/billing/plans/EnterprisePlanCard.tsx new file mode 100644 index 00000000000..9ab831628d7 --- /dev/null +++ b/src/renderer/src/components/billing/plans/EnterprisePlanCard.tsx @@ -0,0 +1,49 @@ +import { Building2, ExternalLink } from 'lucide-react' +import { Button } from '@/components/ui/button' +import { useTranslate } from '@/i18n/use-translate' +import type { OrcaBillingOffers } from '../../../../../shared/orca-billing' +import { PlanCardShell } from './PlanCardShell' + +export function EnterprisePlanCard({ + enterprise, + onContactSales +}: { + enterprise: OrcaBillingOffers['enterprise'] + onContactSales: () => void +}): React.JSX.Element { + const t = useTranslate() + return ( + + + {enterprise.label} + + } + price={

{enterprise.priceLabel}

} + priceNote={t('auto.components.billing.plans.entPriced', 'Priced for your organization')} + featuresLead={t('auto.components.billing.plans.entLead', 'Everything in Team, plus:')} + features={[ + t('auto.components.billing.plans.entByoc', 'Run sandboxes in your own cloud account'), + t('auto.components.billing.plans.entSso', 'SSO, SCIM provisioning and audit logs'), + t('auto.components.billing.plans.entNetwork', 'Network policy and egress controls'), + t('auto.components.billing.plans.entTerms', 'Custom limits, invoicing and terms') + ]} + footer={ + <> + +

+ {t( + 'auto.components.billing.plans.entHint', + 'Tell us about your team and where you run.' + )} +

+ + } + /> + ) +} diff --git a/src/renderer/src/components/billing/plans/PlanCardShell.tsx b/src/renderer/src/components/billing/plans/PlanCardShell.tsx new file mode 100644 index 00000000000..22c941d7479 --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlanCardShell.tsx @@ -0,0 +1,64 @@ +import { Check } from 'lucide-react' +import type { ReactNode } from 'react' + +/** + * Shared plan card layout. Both cards use the same rows (title, price, details, features, + * footer) and stretch to the same height, so their dividers and buttons line up. + */ +export function PlanCardShell({ + id, + title, + badge, + price, + priceNote, + details, + featuresLead, + features, + footer +}: { + id: string + title: ReactNode + badge?: ReactNode + price: ReactNode + priceNote: ReactNode + details?: ReactNode + featuresLead?: string + features: string[] + footer: ReactNode +}): React.JSX.Element { + return ( +
+
+

+ {title} +

+ {badge} +
+
+ {price} +

{priceNote}

+
+ {details} +
+ {featuresLead ? ( +

{featuresLead}

+ ) : null} +
    + {features.map((feature) => ( +
  • + + {feature} +
  • + ))} +
+
+ {/* Fixed footer height (button + up to two lines) keeps both cards' dividers level. */} +
+ {footer} +
+
+ ) +} diff --git a/src/renderer/src/components/billing/plans/PlansDialogContent.tsx b/src/renderer/src/components/billing/plans/PlansDialogContent.tsx new file mode 100644 index 00000000000..fbd68354417 --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlansDialogContent.tsx @@ -0,0 +1,45 @@ +import { DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' +import { useTranslate } from '@/i18n/use-translate' +import { PlansView, isSubscribed, type PlansViewProps } from './PlansView' + +/** The plans page surface: heading plus plans. Rendered inside a . */ +export function PlansDialogContent(props: PlansViewProps): React.JSX.Element { + const t = useTranslate() + const subscribed = isSubscribed(props.result) + return ( + { + event.preventDefault() + if (event.currentTarget instanceof HTMLElement) { + event.currentTarget.focus() + } + }} + > + +

+ {t('auto.components.billing.plans.eyebrow', 'Orca Cloud')} +

+ + {subscribed + ? t('auto.components.billing.plans.titleSubscribed', 'Your Orca Cloud plan') + : t('auto.components.billing.plans.title', 'Cloud sandboxes for your whole team')} + + + {subscribed + ? t( + 'auto.components.billing.plans.subtitleSubscribed', + 'Usage this month, your plan, and what Enterprise adds.' + ) + : t( + 'auto.components.billing.plans.subtitle2', + 'Run Orca agents in hosted sandboxes that keep going when you step away.' + )} + +
+ +
+ ) +} diff --git a/src/renderer/src/components/billing/plans/PlansDialogHost.tsx b/src/renderer/src/components/billing/plans/PlansDialogHost.tsx new file mode 100644 index 00000000000..7e32192da75 --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlansDialogHost.tsx @@ -0,0 +1,73 @@ +import { useState } from 'react' +import { Dialog } from '@/components/ui/dialog' +import { useTranslate } from '@/i18n/use-translate' +import type { OrcaBillingOpenResult } from '../../../../../shared/orca-billing' +import { openFailureCopy } from '../plan-billing-copy' +import { useOrcaBillingStatus } from '../use-orca-billing-status' +import { PlansDialogContent } from './PlansDialogContent' +import { closePlansPage, usePlansPageState } from './plans-page-store' +import { useOrgSeatCount } from './use-org-seat-count' +import { useAppStore } from '@/store/index' + +/** + * The plans page. A dialog because choosing a plan is a decision step in front of Checkout; + * it reads the billing service's offers, so prices and limits are never hardcoded here. + */ +export function PlansDialogHost(): React.JSX.Element { + const t = useTranslate() + const { open } = usePlansPageState() + const billing = useOrcaBillingStatus(open) + const orgId = useAppStore((state) => state.orcaProfileAuthStatus?.cloud?.activeOrgId ?? null) + const seats = useOrgSeatCount(open ? orgId : null) + const [busy, setBusy] = useState(false) + const [notice, setNotice] = useState(null) + + const run = async (action: () => Promise): Promise => { + setBusy(true) + setNotice(null) + const outcome = await action().catch((error: unknown): OrcaBillingOpenResult => ({ + status: 'failed', + error: String(error) + })) + setBusy(false) + setNotice( + outcome.status === 'opened' + ? t( + 'auto.components.billing.continueInBrowser', + 'Continue in your browser. This updates when you come back.' + ) + : openFailureCopy(outcome, t) + ) + } + + return ( + { + if (!next) { + closePlansPage() + setNotice(null) + } + }} + > + {open ? ( + + void run(() => + window.api.orcaProfiles.billingOpenCheckout({ plan: 'team', interval, seats: count }) + ) + } + onManage={() => void run(() => window.api.orcaProfiles.billingOpenPortal())} + onContactSales={(url) => void window.api.shell.openUrl(url)} + onRetry={billing.refresh} + /> + ) : null} + + ) +} diff --git a/src/renderer/src/components/billing/plans/PlansIntervalToggle.tsx b/src/renderer/src/components/billing/plans/PlansIntervalToggle.tsx new file mode 100644 index 00000000000..553f601513f --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlansIntervalToggle.tsx @@ -0,0 +1,70 @@ +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import type { OrcaBillingInterval } from '../../../../../shared/orca-billing' + +/** Monthly / Annual switch with a sliding thumb (still under reduced motion). */ +export function PlansIntervalToggle({ + value, + savingsPercent, + disabled, + onChange +}: { + value: OrcaBillingInterval + savingsPercent: number | null + disabled: boolean + onChange: (interval: OrcaBillingInterval) => void +}): React.JSX.Element { + const t = useTranslate() + const options: { interval: OrcaBillingInterval; label: string; hint: string | null }[] = [ + { interval: 'month', label: t('auto.components.billing.plans.monthly', 'Monthly'), hint: null }, + { + interval: 'year', + label: t('auto.components.billing.plans.annual', 'Annual'), + hint: + savingsPercent === null + ? null + : t('auto.components.billing.plans.saveShort', '−{{value0}}%', { + value0: String(savingsPercent) + }) + } + ] + return ( +
+
+ + {options.map((option) => ( + + ))} +
+
+ ) +} diff --git a/src/renderer/src/components/billing/plans/PlansView.fixture.tsx b/src/renderer/src/components/billing/plans/PlansView.fixture.tsx new file mode 100644 index 00000000000..2ccf272d2a2 --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlansView.fixture.tsx @@ -0,0 +1,118 @@ +import { defineFixture } from '../../../component-fixtures/component-fixture' +import type { OrcaBillingEntitlement } from '../../../../../shared/orca-billing' +import { sampleTeam } from '../plan-billing-fixtures' +import { Dialog } from '@/components/ui/dialog' +import { PlansDialogContent } from './PlansDialogContent' +import type { PlansViewProps } from './PlansView' + +const HOUR = 3600 + +const handlers = { + busy: false, + notice: null, + seats: 4, + onCheckout: () => {}, + onManage: () => {}, + onContactSales: () => {}, + onRetry: () => {} +} + +const noPlan: OrcaBillingEntitlement = { + ...sampleTeam, + plan: null, + planLabel: null, + status: 'none', + interval: null, + seats: 0, + period: null, + usageWindow: null, + includedSeconds: 0, + usedSeconds: 0, + remainingIncludedSeconds: 0, + canStartSandbox: false, + reason: 'no_subscription' +} + +const width = 1280 + +function Page(props: Pick & Partial): React.JSX.Element { + return ( + + + + ) +} + +export const choosePlan = defineFixture({ + width, + render: () => +}) + +export const memberCannotBuy = defineFixture({ + width, + render: () => ( + + ) +}) + +export const currentPlan = defineFixture({ + width, + render: () => ( + + ) +}) + +export const paymentFailed = defineFixture({ + width, + render: () => ( + + ) +}) + +export const checkoutOpened = defineFixture({ + width, + render: () => ( + + ) +}) + +export const loading = defineFixture({ + width, + render: () => +}) + +export const loadFailed = defineFixture({ + width, + render: () => +}) diff --git a/src/renderer/src/components/billing/plans/PlansView.tsx b/src/renderer/src/components/billing/plans/PlansView.tsx new file mode 100644 index 00000000000..a1cbb39c4df --- /dev/null +++ b/src/renderer/src/components/billing/plans/PlansView.tsx @@ -0,0 +1,273 @@ +import { useState } from 'react' +import { KeyRound, LaptopMinimal, RefreshCw, Zap } from 'lucide-react' +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { Progress } from '@/components/ui/progress' +import { LoadingSpinner } from '@/components/LoadingSpinner' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import { + ORCA_BILLING_MAX_SEATS, + type OrcaBillingEntitlement, + type OrcaBillingInterval, + type OrcaBillingStatusResult +} from '../../../../../shared/orca-billing' +import { PaymentIssueNotice } from '../PaymentIssueNotice' +import { statusLabel } from '../plan-billing-copy' +import { formatHours, includedUsagePercent } from '../plan-billing-format' +import { EnterprisePlanCard } from './EnterprisePlanCard' +import { PlansIntervalToggle } from './PlansIntervalToggle' +import { TeamPlanCard, type TeamPlanCardMode } from './TeamPlanCard' +import { annualSavingsPercent, clampSeats } from './plans-pricing' + +export type PlansViewProps = { + result: OrcaBillingStatusResult | null + busy: boolean + notice: string | null + /** Seats Checkout will bill: org members, or 1 for a personal account. */ + seats: number + onCheckout: (args: { interval: OrcaBillingInterval; seats: number }) => void + onManage: () => void + onContactSales: (url: string) => void + onRetry: () => void +} + +export function isSubscribed(result: OrcaBillingStatusResult | null): boolean { + return ( + result?.status === 'ok' && + result.entitlement.plan !== null && + result.entitlement.status !== 'canceled' + ) +} + +function ValueProps(): React.JSX.Element { + const t = useTranslate() + // Same promises as the marketing site's /cloud page. + const items = [ + { + icon: LaptopMinimal, + title: t('auto.components.billing.plans.vpRunningTitle', 'Agents keep running'), + body: t( + 'auto.components.billing.plans.vpRunningBody2', + 'Sandboxes keep working while you are away and report back when they finish.' + ) + }, + { + icon: Zap, + title: t('auto.components.billing.plans.vpWakeTitle', 'Wake in seconds'), + body: t( + 'auto.components.billing.plans.vpWakeBody', + 'Idle sandboxes sleep to save hours and resume right where they left off.' + ) + }, + { + icon: KeyRound, + title: t('auto.components.billing.plans.vpSecretsTitle', 'Secrets never stored in the VM'), + body: t( + 'auto.components.billing.plans.vpSecretsBody2', + 'Credentials are injected into outbound requests and never touch the VM’s disk.' + ) + } + ] + return ( +
    + {items.map((item) => ( +
  • + + + +
    +

    {item.title}

    +

    {item.body}

    +
    +
  • + ))} +
+ ) +} + +function CurrentPlanStrip({ + entitlement +}: { + entitlement: OrcaBillingEntitlement +}): React.JSX.Element { + const t = useTranslate() + const locale = useIntlLocale() + const paymentIssue = entitlement.payment.pastDueSince !== null + const resets = entitlement.usageWindow + ? new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(entitlement.usageWindow.end) + : null + return ( +
+
+ {entitlement.planLabel} + + {t('auto.components.billing.plans.seatsAndPeriod', 'Seats: {{value0}} · {{value1}}', { + value0: String(entitlement.seats), + value1: + entitlement.interval === 'year' + ? t('auto.components.billing.annual', 'Annual') + : t('auto.components.billing.monthly', 'Monthly') + })} + + {/* The payment banner carries the alarm; the badge stays quiet. */} + + {statusLabel(entitlement.status, t)} + +
+ +
+ + {t( + 'auto.components.billing.hoursUsedMonth', + '{{value0}} of {{value1}} hours used this month', + { + value0: formatHours(entitlement.usedSeconds), + value1: formatHours(entitlement.includedSeconds) + } + )} + + + {resets + ? t( + 'auto.components.billing.plans.leftResets', + '{{value0}} hours left · resets {{value1}}', + { + value0: formatHours(entitlement.remainingIncludedSeconds), + value1: resets + } + ) + : null} + +
+
+ ) +} + +function PlaceholderCards({ + failed, + onRetry +}: { + failed: boolean + onRetry: () => void +}): React.JSX.Element { + const t = useTranslate() + return ( +
+ +
+
+
+
+ {failed ? ( + <> +

+ {t( + 'auto.components.billing.plans.loadFailed', + 'Could not load plans. Try again in a moment.' + )} +

+ + + ) : ( + <> + +

{t('auto.components.billing.plans.loading', 'Loading plans…')}

+ + )} +
+
+
+ ) +} + +export function PlansView(props: PlansViewProps): React.JSX.Element { + const t = useTranslate() + const [interval, setInterval] = useState('month') + const seats = clampSeats(props.seats, ORCA_BILLING_MAX_SEATS) + const result = props.result + + if (result?.status === 'signed-out' || result?.status === 'unconfigured') { + return ( +

+ {result.status === 'signed-out' + ? t('auto.components.billing.openSignedOut', 'Sign in to Orca again to manage billing.') + : t('auto.components.billing.openUnconfigured', 'Billing is unavailable in this build.')} +

+ ) + } + if (!result || result.status !== 'ok' || !result.entitlement.offers) { + return + } + + const entitlement = result.entitlement + const offers = result.entitlement.offers + const subscribed = isSubscribed(result) + const mode: TeamPlanCardMode = subscribed + ? 'current' + : entitlement.canManageBilling + ? 'choose' + : 'locked' + const paymentIssue = entitlement.payment.pastDueSince !== null + + return ( +
+ {subscribed ? ( + <> + + + + ) : ( + + )} + {mode === 'choose' ? ( + + ) : null} +
+ props.onCheckout({ interval, seats })} + onManage={props.onManage} + /> + props.onContactSales(offers.enterprise.contactUrl)} + /> +
+
+

+ {t( + 'auto.components.billing.plans.footHours', + 'Hours reset every month, including on annual plans. When they run out, new sandboxes wait until the next month unless overage is on.' + )} +

+

+ {t( + 'auto.components.billing.plans.footTerms', + 'Prices in USD, before tax. Overage stays off until an owner or admin turns it on and sets a spend cap. Cancel anytime; your plan runs to the end of the period.' + )} +

+
+
+ ) +} diff --git a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx new file mode 100644 index 00000000000..c5a06af4deb --- /dev/null +++ b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx @@ -0,0 +1,219 @@ +import { ExternalLink, Lock, Users } from 'lucide-react' +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { LoadingSpinner } from '@/components/LoadingSpinner' +import { useIntlLocale, useTranslate } from '@/i18n/use-translate' +import type { OrcaBillingInterval, OrcaBillingOffers } from '../../../../../shared/orca-billing' +import { PlanCardShell } from './PlanCardShell' +import { formatCents, perSeatMonthlyCents, periodTotalCents } from './plans-pricing' + +export type TeamPlanCardMode = 'choose' | 'current' | 'locked' + +export function TeamPlanCard({ + team, + interval, + seats, + mode, + busy, + notice, + hideManage, + onCheckout, + onManage +}: { + team: OrcaBillingOffers['team'] + interval: OrcaBillingInterval + seats: number + /** choose: can buy; current: already on Team; locked: only owners/admins can buy. */ + mode: TeamPlanCardMode + busy: boolean + notice: string | null + /** A payment banner already offers the same Portal action. */ + hideManage: boolean + onCheckout: () => void + onManage: () => void +}): React.JSX.Element { + const t = useTranslate() + const locale = useIntlLocale() + const monthly = perSeatMonthlyCents(team, interval) + const fullMonthly = team.monthlyPriceCentsPerSeat + const total = periodTotalCents(team, interval, seats) + const discounted = + interval === 'year' && monthly !== null && fullMonthly !== null && monthly < fullMonthly + + const features = [ + t( + 'auto.components.billing.plans.featureHours2', + '{{value0}} Standard hours (4 vCPU / 8 GB) per seat each month, pooled across your team', + { value0: String(team.includedHoursPerSeat) } + ), + t( + 'auto.components.billing.plans.featureLarge2', + 'Large (8 vCPU / 16 GB) uses hours at {{value0}}×', + { + value0: String(team.largeMachineWeight) + } + ), + t('auto.components.billing.plans.featureConcurrency', '{{value0}} running sandboxes per seat', { + value0: String(team.concurrentSandboxesPerSeat) + }), + t('auto.components.billing.plans.featureSleep2', 'Sleeps after {{value0}} minutes idle', { + value0: String(team.idleSleepMinutes) + }), + t( + 'auto.components.billing.plans.featureOverage', + 'Optional overage at {{value0}}/hr ({{value1}}/hr Large) with a monthly spend cap', + { + value0: formatCents(team.overageCentsPerHour, locale), + value1: formatCents(team.overageCentsPerHour * team.largeMachineWeight, locale) + } + ) + ] + + const price = ( + // Keyed so the figure fades in again when the period changes. +

+ {discounted && fullMonthly !== null ? ( + + {formatCents(fullMonthly, locale)} + + ) : null} + + {monthly === null + ? interval === 'month' + ? team.monthlyPriceLabel + : team.annualPriceLabel + : formatCents(monthly, locale)} + + {monthly === null ? null : ( + + {t('auto.components.billing.plans.perSeatMonth', 'per seat / month')} + + )} +

+ ) + + const priceNote = + interval === 'year' + ? team.annualPriceCentsPerSeat === null + ? t('auto.components.billing.plans.billedYearly', 'Billed yearly') + : t( + 'auto.components.billing.plans.billedYearlyAmount', + 'Billed {{value0}} per seat yearly', + { + value0: formatCents(team.annualPriceCentsPerSeat, locale) + } + ) + : t('auto.components.billing.plans.billedMonthly', 'Billed monthly') + + const details = + mode === 'choose' ? ( +
+

+ {t('auto.components.billing.plans.seatsOne', 'Seats: {{value0}} (one per team member)', { + value0: String(seats) + })} +

+ {total === null ? null : ( +

+ {interval === 'year' + ? t('auto.components.billing.plans.totalYear', '{{value0}} / year', { + value0: formatCents(total, locale) + }) + : t('auto.components.billing.plans.totalMonth', '{{value0}} / month', { + value0: formatCents(total, locale) + })} +

+ )} +
+ ) : null + + let footer: React.ReactNode + if (mode === 'choose') { + footer = ( + <> + +

+ {notice ?? + t( + 'auto.components.billing.plans.seatsFollow', + 'Seats follow your team: new members add a seat, removals credit your next invoice.' + )} +

+ + ) + } else if (mode === 'current') { + footer = hideManage ? ( +

+ {t( + 'auto.components.billing.plans.fixPaymentAbove', + 'Update your payment method above to keep your plan active.' + )} +

+ ) : ( + <> + +

+ {notice ?? + t( + 'auto.components.billing.plans.manageHint', + 'Switch monthly or annual, update payment, or cancel.' + )} +

+ + ) + } else { + footer = ( +
+ +

+ {t( + 'auto.components.billing.plans.lockedBody', + 'Only an owner or admin of your org can choose a plan. Ask one of them to open Plan & billing in Orca.' + )} +

+
+ ) + } + + return ( + + + {team.label} + + } + badge={ + mode === 'current' ? ( + + {t('auto.components.billing.plans.currentPlan', 'Current plan')} + + ) : null + } + price={price} + priceNote={priceNote} + details={details} + features={features} + footer={footer} + /> + ) +} diff --git a/src/renderer/src/components/billing/plans/plans-page-store.ts b/src/renderer/src/components/billing/plans/plans-page-store.ts new file mode 100644 index 00000000000..2872728f2f9 --- /dev/null +++ b/src/renderer/src/components/billing/plans/plans-page-store.ts @@ -0,0 +1,38 @@ +// Open state for the plans page. Any entry point (account menu, Settings, a "start a cloud +// sandbox" upsell) calls openPlansPage(); PlansDialogHost renders it. +import { useSyncExternalStore } from 'react' + +export type PlansPageSource = 'account-menu' | 'settings' | 'sandbox' | 'other' + +type PlansPageState = { open: boolean; source: PlansPageSource | null } + +let state: PlansPageState = { open: false, source: null } +const listeners = new Set<() => void>() + +function set(next: PlansPageState): void { + state = next + for (const listener of listeners) { + listener() + } +} + +export function openPlansPage(source: PlansPageSource = 'other'): void { + set({ open: true, source }) +} + +export function closePlansPage(): void { + set({ open: false, source: state.source }) +} + +function subscribe(listener: () => void): () => void { + listeners.add(listener) + return () => listeners.delete(listener) +} + +export function getPlansPageState(): PlansPageState { + return state +} + +export function usePlansPageState(): PlansPageState { + return useSyncExternalStore(subscribe, getPlansPageState) +} diff --git a/src/renderer/src/components/billing/plans/plans-pricing.test.ts b/src/renderer/src/components/billing/plans/plans-pricing.test.ts new file mode 100644 index 00000000000..19dd7bb2c5b --- /dev/null +++ b/src/renderer/src/components/billing/plans/plans-pricing.test.ts @@ -0,0 +1,56 @@ +import { describe, expect, it } from 'vitest' +import { sampleOffers } from '../plan-billing-fixtures' +import { + annualSavingsPercent, + clampSeats, + formatCents, + perSeatMonthlyCents, + periodTotalCents +} from './plans-pricing' +import { closePlansPage, getPlansPageState, openPlansPage } from './plans-page-store' + +const team = sampleOffers.team + +describe('plans pricing', () => { + it('shows annual as its monthly equivalent and totals per period', () => { + expect(perSeatMonthlyCents(team, 'month')).toBe(5000) + expect(perSeatMonthlyCents(team, 'year')).toBe(4000) + expect(periodTotalCents(team, 'month', 3)).toBe(15000) + expect(periodTotalCents(team, 'year', 3)).toBe(144000) + expect(periodTotalCents(team, 'year', 0)).toBe(48000) + }) + + it('computes the annual saving and hides it when annual is not cheaper', () => { + expect(annualSavingsPercent(team)).toBe(20) + expect(annualSavingsPercent({ ...team, annualPriceCentsPerSeat: 60000 })).toBeNull() + expect(annualSavingsPercent({ ...team, monthlyPriceCentsPerSeat: null })).toBeNull() + }) + + it('falls back to labels when an older service sends no cents', () => { + const legacy = { ...team, monthlyPriceCentsPerSeat: null, annualPriceCentsPerSeat: null } + expect(perSeatMonthlyCents(legacy, 'month')).toBeNull() + expect(periodTotalCents(legacy, 'year', 2)).toBeNull() + }) + + it('formats whole and fractional dollars', () => { + expect(formatCents(5000, 'en-US')).toBe('$50') + expect(formatCents(75, 'en-US')).toBe('$0.75') + expect(formatCents(144000, 'en-US')).toBe('$1,440') + }) + + it('clamps seats to 1..max', () => { + expect(clampSeats(0, 500)).toBe(1) + expect(clampSeats(Number.NaN, 500)).toBe(1) + expect(clampSeats(900, 500)).toBe(500) + expect(clampSeats(3.6, 500)).toBe(4) + }) +}) + +describe('plans page store', () => { + it('opens from any entry point and closes', () => { + openPlansPage('sandbox') + expect(getPlansPageState()).toEqual({ open: true, source: 'sandbox' }) + closePlansPage() + expect(getPlansPageState().open).toBe(false) + }) +}) diff --git a/src/renderer/src/components/billing/plans/plans-pricing.ts b/src/renderer/src/components/billing/plans/plans-pricing.ts new file mode 100644 index 00000000000..5e94f0aac1b --- /dev/null +++ b/src/renderer/src/components/billing/plans/plans-pricing.ts @@ -0,0 +1,56 @@ +// Price math for the plans page. Every number comes from the billing service's offers. +import type { OrcaBillingInterval, OrcaBillingOffers } from '../../../../../shared/orca-billing' + +type TeamOffer = OrcaBillingOffers['team'] + +/** What one seat costs per month on this interval (annual shown as its monthly equivalent). */ +export function perSeatMonthlyCents(team: TeamOffer, interval: OrcaBillingInterval): number | null { + if (interval === 'month') { + return team.monthlyPriceCentsPerSeat + } + return team.annualPriceCentsPerSeat === null + ? null + : Math.round(team.annualPriceCentsPerSeat / 12) +} + +/** The amount billed each period for `seats` seats. */ +export function periodTotalCents( + team: TeamOffer, + interval: OrcaBillingInterval, + seats: number +): number | null { + const perSeat = + interval === 'month' ? team.monthlyPriceCentsPerSeat : team.annualPriceCentsPerSeat + return perSeat === null ? null : perSeat * Math.max(1, seats) +} + +/** Whole-percent saving of annual over twelve monthly payments, or null when not cheaper. */ +export function annualSavingsPercent(team: TeamOffer): number | null { + const monthly = team.monthlyPriceCentsPerSeat + const annual = team.annualPriceCentsPerSeat + if (monthly === null || annual === null || monthly <= 0) { + return null + } + const percent = Math.round((1 - annual / (monthly * 12)) * 100) + return percent > 0 ? percent : null +} + +/** "$50", "$40", "$0.75", "$1,200" in the given locale. */ +export function formatCents(cents: number, locale: string): string { + const whole = cents % 100 === 0 + return new Intl.NumberFormat(locale, { + style: 'currency', + currency: 'USD', + minimumFractionDigits: whole ? 0 : 2, + maximumFractionDigits: whole ? 0 : 2 + }).format(cents / 100) +} + +export const MIN_SEATS = 1 + +export function clampSeats(value: number, max: number): number { + if (!Number.isFinite(value)) { + return MIN_SEATS + } + return Math.min(max, Math.max(MIN_SEATS, Math.round(value))) +} diff --git a/src/renderer/src/components/billing/plans/use-org-seat-count.ts b/src/renderer/src/components/billing/plans/use-org-seat-count.ts new file mode 100644 index 00000000000..a64bca3c338 --- /dev/null +++ b/src/renderer/src/components/billing/plans/use-org-seat-count.ts @@ -0,0 +1,32 @@ +import { useEffect, useState } from 'react' + +/** + * Seats Team bills at Checkout: the active org's members (pending invites excluded), or 1 for a + * personal account. Billing re-reads the count from auth; this is only for showing the total. + */ +export function useOrgSeatCount(orgId: string | null): number { + const [members, setMembers] = useState<{ orgId: string; count: number } | null>(null) + + useEffect(() => { + if (!orgId) { + return + } + let disposed = false + void window.api.orcaProfiles + .orgMembersList({ orgId }) + .then((result) => { + if (!disposed && result.status === 'ok') { + setMembers({ orgId, count: result.roster.members.length }) + } + }) + .catch(() => undefined) + return () => { + disposed = true + } + }, [orgId]) + + if (!orgId) { + return 1 + } + return members?.orgId === orgId ? Math.max(1, members.count) : 1 +} diff --git a/src/renderer/src/components/billing/register-plan-billing-menu-item.test.ts b/src/renderer/src/components/billing/register-plan-billing-menu-item.test.ts new file mode 100644 index 00000000000..534d06c7b35 --- /dev/null +++ b/src/renderer/src/components/billing/register-plan-billing-menu-item.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it, vi } from 'vitest' + +const openSettingsTarget = vi.fn() +const openSettingsPage = vi.fn() +vi.mock('@/store/index', () => ({ + useAppStore: { getState: () => ({ openSettingsTarget, openSettingsPage }) } +})) + +import { + getRegisteredAccountMenuItems, + selectVisibleAccountMenuItems +} from '../sidebar/account-menu/account-menu-items' +import { BILLING_CAPABILITY, registerPlanBillingMenuItem } from './register-plan-billing-menu-item' +import { getPlansPageState } from './plans/plans-page-store' + +describe('Plan & billing and Team account menu items', () => { + it('register behind the billing capability and open Orca account settings', () => { + const dispose = registerPlanBillingMenuItem() + const items = getRegisteredAccountMenuItems() + expect(items.map((item) => [item.id, item.label(), item.requiredCapability])).toEqual([ + ['orca.plan-billing', 'Plan & billing', BILLING_CAPABILITY], + ['orca.team', 'Team', BILLING_CAPABILITY] + ]) + items[0]?.onSelect() + expect(getPlansPageState()).toEqual({ open: true, source: 'account-menu' }) + items[1]?.onSelect() + expect(openSettingsTarget).toHaveBeenCalledWith({ pane: 'orca-account', repoId: null }) + expect(openSettingsPage).toHaveBeenCalled() + + const visible = (flags: Record): string[] => + selectVisibleAccountMenuItems(getRegisteredAccountMenuItems(), { + activeProfileId: 'p1', + configured: true, + state: 'connected', + persistence: 'encrypted', + capabilities: { flags, refreshedAt: 0 } + }).map((entry) => entry.id) + expect(visible({})).toEqual([]) + expect(visible({ [BILLING_CAPABILITY]: true })).toEqual(['orca.plan-billing', 'orca.team']) + + dispose() + expect(getRegisteredAccountMenuItems()).toHaveLength(0) + }) +}) diff --git a/src/renderer/src/components/billing/register-plan-billing-menu-item.ts b/src/renderer/src/components/billing/register-plan-billing-menu-item.ts new file mode 100644 index 00000000000..67c0333424c --- /dev/null +++ b/src/renderer/src/components/billing/register-plan-billing-menu-item.ts @@ -0,0 +1,38 @@ +import { CreditCard, Users } from 'lucide-react' +import { translate } from '@/i18n/i18n' +import { useAppStore } from '@/store/index' +import { registerAccountMenuItem } from '../sidebar/account-menu/account-menu-items' +import { openPlansPage } from './plans/plans-page-store' + +/** Cloud capability flag that turns billing on per account (server-controlled rollout). */ +export const BILLING_CAPABILITY = 'billing' + +function openAccountSettings(): void { + const state = useAppStore.getState() + state.openSettingsTarget({ pane: 'orca-account', repoId: null }) + state.openSettingsPage() +} + +/** Account menu: "Plan & billing" opens the plans page; "Team" opens the Orca account settings. */ +export function registerPlanBillingMenuItem(): () => void { + const disposeBilling = registerAccountMenuItem({ + id: 'orca.plan-billing', + label: () => translate('auto.components.billing.menuItem', 'Plan & billing'), + icon: CreditCard, + order: 10, + requiredCapability: BILLING_CAPABILITY, + onSelect: () => openPlansPage('account-menu') + }) + const disposeTeam = registerAccountMenuItem({ + id: 'orca.team', + label: () => translate('auto.components.billing.team.menuItem', 'Team'), + icon: Users, + order: 11, + requiredCapability: BILLING_CAPABILITY, + onSelect: openAccountSettings + }) + return () => { + disposeBilling() + disposeTeam() + } +} diff --git a/src/renderer/src/components/billing/team/TeamMembersSection.tsx b/src/renderer/src/components/billing/team/TeamMembersSection.tsx new file mode 100644 index 00000000000..4e4630f3408 --- /dev/null +++ b/src/renderer/src/components/billing/team/TeamMembersSection.tsx @@ -0,0 +1,113 @@ +import { useState } from 'react' +import { useConfirmationDialog } from '@/components/confirmation-dialog-context' +import { useTranslate } from '@/i18n/use-translate' +import type { OrcaProfileOrgMemberMutationResult } from '../../../../../shared/orca-profiles' +import { useOrcaBillingStatus } from '../use-orca-billing-status' +import { TeamMembersView } from './TeamMembersView' +import { mutationFailureCopy } from './team-roles' +import { useOrgRoster } from './use-org-roster' + +/** + * Team members for the active org: list, invite by email (accepted at Orca's sign-in provider), + * change roles, remove. Membership lives in Orca Cloud auth; Team seats follow it automatically + * (auth pushes removals, billing re-reads the count), so nothing here touches Stripe. + */ +export function TeamMembersSection({ + orgId, + viewerUserId +}: { + orgId: string + viewerUserId: string | null +}): React.JSX.Element { + const t = useTranslate() + const confirm = useConfirmationDialog() + const roster = useOrgRoster(orgId) + const billing = useOrcaBillingStatus(true) + const [busy, setBusy] = useState(false) + const [notice, setNotice] = useState(null) + + const run = async ( + action: () => Promise, + success: string + ): Promise => { + setBusy(true) + setNotice(null) + const outcome = await action().catch((error: unknown): OrcaProfileOrgMemberMutationResult => ({ + status: 'failed', + error: String(error) + })) + setBusy(false) + setNotice(outcome.status === 'ok' ? success : mutationFailureCopy(outcome, t)) + if (outcome.status === 'ok') { + roster.refresh() + billing.refresh() + } + } + + const entitlement = billing.result?.status === 'ok' ? billing.result.entitlement : null + const billedSeats = entitlement && entitlement.plan !== null ? entitlement.seats : null + + return ( + { + roster.refresh() + billing.refresh() + }} + onInvite={(email, role) => + void run( + () => window.api.orcaProfiles.orgMemberInvite({ orgId, email, role }), + t( + 'auto.components.billing.team.invited', + 'Invite sent to {{value0}}. They join (and become a billed seat) when they accept.', + { value0: email } + ) + ) + } + onRevoke={(email) => + void run( + () => window.api.orcaProfiles.orgInviteRevoke({ orgId, email }), + t('auto.components.billing.team.revoked', 'Invite revoked.') + ) + } + onChangeRole={(member, role) => { + if (member.userId) { + const userId = member.userId + void run( + () => window.api.orcaProfiles.orgMemberChangeRole({ orgId, userId, role }), + t('auto.components.billing.team.roleChanged', 'Role updated.') + ) + } + }} + onRemove={(member) => { + const userId = member.userId + if (!userId) { + return + } + void confirm({ + title: t('auto.components.billing.team.removeTitle', 'Remove {{value0}}?', { + value0: member.displayName ?? member.email + }), + description: t( + 'auto.components.billing.team.removeDescription', + 'They lose access to the org right away. Their seat is removed and the unused time is credited on your next invoice.' + ), + confirmLabel: t('auto.components.billing.team.removeConfirm', 'Remove member'), + confirmVariant: 'destructive' + }).then((accepted) => { + if (accepted) { + void run( + () => window.api.orcaProfiles.orgMemberRemove({ orgId, userId }), + t('auto.components.billing.team.removed', 'Member removed.') + ) + } + }) + }} + /> + ) +} diff --git a/src/renderer/src/components/billing/team/TeamMembersView.fixture.tsx b/src/renderer/src/components/billing/team/TeamMembersView.fixture.tsx new file mode 100644 index 00000000000..53e4644efe9 --- /dev/null +++ b/src/renderer/src/components/billing/team/TeamMembersView.fixture.tsx @@ -0,0 +1,56 @@ +import { defineFixture } from '../../../component-fixtures/component-fixture' +import type { OrcaOrgMembersRoster } from '../../../../../shared/orca-profiles' +import { TeamMembersView } from './TeamMembersView' + +const roster: OrcaOrgMembersRoster = { + members: [ + { userId: 'u1', email: 'ada@acme.dev', displayName: 'Ada Lovelace', role: 'owner' }, + { userId: 'u2', email: 'grace@acme.dev', displayName: 'Grace Hopper', role: 'admin' }, + { userId: 'u3', email: 'linus@acme.dev', role: 'member' }, + { userId: null, email: 'sso-only@acme.dev', role: 'member' } + ], + pendingInvites: [ + { email: 'margaret@acme.dev', role: 'member', createdAt: Date.UTC(2026, 10, 7) } + ], + viewerRole: 'owner', + canManageMembers: true +} + +const handlers = { + loading: false, + busy: false, + onRefresh: () => {}, + onInvite: () => {}, + onRevoke: () => {}, + onChangeRole: () => {}, + onRemove: () => {} +} + +export const ownerView = defineFixture({ + width: 600, + render: () => ( + + ) +}) + +export const memberView = defineFixture({ + width: 600, + render: () => ( + + ) +}) diff --git a/src/renderer/src/components/billing/team/TeamMembersView.tsx b/src/renderer/src/components/billing/team/TeamMembersView.tsx new file mode 100644 index 00000000000..3decb50169d --- /dev/null +++ b/src/renderer/src/components/billing/team/TeamMembersView.tsx @@ -0,0 +1,265 @@ +import { useState } from 'react' +import { Mail, RefreshCw, UserMinus, UserPlus } from 'lucide-react' +import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue +} from '@/components/ui/select' +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import type { + OrcaOrgMember, + OrcaOrgRole, + OrcaProfileOrgMembersListResult +} from '../../../../../shared/orca-profiles' +import { assignableRoles, canActOnRole, roleLabel } from './team-roles' + +export type TeamMembersViewProps = { + result: OrcaProfileOrgMembersListResult | null + loading: boolean + busy: boolean + notice: string | null + /** Seats Team bills (from billing), or null when there is no plan or billing is off. */ + billedSeats: number | null + viewerUserId: string | null + onRefresh: () => void + onInvite: (email: string, role: OrcaOrgRole) => void + onRevoke: (email: string) => void + onChangeRole: (member: OrcaOrgMember, role: OrcaOrgRole) => void + onRemove: (member: OrcaOrgMember) => void +} + +const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ + +function RoleSelect({ + value, + roles, + disabled, + onChange, + label +}: { + value: OrcaOrgRole + roles: OrcaOrgRole[] + disabled: boolean + onChange: (role: OrcaOrgRole) => void + label: string +}): React.JSX.Element { + const t = useTranslate() + return ( + + ) +} + +export function TeamMembersView(props: TeamMembersViewProps): React.JSX.Element { + const t = useTranslate() + const { result, loading, busy, notice, billedSeats } = props + const [email, setEmail] = useState('') + const [inviteRole, setInviteRole] = useState('member') + + const header = ( +
+

+ {t('auto.components.billing.team.title', 'Team')} +

+ +
+ ) + + if (!result || result.status !== 'ok') { + return ( +
+ {header} +

+ {result === null + ? t('auto.components.billing.team.loading', 'Loading your team…') + : t('auto.components.billing.team.loadFailed', 'Could not load your team. Try again.')} +

+
+ ) + } + + const roster = result.roster + const manager = roster.canManageMembers + const roles = assignableRoles(roster.viewerRole) + const emailValid = EMAIL.test(email.trim()) + + return ( +
+ {header} +

+ {billedSeats === null + ? t('auto.components.billing.team.memberCount', '{{value0}} members', { + value0: String(roster.members.length) + }) + : t( + 'auto.components.billing.team.memberSeats', + '{{value0}} members · {{value1}} seats billed. Seats follow membership; pending invites are free until accepted.', + { value0: String(roster.members.length), value1: String(billedSeats) } + )} +

+ +
    + {roster.members.map((member) => { + const self = member.userId !== null && member.userId === props.viewerUserId + const actionable = + manager && + !self && + member.userId !== null && + canActOnRole(roster.viewerRole, member.role) + return ( +
  • +
    +

    + {member.displayName ?? member.email} + {self ? ( + + {t('auto.components.billing.team.you', '(you)')} + + ) : null} +

    + {member.displayName ? ( +

    {member.email}

    + ) : null} +
    + {actionable ? ( + props.onChangeRole(member, role)} + label={t('auto.components.billing.team.roleFor', 'Role for {{value0}}', { + value0: member.email + })} + /> + ) : ( + {roleLabel(member.role, t)} + )} + {actionable ? ( + + ) : null} +
  • + ) + })} +
+ + {manager && roster.pendingInvites.length > 0 ? ( +
+

+ {t('auto.components.billing.team.pending', 'Pending invites')} +

+
    + {roster.pendingInvites.map((invite) => ( +
  • + +

    {invite.email}

    + {roleLabel(invite.role, t)} + {canActOnRole(roster.viewerRole, invite.role) ? ( + + ) : null} +
  • + ))} +
+
+ ) : null} + + {manager ? ( +
{ + event.preventDefault() + if (emailValid) { + props.onInvite(email.trim(), inviteRole) + setEmail('') + } + }} + > +
+ setEmail(event.target.value)} + aria-label={t('auto.components.billing.team.inviteEmail', 'Email to invite')} + /> +
+ + + + ) : ( +

+ {t( + 'auto.components.billing.team.askAdmin', + 'Only owners and admins can invite or remove members.' + )} +

+ )} + + {notice ?

{notice}

: null} +
+ ) +} diff --git a/src/renderer/src/components/billing/team/team-roles.test.ts b/src/renderer/src/components/billing/team/team-roles.test.ts new file mode 100644 index 00000000000..c903cbe9e46 --- /dev/null +++ b/src/renderer/src/components/billing/team/team-roles.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest' +import { assignableRoles, canActOnRole, canManageMembers } from './team-roles' + +describe('team roles (mirrors auth org-roles)', () => { + it('lets owners act on everyone, admins on admins and members, members on no one', () => { + expect(canActOnRole('owner', 'owner')).toBe(true) + expect(canActOnRole('admin', 'owner')).toBe(false) + expect(canActOnRole('admin', 'admin')).toBe(true) + expect(canActOnRole('admin', 'member')).toBe(true) + expect(canActOnRole('member', 'member')).toBe(false) + expect(canManageMembers('member')).toBe(false) + }) + + it('offers only assignable roles', () => { + expect(assignableRoles('owner')).toEqual(['owner', 'admin', 'member']) + expect(assignableRoles('admin')).toEqual(['admin', 'member']) + expect(assignableRoles('member')).toEqual([]) + }) +}) diff --git a/src/renderer/src/components/billing/team/team-roles.ts b/src/renderer/src/components/billing/team/team-roles.ts new file mode 100644 index 00000000000..505bed8fae4 --- /dev/null +++ b/src/renderer/src/components/billing/team/team-roles.ts @@ -0,0 +1,67 @@ +// Mirrors Orca Cloud auth's role rules (apps/auth/src/accounts/org-roles.ts) so the UI only offers +// actions the server will accept: owner > admin > member; only owners act on owners. +import type { Translate } from '@/i18n/use-translate' +import type { + OrcaOrgRole, + OrcaProfileOrgMemberMutationResult +} from '../../../../../shared/orca-profiles' + +export const ORG_ROLES: readonly OrcaOrgRole[] = ['owner', 'admin', 'member'] + +export function canManageMembers(role: OrcaOrgRole): boolean { + return role === 'owner' || role === 'admin' +} + +export function canActOnRole(actor: OrcaOrgRole, target: OrcaOrgRole): boolean { + if (!canManageMembers(actor)) { + return false + } + return target !== 'owner' || actor === 'owner' +} + +/** Roles the viewer may assign (to an invite or an existing member). */ +export function assignableRoles(actor: OrcaOrgRole): OrcaOrgRole[] { + return ORG_ROLES.filter((role) => canActOnRole(actor, role)) +} + +export function roleLabel(role: OrcaOrgRole, t: Translate): string { + switch (role) { + case 'owner': + return t('auto.components.billing.team.roleOwner', 'Owner') + case 'admin': + return t('auto.components.billing.team.roleAdmin', 'Admin') + case 'member': + return t('auto.components.billing.team.roleMember', 'Member') + } +} + +export function mutationFailureCopy( + result: OrcaProfileOrgMemberMutationResult, + t: Translate +): string | null { + switch (result.status) { + case 'ok': + return null + case 'conflict': + return result.reason === 'already_member' + ? t('auto.components.billing.team.alreadyMember', 'That person is already a member.') + : t('auto.components.billing.team.alreadyInvited', 'That person already has an invite.') + case 'invalid': + return result.reason === 'cannot_remove_self' + ? t('auto.components.billing.team.cannotRemoveSelf', 'You cannot remove yourself.') + : t('auto.components.billing.team.cannotChangeOwnRole', 'You cannot change your own role.') + case 'forbidden': + return t( + 'auto.components.billing.team.forbidden', + 'Only owners and admins can manage members, and only owners can manage owners.' + ) + case 'not-found': + return t('auto.components.billing.team.notFound', 'That member or invite no longer exists.') + case 'reconnect-required': + return t('auto.components.billing.openSignedOut', 'Sign in to Orca again to manage billing.') + case 'unconfigured': + return t('auto.components.billing.openUnconfigured', 'Billing is unavailable in this build.') + case 'failed': + return t('auto.components.billing.team.failed', 'Could not update the team. Try again.') + } +} diff --git a/src/renderer/src/components/billing/team/use-org-roster.ts b/src/renderer/src/components/billing/team/use-org-roster.ts new file mode 100644 index 00000000000..ee0869fd5c6 --- /dev/null +++ b/src/renderer/src/components/billing/team/use-org-roster.ts @@ -0,0 +1,35 @@ +import { useCallback, useEffect, useState } from 'react' +import type { OrcaProfileOrgMembersListResult } from '../../../../../shared/orca-profiles' + +type RosterState = { loading: boolean; result: OrcaProfileOrgMembersListResult | null } + +function readRoster(orgId: string): Promise { + return window.api.orcaProfiles + .orgMembersList({ orgId }) + .catch((error: unknown): OrcaProfileOrgMembersListResult => ({ + status: 'failed', + error: error instanceof Error ? error.message : String(error) + })) +} + +/** The org's live roster (members, pending invites, viewer role), re-read on `refresh()`. */ +export function useOrgRoster(orgId: string): RosterState & { refresh: () => void } { + const [state, setState] = useState({ loading: true, result: null }) + const [reloadKey, setReloadKey] = useState(0) + const refresh = useCallback(() => setReloadKey((key) => key + 1), []) + + useEffect(() => { + let disposed = false + setState((previous) => ({ ...previous, loading: true })) + void readRoster(orgId).then((result) => { + if (!disposed) { + setState({ loading: false, result }) + } + }) + return () => { + disposed = true + } + }, [orgId, reloadKey]) + + return { ...state, refresh } +} diff --git a/src/renderer/src/components/billing/use-orca-billing-status.ts b/src/renderer/src/components/billing/use-orca-billing-status.ts new file mode 100644 index 00000000000..465a745b341 --- /dev/null +++ b/src/renderer/src/components/billing/use-orca-billing-status.ts @@ -0,0 +1,54 @@ +import { useCallback, useEffect, useState } from 'react' +import type { OrcaBillingStatusResult } from '../../../../shared/orca-billing' + +type BillingStatusState = { + loading: boolean + result: OrcaBillingStatusResult | null +} + +function readBillingStatus(): Promise { + return window.api.orcaProfiles + .billingStatus() + .catch((error: unknown): OrcaBillingStatusResult => ({ + status: 'failed', + error: error instanceof Error ? error.message : String(error) + })) +} + +/** + * Reads the billing status while `enabled`, again on `refresh()`, and whenever the window regains + * focus (the user comes back from Checkout or the Portal in their browser). + */ +export function useOrcaBillingStatus( + enabled: boolean +): BillingStatusState & { refresh: () => void } { + const [state, setState] = useState({ loading: enabled, result: null }) + const [reloadKey, setReloadKey] = useState(0) + const refresh = useCallback(() => setReloadKey((key) => key + 1), []) + + useEffect(() => { + if (!enabled) { + return + } + let disposed = false + let latest = 0 + const load = (): void => { + const request = ++latest + setState((previous) => ({ ...previous, loading: true })) + void readBillingStatus().then((result) => { + // A newer request, or unmounting, supersedes this one. + if (!disposed && request === latest) { + setState({ loading: false, result }) + } + }) + } + load() + window.addEventListener('focus', load) + return () => { + disposed = true + window.removeEventListener('focus', load) + } + }, [enabled, reloadKey]) + + return { ...state, refresh } +} diff --git a/src/renderer/src/components/new-workspace/RunTargetCombobox.tsx b/src/renderer/src/components/new-workspace/RunTargetCombobox.tsx index a655c4c9e1f..f66c82e5530 100644 --- a/src/renderer/src/components/new-workspace/RunTargetCombobox.tsx +++ b/src/renderer/src/components/new-workspace/RunTargetCombobox.tsx @@ -203,7 +203,9 @@ export default function RunTargetCombobox({ ) const fieldLabel = selectedRecipe - ? `${getEphemeralVmLabel(t)} / ${selectedRecipe.name}` + ? selectedRecipe.provider + ? selectedRecipe.name + : `${getEphemeralVmLabel(t)} / ${selectedRecipe.name}` : (selectedHost?.label ?? '') const fieldDetail = selectedRecipe ? getRecipeDetail(selectedRecipe, t) diff --git a/src/renderer/src/components/new-workspace/run-target-options.ts b/src/renderer/src/components/new-workspace/run-target-options.ts index 47a65e5f79d..bbbfee753a7 100644 --- a/src/renderer/src/components/new-workspace/run-target-options.ts +++ b/src/renderer/src/components/new-workspace/run-target-options.ts @@ -45,6 +45,10 @@ export function getRecipeDestroyLabel( } export function getRecipeDetail(recipe: EphemeralVmRecipeOption, t?: Translate): string { + // Built-in providers run in-process; their lifecycle strings are labels, not commands to show. + if (recipe.provider && recipe.description) { + return recipe.description + } return `${getRecipeCommandDisplay(recipe.create)} · ${getRecipeDestroyLabel(recipe, t)}` } diff --git a/src/renderer/src/components/settings/accounts/orca/OrcaAccountSettingsPane.tsx b/src/renderer/src/components/settings/accounts/orca/OrcaAccountSettingsPane.tsx index c4c5645da55..b086409dc40 100644 --- a/src/renderer/src/components/settings/accounts/orca/OrcaAccountSettingsPane.tsx +++ b/src/renderer/src/components/settings/accounts/orca/OrcaAccountSettingsPane.tsx @@ -9,6 +9,8 @@ import { useTranslate, type Translate } from '@/i18n/use-translate' import { useAppStore } from '@/store/index' import { OrcaProfileSignOutConfirmDialog } from '../../../orca-profiles/OrcaProfileSignOutConfirmDialog' import { OrcaSignInButton, OrcaSignInWaitNotice } from '../../../OrcaSignInProgress' +import { PlanBillingSection } from '../../../billing/PlanBillingSection' +import { TeamMembersSection } from '../../../billing/team/TeamMembersSection' function accountStatusCopy( state: 'local' | 'unconfigured' | 'connected' | 'reconnect-required' | undefined, @@ -128,6 +130,14 @@ export function OrcaAccountSettingsPane(): React.JSX.Element { {connected ? null : }
+ {connected ? : null} + {connected && authStatus?.cloud?.activeOrgId ? ( + + ) : null} +

{t('auto.components.settings.orcaAccount.benefitsTitle', 'Included with your account')} diff --git a/src/renderer/src/components/sidebar/SidebarSettingsHelpMenu.tsx b/src/renderer/src/components/sidebar/SidebarSettingsHelpMenu.tsx deleted file mode 100644 index 7bf57319fe7..00000000000 --- a/src/renderer/src/components/sidebar/SidebarSettingsHelpMenu.tsx +++ /dev/null @@ -1,330 +0,0 @@ -import React, { useState } from 'react' -import { - BookOpen, - CircleHelp, - ExternalLink, - Github, - Keyboard, - Loader2, - MessageSquareText, - RefreshCw, - RotateCw, - School, - ScrollText, - Settings -} from 'lucide-react' -import { toast } from 'sonner' -import logo from '../../../../../resources/logo.svg' -import { useAppStore } from '@/store' -import { Button } from '@/components/ui/button' -import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuSeparator, - DropdownMenuTrigger -} from '@/components/ui/dropdown-menu' -import { useMountedRef } from '@/hooks/useMountedRef' -import { useShortcutKeyDetails } from '@/hooks/useShortcutLabel' -import { ShortcutKeyCombo } from '@/components/ShortcutKeyCombo' -import { showOnboardingFromRenderer } from '../onboarding/show-onboarding-event' -import { SetupGuideProgressRing } from '../setup-guide/SetupGuideProgressRing' -import { useSetupGuideProgress } from '../setup-guide/use-setup-guide-progress' -import { lazyWithRetry } from '@/lib/lazy-with-retry' -import type * as SidebarFeedbackDialogModule from './sidebar-feedback/SidebarFeedbackDialog' -import { useTranslate } from '@/i18n/use-translate' -import { getUpdateCheckClickOptions, getUpdateCheckHint } from '@/lib/update-check-click-options' -import { openAppHttpLink } from '@/lib/browser/navigation/app-http-link-open' - -// Keep feedback off the boot graph; menu warming and selection share the same lazy module entry. -const loadSidebarFeedbackDialog = (): Promise => - import('./sidebar-feedback/SidebarFeedbackDialog') - -const SidebarFeedbackDialog = lazyWithRetry( - () => loadSidebarFeedbackDialog().then((module) => ({ default: module.SidebarFeedbackDialog })), - { reloadKey: 'sidebar-feedback-dialog' } -) - -const DOCS_URL = 'https://www.onorca.dev/docs' -const CHANGELOG_URL = 'https://onorca.dev/changelog' -const GITHUB_URL = 'https://github.com/stablyai/orca' -const DISCORD_URL = 'https://discord.gg/fzjDKHxv8Q' -const X_URL = 'https://x.com/orca_build' -const NO_UPDATE_CHECK_MODIFIERS = { - altKey: false, - ctrlKey: false, - metaKey: false, - shiftKey: false -} - -function DiscordIcon(): React.JSX.Element { - return ( - - ) -} - -function XIcon(): React.JSX.Element { - return ( - - ) -} - -function ExternalMenuItem({ - label, - url, - icon -}: { - label: string - url: string - icon: React.ReactNode -}): React.JSX.Element { - return ( - openAppHttpLink(url)}> - {icon} - {label} - - - ) -} - -export function SidebarSettingsHelpMenu(): React.JSX.Element { - const t = useTranslate() - const openModal = useAppStore((s) => s.openModal) - const openSettingsPage = useAppStore((s) => s.openSettingsPage) - const openSettingsTarget = useAppStore((s) => s.openSettingsTarget) - const updateStatus = useAppStore((s) => s.updateStatus) - const setupProgress = useSetupGuideProgress(true, false, false) - - const settingsShortcut = useShortcutKeyDetails('app.settings') - const [menuOpen, setMenuOpen] = useState(false) - const [feedbackOpen, setFeedbackOpen] = useState(false) - // Retain the mounted dialog through its closing animation. - const [feedbackDialogMounted, setFeedbackDialogMounted] = useState(false) - const [isRestartingOrca, setIsRestartingOrca] = useState(false) - const lastShowOnboardingAtRef = React.useRef(0) - const updateCheckModifiersRef = React.useRef(NO_UPDATE_CHECK_MODIFIERS) - const mountedRef = useMountedRef() - const updateCheckHint = getUpdateCheckHint() - - const showMilestones = - setupProgress.ready && setupProgress.coreDoneCount < setupProgress.coreTotal - - const handleMenuOpenChange = (open: boolean): void => { - setMenuOpen(open) - updateCheckModifiersRef.current = NO_UPDATE_CHECK_MODIFIERS - if (open) { - // Warm while users read the menu so feedback is loaded before selection. - void loadSidebarFeedbackDialog().catch(() => {}) - } - } - - const handleOpenFeedback = (): void => { - setFeedbackDialogMounted(true) - setFeedbackOpen(true) - } - - const handleShowOnboarding = (): void => { - const now = Date.now() - if (now - lastShowOnboardingAtRef.current < 500) { - return - } - lastShowOnboardingAtRef.current = now - void showOnboardingFromRenderer() - } - - const handleRestartOrca = (): void => { - if (isRestartingOrca) { - return - } - setIsRestartingOrca(true) - toast.info(t('auto.components.sidebar.SidebarSettingsHelpMenu.5161eef55d', 'Restarting Orca…')) - void window.api.app.restart().catch((error) => { - if (mountedRef.current) { - setIsRestartingOrca(false) - toast.error( - t('auto.components.sidebar.SidebarSettingsHelpMenu.4e8f5710d3', "Couldn't restart Orca."), - { - description: error instanceof Error ? error.message : undefined - } - ) - } - }) - } - - const openShortcutsSettings = (): void => { - openSettingsTarget({ pane: 'shortcuts', repoId: null }) - openSettingsPage() - } - - const handleCheckForUpdatesPointerDown = (event: React.PointerEvent): void => { - updateCheckModifiersRef.current = { - altKey: event.altKey, - ctrlKey: event.ctrlKey, - metaKey: event.metaKey, - shiftKey: event.shiftKey - } - } - - const handleCheckForUpdates = (): void => { - const modifiers = updateCheckModifiersRef.current - updateCheckModifiersRef.current = NO_UPDATE_CHECK_MODIFIERS - void window.api.updater.check(getUpdateCheckClickOptions(modifiers)) - } - - const openMilestones = (): void => { - openModal('setup-guide', { telemetrySource: 'help_menu' }) - } - - return ( - <> -

- - - - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.a428c25998', 'Settings')} - {settingsShortcut.keys.length > 0 ? ( - - ) : null} - - - - - - - - - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.2991a0106c', 'Help')} - - - - - - {t( - 'auto.components.sidebar.SidebarSettingsHelpMenu.e565171a7c', - 'Keyboard Shortcuts' - )} - - - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.4cf5b868d7', 'Send Feedback')} - - {showMilestones ? ( - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.f8a2c91d4e', 'Milestones')} - - - ) : null} - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.b7e4d2a19c', 'Onboarding')} - - } - /> - } - /> - - } - /> - openAppHttpLink(DISCORD_URL)}> - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.eb9884e55b', 'Discord')} - - - openAppHttpLink(X_URL)}> - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.c4f8e1b72a', 'X')} - - - - - {updateStatus.state === 'checking' ? ( - - ) : ( - - )} - {t('auto.components.sidebar.SidebarSettingsHelpMenu.29c56f30ee', 'Check for Updates')} - - - - - {t('auto.components.sidebar.SidebarSettingsHelpMenu.ad3d3ed7f1', 'Restart Orca')} - - - -
- {feedbackDialogMounted ? ( - - - - ) : null} - - ) -} diff --git a/src/renderer/src/components/sidebar/SidebarToolbar.fixture.tsx b/src/renderer/src/components/sidebar/SidebarToolbar.fixture.tsx new file mode 100644 index 00000000000..8724aa2ce31 --- /dev/null +++ b/src/renderer/src/components/sidebar/SidebarToolbar.fixture.tsx @@ -0,0 +1,80 @@ +import { defineFixture } from '../../component-fixtures/component-fixture' +import type { OrcaProfileAuthStatus } from '../../../../shared/orca-profiles' +import SidebarToolbar from './SidebarToolbar' + +const settle = (ms = 300): Promise => new Promise((resolve) => setTimeout(resolve, ms)) + +const openAccountMenu = async (frame: HTMLElement): Promise => { + frame + .querySelector('[data-account-menu-trigger]') + ?.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true, button: 0 })) + await settle() +} + +const openHelpSubmenu = async (frame: HTMLElement): Promise => { + await openAccountMenu(frame) + const help = Array.from( + document.querySelectorAll('[data-slot="dropdown-menu-sub-trigger"]') + ).find((element) => element.textContent?.includes('Help')) + help?.focus() + help?.dispatchEvent(new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true })) + await settle() +} + +const signedIn: OrcaProfileAuthStatus = { + activeProfileId: 'local-default', + configured: true, + state: 'connected', + persistence: 'encrypted', + cloud: { + cloudProfileId: 'fixture', + userId: 'fixture-user', + email: 'ada@example.com', + displayName: 'Ada Lovelace', + linkedAt: 0 + } +} + +const signedOut: OrcaProfileAuthStatus = { + activeProfileId: 'local-default', + configured: true, + state: 'local', + persistence: 'encrypted' +} + +const footer = ({ + width = 303, + height = 96, + authStatus = signedOut, + interact +}: { + width?: number + height?: number + authStatus?: OrcaProfileAuthStatus + interact?: (frame: HTMLElement) => Promise +}) => + defineFixture({ + width, + store: { persistedUIReady: true, orcaProfileAuthStatus: authStatus }, + interact, + render: () => ( +
+ {}} /> +
+ ) + }) + +export const signedOutFooter = footer({}) +export const signedInFooter = footer({ authStatus: signedIn }) +export const narrowSignedInFooter = footer({ width: 220, authStatus: signedIn }) +export const signedOutMenu = footer({ height: 260, interact: openAccountMenu }) +export const signedInMenu = footer({ height: 260, authStatus: signedIn, interact: openAccountMenu }) +export const helpSubmenu = footer({ + width: 560, + height: 480, + authStatus: signedIn, + interact: openHelpSubmenu +}) diff --git a/src/renderer/src/components/sidebar/SidebarToolbar.tsx b/src/renderer/src/components/sidebar/SidebarToolbar.tsx index ca55a74416b..a88f7526ea6 100644 --- a/src/renderer/src/components/sidebar/SidebarToolbar.tsx +++ b/src/renderer/src/components/sidebar/SidebarToolbar.tsx @@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next' import { Button } from '@/components/ui/button' import { Tooltip, TooltipTrigger, TooltipContent } from '@/components/ui/tooltip' import { ScrollToCurrentWorkspaceToolbarButton } from './worktree-list/navigation/ScrollToCurrentWorkspaceToolbarButton' -import { SidebarSettingsHelpMenu } from './SidebarSettingsHelpMenu' +import { SidebarAccountMenu } from './account-menu/SidebarAccountMenu' import { useTranslate } from '@/i18n/use-translate' import { useAppStore } from '@/store' import { hasFeatureInteraction } from '../../../../shared/feature-interactions' @@ -69,7 +69,7 @@ const SidebarToolbar = React.memo(function SidebarToolbar({
- +
diff --git a/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.test.tsx b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.test.tsx new file mode 100644 index 00000000000..1bbf4bf0627 --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.test.tsx @@ -0,0 +1,138 @@ +// @vitest-environment happy-dom +import '@testing-library/jest-dom/vitest' +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, expect, it, vi } from 'vitest' +import { useAppStore } from '@/store' +import { TooltipProvider } from '@/components/ui/tooltip' +import type { OrcaProfileAuthStatus } from '../../../../../shared/orca-profiles' +import { registerAccountMenuItem } from './account-menu-items' +import { SidebarAccountMenu } from './SidebarAccountMenu' + +vi.mock('../../setup-guide/use-setup-guide-progress', () => ({ + useSetupGuideProgress: () => ({ ready: false, coreDoneCount: 0, coreTotal: 0 }) +})) + +const initialState = useAppStore.getState() +const disposers: (() => void)[] = [] + +const connected: OrcaProfileAuthStatus = { + activeProfileId: 'local-default', + configured: true, + state: 'connected', + persistence: 'encrypted', + cloud: { + cloudProfileId: 'cp', + userId: 'u', + email: 'ada@example.com', + displayName: 'Ada Lovelace', + linkedAt: 0 + }, + capabilities: { flags: { 'team-admin': true }, refreshedAt: 0 } +} + +afterEach(() => { + cleanup() + for (const dispose of disposers.splice(0)) { + dispose() + } + useAppStore.setState(initialState, true) + vi.restoreAllMocks() +}) + +function renderOpenMenu(): HTMLElement { + render( + + + + ) + const trigger = screen.getByRole('button', { name: 'Account' }) + fireEvent.pointerDown(trigger, { button: 0, pointerType: 'mouse', ctrlKey: false }) + return trigger +} + +it('groups the account header, Settings, and Help, and opens Settings', () => { + const openSettingsPage = vi.fn() + useAppStore.setState({ orcaProfileAuthStatus: connected, openSettingsPage }) + renderOpenMenu() + + expect(screen.getByRole('menuitem', { name: /Ada Lovelace/ })).toHaveTextContent( + 'ada@example.com' + ) + expect(screen.getByRole('menuitem', { name: 'Help' })).toBeInTheDocument() + fireEvent.click(screen.getByRole('menuitem', { name: 'Settings' })) + expect(openSettingsPage).toHaveBeenCalledTimes(1) +}) + +it('starts sign-in from the header when signed out', () => { + const connectCurrentOrcaProfile = vi.fn(async () => null) + useAppStore.setState({ + orcaProfileAuthStatus: { + activeProfileId: 'local-default', + configured: true, + state: 'local', + persistence: 'encrypted' + }, + connectCurrentOrcaProfile + }) + renderOpenMenu() + + fireEvent.click(screen.getByRole('menuitem', { name: /Sign in/ })) + expect(connectCurrentOrcaProfile).toHaveBeenCalledTimes(1) +}) + +it('renders registered items only when their capability flag is on', () => { + const onSelect = vi.fn() + disposers.push( + registerAccountMenuItem({ + id: 'gated', + label: () => 'Gated item', + requiredCapability: 'team-admin', + onSelect + }), + registerAccountMenuItem({ + id: 'hidden', + label: () => 'Hidden item', + requiredCapability: 'not-granted', + onSelect: vi.fn() + }) + ) + useAppStore.setState({ orcaProfileAuthStatus: connected }) + renderOpenMenu() + + expect(screen.queryByRole('menuitem', { name: 'Hidden item' })).not.toBeInTheDocument() + fireEvent.click(screen.getByRole('menuitem', { name: 'Gated item' })) + expect(onSelect).toHaveBeenCalledTimes(1) +}) + +it('hides capability-gated items while signed out', () => { + disposers.push( + registerAccountMenuItem({ + id: 'gated', + label: () => 'Gated item', + requiredCapability: 'team-admin', + onSelect: vi.fn() + }) + ) + useAppStore.setState({ + orcaProfileAuthStatus: { ...connected, state: 'reconnect-required' } + }) + renderOpenMenu() + + expect(screen.getByRole('menuitem', { name: /Sign in again/ })).toBeInTheDocument() + expect(screen.queryByRole('menuitem', { name: 'Gated item' })).not.toBeInTheDocument() +}) + +it('omits the account header in builds without Orca sign-in', () => { + useAppStore.setState({ + orcaProfileAuthStatus: { + activeProfileId: 'local-default', + configured: false, + state: 'unconfigured', + persistence: 'none' + } + }) + renderOpenMenu() + + expect(screen.queryByRole('menuitem', { name: /Sign in/ })).not.toBeInTheDocument() + expect(screen.getByRole('menuitem', { name: 'Settings' })).toBeInTheDocument() +}) diff --git a/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx new file mode 100644 index 00000000000..089779bb6f5 --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx @@ -0,0 +1,221 @@ +import React, { useState } from 'react' +import { Settings, UserRound } from 'lucide-react' +import { toast } from 'sonner' +import { useAppStore } from '@/store' +import { Button } from '@/components/ui/button' +import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuShortcut, + DropdownMenuTrigger +} from '@/components/ui/dropdown-menu' +import { useMountedRef } from '@/hooks/useMountedRef' +import { useOptionalShortcutLabel } from '@/hooks/useShortcutLabel' +import { useTranslate } from '@/i18n/use-translate' +import { cn } from '@/lib/utils' +import { useSetupGuideProgress } from '../../setup-guide/use-setup-guide-progress' +import { getAccountMenuIdentity, type AccountMenuIdentity } from './account-menu-identity' +import { selectVisibleAccountMenuItems, useRegisteredAccountMenuItems } from './account-menu-items' +import { + loadSidebarFeedbackDialog, + SidebarFeedbackDialog, + SidebarHelpSubmenu +} from './SidebarHelpSubmenu' + +function AccountAvatar({ + identity, + className +}: { + identity: AccountMenuIdentity + className?: string +}): React.JSX.Element { + return ( + + ) +} + +export function SidebarAccountMenu(): React.JSX.Element { + const t = useTranslate() + const authStatus = useAppStore((s) => s.orcaProfileAuthStatus) + const signingIn = useAppStore((s) => Boolean(s.orcaProfileSignInProgress)) + const connectCurrentOrcaProfile = useAppStore((s) => s.connectCurrentOrcaProfile) + const openSettingsPage = useAppStore((s) => s.openSettingsPage) + const openSettingsTarget = useAppStore((s) => s.openSettingsTarget) + const setupProgress = useSetupGuideProgress(true, false, false) + const settingsShortcut = useOptionalShortcutLabel('app.settings') + const registeredItems = useRegisteredAccountMenuItems() + const [menuOpen, setMenuOpen] = useState(false) + const [feedbackOpen, setFeedbackOpen] = useState(false) + // Retain the mounted dialog through its closing animation. + const [feedbackDialogMounted, setFeedbackDialogMounted] = useState(false) + const [isRestartingOrca, setIsRestartingOrca] = useState(false) + const mountedRef = useMountedRef() + + const identity = getAccountMenuIdentity(authStatus) + const extensionItems = selectVisibleAccountMenuItems(registeredItems, authStatus) + const milestones = + setupProgress.ready && setupProgress.coreDoneCount < setupProgress.coreTotal + ? { done: setupProgress.coreDoneCount, total: setupProgress.coreTotal } + : null + const accountLabel = t('auto.components.sidebar.SidebarAccountMenu.account', 'Account') + + const handleMenuOpenChange = (open: boolean): void => { + setMenuOpen(open) + if (open) { + // Warm while users read the menu so feedback is loaded before selection. + void loadSidebarFeedbackDialog().catch(() => {}) + } + } + + const openAccountSettings = (): void => { + openSettingsTarget({ pane: 'orca-account', repoId: null }) + openSettingsPage() + } + + // Builds without Orca sign-in (and the unloaded state) have no account to show or connect. + const showAccountHeader = identity.signedIn || authStatus?.configured === true + + const handleAccountHeaderSelect = (): void => { + // The account pane hosts sign-out and the pending sign-in link. + if (identity.signedIn || signingIn) { + openAccountSettings() + return + } + void connectCurrentOrcaProfile() + } + + const handleOpenFeedback = (): void => { + setFeedbackDialogMounted(true) + setFeedbackOpen(true) + } + + const handleRestartOrca = (): void => { + if (isRestartingOrca) { + return + } + setIsRestartingOrca(true) + toast.info(t('auto.components.sidebar.SidebarSettingsHelpMenu.5161eef55d', 'Restarting Orca…')) + void window.api.app.restart().catch((error) => { + if (mountedRef.current) { + setIsRestartingOrca(false) + toast.error( + t('auto.components.sidebar.SidebarSettingsHelpMenu.4e8f5710d3', "Couldn't restart Orca."), + { + description: error instanceof Error ? error.message : undefined + } + ) + } + }) + } + + return ( + <> + + + + + + + + + {identity.signedIn ? identity.name : accountLabel} + + + + {showAccountHeader ? ( + <> + + + + {identity.signedIn ? ( + <> + {identity.name} + {identity.name !== identity.email ? ( + + {identity.email} + + ) : null} + + ) : ( + <> + + {identity.needsReconnect + ? t('auto.components.settings.orcaAccount.signInAgain', 'Sign in again') + : t('auto.components.sidebar.SidebarAccountMenu.signIn', 'Sign in')} + + + {signingIn + ? t('auto.components.OrcaSignInProgress.signingIn', 'Signing in…') + : t('auto.components.settings.orcaAccount.account', 'Orca account')} + + + )} + + + + + ) : null} + {extensionItems.length > 0 ? ( + <> + {extensionItems.map((item) => { + const Icon = item.icon + return ( + + {Icon ? : null} + {item.label()} + + ) + })} + + + ) : null} + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.a428c25998', 'Settings')} + {settingsShortcut ? ( + {settingsShortcut} + ) : null} + + + + + {feedbackDialogMounted ? ( + + + + ) : null} + + ) +} diff --git a/src/renderer/src/components/sidebar/account-menu/SidebarHelpSubmenu.tsx b/src/renderer/src/components/sidebar/account-menu/SidebarHelpSubmenu.tsx new file mode 100644 index 00000000000..fc9e95a4fff --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/SidebarHelpSubmenu.tsx @@ -0,0 +1,232 @@ +import React from 'react' +import { + BookOpen, + CircleHelp, + ExternalLink, + Github, + Keyboard, + Loader2, + MessageSquareText, + RefreshCw, + RotateCw, + School, + ScrollText +} from 'lucide-react' +import logo from '../../../../../../resources/logo.svg' +import { useAppStore } from '@/store' +import { + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuSub, + DropdownMenuSubContent, + DropdownMenuSubTrigger +} from '@/components/ui/dropdown-menu' +import { showOnboardingFromRenderer } from '../../onboarding/show-onboarding-event' +import { SetupGuideProgressRing } from '../../setup-guide/SetupGuideProgressRing' +import { lazyWithRetry } from '@/lib/lazy-with-retry' +import type * as SidebarFeedbackDialogModule from '../sidebar-feedback/SidebarFeedbackDialog' +import { useTranslate } from '@/i18n/use-translate' +import { getUpdateCheckClickOptions, getUpdateCheckHint } from '@/lib/update-check-click-options' +import { openAppHttpLink } from '@/lib/browser/navigation/app-http-link-open' + +// Keep feedback off the boot graph; menu warming and selection share the same lazy module entry. +export const loadSidebarFeedbackDialog = (): Promise => + import('../sidebar-feedback/SidebarFeedbackDialog') + +export const SidebarFeedbackDialog = lazyWithRetry( + () => loadSidebarFeedbackDialog().then((module) => ({ default: module.SidebarFeedbackDialog })), + { reloadKey: 'sidebar-feedback-dialog' } +) + +const DOCS_URL = 'https://www.onorca.dev/docs' +const CHANGELOG_URL = 'https://onorca.dev/changelog' +const GITHUB_URL = 'https://github.com/stablyai/orca' +const DISCORD_URL = 'https://discord.gg/fzjDKHxv8Q' +const X_URL = 'https://x.com/orca_build' +const NO_UPDATE_CHECK_MODIFIERS = { + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false +} + +function DiscordIcon(): React.JSX.Element { + return ( + + ) +} + +function XIcon(): React.JSX.Element { + return ( + + ) +} + +function ExternalMenuItem({ + label, + url, + icon +}: { + label: string + url: string + icon: React.ReactNode +}): React.JSX.Element { + return ( + openAppHttpLink(url)}> + {icon} + {label} + + + ) +} + +export type SidebarHelpMilestones = { done: number; total: number } + +/** Every entry the sidebar's former "?" menu offered, now nested under Help in the account menu. */ +export function SidebarHelpSubmenu({ + milestones, + onOpenFeedback, + restartPending, + onRestart +}: { + milestones: SidebarHelpMilestones | null + onOpenFeedback: () => void + restartPending: boolean + onRestart: () => void +}): React.JSX.Element { + const t = useTranslate() + const openModal = useAppStore((s) => s.openModal) + const openSettingsPage = useAppStore((s) => s.openSettingsPage) + const openSettingsTarget = useAppStore((s) => s.openSettingsTarget) + const updateStatus = useAppStore((s) => s.updateStatus) + const lastShowOnboardingAtRef = React.useRef(0) + const updateCheckModifiersRef = React.useRef(NO_UPDATE_CHECK_MODIFIERS) + const updateCheckHint = getUpdateCheckHint() + + const handleShowOnboarding = (): void => { + const now = Date.now() + if (now - lastShowOnboardingAtRef.current < 500) { + return + } + lastShowOnboardingAtRef.current = now + void showOnboardingFromRenderer() + } + + const openShortcutsSettings = (): void => { + openSettingsTarget({ pane: 'shortcuts', repoId: null }) + openSettingsPage() + } + + const handleCheckForUpdatesPointerDown = (event: React.PointerEvent): void => { + updateCheckModifiersRef.current = { + altKey: event.altKey, + ctrlKey: event.ctrlKey, + metaKey: event.metaKey, + shiftKey: event.shiftKey + } + } + + const handleCheckForUpdates = (): void => { + const modifiers = updateCheckModifiersRef.current + updateCheckModifiersRef.current = NO_UPDATE_CHECK_MODIFIERS + void window.api.updater.check(getUpdateCheckClickOptions(modifiers)) + } + + const openMilestones = (): void => { + openModal('setup-guide', { telemetrySource: 'help_menu' }) + } + + return ( + + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.2991a0106c', 'Help')} + + + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.e565171a7c', 'Keyboard Shortcuts')} + + + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.4cf5b868d7', 'Send Feedback')} + + {milestones ? ( + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.f8a2c91d4e', 'Milestones')} + + + ) : null} + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.b7e4d2a19c', 'Onboarding')} + + } + /> + } + /> + + } + /> + } + /> + } + /> + + + {updateStatus.state === 'checking' ? ( + + ) : ( + + )} + {t('auto.components.sidebar.SidebarSettingsHelpMenu.29c56f30ee', 'Check for Updates')} + + + + + {t('auto.components.sidebar.SidebarSettingsHelpMenu.ad3d3ed7f1', 'Restart Orca')} + + + + ) +} diff --git a/src/renderer/src/components/sidebar/account-menu/account-menu-identity.ts b/src/renderer/src/components/sidebar/account-menu/account-menu-identity.ts new file mode 100644 index 00000000000..a803249901f --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/account-menu-identity.ts @@ -0,0 +1,18 @@ +import type { OrcaProfileAuthStatus } from '../../../../../shared/orca-profiles' + +export type AccountMenuIdentity = + | { signedIn: false; needsReconnect: boolean } + | { signedIn: true; initial: string; name: string; email: string } + +export function getAccountMenuIdentity( + authStatus: OrcaProfileAuthStatus | null +): AccountMenuIdentity { + const cloud = authStatus?.cloud + if (authStatus?.state !== 'connected' || !cloud) { + return { signedIn: false, needsReconnect: authStatus?.state === 'reconnect-required' } + } + const name = cloud.displayName?.trim() || cloud.email + // Array.from keeps a leading astral character (e.g. emoji) whole instead of half a surrogate pair. + const initial = (Array.from(name.trim())[0] ?? '?').toLocaleUpperCase() + return { signedIn: true, initial, name, email: cloud.email } +} diff --git a/src/renderer/src/components/sidebar/account-menu/account-menu-items.test.ts b/src/renderer/src/components/sidebar/account-menu/account-menu-items.test.ts new file mode 100644 index 00000000000..a52c004498d --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/account-menu-items.test.ts @@ -0,0 +1,43 @@ +import { afterEach, expect, it } from 'vitest' +import { + getRegisteredAccountMenuItems, + registerAccountMenuItem, + selectVisibleAccountMenuItems, + type AccountMenuItem +} from './account-menu-items' + +const disposers: (() => void)[] = [] + +afterEach(() => { + for (const dispose of disposers.splice(0)) { + dispose() + } +}) + +function register(id: string, order?: number): () => void { + const entry: AccountMenuItem = { id, label: () => id, order, onSelect: () => {} } + const dispose = registerAccountMenuItem(entry) + disposers.push(dispose) + return dispose +} + +function visibleIds(): string[] { + return selectVisibleAccountMenuItems(getRegisteredAccountMenuItems(), null).map( + (entry) => entry.id + ) +} + +it('sorts by order, keeping registration order for ties', () => { + register('b', 10) + register('a', 10) + register('c', 5) + expect(visibleIds()).toEqual(['c', 'b', 'a']) +}) + +it('replaces an entry with the same id and ignores the replaced entry disposer', () => { + const disposeOriginal = register('item', 1) + register('other', 2) + register('item', 3) + disposeOriginal() + expect(visibleIds()).toEqual(['other', 'item']) +}) diff --git a/src/renderer/src/components/sidebar/account-menu/account-menu-items.ts b/src/renderer/src/components/sidebar/account-menu/account-menu-items.ts new file mode 100644 index 00000000000..c2b6dbc6c0f --- /dev/null +++ b/src/renderer/src/components/sidebar/account-menu/account-menu-items.ts @@ -0,0 +1,63 @@ +import { useSyncExternalStore } from 'react' +import type { ComponentType } from 'react' +import type { OrcaProfileAuthStatus } from '../../../../../shared/orca-profiles' + +export type AccountMenuItem = { + /** Stable id; registering the same id again replaces the earlier entry. */ + id: string + /** Called at render time so registered labels follow the active locale. */ + label: () => string + icon?: ComponentType<{ className?: string }> + /** Lower sorts first; ties keep registration order. */ + order?: number + /** Hide the item unless the signed-in account reports this capability flag as true. */ + requiredCapability?: string + onSelect: () => void +} + +let items: readonly AccountMenuItem[] = [] +const listeners = new Set<() => void>() + +function emit(): void { + for (const listener of listeners) { + listener() + } +} + +/** Adds an entry to the sidebar account menu; returns a disposer that removes exactly this entry. */ +export function registerAccountMenuItem(item: AccountMenuItem): () => void { + items = [...items.filter((existing) => existing.id !== item.id), item] + emit() + return () => { + if (!items.includes(item)) { + return + } + items = items.filter((existing) => existing !== item) + emit() + } +} + +function subscribe(listener: () => void): () => void { + listeners.add(listener) + return () => listeners.delete(listener) +} + +export function getRegisteredAccountMenuItems(): readonly AccountMenuItem[] { + return items +} + +export function selectVisibleAccountMenuItems( + registered: readonly AccountMenuItem[], + authStatus: OrcaProfileAuthStatus | null +): AccountMenuItem[] { + const flags = authStatus?.state === 'connected' ? authStatus.capabilities?.flags : undefined + return registered + .filter((item) => !item.requiredCapability || flags?.[item.requiredCapability] === true) + .map((item, index) => ({ item, index })) + .sort((a, b) => (a.item.order ?? 0) - (b.item.order ?? 0) || a.index - b.index) + .map(({ item }) => item) +} + +export function useRegisteredAccountMenuItems(): readonly AccountMenuItem[] { + return useSyncExternalStore(subscribe, getRegisteredAccountMenuItems) +} diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index 8633e53f48e..3d265637c65 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -2034,6 +2034,10 @@ "errors": { "cleanupInterrupted": "Cleanup was interrupted. Retry cleanup to continue.", "cleanupStopped": "Cleanup stopped by user.", + "cloudConcurrencyLimit": "You already have as many Orca Cloud workspaces running as your plan allows. Sleep or delete one, then try again.", + "cloudPlanRequired": "Orca Cloud workspaces need a plan with cloud hours. Open Plan & billing to choose one.", + "cloudSignInRequired": "Sign in to Orca to use Orca Cloud workspaces.", + "cloudUnavailable": "Orca Cloud is unavailable right now. Try again in a moment.", "defaultStartRefUnresolved": "Could not resolve a default provisioned-root start ref.", "destroyDisabled": "Destroy is disabled for this recipe.", "destroyExited": "Destroy exited with code {{exitCode}}.", diff --git a/src/renderer/src/i18n/locales/de.json b/src/renderer/src/i18n/locales/de.json index 6ef3112131e..c02f719dc08 100644 --- a/src/renderer/src/i18n/locales/de.json +++ b/src/renderer/src/i18n/locales/de.json @@ -6272,6 +6272,10 @@ "selectedProjects_one": "{{count}} Projekt", "selectedProjects_other": "{{count}} Projekte" }, + "SidebarAccountMenu": { + "account": "Konto", + "signIn": "Anmelden" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Orca neu starten", "29c56f30ee": "Nach Updates suchen", diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 9a2acc7c060..91145faf6bd 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -2149,6 +2149,12 @@ "backgroundFailed": "Environment checks failed for a new VM workspace. Open it in the sidebar to continue.", "openedDegraded": "Creating the workspace on a VM whose environment checks failed.", "unreachable": "The environment checks could not finish: {{error}}" + }, + "orcaCloudSession": { + "restartSoon": "Cloud workspace restarts in {{minutes}} min", + "restartSoonDetail": "{{name}} reaches its 24-hour session limit. Files stay; running agents pick up where they left off.", + "thisWorkspace": "This workspace", + "seePlans": "See plans" } }, "hooks": { @@ -6581,6 +6587,10 @@ "selectedProjects_one": "{{count}} project", "selectedProjects_other": "{{count}} projects" }, + "SidebarAccountMenu": { + "account": "Account", + "signIn": "Sign in" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Restart Orca", "29c56f30ee": "Check for Updates", @@ -19524,6 +19534,146 @@ "setting-global-off": "Folder trust is turned off in Agents settings, so agents always ask.", "keyword-trust": "trust", "keyword-hooks": "hooks" + }, + "billing": { + "reasonNoPlan": "Hosted sandboxes need a paid plan.", + "reasonInactive": "Your plan is not active. Choose a plan to start sandboxes.", + "reasonPastDue": "Your last payment failed. Update your payment method to start new sandboxes.", + "openForbidden": "Only an org owner or admin can manage billing.", + "openAlreadySubscribed": "This account already has a plan. Use Manage billing to change it.", + "openNoAccount": "There is no billing account yet. Choose a plan first.", + "openSignedOut": "Sign in to Orca again to manage billing.", + "openUnconfigured": "Billing is unavailable in this build.", + "openFailed": "Could not reach billing. Try again.", + "hoursLeft": "{{value0}} hours left", + "usageLabel": "Included hours used", + "endsOn": "Plan ends {{value0}}", + "renewsOn": "Renews {{value0}}", + "continueInBrowser": "Continue in your browser. This updates when you come back.", + "title": "Plan & billing", + "refresh": "Refresh plan", + "loadFailed": "Could not load your plan. Try again.", + "askAdmin": "Ask an org owner or admin to change the plan.", + "menuItem": "Plan & billing", + "statusActive": "Active", + "statusTrial": "Trial", + "statusPastDue": "Payment due", + "statusCanceled": "Canceled", + "statusInactive": "Inactive", + "reasonExhaustedOptIn": "You have used all included hours this month. Turn on overage to keep starting sandboxes.", + "reasonCapRaise": "You reached this month’s overage spend cap. An admin can raise it.", + "capTooHigh": "That spend cap is above the allowed maximum.", + "overageNoPlan": "Overage needs an active plan.", + "hoursUsedMonth": "{{value0}} of {{value1}} hours used this month", + "warn100": "All included hours for this month are used.", + "warnPercent": "You have used {{value0}}% of this month’s included hours.", + "resets": "Hours reset {{value0}}", + "overageTitle": "Overage", + "overageDescription": "Keep starting sandboxes after included hours at {{value0}}/hr Standard ({{value1}}/hr Large).", + "overageToggle": "Allow overage", + "overageSpent": "{{value0}} of {{value1}} monthly cap spent", + "capLabel": "Monthly cap ($)", + "saveCap": "Save", + "monthly": "Monthly", + "annual": "Annual", + "seatsInterval": "{{value0}} seats · {{value1}}", + "paymentSuspended": "Sandboxes suspended: payment overdue", + "paymentFailed": "Payment failed", + "paymentSuspendedBody": "Running sandboxes were suspended and new ones cannot start. Update your payment method; everything resumes once the invoice is paid.", + "paymentGraceBody": "New sandboxes are paused. Running sandboxes will be suspended on {{value0}} unless the payment goes through. Stripe will retry the card and email your billing contact.", + "updatePayment": "Update payment method", + "reasonGraceExpired": "Your payment is overdue, so sandboxes are suspended. Update your payment method to resume.", + "team": { + "menuItem": "Team", + "invited": "Invite sent to {{value0}}. They join (and become a billed seat) when they accept.", + "revoked": "Invite revoked.", + "roleChanged": "Role updated.", + "removeTitle": "Remove {{value0}}?", + "removeDescription": "They lose access to the org right away. Their seat is removed and the unused time is credited on your next invoice.", + "removeConfirm": "Remove member", + "removed": "Member removed.", + "title": "Team", + "refresh": "Refresh team", + "loading": "Loading your team…", + "loadFailed": "Could not load your team. Try again.", + "memberCount": "{{value0}} members", + "memberSeats": "{{value0}} members · {{value1}} seats billed. Seats follow membership; pending invites are free until accepted.", + "you": "(you)", + "roleFor": "Role for {{value0}}", + "remove": "Remove {{value0}}", + "pending": "Pending invites", + "revoke": "Revoke", + "invitePlaceholder": "teammate@company.com", + "inviteEmail": "Email to invite", + "inviteRole": "Role for the invite", + "invite": "Invite", + "askAdmin": "Only owners and admins can invite or remove members.", + "roleOwner": "Owner", + "roleAdmin": "Admin", + "roleMember": "Member", + "alreadyMember": "That person is already a member.", + "alreadyInvited": "That person already has an invite.", + "cannotRemoveSelf": "You cannot remove yourself.", + "cannotChangeOwnRole": "You cannot change your own role.", + "forbidden": "Only owners and admins can manage members, and only owners can manage owners.", + "notFound": "That member or invite no longer exists.", + "failed": "Could not update the team. Try again." + }, + "manageBilling": "Manage billing", + "choosePlan": "Choose a plan", + "comparePlans": "Compare plans", + "plans": { + "entByoc": "Run sandboxes in your own cloud account", + "entSso": "SSO, SCIM provisioning and audit logs", + "entNetwork": "Network policy and egress controls", + "entTerms": "Custom limits, invoicing and terms", + "entPriced": "Priced for your organization", + "contactSales": "Contact sales", + "eyebrow": "Orca Cloud", + "title": "Cloud sandboxes for your whole team", + "monthly": "Monthly", + "annual": "Annual", + "billingPeriod": "Billing period", + "vpRunningTitle": "Agents keep running", + "vpWakeTitle": "Wake in seconds", + "vpWakeBody": "Idle sandboxes sleep to save hours and resume right where they left off.", + "vpSecretsTitle": "Secrets never stored in the VM", + "loading": "Loading plans…", + "loadFailed": "Could not load plans. Try again in a moment.", + "featureConcurrency": "{{value0}} running sandboxes per seat", + "featureOverage": "Optional overage at {{value0}}/hr ({{value1}}/hr Large) with a monthly spend cap", + "currentPlan": "Current plan", + "perSeatMonth": "per seat / month", + "billedMonthly": "Billed monthly", + "totalYear": "{{value0}} / year", + "totalMonth": "{{value0}} / month", + "checkout": "Continue to checkout", + "seatsFollow": "Seats follow your team: new members add a seat, removals credit your next invoice.", + "entLead": "Everything in Team, plus:", + "entHint": "Tell us about your team and where you run.", + "titleSubscribed": "Your Orca Cloud plan", + "subtitleSubscribed": "Usage this month, your plan, and what Enterprise adds.", + "subtitle2": "Run Orca agents in hosted sandboxes that keep going when you step away.", + "saveShort": "−{{value0}}%", + "vpRunningBody2": "Sandboxes keep working while you are away and report back when they finish.", + "vpSecretsBody2": "Credentials are injected into outbound requests and never touch the VM’s disk.", + "seatsAndPeriod": "Seats: {{value0}} · {{value1}}", + "leftResets": "{{value0}} hours left · resets {{value1}}", + "retry": "Try again", + "footHours": "Hours reset every month, including on annual plans. When they run out, new sandboxes wait until the next month unless overage is on.", + "footTerms": "Prices in USD, before tax. Overage stays off until an owner or admin turns it on and sets a spend cap. Cancel anytime; your plan runs to the end of the period.", + "featureHours2": "{{value0}} Standard hours (4 vCPU / 8 GB) per seat each month, pooled across your team", + "featureLarge2": "Large (8 vCPU / 16 GB) uses hours at {{value0}}×", + "featureSleep2": "Sleeps after {{value0}} minutes idle", + "billedYearly": "Billed yearly", + "billedYearlyAmount": "Billed {{value0}} per seat yearly", + "seatsOne": "Seats: {{value0}} (one per team member)", + "waiting": "Waiting for checkout…", + "fixPaymentAbove": "Update your payment method above to keep your plan active.", + "manageHint": "Switch monthly or annual, update payment, or cancel.", + "lockedBody": "Only an owner or admin of your org can choose a plan. Ask one of them to open Plan & billing in Orca." + }, + "teamFrom": "Team starts at {{value0}} per seat per month, billed annually." } }, "i18n": { @@ -24316,6 +24466,9 @@ "cleanupStopped": "Cleanup stopped by user.", "cleanupInterrupted": "Cleanup was interrupted. Retry cleanup to continue.", "sshTargetRemoveFailed": "Failed to remove the hidden SSH target.", + "cloudPlanRequired": "Orca Cloud workspaces need a plan with cloud hours. Open Plan & billing to choose one.", + "cloudSignInRequired": "Sign in to Orca to use Orca Cloud workspaces.", + "cloudUnavailable": "Orca Cloud is unavailable right now. Try again in a moment.", "recipeExitedWithoutCode": "Recipe exited with code unknown.", "destroyExitedWithoutCode": "Destroy exited with code unknown.", "suspendExited": "Suspend exited with code {{exitCode}}.", @@ -24323,7 +24476,8 @@ "resumeExited": "Resume exited with code {{exitCode}}.", "resumeExitedWithoutCode": "Resume exited with code unknown.", "recipeTimedOut": "The recipe {{mode}} step did not finish within {{timeout}}, so Orca stopped it. Check the recipe output, or raise timeoutSeconds in orca.yaml if it needs longer.", - "provisionInterrupted": "Orca stopped while this VM was being created. Destroy it to release any provider resources." + "provisionInterrupted": "Orca stopped while this VM was being created. Destroy it to release any provider resources.", + "cloudConcurrencyLimit": "You already have as many Orca Cloud workspaces running as your plan allows. Sleep or delete one, then try again." } }, "installDirAcl": { diff --git a/src/renderer/src/i18n/locales/es.json b/src/renderer/src/i18n/locales/es.json index 99b87eef29e..05edc458bd7 100644 --- a/src/renderer/src/i18n/locales/es.json +++ b/src/renderer/src/i18n/locales/es.json @@ -5872,6 +5872,10 @@ "selectedProjects_other": "{{count}} proyectos", "selectedProjects_many": "{{count}} proyectos" }, + "SidebarAccountMenu": { + "account": "Cuenta", + "signIn": "Iniciar sesión" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Reiniciar Orca", "29c56f30ee": "Buscar actualizaciones", diff --git a/src/renderer/src/i18n/locales/fr.json b/src/renderer/src/i18n/locales/fr.json index 1c9b7e6bc21..1a02de9fc16 100644 --- a/src/renderer/src/i18n/locales/fr.json +++ b/src/renderer/src/i18n/locales/fr.json @@ -6112,6 +6112,10 @@ "selectedProjects_other": "{{count}} projets", "selectedProjects_many": "{{count}} projets" }, + "SidebarAccountMenu": { + "account": "Compte", + "signIn": "Se connecter" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Redémarrer Orca", "29c56f30ee": "Rechercher des mises à jour", diff --git a/src/renderer/src/i18n/locales/ja.json b/src/renderer/src/i18n/locales/ja.json index 4b50f6f899b..692998a2139 100644 --- a/src/renderer/src/i18n/locales/ja.json +++ b/src/renderer/src/i18n/locales/ja.json @@ -5626,6 +5626,10 @@ "allProjects": "すべてのプロジェクト", "selectedProjects_other": "{{count}} 件のプロジェクト" }, + "SidebarAccountMenu": { + "account": "アカウント", + "signIn": "サインイン" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Orca を再起動", "29c56f30ee": "アップデートをチェックする", diff --git a/src/renderer/src/i18n/locales/ko.json b/src/renderer/src/i18n/locales/ko.json index 1cb5d7959ad..7863064d72c 100644 --- a/src/renderer/src/i18n/locales/ko.json +++ b/src/renderer/src/i18n/locales/ko.json @@ -5626,6 +5626,10 @@ "allProjects": "모든 프로젝트", "selectedProjects_other": "프로젝트 {{count}}개" }, + "SidebarAccountMenu": { + "account": "계정", + "signIn": "로그인" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Orca 재시작", "29c56f30ee": "업데이트 확인", diff --git a/src/renderer/src/i18n/locales/pt.json b/src/renderer/src/i18n/locales/pt.json index 8fd26db8efa..bb42c45bc0d 100644 --- a/src/renderer/src/i18n/locales/pt.json +++ b/src/renderer/src/i18n/locales/pt.json @@ -11960,6 +11960,10 @@ "cloneFolderTakenByRepo": "\"{{value0}}\" nesta pasta já é outro repositório, então este clone precisa da sua própria pasta.", "cloneFolderNotEmpty": "\"{{value0}}\" já existe nesta pasta, então este clone precisa da sua própria pasta." }, + "SidebarAccountMenu": { + "account": "Conta", + "signIn": "Entrar" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Reiniciar o Orca", "29c56f30ee": "Verificar Atualizações", diff --git a/src/renderer/src/i18n/locales/ru.json b/src/renderer/src/i18n/locales/ru.json index 234a841efd0..38242c869e1 100644 --- a/src/renderer/src/i18n/locales/ru.json +++ b/src/renderer/src/i18n/locales/ru.json @@ -6558,6 +6558,10 @@ "selectedProjects_few": "{{count}} проекта", "selectedProjects_many": "{{count}} проектов" }, + "SidebarAccountMenu": { + "account": "Аккаунт", + "signIn": "Войти" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Перезапустить Orca", "29c56f30ee": "Проверить наличие обновлений", diff --git a/src/renderer/src/i18n/locales/tr.json b/src/renderer/src/i18n/locales/tr.json index ff788dba404..943037f8f22 100644 --- a/src/renderer/src/i18n/locales/tr.json +++ b/src/renderer/src/i18n/locales/tr.json @@ -6256,6 +6256,10 @@ "selectedProjects_one": "{{count}} proje", "selectedProjects_other": "{{count}} proje" }, + "SidebarAccountMenu": { + "account": "Hesap", + "signIn": "Oturum aç" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "Orca'yı Yeniden Başlat", "29c56f30ee": "Güncellemeleri Kontrol Et", diff --git a/src/renderer/src/i18n/locales/zh-Hant.json b/src/renderer/src/i18n/locales/zh-Hant.json index 53ab837158d..9217f8cb3a2 100644 --- a/src/renderer/src/i18n/locales/zh-Hant.json +++ b/src/renderer/src/i18n/locales/zh-Hant.json @@ -6112,6 +6112,10 @@ "allProjects": "所有專案", "selectedProjects_other": "{{count}} 個專案" }, + "SidebarAccountMenu": { + "account": "帳戶", + "signIn": "登入" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "重新啟動 Orca", "29c56f30ee": "檢查更新", diff --git a/src/renderer/src/i18n/locales/zh.json b/src/renderer/src/i18n/locales/zh.json index 15f85644cfd..e5daca6c626 100644 --- a/src/renderer/src/i18n/locales/zh.json +++ b/src/renderer/src/i18n/locales/zh.json @@ -5626,6 +5626,10 @@ "allProjects": "所有项目", "selectedProjects_other": "{{count}} 个项目" }, + "SidebarAccountMenu": { + "account": "账户", + "signIn": "登录" + }, "SidebarSettingsHelpMenu": { "ad3d3ed7f1": "重启 Orca", "29c56f30ee": "检查更新", diff --git a/src/renderer/src/lib/ephemeral-vm-error-copy.ts b/src/renderer/src/lib/ephemeral-vm-error-copy.ts index 6e169290cad..6a7abc5dfd1 100644 --- a/src/renderer/src/lib/ephemeral-vm-error-copy.ts +++ b/src/renderer/src/lib/ephemeral-vm-error-copy.ts @@ -121,7 +121,13 @@ const COPY: Record strin cleanupInterrupted: () => translate('ephemeralVm.errors.cleanupInterrupted', EN.cleanupInterrupted), sshTargetRemoveFailed: () => - translate('ephemeralVm.errors.sshTargetRemoveFailed', EN.sshTargetRemoveFailed) + translate('ephemeralVm.errors.sshTargetRemoveFailed', EN.sshTargetRemoveFailed), + cloudPlanRequired: () => translate('ephemeralVm.errors.cloudPlanRequired', EN.cloudPlanRequired), + cloudConcurrencyLimit: () => + translate('ephemeralVm.errors.cloudConcurrencyLimit', EN.cloudConcurrencyLimit), + cloudSignInRequired: () => + translate('ephemeralVm.errors.cloudSignInRequired', EN.cloudSignInRequired), + cloudUnavailable: () => translate('ephemeralVm.errors.cloudUnavailable', EN.cloudUnavailable) } function isEphemeralVmFailureCode(code: string): code is EphemeralVmFailureCode { diff --git a/src/renderer/src/lib/ephemeral-vm-workspace-target.ts b/src/renderer/src/lib/ephemeral-vm-workspace-target.ts index a735b615577..ad0f768f2a1 100644 --- a/src/renderer/src/lib/ephemeral-vm-workspace-target.ts +++ b/src/renderer/src/lib/ephemeral-vm-workspace-target.ts @@ -47,6 +47,8 @@ export type PrepareEphemeralVmWorkspaceTargetResult = | { ok: false error: string + /** Catalogued failure code, e.g. cloudPlanRequired for the Orca Cloud upsell. */ + errorCode?: string stderr: string } @@ -63,7 +65,12 @@ export async function prepareEphemeralVmWorkspaceTarget( ...(args.provisionId ? { provisionId: args.provisionId } : {}) }) if (!provisioned.ok) { - return { ok: false, error: ephemeralVmErrorText(provisioned), stderr: provisioned.stderr } + return { + ok: false, + error: ephemeralVmErrorText(provisioned), + ...(provisioned.errorCode ? { errorCode: provisioned.errorCode } : {}), + stderr: provisioned.stderr + } } const checkoutMode = getEphemeralVmRecipeResultCheckoutMode(provisioned.runtime.recipeResult) diff --git a/src/renderer/src/lib/ephemeral-vm-worktree-creation.ts b/src/renderer/src/lib/ephemeral-vm-worktree-creation.ts index 5347af12a6e..bdff6cb12a7 100644 --- a/src/renderer/src/lib/ephemeral-vm-worktree-creation.ts +++ b/src/renderer/src/lib/ephemeral-vm-worktree-creation.ts @@ -7,6 +7,7 @@ import type { Repo } from '../../../shared/repo-types' import { translate } from '@/i18n/i18n' import { cleanupFailedEphemeralVmWorkspace } from '@/lib/ephemeral-vm-failed-create-cleanup' import { gateVmCreateOnEnvironmentCheck } from '@/lib/ephemeral-vm-environment-check' +import { showOrcaCloudPlanUpsell } from '@/lib/orca-cloud-session-events' import { appendProvisioningLog, appendProvisioningWarnings @@ -73,7 +74,9 @@ async function prepareEphemeralVmRecipeRequest( status: 'error', error: preparedTarget.error }) - if (useAppStore.getState().activePendingCreationId !== creationId) { + if (preparedTarget.errorCode === 'cloudPlanRequired') { + showOrcaCloudPlanUpsell(preparedTarget.error) + } else if (useAppStore.getState().activePendingCreationId !== creationId) { toast.error(preparedTarget.error) } return null diff --git a/src/renderer/src/lib/orca-cloud-session-events.ts b/src/renderer/src/lib/orca-cloud-session-events.ts new file mode 100644 index 00000000000..9f3d404003b --- /dev/null +++ b/src/renderer/src/lib/orca-cloud-session-events.ts @@ -0,0 +1,56 @@ +// Window side of Orca Cloud's session lifecycle: cap warnings and idle sleep requests from main. +import { toast } from 'sonner' +import { translate } from '@/i18n/i18n' +import { useAppStore } from '@/store' +import { findWorktreeById } from '@/store/github/refresh/worktree-refresh' +import { runSleepWorktree } from '@/components/sidebar/workspace-actions/sleep/sleep-worktree-flow' +import { openPlansPage } from '@/components/billing/plans/plans-page-store' + +export function installOrcaCloudSessionEvents(): () => void { + const api = window.api?.ephemeralVm + if (!api?.onCloudSessionWarning || !api.onCloudIdleSleepRequested) { + return () => {} + } + const disposeWarning = api.onCloudSessionWarning(({ workspaceName, minutesLeft }) => { + toast.warning( + translate( + 'auto.lib.orcaCloudSession.restartSoon', + 'Cloud workspace restarts in {{minutes}} min', + { + minutes: minutesLeft + } + ), + { + duration: 20_000, + description: translate( + 'auto.lib.orcaCloudSession.restartSoonDetail', + '{{name}} reaches its 24-hour session limit. Files stay; running agents pick up where they left off.', + { + name: + workspaceName ?? + translate('auto.lib.orcaCloudSession.thisWorkspace', 'This workspace') + } + ) + } + ) + }) + const disposeIdle = api.onCloudIdleSleepRequested(({ workspaceId }) => { + if (workspaceId && findWorktreeById(useAppStore.getState(), workspaceId)) { + void runSleepWorktree(workspaceId) + } + }) + return () => { + disposeWarning() + disposeIdle() + } +} + +/** "See plans" on the create failure when the account has no plan with cloud hours. */ +export function showOrcaCloudPlanUpsell(message: string): void { + toast.error(message, { + action: { + label: translate('auto.lib.orcaCloudSession.seePlans', 'See plans'), + onClick: () => openPlansPage('sandbox') + } + }) +} diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index f57ca3035ab..1abd8805c3b 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -31,11 +31,17 @@ import { handleGuestWindowCloseMessage } from './components/browser-pane/host-gu import { installOsFileDropCancellationGuard } from './lib/os-file-drop-cancellation-guard' import { installBodyOverlayAttributes } from './lib/body-overlay-attributes' import { applyWindowMaterialToDocument } from './lib/window-material-runtime' +import { registerPlanBillingMenuItem } from './components/billing/register-plan-billing-menu-item' +import { installOrcaCloudSessionEvents } from './lib/orca-cloud-session-events' const disposeOsFileDropGuard = installOsFileDropCancellationGuard() import.meta.hot?.dispose(disposeOsFileDropGuard) const disposeBodyOverlayAttributes = installBodyOverlayAttributes() import.meta.hot?.dispose(disposeBodyOverlayAttributes) +const disposePlanBillingMenuItem = registerPlanBillingMenuItem() +import.meta.hot?.dispose(disposePlanBillingMenuItem) +const disposeOrcaCloudSessionEvents = installOrcaCloudSessionEvents() +import.meta.hot?.dispose(disposeOrcaCloudSessionEvents) recordRendererCrashBreadcrumb('renderer_bootstrap_started', { dev: import.meta.env.DEV }) installRendererCrashDiagnostics() installTypingLatencyDiagnostic() diff --git a/src/renderer/src/web/preload-api/web-orca-profiles-api.ts b/src/renderer/src/web/preload-api/web-orca-profiles-api.ts index 16ff0d1c356..7a06b13026b 100644 --- a/src/renderer/src/web/preload-api/web-orca-profiles-api.ts +++ b/src/renderer/src/web/preload-api/web-orca-profiles-api.ts @@ -69,7 +69,11 @@ export function createWebOrcaProfilesApi(): Partial { orgMemberInvite: async () => ({ status: 'unconfigured' }), orgInviteRevoke: async () => ({ status: 'unconfigured' }), orgMemberChangeRole: async () => ({ status: 'unconfigured' }), - orgMemberRemove: async () => ({ status: 'unconfigured' }) + orgMemberRemove: async () => ({ status: 'unconfigured' }), + billingStatus: async () => ({ status: 'unconfigured' }), + billingOpenCheckout: async () => ({ status: 'unconfigured' }), + billingOpenPortal: async () => ({ status: 'unconfigured' }), + billingSetOverage: async () => ({ status: 'unconfigured' }) } } } diff --git a/src/shared/ephemeral-vm-failure-copy.ts b/src/shared/ephemeral-vm-failure-copy.ts index 604ee17201b..2acbc7d2a1f 100644 --- a/src/shared/ephemeral-vm-failure-copy.ts +++ b/src/shared/ephemeral-vm-failure-copy.ts @@ -35,7 +35,13 @@ export const EPHEMERAL_VM_FAILURE_COPY = { destroyDisabled: 'Destroy is disabled for this recipe.', cleanupStopped: 'Cleanup stopped by user.', cleanupInterrupted: 'Cleanup was interrupted. Retry cleanup to continue.', - sshTargetRemoveFailed: 'Failed to remove the hidden SSH target.' + sshTargetRemoveFailed: 'Failed to remove the hidden SSH target.', + cloudPlanRequired: + 'Orca Cloud workspaces need a plan with cloud hours. Open Plan & billing to choose one.', + cloudConcurrencyLimit: + 'You already have as many Orca Cloud workspaces running as your plan allows. Sleep or delete one, then try again.', + cloudSignInRequired: 'Sign in to Orca to use Orca Cloud workspaces.', + cloudUnavailable: 'Orca Cloud is unavailable right now. Try again in a moment.' } as const export type EphemeralVmFailureCode = keyof typeof EPHEMERAL_VM_FAILURE_COPY diff --git a/src/shared/ephemeral-vm-recipe-step-providers.ts b/src/shared/ephemeral-vm-recipe-step-providers.ts new file mode 100644 index 00000000000..fc6309d8ba8 --- /dev/null +++ b/src/shared/ephemeral-vm-recipe-step-providers.ts @@ -0,0 +1,42 @@ +// In-process lifecycle steps for built-in providers (Orca Cloud). They answer exactly what a recipe +// process would — stdout JSON, stderr progress, an exit code — so records, timeouts, pairing reuse +// and failure copy keep one path. +import type { EphemeralVmRecipeContext } from './ephemeral-vm-recipe-runner' +import type { ProcessRunResult } from './ephemeral-vm-recipe-process' +import type { EphemeralVmFailure } from './ephemeral-vm-failure-copy' +import type { OrcaVmRecipeLifecycleMode, OrcaVmRecipeProviderId } from './orca-yaml-hook-types' + +export type RecipeStepProviderResult = ProcessRunResult & { + /** A catalogued failure that replaces the generic "exited with code" copy. */ + failure?: EphemeralVmFailure +} + +export type RecipeStepProvider = (step: { + mode: OrcaVmRecipeLifecycleMode + repoPath: string + context: EphemeralVmRecipeContext + /** The lifecycle payload a recipe would read on stdin (suspend/resume/destroy). */ + stdin?: string + signal?: AbortSignal + onStderr?: (chunk: string) => void +}) => Promise + +const providers = new Map() + +export function registerEphemeralVmRecipeStepProvider( + id: OrcaVmRecipeProviderId, + provider: RecipeStepProvider +): () => void { + providers.set(id, provider) + return () => { + if (providers.get(id) === provider) { + providers.delete(id) + } + } +} + +export function getEphemeralVmRecipeStepProvider( + id: OrcaVmRecipeProviderId +): RecipeStepProvider | null { + return providers.get(id) ?? null +} diff --git a/src/shared/ephemeral-vm-recipe-step.ts b/src/shared/ephemeral-vm-recipe-step.ts index d3be4316814..d593bd1ebcf 100644 --- a/src/shared/ephemeral-vm-recipe-step.ts +++ b/src/shared/ephemeral-vm-recipe-step.ts @@ -11,6 +11,10 @@ import { type EphemeralVmFailure } from './ephemeral-vm-failure-copy' import { getEphemeralVmRecipeTimeoutSeconds } from './ephemeral-vm-recipe-timeouts' +import { + getEphemeralVmRecipeStepProvider, + type RecipeStepProviderResult +} from './ephemeral-vm-recipe-step-providers' type RecipeStepArgs = Pick< EphemeralVmRecipeStartArgs, @@ -32,6 +36,9 @@ export function runRecipeStep( context: EphemeralVmRecipeContext, stdin?: string ): Promise { + if (args.recipe.provider) { + return runProviderStep(args, mode, context, stdin) + } return runRecipeCommand({ command, repoPath: args.repoPath, @@ -50,11 +57,60 @@ export function runRecipeStep( }) } +async function runProviderStep( + args: RecipeStepArgs, + mode: OrcaVmRecipeLifecycleMode, + context: EphemeralVmRecipeContext, + stdin: string | undefined +): Promise { + const provider = getEphemeralVmRecipeStepProvider(args.recipe.provider!) + if (!provider) { + return { + stdout: '', + stderr: 'This provider is not available in this build.\n', + exitCode: 1, + signal: null + } + } + const timeoutMs = toTimeoutMs(getEphemeralVmRecipeTimeoutSeconds(args.recipe, mode)) + const timeout = timeoutMs === undefined ? null : AbortSignal.timeout(timeoutMs) + const signals = [args.signal, timeout].filter( + (s): s is AbortSignal => s !== undefined && s !== null + ) + args.onSpawn?.() + try { + return await provider({ + mode, + repoPath: args.repoPath, + context, + ...(stdin === undefined ? {} : { stdin }), + ...(signals.length > 0 ? { signal: AbortSignal.any(signals) } : {}), + onStderr: args.onStderr + }) + } catch (error) { + if (timeout?.aborted) { + return { stdout: '', stderr: '', exitCode: null, signal: null, timedOut: true } + } + if (args.signal?.aborted) { + return { stdout: '', stderr: '', exitCode: null, signal: null, aborted: true } + } + return { + stdout: '', + stderr: `${error instanceof Error ? error.message : String(error)}\n`, + exitCode: 1, + signal: null + } + } +} + export function getRecipeStepFailure( recipe: OrcaVmRecipe, mode: 'create' | 'suspend' | 'resume', - result: ProcessRunResult + result: RecipeStepProviderResult ): EphemeralVmFailure | null { + if (result.failure) { + return result.failure + } if (result.timedOut) { return ephemeralVmTimeoutFailure(mode, getEphemeralVmRecipeTimeoutSeconds(recipe, mode) ?? 0) } diff --git a/src/shared/ephemeral-vm-runtime-feature-store.ts b/src/shared/ephemeral-vm-runtime-feature-store.ts index 57cb068c611..83831b283f9 100644 --- a/src/shared/ephemeral-vm-runtime-feature-store.ts +++ b/src/shared/ephemeral-vm-runtime-feature-store.ts @@ -22,7 +22,9 @@ const EphemeralVmRuntimeFeatureEntrySchema = z recipeId: z.string().min(1), createdAt: z.number().finite(), recipeCheckoutMode: z.enum(['orca-worktree', 'provisioned-root']).optional(), - resultCheckoutMode: z.literal('provisioned-root').optional() + resultCheckoutMode: z.literal('provisioned-root').optional(), + // Built-in provider (Orca Cloud); rollback builds never see it, so they never run its labels. + recipeProvider: z.literal('orca-cloud').optional() }) .strict() @@ -152,7 +154,8 @@ export function featureEntryFromRuntime( const recipeCheckoutMode = runtime.recipe?.checkoutMode const resultCheckoutMode = runtime.recipeResult?.schemaVersion === 2 ? runtime.recipeResult.checkoutMode : undefined - if (!recipeCheckoutMode && !resultCheckoutMode) { + const recipeProvider = runtime.recipe?.provider + if (!recipeCheckoutMode && !resultCheckoutMode && !recipeProvider) { return null } return { @@ -160,7 +163,8 @@ export function featureEntryFromRuntime( recipeId: runtime.recipeId, createdAt: runtime.createdAt, ...(recipeCheckoutMode ? { recipeCheckoutMode } : {}), - ...(resultCheckoutMode ? { resultCheckoutMode } : {}) + ...(resultCheckoutMode ? { resultCheckoutMode } : {}), + ...(recipeProvider ? { recipeProvider } : {}) } } @@ -189,8 +193,14 @@ function restoreRuntimeFeatures( } return EphemeralVmRuntimeRecordSchema.parse({ ...runtime, - ...(runtime.recipe && feature.recipeCheckoutMode - ? { recipe: { ...runtime.recipe, checkoutMode: feature.recipeCheckoutMode } } + ...(runtime.recipe && (feature.recipeCheckoutMode || feature.recipeProvider) + ? { + recipe: { + ...runtime.recipe, + ...(feature.recipeCheckoutMode ? { checkoutMode: feature.recipeCheckoutMode } : {}), + ...(feature.recipeProvider ? { provider: feature.recipeProvider } : {}) + } + } : {}), ...(feature.resultCheckoutMode && runtime.recipeResult ? { diff --git a/src/shared/ephemeral-vm-runtime-rollback-projection.ts b/src/shared/ephemeral-vm-runtime-rollback-projection.ts index 5d977f447d3..59ee09ff0a9 100644 --- a/src/shared/ephemeral-vm-runtime-rollback-projection.ts +++ b/src/shared/ephemeral-vm-runtime-rollback-projection.ts @@ -27,7 +27,9 @@ export function projectRuntimeForRollback( runtime: EphemeralVmProvisionedRuntimeRecord ): EphemeralVmRuntimeRecord { const recipe = runtime.recipe - ? (({ checkoutMode: _checkoutMode, ...rollbackRecipe }) => rollbackRecipe)(runtime.recipe) + ? (({ checkoutMode: _checkoutMode, provider: _provider, ...rollbackRecipe }) => rollbackRecipe)( + runtime.recipe + ) : undefined const recipeResult = runtime.recipeResult.schemaVersion === 2 diff --git a/src/shared/ephemeral-vm-runtime-store.test.ts b/src/shared/ephemeral-vm-runtime-store.test.ts index c5163aedf52..8346f2b5f8f 100644 --- a/src/shared/ephemeral-vm-runtime-store.test.ts +++ b/src/shared/ephemeral-vm-runtime-store.test.ts @@ -1,4 +1,4 @@ -import { mkdtempSync, rmSync } from 'node:fs' +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' @@ -104,6 +104,23 @@ describe('ephemeral VM runtime store', () => { expect(recovered.cleanupLastError).toBeUndefined() }) + it('keeps a built-in provider across reads while the main file stays rollback-shaped', () => { + const userDataPath = makeUserDataPath() + const recipe = { + id: 'cloud', + name: 'Orca Cloud', + create: 'orca-cloud create', + provider: 'orca-cloud' as const + } + upsertEphemeralVmRuntime(userDataPath, runtimeRecord({ recipeId: 'cloud', recipe })) + + expect(listEphemeralVmRuntimes(userDataPath)[0]?.recipe?.provider).toBe('orca-cloud') + const main = JSON.parse( + readFileSync(join(userDataPath, 'orca-ephemeral-vm-runtimes.json'), 'utf8') + ) + expect(main.runtimes[0].recipe.provider).toBeUndefined() + }) + it('rejects an oversized write without publishing a partial runtime record', () => { const userDataPath = makeUserDataPath() diff --git a/src/shared/ephemeral-vm-runtimes.ts b/src/shared/ephemeral-vm-runtimes.ts index 2eb6d84dead..796cf2c8dcd 100644 --- a/src/shared/ephemeral-vm-runtimes.ts +++ b/src/shared/ephemeral-vm-runtimes.ts @@ -41,7 +41,8 @@ const EphemeralVmRuntimeRecipeSchema = z suspend: z.string().min(1).optional(), resume: z.string().min(1).optional(), destroy: z.string().min(1).optional(), - destroyDisabled: z.boolean().optional() + destroyDisabled: z.boolean().optional(), + provider: z.literal('orca-cloud').optional() }) .strict() diff --git a/src/shared/hosted-sandbox/hosted-session-events.ts b/src/shared/hosted-sandbox/hosted-session-events.ts new file mode 100644 index 00000000000..5a08309babb --- /dev/null +++ b/src/shared/hosted-sandbox/hosted-session-events.ts @@ -0,0 +1,22 @@ +// Main ↔ window events and the orcad RPC the Orca Cloud lease scheduler uses. +import { z } from 'zod' + +/** orcad: is real work running (agents, chats, automations), and when did a person last type in a PTY. */ +export const ORCAD_WORK_VERDICT_METHOD = 'orcad.workVerdict' +export const OrcadWorkVerdictParamsSchema = z.object({}).strict() + +export type OrcadWorkVerdict = { + verdict: 'idle' | 'busy' | 'unverifiable' + /** Epoch ms since every work probe has read idle, as seen by this orcad. */ + idleSince: number | null + /** Epoch ms of the latest driving (user) input to any terminal. */ + lastPtyInputAt: number | null +} + +export type HostedSessionWarningEvent = { + workspaceId: string | null + workspaceName: string | null + minutesLeft: 60 | 15 +} + +export type HostedSessionIdleSleepEvent = { workspaceId: string | null } diff --git a/src/shared/orca-billing.ts b/src/shared/orca-billing.ts new file mode 100644 index 00000000000..205d4122869 --- /dev/null +++ b/src/shared/orca-billing.ts @@ -0,0 +1,114 @@ +// Orca Cloud plan & billing, as the desktop sees it. The billing service +// (cloud/apps/billing) is the authority; these mirror its /v1/billing responses. +// One self-serve plan (Team, per seat, monthly or annual) plus Enterprise (contact sales). + +export const ORCA_BILLING_PLAN_IDS = ['team'] as const +export type OrcaBillingPlanId = (typeof ORCA_BILLING_PLAN_IDS)[number] +export const ORCA_BILLING_INTERVALS = ['month', 'year'] as const +export type OrcaBillingInterval = (typeof ORCA_BILLING_INTERVALS)[number] +export const ORCA_BILLING_MAX_SEATS = 500 + +export type OrcaBillingDenyReason = + | 'no_subscription' + | 'subscription_inactive' + | 'payment_past_due' + | 'payment_grace_expired' + | 'included_hours_exhausted' + | 'overage_cap_reached' + +export type OrcaBillingOffers = { + team: { + label: string + /** Display only, e.g. "$50/seat/mo"; Stripe charges the configured price. */ + monthlyPriceLabel: string + annualPriceLabel: string + /** Per-seat prices in cents for totals and the annual saving; null from older services. */ + monthlyPriceCentsPerSeat: number | null + annualPriceCentsPerSeat: number | null + /** How many Standard hours one Large hour uses. */ + largeMachineWeight: number + includedHoursPerSeat: number + concurrentSandboxesPerSeat: number + idleSleepMinutes: number + overageCentsPerHour: number + defaultSpendCapCents: number + } + enterprise: { label: string; priceLabel: string; contactUrl: string } +} + +export type OrcaBillingEntitlement = { + plan: OrcaBillingPlanId | null + planLabel: string | null + /** Stripe subscription status, or 'none'. */ + status: string + interval: OrcaBillingInterval | null + seats: number + period: { start: number; end: number } | null + /** Monthly window the hours count against (a slice of the year on annual plans). */ + usageWindow: { start: number; end: number } | null + cancelAtPeriodEnd: boolean + includedSeconds: number + usedSeconds: number + remainingIncludedSeconds: number + /** Highest included-hours warning level reached (80, 100), or null. */ + usageWarningPercent: number | null + overageSeconds: number + overage: { + optedIn: boolean + active: boolean + spendCapCents: number + spentCents: number + centsPerHour: number + } + /** Failed payment: new starts stop at once; running sandboxes suspend after `graceEndsAt`. */ + payment: { pastDueSince: number | null; graceEndsAt: number | null; suspendRunning: boolean } + canStartSandbox: boolean + reason: OrcaBillingDenyReason | null + canManageBilling: boolean + /** What is on offer, from the billing service's catalog (prices are config there, not here). */ + offers: OrcaBillingOffers | null +} + +export type OrcaBillingStatusResult = + | { status: 'ok'; entitlement: OrcaBillingEntitlement } + | { status: 'unconfigured' } + | { status: 'signed-out' } + | { status: 'failed'; error: string } + +export type OrcaBillingCheckoutArgs = { + plan: OrcaBillingPlanId + interval: OrcaBillingInterval + seats: number +} + +export type OrcaBillingOverageArgs = { + enabled: boolean + /** Whole cents; omitted keeps the current cap. */ + spendCapCents?: number +} + +export type OrcaBillingOpenResult = + | { status: 'opened' } + | { status: 'unconfigured' } + | { status: 'signed-out' } + | { status: 'forbidden' } + | { status: 'already-subscribed' } + | { status: 'no-billing-account' } + | { status: 'failed'; error: string } + +export type OrcaBillingOverageResult = + | { status: 'ok'; entitlement: OrcaBillingEntitlement } + | { status: 'unconfigured' } + | { status: 'signed-out' } + | { status: 'forbidden' } + | { status: 'no-subscription' } + | { status: 'cap-too-high' } + | { status: 'failed'; error: string } + +export function isOrcaBillingPlanId(value: unknown): value is OrcaBillingPlanId { + return ORCA_BILLING_PLAN_IDS.some((plan) => plan === value) +} + +export function isOrcaBillingInterval(value: unknown): value is OrcaBillingInterval { + return ORCA_BILLING_INTERVALS.some((interval) => interval === value) +} diff --git a/src/shared/orca-yaml-hook-types.ts b/src/shared/orca-yaml-hook-types.ts index 6206a270540..301b4417033 100644 --- a/src/shared/orca-yaml-hook-types.ts +++ b/src/shared/orca-yaml-hook-types.ts @@ -40,8 +40,12 @@ export type OrcaVmRecipe = { destroyDisabled?: boolean // Per-mode wall-clock limits; orca.yaml accepts one number for every mode or a per-mode map. timeoutSeconds?: OrcaVmRecipeTimeoutSeconds + /** Built-in providers run in-process; the lifecycle strings are labels, never executed. */ + provider?: OrcaVmRecipeProviderId } +export type OrcaVmRecipeProviderId = 'orca-cloud' + export type OrcaVmRecipeLifecycleMode = 'create' | 'suspend' | 'resume' | 'destroy' export type OrcaVmRecipeTimeoutSeconds = Partial> diff --git a/src/shared/rpc-contract/rpc-params-catalog.generated.ts b/src/shared/rpc-contract/rpc-params-catalog.generated.ts index 8e5fdeaa8e1..6e2b319f4bc 100644 --- a/src/shared/rpc-contract/rpc-params-catalog.generated.ts +++ b/src/shared/rpc-contract/rpc-params-catalog.generated.ts @@ -31,6 +31,7 @@ import { DesktopBrowserBridgeAttachParams, DesktopBrowserBridgeResultParams } from '../desktop-browser-bridge-protocol' +import { OrcadWorkVerdictParamsSchema } from '../hosted-sandbox/hosted-session-events' import { MobileWebBundleChunkParamsSchema } from '../mobile-web-bundle/bundle-rpc-contract' import { OrcadMigrationBlobChunkRequestSchema } from '../orcad-migration/orcad-migration-blob-transfer' import { OrcadMigrationSnapshotChunkRequestSchema } from '../orcad-migration/orcad-migration-scrollback' @@ -1335,6 +1336,7 @@ export const RPC_PARAMS_BY_METHOD = { 'orcad.migration.stageCatalog': OrcadMigrationCatalogParams, 'orcad.migration.stageSnapshotChunk': OrcadMigrationSnapshotChunkRequestSchema, 'orcad.terminalCensus': OrcadTerminalCensusParamsSchema, + 'orcad.workVerdict': OrcadWorkVerdictParamsSchema, 'orchestration.ask': AskParams, 'orchestration.callerShow': null, 'orchestration.check': CheckParams, diff --git a/tests/e2e/agent-auto-permissions.spec.ts b/tests/e2e/agent-auto-permissions.spec.ts index 34ccb07a7b0..c1a2aac820d 100644 --- a/tests/e2e/agent-auto-permissions.spec.ts +++ b/tests/e2e/agent-auto-permissions.spec.ts @@ -7,7 +7,8 @@ test('global Auto preserves other agents’ choices and the single permission se orcaPage }) => { await waitForSessionReady(orcaPage) - await orcaPage.getByRole('button', { name: 'Settings', exact: true }).click() + await orcaPage.getByRole('button', { name: 'Account', exact: true }).click() + await orcaPage.getByRole('menuitem', { name: 'Settings', exact: true }).click() await orcaPage.getByRole('button', { name: 'Agents', exact: true }).click() const permissions = orcaPage.getByRole('button', { name: 'Permissions', exact: true }) const group = orcaPage.getByRole('group', { name: 'Permissions', exact: true }) diff --git a/tests/e2e/onboarding.spec.ts b/tests/e2e/onboarding.spec.ts index 4d403803885..76d3ea2a8a6 100644 --- a/tests/e2e/onboarding.spec.ts +++ b/tests/e2e/onboarding.spec.ts @@ -351,7 +351,8 @@ test.describe('Onboarding flow', () => { .poll(async () => (await getSettings(orcaPage)).defaultTuiAgent, { timeout: 5_000 }) .toBe('codex') - await orcaPage.getByRole('button', { name: 'Help', exact: true }).click() + await orcaPage.getByRole('button', { name: 'Account', exact: true }).click() + await orcaPage.getByRole('menuitem', { name: 'Help', exact: true }).click() await orcaPage.getByRole('menuitem', { name: 'Onboarding', exact: true }).click() await onboardingFooterButton(orcaPage, SET_UP_LATER_BUTTON).click() await expectMainPage(orcaPage) From 9842aecacf0eb27d5708c06951251a9539439985 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:03:47 -0800 Subject: [PATCH 13/18] chore(cloud): dev golden image rebuilt with the secret-store start script (#27) --- cloud/apps/sandbox-control/wrangler.dev.jsonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index f246c2d13ee..b0e853671ff 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -32,7 +32,7 @@ "VERCEL_REGION": "iad1", "BILLING_URL": "https://orca-billing-dev.lovecast.workers.dev", // Golden image from image/build-golden-image.ts; lifecycle routes answer 503 without one. - "GOLDEN_SNAPSHOT_ID": "snap_0fYmcpIUlc4OUC9viFN0Y4dVCDuD", + "GOLDEN_SNAPSHOT_ID": "snap_vpcmcCAgTtISiTZgsCosOlThQbFd", "GITHUB_APP_SLUG": "orca-cloud-dev", "EGRESS_PROXY_URL": "https://orca-sandbox-control-dev.lovecast.workers.dev/v1/egress/proxy" } From 3de9ec36731b52da8e089c60bad1885b542b7cc5 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 03:28:58 -0800 Subject: [PATCH 14/18] feat(cloud): self-rotating golden images (refresh before expiry, release builds, smoke-gated atomic swap); Orca Cloud gated on the sandbox capability (#28) --- .../image/generate-vm-scripts.ts | 26 ++ .../image/golden-image.manifest.json | 9 +- .../image/golden-image.test.ts | 9 + .../sandbox-control/image/golden-image.ts | 14 +- .../image/vm-scripts.generated.ts | 6 + .../migrations/0005_golden_images.sql | 20 ++ cloud/apps/sandbox-control/src/env.ts | 6 + .../src/golden-image-provider.ts | 11 +- .../apps/sandbox-control/src/golden-images.ts | 314 ++++++++++++++++++ cloud/apps/sandbox-control/src/index.ts | 42 ++- .../sandbox-control/src/vercel-sandbox-api.ts | 29 ++ .../apps/sandbox-control/test/fake-vercel.ts | 3 + .../test/golden-images.test.ts | 131 ++++++++ .../hosted-sandbox/hosted-sandbox-install.ts | 28 +- 14 files changed, 639 insertions(+), 9 deletions(-) create mode 100644 cloud/apps/sandbox-control/image/generate-vm-scripts.ts create mode 100644 cloud/apps/sandbox-control/image/vm-scripts.generated.ts create mode 100644 cloud/apps/sandbox-control/migrations/0005_golden_images.sql create mode 100644 cloud/apps/sandbox-control/src/golden-images.ts create mode 100644 cloud/apps/sandbox-control/test/golden-images.test.ts diff --git a/cloud/apps/sandbox-control/image/generate-vm-scripts.ts b/cloud/apps/sandbox-control/image/generate-vm-scripts.ts new file mode 100644 index 00000000000..1ac517c6d1a --- /dev/null +++ b/cloud/apps/sandbox-control/image/generate-vm-scripts.ts @@ -0,0 +1,26 @@ +// Embeds the image scripts + manifest as strings so the Worker can run a golden build itself. +// Regenerate after editing image/: `pnpm --filter @orca-cloud/sandbox-control exec tsx image/generate-vm-scripts.ts` +// (image/vm-scripts.test.ts fails while the generated file is stale). +import { readFileSync, writeFileSync } from 'node:fs' +import { fileURLToPath } from 'node:url' +import { VM_SCRIPTS } from './golden-image' + +const here = (p: string): string => fileURLToPath(new URL(p, import.meta.url).href) + +export function renderVmScriptsModule(): string { + const manifest = JSON.parse(readFileSync(here('./golden-image.manifest.json'), 'utf8')) + const scripts = Object.fromEntries(VM_SCRIPTS.map((name) => [name, readFileSync(here(`./vm/${name}`), 'utf8')])) + return [ + '// GENERATED by image/generate-vm-scripts.ts from image/. Do not edit.', + "import type { BuildInputs, GoldenImageManifest } from './golden-image'", + '', + `export const MANIFEST: GoldenImageManifest = ${JSON.stringify(manifest)}`, + `export const PROVISION_SCRIPT: string = ${JSON.stringify(readFileSync(here('./provision-golden.sh'), 'utf8'))}`, + `export const VM_SCRIPT_SOURCES: BuildInputs['vmScripts'] = ${JSON.stringify(scripts)}`, + '' + ].join('\n') +} + +if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href) { + writeFileSync(here('./vm-scripts.generated.ts'), renderVmScriptsModule()) +} diff --git a/cloud/apps/sandbox-control/image/golden-image.manifest.json b/cloud/apps/sandbox-control/image/golden-image.manifest.json index 94d061fdc39..cef924f0c09 100644 --- a/cloud/apps/sandbox-control/image/golden-image.manifest.json +++ b/cloud/apps/sandbox-control/image/golden-image.manifest.json @@ -8,8 +8,11 @@ "sha256": null }, "gh": { - "version": null, - "sha256": null + "version": "2.103.3", + "sha256": "4c806978ffce1cec70853302a56d70ac6da4f19bae7adca4e1a8ab00633dd91d" }, - "agentCliPackages": ["@anthropic-ai/claude-code@2.1.319", "@openai/codex@0.166.0"] + "agentCliPackages": [ + "@anthropic-ai/claude-code@2.1.319", + "@openai/codex@0.166.0" + ] } diff --git a/cloud/apps/sandbox-control/image/golden-image.test.ts b/cloud/apps/sandbox-control/image/golden-image.test.ts index e68ef00d1cc..f4ea0e13437 100644 --- a/cloud/apps/sandbox-control/image/golden-image.test.ts +++ b/cloud/apps/sandbox-control/image/golden-image.test.ts @@ -1,7 +1,9 @@ import { spawnSync } from 'node:child_process' +import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { describe, expect, it } from 'vitest' import { VM_SCRIPTS, buildGoldenImage, resolveReleaseBundle, type BuildInputs } from './golden-image' +import { renderVmScriptsModule } from './generate-vm-scripts' const manifest: BuildInputs['manifest'] = { imageVersion: '2026-11-08.1', @@ -101,3 +103,10 @@ describe('golden image build', () => { await expect(resolveReleaseBundle('v1.4.254', 'x64', async () => new Response('', { status: 404 }))).rejects.toThrow(/source build/) }) }) + +describe('Worker-embedded image scripts', () => { + it('match image/ (run image/generate-vm-scripts.ts after editing a script)', () => { + const generated = readFileSync(fileURLToPath(new URL('./vm-scripts.generated.ts', import.meta.url).href), 'utf8') + expect(generated).toBe(renderVmScriptsModule()) + }) +}) diff --git a/cloud/apps/sandbox-control/image/golden-image.ts b/cloud/apps/sandbox-control/image/golden-image.ts index f12fcfcf18f..43c3d341998 100644 --- a/cloud/apps/sandbox-control/image/golden-image.ts +++ b/cloud/apps/sandbox-control/image/golden-image.ts @@ -82,18 +82,28 @@ export function provisionEnv(inputs: BuildInputs): Record { env.GH_SHA256 = manifest.gh.sha256 } for (const name of VM_SCRIPTS) { - env[`ORCA_VM_SCRIPT_${name.replaceAll('-', '_')}`] = Buffer.from(inputs.vmScripts[name]).toString('base64') + env[`ORCA_VM_SCRIPT_${name.replaceAll('-', '_')}`] = utf8Base64(inputs.vmScripts[name]) } return env } +/** Runs in Node (operator CLI) and in Workers (scheduled rotation), so no Buffer. */ +function utf8Base64(text: string): string { + let binary = '' + for (const byte of new TextEncoder().encode(text)) { + binary += String.fromCharCode(byte) + } + return btoa(binary) +} + export type BuildResult = { imageVersion: string snapshotId: string report: unknown } -type Provider = Pick & GoldenImageProviderApi +type Provider = Pick & + Pick export async function buildGoldenImage( provider: Provider, diff --git a/cloud/apps/sandbox-control/image/vm-scripts.generated.ts b/cloud/apps/sandbox-control/image/vm-scripts.generated.ts new file mode 100644 index 00000000000..7f2c0fa3182 --- /dev/null +++ b/cloud/apps/sandbox-control/image/vm-scripts.generated.ts @@ -0,0 +1,6 @@ +// GENERATED by image/generate-vm-scripts.ts from image/. Do not edit. +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"} diff --git a/cloud/apps/sandbox-control/migrations/0005_golden_images.sql b/cloud/apps/sandbox-control/migrations/0005_golden_images.sql new file mode 100644 index 00000000000..1fa81564414 --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0005_golden_images.sql @@ -0,0 +1,20 @@ +-- Golden images the Worker builds and rotates on its own. One row is `active`; the previous one +-- stays `retired` (kept, usable) until a newer image has passed its smoke test. +CREATE TABLE golden_images ( + id TEXT PRIMARY KEY, + -- seed | clone: | release: + source TEXT NOT NULL, + bundle_sha256 TEXT, + -- building | smoking | active | retired | failed | deleted + status TEXT NOT NULL, + build_name TEXT, + session_id TEXT, + command_id TEXT, + snapshot_id TEXT, + expires_at INTEGER, + error TEXT, + created_at INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + activated_at INTEGER +); +CREATE INDEX golden_images_status ON golden_images (status, activated_at); diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 478ee7648fe..3b467b55f7a 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -14,6 +14,12 @@ export type Env = { VERCEL_API_BASE?: string /** Golden image (orcad + agent CLIs) every new sandbox boots from; see image/. */ GOLDEN_SNAPSHOT_ID?: string + /** `release:latest` | `release:v1.2.3`: build golden images from Orca's released orca-server tarball. */ + GOLDEN_IMAGE_SOURCE?: string + /** TTL for Worker-built golden images; unset/0 keeps them until rotated out. */ + GOLDEN_SNAPSHOT_EXPIRATION_DAYS?: string + /** Refresh lead time before the active image expires (hours, default 24). */ + GOLDEN_REFRESH_BEFORE_HOURS?: string // Desktop tokens: short-lived ES256 JWTs from Orca Cloud auth, aud=SANDBOX_JWT_AUDIENCE. // SANDBOX_JWKS (inline set) is for local dev and tests only. diff --git a/cloud/apps/sandbox-control/src/golden-image-provider.ts b/cloud/apps/sandbox-control/src/golden-image-provider.ts index 12a6d0286c8..aa5030dc1bd 100644 --- a/cloud/apps/sandbox-control/src/golden-image-provider.ts +++ b/cloud/apps/sandbox-control/src/golden-image-provider.ts @@ -1,4 +1,5 @@ -// Provider calls only the operator-run golden image build needs (image/build-golden-image.ts). +// Provider calls the golden image build needs: the operator CLI (image/build-golden-image.ts) and the +// Worker's scheduled rotation (src/golden-images.ts). import type { ProviderSandboxState } from './sandbox-provider' export type GoldenImageProviderApi = { @@ -14,4 +15,12 @@ export type GoldenImageProviderApi = { createSnapshot(sessionId: string, expirationMs: number): Promise /** Deletes the sandbox but keeps snapshots it produced. */ deleteBuildSandbox(name: string): Promise + /** Starts a command without waiting; poll it with getCommand (builds outlast one cron tick). */ + startCommand( + sessionId: string, + params: { command: string; args?: string[]; env?: Record; timeoutMs: number } + ): Promise + /** Null exit code while it runs. */ + getCommand(sessionId: string, commandId: string): Promise<{ exitCode: number | null }> + getSnapshot(snapshotId: string): Promise<{ id: string; status: 'created' | 'deleted' | 'failed'; expiresAt: number | null } | null> } diff --git a/cloud/apps/sandbox-control/src/golden-images.ts b/cloud/apps/sandbox-control/src/golden-images.ts new file mode 100644 index 00000000000..1a0f728837f --- /dev/null +++ b/cloud/apps/sandbox-control/src/golden-images.ts @@ -0,0 +1,314 @@ +// Golden images the Worker keeps fresh by itself, one step per cron tick: +// - refresh: before the active image expires, clone it into a new snapshot (no source build needed); +// - release: when GOLDEN_IMAGE_SOURCE names an Orca release with an orca-server tarball, build from it. +// A new image becomes active only after a smoke boot reaches orcad ready; the previous one is kept +// (`retired`, still bootable) until the next rotation succeeds, so a bad build never strands creates. +import { MANIFEST, PROVISION_SCRIPT, VM_SCRIPT_SOURCES } from '../image/vm-scripts.generated' +import { provisionEnv, resolveReleaseBundle, type GoldenImageManifest } from '../image/golden-image' +import type { GoldenImageProviderApi } from './golden-image-provider' +import { compileNetworkPolicy } from './network-policy' +import type { SandboxProviderApi } from './sandbox-provider' +import { ORCAD_PORT, startOrcad } from './sandbox-vm-commands' + +export type GoldenImageDeps = { + db: D1Database + provider: SandboxProviderApi & GoldenImageProviderApi + now: () => number + config: { + environment: string + region: string + /** Snapshot TTL for new images; 0 keeps them until rotated out. */ + expirationMs: number + /** `release:latest` or `release:v1.2.3`; unset means refresh-only. */ + source?: string + /** Fallback when no row is active yet (wrangler var); adopted as the first active row. */ + seedSnapshotId?: string + /** Refresh this long before the active image expires (default 24 h). */ + refreshBeforeMs?: number + fetch?: typeof fetch + } +} + +type GoldenRow = { + id: string + source: string + bundle_sha256: string | null + status: 'building' | 'smoking' | 'active' | 'retired' | 'failed' | 'deleted' + build_name: string | null + session_id: string | null + command_id: string | null + snapshot_id: string | null + expires_at: number | null + error: string | null + created_at: number + updated_at: number + activated_at: number | null +} + +const REFRESH_BEFORE_MS = 24 * 60 * 60_000 +const BUILD_DEADLINE_MS = 45 * 60_000 +const RETRY_AFTER_FAILURE_MS = 6 * 60 * 60_000 +const WIPE = '/opt/orca/bin/orca-image-wipe' + +function randomId(): string { + const bytes = crypto.getRandomValues(new Uint8Array(8)) + return `gld_${Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')}` +} + +/** The image new sandboxes boot from: the active row, else the configured seed. */ +export async function currentGoldenSnapshotId(db: D1Database, seed?: string): Promise { + const row = await db + .prepare(`SELECT snapshot_id FROM golden_images WHERE status = 'active' ORDER BY activated_at DESC LIMIT 1`) + .first<{ snapshot_id: string }>() + return row?.snapshot_id ?? seed ?? null +} + +async function rows(db: D1Database, statuses: GoldenRow['status'][]): Promise { + const marks = statuses.map(() => '?').join(', ') + const { results } = await db + .prepare(`SELECT * FROM golden_images WHERE status IN (${marks}) ORDER BY created_at DESC`) + .bind(...statuses) + .all() + return results +} + +async function update(db: D1Database, id: string, patch: Partial, now: number): Promise { + const entries = Object.entries(patch) + await db + .prepare(`UPDATE golden_images SET ${entries.map(([k]) => `${k} = ?`).join(', ')}, updated_at = ? WHERE id = ?`) + .bind(...entries.map(([, v]) => v ?? null), now, id) + .run() +} + +async function insert(db: D1Database, row: Pick & Partial, now: number) { + await db + .prepare( + `INSERT INTO golden_images (id, source, bundle_sha256, status, build_name, session_id, command_id, snapshot_id, + expires_at, created_at, updated_at, activated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` + ) + .bind(row.id, row.source, row.bundle_sha256 ?? null, row.status, row.build_name ?? null, row.session_id ?? null, + row.command_id ?? null, row.snapshot_id ?? null, row.expires_at ?? null, now, now, row.activated_at ?? null) + .run() +} + +export type GoldenTick = + | 'idle' + | 'seeded' + | 'refresh-started' + | 'release-started' + | 'building' + | 'built' + | 'activated' + | 'failed' + | 'release-unavailable' + +/** One step of the rotation state machine; safe to call every cron tick. */ +export async function advanceGoldenImages(deps: GoldenImageDeps): Promise { + const { db, provider } = deps + const now = deps.now() + const [inFlight] = await rows(db, ['building', 'smoking']) + if (inFlight?.status === 'building') { + return stepBuild(deps, inFlight) + } + if (inFlight?.status === 'smoking') { + return stepSmoke(deps, inFlight) + } + let [active] = await rows(db, ['active']) + if (!active && deps.config.seedSnapshotId) { + const snapshot = await provider.getSnapshot(deps.config.seedSnapshotId) + await insert(db, { + id: randomId(), + source: 'seed', + status: 'active', + snapshot_id: deps.config.seedSnapshotId, + expires_at: snapshot?.expiresAt ?? null, + activated_at: now + }, now) + ;[active] = await rows(db, ['active']) + if (!active) { + return 'idle' + } + return 'seeded' + } + if (!active?.snapshot_id) { + return 'idle' + } + const recentFailure = (await rows(db, ['failed'])).find((r) => now - r.updated_at < RETRY_AFTER_FAILURE_MS) + const release = deps.config.source?.startsWith('release:') ? await desiredRelease(deps) : null + if (release === 'unavailable') { + // No orca-server tarball on that release yet; keep refreshing the current image meanwhile. + } else if (release && release.sha256 !== active.bundle_sha256 && recentFailure?.bundle_sha256 !== release.sha256) { + return startRelease(deps, release) + } + const refreshBefore = deps.config.refreshBeforeMs ?? REFRESH_BEFORE_MS + if (active.expires_at !== null && active.expires_at - now < refreshBefore && !recentFailure) { + return startRefresh(deps, active) + } + return release === 'unavailable' ? 'release-unavailable' : 'idle' +} + +async function desiredRelease(deps: GoldenImageDeps): Promise<{ tag: string; bundleUrl: string; sha256: string } | 'unavailable'> { + const doFetch = deps.config.fetch ?? fetch + let tag = deps.config.source!.slice('release:'.length) + if (tag === 'latest') { + const res = await doFetch('https://api.github.com/repos/stablyai/orca/releases/latest', { + headers: { accept: 'application/vnd.github+json', 'user-agent': 'orca-sandbox-control' } + }) + if (!res.ok) { + return 'unavailable' + } + tag = String(((await res.json()) as { tag_name?: string }).tag_name ?? '') + } + try { + return { tag, ...(await resolveReleaseBundle(tag, 'x64', doFetch)) } + } catch { + return 'unavailable' + } +} + +/** Clone the active image into a fresh snapshot (new TTL). Fits in one tick: boot, wipe, snapshot. */ +async function startRefresh(deps: GoldenImageDeps, active: GoldenRow): Promise { + const { db, provider } = deps + const now = deps.now() + const id = randomId() + const name = `orca-${deps.config.environment}-golden-${id}` + await insert(db, { id, source: `clone:${active.snapshot_id}`, bundle_sha256: active.bundle_sha256, status: 'building', build_name: name }, now) + try { + const box = await provider.createSandbox({ + name, + snapshotId: active.snapshot_id!, + vcpus: 2, + region: deps.config.region, + timeoutMs: 15 * 60_000, + ports: [], + networkPolicy: compileNetworkPolicy({ mode: 'allowlist', allowlist: [] }), + tags: { orcaGoldenImage: id, orcaEnv: deps.config.environment }, + snapshotExpirationMs: 86_400_000 + }) + await finishBuild(deps, id, name, box.session.id) + return 'refresh-started' + } catch (error) { + await fail(deps, id, name, String(error)) + return 'failed' + } +} + +async function startRelease(deps: GoldenImageDeps, release: { tag: string; bundleUrl: string; sha256: string }): Promise { + const { db, provider } = deps + const now = deps.now() + const id = randomId() + const name = `orca-${deps.config.environment}-golden-${id}` + await insert(db, { id, source: `release:${release.tag}`, bundle_sha256: release.sha256, status: 'building', build_name: name }, now) + try { + const manifest: GoldenImageManifest = { ...MANIFEST, orcad: { bundleUrl: release.bundleUrl, sha256: release.sha256 } } + const box = await provider.createBuildSandbox({ + name, + runtime: manifest.runtime, + vcpus: manifest.vcpus, + region: deps.config.region, + timeoutMs: BUILD_DEADLINE_MS, + tags: { orcaGoldenImage: id, orcaEnv: deps.config.environment } + }) + const commandId = await provider.startCommand(box.session.id, { + command: 'bash', + args: ['-c', PROVISION_SCRIPT], + env: provisionEnv({ manifest, provisionScript: PROVISION_SCRIPT, vmScripts: VM_SCRIPT_SOURCES, buildId: id }), + timeoutMs: 30 * 60_000 + }) + await update(db, id, { session_id: box.session.id, command_id: commandId }, now) + return 'release-started' + } catch (error) { + await fail(deps, id, name, String(error)) + return 'failed' + } +} + +async function stepBuild(deps: GoldenImageDeps, row: GoldenRow): Promise { + const now = deps.now() + if (now - row.created_at > BUILD_DEADLINE_MS) { + await fail(deps, row.id, row.build_name, 'build_deadline') + return 'failed' + } + if (!row.session_id || !row.command_id) { + return 'building' + } + const { exitCode } = await deps.provider.getCommand(row.session_id, row.command_id) + if (exitCode === null) { + return 'building' + } + if (exitCode !== 0) { + await fail(deps, row.id, row.build_name, `provision_exit_${exitCode}`) + return 'failed' + } + try { + await finishBuild(deps, row.id, row.build_name!, row.session_id) + } catch (error) { + await fail(deps, row.id, row.build_name, String(error)) + return 'failed' + } + return 'built' +} + +/** Wipe identity, snapshot (stops the box), drop the build box; the snapshot then waits for its smoke test. */ +async function finishBuild(deps: GoldenImageDeps, id: string, name: string, sessionId: string): Promise { + const { provider } = deps + const wipe = await provider.runCommand(sessionId, { command: WIPE, timeoutMs: 60_000 }) + if (wipe.exitCode !== 0 || !wipe.stdout.includes('"wiped":true')) { + throw new Error('identity_wipe_failed') + } + const snapshotId = await provider.createSnapshot(sessionId, deps.config.expirationMs) + await provider.deleteBuildSandbox(name) + const snapshot = await provider.getSnapshot(snapshotId) + await update(deps.db, id, { status: 'smoking', snapshot_id: snapshotId, expires_at: snapshot?.expiresAt ?? null }, deps.now()) +} + +/** Boot the candidate and require orcad ready before it serves a single user create. */ +async function stepSmoke(deps: GoldenImageDeps, row: GoldenRow): Promise { + const { db, provider } = deps + const name = `orca-${deps.config.environment}-goldsmoke-${row.id}` + try { + const box = await provider.createSandbox({ + name, + snapshotId: row.snapshot_id!, + vcpus: 2, + region: deps.config.region, + timeoutMs: 10 * 60_000, + ports: [ORCAD_PORT], + networkPolicy: compileNetworkPolicy({ mode: 'allowlist' }), + tags: { orcaGoldenSmoke: row.id, orcaEnv: deps.config.environment }, + snapshotExpirationMs: 86_400_000 + }) + await startOrcad(provider, box, { sandboxId: `smoke-${row.id}` }) + } catch (error) { + await provider.deleteSandbox(name).catch(() => undefined) + await fail(deps, row.id, null, `smoke_failed: ${String(error)}`) + if (row.snapshot_id) { + await provider.deleteSnapshot(row.snapshot_id).catch(() => undefined) + } + return 'failed' + } + await provider.deleteSandbox(name) + const now = deps.now() + // Atomic swap: the old active becomes retired (still bootable) in the same batch. + await db.batch([ + db.prepare(`UPDATE golden_images SET status = 'retired', updated_at = ? WHERE status = 'active'`).bind(now), + db.prepare(`UPDATE golden_images SET status = 'active', activated_at = ?, updated_at = ? WHERE id = ?`).bind(now, now, row.id) + ]) + // Keep one retired image as the rollback; older ones are deleted. + for (const old of (await rows(db, ['retired'])).slice(1)) { + if (old.snapshot_id) { + await provider.deleteSnapshot(old.snapshot_id).catch(() => undefined) + } + await update(db, old.id, { status: 'deleted' }, now) + } + console.log(JSON.stringify({ event: 'golden_image_activated', id: row.id, source: row.source, snapshotId: row.snapshot_id })) + return 'activated' +} + +async function fail(deps: GoldenImageDeps, id: string, buildName: string | null, error: string): Promise { + if (buildName) { + await deps.provider.deleteBuildSandbox(buildName).catch(() => undefined) + } + await update(deps.db, id, { status: 'failed', error: error.slice(0, 500) }, deps.now()) + console.log(JSON.stringify({ event: 'golden_image_failed', id, error: error.slice(0, 200) })) +} diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 87815c21114..6477e77bbbc 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -13,6 +13,7 @@ // Operator (OPERATOR_TOKEN): /v1/internal/* (see operator.ts) // Vercel network-policy forwardURL target (Vercel sandbox OIDC): /v1/egress/proxy import { authenticateDesktop } from './auth' +import { advanceGoldenImages, currentGoldenSnapshotId } from './golden-images' import { NO_CREDENTIALS, handleEgressProxy, type CredentialBroker, type ProxiedRoute } from './egress-proxy' import { BillingUnavailableError, @@ -262,7 +263,12 @@ async function readJson(request: Request): Promise { /** Lifecycle routes refuse to run half-configured rather than guess at Orca's provider account. */ async function lifecycleDeps(env: Env, deps: Deps): Promise { - if (!env.GOLDEN_SNAPSHOT_ID || (!deps.provider && (!env.VERCEL_TOKEN || !env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID))) { + if (!deps.provider && (!env.VERCEL_TOKEN || !env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID)) { + return null + } + // The Worker rotates golden images itself (golden-images.ts); the var only seeds the first one. + const goldenSnapshotId = await currentGoldenSnapshotId(env.SANDBOX_DB, env.GOLDEN_SNAPSHOT_ID) + if (!goldenSnapshotId) { return null } return { @@ -287,7 +293,7 @@ async function lifecycleDeps(env: Env, deps: Deps): Promise { + if (!env.VERCEL_TOKEN || !env.VERCEL_TEAM_ID || !env.VERCEL_PROJECT_ID) { + return + } + const days = Number(env.GOLDEN_SNAPSHOT_EXPIRATION_DAYS ?? '0') + const tick = await advanceGoldenImages({ + db: env.SANDBOX_DB, + provider: createVercelSandboxApi({ + token: env.VERCEL_TOKEN, + teamId: env.VERCEL_TEAM_ID, + projectId: env.VERCEL_PROJECT_ID, + baseUrl: env.VERCEL_API_BASE + }), + now: Date.now, + config: { + environment: env.SANDBOX_ENV, + region: env.VERCEL_REGION ?? 'iad1', + expirationMs: Number.isFinite(days) && days >= 1 ? Math.floor(days) * 86_400_000 : 0, + source: env.GOLDEN_IMAGE_SOURCE, + seedSnapshotId: env.GOLDEN_SNAPSHOT_ID, + refreshBeforeMs: env.GOLDEN_REFRESH_BEFORE_HOURS ? Number(env.GOLDEN_REFRESH_BEFORE_HOURS) * 3_600_000 : undefined + } + }) + if (tick !== 'idle') { + console.log(JSON.stringify({ event: 'golden_image_tick', tick })) + } +} + async function secretsDeps(env: Env): Promise { const keyring = keyringFor(env) if (!keyring) { @@ -524,6 +559,9 @@ export default { const summary = await reconcile(life) console.log(JSON.stringify({ event: 'reconcile', ...summary })) } + await advanceGoldenImagesFromEnv(env).catch((error: unknown) => + console.log(JSON.stringify({ event: 'golden_image_tick_failed', error: String(error).slice(0, 200) })) + ) }) ) } diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts index b30591fd7a0..19a8b5364cf 100644 --- a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -235,6 +235,35 @@ export function createVercelSandboxApi( request('DELETE', `/v2/sandboxes/${encodeURIComponent(name)}`, { query: { projectId: config.projectId } }) ), + async startCommand(sessionId, params) { + const response = await request('POST', `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/cmd`, { + body: { command: params.command, args: params.args ?? [], env: params.env ?? {}, timeout: params.timeoutMs } + }) + return (await json(z.looseObject({ command: z.looseObject({ id: z.string() }) }), response)).command.id + }, + + async getCommand(sessionId, commandId) { + const response = await request( + 'GET', + `/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/cmd/${encodeURIComponent(commandId)}` + ) + const body = await json(z.looseObject({ command: z.looseObject({ exitCode: z.number().nullable().optional() }) }), response) + return { exitCode: body.command.exitCode ?? null } + }, + + async getSnapshot(snapshotId) { + try { + const response = await request('GET', `/v2/sandboxes/snapshots/${encodeURIComponent(snapshotId)}`) + const body = await json(z.looseObject({ snapshot: Snapshot.extend({ expiresAt: z.number().optional() }) }), response) + return { id: body.snapshot.id, status: body.snapshot.status, expiresAt: body.snapshot.expiresAt ?? null } + } catch (error) { + if (error instanceof ProviderError && error.status === 404) { + return null + } + throw error + } + }, + async listSnapshots(name) { const snapshots: z.infer[] = [] let cursor: string | undefined diff --git a/cloud/apps/sandbox-control/test/fake-vercel.ts b/cloud/apps/sandbox-control/test/fake-vercel.ts index 43b974b5082..ad3c665fe4e 100644 --- a/cloud/apps/sandbox-control/test/fake-vercel.ts +++ b/cloud/apps/sandbox-control/test/fake-vercel.ts @@ -133,6 +133,9 @@ export function createFakeVercel(now: () => number): FakeVercel { const ready = { schemaVersion: 1, pairingCode: `orca://pair?code=${box.params.name}`, projectRoot: '/vercel/sandbox/project' } return { exitCode: 0, stdout: `booting\n${JSON.stringify(ready)}\n`, stderr: '' } } + if (params.command === '/opt/orca/bin/orca-image-wipe') { + return { exitCode: 0, stdout: '{"wiped":true}\n', stderr: '' } + } if (params.command === DRAIN_COMMAND) { return { exitCode: 0, stdout: '', stderr: '' } } diff --git a/cloud/apps/sandbox-control/test/golden-images.test.ts b/cloud/apps/sandbox-control/test/golden-images.test.ts new file mode 100644 index 00000000000..a975146cc15 --- /dev/null +++ b/cloud/apps/sandbox-control/test/golden-images.test.ts @@ -0,0 +1,131 @@ +import { describe, expect, it } from 'vitest' +import type { GoldenImageProviderApi } from '../src/golden-image-provider' +import { advanceGoldenImages, currentGoldenSnapshotId, type GoldenImageDeps } from '../src/golden-images' +import { createFakeD1 } from './d1-fake' +import { createFakeVercel } from './fake-vercel' +import { createHarness } from './harness' + +const HOUR = 60 * 60_000 + +function setup(options: { source?: string; seedExpiresInMs?: number; fetch?: typeof fetch } = {}) { + const { db, raw } = createFakeD1() + let now = 1_800_000_000_000 + const vercel = createFakeVercel(() => now) + const expiries = new Map([['snap_seed', options.seedExpiresInMs === undefined ? null : now + options.seedExpiresInMs]]) + vercel.snapshots.set('snap_seed', { id: 'snap_seed', status: 'created', sourceSessionId: 'sess_seed', sandboxName: 'build', creationMethod: 'manual', createdAt: now }) + const commands = new Map() + let counter = 0 + const golden: GoldenImageProviderApi = { + async createBuildSandbox(params) { + return vercel.createSandbox({ ...params, snapshotId: 'runtime:node24', ports: [], networkPolicy: { allow: {} }, snapshotExpirationMs: 0 }) + }, + async createSnapshot(sessionId, expirationMs) { + const box = [...vercel.boxes.values()].find((b) => b.sessionId === sessionId)! + box.status = 'stopped' + const id = `snap_gold_${++counter}` + vercel.snapshots.set(id, { id, status: 'created', sourceSessionId: sessionId, sandboxName: box.params.name, creationMethod: 'manual', createdAt: now }) + expiries.set(id, expirationMs === 0 ? null : now + expirationMs) + return id + }, + async deleteBuildSandbox(name) { + vercel.boxes.delete(name) + }, + async startCommand() { + const id = `cmd_${++counter}` + commands.set(id, null) + return id + }, + async getCommand(_session, commandId) { + return { exitCode: commands.get(commandId) ?? null } + }, + async getSnapshot(id) { + const snapshot = vercel.snapshots.get(id) + return snapshot ? { id, status: snapshot.status, expiresAt: expiries.get(id) ?? null } : null + } + } + const deps: GoldenImageDeps = { + db, + provider: { ...vercel, ...golden }, + now: () => now, + config: { environment: 'test', region: 'iad1', expirationMs: 0, seedSnapshotId: 'snap_seed', source: options.source, fetch: options.fetch } + } + return { + deps, + vercel, + commands, + raw, + advance: (ms: number) => (now += ms), + statuses: () => raw.prepare('SELECT source, status, snapshot_id FROM golden_images ORDER BY source DESC').all() + } +} + +describe('golden image rotation', () => { + it('refreshes an expiring image, smoke-tests it, then swaps it in and keeps the previous one', async () => { + const t = setup({ seedExpiresInMs: 20 * HOUR }) + expect(await advanceGoldenImages(t.deps)).toBe('seeded') + expect(await advanceGoldenImages(t.deps)).toBe('refresh-started') + // Not active until the smoke boot passes. + expect(await currentGoldenSnapshotId(t.deps.db)).toBe('snap_seed') + expect(await advanceGoldenImages(t.deps)).toBe('activated') + expect(await currentGoldenSnapshotId(t.deps.db)).toBe('snap_gold_1') + expect(t.statuses()).toEqual([ + { source: 'seed', status: 'retired', snapshot_id: 'snap_seed' }, + { source: 'clone:snap_seed', status: 'active', snapshot_id: 'snap_gold_1' } + ]) + expect(t.vercel.snapshots.get('snap_seed')!.status).toBe('created') + // The new image never expires, so nothing more to do; build and smoke boxes are gone. + expect(await advanceGoldenImages(t.deps)).toBe('idle') + expect([...t.vercel.boxes.keys()]).toEqual([]) + }) + + it('keeps serving the previous image when the candidate fails its smoke boot', async () => { + const t = setup({ seedExpiresInMs: 20 * HOUR }) + await advanceGoldenImages(t.deps) + await advanceGoldenImages(t.deps) + t.vercel.failStart = true + expect(await advanceGoldenImages(t.deps)).toBe('failed') + expect(await currentGoldenSnapshotId(t.deps.db)).toBe('snap_seed') + expect(t.vercel.snapshots.get('snap_gold_1')!.status).toBe('deleted') + // Backs off instead of rebuilding every five minutes. + expect(await advanceGoldenImages(t.deps)).toBe('idle') + }) + + it('builds from a new orca-server release, waits for the build, and activates it after smoke', async () => { + const sha = 'c'.repeat(64) + const fetchRelease: typeof fetch = async (input) => { + const url = String(input) + if (url.endsWith('/releases/latest')) { + return Response.json({ tag_name: 'v1.4.256' }) + } + return new Response(`${sha} orca-server-linux-x64.tar.gz\n`) + } + const t = setup({ source: 'release:latest', fetch: fetchRelease }) + expect(await advanceGoldenImages(t.deps)).toBe('seeded') + expect(await advanceGoldenImages(t.deps)).toBe('release-started') + expect(await advanceGoldenImages(t.deps)).toBe('building') + t.commands.set([...t.commands.keys()][0]!, 0) + expect(await advanceGoldenImages(t.deps)).toBe('built') + expect(await advanceGoldenImages(t.deps)).toBe('activated') + expect(t.raw.prepare(`SELECT source, bundle_sha256 FROM golden_images WHERE status = 'active'`).get()).toEqual({ + source: 'release:v1.4.256', + bundle_sha256: sha + }) + expect(await advanceGoldenImages(t.deps)).toBe('idle') + }) + + it('keeps refreshing when the release has no orca-server tarball yet', async () => { + const t = setup({ source: 'release:v1.4.254', fetch: async () => new Response('', { status: 404 }) }) + await advanceGoldenImages(t.deps) + expect(await advanceGoldenImages(t.deps)).toBe('release-unavailable') + }) + + it('serves creates from the rotated image, not the seed var', async () => { + const h = await createHarness() + h.raw + .prepare(`INSERT INTO golden_images (id, source, status, snapshot_id, created_at, updated_at, activated_at) VALUES ('g1', 'seed', 'active', 'snap_rotated', 1, 1, 1)`) + .run() + await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' } }) + expect([...h.vercel.boxes.values()][0]!.params.snapshotId).toBe('snap_rotated') + }) +}) + diff --git a/src/main/hosted-sandbox/hosted-sandbox-install.ts b/src/main/hosted-sandbox/hosted-sandbox-install.ts index a9fb2755506..4a60fc31849 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-install.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-install.ts @@ -1,5 +1,8 @@ // Wires Orca Cloud into the desktop when this build has a control plane configured (off by default). import { app, BrowserWindow } from 'electron' +import { ensureActiveOrcaProfile } from '../orca-profiles/profile-index-store' +import { getOrcaCloudAuthConfig } from '../orca-profiles/profile-cloud-auth-config' +import { readFreshOrcaCloudSession } from '../orca-profiles/profile-cloud-session-refresh' import type { OrcaVmRecipe } from '../../shared/orca-yaml-hook-types' import { getApprovedPluginVmRecipes } from '../plugins/plugin-approved-vm-recipes' import type { PluginService } from '../plugins/plugin-service' @@ -12,6 +15,9 @@ import { import { createHostedSessionRegistry, type HostedSessionRegistry } from './hosted-session-registry' import { setHostedSessionSync } from './hosted-session-sync' +/** Capability Orca Cloud auth grants per account (stablyai/orca-cloud#689). */ +export const ORCA_CLOUD_SANDBOX_CAPABILITY = 'sandbox' + let installed: { registry: HostedSessionRegistry } | null = null export function installHostedSandbox(): void { @@ -59,10 +65,30 @@ export function installHostedSandbox(): void { }) } +/** Orca Cloud auth's `sandbox` capability rolls it out per account; the dev key bypasses it in dev builds. */ +async function orcaCloudOffered(): Promise { + if (!app.isPackaged && process.env.ORCA_CLOUD_SANDBOX_DEV_KEY_FILE) { + return true + } + const auth = getOrcaCloudAuthConfig(process.env, app.isPackaged) + const userDataPath = app.getPath('userData') + const active = ensureActiveOrcaProfile(userDataPath) + if (!auth.configured || !active.profile.cloud) { + return false + } + const session = await readFreshOrcaCloudSession(auth.config, active, userDataPath).catch( + () => null + ) + return ( + session?.status === 'found' && + session.session.capabilities.flags[ORCA_CLOUD_SANDBOX_CAPABILITY] === true + ) +} + /** Built-in providers (Orca Cloud) ride along with approved plugin recipes and lead every list. */ export async function withProviderRecipes(pluginService?: PluginService): Promise { return [ - ...(installed ? [ORCA_CLOUD_RECIPE] : []), + ...(installed && (await orcaCloudOffered()) ? [ORCA_CLOUD_RECIPE] : []), ...(await getApprovedPluginVmRecipes(pluginService)) ] } From 7bbe80aefddaeb9a7c099b4f7403be665fd5cb05 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 15/18] 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 6cecb19b29f51010553d45ecc12a48511e4a1a98 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 16/18] 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 0f6db1c6bf0f7d5f4ef3bfa42cec96d89a79a03f 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 17/18] 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) From 85ce2f9994f8352bacbe0c7a7368a6a396e1edec Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 04:35:10 -0800 Subject: [PATCH 18/18] refactor(cloud): hosted driver implements the upstream EnvironmentProviderDriver Upstream now ships the generic interface (results are recipe documents; no lease, rollover, egress or entitlement concepts). Hosted extensions move to hosted-environment-provider.ts: HostedEnvironmentProviderDriver adds renewLease / rollover / leaseExpiresAt / egressInjection, and entitlement denials become HostedEntitlementDeniedError (code unauthorized, reason = deny reason). The capped-session rollover check moves from the shared kit into the hosted test. --- .../environment-provider-recipe-adapter.ts | 19 +++-- .../hosted-environment-provider.ts | 74 +++++++++++++++++++ .../hosted-sandbox-control-plane-client.ts | 8 +- .../hosted-sandbox-driver.test.ts | 52 +++++++++++-- .../hosted-sandbox/hosted-sandbox-driver.ts | 42 ++++++----- .../hosted-sandbox/hosted-sandbox-recipe.ts | 30 ++++---- .../hosted-session-lease-policy.ts | 8 +- .../hosted-session-lease-scheduler.ts | 23 +++--- .../hosted-session-lease.test.ts | 4 +- .../hosted-sandbox/hosted-session-registry.ts | 14 ++-- 10 files changed, 195 insertions(+), 79 deletions(-) create mode 100644 src/main/hosted-sandbox/hosted-environment-provider.ts diff --git a/src/main/environment-providers/environment-provider-recipe-adapter.ts b/src/main/environment-providers/environment-provider-recipe-adapter.ts index 4963bde6b8a..d4702cd0b1b 100644 --- a/src/main/environment-providers/environment-provider-recipe-adapter.ts +++ b/src/main/environment-providers/environment-provider-recipe-adapter.ts @@ -7,7 +7,7 @@ import { type JsonValue } from '../../shared/ephemeral-vm-recipes' import type { EphemeralVmCheckoutMode } from '../../shared/orca-yaml-hook-types' -import type { EnvironmentHandle, EnvironmentReady } from './environment-provider-driver' +import type { EnvironmentHandle } from './environment-provider-driver' const USER_DATA_KEY = 'environmentDriver' @@ -19,18 +19,23 @@ const HandleSchema = z }) .strict() -export function environmentReadyToRecipeResult( - ready: EnvironmentReady, +export function environmentRecipeResult( + handle: EnvironmentHandle, + orcaServer: { pairingCode: string; projectRoot: string }, checkoutMode: EphemeralVmCheckoutMode ): EphemeralVmRecipeResult { const userData: Record = { [USER_DATA_KEY]: { - driverId: ready.handle.driverId, - environmentId: ready.handle.environmentId, - driverState: ready.handle.driverState + driverId: handle.driverId, + environmentId: handle.environmentId, + driverState: handle.driverState } } - const connection = { ...ready.connection } + const connection = { + type: 'orca-server' as const, + pairingCode: orcaServer.pairingCode, + projectRoot: orcaServer.projectRoot + } return checkoutMode === 'provisioned-root' ? { schemaVersion: 2, checkoutMode: 'provisioned-root', connection, userData } : { schemaVersion: 1, connection, userData } diff --git a/src/main/hosted-sandbox/hosted-environment-provider.ts b/src/main/hosted-sandbox/hosted-environment-provider.ts new file mode 100644 index 00000000000..948856e89f5 --- /dev/null +++ b/src/main/hosted-sandbox/hosted-environment-provider.ts @@ -0,0 +1,74 @@ +// Orca Cloud's additions on top of the upstream EnvironmentProviderDriver: a lease on a capped +// session, rollover onto the same disk, egress-injected secrets and plan entitlement denials. +import { + EnvironmentProviderError, + type EnvironmentHandle, + type EnvironmentProviderCallContext, + type EnvironmentProviderCapabilities, + type EnvironmentProviderDriver, + type EnvironmentReady, + type EnvironmentSessionInfo, + type EnvironmentStatus +} from '../environment-providers/environment-provider-driver' + +export type HostedEnvironmentCapabilities = EnvironmentProviderCapabilities & + Readonly<{ + /** Secrets can be injected at the egress proxy instead of living in the VM. */ + egressInjection: boolean + }> + +export type HostedSessionInfo = EnvironmentSessionInfo & { + /** When the current Orca lease lapses unless renewed. */ + leaseExpiresAt: number | null +} + +export type HostedStatusSnapshot = { + status: EnvironmentStatus + session: HostedSessionInfo | null +} + +export type HostedEnvironmentReady = EnvironmentReady & { session: HostedSessionInfo } + +// Narrows the upstream return types; still assignable wherever an EnvironmentProviderDriver is expected. +export type HostedEnvironmentProviderDriver = Omit< + EnvironmentProviderDriver, + 'capabilities' | 'create' | 'resume' | 'suspend' | 'status' +> & { + readonly capabilities: HostedEnvironmentCapabilities + create(...args: Parameters): Promise + resume( + handle: EnvironmentHandle, + ctx?: EnvironmentProviderCallContext + ): Promise + suspend( + handle: EnvironmentHandle, + ctx?: EnvironmentProviderCallContext + ): Promise + status( + handle: EnvironmentHandle, + ctx?: EnvironmentProviderCallContext + ): Promise + /** Keep the capped session alive while Orca reports work. */ + renewLease( + handle: EnvironmentHandle, + extendMs: number, + ctx?: EnvironmentProviderCallContext + ): Promise + /** End this session and start a fresh one from the same disk. */ + rollover( + handle: EnvironmentHandle, + ctx?: EnvironmentProviderCallContext + ): Promise +} + +/** The plan refused the start; `reason` is the billing deny reason. Reads as `unauthorized` generically. */ +export class HostedEntitlementDeniedError extends EnvironmentProviderError { + constructor(message: string, reason: string | null) { + super('unauthorized', message, reason) + this.name = 'HostedEntitlementDeniedError' + } +} + +export function isHostedEntitlementDenied(error: unknown): error is HostedEntitlementDeniedError { + return error instanceof HostedEntitlementDeniedError +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts b/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts index 065d7474761..79543ce6931 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-control-plane-client.ts @@ -1,6 +1,7 @@ // HTTPS client for the Orca sandbox control plane. The desktop holds only a short-lived Orca token; // the provider credential never leaves the control plane. import type { z } from 'zod' +import { HostedEntitlementDeniedError } from './hosted-environment-provider' import { HostedSandboxErrorResponseSchema, HostedSandboxListResponseSchema, @@ -176,7 +177,6 @@ const STATUS_CODES: Record = { forbidden: 'unauthorized', not_found: 'not_found', conflict: 'conflict', - entitlement_denied: 'entitlement_denied', invalid_request: 'invalid_request', invalid_json: 'invalid_request', billing_unavailable: 'unavailable', @@ -187,6 +187,12 @@ const STATUS_CODES: Record = { function errorFromResponse(status: number, payload: unknown): EnvironmentProviderError { const parsed = HostedSandboxErrorResponseSchema.safeParse(payload) + if (parsed.success && parsed.data.error === 'entitlement_denied') { + return new HostedEntitlementDeniedError( + 'Orca Cloud refused the request (entitlement_denied).', + parsed.data.reason ?? null + ) + } if (parsed.success) { const code = STATUS_CODES[parsed.data.error] ?? 'unavailable' return new EnvironmentProviderError( diff --git a/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts b/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts index 35c71b03055..1f582a57a71 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-driver.test.ts @@ -1,11 +1,15 @@ import { describe, expect, it } from 'vitest' -import { parseEphemeralVmRecipeResult } from '../../shared/ephemeral-vm-recipes' +import { + getEphemeralVmRecipeResultConnection, + parseEphemeralVmRecipeResult +} from '../../shared/ephemeral-vm-recipes' import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver' import { runEnvironmentProviderDriverContract } from '../environment-providers/environment-provider-driver-contract.test-support' import { environmentHandleFromRecipeResult, - environmentReadyToRecipeResult + environmentRecipeResult } from '../environment-providers/environment-provider-recipe-adapter' +import { isHostedEntitlementDenied } from './hosted-environment-provider' import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' import { createFakeHostedControlPlane } from './fake-hosted-control-plane.test-support' @@ -17,7 +21,7 @@ function setup(options: { token?: string; fetch?: typeof fetch } = {}) { getAccessToken: async () => options.token ?? plane.token, fetch: options.fetch ?? plane.fetch }) - return { plane, driver: createHostedSandboxDriver(client) } + return { plane, client, driver: createHostedSandboxDriver(client) } } async function rejection(promise: Promise): Promise { @@ -28,11 +32,12 @@ async function rejection(promise: Promise): Promise { } runEnvironmentProviderDriverContract('HostedSandboxDriver', async () => { - const { plane, driver } = setup() + const { plane, client, driver } = setup() return { driver, liveEnvironmentIds: async () => - [...plane.sandboxes.values()].filter((s) => s.status !== 'destroyed').map((s) => s.id) + [...plane.sandboxes.values()].filter((s) => s.status !== 'destroyed').map((s) => s.id), + reopenDriver: () => createHostedSandboxDriver(client) } }) @@ -41,7 +46,7 @@ describe('HostedSandboxDriver', () => { const { plane, driver } = setup() plane.denyNextStart('included_hours_exhausted') const error = await rejection(driver.create({ workspaceId: 'ws-1' })) - expect(isEnvironmentProviderError(error, 'entitlement_denied')).toBe(true) + expect(isHostedEntitlementDenied(error)).toBe(true) expect(isEnvironmentProviderError(error) && error.reason).toBe('included_hours_exhausted') }) @@ -83,7 +88,14 @@ describe('HostedSandboxDriver', () => { it('round-trips through the recipe result the runtime record stores', async () => { const { driver } = setup() const ready = await driver.create({ workspaceId: 'ws-1' }) - const recipeResult = environmentReadyToRecipeResult(ready, 'provisioned-root') + expect(ready.result).toEqual( + environmentRecipeResult(ready.handle, readyConnection(ready), 'orca-worktree') + ) + const recipeResult = environmentRecipeResult( + ready.handle, + readyConnection(ready), + 'provisioned-root' + ) const parsed = parseEphemeralVmRecipeResult(JSON.stringify(recipeResult)) expect(parsed.ok).toBe(true) if (!parsed.ok) { @@ -97,4 +109,30 @@ describe('HostedSandboxDriver', () => { ready.handle.environmentId ) }) + + it('keeps a capped session alive and rolls it over onto the same disk', async () => { + const { driver } = setup() + const created = await driver.create({ workspaceId: 'ws-rollover' }) + const renewed = await driver.renewLease(created.handle, 10 * 60_000) + expect(renewed.status).toBe('running') + expect(renewed.session!.leaseExpiresAt!).toBeLessThanOrEqual(renewed.session!.deadlineAt!) + + const rolled = await driver.rollover(created.handle) + expect(rolled.handle.environmentId).toBe(created.handle.environmentId) + expect(rolled.session.startedAt).toBeGreaterThan(created.session.startedAt) + expect(rolled.session.deadlineAt! - rolled.session.startedAt).toBeLessThanOrEqual( + driver.capabilities.maxSessionMs! + ) + await driver.destroy(created.handle) + }) }) + +function readyConnection(ready: { + result: Parameters[0] +}) { + const connection = getEphemeralVmRecipeResultConnection(ready.result) + if (connection.type !== 'orca-server') { + throw new Error('expected an orca-server connection') + } + return connection +} diff --git a/src/main/hosted-sandbox/hosted-sandbox-driver.ts b/src/main/hosted-sandbox/hosted-sandbox-driver.ts index 244625e4489..d6b9bc690b3 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-driver.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-driver.ts @@ -10,19 +10,22 @@ import { EnvironmentProviderError, isEnvironmentProviderError, type EnvironmentHandle, - type EnvironmentProviderCapabilities, - type EnvironmentProviderDriver, - type EnvironmentReady, - type EnvironmentSessionInfo, - type EnvironmentStatus, - type EnvironmentStatusSnapshot + type EnvironmentStatus } from '../environment-providers/environment-provider-driver' +import { environmentRecipeResult } from '../environment-providers/environment-provider-recipe-adapter' +import type { + HostedEnvironmentCapabilities, + HostedEnvironmentProviderDriver, + HostedEnvironmentReady, + HostedSessionInfo, + HostedStatusSnapshot +} from './hosted-environment-provider' import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' export const HOSTED_SANDBOX_DRIVER_ID = 'orca-cloud' /** Measured on Vercel: stop is a reboot (PTYs lost), disk persists, Pro caps a session at 24h. */ -export const HOSTED_SANDBOX_CAPABILITIES: EnvironmentProviderCapabilities = Object.freeze({ +export const HOSTED_SANDBOX_CAPABILITIES: HostedEnvironmentCapabilities = Object.freeze({ memoryPause: false, maxSessionMs: 24 * 60 * 60_000, fsPersistOnStop: true, @@ -43,13 +46,12 @@ const STATUS: Record = { export function createHostedSandboxDriver( client: HostedSandboxControlPlaneClient -): EnvironmentProviderDriver { +): HostedEnvironmentProviderDriver { return { id: HOSTED_SANDBOX_DRIVER_ID, capabilities: HOSTED_SANDBOX_CAPABILITIES, async create(spec, ctx) { - ctx?.onProgress?.({ phase: 'checking-entitlement' }) const machineSize = toMachineSize(spec.machineSize) ctx?.onProgress?.({ phase: 'provisioning' }) const ready = await client.create( @@ -94,12 +96,11 @@ export function createHostedSandboxDriver( }, async renewLease(handle, extendMs, ctx) { - ctx?.onProgress?.({ phase: 'renewing' }) return toSnapshot(await client.renewLease(sandboxIdOf(handle), extendMs, ctx?.signal)) }, async rollover(handle, ctx) { - ctx?.onProgress?.({ phase: 'rolling-over' }) + ctx?.onProgress?.({ phase: 'resuming' }) const ready = await client.rollover(sandboxIdOf(handle), ctx?.signal) ctx?.onProgress?.({ phase: 'connecting' }) return toReady(ready) @@ -126,7 +127,7 @@ function sandboxIdOf(handle: EnvironmentHandle): string { return id } -function toSession(view: HostedSandboxView): EnvironmentSessionInfo | null { +function toSession(view: HostedSandboxView): HostedSessionInfo | null { return view.session ? { startedAt: view.session.startedAt, @@ -136,11 +137,11 @@ function toSession(view: HostedSandboxView): EnvironmentSessionInfo | null { : null } -function toSnapshot(view: HostedSandboxView): EnvironmentStatusSnapshot { +function toSnapshot(view: HostedSandboxView): HostedStatusSnapshot { return { status: STATUS[view.status], session: toSession(view) } } -function toReady(ready: HostedSandboxReadyResponse): EnvironmentReady { +function toReady(ready: HostedSandboxReadyResponse): HostedEnvironmentReady { const session = toSession(ready.sandbox) if (ready.sandbox.status !== 'running' || !session) { throw new EnvironmentProviderError( @@ -148,13 +149,14 @@ function toReady(ready: HostedSandboxReadyResponse): EnvironmentReady { 'Orca Cloud reported a connection for an environment that is not running.' ) } + const handle: EnvironmentHandle = { + driverId: HOSTED_SANDBOX_DRIVER_ID, + environmentId: ready.sandbox.id, + driverState: { sandboxId: ready.sandbox.id } + } return { - handle: { - driverId: HOSTED_SANDBOX_DRIVER_ID, - environmentId: ready.sandbox.id, - driverState: { sandboxId: ready.sandbox.id } - }, - connection: ready.connection, + handle, + result: environmentRecipeResult(handle, ready.connection, 'orca-worktree'), session } } diff --git a/src/main/hosted-sandbox/hosted-sandbox-recipe.ts b/src/main/hosted-sandbox/hosted-sandbox-recipe.ts index de1508ace00..3089c8ca5a6 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-recipe.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-recipe.ts @@ -11,15 +11,13 @@ import { type RecipeStepProviderResult } from '../../shared/ephemeral-vm-recipe-step-providers' import { getRemoteUrl } from '../git/repo' +import { isEnvironmentProviderError } from '../environment-providers/environment-provider-driver' +import { environmentHandleFromRecipeResult } from '../environment-providers/environment-provider-recipe-adapter' import { - isEnvironmentProviderError, - type EnvironmentProviderDriver, - type EnvironmentReady -} from '../environment-providers/environment-provider-driver' -import { - environmentHandleFromRecipeResult, - environmentReadyToRecipeResult -} from '../environment-providers/environment-provider-recipe-adapter' + isHostedEntitlementDenied, + type HostedEnvironmentProviderDriver, + type HostedEnvironmentReady +} from './hosted-environment-provider' import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' import { createHostedSandboxDriver, HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' import type { HostedSandboxConfig } from './hosted-sandbox-config' @@ -43,7 +41,7 @@ const LifecyclePayload = z.looseObject({ recipeResult: EphemeralVmRecipeResultSc export function createHostedSandboxRecipeDriver( config: HostedSandboxConfig -): EnvironmentProviderDriver { +): HostedEnvironmentProviderDriver { return createHostedSandboxDriver( createHostedSandboxControlPlaneClient({ baseUrl: config.controlPlaneUrl, @@ -66,7 +64,7 @@ function ok(stdout: string): RecipeStepProviderResult { function failed(error: unknown): RecipeStepProviderResult { const base = { stdout: '', exitCode: 1, signal: null } if ( - isEnvironmentProviderError(error, 'entitlement_denied') && + isHostedEntitlementDenied(error) && (error.reason === 'concurrency_limit' || error.reason === 'member_concurrency_limit') ) { return { @@ -75,7 +73,7 @@ function failed(error: unknown): RecipeStepProviderResult { failure: ephemeralVmFailure('cloudConcurrencyLimit') } } - if (isEnvironmentProviderError(error, 'entitlement_denied')) { + if (isHostedEntitlementDenied(error)) { return { ...base, stderr: `Orca Cloud: ${error.reason ?? 'no plan'}\n`, @@ -99,9 +97,9 @@ function failed(error: unknown): RecipeStepProviderResult { } export function createHostedSandboxStepProvider( - driver: EnvironmentProviderDriver, + driver: HostedEnvironmentProviderDriver, hooks: { - onReady?: (ready: EnvironmentReady) => void + onReady?: (ready: HostedEnvironmentReady) => void onStopped?: (environmentId: string) => void } = {} ): RecipeStepProvider { @@ -125,7 +123,7 @@ export function createHostedSandboxStepProvider( ctx ) hooks.onReady?.(ready) - return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree'))) + return ok(JSON.stringify(ready.result)) } const payload = LifecyclePayload.parse(JSON.parse(stdin ?? '{}')) const handle = payload.recipeResult @@ -137,7 +135,7 @@ export function createHostedSandboxStepProvider( if (mode === 'resume') { const ready = await driver.resume(handle, ctx) hooks.onReady?.(ready) - return ok(JSON.stringify(environmentReadyToRecipeResult(ready, 'orca-worktree'))) + return ok(JSON.stringify(ready.result)) } await (mode === 'suspend' ? driver.suspend(handle, ctx) : driver.destroy(handle, ctx)) hooks.onStopped?.(handle.environmentId) @@ -149,7 +147,7 @@ export function createHostedSandboxStepProvider( } export function registerHostedSandboxRecipe( - driver: EnvironmentProviderDriver, + driver: HostedEnvironmentProviderDriver, hooks: Parameters[1] = {} ): () => void { return registerEphemeralVmRecipeStepProvider( diff --git a/src/main/hosted-sandbox/hosted-session-lease-policy.ts b/src/main/hosted-sandbox/hosted-session-lease-policy.ts index e99c69de030..14fdda121c3 100644 --- a/src/main/hosted-sandbox/hosted-session-lease-policy.ts +++ b/src/main/hosted-sandbox/hosted-session-lease-policy.ts @@ -1,9 +1,7 @@ // What to do next with a session-capped, no-memory-pause environment (Vercel: stop = reboot, 24h cap). // Idle stop, the opportunistic refresh and the forced cap rollover are one decision so they never race. -import type { - EnvironmentProviderCapabilities, - EnvironmentSessionInfo -} from '../environment-providers/environment-provider-driver' +import type { EnvironmentProviderCapabilities } from '../environment-providers/environment-provider-driver' +import type { HostedSessionInfo } from './hosted-environment-provider' export type HostedActivitySample = { /** orcad's idle probes: clients, terminals, agents, automations. Unverifiable counts as busy. */ @@ -36,7 +34,7 @@ export const HOSTED_LEASE_POLICY = { export function decideHostedLeaseAction(input: { now: number capabilities: EnvironmentProviderCapabilities - session: EnvironmentSessionInfo + session: HostedSessionInfo activity: HostedActivitySample warned: ReadonlySet }): HostedLeaseAction { diff --git a/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts index caf06448193..26bde666efb 100644 --- a/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts +++ b/src/main/hosted-sandbox/hosted-session-lease-scheduler.ts @@ -1,11 +1,11 @@ // Drives one Orca Cloud environment's session: renews Orca's lease while work runs, warns before the // cap, suspends when idle, and rolls the session over (drain → stop → boot → re-pair → continue agents). +import type { EnvironmentHandle } from '../environment-providers/environment-provider-driver' import type { - EnvironmentHandle, - EnvironmentProviderDriver, - EnvironmentReady, - EnvironmentSessionInfo -} from '../environment-providers/environment-provider-driver' + HostedEnvironmentProviderDriver, + HostedEnvironmentReady, + HostedSessionInfo +} from './hosted-environment-provider' import type { HostedAgentAutoResumeOutcome } from './hosted-agent-auto-resume' import { HOSTED_LEASE_POLICY, @@ -16,13 +16,13 @@ import { } from './hosted-session-lease-policy' export type HostedSessionLeasePorts = { - driver: EnvironmentProviderDriver + driver: HostedEnvironmentProviderDriver handle: EnvironmentHandle - session: EnvironmentSessionInfo + session: HostedSessionInfo readActivity: () => Promise warn: (minutesLeft: HostedLeaseWarning) => void /** Re-pairs with the rebooted orcad; resolves once the runtime answers again. */ - reconnect: (ready: EnvironmentReady) => Promise + reconnect: (ready: HostedEnvironmentReady) => Promise resumeAgents: (drainStartedAt: number) => Promise onSuspended: () => void /** Replaces the driver suspend, e.g. to run the window's ordinary Sleep for the workspace. */ @@ -38,7 +38,7 @@ const UNVERIFIABLE: HostedActivitySample = { } export class HostedSessionLeaseScheduler { - private session: EnvironmentSessionInfo + private session: HostedSessionInfo private readonly warned = new Set() private timer: ReturnType | null = null private running: Promise | null = null @@ -84,7 +84,7 @@ export class HostedSessionLeaseScheduler { }) switch (action.kind) { case 'renew': { - const renewed = await this.ports.driver.renewLease?.(this.ports.handle, action.extendMs) + const renewed = await this.ports.driver.renewLease(this.ports.handle, action.extendMs) if (renewed?.session) { this.session = renewed.session } @@ -117,9 +117,6 @@ export class HostedSessionLeaseScheduler { private async rollover(reason: 'opportunistic' | 'forced'): Promise { const { driver, handle } = this.ports - if (!driver.rollover) { - return - } const drainStartedAt = this.now() this.log(`[orca-cloud] ${reason} rollover`) const ready = await driver.rollover(handle) diff --git a/src/main/hosted-sandbox/hosted-session-lease.test.ts b/src/main/hosted-sandbox/hosted-session-lease.test.ts index e381b24d8b5..ef018530cd6 100644 --- a/src/main/hosted-sandbox/hosted-session-lease.test.ts +++ b/src/main/hosted-sandbox/hosted-session-lease.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it, vi } from 'vitest' -import type { EnvironmentReady } from '../environment-providers/environment-provider-driver' +import type { HostedEnvironmentReady } from './hosted-environment-provider' import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume' import { createHostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' import { createHostedSandboxDriver, HOSTED_SANDBOX_CAPABILITIES } from './hosted-sandbox-driver' @@ -76,7 +76,7 @@ describe('hosted session lease scheduler', () => { }) ) const created = await driver.create({ workspaceId: 'ws-1' }) - const reconnect = vi.fn(async (_ready: EnvironmentReady) => {}) + const reconnect = vi.fn(async (_ready: HostedEnvironmentReady) => {}) const resumeAgents = vi.fn(async () => ({ offered: 2, continued: 2, diff --git a/src/main/hosted-sandbox/hosted-session-registry.ts b/src/main/hosted-sandbox/hosted-session-registry.ts index fb0153f2731..bc7094e7d19 100644 --- a/src/main/hosted-sandbox/hosted-session-registry.ts +++ b/src/main/hosted-sandbox/hosted-session-registry.ts @@ -15,11 +15,9 @@ import { callRuntimeEnvironment } from '../ipc/runtime-environments/runtime-envi import { invalidateRuntimeEnvironmentTransport } from '../ipc/runtime-environments/runtime-environments' import { applyResumedRuntimePairing } from '../ephemeral-vm/runtime-resume-pairing' import { noteRecipeVmWakePairing } from '../ephemeral-vm/recipe-vm-wake-pairings' -import type { EnvironmentProviderDriver } from '../environment-providers/environment-provider-driver' -import { - environmentHandleFromRecipeResult, - environmentReadyToRecipeResult -} from '../environment-providers/environment-provider-recipe-adapter' +import { getEphemeralVmRecipeResultPairingCode } from '../../shared/ephemeral-vm-recipes' +import { environmentHandleFromRecipeResult } from '../environment-providers/environment-provider-recipe-adapter' +import type { HostedEnvironmentProviderDriver } from './hosted-environment-provider' import { autoResumeAgentsAfterRollover } from './hosted-agent-auto-resume' import { HOSTED_SANDBOX_DRIVER_ID } from './hosted-sandbox-driver' import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call' @@ -43,7 +41,7 @@ export type HostedSessionRegistry = { export function createHostedSessionRegistry(deps: { userDataPath: string - driver: EnvironmentProviderDriver + driver: HostedEnvironmentProviderDriver windows: () => readonly BrowserWindow[] log?: (line: string) => void }): HostedSessionRegistry { @@ -100,12 +98,12 @@ export function createHostedSessionRegistry(deps: { }, reconnect: async (ready) => { updateEphemeralVmRuntimeStatus(deps.userDataPath, runtime.id, { - recipeResult: environmentReadyToRecipeResult(ready, 'orca-worktree') + recipeResult: ready.result }) const { environment } = await applyResumedRuntimePairing({ userDataPath: deps.userDataPath, environmentId: runtimeEnvironmentId, - pairingCode: ready.connection.pairingCode, + pairingCode: getEphemeralVmRecipeResultPairingCode(ready.result) ?? '', invalidateTransport: invalidateRuntimeEnvironmentTransport }) noteRecipeVmWakePairing(