Make passive monitor a non-blocking background toggle with live counts

The monitor was a blocking listen window that monopolized the app. Convert
it to an always-on background subscription that logs overheard adverts/
telemetry while the user keeps using the menu.

- Device: add subscribe_observations() as the non-blocking capture primitive;
  listen() is now a thin foreground wrapper over it. Mock streams a live
  packet burst + cadence instead of a bounded synth loop.
- MonitorService (ctx.monitor): owns the subscription, session/total counters,
  and the on/off preference; logs each packet as it arrives.
- MonitorStore: persist on/off in <config_dir>/monitor.json, default off,
  remembered between sessions and auto-resumed at launch.
- Menu: the monitor entry is now a toggle/heard-nodes control panel; the
  header shows live session/total counts and repaints ~1s via refresh_interval.
- CLI monitor gains --on/--off (preference) and keeps --seconds for a scripted
  foreground capture.
- Add Repository.observation_count(); README + tests (129 pass).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-04 13:35:43 -04:00
1 parent 923c771817
commit 1036ff52f6
9 files changed
+828 -166

No files matched your search

+25 -2
View File
@@ -8,7 +8,7 @@ never open a serial port.
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Optional
from typing import TYPE_CHECKING, Optional
from rich.console import Console
@@ -21,6 +21,9 @@ from .core.selection import resolve_device
from .persistence.logging import get_logger
from .persistence.repository import Repository
if TYPE_CHECKING:
from .services.monitor_service import MonitorService
@dataclass(slots=True)
class AppContext:
@@ -55,12 +58,30 @@ class AppContext:
selected_device: Optional[DiscoveredDevice] = None
explicit_selection: bool = False
_device: Optional[Device] = field(default=None, init=False, repr=False)
_monitor: "Optional[MonitorService]" = field(default=None, init=False, repr=False)
@property
def profile_name(self) -> Optional[str]:
"""Name of the active profile, if any."""
return self.profile.name if self.profile else None
@property
def monitor(self) -> "MonitorService":
"""Return the session's passive-monitor service, creating it on first use.
The service is built lazily so the (cheap) database read for the "total"
observation count and the on/off preference load happen only once, when
monitoring is first referenced.
"""
if self._monitor is None:
from .core.monitor_store import MonitorStore
from .services.monitor_service import MonitorService
self._monitor = MonitorService(
self, MonitorStore(self.settings.config_dir / "monitor.json")
)
return self._monitor
@property
def log(self): # type: ignore[no-untyped-def]
"""The application logger."""
@@ -105,7 +126,9 @@ class AppContext:
return self._device
async def aclose(self) -> None:
"""Disconnect the device (if connected) and close the repository."""
"""Stop monitoring, disconnect the device (if connected), and close the repo."""
if self._monitor is not None:
await self._monitor.aclose()
if self._device is not None:
await self._device.disconnect()
self._device = None