Skip to content

The agent that grows with you

test(desktop): pin deferred launcher-entry writes and the terminal-launch control

teknium15d2acf7066a9
diff --git a/apps/desktop/electron/linux-launcher-ready.test.ts b/apps/desktop/electron/linux-launcher-ready.test.ts
new file mode 100644
index 00000000000..7f9f4523291
--- /dev/null
+++ b/apps/desktop/electron/linux-launcher-ready.test.ts
@@ -0,0 +1,48 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { LAUNCHER_READY_FD_ENV, notifyLauncherWindowRevealed } from './linux-launcher-ready'
+
+function createIo() {
+ const writes: Array<[number, string]> = []
+ const closes: number[] = []
+
+ return {
+ closes,
+ io: {
+ closeSync: (fd: number) => {
+ closes.push(fd)
+ },
+ writeSync: (fd: number, data: string) => {
+ writes.push([fd, data])
+
+ return data.length
+ }
+ },
+ writes
+ }
+}
+
+test('writes one byte to the launcher fd, closes it and consumes the variable', () => {
+ const env: NodeJS.ProcessEnv = { [LAUNCHER_READY_FD_ENV]: '7' }
+ const { closes, io, writes } = createIo()
+
+ assert.equal(notifyLauncherWindowRevealed(env, io), true)
+ assert.deepEqual(writes, [[7, 'r']])
+ assert.deepEqual(closes, [7])
+ assert.equal(LAUNCHER_READY_FD_ENV in env, false)
+
+ // A second reveal must not touch fd 7 again: the number may now belong to another file.
+ assert.equal(notifyLauncherWindowRevealed(env, io), false)
+ assert.equal(writes.length, 1)
+})
+
+test('a launch without the variable, or with a garbage value, is a no-op', () => {
+ const { io, writes } = createIo()
+
+ assert.equal(notifyLauncherWindowRevealed({}, io), false)
+ assert.equal(notifyLauncherWindowRevealed({ [LAUNCHER_READY_FD_ENV]: 'seven' }, io), false)
+ assert.equal(notifyLauncherWindowRevealed({ [LAUNCHER_READY_FD_ENV]: '-1' }, io), false)
+ assert.deepEqual(writes, [])
+})
diff --git a/tests/hermes_cli/test_gui_command.py b/tests/hermes_cli/test_gui_command.py
index 5419ad8d168..d0a55a5ff13 100644
--- a/tests/hermes_cli/test_gui_command.py
+++ b/tests/hermes_cli/test_gui_command.py
@@ -3,8 +3,10 @@
from __future__ import annotations
import argparse
+import os
import subprocess
import sys
+import time
from pathlib import Path
from unittest.mock import patch
@@ -974,9 +976,10 @@ def test_relaunchable_fixup_legacy_adhoc_success_still_verifies_and_never_delete
@pytest.mark.linux_only
def test_gui_registers_linux_desktop_entry_before_launch(tmp_path, monkeypatch):
- """`hermes desktop` gives the app a launcher presence on Linux."""
+ """A terminal launch (no DESKTOP_STARTUP_ID) still installs the entry before spawning Electron."""
root = _make_desktop_tree(tmp_path)
monkeypatch.setattr(cli_main, "PROJECT_ROOT", root)
+ monkeypatch.delenv("DESKTOP_STARTUP_ID", raising=False)
packaged_exe = _make_packaged_executable(root, monkeypatch)
registered: list[Path] = []
@@ -998,6 +1001,46 @@ def test_gui_registers_linux_desktop_entry_before_launch(tmp_path, monkeypatch):
assert registered == [root]
+def test_gui_shell_launch_defers_desktop_entry_until_window_reveal(tmp_path, monkeypatch):
+ """An app-grid launch (DESKTOP_STARTUP_ID set) writes the entry only after Electron reports
+ its window on screen — never before the spawn, while gnome-shell has the app in STARTING
+ (#111906). Electron gets the pipe's write end via HERMES_DESKTOP_READY_FD."""
+ root = _make_desktop_tree(tmp_path)
+ monkeypatch.setattr(cli_main, "PROJECT_ROOT", root)
+ monkeypatch.setenv("DESKTOP_STARTUP_ID", "gnome-shell/Hermes/1-0_TIME1")
+ monkeypatch.setattr("hermes_cli.linux_desktop_entry.time.sleep", lambda _s: None)
+ packaged_exe = _make_packaged_executable(root, monkeypatch)
+
+ events: list[str] = []
+ monkeypatch.setattr("hermes_cli.linux_desktop_entry.is_supported", lambda: True)
+ monkeypatch.setattr(
+ "hermes_cli.linux_desktop_entry.install_desktop_entry",
+ lambda project_root: events.append(f"install:{project_root}") or (tmp_path / "hermes.desktop"),
+ )
+
+ def fake_electron(cmd, **kwargs):
+ events.append("spawn")
+ fd = int(kwargs["env"]["HERMES_DESKTOP_READY_FD"])
+ assert fd in kwargs["pass_fds"]
+ os.write(fd, b"r") # main window revealed
+ deadline = time.monotonic() + 10
+ while not any(e.startswith("install:") for e in events) and time.monotonic() < deadline:
+ time.sleep(0.01)
+ return subprocess.CompletedProcess(cmd, 0)
+
+ with patch("hermes_cli.main_desktop._desktop_build_needed", return_value=False), \
+ patch("hermes_cli.main_install_repair._resolve_node_runtime_npm", return_value="/usr/bin/npm"), \
+ patch("hermes_cli.main_desktop._desktop_linux_sandbox_fixup", return_value=True), \
+ patch("hermes_cli.main.subprocess.run", side_effect=fake_electron), \
+ pytest.raises(SystemExit) as exc:
+ cli_main.cmd_gui(_ns())
+
+ assert exc.value.code == 0
+ assert events == ["spawn", f"install:{root}"]
+ assert packaged_exe.exists()
+
+
@pytest.mark.linux_only
def test_gui_launches_even_when_desktop_entry_install_fails(tmp_path, monkeypatch):
"""Launcher plumbing is a convenience — it must never block the app."""
diff --git a/tests/hermes_cli/test_linux_desktop_entry.py b/tests/hermes_cli/test_linux_desktop_entry.py
index 06dcbf4cf74..db72c991f3d 100644
--- a/tests/hermes_cli/test_linux_desktop_entry.py
+++ b/tests/hermes_cli/test_linux_desktop_entry.py
@@ -7,6 +7,7 @@ import os
import stat
import struct
import sys
+import time
from pathlib import Path
import pytest
@@ -1147,3 +1148,20 @@ def test_install_resizes_decodable_png_to_panel_sizes(
assert not stale.exists()
assert struct.unpack(">II", dest_24.read_bytes()[16:24]) == (24, 24)
assert struct.unpack(">II", dest_256.read_bytes()[16:24]) == (256, 256)
+
+
+def test_deferred_install_heals_exactly_once_after_exit_without_reveal():
+ """Electron exiting without ever revealing a window (crash, --version, closed at the
+ onboarding step) must still self-heal the entry — once, after the exit, when no
+ STARTING app object exists (#111906)."""
+ calls: list[Path] = []
+ deferred = lde.DeferredDesktopEntryInstall(
+ Path("/proj"), install=lambda root: calls.append(root) or Path("/entry"), settle_seconds=0
+ )
+ deferred.start()
+ time.sleep(0.05)
+ assert calls == [] # nothing is written while the app may still be STARTING
+
+ deferred.finish()
+ assert calls == [Path("/proj")]
+ assert not deferred._thread.is_alive()
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index d43cb51cd70..ba24216dc09 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -334,6 +334,8 @@ hermes config set desktop.manage_launcher_entry false
A missing entry is still created; the flag only stops `hermes desktop` from rewriting an entry that already exists.
+When you start Hermes from the application grid or menu (the launcher sets `DESKTOP_STARTUP_ID`), the entry is written only after the window is on screen, or after the app exits if no window ever appeared. Some GNOME Shell versions lose track of an app whose `.desktop` file changes while it is still starting, and that can crash the whole session later; waiting for the window avoids it. Terminal launches and the updater's relaunch still write the entry immediately.
+
| Flag | Description |
| -------------------- | ----------------------------------------------------------------------------------------- |
| `--skip-build` | Skip npm install/package and launch the existing unpacked app from `apps/desktop/release` |