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.

2.4 KB
Raw Blame
export const MOBILE_RELAY_CLOSE_CODE = {
  BAD_OUTER_CREDENTIAL: 4401,
  HOST_OFFLINE: 4404,
  PEER_DROPPED: 4408,
  WRONG_CELL: 4409,
  LIMIT_EXCEEDED: 4429,
  DRAINING: 4503
} as const

export type MobileRelayCloseCode =
  (typeof MOBILE_RELAY_CLOSE_CODE)[keyof typeof MOBILE_RELAY_CLOSE_CODE]

// Mirror cloud/packages/relay-contract RELAY_PHONE_CLOSE_REASON for phone sockets, distinct from host control closes. Older peers omit unknown reasons.
export const MOBILE_RELAY_PHONE_CLOSE_REASON = {
  // 4404 with live control but missed data attach deadline; allow one quick same-cell redial.
  HOST_ATTACH_TIMEOUT: 'attach-timeout'
} as const
export type MobileRelayLeg = 'host-control' | 'host-data' | 'phone-invite' | 'phone-resume'

export type MobileRelayRecovery =
  | { kind: 'disable-relay-credential'; directUnaffected: true }
  | { kind: 'retry-after-host-offline'; fullJitter: true }
  | { kind: 'reconnect-fresh-e2ee'; fullJitter: true }
  | { kind: 'resolve-invite-through-director-ws'; requireStrictlyNewerEpoch: true }
  | { kind: 'resolve-resume-through-director-post' }
  | { kind: 'request-director-assignment' }
  | { kind: 'backoff'; fullJitter: true }
  | { kind: 'resolve-configured-director'; fullJitter: true }

export function mobileRelayRecoveryFor(
  code: MobileRelayCloseCode,
  leg: MobileRelayLeg
): MobileRelayRecovery {
  switch (code) {
    case MOBILE_RELAY_CLOSE_CODE.BAD_OUTER_CREDENTIAL:
      return { kind: 'disable-relay-credential', directUnaffected: true }
    case MOBILE_RELAY_CLOSE_CODE.HOST_OFFLINE:
      return { kind: 'retry-after-host-offline', fullJitter: true }
    case MOBILE_RELAY_CLOSE_CODE.PEER_DROPPED:
      return { kind: 'reconnect-fresh-e2ee', fullJitter: true }
    case MOBILE_RELAY_CLOSE_CODE.WRONG_CELL:
      if (leg === 'phone-invite') {
        return { kind: 'resolve-invite-through-director-ws', requireStrictlyNewerEpoch: true }
      }
      return leg === 'phone-resume'
        ? { kind: 'resolve-resume-through-director-post' }
        : { kind: 'request-director-assignment' }
    case MOBILE_RELAY_CLOSE_CODE.LIMIT_EXCEEDED:
      return { kind: 'backoff', fullJitter: true }
    case MOBILE_RELAY_CLOSE_CODE.DRAINING:
      return { kind: 'resolve-configured-director', fullJitter: true }
  }
}

export function isMobileRelayCloseCode(value: number): value is MobileRelayCloseCode {
  return Object.values(MOBILE_RELAY_CLOSE_CODE).some((code) => code === value)
}