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.

3.8 KB
Raw Blame
import type { RpcClientAuthenticationRetry } from './rpc-client-authentication-retry'
import type { RpcClientConnectionState } from './rpc-client-connection-state'
import type { RpcClientReconnectSchedule } from './rpc-client-reconnect-schedule'
import type { RpcClientRequestTracker } from './rpc-client-request-tracker'
import type { RpcClientSocketFactory } from './rpc-client-socket-factory'
import type { RpcClientSocketSession } from './rpc-client-socket-session'
import type { RpcClientStreamRegistry } from './rpc-client-stream-registry'
import { RpcSynthesizedCloseIndex } from './rpc-socket-close-evidence'
import type { ConnectionLogEntry } from './types'

const UNAUTHORIZED_CLOSE_CODE = 4001

type SocketCloseControllerOptions = {
  connectionState: RpcClientConnectionState
  reconnect: RpcClientReconnectSchedule
  requests: RpcClientRequestTracker
  streams: RpcClientStreamRegistry
  socketFactory: RpcClientSocketFactory
  authenticationRetry: RpcClientAuthenticationRetry
  getCurrentSession: () => RpcClientSocketSession | null
  clearCurrentSession: () => void
  getAuthenticationGeneration: () => number
  isIntentionallyClosed: () => boolean
  stopLiveness: (session: RpcClientSocketSession) => void
  emitWarning: (
    message: string,
    detail: string,
    evidence?: Pick<ConnectionLogEntry, 'code' | 'path'>
  ) => void
}

export class RpcClientSocketCloseController {
  private readonly synthesizedCloses = new RpcSynthesizedCloseIndex()

  constructor(private readonly options: SocketCloseControllerOptions) {}

  forceClose(session: RpcClientSocketSession): void {
    session.close()
    if (this.options.getCurrentSession() === session) {
      this.synthesizedCloses.remember(session.socket, this.options.getAuthenticationGeneration())
      this.handle(session)
    }
  }

  handle(session: RpcClientSocketSession, closeCode?: number): void {
    if (this.options.getCurrentSession() !== session) {
      if (
        this.synthesizedCloses.takeUnauthorized(
          session.socket,
          closeCode,
          this.options.getAuthenticationGeneration(),
          UNAUTHORIZED_CLOSE_CODE
        )
      ) {
        this.options.authenticationRetry.reject('Unauthorized — pairing may be revoked', true)
        return
      }
      console.log('[net] handleSocketClosed STALE — ignoring (ws already swapped)', {
        state: this.options.connectionState.get(),
        attempt: this.options.reconnect.getAttempt()
      })
      return
    }
    this.options.socketFactory.noteClosed()
    session.clearTimers()
    session.clearKey()
    this.options.clearCurrentSession()
    this.options.streams.markForReplay()
    this.options.stopLiveness(session)
    if (this.options.isIntentionallyClosed()) {
      console.log('[net] handleSocketClosed — intentional close')
      this.options.connectionState.publish('disconnected')
      this.options.requests.rejectAll('Connection closed', { deliveryUnknown: true })
      return
    }
    if (closeCode === UNAUTHORIZED_CLOSE_CODE) {
      console.log('[net] handleSocketClosed — unauthorized close code', {
        attempt: this.options.reconnect.getAttempt()
      })
      this.options.authenticationRetry.reject('Unauthorized — pairing may be revoked')
      return
    }
    console.log('[net] handleSocketClosed → reconnect', {
      pendingCount: this.options.requests.size(),
      streamCount: this.options.streams.size(),
      attempt: this.options.reconnect.getAttempt()
    })
    this.options.emitWarning(
      'WebSocket closed',
      `${closeCode == null ? 'Close code unavailable' : `Close code ${closeCode}`}; reconnect scheduled`,
      { code: 'socket-closed' }
    )
    this.options.requests.rejectAll('Connection interrupted', { deliveryUnknown: true })
    this.options.connectionState.publish('reconnecting')
    this.options.reconnect.schedule()
  }
}