Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

10.4 KB
Raw Blame
import { Platform } from 'react-native'
import { connect, type ConnectOptions } from './rpc-client'
import { resolvePairingHostIdentity, savePairedHost } from './host-store'
import type { HostProfile, PairingOffer } from './types'
import { isPairingRelayRpcUnavailable } from './pairing-relay-rpc-unavailable'
import {
  relayCredentialProvision,
  relayPairingEndpointsRead
} from './mobile-relay-pairing-operations'
import {
  createMobileRelayPairingJournal,
  type MobileRelayPairingJournal
} from './mobile-relay-pairing-journal'
import {
  clearMobileRelayPairingJournal,
  releaseMobileRelayPairingJournal,
  saveMobileRelayPairingJournal,
  updateMobileRelayPairingJournal
} from './mobile-relay-pairing-journal-store'
import { settleMobileRelayPairingRecovery } from './mobile-relay-pairing-recovery'
import {
  promotePairingJournalCredential,
  writeMobileRelayCredentialBundle
} from './mobile-relay-credential-bundle'
import {
  connectMobileRelayForPairing,
  type PairingCandidateClient
} from './mobile-relay-physical-client'
import { racePairingCandidates, type PairingCandidate } from './pairing-candidate-race'
import { attributePairingLogPath } from './pairing-log-path'
import { resolvePairingInviteThroughDirector } from './mobile-relay-invite-director'
import { createRecoveringPairingRelayCandidate } from './pairing-relay-candidate'
import { createPairingRelayLogger } from './pairing-relay-log'
import { redactSocketEndpoint } from './socket-event-debug'
import { assertCommittedInstall, relayHost } from './pairing-relay-host'
import { recordHostDescriptorFromStatus } from './host-descriptor-recorder'
import type { HostStatusReply } from './host-status-reply-schema'

export type PreProfilePairingAttempt = {
  readonly result: Promise<{ hostId: string }>
  readonly timedOut: boolean
  dispose(): void
}

type Dependencies = {
  connectDirect: typeof connect
  connectRelay: typeof connectMobileRelayForPairing
  resolveInviteDirector: typeof resolvePairingInviteThroughDirector
  resolveHostIdentity: typeof resolvePairingHostIdentity
  savePairedHost: typeof savePairedHost
  saveJournal: typeof saveMobileRelayPairingJournal
  updateJournal: typeof updateMobileRelayPairingJournal
  clearJournal: typeof clearMobileRelayPairingJournal
  releaseJournal: typeof releaseMobileRelayPairingJournal
  recoverPendingJournal: () => Promise<void>
  writeCredentialBundle: typeof writeMobileRelayCredentialBundle
  recordDescriptorFromStatus: typeof recordHostDescriptorFromStatus
  now: () => number
  platform: string
}

const defaultDependencies: Dependencies = {
  connectDirect: connect,
  connectRelay: connectMobileRelayForPairing,
  resolveInviteDirector: resolvePairingInviteThroughDirector,
  resolveHostIdentity: resolvePairingHostIdentity,
  savePairedHost,
  saveJournal: saveMobileRelayPairingJournal,
  updateJournal: updateMobileRelayPairingJournal,
  clearJournal: clearMobileRelayPairingJournal,
  releaseJournal: releaseMobileRelayPairingJournal,
  recoverPendingJournal: () => settleMobileRelayPairingRecovery(PENDING_RECOVERY_WAIT_MS),
  writeCredentialBundle: writeMobileRelayCredentialBundle,
  recordDescriptorFromStatus: recordHostDescriptorFromStatus,
  now: Date.now,
  platform: Platform.OS
}

// How long a new scan lets a previous attempt's recovery publish a committed
// install before the new journal supersedes it.
const PENDING_RECOVERY_WAIT_MS = 8_000

export function startPreProfilePairing(args: {
  offer: PairingOffer
  timeoutMs: number
  connectOptions?: ConnectOptions
  dependencies?: Partial<Dependencies>
}): PreProfilePairingAttempt {
  const dependencies = { ...defaultDependencies, ...args.dependencies }
  const clients = new Set<PairingCandidateClient>()
  let disposed = false
  let timedOut = false
  let timer: ReturnType<typeof setTimeout> | null = null

  const dispose = (): void => {
    if (disposed) {
      return
    }
    disposed = true
    if (timer) {
      clearTimeout(timer)
      timer = null
    }
    for (const client of clients) {
      client.close()
    }
    clients.clear()
  }

  timer = setTimeout(() => {
    timedOut = true
    dispose()
  }, args.timeoutMs)

  let savedJournalId: string | null = null
  const result = runPairing(
    args.offer,
    args.connectOptions,
    dependencies,
    clients,
    () => disposed,
    (journalId) => {
      savedJournalId = journalId
    }
  )
    .catch((error: unknown) => {
      if (timedOut) {
        throw new Error('mobile pairing timed out')
      }
      throw error
    })
    .finally(() => {
      if (savedJournalId) {
        dependencies.releaseJournal(savedJournalId)
      }
      if (timer) {
        clearTimeout(timer)
        timer = null
      }
      for (const client of clients) {
        client.close()
      }
      clients.clear()
    })

  return {
    result,
    get timedOut() {
      return timedOut
    },
    dispose
  }
}

