From 69526e76e08d34e0edc43fed023e74f509eb372c Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Sun, 19 Jul 2026 12:19:23 -0400 Subject: [PATCH] Chat colour: an unknown @mention in a direct chat stays muted MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A direct thread's sender label borrows the peer's key hue so a name that won't resolve still carries a colour. That peer-key fallback was leaking onto @mentions in the message body, painting an unknown mentioned name with the peer's hue instead of leaving it gray. A mention names an arbitrary person, so _sender_style(mention=True) now skips the fallback — an unresolved mention stays muted, matching channel behaviour. Co-Authored-By: Claude Opus 4.8 --- meshterm/ui/chat.py | 18 ++++++++++++------ tests/test_chat.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/meshterm/ui/chat.py b/meshterm/ui/chat.py index f9f50d7..c8f962e 100644 --- a/meshterm/ui/chat.py +++ b/meshterm/ui/chat.py @@ -365,7 +365,7 @@ class ChatScreen(Screen): if match.start() > pos: text.append(body[pos : match.start()], style=base) name = match.group(1) - text.append(f"@{name}", style=self._sender_style(name)) + text.append(f"@{name}", style=self._sender_style(name, mention=True)) pos = match.end() if pos < len(body): text.append(body[pos:], style=base) @@ -402,23 +402,29 @@ class ChatScreen(Screen): "Enter to see the paths this message took · End to cancel", style="accent" ) - def _sender_style(self, sender: str, *, is_self: bool = False) -> str: + def _sender_style( + self, sender: str, *, is_self: bool = False, mention: bool = False + ) -> str: """Pick a stable color for a sender name — keyed on the sender's node key. 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 resolves its name back to a key (contacts, then the recorder's - stored names; a direct thread falls back to the peer's own key) and takes that - key's hue; a name no known node carries stays muted — the app-wide rule that - colour marks a keyed identity. + stored names; a direct thread's *sender label* falls back to the peer's own key) + and takes that key's hue; a name no known node carries stays muted — the app-wide + rule that colour marks a keyed identity. + + ``mention=True`` styles an ``@mention`` in the body rather than a sender label: a + mention names an arbitrary person, so the peer-key fallback must not apply — an + unresolved one stays muted (gray) in a direct chat exactly as it does in a channel. """ if is_self: return "you" # white, out of the per-sender hue range — always easy to spot if sender == "·": return "muted" key = self._key_of(sender) - if not key and not self._is_channel: + if not key and not self._is_channel and not mention: key = self._peer_key or None return name_style(sender, key) diff --git a/tests/test_chat.py b/tests/test_chat.py index 3c77cc8..fa829a5 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -756,6 +756,35 @@ def test_at_mention_renders_as_name_in_sender_hue() -> None: assert screen._sender_style("Zed") == "muted" +def test_direct_chat_unknown_mention_stays_muted() -> None: + """In a direct chat an unresolved ``@mention`` stays muted — as it does in a channel. + + A direct thread's *sender label* falls back to the peer's own key so it keeps its + hue even when the display name won't resolve, but that fallback must not leak onto + an ``@mention`` in the body: a mention names an arbitrary person, so an unknown one + is gray. Regression guard for the peer-key fallback bleeding into mentions. + """ + from meshterm.ui.theme import node_style + + # Direct chat with peer key d4… — the sender label may borrow this hue, mentions must not. + conv = Conversation( + label="Alice", is_channel=False, + contact=Contact(name="Alice", public_key="d4" + "0" * 62, key_prefix="d4e5f6a7"), + ) + screen = ChatScreen( + conv, [], send=None, names={"d4e5f6a7": "Alice"}, session=_StubSession(), + key_of=_keys_of({"alice": "60" + "0" * 62}), + ) + + # A resolvable mention still lights in its own key-derived hue… + assert screen._sender_style("Alice", mention=True) == node_style("60") + # …but an unknown mention is muted, not painted with the peer's (d4…) hue. + assert screen._sender_style("Zed", mention=True) == "muted" + assert screen._sender_style("Zed", mention=True) != node_style("d4") + # The sender label keeps the peer-key fallback (unchanged behaviour). + assert screen._sender_style("Zed") == node_style("d4") + + 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