Skip to content

The agent that grows with you

fix(install-e2e): prove the desktop backend's tree from the environment

ethernet4abc223f9069
diff --git a/tests-js/scripts/desktop-smoke-process.ts b/tests-js/scripts/desktop-smoke-process.ts
index 35296d0fab6..c72628d97ef 100644
--- a/tests-js/scripts/desktop-smoke-process.ts
+++ b/tests-js/scripts/desktop-smoke-process.ts
@@ -10,6 +10,11 @@ export interface NativeProcess {
executable: string
command: string
sourceRoot?: string
+ // The app binds its backend to a tree by environment, not only by argv: the
+ // installation root leads PYTHONPATH and VIRTUAL_ENV names the venv. Only the
+ // platforms that can read a process environment populate these.
+ pythonPath?: string
+ virtualEnv?: string
cwd?: string
}
@@ -22,6 +27,11 @@ function nativeText(command: string, args: string[]): string {
return execFileSync(command, args, { encoding: 'utf8', timeout: 30_000, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }).trim()
}
+/** One NAME=value from `ps eww`'s environment tail (values may contain spaces). */
+function psEnvValue(environment: string, name: string): string | undefined {
+ return new RegExp(`(?:^|\\s)${name}=(.*?)(?=\\s+[A-Za-z_][A-Za-z_0-9]*=|$)`).exec(environment)?.[1]
+}
+
/** Call only after binding the live listener (and bundled resources) to root. */
export function readInstallationCommit(root: string, origin: 'source' | 'bundled'): string {
const commit = origin === 'source' ? nativeText('git', ['-C', root, 'rev-parse', 'HEAD'])
@@ -136,8 +146,10 @@ export function localBackendProcess(port: number, electronPid: number): NativePr
backend.command = nativeText('ps', ['-p', String(backend.pid), '-o', 'args='])
backend.cwd = nativeText('/usr/sbin/lsof', ['-a', '-p', String(backend.pid), '-d', 'cwd', '-Fn'])
.split('\n').find((line: string): boolean => line.startsWith('n'))?.slice(1)
- backend.sourceRoot = /(?:^|\s)HERMES_PYTHON_SRC_ROOT=(.*?)(?=\s+[A-Za-z_][A-Za-z_0-9]*=|$)/
- .exec(nativeText('ps', ['eww', '-p', String(backend.pid), '-o', 'args=']))?.[1]
+ const environment = nativeText('ps', ['eww', '-p', String(backend.pid), '-o', 'args='])
+ backend.sourceRoot = psEnvValue(environment, 'HERMES_PYTHON_SRC_ROOT')
+ backend.pythonPath = psEnvValue(environment, 'PYTHONPATH')
+ backend.virtualEnv = psEnvValue(environment, 'VIRTUAL_ENV')
}
return backend
}
@@ -182,10 +194,21 @@ export function assertBackendOrigin(backend: NativeProcess, root: string, origin
// cwd-only inference calls a backend running the installation's own venv
// interpreter "a different source tree". Accept the launcher cd'ing into the
// tree, or a command that names it (`<root>/venv/bin/python -m hermes_cli.main`).
- if (!sameTree(backend.cwd) && !namesInstallRoot) {
+ // The app binds the backend to the tree by environment as well as argv:
+ // `main.ts` puts the installation root first on the backend's PYTHONPATH and
+ // VIRTUAL_ENV names its venv, which is how `import hermes_cli` resolves from
+ // the installation. A platform that can read that environment needs no
+ // spelling in argv (macOS resolves the venv symlink before spawning); a
+ // platform that cannot (Windows) stays strict.
+ const joinsInstall = (value?: string): boolean => value !== undefined
+ && value.split(path.delimiter).some((entry: string): boolean => entry !== '' && sameTree(entry))
+ const usesInstallEnvironment = joinsInstall(backend.pythonPath)
+ || (backend.virtualEnv !== undefined && backend.virtualEnv.trim() !== ''
+ && sameTree(path.dirname(backend.virtualEnv)))
+ if (!sameTree(backend.cwd) && !namesInstallRoot && !usesInstallEnvironment) {
throw new Error('Source backend listener imports a different source tree'
+ ` (cwd=${backend.cwd ?? '(unreadable)'}, HERMES_PYTHON_SRC_ROOT=(unset),`
- + ` expected=${root}, command=${backend.command})`)
+ + ` executable=${backend.executable}, expected=${root}, command=${backend.command})`)
}
return
}
diff --git a/tests-js/scripts/desktop-smoke.test.ts b/tests-js/scripts/desktop-smoke.test.ts
index 2bffcbb7331..c77c29c7a16 100644
--- a/tests-js/scripts/desktop-smoke.test.ts
+++ b/tests-js/scripts/desktop-smoke.test.ts
@@ -132,6 +132,37 @@ test.runIf(process.platform === 'linux')('origin proof finds the live child list
} finally { child.kill(); await new Promise<void>((resolve): void => { child.once('exit', (): void => resolve()) }) }
})
+test('a backend bound to the tree by environment needs no root in argv', (): void => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-env-origin-'))
+ try {
+ const root = path.join(home, 'hermes-agent')
+ fs.mkdirSync(path.join(root, 'venv'), { recursive: true })
+ // The macOS shape: a venv's interpreter is a SYMLINK to the framework
+ // binary, and the app resolves it before spawning, so argv names that binary
+ // and never the installation root.
+ const resolvedPython = fs.realpathSync(process.execPath)
+ const interpreter = path.join(home, 'python3.11')
+ fs.symlinkSync(resolvedPython, interpreter)
+ const base = { pid: process.pid, parentPid: 1, executable: interpreter, cwd: home,
+ command: `"${resolvedPython}" "-m" "hermes_cli.main" serve --host 127.0.0.1 --port 0` }
+ // Control: with no environment evidence this is still a different tree.
+ expect((): void => { assertBackendOrigin(base, root, 'source') }).toThrow('source tree')
+ expect((): void => {
+ assertBackendOrigin({ ...base, pythonPath: `${root}${path.delimiter}/elsewhere` }, root, 'source')
+ }).not.toThrow()
+ expect((): void => {
+ assertBackendOrigin({ ...base, virtualEnv: path.join(root, 'venv') }, root, 'source')
+ }).not.toThrow()
+ // Evidence naming some OTHER tree is no evidence for this one.
+ expect((): void => {
+ assertBackendOrigin({ ...base, pythonPath: path.join(home, 'elsewhere') }, root, 'source')
+ }).toThrow('source tree')
+ expect((): void => {
+ assertBackendOrigin({ ...base, virtualEnv: path.join(home, 'other-venv') }, root, 'source')
+ }).toThrow('source tree')
+ } finally { fs.rmSync(home, { recursive: true, force: true }) }
+})
+
test('source launch restores only an explicitly captured exact editable root', (): void => {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-source-'))
const specPath = path.join(home, 'launch.json')