async function runPairing(
  offer: PairingOffer,
  connectOptions: ConnectOptions | undefined,
  dependencies: Dependencies,
  clients: Set<PairingCandidateClient>,
  isDisposed: () => boolean,
  onJournalSaved: (journalId: string) => void
): Promise<{ hostId: string }> {
  const now = dependencies.now()
  // Use the preserved host id for every artifact so re-pairing updates one card (STA-1840).
  const { id: hostId, name: hostName } = await dependencies.resolveHostIdentity(
    offer.publicKeyB64,
    `host-${now}`
  )
  assertActive(isDisposed)
  let journal: MobileRelayPairingJournal | null = null
  if (offer.relay && dependencies.platform !== 'web') {
    // Give committed recovery a bounded publication chance before the new scan supersedes its journal.
    await dependencies.recoverPendingJournal()
    assertActive(isDisposed)
    journal = createMobileRelayPairingJournal({
      offer: { ...offer, relay: offer.relay },
      hostId,
      hostName,
      now
    })
    await dependencies.saveJournal(journal)
    onJournalSaved(journal.metadata.journalId)
    assertActive(isDisposed)
  }

  const directClient = dependencies.connectDirect(
    offer.endpoint,
    offer.deviceToken,
    offer.publicKeyB64,
    { ...connectOptions, onLog: attributePairingLogPath('direct', connectOptions?.onLog) }
  )
  clients.add(directClient)
  const candidates: PairingCandidate[] = [{ path: 'direct', client: directClient }]
  const log = createPairingRelayLogger(connectOptions?.onLog)
  if (journal) {
    log(
      'info',
      'Relay: pairing candidate started',
      redactSocketEndpoint(journal.metadata.relay.cellUrl)
    )
    const relayClient = createRecoveringPairingRelayCandidate({
      journal,
      connect: (relay, onLog) =>
        dependencies.connectRelay({
          relay,
          deviceToken: offer.deviceToken,
          desktopPublicKeyB64: offer.publicKeyB64,
          onLog
        }),
      resolveDirector: (relay) => dependencies.resolveInviteDirector({ relay }),
      persistMove: async (relay) => {
        journal = {
          ...journal!,
          metadata: {
            ...journal!.metadata,
            relay: {
              ...journal!.metadata.relay,
              cellUrl: relay.cellUrl,
              assignmentEpoch: relay.assignmentEpoch
            }
          }
        }
        await dependencies.updateJournal(journal.metadata.journalId, () => journal!.metadata)
      },
      now: dependencies.now,
      onLog: attributePairingLogPath('relay', connectOptions?.onLog)
    })
    clients.add(relayClient)
    candidates.push({ path: 'relay', client: relayClient })
  }
  const winner = await racePairingCandidates(candidates)
  log('success', 'Pairing path selected', `winner: ${winner.path}`)
  assertActive(isDisposed)

  if (!journal) {
    await dependencies.savePairedHost(baseHost(offer, hostId, hostName, now))
    recordWinnerDescriptor(dependencies, hostId, winner.status)
    return { hostId }
  }

  journal = {
    ...journal,
    metadata: {
      ...journal.metadata,
      winner: winner.path,
      authorizationMode: winner.path === 'direct' ? 'authenticated-direct' : 'relay-basis'
    }
  }
  await dependencies.updateJournal(journal.metadata.journalId, () => journal!.metadata)
  const provision = await relayCredentialProvision.request(winner.client, {
    reqId: journal.metadata.installReqId,
    newResumeTokenHash: journal.metadata.pendingResumeTokenHash
  })
  if (isPairingRelayRpcUnavailable(provision)) {
    if (winner.path !== 'direct') {
      throw new Error('relay pairing RPC unavailable after relay path authentication')
    }
    // Log the refusal code because LAN-only success otherwise hides why relay was unavailable.
    log('info', 'Relay: desktop will not serve relay pairing', provision.error.code)
    await dependencies.savePairedHost(baseHost(offer, hostId, hostName, now))
    await dependencies.clearJournal(journal.metadata.journalId)
    recordWinnerDescriptor(dependencies, hostId, winner.status)
    return { hostId }
  }
  const installed = relayCredentialProvision.interpret(provision)
  const endpointsReply = await relayPairingEndpointsRead.request(winner.client, {
    installReqId: journal.metadata.installReqId
  })
  const endpoints = relayPairingEndpointsRead.interpret(endpointsReply)
  assertCommittedInstall(endpoints.installStatus, installed)
  if (!endpoints.relay) {
    throw new Error('desktop returned no relay endpoint after credential install')
  }
  assertActive(isDisposed)
  await dependencies.writeCredentialBundle(promotePairingJournalCredential({ journal, installed }))
  await dependencies.savePairedHost(relayHost(journal, endpoints.relay))
  await dependencies.clearJournal(journal.metadata.journalId)
  recordWinnerDescriptor(dependencies, hostId, winner.status)
  return { hostId }
}

// After save, record descriptors on the same serialized chain so pairing returns the adopted machine name.
// Swallow bookkeeping failures; the host is already committed.
function recordWinnerDescriptor(
  dependencies: Dependencies,
  hostId: string,
  status: HostStatusReply | null
): void {
  if (!status) {
    return
  }
  try {
    dependencies.recordDescriptorFromStatus(hostId, status)
  } catch {
    // Best-effort bookkeeping; the host is already saved.
  }
}

function baseHost(
  offer: PairingOffer,
  hostId: string,
  name: string,
  lastConnected: number
): HostProfile {
  return {
    id: hostId,
    name,
    endpoint: offer.endpoint,
    deviceToken: offer.deviceToken,
    publicKeyB64: offer.publicKeyB64,
    lastConnected
  }
}

function assertActive(isDisposed: () => boolean): void {
  if (isDisposed()) {
    throw new Error('mobile pairing cancelled')
  }
}