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.

1.8 KB
Raw Blame
import { recognizeAgentLaunchCommand } from './agent-command-line-intent'
import { gitCredentialPromptGuardEnv } from './git-credential-prompt-env'

const GIT_CONFIG_PROTOCOL_KEY_RE = /^GIT_CONFIG_(?:COUNT|KEY_\d+|VALUE_\d+)$/

export const TERMINAL_GIT_CREDENTIAL_GUARD_POLICY_ENV =
  'ORCA_INTERNAL_TERMINAL_GIT_CREDENTIAL_GUARD_POLICY'

/** Disable credential UI only for recognized agents and marked automation. */
export function applyTerminalGitCredentialPromptGuard(
  env: Record<string, string>,
  opts: {
    launchCommand?: string | null
    isUnattended?: boolean
    platform?: NodeJS.Platform
    /** A detached host appends indexed config after its authoritative env merge. */
    deferGitConfigGuardToHost?: boolean
  }
): boolean {
  const explicitlyGuarded = env[TERMINAL_GIT_CREDENTIAL_GUARD_POLICY_ENV] === 'guard'
  delete env[TERMINAL_GIT_CREDENTIAL_GUARD_POLICY_ENV]

  const shouldGuard =
    opts.isUnattended === true ||
    Boolean(
      recognizeAgentLaunchCommand(opts.launchCommand, {
        includeHeadlessOneShot: true
      })
    )
  if (!explicitlyGuarded && !shouldGuard) {
    return false
  }

  const guarded = gitCredentialPromptGuardEnv(env, opts.platform ?? process.platform)
  if (!opts.deferGitConfigGuardToHost) {
    Object.assign(env, guarded)
    return true
  }

  // The daemon must append indexed config after merging inherited env; send only the policy and scalar guards.
  env[TERMINAL_GIT_CREDENTIAL_GUARD_POLICY_ENV] = 'guard'
  for (const [key, value] of Object.entries(guarded)) {
    if (typeof value !== 'string' || GIT_CONFIG_PROTOCOL_KEY_RE.test(key)) {
      continue
    }
    if (key === 'WSLENV') {
      continue
    }
    if ((key === 'GIT_ASKPASS' || key === 'SSH_ASKPASS') && !Object.hasOwn(env, key)) {
      continue
    }
    env[key] = value
  }
  return true
}