Merge remote-tracking branch 'private/cloud/integration' into cloud/w2-final

This commit is contained in:
Neil
2026-11-09 17:36:10 -08:00
161 changed files with 12914 additions and 117 deletions
@@ -0,0 +1,790 @@
#!/usr/bin/env node
// Desktop (Electron) acceptance run for Orca Cloud: launches the dev Orca built from this checkout
// against the DEV cloud backends, drives the real UI over CDP (playwright-core), and writes
// results.json + screenshots into the artifacts dir given as argv[2].
//
// node cloud/apps/acceptance/desktop/run-desktop.mjs <artifactsDir>
//
// Env: ORCA_CLOUD_SANDBOX_URL / ORCA_CLOUD_SANDBOX_DEV_KEY_FILE / ORCA_BILLING_API_URL override the
// dev defaults; W9D_USER (default w9-desktop) is the dev account; W9D_WAKE_CYCLES (default 5);
// W9D_SKIP_BUILD=1 skips the workspace-package/native/CLI prebuild.
import { execFileSync, spawn, spawnSync } from 'node:child_process'
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync, createWriteStream, readdirSync, rmdirSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { chromium } from 'playwright-core'
import { DEV_KEY_FILE, SANDBOX_URL, deleteAllSandboxes, listSandboxes, sandboxApi } from './sandbox-api.mjs'
const repoRoot = path.resolve(import.meta.dirname, '../../../..')
const artifactsDir = path.resolve(process.argv[2] ?? `/tmp/orca-desktop-acceptance-${Date.now()}`)
const shotsDir = path.join(artifactsDir, 'screenshots')
mkdirSync(shotsDir, { recursive: true })
const USER = process.env.W9D_USER ?? 'w9-desktop'
const REPO_SLUG = 'nwparker/orca-sandbox-push-test'
const WAKE_CYCLES = Number(process.env.W9D_WAKE_CYCLES ?? 5)
const KEY_SAMPLES = 10
const HTTP_SAMPLES = 10
const PORT = 4173
const runTag = new Date().toISOString().replace(/[:.]/g, '-')
const wsName = `w9d-${Date.now().toString(36)}`
const HEAVY = '/tmp/vdrv/heavy.sh'
const results = {
runTag,
startedAt: new Date().toISOString(),
repoRoot,
gitHead: git('rev-parse', 'HEAD'),
branch: git('rev-parse', '--abbrev-ref', 'HEAD'),
sandboxUrl: SANDBOX_URL,
account: `dev:${USER}`,
workspaceName: wsName,
notes: [],
app: {},
steps: [],
bugs: [],
cleanup: {}
}
const created = { sandboxIds: new Set(), localPaths: [] }
let runner = null
let browser = null
let page = null
let ctx = null
function git(...args) {
try {
return execFileSync('git', ['-C', repoRoot, ...args], { encoding: 'utf8' }).trim()
} catch {
return null
}
}
function log(...a) {
console.error(`[desktop ${new Date().toISOString().slice(11, 19)}]`, ...a)
}
function save() {
results.updatedAt = new Date().toISOString()
writeFileSync(path.join(artifactsDir, 'results.json'), JSON.stringify(results, null, 2))
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
function pct(samples, p) {
const s = [...samples].sort((a, b) => a - b)
if (!s.length) return null
return s[Math.min(s.length - 1, Math.ceil((p / 100) * s.length) - 1)]
}
function stats(samples) {
return samples.length
? { n: samples.length, p50: pct(samples, 50), p95: pct(samples, 95), min: Math.min(...samples), max: Math.max(...samples), samples }
: { n: 0 }
}
const redact = (s) => String(s ?? '').replace(/[A-Za-z0-9_-]{32,}/g, '<redacted>')
async function shot(step, name) {
const file = path.join(shotsDir, `${step.id.replace(/[^a-z0-9.-]/gi, '_')}-${name}.png`)
try {
await page.screenshot({ path: file })
step.screenshots.push(path.relative(artifactsDir, file))
} catch (e) {
step.screenshots.push(`(screenshot failed: ${e.message})`)
}
return file
}
async function runStep(id, title, fn) {
const step = { id, title, status: 'fail', detail: '', screenshots: [], timings: {}, startedAt: new Date().toISOString() }
results.steps.push(step)
log('step', id, title)
const t0 = Date.now()
try {
await fn(step)
} catch (e) {
step.status = 'fail'
step.detail = `${step.detail ? step.detail + ' | ' : ''}error: ${redact(e.message).slice(0, 600)}`
await shot(step, 'error')
}
step.durationMs = Date.now() - t0
log(' ->', step.status, step.detail.slice(0, 200))
save()
return step
}
function bug(step, { title, expected, actual, repro, screenshot }) {
results.bugs.push({ step, title, expected, actual, repro, screenshot })
save()
}
// ---------- app lifecycle ----------
function prebuild() {
if (process.env.W9D_SKIP_BUILD === '1') return
const cmd = [
`cd ${JSON.stringify(repoRoot)}`,
'node config/scripts/workspace-package-build/build-workspace-packages.mjs',
'node config/scripts/native-build/node-addons/ensure-native-runtime.mjs --runtime=electron',
// `orca ssh` / Copy SSH Command tunnel through the dev CLI; `pnpm dev` does not build it.
...(existsSync(path.join(repoRoot, 'out/cli/index.js')) ? [] : ['node config/scripts/cli-build/build-cli.mjs'])
].join(' && ')
log('prebuild (heavy lock)')
const t0 = Date.now()
const r = existsSync(HEAVY)
? spawnSync(HEAVY, [cmd], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8' })
: spawnSync('bash', ['-c', cmd], { stdio: ['ignore', 'pipe', 'pipe'], encoding: 'utf8' })
writeFileSync(path.join(artifactsDir, 'prebuild.log'), `${r.stdout}\n${r.stderr}`)
results.app.prebuildMs = Date.now() - t0
if (r.status !== 0) throw new Error(`prebuild failed (${r.status}); see prebuild.log`)
}
const appEnv = (userData) => ({
...process.env,
ORCA_BACKGROUND_LAUNCH: '1',
ORCA_DEV_USER_DATA_PATH: userData,
ORCA_CLOUD_SANDBOX_URL: SANDBOX_URL,
ORCA_CLOUD_SANDBOX_DEV_KEY_FILE: DEV_KEY_FILE,
ORCA_BILLING_API_URL: process.env.ORCA_BILLING_API_URL ?? 'https://orca-billing-dev.lovecast.workers.dev',
ORCA_CLOUD_DEV_AUTH: '1',
ORCA_CLOUD_DEV_USER_ID: USER
})
async function launchApp(userData, label) {
const logFile = path.join(artifactsDir, `app-${label}.log`)
const out = createWriteStream(logFile)
const t0 = Date.now()
const child = spawn(process.execPath, ['config/scripts/development/launch/run-electron-vite-dev.mjs'], {
cwd: repoRoot,
env: appEnv(userData),
stdio: ['ignore', 'pipe', 'pipe']
})
runner = child
let buf = ''
const instance = await new Promise((resolve, reject) => {
const timer = setTimeout(() => reject(new Error('no ORCA_DEV_INSTANCE line within 180s')), 180_000)
const onData = (d) => {
out.write(d)
buf += d.toString()
const m = buf.match(/^ORCA_DEV_INSTANCE (\{.*\})$/m)
if (m) {
clearTimeout(timer)
resolve(JSON.parse(m[1]))
}
}
child.stdout.on('data', onData)
child.stderr.on('data', onData)
child.once('exit', (code) => reject(new Error(`dev runner exited early (${code}); see ${logFile}`)))
})
child.stdout.removeAllListeners('data')
child.stderr.removeAllListeners('data')
child.stdout.pipe(out)
child.stderr.pipe(out)
log('instance', instance.cdpUrl, instance.rendererUrl, 'runner pid', child.pid)
const deadline = Date.now() + 300_000
for (;;) {
try {
const targets = await (await fetch(`${instance.cdpUrl}/json`)).json()
if (targets.some((t) => t.type === 'page' && t.url.startsWith(instance.rendererUrl))) break
} catch {}
if (Date.now() > deadline) throw new Error('renderer never appeared on CDP')
await sleep(1000)
}
browser = await chromium.connectOverCDP(instance.cdpUrl)
page = browser.contexts().flatMap((c) => c.pages()).find((p) => p.url().startsWith(instance.rendererUrl))
if (!page) throw new Error('renderer page not found after connect')
await page.waitForFunction(() => Boolean(window.__store?.getState?.().repos), null, { timeout: 120_000 })
results.app[label] = { cdpUrl: instance.cdpUrl, rendererUrl: instance.rendererUrl, runnerPid: child.pid, launchMs: Date.now() - t0, log: path.relative(artifactsDir, logFile) }
save()
return instance
}
async function stopApp() {
if (!runner?.pid) return
const pid = runner.pid
const cmd = spawnSync('ps', ['-p', String(pid), '-o', 'command='], { encoding: 'utf8' }).stdout
if (!cmd.includes('run-electron-vite-dev.mjs')) {
log('refusing to kill pid', pid, 'cmd:', cmd.trim())
return
}
// Leave the CDP socket without closing the app's windows, then stop only our runner tree.
browser = null
runner.kill('SIGTERM')
for (let i = 0; i < 30 && spawnSync('ps', ['-p', String(pid)]).status === 0; i++) await sleep(500)
if (spawnSync('ps', ['-p', String(pid)]).status === 0) runner.kill('SIGKILL')
runner = null
}
// ---------- UI helpers ----------
const store = (fn, arg) => page.evaluate(fn, arg)
async function installToastRecorder() {
await page.evaluate(() => {
window.__w9dToasts = []
if (window.__w9dToastObs) return
window.__w9dToastObs = new MutationObserver(() => {
document.querySelectorAll('[data-sonner-toast]').forEach((t) => {
const s = t.innerText
if (s && !window.__w9dToasts.includes(s)) window.__w9dToasts.push(s)
})
})
window.__w9dToastObs.observe(document.body, { subtree: true, childList: true, characterData: true })
})
}
const toasts = () => page.evaluate(() => window.__w9dToasts ?? [])
const clearToasts = () => page.evaluate(() => { window.__w9dToasts = [] })
function rowTitle(name) {
const esc = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
return page.locator('[data-worktree-title-inline-rename]', { hasText: new RegExp(`^${esc}$`) }).first()
}
async function openRowMenu(name) {
await page.keyboard.press('Escape').catch(() => {})
await rowTitle(name).click({ button: 'right' })
await page.getByRole('menuitem').first().waitFor({ timeout: 5000 })
await sleep(250)
}
const menuItems = () => page.getByRole('menuitem').allInnerTexts()
async function dismissNoise() {
for (const name of ['No thanks', 'Later']) {
await page.getByRole('button', { name, exact: true }).first().click({ timeout: 500 }).catch(() => {})
}
}
async function visibleTerminals() {
return page.evaluate(() =>
[...document.querySelectorAll('.xterm-rows')]
.filter((r) => r.getBoundingClientRect().width > 0)
.map((r) => r.innerText.replace(/ /g, ' ').trimEnd())
)
}
async function focusTerminal() {
await page.locator('.xterm-screen').filter({ visible: true }).last().click()
}
const lastLine = (t) => t.split('\n').filter((l) => l.trim()).pop() ?? ''
const isShellPrompt = (t) => /\$\s*$/.test(lastLine(t))
async function openNewTerminalTab() {
await page.locator('button[aria-label="New tab"]:visible').first().click()
await page.getByRole('menuitem', { name: /New Terminal/ }).click()
}
/** Keydown (capture listener, in-page clock) -> the char shows up in the visible xterm DOM. */
async function measureKeystroke(marker) {
await page.keyboard.type(marker.slice(0, -1))
await sleep(350)
await page.evaluate((marker) => {
const rows = [...document.querySelectorAll('.xterm-rows')].filter((r) => r.getBoundingClientRect().width > 0).pop()
window.__w9dLat = { down: 0, echo: 0 }
window.addEventListener('keydown', () => { if (!window.__w9dLat.down) window.__w9dLat.down = performance.now() }, { capture: true, once: true })
const obs = new MutationObserver(() => {
if (!window.__w9dLat.echo && rows.innerText.includes(marker)) {
window.__w9dLat.echo = performance.now()
obs.disconnect()
}
})
obs.observe(rows, { subtree: true, childList: true, characterData: true })
}, marker)
await page.keyboard.press(marker.slice(-1))
await page.waitForFunction(() => window.__w9dLat.echo > 0, null, { timeout: 15_000 })
const l = await page.evaluate(() => window.__w9dLat)
await page.keyboard.press('Control+U')
await sleep(250)
return Math.round(l.echo - l.down)
}
/** The sandbox this run created: listed under the dev account, created after Create was clicked. */
async function sandboxForWorkspace(since) {
const { items } = await listSandboxes(USER)
const mine = items.filter((s) => (s.createdAt ?? 0) >= since - 5000)
return { items, newest: mine.sort((a, b) => (b.createdAt ?? 0) - (a.createdAt ?? 0))[0] ?? null }
}
async function sandboxStatus(id) {
const r = await sandboxApi(USER, 'GET', `/v1/sandboxes/${id}`)
return { http: r.status, status: r.json?.sandbox?.status ?? r.json?.error ?? null }
}
async function waitSandboxStatus(id, want, timeoutMs) {
const t0 = Date.now()
let last = null
while (Date.now() - t0 < timeoutMs) {
last = await sandboxStatus(id)
if (want.includes(last.status)) return { ...last, ms: Date.now() - t0 }
await sleep(1000)
}
return { ...last, ms: Date.now() - t0, timedOut: true }
}
// ---------- flow ----------
async function main() {
save()
if (!existsSync(DEV_KEY_FILE)) throw new Error(`dev key file missing: ${DEV_KEY_FILE}`)
const before = await listSandboxes(USER)
results.preexistingSandboxes = before.items.map((s) => ({ id: s.id, status: s.status ?? s.state }))
prebuild()
const tmp = mkdtempSync(path.join(os.tmpdir(), 'orca-w9d-'))
const userData = path.join(tmp, 'profile')
const repoDir = path.join(tmp, 'repos', 'orca-sandbox-push-test')
mkdirSync(userData, { recursive: true })
results.app.userData = userData
execFileSync('gh', ['repo', 'clone', REPO_SLUG, repoDir, '--', '--quiet'], { stdio: 'ignore' })
created.tmp = tmp
await runStep('setup', 'Launch dev Orca, sign in (dev auth), add repo', async (step) => {
await launchApp(userData, 'first')
// Sign in before any cloud call, then relaunch: the sandbox token cache is not keyed on the
// signed-in identity (see bugs), so a fresh process guarantees the dev:<user> owner.
await store(async () => {
const s = window.__store.getState()
await s.connectCurrentOrcaProfile?.()
await s.fetchOrcaProfiles?.()
})
const who = await store(() => window.__store.getState().orcaProfiles?.map((p) => p.cloud?.userId ?? null))
step.timings.firstLaunchMs = results.app.first.launchMs
await stopApp()
await launchApp(userData, 'main')
step.timings.relaunchMs = results.app.main.launchMs
await store(() => window.__store.getState().updateSettings({ terminalGpuAcceleration: 'off' }))
await store(async (p) => {
await window.api.repos.add({ path: p })
await window.__store.getState().fetchRepos({})
}, repoDir)
const repos = await store(() => window.__store.getState().repos.map((r) => r.displayName))
await installToastRecorder()
step.status = who?.includes(USER) && repos.includes('orca-sandbox-push-test') ? 'pass' : 'partial'
step.detail = `signed in as ${JSON.stringify(who)}; repos ${JSON.stringify(repos)}; terminal GPU off so xterm renders to DOM`
await shot(step, 'home')
})
let sandboxId = null
await runStep('cli.desktop', 'Create an Orca Cloud workspace from the composer', async (step) => {
await page.getByRole('button', { name: 'New workspace', exact: true }).first().click()
const d = page.getByRole('dialog')
await d.waitFor()
await d.getByRole('combobox', { name: 'Project' }).click()
await page.getByRole('option', { name: /orca-sandbox-push-test/ }).first().click()
await d.getByRole('combobox', { name: 'Run on' }).click()
const targets = page.getByRole('listbox', { name: 'Run targets' })
await targets.waitFor()
step.runTargets = (await targets.getByRole('option').allInnerTexts()).map((t) => t.split('\n')[0])
await shot(step, 'picker')
await targets.getByRole('option', { name: /Orca Cloud/ }).click()
await sleep(800)
await d.getByRole('button', { name: 'Later', exact: true }).click({ timeout: 1500 }).catch(() => {})
step.composerHint = (await d.innerText()).match(/Hosted sandbox[\s\S]*?idle/)?.[0]?.replace(/\s+/g, ' ')
// Smart mode offers "Use … as workspace name"; the Name tab takes the text as is.
await d.getByPlaceholder(/Type a name|Workspace name/).fill(wsName)
await page.getByRole('option', { name: /as workspace name/ }).click({ timeout: 2000 }).catch(() => {})
await shot(step, 'composer-orca-cloud')
await clearToasts()
const t0 = Date.now()
await d.getByRole('button', { name: /Create workspace/ }).click()
let provisioningShot = false
let firstOutput = null
const phases = []
for (;;) {
const s = await page.evaluate(() => {
const body = document.body.innerText
const rows = [...document.querySelectorAll('.xterm-rows')].filter((r) => r.getBoundingClientRect().width > 0)
return {
starting: /Starting your cloud workspace/.test(body),
failed: /Couldn.t start your cloud workspace/.test(body),
text: (body.match(/Starting your cloud workspace[\s\S]{0,400}/)?.[0] ?? '').replace(/\s+/g, ' '),
xterm: rows.map((r) => r.innerText.trim().length)
}
})
const t = Date.now() - t0
const phase = s.text.match(/(Start sandbox|Prepare environment|Open workspace) In progress/)?.[1] ?? null
if (phase && phases.at(-1)?.phase !== phase) phases.push({ phase, t })
if (s.starting && !provisioningShot && t > 1500) {
await shot(step, 'provisioning')
provisioningShot = true
}
if (s.failed) {
step.detail = `create failed: ${redact((await page.locator('main').innerText().catch(() => '')).slice(0, 400))}`
throw new Error(step.detail)
}
if (!s.starting && s.xterm.some((n) => n > 0)) {
firstOutput = t
break
}
if (t > 300_000) throw new Error('workspace not ready in 300s')
await sleep(100)
}
step.timings.createToFirstTerminalOutputMs = firstOutput
step.timings.phases = phases
await dismissNoise()
await shot(step, 'ready-agent-terminal')
// A plain shell next to the agent the composer launches: usable once it prints a prompt.
const t1 = Date.now()
await openNewTerminalTab()
await page.waitForFunction(
() => [...document.querySelectorAll('.xterm-rows')].some((r) => r.getBoundingClientRect().width > 0 && /\$\s*$/.test(r.innerText.trim())),
null,
{ timeout: 60_000 }
)
step.timings.newTerminalToPromptMs = Date.now() - t1
step.timings.createToShellPromptMs = Date.now() - t0
const prompt = lastLine((await visibleTerminals()).pop() ?? '')
step.prompt = prompt
await shot(step, 'ready-shell')
const sb = await sandboxForWorkspace(t0)
sandboxId = sb.newest?.id ?? null
if (sandboxId) created.sandboxIds.add(sandboxId)
step.sandboxId = sandboxId
step.sandboxOwnerOk = Boolean(sandboxId)
// The cloud copy of the repo registers as its own project; its name should stay the repo's.
const repoNames = await store(() => window.__store.getState().repos.map((r) => ({ name: r.displayName, path: r.path })))
step.repos = repoNames
const renamed = repoNames.find((r) => r.path.startsWith('/vercel/') && r.name !== 'orca-sandbox-push-test')
if (renamed) {
const projectHeader = await page.getByText(renamed.name, { exact: true }).first().isVisible().catch(() => false)
bug('cli.desktop', {
title: `Cloud repo registers as "${renamed.name}" (basename of ${renamed.path}) and the sidebar project group / composer project list show that name instead of orca-sandbox-push-test`,
expected: 'project keeps the repo name orca-sandbox-push-test',
actual: `repo ${renamed.path} named "${renamed.name}"; sidebar header shows it: ${projectHeader}`,
repro: 'Create an Orca Cloud workspace for a repo whose cloud checkout lands in /vercel/sandbox/project; look at the sidebar project header',
screenshot: step.screenshots.at(-1)
})
}
const cloudy = /^ubuntu@/.test(prompt) && /orca\/workspaces/.test(prompt)
step.status = cloudy && sandboxId ? 'pass' : 'partial'
step.detail = `prompt "${prompt}"; sandbox ${sandboxId ?? 'NOT listed for ' + USER}; create->first terminal output ${firstOutput}ms, create->shell prompt ${step.timings.createToShellPromptMs}ms`
if (!sandboxId) {
bug('cli.desktop', {
title: 'Created sandbox is not owned by the signed-in dev account',
expected: `GET /v1/sandboxes as ${USER} lists the new sandbox`,
actual: 'not listed',
screenshot: step.screenshots.at(-1)
})
}
})
if (!sandboxId) results.notes.push('No sandbox id resolved for the workspace; lifecycle checks below rely on the UI only.')
const workspaceRow = await rowTitle(wsName).isVisible().catch(() => false)
if (!workspaceRow) {
for (const id of ['term.keystroke', 'acc.port-forward', 'acc.ssh', 'life.sleep-wake', 'life.transfer-local', 'life.destroy']) {
results.steps.push({ id, status: 'skipped', detail: 'no cloud workspace row (create failed)', screenshots: [], timings: {} })
}
save()
return
}
await runStep('term.keystroke', `First-keystroke latency in the cloud xterm (n=${KEY_SAMPLES})`, async (step) => {
await focusTerminal()
const samples = []
for (let i = 0; i < KEY_SAMPLES; i++) samples.push(await measureKeystroke(`k${i}${String.fromCharCode(97 + i)}q`))
step.timings.keystrokeToEchoMs = stats(samples)
step.status = pct(samples, 95) < 250 ? 'pass' : 'partial'
step.detail = `keydown -> char in xterm DOM: p50 ${pct(samples, 50)}ms, p95 ${pct(samples, 95)}ms (DOM renderer)`
await shot(step, 'typed')
})
await runStep('acc.port-forward', `Ports panel detects :${PORT}, forward to localhost, ${HTTP_SAMPLES} GETs`, async (step) => {
const preLocal = await fetch(`http://localhost:${PORT}/`).then((r) => r.status, () => 'closed')
step.localPortBefore = preLocal
await focusTerminal()
await page.keyboard.type(`python3 -m http.server ${PORT}\n`)
await page.waitForFunction((p) => [...document.querySelectorAll('.xterm-rows')].some((r) => r.innerText.includes(`port ${p}`)), PORT, { timeout: 20_000 })
const tServe = Date.now()
await page.locator('[aria-label^="Ports ("]').first().click()
const portRow = page.getByText(`:${PORT}`, { exact: true }).first()
await portRow.waitFor({ timeout: 60_000 })
step.timings.serveToPanelRowMs = Date.now() - tServe
let forwardedMs = null
for (let i = 0; i < 120; i++) {
const ok = await page.getByText(`localhost:${PORT}`).first().isVisible().catch(() => false)
const open = await fetch(`http://localhost:${PORT}/`).then((r) => r.ok, () => false)
if (ok && open) {
forwardedMs = Date.now() - tServe
break
}
await sleep(250)
}
step.timings.serveToLocalhostReachableMs = forwardedMs
step.autoForwardToast = (await toasts()).find((t) => /Forwarded/.test(t)) ?? null
await shot(step, 'ports-panel')
if (forwardedMs === null) {
step.status = 'partial'
step.detail = 'port row shown but localhost not reachable within 30s'
return
}
const samples = []
let server = null
let body = ''
for (let i = 0; i < HTTP_SAMPLES; i++) {
const s = performance.now()
const r = await fetch(`http://localhost:${PORT}/`)
body = await r.text()
samples.push(Math.round(performance.now() - s))
server = r.headers.get('server')
}
step.timings.httpGetMs = stats(samples)
const fromVm = /SimpleHTTP/i.test(server ?? '') && /README\.md/.test(body)
step.status = fromVm && preLocal === 'closed' ? 'pass' : 'partial'
step.detail = `auto-forwarded ${step.autoForwardToast ? `("${step.autoForwardToast.split('\n')[0]}")` : ''}; server "${server}"; listing has README.md: ${/README\.md/.test(body)}; GET p50 ${pct(samples, 50)}ms p95 ${pct(samples, 95)}ms`
await focusTerminal()
await page.keyboard.press('Control+C')
await sleep(500)
})
await runStep('acc.ssh', 'Copy SSH Command / Open in editor / orca ssh', async (step) => {
await openRowMenu(wsName)
step.contextMenu = (await menuItems()).map((t) => t.split('\n')[0])
await page.getByRole('menuitem', { name: 'Open in', exact: true }).hover()
await sleep(600)
const afterOpenIn = (await menuItems()).map((t) => t.replace(/\n/g, ' '))
step.openInOptions = afterOpenIn.slice(step.contextMenu.length)
await shot(step, 'open-in-menu')
// A fresh menu: hovering from one open submenu to another does not swap them reliably.
await page.keyboard.press('Escape')
await page.keyboard.press('Escape')
await openRowMenu(wsName)
await page.getByRole('menuitem', { name: 'SSH', exact: true }).hover()
await sleep(600)
const clipBefore = spawnSync('pbpaste', { encoding: 'utf8' }).stdout
await clearToasts()
const t0 = Date.now()
await page.getByRole('menuitem', { name: 'Copy SSH Command' }).click()
await page.waitForFunction(() => (window.__w9dToasts ?? []).some((t) => /copied|Couldn/.test(t)), null, { timeout: 90_000 })
step.timings.copySshCommandMs = Date.now() - t0
const tl = await toasts()
await shot(step, 'ssh-copied')
const clip = spawnSync('pbpaste', { encoding: 'utf8' }).stdout.trim()
spawnSync('pbcopy', { input: clipBefore })
step.toasts = tl.map(redact)
const copied = /^ssh /.test(clip) ? clip : null
step.sshCommand = redact(copied ?? '(not copied)')
let sshOk = false
if (copied) {
const parts = copied.split(/\s+/).slice(1)
const t1 = Date.now()
const r = spawnSync('ssh', ['-o', 'BatchMode=yes', '-o', 'ConnectTimeout=30', ...parts, 'echo ssh-ok $(hostname)'], { encoding: 'utf8', timeout: 90_000 })
step.timings.sshRoundTripMs = Date.now() - t1
step.sshResult = redact((r.stdout + r.stderr).trim().split('\n').slice(-3).join(' | '))
sshOk = /ssh-ok/.test(r.stdout)
}
const cli = path.join(repoRoot, 'out/cli/index.js')
let cliOk = false
if (existsSync(cli)) {
const t2 = Date.now()
const r = spawnSync(process.execPath, [cli, 'ssh', wsName, '--command', 'echo orca-ssh-ok $(hostname)'], {
encoding: 'utf8',
timeout: 120_000,
env: { ...process.env, ORCA_USER_DATA_PATH: results.app.userData }
})
step.timings.orcaSshMs = Date.now() - t2
step.orcaSshResult = redact((r.stdout + r.stderr).trim().split('\n').slice(-3).join(' | '))
cliOk = /orca-ssh-ok/.test(r.stdout)
} else {
step.orcaSshResult = 'out/cli/index.js missing'
}
step.status = sshOk && cliOk ? 'pass' : sshOk || cliOk ? 'partial' : 'fail'
step.detail = `Copy SSH Command -> ${step.sshCommand}; ssh: ${sshOk ? 'ok' : 'FAILED'} (${step.timings.sshRoundTripMs ?? '-'}ms); orca ssh: ${cliOk ? 'ok' : 'FAILED'} (${step.timings.orcaSshMs ?? '-'}ms); Open in offers ${JSON.stringify(step.openInOptions)} (not launched)`
})
await runStep('life.sleep-wake', `Suspend VM via UI, wake by opening the workspace, wake -> first echo (n=${WAKE_CYCLES})`, async (step) => {
const suspendMs = []
const wakeEchoMs = []
const cycles = []
for (let c = 0; c < WAKE_CYCLES; c++) {
const cyc = { cycle: c + 1 }
cycles.push(cyc)
await openRowMenu(wsName)
const ts = Date.now()
await page.getByRole('menuitem', { name: /^(Suspend VM|Sleep)$/ }).first().click()
await page.getByRole('dialog').getByRole('button', { name: /^(Sleep|Suspend)/ }).click({ timeout: 1500 }).catch(() => {})
if (sandboxId) {
const w = await waitSandboxStatus(sandboxId, ['suspended', 'stopped'], 120_000)
cyc.suspend = w
if (w.timedOut) throw new Error(`sandbox not suspended after 120s (last ${w.status})`)
} else {
await sleep(20_000)
}
cyc.suspendMs = Date.now() - ts
suspendMs.push(cyc.suspendMs)
await sleep(1500)
if (c === 0) await shot(step, 'asleep')
// Wake: open the workspace from the sidebar, keep typing until an echo makes it back.
await clearToasts()
const t0 = Date.now()
await rowTitle(wsName).click()
let wakingShot = c !== 0
let echo = null
let attempts = 0
let lastType = 0
const marker = `echo wake${c + 1}`
while (Date.now() - t0 < 180_000) {
if (!wakingShot && Date.now() - t0 > 1500) {
await shot(step, 'waking')
wakingShot = true
}
const terms = await visibleTerminals()
if (terms.some((t) => t.includes(marker))) {
echo = Date.now() - t0
break
}
if (terms.length && Date.now() - lastType > 1500) {
await focusTerminal().catch(() => {})
await page.keyboard.press('Control+U')
await page.keyboard.type(marker)
attempts++
lastType = Date.now()
}
await sleep(50)
}
if (echo === null) throw new Error(`no echo within 180s of wake (cycle ${c + 1})`)
cyc.wakeToEchoMs = echo
cyc.typeAttempts = attempts
wakeEchoMs.push(echo)
await page.keyboard.press('Control+U')
if (sandboxId) cyc.statusAfterWake = (await sandboxStatus(sandboxId)).status
cyc.prompt = lastLine((await visibleTerminals()).pop() ?? '')
if (c === 0) await shot(step, 'woken')
await sleep(2000)
}
step.cycles = cycles
step.timings.suspendToSuspendedMs = stats(suspendMs)
step.timings.wakeToFirstEchoMs = stats(wakeEchoMs)
step.status = wakeEchoMs.length === WAKE_CYCLES ? 'pass' : 'partial'
step.detail = `${wakeEchoMs.length}/${WAKE_CYCLES} cycles; suspend p50 ${pct(suspendMs, 50)}ms; wake->echo p50 ${pct(wakeEchoMs, 50)}ms p95 ${pct(wakeEchoMs, 95)}ms`
})
await runStep('life.transfer-local', 'Move to this computer', async (step) => {
await openRowMenu(wsName)
const items = (await menuItems()).map((t) => t.split('\n')[0])
if (!items.includes('Move to this computer')) {
step.status = 'partial'
step.detail = `absent; menu: ${JSON.stringify(items)}`
await page.keyboard.press('Escape')
return
}
await clearToasts()
const t0 = Date.now()
await page.getByRole('menuitem', { name: 'Move to this computer' }).click()
await sleep(800)
const dlg = await page.getByRole('dialog').innerText().catch(() => null)
if (dlg) {
step.confirmDialog = dlg.replace(/\s+/g, ' ')
await page.getByRole('dialog').getByRole('button', { name: /Move|Transfer|Continue/ }).first().click()
}
await shot(step, 'started')
let local = null
while (Date.now() - t0 < 180_000) {
local = await store((n) => Object.values(window.__store.getState().worktreesByRepo ?? {}).flat().find((w) => !w.path.startsWith('/vercel') && w.path.includes(n)), wsName)
if (local) break
await sleep(500)
}
step.timings.transferMs = Date.now() - t0
step.toasts = (await toasts()).map(redact)
await sleep(1500)
await shot(step, 'done')
if (!local) throw new Error('no local worktree appeared within 180s')
created.localPaths.push(local.path)
step.localWorktree = { name: local.displayName, path: local.path, branch: local.branch }
const files = existsSync(local.path) ? readdirSync(local.path) : []
const cloudStill = sandboxId ? (await sandboxStatus(sandboxId)).status : null
step.cloudStatusAfter = cloudStill
step.status = files.includes('README.md') ? 'pass' : 'partial'
step.detail = `local worktree "${local.displayName}" at ${local.path} (files: ${files.join(',')}); ${step.timings.transferMs}ms; cloud sandbox after: ${cloudStill} (copy, not move); no confirmation dialog: ${!dlg}`
// Remove the local copy through the UI so the repo stays clean.
await openRowMenu(local.displayName)
await page.getByRole('menuitem', { name: /^Delete/ }).click()
await page.getByRole('dialog').getByRole('button', { name: /^Delete( Workspace)?$/ }).click()
await rowTitle(local.displayName).waitFor({ state: 'detached', timeout: 60_000 })
step.localCopyDeleted = !existsSync(local.path)
})
await runStep('life.destroy', 'Delete the cloud workspace through the UI and confirm the VM is gone', async (step) => {
await openRowMenu(wsName)
await page.getByRole('menuitem', { name: /^Delete/ }).click()
const d = page.getByRole('dialog')
step.dialog = (await d.innerText()).replace(/\s+/g, ' ')
await shot(step, 'confirm')
const t0 = Date.now()
await d.getByRole('button', { name: /^Delete( Workspace)?$/ }).click()
await rowTitle(wsName).waitFor({ state: 'detached', timeout: 60_000 })
step.timings.uiRowGoneMs = Date.now() - t0
await shot(step, 'after')
if (!sandboxId) {
step.status = 'partial'
step.detail = 'row removed; no sandbox id to confirm against the API'
return
}
const w = await waitSandboxStatus(sandboxId, ['destroyed', 'deleted', 'not_found'], 120_000)
step.timings.deleteToDestroyedMs = w.ms
step.finalSandboxStatus = w.status
const listed = (await listSandboxes(USER)).items.some((s) => s.id === sandboxId)
step.status = !w.timedOut ? 'pass' : 'fail'
step.detail = `row gone in ${step.timings.uiRowGoneMs}ms; sandbox ${sandboxId} -> ${w.status} after ${w.ms}ms; still listed: ${listed}`
if (w.timedOut) {
bug('life.destroy', {
title: 'Deleting a cloud workspace in the UI leaves the sandbox running',
expected: 'sandbox destroyed',
actual: `status ${w.status} 120s after delete`,
screenshot: step.screenshots.at(-1)
})
}
})
}
async function cleanup() {
const c = results.cleanup
try {
c.sandboxes = await deleteAllSandboxes(USER)
for (const id of created.sandboxIds) {
const s = await sandboxStatus(id)
if (!['destroyed', 'deleted', 'not_found'].includes(s.status)) {
const r = await sandboxApi(USER, 'DELETE', `/v1/sandboxes/${id}`)
c.sandboxes.deleted.push({ id, forced: true, deleteStatus: r.status })
}
}
c.remaining = (await listSandboxes(USER)).items.map((s) => ({ id: s.id, status: s.status ?? s.state }))
} catch (e) {
c.sandboxError = e.message
}
try {
await stopApp()
c.appStopped = true
} catch (e) {
c.appStopError = e.message
}
// Orca's terminal daemon (and its shells) outlive the app by design; stop the ones bound to this
// run's throwaway profile — nothing else matches that unique temp path.
if (created.tmp) {
await sleep(2000)
const ps = spawnSync('ps', ['-axo', 'pid=,command='], { encoding: 'utf8' }).stdout
const pids = ps
.split('\n')
.filter((l) => l.includes(created.tmp))
.map((l) => Number(l.trim().split(/\s+/)[0]))
.filter((pid) => pid && pid !== process.pid)
for (const pid of pids) {
try {
process.kill(pid, 'SIGTERM')
} catch {}
}
c.profileProcessesStopped = pids.length
}
for (const p of created.localPaths) {
if (existsSync(p)) {
c.leftoverLocal = [...(c.leftoverLocal ?? []), p]
}
try {
rmdirSync(path.dirname(p)) // only succeeds when empty
} catch {}
}
if (created.tmp && process.env.W9D_KEEP_PROFILE !== '1') rmSync(created.tmp, { recursive: true, force: true })
save()
}
let exitCode = 0
try {
await main()
} catch (e) {
results.fatal = redact(e.stack ?? e.message)
exitCode = 1
} finally {
await cleanup()
results.finishedAt = new Date().toISOString()
results.summary = results.steps.map((s) => `${s.id}: ${s.status}`)
save()
log('results', path.join(artifactsDir, 'results.json'))
process.exit(exitCode || (results.steps.some((s) => s.status === 'fail') ? 2 : 0))
}
@@ -0,0 +1,72 @@
// Direct dev control-plane calls for the desktop acceptance run: list/delete leftovers and probe
// endpoints the UI depends on. Tokens are dev-signed (ES256) with the kid from the key file.
import { readFileSync } from 'node:fs'
import { SignJWT, importJWK } from 'jose'
export const SANDBOX_URL =
process.env.ORCA_CLOUD_SANDBOX_URL ?? 'https://orca-sandbox-control-dev.lovecast.workers.dev'
export const DEV_KEY_FILE = process.env.ORCA_CLOUD_SANDBOX_DEV_KEY_FILE ?? '/tmp/vdrv/secrets/dev-jwk.json'
export async function sandboxToken(sub) {
const file = JSON.parse(readFileSync(DEV_KEY_FILE, 'utf8'))
const kid = file.private.kid ?? file.kid ?? 'dev1'
const key = await importJWK(file.private, 'ES256')
return new SignJWT({ sub, purpose: 'sandbox' })
.setProtectedHeader({ alg: 'ES256', kid })
.setIssuer('orca-sandbox-dev-test')
.setAudience('orca-sandbox')
.setIssuedAt()
.setExpirationTime('5m')
.sign(key)
}
export async function sandboxApi(sub, method, path, body) {
const res = await fetch(SANDBOX_URL + path, {
method,
headers: {
authorization: `Bearer ${await sandboxToken(sub)}`,
...(body ? { 'content-type': 'application/json' } : {})
},
body: body ? JSON.stringify(body) : undefined
})
const text = await res.text()
let json = null
try {
json = JSON.parse(text)
} catch {}
return { status: res.status, json, text: json ? undefined : text.slice(0, 300) }
}
export async function listSandboxes(sub) {
const r = await sandboxApi(sub, 'GET', '/v1/sandboxes')
const items = r.json?.sandboxes ?? r.json?.items ?? (Array.isArray(r.json) ? r.json : [])
return { status: r.status, items }
}
/** Deletes every sandbox owned by `sub` that is not already gone; returns what it did. */
export async function deleteAllSandboxes(sub) {
const { status, items } = await listSandboxes(sub)
const live = items.filter((s) => !/^(deleted|destroyed|deleting)$/i.test(String(s.state ?? s.status ?? '')))
const deleted = []
for (const s of live) {
const r = await sandboxApi(sub, 'DELETE', `/v1/sandboxes/${encodeURIComponent(s.id)}`)
deleted.push({ id: s.id, state: s.state ?? s.status, deleteStatus: r.status })
}
return { listStatus: status, seen: items.length, deleted }
}
if (import.meta.url === `file://${process.argv[1]}`) {
const [cmd = 'list', sub = 'w9-desktop'] = process.argv.slice(2)
if (cmd === 'list') {
const { status, items } = await listSandboxes(sub)
console.log(
JSON.stringify(
{ status, items: items.map((s) => ({ id: s.id, state: s.state ?? s.status, name: s.name, createdAt: s.createdAt })) },
null,
1
)
)
} else if (cmd === 'cleanup') {
console.log(JSON.stringify(await deleteAllSandboxes(sub), null, 1))
}
}
+18
View File
@@ -0,0 +1,18 @@
{
"findings": [
"<b>W1-1 (bug, w1-env-builds, open 2026-11-09):</b> an environment build only advances while a client streams <code>GET …/builds/:id/events</code>. Polling <code>GET …/builds/:id</code> every 10 s (documented as also advancing the build) left the build in <code>running</code> with 0 log bytes for 25 min, and again for 4 min in a minimal repro. The same build finished in 15–19 s once the events stream was opened. API, CLI and MCP clients that poll, or a desktop that closes Settings mid-build, stall builds. The harness now follows the events stream the way the desktop does.",
"<b>D-1 (bug, core desktop, high):</b> starting an Orca Cloud create before signing in, then signing in from the card, created a sandbox owned by the fallback dev user (<code>desktop-dogfood</code>), not the signed-in user. Keep-alive, Copy SSH Command and Delete then got <code>not_found</code>. Likely cause: <code>getHostedSandboxConfig()</code> caches the token for the app process and does not reset it on sign-in (<code>src/main/hosted-sandbox/hosted-sandbox-config.ts</code>).",
"<b>D-2 (bug, core desktop, high):</b> Delete Workspace hides the row and reports success when the control plane answers <code>not_found</code>, while the VM keeps running (a billing leak).",
"<b>D-3 (bug, core desktop):</b> after a cloud create, the checkout at <code>/vercel/sandbox/project</code> is added as a new project named \"project\" instead of being grouped under orca-sandbox-push-test.",
"<b>D-4 (minor, desktop):</b> the \"Sign in to use Orca Cloud\" card stays after sign-in; the Ports panel says \"Port scan unavailable on unknown\" while waking; \"Move to this computer\" has no confirmation and copies rather than moves (the VM keeps running); the agent picker is disabled for Orca Cloud.",
"<b>W6-1 (resolved):</b> provider keys are now egress placeholders in orcad's env (swap on by default on dev), so <code>sec.precedence</code> reads <code>AWS_REGION</code>. W6 confirmed the swap resolves keys with the same precedence as injection (team &gt; repo &gt; personal for agent keys) and added a test at the egress proxy (cloud/w6-precedence-test). Real Claude Code and Codex PRs: nwparker/orca-sandbox-push-test#5, #6.",
"<b>Fixed since the last report:</b> F1 resume on a running sandbox now returns in 0.5 s (was a 60 s 502); F3 ai-gateway.vercel.sh is allowlisted (real Claude Code task passes); F4 the image now has orca, rg, tmux, vim, git-lfs and Chrome; F5 disk is 64 GB.",
"<b>Note:</b> kill-switch drain mode is not exercised on the shared dev Worker (it would suspend other workstreams' sandboxes); refusal of creates and wakes is verified."
],
"gaps": [
"Real Anthropic-model Claude run is pending an AI Gateway credit top-up (no anthropic/* models on free credits); agent.real-task uses moonshotai/kimi-k2 through Claude Code.",
"Payment-grace expiry (96 h) is not waited on: dunning verifies past_due, blocked starts, grace window, and pay leading to resume.",
"Team create/invite need the Orca Cloud auth endpoints on staging (team-billing-ux is standing them up).",
"The 15-min idle auto-sleep is not waited out in the routine run (config value checked); the mobile app UI is covered by W8's own runs (device-session API verified here)."
]
}
+24
View File
@@ -0,0 +1,24 @@
{
"name": "@orca-cloud/acceptance",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"accept": "tsx src/run.ts",
"report": "tsx src/report-cli.ts",
"sweep": "tsx src/sweep.ts",
"typecheck": "tsc --noEmit",
"test": "vitest run"
},
"dependencies": {
"jose": "^6.2.13",
"playwright-core": "^1.56.1",
"zod": "^4.6.7"
},
"devDependencies": {
"@types/node": "^24.19.2",
"tsx": "^4.23.15",
"typescript": "^5.9.3",
"vitest": "^5.0.5"
}
}
+125
View File
@@ -0,0 +1,125 @@
// Benchmarks (n >= 10 each) on real Vercel sandboxes through the dev control plane.
// create.api POST /v1/sandboxes (golden snapshot + repo clone + orcad ready)
// create.usable create.api + the user's client connects (orca CLI E2EE handshake, status ok)
// create.firstEcho create.usable + repo add + PTY open + typed command echoed
// wake.api POST resume on a slept sandbox (boot from snapshot + orcad ready)
// wake.firstEcho wake.api + reconnect + PTY open + typed command echoed ("wake -> first keystroke")
// suspend.api POST suspend (drain orcad + stop + snapshot)
// rollover.gap POST rollover (drain, stop, boot, orcad ready): the time agents are offline
import { config } from './env'
import type { Ctx } from './context'
import { openTerminal, orca, typeAndSee } from './orca-cli'
export const EARLIER: Record<string, string> = {
'create.api': 'Pro, 2026-11-09: 4.1-5.4 s (n=3 in-process), 5.1 s via dev Worker',
'create.usable': 'Desktop dogfood 2026-11-09: create -> ready in app 8-12 s',
'wake.api': 'Pro 2026-11-09: resume -> ready 6.0 s (n=1); Hobby 2 vCPU: 4.3-5.1 s',
'suspend.api': 'Pro 2026-11-09: drain + stop 7.6 s (n=1); Hobby stop 2.2 s',
'rollover.gap': 'Pro 2026-11-09: 12.2 s via dev Worker, 17.3 s at shortened cap',
'desktop.createToTerminal': 'Desktop dogfood 2026-11-09: 8-12 s'
}
export const CONDUCTOR: Record<string, string> = {
'create.api': '0.80: "1.5-3x faster sandbox start" (no absolute number published)',
'create.usable': 'not published',
'wake.firstEcho': 'not published; wake on open, files kept, processes lost',
'rollover.gap': 'no rollover: hard stop at 23h50m, manual wake + resend turn',
'suspend.api': 'not published (20 min idle sleep)',
'port.forward': '0.83 auto port forwarding; latency not published',
'share.coldLoad': '0.84.1 share links; latency not published',
'keystroke.echo': 'not published (terminal is a remote PTY too)',
'wake.firstKeystroke': 'not published; wake on open',
'desktop.createToTerminal': 'not published; 0.80 "1.5-3x faster start"'
}
async function firstEcho(code: string, root: string, kind: 'git' | 'folder') {
const t = await openTerminal(code, root, kind)
if (!t.handle) {
return null
}
const e = await typeAndSee(code, t.handle, 'echo BENCH_$((7*6))', 'BENCH_42')
return e.ok ? e : null
}
export async function benchCreate(ctx: Ctx, n: number): Promise<void> {
const api: number[] = []
const usable: number[] = []
const echo: number[] = []
const errors: string[] = []
for (let i = 0; i < n; i++) {
const t0 = performance.now()
const r = await ctx.sandboxes.create(ctx.solo, { workspaceId: `w9-bench-c${i}`, repo: { url: config.testRepo } })
if (r.status !== 200) {
errors.push(`${r.status}:${r.body?.error}:${r.body?.reason ?? ''}`)
continue
}
api.push(r.ms)
try {
const st = await orca(r.body.connection.pairingCode, ['status'])
if (st.ok) {
usable.push(Math.round(performance.now() - t0))
}
const e = await firstEcho(r.body.connection.pairingCode, r.body.connection.projectRoot, 'git')
if (e) {
echo.push(Math.round(performance.now() - t0))
}
} finally {
await ctx.sandboxes.destroy(r.body.sandbox.id)
}
console.log(`bench create ${i + 1}/${n}: api ${api.at(-1)} usable ${usable.at(-1)} echo ${echo.at(-1)}`)
}
ctx.run.bench('create.api', api, errors.length ? `errors: ${errors.join(' ')}` : undefined)
ctx.run.bench('create.usable', usable)
ctx.run.bench('create.firstEcho', echo)
}
export async function benchWakeAndRollover(ctx: Ctx, n: number): Promise<void> {
const r = await ctx.sandboxes.create(ctx.solo, { workspaceId: 'w9-bench-wake' })
if (r.status !== 200) {
ctx.run.note(`wake bench: create failed ${r.status}`)
return
}
const id = r.body.sandbox.id
const wakeApi: number[] = []
const wakeEcho: number[] = []
const suspend: number[] = []
const rollover: number[] = []
const errors: string[] = []
try {
for (let i = 0; i < n; i++) {
const s = await ctx.sandboxes.suspend(id)
if (s.status !== 200) {
errors.push(`suspend ${s.status}`)
continue
}
suspend.push(s.ms)
const t0 = performance.now()
const w = await ctx.sandboxes.resume(id)
if (w.status !== 200) {
errors.push(`resume ${w.status}:${(w.body as any)?.reason ?? ''}`)
continue
}
wakeApi.push(w.ms)
const e = await firstEcho(w.body.connection.pairingCode, w.body.connection.projectRoot, 'folder')
if (e) {
wakeEcho.push(Math.round(performance.now() - t0))
}
console.log(`bench wake ${i + 1}/${n}: suspend ${s.ms} wake ${w.ms} echo ${wakeEcho.at(-1)}`)
}
for (let i = 0; i < n; i++) {
const ro = await ctx.sandboxes.rollover(id)
if (ro.status !== 200) {
errors.push(`rollover ${ro.status}:${(ro.body as any)?.reason ?? ''}`)
continue
}
rollover.push(ro.ms)
console.log(`bench rollover ${i + 1}/${n}: ${ro.ms}`)
}
} finally {
await ctx.sandboxes.destroy(id)
}
const note = errors.length ? `errors: ${errors.join(' ')}` : undefined
ctx.run.bench('suspend.api', suspend, note)
ctx.run.bench('wake.api', wakeApi, note)
ctx.run.bench('wake.firstEcho', wakeEcho)
ctx.run.bench('rollover.gap', rollover, note)
}
+106
View File
@@ -0,0 +1,106 @@
// Every feature the eval covers: the rows of conductor-parity-matrix.html plus the accounts, team,
// Stripe and safety flows. `conductor` is what Conductor shipped (from the matrix), `owner` is the
// workstream that lands it, `better` is where Orca is designed to beat Conductor (verified by the check).
export type Feature = {
id: string
section: string
feature: string
conductor: string
owner: string
priority: 'P0' | 'P1' | 'P2' | 'ops'
better?: string
}
const S = {
acct: '0. Accounts, team and billing',
sub: '1. Substrate, size and limits',
env: '2. Environment builds',
sec: '3. Secrets, env and agent auth',
life: '4. Lifecycle: sleep, wake, cap, health',
acc: '5. Access: terminals, SSH, files, ports, preview',
mp: '6. Team / multiplayer',
api: '7. Programmability: API, MCP, CLI, routines',
cli: '8. Clients',
ops: '9. Safety, cost and operations'
}
export const SECTIONS = Object.values(S)
export const CATALOG: Feature[] = [
// 0. Accounts, team, billing
{ id: 'acct.signin', section: S.acct, feature: 'Sign in (dev-signed Orca token) reaches control plane and billing', conductor: 'Conductor account sign-in', owner: 'core', priority: 'P0' },
{ id: 'acct.isolation', section: S.acct, feature: 'Another account cannot see or act on my sandboxes', conductor: 'n/a (not documented)', owner: 'core', priority: 'P0' },
{ id: 'team.create', section: S.acct, feature: 'Create a team (org) and become its owner', conductor: 'Teams plan, invite-only then self-serve', owner: 'team-billing-ux', priority: 'P0' },
{ id: 'team.invite', section: S.acct, feature: 'Invite a member; seats follow membership', conductor: 'Teams $60/user/mo', owner: 'team-billing-ux', priority: 'P0' },
{ id: 'billing.checkout', section: S.acct, feature: 'Stripe TEST Checkout with 4242 card activates Team plan', conductor: 'Stripe billing portal, monthly only', owner: 'team-billing-ux', priority: 'P0', better: 'Annual plan option, in-app plans page' },
{ id: 'billing.checkout-3ds', section: S.acct, feature: 'Checkout with a 3DS-required test card (challenge completed)', conductor: 'not documented', owner: 'team-billing-ux', priority: 'P0' },
{ id: 'billing.seat-proration', section: S.acct, feature: 'Adding a seat prorates immediately; removing credits next invoice', conductor: 'not documented', owner: 'team-billing-ux', priority: 'P0' },
{ id: 'billing.overage', section: S.acct, feature: 'Overage opt-in at $0.75/h with a monthly spend cap that blocks starts', conductor: 'No metering ("no extra charge today")', owner: 'team-billing-ux', priority: 'P0', better: 'Metered from day one with a hard cap' },
{ id: 'billing.dunning', section: S.acct, feature: 'Payment failure -> grace -> suspend -> pay -> resume (test clocks)', conductor: 'not documented', owner: 'team-billing-ux', priority: 'P0' },
{ id: 'billing.entitlement-gate', section: S.acct, feature: 'Account without a plan is refused a sandbox (fail closed)', conductor: 'Pro/org required', owner: 'core', priority: 'P0' },
// 1. Substrate
{ id: 'sub.create', section: S.sub, feature: 'Hosted microVM per workspace, one call / one click', conductor: '0.78 GA', owner: 'core', priority: 'P0' },
{ id: 'sub.size', section: S.sub, feature: 'Machine sizes (standard 4 vCPU, large 8 vCPU)', conductor: 'Fixed 8 vCPU / 16 GB', owner: 'core', priority: 'P0', better: 'Choice of size' },
{ id: 'sub.disk', section: S.sub, feature: 'Disk >= 32 GB and low-disk alert', conductor: '64 GB (0.82), low-disk alerts', owner: 'w1-env-builds', priority: 'P1' },
{ id: 'sub.tools', section: S.sub, feature: 'Preinstalled tools (node, git, gh, rg, jq, python3, orca CLI, Claude, Codex)', conductor: 'AL2023, Node 24, git, gh, jq, rg, LFS, tmux, Vim, Chrome', owner: 'core', priority: 'P0' },
{ id: 'sub.region', section: S.sub, feature: 'Region iad1', conductor: 'us-east-1 only', owner: 'core', priority: 'P0' },
{ id: 'sub.github-clone', section: S.sub, feature: 'Private GitHub repo cloned at create via the Orca GitHub App', conductor: 'GitHub App, GitHub only', owner: 'core', priority: 'P0', better: 'Token never written to the VM disk (egress injection)' },
// 2. Env builds
{ id: 'env.build', section: S.env, feature: 'Org environment build (repos, env, install script) with logs', conductor: '0.78 Cloud Computer, build logs', owner: 'w1-env-builds', priority: 'P0' },
{ id: 'env.versions', section: S.env, feature: 'Versioned builds, history, activate older build (rollback)', conductor: '0.78 history + rollback', owner: 'w1-env-builds', priority: 'P1' },
{ id: 'env.pinned', section: S.env, feature: 'Existing workspaces keep their build after a rebuild', conductor: 'Yes', owner: 'w1-env-builds', priority: 'P0' },
{ id: 'env.from-build', section: S.env, feature: 'Create a cloud workspace from a build (warm repo snapshot)', conductor: 'Yes', owner: 'w1-env-builds', priority: 'P0', better: 'Warm per-repo snapshot' },
{ id: 'env.setup-script', section: S.env, feature: 'Per-repo setup script runs inside the VM on each new workspace', conductor: 'Cloud repo setup scripts', owner: 'w1-env-builds', priority: 'P0' },
// 3. Secrets & agents
{ id: 'sec.org-secret', section: S.sec, feature: 'Org secret: write-only (hidden after save), injected into the VM env', conductor: '0.78/0.80, values hidden, plain env in VM', owner: 'w6-agents-secrets', priority: 'P0', better: 'Envelope encrypted; optional egress placeholder swap' },
{ id: 'sec.precedence', section: S.sec, feature: 'Personal beats org for env; org beats personal for agent keys', conductor: 'Same precedence', owner: 'w6-agents-secrets', priority: 'P0' },
{ id: 'sec.no-rebuild', section: S.sec, feature: 'Changed secret picked up on next session (no rebuild)', conductor: '0.80/0.83', owner: 'w6-agents-secrets', priority: 'P0' },
{ id: 'sec.reserved', section: S.sec, feature: 'Reserved names refused (PATH, GH_TOKEN, ORCA_*, CLAUDE_CODE_OAUTH_TOKEN)', conductor: 'Reserved names', owner: 'core', priority: 'P0' },
{ id: 'sec.git-push', section: S.sec, feature: 'Real git push from the VM with an injected GitHub App token', conductor: 'GitHub App', owner: 'core', priority: 'P0', better: 'No token on disk; attacker-shaped requests 403' },
{ id: 'sec.egress-deny', section: S.sec, feature: 'Egress fails closed: unlisted hosts do not resolve; foreign tokens refused', conductor: 'not documented', owner: 'core', priority: 'P0', better: 'Default-deny egress' },
{ id: 'agent.real-task', section: S.sec, feature: 'Real agent task in the VM (Claude Code / Codex via AI Gateway key)', conductor: 'Claude Code, Codex, Cursor with API key or subscription', owner: 'w6-agents-secrets', priority: 'P0' },
{ id: 'agent.signin', section: S.sec, feature: 'Agent sign-in on the VM persists across sleep/rollover', conductor: 'Per-org agent connections', owner: 'w6-agents-secrets', priority: 'P0' },
// 4. Lifecycle
{ id: 'life.sleep-wake', section: S.life, feature: 'Sleep (stop + snapshot) and wake keep files and pairing', conductor: 'Files + chats survive sleep; processes do not', owner: 'core', priority: 'P0', better: 'Same pairing; no re-pair' },
{ id: 'life.idle-sleep', section: S.life, feature: 'Auto-sleep when idle (lease lapses / idle stop)', conductor: '20 min idle', owner: 'w2-lifecycle', priority: 'P0' },
{ id: 'life.rollover', section: S.life, feature: '24h cap rollover keeps files, pairing and host key', conductor: 'Hard stop at 23h50m, manual recovery', owner: 'core', priority: 'P0', better: 'Automatic rollover, no user action' },
{ id: 'life.auto-resume', section: S.life, feature: 'Agents auto-resume after rollover (durable offers)', conductor: 'User must resend interrupted turn', owner: 'w2-lifecycle', priority: 'P0', better: 'Agent continues by itself' },
{ id: 'life.lease', section: S.life, feature: 'Orca-owned lease renewal, clamped to the session deadline', conductor: 'n/a', owner: 'core', priority: 'P0' },
{ id: 'life.wake-on-message', section: S.life, feature: 'Wake on message and deliver it', conductor: '0.81/0.83', owner: 'w2-lifecycle', priority: 'P0' },
{ id: 'life.status', section: S.life, feature: 'Status panel: provisioning/sleeping/waking/ready/unresponsive + Restart', conductor: 'CPU, memory, Restart, Unresponsive', owner: 'w2-lifecycle', priority: 'P0' },
{ id: 'life.archive', section: S.life, feature: 'Archive (stopped, snapshot kept with TTL) and unarchive', conductor: 'API 0.79, docs', owner: 'w2-lifecycle', priority: 'P0' },
{ id: 'life.transfer-local', section: S.life, feature: 'Transfer a cloud workspace to local', conductor: '0.93', owner: 'w2-lifecycle', priority: 'P1' },
{ id: 'life.oom', section: S.life, feature: 'OOM explanation and restart offer', conductor: 'Changelog', owner: 'w2-lifecycle', priority: 'P1' },
// 5. Access
{ id: 'acc.terminal', section: S.acc, feature: 'Cloud terminal: open PTY, type, read echo (orca CLI over pairing)', conductor: 'Cloud terminal', owner: 'core', priority: 'P0' },
{ id: 'acc.ports', section: S.acc, feature: 'Listening-port detection with process name', conductor: '0.81/0.89', owner: 'w3-networking', priority: 'P0' },
{ id: 'acc.port-forward', section: S.acc, feature: 'Forward a VM port to localhost (same port number)', conductor: '0.80/0.82/0.83 auto-forward', owner: 'w3-networking', priority: 'P0' },
{ id: 'acc.share-link', section: S.acc, feature: 'Share link to a cloud app (public preview URL)', conductor: '0.84.1', owner: 'w3-networking', priority: 'P1' },
{ id: 'acc.ssh', section: S.acc, feature: 'SSH / open in VS Code or Cursor', conductor: '0.83/0.89', owner: 'w3-networking', priority: 'P1' },
{ id: 'acc.download', section: S.acc, feature: 'Download a file from the VM', conductor: '0.82', owner: 'w3-networking', priority: 'P1' },
{ id: 'acc.search', section: S.acc, feature: 'File-content search over the runtime', conductor: '0.85/0.87', owner: 'core', priority: 'P0' },
// 6. Multiplayer
{ id: 'mp.registry', section: S.mp, feature: 'Org workspaces visible to the team', conductor: '0.77 alpha, 0.78 GA', owner: 'w4-multiplayer', priority: 'P1' },
{ id: 'mp.teammate-open', section: S.mp, feature: "Team member opens a teammate's workspace", conductor: 'Yes', owner: 'w4-multiplayer', priority: 'P1' },
{ id: 'mp.private', section: S.mp, feature: 'Private workspaces hidden from the team', conductor: '0.85', owner: 'w4-multiplayer', priority: 'P1' },
{ id: 'mp.links', section: S.mp, feature: 'Workspace links resolve through the registry', conductor: '0.71/0.78', owner: 'w4-multiplayer', priority: 'P1' },
{ id: 'mp.cost', section: S.mp, feature: 'Cost per workspace in the list', conductor: '0.92', owner: 'w4-multiplayer', priority: 'P1' },
// 7. Programmability
{ id: 'api.rest', section: S.api, feature: 'REST API: create/status/sleep/archive workspaces with an API key', conductor: 'API v0 (beta)', owner: 'w5-api-routines', priority: 'P1' },
{ id: 'api.mcp', section: S.api, feature: 'Hosted MCP server', conductor: '0.82 (beta)', owner: 'w5-api-routines', priority: 'P1' },
{ id: 'api.cli', section: S.api, feature: 'Create a cloud workspace from the CLI', conductor: 'API create_session', owner: 'w5-api-routines', priority: 'P1' },
{ id: 'api.routine-cron', section: S.api, feature: 'Routine on a schedule runs on a fresh VM, then destroys it', conductor: '0.85/0.87', owner: 'w5-api-routines', priority: 'P1' },
{ id: 'api.routine-webhook', section: S.api, feature: 'Webhook / GitHub-triggered routine', conductor: '0.85/0.87, API webhook', owner: 'w5-api-routines', priority: 'P1' },
// 8. Clients
{ id: 'cli.mobile', section: S.cli, feature: 'Mobile reaches a cloud workspace (stable endpoint, wake from phone)', conductor: 'iOS app 0.90', owner: 'w8-mobile', priority: 'P0', better: 'iOS + Android, E2EE' },
{ id: 'cli.web', section: S.cli, feature: 'Web client reaches a cloud workspace', conductor: 'Mac only', owner: 'w8-mobile', priority: 'P1', better: 'Web, Windows, Linux too' },
{ id: 'cli.desktop', section: S.cli, feature: 'Desktop app: pick Orca Cloud, create, shell, sleep/wake (Playwright/Electron)', conductor: 'Mac desktop', owner: 'core', priority: 'P0' },
// 9. Ops
{ id: 'ops.kill-switch', section: S.ops, feature: 'Operator kill switch refuses creates/resumes', conductor: 'n/a', owner: 'core', priority: 'ops' },
{ id: 'ops.caps', section: S.ops, feature: 'Per-account concurrency cap and create rate limit', conductor: 'n/a', owner: 'core', priority: 'ops' },
{ id: 'ops.orphan-sweep', section: S.ops, feature: 'Orphan sweep / housekeeping runs and leaves no strays', conductor: 'n/a', owner: 'core', priority: 'ops' },
{ id: 'ops.cleanup', section: S.ops, feature: 'Destroy removes the sandbox and every snapshot (zero left after the run)', conductor: 'n/a', owner: 'core', priority: 'ops' },
{ id: 'ops.usage', section: S.ops, feature: 'Usage metered per session segment (billing ledger)', conductor: 'No metering', owner: 'core', priority: 'ops' }
]
export const featureById = new Map(CATALOG.map((f) => [f.id, f]))
@@ -0,0 +1,93 @@
// Real agent run in a cloud workspace: Claude Code (and Codex) talking to Vercel AI Gateway with the
// shared dev key, delivered the product way (personal secrets -> injected into orcad's env -> the
// terminal the agent runs in). One tiny task per run; model spend is read from the gateway.
import { control } from '../clients'
import { log, type Check } from '../context'
import { secrets } from '../env'
import { openTerminal, orca } from '../orca-cli'
const GATEWAY = 'https://ai-gateway.vercel.sh'
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
async function credits(key: string): Promise<number | null> {
const r = await fetch(`${GATEWAY}/v1/credits`, { headers: { authorization: `Bearer ${key}` } }).catch(() => null)
const body = (await r?.json().catch(() => null)) as { balance?: string | number } | null
return body?.balance !== undefined ? Number(body.balance) : null
}
export const agentChecks: Record<string, Check> = {
'agent.real-task': async (ctx) => {
const key = secrets.aiGatewayKey()
if (!key) {
return { status: 'skipped', detail: 'no AI Gateway key in the secrets dir' }
}
const who = { sub: `w9-agent-${ctx.run.file.runId}` }
const env: Record<string, string> = {
ANTHROPIC_BASE_URL: GATEWAY,
ANTHROPIC_AUTH_TOKEN: key,
ANTHROPIC_MODEL: 'moonshotai/kimi-k2',
ANTHROPIC_SMALL_FAST_MODEL: 'openai/gpt-5-nano'
}
for (const [name, value] of Object.entries(env)) {
const put = await control.as(who, 'PUT', `/v1/secrets/personal/${name}`, { value })
if (put.status !== 200) {
return { status: 'fail', detail: `secret ${name} -> ${put.status} ${JSON.stringify(put.body)}` }
}
}
const before = await credits(key)
const box = await ctx.sandboxes.create(who, { workspaceId: 'w9-agent' })
try {
if (box.status !== 200) {
return { status: 'fail', detail: `create ${box.status}` }
}
const sid = box.body.sandbox.session!.id
const root = box.body.connection.projectRoot
// Fail fast (no model spend) when the VM cannot reach the gateway.
const reach = await ctx.exec(sid, `curl -s -m 10 -o /dev/null -w '%{http_code}' ${GATEWAY}/v1/models; echo " rc=$?"`)
if (!/^(200|401|403)/.test(reach.stdout.trim())) {
return {
status: 'fail',
detail: `VM cannot reach ${GATEWAY} (${reach.stdout.trim()}): host is not on the sandbox egress allowlist, so gateway-backed agents cannot run`,
evidence: [log('reachability', reach.stdout)]
}
}
const t = await openTerminal(box.body.connection.pairingCode, root, 'folder')
if (!t.handle) {
return { status: 'fail', detail: `terminal ${t.term.stderr}` }
}
const prompt = 'Create a file named hello.txt in the current directory containing exactly the text orca-cloud-ok and nothing else. Do not do anything else.'
const started = performance.now()
await orca(box.body.connection.pairingCode, [
'terminal', 'send', '--terminal', t.handle, '--enter',
'--text', `cd ${root} && claude -p ${JSON.stringify(prompt)} --dangerously-skip-permissions > /tmp/agent.log 2>&1; echo AGENT_EXIT=$? >> /tmp/agent.log`
])
let out = ''
while (performance.now() - started < 240_000) {
await sleep(5000)
const r = await ctx.exec(sid, `cat /tmp/agent.log 2>/dev/null | tail -c 3000; echo; echo "FILE=$(cat ${root}/hello.txt 2>/dev/null)"`)
out = r.stdout
if (/AGENT_EXIT=/.test(out)) {
break
}
}
const ms = Math.round(performance.now() - started)
const after = await credits(key)
const spent = before !== null && after !== null ? (before - after).toFixed(4) : 'n/a'
ctx.run.note(`agent.real-task gateway spend $${spent}`)
const ok = /FILE=orca-cloud-ok/.test(out) && /AGENT_EXIT=0/.test(out)
return {
status: ok ? 'pass' : 'fail',
detail: `Claude Code (${env.ANTHROPIC_MODEL} via AI Gateway, key injected from the secret store) ${ok ? 'created hello.txt' : 'did not finish the task'} in ${ms} ms; gateway spend $${spent}. Real Anthropic model run pending a credit top-up.`,
timingsMs: { agentTask: ms },
evidence: [log('agent log + file', out.replaceAll(key, '<key>'))]
}
} finally {
for (const name of Object.keys(env)) {
await control.as(who, 'DELETE', `/v1/secrets/personal/${name}`)
}
if (box.status === 200) {
await ctx.sandboxes.destroy(box.body.sandbox.id)
}
}
}
}
+237
View File
@@ -0,0 +1,237 @@
// Billing checks against the dev billing Worker and Stripe TEST mode (real hosted Checkout in headless
// Chrome, real webhooks to the dev Worker). Stripe objects created here are canceled/deleted after.
import { execFileSync } from 'node:child_process'
import { join } from 'node:path'
import { billing } from '../clients'
import { secrets } from '../env'
import type { Check, Ctx } from '../context'
import { log } from '../context'
import { CARDS, completeCheckout, stripe, type CheckoutCard } from '../stripe'
import type { Identity } from '../tokens'
const HOUR = 3600
const TEAM_MONTHLY_PRICE = 'price_1UZmKpKb73TzRkjk33aiB2th' // wrangler.dev.jsonc STRIPE_PRICE_TEAM_MONTHLY
const BILLING_DIR = new URL('../../../billing/', import.meta.url).pathname
/** Test-clock customers are created by us, so link them to the account in the dev billing D1. */
function linkBillingAccount(accountId: string, customer: string): void {
if (!/^[A-Za-z0-9_:.-]+$/.test(accountId + customer)) {
throw new Error('unsafe id')
}
const now = Date.now()
execFileSync(
`${BILLING_DIR}node_modules/.bin/wrangler`,
['d1', 'execute', 'BILLING_DB', '-c', 'wrangler.dev.jsonc', '--remote', '--command',
`INSERT INTO billing_accounts (account_id, stripe_customer_id, created_at, updated_at) VALUES ('${accountId}', '${customer}', ${now}, ${now})`],
{ cwd: BILLING_DIR, stdio: 'ignore', env: { ...process.env, CI: '1', WRANGLER_SEND_METRICS: 'false' } }
)
}
async function advanceClock(clock: string, to: number): Promise<void> {
await stripe('POST', `/v1/test_helpers/test_clocks/${clock}/advance`, { frozen_time: Math.floor(to) })
await until(() => stripe('GET', `/v1/test_helpers/test_clocks/${clock}`), (c: any) => c.status === 'ready', 180_000)
}
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
async function until<T>(read: () => Promise<T>, done: (v: T) => boolean, timeoutMs = 90_000): Promise<T> {
const deadline = Date.now() + timeoutMs
let last = await read()
while (!done(last) && Date.now() < deadline) {
await sleep(2000)
last = await read()
}
return last
}
const orgFor = (ctx: Ctx, tag: string): Identity => {
const org = `w9-${ctx.run.file.runId.toLowerCase()}-${tag}`
return { sub: `w9-owner-${tag}`, org, orgRole: 'owner', email: `eval+${org}@example.com` }
}
const entitlement = async (who: Identity) => (await billing.as(who, 'GET', '/v1/billing/entitlements')).body
async function customerFor(who: Identity): Promise<string | null> {
const list = await stripe<{ data: Array<{ id: string; metadata: Record<string, string> }> }>('GET', '/v1/customers', { email: who.email, limit: 5 })
return list.data.find((c) => c.metadata.account_id === `org:${who.org}`)?.id ?? null
}
/** Cancels the account's subscriptions and deletes the test customer; records both in the ledger. */
async function teardown(ctx: Ctx, who: Identity): Promise<void> {
const customer = await customerFor(who).catch(() => null)
if (!customer) {
return
}
const subs = await stripe<{ data: Array<{ id: string; status: string }> }>('GET', '/v1/subscriptions', { customer, status: 'all' })
for (const sub of subs.data) {
if (!['canceled', 'incomplete_expired'].includes(sub.status)) {
await stripe('DELETE', `/v1/subscriptions/${sub.id}`).catch(() => {})
}
ctx.run.destroyed('stripe_subscription', sub.id)
}
await stripe('DELETE', `/v1/customers/${customer}`).catch(() => {})
ctx.run.destroyed('stripe_customer', customer)
}
async function checkoutWith(ctx: Ctx, tag: string, card: CheckoutCard, seats: number) {
const who = orgFor(ctx, tag)
const session = await billing.as(who, 'POST', '/v1/billing/checkout', { plan: 'team', seats, interval: 'month' })
if (session.status !== 200 || !/^https:\/\/checkout\.stripe\.com\//.test(session.body?.url ?? '')) {
return { who, error: `checkout ${session.status} ${JSON.stringify(session.body).slice(0, 300)}` }
}
const shot = join(ctx.run.artifactsDir, `checkout-${tag}.png`)
const done = await completeCheckout(session.body.url, card, shot)
const customer = await until(() => customerFor(who), (c) => c !== null, 30_000)
if (customer) {
ctx.run.created({ kind: 'stripe_customer', id: customer, label: `${card.label} checkout (${who.org})` })
}
const ent = await until(() => entitlement(who), (e) => e?.status === 'active')
return { who, done, ent, customer, shot }
}
let activeTeam: Promise<Awaited<ReturnType<typeof checkoutWith>>> | null = null
const team = (ctx: Ctx) => (activeTeam ??= checkoutWith(ctx, 'card', CARDS.visa, 2))
export const billingChecks: Record<string, Check> = {
'billing.checkout': async (ctx) => {
const r = await team(ctx)
if ('error' in r && r.error) {
return { status: 'fail', detail: r.error }
}
const e = r.ent
const ok = e?.status === 'active' && e.seats === 2 && e.includedSeconds === 120 * HOUR && e.canStartSandbox === true
return {
status: ok ? 'pass' : 'fail',
detail: `Checkout (2 seats, ${CARDS.visa.label}) completed in ${r.done!.ms} ms -> webhook -> ${e?.status}, ${e?.seats} seats, ${(e?.includedSeconds ?? 0) / HOUR} h included, canStart=${e?.canStartSandbox}`,
timingsMs: { checkout: r.done!.ms },
evidence: [
{ kind: 'screenshot', label: 'card form', path: r.shot!.replace('.png', '-form.png') },
{ kind: 'screenshot', label: 'return page', path: r.shot! },
log('entitlements', JSON.stringify(e).slice(0, 1500))
]
}
},
'billing.checkout-3ds': async (ctx) => {
const r = await checkoutWith(ctx, '3ds', CARDS.threeDS, 1)
try {
if ('error' in r && r.error) {
return { status: 'fail', detail: r.error }
}
const ok = r.ent?.status === 'active'
return {
status: ok ? 'pass' : 'fail',
detail: `3DS2 challenge completed in Checkout (${r.done!.ms} ms) -> ${r.ent?.status}`,
evidence: [
{ kind: 'screenshot', label: '3DS challenge', path: r.shot!.replace('.png', '-3ds.png') },
{ kind: 'screenshot', label: 'return page', path: r.shot! }
]
}
} finally {
await teardown(ctx, r.who)
}
},
'billing.overage': async (ctx) => {
const r = await team(ctx)
if ('error' in r && r.error) {
return { status: 'fail', detail: r.error }
}
const optIn = await billing.as(r.who, 'POST', '/v1/billing/overage', { enabled: true, spendCapCents: 5000 })
const member = await billing.as({ ...r.who, sub: 'w9-member', orgRole: 'member' }, 'POST', '/v1/billing/overage', { enabled: false })
const sub = (await stripe<{ data: Array<{ items: { data: Array<{ price: { id: string; recurring?: { usage_type?: string } } }> } }> }>('GET', '/v1/subscriptions', { customer: r.customer! })).data[0]
const metered = sub?.items.data.some((i) => i.price.recurring?.usage_type === 'metered') ?? false
const ent = await entitlement(r.who)
const ok = optIn.status === 200 && optIn.body?.overage?.active === true && optIn.body?.overage?.spendCapCents === 5000 && member.status === 403 && metered
return {
status: ok ? 'partial' : 'fail',
detail: `opt-in ${optIn.status} (active=${optIn.body?.overage?.active}, cap $${(optIn.body?.overage?.spendCapCents ?? 0) / 100}); metered item on subscription=${metered}; member toggle -> ${member.status}. Cap enforcement needs usage reports (CONTROL_PLANE_TOKEN), covered by billing e2e-test-mode.ts.`,
evidence: [log('overage', JSON.stringify(ent?.overage ?? optIn.body).slice(0, 800))]
}
},
'billing.seat-proration': async (ctx) => {
const token = secrets.membershipToken()
if (!token) {
return { status: 'skipped', detail: 'no membership sync token' }
}
const r = await team(ctx)
if ('error' in r && r.error) {
return { status: 'fail', detail: r.error }
}
const accountId = `org:${r.who.org}`
const push = (members: number) => billing.raw('POST', '/v1/internal/seats', token, { accountId, members })
const sub = async () => (await stripe('GET', '/v1/subscriptions', { customer: r.customer!, limit: 1 })).data[0]
const seatQty = async () => (await sub())?.items.data.find((i: any) => i.price.recurring?.usage_type !== 'metered')?.quantity
const added = await push(3)
const qtyAfterAdd = await seatQty()
const invoice = (await stripe('GET', '/v1/invoices', { subscription: (await sub()).id, limit: 1 })).data[0]
const removed = await push(2)
const qtyAfterRemove = await seatQty()
const ent = await entitlement(r.who)
const same = await push(2)
const ok =
added.body?.status === 'increased' && qtyAfterAdd === 3 && invoice?.billing_reason === 'subscription_update' && invoice?.status === 'paid' &&
removed.body?.status === 'deferred' && qtyAfterRemove === 3 && removed.body?.effectiveAt > Date.now() && same.body?.status === 'deferred'
return {
status: ok ? 'pass' : 'fail',
detail: `member added -> ${qtyAfterAdd} seats, proration invoice ${invoice?.billing_reason}/${invoice?.status} $${((invoice?.amount_paid ?? 0) / 100).toFixed(2)} charged now; member removed -> ${removed.body?.status} until ${removed.body?.effectiveAt ? new Date(removed.body.effectiveAt).toISOString().slice(0, 10) : '?'} (peak-seat billing: paid seat kept to period end, no mid-period refund abuse), still ${qtyAfterRemove} seats / entitlements ${ent?.seats}; repeat push -> ${same.body?.status}`,
evidence: [log('seat pushes', JSON.stringify({ added: added.body, removed: removed.body, same: same.body }))]
}
},
'billing.dunning': async (ctx) => {
const cp = secrets.controlPlaneToken()
if (!cp) {
return { status: 'skipped', detail: 'no control-plane token' }
}
const who = orgFor(ctx, 'dunning')
const accountId = `org:${who.org}`
const startCheck = async () =>
(await billing.raw('POST', '/v1/internal/entitlements/check', cp, { accountId, machineSize: 'standard', runningSandboxes: 0 })).body
const clock = await stripe('POST', '/v1/test_helpers/test_clocks', { frozen_time: Math.floor(Date.now() / 1000), name: `orca-acceptance-${ctx.run.file.runId}` })
ctx.run.created({ kind: 'stripe_test_clock', id: clock.id, label: 'dunning clock' })
const customer = await stripe('POST', '/v1/customers', { email: who.email, test_clock: clock.id, metadata: { account_id: accountId } })
ctx.run.created({ kind: 'stripe_customer', id: customer.id, label: 'dunning customer (test clock)' })
try {
linkBillingAccount(accountId, customer.id)
const visa = await stripe('POST', '/v1/payment_methods/pm_card_visa/attach', { customer: customer.id })
await stripe('POST', `/v1/customers/${customer.id}`, { invoice_settings: { default_payment_method: visa.id } })
const sub = await stripe('POST', '/v1/subscriptions', { customer: customer.id, items: [{ price: TEAM_MONTHLY_PRICE, quantity: 1 }], metadata: { account_id: accountId } })
const active = await until(() => entitlement(who), (e) => e?.status === 'active')
const failing = await stripe('POST', '/v1/payment_methods/pm_card_chargeCustomerFail/attach', { customer: customer.id })
await stripe('POST', `/v1/customers/${customer.id}`, { invoice_settings: { default_payment_method: failing.id } })
await advanceClock(clock.id, active.period.end / 1000 + HOUR)
const pastDue = await until(() => entitlement(who), (e) => e?.status === 'past_due', 150_000)
const blocked = await startCheck()
const graceH = pastDue?.payment ? (pastDue.payment.graceEndsAt - pastDue.payment.pastDueSince) / 3_600_000 : null
// Pay the open invoice with a good card -> active -> starts allowed again.
const good = await stripe('POST', '/v1/payment_methods/pm_card_visa/attach', { customer: customer.id })
await stripe('POST', `/v1/customers/${customer.id}`, { invoice_settings: { default_payment_method: good.id } })
const live = await stripe('GET', `/v1/subscriptions/${sub.id}`)
await stripe('POST', `/v1/invoices/${live.latest_invoice}/pay`, { payment_method: good.id })
const resumed = await until(startCheck, (c) => c?.allow === true, 120_000)
const ok = pastDue?.status === 'past_due' && blocked?.allow === false && resumed?.allow === true && resumed?.suspendRunning === false
return {
status: ok ? 'partial' : 'fail',
detail: `test clock renewal with a failing card -> ${pastDue?.status} (reason ${pastDue?.reason}); new starts ${blocked?.allow === false ? 'blocked' : 'ALLOWED'}; grace ${graceH} h before running sandboxes are suspended (expiry not waited on the dev Worker); paid -> starts ${resumed?.allow ? 'allowed' : 'still blocked'}, suspendRunning=${resumed?.suspendRunning}`,
evidence: [log('past_due entitlements', JSON.stringify(pastDue).slice(0, 1500))]
}
} finally {
await stripe('DELETE', `/v1/customers/${customer.id}`).catch(() => {})
ctx.run.destroyed('stripe_customer', customer.id)
await stripe('DELETE', `/v1/test_helpers/test_clocks/${clock.id}`).catch(() => {})
ctx.run.destroyed('stripe_test_clock', clock.id)
}
},
'team.create': async () => ({ status: 'not_landed', detail: 'Orgs live in Orca Cloud auth (login.onorca.dev); team endpoints merged but not deployed — waiting on team-billing-ux' }),
'team.invite': async () => ({ status: 'not_landed', detail: 'Invites live in Orca Cloud auth; not deployed — waiting on team-billing-ux' }),
/** Not a catalog row: tears down the shared Team checkout at the end of the billing checks. */
'billing.teardown': async (ctx) => {
if (activeTeam) {
const r = await activeTeam
await teardown(ctx, r.who)
}
return { status: 'pass', detail: 'test customers deleted' }
}
}
+219
View File
@@ -0,0 +1,219 @@
// Core platform checks against the deployed dev control plane and real Vercel sandboxes.
import { billing, control, vercelRest } from '../clients'
import { config } from '../env'
import { log, timing, type Check } from '../context'
import { openTerminal, orca, typeAndSee } from '../orca-cli'
/** Login-shell PATH as the user's terminal sees it (the golden image puts tools under /opt/orca). */
const LOGIN = (script: string) => `bash -lc '${script.replaceAll("'", `'"'"'`)}'`
export const coreChecks: Record<string, Check> = {
'acct.signin': async (ctx) => {
const list = await control.as(ctx.solo, 'GET', '/v1/sandboxes')
const bad = await control.raw('GET', '/v1/sandboxes', 'not-a-token')
const ent = await billing.as(ctx.owner, 'GET', '/v1/billing/entitlements')
const ok = list.status === 200 && bad.status === 401 && ent.status === 200
return {
status: ok ? 'pass' : 'fail',
detail: `control list ${list.status}, bad token ${bad.status}, billing entitlements ${ent.status} (account ${ent.body?.accountId})`,
evidence: [log('responses', JSON.stringify({ list: list.status, bad: bad.body, entitlements: { accountId: ent.body?.accountId, status: ent.body?.status } }))]
}
},
'acct.isolation': async (ctx) => {
const p = await ctx.primary()
const id = p.sandbox.id
const get = await control.as(ctx.outsider, 'GET', `/v1/sandboxes/${id}`)
const del = await control.as(ctx.outsider, 'DELETE', `/v1/sandboxes/${id}`)
const sus = await control.as(ctx.outsider, 'POST', `/v1/sandboxes/${id}/suspend`)
const list = await control.as(ctx.outsider, 'GET', '/v1/sandboxes')
const leaked = (list.body?.sandboxes ?? []).some((s: any) => s.id === id)
const ok = get.status === 404 && del.status === 404 && sus.status === 404 && !leaked
return { status: ok ? 'pass' : 'fail', detail: `outsider GET ${get.status}, DELETE ${del.status}, suspend ${sus.status}, in list: ${leaked}` }
},
'billing.entitlement-gate': async (ctx) => {
const ent = await billing.as({ sub: 'w9-noplan', org: `w9-noplan-${ctx.run.file.runId}`, orgRole: 'owner' }, 'GET', '/v1/billing/entitlements')
const refused = ent.body?.canStartSandbox === false
return {
status: refused ? 'partial' : 'fail',
detail: `no-plan org: canStartSandbox=${ent.body?.canStartSandbox} reason=${ent.body?.reason ?? ent.body?.startBlockedReason ?? '?'}; control-plane side not reachable with dev tokens (dev:* accounts bypass billing by design), covered by unit tests`,
evidence: [log('entitlements', JSON.stringify(ent.body).slice(0, 1500))]
}
},
'sub.create': async (ctx) => {
const p = await ctx.primary()
const ok = p.sandbox.status === 'running' && p.connection.type === 'orca-server' && p.connection.pairingCode.startsWith('orca://pair?')
return { status: ok ? 'pass' : 'fail', detail: `POST /v1/sandboxes -> ${p.sandbox.status} in ${p.createMs} ms (orcad ready, repo cloned)`, timingsMs: { createReady: p.createMs }, evidence: [timing('create -> orcad ready', p.createMs)] }
},
'sub.size': async (ctx) => {
const p = await ctx.primary()
const r = await ctx.exec(p.sandbox.session!.id, 'nproc; awk "/MemTotal/{print int(\\$2/1024/1024+0.5)}" /proc/meminfo')
const [cpus, memGb] = r.stdout.trim().split('\n').map(Number)
const large = await control.as(ctx.solo, 'POST', '/v1/sandboxes', { workspaceId: 'w9-xl', machineSize: 'xlarge' })
const ok = cpus === 4 && (memGb ?? 0) >= 7
return {
status: ok ? 'pass' : 'fail',
detail: `standard = ${cpus} vCPU / ${memGb} GB; xlarge refused with ${large.status} (${large.body?.error}). Large (8 vCPU) is exercised in the benchmark run.`,
evidence: [log('nproc/mem', r.stdout)]
}
},
'sub.disk': async (ctx) => {
const p = await ctx.primary()
const r = await ctx.exec(p.sandbox.session!.id, 'df -BG --output=size,avail /vercel/sandbox | tail -1')
const [size, avail] = r.stdout.trim().split(/\s+/).map((v) => Number.parseInt(v, 10))
return {
status: (size ?? 0) >= 60 ? 'pass' : (size ?? 0) >= 30 ? 'partial' : 'fail',
detail: `disk ${size} GB (${avail} GB free). Conductor: 64 GB. Low-disk banner is desktop UI (W1 #47), driven by status.metrics.diskUsedBytes.`,
evidence: [log('df', r.stdout)]
}
},
'sub.tools': async (ctx) => {
const p = await ctx.primary()
const tools = ['node', 'npm', 'pnpm', 'python3', 'git', 'gh', 'jq', 'rg', 'git-lfs', 'tmux', 'vim', 'claude', 'codex', 'orca', 'google-chrome']
const r = await ctx.exec(p.sandbox.session!.id, LOGIN(`for t in ${tools.join(' ')}; do command -v $t >/dev/null 2>&1 && echo "$t=yes" || echo "$t=no"; done; node --version; claude --version 2>/dev/null | head -1; codex --version 2>/dev/null | head -1`))
const have = new Set(r.stdout.split('\n').filter((l) => l.endsWith('=yes')).map((l) => l.slice(0, -4)))
const missing = tools.filter((t) => !have.has(t))
const required = ['node', 'git', 'gh', 'claude', 'codex']
const requiredMissing = required.filter((t) => !have.has(t))
return {
status: requiredMissing.length ? 'fail' : missing.length ? 'partial' : 'pass',
detail: `present: ${[...have].join(', ')}; missing: ${missing.join(', ') || 'none'}`,
evidence: [log('login shell tools', r.stdout)]
}
},
'sub.region': async (ctx) => {
const p = await ctx.primary()
const v = await vercelRest('GET', `/v2/sandboxes/orca-dev-${p.sandbox.id}`, { projectId: config.vercelProjectId })
const region = v.json?.sandbox?.region ?? v.json?.region ?? null
const view = await ctx.sandboxes.get(p.sandbox.id)
const ok = (view.body?.sandbox as any)?.region === 'iad1'
return { status: ok ? 'pass' : 'fail', detail: `control plane region ${(view.body?.sandbox as any)?.region}; Vercel reports ${region ?? 'n/a'}` }
},
'sub.github-clone': async (ctx) => {
const p = await ctx.primary()
const r = await ctx.exec(
p.sandbox.session!.id,
`cd ${p.connection.projectRoot} && git log --oneline -1 && git remote get-url origin && (grep -rIl "ghs_\\|ghp_\\|github_pat_" ~/.git-credentials ~/.config/gh .git/config 2>/dev/null | wc -l)`
)
const lines = r.stdout.trim().split('\n')
const tokensOnDisk = Number(lines.at(-1))
const ok = r.exitCode === 0 && /orca-sandbox-push-test/.test(r.stdout) && tokensOnDisk === 0
return { status: ok ? 'pass' : 'fail', detail: `private repo cloned (${lines[0]}); GitHub tokens on disk: ${tokensOnDisk}`, evidence: [log('git', r.stdout)] }
},
'acc.terminal': async (ctx) => {
const p = await ctx.primary()
const t = await openTerminal(p.connection.pairingCode, p.connection.projectRoot)
if (!t.handle) {
return { status: 'fail', detail: `terminal create failed: ${t.term.stderr || JSON.stringify(t.term.json?.error)}` }
}
const echo = await typeAndSee(p.connection.pairingCode, t.handle, 'echo EVAL_$((6*7)) $(whoami) $(command -v claude)', 'EVAL_42')
return {
status: echo.ok ? 'pass' : 'fail',
detail: `repo add ${t.repo.ms} ms, terminal create ${t.term.ms} ms, type->echo ${echo.ms} ms (via orca CLI; each CLI call reconnects E2EE)`,
timingsMs: { repoAdd: t.repo.ms, terminalCreate: t.term.ms, typeToEcho: echo.ms },
evidence: [log('terminal output tail', echo.output || echo.error)]
}
},
'sec.git-push': async (ctx) => {
const p = await ctx.primary()
const branch = `w9-eval/${ctx.run.file.runId}`
const r = await ctx.exec(
p.sandbox.session!.id,
LOGIN(
`cd ${p.connection.projectRoot} && git checkout -qb ${branch} && echo "eval ${ctx.run.file.runId}" > EVAL.md && git add EVAL.md && git -c user.email=eval@orca.dev -c user.name="Orca eval" commit -qm "eval ${ctx.run.file.runId}" && S=$(date +%s%3N) && git push -q origin HEAD 2>&1 | tail -2; echo "push_rc=\${PIPESTATUS[0]} push_ms=$(( $(date +%s%3N) - S ))"; git push -q origin --delete ${branch} 2>&1 | tail -1; echo "delete_rc=\${PIPESTATUS[0]}"`
),
120_000
)
const ok = /push_rc=0/.test(r.stdout) && /delete_rc=0/.test(r.stdout)
const pushMs = Number(/push_ms=(\d+)/.exec(r.stdout)?.[1] ?? NaN)
return { status: ok ? 'pass' : 'fail', detail: `push to ${branch}: ${ok ? 'ok' : 'failed'} in ${pushMs} ms, branch deleted after`, timingsMs: { push: pushMs }, evidence: [log('git push', r.stdout + r.stderr)] }
},
'sec.egress-deny': async (ctx) => {
const p = await ctx.primary()
const r = await ctx.exec(
p.sandbox.session!.id,
[
`echo "unlisted: $(curl -s -m 10 -o /dev/null -w '%{http_code}' https://example.com; echo " rc=$?")"`,
`echo "npm: $(curl -s -m 10 -o /dev/null -w '%{http_code}' https://registry.npmjs.org/)"`,
`echo "attacker-token: $(curl -s -m 15 -o /dev/null -w '%{http_code}' -H 'Authorization: Bearer ghp_attacker' https://api.github.com/rate_limit)"`,
`echo "ai-gateway: $(curl -s -m 10 -o /dev/null -w '%{http_code}' https://ai-gateway.vercel.sh/v1/models; echo " rc=$?")"`
].join('\n')
)
const unlistedBlocked = /unlisted: 000 rc=(6|7|28|35)/.test(r.stdout)
const npmOk = /npm: 200/.test(r.stdout)
const attackerRefused = /attacker-token: 403/.test(r.stdout)
const ok = unlistedBlocked && npmOk && attackerRefused
return { status: ok ? 'pass' : 'fail', detail: `unlisted blocked=${unlistedBlocked}, npm=${npmOk}, foreign GitHub token refused=${attackerRefused}`, evidence: [log('egress', r.stdout)] }
},
'life.lease': async (ctx) => {
const p = await ctx.primary()
const before = p.sandbox.session!.leaseExpiresAt
const r = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${p.sandbox.id}/lease`, { extendMs: 30 * 60_000 })
const after = r.body?.sandbox?.session?.leaseExpiresAt
const tooLong = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${p.sandbox.id}/lease`, { extendMs: 24 * 60 * 60_000 })
const ok = r.status === 200 && after >= before && after <= r.body.sandbox.session.deadlineAt && tooLong.status === 400
return { status: ok ? 'pass' : 'fail', detail: `lease ${r.status} (+${Math.round((after - before) / 1000)} s, <= deadline), oversize ${tooLong.status}`, timingsMs: { lease: r.ms } }
},
'life.sleep-wake': async (ctx) => {
const p = await ctx.primary()
const sid = p.sandbox.session!.id
await ctx.exec(sid, `echo keep-${ctx.run.file.runId} > ${p.connection.projectRoot}/.w9-keep; sync`)
const suspended = await ctx.sandboxes.suspend(p.sandbox.id)
const resumed = await ctx.sandboxes.resume(p.sandbox.id)
if (resumed.status !== 200) {
return { status: 'fail', detail: `suspend ${suspended.status}, resume ${resumed.status} ${JSON.stringify(resumed.body)}` }
}
ctx.updatePrimary(resumed.body)
const r = await ctx.exec(resumed.body.sandbox.session!.id, `cat ${p.connection.projectRoot}/.w9-keep`)
const samePairing = pairingKey(resumed.body.connection.pairingCode) === pairingKey(p.connection.pairingCode)
const st = await orca(resumed.body.connection.pairingCode, ['status'])
const ok = suspended.status === 200 && r.stdout.trim() === `keep-${ctx.run.file.runId}` && samePairing && st.ok
return {
status: ok ? 'pass' : 'fail',
detail: `suspend ${suspended.ms} ms (${suspended.body?.sandbox?.status}), resume->ready ${resumed.ms} ms, file kept=${r.stdout.trim() === `keep-${ctx.run.file.runId}`}, same host key=${samePairing}, CLI reconnect ${st.ms} ms`,
timingsMs: { suspend: suspended.ms, resume: resumed.ms, reconnect: st.ms }
}
},
'life.rollover': async (ctx) => {
const p = await ctx.primary()
await ctx.exec(p.sandbox.session!.id, `echo roll-${ctx.run.file.runId} > ${p.connection.projectRoot}/.w9-roll; sync`)
const rolled = await ctx.sandboxes.rollover(p.sandbox.id)
if (rolled.status !== 200) {
return { status: 'fail', detail: `rollover ${rolled.status} ${JSON.stringify(rolled.body)}` }
}
ctx.updatePrimary(rolled.body)
const r = await ctx.exec(rolled.body.sandbox.session!.id, `cat ${p.connection.projectRoot}/.w9-roll`)
const samePairing = pairingKey(rolled.body.connection.pairingCode) === pairingKey(p.connection.pairingCode)
const newSession = rolled.body.sandbox.session!.id !== p.sandbox.session!.id
const st = await orca(rolled.body.connection.pairingCode, ['status'])
const ok = newSession && samePairing && r.stdout.trim() === `roll-${ctx.run.file.runId}` && st.ok
return {
status: ok ? 'pass' : 'fail',
detail: `rollover gap ${rolled.ms} ms (drain, stop, boot, orcad ready), new session=${newSession}, same host key=${samePairing}, file kept, CLI reconnect ${st.ms} ms`,
timingsMs: { rollover: rolled.ms, reconnect: st.ms }
}
}
}
/** The host public key inside an orca://pair code: same key = no re-pair needed. */
export function pairingKey(code: string): string | null {
try {
const payload = new URL(code).searchParams.get('code')
return (JSON.parse(Buffer.from(payload!, 'base64url').toString()) as { publicKeyB64?: string }).publicKeyB64 ?? null
} catch {
return null
}
}
@@ -0,0 +1,198 @@
// W2 lifecycle + W3 networking checks on a dedicated workspace (status, OOM, wake, archive,
// share links, SSH keys, ports). Share-link cold load is also benchmarked here (n >= 10).
import { generateKeyPairSync } from 'node:crypto'
import { control } from '../clients'
import { config } from '../env'
import { log, type Check, type Ctx } from '../context'
import { orca, openTerminal } from '../orca-cli'
import { sandboxToken } from '../tokens'
const ARCHIVE = { 'x-orca-sandbox-features': 'archive' }
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
let box: Promise<{ id: string; sessionId: string; code: string; root: string }> | null = null
function featureBox(ctx: Ctx) {
box ??= (async () => {
const r = await ctx.sandboxes.create(ctx.solo, { workspaceId: `w9-life-${ctx.run.file.runId}`, repo: { url: config.testRepo } })
if (r.status !== 200) {
throw new Error(`feature box create ${r.status} ${JSON.stringify(r.body)}`)
}
return { id: r.body.sandbox.id, sessionId: r.body.sandbox.session!.id, code: r.body.connection.pairingCode, root: r.body.connection.projectRoot }
})()
return box
}
async function refresh(ctx: Ctx): Promise<{ id: string; sessionId: string; code: string; root: string }> {
const b = await featureBox(ctx)
const g = await ctx.sandboxes.get(b.id)
const next = { ...b, sessionId: g.body?.sandbox?.session?.id ?? b.sessionId }
box = Promise.resolve(next)
return next
}
const status = async (ctx: Ctx, id: string) => (await control.as(ctx.solo, 'GET', `/v1/sandboxes/${id}/status`)).body
/** Starts a tiny HTTP server on `port` inside the VM (survives the exec call). */
/** Starts a tiny HTTP server on `port` the way a user would: typed into a cloud terminal (orca CLI). */
async function serve(ctx: Ctx, b: { sessionId: string; code: string; root: string }, port: number) {
const up = async () => (await ctx.exec(b.sessionId, `curl -s -m 3 localhost:${port}/ || true`)).stdout.includes('orca-share-ok')
if (await up()) {
return
}
await ctx.exec(b.sessionId, 'mkdir -p /tmp/www && echo "orca-share-ok" > /tmp/www/index.html')
const t = await openTerminal(b.code, b.root)
if (t.handle) {
await orca(b.code, ['terminal', 'send', '--terminal', t.handle, '--enter', '--text', `cd ${b.root} && python3 -m http.server ${port} --directory /tmp/www`])
}
for (let i = 0; i < 10 && !(await up()); i++) {
await sleep(1000)
}
}
export const lifecycleChecks: Record<string, Check> = {
'life.status': async (ctx) => {
const b = await featureBox(ctx)
const s = await status(ctx, b.id)
const m = s?.status?.metrics
const ok = s?.status?.state === 'ready' && m?.orcadAlive === true && typeof m?.cpuPercent === 'number' && m?.memTotalBytes > 0 && s?.status?.usage?.centsPerHour > 0
return {
status: ok ? 'pass' : 'fail',
detail: `state ${s?.status?.state}; cpu ${m?.cpuPercent}% of ${m?.cpus}, mem ${Math.round((m?.memUsedBytes ?? 0) / 2 ** 20)}/${Math.round((m?.memTotalBytes ?? 0) / 2 ** 20)} MiB, disk ${Math.round((m?.diskUsedBytes ?? 0) / 2 ** 30)}/${Math.round((m?.diskTotalBytes ?? 0) / 2 ** 30)} GiB, orcadAlive ${m?.orcadAlive}; cost ${s?.status?.usage?.centsPerHour}¢/h. Restart covered by life.restart path in life.oom.`,
evidence: [log('status', JSON.stringify(s?.status).slice(0, 1500))]
}
},
'life.oom': async (ctx) => {
const b = await featureBox(ctx)
const before = (await status(ctx, b.id))?.status?.metrics?.oomKills ?? 0
// A process that allocates past RAM; the kernel OOM killer takes it.
await ctx.exec(b.sessionId, `python3 -c "x=[]\nwhile True: x.append(bytearray(256*1024*1024))" >/dev/null 2>&1; echo rc=$?`, 120_000).catch(() => null)
const after = await status(ctx, b.id)
const m = after?.status?.metrics
const restart = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/restart`)
if (restart.status === 200) {
box = Promise.resolve({ ...b, sessionId: restart.body.sandbox.session.id, code: restart.body.connection.pairingCode })
}
const ok = (m?.oomKills ?? 0) > before && restart.status === 200
return {
status: ok ? 'pass' : (m?.oomKills ?? 0) > before ? 'partial' : 'fail',
detail: `oomKills ${before} -> ${m?.oomKills} (victim ${m?.lastOomVictim ?? 'n/a'}), orcad survived=${m?.orcadAlive}; Restart -> ${restart.status} in ${restart.ms} ms. The banner + restart offer is desktop UI.`,
timingsMs: { restart: restart.ms }
}
},
'life.wake-on-message': async (ctx) => {
const b = await refresh(ctx)
const s = await ctx.sandboxes.suspend(b.id)
const t0 = performance.now()
const w = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/wake?wait=30s`)
const wakeMs = Math.round(performance.now() - t0)
const again = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/wake?wait=30s`)
await refresh(ctx)
const ok = s.status === 200 && w.body?.state === 'ready' && again.body?.state === 'ready' && again.ms < 3000
return {
status: ok ? 'partial' : 'fail',
detail: `wake (idempotent, single-flight) on a slept workspace -> ${w.body?.state} in ${wakeMs} ms; second wake -> ${again.body?.state} in ${again.ms} ms. Message delivery after wake is covered by api.rest messages + desktop held typing.`,
timingsMs: { wake: wakeMs, wakeAgain: again.ms }
}
},
'life.idle-sleep': async () => {
const r = await fetch(`${config.controlPlaneUrl}/v1/config`).then((x) => x.json()).catch(() => null)
return {
status: r?.idleSleepMinutes ? 'partial' : 'fail',
detail: `GET /v1/config -> idleSleepMinutes ${r?.idleSleepMinutes} (Conductor: 20). Idle stop is driven by the desktop scheduler + lease lapse; a ${r?.idleSleepMinutes}-min idle wait is not part of the routine run.`
}
},
'life.archive': async (ctx) => {
const b = await refresh(ctx)
await ctx.exec(b.sessionId, `echo arch > ${b.root}/.w9-arch; sync`)
const a = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/archive`, {})
const s = (await (await fetch(`${config.controlPlaneUrl}/v1/sandboxes/${b.id}/status`, { headers: { ...ARCHIVE, authorization: `Bearer ${await sandboxToken(ctx.solo)}` } })).json()) as any
const meta = await control.as(ctx.solo, 'GET', `/v1/sandboxes/${b.id}/archive`)
const u = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/unarchive`)
let kept = false
if (u.status === 200) {
box = Promise.resolve({ ...b, sessionId: u.body.sandbox.session.id, code: u.body.connection.pairingCode })
kept = (await ctx.exec(u.body.sandbox.session.id, `cat ${b.root}/.w9-arch`)).stdout.trim() === 'arch'
}
const ok = a.status === 200 && s?.status?.state === 'archived' && u.status === 200 && kept
return {
status: ok ? 'pass' : 'fail',
detail: `archive ${a.status} in ${a.ms} ms -> state ${s?.status?.state}, expires ${s?.status?.archive?.expiresAt ? new Date(s.status.archive.expiresAt).toISOString().slice(0, 10) : 'n/a'}; archive metadata ${meta.body?.archive ? 'captured' : 'none'}; unarchive ${u.status} in ${u.ms} ms, files kept=${kept}`,
timingsMs: { archive: a.ms, unarchive: u.ms }
}
},
'acc.ports': async (ctx) => {
const b = await refresh(ctx)
await serve(ctx, b, 4173)
let found: any = null
let last = ''
for (let i = 0; i < 3 && !found; i++) {
const r = await orca(b.code, ['ports', '--worktree', `path:${b.root}`], 30_000)
last = `${r.ms} ms ${r.stderr.slice(0, 200) || JSON.stringify(r.json?.error ?? r.json).slice(0, 200)}`
found = JSON.stringify(r.json ?? '').includes('4173') ? r.json : null
if (!found) {
await sleep(2000)
}
}
return {
status: found ? 'pass' : 'fail',
detail: found ? 'orca ports over the pairing sees port 4173 in the VM' : `port 4173 not listed by orca ports over the pairing code (last: ${last})`,
evidence: found ? [log('orca ports', JSON.stringify(found).slice(0, 1200))] : []
}
},
'acc.share-link': async (ctx) => {
const b = await refresh(ctx)
await serve(ctx, b, 4173)
const samples: number[] = []
const created: string[] = []
let firstBody = ''
for (let i = 0; i < 10; i++) {
const s = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/shares`, { port: 4173, audience: 'public', expiresIn: '1h', allowWake: true })
if (s.status !== 200 && s.status !== 201) {
return { status: 'fail', detail: `create share ${s.status} ${JSON.stringify(s.body)}` }
}
created.push(s.body.share.id)
const t0 = performance.now()
const page = await fetch(s.body.share.url, { redirect: 'follow' })
const text = await page.text()
samples.push(Math.round(performance.now() - t0))
firstBody ||= `${page.status} ${text.slice(0, 80)}`
}
ctx.run.bench('share.coldLoad', samples, 'first GET of a freshly created public share URL from this Mac (dev path-mode gateway)')
// Asleep: the gateway shows a wake page instead of an error.
await ctx.sandboxes.suspend(b.id)
const lastUrl = (await control.as(ctx.solo, 'GET', `/v1/sandboxes/${b.id}/shares`)).body?.shares?.[0]?.url
const asleep = lastUrl ? await fetch(lastUrl).then(async (r) => `${r.status} ${(await r.text()).includes('ake') ? 'wake page' : 'other'}`) : 'n/a'
for (const id of created) {
await control.as(ctx.solo, 'DELETE', `/v1/sandboxes/${b.id}/shares/${id}`)
}
const r = await ctx.sandboxes.resume(b.id)
if (r.status === 200) {
box = Promise.resolve({ ...b, sessionId: r.body.sandbox.session!.id, code: r.body.connection.pairingCode })
}
const ok = firstBody.startsWith('200') && firstBody.includes('orca-share-ok')
return {
status: ok ? 'pass' : 'fail',
detail: `public share -> ${firstBody.slice(0, 40)}; cold load p50 ${[...samples].sort((x, y) => x - y)[4]} ms (n=10); while asleep -> ${asleep}`,
evidence: [log('cold loads ms', samples.join(', '))]
}
},
'acc.ssh': async (ctx) => {
const b = await refresh(ctx)
const { publicKey } = generateKeyPairSync('ed25519')
const raw = Buffer.from(publicKey.export({ format: 'jwk' }).x!, 'base64url')
const blob = Buffer.concat([Buffer.from([0, 0, 0, 11]), Buffer.from('ssh-ed25519'), Buffer.from([0, 0, 0, 32]), raw]).toString('base64')
const r = await control.as(ctx.solo, 'POST', `/v1/sandboxes/${b.id}/ssh-keys`, { publicKey: `ssh-ed25519 ${blob} w9-acceptance` })
const portHex = (r.body?.ssh?.port ?? 2222).toString(16).toUpperCase().padStart(4, '0')
const listening = (await ctx.exec(b.sessionId, `cat /proc/net/tcp /proc/net/tcp6 2>/dev/null | awk '$4=="0A"' | grep -c ":${portHex} "`)).stdout.trim().split('\n')
const ok = r.status === 200 && Number(listening[0]) > 0 && Boolean(r.body?.ssh?.hostKey)
return {
status: ok ? 'pass' : 'fail',
detail: `ssh-keys ${r.status} -> port ${r.body?.ssh?.port}, user ${r.body?.ssh?.user}, host key ${r.body?.ssh?.hostKey ? 'returned' : 'missing'}; sshd listening on ${r.body?.ssh?.port}=${Number(listening[0]) > 0}. ssh round trip, orca ssh and Open in VS Code/Cursor verified in the desktop run (desktop-20261109-154725).`
}
}
}
+160
View File
@@ -0,0 +1,160 @@
// Safety and cost checks: kill switch, caps, orphan sweep, cleanup. The dev Worker is shared with
// other workstreams, so the kill switch is flipped without drain and only for a few seconds.
import { control, vercel, vercelRest } from '../clients'
import { config } from '../env'
import { log, type Check } from '../context'
export const opsChecks: Record<string, Check> = {
'ops.kill-switch': async (ctx) => {
let created = 0
let createWhileOff: number | null = null
let resumeWhileOff: number | null = null
const before = await control.operator('GET', '/v1/internal/status')
if (before.body?.killSwitch) {
return { status: 'skipped', detail: 'kill switch already on (someone else is using it); not touching it' }
}
// A slept sandbox: waking it is a start, which the kill switch must refuse.
const box = await ctx.sandboxes.create(ctx.solo, { workspaceId: 'w9-kill' })
if (box.status !== 200) {
return { status: 'fail', detail: `setup create ${box.status}` }
}
const boxId = box.body.sandbox.id
await ctx.sandboxes.suspend(boxId)
try {
const on = await control.operator('POST', '/v1/internal/kill-switch', { enabled: true, drain: false })
if (on.status !== 200) {
return { status: 'fail', detail: `kill switch on -> ${on.status}` }
}
const c = await control.as(ctx.solo, 'POST', '/v1/sandboxes', { workspaceId: 'w9-killed', machineSize: 'standard' })
createWhileOff = c.status
if (c.status === 200) {
created++
await control.as(ctx.solo, 'DELETE', `/v1/sandboxes/${c.body.sandbox.id}`)
}
resumeWhileOff = (await control.as(ctx.solo, 'POST', `/v1/sandboxes/${boxId}/resume`)).status
} finally {
await control.operator('POST', '/v1/internal/kill-switch', { enabled: false })
await ctx.sandboxes.destroy(boxId)
}
const after = await control.operator('GET', '/v1/internal/status')
const ok = createWhileOff === 503 && resumeWhileOff === 503 && after.body?.killSwitch === false && created === 0
return {
status: ok ? 'pass' : 'fail',
detail: `while on: create ${createWhileOff}, resume ${resumeWhileOff} (want 503 maintenance); off again: ${after.body?.killSwitch === false}. Drain mode not exercised on the shared dev Worker.`
}
},
'ops.caps': async (ctx) => {
// Rate limit: every create request consumes a token before the body is parsed, so invalid
// bodies probe the limiter without starting any VM. Own account so other checks are unaffected.
const who = { sub: `w9-rate-${ctx.run.file.runId}` }
const statuses: number[] = []
for (let i = 0; i < 12; i++) {
statuses.push((await control.as(who, 'POST', '/v1/sandboxes', { bogus: true })).status)
}
const limited = statuses.indexOf(429)
// Concurrency: dev accounts get 3 active per seat. Hold 3, the 4th must be refused.
const capWho = { sub: `w9-cap-${ctx.run.file.runId}` }
const held: string[] = []
let fourth: { status: number; reason?: string } = { status: 0 }
try {
for (let i = 0; i < 3; i++) {
const r = await ctx.sandboxes.create(capWho, { workspaceId: `w9-cap-${i}` })
if (r.status === 200) {
held.push(r.body.sandbox.id)
}
}
const r = await control.as(capWho, 'POST', '/v1/sandboxes', { workspaceId: 'w9-cap-3', machineSize: 'standard' })
fourth = { status: r.status, reason: r.body?.reason }
if (r.status === 200) {
ctx.sandboxes.adopt(r.body.sandbox.id, capWho)
ctx.run.created({ kind: 'sandbox', id: r.body.sandbox.id, label: 'w9-cap-3 (cap breach!)', vcpus: 4 })
held.push(r.body.sandbox.id)
}
} finally {
for (const id of held) {
await ctx.sandboxes.destroy(id)
}
}
const ok = limited === 10 && held.length === 3 && fourth.status === 403 && fourth.reason === 'concurrency_limit'
return {
status: ok ? 'pass' : 'fail',
detail: `rate limit: first 429 at request #${limited + 1} (limit 10/min); concurrency: held ${held.length}, 4th -> ${fourth.status} ${fourth.reason ?? ''}`,
evidence: [log('create statuses', statuses.join(','))]
}
},
'ops.orphan-sweep': async (ctx) => {
// A provider sandbox tagged as a dev Orca sandbox with no control-plane row is an orphan.
const name = `orca-dev-sbx_w9orphan${ctx.run.file.runId.replace(/[^a-z0-9]/g, '')}`
const api = vercel()
const created = await api.createSandbox({
name,
snapshotId: await goldenSnapshot(),
vcpus: 2,
region: 'iad1',
timeoutMs: 10 * 60_000,
ports: [6768],
networkPolicy: { allow: { 'registry.npmjs.org': [] } },
tags: { orcaEnv: 'dev', orcaSandbox: 'w9-orphan' },
snapshotExpirationMs: 24 * 60 * 60_000
})
ctx.run.created({ kind: 'sandbox', id: name, label: 'orphan-sweep probe (no D1 row)', vcpus: 2 })
try {
const h = await control.operator('POST', '/v1/internal/housekeeping')
const after = await api.getSandbox(name, { resume: false })
const stopped = after ? ['stopped', 'stopping', 'snapshotting'].includes(after.session.status) || after.sandbox.status !== 'running' : true
const ok = h.status === 200 && (h.body?.housekeeping?.orphanSandboxesSuspended ?? 0) >= 1 && stopped
return {
status: ok ? 'pass' : 'fail',
detail: `housekeeping ${h.status}: suspended ${h.body?.housekeeping?.orphanSandboxesSuspended}, deleted ${h.body?.housekeeping?.orphanSandboxesDeleted}; probe sandbox ${created.sandbox.status} -> ${after?.session.status ?? 'gone'} (deleted after the grace period; the eval deletes it now)`,
evidence: [log('housekeeping summary', JSON.stringify(h.body))]
}
} finally {
await api.deleteSandbox(name)
for (const snap of await api.listSnapshots(name)) {
if (snap.status === 'created') {
await api.deleteSnapshot(snap.id)
}
}
ctx.run.destroyed('sandbox', name)
}
},
'ops.cleanup': async (ctx) => {
// Runs last: everything this run created must be gone at Vercel (sandboxes and snapshots).
const { failed } = await ctx.sandboxes.destroyAll()
// API keys minted for the W5 checks: revoke (they would expire in a day anyway).
for (const r of ctx.run.file.resources.filter((x) => x.kind === 'other' && x.id.startsWith('key_') && !x.destroyedAt)) {
const d = await control.as(ctx.solo, 'DELETE', `/v1/api-keys/${r.id}`)
if (d.status < 300 || d.status === 404) {
ctx.run.destroyed('other', r.id)
}
}
const ours = ctx.run.file.resources.filter((r) => r.kind === 'sandbox').map((r) => (r.id.startsWith('orca-') ? r.id : `orca-dev-${r.id}`))
const listing = await vercelRest('GET', '/v2/sandboxes', { project: config.vercelProjectId, limit: '100' })
const live = (listing.json?.sandboxes ?? []).filter((s: any) => ours.includes(s.name)).map((s: any) => `${s.name}:${s.status}`)
let snapshotsLeft = 0
for (const name of ours) {
snapshotsLeft += (await vercel().listSnapshots(name)).filter((s) => s.status === 'created').length
}
const ok = failed.length === 0 && live.length === 0 && snapshotsLeft === 0
return {
status: ok ? 'pass' : 'fail',
detail: `${ours.length} sandboxes created by this run; still present at Vercel: ${live.length ? live.join(', ') : 'none'}; snapshots left: ${snapshotsLeft}; destroy failures: ${failed.length}`
}
}
}
/** Newest manual (golden-image) snapshot in the project; golden images self-rotate. */
async function goldenSnapshot(): Promise<string> {
if (process.env.ORCA_EVAL_GOLDEN_SNAPSHOT) {
return process.env.ORCA_EVAL_GOLDEN_SNAPSHOT
}
const { snapshots } = await vercel().listProjectSnapshots()
const golden = snapshots.filter((s) => s.status === 'created' && s.creationMethod === 'manual').sort((a, b) => b.createdAt - a.createdAt)[0]
if (!golden) {
throw new Error('no golden snapshot found')
}
return golden.id
}
@@ -0,0 +1,22 @@
// Features owned by workstreams that have not landed in the deployed dev stack yet. Each probe looks
// for the surface the feature needs; it flips to a real check when the workstream merges.
import { control } from '../clients'
import type { Check, Outcome } from '../context'
const waiting = (owner: string, what: string): Outcome => ({ status: 'not_landed', detail: `${what} — waiting on ${owner}` })
async function routeExists(path: string, method = 'GET'): Promise<number> {
const r = await control.as({ sub: 'w9-eval' }, method, path)
return r.status
}
export const pendingChecks: Record<string, Check> = {
'env.build': async () => {
const status = await routeExists('/v1/environments')
return status === 404 ? waiting('w1-env-builds', 'no /v1/environments route on the dev control plane') : { status: 'partial', detail: `GET /v1/environments -> ${status}; check not written yet` }
},
'api.rest': async () => {
const status = await routeExists('/v1/workspaces')
return status === 404 ? waiting('w5-api-routines', 'no /v1/workspaces (API key) route') : { status: 'partial', detail: `GET /v1/workspaces -> ${status}; check not written yet` }
}
}
@@ -0,0 +1,434 @@
// W1 environment builds, W4 multiplayer, W5 public API / MCP / routines, W8 device sessions.
import { createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, createDecipheriv } from 'node:crypto'
import { SignJWT } from 'jose'
import { control } from '../clients'
import { config, secrets } from '../env'
import { log, type Check, type Ctx } from '../context'
import { orca } from '../orca-cli'
import { sandboxToken, type Identity } from '../tokens'
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
const tag = (ctx: Ctx) => ctx.run.file.runId.replace(/[^0-9]/g, '').slice(-10)
// ---------------- W1 environment builds ----------------
type BuildState = { envId: string; first: any; second: any; error?: string }
let builds: Promise<BuildState> | null = null
/** Follows the build the way the desktop does (live NDJSON events); polls GET as a fallback. */
async function waitBuild(who: Identity, envId: string, buildId: string, timeoutMs = 25 * 60_000) {
const deadline = Date.now() + timeoutMs
let b: any = null
while (Date.now() < deadline) {
const ctl = new AbortController()
const timer = setTimeout(() => ctl.abort(), Math.min(5 * 60_000, deadline - Date.now()))
try {
const r = await fetch(`${config.controlPlaneUrl}/v1/environments/${envId}/builds/${buildId}/events`, {
headers: { authorization: `Bearer ${await sandboxToken(who)}` },
signal: ctl.signal
})
const dec = new TextDecoder()
let buf = ''
for await (const chunk of r.body as unknown as AsyncIterable<Uint8Array>) {
buf += dec.decode(chunk, { stream: true })
const lines = buf.split('\n')
buf = lines.pop() ?? ''
for (const line of lines.filter(Boolean)) {
const e = JSON.parse(line)
if (e.type === 'build') {
b = e.build
}
}
if (['succeeded', 'failed', 'canceled'].includes(b?.status)) {
return b
}
}
} catch {
// stream ended or timed out: re-check and reconnect
} finally {
clearTimeout(timer)
}
b = (await control.as(who, 'GET', `/v1/environments/${envId}/builds/${buildId}`)).body?.build ?? b
if (['succeeded', 'failed', 'canceled'].includes(b?.status)) {
return b
}
await sleep(2000)
}
return b
}
function environmentBuilds(ctx: Ctx): Promise<BuildState> {
builds ??= (async () => {
const who = ctx.owner
const env = await control.as(who, 'POST', '/v1/environments', {
name: `w9-acceptance-${tag(ctx)}`,
repos: [{ url: config.testRepo }],
installScript: 'echo "build-1 $(date -u +%s)" | sudo tee /opt/w9-build-marker >/dev/null',
machineSize: 'standard'
})
if (env.status !== 200 && env.status !== 201) {
return { envId: '', first: null, second: null, error: `create env ${env.status} ${JSON.stringify(env.body)}` }
}
const envId = env.body.environment.id
ctx.run.created({ kind: 'other', id: envId, label: 'environment (deleted after)' })
const b1 = await control.as(who, 'POST', `/v1/environments/${envId}/builds`)
const first = b1.body?.build ? await waitBuild(who, envId, b1.body.build.id) : null
if (first?.status !== 'succeeded') {
return { envId, first, second: null, error: `build 1 ${b1.status} -> ${first?.status} ${first?.error ?? JSON.stringify(b1.body)} (step ${first?.failedStep})` }
}
await control.as(who, 'PATCH', `/v1/environments/${envId}`, { installScript: 'echo "build-2" | sudo tee /opt/w9-build-marker >/dev/null' })
const b2 = await control.as(who, 'POST', `/v1/environments/${envId}/builds`)
const second = b2.body?.build ? await waitBuild(who, envId, b2.body.build.id) : null
return { envId, first, second }
})()
return builds
}
async function marker(ctx: Ctx, sessionId: string) {
return (await ctx.exec(sessionId, 'cat /opt/w9-build-marker 2>/dev/null || echo none')).stdout.trim()
}
// ---------------- W4 / W8 crypto helpers ----------------
function x25519Pair() {
const { publicKey, privateKey } = generateKeyPairSync('x25519')
return { privateKey, publicRaw: Buffer.from(publicKey.export({ format: 'jwk' }).x!, 'base64url') }
}
function openGrant(sealed: any, grantId: string, pair: ReturnType<typeof x25519Pair>): { pairingUrl: string } {
const eph = Buffer.from(sealed.ephemeralPublicKeyB64, 'base64')
const ephKey = createPublicKey({ key: { kty: 'OKP', crv: 'X25519', x: eph.toString('base64url') }, format: 'jwk' })
const shared = diffieHellman({ privateKey: pair.privateKey, publicKey: ephKey })
const key = Buffer.from(hkdfSync('sha256', shared, Buffer.concat([eph, pair.publicRaw]), Buffer.from('orca-cloud-grant-v1'), 32))
const ct = Buffer.from(sealed.ciphertextB64, 'base64')
const d = createDecipheriv('aes-256-gcm', key, Buffer.from(sealed.nonceB64, 'base64'))
d.setAAD(Buffer.from(`orca-cloud-grant-v1:${grantId}`))
d.setAuthTag(ct.subarray(ct.length - 16))
return JSON.parse(Buffer.concat([d.update(ct.subarray(0, ct.length - 16)), d.final()]).toString('utf8'))
}
let teamWs: Promise<{ shared: string; priv: string }> | null = null
function teamWorkspaces(ctx: Ctx) {
teamWs ??= (async () => {
const a = await ctx.sandboxes.create(ctx.owner, { workspaceId: `w9-team-shared-${tag(ctx)}`, title: 'W9 shared', visibility: 'team', useEnvironment: false })
const b = await ctx.sandboxes.create(ctx.owner, { workspaceId: `w9-team-private-${tag(ctx)}`, title: 'W9 private', visibility: 'private', useEnvironment: false })
if (a.status !== 200 || b.status !== 200) {
throw new Error(`team workspaces ${a.status}/${b.status} ${JSON.stringify(a.body).slice(0, 200)}`)
}
return { shared: a.body.sandbox.id, priv: b.body.sandbox.id }
})()
return teamWs
}
// ---------------- W5 API helpers ----------------
let apiKey: Promise<string> | null = null
function key(ctx: Ctx) {
apiKey ??= (async () => {
const r = await control.as(ctx.solo, 'POST', '/v1/api-keys', {
name: `w9-${tag(ctx)}`,
scopes: ['workspaces:read', 'workspaces:write', 'messages:write', 'projects:read', 'routines:read', 'routines:write'],
expiresInDays: 1
})
if (!r.body?.secret) {
throw new Error(`api key ${r.status} ${JSON.stringify(r.body)}`)
}
ctx.run.created({ kind: 'other', id: r.body.key?.id ?? 'api-key', label: 'API key (1 day, revoked after)' })
return r.body.secret as string
})()
return apiKey
}
const api = async (ctx: Ctx, method: string, path: string, body?: unknown) => control.raw(method, path, await key(ctx), body)
const routineWebhook = (ctx: Ctx) => platformChecks['api.routine-webhook-impl']!(ctx)
export const platformChecks: Record<string, Check> = {
'env.build': async (ctx) => {
const s = await environmentBuilds(ctx)
if (!s.first) {
return { status: 'fail', detail: s.error ?? 'no build' }
}
const ok = s.first.status === 'succeeded'
return {
status: ok ? 'pass' : 'fail',
detail: `org environment build 1 -> ${s.first.status} in ${Math.round((s.first.durationMs ?? 0) / 1000)} s (repo commit ${s.first.repos?.[0]?.commit?.slice(0, 7) ?? '?'})${s.error ? `; ${s.error}` : ''}`,
timingsMs: { build: s.first.durationMs ?? 0 }
}
},
'env.versions': async (ctx) => {
const s = await environmentBuilds(ctx)
if (!s.second) {
return { status: 'fail', detail: s.error ?? 'second build missing' }
}
const list = await control.as(ctx.owner, 'GET', `/v1/environments/${s.envId}/builds`)
const act = await control.as(ctx.owner, 'POST', `/v1/environments/${s.envId}/builds/${s.first.id}/activate`)
const env = (await control.as(ctx.owner, 'GET', `/v1/environments/${s.envId}`)).body
const ok = s.second.status === 'succeeded' && (list.body?.builds?.length ?? 0) >= 2 && act.status === 200 && env?.environment?.activeBuildId === s.first.id
return {
status: ok ? 'pass' : 'fail',
detail: `build 2 -> ${s.second.status} (${Math.round((s.second.durationMs ?? 0) / 1000)} s); history ${list.body?.builds?.length} builds; activate older build 1 (rollback) -> ${act.status}, active=${env?.environment?.activeBuildId === s.first.id ? 'build 1' : env?.environment?.activeBuildId}`
}
},
'env.from-build': async (ctx) => {
const s = await environmentBuilds(ctx)
if (!s.second) {
return { status: 'fail', detail: s.error ?? 'builds missing' }
}
const r = await ctx.sandboxes.create(ctx.owner, { workspaceId: `w9-env-${tag(ctx)}`, repo: { url: config.testRepo }, environmentBuildId: s.second.id })
if (r.status !== 200) {
return { status: 'fail', detail: `create from build ${r.status} ${JSON.stringify(r.body)}` }
}
const m = await marker(ctx, r.body.sandbox.session!.id)
await ctx.sandboxes.destroy(r.body.sandbox.id)
const ok = m === 'build-2' && r.body.sandbox.environmentBuildId === s.second.id
return { status: ok ? 'pass' : 'fail', detail: `create pinned to build 2 in ${r.ms} ms; install-script marker in VM: "${m}"`, timingsMs: { createFromBuild: r.ms } }
},
'env.pinned': async (ctx) => {
const s = await environmentBuilds(ctx)
if (!s.second) {
return { status: 'fail', detail: s.error ?? 'builds missing' }
}
// Active build is build 1 now (env.versions). A workspace on build 2 must keep build 2 after a wake.
const r = await ctx.sandboxes.create(ctx.owner, { workspaceId: `w9-pin-${tag(ctx)}`, repo: { url: config.testRepo }, environmentBuildId: s.second.id })
if (r.status !== 200) {
return { status: 'fail', detail: `create ${r.status}` }
}
await ctx.sandboxes.suspend(r.body.sandbox.id)
const w = await ctx.sandboxes.resume(r.body.sandbox.id)
const m = w.status === 200 ? await marker(ctx, w.body.sandbox.session!.id) : 'resume failed'
const def = await ctx.sandboxes.create(ctx.owner, { workspaceId: `w9-default-${tag(ctx)}`, repo: { url: config.testRepo } })
const dm = def.status === 200 ? await marker(ctx, def.body.sandbox.session!.id) : `create ${def.status}`
await ctx.sandboxes.destroy(r.body.sandbox.id)
if (def.status === 200) {
await ctx.sandboxes.destroy(def.body.sandbox.id)
}
const ok = m === 'build-2' && dm.startsWith('build-1')
return { status: ok ? 'pass' : 'fail', detail: `workspace pinned to build 2 still on "${m}" after wake; new default workspace uses active build: "${dm}"` }
},
'env.setup-script': async (ctx) => {
const s = await environmentBuilds(ctx)
const ok = s.first?.status === 'succeeded'
// Teardown of the environment rides on the last env check.
if (s.envId) {
const d = await control.as(ctx.owner, 'DELETE', `/v1/environments/${s.envId}`)
if (d.status === 200) {
ctx.run.destroyed('other', s.envId)
}
}
return { status: ok ? 'partial' : 'fail', detail: 'install script ran during the build (marker in env.from-build); per-repo orca.yaml setup on each new workspace runs through the desktop hook runtime (Electron run)' }
},
// ---------------- W4 ----------------
'mp.registry': async (ctx) => {
const ws = await teamWorkspaces(ctx)
const list = await control.as(ctx.member, 'GET', '/v1/team/workspaces?scope=team')
const views = list.body?.workspaces ?? []
const shared = views.find((w: any) => w.id === ws.shared || w.sandboxId === ws.shared)
const ok = list.status === 200 && shared?.access === 'can_open' && shared?.title === 'W9 shared'
return { status: ok ? 'pass' : 'fail', detail: `member sees ${views.length} team workspace(s); shared one access=${shared?.access}, owner ${shared?.ownerUserId}`, evidence: [log('team list', JSON.stringify(views).slice(0, 1500))] }
},
'mp.private': async (ctx) => {
const ws = await teamWorkspaces(ctx)
const list = await control.as(ctx.member, 'GET', '/v1/team/workspaces?scope=team')
const sees = JSON.stringify(list.body).includes(ws.priv)
const direct = await control.as(ctx.member, 'GET', `/v1/team/workspaces/${ws.priv}`)
const grant = await control.as(ctx.member, 'POST', `/v1/team/workspaces/${ws.priv}/grants`, { recipientPublicKeyB64: x25519Pair().publicRaw.toString('base64'), deviceLabel: 'w9', deviceKind: 'desktop' })
const ok = !sees && direct.status === 404 && grant.status === 404
return { status: ok ? 'pass' : 'fail', detail: `private workspace in member list=${sees}; direct GET ${direct.status}; grant ${grant.status}` }
},
'mp.teammate-open': async (ctx) => {
const ws = await teamWorkspaces(ctx)
const pair = x25519Pair()
const g = await control.as(ctx.member, 'POST', `/v1/team/workspaces/${ws.shared}/grants`, { recipientPublicKeyB64: pair.publicRaw.toString('base64'), deviceLabel: 'w9-acceptance', deviceKind: 'desktop' })
if (g.status !== 200 && g.status !== 201) {
return { status: 'fail', detail: `grant ${g.status} ${JSON.stringify(g.body)}` }
}
const opened = openGrant(g.body.sealed, g.body.grant.id, pair)
const st = await orca(opened.pairingUrl, ['status'])
const presence = await control.as(ctx.member, 'POST', `/v1/team/workspaces/${ws.shared}/presence`, { clientId: 'w9-member-desktop', state: 'viewing' })
const ownerView = await control.as(ctx.owner, 'GET', `/v1/team/workspaces/${ws.shared}`)
const revoke = await control.as(ctx.member, 'DELETE', `/v1/team/workspaces/${ws.shared}/grants/${g.body.grant.id}`)
const ok = st.ok && st.json?.result?.runtime?.state === 'ready'
return {
status: ok ? 'pass' : 'fail',
detail: `member grant ${g.status} in ${g.ms} ms; sealed offer opened locally (X25519+HKDF+AES-GCM) -> orca CLI connected to the teammate's VM: ${st.json?.result?.runtime?.state ?? st.stderr.slice(0, 120)}; presence ${presence.status}, owner sees ${JSON.stringify(ownerView.body?.workspace?.presence ?? []).slice(0, 120)}; revoke ${revoke.status}`,
timingsMs: { grant: g.ms, connect: st.ms }
}
},
'mp.links': async (ctx) => {
const ws = await teamWorkspaces(ctx)
const r = await control.as(ctx.member, 'GET', `/v1/team/links/resolve?w=${ws.shared}`)
const ok = r.status === 200 && JSON.stringify(r.body).includes(ws.shared)
return { status: ok ? 'pass' : 'fail', detail: `links/resolve as teammate -> ${r.status}` }
},
'mp.cost': async (ctx) => {
const ws = await teamWorkspaces(ctx)
await sleep(2000)
const list = await control.as(ctx.owner, 'GET', '/v1/team/workspaces?scope=mine')
const w = (list.body?.workspaces ?? []).find((x: any) => x.id === ws.shared || x.sandboxId === ws.shared)
const ok = typeof w?.cost?.computeMs === 'number' && typeof w?.cost?.totalUsdMicros === 'number'
const dShared = await ctx.sandboxes.destroy(ws.shared)
const dPriv = await ctx.sandboxes.destroy(ws.priv)
void dShared
void dPriv
teamWs = null
return { status: ok ? 'pass' : 'fail', detail: `cost in list: compute ${w?.cost?.computeMs} ms, $${((w?.cost?.totalUsdMicros ?? 0) / 1e6).toFixed(4)}` }
},
// ---------------- W5 ----------------
'api.rest': async (ctx) => {
const created = await api(ctx, 'POST', '/v1/workspaces', { name: `w9-api-${tag(ctx)}`, repo: { url: config.testRepo } })
const id = created.body?.workspace?.id ?? created.body?.id
if (!id) {
return { status: 'fail', detail: `POST /v1/workspaces ${created.status} ${JSON.stringify(created.body).slice(0, 300)}` }
}
ctx.run.created({ kind: 'sandbox', id, label: 'public API workspace', vcpus: 4 })
try {
const get = await api(ctx, 'GET', `/v1/workspaces/${id}`)
const slept = await api(ctx, 'POST', `/v1/workspaces/${id}/sleep`)
const woke = await api(ctx, 'POST', `/v1/workspaces/${id}/wake`)
const arch = await api(ctx, 'POST', `/v1/workspaces/${id}/archive`)
const noKey = await control.raw('GET', '/v1/workspaces', 'orca_sk_' + 'x'.repeat(32))
const ok = [200, 201, 202].includes(created.status) && get.status === 200 && slept.status < 300 && woke.status < 300 && arch.status < 300 && noKey.status === 401
return {
status: ok ? 'pass' : 'fail',
detail: `API key: create ${created.status} (${created.ms} ms), get ${get.status} (${get.body?.workspace?.state ?? get.body?.state}), sleep ${slept.status} (${slept.ms} ms), wake ${woke.status} (${woke.ms} ms), archive ${arch.status}; bad key ${noKey.status}`,
timingsMs: { create: created.ms, sleep: slept.ms, wake: woke.ms }
}
} finally {
const d = await api(ctx, 'DELETE', `/v1/workspaces/${id}`)
if (d.status < 300 || d.status === 404) {
ctx.run.destroyed('sandbox', id)
}
}
},
'api.mcp': async (ctx) => {
const rpc = (method: string, params: unknown, id: number) => api(ctx, 'POST', '/mcp', { jsonrpc: '2.0', id, method, params })
const init = await rpc('initialize', { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'w9-acceptance', version: '1' } }, 1)
const tools = await rpc('tools/list', {}, 2)
const names = (tools.body?.result?.tools ?? []).map((t: any) => t.name)
const call = await rpc('tools/call', { name: 'list_workspaces', arguments: {} }, 3)
const ok = init.status === 200 && names.includes('create_workspace') && call.status === 200 && !call.body?.error
return { status: ok ? 'pass' : 'fail', detail: `initialize ${init.status}; ${names.length} tools (${names.slice(0, 6).join(', ')}…); list_workspaces ${call.status}${call.body?.error ? ` ${JSON.stringify(call.body.error)}` : ''}` }
},
'api.cli': async () => ({
status: 'partial',
detail: 'CLI has `orca cloud env …` (builds) and `orca ssh`; creating a cloud workspace is via REST/MCP, not a CLI verb yet'
}),
'api.routine-webhook': async (ctx) => {
// Routine runs act with the creator's credentials: give the account the gateway key the product way.
const gw = secrets.aiGatewayKey()
const agentEnv: Record<string, string> = gw
? { ANTHROPIC_BASE_URL: 'https://ai-gateway.vercel.sh', ANTHROPIC_AUTH_TOKEN: gw, ANTHROPIC_MODEL: 'moonshotai/kimi-k2', ANTHROPIC_SMALL_FAST_MODEL: 'openai/gpt-5-nano' }
: {}
for (const [name, value] of Object.entries(agentEnv)) {
await control.as(ctx.solo, 'PUT', `/v1/secrets/personal/${name}`, { value })
}
try {
return await routineWebhook(ctx)
} finally {
for (const name of Object.keys(agentEnv)) {
await control.as(ctx.solo, 'DELETE', `/v1/secrets/personal/${name}`)
}
}
},
'api.routine-webhook-impl': async (ctx) => {
const r = await api(ctx, 'POST', '/v1/routines', {
name: `w9-hook-${tag(ctx)}`,
repo: { url: config.testRepo },
trigger: { kind: 'webhook' },
agent: 'claude',
prompt: 'Reply with the single word ok. Do not change any files.',
postResults: 'none',
maxRuntimeMinutes: 5
})
if (!r.body?.webhookUrl) {
return { status: 'fail', detail: `create routine ${r.status} ${JSON.stringify(r.body).slice(0, 300)}` }
}
const routineId = r.body.routine.id
try {
const hook = await fetch(r.body.webhookUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{"from":"w9"}' })
const hookBody = (await hook.json().catch(() => ({}))) as any
const runId = hookBody?.run?.id ?? hookBody?.runId
let run: any = null
const deadline = Date.now() + 8 * 60_000
while (runId && Date.now() < deadline) {
run = (await api(ctx, 'GET', `/v1/routines/${routineId}/runs/${runId}`)).body?.run
if (['succeeded', 'failed', 'cancelled', 'timed_out'].includes(run?.status)) {
break
}
await sleep(10_000)
}
const left = run?.workspaceId ? await control.as(ctx.solo, 'GET', `/v1/sandboxes/${run.workspaceId}`) : null
const destroyed = !left || left.status === 404 || left.body?.sandbox?.status === 'destroyed'
const ok = hook.status < 300 && ['succeeded', 'failed', 'timed_out'].includes(run?.status) && destroyed
return {
status: ok ? (run?.status === 'succeeded' ? 'pass' : 'partial') : 'fail',
detail: `webhook ${hook.status} -> run ${run?.status ?? 'none'} on a fresh VM (${run?.result?.summary?.slice(0, 100) ?? run?.error ?? ''}); VM destroyed after: ${destroyed}`
}
} finally {
await api(ctx, 'DELETE', `/v1/routines/${routineId}`)
}
},
'api.routine-cron': async (ctx) => {
const r = await api(ctx, 'POST', '/v1/routines', {
name: `w9-cron-${tag(ctx)}`,
repo: { url: config.testRepo },
trigger: { kind: 'schedule', cron: '0 9 * * 1-5', timezone: 'America/Los_Angeles' },
agent: 'claude',
prompt: 'Reply ok.',
postResults: 'none',
enabled: false,
maxRuntimeMinutes: 5
})
const id = r.body?.routine?.id
const gh = await api(ctx, 'POST', '/v1/routines', {
name: `w9-gh-${tag(ctx)}`,
repo: { url: config.testRepo },
trigger: { kind: 'github', events: ['pull_request.opened'], labels: ['orca'] },
agent: 'codex',
prompt: 'Review.',
postResults: 'comment',
enabled: false,
maxRuntimeMinutes: 5
})
for (const rid of [id, gh.body?.routine?.id].filter(Boolean)) {
await api(ctx, 'DELETE', `/v1/routines/${rid}`)
}
const ok = (r.status === 201 || r.status === 200) && (gh.status === 201 || gh.status === 200)
return { status: ok ? 'partial' : 'fail', detail: `weekday schedule routine ${r.status} (next ${r.body?.routine?.nextRunAt ? new Date(r.body.routine.nextRunAt).toISOString() : 'n/a'}); GitHub label-filtered routine ${gh.status}. Firing path verified by api.routine-webhook (same runner).` }
},
// ---------------- W8 ----------------
'cli.mobile': async (ctx) => {
const { publicKey, privateKey } = generateKeyPairSync('ed25519')
const pub = Buffer.from(publicKey.export({ format: 'jwk' }).x!, 'base64url').toString('base64')
const enroll = await control.as(ctx.solo, 'POST', '/v1/device-sessions', { label: 'w9 phone', platform: 'ios', publicKeyB64: pub })
const ds = enroll.body?.deviceSession?.id
if (!ds) {
return { status: 'fail', detail: `enroll ${enroll.status} ${JSON.stringify(enroll.body)}` }
}
const token = await new SignJWT({}).setProtectedHeader({ alg: 'EdDSA', kid: ds }).setIssuer('orca-device').setSubject(ds).setAudience('orca-sandbox').setIssuedAt().setExpirationTime('4m').sign(createPrivateKey(privateKey.export({ format: 'pem', type: 'pkcs8' })))
const p = (await ctx.primary()).sandbox.id
const list = await control.raw('GET', '/v1/sandboxes', token)
const wake = await control.raw('POST', `/v1/sandboxes/${p}/wake?wait=30s`, token)
const secretsBlocked = await control.raw('GET', '/v1/secrets?scope=personal', token)
const del = await control.as(ctx.solo, 'DELETE', `/v1/device-sessions/${ds}`)
const after = await control.raw('GET', '/v1/sandboxes', token)
const ok = list.status === 200 && wake.body?.state === 'ready' && secretsBlocked.status === 403 && del.status === 200 && after.status === 401
return {
status: ok ? 'pass' : 'fail',
detail: `phone device session ${enroll.status}; as the phone: list ${list.status} (${list.body?.sandboxes?.length} ws), wake -> ${wake.body?.state} (${wake.ms} ms), secrets ${secretsBlocked.status}; revoke ${del.status} -> token ${after.status}. Mobile app UI checked by W8's own runs.`
}
}
}
@@ -0,0 +1,98 @@
// Secret store checks on the team account: write-only values, scope precedence, injection at session
// start (no rebuild). Only synthetic values; real keys never pass through these checks.
import { control } from '../clients'
import { log, type Check, type Ctx } from '../context'
const put = (ctx: Ctx, who: Ctx['owner'], scope: string, name: string, value: string) =>
control.as(who, 'PUT', `/v1/secrets/${scope}/${name}`, { value })
async function orcadEnv(ctx: Ctx, sessionId: string, names: string[]): Promise<Record<string, string | null>> {
const r = await ctx.exec(sessionId, `tr '\\0' '\\n' < /proc/$(cat ~/.orca-sandbox/orcad.pid)/environ`)
const env = Object.fromEntries(
r.stdout
.split('\n')
.filter((l) => l.includes('='))
.map((l) => [l.slice(0, l.indexOf('=')), l.slice(l.indexOf('=') + 1)])
)
return Object.fromEntries(names.map((n) => [n, env[n] ?? null]))
}
let teamBox: Promise<{ id: string; sessionId: string }> | null = null
function teamSandbox(ctx: Ctx) {
teamBox ??= (async () => {
const tag = ctx.run.file.runId
await put(ctx, ctx.owner, 'org', 'W9_SHARED', `org-${tag}`)
await put(ctx, ctx.owner, 'org', 'W9_OVERRIDE', `org-${tag}`)
await put(ctx, ctx.owner, 'org', 'AWS_REGION', `org-fake-${tag}`)
await put(ctx, ctx.member, 'personal', 'W9_OVERRIDE', `personal-${tag}`)
await put(ctx, ctx.member, 'personal', 'AWS_REGION', `personal-fake-${tag}`)
const r = await ctx.sandboxes.create(ctx.member, { workspaceId: `w9-team-${tag}` })
if (r.status !== 200) {
throw new Error(`team sandbox create ${r.status} ${JSON.stringify(r.body)}`)
}
return { id: r.body.sandbox.id, sessionId: r.body.sandbox.session!.id }
})()
return teamBox
}
export const secretChecks: Record<string, Check> = {
'sec.reserved': async (ctx) => {
const names = ['PATH', 'GH_TOKEN', 'ORCA_ANYTHING', 'CLAUDE_CODE_OAUTH_TOKEN', 'LD_PRELOAD']
const statuses = await Promise.all(names.map(async (n) => [n, (await put(ctx, ctx.solo, 'personal', n, 'x')).status] as const))
const ok = statuses.every(([, s]) => s === 400)
return { status: ok ? 'pass' : 'fail', detail: statuses.map(([n, s]) => `${n}=${s}`).join(', ') }
},
'sec.org-secret': async (ctx) => {
const tag = ctx.run.file.runId
const memberWrite = await put(ctx, ctx.member, 'org', 'W9_MEMBER_TRY', 'nope')
const box = await teamSandbox(ctx)
const list = await control.as(ctx.member, 'GET', '/v1/secrets?scope=org')
const listed = JSON.stringify(list.body)
const hidden = !listed.includes(`org-${tag}`) && /W9_SHARED/.test(listed)
const env = await orcadEnv(ctx, box.sessionId, ['W9_SHARED'])
const ok = memberWrite.status === 403 && hidden && env.W9_SHARED === `org-${tag}`
return {
status: ok ? 'pass' : 'fail',
detail: `member write to org -> ${memberWrite.status}; list shows name without value: ${hidden}; injected into VM: ${env.W9_SHARED === `org-${tag}`}`,
evidence: [log('org list (metadata only)', listed.slice(0, 1200))]
}
},
'sec.precedence': async (ctx) => {
const tag = ctx.run.file.runId
const box = await teamSandbox(ctx)
const env = await orcadEnv(ctx, box.sessionId, ['W9_OVERRIDE', 'AWS_REGION'])
const personalWinsEnv = env.W9_OVERRIDE === `personal-${tag}`
const orgWinsAgentKey = env.AWS_REGION === `org-fake-${tag}`
return {
status: personalWinsEnv && orgWinsAgentKey ? 'pass' : 'fail',
detail: `env: personal beats org = ${personalWinsEnv}; agent key: org beats personal = ${orgWinsAgentKey}`
}
},
'sec.no-rebuild': async (ctx) => {
const tag = ctx.run.file.runId
const box = await teamSandbox(ctx)
await put(ctx, ctx.owner, 'org', 'W9_SHARED', `org2-${tag}`)
const s = await ctx.sandboxes.suspend(box.id)
const r = await ctx.sandboxes.resume(box.id)
if (r.status !== 200) {
return { status: 'fail', detail: `suspend ${s.status} resume ${r.status}` }
}
const env = await orcadEnv(ctx, r.body.sandbox.session!.id, ['W9_SHARED'])
for (const [scope, who, name] of [
['org', ctx.owner, 'W9_SHARED'],
['org', ctx.owner, 'W9_OVERRIDE'],
['org', ctx.owner, 'AWS_REGION'],
['personal', ctx.member, 'W9_OVERRIDE'],
['personal', ctx.member, 'AWS_REGION']
] as const) {
await control.as(who, 'DELETE', `/v1/secrets/${scope}/${name}`)
}
await ctx.sandboxes.destroy(box.id)
teamBox = null
const ok = env.W9_SHARED === `org2-${tag}`
return { status: ok ? 'pass' : 'fail', detail: `changed org secret visible after wake without rebuild: ${ok} (wake ${r.ms} ms)` }
}
}
+176
View File
@@ -0,0 +1,176 @@
// HTTP clients for the dev control plane, the dev billing Worker and Vercel (read/cleanup only).
// Every sandbox create/destroy goes through `Sandboxes` so the run ledger is always complete.
import { createVercelSandboxApi } from '../../sandbox-control/src/vercel-sandbox-api.ts'
import { config, secrets } from './env'
import type { RunState } from './run-state'
import { billingToken, sandboxToken, type Identity } from './tokens'
export type Reply<T = any> = { status: number; ms: number; body: T; headers: Headers }
async function call<T = any>(base: string, method: string, path: string, bearer: string, body?: unknown): Promise<Reply<T>> {
const started = performance.now()
const response = await fetch(`${base}${path}`, {
method,
headers: { authorization: `Bearer ${bearer}`, 'content-type': 'application/json' },
body: body === undefined ? undefined : JSON.stringify(body),
redirect: 'manual'
})
const text = await response.text()
let parsed: unknown = text
try {
parsed = text ? JSON.parse(text) : null
} catch {
// keep text
}
return { status: response.status, ms: Math.round(performance.now() - started), body: parsed as T, headers: response.headers }
}
export const control = {
as: async (who: Identity, method: string, path: string, body?: unknown) =>
call<any>(config.controlPlaneUrl, method, path, await sandboxToken(who), body),
operator: (method: string, path: string, body?: unknown) => {
const token = secrets.operatorToken()
if (!token) {
throw new Error('missing operator-dev.json')
}
return call(config.controlPlaneUrl, method, path, token, body)
},
raw: (method: string, path: string, bearer: string, body?: unknown) => call<any>(config.controlPlaneUrl, method, path, bearer, body)
}
export const billing = {
as: async (who: Identity, method: string, path: string, body?: unknown) =>
call<any>(config.billingUrl, method, path, await billingToken(who), body),
raw: (method: string, path: string, bearer: string, body?: unknown) => call<any>(config.billingUrl, method, path, bearer, body)
}
let vercelApi: ReturnType<typeof createVercelSandboxApi> | null = null
export function vercel() {
const token = secrets.vercelToken()
if (!token) {
throw new Error('missing tok.json (Vercel token)')
}
vercelApi ??= createVercelSandboxApi({ token, teamId: config.vercelTeamId, projectId: config.vercelProjectId })
return vercelApi
}
export async function vercelRest(method: string, path: string, query: Record<string, string> = {}) {
const url = new URL(`https://vercel.com/api${path}`)
url.searchParams.set('teamId', config.vercelTeamId)
for (const [k, v] of Object.entries(query)) {
url.searchParams.set(k, v)
}
const response = await fetch(url, { method, headers: { authorization: `Bearer ${secrets.vercelToken()}` } })
const text = await response.text()
return { status: response.status, json: text ? (JSON.parse(text) as any) : null }
}
export type SandboxView = {
id: string
workspaceId: string
status: string
machineSize: string
environmentBuildId?: string
region?: string
session: { id: string; startedAt: number; deadlineAt: number; leaseExpiresAt: number } | null
}
export type ReadyView = { sandbox: SandboxView; connection: { type: 'orca-server'; pairingCode: string; projectRoot: string } }
const VCPUS: Record<string, number> = { standard: 4, large: 8 }
/** Lifecycle calls with ledger bookkeeping; the provider name is `orca-<env>-<id>` (dev: orca-dev-). */
export class Sandboxes {
private live = new Map<string, { who: Identity; since: number | null }>()
constructor(private readonly run: RunState) {}
get count(): number {
return this.live.size
}
async create(
who: Identity,
body: { workspaceId: string; machineSize?: 'standard' | 'large'; repo?: { url: string; ref?: string }; [extra: string]: unknown }
): Promise<Reply<ReadyView & { error?: string; reason?: string }>> {
if (this.live.size >= config.maxConcurrentSandboxes) {
throw new Error(`eval concurrency cap (${config.maxConcurrentSandboxes}) reached`)
}
const reply = await control.as(who, 'POST', '/v1/sandboxes', { machineSize: 'standard', ...body })
if (reply.status === 200 && reply.body?.sandbox?.id) {
const id = reply.body.sandbox.id
this.run.created({ kind: 'sandbox', id, label: `orca-dev-${id} (${body.workspaceId})`, vcpus: VCPUS[reply.body.sandbox.machineSize] ?? 4 })
this.live.set(id, { who, since: Date.now() })
}
return reply
}
private stopClock(id: string): void {
const entry = this.live.get(id)
if (entry?.since) {
this.run.addRunning('sandbox', id, Date.now() - entry.since)
entry.since = null
}
}
async suspend(id: string): Promise<Reply<{ sandbox: SandboxView }>> {
const reply = await control.as(this.who(id), 'POST', `/v1/sandboxes/${id}/suspend`)
if (reply.status === 200) {
this.stopClock(id)
}
return reply
}
async resume(id: string): Promise<Reply<ReadyView>> {
const reply = await control.as(this.who(id), 'POST', `/v1/sandboxes/${id}/resume`)
const entry = this.live.get(id)
if (reply.status === 200 && entry && !entry.since) {
entry.since = Date.now()
}
return reply
}
async rollover(id: string): Promise<Reply<ReadyView>> {
return control.as(this.who(id), 'POST', `/v1/sandboxes/${id}/rollover`)
}
async get(id: string): Promise<Reply<{ sandbox: SandboxView }>> {
return control.as(this.who(id), 'GET', `/v1/sandboxes/${id}`)
}
async destroy(id: string): Promise<Reply<{ sandbox: SandboxView }>> {
const reply = await control.as(this.who(id), 'DELETE', `/v1/sandboxes/${id}`)
if (reply.status === 200 || reply.status === 404) {
this.stopClock(id)
this.run.destroyed('sandbox', id)
this.live.delete(id)
}
return reply
}
/** Destroy everything this run still holds, then verify nothing of ours is left at Vercel. */
async destroyAll(): Promise<{ destroyed: string[]; failed: string[] }> {
const destroyed: string[] = []
const failed: string[] = []
for (const id of [...this.live.keys()]) {
const reply = await this.destroy(id).catch(() => null)
;(reply && (reply.status === 200 || reply.status === 404) ? destroyed : failed).push(id)
}
return { destroyed, failed }
}
/** Commands on the VM over Vercel's API (operator path, not the user path). */
async exec(sessionId: string, script: string, timeoutMs = 60_000) {
return vercel().runCommand(sessionId, { command: 'bash', args: ['-c', script], timeoutMs })
}
private who(id: string): Identity {
const entry = this.live.get(id)
if (!entry) {
throw new Error(`sandbox ${id} is not tracked by this run`)
}
return entry.who
}
adopt(id: string, who: Identity): void {
this.live.set(id, { who, since: Date.now() })
}
}
+75
View File
@@ -0,0 +1,75 @@
// Shared per-run context for checks: the run file, the sandbox ledger, identities and a lazily
// created primary workspace (with the test repo) that most single-VM checks reuse to save cost.
import { Sandboxes, type ReadyView } from './clients'
import { config } from './env'
import type { CheckResult, Evidence, RunState, Status } from './run-state'
import type { Identity } from './tokens'
export type Outcome = { status: Status; detail: string; evidence?: Evidence[]; timingsMs?: Record<string, number> }
export type Check = (ctx: Ctx) => Promise<Outcome>
export class Ctx {
readonly sandboxes: Sandboxes
/** Solo account (dev:w9-eval) and a team org with an owner and a member (dev:org:w9-eval-team). */
readonly solo: Identity = { sub: 'w9-eval' }
readonly owner: Identity = { sub: 'w9-owner', org: 'w9-eval-team', orgRole: 'owner' }
readonly member: Identity = { sub: 'w9-member', org: 'w9-eval-team', orgRole: 'member' }
readonly outsider: Identity = { sub: 'w9-outsider' }
private primaryReady: Promise<ReadyView & { createMs: number }> | null = null
constructor(readonly run: RunState) {
this.sandboxes = new Sandboxes(run)
}
/** One workspace with the private test repo, shared by the single-VM checks of this run. */
primary(): Promise<ReadyView & { createMs: number }> {
this.primaryReady ??= (async () => {
const reply = await this.sandboxes.create(this.solo, {
workspaceId: `w9-primary-${this.run.file.runId}`,
repo: { url: config.testRepo }
})
if (reply.status !== 200) {
throw new Error(`primary create failed: ${reply.status} ${JSON.stringify(reply.body)}`)
}
return { ...reply.body, createMs: reply.ms }
})()
return this.primaryReady
}
/** Re-point the cached primary after a resume/rollover hands back a new session. */
updatePrimary(view: ReadyView): void {
const prev = this.primaryReady
this.primaryReady = (async () => ({ ...view, createMs: (await prev)?.createMs ?? 0 }))()
}
exec(sessionId: string, script: string, timeoutMs?: number) {
return this.sandboxes.exec(sessionId, script, timeoutMs)
}
}
export async function runCheck(ctx: Ctx, id: string, check: Check): Promise<CheckResult> {
const startedAt = new Date().toISOString()
const started = performance.now()
let outcome: Outcome
try {
outcome = await check(ctx)
} catch (error) {
outcome = { status: 'fail', detail: `threw: ${String(error instanceof Error ? error.message : error).slice(0, 600)}` }
}
const result: CheckResult = {
id,
status: outcome.status,
detail: outcome.detail,
evidence: outcome.evidence ?? [],
...(outcome.timingsMs ? { timingsMs: outcome.timingsMs } : {}),
startedAt,
durationMs: Math.round(performance.now() - started)
}
ctx.run.record(result)
const mark = { pass: 'PASS', fail: 'FAIL', partial: 'PART', not_landed: 'TODO', skipped: 'SKIP' }[result.status]
console.log(`${mark} ${id} (${result.durationMs}ms) ${result.detail.slice(0, 200)}`)
return result
}
export const log = (label: string, text: string): Evidence => ({ kind: 'log', label, text: text.slice(0, 4000) })
export const timing = (label: string, ms: number): Evidence => ({ kind: 'timing', label, text: `${ms} ms` })
+64
View File
@@ -0,0 +1,64 @@
// Eval configuration. DEV backends only: the harness refuses prod hosts and live Stripe keys.
// Secret files are read here and never printed; results and logs carry ids and timings only.
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
const SECRETS_DIR = process.env.ORCA_EVAL_SECRETS_DIR ?? '/tmp/vdrv/secrets'
const CONTEXT_DIR =
process.env.ORCA_EVAL_CONTEXT_DIR ??
'/Users/nwparker/orca/workspaces/orca/cloudy-cloud-with-rain-cloudy-Cloud-cloudy-cloud-with-rain-cloudy/.context/cloud'
export const config = {
controlPlaneUrl: process.env.ORCA_EVAL_CONTROL_URL ?? 'https://orca-sandbox-control-dev.lovecast.workers.dev',
billingUrl: process.env.ORCA_EVAL_BILLING_URL ?? 'https://orca-billing-dev.lovecast.workers.dev',
vercelTeamId: 'team_c5IO9DfEYAxKEUgMxrbEtO0a',
vercelProjectId: 'prj_SkVtv6zJurF9dUHpfTRy4v9avwPq',
/** Test repo the dev GitHub App is installed on. */
testRepo: 'https://github.com/nwparker/orca-sandbox-push-test',
orcaCli: process.env.ORCA_EVAL_CLI ?? '/Applications/Orca.app/Contents/Resources/bin/orca',
contextDir: CONTEXT_DIR,
runsDir: join(CONTEXT_DIR, 'acceptance-runs'),
reportPath: join(CONTEXT_DIR, 'orca-cloud-evaluation-report.html'),
secretsDir: SECRETS_DIR,
/** Hard concurrency ceiling for sandboxes this harness holds at once (brief: ≤6). */
maxConcurrentSandboxes: 6
}
for (const url of [config.controlPlaneUrl, config.billingUrl]) {
if (!/(-dev\.lovecast\.workers\.dev|127\.0\.0\.1|localhost)/.test(url)) {
throw new Error(`Refusing non-dev backend: ${url}`)
}
}
function readSecretJson<T>(file: string): T | null {
const path = join(SECRETS_DIR, file)
return existsSync(path) ? (JSON.parse(readFileSync(path, 'utf8')) as T) : null
}
function readText(path: string): string | null {
return existsSync(path) ? readFileSync(path, 'utf8').trim() || null : null
}
export const secrets = {
devJwk: () => readSecretJson<{ private: JsonWebKey & { kid?: string }; public: JsonWebKey }>('dev-jwk.json'),
operatorToken: () => readSecretJson<{ OPERATOR_TOKEN: string }>('operator-dev.json')?.OPERATOR_TOKEN ?? null,
/** Sandbox-scoped token if present, else the authed Vercel CLI's token (read-only use + cleanup). */
vercelToken: () =>
readSecretJson<{ bearerToken: string }>('tok.json')?.bearerToken ??
(() => {
const path = join(process.env.HOME ?? '', 'Library/Application Support/com.vercel.cli/auth.json')
return existsSync(path) ? ((JSON.parse(readFileSync(path, 'utf8')) as { token?: string }).token ?? null) : null
})(),
/** Shared AI Gateway key (W6; ~$5 free credit, no anthropic/* models). One tiny task per check. */
aiGatewayKey: () => {
const path = join(SECRETS_DIR, 'ai-gateway.key')
return existsSync(path) ? readFileSync(path, 'utf8').trim() : null
},
/** Billing internal routes (usage, entitlements/check, suspensions). */
controlPlaneToken: () => readText(join(SECRETS_DIR, 'control-plane.token')),
/** Auth -> billing membership pushes (POST /v1/internal/seats). */
membershipToken: () =>
process.env.ORCA_EVAL_MEMBERSHIP_TOKEN ?? readText(join(process.env.HOME ?? '', '.orca-secrets/billing-membership-token')),
/** Optional: sk_test_/rk_test_ only. Falls back to the Stripe CLI `orca` project profile. */
stripeKey: () => process.env.ORCA_EVAL_STRIPE_KEY ?? null
}
@@ -0,0 +1,70 @@
// Folds a desktop run (desktop/run-desktop.mjs results.json) into acceptance-runs/ as a RunFile so the
// report shows UI evidence (screenshots) and the desktop-measured benchmarks.
// tsx src/import-desktop.ts <desktop artifacts dir>
import { readFileSync, writeFileSync } from 'node:fs'
import { basename, join } from 'node:path'
import { config } from './env'
import type { CheckResult, RunFile } from './run-state'
const dir = process.argv[2]!
const d = JSON.parse(readFileSync(join(dir, 'results.json'), 'utf8'))
/** Desktop step ids -> catalog ids (the desktop's sleep/wake row is UI evidence for the same feature). */
const MAP: Record<string, string> = {
'cli.desktop': 'cli.desktop',
'acc.port-forward': 'acc.port-forward',
'acc.ssh': 'acc.ssh',
'life.transfer-local': 'life.transfer-local'
}
const checks: CheckResult[] = []
const benchmarks: RunFile['benchmarks'] = []
const samples = (v: any): number[] | null => (v && Array.isArray(v.samples) ? v.samples : null)
for (const s of d.steps ?? []) {
const flat: Record<string, number> = {}
for (const [k, v] of Object.entries(s.timings ?? {})) {
if (typeof v === 'number') {
flat[k] = v
} else if (samples(v)) {
flat[`${k}.p50`] = (v as any).p50
}
}
const id = MAP[s.id]
if (id) {
checks.push({
id,
status: s.status,
detail: `Desktop app (Electron, real UI): ${s.detail}`,
evidence: (s.screenshots ?? []).map((p: string) => ({ kind: 'screenshot' as const, label: basename(p), path: join(dir, p) })),
timingsMs: flat,
startedAt: s.startedAt ?? d.startedAt,
durationMs: s.durationMs ?? 0
})
}
const t = s.timings ?? {}
if (s.id === 'term.keystroke' && samples(t.keystrokeToEchoMs)) {
benchmarks.push({ name: 'keystroke.echo', samplesMs: samples(t.keystrokeToEchoMs)!, note: 'desktop xterm: keydown -> char in DOM, warm connection' })
}
if (s.id === 'life.sleep-wake' && samples(t.wakeToFirstEchoMs)) {
benchmarks.push({ name: 'wake.firstKeystroke', samplesMs: samples(t.wakeToFirstEchoMs)!, note: 'desktop: open slept workspace -> first typed char echoed in xterm' })
}
if (s.id === 'acc.port-forward' && samples(t.httpGetMs)) {
benchmarks.push({ name: 'port.forward', samplesMs: samples(t.httpGetMs)!, note: 'GET localhost:4173 forwarded by the desktop to the VM (Mac -> iad1)' })
}
if (s.id === 'cli.desktop' && typeof t.createToFirstTerminalOutputMs === 'number') {
benchmarks.push({ name: 'desktop.createToTerminal', samplesMs: [t.createToFirstTerminalOutputMs], note: 'desktop: click Create -> first terminal output (n=1 per desktop run)' })
}
}
const runId = `desktop-${basename(dir).replace(/^desktop-/, '')}`
const run: RunFile = {
runId,
startedAt: d.startedAt,
finishedAt: d.finishedAt,
target: { controlPlaneUrl: d.sandboxUrl, billingUrl: config.billingUrl, desktopRef: `${d.branch}@${String(d.gitHead).slice(0, 11)}` },
checks,
benchmarks,
resources: [],
notes: [`desktop account ${d.account}; cleanup: ${JSON.stringify(d.cleanup).slice(0, 300)}`]
}
writeFileSync(join(config.runsDir, `${runId}.json`), JSON.stringify(run, null, 2))
console.log(`imported ${checks.length} checks, ${benchmarks.length} benchmarks -> ${runId}.json`)
@@ -0,0 +1,12 @@
// Debug helper: prints an environment build (as the team owner) without secrets.
// tsx src/inspect-build.ts <envId> <buildId>
import { control } from './clients'
const [envId, buildId] = process.argv.slice(2)
const owner = { sub: 'w9-owner', org: 'w9-eval-team', orgRole: 'owner' as const }
const envs = await control.as(owner, 'GET', '/v1/environments')
console.log('environments', JSON.stringify(envs.body).slice(0, 1500))
if (envId && buildId) {
const b = await control.as(owner, 'GET', `/v1/environments/${envId}/builds/${buildId}`)
console.log('build', JSON.stringify(b.body, null, 1).slice(0, 4000))
}
@@ -0,0 +1,8 @@
// Lists every sandbox in Orca's Vercel project (status + tags) so stray eval resources are visible.
// tsx src/list-provider.ts
import { vercel } from './clients'
const { sandboxes } = await vercel().listSandboxes()
for (const s of sandboxes) {
console.log(`${s.name}\t${s.status}\t${JSON.stringify(s.tags)}`)
}
+60
View File
@@ -0,0 +1,60 @@
// The user path into a cloud workspace: the shipped `orca` CLI over the sandbox's pairing code
// (E2EE to orcad in the VM, the same protocol the desktop uses). The pairing code is never logged.
import { execFile } from 'node:child_process'
import { config } from './env'
export type CliResult = { ok: boolean; ms: number; json: any; stderr: string }
export function orca(pairingCode: string, args: string[], timeoutMs = 60_000): Promise<CliResult> {
const started = performance.now()
return new Promise((resolve) => {
execFile(
config.orcaCli,
[...args, '--json', '--pairing-code', pairingCode],
{ timeout: timeoutMs, maxBuffer: 8 * 1024 * 1024, env: { ...process.env, ORCA_PAIRING_CODE: '' } },
(error, stdout, stderr) => {
let json: any = null
try {
json = JSON.parse(stdout)
} catch {
json = null
}
resolve({
ok: !error && json?.ok !== false,
ms: Math.round(performance.now() - started),
json,
stderr: String(stderr).replaceAll(pairingCode, '<pairing>').slice(0, 2000)
})
}
)
})
}
/** Registers the project root and opens a terminal there; returns the terminal handle. */
export async function openTerminal(pairingCode: string, projectRoot: string, kind: 'git' | 'folder' = 'git') {
let repo = await orca(pairingCode, ['repo', 'add', '--path', projectRoot, '--kind', kind])
if (!repo.ok && kind === 'git') {
repo = await orca(pairingCode, ['repo', 'add', '--path', projectRoot, '--kind', 'folder'])
}
const term = await orca(pairingCode, ['terminal', 'create', '--worktree', `path:${projectRoot}`, '--title', 'eval'])
const handle: string | undefined =
term.json?.result?.terminal?.handle ?? term.json?.result?.handle ?? term.json?.result?.terminal?.id
return { repo, term, handle }
}
/** Sends a line and waits until `marker` shows up in the terminal output. Returns ms send->seen. */
export async function typeAndSee(pairingCode: string, handle: string, line: string, marker: string, timeoutMs = 30_000) {
const started = performance.now()
const sent = await orca(pairingCode, ['terminal', 'send', '--terminal', handle, '--text', line, '--enter'])
if (!sent.ok) {
return { ok: false, ms: Math.round(performance.now() - started), output: '', error: sent.stderr || JSON.stringify(sent.json?.error ?? null) }
}
while (performance.now() - started < timeoutMs) {
const read = await orca(pairingCode, ['terminal', 'read', '--terminal', handle])
const text = JSON.stringify(read.json?.result ?? '')
if (text.includes(marker)) {
return { ok: true, ms: Math.round(performance.now() - started), output: text.slice(-1500), error: '' }
}
}
return { ok: false, ms: Math.round(performance.now() - started), output: '', error: 'marker not seen' }
}
+49
View File
@@ -0,0 +1,49 @@
// Debug: minimal environment build, streams events for a few minutes, then deletes the environment.
// tsx src/probe-build.ts [script]
import { sandboxToken } from './tokens'
import { control } from './clients'
import { config } from './env'
const owner = { sub: 'w9-owner', org: 'w9-eval-team', orgRole: 'owner' as const }
const env = await control.as(owner, 'POST', '/v1/environments', { name: `w9-probe-${Date.now() % 100000}`, repos: [{ url: config.testRepo }], installScript: process.argv[2] ?? 'echo hello-build' })
const envId = env.body?.environment?.id
console.log('env', env.status, envId)
try {
const b = await control.as(owner, 'POST', `/v1/environments/${envId}/builds`)
const id = b.body?.build?.id
console.log('build', b.status, id, b.body?.build?.status)
if (process.argv[3] === 'poll') {
const t0 = Date.now()
let s: any = null
while (Date.now() - t0 < 240_000) {
s = (await control.as(owner, 'GET', `/v1/environments/${envId}/builds/${id}`)).body?.build
console.log(Math.round((Date.now() - t0) / 1000), s?.status, s?.logBytes)
if (['succeeded', 'failed', 'canceled'].includes(s?.status)) break
await new Promise((r) => setTimeout(r, 10_000))
}
process.exitCode = 0
}
const ctl = new AbortController()
setTimeout(() => ctl.abort(), 240_000)
const r = await fetch(`${config.controlPlaneUrl}/v1/environments/${envId}/builds/${id}/events`, { headers: { authorization: `Bearer ${await sandboxToken(owner)}` }, signal: ctl.signal }).catch((e) => e)
if (r instanceof Response && r.body) {
const t0 = Date.now()
const dec = new TextDecoder()
try {
for await (const chunk of r.body as any) {
for (const line of dec.decode(chunk).split('\n').filter(Boolean)) {
const e = JSON.parse(line)
console.log(Math.round((Date.now() - t0) / 1000), e.type, e.type === 'log' ? String(e.text ?? e.line ?? JSON.stringify(e)).slice(0, 160) : JSON.stringify(e.build ?? e).slice(0, 200))
}
}
} catch (e) {
console.log('stream ended', String(e).slice(0, 80))
}
} else {
console.log('events', r instanceof Response ? r.status : String(r))
}
const final = await control.as(owner, 'GET', `/v1/environments/${envId}/builds/${id}`)
console.log('final', JSON.stringify(final.body?.build).slice(0, 600))
} finally {
console.log('delete', (await control.as(owner, 'DELETE', `/v1/environments/${envId}`)).status)
}
@@ -0,0 +1,16 @@
// Repro for F1: POST resume on a sandbox that is already running (expected: fast, idempotent).
// tsx src/probe-resume-running.ts
import { control } from './clients'
const who = { sub: 'w9-probe' }
const c = await control.as(who, 'POST', '/v1/sandboxes', { workspaceId: 'w9-probe-resume', machineSize: 'standard' })
console.log('create', c.status, c.ms)
const id = c.body?.sandbox?.id
try {
const r = await control.as(who, 'POST', `/v1/sandboxes/${id}/resume`)
console.log('resume-while-running', r.status, r.ms, JSON.stringify(r.body).slice(0, 120))
const w = await control.as(who, 'POST', `/v1/sandboxes/${id}/wake?wait=30s`)
console.log('wake-while-running', w.status, w.ms, w.body?.state)
} finally {
console.log('destroy', (await control.as(who, 'DELETE', `/v1/sandboxes/${id}`)).status)
}
+3
View File
@@ -0,0 +1,3 @@
import { renderReport } from './report'
console.log(renderReport())
+181
View File
@@ -0,0 +1,181 @@
// Renders .context/cloud/orca-cloud-evaluation-report.html from every run on disk: the latest
// result per feature (with the run it came from), benchmarks, gaps, cost and the resource ledger.
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { relative } from 'node:path'
import { CONDUCTOR, EARLIER } from './bench'
import { CATALOG, SECTIONS, type Feature } from './catalog'
import { config } from './env'
import { loadRuns, percentile, type CheckResult, type Resource, type RunFile } from './run-state'
/** Vercel Sandbox list prices (Pro, 2026-11): active CPU per vCPU-hour, memory per GB-hour (2 GB/vCPU). */
const CPU_PER_VCPU_HOUR = 0.128
const MEM_PER_GB_HOUR = 0.0106
const GB_PER_VCPU = 2
const esc = (s: unknown) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' })[c]!)
const sec = (ms: number | null) => (ms === null ? 'n/a' : `${(ms / 1000).toFixed(2)} s`)
const STATUS: Record<string, { cls: string; label: string }> = {
pass: { cls: 'g', label: '✅ pass' },
partial: { cls: 'w', label: '⚠️ partial' },
fail: { cls: 'b', label: '❌ fail' },
not_landed: { cls: 'n', label: '⏳ not landed' },
skipped: { cls: 'n', label: '— skipped' }
}
export function resourceCost(rows: Resource[]): { vcpuHours: number; usd: number } {
let vcpuHours = 0
for (const r of rows) {
if (r.kind !== 'sandbox') {
continue
}
const ms = r.runningMs ?? (r.destroyedAt ? Date.parse(r.destroyedAt) - Date.parse(r.createdAt) : 0)
vcpuHours += ((r.vcpus ?? 4) * ms) / 3_600_000
}
// Upper bound: Vercel bills active CPU, so idle time is cheaper than this.
const usd = vcpuHours * (CPU_PER_VCPU_HOUR + GB_PER_VCPU * MEM_PER_GB_HOUR)
return { vcpuHours, usd }
}
function latestResults(runs: RunFile[]): Map<string, { result: CheckResult; run: RunFile }> {
const out = new Map<string, { result: CheckResult; run: RunFile }>()
for (const run of runs) {
for (const result of run.checks) {
out.set(result.id, { result, run })
}
}
return out
}
function evidenceHtml(result: CheckResult, run: RunFile): string {
const parts: string[] = []
if (result.timingsMs) {
parts.push(Object.entries(result.timingsMs).map(([k, v]) => `<code>${esc(k)} ${sec(v)}</code>`).join(' '))
}
for (const e of result.evidence) {
if (e.path) {
const href = relative(config.contextDir, e.path)
parts.push(e.kind === 'screenshot' ? `<a href="${esc(href)}"><img src="${esc(href)}" alt="${esc(e.label)}" loading="lazy"></a>` : `<a href="${esc(href)}">${esc(e.label)}</a>`)
} else if (e.text) {
parts.push(`<details><summary>${esc(e.label)}</summary><pre>${esc(e.text)}</pre></details>`)
}
}
parts.push(`<span class="small muted">run ${esc(run.runId)} · ${esc(result.startedAt.slice(0, 16).replace('T', ' '))}Z</span>`)
return parts.join('<br>')
}
function featureRow(f: Feature, hit: { result: CheckResult; run: RunFile } | undefined): string {
const status = hit?.result.status ?? 'not_landed'
const s = STATUS[status]!
const detail = hit ? esc(hit.result.detail) : `No check result yet; waiting on <b>${esc(f.owner)}</b>.`
return `<tr><th>${esc(f.feature)}<div class="small muted"><code>${esc(f.id)}</code> · ${esc(f.priority)} · ${esc(f.owner)}</div></th>
<td class="${s.cls} c">${s.label}</td><td>${detail}</td><td>${hit ? evidenceHtml(hit.result, hit.run) : ''}</td>
<td>${esc(f.conductor)}</td><td>${f.better ? esc(f.better) : ''}</td></tr>`
}
function benchTable(runs: RunFile[]): string {
const latest = new Map<string, { samples: number[]; run: RunFile; note?: string }>()
for (const run of runs) {
for (const b of run.benchmarks) {
if (b.samplesMs.length > 0) {
latest.set(b.name, { samples: b.samplesMs, run, ...(b.note ? { note: b.note } : {}) })
}
}
}
const names = ['create.api', 'create.usable', 'create.firstEcho', 'desktop.createToTerminal', 'keystroke.echo', 'wake.api', 'wake.firstEcho', 'wake.firstKeystroke', 'suspend.api', 'rollover.gap', 'port.forward', 'share.coldLoad']
const rows = names.map((name) => {
const b = latest.get(name)
const n = b?.samples.length ?? 0
return `<tr><th><code>${esc(name)}</code></th><td class="c">${n}</td><td class="c ${n >= 10 ? '' : 'w'}">${b ? sec(percentile(b.samples, 50)) : 'pending'}</td>
<td class="c">${b ? sec(percentile(b.samples, 95)) : ''}</td><td class="c">${b ? `${sec(Math.min(...b.samples))} – ${sec(Math.max(...b.samples))}` : ''}</td>
<td>${esc(CONDUCTOR[name] ?? '')}</td><td>${esc(EARLIER[name] ?? '')}</td><td class="small">${b ? `run ${esc(b.run.runId)}${b.note ? `<br>${esc(b.note)}` : ''}` : ''}</td></tr>`
})
return `<div class="tablewrap"><table><thead><tr><th>Metric</th><th>n</th><th>p50</th><th>p95</th><th>min – max</th><th>Conductor (documented)</th><th>Our earlier numbers</th><th>Source</th></tr></thead><tbody>${rows.join('')}</tbody></table></div>`
}
function ledgerTable(runs: RunFile[]): string {
const rows = runs.flatMap((run) =>
run.resources.map(
(r) => `<tr><td class="c">${esc(run.runId)}</td><td>${esc(r.kind)}</td><td><code>${esc(r.id)}</code></td><td>${esc(r.label ?? '')}</td>
<td class="c">${esc(r.createdAt.slice(11, 19))}</td><td class="c ${r.destroyedAt ? 'g' : 'b'}">${r.destroyedAt ? esc(r.destroyedAt.slice(11, 19)) : 'NOT DESTROYED'}</td></tr>`
)
)
return `<details><summary>${rows.length} resources across ${runs.length} runs</summary><div class="tablewrap"><table><thead><tr><th>Run</th><th>Kind</th><th>Id</th><th>Label</th><th>Created (UTC)</th><th>Destroyed</th></tr></thead><tbody>${rows.join('')}</tbody></table></div></details>`
}
/** Hand-curated findings/gaps (HTML snippets), versioned with the harness: findings.json {findings, gaps}. */
function loadFindings(): { gaps?: string[]; findings?: string[] } {
const path = new URL('../findings.json', import.meta.url).pathname
return existsSync(path) ? (JSON.parse(readFileSync(path, 'utf8')) as { gaps?: string[]; findings?: string[] }) : {}
}
export function renderReport(): string {
const extra = loadFindings()
const runs = loadRuns()
const latest = latestResults(runs)
const counts = { pass: 0, partial: 0, fail: 0, not_landed: 0, skipped: 0 }
for (const f of CATALOG) {
counts[(latest.get(f.id)?.result.status ?? 'not_landed') as keyof typeof counts]++
}
const allResources = runs.flatMap((r) => r.resources)
const cost = resourceCost(allResources)
const leaked = allResources.filter((r) => !r.destroyedAt)
const sections = SECTIONS.map((section) => {
const rows = CATALOG.filter((f) => f.section === section).map((f) => featureRow(f, latest.get(f.id)))
return `<tr class="sec"><th colspan="6">${esc(section)}</th></tr>${rows.join('')}`
}).join('')
const lastRun = runs.at(-1)
const html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Orca Cloud evaluation report</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
:root{--bg:#fff;--fg:#1b1f24;--muted:#5b636e;--border:#d8dde3;--head:#f3f5f8;--good-bg:#e6f6ea;--good-fg:#145c2a;--warn-bg:#fff5d6;--warn-fg:#6b4e00;--bad-bg:#fde8e8;--bad-fg:#8a1c1c;--na-bg:#eef0f3;--link:#0b5fcc;--code:#f1f3f6}
@media (prefers-color-scheme:dark){:root{--bg:#111418;--fg:#e3e7ec;--muted:#9aa3ad;--border:#2c333b;--head:#1a1f25;--good-bg:#12301c;--good-fg:#8fe0a6;--warn-bg:#352a0b;--warn-fg:#f2cf6b;--bad-bg:#3a1515;--bad-fg:#f3a3a3;--na-bg:#1d2228;--link:#6aa7ff;--code:#1d2228}}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--fg);font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif}
main{max-width:1500px;margin:0 auto;padding:24px}h1{font-size:22px;margin:0 0 4px}h2{font-size:18px;margin:32px 0 8px;border-bottom:1px solid var(--border);padding-bottom:4px}
.sub,.muted{color:var(--muted)}a{color:var(--link)}code{background:var(--code);padding:1px 4px;border-radius:3px;font-size:12.5px}
.tablewrap{overflow:auto;max-height:85vh;border:1px solid var(--border);border-radius:6px}table{border-collapse:separate;border-spacing:0;width:100%}
th,td{border-bottom:1px solid var(--border);border-right:1px solid var(--border);padding:7px 9px;vertical-align:top;text-align:left}
thead th{position:sticky;top:0;background:var(--head);z-index:2;font-weight:600}tbody th{background:var(--head);font-weight:600;width:20%}
td.g{background:var(--good-bg);color:var(--good-fg)}td.w{background:var(--warn-bg);color:var(--warn-fg)}td.b{background:var(--bad-bg);color:var(--bad-fg)}td.n{background:var(--na-bg)}
tr.sec th{background:var(--bg);font-size:12px;text-transform:uppercase;letter-spacing:.05em;color:var(--muted)}
.callout{border-left:4px solid var(--link);background:var(--head);padding:10px 14px;border-radius:4px;margin:12px 0}
.small{font-size:12.5px}td.c{white-space:nowrap}pre{white-space:pre-wrap;word-break:break-word;font-size:12px;max-height:300px;overflow:auto}
img{max-width:260px;border:1px solid var(--border);border-radius:4px}.kpi span{display:inline-block;padding:2px 8px;border-radius:4px;margin-right:6px}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head><body><main>
<h1>Orca Cloud evaluation report</h1>
<p class="sub">Generated ${esc(new Date().toISOString().slice(0, 16).replace('T', ' '))}Z by <code>cloud/apps/acceptance</code> (branch <code>cloud/w9-eval</code>) from ${runs.length} run(s) in <code>acceptance-runs/</code>.
Real Vercel Sandbox VMs (team lovecast, Pro, iad1) via the DEV control plane <code>${esc(config.controlPlaneUrl)}</code> and DEV billing <code>${esc(config.billingUrl)}</code>; Stripe TEST mode only.
Parity bar: <a href="conductor-parity-matrix.html">conductor-parity-matrix.html</a>. Each row shows the most recent result for that feature and the run it came from.</p>
<div class="callout kpi"><b>Status (${CATALOG.length} features):</b>
<span class="g" style="background:var(--good-bg);color:var(--good-fg)">✅ ${counts.pass} pass</span>
<span style="background:var(--warn-bg);color:var(--warn-fg)">⚠️ ${counts.partial} partial</span>
<span style="background:var(--bad-bg);color:var(--bad-fg)">❌ ${counts.fail} fail</span>
<span style="background:var(--na-bg)">⏳ ${counts.not_landed} not landed</span>
<span style="background:var(--na-bg)">— ${counts.skipped} skipped</span><br>
<b>Last run:</b> ${lastRun ? `${esc(lastRun.runId)} (${esc(lastRun.startedAt.slice(0, 16))}Z, target ${esc(lastRun.target.integrationSha ?? lastRun.target.desktopRef ?? 'API only')})` : 'none'} ·
<b>Eval cost so far:</b> ≈ $${cost.usd.toFixed(2)} Vercel compute (${cost.vcpuHours.toFixed(2)} vCPU-h, upper bound at list price) · <b>Leaked resources:</b> ${leaked.length === 0 ? '0 ✅' : `<span style="color:var(--bad-fg)">${leaked.length}</span>`}</div>
<h2>Features</h2>
<div class="tablewrap"><table><thead><tr><th>Feature</th><th>Status</th><th>Result</th><th>Evidence</th><th>Conductor</th><th>Better than Conductor</th></tr></thead><tbody>${sections}</tbody></table></div>
<h2>Benchmarks</h2>
<p class="small muted">Nearest-rank percentiles. Definitions: <code>create.api</code> = POST /v1/sandboxes until orcad is ready (golden snapshot boot + private repo clone);
<code>create.usable</code> adds the user's client connecting (orca CLI, E2EE handshake); <code>*.firstEcho</code> adds registering the project, opening a PTY and seeing a typed command echoed
(each CLI call reconnects, so this is an upper bound on what the desktop's warm connection sees); <code>rollover.gap</code> = drain + stop + boot + orcad ready.</p>
${benchTable(runs)}
<p class="small muted">${runs.filter((r) => r.benchmarks.length).slice(-1).flatMap((r) => r.notes.filter((n) => n.startsWith('load'))).map(esc).join(' · ')}</p>
<h2>Known gaps and findings</h2>
<ul>${[...(extra.findings ?? []), ...(extra.gaps ?? [])].map((g) => `<li>${g}</li>`).join('') || '<li>none recorded</li>'}</ul>
<h2>Cost of the eval and every cloud resource</h2>
<p class="small">Compute estimate = Σ vCPU × running time × ($${CPU_PER_VCPU_HOUR}/vCPU-h + ${GB_PER_VCPU} GB × $${MEM_PER_GB_HOUR}/GB-h). AI Gateway spend is read from <code>/v1/credits</code> when an agent check runs. Stripe objects are test mode (free).</p>
${ledgerTable(runs)}
<h2>How to rerun</h2>
<pre>cd cloud/apps/acceptance
pnpm accept # every check + cleanup verification, then this report
pnpm accept --bench --n 10 # + benchmarks (n per metric)
pnpm accept --only sub.tools,life.rollover
pnpm sweep # list/destroy anything a crashed run left behind
pnpm report # re-render this page from acceptance-runs/*.json</pre>
</main></body></html>`
writeFileSync(config.reportPath, html)
return config.reportPath
}
+127
View File
@@ -0,0 +1,127 @@
// One eval run on disk: checks, benchmarks and a ledger of every cloud resource created/destroyed.
// Written after every change so a crash still leaves an accurate ledger for the sweeper.
import { mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { config } from './env'
export type Status = 'pass' | 'fail' | 'partial' | 'not_landed' | 'skipped'
export type Evidence = { kind: 'log' | 'screenshot' | 'video' | 'timing' | 'json'; label: string; path?: string; text?: string }
export type CheckResult = {
id: string
status: Status
detail: string
evidence: Evidence[]
timingsMs?: Record<string, number>
startedAt: string
durationMs: number
}
export type Resource = {
kind: 'sandbox' | 'snapshot' | 'stripe_customer' | 'stripe_test_clock' | 'stripe_subscription' | 'ai_gateway_key' | 'other'
id: string
label?: string
createdAt: string
destroyedAt?: string
/** For sandboxes: vCPUs and running intervals, for the cost estimate. */
vcpus?: number
runningMs?: number
}
export type Benchmark = { name: string; samplesMs: number[]; note?: string }
export type RunFile = {
runId: string
startedAt: string
finishedAt?: string
target: { controlPlaneUrl: string; billingUrl: string; desktopRef?: string; integrationSha?: string }
checks: CheckResult[]
benchmarks: Benchmark[]
resources: Resource[]
notes: string[]
}
export class RunState {
readonly file: RunFile
readonly path: string
readonly artifactsDir: string
constructor(runId: string, target: RunFile['target']) {
mkdirSync(config.runsDir, { recursive: true })
this.path = join(config.runsDir, `${runId}.json`)
this.artifactsDir = join(config.runsDir, runId)
mkdirSync(this.artifactsDir, { recursive: true })
this.file = { runId, startedAt: new Date().toISOString(), target, checks: [], benchmarks: [], resources: [], notes: [] }
this.save()
}
save(): void {
writeFileSync(this.path, JSON.stringify(this.file, null, 2))
}
created(resource: Omit<Resource, 'createdAt'>): Resource {
const row = { ...resource, createdAt: new Date().toISOString() }
this.file.resources.push(row)
this.save()
return row
}
destroyed(kind: Resource['kind'], id: string, runningMs?: number): void {
for (const row of this.file.resources) {
if (row.kind === kind && row.id === id && !row.destroyedAt) {
row.destroyedAt = new Date().toISOString()
if (runningMs !== undefined) {
row.runningMs = runningMs
}
}
}
this.save()
}
addRunning(kind: Resource['kind'], id: string, ms: number): void {
const row = this.file.resources.find((r) => r.kind === kind && r.id === id)
if (row) {
row.runningMs = (row.runningMs ?? 0) + ms
this.save()
}
}
record(result: CheckResult): void {
this.file.checks = this.file.checks.filter((c) => c.id !== result.id)
this.file.checks.push(result)
this.save()
}
bench(name: string, samplesMs: number[], note?: string): void {
this.file.benchmarks = this.file.benchmarks.filter((b) => b.name !== name)
this.file.benchmarks.push({ name, samplesMs, ...(note ? { note } : {}) })
this.save()
}
note(text: string): void {
this.file.notes.push(text)
this.save()
}
finish(): void {
this.file.finishedAt = new Date().toISOString()
this.save()
}
}
export function loadRuns(): RunFile[] {
try {
return readdirSync(config.runsDir)
.filter((name) => name.endsWith('.json'))
.map((name) => JSON.parse(readFileSync(join(config.runsDir, name), 'utf8')) as RunFile)
.sort((a, b) => a.startedAt.localeCompare(b.startedAt))
} catch {
return []
}
}
export function percentile(samples: number[], p: number): number | null {
if (samples.length === 0) {
return null
}
const sorted = [...samples].sort((a, b) => a - b)
// Nearest-rank: the smallest sample with at least p% of samples at or below it.
const rank = Math.max(1, Math.ceil((p / 100) * sorted.length))
return sorted[rank - 1]!
}
+94
View File
@@ -0,0 +1,94 @@
// Orca Cloud eval: every parity-matrix feature against real Vercel sandboxes via the DEV control plane.
// tsx src/run.ts [--only a,b] [--skip a,b] [--bench] [--n 10] [--no-checks] [--sha <integration sha>]
// Always destroys what it created (also on failure/Ctrl-C), verifies nothing is left, renders the report.
import { benchCreate, benchWakeAndRollover } from './bench'
import { CATALOG } from './catalog'
import { agentChecks } from './checks/agents'
import { billingChecks } from './checks/billing'
import { lifecycleChecks } from './checks/lifecycle'
import { platformChecks } from './checks/platform'
import { coreChecks } from './checks/core'
import { opsChecks } from './checks/ops'
import { pendingChecks } from './checks/pending'
import { secretChecks } from './checks/secrets'
import { Ctx, runCheck, type Check } from './context'
import { control } from './clients'
import { config } from './env'
import { renderReport } from './report'
import { RunState } from './run-state'
const args = process.argv.slice(2)
const flag = (name: string) => args.includes(`--${name}`)
const value = (name: string) => {
const i = args.indexOf(`--${name}`)
return i >= 0 ? args[i + 1] : undefined
}
const only = value('only')?.split(',')
const skip = new Set(value('skip')?.split(',') ?? [])
const n = Number(value('n') ?? 10)
const CHECKS: Record<string, Check> = { ...pendingChecks, ...coreChecks, ...secretChecks, ...opsChecks, ...billingChecks, ...agentChecks, ...lifecycleChecks, ...platformChecks }
/** Checks that change the primary's lifecycle run after the ones that only read it; cleanup is last. */
const LATE = ['billing.teardown', 'ops.kill-switch', 'life.sleep-wake', 'life.rollover', 'ops.cleanup']
const runId = new Date().toISOString().replace(/[-:]/g, '').replace(/\..+/, '').replace('T', '-')
const run = new RunState(runId, {
controlPlaneUrl: config.controlPlaneUrl,
billingUrl: config.billingUrl,
...(value('sha') ? { integrationSha: value('sha') } : {})
})
const ctx = new Ctx(run)
console.log(`eval run ${runId} -> ${run.path}`)
let interrupted = false
process.on('SIGINT', () => {
if (interrupted) {
process.exit(130)
}
interrupted = true
console.log('\ninterrupted: cleaning up (Ctrl-C again to abort cleanup)')
})
/** Load on the shared dev stack while we measure (other workstreams run VMs too). */
async function noteLoad(when: string): Promise<void> {
const s = await control.operator('GET', '/v1/internal/status').catch(() => null)
run.note(`load ${when}: ${s?.body?.active ?? '?'} active / ${s?.body?.running ?? '?'} running sandboxes on the dev control plane (all accounts)`)
}
try {
await noteLoad('at start')
// Idempotent: the eval accounts need the dev GitHub App installation to clone the private test repo.
for (const accountId of ['dev:w9-eval', 'dev:org:w9-eval-team', 'dev:w9-desktop']) {
const b = await control.operator('POST', '/v1/internal/github/bindings', { repoUrl: config.testRepo, accountId })
if (b.status !== 200) {
run.note(`github binding for ${accountId} -> ${b.status}`)
}
}
if (!flag('no-checks')) {
const ids = CATALOG.map((f) => f.id).filter((id) => (!only || only.includes(id)) && !skip.has(id) && !LATE.includes(id))
const late = LATE.filter((id) => (!only || only.includes(id)) && !skip.has(id))
for (const id of [...ids, ...late]) {
if (interrupted) {
break
}
const check = CHECKS[id]
if (check) {
await runCheck(ctx, id, check)
}
}
}
if (flag('bench') && !interrupted) {
await noteLoad('before benchmarks')
await benchCreate(ctx, n)
await benchWakeAndRollover(ctx, n)
await noteLoad('after benchmarks')
}
} finally {
const { failed } = await ctx.sandboxes.destroyAll()
if (failed.length) {
run.note(`cleanup failed for ${failed.join(', ')}; run pnpm sweep`)
console.log(`CLEANUP FAILED: ${failed.join(', ')}`)
}
run.finish()
console.log(`report: ${renderReport()}`)
}
+127
View File
@@ -0,0 +1,127 @@
// Stripe TEST-mode helpers: key from ORCA_EVAL_STRIPE_KEY or the Stripe CLI `orca` profile (never
// printed), refusing anything that is not a test key or returns a live-mode object.
import { readFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { secrets } from './env'
let cached: string | null = null
export function stripeKey(): string {
if (cached) {
return cached
}
let key = secrets.stripeKey()
if (!key) {
const toml = readFileSync(`${homedir()}/.config/stripe/config.toml`, 'utf8')
const section = toml.split(/^\[/m).find((s) => s.startsWith('orca]'))
key = /test_mode_api_key\s*=\s*['"]([^'"]+)['"]/.exec(section ?? '')?.[1] ?? null
}
if (!key || !/^(sk|rk)_test_/.test(key)) {
throw new Error('Refusing: a Stripe TEST key is required')
}
cached = key
return key
}
function encode(params: Record<string, unknown>, prefix = ''): string[] {
return Object.entries(params).flatMap(([k, v]) => {
const key = prefix ? `${prefix}[${k}]` : k
if (v === undefined) {
return []
}
if (v !== null && typeof v === 'object') {
return encode(v as Record<string, unknown>, key)
}
return [`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`]
})
}
export async function stripe<T = any>(method: 'GET' | 'POST' | 'DELETE', path: string, params?: Record<string, unknown>): Promise<T> {
const url = new URL(`https://api.stripe.com${path}`)
const body = params ? encode(params).join('&') : undefined
if (method !== 'POST' && body) {
url.search = body
}
const response = await fetch(url, {
method,
headers: { authorization: `Bearer ${stripeKey()}`, 'content-type': 'application/x-www-form-urlencoded', 'stripe-version': '2025-09-30.clover' },
body: method === 'POST' ? body : 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
}
export type CheckoutCard = { number: string; label: string; threeDS?: boolean }
export const CARDS = {
visa: { number: '4242424242424242', label: 'Visa 4242' },
threeDS: { number: '4000002760003184', label: '3DS2 required 3184', threeDS: true }
} satisfies Record<string, CheckoutCard>
/** Completes hosted Checkout in headless Chrome; returns ms and a screenshot path. */
export async function completeCheckout(url: string, card: CheckoutCard, shotPath: string): Promise<{ ms: number; shot: string; landed: string }> {
const { chromium } = await import('playwright-core')
const browser = await chromium.launch({ headless: true, channel: 'chrome' })
const started = performance.now()
try {
const page = await browser.newPage({ viewport: { width: 1200, height: 900 } })
await page.goto(url, { waitUntil: 'domcontentloaded' })
// Newer Checkout pages show the card form behind a "Card" accordion.
const cardTab = page.locator('[data-testid="card-accordion-item-button"]')
if (await cardTab.count().catch(() => 0)) {
await cardTab.click().catch(() => {})
}
await page.locator('#cardNumber').waitFor({ timeout: 60_000 })
await page.fill('#cardNumber', card.number)
await page.fill('#cardExpiry', '12/34')
await page.fill('#cardCvc', '123')
if (await page.locator('#billingName').count()) {
await page.fill('#billingName', 'Orca Eval')
}
if (await page.locator('#billingPostalCode').count()) {
await page.fill('#billingPostalCode', '94107')
}
const link = page.locator('#enableStripePass')
if ((await link.count()) && (await link.isChecked())) {
await link.uncheck({ force: true })
}
await page.screenshot({ path: shotPath.replace('.png', '-form.png') })
await page.locator('[data-testid="hosted-payment-submit-button"], .SubmitButton').first().click()
if (card.threeDS) {
// Stripe's test 3DS2 challenge: nested iframes with a "Complete" button.
const deadline = Date.now() + 60_000
let done = false
while (!done && Date.now() < deadline) {
for (const frame of page.frames()) {
if (frame === page.mainFrame()) {
continue
}
const button = frame.locator('#test-source-authorize-3ds').first()
if (await button.isVisible().catch(() => false)) {
await page.screenshot({ path: shotPath.replace('.png', '-3ds.png') })
done = await button
.click({ timeout: 5000 })
.then(() => true)
.catch(() => false)
break
}
}
if (!done) {
await page.waitForTimeout(1000)
}
}
if (!done) {
throw new Error('3DS challenge not found')
}
}
await page.waitForURL((current) => !current.hostname.endsWith('checkout.stripe.com'), { timeout: 90_000 })
await page.screenshot({ path: shotPath })
return { ms: Math.round(performance.now() - started), shot: shotPath, landed: page.url().replace(/session_id=[^&]+/, 'session_id=…') }
} finally {
await browser.close()
}
}
+35
View File
@@ -0,0 +1,35 @@
// Prints the latest status per catalog feature (non-pass only), leaked resources, and benchmark p50/p95.
// tsx src/summary.ts
import { CATALOG } from './catalog'
import { loadRuns, percentile } from './run-state'
const runs = loadRuns()
const latest = new Map<string, { status: string; detail: string; run: string }>()
const bench = new Map<string, number[]>()
for (const run of runs) {
for (const c of run.checks) {
latest.set(c.id, { status: c.status, detail: c.detail, run: run.runId })
}
for (const b of run.benchmarks) {
if (b.samplesMs.length) {
bench.set(b.name, b.samplesMs)
}
}
for (const r of run.resources.filter((x) => !x.destroyedAt)) {
console.log(`LEAK ${run.runId} ${r.kind} ${r.id}`)
}
}
for (const f of CATALOG) {
const l = latest.get(f.id)
if (!l || l.status !== 'pass') {
console.log(`${(l?.status ?? 'not_landed').padEnd(10)} ${f.id.padEnd(22)} ${f.owner.padEnd(18)} ${(l?.detail ?? '').slice(0, 110)}`)
}
}
for (const [name, s] of bench) {
console.log(`bench ${name.padEnd(26)} n=${s.length} p50=${percentile(s, 50)} p95=${percentile(s, 95)}`)
}
for (const run of runs.slice(-6)) {
for (const n of run.notes.filter((x) => x.startsWith('load') || x.includes('spend'))) {
console.log(`note ${run.runId}: ${n}`)
}
}
+51
View File
@@ -0,0 +1,51 @@
// Finds and removes anything an eval run left behind: ledger sandboxes not marked destroyed (deleted at
// Vercel with their snapshots) and Stripe test customers. Only ids from eval ledgers are touched.
// tsx src/sweep.ts [--dry-run]
import { writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { control, vercel } from './clients'
import { config } from './env'
import { loadRuns } from './run-state'
import { stripe } from './stripe'
const dry = process.argv.includes('--dry-run')
const api = vercel()
for (const run of loadRuns()) {
let changed = false
for (const r of run.resources.filter((x) => !x.destroyedAt)) {
if (r.kind === 'sandbox') {
const name = r.id.startsWith('orca-') ? r.id : `orca-dev-${r.id}`
const live = await api.getSandbox(name, { resume: false }).catch(() => null)
const snaps = (await api.listSnapshots(name).catch(() => [])).filter((s) => s.status === 'created')
console.log(`${run.runId} sandbox ${name}: ${live ? live.session.status : 'gone'}, ${snaps.length} snapshots`)
if (!dry) {
await api.deleteSandbox(name)
for (const s of snaps) {
await api.deleteSnapshot(s.id)
}
r.destroyedAt = new Date().toISOString()
r.label = `${r.label ?? ''} [swept]`
changed = true
}
} else if (r.kind === 'other' && r.id.startsWith('key_')) {
console.log(`${run.runId} api key ${r.id}`)
if (!dry) {
const d = await control.as({ sub: 'w9-eval' }, 'DELETE', `/v1/api-keys/${r.id}`)
if (d.status < 300 || d.status === 404) {
r.destroyedAt = new Date().toISOString()
changed = true
}
}
} else if (r.kind === 'stripe_customer') {
console.log(`${run.runId} stripe customer ${r.id}`)
if (!dry) {
await stripe('DELETE', `/v1/customers/${r.id}`).catch(() => {})
r.destroyedAt = new Date().toISOString()
changed = true
}
}
}
if (changed) {
writeFileSync(join(config.runsDir, `${run.runId}.json`), JSON.stringify(run, null, 2))
}
}
+38
View File
@@ -0,0 +1,38 @@
// Dev-signed tokens: the dev Workers accept an operator key set under their own issuers, so the eval
// runs before Orca Cloud auth mints sandbox/billing tokens. Dev sandbox accounts land in `dev:*`.
import { SignJWT, importJWK, type CryptoKey } from 'jose'
import { secrets } from './env'
export type Identity = { sub: string; org?: string; orgRole?: 'owner' | 'admin' | 'member'; email?: string }
let key: Promise<CryptoKey | Uint8Array> | null = null
function signingKey() {
const jwk = secrets.devJwk()
if (!jwk) {
throw new Error('missing dev-jwk.json in the secrets dir')
}
key ??= importJWK(jwk.private, 'ES256')
return key
}
function kid(): string {
const jwk = secrets.devJwk() as { kid?: string; private: { kid?: string } } | null
return jwk?.kid ?? jwk?.private.kid ?? 'dev1'
}
async function sign(claims: Record<string, unknown>, issuer: string, audience: string): Promise<string> {
return new SignJWT(claims)
.setProtectedHeader({ alg: 'ES256', kid: kid() })
.setIssuer(issuer)
.setAudience(audience)
.setIssuedAt()
.setExpirationTime('5m')
.sign(await signingKey())
}
export function sandboxToken(who: Identity): Promise<string> {
return sign({ ...who, purpose: 'sandbox' }, 'orca-sandbox-dev-test', 'orca-sandbox')
}
export function billingToken(who: Identity): Promise<string> {
return sign({ ...who, purpose: 'billing' }, 'orca-billing-dev-test', 'orca-billing')
}
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { CATALOG } from '../src/catalog'
import { pairingKey } from '../src/checks/core'
import { percentile } from '../src/run-state'
import { resourceCost } from '../src/report'
describe('percentile (nearest rank)', () => {
it('handles empty, odd and even sample counts', () => {
expect(percentile([], 50)).toBeNull()
expect(percentile([5, 1, 3], 50)).toBe(3)
expect(percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 50)).toBe(5)
expect(percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 95)).toBe(10)
})
})
describe('resourceCost', () => {
it('bills sandboxes by vCPU x running time and ignores other kinds', () => {
const hour = 3_600_000
const cost = resourceCost([
{ kind: 'sandbox', id: 'a', vcpus: 4, runningMs: hour, createdAt: '2026-11-09T00:00:00Z', destroyedAt: '2026-11-09T01:00:00Z' },
{ kind: 'stripe_customer', id: 'cus_1', createdAt: '2026-11-09T00:00:00Z' }
])
expect(cost.vcpuHours).toBe(4)
expect(cost.usd).toBeCloseTo(4 * (0.128 + 2 * 0.0106), 6)
})
})
describe('pairingKey', () => {
it('reads the host public key from an orca://pair code and tolerates garbage', () => {
const payload = Buffer.from(JSON.stringify({ v: 2, publicKeyB64: 'abc' })).toString('base64url')
expect(pairingKey(`orca://pair?code=${payload}`)).toBe('abc')
expect(pairingKey('not a url')).toBeNull()
})
})
describe('catalog', () => {
it('has unique ids', () => {
expect(new Set(CATALOG.map((f) => f.id)).size).toBe(CATALOG.length)
})
})
+15
View File
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "bundler",
"lib": ["es2022", "dom"],
"types": ["@types/node"],
"strict": true,
"noUncheckedIndexedAccess": true,
"allowImportingTsExtensions": true,
"noEmit": true,
"skipLibCheck": true
},
"include": ["src", "test"]
}
+7
View File
@@ -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'] }
})
@@ -0,0 +1,17 @@
-- Idempotency-Key for public API creates (POST /v1/workspaces): a retried request with the same key
-- gets the first request's response instead of booting a second sandbox. Kept 24h. Additive.
CREATE TABLE api_idempotency (
account_id TEXT NOT NULL,
scope TEXT NOT NULL,
idem_key TEXT NOT NULL,
-- SHA-256 of the canonical request body: the same key with a different body is refused.
request_hash TEXT NOT NULL,
-- pending (first request still running) | done
state TEXT NOT NULL,
status INTEGER,
response TEXT,
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
PRIMARY KEY (account_id, scope, idem_key)
);
CREATE INDEX api_idempotency_expiry ON api_idempotency (expires_at);
+2 -2
View File
@@ -64,7 +64,7 @@ export async function listAudit(
return results
}
export function auditView(row: AuditRow) {
export function auditView(row: AuditRow, names: ReadonlyMap<string, string> = new Map()) {
let detail: unknown = null
try {
detail = row.detail ? JSON.parse(row.detail) : null
@@ -73,7 +73,7 @@ export function auditView(row: AuditRow) {
}
return {
id: row.id,
actor: { type: row.actor_type, id: row.actor_id, userId: row.user_id },
actor: { type: row.actor_type, id: row.actor_id, userId: row.user_id, userName: row.user_id ? (names.get(row.user_id) ?? null) : null },
action: row.action,
target: row.target_type ? { type: row.target_type, id: row.target_id } : null,
detail,
@@ -0,0 +1,109 @@
// Idempotency-Key (Stripe-style) for public API creates. The first request with a key claims it
// (insert-or-nothing), runs, and stores its response; a retry with the same key and body replays that
// response with `idempotent-replayed: true`. Same key, different body: 422. Retry while the first is
// still running: 409 with Retry-After. Failures that left nothing behind (4xx before any work, 5xx)
// release the key so the client can retry for real. Keys live 24h.
import { ApiError } from './caller'
import { sha256Hex } from './keys'
export const IDEMPOTENCY_TTL_MS = 24 * 60 * 60_000
/** A pending claim older than this belongs to a request that died; it may be taken over. */
const PENDING_STALE_MS = 3 * 60_000
const KEY = /^[A-Za-z0-9_.:\-]{8,128}$/
type Row = { request_hash: string; state: 'pending' | 'done'; status: number | null; response: string | null; created_at: number }
export function readIdempotencyKey(request: Request): string | null {
const value = request.headers.get('idempotency-key')
if (value === null) {
return null
}
if (!KEY.test(value)) {
throw new ApiError(400, 'invalid_request', 'idempotency_key: 8-128 chars of [A-Za-z0-9_.:-]')
}
return value
}
function canonical(value: unknown): string {
if (Array.isArray(value)) {
return `[${value.map(canonical).join(',')}]`
}
if (value && typeof value === 'object') {
return `{${Object.keys(value)
.sort()
.map((k) => `${JSON.stringify(k)}:${canonical((value as Record<string, unknown>)[k])}`)
.join(',')}}`
}
return JSON.stringify(value ?? null)
}
/**
* Runs `work` at most once per (account, scope, key). `work` returns the JSON body and status to
* store and replay. Errors thrown by `work` release the key (nothing was created) and propagate.
*/
export async function withIdempotency(
db: D1Database,
input: { accountId: string; scope: string; key: string | null; body: unknown; now: number },
work: () => Promise<{ status: number; body: unknown }>
): Promise<Response> {
const respond = (status: number, body: unknown, replayed: boolean) =>
Response.json(body, { status, headers: { 'cache-control': 'no-store', ...(replayed ? { 'idempotent-replayed': 'true' } : {}) } })
if (!input.key) {
const done = await work()
return respond(done.status, done.body, false)
}
const hash = await sha256Hex(canonical(input.body))
const { accountId, scope, key, now } = input
await db.prepare('DELETE FROM api_idempotency WHERE account_id = ? AND scope = ? AND idem_key = ? AND expires_at <= ?').bind(accountId, scope, key, now).run()
const claimed = await db
.prepare(
`INSERT INTO api_idempotency (account_id, scope, idem_key, request_hash, state, created_at, expires_at)
VALUES (?, ?, ?, ?, 'pending', ?, ?) ON CONFLICT DO NOTHING`
)
.bind(accountId, scope, key, hash, now, now + IDEMPOTENCY_TTL_MS)
.run()
if (claimed.meta.changes !== 1) {
const row = await db
.prepare('SELECT request_hash, state, status, response, created_at FROM api_idempotency WHERE account_id = ? AND scope = ? AND idem_key = ?')
.bind(accountId, scope, key)
.first<Row>()
if (!row) {
throw new ApiError(409, 'conflict', 'idempotency_in_progress')
}
if (row.request_hash !== hash) {
throw new ApiError(422, 'idempotency_key_reused', 'same key, different request')
}
if (row.state === 'done' && row.status !== null && row.response !== null) {
return respond(row.status, JSON.parse(row.response), true)
}
if (now - row.created_at < PENDING_STALE_MS) {
throw new ApiError(409, 'conflict', 'idempotency_in_progress')
}
// The first attempt died mid-way: take the claim over (compare-and-set on its timestamp).
const taken = await db
.prepare(`UPDATE api_idempotency SET created_at = ? WHERE account_id = ? AND scope = ? AND idem_key = ? AND state = 'pending' AND created_at = ?`)
.bind(now, accountId, scope, key, row.created_at)
.run()
if (taken.meta.changes !== 1) {
throw new ApiError(409, 'conflict', 'idempotency_in_progress')
}
}
let done: { status: number; body: unknown }
try {
done = await work()
} catch (error) {
await db.prepare(`DELETE FROM api_idempotency WHERE account_id = ? AND scope = ? AND idem_key = ? AND state = 'pending'`).bind(accountId, scope, key).run()
throw error
}
await db
.prepare(`UPDATE api_idempotency SET state = 'done', status = ?, response = ? WHERE account_id = ? AND scope = ? AND idem_key = ?`)
.bind(done.status, JSON.stringify(done.body), accountId, scope, key)
.run()
return respond(done.status, done.body, false)
}
/** Cron: drop expired keys. */
export async function purgeIdempotency(db: D1Database, now: number): Promise<number> {
const result = await db.prepare('DELETE FROM api_idempotency WHERE expires_at <= ?').bind(now).run()
return result.meta.changes
}
+2 -1
View File
@@ -198,13 +198,14 @@ export async function revokeWorkspaceTokens(db: D1Database, sandboxId: string, n
.run()
}
export function apiKeyView(row: ApiKeyRow) {
export function apiKeyView(row: ApiKeyRow, names: ReadonlyMap<string, string> = new Map()) {
return {
id: row.id,
name: row.name,
prefix: row.prefix,
scopes: row.scopes.split(' ').filter(Boolean),
createdBy: row.created_by,
createdByName: names.get(row.created_by) ?? null,
createdAt: row.created_at,
lastUsedAt: row.last_used_at,
expiresAt: row.expires_at
@@ -0,0 +1,49 @@
// Keys and MCP grants act as the member who made them, capped by that member's role. The role is
// stored on the key (`creator_role`) and re-synced whenever it changes, so a demoted admin's keys
// and grants lose admin-only scopes at once (the cap is applied at every authentication).
// Two triggers: auth's internal call on a role change (POST /v1/internal/members/role-changed), and
// the member's own signed-in requests, which carry the live role (self-heal if a call was missed).
// Departures are handled by /v1/internal/members/removed (revokes keys, grants, routines).
import type { OrgRole } from '../auth'
import { audit } from './audit'
const SYNC_INTERVAL_MS = 60_000
/** Per-isolate throttle: at most one sync write per member per minute from request traffic. */
const lastSynced = new Map<string, { role: OrgRole; at: number }>()
/** Sets the cap on every live key and grant the member made in the account; returns rows changed. */
export async function syncMemberRole(db: D1Database, accountId: string, userId: string, role: OrgRole): Promise<number> {
// The stored cap changed out of band: the next signed-in request must compare again.
lastSynced.delete(`${accountId}\u0000${userId}`)
const result = await db
.prepare(
`UPDATE api_keys SET creator_role = ?
WHERE account_id = ? AND created_by = ? AND revoked_at IS NULL AND kind <> 'workspace'
AND (creator_role IS NULL OR creator_role <> ?)`
)
.bind(role, accountId, userId, role)
.run()
return result.meta.changes
}
/** From a signed-in org request: bring the member's keys in line with the role their token carries. */
export async function syncRoleFromSession(
db: D1Database,
caller: { accountId: string; userId: string; orgId?: string; orgRole?: OrgRole },
now: number
): Promise<void> {
if (!caller.orgId) {
return
}
const role: OrgRole = caller.orgRole ?? 'member'
const cacheKey = `${caller.accountId}\u0000${caller.userId}`
const seen = lastSynced.get(cacheKey)
if (seen && seen.role === role && now - seen.at < SYNC_INTERVAL_MS) {
return
}
const changed = await syncMemberRole(db, caller.accountId, caller.userId, role)
lastSynced.set(cacheKey, { role, at: now })
if (changed > 0) {
await audit(db, { accountId: caller.accountId, type: 'system', id: 'role-sync', userId: caller.userId }, { action: 'member.role_synced', detail: { role, keysUpdated: changed } }, now)
}
}
@@ -3,6 +3,7 @@
import { LifecycleError, resumeSandbox, type LifecycleDeps } from '../lifecycle'
import { getSandbox } from '../store'
import { withSenderEnvelope, parseSenderEnvelope } from '../../../../../src/shared/cloud-workspaces/sender-envelope'
import { memberNamesFor } from '../member-names'
import { randomId } from './keys'
import { launchAgent, sendToSession, VmAgentError, type AgentId } from './vm-agent'
import type { WorkspaceRow } from './workspaces'
@@ -31,11 +32,13 @@ const MAX_ATTEMPTS = 4
/** A delivery claimed this long ago without finishing is presumed dead and retried. */
const DELIVERY_STALE_MS = 3 * 60_000
export function messageView(row: MessageRow) {
export function messageView(row: MessageRow, names: ReadonlyMap<string, string> = new Map()) {
return {
id: row.id,
workspaceId: row.sandbox_id,
agent: row.agent,
sentBy: row.sent_by,
sentByName: names.get(row.sent_by) ?? null,
status: row.status,
// The session that received it: pass back as `session` to continue the same conversation.
session: row.result_handle ?? row.session_handle,
@@ -129,7 +132,10 @@ export async function deliverMessage(life: LifecycleDeps, message: MessageRow):
const text =
message.sent_by === row.user_id
? parseSenderEnvelope(message.body).body
: withSenderEnvelope(message.body, { userId: message.sent_by, name: message.sent_by })
: withSenderEnvelope(message.body, {
userId: message.sent_by,
name: (await memberNamesFor(life.db, message.account_id, [message.sent_by])).get(message.sent_by) ?? message.sent_by
})
let handle: string
if (message.session_handle) {
await sendToSession(life.provider, sessionId, message.session_handle, text)
@@ -6,7 +6,7 @@ type Op = {
summary: string
description?: string
scope?: string
params?: { name: string; in: 'path' | 'query'; description?: string; schema?: object; required?: boolean }[]
params?: { name: string; in: 'path' | 'query' | 'header'; description?: string; schema?: object; required?: boolean }[]
body?: object
responses: Record<string, string | { description: string; schema: object }>
tags: string[]
@@ -31,6 +31,7 @@ const schemas: Record<string, object> = {
prefix: { type: 'string', description: 'Non-secret start of the key, e.g. `orca_sk_Ab3x`' },
scopes: { type: 'array', items: { type: 'string', enum: [...API_SCOPES] } },
createdBy: { type: 'string' },
createdByName: { type: ['string', 'null'], description: 'Display name of the member, when known' },
createdAt: { type: 'integer', description: 'Unix ms' },
lastUsedAt: { type: ['integer', 'null'] },
expiresAt: { type: ['integer', 'null'] }
@@ -310,6 +311,7 @@ export const OPERATIONS: Record<string, Record<string, Op>> = {
tags: ['Workspaces'],
scope: 'workspaces:write',
summary: 'Create a workspace (starts a sandbox)',
params: [{ name: 'Idempotency-Key', in: 'header', description: 'Optional, 8-128 chars. A retry with the same key and body within 24 h returns the first response (header `idempotent-replayed: true`) instead of starting another sandbox; the same key with a different body answers 422.', schema: { type: 'string' } }],
description: 'Returns once the sandbox is running (typically a few seconds). Pass `message` to queue a first prompt for an agent.',
body: obj({
name: { type: 'string' },
+22 -5
View File
@@ -5,6 +5,9 @@ import { z } from 'zod'
import { GuardrailError, consumeCreateToken } from '../guardrails'
import type { LifecycleDeps } from '../lifecycle'
import { AGENTS } from './vm-agent'
import { memberNamesFor, rememberCallerName } from '../member-names'
import { syncRoleFromSession } from './member-role-sync'
import { readIdempotencyKey, withIdempotency } from './idempotency'
import { allowed, authorize, canSee, isOrgAdmin, isWorkspaceOwner } from './access'
import { ApiError, actorOf, authenticateApi, isAdmin, requireAdmin, requireScope, scopesForRole, type ApiCaller } from './caller'
import { auditCaller, auditView, consumeRate, listAudit, rateHeaders, RateLimitedError, type RateLimit } from './audit'
@@ -142,6 +145,11 @@ export async function handleApi(request: Request, url: URL, env: Parameters<type
if (!caller || caller.via === 'oauth') {
return json({ error: 'unauthorized' }, 401, { 'www-authenticate': caller ? 'Bearer error="invalid_token"' : 'Bearer' })
}
if (caller.via === 'session' || caller.via === 'device') {
// Labels for teammates, and the live role capping this member's keys and grants. Best effort.
await rememberCallerName(ctx.db, caller, ctx.now()).catch(() => undefined)
await syncRoleFromSession(ctx.db, caller, ctx.now()).catch(() => undefined)
}
return handleApiAs(caller, request, url, ctx)
}
@@ -217,7 +225,8 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont
}
if (!id && method === 'GET') {
const rows = await listApiKeys(ctx.db, caller.accountId, isAdmin(caller) ? {} : { createdBy: caller.userId })
return json({ keys: rows.map(apiKeyView), availableScopes: scopesForRole(caller) })
const names = await memberNamesFor(ctx.db, caller.accountId, rows.map((row) => row.created_by))
return json({ keys: rows.map((row) => apiKeyView(row, names)), availableScopes: scopesForRole(caller) })
}
if (!id && method === 'POST') {
const body = parse(CreateKeyBody, await ctx.readJson())
@@ -276,7 +285,8 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont
const limit = Math.min(200, Math.max(1, Number(url.searchParams.get('limit') ?? 50) || 50))
const before = Number(url.searchParams.get('before')) || undefined
const rows = await listAudit(ctx.db, caller.accountId, { before, limit })
return json({ entries: rows.map(auditView), nextBefore: rows.length === limit ? rows.at(-1)!.created_at : null })
const names = await memberNamesFor(ctx.db, caller.accountId, rows.flatMap((row) => (row.user_id ? [row.user_id] : [])))
return json({ entries: rows.map((row) => auditView(row, names)), nextBefore: rows.length === limit ? rows.at(-1)!.created_at : null })
}
if (resource === 'projects') {
@@ -359,7 +369,11 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont
if (!id && method === 'POST') {
requireScope(caller, 'workspaces:write')
const life = requireLife(ctx)
const body = parse(CreateWorkspaceBody, await ctx.readJson())
const idempotencyKey = readIdempotencyKey(request)
const raw = await ctx.readJson()
const body = parse(CreateWorkspaceBody, raw)
// The key is the caller's, scoped per member: a teammate's identical key never collides.
return withIdempotency(ctx.db, { accountId: caller.accountId, scope: `workspace.create:${caller.userId}`, key: idempotencyKey, body: raw, now }, async () => {
if (body.message) {
requireScope(caller, 'messages:write')
await consumeRate(ctx.db, messageBucket(caller), MESSAGES_PER_MINUTE, now)
@@ -385,7 +399,8 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont
ctx.waitUntil(deliverMessage(life, queued))
message = messageView(queued)
}
return json({ workspace: workspaceView(row), ...(message ? { message } : {}) }, 201)
return { status: 201, body: { workspace: workspaceView(row), ...(message ? { message } : {}) } }
})
}
if (id) {
const row = await getWorkspace(ctx.db, caller.accountId, id)
@@ -457,7 +472,9 @@ async function route(request: Request, url: URL, caller: ApiCaller, ctx: ApiCont
if (sub === 'messages' && !subId && method === 'GET') {
requireScope(caller, 'workspaces:read')
authorize(caller, row, 'open')
return json({ messages: (await listMessages(ctx.db, row.id, 50)).map(messageView) })
const messages = await listMessages(ctx.db, row.id, 50)
const names = await memberNamesFor(ctx.db, caller.accountId, messages.map((m) => m.sent_by))
return json({ messages: messages.map((m) => messageView(m, names)) })
}
if (sub === 'messages' && subId && method === 'GET') {
requireScope(caller, 'workspaces:read')
+2
View File
@@ -42,6 +42,8 @@ export type Env = {
BILLING_URL?: string
/** Service binding to the billing Worker; Workers on one account cannot reach each other over workers.dev. */
BILLING?: Fetcher
/** Durable Object that advances environment builds on alarms (EnvironmentBuildRunner in index.ts). */
ENV_BUILD_RUNNER?: DurableObjectNamespace
CONTROL_PLANE_TOKEN?: string
/** `stub` allows every start and records nothing: local dev only, refused when SANDBOX_ENV=prod. */
ENTITLEMENTS_MODE?: string
@@ -34,6 +34,8 @@ const SNAPSHOTTING_STUCK_MS = 3 * 60_000
const MIN_REDACTED_SECRET = 6
/** Event-stream keepalive; desktop watchers treat ~90 s of silence as a dead connection. */
const HEARTBEAT_MS = 20_000
/** How long one advance waits for a running command's exit before calling it still running. */
const EXIT_PROBE_MS = 4_000
/** Succeeded builds whose snapshot is kept (rollback window); older ones keep their record and log. */
export const KEPT_BUILDS = 5
const MAX_LOG_BYTES = 2 * 1024 * 1024
@@ -107,6 +109,11 @@ export type BuildDeps = {
githubFetch?: typeof fetch
/** Keeps finalization running after a watcher disconnects (Worker ctx.waitUntil). */
waitUntil?: (promise: Promise<unknown>) => void
/**
* Hands a started build to its server-side runner (a Durable Object alarm in the Worker), so it
* advances whether or not anyone is watching. Absent in tests that tick builds by hand.
*/
schedule?: (build: { id: string; environmentId: string }) => Promise<void>
config: { environment: string; goldenSnapshotId: string; region: string; guardrails?: GuardrailConfig }
}
@@ -250,7 +257,12 @@ export async function startBuild(deps: BuildDeps, caller: SandboxCaller, environ
const row = (await getBuild(deps.db, environment.id, id))!
log('environment_build_started', { buildId: id, environmentId: environment.id, number: row.number, repos: repos.length })
try {
return await bootBuild(deps, row, config)
const booted = await bootBuild(deps, row, config)
// Progress is server-side from here: a client that never opens the log stream still gets a build.
await deps.schedule?.({ id: booted.id, environmentId: booted.environment_id }).catch((error: unknown) =>
log('environment_build_schedule_failed', { buildId: booted.id, error: String(error).slice(0, 120) })
)
return booted
} catch (error) {
const current = (await getBuild(deps.db, environment.id, id)) ?? row
const code =
@@ -361,7 +373,14 @@ export async function advanceBuild(deps: BuildDeps, row: BuildRow, options: { wa
}
let exitCode: number | null
try {
exitCode = (await deps.provider.getCommand(row.session_id, row.command_id, { wait: options.waitForExit })).exitCode
// Always a (bounded) wait: the provider reports an exit code only to waiting readers, so a plain
// poll would keep a finished build "running" until a log stream happened to ask (W9 stall).
exitCode = (
await deps.provider.getCommand(row.session_id, row.command_id, {
wait: true,
waitMs: options.waitForExit ? 30_000 : EXIT_PROBE_MS
})
).exitCode
} catch (error) {
if (error instanceof ProviderError && (error.status === 404 || error.status === 410)) {
return (await failBuild(deps, row, 'install', 'vm_lost')) ?? row
@@ -740,6 +759,43 @@ export async function reportBuildUsage(deps: Pick<BuildDeps, 'db' | 'entitlement
return true
}
/** Runner cadence: how soon to look again at a build in this state; null once it is final. */
export function nextBuildTickMs(row: Pick<BuildRow, 'status'>): number | null {
switch (row.status) {
case 'starting':
case 'running':
return 3_000
case 'snapshotting':
return 10_000
default:
return null
}
}
/**
* One server-side step for a build (the runner's alarm): advance it and say when to look again.
* Independent of readers: the log stream and GET also advance, all through the same CAS steps.
*/
export async function tickBuild(deps: BuildDeps, environmentId: string, buildId: string): Promise<number | null> {
const row = await getBuild(deps.db, environmentId, buildId)
if (!row) {
return null
}
const next = await advanceBuild(deps, row)
if (TERMINAL.has(next.status) && next.usage_reported_at === null) {
await reportBuildUsage(deps, (await getBuild(deps.db, environmentId, buildId)) ?? next)
}
return nextBuildTickMs(next)
}
/** Unfinished builds (the cron re-arms their runners in case one was lost). */
export async function unfinishedBuilds(db: D1Database): Promise<{ id: string; environment_id: string }[]> {
const { results } = await db
.prepare(`SELECT id, environment_id FROM environment_builds WHERE status IN ('starting', 'running', 'snapshotting') LIMIT 50`)
.all<{ id: string; environment_id: string }>()
return results
}
/** Cron: advance every unfinished build (finalize the ones nobody is watching). */
export async function advanceAllBuilds(deps: BuildDeps): Promise<number> {
const { results } = await deps.db
@@ -22,7 +22,12 @@ export type GoldenImageProviderApi = {
params: { command: string; args?: string[]; env?: Record<string, string>; timeoutMs: number }
): Promise<string>
/** Null exit code while it runs; `wait` blocks (provider-side) until it ends. */
getCommand(sessionId: string, commandId: string, options?: { wait?: boolean }): Promise<{ exitCode: number | null }>
getCommand(
sessionId: string,
commandId: string,
/** `wait`: block until it exits (the only way the provider reports an exit code); `waitMs` caps that. */
options?: { wait?: boolean; waitMs?: number }
): Promise<{ exitCode: number | null }>
/**
* The command's output as provider ndjson (`{"stream":"stdout"|"stderr","data":"..."}` lines), from
* the first byte; follows live output until the command ends.
+81 -7
View File
@@ -98,7 +98,7 @@ import { SshKeyRequestSchema, updateSshKey } from './ssh-access'
import { audit } from './api/audit'
import { getSandbox, getSandboxByProviderName, listAccountSandboxes, type SandboxRow } from './store'
import { createVercelSandboxApi } from './vercel-sandbox-api'
import { advanceAllBuilds, type BuildDeps } from './environment-builds'
import { advanceAllBuilds, tickBuild, unfinishedBuilds, type BuildDeps } from './environment-builds'
import { environmentErrorResponse, handleEnvironmentRoute, isEnvironmentRoute } from './environment-routes'
import { EnvironmentError } from './environments'
import type { GoldenImageProviderApi } from './golden-image-provider'
@@ -178,6 +178,8 @@ export type Deps = {
retention?: RetentionConfig
/** Anonymous github.com reachability (tests stub it). */
publicRepoCheck?: (repoUrl: string) => Promise<boolean>
/** Build runner override (tests tick builds by hand). */
scheduleBuild?: BuildDeps['schedule']
/** ctx.waitUntil from the Worker entry; tests collect the promises. */
waitUntil?: (work: Promise<unknown>) => void
/** Route groups mounted on the public API (routines, see src/routines). */
@@ -525,8 +527,22 @@ async function lifecycleDeps(env: Env, deps: Deps): Promise<LifecycleDeps | null
}
}
function buildDeps(life: LifecycleDeps, deps: Deps): BuildDeps {
/** Arms the build's runner (one Durable Object per build); a no-op where the binding is absent. */
function buildScheduler(env: Env): BuildDeps['schedule'] {
const runners = env.ENV_BUILD_RUNNER
if (!runners) {
return undefined
}
return async (build) => {
const stub = runners.get(runners.idFromName(build.id))
const response = await stub.fetch('https://runner/start', { method: 'POST', body: JSON.stringify(build) })
await response.body?.cancel()
}
}
function buildDeps(life: LifecycleDeps, deps: Deps, env?: Env): BuildDeps {
return {
schedule: deps.scheduleBuild ?? (env ? buildScheduler(env) : undefined),
db: life.db,
// SAFETY: the Vercel client implements both; test fakes for builds do too.
provider: life.provider as BuildDeps['provider'],
@@ -980,7 +996,7 @@ export async function handleRequest(request: Request, env: Env, deps: Deps = {})
if (!life) {
return fail('not_configured', 503)
}
return await handleEnvironmentRoute(request, url, caller, buildDeps(life, deps), () => readJson(request))
return await handleEnvironmentRoute(request, url, caller, buildDeps(life, deps, env), () => readJson(request))
}
if (/^\/v1\/sandboxes\/[A-Za-z0-9_]+\/shares(?:\/|$)|^\/v1\/shares\//.test(url.pathname)) {
const caller = await authenticateDesktop(env, request, (deps.now ?? Date.now)())
@@ -1401,6 +1417,23 @@ export default {
}
})
)
// Environment builds: every minute and on their own, so a failing reconcile never stalls one.
// Advances what nobody watches and re-arms each build's runner in case one was lost.
ctx.waitUntil(
lifecycleDeps(env, {}).then(async (life) => {
if (!life) {
return
}
const deps = buildDeps(life, {}, env)
const builds = await advanceAllBuilds(deps).catch(() => 0)
if (builds > 0) {
console.log(JSON.stringify({ event: 'environment_builds_advanced', builds }))
}
for (const build of await unfinishedBuilds(env.SANDBOX_DB).catch(() => [])) {
await deps.schedule?.({ id: build.id, environmentId: build.environment_id }).catch(() => undefined)
}
})
)
if (new Date(controller.scheduledTime).getUTCMinutes() % 5 !== 0) {
return
}
@@ -1410,10 +1443,6 @@ export default {
// Counts only; no ids, tokens or pairing material.
const summary = await reconcile(life)
console.log(JSON.stringify({ event: 'reconcile', ...summary }))
const builds = await advanceAllBuilds(buildDeps(life, {})).catch(() => 0)
if (builds > 0) {
console.log(JSON.stringify({ event: 'environment_builds_advanced', builds }))
}
const retried = await retryPendingMessages(life).catch(() => -1)
if (retried !== 0) {
console.log(JSON.stringify({ event: 'message_retry', retried }))
@@ -1438,3 +1467,48 @@ export default {
)
}
} satisfies ExportedHandler<Env>
/**
* One per environment build: wakes on an alarm, advances the build one step (finalize, snapshot,
* smoke boot, billing all run here) and re-arms until the build is final. Builds therefore finish
* with no client watching, at most a few seconds after their VM command ends.
*/
export class EnvironmentBuildRunner implements DurableObject {
constructor(
private readonly state: DurableObjectState,
private readonly env: Env
) {}
async fetch(request: Request): Promise<Response> {
const body = (await request.json().catch(() => null)) as { id?: string; environmentId?: string } | null
if (!body?.id || !body.environmentId) {
return new Response('bad request', { status: 400 })
}
await this.state.storage.put('build', { id: body.id, environmentId: body.environmentId })
if ((await this.state.storage.getAlarm()) === null) {
await this.state.storage.setAlarm(Date.now() + 2_000)
}
return new Response(null, { status: 204 })
}
async alarm(): Promise<void> {
const build = await this.state.storage.get<{ id: string; environmentId: string }>('build')
const life = build ? await lifecycleDeps(this.env, {}) : null
if (!build || !life) {
await this.state.storage.deleteAll()
return
}
let next: number | null
try {
next = await tickBuild(buildDeps(life, {}, this.env), build.environmentId, build.id)
} catch (error) {
console.log(JSON.stringify({ event: 'environment_build_tick_failed', buildId: build.id, error: String(error).slice(0, 200) }))
next = 15_000
}
if (next === null) {
await this.state.storage.deleteAll()
return
}
await this.state.storage.setAlarm(Date.now() + next)
}
}
+3 -1
View File
@@ -1,5 +1,6 @@
// Hosted MCP server: routing for /mcp, the OAuth endpoints and their discovery documents, plus the
// /v1/oauth-approvals API route the desktop uses to approve an MCP client.
import { memberNamesFor } from '../member-names'
import { z } from 'zod'
import { auditCaller } from '../api/audit'
import { ApiError, isAdmin, scopesForRole, type ApiCaller } from '../api/caller'
@@ -194,7 +195,8 @@ export const oauthGrantsApi: ApiExtension = async (request, url, caller, ctx) =>
const familyId = match[1]
if (!familyId && request.method === 'GET') {
const rows = await listGrants(ctx.db, caller.accountId, now, isAdmin(caller) ? undefined : caller.userId)
return Response.json({ grants: rows.map(grantView) }, { headers: { 'cache-control': 'no-store' } })
const names = await memberNamesFor(ctx.db, caller.accountId, rows.map((row) => row.created_by))
return Response.json({ grants: rows.map((row) => grantView(row, names)) }, { headers: { 'cache-control': 'no-store' } })
}
if (familyId && request.method === 'DELETE') {
const owner = await grantOwner(ctx.db, caller.accountId, familyId)
+2 -1
View File
@@ -685,13 +685,14 @@ function redirectHosts(raw: string | null): string[] {
}
}
export function grantView(row: GrantRow) {
export function grantView(row: GrantRow, names: ReadonlyMap<string, string> = new Map()) {
return {
id: row.family_id,
clientId: row.client_id,
clientName: row.client_name,
redirectHosts: redirectHosts(row.redirect_uris),
userId: row.created_by,
userName: names.get(row.created_by) ?? null,
scopes: row.scopes.split(' ').filter(Boolean),
approvedWith: row.via_key ? { apiKeyPrefix: row.via_key } : { approvalCode: true },
grantedAt: row.granted_at,
@@ -5,9 +5,11 @@
// 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?}
// POST /v1/internal/members/role-changed {accountId, userId, role} re-cap their keys and MCP grants (auth calls this)
// POST /v1/internal/members/removed {accountId, userId} revoke a departed member's API keys, disable their routines (auth calls this)
import { z } from 'zod'
import { audit } from './api/audit'
import { syncMemberRole } from './api/member-role-sync'
import { readBearer } from './auth'
import type { GitHubAppMinter } from './github-app'
import { bindInstallation, revokeInstallation, timingSafeEqual } from './github-connect'
@@ -29,6 +31,7 @@ const BindingBody = z
.strict()
.refine((body) => (body.installationId === undefined) !== (body.repoUrl === undefined))
const MemberRemovedBody = z.object({ accountId: AccountId, userId: z.string().min(1).max(128) }).strict()
const RoleChangedBody = z.object({ accountId: AccountId, userId: z.string().min(1).max(128), role: z.enum(['owner', 'admin', 'member']) }).strict()
const UnbindBody = z.object({ installationId: z.number().int().positive(), accountId: AccountId.optional() }).strict()
async function digest(value: string): Promise<Uint8Array> {
@@ -115,6 +118,15 @@ export async function handleOperator(
console.log(JSON.stringify({ event: 'github_unbind_operator', installationId: body.data.installationId, revoked }))
return Response.json({ revoked })
}
case 'POST /v1/internal/members/role-changed': {
const body = RoleChangedBody.safeParse(await deps.readJson())
if (!body.success) {
return Response.json({ error: 'invalid_request' }, { status: 400 })
}
const updated = await syncMemberRole(life.db, body.data.accountId, body.data.userId, body.data.role)
await audit(life.db, { accountId: body.data.accountId, type: 'system', id: 'auth', userId: body.data.userId }, { action: 'member.role_changed', detail: { role: body.data.role, keysUpdated: updated } }, now)
return Response.json({ updated })
}
case 'POST /v1/internal/members/removed': {
const body = MemberRemovedBody.safeParse(await deps.readJson())
if (!body.success) {
@@ -10,6 +10,7 @@
// POST /v1/routines/:id/rotate-webhook -> {url}
// GET /v1/routines/:id/runs?limit=&before= -> {runs, nextBefore}
// GET /v1/routines/:id/runs/:runId -> {run}
import { memberNamesFor } from '../member-names'
import { z } from 'zod'
import { auditCaller, consumeRate } from '../api/audit'
import { ApiError, isAdmin, requireScope, type ApiCaller } from '../api/caller'
@@ -217,7 +218,9 @@ export function createRoutinesApi(options: { publicUrl: string; deps: () => Rout
if (!id && method === 'GET') {
requireScope(caller, 'routines:read')
return json({ routines: (await listRoutines(ctx.db, caller.accountId)).map(routineView) })
const routines = await listRoutines(ctx.db, caller.accountId)
const names = await memberNamesFor(ctx.db, caller.accountId, routines.map((r) => r.created_by))
return json({ routines: routines.map((r) => routineView(r, names)) })
}
if (!id && method === 'POST') {
@@ -103,7 +103,7 @@ export function triggerConfig(row: Pick<RoutineRow, 'trigger_kind' | 'trigger_co
return { kind: row.trigger_kind, ...config } as TriggerConfig
}
export function routineView(row: RoutineRecord) {
export function routineView(row: RoutineRecord, names: ReadonlyMap<string, string> = new Map()) {
const trigger = triggerConfig(row)
return {
id: row.id,
@@ -120,6 +120,7 @@ export function routineView(row: RoutineRecord) {
nextRunAt: row.enabled === 1 && row.trigger_kind === 'schedule' ? row.next_run_at : null,
lastRun: row.last_run_id ? { id: row.last_run_id, status: row.last_run_status, finishedAt: row.last_run_finished_at } : null,
createdBy: row.created_by,
createdByName: names.get(row.created_by) ?? null,
createdAt: row.created_at,
updatedAt: row.updated_at
}
+5 -1
View File
@@ -281,7 +281,11 @@ export async function deleteSecret(db: D1Database, scope: SecretScope, ownerId:
return result.meta.changes === 1
}
/** Higher wins. Env: repo and personal beat org. Agent keys: org-shared beats repo beats personal. */
/**
* Higher wins. Variables (env): Repository > Personal > Shared. Agent keys (AGENT_KEY_NAMES, incl.
* provider base URLs/models and AWS_*): Shared (team) > Repository > Personal. The egress swap resolves the
* real key through this same function, so a placeholder always stands for the value plain injection would give.
*/
const RANK: Record<SecretKind, Record<SecretScope, number>> = {
env: { repo: 3, personal: 2, org: 1 },
agent_key: { org: 3, repo: 2, personal: 1 }
@@ -64,7 +64,7 @@ export function createVercelSandboxApi(
async function request(
method: string,
path: string,
options: { query?: Record<string, string | undefined>; body?: unknown } = {}
options: { query?: Record<string, string | undefined>; body?: unknown; signal?: AbortSignal } = {}
): Promise<Response> {
const url = new URL(`${base}${path}`)
url.searchParams.set('teamId', config.teamId)
@@ -80,7 +80,8 @@ export function createVercelSandboxApi(
'content-type': 'application/json'
},
body: options.body === undefined ? undefined : JSON.stringify(options.body),
redirect: 'manual'
redirect: 'manual',
signal: options.signal
})
if (!response.ok) {
const parsed = ErrorBody.safeParse(await response.json().catch(() => null))
@@ -280,11 +281,22 @@ export function createVercelSandboxApi(
},
async getCommand(sessionId, commandId, options) {
const response = await request(
'GET',
`/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/cmd/${encodeURIComponent(commandId)}`,
{ query: { wait: options?.wait ? 'true' : undefined } }
)
// Found live: a detached command's exitCode stays null on plain GETs even after it exited;
// only `wait=true` reports it. `waitMs` bounds that wait so a running command reads as null.
const signal = options?.wait && options.waitMs ? AbortSignal.timeout(options.waitMs) : undefined
let response: Response
try {
response = await request(
'GET',
`/v2/sandboxes/sessions/${encodeURIComponent(sessionId)}/cmd/${encodeURIComponent(commandId)}`,
{ query: { wait: options?.wait ? 'true' : undefined }, signal }
)
} catch (error) {
if (signal?.aborted) {
return { exitCode: null }
}
throw error
}
const body = await json(z.looseObject({ command: z.looseObject({ exitCode: z.number().nullable().optional() }) }), response)
return { exitCode: body.command.exitCode ?? null }
},
@@ -363,3 +363,71 @@ describe('workspace tool hosts (synced remote MCP servers)', () => {
expect((await h.call('GET', `/v1/sandboxes/${id}/egress/hosts`, { bearer: mia })).body).toEqual({ hosts: ['mcp.linear.app', 'mcp.notion.com'] })
})
})
describe('precedence is the same for plain injection and the egress swap', () => {
// Agent keys: Shared (team) > Repository > Personal. Variables: Repository > Personal > Shared.
async function egressKey(placeholders: 'on' | 'off', personal: boolean, team: boolean) {
const { privateKey, publicKey } = await generateKeyPair('RS256')
const jwks = { keys: [{ ...(await exportJWK(publicKey)), kid: 'v1', alg: 'RS256' }] }
const upstream: Headers[] = []
const { h, as, put, commandEnv } = await setup(
{ SECRETS_EGRESS_PLACEHOLDERS: placeholders, EGRESS_PROXY_URL: PROXY_URL },
{
egressKeysForIssuer: () => createLocalJWKSet(jwks),
credentials: { authorizationFor: async () => null },
egressFetch: async (_input, init) => {
upstream.push(new Headers(init?.headers))
return new Response('ok')
}
}
)
const ada = await as('ada', 'acme', 'admin')
const mia = await as('mia', 'acme', 'member')
if (team) {
await put('org', 'ANTHROPIC_API_KEY', ORG_KEY, ada)
await put('org', 'AWS_REGION', 'us-east-1', ada)
await put('org', 'APP_MODE', 'team', ada)
}
if (personal) {
await put('personal', 'ANTHROPIC_API_KEY', PERSONAL_KEY, mia)
await put('personal', 'AWS_REGION', 'eu-west-1', mia)
await put('personal', 'APP_MODE', 'mine', mia)
}
const created = await h.call('POST', '/v1/sandboxes', { body: { workspaceId: 'ws-1', machineSize: 'standard' }, bearer: mia })
const id: string = created.body.sandbox.id
const env = commandEnv(START_COMMAND)
if (placeholders === 'off') {
return { key: env.ANTHROPIC_API_KEY, region: env.AWS_REGION, mode: env.APP_MODE }
}
const oidc = await new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_name: `orca-test-${id}` })
.setProtectedHeader({ alg: 'RS256', kid: 'v1' })
.setIssuer('https://oidc.vercel.com/team_orca')
.setAudience(PROXY_URL)
.setIssuedAt()
.setExpirationTime('5m')
.sign(privateKey)
await h.send(
new Request(`${PROXY_URL}/v1/messages`, {
method: 'POST',
body: '{}',
headers: {
'vercel-forwarded-host': 'api.anthropic.com',
'vercel-forwarded-scheme': 'https',
'vercel-forwarded-port': '443',
'vercel-forwarded-path': '/v1/messages',
'vercel-sandbox-oidc-token': oidc,
'x-api-key': env.ANTHROPIC_API_KEY!
}
})
)
return { key: upstream.at(-1)?.get('x-api-key'), region: env.AWS_REGION, mode: env.APP_MODE }
}
it('a team key beats a personal key in both modes; a personal key is used when the team has none', async () => {
for (const mode of ['off', 'on'] as const) {
expect(await egressKey(mode, true, true)).toEqual({ key: ORG_KEY, region: 'us-east-1', mode: 'mine' })
expect((await egressKey(mode, true, false)).key).toBe(PERSONAL_KEY)
expect((await egressKey(mode, false, true)).key).toBe(ORG_KEY)
}
})
})
@@ -0,0 +1,104 @@
import { describe, expect, it } from 'vitest'
import { createHarness } from './harness'
const OP = 'o'.repeat(40)
async function harness() {
const pending: Promise<unknown>[] = []
const h = await createHarness({ PUBLIC_API_URL: 'https://api.test', OPERATOR_TOKEN: OP }, { waitUntil: (p) => void pending.push(p) })
return { ...h, settle: () => Promise.all(pending.splice(0)) }
}
describe('member role changes cap keys and MCP grants', () => {
it('drops admin-only scopes from a demoted admin key, via the auth route and via their own next request', async () => {
const h = await harness()
const asAdmin = await h.token('ada', 'acme', 'sandbox', { orgRole: 'admin' })
const { secret } = (await h.call('POST', '/v1/api-keys', { bearer: asAdmin, body: { name: 'audit', scopes: ['audit:read', 'workspaces:read'] } })).body
expect((await h.call('GET', '/v1/audit', { bearer: secret })).status).toBe(200)
// Auth tells us about the demotion.
const changed = await h.call('POST', '/v1/internal/members/role-changed', { bearer: OP, body: { accountId: 'org:acme', userId: 'ada', role: 'member' } })
expect(changed.body).toEqual({ updated: 1 })
expect((await h.call('GET', '/v1/audit', { bearer: secret })).status).toBe(403)
expect((await h.call('GET', '/v1/me', { bearer: secret })).body.scopes).toEqual(['workspaces:read'])
// Re-promoted: a signed-in request with the live role re-syncs it (self-heal without the auth call).
const promoted = await h.token('ada', 'acme', 'sandbox', { orgRole: 'admin' })
await h.call('GET', '/v1/me', { bearer: promoted })
expect((await h.call('GET', '/v1/audit', { bearer: secret })).status).toBe(200)
h.advance(61_000)
const demoted = await h.token('ada', 'acme', 'sandbox', { orgRole: 'member' })
await h.call('GET', '/v1/me', { bearer: demoted })
expect((await h.call('GET', '/v1/audit', { bearer: secret })).status).toBe(403)
})
})
describe('Idempotency-Key on workspace create', () => {
it('replays the first response for a retry, refuses a reused key with a different body, and scopes keys per member', async () => {
const h = await harness()
const send = async (body: unknown, key: string, as = 'alice') =>
h.send(new Request('https://sandbox.test/v1/workspaces', {
method: 'POST',
headers: { authorization: `Bearer ${await h.token(as)}`, 'content-type': 'application/json', 'idempotency-key': key },
body: JSON.stringify(body)
}))
const first = await send({ name: 'once' }, 'retry-key-0001')
expect(first.status).toBe(201)
const firstBody = (await first.json()) as { workspace: { id: string } }
const retry = await send({ name: 'once' }, 'retry-key-0001')
expect(retry.status).toBe(201)
expect(retry.headers.get('idempotent-replayed')).toBe('true')
expect(((await retry.json()) as { workspace: { id: string } }).workspace.id).toBe(firstBody.workspace.id)
expect(h.vercel.boxes.size).toBe(1)
expect((await send({ name: 'different' }, 'retry-key-0001')).status).toBe(422)
// Expires after 24h: the same key creates again.
h.advance(24 * 60 * 60_000 + 1)
expect((await send({ name: 'once' }, 'retry-key-0001')).headers.get('idempotent-replayed')).toBeNull()
expect((await send({ name: 'x' }, 'bad key!')).status).toBe(400)
})
it('releases the key when the create fails so a retry really runs', async () => {
const h = await harness()
h.vercel.failCreate = true
const req = async () =>
h.send(new Request('https://sandbox.test/v1/workspaces', {
method: 'POST',
headers: { authorization: `Bearer ${await h.token('alice')}`, 'content-type': 'application/json', 'idempotency-key': 'fail-then-ok' },
body: '{}'
}))
expect((await req()).status).toBeGreaterThanOrEqual(500)
h.vercel.failCreate = false
const ok = await req()
expect(ok.status).toBe(201)
expect(ok.headers.get('idempotent-replayed')).toBeNull()
})
})
describe('teammate names', () => {
it('labels keys, audit entries and messages with the member name from their token, and names the sender to the agent', async () => {
const h = await harness()
try {
h.raw.exec("ALTER TABLE sandboxes ADD COLUMN visibility TEXT NOT NULL DEFAULT 'private'")
} catch {
// present once W4's migration is in the test schema
}
const ada = await h.token('ada', 'acme', 'sandbox', { orgRole: 'owner', name: 'Ada Lovelace' })
const bob = await h.token('bob', 'acme', 'sandbox', { orgRole: 'member', name: 'Bob Byte' })
await h.call('POST', '/v1/api-keys', { bearer: bob, body: { name: 'bob ci', scopes: ['workspaces:read'] } })
const keys = (await h.call('GET', '/v1/api-keys', { bearer: ada })).body.keys
expect(keys[0]).toMatchObject({ createdBy: 'bob', createdByName: 'Bob Byte' })
const audit = (await h.call('GET', '/v1/audit', { bearer: ada })).body.entries
expect(audit[0].actor).toMatchObject({ userId: 'bob', userName: 'Bob Byte' })
const ws = (await h.call('POST', '/v1/workspaces', { bearer: ada, body: {} })).body.workspace
h.raw.prepare("UPDATE sandboxes SET visibility = 'team' WHERE id = ?").run(ws.id)
h.vercel.commandHandler = (params) =>
params.args?.includes('agent') ? { exitCode: 0, stdout: JSON.stringify({ ok: true, result: { outcome: { kind: 'terminal', handle: 'term_n1' } } }), stderr: '' } : undefined
await h.call('POST', `/v1/workspaces/${ws.id}/messages`, { bearer: bob, body: { text: 'hello' } })
await h.settle()
const launch = h.vercel.commands.filter((c) => c.args?.includes('agent')).at(-1)!
expect(launch.args!.at(-1)).toBe('--prompt=[orca-sender name="Bob Byte" id="bob"]\nhello')
const messages = (await h.call('GET', `/v1/workspaces/${ws.id}/messages`, { bearer: ada })).body.messages
expect(messages[0]).toMatchObject({ sentBy: 'bob', sentByName: 'Bob Byte' })
})
})
@@ -124,6 +124,46 @@ describe('org environments', () => {
expect(refused.body.reason).toBe('hours_exhausted')
})
it('finishes a build server-side with nobody streaming or polling it (regression: W9 poll-only stall)', async () => {
const scheduled: { id: string; environmentId: string }[] = []
const h = await createHarness({}, { scheduleBuild: async (build) => void scheduled.push(build) })
h.vercel.buildResult = RESULT
const admin = await h.token('alice', 'acme', 'sandbox', { orgRole: 'admin' })
const env = await h.call('POST', '/v1/environments', { bearer: admin, body: { name: 'Default', repos: [{ url: 'acme/web' }], installScript: 'echo hi' } })
const started = await h.call('POST', `/v1/environments/${env.body.environment.id}/builds`, { bearer: admin })
expect(started.body.build.status).toBe('running')
// The build is handed to its runner at start; no reader ever looks at it again.
expect(scheduled).toEqual([{ id: started.body.build.id, environmentId: env.body.environment.id }])
const { tickBuild, unfinishedBuilds } = await import('../src/environment-builds')
const deps = {
db: h.env.SANDBOX_DB,
provider: h.vercel,
entitlements: { checkStart: async () => h.billing.decision, reportUsage: async (s: never) => { h.billing.usage.push(s); return h.billing.usageAnswer }, listSuspensions: async () => [] },
now: h.now,
config: { environment: 'test', goldenSnapshotId: 'snap_golden', region: 'iad1' }
}
// While the VM command runs, the runner keeps a short cadence.
expect(await tickBuild(deps, env.body.environment.id, started.body.build.id)).toBe(3_000)
expect(await unfinishedBuilds(h.env.SANDBOX_DB)).toHaveLength(1)
const command = [...h.vercel.buildCommands.values()].at(-1)!
command.logs = log('::orca-step:: install\nhi\n')
command.exitCode = 0
h.advance(3_000)
// The next alarm finalizes it (snapshot, smoke boot, activation, billing) and stops.
expect(await tickBuild(deps, env.body.environment.id, started.body.build.id)).toBeNull()
const row = h.raw.prepare('SELECT status, usage_reported_at, log_bytes FROM environment_builds WHERE id = ?').get(started.body.build.id) as {
status: string
usage_reported_at: number | null
log_bytes: number
}
expect(row.status).toBe('succeeded')
expect(row.usage_reported_at).not.toBeNull()
expect(row.log_bytes).toBeGreaterThan(0)
expect(await unfinishedBuilds(h.env.SANDBOX_DB)).toEqual([])
const environment = (await h.call('GET', `/v1/environments/${env.body.environment.id}`, { bearer: admin })).body.environment
expect(environment.activeBuildId).toBe(started.body.build.id)
})
it('refuses a second concurrent build and can cancel one', async () => {
const { adminCall, envId } = await setup()
const started = await adminCall('POST', `/v1/environments/${envId}/builds`)
@@ -145,12 +145,13 @@ export function createFakeVercel(now: () => number): FakeVercel {
buildCommands.set(id, { sessionId, env: params.env ?? {}, args: params.args ?? [], exitCode: null, logs: '' })
return id
},
async getCommand(_sessionId, commandId) {
async getCommand(_sessionId, commandId, options) {
const command = buildCommands.get(commandId)
if (!command) {
throw new ProviderError(404, 'not_found')
}
return { exitCode: command.exitCode }
// As Vercel (found live): a detached command's exit code is reported only to `wait=true` reads.
return { exitCode: options?.wait ? command.exitCode : null }
},
async streamCommandLogs(_sessionId, commandId) {
const command = buildCommands.get(commandId)
@@ -4,6 +4,32 @@ import { createVercelSandboxApi } from '../src/vercel-sandbox-api'
type Seen = { method: string; url: URL; headers: Headers; body: any }
describe('command exit codes', () => {
it('asks with wait=true and treats a bounded wait that runs out as still running', async () => {
const seen: URL[] = []
const client = createVercelSandboxApi({
token: 'vercel_test_token',
teamId: 'team_orca',
projectId: 'prj_orca',
fetch: (input, init) => {
seen.push(new URL(String(input)))
// A running command: the provider holds the wait open until the caller gives up.
return new Promise<Response>((_, reject) =>
init?.signal?.addEventListener('abort', () => reject(init.signal!.reason))
)
}
})
expect(await client.getCommand('sess_1', 'cmd_1', { wait: true, waitMs: 20 })).toEqual({ exitCode: null })
expect(seen[0]!.searchParams.get('wait')).toBe('true')
})
it('reports the exit code a finished command answers the wait with', async () => {
const { client, seen } = api(() => Response.json({ command: { id: 'cmd_1', exitCode: 0 } }))
expect(await client.getCommand('sess_1', 'cmd_1', { wait: true, waitMs: 4000 })).toEqual({ exitCode: 0 })
expect(seen[0]!.url.searchParams.get('wait')).toBe('true')
})
})
function api(answer: (seen: Seen) => Response) {
const seen: Seen[] = []
const client = createVercelSandboxApi({
@@ -22,6 +22,9 @@
// Same-account Workers cannot call each other over workers.dev (error 1042); BILLING_URL names the origin.
"services": [{ "binding": "BILLING", "service": "orca-billing-dev" }],
// Every minute for routines (src/routines); the rest of the cron work runs on minute % 5 == 0.
// Environment builds advance on Durable Object alarms (EnvironmentBuildRunner), not on readers.
"durable_objects": { "bindings": [{ "name": "ENV_BUILD_RUNNER", "class_name": "EnvironmentBuildRunner" }] },
"migrations": [{ "tag": "env-build-runner-v1", "new_sqlite_classes": ["EnvironmentBuildRunner"] }],
"triggers": { "crons": ["* * * * *"] },
"vars": {
"SANDBOX_ENV": "dev",
@@ -17,6 +17,9 @@
}
],
// Usage settlement, lease/deadline enforcement and stuck-row reconciliation.
// Environment builds advance on Durable Object alarms (EnvironmentBuildRunner), not on readers.
"durable_objects": { "bindings": [{ "name": "ENV_BUILD_RUNNER", "class_name": "EnvironmentBuildRunner" }] },
"migrations": [{ "tag": "env-build-runner-v1", "new_sqlite_classes": ["EnvironmentBuildRunner"] }],
"triggers": { "crons": ["*/5 * * * *"] },
"vars": {
"SANDBOX_ENV": "local",
+32
View File
@@ -210,6 +210,31 @@ importers:
specifier: ^4.157.0
version: 4.157.0(@cloudflare/workers-types@5.20261104.1)(@types/node@24.19.2)
apps/eval:
dependencies:
jose:
specifier: ^6.2.13
version: 6.2.13
playwright-core:
specifier: ^1.56.1
version: 1.65.0
zod:
specifier: ^4.6.7
version: 4.6.7
devDependencies:
'@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))
apps/push:
dependencies:
'@hono/node-server':
@@ -1437,6 +1462,11 @@ packages:
resolution: {integrity: sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==}
engines: {node: '>=12'}
playwright-core@1.65.0:
resolution: {integrity: sha512-vNSKgH6YqtN7cdT/L334zo0a+YwkrpicyrHJlWWPIm/Q+Nn2SWInil01oT0mbu96U4GgoRtCYQD9CzJQgju+lA==}
engines: {node: '>=20'}
hasBin: true
postcss@8.5.30:
resolution: {integrity: sha512-zkd+e9S1AQHhmaBKye9zlFHWjOwVgqXl1q3ECYy8561K7qxR2cREf/STLskulBFd78k6AOFeA+ff1TtEzk9bAA==}
engines: {node: ^10 || ^12 || >=14}
@@ -2355,6 +2385,8 @@ snapshots:
picomatch@4.0.7: {}
playwright-core@1.65.0: {}
postcss@8.5.30:
dependencies:
nanoid: 3.3.20
@@ -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 acceptance',
include: ['cloud/apps/acceptance/test/**/*.test.ts'],
sources: ['cloud/apps/acceptance/vitest.config.ts'],
mirrors: ["include: ['test/**/*.test.ts']"]
},
{
name: 'cloud billing',
include: ['cloud/apps/billing/test/**/*.test.ts'],
+4
View File
@@ -226,6 +226,10 @@ export default function RootLayout() {
<Stack.Screen name="connection-log" options={{ headerShown: false }} />
<Stack.Screen name="about" options={{ headerShown: false }} />
<Stack.Screen name="h" options={{ headerShown: false }} />
<Stack.Screen name="cloud/[sandboxId]" options={{ headerShown: false }} />
<Stack.Screen name="cloud/new" options={{ headerShown: false }} />
<Stack.Screen name="cloud/access" options={{ headerShown: false }} />
<Stack.Screen name="cloud/w/[sandboxId]" options={{ headerShown: false }} />
<Stack.Screen
name="new-workspace"
options={{
+14
View File
@@ -0,0 +1,14 @@
import { useLocalSearchParams } from 'expo-router'
import { CloudWorkspaceScreen } from '../../src/cloud/CloudWorkspaceScreen'
export default function CloudWorkspaceRoute() {
const params = useLocalSearchParams<{ sandboxId?: string; autostart?: string }>()
const sandboxId = typeof params.sandboxId === 'string' ? params.sandboxId : ''
return (
<CloudWorkspaceScreen
key={sandboxId}
sandboxId={sandboxId}
autostart={params.autostart === '1'}
/>
)
}
+5
View File
@@ -0,0 +1,5 @@
import { CloudAccessScreen } from '../../src/cloud/CloudAccessScreen'
export default function CloudAccessRoute() {
return <CloudAccessScreen />
}
+5
View File
@@ -0,0 +1,5 @@
import { NewCloudWorkspaceScreen } from '../../src/cloud/NewCloudWorkspaceScreen'
export default function NewCloudWorkspaceRoute() {
return <NewCloudWorkspaceScreen />
}
+9
View File
@@ -0,0 +1,9 @@
import { useLocalSearchParams } from 'expo-router'
import { CloudWorkspaceScreen } from '../../../src/cloud/CloudWorkspaceScreen'
/** Team share links (orca://cloud/w/<sandboxId>, from the desktop's "Copy link") open here on a phone. */
export default function CloudWorkspaceLinkRoute() {
const params = useLocalSearchParams<{ sandboxId?: string }>()
const sandboxId = typeof params.sandboxId === 'string' ? params.sandboxId : ''
return <CloudWorkspaceScreen key={sandboxId} sandboxId={sandboxId} />
}
+1
View File
@@ -24,6 +24,7 @@
},
"dependencies": {
"@formatjs/intl-pluralrules": "6.3.16",
"@noble/ciphers": "1.3.0",
"@noble/hashes": "1.8.0",
"@orca/expo-two-way-audio": "file:./packages/expo-two-way-audio",
"@react-native-async-storage/async-storage": "^2.2.0",
+9
View File
@@ -194,6 +194,9 @@ importers:
'@formatjs/intl-pluralrules':
specifier: 6.3.16
version: 6.3.16
'@noble/ciphers':
specifier: 1.3.0
version: 1.3.0
'@noble/hashes':
specifier: 1.8.0
version: 1.8.0
@@ -2006,6 +2009,10 @@ packages:
'@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3':
resolution: {integrity: sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ==}
'@noble/ciphers@1.3.0':
resolution: {integrity: sha512-2I0gnIVPtfnMw9ee9h1dJG7tp81+8Ob3OJb3Mv37rx5L40/b0i7djjCVvGOVqc9AEIQyvyu1i6ypKdFw8R8gQw==}
engines: {node: ^14.21.3 || >=16}
'@noble/hashes@1.8.0':
resolution: {integrity: sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==}
engines: {node: ^14.21.3 || >=16}
@@ -9571,6 +9578,8 @@ snapshots:
'@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3':
optional: true
'@noble/ciphers@1.3.0': {}
'@noble/hashes@1.8.0': {}
'@orca/expo-two-way-audio@file:packages/expo-two-way-audio(expo@55.0.31)(react-native@0.83.10(patch_hash=9430d1c3d0071e5b8e2c9d98f9ba5594ad3bb27f4f6e8f3884ab1f03658fb6f0)(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.85.2(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8)':
+204
View File
@@ -0,0 +1,204 @@
import { useQuery } from '@tanstack/react-query'
import { useRouter } from 'expo-router'
import { Smartphone } from 'lucide-react-native'
import { useState } from 'react'
import { ConfirmModal } from '../components/ConfirmModal'
import { ListRow } from '../components/list/ListGroup'
import { getUiIntlLocale } from '../i18n/ui-intl-locale'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import {
SettingsEmptyRow,
SettingsNote,
SettingsScreen,
SettingsSection
} from '../settings/settings-screen-frame'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { listCloudDeviceSessions, revokeCloudDeviceSession } from './cloud-control-plane-access'
import {
forgetAllCloudAccess,
forgetRevokedCloudSession,
loadCloudControlPlane,
useCloudHostRemoval
} from './cloud-session'
import { useCloudEnrollment } from './use-cloud-workspaces'
function formatDay(ms: number | null | undefined): string {
return ms
? new Date(ms).toLocaleDateString(getUiIntlLocale(), { month: 'short', day: 'numeric' })
: ''
}
/** Which phones hold Orca Cloud access as this user; any of them can be cut off from here. */
export function CloudAccessScreen() {
useMobileColorScheme()
const t = useMobileTranslation()
const router = useRouter()
const removal = useCloudHostRemoval()
const enrollment = useCloudEnrollment()
// The target outlives `open` so the dialog keeps its title while it animates closed.
const [confirm, setTarget] = useState<{ id: string; label: string; self: boolean } | null>(null)
const [open, setOpen] = useState(false)
const setConfirm = (target: { id: string; label: string; self: boolean } | null) => {
if (target) {
setTarget(target)
}
setOpen(target !== null)
}
const [error, setError] = useState<string | null>(null)
const sessions = useQuery({
queryKey: ['orca-cloud', 'device-sessions', enrollment?.deviceSessionId ?? null],
enabled: Boolean(enrollment),
queryFn: async () => {
const plane = await loadCloudControlPlane()
if (!plane) {
return []
}
try {
return await listCloudDeviceSessions(plane)
} catch (failure) {
await forgetRevokedCloudSession(failure, removal)
throw failure
}
}
})
const revoke = async (target: { id: string; self: boolean }) => {
setConfirm(null)
setError(null)
const plane = await loadCloudControlPlane()
try {
if (plane) {
await revokeCloudDeviceSession(plane, target.id)
}
} catch (failure) {
// Already revoked is the outcome we wanted; anything else for another phone is shown.
if (!(await forgetRevokedCloudSession(failure, removal)) && !target.self) {
setError(t('cloud.access.revokeFailed', 'Couldn’t remove that phone. Try again.'))
return
}
}
if (!target.self) {
void sessions.refetch()
return
}
// This phone keeps nothing: every cloud pairing goes, then the session and key.
await forgetAllCloudAccess(removal)
router.back()
}
const others = (sessions.data ?? []).filter((session) => !session.current)
return (
<SettingsScreen
title={t('cloud.access.title', 'Orca Cloud access')}
onBack={() => router.back()}
overlays={
<ConfirmModal
visible={open}
title={
confirm?.self
? t('cloud.access.disconnectTitle', 'Disconnect this phone?')
: t('cloud.access.removeTitle', 'Remove {{device}}?', {
device: confirm?.label ?? ''
})
}
message={
confirm?.self
? t(
'cloud.access.disconnectMessage',
'This phone loses access to your cloud workspaces right away. They keep running.'
)
: t(
'cloud.access.removeMessage',
'That phone loses access to your cloud workspaces on its next request.'
)
}
confirmLabel={
confirm?.self
? t('cloud.access.disconnect', 'Disconnect')
: t('cloud.access.remove', 'Remove')
}
destructive
onConfirm={() => confirm && void revoke(confirm)}
onCancel={() => setConfirm(null)}
/>
}
>
<SettingsSection
first
title={t('cloud.access.thisPhone', 'This phone')}
footer={t(
'cloud.access.thisPhoneNote',
'It signs its own requests with a key that never leaves it. No password or token is stored.'
)}
>
{enrollment ? (
<ListRow
key="self"
leading={<Smartphone size={18} color={colors.textSecondary} />}
title={enrollment.label}
subtitle={
enrollment.viaHostName
? t('cloud.access.connectedVia', 'Connected {{date}} through {{desktop}}', {
date: formatDay(enrollment.enrolledAt),
desktop: enrollment.viaHostName
})
: t('cloud.access.since', 'Connected {{date}}', {
date: formatDay(enrollment.enrolledAt)
})
}
/>
) : (
<SettingsEmptyRow key="none">
{t('cloud.access.notConnected', 'Not connected to Orca Cloud.')}
</SettingsEmptyRow>
)}
</SettingsSection>
{enrollment ? (
<SettingsSection title={t('cloud.access.otherPhones', 'Other phones')}>
{others.length === 0 ? (
<SettingsEmptyRow key="empty">
{sessions.isPending
? t('common.loading', 'Loading…')
: t('cloud.access.noOthers', 'No other phones have access.')}
</SettingsEmptyRow>
) : (
others.map((session) => (
<ListRow
key={session.id}
leading={<Smartphone size={18} color={colors.textSecondary} />}
title={session.label}
subtitle={
session.lastUsedAt
? t('cloud.access.lastUsed', 'Last used {{date}}', {
date: formatDay(session.lastUsedAt)
})
: t('cloud.access.since', 'Connected {{date}}', {
date: formatDay(session.createdAt)
})
}
onPress={() => setConfirm({ id: session.id, label: session.label, self: false })}
accessibilityLabel={t('cloud.access.removeLabel', 'Remove {{device}}', {
device: session.label
})}
/>
))
)}
</SettingsSection>
) : null}
{enrollment ? (
<SettingsSection>
<ListRow
key="disconnect"
title={t('cloud.access.disconnectThis', 'Disconnect this phone')}
destructive
onPress={() =>
setConfirm({ id: enrollment.deviceSessionId, label: enrollment.label, self: true })
}
/>
</SettingsSection>
) : null}
{error ? <SettingsNote error>{error}</SettingsNote> : null}
</SettingsScreen>
)
}
+80
View File
@@ -0,0 +1,80 @@
import { Pressable, Text, View } from 'react-native'
import { formatAgentTypeLabel } from '../../../src/shared/agent-status/presentation/agent-type-label'
import { ClaudeIcon, OpenAIIcon } from '../components/AgentIcons'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors, radii, spacing } from '../theme/mobile-theme'
import { themedStyleSheet } from '../theme/themed-styles'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
/** The agent CLIs every Orca Cloud image ships. */
const CLOUD_AGENTS = [
{ id: 'claude', Icon: ClaudeIcon },
{ id: 'codex', Icon: OpenAIIcon }
] as const
export const CLOUD_DEFAULT_AGENT = 'claude'
/** Product names, from the app's one agent-name table. */
export function cloudAgentLabel(agent: string): string {
return formatAgentTypeLabel(agent)
}
export function CloudAgentPicker(props: {
value: string
onChange: (agent: string) => void
disabled?: boolean
}) {
useMobileColorScheme()
const t = useMobileTranslation()
return (
<View
style={styles.row}
accessibilityRole="radiogroup"
accessibilityLabel={t('cloud.agent.label', 'Agent')}
>
{CLOUD_AGENTS.map(({ id, Icon }) => {
const label = cloudAgentLabel(id)
const checked = props.value === id
return (
<Pressable
key={id}
accessibilityRole="radio"
accessibilityState={{ checked, disabled: props.disabled }}
disabled={props.disabled}
onPress={() => props.onChange(id)}
hitSlop={{ top: 4, bottom: 4 }}
style={({ pressed }) => [
styles.chip,
checked && styles.chipSelected,
props.disabled && styles.disabled,
pressed && styles.pressed
]}
>
<Icon size={15} />
<Text style={[styles.label, checked && styles.labelSelected]}>{label}</Text>
</Pressable>
)
})}
</View>
)
}
const styles = themedStyleSheet(() => ({
row: { flexDirection: 'row', gap: spacing.sm, flexWrap: 'wrap' },
chip: {
flexDirection: 'row',
alignItems: 'center',
gap: 6,
minHeight: 40,
paddingHorizontal: spacing.md,
borderRadius: radii.pill,
borderWidth: 1,
borderColor: colors.borderSubtle,
backgroundColor: colors.bgRaised
},
chipSelected: { borderColor: colors.textPrimary },
disabled: { opacity: 0.5 },
pressed: { opacity: 0.7 },
label: { fontSize: 14, color: colors.textSecondary },
labelSelected: { color: colors.textPrimary, fontWeight: '600' }
}))
@@ -0,0 +1,52 @@
import { CircleCheck } from 'lucide-react-native'
import { Pressable, Text, View } from 'react-native'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { cloudAgentLabel } from './CloudAgentPicker'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
/** After a prompt starts an agent on the sandbox: what happened, and where its sign-in lives.
* A cloud agent that isn't signed in yet waits in its terminal; the chat view alone would look
* empty and invite a resend. */
export function CloudAgentStartedCard(props: {
agent: string
onOpenTerminal: (() => void) | null
}) {
useMobileColorScheme()
const t = useMobileTranslation()
const agent = cloudAgentLabel(props.agent)
return (
<View style={styles.startedCard} accessibilityRole="summary">
<View style={styles.startedHeadline}>
<CircleCheck size={18} color={colors.textPrimary} strokeWidth={2} />
<Text style={styles.startedTitle}>
{t('cloud.started.title', '{{agent}} started with your prompt', { agent })}
</Text>
</View>
<Text style={styles.statusLine}>
{t(
'cloud.started.signIn',
'If {{agent}} isn’t signed in on this sandbox yet, it asks in its terminal. Your prompt is saved and runs once you sign in.',
{ agent }
)}
</Text>
{props.onOpenTerminal ? (
<Pressable
accessibilityRole="button"
onPress={props.onOpenTerminal}
hitSlop={8}
style={({ pressed }) => [
styles.errorRetry,
styles.startedAction,
pressed && styles.pressed
]}
>
<Text style={styles.errorRetryText}>
{t('cloud.started.openTerminal', 'Open terminal to sign in')}
</Text>
</Pressable>
) : null}
</View>
)
}
+17
View File
@@ -0,0 +1,17 @@
import { useEffect, useState } from 'react'
import { Text, type StyleProp, type TextStyle } from 'react-native'
import { formatElapsed } from './cloud-workspace-display'
/** A ticking "12s" leaf: only this re-renders each second, and screen readers skip it. */
export function CloudElapsed({ since, style }: { since: number; style?: StyleProp<TextStyle> }) {
const [now, setNow] = useState(() => Date.now())
useEffect(() => {
const timer = setInterval(() => setNow(Date.now()), 1_000)
return () => clearInterval(timer)
}, [])
return (
<Text style={style} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
{formatElapsed(now - since)}
</Text>
)
}
+28
View File
@@ -0,0 +1,28 @@
import { CircleAlert } from 'lucide-react-native'
import { Pressable, Text, View } from 'react-native'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
/** What went wrong, in one line, with the one action that fixes it. */
export function CloudErrorBanner(props: { message: string; onRetry: (() => void) | null }) {
useMobileColorScheme()
const t = useMobileTranslation()
return (
<View style={styles.errorBanner} accessibilityRole="alert">
<CircleAlert size={18} color={colors.statusRed} strokeWidth={2} />
<Text style={styles.errorText}>{props.message}</Text>
{props.onRetry ? (
<Pressable
accessibilityRole="button"
onPress={props.onRetry}
hitSlop={8}
style={({ pressed }) => [styles.errorRetry, pressed && styles.pressed]}
>
<Text style={styles.errorRetryText}>{t('common.tryAgain', 'Try again')}</Text>
</Pressable>
) : null}
</View>
)
}
+105
View File
@@ -0,0 +1,105 @@
import { Check } from 'lucide-react-native'
import { ActivityIndicator, Text, View } from 'react-native'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { CloudElapsed } from './CloudElapsed'
import { colors, radii, spacing } from '../theme/mobile-theme'
import { themedStyleSheet } from '../theme/themed-styles'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
export type CloudProgressPhase =
| 'creating'
| 'waking'
| 'granting'
| 'pairing'
| 'connected'
| 'delivering'
type Step = { id: CloudProgressPhase; label: string }
/** Honest progress: each real step the phone is waiting on, with the elapsed time on the live one. */
export function CloudPhaseSteps(props: {
phase: CloudProgressPhase
/** When the live step started; its elapsed time ticks on the live row only. */
since: number
/** Show the "start the agent" step (a prompt is waiting). */
delivering: boolean
/** Creating a new workspace replaces "wake" with "create and clone". */
creating?: boolean
}) {
useMobileColorScheme()
const t = useMobileTranslation()
const steps: Step[] = [
props.creating
? { id: 'creating', label: t('cloud.step.create', 'Start the sandbox and clone the repo') }
: { id: 'waking', label: t('cloud.step.wake', 'Wake the sandbox') },
{ id: 'granting', label: t('cloud.step.grant', 'Give this phone its own access') },
{ id: 'pairing', label: t('cloud.step.pair', 'Connect, end-to-end encrypted') },
...(props.delivering
? [
{
id: 'delivering' as const,
label: t('cloud.step.deliver', 'Start the agent with your prompt')
}
]
: [])
]
const order: CloudProgressPhase[] = steps.map((step) => step.id)
// `connected` counts as past the pairing step; a reused pairing skips granting entirely.
const currentIndex =
props.phase === 'connected' ? order.indexOf('pairing') + 1 : order.indexOf(props.phase)
return (
<View
style={styles.list}
accessibilityRole="progressbar"
accessibilityLabel={steps[currentIndex]?.label}
accessibilityValue={{
min: 0,
max: steps.length,
now: Math.min(currentIndex + 1, steps.length)
}}
>
{steps.map((step, index) => {
const done = index < currentIndex
const current = index === currentIndex
return (
<View key={step.id} style={styles.row}>
<View style={[styles.marker, done && styles.markerDone]}>
{done ? (
<Check size={12} color={colors.bgBase} strokeWidth={3} />
) : current ? (
<ActivityIndicator
size="small"
color={colors.textSecondary}
style={styles.spinner}
/>
) : null}
</View>
<Text style={[styles.label, (done || current) && styles.labelActive]} numberOfLines={2}>
{step.label}
</Text>
{current ? <CloudElapsed since={props.since} style={styles.elapsed} /> : null}
</View>
)
})}
</View>
)
}
const styles = themedStyleSheet(() => ({
list: { gap: spacing.sm, paddingTop: spacing.xs },
row: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, minHeight: 22 },
marker: {
width: 18,
height: 18,
borderRadius: radii.pill,
borderWidth: 1.5,
borderColor: colors.borderSubtle,
alignItems: 'center',
justifyContent: 'center'
},
markerDone: { backgroundColor: colors.statusGreen, borderColor: colors.statusGreen },
spinner: { transform: [{ scale: 0.7 }] },
label: { flex: 1, fontSize: 14, color: colors.textMuted },
labelActive: { color: colors.textPrimary },
elapsed: { fontSize: 13, color: colors.textSecondary, fontVariant: ['tabular-nums'] }
}))
+78
View File
@@ -0,0 +1,78 @@
import { ArrowUp } from 'lucide-react-native'
import { useState } from 'react'
import { Pressable, Text, TextInput, View } from 'react-native'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { CloudAgentPicker, CLOUD_DEFAULT_AGENT } from './CloudAgentPicker'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
/** One card: the agent, the prompt, and Send. The send button keeps its shape in every state; when
* asleep its label and the caption say that sending also wakes the sandbox. */
export function CloudPromptComposer(props: {
awake: boolean
/** Waking now: a prompt sent goes out as soon as it is awake. */
waking: boolean
/** Creating or archived: typing is fine, sending waits. */
disabled: boolean
onSend: (text: string, agent: string) => Promise<boolean>
}) {
useMobileColorScheme()
const t = useMobileTranslation()
const [draft, setDraft] = useState('')
const [agent, setAgent] = useState(CLOUD_DEFAULT_AGENT)
const canSend = draft.trim() !== '' && !props.disabled
const send = async () => {
if (canSend && (await props.onSend(draft.trim(), agent))) {
setDraft('')
}
}
const asleep = !props.awake && !props.waking
return (
<View>
<View style={styles.composerCard}>
<CloudAgentPicker value={agent} onChange={setAgent} />
<View style={styles.inputWrap}>
<TextInput
style={styles.input}
value={draft}
onChangeText={setDraft}
multiline
placeholder={t('cloud.prompt.placeholder', 'Describe the task')}
placeholderTextColor={colors.textMuted}
accessibilityLabel={t('cloud.prompt.label', 'Prompt')}
maxLength={20_000}
/>
<Pressable
accessibilityRole="button"
accessibilityLabel={
asleep ? t('cloud.prompt.wakeSend', 'Wake and send') : t('cloud.prompt.send', 'Send')
}
accessibilityState={{ disabled: !canSend }}
disabled={!canSend}
onPress={() => void send()}
hitSlop={6}
style={({ pressed }) => [
styles.sendButton,
!canSend && styles.sendDisabled,
pressed && styles.pressed
]}
>
<ArrowUp
size={18}
color={canSend ? colors.bgBase : colors.textMuted}
strokeWidth={2.4}
/>
</Pressable>
</View>
</View>
{props.awake ? null : (
<Text style={styles.caption}>
{props.waking
? t('cloud.prompt.wakingCaption', 'Sends when it’s awake.')
: t('cloud.prompt.wakeCaption', 'Sending wakes the sandbox first.')}
</Text>
)}
</View>
)
}
@@ -0,0 +1,95 @@
import { Lock, RotateCw, X } from 'lucide-react-native'
import { ActivityIndicator, Pressable, Text, View } from 'react-native'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { cloudAgentLabel } from './CloudAgentPicker'
import { removeCloudPrompt, retryCloudPrompt, type QueuedCloudPrompt } from './cloud-prompt-queue'
import { cloudPromptErrorMessage } from './cloud-workspace-copy'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
/** Prompts held on this phone until the workspace is awake; each can be retried or removed. */
export function CloudPromptQueueSection(props: {
queue: QueuedCloudPrompt[]
/** Awake prompts wait on this phone's connection, not on the workspace. */
awake: boolean
onRetry: () => void
}) {
useMobileColorScheme()
const t = useMobileTranslation()
if (props.queue.length === 0) {
return null
}
return (
<View style={styles.section}>
<Text style={styles.sectionHeading} accessibilityRole="header">
{t('cloud.prompt.queued', 'Waiting to send')}
</Text>
<View style={styles.card}>
{props.queue.map((prompt, index) => {
const failed = prompt.status === 'failed'
const state = failed
? t('cloud.prompt.notSent', 'Not sent: {{reason}}', {
reason: cloudPromptErrorMessage(prompt.error, t)
})
: prompt.status === 'delivering'
? t('cloud.prompt.delivering', 'Sending…')
: props.awake
? t('cloud.prompt.waitingConnect', 'Waiting to connect')
: t('cloud.prompt.waiting', 'Sends when the workspace wakes')
const retry = () => void retryCloudPrompt(prompt.id).then(props.onRetry)
const remove = () => void removeCloudPrompt(prompt.id)
return (
<View
key={prompt.id}
style={[styles.queueRow, index > 0 && styles.queueRowDivider]}
accessible
accessibilityLabel={`${cloudAgentLabel(prompt.agent)}: ${prompt.text}. ${state}`}
accessibilityActions={[
...(failed ? [{ name: 'retry', label: t('common.tryAgain', 'Try again') }] : []),
{ name: 'delete', label: t('cloud.prompt.remove', 'Remove') }
]}
onAccessibilityAction={(event) =>
event.nativeEvent.actionName === 'retry' ? retry() : remove()
}
>
<View style={styles.queueText}>
<Text style={styles.queuePrompt} numberOfLines={3}>
{prompt.text}
</Text>
<Text style={[styles.queueState, failed && styles.queueStateFailed]}>
{`${cloudAgentLabel(prompt.agent)} · ${state}`}
</Text>
</View>
{prompt.status === 'delivering' ? (
<ActivityIndicator size="small" color={colors.textMuted} />
) : (
<View style={styles.queueButtons} importantForAccessibility="no-hide-descendants">
{failed ? (
<Pressable onPress={retry} hitSlop={6} style={styles.iconButton}>
<RotateCw size={17} color={colors.accentBlue} />
</Pressable>
) : null}
<Pressable onPress={remove} hitSlop={6} style={styles.iconButton}>
<X size={18} color={colors.textMuted} />
</Pressable>
</View>
)}
</View>
)
})}
{props.awake ? null : (
<View style={[styles.queueRow, styles.queueRowDivider, styles.queueNote]}>
<Lock size={13} color={colors.textMuted} />
<Text style={styles.caption}>
{t(
'cloud.prompt.queuedNote',
'Kept on this phone until the workspace is awake, then sent end-to-end encrypted.'
)}
</Text>
</View>
)}
</View>
</View>
)
}
+75
View File
@@ -0,0 +1,75 @@
import { Text, View } from 'react-native'
import type { Translate } from '../i18n/mobile-i18n'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { CloudPhaseSteps } from './CloudPhaseSteps'
import { CloudStatusDot } from './CloudStatusDot'
import type { CloudWorkspace } from './cloud-control-plane-client'
import { cloudStatusLabel, formatCloudCost } from './cloud-workspace-display'
import { statusSentence } from './cloud-workspace-copy'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
import type { CloudWorkspaceRun } from './use-cloud-workspace-run'
/** Spend once, honestly: what it cost so far, and whether it is using sandbox hours now. Plans
* include hours, so no per-hour dollar figure is quoted as if it were the meter. */
function spendLine(
workspace: CloudWorkspace,
usage: { costCents: number } | null,
awake: boolean,
t: Translate
): string | null {
const usd = usage ? usage.costCents / 100 : (workspace.cost?.usd ?? null)
if (usd === null) {
return null
}
const cost = formatCloudCost(usd)
if (!awake) {
return t('cloud.spend.asleep', '{{cost}} so far · no sandbox hours while asleep', { cost })
}
return workspace.machineSize === 'large'
? t('cloud.spend.runningLarge', '{{cost}} so far · uses sandbox hours at 2× the rate', { cost })
: t('cloud.spend.running', '{{cost}} so far · uses sandbox hours while running', { cost })
}
export function CloudStatusCard(props: {
workspace: CloudWorkspace
run: CloudWorkspaceRun
usage: { costCents: number } | null
uptimeMs: number | null
/** Shows the "start the agent" step only when a prompt is actually waiting. */
promptsWaiting: boolean
}) {
useMobileColorScheme()
const t = useMobileTranslation()
const { workspace, run } = props
const active = run.kind === 'attaching' || run.kind === 'delivering'
// Up but unreachable from this phone: a filled amber dot, as a desktop that can't connect.
const notConnected = run.kind === 'error' && workspace.status === 'running'
const awake = active || workspace.status === 'running' || workspace.status === 'creating'
const spend = spendLine(workspace, props.usage, awake, t)
return (
<View style={styles.statusCard}>
<View style={styles.statusHeadline}>
<CloudStatusDot status={active ? 'waking' : workspace.status} warn={notConnected} />
<Text style={styles.statusLabel} accessibilityRole="header">
{active
? t('cloud.status.waking', 'Waking')
: notConnected
? t('cloud.status.cantConnect', 'Can’t connect')
: cloudStatusLabel(workspace.status, t)}
</Text>
</View>
{active ? (
<CloudPhaseSteps
phase={run.kind === 'attaching' ? run.phase : 'delivering'}
since={run.startedAt}
delivering={run.kind === 'delivering' || props.promptsWaiting}
/>
) : notConnected ? null : (
// Not connected: the error banner right below says it, once.
<Text style={styles.statusLine}>{statusSentence(workspace, t, props.uptimeMs)}</Text>
)}
{spend ? <Text style={styles.statusSpend}>{spend}</Text> : null}
</View>
)
}
+84
View File
@@ -0,0 +1,84 @@
import { useEffect, useRef } from 'react'
import { Animated, Easing, View } from 'react-native'
import { useReducedMotionEnabled } from '../platform/use-reduced-motion'
import { colors, radii } from '../theme/mobile-theme'
import type { CloudWorkspaceStatus } from './cloud-control-plane-client'
import { cloudStatusColor, isCloudStatusTransitional } from './cloud-workspace-display'
/** A status dot that breathes while the workspace changes state (still under Reduce Motion). */
export function CloudStatusDot({
status,
size = 10,
warn = false
}: {
status: CloudWorkspaceStatus
size?: number
/** Filled amber, still: running but unreachable (matches a desktop that can't connect). */
warn?: boolean
}) {
const reducedMotion = useReducedMotionEnabled() !== false
const pulse = useRef(new Animated.Value(1)).current
const moving = isCloudStatusTransitional(status) && !reducedMotion && !warn
useEffect(() => {
if (!moving) {
// Settle to full opacity: a loop stopped mid-breath must not leave a dim dot behind.
Animated.timing(pulse, { toValue: 1, duration: 150, useNativeDriver: true }).start()
return
}
const loop = Animated.loop(
Animated.sequence([
Animated.timing(pulse, {
toValue: 0.35,
duration: 700,
easing: Easing.inOut(Easing.quad),
useNativeDriver: true
}),
Animated.timing(pulse, {
toValue: 1,
duration: 700,
easing: Easing.inOut(Easing.quad),
useNativeDriver: true
})
])
)
loop.start()
return () => loop.stop()
}, [moving, pulse])
const color = warn ? colors.statusAmber : cloudStatusColor(status)
// Shapes, not only colors (glossary CloudStateDot): filled running, half waking, hollow asleep,
// square archived, so state reads without color too.
const half = isCloudStatusTransitional(status) && !warn
const shape = warn
? { backgroundColor: color }
: half
? {}
: status === 'sleeping'
? { borderWidth: 1.5, borderColor: color }
: status === 'archived'
? { borderRadius: radii.mark / 2, backgroundColor: color }
: { backgroundColor: color }
return (
<Animated.View
importantForAccessibility="no"
style={[
{ width: size, height: size, borderRadius: radii.pill, overflow: 'hidden', opacity: pulse },
shape
]}
>
{half ? (
<View
style={{
flex: 1,
flexDirection: 'row',
borderRadius: radii.pill,
borderWidth: 1.5,
borderColor: color
}}
>
<View style={{ flex: 1, backgroundColor: color }} />
<View style={{ flex: 1 }} />
</View>
) : null}
</Animated.View>
)
}
@@ -0,0 +1,97 @@
import { Archive, Moon } from 'lucide-react-native'
import { useState } from 'react'
import { View } from 'react-native'
import { ConfirmModal } from '../components/ConfirmModal'
import { ListRow } from '../components/list/ListGroup'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { colors } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import type { CloudWorkspace } from './cloud-control-plane-client'
import { cloudWorkspaceScreenStyles as styles } from './cloud-workspace-screen-styles'
/** Sleep (no sandbox hours, files kept) and Archive, each behind a confirm because both stop agents. */
export function CloudWorkspaceActions(props: {
workspace: CloudWorkspace
busy: 'sleep' | 'archive' | null
disabled: boolean
onSleep: () => void
onArchive: () => void
}) {
useMobileColorScheme()
const t = useMobileTranslation()
// The kind outlives `open` so the dialog keeps its own title while it animates closed.
const [kind, setKind] = useState<'sleep' | 'archive'>('sleep')
const [open, setOpen] = useState(false)
const ask = (next: 'sleep' | 'archive') => {
setKind(next)
setOpen(true)
}
const running = props.workspace.status === 'running'
return (
<>
{running ? (
<View style={styles.section}>
<View style={styles.card}>
<ListRow
key="sleep"
leading={<Moon size={18} color={colors.textSecondary} />}
title={t('cloud.actions.sleep', 'Sleep')}
subtitle={t(
'cloud.actions.sleepCaption',
'Sandbox hours stop. Files are kept; agents restart when it wakes.'
)}
busy={props.busy === 'sleep'}
disabled={props.disabled}
onPress={() => ask('sleep')}
/>
</View>
</View>
) : null}
<View style={styles.section}>
<View style={styles.card}>
<ListRow
key="archive"
leading={<Archive size={18} color={colors.textSecondary} />}
title={t('cloud.actions.archive', 'Archive')}
busy={props.busy === 'archive'}
disabled={props.disabled || props.workspace.status === 'archived'}
onPress={() => ask('archive')}
/>
</View>
</View>
<ConfirmModal
visible={open}
title={
kind === 'sleep'
? t('cloud.sleep.title', 'Sleep {{title}}?', { title: props.workspace.title })
: t('cloud.archive.title', 'Archive {{title}}?', { title: props.workspace.title })
}
message={
kind === 'sleep'
? t(
'cloud.sleep.message',
'Running agents stop now and restart when you wake it. Files are kept.'
)
: t(
'cloud.archive.message',
'It goes to sleep and keeps a snapshot you can unarchive. Unpushed work stays in that snapshot.'
)
}
confirmLabel={
kind === 'sleep'
? t('cloud.sleep.confirm', 'Sleep')
: t('cloud.archive.confirm', 'Archive')
}
onConfirm={() => {
setOpen(false)
if (kind === 'sleep') {
props.onSleep()
} else {
props.onArchive()
}
}}
onCancel={() => setOpen(false)}
/>
</>
)
}
+307
View File
@@ -0,0 +1,307 @@
import { useRouter } from 'expo-router'
import { useCallback, useEffect, useRef, useState } from 'react'
import {
ActivityIndicator,
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
Text,
View
} from 'react-native'
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'
import { ListRow } from '../components/list/ListGroup'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { structuredSessionOperationId } from '../session/structured-session-operation-id'
import { SettingsNavBar } from '../settings/settings-screen-frame'
import { colors, spacing } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { CloudAgentStartedCard } from './CloudAgentStartedCard'
import { CloudErrorBanner } from './CloudErrorBanner'
import { CloudPromptComposer } from './CloudPromptComposer'
import { CloudPromptQueueSection } from './CloudPromptQueueSection'
import { CloudStatusCard } from './CloudStatusCard'
import { CloudWorkspaceActions } from './CloudWorkspaceActions'
import { archiveCloudWorkspace, sleepCloudWorkspace } from './cloud-control-plane-client'
import { CloudRequestError } from './cloud-control-plane-request'
import {
enqueueCloudPrompt,
listQueuedCloudPrompts,
subscribeCloudPromptQueue,
type QueuedCloudPrompt
} from './cloud-prompt-queue'
import {
forgetRevokedCloudSession,
loadCloudControlPlane,
useCloudHostRemoval
} from './cloud-session'
import { attachErrorMessage, phaseLabel } from './cloud-workspace-copy'
import { cloudRepoLine } from './cloud-workspace-display'
import {
cloudScrollBottomPadding,
cloudWorkspaceScreenStyles as styles
} from './cloud-workspace-screen-styles'
import { useCloudWorkspaceRun } from './use-cloud-workspace-run'
import {
useCloudEnrollment,
useCloudWorkspace,
useCloudWorkspaceLiveStatus,
useInvalidateCloudWorkspaces
} from './use-cloud-workspaces'
export function CloudWorkspaceScreen({
sandboxId,
autostart = false
}: {
sandboxId: string
autostart?: boolean
}) {
useMobileColorScheme()
const t = useMobileTranslation()
const router = useRouter()
const insets = useSafeAreaInsets()
const removal = useCloudHostRemoval()
const enrollment = useCloudEnrollment()
const { workspace, isPending, isFetching, refetch } = useCloudWorkspace(enrollment, sandboxId)
const live = useCloudWorkspaceLiveStatus(sandboxId, workspace?.status === 'running')
const invalidate = useInvalidateCloudWorkspaces()
const { run, active, start, fail, clearError, openSession } = useCloudWorkspaceRun(
sandboxId,
workspace
)
const [queue, setQueue] = useState<QueuedCloudPrompt[]>([])
const [busyAction, setBusyAction] = useState<'sleep' | 'archive' | null>(null)
useEffect(() => {
const load = () =>
void listQueuedCloudPrompts(sandboxId)
.then(setQueue)
.catch(() => setQueue([]))
load()
return subscribeCloudPromptQueue(load)
}, [sandboxId])
// Arriving from "New cloud workspace": carry straight on to pairing and the first prompt.
const autostarted = useRef(false)
useEffect(() => {
if (autostart && !autostarted.current && workspace) {
autostarted.current = true
void start('send')
}
}, [autostart, workspace, start])
const send = useCallback(
async (text: string, agent: string): Promise<boolean> => {
try {
await enqueueCloudPrompt({
sandboxId,
text,
agent,
operationId: structuredSessionOperationId()
})
} catch (error) {
fail(
error instanceof Error && error.message === 'cloud_prompt_queue_full'
? t('cloud.error.queueFull', 'Too many prompts are waiting. Send or remove some first.')
: t('cloud.error.generic', 'Something went wrong. Your files are safe.')
)
return false
}
void start('send')
return true
},
[sandboxId, start, fail, t]
)
const lifecycle = useCallback(
async (action: 'sleep' | 'archive') => {
const plane = await loadCloudControlPlane()
if (!plane || !workspace) {
return
}
clearError()
setBusyAction(action)
try {
if (action === 'sleep') {
await sleepCloudWorkspace(plane, workspace.id)
} else {
await archiveCloudWorkspace(plane, workspace.id)
router.back()
}
} catch (error) {
await forgetRevokedCloudSession(error, removal)
fail(
action === 'archive' && error instanceof CloudRequestError && error.status === 404
? t(
'cloud.error.archiveUnavailable',
'Archiving isn’t available yet. Put it to sleep instead.'
)
: attachErrorMessage(error, t)
)
} finally {
setBusyAction(null)
invalidate()
}
},
[workspace, clearError, router, removal, fail, t, invalidate]
)
if (!workspace) {
return (
<SafeAreaView style={styles.container} edges={['top']}>
<SettingsNavBar
title={t('cloud.workspace.title', 'Cloud workspace')}
onBack={() => router.back()}
/>
<View style={styles.centered}>
{isPending || isFetching || enrollment === undefined ? (
<ActivityIndicator color={colors.textMuted} />
) : (
<>
<Text style={styles.emptyText}>
{t(
'cloud.workspace.missing',
'This workspace is gone or you no longer have access.'
)}
</Text>
<Pressable
accessibilityRole="button"
onPress={() => void refetch()}
style={styles.linkButton}
>
<Text style={styles.link}>{t('common.tryAgain', 'Try again')}</Text>
</Pressable>
</>
)}
</View>
</SafeAreaView>
)
}
const awake = workspace.status === 'running'
const unavailable =
workspace.status === 'creating' ||
workspace.status === 'stopping' ||
workspace.status === 'archived'
const primaryLabel =
run.kind === 'launched'
? t('cloud.action.openSession', 'Open session')
: run.kind === 'attaching'
? phaseLabel(run.phase, t)
: run.kind === 'delivering'
? t('cloud.phase.delivering', 'Starting your agent')
: workspace.status === 'creating'
? t('cloud.action.settingUp', 'Starting…')
: workspace.status === 'archived'
? t('cloud.status.archived', 'Archived')
: awake
? t('cloud.action.open', 'Open workspace')
: t('cloud.action.wakeOpen', 'Wake and open workspace')
const primaryDisabled = active || unavailable
return (
<SafeAreaView style={styles.container} edges={['top']}>
<KeyboardAvoidingView
style={styles.flex}
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
>
<SettingsNavBar title={workspace.title} onBack={() => router.back()} />
<ScrollView
contentContainerStyle={[
styles.scroll,
{ paddingBottom: cloudScrollBottomPadding(insets.bottom) }
]}
keyboardShouldPersistTaps="handled"
showsVerticalScrollIndicator={false}
>
<CloudStatusCard
workspace={workspace}
run={run}
usage={live.data?.usage ?? null}
uptimeMs={live.data?.uptimeMs ?? null}
promptsWaiting={queue.some((prompt) => prompt.status !== 'failed')}
/>
{run.kind === 'launched' ? (
<CloudAgentStartedCard
agent={run.agent.agent}
onOpenTerminal={run.agent.paneKey ? () => openSession('terminal') : null}
/>
) : null}
{run.kind === 'error' ? (
<CloudErrorBanner
message={run.message}
onRetry={run.retry ? () => void start(run.retry ?? 'open') : null}
/>
) : null}
<View style={styles.section}>
<Text style={styles.sectionHeading} accessibilityRole="header">
{t('cloud.prompt.heading', 'Ask an agent')}
</Text>
<CloudPromptComposer
awake={awake}
waking={active || workspace.status === 'waking'}
disabled={unavailable}
onSend={send}
/>
</View>
<CloudPromptQueueSection queue={queue} awake={awake} onRetry={() => void start('send')} />
<View style={styles.section}>
<Text style={styles.sectionHeading} accessibilityRole="header">
{t('cloud.details.heading', 'Details')}
</Text>
<View style={styles.card}>
{cloudRepoLine(workspace) ? (
<ListRow
key="repo"
title={t('cloud.details.repo', 'Repository')}
value={cloudRepoLine(workspace) ?? ''}
/>
) : null}
{cloudRepoLine(workspace) ? <View style={styles.detailDivider} /> : null}
<ListRow
key="machine"
title={t('cloud.details.machine', 'Sandbox size')}
value={
workspace.machineSize === 'large'
? t('cloud.size.large', 'Large (8 vCPU · 16 GB)')
: t('cloud.size.standard', 'Standard (4 vCPU · 8 GB)')
}
/>
</View>
</View>
<CloudWorkspaceActions
workspace={workspace}
busy={busyAction}
disabled={active || busyAction !== null}
onSleep={() => void lifecycle('sleep')}
onArchive={() => void lifecycle('archive')}
/>
</ScrollView>
<View style={[styles.actionBar, { paddingBottom: Math.max(insets.bottom, spacing.md) }]}>
<Pressable
accessibilityRole="button"
accessibilityState={{ disabled: primaryDisabled, busy: active }}
disabled={primaryDisabled}
onPress={() => (run.kind === 'launched' ? openSession('chat') : void start('open'))}
style={({ pressed }) => [
styles.primary,
primaryDisabled && !active && styles.primaryDisabled,
pressed && styles.pressed
]}
>
{active ? <ActivityIndicator size="small" color={colors.bgBase} /> : null}
<Text
style={[styles.primaryText, primaryDisabled && !active && styles.primaryTextDisabled]}
>
{primaryLabel}
</Text>
</Pressable>
</View>
</KeyboardAvoidingView>
</SafeAreaView>
)
}
@@ -0,0 +1,391 @@
import { useRouter } from 'expo-router'
import { useEffect, useRef, useState } from 'react'
import {
ActivityIndicator,
KeyboardAvoidingView,
Platform,
Pressable,
ScrollView,
Text,
TextInput,
View
} from 'react-native'
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context'
import { useMobileTranslation } from '../i18n/use-mobile-translation'
import { structuredSessionOperationId } from '../session/structured-session-operation-id'
import { SettingsNavBar, SettingsNote, SettingsSection } from '../settings/settings-screen-frame'
import { colors, radii, spacing, typography } from '../theme/mobile-theme'
import { themedStyleSheet } from '../theme/themed-styles'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'
import { CloudAgentPicker, CLOUD_DEFAULT_AGENT } from './CloudAgentPicker'
import { CloudPhaseSteps } from './CloudPhaseSteps'
import { createCloudWorkspace, renameCloudWorkspace } from './cloud-control-plane-client'
import { enqueueCloudPrompt } from './cloud-prompt-queue'
import { loadCloudDeviceIdentity } from './cloud-device-identity'
import { CloudElapsed } from './CloudElapsed'
import {
forgetRevokedCloudSession,
loadCloudControlPlane,
useCloudHostRemoval
} from './cloud-session'
import { recordCloudWorkspaceProjectRoot } from './cloud-workspace-links'
import { cloudScrollBottomPadding } from './cloud-workspace-screen-styles'
import { cloudWorkspaceIdFor, normalizeCloudRepoInput } from './cloud-repo-input'
import {
useCloudEnrollment,
useCloudWorkspaces,
useInvalidateCloudWorkspaces,
useSeedCloudWorkspace
} from './use-cloud-workspaces'
import { nextCloudWorkspaceTitle } from './cloud-workspace-display'
import { createErrorMessage } from './cloud-workspace-copy'
type Size = 'standard' | 'large'
export function NewCloudWorkspaceScreen() {
useMobileColorScheme()
const t = useMobileTranslation()
const router = useRouter()
const insets = useSafeAreaInsets()
const invalidate = useInvalidateCloudWorkspaces()
const existing = useCloudWorkspaces(useCloudEnrollment()).data
const [repo, setRepo] = useState('')
const [branch, setBranch] = useState('')
const [agent, setAgent] = useState(CLOUD_DEFAULT_AGENT)
const [prompt, setPrompt] = useState('')
const [size, setSize] = useState<Size>('standard')
const [startedAt, setStartedAt] = useState<number | null>(null)
const [error, setError] = useState<string | null>(null)
const removal = useCloudHostRemoval()
const seed = useSeedCloudWorkspace()
const mountedRef = useRef(true)
const creatingRef = useRef(false)
// One id per form: a retry after a lost reply reuses it, and the server answers with the same sandbox.
const workspaceIdRef = useRef<string | null>(null)
const abortRef = useRef<AbortController | null>(null)
useEffect(() => {
mountedRef.current = true
return () => {
mountedRef.current = false
abortRef.current?.abort()
}
}, [])
const repoUrl = repo.trim() ? normalizeCloudRepoInput(repo) : null
// A different repo or branch is a different workspace, so it gets its own id.
useEffect(() => {
workspaceIdRef.current = null
}, [repoUrl, branch, size])
const repoInvalid = repo.trim() !== '' && repoUrl === null
const creating = startedAt !== null
const create = async () => {
if (creatingRef.current || repoInvalid) {
return
}
creatingRef.current = true
const plane = await loadCloudControlPlane()
const enrollment = (await loadCloudDeviceIdentity()).enrollment
if (!plane || !enrollment) {
creatingRef.current = false
router.replace('/')
return
}
setError(null)
setStartedAt(Date.now())
const controller = new AbortController()
abortRef.current = controller
let created: Awaited<ReturnType<typeof createCloudWorkspace>>
try {
created = await createCloudWorkspace(
plane,
{
workspaceId: (workspaceIdRef.current ??= cloudWorkspaceIdFor(repoUrl)),
machineSize: size,
...(repoUrl
? { repo: { url: repoUrl, ...(branch.trim() ? { ref: branch.trim() } : {}) } }
: {})
},
controller.signal
)
} catch (failure) {
if (controller.signal.aborted || !mountedRef.current) {
creatingRef.current = false
return
}
await forgetRevokedCloudSession(failure, removal)
creatingRef.current = false
setStartedAt(null)
setError(createErrorMessage(failure, t))
return
}
// The sandbox exists from here on: nothing below may report "couldn't create" or create again.
const { projectRoot } = created
const workspace = {
...created.workspace,
title: nextCloudWorkspaceTitle(created.workspace.title, existing ?? [])
}
await recordCloudWorkspaceProjectRoot(workspace.id, projectRoot).catch(() => undefined)
void renameCloudWorkspace(plane, workspace.id, workspace.title)
if (prompt.trim()) {
await enqueueCloudPrompt({
sandboxId: workspace.id,
text: prompt.trim(),
agent,
operationId: structuredSessionOperationId()
}).catch(() => undefined)
}
seed(enrollment.deviceSessionId, workspace)
invalidate()
if (mountedRef.current) {
router.replace({
pathname: '/cloud/[sandboxId]',
params: { sandboxId: workspace.id, autostart: '1' }
})
}
}
return (
<SafeAreaView style={styles.container} edges={['top']}>
<KeyboardAvoidingView
style={styles.flex}
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
>
<SettingsNavBar
title={t('cloud.new.title', 'Create workspace')}
onBack={() => router.back()}
/>
<ScrollView
contentContainerStyle={[
styles.scroll,
{ paddingBottom: cloudScrollBottomPadding(insets.bottom) }
]}
keyboardShouldPersistTaps="handled"
showsVerticalScrollIndicator={false}
>
<SettingsSection
first
title={t('cloud.new.repoHeading', 'Repository')}
footer={
repoInvalid ? (
<SettingsNote error>
{t('cloud.new.repoInvalid', 'Use a GitHub repository, like owner/repo.')}
</SettingsNote>
) : (
t(
'cloud.new.repoNote',
'Private repos need the Orca Cloud GitHub app, connected once from Orca on your computer. Leave empty for a blank project.'
)
)
}
>
<View key="repo" style={styles.field}>
<TextInput
style={styles.input}
value={repo}
onChangeText={setRepo}
editable={!creating}
placeholder={t('cloud.new.repoPlaceholder', 'owner/repo')}
placeholderTextColor={colors.textMuted}
autoCapitalize="none"
autoCorrect={false}
keyboardType="url"
accessibilityLabel={t('cloud.new.repoLabel', 'GitHub repository')}
/>
</View>
</SettingsSection>
<SettingsSection title={t('cloud.new.branchLabel', 'Branch')}>
<View key="branch" style={styles.field}>
<TextInput
style={styles.input}
value={branch}
onChangeText={setBranch}
editable={!creating}
placeholder={t('cloud.new.branchPlaceholder', 'Default branch')}
placeholderTextColor={colors.textMuted}
autoCapitalize="none"
autoCorrect={false}
accessibilityLabel={t('cloud.new.branchLabel', 'Branch')}
/>
</View>
</SettingsSection>
<SettingsSection title={t('cloud.new.agentHeading', 'First task')}>
<View key="task" style={styles.field}>
<CloudAgentPicker value={agent} onChange={setAgent} disabled={creating} />
<TextInput
style={[styles.input, styles.multiline]}
value={prompt}
onChangeText={setPrompt}
editable={!creating}
multiline
placeholder={t(
'cloud.new.promptPlaceholder',
'Optional. What should the agent start on?'
)}
placeholderTextColor={colors.textMuted}
accessibilityLabel={t('cloud.prompt.label', 'Prompt')}
maxLength={20_000}
/>
</View>
</SettingsSection>
<SettingsSection title={t('cloud.new.sizeHeading', 'Sandbox size')} radioGroup>
<View key="size" style={styles.segmented}>
{(['standard', 'large'] as const).map((option) => (
<Pressable
key={option}
accessibilityRole="radio"
accessibilityState={{ checked: size === option, disabled: creating }}
disabled={creating}
onPress={() => setSize(option)}
style={[styles.segment, size === option && styles.segmentSelected]}
>
<Text
style={[styles.segmentTitle, size === option && styles.segmentTitleSelected]}
>
{option === 'standard'
? t('cloud.size.standardShort', 'Standard')
: t('cloud.size.largeShort', 'Large')}
</Text>
<Text style={styles.segmentDetail}>
{option === 'standard'
? t('cloud.size.standardSpec', '4 vCPU · 8 GB')
: t('cloud.size.largeSpec', '8 vCPU · 16 GB')}
</Text>
</Pressable>
))}
</View>
</SettingsSection>
<Text style={styles.sizeNote}>
{t('cloud.size.largeNote', 'Large uses sandbox hours at 2× the rate.')}
</Text>
{creating ? (
<View style={styles.progress}>
<CloudPhaseSteps
phase="creating"
creating
delivering={prompt.trim() !== ''}
since={startedAt}
/>
</View>
) : null}
{error ? (
<Text style={styles.error} accessibilityRole="alert">
{error}
</Text>
) : null}
</ScrollView>
<View style={[styles.actionBar, { paddingBottom: Math.max(insets.bottom, spacing.md) }]}>
<Pressable
accessibilityRole="button"
accessibilityState={{ disabled: creating || repoInvalid, busy: creating }}
disabled={creating || repoInvalid}
onPress={() => void create()}
style={({ pressed }) => [
styles.primary,
repoInvalid && styles.primaryDisabled,
pressed && styles.pressed
]}
>
{creating ? <ActivityIndicator size="small" color={colors.bgBase} /> : null}
<Text style={styles.primaryText}>
{creating
? t('cloud.new.creating', 'Starting your cloud workspace')
: prompt.trim()
? t('cloud.new.createAndStart', 'Start workspace and agent')
: t('cloud.new.create', 'Start workspace')}
</Text>
{startedAt !== null ? (
<CloudElapsed since={startedAt} style={styles.primaryElapsed} />
) : null}
</Pressable>
</View>
</KeyboardAvoidingView>
</SafeAreaView>
)
}
const styles = themedStyleSheet(() => ({
container: { flex: 1, backgroundColor: colors.bgBase },
flex: { flex: 1 },
scroll: { paddingHorizontal: spacing.lg, paddingTop: spacing.sm },
field: { padding: spacing.md, gap: spacing.md },
input: {
minHeight: 44,
fontSize: 16,
color: colors.textPrimary,
backgroundColor: colors.bgRaised,
borderRadius: radii.control,
paddingHorizontal: spacing.md,
paddingVertical: 10
},
multiline: { minHeight: 96, maxHeight: 200, textAlignVertical: 'top', lineHeight: 21 },
segmented: { flexDirection: 'row', gap: spacing.sm, padding: spacing.md },
segment: {
flex: 1,
borderRadius: radii.control,
borderWidth: 1,
borderColor: colors.borderSubtle,
backgroundColor: colors.bgRaised,
paddingVertical: spacing.sm + 2,
paddingHorizontal: spacing.md,
gap: 2
},
segmentSelected: { borderColor: colors.textPrimary },
segmentTitle: { fontSize: 15, color: colors.textSecondary, fontWeight: '500' },
segmentTitleSelected: { color: colors.textPrimary, fontWeight: '600' },
sizeNote: {
fontSize: 13,
lineHeight: 18,
color: colors.textMuted,
marginTop: 6,
paddingHorizontal: spacing.lg
},
segmentDetail: { fontSize: 13, color: colors.textMuted },
progress: {
marginTop: spacing.lg,
backgroundColor: colors.bgPanel,
borderRadius: radii.surface,
padding: spacing.lg
},
error: {
marginTop: spacing.lg,
fontSize: typography.bodySize,
lineHeight: 20,
color: colors.statusRed,
paddingHorizontal: spacing.xs
},
actionBar: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
paddingHorizontal: spacing.lg,
paddingTop: spacing.md,
backgroundColor: colors.bgBase,
borderTopWidth: 1,
borderTopColor: colors.borderSubtle
},
primary: {
minHeight: 50,
borderRadius: radii.control,
backgroundColor: colors.textPrimary,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
gap: spacing.sm,
paddingHorizontal: spacing.lg
},
primaryDisabled: { opacity: 0.4 },
pressed: { opacity: 0.7 },
primaryText: { fontSize: 16, fontWeight: '600', color: colors.bgBase },
primaryElapsed: {
fontSize: 15,
color: colors.bgBase,
opacity: 0.7,
fontVariant: ['tabular-nums']
}
}))
@@ -0,0 +1,45 @@
import { z } from 'zod'
import {
CloudWorkspaceGrantIssuedSchema,
SealedGrantSchema
} from '../../../src/shared/cloud-workspaces/cloud-workspaces-wire'
import { OrcaCloudDeviceSessionSchema } from '../../../src/shared/orca-cloud-device-enrollment-contract'
import { cloudRequest as request, type CloudControlPlane } from './cloud-control-plane-request'
// Access: a VM's sealed per-device grant, and this user's device sessions.
export type CloudSealedGrant = z.infer<typeof SealedGrantSchema>
/** Asks the VM for this phone's own orcad device, sealed to a one-time key only this phone holds. */
export async function requestCloudWorkspaceGrant(
plane: CloudControlPlane,
sandboxId: string,
body: { recipientPublicKeyB64: string; deviceLabel: string }
): Promise<{ grantId: string; sealed: CloudSealedGrant }> {
const reply = await request(
plane,
'POST',
`/v1/team/workspaces/${encodeURIComponent(sandboxId)}/grants`,
CloudWorkspaceGrantIssuedSchema,
{ body: { ...body, deviceKind: 'mobile' }, timeoutMs: 60_000 }
)
return { grantId: reply.grant.id, sealed: reply.sealed }
}
export async function listCloudDeviceSessions(plane: CloudControlPlane) {
return (
await request(
plane,
'GET',
'/v1/device-sessions',
z.object({ deviceSessions: z.array(OrcaCloudDeviceSessionSchema) })
)
).deviceSessions
}
export async function revokeCloudDeviceSession(
plane: CloudControlPlane,
id: string
): Promise<void> {
await request(plane, 'DELETE', `/v1/device-sessions/${encodeURIComponent(id)}`, z.unknown())
}
@@ -0,0 +1,251 @@
import { z } from 'zod'
import {
CloudWorkspaceListResponseSchema,
type CloudWorkspace as TeamWorkspace
} from '../../../src/shared/cloud-workspaces/cloud-workspaces-wire'
import {
HostedWakeResponseSchema,
HostedWorkspaceStatusResponseSchema,
type HostedWakeResponse,
type HostedWorkspaceStatus
} from '../../../src/shared/hosted-sandbox/hosted-sandbox-wire'
import {
CloudRequestError,
cloudRequest as request,
type CloudControlPlane
} from './cloud-control-plane-request'
// The phone's direct HTTPS client for the Orca Cloud control plane. Every request carries a fresh
// device-signed token; the phone holds no bearer secret and no desktop sits in the path.
export type CloudWorkspaceStatus =
| 'creating'
| 'running'
| 'waking'
| 'stopping'
| 'sleeping'
| 'archived'
| 'failed'
export type CloudWorkspace = {
/** The sandbox row id: stable across sleep, wake and rollover. */
id: string
workspaceId: string
title: string
repo: { url: string; branch: string | null } | null
status: CloudWorkspaceStatus
machineSize: string
createdAt: number
lastActivityAt: number | null
/** Compute spend so far, when the registry reports it. */
cost: { usd: number; usdPerHour: number | null } | null
createdByUserId: string | null
}
const SANDBOX_STATUS: Record<string, CloudWorkspaceStatus> = {
creating: 'creating',
running: 'running',
ready: 'running',
resuming: 'waking',
waking: 'waking',
suspending: 'stopping',
stopping: 'stopping',
suspended: 'sleeping',
sleeping: 'sleeping',
archived: 'archived',
failed: 'failed',
unresponsive: 'failed',
destroying: 'stopping'
}
const SandboxViewSchema = z.object({
id: z.string(),
workspaceId: z.string(),
status: z.string(),
machineSize: z.string(),
createdAt: z.number()
})
export function cloudWorkspaceTitleFallback(repoUrl: string | null, workspaceId: string): string {
const name = repoUrl
?.replace(/\.git$/, '')
.split('/')
.findLast(Boolean)
if (name) {
return name
}
// Phone-made ids are `<repo>-<8 hex>`; other clients use opaque ids that read as noise.
const named = /^(.+)-[0-9a-f]{8}$/.exec(workspaceId)?.[1]
return named && !/^(m|orca)$/.test(named) && !/[0-9a-f]{8}-[0-9a-f]{4}/.test(named)
? named
: 'Cloud workspace'
}
function fromRegistry(row: TeamWorkspace): CloudWorkspace {
return {
id: row.id,
workspaceId: row.workspaceId,
title: row.title?.trim() || cloudWorkspaceTitleFallback(row.repo?.url ?? null, row.workspaceId),
repo: row.repo,
status: row.archivedAt ? 'archived' : (SANDBOX_STATUS[row.status] ?? 'failed'),
machineSize: row.machineSize,
createdAt: row.createdAt,
lastActivityAt: row.lastActivityAt,
cost: {
usd: row.cost.totalUsdMicros / 1_000_000,
usdPerHour: row.cost.rateUsdMicrosPerHour ? row.cost.rateUsdMicrosPerHour / 1_000_000 : null
},
createdByUserId: row.createdBy
}
}
function fromSandbox(row: z.infer<typeof SandboxViewSchema>): CloudWorkspace {
return {
id: row.id,
workspaceId: row.workspaceId,
title: cloudWorkspaceTitleFallback(null, row.workspaceId),
repo: null,
status: SANDBOX_STATUS[row.status] ?? 'failed',
machineSize: row.machineSize,
createdAt: row.createdAt,
lastActivityAt: null,
cost: null,
createdByUserId: null
}
}
// Lists archived rows as `archived` rather than folding them into `suspended` (W2 lifecycle).
const FEATURES = { 'x-orca-sandbox-features': 'archive' }
/** The team registry (titles, repos, cost) first; the owner-only lifecycle list until it ships. */
export async function listCloudWorkspaces(
plane: CloudControlPlane,
signal?: AbortSignal
): Promise<CloudWorkspace[]> {
try {
const body = await request(
plane,
'GET',
'/v1/team/workspaces?scope=team&archived=include',
CloudWorkspaceListResponseSchema,
{ signal }
)
return body.workspaces.map(fromRegistry)
} catch (error) {
if (!(error instanceof CloudRequestError) || error.status !== 404) {
throw error
}
}
const body = await request(
plane,
'GET',
'/v1/sandboxes',
z.object({ sandboxes: z.array(SandboxViewSchema) }),
{ signal, headers: FEATURES }
)
return body.sandboxes.map(fromSandbox)
}
export type CloudWakeState = HostedWakeResponse['state']
/** One long-poll wake (the server holds it up to 30 s); idempotent and single-flight server side. */
export async function wakeCloudWorkspace(
plane: CloudControlPlane,
sandboxId: string,
signal?: AbortSignal
): Promise<{ state: CloudWakeState; endpoint: string | null }> {
const body = await request(
plane,
'POST',
`/v1/sandboxes/${encodeURIComponent(sandboxId)}/wake?wait=30s`,
HostedWakeResponseSchema,
{ body: {}, timeoutMs: 45_000, signal }
)
return { state: body.state, endpoint: body.endpoint }
}
export async function sleepCloudWorkspace(
plane: CloudControlPlane,
sandboxId: string
): Promise<void> {
await request(
plane,
'POST',
`/v1/sandboxes/${encodeURIComponent(sandboxId)}/suspend`,
z.unknown(),
{ timeoutMs: 60_000 }
)
}
export async function archiveCloudWorkspace(
plane: CloudControlPlane,
sandboxId: string
): Promise<void> {
await request(
plane,
'POST',
`/v1/sandboxes/${encodeURIComponent(sandboxId)}/archive`,
z.unknown(),
{ body: {}, timeoutMs: 90_000 }
)
}
export type CloudWorkspaceLiveStatus = HostedWorkspaceStatus
/** Live state, uptime and spend. */
export async function readCloudWorkspaceStatus(
plane: CloudControlPlane,
sandboxId: string
): Promise<CloudWorkspaceLiveStatus> {
const reply = await request(
plane,
'GET',
`/v1/sandboxes/${encodeURIComponent(sandboxId)}/status`,
HostedWorkspaceStatusResponseSchema
)
return reply.status
}
const ReadySchema = z.object({
sandbox: SandboxViewSchema,
connection: z.object({ projectRoot: z.string() }).passthrough()
})
export async function createCloudWorkspace(
plane: CloudControlPlane,
body: {
workspaceId: string
machineSize: 'standard' | 'large'
repo?: { url: string; ref?: string }
},
signal?: AbortSignal
): Promise<{ workspace: CloudWorkspace; projectRoot: string }> {
const ready = await request(plane, 'POST', '/v1/sandboxes', ReadySchema, {
body,
timeoutMs: 180_000,
signal
})
const workspace = fromSandbox(ready.sandbox)
return {
workspace: {
...workspace,
title: cloudWorkspaceTitleFallback(body.repo?.url ?? null, body.workspaceId),
repo: body.repo ? { url: body.repo.url, branch: body.repo.ref ?? null } : null
},
projectRoot: ready.connection.projectRoot
}
}
export async function renameCloudWorkspace(
plane: CloudControlPlane,
sandboxId: string,
title: string
): Promise<void> {
await request(
plane,
'PATCH',
`/v1/team/workspaces/${encodeURIComponent(sandboxId)}`,
z.unknown(),
{
body: { title }
}
).catch(() => undefined)
}
@@ -0,0 +1,79 @@
import { z } from 'zod'
// One HTTPS call to the Orca Cloud control plane with a fresh device-signed token.
export class CloudRequestError extends Error {
constructor(
readonly code: string,
readonly status: number,
readonly reason?: string
) {
super(reason ? `${code}:${reason}` : code)
this.name = 'CloudRequestError'
}
}
export type CloudControlPlane = {
baseUrl: string
token: () => string
fetch?: typeof fetch
}
const ErrorBodySchema = z.object({ error: z.string(), reason: z.string().optional() })
export async function cloudRequest<T>(
plane: CloudControlPlane,
method: 'GET' | 'POST' | 'PATCH' | 'DELETE',
path: string,
schema: z.ZodType<T>,
init: {
body?: unknown
timeoutMs?: number
signal?: AbortSignal
headers?: Record<string, string>
} = {}
): Promise<T> {
// Hermes lacks AbortSignal.timeout/any, so the deadline is a plain controller.
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), init.timeoutMs ?? 30_000)
const onAbort = () => controller.abort()
init.signal?.addEventListener('abort', onAbort)
const signal = controller.signal
let response: Response
try {
response = await (plane.fetch ?? fetch)(new URL(path, `${plane.baseUrl}/`).toString(), {
method,
headers: {
...init.headers,
authorization: `Bearer ${plane.token()}`,
...(init.body === undefined ? {} : { 'content-type': 'application/json' })
},
body: init.body === undefined ? undefined : JSON.stringify(init.body),
signal
})
} catch {
throw new CloudRequestError(
init.signal?.aborted ? 'aborted' : signal.aborted ? 'timeout' : 'network_error',
0
)
} finally {
clearTimeout(timer)
init.signal?.removeEventListener('abort', onAbort)
}
const raw: unknown = await response.json().catch(() => null)
if (!response.ok) {
const parsed = ErrorBodySchema.safeParse(raw)
throw new CloudRequestError(
parsed.data?.error ?? 'http_error',
response.status,
parsed.data?.reason
)
}
const parsed = schema.safeParse(raw)
if (!parsed.success) {
throw new CloudRequestError('invalid_response', response.status)
}
return parsed.data
}
/** Org registry first (titles, repos, cost); the owner-only lifecycle list until the registry ships. */
@@ -0,0 +1,74 @@
import { Platform } from 'react-native'
import {
ORCA_CLOUD_ENROLLMENT_SIGNED_OUT,
ORCA_CLOUD_ENROLLMENT_UNAVAILABLE,
OrcaCloudEnrollDeviceResultSchema
} from '../../../src/shared/orca-cloud-device-enrollment-contract'
import { bindDeferredRpcOperation, defineRpcOperation } from '../transport/rpc/rpc-operation'
import { rpcResultVariant } from '../transport/rpc/rpc-operation-result-reader'
import type { UnvalidatedRpcRequestPort } from '../transport/rpc/unvalidated-rpc-request-port'
import { loadCloudDeviceIdentity, saveCloudEnrollment } from './cloud-device-identity'
const enrollDevice = bindDeferredRpcOperation(
defineRpcOperation({
name: 'orca-cloud.enroll-device',
method: 'orcaCloud.enrollDevice',
acceptance: 'require-result-or-throw-message',
barrier: 'after-caller-barrier',
read: rpcResultVariant('orca-cloud-device-session', OrcaCloudEnrollDeviceResultSchema)
})
)
export type CloudEnrollmentFailure = 'signed-out' | 'not-offered' | 'unreachable'
export class CloudEnrollmentError extends Error {
constructor(readonly failure: CloudEnrollmentFailure) {
super(failure)
this.name = 'CloudEnrollmentError'
}
}
function failureFor(message: string): CloudEnrollmentFailure {
if (message.includes(ORCA_CLOUD_ENROLLMENT_SIGNED_OUT)) {
return 'signed-out'
}
// Older desktops refuse the method by name; both mean "this desktop cannot lend Orca Cloud".
if (
message.includes(ORCA_CLOUD_ENROLLMENT_UNAVAILABLE) ||
/not available|unknown method|method_not_found/i.test(message)
) {
return 'not-offered'
}
return 'unreachable'
}
/** Borrows a paired desktop's Orca Cloud sign-in once to enroll this phone's own key. */
export async function enrollCloudDevice(args: {
desktop: UnvalidatedRpcRequestPort
desktopName: string
deviceLabel: string
}): Promise<void> {
const identity = await loadCloudDeviceIdentity()
const platform = Platform.OS === 'ios' || Platform.OS === 'android' ? Platform.OS : 'web'
let result
try {
result = enrollDevice.interpret(
await enrollDevice.request(
args.desktop,
{ label: args.deviceLabel, platform, publicKeyB64: identity.publicKeyB64 },
{ timeoutMs: 20_000 }
)
)
} catch (error) {
throw new CloudEnrollmentError(
failureFor(error instanceof Error ? error.message : String(error))
)
}
await saveCloudEnrollment({
controlPlaneUrl: result.controlPlaneUrl,
deviceSessionId: result.deviceSession.id,
label: result.deviceSession.label,
enrolledAt: result.deviceSession.createdAt,
viaHostName: args.desktopName
})
}
+101
View File
@@ -0,0 +1,101 @@
import * as SecureStore from 'expo-secure-store'
import nacl from 'tweetnacl'
import { z } from 'zod'
import { randomBytes } from '../transport/encryption/e2ee'
import { decodeBase64Bytes, encodeBase64Bytes } from '../transport/encryption/base64-byte-codec'
// This phone's own Orca Cloud key and enrollment. Kept off iCloud Keychain and backups, like pairing
// credentials, so a restored phone has to be enrolled again rather than inheriting access.
const STORE_KEY = 'orca.cloud-device.v1'
const OPTIONS: SecureStore.SecureStoreOptions = {
keychainAccessible: SecureStore.WHEN_UNLOCKED_THIS_DEVICE_ONLY
}
const EnrollmentSchema = z.object({
controlPlaneUrl: z.string().url(),
deviceSessionId: z.string().min(1),
label: z.string(),
enrolledAt: z.number(),
/** The desktop that lent its sign-in; shown so the user knows which account this is. */
viaHostName: z.string().optional()
})
const StoredSchema = z.object({
seedB64: z.string(),
enrollment: EnrollmentSchema.nullable()
})
export type CloudEnrollment = z.infer<typeof EnrollmentSchema>
export type CloudDeviceIdentity = {
publicKeyB64: string
secretKey: Uint8Array
enrollment: CloudEnrollment | null
}
let cached: Promise<CloudDeviceIdentity> | null = null
const listeners = new Set<() => void>()
function fromSeed(seed: Uint8Array, enrollment: CloudEnrollment | null): CloudDeviceIdentity {
const pair = nacl.sign.keyPair.fromSeed(new Uint8Array(seed))
return { publicKeyB64: encodeBase64Bytes(pair.publicKey), secretKey: pair.secretKey, enrollment }
}
async function write(seed: Uint8Array, enrollment: CloudEnrollment | null): Promise<void> {
await SecureStore.setItemAsync(
STORE_KEY,
JSON.stringify({ seedB64: encodeBase64Bytes(seed), enrollment }),
OPTIONS
)
}
async function readSeed(): Promise<{ seed: Uint8Array; enrollment: CloudEnrollment | null }> {
const raw = await SecureStore.getItemAsync(STORE_KEY, OPTIONS)
let parsed: ReturnType<typeof StoredSchema.safeParse> | null = null
try {
parsed = raw ? StoredSchema.safeParse(JSON.parse(raw)) : null
} catch {
parsed = null
}
if (parsed?.success) {
const seed = decodeBase64Bytes(parsed.data.seedB64)
if (seed.length === 32) {
return { seed, enrollment: parsed.data.enrollment }
}
}
// Nothing usable stored: a fresh key. An unreadable old key could never sign again anyway, so
// its enrollment goes with it and the phone is simply connected again.
const seed = randomBytes(32)
await write(seed, null)
return { seed, enrollment: null }
}
export function loadCloudDeviceIdentity(): Promise<CloudDeviceIdentity> {
cached ??= readSeed()
.then(({ seed, enrollment }) => fromSeed(seed, enrollment))
.catch((error: unknown) => {
cached = null
throw error
})
return cached
}
export async function saveCloudEnrollment(enrollment: CloudEnrollment): Promise<void> {
const { seed } = await readSeed()
await write(seed, enrollment)
cached = Promise.resolve(fromSeed(seed, enrollment))
listeners.forEach((listener) => listener())
}
/** Forgets the enrollment and rotates the key, so a revoked session can never be reused. */
export async function clearCloudEnrollment(): Promise<void> {
const seed = randomBytes(32)
await write(seed, null)
cached = Promise.resolve(fromSeed(seed, null))
listeners.forEach((listener) => listener())
}
export function subscribeCloudDeviceIdentity(listener: () => void): () => void {
listeners.add(listener)
return () => listeners.delete(listener)
}
+11
View File
@@ -0,0 +1,11 @@
import Constants from 'expo-constants'
import { Platform } from 'react-native'
/** How this phone appears in Orca Cloud's device list (the user's own device name when the OS shares it). */
export function cloudDeviceLabel(): string {
const name = Constants.deviceName?.trim()
if (name) {
return name.slice(0, 64)
}
return Platform.OS === 'ios' ? 'iPhone' : Platform.OS === 'android' ? 'Android phone' : 'Browser'
}
+43
View File
@@ -0,0 +1,43 @@
import nacl from 'tweetnacl'
import { encodeBase64Bytes } from '../transport/encryption/base64-byte-codec'
/** Must match cloud/apps/sandbox-control/src/device-sessions.ts. */
export const CLOUD_DEVICE_TOKEN_ISSUER = 'orca-device'
export const CLOUD_DEVICE_TOKEN_AUDIENCE = 'orca-sandbox'
const TOKEN_TTL_SECONDS = 120
function base64Url(bytes: Uint8Array): string {
return encodeBase64Bytes(bytes).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
function utf8(value: string): Uint8Array {
return new TextEncoder().encode(value)
}
/** A compact EdDSA JWT the control plane verifies against this device's enrolled public key. */
export function signCloudDeviceToken(args: {
deviceSessionId: string
secretKey: Uint8Array
nowMs: number
nonce: Uint8Array
}): string {
const iat = Math.floor(args.nowMs / 1000)
const header = base64Url(
utf8(JSON.stringify({ alg: 'EdDSA', typ: 'JWT', kid: args.deviceSessionId }))
)
const payload = base64Url(
utf8(
JSON.stringify({
iss: CLOUD_DEVICE_TOKEN_ISSUER,
sub: args.deviceSessionId,
aud: CLOUD_DEVICE_TOKEN_AUDIENCE,
iat,
exp: iat + TOKEN_TTL_SECONDS,
jti: base64Url(args.nonce)
})
)
)
const signingInput = `${header}.${payload}`
const signature = nacl.sign.detached(utf8(signingInput), new Uint8Array(args.secretKey))
return `${signingInput}.${base64Url(signature)}`
}
+196
View File
@@ -0,0 +1,196 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { z } from 'zod'
import { randomV4Uuid } from '../platform/random-v4-uuid'
import { structuredSessionOperationId } from '../session/structured-session-operation-id'
// Prompts typed for a sleeping cloud workspace. They stay on the phone (the control plane must
// never hold prompt text in the clear) until the VM is up and the phone delivers them over E2EE.
const QUEUE_KEY = 'orca:cloud-prompt-queue:v1'
const MAX_QUEUED_PER_WORKSPACE = 20
const QueuedPromptSchema = z.object({
id: z.string(),
sandboxId: z.string(),
text: z.string().min(1).max(20_000),
agent: z.string().min(1),
createdAt: z.number(),
/** Reused on every retry so a reply lost mid-launch never starts a second agent. */
operationId: z.string(),
status: z.enum(['queued', 'delivering', 'failed']),
/** A code the screen maps to words (see cloud-workspace-copy.ts); never raw host text. */
error: z.string().optional()
})
export type QueuedCloudPrompt = z.infer<typeof QueuedPromptSchema>
const listeners = new Set<() => void>()
let mutation: Promise<unknown> = Promise.resolve()
async function read(): Promise<QueuedCloudPrompt[]> {
const raw = await AsyncStorage.getItem(QUEUE_KEY)
try {
const value: unknown = raw ? JSON.parse(raw) : []
// Entry by entry: one bad record must not drop every other waiting prompt.
return Array.isArray(value)
? value.flatMap((entry) => {
const parsed = QueuedPromptSchema.safeParse(entry)
return parsed.success ? [parsed.data] : []
})
: []
} catch {
// A corrupt value must not wedge the queue forever; it is replaced on the next write.
return []
}
}
function mutate<T>(
change: (queue: QueuedCloudPrompt[]) => { next: QueuedCloudPrompt[]; result: T }
): Promise<T> {
const run = mutation.then(async () => {
const { next, result } = change(await read())
await AsyncStorage.setItem(QUEUE_KEY, JSON.stringify(next))
listeners.forEach((listener) => listener())
return result
})
mutation = run.catch(() => undefined)
return run
}
export async function listQueuedCloudPrompts(sandboxId: string): Promise<QueuedCloudPrompt[]> {
return (await read()).filter((prompt) => prompt.sandboxId === sandboxId)
}
export function enqueueCloudPrompt(args: {
sandboxId: string
text: string
agent: string
operationId: string
now?: number
}): Promise<QueuedCloudPrompt> {
return mutate((queue) => {
const prompt: QueuedCloudPrompt = {
id: randomV4Uuid(),
sandboxId: args.sandboxId,
text: args.text,
agent: args.agent,
createdAt: args.now ?? Date.now(),
operationId: args.operationId,
status: 'queued'
}
const mine = queue.filter((entry) => entry.sandboxId === args.sandboxId)
if (mine.length >= MAX_QUEUED_PER_WORKSPACE) {
throw new Error('cloud_prompt_queue_full')
}
return { next: [...queue, prompt], result: prompt }
})
}
export function markCloudPrompt(
id: string,
update: Pick<QueuedCloudPrompt, 'status'> & { error?: string }
): Promise<void> {
return mutate((queue) => ({
next: queue.map((entry) =>
entry.id === id
? {
...entry,
status: update.status,
...(update.error ? { error: update.error } : { error: undefined })
}
: entry
),
result: undefined
}))
}
export function markWaitingCloudPromptsFailed(sandboxId: string, error: string): Promise<void> {
return mutate((queue) => ({
next: queue.map((entry) =>
entry.sandboxId === sandboxId && entry.status === 'queued'
? { ...entry, status: 'failed' as const, error }
: entry
),
result: undefined
}))
}
/** Never sent (the phone couldn't connect): keep the operation id. The host answered with a failure:
* its ledger replays that answer for the same id, so a real retry needs a new one. */
function retried(entry: QueuedCloudPrompt): QueuedCloudPrompt {
// Never sent, or the reply was lost: the same id lets the host dedupe instead of launching twice.
const neverSent = entry.error === 'not_connected' || entry.error === 'transport_lost'
return {
...entry,
status: 'queued',
error: undefined,
operationId: neverSent ? entry.operationId : structuredSessionOperationId()
}
}
export function retryCloudPrompt(id: string): Promise<void> {
return mutate((queue) => ({
next: queue.map((entry) =>
entry.id === id && entry.status === 'failed' ? retried(entry) : entry
),
result: undefined
}))
}
/** Prompts held back only because the phone couldn't connect go out with the next attach. */
export function requeueUnsentCloudPrompts(sandboxId: string): Promise<void> {
return mutate((queue) => ({
next: queue.map((entry) =>
entry.sandboxId === sandboxId && entry.status === 'failed' && entry.error === 'not_connected'
? retried(entry)
: entry
),
result: undefined
}))
}
export function removeCloudPrompt(id: string): Promise<void> {
return mutate((queue) => ({ next: queue.filter((entry) => entry.id !== id), result: undefined }))
}
export function subscribeCloudPromptQueue(listener: () => void): () => void {
listeners.add(listener)
return () => listeners.delete(listener)
}
const inFlight = new Set<string>()
/** Waiting prompts go out in order; failed ones wait for the user's Retry so one bad prompt never
* blocks the rest. `delivering` ones (app killed mid-send) are resent with the same operation id,
* which the host's durable launch ledger deduplicates. One run per workspace at a time. */
export async function deliverQueuedCloudPrompts(
sandboxId: string,
deliver: (prompt: QueuedCloudPrompt) => Promise<void>,
errorCodeFor: (error: unknown) => string
): Promise<{ delivered: number; failed: QueuedCloudPrompt[] }> {
if (inFlight.has(sandboxId)) {
return { delivered: 0, failed: [] }
}
inFlight.add(sandboxId)
let delivered = 0
const failed: QueuedCloudPrompt[] = []
try {
for (const prompt of await listQueuedCloudPrompts(sandboxId)) {
if (prompt.status === 'failed') {
continue
}
await markCloudPrompt(prompt.id, { status: 'delivering' })
try {
await deliver(prompt)
await removeCloudPrompt(prompt.id)
delivered += 1
} catch (error) {
const code = errorCodeFor(error)
await markCloudPrompt(prompt.id, { status: 'failed', error: code })
failed.push({ ...prompt, status: 'failed', error: code })
}
}
} finally {
inFlight.delete(sandboxId)
}
return { delivered, failed }
}
+21
View File
@@ -0,0 +1,21 @@
import { describe, expect, it } from 'vitest'
import { normalizeCloudRepoInput } from './cloud-repo-input'
describe('normalizeCloudRepoInput', () => {
it('accepts owner/repo and github.com URLs', () => {
expect(normalizeCloudRepoInput('stablyai/orca')).toBe('https://github.com/stablyai/orca')
expect(normalizeCloudRepoInput('https://github.com/stablyai/orca.git')).toBe(
'https://github.com/stablyai/orca'
)
expect(normalizeCloudRepoInput('github.com/stablyai/orca/')).toBe(
'https://github.com/stablyai/orca'
)
})
it('refuses other hosts, deeper paths and traversal', () => {
expect(normalizeCloudRepoInput('https://gitlab.com/a/b')).toBeNull()
expect(normalizeCloudRepoInput('stablyai/orca/tree/main')).toBeNull()
expect(normalizeCloudRepoInput('../etc')).toBeNull()
expect(normalizeCloudRepoInput('file://a/b')).toBeNull()
})
})
+32
View File
@@ -0,0 +1,32 @@
import { randomBytes } from '../transport/encryption/e2ee'
const SEGMENT = /^[A-Za-z0-9_.-]+$/
/** `owner/repo`, a github.com URL or its .git form, as the https URL the control plane clones; null otherwise. */
export function normalizeCloudRepoInput(input: string): string | null {
const trimmed = input
.trim()
.replace(/\/+$/, '')
.replace(/\.git$/, '')
const path = trimmed.replace(/^(https?:\/\/)?(www\.)?github\.com\//i, '')
const parts = path.split('/')
if (
parts.length !== 2 ||
!parts.every((part) => SEGMENT.test(part) && part !== '.' && part !== '..')
) {
return null
}
if (path === trimmed && /^[a-z]+:/i.test(trimmed)) {
return null
}
return `https://github.com/${parts[0]}/${parts[1]}`
}
/** `<repo>-<8 hex>`: readable in every list, unique per create. */
export function cloudWorkspaceIdFor(repoUrl: string | null): string {
const name = (repoUrl?.split('/').pop() ?? 'workspace')
.replace(/[^A-Za-z0-9_.-]/g, '-')
.slice(0, 48)
const suffix = Array.from(randomBytes(4), (byte) => byte.toString(16).padStart(2, '0')).join('')
return `${name}-${suffix}`
}
@@ -0,0 +1,74 @@
import {
createCipheriv,
createPublicKey,
diffieHellman,
generateKeyPairSync,
hkdfSync,
randomBytes
} from 'node:crypto'
import { describe, expect, it } from 'vitest'
import { createGrantRecipientKey, openCloudSealedGrant } from './cloud-sealed-grant'
// Seals the way the VM does (node:crypto), so the phone's pure-JS opener is checked against it.
function sealLikeVm(recipientPublicKey: Uint8Array, grantId: string, plaintext: string) {
const eph = generateKeyPairSync('x25519')
const ephRaw = Buffer.from(eph.publicKey.export({ format: 'jwk' }).x!, 'base64url')
const recipient = createPublicKey({
key: { kty: 'OKP', crv: 'X25519', x: Buffer.from(recipientPublicKey).toString('base64url') },
format: 'jwk'
})
const shared = diffieHellman({ privateKey: eph.privateKey, publicKey: recipient })
const key = Buffer.from(
hkdfSync(
'sha256',
shared,
Buffer.concat([ephRaw, recipientPublicKey]),
'orca-cloud-grant-v1',
32
)
)
const nonce = randomBytes(12)
const cipher = createCipheriv('aes-256-gcm', key, nonce)
cipher.setAAD(Buffer.from(`orca-cloud-grant-v1:${grantId}`))
const ct = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final(), cipher.getAuthTag()])
return {
v: 1 as const,
ephemeralPublicKeyB64: ephRaw.toString('base64'),
nonceB64: nonce.toString('base64'),
ciphertextB64: ct.toString('base64')
}
}
const OFFER = JSON.stringify({
pairingUrl: 'orca://pair?code=abc',
deviceId: 'dev-1',
scope: 'mobile'
})
describe('openCloudSealedGrant', () => {
it('opens a grant sealed by the VM for this grant id', async () => {
const recipient = createGrantRecipientKey()
const sealed = sealLikeVm(recipient.publicKey, 'grant_1', OFFER)
await expect(openCloudSealedGrant(sealed, 'grant_1', recipient.secretKey)).resolves.toEqual({
pairingUrl: 'orca://pair?code=abc',
deviceId: 'dev-1',
scope: 'mobile'
})
})
it('refuses a box replayed under another grant id', async () => {
const recipient = createGrantRecipientKey()
const sealed = sealLikeVm(recipient.publicKey, 'grant_1', OFFER)
await expect(openCloudSealedGrant(sealed, 'grant_2', recipient.secretKey)).rejects.toThrow()
})
it('refuses a runtime-scope offer even when it decrypts', async () => {
const recipient = createGrantRecipientKey()
const sealed = sealLikeVm(
recipient.publicKey,
'g',
JSON.stringify({ pairingUrl: 'orca://pair?code=x', deviceId: 'd', scope: 'runtime' })
)
await expect(openCloudSealedGrant(sealed, 'g', recipient.secretKey)).rejects.toThrow()
})
})
+33
View File
@@ -0,0 +1,33 @@
import { gcm } from '@noble/ciphers/aes'
import { hkdf } from '@noble/hashes/hkdf'
import { sha256 } from '@noble/hashes/sha256'
import nacl from 'tweetnacl'
import {
openSealedGrant,
type OpenedGrant,
type SealedGrant,
type SealedGrantPrimitives
} from '../../../src/shared/cloud-workspaces/sealed-grant'
// Hermes has no WebCrypto, so the shared opener runs on pure-JS primitives here.
const PRIMITIVES: SealedGrantPrimitives = {
x25519: (secretKey, publicKey) =>
nacl.scalarMult(new Uint8Array(secretKey), new Uint8Array(publicKey)),
x25519PublicKey: (secretKey) => nacl.scalarMult.base(new Uint8Array(secretKey)),
hkdfSha256: (ikm, salt, info, length) => hkdf(sha256, ikm, salt, info, length),
aesGcmDecrypt: (key, nonce, ciphertext, aad) => gcm(key, nonce, aad).decrypt(ciphertext)
}
export function createGrantRecipientKey(): { publicKey: Uint8Array; secretKey: Uint8Array } {
const pair = nacl.box.keyPair()
return { publicKey: new Uint8Array(pair.publicKey), secretKey: new Uint8Array(pair.secretKey) }
}
/** Opens a grant the VM sealed to this phone's one-time key; a runtime-scope offer is refused. */
export function openCloudSealedGrant(
sealed: SealedGrant,
grantId: string,
recipientSecretKey: Uint8Array
): Promise<OpenedGrant> {
return openSealedGrant(sealed, grantId, recipientSecretKey, PRIMITIVES, 'mobile')
}
+63
View File
@@ -0,0 +1,63 @@
import { useQueryClient, type QueryClient } from '@tanstack/react-query'
import { useMemo } from 'react'
import { useForgetHostClient } from '../transport/connection/host-client-hooks'
import { randomBytes } from '../transport/encryption/e2ee'
import { removeHostAndCloseClient } from '../transport/hosts/host-removal-lifecycle'
import { CloudRequestError, type CloudControlPlane } from './cloud-control-plane-request'
import { clearCloudEnrollment, loadCloudDeviceIdentity } from './cloud-device-identity'
import { signCloudDeviceToken } from './cloud-device-token'
import { clearCloudWorkspaceLinks, cloudWorkspaceHostIds } from './cloud-workspace-links'
/** The control plane as this enrolled phone, or null before enrollment. */
export async function loadCloudControlPlane(): Promise<CloudControlPlane | null> {
const identity = await loadCloudDeviceIdentity()
const enrollment = identity.enrollment
if (!enrollment) {
return null
}
return {
baseUrl: enrollment.controlPlaneUrl,
token: () =>
signCloudDeviceToken({
deviceSessionId: enrollment.deviceSessionId,
secretKey: identity.secretKey,
nowMs: Date.now(),
nonce: randomBytes(12)
})
}
}
/** What removing a paired host needs from the app's React context. */
export type CloudHostRemoval = {
forgetHostClient: (hostId: string) => void
queryClient: QueryClient
}
export function useCloudHostRemoval(): CloudHostRemoval {
const forgetHostClient = useForgetHostClient()
const queryClient = useQueryClient()
return useMemo(() => ({ forgetHostClient, queryClient }), [forgetHostClient, queryClient])
}
/** Ends everything Orca Cloud gave this phone: its VM pairings first, then the session and key. */
export async function forgetAllCloudAccess(removal: CloudHostRemoval): Promise<void> {
for (const hostId of await cloudWorkspaceHostIds()) {
await removeHostAndCloseClient(hostId, removal.forgetHostClient, removal.queryClient).catch(
() => undefined
)
}
await clearCloudWorkspaceLinks()
await clearCloudEnrollment()
}
/** A 401 means this phone's session was revoked or expired: drop all cloud access so nothing outlives it. */
export async function forgetRevokedCloudSession(
error: unknown,
removal: CloudHostRemoval
): Promise<boolean> {
if (error instanceof CloudRequestError && error.status === 401) {
await forgetAllCloudAccess(removal)
return true
}
return false
}
+125
View File
@@ -0,0 +1,125 @@
import { parsePairingCode } from '../pairing/pairing'
import { startPreProfilePairing } from '../pairing/pre-profile-pairing-coordinator'
import { updateHostNameAndEndpoint } from '../transport/hosts/host-store'
import { cloudWorkspaceHostId, recordCloudWorkspaceHost } from './cloud-workspace-links'
import { requestCloudWorkspaceGrant } from './cloud-control-plane-access'
import { wakeCloudWorkspace } from './cloud-control-plane-client'
import { CloudRequestError, type CloudControlPlane } from './cloud-control-plane-request'
import { encodeBase64Bytes } from '../transport/encryption/base64-byte-codec'
import { createGrantRecipientKey, openCloudSealedGrant } from './cloud-sealed-grant'
// Opening a cloud workspace from the phone: wake the VM through the control plane, then talk to its
// orcad directly as an ordinary paired host (stable wss endpoint, this phone's own device). The
// first open asks the VM for a sealed per-device grant; later opens reuse the saved host.
export type CloudAttachPhase = 'waking' | 'granting' | 'pairing' | 'connected'
export class CloudAttachError extends Error {
constructor(
readonly code:
| 'archived'
| 'failed'
| 'not_found'
| 'wake_timeout'
| 'grant_failed'
| 'pairing_failed'
) {
super(code)
this.name = 'CloudAttachError'
}
}
const WAKE_DEADLINE_MS = 3 * 60_000
const WAKE_RETRY_MS = 1_500
function pause(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise((resolve) => {
const timer = setTimeout(resolve, ms)
signal?.addEventListener('abort', () => {
clearTimeout(timer)
resolve()
})
})
}
/** Long-polls the wake route until the VM is ready; each call holds up to 30 s on the server. */
export async function wakeCloudWorkspaceUntilReady(
plane: CloudControlPlane,
sandboxId: string,
signal?: AbortSignal
): Promise<void> {
const deadline = Date.now() + WAKE_DEADLINE_MS
while (Date.now() < deadline && !signal?.aborted) {
const startedAt = Date.now()
const { state } = await wakeCloudWorkspace(plane, sandboxId, signal)
if (state === 'ready') {
return
}
if (state === 'archived' || state === 'failed' || state === 'not_found') {
throw new CloudAttachError(state)
}
// A server that answers `waking` without holding the request must not be hammered.
if (Date.now() - startedAt < WAKE_RETRY_MS) {
await pause(WAKE_RETRY_MS, signal)
}
}
throw new CloudAttachError('wake_timeout')
}
export async function attachCloudWorkspace(args: {
plane: CloudControlPlane
sandboxId: string
title: string
deviceLabel: string
onPhase: (phase: CloudAttachPhase) => void
signal?: AbortSignal
}): Promise<{ hostId: string; paired: boolean }> {
args.onPhase('waking')
await wakeCloudWorkspaceUntilReady(args.plane, args.sandboxId, args.signal)
const existing = await cloudWorkspaceHostId(args.sandboxId)
if (existing) {
args.onPhase('connected')
return { hostId: existing, paired: false }
}
args.onPhase('granting')
const recipient = createGrantRecipientKey()
let pairingUrl: string
try {
const grant = await requestCloudWorkspaceGrant(args.plane, args.sandboxId, {
recipientPublicKeyB64: encodeBase64Bytes(recipient.publicKey),
deviceLabel: args.deviceLabel
})
pairingUrl = (await openCloudSealedGrant(grant.sealed, grant.grantId, recipient.secretKey))
.pairingUrl
} catch (error) {
if (error instanceof CloudRequestError && error.status === 401) {
throw error
}
throw new CloudAttachError('grant_failed')
} finally {
recipient.secretKey.fill(0)
}
const offer = parsePairingCode(pairingUrl)
if (!offer) {
throw new CloudAttachError('grant_failed')
}
if (args.signal?.aborted) {
throw new CloudAttachError('wake_timeout')
}
args.onPhase('pairing')
const attempt = startPreProfilePairing({ offer, timeoutMs: 30_000 })
const disposeOnAbort = () => attempt.dispose()
args.signal?.addEventListener('abort', disposeOnAbort)
try {
const { hostId } = await attempt.result
await recordCloudWorkspaceHost(args.sandboxId, hostId)
await updateHostNameAndEndpoint(hostId, { personalName: args.title }).catch(() => undefined)
args.onPhase('connected')
return { hostId, paired: true }
} catch {
throw new CloudAttachError('pairing_failed')
} finally {
args.signal?.removeEventListener('abort', disposeOnAbort)
attempt.dispose()
}
}
+174
View File
@@ -0,0 +1,174 @@
import type { Translate } from '../i18n/mobile-i18n'
import { isRpcDeliveryUnknown } from '../transport/rpc/rpc-delivery-ambiguity'
import type { CloudWorkspace } from './cloud-control-plane-client'
import { CloudRequestError } from './cloud-control-plane-request'
import { CloudAttachError, type CloudAttachPhase } from './cloud-workspace-attach'
import { formatElapsed } from './cloud-workspace-display'
// What the cloud workspace screen says about each state and failure.
export function attachErrorMessage(error: unknown, t: Translate): string {
if (error instanceof CloudAttachError) {
switch (error.code) {
case 'archived':
return t(
'cloud.error.archived',
'This workspace is archived. Unarchive it on your computer first.'
)
case 'wake_timeout':
return t(
'cloud.error.wakeTimeout',
'The sandbox is taking longer than usual to wake. Your files are safe.'
)
case 'grant_failed':
return t(
'cloud.error.grant',
'This phone couldn’t get access to the workspace. Your files are safe.'
)
case 'pairing_failed':
return t(
'cloud.error.pairing',
'The workspace is running, but this phone couldn’t connect to it. Your files are safe.'
)
default:
return t(
'cloud.error.failed',
'The workspace couldn’t start. Try again, or open it on your desktop.'
)
}
}
if (error instanceof CloudRequestError) {
if (error.status === 401) {
return t(
'cloud.error.revoked',
'This phone’s Orca Cloud access ended. Connect it again from Home.'
)
}
if (error.code === 'entitlement_denied') {
return t(
'cloud.error.entitlement',
'Out of sandbox hours. Ask an owner or admin to add hours, then try again.'
)
}
if (error.code === 'network_error' || error.code === 'timeout') {
return t(
'cloud.error.network',
'Couldn’t reach Orca Cloud. Check your connection and try again.'
)
}
}
return t('cloud.error.generic', 'Something went wrong. Your files are safe.')
}
export function phaseLabel(phase: CloudAttachPhase, t: Translate): string {
switch (phase) {
case 'waking':
return t('cloud.phase.waking', 'Waking the sandbox')
case 'granting':
return t('cloud.phase.granting', 'Getting this phone access')
case 'pairing':
return t('cloud.phase.pairing', 'Connecting securely')
case 'connected':
return t('cloud.phase.connected', 'Connected')
}
}
export function statusSentence(
workspace: CloudWorkspace,
t: Translate,
uptimeMs: number | null
): string {
switch (workspace.status) {
case 'running':
return uptimeMs !== null
? t(
'cloud.line.runningFor',
'Up {{elapsed}}. Agents keep working when you close the app.',
{ elapsed: formatElapsed(uptimeMs) }
)
: t('cloud.line.running', 'Agents keep working when you close the app.')
case 'sleeping':
return t('cloud.line.sleeping', 'Files are kept, and waking takes about 5 seconds.')
case 'creating':
return t('cloud.line.creating', 'Starting the sandbox and cloning the repository.')
case 'waking':
return t('cloud.line.waking', 'Starting up. This usually takes about 5 seconds.')
case 'stopping':
return t('cloud.line.stopping', 'Files are kept.')
case 'archived':
return t('cloud.line.archived', 'Unarchive it on your computer to work in it again.')
case 'failed':
return t('cloud.line.failed', 'The sandbox couldn’t start. Your files are safe.')
}
}
const PROMPT_ERROR_CODES = ['cloud_project_unavailable', 'cloud_agent_unknown'] as const
/** What a failed prompt keeps: a known code, never raw host text (it may be untranslated or long). */
export function cloudPromptErrorCode(error: unknown): string {
// The launch may have reached the host: keep the operation id so a retry is deduped, never doubled.
if (isRpcDeliveryUnknown(error)) {
return 'transport_lost'
}
const message = error instanceof Error ? error.message : ''
return PROMPT_ERROR_CODES.find((code) => message.includes(code)) ?? 'launch_failed'
}
export function cloudPromptErrorMessage(code: string | undefined, t: Translate): string {
switch (code) {
case 'cloud_project_unavailable':
return t('cloud.prompt.errorProject', 'The workspace’s project isn’t ready yet')
case 'transport_lost':
return t('cloud.prompt.errorTransport', 'The connection dropped while sending')
case 'not_connected':
return t('cloud.prompt.errorNotConnected', 'This phone couldn’t reach the workspace')
case 'cloud_agent_unknown':
return t('cloud.prompt.errorAgent', 'That agent isn’t installed in this workspace')
default:
return t('cloud.prompt.errorLaunch', 'The agent didn’t start')
}
}
export function createErrorMessage(error: unknown, t: Translate): string {
if (error instanceof CloudRequestError) {
if (error.status === 401) {
return t(
'cloud.error.revoked',
'This phone’s Orca Cloud access ended. Connect it again from Home.'
)
}
if (error.code === 'entitlement_denied') {
return error.reason === 'concurrency_limit'
? t(
'cloud.error.concurrency',
'You’re at your team’s limit of running sandboxes. Put one to sleep or delete it, then try again.'
)
: t(
'cloud.error.entitlement',
'Out of sandbox hours. Ask an owner or admin to add hours, then try again.'
)
}
if (error.code === 'provider_unavailable' && error.reason === 'orcad_start_failed') {
return t(
'cloud.error.clone',
'Couldn’t clone the repository. Check the name and that the Orca Cloud GitHub app can see it. Nothing was created, and you weren’t charged.'
)
}
if (error.code === 'timeout' || error.code === 'network_error' || error.status >= 500) {
return t(
'cloud.error.createUnknown',
'Lost the connection while starting it. It may still appear in your list. Try again to pick it up.'
)
}
if (error.code === 'rate_limited') {
return t(
'cloud.error.rateLimited',
'Too many new workspaces at once. Wait a minute, then try again.'
)
}
}
return t(
'cloud.error.create',
'Couldn’t start your cloud workspace. Nothing was created, and you weren’t charged.'
)
}
@@ -0,0 +1,98 @@
import { getUiIntlLocale } from '../i18n/ui-intl-locale'
import type { Translate } from '../i18n/mobile-i18n'
import { colors } from '../theme/mobile-theme'
import type { CloudWorkspace, CloudWorkspaceStatus } from './cloud-control-plane-client'
export function cloudStatusLabel(status: CloudWorkspaceStatus, t: Translate): string {
switch (status) {
case 'creating':
return t('cloud.status.creating', 'Starting')
case 'running':
return t('cloud.status.running', 'Running')
case 'waking':
return t('cloud.status.waking', 'Waking')
case 'stopping':
return t('cloud.status.stopping', 'Going to sleep')
case 'sleeping':
return t('cloud.status.sleeping', 'Asleep')
case 'archived':
return t('cloud.status.archived', 'Archived')
case 'failed':
return t('cloud.status.failed', 'Couldn’t start')
}
}
/** Read at render time: colors are scheme-dependent getters. */
export function cloudStatusColor(status: CloudWorkspaceStatus): string {
switch (status) {
// Glossary CloudStateDot: shapes carry the state in a neutral ink; colour is only for problems.
case 'running':
case 'creating':
case 'waking':
case 'stopping':
return colors.textSecondary
case 'failed':
return colors.statusRed
case 'sleeping':
case 'archived':
return colors.textMuted
}
}
export function isCloudStatusTransitional(status: CloudWorkspaceStatus): boolean {
return status === 'creating' || status === 'waking' || status === 'stopping'
}
const formatters = new Map<string, Intl.NumberFormat>()
function usd(value: number): string {
const locale = getUiIntlLocale()
try {
let format = formatters.get(locale)
if (!format) {
format = new Intl.NumberFormat(locale, { style: 'currency', currency: 'USD' })
formatters.set(locale, format)
}
return format.format(value)
} catch {
return `$${value.toFixed(2)}`
}
}
export function formatCloudCost(value: number): string {
return value > 0 && value < 0.01 ? `<${usd(0.01)}` : usd(value)
}
/** "owner/repo · branch" from a GitHub URL, or null for an empty project. */
export function cloudRepoLine(workspace: Pick<CloudWorkspace, 'repo'>): string | null {
if (!workspace.repo) {
return null
}
const path = workspace.repo.url
.replace(/^https?:\/\/(www\.)?github\.com\//, '')
.replace(/\.git$/, '')
return workspace.repo.branch ? `${path} · ${workspace.repo.branch}` : path
}
export function formatElapsed(ms: number): string {
const seconds = Math.max(0, Math.floor(ms / 1000))
return seconds < 60
? `${seconds}s`
: `${Math.floor(seconds / 60)}m ${String(seconds % 60).padStart(2, '0')}s`
}
/** "Hello-World", then "Hello-World 2", "Hello-World 3": a new workspace never repeats a title. */
export function nextCloudWorkspaceTitle(
base: string,
existing: readonly Pick<CloudWorkspace, 'title'>[]
): string {
const taken = new Set(existing.map((workspace) => workspace.title))
if (!taken.has(base)) {
return base
}
let n = 2
while (taken.has(`${base} ${n}`)) {
n += 1
}
return `${base} ${n}`
}
@@ -0,0 +1,103 @@
import { z } from 'zod'
import { isTuiAgent } from '../../../src/shared/agent-launch/catalog/tui-agent-config'
import { agentLaunchReplayRun } from '../tasks/mobile-workspace-create-operations'
import { bindDeferredRpcOperation, defineRpcOperation } from '../transport/rpc/rpc-operation'
import { rpcResultVariant } from '../transport/rpc/rpc-operation-result-reader'
import type { UnvalidatedRpcRequestPort } from '../transport/rpc/unvalidated-rpc-request-port'
import type { QueuedCloudPrompt } from './cloud-prompt-queue'
// What the phone does on a cloud VM's own orcad once attached: register the cloned project the VM
// booted with, and start agents in it with a prompt.
/** Where the control plane clones the repo; the create response confirms it. */
export const CLOUD_DEFAULT_PROJECT_ROOT = '/vercel/sandbox/project'
const RepoSchema = z.looseObject({ id: z.string(), path: z.string() })
const cloudRepoList = bindDeferredRpcOperation(
defineRpcOperation({
name: 'repo.list-cloud-project',
method: 'repo.list',
acceptance: 'require-result-or-throw-message',
barrier: 'after-caller-barrier',
read: rpcResultVariant('cloud-repos', z.object({ repos: z.array(RepoSchema) }))
})
)
const cloudRepoAdd = bindDeferredRpcOperation(
defineRpcOperation({
name: 'repo.add-cloud-project',
method: 'repo.add',
acceptance: 'require-result-or-throw-message',
barrier: 'after-caller-barrier',
read: rpcResultVariant('cloud-repo-added', z.object({ repo: RepoSchema }))
})
)
const cloudWorktreeList = bindDeferredRpcOperation(
defineRpcOperation({
name: 'worktree.list-cloud-project',
method: 'worktree.list',
acceptance: 'require-result-or-throw-message',
barrier: 'after-caller-barrier',
read: rpcResultVariant(
'cloud-worktrees',
z.object({ worktrees: z.array(z.looseObject({ id: z.string(), path: z.string() })) })
)
})
)
function samePath(a: string, b: string): boolean {
return a.replace(/\/+$/, '') === b.replace(/\/+$/, '')
}
/** The cloud project's main worktree id, registering the project on first use. */
export async function ensureCloudProjectWorktree(
client: UnvalidatedRpcRequestPort,
recordedRoot?: string
): Promise<string> {
const projectRoot = recordedRoot ?? CLOUD_DEFAULT_PROJECT_ROOT
const { repos } = cloudRepoList.interpret(await cloudRepoList.request(client))
// Sandboxes register their one project at boot; with no recorded root, that repo is the project.
const repo =
repos.find((entry) => samePath(entry.path, projectRoot)) ??
(repos.length === 1 && !recordedRoot ? repos[0] : undefined) ??
cloudRepoAdd.interpret(await cloudRepoAdd.request(client, { path: projectRoot })).repo
const { worktrees } = cloudWorktreeList.interpret(
await cloudWorktreeList.request(client, { repo: `id:${repo.id}` })
)
// Never guess another checkout: an agent started in the wrong repository is worse than none.
const main = worktrees.find((entry) => samePath(entry.path, repo.path))
if (!main) {
throw new Error('cloud_project_unavailable')
}
return main.id
}
export type CloudLaunchedAgent = {
agent: string
/** The terminal pane the agent runs in, when the host reports it (where its sign-in happens). */
paneKey?: string
}
export async function launchCloudPrompt(
client: UnvalidatedRpcRequestPort,
worktreeId: string,
prompt: Pick<QueuedCloudPrompt, 'agent' | 'text' | 'operationId'>
): Promise<CloudLaunchedAgent> {
if (!isTuiAgent(prompt.agent)) {
throw new Error('cloud_agent_unknown')
}
// The replay method dedupes by operation id in the host's durable ledger, so a resend after a
// lost reply (or an app killed mid-send) never starts a second agent.
const reply = await agentLaunchReplayRun.request(client, {
agent: prompt.agent,
operationId: prompt.operationId,
target: { kind: 'existing', worktree: `id:${worktreeId}` },
prompt: { text: prompt.text, delivery: 'submit' },
launchSource: 'mobile'
})
const result = agentLaunchReplayRun.interpret(reply)
const paneKey = result.outcome.kind === 'terminal' ? result.outcome.paneKey : undefined
return { agent: prompt.agent, ...(paneKey ? { paneKey } : {}) }
}
+100
View File
@@ -0,0 +1,100 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import { z } from 'zod'
import { loadHostCatalog } from '../transport/hosts/host-store'
// Which paired host is which cloud workspace's VM, and where its project lives on that VM.
const LINKS_KEY = 'orca:cloud-workspace-hosts:v1'
const LinkSchema = z.object({
hostId: z.string().optional(),
pairedAt: z.number().optional(),
/** From the create response; the VM's checkout of the workspace's repo. */
projectRoot: z.string().startsWith('/').optional()
})
const LinksSchema = z.record(z.string(), LinkSchema)
type Links = z.infer<typeof LinksSchema>
let mutation: Promise<unknown> = Promise.resolve()
async function readLinks(): Promise<Links> {
const raw = await AsyncStorage.getItem(LINKS_KEY)
try {
const value: unknown = raw ? JSON.parse(raw) : {}
// Entry by entry: one bad record must not orphan every other VM pairing.
const links: Links = {}
if (value && typeof value === 'object') {
for (const [key, entry] of Object.entries(value)) {
const parsed = LinkSchema.safeParse(entry)
if (parsed.success) {
links[key] = parsed.data
}
}
}
return links
} catch {
// Corrupt storage costs one re-pair, never a workspace that can't be opened.
return {}
}
}
function updateLinks(change: (links: Links) => void): Promise<void> {
const run = mutation.then(async () => {
const links = await readLinks()
change(links)
await AsyncStorage.setItem(LINKS_KEY, JSON.stringify(links))
})
mutation = run.catch(() => undefined)
return run
}
export function recordCloudWorkspaceHost(sandboxId: string, hostId: string | null): Promise<void> {
return updateLinks((links) => {
const current = links[sandboxId] ?? {}
links[sandboxId] = hostId
? { ...current, hostId, pairedAt: Date.now() }
: { projectRoot: current.projectRoot }
})
}
export function recordCloudWorkspaceProjectRoot(
sandboxId: string,
projectRoot: string
): Promise<void> {
if (!projectRoot.startsWith('/')) {
return Promise.resolve()
}
return updateLinks((links) => {
links[sandboxId] = { ...links[sandboxId], projectRoot }
})
}
export async function cloudWorkspaceProjectRoot(sandboxId: string): Promise<string | null> {
return (await readLinks())[sandboxId]?.projectRoot ?? null
}
/** The paired host for a cloud workspace, if this phone still holds a usable credential for it. */
export async function cloudWorkspaceHostId(sandboxId: string): Promise<string | null> {
const hostId = (await readLinks())[sandboxId]?.hostId
if (!hostId) {
return null
}
const host = (await loadHostCatalog()).find((entry) => entry.id === hostId)
if (!host || host.credentialStatus === 'missing') {
await recordCloudWorkspaceHost(sandboxId, null)
return null
}
return host.id
}
export async function cloudWorkspaceHostIds(): Promise<ReadonlySet<string>> {
const ids = Object.values(await readLinks()).flatMap((link) => (link.hostId ? [link.hostId] : []))
return new Set(ids)
}
export async function clearCloudWorkspaceLinks(): Promise<void> {
await updateLinks((links) => {
for (const key of Object.keys(links)) {
delete links[key]
}
})
}

Some files were not shown because too many files have changed in this diff Show More