From 6b7eb9645c6b79c2c3db86695394b4dcc72136bf Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 14:57:49 -0800 Subject: [PATCH] perf(cloud): wake reconnects without waiting for a saved-pairing probe; one wake timeline log per wake; faster ready poll --- .../runtime-resume-pairing.test.ts | 52 ++++++++++++++++++- .../ephemeral-vm/runtime-resume-pairing.ts | 40 ++++++++++++-- src/main/ephemeral-vm/workspace-resume.ts | 7 ++- .../hosted-sandbox/cloud-wake-timeline.ts | 23 ++++++++ .../hosted-sandbox/hosted-session-registry.ts | 10 +++- .../hosted-workspace-runtime.ts | 3 +- .../hosted-workspace-service.ts | 15 +++--- 7 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 src/main/hosted-sandbox/cloud-wake-timeline.ts diff --git a/src/main/ephemeral-vm/runtime-resume-pairing.test.ts b/src/main/ephemeral-vm/runtime-resume-pairing.test.ts index 1cb7937226d..d97cbcabb20 100644 --- a/src/main/ephemeral-vm/runtime-resume-pairing.test.ts +++ b/src/main/ephemeral-vm/runtime-resume-pairing.test.ts @@ -124,6 +124,7 @@ describe('resuming a recipe VM server', () => { ) expect(outcome).toMatchObject({ reusedPairing: true, endpointChanged: false }) expect(listEnvironments(desktopDataPath)[0]).toEqual(before) + expect(invalidateTransport).toHaveBeenCalledTimes(1) expect(invalidateTransport).toHaveBeenCalledWith(environmentId) }) @@ -175,11 +176,58 @@ describe('resuming a recipe VM server', () => { expect(next.deviceToken).not.toBe(original.deviceToken) expect(next.publicKeyB64).toBe(savedKeySpelling) expect(listEnvironments(desktopDataPath)[0]!.pairingRevision).not.toBe(before.pairingRevision) - // Only once, after the probe: reconnecting earlier would dial with the rejected token. - expect(invalidateTransport).toHaveBeenCalledTimes(1) + // Dialed alongside the probe, then again once the re-pair replaced the rejected token. + expect(invalidateTransport).toHaveBeenCalledTimes(2) connect(serverDataPath, desktopDataPath, environmentId) }) + it('starts the reconnect before the saved-pairing probe answers', async () => { + const environmentId = provision() + const order: string[] = [] + await applyResumedRuntimePairing({ + userDataPath: desktopDataPath, + environmentId, + pairingCode: bootVm(serverDataPath, 'wss://vm-1.example/runtime'), + invalidateTransport: () => order.push('invalidate'), + probeSavedPairing: async () => { + order.push('probe') + } + }) + expect(order).toEqual(['invalidate', 'probe']) + }) + + it('answers before the probe when confirming in the background, and re-pairs later if needed', async () => { + const environmentId = provision() + const before = listEnvironments(desktopDataPath)[0]! + rmSync(serverDataPath, { recursive: true, force: true }) + serverDataPath = mkdtempSync(join(tmpdir(), 'orca-resume-server-')) + const invalidateTransport = vi.fn() + let release!: () => void + const gate = new Promise((resolve) => (release = resolve)) + const auth = serverAuth(serverDataPath) + const repaired = new Promise((resolve) => { + void applyResumedRuntimePairing({ + userDataPath: desktopDataPath, + environmentId, + pairingCode: bootVm(serverDataPath, 'wss://vm-1.example/runtime', unpaddedHostKey), + invalidateTransport, + probeSavedPairing: async (connection) => { + await gate + return auth(connection) + }, + confirmInBackground: true, + onRepairedInBackground: (environment) => resolve(String(environment.pairingRevision)) + }).then((outcome) => { + // Answered while the probe is still held: the kept pairing, transport already redialing. + expect(outcome.reusedPairing).toBe(true) + expect(invalidateTransport).toHaveBeenCalledTimes(1) + release() + }) + }) + expect(await repaired).not.toBe(String(before.pairingRevision)) + expect(invalidateTransport).toHaveBeenCalledTimes(2) + }) + it('keeps the saved pairing when the server cannot be reached to check it', async () => { const environmentId = provision() const original = resolveEnvironmentPairingOffer(desktopDataPath, environmentId) diff --git a/src/main/ephemeral-vm/runtime-resume-pairing.ts b/src/main/ephemeral-vm/runtime-resume-pairing.ts index 46543df17c0..0994d3162e8 100644 --- a/src/main/ephemeral-vm/runtime-resume-pairing.ts +++ b/src/main/ephemeral-vm/runtime-resume-pairing.ts @@ -27,6 +27,13 @@ export async function applyResumedRuntimePairing(args: { pairingCode: string invalidateTransport: (environmentId: string) => unknown probeSavedPairing?: (connection: PairedRuntimeConnection) => Promise + /** + * Confirm a kept token off the critical path: answer at once and re-pair later only if the + * server turns out to have lost the device. For wakes, where every connection setup is latency. + */ + confirmInBackground?: boolean + /** Called when a background confirmation had to re-pair (the environment's pairing changed). */ + onRepairedInBackground?: (environment: KnownRuntimeEnvironment) => void now?: number }): Promise { const { repairFromFreshOffer, ...refreshed } = refreshEnvironmentFromResumePairingCode( @@ -34,13 +41,36 @@ export async function applyResumedRuntimePairing(args: { args.environmentId, { pairingCode: args.pairingCode, now: args.now } ) - const outcome = refreshed.reusedPairing - ? await confirmSavedPairing(refreshed, repairFromFreshOffer, args.probeSavedPairing) - : refreshed // Why also on an unchanged endpoint: the VM restarted, so any open socket is already dead. - // Why after the probe: reconnecting first would dial with a token about to be replaced. + if (!refreshed.reusedPairing) { + void args.invalidateTransport(args.environmentId) + return refreshed + } + // Why before the probe: a kept token is almost always still valid, so the real connection dials + // while the probe confirms it instead of after (one connection setup off the wake path). In the + // rare case the server lost the device, the re-pair below drops that socket and dials again. void args.invalidateTransport(args.environmentId) - return outcome + const confirmed = confirmSavedPairing( + refreshed, + repairFromFreshOffer, + args.probeSavedPairing + ).then((outcome) => { + if (!outcome.reusedPairing) { + void args.invalidateTransport(args.environmentId) + } + return outcome + }) + if (!args.confirmInBackground) { + return confirmed + } + void confirmed + .then((outcome) => { + if (!outcome.reusedPairing) { + args.onRepairedInBackground?.(outcome.environment) + } + }) + .catch(() => undefined) + return refreshed } async function confirmSavedPairing( diff --git a/src/main/ephemeral-vm/workspace-resume.ts b/src/main/ephemeral-vm/workspace-resume.ts index 64310389bf0..ece8cadf12d 100644 --- a/src/main/ephemeral-vm/workspace-resume.ts +++ b/src/main/ephemeral-vm/workspace-resume.ts @@ -139,7 +139,12 @@ export async function resumeEphemeralVmWorkspace( userDataPath, environmentId: runtime.runtimeEnvironmentId, pairingCode, - invalidateTransport: invalidateRuntimeEnvironmentTransport + invalidateTransport: invalidateRuntimeEnvironmentTransport, + // The wake's own reconnect proves the kept token; a separate probe first would put one more + // connection setup between the click and the terminal. + confirmInBackground: true, + onRepairedInBackground: (repaired) => + noteRecipeVmWakePairing(repaired.id, repaired.pairingRevision ?? repaired.createdAt, true) }) noteRecipeVmWakePairing( environment.id, diff --git a/src/main/hosted-sandbox/cloud-wake-timeline.ts b/src/main/hosted-sandbox/cloud-wake-timeline.ts new file mode 100644 index 00000000000..b60d29fafc2 --- /dev/null +++ b/src/main/hosted-sandbox/cloud-wake-timeline.ts @@ -0,0 +1,23 @@ +// One log line per finished wake with the time each phase was reached, so wake latency can be +// read off a real run (click → resume answered → usable) without a profiler attached. +import type { CloudWakeProgressEvent } from '../../shared/hosted-sandbox/cloud-workspace-api' + +type Phase = CloudWakeProgressEvent['phase'] +const open = new Map> }>() + +export function noteWakeTimeline( + event: Pick, + now: number, + log: (line: string) => void = (line) => console.log(line) +): void { + let entry = open.get(event.workspaceId) + if (!entry || entry.startedAt !== event.startedAt) { + entry = { startedAt: event.startedAt, marks: {} } + open.set(event.workspaceId, entry) + } + entry.marks[event.phase] ??= now - event.startedAt + if (event.phase === 'ready' || event.phase === 'failed') { + open.delete(event.workspaceId) + log(JSON.stringify({ event: 'cloud_wake_timeline', source: event.source, ms: entry.marks })) + } +} diff --git a/src/main/hosted-sandbox/hosted-session-registry.ts b/src/main/hosted-sandbox/hosted-session-registry.ts index 9150eb10afe..b3be73ef324 100644 --- a/src/main/hosted-sandbox/hosted-session-registry.ts +++ b/src/main/hosted-sandbox/hosted-session-registry.ts @@ -129,7 +129,15 @@ export function createHostedSessionRegistry(deps: { userDataPath: deps.userDataPath, environmentId: runtimeEnvironmentId, pairingCode: getEphemeralVmRecipeResultPairingCode(ready.result) ?? '', - invalidateTransport: invalidateRuntimeEnvironmentTransport + invalidateTransport: invalidateRuntimeEnvironmentTransport, + // Restart/rollover: the reconnect proves the kept token; re-pair later only if refused. + confirmInBackground: true, + onRepairedInBackground: (repaired) => + noteRecipeVmWakePairing( + repaired.id, + repaired.pairingRevision ?? repaired.createdAt, + true + ) }) noteRecipeVmWakePairing( environment.id, diff --git a/src/main/hosted-sandbox/hosted-workspace-runtime.ts b/src/main/hosted-sandbox/hosted-workspace-runtime.ts index b786880fc3e..590d1596502 100644 --- a/src/main/hosted-sandbox/hosted-workspace-runtime.ts +++ b/src/main/hosted-sandbox/hosted-workspace-runtime.ts @@ -240,7 +240,8 @@ export async function announceWakeUsable( } return } - await new Promise((resolve) => setTimeout(resolve, 250)) + // Short beat: this sits on the wake's critical path until the runtime record flips to running. + await new Promise((resolve) => setTimeout(resolve, 50)) } } diff --git a/src/main/hosted-sandbox/hosted-workspace-service.ts b/src/main/hosted-sandbox/hosted-workspace-service.ts index 5a86ccb93e8..33b75e2c85e 100644 --- a/src/main/hosted-sandbox/hosted-workspace-service.ts +++ b/src/main/hosted-sandbox/hosted-workspace-service.ts @@ -21,6 +21,7 @@ import { invalidateRuntimeEnvironmentTransport } from '../ipc/runtime-environmen import { autoResumeAgentsAfterRollover, listRestartOfferIds } from './hosted-agent-auto-resume' import type { HostedSandboxControlPlaneClient } from './hosted-sandbox-control-plane-client' import { toReady } from './hosted-sandbox-driver' +import { noteWakeTimeline } from './cloud-wake-timeline' import { createHostedRuntimeEnvironmentCall } from './hosted-runtime-environment-call' import { NOT_CLOUD, @@ -86,7 +87,11 @@ export function installHostedWorkspaceService(deps: { userDataPath: deps.userDataPath, environmentId: runtimeEnvironmentId, pairingCode: ready.connection.pairingCode, - invalidateTransport: invalidateRuntimeEnvironmentTransport + invalidateTransport: invalidateRuntimeEnvironmentTransport, + // Restart/rollover: the reconnect proves the kept token; re-pair later only if refused. + confirmInBackground: true, + onRepairedInBackground: (repaired) => + noteRecipeVmWakePairing(repaired.id, repaired.pairingRevision ?? repaired.createdAt, true) }) noteRecipeVmWakePairing( environment.id, @@ -112,11 +117,9 @@ export function installHostedWorkspaceService(deps: { if (event.phase === 'ready' || event.phase === 'failed') { wakes.delete(event.workspaceId) } - broadcast(CH.wakeProgress, { - ...event, - source, - at: Date.now() - } satisfies CloudWakeProgressEvent) + const at = Date.now() + noteWakeTimeline({ ...event, source }, at) + broadcast(CH.wakeProgress, { ...event, source, at } satisfies CloudWakeProgressEvent) if (event.phase === 'connecting' && event.source !== 'restart') { void announceReadyWhenRunning(event.workspaceId, event.startedAt) }