From 61c66f741dbcaee63f09176e754be0bc06fbc5c4 Mon Sep 17 00:00:00 2001 From: BearHuddleston Date: Sat, 28 Nov 2026 20:23:07 +0000 Subject: [PATCH] refactor(desktop): share AppImage payloadCopyKey and mountTypeFor helpers with smoke driver --- .../electron/appimage-payload-shared.d.mts | 10 +++++ .../electron/appimage-payload-shared.mjs | 41 +++++++++++++++++++ apps/desktop/electron/appimage-payload.ts | 40 ++---------------- tests-js/bundle-smoke-metadata.test.mjs | 9 ++-- tests-js/scripts/desktop-smoke-process.ts | 25 ----------- tests-js/scripts/desktop-smoke.test.ts | 14 +++---- .../e2e-assets/bundle-smoke-metadata.mjs | 12 +----- tests/install/e2e-assets/desktop-smoke.ts | 5 ++- 8 files changed, 69 insertions(+), 87 deletions(-) create mode 100644 apps/desktop/electron/appimage-payload-shared.d.mts create mode 100644 apps/desktop/electron/appimage-payload-shared.mjs diff --git a/apps/desktop/electron/appimage-payload-shared.d.mts b/apps/desktop/electron/appimage-payload-shared.d.mts new file mode 100644 index 0000000000..359865a131 --- /dev/null +++ b/apps/desktop/electron/appimage-payload-shared.d.mts @@ -0,0 +1,10 @@ +export interface PayloadKeyStamp { + commit?: string | null + builtAt?: string | null + source?: string | null + displayVersion?: string | null +} + +export function payloadCopyKey(stamp: Readonly | null): string | null + +export function mountTypeFor(target: string, mountinfo: string): string | null diff --git a/apps/desktop/electron/appimage-payload-shared.mjs b/apps/desktop/electron/appimage-payload-shared.mjs new file mode 100644 index 0000000000..1e4b5e1352 --- /dev/null +++ b/apps/desktop/electron/appimage-payload-shared.mjs @@ -0,0 +1,41 @@ +// appimage-payload-shared.mjs — pure AppImage payload key and Linux mountinfo +// parsing helpers, shared by the desktop app (via appimage-payload.ts) and the +// CI smoke driver (which runs under Node's type-stripping and cannot import +// extensionless TypeScript modules). No Electron imports here; only node:path. + +import path from 'node:path' + +/** Matches write-build-stamp.mjs's all-zero placeholder for non-git builds. */ +function isFallbackCommit(commit) { + return /^0{7,40}$/.test(commit) +} + +/** One directory per build, so a rebuild at the same commit never runs an older copy. */ +export function payloadCopyKey(stamp) { + if (!stamp?.commit || !stamp.builtAt || stamp.source === 'fallback' || isFallbackCommit(stamp.commit)) { + return null + } + const version = stamp.displayVersion ? `${stamp.displayVersion}-` : '' + return `${version}${stamp.commit.slice(0, 12)}-${stamp.builtAt}`.replace(/[^\w.+-]/g, '') +} + +function unescapeMountPath(value) { + return value.replace(/\\([0-7]{3})/g, (_match, octal) => String.fromCharCode(Number.parseInt(octal, 8))) +} + +/** The filesystem type of the innermost mount holding `target`, from /proc/self/mountinfo. */ +export function mountTypeFor(target, mountinfo) { + let best = null + for (const line of mountinfo.split('\n')) { + const [mount, filesystem] = line.split(' - ') + const point = unescapeMountPath(mount?.split(' ')[4] ?? '') + const relative = path.posix.relative(point, target) + if (!filesystem || !point || relative.startsWith('..') || path.posix.isAbsolute(relative)) { + continue + } + if (!best || point.length >= best.point.length) { + best = { point, type: filesystem.split(' ')[0] } + } + } + return best?.type ?? null +} diff --git a/apps/desktop/electron/appimage-payload.ts b/apps/desktop/electron/appimage-payload.ts index 01db64fcbb..380fbf02bb 100644 --- a/apps/desktop/electron/appimage-payload.ts +++ b/apps/desktop/electron/appimage-payload.ts @@ -11,11 +11,13 @@ import os from 'node:os' import path from 'node:path' import { promisify } from 'node:util' -import { isFallbackCommit } from './bundle-skew' +import { mountTypeFor, payloadCopyKey } from './appimage-payload-shared.mjs' import { removeBundleCliLinks } from './cli-provision' import { INSTALL_STAMP, type InstallStamp } from './install-stamp' import { ownAppImage } from './relaunch-target' +export { mountTypeFor, payloadCopyKey } + export type BootProgress = (phase: string, message: string, progress: number) => Promise | void export interface PayloadTally { @@ -78,42 +80,6 @@ export function payloadCopiesRoot(env: NodeJS.ProcessEnv, home: string): string return path.join(xdg && path.isAbsolute(xdg) ? xdg : path.join(home, '.local', 'share'), 'hermes', 'bundles') } -/** One directory per build, so a rebuild at the same commit never runs an older copy. */ -export function payloadCopyKey(stamp: Readonly | null): string | null { - if (!stamp?.commit || !stamp.builtAt || stamp.source === 'fallback' || isFallbackCommit(stamp.commit)) { - return null - } - - const version: string = stamp.displayVersion ? `${stamp.displayVersion}-` : '' - - return `${version}${stamp.commit.slice(0, 12)}-${stamp.builtAt}`.replace(/[^\w.+-]/g, '') -} - -function unescapeMountPath(value: string): string { - return value.replace(/\\([0-7]{3})/g, (_match, octal: string) => String.fromCharCode(parseInt(octal, 8))) -} - -/** The filesystem type of the innermost mount holding `target`, from /proc/self/mountinfo. */ -export function mountTypeFor(target: string, mountinfo: string): string | null { - let best: { point: string; type: string } | null = null - - for (const line of mountinfo.split('\n')) { - const [mount, filesystem] = line.split(' - ') - const point: string = unescapeMountPath(mount?.split(' ')[4] ?? '') - const relative: string = path.relative(point, target) - - if (!filesystem || !point || relative.startsWith('..') || path.isAbsolute(relative)) { - continue - } - - if (!best || point.length >= best.point.length) { - best = { point, type: filesystem.split(' ')[0] } - } - } - - return best?.type ?? null -} - /** Only a payload served from the AppImage's FUSE mount is copied; an extracted AppImage runs in place. */ export function payloadCopyPlan( host: Pick, diff --git a/tests-js/bundle-smoke-metadata.test.mjs b/tests-js/bundle-smoke-metadata.test.mjs index d8c0441439..e5cd8cf124 100644 --- a/tests-js/bundle-smoke-metadata.test.mjs +++ b/tests-js/bundle-smoke-metadata.test.mjs @@ -3,8 +3,8 @@ import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { execFileSync, spawnSync } from 'node:child_process' -import { bundleIdentity, linuxPayloadCopyKey, shouldVerifyMacDmg, verifyBundleStamp, verifyLinuxHeader, verifyLinuxMetadata, verifyMacDmg, verifyMacMetadata } from '../tests/install/e2e-assets/bundle-smoke-metadata.mjs' -import { payloadCopyKey } from '../apps/desktop/electron/appimage-payload.ts' +import { bundleIdentity, shouldVerifyMacDmg, verifyBundleStamp, verifyLinuxHeader, verifyLinuxMetadata, verifyMacDmg, verifyMacMetadata } from '../tests/install/e2e-assets/bundle-smoke-metadata.mjs' +import { payloadCopyKey } from '../apps/desktop/electron/appimage-payload-shared.mjs' import { stampAssertions } from '../tests/install/e2e-assets/mac-bundled-manifest.cjs' import { randomBytes } from 'node:crypto' import { buildStampPayload } from '../apps/desktop/scripts/write-build-stamp.mjs' @@ -259,9 +259,8 @@ test('verifyLinuxHeader and verifyLinuxMetadata enforce type-2 ELF arch, desktop cliName: 'hermes-canary', payloadKey: payloadCopyKey(stamp), }) - expect(linuxPayloadCopyKey(stamp)).toBe(payloadCopyKey(stamp)) - expect(linuxPayloadCopyKey({ ...stamp, source: 'fallback' })).toBeNull() - expect(linuxPayloadCopyKey({ ...stamp, commit: '0'.repeat(40) })).toBeNull() + expect(payloadCopyKey({ ...stamp, source: 'fallback' })).toBeNull() + expect(payloadCopyKey({ ...stamp, commit: '0'.repeat(40) })).toBeNull() expect(() => verifyLinuxMetadata(desktop.replace(expected.appId, 'com.wrong.app'), stamp, { commit, tag })).toThrow('StartupWMClass') expect(() => verifyLinuxMetadata(desktop.replace(version, '9.9.9'), stamp, { commit, tag })).toThrow('X-AppImage-Version') diff --git a/tests-js/scripts/desktop-smoke-process.ts b/tests-js/scripts/desktop-smoke-process.ts index f9d9d3ef9e..5be4e976d8 100644 --- a/tests-js/scripts/desktop-smoke-process.ts +++ b/tests-js/scripts/desktop-smoke-process.ts @@ -115,31 +115,6 @@ export function readBundledBundleEnv(root: string): Record String.fromCharCode(Number.parseInt(octal, 8))) -} - -/** The filesystem type of the innermost mount holding `target`, from /proc/self/mountinfo. */ -export function linuxMountTypeFor(target: string, mountinfo: string): string | null { - let best: { point: string; type: string } | null = null - - for (const line of mountinfo.split('\n')) { - const [mount, filesystem] = line.split(' - ') - const point = unescapeMountPath(mount?.split(' ')[4] ?? '') - const relative = path.posix.relative(point, target) - - if (!filesystem || !point || relative.startsWith('..') || path.posix.isAbsolute(relative)) { - continue - } - - if (!best || point.length >= best.point.length) { - best = { point, type: filesystem.split(' ')[0] } - } - } - - return best?.type ?? null -} - const windowsProcesses = z.array(z.object({ ProcessId: z.number(), ParentProcessId: z.number(), ExecutablePath: z.string().nullable(), CommandLine: z.string().nullable(), })) diff --git a/tests-js/scripts/desktop-smoke.test.ts b/tests-js/scripts/desktop-smoke.test.ts index cac2566337..d10d9b4882 100644 --- a/tests-js/scripts/desktop-smoke.test.ts +++ b/tests-js/scripts/desktop-smoke.test.ts @@ -6,13 +6,13 @@ import path from 'node:path' import yaml from 'js-yaml' import { expect, test } from 'vitest' -import { mountTypeFor } from '../../apps/desktop/electron/appimage-payload.ts' +import { mountTypeFor } from '../../apps/desktop/electron/appimage-payload-shared.mjs' import { candidateSmokeHermesHomes, predictSmokeHermesHome, resolveSmokeLaunch, runInstalledDesktopSmoke, smokeEnvironment } from '../../tests/install/e2e-assets/desktop-smoke.ts' import { sourceRuntimeSettleCommand } from '../../tests/install/e2e-assets/source-runtime-settle.mjs' import { assertUpdateWindowBackendOrigin, assertUpdateWindowProcess } from '../../tests/install/e2e-assets/update-window-chat.mjs' import { assertChatCommit, newCompletedPair, readMockPrompts, type TranscriptMessage } from './desktop-chat-smoke.ts' -import { assertBackendOrigin, linuxMountTypeFor, localBackendProcess, readBundledBundleEnv, readInstallationCommit } from './desktop-smoke-process.ts' +import { assertBackendOrigin, localBackendProcess, readBundledBundleEnv, readInstallationCommit } from './desktop-smoke-process.ts' import { writeEnvFile, writeMockProviderConfig } from './mock-provider-config.ts' import { MOCK_REPLY, startMockServer } from './mock-server.ts' @@ -829,17 +829,15 @@ test('re-writing the same provider config is byte-identical', (): void => { } finally { fs.rmSync(home, { recursive: true, force: true }) } }) -test('linuxMountTypeFor matches appimage-payload mountTypeFor and relaunch reuses only the run home', async (): Promise => { +test('shared mountTypeFor handles escaped mountinfo paths and relaunch reuses only the run home', async (): Promise => { const mountinfo = [ '22 1 0:21 / / rw,relatime - ext4 /dev/vda1 rw', '99 22 0:55 / /tmp/.mount_Hermes\\040Test ro,nosuid - fuse.HermesBundled HermesBundled ro', ].join('\n') - for (const target of ['/tmp/.mount_Hermes Test/resources', '/home/user/bundles', '/tmp/.mount_Hermes']) { - expect(linuxMountTypeFor(target, mountinfo)).toBe(mountTypeFor(target, mountinfo)) - } - - expect(linuxMountTypeFor('/tmp/.mount_Hermes Test/resources', mountinfo)).toBe('fuse.HermesBundled') + expect(mountTypeFor('/tmp/.mount_Hermes Test/resources', mountinfo)).toBe('fuse.HermesBundled') + expect(mountTypeFor('/home/user/bundles', mountinfo)).toBe('ext4') + expect(mountTypeFor('/tmp/.mount_Hermes', mountinfo)).toBe('ext4') const root = fs.mkdtempSync(path.join(os.tmpdir(), 'smoke-relaunch-')) const home = path.join(root, 'home') diff --git a/tests/install/e2e-assets/bundle-smoke-metadata.mjs b/tests/install/e2e-assets/bundle-smoke-metadata.mjs index dcc9017a9a..07d53328e5 100644 --- a/tests/install/e2e-assets/bundle-smoke-metadata.mjs +++ b/tests/install/e2e-assets/bundle-smoke-metadata.mjs @@ -5,6 +5,7 @@ import path from 'node:path' import { execFileSync, spawnSync } from 'node:child_process' import { parseArgs } from 'node:util' import { fileURLToPath } from 'node:url' +import { payloadCopyKey } from '../../../apps/desktop/electron/appimage-payload-shared.mjs' import { COMMIT_TAG_RE, OUT_OF_STORE_PUBLISHER, channelBuildRequest, releaseVersion } from '../../../scripts/msix-shared.mjs' import { codesignTeam, channelStampAssertions } from './mac-bundled-manifest.cjs' @@ -146,15 +147,6 @@ export function verifyLinuxHeader(header, arch) { } } -export function linuxPayloadCopyKey(stamp) { - const commit = typeof stamp?.commit === 'string' ? stamp.commit.trim() : '' - if (!commit || !stamp?.builtAt || stamp.source === 'fallback' || commit === 'unknown' || /^0+$/.test(commit)) { - return null - } - const version = stamp.displayVersion ? `${stamp.displayVersion}-` : '' - return `${version}${commit.slice(0, 12)}-${stamp.builtAt}`.replace(/[^\w.+-]/g, '') -} - function parseDesktopEntry(text) { const entries = {} let inMain = false @@ -184,7 +176,7 @@ export function verifyLinuxMetadata(desktopText, stamp, { commit, tag = '', chan || hermesCommand.split('/').includes('..')) { throw new Error('Invalid stamp.runtime.commands.hermes') } - const payloadKey = linuxPayloadCopyKey(stamp) + const payloadKey = payloadCopyKey(stamp) if (!payloadKey) throw new Error('Install stamp cannot produce a stable AppImage payload copy key') return { version, bundleIdentifier: expected.appId, cliName: path.posix.basename(hermesCommand), payloadKey } } diff --git a/tests/install/e2e-assets/desktop-smoke.ts b/tests/install/e2e-assets/desktop-smoke.ts index e2000ab523..a64ee83090 100644 --- a/tests/install/e2e-assets/desktop-smoke.ts +++ b/tests/install/e2e-assets/desktop-smoke.ts @@ -6,10 +6,11 @@ import path from 'node:path' import { pathToFileURL } from 'node:url' import { parseArgs } from 'node:util' +import { mountTypeFor } from '../../../apps/desktop/electron/appimage-payload-shared.mjs' import { resolveDesktopHermesHome } from '../../../apps/desktop/electron/data-paths.mjs' import { applyBundleEnvironment } from '../../../apps/desktop/scripts/bundle-env.mjs' import { type ChatIdentity, readChatIdentity, runDesktopChatSmoke, waitForChatReady } from '../../../tests-js/scripts/desktop-chat-smoke.ts' -import { assertBackendOrigin, linuxMountTypeFor, localBackendProcess, readBundledBundleEnv, readBundledBundleEnvFromStamp, readInstallationCommit, reusedDesktopBuildCommit } from '../../../tests-js/scripts/desktop-smoke-process.ts' +import { assertBackendOrigin, localBackendProcess, readBundledBundleEnv, readBundledBundleEnvFromStamp, readInstallationCommit, reusedDesktopBuildCommit } from '../../../tests-js/scripts/desktop-smoke-process.ts' import { _electron, type ElectronApplication, type Page, z } from '../../../tests-js/scripts/e2e-driver-deps.ts' import { validateMockUrl, writeEnvFile, writeMockProviderConfig } from '../../../tests-js/scripts/mock-provider-config.ts' import { type MockServer, startMockServer } from '../../../tests-js/scripts/mock-server.ts' @@ -249,7 +250,7 @@ function verifyAppImageDesktop(running: RunningDesktop, options: SmokeOptions): if (!running.appDir || !within(running.appDir, running.executable) || !within(running.appDir, running.resources)) { throw new Error('Running Electron executable or resources escaped the AppImage mount') } - const mountType = linuxMountTypeFor(fs.realpathSync(running.resources), fs.readFileSync('/proc/self/mountinfo', 'utf8')) + const mountType = mountTypeFor(fs.realpathSync(running.resources), fs.readFileSync('/proc/self/mountinfo', 'utf8')) if (!mountType || !/^fuse(\.|blk$|$)/.test(mountType)) { throw new Error(`Expected a FUSE-mounted AppImage, got mount type ${mountType ?? '(none)'}`) }