From ba6e92107d96153ee737a47eeb99fe62dc8f8e04 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 7 Aug 2026 19:21:29 -0400 Subject: [PATCH] Message paths: direct frames never matched at all MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The direct-message path view looked for frames of class TXT_MSG. The meshcore library reports that class as TEXT_MSG — the spelling every other reader uses (core.frames, the packet viewer's lane, the dashboard's traffic keys) — so the filter matched nothing, and every direct message reported an empty window no matter how much of the conversation the radio had overheard. Its test fabricated rows under the same wrong name, so it passed against a mesh that can't exist. With the class fixed, the evidence can also be far better than "±90 s of every direct frame we heard": a TEXT_MSG frame is encrypted, but its addressing is not — both ends ride in the clear as one-byte key hashes, and core.frames has been recovering and storing them all along. The window now narrows to frames running between this conversation's two ends in either direction, and the summary bills it as matched by address and time. A frame whose addressing was never recovered drops out rather than being counted: paths are evidence, and a hash that isn't there proves nothing. Still not a content match — a one-byte hash collides — so the view keeps its warn-coloured summary and makes no claim it can't back. Co-Authored-By: Claude Opus 5 (1M context) --- meshterm/services/message_paths.py | 80 +++++++++++++++++++++++------- meshterm/ui/chat.py | 19 +++++-- tests/test_message_paths.py | 54 +++++++++++++++++--- 3 files changed, 124 insertions(+), 29 deletions(-) diff --git a/meshterm/services/message_paths.py b/meshterm/services/message_paths.py index d1f2e4d..3daf8e8 100644 --- a/meshterm/services/message_paths.py +++ b/meshterm/services/message_paths.py @@ -14,10 +14,14 @@ chat message so the chat screen can show, on demand, every way the message reach ``Name: `` sender prefix convention on either side) is one arrival of that message — our own broadcasts included, since a repeater's rebroadcast of us is overheard and logged like anything else. -* **Direct messages** ride ECDH-encrypted ``TXT_MSG`` frames that only the companion - can decrypt, so no content match is possible from the log. Frames of that class - logged within a tight window around the message are offered instead, clearly billed - as matched by time — honest evidence, not a claim. +* **Direct messages** ride ECDH-encrypted ``TEXT_MSG`` frames that only the recipient + can decrypt, so no content match is possible from the log. What such a frame does + carry in the clear is its *addressing* — the one-byte key hash of each end + (:mod:`~meshterm.core.frames`) — so the window narrows to the frames travelling + between this conversation's two ends, in either direction, and what is left is billed + as matched by address and time. Honest evidence, not a claim: a one-byte hash + collides, and a frame between the right pair in the right ninety seconds is still + only *probably* this message. Nothing here transmits; it is a read-model over the repository. """ @@ -26,9 +30,10 @@ from __future__ import annotations from dataclasses import dataclass from datetime import datetime, timedelta -from typing import TYPE_CHECKING, Optional +from typing import TYPE_CHECKING, Optional, Sequence from ..core.channels import decrypt_channel_text, split_channel_sender +from ..core.frames import ENDPOINT_HASH_BYTES from ..core.models import ChatMessage, Observation if TYPE_CHECKING: @@ -38,12 +43,20 @@ if TYPE_CHECKING: #: message is stamped with the *sender's* clock, which may drift from ours by minutes. _CHANNEL_WINDOW = timedelta(minutes=15) -#: How far around a direct message the log is searched. Tight, because time proximity -#: is the only evidence tying an encrypted frame to the message. +#: How far around a direct message the log is searched. Tight, because the frame's +#: addressing and the clock are the only evidence tying it to the message. _DIRECT_WINDOW = timedelta(seconds=90) -#: Frame classes that carry a direct (addressed) text message. -_DIRECT_TYPENAMES = frozenset({"TXT_MSG"}) +#: Frame classes that carry a direct (addressed) text message — the payload class the +#: meshcore library names, spelled exactly as it reports it (see +#: :data:`~meshterm.core.frames.ADDRESSED_CLASSES`, the packet viewer's lane, the +#: dashboard's traffic keys). A near-miss here matches nothing at all and reads as a +#: mesh that never carries direct messages. +_DIRECT_TYPENAMES = frozenset({"TEXT_MSG"}) + +#: Hex digits of a frame's endpoint hash — the leading byte of a public key, as both +#: ends of a direct frame are addressed by. +_HASH_CHARS = 2 * ENDPOINT_HASH_BYTES @dataclass(slots=True) @@ -135,28 +148,57 @@ def channel_arrivals( return arrivals -def direct_frames_near(repo: "Repository", message: ChatMessage) -> list[Arrival]: - """Direct-message frames logged around ``message``, matched by time alone. +def _endpoint_hash(key: Optional[str]) -> str: + """A node's endpoint hash — the leading byte of its key, as a frame addresses it.""" + text = (key or "").strip().lower() + return text[:_HASH_CHARS] if len(text) >= _HASH_CHARS else "" + + +def direct_frames_near( + repo: "Repository", + message: ChatMessage, + *, + ends: Sequence[Optional[str]] = (), +) -> list[Arrival]: + """Direct-message frames logged around ``message``, matched by address and time. Direct frames are encrypted to their recipient, so the log can't confirm which - message a frame carried — the caller must present these as time-correlated - evidence, not a claim (see the module docstring). + message a frame carried — the caller must present these as correlated evidence, not + a claim (see the module docstring). Their *addressing* is in the clear, though, so + naming the conversation's two ends narrows the window to the frames that ran between + those two nodes, in either direction, instead of every direct frame the radio + happened to overhear. A frame whose addressing wasn't recovered can't be shown to + belong, so it drops out — evidence, not guesswork. Args: repo: The repository holding the packet log. message: The chat message to search around. + ends: Keys (or key prefixes) of the conversation's two ends — the peer and our + own node. Each contributes its endpoint hash; whichever are given must + *both* appear on a frame for it to count. Empty falls back to every direct + frame in the window, matched by time alone. Returns: - The window's direct-class frames, oldest first. + The matching frames as arrivals, oldest first. """ + wanted = {h for h in (_endpoint_hash(end) for end in ends) if h} frames = repo.packet_frames_between( message.created_at - _DIRECT_WINDOW, message.created_at + _DIRECT_WINDOW ) - return [ - Arrival(when=f.observed_at, hops=_frame_hops(f), snr=f.snr) - for f in frames - if isinstance(f.raw, dict) and f.raw.get("payload_typename") in _DIRECT_TYPENAMES - ] + arrivals: list[Arrival] = [] + for frame in frames: + raw = frame.raw if isinstance(frame.raw, dict) else {} + if raw.get("payload_typename") not in _DIRECT_TYPENAMES: + continue + if wanted: + addressed = { + str(raw.get("dest_hash") or "").lower(), + str(raw.get("src_hash") or "").lower(), + } + if not wanted <= addressed: + continue + arrivals.append(Arrival(when=frame.observed_at, hops=_frame_hops(frame), snr=frame.snr)) + return arrivals def distinct_paths(arrivals: list[Arrival]) -> int: diff --git a/meshterm/ui/chat.py b/meshterm/ui/chat.py index 670d19c..4f0d205 100644 --- a/meshterm/ui/chat.py +++ b/meshterm/ui/chat.py @@ -1054,8 +1054,13 @@ async def _make_paths_presenter( key_of = trace_runner.make_name_key_resolver(contacts, stored_names) prefix_bytes = await _routing_prefix_bytes(ctx) self_name: Optional[str] = None + self_key: Optional[str] = None try: - self_name = str((await ctx.devstate.self_info()).get("name") or "") or None + info = await ctx.devstate.self_info() + self_name = str(info.get("name") or "") or None + # Our own key names one end of every direct frame in this conversation; the + # contact's key names the other (see :func:`direct_frames_near`). + self_key = str(info.get("public_key") or "") or None except Exception: # noqa: BLE001 - a nameless self just skips the white highlight self_name = None @@ -1091,9 +1096,17 @@ async def _make_paths_presenter( ) return else: - arrivals = direct_frames_near(ctx.repo, message) + contact = conversation.contact + peer_key = conversation.peer or ( + (contact.public_key or contact.key_prefix) if contact else None + ) + arrivals = direct_frames_near(ctx.repo, message, ends=(peer_key, self_key)) matched = False - summary = "direct frames are encrypted — matched by time alone (±90 s)" + summary = ( + "direct frames are encrypted — matched by address and time (±90 s)" + if peer_key and self_key + else "direct frames are encrypted — matched by time alone (±90 s)" + ) # The graph's left endpoint: who the message set out from. Our own sends are # us; an inbound channel message names its sender on the wire; a direct chat's # origin is the conversation's peer (billed as time-matched by the summary). diff --git a/tests/test_message_paths.py b/tests/test_message_paths.py index 048c5ba..f287a23 100644 --- a/tests/test_message_paths.py +++ b/tests/test_message_paths.py @@ -113,23 +113,63 @@ def test_channel_arrivals_ignore_frames_outside_the_window(tmp_path: Path) -> No repo.close() +def _direct_raw(dest: str = "", src: str = "") -> dict: + """A direct-message frame's raw payload, spelled as the meshcore library reports it.""" + raw: dict = {"payload_typename": "TEXT_MSG"} + if dest: + raw["dest_hash"] = dest + if src: + raw["src_hash"] = src + return raw + + def test_direct_frames_match_by_time_only(tmp_path: Path) -> None: - """Direct frames are encrypted, so the window's TXT_MSG frames are the evidence.""" + """With neither end named, every direct frame in the tight window is the evidence.""" repo, run = _repo(tmp_path) now = utcnow() - _record_frame( - repo, run, when=now + timedelta(seconds=3), path="3d63", - raw={"payload_typename": "TXT_MSG"}, - ) + _record_frame(repo, run, when=now + timedelta(seconds=3), path="3d63", raw=_direct_raw()) _record_frame( # a channel frame in the window is not direct-message evidence repo, run, when=now + timedelta(seconds=4), path="", raw=_grp_txt_raw(SECRET, "Alice: hi"), ) _record_frame( # a direct frame far outside the tight window doesn't correlate - repo, run, when=now + timedelta(minutes=10), path="", - raw={"payload_typename": "TXT_MSG"}, + repo, run, when=now + timedelta(minutes=10), path="", raw=_direct_raw(), ) message = ChatMessage(text="see you at 8", peer="d4e5", created_at=now) arrivals = direct_frames_near(repo, message) assert len(arrivals) == 1 and arrivals[0].hops == ("3d63",) repo.close() + + +def test_direct_frames_narrow_to_the_conversation_ends(tmp_path: Path) -> None: + """Naming both ends keeps only the frames that ran between them, either direction.""" + repo, run = _repo(tmp_path) + now = utcnow() + # Us (a1…) and the peer (d4…), each way round: both are this conversation's traffic. + _record_frame( + repo, run, when=now + timedelta(seconds=1), path="3d63", + raw=_direct_raw(dest="d4", src="a1"), + ) + _record_frame( + repo, run, when=now + timedelta(seconds=2), path="c0", + raw=_direct_raw(dest="a1", src="d4"), + ) + # Someone else's direct message, overheard in the same window. + _record_frame( + repo, run, when=now + timedelta(seconds=3), path="3d63", + raw=_direct_raw(dest="7f", src="c0"), + ) + # Addressed to us, but by a third party — not this conversation. + _record_frame( + repo, run, when=now + timedelta(seconds=4), path="", + raw=_direct_raw(dest="a1", src="7f"), + ) + # Its addressing was never recovered, so it can't be shown to belong. + _record_frame(repo, run, when=now + timedelta(seconds=5), path="", raw=_direct_raw()) + + message = ChatMessage(text="see you at 8", peer="d4e5f6a7", created_at=now) + arrivals = direct_frames_near( + repo, message, ends=("d4e5f6a7", "a1" + "0" * 62), + ) + assert [a.hops for a in arrivals] == [("3d63",), ("c0",)] + repo.close()