Skip to content

The agent that grows with you

fix(desktop): isolate pending relaunch markers by installation

ethernet77e35e1da30a
diff --git a/apps/desktop/electron/updater/app-installer-recovery.test.ts b/apps/desktop/electron/updater/app-installer-recovery.test.ts
index b091aadaa29..4ad859e6def 100644
--- a/apps/desktop/electron/updater/app-installer-recovery.test.ts
+++ b/apps/desktop/electron/updater/app-installer-recovery.test.ts
@@ -6,7 +6,7 @@ import path from 'node:path'
import { test } from 'vitest'
import { AppInstallerStrategy, type AppInstallerStrategyDeps } from './app-installer'
-import { PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch } from './relaunch'
+import { PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch, type RelaunchRegistration } from './relaunch'
for (const failureAt of ['prepare', 'register', 'teardown', 'open', 'none']) {
test(`App Installer restores only after teardown begins (${failureAt})`, async () => {
@@ -40,7 +40,7 @@ for (const failureAt of ['prepare', 'register', 'teardown', 'open', 'none']) {
return ''
}
},
- registerPendingRelaunch: version => registerUpdateRelaunch(home, version, {
+ registerPendingRelaunch: (version: string): Promise<RelaunchRegistration> => registerUpdateRelaunch({ getPath: (): string => home }, version, {
relaunch: async () => {
act('register')
@@ -86,7 +86,7 @@ test('handoff errors retain cleanup failures while still restoring the backend',
python: 'unused', script: 'unused.py', run: async () => ({ code: 0, stdout: '' }),
channel: 'stable', light: false, appVersion: '1.0', feedBaseUrl: 'https://example.invalid',
installer: { prepare: async () => 'file', open: async () => { throw original } },
- registerPendingRelaunch: version => registerUpdateRelaunch(home, version, {
+ registerPendingRelaunch: (version: string): Promise<RelaunchRegistration> => registerUpdateRelaunch({ getPath: (): string => home }, version, {
relaunch: async () => ({ cancel: async () => { order.push('cancel'); throw cancellation } })
}),
teardownBundledBackend: async () => { order.push('stop') },
diff --git a/apps/desktop/electron/updater/relaunch-isolation.test.ts b/apps/desktop/electron/updater/relaunch-isolation.test.ts
new file mode 100644
index 00000000000..80bc8041f85
--- /dev/null
+++ b/apps/desktop/electron/updater/relaunch-isolation.test.ts
@@ -0,0 +1,153 @@
+import assert from 'node:assert/strict'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+import type { App } from 'electron'
+import { afterEach, beforeEach, test, vi } from 'vitest'
+
+import {
+ consumePendingRelaunch,
+ PENDING_RELAUNCH_FILENAME,
+ registerUpdateRelaunch,
+ type RelaunchRegistration,
+ writePendingRelaunch
+} from './relaunch'
+import type { RelaunchWaiterHandle } from './relaunch-waiter'
+
+let root: string
+let hermesHome: string
+let stable: Pick<App, 'getPath'>
+let canary: Pick<App, 'getPath'>
+let commit: Pick<App, 'getPath'>
+
+function installation(name: string): Pick<App, 'getPath'> {
+ const userData: string = path.join(root, 'app-data', name)
+ fs.mkdirSync(userData, { recursive: true })
+
+ return {
+ getPath: (key: Parameters<App['getPath']>[0]): string => {
+ assert.equal(key, 'userData')
+
+ return userData
+ }
+ }
+}
+
+function marker(app: Pick<App, 'getPath'>): string {
+ return path.join(app.getPath('userData'), PENDING_RELAUNCH_FILENAME)
+}
+
+beforeEach((): void => {
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'relaunch-isolation-'))
+ hermesHome = path.join(root, 'hermes-home')
+ fs.mkdirSync(hermesHome)
+ vi.stubEnv('HERMES_HOME', hermesHome)
+ stable = installation('stable')
+ canary = installation('canary')
+ commit = installation('commit')
+})
+
+afterEach((): void => {
+ vi.unstubAllEnvs()
+ fs.rmSync(root, { recursive: true, force: true })
+})
+
+test('writes and startup consumption stay in each installation despite a shared Hermes home', async (): Promise<void> => {
+ const globalMarker: string = path.join(hermesHome, PENDING_RELAUNCH_FILENAME)
+ const legacyContents: string = JSON.stringify({ schemaVersion: 1, fromVersion: 'legacy', startedAt: 1 })
+ fs.writeFileSync(globalMarker, legacyContents)
+
+ assert.equal(writePendingRelaunch(stable, 'stable-old'), true)
+ const stableContents: string = fs.readFileSync(marker(stable), 'utf8')
+
+ const registration: RelaunchRegistration = await registerUpdateRelaunch(canary, 'canary-old', {
+ relaunch: (): undefined => undefined
+ })
+
+ assert.equal(registration.automatic, false)
+ const canaryContents: string = fs.readFileSync(marker(canary), 'utf8')
+
+ assert.deepEqual(consumePendingRelaunch(commit, 'commit-build'), { wasUpdateRelaunch: false })
+ assert.equal(fs.readFileSync(marker(stable), 'utf8'), stableContents)
+ assert.equal(fs.readFileSync(marker(canary), 'utf8'), canaryContents)
+ assert.deepEqual(consumePendingRelaunch(canary, 'canary-new'), {
+ wasUpdateRelaunch: true,
+ fromVersion: 'canary-old'
+ })
+ assert.equal(fs.existsSync(marker(canary)), false)
+ assert.equal(fs.readFileSync(marker(stable), 'utf8'), stableContents)
+ assert.deepEqual(consumePendingRelaunch(canary, 'canary-new'), { wasUpdateRelaunch: false })
+ assert.equal(writePendingRelaunch(canary, 'canary-new'), true)
+ const nextCanaryContents: string = fs.readFileSync(marker(canary), 'utf8')
+ assert.deepEqual(consumePendingRelaunch(stable, 'stable-old'), {
+ wasUpdateRelaunch: false,
+ fromVersion: 'stable-old'
+ })
+ assert.equal(fs.existsSync(marker(stable)), false)
+ assert.equal(fs.readFileSync(marker(canary), 'utf8'), nextCanaryContents)
+ assert.equal(fs.readFileSync(globalMarker, 'utf8'), legacyContents)
+})
+
+test('cancellation and failed registration remove only their own installation marker', async (): Promise<void> => {
+ const survivor: RelaunchRegistration = await registerUpdateRelaunch(stable, 'stable-old', {
+ relaunch: (): undefined => undefined
+ })
+
+ const stableContents: string = fs.readFileSync(marker(stable), 'utf8')
+ let cancelled: number = 0
+
+ for (const automatic of [true, false]) {
+ const registration: RelaunchRegistration = await registerUpdateRelaunch(canary, 'canary-old', {
+ relaunch: (): RelaunchWaiterHandle | undefined =>
+ automatic
+ ? {
+ cancel: async (): Promise<void> => {
+ cancelled++
+ }
+ }
+ : undefined
+ })
+
+ assert.equal(fs.existsSync(marker(canary)), true)
+ await Promise.all([registration.cancel(), registration.cancel()])
+ assert.equal(fs.existsSync(marker(canary)), false)
+ assert.equal(fs.readFileSync(marker(stable), 'utf8'), stableContents)
+ }
+
+ assert.equal(cancelled, 1)
+ const failure: Error = new Error('waiter failed')
+ await assert.rejects(
+ registerUpdateRelaunch(canary, 'canary-old', {
+ relaunch: async (): Promise<never> => {
+ throw failure
+ }
+ }),
+ (error: unknown): boolean => error === failure
+ )
+ assert.equal(fs.existsSync(marker(canary)), false)
+ assert.equal(fs.readFileSync(marker(stable), 'utf8'), stableContents)
+
+ const failedCancellation: RelaunchRegistration = await registerUpdateRelaunch(canary, 'canary-old', {
+ relaunch: (): RelaunchWaiterHandle => ({
+ cancel: async (): Promise<never> => {
+ throw failure
+ }
+ })
+ })
+
+ await assert.rejects(failedCancellation.cancel(), AggregateError)
+ assert.equal(fs.existsSync(marker(canary)), false)
+ assert.equal(fs.readFileSync(marker(stable), 'utf8'), stableContents)
+ assert.equal(fs.existsSync(path.join(hermesHome, PENDING_RELAUNCH_FILENAME)), false)
+
+ const sibling: RelaunchRegistration = await registerUpdateRelaunch(canary, 'canary-old', {
+ relaunch: (): undefined => undefined
+ })
+
+ const siblingContents: string = fs.readFileSync(marker(canary), 'utf8')
+ await survivor.cancel()
+ assert.equal(fs.existsSync(marker(stable)), false)
+ assert.equal(fs.readFileSync(marker(canary), 'utf8'), siblingContents)
+ await sibling.cancel()
+})
diff --git a/apps/desktop/electron/updater/relaunch-registration.test.ts b/apps/desktop/electron/updater/relaunch-registration.test.ts
index 46d4f7b1b8c..da2aa112c88 100644
--- a/apps/desktop/electron/updater/relaunch-registration.test.ts
+++ b/apps/desktop/electron/updater/relaunch-registration.test.ts
@@ -5,7 +5,7 @@ import path from 'node:path'
import { test } from 'vitest'
-import { PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch } from './relaunch'
+import { PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch, type RelaunchRegistration } from './relaunch'
import type { RelaunchWaiterHandle } from './relaunch-waiter'
for (const automatic of [true, false]) {
@@ -18,7 +18,7 @@ for (const automatic of [true, false]) {
let registered = false
try {
- const pending = registerUpdateRelaunch(home, '1.0', { relaunch: () => ready })
+ const pending: Promise<RelaunchRegistration> = registerUpdateRelaunch({ getPath: (): string => home }, '1.0', { relaunch: (): Promise<RelaunchWaiterHandle | undefined> => ready })
.then(result => { registered = true;
return result })
@@ -46,14 +46,14 @@ test('start and cancellation failures preserve the cause and still clean owned m
const failure = new Error('owned child refused to stop')
try {
- await assert.rejects(registerUpdateRelaunch(home, '1.0', {
+ await assert.rejects(registerUpdateRelaunch({ getPath: (): string => home }, '1.0', {
relaunch: async () => { throw failure }
}), error => error === failure)
assert.equal(fs.existsSync(marker), false)
let attempts = 0
- const registration = await registerUpdateRelaunch(home, '1.0', {
+ const registration: RelaunchRegistration = await registerUpdateRelaunch({ getPath: (): string => home }, '1.0', {
relaunch: async () => ({ cancel: async () => { attempts++; throw failure } })
})
@@ -62,7 +62,7 @@ test('start and cancellation failures preserve the cause and still clean owned m
assert.equal(attempts, 1)
assert.equal(fs.existsSync(marker), false)
- const blocked = await registerUpdateRelaunch(home, '1.0', {
+ const blocked: RelaunchRegistration = await registerUpdateRelaunch({ getPath: (): string => home }, '1.0', {
relaunch: async () => ({ cancel: async () => { throw failure } })
})
diff --git a/apps/desktop/electron/updater/relaunch.ts b/apps/desktop/electron/updater/relaunch.ts
index 7534c5a428c..2178cff1a8c 100644
--- a/apps/desktop/electron/updater/relaunch.ts
+++ b/apps/desktop/electron/updater/relaunch.ts
@@ -1,3 +1,4 @@
+// Electron userData isolates installations that share HERMES_HOME.
// The marker survives a package swap and records the version that started it.
// The separate waiter owns automatic relaunch. Cancelling an update stops
// that waiter and removes its marker, including when startup stays manual.
@@ -5,6 +6,8 @@
import * as fs from 'node:fs'
import * as path from 'node:path'
+import type { App } from 'electron'
+
import type { RelaunchWaiterHandle } from './relaunch-waiter'
export interface PendingRelaunchMarker {
@@ -17,8 +20,8 @@ export interface PendingRelaunchMarker {
const MARKER_FILENAME = 'pending-update-relaunch.json'
-function markerPath(hermesHome: string): string {
- return path.join(hermesHome, MARKER_FILENAME)
+function markerPath(app: Pick<App, 'getPath'>): string {
+ return path.join(app.getPath('userData'), MARKER_FILENAME)
}
/** Pure: the filename constant, for tests. */
@@ -29,13 +32,13 @@ export const PENDING_RELAUNCH_FILENAME = MARKER_FILENAME
* to write never blocks the update; relaunch just stays manual.
*/
export function writePendingRelaunch(
- hermesHome: string,
+ app: Pick<App, 'getPath'>,
fromVersion: string,
- writeFile: (file: string, contents: string) => void = (f, c) => fs.writeFileSync(f, c)
+ writeFile: (file: string, contents: string) => void = (f: string, c: string): void => fs.writeFileSync(f, c)
): boolean {
try {
const marker: PendingRelaunchMarker = { schemaVersion: 1, fromVersion, startedAt: Date.now() }
- writeFile(markerPath(hermesHome), JSON.stringify(marker))
+ writeFile(markerPath(app), JSON.stringify(marker))
return true
} catch {
@@ -55,20 +58,24 @@ export interface RelaunchRegistration {
/** Marker failure permits an update, but failed waiter cleanup must abort it. */
export async function registerUpdateRelaunch(
- hermesHome: string,
+ app: Pick<App, 'getPath'>,
fromVersion: string,
deps: UpdateRelaunchDeps,
- writeFile: (file: string, contents: string) => void = (f, c) => fs.writeFileSync(f, c)
+ writeFile: (file: string, contents: string) => void = (f: string, c: string): void => fs.writeFileSync(f, c)
): Promise<RelaunchRegistration> {
- const ownsMarker = writePendingRelaunch(hermesHome, fromVersion, writeFile)
+ const ownsMarker: boolean = writePendingRelaunch(app, fromVersion, writeFile)
const removeMarker = (): void => {
- if (!ownsMarker) { return }
+ if (!ownsMarker) {
+ return
+ }
try {
- fs.unlinkSync(markerPath(hermesHome))
+ fs.unlinkSync(markerPath(app))
} catch (error) {
- if ((error as NodeJS.ErrnoException).code !== 'ENOENT') { throw error }
+ if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
+ throw error
+ }
}
}
@@ -106,7 +113,9 @@ export async function registerUpdateRelaunch(
errors.push(error)
}
- if (errors.length) { throw new AggregateError(errors, 'Relaunch cancellation failed') }
+ if (errors.length) {
+ throw new AggregateError(errors, 'Relaunch cancellation failed')
+ }
})()
return cancellation
@@ -134,7 +143,7 @@ export interface RelaunchFsDeps {
* (cancelled/failed OS install), so the marker is deleted and no toast fires.
*/
export function consumePendingRelaunch(
- hermesHome: string,
+ app: Pick<App, 'getPath'>,
currentVersion: string,
deps: RelaunchFsDeps = {}
): ConsumedRelaunch {
@@ -142,7 +151,7 @@ export function consumePendingRelaunch(
const readFileSync = deps.readFileSync ?? ((file: string) => fs.readFileSync(file, 'utf8'))
const unlinkSync = deps.unlinkSync ?? ((file: string) => fs.unlinkSync(file))
- const file = markerPath(hermesHome)
+ const file: string = markerPath(app)
if (!existsSync(file)) {
return { wasUpdateRelaunch: false }
diff --git a/apps/desktop/electron/updater/updater.test.ts b/apps/desktop/electron/updater/updater.test.ts
index 750f1ee302e..ea04bff9f56 100644
--- a/apps/desktop/electron/updater/updater.test.ts
+++ b/apps/desktop/electron/updater/updater.test.ts
@@ -7,7 +7,7 @@ import { buildStampPayload } from '../../scripts/write-build-stamp.mjs'
import { appInstallerCheckToStatus, parseCheckOutput } from './app-installer'
import { buildManualUpdateCommand } from './checkout'
-import { consumePendingRelaunch, PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch, writePendingRelaunch } from './relaunch'
+import { type ConsumedRelaunch, consumePendingRelaunch, PENDING_RELAUNCH_FILENAME, registerUpdateRelaunch, type RelaunchRegistration, writePendingRelaunch } from './relaunch'
import { resolveUpdaterMechanism } from './index'
@@ -123,10 +123,10 @@ describe('pending relaunch marker', () => {
it('write then consume on a different version = update relaunch', () => {
const files: Record<string, string> = {}
- expect(writePendingRelaunch('/home', '0.18.2', (f, c) => { files[f] = c as string })).toBe(true)
+ expect(writePendingRelaunch({ getPath: (): string => '/home' }, '0.18.2', (f: string, c: string): void => { files[f] = c })).toBe(true)
expect(JSON.parse(Object.values(files)[0]).fromVersion).toBe('0.18.2')
- const r = consumePendingRelaunch('/home', '0.18.3', fakeFs(files))
+ const r: ConsumedRelaunch = consumePendingRelaunch({ getPath: (): string => '/home' }, '0.18.3', fakeFs(files))
expect(r.wasUpdateRelaunch).toBe(true)
expect(r.fromVersion).toBe('0.18.2')
// one-shot: consumed
@@ -135,25 +135,25 @@ describe('pending relaunch marker', () => {
it('same version = update never landed; marker consumed silently', () => {
const files: Record<string, string> = {}
- writePendingRelaunch('/home', '0.18.2', (f, c) => { files[f] = c as string })
+ writePendingRelaunch({ getPath: (): string => '/home' }, '0.18.2', (f: string, c: string): void => { files[f] = c })
- const r = consumePendingRelaunch('/home', '0.18.2', fakeFs(files))
+ const r: ConsumedRelaunch = consumePendingRelaunch({ getPath: (): string => '/home' }, '0.18.2', fakeFs(files))
expect(r.wasUpdateRelaunch).toBe(false)
expect(Object.keys(files)).toHaveLength(0)
})
it('no marker = normal launch', () => {
- expect(consumePendingRelaunch('/home', '0.18.3', fakeFs({})).wasUpdateRelaunch).toBe(false)
+ expect(consumePendingRelaunch({ getPath: (): string => '/home' }, '0.18.3', fakeFs({})).wasUpdateRelaunch).toBe(false)
})
it('corrupt marker is consumed and treated as unknown', () => {
const files: Record<string, string> = {}
- writePendingRelaunch('/home', '0.18.2', (f, c) => { files[f] = c as string })
+ writePendingRelaunch({ getPath: (): string => '/home' }, '0.18.2', (f: string, c: string): void => { files[f] = c })
// corrupt the contents under the same key
for (const k of Object.keys(files)) {files[k] = 'not json'}
- const r = consumePendingRelaunch('/home', '0.18.3', fakeFs(files))
+ const r: ConsumedRelaunch = consumePendingRelaunch({ getPath: (): string => '/home' }, '0.18.3', fakeFs(files))
expect(r.wasUpdateRelaunch).toBe(false)
expect(Object.keys(files)).toHaveLength(0)
})
@@ -164,13 +164,13 @@ describe('registerUpdateRelaunch — the mechanism, not just the marker', () =>
const files: Record<string, string> = {}
let started = 0
- const ok = await registerUpdateRelaunch(
- '/home',
+ const ok: RelaunchRegistration = await registerUpdateRelaunch(
+ { getPath: (): string => '/home' },
'0.18.2',
{ relaunch: () => { started += 1;
return { cancel: async () => {} } } },
- (f, c) => { files[f] = c as string }
+ (f: string, c: string): void => { files[f] = c }
)
expect(ok.automatic).toBe(true)
@@ -181,11 +181,11 @@ describe('registerUpdateRelaunch — the mechanism, not just the marker', () =>
it('retains the marker when a safely failed waiter requires manual relaunch', async () => {
const files: Record<string, string> = {}
- const ok = await registerUpdateRelaunch(
- '/home',
+ const ok: RelaunchRegistration = await registerUpdateRelaunch(
+ { getPath: (): string => '/home' },
'0.18.2',
{ relaunch: async () => undefined },
- (f, c) => { files[f] = c as string }
+ (f: string, c: string): void => { files[f] = c }
)
expect(ok.automatic).toBe(false)