From 2892ba1f4789828176429aec9a128a11dafdbc9a Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 08:08:35 -0800 Subject: [PATCH 1/2] fix(cloud-integration): lint and typecheck green on HEAD; premerge gate script - cloud-transfer-steps runs git through gitExecFileAsync (no direct child_process); VM replies parsed with zod. - Casts replaced by types, guards and zod across hosted-sandbox, cloud-api, cloud-api settings and tests. - Offline cache and message-queue store use node:fs/promises (main-sync-fs ratchet); the offline service's 30 s setInterval becomes a backing-off beat that runs only while prompts are queued; renderer intervals use installWindowVisibilityInterval. - Flaky-test gates: fixed timestamps, vi.waitFor, no sleeps in cloud-message-queue/cloud-transfer tests. - shadcn restyles become primitive variants (Skeleton shape/motion, Button default-held, Textarea compact/bare, InlineError toast layout, Popover focusRing); exhaustive switch; derived pending state in the run-target hook. - Localization: routine labels/placeholders and 'Orca Cloud is unavailable.' through t(); cloudSecrets plurals pass the singular as defaultValue_one. - config/scripts/cloud-integration/premerge-check.sh: merge-into-integration gate (typecheck + full lint + tests). --- .../cloud-integration/premerge-check.sh | 24 ++ src/main/cloud-api/cloud-api-client.test.ts | 87 +++--- src/main/cloud-api/cloud-api-client.ts | 6 +- src/main/cloud-api/cloud-api-ipc.ts | 1 + .../cloud-message-queue-store.ts | 111 ++++++- .../cloud-message-queue.test.ts | 58 ++-- .../hosted-sandbox/cloud-message-queue.ts | 61 ++-- .../hosted-sandbox/cloud-offline-beat.test.ts | 65 +++++ src/main/hosted-sandbox/cloud-offline-beat.ts | 59 ++++ .../cloud-offline-cache-types.ts | 60 +++- .../cloud-offline-cache.test.ts | 64 +++-- .../hosted-sandbox/cloud-offline-cache.ts | 270 +++++++++--------- .../hosted-sandbox/cloud-offline-service.ts | 73 +++-- .../hosted-sandbox/cloud-offline-snapshot.ts | 171 +++++++---- .../hosted-sandbox/cloud-transfer-steps.ts | 87 +++--- .../hosted-sandbox/cloud-transfer.test.ts | 11 +- src/main/hosted-sandbox/cloud-transfer.ts | 4 +- ...me-environment-request-preparation.test.ts | 24 +- .../CloudRemoteDesktopToast.tsx | 25 +- .../new-workspace/RunTargetCombobox.test.tsx | 7 +- .../use-run-target-orca-cloud.ts | 17 +- .../orca-cloud/CloudArchivedWorkspaceView.tsx | 4 +- .../orca-cloud/CloudWakingOverlay.tsx | 8 +- .../orca-cloud/CloudWorkspaceMark.tsx | 19 +- .../orca-cloud/CloudWorkspaceNotice.tsx | 4 +- .../orca-cloud/CloudWorkspaceStatusPanel.tsx | 11 +- .../orca-cloud/cloud-focus-return.test.ts | 5 +- .../orca-cloud/cloud-focus-return.ts | 2 +- .../offline/CloudOfflineChatPanel.tsx | 7 +- .../offline/CloudWorkspaceOfflineView.tsx | 128 ++++----- .../settings/cloud-api/ApproveAppDialog.tsx | 5 +- .../settings/cloud-api/CloudRoutineRow.tsx | 4 +- .../settings/cloud-api/CreateApiKeyDialog.tsx | 10 +- .../cloud-api/RoutineAgentSection.tsx | 10 +- .../cloud-api/RoutineEditorDialog.tsx | 5 +- .../settings/cloud-api/RoutineRunHistory.tsx | 16 +- .../cloud-api/RoutineSourceSection.tsx | 5 +- .../cloud-api/RoutineTriggerFields.tsx | 23 +- .../settings/cloud-api/cloud-api-format.ts | 6 +- .../settings/cloud-api/routine-draft.ts | 38 ++- .../cloud-secrets/CloudSecretsPasteEnv.tsx | 11 +- .../cloud-secrets/SecretValueInput.tsx | 3 +- src/renderer/src/components/ui/button.tsx | 2 + .../src/components/ui/inline-error.tsx | 16 +- src/renderer/src/components/ui/popover.tsx | 5 + src/renderer/src/components/ui/skeleton.tsx | 27 +- src/renderer/src/components/ui/textarea.tsx | 4 + .../src/i18n/en-runtime-required.json | 4 + src/renderer/src/i18n/locales/en.json | 15 +- .../src/lib/orca-cloud-cancel-cleanup.test.ts | 7 +- .../orca-cloud-workspace-actions.ts | 22 +- .../orca-cloud/orca-cloud-workspace-state.ts | 2 - .../orca-cloud-workspace-store-wake.test.ts | 19 +- .../orca-cloud-workspace-store.test.ts | 8 +- src/shared/cloud-api/cloud-api-primitives.ts | 2 +- 55 files changed, 1146 insertions(+), 596 deletions(-) create mode 100755 config/scripts/cloud-integration/premerge-check.sh create mode 100644 src/main/hosted-sandbox/cloud-offline-beat.test.ts create mode 100644 src/main/hosted-sandbox/cloud-offline-beat.ts diff --git a/config/scripts/cloud-integration/premerge-check.sh b/config/scripts/cloud-integration/premerge-check.sh new file mode 100755 index 00000000000..5b683312d81 --- /dev/null +++ b/config/scripts/cloud-integration/premerge-check.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Gate for merging into cloud/integration (no CI on nwparker/orca-private): run from your branch's clone. +# Merges the current private/cloud/integration into a throwaway worktree with your branch, then requires +# typecheck, the full lint suite (pre-commit hooks only lint staged files) and the tests your branch changed. +set -euo pipefail +branch=$(git rev-parse --abbrev-ref HEAD) +git fetch -q private +cloud/integration:refs/remotes/private/cloud/integration +dir=$(mktemp -d "${TMPDIR:-/tmp}/cloud-premerge-XXXX") +trap 'git worktree remove --force "$dir" >/dev/null 2>&1 || true' EXIT +git worktree add -q --detach "$dir" HEAD +cd "$dir" +git merge -q --no-edit private/cloud/integration +pnpm install --frozen-lockfile >/dev/null +pnpm typecheck +pnpm lint +tests=$(git diff --name-only --diff-filter=AM private/cloud/integration...HEAD -- 'src/*.test.ts' 'src/*.test.tsx' | tr '\n' ' ') +if [[ -n "$tests" ]]; then + node config/scripts/native-build/node-addons/ensure-native-runtime.mjs --runtime=node >/dev/null + npx vitest run --config config/vitest.config.ts $tests +fi +for app in $(git diff --name-only private/cloud/integration...HEAD -- cloud/apps | cut -d/ -f1-3 | sort -u); do + (cd "$app" && npx vitest run) +done +echo "premerge OK: $branch merges cleanly into cloud/integration with typecheck, lint and tests green." diff --git a/src/main/cloud-api/cloud-api-client.test.ts b/src/main/cloud-api/cloud-api-client.test.ts index 45a9d7767f5..783b62effa4 100644 --- a/src/main/cloud-api/cloud-api-client.test.ts +++ b/src/main/cloud-api/cloud-api-client.test.ts @@ -27,6 +27,17 @@ function client(fetchImpl: typeof fetch, getAccessToken = async () => 'tok_secre }) } +async function cloudApiErrorOf(promise: Promise): Promise { + const caught = await promise.then( + () => null, + (error: unknown) => error + ) + if (!(caught instanceof CloudApiError)) { + throw new Error(`expected a CloudApiError, got ${String(caught)}`) + } + return caught +} + async function codeOf(promise: Promise): Promise { try { await promise @@ -41,45 +52,48 @@ async function codeOf(promise: Promise): Promise { describe('createCloudApiClient', () => { it('sends the bearer token, JSON body and query, and validates the answer', async () => { - const fetchImpl = vi.fn(async () => + const fetchImpl = vi.fn(async () => respond(200, { keys: [KEY], availableScopes: ['audit:read'] }) ) - const keys = await client(fetchImpl as unknown as typeof fetch).listApiKeys() + const keys = await client(fetchImpl).listApiKeys() expect(keys.keys[0]?.prefix).toBe('orca_sk_Ab3x') // Unknown scopes from a newer server are dropped, not fatal. expect(keys.keys[0]?.scopes).toEqual(['workspaces:read']) - const [url, init] = fetchImpl.mock.calls[0] as unknown as [string, RequestInit] + const [url, init] = fetchImpl.mock.calls[0] ?? [] expect(url).toBe('https://cp.example.dev/v1/api-keys') - expect((init.headers as Record).authorization).toBe('Bearer tok_secret') - expect(init.redirect).toBe('error') + expect(new Headers(init?.headers).get('authorization')).toBe('Bearer tok_secret') + expect(init?.redirect).toBe('error') - const created = vi.fn(async () => + const created = vi.fn(async () => respond(201, { key: KEY, secret: `orca_sk_${'a'.repeat(32)}` }) ) - await client(created as unknown as typeof fetch).createApiKey({ + await client(created).createApiKey({ + name: 'CI', + scopes: ['workspaces:read'] + }) + const [, createInit] = created.mock.calls[0] ?? [] + expect(createInit?.method).toBe('POST') + expect(JSON.parse(String(createInit?.body))).toEqual({ name: 'CI', scopes: ['workspaces:read'] }) - const [, createInit] = created.mock.calls[0] as unknown as [string, RequestInit] - expect(createInit.method).toBe('POST') - expect(JSON.parse(String(createInit.body))).toEqual({ name: 'CI', scopes: ['workspaces:read'] }) - const runs = vi.fn(async () => respond(200, { runs: [], nextBefore: null })) - await client(runs as unknown as typeof fetch).listRoutineRuns('rt/1', { limit: 20, before: 99 }) - expect((runs.mock.calls[0] as unknown as [string])[0]).toBe( + const runs = vi.fn(async () => respond(200, { runs: [], nextBefore: null })) + await client(runs).listRoutineRuns('rt/1', { limit: 20, before: 99 }) + expect(runs.mock.calls[0]?.[0]).toBe( 'https://cp.example.dev/v1/routines/rt%2F1/runs?limit=20&before=99' ) }) it('rejects a malformed answer as invalid-response', async () => { - const fetchImpl = vi.fn(async () => respond(200, { keys: 'nope' })) - expect(await codeOf(client(fetchImpl as unknown as typeof fetch).listApiKeys())).toBe( - 'invalid-response' + const fetchImpl = vi.fn(async () => respond(200, { keys: 'nope' })) + expect(await codeOf(client(fetchImpl).listApiKeys())).toBe('invalid-response') + const badSecret = vi.fn(async () => + respond(201, { key: KEY, secret: 'not-a-key' }) ) - const badSecret = vi.fn(async () => respond(201, { key: KEY, secret: 'not-a-key' })) expect( await codeOf( - client(badSecret as unknown as typeof fetch).createApiKey({ + client(badSecret).createApiKey({ name: 'x', scopes: ['audit:read'] }) @@ -88,51 +102,44 @@ describe('createCloudApiClient', () => { }) it('maps network and token failures without leaking the token', async () => { - const offline = vi.fn(async () => { + const offline = vi.fn(async () => { throw new TypeError('fetch failed tok_secret') }) - const error = await client(offline as unknown as typeof fetch) - .me() - .catch((caught: unknown) => caught) - expect(error).toBeInstanceOf(CloudApiError) - expect((error as CloudApiError).code).toBe('network') - expect(String((error as CloudApiError).message)).not.toContain('tok_secret') + const error = await cloudApiErrorOf(client(offline).me()) + expect(error.code).toBe('network') + expect(error.message).not.toContain('tok_secret') - const noToken = client(vi.fn() as unknown as typeof fetch, async () => { + const noToken = client(vi.fn(), async () => { throw new Error('signed out') }) expect(await codeOf(noToken.me())).toBe('signed-out') }) it('treats a missing routines route as not-available but a missing routine as not-found', async () => { - const missing = vi.fn(async () => respond(404, { error: 'not_found' })) - const c = client(missing as unknown as typeof fetch) + const missing = vi.fn(async () => respond(404, { error: 'not_found' })) + const c = client(missing) expect(await codeOf(c.listRoutines())).toBe('not-available') expect(await codeOf(c.getRoutine('rt_1'))).toBe('not-found') }) it('carries retry-after on rate limits and accepts empty DELETE bodies', async () => { - const limited = vi.fn(async () => + const limited = vi.fn(async () => respond(429, { error: 'rate_limited' }, { 'retry-after': '7' }) ) - const error = (await client(limited as unknown as typeof fetch) - .listApiKeys() - .catch((caught: unknown) => caught)) as CloudApiError + const error = await cloudApiErrorOf(client(limited).listApiKeys()) expect(error.code).toBe('rate-limited') expect(error.retryAfterSeconds).toBe(7) - const deleted = vi.fn(async () => new Response(null, { status: 200 })) - await expect( - client(deleted as unknown as typeof fetch).revokeApiKey('key_1') - ).resolves.toBeUndefined() + const deleted = vi.fn(async () => new Response(null, { status: 200 })) + await expect(client(deleted).revokeApiKey('key_1')).resolves.toBeUndefined() }) }) describe('response hardening', () => { it('keeps a freshly minted secret even if the key metadata drifted', async () => { const secret = `orca_sk_${'b'.repeat(32)}` - const fetchImpl = vi.fn(async () => respond(201, { key: { id: 7 }, secret })) - const created = await client(fetchImpl as unknown as typeof fetch).createApiKey({ + const fetchImpl = vi.fn(async () => respond(201, { key: { id: 7 }, secret })) + const created = await client(fetchImpl).createApiKey({ name: 'x', scopes: ['workspaces:read'] }) @@ -154,9 +161,9 @@ describe('response hardening', () => { finishedAt: 2, durationMs: 1 } - const fetchImpl = vi.fn(async () => respond(202, { run })) + const fetchImpl = vi.fn(async () => respond(202, { run })) // A bad link is dropped, never opened, and doesn't fail the whole response. - const { run: parsed } = await client(fetchImpl as unknown as typeof fetch).runRoutine('rt_1') + const { run: parsed } = await client(fetchImpl).runRoutine('rt_1') expect(parsed.result?.prUrl).toBeUndefined() expect(parsed.status).toBe('succeeded') }) diff --git a/src/main/cloud-api/cloud-api-client.ts b/src/main/cloud-api/cloud-api-client.ts index 7c29ae6bb8a..7b2a1b83390 100644 --- a/src/main/cloud-api/cloud-api-client.ts +++ b/src/main/cloud-api/cloud-api-client.ts @@ -61,10 +61,12 @@ function readServerError(body: unknown): { error?: string; reason?: string } { if (typeof body !== 'object' || body === null) { return {} } - const record = body as Record const pick = (value: unknown): string | undefined => typeof value === 'string' ? value.slice(0, MAX_DETAIL_CHARS) : undefined - return { error: pick(record.error), reason: pick(record.reason) } + return { + error: pick('error' in body ? body.error : undefined), + reason: pick('reason' in body ? body.reason : undefined) + } } /** Maps a non-2xx control-plane answer to a stable code the renderer can word. */ diff --git a/src/main/cloud-api/cloud-api-ipc.ts b/src/main/cloud-api/cloud-api-ipc.ts index e69115d6a59..494cac86df7 100644 --- a/src/main/cloud-api/cloud-api-ipc.ts +++ b/src/main/cloud-api/cloud-api-ipc.ts @@ -40,6 +40,7 @@ function call( } parsed.push(result.data) } + // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: parsed[i] is schemas[i]'s output and schemas is typed per A[i]; TS cannot prove a tuple built in a loop. return callCloudApi((client) => run(client, ...(parsed as A))) } } diff --git a/src/main/hosted-sandbox/cloud-message-queue-store.ts b/src/main/hosted-sandbox/cloud-message-queue-store.ts index 7d98869cbe1..1b29d35b1da 100644 --- a/src/main/hosted-sandbox/cloud-message-queue-store.ts +++ b/src/main/hosted-sandbox/cloud-message-queue-store.ts @@ -1,6 +1,7 @@ // Queued prompts on disk: sealed with the OS keychain; without one they live in memory only. -import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs' +import { mkdir, readFile, rename, rm, writeFile } from 'node:fs/promises' import { dirname, join } from 'node:path' +import { z } from 'zod' import type { CloudQueuedPrompt } from '../../shared/hosted-sandbox/cloud-workspace-offline-api' import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope' import type { OfflineCacheCodec } from './cloud-offline-cache' @@ -13,38 +14,103 @@ export type StoredPrompt = CloudQueuedPrompt & { nextAttemptAt: number } -export function readSealedPrompts(path: string, codec: OfflineCacheCodec | null): StoredPrompt[] { +// The sealed file is ours, but only its shape is checked here; a prompt that does not match is dropped. +const StoredPromptSchema = z.looseObject({ + id: z.string(), + clientOperationId: z.string(), + workspaceId: z.string(), + sessionId: z.string(), + text: z.string(), + state: z.enum(['queued', 'sending', 'delivered', 'failed', 'unconfirmed']), + createdAt: z.number(), + updatedAt: z.number(), + failures: z.number(), + nextAttemptAt: z.number(), + error: z.string().optional() +}) +const SealedQueueSchema = z.object({ version: z.literal(2), prompts: z.array(z.unknown()) }) + +export async function readSealedPrompts( + path: string, + codec: OfflineCacheCodec | null +): Promise { if (!codec) { return [] } try { - const parsed = JSON.parse(codec.decrypt(readFileSync(path))) as { - version: 2 - prompts: StoredPrompt[] + const parsed = SealedQueueSchema.safeParse(JSON.parse(codec.decrypt(await readFile(path)))) + if (!parsed.success) { + return [] } - return parsed.version === 2 && Array.isArray(parsed.prompts) ? parsed.prompts : [] + return parsed.data.prompts.flatMap((raw) => { + const prompt = StoredPromptSchema.safeParse(raw) + return prompt.success ? [prompt.data] : [] + }) } catch { return [] } } -export function writeSealedPrompts( +export async function writeSealedPrompts( path: string, codec: OfflineCacheCodec | null, prompts: readonly StoredPrompt[] -): void { +): Promise { if (!codec) { return } - mkdirSync(dirname(path), { recursive: true, mode: 0o700 }) + const sealed = codec.encrypt(JSON.stringify({ version: 2, prompts })) + await mkdir(dirname(path), { recursive: true, mode: 0o700 }) const temp = `${path}.${process.pid}.tmp` - writeFileSync(temp, codec.encrypt(JSON.stringify({ version: 2, prompts })), { mode: 0o600 }) - renameSync(temp, path) + await writeFile(temp, sealed, { mode: 0o600 }) + await rename(temp, path) } /** The first build kept the queue as plaintext JSON beside the sealed file; it is never read. */ -export function removeLegacyPlaintextQueue(path: string): void { - rmSync(join(dirname(path), 'orca-cloud-queued-prompts.json'), { force: true }) +export function removeLegacyPlaintextQueue(path: string): Promise { + return rm(join(dirname(path), 'orca-cloud-queued-prompts.json'), { force: true }) +} + +/** The sealed queue file: read once, then every write and removal in call order. */ +export function createSealedPromptDisk( + path: string, + codec: OfflineCacheCodec | null, + log: (line: string) => void +) { + const load = (async () => { + await removeLegacyPlaintextQueue(path).catch(() => undefined) + return readSealedPrompts(path, codec) + })() + // Nothing is written before the sealed file has been read. + let tail: Promise = load + function serial(task: () => Promise): Promise { + const run = tail.then(task) + tail = run.catch((error: unknown) => + log(`[orca-cloud] queued prompts write failed: ${String(error)}`) + ) + return run + } + return { + load, + write: (prompts: readonly StoredPrompt[]) => + serial(() => writeSealedPrompts(path, codec, prompts)), + remove: () => + serial(async () => { + await removeLegacyPlaintextQueue(path) + await rm(path, { force: true }) + }), + flushed: () => serial(async () => {}) + } +} + +export function earliestOwedAttempt(prompts: readonly StoredPrompt[]): number | null { + let earliest: number | null = null + for (const prompt of prompts) { + if (prompt.state === 'queued' || prompt.state === 'sending') { + earliest = earliest === null ? prompt.nextAttemptAt : Math.min(earliest, prompt.nextAttemptAt) + } + } + return earliest } /** Refusals that mean "not now", not "never": the same operation is sent again. */ @@ -87,11 +153,24 @@ export async function currentFence( ? { refused: code } : null } - const result = response.result as { ok: boolean; page?: { fence?: number }; fence?: number } + const parsed = FenceResultSchema.safeParse(response.result) + const result = parsed.success ? parsed.data : { ok: false } const fence = result.page?.fence ?? (result.ok ? undefined : result.fence) return typeof fence === 'number' ? fence : null } +const FenceResultSchema = z.looseObject({ + ok: z.boolean(), + page: z.looseObject({ fence: z.number().optional() }).optional(), + fence: z.number().optional() +}) + +/** `agentSession.send` answer: delivered, or refused with a code. */ +export const SendResultSchema = z.looseObject({ + ok: z.boolean(), + refusal: z.looseObject({ code: z.string().optional(), message: z.string().optional() }).optional() +}) + export type WorkspaceAvailability = | { kind: 'up'; environmentId: string } | { kind: 'asleep'; environmentId: string } @@ -133,4 +212,8 @@ export type CloudMessageQueue = { failPending(workspaceId: string, reason: string): void /** Sign-out / profile switch: forget everything. */ clear(): void + /** Resolves once every disk write requested so far has finished (shutdown, tests). */ + flushed(): Promise + /** Earliest background attempt still owed (ms epoch), or null when nothing is pending. */ + nextAttemptAt(): number | null } diff --git a/src/main/hosted-sandbox/cloud-message-queue.test.ts b/src/main/hosted-sandbox/cloud-message-queue.test.ts index 609caf86199..fad4f9d05cd 100644 --- a/src/main/hosted-sandbox/cloud-message-queue.test.ts +++ b/src/main/hosted-sandbox/cloud-message-queue.test.ts @@ -18,18 +18,14 @@ import { import type { OfflineCacheCodec } from './cloud-offline-cache' const SESSION = '0f8e7c1a-2b3d-4e5f-8a9b-0c1d2e3f4a5b' +/** Fixed test epoch: operation ids and the host's admission window both read this clock. */ +const T0 = 1_800_000_000_000 const codec: OfflineCacheCodec = { encrypt: (plain) => Buffer.from(Buffer.from(plain, 'utf8').map((b) => b ^ 0x5a)), decrypt: (sealed) => Buffer.from(sealed.map((b) => b ^ 0x5a)).toString('utf8') } -type Envelope = { - clientOperationId: string - expectedRuntimeFence: number - payloadFingerprint: string -} - /** * A host that admits operations with the real ledger rule (id format, age, replay by id) and * validates params with the real send schema. @@ -59,7 +55,7 @@ function realishHost(clock: () => number) { } } const parsed = SendParams.parse(params) - const envelope = parsed.envelope as Envelope + const envelope = parsed.envelope const decision = evaluateAgentSessionOperation({ rows, callerKey: 'desktop', @@ -78,9 +74,10 @@ function realishHost(clock: () => number) { } if (decision.decision === 'admit') { rows.set(agentSessionOperationKey('desktop', envelope.clientOperationId), decision.row) + const block = parsed.body.blocks[0] effects.push({ - text: (parsed.body.blocks[0] as { text: string }).text, - fence: envelope.expectedRuntimeFence + text: block?.type === 'text' ? block.text : '', + fence: envelope.expectedRuntimeFence ?? 0 }) } if (loseNextReply) { @@ -113,7 +110,7 @@ function setup( clock?: { now: number } } = {} ) { - const clock = opts.clock ?? { now: Date.now() } + const clock = opts.clock ?? { now: T0 } const path = opts.path ?? join(mkdtempSync(join(tmpdir(), 'orca-q-')), 'q.bin') const host = opts.host ?? realishHost(() => clock.now) let asleep = true @@ -132,14 +129,11 @@ function setup( call: host.call, log: () => {} }) - const settle = () => new Promise((resolve) => setTimeout(resolve, 0)) - return { queue, host, wake, path, clock, settle, sleep: () => (asleep = true) } + return { queue, host, wake, path, clock, sleep: () => (asleep = true) } } async function until(check: () => boolean) { - for (let i = 0; i < 100 && !check(); i++) { - await new Promise((resolve) => setTimeout(resolve, 1)) - } + await vi.waitFor(() => expect(check()).toBe(true)) } describe('cloud message queue', () => { @@ -155,15 +149,18 @@ describe('cloud message queue', () => { }) it('re-mints a never-sent id before it ages out of the host admission window', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) - const place = { current: { kind: 'asleep', environmentId: 'env' } as WorkspaceAvailability } + const place: { current: WorkspaceAvailability } = { + current: { kind: 'asleep', environmentId: 'env' } + } const parked = setup({ clock, host, place: () => place.current }) parked.wake.mockResolvedValueOnce(false) await parked.queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'a day later' }) // The wake failed: the prompt waits, queued, for the VM to be up. await until(() => parked.wake.mock.calls.length === 1) - await new Promise((resolve) => setTimeout(resolve, 5)) + // Joins the in-flight pass (never wakes on its own). + await parked.queue.drive('w') expect(parked.queue.list('w')[0]!.state).toBe('queued') clock.now += AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS + 60_000 place.current = { kind: 'up', environmentId: 'env' } @@ -177,9 +174,10 @@ describe('cloud message queue', () => { first.wake.mockResolvedValueOnce(false) await first.queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'after restart' }) await until(() => first.wake.mock.calls.length === 1) - await new Promise((resolve) => setTimeout(resolve, 5)) + await first.queue.drive('w') + await first.queue.flushed() expect(readFileSync(first.path).toString('utf8')).not.toContain('after restart') - const host = realishHost(() => Date.now()) + const host = realishHost(() => first.clock.now) let up = false const second = setup({ path: first.path, @@ -187,6 +185,7 @@ describe('cloud message queue', () => { place: () => up ? { kind: 'up', environmentId: 'env' } : { kind: 'asleep', environmentId: 'env' } }) + await second.queue.flushed() expect(second.queue.list('w').map((p) => p.state)).toEqual(['queued']) await second.queue.drive() expect(second.wake).not.toHaveBeenCalled() @@ -198,7 +197,7 @@ describe('cloud message queue', () => { }) it('is at most once when the reply is lost: the retry reuses the id and the host replays', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) const { queue, path } = setup({ clock, @@ -208,6 +207,7 @@ describe('cloud message queue', () => { host.loseNextReply() await queue.enqueue({ workspaceId: 'w', sessionId: SESSION, text: 'once' }) await until(() => queue.list('w')[0]!.state === 'sending') + await queue.flushed() clock.now += 60 * 60_000 const restarted = setup({ path, @@ -222,7 +222,7 @@ describe('cloud message queue', () => { }) it('never wakes from the background, and stops after its failure budget', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) host.setHistoryFails(true) const { queue, wake } = setup({ @@ -241,7 +241,7 @@ describe('cloud message queue', () => { }) it('backs off between background attempts', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) host.setHistoryFails(true) const { queue } = setup({ clock, host, place: () => ({ kind: 'up', environmentId: 'env' }) }) @@ -280,7 +280,7 @@ describe('cloud message queue', () => { }) it('does not send a prompt cancelled or edited while its fence was being read', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) let release: () => void = () => {} const gate = new Promise((resolve) => (release = resolve)) @@ -304,7 +304,6 @@ describe('cloud message queue', () => { expect(queue.cancel(a.id)).toBe(true) release() await queue.drive('w') - await new Promise((resolve) => setTimeout(resolve, 5)) expect(host.effects).toHaveLength(0) }) @@ -359,7 +358,7 @@ describe('cloud message queue', () => { }) it('does not resurrect a prompt a Sleep failed while its fence was being read', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) let release: () => void = () => {} const gate = new Promise((resolve) => (release = resolve)) @@ -382,13 +381,14 @@ describe('cloud message queue', () => { await new Promise((resolve) => setTimeout(resolve, 0)) queue.failPending('w', 'The workspace was put to sleep before this was sent.') release() - await new Promise((resolve) => setTimeout(resolve, 5)) + // Joins the pass that was waiting on the fence. + await queue.drive('w') expect(host.effects).toHaveLength(0) expect(queue.list('w')[0]!.state).toBe('failed') }) it('ends a sent-but-unconfirmed prompt as unconfirmed, not failed, at its budget', async () => { - const clock = { now: Date.now() } + const clock = { now: T0 } const host = realishHost(() => clock.now) const lossy: CloudMessageQueueDeps['call'] = async (env, method, params, t) => { const answer = await host.call(env, method, params, t) @@ -430,9 +430,11 @@ describe('cloud message queue', () => { }, log: () => {} }) + await queue.flushed() expect(existsSync(legacy)).toBe(false) writeFileSync(legacy, 'again') queue.clear() + await queue.flushed() expect(existsSync(legacy)).toBe(false) }) }) diff --git a/src/main/hosted-sandbox/cloud-message-queue.ts b/src/main/hosted-sandbox/cloud-message-queue.ts index e24e3034259..489ca6371ac 100644 --- a/src/main/hosted-sandbox/cloud-message-queue.ts +++ b/src/main/hosted-sandbox/cloud-message-queue.ts @@ -10,7 +10,6 @@ // Only the person queuing a prompt wakes a VM. Background retries deliver only to a VM that is // already up, never to one asleep, archived or manually disconnected, and stop after a budget. import { randomUUID } from 'node:crypto' -import { rmSync } from 'node:fs' import { CLOUD_QUEUED_PROMPT_MAX_CHARS } from '../../shared/hosted-sandbox/cloud-workspace-offline-api' import type { RuntimeRpcResponse } from '../../shared/runtime-rpc-envelope' import { structuredAgentSessionMessageSendMutation } from '../../shared/agent-session/requests/structured-agent-session-send-mutation' @@ -21,12 +20,12 @@ import { import { createStructuredAgentSessionOperationId } from '../../shared/agent-session/lifecycle/structured-agent-session-mutation' import { SendParams } from '../../shared/rpc-contract/structured-agent-session-params' import { - readSealedPrompts, + createSealedPromptDisk, currentFence, + earliestOwedAttempt, publicView, RETRYABLE_REFUSALS, - removeLegacyPlaintextQueue, - writeSealedPrompts, + SendResultSchema, type CloudMessageQueue, type CloudMessageQueueDeps, type StoredPrompt @@ -52,26 +51,33 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa const log = deps.log ?? ((line: string) => console.error(line)) const driving = new Map>() const mintId = () => createStructuredAgentSessionOperationId(randomUUID, now()) - removeLegacyPlaintextQueue(deps.path) - let prompts: StoredPrompt[] = readSealedPrompts(deps.path, deps.codec) + let prompts: StoredPrompt[] = [] + // Bumped by clear() so a load still reading the old file cannot bring cleared prompts back. + let generation = 0 + const disk = createSealedPromptDisk(deps.path, deps.codec, log) + const loaded = disk.load.then((stored) => { + prompts = generation === 0 ? [...stored, ...prompts] : prompts + }) - function save(): void { + /** Resolves once the queue is on disk as of this call; rejects if that write failed. */ + function save(): Promise { const cutoff = now() - DELIVERED_RETENTION_MS prompts = prompts.filter((prompt) => prompt.state !== 'delivered' || prompt.updatedAt >= cutoff) - writeSealedPrompts(deps.path, deps.codec, prompts) + return disk.write(prompts) } - function update(prompt: StoredPrompt, patch: Partial): void { + function update(prompt: StoredPrompt, patch: Partial): Promise { Object.assign(prompt, patch, { updatedAt: now() }) - save() + const saved = save() deps.onChange?.(prompt.workspaceId) + return saved } function failOnce(prompt: StoredPrompt, error: string): void { const failures = prompt.failures + 1 if (failures >= MAX_FAILURES) { // A prompt that reached `sending` may already be on the host: say so, so nobody re-sends it. - update( + void update( prompt, prompt.state === 'sending' ? { @@ -83,7 +89,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa ) return } - update(prompt, { + void update(prompt, { failures, nextAttemptAt: now() + Math.min(BACKOFF_MAX_MS, BACKOFF_BASE_MS * 2 ** (failures - 1)) }) @@ -104,7 +110,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa prompt.state === 'queued' && now() - mintedAt > AGENT_SESSION_MAX_NEW_OPERATION_AGE_MS - REMINT_MARGIN_MS ) { - update(prompt, { clientOperationId: mintId() }) + await update(prompt, { clientOperationId: mintId() }) } const before = { id: prompt.clientOperationId, text: prompt.text, state: prompt.state } let fence: number | { refused: string } | null @@ -129,7 +135,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa return 'stop' } if (typeof fence !== 'number') { - update(prompt, { state: 'failed', error: 'This chat is no longer on the workspace.' }) + await update(prompt, { state: 'failed', error: 'This chat is no longer on the workspace.' }) return 'next' } const params = structuredAgentSessionMessageSendMutation({ @@ -142,11 +148,11 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa }) // The host's own schema: a prompt it would reject outright is failed here, never retried. if (!SendParams.safeParse(params).success) { - update(prompt, { state: 'failed', error: 'This message cannot be sent to that chat.' }) + await update(prompt, { state: 'failed', error: 'This message cannot be sent to that chat.' }) return 'next' } // Durable before the send: from here on, any retry resends this same operation id. - update(prompt, { state: 'sending' }) + await update(prompt, { state: 'sending' }) let response: RuntimeRpcResponse try { response = await deps.call(environmentId, 'agentSession.send', params, SEND_TIMEOUT_MS) @@ -159,9 +165,10 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa failOnce(prompt, response.error.message.slice(0, 200)) return 'stop' } - const result = response.result as { ok: boolean; refusal?: { code?: string; message?: string } } + const parsed = SendResultSchema.safeParse(response.result) + const result = parsed.success ? parsed.data : { ok: false, refusal: undefined } if (result.ok) { - update(prompt, { state: 'delivered', error: undefined }) + await update(prompt, { state: 'delivered', error: undefined }) return 'next' } const code = result.refusal?.code ?? 'unknown' @@ -169,11 +176,12 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa failOnce(prompt, result.refusal?.message ?? code) return 'stop' } - update(prompt, { state: 'failed', error: result.refusal?.message ?? code }) + await update(prompt, { state: 'failed', error: result.refusal?.message ?? code }) return 'next' } async function driveWorkspace(workspaceId: string, allowWake: boolean): Promise { + await loaded if (pendingOf(workspaceId).length === 0) { return } @@ -233,6 +241,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa if (!trimmed || trimmed.length > CLOUD_QUEUED_PROMPT_MAX_CHARS) { throw new Error('Prompt is empty or too long.') } + await loaded const place = await deps.locate(workspaceId) if (place.kind === 'blocked') { throw new Error(place.reason) @@ -254,7 +263,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa nextAttemptAt: at } prompts.push(prompt) - save() + await save() deps.onChange?.(workspaceId) void run(workspaceId, true) return publicView(prompt) @@ -268,7 +277,7 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa return false } prompts = prompts.filter((p) => p !== prompt) - save() + void save() deps.onChange?.(prompt.workspaceId) return true }, @@ -284,9 +293,11 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa return false } // Never sent, so a new text is a new operation; nothing on the host knows the old id. - update(prompt, { text: trimmed, clientOperationId: mintId() }) + void update(prompt, { text: trimmed, clientOperationId: mintId() }) return true }, + nextAttemptAt: () => earliestOwedAttempt(prompts), + flushed: () => disk.flushed(), drive(workspaceId) { const ids = workspaceId ? [workspaceId] @@ -309,15 +320,15 @@ export function createCloudMessageQueue(deps: CloudMessageQueueDeps): CloudMessa } } if (changed) { - save() + void save() deps.onChange?.(workspaceId) } }, clear() { - removeLegacyPlaintextQueue(deps.path) + generation++ const workspaces = new Set(prompts.map((p) => p.workspaceId)) prompts = [] - rmSync(deps.path, { force: true }) + void disk.remove().catch(() => undefined) for (const workspaceId of workspaces) { deps.onChange?.(workspaceId) } diff --git a/src/main/hosted-sandbox/cloud-offline-beat.test.ts b/src/main/hosted-sandbox/cloud-offline-beat.test.ts new file mode 100644 index 00000000000..b433a4c8200 --- /dev/null +++ b/src/main/hosted-sandbox/cloud-offline-beat.test.ts @@ -0,0 +1,65 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { createOwedWorkBeat } from './cloud-offline-beat' + +const T0 = 1_800_000_000_000 + +describe('owed-work beat', () => { + beforeEach(() => { + vi.useFakeTimers() + vi.setSystemTime(T0) + }) + afterEach(() => vi.useRealTimers()) + + it('stays idle with nothing owed, and backs off while passes change nothing', async () => { + let due: number | null = null + const pass = vi.fn(async () => {}) + const beat = createOwedWorkBeat({ + nextDueAt: () => due, + pass, + baseMs: 1_000, + maxBackoffSteps: 2 + }) + beat.poke() + await vi.advanceTimersByTimeAsync(60_000) + expect(pass).not.toHaveBeenCalled() + + due = T0 + beat.poke() + await vi.advanceTimersByTimeAsync(1_000) + expect(pass).toHaveBeenCalledTimes(1) + // Unchanged after that pass: the next waits 2 s, then 4 s (the cap). + await vi.advanceTimersByTimeAsync(1_999) + expect(pass).toHaveBeenCalledTimes(1) + await vi.advanceTimersByTimeAsync(1) + expect(pass).toHaveBeenCalledTimes(2) + await vi.advanceTimersByTimeAsync(4_000) + expect(pass).toHaveBeenCalledTimes(3) + await vi.advanceTimersByTimeAsync(4_000) + expect(pass).toHaveBeenCalledTimes(4) + + due = null + await vi.advanceTimersByTimeAsync(4_000) + const settled = pass.mock.calls.length + await vi.advanceTimersByTimeAsync(60_000) + expect(pass).toHaveBeenCalledTimes(settled) + beat.dispose() + }) + + it('waits for a later due attempt and stops on dispose', async () => { + const pass = vi.fn(async () => {}) + const beat = createOwedWorkBeat({ + nextDueAt: () => T0 + 10_000, + pass, + baseMs: 1_000, + maxBackoffSteps: 2 + }) + beat.poke() + await vi.advanceTimersByTimeAsync(9_999) + expect(pass).not.toHaveBeenCalled() + await vi.advanceTimersByTimeAsync(1) + expect(pass).toHaveBeenCalledTimes(1) + beat.dispose() + await vi.advanceTimersByTimeAsync(60_000) + expect(pass).toHaveBeenCalledTimes(1) + }) +}) diff --git a/src/main/hosted-sandbox/cloud-offline-beat.ts b/src/main/hosted-sandbox/cloud-offline-beat.ts new file mode 100644 index 00000000000..9259d8a7351 --- /dev/null +++ b/src/main/hosted-sandbox/cloud-offline-beat.ts @@ -0,0 +1,59 @@ +// Background delivery for queued Orca Cloud prompts runs only while prompts are owed: at the next +// due attempt, never sooner than the base cadence, doubling while a pass changes nothing. +export type OwedWorkBeat = { + /** Something changed (a prompt was queued or settled): arm at the base cadence again. */ + poke(): void + /** Run a pass now (startup), then keep the cadence while work is owed. */ + runNow(): Promise + dispose(): void +} + +export function createOwedWorkBeat(options: { + /** Earliest owed attempt (ms epoch), or null when nothing is owed: the beat stops. */ + nextDueAt: () => number | null + pass: () => Promise + baseMs: number + maxBackoffSteps: number + now?: () => number +}): OwedWorkBeat { + const now = options.now ?? Date.now + let timer: ReturnType | null = null + let idlePasses = 0 + let disposed = false + + function arm(): void { + const due = options.nextDueAt() + if (timer || disposed || due === null) { + return + } + const idleDelay = options.baseMs * 2 ** Math.min(idlePasses, options.maxBackoffSteps) + timer = setTimeout(() => void runNow(), Math.max(due - now(), idleDelay)) + timer.unref?.() + } + + async function runNow(): Promise { + if (timer) { + clearTimeout(timer) + timer = null + } + const before = options.nextDueAt() + await options.pass() + idlePasses = options.nextDueAt() === before ? idlePasses + 1 : 0 + arm() + } + + return { + poke() { + idlePasses = 0 + arm() + }, + runNow, + dispose() { + disposed = true + if (timer) { + clearTimeout(timer) + timer = null + } + } + } +} diff --git a/src/main/hosted-sandbox/cloud-offline-cache-types.ts b/src/main/hosted-sandbox/cloud-offline-cache-types.ts index bd04c3f6c3b..9656b8d442a 100644 --- a/src/main/hosted-sandbox/cloud-offline-cache-types.ts +++ b/src/main/hosted-sandbox/cloud-offline-cache-types.ts @@ -1,6 +1,8 @@ // Types, limits and small helpers of the Orca Cloud offline cache (see cloud-offline-cache.ts). -import { readdirSync, rmSync } from 'node:fs' +import { createHash } from 'node:crypto' +import { readdir, rm } from 'node:fs/promises' import { join } from 'node:path' +import { z } from 'zod' import type { RuntimeRpcSuccess } from '../../shared/runtime-rpc-envelope' /** Read-only methods whose answers stay meaningful while the VM sleeps. Nothing that mutates. */ @@ -76,14 +78,15 @@ export type CloudOfflineCache = { params: unknown, response: RuntimeRpcSuccess ): void - lookup(environmentId: string, method: string, params: unknown): OfflineCacheEntry | null - entries(environmentId: string, method?: string): OfflineCacheEntry[] - forget(environmentId: string): void + lookup(environmentId: string, method: string, params: unknown): Promise + entries(environmentId: string, method?: string): Promise + forget(environmentId: string): Promise /** Drop every environment not in `keep` (removed workspaces). */ - prune(keep: ReadonlySet): void + prune(keep: ReadonlySet): Promise /** Delete everything (sign-out, profile switch). */ - clear(): void - flush(): void + clear(): Promise + /** Writes pending changes; resolves once earlier records and file work have landed. */ + flush(): Promise } /** Key-order independent, so `{a,b}` and `{b,a}` hit the same entry. */ @@ -105,14 +108,51 @@ export function stableKey(method: string, params: unknown): string { return `${method} ${JSON.stringify(sort(params ?? null))}` } -export function removeLegacyPlaintextFiles(root: string): void { +export async function removeLegacyPlaintextFiles(root: string): Promise { try { - for (const name of readdirSync(root)) { + for (const name of await readdir(root)) { if (name.endsWith('.json')) { - rmSync(join(root, name), { force: true }) + await rm(join(root, name), { force: true }) } } } catch { // No cache directory yet. } } + +export const EnvironmentFileSchema = z.object({ + version: z.literal(2), + environmentId: z.string(), + updatedAt: z.number(), + entries: z.record( + z.string(), + z.object({ + method: z.string(), + params: z.unknown(), + resultJson: z.string(), + runtimeId: z.string(), + at: z.number(), + bytes: z.number() + }) + ) +}) + +export function fileName(environmentId: string): string { + return `${createHash('sha256').update(environmentId).digest('hex').slice(0, 32)}.bin` +} + +export function decode(entry: StoredEntry): OfflineCacheEntry { + const { resultJson, ...rest } = entry + const result: unknown = JSON.parse(resultJson) + return { ...rest, result } +} + +export const DISABLED_OFFLINE_CACHE: CloudOfflineCache = { + record: () => {}, + lookup: async () => null, + entries: async () => [], + forget: async () => {}, + prune: async () => {}, + clear: async () => {}, + flush: async () => {} +} diff --git a/src/main/hosted-sandbox/cloud-offline-cache.test.ts b/src/main/hosted-sandbox/cloud-offline-cache.test.ts index 6dfc1553dbf..e252e2b201d 100644 --- a/src/main/hosted-sandbox/cloud-offline-cache.test.ts +++ b/src/main/hosted-sandbox/cloud-offline-cache.test.ts @@ -11,6 +11,7 @@ import { import { archiveCheckoutsFromCache, buildOfflineSnapshot, + stringParam, stripAnsi } from './cloud-offline-snapshot' @@ -41,17 +42,17 @@ function setup(limits = {}) { } describe('cloud offline cache', () => { - it('keeps only allowlisted read-only methods', () => { + it('keeps only allowlisted read-only methods', async () => { const { cache } = setup() cache.record('env', 'git.status', { worktree: 'id:w' }, ok({ entries: [] })) cache.record('env', 'agentSession.send', { sessionId: 's' }, ok({})) cache.record('env', 'git.commit', { worktree: 'id:w' }, ok({})) - expect(cache.entries('env').map((e) => e.method)).toEqual(['git.status']) + expect((await cache.entries('env')).map((e) => e.method)).toEqual(['git.status']) expect(OFFLINE_READ_METHODS.has('terminal.send')).toBe(false) - expect(cache.lookup('env', 'agentSession.send', { sessionId: 's' })).toBeNull() + expect(await cache.lookup('env', 'agentSession.send', { sessionId: 's' })).toBeNull() }) - it('seals files at rest and reads them back after a reload', () => { + it('seals files at rest and reads them back after a reload', async () => { const { root, cache } = setup() cache.record( 'env', @@ -60,23 +61,27 @@ describe('cloud offline cache', () => { ok({ items: ['secret subject'] }) ) expect(stableKey('m', { a: 1, b: 2 })).toBe(stableKey('m', { b: 2, a: 1 })) + await cache.flush() const [name] = readdirSync(root) expect(readFileSync(join(root, name!)).toString('utf8')).not.toContain('secret subject') const reloaded = createCloudOfflineCache({ root, codec: testCodec, flushDelayMs: 0 }) - expect(reloaded.lookup('env', 'git.history', { limit: 20, worktree: 'id:w' })?.result).toEqual({ + expect( + (await reloaded.lookup('env', 'git.history', { limit: 20, worktree: 'id:w' }))?.result + ).toEqual({ items: ['secret subject'] }) }) - it('keeps nothing when the OS cannot seal files', () => { + it('keeps nothing when the OS cannot seal files', async () => { const root = mkdtempSync(join(tmpdir(), 'orca-offline-')) const cache = createCloudOfflineCache({ root, codec: null, flushDelayMs: 0 }) cache.record('env', 'git.status', {}, ok({ entries: [] })) - expect(cache.entries('env')).toEqual([]) + expect(await cache.entries('env')).toEqual([]) + await cache.flush() expect(readdirSync(root)).toEqual([]) }) - it('does not re-serialize a key polled again within the interval', () => { + it('does not re-serialize a key polled again within the interval', async () => { const root = mkdtempSync(join(tmpdir(), 'orca-offline-')) let now = 0 const cache = createCloudOfflineCache({ @@ -88,27 +93,24 @@ describe('cloud offline cache', () => { cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['a'] } })) now = 1_000 cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['b'] } })) - expect(cache.lookup('env', 'terminal.read', { terminal: 't' })?.result).toEqual({ + expect((await cache.lookup('env', 'terminal.read', { terminal: 't' }))?.result).toEqual({ terminal: { tail: ['a'] } }) now = 6_000 cache.record('env', 'terminal.read', { terminal: 't' }, ok({ terminal: { tail: ['c'] } })) - expect(cache.lookup('env', 'terminal.read', { terminal: 't' })?.result).toEqual({ + expect((await cache.lookup('env', 'terminal.read', { terminal: 't' }))?.result).toEqual({ terminal: { tail: ['c'] } }) }) - it('evicts the oldest entries past the per-workspace bound and refuses oversized answers', () => { + it('evicts the oldest entries past the per-workspace bound and refuses oversized answers', async () => { const { cache, tick } = setup({ maxEntriesPerEnvironment: 3, maxEntryBytes: 200 }) for (let i = 0; i < 5; i++) { tick() cache.record('env', 'terminal.read', { terminal: `t${i}` }, ok({ terminal: { tail: ['x'] } })) } expect( - cache - .entries('env') - .map((e) => (e.params as { terminal: string }).terminal) - .sort() + (await cache.entries('env')).map((e) => stringParam(e.params, 'terminal')).sort() ).toEqual(['t2', 't3', 't4']) cache.record( 'env', @@ -116,50 +118,53 @@ describe('cloud offline cache', () => { { terminal: 't4' }, ok({ terminal: { tail: ['y'.repeat(500)] } }) ) - expect(cache.lookup('env', 'terminal.read', { terminal: 't4' })).toBeNull() + expect(await cache.lookup('env', 'terminal.read', { terminal: 't4' })).toBeNull() }) - it('holds a bounded number of workspaces in memory and still reads evicted ones from disk', () => { + it('holds a bounded number of workspaces in memory and still reads evicted ones from disk', async () => { const { cache, tick } = setup({ maxLoadedEnvironments: 2 }) for (const env of ['a', 'b', 'c', 'd']) { tick() cache.record(env, 'git.status', { worktree: env }, ok({ branch: env })) } - expect(cache.lookup('a', 'git.status', { worktree: 'a' })?.result).toEqual({ branch: 'a' }) + expect((await cache.lookup('a', 'git.status', { worktree: 'a' }))?.result).toEqual({ + branch: 'a' + }) }) - it('evicts whole least-recent workspaces past the total bound, prunes removed ones, and clears on sign-out', () => { + it('evicts whole least-recent workspaces past the total bound, prunes removed ones, and clears on sign-out', async () => { const { root, cache, tick } = setup({ maxTotalBytes: 900 }) for (const env of ['a', 'b', 'c']) { tick(1000) cache.record(env, 'git.status', { worktree: env }, ok({ entries: ['z'.repeat(300)] })) } + await cache.flush() expect(readdirSync(root).length).toBeLessThan(3) - expect(cache.lookup('c', 'git.status', { worktree: 'c' })).not.toBeNull() - cache.prune(new Set(['c'])) + expect(await cache.lookup('c', 'git.status', { worktree: 'c' })).not.toBeNull() + await cache.prune(new Set(['c'])) expect(readdirSync(root)).toHaveLength(1) - cache.clear() + await cache.clear() expect(existsSync(root)).toBe(false) - expect(cache.lookup('c', 'git.status', { worktree: 'c' })).toBeNull() + expect(await cache.lookup('c', 'git.status', { worktree: 'c' })).toBeNull() }) }) describe('legacy plaintext cache files', () => { - it('are removed whatever the codec', () => { + it('are removed whatever the codec', async () => { for (const sealing of [testCodec, null]) { const root = mkdtempSync(join(tmpdir(), 'orca-offline-')) writeFileSync( join(root, 'abc.json'), '{"version":1,"entries":{"k":{"result":"old plaintext"}}}' ) - createCloudOfflineCache({ root, codec: sealing, flushDelayMs: 0 }) + await createCloudOfflineCache({ root, codec: sealing, flushDelayMs: 0 }).flush() expect(readdirSync(root)).toEqual([]) } }) }) describe('offline snapshot', () => { - it('builds chats, terminals and git state from cached answers', () => { + it('builds chats, terminals and git state from cached answers', async () => { const { cache, tick } = setup() cache.record( 'env', @@ -222,7 +227,7 @@ describe('offline snapshot', () => { } }) ) - const snapshot = buildOfflineSnapshot(cache, 'env', 'w', true) + const snapshot = await buildOfflineSnapshot(cache, 'env', 'w', true) expect(snapshot.asleep).toBe(true) expect(snapshot.terminals).toEqual([ { handle: 'h1', title: 'Dev server', lines: ['ready on :3000'], at: 1000 } @@ -237,7 +242,8 @@ describe('offline snapshot', () => { ['assistant', 'Done.'] ]) expect(snapshot.capturedAt).toBe(1001) - const [checkout] = archiveCheckoutsFromCache(cache, 'env', '/vercel/sandbox/project')! + const [checkout] = + (await archiveCheckoutsFromCache(cache, 'env', '/vercel/sandbox/project')) ?? [] expect(checkout).toMatchObject({ branch: 'feat', head: 'abcdef1', @@ -245,7 +251,7 @@ describe('offline snapshot', () => { }) }) - it('strips terminal escapes', () => { + it('strips terminal escapes', async () => { expect(stripAnsi('\u001b]0;title\u0007a\u001b[1;31mb\u001b[0m\rc')).toBe('abc') }) }) diff --git a/src/main/hosted-sandbox/cloud-offline-cache.ts b/src/main/hosted-sandbox/cloud-offline-cache.ts index 8ab341f12a2..f1b9a19458a 100644 --- a/src/main/hosted-sandbox/cloud-offline-cache.ts +++ b/src/main/hosted-sandbox/cloud-offline-cache.ts @@ -3,28 +3,21 @@ // servers; Orca keeps them on the VM (end-to-end encrypted), so the only copy outside the VM is // this one, on the user's own disk, sealed with the OS keychain. Without a keychain nothing is kept. // Bounded per workspace, in total, and in memory; one file per environment. -import { createHash } from 'node:crypto' -import { - mkdirSync, - readFileSync, - readdirSync, - renameSync, - rmSync, - statSync, - writeFileSync -} from 'node:fs' +import { mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { + decode, DEFAULT_OFFLINE_CACHE_LIMITS, + DISABLED_OFFLINE_CACHE, + EnvironmentFileSchema, + fileName, OFFLINE_READ_METHODS, removeLegacyPlaintextFiles, stableKey, type CloudOfflineCache, type EnvironmentFile, type OfflineCacheCodec, - type OfflineCacheEntry, - type OfflineCacheLimits, - type StoredEntry + type OfflineCacheLimits } from './cloud-offline-cache-types' export { @@ -37,90 +30,98 @@ export { type OfflineCacheLimits } from './cloud-offline-cache-types' -function fileName(environmentId: string): string { - return `${createHash('sha256').update(environmentId).digest('hex').slice(0, 32)}.bin` -} - -function decode(entry: StoredEntry): OfflineCacheEntry { - const { resultJson, ...rest } = entry - return { ...rest, result: JSON.parse(resultJson) as unknown } -} - -const DISABLED: CloudOfflineCache = { - record: () => {}, - lookup: () => null, - entries: () => [], - forget: () => {}, - prune: () => {}, - clear: () => {}, - flush: () => {} -} - export function createCloudOfflineCache(options: { root: string /** Null: the OS cannot seal files, so nothing is cached. */ codec: OfflineCacheCodec | null limits?: Partial now?: () => number - /** Write delay; 0 writes synchronously (tests). */ + /** Write delay; 0 writes as soon as a record lands (tests). */ flushDelayMs?: number }): CloudOfflineCache { - const codec = options.codec + // All file work runs one task at a time, in call order, off the main thread's sync fs path. + let io: Promise = Promise.resolve() + function serial(task: () => Promise): Promise { + const run = io.then(task, task) + io = run.catch(() => undefined) + return run + } // Plaintext v1 files from the first build are never read; remove them whatever the codec. - removeLegacyPlaintextFiles(options.root) + const legacyRemoved = serial(() => removeLegacyPlaintextFiles(options.root)) + const codec = options.codec if (!codec) { - return DISABLED + return { ...DISABLED_OFFLINE_CACHE, flush: () => legacyRemoved } } const limits = { ...DEFAULT_OFFLINE_CACHE_LIMITS, ...options.limits } const now = options.now ?? Date.now const flushDelayMs = options.flushDelayMs ?? 1_000 // Map insertion order is the LRU order: a touched environment is re-inserted at the end. const loaded = new Map() + const loading = new Map>() const dirty = new Set() + // Bumped by forget/prune/clear so a read that started before them is not cached afterwards. + let generation = 0 let timer: ReturnType | null = null const pathOf = (environmentId: string) => join(options.root, fileName(environmentId)) - function write(environmentId: string): void { - const file = loaded.get(environmentId) - if (!file) { - return - } - mkdirSync(options.root, { recursive: true, mode: 0o700 }) - const target = pathOf(environmentId) - const temp = `${target}.${process.pid}.tmp` - writeFileSync(temp, codec!.encrypt(JSON.stringify(file)), { mode: 0o600 }) - renameSync(temp, target) + function write(file: EnvironmentFile): Promise { + const serialized = codec!.encrypt(JSON.stringify(file)) + return serial(async () => { + await mkdir(options.root, { recursive: true, mode: 0o700 }) + const target = pathOf(file.environmentId) + const temp = `${target}.${process.pid}.tmp` + await writeFile(temp, serialized, { mode: 0o600 }) + await rename(temp, target) + }) } - function load(environmentId: string): EnvironmentFile { - const hit = loaded.get(environmentId) - if (hit) { - loaded.delete(environmentId) - loaded.set(environmentId, hit) - return hit - } - let file: EnvironmentFile = { version: 2, environmentId, updatedAt: 0, entries: {} } + async function readFromDisk(environmentId: string): Promise { try { - const parsed = JSON.parse( - codec!.decrypt(readFileSync(pathOf(environmentId))) - ) as EnvironmentFile - if (parsed.version === 2 && parsed.environmentId === environmentId && parsed.entries) { - file = parsed + const parsed = EnvironmentFileSchema.safeParse( + JSON.parse(codec!.decrypt(await readFile(pathOf(environmentId)))) + ) + if (parsed.success && parsed.data.environmentId === environmentId) { + return parsed.data } } catch { // Missing, unreadable or sealed by another keychain: start empty. A cache is never a source of truth. } - loaded.set(environmentId, file) - while (loaded.size > limits.maxLoadedEnvironments) { - const oldest = loaded.keys().next().value! - if (dirty.has(oldest)) { - write(oldest) - dirty.delete(oldest) - } - loaded.delete(oldest) + return { version: 2, environmentId, updatedAt: 0, entries: {} } + } + + function load(environmentId: string): Promise { + const hit = loaded.get(environmentId) + if (hit) { + loaded.delete(environmentId) + loaded.set(environmentId, hit) + return Promise.resolve(hit) } - return file + const pending = loading.get(environmentId) + if (pending) { + return pending + } + const startedAt = generation + const reading = serial(() => readFromDisk(environmentId)).then((file) => { + loading.delete(environmentId) + const raced = loaded.get(environmentId) + if (raced || startedAt !== generation) { + return raced ?? file + } + loaded.set(environmentId, file) + while (loaded.size > limits.maxLoadedEnvironments) { + const oldest = loaded.keys().next().value! + const evicted = loaded.get(oldest) + if (evicted && dirty.has(oldest)) { + dirty.delete(oldest) + void write(evicted).catch(logWriteFailure) + } + loaded.delete(oldest) + } + return file + }) + loading.set(environmentId, reading) + return reading } function evictWithin(file: EnvironmentFile): void { @@ -138,15 +139,16 @@ export function createCloudOfflineCache(options: { } /** Whole environments go least-recently-written first once the total passes its bound. */ - function evictTotal(): void { + async function evictTotal(): Promise { let files: { name: string; size: number; mtime: number }[] try { - files = readdirSync(options.root) - .filter((name) => name.endsWith('.bin')) - .map((name) => { - const stat = statSync(join(options.root, name)) - return { name, size: stat.size, mtime: stat.mtimeMs } + const names = (await readdir(options.root)).filter((name) => name.endsWith('.bin')) + files = await Promise.all( + names.map(async (name) => { + const info = await stat(join(options.root, name)) + return { name, size: info.size, mtime: info.mtimeMs } }) + ) } catch { return } @@ -155,7 +157,7 @@ export function createCloudOfflineCache(options: { if (total <= limits.maxTotalBytes) { break } - rmSync(join(options.root, file.name), { force: true }) + await rm(join(options.root, file.name), { force: true }) for (const environmentId of loaded.keys()) { if (fileName(environmentId) === file.name) { loaded.delete(environmentId) @@ -165,30 +167,33 @@ export function createCloudOfflineCache(options: { } } - function flush(): void { + function logWriteFailure(error: unknown): void { + console.error(`[orca-cloud] offline cache write failed: ${String(error)}`) + } + + // Records apply after their environment loads; flush waits for those still landing. + const applying = new Set>() + + async function flush(): Promise { if (timer) { clearTimeout(timer) timer = null } - for (const environmentId of dirty) { - try { - write(environmentId) - } catch (error) { - console.error(`[orca-cloud] offline cache write failed: ${String(error)}`) - } - } + await Promise.all(applying) + const files = [...dirty].flatMap((environmentId) => loaded.get(environmentId) ?? []) dirty.clear() - evictTotal() + await Promise.all(files.map((file) => write(file).catch(logWriteFailure))) + await serial(evictTotal) } function markDirty(environmentId: string): void { dirty.add(environmentId) if (flushDelayMs === 0) { - flush() + void flush() return } if (!timer) { - timer = setTimeout(flush, flushDelayMs) + timer = setTimeout(() => void flush(), flushDelayMs) timer.unref?.() } } @@ -198,81 +203,86 @@ export function createCloudOfflineCache(options: { if (!OFFLINE_READ_METHODS.has(method)) { return } - const file = load(environmentId) - const key = stableKey(method, params) - const existing = file.entries[key] const at = now() - if (existing && at - existing.at < limits.minRecordIntervalMs) { - return - } - const resultJson = JSON.stringify(response.result ?? null) - const bytes = resultJson.length + key.length - if (bytes > limits.maxEntryBytes) { - // Too big to keep; don't serve an older, smaller answer as if it were current either. - if (existing) { - delete file.entries[key] - markDirty(environmentId) + const applied = load(environmentId).then((file) => { + const key = stableKey(method, params) + const existing = file.entries[key] + if (existing && at - existing.at < limits.minRecordIntervalMs) { + return } - return - } - file.entries[key] = { - method, - params: params ?? null, - resultJson, - runtimeId: response._meta.runtimeId, - at, - bytes - } - file.updatedAt = at - evictWithin(file) - markDirty(environmentId) + const resultJson = JSON.stringify(response.result ?? null) + const bytes = resultJson.length + key.length + if (bytes > limits.maxEntryBytes) { + // Too big to keep; don't serve an older, smaller answer as if it were current either. + if (existing) { + delete file.entries[key] + markDirty(environmentId) + } + return + } + file.entries[key] = { + method, + params: params ?? null, + resultJson, + runtimeId: response._meta.runtimeId, + at, + bytes + } + file.updatedAt = at + evictWithin(file) + markDirty(environmentId) + }) + applying.add(applied) + void applied.finally(() => applying.delete(applied)) }, - lookup(environmentId, method, params) { + async lookup(environmentId, method, params) { if (!OFFLINE_READ_METHODS.has(method)) { return null } - const entry = load(environmentId).entries[stableKey(method, params)] + await Promise.all(applying) + const entry = (await load(environmentId)).entries[stableKey(method, params)] return entry ? decode(entry) : null }, - entries(environmentId, method) { - return Object.values(load(environmentId).entries) + async entries(environmentId, method) { + await Promise.all(applying) + return Object.values((await load(environmentId)).entries) .filter((entry) => method === undefined || entry.method === method) .sort((a, b) => b.at - a.at) .map(decode) }, forget(environmentId) { + generation++ loaded.delete(environmentId) dirty.delete(environmentId) - rmSync(pathOf(environmentId), { force: true }) + return serial(() => rm(pathOf(environmentId), { force: true })) }, prune(keep) { - const keepFiles = new Set([...keep].map(fileName)) - let names: string[] = [] - try { - names = readdirSync(options.root) - } catch { - return - } - for (const name of names) { - if (!keepFiles.has(name)) { - rmSync(join(options.root, name), { force: true }) - } - } + generation++ for (const environmentId of loaded.keys()) { if (!keep.has(environmentId)) { loaded.delete(environmentId) dirty.delete(environmentId) } } + const keepFiles = new Set([...keep].map(fileName)) + return serial(async () => { + const names = await readdir(options.root).catch(() => []) + for (const name of names) { + if (!keepFiles.has(name)) { + await rm(join(options.root, name), { force: true }) + } + } + }) }, clear() { if (timer) { clearTimeout(timer) timer = null } + generation++ loaded.clear() dirty.clear() - rmSync(options.root, { recursive: true, force: true }) + return serial(() => rm(options.root, { recursive: true, force: true })) }, flush } diff --git a/src/main/hosted-sandbox/cloud-offline-service.ts b/src/main/hosted-sandbox/cloud-offline-service.ts index 70c494d7ae5..89ee90f9d63 100644 --- a/src/main/hosted-sandbox/cloud-offline-service.ts +++ b/src/main/hosted-sandbox/cloud-offline-service.ts @@ -26,9 +26,15 @@ import { type CloudOfflineCache, type OfflineCacheCodec } from './cloud-offline-cache' +import { createOwedWorkBeat } from './cloud-offline-beat' import { setCloudOfflineReset } from './cloud-offline-reset' import { safeStorageCodec, setCloudOfflineObserver } from './cloud-offline-seam' -import { archiveCheckoutsFromCache, buildOfflineSnapshot } from './cloud-offline-snapshot' +import { + archiveCheckoutsFromCache, + buildOfflineSnapshot, + stringParam, + terminalHandlesOf +} from './cloud-offline-snapshot' import { getHostedWorkspaceService, setArchiveCheckoutsProvider } from './hosted-workspace-service' type CloudRuntimeInfo = { @@ -40,6 +46,9 @@ type CloudRuntimeInfo = { const CAPTURE_BUDGET_MS = 8_000 const MAX_CAPTURED_TERMINALS = 12 +/** Background delivery cadence while prompts are owed; doubles while nothing moves. */ +const BEAT_MS = 30_000 +const MAX_BEAT_BACKOFF_STEPS = 4 export type CloudOfflineService = { cache: CloudOfflineCache @@ -155,9 +164,8 @@ export function installCloudOfflineService(deps: { // Chats the window has read stay current: refresh their tail pages. const sessions = [ ...new Set( - cache - .entries(env, 'agentSession.history') - .map((entry) => (entry.params as { sessionId?: string } | null)?.sessionId) + (await cache.entries(env, 'agentSession.history')) + .map((entry) => stringParam(entry.params, 'sessionId')) .filter((id): id is string => typeof id === 'string') ) ].slice(0, 20) @@ -174,15 +182,11 @@ export function installCloudOfflineService(deps: { ) ) ]) - const terminals = ( - (list as { terminals?: { handle: string }[] } | null)?.terminals ?? [] - ).slice(0, MAX_CAPTURED_TERMINALS) + const handles = terminalHandlesOf(list).slice(0, MAX_CAPTURED_TERMINALS) await Promise.all( - terminals.map((terminal) => - readInto(env, 'terminal.read', { terminal: terminal.handle, limit: 400 }, deadline) - ) + handles.map((terminal) => readInto(env, 'terminal.read', { terminal, limit: 400 }, deadline)) ) - cache.flush() + await cache.flush() } async function locate(workspaceId: string): Promise { @@ -218,23 +222,43 @@ export function installCloudOfflineService(deps: { return opening === 'up' }, call: deps.call, - onChange: (workspaceId) => broadcast(CH.queuedPromptsChanged, { workspaceId }), + onChange: (workspaceId) => { + broadcast(CH.queuedPromptsChanged, { workspaceId }) + beat.poke() + }, log }) + // Background: deliver to VMs already up (never wakes) and drop removed workspaces, only while + // prompts are owed. A live answer from a workspace (it is up) delivers its prompts at once. + const beat = createOwedWorkBeat({ + nextDueAt: () => queue.nextAttemptAt(), + pass: async () => { + invalidate() + await queue.drive() + void cache.prune(new Set(runtimes().byEnvironment.keys())) + }, + baseMs: BEAT_MS, + maxBackoffSteps: MAX_BEAT_BACKOFF_STEPS + }) + setCloudOfflineObserver((environmentId, method, params, response) => { - if (response.ok && runtimes().byEnvironment.has(environmentId)) { + const info = runtimes().byEnvironment.get(environmentId) + if (response.ok && info) { cache.record(environmentId, method, params, response) + if (queue.nextAttemptAt() !== null) { + void queue.drive(info.workspaceId) + } } }) setCloudOfflineReset(() => { - cache.clear() + void cache.clear() queue.clear() }) setArchiveCheckoutsProvider(async (workspaceId) => { const info = runtimes().byWorkspace.get(workspaceId) return info - ? archiveCheckoutsFromCache( + ? await archiveCheckoutsFromCache( cache, info.environmentId, info.projectRoot ?? '/vercel/sandbox/project' @@ -247,11 +271,11 @@ export function installCloudOfflineService(deps: { } ipcMain.handle( CH.getSnapshot, - (_event, args: { workspaceId: string }): CloudOfflineSnapshot | null => { + async (_event, args: { workspaceId: string }): Promise => { invalidate() const info = runtimes().byWorkspace.get(args.workspaceId) return info - ? buildOfflineSnapshot(cache, info.environmentId, args.workspaceId, info.asleep) + ? await buildOfflineSnapshot(cache, info.environmentId, args.workspaceId, info.asleep) : null } ) @@ -273,14 +297,8 @@ export function installCloudOfflineService(deps: { queue.edit(args.id, args.text) ) - // Background: deliver to VMs that are already up (never wakes), and drop removed workspaces. - const beat = setInterval(() => { - invalidate() - void queue.drive() - cache.prune(new Set(runtimes().byEnvironment.keys())) - }, 30_000) - beat.unref?.() - const startup = setTimeout(() => void queue.drive(), 5_000) + // Startup: deliver what survived a restart (never wakes), and drop removed workspaces. + const startup = setTimeout(() => void beat.runNow(), 5_000) startup.unref?.() return { @@ -288,9 +306,10 @@ export function installCloudOfflineService(deps: { queue, capture, dispose() { - clearInterval(beat) + beat.dispose() clearTimeout(startup) - cache.flush() + void cache.flush() + void queue.flushed() setCloudOfflineObserver(null) setCloudOfflineReset(null) setArchiveCheckoutsProvider(null) diff --git a/src/main/hosted-sandbox/cloud-offline-snapshot.ts b/src/main/hosted-sandbox/cloud-offline-snapshot.ts index 4c8f124ae53..27233aa83a8 100644 --- a/src/main/hosted-sandbox/cloud-offline-snapshot.ts +++ b/src/main/hosted-sandbox/cloud-offline-snapshot.ts @@ -1,4 +1,5 @@ // Turns cached RPC answers into what the asleep view shows, and into archive git metadata. +import { z } from 'zod' import type { CloudOfflineChat, CloudOfflineChatMessage, @@ -26,40 +27,98 @@ export function stripAnsi(text: string): string { return text.replace(ANSI, '').replace(/\r(?!\n)/g, '') } -type HistoryResult = { - ok?: boolean - page?: { - sessionId?: string - items?: { - itemId: string - observedAt: number - body: { kind: string; role?: string; blocks?: { type: string; text?: string }[] } - }[] - } +// Cached answers came from the VM: parse leniently, and a malformed piece is skipped, never trusted. +const Text = z.string() +const ChatItemSchema = z.looseObject({ + itemId: Text, + observedAt: z.number(), + body: z.looseObject({ + kind: Text, + role: Text.optional(), + blocks: z.array(z.looseObject({ type: Text, text: z.unknown().optional() })).optional() + }) +}) +const HistoryResultSchema = z.looseObject({ + page: z + .looseObject({ sessionId: Text.optional(), items: z.array(z.unknown()).optional() }) + .optional() +}) +const TerminalListSchema = z.looseObject({ + terminals: z + .array( + z.looseObject({ + handle: Text, + title: Text.nullable().optional(), + paneTitle: Text.optional() + }) + ) + .catch([]) + .optional() +}) +// `terminal.read` answers `{terminal: RuntimeTerminalRead}`. +const TerminalReadSchema = z.looseObject({ + terminal: z.looseObject({ handle: Text.optional(), tail: z.array(Text).optional() }).optional() +}) +const StatusResultSchema = z.looseObject({ + entries: z + .array(z.looseObject({ path: Text, status: Text, area: Text })) + .catch([]) + .optional(), + head: Text.optional().catch(undefined), + branch: Text.optional().catch(undefined) +}) +const HistoryItemsSchema = z.looseObject({ + items: z + .array( + z.looseObject({ + id: Text, + subject: Text, + author: Text.optional(), + timestamp: z.number().optional() + }) + ) + .catch([]) + .optional() +}) +const ParamsSchema = z.record(z.string(), z.unknown()) + +/** A string field of a cached call's params, if it has one. */ +export function stringParam(params: unknown, key: string): string | undefined { + const parsed = ParamsSchema.safeParse(params) + const value = parsed.success ? parsed.data[key] : undefined + return typeof value === 'string' ? value : undefined +} + +/** Terminal handles from a cached `terminal.list` answer. */ +export function terminalHandlesOf(result: unknown): string[] { + const parsed = TerminalListSchema.safeParse(result) + return parsed.success ? (parsed.data.terminals ?? []).map((terminal) => terminal.handle) : [] } function chatOf(entry: OfflineCacheEntry): CloudOfflineChat | null { - const result = entry.result as HistoryResult - const sessionId = - result?.page?.sessionId ?? (entry.params as { sessionId?: string } | null)?.sessionId - if (!sessionId || !Array.isArray(result?.page?.items)) { + const parsed = HistoryResultSchema.safeParse(entry.result) + const page = parsed.success ? parsed.data.page : undefined + const sessionId = page?.sessionId ?? stringParam(entry.params, 'sessionId') + if (!sessionId || !Array.isArray(page?.items)) { return null } const messages: CloudOfflineChatMessage[] = [] - for (const item of result.page.items) { - if (item.body?.kind !== 'message' || !Array.isArray(item.body.blocks)) { + for (const raw of page.items) { + const item = ChatItemSchema.safeParse(raw) + if (!item.success || item.data.body.kind !== 'message' || !item.data.body.blocks) { continue } - const text = item.body.blocks - .filter((block) => block.type === 'text' && typeof block.text === 'string') - .map((block) => block.text) + const text = item.data.body.blocks + .flatMap((block) => + block.type === 'text' && typeof block.text === 'string' ? [block.text] : [] + ) .join('\n') .trim() - const role = item.body.role + const role = item.data.body.role if (!text || (role !== 'user' && role !== 'assistant' && role !== 'system')) { continue } - messages.push({ itemId: item.itemId, role, text, at: item.observedAt }) + messages.push({ itemId: item.data.itemId, role, text, at: item.data.observedAt }) } return { sessionId, at: entry.at, messages: messages.slice(-MAX_MESSAGES) } } @@ -68,7 +127,7 @@ function chatOf(entry: OfflineCacheEntry): CloudOfflineChat | null { function chatsOf(entries: OfflineCacheEntry[]): CloudOfflineChat[] { const bySession = new Map() for (const entry of entries) { - const direction = (entry.params as { direction?: string } | null)?.direction + const direction = stringParam(entry.params, 'direction') if (direction && direction !== 'tail') { continue } @@ -85,17 +144,15 @@ function chatsOf(entries: OfflineCacheEntry[]): CloudOfflineChat[] { return [...bySession.values()].sort((a, b) => b.at - a.at) } -function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineTerminal[] { +async function terminalsOf( + cache: CloudOfflineCache, + environmentId: string +): Promise { // Every listing contributes titles (a filtered listing can be empty); newest first wins. const titles = new Map() - for (const list of cache.entries(environmentId, 'terminal.list')) { - const listed = - ( - list.result as { - terminals?: { handle: string; title: string | null; paneTitle?: string }[] - } | null - )?.terminals ?? [] - for (const terminal of listed) { + for (const list of await cache.entries(environmentId, 'terminal.list')) { + const parsed = TerminalListSchema.safeParse(list.result) + for (const terminal of parsed.success ? (parsed.data.terminals ?? []) : []) { if (!titles.has(terminal.handle)) { titles.set(terminal.handle, terminal.paneTitle ?? terminal.title ?? null) } @@ -103,12 +160,11 @@ function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOffl } const seen = new Set() const terminals: CloudOfflineTerminal[] = [] - for (const entry of cache.entries(environmentId, 'terminal.read')) { - // `terminal.read` answers `{terminal: RuntimeTerminalRead}`. - const wrapped = entry.result as { terminal?: { handle?: string; tail?: string[] } } | null - const result = wrapped?.terminal - const handle = result?.handle ?? (entry.params as { terminal?: string } | null)?.terminal - if (!handle || seen.has(handle) || !Array.isArray(result?.tail)) { + for (const entry of await cache.entries(environmentId, 'terminal.read')) { + const parsed = TerminalReadSchema.safeParse(entry.result) + const result = parsed.success ? parsed.data.terminal : undefined + const handle = result?.handle ?? stringParam(entry.params, 'terminal') + if (!handle || seen.has(handle) || !result?.tail) { continue } seen.add(handle) @@ -122,22 +178,19 @@ function terminalsOf(cache: CloudOfflineCache, environmentId: string): CloudOffl return terminals } -type StatusResult = { - entries?: { path: string; status: string; area: string }[] - head?: string - branch?: string -} -type HistoryItems = { - items?: { id: string; subject: string; author?: string; timestamp?: number }[] -} - -function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit | null { - const status = cache.entries(environmentId, 'git.status')[0] - const history = cache.entries(environmentId, 'git.history')[0] +async function gitOf( + cache: CloudOfflineCache, + environmentId: string +): Promise { + const status = (await cache.entries(environmentId, 'git.status'))[0] + const history = (await cache.entries(environmentId, 'git.history'))[0] if (!status && !history) { return null } - const statusResult = (status?.result ?? {}) as StatusResult + const parsedStatus = StatusResultSchema.safeParse(status?.result ?? {}) + const statusResult = parsedStatus.success ? parsedStatus.data : {} + const parsedHistory = HistoryItemsSchema.safeParse(history?.result ?? {}) + const items = parsedHistory.success ? (parsedHistory.data.items ?? []) : [] const entries = statusResult.entries ?? [] return { branch: statusResult.branch?.replace(/^refs\/heads\//, '') ?? null, @@ -146,7 +199,7 @@ function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit .slice(0, MAX_CHANGES) .map(({ path, status: s, area }) => ({ path, status: s, area })), changeCount: entries.length, - commits: (((history?.result ?? {}) as HistoryItems).items ?? []).slice(0, 20).map((item) => ({ + commits: items.slice(0, 20).map((item) => ({ id: item.id, subject: item.subject, author: item.author ?? null, @@ -156,15 +209,15 @@ function gitOf(cache: CloudOfflineCache, environmentId: string): CloudOfflineGit } } -export function buildOfflineSnapshot( +export async function buildOfflineSnapshot( cache: CloudOfflineCache, environmentId: string, workspaceId: string, asleep: boolean -): CloudOfflineSnapshot { - const git = gitOf(cache, environmentId) - const terminals = terminalsOf(cache, environmentId) - const chats = chatsOf(cache.entries(environmentId, 'agentSession.history')) +): Promise { + const git = await gitOf(cache, environmentId) + const terminals = await terminalsOf(cache, environmentId) + const chats = chatsOf(await cache.entries(environmentId, 'agentSession.history')) const times = [git?.at ?? 0, ...terminals.map((t) => t.at), ...chats.map((c) => c.at)].filter( (t) => t > 0 ) @@ -188,12 +241,12 @@ const STATUS_LETTER: Record = { } /** Archive metadata from the cache, for archiving a VM that is already asleep. */ -export function archiveCheckoutsFromCache( +export async function archiveCheckoutsFromCache( cache: CloudOfflineCache, environmentId: string, projectRoot: string -): HostedArchiveCheckout[] | null { - const git = gitOf(cache, environmentId) +): Promise { + const git = await gitOf(cache, environmentId) if (!git || (!git.branch && !git.head)) { return null } diff --git a/src/main/hosted-sandbox/cloud-transfer-steps.ts b/src/main/hosted-sandbox/cloud-transfer-steps.ts index 86dcfad4383..3a9a9b87fbc 100644 --- a/src/main/hosted-sandbox/cloud-transfer-steps.ts +++ b/src/main/hosted-sandbox/cloud-transfer-steps.ts @@ -5,11 +5,11 @@ // prepare: pack in the VM → download in chunks → fetch the branch into the local repo. // finish (after the window created the local worktree): apply staged/unstaged diffs, untracked // files, and install Claude/Codex sessions remapped to the new path so native resume works. -import { execFile } from 'node:child_process' import { createWriteStream } from 'node:fs' import { cp, lstat, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises' import { join, relative } from 'node:path' -import { promisify } from 'node:util' +import { z } from 'zod' +import { gitExecFileAsync } from '../git/command-runner/git-exec-file' import { CLOUD_TRANSFER_DIR, CLOUD_TRANSFER_LOG, @@ -24,7 +24,6 @@ import { } from '../../shared/hosted-sandbox/cloud-transfer-pack' import type { RuntimeCall } from './hosted-agent-auto-resume' -const run = promisify(execFile) const CHUNK_BYTES = 512 * 1024 const PACK_TIMEOUT_MS = 10 * 60_000 @@ -36,10 +35,33 @@ export type Prepared = { } async function git(cwd: string, args: string[]): Promise { - const { stdout } = await run('git', args, { cwd, maxBuffer: 64 * 1024 * 1024 }) + const { stdout } = await gitExecFileAsync(args, { + cwd, + maxBuffer: 64 * 1024 * 1024, + // Fetching a large bundle can outlast the default read deadline. + timeout: PACK_TIMEOUT_MS + }) return stdout.trim() } +// orcad RPC replies, validated instead of trusted: the VM side is not this process. +const TerminalCreatedSchema = z.looseObject({ + terminal: z.union([z.string(), z.looseObject({ handle: z.string().optional() })]).optional() +}) +const FileStatSchema = z.looseObject({ size: z.number().optional() }) +const FileChunkSchema = z.looseObject({ + contentBase64: z.string(), + bytesRead: z.number().optional(), + eof: z.boolean().optional() +}) + +async function readChunk( + call: RuntimeCall, + params: { worktree: string; relativePath: string; offset: number; length: number } +): Promise> { + return FileChunkSchema.parse(await call('files.readChunk', params)) +} + export async function fileSize(path: string): Promise { return stat(path) .then((s) => s.size) @@ -48,6 +70,14 @@ export async function fileSize(path: string): Promise { export type WorktreeRow = { id?: unknown; path?: unknown } +/** `worktree.list` reply: rows stay loose and pickCloudWorktree checks their fields. */ +export const WorktreeListingSchema = z.looseObject({ + worktrees: z + .array(z.looseObject({ id: z.unknown(), path: z.unknown() })) + .optional() + .catch(undefined) +}) + /** The orcad-side worktree for this workspace: its id, else the VM project root, else the only one. */ export function pickCloudWorktree( rows: WorktreeRow[], @@ -75,14 +105,16 @@ async function runInCloud( commandLine: string, title: string ): Promise { - const created = (await call('terminal.create', { - worktree, - command: commandLine, - title, - presentation: 'background', - focus: false, - activate: false - })) as { terminal?: { handle?: string } | string } + const created = TerminalCreatedSchema.parse( + await call('terminal.create', { + worktree, + command: commandLine, + title, + presentation: 'background', + focus: false, + activate: false + }) + ) const handle = typeof created.terminal === 'string' ? created.terminal : created.terminal?.handle if (!handle) { throw new Error('Could not run a command in the cloud workspace.') @@ -127,16 +159,9 @@ async function readCloudTail( worktree: string, relativePath: string ): Promise { - const stat = (await call('files.stat', { worktree, relativePath })) as { size?: number } + const stat = FileStatSchema.parse(await call('files.stat', { worktree, relativePath })) const offset = Math.max(0, (stat.size ?? 0) - 8192) - const page = (await call('files.readChunk', { - worktree, - relativePath, - offset, - length: 8192 - })) as { - contentBase64: string - } + const page = await readChunk(call, { worktree, relativePath, offset, length: 8192 }) return Buffer.from(page.contentBase64, 'base64').toString('utf8') } @@ -155,14 +180,7 @@ async function readCloudText( worktree: string, relativePath: string ): Promise { - const page = (await call('files.readChunk', { - worktree, - relativePath, - offset: 0, - length: CHUNK_BYTES - })) as { - contentBase64: string - } + const page = await readChunk(call, { worktree, relativePath, offset: 0, length: CHUNK_BYTES }) return Buffer.from(page.contentBase64, 'base64').toString('utf8') } @@ -178,16 +196,7 @@ export async function downloadCloudFile( let offset = 0 try { for (;;) { - const page = (await call('files.readChunk', { - worktree, - relativePath, - offset, - length: CHUNK_BYTES - })) as { - contentBase64: string - bytesRead: number - eof: boolean - } + const page = await readChunk(call, { worktree, relativePath, offset, length: CHUNK_BYTES }) const bytes = Buffer.from(page.contentBase64, 'base64') if (!out.write(bytes)) { await new Promise((resolve) => out.once('drain', () => resolve())) diff --git a/src/main/hosted-sandbox/cloud-transfer.test.ts b/src/main/hosted-sandbox/cloud-transfer.test.ts index 31215da800c..77cbc0285c0 100644 --- a/src/main/hosted-sandbox/cloud-transfer.test.ts +++ b/src/main/hosted-sandbox/cloud-transfer.test.ts @@ -1,5 +1,5 @@ import { execFileSync } from 'node:child_process' -import { mkdtempSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' +import { mkdirSync, mkdtempSync, readFileSync, utimesSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, expect, it, vi } from 'vitest' @@ -128,9 +128,12 @@ describe('cloud transfer housekeeping', () => { mkdirSync(join(root, 'abandoned')) writeFileSync(join(root, 'abandoned', 'transfer.tar'), 'x') mkdirSync(join(root, 'kept', 'unapplied'), { recursive: true }) - expect(await sweepCloudTransfers(root, Date.now())).toBe(1) - expect(await sweepCloudTransfers(root, Date.now() + 31 * 86_400_000)).toBe(1) - expect(await sweepCloudTransfers(join(root, 'missing'), Date.now())).toBe(0) + // Fixed times: the leftovers were written at T0; the sweep runs a minute and a month later. + const T0 = 1_800_000_000_000 + utimesSync(join(root, 'kept', 'unapplied'), T0 / 1000, T0 / 1000) + expect(await sweepCloudTransfers(root, T0 + 60_000)).toBe(1) + expect(await sweepCloudTransfers(root, T0 + 31 * 86_400_000)).toBe(1) + expect(await sweepCloudTransfers(join(root, 'missing'), T0)).toBe(0) }) it('installs only session transcripts, never other files from the project folder', async () => { diff --git a/src/main/hosted-sandbox/cloud-transfer.ts b/src/main/hosted-sandbox/cloud-transfer.ts index 0fd95b01bc7..3b72b6c29cc 100644 --- a/src/main/hosted-sandbox/cloud-transfer.ts +++ b/src/main/hosted-sandbox/cloud-transfer.ts @@ -33,7 +33,7 @@ import { packInCloud, pickCloudWorktree, type Prepared, - type WorktreeRow + WorktreeListingSchema } from './cloud-transfer-steps' import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call' import { findCloudRuntime } from './hosted-workspace-service' @@ -112,7 +112,7 @@ export function installCloudTransferHandlers(deps: { let selector: string | null = null try { step('packing') - const listing = (await call('worktree.list', {})) as { worktrees?: WorktreeRow[] } + const listing = WorktreeListingSchema.parse(await call('worktree.list', {})) const connection = runtime.recipeResult ? getEphemeralVmRecipeResultConnection(runtime.recipeResult) : null diff --git a/src/main/ipc/runtime-environments/runtime-environment-request-preparation.test.ts b/src/main/ipc/runtime-environments/runtime-environment-request-preparation.test.ts index 38bc56ae4a8..0e8608793ce 100644 --- a/src/main/ipc/runtime-environments/runtime-environment-request-preparation.test.ts +++ b/src/main/ipc/runtime-environments/runtime-environment-request-preparation.test.ts @@ -2,8 +2,28 @@ // opening request is held to its caller's pairing as of before that wake, then to the pairing the // wake made; any other change still refuses it. import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { KnownRuntimeEnvironment } from '../../../shared/remote-runtime/host-catalog/runtime-environments' -const BEFORE = { id: 'env-vm', pairingRevision: 1, runtimeId: 'before', createdAt: 0 } +const BEFORE: KnownRuntimeEnvironment = { + id: 'env-vm', + name: 'VM', + pairingRevision: 1, + runtimeId: 'before', + createdAt: 0, + updatedAt: 0, + lastUsedAt: null, + endpoints: [ + { + id: 'ep', + kind: 'websocket', + label: 'VM', + endpoint: 'ws://vm.test', + deviceToken: 'token', + publicKeyB64: 'key' + } + ], + preferredEndpointId: 'ep' +} const AFTER_WAKE = { id: 'env-vm', pairingRevision: 2, runtimeId: 'after', createdAt: 0 } type Prepared = { @@ -73,7 +93,7 @@ describe('a fenced request that waited through a wake already in flight', () => undefined, { pairingRevision, runtimeId: 'before' }, undefined, - { before: BEFORE as never, wokenPairingRevision } + { before: BEFORE, wokenPairingRevision } ) it('passes the pre-wake fence and then expects the pairing that wake made', async () => { diff --git a/src/renderer/src/components/cloud-remote-desktop/CloudRemoteDesktopToast.tsx b/src/renderer/src/components/cloud-remote-desktop/CloudRemoteDesktopToast.tsx index e7c946379d2..f515a34a0f4 100644 --- a/src/renderer/src/components/cloud-remote-desktop/CloudRemoteDesktopToast.tsx +++ b/src/renderer/src/components/cloud-remote-desktop/CloudRemoteDesktopToast.tsx @@ -146,19 +146,20 @@ export function CloudRemoteDesktopToast({ onAction(copy.action!)} + > + {actionLabel(t, copy.action)} + + ) : undefined + } /> - {copy.action ? ( - - ) : null} diff --git a/src/renderer/src/components/settings/cloud-secrets/SecretValueInput.tsx b/src/renderer/src/components/settings/cloud-secrets/SecretValueInput.tsx index 0b12be4e159..39a10bac6f3 100644 --- a/src/renderer/src/components/settings/cloud-secrets/SecretValueInput.tsx +++ b/src/renderer/src/components/settings/cloud-secrets/SecretValueInput.tsx @@ -84,7 +84,8 @@ export function SecretValueInput({ autoFocus={pastedMultiline} variant="code" value={t('cloudSecrets.value.lines', 'Multi-line value · {{count}} lines', { - count: lines + count: lines, + defaultValue_one: 'Multi-line value · {{count}} line' })} aria-invalid={invalid || undefined} aria-describedby={describedBy} diff --git a/src/renderer/src/components/ui/button.tsx b/src/renderer/src/components/ui/button.tsx index 3fbd3b1d756..52caaf0a1cc 100644 --- a/src/renderer/src/components/ui/button.tsx +++ b/src/renderer/src/components/ui/button.tsx @@ -19,6 +19,8 @@ const buttonVariants = cva( variants: { variant: { default: `bg-primary text-primary-foreground hover:bg-primary/90 ${pressClasses} ${disabledSolidClasses}`, + // Primary that stays focusable while unavailable (aria-disabled): reads muted like disabled. + 'default-held': `bg-primary text-primary-foreground hover:bg-primary/90 ${pressClasses} ${disabledSolidClasses} aria-disabled:cursor-default aria-disabled:bg-muted aria-disabled:text-muted-foreground aria-disabled:hover:bg-muted`, destructive: `bg-destructive text-white hover:bg-destructive/90 ${pressClasses} dark:bg-destructive/60 dark:disabled:bg-muted ${disabledSolidClasses}`, outline: `${outlineButtonClasses} ${pressClasses}`, 'orchestration-tab': `${outlineButtonClasses} border-(--crew-border) bg-(--crew-surface) text-card-foreground shadow-floating dark:border-(--crew-border) dark:bg-(--crew-surface) dark:hover:bg-accent dark:hover:text-accent-foreground`, diff --git a/src/renderer/src/components/ui/inline-error.tsx b/src/renderer/src/components/ui/inline-error.tsx index b2aeb411a75..024553dd629 100644 --- a/src/renderer/src/components/ui/inline-error.tsx +++ b/src/renderer/src/components/ui/inline-error.tsx @@ -16,6 +16,8 @@ type InlineErrorProps = { retrying?: boolean /** 'compact' for narrow panels (sidebar sections): tighter padding, xs text. */ density?: 'default' | 'compact' + /** 'toast' leaves room for the toast's own dismiss button and puts the action under the text. */ + layout?: 'inline' | 'toast' /** A next step other than retry (e.g. "Sign in"), rendered inside the box after the text. * Pass an outline xs Button with a short verb-first label; never a primary button. */ action?: React.ReactNode @@ -35,6 +37,7 @@ function InlineError({ onRetry, retrying = false, density = 'default', + layout = 'inline', action, className }: InlineErrorProps): React.JSX.Element { @@ -48,6 +51,7 @@ function InlineError({ // instead of squeezing the text to one word per line. 'flex flex-wrap rounded-lg border', density === 'compact' ? 'gap-2 px-2.5 py-2 text-xs' : 'gap-3 px-3 py-2.5 text-sm', + layout === 'toast' && 'pr-9', detail ? 'items-start' : 'items-center', tone === 'error' ? 'border-destructive/15 bg-destructive/5 dark:border-destructive/25' @@ -77,7 +81,17 @@ function InlineError({

) : null} - {action ?
{action}
: null} + {action ? ( +
+ {action} +
+ ) : null} {onRetry && retryLabel ? (