Relay marks count every repeater; near relays are gone

The first build counted only the relays past the first hops of the
outbound paths. On JP's Public channel, 23 of 23 sent messages came back
only through the two home repeaters: each repeater relays one time, and
the next hop (8 km away) is too far for the companion to hear. So every
message showed the warning mark, and nothing was ever confirmed.

Now each repeater that sends a copy back counts, the same as a DM ack:
spinner, then a check with the number of repeaters, or a cross. The
warning state, the near-relay snapshot of each send, the ^P atom, and
heard_via are removed. heard_relays keeps only the paths; a database with
the first build of the table gets it made again (derived data only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 5.5 committed 2026-11-20 21:21:11 -05:00
1 parent f4b1d659d1
commit 5dc62cdc5b
13 files changed
+164 -379

No files matched your search

+11 -21
View File
@@ -68,7 +68,7 @@ from meshterm.platforms import (
set_platform,
)
from meshterm.services.courier import CourierService
from meshterm.services.heard_by import BEYOND, NEARBY, UNHEARD, WAITING, HeardBy
from meshterm.services.heard_by import HEARD, UNHEARD, WAITING, HeardBy
from meshterm.services.message_paths import Arrival
from meshterm.services.monitor_service import ACTIVITY_BUCKETS
from meshterm.services.records import CATEGORY_BY_ID
@@ -826,37 +826,27 @@ class _GalleryHeard:
def _chat_heard(cols: int, rows: int) -> Screen:
"""Our channel messages with the four relay marks, and the note of the picked one.
"""Our channel messages with the three relay marks, and the note of the picked one.
The marks are the spinner, ``⚠`` (only the near relays sent it back), ``✗`` (no copy),
and ``✓ 3`` (three relays past the near relays). The newest message is picked, so its
note names the relays and the near relay that carried it out. The names are long, so
the note wraps under the body on the PicoCalc.
The marks are ``✗`` (no repeater sent a copy back), the spinner, and ``✓ 3`` (three
relays sent a copy back). The newest message is picked, so its note names the relays.
The names are long, so the note wraps under the body on the PicoCalc.
"""
conv = Conversation(label="Lakeside", is_channel=True, channel_idx=1)
messages = [
ChatMessage(text="Alice: anyone on the ridge tonight?", is_channel=True),
ChatMessage(text="testing the new antenna", outbound=True, is_channel=True, row_id=1),
ChatMessage(text="anyone hear this?", outbound=True, is_channel=True, row_id=2),
ChatMessage(text="one more try", outbound=True, is_channel=True, row_id=3),
ChatMessage(text="heading up the ridge now", outbound=True, is_channel=True, row_id=4),
ChatMessage(text="anyone hear this?", outbound=True, is_channel=True, row_id=1),
ChatMessage(text="one more try", outbound=True, is_channel=True, row_id=2),
ChatMessage(text="heading up the ridge now", outbound=True, is_channel=True, row_id=3),
]
heard = _GalleryHeard(
{
"testing the new antenna": HeardBy(UNHEARD),
"anyone hear this?": HeardBy(NEARBY, copies=2),
"anyone hear this?": HeardBy(UNHEARD),
"one more try": HeardBy(WAITING),
"heading up the ridge now": HeardBy(
BEYOND, beyond=("a1", "b2", "c3"), via=("3d",), copies=4
),
"heading up the ridge now": HeardBy(HEARD, relays=("a1", "b2", "c3"), copies=3),
}
)
names = {
"a1": "Lakeside-North-Shore",
"b2": "Marina-Tower",
"c3": "Ridge-Top-Solar",
"3d": "Home-Yagi",
}
names = {"a1": "Lakeside-North-Shore", "b2": "Marina-Tower", "c3": "Ridge-Top-Solar"}
screen = ChatScreen(
conv,
messages,
+39 -84
View File
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
"""Tests for the verdict on our channel messages: did a relay past our near relays hear it?
"""Tests for the verdict on our channel messages: did a repeater send a copy back?
The pure part (:func:`judge` and its helpers) is tested with plain paths. The tracker runs
against a real temporary repository, with GRP_TXT packets in the firmware form, so the
@@ -9,6 +9,7 @@ tests also use the window query, the decryption, and the stored verdicts.
from __future__ import annotations
import asyncio
import sqlite3
from datetime import datetime, timedelta, timezone
from pathlib import Path
@@ -19,25 +20,17 @@ from meshterm.core.channels import channel_hash, derive_secret
from meshterm.core.models import ChatMessage, Observation, utcnow
from meshterm.persistence.repository import Repository
from meshterm.services.heard_by import (
BEYOND,
NEARBY,
HEARD,
UNHEARD,
WAITING,
HeardTracker,
is_near,
judge,
near_relays,
relay_paths,
)
from meshterm.services.message_paths import Arrival, channel_arrivals
SECRET = derive_secret("#general")
# Invented repeaters: two near relays at home (one of them on a yagi), and two far ones.
HOME_OMNI = "3d00aa000001"
HOME_YAGI = "27d4bb000002"
NEAR = (HOME_OMNI, HOME_YAGI)
def _grp_txt_raw(secret: bytes, text: str, *, sent: datetime | None = None) -> dict:
"""A GRP_TXT packet payload in the firmware form, with ``sent`` as its sender time."""
@@ -85,57 +78,19 @@ def _sent(repo: Repository, text: str, at: datetime) -> ChatMessage:
# --- the pure verdict ------------------------------------------------------------------
def test_near_relays_are_the_first_hops_of_the_outbound_paths() -> None:
"""Each outbound path gives its first hop one time, in order, in lowercase."""
paths = [("3D00AA000001", "51548cda57ae"), ("27d4bb000002", "3d00aa000001"), ()]
assert near_relays(paths) == (HOME_OMNI, HOME_YAGI)
def test_a_hash_matches_a_near_relay_by_its_prefix() -> None:
"""A one-byte hash matches the longer key prefix of a near relay, in either case."""
assert is_near("3D", NEAR)
assert is_near("27d4", NEAR)
assert not is_near("51", NEAR)
assert not is_near("", NEAR)
def test_a_relay_past_the_near_relays_makes_the_verdict_beyond() -> None:
"""A copy through a far relay proves that the message went farther, at once."""
heard = judge([("3d", "51"), ("27", "3d")], NEAR, age_s=1, wait_s=30)
assert heard.state == BEYOND
assert heard.beyond == ("51",)
assert heard.via == ("3d",)
def test_each_relay_in_the_copies_counts_one_time() -> None:
"""The repeater next to us counts, and a relay in two copies counts one time."""
heard = judge([("3D",), ("27", "3d")], age_s=1, wait_s=30)
assert heard.state == HEARD
assert heard.relays == ("3d", "27")
assert heard.copies == 2
def test_near_relays_are_near_at_any_place_in_the_path() -> None:
"""The yagi relay after the omni relay is still a near relay, so nothing went farther."""
heard = judge([("27", "3d")], NEAR, age_s=60, wait_s=30)
assert heard.state == NEARBY
assert heard.beyond == ()
def test_the_verdict_waits_before_it_says_nearby_or_unheard() -> None:
"""Inside the wait, no copy and near copies both wait. After it, they differ."""
assert judge([], NEAR, age_s=5, wait_s=30).state == WAITING
assert judge([("3d",)], NEAR, age_s=5, wait_s=30).state == WAITING
assert judge([("3d",)], NEAR, age_s=31, wait_s=30).state == NEARBY
assert judge([], NEAR, age_s=31, wait_s=30).state == UNHEARD
def test_with_no_near_relays_each_relay_counts() -> None:
"""With no outbound paths, a relay next to us is proof too."""
heard = judge([("3d",), ("27", "51")], (), age_s=1, wait_s=30)
assert heard.state == BEYOND
assert heard.beyond == ("3d", "27", "51")
assert heard.via == ()
def test_the_via_names_each_near_relay_that_started_a_far_copy() -> None:
"""Both repeaters at home show when each of them carried the message out."""
heard = judge([("3d", "51"), ("27", "a9", "51")], NEAR, age_s=1, wait_s=30)
assert heard.beyond == ("51", "a9")
assert heard.via == ("3d", "27")
def test_the_verdict_waits_before_it_says_unheard() -> None:
"""With no copy, the verdict waits, then it is unheard. A copy ends the wait at once."""
assert judge([], age_s=5, wait_s=30).state == WAITING
assert judge([], age_s=31, wait_s=30).state == UNHEARD
assert judge([("3d",)], age_s=5, wait_s=30).state == HEARD
def test_relay_paths_skip_the_copies_that_tell_nothing() -> None:
@@ -171,12 +126,11 @@ def test_two_sends_of_the_same_text_keep_their_own_copies(tmp_path: Path) -> Non
# --- the tracker -----------------------------------------------------------------------
def _tracker(repo: Repository, now: datetime, *, near=NEAR) -> HeardTracker:
def _tracker(repo: Repository, now: datetime) -> HeardTracker:
return HeardTracker(
repo,
channel_name="#general",
secret=SECRET,
near_now=near,
wait_s=lambda: 30.0,
clock=lambda: now,
)
@@ -209,18 +163,17 @@ def test_the_tracker_ignores_other_messages(tmp_path: Path) -> None:
def test_a_late_copy_changes_the_verdict(tmp_path: Path) -> None:
"""A copy through a far relay after the wait turns the verdict from nearby to beyond."""
"""A copy after the wait turns the verdict from unheard to heard."""
repo, run = _repo(tmp_path)
sent_at = utcnow().replace(microsecond=0) - timedelta(seconds=40)
message = _sent(repo, "hello", sent_at)
_copy(repo, run, when=sent_at + timedelta(seconds=1), path="3d", text="hello", sent=sent_at)
tracker = _tracker(repo, sent_at + timedelta(seconds=40))
asyncio.run(tracker.fill([message]))
assert tracker.verdict(message).state == NEARBY # type: ignore[union-attr]
_copy(repo, run, when=sent_at + timedelta(seconds=35), path="3d,51", text="hello", sent=sent_at)
assert tracker.verdict(message).state == UNHEARD # type: ignore[union-attr]
_copy(repo, run, when=sent_at + timedelta(seconds=35), path="3d", text="hello", sent=sent_at)
assert tracker.refresh([message]) is True
verdict = tracker.verdict(message)
assert verdict is not None and verdict.state == BEYOND and verdict.beyond == ("51",)
assert verdict is not None and verdict.state == HEARD and verdict.relays == ("3d",)
repo.close()
@@ -233,32 +186,17 @@ def test_an_old_message_is_stored_and_read_back(tmp_path: Path) -> None:
repo, run, when=sent_at + timedelta(seconds=3), path="27,9f", text="old one", sent=sent_at
)
asyncio.run(_tracker(repo, utcnow()).fill([message]))
stored = repo.heard_records([message.row_id]) # type: ignore[list-item]
assert stored[message.row_id] == (NEAR, [("27", "9f")]) # type: ignore[index]
assert repo.heard_records([message.row_id]) == {message.row_id: [("27", "9f")]} # type: ignore[list-item,dict-item]
# A second visit with other near relays keeps the ones that the verdict used.
again = _tracker(repo, utcnow(), near=())
again = _tracker(repo, utcnow())
asyncio.run(again.fill([message]))
verdict = again.verdict(message)
assert verdict is not None and verdict.beyond == ("9f",) and verdict.via == ("27",)
repo.close()
def test_the_near_relays_of_the_send_win_over_the_ones_of_today(tmp_path: Path) -> None:
"""A message keeps the near relays of its send, also after a change of site."""
repo, run = _repo(tmp_path)
sent_at = utcnow().replace(microsecond=0) - timedelta(seconds=60)
message = _sent(repo, "from home", sent_at)
repo.set_near_hops(message.row_id, NEAR) # type: ignore[arg-type]
_copy(repo, run, when=sent_at + timedelta(seconds=2), path="3d", text="from home", sent=sent_at)
tracker = _tracker(repo, sent_at + timedelta(seconds=60), near=("51000000",))
asyncio.run(tracker.fill([message]))
assert tracker.verdict(message).state == NEARBY # type: ignore[union-attr]
assert verdict is not None and verdict.relays == ("27", "9f")
repo.close()
def test_a_message_sent_while_the_screen_is_open_waits(tmp_path: Path) -> None:
"""A new send starts in the wait, with the near relays that apply now."""
"""A new send starts in the wait."""
repo, _run = _repo(tmp_path)
now = datetime.now(timezone.utc).replace(microsecond=0)
message = _sent(repo, "new", now)
@@ -268,3 +206,20 @@ def test_a_message_sent_while_the_screen_is_open_waits(tmp_path: Path) -> None:
assert tracker.revision != before
assert tracker.waiting([message])
repo.close()
def test_the_first_build_of_the_table_is_made_again(tmp_path: Path) -> None:
"""A database with the near relays column of the first build gets the table again."""
path = tmp_path / "old.db"
Repository(path).close()
conn = sqlite3.connect(path)
conn.execute("DROP TABLE heard_relays")
conn.execute(
"CREATE TABLE heard_relays (message_id INTEGER PRIMARY KEY, near_hops TEXT, paths TEXT)"
)
conn.commit()
conn.close()
repo = Repository(path)
cols = {row["name"] for row in repo._conn.execute("PRAGMA table_info(heard_relays)")}
assert cols == {"message_id", "paths"}
repo.close()