From 59ca1f5f77b905fe642e7c5ae03ca47ddeb43189 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 18 Nov 2026 16:05:12 +0700 Subject: [PATCH] Replace Desktop's own daemon on update after Desktop relaunches (#10067) An in-app update and the startup version check again use the desktopManaged flag in paseo.pid, while Manage built-in daemon is on. A daemon that an earlier Desktop session launched is now replaced on update. A daemon that the user started through the CLI is still never stopped automatically. Closes #6385. --- docs/architecture.md | 2 +- packages/desktop/bin/paseo.cmd | 3 - packages/desktop/e2e/daemon-lifecycle.e2e.mjs | 63 ++++++++++++++----- packages/desktop/src/daemon/daemon-manager.ts | 27 ++++++-- 4 files changed, 71 insertions(+), 24 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index de6c619ae3..f74d6456c9 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -506,5 +506,5 @@ Agent and terminal environments are unchanged. System proxy settings and SOCKS-o ## Deployment models 1. **Local daemon** (default): `paseo daemon start` on `127.0.0.1:6767` -2. **Managed desktop**: Electron uses the server package’s local-instance lifecycle capability for sanitized launch, published readiness, and captured-PID stop. Only a matching `{pid, startedAt}` from a spawn in the current Desktop session authorizes automatic stop or binary replacement. Preexisting instances, including legacy `desktopManaged` records, are attach-only. Keep-running survivors attach in the next session. Explicit attached Stop requires a confirmation naming the captured home/PID. Ordinary Restart uses the worker RPC on every app platform. +2. **Managed desktop**: Electron uses the server package’s local-instance lifecycle capability for sanitized launch, published readiness, and captured-PID stop. Only Desktop's own launch writes `desktopManaged: true` to `paseo.pid`. The CLI launch path removes the flag. While Desktop manages the built-in daemon, an app update or a version mismatch replaces any `desktopManaged` daemon, including one that an earlier Desktop session left running. Bundled CLIs before v0.9.0 exported the flag, so a daemon that one of them started also carries it and is replaced. Quit, Settings, and host removal stop only a daemon that the current session launched, matched by `{pid, startedAt}`. A daemon that the user started through the CLI is attach-only. Explicit attached Stop requires a confirmation naming the captured home/PID. Ordinary Restart uses the worker RPC on every app platform. 3. **Remote + relay**: Daemon behind firewall, relay bridges with E2E encryption diff --git a/packages/desktop/bin/paseo.cmd b/packages/desktop/bin/paseo.cmd index 1e767523bb..2dac445381 100644 --- a/packages/desktop/bin/paseo.cmd +++ b/packages/desktop/bin/paseo.cmd @@ -11,9 +11,6 @@ if not exist "%APP_EXECUTABLE%" ( set "ELECTRON_RUN_AS_NODE=1" set "PASEO_NODE_ENV=production" -rem PASEO_DESKTOP_MANAGED marks daemons started through this bundled CLI as -rem desktop-managed, so the desktop app restarts them when it upgrades. -set "PASEO_DESKTOP_MANAGED=1" set "PASEO_CLI=%~f0" "%APP_EXECUTABLE%" --disable-warning=DEP0040 "%RESOURCES_DIR%\app.asar.unpacked\dist\daemon\node-entrypoint-runner.js" node-script "%RESOURCES_DIR%\app.asar\node_modules\@getpaseo\cli\dist\index.js" %* exit /b %errorlevel% diff --git a/packages/desktop/e2e/daemon-lifecycle.e2e.mjs b/packages/desktop/e2e/daemon-lifecycle.e2e.mjs index 870e0f7db5..f0cbb74553 100644 --- a/packages/desktop/e2e/daemon-lifecycle.e2e.mjs +++ b/packages/desktop/e2e/daemon-lifecycle.e2e.mjs @@ -41,16 +41,24 @@ savePersistedConfig(home, { }, }); const main = path.join(root, "main.cjs"); +const updatedMain = path.join(root, "updated-main.cjs"); const managerPath = path.join(repo, "packages/desktop/dist/daemon/daemon-manager.js"); +const desktopMain = (setup) => + `const { app } = require("electron"); ${setup} app.setPath("userData", ${JSON.stringify(path.join(root, "user-data"))}); app.whenReady().then(() => { global.lifecycle = require(${JSON.stringify(managerPath)}); });`; +await writeFile(main, desktopMain("")); +// An unpackaged Desktop reads its version from packages/desktop/package.json. This entry +// reports a newer version, standing in for the Desktop an update installs. await writeFile( - main, - `const { app } = require("electron"); app.setPath("userData", ${JSON.stringify(path.join(root, "user-data"))}); app.whenReady().then(() => { global.lifecycle = require(${JSON.stringify(managerPath)}); });`, + updatedMain, + desktopMain( + `const fs = require("node:fs"); const path = require("node:path"); const readFileSync = fs.readFileSync; fs.readFileSync = (file, ...rest) => typeof file === "string" && path.relative(${JSON.stringify(path.join(repo, "packages/desktop"))}, file) === "package.json" ? JSON.stringify({ version: "999.0.0" }) : readFileSync(file, ...rest);`, + ), ); let desktop; let captured; -async function openDesktop() { +async function openDesktop(entry = main) { desktop = await electron.launch({ - args: ["--no-sandbox", "--ozone-platform=headless", main], + args: ["--no-sandbox", "--ozone-platform=headless", entry], env: { ...env, PASEO_LISTEN: "127.0.0.1:1", @@ -78,27 +86,31 @@ async function command(name, args) { ); } try { - // Simulate a legacy independent launch that carries the old Desktop flag. + // A daemon the user started through the CLI, which Desktop must never stop on its own. + // Older bundled CLIs exported PASEO_DESKTOP_MANAGED, and the CLI launch must not record it. const launch = await startDaemonInstance({ home, command: process.execPath, args: [path.join(repo, "packages/server/dist/scripts/supervisor-entrypoint.js")], - env, + env: { ...env, PASEO_DESKTOP_MANAGED: "1" }, mode: "deployment", - desktopManaged: true, timeoutMs: 30_000, }); captured = launch.instance; await openDesktop(); const attached = await command("start_desktop_daemon"); assert.equal(attached.pid, captured.pid); - assert.equal(attached.desktopManaged, true); + assert.equal(attached.desktopManaged, false); assert.equal(attached.ownedByDesktop, false); for (const reason of ["quit", "settings", "app_update", "host_remove", "version_mismatch"]) { const status = await command("stop_desktop_daemon", { reason }); assert.equal(status.pid, captured.pid, `${reason} stopped an attached daemon`); process.kill(captured.pid, 0); } + await closeDesktop(); + await openDesktop(updatedMain); + const afterUpdate = await command("start_desktop_daemon"); + assert.equal(afterUpdate.pid, captured.pid, "a newer Desktop replaced a daemon the user started"); const restarted = await command("restart_desktop_daemon"); assert.equal(restarted.pid, captured.pid); await assert.rejects( @@ -134,18 +146,37 @@ try { desktop = null; process.kill(captured.pid, 0); await openDesktop(); - assert.equal((await command("start_desktop_daemon")).ownedByDesktop, false); - await command("stop_desktop_daemon", { reason: "quit" }); + // The next session attaches to the daemon Desktop launched before it quit. + const survivor = await command("start_desktop_daemon"); + assert.equal(survivor.pid, captured.pid); + assert.equal(survivor.desktopManaged, true); + assert.equal(survivor.ownedByDesktop, false); + for (const reason of ["quit", "settings", "host_remove"]) { + await command("stop_desktop_daemon", { reason }); + process.kill(captured.pid, 0); + } + // With management off, Desktop leaves the daemon running through an update. + await command("patch_desktop_settings", { daemon: { manageBuiltInDaemon: false } }); + await command("stop_desktop_daemon", { reason: "app_update" }); process.kill(captured.pid, 0); - await command("stop_desktop_daemon", { - reason: "manual_ipc", - pid: captured.pid, - startedAt: captured.startedAt, - }); + await command("patch_desktop_settings", { daemon: { manageBuiltInDaemon: true } }); + // Installing an update stops it, so the updated Desktop starts its own daemon. + const beforeInstall = await command("stop_desktop_daemon", { reason: "app_update" }); + assert.equal(beforeInstall.status, "stopped", "app_update left Desktop's daemon running"); captured = null; const next = await command("start_desktop_daemon"); captured = await readDaemonInstance(home); assert.equal(next.ownedByDesktop, true); + await closeDesktop(); + process.kill(captured.pid, 0); + // A newer Desktop replaces the daemon an older Desktop launched and kept running. + const outdated = captured; + await openDesktop(updatedMain); + const replaced = await command("start_desktop_daemon"); + captured = await readDaemonInstance(home); + assert.notEqual(replaced.pid, outdated.pid, "a newer Desktop kept the outdated daemon"); + assert.equal(replaced.ownedByDesktop, true); + assert.throws(() => process.kill(outdated.pid, 0), "the outdated daemon is still running"); await command("stop_desktop_daemon", { reason: "quit" }); assert.equal(await readDaemonInstance(home), null); captured = null; @@ -195,7 +226,7 @@ try { ); } console.log( - "PASS: attached legacy daemon survives automatic actions; explicit captured stop, owned launch, managed settings, worker restart, and next-session attachment.", + "PASS: a user-started daemon survives automatic actions and updates; explicit captured stop, owned launch, managed settings, worker restart, next-session attachment, and update replacement of an earlier session's daemon.", ); } finally { await closeDesktop(); diff --git a/packages/desktop/src/daemon/daemon-manager.ts b/packages/desktop/src/daemon/daemon-manager.ts index 98d5287932..44076bbc4b 100644 --- a/packages/desktop/src/daemon/daemon-manager.ts +++ b/packages/desktop/src/daemon/daemon-manager.ts @@ -269,7 +269,7 @@ function normalizeVersion(version: string | null): string | null { } function shouldRestartForVersion(current: DesktopDaemonStatus): boolean { - if (!current.ownedByDesktop) return false; + if (!current.desktopManaged) return false; const appVersion = normalizeVersion(resolveDesktopAppVersion()); const daemonVersion = normalizeVersion(current.version); return Boolean(appVersion && daemonVersion && appVersion !== daemonVersion); @@ -336,12 +336,13 @@ export async function stopDesktopDaemon( ): Promise { const home = getPaseoHome(); const instance = await readDaemonInstance(home); - const owned = Boolean( + const launchedThisSession = Boolean( instance && ownedLaunch && ownedLaunch.home === home && isSameDaemonInstance(instance, ownedLaunch.instance), ); + const owned = launchedThisSession || (await replacesDesktopDaemon(reason, instance)); const explicit = reason === "manual_ipc" && confirmedInstance && @@ -361,10 +362,28 @@ export async function stopDesktopDaemon( keepTerminals, }); await stopCapturedDaemon({ home, instance, reason, keepTerminals }); - if (owned) ownedLaunch = null; + if (launchedThisSession) ownedLaunch = null; return resolveDesktopDaemonStatus(); } +/** An app update or a version mismatch replaces the daemon with the one this Desktop bundles. */ +function isReplacement(reason: DesktopDaemonStopReason): boolean { + return reason === "app_update" || reason === "version_mismatch"; +} + +/** + * While Desktop manages the built-in daemon, a replacement applies to every daemon Desktop + * launched, which `paseo.pid` records as `desktopManaged`. That includes one an earlier Desktop + * session left running. Other automatic stops apply only to a daemon this session launched. + */ +async function replacesDesktopDaemon( + reason: DesktopDaemonStopReason, + instance: DaemonInstance | null, +): Promise { + if (!isReplacement(reason) || instance?.desktopManaged !== true) return false; + return (await getDesktopSettingsStore().get()).daemon.manageBuiltInDaemon; +} + export interface StopCapturedDaemonInput { home: string; instance: DaemonInstance; @@ -428,7 +447,7 @@ export function keepsTerminalsOnStop( platform: NodeJS.Platform, ): boolean { if (platform === "win32") return false; - return reason === "app_update" || reason === "version_mismatch"; + return isReplacement(reason); } async function restartDaemon(): Promise {