perf(cloud): wake reconnects without waiting for a saved-pairing probe; one wake timeline log per wake; faster ready poll

This commit is contained in:
Neil
2026-11-09 14:57:49 -08:00
parent 2983b93d33
commit 6b7eb9645c
7 changed files with 134 additions and 16 deletions
@@ -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<void>((resolve) => (release = resolve))
const auth = serverAuth(serverDataPath)
const repaired = new Promise<string>((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)
@@ -27,6 +27,13 @@ export async function applyResumedRuntimePairing(args: {
pairingCode: string
invalidateTransport: (environmentId: string) => unknown
probeSavedPairing?: (connection: PairedRuntimeConnection) => Promise<unknown>
/**
* 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<ResumedRuntimePairingOutcome> {
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(
+6 -1
View File
@@ -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,
@@ -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<string, { startedAt: number; marks: Partial<Record<Phase, number>> }>()
export function noteWakeTimeline(
event: Pick<CloudWakeProgressEvent, 'workspaceId' | 'phase' | 'source' | 'startedAt'>,
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 }))
}
}
@@ -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,
@@ -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))
}
}
@@ -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)
}