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.

4.7 KB
Raw Blame
import {
  IdleRegionalRehomeResponseSchema,
  isGlobalIdleRegionalRehomeDeferral
} from '@orca-cloud/relay-contract'
import type { RelayAssignmentStore } from './assignment-store.js'
import type { RelayConfig } from './config.js'
import { googleMetadataIdentityToken } from './google-metadata-identity-token.js'
import type { RegionalRehomeSafetySnapshot } from './relay-observability.js'
import { jitteredSweepIntervalMs } from './relay-sweep-schedule.js'

type RegionalRehomeWorkerOptions = {
  fetch?: typeof fetch
  identityToken?: (audience: string) => Promise<string>
  now?: () => number
  intervalMs?: number
  requestTimeoutMs?: number
  random?: () => number
  safetySnapshot?: () => RegionalRehomeSafetySnapshot
}

export type RegionalRehomeWorker = {
  run: () => Promise<void>
  stop: () => void
}

export function startRegionalRehomeWorker(
  config: RelayConfig,
  assignments: RelayAssignmentStore,
  options: RegionalRehomeWorkerOptions = {}
): RegionalRehomeWorker | null {
  if (
    config.role !== 'director' ||
    !config.rehomeAudience ||
    !config.rehomeDirectorServiceAccount ||
    !options.safetySnapshot
  ) {
    return null
  }
  const audience = config.rehomeAudience
  const safetySnapshot = options.safetySnapshot
  const fetchImpl = options.fetch ?? fetch
  const tokenProvider =
    options.identityToken ??
    ((audience: string) => googleMetadataIdentityToken(audience, fetchImpl))
  let stopped = false
  let inFlight = false
  const run = async (): Promise<void> => {
    if (stopped || inFlight) return
    inFlight = true
    try {
      const candidates = await assignments.selectIdleRegionalRehomeCandidates(safetySnapshot())
      if (candidates.length === 0) return
      const token = await tokenProvider(audience)
      const outcomes: Record<string, number> = {}
      const tally = (key: string) => {
        outcomes[key] = (outcomes[key] ?? 0) + 1
      }
      let stoppedBy: string | null = null
      for (const candidate of candidates) {
        if (stopped) break
        const { sourceCellUrl, ...request } = candidate
        try {
          const response = await fetchImpl(new URL('/v1/admin/host-idle-rehome', sourceCellUrl), {
            method: 'POST',
            headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
            body: JSON.stringify({
              ...request,
              cohortPercent: config.regionCorrectionCohortPercent ?? 0,
              directorSafety: safetySnapshot()
            }),
            signal: AbortSignal.timeout(options.requestTimeoutMs ?? 10_000)
          })
          if (!response.ok) throw new Error(`regional_rehome_source_${response.status}`)
          const body = IdleRegionalRehomeResponseSchema.parse(await response.json())
          tally(body.reason ? `${body.outcome}:${body.reason}` : body.outcome)
          if (body.outcome === 'committed') {
            console.warn(
              JSON.stringify({
                event: 'orca_relay_idle_rehome_committed',
                sourceCellId: candidate.sourceCellId,
                targetCellId: candidate.targetCellId
              })
            )
            stoppedBy = 'committed'
            break
          }
          // Global deferrals repeat the same durable verdict for every candidate; older sources omit reason and retain the full walk.
          if (body.outcome === 'deferred' && isGlobalIdleRegionalRehomeDeferral(body.reason)) {
            stoppedBy = body.reason
            break
          }
        } catch (error) {
          tally('failed')
          // The source may have committed; its durable outcome owns recovery.
          console.warn(
            JSON.stringify({
              event: 'orca_relay_idle_rehome_request_failed',
              reason: error instanceof Error ? error.message : 'unknown'
            })
          )
        }
      }
      // Summarize dispatched polls so all-deferred pages remain visible.
      console.warn(
        JSON.stringify({
          event: 'orca_relay_idle_rehome_dispatch_summary',
          candidates: candidates.length,
          dispatched: Object.values(outcomes).reduce((total, count) => total + count, 0),
          stoppedBy,
          outcomes
        })
      )
    } catch (error) {
      console.warn(
        JSON.stringify({
          event: 'orca_relay_regional_rehome_poll_failed',
          reason: error instanceof Error ? error.message : 'unknown'
        })
      )
    } finally {
      inFlight = false
    }
  }
  const timer = setInterval(
    () => void run(),
    // Match the initial ten-moves/minute budget without replanning the join every second.
    options.intervalMs ?? jitteredSweepIntervalMs(6_000, options.random)
  )
  timer.unref()
  void run()
  return {
    run,
    stop: () => {
      stopped = true
      clearInterval(timer)
    }
  }
}