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.

12.2 KB
Raw Blame
import type { ConnectOptions, RpcClient, SendRequestOptions } from './rpc-client'
import { DirectConnectionLog } from './direct-connection-log'
import { RpcClientAuthenticationRetry } from './rpc-client-authentication-retry'
import { RpcClientConnectionState } from './rpc-client-connection-state'
import {
  RpcClientReconnectSchedule,
  RPC_RECONNECT_ATTEMPT_LIMIT
} from './rpc-client-reconnect-schedule'
import { buildDirectRpcWireRequest, type RpcRequestFrame } from './rpc-envelope-fields'
import { RpcClientRequestTracker } from './rpc-client-request-tracker'
import { RpcClientSocketCloseController } from './rpc-client-socket-close-controller'
import { RpcClientSocketFactory } from './rpc-client-socket-factory'
import type { RpcClientSocketSession } from './rpc-client-socket-session'
import {
  RpcClientStreamRegistry,
  type RpcStreamingListener,
  type RpcStreamSubscribeOptions
} from './rpc-client-stream-registry'
import {
  DECISIVE_PROBE_TIMEOUT_MS,
  RpcSessionLivenessWatchdog
} from './rpc-session-liveness-watchdog'
import { isStaleForegroundDial } from './rpc-stale-dial'
import type { ConnectionState, ForegroundNudgeReason, RpcResponse } from './types'
import { negotiateMobileRuntimeCapabilities } from './mobile-runtime-capability-negotiation'

const LIVENESS_REQUEST_ID_PREFIX = 'mobile-liveness-'
const DIRECT_FOREGROUND_PROBE_MIN_INTERVAL_MS = 10_000

export class DirectRpcClient implements RpcClient {
  private socketSession: RpcClientSocketSession | null = null
  private readonly connectionState: RpcClientConnectionState
  private readonly reconnect: RpcClientReconnectSchedule
  private readonly requests: RpcClientRequestTracker
  private readonly streams: RpcClientStreamRegistry
  private readonly liveness: RpcSessionLivenessWatchdog
  private readonly socketFactory: RpcClientSocketFactory
  private readonly authenticationRetry: RpcClientAuthenticationRetry
  private readonly socketClose: RpcClientSocketCloseController
  private requestCounter = 0
  private readonly connectionLog: DirectConnectionLog
  private intentionallyClosed = false
  private authenticationGeneration = 0
  private livenessSession: RpcClientSocketSession | null = null

