aboutsummaryrefslogtreecommitdiffstats
path: root/gleam
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-11-15 20:44:37 -0500
committerelioat <elioat@tilde.institute>2025-11-15 20:44:37 -0500
commit42be94d2b24ce91bf6baf43407dd1fd2bb0b60cf (patch)
treee0557d5727ab0c5b5b638e4d24de78efde517dac /gleam
parentf0dce4b348acea832f1059e0e0654320cae3e026 (diff)
downloadtour-42be94d2b24ce91bf6baf43407dd1fd2bb0b60cf.tar.gz
Fix duplicate message display by tracking local vs forwarded messages
Add an is_local flag to Broadcast messages to distinguish between messages originating from local input vs those forwarded from peers. When a message originates locally, we don't display it again (avoiding echo), but when it comes from a peer, we display it to the user. This prevents the duplicate message issue where both the terminal echo and the registry display were showing the user's own messages.
Diffstat (limited to 'gleam')
-rw-r--r--gleam/gossip/src/messages.gleam9
-rw-r--r--gleam/gossip/src/p2p_peer.gleam17
-rw-r--r--gleam/gossip/src/registry.gleam84
3 files changed, 62 insertions, 48 deletions
diff --git a/gleam/gossip/src/messages.gleam b/gleam/gossip/src/messages.gleam
index 4d6f393..592f446 100644
--- a/gleam/gossip/src/messages.gleam
+++ b/gleam/gossip/src/messages.gleam
@@ -14,10 +14,11 @@ pub type Message {
ClientJoin(name: String, subject: process.Subject(Message), origin_id: Int)
/// Client leaving (propagates to all peers except origin)
ClientLeave(name: String, origin_id: Int)
- /// Message broadcast to all peers (propagates except to the peer that sent it)
- /// last_sender is the registry_id of the peer that relayed this message to us
- /// timestamp: HH:MM:SS when the message was created
- Broadcast(from: String, text: String, origin_id: Int, last_sender_id: Int, timestamp: String)
+ /// Message broadcast to all peers (propagates except to the peer that sent it)
+ /// last_sender is the registry_id of the peer that relayed this message to us
+ /// timestamp: HH:MM:SS when the message was created
+ /// is_local: True if this message originated from local input (not forwarded)
+ Broadcast(from: String, text: String, origin_id: Int, last_sender_id: Int, timestamp: String, is_local: Bool)
/// Gossip message with client list and peer info for network discovery
/// Includes peer names and IDs so new peers can connect to the full network
Gossip(client_names: List(String), peer_info: List(#(String, Int)))
diff --git a/gleam/gossip/src/p2p_peer.gleam b/gleam/gossip/src/p2p_peer.gleam
index f1304d6..cde8cc2 100644
--- a/gleam/gossip/src/p2p_peer.gleam
+++ b/gleam/gossip/src/p2p_peer.gleam
@@ -172,14 +172,15 @@ pub fn input_loop(registry_subject: process.Subject(messages.Message), name: Str
Nil
}
_ -> {
- // Get current timestamp
- let timestamp = get_timestamp()
-
- // Send message to registry with:
- // - origin_id = our registry_id (so other peers know it's from us)
- // - last_sender_id = -1 (local input, not forwarded from peers)
- // - timestamp = current time
- process.send(registry_subject, messages.Broadcast(name, trimmed, registry_id, -1, timestamp))
+ // Get current timestamp
+ let timestamp = get_timestamp()
+
+ // Send message to registry with:
+ // - origin_id = our registry_id (so other peers know it's from us)
+ // - last_sender_id = -1 (local input, not forwarded from peers)
+ // - timestamp = current time
+ // - is_local = True (indicates this came from local input)
+ process.send(registry_subject, messages.Broadcast(name, trimmed, registry_id, -1, timestamp, True))
input_loop(registry_subject, name, registry_id)
}
}
diff --git a/gleam/gossip/src/registry.gleam b/gleam/gossip/src/registry.gleam
index f9876e5..fe0a3d3 100644
--- a/gleam/gossip/src/registry.gleam
+++ b/gleam/gossip/src/registry.gleam
@@ -326,42 +326,54 @@ fn loop(subject: process.Subject(messages.Message), state: RegistryState) -> Nil
loop(subject, new_state)
}
- messages.Broadcast(from, text, origin_id, last_sender_id, timestamp) -> {
- // Broadcast message: display and forward
- //
- // Message display strategy: Display all messages exactly once.
- // The key insight: In a mesh network, messages can arrive via multiple paths.
- // To display each message exactly once, track which messages we've seen.
- //
- // We use (origin_id, from, text, timestamp) as a unique identifier.
- // This avoids duplicates while handling mesh topologies correctly.
-
- let message_id = #(origin_id, from, text, timestamp)
- let should_display = !has_seen_message(message_id)
-
- case should_display {
- True -> {
- let _ = process.send(state.local_client, messages.ChatMessage(from, text, timestamp))
- let _ = mark_message_seen(message_id)
- Nil
- }
- False -> Nil
- }
-
- case state.peers {
- [] -> Nil
- _peers -> {
- // Filter out the peer that sent this to us (identified by last_sender_id)
- let peers_to_forward = list.filter(state.peers, fn(peer) {
- peer.1 != last_sender_id
- })
-
- // Forward with our registry_id as last_sender_id so peers know not to echo back
- propagate_broadcasts(peers_to_forward, messages.Broadcast(from, text, origin_id, state.registry_id, timestamp))
- }
- }
- loop(subject, state)
- }
+ messages.Broadcast(from, text, origin_id, last_sender_id, timestamp, is_local) -> {
+ // Broadcast message: display and forward
+ //
+ // Local vs Forwarded:
+ // - is_local=True: This message came from our local input, so it's already been "displayed"
+ // (the user just typed it). We send it to the registry for forwarding only.
+ // - is_local=False: This message came from a peer. Display it to the user.
+ //
+ // Message deduplication: Track which messages we've seen to handle mesh topologies
+ // where a message might arrive via multiple paths.
+ // We use (origin_id, from, text, timestamp) as a unique identifier.
+
+ let message_id = #(origin_id, from, text, timestamp)
+ let should_display = !has_seen_message(message_id) && !is_local
+
+ case should_display {
+ True -> {
+ let _ = process.send(state.local_client, messages.ChatMessage(from, text, timestamp))
+ let _ = mark_message_seen(message_id)
+ Nil
+ }
+ False -> Nil
+ }
+
+ // If this is a local message, still mark it as seen (but don't display)
+ case is_local {
+ True -> {
+ let _ = mark_message_seen(message_id)
+ Nil
+ }
+ False -> Nil
+ }
+
+ case state.peers {
+ [] -> Nil
+ _peers -> {
+ // Filter out the peer that sent this to us (identified by last_sender_id)
+ let peers_to_forward = list.filter(state.peers, fn(peer) {
+ peer.1 != last_sender_id
+ })
+
+ // Forward with our registry_id as last_sender_id so peers know not to echo back
+ // Set is_local=False since we're forwarding from a peer
+ propagate_broadcasts(peers_to_forward, messages.Broadcast(from, text, origin_id, state.registry_id, timestamp, False))
+ }
+ }
+ loop(subject, state)
+ }
messages.Gossip(_peer_client_names, peer_info) -> {
// Gossip: learn about clients and peers from remote registries