bug(webui): sustained UI flicker after returning from background during active streaming (iOS PWA) — recovery storm + /api/session blocked on models-catalog rebuild
Summary
When a session is actively streaming, switching away from the WebUI tab/PWA and switching back causes sustained rapid UI flickering (sidebar and/or transcript re-rendering in a loop) that does not settle on its own. It only stops after pressing the manual reload button on the affected session. Once the stream is idle, switch-back causes only a brief 1–2s re-render burst, which is acceptable.
Environment
- Hermes WebUI master
c296673(2026-09-25), served byserver.pybehind a reverse proxy - Client: iOS Safari / PWA standalone
- Trigger condition: the agent is streaming (tool calls firing) when the user backgrounds the page, then returns
Evidence A — multiple concurrent recovery paths fire on return
On visibilitychange → visible, at least 4 independent recovery paths run simultaneously:
- Streaming poll —
_streamingPollVisibilityHandler→renderSessionList({deferWhileInteracting:true})(sessions.js ~6397) - Sidebar session-events SSE — closed on hidden, reopened on visible;
onopen→_refreshSessionListAfterSidebarResume('visible')→refreshSessionList(force:true)(sessions.js ~6744) - Gateway SSE — same pattern →
startGatewaySSE()→sessions_changed→renderSessionList(sessions.js ~6896) - Active-session external refresh — visibility + focus hooks →
refreshActiveSessionIfExternallyUpdated('visible'|'focus')(sessions.js ~6617) - Additionally, in-flight stream recovery:
_deferStreamErrorIfPageHiddenbinds THREE listeners (visibilitychange + focus + pageshow) and each can fire_reattachOrRestoreAfterDeferredStreamError→ full transcript reattach/replay (messages.js ~2717) - PWA standalone also fires
pageshow(bfcache) → fullloadSession(sid)re-run (boot.js ~3910)
Each path fetches and force-renders. Under normal conditions this is a 1–2s burst. During streaming it compounds: every tool call emits a session-updated event → _scheduleSessionEventsRefresh({force:true, refreshActive:true}) (300ms debounce) → more sidebar re-renders on top of the recovery storm.
Evidence B — server responses are slow, so recovery fetches pile up out of order
The server's slow-request watchdog logged a metadata-only /api/session probe (messages=0) taking ~5 seconds, with the time concentrated after session load. The captured stack shows the request blocked here:
_handle_session_get → _resolve_effective_session_model_for_display → get_available_models(prefer_cache=True) → _cache_build_cv.wait_for (api/config.py ~10194)
i.e. even metadata-only session probes block for seconds whenever a models-catalog rebuild is in progress. The rebuild thread was observed running concurrently (models-catalog-rebuild → _openrouter_model_display_name → _load_model_metadata_disk_cache), and invalidate_models_cache() is called from multiple sites — so during an active session the catalog can be invalidated repeatedly, making every recovery fetch slow.
Meanwhile the stream SSE reconnect ladder (messages.js ~6884, delays 1.5s→3s→5s→8s→12s→20s) reattaches with full run-journal replay each time a probe sees active/replay_available. On iOS, backgrounding kills the EventSource; if the server is slow to first byte (queued behind the slow requests above), the reattached SSE errors again → another replay → another full transcript paint. This is the sustained-flicker loop. A manual reload breaks it because it resets state and performs a single clean reattach.
Proposed fixes (for discussion)
- Coalesce the return-to-foreground recovery into one supervisor: a single
visiblehandler schedules ONE recovery pass (sidebar + gateway + active-session probe + stream reattach), instead of 4+ independent listeners each forcing renders. - Guard stream reattach against reconnect storms: track consecutive reattach failures within a window; after N failures, back off and show "Reconnecting…" instead of replaying the journal each time.
- Don't block
/api/sessionon the models catalog:_resolve_effective_session_model_for_displaycould serve a stale-if-rebuilding cached catalog (last-good model label, rebuild async), so recovery probes stay fast even mid-rebuild. - Consider journal delta buffering while hidden, so reattach replay carries only the missed tail instead of a full replay.
Reproduction
- Open a session, send a prompt that streams for a while with frequent tool calls
- While tokens/tool cards are actively rendering, switch to another app (or another Safari tab)
- Return after 10–30s
- Observe: sidebar/transcript flicker continuously; pressing the session reload button stops it
- Contrast: same switch-back with an idle session → only a brief single re-render
Source: nesquena/hermes-webui