| 1 | # state_monitor.py DOX |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | - Own the `state_monitor.py` helper module. |
| 6 | - This module tracks dirty state and connection projections for WebUI sync. |
| 7 | - Keep this file-level DOX profile synchronized with `state_monitor.py` because this directory is intentionally flat. |
| 8 | |
| 9 | ## Ownership |
| 10 | |
| 11 | - `state_monitor.py` owns the runtime implementation. |
| 12 | - `state_monitor.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation. |
| 13 | - Classes: |
| 14 | - `ConnectionProjection` (no explicit base class) |
| 15 | - `StateMonitor` (no explicit base class) |
| 16 | - `bind_manager(self, manager: 'WsManager', handler_id: str | None=...) -> None` |
| 17 | - `register_sid(self, namespace: str, sid: str) -> None` |
| 18 | - `unregister_sid(self, namespace: str, sid: str) -> None` |
| 19 | - `mark_dirty_all(self, reason: str | None=...) -> None` |
| 20 | - `mark_dirty_for_context(self, context_id: str, reason: str | None=..., include_collections: bool=...) -> None` |
| 21 | - `update_projection(self, namespace: str, sid: str, request: StateRequestV1, seq_base: int) -> None` |
| 22 | - `mark_dirty(self, namespace: str, sid: str, reason: str | None=..., wave_id: str | None=..., include_collections: bool=...) -> None` |
| 23 | - Top-level functions: |
| 24 | - `get_state_monitor() -> StateMonitor` |
| 25 | - `_reset_state_monitor_for_testing() -> None` |
| 26 | - Notable constants/configuration names: `_STATE_MONITOR_HOLDER`, `_STATE_MONITOR_LOCK`. |
| 27 | |
| 28 | ## Runtime Contracts |
| 29 | |
| 30 | - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together. |
| 31 | - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change. |
| 32 | - Observed side-effect areas: filesystem deletion, WebSocket state, settings/state persistence, scheduler state. |
| 33 | - Imported dependency areas include: `__future__`, `asyncio`, `dataclasses`, `helpers`, `helpers.print_style`, `helpers.state_snapshot`, `helpers.ws`, `helpers.ws_manager`, `os`, `threading`, `time`, `typing`. |
| 34 | |
| 35 | ## Key Concepts |
| 36 | |
| 37 | - 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`. |
| 38 | - The default monitor coalesces dirty waves into at most one state push per 100 ms per connection without postponing an already scheduled push. |
| 39 | - 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. |
| 40 | - Keep request/response, tool, or helper semantics documented here at the same time as source changes. |
| 41 | |
| 42 | ## Work Guidance |
| 43 | |
| 44 | - Preserve public helper APIs used by core code and plugins unless every caller is updated. |
| 45 | - Keep path, auth, secret, persistence, network, and subprocess behavior explicit and bounded. |
| 46 | - Prefer adding cohesive helper functions here only when behavior is reused across modules. |
| 47 | |
| 48 | ## Verification |
| 49 | |
| 50 | - Run targeted tests for changed helper behavior; run security regressions for auth, filesystem, WebSocket, tunnel, upload, or secret-handling helpers. |
| 51 | - Related tests observed by source search: |
| 52 | - `tests/test_model_config_api_keys.py` |
| 53 | - `tests/test_multi_tab_isolation.py` |
| 54 | - `tests/test_state_monitor.py` |
| 55 | - `tests/test_state_sync_handler.py` |
| 56 | - `tests/test_state_sync_welcome_screen.py` |
| 57 | - `tests/test_ws_handlers.py` |
| 58 | |
| 59 | ## Child DOX Index |
| 60 | |
| 61 | No child DOX files. |