Skip to content

The agent that grows with you

fix(desktop): don't warn about lost work when quitting a remote backend

brooklyn!0e69909aabc7
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index cc4c5ceb3ad..d557516c34d 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -384,7 +384,7 @@ import {
} from './profile-session-routing'
import { createQuickEntryShortcut, quickEntryWindowBounds, sanitizeQuickEntrySettings } from './quick-entry'
import { createQuitFinalization } from './quit-finalization'
-import { type ActiveWork, mergeActiveWork, normalizeActiveWork, quitPromptFor } from './quit-guard'
+import { type ActiveWork, backendOwnedByApp, mergeActiveWork, normalizeActiveWork, quitPromptFor } from './quit-guard'
import { backendQuitNeedsWait, createQuitTeardownCoordinator } from './quit-teardown'
import * as remoteLifecycle from './remote-lifecycle'
import {
@@ -18855,8 +18855,39 @@ function configureSpellChecker() {
}
}
+// Does quitting take the agent down with the app? Reads the primary profile's
+// route through the same resolver resolveRemoteBackend uses, plus every backend
+// the quit teardown below will stop (spawned children, SSH-managed servers).
+// A route we can't resolve counts as owned: the lost-work warning is the safe
+// side to be wrong on.
+function quitStopsBackendWork(): boolean {
+ let primaryRouteKind: 'cloud' | 'remote' | 'ssh' | null
+
+ try {
+ primaryRouteKind =
+ resolveDesktopRemoteRoute({
+ config: readDesktopConnectionConfig(),
+ env: {
+ token: process.env.HERMES_DESKTOP_REMOTE_TOKEN,
+ url: process.env.HERMES_DESKTOP_REMOTE_URL
+ },
+ profile: primaryProfileKey(),
+ registry: readDesktopConnectionsRegistry()
+ })?.kind ?? null
+ } catch {
+ return true
+ }
+
+ const ownedBackendCount =
+ (backendConnectionState.getProcess() ? 1 : 0) +
+ [...backendPool.values()].filter(entry => entry?.process).length +
+ sshConnections.size
+
+ return backendOwnedByApp({ ownedBackendCount, primaryRouteKind })
+}
+
// Ask before a quit kills a turn in flight. True when the quit was intercepted
-// and the confirmation is on screen; "Quit Anyway" re-enters before-quit with
+// and the confirmation is on screen; the confirm button re-enters before-quit with
// the latch set and falls straight through to the teardown below.
function heldQuitForActiveWork(event: Electron.Event): boolean {
if (SKIP_QUIT_CONFIRM || quitConfirmedWithActiveWork || isQuittingForHandoff) {
@@ -18869,7 +18900,11 @@ function heldQuitForActiveWork(event: Electron.Event): boolean {
return true
}
- const prompt = quitPromptFor(mergeActiveWork(activeWorkByWebContents.values()), isQuittingForHandoff)
+ const prompt = quitPromptFor(
+ mergeActiveWork(activeWorkByWebContents.values()),
+ isQuittingForHandoff,
+ quitStopsBackendWork()
+ )
// A tray quit with live work still needs the ordinary visible confirmation.
if (prompt && minimizeToTray.status().available) {
@@ -18889,7 +18924,7 @@ function heldQuitForActiveWork(event: Electron.Event): boolean {
void dialog
.showMessageBox(parent, {
- buttons: ['Keep Running', 'Quit Anyway'],
+ buttons: [...prompt.buttons],
cancelId: 0,
defaultId: 0,
detail: prompt.detail,
diff --git a/apps/desktop/electron/quit-guard.test.ts b/apps/desktop/electron/quit-guard.test.ts
index fdef3c5b051..58c91150d98 100644
--- a/apps/desktop/electron/quit-guard.test.ts
+++ b/apps/desktop/electron/quit-guard.test.ts
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
-import { mergeActiveWork, normalizeActiveWork, quitPromptFor } from './quit-guard'
+import { backendOwnedByApp, mergeActiveWork, normalizeActiveWork, quitPromptFor } from './quit-guard'
test('normalizeActiveWork drops junk and keeps the count at least the title count', () => {
assert.deepEqual(normalizeActiveWork(null), { count: 0, titles: [] })
@@ -50,3 +50,46 @@ test('quitPromptFor summarizes past the list cap and counts untitled work', () =
assert.ok(!prompt.detail.includes('• e'))
assert.ok(prompt.detail.includes('• 5 more'))
})
+
+// #79579: only a backend the app owns (spawned locally, or started over SSH)
+// dies with it. A remote URL or Hermes Cloud backend keeps the turn running
+// after the app quits, so the prompt must not claim the work is lost.
+test('backendOwnedByApp: a local primary is owned even before its child attaches', () => {
+ assert.equal(backendOwnedByApp({ ownedBackendCount: 0, primaryRouteKind: null }), true)
+})
+
+test('backendOwnedByApp: an SSH primary is owned (the app starts and stops that server)', () => {
+ assert.equal(backendOwnedByApp({ ownedBackendCount: 0, primaryRouteKind: 'ssh' }), true)
+})
+
+test('backendOwnedByApp: a remote URL or cloud primary with nothing spawned is not owned', () => {
+ assert.equal(backendOwnedByApp({ ownedBackendCount: 0, primaryRouteKind: 'remote' }), false)
+ assert.equal(backendOwnedByApp({ ownedBackendCount: 0, primaryRouteKind: 'cloud' }), false)
+})
+
+test('backendOwnedByApp: a remote primary alongside a spawned backend stays owned', () => {
+ // Another window/profile may be running its turn on that local child.
+ assert.equal(backendOwnedByApp({ ownedBackendCount: 1, primaryRouteKind: 'remote' }), true)
+})
+
+test('quitPromptFor warns about lost work when the app owns the backend (local)', () => {
+ const owned = backendOwnedByApp({ ownedBackendCount: 1, primaryRouteKind: null })
+ const prompt = quitPromptFor({ count: 1, titles: ['Fix login'] }, false, owned)
+
+ assert.ok(prompt)
+ assert.ok(prompt.detail.includes('is lost'))
+ assert.deepEqual(prompt.buttons, ['Keep Running', 'Quit Anyway'])
+})
+
+for (const primaryRouteKind of ['remote', 'cloud'] as const) {
+ test(`quitPromptFor says the agent keeps running on a ${primaryRouteKind} backend`, () => {
+ const owned = backendOwnedByApp({ ownedBackendCount: 0, primaryRouteKind })
+ const prompt = quitPromptFor({ count: 1, titles: ['Fix login'] }, false, owned)
+
+ assert.ok(prompt)
+ assert.ok(prompt.detail.includes('• Fix login'))
+ assert.ok(!prompt.detail.includes('lost'), 'a backend that outlives the app loses nothing')
+ assert.ok(prompt.detail.includes('keeps running'))
+ assert.notDeepEqual(prompt.buttons, ['Keep Running', 'Quit Anyway'])
+ })
+}
diff --git a/apps/desktop/electron/quit-guard.ts b/apps/desktop/electron/quit-guard.ts
index e2bb40e59da..aee55e6575c 100644
--- a/apps/desktop/electron/quit-guard.ts
+++ b/apps/desktop/electron/quit-guard.ts
@@ -3,6 +3,10 @@
// Renderers publish what they're running; the main process asks before it lets
// that go. The decision + copy live here (pure, testable) so main.ts only owns
// the IPC and the dialog call.
+//
+// That's only true for a backend the app owns. A remote URL or Hermes Cloud
+// backend is supervised elsewhere and finishes the turn after the app quits,
+// so its prompt says so instead of warning about lost work (#79579).
const MAX_LISTED = 4
@@ -54,18 +58,47 @@ export function mergeActiveWork(reports: Iterable<ActiveWork>): ActiveWork {
}
export interface QuitPrompt {
+ /** [cancel, confirm]: index 0 keeps the app open, index 1 quits. */
+ buttons: readonly [string, string]
detail: string
message: string
}
+export interface BackendOwnershipInput {
+ /**
+ * Backends this app stops when it quits: spawned local children plus
+ * SSH-managed servers, across the primary and every pooled profile.
+ */
+ ownedBackendCount: number
+ /** What the primary profile resolves to; null means a locally spawned backend. */
+ primaryRouteKind: 'cloud' | 'remote' | 'ssh' | null
+}
+
+/**
+ * Whether quitting takes the agent down with the app. Local and SSH backends
+ * are started and stopped by the app. A remote URL or cloud backend is not,
+ * but any other backend the app spawned (another window's connection, a
+ * pooled profile) might be where the turn is running, so it still counts.
+ */
+export function backendOwnedByApp({ ownedBackendCount, primaryRouteKind }: BackendOwnershipInput): boolean {
+ return primaryRouteKind === null || primaryRouteKind === 'ssh' || ownedBackendCount > 0
+}
+
/**
* The confirmation to show, or null when quitting should just proceed.
*
* `quittingForHandoff` covers the update / swap / uninstall relaunches: those
* are the app replacing itself, not the user walking away, and a modal there
* would strand the detached script waiting on a PID that never exits.
+ *
+ * `backendOwned` (see backendOwnedByApp) picks the copy: an owned backend dies
+ * with the app, a remote/cloud one keeps working after it closes.
*/
-export function quitPromptFor(work: ActiveWork, quittingForHandoff: boolean): null | QuitPrompt {
+export function quitPromptFor(
+ work: ActiveWork,
+ quittingForHandoff: boolean,
+ backendOwned: boolean = true
+): null | QuitPrompt {
if (quittingForHandoff || work.count < 1) {
return null
}
@@ -79,10 +112,13 @@ export function quitPromptFor(work: ActiveWork, quittingForHandoff: boolean): nu
}
return {
+ buttons: backendOwned ? ['Keep Running', 'Quit Anyway'] : ['Cancel', 'Quit'],
detail: [
lines.join('\n'),
lines.length > 0 ? '' : null,
- 'Quitting stops the agent mid-turn. Any work it has not finished writing is lost.'
+ backendOwned
+ ? 'Quitting stops the agent mid-turn. Any work it has not finished writing is lost.'
+ : 'The agent keeps running on the remote backend. Quitting only closes Hermes on this computer; reconnect later to see the results.'
]
.filter(line => line !== null)
.join('\n')