fix(web/claw): keep the stream watchdog quiet on a tab switch

A hidden tab throttles timers and can stall the SSE reader. The 45s
silence watchdog kept counting that time, aborted the stream, and pushed
"the event stream went quiet" on every tab switch even though the
connection was fine.

The watchdog now pauses while the page is hidden and re-arms from zero
when it becomes visible. The attempt also remembers whether it crossed a
hidden period: if the browser held or dropped the connection there, the
reconnect stays silent. A keep-alive or frame clears that mark, so a
stream that really dies while visible still reports itself.

Tested in headless Chromium against the dev mock: 50s hidden, then
visible, no notice, and the stream went back to live.
This commit is contained in:
vhaudiquet committed 2026-11-18 23:59:01 +01:00
1 parent 51456f5b4c
commit ed645d1faf
1 file changed
+40 -4
+40 -4
View File
@@ -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<string | null>): ClawApi {
* notice says so instead of naming an opaque abort error. */
let timedOut = false
let silence: ReturnType<typeof setTimeout> | 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<string | null>): 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<string | null>): 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<string | null>): 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<string | null>): 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<string | null>): 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++)
// 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<string | null>): ClawApi {
close() {
closed = true
stopSilence()
if (typeof document !== 'undefined') {
document.removeEventListener('visibilitychange', visibility)
}
current?.abort()
},
}