Add message delivery retry, channel capacity discovery, and a compose byte budget

Three interrelated improvements to the chat/channel experience:

Outgoing byte budget & unified transcript (ui/chat.py, ui/tui/prompt.py):
- Cap outgoing messages at MeshCore's UTF-8 limits (150B direct, 130B
  channel) and block over-limit sends, keeping the buffer intact to trim.
- Show a live `used/limit` gauge in the compose bar whose used count steps
  green→yellow→orange→red by bytes remaining, with truer yellow/orange hues.
- Color the overflow characters red in the compose line (multibyte-safe).
- Render direct chats with the same grouped, per-sender layout as channels
  (headers, day dividers) instead of one line per message.

Direct-message delivery retry (services/chat_service.py, persistence,
core/models.py, tools/chat.py, ui/tui/session.py):
- Track outbound acked state (None awaiting / True / False) with a persisted
  row_id, add resend_direct + update_chat_ack, and wire Ctrl-R to retry the
  latest unacknowledged message in place with ⏳/✅/❌ delivery glyphs.

Channel slot capacity discovery (core/channels.py, core/connection.py,
ui/channels.py, tools):
- Discover the firmware's real channel-slot count via a bounded read-only
  probe (Device.channel_capacity) instead of assuming 8; show used/total in
  the manager and refresh the chat slot→identity cache after any mutation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jean-Pierre MartineauandClaude Opus 4.8 committed 2026-07-06 23:12:25 -04:00
1 parent 61101cf778
commit a03dedb09c
14 files changed
+723 -130

No files matched your search

