Files
orca/src/main/runtime/relay/relay-control-protocol.ts
T
Jinwoo HongandOrca 77b154d5dd Add Orca Relay desktop and mobile transport (#8536)
* feat(mobile): define relay protocol groundwork

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): implement replay-safe E2EE v2 sessions

Co-authored-by: Orca <help@stably.ai>

* test(auth): lock cloud refresh single-flight

Co-authored-by: Orca <help@stably.ai>

* test(mobile): complete E2EE v2 adversarial coverage

Co-authored-by: Orca <help@stably.ai>

* refactor(runtime): unify mobile socket wiring

Co-authored-by: Orca <help@stably.ai>

* feat(runtime): add relay control and data clients

Co-authored-by: Orca <help@stably.ai>

* feat(runtime): coordinate desktop relay sessions

Co-authored-by: Orca <help@stably.ai>

* fix(auth): fence stale cloud session mutations

Co-authored-by: Orca <help@stably.ai>

* feat(runtime): add relay pairing and durable revoke

Co-authored-by: Orca <help@stably.ai>

* feat(runtime): add relay credential pairing RPCs

Co-authored-by: Orca <help@stably.ai>

* feat(settings): show Orca Relay sign-in status

Co-authored-by: Orca <help@stably.ai>

* test(relay): prove desktop lifecycle and E2EE splice

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): persist relay pairing state

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): race direct and relay pairing

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): recover pairing through relay director

Co-authored-by: Orca <help@stably.ai>

* fix(relay): preserve origin controls during drain

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): recover interrupted relay pairing

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): add stable relay RPC sessions

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): supervise direct and relay endpoints

Co-authored-by: Orca <help@stably.ai>

* Cover mobile relay director fallback matrix

Co-authored-by: Orca <help@stably.ai>

* Fix relay settings component test isolation

Co-authored-by: Orca <help@stably.ai>

* Remove unrelated merge formatting drift

Co-authored-by: Orca <help@stably.ai>

* Update runtime connection count integration assertion

Co-authored-by: Orca <help@stably.ai>

* Run mobile typecheck through pnpm

Co-authored-by: Orca <help@stably.ai>

* feat(relay): gate desktop controls on mobile demand

Co-authored-by: Orca <help@stably.ai>

* test(mobile): cover served relay recovery

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): upgrade direct pairings to relay

Co-authored-by: Orca <help@stably.ai>

* fix(relay): harden mobile reconnect and teardown

Co-authored-by: Orca <help@stably.ai>

* fix(auth): clarify account sign-in state

Co-authored-by: Orca <help@stably.ai>

* fix(auth): polish sign-in completion flow

Co-authored-by: Orca <help@stably.ai>

* fix(auth): clarify sign-out confirmation

Co-authored-by: Orca <help@stably.ai>

* fix(auth): simplify sign-in completion page

Co-authored-by: Orca <help@stably.ai>

* feat(mobile): add per-device pairing connection mode

Co-authored-by: Orca <help@stably.ai>

* fix(mobile): stabilize pairing option layout

Co-authored-by: Orca <help@stably.ai>

* fix(mobile): give pairing choices stable space

Co-authored-by: Orca <help@stably.ai>

* fix(mobile): stabilize pairing QR regeneration

Co-authored-by: Orca <help@stably.ai>

* Animate mobile pairing flow height

Co-authored-by: Orca <help@stably.ai>

* Configure auth in packaged builds

Co-authored-by: Orca <help@stably.ai>

* Make Orca Relay pairing an opt-in beta

Co-authored-by: Orca <help@stably.ai>

* Show Relay beta details on hover

Co-authored-by: Orca <help@stably.ai>

* Refine mobile relay pairing choice

Co-authored-by: Orca <help@stably.ai>

* Polish Orca Relay pairing controls

Co-authored-by: Orca <help@stably.ai>

* Keep mobile contract fallback test additive

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-07-14 11:47:05 -07:00

147 lines
4.5 KiB
TypeScript

