Files
MeshTerm/tests/test_trace_plan.py
T
Jean-Pierre MartineauandClaude Opus 5.5 d3166170ff A trace to a companion turns at its last relay
A companion, a room server, or a sensor does not relay a trace with the
firmware defaults (allowPacketForward in Mesh.cpp: client repeat is off,
and allowed only on 433.000, 869.495, and 918.000 MHz; disable_fwd is on).
A walk that turned at one of them never came home: the history held 36
companion traces and no reply from a real one.

The route to such a node is the same. Only the walk changes: it turns at
the last relay of the route, and the node is not in the spec. The route
out of a walk that comes home is still the full route to the node, so
the Device route row can offer it for DMs and admin.

- relays_traces() in models states the rule. render_forced_spec takes
  relays=False, and forced_outbound reads the route back from a walk.
- services/trace_plan holds the route choice that the trace screen made
  inline (radio route, outbound paths, best observed, last trace,
  direct), so the CLI uses the same one.
- The trace screen names the relay where the walk turns, in a line under
  the route. With no relay to turn at, Trace is dim. A neighbour that
  does not relay gets "no relays" offered on the Device route row, with
  the packets heard directly as its proof.
- The trace session turns a caller's spec (node page, Message paths) at
  the last relay, so the callers do not need the rule. A sender that is
  not a contact gets its type from its stored adverts.
- The composer and Explore paths turn the walk at the last hop.
- `meshterm trace -t NAME` with no --path walks the menu's route and says
  on stderr where it turns. With no known relay, it is a usage error.
- The connection turns a device-routed trace at the last learned relay.
  The simulator now stops a walk at a node that does not relay, as the
  mesh does, and the tests that walked through Alice walk repeaters.

The picker keeps one list with no mark, sorted as before (JP's choice).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 03:42:56 -05:00

84 lines
3.2 KiB
Python

# SPDX-License-Identifier: Apache-2.0
"""The route that an automatic Trace target walks (refer to meshterm.services.trace_plan)."""
from __future__ import annotations
from datetime import timedelta
from meshterm.core.models import Contact, Hop, TraceResult, utcnow
from meshterm.persistence.repository import PacketPath
from meshterm.services.topology import build_topology
from meshterm.services.trace_plan import RoutePlan, plan_route
US = "aabb" + "00" * 30
HUB = Contact(name="Hub", public_key="3d63c6429436" + "0" * 52, key_prefix="3d63c6429436")
FAR = Contact(name="Far", public_key="f2c24f54551e" + "0" * 52, key_prefix="f2c24f54551e")
ALICE = Contact(
name="Alice", public_key="d4e5f6a7b8c9" + "0" * 52, key_prefix="d4e5f6a7b8c9", node_type=1
)
def _topology(packets=()): # noqa: ANN001, ANN202
return lambda: build_topology(
self_id=US,
contacts=[HUB, FAR, ALICE],
trace_paths=[],
packet_paths=list(packets),
neighbour_links=[],
)
def _plan(**overrides) -> RoutePlan: # noqa: ANN003
args = dict(
topology=_topology(),
device_route=None,
outbound=(),
choice=None,
silent_before=utcnow() - timedelta(hours=1),
previous=None,
)
args.update(overrides)
return plan_route(ALICE.public_key, **args)
def _walk(*nodes: str) -> TraceResult:
"""A walk that came home on ``nodes`` (the last hop is our node, with no hash)."""
hops = [Hop(index=i, node=n, snr=1.0) for i, n in enumerate(nodes)]
hops.append(Hop(index=len(nodes), node=None, snr=1.0))
return TraceResult(
target="Alice", success=True, hops=hops, round_trip_ms=200.0, path_hash_bytes=2
)
def test_the_radio_route_comes_first() -> None:
"""With no outbound path of the user, the route that the radio holds wins."""
assert _plan(device_route=("3d63c6429436",)) == RoutePlan(("3d63c6429436",), "device route")
def test_the_strongest_heard_relay_of_a_companion_gives_its_route() -> None:
"""Alice's packets came in through Hub five times and through Far once.
The evidence graph counts each relay by node and gives recent packets more weight.
Thus the route to Alice goes through Hub, the relay that hears her most.
"""
now = utcnow()
packets = [PacketPath(now, "d4e5f6a7b8c9", 5.0, ["3d63c6429436"]) for _ in range(5)]
packets.append(PacketPath(now, "d4e5f6a7b8c9", 5.0, ["f2c24f54551e"]))
plan = _plan(topology=_topology(packets), relays=False)
assert plan.hops == ("3d63c6429436",)
assert plan.source.startswith("best observed · ")
def test_with_no_evidence_the_last_walk_home_gives_the_route() -> None:
"""A walk to a companion turned at its relay, so that relay ends the route to it."""
relays = _plan(previous=_walk("3d63", "d4e5", "3d63"))
assert relays.hops == ("3d63",)
assert relays.source.startswith("last trace · ")
companion = _plan(previous=_walk("3d63", "f2c2", "3d63"), relays=False)
assert companion.hops == ("3d63", "f2c2")
def test_with_nothing_known_the_plan_is_a_direct_attempt() -> None:
"""No route anywhere: no relay, and the source says so."""
assert _plan() == RoutePlan((), "direct — no known route")