From 9d48206ac8de446fd174aca85af3fb826574bfc8 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 04:37:10 -0800 Subject: [PATCH 01/30] feat(files): stream runtime file and folder downloads with progress, cancel and open-locally --- src/main/ipc/filesystem/filesystem.ts | 2 + .../runtime-download-errors.ts | 47 ++++ .../runtime-download-handlers.ts | 238 +++++++++++++++++ .../runtime-download-transfer.test.ts | 123 +++++++++ .../runtime-download-transfer.ts | 187 +++++++++++++ .../runtime-open-local-cache.ts | 44 +++ .../runtime-file-command-surface.ts | 2 + .../archive/folder-archive-sessions.test.ts | 172 ++++++++++++ .../files/archive/folder-archive-sessions.ts | 164 ++++++++++++ .../files/archive/folder-archive-walk.ts | 189 +++++++++++++ .../archive/folder-archive-zip-stream.ts | 252 ++++++++++++++++++ src/main/runtime/files/file-reads.ts | 10 + .../methods/files/files-archive-methods.ts | 58 ++++ src/main/runtime/rpc/methods/files/files.ts | 2 + src/preload/api-types.ts | 3 + src/preload/api/runtime-downloads-bridge.ts | 24 ++ src/preload/index.ts | 2 + .../file-explorer-row-action-visibility.ts | 17 +- .../panel/file-explorer-row-context-menu.tsx | 37 ++- .../panel/runtime-download-actions.tsx | 225 ++++++++++++++++ .../panel/runtime-download-toast.tsx | 139 ++++++++++ .../runtime-download-progress-store.ts | 84 ++++++ .../rpc-contract/files-archive-params.ts | 54 ++++ .../rpc-params-catalog.generated.ts | 4 + .../runtime-download-contract.ts | 57 ++++ src/shared/runtime-rpc-call-queue.ts | 1 + 26 files changed, 2131 insertions(+), 6 deletions(-) create mode 100644 src/main/runtime-downloads/runtime-download-errors.ts create mode 100644 src/main/runtime-downloads/runtime-download-handlers.ts create mode 100644 src/main/runtime-downloads/runtime-download-transfer.test.ts create mode 100644 src/main/runtime-downloads/runtime-download-transfer.ts create mode 100644 src/main/runtime-downloads/runtime-open-local-cache.ts create mode 100644 src/main/runtime/files/archive/folder-archive-sessions.test.ts create mode 100644 src/main/runtime/files/archive/folder-archive-sessions.ts create mode 100644 src/main/runtime/files/archive/folder-archive-walk.ts create mode 100644 src/main/runtime/files/archive/folder-archive-zip-stream.ts create mode 100644 src/main/runtime/rpc/methods/files/files-archive-methods.ts create mode 100644 src/preload/api/runtime-downloads-bridge.ts create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx create mode 100644 src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts create mode 100644 src/shared/rpc-contract/files-archive-params.ts create mode 100644 src/shared/runtime-downloads/runtime-download-contract.ts diff --git a/src/main/ipc/filesystem/filesystem.ts b/src/main/ipc/filesystem/filesystem.ts index 014f16a429a..688c3ae8b5f 100644 --- a/src/main/ipc/filesystem/filesystem.ts +++ b/src/main/ipc/filesystem/filesystem.ts @@ -8,6 +8,7 @@ import { } from './filesystem-handler-context' import { registerFilesystemReadHandlers } from './filesystem-read-handlers' import { registerFilesystemDownloadHandlers } from './filesystem-download-handlers' +import { registerRuntimeDownloadHandlers } from '../../runtime-downloads/runtime-download-handlers' import { registerFilesystemWriteHandlers } from './filesystem-write-handlers' import { registerFilesystemSearchHandlers } from './filesystem-search-handlers' import { registerFilesystemGitStatusHandlers } from './filesystem-git-status-handlers' @@ -33,6 +34,7 @@ export function registerFilesystemHandlers( registerFilesystemReadHandlers(context) registerFilesystemDownloadHandlers(context) + registerRuntimeDownloadHandlers() registerFilesystemWriteHandlers(context) registerFilesystemSearchHandlers(context) registerFilesystemGitStatusHandlers(context) diff --git a/src/main/runtime-downloads/runtime-download-errors.ts b/src/main/runtime-downloads/runtime-download-errors.ts new file mode 100644 index 00000000000..67849ecc383 --- /dev/null +++ b/src/main/runtime-downloads/runtime-download-errors.ts @@ -0,0 +1,47 @@ +import type { RuntimeDownloadErrorCode } from '../../shared/runtime-downloads/runtime-download-contract' +import { RuntimeDownloadError } from './runtime-download-transfer' + +const HOST_MESSAGE_CODES: Record = { + archive_too_large: 'too_large', + archive_too_many_entries: 'too_many_entries', + archive_busy: 'host_busy', + archive_not_directory: 'not_a_directory', + archive_canceled: 'canceled', + file_too_large: 'too_large' +} + +/** Maps a failed host reply onto a stable code the renderer can word. */ +export function runtimeDownloadErrorFromResponse(error: { + code: string + message: string +}): RuntimeDownloadError { + if (error.code === 'method_not_found') { + return new RuntimeDownloadError('host_update_required', error.message) + } + const mapped = HOST_MESSAGE_CODES[error.message] + if (mapped) { + return new RuntimeDownloadError(mapped, error.message) + } + if (/ENOENT|not[_ ]found|no such file/i.test(error.message)) { + return new RuntimeDownloadError('not_found', error.message) + } + if (/disconnect|unreachable|closed|timed? ?out|offline/i.test(`${error.code} ${error.message}`)) { + return new RuntimeDownloadError('disconnected', error.message) + } + return new RuntimeDownloadError('transfer_failed', error.message) +} + +export function toRuntimeDownloadError(error: unknown, signal: AbortSignal): RuntimeDownloadError { + if (signal.aborted) { + return new RuntimeDownloadError('canceled') + } + if (error instanceof RuntimeDownloadError) { + return error + } + const message = error instanceof Error ? error.message : String(error) + const code = + typeof (error as { code?: unknown } | null)?.code === 'string' + ? (error as { code: string }).code + : 'runtime_error' + return runtimeDownloadErrorFromResponse({ code, message }) +} diff --git a/src/main/runtime-downloads/runtime-download-handlers.ts b/src/main/runtime-downloads/runtime-download-handlers.ts new file mode 100644 index 00000000000..b7819ada7c3 --- /dev/null +++ b/src/main/runtime-downloads/runtime-download-handlers.ts @@ -0,0 +1,238 @@ +import { app, BrowserWindow, dialog, ipcMain, shell, type WebContents } from 'electron' +import { open } from 'node:fs/promises' +import { join } from 'node:path' +import { + RUNTIME_OPEN_LOCALLY_MAX_BYTES, + type RuntimeDownloadProgressEvent, + type RuntimeDownloadResult, + type RuntimeDownloadStartArgs +} from '../../shared/runtime-downloads/runtime-download-contract' +import { callRuntimeEnvironment } from '../ipc/runtime-environments/runtime-environment-transport-routing' +import { getRuntimeEnvironmentRegistryRoot } from '../ipc/runtime-environments/runtime-environment-registry-root' +import { sanitizeLocalDownloadFilename } from '../ipc/filesystem/local-download-filename' +import { + cleanupLocalTransferPath, + createSiblingTransferPath, + inspectDownloadDestination, + promoteDownloadedFile +} from '../ipc/filesystem/filesystem-download-promotion' +import { abortWhenRendererGone } from '../ipc/renderer-lifetime-abort' +import { + RuntimeDownloadError, + transferRuntimeFile, + transferRuntimeFolder, + type RuntimeDownloadCall, + type RuntimeDownloadProgress +} from './runtime-download-transfer' +import { runtimeDownloadErrorFromResponse, toRuntimeDownloadError } from './runtime-download-errors' +import { + createRuntimeOpenLocalDirectory, + pruneRuntimeOpenLocalCache +} from './runtime-open-local-cache' + +export const RUNTIME_DOWNLOAD_PROGRESS_CHANNEL = 'runtimeDownloads:progress' +// A toast cannot show finer progress than this, and every event crosses IPC. +const PROGRESS_INTERVAL_MS = 120 +const MAX_ACTIVE_DOWNLOADS = 8 + +const activeDownloads = new Map() + +function validateArgs(args: unknown): RuntimeDownloadStartArgs { + const value = args as Partial | null + const isText = (field: unknown, allowEmpty = false): field is string => + typeof field === 'string' && (allowEmpty || field.trim() !== '') && field.length <= 4096 + if ( + !value || + !isText(value.downloadId) || + !isText(value.environmentId) || + !isText(value.worktree) || + !isText(value.relativePath, true) || + !isText(value.name) || + (value.kind !== 'file' && value.kind !== 'folder') || + (value.intent !== 'save' && value.intent !== 'open') + ) { + throw new Error('Invalid runtime download request') + } + if (value.intent === 'open' && value.kind !== 'file') { + throw new Error('Only files can be opened locally') + } + return value as RuntimeDownloadStartArgs +} + +function createCall(args: RuntimeDownloadStartArgs): RuntimeDownloadCall { + return async ( + method: string, + params: unknown, + options: { timeoutMs: number; signal: AbortSignal } + ): Promise => { + const response = await callRuntimeEnvironment( + getRuntimeEnvironmentRegistryRoot(), + args.environmentId, + method, + params, + options.timeoutMs, + args.expectedEnvironmentPairingRevision, + undefined, + { signal: options.signal } + ) + if (!response.ok) { + throw runtimeDownloadErrorFromResponse(response.error) + } + return response.result as T + } +} + +function createProgressEmitter(sender: WebContents, downloadId: string) { + let lastSentAt = 0 + let destinationPath: string | undefined + const send = (event: Omit) => { + if (!sender.isDestroyed()) { + sender.send(RUNTIME_DOWNLOAD_PROGRESS_CHANNEL, { + downloadId, + ...event, + ...(destinationPath ? { destinationPath } : {}) + } satisfies RuntimeDownloadProgressEvent) + } + } + return { + setDestination(path: string) { + destinationPath = path + }, + preparing() { + send({ phase: 'preparing', receivedBytes: 0, totalBytes: null, processedBytes: 0 }) + }, + progress(progress: RuntimeDownloadProgress) { + const now = Date.now() + if (now - lastSentAt < PROGRESS_INTERVAL_MS) { + return + } + lastSentAt = now + send({ phase: 'transferring', ...progress }) + } + } +} + +async function chooseDestination( + sender: WebContents, + args: RuntimeDownloadStartArgs +): Promise { + const baseName = sanitizeLocalDownloadFilename(args.name) + if (args.intent === 'open') { + void pruneRuntimeOpenLocalCache() + return join(await createRuntimeOpenLocalDirectory(), baseName) + } + const suggestedName = args.kind === 'folder' ? `${baseName}.zip` : baseName + const parentWindow = BrowserWindow.fromWebContents(sender) ?? undefined + const options: Electron.SaveDialogOptions = { + defaultPath: join(app.getPath('downloads'), suggestedName), + ...(args.kind === 'folder' ? { filters: [{ name: 'ZIP archive', extensions: ['zip'] }] } : {}) + } + const result = parentWindow + ? await dialog.showSaveDialog(parentWindow, options) + : await dialog.showSaveDialog(options) + return result.canceled || !result.filePath ? null : result.filePath +} + +async function runDownload( + sender: WebContents, + args: RuntimeDownloadStartArgs, + controller: AbortController +): Promise { + const destinationPath = await chooseDestination(sender, args) + if (!destinationPath || controller.signal.aborted) { + return { status: 'canceled' } + } + const emitter = createProgressEmitter(sender, args.downloadId) + emitter.setDestination(destinationPath) + emitter.preparing() + const { existed } = await inspectDownloadDestination(destinationPath) + const tempPath = createSiblingTransferPath(destinationPath, 'download') + const sink = await open(tempPath, 'wx') + let promoted = false + try { + const transferInput = { + call: createCall(args), + worktree: args.worktree, + relativePath: args.relativePath, + sink, + signal: controller.signal, + onProgress: (progress: RuntimeDownloadProgress) => emitter.progress(progress) + } + const outcome = + args.kind === 'folder' + ? await transferRuntimeFolder({ + ...transferInput, + excludeDefaultDirs: args.excludeDefaultDirs !== false + }) + : await transferRuntimeFile({ + ...transferInput, + ...(args.intent === 'open' ? { maxBytes: RUNTIME_OPEN_LOCALLY_MAX_BYTES } : {}) + }) + await sink.close() + if (controller.signal.aborted) { + throw new RuntimeDownloadError('canceled') + } + await promoteDownloadedFile(tempPath, destinationPath, existed) + promoted = true + let openError: string | undefined + if (args.intent === 'open') { + const message = await shell.openPath(destinationPath) + openError = message === '' ? undefined : message + } + return { + status: 'done', + destinationPath, + ...outcome, + ...(openError ? { openError } : {}) + } + } finally { + await sink.close().catch(() => {}) + if (!promoted) { + await cleanupLocalTransferPath(tempPath) + } + } +} + +export function registerRuntimeDownloadHandlers(): void { + ipcMain.handle( + 'runtimeDownloads:start', + async (event, rawArgs: unknown): Promise => { + const args = validateArgs(rawArgs) + if (activeDownloads.has(args.downloadId)) { + throw new Error('Download already running') + } + if (activeDownloads.size >= MAX_ACTIVE_DOWNLOADS) { + return { + status: 'error', + code: 'host_busy', + message: 'Too many downloads are running; wait for one to finish.' + } + } + const controller = new AbortController() + activeDownloads.set(args.downloadId, controller) + // Closing the window must not leave a transfer running nobody can cancel. + const lifetime = abortWhenRendererGone(event.sender) + const onGone = () => controller.abort() + lifetime.signal.addEventListener('abort', onGone, { once: true }) + try { + return await runDownload(event.sender, args, controller) + } catch (error) { + const failure = toRuntimeDownloadError(error, controller.signal) + return failure.code === 'canceled' + ? { status: 'canceled' } + : { status: 'error', code: failure.code, message: failure.message } + } finally { + lifetime.signal.removeEventListener('abort', onGone) + lifetime.dispose() + activeDownloads.delete(args.downloadId) + } + } + ) + + ipcMain.handle('runtimeDownloads:cancel', (_event, args: { downloadId?: unknown }) => { + const controller = + typeof args?.downloadId === 'string' ? activeDownloads.get(args.downloadId) : undefined + controller?.abort() + return { canceled: controller !== undefined } + }) +} diff --git a/src/main/runtime-downloads/runtime-download-transfer.test.ts b/src/main/runtime-downloads/runtime-download-transfer.test.ts new file mode 100644 index 00000000000..75b588c0e33 --- /dev/null +++ b/src/main/runtime-downloads/runtime-download-transfer.test.ts @@ -0,0 +1,123 @@ +import { mkdtemp, open, readFile, rm } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { + RuntimeDownloadError, + transferRuntimeFile, + transferRuntimeFolder, + type RuntimeDownloadCall +} from './runtime-download-transfer' + +let dir: string + +beforeEach(async () => { + dir = await mkdtemp(join(tmpdir(), 'orca-runtime-download-')) +}) + +afterEach(async () => { + await rm(dir, { recursive: true, force: true }) +}) + +function fileHost(content: Buffer, statSize = content.byteLength): RuntimeDownloadCall { + return (async (method: string, params: { offset: number; length: number }) => { + if (method === 'files.stat') { + return { size: statSize, isDirectory: false } + } + const bytes = content.subarray(params.offset, params.offset + params.length) + // Out-of-order replies: later pages may land first. + await new Promise((resolve) => setTimeout(resolve, params.offset % 3)) + return { + contentBase64: bytes.toString('base64'), + bytesRead: bytes.byteLength, + eof: params.offset + bytes.byteLength >= content.byteLength + } + }) as RuntimeDownloadCall +} + +async function runFile(call: RuntimeDownloadCall, extra: { maxBytes?: number } = {}) { + const sink = await open(join(dir, 'out'), 'w+') + try { + const result = await transferRuntimeFile({ + call, + worktree: 'wt', + relativePath: 'a.bin', + sink, + signal: new AbortController().signal, + onProgress: () => {}, + chunkBytes: 7, + concurrency: 3, + ...extra + }) + return { result, bytes: await readFile(join(dir, 'out')) } + } finally { + await sink.close() + } +} + +describe('transferRuntimeFile', () => { + it('reassembles pages fetched in parallel in order', async () => { + const content = Buffer.from('the quick brown fox jumps over the lazy dog, twice over') + const { result, bytes } = await runFile(fileHost(content)) + expect(result.bytes).toBe(content.byteLength) + expect(bytes.equals(content)).toBe(true) + }) + + it('keeps a file that shrank after stat to its final length', async () => { + const content = Buffer.from('short file') + const { bytes } = await runFile(fileHost(content, 1000)) + expect(bytes.toString()).toBe('short file') + }) + + it('downloads an empty file', async () => { + const { bytes } = await runFile(fileHost(Buffer.alloc(0))) + expect(bytes.byteLength).toBe(0) + }) + + it('refuses a file over the caller limit before reading it', async () => { + await expect(runFile(fileHost(Buffer.alloc(20)), { maxBytes: 10 })).rejects.toEqual( + new RuntimeDownloadError('too_large') + ) + }) +}) + +describe('transferRuntimeFolder', () => { + it('releases the host archive when the download is canceled', async () => { + const controller = new AbortController() + const calls: string[] = [] + const call = (async (method: string) => { + calls.push(method) + if (method === 'files.archiveStart') { + return { archiveId: 'z1', totalBytes: 10, excludedDirCount: 0, fileCount: 1 } + } + if (method === 'files.archiveRead') { + controller.abort() + return { + contentBase64: 'AA==', + bytesRead: 1, + eof: false, + processedBytes: 1, + skippedCount: 0 + } + } + return { released: true } + }) as RuntimeDownloadCall + const sink = await open(join(dir, 'out.zip'), 'w+') + try { + await expect( + transferRuntimeFolder({ + call, + worktree: 'wt', + relativePath: 'src', + sink, + signal: controller.signal, + onProgress: () => {}, + excludeDefaultDirs: true + }) + ).rejects.toEqual(new RuntimeDownloadError('canceled')) + } finally { + await sink.close() + } + expect(calls).toEqual(['files.archiveStart', 'files.archiveRead', 'files.archiveCancel']) + }) +}) diff --git a/src/main/runtime-downloads/runtime-download-transfer.ts b/src/main/runtime-downloads/runtime-download-transfer.ts new file mode 100644 index 00000000000..3d6544cf7ba --- /dev/null +++ b/src/main/runtime-downloads/runtime-download-transfer.ts @@ -0,0 +1,187 @@ +import type { FileHandle } from 'node:fs/promises' +import type { RuntimeFileReadChunkResult } from '../../shared/file-content/runtime-file-read' +import { + FOLDER_ARCHIVE_MAX_READ_BYTES, + type FileArchiveReadResult, + type FileArchiveStartResult +} from '../../shared/rpc-contract/files-archive-params' +import type { RuntimeDownloadErrorCode } from '../../shared/runtime-downloads/runtime-download-contract' + +/** files.readChunk's schema ceiling; older hosts accept nothing larger. */ +const FILE_CHUNK_BYTES = 512 * 1024 +// The main-process bulk-read lane admits two calls per host, so a third would only queue. +const FILE_CHUNK_CONCURRENCY = 2 +const CHUNK_TIMEOUT_MS = 60_000 +// The walk of a large tree runs before the first byte; give it room. +const ARCHIVE_START_TIMEOUT_MS = 120_000 + +export class RuntimeDownloadError extends Error { + constructor( + readonly code: RuntimeDownloadErrorCode, + message: string = code + ) { + super(message) + this.name = 'RuntimeDownloadError' + } +} + +export type RuntimeDownloadCall = ( + method: string, + params: unknown, + options: { timeoutMs: number; signal: AbortSignal } +) => Promise + +export type RuntimeDownloadProgress = { + receivedBytes: number + /** Expected bytes; for folders this counts uncompressed source bytes, not ZIP bytes. */ + totalBytes: number | null + /** Source bytes covered so far, comparable with totalBytes. */ + processedBytes: number +} + +type TransferInput = { + call: RuntimeDownloadCall + worktree: string + relativePath: string + sink: FileHandle + signal: AbortSignal + onProgress: (progress: RuntimeDownloadProgress) => void +} + +/** Pages a remote file into `sink` with a few positional reads in flight to hide link latency. */ +export async function transferRuntimeFile( + input: TransferInput & { maxBytes?: number; chunkBytes?: number; concurrency?: number } +): Promise<{ bytes: number }> { + const chunkBytes = input.chunkBytes ?? FILE_CHUNK_BYTES + const stat = await input.call<{ size: number; isDirectory: boolean }>( + 'files.stat', + { worktree: input.worktree, relativePath: input.relativePath }, + { timeoutMs: CHUNK_TIMEOUT_MS, signal: input.signal } + ) + if (stat.isDirectory) { + throw new RuntimeDownloadError('not_a_file') + } + if (input.maxBytes !== undefined && stat.size > input.maxBytes) { + throw new RuntimeDownloadError('too_large') + } + let nextOffset = 0 + // The file may change size mid-transfer; the first EOF page fixes the final length. + let endOffset: number | null = null + let received = 0 + const worker = async (): Promise => { + for (;;) { + if (input.signal.aborted) { + throw new RuntimeDownloadError('canceled') + } + if (endOffset !== null && nextOffset >= endOffset) { + return + } + const offset = nextOffset + nextOffset += chunkBytes + const chunk = await input.call( + 'files.readChunk', + { + worktree: input.worktree, + relativePath: input.relativePath, + offset, + length: chunkBytes + }, + { timeoutMs: CHUNK_TIMEOUT_MS, signal: input.signal } + ) + if (chunk.bytesRead > 0) { + const bytes = Buffer.from(chunk.contentBase64, 'base64') + if (bytes.byteLength !== chunk.bytesRead) { + throw new RuntimeDownloadError('transfer_failed', 'Remote page length mismatch') + } + await input.sink.write(bytes, 0, bytes.byteLength, offset) + received += bytes.byteLength + input.onProgress({ + receivedBytes: received, + totalBytes: stat.size, + processedBytes: received + }) + } + if (chunk.eof || chunk.bytesRead < chunkBytes) { + const end = offset + chunk.bytesRead + endOffset = endOffset === null ? end : Math.min(endOffset, end) + return + } + } + } + const concurrency = Math.max(1, input.concurrency ?? FILE_CHUNK_CONCURRENCY) + await Promise.all(Array.from({ length: concurrency }, worker)) + const finalLength = endOffset ?? received + // A page past a shrunken EOF may have landed; cut the file back to the agreed length. + await input.sink.truncate(finalLength) + return { bytes: finalLength } +} + +export type FolderTransferResult = { + bytes: number + skippedCount: number + excludedDirCount: number + fileCount: number +} + +/** Asks the host to zip a folder, then appends its pages to `sink` until EOF. */ +export async function transferRuntimeFolder( + input: TransferInput & { excludeDefaultDirs: boolean } +): Promise { + const started = await input.call( + 'files.archiveStart', + { + worktree: input.worktree, + relativePath: input.relativePath, + excludeDefaultDirs: input.excludeDefaultDirs + }, + { timeoutMs: ARCHIVE_START_TIMEOUT_MS, signal: input.signal } + ) + let finished = false + let received = 0 + try { + for (;;) { + if (input.signal.aborted) { + throw new RuntimeDownloadError('canceled') + } + const page = await input.call( + 'files.archiveRead', + { archiveId: started.archiveId, maxBytes: FOLDER_ARCHIVE_MAX_READ_BYTES }, + { timeoutMs: CHUNK_TIMEOUT_MS, signal: input.signal } + ) + if (page.bytesRead > 0) { + const bytes = Buffer.from(page.contentBase64, 'base64') + await input.sink.write(bytes, 0, bytes.byteLength, received) + received += bytes.byteLength + } + input.onProgress({ + receivedBytes: received, + totalBytes: started.totalBytes, + processedBytes: page.processedBytes + }) + if (page.eof) { + finished = true + return { + bytes: received, + skippedCount: page.skippedCount, + excludedDirCount: started.excludedDirCount, + fileCount: started.fileCount + } + } + } + } finally { + if (!finished) { + // Best effort: the host's idle timer frees the session if this never arrives. + const release = new AbortController() + void input + .call( + 'files.archiveCancel', + { archiveId: started.archiveId }, + { + timeoutMs: 10_000, + signal: release.signal + } + ) + .catch(() => {}) + } + } +} diff --git a/src/main/runtime-downloads/runtime-open-local-cache.ts b/src/main/runtime-downloads/runtime-open-local-cache.ts new file mode 100644 index 00000000000..63c6bc25ea5 --- /dev/null +++ b/src/main/runtime-downloads/runtime-open-local-cache.ts @@ -0,0 +1,44 @@ +import { randomUUID } from 'node:crypto' +import { mkdir, readdir, rm, stat } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +// Opened copies only need to outlive the app that opens them; a day covers a long session. +const OPEN_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000 + +export function runtimeOpenLocalCacheRoot(): string { + return join(tmpdir(), 'orca-opened-remote-files') +} + +/** A fresh directory per open, so two opens of same-named files never clobber each other. */ +export async function createRuntimeOpenLocalDirectory(root = runtimeOpenLocalCacheRoot()) { + const directory = join(root, randomUUID()) + await mkdir(directory, { recursive: true, mode: 0o700 }) + return directory +} + +/** Best effort: removes cached copies older than a day. Failures never block an open. */ +export async function pruneRuntimeOpenLocalCache( + root = runtimeOpenLocalCacheRoot(), + now = Date.now(), + maxAgeMs = OPEN_CACHE_MAX_AGE_MS +): Promise { + let names: string[] + try { + names = await readdir(root) + } catch { + return + } + await Promise.all( + names.map(async (name) => { + const path = join(root, name) + try { + if (now - (await stat(path)).mtimeMs > maxAgeMs) { + await rm(path, { recursive: true, force: true }) + } + } catch { + // Another app instance may be pruning the same entry. + } + }) + ) +} diff --git a/src/main/runtime/construction/runtime-file-command-surface.ts b/src/main/runtime/construction/runtime-file-command-surface.ts index c975065c25f..87e3ca3f822 100644 --- a/src/main/runtime/construction/runtime-file-command-surface.ts +++ b/src/main/runtime/construction/runtime-file-command-surface.ts @@ -29,6 +29,7 @@ export type RuntimeFileCommandSurface = Pick< | 'readFileExplorerPreview' | 'readChatImagePreview' | 'readFileExplorerChunk' + | 'resolveFileExplorerArchivePath' | 'readDocPreviewFile' | 'readDocPreviewChunk' > & @@ -91,6 +92,7 @@ export function installRuntimeFileCommandSurface( readFileExplorerPreview: fileReads.readFileExplorerPreview.bind(fileReads), readChatImagePreview: fileReads.readChatImagePreview.bind(fileReads), readFileExplorerChunk: fileReads.readFileExplorerChunk.bind(fileReads), + resolveFileExplorerArchivePath: fileReads.resolveFileExplorerArchivePath.bind(fileReads), readDocPreviewFile: fileReads.readDocPreviewFile.bind(fileReads), readDocPreviewChunk: fileReads.readDocPreviewChunk.bind(fileReads), writeFileExplorerFile: fileMutations.writeFileExplorerFile.bind(fileMutations), diff --git a/src/main/runtime/files/archive/folder-archive-sessions.test.ts b/src/main/runtime/files/archive/folder-archive-sessions.test.ts new file mode 100644 index 00000000000..88681003b4f --- /dev/null +++ b/src/main/runtime/files/archive/folder-archive-sessions.test.ts @@ -0,0 +1,172 @@ +import { mkdtemp, mkdir, rm, symlink, writeFile, chmod } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { unzipSync } from 'fflate' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { FolderArchiveSessions } from './folder-archive-sessions' +import { FolderArchiveLimitError, planFolderArchive } from './folder-archive-walk' +import { FolderArchiveZipStream } from './folder-archive-zip-stream' + +let root: string + +beforeEach(async () => { + root = await mkdtemp(join(tmpdir(), 'orca-archive-test-')) +}) + +afterEach(async () => { + await rm(root, { recursive: true, force: true }) +}) + +async function drain(sessions: FolderArchiveSessions, archiveId: string, ownerKey = 'device-a') { + const parts: Buffer[] = [] + for (;;) { + const page = await sessions.read({ archiveId, ownerKey, maxBytes: 64 * 1024 }) + parts.push(Buffer.from(page.contentBase64, 'base64')) + if (page.eof) { + return { bytes: Buffer.concat(parts), last: page } + } + } +} + +describe('FolderArchiveSessions', () => { + it('zips a folder with nested files, empty dirs and the default exclusions applied', async () => { + const project = join(root, 'project') + await mkdir(join(project, 'src', 'empty'), { recursive: true }) + await mkdir(join(project, 'node_modules', 'pkg'), { recursive: true }) + await mkdir(join(project, '.git'), { recursive: true }) + await writeFile(join(project, 'src', 'a.txt'), 'hello') + await writeFile(join(project, 'run.sh'), '#!/bin/sh\n') + await chmod(join(project, 'run.sh'), 0o755) + await writeFile(join(project, 'node_modules', 'pkg', 'index.js'), 'x') + await writeFile(join(project, '.git', 'HEAD'), 'ref') + const sessions = new FolderArchiveSessions() + + const started = await sessions.start({ + folderPath: project, + ownerKey: 'device-a', + excludeDefaultDirs: true + }) + expect(started).toMatchObject({ + suggestedName: 'project.zip', + fileCount: 2, + totalBytes: 15, + excludedDirCount: 2 + }) + const { bytes, last } = await drain(sessions, started.archiveId) + expect(last.processedBytes).toBe(15) + const files = unzipSync(bytes) + expect(Object.keys(files).sort()).toEqual([ + 'project/', + 'project/run.sh', + 'project/src/', + 'project/src/a.txt', + 'project/src/empty/' + ]) + expect(Buffer.from(files['project/src/a.txt']).toString()).toBe('hello') + expect(sessions.activeCount()).toBe(0) + }) + + it('includes dependency folders when exclusion is off', async () => { + await mkdir(join(root, 'node_modules'), { recursive: true }) + await writeFile(join(root, 'node_modules', 'x.js'), 'x') + const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: false }) + expect(plan.entries.map((entry) => entry.archivePath)).toContain('r/node_modules/x.js') + }) + + it('stores symlinks as links and never reads what they point at', async () => { + const outside = join(root, 'outside-secret.txt') + await writeFile(outside, 'secret') + const project = join(root, 'p') + await mkdir(project) + await symlink(outside, join(project, 'link')) + await symlink(root, join(project, 'dir-link')) + const plan = await planFolderArchive(project, 'p', { excludeDefaultDirs: true }) + expect(plan.totalBytes).toBe(0) + const stream = new FolderArchiveZipStream(plan.entries) + const parts: Buffer[] = [] + for (;;) { + const page = await stream.read(1 << 20) + parts.push(page.bytes) + if (page.eof) { + break + } + } + const files = unzipSync(Buffer.concat(parts)) + expect(Buffer.from(files['p/link']).toString()).toBe(outside) + expect(Object.keys(files).some((name) => name.includes('outside-secret'))).toBe(false) + expect(Object.keys(files)).not.toContain('p/dir-link/') + }) + + it('refuses folders over the byte or entry limits', async () => { + await writeFile(join(root, 'a'), Buffer.alloc(10)) + await writeFile(join(root, 'b'), Buffer.alloc(10)) + await expect( + planFolderArchive(root, 'r', { excludeDefaultDirs: true, maxTotalBytes: 15 }) + ).rejects.toEqual(new FolderArchiveLimitError('archive_too_large')) + await expect( + planFolderArchive(root, 'r', { excludeDefaultDirs: true, maxEntries: 2 }) + ).rejects.toEqual(new FolderArchiveLimitError('archive_too_many_entries')) + }) + + it('refuses to archive a file', async () => { + await writeFile(join(root, 'f'), 'x') + const sessions = new FolderArchiveSessions() + await expect( + sessions.start({ folderPath: join(root, 'f'), ownerKey: 'a', excludeDefaultDirs: true }) + ).rejects.toThrow('archive_not_directory') + }) + + it('hides an archive from other devices and frees it on cancel', async () => { + await writeFile(join(root, 'f'), 'x') + const sessions = new FolderArchiveSessions() + const { archiveId } = await sessions.start({ + folderPath: root, + ownerKey: 'device-a', + excludeDefaultDirs: true + }) + await expect(sessions.read({ archiveId, ownerKey: 'device-b', maxBytes: 10 })).rejects.toThrow( + 'archive_not_found' + ) + expect(sessions.cancel({ archiveId, ownerKey: 'device-b' })).toBe(false) + expect(sessions.cancel({ archiveId, ownerKey: 'device-a' })).toBe(true) + expect(sessions.activeCount()).toBe(0) + await expect(sessions.read({ archiveId, ownerKey: 'device-a', maxBytes: 10 })).rejects.toThrow( + 'archive_not_found' + ) + }) + + it('caps concurrent archives and releases idle ones', async () => { + const sessions = new FolderArchiveSessions(20, 1) + const first = await sessions.start({ + folderPath: root, + ownerKey: 'a', + excludeDefaultDirs: true + }) + await expect( + sessions.start({ folderPath: root, ownerKey: 'a', excludeDefaultDirs: true }) + ).rejects.toThrow('archive_busy') + await new Promise((resolve) => setTimeout(resolve, 60)) + expect(sessions.activeCount()).toBe(0) + await expect( + sessions.read({ archiveId: first.archiveId, ownerKey: 'a', maxBytes: 10 }) + ).rejects.toThrow('archive_not_found') + }) + + it('pauses the producer at the high-water mark until the reader drains', async () => { + await writeFile(join(root, 'big.png'), Buffer.alloc(4 * 1024 * 1024, 7)) + const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: true }) + const stream = new FolderArchiveZipStream(plan.entries, 512 * 1024) + await new Promise((resolve) => setTimeout(resolve, 50)) + expect(stream.processedBytes).toBeLessThanOrEqual(1024 * 1024) + let total = 0 + for (;;) { + const page = await stream.read(256 * 1024) + total += page.bytes.byteLength + if (page.eof) { + break + } + } + expect(stream.processedBytes).toBe(4 * 1024 * 1024) + expect(total).toBeGreaterThan(4 * 1024 * 1024) + }) +}) diff --git a/src/main/runtime/files/archive/folder-archive-sessions.ts b/src/main/runtime/files/archive/folder-archive-sessions.ts new file mode 100644 index 00000000000..9649e95e668 --- /dev/null +++ b/src/main/runtime/files/archive/folder-archive-sessions.ts @@ -0,0 +1,164 @@ +import { randomUUID } from 'node:crypto' +import { realpath } from 'node:fs/promises' +import { basename } from 'node:path' +import type { + FileArchiveReadResult, + FileArchiveStartResult +} from '../../../../shared/rpc-contract/files-archive-params' +import { planFolderArchive } from './folder-archive-walk' +import { FolderArchiveZipStream } from './folder-archive-zip-stream' + +// A lost cancel (closed laptop, dropped socket) must not pin host memory or file handles. +export const FOLDER_ARCHIVE_IDLE_TIMEOUT_MS = 2 * 60_000 +const MAX_ACTIVE_ARCHIVES = 4 + +type ArchiveSession = { + id: string + /** Only the connection that started an archive may read or cancel it. */ + ownerKey: string + stream: FolderArchiveZipStream + reading: boolean + idleTimer: ReturnType | null +} + +export class FolderArchiveSessions { + private readonly sessions = new Map() + + constructor( + private readonly idleTimeoutMs = FOLDER_ARCHIVE_IDLE_TIMEOUT_MS, + private readonly maxActive = MAX_ACTIVE_ARCHIVES + ) {} + + /** `folderPath` must already be confined to an allowed workspace root by the caller. */ + async start(input: { + folderPath: string + ownerKey: string + excludeDefaultDirs: boolean + signal?: AbortSignal + }): Promise { + if (this.sessions.size >= this.maxActive) { + throw new Error('archive_busy') + } + const rootName = sanitizeArchiveRootName(basename(await realpath(input.folderPath))) + const plan = await planFolderArchive(input.folderPath, rootName, { + excludeDefaultDirs: input.excludeDefaultDirs, + ...(input.signal ? { signal: input.signal } : {}) + }) + // Re-check after the walk: concurrent starts may have filled the slots meanwhile. + if (this.sessions.size >= this.maxActive) { + throw new Error('archive_busy') + } + const session: ArchiveSession = { + id: randomUUID(), + ownerKey: input.ownerKey, + stream: new FolderArchiveZipStream(plan.entries), + reading: false, + idleTimer: null + } + this.sessions.set(session.id, session) + this.armIdleTimer(session) + return { + archiveId: session.id, + suggestedName: `${rootName}.zip`, + fileCount: plan.fileCount, + directoryCount: plan.directoryCount, + totalBytes: plan.totalBytes, + excludedDirCount: plan.excludedDirCount + } + } + + async read(input: { + archiveId: string + ownerKey: string + maxBytes: number + }): Promise { + const session = this.requireSession(input.archiveId, input.ownerKey) + if (session.reading) { + throw new Error('archive_busy') + } + session.reading = true + this.clearIdleTimer(session) + try { + const page = await session.stream.read(input.maxBytes) + if (page.eof) { + this.release(session) + } + return { + contentBase64: page.bytes.toString('base64'), + bytesRead: page.bytes.byteLength, + eof: page.eof, + processedBytes: session.stream.processedBytes, + skippedCount: session.stream.skippedCount + } + } catch (error) { + this.release(session) + throw error + } finally { + session.reading = false + if (this.sessions.get(session.id) === session) { + this.armIdleTimer(session) + } + } + } + + cancel(input: { archiveId: string; ownerKey: string }): boolean { + const session = this.sessions.get(input.archiveId) + if (!session || session.ownerKey !== input.ownerKey) { + return false + } + this.release(session) + return true + } + + activeCount(): number { + return this.sessions.size + } + + private requireSession(archiveId: string, ownerKey: string): ArchiveSession { + const session = this.sessions.get(archiveId) + // Another connection's archive looks the same as a missing one. + if (!session || session.ownerKey !== ownerKey) { + throw new Error('archive_not_found') + } + return session + } + + private release(session: ArchiveSession): void { + this.clearIdleTimer(session) + if (this.sessions.get(session.id) === session) { + this.sessions.delete(session.id) + } + session.stream.cancel() + } + + private armIdleTimer(session: ArchiveSession): void { + this.clearIdleTimer(session) + session.idleTimer = setTimeout(() => this.release(session), this.idleTimeoutMs) + session.idleTimer.unref?.() + } + + private clearIdleTimer(session: ArchiveSession): void { + if (session.idleTimer) { + clearTimeout(session.idleTimer) + session.idleTimer = null + } + } +} + +function sanitizeArchiveRootName(name: string): string { + // A filesystem root has no basename; zip readers choke on separators or control characters here. + // oxlint-disable-next-line no-control-regex + const cleaned = name.replace(/[\u0000-\u001f/\\]/g, '_').trim() + return cleaned === '' || cleaned === '.' || cleaned === '..' ? 'folder' : cleaned +} + +const sessionsByRuntime = new WeakMap() + +export function getFolderArchiveSessions(runtime: object): FolderArchiveSessions { + let sessions = sessionsByRuntime.get(runtime) + if (!sessions) { + sessions = new FolderArchiveSessions() + sessionsByRuntime.set(runtime, sessions) + } + return sessions +} diff --git a/src/main/runtime/files/archive/folder-archive-walk.ts b/src/main/runtime/files/archive/folder-archive-walk.ts new file mode 100644 index 00000000000..dee15d584d0 --- /dev/null +++ b/src/main/runtime/files/archive/folder-archive-walk.ts @@ -0,0 +1,189 @@ +import { lstat, readdir, realpath } from 'node:fs/promises' +import { join } from 'node:path' +import { + FOLDER_ARCHIVE_DEFAULT_EXCLUDED_NAMES, + FOLDER_ARCHIVE_MAX_ENTRIES, + FOLDER_ARCHIVE_MAX_TOTAL_BYTES +} from '../../../../shared/rpc-contract/files-archive-params' + +export type FolderArchiveEntry = { + /** Path inside the archive, `/`-separated, prefixed by the archived folder's name. */ + archivePath: string + absolutePath: string + kind: 'file' | 'directory' | 'symlink' + size: number + mode: number + mtimeMs: number +} + +export type FolderArchivePlan = { + entries: FolderArchiveEntry[] + fileCount: number + directoryCount: number + totalBytes: number + excludedDirCount: number +} + +export class FolderArchiveLimitError extends Error { + constructor(readonly code: 'archive_too_large' | 'archive_too_many_entries') { + super(code) + this.name = 'FolderArchiveLimitError' + } +} + +export type FolderArchiveWalkOptions = { + excludeDefaultDirs: boolean + maxTotalBytes?: number + maxEntries?: number + signal?: AbortSignal +} + +/** + * Lists a folder for archiving without ever following a symlink, so every entry stays under + * the root's real path. A directory swapped for a link mid-walk fails its realpath check and is skipped. + */ +export async function planFolderArchive( + rootPath: string, + archiveRootName: string, + options: FolderArchiveWalkOptions +): Promise { + // Child realpath checks compare against this, so the root itself must be canonical. + const rootRealPath = await realpath(rootPath) + const maxTotalBytes = options.maxTotalBytes ?? FOLDER_ARCHIVE_MAX_TOTAL_BYTES + const maxEntries = options.maxEntries ?? FOLDER_ARCHIVE_MAX_ENTRIES + const excluded = new Set( + options.excludeDefaultDirs ? FOLDER_ARCHIVE_DEFAULT_EXCLUDED_NAMES : [] + ) + const rootStat = await lstat(rootRealPath) + if (!rootStat.isDirectory()) { + throw new Error('archive_not_directory') + } + const plan: FolderArchivePlan = { + entries: [ + { + archivePath: `${archiveRootName}/`, + absolutePath: rootRealPath, + kind: 'directory', + size: 0, + mode: rootStat.mode, + mtimeMs: rootStat.mtimeMs + } + ], + fileCount: 0, + directoryCount: 1, + totalBytes: 0, + excludedDirCount: 0 + } + // Depth-first with an explicit stack: deep trees must not grow the JS call stack. + const pending: { absolutePath: string; archivePath: string }[] = [ + { absolutePath: rootRealPath, archivePath: `${archiveRootName}/` } + ] + while (pending.length > 0) { + if (options.signal?.aborted) { + throw new Error('archive_canceled') + } + const dir = pending.pop()! + if (dir.absolutePath !== rootRealPath && !(await isUnmovedDirectory(dir.absolutePath))) { + continue + } + let names: string[] + try { + names = await readdir(dir.absolutePath) + } catch (error) { + if (isVanishedOrDenied(error)) { + continue + } + throw error + } + names.sort() + const stats = await Promise.all( + names.map(async (name) => { + try { + return { name, stat: await lstat(join(dir.absolutePath, name)) } + } catch (error) { + if (isVanishedOrDenied(error)) { + return null + } + throw error + } + }) + ) + const childDirs: { absolutePath: string; archivePath: string }[] = [] + for (const item of stats) { + if (!item) { + continue + } + const { name, stat } = item + const absolutePath = join(dir.absolutePath, name) + const archivePath = `${dir.archivePath}${name}` + if (stat.isDirectory()) { + if (excluded.has(name)) { + plan.excludedDirCount += 1 + continue + } + plan.directoryCount += 1 + plan.entries.push({ + archivePath: `${archivePath}/`, + absolutePath, + kind: 'directory', + size: 0, + mode: stat.mode, + mtimeMs: stat.mtimeMs + }) + childDirs.push({ absolutePath, archivePath: `${archivePath}/` }) + } else if (stat.isFile() || stat.isSymbolicLink()) { + const kind = stat.isFile() ? 'file' : 'symlink' + if (kind === 'file') { + plan.fileCount += 1 + plan.totalBytes += stat.size + if (plan.totalBytes > maxTotalBytes) { + throw new FolderArchiveLimitError('archive_too_large') + } + } + plan.entries.push({ + archivePath, + absolutePath, + kind, + size: kind === 'file' ? stat.size : 0, + mode: stat.mode, + mtimeMs: stat.mtimeMs + }) + } + // Sockets, FIFOs and devices have no archivable content. + if (plan.entries.length > maxEntries) { + throw new FolderArchiveLimitError('archive_too_many_entries') + } + } + // Reverse so the stack pops children in sorted order. + for (let index = childDirs.length - 1; index >= 0; index -= 1) { + pending.push(childDirs[index]) + } + } + return plan +} + +async function isUnmovedDirectory(absolutePath: string): Promise { + try { + const resolved = await realpath(absolutePath) + // Windows paths compare case-insensitively; realpath may report a different casing. + return process.platform === 'win32' + ? resolved.toLowerCase() === absolutePath.toLowerCase() + : resolved === absolutePath + } catch (error) { + if (isVanishedOrDenied(error)) { + return false + } + throw error + } +} + +export function isVanishedOrDenied(error: unknown): boolean { + const code = (error as NodeJS.ErrnoException | null)?.code + return ( + code === 'ENOENT' || + code === 'ENOTDIR' || + code === 'EACCES' || + code === 'EPERM' || + code === 'ELOOP' + ) +} diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts new file mode 100644 index 00000000000..c40103a598d --- /dev/null +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -0,0 +1,252 @@ +import { constants } from 'node:fs' +import { open, readlink } from 'node:fs/promises' +import { extname } from 'node:path' +import { Zip, ZipDeflate, ZipPassThrough, type ZipInputFile } from 'fflate' +import { isVanishedOrDenied, type FolderArchiveEntry } from './folder-archive-walk' + +const FILE_READ_BYTES = 256 * 1024 +/** Encoded bytes buffered ahead of the reader before the producer pauses. */ +const DEFAULT_HIGH_WATER_BYTES = 8 * 1024 * 1024 +// Level 4 keeps pure-JS deflate fast enough not to starve a fast link while still shrinking text. +const DEFLATE_LEVEL = 4 +const UNIX_OS = 3 +const S_IFLNK = 0o120000 +// Recompressing these costs CPU for no size win. +const STORED_EXTENSIONS = new Set([ + '.7z', + '.avif', + '.br', + '.bz2', + '.dmg', + '.gif', + '.gz', + '.heic', + '.jar', + '.jpeg', + '.jpg', + '.m4a', + '.mkv', + '.mov', + '.mp3', + '.mp4', + '.ogg', + '.png', + '.rar', + '.tgz', + '.webm', + '.webp', + '.whl', + '.woff', + '.woff2', + '.xz', + '.zip', + '.zst' +]) + +/** + * Turns a planned entry list into ZIP bytes on demand. The producer runs ahead of the reader by at + * most `highWaterBytes`, so a slow link never makes the host buffer a whole folder. + */ +export class FolderArchiveZipStream { + private readonly chunks: Uint8Array[] = [] + private bufferedBytes = 0 + private finished = false + private failure: Error | null = null + private canceled = false + private wakeReader: (() => void) | null = null + private wakeProducer: (() => void) | null = null + private readonly zip: Zip + processedBytes = 0 + skippedCount = 0 + + constructor( + private readonly entries: readonly FolderArchiveEntry[], + private readonly highWaterBytes = DEFAULT_HIGH_WATER_BYTES + ) { + this.zip = new Zip((error, data, final) => { + if (error) { + this.fail(error) + return + } + if (data.byteLength > 0) { + this.chunks.push(data) + this.bufferedBytes += data.byteLength + } + if (final) { + this.finished = true + } + this.notifyReader() + }) + void this.produce() + } + + /** Resolves with up to `maxBytes` once that much is buffered or the archive ends. */ + async read(maxBytes: number): Promise<{ bytes: Buffer; eof: boolean }> { + while (!this.failure && !this.finished && this.bufferedBytes < maxBytes) { + await new Promise((resolve) => { + this.wakeReader = resolve + }) + } + if (this.failure) { + throw this.failure + } + const parts: Uint8Array[] = [] + let taken = 0 + while (this.chunks.length > 0 && taken < maxBytes) { + const head = this.chunks[0] + const room = maxBytes - taken + if (head.byteLength <= room) { + parts.push(head) + taken += head.byteLength + this.chunks.shift() + } else { + parts.push(head.subarray(0, room)) + this.chunks[0] = head.subarray(room) + taken += room + } + } + this.bufferedBytes -= taken + this.notifyProducer() + return { + bytes: Buffer.concat(parts, taken), + eof: this.finished && this.chunks.length === 0 + } + } + + cancel(): void { + this.canceled = true + this.zip.terminate() + this.chunks.length = 0 + this.bufferedBytes = 0 + this.fail(new Error('archive_canceled')) + this.notifyProducer() + } + + private async produce(): Promise { + try { + for (const entry of this.entries) { + if (this.canceled) { + return + } + await this.addEntry(entry) + } + this.zip.end() + } catch (error) { + this.fail(error instanceof Error ? error : new Error(String(error))) + } + } + + private async addEntry(entry: FolderArchiveEntry): Promise { + if (entry.kind === 'directory') { + const file = withAttributes(new ZipPassThrough(entry.archivePath), entry, entry.mode) + this.zip.add(file) + file.push(new Uint8Array(0), true) + return + } + if (entry.kind === 'symlink') { + let target: string + try { + target = await readlink(entry.absolutePath) + } catch (error) { + return this.skip(error) + } + // Stored as a link, never dereferenced: the target may name anything on the host. + const file = withAttributes( + new ZipPassThrough(entry.archivePath), + entry, + S_IFLNK | (entry.mode & 0o777) + ) + this.zip.add(file) + file.push(new TextEncoder().encode(target), true) + return + } + let handle: Awaited> + try { + handle = await open(entry.absolutePath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)) + } catch (error) { + return this.skip(error) + } + try { + // A file swapped for something else after the walk is skipped, not archived. + if (!(await handle.stat()).isFile()) { + this.skippedCount += 1 + return + } + const file = withAttributes( + STORED_EXTENSIONS.has(extname(entry.archivePath).toLowerCase()) + ? new ZipPassThrough(entry.archivePath) + : new ZipDeflate(entry.archivePath, { level: DEFLATE_LEVEL }), + entry, + entry.mode + ) + this.zip.add(file) + const buffer = Buffer.allocUnsafe(FILE_READ_BYTES) + for (;;) { + await this.waitForRoom() + if (this.canceled) { + return + } + const { bytesRead } = await handle.read(buffer, 0, FILE_READ_BYTES, null) + if (bytesRead === 0) { + file.push(new Uint8Array(0), true) + return + } + this.processedBytes += bytesRead + // fflate may keep a reference until it deflates, so hand it a copy. + file.push(Uint8Array.prototype.slice.call(buffer, 0, bytesRead) as Uint8Array, false) + } + } finally { + await handle.close().catch(() => {}) + } + } + + private skip(error: unknown): void { + if (!isVanishedOrDenied(error)) { + throw error + } + this.skippedCount += 1 + } + + private async waitForRoom(): Promise { + while (!this.canceled && this.bufferedBytes >= this.highWaterBytes) { + await new Promise((resolve) => { + this.wakeProducer = resolve + }) + } + } + + private fail(error: Error): void { + this.failure ??= error + this.notifyReader() + } + + private notifyReader(): void { + const wake = this.wakeReader + this.wakeReader = null + wake?.() + } + + private notifyProducer(): void { + const wake = this.wakeProducer + this.wakeProducer = null + wake?.() + } +} + +function withAttributes( + file: T, + entry: FolderArchiveEntry, + mode: number +): T { + file.os = UNIX_OS + file.attrs = (mode & 0xffff) << 16 + file.mtime = clampZipMtime(entry.mtimeMs) + return file +} + +// DOS timestamps cover 1980-2107; fflate throws outside that range. +function clampZipMtime(mtimeMs: number): number { + const min = Date.UTC(1980, 0, 2) + const max = Date.UTC(2107, 11, 30) + return Math.min(Math.max(mtimeMs, min), max) +} diff --git a/src/main/runtime/files/file-reads.ts b/src/main/runtime/files/file-reads.ts index f84056c724b..9c02e16c2c6 100644 --- a/src/main/runtime/files/file-reads.ts +++ b/src/main/runtime/files/file-reads.ts @@ -198,6 +198,16 @@ export class RuntimeFileReads { return readLocalFileRange(filePath, offset, length) } + /** A workspace folder's real path for archiving, under the same containment as chunk reads. */ + async resolveFileExplorerArchivePath( + worktreeSelector: string, + relativePath: string + ): Promise { + const target = await resolveFileExplorerPath(this.host, worktreeSelector, relativePath) + requireLocalRuntimeFileRoute(target) + return resolveRuntimeAuthorizedPath(target.path, this.host.requireStore()) + } + /** A host image a chat names, under desktop's chat-image rule; the path must be on this machine. */ async readChatImagePreview( worktreeSelector: string, diff --git a/src/main/runtime/rpc/methods/files/files-archive-methods.ts b/src/main/runtime/rpc/methods/files/files-archive-methods.ts new file mode 100644 index 00000000000..037b871ce55 --- /dev/null +++ b/src/main/runtime/rpc/methods/files/files-archive-methods.ts @@ -0,0 +1,58 @@ +import { defineMethod, type RpcContext } from '../../core' +import { + FileArchiveCancel, + FileArchiveRead, + FileArchiveStart +} from '../../../../../shared/rpc-contract/files-archive-params' +import { getFolderArchiveSessions } from '../../../files/archive/folder-archive-sessions' + +// Why device identity: one-shot calls from the same desktop may arrive on different sockets. +function archiveOwnerKey( + context: Pick +) { + return context.pairedDeviceId ?? context.clientId ?? context.connectionId ?? 'in-process' +} + +/** Folder downloads: the host zips a workspace folder and the client pages the bytes. */ +export const FILE_ARCHIVE_METHODS = [ + defineMethod({ + name: 'files.archiveStart', + permission: 'workspace', + params: FileArchiveStart, + handler: async (params, context) => { + // Same containment as every files.* read: workspace-relative, then authorized-root checked. + const folderPath = await context.runtime.resolveFileExplorerArchivePath( + params.worktree, + params.relativePath + ) + return getFolderArchiveSessions(context.runtime).start({ + folderPath, + ownerKey: archiveOwnerKey(context), + excludeDefaultDirs: params.excludeDefaultDirs !== false, + ...(context.signal ? { signal: context.signal } : {}) + }) + } + }), + defineMethod({ + name: 'files.archiveRead', + permission: 'workspace', + params: FileArchiveRead, + handler: async (params, context) => + getFolderArchiveSessions(context.runtime).read({ + archiveId: params.archiveId, + ownerKey: archiveOwnerKey(context), + maxBytes: params.maxBytes + }) + }), + defineMethod({ + name: 'files.archiveCancel', + permission: 'workspace', + params: FileArchiveCancel, + handler: async (params, context) => ({ + released: getFolderArchiveSessions(context.runtime).cancel({ + archiveId: params.archiveId, + ownerKey: archiveOwnerKey(context) + }) + }) + }) +] diff --git a/src/main/runtime/rpc/methods/files/files.ts b/src/main/runtime/rpc/methods/files/files.ts index 56c8c93d755..671fa50c1f2 100644 --- a/src/main/runtime/rpc/methods/files/files.ts +++ b/src/main/runtime/rpc/methods/files/files.ts @@ -1,5 +1,6 @@ import { defineMethod, defineStreamingMethod } from '../../core' import { runFileWatchStream } from './file-watch-stream-lifecycle' +import { FILE_ARCHIVE_METHODS } from './files-archive-methods' import { FILE_MUTATION_METHODS } from './files-mutation-methods' import { dirListingPageCache } from './files-read-dir-page' import { FILE_STAGED_SAVE_METHODS } from './files-staged-save-methods' @@ -210,6 +211,7 @@ export const FILE_METHODS = [ }), ...FILE_MUTATION_METHODS, ...FILE_STAGED_SAVE_METHODS, + ...FILE_ARCHIVE_METHODS, defineMethod({ name: 'files.search', permission: 'workspace', diff --git a/src/preload/api-types.ts b/src/preload/api-types.ts index 43b3290ac25..e4979e756be 100644 --- a/src/preload/api-types.ts +++ b/src/preload/api-types.ts @@ -71,6 +71,7 @@ import type { ShellApi } from './api/shell-api' import type { SpeechApi } from './api/speech-api' import type { SshApi } from './api/ssh-api' import type { PlainSshTerminalApi } from './api/plain-ssh-terminal-bridge' +import type { RuntimeDownloadsApi } from './api/runtime-downloads-bridge' import type { LegacyRelayBridgeApi } from './api/legacy-relay-bridge-api' import type { DiagnosticsApi, MemoryApi, StatsApi, TelemetryApi } from './api/telemetry-api' import type { UiCommandEventApi } from './api/ui-command-event-api' @@ -180,6 +181,8 @@ export type PreloadApi = { ssh: SshApi /** Desktop only: an unmanaged ssh login to a host whose move is blocked. */ plainSsh?: PlainSshTerminalApi + /** Desktop only: streams files and folders from a paired runtime host to this machine. */ + runtimeDownloads?: RuntimeDownloadsApi /** Desktop only: the API keys Orca hands to agent launches. */ agentApiKeys?: AgentApiKeysApi /** Desktop only: Orca Cloud API keys, org settings and routines (hosted builds). */ diff --git a/src/preload/api/runtime-downloads-bridge.ts b/src/preload/api/runtime-downloads-bridge.ts new file mode 100644 index 00000000000..1f4b3c25a35 --- /dev/null +++ b/src/preload/api/runtime-downloads-bridge.ts @@ -0,0 +1,24 @@ +import { ipcRenderer } from 'electron' +import type { + RuntimeDownloadProgressEvent, + RuntimeDownloadResult, + RuntimeDownloadStartArgs +} from '../../shared/runtime-downloads/runtime-download-contract' + +export type RuntimeDownloadsApi = { + /** Resolves when the download finishes, fails or is canceled. */ + start: (args: RuntimeDownloadStartArgs) => Promise + cancel: (args: { downloadId: string }) => Promise<{ canceled: boolean }> + onProgress: (callback: (event: RuntimeDownloadProgressEvent) => void) => () => void +} + +export const runtimeDownloadsApi: RuntimeDownloadsApi = { + start: (args) => ipcRenderer.invoke('runtimeDownloads:start', args), + cancel: (args) => ipcRenderer.invoke('runtimeDownloads:cancel', args), + onProgress: (callback) => { + const listener = (_event: Electron.IpcRendererEvent, payload: RuntimeDownloadProgressEvent) => + callback(payload) + ipcRenderer.on('runtimeDownloads:progress', listener) + return () => ipcRenderer.removeListener('runtimeDownloads:progress', listener) + } +} diff --git a/src/preload/index.ts b/src/preload/index.ts index 6cc4d785126..96045137488 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -91,6 +91,7 @@ import { grokAccountsApi } from './api/grok-accounts-bridge' import { cursorAccountsApi } from './api/cursor-accounts-bridge' import { sshApi } from './api/ssh-bridge' import { plainSshTerminalApi } from './api/plain-ssh-terminal-bridge' +import { runtimeDownloadsApi } from './api/runtime-downloads-bridge' import { agentApiKeysApi } from './api/agent-api-keys-bridge' import { cloudApiBridge } from './api/cloud-api-bridge' import { cloudSecretsApi } from './api/cloud-secrets-bridge' @@ -205,6 +206,7 @@ const api = { cursorAccounts: cursorAccountsApi, ssh: sshApi, plainSsh: plainSshTerminalApi, + runtimeDownloads: runtimeDownloadsApi, agentApiKeys: agentApiKeysApi, cloudApi: cloudApiBridge, cloudSecrets: cloudSecretsApi, diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts index 36d538db94c..17776c71b1f 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts @@ -22,11 +22,13 @@ export function shouldShowRemoteDownloadAction( connectionId?: string | null, runtimeDownloadContext?: RuntimeFileOperationArgs | null, // Require advertised SFTP recursive transfer; unknown and system-SSH folder downloads stay hidden. - supportsFolderDownload = false + supportsFolderDownload = false, + // Desktop main can stream from the runtime host directly, including host-zipped folders. + canStreamFromRuntime = false ): boolean { - // Native save dialogs require desktop; runtime/system-SSH folders lack recursive transfer contracts. + // Native save dialogs require desktop; system-SSH folders lack a recursive transfer contract. const hasDownloadCapability = node.isDirectory - ? Boolean(connectionId && supportsFolderDownload) + ? Boolean(connectionId ? supportsFolderDownload : canStreamFromRuntime) : Boolean(connectionId || runtimeDownloadContext) return ( hasDownloadCapability && @@ -34,6 +36,15 @@ export function shouldShowRemoteDownloadAction( ) } +/** Fetch a runtime-hosted file into a temp copy and open it with the local default app. */ +export function shouldShowOpenLocallyAction( + node: TreeNode, + connectionId: string | null | undefined, + canStreamFromRuntime: boolean +): boolean { + return !node.isDirectory && !connectionId && canStreamFromRuntime +} + export function shouldShowCopyFileAction( node: TreeNode, connectionId?: string | null, diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx index ffeed794e48..9cb77482fc2 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx @@ -11,6 +11,7 @@ import { FolderPlus, Globe, ListCollapse, + MonitorDown, Pencil, Search, SquareTerminal, @@ -43,11 +44,13 @@ import { shouldShowCopyFileAction, shouldShowFindInFolderAction, shouldShowOpenInTerminalAction, + shouldShowOpenLocallyAction, shouldShowRemoteDownloadAction, shouldShowViewFileAction } from './file-explorer-row-action-visibility' import { copyFileToOsClipboard, downloadRemoteFile } from './file-explorer-row-file-transfer' import { fileExplorerNodeTabRelativePath } from '../tree/file-explorer-directory-listing' +import { canStreamRuntimeDownload, startRuntimeDownload } from './runtime-download-actions' const isMac = navigator.userAgent.includes('Mac') @@ -107,11 +110,19 @@ export function FileExplorerRowContextMenu({ const copyPathShortcutLabel = useShortcutLabel('fileExplorer.copyPath') const copyRelativePathShortcutLabel = useShortcutLabel('fileExplorer.copyRelativePath') const findInFolderShortcutLabel = useShortcutLabel('sidebar.search.toggle') + const canStreamFromRuntime = + !connectionId && canStreamRuntimeDownload(node, runtimeDownloadContext) const showRemoteDownloadAction = shouldShowRemoteDownloadAction( node, connectionId, runtimeDownloadContext, - supportsFolderDownload + supportsFolderDownload, + canStreamFromRuntime + ) + const showOpenLocallyAction = shouldShowOpenLocallyAction( + node, + connectionId, + canStreamFromRuntime ) const showCopyFileAction = shouldShowCopyFileAction(node, connectionId, selectionSize) const revealOwner = useAppStore((s) => @@ -133,12 +144,21 @@ export function FileExplorerRowContextMenu({ } }, [activeWorktreeId, node.path]) const handleDownload = useCallback(() => { + if (canStreamFromRuntime && runtimeDownloadContext) { + void startRuntimeDownload(node, runtimeDownloadContext, 'save') + return + } const downloadTarget = connectionId || runtimeDownloadContext if (!downloadTarget) { return } void downloadRemoteFile(node, downloadTarget) - }, [connectionId, node, runtimeDownloadContext]) + }, [canStreamFromRuntime, connectionId, node, runtimeDownloadContext]) + const handleOpenLocally = useCallback(() => { + if (runtimeDownloadContext) { + void startRuntimeDownload(node, runtimeDownloadContext, 'open') + } + }, [node, runtimeDownloadContext]) const handleCopyFile = useCallback(() => { void copyFileToOsClipboard(node, connectionId) }, [connectionId, node]) @@ -237,10 +257,21 @@ export function FileExplorerRowContextMenu({ {node.isDirectory - ? t('auto.components.right.sidebar.FileExplorerRow.7ac885bd2f', 'Download Folder') + ? canStreamFromRuntime + ? t( + 'auto.components.right.sidebar.FileExplorerRow.downloadFolderZip', + 'Download as ZIP…' + ) + : t('auto.components.right.sidebar.FileExplorerRow.7ac885bd2f', 'Download Folder') : t('auto.components.right.sidebar.FileExplorerRow.c2112579f6', 'Download')} )} + {showOpenLocallyAction && ( + + + {t('auto.components.right.sidebar.FileExplorerRow.openLocally', 'Open on This Computer')} + + )} {canCollapseFolderSubtree && shouldShowCollapseFolderAction(node, isExpanded) && ( diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx new file mode 100644 index 00000000000..641f16e7ac1 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx @@ -0,0 +1,225 @@ +import { toast } from 'sonner' +import { createNonSecureContextUuid } from '../../../../../../shared/non-secure-context-uuid' +import { translate } from '@/i18n/i18n' +import { getRevealInFileManagerLabel, revealInFileManager } from '@/lib/reveal-in-file-manager' +import { getRemoteFileArgs } from '@/runtime/files/runtime-file-routing' +import type { RuntimeFileOperationArgs } from '@/runtime/files/runtime-file-client-types' +import { captureRuntimeEnvironmentRequestRevision } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' +import { trackRuntimeDownload } from '@/runtime/files/downloads/runtime-download-progress-store' +import { LOCAL_EXECUTION_HOST_ID } from '../../../../../../shared/execution-host' +import { formatBytes } from '../../../../../../shared/format-bytes' +import type { RuntimeDownloadResult } from '../../../../../../shared/runtime-downloads/runtime-download-contract' +import type { TreeNode } from '../tree/file-explorer-types' +import { RuntimeDownloadProgressBody, runtimeDownloadErrorMessage } from './runtime-download-toast' + +export type RuntimeDownloadIntent = 'save' | 'open' + +/** Whether this desktop can stream the node straight from its runtime host. */ +export function canStreamRuntimeDownload( + node: TreeNode, + context: RuntimeFileOperationArgs | null | undefined +): context is RuntimeFileOperationArgs { + return Boolean( + context && window.api.runtimeDownloads && getRemoteFileArgs(context, node.path) !== null + ) +} + +function localName(destinationPath: string): string { + const separator = window.api.platform.get().platform === 'win32' ? /[\\/]/ : /\// + return destinationPath.split(separator).pop() || destinationPath +} + +export async function startRuntimeDownload( + node: TreeNode, + context: RuntimeFileOperationArgs, + intent: RuntimeDownloadIntent, + options: { excludeDefaultDirs?: boolean } = {} +): Promise { + const api = window.api.runtimeDownloads + const remote = getRemoteFileArgs(context, node.path) + if (!api || !remote) { + return + } + const kind = node.isDirectory ? 'folder' : 'file' + const downloadId = createNonSecureContextUuid() + const toastId = `runtime-download:${downloadId}` + const release = trackRuntimeDownload(downloadId) + const excludeDefaultDirs = options.excludeDefaultDirs !== false + const pending = api.start({ + downloadId, + environmentId: remote.target.environmentId, + worktree: remote.worktreeSelector, + relativePath: remote.relativePath, + name: node.name, + kind, + intent, + excludeDefaultDirs, + expectedEnvironmentPairingRevision: captureRuntimeEnvironmentRequestRevision( + remote.target.environmentId + ) + }) + // The save dialog comes first; show progress only after the user has picked a place. + const showProgress = (): void => { + toast.loading( + intent === 'open' + ? translate( + 'auto.components.right.sidebar.runtimeDownload.opening', + "Opening '{{value0}}'", + { + value0: node.name + } + ) + : translate( + 'auto.components.right.sidebar.runtimeDownload.downloading', + "Downloading '{{value0}}'", + { value0: kind === 'folder' ? `${node.name}.zip` : node.name } + ), + { + id: toastId, + description: , + duration: Infinity, + dismissible: false, + action: { + label: translate('auto.components.right.sidebar.runtimeDownload.cancel', 'Cancel'), + onClick: (event) => { + event.preventDefault() + void api.cancel({ downloadId }) + } + } + } + ) + } + const stopProgressWatch = window.api.runtimeDownloads!.onProgress((event) => { + if (event.downloadId === downloadId) { + stopProgressWatch() + showProgress() + } + }) + let result: RuntimeDownloadResult + try { + result = await pending + } catch (error) { + result = { + status: 'error', + code: 'transfer_failed', + message: error instanceof Error ? error.message : String(error) + } + } finally { + stopProgressWatch() + release() + } + showOutcome(result, { node, kind, intent, toastId, context, excludeDefaultDirs }) +} + +function showOutcome( + result: RuntimeDownloadResult, + input: { + node: TreeNode + kind: 'file' | 'folder' + intent: RuntimeDownloadIntent + toastId: string + context: RuntimeFileOperationArgs + excludeDefaultDirs: boolean + } +): void { + const { node, kind, intent, toastId } = input + if (result.status === 'canceled') { + toast.dismiss(toastId) + return + } + if (result.status === 'error') { + toast.error( + intent === 'open' + ? translate( + 'auto.components.right.sidebar.runtimeDownload.openFailed', + "Couldn't open '{{value0}}'", + { value0: node.name } + ) + : translate( + 'auto.components.right.sidebar.runtimeDownload.failed', + "Couldn't download '{{value0}}'", + { value0: node.name } + ), + { + id: toastId, + description: runtimeDownloadErrorMessage(result.code, result.message), + duration: 8000, + dismissible: true, + action: undefined + } + ) + return + } + const reveal = { + label: getRevealInFileManagerLabel(), + onClick: () => void revealInFileManager(result.destinationPath, LOCAL_EXECUTION_HOST_ID) + } + if (intent === 'open') { + if (!result.openError) { + toast.dismiss(toastId) + return + } + toast.error( + translate( + 'auto.components.right.sidebar.runtimeDownload.noAppToOpen', + "No app could open '{{value0}}'", + { value0: node.name } + ), + { + id: toastId, + description: result.openError, + duration: 8000, + dismissible: true, + action: reveal + } + ) + return + } + const parts = [formatBytes(result.bytes)] + if (kind === 'folder' && result.fileCount !== undefined) { + parts.push( + translate('auto.components.right.sidebar.runtimeDownload.fileCount', '{{count}} files', { + count: result.fileCount, + defaultValue_one: '{{count}} file' + }) + ) + } + if (kind === 'folder' && (result.excludedDirCount ?? 0) > 0) { + parts.push( + translate( + 'auto.components.right.sidebar.runtimeDownload.excludedDefaultDirs', + 'without node_modules and .git' + ) + ) + } + if ((result.skippedCount ?? 0) > 0) { + parts.push( + translate( + 'auto.components.right.sidebar.runtimeDownload.skipped', + '{{count}} unreadable items skipped', + { count: result.skippedCount, defaultValue_one: '{{count}} unreadable item skipped' } + ) + ) + } + toast.success( + translate('auto.components.right.sidebar.runtimeDownload.done', "Downloaded '{{value0}}'", { + value0: localName(result.destinationPath) + }), + { + id: toastId, + description: parts.join(' · '), + duration: 8000, + dismissible: true, + action: reveal, + ...(kind === 'file' + ? { + cancel: { + label: translate('auto.components.right.sidebar.runtimeDownload.open', 'Open'), + onClick: () => + void window.api.shell.openPath(result.destinationPath, LOCAL_EXECUTION_HOST_ID) + } + } + : {}) + } + ) +} diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx new file mode 100644 index 00000000000..69c7a217f92 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx @@ -0,0 +1,139 @@ +import { useSyncExternalStore } from 'react' +import { Progress } from '@/components/ui/progress' +import { useTranslate } from '@/i18n/use-translate' +import { translate } from '@/i18n/i18n' +import { formatBytes } from '../../../../../../shared/format-bytes' +import type { RuntimeDownloadErrorCode } from '../../../../../../shared/runtime-downloads/runtime-download-contract' +import { + getRuntimeDownloadProgress, + subscribeRuntimeDownloadProgress, + type RuntimeDownloadProgressSnapshot +} from '@/runtime/files/downloads/runtime-download-progress-store' + +/** Fraction done, or null while the size is unknown (folder walk, empty file). */ +export function runtimeDownloadFraction( + snapshot: RuntimeDownloadProgressSnapshot | null +): number | null { + if (!snapshot || snapshot.phase !== 'transferring' || !snapshot.totalBytes) { + return null + } + return Math.min(1, snapshot.processedBytes / snapshot.totalBytes) +} + +export function RuntimeDownloadProgressBody({ + downloadId, + kind +}: { + downloadId: string + kind: 'file' | 'folder' +}): React.JSX.Element { + const t = useTranslate() + const snapshot = useSyncExternalStore( + (listener) => subscribeRuntimeDownloadProgress(downloadId, listener), + () => getRuntimeDownloadProgress(downloadId) + ) + const fraction = runtimeDownloadFraction(snapshot) + const percent = fraction === null ? null : Math.round(fraction * 100) + const progressLabel = t( + 'auto.components.right.sidebar.runtimeDownload.progressLabel', + 'Download progress' + ) + let detail: string + if (!snapshot || snapshot.phase === 'preparing') { + detail = + kind === 'folder' + ? t('auto.components.right.sidebar.runtimeDownload.preparingFolder', 'Collecting files…') + : t('auto.components.right.sidebar.runtimeDownload.preparingFile', 'Starting…') + } else { + const received = formatBytes(snapshot.receivedBytes) + const rate = + snapshot.bytesPerSecond && snapshot.bytesPerSecond > 0 + ? t('auto.components.right.sidebar.runtimeDownload.rate', '{{value0}}/s', { + value0: formatBytes(snapshot.bytesPerSecond) + }) + : null + const amount = + kind === 'file' && snapshot.totalBytes + ? t( + 'auto.components.right.sidebar.runtimeDownload.amountOfTotal', + '{{value0}} of {{value1}}', + { + value0: received, + value1: formatBytes(snapshot.totalBytes) + } + ) + : received + detail = rate ? `${amount} · ${rate}` : amount + } + return ( +
+ {percent === null ? ( + // Unknown size: an honest sweep beats a faked percentage. + + + + ) : ( + + )} +

{detail}

+
+ ) +} + +export function runtimeDownloadErrorMessage( + code: RuntimeDownloadErrorCode, + hostMessage: string +): string { + switch (code) { + case 'too_large': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorTooLarge', + 'It is larger than the 2 GB download limit. Download smaller folders or individual files.' + ) + case 'too_many_entries': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorTooManyEntries', + 'It has more than 65,000 files and folders. Download a smaller folder.' + ) + case 'host_update_required': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorHostUpdate', + 'The remote host runs an older Orca server that cannot do this. Update it and try again.' + ) + case 'host_busy': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorBusy', + 'Too many downloads are running. Wait for one to finish and try again.' + ) + case 'not_found': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorNotFound', + 'It no longer exists on the remote host.' + ) + case 'not_a_file': + case 'not_a_directory': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorChanged', + 'It changed on the remote host. Refresh the file explorer and try again.' + ) + case 'disconnected': + return translate( + 'auto.components.right.sidebar.runtimeDownload.errorDisconnected', + 'The connection to the remote host was lost. Reconnect and try again.' + ) + case 'open_failed': + case 'transfer_failed': + case 'canceled': + return hostMessage + } +} diff --git a/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts b/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts new file mode 100644 index 00000000000..51537080a02 --- /dev/null +++ b/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts @@ -0,0 +1,84 @@ +import type { RuntimeDownloadProgressEvent } from '../../../../../shared/runtime-downloads/runtime-download-contract' + +export type RuntimeDownloadProgressSnapshot = RuntimeDownloadProgressEvent & { + /** Recent receive rate, smoothed so the label does not jitter. */ + bytesPerSecond: number | null +} + +type Entry = { + snapshot: RuntimeDownloadProgressSnapshot | null + listeners: Set<() => void> + lastSample: { at: number; bytes: number } | null +} + +const entries = new Map() +let unsubscribeIpc: (() => void) | null = null +// Rate is averaged over this window; shorter windows swing with every page. +const RATE_SMOOTHING = 0.3 + +function entryFor(downloadId: string): Entry { + let entry = entries.get(downloadId) + if (!entry) { + entry = { snapshot: null, listeners: new Set(), lastSample: null } + entries.set(downloadId, entry) + } + return entry +} + +export function applyRuntimeDownloadProgress( + event: RuntimeDownloadProgressEvent, + now = performance.now() +): void { + const entry = entries.get(event.downloadId) + if (!entry) { + return + } + let bytesPerSecond = entry.snapshot?.bytesPerSecond ?? null + if (entry.lastSample && now > entry.lastSample.at) { + const instant = + ((event.receivedBytes - entry.lastSample.bytes) * 1000) / (now - entry.lastSample.at) + bytesPerSecond = + bytesPerSecond === null + ? instant + : bytesPerSecond + RATE_SMOOTHING * (instant - bytesPerSecond) + } + entry.lastSample = { at: now, bytes: event.receivedBytes } + entry.snapshot = { ...event, bytesPerSecond } + for (const listener of entry.listeners) { + listener() + } +} + +/** Starts tracking a download; returns a release that forgets it once its toast is done. */ +export function trackRuntimeDownload(downloadId: string): () => void { + entryFor(downloadId) + if (!unsubscribeIpc && window.api.runtimeDownloads) { + unsubscribeIpc = window.api.runtimeDownloads.onProgress((event) => + applyRuntimeDownloadProgress(event) + ) + } + return () => { + entries.delete(downloadId) + if (entries.size === 0 && unsubscribeIpc) { + unsubscribeIpc() + unsubscribeIpc = null + } + } +} + +export function subscribeRuntimeDownloadProgress( + downloadId: string, + listener: () => void +): () => void { + const entry = entryFor(downloadId) + entry.listeners.add(listener) + return () => { + entry.listeners.delete(listener) + } +} + +export function getRuntimeDownloadProgress( + downloadId: string +): RuntimeDownloadProgressSnapshot | null { + return entries.get(downloadId)?.snapshot ?? null +} diff --git a/src/shared/rpc-contract/files-archive-params.ts b/src/shared/rpc-contract/files-archive-params.ts new file mode 100644 index 00000000000..04a7acb1253 --- /dev/null +++ b/src/shared/rpc-contract/files-archive-params.ts @@ -0,0 +1,54 @@ +import { z } from 'zod' +import { FileTreePath } from './files-params' + +/** Uncompressed bytes one folder archive may hold; ZIP without ZIP64 tops out at 4 GiB. */ +export const FOLDER_ARCHIVE_MAX_TOTAL_BYTES = 2 * 1024 * 1024 * 1024 +/** Entries one folder archive may hold; a ZIP without ZIP64 counts entries in 16 bits. */ +export const FOLDER_ARCHIVE_MAX_ENTRIES = 65_000 +/** Largest archive page one read returns; base64 keeps the reply well under the 4 MiB frame budget. */ +export const FOLDER_ARCHIVE_MAX_READ_BYTES = 2 * 1024 * 1024 +/** Directory names skipped when the caller asks to leave out dependency and VCS folders. */ +export const FOLDER_ARCHIVE_DEFAULT_EXCLUDED_NAMES = ['node_modules', '.git'] as const + +export const FileArchiveStart = FileTreePath.extend({ + /** Skip FOLDER_ARCHIVE_DEFAULT_EXCLUDED_NAMES at any depth. */ + excludeDefaultDirs: z.boolean().optional() +}) + +export const FileArchiveRead = z.object({ + archiveId: z.string().min(1).max(128), + maxBytes: z.number().int().positive().max(FOLDER_ARCHIVE_MAX_READ_BYTES) +}) + +export const FileArchiveCancel = z.object({ + archiveId: z.string().min(1).max(128) +}) + +export type FileArchiveStartResult = { + archiveId: string + /** Archive name the host suggests, e.g. `src.zip`. */ + suggestedName: string + fileCount: number + directoryCount: number + /** Sum of the regular files' sizes at walk time. */ + totalBytes: number + /** Directories left out by `excludeDefaultDirs`. */ + excludedDirCount: number +} + +export type FileArchiveReadResult = { + contentBase64: string + bytesRead: number + eof: boolean + /** Uncompressed file bytes archived so far; compare with `totalBytes` for progress. */ + processedBytes: number + /** Entries skipped because they vanished or became unreadable mid-archive. */ + skippedCount: number +} + +export type FileArchiveErrorCode = + | 'archive_not_directory' + | 'archive_too_large' + | 'archive_too_many_entries' + | 'archive_not_found' + | 'archive_busy' diff --git a/src/shared/rpc-contract/rpc-params-catalog.generated.ts b/src/shared/rpc-contract/rpc-params-catalog.generated.ts index 3f59c0c86c6..8f1cbef09cf 100644 --- a/src/shared/rpc-contract/rpc-params-catalog.generated.ts +++ b/src/shared/rpc-contract/rpc-params-catalog.generated.ts @@ -238,6 +238,7 @@ import { TypeParams } from './emulator-params' import { EnvironmentDoctorParams } from './environment-doctor-params' +import { FileArchiveCancel, FileArchiveRead, FileArchiveStart } from './files-archive-params' import { FileCommitUpload, FileCopy, @@ -1066,6 +1067,9 @@ export const RPC_PARAMS_BY_METHOD = { 'emulator.type': TypeParams, 'emulator.unregisterActive': EmulatorUnregisterActiveParams, 'environment.doctor': EnvironmentDoctorParams, + 'files.archiveCancel': FileArchiveCancel, + 'files.archiveRead': FileArchiveRead, + 'files.archiveStart': FileArchiveStart, 'files.browseServerDir': ServerDirectoryBrowse, 'files.commitUpload': FileCommitUpload, 'files.copy': FileCopy, diff --git a/src/shared/runtime-downloads/runtime-download-contract.ts b/src/shared/runtime-downloads/runtime-download-contract.ts new file mode 100644 index 00000000000..dbdb5ec71de --- /dev/null +++ b/src/shared/runtime-downloads/runtime-download-contract.ts @@ -0,0 +1,57 @@ +/** Desktop downloads of files and folders that live on a paired runtime host. */ + +export type RuntimeDownloadErrorCode = + | 'canceled' + | 'not_a_file' + | 'not_a_directory' + | 'too_large' + | 'too_many_entries' + | 'host_update_required' + | 'host_busy' + | 'not_found' + | 'disconnected' + | 'open_failed' + | 'transfer_failed' + +/** Largest file "Open locally" will fetch into the temp cache. */ +export const RUNTIME_OPEN_LOCALLY_MAX_BYTES = 1024 * 1024 * 1024 + +export type RuntimeDownloadStartArgs = { + /** Caller-chosen id; progress events and cancel use it. */ + downloadId: string + environmentId: string + worktree: string + relativePath: string + /** Display name of the remote entry, used for the suggested local name. */ + name: string + kind: 'file' | 'folder' + /** `save` asks where to put it; `open` fetches into a temp cache and opens it. */ + intent: 'save' | 'open' + excludeDefaultDirs?: boolean + expectedEnvironmentPairingRevision?: number +} + +export type RuntimeDownloadProgressEvent = { + downloadId: string + phase: 'preparing' | 'transferring' + receivedBytes: number + totalBytes: number | null + processedBytes: number + /** Present once a save location is chosen. */ + destinationPath?: string +} + +export type RuntimeDownloadResult = + | { status: 'canceled' } + | { + status: 'done' + destinationPath: string + bytes: number + /** Folder downloads only. */ + fileCount?: number + skippedCount?: number + excludedDirCount?: number + /** `open` intent: the OS refused to open the fetched file. */ + openError?: string + } + | { status: 'error'; code: RuntimeDownloadErrorCode; message: string } diff --git a/src/shared/runtime-rpc-call-queue.ts b/src/shared/runtime-rpc-call-queue.ts index 532d6241358..759d8e9d8c0 100644 --- a/src/shared/runtime-rpc-call-queue.ts +++ b/src/shared/runtime-rpc-call-queue.ts @@ -63,6 +63,7 @@ function isLongWaitRuntimeMethod(method: string): boolean { function isBulkReadRuntimeMethod(method: string): boolean { return ( method === 'files.readChunk' || + method === 'files.archiveRead' || method === 'files.readDocPreviewChunk' || method === 'files.readTerminalArtifactChunk' ) From f1752cbe28e87d526b90c4f996023a25b3c8f74b Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 07:32:32 -0800 Subject: [PATCH 02/30] fix(files): refuse files swapped after the archive walk, sanitize zip names, prefetch small files --- .../archive/folder-archive-file-reads.ts | 66 +++++++++++++ .../archive/folder-archive-sessions.test.ts | 40 +++++++- .../files/archive/folder-archive-walk.ts | 27 +++++- .../archive/folder-archive-zip-stream.ts | 94 ++++++++++++++----- 4 files changed, 195 insertions(+), 32 deletions(-) create mode 100644 src/main/runtime/files/archive/folder-archive-file-reads.ts diff --git a/src/main/runtime/files/archive/folder-archive-file-reads.ts b/src/main/runtime/files/archive/folder-archive-file-reads.ts new file mode 100644 index 00000000000..bec9f106f25 --- /dev/null +++ b/src/main/runtime/files/archive/folder-archive-file-reads.ts @@ -0,0 +1,66 @@ +import { constants } from 'node:fs' +import { open, type FileHandle } from 'node:fs/promises' +import type { FolderArchiveEntry } from './folder-archive-walk' + +/** Files up to this size are read whole, ahead of the zipper, so per-file open/stat/close overlaps. */ +const SMALL_FILE_BYTES = 64 * 1024 + +export type SmallFileRead = + | { kind: 'bytes'; bytes: Uint8Array } + | { kind: 'skip'; error: unknown } + /** Grew past the small-file limit since the walk; stream it instead. */ + | { kind: 'stream' } + +export class ChangedSinceWalkError extends Error {} + +export function isSmallFile(entry: FolderArchiveEntry): boolean { + return entry.kind === 'file' && entry.size <= SMALL_FILE_BYTES +} + +/** + * Opens a planned file without following a final link, and refuses it if it is no longer the file + * the walk saw. A parent directory swapped for a link after the walk slips past O_NOFOLLOW but + * changes dev/ino, so nothing outside the root is ever read. + */ +export async function openUnchangedFile(entry: FolderArchiveEntry): Promise { + const handle = await open(entry.absolutePath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)) + try { + const opened = await handle.stat() + if (!opened.isFile() || opened.dev !== entry.dev || opened.ino !== entry.ino) { + throw new ChangedSinceWalkError('archive_entry_changed') + } + return handle + } catch (error) { + await handle.close().catch(() => {}) + throw error + } +} + +/** Never rejects: failures come back as `skip` so a look-ahead read cannot go unhandled. */ +export async function readSmallFile(entry: FolderArchiveEntry): Promise { + let handle: FileHandle + try { + handle = await openUnchangedFile(entry) + } catch (error) { + return { kind: 'skip', error } + } + try { + // One spare byte tells a file that grew since the walk apart from one that matches. + const buffer = Buffer.allocUnsafe(Math.min(entry.size, SMALL_FILE_BYTES) + 1) + let filled = 0 + for (;;) { + const { bytesRead } = await handle.read(buffer, filled, buffer.byteLength - filled, filled) + if (bytesRead === 0) { + return { kind: 'bytes', bytes: buffer.subarray(0, filled) } + } + filled += bytesRead + if (filled === buffer.byteLength) { + return { kind: 'stream' } + } + } + } catch (error) { + return { kind: 'skip', error } + } finally { + await handle.close().catch(() => {}) + } +} diff --git a/src/main/runtime/files/archive/folder-archive-sessions.test.ts b/src/main/runtime/files/archive/folder-archive-sessions.test.ts index 88681003b4f..e3e3e73fda3 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.test.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.test.ts @@ -1,10 +1,14 @@ -import { mkdtemp, mkdir, rm, symlink, writeFile, chmod } from 'node:fs/promises' +import { mkdtemp, mkdir, rename, rm, symlink, writeFile, chmod } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { unzipSync } from 'fflate' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { FolderArchiveSessions } from './folder-archive-sessions' -import { FolderArchiveLimitError, planFolderArchive } from './folder-archive-walk' +import { + FolderArchiveLimitError, + planFolderArchive, + sanitizeArchiveSegment +} from './folder-archive-walk' import { FolderArchiveZipStream } from './folder-archive-zip-stream' let root: string @@ -97,6 +101,38 @@ describe('FolderArchiveSessions', () => { expect(Object.keys(files)).not.toContain('p/dir-link/') }) + it('skips a file whose parent directory was swapped for a link after the walk', async () => { + const outside = join(root, 'outside') + await mkdir(outside) + await writeFile(join(outside, 'a.txt'), 'secret') + const project = join(root, 'p') + await mkdir(join(project, 'sub'), { recursive: true }) + await writeFile(join(project, 'sub', 'a.txt'), 'inside') + const plan = await planFolderArchive(project, 'p', { excludeDefaultDirs: true }) + // Same name, different file: O_NOFOLLOW alone would follow the swapped parent. + await rename(join(project, 'sub'), join(root, 'moved')) + await symlink(outside, join(project, 'sub')) + const stream = new FolderArchiveZipStream(plan.entries) + const parts: Buffer[] = [] + for (;;) { + const page = await stream.read(1 << 20) + parts.push(page.bytes) + if (page.eof) { + break + } + } + const files = unzipSync(Buffer.concat(parts)) + expect(files['p/sub/a.txt']).toBeUndefined() + expect(stream.skippedCount).toBe(1) + }) + + it('rewrites backslashes and control characters so Windows extractors cannot escape', () => { + expect(sanitizeArchiveSegment('..\\..\\evil')).toBe('.._.._evil') + expect(sanitizeArchiveSegment('a\u0001b')).toBe('a_b') + expect(sanitizeArchiveSegment('..')).toBe('_') + expect(sanitizeArchiveSegment('normal name.txt')).toBe('normal name.txt') + }) + it('refuses folders over the byte or entry limits', async () => { await writeFile(join(root, 'a'), Buffer.alloc(10)) await writeFile(join(root, 'b'), Buffer.alloc(10)) diff --git a/src/main/runtime/files/archive/folder-archive-walk.ts b/src/main/runtime/files/archive/folder-archive-walk.ts index dee15d584d0..2498ee91012 100644 --- a/src/main/runtime/files/archive/folder-archive-walk.ts +++ b/src/main/runtime/files/archive/folder-archive-walk.ts @@ -14,6 +14,9 @@ export type FolderArchiveEntry = { size: number mode: number mtimeMs: number + /** Walk-time identity; the zip stream refuses to read a file whose dev/ino changed since. */ + dev: number + ino: number } export type FolderArchivePlan = { @@ -66,7 +69,9 @@ export async function planFolderArchive( kind: 'directory', size: 0, mode: rootStat.mode, - mtimeMs: rootStat.mtimeMs + mtimeMs: rootStat.mtimeMs, + dev: rootStat.dev, + ino: rootStat.ino } ], fileCount: 0, @@ -115,7 +120,7 @@ export async function planFolderArchive( } const { name, stat } = item const absolutePath = join(dir.absolutePath, name) - const archivePath = `${dir.archivePath}${name}` + const archivePath = `${dir.archivePath}${sanitizeArchiveSegment(name)}` if (stat.isDirectory()) { if (excluded.has(name)) { plan.excludedDirCount += 1 @@ -128,7 +133,9 @@ export async function planFolderArchive( kind: 'directory', size: 0, mode: stat.mode, - mtimeMs: stat.mtimeMs + mtimeMs: stat.mtimeMs, + dev: stat.dev, + ino: stat.ino }) childDirs.push({ absolutePath, archivePath: `${archivePath}/` }) } else if (stat.isFile() || stat.isSymbolicLink()) { @@ -146,7 +153,9 @@ export async function planFolderArchive( kind, size: kind === 'file' ? stat.size : 0, mode: stat.mode, - mtimeMs: stat.mtimeMs + mtimeMs: stat.mtimeMs, + dev: stat.dev, + ino: stat.ino }) } // Sockets, FIFOs and devices have no archivable content. @@ -162,6 +171,16 @@ export async function planFolderArchive( return plan } +/** + * One path segment as stored in the ZIP. Linux allows `\` and control characters in names; Windows + * extractors treat `\` as a separator, so a file named `..\..\x` would escape the extract folder. + */ +export function sanitizeArchiveSegment(name: string): string { + // oxlint-disable-next-line no-control-regex + const cleaned = name.replace(/[\u0000-\u001f\\]/g, '_') + return cleaned === '.' || cleaned === '..' ? '_' : cleaned +} + async function isUnmovedDirectory(absolutePath: string): Promise { try { const resolved = await realpath(absolutePath) diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts index c40103a598d..12b2eb09af1 100644 --- a/src/main/runtime/files/archive/folder-archive-zip-stream.ts +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -1,10 +1,18 @@ -import { constants } from 'node:fs' -import { open, readlink } from 'node:fs/promises' +import { readlink, type FileHandle } from 'node:fs/promises' import { extname } from 'node:path' import { Zip, ZipDeflate, ZipPassThrough, type ZipInputFile } from 'fflate' import { isVanishedOrDenied, type FolderArchiveEntry } from './folder-archive-walk' +import { + ChangedSinceWalkError, + isSmallFile, + openUnchangedFile, + readSmallFile, + type SmallFileRead +} from './folder-archive-file-reads' const FILE_READ_BYTES = 256 * 1024 +/** Small files read ahead at once; bounds look-ahead memory to about 2 MiB. */ +const PREFETCH_WINDOW = 32 /** Encoded bytes buffered ahead of the reader before the producer pauses. */ const DEFAULT_HIGH_WATER_BYTES = 8 * 1024 * 1024 // Level 4 keeps pure-JS deflate fast enough not to starve a fast link while still shrinking text. @@ -49,6 +57,8 @@ const STORED_EXTENSIONS = new Set([ */ export class FolderArchiveZipStream { private readonly chunks: Uint8Array[] = [] + // Head index instead of shift(): a folder of small files leaves thousands of tiny chunks queued. + private chunkHead = 0 private bufferedBytes = 0 private finished = false private failure: Error | null = null @@ -92,24 +102,28 @@ export class FolderArchiveZipStream { } const parts: Uint8Array[] = [] let taken = 0 - while (this.chunks.length > 0 && taken < maxBytes) { - const head = this.chunks[0] + while (this.chunkHead < this.chunks.length && taken < maxBytes) { + const head = this.chunks[this.chunkHead] const room = maxBytes - taken if (head.byteLength <= room) { parts.push(head) taken += head.byteLength - this.chunks.shift() + this.chunkHead += 1 } else { parts.push(head.subarray(0, room)) - this.chunks[0] = head.subarray(room) + this.chunks[this.chunkHead] = head.subarray(room) taken += room } } + if (this.chunkHead > 1024 || this.chunkHead === this.chunks.length) { + this.chunks.splice(0, this.chunkHead) + this.chunkHead = 0 + } this.bufferedBytes -= taken this.notifyProducer() return { bytes: Buffer.concat(parts, taken), - eof: this.finished && this.chunks.length === 0 + eof: this.finished && this.chunkHead >= this.chunks.length } } @@ -117,18 +131,33 @@ export class FolderArchiveZipStream { this.canceled = true this.zip.terminate() this.chunks.length = 0 + this.chunkHead = 0 this.bufferedBytes = 0 this.fail(new Error('archive_canceled')) this.notifyProducer() } private async produce(): Promise { + const prefetched = new Map>() try { - for (const entry of this.entries) { + for (let index = 0; index < this.entries.length; index += 1) { if (this.canceled) { return } - await this.addEntry(entry) + // Keep the next few small files' reads in flight while this entry is zipped. + for ( + let ahead = index; + ahead < index + PREFETCH_WINDOW && ahead < this.entries.length; + ahead += 1 + ) { + const next = this.entries[ahead] + if (!prefetched.has(ahead) && isSmallFile(next)) { + prefetched.set(ahead, readSmallFile(next)) + } + } + const small = prefetched.get(index) + prefetched.delete(index) + await this.addEntry(this.entries[index], small ? await small : undefined) } this.zip.end() } catch (error) { @@ -136,7 +165,7 @@ export class FolderArchiveZipStream { } } - private async addEntry(entry: FolderArchiveEntry): Promise { + private async addEntry(entry: FolderArchiveEntry, small?: SmallFileRead): Promise { if (entry.kind === 'directory') { const file = withAttributes(new ZipPassThrough(entry.archivePath), entry, entry.mode) this.zip.add(file) @@ -160,26 +189,27 @@ export class FolderArchiveZipStream { file.push(new TextEncoder().encode(target), true) return } - let handle: Awaited> + if (small?.kind === 'skip') { + return this.skip(small.error) + } + if (small?.kind === 'bytes') { + await this.waitForRoom() + if (this.canceled) { + return + } + const file = this.addFileEntry(entry) + this.processedBytes += small.bytes.byteLength + file.push(small.bytes, true) + return + } + let handle: FileHandle try { - handle = await open(entry.absolutePath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)) + handle = await openUnchangedFile(entry) } catch (error) { return this.skip(error) } try { - // A file swapped for something else after the walk is skipped, not archived. - if (!(await handle.stat()).isFile()) { - this.skippedCount += 1 - return - } - const file = withAttributes( - STORED_EXTENSIONS.has(extname(entry.archivePath).toLowerCase()) - ? new ZipPassThrough(entry.archivePath) - : new ZipDeflate(entry.archivePath, { level: DEFLATE_LEVEL }), - entry, - entry.mode - ) - this.zip.add(file) + const file = this.addFileEntry(entry) const buffer = Buffer.allocUnsafe(FILE_READ_BYTES) for (;;) { await this.waitForRoom() @@ -200,8 +230,20 @@ export class FolderArchiveZipStream { } } + private addFileEntry(entry: FolderArchiveEntry): ZipInputFile { + const file = withAttributes( + STORED_EXTENSIONS.has(extname(entry.archivePath).toLowerCase()) + ? new ZipPassThrough(entry.archivePath) + : new ZipDeflate(entry.archivePath, { level: DEFLATE_LEVEL }), + entry, + entry.mode + ) + this.zip.add(file) + return file + } + private skip(error: unknown): void { - if (!isVanishedOrDenied(error)) { + if (!(error instanceof ChangedSinceWalkError) && !isVanishedOrDenied(error)) { throw error } this.skippedCount += 1 From 3384c5b08cf811564c2a5e65a1942b3e6bfce66e Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 07:48:06 -0800 Subject: [PATCH 03/30] feat(files): download lane, glossary toasts with InlineError and one next action, fixtures --- .../runtime-environment-call-queue.ts | 10 +- .../runtime-environment-transport-routing.ts | 6 +- .../runtime-download-handlers.ts | 2 +- .../runtime-download-transfer.test.ts | 39 ++ .../runtime-download-transfer.ts | 72 ++- .../archive/folder-archive-zip-stream.ts | 2 +- .../panel/RuntimeDownloads.fixture.tsx | 202 ++++++++ .../panel/runtime-download-actions.tsx | 277 +++++----- .../panel/runtime-download-toast.tsx | 485 ++++++++++++++---- .../src/i18n/en-runtime-required.json | 8 + src/renderer/src/i18n/locales/en.json | 42 +- .../cloud-environment-ids.ts | 5 + .../runtime-download-contract.ts | 2 + src/shared/runtime-rpc-call-queue.test.ts | 38 ++ src/shared/runtime-rpc-call-queue.ts | 30 +- 15 files changed, 936 insertions(+), 284 deletions(-) create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx diff --git a/src/main/ipc/runtime-environments/runtime-environment-call-queue.ts b/src/main/ipc/runtime-environments/runtime-environment-call-queue.ts index 558fa9e21d5..39c14a03593 100644 --- a/src/main/ipc/runtime-environments/runtime-environment-call-queue.ts +++ b/src/main/ipc/runtime-environments/runtime-environment-call-queue.ts @@ -1,4 +1,7 @@ -import { RuntimeRpcCallQueuePool } from '../../../shared/runtime-rpc-call-queue' +import { + RuntimeRpcCallQueuePool, + type RuntimeRpcCallLane +} from '../../../shared/runtime-rpc-call-queue' const runtimeCallQueuePool = new RuntimeRpcCallQueuePool() @@ -6,7 +9,8 @@ export function enqueueRuntimeCall( selector: string, method: string, run: () => Promise, - signal?: AbortSignal + signal?: AbortSignal, + lane?: RuntimeRpcCallLane ): Promise { - return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal) + return runtimeCallQueuePool.enqueue(selector, method, run, 0, signal, lane) } diff --git a/src/main/ipc/runtime-environments/runtime-environment-transport-routing.ts b/src/main/ipc/runtime-environments/runtime-environment-transport-routing.ts index e0fec03129a..5d87ef7e91a 100644 --- a/src/main/ipc/runtime-environments/runtime-environment-transport-routing.ts +++ b/src/main/ipc/runtime-environments/runtime-environment-transport-routing.ts @@ -24,6 +24,7 @@ import type { RemoteRuntimeTransportSubscription } from '../../../shared/remote- import { withRuntimeUnreachableHint } from './runtime-environment-unreachable-hint' import { decodeRuntimeEnvironmentSubscriptionEvents } from './runtime-environment-subscription-events' import { enqueueRuntimeCall } from './runtime-environment-call-queue' +import type { RuntimeRpcCallLane } from '../../../shared/runtime-rpc-call-queue' import { holdForInFlightRecipeVmWake } from './runtime-environment-recipe-vm-wake' import { sendRemoteRuntimeConnectionRequestAbortable, @@ -54,6 +55,8 @@ export async function callRuntimeEnvironment( signal?: AbortSignal expectedEnvironmentRuntimeId?: string preparation?: ManagedRuntimeCallPreparation + /** Queue lane override; see RuntimeRpcCallLane. */ + lane?: RuntimeRpcCallLane } ): Promise> { if (method === 'status.get') { @@ -168,7 +171,8 @@ export async function callRuntimeEnvironment( markEnvironmentUsedFromResponse(userDataPath, currentEnvironment.id, response) return response }, - options?.signal + options?.signal, + options?.lane ) } catch (error) { if (error instanceof Error && error.name !== 'AbortError') { diff --git a/src/main/runtime-downloads/runtime-download-handlers.ts b/src/main/runtime-downloads/runtime-download-handlers.ts index b7819ada7c3..52ad99a4e8f 100644 --- a/src/main/runtime-downloads/runtime-download-handlers.ts +++ b/src/main/runtime-downloads/runtime-download-handlers.ts @@ -73,7 +73,7 @@ function createCall(args: RuntimeDownloadStartArgs): RuntimeDownloadCall { options.timeoutMs, args.expectedEnvironmentPairingRevision, undefined, - { signal: options.signal } + { signal: options.signal, lane: 'download' } ) if (!response.ok) { throw runtimeDownloadErrorFromResponse(response.error) diff --git a/src/main/runtime-downloads/runtime-download-transfer.test.ts b/src/main/runtime-downloads/runtime-download-transfer.test.ts index 75b588c0e33..2073967cce7 100644 --- a/src/main/runtime-downloads/runtime-download-transfer.test.ts +++ b/src/main/runtime-downloads/runtime-download-transfer.test.ts @@ -120,4 +120,43 @@ describe('transferRuntimeFolder', () => { } expect(calls).toEqual(['files.archiveStart', 'files.archiveRead', 'files.archiveCancel']) }) + + it('releases an archive whose start reply lands after the user canceled', async () => { + const controller = new AbortController() + const calls: { method: string; params: unknown }[] = [] + let finishStart: (value: unknown) => void = () => {} + const call = (async (method: string, params: unknown) => { + calls.push({ method, params }) + if (method === 'files.archiveStart') { + return new Promise((resolve) => { + finishStart = resolve + }) + } + return { released: true } + }) as RuntimeDownloadCall + const sink = await open(join(dir, 'late.zip'), 'w+') + try { + const transfer = transferRuntimeFolder({ + call, + worktree: 'wt', + relativePath: 'src', + sink, + signal: controller.signal, + onProgress: () => {}, + excludeDefaultDirs: true + }) + controller.abort() + // Canceled at once, before the host has even finished walking. + await expect(transfer).rejects.toEqual(new RuntimeDownloadError('canceled')) + finishStart({ archiveId: 'late', totalBytes: 1, excludedDirCount: 0, fileCount: 1 }) + await new Promise((resolve) => setTimeout(resolve, 0)) + } finally { + await sink.close() + } + expect(calls.map((entry) => entry.method)).toEqual([ + 'files.archiveStart', + 'files.archiveCancel' + ]) + expect(calls[1].params).toEqual({ archiveId: 'late' }) + }) }) diff --git a/src/main/runtime-downloads/runtime-download-transfer.ts b/src/main/runtime-downloads/runtime-download-transfer.ts index 3d6544cf7ba..758262d5cb9 100644 --- a/src/main/runtime-downloads/runtime-download-transfer.ts +++ b/src/main/runtime-downloads/runtime-download-transfer.ts @@ -9,8 +9,8 @@ import type { RuntimeDownloadErrorCode } from '../../shared/runtime-downloads/ru /** files.readChunk's schema ceiling; older hosts accept nothing larger. */ const FILE_CHUNK_BYTES = 512 * 1024 -// The main-process bulk-read lane admits two calls per host, so a third would only queue. -const FILE_CHUNK_CONCURRENCY = 2 +// Matches the main-process download lane: four pages in flight hide ~65 ms of RTT. +const FILE_CHUNK_CONCURRENCY = 4 const CHUNK_TIMEOUT_MS = 60_000 // The walk of a large tree runs before the first byte; give it room. const ARCHIVE_START_TIMEOUT_MS = 120_000 @@ -37,6 +37,8 @@ export type RuntimeDownloadProgress = { totalBytes: number | null /** Source bytes covered so far, comparable with totalBytes. */ processedBytes: number + /** Folder downloads only. */ + fileCount?: number } type TransferInput = { @@ -127,14 +129,19 @@ export type FolderTransferResult = { export async function transferRuntimeFolder( input: TransferInput & { excludeDefaultDirs: boolean } ): Promise { - const started = await input.call( + // Not tied to the user's cancel: the host only learns of a cancel through archiveCancel, so a + // start canceled mid-walk must still deliver its id for release (else it holds a slot until idle). + const startRequest = input.call( 'files.archiveStart', { worktree: input.worktree, relativePath: input.relativePath, excludeDefaultDirs: input.excludeDefaultDirs }, - { timeoutMs: ARCHIVE_START_TIMEOUT_MS, signal: input.signal } + { timeoutMs: ARCHIVE_START_TIMEOUT_MS, signal: new AbortController().signal } + ) + const started = await raceAbort(startRequest, input.signal, (late) => + releaseArchive(input.call, late.archiveId) ) let finished = false let received = 0 @@ -156,7 +163,8 @@ export async function transferRuntimeFolder( input.onProgress({ receivedBytes: received, totalBytes: started.totalBytes, - processedBytes: page.processedBytes + processedBytes: page.processedBytes, + fileCount: started.fileCount }) if (page.eof) { finished = true @@ -170,18 +178,48 @@ export async function transferRuntimeFolder( } } finally { if (!finished) { - // Best effort: the host's idle timer frees the session if this never arrives. - const release = new AbortController() - void input - .call( - 'files.archiveCancel', - { archiveId: started.archiveId }, - { - timeoutMs: 10_000, - signal: release.signal - } - ) - .catch(() => {}) + releaseArchive(input.call, started.archiveId) } } } + +/** Best effort: the host's idle timer frees the session if this never arrives. */ +function releaseArchive(call: RuntimeDownloadCall, archiveId: string): void { + void call( + 'files.archiveCancel', + { archiveId }, + { timeoutMs: 10_000, signal: new AbortController().signal } + ).catch(() => {}) +} + +/** Rejects with `canceled` as soon as `signal` aborts; a result that lands later goes to `onLate`. */ +function raceAbort( + request: Promise, + signal: AbortSignal, + onLate: (value: T) => void +): Promise { + if (signal.aborted) { + void request.then(onLate, () => {}) + return Promise.reject(new RuntimeDownloadError('canceled')) + } + return new Promise((resolve, reject) => { + const onAbort = (): void => { + void request.then(onLate, () => {}) + reject(new RuntimeDownloadError('canceled')) + } + signal.addEventListener('abort', onAbort, { once: true }) + request.then( + (value) => { + signal.removeEventListener('abort', onAbort) + if (signal.aborted) { + return + } + resolve(value) + }, + (error: unknown) => { + signal.removeEventListener('abort', onAbort) + reject(error) + } + ) + }) +} diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts index 12b2eb09af1..85fe4411fe7 100644 --- a/src/main/runtime/files/archive/folder-archive-zip-stream.ts +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -230,7 +230,7 @@ export class FolderArchiveZipStream { } } - private addFileEntry(entry: FolderArchiveEntry): ZipInputFile { + private addFileEntry(entry: FolderArchiveEntry): ZipPassThrough | ZipDeflate { const file = withAttributes( STORED_EXTENSIONS.has(extname(entry.archivePath).toLowerCase()) ? new ZipPassThrough(entry.archivePath) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx new file mode 100644 index 00000000000..6905d35f661 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx @@ -0,0 +1,202 @@ +import { Copy, Download, File, MonitorDown, Pencil, Search, SquareTerminal } from 'lucide-react' +import { FixtureCodeBackdrop } from '../../../../component-fixtures/FixtureCodeBackdrop' +import { defineFixture } from '../../../../component-fixtures/component-fixture' +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuSeparator, + DropdownMenuTrigger +} from '../../../ui/dropdown-menu' +import { RuntimeDownloadToast, type RuntimeDownloadToastState } from './runtime-download-toast' + +const noop = (): void => {} +const MB = 1024 * 1024 + +function toastFixture(state: RuntimeDownloadToastState) { + return defineFixture({ + width: 380, + render: () => ( +
+ +
+ ) + }) +} + +// Mirrors the file explorer row menu around the new entries (a dropdown, since a context menu +// cannot be held open in a fixture). +function menuFixture(kind: 'file' | 'folder') { + return defineFixture({ + width: 300, + render: () => ( +
+ + + + + + + Open in Terminal + + {kind === 'file' ? ( + + + View File + + ) : null} + + + {kind === 'file' ? 'Download' : 'Download as ZIP…'} + + {kind === 'file' ? ( + + + Open on This Computer + + ) : ( + + + Find in Folder + + )} + + + + Copy Path + + + + Rename + + + +
+ ) + }) +} + +export const menuFile = menuFixture('file') +export const menuFolder = menuFixture('folder') + +export const fileProgress = toastFixture({ + kind: 'progress', + intent: 'save', + itemKind: 'file', + name: 'random-200mb.bin', + snapshot: { + downloadId: 'f', + phase: 'transferring', + receivedBytes: 87 * MB, + totalBytes: 200 * MB, + processedBytes: 87 * MB, + bytesPerSecond: 14.2 * MB + } +}) +export const folderCollecting = toastFixture({ + kind: 'progress', + intent: 'save', + itemKind: 'folder', + name: 'smallfiles.zip', + snapshot: null +}) +export const folderProgress = toastFixture({ + kind: 'progress', + intent: 'save', + itemKind: 'folder', + name: 'smallfiles.zip', + snapshot: { + downloadId: 'd', + phase: 'transferring', + receivedBytes: 1.1 * MB, + totalBytes: 2.4 * MB, + processedBytes: 1.6 * MB, + fileCount: 5000, + bytesPerSecond: 1.2 * MB + } +}) +export const openProgress = toastFixture({ + kind: 'progress', + intent: 'open', + itemKind: 'file', + name: 'build-output.zip', + snapshot: { + downloadId: 'o', + phase: 'transferring', + receivedBytes: 0.6 * MB, + totalBytes: 1.9 * MB, + processedBytes: 0.6 * MB, + bytesPerSecond: 5.4 * MB + } +}) +export const fileDone = toastFixture({ + kind: 'done', + itemKind: 'file', + name: 'random-200mb.bin', + bytes: 200 * MB, + elapsedMs: 14_300 +}) +export const folderDone = toastFixture({ + kind: 'done', + itemKind: 'folder', + name: 'smallfiles.zip', + bytes: 0.7 * MB, + elapsedMs: 2_400, + fileCount: 5000, + excludedDefaultDirs: true +}) +export const folderDoneSkipped = toastFixture({ + kind: 'done', + itemKind: 'folder', + name: 'logs.zip', + bytes: 3.2 * MB, + elapsedMs: 1_800, + fileCount: 1, + skippedCount: 2 +}) +export const errorDisconnected = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'file', + name: 'random-200mb.bin', + code: 'disconnected', + place: 'sandbox' +}) +export const errorTooLarge = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'folder', + name: 'datasets', + code: 'too_large', + place: 'sandbox' +}) +export const errorTooLargeToOpen = toastFixture({ + kind: 'failed', + intent: 'open', + itemKind: 'file', + name: 'model.safetensors', + code: 'too_large', + place: 'sandbox' +}) +export const errorOldSandbox = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'folder', + name: 'src', + code: 'host_update_required', + place: 'sandbox' +}) +export const errorNotFound = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'file', + name: 'report.pdf', + code: 'not_found', + place: 'sandbox' +}) +export const errorNoApp = toastFixture({ kind: 'no_app', name: 'trace.perfetto' }) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx index 641f16e7ac1..9493927f897 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx @@ -1,18 +1,33 @@ +import { useSyncExternalStore } from 'react' import { toast } from 'sonner' import { createNonSecureContextUuid } from '../../../../../../shared/non-secure-context-uuid' -import { translate } from '@/i18n/i18n' import { getRevealInFileManagerLabel, revealInFileManager } from '@/lib/reveal-in-file-manager' +import { + isKnownCloudEnvironment, + refreshCloudEnvironmentIds +} from '@/lib/cloud-remote-desktop/cloud-environment-ids' import { getRemoteFileArgs } from '@/runtime/files/runtime-file-routing' import type { RuntimeFileOperationArgs } from '@/runtime/files/runtime-file-client-types' import { captureRuntimeEnvironmentRequestRevision } from '@/runtime/rpc/environment-revisions/runtime-environment-revision' -import { trackRuntimeDownload } from '@/runtime/files/downloads/runtime-download-progress-store' +import { + getRuntimeDownloadProgress, + subscribeRuntimeDownloadProgress, + trackRuntimeDownload +} from '@/runtime/files/downloads/runtime-download-progress-store' import { LOCAL_EXECUTION_HOST_ID } from '../../../../../../shared/execution-host' -import { formatBytes } from '../../../../../../shared/format-bytes' import type { RuntimeDownloadResult } from '../../../../../../shared/runtime-downloads/runtime-download-contract' import type { TreeNode } from '../tree/file-explorer-types' -import { RuntimeDownloadProgressBody, runtimeDownloadErrorMessage } from './runtime-download-toast' +import { + RuntimeDownloadToast, + type RuntimeDownloadToastAction, + type RuntimeDownloadToastIntent, + type RuntimeDownloadToastState +} from './runtime-download-toast' -export type RuntimeDownloadIntent = 'save' | 'open' +export type RuntimeDownloadIntent = RuntimeDownloadToastIntent + +// Long enough to reach "Reveal in Finder" after a long download, short enough not to pile up. +const DONE_TOAST_MS = 12_000 /** Whether this desktop can stream the node straight from its runtime host. */ export function canStreamRuntimeDownload( @@ -29,6 +44,30 @@ function localName(destinationPath: string): string { return destinationPath.split(separator).pop() || destinationPath } +/** Progress toast body bound to the live progress store. */ +function LiveProgressToast({ + downloadId, + state, + onAction +}: { + downloadId: string + state: Omit, 'snapshot'> + onAction: (action: RuntimeDownloadToastAction) => void +}): React.JSX.Element { + const snapshot = useSyncExternalStore( + (listener) => subscribeRuntimeDownloadProgress(downloadId, listener), + () => getRuntimeDownloadProgress(downloadId) + ) + return ( + {}} + /> + ) +} + export async function startRuntimeDownload( node: TreeNode, context: RuntimeFileOperationArgs, @@ -40,58 +79,45 @@ export async function startRuntimeDownload( if (!api || !remote) { return } - const kind = node.isDirectory ? 'folder' : 'file' + const environmentId = remote.target.environmentId + // Error copy says "sandbox" for cloud workspaces; the list is usually primed already. + void refreshCloudEnvironmentIds() + const itemKind = node.isDirectory ? 'folder' : 'file' const downloadId = createNonSecureContextUuid() const toastId = `runtime-download:${downloadId}` const release = trackRuntimeDownload(downloadId) const excludeDefaultDirs = options.excludeDefaultDirs !== false + // Timed from the first progress event so the save dialog doesn't count. + let startedAt = performance.now() const pending = api.start({ downloadId, - environmentId: remote.target.environmentId, + environmentId, worktree: remote.worktreeSelector, relativePath: remote.relativePath, name: node.name, - kind, + kind: itemKind, intent, excludeDefaultDirs, - expectedEnvironmentPairingRevision: captureRuntimeEnvironmentRequestRevision( - remote.target.environmentId - ) + expectedEnvironmentPairingRevision: captureRuntimeEnvironmentRequestRevision(environmentId) }) + const progressName = itemKind === 'folder' ? `${node.name}.zip` : node.name // The save dialog comes first; show progress only after the user has picked a place. const showProgress = (): void => { - toast.loading( - intent === 'open' - ? translate( - 'auto.components.right.sidebar.runtimeDownload.opening', - "Opening '{{value0}}'", - { - value0: node.name - } - ) - : translate( - 'auto.components.right.sidebar.runtimeDownload.downloading', - "Downloading '{{value0}}'", - { value0: kind === 'folder' ? `${node.name}.zip` : node.name } - ), - { - id: toastId, - description: , - duration: Infinity, - dismissible: false, - action: { - label: translate('auto.components.right.sidebar.runtimeDownload.cancel', 'Cancel'), - onClick: (event) => { - event.preventDefault() - void api.cancel({ downloadId }) - } - } - } + toast.custom( + () => ( + void api.cancel({ downloadId })} + /> + ), + { id: toastId, duration: Number.POSITIVE_INFINITY, dismissible: false } ) } - const stopProgressWatch = window.api.runtimeDownloads!.onProgress((event) => { + const stopProgressWatch = api.onProgress((event) => { if (event.downloadId === downloadId) { stopProgressWatch() + startedAt = performance.now() showProgress() } }) @@ -108,118 +134,81 @@ export async function startRuntimeDownload( stopProgressWatch() release() } - showOutcome(result, { node, kind, intent, toastId, context, excludeDefaultDirs }) -} - -function showOutcome( - result: RuntimeDownloadResult, - input: { - node: TreeNode - kind: 'file' | 'folder' - intent: RuntimeDownloadIntent - toastId: string - context: RuntimeFileOperationArgs - excludeDefaultDirs: boolean - } -): void { - const { node, kind, intent, toastId } = input - if (result.status === 'canceled') { + const elapsedMs = performance.now() - startedAt + const outcome = outcomeState(result, { node, itemKind, intent, environmentId, elapsedMs }) + if (!outcome) { toast.dismiss(toastId) return } if (result.status === 'error') { - toast.error( - intent === 'open' - ? translate( - 'auto.components.right.sidebar.runtimeDownload.openFailed', - "Couldn't open '{{value0}}'", - { value0: node.name } - ) - : translate( - 'auto.components.right.sidebar.runtimeDownload.failed', - "Couldn't download '{{value0}}'", - { value0: node.name } - ), - { - id: toastId, - description: runtimeDownloadErrorMessage(result.code, result.message), - duration: 8000, - dismissible: true, - action: undefined - } - ) - return + // Host details stay out of the toast (plain words only) but remain debuggable. + console.warn('[runtime-download] failed', result.code, result.message) } - const reveal = { - label: getRevealInFileManagerLabel(), - onClick: () => void revealInFileManager(result.destinationPath, LOCAL_EXECUTION_HOST_ID) + if (result.status === 'done' && result.openError) { + console.warn('[runtime-download] open failed', result.openError) } - if (intent === 'open') { - if (!result.openError) { - toast.dismiss(toastId) - return + const destinationPath = result.status === 'done' ? result.destinationPath : null + const onAction = (action: RuntimeDownloadToastAction): void => { + toast.dismiss(toastId) + if ((action === 'reveal' || action === 'open') && destinationPath) { + void (action === 'reveal' + ? revealInFileManager(destinationPath, LOCAL_EXECUTION_HOST_ID) + : window.api.shell.openPath(destinationPath, LOCAL_EXECUTION_HOST_ID)) + } else if (action === 'retry') { + void startRuntimeDownload(node, context, intent, options) + } else if (action === 'download_instead') { + void startRuntimeDownload(node, context, 'save', options) } - toast.error( - translate( - 'auto.components.right.sidebar.runtimeDownload.noAppToOpen', - "No app could open '{{value0}}'", - { value0: node.name } - ), - { - id: toastId, - description: result.openError, - duration: 8000, - dismissible: true, - action: reveal - } - ) - return } - const parts = [formatBytes(result.bytes)] - if (kind === 'folder' && result.fileCount !== undefined) { - parts.push( - translate('auto.components.right.sidebar.runtimeDownload.fileCount', '{{count}} files', { - count: result.fileCount, - defaultValue_one: '{{count}} file' - }) - ) - } - if (kind === 'folder' && (result.excludedDirCount ?? 0) > 0) { - parts.push( - translate( - 'auto.components.right.sidebar.runtimeDownload.excludedDefaultDirs', - 'without node_modules and .git' - ) - ) - } - if ((result.skippedCount ?? 0) > 0) { - parts.push( - translate( - 'auto.components.right.sidebar.runtimeDownload.skipped', - '{{count}} unreadable items skipped', - { count: result.skippedCount, defaultValue_one: '{{count}} unreadable item skipped' } - ) - ) - } - toast.success( - translate('auto.components.right.sidebar.runtimeDownload.done', "Downloaded '{{value0}}'", { - value0: localName(result.destinationPath) - }), - { - id: toastId, - description: parts.join(' · '), - duration: 8000, - dismissible: true, - action: reveal, - ...(kind === 'file' - ? { - cancel: { - label: translate('auto.components.right.sidebar.runtimeDownload.open', 'Open'), - onClick: () => - void window.api.shell.openPath(result.destinationPath, LOCAL_EXECUTION_HOST_ID) - } - } - : {}) - } + toast.custom( + () => ( + toast.dismiss(toastId)} + /> + ), + { id: toastId, duration: DONE_TOAST_MS, dismissible: true } ) } + +/** The toast to leave behind, or null when it should just go away (canceled, opened fine). */ +export function outcomeState( + result: RuntimeDownloadResult, + input: { + node: Pick + itemKind: 'file' | 'folder' + intent: RuntimeDownloadIntent + environmentId: string + elapsedMs: number + } +): RuntimeDownloadToastState | null { + const { node, itemKind, intent } = input + if (result.status === 'canceled') { + return null + } + if (result.status === 'error') { + return { + kind: 'failed', + intent, + itemKind, + name: node.name, + code: result.code, + place: isKnownCloudEnvironment(input.environmentId) ? 'sandbox' : 'host' + } + } + if (intent === 'open') { + return result.openError ? { kind: 'no_app', name: node.name } : null + } + return { + kind: 'done', + itemKind, + name: localName(result.destinationPath), + bytes: result.bytes, + elapsedMs: input.elapsedMs, + ...(result.fileCount !== undefined ? { fileCount: result.fileCount } : {}), + excludedDefaultDirs: (result.excludedDirCount ?? 0) > 0, + ...(result.skippedCount ? { skippedCount: result.skippedCount } : {}) + } +} diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx index 69c7a217f92..c0dec77889a 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx @@ -1,14 +1,56 @@ -import { useSyncExternalStore } from 'react' +import type React from 'react' +import { CircleCheck, Download, ExternalLink, FolderOpen, RotateCw, X } from 'lucide-react' +import { Button } from '@/components/ui/button' +import { InlineError } from '@/components/ui/inline-error' import { Progress } from '@/components/ui/progress' -import { useTranslate } from '@/i18n/use-translate' -import { translate } from '@/i18n/i18n' +import { useTranslate, type Translate } from '@/i18n/use-translate' +import { formatElapsedTime } from '@/components/worktree-creation/worktree-creation-clock' import { formatBytes } from '../../../../../../shared/format-bytes' import type { RuntimeDownloadErrorCode } from '../../../../../../shared/runtime-downloads/runtime-download-contract' -import { - getRuntimeDownloadProgress, - subscribeRuntimeDownloadProgress, - type RuntimeDownloadProgressSnapshot -} from '@/runtime/files/downloads/runtime-download-progress-store' +import type { RuntimeDownloadProgressSnapshot } from '@/runtime/files/downloads/runtime-download-progress-store' + +export type RuntimeDownloadItemKind = 'file' | 'folder' +export type RuntimeDownloadToastIntent = 'save' | 'open' +/** Cloud workspaces live in a sandbox; other runtime hosts are named generically. */ +export type RuntimeDownloadPlace = 'sandbox' | 'host' + +export type RuntimeDownloadToastState = + | { + kind: 'progress' + intent: RuntimeDownloadToastIntent + itemKind: RuntimeDownloadItemKind + /** Local name being written, e.g. `src.zip`. */ + name: string + snapshot: RuntimeDownloadProgressSnapshot | null + } + | { + kind: 'done' + itemKind: RuntimeDownloadItemKind + /** Name of the saved file on this computer. */ + name: string + bytes: number + elapsedMs: number + fileCount?: number + excludedDefaultDirs?: boolean + skippedCount?: number + } + | { + kind: 'failed' + intent: RuntimeDownloadToastIntent + itemKind: RuntimeDownloadItemKind + name: string + code: RuntimeDownloadErrorCode + place: RuntimeDownloadPlace + } + | { kind: 'no_app'; name: string } + +export type RuntimeDownloadToastAction = 'cancel' | 'reveal' | 'open' | 'retry' | 'download_instead' + +const countFormat = new Intl.NumberFormat() +const formatCount = (value: number): string => countFormat.format(value) + +const CARD = + 'relative w-[356px] rounded-lg border border-border bg-popover px-3 py-2.5 text-sm text-popover-foreground shadow-lg' /** Fraction done, or null while the size is unknown (folder walk, empty file). */ export function runtimeDownloadFraction( @@ -20,120 +62,347 @@ export function runtimeDownloadFraction( return Math.min(1, snapshot.processedBytes / snapshot.totalBytes) } -export function RuntimeDownloadProgressBody({ - downloadId, - kind -}: { - downloadId: string - kind: 'file' | 'folder' -}): React.JSX.Element { - const t = useTranslate() - const snapshot = useSyncExternalStore( - (listener) => subscribeRuntimeDownloadProgress(downloadId, listener), - () => getRuntimeDownloadProgress(downloadId) - ) - const fraction = runtimeDownloadFraction(snapshot) - const percent = fraction === null ? null : Math.round(fraction * 100) - const progressLabel = t( - 'auto.components.right.sidebar.runtimeDownload.progressLabel', - 'Download progress' - ) - let detail: string +function progressDetail( + t: Translate, + itemKind: RuntimeDownloadItemKind, + snapshot: RuntimeDownloadProgressSnapshot | null +): string { if (!snapshot || snapshot.phase === 'preparing') { - detail = - kind === 'folder' - ? t('auto.components.right.sidebar.runtimeDownload.preparingFolder', 'Collecting files…') - : t('auto.components.right.sidebar.runtimeDownload.preparingFile', 'Starting…') - } else { - const received = formatBytes(snapshot.receivedBytes) - const rate = - snapshot.bytesPerSecond && snapshot.bytesPerSecond > 0 - ? t('auto.components.right.sidebar.runtimeDownload.rate', '{{value0}}/s', { - value0: formatBytes(snapshot.bytesPerSecond) - }) - : null - const amount = - kind === 'file' && snapshot.totalBytes - ? t( - 'auto.components.right.sidebar.runtimeDownload.amountOfTotal', - '{{value0}} of {{value1}}', - { - value0: received, - value1: formatBytes(snapshot.totalBytes) - } - ) - : received - detail = rate ? `${amount} · ${rate}` : amount + return itemKind === 'folder' + ? t('components.runtimeDownload.collectingFiles', 'Collecting files…') + : t('components.runtimeDownload.starting', 'Starting…') } - return ( -
- {percent === null ? ( - // Unknown size: an honest sweep beats a faked percentage. - - - - ) : ( - - )} -

{detail}

-
- ) + if (itemKind === 'folder') { + const amount = snapshot.totalBytes + ? t('components.runtimeDownload.amountOfTotal', '{{done}} of {{total}}', { + done: formatBytes(snapshot.processedBytes), + total: formatBytes(snapshot.totalBytes) + }) + : formatBytes(snapshot.receivedBytes) + return snapshot.fileCount === undefined + ? amount + : t('components.runtimeDownload.folderProgress', '{{files}} files · {{amount}}', { + count: snapshot.fileCount, + files: formatCount(snapshot.fileCount), + defaultValue_one: '{{files}} file · {{amount}}', + amount + }) + } + const amount = snapshot.totalBytes + ? t('components.runtimeDownload.amountOfTotal', '{{done}} of {{total}}', { + done: formatBytes(snapshot.receivedBytes), + total: formatBytes(snapshot.totalBytes) + }) + : formatBytes(snapshot.receivedBytes) + return snapshot.bytesPerSecond && snapshot.bytesPerSecond > 0 + ? t('components.runtimeDownload.amountAndRate', '{{amount}} · {{rate}}/s', { + amount, + rate: formatBytes(snapshot.bytesPerSecond) + }) + : amount } -export function runtimeDownloadErrorMessage( - code: RuntimeDownloadErrorCode, - hostMessage: string -): string { - switch (code) { +function failureCopy( + t: Translate, + state: Extract +): { title: string; detail: string; action: 'retry' | 'download_instead' | null } { + const { name, place } = state + const title = + state.intent === 'open' + ? t('components.runtimeDownload.openFailedTitle', 'Couldn’t open {{name}}', { name }) + : t('components.runtimeDownload.failedTitle', 'Couldn’t download {{name}}', { name }) + switch (state.code) { case 'too_large': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorTooLarge', - 'It is larger than the 2 GB download limit. Download smaller folders or individual files.' - ) + return state.intent === 'open' + ? { + title, + detail: t( + 'components.runtimeDownload.tooLargeToOpen', + 'Files over 1 GB can’t be opened directly. Download it instead.' + ), + action: 'download_instead' + } + : { + title, + detail: t( + 'components.runtimeDownload.tooLarge', + 'Folders over 2 GB can’t be downloaded at once. Download the folders inside it instead.' + ), + action: null + } case 'too_many_entries': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorTooManyEntries', - 'It has more than 65,000 files and folders. Download a smaller folder.' - ) + return { + title, + detail: t( + 'components.runtimeDownload.tooManyEntries', + 'Folders with more than 65,000 items can’t be downloaded at once. Download the folders inside it instead.' + ), + action: null + } case 'host_update_required': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorHostUpdate', - 'The remote host runs an older Orca server that cannot do this. Update it and try again.' - ) + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.sandboxUpdateRequired', + 'This sandbox runs an older version of Orca. New cloud workspaces include downloads. Your files are safe.' + ) + : t( + 'components.runtimeDownload.hostUpdateRequired', + 'The remote host runs an older version of Orca. Update Orca there, then try again.' + ), + action: null + } case 'host_busy': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorBusy', - 'Too many downloads are running. Wait for one to finish and try again.' - ) + return { + title, + detail: t( + 'components.runtimeDownload.busy', + 'Too many downloads are running. Wait for one to finish, then try again.' + ), + action: 'retry' + } case 'not_found': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorNotFound', - 'It no longer exists on the remote host.' - ) + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.notFoundInSandbox', + 'It was moved or deleted in the sandbox. Refresh the file explorer to see the latest files.' + ) + : t( + 'components.runtimeDownload.notFoundOnHost', + 'It was moved or deleted on the remote host. Refresh the file explorer to see the latest files.' + ), + action: null + } case 'not_a_file': case 'not_a_directory': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorChanged', - 'It changed on the remote host. Refresh the file explorer and try again.' - ) + return { + title, + detail: t( + 'components.runtimeDownload.changed', + 'It changed while Orca was reading it. Try again.' + ), + action: 'retry' + } case 'disconnected': - return translate( - 'auto.components.right.sidebar.runtimeDownload.errorDisconnected', - 'The connection to the remote host was lost. Reconnect and try again.' - ) + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.sandboxDisconnected', + 'The connection to the sandbox dropped. Nothing was saved on this computer.' + ) + : t( + 'components.runtimeDownload.hostDisconnected', + 'The connection to the remote host dropped. Nothing was saved on this computer.' + ), + action: 'retry' + } case 'open_failed': case 'transfer_failed': case 'canceled': - return hostMessage + return { + title, + detail: t( + 'components.runtimeDownload.transferFailed', + 'Something went wrong while copying it. Nothing was saved on this computer.' + ), + action: 'retry' + } } } + +function DismissButton({ onDismiss }: { onDismiss: () => void }): React.JSX.Element { + const t = useTranslate() + return ( + + ) +} + +/** + * Toast for a download from a runtime host: live progress with Cancel, a done card whose actions + * open the result on this computer, or one error with at most one next action. + */ +export function RuntimeDownloadToast({ + state, + revealLabel, + onAction, + onDismiss +}: { + state: RuntimeDownloadToastState + /** Platform name for revealing a path, e.g. "Reveal in Finder". */ + revealLabel: string + onAction: (action: RuntimeDownloadToastAction) => void + onDismiss: () => void +}): React.JSX.Element { + const t = useTranslate() + if (state.kind === 'progress') { + const fraction = runtimeDownloadFraction(state.snapshot) + const percent = fraction === null ? null : Math.round(fraction * 100) + const detail = progressDetail(t, state.itemKind, state.snapshot) + const title = + state.intent === 'open' + ? t('components.runtimeDownload.opening', 'Opening {{name}}', { name: state.name }) + : t('components.runtimeDownload.downloading', 'Downloading {{name}}', { name: state.name }) + const progressLabel = t('components.runtimeDownload.progressLabel', 'Download progress') + return ( +
+
+ +

+ {title} +

+ +
+
+ {percent === null ? ( + // Unknown size: an honest sweep beats a faked percentage. + + + + ) : ( + + )} +

{detail}

+
+
+ ) + } + if (state.kind === 'done') { + const parts = + state.itemKind === 'folder' && state.fileCount !== undefined + ? [ + t('components.runtimeDownload.fileCount', '{{files}} files', { + count: state.fileCount, + files: formatCount(state.fileCount), + defaultValue_one: '{{files}} file' + }), + formatBytes(state.bytes) + ] + : [formatBytes(state.bytes)] + parts.push(formatElapsedTime(state.elapsedMs)) + const notes: string[] = [] + if (state.excludedDefaultDirs) { + notes.push( + t('components.runtimeDownload.leftOutDefaultDirs', 'Left out node_modules and .git.') + ) + } + if ((state.skippedCount ?? 0) > 0) { + notes.push( + t( + 'components.runtimeDownload.skipped', + '{{count}} items couldn’t be read and were left out.', + { + count: state.skippedCount, + defaultValue_one: '{{count}} item couldn’t be read and was left out.' + } + ) + ) + } + return ( +
+
+ +
+

+ {t('components.runtimeDownload.downloaded', 'Downloaded {{name}}', { + name: state.name + })} +

+

{parts.join(' · ')}

+ {notes.map((note) => ( +

+ {note} +

+ ))} +
+ + {state.itemKind === 'file' ? ( + + ) : null} +
+
+
+ +
+ ) + } + const copy = + state.kind === 'no_app' + ? { + title: t( + 'components.runtimeDownload.noAppTitle', + 'No app on this computer opens {{name}}', + { + name: state.name + } + ), + detail: t( + 'components.runtimeDownload.noAppDetail', + 'Orca saved a copy. Reveal it to choose an app.' + ), + action: 'reveal' as const + } + : failureCopy(t, state) + const actionLabel = + copy.action === 'retry' + ? t('components.runtimeDownload.retry', 'Try again') + : copy.action === 'download_instead' + ? t('components.runtimeDownload.downloadInstead', 'Download') + : revealLabel + return ( + // The action sits under the text inside the error box: a 356px toast has no room beside it. +
+ + {copy.action ? ( + + ) : null} + +
+ ) +} diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index bbcfd5d5afd..29e487e15aa 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -2024,6 +2024,14 @@ "workers_one": "{{count}} worker", "workers_other": "{{count}} workers" }, + "runtimeDownload": { + "fileCount_one": "{{files}} file", + "fileCount_other": "{{files}} files", + "folderProgress_one": "{{files}} file · {{amount}}", + "folderProgress_other": "{{files}} files · {{amount}}", + "skipped_one": "{{count}} item couldn’t be read and was left out.", + "skipped_other": "{{count}} items couldn’t be read and were left out." + }, "tab": { "bar": { "bulkClose": { diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 4a085520371..1e1b67c905c 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -14650,7 +14650,9 @@ "clipboardStagingUnavailable": "Could not copy the file because Orca's temporary storage is unavailable", "revealInFinder": "Reveal in Finder", "openContainingFolder": "Open Containing Folder", - "revealInFileExplorer": "Reveal in File Explorer" + "revealInFileExplorer": "Reveal in File Explorer", + "downloadFolderZip": "Download as ZIP…", + "openLocally": "Open on This Computer" }, "FileExplorerToolbar": { "d238264654": "Show Git Ignored Files", @@ -22922,6 +22924,44 @@ "terminalOutput": "Saved output of {{title}}", "noGit": "No source control state was saved." } + }, + "runtimeDownload": { + "collectingFiles": "Collecting files…", + "starting": "Starting…", + "amountOfTotal": "{{done}} of {{total}}", + "amountAndRate": "{{amount}} · {{rate}}/s", + "openFailedTitle": "Couldn’t open {{name}}", + "failedTitle": "Couldn’t download {{name}}", + "tooLargeToOpen": "Files over 1 GB can’t be opened directly. Download it instead.", + "tooLarge": "Folders over 2 GB can’t be downloaded at once. Download the folders inside it instead.", + "tooManyEntries": "Folders with more than 65,000 items can’t be downloaded at once. Download the folders inside it instead.", + "sandboxUpdateRequired": "This sandbox runs an older version of Orca. New cloud workspaces include downloads. Your files are safe.", + "hostUpdateRequired": "The remote host runs an older version of Orca. Update Orca there, then try again.", + "busy": "Too many downloads are running. Wait for one to finish, then try again.", + "notFoundInSandbox": "It was moved or deleted in the sandbox. Refresh the file explorer to see the latest files.", + "notFoundOnHost": "It was moved or deleted on the remote host. Refresh the file explorer to see the latest files.", + "changed": "It changed while Orca was reading it. Try again.", + "sandboxDisconnected": "The connection to the sandbox dropped. Nothing was saved on this computer.", + "hostDisconnected": "The connection to the remote host dropped. Nothing was saved on this computer.", + "transferFailed": "Something went wrong while copying it. Nothing was saved on this computer.", + "dismiss": "Dismiss", + "opening": "Opening {{name}}", + "downloading": "Downloading {{name}}", + "progressLabel": "Download progress", + "cancel": "Cancel", + "leftOutDefaultDirs": "Left out node_modules and .git.", + "skipped_one": "{{count}} item couldn’t be read and was left out.", + "skipped_other": "{{count}} items couldn’t be read and were left out.", + "downloaded": "Downloaded {{name}}", + "open": "Open", + "noAppTitle": "No app on this computer opens {{name}}", + "noAppDetail": "Orca saved a copy. Reveal it to choose an app.", + "retry": "Try again", + "downloadInstead": "Download", + "folderProgress_one": "{{files}} file · {{amount}}", + "folderProgress_other": "{{files}} files · {{amount}}", + "fileCount_one": "{{files}} file", + "fileCount_other": "{{files}} files" } }, "dashboardPopout": { diff --git a/src/renderer/src/lib/cloud-remote-desktop/cloud-environment-ids.ts b/src/renderer/src/lib/cloud-remote-desktop/cloud-environment-ids.ts index 5740e280a8c..7b4ca0c192c 100644 --- a/src/renderer/src/lib/cloud-remote-desktop/cloud-environment-ids.ts +++ b/src/renderer/src/lib/cloud-remote-desktop/cloud-environment-ids.ts @@ -33,6 +33,11 @@ function subscribe(listener: () => void): () => void { return () => listeners.delete(listener) } +/** Non-React read of the last loaded list; call refreshCloudEnvironmentIds first when it matters. */ +export function isKnownCloudEnvironment(environmentId: string): boolean { + return ids.has(environmentId) +} + export function useIsCloudEnvironment(environmentId: string | null): boolean { return useSyncExternalStore(subscribe, () => (environmentId ? ids.has(environmentId) : false)) } diff --git a/src/shared/runtime-downloads/runtime-download-contract.ts b/src/shared/runtime-downloads/runtime-download-contract.ts index dbdb5ec71de..3aa7ba861d3 100644 --- a/src/shared/runtime-downloads/runtime-download-contract.ts +++ b/src/shared/runtime-downloads/runtime-download-contract.ts @@ -37,6 +37,8 @@ export type RuntimeDownloadProgressEvent = { receivedBytes: number totalBytes: number | null processedBytes: number + /** Folder downloads: files in the archive, known once the host has listed the folder. */ + fileCount?: number /** Present once a save location is chosen. */ destinationPath?: string } diff --git a/src/shared/runtime-rpc-call-queue.test.ts b/src/shared/runtime-rpc-call-queue.test.ts index 6c870dbdb3e..6c4c26c96d4 100644 --- a/src/shared/runtime-rpc-call-queue.test.ts +++ b/src/shared/runtime-rpc-call-queue.test.ts @@ -100,6 +100,44 @@ describe('runtime RPC call queue', () => { await expect(Promise.all(reads)).resolves.toEqual(Array(6).fill('chunk')) }) + it('runs a download in its own four-wide lane beside preview reads', async () => { + const queue = new RuntimeRpcCallQueuePool(8, 2) + const previewRelease: (() => void)[] = [] + const downloadRelease: (() => void)[] = [] + const previews = Array.from({ length: 3 }, () => + queue.enqueue('web-runtime', 'files.readChunk', async () => { + await new Promise((resolve) => previewRelease.push(resolve)) + return 'preview' + }) + ) + const downloads = Array.from({ length: 6 }, () => + queue.enqueue( + 'web-runtime', + 'files.readChunk', + async () => { + await new Promise((resolve) => downloadRelease.push(resolve)) + return 'page' + }, + 0, + undefined, + 'download' + ) + ) + await vi.waitFor(() => expect(downloadRelease).toHaveLength(4)) + expect(previewRelease).toHaveLength(2) + + for (let finished = 0; finished < 6; finished += 1) { + await vi.waitFor(() => expect(downloadRelease.length).toBeGreaterThan(0)) + downloadRelease.shift()?.() + } + await expect(Promise.all(downloads)).resolves.toEqual(Array(6).fill('page')) + for (let finished = 0; finished < 3; finished += 1) { + await vi.waitFor(() => expect(previewRelease.length).toBeGreaterThan(0)) + previewRelease.shift()?.() + } + await expect(Promise.all(previews)).resolves.toEqual(Array(3).fill('preview')) + }) + it('frees the queue slot when a runtime call throws synchronously', async () => { const queue = new RuntimeRpcCallQueuePool(1, 1) const first = queue.enqueue('web-runtime', 'status.get', () => { diff --git a/src/shared/runtime-rpc-call-queue.ts b/src/shared/runtime-rpc-call-queue.ts index 759d8e9d8c0..82045c07183 100644 --- a/src/shared/runtime-rpc-call-queue.ts +++ b/src/shared/runtime-rpc-call-queue.ts @@ -4,6 +4,9 @@ import { REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES } from './remote-runtime/remote-r const DEFAULT_REMOTE_RUNTIME_CALL_CONCURRENCY = 8 const DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY = 2 const REMOTE_RUNTIME_BULK_READ_CONCURRENCY = 2 +// Explicit file downloads pipeline more pages than media/preview reads: at ~65 ms RTT two 512 KiB +// pages in flight cap a 200 MB download near 9 MB/s, four clear 10 MB/s. +const REMOTE_RUNTIME_DOWNLOAD_CONCURRENCY = 4 export const RUNTIME_RPC_MAX_QUEUED_CALLS_PER_SELECTOR = 256 export const RUNTIME_RPC_MAX_QUEUED_CALLS_TOTAL = 2_048 export const RUNTIME_RPC_QUEUE_OVERLOAD_CODE = 'runtime_rpc_queue_overloaded' @@ -69,6 +72,9 @@ function isBulkReadRuntimeMethod(method: string): boolean { ) } +/** `download`: a user-started transfer that gets its own lane, so it neither starves nor is starved by previews. */ +export type RuntimeRpcCallLane = 'download' + export class RuntimeRpcCallQueuePool { private readonly queues = new Map() private queuedCallCount = 0 @@ -87,18 +93,22 @@ export class RuntimeRpcCallQueuePool { method: string, run: () => Promise, retainedBytes = 0, - signal?: AbortSignal + signal?: AbortSignal, + lane?: RuntimeRpcCallLane ): Promise { if (signal?.aborted) { return Promise.reject(abortSignalReason(signal)) } // Same concurrency bound, counted apart from the selector's other calls; global caps still apply. - const bulkRead = isBulkReadRuntimeMethod(method) - const queueKey = isLongWaitRuntimeMethod(method) - ? `${selector}\u0000long-wait` - : bulkRead - ? `${selector}\u0000bulk-read` - : selector + const download = lane === 'download' + const bulkRead = !download && isBulkReadRuntimeMethod(method) + const queueKey = download + ? `${selector}\u0000download` + : isLongWaitRuntimeMethod(method) + ? `${selector}\u0000long-wait` + : bulkRead + ? `${selector}\u0000bulk-read` + : selector if (this.queuedCallCount >= this.maxQueuedTotal) { return Promise.reject(new RuntimeRpcCallQueueOverloadError('global')) } @@ -116,7 +126,11 @@ export class RuntimeRpcCallQueuePool { const queue = this.getQueue( queueKey, - bulkRead ? Math.min(this.concurrency, REMOTE_RUNTIME_BULK_READ_CONCURRENCY) : this.concurrency + download + ? Math.min(this.concurrency, REMOTE_RUNTIME_DOWNLOAD_CONCURRENCY) + : bulkRead + ? Math.min(this.concurrency, REMOTE_RUNTIME_BULK_READ_CONCURRENCY) + : this.concurrency ) return new Promise((resolve, reject) => { const call: QueuedRuntimeCall = { From b6418aeb455822ad1d363c6bb6fdff1e6ffed227 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 07:51:21 -0800 Subject: [PATCH 04/30] chore(files): drop casts and real-time waits flagged by the quality gates --- .../runtime-download-errors.ts | 7 ++-- .../runtime-download-handlers.ts | 35 +++++++++++-------- .../runtime-download-transfer.test.ts | 26 +++++++++----- .../archive/folder-archive-sessions.test.ts | 8 ++--- .../files/archive/folder-archive-walk.ts | 3 +- .../archive/folder-archive-zip-stream.ts | 2 +- 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/src/main/runtime-downloads/runtime-download-errors.ts b/src/main/runtime-downloads/runtime-download-errors.ts index 67849ecc383..33244d65177 100644 --- a/src/main/runtime-downloads/runtime-download-errors.ts +++ b/src/main/runtime-downloads/runtime-download-errors.ts @@ -39,9 +39,8 @@ export function toRuntimeDownloadError(error: unknown, signal: AbortSignal): Run return error } const message = error instanceof Error ? error.message : String(error) - const code = - typeof (error as { code?: unknown } | null)?.code === 'string' - ? (error as { code: string }).code - : 'runtime_error' + const rawCode = + typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined + const code = typeof rawCode === 'string' ? rawCode : 'runtime_error' return runtimeDownloadErrorFromResponse({ code, message }) } diff --git a/src/main/runtime-downloads/runtime-download-handlers.ts b/src/main/runtime-downloads/runtime-download-handlers.ts index 52ad99a4e8f..ec1ecbf5b1e 100644 --- a/src/main/runtime-downloads/runtime-download-handlers.ts +++ b/src/main/runtime-downloads/runtime-download-handlers.ts @@ -1,5 +1,6 @@ import { app, BrowserWindow, dialog, ipcMain, shell, type WebContents } from 'electron' import { open } from 'node:fs/promises' +import { z } from 'zod' import { join } from 'node:path' import { RUNTIME_OPEN_LOCALLY_MAX_BYTES, @@ -37,26 +38,29 @@ const MAX_ACTIVE_DOWNLOADS = 8 const activeDownloads = new Map() +const Text = z.string().max(4096) +const RequiredText = Text.refine((value) => value.trim() !== '') +const StartArgs = z.object({ + downloadId: RequiredText, + environmentId: RequiredText, + worktree: RequiredText, + relativePath: Text, + name: RequiredText, + kind: z.enum(['file', 'folder']), + intent: z.enum(['save', 'open']), + excludeDefaultDirs: z.boolean().optional(), + expectedEnvironmentPairingRevision: z.number().int().optional() +}) satisfies z.ZodType + function validateArgs(args: unknown): RuntimeDownloadStartArgs { - const value = args as Partial | null - const isText = (field: unknown, allowEmpty = false): field is string => - typeof field === 'string' && (allowEmpty || field.trim() !== '') && field.length <= 4096 - if ( - !value || - !isText(value.downloadId) || - !isText(value.environmentId) || - !isText(value.worktree) || - !isText(value.relativePath, true) || - !isText(value.name) || - (value.kind !== 'file' && value.kind !== 'folder') || - (value.intent !== 'save' && value.intent !== 'open') - ) { + const parsed = StartArgs.safeParse(args) + if (!parsed.success) { throw new Error('Invalid runtime download request') } - if (value.intent === 'open' && value.kind !== 'file') { + if (parsed.data.intent === 'open' && parsed.data.kind !== 'file') { throw new Error('Only files can be opened locally') } - return value as RuntimeDownloadStartArgs + return parsed.data } function createCall(args: RuntimeDownloadStartArgs): RuntimeDownloadCall { @@ -78,6 +82,7 @@ function createCall(args: RuntimeDownloadStartArgs): RuntimeDownloadCall { if (!response.ok) { throw runtimeDownloadErrorFromResponse(response.error) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: each caller names the result type of the host method it calls; the host's RPC contract defines that shape. return response.result as T } } diff --git a/src/main/runtime-downloads/runtime-download-transfer.test.ts b/src/main/runtime-downloads/runtime-download-transfer.test.ts index 2073967cce7..eb5030706e9 100644 --- a/src/main/runtime-downloads/runtime-download-transfer.test.ts +++ b/src/main/runtime-downloads/runtime-download-transfer.test.ts @@ -1,7 +1,7 @@ import { mkdtemp, open, readFile, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { RuntimeDownloadError, transferRuntimeFile, @@ -11,6 +11,12 @@ import { let dir: string +/** A scripted host; the transfer code names each method's result type itself. */ +function fakeCall

(impl: (method: string, params: P) => Promise): RuntimeDownloadCall { + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: each scripted reply matches the result type the transfer code expects for that method. + return impl as RuntimeDownloadCall +} + beforeEach(async () => { dir = await mkdtemp(join(tmpdir(), 'orca-runtime-download-')) }) @@ -20,19 +26,21 @@ afterEach(async () => { }) function fileHost(content: Buffer, statSize = content.byteLength): RuntimeDownloadCall { - return (async (method: string, params: { offset: number; length: number }) => { + return fakeCall(async (method: string, params: { offset: number; length: number }) => { if (method === 'files.stat') { return { size: statSize, isDirectory: false } } const bytes = content.subarray(params.offset, params.offset + params.length) // Out-of-order replies: later pages may land first. - await new Promise((resolve) => setTimeout(resolve, params.offset % 3)) + for (let turn = 0; turn < (params.offset / 7) % 3; turn += 1) { + await Promise.resolve() + } return { contentBase64: bytes.toString('base64'), bytesRead: bytes.byteLength, eof: params.offset + bytes.byteLength >= content.byteLength } - }) as RuntimeDownloadCall + }) } async function runFile(call: RuntimeDownloadCall, extra: { maxBytes?: number } = {}) { @@ -85,7 +93,7 @@ describe('transferRuntimeFolder', () => { it('releases the host archive when the download is canceled', async () => { const controller = new AbortController() const calls: string[] = [] - const call = (async (method: string) => { + const call = fakeCall(async (method: string) => { calls.push(method) if (method === 'files.archiveStart') { return { archiveId: 'z1', totalBytes: 10, excludedDirCount: 0, fileCount: 1 } @@ -101,7 +109,7 @@ describe('transferRuntimeFolder', () => { } } return { released: true } - }) as RuntimeDownloadCall + }) const sink = await open(join(dir, 'out.zip'), 'w+') try { await expect( @@ -125,7 +133,7 @@ describe('transferRuntimeFolder', () => { const controller = new AbortController() const calls: { method: string; params: unknown }[] = [] let finishStart: (value: unknown) => void = () => {} - const call = (async (method: string, params: unknown) => { + const call = fakeCall(async (method: string, params: unknown) => { calls.push({ method, params }) if (method === 'files.archiveStart') { return new Promise((resolve) => { @@ -133,7 +141,7 @@ describe('transferRuntimeFolder', () => { }) } return { released: true } - }) as RuntimeDownloadCall + }) const sink = await open(join(dir, 'late.zip'), 'w+') try { const transfer = transferRuntimeFolder({ @@ -149,7 +157,7 @@ describe('transferRuntimeFolder', () => { // Canceled at once, before the host has even finished walking. await expect(transfer).rejects.toEqual(new RuntimeDownloadError('canceled')) finishStart({ archiveId: 'late', totalBytes: 1, excludedDirCount: 0, fileCount: 1 }) - await new Promise((resolve) => setTimeout(resolve, 0)) + await vi.waitFor(() => expect(calls).toHaveLength(2)) } finally { await sink.close() } diff --git a/src/main/runtime/files/archive/folder-archive-sessions.test.ts b/src/main/runtime/files/archive/folder-archive-sessions.test.ts index e3e3e73fda3..9171aef54ad 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.test.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.test.ts @@ -2,7 +2,7 @@ import { mkdtemp, mkdir, rename, rm, symlink, writeFile, chmod } from 'node:fs/p import { tmpdir } from 'node:os' import { join } from 'node:path' import { unzipSync } from 'fflate' -import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { FolderArchiveSessions } from './folder-archive-sessions' import { FolderArchiveLimitError, @@ -181,8 +181,7 @@ describe('FolderArchiveSessions', () => { await expect( sessions.start({ folderPath: root, ownerKey: 'a', excludeDefaultDirs: true }) ).rejects.toThrow('archive_busy') - await new Promise((resolve) => setTimeout(resolve, 60)) - expect(sessions.activeCount()).toBe(0) + await vi.waitFor(() => expect(sessions.activeCount()).toBe(0)) await expect( sessions.read({ archiveId: first.archiveId, ownerKey: 'a', maxBytes: 10 }) ).rejects.toThrow('archive_not_found') @@ -192,7 +191,8 @@ describe('FolderArchiveSessions', () => { await writeFile(join(root, 'big.png'), Buffer.alloc(4 * 1024 * 1024, 7)) const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: true }) const stream = new FolderArchiveZipStream(plan.entries, 512 * 1024) - await new Promise((resolve) => setTimeout(resolve, 50)) + // The producer fills to the mark, then must stop until something is read. + await vi.waitFor(() => expect(stream.processedBytes).toBeGreaterThanOrEqual(512 * 1024)) expect(stream.processedBytes).toBeLessThanOrEqual(1024 * 1024) let total = 0 for (;;) { diff --git a/src/main/runtime/files/archive/folder-archive-walk.ts b/src/main/runtime/files/archive/folder-archive-walk.ts index 2498ee91012..250ae37e912 100644 --- a/src/main/runtime/files/archive/folder-archive-walk.ts +++ b/src/main/runtime/files/archive/folder-archive-walk.ts @@ -197,7 +197,8 @@ async function isUnmovedDirectory(absolutePath: string): Promise { } export function isVanishedOrDenied(error: unknown): boolean { - const code = (error as NodeJS.ErrnoException | null)?.code + const code = + typeof error === 'object' && error !== null && 'code' in error ? error.code : undefined return ( code === 'ENOENT' || code === 'ENOTDIR' || diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts index 85fe4411fe7..f1de2c7c269 100644 --- a/src/main/runtime/files/archive/folder-archive-zip-stream.ts +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -223,7 +223,7 @@ export class FolderArchiveZipStream { } this.processedBytes += bytesRead // fflate may keep a reference until it deflates, so hand it a copy. - file.push(Uint8Array.prototype.slice.call(buffer, 0, bytesRead) as Uint8Array, false) + file.push(new Uint8Array(buffer.subarray(0, bytesRead)), false) } } finally { await handle.close().catch(() => {}) From 244a7c5134835079897bd05797be963678665369 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:35:26 -0800 Subject: [PATCH 05/30] fix(files): harden runtime downloads and rework the toasts after security, design and FE review Host archive: fd realpath must stay under the folder (Linux /proc/self/fd), O_NONBLOCK opens, directories re-verified (realpath + dev/ino) before and after listing, reads capped at the walked size, links that leave the folder dropped, case/NFC twins renamed, Windows-invalid and reserved names and bidi/zero-width characters mapped, slots reserved before the walk and limited per client. Desktop: hard byte caps against a host that keeps sending, executable types are never opened (Download only) and every saved file gets the OS downloaded-file mark, open-locally cache under userData, busy limit counts transfers not dialogs. Toasts: InlineError layout=toast with one action, neutral tone for limits/refusals/no-app, Refresh for moved files, errors stay until dismissed, ETA, zip size label, title-only live region, stable store subscription, one transfer per path and intent, renderer tests. --- .../ipc/filesystem/local-download-filename.ts | 5 +- .../runtime-download-handlers.ts | 53 +- .../runtime-download-transfer.test.ts | 15 + .../runtime-download-transfer.ts | 20 + .../runtime-open-local-cache.ts | 15 +- .../archive/folder-archive-file-reads.ts | 48 +- .../archive/folder-archive-sessions.test.ts | 101 +++- .../files/archive/folder-archive-sessions.ts | 77 ++- .../files/archive/folder-archive-walk.ts | 84 +++- .../archive/folder-archive-zip-stream.ts | 33 +- .../panel/RuntimeDownloads.fixture.tsx | 64 ++- .../file-explorer-row-action-visibility.ts | 13 +- .../panel/file-explorer-row-context-menu.tsx | 5 +- .../panel/runtime-download-actions.test.ts | 135 +++++ .../panel/runtime-download-actions.tsx | 57 ++- .../panel/runtime-download-failure-copy.ts | 183 +++++++ .../panel/runtime-download-toast.tsx | 471 ++++++++---------- .../panel/useFileExplorerManualRefresh.ts | 4 + .../file-explorer-refresh-requests.ts | 16 + .../src/i18n/en-runtime-required.json | 4 +- src/renderer/src/i18n/locales/en.json | 28 +- .../runtime-download-progress-store.ts | 15 +- .../runtime-download-contract.ts | 11 + 23 files changed, 1078 insertions(+), 379 deletions(-) create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.test.ts create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-failure-copy.ts create mode 100644 src/renderer/src/components/right-sidebar/file-explorer/watching/file-explorer-refresh-requests.ts diff --git a/src/main/ipc/filesystem/local-download-filename.ts b/src/main/ipc/filesystem/local-download-filename.ts index 2880cd9035a..24e271da139 100644 --- a/src/main/ipc/filesystem/local-download-filename.ts +++ b/src/main/ipc/filesystem/local-download-filename.ts @@ -2,8 +2,11 @@ const WINDOWS_RESERVED_LOCAL_BASENAME = /^(?:con|prn|aux|nul|conin\$|conout\$|com[1-9\u00B9\u00B2\u00B3]|lpt[1-9\u00B9\u00B2\u00B3])(?:\..*)?$/i const LOCAL_FILENAME_REPLACEMENT_CHARS = new Set(['<', '>', ':', '"', '/', '\\', '|', '?', '*']) +// Bidi overrides and zero-width characters let `exe.txt` display as `txt.exe` in a save dialog. +const INVISIBLE_FORMAT_CHARS = /[\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/g + export function sanitizeLocalDownloadFilename(remoteBasename: string): string { - const sanitized = Array.from(remoteBasename, (char) => + const sanitized = Array.from(remoteBasename.replace(INVISIBLE_FORMAT_CHARS, ''), (char) => char.charCodeAt(0) < 32 || LOCAL_FILENAME_REPLACEMENT_CHARS.has(char) ? '_' : char ) .join('') diff --git a/src/main/runtime-downloads/runtime-download-handlers.ts b/src/main/runtime-downloads/runtime-download-handlers.ts index ec1ecbf5b1e..00bac9fe62b 100644 --- a/src/main/runtime-downloads/runtime-download-handlers.ts +++ b/src/main/runtime-downloads/runtime-download-handlers.ts @@ -1,8 +1,9 @@ import { app, BrowserWindow, dialog, ipcMain, shell, type WebContents } from 'electron' -import { open } from 'node:fs/promises' +import { open, rm } from 'node:fs/promises' import { z } from 'zod' -import { join } from 'node:path' +import { dirname, join } from 'node:path' import { + canOpenRuntimeDownloadLocally, RUNTIME_OPEN_LOCALLY_MAX_BYTES, type RuntimeDownloadProgressEvent, type RuntimeDownloadResult, @@ -18,6 +19,7 @@ import { promoteDownloadedFile } from '../ipc/filesystem/filesystem-download-promotion' import { abortWhenRendererGone } from '../ipc/renderer-lifetime-abort' +import { markPluginSavedFileAsDownloaded as markFileAsDownloaded } from '../plugins/panels/plugin-save-as-download-mark' import { RuntimeDownloadError, transferRuntimeFile, @@ -37,6 +39,9 @@ const PROGRESS_INTERVAL_MS = 120 const MAX_ACTIVE_DOWNLOADS = 8 const activeDownloads = new Map() +// Counted from the moment a destination is chosen: a download still at its save dialog isn't +// using the link and has no toast to cancel it from. +let transferringCount = 0 const Text = z.string().max(4096) const RequiredText = Text.refine((value) => value.trim() !== '') @@ -143,10 +148,40 @@ async function runDownload( args: RuntimeDownloadStartArgs, controller: AbortController ): Promise { + if ( + args.intent === 'open' && + !canOpenRuntimeDownloadLocally(sanitizeLocalDownloadFilename(args.name)) + ) { + throw new RuntimeDownloadError('open_refused') + } const destinationPath = await chooseDestination(sender, args) if (!destinationPath || controller.signal.aborted) { return { status: 'canceled' } } + if (transferringCount >= MAX_ACTIVE_DOWNLOADS) { + if (args.intent === 'open') { + await rm(dirname(destinationPath), { recursive: true, force: true }).catch(() => {}) + } + return { + status: 'error', + code: 'host_busy', + message: 'Too many downloads are running; wait for one to finish.' + } + } + transferringCount += 1 + try { + return await transferToDestination(sender, args, controller, destinationPath) + } finally { + transferringCount -= 1 + } +} + +async function transferToDestination( + sender: WebContents, + args: RuntimeDownloadStartArgs, + controller: AbortController, + destinationPath: string +): Promise { const emitter = createProgressEmitter(sender, args.downloadId) emitter.setDestination(destinationPath) emitter.preparing() @@ -179,6 +214,9 @@ async function runDownload( } await promoteDownloadedFile(tempPath, destinationPath, existed) promoted = true + // The bytes come from a remote machine: mark them like a browser download so Gatekeeper, + // SmartScreen and Protected View still apply (a zip's mark carries over to what it extracts). + await markFileAsDownloaded(destinationPath) let openError: string | undefined if (args.intent === 'open') { const message = await shell.openPath(destinationPath) @@ -194,6 +232,10 @@ async function runDownload( await sink.close().catch(() => {}) if (!promoted) { await cleanupLocalTransferPath(tempPath) + if (args.intent === 'open') { + // The per-open folder holds nothing else; don't leave it behind empty. + await rm(dirname(destinationPath), { recursive: true, force: true }).catch(() => {}) + } } } } @@ -206,13 +248,6 @@ export function registerRuntimeDownloadHandlers(): void { if (activeDownloads.has(args.downloadId)) { throw new Error('Download already running') } - if (activeDownloads.size >= MAX_ACTIVE_DOWNLOADS) { - return { - status: 'error', - code: 'host_busy', - message: 'Too many downloads are running; wait for one to finish.' - } - } const controller = new AbortController() activeDownloads.set(args.downloadId, controller) // Closing the window must not leave a transfer running nobody can cancel. diff --git a/src/main/runtime-downloads/runtime-download-transfer.test.ts b/src/main/runtime-downloads/runtime-download-transfer.test.ts index eb5030706e9..368f8e416e2 100644 --- a/src/main/runtime-downloads/runtime-download-transfer.test.ts +++ b/src/main/runtime-downloads/runtime-download-transfer.test.ts @@ -87,6 +87,21 @@ describe('transferRuntimeFile', () => { new RuntimeDownloadError('too_large') ) }) + + it('stops a host that keeps sending pages past the limit', async () => { + const endless = fakeCall(async (method: string, params: { length: number }) => + method === 'files.stat' + ? { size: 10, isDirectory: false } + : { + contentBase64: Buffer.alloc(params.length, 1).toString('base64'), + bytesRead: params.length, + eof: false + } + ) + await expect(runFile(endless, { maxBytes: 50 })).rejects.toEqual( + new RuntimeDownloadError('too_large') + ) + }) }) describe('transferRuntimeFolder', () => { diff --git a/src/main/runtime-downloads/runtime-download-transfer.ts b/src/main/runtime-downloads/runtime-download-transfer.ts index 758262d5cb9..9a0e24f4eb9 100644 --- a/src/main/runtime-downloads/runtime-download-transfer.ts +++ b/src/main/runtime-downloads/runtime-download-transfer.ts @@ -1,7 +1,9 @@ import type { FileHandle } from 'node:fs/promises' import type { RuntimeFileReadChunkResult } from '../../shared/file-content/runtime-file-read' import { + FOLDER_ARCHIVE_MAX_ENTRIES, FOLDER_ARCHIVE_MAX_READ_BYTES, + FOLDER_ARCHIVE_MAX_TOTAL_BYTES, type FileArchiveReadResult, type FileArchiveStartResult } from '../../shared/rpc-contract/files-archive-params' @@ -14,6 +16,13 @@ const FILE_CHUNK_CONCURRENCY = 4 const CHUNK_TIMEOUT_MS = 60_000 // The walk of a large tree runs before the first byte; give it room. const ARCHIVE_START_TIMEOUT_MS = 120_000 +// The host is not trusted to stop. A file may grow a little while it is copied; past this much over +// its announced size the download stops instead of filling this computer's disk. +const FILE_GROWTH_ALLOWANCE_BYTES = 64 * 1024 * 1024 +// A ZIP of at most 2 GiB of files: deflate never grows data by more than ~0.1 %, plus local and +// central headers (well under 1 KiB each for 65,000 entries). +const FOLDER_ARCHIVE_MAX_ZIP_BYTES = + FOLDER_ARCHIVE_MAX_TOTAL_BYTES * 1.01 + FOLDER_ARCHIVE_MAX_ENTRIES * 1024 export class RuntimeDownloadError extends Error { constructor( @@ -66,6 +75,11 @@ export async function transferRuntimeFile( if (input.maxBytes !== undefined && stat.size > input.maxBytes) { throw new RuntimeDownloadError('too_large') } + // Hard stop for a host that keeps answering full pages past the size it announced. + const byteLimit = Math.min( + input.maxBytes ?? Number.POSITIVE_INFINITY, + stat.size + FILE_GROWTH_ALLOWANCE_BYTES + ) let nextOffset = 0 // The file may change size mid-transfer; the first EOF page fixes the final length. let endOffset: number | null = null @@ -90,6 +104,9 @@ export async function transferRuntimeFile( }, { timeoutMs: CHUNK_TIMEOUT_MS, signal: input.signal } ) + if (offset + chunk.bytesRead > byteLimit) { + throw new RuntimeDownloadError('too_large') + } if (chunk.bytesRead > 0) { const bytes = Buffer.from(chunk.contentBase64, 'base64') if (bytes.byteLength !== chunk.bytesRead) { @@ -157,6 +174,9 @@ export async function transferRuntimeFolder( ) if (page.bytesRead > 0) { const bytes = Buffer.from(page.contentBase64, 'base64') + if (received + bytes.byteLength > FOLDER_ARCHIVE_MAX_ZIP_BYTES) { + throw new RuntimeDownloadError('too_large') + } await input.sink.write(bytes, 0, bytes.byteLength, received) received += bytes.byteLength } diff --git a/src/main/runtime-downloads/runtime-open-local-cache.ts b/src/main/runtime-downloads/runtime-open-local-cache.ts index 63c6bc25ea5..f136dc5d917 100644 --- a/src/main/runtime-downloads/runtime-open-local-cache.ts +++ b/src/main/runtime-downloads/runtime-open-local-cache.ts @@ -1,30 +1,37 @@ import { randomUUID } from 'node:crypto' import { mkdir, readdir, rm, stat } from 'node:fs/promises' -import { tmpdir } from 'node:os' import { join } from 'node:path' +import { app } from 'electron' // Opened copies only need to outlive the app that opens them; a day covers a long session. const OPEN_CACHE_MAX_AGE_MS = 24 * 60 * 60 * 1000 +// Under userData, not the OS temp dir: Linux /tmp is shared, and another local user could pre-create +// the folder and swap the file Orca is about to open. export function runtimeOpenLocalCacheRoot(): string { - return join(tmpdir(), 'orca-opened-remote-files') + return join(app.getPath('userData'), 'opened-remote-files') } /** A fresh directory per open, so two opens of same-named files never clobber each other. */ export async function createRuntimeOpenLocalDirectory(root = runtimeOpenLocalCacheRoot()) { + await mkdir(root, { recursive: true, mode: 0o700 }) const directory = join(root, randomUUID()) - await mkdir(directory, { recursive: true, mode: 0o700 }) + // Not recursive: a fresh random name that already exists means something else made it. + await mkdir(directory, { mode: 0o700 }) return directory } /** Best effort: removes cached copies older than a day. Failures never block an open. */ export async function pruneRuntimeOpenLocalCache( - root = runtimeOpenLocalCacheRoot(), + rootOverride?: string, now = Date.now(), maxAgeMs = OPEN_CACHE_MAX_AGE_MS ): Promise { + let root: string let names: string[] try { + // Inside the try: before app ready (or in tests) userData may not resolve. + root = rootOverride ?? runtimeOpenLocalCacheRoot() names = await readdir(root) } catch { return diff --git a/src/main/runtime/files/archive/folder-archive-file-reads.ts b/src/main/runtime/files/archive/folder-archive-file-reads.ts index bec9f106f25..72a979cf805 100644 --- a/src/main/runtime/files/archive/folder-archive-file-reads.ts +++ b/src/main/runtime/files/archive/folder-archive-file-reads.ts @@ -1,5 +1,6 @@ import { constants } from 'node:fs' -import { open, type FileHandle } from 'node:fs/promises' +import { open, readlink, realpath, type FileHandle } from 'node:fs/promises' +import { sep } from 'node:path' import type { FolderArchiveEntry } from './folder-archive-walk' /** Files up to this size are read whole, ahead of the zipper, so per-file open/stat/close overlaps. */ @@ -17,18 +18,30 @@ export function isSmallFile(entry: FolderArchiveEntry): boolean { return entry.kind === 'file' && entry.size <= SMALL_FILE_BYTES } +// O_NONBLOCK: a file swapped for a FIFO after the walk must not park a libuv thread in open(); +// fstat then refuses it. Regular files ignore the flag; Windows has neither flag. +const ARCHIVE_OPEN_FLAGS = + constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0) + /** * Opens a planned file without following a final link, and refuses it if it is no longer the file - * the walk saw. A parent directory swapped for a link after the walk slips past O_NOFOLLOW but - * changes dev/ino, so nothing outside the root is ever read. + * the walk saw or if the opened file does not live under `rootRealPath`. A parent directory swapped + * for a link after the walk slips past O_NOFOLLOW; the fd's real path (or, off Linux, a re-resolve + * plus dev/ino) catches that, so nothing outside the root is ever read. */ -export async function openUnchangedFile(entry: FolderArchiveEntry): Promise { - const handle = await open(entry.absolutePath, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0)) +export async function openUnchangedFile( + entry: FolderArchiveEntry, + rootRealPath: string +): Promise { + const handle = await open(entry.absolutePath, ARCHIVE_OPEN_FLAGS) try { const opened = await handle.stat() if (!opened.isFile() || opened.dev !== entry.dev || opened.ino !== entry.ino) { throw new ChangedSinceWalkError('archive_entry_changed') } + if (!isInsideRoot(await openedRealPath(handle, entry), rootRealPath)) { + throw new ChangedSinceWalkError('archive_entry_escaped') + } return handle } catch (error) { await handle.close().catch(() => {}) @@ -36,11 +49,32 @@ export async function openUnchangedFile(entry: FolderArchiveEntry): Promise { + if (process.platform === 'linux') { + try { + return await readlink(`/proc/self/fd/${handle.fd}`) + } catch { + // No /proc (unusual containers): fall through to the path check. + } + } + return realpath(entry.absolutePath) +} + +function isInsideRoot(path: string, rootRealPath: string): boolean { + const root = rootRealPath.endsWith(sep) ? rootRealPath : `${rootRealPath}${sep}` + return process.platform === 'win32' + ? path.toLowerCase().startsWith(root.toLowerCase()) + : path.startsWith(root) +} + /** Never rejects: failures come back as `skip` so a look-ahead read cannot go unhandled. */ -export async function readSmallFile(entry: FolderArchiveEntry): Promise { +export async function readSmallFile( + entry: FolderArchiveEntry, + rootRealPath: string +): Promise { let handle: FileHandle try { - handle = await openUnchangedFile(entry) + handle = await openUnchangedFile(entry, rootRealPath) } catch (error) { return { kind: 'skip', error } } diff --git a/src/main/runtime/files/archive/folder-archive-sessions.test.ts b/src/main/runtime/files/archive/folder-archive-sessions.test.ts index 9171aef54ad..f1e73c17f98 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.test.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.test.ts @@ -9,7 +9,7 @@ import { planFolderArchive, sanitizeArchiveSegment } from './folder-archive-walk' -import { FolderArchiveZipStream } from './folder-archive-zip-stream' +import { FolderArchiveZipStream, isContainedLinkTarget } from './folder-archive-zip-stream' let root: string @@ -21,6 +21,17 @@ afterEach(async () => { await rm(root, { recursive: true, force: true }) }) +async function zipEntries(stream: FolderArchiveZipStream) { + const parts: Buffer[] = [] + for (;;) { + const page = await stream.read(1 << 20) + parts.push(page.bytes) + if (page.eof) { + return unzipSync(Buffer.concat(parts)) + } + } +} + async function drain(sessions: FolderArchiveSessions, archiveId: string, ownerKey = 'device-a') { const parts: Buffer[] = [] for (;;) { @@ -77,28 +88,64 @@ describe('FolderArchiveSessions', () => { expect(plan.entries.map((entry) => entry.archivePath)).toContain('r/node_modules/x.js') }) - it('stores symlinks as links and never reads what they point at', async () => { + it('keeps links that stay inside the folder and drops ones that point out of it', async () => { const outside = join(root, 'outside-secret.txt') await writeFile(outside, 'secret') const project = join(root, 'p') - await mkdir(project) - await symlink(outside, join(project, 'link')) + await mkdir(join(project, 'sub'), { recursive: true }) + await writeFile(join(project, 'target.txt'), 'in') + await symlink(outside, join(project, 'abs-link')) + await symlink('../outside-secret.txt', join(project, 'rel-escape')) await symlink(root, join(project, 'dir-link')) + await symlink('../target.txt', join(project, 'sub', 'inner-link')) const plan = await planFolderArchive(project, 'p', { excludeDefaultDirs: true }) - expect(plan.totalBytes).toBe(0) - const stream = new FolderArchiveZipStream(plan.entries) - const parts: Buffer[] = [] - for (;;) { - const page = await stream.read(1 << 20) - parts.push(page.bytes) - if (page.eof) { - break - } + const stream = new FolderArchiveZipStream(plan.entries, plan.rootRealPath) + const files = await zipEntries(stream) + expect(Buffer.from(files['p/sub/inner-link']).toString()).toBe('../target.txt') + for (const dropped of ['p/abs-link', 'p/rel-escape', 'p/dir-link', 'p/dir-link/']) { + expect(files[dropped]).toBeUndefined() } - const files = unzipSync(Buffer.concat(parts)) - expect(Buffer.from(files['p/link']).toString()).toBe(outside) expect(Object.keys(files).some((name) => name.includes('outside-secret'))).toBe(false) - expect(Object.keys(files)).not.toContain('p/dir-link/') + expect(stream.skippedCount).toBe(3) + }) + + it('judges link targets against the archive root', () => { + expect(isContainedLinkTarget('p/a/l', '../b')).toBe(true) + expect(isContainedLinkTarget('p/a/l', '../../x')).toBe(false) + expect(isContainedLinkTarget('p/l', '/etc/passwd')).toBe(false) + expect(isContainedLinkTarget('p/l', 'C:x')).toBe(false) + expect(isContainedLinkTarget('p/l', '..\\x')).toBe(false) + }) + + it('skips a file moved out of the folder and linked back in after the walk', async () => { + const project = join(root, 'p') + await mkdir(join(project, 'sub'), { recursive: true }) + await writeFile(join(project, 'sub', 'a.txt'), 'moved-out') + const plan = await planFolderArchive(project, 'p', { excludeDefaultDirs: true }) + // Same dev/ino as the walk saw, but now outside the root. + await rename(join(project, 'sub'), join(root, 'moved')) + await symlink(join(root, 'moved'), join(project, 'sub')) + const stream = new FolderArchiveZipStream(plan.entries, plan.rootRealPath) + const files = await zipEntries(stream) + expect(files['p/sub/a.txt']).toBeUndefined() + expect(stream.skippedCount).toBe(1) + }) + + it('never reads past the walked size of a file that grew', async () => { + await writeFile(join(root, 'log.bin'), Buffer.alloc(100 * 1024, 1)) + const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: true }) + await writeFile(join(root, 'log.bin'), Buffer.alloc(900 * 1024, 1), { flag: 'a' }) + const files = await zipEntries(new FolderArchiveZipStream(plan.entries, plan.rootRealPath)) + expect(files['r/log.bin'].byteLength).toBe(100 * 1024) + }) + + it('gives case- and Unicode-folded twins distinct names', async () => { + await writeFile(join(root, 'Read.md'), 'a') + await writeFile(join(root, 'read.md'), 'b') + const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: true }) + const names = plan.entries.map((entry) => entry.archivePath).filter((p) => p.endsWith('.md')) + // macOS temp dirs fold case themselves; then there is only one file to name. + expect(new Set(names.map((name) => name.toLowerCase())).size).toBe(names.length) }) it('skips a file whose parent directory was swapped for a link after the walk', async () => { @@ -112,7 +159,7 @@ describe('FolderArchiveSessions', () => { // Same name, different file: O_NOFOLLOW alone would follow the swapped parent. await rename(join(project, 'sub'), join(root, 'moved')) await symlink(outside, join(project, 'sub')) - const stream = new FolderArchiveZipStream(plan.entries) + const stream = new FolderArchiveZipStream(plan.entries, plan.rootRealPath) const parts: Buffer[] = [] for (;;) { const page = await stream.read(1 << 20) @@ -129,8 +176,14 @@ describe('FolderArchiveSessions', () => { it('rewrites backslashes and control characters so Windows extractors cannot escape', () => { expect(sanitizeArchiveSegment('..\\..\\evil')).toBe('.._.._evil') expect(sanitizeArchiveSegment('a\u0001b')).toBe('a_b') - expect(sanitizeArchiveSegment('..')).toBe('_') + expect(sanitizeArchiveSegment('..')).toBe('__') + expect(sanitizeArchiveSegment('CON')).toBe('_CON') + expect(sanitizeArchiveSegment('aux.txt')).toBe('_aux.txt') + expect(sanitizeArchiveSegment('a:b*c?.txt')).toBe('a_b_c_.txt') + expect(sanitizeArchiveSegment('notes. ')).toBe('notes__') expect(sanitizeArchiveSegment('normal name.txt')).toBe('normal name.txt') + expect(sanitizeArchiveSegment('invoice\u202Etxt.exe')).toBe('invoicetxt.exe') + expect(sanitizeArchiveSegment('\u200B')).toBe('_') }) it('refuses folders over the byte or entry limits', async () => { @@ -187,10 +240,20 @@ describe('FolderArchiveSessions', () => { ).rejects.toThrow('archive_not_found') }) + it('limits archives per client so one client cannot hold every slot', async () => { + const sessions = new FolderArchiveSessions(60_000, 8, 2) + const start = (ownerKey: string) => + sessions.start({ folderPath: root, ownerKey, excludeDefaultDirs: true }) + // Concurrent starts reserve their slot before walking. + const results = await Promise.allSettled([start('a'), start('a'), start('a')]) + expect(results.filter((result) => result.status === 'fulfilled')).toHaveLength(2) + await expect(start('b')).resolves.toMatchObject({ suggestedName: expect.any(String) }) + }) + it('pauses the producer at the high-water mark until the reader drains', async () => { await writeFile(join(root, 'big.png'), Buffer.alloc(4 * 1024 * 1024, 7)) const plan = await planFolderArchive(root, 'r', { excludeDefaultDirs: true }) - const stream = new FolderArchiveZipStream(plan.entries, 512 * 1024) + const stream = new FolderArchiveZipStream(plan.entries, plan.rootRealPath, 512 * 1024) // The producer fills to the mark, then must stop until something is read. await vi.waitFor(() => expect(stream.processedBytes).toBeGreaterThanOrEqual(512 * 1024)) expect(stream.processedBytes).toBeLessThanOrEqual(1024 * 1024) diff --git a/src/main/runtime/files/archive/folder-archive-sessions.ts b/src/main/runtime/files/archive/folder-archive-sessions.ts index 9649e95e668..4822cc7db0c 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.ts @@ -5,12 +5,15 @@ import type { FileArchiveReadResult, FileArchiveStartResult } from '../../../../shared/rpc-contract/files-archive-params' -import { planFolderArchive } from './folder-archive-walk' +import { planFolderArchive, sanitizeArchiveSegment } from './folder-archive-walk' import { FolderArchiveZipStream } from './folder-archive-zip-stream' // A lost cancel (closed laptop, dropped socket) must not pin host memory or file handles. export const FOLDER_ARCHIVE_IDLE_TIMEOUT_MS = 2 * 60_000 -const MAX_ACTIVE_ARCHIVES = 4 +/** Per client (paired device or connection), so one client can't hold every slot. */ +const MAX_ARCHIVES_PER_OWNER = 2 +/** Host-wide backstop across all clients. */ +const MAX_ACTIVE_ARCHIVES = 8 type ArchiveSession = { id: string @@ -23,12 +26,51 @@ type ArchiveSession = { export class FolderArchiveSessions { private readonly sessions = new Map() + // Slots held by walks still running, so parallel starts can't all walk before the limit applies. + private readonly reservations = new Map() + private reservedTotal = 0 constructor( private readonly idleTimeoutMs = FOLDER_ARCHIVE_IDLE_TIMEOUT_MS, - private readonly maxActive = MAX_ACTIVE_ARCHIVES + private readonly maxActive = MAX_ACTIVE_ARCHIVES, + private readonly maxPerOwner = MAX_ARCHIVES_PER_OWNER ) {} + private ownerCount(ownerKey: string): number { + let count = this.reservations.get(ownerKey) ?? 0 + for (const session of this.sessions.values()) { + if (session.ownerKey === ownerKey) { + count += 1 + } + } + return count + } + + private reserve(ownerKey: string): () => void { + if ( + this.sessions.size + this.reservedTotal >= this.maxActive || + this.ownerCount(ownerKey) >= this.maxPerOwner + ) { + throw new Error('archive_busy') + } + this.reservations.set(ownerKey, (this.reservations.get(ownerKey) ?? 0) + 1) + this.reservedTotal += 1 + let released = false + return () => { + if (released) { + return + } + released = true + this.reservedTotal -= 1 + const left = (this.reservations.get(ownerKey) ?? 1) - 1 + if (left > 0) { + this.reservations.set(ownerKey, left) + } else { + this.reservations.delete(ownerKey) + } + } + } + /** `folderPath` must already be confined to an allowed workspace root by the caller. */ async start(input: { folderPath: string @@ -36,22 +78,23 @@ export class FolderArchiveSessions { excludeDefaultDirs: boolean signal?: AbortSignal }): Promise { - if (this.sessions.size >= this.maxActive) { - throw new Error('archive_busy') - } - const rootName = sanitizeArchiveRootName(basename(await realpath(input.folderPath))) - const plan = await planFolderArchive(input.folderPath, rootName, { - excludeDefaultDirs: input.excludeDefaultDirs, - ...(input.signal ? { signal: input.signal } : {}) - }) - // Re-check after the walk: concurrent starts may have filled the slots meanwhile. - if (this.sessions.size >= this.maxActive) { - throw new Error('archive_busy') + const releaseReservation = this.reserve(input.ownerKey) + let rootName: string + let plan: Awaited> + try { + rootName = sanitizeArchiveRootName(basename(await realpath(input.folderPath))) + plan = await planFolderArchive(input.folderPath, rootName, { + excludeDefaultDirs: input.excludeDefaultDirs, + ...(input.signal ? { signal: input.signal } : {}) + }) + } finally { + // The session below takes over the slot synchronously, so no other start can slip in. + releaseReservation() } const session: ArchiveSession = { id: randomUUID(), ownerKey: input.ownerKey, - stream: new FolderArchiveZipStream(plan.entries), + stream: new FolderArchiveZipStream(plan.entries, plan.rootRealPath), reading: false, idleTimer: null } @@ -149,7 +192,9 @@ function sanitizeArchiveRootName(name: string): string { // A filesystem root has no basename; zip readers choke on separators or control characters here. // oxlint-disable-next-line no-control-regex const cleaned = name.replace(/[\u0000-\u001f/\\]/g, '_').trim() - return cleaned === '' || cleaned === '.' || cleaned === '..' ? 'folder' : cleaned + return cleaned === '' || cleaned === '.' || cleaned === '..' + ? 'folder' + : sanitizeArchiveSegment(cleaned) } const sessionsByRuntime = new WeakMap() diff --git a/src/main/runtime/files/archive/folder-archive-walk.ts b/src/main/runtime/files/archive/folder-archive-walk.ts index 250ae37e912..e276a35dd60 100644 --- a/src/main/runtime/files/archive/folder-archive-walk.ts +++ b/src/main/runtime/files/archive/folder-archive-walk.ts @@ -20,6 +20,8 @@ export type FolderArchiveEntry = { } export type FolderArchivePlan = { + /** Canonical path of the archived folder; every read must land under it. */ + rootRealPath: string entries: FolderArchiveEntry[] fileCount: number directoryCount: number @@ -62,6 +64,7 @@ export async function planFolderArchive( throw new Error('archive_not_directory') } const plan: FolderArchivePlan = { + rootRealPath, entries: [ { archivePath: `${archiveRootName}/`, @@ -80,15 +83,20 @@ export async function planFolderArchive( excludedDirCount: 0 } // Depth-first with an explicit stack: deep trees must not grow the JS call stack. - const pending: { absolutePath: string; archivePath: string }[] = [ - { absolutePath: rootRealPath, archivePath: `${archiveRootName}/` } + const pending: PendingDirectory[] = [ + { + absolutePath: rootRealPath, + archivePath: `${archiveRootName}/`, + dev: rootStat.dev, + ino: rootStat.ino + } ] while (pending.length > 0) { if (options.signal?.aborted) { throw new Error('archive_canceled') } const dir = pending.pop()! - if (dir.absolutePath !== rootRealPath && !(await isUnmovedDirectory(dir.absolutePath))) { + if (!(await isUnmovedDirectory(dir))) { continue } let names: string[] @@ -113,14 +121,22 @@ export async function planFolderArchive( } }) ) - const childDirs: { absolutePath: string; archivePath: string }[] = [] + // A directory swapped for a link while it was listed would have its children lstat'ed outside + // the root; check it again and drop the whole listing if it moved. + if (!(await isUnmovedDirectory(dir))) { + continue + } + const childDirs: PendingDirectory[] = [] + // ZIP extractors on macOS and Windows fold case and Unicode form, so `A` and `a` (or NFC and + // NFD spellings) would land on one path; later twins get a ` (2)` suffix instead. + const usedNames = new Set() for (const item of stats) { if (!item) { continue } const { name, stat } = item const absolutePath = join(dir.absolutePath, name) - const archivePath = `${dir.archivePath}${sanitizeArchiveSegment(name)}` + const archivePath = `${dir.archivePath}${uniqueArchiveSegment(sanitizeArchiveSegment(name), usedNames)}` if (stat.isDirectory()) { if (excluded.has(name)) { plan.excludedDirCount += 1 @@ -137,7 +153,12 @@ export async function planFolderArchive( dev: stat.dev, ino: stat.ino }) - childDirs.push({ absolutePath, archivePath: `${archivePath}/` }) + childDirs.push({ + absolutePath, + archivePath: `${archivePath}/`, + dev: stat.dev, + ino: stat.ino + }) } else if (stat.isFile() || stat.isSymbolicLink()) { const kind = stat.isFile() ? 'file' : 'symlink' if (kind === 'file') { @@ -176,18 +197,53 @@ export async function planFolderArchive( * extractors treat `\` as a separator, so a file named `..\..\x` would escape the extract folder. */ export function sanitizeArchiveSegment(name: string): string { - // oxlint-disable-next-line no-control-regex - const cleaned = name.replace(/[\u0000-\u001f\\]/g, '_') - return cleaned === '.' || cleaned === '..' ? '_' : cleaned + // Bidi overrides and zero-width characters let `exe.txt` display as `txt.exe`; drop them. + const visible = name.replace(INVISIBLE_FORMAT_CHARS, '') + // Windows refuses these characters (`:` can even open an alternate data stream) and drops + // trailing dots and spaces, so map them as local download names are mapped. `.`/`..` become `_`/`__`. + const cleaned = visible + // oxlint-disable-next-line no-control-regex + .replace(/[\u0000-\u001f\u007f\\:*?"<>|]/g, '_') + .replace(/[. ]+$/, (tail) => '_'.repeat(tail.length)) + if (cleaned === '') { + return '_' + } + return WINDOWS_RESERVED_NAME.test(cleaned) ? `_${cleaned}` : cleaned } -async function isUnmovedDirectory(absolutePath: string): Promise { +const WINDOWS_RESERVED_NAME = + /^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9¹²³]|lpt[0-9¹²³])(?:\..*)?$/i + +const INVISIBLE_FORMAT_CHARS = /[\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/g + +function uniqueArchiveSegment(segment: string, used: Set): string { + const fold = (value: string) => value.normalize('NFC').toLowerCase() + let candidate = segment + const dot = segment.lastIndexOf('.') + const stem = dot > 0 ? segment.slice(0, dot) : segment + const extension = dot > 0 ? segment.slice(dot) : '' + for (let copy = 2; used.has(fold(candidate)); copy += 1) { + candidate = `${stem} (${copy})${extension}` + } + used.add(fold(candidate)) + return candidate +} + +type PendingDirectory = { absolutePath: string; archivePath: string; dev: number; ino: number } + +async function isUnmovedDirectory(dir: PendingDirectory): Promise { try { - const resolved = await realpath(absolutePath) + const resolved = await realpath(dir.absolutePath) // Windows paths compare case-insensitively; realpath may report a different casing. - return process.platform === 'win32' - ? resolved.toLowerCase() === absolutePath.toLowerCase() - : resolved === absolutePath + const samePath = + process.platform === 'win32' + ? resolved.toLowerCase() === dir.absolutePath.toLowerCase() + : resolved === dir.absolutePath + if (!samePath) { + return false + } + const current = await lstat(dir.absolutePath) + return current.isDirectory() && current.dev === dir.dev && current.ino === dir.ino } catch (error) { if (isVanishedOrDenied(error)) { return false diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts index f1de2c7c269..339cf7f1f63 100644 --- a/src/main/runtime/files/archive/folder-archive-zip-stream.ts +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -1,5 +1,5 @@ import { readlink, type FileHandle } from 'node:fs/promises' -import { extname } from 'node:path' +import { extname, isAbsolute, posix } from 'node:path' import { Zip, ZipDeflate, ZipPassThrough, type ZipInputFile } from 'fflate' import { isVanishedOrDenied, type FolderArchiveEntry } from './folder-archive-walk' import { @@ -71,6 +71,7 @@ export class FolderArchiveZipStream { constructor( private readonly entries: readonly FolderArchiveEntry[], + private readonly rootRealPath: string, private readonly highWaterBytes = DEFAULT_HIGH_WATER_BYTES ) { this.zip = new Zip((error, data, final) => { @@ -152,7 +153,7 @@ export class FolderArchiveZipStream { ) { const next = this.entries[ahead] if (!prefetched.has(ahead) && isSmallFile(next)) { - prefetched.set(ahead, readSmallFile(next)) + prefetched.set(ahead, readSmallFile(next, this.rootRealPath)) } } const small = prefetched.get(index) @@ -179,7 +180,12 @@ export class FolderArchiveZipStream { } catch (error) { return this.skip(error) } - // Stored as a link, never dereferenced: the target may name anything on the host. + // Stored as a link, never dereferenced. A link that points outside the archived folder would + // let an extractor write through it (zip-slip), so only links that stay inside are kept. + if (!isContainedLinkTarget(entry.archivePath, target)) { + this.skippedCount += 1 + return + } const file = withAttributes( new ZipPassThrough(entry.archivePath), entry, @@ -204,23 +210,30 @@ export class FolderArchiveZipStream { } let handle: FileHandle try { - handle = await openUnchangedFile(entry) + handle = await openUnchangedFile(entry, this.rootRealPath) } catch (error) { return this.skip(error) } try { const file = this.addFileEntry(entry) const buffer = Buffer.allocUnsafe(FILE_READ_BYTES) + // Never past the walked size: a file that keeps growing (a log, /dev/zero behind a bind + // mount) cannot push the archive past the limits the walk enforced. + let remaining = entry.size for (;;) { await this.waitForRoom() if (this.canceled) { return } - const { bytesRead } = await handle.read(buffer, 0, FILE_READ_BYTES, null) + const { bytesRead } = + remaining > 0 + ? await handle.read(buffer, 0, Math.min(FILE_READ_BYTES, remaining), null) + : { bytesRead: 0 } if (bytesRead === 0) { file.push(new Uint8Array(0), true) return } + remaining -= bytesRead this.processedBytes += bytesRead // fflate may keep a reference until it deflates, so hand it a copy. file.push(new Uint8Array(buffer.subarray(0, bytesRead)), false) @@ -275,6 +288,16 @@ export class FolderArchiveZipStream { } } +/** True when a link stored at `archivePath` resolves inside the archive's top folder. */ +export function isContainedLinkTarget(archivePath: string, target: string): boolean { + if (target === '' || isAbsolute(target) || target.includes('\\') || /^[a-zA-Z]:/.test(target)) { + return false + } + const rootName = archivePath.slice(0, archivePath.indexOf('/')) + const resolved = posix.normalize(posix.join(posix.dirname(archivePath), target)) + return resolved === rootName || resolved.startsWith(`${rootName}/`) +} + function withAttributes( file: T, entry: FolderArchiveEntry, diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx index 6905d35f661..11f43696e0e 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/RuntimeDownloads.fixture.tsx @@ -52,7 +52,7 @@ function menuFixture(kind: 'file' | 'folder') { ) : null} - {kind === 'file' ? 'Download' : 'Download as ZIP…'} + {kind === 'file' ? 'Download…' : 'Download as ZIP…'} {kind === 'file' ? ( @@ -95,7 +95,8 @@ export const fileProgress = toastFixture({ receivedBytes: 87 * MB, totalBytes: 200 * MB, processedBytes: 87 * MB, - bytesPerSecond: 14.2 * MB + bytesPerSecond: 11.5 * MB, + rateWindowMs: 7_500 } }) export const folderCollecting = toastFixture({ @@ -117,7 +118,8 @@ export const folderProgress = toastFixture({ totalBytes: 2.4 * MB, processedBytes: 1.6 * MB, fileCount: 5000, - bytesPerSecond: 1.2 * MB + bytesPerSecond: 1.2 * MB, + rateWindowMs: 1_200 } }) export const openProgress = toastFixture({ @@ -131,7 +133,8 @@ export const openProgress = toastFixture({ receivedBytes: 0.6 * MB, totalBytes: 1.9 * MB, processedBytes: 0.6 * MB, - bytesPerSecond: 5.4 * MB + bytesPerSecond: 5.4 * MB, + rateWindowMs: 400 } }) export const fileDone = toastFixture({ @@ -139,14 +142,14 @@ export const fileDone = toastFixture({ itemKind: 'file', name: 'random-200mb.bin', bytes: 200 * MB, - elapsedMs: 14_300 + elapsedMs: 17_400 }) export const folderDone = toastFixture({ kind: 'done', itemKind: 'folder', name: 'smallfiles.zip', - bytes: 0.7 * MB, - elapsedMs: 2_400, + bytes: 0.86 * MB, + elapsedMs: 2_900, fileCount: 5000, excludedDefaultDirs: true }) @@ -200,3 +203,50 @@ export const errorNotFound = toastFixture({ place: 'sandbox' }) export const errorNoApp = toastFixture({ kind: 'no_app', name: 'trace.perfetto' }) +export const errorOpenRefused = toastFixture({ + kind: 'failed', + intent: 'open', + itemKind: 'file', + name: 'install.sh', + code: 'open_refused', + place: 'sandbox' +}) +export const errorFileGrew = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'file', + name: 'server.log', + code: 'too_large', + place: 'sandbox' +}) +export const errorTooManyItems = toastFixture({ + kind: 'failed', + intent: 'save', + itemKind: 'folder', + name: 'node_cache', + code: 'too_many_entries', + place: 'sandbox' +}) +const LONG_NAME = 'a-very-long-generated-artifact-name-2026-11-09-build-output.tar.gz' +export const longNameProgress = toastFixture({ + kind: 'progress', + intent: 'save', + itemKind: 'file', + name: LONG_NAME, + snapshot: { + downloadId: 'l', + phase: 'transferring', + receivedBytes: 412 * MB, + totalBytes: 980 * MB, + processedBytes: 412 * MB, + bytesPerSecond: 12.1 * MB, + rateWindowMs: 30_000 + } +}) +export const longNameDone = toastFixture({ + kind: 'done', + itemKind: 'file', + name: LONG_NAME, + bytes: 980 * MB, + elapsedMs: 83_000 +}) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts index 17776c71b1f..f077b69bff4 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-action-visibility.ts @@ -1,5 +1,6 @@ import type { RuntimeFileOperationArgs } from '@/runtime/files/runtime-file-client-types' import type { TreeNode } from '../tree/file-explorer-types' +import { canOpenRuntimeDownloadLocally } from '../../../../../../shared/runtime-downloads/runtime-download-contract' export function shouldShowCollapseFolderAction(node: TreeNode, isExpanded: boolean): boolean { return node.isDirectory && isExpanded @@ -36,13 +37,21 @@ export function shouldShowRemoteDownloadAction( ) } -/** Fetch a runtime-hosted file into a temp copy and open it with the local default app. */ +/** + * Fetch a runtime-hosted file into a temp copy and open it with the local default app. Hidden for + * files the OS would run (scripts, installers, apps): those can only be downloaded. + */ export function shouldShowOpenLocallyAction( node: TreeNode, connectionId: string | null | undefined, canStreamFromRuntime: boolean ): boolean { - return !node.isDirectory && !connectionId && canStreamFromRuntime + return ( + !node.isDirectory && + !connectionId && + canStreamFromRuntime && + canOpenRuntimeDownloadLocally(node.name) + ) } export function shouldShowCopyFileAction( diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx index 9cb77482fc2..33173a81421 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/file-explorer-row-context-menu.tsx @@ -263,7 +263,10 @@ export function FileExplorerRowContextMenu({ 'Download as ZIP…' ) : t('auto.components.right.sidebar.FileExplorerRow.7ac885bd2f', 'Download Folder') - : t('auto.components.right.sidebar.FileExplorerRow.c2112579f6', 'Download')} + : canStreamFromRuntime + ? // Opens a save dialog, like "Download as ZIP…". + t('auto.components.right.sidebar.FileExplorerRow.downloadFileEllipsis', 'Download…') + : t('auto.components.right.sidebar.FileExplorerRow.c2112579f6', 'Download')} )} {showOpenLocallyAction && ( diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.test.ts b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.test.ts new file mode 100644 index 00000000000..155c7283fbe --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.test.ts @@ -0,0 +1,135 @@ +// @vitest-environment happy-dom +import { afterEach, describe, expect, it, vi } from 'vitest' +import type { RuntimeDownloadProgressEvent } from '../../../../../../shared/runtime-downloads/runtime-download-contract' +import type { TreeNode } from '../tree/file-explorer-types' +import { + applyRuntimeDownloadProgress, + getRuntimeDownloadProgress, + subscribeRuntimeDownloadProgress, + trackRuntimeDownload +} from '@/runtime/files/downloads/runtime-download-progress-store' +import { + shouldShowOpenLocallyAction, + shouldShowRemoteDownloadAction +} from './file-explorer-row-action-visibility' + +vi.mock('@/lib/cloud-remote-desktop/cloud-environment-ids', () => ({ + isKnownCloudEnvironment: (id: string) => id === 'cloud-env', + refreshCloudEnvironmentIds: async () => {} +})) + +const { outcomeState } = await import('./runtime-download-actions') + +function node(name: string, isDirectory = false): TreeNode { + return { name, path: `/w/${name}`, relativePath: name, isDirectory, depth: 0 } +} + +const base = { itemKind: 'file' as const, environmentId: 'cloud-env', elapsedMs: 1_000 } + +describe('outcomeState', () => { + it('leaves nothing behind for a canceled download or a file that opened', () => { + expect(outcomeState({ status: 'canceled' }, { ...base, node: node('a'), intent: 'save' })).toBe( + null + ) + expect( + outcomeState( + { status: 'done', destinationPath: '/tmp/a', bytes: 1 }, + { ...base, node: node('a'), intent: 'open' } + ) + ).toBe(null) + }) + + it('reports a saved copy no app could open as a soft no-app state', () => { + expect( + outcomeState( + { status: 'done', destinationPath: '/tmp/a.xyz', bytes: 1, openError: 'no app' }, + { ...base, node: node('a.xyz'), intent: 'open' } + ) + ).toEqual({ kind: 'no_app', name: 'a.xyz' }) + }) + + it('names the sandbox only for cloud workspaces', () => { + const error = { status: 'error', code: 'not_found', message: 'x' } as const + expect(outcomeState(error, { ...base, node: node('a'), intent: 'save' })).toMatchObject({ + kind: 'failed', + place: 'sandbox' + }) + expect( + outcomeState(error, { ...base, environmentId: 'ssh-box', node: node('a'), intent: 'save' }) + ).toMatchObject({ kind: 'failed', place: 'host' }) + }) + + it('never offers Open on the done card for files the OS would run', () => { + vi.stubGlobal('api', { platform: { get: () => ({ platform: 'darwin' }) } }) + const done = { status: 'done', destinationPath: '/d/x', bytes: 3 } as const + expect(outcomeState(done, { ...base, node: node('setup.sh'), intent: 'save' })).toMatchObject({ + kind: 'done', + canOpen: false + }) + expect(outcomeState(done, { ...base, node: node('report.pdf'), intent: 'save' })).toMatchObject( + { kind: 'done', canOpen: true } + ) + }) +}) + +describe('row action visibility', () => { + it('offers folder downloads on runtime hosts without an SSH connection', () => { + expect(shouldShowRemoteDownloadAction(node('src', true), null, null, false, true)).toBe(true) + expect(shouldShowRemoteDownloadAction(node('src', true), null, null, false, false)).toBe(false) + }) + + it('hides Open on This Computer for folders, SSH and runnable files', () => { + expect(shouldShowOpenLocallyAction(node('a.pdf'), null, true)).toBe(true) + expect(shouldShowOpenLocallyAction(node('src', true), null, true)).toBe(false) + expect(shouldShowOpenLocallyAction(node('a.pdf'), 'ssh-1', true)).toBe(false) + for (const name of ['run.sh', 'Setup.EXE', 'x.command', 'evil.bat. ']) { + expect(shouldShowOpenLocallyAction(node(name), null, true)).toBe(false) + } + }) +}) + +describe('runtime download progress store', () => { + const unsubscribe = vi.fn() + afterEach(() => { + unsubscribe.mockReset() + }) + + function event(receivedBytes: number): RuntimeDownloadProgressEvent { + return { + downloadId: 'd1', + phase: 'transferring', + receivedBytes, + totalBytes: 100, + processedBytes: receivedBytes + } + } + + it('smooths the rate, tracks its window and drops the IPC listener on release', () => { + const onProgress = vi.fn(() => unsubscribe) + vi.stubGlobal('api', { runtimeDownloads: { onProgress } }) + const release = trackRuntimeDownload('d1') + expect(onProgress).toHaveBeenCalledOnce() + applyRuntimeDownloadProgress(event(0), 0) + applyRuntimeDownloadProgress(event(10), 1_000) + applyRuntimeDownloadProgress(event(30), 2_000) + const snapshot = getRuntimeDownloadProgress('d1') + // 10 B/s, then 20 B/s smoothed by 0.3. + expect(snapshot?.bytesPerSecond).toBeCloseTo(13) + expect(snapshot?.rateWindowMs).toBe(2_000) + release() + expect(unsubscribe).toHaveBeenCalledOnce() + expect(getRuntimeDownloadProgress('d1')).toBe(null) + }) + + it('does not bring back a released download when a late toast subscribes', () => { + const onProgress = vi.fn(() => unsubscribe) + vi.stubGlobal('api', { runtimeDownloads: { onProgress } }) + const release = trackRuntimeDownload('d2') + release() + subscribeRuntimeDownloadProgress('d2', () => {})() + // A new download must start a fresh IPC listener: the old entry did not linger. + trackRuntimeDownload('d3')() + expect(onProgress).toHaveBeenCalledTimes(2) + expect(unsubscribe).toHaveBeenCalledTimes(2) + }) +}) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx index 9493927f897..e7b266395ec 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-actions.tsx @@ -1,4 +1,4 @@ -import { useSyncExternalStore } from 'react' +import { useCallback, useSyncExternalStore } from 'react' import { toast } from 'sonner' import { createNonSecureContextUuid } from '../../../../../../shared/non-secure-context-uuid' import { getRevealInFileManagerLabel, revealInFileManager } from '@/lib/reveal-in-file-manager' @@ -15,7 +15,11 @@ import { trackRuntimeDownload } from '@/runtime/files/downloads/runtime-download-progress-store' import { LOCAL_EXECUTION_HOST_ID } from '../../../../../../shared/execution-host' -import type { RuntimeDownloadResult } from '../../../../../../shared/runtime-downloads/runtime-download-contract' +import { + canOpenRuntimeDownloadLocally, + type RuntimeDownloadResult +} from '../../../../../../shared/runtime-downloads/runtime-download-contract' +import { requestFileExplorerRefresh } from '../watching/file-explorer-refresh-requests' import type { TreeNode } from '../tree/file-explorer-types' import { RuntimeDownloadToast, @@ -27,8 +31,13 @@ import { export type RuntimeDownloadIntent = RuntimeDownloadToastIntent // Long enough to reach "Reveal in Finder" after a long download, short enough not to pile up. +// Errors stay until dismissed: their one next action must not time out. const DONE_TOAST_MS = 12_000 +// One transfer per path and intent: "Open on This Computer" has no dialog, so a double click or a +// retry while the first copy still runs would otherwise fetch and open it twice. +const inFlight = new Set() + /** Whether this desktop can stream the node straight from its runtime host. */ export function canStreamRuntimeDownload( node: TreeNode, @@ -54,10 +63,13 @@ function LiveProgressToast({ state: Omit, 'snapshot'> onAction: (action: RuntimeDownloadToastAction) => void }): React.JSX.Element { - const snapshot = useSyncExternalStore( - (listener) => subscribeRuntimeDownloadProgress(downloadId, listener), - () => getRuntimeDownloadProgress(downloadId) + // Stable identities: an inline subscribe would resubscribe on every progress tick. + const subscribe = useCallback( + (listener: () => void) => subscribeRuntimeDownloadProgress(downloadId, listener), + [downloadId] ) + const getSnapshot = useCallback(() => getRuntimeDownloadProgress(downloadId), [downloadId]) + const snapshot = useSyncExternalStore(subscribe, getSnapshot) return ( > +): Promise { + const api = window.api.runtimeDownloads + if (!api) { + return + } + const environmentId = remote.target.environmentId // Error copy says "sandbox" for cloud workspaces; the list is usually primed already. void refreshCloudEnvironmentIds() const itemKind = node.isDirectory ? 'folder' : 'file' @@ -114,6 +150,8 @@ export async function startRuntimeDownload( { id: toastId, duration: Number.POSITIVE_INFINITY, dismissible: false } ) } + // `preparing` is always the first event and is sent once the save dialog has closed, so the + // elapsed time never includes the dialog, even for a file too small to send any progress. const stopProgressWatch = api.onProgress((event) => { if (event.downloadId === downloadId) { stopProgressWatch() @@ -158,6 +196,8 @@ export async function startRuntimeDownload( void startRuntimeDownload(node, context, intent, options) } else if (action === 'download_instead') { void startRuntimeDownload(node, context, 'save', options) + } else if (action === 'refresh') { + requestFileExplorerRefresh() } } toast.custom( @@ -169,7 +209,11 @@ export async function startRuntimeDownload( onDismiss={() => toast.dismiss(toastId)} /> ), - { id: toastId, duration: DONE_TOAST_MS, dismissible: true } + { + id: toastId, + duration: outcome.kind === 'done' ? DONE_TOAST_MS : Number.POSITIVE_INFINITY, + dismissible: true + } ) } @@ -209,6 +253,7 @@ export function outcomeState( elapsedMs: input.elapsedMs, ...(result.fileCount !== undefined ? { fileCount: result.fileCount } : {}), excludedDefaultDirs: (result.excludedDirCount ?? 0) > 0, + canOpen: canOpenRuntimeDownloadLocally(node.name), ...(result.skippedCount ? { skippedCount: result.skippedCount } : {}) } } diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-failure-copy.ts b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-failure-copy.ts new file mode 100644 index 00000000000..d62788c7caa --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-failure-copy.ts @@ -0,0 +1,183 @@ +import type { Translate } from '@/i18n/use-translate' +import type { RuntimeDownloadErrorCode } from '../../../../../../shared/runtime-downloads/runtime-download-contract' + +export type RuntimeDownloadFailureAction = 'retry' | 'download_instead' | 'refresh' | 'reveal' + +export type RuntimeDownloadFailureCopy = { + title: string + detail: string + /** 'neutral' for limits and deliberate refusals; 'error' when something actually broke. */ + tone: 'error' | 'neutral' + action: RuntimeDownloadFailureAction | null +} + +/** Cloud workspaces live in a sandbox; other runtime hosts are named generically. */ +export type RuntimeDownloadPlace = 'sandbox' | 'host' + +/** Glossary error tone: what happened, whether anything was lost, at most one next action. */ +export function runtimeDownloadFailureCopy( + t: Translate, + state: { + intent: 'save' | 'open' + itemKind: 'file' | 'folder' + name: string + code: RuntimeDownloadErrorCode + place: RuntimeDownloadPlace + } +): RuntimeDownloadFailureCopy { + const { name, place } = state + const title = + state.intent === 'open' + ? t('components.runtimeDownload.openFailedTitle', 'Couldn’t open {{name}}', { name }) + : t('components.runtimeDownload.failedTitle', 'Couldn’t download {{name}}', { name }) + const transferFailed: RuntimeDownloadFailureCopy = { + title, + detail: t( + 'components.runtimeDownload.transferFailed', + 'Something went wrong while copying it. Nothing was saved on this computer.' + ), + tone: 'error', + action: 'retry' + } + switch (state.code) { + case 'too_large': + if (state.itemKind === 'folder') { + return { + title, + detail: t( + 'components.runtimeDownload.folderTooLarge', + 'This folder is over 2 GB, the most Orca can download at once. Download the folders inside it one at a time. Nothing was saved on this computer.' + ), + tone: 'neutral', + action: null + } + } + return state.intent === 'open' + ? { + title, + detail: t( + 'components.runtimeDownload.tooLargeToOpen', + 'Files over 1 GB can’t be opened directly. Download it instead.' + ), + tone: 'neutral', + action: 'download_instead' + } + : { + title, + detail: t( + 'components.runtimeDownload.fileGrew', + 'The file grew while Orca was copying it. Nothing was saved on this computer.' + ), + tone: 'error', + action: 'retry' + } + case 'too_many_entries': + return { + title, + detail: t( + 'components.runtimeDownload.folderTooManyItems', + 'This folder has more than 65,000 items, the most Orca can download at once. Download the folders inside it one at a time. Nothing was saved on this computer.' + ), + tone: 'neutral', + action: null + } + case 'open_refused': + return { + title, + detail: t( + 'components.runtimeDownload.openRefused', + 'Orca doesn’t open files that can run programs, like scripts, installers and apps. Download it to check it first. Nothing was saved on this computer.' + ), + tone: 'neutral', + action: 'download_instead' + } + case 'host_update_required': + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.sandboxUpdateRequired', + 'This sandbox runs an older version of Orca. New cloud workspaces include downloads. Your files are safe.' + ) + : t( + 'components.runtimeDownload.hostUpdateRequired', + 'The remote host runs an older version of Orca. Update Orca there, then try again.' + ), + tone: 'error', + action: null + } + case 'host_busy': + return { + title, + detail: t( + 'components.runtimeDownload.busy', + 'Too many downloads are running. Wait for one to finish, then try again.' + ), + tone: 'error', + action: 'retry' + } + case 'not_found': + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.notFoundInSandboxNothingSaved', + 'It was moved or deleted in the sandbox. Nothing was saved on this computer.' + ) + : t( + 'components.runtimeDownload.notFoundOnHostNothingSaved', + 'It was moved or deleted on the remote host. Nothing was saved on this computer.' + ), + tone: 'error', + action: 'refresh' + } + case 'not_a_file': + case 'not_a_directory': + return { + title, + detail: t( + 'components.runtimeDownload.changed', + 'It changed while Orca was reading it. Try again.' + ), + tone: 'error', + action: 'retry' + } + case 'disconnected': + return { + title, + detail: + place === 'sandbox' + ? t( + 'components.runtimeDownload.sandboxDisconnected', + 'The connection to the sandbox dropped. Nothing was saved on this computer.' + ) + : t( + 'components.runtimeDownload.hostDisconnected', + 'The connection to the remote host dropped. Nothing was saved on this computer.' + ), + tone: 'error', + action: 'retry' + } + case 'canceled': + // Unreachable: outcomeState drops canceled results. Never offer "Try again" for a cancel. + return { ...transferFailed, action: null } + case 'open_failed': + case 'transfer_failed': + return transferFailed + } +} + +/** The file was saved, but nothing on this computer opens its type: a soft state, not a failure. */ +export function runtimeDownloadNoAppCopy(t: Translate, name: string): RuntimeDownloadFailureCopy { + return { + title: t('components.runtimeDownload.savedName', 'Saved {{name}}', { name }), + detail: t( + 'components.runtimeDownload.noAppForType', + 'No app on this computer opens this file type. Reveal it to choose one.' + ), + tone: 'neutral', + action: 'reveal' + } +} diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx index c0dec77889a..b2b197553ca 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx @@ -1,5 +1,5 @@ import type React from 'react' -import { CircleCheck, Download, ExternalLink, FolderOpen, RotateCw, X } from 'lucide-react' +import { CircleCheck, Download, ExternalLink, FolderOpen, MonitorDown, X } from 'lucide-react' import { Button } from '@/components/ui/button' import { InlineError } from '@/components/ui/inline-error' import { Progress } from '@/components/ui/progress' @@ -8,11 +8,16 @@ import { formatElapsedTime } from '@/components/worktree-creation/worktree-creat import { formatBytes } from '../../../../../../shared/format-bytes' import type { RuntimeDownloadErrorCode } from '../../../../../../shared/runtime-downloads/runtime-download-contract' import type { RuntimeDownloadProgressSnapshot } from '@/runtime/files/downloads/runtime-download-progress-store' +import { + runtimeDownloadFailureCopy, + runtimeDownloadNoAppCopy, + type RuntimeDownloadFailureAction, + type RuntimeDownloadPlace +} from './runtime-download-failure-copy' +export type { RuntimeDownloadPlace } export type RuntimeDownloadItemKind = 'file' | 'folder' export type RuntimeDownloadToastIntent = 'save' | 'open' -/** Cloud workspaces live in a sandbox; other runtime hosts are named generically. */ -export type RuntimeDownloadPlace = 'sandbox' | 'host' export type RuntimeDownloadToastState = | { @@ -33,6 +38,8 @@ export type RuntimeDownloadToastState = fileCount?: number excludedDefaultDirs?: boolean skippedCount?: number + /** False for files the OS would run: those are only revealed, never opened from the toast. */ + canOpen?: boolean } | { kind: 'failed' @@ -44,10 +51,12 @@ export type RuntimeDownloadToastState = } | { kind: 'no_app'; name: string } -export type RuntimeDownloadToastAction = 'cancel' | 'reveal' | 'open' | 'retry' | 'download_instead' +export type RuntimeDownloadToastAction = 'cancel' | 'open' | RuntimeDownloadFailureAction const countFormat = new Intl.NumberFormat() const formatCount = (value: number): string => countFormat.format(value) +// A time-left estimate from the first second of a transfer swings wildly; wait for the rate to settle. +const ETA_MIN_RATE_WINDOW_MS = 2_000 const CARD = 'relative w-[356px] rounded-lg border border-border bg-popover px-3 py-2.5 text-sm text-popover-foreground shadow-lg' @@ -73,19 +82,14 @@ function progressDetail( : t('components.runtimeDownload.starting', 'Starting…') } if (itemKind === 'folder') { - const amount = snapshot.totalBytes - ? t('components.runtimeDownload.amountOfTotal', '{{done}} of {{total}}', { - done: formatBytes(snapshot.processedBytes), - total: formatBytes(snapshot.totalBytes) - }) - : formatBytes(snapshot.receivedBytes) + // Counted in files, not bytes: the source size and the ZIP size differ, and the done card + // reports the ZIP's size. return snapshot.fileCount === undefined - ? amount - : t('components.runtimeDownload.folderProgress', '{{files}} files · {{amount}}', { + ? formatBytes(snapshot.receivedBytes) + : t('components.runtimeDownload.folderFilesProgress', '{{files}} files', { count: snapshot.fileCount, files: formatCount(snapshot.fileCount), - defaultValue_one: '{{files}} file · {{amount}}', - amount + defaultValue_one: '{{files}} file' }) } const amount = snapshot.totalBytes @@ -94,127 +98,22 @@ function progressDetail( total: formatBytes(snapshot.totalBytes) }) : formatBytes(snapshot.receivedBytes) - return snapshot.bytesPerSecond && snapshot.bytesPerSecond > 0 - ? t('components.runtimeDownload.amountAndRate', '{{amount}} · {{rate}}/s', { - amount, - rate: formatBytes(snapshot.bytesPerSecond) - }) - : amount -} - -function failureCopy( - t: Translate, - state: Extract -): { title: string; detail: string; action: 'retry' | 'download_instead' | null } { - const { name, place } = state - const title = - state.intent === 'open' - ? t('components.runtimeDownload.openFailedTitle', 'Couldn’t open {{name}}', { name }) - : t('components.runtimeDownload.failedTitle', 'Couldn’t download {{name}}', { name }) - switch (state.code) { - case 'too_large': - return state.intent === 'open' - ? { - title, - detail: t( - 'components.runtimeDownload.tooLargeToOpen', - 'Files over 1 GB can’t be opened directly. Download it instead.' - ), - action: 'download_instead' - } - : { - title, - detail: t( - 'components.runtimeDownload.tooLarge', - 'Folders over 2 GB can’t be downloaded at once. Download the folders inside it instead.' - ), - action: null - } - case 'too_many_entries': - return { - title, - detail: t( - 'components.runtimeDownload.tooManyEntries', - 'Folders with more than 65,000 items can’t be downloaded at once. Download the folders inside it instead.' - ), - action: null - } - case 'host_update_required': - return { - title, - detail: - place === 'sandbox' - ? t( - 'components.runtimeDownload.sandboxUpdateRequired', - 'This sandbox runs an older version of Orca. New cloud workspaces include downloads. Your files are safe.' - ) - : t( - 'components.runtimeDownload.hostUpdateRequired', - 'The remote host runs an older version of Orca. Update Orca there, then try again.' - ), - action: null - } - case 'host_busy': - return { - title, - detail: t( - 'components.runtimeDownload.busy', - 'Too many downloads are running. Wait for one to finish, then try again.' - ), - action: 'retry' - } - case 'not_found': - return { - title, - detail: - place === 'sandbox' - ? t( - 'components.runtimeDownload.notFoundInSandbox', - 'It was moved or deleted in the sandbox. Refresh the file explorer to see the latest files.' - ) - : t( - 'components.runtimeDownload.notFoundOnHost', - 'It was moved or deleted on the remote host. Refresh the file explorer to see the latest files.' - ), - action: null - } - case 'not_a_file': - case 'not_a_directory': - return { - title, - detail: t( - 'components.runtimeDownload.changed', - 'It changed while Orca was reading it. Try again.' - ), - action: 'retry' - } - case 'disconnected': - return { - title, - detail: - place === 'sandbox' - ? t( - 'components.runtimeDownload.sandboxDisconnected', - 'The connection to the sandbox dropped. Nothing was saved on this computer.' - ) - : t( - 'components.runtimeDownload.hostDisconnected', - 'The connection to the remote host dropped. Nothing was saved on this computer.' - ), - action: 'retry' - } - case 'open_failed': - case 'transfer_failed': - case 'canceled': - return { - title, - detail: t( - 'components.runtimeDownload.transferFailed', - 'Something went wrong while copying it. Nothing was saved on this computer.' - ), - action: 'retry' - } + const rate = snapshot.bytesPerSecond + if (!rate || rate <= 0) { + return amount } + if (snapshot.totalBytes && snapshot.rateWindowMs >= ETA_MIN_RATE_WINDOW_MS) { + const leftMs = (Math.max(0, snapshot.totalBytes - snapshot.receivedBytes) / rate) * 1000 + return t('components.runtimeDownload.amountRateEta', '{{amount}} · {{rate}}/s · {{eta}} left', { + amount, + rate: formatBytes(rate), + eta: formatElapsedTime(leftMs) + }) + } + return t('components.runtimeDownload.amountAndRate', '{{amount}} · {{rate}}/s', { + amount, + rate: formatBytes(rate) + }) } function DismissButton({ onDismiss }: { onDismiss: () => void }): React.JSX.Element { @@ -233,6 +132,158 @@ function DismissButton({ onDismiss }: { onDismiss: () => void }): React.JSX.Elem ) } +function ProgressCard({ + state, + onAction +}: { + state: Extract + onAction: (action: RuntimeDownloadToastAction) => void +}): React.JSX.Element { + const t = useTranslate() + const fraction = runtimeDownloadFraction(state.snapshot) + const percent = fraction === null ? null : Math.round(fraction * 100) + const detail = progressDetail(t, state.itemKind, state.snapshot) + const title = + state.intent === 'open' + ? t('components.runtimeDownload.opening', 'Opening {{name}}', { name: state.name }) + : t('components.runtimeDownload.downloading', 'Downloading {{name}}', { name: state.name }) + const progressLabel = t('components.runtimeDownload.progressLabel', 'Download progress') + const Icon = state.intent === 'open' ? MonitorDown : Download + return ( +

+
+ + {/* Only the title is live: the numbers change several times a second and would chatter. + The progressbar's value text carries them when asked. */} +

+ {title} +

+ +
+
+ {percent === null ? ( + // Unknown size: an honest sweep beats a faked percentage. + + + + ) : ( + + )} +

{detail}

+
+
+ ) +} + +function DoneCard({ + state, + revealLabel, + onAction, + onDismiss +}: { + state: Extract + revealLabel: string + onAction: (action: RuntimeDownloadToastAction) => void + onDismiss: () => void +}): React.JSX.Element { + const t = useTranslate() + const isFolder = state.itemKind === 'folder' + const size = isFolder + ? t('components.runtimeDownload.zipSize', '{{size}} zip', { size: formatBytes(state.bytes) }) + : formatBytes(state.bytes) + const parts = + isFolder && state.fileCount !== undefined + ? [ + t('components.runtimeDownload.fileCount', '{{files}} files', { + count: state.fileCount, + files: formatCount(state.fileCount), + defaultValue_one: '{{files}} file' + }), + size + ] + : [size] + parts.push(formatElapsedTime(state.elapsedMs)) + const notes: string[] = [] + if (state.excludedDefaultDirs) { + notes.push( + t( + 'components.runtimeDownload.defaultDirsNotIncluded', + 'node_modules and .git weren’t included.' + ) + ) + } + if ((state.skippedCount ?? 0) > 0) { + notes.push( + t( + 'components.runtimeDownload.skipped', + '{{count}} items couldn’t be read and were left out.', + { + count: state.skippedCount, + defaultValue_one: '{{count}} item couldn’t be read and was left out.' + } + ) + ) + } + return ( +
+
+ +
+

+ {t('components.runtimeDownload.downloaded', 'Downloaded {{name}}', { + name: state.name + })} +

+

{parts.join(' · ')}

+ {notes.map((note) => ( +

+ {note} +

+ ))} +
+ + {state.itemKind === 'file' && state.canOpen !== false ? ( + + ) : null} +
+
+
+ +
+ ) +} + +function actionLabel(t: Translate, action: RuntimeDownloadFailureAction, revealLabel: string) { + switch (action) { + case 'retry': + return t('components.runtimeDownload.retry', 'Try again') + case 'download_instead': + return t('components.runtimeDownload.downloadInstead', 'Download') + case 'refresh': + return t('components.runtimeDownload.refresh', 'Refresh') + case 'reveal': + return revealLabel + } +} + /** * Toast for a download from a runtime host: live progress with Cancel, a done card whose actions * open the result on this computer, or one error with at most one next action. @@ -251,157 +302,33 @@ export function RuntimeDownloadToast({ }): React.JSX.Element { const t = useTranslate() if (state.kind === 'progress') { - const fraction = runtimeDownloadFraction(state.snapshot) - const percent = fraction === null ? null : Math.round(fraction * 100) - const detail = progressDetail(t, state.itemKind, state.snapshot) - const title = - state.intent === 'open' - ? t('components.runtimeDownload.opening', 'Opening {{name}}', { name: state.name }) - : t('components.runtimeDownload.downloading', 'Downloading {{name}}', { name: state.name }) - const progressLabel = t('components.runtimeDownload.progressLabel', 'Download progress') - return ( -
-
- -

- {title} -

- -
-
- {percent === null ? ( - // Unknown size: an honest sweep beats a faked percentage. - - - - ) : ( - - )} -

{detail}

-
-
- ) + return } if (state.kind === 'done') { - const parts = - state.itemKind === 'folder' && state.fileCount !== undefined - ? [ - t('components.runtimeDownload.fileCount', '{{files}} files', { - count: state.fileCount, - files: formatCount(state.fileCount), - defaultValue_one: '{{files}} file' - }), - formatBytes(state.bytes) - ] - : [formatBytes(state.bytes)] - parts.push(formatElapsedTime(state.elapsedMs)) - const notes: string[] = [] - if (state.excludedDefaultDirs) { - notes.push( - t('components.runtimeDownload.leftOutDefaultDirs', 'Left out node_modules and .git.') - ) - } - if ((state.skippedCount ?? 0) > 0) { - notes.push( - t( - 'components.runtimeDownload.skipped', - '{{count}} items couldn’t be read and were left out.', - { - count: state.skippedCount, - defaultValue_one: '{{count}} item couldn’t be read and was left out.' - } - ) - ) - } return ( -
-
- -
-

- {t('components.runtimeDownload.downloaded', 'Downloaded {{name}}', { - name: state.name - })} -

-

{parts.join(' · ')}

- {notes.map((note) => ( -

- {note} -

- ))} -
- - {state.itemKind === 'file' ? ( - - ) : null} -
-
-
- -
+ ) } const copy = state.kind === 'no_app' - ? { - title: t( - 'components.runtimeDownload.noAppTitle', - 'No app on this computer opens {{name}}', - { - name: state.name - } - ), - detail: t( - 'components.runtimeDownload.noAppDetail', - 'Orca saved a copy. Reveal it to choose an app.' - ), - action: 'reveal' as const - } - : failureCopy(t, state) - const actionLabel = - copy.action === 'retry' - ? t('components.runtimeDownload.retry', 'Try again') - : copy.action === 'download_instead' - ? t('components.runtimeDownload.downloadInstead', 'Download') - : revealLabel + ? runtimeDownloadNoAppCopy(t, state.name) + : runtimeDownloadFailureCopy(t, state) + const action = copy.action return ( - // The action sits under the text inside the error box: a 356px toast has no room beside it.
onAction(action)}> + {actionLabel(t, action, revealLabel)} + + ) : undefined + } /> - {copy.action ? ( - - ) : null}
) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/useFileExplorerManualRefresh.ts b/src/renderer/src/components/right-sidebar/file-explorer/panel/useFileExplorerManualRefresh.ts index 7ff95062fdc..093ae92945a 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/useFileExplorerManualRefresh.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/useFileExplorerManualRefresh.ts @@ -1,4 +1,5 @@ import { useCallback, useEffect, useRef, useState } from 'react' +import { subscribeFileExplorerRefreshRequests } from '../watching/file-explorer-refresh-requests' const REFRESH_SPINNER_DELAY_MS = 200 @@ -51,5 +52,8 @@ export function useFileExplorerManualRefresh(refreshTree: () => Promise }) }, [clearSpinnerTimer, refreshTree]) + // A toast outside the tree (e.g. a download whose file was gone) can ask for the same refresh. + useEffect(() => subscribeFileExplorerRefreshRequests(handleRefresh), [handleRefresh]) + return { isRefreshing, showRefreshSpinner, handleRefresh } } diff --git a/src/renderer/src/components/right-sidebar/file-explorer/watching/file-explorer-refresh-requests.ts b/src/renderer/src/components/right-sidebar/file-explorer/watching/file-explorer-refresh-requests.ts new file mode 100644 index 00000000000..0b4b3200bf4 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/watching/file-explorer-refresh-requests.ts @@ -0,0 +1,16 @@ +// Lets surfaces outside the explorer tree (e.g. a download toast that found its file gone) ask the +// mounted explorer to refresh, without threading a callback through every row. +const listeners = new Set<() => void>() + +export function requestFileExplorerRefresh(): void { + for (const listener of listeners) { + listener() + } +} + +export function subscribeFileExplorerRefreshRequests(listener: () => void): () => void { + listeners.add(listener) + return () => { + listeners.delete(listener) + } +} diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index 29e487e15aa..1167a4addb4 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -2027,8 +2027,8 @@ "runtimeDownload": { "fileCount_one": "{{files}} file", "fileCount_other": "{{files}} files", - "folderProgress_one": "{{files}} file · {{amount}}", - "folderProgress_other": "{{files}} files · {{amount}}", + "folderFilesProgress_one": "{{files}} file", + "folderFilesProgress_other": "{{files}} files", "skipped_one": "{{count}} item couldn’t be read and was left out.", "skipped_other": "{{count}} items couldn’t be read and were left out." }, diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 1e1b67c905c..f0a9632039c 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -14652,7 +14652,8 @@ "openContainingFolder": "Open Containing Folder", "revealInFileExplorer": "Reveal in File Explorer", "downloadFolderZip": "Download as ZIP…", - "openLocally": "Open on This Computer" + "openLocally": "Open on This Computer", + "downloadFileEllipsis": "Download…" }, "FileExplorerToolbar": { "d238264654": "Show Git Ignored Files", @@ -22933,13 +22934,9 @@ "openFailedTitle": "Couldn’t open {{name}}", "failedTitle": "Couldn’t download {{name}}", "tooLargeToOpen": "Files over 1 GB can’t be opened directly. Download it instead.", - "tooLarge": "Folders over 2 GB can’t be downloaded at once. Download the folders inside it instead.", - "tooManyEntries": "Folders with more than 65,000 items can’t be downloaded at once. Download the folders inside it instead.", "sandboxUpdateRequired": "This sandbox runs an older version of Orca. New cloud workspaces include downloads. Your files are safe.", "hostUpdateRequired": "The remote host runs an older version of Orca. Update Orca there, then try again.", "busy": "Too many downloads are running. Wait for one to finish, then try again.", - "notFoundInSandbox": "It was moved or deleted in the sandbox. Refresh the file explorer to see the latest files.", - "notFoundOnHost": "It was moved or deleted on the remote host. Refresh the file explorer to see the latest files.", "changed": "It changed while Orca was reading it. Try again.", "sandboxDisconnected": "The connection to the sandbox dropped. Nothing was saved on this computer.", "hostDisconnected": "The connection to the remote host dropped. Nothing was saved on this computer.", @@ -22949,19 +22946,28 @@ "downloading": "Downloading {{name}}", "progressLabel": "Download progress", "cancel": "Cancel", - "leftOutDefaultDirs": "Left out node_modules and .git.", "skipped_one": "{{count}} item couldn’t be read and was left out.", "skipped_other": "{{count}} items couldn’t be read and were left out.", "downloaded": "Downloaded {{name}}", "open": "Open", - "noAppTitle": "No app on this computer opens {{name}}", - "noAppDetail": "Orca saved a copy. Reveal it to choose an app.", "retry": "Try again", "downloadInstead": "Download", - "folderProgress_one": "{{files}} file · {{amount}}", - "folderProgress_other": "{{files}} files · {{amount}}", "fileCount_one": "{{files}} file", - "fileCount_other": "{{files}} files" + "fileCount_other": "{{files}} files", + "amountRateEta": "{{amount}} · {{rate}}/s · {{eta}} left", + "folderFilesProgress_one": "{{files}} file", + "folderFilesProgress_other": "{{files}} files", + "zipSize": "{{size}} zip", + "defaultDirsNotIncluded": "node_modules and .git weren’t included.", + "refresh": "Refresh", + "folderTooLarge": "This folder is over 2 GB, the most Orca can download at once. Download the folders inside it one at a time. Nothing was saved on this computer.", + "fileGrew": "The file grew while Orca was copying it. Nothing was saved on this computer.", + "folderTooManyItems": "This folder has more than 65,000 items, the most Orca can download at once. Download the folders inside it one at a time. Nothing was saved on this computer.", + "openRefused": "Orca doesn’t open files that can run programs, like scripts, installers and apps. Download it to check it first. Nothing was saved on this computer.", + "notFoundInSandboxNothingSaved": "It was moved or deleted in the sandbox. Nothing was saved on this computer.", + "notFoundOnHostNothingSaved": "It was moved or deleted on the remote host. Nothing was saved on this computer.", + "savedName": "Saved {{name}}", + "noAppForType": "No app on this computer opens this file type. Reveal it to choose one." } }, "dashboardPopout": { diff --git a/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts b/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts index 51537080a02..8bf3aba084e 100644 --- a/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts +++ b/src/renderer/src/runtime/files/downloads/runtime-download-progress-store.ts @@ -3,12 +3,15 @@ import type { RuntimeDownloadProgressEvent } from '../../../../../shared/runtime export type RuntimeDownloadProgressSnapshot = RuntimeDownloadProgressEvent & { /** Recent receive rate, smoothed so the label does not jitter. */ bytesPerSecond: number | null + /** How long the rate has been sampled; a time-left estimate waits until it settles. */ + rateWindowMs: number } type Entry = { snapshot: RuntimeDownloadProgressSnapshot | null listeners: Set<() => void> lastSample: { at: number; bytes: number } | null + firstSampleAt: number | null } const entries = new Map() @@ -19,7 +22,7 @@ const RATE_SMOOTHING = 0.3 function entryFor(downloadId: string): Entry { let entry = entries.get(downloadId) if (!entry) { - entry = { snapshot: null, listeners: new Set(), lastSample: null } + entry = { snapshot: null, listeners: new Set(), lastSample: null, firstSampleAt: null } entries.set(downloadId, entry) } return entry @@ -43,7 +46,8 @@ export function applyRuntimeDownloadProgress( : bytesPerSecond + RATE_SMOOTHING * (instant - bytesPerSecond) } entry.lastSample = { at: now, bytes: event.receivedBytes } - entry.snapshot = { ...event, bytesPerSecond } + entry.firstSampleAt ??= now + entry.snapshot = { ...event, bytesPerSecond, rateWindowMs: now - entry.firstSampleAt } for (const listener of entry.listeners) { listener() } @@ -70,7 +74,12 @@ export function subscribeRuntimeDownloadProgress( downloadId: string, listener: () => void ): () => void { - const entry = entryFor(downloadId) + // Never creates an entry: a toast rendering after release() must not resurrect one, or the IPC + // listener would never be removed. + const entry = entries.get(downloadId) + if (!entry) { + return () => {} + } entry.listeners.add(listener) return () => { entry.listeners.delete(listener) diff --git a/src/shared/runtime-downloads/runtime-download-contract.ts b/src/shared/runtime-downloads/runtime-download-contract.ts index 3aa7ba861d3..165afdf8847 100644 --- a/src/shared/runtime-downloads/runtime-download-contract.ts +++ b/src/shared/runtime-downloads/runtime-download-contract.ts @@ -1,4 +1,5 @@ /** Desktop downloads of files and folders that live on a paired runtime host. */ +import { blockedPluginSaveAsExtension } from '../plugins/frames/plugin-save-as-extensions' export type RuntimeDownloadErrorCode = | 'canceled' @@ -11,11 +12,21 @@ export type RuntimeDownloadErrorCode = | 'not_found' | 'disconnected' | 'open_failed' + /** "Open on this computer" refuses files the OS would run (scripts, installers, apps). */ + | 'open_refused' | 'transfer_failed' /** Largest file "Open locally" will fetch into the temp cache. */ export const RUNTIME_OPEN_LOCALLY_MAX_BYTES = 1024 * 1024 * 1024 +/** + * Whether Orca may hand a fetched copy straight to the OS opener. The host controls the bytes and the + * name, so anything Windows or macOS would run on a double-click is only ever saved, never opened. + */ +export function canOpenRuntimeDownloadLocally(name: string): boolean { + return blockedPluginSaveAsExtension(name) === null +} + export type RuntimeDownloadStartArgs = { /** Caller-chosen id; progress events and cancel use it. */ downloadId: string From 265c588da3d3ddb89283737110875a8401adf75b Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:38:07 -0800 Subject: [PATCH 06/30] chore(i18n): sync runtime-required and protected-copy catalogs --- src/renderer/src/i18n/en-runtime-required.json | 16 ++++++++++++++++ .../plugins/languages/protected-copy-keys.json | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index 1167a4addb4..69e2fc8a05e 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -1450,6 +1450,22 @@ "branchMoved_other": "{{branch}} is {{count}} commits ahead of build #{{number}}" } }, + "cloudMcpSync": { + "summary": { + "found": "{{count}} found", + "found_one": "{{count}} found", + "found_other": "{{count}} found", + "notSynced": "{{count}} found, not synced yet", + "notSynced_one": "{{count}} found, not synced yet", + "notSynced_other": "{{count}} found, not synced yet", + "skipped": "{{count}} skipped", + "skipped_one": "{{count}} skipped", + "skipped_other": "{{count}} skipped", + "synced": "{{count}} synced", + "synced_one": "{{count}} synced", + "synced_other": "{{count}} synced" + } + }, "cloudSecrets": { "paste": { "blocked": "{{count}} need a fix and will be skipped", diff --git a/src/shared/plugins/languages/protected-copy-keys.json b/src/shared/plugins/languages/protected-copy-keys.json index aaf70246989..5a801b63bab 100644 --- a/src/shared/plugins/languages/protected-copy-keys.json +++ b/src/shared/plugins/languages/protected-copy-keys.json @@ -640,6 +640,13 @@ "auto.components.workspace.cleanup.WorkspaceCleanupDialog.e8b3741ff7", "auto.components.workspace.cleanup.WorkspaceCleanupDialog.e97e4580c7", "auto.components.workspace.cleanup.backgroundRemoval.error", + "auto.components.worktree.creation.VmBuildFailureCard.deleteSandbox", + "auto.components.worktree.creation.VmBuildFailureCard.deleteVm", + "auto.components.worktree.creation.VmDeleteConfirmDialog.bodyCloud", + "auto.components.worktree.creation.VmDeleteConfirmDialog.bodyVm", + "auto.components.worktree.creation.VmDeleteConfirmDialog.keep", + "auto.components.worktree.creation.VmDeleteConfirmDialog.titleCloud", + "auto.components.worktree.creation.VmDeleteConfirmDialog.titleVm", "auto.lib.agent.catalog.e7a4ca5103", "auto.lib.agent.catalog.fc80296033", "auto.main.ipc.diagnostics.uploadConfirm.cancel", From 0dba221289e5311aeb7f7e13a37d7888ea10a07a Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:42:47 -0800 Subject: [PATCH 07/30] fix(files): middle-truncate long names in download toast titles so the extension stays visible --- .../panel/runtime-download-toast.tsx | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx index b2b197553ca..144c592c5f4 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx @@ -58,6 +58,19 @@ const formatCount = (value: number): string => countFormat.format(value) // A time-left estimate from the first second of a transfer swings wildly; wait for the rate to settle. const ETA_MIN_RATE_WINDOW_MS = 2_000 +// Titles truncate at the end in CSS, which hides the extension; shorten long names in the middle. +const MAX_TITLE_NAME_CHARS = 40 + +export function middleTruncateName(name: string, max = MAX_TITLE_NAME_CHARS): string { + const chars = Array.from(name) + if (chars.length <= max) { + return name + } + const tail = Math.floor((max - 1) / 2) + const head = max - 1 - tail + return `${chars.slice(0, head).join('')}…${chars.slice(chars.length - tail).join('')}` +} + const CARD = 'relative w-[356px] rounded-lg border border-border bg-popover px-3 py-2.5 text-sm text-popover-foreground shadow-lg' @@ -145,8 +158,12 @@ function ProgressCard({ const detail = progressDetail(t, state.itemKind, state.snapshot) const title = state.intent === 'open' - ? t('components.runtimeDownload.opening', 'Opening {{name}}', { name: state.name }) - : t('components.runtimeDownload.downloading', 'Downloading {{name}}', { name: state.name }) + ? t('components.runtimeDownload.opening', 'Opening {{name}}', { + name: middleTruncateName(state.name) + }) + : t('components.runtimeDownload.downloading', 'Downloading {{name}}', { + name: middleTruncateName(state.name) + }) const progressLabel = t('components.runtimeDownload.progressLabel', 'Download progress') const Icon = state.intent === 'open' ? MonitorDown : Download return ( @@ -243,7 +260,7 @@ function DoneCard({

{t('components.runtimeDownload.downloaded', 'Downloaded {{name}}', { - name: state.name + name: middleTruncateName(state.name) })}

{parts.join(' · ')}

From 5b030242fed8c8df07c6ed501c7a30fcb1f576d4 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:50:23 -0800 Subject: [PATCH 08/30] fix(files): refuse a re-pointed archive root, block bare-extension names, drop links that climb after a normal segment, keep the orcad bundle ASCII --- .../files/archive/folder-archive-sessions.test.ts | 14 ++++++++++++-- .../runtime/files/archive/folder-archive-walk.ts | 13 +++++++++++-- .../files/archive/folder-archive-zip-stream.ts | 7 +++++++ .../plugins/frames/plugin-save-as-extensions.ts | 3 ++- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/runtime/files/archive/folder-archive-sessions.test.ts b/src/main/runtime/files/archive/folder-archive-sessions.test.ts index f1e73c17f98..0b064f961e4 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.test.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, mkdir, rename, rm, symlink, writeFile, chmod } from 'node:fs/promises' +import { mkdtemp, mkdir, realpath, rename, rm, symlink, writeFile, chmod } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { unzipSync } from 'fflate' @@ -14,7 +14,8 @@ import { FolderArchiveZipStream, isContainedLinkTarget } from './folder-archive- let root: string beforeEach(async () => { - root = await mkdtemp(join(tmpdir(), 'orca-archive-test-')) + // Canonical, as the RPC hands it over (macOS tmpdir sits behind /var -> /private/var). + root = await realpath(await mkdtemp(join(tmpdir(), 'orca-archive-test-'))) }) afterEach(async () => { @@ -114,6 +115,7 @@ describe('FolderArchiveSessions', () => { expect(isContainedLinkTarget('p/a/l', '../../x')).toBe(false) expect(isContainedLinkTarget('p/l', '/etc/passwd')).toBe(false) expect(isContainedLinkTarget('p/l', 'C:x')).toBe(false) + expect(isContainedLinkTarget('p/a/b/l', 's/../../..')).toBe(false) expect(isContainedLinkTarget('p/l', '..\\x')).toBe(false) }) @@ -197,6 +199,14 @@ describe('FolderArchiveSessions', () => { ).rejects.toEqual(new FolderArchiveLimitError('archive_too_many_entries')) }) + it('refuses a folder that was swapped for a link after it was authorized', async () => { + await mkdir(join(root, 'real')) + await symlink(join(root, 'real'), join(root, 'swapped')) + await expect( + planFolderArchive(join(root, 'swapped'), 's', { excludeDefaultDirs: true }) + ).rejects.toThrow('archive_not_directory') + }) + it('refuses to archive a file', async () => { await writeFile(join(root, 'f'), 'x') const sessions = new FolderArchiveSessions() diff --git a/src/main/runtime/files/archive/folder-archive-walk.ts b/src/main/runtime/files/archive/folder-archive-walk.ts index e276a35dd60..84d9fa6196b 100644 --- a/src/main/runtime/files/archive/folder-archive-walk.ts +++ b/src/main/runtime/files/archive/folder-archive-walk.ts @@ -52,8 +52,16 @@ export async function planFolderArchive( archiveRootName: string, options: FolderArchiveWalkOptions ): Promise { - // Child realpath checks compare against this, so the root itself must be canonical. + // The caller hands over the authorized, canonical path. If it no longer resolves to itself (the + // folder was swapped for a link after authorization), refuse rather than walk wherever it points. const rootRealPath = await realpath(rootPath) + const sameRoot = + process.platform === 'win32' + ? rootRealPath.toLowerCase() === rootPath.toLowerCase() + : rootRealPath === rootPath + if (!sameRoot) { + throw new Error('archive_not_directory') + } const maxTotalBytes = options.maxTotalBytes ?? FOLDER_ARCHIVE_MAX_TOTAL_BYTES const maxEntries = options.maxEntries ?? FOLDER_ARCHIVE_MAX_ENTRIES const excluded = new Set( @@ -211,8 +219,9 @@ export function sanitizeArchiveSegment(name: string): string { return WINDOWS_RESERVED_NAME.test(cleaned) ? `_${cleaned}` : cleaned } +// Superscript digits written as escapes: the orcad bundle must stay ASCII. const WINDOWS_RESERVED_NAME = - /^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9¹²³]|lpt[0-9¹²³])(?:\..*)?$/i + /^(?:con|prn|aux|nul|conin\$|conout\$|com[0-9\u00B9\u00B2\u00B3]|lpt[0-9\u00B9\u00B2\u00B3])(?:\..*)?$/i const INVISIBLE_FORMAT_CHARS = /[\u200b-\u200f\u202a-\u202e\u2060-\u2069\ufeff]/g diff --git a/src/main/runtime/files/archive/folder-archive-zip-stream.ts b/src/main/runtime/files/archive/folder-archive-zip-stream.ts index 339cf7f1f63..4b44cd1e93b 100644 --- a/src/main/runtime/files/archive/folder-archive-zip-stream.ts +++ b/src/main/runtime/files/archive/folder-archive-zip-stream.ts @@ -293,6 +293,13 @@ export function isContainedLinkTarget(archivePath: string, target: string): bool if (target === '' || isAbsolute(target) || target.includes('\\') || /^[a-zA-Z]:/.test(target)) { return false } + // A `..` after a normal segment may climb through another stored link (`s/../..`), which the + // lexical check below can't see; keep only targets whose `..` steps all come first. + const segments = target.split('/') + const firstNormal = segments.findIndex((segment) => segment !== '..' && segment !== '.') + if (firstNormal !== -1 && segments.slice(firstNormal).includes('..')) { + return false + } const rootName = archivePath.slice(0, archivePath.indexOf('/')) const resolved = posix.normalize(posix.join(posix.dirname(archivePath), target)) return resolved === rootName || resolved.startsWith(`${rootName}/`) diff --git a/src/shared/plugins/frames/plugin-save-as-extensions.ts b/src/shared/plugins/frames/plugin-save-as-extensions.ts index fd9afbf7b47..e40b3fd59b3 100644 --- a/src/shared/plugins/frames/plugin-save-as-extensions.ts +++ b/src/shared/plugins/frames/plugin-save-as-extensions.ts @@ -124,6 +124,7 @@ export function blockedPluginSaveAsExtension(name: string): string | null { .split(/[\\/]/) .at(-1) ?? '' const dot = base.lastIndexOf('.') - const extension = dot > 0 ? base.slice(dot + 1).toLowerCase() : '' + // `>= 0`: Windows treats a bare `.hta` or `.bat` as that type and runs it. + const extension = dot !== -1 ? base.slice(dot + 1).toLowerCase() : '' return PLUGIN_SAVE_AS_BLOCKED_EXTENSIONS.has(extension) ? extension : null } From 04bcdbe8f4c0c9f06520e96b955fe4a59979e3f7 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:52:08 -0800 Subject: [PATCH 09/30] fix(cloud-integration): lint green after w5-cli (#112) - --on cloud reads origin through @orca/process-host runProcessSync - isCloudAgent guard replaces agent casts; credentials and API error bodies read through jsonFields - client's generic response boundary carries a SAFETY note; test fixtures typed (vi.fn) --- src/cli/cloud/cloud-api-client.ts | 5 +++-- src/cli/cloud/cloud-cli.test.ts | 2 +- src/cli/cloud/cloud-credentials.ts | 9 ++++++++- src/cli/cloud/cloud-operations.ts | 9 +++++++-- src/cli/cloud/index-cloud-commands.test.ts | 14 +++++++------- src/cli/cloud/worktree-create-on-cloud.ts | 20 ++++++++++---------- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/cli/cloud/cloud-api-client.ts b/src/cli/cloud/cloud-api-client.ts index 4570426114b..bbd55f27080 100644 --- a/src/cli/cloud/cloud-api-client.ts +++ b/src/cli/cloud/cloud-api-client.ts @@ -2,7 +2,7 @@ // becomes a RuntimeClientError with a stable `cloud_*` code and next steps, so `--json` callers // (agents) and people both get an actionable message. import { RuntimeClientError } from '../runtime/types' -import type { CloudCredentials } from './cloud-credentials' +import { jsonFields, type CloudCredentials } from './cloud-credentials' export type CloudFetch = (input: string, init: RequestInit) => Promise @@ -68,11 +68,12 @@ export class CloudApiClient { } } if (!response.ok) { - throw cloudApiError(response.status, (body ?? {}) as CloudApiErrorBody, { + throw cloudApiError(response.status, jsonFields(body), { credentials: this.credentials, retryAfter: response.headers.get('retry-after') }) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the client's typed boundary; each caller names the control-plane response shape it reads return body as T } } diff --git a/src/cli/cloud/cloud-cli.test.ts b/src/cli/cloud/cloud-cli.test.ts index 0832a5de031..54b26931fb3 100644 --- a/src/cli/cloud/cloud-cli.test.ts +++ b/src/cli/cloud/cloud-cli.test.ts @@ -120,7 +120,7 @@ function ctx(flags: Record, json = true): HandlerConte }, cwd: home, json - } as HandlerContext + } } function lastJson(): { ok: boolean; schemaVersion: number; result: Record } { diff --git a/src/cli/cloud/cloud-credentials.ts b/src/cli/cloud/cloud-credentials.ts index d33f1033a2d..2e61cfa15a1 100644 --- a/src/cli/cloud/cloud-credentials.ts +++ b/src/cli/cloud/cloud-credentials.ts @@ -101,6 +101,13 @@ export function normalizeCloudApiUrl(raw: string, label: string): string { type CredentialsFile = { apiUrl?: unknown; token?: unknown; workspaceId?: unknown } +/** A parsed JSON value's own fields, or none when it is not an object. */ +export function jsonFields(value: unknown): Record { + return typeof value === 'object' && value !== null + ? Object.fromEntries(Object.entries(value)) + : {} +} + function readCredentialsFile( path: string, fs: CloudCredentialFs @@ -111,7 +118,7 @@ function readCredentialsFile( } let parsed: CredentialsFile try { - parsed = JSON.parse(raw) as CredentialsFile + parsed = jsonFields(JSON.parse(raw)) } catch { throw new RuntimeClientError('cloud_credentials_invalid', `${path} is not valid JSON.`, { path, diff --git a/src/cli/cloud/cloud-operations.ts b/src/cli/cloud/cloud-operations.ts index 76a98d90bf5..031f66ae763 100644 --- a/src/cli/cloud/cloud-operations.ts +++ b/src/cli/cloud/cloud-operations.ts @@ -24,6 +24,11 @@ export type CloudHandlerDeps = { } export const CLOUD_AGENTS = ['claude', 'codex'] as const +export type CloudAgent = (typeof CLOUD_AGENTS)[number] + +export function isCloudAgent(value: string): value is CloudAgent { + return CLOUD_AGENTS.some((agent) => agent === value) +} const WORKSPACE_ID = /^sbx_[A-Za-z0-9]+$/ const PROJECT_ID = /^prj_[A-Za-z0-9]+$/ const SESSION_HANDLE = /^term_[A-Za-z0-9-]{1,80}$/ @@ -71,7 +76,7 @@ export function getAgentFlag( if (agent === undefined) { return undefined } - if (!(CLOUD_AGENTS as readonly string[]).includes(agent)) { + if (!isCloudAgent(agent)) { throw new RuntimeClientError( 'invalid_argument', `--agent must be one of: ${CLOUD_AGENTS.join(', ')}`, @@ -80,7 +85,7 @@ export function getAgentFlag( } ) } - return agent as (typeof CLOUD_AGENTS)[number] + return agent } /** diff --git a/src/cli/cloud/index-cloud-commands.test.ts b/src/cli/cloud/index-cloud-commands.test.ts index 31ac30f52d3..ee83e53edf6 100644 --- a/src/cli/cloud/index-cloud-commands.test.ts +++ b/src/cli/cloud/index-cloud-commands.test.ts @@ -26,7 +26,7 @@ import { main } from '../index' let home: string let out: string[] let err: string[] -const fetchMock = vi.fn() +const fetchMock = vi.fn() beforeEach(() => { home = mkdtempSync(join(tmpdir(), 'orca-cloud-main-')) @@ -34,10 +34,10 @@ beforeEach(() => { err = [] vi.spyOn(console, 'log').mockImplementation((line: unknown) => void out.push(String(line))) vi.spyOn(console, 'error').mockImplementation((line: unknown) => void err.push(String(line))) - vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown) => { + vi.spyOn(process.stdout, 'write').mockImplementation((chunk) => { out.push(String(chunk)) return true - }) as typeof process.stdout.write) + }) vi.stubEnv('ORCA_CLOUD_CREDENTIALS_FILE', join(home, 'credentials.json')) vi.stubEnv('ORCA_CLOUD_TOKEN', 'orca_ws_test') vi.stubEnv('ORCA_CLOUD_API_URL', 'https://cloud.test') @@ -65,9 +65,9 @@ describe('orca cloud via main()', () => { ) await main(['cloud', 'workspace', 'create', '--name', 'child', '--json'], home) expect(process.exitCode).toBeUndefined() - const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit] + const [url, init] = fetchMock.mock.calls[0] ?? [] expect(url).toBe('https://cloud.test/v1/workspaces') - expect(JSON.parse(String(init.body))).toEqual({ name: 'child' }) + expect(JSON.parse(String(init?.body))).toEqual({ name: 'child' }) expect(JSON.parse(out.join(''))).toMatchObject({ ok: true, schemaVersion: 1, @@ -87,9 +87,9 @@ describe('orca cloud via main()', () => { ['cloud', 'message', 'sbx_c1', '--text', 'hello', '--agent', 'codex', '--json'], home ) - const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit] + const [url, init] = fetchMock.mock.calls[0] ?? [] expect(url).toBe('https://cloud.test/v1/workspaces/sbx_c1/messages') - expect(JSON.parse(String(init.body))).toEqual({ text: 'hello', agent: 'codex' }) + expect(JSON.parse(String(init?.body))).toEqual({ text: 'hello', agent: 'codex' }) fetchMock.mockResolvedValue(Response.json({ workspaceStatus: 'running', sessions: [] })) await main(['cloud', 'sessions', 'sbx_c1', '--json'], home) diff --git a/src/cli/cloud/worktree-create-on-cloud.ts b/src/cli/cloud/worktree-create-on-cloud.ts index e747288b5d7..91607d083cc 100644 --- a/src/cli/cloud/worktree-create-on-cloud.ts +++ b/src/cli/cloud/worktree-create-on-cloud.ts @@ -4,7 +4,7 @@ // --on cloud: uses an Orca Cloud project's repo and environment defaults. // It creates an Orca Cloud workspace through the public API with the same credentials as // `orca cloud …`, so it works on a desktop and inside a VM (no Orca runtime involved). -import { execFileSync } from 'node:child_process' +import { runProcessSync } from '@orca/process-host' import { existsSync } from 'node:fs' import { isAbsolute, resolve } from 'node:path' import type { HandlerContext } from '../dispatch' @@ -17,6 +17,7 @@ import { createCloudWorkspace, defaultCloudHandlerDeps, getSizeFlag, + isCloudAgent, normalizeCloudRepoUrl, resolveCloudProjectId, type CloudHandlerDeps, @@ -46,11 +47,12 @@ export type CloudOnDeps = CloudHandlerDeps & { function gitOriginUrl(dir: string): string | null { try { - const out = execFileSync('git', ['-C', dir, 'remote', 'get-url', 'origin'], { - encoding: 'utf8', - stdio: ['ignore', 'pipe', 'ignore'], - timeout: 5000 - }).trim() + const result = runProcessSync({ + program: 'git', + args: ['-C', dir, 'remote', 'get-url', 'origin'], + timeoutMs: 5000 + }) + const out = result.code === 0 ? result.stdout.trim() : '' return out || null } catch { return null @@ -139,7 +141,7 @@ export async function createWorktreeOnCloud( } const agent = getOptionalStringFlag(flags, 'agent') const prompt = getOptionalStringFlag(flags, 'prompt') - if (agent !== undefined && !(CLOUD_AGENTS as readonly string[]).includes(agent)) { + if (agent !== undefined && !isCloudAgent(agent)) { throw new RuntimeClientError( 'invalid_argument', `--on cloud runs ${CLOUD_AGENTS.join(' or ')}; --agent ${agent} is not available there yet.`, @@ -181,9 +183,7 @@ export async function createWorktreeOnCloud( ...(target.project ? { projectId: await resolveCloudProjectId(api, target.project) } : {}), ...(repo ? { repo: { url: repo.url, ...(ref ? { ref } : {}) } } : {}), ...(machineSize ? { machineSize } : {}), - ...(prompt && agent - ? { message: { text: prompt, agent: agent as (typeof CLOUD_AGENTS)[number] } } - : {}) + ...(prompt && agent ? { message: { text: prompt, agent } } : {}) } const phase = (name: string, text: string) => { if (progress === 'json') { From 1f734a34f5ae79be08a2bf56eefecc11ddc04838 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:08:33 -0800 Subject: [PATCH 10/30] chore(files): name the archive runtime type and format counts for the UI language --- src/main/runtime/files/archive/folder-archive-sessions.ts | 8 ++++++-- .../file-explorer/panel/runtime-download-toast.tsx | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/runtime/files/archive/folder-archive-sessions.ts b/src/main/runtime/files/archive/folder-archive-sessions.ts index 4822cc7db0c..c747dbdbcab 100644 --- a/src/main/runtime/files/archive/folder-archive-sessions.ts +++ b/src/main/runtime/files/archive/folder-archive-sessions.ts @@ -5,6 +5,7 @@ import type { FileArchiveReadResult, FileArchiveStartResult } from '../../../../shared/rpc-contract/files-archive-params' +import type { RpcContext } from '../../rpc/core' import { planFolderArchive, sanitizeArchiveSegment } from './folder-archive-walk' import { FolderArchiveZipStream } from './folder-archive-zip-stream' @@ -197,9 +198,12 @@ function sanitizeArchiveRootName(name: string): string { : sanitizeArchiveSegment(cleaned) } -const sessionsByRuntime = new WeakMap() +type ArchiveRuntime = RpcContext['runtime'] -export function getFolderArchiveSessions(runtime: object): FolderArchiveSessions { +const sessionsByRuntime = new WeakMap() + +/** One session table per runtime, so archives never outlive the runtime that started them. */ +export function getFolderArchiveSessions(runtime: ArchiveRuntime): FolderArchiveSessions { let sessions = sessionsByRuntime.get(runtime) if (!sessions) { sessions = new FolderArchiveSessions() diff --git a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx index 144c592c5f4..a1fc94da1cb 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/panel/runtime-download-toast.tsx @@ -4,6 +4,7 @@ import { Button } from '@/components/ui/button' import { InlineError } from '@/components/ui/inline-error' import { Progress } from '@/components/ui/progress' import { useTranslate, type Translate } from '@/i18n/use-translate' +import { uiNumberFormat } from '@/i18n/ui-intl-formatters' import { formatElapsedTime } from '@/components/worktree-creation/worktree-creation-clock' import { formatBytes } from '../../../../../../shared/format-bytes' import type { RuntimeDownloadErrorCode } from '../../../../../../shared/runtime-downloads/runtime-download-contract' @@ -53,8 +54,7 @@ export type RuntimeDownloadToastState = export type RuntimeDownloadToastAction = 'cancel' | 'open' | RuntimeDownloadFailureAction -const countFormat = new Intl.NumberFormat() -const formatCount = (value: number): string => countFormat.format(value) +const formatCount = (value: number): string => uiNumberFormat({}).format(value) // A time-left estimate from the first second of a transfer swings wildly; wait for the rate to settle. const ETA_MIN_RATE_WINDOW_MS = 2_000 From b969ccbc0a16d8160c6c21ec576a7282dfdec8b9 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:10:10 -0800 Subject: [PATCH 11/30] copy(plans): drop the per-seat concurrent-sandbox line from the Team card to match the /cloud pricing page --- src/renderer/src/components/billing/plans/TeamPlanCard.tsx | 7 ------- src/renderer/src/i18n/locales/en.json | 1 - 2 files changed, 8 deletions(-) diff --git a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx index 2bbb0a43b13..6a98e6461c7 100644 --- a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx +++ b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx @@ -55,13 +55,6 @@ export function TeamPlanCard({ value0: String(team.largeMachineWeight) } ), - t( - 'auto.components.billing.plans.featureConcurrency2', - '{{value0}} sandboxes running at once per seat', - { - value0: String(team.concurrentSandboxesPerSeat) - } - ), t('auto.components.billing.plans.featureSleep2', 'Sleeps after {{value0}} minutes idle', { value0: String(team.idleSleepMinutes) }), diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index c7bc95a0e3f..908dafb69cf 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -20529,7 +20529,6 @@ "savePercent": "Save {{value0}}%", "seatsPeriod": "{{value0}} · {{value1}}", "was": "Was", - "featureConcurrency2": "{{value0}} sandboxes running at once per seat", "featureHours4": "{{value0}} sandbox hours per seat each month, pooled across your team", "featureLarge3": "Standard (4 vCPU · 8 GB) sandboxes; Large (8 vCPU · 16 GB) uses sandbox hours at {{value0}}× the rate", "featureOverage3": "Optional overage at {{value0}}/hour ({{value1}}/hour on Large), capped at a monthly spend cap you set ({{value2}} by default)", From 8fec6ef31e7b8319a83f09f57513646866610b08 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:12:43 -0800 Subject: [PATCH 12/30] feat(sandbox-control): role changes re-cap keys and MCP grants; Idempotency-Key on workspace create; teammate names in API views and the sender envelope --- .../migrations/0036_api_idempotency.sql | 17 +++ cloud/apps/sandbox-control/src/api/audit.ts | 4 +- .../sandbox-control/src/api/idempotency.ts | 109 ++++++++++++++++++ cloud/apps/sandbox-control/src/api/keys.ts | 3 +- .../src/api/member-role-sync.ts | 49 ++++++++ .../apps/sandbox-control/src/api/messages.ts | 10 +- cloud/apps/sandbox-control/src/api/openapi.ts | 4 +- cloud/apps/sandbox-control/src/api/router.ts | 27 ++++- cloud/apps/sandbox-control/src/mcp/index.ts | 4 +- cloud/apps/sandbox-control/src/mcp/oauth.ts | 3 +- cloud/apps/sandbox-control/src/operator.ts | 12 ++ .../apps/sandbox-control/src/routines/api.ts | 5 +- .../sandbox-control/src/routines/store.ts | 3 +- .../test/api-followups.test.ts | 104 +++++++++++++++++ 14 files changed, 339 insertions(+), 15 deletions(-) create mode 100644 cloud/apps/sandbox-control/migrations/0036_api_idempotency.sql create mode 100644 cloud/apps/sandbox-control/src/api/idempotency.ts create mode 100644 cloud/apps/sandbox-control/src/api/member-role-sync.ts create mode 100644 cloud/apps/sandbox-control/test/api-followups.test.ts diff --git a/cloud/apps/sandbox-control/migrations/0036_api_idempotency.sql b/cloud/apps/sandbox-control/migrations/0036_api_idempotency.sql new file mode 100644 index 00000000000..e3a40d068c7 --- /dev/null +++ b/cloud/apps/sandbox-control/migrations/0036_api_idempotency.sql @@ -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); diff --git a/cloud/apps/sandbox-control/src/api/audit.ts b/cloud/apps/sandbox-control/src/api/audit.ts index 6aebc2d5ff2..03c73aa1ec5 100644 --- a/cloud/apps/sandbox-control/src/api/audit.ts +++ b/cloud/apps/sandbox-control/src/api/audit.ts @@ -64,7 +64,7 @@ export async function listAudit( return results } -export function auditView(row: AuditRow) { +export function auditView(row: AuditRow, names: ReadonlyMap = 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, diff --git a/cloud/apps/sandbox-control/src/api/idempotency.ts b/cloud/apps/sandbox-control/src/api/idempotency.ts new file mode 100644 index 00000000000..99a981c0bdd --- /dev/null +++ b/cloud/apps/sandbox-control/src/api/idempotency.ts @@ -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)[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 { + 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() + 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 { + const result = await db.prepare('DELETE FROM api_idempotency WHERE expires_at <= ?').bind(now).run() + return result.meta.changes +} diff --git a/cloud/apps/sandbox-control/src/api/keys.ts b/cloud/apps/sandbox-control/src/api/keys.ts index 27cb848feb4..eb2454fe4c5 100644 --- a/cloud/apps/sandbox-control/src/api/keys.ts +++ b/cloud/apps/sandbox-control/src/api/keys.ts @@ -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 = 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 diff --git a/cloud/apps/sandbox-control/src/api/member-role-sync.ts b/cloud/apps/sandbox-control/src/api/member-role-sync.ts new file mode 100644 index 00000000000..6bbf7517b89 --- /dev/null +++ b/cloud/apps/sandbox-control/src/api/member-role-sync.ts @@ -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() + +/** 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 { + // 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 { + 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) + } +} diff --git a/cloud/apps/sandbox-control/src/api/messages.ts b/cloud/apps/sandbox-control/src/api/messages.ts index 61a4eba7cdf..bed03311306 100644 --- a/cloud/apps/sandbox-control/src/api/messages.ts +++ b/cloud/apps/sandbox-control/src/api/messages.ts @@ -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 = 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) diff --git a/cloud/apps/sandbox-control/src/api/openapi.ts b/cloud/apps/sandbox-control/src/api/openapi.ts index e44146d0bb9..9ee5ff1ad44 100644 --- a/cloud/apps/sandbox-control/src/api/openapi.ts +++ b/cloud/apps/sandbox-control/src/api/openapi.ts @@ -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 tags: string[] @@ -31,6 +31,7 @@ const schemas: Record = { 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> = { 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' }, diff --git a/cloud/apps/sandbox-control/src/api/router.ts b/cloud/apps/sandbox-control/src/api/router.ts index 6d7e62df9f7..d1671dae01d 100644 --- a/cloud/apps/sandbox-control/src/api/router.ts +++ b/cloud/apps/sandbox-control/src/api/router.ts @@ -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 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') diff --git a/cloud/apps/sandbox-control/src/mcp/index.ts b/cloud/apps/sandbox-control/src/mcp/index.ts index 592429a5ba0..ed3c640a007 100644 --- a/cloud/apps/sandbox-control/src/mcp/index.ts +++ b/cloud/apps/sandbox-control/src/mcp/index.ts @@ -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) diff --git a/cloud/apps/sandbox-control/src/mcp/oauth.ts b/cloud/apps/sandbox-control/src/mcp/oauth.ts index f7508ab08e1..13d6aa5373f 100644 --- a/cloud/apps/sandbox-control/src/mcp/oauth.ts +++ b/cloud/apps/sandbox-control/src/mcp/oauth.ts @@ -685,13 +685,14 @@ function redirectHosts(raw: string | null): string[] { } } -export function grantView(row: GrantRow) { +export function grantView(row: GrantRow, names: ReadonlyMap = 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, diff --git a/cloud/apps/sandbox-control/src/operator.ts b/cloud/apps/sandbox-control/src/operator.ts index de716b8cd99..5ba5d1067a5 100644 --- a/cloud/apps/sandbox-control/src/operator.ts +++ b/cloud/apps/sandbox-control/src/operator.ts @@ -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 { @@ -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) { diff --git a/cloud/apps/sandbox-control/src/routines/api.ts b/cloud/apps/sandbox-control/src/routines/api.ts index 7e97c9b19f7..6f8992c3336 100644 --- a/cloud/apps/sandbox-control/src/routines/api.ts +++ b/cloud/apps/sandbox-control/src/routines/api.ts @@ -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') { diff --git a/cloud/apps/sandbox-control/src/routines/store.ts b/cloud/apps/sandbox-control/src/routines/store.ts index a34188df614..0a3b005cb95 100644 --- a/cloud/apps/sandbox-control/src/routines/store.ts +++ b/cloud/apps/sandbox-control/src/routines/store.ts @@ -103,7 +103,7 @@ export function triggerConfig(row: Pick = 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 } diff --git a/cloud/apps/sandbox-control/test/api-followups.test.ts b/cloud/apps/sandbox-control/test/api-followups.test.ts new file mode 100644 index 00000000000..07831ccce94 --- /dev/null +++ b/cloud/apps/sandbox-control/test/api-followups.test.ts @@ -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[] = [] + 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' }) + }) +}) From 26ee19ee0d1dde809c2bf256e8d4c91ac7a3bae2 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:13:25 -0800 Subject: [PATCH 13/30] copy(plans): Team card bullets match the final /cloud pricing strings --- .../components/billing/plans/TeamPlanCard.tsx | 19 +++++++++++-------- src/renderer/src/i18n/locales/en.json | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx index 6a98e6461c7..f5634cf2918 100644 --- a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx +++ b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx @@ -48,16 +48,19 @@ export function TeamPlanCard({ '{{value0}} sandbox hours per seat each month, pooled across your team', { value0: String(team.includedHoursPerSeat) } ), + // Same bullets as the marketing site's /cloud pricing card; overage below is in-app only. + t('auto.components.billing.plans.featureStandard', 'Standard (4 vCPU · 8 GB)'), t( - 'auto.components.billing.plans.featureLarge3', - 'Standard (4 vCPU · 8 GB) sandboxes; Large (8 vCPU · 16 GB) uses sandbox hours at {{value0}}× the rate', - { - value0: String(team.largeMachineWeight) - } + 'auto.components.billing.plans.featureLarge4', + 'Large (8 vCPU · 16 GB) uses sandbox hours at {{value0}}× the rate', + { value0: String(team.largeMachineWeight) } ), - t('auto.components.billing.plans.featureSleep2', 'Sleeps after {{value0}} minutes idle', { - value0: String(team.idleSleepMinutes) - }), + t( + 'auto.components.billing.plans.featureSleep3', + 'Sleeps after {{value0}} minutes idle, so quiet time is free', + { value0: String(team.idleSleepMinutes) } + ), + t('auto.components.billing.plans.featureReady', 'Ready in seconds, nothing to set up or host'), t( 'auto.components.billing.plans.featureOverage3', 'Optional overage at {{value0}}/hour ({{value1}}/hour on Large), capped at a monthly spend cap you set ({{value2}} by default)', diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 908dafb69cf..2b97fc9d63e 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -20519,7 +20519,8 @@ "vpRunningBody2": "Sandboxes keep working while you are away and report back when they finish.", "leftResets": "{{value0}} hours left · resets {{value1}}", "retry": "Try again", - "featureSleep2": "Sleeps after {{value0}} minutes idle", + "featureSleep3": "Sleeps after {{value0}} minutes idle, so quiet time is free", + "featureReady": "Ready in seconds, nothing to set up or host", "billedYearly": "Billed yearly", "waiting": "Waiting for checkout…", "fixPaymentAbove": "Update your payment method above to keep your plan active.", @@ -20530,7 +20531,8 @@ "seatsPeriod": "{{value0}} · {{value1}}", "was": "Was", "featureHours4": "{{value0}} sandbox hours per seat each month, pooled across your team", - "featureLarge3": "Standard (4 vCPU · 8 GB) sandboxes; Large (8 vCPU · 16 GB) uses sandbox hours at {{value0}}× the rate", + "featureStandard": "Standard (4 vCPU · 8 GB)", + "featureLarge4": "Large (8 vCPU · 16 GB) uses sandbox hours at {{value0}}× the rate", "featureOverage3": "Optional overage at {{value0}}/hour ({{value1}}/hour on Large), capped at a monthly spend cap you set ({{value2}} by default)", "footTerms4": "Prices in USD, before tax. Overage is off until an owner or admin turns it on. Cancel anytime; your plan runs to the end of the billing period.", "lockedBody2": "Only an owner or admin of your team can choose a plan. Ask one of them to open Plan & billing in Orca.", From 9f21a6a3750f04393ffa47027d1f9c3f76281d7f Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:14:07 -0800 Subject: [PATCH 14/30] copy(plans): comment placement --- src/renderer/src/components/billing/plans/TeamPlanCard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx index f5634cf2918..7a2f993b7dc 100644 --- a/src/renderer/src/components/billing/plans/TeamPlanCard.tsx +++ b/src/renderer/src/components/billing/plans/TeamPlanCard.tsx @@ -42,13 +42,13 @@ export function TeamPlanCard({ const discounted = interval === 'year' && monthly !== null && fullMonthly !== null && monthly < fullMonthly + // Same bullets as the marketing site's /cloud pricing card; the overage line is in-app only. const features = [ t( 'auto.components.billing.plans.featureHours4', '{{value0}} sandbox hours per seat each month, pooled across your team', { value0: String(team.includedHoursPerSeat) } ), - // Same bullets as the marketing site's /cloud pricing card; overage below is in-app only. t('auto.components.billing.plans.featureStandard', 'Standard (4 vCPU · 8 GB)'), t( 'auto.components.billing.plans.featureLarge4', From 664a5206df0b8c7f6c4b269af3a3371f5c4ee947 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 04:26:07 -0800 Subject: [PATCH 15/30] feat(cloud): real-VM acceptance harness skeleton (checks, benchmarks, ledger, report) --- cloud/apps/acceptance/package.json | 24 +++ cloud/apps/acceptance/src/bench.ts | 121 +++++++++++ cloud/apps/acceptance/src/catalog.ts | 106 ++++++++++ cloud/apps/acceptance/src/checks/billing.ts | 149 +++++++++++++ cloud/apps/acceptance/src/checks/core.ts | 219 ++++++++++++++++++++ cloud/apps/acceptance/src/checks/ops.ts | 138 ++++++++++++ cloud/apps/acceptance/src/checks/pending.ts | 22 ++ cloud/apps/acceptance/src/checks/secrets.ts | 98 +++++++++ cloud/apps/acceptance/src/clients.ts | 174 ++++++++++++++++ cloud/apps/acceptance/src/context.ts | 75 +++++++ cloud/apps/acceptance/src/env.ts | 49 +++++ cloud/apps/acceptance/src/orca-cli.ts | 60 ++++++ cloud/apps/acceptance/src/report-cli.ts | 3 + cloud/apps/acceptance/src/report.ts | 180 ++++++++++++++++ cloud/apps/acceptance/src/run-state.ts | 127 ++++++++++++ cloud/apps/acceptance/src/run.ts | 74 +++++++ cloud/apps/acceptance/src/stripe.ts | 122 +++++++++++ cloud/apps/acceptance/src/sweep.ts | 42 ++++ cloud/apps/acceptance/src/tokens.ts | 34 +++ cloud/apps/acceptance/tsconfig.json | 15 ++ cloud/pnpm-lock.yaml | 32 +++ 21 files changed, 1864 insertions(+) create mode 100644 cloud/apps/acceptance/package.json create mode 100644 cloud/apps/acceptance/src/bench.ts create mode 100644 cloud/apps/acceptance/src/catalog.ts create mode 100644 cloud/apps/acceptance/src/checks/billing.ts create mode 100644 cloud/apps/acceptance/src/checks/core.ts create mode 100644 cloud/apps/acceptance/src/checks/ops.ts create mode 100644 cloud/apps/acceptance/src/checks/pending.ts create mode 100644 cloud/apps/acceptance/src/checks/secrets.ts create mode 100644 cloud/apps/acceptance/src/clients.ts create mode 100644 cloud/apps/acceptance/src/context.ts create mode 100644 cloud/apps/acceptance/src/env.ts create mode 100644 cloud/apps/acceptance/src/orca-cli.ts create mode 100644 cloud/apps/acceptance/src/report-cli.ts create mode 100644 cloud/apps/acceptance/src/report.ts create mode 100644 cloud/apps/acceptance/src/run-state.ts create mode 100644 cloud/apps/acceptance/src/run.ts create mode 100644 cloud/apps/acceptance/src/stripe.ts create mode 100644 cloud/apps/acceptance/src/sweep.ts create mode 100644 cloud/apps/acceptance/src/tokens.ts create mode 100644 cloud/apps/acceptance/tsconfig.json diff --git a/cloud/apps/acceptance/package.json b/cloud/apps/acceptance/package.json new file mode 100644 index 00000000000..b185d20d828 --- /dev/null +++ b/cloud/apps/acceptance/package.json @@ -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" + } +} diff --git a/cloud/apps/acceptance/src/bench.ts b/cloud/apps/acceptance/src/bench.ts new file mode 100644 index 00000000000..a5b70dfbc27 --- /dev/null +++ b/cloud/apps/acceptance/src/bench.ts @@ -0,0 +1,121 @@ +// 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 = { + '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' +} +export const CONDUCTOR: Record = { + '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' +} + +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 { + 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 { + 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) +} diff --git a/cloud/apps/acceptance/src/catalog.ts b/cloud/apps/acceptance/src/catalog.ts new file mode 100644 index 00000000000..c3e25b98548 --- /dev/null +++ b/cloud/apps/acceptance/src/catalog.ts @@ -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])) diff --git a/cloud/apps/acceptance/src/checks/billing.ts b/cloud/apps/acceptance/src/checks/billing.ts new file mode 100644 index 00000000000..90ad3ed7933 --- /dev/null +++ b/cloud/apps/acceptance/src/checks/billing.ts @@ -0,0 +1,149 @@ +// 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 { join } from 'node:path' +import { billing } from '../clients' +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 sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)) + +async function until(read: () => Promise, done: (v: T) => boolean, timeoutMs = 90_000): Promise { + const deadline = Date.now() + timeoutMs + let last = await read() + while (!done(last) && Date.now() < deadline) { + await sleep(2000) + last = await read() + } + 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 { + const list = await stripe<{ data: Array<{ id: string; metadata: Record }> }>('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 { + 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>> | null = null +const team = (ctx: Ctx) => (activeTeam ??= checkoutWith(ctx, 'card', CARDS.visa, 2)) + +export const billingChecks: Record = { + '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 () => ({ + status: 'not_landed', + detail: 'Seats follow Orca Cloud auth membership (POST /v1/internal/seats, MEMBERSHIP_SYNC_TOKEN); auth not deployed and the eval has no membership token yet — waiting on team-billing-ux' + }), + + 'billing.dunning': async () => ({ + status: 'not_landed', + detail: 'Needs CONTROL_PLANE_TOKEN + D1 account link for the test-clock flow (billing e2e-test-mode.ts covers it); eval wiring pending — waiting on team-billing-ux' + }), + + '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' } + } +} diff --git a/cloud/apps/acceptance/src/checks/core.ts b/cloud/apps/acceptance/src/checks/core.ts new file mode 100644 index 00000000000..d72a73195c1 --- /dev/null +++ b/cloud/apps/acceptance/src/checks/core.ts @@ -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 ${JSON.stringify(script)}` + +export const coreChecks: Record = { + '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) >= 30 ? 'partial' : 'fail', + detail: `disk ${size} GB (${avail} GB free). Conductor: 64 GB. Low-disk alert not yet probed (W1/W2).`, + 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', 'chromium', '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 + } +} diff --git a/cloud/apps/acceptance/src/checks/ops.ts b/cloud/apps/acceptance/src/checks/ops.ts new file mode 100644 index 00000000000..f1f97ad7f4e --- /dev/null +++ b/cloud/apps/acceptance/src/checks/ops.ts @@ -0,0 +1,138 @@ +// 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 = { + 'ops.kill-switch': async (ctx) => { + const p = await ctx.primary() + 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' } + } + 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}`) + } + // The primary is running; resume of a running sandbox must still be refused while killed. + resumeWhileOff = (await control.as(ctx.solo, 'POST', `/v1/sandboxes/${p.sandbox.id}/resume`)).status + } finally { + await control.operator('POST', '/v1/internal/kill-switch', { enabled: false }) + } + 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: GOLDEN(), + vcpus: 2, + region: 'iad1', + timeoutMs: 10 * 60_000, + ports: [], + networkPolicy: { allow: {} }, + 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() + 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}` + } + } +} + +function GOLDEN(): string { + return process.env.ORCA_EVAL_GOLDEN_SNAPSHOT ?? 'snap_vpcmcCAgTtISiTZgsCosOlThQbFd' +} diff --git a/cloud/apps/acceptance/src/checks/pending.ts b/cloud/apps/acceptance/src/checks/pending.ts new file mode 100644 index 00000000000..c0b018aee28 --- /dev/null +++ b/cloud/apps/acceptance/src/checks/pending.ts @@ -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 { + const r = await control.as({ sub: 'w9-eval' }, method, path) + return r.status +} + +export const pendingChecks: Record = { + '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` } + } +} diff --git a/cloud/apps/acceptance/src/checks/secrets.ts b/cloud/apps/acceptance/src/checks/secrets.ts new file mode 100644 index 00000000000..b7c932e8f80 --- /dev/null +++ b/cloud/apps/acceptance/src/checks/secrets.ts @@ -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> { + 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', 'OPENAI_API_KEY', `org-fake-${tag}`) + await put(ctx, ctx.member, 'personal', 'W9_OVERRIDE', `personal-${tag}`) + await put(ctx, ctx.member, 'personal', 'OPENAI_API_KEY', `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 = { + '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', 'OPENAI_API_KEY']) + const personalWinsEnv = env.W9_OVERRIDE === `personal-${tag}` + const orgWinsAgentKey = env.OPENAI_API_KEY === `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, 'OPENAI_API_KEY'], + ['personal', ctx.member, 'W9_OVERRIDE'], + ['personal', ctx.member, 'OPENAI_API_KEY'] + ] 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)` } + } +} diff --git a/cloud/apps/acceptance/src/clients.ts b/cloud/apps/acceptance/src/clients.ts new file mode 100644 index 00000000000..e9b4ba217c8 --- /dev/null +++ b/cloud/apps/acceptance/src/clients.ts @@ -0,0 +1,174 @@ +// 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 = { status: number; ms: number; body: T; headers: Headers } + +async function call(base: string, method: string, path: string, bearer: string, body?: unknown): Promise> { + 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(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(config.controlPlaneUrl, method, path, bearer, body) +} + +export const billing = { + as: async (who: Identity, method: string, path: string, body?: unknown) => + call(config.billingUrl, method, path, await billingToken(who), body), + raw: (method: string, path: string, bearer: string, body?: unknown) => call(config.billingUrl, method, path, bearer, body) +} + +let vercelApi: ReturnType | 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 = {}) { + 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 + 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 = { standard: 4, large: 8 } + +/** Lifecycle calls with ledger bookkeeping; the provider name is `orca--` (dev: orca-dev-). */ +export class Sandboxes { + private live = new Map() + 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 } } + ): Promise> { + 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> { + 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> { + 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> { + return control.as(this.who(id), 'POST', `/v1/sandboxes/${id}/rollover`) + } + + async get(id: string): Promise> { + return control.as(this.who(id), 'GET', `/v1/sandboxes/${id}`) + } + + async destroy(id: string): Promise> { + 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() }) + } +} diff --git a/cloud/apps/acceptance/src/context.ts b/cloud/apps/acceptance/src/context.ts new file mode 100644 index 00000000000..671281a44e7 --- /dev/null +++ b/cloud/apps/acceptance/src/context.ts @@ -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 } +export type Check = (ctx: Ctx) => Promise + +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 | 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 { + 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 { + 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` }) diff --git a/cloud/apps/acceptance/src/env.ts b/cloud/apps/acceptance/src/env.ts new file mode 100644 index 00000000000..36861371beb --- /dev/null +++ b/cloud/apps/acceptance/src/env.ts @@ -0,0 +1,49 @@ +// 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(file: string): T | null { + const path = join(SECRETS_DIR, file) + return existsSync(path) ? (JSON.parse(readFileSync(path, 'utf8')) as T) : 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, + vercelToken: () => readSecretJson<{ bearerToken: string }>('tok.json')?.bearerToken ?? 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 + }, + /** Optional: sk_test_/rk_test_ only. Falls back to the Stripe CLI `orca` project profile. */ + stripeKey: () => process.env.ORCA_EVAL_STRIPE_KEY ?? null +} diff --git a/cloud/apps/acceptance/src/orca-cli.ts b/cloud/apps/acceptance/src/orca-cli.ts new file mode 100644 index 00000000000..2b7b3a6bf7e --- /dev/null +++ b/cloud/apps/acceptance/src/orca-cli.ts @@ -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 { + 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, '').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' } +} diff --git a/cloud/apps/acceptance/src/report-cli.ts b/cloud/apps/acceptance/src/report-cli.ts new file mode 100644 index 00000000000..691eb27a02c --- /dev/null +++ b/cloud/apps/acceptance/src/report-cli.ts @@ -0,0 +1,3 @@ +import { renderReport } from './report' + +console.log(renderReport()) diff --git a/cloud/apps/acceptance/src/report.ts b/cloud/apps/acceptance/src/report.ts new file mode 100644 index 00000000000..a0a1495dac2 --- /dev/null +++ b/cloud/apps/acceptance/src/report.ts @@ -0,0 +1,180 @@ +// 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) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]!) +const sec = (ms: number | null) => (ms === null ? 'n/a' : `${(ms / 1000).toFixed(2)} s`) + +const STATUS: Record = { + 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 { + const out = new Map() + 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]) => `${esc(k)} ${sec(v)}`).join(' ')) + } + for (const e of result.evidence) { + if (e.path) { + const href = relative(config.contextDir, e.path) + parts.push(e.kind === 'screenshot' ? `${esc(e.label)}` : `${esc(e.label)}`) + } else if (e.text) { + parts.push(`
${esc(e.label)}
${esc(e.text)}
`) + } + } + parts.push(`run ${esc(run.runId)} · ${esc(result.startedAt.slice(0, 16).replace('T', ' '))}Z`) + return parts.join('
') +} + +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 ${esc(f.owner)}.` + return `${esc(f.feature)}
${esc(f.id)} · ${esc(f.priority)} · ${esc(f.owner)}
+${s.label}${detail}${hit ? evidenceHtml(hit.result, hit.run) : ''} +${esc(f.conductor)}${f.better ? esc(f.better) : ''}` +} + +function benchTable(runs: RunFile[]): string { + const latest = new Map() + 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', 'wake.api', 'wake.firstEcho', '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 `${esc(name)}${n}${b ? sec(percentile(b.samples, 50)) : 'pending'} +${b ? sec(percentile(b.samples, 95)) : ''}${b ? `${sec(Math.min(...b.samples))} – ${sec(Math.max(...b.samples))}` : ''} +${esc(CONDUCTOR[name] ?? '')}${esc(EARLIER[name] ?? '')}${b ? `run ${esc(b.run.runId)}${b.note ? `
${esc(b.note)}` : ''}` : ''}` + }) + return `
${rows.join('')}
Metricnp50p95min – maxConductor (documented)Our earlier numbersSource
` +} + +function ledgerTable(runs: RunFile[]): string { + const rows = runs.flatMap((run) => + run.resources.map( + (r) => `${esc(run.runId)}${esc(r.kind)}${esc(r.id)}${esc(r.label ?? '')} +${esc(r.createdAt.slice(11, 19))}${r.destroyedAt ? esc(r.destroyedAt.slice(11, 19)) : 'NOT DESTROYED'}` + ) + ) + return `
${rows.length} resources across ${runs.length} runs
${rows.join('')}
RunKindIdLabelCreated (UTC)Destroyed
` +} + +/** Hand-curated findings/gaps (HTML snippets), kept beside the report: acceptance-findings.json {findings, gaps}. */ +function loadFindings(): { gaps?: string[]; findings?: string[] } { + const path = `${config.contextDir}/acceptance-findings.json` + 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 `${esc(section)}${rows.join('')}` + }).join('') + const lastRun = runs.at(-1) + const html = `Orca Cloud evaluation report + +
+

Orca Cloud evaluation report

+

Generated ${esc(new Date().toISOString().slice(0, 16).replace('T', ' '))}Z by cloud/apps/acceptance (branch cloud/w9-eval) from ${runs.length} run(s) in acceptance-runs/. +Real Vercel Sandbox VMs (team lovecast, Pro, iad1) via the DEV control plane ${esc(config.controlPlaneUrl)} and DEV billing ${esc(config.billingUrl)}; Stripe TEST mode only. +Parity bar: conductor-parity-matrix.html. Each row shows the most recent result for that feature and the run it came from.

+
Status (${CATALOG.length} features): +✅ ${counts.pass} pass +⚠️ ${counts.partial} partial +❌ ${counts.fail} fail +⏳ ${counts.not_landed} not landed +— ${counts.skipped} skipped
+Last run: ${lastRun ? `${esc(lastRun.runId)} (${esc(lastRun.startedAt.slice(0, 16))}Z, target ${esc(lastRun.target.integrationSha ?? lastRun.target.desktopRef ?? 'API only')})` : 'none'} · +Eval cost so far: ≈ $${cost.usd.toFixed(2)} Vercel compute (${cost.vcpuHours.toFixed(2)} vCPU-h, upper bound at list price) · Leaked resources: ${leaked.length === 0 ? '0 ✅' : `${leaked.length}`}
+

Features

+
${sections}
FeatureStatusResultEvidenceConductorBetter than Conductor
+

Benchmarks

+

Nearest-rank percentiles. Definitions: create.api = POST /v1/sandboxes until orcad is ready (golden snapshot boot + private repo clone); +create.usable adds the user's client connecting (orca CLI, E2EE handshake); *.firstEcho 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); rollover.gap = drain + stop + boot + orcad ready.

+${benchTable(runs)} +

Known gaps and findings

+
    ${[...(extra.findings ?? []), ...(extra.gaps ?? [])].map((g) => `
  • ${g}
  • `).join('') || '
  • none recorded
  • '}
+

Cost of the eval and every cloud resource

+

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 /v1/credits when an agent check runs. Stripe objects are test mode (free).

+${ledgerTable(runs)} +

How to rerun

+
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
+
` + writeFileSync(config.reportPath, html) + return config.reportPath +} diff --git a/cloud/apps/acceptance/src/run-state.ts b/cloud/apps/acceptance/src/run-state.ts new file mode 100644 index 00000000000..d3cec57eab4 --- /dev/null +++ b/cloud/apps/acceptance/src/run-state.ts @@ -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 + 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 { + 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]! +} diff --git a/cloud/apps/acceptance/src/run.ts b/cloud/apps/acceptance/src/run.ts new file mode 100644 index 00000000000..bb7814e68d4 --- /dev/null +++ b/cloud/apps/acceptance/src/run.ts @@ -0,0 +1,74 @@ +// 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 ] +// 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 { billingChecks } from './checks/billing' +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 { 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 = { ...pendingChecks, ...coreChecks, ...secretChecks, ...opsChecks, ...billingChecks } +/** 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)') +}) + +try { + 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 benchCreate(ctx, n) + await benchWakeAndRollover(ctx, n) + } +} 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()}`) +} diff --git a/cloud/apps/acceptance/src/stripe.ts b/cloud/apps/acceptance/src/stripe.ts new file mode 100644 index 00000000000..7cb53eaf5bf --- /dev/null +++ b/cloud/apps/acceptance/src/stripe.ts @@ -0,0 +1,122 @@ +// 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, 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, key) + } + return [`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`] + }) +} + +export async function stripe(method: 'GET' | 'POST' | 'DELETE', path: string, params?: Record): Promise { + 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 + +/** 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()) { + const button = frame.locator('#test-source-authorize-3ds, button:has-text("Complete")') + if (await button.count().catch(() => 0)) { + await page.screenshot({ path: shotPath.replace('.png', '-3ds.png') }) + await button.first().click().catch(() => {}) + done = true + 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() + } +} diff --git a/cloud/apps/acceptance/src/sweep.ts b/cloud/apps/acceptance/src/sweep.ts new file mode 100644 index 00000000000..145da2e795b --- /dev/null +++ b/cloud/apps/acceptance/src/sweep.ts @@ -0,0 +1,42 @@ +// 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 { 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 === '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)) + } +} diff --git a/cloud/apps/acceptance/src/tokens.ts b/cloud/apps/acceptance/src/tokens.ts new file mode 100644 index 00000000000..bfcac2b4aba --- /dev/null +++ b/cloud/apps/acceptance/src/tokens.ts @@ -0,0 +1,34 @@ +// 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 | 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 +} + +async function sign(claims: Record, issuer: string, audience: string): Promise { + return new SignJWT(claims) + .setProtectedHeader({ alg: 'ES256', kid: 'dev1' }) + .setIssuer(issuer) + .setAudience(audience) + .setIssuedAt() + .setExpirationTime('5m') + .sign(await signingKey()) +} + +export function sandboxToken(who: Identity): Promise { + return sign({ ...who, purpose: 'sandbox' }, 'orca-sandbox-dev-test', 'orca-sandbox') +} + +export function billingToken(who: Identity): Promise { + return sign({ ...who, purpose: 'billing' }, 'orca-billing-dev-test', 'orca-billing') +} diff --git a/cloud/apps/acceptance/tsconfig.json b/cloud/apps/acceptance/tsconfig.json new file mode 100644 index 00000000000..e04e52b66cb --- /dev/null +++ b/cloud/apps/acceptance/tsconfig.json @@ -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"] +} diff --git a/cloud/pnpm-lock.yaml b/cloud/pnpm-lock.yaml index 40def6043a6..5d1b823b643 100644 --- a/cloud/pnpm-lock.yaml +++ b/cloud/pnpm-lock.yaml @@ -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 From 8433de503228a4508745fca3f33e7af96bd1c265 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 04:37:45 -0800 Subject: [PATCH 16/30] feat(cloud): acceptance checks for agents, findings, 3DS, kill switch on a slept box --- cloud/apps/acceptance/findings.json | 15 ++++ cloud/apps/acceptance/src/checks/agents.ts | 93 ++++++++++++++++++++++ cloud/apps/acceptance/src/checks/core.ts | 2 +- cloud/apps/acceptance/src/checks/ops.ts | 31 ++++++-- cloud/apps/acceptance/src/report.ts | 4 +- cloud/apps/acceptance/src/run.ts | 3 +- cloud/apps/acceptance/src/stripe.ts | 13 ++- 7 files changed, 145 insertions(+), 16 deletions(-) create mode 100644 cloud/apps/acceptance/findings.json create mode 100644 cloud/apps/acceptance/src/checks/agents.ts diff --git a/cloud/apps/acceptance/findings.json b/cloud/apps/acceptance/findings.json new file mode 100644 index 00000000000..818894118f7 --- /dev/null +++ b/cloud/apps/acceptance/findings.json @@ -0,0 +1,15 @@ +{ + "findings": [ + "F1 (bug, core/vercel-driver, filed 2026-11-09): POST /v1/sandboxes/:id/resume on a sandbox that is already running hangs ~60 s and answers 502 provider_unavailable / orcad_start_failed (2/2 repro, VM stays running). Wake-on-open after a desktop restart hits this path. Expected: idempotent, return the live pairing in <1 s.", + "F2 (UX, core): creating with repo for an account with no GitHub App binding fails as 502 orcad_start_failed after 3.8 s instead of a clear github_not_connected the desktop can turn into a Connect GitHub prompt.", + "F3 (blocker for agent.real-task, W6): ai-gateway.vercel.sh is not on the sandbox egress allowlist (DNS fails, curl rc 6), so Claude Code/Codex via the AI Gateway key cannot run in a cloud VM. Secret injection itself works (ANTHROPIC_* reach orcad's env).", + "F4 (parity gap, golden image): the login shell lacks the orca CLI (Conductor ships its CLI in the VM, P0), rg, tmux, vim, git-lfs and Chrome/Chromium. Present: node 24, npm, pnpm, python3, git, gh, jq, Claude Code 2.1.319, Codex 0.166.0.", + "F5 (parity gap): standard disk is 32 GB (Conductor 64 GB since 0.82); no low-disk alert yet.", + "Note: 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 pending an AI Gateway credit top-up (free credits ~$4.94, no anthropic/* models); the agent check uses moonshotai/kimi-k2.", + "Seat proration and the dunning flow (grace -> suspend -> pay -> resume) need MEMBERSHIP_SYNC_TOKEN / CONTROL_PLANE_TOKEN wiring for the harness (billing e2e-test-mode.ts covers them in isolation).", + "Team create/invite need the Orca Cloud auth endpoints deployed (merged, not deployed)." + ] +} diff --git a/cloud/apps/acceptance/src/checks/agents.ts b/cloud/apps/acceptance/src/checks/agents.ts new file mode 100644 index 00000000000..930fead4f5e --- /dev/null +++ b/cloud/apps/acceptance/src/checks/agents.ts @@ -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 { + 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 = { + '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 = { + 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, ''))] + } + } 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) + } + } + } +} diff --git a/cloud/apps/acceptance/src/checks/core.ts b/cloud/apps/acceptance/src/checks/core.ts index d72a73195c1..929a89860be 100644 --- a/cloud/apps/acceptance/src/checks/core.ts +++ b/cloud/apps/acceptance/src/checks/core.ts @@ -5,7 +5,7 @@ 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 ${JSON.stringify(script)}` +const LOGIN = (script: string) => `bash -lc '${script.replaceAll("'", `'"'"'`)}'` export const coreChecks: Record = { 'acct.signin': async (ctx) => { diff --git a/cloud/apps/acceptance/src/checks/ops.ts b/cloud/apps/acceptance/src/checks/ops.ts index f1f97ad7f4e..3e60aa8411e 100644 --- a/cloud/apps/acceptance/src/checks/ops.ts +++ b/cloud/apps/acceptance/src/checks/ops.ts @@ -6,7 +6,6 @@ import { log, type Check } from '../context' export const opsChecks: Record = { 'ops.kill-switch': async (ctx) => { - const p = await ctx.primary() let created = 0 let createWhileOff: number | null = null let resumeWhileOff: number | null = null @@ -14,6 +13,13 @@ export const opsChecks: Record = { 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) { @@ -25,10 +31,10 @@ export const opsChecks: Record = { created++ await control.as(ctx.solo, 'DELETE', `/v1/sandboxes/${c.body.sandbox.id}`) } - // The primary is running; resume of a running sandbox must still be refused while killed. - resumeWhileOff = (await control.as(ctx.solo, 'POST', `/v1/sandboxes/${p.sandbox.id}/resume`)).status + 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 @@ -84,12 +90,12 @@ export const opsChecks: Record = { const api = vercel() const created = await api.createSandbox({ name, - snapshotId: GOLDEN(), + snapshotId: await goldenSnapshot(), vcpus: 2, region: 'iad1', timeoutMs: 10 * 60_000, - ports: [], - networkPolicy: { allow: {} }, + ports: [6768], + networkPolicy: { allow: { 'registry.npmjs.org': [] } }, tags: { orcaEnv: 'dev', orcaSandbox: 'w9-orphan' }, snapshotExpirationMs: 24 * 60 * 60_000 }) @@ -133,6 +139,15 @@ export const opsChecks: Record = { } } -function GOLDEN(): string { - return process.env.ORCA_EVAL_GOLDEN_SNAPSHOT ?? 'snap_vpcmcCAgTtISiTZgsCosOlThQbFd' +/** Newest manual (golden-image) snapshot in the project; golden images self-rotate. */ +async function goldenSnapshot(): Promise { + 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 } diff --git a/cloud/apps/acceptance/src/report.ts b/cloud/apps/acceptance/src/report.ts index a0a1495dac2..09c391215f5 100644 --- a/cloud/apps/acceptance/src/report.ts +++ b/cloud/apps/acceptance/src/report.ts @@ -103,9 +103,9 @@ function ledgerTable(runs: RunFile[]): string { return `
${rows.length} resources across ${runs.length} runs
${rows.join('')}
RunKindIdLabelCreated (UTC)Destroyed
` } -/** Hand-curated findings/gaps (HTML snippets), kept beside the report: acceptance-findings.json {findings, gaps}. */ +/** Hand-curated findings/gaps (HTML snippets), versioned with the harness: findings.json {findings, gaps}. */ function loadFindings(): { gaps?: string[]; findings?: string[] } { - const path = `${config.contextDir}/acceptance-findings.json` + const path = new URL('../findings.json', import.meta.url).pathname return existsSync(path) ? (JSON.parse(readFileSync(path, 'utf8')) as { gaps?: string[]; findings?: string[] }) : {} } diff --git a/cloud/apps/acceptance/src/run.ts b/cloud/apps/acceptance/src/run.ts index bb7814e68d4..944723a4e49 100644 --- a/cloud/apps/acceptance/src/run.ts +++ b/cloud/apps/acceptance/src/run.ts @@ -3,6 +3,7 @@ // 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 { coreChecks } from './checks/core' import { opsChecks } from './checks/ops' @@ -23,7 +24,7 @@ const only = value('only')?.split(',') const skip = new Set(value('skip')?.split(',') ?? []) const n = Number(value('n') ?? 10) -const CHECKS: Record = { ...pendingChecks, ...coreChecks, ...secretChecks, ...opsChecks, ...billingChecks } +const CHECKS: Record = { ...pendingChecks, ...coreChecks, ...secretChecks, ...opsChecks, ...billingChecks, ...agentChecks } /** 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'] diff --git a/cloud/apps/acceptance/src/stripe.ts b/cloud/apps/acceptance/src/stripe.ts index 7cb53eaf5bf..3479a14cf19 100644 --- a/cloud/apps/acceptance/src/stripe.ts +++ b/cloud/apps/acceptance/src/stripe.ts @@ -97,11 +97,16 @@ export async function completeCheckout(url: string, card: CheckoutCard, shotPath let done = false while (!done && Date.now() < deadline) { for (const frame of page.frames()) { - const button = frame.locator('#test-source-authorize-3ds, button:has-text("Complete")') - if (await button.count().catch(() => 0)) { + 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') }) - await button.first().click().catch(() => {}) - done = true + done = await button + .click({ timeout: 5000 }) + .then(() => true) + .catch(() => false) break } } From d904c66d96eb679debf11f1a7698c80bdbe482b6 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 14:45:27 -0800 Subject: [PATCH 17/30] feat(cloud): acceptance seat proration and dunning checks (test clocks) --- cloud/apps/acceptance/src/checks/billing.ts | 104 ++++++++++++++++++-- cloud/apps/acceptance/src/env.ts | 9 ++ 2 files changed, 105 insertions(+), 8 deletions(-) diff --git a/cloud/apps/acceptance/src/checks/billing.ts b/cloud/apps/acceptance/src/checks/billing.ts index 90ad3ed7933..da2cea3c465 100644 --- a/cloud/apps/acceptance/src/checks/billing.ts +++ b/cloud/apps/acceptance/src/checks/billing.ts @@ -1,13 +1,36 @@ // 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 { + 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(read: () => Promise, done: (v: T) => boolean, timeoutMs = 90_000): Promise { @@ -125,15 +148,80 @@ export const billingChecks: Record = { } }, - 'billing.seat-proration': async () => ({ - status: 'not_landed', - detail: 'Seats follow Orca Cloud auth membership (POST /v1/internal/seats, MEMBERSHIP_SYNC_TOKEN); auth not deployed and the eval has no membership token yet — waiting on team-billing-ux' - }), + '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 === 'updated' && qtyAfterAdd === 3 && invoice?.billing_reason === 'subscription_update' && invoice?.status === 'paid' && + removed.body?.status === 'updated' && qtyAfterRemove === 2 && ent?.seats === 2 && same.body?.status === 'unchanged' + 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 -> ${qtyAfterRemove} seats (credit next invoice), entitlements ${ent?.seats} seats; repeat push -> ${same.body?.status}`, + evidence: [log('seat pushes', JSON.stringify({ added: added.body, removed: removed.body, same: same.body }))] + } + }, - 'billing.dunning': async () => ({ - status: 'not_landed', - detail: 'Needs CONTROL_PLANE_TOKEN + D1 account link for the test-clock flow (billing e2e-test-mode.ts covers it); eval wiring pending — waiting on team-billing-ux' - }), + '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' }), diff --git a/cloud/apps/acceptance/src/env.ts b/cloud/apps/acceptance/src/env.ts index 36861371beb..48b37763824 100644 --- a/cloud/apps/acceptance/src/env.ts +++ b/cloud/apps/acceptance/src/env.ts @@ -35,6 +35,10 @@ function readSecretJson(file: string): T | null { 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, @@ -44,6 +48,11 @@ export const secrets = { 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 } From f0192658c893c63e6ac66068b7e284b27b475345 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 14:54:53 -0800 Subject: [PATCH 18/30] feat(cloud): acceptance checks for W1 builds, W2 lifecycle, W3 shares/ssh/ports, W4 team, W5 API/MCP/routines, W8 devices --- cloud/apps/acceptance/src/checks/lifecycle.ts | 184 +++++++++ cloud/apps/acceptance/src/checks/platform.ts | 385 ++++++++++++++++++ cloud/apps/acceptance/src/clients.ts | 4 +- cloud/apps/acceptance/src/env.ts | 8 +- cloud/apps/acceptance/src/run.ts | 4 +- 5 files changed, 582 insertions(+), 3 deletions(-) create mode 100644 cloud/apps/acceptance/src/checks/lifecycle.ts create mode 100644 cloud/apps/acceptance/src/checks/platform.ts diff --git a/cloud/apps/acceptance/src/checks/lifecycle.ts b/cloud/apps/acceptance/src/checks/lifecycle.ts new file mode 100644 index 00000000000..238f88b6635 --- /dev/null +++ b/cloud/apps/acceptance/src/checks/lifecycle.ts @@ -0,0 +1,184 @@ +// 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). */ +async function serve(ctx: Ctx, sessionId: string, port: number) { + await ctx.exec(sessionId, `mkdir -p /tmp/www && echo "orca-share-ok" > /tmp/www/index.html && (cd /tmp/www && nohup python3 -m http.server ${port} >/tmp/www.log 2>&1 &) ; sleep 1; curl -s -m 5 localhost:${port}/`) +} + +export const lifecycleChecks: Record = { + '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.sessionId, 4173) + await openTerminal(b.code, b.root) + let found: any = null + for (let i = 0; i < 6 && !found; i++) { + const r = await orca(b.code, ['ports', '--all']) + 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', + 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.sessionId, 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 listening = (await ctx.exec(b.sessionId, `ss -ltn 2>/dev/null | grep -c ":${r.body?.ssh?.port ?? 2222} " ; grep -c w9-acceptance ~/.ssh/authorized_keys 2>/dev/null`)).stdout.trim().split('\n') + const ok = r.status === 200 && Number(listening[0]) > 0 && Number(listening[1]) > 0 + return { + status: ok ? 'partial' : '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=${listening[0]}, key installed=${listening[1]}. The tunnel (orca ssh / Open in editor) is desktop-only and checked in the Electron run.` + } + } +} diff --git a/cloud/apps/acceptance/src/checks/platform.ts b/cloud/apps/acceptance/src/checks/platform.ts new file mode 100644 index 00000000000..6f899f1184e --- /dev/null +++ b/cloud/apps/acceptance/src/checks/platform.ts @@ -0,0 +1,385 @@ +// 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 } from '../env' +import { log, type Check, type Ctx } from '../context' +import { orca } from '../orca-cli' +import 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 | null = null + +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) { + b = (await control.as(who, 'GET', `/v1/environments/${envId}/builds/${buildId}`)).body?.build + if (['succeeded', 'failed', 'canceled'].includes(b?.status)) { + return b + } + await sleep(10_000) + } + return b +} + +function environmentBuilds(ctx: Ctx): Promise { + 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): { 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 | 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) + +export const platformChecks: Record = { + '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?.activeBuild?.id === 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?.activeBuild?.id === s.first.id ? 'build 1' : env?.environment?.activeBuild?.id}` + } + }, + + '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: 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) => { + const r = await api(ctx, 'POST', '/v1/routines', { + name: `w9-hook-${tag(ctx)}`, + repo: 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: 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: 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.` + } + } +} diff --git a/cloud/apps/acceptance/src/clients.ts b/cloud/apps/acceptance/src/clients.ts index e9b4ba217c8..084245ee680 100644 --- a/cloud/apps/acceptance/src/clients.ts +++ b/cloud/apps/acceptance/src/clients.ts @@ -70,6 +70,8 @@ export type SandboxView = { 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 } } @@ -87,7 +89,7 @@ export class Sandboxes { async create( who: Identity, - body: { workspaceId: string; machineSize?: 'standard' | 'large'; repo?: { url: string; ref?: string } } + body: { workspaceId: string; machineSize?: 'standard' | 'large'; repo?: { url: string; ref?: string }; [extra: string]: unknown } ): Promise> { if (this.live.size >= config.maxConcurrentSandboxes) { throw new Error(`eval concurrency cap (${config.maxConcurrentSandboxes}) reached`) diff --git a/cloud/apps/acceptance/src/env.ts b/cloud/apps/acceptance/src/env.ts index 48b37763824..6c8ee086dc6 100644 --- a/cloud/apps/acceptance/src/env.ts +++ b/cloud/apps/acceptance/src/env.ts @@ -42,7 +42,13 @@ function readText(path: string): string | 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, - vercelToken: () => readSecretJson<{ bearerToken: string }>('tok.json')?.bearerToken ?? 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') diff --git a/cloud/apps/acceptance/src/run.ts b/cloud/apps/acceptance/src/run.ts index 944723a4e49..f5d8ee785b2 100644 --- a/cloud/apps/acceptance/src/run.ts +++ b/cloud/apps/acceptance/src/run.ts @@ -5,6 +5,8 @@ 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' @@ -24,7 +26,7 @@ const only = value('only')?.split(',') const skip = new Set(value('skip')?.split(',') ?? []) const n = Number(value('n') ?? 10) -const CHECKS: Record = { ...pendingChecks, ...coreChecks, ...secretChecks, ...opsChecks, ...billingChecks, ...agentChecks } +const CHECKS: Record = { ...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'] From 947813d4fca3edeee7d03aeb10b5e608342673cd Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:46:10 -0800 Subject: [PATCH 19/30] fix(cloud): acceptance check fixes (seat true-down, repo object, ssh probe, ports bound, kid from key file, load notes) --- cloud/apps/acceptance/desktop/run-desktop.mjs | 786 ++++++++++++++++++ cloud/apps/acceptance/desktop/sandbox-api.mjs | 72 ++ cloud/apps/acceptance/src/checks/billing.ts | 4 +- cloud/apps/acceptance/src/checks/core.ts | 6 +- cloud/apps/acceptance/src/checks/lifecycle.ts | 11 +- cloud/apps/acceptance/src/checks/platform.ts | 8 +- cloud/apps/acceptance/src/checks/secrets.ts | 12 +- cloud/apps/acceptance/src/inspect-build.ts | 12 + cloud/apps/acceptance/src/list-provider.ts | 8 + cloud/apps/acceptance/src/probe-build.ts | 49 ++ cloud/apps/acceptance/src/report.ts | 1 + cloud/apps/acceptance/src/run.ts | 17 + cloud/apps/acceptance/src/tokens.ts | 6 +- 13 files changed, 972 insertions(+), 20 deletions(-) create mode 100644 cloud/apps/acceptance/desktop/run-desktop.mjs create mode 100644 cloud/apps/acceptance/desktop/sandbox-api.mjs create mode 100644 cloud/apps/acceptance/src/inspect-build.ts create mode 100644 cloud/apps/acceptance/src/list-provider.ts create mode 100644 cloud/apps/acceptance/src/probe-build.ts diff --git a/cloud/apps/acceptance/desktop/run-desktop.mjs b/cloud/apps/acceptance/desktop/run-desktop.mjs new file mode 100644 index 00000000000..90aca0003e4 --- /dev/null +++ b/cloud/apps/acceptance/desktop/run-desktop.mjs @@ -0,0 +1,786 @@ +#!/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 +// +// 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, '') + +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: 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') + 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)) +} diff --git a/cloud/apps/acceptance/desktop/sandbox-api.mjs b/cloud/apps/acceptance/desktop/sandbox-api.mjs new file mode 100644 index 00000000000..2b0ecc2b607 --- /dev/null +++ b/cloud/apps/acceptance/desktop/sandbox-api.mjs @@ -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)) + } +} diff --git a/cloud/apps/acceptance/src/checks/billing.ts b/cloud/apps/acceptance/src/checks/billing.ts index da2cea3c465..ed4bddfc762 100644 --- a/cloud/apps/acceptance/src/checks/billing.ts +++ b/cloud/apps/acceptance/src/checks/billing.ts @@ -170,10 +170,10 @@ export const billingChecks: Record = { const same = await push(2) const ok = added.body?.status === 'updated' && qtyAfterAdd === 3 && invoice?.billing_reason === 'subscription_update' && invoice?.status === 'paid' && - removed.body?.status === 'updated' && qtyAfterRemove === 2 && ent?.seats === 2 && same.body?.status === 'unchanged' + 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 -> ${qtyAfterRemove} seats (credit next invoice), entitlements ${ent?.seats} seats; repeat push -> ${same.body?.status}`, + 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 }))] } }, diff --git a/cloud/apps/acceptance/src/checks/core.ts b/cloud/apps/acceptance/src/checks/core.ts index 929a89860be..76d41ea0f12 100644 --- a/cloud/apps/acceptance/src/checks/core.ts +++ b/cloud/apps/acceptance/src/checks/core.ts @@ -66,15 +66,15 @@ export const coreChecks: Record = { 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) >= 30 ? 'partial' : 'fail', - detail: `disk ${size} GB (${avail} GB free). Conductor: 64 GB. Low-disk alert not yet probed (W1/W2).`, + 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', 'chromium', 'google-chrome'] + 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)) diff --git a/cloud/apps/acceptance/src/checks/lifecycle.ts b/cloud/apps/acceptance/src/checks/lifecycle.ts index 238f88b6635..912aaf6d1bc 100644 --- a/cloud/apps/acceptance/src/checks/lifecycle.ts +++ b/cloud/apps/acceptance/src/checks/lifecycle.ts @@ -116,8 +116,10 @@ export const lifecycleChecks: Record = { await serve(ctx, b.sessionId, 4173) await openTerminal(b.code, b.root) let found: any = null - for (let i = 0; i < 6 && !found; i++) { - const r = await orca(b.code, ['ports', '--all']) + let last = '' + for (let i = 0; i < 3 && !found; i++) { + const r = await orca(b.code, ['ports', '--all'], 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) @@ -125,7 +127,7 @@ export const lifecycleChecks: Record = { } 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', + 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))] : [] } }, @@ -174,7 +176,8 @@ export const lifecycleChecks: Record = { 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 listening = (await ctx.exec(b.sessionId, `ss -ltn 2>/dev/null | grep -c ":${r.body?.ssh?.port ?? 2222} " ; grep -c w9-acceptance ~/.ssh/authorized_keys 2>/dev/null`)).stdout.trim().split('\n') + 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} "; sudo sh -c 'cat /home/*/.ssh/authorized_keys /root/.ssh/authorized_keys 2>/dev/null' | grep -c w9-acceptance`)).stdout.trim().split('\n') const ok = r.status === 200 && Number(listening[0]) > 0 && Number(listening[1]) > 0 return { status: ok ? 'partial' : 'fail', diff --git a/cloud/apps/acceptance/src/checks/platform.ts b/cloud/apps/acceptance/src/checks/platform.ts index 6f899f1184e..3e291f6997b 100644 --- a/cloud/apps/acceptance/src/checks/platform.ts +++ b/cloud/apps/acceptance/src/checks/platform.ts @@ -251,7 +251,7 @@ export const platformChecks: Record = { // ---------------- W5 ---------------- 'api.rest': async (ctx) => { - const created = await api(ctx, 'POST', '/v1/workspaces', { name: `w9-api-${tag(ctx)}`, repo: config.testRepo }) + 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)}` } @@ -295,7 +295,7 @@ export const platformChecks: Record = { 'api.routine-webhook': async (ctx) => { const r = await api(ctx, 'POST', '/v1/routines', { name: `w9-hook-${tag(ctx)}`, - repo: config.testRepo, + repo: { url: config.testRepo }, trigger: { kind: 'webhook' }, agent: 'claude', prompt: 'Reply with the single word ok. Do not change any files.', @@ -334,7 +334,7 @@ export const platformChecks: Record = { 'api.routine-cron': async (ctx) => { const r = await api(ctx, 'POST', '/v1/routines', { name: `w9-cron-${tag(ctx)}`, - repo: config.testRepo, + repo: { url: config.testRepo }, trigger: { kind: 'schedule', cron: '0 9 * * 1-5', timezone: 'America/Los_Angeles' }, agent: 'claude', prompt: 'Reply ok.', @@ -345,7 +345,7 @@ export const platformChecks: Record = { const id = r.body?.routine?.id const gh = await api(ctx, 'POST', '/v1/routines', { name: `w9-gh-${tag(ctx)}`, - repo: config.testRepo, + repo: { url: config.testRepo }, trigger: { kind: 'github', events: ['pull_request.opened'], labels: ['orca'] }, agent: 'codex', prompt: 'Review.', diff --git a/cloud/apps/acceptance/src/checks/secrets.ts b/cloud/apps/acceptance/src/checks/secrets.ts index b7c932e8f80..9fed85fd724 100644 --- a/cloud/apps/acceptance/src/checks/secrets.ts +++ b/cloud/apps/acceptance/src/checks/secrets.ts @@ -23,9 +23,9 @@ function teamSandbox(ctx: Ctx) { 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', 'OPENAI_API_KEY', `org-fake-${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', 'OPENAI_API_KEY', `personal-fake-${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)}`) @@ -62,9 +62,9 @@ export const secretChecks: Record = { 'sec.precedence': async (ctx) => { const tag = ctx.run.file.runId const box = await teamSandbox(ctx) - const env = await orcadEnv(ctx, box.sessionId, ['W9_OVERRIDE', 'OPENAI_API_KEY']) + const env = await orcadEnv(ctx, box.sessionId, ['W9_OVERRIDE', 'AWS_REGION']) const personalWinsEnv = env.W9_OVERRIDE === `personal-${tag}` - const orgWinsAgentKey = env.OPENAI_API_KEY === `org-fake-${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}` @@ -84,9 +84,9 @@ export const secretChecks: Record = { for (const [scope, who, name] of [ ['org', ctx.owner, 'W9_SHARED'], ['org', ctx.owner, 'W9_OVERRIDE'], - ['org', ctx.owner, 'OPENAI_API_KEY'], + ['org', ctx.owner, 'AWS_REGION'], ['personal', ctx.member, 'W9_OVERRIDE'], - ['personal', ctx.member, 'OPENAI_API_KEY'] + ['personal', ctx.member, 'AWS_REGION'] ] as const) { await control.as(who, 'DELETE', `/v1/secrets/${scope}/${name}`) } diff --git a/cloud/apps/acceptance/src/inspect-build.ts b/cloud/apps/acceptance/src/inspect-build.ts new file mode 100644 index 00000000000..a3a038c9ffd --- /dev/null +++ b/cloud/apps/acceptance/src/inspect-build.ts @@ -0,0 +1,12 @@ +// Debug helper: prints an environment build (as the team owner) without secrets. +// tsx src/inspect-build.ts +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)) +} diff --git a/cloud/apps/acceptance/src/list-provider.ts b/cloud/apps/acceptance/src/list-provider.ts new file mode 100644 index 00000000000..cc1cb2ce498 --- /dev/null +++ b/cloud/apps/acceptance/src/list-provider.ts @@ -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)}`) +} diff --git a/cloud/apps/acceptance/src/probe-build.ts b/cloud/apps/acceptance/src/probe-build.ts new file mode 100644 index 00000000000..4bf1a79270e --- /dev/null +++ b/cloud/apps/acceptance/src/probe-build.ts @@ -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) +} diff --git a/cloud/apps/acceptance/src/report.ts b/cloud/apps/acceptance/src/report.ts index 09c391215f5..d1b84778355 100644 --- a/cloud/apps/acceptance/src/report.ts +++ b/cloud/apps/acceptance/src/report.ts @@ -162,6 +162,7 @@ Parity bar: conductor-parity-matrix.html< create.usable adds the user's client connecting (orca CLI, E2EE handshake); *.firstEcho 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); rollover.gap = drain + stop + boot + orcad ready.

${benchTable(runs)} +

${runs.filter((r) => r.benchmarks.length).slice(-1).flatMap((r) => r.notes.filter((n) => n.startsWith('load'))).map(esc).join(' · ')}

Known gaps and findings

    ${[...(extra.findings ?? []), ...(extra.gaps ?? [])].map((g) => `
  • ${g}
  • `).join('') || '
  • none recorded
  • '}

Cost of the eval and every cloud resource

diff --git a/cloud/apps/acceptance/src/run.ts b/cloud/apps/acceptance/src/run.ts index f5d8ee785b2..f0fbde0a72f 100644 --- a/cloud/apps/acceptance/src/run.ts +++ b/cloud/apps/acceptance/src/run.ts @@ -12,6 +12,7 @@ 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' @@ -48,7 +49,21 @@ process.on('SIGINT', () => { 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 { + 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)) @@ -63,8 +78,10 @@ try { } } 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() diff --git a/cloud/apps/acceptance/src/tokens.ts b/cloud/apps/acceptance/src/tokens.ts index bfcac2b4aba..25d0ca20351 100644 --- a/cloud/apps/acceptance/src/tokens.ts +++ b/cloud/apps/acceptance/src/tokens.ts @@ -14,10 +14,14 @@ function signingKey() { 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, issuer: string, audience: string): Promise { return new SignJWT(claims) - .setProtectedHeader({ alg: 'ES256', kid: 'dev1' }) + .setProtectedHeader({ alg: 'ES256', kid: kid() }) .setIssuer(issuer) .setAudience(audience) .setIssuedAt() From 86390320c8727b2eca68579c639cd76055753be2 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 15:53:02 -0800 Subject: [PATCH 20/30] feat(cloud): desktop Electron acceptance run (create, keystroke, ports, ssh, sleep/wake, transfer, destroy) --- cloud/apps/acceptance/desktop/run-desktop.mjs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cloud/apps/acceptance/desktop/run-desktop.mjs b/cloud/apps/acceptance/desktop/run-desktop.mjs index 90aca0003e4..204a4eb1882 100644 --- a/cloud/apps/acceptance/desktop/run-desktop.mjs +++ b/cloud/apps/acceptance/desktop/run-desktop.mjs @@ -532,6 +532,10 @@ async function main() { 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 From be86278bb513aecf953806b3dcb86c4064c756f3 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:16:29 -0800 Subject: [PATCH 21/30] feat(cloud): acceptance desktop import, build event-stream wait, routine creds, key revocation, summary --- cloud/apps/acceptance/findings.json | 19 ++--- cloud/apps/acceptance/src/bench.ts | 8 ++- cloud/apps/acceptance/src/checks/billing.ts | 2 +- cloud/apps/acceptance/src/checks/lifecycle.ts | 31 +++++--- cloud/apps/acceptance/src/checks/ops.ts | 7 ++ cloud/apps/acceptance/src/checks/platform.ts | 61 ++++++++++++++-- cloud/apps/acceptance/src/import-desktop.ts | 70 +++++++++++++++++++ .../acceptance/src/probe-resume-running.ts | 16 +++++ cloud/apps/acceptance/src/report.ts | 2 +- cloud/apps/acceptance/src/summary.ts | 35 ++++++++++ cloud/apps/acceptance/src/sweep.ts | 11 ++- 11 files changed, 233 insertions(+), 29 deletions(-) create mode 100644 cloud/apps/acceptance/src/import-desktop.ts create mode 100644 cloud/apps/acceptance/src/probe-resume-running.ts create mode 100644 cloud/apps/acceptance/src/summary.ts diff --git a/cloud/apps/acceptance/findings.json b/cloud/apps/acceptance/findings.json index 818894118f7..afce6a89103 100644 --- a/cloud/apps/acceptance/findings.json +++ b/cloud/apps/acceptance/findings.json @@ -1,15 +1,18 @@ { "findings": [ - "F1 (bug, core/vercel-driver, filed 2026-11-09): POST /v1/sandboxes/:id/resume on a sandbox that is already running hangs ~60 s and answers 502 provider_unavailable / orcad_start_failed (2/2 repro, VM stays running). Wake-on-open after a desktop restart hits this path. Expected: idempotent, return the live pairing in <1 s.", - "F2 (UX, core): creating with repo for an account with no GitHub App binding fails as 502 orcad_start_failed after 3.8 s instead of a clear github_not_connected the desktop can turn into a Connect GitHub prompt.", - "F3 (blocker for agent.real-task, W6): ai-gateway.vercel.sh is not on the sandbox egress allowlist (DNS fails, curl rc 6), so Claude Code/Codex via the AI Gateway key cannot run in a cloud VM. Secret injection itself works (ANTHROPIC_* reach orcad's env).", - "F4 (parity gap, golden image): the login shell lacks the orca CLI (Conductor ships its CLI in the VM, P0), rg, tmux, vim, git-lfs and Chrome/Chromium. Present: node 24, npm, pnpm, python3, git, gh, jq, Claude Code 2.1.319, Codex 0.166.0.", - "F5 (parity gap): standard disk is 32 GB (Conductor 64 GB since 0.82); no low-disk alert yet.", + "W1-1 (bug, w1-env-builds, open 2026-11-09): an environment build only advances while a client streams GET …/builds/:id/events. Polling GET …/builds/:id every 10 s (documented as also advancing the build) left the build in running 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.", + "D-1 (bug, core desktop, high): starting an Orca Cloud create before signing in, then signing in from the card, created a sandbox owned by the fallback dev user (desktop-dogfood), not the signed-in user. Keep-alive, Copy SSH Command and Delete then got not_found. Likely cause: getHostedSandboxConfig() caches the token for the app process and does not reset it on sign-in (src/main/hosted-sandbox/hosted-sandbox-config.ts).", + "D-2 (bug, core desktop, high): Delete Workspace hides the row and reports success when the control plane answers not_found, while the VM keeps running (a billing leak).", + "D-3 (bug, core desktop): after a cloud create, the checkout at /vercel/sandbox/project is added as a new project named \"project\" instead of being grouped under orca-sandbox-push-test.", + "D-4 (minor, desktop): 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.", + "W6-1 (check, w6-agents-secrets): sec.precedence on OPENAI_API_KEY no longer saw the org value in orcad's env, likely because egress placeholders now replace provider keys. The check moved to AWS_REGION (also an agent-key name); W6 to confirm that org beats personal is still enforced at the egress swap.", + "Fixed since the last report: 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.", "Note: 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 pending an AI Gateway credit top-up (free credits ~$4.94, no anthropic/* models); the agent check uses moonshotai/kimi-k2.", - "Seat proration and the dunning flow (grace -> suspend -> pay -> resume) need MEMBERSHIP_SYNC_TOKEN / CONTROL_PLANE_TOKEN wiring for the harness (billing e2e-test-mode.ts covers them in isolation).", - "Team create/invite need the Orca Cloud auth endpoints deployed (merged, not deployed)." + "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)." ] } diff --git a/cloud/apps/acceptance/src/bench.ts b/cloud/apps/acceptance/src/bench.ts index a5b70dfbc27..a88810e4bda 100644 --- a/cloud/apps/acceptance/src/bench.ts +++ b/cloud/apps/acceptance/src/bench.ts @@ -15,7 +15,8 @@ export const EARLIER: Record = { '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' + '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 = { 'create.api': '0.80: "1.5-3x faster sandbox start" (no absolute number published)', @@ -24,7 +25,10 @@ export const CONDUCTOR: Record = { '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' + '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') { diff --git a/cloud/apps/acceptance/src/checks/billing.ts b/cloud/apps/acceptance/src/checks/billing.ts index ed4bddfc762..bb4268f7fcc 100644 --- a/cloud/apps/acceptance/src/checks/billing.ts +++ b/cloud/apps/acceptance/src/checks/billing.ts @@ -169,7 +169,7 @@ export const billingChecks: Record = { const ent = await entitlement(r.who) const same = await push(2) const ok = - added.body?.status === 'updated' && qtyAfterAdd === 3 && invoice?.billing_reason === 'subscription_update' && invoice?.status === 'paid' && + 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', diff --git a/cloud/apps/acceptance/src/checks/lifecycle.ts b/cloud/apps/acceptance/src/checks/lifecycle.ts index 912aaf6d1bc..ac6911eebe7 100644 --- a/cloud/apps/acceptance/src/checks/lifecycle.ts +++ b/cloud/apps/acceptance/src/checks/lifecycle.ts @@ -31,8 +31,20 @@ async function refresh(ctx: Ctx): Promise<{ id: string; sessionId: string; code: 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). */ -async function serve(ctx: Ctx, sessionId: string, port: number) { - await ctx.exec(sessionId, `mkdir -p /tmp/www && echo "orca-share-ok" > /tmp/www/index.html && (cd /tmp/www && nohup python3 -m http.server ${port} >/tmp/www.log 2>&1 &) ; sleep 1; curl -s -m 5 localhost:${port}/`) +/** 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 = { @@ -113,12 +125,11 @@ export const lifecycleChecks: Record = { 'acc.ports': async (ctx) => { const b = await refresh(ctx) - await serve(ctx, b.sessionId, 4173) - await openTerminal(b.code, b.root) + 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', '--all'], 30_000) + 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) { @@ -134,7 +145,7 @@ export const lifecycleChecks: Record = { 'acc.share-link': async (ctx) => { const b = await refresh(ctx) - await serve(ctx, b.sessionId, 4173) + await serve(ctx, b, 4173) const samples: number[] = [] const created: string[] = [] let firstBody = '' @@ -177,11 +188,11 @@ export const lifecycleChecks: Record = { 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} "; sudo sh -c 'cat /home/*/.ssh/authorized_keys /root/.ssh/authorized_keys 2>/dev/null' | grep -c w9-acceptance`)).stdout.trim().split('\n') - const ok = r.status === 200 && Number(listening[0]) > 0 && Number(listening[1]) > 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 ? 'partial' : '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=${listening[0]}, key installed=${listening[1]}. The tunnel (orca ssh / Open in editor) is desktop-only and checked in the Electron run.` + 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).` } } } diff --git a/cloud/apps/acceptance/src/checks/ops.ts b/cloud/apps/acceptance/src/checks/ops.ts index 3e60aa8411e..8813e8b3f3a 100644 --- a/cloud/apps/acceptance/src/checks/ops.ts +++ b/cloud/apps/acceptance/src/checks/ops.ts @@ -124,6 +124,13 @@ export const opsChecks: Record = { '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}`) diff --git a/cloud/apps/acceptance/src/checks/platform.ts b/cloud/apps/acceptance/src/checks/platform.ts index 3e291f6997b..f687900f3d4 100644 --- a/cloud/apps/acceptance/src/checks/platform.ts +++ b/cloud/apps/acceptance/src/checks/platform.ts @@ -2,10 +2,10 @@ import { createPrivateKey, createPublicKey, diffieHellman, generateKeyPairSync, hkdfSync, createDecipheriv } from 'node:crypto' import { SignJWT } from 'jose' import { control } from '../clients' -import { config } from '../env' +import { config, secrets } from '../env' import { log, type Check, type Ctx } from '../context' import { orca } from '../orca-cli' -import type { Identity } from '../tokens' +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) @@ -14,15 +14,44 @@ const tag = (ctx: Ctx) => ctx.run.file.runId.replace(/[^0-9]/g, '').slice(-10) type BuildState = { envId: string; first: any; second: any; error?: string } let builds: Promise | 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) { - b = (await control.as(who, 'GET', `/v1/environments/${envId}/builds/${buildId}`)).body?.build + 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) { + 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(10_000) + await sleep(2000) } return b } @@ -108,6 +137,8 @@ function key(ctx: Ctx) { } 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 = { 'env.build': async (ctx) => { const s = await environmentBuilds(ctx) @@ -130,10 +161,10 @@ export const platformChecks: Record = { 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?.activeBuild?.id === s.first.id + 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?.activeBuild?.id === s.first.id ? 'build 1' : env?.environment?.activeBuild?.id}` + 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}` } }, @@ -293,6 +324,24 @@ export const platformChecks: Record = { }), '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 = 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 }, diff --git a/cloud/apps/acceptance/src/import-desktop.ts b/cloud/apps/acceptance/src/import-desktop.ts new file mode 100644 index 00000000000..4a1a08ae92c --- /dev/null +++ b/cloud/apps/acceptance/src/import-desktop.ts @@ -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 +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 = { + '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 = {} + 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`) diff --git a/cloud/apps/acceptance/src/probe-resume-running.ts b/cloud/apps/acceptance/src/probe-resume-running.ts new file mode 100644 index 00000000000..b88342bbd1d --- /dev/null +++ b/cloud/apps/acceptance/src/probe-resume-running.ts @@ -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) +} diff --git a/cloud/apps/acceptance/src/report.ts b/cloud/apps/acceptance/src/report.ts index d1b84778355..c245490a934 100644 --- a/cloud/apps/acceptance/src/report.ts +++ b/cloud/apps/acceptance/src/report.ts @@ -82,7 +82,7 @@ function benchTable(runs: RunFile[]): string { } } } - const names = ['create.api', 'create.usable', 'create.firstEcho', 'wake.api', 'wake.firstEcho', 'suspend.api', 'rollover.gap', 'port.forward', 'share.coldLoad'] + 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 diff --git a/cloud/apps/acceptance/src/summary.ts b/cloud/apps/acceptance/src/summary.ts new file mode 100644 index 00000000000..45a656b0672 --- /dev/null +++ b/cloud/apps/acceptance/src/summary.ts @@ -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() +const bench = new Map() +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}`) + } +} diff --git a/cloud/apps/acceptance/src/sweep.ts b/cloud/apps/acceptance/src/sweep.ts index 145da2e795b..47e631b20c2 100644 --- a/cloud/apps/acceptance/src/sweep.ts +++ b/cloud/apps/acceptance/src/sweep.ts @@ -3,7 +3,7 @@ // tsx src/sweep.ts [--dry-run] import { writeFileSync } from 'node:fs' import { join } from 'node:path' -import { vercel } from './clients' +import { control, vercel } from './clients' import { config } from './env' import { loadRuns } from './run-state' import { stripe } from './stripe' @@ -27,6 +27,15 @@ for (const run of loadRuns()) { 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) { From 36ef6c1f650436b543f65d13efeb3074978a20ad Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:17:06 -0800 Subject: [PATCH 22/30] test(cloud): acceptance harness unit tests --- cloud/apps/acceptance/test/run-state.test.ts | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 cloud/apps/acceptance/test/run-state.test.ts diff --git a/cloud/apps/acceptance/test/run-state.test.ts b/cloud/apps/acceptance/test/run-state.test.ts new file mode 100644 index 00000000000..dd451a38b5c --- /dev/null +++ b/cloud/apps/acceptance/test/run-state.test.ts @@ -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) + }) +}) From 5f4e84fd6de14db08e51b6d383302fdcafe2c57e Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:19:02 -0800 Subject: [PATCH 23/30] test(sandbox-control): egress swap and plain injection resolve keys with the same precedence (team > repository > personal for agent keys) --- cloud/apps/sandbox-control/src/secrets.ts | 6 +- .../test/agent-providers.test.ts | 68 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/cloud/apps/sandbox-control/src/secrets.ts b/cloud/apps/sandbox-control/src/secrets.ts index 2bc6f968168..3b6b15b8338 100644 --- a/cloud/apps/sandbox-control/src/secrets.ts +++ b/cloud/apps/sandbox-control/src/secrets.ts @@ -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> = { env: { repo: 3, personal: 2, org: 1 }, agent_key: { org: 3, repo: 2, personal: 1 } diff --git a/cloud/apps/sandbox-control/test/agent-providers.test.ts b/cloud/apps/sandbox-control/test/agent-providers.test.ts index f1f17d7df9f..422ad721ba3 100644 --- a/cloud/apps/sandbox-control/test/agent-providers.test.ts +++ b/cloud/apps/sandbox-control/test/agent-providers.test.ts @@ -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) + } + }) +}) From 721d726c4a29a87d419d83a1f55a1db5f67fb875 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:26:37 -0800 Subject: [PATCH 24/30] chore(cloud): register the acceptance harness test runner --- cloud/apps/acceptance/vitest.config.ts | 7 +++++++ .../scripts/ci/unit-selection/check-test-file-runners.mjs | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 cloud/apps/acceptance/vitest.config.ts diff --git a/cloud/apps/acceptance/vitest.config.ts b/cloud/apps/acceptance/vitest.config.ts new file mode 100644 index 00000000000..9f469145c99 --- /dev/null +++ b/cloud/apps/acceptance/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + // A string tsconfig skips the lookup, which otherwise climbs to the desktop app's tsconfig. + oxc: { tsconfig: false }, + test: { include: ['test/**/*.test.ts'] } +}) diff --git a/config/scripts/ci/unit-selection/check-test-file-runners.mjs b/config/scripts/ci/unit-selection/check-test-file-runners.mjs index db9bfa6de79..b02e52ae92c 100644 --- a/config/scripts/ci/unit-selection/check-test-file-runners.mjs +++ b/config/scripts/ci/unit-selection/check-test-file-runners.mjs @@ -64,6 +64,12 @@ const NON_UNIT_TEST_RUNNERS = [ sources: ['cloud/apps/sandbox-control/vitest.config.ts'], mirrors: ["include: ['test/**/*.test.ts', 'image/**/*.test.ts']"] }, + { + name: 'cloud 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'], From e95a1478378f7553868bc1e2fb1abea9000fcbb2 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:26:55 -0800 Subject: [PATCH 25/30] fix(cloud): env builds progress server-side on a Durable Object alarm, not only while streamed (#118) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sandbox-control): read the orgRole claim that Orca Cloud auth mints (#29) Auth mints the live org role as orgRole on sandbox tokens (same claim as billing tokens, stablyai/orca-cloud#695); the verifier read org_role, so admins would never have been recognized. * wip(cloud): environment builds control plane * wip(cloud): environment build boot path, staleness, routes * wip(cloud): env build tests, anonymous staleness fallback * wip(cloud): migration 0008 (claimed), quieter recovered clone errors * fix(cloud): env build review: interrupted-snapshot recovery, bounded log reads, secret redaction, activation guard, hook/prelude fixes * fix(cloud): wait briefly for the build exit code after its log ends; print the base image and package manager * wip(desktop): org environments client, IPC, CLI, settings UI, orcad disk usage + low-disk banner * perf(cloud): smoke-boot each environment build before activating it; skip the redundant prelude fetch * fix(cloud): a GitHub token error fails the build as github_unavailable instead of cloning anonymously * perf(cloud): warm pool VMs per hot environment build, so a create from an org environment skips the provider boot * wip(desktop): cloud environment UI round-3 fixes * fix(cloud): pool-per-build review: smoke race safety, provider hiccups skip smoke, usage-based hot builds with caps, bounded boots, index * fix(desktop): Configure with an agent hands the prompt over at launch (trust dialog swallowed the pasted Enter); CLI builds table widths * polish(desktop): W10 P2s — InlineError action slot, shared focus-ring, copy * feat(cloud): build event stream heartbeat every 20 s * fix(desktop): FE review — stable virtualizer, shadow-buffer replays, heartbeat-aware build watch, CLI follower restarts, repo import path checks, poll races * feat(cloud): meter environment build sandbox time to the member who started it; no setup-script nudge for workspaces booted from an env build * fix(cloud): environment builds progress server-side on a Durable Object alarm, not only while someone streams their log --- cloud/apps/sandbox-control/src/env.ts | 2 + .../sandbox-control/src/environment-builds.ts | 49 ++++++++++- cloud/apps/sandbox-control/src/index.ts | 88 +++++++++++++++++-- .../sandbox-control/test/environments.test.ts | 40 +++++++++ cloud/apps/sandbox-control/wrangler.dev.jsonc | 3 + cloud/apps/sandbox-control/wrangler.jsonc | 3 + 6 files changed, 177 insertions(+), 8 deletions(-) diff --git a/cloud/apps/sandbox-control/src/env.ts b/cloud/apps/sandbox-control/src/env.ts index 690bddcc30e..4d985b4def6 100644 --- a/cloud/apps/sandbox-control/src/env.ts +++ b/cloud/apps/sandbox-control/src/env.ts @@ -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 diff --git a/cloud/apps/sandbox-control/src/environment-builds.ts b/cloud/apps/sandbox-control/src/environment-builds.ts index 9e993a9016f..00c5e9019a0 100644 --- a/cloud/apps/sandbox-control/src/environment-builds.ts +++ b/cloud/apps/sandbox-control/src/environment-builds.ts @@ -107,6 +107,11 @@ export type BuildDeps = { githubFetch?: typeof fetch /** Keeps finalization running after a watcher disconnects (Worker ctx.waitUntil). */ waitUntil?: (promise: Promise) => 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 config: { environment: string; goldenSnapshotId: string; region: string; guardrails?: GuardrailConfig } } @@ -250,7 +255,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 = @@ -740,6 +750,43 @@ export async function reportBuildUsage(deps: Pick): 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 { + 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 { const { results } = await deps.db diff --git a/cloud/apps/sandbox-control/src/index.ts b/cloud/apps/sandbox-control/src/index.ts index 3dde83b32f9..643718cdf6c 100644 --- a/cloud/apps/sandbox-control/src/index.ts +++ b/cloud/apps/sandbox-control/src/index.ts @@ -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 + /** Build runner override (tests tick builds by hand). */ + scheduleBuild?: BuildDeps['schedule'] /** ctx.waitUntil from the Worker entry; tests collect the promises. */ waitUntil?: (work: Promise) => void /** Route groups mounted on the public API (routines, see src/routines). */ @@ -523,8 +525,22 @@ async function lifecycleDeps(env: Env, deps: Deps): Promise { + 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'], @@ -978,7 +994,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)()) @@ -1393,6 +1409,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 } @@ -1402,10 +1435,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 })) @@ -1430,3 +1459,48 @@ export default { ) } } satisfies ExportedHandler + +/** + * 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 { + 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 { + 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) + } +} diff --git a/cloud/apps/sandbox-control/test/environments.test.ts b/cloud/apps/sandbox-control/test/environments.test.ts index a1b6c5beac7..2fa3826444d 100644 --- a/cloud/apps/sandbox-control/test/environments.test.ts +++ b/cloud/apps/sandbox-control/test/environments.test.ts @@ -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`) diff --git a/cloud/apps/sandbox-control/wrangler.dev.jsonc b/cloud/apps/sandbox-control/wrangler.dev.jsonc index 1a373b1f18d..650b04dd9df 100644 --- a/cloud/apps/sandbox-control/wrangler.dev.jsonc +++ b/cloud/apps/sandbox-control/wrangler.dev.jsonc @@ -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", diff --git a/cloud/apps/sandbox-control/wrangler.jsonc b/cloud/apps/sandbox-control/wrangler.jsonc index 677c810b07c..1958c91974d 100644 --- a/cloud/apps/sandbox-control/wrangler.jsonc +++ b/cloud/apps/sandbox-control/wrangler.jsonc @@ -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", From 7c9abb57f1088d2d86a4903044c8ff6ce0bbb7cd Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 16:30:34 -0800 Subject: [PATCH 26/30] copy(cloud): state the running-sandbox cap where it's enforced (limit cards, workspace start error) and in the plans fine print --- .../components/billing/plans/PlansView.tsx | 7 ++- .../cloud-workspace-errors.ts | 21 ++++++-- .../worktree-creation/VmBuildFailureCard.tsx | 14 +++++- .../vm-build-failure-copy.ts | 50 +++++++++++++++---- .../vm-build-failure-toast.ts | 9 +++- .../vm-build-failure.test.ts | 34 ++++++++++++- src/renderer/src/i18n/locales/en.json | 6 ++- 7 files changed, 121 insertions(+), 20 deletions(-) diff --git a/src/renderer/src/components/billing/plans/PlansView.tsx b/src/renderer/src/components/billing/plans/PlansView.tsx index dee2a120e06..b92fd2d40e2 100644 --- a/src/renderer/src/components/billing/plans/PlansView.tsx +++ b/src/renderer/src/components/billing/plans/PlansView.tsx @@ -306,8 +306,11 @@ export function PlansView(props: PlansViewProps): React.JSX.Element {

{t( - 'auto.components.billing.plans.footHours3', - 'Hours reset every month, including on annual plans.' + 'auto.components.billing.plans.footHours4', + 'Hours reset every month, including on annual plans. Each seat can run {{value0}} sandboxes at once.', + // The cap is enforced by the control plane; the fine print states it before anyone + // reaches the limit card, while the Team card keeps to the marketing bullets. + { value0: String(offers.team.concurrentSandboxesPerSeat) } )}

diff --git a/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts b/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts index 8af2d1638aa..46394567b9e 100644 --- a/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts +++ b/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts @@ -1,6 +1,8 @@ // Copy for control-plane error codes from the team workspace registry. import { translate } from '@/i18n/i18n' import type { Translate } from '@/i18n/use-translate' +import { getLastBillingStatus } from '@/components/billing/billing-summary-store' +import { sandboxLimitFromBilling } from '@/components/worktree-creation/vm-build-failure-copy' export function describeError(error: string, reason?: string, t: Translate = translate): string { if (error === 'not_found') { @@ -10,10 +12,23 @@ export function describeError(error: string, reason?: string, t: Translate = tra ) } if (error === 'entitlement_denied') { - return reason === 'concurrency_limit' + if (reason === 'concurrency_limit') { + const limit = sandboxLimitFromBilling(getLastBillingStatus()) + return limit + ? t( + 'auto.components.cloudWorkspaces.errorConcurrencyLimitCount', + "You're at your team's limit: your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.", + { value0: String(limit.team), value1: String(limit.perSeat) } + ) + : t( + 'auto.components.cloudWorkspaces.errorConcurrencyLimit', + "You're at your team's limit of running sandboxes. Put one to sleep or delete it, then try again." + ) + } + return reason === 'member_concurrency_limit' ? t( - 'auto.components.cloudWorkspaces.errorConcurrencyLimit', - "You're at your team's limit of running sandboxes. Put one to sleep or delete it, then try again." + 'auto.components.cloudWorkspaces.errorMemberConcurrencyLimit', + "You're at your limit of running sandboxes. Put one to sleep or delete it, then try again." ) : t( 'auto.components.cloudWorkspaces.errorEntitlementDenied', diff --git a/src/renderer/src/components/worktree-creation/VmBuildFailureCard.tsx b/src/renderer/src/components/worktree-creation/VmBuildFailureCard.tsx index 3b34cc9768a..fd5245ced1b 100644 --- a/src/renderer/src/components/worktree-creation/VmBuildFailureCard.tsx +++ b/src/renderer/src/components/worktree-creation/VmBuildFailureCard.tsx @@ -3,7 +3,8 @@ import { cn } from '@/lib/utils' import { useTranslate } from '@/i18n/use-translate' import { VmBuildReveal } from './VmBuildReveal' import { isAccountGate, type VmBuildFailure } from './vm-build-failure' -import { vmBuildFailureCopy } from './vm-build-failure-copy' +import { getLastBillingStatus } from '@/components/billing/billing-summary-store' +import { sandboxLimitFromBilling, vmBuildFailureCopy } from './vm-build-failure-copy' import { FailureActionRow, type VmBuildFailureActions } from './VmBuildFailureActions' import { CheckRow } from './VmBuildCheckRow' @@ -54,7 +55,16 @@ export function VmBuildFailureCard({ } const visible = failure ?? shown const copy = visible - ? vmBuildFailureCopy(visible, { cloud, canManageBilling, repoName }, t) + ? vmBuildFailureCopy( + visible, + { + cloud, + canManageBilling, + repoName, + sandboxLimit: sandboxLimitFromBilling(getLastBillingStatus()) + }, + t + ) : null const headingRef = React.useRef(null) const cardRef = React.useRef(null) diff --git a/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts b/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts index 35f65e34928..f9ecc31b8a2 100644 --- a/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts +++ b/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts @@ -1,18 +1,38 @@ import type { Translate } from '@/i18n/use-translate' +import type { OrcaBillingStatusResult } from '../../../../shared/orca-billing' import type { VmBuildFailure } from './vm-build-failure' +/** The running-sandbox caps billing enforces: per member, and per team (per seat × seats). */ +export type SandboxLimit = { perSeat: number; team: number } + +/** Mirrors billing's checkStart limits; null until the plan has loaded. */ +export function sandboxLimitFromBilling( + status: OrcaBillingStatusResult | null +): SandboxLimit | null { + if (status?.status !== 'ok') { + return null + } + const perSeat = status.entitlement.offers?.team.concurrentSandboxesPerSeat + if (!perSeat) { + return null + } + return { perSeat, team: perSeat * Math.max(1, status.entitlement.seats) } +} + export type VmBuildFailureCopyContext = { cloud: boolean /** How the user knows the repository: its GitHub owner/name, else its name in Orca. */ repoName: string /** False for team members who can't change billing; they are told to ask an owner or admin. */ canManageBilling: boolean + /** States the cap on the limit cards; omitted or null falls back to copy without numbers. */ + sandboxLimit?: SandboxLimit | null } /** The card's cause-specific title and one plain-language sentence on what to do next. */ export function vmBuildFailureCopy( failure: VmBuildFailure, - { cloud, canManageBilling, repoName }: VmBuildFailureCopyContext, + { cloud, canManageBilling, repoName, sandboxLimit }: VmBuildFailureCopyContext, t: Translate ): { title: string; body: string | null } { switch (failure.kind) { @@ -104,10 +124,16 @@ export function vmBuildFailureCopy( 'auto.components.worktree.creation.VmBuildFailureCard.teamLimitTitle', 'You’re at your team’s limit of running sandboxes' ), - body: t( - 'auto.components.worktree.creation.VmBuildFailureCard.limitBody', - 'Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.' - ) + body: sandboxLimit + ? t( + 'auto.components.worktree.creation.VmBuildFailureCard.teamLimitBody', + 'Your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.', + { value0: String(sandboxLimit.team), value1: String(sandboxLimit.perSeat) } + ) + : t( + 'auto.components.worktree.creation.VmBuildFailureCard.limitBody', + 'Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.' + ) } case 'memberConcurrency': return { @@ -115,10 +141,16 @@ export function vmBuildFailureCopy( 'auto.components.worktree.creation.VmBuildFailureCard.memberLimitTitle', 'You’re at your limit of running sandboxes' ), - body: t( - 'auto.components.worktree.creation.VmBuildFailureCard.limitBody', - 'Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.' - ) + body: sandboxLimit + ? t( + 'auto.components.worktree.creation.VmBuildFailureCard.memberLimitBody', + 'Each person can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.', + { value0: String(sandboxLimit.perSeat) } + ) + : t( + 'auto.components.worktree.creation.VmBuildFailureCard.limitBody', + 'Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.' + ) } case 'githubNotConnected': return { diff --git a/src/renderer/src/components/worktree-creation/vm-build-failure-toast.ts b/src/renderer/src/components/worktree-creation/vm-build-failure-toast.ts index 2651fff4057..993a1f3c61b 100644 --- a/src/renderer/src/components/worktree-creation/vm-build-failure-toast.ts +++ b/src/renderer/src/components/worktree-creation/vm-build-failure-toast.ts @@ -4,7 +4,8 @@ import type { Translate } from '@/i18n/use-translate' import { openPlansPage } from '@/components/billing/plans/plans-page-store' import type { PendingWorktreeCreation } from '@/lib/workspace/creation/pending-worktree-creation' import { classifyVmBuildFailure, type VmBuildFailure } from './vm-build-failure' -import { vmBuildFailureCopy } from './vm-build-failure-copy' +import { getLastBillingStatus } from '@/components/billing/billing-summary-store' +import { sandboxLimitFromBilling, vmBuildFailureCopy } from './vm-build-failure-copy' import { isOrcaCloudBuild } from './vm-build-steps' import { orcaCloudRepoName } from './vm-build-repo-actions' import type { Repo } from '../../../../shared/repo-types' @@ -26,7 +27,11 @@ export function vmBuildFailureSummary( recipe ? repos.find((repo) => repo.id === recipe.sourceRepoId) : undefined ) // Unknown here; the card itself refines the copy for members once billing status loads. - return { failure, ...vmBuildFailureCopy(failure, { cloud, canManageBilling: true, repoName }, t) } + const sandboxLimit = sandboxLimitFromBilling(getLastBillingStatus()) + return { + failure, + ...vmBuildFailureCopy(failure, { cloud, canManageBilling: true, repoName, sandboxLimit }, t) + } } /** The build sheet's card as a toast, for a VM build that failed while its sheet was off screen. */ diff --git a/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts b/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts index 849a60a64c3..55c7f3b7dc0 100644 --- a/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts +++ b/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' import { classifyVmBuildFailure, isAccountGate, type VmBuildFailure } from './vm-build-failure' -import { vmBuildFailureCopy } from './vm-build-failure-copy' +import { sandboxLimitFromBilling, vmBuildFailureCopy } from './vm-build-failure-copy' import { orcaCloudRepoName } from './vm-build-repo-actions' import type { Translate } from '@/i18n/use-translate' @@ -104,6 +104,38 @@ describe('vmBuildFailureCopy', () => { ) }) + it('states the running-sandbox cap on the limit cards once the plan is known', () => { + const copy = (kind: VmBuildFailure, sandboxLimit: { perSeat: number; team: number } | null) => + vmBuildFailureCopy( + kind, + { cloud: true, canManageBilling: true, repoName: 'acme/web', sandboxLimit }, + t + ).body + expect(copy({ kind: 'concurrency' }, { perSeat: 3, team: 12 })).toContain( + 'Your team can run 12 sandboxes at once (3 per seat). Put one to sleep or delete it' + ) + expect(copy({ kind: 'memberConcurrency' }, { perSeat: 3, team: 12 })).toContain( + 'Each person can run 3 sandboxes at once. Put one to sleep or delete it' + ) + expect(copy({ kind: 'concurrency' }, null)).toMatch(/^Put one to sleep or delete it/) + }) + + it('derives the caps the way billing enforces them', () => { + const status = (seats: number, perSeat: number | null) => + ({ + status: 'ok', + entitlement: { + seats, + offers: perSeat === null ? null : { team: { concurrentSandboxesPerSeat: perSeat } } + } + }) as unknown as Parameters[0] + expect(sandboxLimitFromBilling(status(4, 3))).toEqual({ perSeat: 3, team: 12 }) + // Billing counts at least one seat. + expect(sandboxLimitFromBilling(status(0, 3))).toEqual({ perSeat: 3, team: 3 }) + expect(sandboxLimitFromBilling(status(4, null))).toBeNull() + expect(sandboxLimitFromBilling(null)).toBeNull() + }) + it('names the repository GitHub needs to reach', () => { const { title } = vmBuildFailureCopy( { kind: 'githubNotConnected' }, diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 2b97fc9d63e..1182ccaade2 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -4108,6 +4108,8 @@ "paymentFailedBodyMember": "New sandboxes can’t start until the payment goes through. Ask an owner or admin to update the payment method. Nothing was created, and you weren’t charged.", "teamLimitTitle": "You’re at your team’s limit of running sandboxes", "limitBody": "Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", + "teamLimitBody": "Your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", + "memberLimitBody": "Each person can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", "memberLimitTitle": "You’re at your limit of running sandboxes", "githubNotConnectedTitle": "GitHub isn’t connected for {{repo}}", "githubNotConnectedBody": "Orca Cloud clones your code from GitHub and needs access to this repository first. Connect GitHub, then try again. Nothing was created, and you weren’t charged.", @@ -20540,7 +20542,7 @@ "vpSecretsTitle3": "API keys stay out of the sandbox", "billedYearlyAmount2": "{{value0}} per seat, billed yearly", "entPriced2": "Priced for your team", - "footHours3": "Hours reset every month, including on annual plans.", + "footHours4": "Hours reset every month, including on annual plans. Each seat can run {{value0}} sandboxes at once.", "perSeatPerMonth": "per seat per month", "seatsPerMember_one": "{{count}} seat, one per team member", "seatsPerMember_other": "{{count}} seats, one per team member", @@ -20676,6 +20678,8 @@ "emptyTeamTitle": "No team workspaces yet", "environmentName": "{{value0}} (cloud)", "errorConcurrencyLimit": "You're at your team's limit of running sandboxes. Put one to sleep or delete it, then try again.", + "errorConcurrencyLimitCount": "You're at your team's limit: your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.", + "errorMemberConcurrencyLimit": "You're at your limit of running sandboxes. Put one to sleep or delete it, then try again.", "errorConflict": "This workspace is still starting or going to sleep. Nothing was changed. Try again in a moment.", "errorConflictInline": "This workspace is still starting or going to sleep. Nothing was changed.", "errorEntitlementDenied": "Your team's plan can't start this workspace right now. Your files are safe. Ask an owner or admin to check the plan.", From d84196a177d92e40d3cc7579362d8857bb683a68 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 17:28:51 -0800 Subject: [PATCH 27/30] =?UTF-8?q?docs(cloud):=20acceptance=20findings=20?= =?UTF-8?q?=E2=80=94=20W6=20precedence=20confirmed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/apps/acceptance/findings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud/apps/acceptance/findings.json b/cloud/apps/acceptance/findings.json index afce6a89103..aee05cad327 100644 --- a/cloud/apps/acceptance/findings.json +++ b/cloud/apps/acceptance/findings.json @@ -5,7 +5,7 @@ "D-2 (bug, core desktop, high): Delete Workspace hides the row and reports success when the control plane answers not_found, while the VM keeps running (a billing leak).", "D-3 (bug, core desktop): after a cloud create, the checkout at /vercel/sandbox/project is added as a new project named \"project\" instead of being grouped under orca-sandbox-push-test.", "D-4 (minor, desktop): 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.", - "W6-1 (check, w6-agents-secrets): sec.precedence on OPENAI_API_KEY no longer saw the org value in orcad's env, likely because egress placeholders now replace provider keys. The check moved to AWS_REGION (also an agent-key name); W6 to confirm that org beats personal is still enforced at the egress swap.", + "W6-1 (resolved): provider keys are now egress placeholders in orcad's env (swap on by default on dev), so sec.precedence reads AWS_REGION. W6 confirmed the swap resolves keys with the same precedence as injection (team > repo > 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.", "Fixed since the last report: 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.", "Note: 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." ], From e74d775884c40b2fdc4f36ec4c1531be30be10a9 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:29:05 -0800 Subject: [PATCH 28/30] feat(cloud): Orca Cloud workspaces on the phone (W8) (#113) * feat(cloud): Orca Cloud workspaces on the phone (W8) Home gets a Cloud section that lists the team's cloud workspaces straight from the control plane (phone-signed device-session tokens; no desktop in the path). A workspace screen wakes the sandbox (W2 long-poll wake), gets this phone its own revocable orcad device through a W4 sealed grant (opened with the shared opener on pure-JS primitives for Hermes), pairs directly over E2EE to the stable sandbox endpoint, and delivers prompts queued on the phone while it was asleep (agent.launchReplay, deduped by operation id). Create a workspace from the phone (repo, branch, agent, first prompt, sandbox size), sleep, archive, live status and spend, per-phone access management with revoke and disconnect, and team share links (orca://cloud/w/) open on the phone. Copy follows the Orca Cloud glossary; dark/light, Reduce Motion, VoiceOver announcements. * fix(w8): design round 2 (honest spend, can't-connect state, queue states, distinct titles, neutral archive, hairline groups) * fix(w8): FE + W10 round 2 (retry ids, pairing give-up, create idempotency, neutral dots, waking composer, a11y values, salvage storage) * fix(w8): tell same-named cloud workspaces apart by when they were made * fix(w8): agent names from the shared table; localized repo placeholder * fix(w8): round 3 (agent-started card with terminal sign-in path, transport-safe retries, open waits for the connection, single wake timer, created-ago rows) * fix(w8): FE round 4 (open never blocked on worktree lookup, one connect deadline per run, mid-delivery prompts never stranded, no blank terminal on open session) * fix(w8): prompts interrupted mid-delivery count as waiting (Open and retry resume them) --- mobile/app/_layout.tsx | 4 + mobile/app/cloud/[sandboxId].tsx | 14 + mobile/app/cloud/access.tsx | 5 + mobile/app/cloud/new.tsx | 5 + mobile/app/cloud/w/[sandboxId].tsx | 9 + mobile/package.json | 1 + mobile/pnpm-lock.yaml | 9 + mobile/src/cloud/CloudAccessScreen.tsx | 204 +++++++++ mobile/src/cloud/CloudAgentPicker.tsx | 80 ++++ mobile/src/cloud/CloudAgentStartedCard.tsx | 52 +++ mobile/src/cloud/CloudElapsed.tsx | 17 + mobile/src/cloud/CloudErrorBanner.tsx | 28 ++ mobile/src/cloud/CloudPhaseSteps.tsx | 105 +++++ mobile/src/cloud/CloudPromptComposer.tsx | 78 ++++ mobile/src/cloud/CloudPromptQueueSection.tsx | 95 +++++ mobile/src/cloud/CloudStatusCard.tsx | 75 ++++ mobile/src/cloud/CloudStatusDot.tsx | 84 ++++ mobile/src/cloud/CloudWorkspaceActions.tsx | 97 +++++ mobile/src/cloud/CloudWorkspaceScreen.tsx | 307 ++++++++++++++ mobile/src/cloud/NewCloudWorkspaceScreen.tsx | 391 ++++++++++++++++++ .../src/cloud/cloud-control-plane-access.ts | 45 ++ .../src/cloud/cloud-control-plane-client.ts | 251 +++++++++++ .../src/cloud/cloud-control-plane-request.ts | 79 ++++ mobile/src/cloud/cloud-device-enrollment.ts | 74 ++++ mobile/src/cloud/cloud-device-identity.ts | 101 +++++ mobile/src/cloud/cloud-device-label.ts | 11 + mobile/src/cloud/cloud-device-token.ts | 43 ++ mobile/src/cloud/cloud-prompt-queue.ts | 196 +++++++++ mobile/src/cloud/cloud-repo-input.test.ts | 21 + mobile/src/cloud/cloud-repo-input.ts | 32 ++ mobile/src/cloud/cloud-sealed-grant.test.ts | 74 ++++ mobile/src/cloud/cloud-sealed-grant.ts | 33 ++ mobile/src/cloud/cloud-session.ts | 63 +++ mobile/src/cloud/cloud-workspace-attach.ts | 125 ++++++ mobile/src/cloud/cloud-workspace-copy.ts | 174 ++++++++ mobile/src/cloud/cloud-workspace-display.ts | 98 +++++ .../cloud/cloud-workspace-host-operations.ts | 103 +++++ mobile/src/cloud/cloud-workspace-links.ts | 100 +++++ .../cloud/cloud-workspace-screen-styles.ts | 214 ++++++++++ .../src/cloud/cloud-workspace-title.test.ts | 12 + mobile/src/cloud/use-cloud-host-ids.ts | 30 ++ mobile/src/cloud/use-cloud-workspace-run.ts | 288 +++++++++++++ mobile/src/cloud/use-cloud-workspaces.ts | 135 ++++++ mobile/src/home/MobileHomeCloudSection.tsx | 297 +++++++++++++ mobile/src/home/MobileHomeScreen.tsx | 18 +- mobile/src/home/tabs/MobileHomeFeedTab.tsx | 33 +- mobile/src/i18n/locales/en.json | 187 ++++++++- 47 files changed, 4493 insertions(+), 4 deletions(-) create mode 100644 mobile/app/cloud/[sandboxId].tsx create mode 100644 mobile/app/cloud/access.tsx create mode 100644 mobile/app/cloud/new.tsx create mode 100644 mobile/app/cloud/w/[sandboxId].tsx create mode 100644 mobile/src/cloud/CloudAccessScreen.tsx create mode 100644 mobile/src/cloud/CloudAgentPicker.tsx create mode 100644 mobile/src/cloud/CloudAgentStartedCard.tsx create mode 100644 mobile/src/cloud/CloudElapsed.tsx create mode 100644 mobile/src/cloud/CloudErrorBanner.tsx create mode 100644 mobile/src/cloud/CloudPhaseSteps.tsx create mode 100644 mobile/src/cloud/CloudPromptComposer.tsx create mode 100644 mobile/src/cloud/CloudPromptQueueSection.tsx create mode 100644 mobile/src/cloud/CloudStatusCard.tsx create mode 100644 mobile/src/cloud/CloudStatusDot.tsx create mode 100644 mobile/src/cloud/CloudWorkspaceActions.tsx create mode 100644 mobile/src/cloud/CloudWorkspaceScreen.tsx create mode 100644 mobile/src/cloud/NewCloudWorkspaceScreen.tsx create mode 100644 mobile/src/cloud/cloud-control-plane-access.ts create mode 100644 mobile/src/cloud/cloud-control-plane-client.ts create mode 100644 mobile/src/cloud/cloud-control-plane-request.ts create mode 100644 mobile/src/cloud/cloud-device-enrollment.ts create mode 100644 mobile/src/cloud/cloud-device-identity.ts create mode 100644 mobile/src/cloud/cloud-device-label.ts create mode 100644 mobile/src/cloud/cloud-device-token.ts create mode 100644 mobile/src/cloud/cloud-prompt-queue.ts create mode 100644 mobile/src/cloud/cloud-repo-input.test.ts create mode 100644 mobile/src/cloud/cloud-repo-input.ts create mode 100644 mobile/src/cloud/cloud-sealed-grant.test.ts create mode 100644 mobile/src/cloud/cloud-sealed-grant.ts create mode 100644 mobile/src/cloud/cloud-session.ts create mode 100644 mobile/src/cloud/cloud-workspace-attach.ts create mode 100644 mobile/src/cloud/cloud-workspace-copy.ts create mode 100644 mobile/src/cloud/cloud-workspace-display.ts create mode 100644 mobile/src/cloud/cloud-workspace-host-operations.ts create mode 100644 mobile/src/cloud/cloud-workspace-links.ts create mode 100644 mobile/src/cloud/cloud-workspace-screen-styles.ts create mode 100644 mobile/src/cloud/cloud-workspace-title.test.ts create mode 100644 mobile/src/cloud/use-cloud-host-ids.ts create mode 100644 mobile/src/cloud/use-cloud-workspace-run.ts create mode 100644 mobile/src/cloud/use-cloud-workspaces.ts create mode 100644 mobile/src/home/MobileHomeCloudSection.tsx diff --git a/mobile/app/_layout.tsx b/mobile/app/_layout.tsx index ba532452c05..494a66abf56 100644 --- a/mobile/app/_layout.tsx +++ b/mobile/app/_layout.tsx @@ -226,6 +226,10 @@ export default function RootLayout() { + + + + () + const sandboxId = typeof params.sandboxId === 'string' ? params.sandboxId : '' + return ( + + ) +} diff --git a/mobile/app/cloud/access.tsx b/mobile/app/cloud/access.tsx new file mode 100644 index 00000000000..84d379dc4cf --- /dev/null +++ b/mobile/app/cloud/access.tsx @@ -0,0 +1,5 @@ +import { CloudAccessScreen } from '../../src/cloud/CloudAccessScreen' + +export default function CloudAccessRoute() { + return +} diff --git a/mobile/app/cloud/new.tsx b/mobile/app/cloud/new.tsx new file mode 100644 index 00000000000..07d777243b6 --- /dev/null +++ b/mobile/app/cloud/new.tsx @@ -0,0 +1,5 @@ +import { NewCloudWorkspaceScreen } from '../../src/cloud/NewCloudWorkspaceScreen' + +export default function NewCloudWorkspaceRoute() { + return +} diff --git a/mobile/app/cloud/w/[sandboxId].tsx b/mobile/app/cloud/w/[sandboxId].tsx new file mode 100644 index 00000000000..14938af67bc --- /dev/null +++ b/mobile/app/cloud/w/[sandboxId].tsx @@ -0,0 +1,9 @@ +import { useLocalSearchParams } from 'expo-router' +import { CloudWorkspaceScreen } from '../../../src/cloud/CloudWorkspaceScreen' + +/** Team share links (orca://cloud/w/, 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 +} diff --git a/mobile/package.json b/mobile/package.json index 7fbcd30f8b7..39ee0ad0379 100644 --- a/mobile/package.json +++ b/mobile/package.json @@ -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", diff --git a/mobile/pnpm-lock.yaml b/mobile/pnpm-lock.yaml index fffdc5f0a6a..765c1819f5e 100644 --- a/mobile/pnpm-lock.yaml +++ b/mobile/pnpm-lock.yaml @@ -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)': diff --git a/mobile/src/cloud/CloudAccessScreen.tsx b/mobile/src/cloud/CloudAccessScreen.tsx new file mode 100644 index 00000000000..6076113a714 --- /dev/null +++ b/mobile/src/cloud/CloudAccessScreen.tsx @@ -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(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 ( + router.back()} + overlays={ + confirm && void revoke(confirm)} + onCancel={() => setConfirm(null)} + /> + } + > + + {enrollment ? ( + } + 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) + }) + } + /> + ) : ( + + {t('cloud.access.notConnected', 'Not connected to Orca Cloud.')} + + )} + + {enrollment ? ( + + {others.length === 0 ? ( + + {sessions.isPending + ? t('common.loading', 'Loading…') + : t('cloud.access.noOthers', 'No other phones have access.')} + + ) : ( + others.map((session) => ( + } + 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 + })} + /> + )) + )} + + ) : null} + {enrollment ? ( + + + setConfirm({ id: enrollment.deviceSessionId, label: enrollment.label, self: true }) + } + /> + + ) : null} + {error ? {error} : null} + + ) +} diff --git a/mobile/src/cloud/CloudAgentPicker.tsx b/mobile/src/cloud/CloudAgentPicker.tsx new file mode 100644 index 00000000000..e4171470df6 --- /dev/null +++ b/mobile/src/cloud/CloudAgentPicker.tsx @@ -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 ( + + {CLOUD_AGENTS.map(({ id, Icon }) => { + const label = cloudAgentLabel(id) + const checked = props.value === id + return ( + props.onChange(id)} + hitSlop={{ top: 4, bottom: 4 }} + style={({ pressed }) => [ + styles.chip, + checked && styles.chipSelected, + props.disabled && styles.disabled, + pressed && styles.pressed + ]} + > + + {label} + + ) + })} + + ) +} + +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' } +})) diff --git a/mobile/src/cloud/CloudAgentStartedCard.tsx b/mobile/src/cloud/CloudAgentStartedCard.tsx new file mode 100644 index 00000000000..b0fa90c1890 --- /dev/null +++ b/mobile/src/cloud/CloudAgentStartedCard.tsx @@ -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 ( + + + + + {t('cloud.started.title', '{{agent}} started with your prompt', { agent })} + + + + {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 } + )} + + {props.onOpenTerminal ? ( + [ + styles.errorRetry, + styles.startedAction, + pressed && styles.pressed + ]} + > + + {t('cloud.started.openTerminal', 'Open terminal to sign in')} + + + ) : null} + + ) +} diff --git a/mobile/src/cloud/CloudElapsed.tsx b/mobile/src/cloud/CloudElapsed.tsx new file mode 100644 index 00000000000..18d8eb3be30 --- /dev/null +++ b/mobile/src/cloud/CloudElapsed.tsx @@ -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 }) { + const [now, setNow] = useState(() => Date.now()) + useEffect(() => { + const timer = setInterval(() => setNow(Date.now()), 1_000) + return () => clearInterval(timer) + }, []) + return ( + + {formatElapsed(now - since)} + + ) +} diff --git a/mobile/src/cloud/CloudErrorBanner.tsx b/mobile/src/cloud/CloudErrorBanner.tsx new file mode 100644 index 00000000000..e48188a78f1 --- /dev/null +++ b/mobile/src/cloud/CloudErrorBanner.tsx @@ -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 ( + + + {props.message} + {props.onRetry ? ( + [styles.errorRetry, pressed && styles.pressed]} + > + {t('common.tryAgain', 'Try again')} + + ) : null} + + ) +} diff --git a/mobile/src/cloud/CloudPhaseSteps.tsx b/mobile/src/cloud/CloudPhaseSteps.tsx new file mode 100644 index 00000000000..25582bdb7dc --- /dev/null +++ b/mobile/src/cloud/CloudPhaseSteps.tsx @@ -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 ( + + {steps.map((step, index) => { + const done = index < currentIndex + const current = index === currentIndex + return ( + + + {done ? ( + + ) : current ? ( + + ) : null} + + + {step.label} + + {current ? : null} + + ) + })} + + ) +} + +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'] } +})) diff --git a/mobile/src/cloud/CloudPromptComposer.tsx b/mobile/src/cloud/CloudPromptComposer.tsx new file mode 100644 index 00000000000..25887cb0375 --- /dev/null +++ b/mobile/src/cloud/CloudPromptComposer.tsx @@ -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 +}) { + 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 ( + + + + + + void send()} + hitSlop={6} + style={({ pressed }) => [ + styles.sendButton, + !canSend && styles.sendDisabled, + pressed && styles.pressed + ]} + > + + + + + {props.awake ? null : ( + + {props.waking + ? t('cloud.prompt.wakingCaption', 'Sends when it’s awake.') + : t('cloud.prompt.wakeCaption', 'Sending wakes the sandbox first.')} + + )} + + ) +} diff --git a/mobile/src/cloud/CloudPromptQueueSection.tsx b/mobile/src/cloud/CloudPromptQueueSection.tsx new file mode 100644 index 00000000000..67b3c7f28a9 --- /dev/null +++ b/mobile/src/cloud/CloudPromptQueueSection.tsx @@ -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 ( + + + {t('cloud.prompt.queued', 'Waiting to send')} + + + {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 ( + 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() + } + > + + + {prompt.text} + + + {`${cloudAgentLabel(prompt.agent)} · ${state}`} + + + {prompt.status === 'delivering' ? ( + + ) : ( + + {failed ? ( + + + + ) : null} + + + + + )} + + ) + })} + {props.awake ? null : ( + + + + {t( + 'cloud.prompt.queuedNote', + 'Kept on this phone until the workspace is awake, then sent end-to-end encrypted.' + )} + + + )} + + + ) +} diff --git a/mobile/src/cloud/CloudStatusCard.tsx b/mobile/src/cloud/CloudStatusCard.tsx new file mode 100644 index 00000000000..b6a067f053d --- /dev/null +++ b/mobile/src/cloud/CloudStatusCard.tsx @@ -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 ( + + + + + {active + ? t('cloud.status.waking', 'Waking') + : notConnected + ? t('cloud.status.cantConnect', 'Can’t connect') + : cloudStatusLabel(workspace.status, t)} + + + {active ? ( + + ) : notConnected ? null : ( + // Not connected: the error banner right below says it, once. + {statusSentence(workspace, t, props.uptimeMs)} + )} + {spend ? {spend} : null} + + ) +} diff --git a/mobile/src/cloud/CloudStatusDot.tsx b/mobile/src/cloud/CloudStatusDot.tsx new file mode 100644 index 00000000000..fe5c5f9cde0 --- /dev/null +++ b/mobile/src/cloud/CloudStatusDot.tsx @@ -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 ( + + {half ? ( + + + + + ) : null} + + ) +} diff --git a/mobile/src/cloud/CloudWorkspaceActions.tsx b/mobile/src/cloud/CloudWorkspaceActions.tsx new file mode 100644 index 00000000000..fd876698b39 --- /dev/null +++ b/mobile/src/cloud/CloudWorkspaceActions.tsx @@ -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 ? ( + + + } + 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')} + /> + + + ) : null} + + + } + title={t('cloud.actions.archive', 'Archive')} + busy={props.busy === 'archive'} + disabled={props.disabled || props.workspace.status === 'archived'} + onPress={() => ask('archive')} + /> + + + { + setOpen(false) + if (kind === 'sleep') { + props.onSleep() + } else { + props.onArchive() + } + }} + onCancel={() => setOpen(false)} + /> + + ) +} diff --git a/mobile/src/cloud/CloudWorkspaceScreen.tsx b/mobile/src/cloud/CloudWorkspaceScreen.tsx new file mode 100644 index 00000000000..d040de7a1f2 --- /dev/null +++ b/mobile/src/cloud/CloudWorkspaceScreen.tsx @@ -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([]) + 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 => { + 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 ( + + router.back()} + /> + + {isPending || isFetching || enrollment === undefined ? ( + + ) : ( + <> + + {t( + 'cloud.workspace.missing', + 'This workspace is gone or you no longer have access.' + )} + + void refetch()} + style={styles.linkButton} + > + {t('common.tryAgain', 'Try again')} + + + )} + + + ) + } + + 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 ( + + + router.back()} /> + + prompt.status !== 'failed')} + /> + {run.kind === 'launched' ? ( + openSession('terminal') : null} + /> + ) : null} + {run.kind === 'error' ? ( + void start(run.retry ?? 'open') : null} + /> + ) : null} + + + + {t('cloud.prompt.heading', 'Ask an agent')} + + + + void start('send')} /> + + + + {t('cloud.details.heading', 'Details')} + + + {cloudRepoLine(workspace) ? ( + + ) : null} + {cloudRepoLine(workspace) ? : null} + + + + + void lifecycle('sleep')} + onArchive={() => void lifecycle('archive')} + /> + + + + (run.kind === 'launched' ? openSession('chat') : void start('open'))} + style={({ pressed }) => [ + styles.primary, + primaryDisabled && !active && styles.primaryDisabled, + pressed && styles.pressed + ]} + > + {active ? : null} + + {primaryLabel} + + + + + + ) +} diff --git a/mobile/src/cloud/NewCloudWorkspaceScreen.tsx b/mobile/src/cloud/NewCloudWorkspaceScreen.tsx new file mode 100644 index 00000000000..b68ced925d9 --- /dev/null +++ b/mobile/src/cloud/NewCloudWorkspaceScreen.tsx @@ -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('standard') + const [startedAt, setStartedAt] = useState(null) + const [error, setError] = useState(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(null) + const abortRef = useRef(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> + 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 ( + + + router.back()} + /> + + + {t('cloud.new.repoInvalid', 'Use a GitHub repository, like owner/repo.')} + + ) : ( + 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.' + ) + ) + } + > + + + + + + + + + + + + + + + + + + + + {(['standard', 'large'] as const).map((option) => ( + setSize(option)} + style={[styles.segment, size === option && styles.segmentSelected]} + > + + {option === 'standard' + ? t('cloud.size.standardShort', 'Standard') + : t('cloud.size.largeShort', 'Large')} + + + {option === 'standard' + ? t('cloud.size.standardSpec', '4 vCPU · 8 GB') + : t('cloud.size.largeSpec', '8 vCPU · 16 GB')} + + + ))} + + + + {t('cloud.size.largeNote', 'Large uses sandbox hours at 2× the rate.')} + + + {creating ? ( + + + + ) : null} + {error ? ( + + {error} + + ) : null} + + + + void create()} + style={({ pressed }) => [ + styles.primary, + repoInvalid && styles.primaryDisabled, + pressed && styles.pressed + ]} + > + {creating ? : null} + + {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')} + + {startedAt !== null ? ( + + ) : null} + + + + + ) +} + +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'] + } +})) diff --git a/mobile/src/cloud/cloud-control-plane-access.ts b/mobile/src/cloud/cloud-control-plane-access.ts new file mode 100644 index 00000000000..bb65cbfa6b2 --- /dev/null +++ b/mobile/src/cloud/cloud-control-plane-access.ts @@ -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 + +/** 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 { + await request(plane, 'DELETE', `/v1/device-sessions/${encodeURIComponent(id)}`, z.unknown()) +} diff --git a/mobile/src/cloud/cloud-control-plane-client.ts b/mobile/src/cloud/cloud-control-plane-client.ts new file mode 100644 index 00000000000..81e686ef162 --- /dev/null +++ b/mobile/src/cloud/cloud-control-plane-client.ts @@ -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 = { + 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 `-<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): 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 { + 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 { + await request( + plane, + 'POST', + `/v1/sandboxes/${encodeURIComponent(sandboxId)}/suspend`, + z.unknown(), + { timeoutMs: 60_000 } + ) +} + +export async function archiveCloudWorkspace( + plane: CloudControlPlane, + sandboxId: string +): Promise { + 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 { + 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 { + await request( + plane, + 'PATCH', + `/v1/team/workspaces/${encodeURIComponent(sandboxId)}`, + z.unknown(), + { + body: { title } + } + ).catch(() => undefined) +} diff --git a/mobile/src/cloud/cloud-control-plane-request.ts b/mobile/src/cloud/cloud-control-plane-request.ts new file mode 100644 index 00000000000..b8f552dc4d1 --- /dev/null +++ b/mobile/src/cloud/cloud-control-plane-request.ts @@ -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( + plane: CloudControlPlane, + method: 'GET' | 'POST' | 'PATCH' | 'DELETE', + path: string, + schema: z.ZodType, + init: { + body?: unknown + timeoutMs?: number + signal?: AbortSignal + headers?: Record + } = {} +): Promise { + // 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. */ diff --git a/mobile/src/cloud/cloud-device-enrollment.ts b/mobile/src/cloud/cloud-device-enrollment.ts new file mode 100644 index 00000000000..9b37e98db1b --- /dev/null +++ b/mobile/src/cloud/cloud-device-enrollment.ts @@ -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 { + 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 + }) +} diff --git a/mobile/src/cloud/cloud-device-identity.ts b/mobile/src/cloud/cloud-device-identity.ts new file mode 100644 index 00000000000..820d32fe430 --- /dev/null +++ b/mobile/src/cloud/cloud-device-identity.ts @@ -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 + +export type CloudDeviceIdentity = { + publicKeyB64: string + secretKey: Uint8Array + enrollment: CloudEnrollment | null +} + +let cached: Promise | 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 { + 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 | 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 { + cached ??= readSeed() + .then(({ seed, enrollment }) => fromSeed(seed, enrollment)) + .catch((error: unknown) => { + cached = null + throw error + }) + return cached +} + +export async function saveCloudEnrollment(enrollment: CloudEnrollment): Promise { + 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 { + 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) +} diff --git a/mobile/src/cloud/cloud-device-label.ts b/mobile/src/cloud/cloud-device-label.ts new file mode 100644 index 00000000000..3bc660a7330 --- /dev/null +++ b/mobile/src/cloud/cloud-device-label.ts @@ -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' +} diff --git a/mobile/src/cloud/cloud-device-token.ts b/mobile/src/cloud/cloud-device-token.ts new file mode 100644 index 00000000000..dd65c7762e4 --- /dev/null +++ b/mobile/src/cloud/cloud-device-token.ts @@ -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)}` +} diff --git a/mobile/src/cloud/cloud-prompt-queue.ts b/mobile/src/cloud/cloud-prompt-queue.ts new file mode 100644 index 00000000000..00f397373ef --- /dev/null +++ b/mobile/src/cloud/cloud-prompt-queue.ts @@ -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 + +const listeners = new Set<() => void>() +let mutation: Promise = Promise.resolve() + +async function read(): Promise { + 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( + change: (queue: QueuedCloudPrompt[]) => { next: QueuedCloudPrompt[]; result: T } +): Promise { + 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 { + return (await read()).filter((prompt) => prompt.sandboxId === sandboxId) +} + +export function enqueueCloudPrompt(args: { + sandboxId: string + text: string + agent: string + operationId: string + now?: number +}): Promise { + 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 & { error?: string } +): Promise { + 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 { + 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 { + 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 { + 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 { + 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() + +/** 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, + 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 } +} diff --git a/mobile/src/cloud/cloud-repo-input.test.ts b/mobile/src/cloud/cloud-repo-input.test.ts new file mode 100644 index 00000000000..74f254bbfb0 --- /dev/null +++ b/mobile/src/cloud/cloud-repo-input.test.ts @@ -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() + }) +}) diff --git a/mobile/src/cloud/cloud-repo-input.ts b/mobile/src/cloud/cloud-repo-input.ts new file mode 100644 index 00000000000..04d1727c7c0 --- /dev/null +++ b/mobile/src/cloud/cloud-repo-input.ts @@ -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]}` +} + +/** `-<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}` +} diff --git a/mobile/src/cloud/cloud-sealed-grant.test.ts b/mobile/src/cloud/cloud-sealed-grant.test.ts new file mode 100644 index 00000000000..8ab9eb8650c --- /dev/null +++ b/mobile/src/cloud/cloud-sealed-grant.test.ts @@ -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() + }) +}) diff --git a/mobile/src/cloud/cloud-sealed-grant.ts b/mobile/src/cloud/cloud-sealed-grant.ts new file mode 100644 index 00000000000..6cbf032cbce --- /dev/null +++ b/mobile/src/cloud/cloud-sealed-grant.ts @@ -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 { + return openSealedGrant(sealed, grantId, recipientSecretKey, PRIMITIVES, 'mobile') +} diff --git a/mobile/src/cloud/cloud-session.ts b/mobile/src/cloud/cloud-session.ts new file mode 100644 index 00000000000..54625a48990 --- /dev/null +++ b/mobile/src/cloud/cloud-session.ts @@ -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 { + 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 { + 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 { + if (error instanceof CloudRequestError && error.status === 401) { + await forgetAllCloudAccess(removal) + return true + } + return false +} diff --git a/mobile/src/cloud/cloud-workspace-attach.ts b/mobile/src/cloud/cloud-workspace-attach.ts new file mode 100644 index 00000000000..195671e555a --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-attach.ts @@ -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 { + 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 { + 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() + } +} diff --git a/mobile/src/cloud/cloud-workspace-copy.ts b/mobile/src/cloud/cloud-workspace-copy.ts new file mode 100644 index 00000000000..43fd17ed73d --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-copy.ts @@ -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.' + ) +} diff --git a/mobile/src/cloud/cloud-workspace-display.ts b/mobile/src/cloud/cloud-workspace-display.ts new file mode 100644 index 00000000000..b603ec92472 --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-display.ts @@ -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() + +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): 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[] +): 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}` +} diff --git a/mobile/src/cloud/cloud-workspace-host-operations.ts b/mobile/src/cloud/cloud-workspace-host-operations.ts new file mode 100644 index 00000000000..d31e0b95049 --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-host-operations.ts @@ -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 { + 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 +): Promise { + 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 } : {}) } +} diff --git a/mobile/src/cloud/cloud-workspace-links.ts b/mobile/src/cloud/cloud-workspace-links.ts new file mode 100644 index 00000000000..999fa21da0e --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-links.ts @@ -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 + +let mutation: Promise = Promise.resolve() + +async function readLinks(): Promise { + 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 { + 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 { + 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 { + if (!projectRoot.startsWith('/')) { + return Promise.resolve() + } + return updateLinks((links) => { + links[sandboxId] = { ...links[sandboxId], projectRoot } + }) +} + +export async function cloudWorkspaceProjectRoot(sandboxId: string): Promise { + 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 { + 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> { + const ids = Object.values(await readLinks()).flatMap((link) => (link.hostId ? [link.hostId] : [])) + return new Set(ids) +} + +export async function clearCloudWorkspaceLinks(): Promise { + await updateLinks((links) => { + for (const key of Object.keys(links)) { + delete links[key] + } + }) +} diff --git a/mobile/src/cloud/cloud-workspace-screen-styles.ts b/mobile/src/cloud/cloud-workspace-screen-styles.ts new file mode 100644 index 00000000000..4041be012ed --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-screen-styles.ts @@ -0,0 +1,214 @@ +import { StyleSheet } from 'react-native' +import { colors, radii, spacing, typography } from '../theme/mobile-theme' +import { themedStyleSheet } from '../theme/themed-styles' + +// One rhythm for every cloud screen: 24 between sections, 8 from a heading to its card, 6 to a caption. +const SECTION_GAP = spacing.xl +const ACTION_BAR_HEIGHT = 50 + spacing.md * 2 + +/** Scroll padding that keeps the last row clear of the sticky action bar. */ +export function cloudScrollBottomPadding(bottomInset: number): number { + return ACTION_BAR_HEIGHT + Math.max(bottomInset, spacing.md) + spacing.lg +} + +export const cloudWorkspaceScreenStyles = themedStyleSheet(() => ({ + container: { flex: 1, backgroundColor: colors.bgBase }, + flex: { flex: 1 }, + scroll: { paddingHorizontal: spacing.lg, paddingTop: spacing.sm }, + centered: { + flex: 1, + alignItems: 'center', + justifyContent: 'center', + padding: spacing.xl, + gap: spacing.md + }, + emptyText: { fontSize: typography.bodySize, color: colors.textSecondary, textAlign: 'center' }, + linkButton: { minHeight: 44, minWidth: 44, padding: spacing.sm, justifyContent: 'center' }, + link: { fontSize: 16, color: colors.accentBlue, fontWeight: '500' }, + section: { marginTop: SECTION_GAP }, + sectionHeading: { + fontSize: 13, + fontWeight: '600', + color: colors.textSecondary, + marginBottom: spacing.sm, + paddingHorizontal: spacing.xs + }, + // The hairline keeps groups readable where the panel and page are nearly the same (light mode). + card: { + backgroundColor: colors.bgPanel, + borderRadius: radii.surface, + borderWidth: StyleSheet.hairlineWidth, + borderColor: colors.borderSubtle, + overflow: 'hidden' + }, + caption: { + flex: 1, + fontSize: 13, + lineHeight: 18, + color: colors.textMuted, + marginTop: 6, + paddingHorizontal: spacing.xs + }, + statusCard: { + backgroundColor: colors.bgPanel, + borderWidth: StyleSheet.hairlineWidth, + borderColor: colors.borderSubtle, + borderRadius: radii.surface, + padding: spacing.lg, + gap: spacing.sm, + marginTop: spacing.xs + }, + statusHeadline: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm }, + statusLabel: { flex: 1, fontSize: 20, fontWeight: '600', color: colors.textPrimary }, + statusLine: { fontSize: typography.bodySize, lineHeight: 20, color: colors.textSecondary }, + statusSpend: { + fontSize: typography.bodySize, + color: colors.textPrimary, + fontVariant: ['tabular-nums'] + }, + errorBanner: { + flexDirection: 'row', + alignItems: 'center', + gap: spacing.sm, + marginTop: spacing.sm, + paddingLeft: spacing.md, + paddingRight: spacing.xs, + paddingVertical: spacing.xs, + minHeight: 52, + borderRadius: radii.surface, + borderWidth: StyleSheet.hairlineWidth, + borderColor: colors.statusRed, + backgroundColor: colors.bgPanel + }, + errorText: { flex: 1, fontSize: typography.bodySize, lineHeight: 20, color: colors.textPrimary }, + errorRetry: { + minHeight: 36, + marginVertical: 4, + paddingHorizontal: spacing.md, + borderRadius: radii.control, + borderWidth: 1, + borderColor: colors.inputBorder, + justifyContent: 'center' + }, + startedCard: { + marginTop: spacing.sm, + padding: spacing.lg, + gap: spacing.sm, + borderRadius: radii.surface, + borderWidth: StyleSheet.hairlineWidth, + borderColor: colors.borderSubtle, + backgroundColor: colors.bgPanel + }, + startedHeadline: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm }, + startedTitle: { flex: 1, fontSize: 16, fontWeight: '600', color: colors.textPrimary }, + startedAction: { alignSelf: 'flex-start' }, + detailDivider: { + height: StyleSheet.hairlineWidth, + backgroundColor: colors.borderSubtle, + marginLeft: spacing.lg + }, + errorRetryText: { fontSize: 15, fontWeight: '600', color: colors.textPrimary }, + composerCard: { + backgroundColor: colors.bgPanel, + borderWidth: StyleSheet.hairlineWidth, + borderColor: colors.borderSubtle, + borderRadius: radii.surface, + padding: spacing.md, + gap: spacing.md + }, + inputWrap: { + backgroundColor: colors.bgRaised, + borderRadius: radii.control, + paddingLeft: spacing.md, + paddingRight: 6, + paddingVertical: 6, + flexDirection: 'row', + alignItems: 'flex-end', + gap: spacing.sm + }, + input: { + flex: 1, + minHeight: 60, + maxHeight: 180, + fontSize: 16, + lineHeight: 21, + color: colors.textPrimary, + paddingTop: 6, + paddingBottom: 6, + textAlignVertical: 'top' + }, + sendButton: { + width: 36, + height: 36, + borderRadius: radii.pill, + backgroundColor: colors.textPrimary, + alignItems: 'center', + justifyContent: 'center' + }, + sendPill: { + height: 36, + paddingHorizontal: spacing.md, + borderRadius: radii.pill, + backgroundColor: colors.textPrimary, + alignItems: 'center', + justifyContent: 'center' + }, + sendPillText: { fontSize: 14, fontWeight: '600', color: colors.bgBase }, + sendDisabled: { + backgroundColor: colors.bgRaised, + borderWidth: 1, + borderColor: colors.borderSubtle + }, + sendDisabledText: { color: colors.textMuted }, + sendRow: { flexDirection: 'row', justifyContent: 'flex-end' }, + pressed: { opacity: 0.7 }, + queueRow: { + flexDirection: 'row', + alignItems: 'center', + gap: spacing.sm, + paddingLeft: spacing.lg, + paddingRight: spacing.sm, + paddingVertical: spacing.sm + }, + queueRowDivider: { + borderTopWidth: StyleSheet.hairlineWidth, + borderTopColor: colors.borderSubtle + }, + queueNote: { alignItems: 'flex-start', paddingRight: spacing.lg }, + queueText: { flex: 1, gap: 2 }, + queuePrompt: { fontSize: 15, lineHeight: 20, color: colors.textPrimary }, + queueState: { fontSize: 13, lineHeight: 18, color: colors.textSecondary }, + queueStateFailed: { color: colors.statusRed }, + queueButtons: { flexDirection: 'row' }, + iconButton: { width: 44, height: 44, alignItems: 'center', justifyContent: 'center' }, + actionBar: { + position: 'absolute', + left: 0, + right: 0, + bottom: 0, + paddingHorizontal: spacing.lg, + paddingTop: spacing.md, + backgroundColor: colors.bgBase, + borderTopWidth: StyleSheet.hairlineWidth, + 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: { backgroundColor: colors.bgRaised }, + primaryText: { fontSize: 16, fontWeight: '600', color: colors.bgBase }, + primaryTextDisabled: { color: colors.textMuted }, + primaryElapsed: { + fontSize: 15, + color: colors.bgBase, + opacity: 0.7, + fontVariant: ['tabular-nums'] + } +})) diff --git a/mobile/src/cloud/cloud-workspace-title.test.ts b/mobile/src/cloud/cloud-workspace-title.test.ts new file mode 100644 index 00000000000..a7aa3d28c7e --- /dev/null +++ b/mobile/src/cloud/cloud-workspace-title.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, it } from 'vitest' +import { nextCloudWorkspaceTitle } from './cloud-workspace-display' + +describe('nextCloudWorkspaceTitle', () => { + it('numbers a repeated title from 2 and skips numbers already taken', () => { + expect(nextCloudWorkspaceTitle('Hello-World', [])).toBe('Hello-World') + expect(nextCloudWorkspaceTitle('Hello-World', [{ title: 'Hello-World' }])).toBe('Hello-World 2') + expect( + nextCloudWorkspaceTitle('Hello-World', [{ title: 'Hello-World' }, { title: 'Hello-World 2' }]) + ).toBe('Hello-World 3') + }) +}) diff --git a/mobile/src/cloud/use-cloud-host-ids.ts b/mobile/src/cloud/use-cloud-host-ids.ts new file mode 100644 index 00000000000..101e3039214 --- /dev/null +++ b/mobile/src/cloud/use-cloud-host-ids.ts @@ -0,0 +1,30 @@ +import { useFocusEffect } from 'expo-router' +import { useCallback, useState } from 'react' +import type { CloudEnrollment } from './cloud-device-identity' +import { cloudWorkspaceHostIds } from './cloud-workspace-links' + +const EMPTY: ReadonlySet = new Set() + +/** Paired hosts that are cloud workspace VMs; re-read on focus since attach happens on other screens. */ +export function useCloudHostIds( + enrollment: CloudEnrollment | null | undefined +): ReadonlySet { + const [ids, setIds] = useState>(EMPTY) + useFocusEffect( + useCallback(() => { + let live = true + void cloudWorkspaceHostIds() + .then((next) => live && setIds((current) => (sameSet(current, next) ? current : next))) + .catch(() => undefined) + return () => { + live = false + } + // Re-read when enrollment changes (disconnect forgets every cloud host). + }, [enrollment?.deviceSessionId]) + ) + return ids +} + +function sameSet(a: ReadonlySet, b: ReadonlySet): boolean { + return a.size === b.size && [...a].every((id) => b.has(id)) +} diff --git a/mobile/src/cloud/use-cloud-workspace-run.ts b/mobile/src/cloud/use-cloud-workspace-run.ts new file mode 100644 index 00000000000..337f96922b5 --- /dev/null +++ b/mobile/src/cloud/use-cloud-workspace-run.ts @@ -0,0 +1,288 @@ +import { useRouter } from 'expo-router' +import { useCallback, useEffect, useRef, useState } from 'react' +import { AccessibilityInfo } from 'react-native' +import { useMobileTranslation } from '../i18n/use-mobile-translation' +import { useHostClient } from '../transport/connection/host-client-hooks' +import type { CloudWorkspace } from './cloud-control-plane-client' +import { cloudDeviceLabel } from './cloud-device-label' +import { + deliverQueuedCloudPrompts, + listQueuedCloudPrompts, + markWaitingCloudPromptsFailed, + requeueUnsentCloudPrompts +} from './cloud-prompt-queue' +import { + forgetRevokedCloudSession, + loadCloudControlPlane, + useCloudHostRemoval +} from './cloud-session' +import { + CloudAttachError, + attachCloudWorkspace, + type CloudAttachPhase +} from './cloud-workspace-attach' +import { + attachErrorMessage, + cloudPromptErrorCode, + cloudPromptErrorMessage, + phaseLabel +} from './cloud-workspace-copy' +import { + ensureCloudProjectWorktree, + launchCloudPrompt, + type CloudLaunchedAgent +} from './cloud-workspace-host-operations' +import { cloudWorkspaceProjectRoot, recordCloudWorkspaceHost } from './cloud-workspace-links' +import { removeHostAndCloseClient } from '../transport/hosts/host-removal-lifecycle' +import { useInvalidateCloudWorkspaces } from './use-cloud-workspaces' + +export type CloudWorkspaceRun = + | { kind: 'idle' } + | { kind: 'attaching'; phase: CloudAttachPhase; startedAt: number; intent: 'open' | 'send' } + | { kind: 'delivering'; hostId: string; startedAt: number; connectBy: number } + | { kind: 'error'; message: string; retry: 'open' | 'send' | null } + | { kind: 'launched'; hostId: string; worktreeId: string; agent: CloudLaunchedAgent } + +// A paired VM that never reaches `connected` must end in an error the user can retry, not a spinner. +const DELIVERY_CONNECT_DEADLINE_MS = 30_000 + +/** Wake → per-device grant → direct E2EE pairing → deliver queued prompts → land in the session. */ +export function useCloudWorkspaceRun(sandboxId: string, workspace: CloudWorkspace | null) { + const t = useMobileTranslation() + const router = useRouter() + const removal = useCloudHostRemoval() + const invalidate = useInvalidateCloudWorkspaces() + const [run, setRun] = useState({ kind: 'idle' }) + const abortRef = useRef(null) + const mountedRef = useRef(true) + const deliveryStarted = useRef(null) + const inFlight = useRef(false) + const host = useHostClient(run.kind === 'delivering' ? run.hostId : undefined) + const active = run.kind === 'attaching' || run.kind === 'delivering' + const title = workspace?.title ?? '' + + useEffect(() => { + mountedRef.current = true + return () => { + mountedRef.current = false + abortRef.current?.abort() + } + }, []) + + // VoiceOver and TalkBack hear each step and any failure once, not every timer tick. + useEffect(() => { + if (run.kind === 'attaching') { + AccessibilityInfo.announceForAccessibility(phaseLabel(run.phase, t)) + } else if (run.kind === 'error') { + AccessibilityInfo.announceForAccessibility(run.message) + } + }, [run, t]) + + useEffect(() => { + if ( + run.kind !== 'delivering' || + host.state === 'connected' || + deliveryStarted.current === run + ) { + return + } + const hostId = run.hostId + // A pairing the VM refuses (revoked, rebuilt) is dropped so the next try gets a fresh grant; a + // slow network keeps it, so retries don't pile up devices on the sandbox. + const giveUp = (forget: boolean) => { + if (forget) { + void recordCloudWorkspaceHost(sandboxId, null) + void removeHostAndCloseClient(hostId, removal.forgetHostClient, removal.queryClient).catch( + () => undefined + ) + } + void markWaitingCloudPromptsFailed(sandboxId, 'not_connected').catch(() => undefined) + setRun({ + kind: 'error', + message: attachErrorMessage(new CloudAttachError('pairing_failed'), t), + retry: 'send' + }) + } + if (host.state === 'auth-failed') { + giveUp(true) + return + } + // One deadline per run, not per state change: a flapping connection still ends. + const timer = setTimeout(() => giveUp(false), Math.max(0, run.connectBy - Date.now())) + return () => clearTimeout(timer) + }, [run, host.state, t, sandboxId, removal]) + + useEffect(() => { + // One delivery per run: a re-render must never launch the same prompts twice. + if ( + run.kind !== 'delivering' || + !host.client || + host.state !== 'connected' || + deliveryStarted.current === run + ) { + return + } + deliveryStarted.current = run + const client = host.client + const hostId = run.hostId + void (async () => { + try { + const waiting = await listQueuedCloudPrompts(sandboxId) + if (!waiting.some((prompt) => prompt.status !== 'failed')) { + // Nothing to send: the connection is proven, so open the sandbox's workspaces. + if (mountedRef.current) { + setRun({ kind: 'idle' }) + router.push(`/h/${hostId}`) + } + return + } + const projectRoot = (await cloudWorkspaceProjectRoot(sandboxId)) ?? undefined + const worktreeId = await ensureCloudProjectWorktree(client, projectRoot) + const launches: CloudLaunchedAgent[] = [] + let failed = 0 + // Prompts typed while this ran are picked up too, so none is left "waiting" behind it. + for (let round = 0; round < 5; round++) { + const next = await deliverQueuedCloudPrompts( + sandboxId, + async (prompt) => { + launches.push(await launchCloudPrompt(client, worktreeId, prompt)) + }, + cloudPromptErrorCode + ) + failed += next.failed.length + const pending = await listQueuedCloudPrompts(sandboxId) + if ( + next.delivered + next.failed.length === 0 || + !pending.some((prompt) => prompt.status !== 'failed') + ) { + break + } + } + invalidate() + if (!mountedRef.current) { + return + } + if (failed > 0) { + const stillQueued = (await listQueuedCloudPrompts(sandboxId)).some( + (prompt) => prompt.status !== 'failed' + ) + setRun({ + kind: 'error', + message: t( + 'cloud.error.deliver', + 'The workspace is running, but a prompt didn’t start. Your files are safe.' + ), + retry: stillQueued ? 'send' : null + }) + return + } + const last = launches[launches.length - 1] + if (last) { + // A cloud agent may still need its sign-in: say what happened before dropping into the + // session, and let the user pick the chat or the terminal. + setRun({ kind: 'launched', hostId, worktreeId, agent: last }) + return + } + setRun({ kind: 'idle' }) + router.push(`/h/${hostId}`) + } catch (error) { + if (mountedRef.current) { + // Delivery failures are about the prompt, not the sandbox: say which, never "something". + setRun({ + kind: 'error', + message: cloudPromptErrorMessage(cloudPromptErrorCode(error), t), + retry: 'send' + }) + } + } + })() + }, [run, host.client, host.state, sandboxId, invalidate, router, t, title]) + + const start = useCallback( + async (intent: 'open' | 'send') => { + // A ref, not state: a second tap in the same frame must not start a second grant. + if (!workspace || active || inFlight.current) { + return + } + inFlight.current = true + const plane = await loadCloudControlPlane() + if (!plane) { + inFlight.current = false + router.replace('/') + return + } + // Prompts held back by an earlier failed attach go out with this one, whichever button started it. + await requeueUnsentCloudPrompts(sandboxId).catch(() => undefined) + abortRef.current?.abort() + const controller = new AbortController() + abortRef.current = controller + const startedAt = Date.now() + setRun({ kind: 'attaching', phase: 'waking', startedAt, intent }) + try { + const { hostId } = await attachCloudWorkspace({ + plane, + sandboxId, + title: workspace.title, + deviceLabel: cloudDeviceLabel(), + signal: controller.signal, + onPhase: (phase) => + setRun((current) => + current.kind === 'attaching' && current.phase !== phase + ? { ...current, phase } + : current + ) + }) + invalidate() + if (controller.signal.aborted || !mountedRef.current) { + return + } + // Opening also waits for the sandbox's orcad to accept this phone, so a stale pairing ends in + // a retryable error here rather than on a dead host screen. + setRun({ + kind: 'delivering', + hostId, + startedAt, + connectBy: Date.now() + DELIVERY_CONNECT_DEADLINE_MS + }) + } catch (error) { + if (controller.signal.aborted || !mountedRef.current) { + return + } + await forgetRevokedCloudSession(error, removal) + // Prompts that were going out with this attach now say so, instead of "waiting". + await markWaitingCloudPromptsFailed(sandboxId, 'not_connected').catch(() => undefined) + setRun({ kind: 'error', message: attachErrorMessage(error, t), retry: intent }) + invalidate() + } finally { + inFlight.current = false + } + }, + [workspace, active, router, sandboxId, invalidate, t, removal] + ) + + const openSession = useCallback( + (view: 'chat' | 'terminal') => { + if (run.kind !== 'launched') { + return + } + // The worktree already exists, so no `created=1` (it would auto-create a blank terminal). + const params = new URLSearchParams({ name: title }) + const paneKey = view === 'terminal' ? run.agent.paneKey : undefined + if (paneKey) { + params.set('paneKey', paneKey) + } + setRun({ kind: 'idle' }) + router.push( + `/h/${encodeURIComponent(run.hostId)}/session/${encodeURIComponent(run.worktreeId)}?${params}` + ) + }, + [run, router, title] + ) + + const fail = useCallback((message: string) => setRun({ kind: 'error', message, retry: null }), []) + const clearError = useCallback( + () => setRun((current) => (current.kind === 'error' ? { kind: 'idle' } : current)), + [] + ) + return { run, active, start, fail, clearError, openSession } +} diff --git a/mobile/src/cloud/use-cloud-workspaces.ts b/mobile/src/cloud/use-cloud-workspaces.ts new file mode 100644 index 00000000000..b1a6ea659aa --- /dev/null +++ b/mobile/src/cloud/use-cloud-workspaces.ts @@ -0,0 +1,135 @@ +import { useQuery, useQueryClient } from '@tanstack/react-query' +import { useFocusEffect } from 'expo-router' +import { useCallback, useEffect, useState } from 'react' +import { + listCloudWorkspaces, + readCloudWorkspaceStatus, + type CloudWorkspace +} from './cloud-control-plane-client' +import { + loadCloudDeviceIdentity, + subscribeCloudDeviceIdentity, + type CloudEnrollment +} from './cloud-device-identity' +import { + forgetRevokedCloudSession, + loadCloudControlPlane, + useCloudHostRemoval +} from './cloud-session' + +const TRANSITIONAL: ReadonlySet = new Set([ + 'creating', + 'waking', + 'stopping' +]) + +export const cloudWorkspacesQueryKey = (sessionId: string | null) => + ['orca-cloud', 'workspaces', sessionId] as const + +/** True while this screen is on top; Home stays mounted under every pushed screen. */ +function useScreenFocused(): boolean { + const [focused, setFocused] = useState(true) + useFocusEffect( + useCallback(() => { + setFocused(true) + return () => setFocused(false) + }, []) + ) + return focused +} + +/** This phone's Orca Cloud enrollment; `undefined` while the keychain read is in flight. */ +export function useCloudEnrollment(): CloudEnrollment | null | undefined { + const [enrollment, setEnrollment] = useState(undefined) + useEffect(() => { + let live = true + const load = () => + void loadCloudDeviceIdentity() + .then((identity) => live && setEnrollment(identity.enrollment)) + .catch(() => live && setEnrollment(null)) + load() + const unsubscribe = subscribeCloudDeviceIdentity(load) + return () => { + live = false + unsubscribe() + } + }, []) + return enrollment +} + +export function useCloudWorkspaces(enrollment: CloudEnrollment | null | undefined) { + const sessionId = enrollment?.deviceSessionId ?? null + const removal = useCloudHostRemoval() + const focused = useScreenFocused() + return useQuery({ + queryKey: cloudWorkspacesQueryKey(sessionId), + enabled: sessionId !== null, + queryFn: async ({ signal }) => { + const plane = await loadCloudControlPlane() + if (!plane) { + return [] + } + try { + return await listCloudWorkspaces(plane, signal) + } catch (error) { + await forgetRevokedCloudSession(error, removal) + throw error + } + }, + // Fast while something changes state, slow otherwise, never while another screen is on top. + refetchInterval: (query) => + !focused + ? false + : query.state.data?.some((workspace) => TRANSITIONAL.has(workspace.status)) + ? 3_000 + : 20_000, + staleTime: 2_000 + }) +} + +export function useCloudWorkspace( + enrollment: CloudEnrollment | null | undefined, + sandboxId: string +) { + const list = useCloudWorkspaces(enrollment) + return { ...list, workspace: list.data?.find((entry) => entry.id === sandboxId) ?? null } +} + +/** Live uptime and spend for one workspace while its screen is open. */ +export function useCloudWorkspaceLiveStatus(sandboxId: string, enabled: boolean) { + const focused = useScreenFocused() + const removal = useCloudHostRemoval() + return useQuery({ + queryKey: ['orca-cloud', 'status', sandboxId], + enabled, + queryFn: async () => { + const plane = await loadCloudControlPlane() + try { + return plane ? await readCloudWorkspaceStatus(plane, sandboxId) : null + } catch (error) { + await forgetRevokedCloudSession(error, removal) + throw error + } + }, + refetchInterval: focused ? 15_000 : false, + retry: false + }) +} + +export function useInvalidateCloudWorkspaces(): () => void { + const client = useQueryClient() + return useCallback(() => void client.invalidateQueries({ queryKey: ['orca-cloud'] }), [client]) +} + +/** Puts a just-created workspace in the list so its screen never flashes "gone". */ +export function useSeedCloudWorkspace(): (sessionId: string, workspace: CloudWorkspace) => void { + const client = useQueryClient() + return useCallback( + (sessionId, workspace) => + client.setQueryData(cloudWorkspacesQueryKey(sessionId), (current) => [ + ...(current ?? []).filter((entry) => entry.id !== workspace.id), + workspace + ]), + [client] + ) +} diff --git a/mobile/src/home/MobileHomeCloudSection.tsx b/mobile/src/home/MobileHomeCloudSection.tsx new file mode 100644 index 00000000000..557b9c733eb --- /dev/null +++ b/mobile/src/home/MobileHomeCloudSection.tsx @@ -0,0 +1,297 @@ +import { ChevronRight, Cloud, Plus, RotateCw } from 'lucide-react-native' +import { Fragment, useState } from 'react' +import { ActivityIndicator, Pressable, Text, View } from 'react-native' +import { CloudStatusDot } from '../cloud/CloudStatusDot' +import type { CloudWorkspace } from '../cloud/cloud-control-plane-client' +import { CloudEnrollmentError, enrollCloudDevice } from '../cloud/cloud-device-enrollment' +import type { CloudEnrollment } from '../cloud/cloud-device-identity' +import { cloudRepoLine, cloudStatusLabel } from '../cloud/cloud-workspace-display' +import { useCloudWorkspaces } from '../cloud/use-cloud-workspaces' +import { formatTimeAgo } from '../worktree/agent-row-display' +import { useMobileTranslation } from '../i18n/use-mobile-translation' +import { colors } from '../theme/mobile-theme' +import { themedStyleSheet } from '../theme/themed-styles' +import { useMobileColorScheme } from '../theme/use-mobile-color-scheme' +import type { RpcClient } from '../transport/rpc/rpc-client-contract' +import { homeListStyles as styles } from './home-list-styles' +import { MobileHomeSectionHeading } from './MobileHomeSectionHeading' + +export type CloudEnrollDesktop = { name: string; client: RpcClient } + +/** Home's Cloud section: this phone's own Orca Cloud workspaces, reached with no desktop in the path. */ +export function MobileHomeCloudSection(props: { + enrollment: CloudEnrollment | null | undefined + /** A connected desktop that can lend its Orca sign-in once, to enroll this phone. */ + enrollDesktop: CloudEnrollDesktop | null + deviceLabel: string + onOpenWorkspace: (workspace: CloudWorkspace) => void + onNewWorkspace: () => void + onManageAccess: () => void +}) { + const t = useMobileTranslation() + useMobileColorScheme() + const query = useCloudWorkspaces(props.enrollment) + if (props.enrollment === undefined) { + return null + } + if (props.enrollment === null) { + return + } + const workspaces = (query.data ?? []).filter((workspace) => workspace.status !== 'archived') + const now = query.dataUpdatedAt || 0 + return ( + <> + 0 ? { count: workspaces.length } : {})} + aside={t('cloud.home.manage', 'Manage')} + onAsidePress={props.onManageAccess} + /> + + {query.isPending ? ( + + + + + + ) : query.isError && workspaces.length === 0 ? ( + [styles.actionRow, pressed && styles.rowPressed]} + onPress={() => void query.refetch()} + > + + + {t('cloud.home.retry', 'Couldn’t reach Orca Cloud. Try again')} + + + ) : workspaces.length === 0 ? ( + + + + {t('cloud.home.emptyTitle', 'No cloud workspaces yet')} + + + {t( + 'cloud.home.emptyDetail', + 'Start one here. It keeps running while this phone and your computer sleep.' + )} + + + + ) : ( + workspaces.map((workspace) => ( + + props.onOpenWorkspace(workspace)} + /> + + + )) + )} + {workspaces.length === 0 && !query.isPending ? ( + + ) : null} + [styles.actionRow, pressed && styles.rowPressed]} + onPress={props.onNewWorkspace} + > + + {t('cloud.home.new', 'New cloud workspace')} + + + + ) +} + +function SkeletonRow() { + return ( + + + + + + + + ) +} + +function CloudWorkspaceRow({ + workspace, + now, + onPress +}: { + workspace: CloudWorkspace + now: number + onPress: () => void +}) { + const t = useMobileTranslation() + const status = cloudStatusLabel(workspace.status, t) + // The repo is already the title for most workspaces; then the branch is what tells them apart. + const repoName = workspace.repo?.url + .replace(/\.git$/, '') + .split('/') + .findLast(Boolean) + const reference = now || workspace.createdAt + // With no branch to tell same-named workspaces apart, when each was made does. + const detail = + repoName === workspace.title + ? (workspace.repo?.branch ?? + t('cloud.home.createdAgo', 'Created {{ago}} ago', { + ago: formatTimeAgo(workspace.createdAt, reference) + })) + : cloudRepoLine(workspace) + const when = formatTimeAgo(workspace.lastActivityAt ?? workspace.createdAt, reference) + const line = [status, detail, when].filter(Boolean).join(' · ') + return ( + [styles.row, pressed && styles.rowPressed]} + onPress={onPress} + > + + + + + + + + + {workspace.title} + + + + {status} + + {[detail, when] + .filter(Boolean) + .map((part) => ` · ${part}`) + .join('')} + + + + + + + ) +} + +function CloudConnectRow({ + desktop, + deviceLabel +}: { + desktop: CloudEnrollDesktop | null + deviceLabel: string +}) { + const t = useMobileTranslation() + const [busy, setBusy] = useState(false) + const [error, setError] = useState(null) + const connect = async () => { + if (!desktop || busy) { + return + } + setBusy(true) + setError(null) + try { + await enrollCloudDevice({ desktop: desktop.client, desktopName: desktop.name, deviceLabel }) + } catch (failure) { + const kind = failure instanceof CloudEnrollmentError ? failure.failure : 'unreachable' + setError( + kind === 'signed-out' + ? t('cloud.connect.signedOut', 'Sign in to Orca on {{desktop}}, then try again.', { + desktop: desktop.name + }) + : kind === 'not-offered' + ? t( + 'cloud.connect.notOffered', + '{{desktop}} doesn’t have Orca Cloud yet. Update Orca there, then try again.', + { desktop: desktop.name } + ) + : t( + 'cloud.connect.unreachable', + 'Couldn’t reach Orca Cloud through {{desktop}}. Try again.', + { desktop: desktop.name } + ) + ) + } finally { + setBusy(false) + } + } + const subtitle = desktop + ? t( + 'cloud.connect.subtitle', + 'Open cloud workspaces from this phone, even when {{desktop}} is off. Uses its Orca sign-in once.', + { desktop: desktop.name } + ) + : t( + 'cloud.connect.noDesktop', + 'Open Orca on a computer you’re signed in to, then connect from here.' + ) + return ( + <> + + + [ + styles.row, + pressed && styles.rowPressed, + !desktop && styles.rowDisabled + ]} + onPress={() => void connect()} + > + + + + + + {t('cloud.connect.title', 'Connect Orca Cloud')} + + {subtitle} + {error ? ( + + {error} + + ) : null} + + + {busy ? ( + + ) : ( + + )} + + + + + ) +} + +const local = themedStyleSheet(() => ({ + dotRing: { + position: 'absolute', + right: -5, + bottom: -5, + padding: 2, + borderRadius: 999, + backgroundColor: colors.bgPanel + }, + empty: { paddingRight: 16 }, + skeletonText: { gap: 8, paddingVertical: 14 }, + skeletonBar: { height: 10, borderRadius: 5, backgroundColor: colors.bgRaised }, + connectTile: { backgroundColor: colors.accentBlue }, + connectTitle: { color: colors.accentBlue, fontWeight: '600' } +})) diff --git a/mobile/src/home/MobileHomeScreen.tsx b/mobile/src/home/MobileHomeScreen.tsx index b54010f1322..f8632c93411 100644 --- a/mobile/src/home/MobileHomeScreen.tsx +++ b/mobile/src/home/MobileHomeScreen.tsx @@ -35,6 +35,8 @@ import { HOME_TABS, type HomeTabId } from './tabs/home-tabs' import { MobileHomeAgentsTab } from './tabs/MobileHomeAgentsTab' import { MobileHomeAutomationsTab } from './tabs/MobileHomeAutomationsTab' import { MobileHomeFab } from './tabs/MobileHomeFab' +import { useCloudEnrollment } from '../cloud/use-cloud-workspaces' +import { useCloudHostIds } from '../cloud/use-cloud-host-ids' import { MobileHomeFeedTab } from './tabs/MobileHomeFeedTab' import { MobileHomeSettingsTab } from './tabs/MobileHomeSettingsTab' import { MobileHomeTabBar } from './tabs/MobileHomeTabBar' @@ -148,19 +150,23 @@ export function MobileHomeScreen() { actionTarget != null && data.hostStates[actionTarget.id] === 'connected' const paired = data.hostCatalogLoaded && data.hostCatalog.length > 0 + const cloudEnrollment = useCloudEnrollment() + const cloudHostIds = useCloudHostIds(cloudEnrollment) + // Cloud sandboxes are paired hosts too; "New workspace" targets computers only. + const connectedDesktops = data.connectedHosts.filter((host) => !cloudHostIds.has(host.id)) const automationsHostConnected = automationsHostId != null && data.hostStates[automationsHostId] === 'connected' const fab = tab === 'feed' && paired ? { label: t('home.bottomBar.newWorkspace', 'New workspace'), - disabled: data.connectedHosts.length === 0, + disabled: connectedDesktops.length === 0, // A desktop still on its first connect would make the hint flash at launch. hint: data.hostsConnecting ? undefined : t('home.bottomBar.connectFirst', 'Connect a desktop to start new work.'), onPress: () => - data.router.push(newWorkspaceOverlayHref(data.connectedHosts.map((host) => host.id))) + data.router.push(newWorkspaceOverlayHref(connectedDesktops.map((host) => host.id))) } : tab === 'automations' && automationsHostId && automationsHostConnected ? { @@ -191,6 +197,14 @@ export function MobileHomeScreen() { onOpenHostActions={openHostActions} onPairDesktop={pairDesktop} onOpenTasks={openTasks} + onOpenCloudWorkspace={(workspace) => + data.router.push({ + pathname: '/cloud/[sandboxId]', + params: { sandboxId: workspace.id } + }) + } + onNewCloudWorkspace={() => data.router.push('/cloud/new')} + onManageCloudAccess={() => data.router.push('/cloud/access')} /> ) : id === 'agents' ? ( void onPairDesktop: () => void onOpenTasks: (hostId: string) => void + onOpenCloudWorkspace: (workspace: CloudWorkspace) => void + onNewCloudWorkspace: () => void + onManageCloudAccess: () => void }) { const t = useMobileTranslation() const { isWideLayout, contentMaxWidth } = useResponsiveLayout() const { data } = props + const enrollment = useCloudEnrollment() + const cloudHostIds = useCloudHostIds(enrollment) + // Cloud VMs are paired hosts too, but they belong in Cloud, not under Desktops. + const desktopCatalog = useMemo( + () => data.sortedHostCatalog.filter((host) => !cloudHostIds.has(host.id)), + [data.sortedHostCatalog, cloudHostIds] + ) + const enrollDesktop = useMemo(() => { + const host = data.connectedHosts.find((entry) => !cloudHostIds.has(entry.id)) + const client = host + ? data.allClients.find((entry) => entry.hostId === host.id)?.client + : undefined + return host && client ? { name: host.personalName ?? host.name, client } : null + }, [data.connectedHosts, data.allClients, cloudHostIds]) const title = // Keychain reads can outlast the splash; show the title instead of a blank frame. if (!data.hostCatalogLoaded) { @@ -66,7 +89,7 @@ export function MobileHomeFeedTab(props: { hostAttempts={data.hostAttempts} hostLastConnected={data.hostLastConnected} hostConnections={data.hostConnections} - hosts={data.sortedHostCatalog} + hosts={desktopCatalog} hostStates={data.hostStates} needsYouByHost={data.needsYouByHost} worktreeInfo={data.worktreeInfo} @@ -74,6 +97,14 @@ export function MobileHomeFeedTab(props: { onOpenActions={props.onOpenHostActions} onPairDesktop={props.onPairDesktop} /> + diff --git a/mobile/src/i18n/locales/en.json b/mobile/src/i18n/locales/en.json index 750db86f234..75e5bb12c6f 100644 --- a/mobile/src/i18n/locales/en.json +++ b/mobile/src/i18n/locales/en.json @@ -201,7 +201,9 @@ "noMatch": { "title": "No matches", "clearSearch": "Clear search" - } + }, + "loading": "Loading…", + "tryAgain": "Try again" }, "host": { "edit": { @@ -4863,5 +4865,188 @@ "needsWindow": "Couldn’t update. Open Orca on {{desktop}}.", "failed": "Couldn’t update the agent list." } + }, + "cloud": { + "access": { + "revokeFailed": "Couldn’t remove that phone. Try again.", + "title": "Orca Cloud access", + "disconnectTitle": "Disconnect this phone?", + "removeTitle": "Remove {{device}}?", + "disconnectMessage": "This phone loses access to your cloud workspaces right away. They keep running.", + "removeMessage": "That phone loses access to your cloud workspaces on its next request.", + "disconnect": "Disconnect", + "remove": "Remove", + "thisPhone": "This phone", + "thisPhoneNote": "It signs its own requests with a key that never leaves it. No password or token is stored.", + "connectedVia": "Connected {{date}} through {{desktop}}", + "since": "Connected {{date}}", + "notConnected": "Not connected to Orca Cloud.", + "otherPhones": "Other phones", + "noOthers": "No other phones have access.", + "lastUsed": "Last used {{date}}", + "removeLabel": "Remove {{device}}", + "disconnectThis": "Disconnect this phone" + }, + "agent": { + "label": "Agent" + }, + "started": { + "title": "{{agent}} started with your prompt", + "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.", + "openTerminal": "Open terminal to sign in" + }, + "step": { + "create": "Start the sandbox and clone the repo", + "wake": "Wake the sandbox", + "grant": "Give this phone its own access", + "pair": "Connect, end-to-end encrypted", + "deliver": "Start the agent with your prompt" + }, + "prompt": { + "placeholder": "Describe the task", + "label": "Prompt", + "wakeSend": "Wake and send", + "send": "Send", + "wakingCaption": "Sends when it’s awake.", + "wakeCaption": "Sending wakes the sandbox first.", + "queued": "Waiting to send", + "notSent": "Not sent: {{reason}}", + "delivering": "Sending…", + "waitingConnect": "Waiting to connect", + "waiting": "Sends when the workspace wakes", + "remove": "Remove", + "queuedNote": "Kept on this phone until the workspace is awake, then sent end-to-end encrypted.", + "heading": "Ask an agent", + "errorProject": "The workspace’s project isn’t ready yet", + "errorTransport": "The connection dropped while sending", + "errorNotConnected": "This phone couldn’t reach the workspace", + "errorAgent": "That agent isn’t installed in this workspace", + "errorLaunch": "The agent didn’t start" + }, + "spend": { + "asleep": "{{cost}} so far · no sandbox hours while asleep", + "runningLarge": "{{cost}} so far · uses sandbox hours at 2× the rate", + "running": "{{cost}} so far · uses sandbox hours while running" + }, + "status": { + "waking": "Waking", + "cantConnect": "Can’t connect", + "archived": "Archived", + "creating": "Starting", + "running": "Running", + "stopping": "Going to sleep", + "sleeping": "Asleep", + "failed": "Couldn’t start" + }, + "actions": { + "sleep": "Sleep", + "sleepCaption": "Sandbox hours stop. Files are kept; agents restart when it wakes.", + "archive": "Archive" + }, + "sleep": { + "title": "Sleep {{title}}?", + "message": "Running agents stop now and restart when you wake it. Files are kept.", + "confirm": "Sleep" + }, + "archive": { + "title": "Archive {{title}}?", + "message": "It goes to sleep and keeps a snapshot you can unarchive. Unpushed work stays in that snapshot.", + "confirm": "Archive" + }, + "error": { + "queueFull": "Too many prompts are waiting. Send or remove some first.", + "generic": "Something went wrong. Your files are safe.", + "archiveUnavailable": "Archiving isn’t available yet. Put it to sleep instead.", + "archived": "This workspace is archived. Unarchive it on your computer first.", + "wakeTimeout": "The sandbox is taking longer than usual to wake. Your files are safe.", + "grant": "This phone couldn’t get access to the workspace. Your files are safe.", + "pairing": "The workspace is running, but this phone couldn’t connect to it. Your files are safe.", + "failed": "The workspace couldn’t start. Try again, or open it on your desktop.", + "revoked": "This phone’s Orca Cloud access ended. Connect it again from Home.", + "entitlement": "Out of sandbox hours. Ask an owner or admin to add hours, then try again.", + "network": "Couldn’t reach Orca Cloud. Check your connection and try again.", + "concurrency": "You’re at your team’s limit of running sandboxes. Put one to sleep or delete it, then try again.", + "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.", + "createUnknown": "Lost the connection while starting it. It may still appear in your list. Try again to pick it up.", + "rateLimited": "Too many new workspaces at once. Wait a minute, then try again.", + "create": "Couldn’t start your cloud workspace. Nothing was created, and you weren’t charged.", + "deliver": "The workspace is running, but a prompt didn’t start. Your files are safe." + }, + "workspace": { + "title": "Cloud workspace", + "missing": "This workspace is gone or you no longer have access." + }, + "action": { + "openSession": "Open session", + "settingUp": "Starting…", + "open": "Open workspace", + "wakeOpen": "Wake and open workspace" + }, + "phase": { + "delivering": "Starting your agent", + "waking": "Waking the sandbox", + "granting": "Getting this phone access", + "pairing": "Connecting securely", + "connected": "Connected" + }, + "details": { + "heading": "Details", + "repo": "Repository", + "machine": "Sandbox size" + }, + "size": { + "large": "Large (8 vCPU · 16 GB)", + "standard": "Standard (4 vCPU · 8 GB)", + "standardShort": "Standard", + "largeShort": "Large", + "standardSpec": "4 vCPU · 8 GB", + "largeSpec": "8 vCPU · 16 GB", + "largeNote": "Large uses sandbox hours at 2× the rate." + }, + "new": { + "title": "Create workspace", + "repoHeading": "Repository", + "repoInvalid": "Use a GitHub repository, like owner/repo.", + "repoNote": "Private repos need the Orca Cloud GitHub app, connected once from Orca on your computer. Leave empty for a blank project.", + "repoPlaceholder": "owner/repo", + "repoLabel": "GitHub repository", + "branchLabel": "Branch", + "branchPlaceholder": "Default branch", + "agentHeading": "First task", + "promptPlaceholder": "Optional. What should the agent start on?", + "sizeHeading": "Sandbox size", + "creating": "Starting your cloud workspace", + "createAndStart": "Start workspace and agent", + "create": "Start workspace" + }, + "line": { + "runningFor": "Up {{elapsed}}. Agents keep working when you close the app.", + "running": "Agents keep working when you close the app.", + "sleeping": "Files are kept, and waking takes about 5 seconds.", + "creating": "Starting the sandbox and cloning the repository.", + "waking": "Starting up. This usually takes about 5 seconds.", + "stopping": "Files are kept.", + "archived": "Unarchive it on your computer to work in it again.", + "failed": "The sandbox couldn’t start. Your files are safe." + }, + "home": { + "heading": "Cloud", + "manage": "Manage", + "loading": "Loading cloud workspaces…", + "retry": "Couldn’t reach Orca Cloud. Try again", + "emptyTitle": "No cloud workspaces yet", + "emptyDetail": "Start one here. It keeps running while this phone and your computer sleep.", + "new": "New cloud workspace", + "createdAgo": "Created {{ago}} ago", + "openWorkspace": "Open {{title}}, {{detail}}" + }, + "connect": { + "signedOut": "Sign in to Orca on {{desktop}}, then try again.", + "notOffered": "{{desktop}} doesn’t have Orca Cloud yet. Update Orca there, then try again.", + "unreachable": "Couldn’t reach Orca Cloud through {{desktop}}. Try again.", + "subtitle": "Open cloud workspaces from this phone, even when {{desktop}} is off. Uses its Orca sign-in once.", + "noDesktop": "Open Orca on a computer you’re signed in to, then connect from here.", + "title": "Connect Orca Cloud" + } } } From 8ca59fb6f58c2c39dab0bb646fec827e070fcd16 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 17:31:06 -0800 Subject: [PATCH 29/30] =?UTF-8?q?copy(cloud):=20W10=20=E2=80=94=20cap=20of?= =?UTF-8?q?f=20the=20pricing=20fine=20print;=20'You=20can=20run=20N';=20st?= =?UTF-8?q?art=20error=20matches=20the=20card?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/src/components/billing/plans/PlansView.tsx | 7 ++----- .../components/cloud-workspaces/cloud-workspace-errors.ts | 4 ++-- .../components/worktree-creation/vm-build-failure-copy.ts | 4 ++-- .../components/worktree-creation/vm-build-failure.test.ts | 2 +- src/renderer/src/i18n/locales/en.json | 6 +++--- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/renderer/src/components/billing/plans/PlansView.tsx b/src/renderer/src/components/billing/plans/PlansView.tsx index b92fd2d40e2..dee2a120e06 100644 --- a/src/renderer/src/components/billing/plans/PlansView.tsx +++ b/src/renderer/src/components/billing/plans/PlansView.tsx @@ -306,11 +306,8 @@ export function PlansView(props: PlansViewProps): React.JSX.Element {

{t( - 'auto.components.billing.plans.footHours4', - 'Hours reset every month, including on annual plans. Each seat can run {{value0}} sandboxes at once.', - // The cap is enforced by the control plane; the fine print states it before anyone - // reaches the limit card, while the Team card keeps to the marketing bullets. - { value0: String(offers.team.concurrentSandboxesPerSeat) } + 'auto.components.billing.plans.footHours3', + 'Hours reset every month, including on annual plans.' )}

diff --git a/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts b/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts index 46394567b9e..2d2b3dfdb54 100644 --- a/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts +++ b/src/renderer/src/components/cloud-workspaces/cloud-workspace-errors.ts @@ -16,8 +16,8 @@ export function describeError(error: string, reason?: string, t: Translate = tra const limit = sandboxLimitFromBilling(getLastBillingStatus()) return limit ? t( - 'auto.components.cloudWorkspaces.errorConcurrencyLimitCount', - "You're at your team's limit: your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.", + 'auto.components.cloudWorkspaces.errorConcurrencyLimitCount2', + 'Your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.', { value0: String(limit.team), value1: String(limit.perSeat) } ) : t( diff --git a/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts b/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts index f9ecc31b8a2..3ec2bcc5a72 100644 --- a/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts +++ b/src/renderer/src/components/worktree-creation/vm-build-failure-copy.ts @@ -143,8 +143,8 @@ export function vmBuildFailureCopy( ), body: sandboxLimit ? t( - 'auto.components.worktree.creation.VmBuildFailureCard.memberLimitBody', - 'Each person can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.', + 'auto.components.worktree.creation.VmBuildFailureCard.memberLimitBody2', + 'You can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.', { value0: String(sandboxLimit.perSeat) } ) : t( diff --git a/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts b/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts index 55c7f3b7dc0..e6dc41a329c 100644 --- a/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts +++ b/src/renderer/src/components/worktree-creation/vm-build-failure.test.ts @@ -115,7 +115,7 @@ describe('vmBuildFailureCopy', () => { 'Your team can run 12 sandboxes at once (3 per seat). Put one to sleep or delete it' ) expect(copy({ kind: 'memberConcurrency' }, { perSeat: 3, team: 12 })).toContain( - 'Each person can run 3 sandboxes at once. Put one to sleep or delete it' + 'You can run 3 sandboxes at once. Put one to sleep or delete it' ) expect(copy({ kind: 'concurrency' }, null)).toMatch(/^Put one to sleep or delete it/) }) diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index 1182ccaade2..207e86bc140 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -4109,7 +4109,7 @@ "teamLimitTitle": "You’re at your team’s limit of running sandboxes", "limitBody": "Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", "teamLimitBody": "Your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", - "memberLimitBody": "Each person can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", + "memberLimitBody2": "You can run {{value0}} sandboxes at once. Put one to sleep or delete it, then try again. Nothing was created, and you weren’t charged.", "memberLimitTitle": "You’re at your limit of running sandboxes", "githubNotConnectedTitle": "GitHub isn’t connected for {{repo}}", "githubNotConnectedBody": "Orca Cloud clones your code from GitHub and needs access to this repository first. Connect GitHub, then try again. Nothing was created, and you weren’t charged.", @@ -20542,7 +20542,7 @@ "vpSecretsTitle3": "API keys stay out of the sandbox", "billedYearlyAmount2": "{{value0}} per seat, billed yearly", "entPriced2": "Priced for your team", - "footHours4": "Hours reset every month, including on annual plans. Each seat can run {{value0}} sandboxes at once.", + "footHours3": "Hours reset every month, including on annual plans.", "perSeatPerMonth": "per seat per month", "seatsPerMember_one": "{{count}} seat, one per team member", "seatsPerMember_other": "{{count}} seats, one per team member", @@ -20678,7 +20678,7 @@ "emptyTeamTitle": "No team workspaces yet", "environmentName": "{{value0}} (cloud)", "errorConcurrencyLimit": "You're at your team's limit of running sandboxes. Put one to sleep or delete it, then try again.", - "errorConcurrencyLimitCount": "You're at your team's limit: your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.", + "errorConcurrencyLimitCount2": "Your team can run {{value0}} sandboxes at once ({{value1}} per seat). Put one to sleep or delete it, then try again.", "errorMemberConcurrencyLimit": "You're at your limit of running sandboxes. Put one to sleep or delete it, then try again.", "errorConflict": "This workspace is still starting or going to sleep. Nothing was changed. Try again in a moment.", "errorConflictInline": "This workspace is still starting or going to sleep. Nothing was changed.", From 04e6a800d87878fa47a72cbe5a5565361c1c5997 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:33:04 -0800 Subject: [PATCH 30/30] fix(cloud): env builds finish without a reader: bounded wait=true exit probe (#123) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(sandbox-control): read the orgRole claim that Orca Cloud auth mints (#29) Auth mints the live org role as orgRole on sandbox tokens (same claim as billing tokens, stablyai/orca-cloud#695); the verifier read org_role, so admins would never have been recognized. * wip(cloud): environment builds control plane * wip(cloud): environment build boot path, staleness, routes * wip(cloud): env build tests, anonymous staleness fallback * wip(cloud): migration 0008 (claimed), quieter recovered clone errors * fix(cloud): env build review: interrupted-snapshot recovery, bounded log reads, secret redaction, activation guard, hook/prelude fixes * fix(cloud): wait briefly for the build exit code after its log ends; print the base image and package manager * wip(desktop): org environments client, IPC, CLI, settings UI, orcad disk usage + low-disk banner * perf(cloud): smoke-boot each environment build before activating it; skip the redundant prelude fetch * fix(cloud): a GitHub token error fails the build as github_unavailable instead of cloning anonymously * perf(cloud): warm pool VMs per hot environment build, so a create from an org environment skips the provider boot * wip(desktop): cloud environment UI round-3 fixes * fix(cloud): pool-per-build review: smoke race safety, provider hiccups skip smoke, usage-based hot builds with caps, bounded boots, index * fix(desktop): Configure with an agent hands the prompt over at launch (trust dialog swallowed the pasted Enter); CLI builds table widths * polish(desktop): W10 P2s — InlineError action slot, shared focus-ring, copy * feat(cloud): build event stream heartbeat every 20 s * fix(desktop): FE review — stable virtualizer, shadow-buffer replays, heartbeat-aware build watch, CLI follower restarts, repo import path checks, poll races * feat(cloud): meter environment build sandbox time to the member who started it; no setup-script nudge for workspaces booted from an env build * fix(cloud): environment builds progress server-side on a Durable Object alarm, not only while someone streams their log * fix(cloud): advance env builds with a bounded wait=true exit probe (Vercel reports exit codes only to waiting readers) --- .../sandbox-control/src/environment-builds.ts | 11 +++++++- .../src/golden-image-provider.ts | 7 ++++- .../sandbox-control/src/vercel-sandbox-api.ts | 26 ++++++++++++++----- .../apps/sandbox-control/test/fake-vercel.ts | 5 ++-- .../test/vercel-sandbox-api.test.ts | 26 +++++++++++++++++++ 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/cloud/apps/sandbox-control/src/environment-builds.ts b/cloud/apps/sandbox-control/src/environment-builds.ts index 00c5e9019a0..08afb44b783 100644 --- a/cloud/apps/sandbox-control/src/environment-builds.ts +++ b/cloud/apps/sandbox-control/src/environment-builds.ts @@ -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 @@ -371,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 diff --git a/cloud/apps/sandbox-control/src/golden-image-provider.ts b/cloud/apps/sandbox-control/src/golden-image-provider.ts index 2af61a69069..adb13f6e0d9 100644 --- a/cloud/apps/sandbox-control/src/golden-image-provider.ts +++ b/cloud/apps/sandbox-control/src/golden-image-provider.ts @@ -22,7 +22,12 @@ export type GoldenImageProviderApi = { params: { command: string; args?: string[]; env?: Record; timeoutMs: number } ): Promise /** 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. diff --git a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts index ddc665a3948..e8715bcac9b 100644 --- a/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts +++ b/cloud/apps/sandbox-control/src/vercel-sandbox-api.ts @@ -64,7 +64,7 @@ export function createVercelSandboxApi( async function request( method: string, path: string, - options: { query?: Record; body?: unknown } = {} + options: { query?: Record; body?: unknown; signal?: AbortSignal } = {} ): Promise { 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 } }, diff --git a/cloud/apps/sandbox-control/test/fake-vercel.ts b/cloud/apps/sandbox-control/test/fake-vercel.ts index 9a654e8c342..9d409b52920 100644 --- a/cloud/apps/sandbox-control/test/fake-vercel.ts +++ b/cloud/apps/sandbox-control/test/fake-vercel.ts @@ -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) diff --git a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts index 429db260c30..320df9435a6 100644 --- a/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts +++ b/cloud/apps/sandbox-control/test/vercel-sandbox-api.test.ts @@ -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((_, 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({