The location picker opens at once instead of waiting on the radio for contacts
Pick location on map gathered its surrounding nodes before opening, and that meant reading the contact list from the radio whenever the session hadn't cached it. A companion refusing that read (ERR_CODE_BAD_STATE, which the log shows all session) held the picker closed through every retry, twenty-odd seconds, only to open with no contacts anyway. Our own position waited the same way after Device config dropped its cached self-info. The picker now opens on what is already in hand: cached contacts and self-info (DeviceState.peek_contacts / peek_self_info) and our own history, via gather_markers(wait=False). Whatever the cache lacked is fetched behind the open map and joins it through MapScreen.set_markers when the radio answers. The read is never cancelled, so a fetch another screen shares survives the picker closing. The Map tool and the node page keep waiting, unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
74eabff25f
commit
e5231687de
4 files changed
+166
-12
No files matched your search
@@ -132,6 +132,16 @@ class DeviceState:
|
||||
self._spawn(self._refresh_contacts_quietly())
|
||||
return self._contacts
|
||||
|
||||
def peek_contacts(self) -> list[Contact] | None:
|
||||
"""The contacts already in hand — possibly stale — or ``None``, never a radio read.
|
||||
|
||||
For a surface that only wants them as *context* and must not wait for them: the
|
||||
location picker's surrounding nodes. A companion can refuse the contacts read for a
|
||||
stretch (``ERR_CODE_BAD_STATE``), and :meth:`contacts` then blocks through every retry
|
||||
of it — twenty-odd seconds — before giving up.
|
||||
"""
|
||||
return self._contacts
|
||||
|
||||
async def _fetch_contacts(self, *, force: bool = False) -> list[Contact]:
|
||||
"""Read the contacts table from the device and cache it (blocking, deduplicated).
|
||||
|
||||
@@ -251,6 +261,13 @@ class DeviceState:
|
||||
self._self_info = dict(await device.get_self_info())
|
||||
return self._self_info
|
||||
|
||||
def peek_self_info(self) -> dict | None:
|
||||
"""The self-info already in hand, or ``None`` — never a radio read.
|
||||
|
||||
The :meth:`peek_contacts` of our own node: context for a surface that must not wait.
|
||||
"""
|
||||
return self._self_info
|
||||
|
||||
async def path_hash_mode(self) -> int:
|
||||
"""Return the device's path-hash routing mode, cached for the session.
|
||||
|
||||
|
||||
+25
-7
@@ -117,7 +117,7 @@ class MapTool(Tool):
|
||||
"""
|
||||
|
||||
|
||||
async def gather_markers(ctx: AppContext) -> list[MapMarker]:
|
||||
async def gather_markers(ctx: AppContext, *, wait: bool = True) -> list[MapMarker]:
|
||||
"""Collect every located node to plot: the device's contacts, plus our own node.
|
||||
|
||||
The companion's **contact list** is the authoritative source for a node's name,
|
||||
@@ -126,8 +126,16 @@ async def gather_markers(ctx: AppContext) -> list[MapMarker]:
|
||||
advert. So contacts drive the markers, and each contact is enriched with signal
|
||||
detail from the observations when we've overheard it directly.
|
||||
|
||||
Shared by the ``map`` tool and the config editor's pick-a-location map, so both
|
||||
show the same mesh.
|
||||
Shared by the ``map`` tool, the node page's preview and the editors' pick-a-location
|
||||
map, so all of them show the same mesh.
|
||||
|
||||
Args:
|
||||
ctx: Shared application context.
|
||||
wait: Whether to wait on the radio for contacts and our own position the session
|
||||
has not cached yet. ``False`` builds the markers from what is already in hand —
|
||||
the cache and our own history — and never makes a round trip, for a surface
|
||||
whose nodes are only context (the location picker): a companion refusing the
|
||||
contacts read can otherwise hold it closed for twenty-odd seconds.
|
||||
"""
|
||||
from ..ui.map_render import MapMarker
|
||||
|
||||
@@ -135,7 +143,8 @@ async def gather_markers(ctx: AppContext) -> list[MapMarker]:
|
||||
markers: list[MapMarker] = []
|
||||
seen: set[str] = set()
|
||||
|
||||
for contact in await _contacts(ctx):
|
||||
contacts = await _contacts(ctx) if wait else (ctx.devstate.peek_contacts() or [])
|
||||
for contact in contacts:
|
||||
if not contact.has_location or not usable_fix(contact.lat, contact.lon):
|
||||
continue
|
||||
key = contact.key_prefix or (contact.public_key or "")[:12]
|
||||
@@ -169,7 +178,11 @@ async def gather_markers(ctx: AppContext) -> list[MapMarker]:
|
||||
)
|
||||
)
|
||||
|
||||
self_marker = await _self_marker(ctx)
|
||||
if wait:
|
||||
self_marker = await _self_marker(ctx)
|
||||
else:
|
||||
info = ctx.devstate.peek_self_info()
|
||||
self_marker = _marker_for_self(info) if info is not None else None
|
||||
if self_marker is not None:
|
||||
markers.append(self_marker)
|
||||
return markers
|
||||
@@ -185,12 +198,17 @@ async def _contacts(ctx: AppContext) -> list:
|
||||
|
||||
async def _self_marker(ctx: AppContext) -> MapMarker | None:
|
||||
"""Build a marker for our own node from the device, if its location is known."""
|
||||
from ..ui.map_render import MapMarker
|
||||
|
||||
try:
|
||||
info = await ctx.devstate.self_info()
|
||||
except Exception: # noqa: BLE001 - the map is useful without our own position
|
||||
return None
|
||||
return _marker_for_self(info)
|
||||
|
||||
|
||||
def _marker_for_self(info: dict) -> MapMarker | None:
|
||||
"""Our own node's marker from a self-info payload, or ``None`` where it has no fix."""
|
||||
from ..ui.map_render import MapMarker
|
||||
|
||||
lat, lon = _as_float(info.get("adv_lat")), _as_float(info.get("adv_lon"))
|
||||
if lat is None or lon is None or not usable_fix(lat, lon):
|
||||
return None # a device with no fix reports 0/0 (or nonsense out-of-range)
|
||||
|
||||
@@ -307,6 +307,17 @@ class MapScreen(Screen):
|
||||
self._needs_scrub = False
|
||||
return 2 # the panel's right padding cell and its right border cell
|
||||
|
||||
def set_markers(self, markers: list[MapMarker]) -> None:
|
||||
"""Replace the plotted nodes on the open map — nodes that arrived after it opened.
|
||||
|
||||
The drawn frame is for the old overlay, so it stops being served; until the new one
|
||||
lands, the nodes are drawn over that frame's ground rather than over black (see
|
||||
:meth:`_ground`). The view stays where it is: the reader may already have moved it.
|
||||
"""
|
||||
self._markers = markers
|
||||
self._frame_key = None
|
||||
self._session.invalidate()
|
||||
|
||||
def _query_echo(self) -> bool:
|
||||
"""Whether this paint echoes the find query over the canvas's bottom row.
|
||||
|
||||
@@ -1232,10 +1243,14 @@ async def pick_location(
|
||||
) -> tuple[float, float] | None:
|
||||
"""Open the full-screen map as a coordinate picker; return ``(lat, lon)`` or ``None``.
|
||||
|
||||
Gathers the mesh's located nodes for context (best-effort — an unreachable radio just
|
||||
means a barer map), warms the tile source off the event loop, then runs a
|
||||
:class:`LocationPickScreen` until the user commits a spot with Enter or backs out
|
||||
with Esc.
|
||||
Opens on the mesh's located nodes *already in hand* — the session's cached contacts and
|
||||
position, and our own history — and never waits on the radio for them: they are context
|
||||
for the pick, not part of it, and a companion refusing the contacts read (some do, for a
|
||||
stretch) held the picker closed through every retry of it, twenty-odd seconds. Whatever
|
||||
the cache lacked is fetched behind the open map and joins it when the radio answers; the
|
||||
read is never cancelled, so a shared fetch another screen is waiting on survives the
|
||||
picker closing. Then runs a :class:`LocationPickScreen` until the user commits a spot
|
||||
with Enter or backs out with Esc.
|
||||
|
||||
Args:
|
||||
ctx: Shared application context (must be in the interactive menu).
|
||||
@@ -1254,16 +1269,32 @@ async def pick_location(
|
||||
if not isinstance(ctx.ui, TuiUi): # pragma: no cover - guarded by the menu-only caller
|
||||
raise RuntimeError("the interactive map is only available in the menu")
|
||||
session = ctx.ui.session
|
||||
devstate = ctx.devstate
|
||||
in_hand = devstate.peek_contacts() is not None and devstate.peek_self_info() is not None
|
||||
try:
|
||||
markers = await gather_markers(ctx)
|
||||
markers = await gather_markers(ctx, wait=False)
|
||||
except Exception: # noqa: BLE001 - context markers are a nicety, never a requirement
|
||||
markers = []
|
||||
source = basemap_source(ctx)
|
||||
max_zoom = await asyncio.to_thread(lambda: source.max_zoom)
|
||||
screen = LocationPickScreen(session, markers, source, max_zoom, initial=initial)
|
||||
showing = True
|
||||
|
||||
async def fill_in() -> None:
|
||||
"""Add what the cache lacked once the radio answers — or never, at no cost."""
|
||||
try:
|
||||
fuller = await gather_markers(ctx)
|
||||
except Exception: # noqa: BLE001 - the picker already works without them
|
||||
return
|
||||
if showing and len(fuller) != len(screen._markers):
|
||||
screen.set_markers(fuller)
|
||||
|
||||
if not in_hand:
|
||||
asyncio.ensure_future(fill_in())
|
||||
try:
|
||||
result = await session.run_screen(screen)
|
||||
finally:
|
||||
showing = False
|
||||
# Same clean-slate repaint as open_map: the braille may have smeared the terminal.
|
||||
session.request_full_repaint()
|
||||
return None if result is CANCEL or result is None else result
|
||||
|
||||
@@ -580,6 +580,94 @@ def test_location_picker_without_nodes_or_initial_shows_the_world() -> None:
|
||||
assert screen.future.value is None
|
||||
|
||||
|
||||
def test_nodes_that_arrive_late_join_the_open_map() -> None:
|
||||
"""``set_markers`` replaces the plotted nodes and redraws, without moving the view."""
|
||||
from meshterm.ui.map_render import MapMarker
|
||||
|
||||
screen = _picker(initial=(45.5, -73.6))
|
||||
screen.render_body(80)
|
||||
view = screen._viewport
|
||||
screen.set_markers([MapMarker("Repeater", 45.51, -73.61, is_repeater=True)])
|
||||
screen.render_body(80)
|
||||
assert screen._viewport is view
|
||||
assert screen._frame_key[3] == 2 # redrawn for the new node, crosshair included
|
||||
|
||||
|
||||
async def test_markers_without_waiting_never_ask_the_radio(
|
||||
ctx: AppContext, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""The picker's markers come from what is in hand; the radio is never asked for them."""
|
||||
from meshterm.tools.map import gather_markers
|
||||
|
||||
device = await ctx.device()
|
||||
|
||||
async def _refuse(*_args: Any, **_kwargs: Any) -> Any: # pragma: no cover - must not run
|
||||
raise AssertionError("the radio was asked")
|
||||
|
||||
monkeypatch.setattr(device, "get_contacts", _refuse)
|
||||
monkeypatch.setattr(device, "get_self_info", _refuse)
|
||||
assert await gather_markers(ctx, wait=False) == [] # nothing cached, nothing heard yet
|
||||
|
||||
|
||||
async def test_the_picker_opens_while_the_radio_is_still_reading_contacts(
|
||||
ctx: AppContext, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""A contacts read that hasn't answered doesn't hold the map closed; its nodes join later.
|
||||
|
||||
Regression: a companion refusing the contacts read (``ERR_CODE_BAD_STATE``) kept the
|
||||
picker from opening through every retry of it — twenty-odd seconds.
|
||||
"""
|
||||
import asyncio
|
||||
|
||||
from meshterm.core.models import Contact
|
||||
from meshterm.ui import map_screen
|
||||
|
||||
device = await ctx.device()
|
||||
answer = asyncio.Event()
|
||||
|
||||
async def _slow_contacts() -> list:
|
||||
await answer.wait()
|
||||
return [
|
||||
Contact(
|
||||
name="Hilltop", public_key="ab" * 32, key_prefix="abababab", lat=45.51, lon=-73.61
|
||||
)
|
||||
]
|
||||
|
||||
monkeypatch.setattr(device, "get_contacts", _slow_contacts)
|
||||
|
||||
class _Session:
|
||||
def base_body_size(self) -> tuple[int, int]:
|
||||
return (72, 20)
|
||||
|
||||
def invalidate(self) -> None:
|
||||
pass
|
||||
|
||||
def request_full_repaint(self) -> None:
|
||||
pass
|
||||
|
||||
async def run_screen(self, screen: Any) -> Any:
|
||||
self.opened_with = len(screen._markers) # the map is up; the radio hasn't answered
|
||||
answer.set()
|
||||
for _ in range(200):
|
||||
await asyncio.sleep(0)
|
||||
if len(screen._markers) > self.opened_with:
|
||||
break
|
||||
self.later = len(screen._markers)
|
||||
return (45.5, -73.6)
|
||||
|
||||
class _Ui:
|
||||
def __init__(self, session: _Session) -> None:
|
||||
self.session = session
|
||||
|
||||
session = _Session()
|
||||
monkeypatch.setattr("meshterm.ui.surface.TuiUi", _Ui)
|
||||
monkeypatch.setattr(map_screen, "basemap_source", lambda _ctx: _StubSource())
|
||||
ctx.ui = _Ui(session) # type: ignore[assignment]
|
||||
assert await map_screen.pick_location(ctx) == (45.5, -73.6)
|
||||
assert session.opened_with == 0
|
||||
assert session.later == 1
|
||||
|
||||
|
||||
# -- the editor's flows against the simulator ---------------------------------------
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user