From 5fdfb70bce2f19d6b232f95eadc3ba446743190c Mon Sep 17 00:00:00 2001 From: nesquena-hermes Date: Thu, 11 Jun 2026 19:10:38 +0000 Subject: [PATCH] fix(#3996): reopen per-session SSE on tab re-show (reopen-bug fix) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3996 closes the gateway + per-session SSE on hidden tabs to free HTTP/1.1 connection-pool slots (#3992). But the per-session-stream visibility hook guarded its reopen on _sessionStreamSessionId, which stopSessionStream() nulls — so the per-session SSE closed on hide but never reopened on re-show (regressing live-stream reattach). Capture the id into a dedicated _sessionStreamHiddenSid before the close and reopen from it on visibility return. Gateway hook (sessions.js) was already correct (reopens unconditionally + re-guards internally). --- static/messages.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/static/messages.js b/static/messages.js index cc3f0202..c69efea4 100644 --- a/static/messages.js +++ b/static/messages.js @@ -4202,6 +4202,9 @@ function stopApprovalPolling() { let _sessionEventSource = null; let _sessionStreamSessionId = null; let _sessionStreamReconnectTimer = null; +// Holds the session id across a hidden-tab close so the visibility handler can +// reopen the per-session SSE on re-show (stopSessionStream nulls _sessionStreamSessionId). +let _sessionStreamHiddenSid = null; function startSessionStream(sid) { if (!sid) return; @@ -4210,13 +4213,19 @@ function startSessionStream(sid) { if (_sessionStreamSessionId === sid && _sessionEventSource) return; stopSessionStream(); _sessionStreamSessionId = sid; - // Visibility hook (install once) — mirror ensureSessionEventsSSE() pattern + // Visibility hook (install once) — mirror ensureSessionEventsSSE() pattern. + // Capture the active session id into a dedicated var BEFORE closing, because + // stopSessionStream() nulls _sessionStreamSessionId — so the reopen path can't + // rely on it (that was the bug: the stream never reopened on tab re-show). if (typeof document !== 'undefined' && !document._hermesSessionStreamVisibilityHook) { document.addEventListener('visibilitychange', () => { if (document.hidden) { + _sessionStreamHiddenSid = _sessionStreamSessionId; stopSessionStream(); - } else if (_sessionStreamSessionId) { - void startSessionStream(_sessionStreamSessionId); + } else if (_sessionStreamHiddenSid) { + const resumeSid = _sessionStreamHiddenSid; + _sessionStreamHiddenSid = null; + void startSessionStream(resumeSid); } }); document._hermesSessionStreamVisibilityHook = true;