From e6666153f59068fba4c3a029ad8c261c54a47475 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 20:57:28 +0100 Subject: [PATCH] K20.2: a reply shows the line it answers above it, in every Mind's packet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RoomLine carries the mouth's event id and the line it replies to (Replied: sender, quoted words), filled by read_room from a delivery's reply link and a tail line's. Every rendering of a room line (the Interpreter's stimulus, the Persona's heard under Earlier and Just now, a beat's lines) shows a reply as `> {other}: "…"` above `{name}, replying to {other}…`. The quoted words of someone else's line are words she was shown, so the echo check reads them. Verification: k20_a_reply_shows_the_line_it_answers_above_it_in_another_minds_packet (Just now, Earlier, stimulus, bodies, Projector sees none, unquoted reply, plain line unchanged). Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/projection.rs | 13 ++- .../src/projection/interpret/tests.rs | 2 +- crates/mind-body/src/projection/select.rs | 83 ++++++++++++++++--- crates/mind-body/src/projection/tests.rs | 35 +++++++- 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index 2421875..8309a29 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -34,7 +34,7 @@ use huginn_mind::HuginnMindResponse; use serde_json::{Value, json}; use crate::address::{Addressee, is_for, localpart}; -use crate::chat_client::{Chat, ChatPending, Form}; +use crate::chat_client::{Chat, ChatPending, Form, ReplyLink}; use crate::heartbeat::HeartbeatView; pub use narrate::{Answer, Failed, KitModel, Model, Spent, connector_key}; @@ -42,7 +42,7 @@ pub use select::TurnKind; pub use tuning::{TUNING_FILE, TUNING_ROOT}; use narrate::{cap_words, distinct_2, field_names, heard_overlap, judge_prompt, judged, offers_options, persona_prompt, projector_prompt, word_budget, words}; -use select::{History, Inputs, Packet, People, Room, RoomLine, select, turn_room}; +use select::{History, Inputs, Packet, People, Replied, Room, RoomLine, select, turn_room}; /// The agent name on the connector's request ids. const AGENT: &str = "mind-persona"; @@ -254,6 +254,8 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo at_ms: delivery.at_unix_ms, // The heartbeat's rule, the one that woke her: never a second opinion here. to_her: is_for(own, delivery), + event: delivery.event_id.clone(), + reply: replied(&delivery.reply_to), }) .collect(); let tail: Vec = pending @@ -261,7 +263,7 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo .iter() .flatten() .filter(|line| line.form == Form::Message && wanted(&line.room)) - .map(|line| RoomLine { room: line.room.clone(), sender: line.sender.clone(), display: String::new(), text: line.body.clone(), at_ms: line.at_unix_ms, to_her: false }) + .map(|line| RoomLine { room: line.room.clone(), sender: line.sender.clone(), display: String::new(), text: line.body.clone(), at_ms: line.at_unix_ms, to_her: false, event: line.event_id.clone(), reply: replied(&line.reply_to) }) .collect(); let mut report = json!({ "read": true, "messages": messages.len(), "tail": tail.len(), "tail_gaps": pending.tail_gaps }); if let Some(here) = &here @@ -275,6 +277,11 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo (Room { messages, tail, here, last_heard: request.last_heard.clone(), direct, own: request.own_rooms.clone() }, report) } +/// The line a reply answers, as the mouth quoted it within its contract; a dropped link is no reply (`ReplyLink::quote`). +fn replied(link: &ReplyLink) -> Option { + link.quote().map(|quote| Replied { sender: quote.sender.clone(), text: quote.excerpt.clone() }) +} + fn packet_json(packet: &Packet) -> Value { json!({ "kind": packet.kind, diff --git a/crates/mind-body/src/projection/interpret/tests.rs b/crates/mind-body/src/projection/interpret/tests.rs index 24cbefc..cf4e135 100644 --- a/crates/mind-body/src/projection/interpret/tests.rs +++ b/crates/mind-body/src/projection/interpret/tests.rs @@ -18,7 +18,7 @@ fn now() -> chrono::DateTime { } fn line(room: &str, sender: &str, display: &str, text: &str, to_her: bool) -> RoomLine { - RoomLine { room: room.into(), sender: sender.into(), display: display.into(), text: text.into(), at_ms: u64::try_from(now().timestamp_millis()).unwrap() - 60_000, to_her } + RoomLine { room: room.into(), sender: sender.into(), display: display.into(), text: text.into(), at_ms: u64::try_from(now().timestamp_millis()).unwrap() - 60_000, to_her, ..RoomLine::default() } } /// Heimdall's packet of a turn of `kind` in `room` over `state`. diff --git a/crates/mind-body/src/projection/select.rs b/crates/mind-body/src/projection/select.rs index 7593ef8..9c4e458 100644 --- a/crates/mind-body/src/projection/select.rs +++ b/crates/mind-body/src/projection/select.rs @@ -101,8 +101,9 @@ pub enum TurnKind { Reflection, } -/// One line of a room: a message or a line of its tail. -#[derive(Clone, Debug, PartialEq, Eq)] +/// One line of a room: a message or a line of its tail. `event` is the mouth's id of it (empty when the mouth gave none); +/// `reply` is the line it replies to, as the mouth quoted it. +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct RoomLine { pub room: String, pub sender: String, @@ -110,6 +111,16 @@ pub(crate) struct RoomLine { pub text: String, pub at_ms: u64, pub to_her: bool, + pub event: String, + pub reply: Option, +} + +/// The line a reply answers (map 16.20.8 (D)): who said it, and its words as the mouth quoted them, none when it quoted +/// none (redacted, edited, or not text). Every Matrix client shows it above the reply; so does every line rendered here. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Replied { + pub sender: String, + pub text: Option, } /// The room as the mouth showed it: new messages, and the recent tail. @@ -713,6 +724,36 @@ fn name_of(line: &RoomLine, inputs: &Inputs) -> String { } } +/// How the sender of a line a reply answers reads: her own name for her, else as `name_of` reads a line of theirs, with the +/// display name a line of theirs in the room carries. +fn sender_name(sender: &str, inputs: &Inputs) -> String { + let own = text(inputs.state, &["publicName"]); + if sender == inputs.people.own_sender && !own.is_empty() { + return clip(&one_line(own), NAME_BYTES); + } + let display = inputs.room.messages.iter().chain(&inputs.room.tail).find(|line| line.sender == sender && !line.display.trim().is_empty()).map(|line| line.display.clone()).unwrap_or_default(); + name_of(&RoomLine { sender: sender.to_owned(), display, ..RoomLine::default() }, inputs) +} + +/// A reply as every line renders it (map 16.20.8 (D), K20): the line it answers, above it, by who said it and in the words +/// the mouth quoted (`> {other}: "…"`), and `, replying to {other}` after the speaker's name. Both empty for a line that +/// replies to nothing. +fn reply_parts(line: &RoomLine, inputs: &Inputs) -> (String, String) { + let Some(replied) = &line.reply else { + return (String::new(), String::new()); + }; + let other = sender_name(&replied.sender, inputs); + let words = replied.text.as_deref().map_or_else(|| "(no words shown)".to_owned(), spoken); + (format!("> {other}: {words}\n"), format!(", replying to {other}")) +} + +/// The words of a line she was shown: its own, and the words it quotes of another's line when it is a reply to someone +/// else's. What an echo is measured against (`Packet::bodies`). +fn shown_words(line: &RoomLine, own_sender: &str) -> impl Iterator { + let quoted = line.reply.as_ref().filter(|replied| replied.sender != own_sender).and_then(|replied| replied.text.clone()); + std::iter::once(line.text.clone()).chain(quoted) +} + /// Lines appended under a byte cap: a line that would pass it is refused whole. struct Lines { out: String, @@ -846,9 +887,16 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning, present: &[(String, String)], return ("Nothing new was said.".into(), String::new(), String::new(), Overflow::default(), Vec::new()); } let (to_her, others): (Vec<&RoomLine>, Vec<&RoomLine>) = messages.into_iter().partition(|line| line.to_her); - let typed = |line: &RoomLine, body: &str| format!("- {} in {}: {body}", name_of(line, inputs), line.room); - // Heard lines are prose for the Persona: the speaker and the room from the mouth, then the escaped words. - let heard = |line: &RoomLine, body: &str| format!("{}, in {}: {body}", name_of(line, inputs), line.room); + let typed = |line: &RoomLine, body: &str| { + let (above, replying) = reply_parts(line, inputs); + format!("{above}- {}{replying} in {}: {body}", name_of(line, inputs), line.room) + }; + // Heard lines are prose for the Persona: the speaker and the room from the mouth, then the escaped words; a reply with + // the line it answers above it. + let heard = |line: &RoomLine, body: &str| { + let (above, replying) = reply_parts(line, inputs); + format!("{above}{}{replying}, in {}: {body}", name_of(line, inputs), line.room) + }; let ceiling = tuning.select.addressed_bytes; // One selection, rendered twice: the stimulus and the heard lines show the same messages to her and the same count. let addressed = said_to_her(&to_her, typed, ceiling); @@ -905,7 +953,7 @@ fn chat_stimulus(inputs: &Inputs, tuning: &Tuning, present: &[(String, String)], if !kept.is_empty() { earlier_block = format!("{heading}\n{}", kept.join("\n")); heard_text = format!("{earlier_block}\n{just}\n{heard_text}"); - earlier_bodies = earlier[dropped..].iter().filter(|(_, line)| line.sender != inputs.people.own_sender).map(|(_, line)| line.text.clone()).collect(); + earlier_bodies = earlier[dropped..].iter().filter(|(_, line)| line.sender != inputs.people.own_sender).flat_map(|(_, line)| shown_words(line, inputs.people.own_sender)).collect(); } } // The Persona's (map 16.21): the stream ends on the arrival, so what she reads next opens on it. Each person who @@ -961,7 +1009,8 @@ fn earlier_lines<'a>(inputs: &'a Inputs) -> Vec<(String, &'a RoomLine)> { .filter(|line| !new(line) && line.at_ms <= newest) .map(|line| { let name = if line.sender == inputs.people.own_sender && !own.is_empty() { clip(&one_line(own), NAME_BYTES) } else { name_of(line, inputs) }; - (format!("{name}, {} min ago: {}", minutes(inputs.now, line.at_ms), spoken(&line.text)), line) + let (above, replying) = reply_parts(line, inputs); + (format!("{above}{name}{replying}, {} min ago: {}", minutes(inputs.now, line.at_ms), spoken(&line.text)), line) }) .collect() } @@ -994,7 +1043,13 @@ fn beat_stimulus(inputs: &Inputs, tuning: &Tuning) -> String { let cap = tuning.select.stimulus_bytes; let mut out = Lines { out: beat_contacts(inputs, tuning), cap }; let her = forms(inputs.state); - let lines: Vec = beat_tail(inputs, tuning).iter().map(|line| format!("- {} in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), spoken(&clip(&line.text, tuning.select.item_bytes / 2)))).collect(); + let lines: Vec = beat_tail(inputs, tuning) + .iter() + .map(|line| { + let (above, replying) = reply_parts(line, inputs); + format!("{above}- {}{replying} in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), spoken(&clip(&line.text, tuning.select.item_bytes / 2))) + }) + .collect(); let section = newest_section(&format!("Lately in {} rooms:", her.poss), &lines, cap.saturating_sub(out.out.len() + 2)); if !section.is_empty() && out.push("") { out.push(§ion); @@ -1038,7 +1093,13 @@ fn beat_contacts(inputs: &Inputs, tuning: &Tuning) -> String { /// A beat's recent room lines as the Persona reads them, after the stream: the speaker, the room and when, then the words /// verbatim, newest kept within `heard_bytes`. fn beat_heard(inputs: &Inputs, tuning: &Tuning) -> String { - let lines: Vec = beat_tail(inputs, tuning).iter().map(|line| format!("{}, in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), spoken(&line.text))).collect(); + let lines: Vec = beat_tail(inputs, tuning) + .iter() + .map(|line| { + let (above, replying) = reply_parts(line, inputs); + format!("{above}{}{replying}, in {}, {} h ago: {}", name_of(line, inputs), line.room, hours(inputs.now, line.at_ms), spoken(&line.text)) + }) + .collect(); newest_that_fit(&lines, tuning.select.heard_bytes).0.join("\n") } @@ -1677,8 +1738,8 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { let her = forms(inputs.state); let mut context = Context::new(guards(tuning, her), her); let bodies: Vec = match inputs.kind { - TurnKind::Chat => inputs.room.messages.iter().map(|line| line.text.clone()).chain(earlier_bodies).collect(), - TurnKind::Beat => beat_tail(inputs, tuning).iter().map(|line| line.text.clone()).collect(), + TurnKind::Chat => inputs.room.messages.iter().flat_map(|line| shown_words(line, inputs.people.own_sender)).chain(earlier_bodies).collect(), + TurnKind::Beat => beat_tail(inputs, tuning).iter().flat_map(|line| shown_words(line, inputs.people.own_sender)).collect(), TurnKind::FollowUp | TurnKind::Reflection => Vec::new(), }; // What came of her last turn, first and under its own guard, so nothing else can crowd it out (map 16.20.5, I16.11). diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index d9d7eb8..d03c8e6 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -69,7 +69,7 @@ fn state() -> Value { fn message(room: &str, sender: &str, display: &str, text: &str, minutes_ago: i64, to_her: bool) -> RoomLine { let at = now() - chrono::Duration::minutes(minutes_ago); - RoomLine { room: room.into(), sender: sender.into(), display: display.into(), text: text.into(), at_ms: u64::try_from(at.timestamp_millis()).unwrap(), to_her } + RoomLine { room: room.into(), sender: sender.into(), display: display.into(), text: text.into(), at_ms: u64::try_from(at.timestamp_millis()).unwrap(), to_her, ..RoomLine::default() } } fn chat_room() -> Room { @@ -2212,6 +2212,39 @@ fn k19_the_conversation_on_screen_reaches_the_persona_and_the_interpreter_and_ne assert_eq!(packet_of(TurnKind::Chat, &state(), &chat_room(), &History::default(), 1).heard, "Libby, in #swarm:gamecult.org: \"Eureka, did you see the door fix?\"\nKiko, in #swarm:gamecult.org: \"She saw it. She hated it.\""); } +/// K20: another Mind's packet shows the relation. A line that replies to another renders with the line it answers above it, +/// by who said it and in the words the mouth quoted, and `{name}, replying to {other}` on her line, wherever she reads it: +/// under Earlier, under Just now and in the Interpreter's stimulus. The quoted words are words she was shown (`bodies`), and +/// the Projector sees none of them. +#[test] +fn k20_a_reply_shows_the_line_it_answers_above_it_in_another_minds_packet() { + const META: &str = "SEALION back at six, the render can wait."; + let replied = Some(Replied { sender: OPERATOR.into(), text: Some(META.into()) }); + let reply = |minutes, new| RoomLine { event: "$r".into(), reply: replied.clone(), ..message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", "This is what she said before she left.", minutes, new) }; + let quote = format!("> Meta, the operator: {}", serde_json::to_string(META).unwrap()); + // Just now, and in the Interpreter's stimulus. + let room = Room { messages: vec![reply(1, true)], tail: vec![message("#swarm:gamecult.org", OPERATOR, "Meta", META, 40, false)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let packet = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1); + assert!(packet.heard.ends_with(&format!("Just now: +{quote} +Libby, replying to Meta, the operator, in #swarm:gamecult.org: \"This is what she said before she left.\"")), "{}", packet.heard); + assert!(packet.stimulus.contains(&format!("{quote} +- Libby, replying to Meta, the operator, in #swarm:gamecult.org: ")), "{}", packet.stimulus); + assert!(packet.bodies.iter().any(|body| body == META), "the quoted words are words she was shown: {:?}", packet.bodies); + assert!(!narrate::projector_prompt(&packet, &Tuning::default()).contains("SEALION"), "the Projector never sees the words"); + // Earlier, from the tail. + let room = Room { messages: vec![message("#swarm:gamecult.org", "@kiko:gamecult.org", "Kiko", "Eureka?", 1, true)], tail: vec![reply(5, false)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let heard = packet_of(TurnKind::Chat, &state(), &room, &History::default(), 1).heard; + assert!(heard.starts_with(&format!("Earlier in #swarm:gamecult.org: +{quote} +Libby, replying to Meta, the operator, 5 min ago: ")), "{heard}"); + // A reply whose words the mouth did not quote says so; a line that replies to nothing renders as it did. + let unquoted = Room { messages: vec![RoomLine { reply: Some(Replied { sender: OPERATOR.into(), text: None }), ..reply(1, true) }], ..Room::default() }; + assert!(packet_of(TurnKind::Chat, &state(), &unquoted, &History::default(), 1).heard.starts_with("> Meta, the operator: (no words shown) +Libby, replying to Meta, the operator, in ")); + assert!(!packet_of(TurnKind::Chat, &state(), &chat_room(), &History::default(), 1).heard.contains("replying")); +} + /// Q-E unanswered, option A: an earlier line someone else wrote, said verbatim, is an echo, bare or behind a lead-in and /// with words of her own after it (Heimdall's live draft at 0cfc386, Soul's K19 pass). #[test]