+10
View File
@@ -18,6 +18,16 @@ from urllib.parse import parse_qs, quote, urlsplit
#: A channel shared secret is exactly 16 bytes (128-bit), per the MeshCore protocol.
CHANNEL_SECRET_BYTES = 16
#: The channel-slot capacity assumed when a device can't be probed. Stock MeshCore companion
#: firmware is built with 8 slots; :meth:`meshtools.core.connection.Device.channel_capacity`
#: discovers the real number from the hardware at runtime, so this is only the fallback.
MAX_CHANNELS = 8
#: Ceiling for a slot probe. A channel slot is addressed on the wire by a single byte, so this
#: is generous headroom over any real firmware while still bounding a scan against a device that
#: never rejects an out-of-range index (so the probe can't loop forever).
CHANNEL_SLOT_PROBE_CAP = 64
#: The default public channel every MeshCore device ships with on slot 0.
DEFAULT_PUBLIC_NAME = "public"
+30
View File
@@ -20,6 +20,7 @@ from abc import ABC, abstractmethod
from datetime import datetime, timezone
from typing import Callable, Optional
from .channels import CHANNEL_SLOT_PROBE_CAP
from .events import MeshEvent
from .models import Ack, Contact, Hop, Message, Observation, TraceResult
@@ -256,6 +257,30 @@ class Device(ABC):
(16 raw bytes), or ``None`` when the slot is empty.
"""
async def channel_capacity(self) -> int:
"""Discover how many channel slots this device exposes (read-only, non-destructive).
Reads slots from 0 upward until the firmware rejects an index. An *empty* slot is a
valid index and returns ``None`` without stopping the scan; only an out-of-range index
makes the firmware answer with an error, which surfaces here as an exception. The scan
is bounded by :data:`CHANNEL_SLOT_PROBE_CAP` so a device that never rejects an index
can't loop forever — in that case the cap itself is reported.
This only ever *reads* channel configuration, so it is safe to call against a live
device without disturbing its state.
Returns:
The number of addressable channel slots the firmware was built with.
"""
count = 0
for idx in range(CHANNEL_SLOT_PROBE_CAP):
try:
await self.get_channel(idx)
except Exception: # noqa: BLE001 - a rejected index is how firmware signals its ceiling
break
count = idx + 1
return count
# -- configuration: settable values -----------------------------------------
@abstractmethod
@@ -1012,6 +1037,9 @@ class MockDevice(Device):
self._path_hash_mode = 0
self._custom_vars: dict[str, str] = {}
self._channels: dict[int, dict] = {}
# Model the firmware's fixed slot count: reads past it are rejected, exactly as a
# real device signals its ceiling (so ``channel_capacity`` discovers 8 on the mock).
self._max_channels = 8
self._device_pin = 0
self._private_key = "11" * 32
# Background emitter tasks spawned by ``subscribe_events``; tracked so they
@@ -1108,6 +1136,8 @@ class MockDevice(Device):
return dict(self._custom_vars)
async def get_channel(self, index: int) -> Optional[dict]: # noqa: D102
if index >= self._max_channels:
raise DeviceCommandError(f"channel index {index} out of range")
return self._channels.get(index)
async def set_name(self, name: str) -> None: # noqa: D102 - inherited docstring
+6 -2
View File
@@ -166,9 +166,12 @@ class ChatMessage:
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.
acked: For an outbound direct message, whether delivery was acknowledged; ``None``
when not applicable (a channel broadcast or an inbound message).
acked: Delivery state of an outbound direct message: ``True`` acknowledged, ``False``
sent but not acknowledged (retryable), ``None`` either still awaiting the ack
or not applicable (a channel broadcast or an inbound message).
created_at: When the message was sent or received.
row_id: The ``messages`` table primary key once persisted, used to update an
outbound message's delivery state in place on retry; ``None`` until stored.
"""
text: str
@@ -181,6 +184,7 @@ class ChatMessage:
snr: Optional[float] = None
acked: Optional[bool] = None
created_at: datetime = field(default_factory=utcnow)
row_id: Optional[int] = None
@property
def key(self) -> str:
+13
View File
@@ -422,6 +422,18 @@ class Repository:
self._conn.commit()
return int(cur.lastrowid)
def update_chat_ack(self, message_id: int, acked: bool) -> None:
"""Update one outbound message's delivery acknowledgement (used on retry).
Args:
message_id: The ``messages`` row to update.
acked: The new delivery state — ``True`` acknowledged, ``False`` not.
"""
self._conn.execute(
"UPDATE messages SET acked = ? WHERE id = ?", (int(acked), message_id)
)
self._conn.commit()
def recent_chat_messages(
self,
*,
@@ -510,4 +522,5 @@ class Repository:
snr=row["snr"],
acked=None if row["acked"] is None else bool(row["acked"]),
created_at=datetime.fromisoformat(row["created_at"]),
row_id=row["id"],
)
+26 -6
View File
@@ -19,7 +19,7 @@ from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING, Optional
from ..core.channels import channel_identity
from ..core.channels import CHANNEL_SLOT_PROBE_CAP, channel_identity
from ..core.connection import Unsubscribe
from ..core.events import EventKind, MeshEvent
from ..core.models import ChatMessage, Contact, Message, utcnow
@@ -27,9 +27,6 @@ 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).
@@ -108,7 +105,7 @@ class ChatService:
"""
device = await self._ctx.device()
ids: dict[int, str] = {}
for idx in range(_MAX_CHANNELS):
for idx in range(CHANNEL_SLOT_PROBE_CAP):
try:
payload = await device.get_channel(idx)
except Exception: # noqa: BLE001 - firmware may not support channel reads
@@ -276,9 +273,32 @@ class ChatService:
acked=ack is not None,
created_at=utcnow(),
)
self._ctx.repo.record_chat_message(chat, run_id=self._run_id)
chat.row_id = self._ctx.repo.record_chat_message(chat, run_id=self._run_id)
return chat
async def resend_direct(self, contact: Contact, message: ChatMessage) -> ChatMessage:
"""Re-attempt delivery of an unacknowledged direct message, updating it in place.
Used to retry a message that was transmitted but never acknowledged (its ``acked``
is ``False``). The same stored row is reused — its delivery state is updated rather
than a duplicate transcript entry created — so the message simply flips to delivered
(or stays unacknowledged for another retry).
Args:
contact: The recipient.
message: The previously-sent :class:`ChatMessage` to re-transmit; mutated in
place with the new delivery state.
Returns:
The same ``message``, with :attr:`~ChatMessage.acked` refreshed.
"""
device = await self._ctx.device()
ack = await device.send_direct_message(contact, message.text)
message.acked = ack is not None
if message.row_id is not None:
self._ctx.repo.update_chat_ack(message.row_id, message.acked)
return message
async def send_channel(
self, index: int, text: str, *, label: Optional[str] = None
) -> ChatMessage:
+7 -6
View File
@@ -21,7 +21,11 @@ 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.channels import (
CHANNEL_SLOT_PROBE_CAP,
DEFAULT_PUBLIC_SECRET,
channel_identity,
)
from ..core.connection import Device
from ..core.events import EventKind, MeshEvent
from ..core.models import ChatMessage, Contact, Conversation
@@ -29,9 +33,6 @@ 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.
_MAX_CHANNELS = 8
#: How many recent messages ``chat history`` prints by default.
_HISTORY_LIMIT = 50
@@ -188,7 +189,7 @@ class ChatTool(Tool):
contact = _resolve_contact(await device.get_contacts(), str(params["to"]))
message = await ctx.chat.send_direct(contact, text)
state = "[ok]delivered[/ok]" if message.acked else "[warn]no ack[/warn]"
state = "[ok]✅ delivered[/ok]" if message.acked else "[warn]❌ no ack[/warn]"
ctx.ui.note(f"[ok]✓[/ok] sent to [brand]{contact.name}[/brand] — {state}")
return ToolResult(summary={"to": contact.name, "acked": bool(message.acked)})
@@ -387,7 +388,7 @@ async def _read_channels(device: Device) -> list[Conversation]:
channel 0).
"""
conversations: list[Conversation] = []
for idx in range(_MAX_CHANNELS):
for idx in range(CHANNEL_SLOT_PROBE_CAP):
try:
channel = await device.get_channel(idx)
except Exception: # noqa: BLE001 - firmware may not support channel reads
+2 -4
View File
@@ -15,6 +15,7 @@ import typer
from rich.table import Table
from ..context import AppContext
from ..core.channels import CHANNEL_SLOT_PROBE_CAP
from ..core.config_io import backup_config, plan_restore, read_backup
from ..core.connection import Device
from ..core.device_config import (
@@ -25,9 +26,6 @@ from ..core.device_config import (
)
from .base import Tool, ToolResult, register
# How many channel slots to probe when backing up.
_MAX_CHANNELS = 8
@register
class ConfigTool(Tool):
@@ -331,7 +329,7 @@ async def _export_key(
async def _read_channels(device: Device) -> list[dict]:
"""Probe channel slots and return the configured ones."""
channels: list[dict] = []
for idx in range(_MAX_CHANNELS):
for idx in range(CHANNEL_SLOT_PROBE_CAP):
try:
ch = await device.get_channel(idx)
except Exception: # noqa: BLE001 - firmware may not support channel reads
+60 -30
View File
@@ -22,6 +22,8 @@ from rich.table import Table
from rich.text import Text
from ..core.channels import (
CHANNEL_SLOT_PROBE_CAP,
MAX_CHANNELS,
channel_hash,
channel_identity,
derive_secret,
@@ -42,9 +44,6 @@ from .widgets import channel_glyph
if TYPE_CHECKING:
from ..context import AppContext
#: How many channel slots the firmware exposes (0-based); matches the chat/backup probes.
_MAX_CHANNELS = 8
# Top-menu action sentinels (distinct from a plain slot index, which selects that channel).
_CREATE = "__create__"
_PUBLIC = "__public__"
@@ -125,38 +124,67 @@ async def manage_channels(ctx: "AppContext") -> int:
The number of channels created, changed, or cleared during the session.
"""
device = await ctx.device()
# The firmware's slot count is fixed for the session, so discover it once (a read-only
# probe) rather than assuming a hard-coded 8; it drives the free-slot check and the
# used/total display below. A device that can't report any slots falls back to the
# standard count so the manager stays usable instead of showing zero capacity.
capacity = await device.channel_capacity() or MAX_CHANNELS
changes = 0
highlight: Optional[object] = None
while True:
slots = await _read_slots(device)
choice = await _main_menu(ctx, slots, default=highlight)
choice = await _main_menu(ctx, slots, capacity, default=highlight)
if choice in (None, _BACK):
return changes
highlight = choice
before = changes
if choice == _CREATE:
changes += await _create_private(ctx, device, slots)
changes += await _create_private(ctx, device, slots, capacity)
elif choice == _PUBLIC:
changes += await _add_public(ctx, device, slots)
changes += await _add_public(ctx, device, slots, capacity)
elif choice == _JOIN:
changes += await _join_with_key(ctx, device, slots)
changes += await _join_with_key(ctx, device, slots, capacity)
elif choice == _IMPORT:
changes += await _import_link(ctx, device, slots)
changes += await _import_link(ctx, device, slots, capacity)
elif choice == _REORDER:
changes += await _reorder_channels(ctx, device, slots)
else: # an existing slot index
slot = next((s for s in slots if s.idx == choice), None)
if slot is not None:
changes += await _channel_detail(ctx, device, slot)
if changes > before:
# A slot's occupant changed (created, re-keyed, cleared, or moved). Inbound
# messages carry only a slot index, which the chat service maps to a channel
# identity through a cache keyed by slot; refresh it now so a message on a
# reused/re-keyed slot is filed under the channel that's actually there and not
# the one that used to be — otherwise its transcript surfaces in the wrong chat.
await _refresh_chat_channels(ctx)
# unreachable
async def _refresh_chat_channels(ctx: "AppContext") -> None:
"""Rebuild the chat service's slot→identity cache after a channel mutation.
Best-effort: a device read hiccup here must never break the channel manager, and the
cache also self-heals on the next miss, so a failure is only logged.
"""
try:
await ctx.chat.refresh_channels()
except Exception as exc: # noqa: BLE001 - refresh is best-effort; never fatal here
ctx.log.debug("channels: chat cache refresh failed: %s", exc)
# --- reading -----------------------------------------------------------------
async def _read_slots(device: Device) -> list[ChannelSlot]:
"""Probe the channel slots and return the configured ones, in index order.
The scan runs up to :data:`CHANNEL_SLOT_PROBE_CAP` and stops as soon as the firmware
rejects a slot index, so it reads exactly the slots the device actually has regardless
of its capacity.
Args:
device: The connected device to query.
@@ -164,7 +192,7 @@ async def _read_slots(device: Device) -> list[ChannelSlot]:
One :class:`ChannelSlot` per configured slot (an empty slot is skipped).
"""
slots: list[ChannelSlot] = []
for idx in range(_MAX_CHANNELS):
for idx in range(CHANNEL_SLOT_PROBE_CAP):
try:
payload = await device.get_channel(idx)
except Exception: # noqa: BLE001 - firmware may not support channel reads
@@ -180,10 +208,10 @@ async def _read_slots(device: Device) -> list[ChannelSlot]:
return slots
def _next_free_slot(slots: list[ChannelSlot]) -> Optional[int]:
def _next_free_slot(slots: list[ChannelSlot], capacity: int) -> Optional[int]:
"""Return the lowest unused slot index, or ``None`` when every slot is full."""
used = {s.idx for s in slots}
return next((i for i in range(_MAX_CHANNELS) if i not in used), None)
return next((i for i in range(capacity) if i not in used), None)
def _slot_label(slot: ChannelSlot) -> str:
@@ -197,10 +225,10 @@ def _slot_label(slot: ChannelSlot) -> str:
async def _main_menu(
ctx: "AppContext", slots: list[ChannelSlot], *, default: object = None
ctx: "AppContext", slots: list[ChannelSlot], capacity: int, *, default: object = None
) -> object:
"""Show the channel list and the add-a-channel actions; return the chosen value."""
items: list = [Separator("── Channels ──")]
items: list = [Separator(f"── Channels ({len(slots)}/{capacity}) ──")]
if slots:
for slot in slots:
items.append(Choice(title=_slot_label(slot), value=slot.idx))
@@ -259,10 +287,10 @@ async def _channel_detail(ctx: "AppContext", device: Device, slot: ChannelSlot)
async def _create_private(
ctx: "AppContext", device: Device, slots: list[ChannelSlot]
ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int
) -> int:
"""Create a private channel with a fresh random key on the next free slot."""
idx = await _pick_free_slot(ctx, slots)
idx = await _pick_free_slot(ctx, slots, capacity)
if idx is None:
return 0
name = await ctx.ui.text("Channel name:", validate=_nonblank)
@@ -275,9 +303,11 @@ async def _create_private(
return 1
async def _add_public(ctx: "AppContext", device: Device, slots: list[ChannelSlot]) -> int:
async def _add_public(
ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int
) -> int:
"""Create a public channel whose key is derived from its (``#``-prefixed) name."""
idx = await _pick_free_slot(ctx, slots)
idx = await _pick_free_slot(ctx, slots, capacity)
if idx is None:
return 0
raw = await ctx.ui.text(
@@ -298,10 +328,10 @@ async def _add_public(ctx: "AppContext", device: Device, slots: list[ChannelSlot
async def _join_with_key(
ctx: "AppContext", device: Device, slots: list[ChannelSlot]
ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int
) -> int:
"""Join an existing private channel by entering its name and 16-byte key."""
idx = await _pick_free_slot(ctx, slots)
idx = await _pick_free_slot(ctx, slots, capacity)
if idx is None:
return 0
name = await ctx.ui.text("Channel name:", validate=_nonblank)
@@ -319,10 +349,10 @@ async def _join_with_key(
async def _import_link(
ctx: "AppContext", device: Device, slots: list[ChannelSlot]
ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int
) -> int:
"""Import a channel from a pasted ``meshcore://channel/add`` link."""
idx = await _pick_free_slot(ctx, slots)
idx = await _pick_free_slot(ctx, slots, capacity)
if idx is None:
return 0
url = await ctx.ui.text(
@@ -412,13 +442,11 @@ 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
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
# Chat history is keyed by each channel's intrinsic identity, not its slot, so it follows
# the channels automatically — reordering needs no history migration. The slot→identity
# cache the chat service resolves inbound messages through is refreshed centrally by
# manage_channels once any change lands (see :func:`_refresh_chat_channels`).
return await _apply_order(device, slots, order)
# --- shared views ------------------------------------------------------------
@@ -473,9 +501,11 @@ async def _open_chat(ctx: "AppContext", slot: ChannelSlot) -> None:
await open_chat(ctx, slot.conversation)
async def _pick_free_slot(ctx: "AppContext", slots: list[ChannelSlot]) -> Optional[int]:
async def _pick_free_slot(
ctx: "AppContext", slots: list[ChannelSlot], capacity: int
) -> Optional[int]:
"""Return the next free slot, warning (and returning ``None``) if all are full."""
idx = _next_free_slot(slots)
idx = _next_free_slot(slots, capacity)
if idx is None:
await ctx.ui.view(
Text.from_markup(
+261 -70
View File
@@ -23,7 +23,7 @@ from rich.console import Group, RenderableType
from rich.text import Text
from ..core.events import EventKind, MeshEvent
from ..core.models import ChatMessage, Contact, Conversation, Message
from ..core.models import ChatMessage, Contact, Conversation, Message, utcnow
from .theme import snr_style
from .tui.prompt import _LineEditor
from .tui.render import render_lines
@@ -59,6 +59,33 @@ _SENDER_COLORS = (
_SENDER_PREFIX = re.compile(r"^([^\s:][^:]{0,19}):[ \t]+(.*)$", re.DOTALL)
#: Delivery-state indicators for an outbound direct message, shown at the end of its line:
#: awaiting the ack, acknowledged, or transmitted-but-unacknowledged (retryable via Ctrl-R).
_SENDING, _DELIVERED, _FAILED = "⏳", "✅", "❌"
#: How many UTF-8 bytes a single outgoing message may carry, by conversation kind. MeshCore's
#: LoRa payload caps a direct message at 150 bytes and an (unscoped) channel broadcast at 130;
#: over the limit the companion would silently drop the packet, so we block the send instead
#: and show the running byte budget in the compose bar.
_DM_BYTE_LIMIT = 150
_CHANNEL_BYTE_LIMIT = 130
#: Byte-counter thresholds (bytes *remaining*) at which its color escalates, plus the two
#: mid-band hues. The theme's ``warn``/``err`` sit too close together (an amber that reads
#: orange, then red), so the counter names a truer yellow and orange directly to keep the
#: green→yellow→orange→red fuel gauge visibly stepped.
_BYTES_TIGHT, _BYTES_LOW = 20, 10
_BYTES_YELLOW = "bold #fde047"
_BYTES_ORANGE = "bold #ff9500"
def _delivery_glyph(acked: Optional[bool]) -> str:
"""Map an outbound direct message's ``acked`` state to its delivery emoji."""
if acked is None:
return _SENDING
return _DELIVERED if acked else _FAILED
def _split_channel_sender(text: str) -> tuple[Optional[str], str]:
"""Split a channel message into ``(sender_name, body)`` when it carries a name prefix.
@@ -99,6 +126,7 @@ class ChatScreen(Screen):
send: Callable[[str], Awaitable[Optional[ChatMessage]]],
names: dict[str, str],
session, # noqa: ANN001 - TuiSession, imported lazily to avoid a cycle
resend: Optional[Callable[[ChatMessage], Awaitable[ChatMessage]]] = None,
) -> None:
"""Build the chat screen.
@@ -111,12 +139,15 @@ class ChatScreen(Screen):
direct messages.
session: The running :class:`~meshtools.ui.tui.session.TuiSession`, used to
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).
"""
super().__init__()
self.title = conversation.label
self._is_channel = conversation.is_channel
self._messages = list(messages)
self._send = send
self._resend = resend
self._names = names
self._session = session
self._editor = _LineEditor()
@@ -133,6 +164,8 @@ class ChatScreen(Screen):
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 · Ctrl-R retry failed · ↑↓ scroll · Esc back"
return "Enter send · ↑↓/PgUp scroll · End latest · Esc back"
if self._selected is not None:
return "Enter reply (@mention) · ↑↓ pick · End/Esc cancel"
@@ -155,20 +188,21 @@ class ChatScreen(Screen):
self._selected_line = None
if not self._messages:
lines = render_lines(Text("No messages yet — say hello!", style="muted"), width)
elif self._is_channel:
# Channels carry many senders, so group consecutive messages from one sender
# under a colored header (Discord/Slack style) rather than the flat one-line
# format used for the two-party direct view. Rendering per-message also lets us
# record where the picked reply target lands (see _selected_line / cursor_line).
else:
# Both direct and channel threads use the same grouped, Discord/Slack-style
# transcript: consecutive messages from one sender share a colored header, with
# day dividers between them. Rendering per-message also lets us record where the
# picked reply target lands (channels only; see _selected_line / cursor_line).
if self._selected is not None:
self._selected = max(0, min(self._selected, len(self._messages) - 1))
lines = self._render_channel(width)
else:
lines = render_lines(Group(*(self._format(m) for m in self._messages)), width)
lines = self._render_grouped(width)
limit = self._byte_limit()
used = self._used_bytes()
footer_parts: list[RenderableType] = [
Text("─" * width, style="muted"),
self._editor.render(),
self._editor.render(overflow_at=self._overflow_at(limit)),
self._byte_counter(width, used, limit),
]
if self._is_channel and self._selected is not None:
footer_parts.append(self._reply_banner())
@@ -186,25 +220,53 @@ class ChatScreen(Screen):
"""Keep the picked reply target in view; otherwise free scroll (managed by stick)."""
return self._selected_line
def _format(self, message: ChatMessage) -> Text:
"""Render one transcript message as ``HH:MM who text`` with quality cues."""
stamp = message.created_at.astimezone().strftime("%H:%M")
if message.outbound:
who, style = "you", "accent"
elif message.is_channel:
who, style = (message.peer_name or "·"), "brand"
else:
who, style = (self._name(message.peer) or message.peer or "?"), "brand"
# --- outgoing byte budget ------------------------------------------------
line = Text()
line.append(f"{stamp} ", style="muted")
line.append(f"{who:>10.10} ", style=style)
line.append(message.text)
if message.outbound and message.acked is False:
line.append(" ⚠ no ack", style="warn")
if message.snr is not None:
line.append(f" {message.snr:+.0f} dB", style=snr_style(message.snr))
return line
def _byte_limit(self) -> int:
"""The UTF-8 byte ceiling for a message in this conversation (channel vs direct)."""
return _CHANNEL_BYTE_LIMIT if self._is_channel else _DM_BYTE_LIMIT
def _used_bytes(self) -> int:
"""UTF-8 byte length of the current compose buffer — what counts against the limit."""
return len(self._editor.text.encode("utf-8"))
def _overflow_at(self, limit: int) -> Optional[int]:
"""Index of the first compose character whose bytes spill past ``limit``, else ``None``.
Walking by character (not byte) keeps multibyte input intact: an emoji or accented
letter is entirely under or entirely over the line, never split mid-sequence.
"""
total = 0
for i, ch in enumerate(self._editor.text):
total += len(ch.encode("utf-8"))
if total > limit:
return i
return None
def _byte_counter(self, width: int, used: int, limit: int) -> Text:
"""Right-aligned ``used/limit`` budget; only ``used`` is colored by how much is left.
Green with room to spare, yellow within :data:`_BYTES_TIGHT` bytes, orange within
:data:`_BYTES_LOW`, and red once the limit is met or exceeded — so the number reads as
a fuel gauge while the ``/limit`` suffix stays muted (it never changes).
"""
tail = f"/{limit}"
pad = max(0, width - len(str(used)) - len(tail))
counter = Text(" " * pad)
counter.append(str(used), style=self._byte_style(limit - used))
counter.append(tail, style="muted")
return counter
@staticmethod
def _byte_style(remaining: int) -> str:
"""Map bytes remaining to the counter's escalating color (green→yellow→orange→red)."""
if remaining <= 0:
return "err" # at or over the limit — the send is blocked
if remaining <= _BYTES_LOW:
return _BYTES_ORANGE
if remaining <= _BYTES_TIGHT:
return _BYTES_YELLOW
return "ok"
def _name(self, peer: Optional[str]) -> Optional[str]:
"""Resolve a sender key prefix to a contact name (exact, then prefix match)."""
@@ -218,24 +280,28 @@ class ChatScreen(Screen):
return name
return None
# --- channel (grouped) rendering -----------------------------------------
# --- grouped rendering ---------------------------------------------------
def _render_channel(self, width: int) -> list[str]:
"""Render the channel transcript to ANSI lines, tracking the picked message's row.
def _render_grouped(self, width: int) -> list[str]:
"""Render the transcript to ANSI lines, tracking the picked message's row.
Consecutive messages from the same sender on the same day share one colored header,
with each message body indented below. A muted divider marks each new day, and a
blank line separates distinct sender groups. Rendering message-by-message (rather
than as one Group) lets us record the body line of the selected reply target in
:attr:`_selected_line` so the frame can scroll it into view.
Serves both direct and channel threads. Consecutive messages from the same sender on
the same day share one colored header, with each message body indented below. A muted
divider marks each new day, and a blank line separates distinct sender groups.
Rendering message-by-message (rather than as one Group) lets us record the body line
of the selected reply target in :attr:`_selected_line` so the frame can scroll it into
view (channels only; direct threads never select).
"""
lines: list[str] = []
prev_sender: Optional[str] = None
prev_group: Optional[tuple[bool, str]] = None
prev_day = None
for idx, message in enumerate(self._messages):
stamp = message.created_at.astimezone()
day = stamp.date()
sender, body = self._sender_and_body(message)
# Group by (are-we-the-sender, display name), not the name alone, so our own
# messages never merge with a remote sender who happens to be named the same.
group = (message.outbound, sender)
new_day = day != prev_day
if new_day:
if lines:
@@ -243,27 +309,35 @@ class ChatScreen(Screen):
lines += render_lines(
Text(f"── {stamp:%a} {stamp:%b} {stamp.day} ──", style="muted"), width
)
if new_day or sender != prev_sender:
if new_day or group != prev_group:
if not new_day and lines:
lines += render_lines(Text(""), width) # gap between sender groups
lines += render_lines(self._group_header(sender), width)
header = self._group_header(sender, is_self=message.outbound)
lines += render_lines(header, width)
selected = idx == self._selected
if selected:
self._selected_line = len(lines)
lines += render_lines(self._body_line(body, message, selected=selected), width)
prev_sender, prev_day = sender, day
prev_group, prev_day = group, day
return lines
def _sender_and_body(self, message: ChatMessage) -> tuple[str, str]:
"""Return the display sender and cleaned body for a channel message."""
"""Return the display sender and cleaned body for a message.
Our own messages are ``you``. Inbound channel messages carry a ``Name: `` prefix we
lift into the sender (falling back to ``·`` when absent); inbound direct messages take
the sender from the resolved contact name (or the raw key, or ``?``).
"""
if message.outbound:
return "you", message.text
name, body = _split_channel_sender(message.text)
return (name or "·"), body
if message.is_channel:
name, body = _split_channel_sender(message.text)
return (name or "·"), body
return (self._name(message.peer) or message.peer or "?"), message.text
def _group_header(self, sender: str) -> Text:
def _group_header(self, sender: str, *, is_self: bool = False) -> Text:
"""Build the sender header that starts a group, colored per sender."""
return Text(sender, style=self._sender_style(sender))
return Text(sender, style=self._sender_style(sender, is_self=is_self))
def _body_line(self, body: str, message: ChatMessage, *, selected: bool = False) -> Text:
"""Build one indented message line, each stamped with its own time.
@@ -279,24 +353,37 @@ class ChatScreen(Screen):
line.append("❯ " if selected else " ", style="brand" if selected else None)
line.append(f"{stamp:%H:%M} ", style="brand" if selected else "muted")
line.append(body, style="brand" if selected else None)
if message.outbound and message.acked is False:
line.append(" ⚠ no ack", style="warn")
if message.outbound:
# Direct messages track per-message delivery (⏳ → ✅/❌, retryable); channel
# broadcasts have no ack, so only flag one that failed to leave the companion.
if message.is_channel:
if message.acked is False:
line.append(" ⚠ no ack", style="warn")
else:
line.append(f" {_delivery_glyph(message.acked)}")
if message.snr is not None:
line.append(f" {message.snr:+.0f} dB", style=snr_style(message.snr))
return line
def _reply_banner(self) -> Text:
"""One-line cue shown above the input when a message is picked to reply to."""
sender, _ = self._sender_and_body(self._messages[self._selected])
who = sender if sender not in ("·", "you") else "this message"
message = self._messages[self._selected]
sender, _ = self._sender_and_body(message)
who = "this message" if message.outbound or sender == "·" else sender
return Text(
f"↩ Enter to reply to {who} with an @mention · End to cancel", style="accent"
)
def _sender_style(self, sender: str) -> str:
"""Pick a stable color for a channel sender (accent for us, muted for unknown)."""
if sender == "you":
return "accent"
def _sender_style(self, sender: str, *, is_self: bool = False) -> str:
"""Pick a stable color for a channel sender.
Our own messages are white — keyed on ``is_self`` (the message being outbound), not
on the ``"you"`` label, so a remote sender who happens to be named ``you`` still gets
a hue from the palette rather than masquerading as us. ``·`` (unknown) is muted; every
other sender gets a stable hue derived from its name.
"""
if is_self:
return "you" # white, out of the per-sender hue range — always easy to spot
if sender == "·":
return "muted"
return _SENDER_COLORS[sum(map(ord, sender)) % len(_SENDER_COLORS)]
@@ -314,6 +401,8 @@ class ChatScreen(Screen):
"""Direct-chat input: send on Enter, scroll the transcript, edit, or leave on Esc."""
if action == "enter":
self._submit()
elif action == "retry":
self._retry()
elif action == "escape":
self.resolve(CANCEL)
elif action == "up":
@@ -333,6 +422,7 @@ class ChatScreen(Screen):
self._stick = True
else:
if self._editor.edit(action, data):
self._status = "" # trimming clears the "too long" notice
self._stick = True # typing snaps back to the live tail
def _handle_channel(self, action: str, data: str = "") -> None:
@@ -372,6 +462,7 @@ class ChatScreen(Screen):
else:
if self._editor.edit(action, data):
# Touching the compose line returns focus there — nothing stays selected.
self._status = "" # trimming clears the "too long" notice
self._clear_selection()
self._stick = True
@@ -406,36 +497,127 @@ class ChatScreen(Screen):
"""Prime the compose line with an ``@mention`` of the picked message's sender."""
message = self._messages[self._selected]
sender, _ = self._sender_and_body(message)
mention = f"@[{sender}] " if sender and sender not in ("you", "·") else "@"
named = not message.outbound and sender != "·"
mention = f"@[{sender}] " if named else "@"
self._editor = _LineEditor(mention + self._editor.text)
self._clear_selection()
self._stick = True
self._session.invalidate()
def _submit(self) -> None:
"""Send the current input line as a message (scheduled off the key handler)."""
"""Send the current input line as a message (scheduled off the key handler).
Refuses an over-budget line: the buffer is kept intact (so the user can trim it) and
the overage is reported, matching the red overflow the compose bar already shows.
"""
text = self._editor.text.strip()
if not text or self._sending:
return
limit = self._byte_limit()
over = self._used_bytes() - limit
if over > 0:
self._status = f"Too long by {over} byte{'s' if over != 1 else ''} — trim to send."
self._session.invalidate()
return
self._editor = _LineEditor()
self._sending = True
self._status = "sending…"
self._stick = True
if self._is_channel:
self._status = "sending…"
self._session.invalidate()
asyncio.ensure_future(self._send_channel(text))
else:
# Direct chats show an optimistic bubble whose trailing glyph tracks delivery:
# ⏳ now, then ✅/❌ once the ack resolves (or times out). acked=None ⇒ ⏳.
pending = ChatMessage(text=text, outbound=True, created_at=utcnow())
self._messages.append(pending)
self._status = ""
self._session.invalidate()
asyncio.ensure_future(self._send_direct(pending))
async def _run() -> None:
try:
message = await self._send(text)
if message is not None:
self._messages.append(message)
self._status = ""
except Exception as exc: # noqa: BLE001 - report inline, keep the chat alive
self._status = f"send failed: {exc}"
finally:
self._sending = False
self._stick = True
self._session.invalidate()
async def _send_channel(self, text: str) -> None:
"""Broadcast a channel message and append it once the companion accepts it."""
try:
message = await self._send(text)
if message is not None:
self._messages.append(message)
self._status = ""
except Exception as exc: # noqa: BLE001 - report inline, keep the chat alive
self._status = f"send failed: {exc}"
finally:
self._sending = False
self._stick = True
self._session.invalidate()
asyncio.ensure_future(_run())
async def _send_direct(self, pending: ChatMessage) -> None:
"""Await delivery of the optimistic ``pending`` bubble, swapping in the stored row.
On success the pending bubble is replaced by the recorded message (carrying its
resolved ``acked`` state and row id, so a ❌ can later be retried in place). A hard
failure — the companion rejecting the send outright — drops the bubble and reports
the error inline, matching how a failed send has always surfaced.
"""
try:
message = await self._send(pending.text)
except Exception as exc: # noqa: BLE001 - report inline, keep the chat alive
self._discard(pending)
self._status = f"send failed: {exc}"
else:
if message is not None:
self._swap(pending, message)
else:
self._discard(pending)
self._status = ""
finally:
self._sending = False
self._stick = True
self._session.invalidate()
def _retry(self) -> None:
"""Re-attempt delivery of the most recent unacknowledged direct message (Ctrl-R)."""
if self._sending or self._resend is None:
return
target = next(
(m for m in reversed(self._messages) if m.outbound and m.acked is False),
None,
)
if target is None:
return
self._sending = True
target.acked = None # back to ⏳ while the retry is in flight
self._status = "retrying…"
self._stick = True
self._session.invalidate()
asyncio.ensure_future(self._resend_message(target))
async def _resend_message(self, message: ChatMessage) -> None:
"""Drive a retry to completion, refreshing the message's delivery state in place."""
assert self._resend is not None
try:
await self._resend(message)
except Exception as exc: # noqa: BLE001 - report inline, keep the chat alive
message.acked = False
self._status = f"retry failed: {exc}"
else:
self._status = "" if message.acked else "still no ack — Ctrl-R to retry"
finally:
self._sending = False
self._stick = True
self._session.invalidate()
def _swap(self, old: ChatMessage, new: ChatMessage) -> None:
"""Replace an optimistic bubble with its recorded message, in place."""
try:
self._messages[self._messages.index(old)] = new
except ValueError: # pragma: no cover - the bubble is always still present
self._messages.append(new)
def _discard(self, message: ChatMessage) -> None:
"""Drop an optimistic bubble that never became a real message."""
try:
self._messages.remove(message)
except ValueError: # pragma: no cover - defensive
pass
async def open_chat(ctx: "AppContext", conversation: Conversation) -> int:
@@ -485,7 +667,16 @@ async def open_chat(ctx: "AppContext", conversation: Conversation) -> int:
assert conversation.contact is not None
return await ctx.chat.send_direct(conversation.contact, text)
screen = ChatScreen(conversation, history, send=send, names=names, session=session)
resend: Optional[Callable[[ChatMessage], Awaitable[ChatMessage]]] = None
if not conversation.is_channel:
async def resend(message: ChatMessage) -> ChatMessage:
assert conversation.contact is not None
return await ctx.chat.resend_direct(conversation.contact, message)
screen = ChatScreen(
conversation, history, send=send, names=names, session=session, resend=resend
)
ctx.chat.set_active(conversation.key)
def on_event(event: MeshEvent) -> None:
+3
View File
@@ -9,6 +9,9 @@ MESH_THEME = Theme(
{
"brand": "bold #5eead4",
"accent": "bold #818cf8",
# Our own messages in a chat: pure white, deliberately outside the per-sender hue
# palette (see meshtools.ui.chat._SENDER_COLORS) so "you" is always easy to spot.
"you": "bold #ffffff",
"ok": "bold #4ade80",
"warn": "bold #fbbf24",
"err": "bold #f87171",
+12 -9
View File
@@ -61,22 +61,25 @@ class _LineEditor:
return False
return True
def render(self, mask: bool = False) -> Text:
def render(self, mask: bool = False, *, overflow_at: Optional[int] = None) -> Text:
"""Render the current line with a reverse-video cursor cell.
Args:
mask: When ``True``, replace each character with a bullet (password entry).
overflow_at: Character index at which the text spills past a byte budget; that
character and everything after it are shown in the error style so the user
can see exactly what to trim. ``None`` (the default) styles the line plainly.
"""
shown = "•" * len(self.text) if mask else self.text
text = Text("› ", style="accent")
before, at, after = (
shown[: self.cursor],
shown[self.cursor : self.cursor + 1],
shown[self.cursor + 1 :],
)
text.append(before)
text.append(at or " ", style="reverse")
text.append(after)
for i, ch in enumerate(shown):
over = overflow_at is not None and i >= overflow_at
if i == self.cursor:
text.append(ch, style="reverse err" if over else "reverse")
else:
text.append(ch, style="err" if over else None)
if self.cursor >= len(shown): # cursor past the last character → trailing block
text.append(" ", style="reverse")
return text
+1
View File
@@ -45,6 +45,7 @@ _KEY_ACTIONS: dict[Any, str] = {
Keys.Backspace: "backspace",
Keys.Delete: "delete",
Keys.Tab: "tab",
Keys.ControlR: "retry",
}
+103 -2
View File
@@ -156,9 +156,28 @@ def test_next_free_slot_finds_gaps_and_full() -> None:
"""The next free slot skips used indices and is None when every slot is taken."""
slots = [ChannelSlot(idx=0, name="a", secret=b"\x00" * 16),
ChannelSlot(idx=2, name="c", secret=b"\x00" * 16)]
assert _next_free_slot(slots) == 1
assert _next_free_slot(slots, capacity=8) == 1
full = [ChannelSlot(idx=i, name=str(i), secret=b"\x00" * 16) for i in range(8)]
assert _next_free_slot(full) is None
assert _next_free_slot(full, capacity=8) is None
# With a larger discovered capacity the same "full-at-8" set still has room.
assert _next_free_slot(full, capacity=16) == 8
async def test_channel_capacity_is_probed_not_assumed(ctx: AppContext) -> None:
"""Capacity comes from probing the device, and counts slots, not their occupants."""
device = await ctx.device()
assert await device.channel_capacity() == 8 # the mock models stock 8-slot firmware
# Occupying some slots must not change the ceiling — capacity is how many slots exist.
await device.set_channel(0, "Alpha", bytes(range(16)))
await device.set_channel(3, "Bravo", bytes(range(16)))
assert await device.channel_capacity() == 8
async def test_channel_capacity_tracks_a_larger_ceiling(ctx: AppContext) -> None:
"""Firmware with more slots is discovered as such — no hard-coded 8 anywhere."""
device = await ctx.device()
device._max_channels = 12 # simulate a firmware build with a larger slot table
assert await device.channel_capacity() == 12
async def test_apply_order_relays_channels_into_new_positions(ctx: AppContext) -> None:
@@ -189,6 +208,88 @@ def test_channel_identity_is_stable_across_slot_moves() -> None:
assert other.identity != at_two.identity # a different key → a different channel
class _ScriptedUi:
"""A UI surface that replays queued answers, for driving the channel manager headless.
``select``/``text``/``confirm`` each pop their next scripted answer; the display methods
are no-ops. Enough of the :class:`~meshtools.ui.surface.Ui` contract for the channel
manager's create/clear flows.
"""
def __init__(self, selects: list, texts: list, confirms: list) -> None:
self._selects = list(selects)
self._texts = list(texts)
self._confirms = list(confirms)
def show(self, *renderables) -> None: # noqa: ANN002
pass
def note(self, markup: str) -> None:
pass
async def view(self, renderable, *, title: str = "", footer_hint: str = "") -> None: # noqa: ANN001
pass
async def select(self, title: str, items: list, *, default=None): # noqa: ANN001, ANN201
return self._selects.pop(0) if self._selects else "__back__"
async def text(self, title: str, *, default: str = "", validate=None, help_text: str = "", password: bool = False): # noqa: ANN001, ANN201
return self._texts.pop(0)
async def confirm(self, title: str, *, default: bool = True): # noqa: ANN201
return self._confirms.pop(0)
async def test_recreating_a_slot_refiles_messages_to_the_new_channel(ctx: AppContext) -> None:
"""Clearing a channel and creating another that reuses its slot must not cross history.
Reproduces the reported bug: inbound channel messages carry only a slot index, which the
chat service maps to a channel identity through a cache. When a slot is cleared and a new
channel takes it, a stale cache filed the newcomer's messages under the old channel — so
opening the old channel showed the new one's transcript. The manager must refresh the
cache after the mutation so a message on the reused slot lands in the right channel.
"""
from meshtools.core.channels import channel_identity
from meshtools.core.events import MeshEvent
from meshtools.core.models import Message
from meshtools.ui.channels import _BACK, _CLEAR, _CREATE, manage_channels
device = await ctx.device()
await device.set_channel(0, "Public", DEFAULT_PUBLIC_SECRET)
public_id = channel_identity("Public", DEFAULT_PUBLIC_SECRET)
await ctx.chat.start() # prime the slot→identity cache (slot 0 == Public)
try:
# A message on Public arrives before we touch anything — files under Public.
ctx.events.publish(
MeshEvent.message_event(Message(text="hi public", channel=0, is_channel=True))
)
# Drive the manager: open Public's detail → clear it, then create a new private
# channel (which reuses freed slot 0), then back out.
ctx.ui = _ScriptedUi(
selects=[0, _CLEAR, _CREATE, _BACK],
texts=["Ops"], # the new channel's name
confirms=[True], # confirm the clear
)
await manage_channels(ctx)
ops = next(s for s in await _read_slots(device) if s.name == "Ops")
assert ops.idx == 0 # the new channel took the freed slot
# A message now arrives on slot 0 — which is Ops, not Public.
ctx.events.publish(
MeshEvent.message_event(Message(text="ops secret", channel=0, is_channel=True))
)
public_msgs = ctx.repo.recent_chat_messages(is_channel=True, channel_id=public_id)
ops_msgs = ctx.repo.recent_chat_messages(is_channel=True, channel_id=ops.identity)
assert [m.text for m in public_msgs] == ["hi public"] # unchanged, no leakage
assert [m.text for m in ops_msgs] == ["ops secret"] # filed under the right channel
finally:
await ctx.chat.stop()
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.
+189 -1
View File
@@ -409,6 +409,141 @@ async def test_chat_screen_enter_sends_and_appends() -> None:
assert session.invalidations > 0
def test_byte_counter_shows_used_over_limit_and_colors_only_used() -> None:
"""The compose bar shows ``used/limit`` with only the used count styled (the max is muted)."""
from rich.text import Text
screen = _screen(_StubSession(), send=None)
for ch in "hello":
screen.handle("text", ch)
counter = screen._byte_counter(80, screen._used_bytes(), screen._byte_limit())
assert isinstance(counter, Text)
assert counter.plain.strip() == "5/150" # 5 bytes used of the 150-byte direct-message cap
# Only the "5" carries a color; the "/150" tail stays muted (it never changes).
used_at = counter.plain.index("5")
slash_at = counter.plain.index("/")
used_spans = [s for s in counter.spans if s.start <= used_at < s.end]
tail_spans = [s for s in counter.spans if s.start <= slash_at < s.end]
assert used_spans and used_spans[0].style == "ok" # green with room to spare
assert tail_spans and tail_spans[0].style == "muted"
def test_byte_style_escalates_as_budget_runs_out() -> None:
"""The used-byte color steps green → yellow → orange → red as fewer bytes remain."""
from meshtools.ui.chat import _BYTES_ORANGE, _BYTES_YELLOW
style = ChatScreen._byte_style
assert style(80) == "ok" # plenty left → green
assert style(20) == _BYTES_YELLOW # within the tight band → yellow
assert style(10) == _BYTES_ORANGE # within the low band → orange
assert style(0) == "err" # limit reached → red
assert style(-5) == "err" # over the limit → still red
def test_channel_byte_limit_is_lower_than_direct() -> None:
"""A channel broadcast has a tighter byte budget than a direct message."""
from meshtools.ui.chat import _CHANNEL_BYTE_LIMIT, _DM_BYTE_LIMIT
direct = _screen(_StubSession(), send=None)
channel = _channel_screen([])
assert direct._byte_limit() == _DM_BYTE_LIMIT == 150
assert channel._byte_limit() == _CHANNEL_BYTE_LIMIT == 130
def test_overflow_counts_multibyte_characters_by_byte() -> None:
"""An emoji (4 UTF-8 bytes) counts as 4 toward the budget, and overflow marks whole chars."""
screen = _channel_screen([]) # 130-byte limit
# 32 emojis = 128 bytes (under), a 33rd tips to 132 (over) at that whole character.
for _ in range(33):
screen.handle("text", "😀")
assert screen._used_bytes() == 33 * 4
assert screen._overflow_at(screen._byte_limit()) == 32 # the 33rd emoji is the first over
async def test_over_limit_message_is_not_sent_and_buffer_is_kept() -> None:
"""Enter on an over-budget line reports the overage and sends nothing, keeping the text."""
session = _StubSession()
sent: list[str] = []
async def send(text: str) -> ChatMessage:
sent.append(text)
return ChatMessage(text=text, outbound=True, peer="d4e5f6a7", acked=True)
screen = _screen(session, send=send)
for ch in "x" * 151: # one byte past the 150-byte direct cap
screen.handle("text", ch)
screen.handle("enter")
await asyncio.sleep(0) # nothing should have been scheduled, but let the loop turn
assert sent == [] # the send was refused
assert screen._editor.text == "x" * 151 # buffer kept intact so the user can trim it
assert "Too long by 1 byte" in screen._status
# Trimming back under the limit clears the notice.
screen.handle("backspace")
assert screen._status == ""
def test_chat_screen_shows_delivery_glyphs() -> None:
"""Each outbound direct message ends with its delivery emoji (⏳ / ✅ / ❌)."""
messages = [
ChatMessage(text="delivered", outbound=True, peer="d4e5f6a7", acked=True),
ChatMessage(text="dropped", outbound=True, peer="d4e5f6a7", acked=False),
ChatMessage(text="inflight", outbound=True, peer="d4e5f6a7", acked=None),
]
screen = _screen(_StubSession(), send=None, messages=messages)
joined = "\n".join(screen.render_body(60))
assert "✅" in joined and "❌" in joined and "⏳" in joined
# A failed message advertises the retry shortcut in the footer hint.
assert "Ctrl-R" in screen.footer_hint
def test_direct_transcript_groups_under_sender_headers() -> None:
"""Direct chats use the same grouped layout as channels: one header per sender run."""
from datetime import datetime, timezone
base = datetime(2026, 7, 5, 14, 24, tzinfo=timezone.utc)
messages = [
ChatMessage(text="hi", peer="d4e5f6a7", created_at=base),
ChatMessage(text="you there?", peer="d4e5f6a7", created_at=base),
ChatMessage(text="yes!", outbound=True, peer="d4e5f6a7", acked=True, created_at=base),
]
screen = _screen(_StubSession(), send=None, messages=messages)
rendered = _strip_ansi("\n".join(screen._render_grouped(80)))
# Inbound sender resolves to the contact name (from the names map), not the raw key.
assert rendered.count("Alice") == 1 # the two inbound messages share one header
assert "d4e5f6a7" not in rendered # the key is never shown when a name is known
assert "you" in rendered # our own reply gets its own header
assert "hi" in rendered and "you there?" in rendered and "yes!" in rendered
assert "✅" in rendered # the outbound message keeps its delivery glyph
async def test_chat_screen_retry_resends_failed_message() -> None:
"""Ctrl-R re-attempts the latest unacknowledged message, flipping it in place."""
session = _StubSession()
failed = ChatMessage(text="oops", outbound=True, peer="d4e5f6a7", acked=False, row_id=7)
async def resend(message: ChatMessage) -> ChatMessage:
message.acked = True # the retry gets through this time
return message
conv = Conversation(
label="Alice",
is_channel=False,
contact=Contact(name="Alice", public_key="d4" + "0" * 62, key_prefix="d4e5f6a7"),
)
screen = ChatScreen(
conv, [failed], send=None, names={}, session=session, resend=resend
)
screen.handle("retry")
await asyncio.sleep(0) # let the scheduled resend task run
assert screen._messages[-1].acked is True # same object, now acknowledged
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)
@@ -448,7 +583,7 @@ def test_channel_transcript_groups_by_sender() -> None:
ChatMessage(text="hello all", outbound=True, is_channel=True, channel_idx=0, created_at=at(9)),
]
screen = ChatScreen(conv, messages, send=None, names={}, session=_StubSession())
rendered = _strip_ansi("\n".join(screen._render_channel(80)))
rendered = _strip_ansi("\n".join(screen._render_grouped(80)))
assert rendered.count("Alice") == 1 # the two Alice messages share one header
assert "Bob" in rendered and "you" in rendered
@@ -461,6 +596,34 @@ def test_channel_transcript_groups_by_sender() -> None:
assert stamps[0] != stamps[1] # grouped Alice messages show distinct times
def test_channel_self_style_keyed_on_concept_not_label() -> None:
"""Our white 'self' style follows the message being outbound, not the 'you' label.
A remote sender who happens to be named 'you' must still get a palette hue, never the
white style reserved for us.
"""
from meshtools.ui.chat import _SENDER_COLORS
screen = _channel_screen([])
assert screen._sender_style("you", is_self=True) == "you" # us → white
remote = screen._sender_style("you", is_self=False)
assert remote != "you" and remote in _SENDER_COLORS # remote 'you' → a normal hue
def test_channel_own_messages_do_not_merge_with_remote_namesake() -> None:
"""A remote sender literally named 'you' groups separately from our own messages."""
from datetime import datetime, timezone
base = datetime(2026, 7, 5, 14, 24, tzinfo=timezone.utc)
messages = [
ChatMessage(text="you: impostor", is_channel=True, channel_idx=0, created_at=base),
ChatMessage(text="mine", outbound=True, is_channel=True, channel_idx=0, created_at=base),
]
screen = _channel_screen(messages)
rendered = _strip_ansi("\n".join(screen._render_grouped(80)))
assert rendered.count("you") == 2 # two separate headers, not one merged group
def _channel_screen(messages, session=None) -> ChatScreen:
"""Build a channel ChatScreen over ``messages`` for selection/reply tests."""
conv = Conversation(label="#public", is_channel=True, channel_idx=0)
@@ -570,6 +733,31 @@ async def test_service_send_records_outbound(repo: Repository) -> None:
await device.disconnect()
async def test_service_resend_updates_ack_in_place(repo: Repository) -> None:
"""Resending a failed message flips its stored ack rather than adding a duplicate row."""
device = MockDevice()
ctx = _StubContext(device, repo)
chat = ChatService(ctx)
await chat.start()
try:
contact = next(c for c in await device.get_contacts() if c.name == "Alice")
peer = contact.key_prefix or contact.public_key[:12]
failed = ChatMessage(
text="retry me", outbound=True, peer=peer, peer_name=contact.name, acked=False
)
failed.row_id = repo.record_chat_message(failed)
await chat.resend_direct(contact, failed)
assert failed.acked is True # the mock always acknowledges
stored = repo.recent_chat_messages(is_channel=False, peer=peer)
assert len(stored) == 1 # updated in place, not duplicated
assert stored[0].acked is True
finally:
await chat.stop()
await device.disconnect()
# -- end-to-end through the real session --------------------------------------