Files
orca/src/shared/remote-runtime-shared-control-ready.ts
T
OrcaWinandm4air f9794d8efc refactor(remote): one paired E2EE handshake core for desktop, CLI, web and mobile clients (#27523)
* refactor(remote): one paired E2EE handshake core for desktop, CLI, web and mobile clients

Every paired client now builds its hello, checks the host's ready, seals its
auth frame and opens host frames through src/shared/paired-e2ee-client-handshake.
Platforms inject their own NaCl/base64 primitives. The v2 client session and its
pure-JS key schedule (@noble/hashes 1.8.0, the version mobile pins) move to
src/shared, and the per-host v2 latch policy is shared with an injected store.

No wire change: frames match the captured legacy and v2 fixtures byte for byte,
and a cross-version test drives the core against the released host and the
released client against this host. The auth step is now async so a device-key
signer can slot in later.

* fix(remote): keep @noble/hashes out of the CLI's unbundled runtime

The packaged CLI loads src/shared output from disk, so a bare @noble/hashes
import failed the packaged-runtime check. The v2 key schedule is now a
primitive: Node clients use the node:crypto schedule (moved to src/shared so
the CLI can reach it), while the browser and phone keep the pure-JS one.

* fix(mobile): re-check the v2 latch before sending a legacy auth frame

The auth step is async, so another socket can latch the host to v2 while a
legacy auth frame is being sealed. Refuse it at send time, as before.

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
2026-10-11 12:14:51 -07:00

107 lines
3.2 KiB
TypeScript

import type { PairedE2EEClientHandshake } from './paired-e2ee-client-handshake'
import WebSocket from 'ws'
import { abortSignalReason } from './abort-signal-reason'
import { RemoteRuntimeClientError } from './remote-runtime-client-error'
import { REMOTE_RUNTIME_MAX_READY_WAITERS } from './remote-runtime-memory-limits'
import { remoteRuntimeUnavailableError } from './remote-runtime-request-frames'
import type {
SharedControlConnectionState,
SharedControlReadyWaiter
} from './remote-runtime-shared-control-types'
export function isSharedControlReady(args: {
state: SharedControlConnectionState
ws: WebSocket | null
handshake: PairedE2EEClientHandshake | null
}): boolean {
return args.state === 'ready' && args.ws?.readyState === WebSocket.OPEN && !!args.handshake
}
export function openIfSocketClosed(ws: WebSocket | null, open: () => void): void {
if (!ws || ws.readyState === WebSocket.CLOSED || ws.readyState === WebSocket.CLOSING) {
open()
}
}
export function waitForSharedControlReadyWithTimeout(args: {
readyWaiters: SharedControlReadyWaiter[]
timeoutMs: number
open: () => void
signal?: AbortSignal
}): Promise<void> {
if (args.signal?.aborted) {
return Promise.reject(abortSignalReason(args.signal))
}
if (args.readyWaiters.length >= REMOTE_RUNTIME_MAX_READY_WAITERS) {
return Promise.reject(
new RemoteRuntimeClientError(
'remote_runtime_busy',
'Remote runtime connection wait limit reached; retry after pending work finishes.'
)
)
}
return new Promise<void>((resolve, reject) => {
let settled = false
let waiter!: SharedControlReadyWaiter
const timeout = setTimeout(() => {
if (settled) {
return
}
settled = true
const index = args.readyWaiters.indexOf(waiter)
if (index !== -1) {
args.readyWaiters.splice(index, 1)
}
args.signal?.removeEventListener('abort', onAbort)
reject(remoteRuntimeUnavailableError())
}, args.timeoutMs)
const onAbort = (): void => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
const index = args.readyWaiters.indexOf(waiter)
if (index !== -1) {
args.readyWaiters.splice(index, 1)
}
reject(abortSignalReason(args.signal!))
}
waiter = {
resolve: () => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
args.signal?.removeEventListener('abort', onAbort)
resolve()
},
reject: (error) => {
if (settled) {
return
}
settled = true
clearTimeout(timeout)
args.signal?.removeEventListener('abort', onAbort)
reject(error)
}
}
args.readyWaiters.push(waiter)
args.signal?.addEventListener('abort', onAbort, { once: true })
if (args.signal?.aborted) {
onAbort()
return
}
try {
args.open()
} catch (error) {
const index = args.readyWaiters.indexOf(waiter)
if (index !== -1) {
args.readyWaiters.splice(index, 1)
}
waiter.reject(error instanceof Error ? error : remoteRuntimeUnavailableError(String(error)))
}
})
}