diff --git a/meshterm/services/device_state.py b/meshterm/services/device_state.py index b3d344a..d898547 100644 --- a/meshterm/services/device_state.py +++ b/meshterm/services/device_state.py @@ -187,6 +187,33 @@ class DeviceState: self._channels = await read_channel_slots(device) return self._channels + # -- prewarm (fill the slow caches in the background, off the navigation path) -- + + def prewarm(self) -> None: + """Warm the slow caches (contacts, channel slots) in the background after connect. + + Called once the session's link is up (see :func:`meshterm.ui.menu._resume_monitor`) so + the first screen that reads them — Chat, Trace, the Dashboard — is served from cache + instantly, rather than paying the round-trips in the navigation path where the user is + waiting on the screen to open. It folds the two unavoidable first reads into one quiet + wait behind the menu instead of surfacing them on the first open. + + The work runs as a tracked background task: **sequential** (never gathered — concurrent + reads collide on the BLE UART; see the module note), best-effort (a failure just leaves + the cache cold for a normal lazy fetch later), and cancelled on :meth:`reset` / + :meth:`aclose`. If a getter is reached before this finishes, it awaits the *same* + in-flight fetch — the per-fetch locks dedupe — so prewarming never doubles a read. + """ + self._spawn(self._prewarm()) + + async def _prewarm(self) -> None: + """Fetch the slow caches one after another, swallowing failures (best-effort warm).""" + for label, fetch in (("contacts", self.contacts), ("channel slots", self.channel_slots)): + try: + await fetch() + except Exception as exc: # noqa: BLE001 - a warm miss just falls back to a lazy fetch + self._ctx.log.debug("devstate: prewarm of %s failed: %s", label, exc) + # -- invalidation (called by the code that writes device state) ------------ def invalidate_contacts(self) -> None: diff --git a/meshterm/ui/menu.py b/meshterm/ui/menu.py index 2551ccf..b64810f 100644 --- a/meshterm/ui/menu.py +++ b/meshterm/ui/menu.py @@ -612,6 +612,13 @@ async def _resume_monitor(ctx: AppContext) -> None: await ctx.chat.start() except Exception: # noqa: BLE001 - surface via the header, don't crash the menu pass + # Warm the slow session caches (contacts, the channel-slot probe) behind the menu now that + # the link is up, so the first Chat/Trace/Dashboard open is served from cache instead of + # paying those round-trips in the navigation path — one quiet wait after login rather than a + # stall on the first open (see DeviceState.prewarm). Only with a live link: a deferred + # connect (connect_on_start off) returns above and warms lazily on first use instead. + if ctx.is_connected: + ctx.devstate.prewarm() @asynccontextmanager diff --git a/tests/test_device_state.py b/tests/test_device_state.py index 9d09f0b..f5bf7cb 100644 --- a/tests/test_device_state.py +++ b/tests/test_device_state.py @@ -158,3 +158,24 @@ def test_reset_clears_everything() -> None: assert dev.self_info_calls == 2 assert dev.contacts_calls == 2 assert dev.mode_calls == 2 + + +def test_prewarm_fills_the_slow_caches_off_the_read_path() -> None: + """prewarm() warms contacts and channels in the background so the first read hits no wire.""" + dev = FakeDevice() + ds = _devstate(dev) + + async def run() -> None: + ds.prewarm() + await asyncio.gather(*list(ds._tasks)) # let the background warm finish + # Both slow caches were filled by the prewarm: contacts once, the channel probe once + # (idx 0 ok, idx 1 rejected). + assert dev.contacts_calls == 1 + assert dev.channel_calls == 2 + # A screen opening now is served from cache — no additional round-trips. + await ds.contacts() + await ds.channel_slots() + assert dev.contacts_calls == 1 + assert dev.channel_calls == 2 + + asyncio.run(run())