hermes-agent/web/src/lib/chat-activation.ts
xxxigm 3bcd0c1b00
fix(dashboard): only open the chat PTY once the chat tab is active (#59551)
* fix(dashboard): only open the chat PTY once the chat tab is active

The dashboard mounts ChatPage persistently (hidden with CSS) on every route
so the embedded chat PTY survives tab switches. But the PTY-connect effect
never checked whether the chat tab was active, so it opened `/api/pty` on
mount for ANY dashboard page. On a source/RPi install that spawns the whole
TUI + agent bootstrap (`Installing TUI dependencies…` → `npm install`) merely
by loading /sessions, /system, etc. — work the user never asked for, and the
trigger behind "dashboard loses custom themes on /chat load".

Gate the connect effect on a sticky activation latch: the PTY is not spawned
until the chat tab has been active at least once, and stays connected across
later tab switches so the persistence UX is preserved.

* test(dashboard): cover chat PTY activation latch

Asserts the invariant behind the fix: activation is sticky. It stays false
while the chat tab has never been active (so the persistently-mounted,
hidden ChatPage never opens /api/pty), flips true when the tab activates,
and stays true after the user navigates away (PTY persistence).
2026-07-18 00:50:13 -04:00

17 lines
866 B
TypeScript

/**
* Chat PTY activation latch.
*
* The dashboard keeps `ChatPage` mounted persistently (just hidden with CSS)
* on every route so the embedded chat PTY survives tab switches. The downside
* is that the PTY-connect effect would otherwise open `/api/pty` — which spawns
* the whole TUI + agent bootstrap (on a fresh checkout this prints
* `Installing TUI dependencies…` and runs `npm install`) — the moment the
* dashboard loads *any* page, even one the user never navigates the chat into.
*
* The fix is to only open the PTY once the chat tab has actually been active,
* while keeping activation **sticky** so the PTY still persists across later
* tab switches. This helper computes that latch: once `true`, it stays `true`.
*/
export function latchChatActivation(previous: boolean, isActive: boolean): boolean {
return previous || isActive;
}