hermes-agent/apps/desktop/src/store/active-work.ts
Brooklyn Nicholson 9ae3bd73c9 feat(desktop): confirm before quitting with a turn in flight
Cmd-Q went straight through to teardown, killing the backend mid-tool-call
— the turn is gone and whatever the agent was part-way through writing
stays part-way written, with nothing on screen to warn about it.

Renderers now report which chats are mid-turn; before-quit merges the
reports and asks, naming them, defaulting to Keep Running. Update, swap,
and uninstall relaunches skip the prompt: those are the app replacing
itself, and a modal there would strand the detached script waiting on a
PID that never exits.
2026-07-27 15:47:57 -05:00

42 lines
1.4 KiB
TypeScript

/**
* Mirror of "which chats are mid-turn" to the main process.
*
* The renderer is the only side that knows a turn is in flight, and the main
* process is the only side that can intercept a quit. This module bridges the
* two: it publishes a small summary on every membership change, and
* `electron/quit-guard.ts` turns that into the confirmation dialog.
*
* Imported for its side effect from `main.tsx`, alongside `store/translucency`.
*/
import { computed } from 'nanostores'
import type { HermesActiveWork } from '@/global'
import { $sessions } from '@/store/session'
import { $workingSessionIds } from '@/store/session-states'
const $activeWork = computed([$workingSessionIds, $sessions], (workingIds, sessions): HermesActiveWork => {
const titleById = new Map(sessions.map(session => [session.id, session.title?.trim() ?? '']))
return {
count: workingIds.length,
titles: workingIds.map(id => titleById.get(id) ?? '').filter(Boolean)
}
})
if (typeof window !== 'undefined') {
// `$sessions` republishes on unrelated churn (previews, heartbeats), so only
// send when the summary itself moved — this crosses a process boundary.
let lastSent = ''
$activeWork.subscribe(work => {
const next = JSON.stringify(work)
if (next === lastSent) {
return
}
lastSent = next
window.hermesDesktop?.setActiveWork?.(work)
})
}