mirror of
https://github.com/stablyai/orca.git
synced 2026-10-31 17:17:38 +00:00
* 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>
98 lines
3.1 KiB
TypeScript
98 lines
3.1 KiB
TypeScript
import {
|
|
RuntimeRpcEnvelopeSchema,
|
|
type RuntimeRpcResponse,
|
|
isKeepaliveFrame
|
|
} from './runtime-rpc-envelope'
|
|
import { RemoteRuntimeClientError } from './remote-runtime-client-error'
|
|
import { assertJsonTextStructureWithinLimits } from './json-text-structure-limit'
|
|
import type { PairedE2EEAuthReply, PairedE2EEReadyVerdict } from './paired-e2ee-client-handshake'
|
|
|
|
export const REMOTE_RUNTIME_JSON_STRUCTURE_LIMITS = {
|
|
structuralTokens: 256 * 1024,
|
|
nestingDepth: 64
|
|
} as const
|
|
|
|
export function parseRemoteRuntimeJsonText(content: string): unknown {
|
|
assertJsonTextStructureWithinLimits(content, REMOTE_RUNTIME_JSON_STRUCTURE_LIMITS)
|
|
return JSON.parse(content) as unknown
|
|
}
|
|
|
|
export type ParsedRemoteRuntimeFrame =
|
|
| { type: 'keepalive' }
|
|
| { type: 'response'; response: RuntimeRpcResponse<unknown> }
|
|
| { type: 'error'; error: RemoteRuntimeClientError }
|
|
|
|
export function remoteRuntimeUnavailableError(
|
|
message = 'Remote Orca runtime closed the connection.'
|
|
): RemoteRuntimeClientError {
|
|
return new RemoteRuntimeClientError('remote_runtime_unavailable', message)
|
|
}
|
|
|
|
export function remoteRuntimeTimeoutError(): RemoteRuntimeClientError {
|
|
return new RemoteRuntimeClientError(
|
|
'runtime_timeout',
|
|
'Timed out waiting for the remote Orca runtime to respond.'
|
|
)
|
|
}
|
|
|
|
export function invalidRemoteRuntimeResponseError(message: string): RemoteRuntimeClientError {
|
|
return new RemoteRuntimeClientError('invalid_runtime_response', message)
|
|
}
|
|
|
|
export function remoteRuntimeReadyError(
|
|
verdict: PairedE2EEReadyVerdict
|
|
): RemoteRuntimeClientError | null {
|
|
if (verdict === 'accepted') {
|
|
return null
|
|
}
|
|
return invalidRemoteRuntimeResponseError(
|
|
verdict === 'invalid'
|
|
? 'Remote Orca runtime returned an invalid E2EE handshake frame.'
|
|
: 'Remote Orca runtime returned an unexpected E2EE handshake frame.'
|
|
)
|
|
}
|
|
|
|
export function remoteRuntimeAuthReplyError(
|
|
reply: PairedE2EEAuthReply
|
|
): RemoteRuntimeClientError | null {
|
|
if (reply.kind === 'authenticated') {
|
|
return null
|
|
}
|
|
if (reply.kind === 'invalid') {
|
|
return invalidRemoteRuntimeResponseError(
|
|
'Remote Orca runtime returned an invalid E2EE auth frame.'
|
|
)
|
|
}
|
|
return new RemoteRuntimeClientError(
|
|
reply.errorCode === 'unauthorized' ? 'unauthorized' : 'invalid_runtime_response',
|
|
'Remote Orca runtime rejected the pairing token.'
|
|
)
|
|
}
|
|
|
|
export function parseRemoteRuntimeRpcFrame(plaintext: string): ParsedRemoteRuntimeFrame {
|
|
let raw: unknown
|
|
try {
|
|
raw = parseRemoteRuntimeJsonText(plaintext)
|
|
} catch {
|
|
return {
|
|
type: 'error',
|
|
error: invalidRemoteRuntimeResponseError(
|
|
'Remote Orca runtime returned an invalid response frame.'
|
|
)
|
|
}
|
|
}
|
|
if (isKeepaliveFrame(raw)) {
|
|
return { type: 'keepalive' }
|
|
}
|
|
const parsed = RuntimeRpcEnvelopeSchema.safeParse(raw)
|
|
if (!parsed.success || '_keepalive' in parsed.data) {
|
|
return {
|
|
type: 'error',
|
|
error: invalidRemoteRuntimeResponseError(
|
|
'Remote Orca runtime returned an invalid response frame.'
|
|
)
|
|
}
|
|
}
|
|
return { type: 'response', response: parsed.data as RuntimeRpcResponse<unknown> }
|
|
}
|