From 4c725bd22a13d5583e85e6406a58de32e6d36d75 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Mon, 6 Jul 2026 20:31:44 -0400 Subject: [PATCH] Key channel history by identity and mark channel openness in chat Store channel messages under a slot-independent channel identity (sha256 of the effective secret) instead of the raw slot index, so reordering or re-keying slots no longer scrambles history. DB schema v3->v4 adds a channel_id column with an idempotent migration and a lazy backfill that maps legacy rows to the channel currently in each slot. Recognize the firmware's fixed-key default "Public" channel as public: add DEFAULT_PUBLIC_SECRET / KNOWN_PUBLIC_SECRETS and split is_name_derived (key reproducible from the name, so it can be re-derived on write) from is_public_channel (shared meshwide). The default channel no longer mislabels itself as private, and its well-known key is preserved on edit/reorder. Lead each chat picker row with a glyph: the channel's openness marker (# name-derived, globe fixed-key public, lock private) or, for a direct chat, a speech balloon once we've exchanged messages and a person before. A shared channel_glyph() keeps the channel manager and the picker in sync. Round out the messaging UX: live callable row titles and plain-text filtering in the TUI select screen, plus assorted device-picker, monitor, route-map, and config-editor menu refinements. Co-Authored-By: Claude Opus 4.8 --- meshtools/core/channels.py | 83 ++++++++++++ meshtools/core/connection.py | 22 ++++ meshtools/core/models.py | 42 ++++-- meshtools/persistence/db.py | 27 +++- meshtools/persistence/repository.py | 41 +++++- meshtools/services/chat_service.py | 110 +++++++++++++++- meshtools/tools/chat.py | 190 ++++++++++++++++++++++++---- meshtools/tools/devices.py | 16 ++- meshtools/tools/monitor.py | 3 +- meshtools/tools/route_map.py | 12 +- meshtools/ui/channels.py | 58 +++++++-- meshtools/ui/chat.py | 8 +- meshtools/ui/config_editor.py | 5 +- meshtools/ui/device_picker.py | 10 +- meshtools/ui/tui/select.py | 33 ++++- meshtools/ui/widgets.py | 46 ++++++- tests/test_channels.py | 68 +++++++++- tests/test_chat.py | 156 +++++++++++++++++++++-- tests/test_tui.py | 17 +++ 19 files changed, 858 insertions(+), 89 deletions(-) diff --git a/meshtools/core/channels.py b/meshtools/core/channels.py index a595ab5..99d6171 100644 --- a/meshtools/core/channels.py +++ b/meshtools/core/channels.py @@ -21,6 +21,16 @@ CHANNEL_SECRET_BYTES = 16 #: The default public channel every MeshCore device ships with on slot 0. DEFAULT_PUBLIC_NAME = "public" +#: MeshCore's built-in public channel ships on slot 0 as ``Public`` with this fixed 16-byte +#: key — it is *not* derived from the name, so it can't be recognized by the ``#`` / name-key +#: heuristics and must be matched on its well-known secret instead. +DEFAULT_PUBLIC_SECRET = bytes.fromhex("8b3387e9c5cdea6ac9e5edbaa115cd72") + +#: Secrets of well-known public channels — public (shared meshwide) regardless of their name +#: or whether their key is name-derived. Today just the firmware default above; a frozenset so +#: further authoritative public channels can be added over time. +KNOWN_PUBLIC_SECRETS = frozenset({DEFAULT_PUBLIC_SECRET}) + def is_public_name(name: str) -> bool: """Whether a channel name is a public one whose key derives from the name. @@ -35,6 +45,41 @@ def is_public_name(name: str) -> bool: return name.startswith("#") +def is_name_derived(name: str, secret: bytes) -> bool: + """Whether a channel's key is reproducible from its name. + + ``True`` for a ``#``-prefixed name and for any channel whose stored secret already equals + ``derive_secret(name)``. Such a channel need not store its key — the firmware recomputes it + from the name — so re-keying or reordering it can pass no secret at all. + + Args: + name: The channel name. + secret: The 16-byte secret stored in the slot. + + Returns: + ``True`` if the name alone reproduces the key. + """ + return is_public_name(name) or bytes(secret) == derive_secret(name) + + +def is_public_channel(name: str, secret: bytes) -> bool: + """Whether a channel is public — shared across the mesh rather than a private, off-mesh one. + + A channel counts as public when its key is name-derived (see :func:`is_name_derived`) *or* + it is a :data:`KNOWN_PUBLIC_SECRETS` channel — notably the firmware's default ``Public``, + whose key is a fixed well-known value rather than one derived from its name. The latter is + why a plain name/``#`` check alone would mislabel it as private. + + Args: + name: The channel name. + secret: The 16-byte secret stored in the slot. + + Returns: + ``True`` if the channel is public. + """ + return is_name_derived(name, secret) or bytes(secret) in KNOWN_PUBLIC_SECRETS + + def derive_secret(name: str) -> bytes: """Derive a channel's 16-byte secret from its name, exactly as the firmware does. @@ -96,6 +141,44 @@ def full_channel_hash(secret: bytes) -> str: return sha256(bytes(secret)).hexdigest() +def effective_secret(name: str, secret: bytes) -> bytes: + """Return the key a channel actually encrypts with, resolving public channels by name. + + A public channel's key is *derived from its name*, so the key material a device happens + to have stored in the slot (some firmware/simulators keep zeros) is not its true secret. + Private channels use their stored secret as-is. + + Args: + name: The channel name (a leading ``#`` marks it public). + secret: The 16-byte secret stored in the slot. + + Returns: + The channel's effective 16-byte secret. + """ + if is_name_derived(name, secret): + return derive_secret(name) + return bytes(secret) + + +def channel_identity(name: str, secret: bytes) -> str: + """Return a channel's stable identity — independent of which slot it occupies. + + The identity is ``sha256`` of the channel's :func:`effective_secret`, so it is intrinsic + to the channel itself (its key material) rather than to its UX position. Reordering + channel slots therefore never changes a channel's identity, and every device that shares + the channel — a public channel by name, a private one by its exchanged key — computes the + same value. Hashing keeps the raw secret out of anything the identity is stored in. + + Args: + name: The channel name. + secret: The 16-byte secret stored in the slot. + + Returns: + A 64-character lowercase hex identity. + """ + return full_channel_hash(effective_secret(name, secret)) + + def channel_hash(secret: bytes) -> str: """Return the 2-hex-character channel hash MeshCore shows for a secret. diff --git a/meshtools/core/connection.py b/meshtools/core/connection.py index ec9d885..792d077 100644 --- a/meshtools/core/connection.py +++ b/meshtools/core/connection.py @@ -455,6 +455,7 @@ class MeshCoreDevice(Device): name=info.get("adv_name", name), public_key=info.get("public_key", ""), key_prefix=info.get("public_key", "")[:12], + last_seen=_advert_time(info.get("last_advert")), ) ) return contacts @@ -1456,6 +1457,27 @@ def _as_float(value: object) -> Optional[float]: return None +def _advert_time(last_advert: object) -> Optional[datetime]: + """Convert a contact's ``last_advert`` Unix timestamp to a UTC datetime. + + The firmware reports the seconds-since-epoch of a contact's most recent advert; a + zero/absent/garbage value means "never heard", which maps to ``None``. + + Args: + last_advert: The raw ``last_advert`` field from a contact payload. + + Returns: + A timezone-aware UTC :class:`datetime`, or ``None`` when unknown. + """ + try: + seconds = int(last_advert) # type: ignore[arg-type] + except (TypeError, ValueError): + return None + if seconds <= 0: + return None + return datetime.fromtimestamp(seconds, tz=timezone.utc) + + def _mock_pub(prefix: str) -> str: """Build a 32-byte mock public key from a short hex prefix (simulator only). diff --git a/meshtools/core/models.py b/meshtools/core/models.py index 1040621..98e7e45 100644 --- a/meshtools/core/models.py +++ b/meshtools/core/models.py @@ -119,25 +119,28 @@ class Ack: def conversation_key( - is_channel: bool, channel_idx: Optional[int], peer: Optional[str] + is_channel: bool, channel_id: Optional[str], peer: Optional[str] ) -> str: """Return a stable key identifying a chat conversation. A conversation is either a channel or a direct exchange with one contact; this key is how history, unread counts, and the live screen all agree on which thread a message - belongs to. + belongs to. It is deliberately keyed on something *intrinsic* to the conversation — a + channel's identity (derived from its secret; see + :func:`~meshtools.core.channels.channel_identity`) and a contact's key prefix — never on + a slot index or list position, so reordering channels never re-points history. Args: is_channel: Whether the conversation is a channel. - channel_idx: The channel slot (for channel conversations). + channel_id: The channel's slot-independent identity (for channel conversations). peer: The contact's key prefix (for direct conversations). Returns: - ``"chan:"`` for a channel or ``"dm:"`` (lowercased) for a direct + ``"chan:"`` for a channel or ``"dm:"`` (lowercased) for a direct exchange. """ if is_channel: - return f"chan:{channel_idx}" + return f"chan:{channel_id}" return f"dm:{(peer or '').lower()}" @@ -155,7 +158,11 @@ class ChatMessage: text: The message body. outbound: ``True`` if we sent it, ``False`` if we received it. is_channel: Whether it belongs to a channel rather than a direct exchange. - channel_idx: The channel slot, for channel messages. + channel_id: The channel's slot-independent identity, for channel messages. This is + what the message is keyed to, so its history follows the channel across slot + reorders (see :func:`~meshtools.core.channels.channel_identity`). + channel_idx: The channel slot the message went out on / arrived on. Retained only + for reference and legacy backfill — the conversation key never uses it. peer: The other party's key prefix, for direct messages. peer_name: A friendly name for the peer/channel, snapshotted for display. snr: Signal-to-noise ratio (dB) of an inbound reception, if reported. @@ -167,6 +174,7 @@ class ChatMessage: text: str outbound: bool = False is_channel: bool = False + channel_id: Optional[str] = None channel_idx: Optional[int] = None peer: Optional[str] = None peer_name: Optional[str] = None @@ -177,17 +185,24 @@ class ChatMessage: @property def key(self) -> str: """The key of the conversation this message belongs to.""" - return conversation_key(self.is_channel, self.channel_idx, self.peer) + return conversation_key(self.is_channel, self.channel_id, self.peer) @classmethod def from_message( - cls, message: "Message", *, peer_name: Optional[str] = None + cls, + message: "Message", + *, + peer_name: Optional[str] = None, + channel_id: Optional[str] = None, ) -> "ChatMessage": """Build an inbound :class:`ChatMessage` from a received :class:`Message`. Args: message: The inbound message delivered by the event hub. peer_name: A friendly name for the sender, resolved from contacts if known. + channel_id: The channel's resolved identity (channel messages only). The wire + carries only a slot index, so the caller resolves it to the channel's + intrinsic identity before recording. Returns: The equivalent inbound :class:`ChatMessage`. @@ -196,6 +211,7 @@ class ChatMessage: text=message.text, outbound=False, is_channel=message.is_channel, + channel_id=channel_id if message.is_channel else None, channel_idx=message.channel, peer=None if message.is_channel else (message.sender or None), peer_name=peer_name, @@ -215,13 +231,19 @@ class Conversation: Attributes: label: Display name (e.g. ``#general`` or ``Alice``). is_channel: Whether this is a channel rather than a direct conversation. - channel_idx: The channel slot, for channels. + channel_idx: The channel slot to address for sending / live matching, for channels. + channel_id: The channel's slot-independent identity, used to key its history (see + :func:`~meshtools.core.channels.channel_identity`). + secret: The channel's 16-byte secret, for channels — used only to show its + public/private openness marker; ``None`` when unknown. contact: The contact, for direct conversations. """ label: str is_channel: bool channel_idx: Optional[int] = None + channel_id: Optional[str] = None + secret: Optional[bytes] = None contact: Optional[Contact] = None @property @@ -234,7 +256,7 @@ class Conversation: @property def key(self) -> str: """The conversation's stable key (see :func:`conversation_key`).""" - return conversation_key(self.is_channel, self.channel_idx, self.peer) + return conversation_key(self.is_channel, self.channel_id, self.peer) @dataclass(slots=True) diff --git a/meshtools/persistence/db.py b/meshtools/persistence/db.py index 6154344..d8dba3d 100644 --- a/meshtools/persistence/db.py +++ b/meshtools/persistence/db.py @@ -10,7 +10,7 @@ from __future__ import annotations import sqlite3 from pathlib import Path -SCHEMA_VERSION = 3 +SCHEMA_VERSION = 4 _SCHEMA = """ CREATE TABLE IF NOT EXISTS schema_meta ( @@ -98,7 +98,8 @@ CREATE TABLE IF NOT EXISTS messages ( run_id INTEGER REFERENCES runs(id) ON DELETE SET NULL, outbound INTEGER NOT NULL DEFAULT 0, is_channel INTEGER NOT NULL DEFAULT 0, - channel_idx INTEGER, + channel_id TEXT, -- a channel's slot-independent identity (its history key) + channel_idx INTEGER, -- the slot it went out on / arrived on (reference only) peer TEXT, peer_name TEXT, text TEXT NOT NULL, @@ -112,7 +113,6 @@ CREATE INDEX IF NOT EXISTS idx_trace_hops_trace ON trace_hops(trace_id); CREATE INDEX IF NOT EXISTS idx_tx_samples_run ON tx_samples(run_id); CREATE INDEX IF NOT EXISTS idx_observations_run ON observations(run_id); CREATE INDEX IF NOT EXISTS idx_observations_node ON observations(node); -CREATE INDEX IF NOT EXISTS idx_messages_channel ON messages(channel_idx); CREATE INDEX IF NOT EXISTS idx_messages_peer ON messages(peer); """ @@ -131,9 +131,30 @@ def connect(db_path: Path) -> sqlite3.Connection: conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON;") conn.executescript(_SCHEMA) + _migrate(conn) conn.execute( "INSERT OR REPLACE INTO schema_meta(key, value) VALUES ('version', ?)", (str(SCHEMA_VERSION),), ) conn.commit() return conn + + +def _migrate(conn: sqlite3.Connection) -> None: + """Bring an existing database's tables up to the current schema. + + ``executescript(_SCHEMA)`` only *creates* missing tables (``IF NOT EXISTS``); it cannot + add a column to a table an older version already created. Column additions are applied + here, guarded so the migration is idempotent and safe to run on every open. Indexes on + added columns are created here too (not in ``_SCHEMA``), so ``executescript`` never + references a column an older database hasn't grown yet. + """ + columns = {row["name"] for row in conn.execute("PRAGMA table_info(messages)")} + if "channel_id" not in columns: + # v3 -> v4: channel history moved from being keyed by slot index to a + # slot-independent channel identity. Existing rows are backfilled lazily at runtime + # (see Repository.backfill_channel_ids) once the device's channels can be read. + conn.execute("ALTER TABLE messages ADD COLUMN channel_id TEXT") + conn.execute( + "CREATE INDEX IF NOT EXISTS idx_messages_channel_id ON messages(channel_id)" + ) diff --git a/meshtools/persistence/repository.py b/meshtools/persistence/repository.py index 1e8c7de..eedc418 100644 --- a/meshtools/persistence/repository.py +++ b/meshtools/persistence/repository.py @@ -403,12 +403,13 @@ class Repository: peer = msg.peer.lower() if msg.peer else None cur = self._conn.execute( "INSERT INTO messages " - "(run_id, outbound, is_channel, channel_idx, peer, peer_name, text, snr, " - "acked, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "(run_id, outbound, is_channel, channel_id, channel_idx, peer, peer_name, text, " + "snr, acked, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( run_id, int(msg.outbound), int(msg.is_channel), + msg.channel_id, msg.channel_idx, peer, msg.peer_name, @@ -425,7 +426,7 @@ class Repository: self, *, is_channel: bool, - channel_idx: Optional[int] = None, + channel_id: Optional[str] = None, peer: Optional[str] = None, limit: int = 200, ) -> list[ChatMessage]: @@ -433,7 +434,7 @@ class Repository: Args: is_channel: Whether to load a channel conversation. - channel_idx: The channel slot (channel conversations). + channel_id: The channel's slot-independent identity (channel conversations). peer: The contact key prefix (direct conversations). limit: Maximum number of messages to return. @@ -441,7 +442,7 @@ class Repository: The messages in chronological order (ready to render as a transcript). """ if is_channel: - where, params = "is_channel = 1 AND channel_idx = ?", [channel_idx] + where, params = "is_channel = 1 AND channel_id = ?", [channel_id] else: where, params = "is_channel = 0 AND peer = ?", [(peer or "").lower()] rows = self._conn.execute( @@ -463,11 +464,38 @@ class Repository: rows = self._conn.execute( "SELECT * FROM messages WHERE id IN (" " SELECT MAX(id) FROM messages GROUP BY " - " CASE WHEN is_channel = 1 THEN 'chan:' || channel_idx " + " CASE WHEN is_channel = 1 THEN 'chan:' || channel_id " " ELSE 'dm:' || peer END)" ).fetchall() return {msg.key: msg for msg in (self._row_to_chat(r) for r in rows)} + def backfill_channel_ids(self, mapping: dict[int, str]) -> int: + """Give legacy channel messages an identity, keyed by the slot they were stored on. + + Messages written before channel history was keyed by identity have a ``NULL`` + ``channel_id``. There is no record of which channel occupied each slot back then, so + the best available guess is the channel *currently* at that slot. ``mapping`` maps a + slot index to the identity of the channel now there; only rows still missing an + identity are touched, so this is safe to run on every startup. + + Args: + mapping: Slot index to the current channel identity at that slot. + + Returns: + The number of legacy rows given an identity. + """ + changed = 0 + for idx, channel_id in mapping.items(): + cur = self._conn.execute( + "UPDATE messages SET channel_id = ? " + "WHERE is_channel = 1 AND channel_id IS NULL AND channel_idx = ?", + (channel_id, idx), + ) + changed += cur.rowcount + if changed: + self._conn.commit() + return changed + @staticmethod def _row_to_chat(row: sqlite3.Row) -> ChatMessage: """Rebuild a :class:`ChatMessage` from a ``messages`` row.""" @@ -475,6 +503,7 @@ class Repository: text=row["text"], outbound=bool(row["outbound"]), is_channel=bool(row["is_channel"]), + channel_id=row["channel_id"], channel_idx=row["channel_idx"], peer=row["peer"], peer_name=row["peer_name"], diff --git a/meshtools/services/chat_service.py b/meshtools/services/chat_service.py index af85290..6d5b660 100644 --- a/meshtools/services/chat_service.py +++ b/meshtools/services/chat_service.py @@ -16,8 +16,10 @@ communications, not overheard noise, so they are always recorded while a device from __future__ import annotations +import asyncio from typing import TYPE_CHECKING, Optional +from ..core.channels import channel_identity from ..core.connection import Unsubscribe from ..core.events import EventKind, MeshEvent from ..core.models import ChatMessage, Contact, Message, utcnow @@ -25,6 +27,20 @@ from ..core.models import ChatMessage, Contact, Message, utcnow if TYPE_CHECKING: from ..context import AppContext +#: Channel slots the firmware exposes; matches the chat/channel-manager probes. +_MAX_CHANNELS = 8 + + +def _fallback_channel_id(idx: Optional[int]) -> str: + """Identity for a channel we can't read (an unconfigured/unknown slot). + + Only used when the device has no channel to identify at ``idx`` — a degenerate case a + configured channel never hits. It is still slot-derived (there is nothing intrinsic to + key on), so it is the one place the old slot coupling survives, and only for channels + that have no real identity yet. + """ + return f"slot:{idx}" + class ChatService: """Records inbound messages to history and tracks unread counts per conversation. @@ -47,6 +63,10 @@ class ChatService: self._unread: dict[str, int] = {} self._active: Optional[str] = None self._session_count = 0 + # Cache of channel slot index -> the channel's intrinsic identity. The wire only + # tells us a message's slot index, so this bridges it to the identity history is + # keyed by. Rebuilt from the device on demand (see :meth:`refresh_channels`). + self._channel_ids: dict[int, str] = {} @property def active(self) -> bool: @@ -79,6 +99,50 @@ class ChatService: if key is not None: self._unread.pop(key, None) + async def refresh_channels(self) -> None: + """Rebuild the slot-index -> channel-identity map from the device's channel table. + + The map is what lets an inbound message (which carries only a slot index) be recorded + against its channel's intrinsic identity. It is rebuilt wholesale so a reordered, + renamed, re-keyed, or cleared slot is reflected accurately. + """ + device = await self._ctx.device() + ids: dict[int, str] = {} + for idx in range(_MAX_CHANNELS): + try: + payload = await device.get_channel(idx) + except Exception: # noqa: BLE001 - firmware may not support channel reads + break + if not payload: + continue # an empty slot; keep scanning (slots can be non-contiguous) + name = str(payload.get("channel_name") or "") + secret = bytes(payload.get("channel_secret") or b"\x00" * 16) + ids[idx] = channel_identity(name, secret) + self._channel_ids = ids + + async def channel_id_for(self, idx: int) -> str: + """Resolve a channel slot index to its intrinsic identity (reading the device once). + + Refreshes the cache on a miss, then falls back to a slot-derived identity only if the + device has no channel there. The fallback is cached so a stream of messages on an + unconfigured slot doesn't re-read the device on every one. + + Args: + idx: The channel slot index the wire reported. + + Returns: + The channel's identity, suitable for keying its history. + """ + cid = self._channel_ids.get(idx) + if cid is not None: + return cid + await self.refresh_channels() + cid = self._channel_ids.get(idx) + if cid is None: + cid = _fallback_channel_id(idx) + self._channel_ids[idx] = cid + return cid + async def start(self) -> None: """Begin recording inbound messages to history. Idempotent. @@ -107,6 +171,22 @@ class ChatService: self._unsubscribe = self._ctx.events.subscribe(on_event, EventKind.MESSAGE) self._run_id = run_id self._ctx.log.info("chat recording (run %s)", run_id) + await self._prime_channels() + + async def _prime_channels(self) -> None: + """Warm the channel-identity cache and backfill legacy (index-keyed) history. + + Reading the channels up front means most inbound messages resolve their identity + from the cache without a device round-trip. It also backfills any pre-identity + messages using the channels currently in each slot — best-effort, since the old + slot-to-channel mapping wasn't recorded. + """ + try: + await self.refresh_channels() + if self._channel_ids: + self._ctx.repo.backfill_channel_ids(dict(self._channel_ids)) + except Exception as exc: # noqa: BLE001 - priming is best-effort, never fatal + self._ctx.log.debug("chat: channel priming failed: %s", exc) async def stop(self) -> None: """Stop recording and close the run record. Idempotent. @@ -134,11 +214,38 @@ class ChatService: def _record_inbound(self, run_id: int, message: Message) -> None: """Persist one inbound message and bump its conversation's unread count. + Channel messages are keyed by identity, but the wire carries only a slot index. When + the slot is already cached this resolves synchronously; on a cache miss (a channel + added since the last device read) resolution is deferred to the event loop so the + message is still recorded against its intrinsic identity. + Args: run_id: The owning background ``chat`` run. message: The received message to record. """ - chat = ChatMessage.from_message(message) + if message.is_channel: + channel_id = self._channel_ids.get(message.channel) + if channel_id is None: + asyncio.ensure_future(self._resolve_then_record(run_id, message)) + return + else: + channel_id = None + self._store_inbound(run_id, message, channel_id) + + async def _resolve_then_record(self, run_id: int, message: Message) -> None: + """Resolve an uncached channel's identity off the event loop, then record it.""" + try: + channel_id = await self.channel_id_for(message.channel) + except Exception as exc: # noqa: BLE001 - fall back rather than drop the message + self._ctx.log.debug("chat: channel resolve failed: %s", exc) + channel_id = _fallback_channel_id(message.channel) + self._store_inbound(run_id, message, channel_id) + + def _store_inbound( + self, run_id: int, message: Message, channel_id: Optional[str] + ) -> None: + """Persist an inbound message under a resolved identity and bump its unread count.""" + chat = ChatMessage.from_message(message, channel_id=channel_id) self._session_count += 1 if chat.key != self._active: self._unread[chat.key] = self._unread.get(chat.key, 0) + 1 @@ -192,6 +299,7 @@ class ChatService: text=text, outbound=True, is_channel=True, + channel_id=await self.channel_id_for(index), channel_idx=index, peer_name=label, created_at=utcnow(), diff --git a/meshtools/tools/chat.py b/meshtools/tools/chat.py index edcd535..7dddbb9 100644 --- a/meshtools/tools/chat.py +++ b/meshtools/tools/chat.py @@ -13,17 +13,20 @@ send, so ``history`` reflects the full transcript regardless of which path produ from __future__ import annotations import asyncio -from typing import Any, Optional +import time +from typing import Any, Callable, Optional, Union import typer from rich.table import Table from rich.text import Text from ..context import AppContext +from ..core.channels import DEFAULT_PUBLIC_SECRET, channel_identity from ..core.connection import Device from ..core.events import EventKind, MeshEvent from ..core.models import ChatMessage, Contact, Conversation from ..ui.tui import Choice, Separator +from ..ui.widgets import channel_glyph from .base import Tool, ToolResult, register #: How many channel slots to probe when listing channels to chat on. @@ -108,17 +111,25 @@ class ChatTool(Tool): device = await ctx.device() channels = await _read_channels(device) contacts = await device.get_contacts() + # A stable snapshot orders the rows (so the list doesn't reshuffle under the cursor), + # while a self-refreshing view feeds each row's live preview (see _LiveLasts). lasts = ctx.repo.last_chat_messages() + live = _LiveLasts(ctx, seed=lasts) items: list = [Separator("── Channels ──")] for conversation in channels: - items.append(Choice(title=_title(ctx, conversation, lasts), value=conversation)) + items.append(Choice(title=_row_title(ctx, conversation, live), value=conversation)) items.append(Separator("── Direct ──")) if contacts: - for contact in contacts: - conversation = Conversation(label=contact.name, is_channel=False, contact=contact) - items.append(Choice(title=_title(ctx, conversation, lasts), value=conversation)) + # List contacts by recency — those with messages first, newest exchange at the + # top — then the never-contacted ones alphabetically (see _recency_key). + direct = [ + Conversation(label=c.name, is_channel=False, contact=c) for c in contacts + ] + direct.sort(key=lambda conv: _recency_key(conv, lasts)) + for conversation in direct: + items.append(Choice(title=_row_title(ctx, conversation, live), value=conversation)) else: items.append(Separator(" (no contacts yet — receive an advert first)")) @@ -187,8 +198,9 @@ class ChatTool(Tool): channel = params.get("channel") to = params.get("to") if channel is not None: + channel_id = await ctx.chat.channel_id_for(int(channel)) messages = ctx.repo.recent_chat_messages( - is_channel=True, channel_idx=int(channel), limit=limit + is_channel=True, channel_id=channel_id, limit=limit ) label = f"#{channel}" else: @@ -382,11 +394,27 @@ async def _read_channels(device: Device) -> list[Conversation]: break if channel: name = str(channel.get("channel_name") or idx) + secret = bytes(channel.get("channel_secret") or b"\x00" * 16) conversations.append( - Conversation(label=name, is_channel=True, channel_idx=idx) + Conversation( + label=name, + is_channel=True, + channel_idx=idx, + channel_id=channel_identity(name, secret), + secret=secret, + ) ) if not any(c.channel_idx == 0 for c in conversations): - conversations.insert(0, Conversation(label="Public", is_channel=True, channel_idx=0)) + conversations.insert( + 0, + Conversation( + label="Public", + is_channel=True, + channel_idx=0, + channel_id="slot:0", + secret=DEFAULT_PUBLIC_SECRET, + ), + ) return conversations @@ -411,29 +439,147 @@ def _resolve_contact(contacts: list[Contact], needle: str) -> Contact: raise typer.BadParameter(f"no contact matches {needle!r}") -def _title(ctx: AppContext, conversation: Conversation, lasts: dict) -> str: - """Build a picker row title: label, an unread marker, and a last-message snippet. +def _recency_key(conversation: Conversation, lasts: dict) -> tuple: + """Sort key ordering conversations by recency, then name. - Choice titles render as plain text, so markup is avoided; the unread count shows as a - ``●`` badge and the most recent message as a short trailing preview. + Conversations that have been chatted with sort first, most-recent exchange at the top; + those never chatted with sort after them, alphabetically by label. The leading ``0``/``1`` + keeps the two groups apart so their differently-typed tie-breakers never compare. Args: - ctx: Shared application context (for the unread count). - conversation: The conversation the row represents. + conversation: The conversation to rank. lasts: Map of conversation key to its most recent message. Returns: - A single-line plain-text title. + A tuple usable as a ``sorted`` key. """ - parts = [conversation.label] - unread = ctx.chat.unread(conversation.key) - if unread: - parts.append(f" ● {unread}") last: Optional[ChatMessage] = lasts.get(conversation.key) if last is not None: - who = "you: " if last.outbound else "" - parts.append(f" · {who}{last.text[:32]}") - return "".join(parts) + return (0, -last.created_at.timestamp()) + return (1, conversation.label.casefold()) + + +class _LiveLasts: + """A self-refreshing view of each conversation's most recent message. + + The picker snapshots :meth:`~meshtools.persistence.repository.Repository.last_chat_messages` + once to *order* the rows (so the list never reshuffles under the cursor), but the row + previews read through this so a message arriving while the list is open updates the + sender/text preview on the next repaint. It re-queries the repository at most a few times a + second (bounded by ``ttl``) rather than once per row per repaint, so a wide list stays cheap. + """ + + def __init__(self, ctx: AppContext, *, seed: dict, ttl: float = 0.5) -> None: + """Bind to a context, seeding the cache with the snapshot already loaded at open.""" + self._ctx = ctx + self._ttl = ttl + self._cache = seed + self._at = time.monotonic() + + def get(self, key: str) -> Optional[ChatMessage]: + """Return the latest message for ``key``, refreshing the cache once its TTL lapses.""" + now = time.monotonic() + if now - self._at >= self._ttl: + try: + self._cache = self._ctx.repo.last_chat_messages() + except Exception: # noqa: BLE001 - keep the last good snapshot on a read error + pass + self._at = now + return self._cache.get(key) + + +#: Column width (display cells) the conversation label is padded/ellipsized to, so the unread +#: badge and message preview line up in fixed lanes down the picker. +_LABEL_WIDTH = 22 +#: Width of the unread-badge lane between the label and the preview (fits ``● 999``). +_BADGE_WIDTH = 5 +#: Longest message preview shown before it is ellipsized. +_PREVIEW_WIDTH = 42 + + +def _row_title( + ctx: AppContext, conversation: Conversation, lasts: "_LiveLasts" +) -> Callable[[], Union[str, Text]]: + """Return a picker-row title *callable* the select screen re-renders on each repaint. + + Both the unread badge and the last-message preview are read live, so a message arriving + while the picker sits open updates that row's ``●`` count *and* its sender/text preview on + the next repaint (the session already repaints ~1×/s for the header). + + Args: + ctx: Shared application context (for the live unread count). + conversation: The conversation the row represents. + lasts: The self-refreshing latest-message view feeding the preview. + + Returns: + A zero-argument callable producing the current row title. + """ + return lambda: _title(ctx, conversation, lasts) + + +def _title( + ctx: AppContext, conversation: Conversation, lasts: "_LiveLasts" +) -> Union[str, Text]: + """Build a picker row as fixed-width columns: label, unread badge, message preview. + + Alignment carries the readability: the label is padded to a fixed lane, the unread ``●`` + badge sits in its own lane, and the preview lines up across every row regardless of how + long the names or counts are. When there is unread, the row is returned as a Rich + :class:`~rich.text.Text` so the ``●`` glyph alone can be tinted red; otherwise a plain + string suffices. + + Args: + ctx: Shared application context (for the live unread count). + conversation: The conversation the row represents. + lasts: The self-refreshing latest-message view. + + Returns: + The row title — a plain ``str``, or a ``Text`` with a red ``●`` when unread. + """ + unread = ctx.chat.unread(conversation.key) + badge = f"● {unread}" if unread else "" + last = lasts.get(conversation.key) + preview = _preview(last) if last is not None else "" + # A channel leads with its openness marker (# / 🌐 / 🔒); a direct chat with a person glyph + # — 💬 once we've exchanged messages, 👤 for a contact we haven't talked to yet. Every glyph + # is one double-width cell, so the label lane still lines up across rows. + if conversation.is_channel: + glyph = channel_glyph(conversation.label, conversation.secret) + else: + glyph = "💬" if last is not None else "👤" + line = f"{glyph} {_fit(conversation.label, _LABEL_WIDTH)} {badge:<{_BADGE_WIDTH}} {preview}".rstrip() + if not unread: + return line + text = Text(line) + dot = line.index("●") # only the badge carries this glyph; colour just it red + text.stylize("err", dot, dot + 1) + return text + + +def _preview(last: ChatMessage, *, width: int = _PREVIEW_WIDTH) -> str: + """One-line preview of the latest message, ``you:``-prefixed when we sent it. + + Only our own outbound messages get an author prefix. Inbound channel messages already + carry the sender's node name inline in their text (the firmware embeds it), and an inbound + direct message's author is the row's own label — so neither needs one added here. + + Args: + last: The most recent message in the conversation. + width: The column budget before the preview is ellipsized. + + Returns: + A single-line, length-bounded preview string. + """ + who = "you: " if last.outbound else "" + body = f"{who}{last.text}".replace("\n", " ") + return body[: width - 1] + "…" if len(body) > width else body + + +def _fit(text: str, width: int) -> str: + """Left-justify ``text`` to ``width`` columns, ellipsizing anything that would overflow.""" + if len(text) > width: + return text[: width - 1] + "…" + return text.ljust(width) def _history_table(label: str, messages: list[ChatMessage]) -> Table: diff --git a/meshtools/tools/devices.py b/meshtools/tools/devices.py index ac53012..cf55518 100644 --- a/meshtools/tools/devices.py +++ b/meshtools/tools/devices.py @@ -36,24 +36,26 @@ class DevicesTool(Tool): order = 5 async def prompt_params(self, ctx: AppContext) -> dict[str, Any]: - """In the menu, offer to change the session's active device. + """In the menu, show the device picker so the user can change the active device. + + The picker itself carries a "Back" row, so it reads like every other menu screen + rather than a bare yes/no confirmation. Args: ctx: Shared application context. Returns: - ``{"select": True}`` when the user opted to re-pick a device, else ``{}``. + Always ``{}`` — the table in :meth:`run` reflects the (possibly changed) device. """ if ctx.mock: return {} - change = await ctx.ui.confirm("Change the active companion device?", default=False) - if not change: - return {} from ..ui.device_picker import prompt_device devices = discover_devices() - chosen = await prompt_device(ctx.ui, devices, ctx.device_store.load()) + chosen = await prompt_device( + ctx.ui, devices, ctx.device_store.load(), cancel_label="Back" + ) if chosen is not None: ctx.selected_device = chosen ctx.port_override = chosen.port @@ -61,7 +63,7 @@ class DevicesTool(Tool): f"[ok]●[/ok] active device set to [accent]{chosen.label}[/accent] " "[muted](remembered once it connects)[/muted]" ) - return {"select": True} + return {} async def run(self, ctx: AppContext, params: dict[str, Any]) -> ToolResult: """Enumerate serial devices and render them as a table. diff --git a/meshtools/tools/monitor.py b/meshtools/tools/monitor.py index f256a0c..8c54892 100644 --- a/meshtools/tools/monitor.py +++ b/meshtools/tools/monitor.py @@ -23,7 +23,7 @@ from rich.text import Text from ..context import AppContext from ..core.models import HeardNode -from ..ui.tui import Choice +from ..ui.tui import Choice, Separator from .base import Tool, ToolResult, register @@ -52,6 +52,7 @@ class MonitorTool(Tool): [ Choice(toggle_label, value="toggle"), Choice("View heard nodes (all time)", value="view"), + Separator(" "), Choice("Back", value="__back__"), ], ) diff --git a/meshtools/tools/route_map.py b/meshtools/tools/route_map.py index 309fd42..6c7582d 100644 --- a/meshtools/tools/route_map.py +++ b/meshtools/tools/route_map.py @@ -19,11 +19,12 @@ from ..context import AppContext from ..services import route_stability, trace_runner from ..services.route_stability import RouteGraph from ..ui.theme import snr_style -from ..ui.tui import Choice +from ..ui.tui import Choice, Separator from ..viz.route_graph import render_route_graph from .base import Tool, ToolResult, register _WHOLE_MESH = "(whole mesh)" +_BACK = "__back__" @register @@ -47,9 +48,14 @@ class RouteMapTool(Tool): targets = ctx.repo.traced_targets() choice = await ctx.ui.select( "Map routing for:", - [Choice(_WHOLE_MESH, _WHOLE_MESH), *(Choice(t, t) for t in targets)], + [ + Choice(_WHOLE_MESH, _WHOLE_MESH), + *(Choice(t, t) for t in targets), + Separator(" "), + Choice("Back", _BACK), + ], ) - if choice is None: + if choice in (None, _BACK): return None samples = await ctx.ui.text( "Take how many fresh traces first? (0 = use stored history only)", diff --git a/meshtools/ui/channels.py b/meshtools/ui/channels.py index 3459e14..0c2b70c 100644 --- a/meshtools/ui/channels.py +++ b/meshtools/ui/channels.py @@ -23,9 +23,11 @@ from rich.text import Text from ..core.channels import ( channel_hash, + channel_identity, derive_secret, full_channel_hash, - is_public_name, + is_name_derived, + is_public_channel, normalize_secret, parse_share_url, random_secret, @@ -35,6 +37,7 @@ from ..core.connection import Device from ..core.models import Conversation from .qr import qr_text from .tui import Choice, Separator +from .widgets import channel_glyph if TYPE_CHECKING: from ..context import AppContext @@ -72,10 +75,18 @@ class ChannelSlot: name: str secret: bytes + @property + def is_name_derived(self) -> bool: + """Whether this channel's key is reproducible from its name (so it needn't be stored).""" + return is_name_derived(self.name, self.secret) + @property def is_public(self) -> bool: - """Whether this channel's key is derived from its name (a public channel).""" - return is_public_name(self.name) or self.secret == derive_secret(self.name) + """Whether this channel is public (shared meshwide) rather than a private one. + + Covers both name-derived channels and the firmware's fixed-key default ``Public``. + """ + return is_public_channel(self.name, self.secret) @property def hash(self) -> str: @@ -87,10 +98,21 @@ class ChannelSlot: """The channel's complete ``sha256(secret)`` digest; its first byte is :attr:`hash`.""" return full_channel_hash(self.secret) + @property + def identity(self) -> str: + """The channel's slot-independent identity, used to key its chat history.""" + return channel_identity(self.name, self.secret) + @property def conversation(self) -> Conversation: """A :class:`~meshtools.core.models.Conversation` for opening this channel in chat.""" - return Conversation(label=self.name, is_channel=True, channel_idx=self.idx) + return Conversation( + label=self.name, + is_channel=True, + channel_idx=self.idx, + channel_id=self.identity, + secret=self.secret, + ) async def manage_channels(ctx: "AppContext") -> int: @@ -166,8 +188,9 @@ def _next_free_slot(slots: list[ChannelSlot]) -> Optional[int]: def _slot_label(slot: ChannelSlot) -> str: """Format a channel for a menu row: name, public/private, and hash (no slot index).""" - lock = "# public " if slot.is_public else "🔒 private" - return f"{slot.name:<18.18} {lock} hash {slot.hash}" + glyph = channel_glyph(slot.name, slot.secret) # # / 🌐 / 🔒 + word = "private" if glyph == "🔒" else "public" + return f"{slot.name:<18.18} {glyph} {word:<7} hash {slot.hash}" # --- menus ------------------------------------------------------------------- @@ -212,6 +235,7 @@ async def _channel_detail(ctx: "AppContext", device: Device, slot: ChannelSlot) Choice(title="💬 Open in chat", value=_CHAT), Choice(title="✎ Rename / change key", value=_EDIT), Choice(title="🗑 Clear this slot", value=_CLEAR), + Separator(" "), Choice(title="Back", value=_BACK), ], ) @@ -324,7 +348,7 @@ async def _edit(ctx: "AppContext", device: Device, slot: ChannelSlot) -> bool: name = name.strip() key = await ctx.ui.text( "Channel key (32 hex chars; blank to derive from the name):", - default="" if slot.is_public else slot.secret.hex(), + default="" if slot.is_name_derived else slot.secret.hex(), validate=_optional_secret, ) if key is None: @@ -351,8 +375,8 @@ async def _clear(ctx: "AppContext", device: Device, slot: ChannelSlot) -> bool: async def _write_slot(device: Device, idx: int, slot: ChannelSlot) -> None: - """Write ``slot``'s contents into slot ``idx`` (public channels re-derive their key).""" - secret = None if slot.is_public else slot.secret + """Write ``slot``'s contents into slot ``idx`` (name-derived channels re-derive their key).""" + secret = None if slot.is_name_derived else slot.secret await device.set_channel(idx, slot.name, secret) @@ -388,7 +412,13 @@ async def _reorder_channels( order = await ctx.ui.reorder("Reorder channels", labels) if not order or order == list(range(len(slots))): return 0 # cancelled or left unchanged - return await _apply_order(device, slots, order) + changes = await _apply_order(device, slots, order) + if changes: + # Chat history is keyed by each channel's intrinsic identity, not its slot, so it + # follows the channels automatically — reordering needs no history migration. Just + # refresh the slot-index→identity cache so freshly-moved slots resolve correctly. + await ctx.chat.refresh_channels() + return changes # --- shared views ------------------------------------------------------------ @@ -423,7 +453,13 @@ async def _show_key(ctx: "AppContext", slot: ChannelSlot) -> None: table.add_column("", style="muted") table.add_column("") table.add_row("Name", Text(slot.name, style="brand")) - table.add_row("Type", "public (key from name)" if slot.is_public else "private") + if slot.is_name_derived: + kind = "public (key from name)" + elif slot.is_public: + kind = "public (default channel)" + else: + kind = "private" + table.add_row("Type", kind) table.add_row("Hash", _hash_text(slot.full_hash)) table.add_row("Key", Text(slot.secret.hex(), style="warn")) table.add_row("Link", Text(share_url(slot.name, slot.secret), style="accent")) diff --git a/meshtools/ui/chat.py b/meshtools/ui/chat.py index 5c04bb7..f46a0d5 100644 --- a/meshtools/ui/chat.py +++ b/meshtools/ui/chat.py @@ -470,7 +470,7 @@ async def open_chat(ctx: "AppContext", conversation: Conversation) -> int: history = ctx.repo.recent_chat_messages( is_channel=conversation.is_channel, - channel_idx=conversation.channel_idx, + channel_id=conversation.channel_id, peer=conversation.peer, limit=_HISTORY_LIMIT, ) @@ -492,7 +492,11 @@ async def open_chat(ctx: "AppContext", conversation: Conversation) -> int: message = event.message if message is not None and _belongs(message, conversation): peer_name = names.get((message.sender or "").lower()) - screen.append(ChatMessage.from_message(message, peer_name=peer_name)) + screen.append( + ChatMessage.from_message( + message, peer_name=peer_name, channel_id=conversation.channel_id + ) + ) unsubscribe = ctx.events.subscribe(on_event, EventKind.MESSAGE) try: diff --git a/meshtools/ui/config_editor.py b/meshtools/ui/config_editor.py index e0af88f..b31b366 100644 --- a/meshtools/ui/config_editor.py +++ b/meshtools/ui/config_editor.py @@ -237,13 +237,15 @@ async def _stage_preset(ctx: AppContext, pending: dict[str, Any]) -> None: """Pick a standard radio preset and stage all of its fields for review/apply.""" from ..core.device_config import RADIO_PRESETS - items = [ + items: list = [ Choice( title=f"{p.name}: {p.freq} MHz, BW {p.bw}, SF{p.sf}, CR{p.cr} — {p.help}", value=i, ) for i, p in enumerate(RADIO_PRESETS) ] + items.append(Separator(" ")) + items.append(Choice(title="Back", value=None)) idx = await ctx.ui.select("Apply which radio preset?", items) if idx is None: return @@ -303,6 +305,7 @@ async def _danger_zone(ctx: AppContext, device: Any, snapshot: dict) -> bool: Choice("Export private key", value="export_key"), Choice("Import private key", value="import_key"), Choice("Factory reset (erase all)", value="factory_reset"), + Separator(" "), Choice("Back", value=None), ], ) diff --git a/meshtools/ui/device_picker.py b/meshtools/ui/device_picker.py index 4c80413..ee33e63 100644 --- a/meshtools/ui/device_picker.py +++ b/meshtools/ui/device_picker.py @@ -32,6 +32,8 @@ async def prompt_device( ui: "Ui", devices: list[DiscoveredDevice], remembered: Optional[RememberedDevice], + *, + cancel_label: str = "Skip (use --mock / configure later)", ) -> Optional[DiscoveredDevice]: """Prompt the user to choose a companion device. @@ -39,10 +41,12 @@ async def prompt_device( ui: The interactive UI surface used to render the picker. devices: Discovered devices (likely-LoRa first). remembered: The remembered default, if any, used to mark and preselect a row. + cancel_label: Label for the final "don't pick" row — "Skip…" at startup (there is + no going back yet), "Back" when re-picking from the devices tool. Returns: - The chosen :class:`DiscoveredDevice`, or ``None`` if the user cancelled or chose to - skip device selection (e.g. to run against ``--mock`` / configure later). + The chosen :class:`DiscoveredDevice`, or ``None`` if the user cancelled or chose the + cancel row (e.g. to run against ``--mock`` / configure later). """ if not devices: await ui.view( @@ -70,6 +74,6 @@ async def prompt_device( default = device items.append(Separator(" ")) - items.append(Choice(title="skip (use --mock / configure later)", value=None)) + items.append(Choice(title=cancel_label, value=None)) return await ui.select("Select a companion device:", items, default=default) diff --git a/meshtools/ui/tui/select.py b/meshtools/ui/tui/select.py index 1c3d365..9b6176a 100644 --- a/meshtools/ui/tui/select.py +++ b/meshtools/ui/tui/select.py @@ -9,7 +9,7 @@ arbitrary objects, so the same screen drives the main menu (tool names), the dev from __future__ import annotations from dataclasses import dataclass -from typing import Any, Optional +from typing import Any, Callable, Optional, Union from rich.text import Text @@ -22,13 +22,26 @@ class Choice: """One selectable row. Attributes: - title: Text shown for the row. + title: Text shown for the row — a plain string or a Rich :class:`~rich.text.Text` + (for a coloured segment such as an unread badge). Either may instead be a + zero-argument callable resolved fresh on every repaint, so a row can track state + that changes while the list is open. value: Value returned when the row is chosen. """ - title: str + title: Union[str, Text, Callable[[], Union[str, Text]]] value: Any + @property + def label(self) -> Union[str, Text]: + """The row's current text, resolving a callable title on each read.""" + return self.title() if callable(self.title) else self.title + + +def _plain(label: Union[str, Text]) -> str: + """The plain-text form of a row label, for filtering (a :class:`Text` keeps its ``plain``).""" + return label.plain if isinstance(label, Text) else label + @dataclass class Separator: @@ -93,7 +106,9 @@ class SelectScreen(Screen): return self._items needle = self._filter.lower() return [ - it for it in self._items if isinstance(it, Choice) and needle in it.title.lower() + it + for it in self._items + if isinstance(it, Choice) and needle in _plain(it.label).lower() ] def _choices(self, rows: Optional[list] = None) -> list: @@ -120,7 +135,15 @@ class SelectScreen(Screen): is_sel = item is selected pointer = "❯ " if is_sel else " " style = "brand" if is_sel else "" - text = Text(pointer + item.title, style=style, no_wrap=True, overflow="ellipsis") + label = item.label + # A Text label carries its own spans (e.g. a red badge); keep them and lay the + # row's base style underneath, so the highlight tints the row while the badge + # keeps its colour. A plain string is styled uniformly as before. + text = Text(pointer, style=style) + text.append_text(label if isinstance(label, Text) else Text(label)) + text.style = style + text.no_wrap = True + text.overflow = "ellipsis" text.truncate(width) lines.append(render_to_ansi(text, width)) if not choices: diff --git a/meshtools/ui/widgets.py b/meshtools/ui/widgets.py index 0b11ca5..5a4f176 100644 --- a/meshtools/ui/widgets.py +++ b/meshtools/ui/widgets.py @@ -18,6 +18,7 @@ from rich.table import Table from rich.text import Text from .. import __version__ +from ..core.channels import is_name_derived, is_public_channel, is_public_name from ..core.models import ( LOCAL_DEVICE_LABEL, Contact, @@ -34,6 +35,30 @@ if TYPE_CHECKING: #: Maps a hop's raw key-prefix hash to a display label (a contact name when known). NodeResolver = Callable[[Optional[str]], Optional[str]] + +def channel_glyph(name: str, secret: Optional[bytes]) -> str: + """The one-character openness marker for a channel, shared across channel-facing screens. + + ``#`` marks a name-derived (``#``-style) channel, ``🌐`` a fixed-key well-known public + channel (e.g. the firmware default ``Public``), and ``🔒`` a private one. Every glyph is a + single double-width cell, so callers can prefix rows with ``"{glyph} "`` without disturbing + column alignment. When the secret is unknown, the name alone is used to guess. + + Args: + name: The channel name. + secret: The channel's 16-byte secret, or ``None`` when only the name is known. + + Returns: + A single-character glyph. + """ + if secret is None: + return "#" if is_public_name(name) else "🔒" + if is_name_derived(name, secret): + return "#" + if is_public_channel(name, secret): + return "🌐" + return "🔒" + #: Non-breaking space, used in the route line to keep ``name (hash)`` and a node's #: trailing arrow on the same line so wraps only ever land *after* an arrow. _NBSP = " " @@ -411,6 +436,24 @@ def stats_panel( return Panel(body, title="[accent]trace summary[/accent]", border_style="accent", expand=False) +def _contact_recency_key(contact: Contact) -> tuple: + """Sort key ordering contacts by last-heard time (newest first), then name. + + Contacts with a known ``last_seen`` sort first, most-recent advert at the top; those + never heard sort after them, alphabetically. The leading ``0``/``1`` keeps the two + groups apart so their differently-typed tie-breakers never compare. + + Args: + contact: The contact to rank. + + Returns: + A tuple usable as a ``sorted`` key. + """ + if contact.last_seen is not None: + return (0, -contact.last_seen.timestamp()) + return (1, contact.name.casefold()) + + def nodes_table( self_name: str, self_key: str, @@ -442,7 +485,8 @@ def nodes_table( highlighted_hash(self_key, prefix_bytes) if self_key else unknown, ) table.add_section() - for c in contacts: + # Most-recently-heard contacts first; those never heard fall to the end, alphabetically. + for c in sorted(contacts, key=_contact_recency_key): table.add_row( Text(c.name, style="brand"), highlighted_hash(c.public_key, prefix_bytes) if c.public_key else unknown, diff --git a/tests/test_channels.py b/tests/test_channels.py index 3957eeb..83c12c2 100644 --- a/tests/test_channels.py +++ b/tests/test_channels.py @@ -17,6 +17,7 @@ from meshtools.context import AppContext from meshtools.core.admin_store import AdminStore from meshtools.core.channels import ( CHANNEL_SECRET_BYTES, + DEFAULT_PUBLIC_SECRET, channel_hash, derive_secret, full_channel_hash, @@ -29,7 +30,13 @@ from meshtools.core.config import Settings from meshtools.core.device_store import DeviceStore from meshtools.persistence.repository import Repository from meshtools.tools.channels import ChannelsTool -from meshtools.ui.channels import ChannelSlot, _apply_order, _next_free_slot, _read_slots +from meshtools.services.chat_service import ChatService +from meshtools.ui.channels import ( + ChannelSlot, + _apply_order, + _next_free_slot, + _read_slots, +) from meshtools.ui.qr import qr_text _ANSI = re.compile(r"\x1b\[[0-9;]*m") @@ -131,8 +138,18 @@ def test_channel_slot_classifies_public_and_private() -> None: assert derived.is_public # key derived from the name, even without a leading # private = ChannelSlot(idx=2, name="Ops", secret=bytes(range(16))) assert not private.is_public + assert not private.is_name_derived + # The firmware default "Public" is public but keyed by a fixed well-known secret, not by + # its name — so it reads as public without being name-derived (its key must be preserved). + default_public = ChannelSlot(idx=0, name="Public", secret=DEFAULT_PUBLIC_SECRET) + assert default_public.is_public + assert not default_public.is_name_derived + assert channel_hash(DEFAULT_PUBLIC_SECRET) == "11" # the fingerprint devices report for it assert private.conversation.is_channel and private.conversation.channel_idx == 2 assert private.conversation.label == "Ops" # raw name, no forced leading '#' + # The conversation is keyed by the channel's identity, not its slot. + assert private.conversation.channel_id == private.identity + assert private.conversation.key == f"chan:{private.identity}" def test_next_free_slot_finds_gaps_and_full() -> None: @@ -162,6 +179,55 @@ async def test_apply_order_relays_channels_into_new_positions(ctx: AppContext) - assert after[2].name == "Alpha" and after[2].secret == bytes(range(16)) +def test_channel_identity_is_stable_across_slot_moves() -> None: + """A channel's identity depends on its key material, not on which slot it occupies.""" + at_two = ChannelSlot(idx=2, name="Ops", secret=bytes(range(16))) + at_five = ChannelSlot(idx=5, name="Ops", secret=bytes(range(16))) + assert at_two.identity == at_five.identity # same channel, different slot → same identity + + other = ChannelSlot(idx=2, name="Ops", secret=bytes(range(16, 32))) + assert other.identity != at_two.identity # a different key → a different channel + + +async def test_reorder_keeps_history_because_key_is_intrinsic(ctx: AppContext) -> None: + """Reordering channels leaves each channel's transcript with it — no migration needed. + + Reproduces the reported bug's setup (a channel moved to a new slot) and asserts the fix: + because history is keyed by channel identity rather than slot, the moved channel keeps + its own messages and the slot it landed on inherits none. + """ + device = await ctx.device() + await device.set_channel(0, "Alpha", bytes(range(16))) + await device.set_channel(1, "Beta", bytes(range(16, 32))) + + chat = ChatService(ctx) + await chat.start() + try: + await chat.send_channel(0, "hello from alpha", label="Alpha") + await chat.send_channel(1, "hello from beta", label="Beta") + + slots = await _read_slots(device) + before = {s.name: s for s in slots} + alpha_id, beta_id = before["Alpha"].identity, before["Beta"].identity + + # Swap the two channels' slots, then refresh the service's slot→identity cache. + writes = await _apply_order(device, slots, [1, 0]) + assert writes == 2 + await chat.refresh_channels() + + after = {s.name: s for s in await _read_slots(device)} + assert after["Alpha"].idx == 1 and after["Beta"].idx == 0 # slots actually swapped + assert after["Alpha"].identity == alpha_id # identity is unchanged by the move + + # History still resolves per channel by identity — no transcript was inherited. + alpha_msgs = ctx.repo.recent_chat_messages(is_channel=True, channel_id=alpha_id) + beta_msgs = ctx.repo.recent_chat_messages(is_channel=True, channel_id=beta_id) + assert [m.text for m in alpha_msgs] == ["hello from alpha"] + assert [m.text for m in beta_msgs] == ["hello from beta"] + finally: + await chat.stop() + + # -- the tool against the simulator ------------------------------------------- diff --git a/tests/test_chat.py b/tests/test_chat.py index d990c75..dc9e34d 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -20,6 +20,7 @@ def _strip_ansi(text: str) -> str: """Remove ANSI color escapes from rendered lines for plain-text assertions.""" return _ANSI.sub("", text) +from meshtools.core.channels import DEFAULT_PUBLIC_SECRET, derive_secret from meshtools.core.connection import MockDevice from meshtools.core.events import MeshEvent from meshtools.core.models import ( @@ -32,6 +33,7 @@ from meshtools.core.models import ( from meshtools.persistence.repository import Repository from meshtools.services.chat_service import ChatService from meshtools.services.event_hub import EventHub +from meshtools.tools.chat import _LiveLasts, _preview, _title from meshtools.ui.chat import ChatScreen from meshtools.ui.tui.screen import CANCEL @@ -72,8 +74,8 @@ def repo(tmp_path: Path) -> Repository: def test_conversation_key_distinguishes_channels_and_directs() -> None: - """Channel and direct keys are stable and case-insensitive on the peer.""" - assert conversation_key(True, 0, None) == "chan:0" + """Channel keys use the channel's identity; direct keys are case-insensitive on the peer.""" + assert conversation_key(True, "deadbeef", None) == "chan:deadbeef" assert conversation_key(False, None, "AABBCC") == "dm:aabbcc" contact = Contact(name="Alice", public_key="d4e5" + "0" * 60, key_prefix="d4e5f6a7") @@ -81,8 +83,8 @@ def test_conversation_key_distinguishes_channels_and_directs() -> None: assert conv.key == "dm:d4e5f6a7" assert conv.peer == "d4e5f6a7" - chan = Conversation(label="#public", is_channel=True, channel_idx=2) - assert chan.key == "chan:2" + chan = Conversation(label="#public", is_channel=True, channel_idx=2, channel_id="deadbeef") + assert chan.key == "chan:deadbeef" # keyed by identity, not the slot index assert chan.peer is None @@ -174,23 +176,152 @@ def test_record_and_load_direct_conversation(repo: Repository) -> None: def test_record_and_load_channel_conversation(repo: Repository) -> None: - """Channel messages are keyed by slot, isolated from direct messages.""" - repo.record_chat_message(ChatMessage(text="c0", is_channel=True, channel_idx=0)) - repo.record_chat_message(ChatMessage(text="c1", is_channel=True, channel_idx=1)) + """Channel messages are keyed by channel identity, isolated from direct messages.""" + repo.record_chat_message(ChatMessage(text="c0", is_channel=True, channel_id="aa00")) + repo.record_chat_message(ChatMessage(text="c1", is_channel=True, channel_id="bb11")) - assert [m.text for m in repo.recent_chat_messages(is_channel=True, channel_idx=0)] == ["c0"] - assert [m.text for m in repo.recent_chat_messages(is_channel=True, channel_idx=1)] == ["c1"] + assert [m.text for m in repo.recent_chat_messages(is_channel=True, channel_id="aa00")] == ["c0"] + assert [m.text for m in repo.recent_chat_messages(is_channel=True, channel_id="bb11")] == ["c1"] def test_last_chat_messages_returns_latest_per_conversation(repo: Repository) -> None: """The picker preview shows the newest message in each conversation.""" repo.record_chat_message(ChatMessage(text="old", peer="aa")) repo.record_chat_message(ChatMessage(text="new", peer="aa")) - repo.record_chat_message(ChatMessage(text="chan", is_channel=True, channel_idx=0)) + repo.record_chat_message(ChatMessage(text="chan", is_channel=True, channel_id="aa00")) lasts = repo.last_chat_messages() assert lasts["dm:aa"].text == "new" - assert lasts["chan:0"].text == "chan" + assert lasts["chan:aa00"].text == "chan" + + +# -- picker row rendering ----------------------------------------------------- + + +class _FakeChat: + """Stand-in for the chat service exposing just the unread lookup a row title reads.""" + + def __init__(self, unread: dict[str, int]) -> None: + self._unread = unread + + def unread(self, key: str) -> int: + return self._unread.get(key, 0) + + +class _RowCtx: + """Minimal ctx exposing only what the picker-row helpers touch (repo + chat).""" + + def __init__(self, repo: Repository, unread: dict[str, int] | None = None) -> None: + self.repo = repo + self.chat = _FakeChat(unread or {}) + + +def test_live_lasts_refreshes_preview_after_ttl(repo: Repository) -> None: + """A new message becomes visible through _LiveLasts once the cache TTL lapses.""" + repo.record_chat_message(ChatMessage(text="first", is_channel=True, channel_id="c0")) + live = _LiveLasts(_RowCtx(repo), seed=repo.last_chat_messages(), ttl=0) # 0 => always fresh + assert live.get("chan:c0").text == "first" + repo.record_chat_message(ChatMessage(text="second", is_channel=True, channel_id="c0")) + assert live.get("chan:c0").text == "second" # picked up live, not stuck on the seed + + +def test_live_lasts_serves_seed_within_ttl(repo: Repository) -> None: + """Within the TTL the seeded snapshot is served without re-querying the repository.""" + live = _LiveLasts(_RowCtx(repo), seed={"chan:c0": ChatMessage(text="seed")}, ttl=999) + repo.record_chat_message(ChatMessage(text="later", is_channel=True, channel_id="c0")) + assert live.get("chan:c0").text == "seed" + + +def test_preview_prefixes_own_messages_only() -> None: + """Only outbound messages get a ``you:`` prefix; inbound text is shown verbatim. + + Channel senders are embedded inline in the message text by the firmware, so no author is + synthesized (that would double it), and a direct message's author is the row label. + """ + chan_in = ChatMessage(text="Bob: hi", is_channel=True, channel_idx=0) # sender inline + assert _preview(chan_in) == "Bob: hi" + dm_in = ChatMessage(text="hey", peer="aa", peer_name="Bob") + assert _preview(dm_in) == "hey" + mine = ChatMessage(text="yo", outbound=True, is_channel=True, channel_idx=0) + assert _preview(mine) == "you: yo" + + +def test_preview_ellipsizes_long_text() -> None: + """An over-long preview is clipped to the width budget with a trailing ellipsis.""" + long = ChatMessage(text="x" * 100, is_channel=True, channel_idx=0) + out = _preview(long, width=10) + assert len(out) == 10 and out.endswith("…") + + +def test_title_shows_badge_and_author_preview(repo: Repository) -> None: + """A channel row renders its live unread badge and its author-prefixed preview.""" + conv = Conversation(label="General", is_channel=True, channel_idx=0, channel_id="c0") + ctx = _RowCtx(repo, unread={"chan:c0": 3}) + last = ChatMessage(text="Bob: hi there", is_channel=True, channel_id="c0") # sender inline + title = _title(ctx, conv, {"chan:c0": last}) + line = title.plain # a Text, since there is unread + assert line.startswith("🔒 General") # a private channel leads with its openness glyph + assert "● 3" in line + assert "Bob: hi there" in line + + +def test_title_reddens_only_the_unread_dot(repo: Repository) -> None: + """With unread the row is a Text whose ``●`` glyph (only) is styled red; else a plain str.""" + from rich.text import Text + + conv = Conversation(label="General", is_channel=True, channel_idx=0, channel_id="c0") + unread = _title(_RowCtx(repo, unread={"chan:c0": 2}), conv, {}) + assert isinstance(unread, Text) + dot = unread.plain.index("●") + reddened = [ + span for span in unread.spans if span.style == "err" and span.start <= dot < span.end + ] + assert reddened and all(span.end - span.start == 1 for span in reddened) # just the glyph + + read = _title(_RowCtx(repo, unread={}), conv, {}) + assert isinstance(read, str) # no badge -> plain string, no styling + + +def test_title_preview_column_aligns_regardless_of_label_length(repo: Repository) -> None: + """The preview starts at the same column whether the label is short or (clipped) long.""" + ctx = _RowCtx(repo) + short = Conversation(label="A", is_channel=True, channel_idx=0, channel_id="c0") + long = Conversation(label="A much longer channel name here", is_channel=True, channel_idx=1, channel_id="c1") + m0 = ChatMessage(text="X: hello", is_channel=True, channel_id="c0") + m1 = ChatMessage(text="Y: hello", is_channel=True, channel_id="c1") + l0 = _title(ctx, short, {"chan:c0": m0}) + l1 = _title(ctx, long, {"chan:c1": m1}) + assert l0.index("X: hello") == l1.index("Y: hello") + + +def test_title_leads_with_openness_glyph(repo: Repository) -> None: + """Channel rows lead with an openness glyph: # name-derived, 🌐 fixed-key public, 🔒 private.""" + ctx = _RowCtx(repo) + head = lambda conv: _title(ctx, conv, {}).split(" ", 1)[0] + named = Conversation( + label="#general", is_channel=True, channel_id="c0", secret=derive_secret("#general") + ) + public = Conversation( + label="Public", is_channel=True, channel_id="c1", secret=DEFAULT_PUBLIC_SECRET + ) + private = Conversation( + label="Ops", is_channel=True, channel_id="c2", secret=bytes(range(16)) + ) + assert head(named) == "#" + assert head(public) == "🌐" + assert head(private) == "🔒" + + +def test_title_contact_glyph_reflects_conversation_history(repo: Repository) -> None: + """A contact shows 💬 once we've exchanged messages, 👤 before any conversation exists.""" + ctx = _RowCtx(repo) + contact = Conversation( + label="Alice", is_channel=False, contact=Contact(name="Alice", public_key="d4" + "0" * 62) + ) + head = lambda lasts: _title(ctx, contact, lasts).split(" ", 1)[0] + assert head({}) == "👤" # no history yet — a contact we haven't talked to + last = ChatMessage(text="hi", peer=contact.peer) + assert head({contact.key: last}) == "💬" # we've exchanged messages # -- chat service ------------------------------------------------------------- @@ -430,7 +561,8 @@ async def test_service_send_records_outbound(repo: Repository) -> None: assert sent.outbound is True and sent.acked is True await chat.send_channel(0, "hello all", label="#public") - stored = repo.recent_chat_messages(is_channel=True, channel_idx=0) + channel_id = await chat.channel_id_for(0) + stored = repo.recent_chat_messages(is_channel=True, channel_id=channel_id) assert [m.text for m in stored] == ["hello all"] assert stored[0].outbound is True finally: diff --git a/tests/test_tui.py b/tests/test_tui.py index 28b6825..8190bcc 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -116,6 +116,23 @@ def test_select_cursor_line_tracks_selection() -> None: assert screen.cursor_line() == 2 +def test_select_callable_title_re_renders_live() -> None: + """A callable title is resolved on every repaint, so a live badge tracks state.""" + unread = {"n": 0} + screen = SelectScreen("pick", [Choice(lambda: f"chan ● {unread['n']}", 1)]) + assert "chan ● 0" in "\n".join(screen.render_body(40)) + unread["n"] = 3 # a message arrived while the list is open + assert "chan ● 3" in "\n".join(screen.render_body(40)) + + +def test_select_filter_matches_callable_title() -> None: + """Type-to-filter matches against a callable title's current text.""" + screen = SelectScreen("pick", [Choice(lambda: "alpha", 1), Choice("beta", 2)]) + screen.handle("text", "alp") + assert [r.label for r in screen._rows()] == ["alpha"] + assert _run(screen, "enter") == 1 + + # --- scroll ------------------------------------------------------------------