Skip to content

The agent that grows with you

fix(install-e2e): accept the app's own report where the backend environment is unreadable

ethernet48f619f4e47e
diff --git a/tests-js/scripts/desktop-smoke-process.ts b/tests-js/scripts/desktop-smoke-process.ts
index c72628d97ef..d8ce5aabf51 100644
--- a/tests-js/scripts/desktop-smoke-process.ts
+++ b/tests-js/scripts/desktop-smoke-process.ts
@@ -18,6 +18,12 @@ export interface NativeProcess {
cwd?: string
}
+/** What the caller already knows about the app that owns this listener. */
+export interface OriginEvidence {
+ /** The root the app itself reported resolving, from its own UI/identity channel. */
+ appReportedRoot?: string
+}
+
export function within(root: string, candidate: string): boolean {
const relative = path.relative(fs.realpathSync(root), fs.realpathSync(candidate))
return relative === '' || (relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
@@ -154,7 +160,8 @@ export function localBackendProcess(port: number, electronPid: number): NativePr
return backend
}
-export function assertBackendOrigin(backend: NativeProcess, root: string, origin: 'source' | 'bundled'): void {
+export function assertBackendOrigin(backend: NativeProcess, root: string, origin: 'source' | 'bundled',
+ evidence: OriginEvidence = {}): void {
if (origin === 'bundled') {
if (path.basename(root) !== 'agent-payload' || !within(root, backend.executable)) {
throw new Error('Bundled backend listener is not running the installed agent-payload interpreter')
@@ -205,10 +212,22 @@ export function assertBackendOrigin(backend: NativeProcess, root: string, origin
const usesInstallEnvironment = joinsInstall(backend.pythonPath)
|| (backend.virtualEnv !== undefined && backend.virtualEnv.trim() !== ''
&& sameTree(path.dirname(backend.virtualEnv)))
- if (!sameTree(backend.cwd) && !namesInstallRoot && !usesInstallEnvironment) {
+ // Some platforms expose no way to read another process's environment or cwd, so a
+ // module-launched backend there can never name its tree in argv (Windows: the venv
+ // launcher hands the interpreter over as a system python). The listener has already
+ // been tied to the app process that owns it, and the caller has already asserted the
+ // root that app reported resolving, so those two facts together are the evidence.
+ // Requiring the process evidence to be absent keeps this from loosening a platform
+ // that can read one.
+ const processEvidenceUnreadable = backend.cwd === undefined
+ && backend.pythonPath === undefined && backend.virtualEnv === undefined
+ const appOwnsBackend = processEvidenceUnreadable
+ && evidence.appReportedRoot !== undefined && sameTree(evidence.appReportedRoot)
+ if (!sameTree(backend.cwd) && !namesInstallRoot && !usesInstallEnvironment && !appOwnsBackend) {
throw new Error('Source backend listener imports a different source tree'
+ ` (cwd=${backend.cwd ?? '(unreadable)'}, HERMES_PYTHON_SRC_ROOT=(unset),`
- + ` executable=${backend.executable}, expected=${root}, command=${backend.command})`)
+ + ` executable=${backend.executable}, expected=${root},`
+ + ` appReportedRoot=${evidence.appReportedRoot ?? '(unreported)'}, command=${backend.command})`)
}
return
}
diff --git a/tests-js/scripts/desktop-smoke.test.ts b/tests-js/scripts/desktop-smoke.test.ts
index c77c29c7a16..09fd5327d32 100644
--- a/tests-js/scripts/desktop-smoke.test.ts
+++ b/tests-js/scripts/desktop-smoke.test.ts
@@ -163,6 +163,42 @@ test('a backend bound to the tree by environment needs no root in argv', (): voi
} finally { fs.rmSync(home, { recursive: true, force: true }) }
})
+test('a platform that cannot read the backend environment proves ownership by the app', (): void => {
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-owner-origin-'))
+ try {
+ const root = path.join(home, 'hermes-agent')
+ const other = path.join(home, 'other-tree')
+ fs.mkdirSync(root, { recursive: true })
+ fs.mkdirSync(other, { recursive: true })
+ // The Windows shape: the app's venv launcher hands the interpreter over as a system
+ // python, so argv never names the tree, and this platform exposes neither the
+ // backend's cwd nor its environment.
+ const backend = {
+ pid: process.pid,
+ parentPid: 1,
+ executable: path.join(home, 'python.exe'),
+ command: `"${path.join(home, 'python.exe')}" "-m" "hermes_cli.main" serve --host 127.0.0.1 --port 0`,
+ }
+ // Control row: with nothing readable and no report from the app, this is still a
+ // different tree.
+ expect((): void => { assertBackendOrigin(backend, root, 'source') }).toThrow('source tree')
+ // The app reported resolving this root (the driver asserts that against options.root
+ // before calling), and the listener was already tied to that same app process.
+ expect((): void => {
+ assertBackendOrigin(backend, root, 'source', { appReportedRoot: root })
+ }).not.toThrow()
+ // A report of some other tree is no evidence for this one.
+ expect((): void => {
+ assertBackendOrigin(backend, root, 'source', { appReportedRoot: other })
+ }).toThrow('source tree')
+ // Readable process evidence keeps the strict path: an environment naming another
+ // tree is not rescued by the app's own report.
+ expect((): void => {
+ assertBackendOrigin({ ...backend, cwd: other, pythonPath: other }, root, 'source', { appReportedRoot: root })
+ }).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')
diff --git a/tests/install/e2e-assets/desktop-smoke.ts b/tests/install/e2e-assets/desktop-smoke.ts
index 4464b17ecf5..c9f1c495b06 100644
--- a/tests/install/e2e-assets/desktop-smoke.ts
+++ b/tests/install/e2e-assets/desktop-smoke.ts
@@ -332,7 +332,10 @@ export async function runInstalledDesktopSmoke(options: SmokeOptions): Promise<v
// assertBackendOrigin fails, or the leg reports a mismatch with nothing to
// inspect.
fs.writeFileSync(path.join(out, `desktop-backend-${options.phase}.log`), connection.logs.map(redact).join('\n'))
- assertBackendOrigin(backend, options.root, options.origin)
+ // `identity.hermesRoot` was asserted against options.root above, and the listener was
+ // tied to this app process when it was identified, so on a platform that cannot read
+ // the backend's own environment those two facts are the available evidence.
+ assertBackendOrigin(backend, options.root, options.origin, { appReportedRoot: identity.hermesRoot })
const provenanceCommit = readInstallationCommit(options.root, options.origin)
if (provenanceCommit !== options['expect-commit']) { throw new Error('Installed commit differs from --expect-commit') }
fs.writeFileSync(path.join(out, `desktop-backend-${options.phase}.log`), connection.logs.map(redact).join('\n'))