aboutsummaryrefslogtreecommitdiffstats
path: root/gleam
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-11-15 21:04:26 -0500
committerelioat <elioat@tilde.institute>2025-11-15 21:04:26 -0500
commitef9042fb2dedff012090ff9c4cc856970202a2d7 (patch)
tree770d32e58ac0b7ca090385c4f2306d6b9be0b236 /gleam
parentb71ee785111d8e5695417e6cd0375c9a91b717ce (diff)
downloadtour-ef9042fb2dedff012090ff9c4cc856970202a2d7.tar.gz
Fix: Format local messages with > prefix in registry (not input_loop)
The previous approach of displaying messages in input_loop caused race conditions and reintroduced duplicate messages. New approach: - input_loop sends message to registry via Broadcast (is_local=True) - Registry receives message and displays it with > prefix for local messages - No direct display in input_loop prevents timing issues This keeps message display centralized in the client actor, preventing race conditions and maintaining the TEA architecture correctly.
Diffstat (limited to 'gleam')
-rw-r--r--gleam/gossip/src/p2p_peer.gleam10
-rw-r--r--gleam/gossip/src/registry.gleam66
2 files changed, 35 insertions, 41 deletions
diff --git a/gleam/gossip/src/p2p_peer.gleam b/gleam/gossip/src/p2p_peer.gleam
index 1bce315..7b7ad8a 100644
--- a/gleam/gossip/src/p2p_peer.gleam
+++ b/gleam/gossip/src/p2p_peer.gleam
@@ -175,22 +175,14 @@ pub fn input_loop(registry_subject: process.Subject(messages.Message), name: Str
// Get current timestamp
let timestamp = get_timestamp()
- // Immediately display local message with > prefix (before it gets to registry)
- // This shows it in the chat history right when the user sends it
- // The > prefix indicates this is a locally sent message (not received)
- io.println(timestamp <> " [" <> name <> "] > " <> trimmed)
-
// 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)
- // Note: We send the message WITHOUT the > prefix (it's just display formatting)
+ // The registry will handle displaying this message with > prefix
process.send(registry_subject, messages.Broadcast(name, trimmed, registry_id, -1, timestamp, True))
- // Display the prompt for the next message
- io.print("> ")
-
input_loop(registry_subject, name, registry_id)
}
}
diff --git a/gleam/gossip/src/registry.gleam b/gleam/gossip/src/registry.gleam
index 8ae717d..5d84332 100644
--- a/gleam/gossip/src/registry.gleam
+++ b/gleam/gossip/src/registry.gleam
@@ -327,38 +327,40 @@ fn loop(subject: process.Subject(messages.Message), state: RegistryState) -> Nil
}
messages.Broadcast(from, text, origin_id, last_sender_id, timestamp, is_local) -> {
- // Broadcast message: display and forward
- //
- // TEA (The Elm Architecture) Model Update:
- // The registry receives messages from two sources:
- // 1. Local input: user types something -> terminal echoes it -> input_loop sends Broadcast(is_local=True)
- // 2. Network: peer sends message -> input_loop not involved -> Broadcast(is_local=False)
- //
- // Display Strategy:
- // - is_local=True: DON'T display (terminal already echoed the input)
- // - is_local=False: Display (this is a message from network, not yet visible to user)
- //
- // Message deduplication: Track seen messages to prevent duplicates in mesh topologies.
- // Use (origin_id, from, text, timestamp) as unique identifier.
-
- let message_id = #(origin_id, from, text, timestamp)
- let already_seen = has_seen_message(message_id)
-
- // Only display if: (1) we haven't seen it before AND (2) it didn't come from local input
- let should_display = !already_seen && !is_local
-
- case should_display {
- True -> {
- let _ = process.send(state.local_client, messages.ChatMessage(from, text, timestamp))
- Nil
- }
- False -> Nil
- }
-
- // Mark as seen regardless of origin
- // Local messages are already visible via terminal echo, but we still track them
- // so that if they bounce back through the network, we don't display them again
- let _ = mark_message_seen(message_id)
+ // Broadcast message: display and forward
+ //
+ // TEA (The Elm Architecture) Model Update:
+ // The registry receives messages from two sources:
+ // 1. Local input: user types something -> input_loop sends Broadcast(is_local=True)
+ // 2. Network: peer sends message -> input_loop not involved -> Broadcast(is_local=False)
+ //
+ // Display Strategy:
+ // - is_local=True: Display with > prefix (marks as user's own message)
+ // - is_local=False: Display without prefix (marks as received from peer)
+ //
+ // Message deduplication: Track seen messages to prevent duplicates in mesh topologies.
+ // Use (origin_id, from, text, timestamp) as unique identifier.
+
+ let message_id = #(origin_id, from, text, timestamp)
+ let already_seen = has_seen_message(message_id)
+
+ // Display if we haven't seen it before (applies to both local and network messages)
+ case !already_seen {
+ True -> {
+ // Add > prefix for local messages, no prefix for received messages
+ let display_text = case is_local {
+ True -> "> " <> text
+ False -> text
+ }
+ let _ = process.send(state.local_client, messages.ChatMessage(from, display_text, timestamp))
+ Nil
+ }
+ False -> Nil
+ }
+
+ // Mark as seen regardless of origin
+ // Local messages need to be tracked so bouncebacks don't display again
+ let _ = mark_message_seen(message_id)
case state.peers {
[] -> Nil