Skip to content

The agent that grows with you

fix(desktop): decode-probe app icon candidates instead of existence-only (#94806)

Leon Phullf0187332d136
diff --git a/apps/desktop/electron/app-icon.test.ts b/apps/desktop/electron/app-icon.test.ts
new file mode 100644
index 00000000000..1351f324ac2
--- /dev/null
+++ b/apps/desktop/electron/app-icon.test.ts
@@ -0,0 +1,112 @@
+import assert from 'node:assert/strict'
+import fs from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+
+import { test } from 'vitest'
+
+import { appIconCandidates, decodingFileProbe, resolveAppIcon } from './app-icon'
+
+// Regression: a packaged app.asar can contain a TRUNCATED apple-touch-icon.png
+// (interrupted electron-builder run, partial copy). Electron's
+// BrowserWindow({ icon }) / app.dock.setIcon() decode synchronously and THROW
+// on undecodable bytes, which killed the main process inside createWindow()
+// and took the app down mid-session. Icon resolution must fail soft: skip a
+// candidate that exists but does not decode, exactly like a missing one.
+
+test('resolveAppIcon skips an existing but undecodable candidate', () => {
+ // First candidate "exists" (probe says true) but does not decode; second
+ // decodes. The resolver must return the second, not the first.
+ const probeCalls: string[] = []
+
+ const probe = (p: string) => {
+ probeCalls.push(p)
+
+ return p !== '/packaged/app.asar/public/apple-touch-icon.png'
+ }
+
+ const picked = resolveAppIcon(
+ ['/packaged/app.asar/public/apple-touch-icon.png', '/packaged/app.asar/dist/apple-touch-icon.png'],
+ probe
+ )
+
+ assert.equal(picked, '/packaged/app.asar/dist/apple-touch-icon.png')
+ assert.deepEqual(probeCalls, [
+ '/packaged/app.asar/public/apple-touch-icon.png',
+ '/packaged/app.asar/dist/apple-touch-icon.png'
+ ])
+})
+
+test('resolveAppIcon returns undefined when every candidate fails the probe', () => {
+ const picked = resolveAppIcon(['/a.png', '/b.ico'], () => false)
+ assert.equal(picked, undefined)
+})
+
+test('resolveAppIcon returns the first candidate that passes the probe', () => {
+ const picked = resolveAppIcon(['/a.png', '/b.png'], () => true)
+ assert.equal(picked, '/a.png')
+})
+
+test('decodingFileProbe rejects a missing file', () => {
+ const missing = path.join(os.tmpdir(), `hermes-icon-missing-${process.pid}.png`)
+ assert.equal(decodingFileProbe(missing), false)
+})
+
+test('decodingFileProbe rejects an existing but empty (0-byte) file', () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-icon-'))
+ const empty = path.join(dir, 'apple-touch-icon.png')
+ fs.writeFileSync(empty, Buffer.alloc(0))
+
+ try {
+ // 0 bytes exist but decode to an empty image — and without electron in
+ // the test runtime the require itself fails. Both paths must be false.
+ assert.equal(decodingFileProbe(empty), false)
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+})
+
+test('decodingFileProbe rejects a directory', () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-icon-dir-'))
+
+ try {
+ assert.equal(decodingFileProbe(dir), false)
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+})
+
+test('appIconCandidates keeps the documented precedence ladder', () => {
+ const mac = appIconCandidates({
+ isWindows: false,
+ appRoot: '/Applications/Hermes.app/Contents/Resources',
+ unpackedPathFor: p => `${p}.unpacked`
+ })
+
+ assert.deepEqual(mac, [
+ path.join('/Applications/Hermes.app/Contents/Resources', 'public', 'apple-touch-icon.png'),
+ path.join('/Applications/Hermes.app/Contents/Resources', 'dist', 'apple-touch-icon.png'),
+ path.join('/Applications/Hermes.app/Contents/Resources.unpacked', 'dist', 'apple-touch-icon.png')
+ ])
+
+ // Windows prepends the two full-bleed .ico rungs ahead of the PNG ladder.
+ const win = appIconCandidates({
+ isWindows: true,
+ appRoot: 'C:\\app',
+ resourcesPath: 'C:\\resources',
+ unpackedPathFor: p => `${p}\\unpacked`
+ })
+
+ assert.equal(win.length, 5)
+ assert.equal(win.filter(c => c.endsWith('.ico')).length, 2)
+ assert.equal(
+ win[0],
+ path.join('C:\\resources', 'icon.ico'),
+ 'resources/ icon.ico is the highest-precedence Windows rung'
+ )
+ assert.equal(
+ win.filter(c => c.endsWith('apple-touch-icon.png')).length,
+ 3,
+ 'all three PNG rungs remain after the ico rungs'
+ )
+})
diff --git a/apps/desktop/electron/app-icon.ts b/apps/desktop/electron/app-icon.ts
new file mode 100644
index 00000000000..975638a32a6
--- /dev/null
+++ b/apps/desktop/electron/app-icon.ts
@@ -0,0 +1,85 @@
+import fs from 'node:fs'
+import path from 'node:path'
+
+import { nativeImage } from 'electron'
+
+/**
+ * Validate that a candidate app-icon file exists and decodes as an image.
+ *
+ * Electron's `new BrowserWindow({ icon })` and `app.dock.setIcon()` decode the
+ * file synchronously on the main process and THROW when the bytes are not a
+ * decodable image — `statSync().isFile()` only proves the file exists, not that
+ * it decodes. A truncated or zero-byte PNG inside a packaged `app.asar` (e.g.
+ * interrupted electron-builder run) therefore killed the main process inside
+ * `createWindow()` and took the whole app down mid-session: the window never
+ * appeared, running turns lost their renderer, and the desktop log showed
+ * `Uncaught exception: Error: Failed to load image from path
+ * '.../app.asar/public/apple-touch-icon.png' at createWindow`.
+ *
+ * This helper makes icon resolution fail-soft: a candidate that exists but does
+ * not decode is skipped like a missing one, so the app falls through to the
+ * next candidate (or starts with the platform default icon) instead of dying.
+ * `nativeImage.createFromPath` is Electron's own decoder with the same failure
+ * mode, so callers can inject a probe matching their environment; the shipped
+ * probe decodes eagerly and treats a thrown error OR an empty image as invalid.
+ */
+export type IconProbe = (filePath: string) => boolean
+
+/** Eager-decoding default probe: the file must decode to a non-empty image. */
+export function decodingFileProbe(filePath: string): boolean {
+ try {
+ if (!fs.statSync(filePath).isFile()) {
+ return false
+ }
+ } catch {
+ return false
+ }
+
+ try {
+ return !nativeImage.createFromPath(filePath).isEmpty()
+ } catch {
+ return false
+ }
+}
+
+/**
+ * Pick the first app-icon candidate that exists AND decodes; `undefined` when
+ * none do (callers already treat a missing icon as optional — `if (icon)`).
+ *
+ * Pure over `(candidates, probe)` so the precedence ladder is unit-testable
+ * without a running Electron app; the shipped probe injects the real decoder.
+ */
+export function resolveAppIcon(
+ candidates: readonly string[],
+ probe: IconProbe = decodingFileProbe
+): string | undefined {
+ for (const candidate of candidates) {
+ if (probe(candidate)) {
+ return candidate
+ }
+ }
+
+ return undefined
+}
+
+/**
+ * Build the platform-aware candidate ladder shared by every window factory.
+ * Kept next to the resolver so precedence has one home; `appRoot` is injected
+ * (packaged `APP_ROOT` vs dev tree) and `unpackedPathFor` maps into
+ * `app.asar.unpacked` for builds that leave assets outside the archive.
+ */
+export function appIconCandidates(opts: {
+ isWindows: boolean
+ appRoot: string
+ resourcesPath?: string
+ unpackedPathFor: (p: string) => string
+}): string[] {
+ const { isWindows, appRoot, resourcesPath, unpackedPathFor } = opts
+
+ return [
+ ...(isWindows ? [path.join(resourcesPath ?? '', 'icon.ico'), path.join(appRoot, 'assets', 'icon.ico')] : []),
+ path.join(appRoot, 'public', 'apple-touch-icon.png'),
+ path.join(appRoot, 'dist', 'apple-touch-icon.png'),
+ path.join(unpackedPathFor(appRoot), 'dist', 'apple-touch-icon.png')
+ ]
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 9226db58d4c..78cd0127df9 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -32,6 +32,7 @@ import {
import { classifyActiveRuntime } from './active-runtime-state'
import { destroyKeepaliveAgents, downloadAgentFor, jsonAgentFor, withRetry } from './api-transport'
+import { appIconCandidates, resolveAppIcon } from './app-icon'
import { stopBackendChild as stopBackendChildImpl, stopBackendTreesForUpdate } from './backend-child'
import {
type BackendOutputTail,
@@ -877,14 +878,15 @@ const WINDOW_BUTTON_POSITION = {
// Windows, where icons are full-bleed. Windows prefers the full-bleed
// assets/icon.ico (shipped to resources/ via extraResources) and only falls
// back to the padded PNG if the ico is missing.
-const APP_ICON_PATHS = [
- ...(IS_WINDOWS
- ? [path.join(process.resourcesPath ?? '', 'icon.ico'), path.join(APP_ROOT, 'assets', 'icon.ico')]
- : []),
- path.join(APP_ROOT, 'public', 'apple-touch-icon.png'),
- path.join(APP_ROOT, 'dist', 'apple-touch-icon.png'),
- path.join(unpackedPathFor(APP_ROOT), 'dist', 'apple-touch-icon.png')
-]
+// The ladder is BUILT once here but each window factory RE-RESOLVES through
+// resolveAppIcon (decoding probe): existence alone is not proof the bytes
+// decode, and an undecodable icon must never take the main process down.
+const APP_ICON_PATHS = appIconCandidates({
+ isWindows: IS_WINDOWS,
+ appRoot: APP_ROOT,
+ resourcesPath: process.resourcesPath,
+ unpackedPathFor
+})
let rendererTitleBarTheme = null
@@ -6383,7 +6385,14 @@ function registerPowerResumeListeners() {
}
function getAppIconPath() {
- return APP_ICON_PATHS.find(fileExists)
+ // Fail-soft: skip candidates that exist but don't decode (truncated PNG in a
+ // packaged app.asar previously crashed createWindow mid-session). Missing
+ // every candidate is fine — the window then uses the platform default icon.
+ try {
+ return resolveAppIcon(APP_ICON_PATHS)
+ } catch {
+ return undefined
+ }
}
function sendOpenUpdatesRequested() {