Files
agent-zero/helpers/state_monitor.py.dox.md
Alessandro a3eb21299b Fix chunky response streaming in the WebUI
Reduce state-push coalescing from 100 ms to 25 ms so transport batching
does not combine successive response updates into large visible bursts.
The existing per-connection timer and in-flight push guards still bound
work and coalesce dirty state.

Skip inner Scroller measurements for root response bodies, which grow
with the chat viewport. The existing chat scroller owns following, and
the collapse handler still resets collapsed content. Other message bodies
retain their scrolling behavior. Update the owning docs and regressions.

Validation:
- 42 focused streaming, state-sync, snapshot and WebUI tests passed.
- 31 WebSocket security, CSRF and handler tests passed.
- Live DeepSeek and Opus checks showed roughly halved median visible
  update intervals; response scrolling and collapse/expand were checked.

Live browser checks used headless Chromium. Long Opus responses still
show occasional dropped frames as more frequent updates increase render work.
2026-10-29 20:30:12 +01:00

3.9 KiB

state_monitor.py DOX

Purpose

  • Own the state_monitor.py helper module.
  • This module tracks dirty state and connection projections for WebUI sync.
  • Keep this file-level DOX profile synchronized with state_monitor.py because this directory is intentionally flat.

Ownership

  • state_monitor.py owns the runtime implementation.
  • state_monitor.py.dox.md owns durable notes about responsibilities, contracts, side effects, and verification for that implementation.
  • Classes:
  • ConnectionProjection (no explicit base class)
  • StateMonitor (no explicit base class)
    • bind_manager(self, manager: 'WsManager', handler_id: str | None=...) -> None
    • register_sid(self, namespace: str, sid: str) -> None
    • unregister_sid(self, namespace: str, sid: str) -> None
    • mark_dirty_all(self, reason: str | None=...) -> None
    • mark_dirty_for_context(self, context_id: str, reason: str | None=..., include_collections: bool=...) -> None
    • update_projection(self, namespace: str, sid: str, request: StateRequestV1, seq_base: int) -> None
    • mark_dirty(self, namespace: str, sid: str, reason: str | None=..., wave_id: str | None=..., include_collections: bool=...) -> None
  • Top-level functions:
  • get_state_monitor() -> StateMonitor
  • _reset_state_monitor_for_testing() -> None
  • Notable constants/configuration names: _STATE_MONITOR_HOLDER, _STATE_MONITOR_LOCK.

Runtime Contracts

  • Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
  • Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
  • Observed side-effect areas: filesystem deletion, WebSocket state, settings/state persistence, scheduler state.
  • Imported dependency areas include: __future__, asyncio, dataclasses, helpers, helpers.print_style, helpers.state_snapshot, helpers.ws, helpers.ws_manager, os, threading, time, typing.

Key Concepts

  • Important called helpers/classes observed in the source: threading.RLock, field, ws_debug, _ws_debug_enabled, context_id.strip, loop.call_soon_threadsafe, self._schedule_debounce_on_loop, asyncio.get_running_loop, asyncio.current_task, loop.is_closed, self._debounce_handles.pop, self._push_tasks.pop, self._projections.pop, self.mark_dirty, self._mark_dirty_on_loop, runtime.is_development, loop.call_later, StateMonitor, ConnectionProjection, handle.cancel.
  • The default monitor coalesces dirty waves into at most one state push per 25 ms per connection without postponing an already scheduled push. Keep this window below the agent's response-stream cadence so transport batching does not turn live text into large bursts.
  • Collection-dirty versions are coalesced independently from ordinary stream dirties. Only clients that explicitly negotiate collections_delta may receive contexts: null and tasks: null; legacy clients remain on full snapshots.
  • Keep request/response, tool, or helper semantics documented here at the same time as source changes.

Work Guidance

  • Preserve public helper APIs used by core code and plugins unless every caller is updated.
  • Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded.
  • Prefer adding cohesive helper functions here only when behavior is reused across modules.

Verification

  • Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers.
  • Related tests observed by source search:
    • tests/test_model_config_api_keys.py
    • tests/test_multi_tab_isolation.py
    • tests/test_state_monitor.py
    • tests/test_state_sync_handler.py
    • tests/test_state_sync_welcome_screen.py
    • tests/test_ws_handlers.py

Child DOX Index

No child DOX files.