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.1 KB
Raw Blame
import {
  LOCAL_EXECUTION_HOST_ID,
  getRepoExecutionHostId,
  normalizeExecutionHostId,
  type ExecutionHostId
} from '../../../src/shared/execution-host'
import {
  getHostContextLabel,
  getMixedHostContextLabels as getSharedMixedHostContextLabels
} from '../../../src/shared/worktree/host-context-labels'
import { composeWorktreeHostIdentity } from '../../../src/shared/worktree/host-qualified-identity'
export {
  buildHostLabelById,
  getHostContextLabel
} from '../../../src/shared/worktree/host-context-labels'
import type { ExecutionHostHealth } from '../../../src/shared/execution-host-health'
import type { Worktree } from './workspace-list-types'
import { getHostHealthBadgeLabel } from './worktree-host-health'

export type HostLabelSources = {
  /** Host id per repo id from repo.list; rows from hosts that predate `hostId` fall back to it. */
  repoHostIdByRepoId: ReadonlyMap<string, ExecutionHostId>
  /** User-facing labels for non-local hosts: SSH target labels, then per-host display overrides. */
  hostLabelById: ReadonlyMap<ExecutionHostId, string>
  /** The paired host's own platform; the phone's platform must never name the desktop. */
  hostPlatform: NodeJS.Platform | null
  /** Health per host the desktop reports one for; an absent host keeps the bare label. */
  hostHealthById: ReadonlyMap<ExecutionHostId, ExecutionHostHealth>
  /** A host's own badge word, over its health's; older desktops send none. */
  hostHealthLabelById?: ReadonlyMap<ExecutionHostId, string>
}

export function buildRepoHostIdByRepoId(
  repos: readonly { id: string; connectionId?: string | null; executionHostId?: string | null }[]
): Map<string, ExecutionHostId> {
  return new Map(repos.map((repo) => [repo.id, getRepoExecutionHostId(repo)]))
}

export function resolveWorktreeHostId(
  worktree: Pick<Worktree, 'hostId' | 'repoId'>,
  repoHostIdByRepoId: ReadonlyMap<string, ExecutionHostId>
): ExecutionHostId {
  return (
    normalizeExecutionHostId(worktree.hostId) ??
    repoHostIdByRepoId.get(worktree.repoId) ??
    LOCAL_EXECUTION_HOST_ID
  )
}

function getResolvedWorktreeRowIdentity(
  worktree: Pick<Worktree, 'worktreeId' | 'hostId' | 'repoId'>,
  repoHostIdByRepoId: ReadonlyMap<string, ExecutionHostId>
): string {
  return composeWorktreeHostIdentity(
    resolveWorktreeHostId(worktree, repoHostIdByRepoId),
    worktree.worktreeId
  )
}

// Match desktop mixed-host badges; single-host lists need no repeated label.
export function getWorktreeHostContextLabels(
  worktrees: readonly Worktree[],
  sources: HostLabelSources
): Map<string, string> | undefined {
  return getSharedMixedHostContextLabels(worktrees, {
    getHostId: (worktree) => resolveWorktreeHostId(worktree, sources.repoHostIdByRepoId),
    // Resolve repo ownership for legacy rows so same-id worktrees on different hosts cannot overwrite labels.
    getIdentity: (worktree) => getResolvedWorktreeRowIdentity(worktree, sources.repoHostIdByRepoId),
    sources
  })
}

export function applyWorktreeHostContextLabels(
  worktrees: Worktree[],
  sources: HostLabelSources
): Worktree[] {
  const labels = getWorktreeHostContextLabels(worktrees, sources)
  // Why: the desktop card shows SSH status on any host count, so an unhealthy host is named even alone.
  const anyUnhealthy =
    [...sources.hostHealthById.values()].some(getHostHealthBadgeLabel) ||
    (sources.hostHealthLabelById?.size ?? 0) > 0
  if (!labels && !anyUnhealthy) {
    return worktrees
  }
  return worktrees.map((worktree) => {
    const hostContextHostId = resolveWorktreeHostId(worktree, sources.repoHostIdByRepoId)
    const hostContextHealthLabel =
      sources.hostHealthLabelById?.get(hostContextHostId) ??
      getHostHealthBadgeLabel(sources.hostHealthById.get(hostContextHostId))
    const hostContextLabel = labels
      ? labels.get(getResolvedWorktreeRowIdentity(worktree, sources.repoHostIdByRepoId))
      : hostContextHealthLabel && getHostContextLabel(hostContextHostId, sources)
    if (!hostContextLabel) {
      return worktree
    }
    return {
      ...worktree,
      hostContextLabel,
      hostContextHostId,
      ...(hostContextHealthLabel ? { hostContextHealthLabel } : {})
    }
  })
}