Skip to content

The agent that grows with you

fix(desktop): honour updates.pre_update_backup=off in the Desktop updater preflight

fangliquan4de06d1dbf7b
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index b83d2fcf02b..70fbac573fb 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -328,6 +328,7 @@ import { createPoolStopper } from './pool-stop'
import { poolTouchKeys } from './pool-touch-scope'
import { createPortalSession } from './portal-session'
import { createKeepAwake } from './power-save'
+import { readPreUpdateBackupEnabled } from './pre-update-backup-config'
import { capturePreviewContents } from './preview-capture'
import { PreviewReachRegistry } from './preview-reach'
import {
@@ -4233,7 +4234,7 @@ async function applyUpdates(opts: { stopSafeBlockers?: boolean } = {}) {
// ── Pre-flight state.db integrity guard (#68474) ─────────────────
// Emergency backup and header verification before the update touches
// anything. Runs while the backend is still alive.
- preflightStateDb(HERMES_HOME, rememberLog)
+ await preflightStateDb(HERMES_HOME, rememberLog)
if (IS_WINDOWS && resolveUpdateScriptHandoff(updateRoot)) {
const message = windowsUpdatePrerequisiteError(updateRoot)
@@ -4621,7 +4622,7 @@ function runningAppBundle() {
// desktop Electron process itself, before the backend is killed and
// before the updater is spawned — a separate safety net from the
// Python-level pre-update snapshot inside `hermes update`.
-function preflightStateDb(hermesHome, rememberLog) {
+async function preflightStateDb(hermesHome, rememberLog) {
const stateDbPath = path.join(hermesHome, 'state.db')
if (!fileExists(stateDbPath)) {
@@ -4655,6 +4656,17 @@ function preflightStateDb(hermesHome, rememberLog) {
)
}
+ if (
+ !(await readPreUpdateBackupEnabled(
+ resolveHermesBackend(['config', 'get', 'updates.pre_update_backup', '--json']),
+ hermesHome
+ ))
+ ) {
+ rememberLog('[updates] emergency state.db backup disabled by updates.pre_update_backup')
+
+ return
+ }
+
// Emergency timestamped backup, separate from the Python-level snapshot.
const ts = new Date().toISOString().replace(/[:.]/g, '-')
@@ -4729,7 +4741,7 @@ async function applyUpdatesPosixHandoff(opts: any) {
}
// ── Pre-flight state.db integrity guard (#68474) ──
- preflightStateDb(HERMES_HOME, rememberLog)
+ await preflightStateDb(HERMES_HOME, rememberLog)
// Branch-pin so a non-main checkout doesn't get switched to main (and
// self-heal to main when the pinned branch no longer exists on origin).
diff --git a/apps/desktop/electron/pre-update-backup-config.test.ts b/apps/desktop/electron/pre-update-backup-config.test.ts
new file mode 100644
index 00000000000..9d2ead15fa2
--- /dev/null
+++ b/apps/desktop/electron/pre-update-backup-config.test.ts
@@ -0,0 +1,61 @@
+import { describe, expect, it, vi } from 'vitest'
+
+import { preUpdateBackupEnabled, readPreUpdateBackupEnabled } from './pre-update-backup-config'
+
+describe('preUpdateBackupEnabled', () => {
+ it.each([false, null, 'off', 'false', 'none', 'disabled', ' DISABLED '])(
+ 'disables the desktop backup for the Python updater off alias %j',
+ value => {
+ expect(preUpdateBackupEnabled(value)).toBe(false)
+ }
+ )
+
+ it.each([true, 'quick', 'full', 'zip', 'true', 'unexpected', 0, {}, undefined])(
+ 'keeps the safety backup for %j',
+ value => {
+ expect(preUpdateBackupEnabled(value)).toBe(true)
+ }
+ )
+})
+
+describe('readPreUpdateBackupEnabled', () => {
+ it.each([
+ ['managed false overriding user true', 'false', false],
+ ['managed true overriding user false', 'true', true],
+ ['an environment-expanded off alias', '"off"', false]
+ ])('uses the effective config result for %s', async (_name, stdout, expected) => {
+ const run = vi.fn().mockResolvedValue({ stdout })
+ const runtime = {
+ command: '/runtime/python',
+ args: ['-m', 'hermes_cli.main', 'config', 'get', 'updates.pre_update_backup', '--json'],
+ env: { PYTHONPATH: '/runtime/hermes' }
+ }
+
+ await expect(readPreUpdateBackupEnabled(runtime, '/profiles/active', run)).resolves.toBe(expected)
+ expect(run).toHaveBeenCalledWith(
+ runtime.command,
+ runtime.args,
+ expect.objectContaining({
+ env: expect.objectContaining({
+ HERMES_HOME: '/profiles/active',
+ PYTHONPATH: '/runtime/hermes'
+ })
+ })
+ )
+ })
+
+ it('fails safe when runtime resolution fails', async () => {
+ await expect(
+ readPreUpdateBackupEnabled(Promise.reject(new Error('resolver failed')), '/profiles/active', vi.fn())
+ ).resolves.toBe(true)
+ })
+
+ it.each([
+ ['runtime failure', vi.fn().mockRejectedValue(new Error('probe failed'))],
+ ['malformed output', vi.fn().mockResolvedValue({ stdout: 'not-json' })]
+ ])('fails safe for %s', async (_name, run) => {
+ await expect(
+ readPreUpdateBackupEnabled({ command: '/runtime/hermes', args: ['config', 'get'] }, '/profiles/active', run)
+ ).resolves.toBe(true)
+ })
+})
diff --git a/apps/desktop/electron/pre-update-backup-config.ts b/apps/desktop/electron/pre-update-backup-config.ts
new file mode 100644
index 00000000000..1f8b7401f6f
--- /dev/null
+++ b/apps/desktop/electron/pre-update-backup-config.ts
@@ -0,0 +1,48 @@
+import { execFile } from 'node:child_process'
+import { promisify } from 'node:util'
+
+const DISABLED_BACKUP_MODES = new Set(['off', 'false', 'none', 'disabled'])
+const execFileAsync = promisify(execFile)
+
+export function preUpdateBackupEnabled(value: unknown): boolean {
+ if (value === false || value === null) {
+ return false
+ }
+
+ return typeof value !== 'string' || !DISABLED_BACKUP_MODES.has(value.trim().toLowerCase())
+}
+
+export interface HermesConfigRuntime {
+ command?: string
+ args?: string[]
+ env?: NodeJS.ProcessEnv
+ shell?: boolean
+}
+
+export async function readPreUpdateBackupEnabled(
+ runtime: HermesConfigRuntime | Promise<HermesConfigRuntime>,
+ hermesHome: string,
+ run = execFileAsync
+): Promise<boolean> {
+ try {
+ const resolvedRuntime = await runtime
+
+ if (!resolvedRuntime.command || !resolvedRuntime.args) {
+ return true
+ }
+
+ const result = await run(resolvedRuntime.command, resolvedRuntime.args, {
+ encoding: 'utf8',
+ env: { ...process.env, ...resolvedRuntime.env, HERMES_HOME: hermesHome },
+ shell: Boolean(resolvedRuntime.shell),
+ timeout: 15_000,
+ windowsHide: true
+ })
+
+ return preUpdateBackupEnabled(JSON.parse(String(result.stdout).trim()))
+ } catch {
+ // A missing runtime, probe failure, or malformed response must not weaken
+ // the emergency recovery path.
+ return true
+ }
+}