The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(install-e2e): the canonical gateway proves its commit where its cwd is unreadable, and the update window looks for it
Teknium48b5c3283b5a
diff --git a/tests-js/scripts/desktop-smoke-process.ts b/tests-js/scripts/desktop-smoke-process.tsindex 32cf9c593d6..34bdc391763 100644--- a/tests-js/scripts/desktop-smoke-process.ts+++ b/tests-js/scripts/desktop-smoke-process.ts@@ -27,12 +27,16 @@ export interface NativeProcess {* or the canonical gateway this home's `gateway_state.json` names). */export interface BackendProcess extends NativeProcess {ownership: 'child' | 'host-ledger' | 'host-gateway'+ /** For a canonical gateway: the commit it stamped into its own `gateway_state.json` (`code_sha`). */+ codeSha?: 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+ /** The commit the expected installation records (`readInstallationCommit`). */+ installedCommit?: string}function nativeText(command: string, args: string[]): string {@@ -183,6 +187,7 @@ function processStartTime(pid: number): number | null {const gatewayStateSchema = z.object({pid: z.number().int().positive(), kind: z.literal('hermes-gateway'),gateway_state: z.string(), start_time: z.number().nullable().optional(),+ code_sha: z.string().nullable().optional(),}).passthrough()/** Whether `pid` is still the incarnation `gateway.status._get_process_start_time` recorded:@@ -209,8 +214,8 @@ function gatewayIncarnationMatches(pid: number, recorded: number | null | undefi/** The running canonical gateway (`hermes gateway run`) recorded for `homes`. Desktop reaches a* local backend through `hermes gateway ensure`, which attaches to that daemon or starts it* detached, so it is neither an Electron descendant nor a `serve` spawn-ledger record. */-function canonicalGatewayPids(homes: string[], processes: NativeProcess[]): number[] {- return [...new Set(homes)].flatMap((home: string): number[] => {+function canonicalGateways(homes: string[], processes: NativeProcess[]): Map<number, string | undefined> {+ return new Map([...new Set(homes)].flatMap((home: string): Array<[number, string | undefined]> => {try {const state = gatewayStateSchema.parse(JSON.parse(fs.readFileSync(path.join(home, 'gateway_state.json'), 'utf8').replace(/^\uFEFF/, '')))@@ -218,9 +223,9 @@ function canonicalGatewayPids(homes: string[], processes: NativeProcess[]): numbconst live = state.gateway_state === 'running' && processes.some(candidate => candidate.pid === state.pid)&& gatewayIncarnationMatches(state.pid, state.start_time)- return live ? [state.pid] : []+ return live ? [[state.pid, state.code_sha ?? undefined]] : []} catch { return [] /* Missing/unreadable state provides no attachment evidence. */ }- })+ }))}/** Identify the actual listener, not a healthy helper or a command recorded before spawn. */@@ -236,7 +241,7 @@ export function localBackendProcess(port: number, electronPid: number, hermesHom// its own child. Registration is only a candidate: the OS listener and the// caller's existing installed-tree checks must independently agree.let registered: number[] = []- let gateways: number[] = []+ let gateways = new Map<number, string | undefined>()if (hermesHome) {// The product writes one machine-root ledger for every profile, and the app@@ -244,7 +249,7 @@ export function localBackendProcess(port: number, electronPid: number, hermesHom// where a `<root>/profiles/<name>` home resolves to `<root>`).const ledgerRoot = resolveDesktopHermesHome({ home: os.homedir(), env: { HERMES_HOME: hermesHome } })// A named profile is served by the root's multiplexer; a standalone one by its own gateway.- gateways = canonicalGatewayPids([hermesHome, ledgerRoot], processes)+ gateways = canonicalGateways([hermesHome, ledgerRoot], processes)try {registered = parseSpawnLedger(fs.readFileSync(path.join(ledgerRoot, SPAWN_LEDGER_FILENAME), 'utf8'))@@ -266,7 +271,7 @@ export function localBackendProcess(port: number, electronPid: number, hermesHom}const candidates = processes.filter(candidate => children.includes(candidate) || registered.includes(candidate.pid)- || gateways.includes(candidate.pid))+ || gateways.has(candidate.pid))let pids: number[]@@ -289,6 +294,10 @@ export function localBackendProcess(port: number, electronPid: number, hermesHomconst ownership = children.includes(matches[0]) ? 'child' : registered.includes(matches[0].pid) ? 'host-ledger' : 'host-gateway'const backend: BackendProcess = { ...matches[0], ownership }+ if (ownership === 'host-gateway') {+ backend.codeSha = gateways.get(backend.pid)+ }+if (process.platform === 'linux') {backend.executable = fs.readlinkSync(`/proc/${backend.pid}/exe`)backend.cwd = fs.readlinkSync(`/proc/${backend.pid}/cwd`)@@ -321,8 +330,18 @@ function appVouchesForBackend(backend: BackendProcess, evidence: OriginEvidence,const processEvidenceUnreadable = backend.cwd === undefined&& backend.pythonPath === undefined && backend.virtualEnv === undefined- return processEvidenceUnreadable && backend.ownership === 'child'- && evidence.appReportedRoot !== undefined && sameTree(evidence.appReportedRoot)+ if (!processEvidenceUnreadable || evidence.appReportedRoot === undefined || !sameTree(evidence.appReportedRoot)) {+ return false+ }++ // A canonical gateway is not the app's descendant (`gateway ensure` starts it detached),+ // and Windows cannot read its cwd, the evidence `main` uses for it. It must vouch for+ // itself: the commit it stamped into its own gateway_state.json is the installed one.+ if (backend.ownership === 'host-gateway') {+ return evidence.installedCommit !== undefined && backend.codeSha === evidence.installedCommit+ }++ return backend.ownership === 'child'}export function assertBackendOrigin(backend: BackendProcess, root: string, origin: 'source' | 'bundled',diff --git a/tests-js/scripts/desktop-smoke.test.ts b/tests-js/scripts/desktop-smoke.test.tsindex 9f9615881aa..e9738671fe0 100644--- a/tests-js/scripts/desktop-smoke.test.ts+++ b/tests-js/scripts/desktop-smoke.test.ts@@ -291,6 +291,24 @@ test('a canonical gateway proves its tree by the cwd `gateway ensure` starts it} finally { fs.rmSync(root, { recursive: true, force: true }) }})+test('a canonical gateway whose cwd is unreadable proves its tree by the commit it stamped', (): void => {+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-gateway-sha-'))++ try {+ // Windows: CIM exposes neither cwd nor environment for the detached `gateway run`.+ const executable = path.join(os.tmpdir(), 'hermes-tools', 'python', 'python.exe')+ const commit = 'a'.repeat(40)++ const backend = { pid: 2, parentPid: 1, executable, ownership: 'host-gateway' as const, codeSha: commit,+ command: [executable, '-m', 'hermes_cli.main', '--profile', 'default', 'gateway', 'run', '--quiet'].map(arg => JSON.stringify(arg)).join(' ') }++ expect(() => assertBackendOrigin(backend, root, 'source', { appReportedRoot: root, installedCommit: commit })).not.toThrow()+ // A gateway started from another commit (or one that stamped none) is not vouched for.+ expect(() => assertBackendOrigin({ ...backend, codeSha: 'b'.repeat(40) }, root, 'source', { appReportedRoot: root, installedCommit: commit })).toThrow('different source tree')+ expect(() => assertBackendOrigin({ ...backend, codeSha: undefined }, root, 'source', { appReportedRoot: root, installedCommit: commit })).toThrow('different source tree')+ } finally { fs.rmSync(root, { recursive: true, force: true }) }+})+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-'))@@ -388,7 +406,7 @@ test('OLD update-window source provenance carries its verified app identity to tfs.mkdirSync(other, { recursive: true })// As `localBackendProcess` reports a listener that descends from the app.- const backend = { pid: 2, parentPid: 1, executable: path.join(home, 'python.exe'), ownership: 'child',+ const backend = { pid: 2, parentPid: 1, executable: path.join(home, 'python.exe'), ownership: 'child' as const,command: `"${path.join(home, 'python.exe')}" -m hermes_cli.main dashboard --port 0` }expect((): void => {diff --git a/tests/install/e2e-assets/desktop-smoke.ts b/tests/install/e2e-assets/desktop-smoke.tsindex 7b1cd0c5258..35c551cc6be 100644--- a/tests/install/e2e-assets/desktop-smoke.ts+++ b/tests/install/e2e-assets/desktop-smoke.ts@@ -346,9 +346,10 @@ export async function runInstalledDesktopSmoke(options: SmokeOptions, launchApp:// `identity.hermesRoot` was asserted against options.root above. For a listener that// descends from this app process, on a platform that cannot read the backend's own// environment, those two facts are the available evidence; a ledger-attached host- // backend is not tied to the app by ancestry and must prove its tree itself.- assertBackendOrigin(backend, options.root, options.origin, { appReportedRoot: identity.hermesRoot })+ // backend is not tied to the app by ancestry and must prove its tree itself; a canonical+ // gateway whose cwd is unreadable must report the installed commit.const provenanceCommit = readInstallationCommit(options.root, options.origin)+ assertBackendOrigin(backend, options.root, options.origin, { appReportedRoot: identity.hermesRoot, installedCommit: provenanceCommit })if (provenanceCommit !== options['expect-commit']) { throw new Error('Installed commit differs from --expect-commit') }// Trace only chat actions. Connection probes can return credential-bearing logs.await app.context().tracing.start({ screenshots: true, snapshots: false, sources: false })diff --git a/tests/install/e2e-assets/update-window-chat.d.mts b/tests/install/e2e-assets/update-window-chat.d.mtsindex 8ce557f8069..8ae3a467500 100644--- a/tests/install/e2e-assets/update-window-chat.d.mts+++ b/tests/install/e2e-assets/update-window-chat.d.mts@@ -1,9 +1,10 @@import type { ElectronApplication, Page } from '@playwright/test'-import type { NativeProcess } from '../../../tests-js/scripts/desktop-smoke-process.ts'+import type { BackendProcess } from '../../../tests-js/scripts/desktop-smoke-process.ts'export interface UpdateWindowIdentity {hermesRoot: string+ hermesHome?: string}export interface UpdateWindowChatOptions {@@ -28,10 +29,11 @@ export function assertUpdateWindowProcess(): voidexport function assertUpdateWindowBackendOrigin(- backend: NativeProcess,+ backend: BackendProcess,identity: UpdateWindowIdentity,root: string,origin: 'source' | 'bundled',+ installedCommit?: string,): voidexport function runUpdateWindowChat(diff --git a/tests/install/e2e-assets/update-window-chat.mjs b/tests/install/e2e-assets/update-window-chat.mjsindex b4cf0bab79c..10bb12ad23d 100644--- a/tests/install/e2e-assets/update-window-chat.mjs+++ b/tests/install/e2e-assets/update-window-chat.mjs@@ -10,11 +10,11 @@ import { assertBackendOrigin, localBackendProcess, readInstallationCommit } from* Electron app. Windows CIM exposes neither cwd nor environment for this* module launch. The verified identity and listener ownership prove provenance.*/-export function assertUpdateWindowBackendOrigin(backend, identity, root, origin) {+export function assertUpdateWindowBackendOrigin(backend, identity, root, origin, installedCommit) {if (origin === 'source' && fs.realpathSync(identity.hermesRoot) !== fs.realpathSync(root)) {throw new Error('OLD update-window chat resolved another source installation');}- assertBackendOrigin(backend, root, origin, { appReportedRoot: identity.hermesRoot });+ assertBackendOrigin(backend, root, origin, { appReportedRoot: identity.hermesRoot, installedCommit });}/**@@ -56,9 +56,10 @@ export async function runUpdateWindowChat(app, page, options) {if (connection.mode !== 'local' || !['127.0.0.1', 'localhost', '[::1]'].includes(base.hostname)) {throw new Error('OLD update-window chat did not use the local installed backend');}- const backend = localBackendProcess(Number(base.port), running.pid);- assertUpdateWindowBackendOrigin(backend, identity, options.root, options.origin);+ // The app's own home names the canonical gateway it dials (`gateway_state.json`).+ const backend = localBackendProcess(Number(base.port), running.pid, identity.hermesHome);const provenanceCommit = readInstallationCommit(options.root, options.origin);+ assertUpdateWindowBackendOrigin(backend, identity, options.root, options.origin, provenanceCommit);if (provenanceCommit !== options.expectCommit) throw new Error('Installed OLD commit differs from the expected commit');const chat = await runDesktopChatSmoke(page, { ...options, phase: 'old', provenanceCommit });const result = { ...chat, origin: options.origin, root: options.root,