refactor(desktop): share AppImage payloadCopyKey and mountTypeFor helpers with smoke driver
This commit is contained in:
committed by
Bear Huddleston
parent
4e812017de
commit
61c66f741d
10
apps/desktop/electron/appimage-payload-shared.d.mts
Normal file
10
apps/desktop/electron/appimage-payload-shared.d.mts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface PayloadKeyStamp {
|
||||
commit?: string | null
|
||||
builtAt?: string | null
|
||||
source?: string | null
|
||||
displayVersion?: string | null
|
||||
}
|
||||
|
||||
export function payloadCopyKey(stamp: Readonly<PayloadKeyStamp> | null): string | null
|
||||
|
||||
export function mountTypeFor(target: string, mountinfo: string): string | null
|
||||
41
apps/desktop/electron/appimage-payload-shared.mjs
Normal file
41
apps/desktop/electron/appimage-payload-shared.mjs
Normal file
@@ -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
|
||||
}
|
||||
@@ -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> | 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<InstallStamp> | 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<RunnableResourcesHost, 'env' | 'home' | 'resourcesPath' | 'stamp' | 'log'>,
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -115,31 +115,6 @@ export function readBundledBundleEnv(root: string): Record<string, string | null
|
||||
return readBundledBundleEnvFromStamp(path.join(root, '..', 'install-stamp.json'))
|
||||
}
|
||||
|
||||
function unescapeMountPath(value: string): string {
|
||||
return value.replace(/\\([0-7]{3})/g, (_match: string, octal: string): string => 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(),
|
||||
}))
|
||||
|
||||
@@ -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<void> => {
|
||||
test('shared mountTypeFor handles escaped mountinfo paths and relaunch reuses only the run home', async (): Promise<void> => {
|
||||
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')
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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)'}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user