Skip to content

The agent that grows with you

5.3 KB
Raw Blame
import { execFile } from 'node:child_process'
import { existsSync } from 'node:fs'
import path from 'node:path'
import { promisify } from 'node:util'

import { buildDesktopBackendEnv } from '../backend-env'
import { resolveInstallationLauncher } from '../updater-process'
import { hiddenWindowsChildOptions } from '../windows-child-options'

import { updateProxyEnv } from './system-proxy-env'

import type { UpdaterStatusWire } from './index'

export interface SourceUpdate extends UpdaterStatusWire {}

export interface SourceUpdateProbe {
  python: string | null
  git: string
  updateRoot: string
  hermesHome: string
  branch?: string
  channel?: 'main' | 'stable' | 'canary'
  /** Persist this install's channel first (the About selector); the check then reports it. */
  setChannel?: 'main' | 'stable'
  force?: boolean
  cachePath?: string
  branchConfigPath?: string
}

const execute: typeof execFile.__promisify__ = promisify(execFile)

/** The two channels a source checkout can select from Settings. */
export function sourceChannelName(name: unknown): 'main' | 'stable' {
  if (name !== 'stable' && name !== 'main') {
    throw new Error('The update channel must be stable or main.')
  }

  return name
}

/** The probe's optional arguments, in the order source_check parses them. */
function probeFlags(probe: SourceUpdateProbe): string[] {
  const flags: [string, string | undefined][] = [
    ['--branch', probe.branch],
    ['--channel', probe.channel],
    ['--set-channel', probe.setChannel],
    ['--cache-path', probe.cachePath],
    ['--branch-config-path', probe.branchConfigPath]
  ]

  return [
    ...flags.flatMap(([flag, value]): string[] => (value ? [flag, value] : [])),
    ...(probe.force ? ['--force'] : [])
  ]
}

/**
 * Every update child's env: this process's env plus the system proxy when none
 * is set (#123387). The proxy env overrides process.env, so its extended
 * NO_PROXY is the one the children read (buildDesktopBackendEnv, spread after
 * it, sets only PYTHON* and PATH); it is empty whenever the user set a proxy.
 */
export async function sourceUpdateEnvironment(
  updateRoot: string,
  hermesHome: string,
  note?: (message: string) => void
): Promise<NodeJS.ProcessEnv> {
  const env: NodeJS.ProcessEnv = {
    ...process.env,
    ...(await updateProxyEnv(process.env, note, updateRoot)),
    ...buildDesktopBackendEnv(),
    HERMES_HOME: hermesHome,
    HERMES_INSTALL_ROOT: updateRoot
  }

  delete env.HERMES_RUNTIME_DIR

  return env
}

/** Python owns config identity and publication checks; this bridge only transports them. */
export async function readSourceUpdate(probe: SourceUpdateProbe): Promise<SourceUpdate | null> {
  // The install launcher boots PM's committed Python and dependency generation.
  // A PATH Python can import this checkout yet lack its selected dependencies.
  const managed = existsSync(path.join(probe.updateRoot, 'pm'))

  const launcher = managed
    ? resolveInstallationLauncher(probe.updateRoot, process.platform === 'win32', probe.hermesHome)
    : null

  if (managed && !launcher) {
    throw new Error('The source installation launcher is missing; repair this installation before checking updates.')
  }

  if (!managed && !probe.python) {
    throw new Error('No Python interpreter is available to check the source update channel.')
  }

  const args: string[] = [
    ...(managed
      ? ['--run-module', 'hermes_cli.source_check']
      : [
          '-c',
          // Inspect the target checkout's callable, not stderr strings or an editable
          // install elsewhere on sys.path. Exceptions inside a present probe propagate.
          'from pathlib import Path; import runpy; p = Path("hermes_cli/source_check.py"); entry = runpy.run_path(str(p)).get("main") if p.is_file() else None; entry() if callable(entry) else print("null")'
        ]),
    '--install-root',
    probe.updateRoot,
    '--home',
    probe.hermesHome,
    '--git',
    probe.git,
    ...probeFlags(probe)
  ]

  const command: string = (managed ? launcher : probe.python)!
  const viaCmd: boolean = process.platform === 'win32' && /\.cmd$/i.test(command)

  // Node refuses direct .cmd execFile; shell:true interpolates untrusted branch
  // and path arguments. Keep cmd.exe's one unavoidable parse fail-closed.
  if (viaCmd && [command, ...args].some((value: string): boolean => /["%&|<>^\r\n]/.test(value))) {
    throw new Error('The source check contains an unsafe Windows command argument.')
  }

  const result: { stdout: string; stderr: string } = await execute(
    viaCmd ? (process.env.ComSpec ?? 'cmd.exe') : command,
    viaCmd
      ? ['/d', '/v:off', '/s', '/c', `""${command}" ${args.map((arg: string): string => `"${arg}"`).join(' ')}"`]
      : args,
    hiddenWindowsChildOptions({
      cwd: probe.updateRoot,
      env: await sourceUpdateEnvironment(probe.updateRoot, probe.hermesHome),
      encoding: 'utf8',
      timeout: 360000,
      maxBuffer: 1024 * 1024,
      windowsVerbatimArguments: viaCmd
    })
  )

  const selection: SourceUpdate | null = JSON.parse(result.stdout) as SourceUpdate | null

  if (selection === null) {
    return null
  }

  if (typeof selection.supported !== 'boolean') {
    throw new Error('The source update check returned an invalid status.')
  }

  // The Python banner uses -1 for an available update with no exact count.
  if (selection.behind === -1) {
    selection.behind = null
  }

  return selection
}