  constructor(
    private readonly endpoint: string,
    private readonly deviceToken: string,
    serverPublicKeyB64: string,
    private readonly options: ConnectOptions
  ) {
    this.connectionLog = new DirectConnectionLog(endpoint, options.onLog)
    this.reconnect = new RpcClientReconnectSchedule({
      openConnection: () => this.openConnection(),
      rejectConnectWaiters: (reason) => this.connectionState.rejectWaiters(reason),
      emitLog: (message, detail) =>
        this.connectionLog.emit('info', message, detail, { code: 'retry-scheduled' })
    })
    this.connectionState = new RpcClientConnectionState({
      endpoint,
      initialListener: options.onStateChange,
      getReconnectAttempt: () => this.reconnect.getAttempt(),
      isClosed: () => this.intentionallyClosed
    })
    this.streams = new RpcClientStreamRegistry({
      nextId: () => this.nextId(),
      getState: () => this.connectionState.get(),
      sendFrame: (frame) => this.sendFrame(frame)
    })
    this.requests = new RpcClientRequestTracker({
      nextId: () => this.nextId(),
      getState: () => this.connectionState.get(),
      waitForConnected: (timeoutMs) => this.waitForConnected(timeoutMs),
      sendFrame: (frame) => this.sendFrame(frame)
    })
    this.liveness = new RpcSessionLivenessWatchdog({
      transport: 'direct',
      voluntaryProbeMinIntervalMs: DIRECT_FOREGROUND_PROBE_MIN_INTERVAL_MS,
      sendProbe: (identity) => identity === this.livenessSession && this.sendLivenessProbe(),
      terminate: (identity) => {
        if (identity === this.livenessSession && this.socketSession === this.livenessSession) {
          this.socketClose.forceClose(this.livenessSession)
        }
      },
      onTimeout: this.connectionLog.livenessTimeout
    })
    this.socketFactory = new RpcClientSocketFactory({
      endpoint,
      deviceToken,
      serverPublicKeyB64,
      getCurrentSocket: () => this.socketSession?.socket ?? null,
      getState: () => this.getState(),
      getReconnectAttempt: () => this.getReconnectAttempt(),
      getLastConnectedAt: () => this.getLastConnectedAt(),
      isIntentionallyClosed: () => this.intentionallyClosed,
      emitLog: this.connectionLog.emit,
      onHandshakeStarted: () => this.connectionState.publish('handshaking'),
      onAuthenticated: (session) => this.handleAuthenticated(session),
      onAuthRejected: (reason) => this.authenticationRetry.reject(reason),
      onRpcResponse: (response) => this.handleRpcResponse(response),
      onBinary: (bytes) => this.streams.handleBinary(bytes),
      onAuthenticatedInbound: (session) => this.liveness.noteAuthenticatedInbound(session),
      onClosed: (session, closeCode) => this.socketClose.handle(session, closeCode),
      onForcedClose: (session) => this.socketClose.forceClose(session)
    })
    this.authenticationRetry = new RpcClientAuthenticationRetry({
      endpoint,
      stopLiveness: () => this.stopLiveness(),
      emitWarning: (message, detail) =>
        this.connectionLog.emit('warn', message, detail, { code: 'authentication-rejected' }),
      retry: (reason) => this.retryAuthentication(reason),
      latchFailure: (reason) => this.latchAuthenticationFailure(reason)
    })
    this.socketClose = new RpcClientSocketCloseController({
      connectionState: this.connectionState,
      reconnect: this.reconnect,
      requests: this.requests,
      streams: this.streams,
      socketFactory: this.socketFactory,
      authenticationRetry: this.authenticationRetry,
      getCurrentSession: () => this.socketSession,
      clearCurrentSession: () => (this.socketSession = null),
      getAuthenticationGeneration: () => this.authenticationGeneration,
      isIntentionallyClosed: () => this.intentionallyClosed,
      stopLiveness: (session) => {
        if (this.livenessSession === session) {
          this.stopLiveness()
        }
      },
      emitWarning: (message, detail, evidence) =>
        this.connectionLog.emit('warn', message, detail, evidence)
    })
    this.openConnection()
  }

  sendRequest(
    method: string,
    params?: unknown,
    options?: SendRequestOptions
  ): Promise<RpcResponse> {
    return this.requests.sendRequest(method, params, options)
  }

  subscribe(
    method: string,
    params: unknown,
    onData: RpcStreamingListener,
    options?: RpcStreamSubscribeOptions
  ): () => void {
    return this.streams.subscribe(method, params, onData, options)
  }

  updateTerminalSubscriptionViewport(
    terminal: string,
    viewport: { cols: number; rows: number }
  ): void {
    this.streams.updateTerminalViewport(terminal, viewport)
  }

  getState = (): ConnectionState => this.connectionState.get()

  getReconnectAttempt = (): number => this.reconnect.getAttempt()

  getLastConnectedAt = (): number | null => this.connectionState.getLastConnectedAt()

  getLastInboundAt = (): number | null => this.liveness.getLastInboundAt() || null

  onStateChange = (listener: (state: ConnectionState) => void): (() => void) =>
    this.connectionState.addListener(listener)

  // The watchdog starts before capability negotiation, when a probe cannot be written yet.
  readonly probeLivenessNow = (): void =>
    void (this.getState() === 'connected' && this.liveness.probeCurrentDecisively())

  notifyForeground(reason: ForegroundNudgeReason = 'focus'): void {
    if (this.intentionallyClosed) {
      return
    }
    if (this.getState() === 'connected') {
      console.log('[net] foreground — probing live connection')
      if (this.livenessSession) {
        if (reason === 'app-resume') {
          this.liveness.probeAfterBackground(this.livenessSession)
        } else {
          this.liveness.probeNow(
            this.livenessSession,
            reason === 'network-change' ? DECISIVE_PROBE_TIMEOUT_MS : null
          )
        }
      }
      return
    }
    const dialing = this.socketSession
    const dialAgeMs = Date.now() - this.socketFactory.getDialStartedAt()
    let abandoned = false
    if (dialing && isStaleForegroundDial(this.getState(), dialAgeMs)) {
      console.log('[net] foreground — abandoning stale dial', {
        state: this.getState(),
        dialAgeMs
      })
      this.socketClose.forceClose(dialing)
      abandoned = true
    }
    if (this.getState() === 'reconnecting') {
      console.log('[net] foreground — restarting reconnect loop', {
        attempt: this.getReconnectAttempt(),
        hadTimer: this.reconnect.hasTimer()
      })
      this.reconnect.redialNow(!abandoned)
    }
  }

