From c3d712eff5520c9cff5f42dea1fa3ce71faf5ab6 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Fri, 24 Jul 2026 04:15:58 -0400 Subject: [PATCH] Channels: offer to add the standard Public channel when absent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An add-action restores MeshCore's built-in fixed-key Public channel on the next free slot, shown only while no slot already carries the well-known secret. Leads with the 🌐 globe to distinguish it from the name-derived # Public channel row. Co-Authored-By: Claude Opus 4.8 (1M context) --- meshterm/ui/channels.py | 38 ++++++++++++++++++++++++++++++-------- tests/test_channels.py | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/meshterm/ui/channels.py b/meshterm/ui/channels.py index 6642348..25314d2 100644 --- a/meshterm/ui/channels.py +++ b/meshterm/ui/channels.py @@ -45,6 +45,7 @@ from rich.text import Text from ..core.channels import ( CHANNEL_SLOT_EMPTY_RUN, CHANNEL_SLOT_PROBE_CAP, + DEFAULT_PUBLIC_SECRET, MAX_CHANNELS, channel_hash, channel_identity, @@ -73,6 +74,7 @@ if TYPE_CHECKING: # Top-menu action sentinels (distinct from a plain slot index, which selects that channel). _CREATE = "__create__" _PUBLIC = "__public__" +_DEFAULT_PUBLIC = "__default_public__" _JOIN = "__join__" _IMPORT = "__import__" _REORDER = "__reorder__" @@ -181,6 +183,8 @@ async def manage_channels(ctx: "AppContext") -> int: before = changes if choice == _CREATE: changes += await _create_private(ctx, device, slots, capacity) + elif choice == _DEFAULT_PUBLIC: + changes += await _add_default_public(ctx, device, slots, capacity) elif choice == _PUBLIC: changes += await _add_public(ctx, device, slots, capacity) elif choice == _JOIN: @@ -546,16 +550,22 @@ def _menu_items( items.append(Choice(title="↕ Reorder channels", value=_REORDER)) items.append(section_heading("Add a channel")) - items.extend( - menu_rows( - [ - ("+ New private channel…", "A fresh random key", _CREATE), - ("# Public channel…", "Key derived from its name", _PUBLIC), - ("🔑 Join with a key…", "Paste a channel's 32-hex key", _JOIN), - ("🔗 Import a link…", "Paste a meshcore:// share link", _IMPORT), - ] + rows = [] + # The firmware's built-in fixed-key Public channel has one well-known secret, so it's the + # same channel on every slot — offer to restore it only while no slot already holds it. + if not any(s.secret == DEFAULT_PUBLIC_SECRET for s in slots): + rows.append( + ("🌐 Standard Public channel", "MeshCore's built-in meshwide channel", _DEFAULT_PUBLIC) ) + rows.extend( + [ + ("+ New private channel…", "A fresh random key", _CREATE), + ("# Public channel…", "Key derived from its name", _PUBLIC), + ("🔑 Join with a key…", "Paste a channel's 32-hex key", _JOIN), + ("🔗 Import a link…", "Paste a meshcore:// share link", _IMPORT), + ] ) + items.extend(menu_rows(rows)) items.extend(back_rows(_BACK)) return f"Channels — {len(slots)}/{capacity} slots", items @@ -693,6 +703,18 @@ async def _create_private( return 1 +async def _add_default_public( + ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int +) -> int: + """Add MeshCore's built-in fixed-key ``Public`` channel on the next free slot.""" + idx = await _pick_free_slot(ctx, slots, capacity) + if idx is None: + return 0 + await device.set_channel(idx, "Public", DEFAULT_PUBLIC_SECRET) + ctx.ui.note("[ok]✓[/ok] added the standard [brand]Public[/brand] channel") + return 1 + + async def _add_public( ctx: "AppContext", device: Device, slots: list[ChannelSlot], capacity: int ) -> int: diff --git a/tests/test_channels.py b/tests/test_channels.py index bae1852..86489f6 100644 --- a/tests/test_channels.py +++ b/tests/test_channels.py @@ -563,6 +563,33 @@ async def test_channel_rows_carry_stats_unread_and_lanes(ctx: AppContext) -> Non assert label.spans[-1].style == "ok" # …and its newest cell reads as live traffic +async def test_standard_public_row_appears_only_while_it_is_absent(ctx: AppContext) -> None: + """The 'Standard Public channel' add-action shows until a slot holds the fixed-key default.""" + from meshterm.core.channels import DEFAULT_PUBLIC_SECRET + from meshterm.ui.channels import _DEFAULT_PUBLIC, _add_default_public, _LiveStats, _menu_items + from meshterm.ui.tui import Choice + + device = await ctx.device() + + def has_default_row(slots: list) -> bool: + _, items = _menu_items(ctx, slots, 8, _LiveStats(ctx)) + return any(isinstance(it, Choice) and it.value == _DEFAULT_PUBLIC for it in items) + + # Absent on an empty table and while an unrelated channel occupies a slot. + assert has_default_row([]) + await device.set_channel(1, "Ops", bytes(range(16))) + slots = await read_channel_slots(device) + assert has_default_row(slots) + + # Adding it lands the well-known secret on the next free slot and retires the offer. + added = await _add_default_public(ctx, device, slots, 8) + assert added == 1 + slots = await read_channel_slots(device) + public = next(s for s in slots if s.secret == DEFAULT_PUBLIC_SECRET) + assert public.name == "Public" and public.is_public + assert not has_default_row(slots) + + async def test_clearing_a_channel_reads_off_its_unread(ctx: AppContext) -> None: """Clearing a channel drops its unread from the global total, not just the slot.""" from meshterm.ui.channels import _clear