A device's contact table is finite, and a busy mesh fills it with whatever adverts arrived — mostly nodes heard once, in passing, four hops out — until there is no room left to discover anyone new. The age ladder could only ask how long ago; it had nothing to say about which of two equally quiet contacts was the one you would miss. So contacts are now rated. Six weighted terms, each normalised and summed: direct correspondence heaviest at 30, then how lately heard (25), how often (15), channel posting (12), hops (10) and distance (8). Additive rather than multiplicative, because a product lets one zero annihilate a contact that is strong everywhere else — a node advertising no position would have scored nothing however much you talk to it. The rule the whole thing rests on is that an unknown scores *neutral*. Half a real mesh advertises no location, plenty of nodes are never overheard as a relayed frame so carry no hop count, and a channel poster whose name two contacts share can be attributed to neither — the wire gives a channel frame no sender key at all. A term that cannot be computed takes the population median of the contacts that could, so the decision falls to the axes that are known. Scoring those as zero would have swept by how much metadata a node happens to broadcast. It is also why the two least reliable terms carry the two smallest weights, and why distance calibrates against this mesh's own median spread rather than a constant: 50 km is far downtown and unremarkable in a valley. A newcomer gets 25 points decaying to nothing over a fortnight, because every other term rewards accumulated evidence and a node first heard this morning is otherwise arithmetically identical to one quiet for a year. Four things are never candidates at all: a watched node, anyone you have sent a message to, a repeater whose admin login is stored, and a contact MeshTerm has never heard — that last one predates this history, and sweeping it would be purging by how long the app has been running. They still rank, since a percentile that dropped them would not be a rank against your contacts; they just consume no kept slot, so starring a node never deepens the next sweep. Only the percentile is ever displayed. A raw 47.3 means nothing without the distribution it came from and would need a legend the moment the weights were retuned. The victims are archived, not deleted: off the device, which is the scarce resource, and kept here with a stamp. Everything else was already independent of the contact row — reception history, map position, transcripts — and the store keeps the full key, so a restore is one write, which the chat screen already performs on demand when a send is rejected for an unknown recipient. merge_contacts learns to withhold them, or the union would put them straight back and the sweep would look like it did nothing. The old age ladder survives as the second section of the one picker, because "everything I haven't heard in a year" is a plain predictable operation a ranking cannot express. Both routes converge on the same preview — weakest first, with each row's percentile and the two or three facts that put it there — so an age rung reaching into contacts that rank perfectly well says so before the typed confirm rather than after. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
208 lines
10 KiB
Python
208 lines
10 KiB
Python
"""The interactive Contacts screen: the device's contacts in the shared sortable contact list.
|
|
|
|
The Time Machine picker's presentation pointed at the companion's contact table: the same
|
|
aligned ``NAME · HEARD · PKTS · KEY`` lanes, our own node pinned first, the same Ctrl+arrow
|
|
sort (now including the key column) and type-to-filter — one contact list app-wide, whatever
|
|
the data source (see :mod:`~meshterm.ui.contactlist`). Enter opens the highlighted node's
|
|
full-screen Node detail page (:mod:`~meshterm.ui.node_detail_screen`) — its identity, a
|
|
location preview, the observed routes to it, and the ways in (trace, map, time machine) —
|
|
and backing out of that page returns to the list right where it stood. The one-shot CLI
|
|
(``meshterm contacts --sort …``) still renders the static
|
|
:func:`~meshterm.ui.widgets.contacts_table`; only the menu gets the live list.
|
|
|
|
Below the contacts sits the list's own maintenance action — **Purge contacts** — the bulk
|
|
counterpart, which exists because a companion's contact table is finite and a busy mesh
|
|
fills it with nodes heard once in passing until there is no room left to discover anyone
|
|
new. It ranks the whole table (see :mod:`~meshterm.core.contact_score`) and archives the
|
|
weakest off the device while MeshTerm keeps them; the flow, its ladder and its preview live
|
|
in :mod:`~meshterm.ui.purge_screen`. Our own node, pinned above the list, is never a
|
|
candidate — it isn't in the device's contact table.
|
|
|
|
The contacts a sweep archived are listed under their own **Archived** section at the foot of
|
|
this screen. They are shown rather than hidden because silent archiving is how a reader
|
|
loses track of what they archived, and because the section is the route back: Enter opens
|
|
the node's detail page, which offers to restore it.
|
|
|
|
Removing *one* named contact for good still belongs to that contact rather than to the list:
|
|
it is the last action on its Node detail page, where the reader is already looking at the
|
|
node they mean to drop. That one is a real deletion — it lands in both halves of what the
|
|
list shows, the device's own table and the cross-session store (see
|
|
:mod:`~meshterm.core.contact_store`), so a firmware-less bridge doesn't merge it back.
|
|
Either way, only the *contact* goes: each node's reception history and overheard traffic in
|
|
MeshTerm are untouched.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from .contactlist import ContactListScreen, ContactRow
|
|
from .menus import marked_label
|
|
from .purge_screen import archived_rows, purge_contacts
|
|
from .tui import Choice, Separator
|
|
from .tui.screen import CANCEL
|
|
from .widgets import ContactsSort, _contact_pkts
|
|
|
|
if TYPE_CHECKING:
|
|
from ..context import AppContext
|
|
from ..core.models import Contact
|
|
|
|
#: A stable identity for the own-node row (its lane data has no key to stand on when no
|
|
#: device answered) — Enter on it opens our own node's detail page.
|
|
YOU = ("you",)
|
|
|
|
#: The contact-list tail sentinels (distinct from any :class:`~meshterm.core.models.Contact`
|
|
#: and from :data:`YOU`): the purge action row and the bare ``Back`` exit row.
|
|
_PURGE = ("purge",)
|
|
|
|
|
|
class ContactsScreen(ContactListScreen):
|
|
"""A full-screen, Ctrl+arrow-sortable list of this node and its known contacts.
|
|
|
|
Enter resolves the highlighted row — our own node (:data:`YOU`) or a
|
|
:class:`~meshterm.core.models.Contact` — for :func:`open_contacts` to open in the
|
|
Node detail page; the shared list handles the sort, filter, and navigation.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
self_name: str,
|
|
self_key: str,
|
|
contacts: "list[Contact]",
|
|
prefix_bytes: int,
|
|
counts: dict[str, int],
|
|
sort: ContactsSort,
|
|
archived: Optional[list] = None,
|
|
) -> None:
|
|
"""Create the contacts screen over already-fetched contact data.
|
|
|
|
Args:
|
|
self_name: This node's advertised name.
|
|
self_key: This node's full public key (hex); blank renders a muted ``?``.
|
|
contacts: Known contacts to list under our own node.
|
|
prefix_bytes: Path-hash width in bytes to highlight in every key.
|
|
counts: Overheard-packet counts keyed by lowercased 12-hex node id; a contact
|
|
with no entry shows a faint ``—``.
|
|
sort: The initial sort; mutated in place by the Ctrl+arrows. Its ring should
|
|
span :data:`~meshterm.ui.contactlist.SORT_COLUMNS` so the hash sort is
|
|
reachable.
|
|
archived: Pre-built rows for the ``Archived`` section (see
|
|
:func:`~meshterm.ui.purge_screen.archived_rows`), appended after the purge
|
|
action. ``None`` draws no section at all.
|
|
"""
|
|
archived = list(archived or [])
|
|
rows = [ContactRow(value=YOU, name=self_name, key=self_key, you=True)]
|
|
for c in contacts:
|
|
rows.append(
|
|
ContactRow(
|
|
# The contact itself is the row's identity, so Enter hands the whole
|
|
# record straight to the detail page — no re-lookup by name/key.
|
|
value=c,
|
|
name=c.name,
|
|
key=c.public_key,
|
|
node_type=c.node_type,
|
|
last_seen=c.last_seen,
|
|
count=_contact_pkts(c, counts),
|
|
)
|
|
)
|
|
# The maintenance action closes the list, past every contact whatever the sort: a
|
|
# blank spacer, then the err-tinted purge row (a `…` — it opens further prompts).
|
|
# Offered only when there are contacts to purge. Anything already archived follows
|
|
# it in its own section, so the two halves of the sweep — what it can still take,
|
|
# and what it has taken — sit together at the foot of the list.
|
|
tail: list = []
|
|
if contacts:
|
|
tail = [
|
|
Separator(" "),
|
|
Choice(
|
|
title=marked_label("🗑", "Purge contacts…", "err"),
|
|
value=_PURGE,
|
|
),
|
|
]
|
|
tail += archived
|
|
super().__init__(
|
|
f"Contacts · {len(contacts)} known",
|
|
rows=rows,
|
|
prefix_bytes=prefix_bytes,
|
|
sort=sort,
|
|
tail=tail,
|
|
)
|
|
|
|
|
|
async def open_contacts(
|
|
ctx: "AppContext",
|
|
self_name: str,
|
|
self_key: str,
|
|
contacts: "list[Contact]",
|
|
prefix_bytes: int,
|
|
counts: dict[str, int],
|
|
sort: ContactsSort,
|
|
) -> None:
|
|
"""Open the interactive contacts list; Enter opens Node detail, Esc leaves.
|
|
|
|
The list stays pushed for the whole visit, so whichever node's detail page Enter commits
|
|
(or the purge flow the tail action opens) nests *above* it and Esc from there is one pop
|
|
back onto the row it was opened from — same sort, same filter, same scroll. Anything that
|
|
removes a contact — a purge here, or the detail page's own single-contact
|
|
``Remove contact…`` — ends the visit, re-reads the device and starts a fresh one, so the
|
|
contacts that went are gone from the list. That rebuild is the deliberate exception to
|
|
keeping the screen: the rows it was holding a place in no longer exist. A row under the
|
|
``Archived`` section is a rebuilt :class:`~meshterm.core.models.Contact` like any other,
|
|
so Enter on one opens the same detail page — where it can be restored to the device.
|
|
|
|
Args:
|
|
ctx: Shared application context (must be in the interactive menu).
|
|
self_name: This node's advertised name.
|
|
self_key: This node's full public key (hex).
|
|
contacts: Known contacts to list under our own node.
|
|
prefix_bytes: Path-hash width in bytes to highlight in every key.
|
|
counts: Overheard-packet counts keyed by lowercased 12-hex node id.
|
|
sort: The initial sort (column + direction) over the shared list's ring — mutated
|
|
in place, so a re-sort survives visiting a detail page and coming back.
|
|
|
|
Raises:
|
|
RuntimeError: If called outside the interactive menu (no full-screen session).
|
|
"""
|
|
from .node_detail_screen import open_node_detail
|
|
from .surface import TuiUi
|
|
|
|
if not isinstance(ctx.ui, TuiUi): # pragma: no cover - guarded by the menu-only caller
|
|
raise RuntimeError("the interactive contacts list is only available in the menu")
|
|
session = ctx.ui.session
|
|
screen = ContactsScreen(
|
|
self_name, self_key, contacts, prefix_bytes, counts, sort,
|
|
archived=archived_rows(ctx, self_key),
|
|
)
|
|
while True:
|
|
rebuild = False
|
|
async with session.stay(screen) as visit:
|
|
while True:
|
|
chosen = await visit.result()
|
|
if chosen is CANCEL or chosen is None: # Esc
|
|
return
|
|
if chosen == _PURGE:
|
|
# The purge ladder, its preview and its confirm all float over the list,
|
|
# which is already the backdrop. A sweep that archived anything
|
|
# invalidates the contacts cache, so the list is re-read and rebuilt —
|
|
# the one deliberate reset in this screen's life, because the rows it was
|
|
# keeping a place in are genuinely gone from the device.
|
|
if await purge_contacts(ctx, self_key):
|
|
rebuild = True
|
|
break
|
|
continue
|
|
# The own-node sentinel opens our own node's page; any other value is a
|
|
# Contact. The page nests above this list and can also *delete* the contact
|
|
# it details (its ``Remove contact…`` row); when it does, it says so on the
|
|
# way out and the list rebuilds without the row — the same re-read the purge
|
|
# does, one contact at a time.
|
|
if await open_node_detail(ctx, None if chosen == YOU else chosen):
|
|
rebuild = True
|
|
break
|
|
if not rebuild:
|
|
return
|
|
contacts = await ctx.devstate.contacts()
|
|
screen = ContactsScreen(
|
|
self_name, self_key, contacts, prefix_bytes, counts, sort,
|
|
archived=archived_rows(ctx, self_key),
|
|
)
|