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.7 KB
Raw Blame
import type { RuntimeMetadata } from '../../shared/runtime-bootstrap'
import { isSameUserDataPath } from '../../shared/serve-user-data-path'
import { readRuntimeSourceStamp, type RuntimeSourceStamp } from '../../shared/runtime-source-env'
import type { ExpectedRuntimeSource } from '../../shared/runtime-rpc-envelope'
import type { RuntimeStatus } from '../../shared/runtime-types'
import { getPlatformUserDataPath } from './metadata'
import { sendRequest } from './transport'
import { RuntimeClientError, type RuntimeRpcResponse } from './types'

type StatusProbe = (metadata: RuntimeMetadata) => Promise<RuntimeRpcResponse<RuntimeStatus>>

/** A different nondefault profile opts out of the inherited runtime fence. */
export function resolveFencedRuntimeSource(
  stamp: RuntimeSourceStamp | null,
  userDataPath: string,
  platformDefaultPath: string | null
): RuntimeSourceStamp | null {
  if (!stamp?.profilePath || isSameUserDataPath(userDataPath, stamp.profilePath)) {
    return stamp
  }
  // Fence unpinned terminals falling back to the platform default.
  return platformDefaultPath !== null && isSameUserDataPath(userDataPath, platformDefaultPath)
    ? stamp
    : null
}

/** Fence requests to their launching source, allowing new incarnations after restarts and updates. */
export class RuntimeSourceFence {
  private readonly verifiedRuntimeIds = new Set<string>()

  constructor(
    private readonly expected: RuntimeSourceStamp | null,
    private readonly probe: StatusProbe
  ) {
    if (expected) {
      this.verifiedRuntimeIds.add(expected.incarnation)
    }
  }

  async check(
    metadata: RuntimeMetadata,
    method: string
  ): Promise<ExpectedRuntimeSource | undefined> {
    // status.get is the read-only probe itself.
    if (!this.expected || method === 'status.get') {
      return undefined
    }
    const expectation = { sourceId: this.expected.sourceId, runtimeId: metadata.runtimeId }
    if (this.verifiedRuntimeIds.has(metadata.runtimeId)) {
      return expectation
    }
    // The transport verifies that this response came from metadata’s runtime.
    const response = await this.probe(metadata)
    if (response.ok === false) {
      throw new RuntimeClientError(
        'runtime_source_unverifiable',
        `Could not confirm that the running Orca is the one that launched this terminal: ${response.error.message}`
      )
    }
    const sourceId = response.result.hostDescriptor?.installationId
    if (!sourceId) {
      throw new RuntimeClientError(
        'runtime_source_unverifiable',
        'The running Orca does not report which profile it serves, so this terminal cannot confirm it is the Orca that launched it. Nothing was sent. Open a new terminal from the Orca you want to control.'
      )
    }
    if (sourceId !== this.expected.sourceId) {
      throw new RuntimeClientError(
        'runtime_source_mismatch',
        'The running Orca is not the one that launched this terminal (another profile or instance took over its runtime record). Nothing was sent. Open a new terminal from the Orca you want to control.'
      )
    }
    this.verifiedRuntimeIds.add(metadata.runtimeId)
    return expectation
  }
}

export function createRuntimeSourceFence(
  env: Readonly<Record<string, string | undefined>>,
  userDataPath: string,
  probeTimeoutMs: number
): RuntimeSourceFence {
  let platformDefaultPath: string | null = null
  try {
    platformDefaultPath = getPlatformUserDataPath()
  } catch {
    // Without a platform default, no terminal can fall back to it.
  }
  return new RuntimeSourceFence(
    resolveFencedRuntimeSource(readRuntimeSourceStamp(env), userDataPath, platformDefaultPath),
    (metadata) => sendRequest<RuntimeStatus>(metadata, 'status.get', undefined, probeTimeoutMs)
  )
}