From 188545ceceb209de48fb91d42618cda6e3199c0d Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Tue, 6 Oct 2026 23:48:50 -0400 Subject: [PATCH] The area drawing of a record puts a contact's position first The Trophy case drew each hop of a walk at the first position that it found, and it looked at the heard adverts before the contacts. The trace scoring and the name resolver do the opposite: a contact wins. Thus a hop could get its name and its colour from one node and its place on the drawing from another. On real data, the one-byte hop 3d is named YUL-Cartierville (a contact with a position). The heard history has no position for that repeater, so the old lookup took the next heard node that starts with 3d, a node in St-Jean, 38 km away. Seven of 43 records drew that pin in St-Jean. The lookup is now node_geo_lookup, a module function with tests. The contacts are first, the same as in the scoring and the resolver. Co-Authored-By: Claude Opus 5.5 (1M context) --- meshterm/ui/records_screen.py | 119 +++++++++++++++++++++------------- tests/test_records_screen.py | 39 +++++++++++ 2 files changed, 113 insertions(+), 45 deletions(-) diff --git a/meshterm/ui/records_screen.py b/meshterm/ui/records_screen.py index 83b2950..cc416e5 100644 --- a/meshterm/ui/records_screen.py +++ b/meshterm/ui/records_screen.py @@ -38,7 +38,7 @@ Nothing here transmits: this screen reads the boards that the trace tools filled from __future__ import annotations import textwrap -from collections.abc import Sequence +from collections.abc import Callable, Iterable, Sequence from dataclasses import dataclass from typing import TYPE_CHECKING @@ -71,6 +71,7 @@ from .widgets import ( if TYPE_CHECKING: from ..context import AppContext + from ..core.models import Contact, HeardNode #: The width at which the descriptions of the disciplines wrap. It is well inside 72 #: cells, so that the text is the same at each terminal width. @@ -789,6 +790,75 @@ class RecordScreen(Screen): return lines +#: The position and the node type of a route node. Each one can be unknown. +NodeGeo = tuple[tuple[float, float] | None, int | None] + + +def node_geo_lookup( + heard: Iterable[HeardNode], contacts: Iterable[Contact] +) -> Callable[[str], NodeGeo]: + """A lookup of the best-known position and node type of a route node. + + The area drawing of a record must use the positions that the score measured. The + trace tools score a walk with the adverts that we heard, and with the contacts put + over them (``build_positions`` in :mod:`~meshterm.ui.trace_screen`). The name resolver + also puts the contacts first (:func:`~meshterm.services.trace_runner.make_node_resolver`). + Thus the contacts are first in the list here too, and the first entry that matches + wins. A contact with no position, or at 0,0, does not hide the heard position. + + A record stores canonical ids. Thus the lookup matches them the same as the resolver + does: a prefix on either side. + + Args: + heard: The heard nodes of the history (:meth:`Repository.heard_nodes`). + contacts: The contacts of the device. + + Returns: + A function from a node id to ``(position, node type)``. The function caches each + answer, because the boards ask for the same route nodes many times (each hop of + each record, and records that share hops). + """ + entries: list[tuple[str, tuple[float, float] | None, int | None]] = [] + for contact in contacts: + ident = (contact.public_key or contact.key_prefix or "").lower().removeprefix("0x") + if not ident: + continue + pos = ( + (contact.lat, contact.lon) + if (contact.has_location and (contact.lat or contact.lon)) + else None + ) + entries.append((ident, pos, contact.node_type)) + for node in heard: + if not node.node: + continue + pos = (node.lat, node.lon) if node.has_location else None + entries.append((node.node.lower().removeprefix("0x"), pos, node.node_type)) + + cache: dict[str, NodeGeo] = {} + + def lookup(node_id: str) -> NodeGeo: + cached = cache.get(node_id) + if cached is not None: + return cached + needle = node_id.lower().removeprefix("0x") + pos: tuple[float, float] | None = None + ntype: int | None = None + for ident, epos, etype in entries: + if not (ident.startswith(needle) or needle.startswith(ident)): + continue + if pos is None: + pos = epos + if ntype is None: + ntype = etype + if pos is not None and ntype is not None: + break + cache[node_id] = (pos, ntype) + return pos, ntype + + return lookup + + async def open_records(ctx: AppContext) -> dict: """Open the Trophy case browser and run it until the user leaves it. @@ -841,50 +911,9 @@ async def open_records(ctx: AppContext) -> dict: mark_lane = discipline_lane() # The node positions and types for the area drawing of a record. They are collected - # live, the same as the trace tools collect them to score a walk: the adverts that we - # heard, with the contacts put over them. A record stores canonical ids, so match them - # the same as the resolver does (a prefix on either side). - node_entries: list[tuple[str, tuple[float, float] | None, int | None]] = [] - for heard in ctx.repo.heard_nodes(): - if not heard.node: - continue - pos = (heard.lat, heard.lon) if heard.has_location else None - node_entries.append((heard.node.lower().removeprefix("0x"), pos, heard.node_type)) - for contact in contacts: - ident = (contact.public_key or contact.key_prefix or "").lower().removeprefix("0x") - if not ident: - continue - pos = ( - (contact.lat, contact.lon) - if (contact.has_location and (contact.lat or contact.lon)) - else None - ) - node_entries.append((ident, pos, contact.node_type)) - - # Cached: the boards ask for the same route nodes many times (each hop of each record, - # and records that share hops). The entry list does not change while the screen is - # open. Thus the prefix scan occurs only once for each different id. - geo_memo: dict[str, tuple[tuple[float, float] | None, int | None]] = {} - - def node_geo(node_id: str) -> tuple[tuple[float, float] | None, int | None]: - """The best-known position and type of a route node, from the heard and contact data.""" - cached = geo_memo.get(node_id) - if cached is not None: - return cached - needle = node_id.lower().removeprefix("0x") - pos: tuple[float, float] | None = None - ntype: int | None = None - for ident, epos, etype in node_entries: - if not (ident.startswith(needle) or needle.startswith(ident)): - continue - if pos is None: - pos = epos - if ntype is None: - ntype = etype - if pos is not None and ntype is not None: - break - geo_memo[node_id] = (pos, ntype) - return pos, ntype + # live, the same as the trace tools collect them to score a walk (refer to + # node_geo_lookup). The entry list does not change while the screen is open. + node_geo = node_geo_lookup(ctx.repo.heard_nodes(), contacts) def walk_drawing( record: DiscoveredPath, diff --git a/tests/test_records_screen.py b/tests/test_records_screen.py index c908b5b..fc8298c 100644 --- a/tests/test_records_screen.py +++ b/tests/test_records_screen.py @@ -22,6 +22,7 @@ from meshterm.context import AppContext from meshterm.core.admin_store import AdminStore from meshterm.core.config import Settings from meshterm.core.device_store import DeviceStore +from meshterm.core.models import NODE_TYPE_REPEATER, Contact, HeardNode from meshterm.persistence.repository import DiscoveredPath, Repository from meshterm.services.records import CATEGORIES, CATEGORY_BY_ID from meshterm.ui.records_screen import ( @@ -29,6 +30,7 @@ from meshterm.ui.records_screen import ( WalkVertex, discipline_label, discipline_lane, + node_geo_lookup, open_records, ) from meshterm.ui.surface import TuiUi @@ -496,6 +498,43 @@ def test_record_area_tab_says_so_when_too_narrow_to_draw() -> None: assert "no room" in body # cut at the edge like each row, but it is there +_SEEN = datetime(2026, 10, 1, tzinfo=timezone.utc) + + +def _heard(node: str, lat: float | None, lon: float | None) -> HeardNode: + return HeardNode( + node=node, name=None, count=1, median_snr=None, best_snr=None, last_rssi=None, + last_seen=_SEEN, lat=lat, lon=lon, node_type=NODE_TYPE_REPEATER, + ) # fmt: skip + + +def test_the_area_drawing_puts_a_contact_position_over_a_heard_one() -> None: + """A contact's position wins over a heard advert's position. + + The trace tools score a walk with the contacts put over the heard adverts, and the name + resolver also puts the contacts first. The area drawing once took the heard position + first. Thus a node whose heard advert and contact disagreed was drawn at a place that + the score did not measure. + """ + heard = [_heard(HUB_ID, 45.40, -73.90)] + contacts = [Contact(name="Hub", public_key=HUB_ID + "0" * 52, lat=45.55, lon=-73.60)] + pos, _ = node_geo_lookup(heard, contacts)(HUB_ID) + assert pos == (45.55, -73.60) + + +def test_the_area_drawing_falls_back_to_the_heard_position() -> None: + """A contact with no position, or at 0,0, leaves the heard position in use.""" + heard = [_heard(HUB_ID, 45.40, -73.90), _heard(FAR_ID, 46.10, -74.20)] + contacts = [ + Contact(name="Hub", public_key=HUB_ID + "0" * 52), + Contact(name="Far", key_prefix=FAR_ID, lat=0.0, lon=0.0), + ] + lookup = node_geo_lookup(heard, contacts) + assert lookup(HUB_ID) == ((45.40, -73.90), NODE_TYPE_REPEATER) + assert lookup(FAR_ID)[0] == (46.10, -74.20) + assert lookup("0123456789ab") == (None, None) + + def test_record_without_a_shape_has_no_area_tab() -> None: """If no hop has a position, there is no Area tab. Info and Route still switch.""" screen = _dialog(_record())