Chat shows a message's delivery paths on demand (^P)

The radio's RX log has recorded every frame's relay path all along;
now a chat message can be asked how it got here. ^P floats a paths
window for the picked (or latest) message: one row per logged arrival
— time, reception SNR, and the relay chain through the shared compact
path widget — so a channel broadcast that reached us three ways reads
as three rows over two distinct paths, resends labelled.

Channel messages are matched by evidence, not guesswork: overheard
GRP_TXT frames decrypt under the channel's own key (fetched from the
device when the conversation didn't carry it) and match the message by
content, tolerating the "Name: " sender-prefix convention on the wire
— our own broadcasts included, since repeaters' rebroadcasts of us are
overheard like anything else. Direct frames are ECDH-encrypted, so the
view offers the window's TXT_MSG frames matched by time alone and says
so plainly. The sender-prefix parser moves to core.channels where the
matcher, the transcript, and the dashboard feed now all share it, and
the repository gains the windowed packet_frames_between query.

Both chat kinds now share one message-pick model: ↑ picks, the pick
walks with ↑↓/PgUp/PgDn/^Home and day-jumps with ^PgUp/^PgDn, ^End
returns to compose. Enter on a pick replies in a channel (unchanged)
and opens the paths in a direct chat, each with its banner cue.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-12 04:37:57 -04:00
1 parent d4f7b33580
commit 56b7580e48
8 files changed
+687 -131

No files matched your search