  close(): void {
    this.intentionallyClosed = true
    this.reconnect.cancel()
    const session = this.socketSession
    session?.clearTimers()
    if (this.livenessSession) {
      this.liveness.stop(this.livenessSession)
      this.livenessSession = null
    }
    session?.close()
    this.socketSession = null
    session?.clearKey()
    this.connectionState.publish('disconnected')
    this.requests.rejectAll('Client closed', { deliveryUnknown: true })
  }

  private openConnection(): void {
    if (this.intentionallyClosed) {
      return
    }
    this.connectionState.publish('connecting')
    this.socketSession = this.socketFactory.open()
  }

  private handleAuthenticated(session: RpcClientSocketSession): void {
    this.livenessSession = session
    this.liveness.start(session)
    const generation = ++this.authenticationGeneration
    negotiateMobileRuntimeCapabilities({
      sendRequest: (method, params) =>
        this.requests.sendAuthenticatedRequest(method, params, 5_000),
      current: () => this.socketSession === session && this.authenticationGeneration === generation,
      onReady: () => {
        this.reconnect.authenticated()
        this.authenticationRetry.accepted()
        this.connectionState.publish('connected')
        this.connectionLog.connected()
        this.streams.replayAfterAuthentication()
      },
      onFailure: () => this.socketClose.forceClose(session)
    })
  }

  private handleRpcResponse(response: RpcResponse): void {
    if (response.id.startsWith(LIVENESS_REQUEST_ID_PREFIX)) {
      return
    }
    if (!response.ok && response.error.code === 'unauthorized') {
      // Settle this correlated refusal before marking other written requests unknown.
      this.requests.resolve(response)
      return this.authenticationRetry.reject('Unauthorized — pairing may be revoked')
    }
    if (!this.streams.handleResponse(response)) {
      this.requests.resolve(response)
    }
  }

  private stopLiveness(): void {
    if (this.livenessSession) {
      this.liveness.stop(this.livenessSession)
      this.livenessSession = null
    }
  }

  private retryAuthentication(reason: string): void {
    const closing = this.socketSession
    this.socketSession = null
    closing?.clearKey()
    this.streams.markForReplay()
    this.requests.rejectAll(reason, { deliveryUnknown: true })
    closing?.close()
    this.connectionState.publish('reconnecting')
    this.reconnect.schedule()
  }

  private latchAuthenticationFailure(reason: string): void {
    this.intentionallyClosed = true
    this.socketSession?.close()
    this.socketSession = null
    this.connectionState.publish('auth-failed')
    this.requests.rejectAll(reason, { deliveryUnknown: true })
  }

  // The one place a direct request gains its credential and its caller's envelope.
  private sendFrame(frame: RpcRequestFrame): boolean {
    if (this.socketSession) {
      return this.socketSession.sendEncrypted(buildDirectRpcWireRequest(frame, this.deviceToken))
    }
    console.log('[net] sendEncrypted FAILED — channel not ready', {
      hasWs: false,
      hasKey: false,
      state: this.getState()
    })
    return false
  }

  private sendLivenessProbe(): boolean {
    return (
      this.getState() === 'connected' &&
      this.sendFrame({ id: `${LIVENESS_REQUEST_ID_PREFIX}${this.nextId()}`, method: 'status.get' })
    )
  }

  private waitForConnected(timeoutMs?: number): Promise<void> {
    if (
      this.getState() === 'reconnecting' &&
      this.getReconnectAttempt() >= RPC_RECONNECT_ATTEMPT_LIMIT
    ) {
      return Promise.reject(new Error('Connection retry limit reached'))
    }
    return this.connectionState.waitForConnected(timeoutMs)
  }

  private nextId = (): string => `rpc-${++this.requestCounter}-${Date.now()}`
}