From 62f19da405f455c3459c6d0de18bd7b2d7c66d1e Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Thu, 9 Jul 2026 21:41:14 -0400 Subject: [PATCH] Soft-retry unacked direct messages up to twice A direct message was transmitted once and marked failed the moment its single delivery-ack window lapsed, leaving Ctrl-R as the only recourse. Now the send path re-sends an unacknowledged message automatically before giving up, matching the mesh convention of a few tries. _deliver_direct loops send_direct_message until an ack lands or the budget is spent; both the initial send and the manual Ctrl-R resend route through it. Each attempt already blocks a full ack window before the next, so the retries are spaced by that window rather than hammering the radio, and a hard send rejection propagates without retry. The budget is the new direct_message_soft_retries setting: retries + 1 tries, clamped to 0..2 so a message is never re-broadcast more than twice (0 = one shot, default 2 = up to three tries). Co-Authored-By: Claude Opus 4.8 --- config.example.toml | 4 ++ meshterm/core/config.py | 26 ++++++++++++ meshterm/services/chat_service.py | 57 +++++++++++++++++++++++--- tests/test_chat.py | 67 ++++++++++++++++++++++++++++++- tests/test_config.py | 26 +++++++++++- 5 files changed, 172 insertions(+), 8 deletions(-) diff --git a/config.example.toml b/config.example.toml index 6602b6f..ee4a6d6 100644 --- a/config.example.toml +++ b/config.example.toml @@ -9,6 +9,10 @@ default_profile = "s3" trace_cooldown_s = 1.0 +# Automatic re-sends of a direct message that goes unacknowledged, before it's marked failed. +# 0, 1, or 2 (clamped): 0 = one shot, 2 (default) = up to three tries. Ctrl-R in chat is a +# further manual retry on top of this. +direct_message_soft_retries = 2 # db_path = "C:/Users/you/meshterm/meshterm.db" # output_dir = "C:/Users/you/meshterm/output" diff --git a/meshterm/core/config.py b/meshterm/core/config.py index d8f3149..fc4bab2 100644 --- a/meshterm/core/config.py +++ b/meshterm/core/config.py @@ -19,6 +19,14 @@ else: # pragma: no cover - exercised only on 3.10 import tomli as tomllib +#: Hard ceiling on a direct message's *soft retries* — the automatic re-sends that follow the +#: initial transmission when it goes unacknowledged. Capped at 2, so a message gets at most one +#: send plus two soft retries (three tries total) and is never re-broadcast more than twice on +#: the shared mesh before being called a failure. :data:`Settings.direct_message_soft_retries` +#: is clamped to ``0..DIRECT_MESSAGE_MAX_SOFT_RETRIES``. +DIRECT_MESSAGE_MAX_SOFT_RETRIES = 2 + + def default_config_dir() -> Path: """Return the directory MeshTerm uses for config and data. @@ -78,6 +86,16 @@ class Settings: trace_cooldown_s: Minimum delay between transmit bursts (duty-cycle safety). tx_opt_min: Lowest TX power explored by the remote-admin optimizer (dBm). tx_opt_max: Highest TX power explored by the remote-admin optimizer (dBm). + direct_message_soft_retries: How many times a direct message is automatically + re-sent after its initial transmission goes unacknowledged, before it is reported + as failed. Each soft retry waits a full delivery-ack window (see + :meth:`Device.send_direct_message`) before firing, so a message only resends after + genuinely going unanswered rather than hammering the radio. Capped at 0, 1, or 2 — + clamped to ``0..DIRECT_MESSAGE_MAX_SOFT_RETRIES`` on load — so the send is tried at + most three times total. ``0`` disables soft retries (one shot); the default of + ``2`` matches the mesh convention of a few tries before giving up. The manual + Ctrl-R resend in the chat screen is a further, user-driven retry layered on top of + these automatic ones, not a substitute for them. connect_on_start: Whether the interactive session opens the companion connection (and starts always-on background listening) immediately at launch. When ``False`` the connection is opened lazily — only once monitoring is turned on @@ -92,6 +110,7 @@ class Settings: trace_cooldown_s: float = 1.0 tx_opt_min: int = 12 tx_opt_max: int = 28 + direct_message_soft_retries: int = 2 connect_on_start: bool = True def __post_init__(self) -> None: @@ -160,5 +179,12 @@ class Settings: trace_cooldown_s=float(data.get("trace_cooldown_s", 1.0)), tx_opt_min=int(data.get("tx_opt_min", 12)), tx_opt_max=int(data.get("tx_opt_max", 28)), + direct_message_soft_retries=max( + 0, + min( + DIRECT_MESSAGE_MAX_SOFT_RETRIES, + int(data.get("direct_message_soft_retries", DIRECT_MESSAGE_MAX_SOFT_RETRIES)), + ), + ), connect_on_start=bool(data.get("connect_on_start", True)), ) diff --git a/meshterm/services/chat_service.py b/meshterm/services/chat_service.py index c0c4e12..058bad8 100644 --- a/meshterm/services/chat_service.py +++ b/meshterm/services/chat_service.py @@ -275,19 +275,63 @@ class ChatService: except Exception as exc: # noqa: BLE001 - never let logging break the subscription self._ctx.log.debug("chat: failed to record message: %s", exc) + async def _deliver_direct(self, contact: Contact, text: str): + """Transmit a direct message, softly retrying until it is acknowledged. + + A single logical send makes one initial transmission plus up to + ``settings.direct_message_soft_retries`` soft retries (two by default, three tries in + all): the message is re-sent only when an attempt goes unacknowledged, and each device + call already blocks for a full delivery-ack window (see + :meth:`~meshterm.core.connection.Device.send_direct_message`) before returning, so the + retries are naturally spaced by that window rather than hammering the radio. The loop + stops the moment an ack arrives; if none ever does, the last (``None``) result stands + and the message is recorded unacknowledged — leaving Ctrl-R in the chat screen as the + user-driven retry on top of these automatic ones. + + Args: + contact: The recipient. + text: The message body. + + Returns: + The delivery :class:`~meshterm.core.models.Ack` from the first attempt that landed, + or ``None`` if every attempt went unacknowledged. + + Raises: + Exception: Propagates a hard send failure (the companion rejecting the send); such + a rejection is not retried, since it is not a lost-in-the-mesh timeout. + """ + device = await self._ctx.device() + attempts = max(0, self._ctx.settings.direct_message_soft_retries) + 1 + ack = None + for attempt in range(1, attempts + 1): + ack = await device.send_direct_message(contact, text) + if ack is not None: + return ack + if attempt < attempts: + self._ctx.log.debug( + "chat: DM to %s unacked on try %d/%d; soft-retrying", + contact.name, + attempt, + attempts, + ) + return ack + async def send_direct(self, contact: Contact, text: str) -> ChatMessage: """Send a direct message to a contact and record it in history. + Delivery is attempted with up to ``settings.direct_message_soft_retries`` automatic + soft retries (see :meth:`_deliver_direct`) before the message is recorded as + unacknowledged. + Args: contact: The recipient. text: The message body. Returns: The recorded outbound :class:`ChatMessage` (its ``acked`` reflects whether a - delivery acknowledgement arrived). + delivery acknowledgement arrived within the soft-retry budget). """ - device = await self._ctx.device() - ack = await device.send_direct_message(contact, text) + ack = await self._deliver_direct(contact, text) chat = ChatMessage( text=text, outbound=True, @@ -306,7 +350,9 @@ class ChatService: 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). + (or stays unacknowledged for another retry). Like an initial send, each manual retry + makes up to ``settings.direct_message_soft_retries`` soft retries of its own (see + :meth:`_deliver_direct`). Args: contact: The recipient. @@ -316,8 +362,7 @@ class ChatService: Returns: The same ``message``, with :attr:`~ChatMessage.acked` refreshed. """ - device = await self._ctx.device() - ack = await device.send_direct_message(contact, message.text) + ack = await self._deliver_direct(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) diff --git a/tests/test_chat.py b/tests/test_chat.py index ff65974..0258f02 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -21,6 +21,7 @@ def _strip_ansi(text: str) -> str: return _ANSI.sub("", text) from meshterm.core.channels import DEFAULT_PUBLIC_SECRET, derive_secret +from meshterm.core.config import Settings from meshterm.core.connection import MockDevice from meshterm.core.events import MeshEvent from meshterm.core.models import ( @@ -56,6 +57,7 @@ class _StubContext: self.repo = repo self.log = logging.getLogger("test.chat") self.profile_name = None + self.settings = Settings() self.events = EventHub(self) async def device(self) -> MockDevice: @@ -1062,6 +1064,70 @@ async def test_service_resend_updates_ack_in_place(repo: Repository) -> None: await device.disconnect() +class _ScriptedDevice: + """A device stub whose direct-message acks follow a fixed script. + + Each :meth:`send_direct_message` pops the next value from ``acks`` — an :class:`Ack` + stand-in (any non-``None`` object counts as delivered) or ``None`` for an unacknowledged + transmission — and appends the sent text to :attr:`sent`, so a test can assert exactly how + many soft retries fired. Once the script is exhausted every further send goes unacked. + """ + + def __init__(self, acks: list) -> None: + self._acks = list(acks) + self.sent: list[str] = [] + + async def connect(self) -> None: # satisfies _StubContext.device() + return None + + async def send_direct_message(self, contact: Contact, text: str): + self.sent.append(text) + return self._acks.pop(0) if self._acks else None + + +def _contact() -> Contact: + return Contact(name="Alice", public_key="d4e5" + "0" * 60, key_prefix="d4e5f6a7") + + +async def test_send_direct_soft_retries_until_acked(repo: Repository) -> None: + """A DM unacked on the first tries is re-sent, and is recorded delivered once one lands.""" + device = _ScriptedDevice([None, None, object()]) # ack only on the third try + ctx = _StubContext(device, repo) + ctx.settings.direct_message_soft_retries = 2 # 1 send + 2 retries = 3 tries + chat = ChatService(ctx) + + sent = await chat.send_direct(_contact(), "hey") + + assert sent.acked is True + assert device.sent == ["hey", "hey", "hey"] # exactly three transmissions + + +async def test_send_direct_soft_retries_capped_by_setting(repo: Repository) -> None: + """The retry budget stops the resends: an always-unacked DM is tried retries+1 times.""" + device = _ScriptedDevice([]) # never acknowledges + ctx = _StubContext(device, repo) + ctx.settings.direct_message_soft_retries = 2 + chat = ChatService(ctx) + + sent = await chat.send_direct(_contact(), "hey") + + assert sent.acked is False + assert len(device.sent) == 3 # capped: no more than one send plus two soft retries + + +async def test_send_direct_zero_retries_is_one_shot(repo: Repository) -> None: + """With soft retries disabled a DM is transmitted exactly once, acked or not.""" + device = _ScriptedDevice([]) # never acknowledges + ctx = _StubContext(device, repo) + ctx.settings.direct_message_soft_retries = 0 + chat = ChatService(ctx) + + sent = await chat.send_direct(_contact(), "hey") + + assert sent.acked is False + assert device.sent == ["hey"] # one shot, no soft retry + + # -- end-to-end through the real session -------------------------------------- @@ -1073,7 +1139,6 @@ async def test_open_chat_sends_through_real_session(tmp_path: Path) -> None: from meshterm.context import AppContext from meshterm.core.admin_store import AdminStore - from meshterm.core.config import Settings from meshterm.core.device_store import DeviceStore from meshterm.ui.chat import open_chat from meshterm.ui.surface import TuiUi diff --git a/tests/test_config.py b/tests/test_config.py index a98772f..ffe9e9c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -4,7 +4,7 @@ from __future__ import annotations from pathlib import Path -from meshterm.core.config import Settings +from meshterm.core.config import DIRECT_MESSAGE_MAX_SOFT_RETRIES, Settings def test_connect_on_start_defaults_true() -> None: @@ -17,3 +17,27 @@ def test_connect_on_start_loads_from_toml(tmp_path: Path) -> None: config = tmp_path / "config.toml" config.write_text("connect_on_start = false\n", encoding="utf-8") assert Settings.load(config).connect_on_start is False + + +def test_direct_message_soft_retries_defaults_to_two() -> None: + """Two soft retries (three tries total) is the out-of-the-box direct-message budget.""" + assert Settings().direct_message_soft_retries == 2 + assert DIRECT_MESSAGE_MAX_SOFT_RETRIES == 2 + + +def test_direct_message_soft_retries_loads_from_toml(tmp_path: Path) -> None: + """The soft-retry budget round-trips from the config file.""" + config = tmp_path / "config.toml" + config.write_text("direct_message_soft_retries = 1\n", encoding="utf-8") + assert Settings.load(config).direct_message_soft_retries == 1 + + +def test_direct_message_soft_retries_clamped_to_range(tmp_path: Path) -> None: + """Out-of-range values are clamped to 0..2 so a DM never floods the mesh (or is dropped).""" + hot = tmp_path / "hot.toml" + hot.write_text("direct_message_soft_retries = 9\n", encoding="utf-8") + assert Settings.load(hot).direct_message_soft_retries == DIRECT_MESSAGE_MAX_SOFT_RETRIES + + cold = tmp_path / "cold.toml" + cold.write_text("direct_message_soft_retries = -4\n", encoding="utf-8") + assert Settings.load(cold).direct_message_soft_retries == 0