+30
View File
@@ -12,6 +12,7 @@ so the app never needs the radio's own decode to read a channel it holds the key
from __future__ import annotations
import re
import secrets
from dataclasses import dataclass
from datetime import datetime, timezone
@@ -22,6 +23,35 @@ from urllib.parse import parse_qs, quote, urlsplit
from Crypto.Cipher import AES
from Crypto.Hash import HMAC, SHA256 as _SHA256
#: Matches the ``Name: message`` convention channel senders use to identify themselves
#: (the protocol carries no sender field). The name is 1–20 non-colon characters and must
#: be followed by ``": "`` — conservative enough to leave ``http://…`` and ``note:x`` alone.
SENDER_PREFIX = re.compile(r"^([^\s:][^:]{0,19}):[ \t]+(.*)$", re.DOTALL)
def split_channel_sender(text: str) -> tuple[Optional[str], str]:
"""Split a channel message into ``(sender_name, body)`` when it carries a name prefix.
Channel messages have no sender field on the wire, so senders identify themselves by
prefixing the text with ``Name: ``. Lifting that name out lets a transcript show it
as a coloured header, a feed name *who* spoke, and the message-paths matcher compare
an on-air body against a stored one.
Args:
text: The raw channel message text.
Returns:
``(name, body)`` when a plausible ``Name: `` prefix is present, else
``(None, text)``.
"""
match = SENDER_PREFIX.match(text)
if match is None:
return None, text
name, body = match.group(1).strip(), match.group(2)
if not name or name.isdigit() or body.startswith("//"): # reject URLs / timestamps
return None, text
return name, body
#: A channel shared secret is exactly 16 bytes (128-bit), per the MeshCore protocol.
CHANNEL_SECRET_BYTES = 16
+32 -1
View File
@@ -726,8 +726,39 @@ class Repository:
"FROM observations WHERE observed_at >= ? ORDER BY observed_at DESC LIMIT ?",
(since.isoformat(), limit),
).fetchall()
return self._hydrate_observations(reversed(rows))
def packet_frames_between(
self, start: datetime, end: datetime, *, limit: int = 2000
) -> list[Observation]:
"""RX-logged ``packet`` frames inside a closed time window, oldest first.
The message-paths view's feed: every relayed frame the radio reported in the
window around a chat message, raw crypto fields included, so a channel frame
can be decrypted and matched to the message and a direct frame correlated by
time. Timestamps compare as ISO strings, like every observation query.
Args:
start: The window's inclusive start.
end: The window's inclusive end.
limit: Hard cap on rows (oldest kept — the window centres on the message).
Returns:
The window's ``packet`` observations, oldest first.
"""
rows = self._conn.execute(
"SELECT node, name, kind, node_type, snr, rssi, lat, lon, path, observed_at, "
"chan_hash, cipher_mac, crypted, payload_typename "
"FROM observations WHERE kind = 'packet' AND observed_at >= ? "
"AND observed_at <= ? ORDER BY observed_at ASC LIMIT ?",
(start.isoformat(), end.isoformat(), limit),
).fetchall()
return self._hydrate_observations(rows)
def _hydrate_observations(self, rows) -> list[Observation]: # noqa: ANN001 - sqlite rows
"""Rebuild :class:`Observation` values from full observation rows, in order."""
observations: list[Observation] = []
for row in reversed(rows):
for row in rows:
try:
observed_at = datetime.fromisoformat(row["observed_at"])
except (TypeError, ValueError):
+164
View File
@@ -0,0 +1,164 @@
"""Recover the paths a chat message rode in on, from the stored packet log.
The radio's RX log records every frame it decodes — including the relay path each one
traversed — and the recorder has been persisting those as ``packet`` observations all
along. A message heard more than once (a channel broadcast rebroadcast by several
repeaters, a flood that reached us two ways) therefore left one logged frame *per
arrival*, each with its own path and SNR. This module matches those frames back to a
chat message so the chat screen can show, on demand, every way the message reached us:
* **Channel messages** match by content: a ``GRP_TXT`` frame names its channel only by
a one-byte hash, but we hold the channel's key, so
:func:`~meshterm.core.channels.decrypt_channel_text` can confirm the channel by MAC
and recover the plaintext. A frame whose text equals the message's (allowing for the
``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.
Nothing here transmits; it is a read-model over the repository.
"""
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Optional
from ..core.channels import decrypt_channel_text, split_channel_sender
from ..core.models import ChatMessage, Observation
if TYPE_CHECKING:
from ..persistence.repository import Repository
#: How far around a channel message the log is searched. Wide, because an inbound
#: 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.
_DIRECT_WINDOW = timedelta(seconds=90)
#: Frame classes that carry a direct (addressed) text message.
_DIRECT_TYPENAMES = frozenset({"TXT_MSG"})
@dataclass(slots=True)
class Arrival:
"""One logged copy of a message reaching this radio.
Attributes:
when: When the frame was heard.
hops: The relay path it rode, in propagation order (empty = arrived direct).
snr: Reception SNR in dB — of the *last relay*, as with every packet row.
resend: The sender's resend counter for this copy (0 = the original send),
recovered from a decrypted channel frame; always 0 for direct frames.
"""
when: datetime
hops: tuple[str, ...]
snr: Optional[float]
resend: int = 0
def _frame_hops(observation: Observation) -> tuple[str, ...]:
"""An observation's relay path as hop hashes (empty = direct)."""
return tuple(h for h in (observation.path or "").split(",") if h)
def _texts_match(wire: str, stored: str) -> bool:
"""Whether an on-air text and a stored chat text are the same message body.
An inbound message is stored verbatim off the wire (sender prefix included), so an
exact match covers it. Our own outbound messages are stored as typed while the
radio prepends the ``Name: `` sender convention — so the wire text also matches
when its prefix-stripped body equals the stored text. Comparison is
whitespace-trimmed but otherwise exact: paths are evidence, and a fuzzy match
would fabricate some.
"""
wire, stored = wire.strip(), stored.strip()
if wire == stored:
return True
_sender, body = split_channel_sender(wire)
body = body.strip()
return bool(body) and body == stored
def channel_arrivals(
repo: "Repository",
message: ChatMessage,
*,
channel_name: str,
secret: bytes,
) -> list[Arrival]:
"""Every logged arrival of a channel message, matched by decrypted content.
Args:
repo: The repository holding the packet log.
message: The chat message whose arrivals to find (in- or outbound).
channel_name: The conversation's channel name (for the decrypt attempt).
secret: The channel's 16-byte key.
Returns:
The matching arrivals, oldest first (resends of the same message included,
each carrying its ``resend`` counter).
"""
frames = repo.packet_frames_between(
message.created_at - _CHANNEL_WINDOW, message.created_at + _CHANNEL_WINDOW
)
arrivals: list[Arrival] = []
for frame in frames:
raw = frame.raw if isinstance(frame.raw, dict) else {}
if raw.get("payload_typename") != "GRP_TXT":
continue
chan_hash = raw.get("chan_hash")
cipher_mac = raw.get("cipher_mac")
crypted = raw.get("crypted")
if not (chan_hash and cipher_mac and crypted):
continue
decrypted = decrypt_channel_text(
chan_hash, cipher_mac, crypted, [(channel_name, secret)]
)
if decrypted is None or not _texts_match(decrypted.text, message.text):
continue
arrivals.append(
Arrival(
when=frame.observed_at,
hops=_frame_hops(frame),
snr=frame.snr,
resend=decrypted.attempt,
)
)
return arrivals
def direct_frames_near(repo: "Repository", message: ChatMessage) -> list[Arrival]:
"""Direct-message frames logged around ``message``, matched by time alone.
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).
Args:
repo: The repository holding the packet log.
message: The chat message to search around.
Returns:
The window's direct-class frames, oldest first.
"""
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
]
def distinct_paths(arrivals: list[Arrival]) -> int:
"""How many distinct relay paths a set of arrivals covers."""
return len({a.hops for a in arrivals})
+235 -98
View File
@@ -22,6 +22,7 @@ from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional
from rich.console import Group, RenderableType
from rich.text import Text
from ..core.channels import split_channel_sender
from ..core.events import EventKind, MeshEvent
from ..core.models import ChatMessage, Contact, Conversation, Message, utcnow
from .theme import name_style, snr_style
@@ -29,6 +30,7 @@ from .tui.prompt import _LineEditor
from .tui.render import render_hanging, render_lines, right_aligned_tail
from .tui.screen import CANCEL, Screen
from .tui.spinner import Spinner
from .widgets import path_text
if TYPE_CHECKING:
from ..context import AppContext
@@ -36,11 +38,6 @@ if TYPE_CHECKING:
#: How many past messages to load into the transcript when a conversation opens.
_HISTORY_LIMIT = 200
#: Matches the ``Name: message`` convention channel senders use to identify themselves
#: (the protocol carries no sender field). The name is 1–20 non-colon characters and must
#: be followed by ``": "`` — conservative enough to leave ``http://…`` and ``note:x`` alone.
_SENDER_PREFIX = re.compile(r"^([^\s:][^:]{0,19}):[ \t]+(.*)$", re.DOTALL)
#: Matches an ``@[Name]`` mention token, as the reply flow primes into the compose line (see
#: :meth:`ChatScreen._begin_reply`). The transcript renders each as a bare ``@Name`` colored
#: in that sender's hue instead of showing the literal brackets. Name is 1–20 non-``]`` chars.
@@ -86,34 +83,23 @@ def _sender_hue(sender: str) -> str:
return name_style(sender)
def _split_channel_sender(text: str) -> tuple[Optional[str], str]:
"""Split a channel message into ``(sender_name, body)`` when it carries a name prefix.
Channel messages have no sender field on the wire, so senders identify themselves by
prefixing the text with ``Name: ``. Lifting that name out lets the transcript show it
as a colored header and keep the body clean.
Args:
text: The raw channel message text.
Returns:
``(name, body)`` when a plausible ``Name: `` prefix is present, else ``(None, text)``.
"""
match = _SENDER_PREFIX.match(text)
if match is None:
return None, text
name, body = match.group(1).strip(), match.group(2)
if not name or name.isdigit() or body.startswith("//"): # reject URLs / timestamps
return None, text
return name, body
#: The sender-prefix parser, shared app-wide from the protocol layer (the transcript,
#: the conversation picker, the dashboard feed, and the message-paths matcher must all
#: split ``Name: body`` identically). Kept under its old private name for the callers
#: that import it from here.
_split_channel_sender = split_channel_sender
class ChatScreen(Screen):
"""A live conversation: a scrolling transcript above a pinned input line.
The transcript auto-sticks to the newest message (and snaps back to the bottom whenever
you type or send). Scrolling up with the arrows / PageUp detaches from the bottom to
read history; End re-attaches. Enter sends the current line; Esc leaves the chat.
The transcript auto-sticks to the newest message (and snaps back to the bottom
whenever you type or send). ↑ picks a message — the pick walks with ↑↓/PgUp/PgDn
and carries the view with it; ^End (or Esc) returns focus to the compose line.
Enter sends the current line, or acts on a picked message: in a channel it primes
a reply ``@mention``, in a direct chat it opens the message's delivery paths. ^P
opens the paths of the picked (or latest) message in either kind. Esc leaves the
chat once nothing is picked.
"""
floating = False
@@ -127,6 +113,7 @@ class ChatScreen(Screen):
names: dict[str, str],
session, # noqa: ANN001 - TuiSession, imported lazily to avoid a cycle
resend: Optional[Callable[[ChatMessage], Awaitable[ChatMessage]]] = None,
paths: Optional[Callable[[ChatMessage], Awaitable[None]]] = None,
) -> None:
"""Build the chat screen.
@@ -141,6 +128,8 @@ class ChatScreen(Screen):
request repaints when messages arrive or a send completes.
resend: Async callable that re-attempts delivery of an unacknowledged direct
message, updating it in place (direct chats only; ``None`` for channels).
paths: Async callable that presents the delivery paths of one message (the
^P view); ``None`` leaves the affordance quietly inert.
"""
super().__init__()
self.title = conversation.label
@@ -148,6 +137,7 @@ class ChatScreen(Screen):
self._messages = list(messages)
self._send = send
self._resend = resend
self._paths = paths
self._names = names
self._session = session
self._editor = _LineEditor()
@@ -158,22 +148,24 @@ class ChatScreen(Screen):
self._spinner = Spinner()
self._status = ""
self._stick = True # keep the newest message in view until the user scrolls up
# Channel-only reply selection: index of the highlighted message (or None when the
# compose line is focused), plus the body line it rendered on so the frame keeps it
# in view. Direct chats keep the flat, free-scrolling view and never select.
self._paths_open = False # one paths dialog at a time
# The pick: index of the highlighted message (or None when the compose line is
# focused), plus the body line it rendered on so the frame keeps it in view.
self._selected: Optional[int] = None
self._selected_line: Optional[int] = None
@property
def footer_hint(self) -> str:
"""Key hint, reflecting whether a message is picked for reply (channels only)."""
if not self._is_channel:
if any(m.outbound and m.acked is False for m in self._messages):
return "Enter send · ^R retry failed · ↑↓/PgUp scroll · ^End latest · Esc back"
return "Enter send · ↑↓/PgUp scroll · ^End latest · Esc back"
"""Key hint, reflecting whether a message is picked and what Enter does to it."""
if self._selected is not None:
return "Enter reply (@mention) · ↑↓ pick · ^End/Esc cancel"
return "Enter send · ↑ pick a message to reply · Esc back"
if self._is_channel:
return "Enter reply (@mention) · ^P paths · ↑↓ pick · ^End/Esc cancel"
return "Enter paths · ↑↓ pick · ^End/Esc cancel"
if not self._is_channel and any(
m.outbound and m.acked is False for m in self._messages
):
return "Enter send · ↑ pick a message · ^R retry failed · ^P paths · Esc back"
return "Enter send · ↑ pick a message · ^P paths · Esc back"
# --- live updates --------------------------------------------------------
@@ -213,8 +205,8 @@ class ChatScreen(Screen):
Text("─" * width, style="muted"),
compose,
]
if self._is_channel and self._selected is not None:
footer_parts.append(self._reply_banner())
if self._selected is not None:
footer_parts.append(self._pick_banner())
if self._status:
footer_parts.append(Text(self._status, style="muted"))
lines += render_lines(Group(*footer_parts), width)
@@ -422,13 +414,23 @@ class ChatScreen(Screen):
glyph, style = _DELIVERED if acked else _FAILED
return Text(glyph, style=style)
def _reply_banner(self) -> Text:
"""One-line cue shown above the input when a message is picked to reply to."""
def _pick_banner(self) -> Text:
"""One-line cue shown above the input while a message is picked.
Channels lead with the reply affordance (Enter's job there); direct chats with
the paths view (their Enter). Both mention what the pick is for, so the state
never reads as a mystery highlight.
"""
message = self._messages[self._selected]
sender, _ = self._sender_and_body(message)
who = "this message" if message.outbound or sender == "·" else sender
if self._is_channel:
return Text(
f"↩ Enter to reply to {who} with an @mention · End to cancel",
style="accent",
)
return Text(
f"↩ Enter to reply to {who} with an @mention · End to cancel", style="accent"
"Enter to see the paths this message took · End to cancel", style="accent"
)
def _sender_style(self, sender: str, *, is_self: bool = False) -> str:
@@ -448,67 +450,34 @@ class ChatScreen(Screen):
# --- input ---------------------------------------------------------------
def handle(self, action: str, data: str = "") -> None:
"""Dispatch a key: channels navigate a reply selection; direct chats free-scroll."""
if self._is_channel:
self._handle_channel(action, data)
else:
self._handle_flat(action, data)
"""Dispatch a key: pick and act on messages, edit the compose line, or leave.
def _handle_flat(self, action: str, data: str = "") -> None:
"""Direct-chat input: send on Enter, scroll the transcript, edit, or leave on Esc.
Home/End and Ctrl+←/→ act on the compose line (like any text field); scrolling the
transcript is on the arrows, PageUp/PageDown (a screenful), Ctrl+Home/End (top / live
tail), and Ctrl+PageUp/PageDown (previous / next day divider).
"""
if action == "enter":
self._submit()
elif action == "retry":
self._retry()
elif action == "escape":
self.resolve(CANCEL)
elif action == "up":
self._detach_and(self.scroll_lines, -1)
elif action == "pageup":
self._detach_and(self.scroll_pages, -1)
elif action == "down":
self._detach_and(self.scroll_lines, 1)
elif action == "pagedown":
self._detach_and(self.scroll_pages, 1)
elif action == "ctrl_home":
self._detach_and(self.scroll_to_top)
elif action == "ctrl_pageup":
self._detach_and(self.scroll_to_section_start)
elif action == "ctrl_pagedown":
self._detach_and(self.scroll_to_next_section)
elif action == "ctrl_end":
self._stick = True # snap back to the live tail / compose line
else:
if self._editor.edit(action, data):
self._status = "" # trimming clears the "too long" notice
self._stick = True # touching the compose line snaps back to the live tail
def _detach_and(self, move: Callable[..., None], *args: Any) -> None:
"""Detach from the live tail and run a scroll move (the shared scroll helpers)."""
self._stick = False
move(*args)
def _handle_channel(self, action: str, data: str = "") -> None:
"""Channel input: arrows pick a message to reply to; Enter sends or starts a reply.
With no message picked the view behaves like the compose line (Enter sends). Moving
up enters the selection from the newest message; moving down past the newest (or
End) returns focus to the compose line and clears the selection. Enter on a picked
message primes the input with an ``@mention`` instead of sending.
Both chat kinds share one model. With no message picked, Enter sends and typing
edits the compose line. ↑ picks the newest message; the pick then walks with
↑↓, PgUp/PgDn (a screenful), Ctrl+Home (the very first message), and
Ctrl+PgUp/PgDn (day dividers), carrying the view with it. Enter on a picked
message primes a reply ``@mention`` in a channel and opens the delivery paths
in a direct chat; ^P opens the paths of the picked (or latest) message in
either kind. ^End (or moving past the newest) returns to the compose line;
Esc peels the pick first, the screen second.
"""
if action == "enter":
if self._selected is not None:
self._begin_reply()
if self._is_channel:
self._begin_reply()
else:
self._open_paths(self._selected)
else:
self._submit()
elif action == "paths":
target = self._selected if self._selected is not None else len(self._messages) - 1
self._open_paths(target)
elif action == "retry":
if not self._is_channel:
self._retry()
elif action == "escape":
if self._selected is not None:
self._clear_selection() # first Esc deselects; next leaves the chat
self._clear_selection() # first Esc unpicks; next leaves the chat
self._session.invalidate()
else:
self.resolve(CANCEL)
@@ -533,11 +502,27 @@ class ChatScreen(Screen):
self._stick = True # jump back to the live tail / compose line
else:
if self._editor.edit(action, data):
# Touching the compose line returns focus there — nothing stays selected.
# Touching the compose line returns focus there — nothing stays picked.
self._status = "" # trimming clears the "too long" notice
self._clear_selection()
self._stick = True
def _open_paths(self, index: Optional[int]) -> None:
"""Float the delivery-paths view for the message at ``index`` (one at a time)."""
if index is None or not self._messages or self._paths is None or self._paths_open:
return
message = self._messages[max(0, min(index, len(self._messages) - 1))]
self._paths_open = True
async def run() -> None:
try:
await self._paths(message)
finally:
self._paths_open = False
self._session.invalidate()
asyncio.ensure_future(run())
def _move_selection(self, delta: int) -> None:
"""Move the reply selection by ``delta`` messages (negative = toward older).
@@ -815,8 +800,15 @@ async def open_chat(ctx: "AppContext", conversation: Conversation) -> int:
assert conversation.contact is not None
return await ctx.chat.resend_direct(conversation.contact, message)
paths = await _make_paths_presenter(ctx, conversation, device)
screen = ChatScreen(
conversation, history, send=send, names=names, session=session, resend=resend
conversation,
history,
send=send,
names=names,
session=session,
resend=resend,
paths=paths,
)
ctx.chat.set_active(conversation.key)
@@ -868,3 +860,148 @@ def _belongs(message: Message, conversation: Conversation) -> bool:
if not sender or not peer:
return False
return sender.startswith(peer) or peer.startswith(sender)
# --- message paths (the ^P view) -----------------------------------------------------
async def _make_paths_presenter(
ctx: "AppContext",
conversation: Conversation,
device, # noqa: ANN001 - core Device; typed at the source
) -> Callable[[ChatMessage], Awaitable[None]]:
"""Build the async presenter behind the chat's ^P delivery-paths view.
Gathers what the presenter needs once per chat open: a hop-name resolver over the
contacts plus every name the recorder ever overheard (the app-wide rule that a
nameable node never shows as a bare hash), our own node's name for the white
``you``, the routing prefix width for the hash highlights, and — for a channel —
its secret, read from the device when the conversation didn't carry one (a picker
conversation knows its identity but not always its key).
Args:
ctx: The shared application context.
conversation: The conversation the chat screen is opening.
device: The connected device (already awaited by the caller).
Returns:
An async callable presenting one message's paths in a floating window.
"""
from ..services import trace_runner
from ..services.message_paths import (
channel_arrivals,
direct_frames_near,
distinct_paths,
)
from .timemachine_screen import _routing_prefix_bytes
session = ctx.ui.session
resolve = trace_runner.make_node_resolver(
await device.get_contacts(), ctx.repo.node_names()
)
prefix_bytes = await _routing_prefix_bytes(ctx)
self_name: Optional[str] = None
try:
self_name = str((await device.get_self_info()).get("name") or "") or None
except Exception: # noqa: BLE001 - a nameless self just skips the white highlight
self_name = None
secret = conversation.secret
if conversation.is_channel and not secret and conversation.channel_idx is not None:
try:
payload = await device.get_channel(conversation.channel_idx)
raw = (payload or {}).get("channel_secret")
secret = bytes(raw) if raw else None
except Exception: # noqa: BLE001 - no key, no decrypt; the view says so honestly
secret = None
async def present(message: ChatMessage) -> None:
if conversation.is_channel and secret:
arrivals = channel_arrivals(
ctx.repo, message, channel_name=conversation.label, secret=secret
)
body = _paths_view(
message, arrivals, matched=True, resolve=resolve,
prefix_bytes=prefix_bytes, self_name=self_name,
summary=f"heard {len(arrivals)} time{'s' if len(arrivals) != 1 else ''}"
f" · {distinct_paths(arrivals)} distinct "
f"path{'s' if distinct_paths(arrivals) != 1 else ''}"
if arrivals else "no copies in the packet log",
)
elif conversation.is_channel:
body = Text(
"This channel's key isn't at hand, so overheard frames can't be "
"matched to the message.",
style="muted",
)
else:
arrivals = direct_frames_near(ctx.repo, message)
body = _paths_view(
message, arrivals, matched=False, resolve=resolve,
prefix_bytes=prefix_bytes, self_name=self_name,
summary="direct frames are encrypted — matched by time alone (±90 s)",
)
await session.scroll(body, title="Message paths")
return present
def _paths_view(
message: ChatMessage,
arrivals: list,
*,
matched: bool,
resolve, # noqa: ANN001 - NodeResolver
prefix_bytes: int,
self_name: Optional[str],
summary: str,
) -> Group:
"""Lay one message's arrivals out: the quoted text, a summary, then one row each.
Every row reads through the shared compact path widget, so a path here looks
exactly like the same path in the dashboard feed or the packet viewer: time,
reception SNR, then ``direct`` or the relay chain (with a resend counter when a
decrypted channel frame carried one).
"""
quoted = message.text.replace("\n", " ")
if len(quoted) > 64:
quoted = quoted[:63] + "…"
head = Text(f"“{quoted}”")
stamp = Text(message.created_at.astimezone().strftime("%b %d %H:%M"), style="muted")
stamp.append(" · ", style="muted")
stamp.append(summary, style="muted" if matched else "warn")
parts: list[RenderableType] = [head, stamp]
if not arrivals:
parts.append(Text())
if matched:
parts.append(Text(
"Nothing overheard — the radio only logs frames it hears while "
"MeshTerm is listening.", style="muted",
))
else:
parts.append(Text("No direct-message frames logged in the window.", style="muted"))
return Group(*parts)
parts.append(Text())
for arrival in arrivals:
row = Text(no_wrap=True, overflow="ellipsis")
row.append(arrival.when.astimezone().strftime("%H:%M:%S"), style="muted")
row.append(" ")
if arrival.snr is not None:
row.append(f"{arrival.snr:+5.1f} dB", style=snr_style(arrival.snr))
else:
row.append(" " * 8, style="muted")
row.append(" ")
if arrival.hops:
row.append("via ", style="muted")
row.append_text(
path_text(
arrival.hops, resolve,
prefix_bytes=prefix_bytes, self_name=self_name, empty="direct",
)
)
if arrival.resend:
row.append(f" (resend #{arrival.resend})", style="muted")
parts.append(row)
return Group(*parts)
+9 -13
View File
@@ -34,7 +34,6 @@ drops the feed highlight, then backs out.
from __future__ import annotations
import asyncio
import re
from collections import Counter, deque
from statistics import median
from typing import TYPE_CHECKING, Any, Optional, Sequence
@@ -43,6 +42,7 @@ from rich.console import Group, RenderableType
from rich.table import Table
from rich.text import Text
from ..core.channels import split_channel_sender
from ..core.events import EventKind, MeshEvent
from ..core.models import NODE_TYPE_REPEATER, Observation, utcnow
from ..persistence.repository import ACTIVITY_WINDOW
@@ -95,19 +95,15 @@ _FEED_LABEL_MIN_WIDTH = 76
_GRID_LABEL_W = 9
#: A channel message carries no sender field on the wire, so senders self-identify by
#: prefixing ``Name: `` (mirrors the chat transcript's own parse). Lifting the name out
#: lets the feed's node lane show *who* sent it rather than the bare channel it came in on.
_SENDER_PREFIX = re.compile(r"^([^\s:][^:]{0,19}):[ \t]+\S")
def _channel_sender(text: Optional[str]) -> Optional[str]:
"""The sender named by a channel message's ``Name: `` prefix, or ``None`` if absent."""
match = _SENDER_PREFIX.match(text or "")
if match is None:
return None
name = match.group(1).strip()
return name if name and not name.isdigit() else None
"""The sender named by a channel message's ``Name: `` prefix, or ``None`` if absent.
The shared protocol-layer parse (see
:func:`~meshterm.core.channels.split_channel_sender`), so the feed's node lane names
exactly the sender the chat transcript would.
"""
name, _body = split_channel_sender(text or "")
return name
def _span_label(minutes: int) -> str:
+1
View File
@@ -66,6 +66,7 @@ _KEY_ACTIONS: dict[Any, str] = {
Keys.Delete: "delete",
Keys.Tab: "tab",
Keys.ControlR: "retry",
Keys.ControlP: "paths",
}
+81 -19
View File
@@ -769,14 +769,19 @@ async def test_chat_screen_retry_resends_failed_message() -> None:
assert "✓" in "\n".join(screen.render_body(60))
def test_chat_screen_scroll_detaches_and_end_reattaches() -> None:
"""Scrolling up detaches from the live tail; End re-sticks to the bottom."""
screen = _screen(_StubSession(), send=None)
assert screen._stick is True
def test_chat_screen_up_picks_and_ctrl_end_returns_to_compose() -> None:
"""↑ picks the newest message (detaching from the tail); ^End returns to compose."""
messages = [
ChatMessage(text="hi", peer="d4e5f6a7"),
ChatMessage(text="reply", outbound=True, peer="d4e5f6a7", acked=True),
]
screen = _screen(_StubSession(), send=None, messages=messages)
assert screen._stick is True and screen._selected is None
screen.handle("up")
assert screen._stick is False
screen.handle("end")
assert screen._stick is True
assert screen._selected == len(screen._messages) - 1 # the newest message
screen.handle("ctrl_end")
assert screen._stick is True and screen._selected is None
def test_split_channel_sender_extracts_name_prefix() -> None:
@@ -870,30 +875,87 @@ def test_chat_home_end_and_word_keys_move_the_compose_cursor() -> None:
assert screen._editor.cursor == 6 # start of "world"
def test_chat_scroll_keys_detach_and_reattach_to_the_tail() -> None:
"""PageUp/Ctrl+Home detach from the live tail; Ctrl+End snaps back to it."""
async def test_chat_paths_key_opens_the_picked_or_latest_message() -> None:
"""^P presents the picked message's paths — or the latest, when nothing is picked."""
import asyncio
shown: list[str] = []
async def paths(message) -> None: # noqa: ANN001 - ChatMessage
shown.append(message.text)
messages = [
ChatMessage(text="first", peer="d4e5f6a7"),
ChatMessage(text="second", peer="d4e5f6a7"),
]
conv = Conversation(
label="Alice",
is_channel=False,
contact=Contact(name="Alice", public_key="d4" + "0" * 62, key_prefix="d4e5f6a7"),
)
screen = ChatScreen(
conv, messages, send=None, names={}, session=_StubSession(), paths=paths
)
screen.handle("paths") # nothing picked → the latest message
await asyncio.sleep(0)
assert shown == ["second"]
screen.handle("up")
screen.handle("up") # pick the older message
screen.handle("paths")
await asyncio.sleep(0)
assert shown == ["second", "first"]
async def test_chat_direct_enter_on_a_pick_opens_paths() -> None:
"""In a direct chat, Enter on a picked message opens its paths (channels reply)."""
import asyncio
shown: list[str] = []
async def paths(message) -> None: # noqa: ANN001 - ChatMessage
shown.append(message.text)
messages = [ChatMessage(text="only", peer="d4e5f6a7")]
conv = Conversation(
label="Alice",
is_channel=False,
contact=Contact(name="Alice", public_key="d4" + "0" * 62, key_prefix="d4e5f6a7"),
)
screen = ChatScreen(
conv, messages, send=None, names={}, session=_StubSession(), paths=paths
)
screen.handle("up")
assert "Enter paths" in screen.footer_hint
screen.handle("enter")
await asyncio.sleep(0)
assert shown == ["only"]
def test_chat_pick_walks_with_page_keys_and_ctrl_home() -> None:
"""PgUp walks the pick a screenful back; ^Home jumps it to the very first message."""
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
lines = screen.render_body(60)
screen.note_metrics(total=len(lines), viewport=5)
screen.handle("pageup")
assert screen._stick is False # a screenful up detaches from the tail
assert screen._stick is False and screen._selected is not None
screen.handle("ctrl_home")
assert screen.scroll == 0 and screen._stick is False
assert screen._selected == 0 # the very first message
screen.handle("ctrl_end")
assert screen._stick is True # re-attached to the newest message
assert screen._stick is True and screen._selected is None
def test_chat_ctrl_page_scrolls_between_day_dividers() -> None:
"""Ctrl+PageDown/PageUp move the transcript scroll between day dividers."""
def test_chat_ctrl_page_picks_across_day_dividers() -> None:
"""Ctrl+PageDown/PageUp move the pick between day boundaries (both chat kinds)."""
screen = _screen(_StubSession(), send=None, messages=_two_day_messages())
lines = screen.render_body(60)
screen.note_metrics(total=len(lines), viewport=5)
(idx0, _), (idx1, _) = screen._sticky_headers
screen.scroll = 0
starts = screen._day_start_indices()
assert len(starts) == 2
screen.handle("ctrl_home")
assert screen._selected == 0
screen.handle("ctrl_pagedown")
assert screen.scroll == idx1 and screen._stick is False # to the second day's divider
assert screen._selected == starts[1] # first message of the second day
screen.handle("ctrl_pageup")
assert screen.scroll == idx0 # back to the first day's divider
assert screen._selected == starts[0] # back to the first day
def _two_day_channel_messages():
+135
View File
@@ -0,0 +1,135 @@
"""Message-paths tests: matching a chat message back to its logged arrivals.
Channel arrivals are matched by decrypting overheard GRP_TXT frames with the channel's
own key (firmware-shaped frames are built here exactly as the packet-viewer tests build
theirs); direct arrivals are matched by time alone. Everything runs against a real
temporary repository, so the window query is exercised too.
"""
from __future__ import annotations
from datetime import timedelta
from pathlib import Path
from Crypto.Cipher import AES
from Crypto.Hash import HMAC, SHA256
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.message_paths import (
channel_arrivals,
direct_frames_near,
distinct_paths,
)
SECRET = derive_secret("#general")
def _grp_txt_raw(secret: bytes, text: str, *, attempt: int = 0) -> dict:
"""A firmware-shaped GRP_TXT packet payload for ``text``, encrypted under ``secret``."""
plain = (0).to_bytes(4, "little") + bytes([attempt]) + text.encode("utf-8")
plain += b"\x00" * (-len(plain) % 16)
crypted = AES.new(secret, AES.MODE_ECB).encrypt(plain)
mac = HMAC.new(secret, digestmod=SHA256)
mac.update(crypted)
return {
"payload_typename": "GRP_TXT",
"chan_hash": channel_hash(secret),
"cipher_mac": mac.digest()[:2].hex(),
"crypted": crypted.hex(),
}
def _repo(tmp_path: Path) -> tuple[Repository, int]:
repo = Repository(tmp_path / "test.db")
run_id = repo.start_run("monitor", {}, None)
return repo, run_id
def _record_frame(repo: Repository, run_id: int, *, when, path: str, raw: dict, snr=2.0):
repo.record_observation(
run_id,
Observation(node=None, kind="packet", path=path, snr=snr, observed_at=when, raw=raw),
)
def test_channel_arrivals_match_by_decrypted_content(tmp_path: Path) -> None:
"""Every overheard copy of a channel message surfaces, each with its own path."""
repo, run = _repo(tmp_path)
now = utcnow()
wire = "Alice: hi mesh"
_record_frame(repo, run, when=now - timedelta(seconds=5), path="3d63", raw=_grp_txt_raw(SECRET, wire))
_record_frame(repo, run, when=now - timedelta(seconds=3), path="3d63,a1b2", raw=_grp_txt_raw(SECRET, wire))
_record_frame(repo, run, when=now - timedelta(seconds=1), path="", raw=_grp_txt_raw(SECRET, "Alice: other"))
message = ChatMessage(text=wire, is_channel=True, created_at=now)
arrivals = channel_arrivals(repo, message, channel_name="#general", secret=SECRET)
assert [a.hops for a in arrivals] == [("3d63",), ("3d63", "a1b2")]
assert distinct_paths(arrivals) == 2
repo.close()
def test_channel_arrivals_tolerate_the_sender_prefix_on_the_wire(tmp_path: Path) -> None:
"""Our own outbound message (stored as typed) matches its prefixed on-air copies."""
repo, run = _repo(tmp_path)
now = utcnow()
_record_frame(
repo, run, when=now + timedelta(seconds=2), path="3d63",
raw=_grp_txt_raw(SECRET, "Homestead: on my way"),
)
message = ChatMessage(text="on my way", outbound=True, is_channel=True, created_at=now)
arrivals = channel_arrivals(repo, message, channel_name="#general", secret=SECRET)
assert len(arrivals) == 1 and arrivals[0].hops == ("3d63",)
repo.close()
def test_channel_arrivals_carry_the_resend_counter(tmp_path: Path) -> None:
"""A decrypted frame's resend counter rides along, telling copies from retries."""
repo, run = _repo(tmp_path)
now = utcnow()
_record_frame(repo, run, when=now, path="", raw=_grp_txt_raw(SECRET, "Alice: hi", attempt=0))
_record_frame(
repo, run, when=now + timedelta(seconds=9), path="",
raw=_grp_txt_raw(SECRET, "Alice: hi", attempt=1),
)
message = ChatMessage(text="Alice: hi", is_channel=True, created_at=now)
arrivals = channel_arrivals(repo, message, channel_name="#general", secret=SECRET)
assert [a.resend for a in arrivals] == [0, 1]
assert distinct_paths(arrivals) == 1 # both arrived direct
repo.close()
def test_channel_arrivals_ignore_frames_outside_the_window(tmp_path: Path) -> None:
"""A matching frame far outside the message's window is someone else's message."""
repo, run = _repo(tmp_path)
now = utcnow()
_record_frame(
repo, run, when=now - timedelta(hours=2), path="3d63",
raw=_grp_txt_raw(SECRET, "Alice: hi"),
)
message = ChatMessage(text="Alice: hi", is_channel=True, created_at=now)
assert channel_arrivals(repo, message, channel_name="#general", secret=SECRET) == []
repo.close()
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."""
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( # 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"},
)
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()