Skip to content

The agent that grows with you

Use prepared Python for bootstrap and desktop build helpers

ethernetc184f04838b6
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index 661813d33f0..9d707c7dafe 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -34,7 +34,7 @@
"builder": "cross-env NODE_OPTIONS=--max-old-space-size=16384 node scripts/run-electron-builder.mjs",
"pack": "npm run build && npm run builder -- --dir --publish never",
"dist": "npm run build && npm run builder",
- "payload": "uv run --no-project python ../../scripts/bundles/stage.py --out build/agent-payload",
+ "payload": "node ../../scripts/build/python.mjs ../../scripts/bundles/stage.py --out build/agent-payload",
"dist:bundled": "npm run payload && cross-env HERMES_DESKTOP_VARIANT=bundled npm run dist",
"dist:mac": "npm run build && npm run builder -- --mac",
"dist:mac:dmg": "npm run build && npm run builder -- --mac dmg",
diff --git a/apps/desktop/scripts/after-pack-digests.test.mjs b/apps/desktop/scripts/after-pack-digests.test.mjs
index f161a662ce6..fbbbbfad2cb 100644
--- a/apps/desktop/scripts/after-pack-digests.test.mjs
+++ b/apps/desktop/scripts/after-pack-digests.test.mjs
@@ -9,7 +9,7 @@ import { test } from 'vitest'
const repo = path.resolve(import.meta.dirname, '../../..')
const hook = pathToFileURL(path.join(import.meta.dirname, 'after-pack.mjs')).href
-const python = process.env.HERMES_PYTHON || process.env.UV_PYTHON || 'python'
+const python = process.env.HERMES_PYTHON || 'python'
function fixture() {
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'after-pack-digests-'))
@@ -29,7 +29,7 @@ function fixture() {
fs.writeFileSync(binary, pe)
const env = { ...process.env, HERMES_HOME: path.join(directory, 'home'),
HERMES_RUNTIME_DIR: path.join(directory, 'runtime'),
- UV_PYTHON: python, UV_PYTHON_DOWNLOADS: 'never', UV_OFFLINE: '1',
+ HERMES_PYTHON: python, UV_PYTHON_DOWNLOADS: 'never', UV_OFFLINE: '1',
UV_CACHE_DIR: path.join(directory, 'uv-cache') }
for (const key of Object.keys(env)) {
if (key.startsWith('AZURE_SIGN_')) delete env[key]
diff --git a/apps/desktop/scripts/after-pack-toolchain.test.mjs b/apps/desktop/scripts/after-pack-toolchain.test.mjs
index ecf70c081a1..43060fdc9f9 100644
--- a/apps/desktop/scripts/after-pack-toolchain.test.mjs
+++ b/apps/desktop/scripts/after-pack-toolchain.test.mjs
@@ -1,5 +1,5 @@
import { execFileSync } from 'node:child_process'
-import { mkdirSync, mkdtempSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { expect, test } from 'vitest'
@@ -13,7 +13,7 @@ test.runIf(process.platform === 'linux')('afterPack uses the provisioned Python
const store = path.join(payload, 'tools', 'python', 'bin')
const venv = path.join(payload, 'venv', 'bin')
const python = execFileSync('python3', ['-c', 'import sys; print(sys.executable)'], { encoding: 'utf8' }).trim()
- const previous = { UV_PYTHON: process.env.UV_PYTHON, UV_PYTHON_DOWNLOADS: process.env.UV_PYTHON_DOWNLOADS, PYTHONPATH: process.env.PYTHONPATH }
+ const previous = { HERMES_PYTHON: process.env.HERMES_PYTHON, PATH: process.env.PATH, UV_PYTHON: process.env.UV_PYTHON, PYTHONPATH: process.env.PYTHONPATH }
const observed = path.join(directory, 'interpreter.json')
const identity = 'import json,os,sys; print(json.dumps(os.path.realpath(sys.executable)))'
const expected = JSON.parse(execFileSync(python, ['-c', identity], { encoding: 'utf8' }))
@@ -21,8 +21,11 @@ test.runIf(process.platform === 'linux')('afterPack uses the provisioned Python
// Record the interpreter that actually executes the relocation script.
writeFileSync(path.join(directory, 'sitecustomize.py'), `import json,os,sys\nfrom pathlib import Path\nPath(${JSON.stringify(observed)}).write_text(json.dumps(os.path.realpath(sys.executable)), encoding="utf-8")\n`)
process.env.PYTHONPATH = directory
- process.env.UV_PYTHON = python
- process.env.UV_PYTHON_DOWNLOADS = 'never'
+ process.env.HERMES_PYTHON = python
+ process.env.UV_PYTHON = path.join(directory, 'not-the-build-python')
+ writeFileSync(path.join(directory, 'uv'), '#!/bin/sh\nexit 91\n')
+ chmodSync(path.join(directory, 'uv'), 0o755)
+ process.env.PATH = `${directory}${path.delimiter}${process.env.PATH}`
mkdirSync(store, { recursive: true })
mkdirSync(venv, { recursive: true })
writeFileSync(path.join(payload, 'manifest.json'), '{}')
diff --git a/apps/desktop/scripts/after-pack.mjs b/apps/desktop/scripts/after-pack.mjs
index 86f3ab667ff..d07c4aef920 100644
--- a/apps/desktop/scripts/after-pack.mjs
+++ b/apps/desktop/scripts/after-pack.mjs
@@ -21,7 +21,7 @@
import path from 'node:path'
import fs from 'node:fs'
-import { execFileSync } from 'node:child_process'
+import { runPython } from '../../../scripts/build/python.mjs'
import { batchSignAppTree } from './batch-sign-binaries.mjs'
import { rehashPayloadDigests } from './payload-digests.mjs'
@@ -36,7 +36,7 @@ export default async function afterPack(context) {
: path.join(context.appOutDir, 'resources')
const payload = path.join(resources, 'agent-payload')
if (platform !== 'win32' && fs.existsSync(path.join(payload, 'manifest.json'))) {
- execFileSync('uv', ['run', '--no-project', 'python',
+ runPython([
path.resolve(import.meta.dirname, '../../../scripts/bundles/payload.py'), 'relocate', payload], { stdio: 'inherit' })
}
if (platform === 'darwin') {
diff --git a/apps/desktop/scripts/mac-sign.test.mjs b/apps/desktop/scripts/mac-sign.test.mjs
index 7213f260f6a..75f16db19d8 100644
--- a/apps/desktop/scripts/mac-sign.test.mjs
+++ b/apps/desktop/scripts/mac-sign.test.mjs
@@ -13,7 +13,7 @@ const require = createRequire(import.meta.url)
const config = require('../electron-builder.config.cjs')
const repo = path.resolve(import.meta.dirname, '../../..')
const desktop = path.resolve(import.meta.dirname, '..')
-const python = process.env.HERMES_PYTHON || process.env.UV_PYTHON || 'python'
+const python = process.env.HERMES_PYTHON || 'python'
function fixture() {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'mac-digest-order-'))
@@ -34,7 +34,7 @@ function fixture() {
const ignored = path.join(tools, 'python', 'non-macho.bin')
fs.writeFileSync(ignored, Buffer.alloc(64))
const env = { ...process.env, HERMES_HOME: path.join(root, 'home'),
- HERMES_RUNTIME_DIR: path.join(root, 'state'), UV_PYTHON: python,
+ HERMES_RUNTIME_DIR: path.join(root, 'state'), HERMES_PYTHON: python,
UV_OFFLINE: '1', UV_PYTHON_DOWNLOADS: 'never', UV_CACHE_DIR: path.join(root, 'cache') }
delete env.PYTHONHOME
delete env.PYTHONPATH
@@ -69,7 +69,7 @@ async function signThroughBuilder(f) {
}
function armEnvironment(f) {
- for (const name of ['HERMES_HOME', 'HERMES_RUNTIME_DIR', 'UV_PYTHON', 'UV_OFFLINE', 'UV_PYTHON_DOWNLOADS', 'UV_CACHE_DIR']) {
+ for (const name of ['HERMES_HOME', 'HERMES_RUNTIME_DIR', 'HERMES_PYTHON', 'UV_OFFLINE', 'UV_PYTHON_DOWNLOADS', 'UV_CACHE_DIR']) {
vi.stubEnv(name, f.env[name])
}
vi.stubEnv('PYTHONHOME', undefined)
diff --git a/apps/desktop/scripts/payload-digests.mjs b/apps/desktop/scripts/payload-digests.mjs
index 6f90cb1e847..f2f52380804 100644
--- a/apps/desktop/scripts/payload-digests.mjs
+++ b/apps/desktop/scripts/payload-digests.mjs
@@ -1,4 +1,4 @@
-import { execFileSync } from 'node:child_process'
+import { runPython } from '../../../scripts/build/python.mjs'
import fs from 'node:fs'
import path from 'node:path'
@@ -6,7 +6,7 @@ import path from 'node:path'
export function rehashPayloadDigests(payload) {
// Light builds have no payload. Present payloads require valid tool facts.
if (!fs.existsSync(path.join(payload, 'manifest.json'))) return
- execFileSync('uv', ['run', '--no-project', 'python',
+ runPython([
path.resolve(import.meta.dirname, '../../../scripts/bundles/payload.py'), 'rehash', payload],
{ stdio: 'inherit' })
}
diff --git a/scripts/build/python.mjs b/scripts/build/python.mjs
new file mode 100644
index 00000000000..9827884cd74
--- /dev/null
+++ b/scripts/build/python.mjs
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+/** Run build helpers with the prepared Python, never an ambient uv environment. */
+import { execFileSync } from 'node:child_process'
+import { pathToFileURL } from 'node:url'
+
+/**
+ * @param {string[]} args
+ * @param {import('node:child_process').ExecFileSyncOptions} [options]
+ */
+export function runPython(args, options = {}) {
+ const env = options.env ?? process.env
+ return execFileSync(env.HERMES_PYTHON || 'python', args, { stdio: 'inherit', ...options, env })
+}
+
+if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
+ try {
+ runPython(process.argv.slice(2))
+ } catch (error) {
+ console.error('[python build]', error.message)
+ process.exitCode = Number.isInteger(error.status) && error.status !== 0 ? error.status : 1
+ }
+}
diff --git a/scripts/install.ps1 b/scripts/install.ps1
index 8335515181c..099fb735f65 100644
--- a/scripts/install.ps1
+++ b/scripts/install.ps1
@@ -516,12 +516,9 @@ function Stage-Repository {
}
function Stage-Venv {
- $uv = Get-Uv
- Log "creating venv"
- Push-Location $InstallDir
- & $uv venv --allow-existing venv; $code = $LASTEXITCODE
- Pop-Location
- if ($code) { Fail "uv venv failed" }
+ # Keep the installer stage protocol; PM alone creates dependency environments.
+ Get-BootstrapPython | Out-Null
+ Log "bootstrap Python ready; PM prepares the dependency environment"
}
# Delegate the whole python+venv+tools install to pm: stage the pinned uv,
diff --git a/scripts/install.sh b/scripts/install.sh
index c78bead0ff7..f9ebde5df5a 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -319,9 +319,10 @@ stage_repository() {
}
stage_venv() {
- ensure_uv
- log "creating venv"
- (cd "$INSTALL_DIR" && "$UV_CMD" venv --allow-existing venv) || fail "uv venv failed"
+ # Keep the installer stage protocol; PM alone creates dependency environments.
+ local boot_py
+ bootstrap_python
+ log "bootstrap Python ready; PM prepares the dependency environment"
}
# Tool-only bootstrap: acquire uv and Python before PM's own dependencies exist.
diff --git a/tests/test_install_ps1_managed_python_provenance.py b/tests/test_install_ps1_managed_python_provenance.py
index 5f63d0b0a12..c61d912a4c1 100644
--- a/tests/test_install_ps1_managed_python_provenance.py
+++ b/tests/test_install_ps1_managed_python_provenance.py
@@ -12,8 +12,8 @@ pytestmark = pytest.mark.platforms("windows")
INSTALLER = Path(__file__).resolve().parents[1] / "scripts" / "install.ps1"
[email protected]("exit_code", [0, 17])
-def test_python_stage_delegates_to_pin_without_touching_existing_env(tmp_path, exit_code):
[email protected]("stage,exit_code", [("venv", 0), ("python-deps", 0), ("python-deps", 17)])
+def test_python_stage_delegates_to_pin_without_touching_existing_env(tmp_path, stage, exit_code):
powershell = shutil.which("powershell")
assert powershell
install = tmp_path / "install with spaces"
@@ -52,11 +52,11 @@ function Invoke-WebRequest { throw 'network access outside test boundary' }
. $Installer -InstallDir $InstallDir -HermesHome $HomeDir
# Dot-sourcing defines Get-Uv, so override only that acquisition boundary.
function Get-Uv { return 'uv' }
-Invoke-StageByName 'python-deps'
+Invoke-StageByName $env:PROBE_STAGE
exit $LASTEXITCODE
''', encoding="utf-8-sig")
# uv python find returns the distribution interpreter, not a venv launcher.
- env = dict(os.environ, PROBE_EXIT=str(exit_code), BOOTSTRAP_PYTHON=sys._base_executable,
+ env = dict(os.environ, PROBE_STAGE=stage, PROBE_EXIT=str(exit_code), BOOTSTRAP_PYTHON=sys._base_executable,
PM_LOG=str(pm_log), UV_BUSY=str(tmp_path / "uv-busy"), PATHEXT=".COM;.EXE;.BAT;.CMD")
env.pop("PYTHONPATH", None)
run = subprocess.run([powershell, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", str(wrapper),
@@ -67,5 +67,8 @@ exit $LASTEXITCODE
assert [json.loads(line) for line in log.read_text(encoding="utf-8-sig").splitlines()] == [
["python", "install", "--no-bin", "3.12"], ["python", "find", "--managed-python", "--no-project", "3.12"],
]
- assert json.loads(pm_log.read_text(encoding="utf-8-sig")) == ["install"]
+ if stage == "python-deps":
+ assert json.loads(pm_log.read_text(encoding="utf-8-sig")) == ["install"]
+ else:
+ assert not pm_log.exists()
assert existing.read_text(encoding="utf-8-sig") == "previous generation"
diff --git a/tests/test_install_sh_python_pin_indent.py b/tests/test_install_sh_python_pin_indent.py
index d7b93c3efc5..4dd24c1b5c3 100644
--- a/tests/test_install_sh_python_pin_indent.py
+++ b/tests/test_install_sh_python_pin_indent.py
@@ -21,7 +21,8 @@ pytestmark = pytest.mark.platforms("posix")
@pytest.mark.parametrize("indent,blank_lines", [(2, False), (4, False), (0, False), ("\t", False), (4, True)],
ids=["two-spaces", "four-spaces", "no-indent", "tabs", "blank-lines"])
-def test_bootstrap_python_reads_pin_independent_of_indentation(tmp_path, indent, blank_lines):
[email protected]("entrypoint", ["bootstrap_python", "stage_venv"])
+def test_bootstrap_python_reads_pin_independent_of_indentation(tmp_path, indent, blank_lines, entrypoint):
bash = shutil.which("bash")
assert bash, "the shell bootstrap contract requires Bash"
core = tmp_path / "checkout"
@@ -55,7 +56,7 @@ def test_bootstrap_python_reads_pin_independent_of_indentation(tmp_path, indent,
f'source "{(ROOT / "scripts/install.sh").as_posix()}" --manifest\n'
f'ensure_uv() {{ UV_CMD="{uv.as_posix()}"; }}\n'
f'INSTALL_DIR="{core.as_posix()}"\n'
- "bootstrap_python\n"
+ f"{entrypoint}\n"
)
env = dict(os.environ, HOME=str(tmp_path), HERMES_HOME=str(tmp_path / ".hermes"))
result = subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, timeout=30)