The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui): ship runtime Python helpers with the built product
Mikhail Tarasovc1f19acb7f8d
diff --git a/scripts/build/README.md b/scripts/build/README.mdindex 7a7da510f5d..54c110ae434 100644--- a/scripts/build/README.md+++ b/scripts/build/README.md@@ -109,7 +109,7 @@ node scripts/build/desktop.mjs --source /work/source \| Builder | Required CLI arguments | Additional CLI arguments | Product contents ||---|---|---|---|-| `tui.mjs` | `--source`, `--out` | None | `dist/entry.js` and `package.json` with `type: module` |+| `tui.mjs` | `--source`, `--out` | None | `dist/entry.js`, gateway/clipboard Python helpers in `dist/scripts/`, and `package.json` with `type: module` || `web.mjs` | `--source`, `--out`, `--icons` | None | `index.html`, Vite assets, and public assets || `desktop.mjs` | `--source`, `--out`, `--icons`, `--stamp`, `--native-deps` | `--typecheck`, `--platform` | Renderer assets, `electron-main.mjs`, `electron-preload.js`, and native `node_modules` |@@ -122,7 +122,7 @@ Each compiler publishes `hermes-build.json` inside its output (inside `dist/`for TUI). `freshness.mjs` owns this receipt and all source input selection.TUI inputs are its source tree, the Ink source alias, shared sources, theirmanifests and TypeScript configuration, dependency locks, and its compiler and-shared compiler helpers. Tests, workspace documentation, dependency-provider+shared compiler helpers, plus the shipped Python runtime helpers. Tests, workspace documentation, dependency-providerrecipes, and other products' compiler recipes do not invalidate the TUI.It records product/host identity, content hashes of workspace/shared sources andbuild inputs, and the exact supplied icon directory, desktop install stamp, anddiff --git a/scripts/build/freshness.mjs b/scripts/build/freshness.mjsindex d97b6365f06..fe5732175a6 100644--- a/scripts/build/freshness.mjs+++ b/scripts/build/freshness.mjs@@ -28,6 +28,7 @@ const tuiInputs = []),'tsconfig.json', 'package.json', 'package-lock.json', '.npmrc', 'pm/lock.json','scripts/build/tui.mjs', 'scripts/build/frontend-common.mjs', 'scripts/build/freshness.mjs',+ 'ui-tui/scripts/gateway_bootstrap.py', 'ui-tui/scripts/clipboard_image.py',]function treeHash(root, inputs, skip, contents = () => true) {diff --git a/scripts/build/tui.mjs b/scripts/build/tui.mjsindex f5db0911dba..021b78d9665 100644--- a/scripts/build/tui.mjs+++ b/scripts/build/tui.mjs@@ -1,6 +1,6 @@#!/usr/bin/env node// Self-contained TUI compiler. Dependencies must already be prepared.-import { cpSync, readFileSync, writeFileSync, mkdtempSync, rmSync } from 'node:fs'+import { cpSync, readFileSync, writeFileSync, mkdirSync, mkdtempSync, rmSync } from 'node:fs'import { tmpdir } from 'node:os'import { resolve, join } from 'node:path'import { pathToFileURL } from 'node:url'@@ -67,6 +67,12 @@ export async function buildTui(options) {if (body.startsWith('#!')) {writeFileSync(out, body.slice(body.indexOf('\n') + 1))}+ // Packaged Python code and the TUI can live in separate store paths.+ const helpers = join(product, 'dist/scripts')+ mkdirSync(helpers)+ for (const name of ['gateway_bootstrap.py', 'clipboard_image.py']) {+ cpSync(join(root, 'scripts', name), join(helpers, name))+ }writeFileSync(join(product, 'package.json'), JSON.stringify({ type: 'module' }) + '\n')recordProduct({ source, product: 'tui', out: join(product, 'dist'), inputs })})diff --git a/tests-js/product-builders.test.mjs b/tests-js/product-builders.test.mjsindex beb69fc3ea5..1f5a7ef2b1f 100644--- a/tests-js/product-builders.test.mjs+++ b/tests-js/product-builders.test.mjs@@ -1,5 +1,5 @@import { execFileSync } from 'node:child_process'-import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, symlinkSync, statSync, utimesSync } from 'node:fs'+import { copyFileSync, mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, symlinkSync, statSync, utimesSync } from 'node:fs'import { tmpdir } from 'node:os'import path from 'node:path'import { createRequire } from 'node:module'@@ -185,12 +185,22 @@ function tuiSource(source) {put(source, 'ui-tui/package.json', '{"type":"module"}')put(source, 'ui-tui/src/entry.tsx', '#!/usr/bin/env node\nimport { answer } from "@hermes/ink"; console.log(answer);')put(source, 'ui-tui/packages/hermes-ink/src/entry-exports.ts', 'import devtools from "react-devtools-core"; devtools.initialize(); export const answer: string = "prepared source";')+ for (const name of ['gateway_bootstrap.py', 'clipboard_image.py']) {+ put(source, `ui-tui/scripts/${name}`, `print(${JSON.stringify(name)})\n`)+ }}test('TUI uses prepared workspace-local tools from an unrelated cwd and publishes a relocatable module product', () => {const base = fixture()const source = path.join(base, 'source with spaces')tuiSource(source)+ copyFileSync(path.join(repo, 'ui-tui/src/lib/pythonHelpers.ts'), path.join(source, 'ui-tui/src/pythonHelpers.ts'))+ put(source, 'ui-tui/src/entry.tsx', `#!/usr/bin/env node+import { readFileSync } from 'node:fs'+import { answer } from '@hermes/ink'+import { pythonHelperPath } from './pythonHelpers'+console.log(JSON.stringify({ answer, helpers: ['gateway_bootstrap.py', 'clipboard_image.py'].map(name =>+ readFileSync(pythonHelperPath(name, process.env.HERMES_PYTHON_SRC_ROOT), 'utf8')) }))`)dependency(source, 'ui-tui', 'esbuild')const out = path.join(base, 'product one')execFileSync(process.execPath, [path.join(repo, 'scripts/build/tui.mjs'), '--source', source, '--out', out], { cwd: base })@@ -198,7 +208,16 @@ test('TUI uses prepared workspace-local tools from an unrelated cwd and publisheconst entry = path.join(out, 'dist/entry.js')expect(readFileSync(entry, 'utf8').startsWith('#!')).toBe(false)rmSync(source, { recursive: true })- expect(execFileSync(process.execPath, [entry], { cwd: base, encoding: 'utf8' }).trim()).toBe('prepared source')+ // Nix keeps Python's site-packages separate from the compiled TUI (#139208).+ const pythonRoot = path.join(base, 'site-packages')+ mkdirSync(pythonRoot)+ const result = execFileSync(process.execPath, [entry], {+ cwd: base, encoding: 'utf8', env: { ...process.env, HERMES_PYTHON_SRC_ROOT: pythonRoot },+ })+ expect(JSON.parse(result)).toEqual({+ answer: 'prepared source',+ helpers: ['gateway_bootstrap.py', 'clipboard_image.py'].map(name => `print(${JSON.stringify(name)})\n`),+ })})test('TUI failure does not publish or disturb a previous product, and missing prepared tools fail clearly', async () => {@@ -325,6 +344,7 @@ test('TUI freshness invalidates source, configuration and compiler inputs and da}for (const name of ['ui-tui/src/value.ts', 'apps/shared/src/value.ts',+ 'ui-tui/scripts/gateway_bootstrap.py', 'ui-tui/scripts/clipboard_image.py','ui-tui/packages/hermes-ink/src/entry-exports.ts','ui-tui/tsconfig.json', 'ui-tui/packages/hermes-ink/tsconfig.json', 'apps/shared/tsconfig.json','ui-tui/package.json', 'ui-tui/packages/hermes-ink/package.json', 'apps/shared/package.json',diff --git a/ui-tui/src/gatewayClient.ts b/ui-tui/src/gatewayClient.tsindex ddcc8c38f5f..45f65bfca9d 100644--- a/ui-tui/src/gatewayClient.ts+++ b/ui-tui/src/gatewayClient.ts@@ -26,6 +26,7 @@ import type { AnyGatewayEvent } from './gatewayTypes.js'import { t } from './i18n/runtime.js'import { CircularBuffer } from './lib/circularBuffer.js'import { recordParentLifecycle } from './lib/parentLog.js'+import { pythonHelperPath } from './lib/pythonHelpers.js'const MAX_GATEWAY_LOG_LINES = 200const MAX_LOG_LINE_BYTES = 4096@@ -133,7 +134,7 @@ const bootstrapLocalGateway = async (start: boolean, recover = false): Promise<Lconst root = process.env.HERMES_PYTHON_SRC_ROOT ?? resolve(import.meta.dirname, '../../')const { stdout } = await promisify(execFile)(resolvePython(),- [resolve(root, 'ui-tui/scripts/gateway_bootstrap.py'), ...(start ? ['--start'] : recover ? ['--recover'] : [])],+ [pythonHelperPath('gateway_bootstrap.py', root), ...(start ? ['--start'] : recover ? ['--recover'] : [])],{ cwd: root, env: { ...process.env, PYTHONPATH: root }, timeout: 40_000, maxBuffer: 1024 * 1024 })return JSON.parse(stdout) as LocalGatewayGrantdiff --git a/ui-tui/src/lib/imageAttachments.ts b/ui-tui/src/lib/imageAttachments.tsindex d2df66881c6..96b375129d4 100644--- a/ui-tui/src/lib/imageAttachments.ts+++ b/ui-tui/src/lib/imageAttachments.ts@@ -11,6 +11,8 @@ import { sessionCwd } from '../app/uiStore.js'import type { GatewayClient } from '../gatewayClient.js'import { t } from '../i18n/runtime.js'+import { pythonHelperPath } from './pythonHelpers.js'+export interface ImageAttachment { path: string; mime: string }export interface StagedImage extends ImageAttachment { name: string; remainder?: string }@@ -76,7 +78,7 @@ async function extractClipboardImage(path: string): Promise<boolean> {const python = process.env.HERMES_PYTHON?.trim() || process.env.PYTHON?.trim() ||(process.platform === 'win32' ? 'python' : 'python3')- const { stdout } = await promisify(execFile)(python, [join(root, 'ui-tui/scripts/clipboard_image.py'), path],+ const { stdout } = await promisify(execFile)(python, [pythonHelperPath('clipboard_image.py', root), path],{ cwd: root, env: { ...process.env, PYTHONPATH: root }, timeout: 15_000, windowsHide: true })return stdout.trim() === 'true'diff --git a/ui-tui/src/lib/pythonHelpers.ts b/ui-tui/src/lib/pythonHelpers.tsnew file mode 100644index 00000000000..76343b87be4--- /dev/null+++ b/ui-tui/src/lib/pythonHelpers.ts@@ -0,0 +1,9 @@+import { existsSync } from 'node:fs'+import { resolve } from 'node:path'++export function pythonHelperPath(name: string, pythonRoot: string): string {+ // esbuild keeps import.meta.dirname relative to the shipped entry.js.+ const bundled = resolve(import.meta.dirname, 'scripts', name)++ return existsSync(bundled) ? bundled : resolve(pythonRoot, 'ui-tui/scripts', name)+}