Skip to content

The agent that grows with you

3.4 KB
Raw Blame
import { readFileSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'

import { app } from 'electron'

import { resolveDesktopHermesHome } from './data-paths'
import { releasePackagedWorkingDirectory } from './packaged-working-directory'
import { readDesktopLaunchConfig } from './renderer-heap-flags'
import { restoreUserProxyEnv } from './updater/system-proxy-env'
import { wslgLaunchArgs } from './wslg-launch'
import { spawnWslgLaunch } from './wslg-launch-process'

// A Desktop relaunched by an update (any rung: hand-off script, staged updater,
// Start-Process) must not keep the system proxy the update's children were given.
restoreUserProxyEnv(process.env)

// `hermes desktop` asks this binary whether Chromium's namespace sandbox starts under the host's
// AppArmor policy (#130177). Chromium creates its sandboxed zygote before any JS runs, so reaching
// this line answers yes; exit before any window, single-instance lock, backend or relaunch.
if (process.argv.includes('--hermes-sandbox-probe')) {
  process.exit(0)
}

// Run before main creates windows/IME integrations or spawns resident helpers.
// Closing Hermes cannot release a CWD inherited by an out-of-tree process.
releasePackagedWorkingDirectory(app.isPackaged || Boolean(process.env.HERMES_DESKTOP_IS_PACKAGED))

function configuredElectronFlags(env: NodeJS.ProcessEnv): string[] {
  // Resolve the home exactly like main.ts does, through the shared resolver:
  // HERMES_DATA_DIR_SUFFIX channel installs and profiles/-rooted HERMES_HOME
  // values must pick the same config.yaml before the relaunch and inside the
  // app, or desktop.electron_flags silently never reaches the relaunch.
  const home = resolveDesktopHermesHome({
    home: os.homedir(),
    env,
    // Linux-only pre-launch path; the win32 legacy-migration probe is never
    // consulted on posix, so its directoryExists callback is not needed here.
    directoryExists: () => false,
    readWindowsHome: () => null
  })

  try {
    return readDesktopLaunchConfig(readFileSync(path.join(home, 'config.yaml'), 'utf8')).electronFlags
  } catch {
    return []
  }
}

const linux = process.platform === 'linux'
const electronFlags = linux ? configuredElectronFlags(process.env) : []

// True only when the NVIDIA proprietary kernel module is loaded
// (not nouveau, not the open kernel module, not WSL's dxg passthrough).
function hasNvidiaProprietaryDriver() {
  if (process.platform !== 'linux') {
    return false
  }

  try {
    const version = readFileSync('/proc/driver/nvidia/version', 'utf8') || ''

    return !/Open Kernel Module/.test(version)
  } catch {
    return false // no nvidia driver on this box
  }
}

const args = wslgLaunchArgs(
  process.argv.slice(1),
  process.env,
  process.platform,
  electronFlags,
  hasNvidiaProprietaryDriver()
)

if (args) {
  // Keep the launcher alive until the child exits: npm's concurrently must not
  // tear down Vite during this handoff. No backend, windows or single-instance
  // lock are created in this parent. The child has an explicit platform flag,
  // so it goes straight into main on its first pass.
  const child = spawnWslgLaunch(args)

  child.once('error', error => {
    console.error('[hermes] Wayland ozone launch failed:', error)
    app.exit(1)
  })
  child.once('exit', code => app.exit(code ?? 1))

  for (const signal of ['SIGINT', 'SIGTERM'] as const) {
    process.once(signal, () => child.kill(signal))
  }
} else {
  await import('./main')
}