diff --git a/web/claw/app/composables/useClaw.ts b/web/claw/app/composables/useClaw.ts index 31f1bc6..65bd86b 100644 --- a/web/claw/app/composables/useClaw.ts +++ b/web/claw/app/composables/useClaw.ts @@ -268,7 +268,9 @@ const INTERACTIVE_PREFIX = 'interactive:' * connection and reconnects: the claw sends a keep-alive comment every * fifteen seconds (hub.rs), so three missed ones means the path is * black-holed — a half-open TCP after a laptop sleep shows the same - * 'live' badge a healthy stream does without this check. */ + * 'live' badge a healthy stream does without this check. The watchdog + * is paused while the tab is hidden: browsers throttle timers and can + * stall the reader there, and a tab switch is not a dead stream. */ const STREAM_SILENCE_TIMEOUT_MS = 45000 /** Interactive keys are the only ones message ingest accepts (http.rs `messages`). */ @@ -684,11 +686,19 @@ export function useClaw(token: Ref): ClawApi { * notice says so instead of naming an opaque abort error. */ let timedOut = false let silence: ReturnType | null = null + const isHidden = (): boolean => + typeof document !== 'undefined' && document.hidden + /** Whether the attempt in flight crossed a hidden tab: a drop there + * is a backgrounded connection, not a stream the user needs a notice + * about. */ + let hiddenDuringAttempt = isHidden() const rearmSilence = (): void => { if (silence !== null) clearTimeout(silence) + silence = null + if (closed || isHidden()) return silence = setTimeout(() => { - if (closed) return + if (closed || isHidden()) return timedOut = true current?.abort() }, STREAM_SILENCE_TIMEOUT_MS) @@ -697,6 +707,23 @@ export function useClaw(token: Ref): ClawApi { if (silence !== null) clearTimeout(silence) silence = null } + /** A frame or keep-alive proved this attempt alive: from here on a + * drop is real, even if the tab was hidden earlier. */ + const noteActivity = (): void => { + hiddenDuringAttempt = false + rearmSilence() + } + const visibility = (): void => { + if (isHidden()) { + hiddenDuringAttempt = true + stopSilence() + return + } + if (current !== null && !current.signal.aborted) rearmSilence() + } + if (typeof document !== 'undefined') { + document.addEventListener('visibilitychange', visibility) + } const status = (next: StreamStatus) => { if (!closed) handlers.onStatus?.(next) @@ -708,6 +735,7 @@ export function useClaw(token: Ref): ClawApi { const controller = new AbortController() current = controller timedOut = false + hiddenDuringAttempt = isHidden() rearmSilence() try { const resume = cursor === null ? '' : `?since=${cursor}` @@ -729,7 +757,7 @@ export function useClaw(token: Ref): ClawApi { await readFrames( response.body, (data, id) => { - rearmSilence() + noteActivity() const event = parseTurnEvent(data) if (!event) return const seq = id !== null && /^\d+$/.test(id) ? Number(id) : null @@ -739,7 +767,7 @@ export function useClaw(token: Ref): ClawApi { (comment) => { // Keep-alives come through here too; each one proves the // path alive. - rearmSilence() + noteActivity() // The record lost what this mirror holds: drop the cursor // so the next open asks for the tail, and let the consumer // drop its items before the tail that already followed @@ -765,11 +793,16 @@ export function useClaw(token: Ref): ClawApi { // Backoff caps at 15s: a live stream of a running turn is the // UI's whole job, so retries stay eager but not tight-looped. const backoff = Math.min(15000, 1000 * 2 ** attempt++) - handlers.onNotice?.( - timedOut - ? `the event stream went quiet (no keep-alive for ${STREAM_SILENCE_TIMEOUT_MS / 1000}s); reconnecting in ${backoff / 1000}s` - : `event stream dropped (${cause instanceof Error ? cause.message : String(cause)}); retrying in ${backoff / 1000}s`, - ) + // A connection that crossed a hidden tab reconnects quietly: + // the browser held it while the page was in the background, + // and the operator did not just lose a live stream. + if (!isHidden() && !hiddenDuringAttempt) { + handlers.onNotice?.( + timedOut + ? `the event stream went quiet (no keep-alive for ${STREAM_SILENCE_TIMEOUT_MS / 1000}s); reconnecting in ${backoff / 1000}s` + : `event stream dropped (${cause instanceof Error ? cause.message : String(cause)}); retrying in ${backoff / 1000}s`, + ) + } status('reconnecting') await new Promise((resolve) => setTimeout(resolve, backoff)) } finally { @@ -783,6 +816,9 @@ export function useClaw(token: Ref): ClawApi { close() { closed = true stopSilence() + if (typeof document !== 'undefined') { + document.removeEventListener('visibilitychange', visibility) + } current?.abort() }, }