diff --git a/meshterm/ui/node_detail_screen.py b/meshterm/ui/node_detail_screen.py index c44f27a..b752e70 100644 --- a/meshterm/ui/node_detail_screen.py +++ b/meshterm/ui/node_detail_screen.py @@ -45,7 +45,7 @@ from __future__ import annotations import asyncio import math -from dataclasses import dataclass, field +from dataclasses import dataclass, field, replace from datetime import datetime from typing import TYPE_CHECKING, Optional @@ -53,6 +53,7 @@ from rich.text import Text from ..core.geo import EARTH_RADIUS_KM, usable_fix from ..core.models import NODE_TYPE_LABELS, Contact, utcnow +from .mapcanvas import RGB, parse_hex from .minimap import MiniMap from .pathgraph import ( DST_NODE, @@ -61,6 +62,7 @@ from .pathgraph import ( LabelOf, LabelRgbOf, PathLayer, + bidir_clusters, render_path_graph, ) from .theme import name_style, snr_style @@ -123,6 +125,11 @@ _LABEL_LANE = 9 _WHITE = (255, 255, 255) _GREY = (120, 120, 120) +#: The synthetic node-id prefix a contracted bidirectional cluster draws under. A ``\x00`` +#: lead keeps it non-hex (so the graph never tries to coalesce it against a hash) and clear of +#: any real hop, mirroring the graph's own endpoint sentinels. +_CLUSTER_NODE = "\x00clu" + @dataclass(slots=True) class _Action: @@ -162,6 +169,30 @@ class _Route: row: Text +@dataclass(slots=True) +class _Cluster: + """A contracted bidirectional cluster's stand-in marker on the graph. + + Three or more repeaters that relay each other in every order are a knot the left-to-right + flow can't seat (see :func:`~meshterm.ui.pathgraph.bidir_clusters`); they draw as one + super-node instead, and this is how it presents. The route rows below the graph still name + every member in order, so the detail the marker folds away is one glance down. + + Attributes: + glyph: The marker glyph — the members' shared node-type mark when they agree + (``▲`` repeater, ``■`` room, …), else the plain node dot. + color: The marker glyph's ``#rrggbb`` colour (the node-type hue, or the plain dot's). + label: The count-and-type label, e.g. ``3 repeaters`` (``n nodes`` when mixed). + rgb: The label colour — the same type hue as the marker, so the super-node reads as a + typed group rather than a named node. + """ + + glyph: str + color: str + label: str + rgb: tuple[int, int, int] + + @dataclass(slots=True) class _RoutesView: """The Routes tab's selectable routes and the shared per-node draw callbacks, or a note. @@ -879,6 +910,63 @@ def _route_line( return text +def _cluster_presentation(members: tuple[str, ...], type_of) -> _Cluster: # noqa: ANN001 + """The marker + label a contracted bidirectional cluster draws under. + + A homogeneous cluster (every member the same node type) wears that type's map marker and + hue — ``3 repeaters`` under a ``▲`` — so it reads as a group of that kind at a glance; a + mixed one falls back to the plain node dot and ``n nodes``. + """ + types = {type_of(m) for m in members} + only = next(iter(types)) if len(types) == 1 else None + if only is not None: + glyph, color = _NODE_GLYPHS.get(only, _DEFAULT_GLYPH) + kind = NODE_TYPE_LABELS.get(only, "node") + else: + glyph, color = _DEFAULT_GLYPH + kind = "node" + return _Cluster(glyph=glyph, color=color, label=f"{len(members)} {kind}s", rgb=parse_hex(color)) + + +def _contract_bidir_clusters( + routes: list[_Route], type_of +) -> tuple[list[_Route], dict[str, _Cluster]]: # noqa: ANN001 + """Fold each 3+ bidirectional cluster in the routes' draw sequences to one super-node. + + A dense knot of mutually-relaying repeaters is a strongly-connected component the flow + graph can't order (see :func:`~meshterm.ui.pathgraph.bidir_clusters`); left as-is every + member collapses onto one jammed column. So each such cluster is contracted to a single + synthetic node: every route's ``draw`` has its members rewritten to the one cluster id + (consecutive members merged), and the returned map gives each cluster its marker and label. + + Only ``draw`` — the graph geometry — changes. Each route's spelled-out ``row`` and its + trace ``spec`` keep every member named in order, so the list under the graph still carries + the exact ordering the single marker can't, and a trace still arms on the real path. + + Returns ``(routes, clusters)`` unchanged (and an empty map) when there is no such knot. + """ + groups = bidir_clusters([(SRC_NODE, *route.draw, DST_NODE) for route in routes]) + if not groups: + return routes, {} + member_of: dict[str, str] = {} + clusters: dict[str, _Cluster] = {} + for i, members in enumerate(groups): + cid = f"{_CLUSTER_NODE}{i}" + clusters[cid] = _cluster_presentation(members, type_of) + for member in members: + member_of[member] = cid + contracted: list[_Route] = [] + for route in routes: + draw: list[str] = [] + for hop in route.draw: + cid = member_of.get(hop, hop) + if draw and draw[-1] == cid: + continue # a run of members through the same cluster is one stop + draw.append(cid) + contracted.append(replace(route, draw=tuple(draw))) + return contracted, clusters + + def _trace_label(routes: Optional["_RoutesView"]) -> str: """The Trace action's row label — armed on the selected route, or auto with none drawn.""" if routes is not None and routes.routes: @@ -979,6 +1067,14 @@ def _routes_view( ) routes.append(_Route(draw=tuple(reversed(hops_out)), spec=spec, row=row)) + # The legend explains the typed relay marks, so it is decided on the members' real types — + # before contraction folds a dense cluster's members behind one synthetic id. + legend = any(type_of(h) is not None for route in routes for h in route.draw) + # Fold any 3+ bidirectional cluster (a knot the flow can't order) to one super-node, so it + # draws as a single marker rather than piling its members onto one column. Each route's row + # and spec keep the members named in order — only the drawn geometry contracts. + routes, clusters = _contract_bidir_clusters(routes, type_of) + glyph_of, byte_label_of, label_rgb_of = style( resolve=resolve, self_name=self_name, source=node_label, type_of=type_of, key_of=key_of ) @@ -987,26 +1083,42 @@ def _routes_view( # graph tags a relay with only its first hash byte, here each relay wears its resolved # contact name (its colour is already the name's hue), so the whole route reads as places # rather than hex; an unidentified relay keeps the byte, the honest most it can be called. - # The two endpoints keep route_graph_style's names (target on the left, us on the right). + # A contracted cluster wears its own count-and-type label. The two endpoints keep + # route_graph_style's names (target on the left, us on the right). def label_of(node: str) -> Optional[str]: + cluster = clusters.get(node) + if cluster is not None: + return cluster.label if node in (SRC_NODE, DST_NODE): return byte_label_of(node) named = resolve(node) return named if named and named != node else node[:2] + base_rgb_of = label_rgb_of + + def cluster_label_rgb_of(node: str) -> RGB: + cluster = clusters.get(node) + return cluster.rgb if cluster is not None else base_rgb_of(node) + # The target wears its own map glyph (▲ repeater, ■ room, ◉ sensor) — the same mark the # header and the map give it. route_graph_style draws the far (left) endpoint as a plain # dot, since on its home screen (Message paths) that end is an arbitrary message origin; - # here it is a known contact whose type we can show. + # here it is a known contact whose type we can show. A contracted cluster draws its own + # type mark; everything else keeps the style's glyph. + base_glyph_of = glyph_of + + def cluster_glyph_of(node: str) -> tuple[str, str]: + cluster = clusters.get(node) + return (cluster.glyph, cluster.color) if cluster is not None else base_glyph_of(node) + glyph_of = _with_target_glyph( - glyph_of, _NODE_GLYPHS.get(type_of(canonical_target), _DEFAULT_GLYPH) + cluster_glyph_of, _NODE_GLYPHS.get(type_of(canonical_target), _DEFAULT_GLYPH) ) - legend = any(type_of(h) is not None for route in routes for h in route.draw) return _RoutesView( routes=routes, glyph_of=glyph_of, label_of=label_of, - label_rgb_of=label_rgb_of, + label_rgb_of=cluster_label_rgb_of, legend=legend, ) diff --git a/meshterm/ui/pathgraph.py b/meshterm/ui/pathgraph.py index 7ce9d6b..e43e329 100644 --- a/meshterm/ui/pathgraph.py +++ b/meshterm/ui/pathgraph.py @@ -196,10 +196,14 @@ def _mid_row(y_dot: float) -> int: def _collapse(layers: Sequence[PathLayer]) -> list[PathLayer]: - """Fold layers with identical hop sequences onto their highest-priority instance. + """Fold layers with identical hop sequences onto one drawn path. - Re-walking a known route highlights it rather than doubling it: the first appearance - fixes draw order, the strongest priority (and its colour) wins the shared path. + Re-walking a known route highlights it rather than doubling it. Geometry keeps the + strongest ``priority`` of the folded copies (so a shared route seats where its best + instance would), while the highlight — the drawn colour and its on-top order — follows the + most-*emphasised* copy, and the emphasis carries across. So two routes that only differ in + a cluster-internal order the caller has contracted away land on one line, and selecting + either lights it, even when a higher-priority copy owns the layout. """ drawn: list[PathLayer] = [] by_hops: dict[tuple[str, ...], int] = {} @@ -208,10 +212,14 @@ def _collapse(layers: Sequence[PathLayer]) -> list[PathLayer]: if at is None: by_hops[layer.hops] = len(drawn) drawn.append(layer) - elif layer.priority > drawn[at].priority: + else: + cur = drawn[at] + top = layer if _draw_rank(layer) > _draw_rank(cur) else cur drawn[at] = PathLayer( - layer.hops, layer.color, layer.priority, - max(layer.emphasis, drawn[at].emphasis), + cur.hops, + top.color, + max(layer.priority, cur.priority), + max(layer.emphasis, cur.emphasis), ) return drawn @@ -610,6 +618,31 @@ def _merge_bidir_pairs( return {node: find(node) for node in ordered_nodes} +def bidir_clusters(sequences: Sequence[tuple[str, ...]]) -> list[tuple[str, ...]]: + """The groups of three or more nodes that form a bidirectional cluster (a flow SCC). + + Two nodes walked in both directions are a 2-cycle the graph draws as one tidy vertical + pair — fine on its own. Three or more mutually linked that way are a strongly-connected + knot the left-to-right flow cannot order: :func:`_merge_bidir_pairs` collapses them all + onto one column, where their markers and labels pile up illegibly. A caller can pass its + path sequences (endpoints included) here to find those knots and contract each to a single + super-node *before* drawing, so the cluster reads as one marker rather than a jam — the one + honest way to seat a cycle in a DAG layout. + + Returns each cluster's member ids in first-appearance order (endpoints excluded); a lone + node or the tidy two-node pair is not a cluster and is not returned. + """ + edges = {pair for seq in sequences for pair in zip(seq, seq[1:])} + ordered = list(dict.fromkeys(node for seq in sequences for node in seq)) + rep = _merge_bidir_pairs(ordered, edges) + groups: dict[str, list[str]] = {} + for node in ordered: + if node in (SRC_NODE, DST_NODE): + continue + groups.setdefault(rep[node], []).append(node) + return [tuple(members) for members in groups.values() if len(members) >= 3] + + def _longest_paths(ordered_nodes: list[str], edges: set[tuple[str, str]]) -> dict[str, int]: """Longest-path depth of each node over ``edges`` (relaxed to a fixed point). diff --git a/tests/test_node_detail.py b/tests/test_node_detail.py index 7b40ba0..975abda 100644 --- a/tests/test_node_detail.py +++ b/tests/test_node_detail.py @@ -333,6 +333,50 @@ def test_route_freshness_drops_a_route_with_a_long_quiet_hop() -> None: assert _route_is_fresh(stale, ("3d63c6429436",), "f2c24f54551e", now) is False +def test_contract_bidir_clusters_folds_a_knot_but_keeps_the_rows() -> None: + """A 3+ bidirectional knot contracts to one super-node in the graph draw, while each route + row and its trace spec keep every member named in order.""" + from meshterm.ui.node_detail_screen import _contract_bidir_clusters + + routes = [ + _Route(draw=("aa", "bb", "cc"), spec="s1", row=Text("A B C")), + _Route(draw=("cc", "bb", "aa"), spec="s2", row=Text("C B A")), + ] + new, clusters = _contract_bidir_clusters(routes, lambda _n: 2) # all repeaters + (cid, cluster), = clusters.items() + assert cluster.label == "3 repeaters" and cluster.glyph == "▲" + assert [r.draw for r in new] == [(cid,), (cid,)] # members folded to the one cluster stop + assert [r.spec for r in new] == ["s1", "s2"] # traces still arm on the real path + assert [r.row.plain for r in new] == ["A B C", "C B A"] # the list keeps the full order + + +def test_contract_leaves_a_two_node_pair_and_unclustered_routes_alone() -> None: + """A tidy two-way pair keeps its own markers (the display JP likes); nothing contracts.""" + from meshterm.ui.node_detail_screen import _contract_bidir_clusters + + routes = [ + _Route(draw=("aa", "bb"), spec="s1", row=Text("A B")), + _Route(draw=("bb", "aa"), spec="s2", row=Text("B A")), + ] + new, clusters = _contract_bidir_clusters(routes, lambda _n: 2) + assert clusters == {} + assert [r.draw for r in new] == [("aa", "bb"), ("bb", "aa")] + + +def test_contract_labels_a_mixed_cluster_generically() -> None: + """A knot whose members are different node types can't wear one type mark — it reads + ``n nodes`` under the plain dot.""" + from meshterm.ui.node_detail_screen import _contract_bidir_clusters + + routes = [ + _Route(draw=("aa", "bb", "cc"), spec="", row=Text("")), + _Route(draw=("cc", "bb", "aa"), spec="", row=Text("")), + ] + _new, clusters = _contract_bidir_clusters(routes, lambda n: {"aa": 2, "bb": 3, "cc": 4}[n[:2]]) + (_cid, cluster), = clusters.items() + assert cluster.label == "3 nodes" and cluster.glyph == "●" + + def test_located_accepts_real_fixes_and_rejects_junk() -> None: """The location preview is gated on a real fix — no null island, no out-of-range advert.""" from meshterm.ui.node_detail_screen import _located diff --git a/tests/test_pathgraph.py b/tests/test_pathgraph.py index 163a91b..6b7823e 100644 --- a/tests/test_pathgraph.py +++ b/tests/test_pathgraph.py @@ -90,6 +90,22 @@ def test_emphasis_moves_the_highlight_not_the_layout() -> None: assert lit_spine != lit_alt +def test_bidir_clusters_finds_knots_not_pairs() -> None: + """Three-plus mutually-bidirectional nodes are a cluster to contract; a tidy two-way pair + (and any lone node) is not.""" + from meshterm.ui.pathgraph import bidir_clusters + + # A single two-way pair draws fine on its own — not a cluster. + pair = [(SRC_NODE, "aa", "bb", DST_NODE), (SRC_NODE, "bb", "aa", DST_NODE)] + assert bidir_clusters(pair) == [] + # aa<->bb and bb<->cc both ways make {aa, bb, cc} one strongly-connected knot. + knot = [(SRC_NODE, "aa", "bb", "cc", DST_NODE), (SRC_NODE, "cc", "bb", "aa", DST_NODE)] + groups = bidir_clusters(knot) + assert len(groups) == 1 and set(groups[0]) == {"aa", "bb", "cc"} + # Endpoints are never cluster members. + assert SRC_NODE not in groups[0] and DST_NODE not in groups[0] + + def test_emphasis_wins_a_shared_edge_over_a_higher_priority_spine() -> None: """A highlighted alternative paints its whole run — even the edge it shares with the spine — rather than dropping out where the higher-priority spine would otherwise own the colour."""