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.1 KB
Raw Blame
import {
  CREATURE_POOL_NAMES,
  creatureNameAtTier,
  creatureNameTier
} from '../worktree/retired-name-registry'

export const CLIENT_WORKTREE_CREATE_MAX_ATTEMPTS = 25

// Older runtimes return conflicts instead of suffix-retrying internally; share the client fallback policy.
const RETRYABLE_WORKTREE_CREATE_CONFLICT_PATTERNS = [
  /already exists locally/i,
  /already exists on a remote/i,
  /^Branch ".+" already exists\./i,
  /already has pr #\d+/i
]

export function getClientWorktreeCreateCandidate(value: string, attempt: number): string {
  return attempt === 0 ? value : `${value}-${attempt + 1}`
}

function generatedNameIdentity(value: string): { poolName: string; tier: number } | null {
  const normalized = value.trim().toLowerCase()
  const canonicalTier = creatureNameTier(normalized)
  if (canonicalTier !== null) {
    const poolName =
      canonicalTier === 1 ? normalized : normalized.slice(0, normalized.lastIndexOf('-'))
    return { poolName, tier: canonicalTier }
  }
  let poolName = normalized
  let tier = 1
  let suffixCount = 0
  for (;;) {
    const match = /-(\d{1,6})$/.exec(poolName)
    if (!match) {
      break
    }
    const suffix = Number(match[1])
    if (suffix < 2 || tier + suffix - 1 > 999_999) {
      return null
    }
    tier += suffix - 1
    suffixCount += 1
    poolName = poolName.slice(0, match.index)
  }
  return suffixCount > 1 && CREATURE_POOL_NAMES.has(poolName) ? { poolName, tier } : null
}

export function isGeneratedWorktreeCreateName(value: string): boolean {
  return generatedNameIdentity(value) !== null
}

export function getGeneratedWorktreeCreateRetryCandidate(
  value: string,
  attempt: number,
  minimumTier = 1
): string {
  const identity = generatedNameIdentity(value)
  if (!identity) {
    return getClientWorktreeCreateCandidate(value, attempt)
  }
  return creatureNameAtTier(identity.poolName, Math.max(identity.tier, minimumTier) + attempt)
}

export function isRetryableWorktreeCreateConflict(message: string): boolean {
  return RETRYABLE_WORKTREE_CREATE_CONFLICT_PATTERNS.some((pattern) => pattern.test(message))
}