import { z } from 'zod'
import type { RawData } from 'ws'
import {
DeviceCredentialInstalledSchema,
DeviceResumeConfirmedSchema
} from '../../../shared/mobile-relay-credential-contract'
const OpaqueIdSchema = z.string().min(1).max(128)
const Base64Url32ByteSchema = z.string().regex(/^[A-Za-z0-9_-]{43}$/)
const Base6432ByteSchema = z.string().regex(/^[A-Za-z0-9+/]{43}=$/)
const Base64Raw24ByteSchema = z.string().regex(/^[A-Za-z0-9+/]{32}$/)
const EpochMsSchema = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)
const GenerationSchema = z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER)
export const RelayHostChallengeMessageSchema = z
.object({
type: z.literal('host-challenge'),
challengeId: OpaqueIdSchema,
relayEphemeralPublicKeyB64: Base6432ByteSchema,
nonceB64: Base64Raw24ByteSchema,
ciphertextB64: z
.string()
.min(1)
.max(16 * 1024),
expiresAt: EpochMsSchema
})
.strict()
const PendingConnectionSchema = z
.object({ connId: OpaqueIdSchema, connTicket: Base64Url32ByteSchema })
.strict()
export const RelayHostHelloAckMessageSchema = z
.object({
type: z.literal('host-hello-ack'),
v: z.literal(1),
generation: GenerationSchema,
controlResumeSecret: Base64Url32ByteSchema,
leaseExpiresAt: EpochMsSchema,
activeConnIds: z.array(OpaqueIdSchema).max(8),
pendingConns: z.array(PendingConnectionSchema).max(8)
})
.strict()
export const RelayConnectionOpenMessageSchema = z
.object({
type: z.literal('conn-open'),
connId: OpaqueIdSchema,
connTicket: Base64Url32ByteSchema,
kind: z.enum(['invite', 'resume']),
relayDeviceId: OpaqueIdSchema,
attachDeadlineMs: z.number().int().positive().max(60_000)
})
.strict()
export const RelayDrainMessageSchema = z
.object({
type: z.literal('drain'),
graceMs: z
.number()
.int()
.nonnegative()
.max(60 * 60 * 1000),
recovery: z.literal('resolve-director')
})
.strict()
export const RelayPingMessageSchema = z
.object({ type: z.literal('ping'), t: EpochMsSchema })
.strict()
export const RelayInviteCreatedMessageSchema = z
.object({
type: z.literal('invite-created'),
reqId: OpaqueIdSchema,
inviteToken: Base64Url32ByteSchema,
expiresAt: EpochMsSchema,
maxAttempts: z.number().int().positive().max(16)
})
.strict()
export const RelayDeviceRevokedMessageSchema = z
.object({ type: z.literal('device-revoked'), reqId: OpaqueIdSchema })
.strict()
export const RelayDeviceCredentialInstalledMessageSchema = DeviceCredentialInstalledSchema.extend({
type: z.literal('device-credential-installed')
}).strict()
export const RelayDeviceCredentialInstallStatusResultMessageSchema = z.union([
z
.object({
type: z.literal('device-credential-install-status-result'),
v: z.literal(1),
reqId: OpaqueIdSchema,
state: z.literal('not-found')
})
.strict(),
z
.object({
type: z.literal('device-credential-install-status-result'),
v: z.literal(1),
reqId: OpaqueIdSchema,
state: z.literal('committed'),
result: DeviceCredentialInstalledSchema
})
.strict()
])
export const RelayDeviceResumeConfirmedMessageSchema = DeviceResumeConfirmedSchema.extend({
type: z.literal('device-resume-confirmed')
}).strict()
export const RelayControlErrorMessageSchema = z
.object({
type: z.literal('control-error'),
reqId: OpaqueIdSchema.optional(),
code: z.string().min(1).max(128)
})
.strict()
export type RelayHostChallengeMessage = z.infer<typeof RelayHostChallengeMessageSchema>
export type RelayHostHelloAckMessage = z.infer<typeof RelayHostHelloAckMessageSchema>
export type RelayConnectionOpenMessage = z.infer<typeof RelayConnectionOpenMessageSchema>
export type RelayDrainMessage = z.infer<typeof RelayDrainMessageSchema>
export type RelayInviteCreatedMessage = z.infer<typeof RelayInviteCreatedMessageSchema>
export type RelayDeviceCredentialInstalledMessage = z.infer<
typeof RelayDeviceCredentialInstalledMessageSchema
>
export type RelayDeviceCredentialInstallStatusResultMessage = z.infer<
typeof RelayDeviceCredentialInstallStatusResultMessageSchema
>
export type RelayDeviceResumeConfirmedMessage = z.infer<
typeof RelayDeviceResumeConfirmedMessageSchema
>
export function parseRelayControlMessage(raw: RawData): Record<string, unknown> | null {
try {
const parsed = JSON.parse(raw.toString()) as unknown
return typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: null
} catch {
return null
}
}