hermes-agent/apps/desktop/electron/quit-guard.ts
Brooklyn Nicholson e6dccfcf37 fix(desktop): decide last-window close guarding in quit-guard
shouldGuardWindowClose keeps the last-chat-window semantics (platform,
handoff, and remaining-window gates) as a pure function so the close path
can share the quit guard's decision logic.

Co-authored-by: rayn1314 <[email protected]>
2026-09-29 18:43:44 -05:00

156 lines
5.5 KiB
TypeScript

// Quitting with a turn in flight kills the backend mid-tool-call: the work is
// lost, and anything the agent had half-written to disk stays half-written.
// 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
export interface ActiveWork {
/** Titles of sessions running a turn. Untitled sessions contribute a count only. */
titles: string[]
/** Running turns, including untitled ones — always >= titles.length. */
count: number
}
export const NO_ACTIVE_WORK: ActiveWork = { count: 0, titles: [] }
/** Coerce an IPC payload from an untrusted renderer into an ActiveWork. */
export function normalizeActiveWork(payload: unknown): ActiveWork {
if (!payload || typeof payload !== 'object') {
return NO_ACTIVE_WORK
}
const raw = payload as { count?: unknown; titles?: unknown }
const titles = Array.isArray(raw.titles)
? raw.titles
.filter((title): title is string => typeof title === 'string')
.map(title => title.trim())
.filter(Boolean)
: []
const count = typeof raw.count === 'number' && Number.isFinite(raw.count) ? Math.max(0, Math.floor(raw.count)) : 0
return { count: Math.max(count, titles.length), titles }
}
/** Merge every window's report into one. Windows can show the same session. */
export function mergeActiveWork(reports: Iterable<ActiveWork>): ActiveWork {
const titles: string[] = []
let count = 0
for (const report of reports) {
count = Math.max(count, report.count)
for (const title of report.titles) {
if (!titles.includes(title)) {
titles.push(title)
}
}
}
return { count: Math.max(count, titles.length), titles }
}
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,
backendOwned: boolean = true
): null | QuitPrompt {
if (quittingForHandoff || work.count < 1) {
return null
}
const listed = work.titles.slice(0, MAX_LISTED)
const remaining = work.count - listed.length
const lines = listed.map(title => `• ${title}`)
if (remaining > 0) {
lines.push(remaining === 1 ? '• 1 more' : `• ${remaining} more`)
}
return {
buttons: backendOwned ? ['Keep Running', 'Quit Anyway'] : ['Cancel', 'Quit'],
detail: [
lines.join('\n'),
lines.length > 0 ? '' : null,
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')
.trim(),
message: work.count === 1 ? 'Hermes is still working on 1 chat.' : `Hermes is still working on ${work.count} chats.`
}
}
/**
* Whether a window's 'close' event should be intercepted to show the
* active-work confirmation.
*
* On Windows and Linux, closing the last window triggers app.quit() →
* before-quit, but by then the renderer's webContents is already destroyed
* and its entry has been removed from the active-work map — so the existing
* before-quit guard finds nothing and the app exits silently. Intercepting
* 'close' instead catches the guard while the work data is still live.
*
* macOS is excluded: closing the primary window there is the standard
* "window stays in Dock" gesture, not a quit, and prompting on it would
* block a normal workflow. Chat windows other than the final one are also
* excluded because closing them does not stop the application or its work.
*/
export function shouldGuardWindowClose(
work: ActiveWork,
quittingForHandoff: boolean,
isMac: boolean,
hasOtherChatWindows: boolean
): boolean {
if (isMac || quittingForHandoff || hasOtherChatWindows || work.count < 1) {
return false
}
return true
}