diff --git a/crates/mind-body/src/chat_client.rs b/crates/mind-body/src/chat_client.rs index 704fb59..9c90d55 100644 --- a/crates/mind-body/src/chat_client.rs +++ b/crates/mind-body/src/chat_client.rs @@ -455,8 +455,10 @@ pub struct ChatPending { pub tail_gaps: Vec, } -/// One line of the room tail: `[room, event_id, at_unix_ms, sender, form, basis, body]`. `room` is the configured alias (the room id -/// for a DM); `basis` is what Matrix says on the event itself, none being unproven; `body` is untrusted text clipped by the mouth. +/// One line of the room tail: `[room, event_id, at_unix_ms, sender, form, basis, body, reply_to]`. `room` is the configured alias (the +/// room id for a DM); `basis` is what Matrix says on the event itself, none being unproven; `body` is untrusted text clipped by the +/// mouth; `reply_to` is the event the line replies to, quoted as a delivery's is (Bifrost B3, map 16.20.10 K20). A mouth that sends +/// seven fields sends no reply links, and every line reads as replying to nothing. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub struct TailLine { pub room: String, @@ -466,6 +468,8 @@ pub struct TailLine { pub form: Form, pub basis: Option, pub body: String, + #[serde(default, skip_serializing_if = "ReplyLink::sends_nothing")] + pub reply_to: ReplyLink, } #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/mind-body/src/chat_client/tests.rs b/crates/mind-body/src/chat_client/tests.rs index c064c7a..770c90d 100644 --- a/crates/mind-body/src/chat_client/tests.rs +++ b/crates/mind-body/src/chat_client/tests.rs @@ -129,12 +129,28 @@ fn the_pending_answer_decodes_with_the_tail_field_and_without_it() { golden(PENDING_UNPROVEN_NOTICE_TAILED_BYTES, &one(false, &[], None, Form::Notice, Some(vec![]))); let tailed: ChatPending = decode(PENDING_TAIL_BYTES).unwrap(); assert!(tailed.deliveries.is_empty()); - let line = |event: &str, at, form, basis, body: &str| TailLine { room: "#operator:gamecult.org".into(), event_id: event.into(), at_unix_ms: at, sender: "@meta:gamecult.org".into(), form, basis, body: body.into() }; + let line = |event: &str, at, form, basis, body: &str| TailLine { room: "#operator:gamecult.org".into(), event_id: event.into(), at_unix_ms: at, sender: "@meta:gamecult.org".into(), form, basis, body: body.into(), reply_to: ReplyLink::Absent }; assert_eq!(tailed.tail, Some(vec![line("$e0", 1_700_000_000_000, Form::Message, None, "earlier"), line("$e1", 1_700_000_001_000, Form::Notice, Some(Basis::Tag), "hello eureka")])); golden(PENDING_TAIL_BYTES, &tailed); assert_eq!(rmp_serde::from_slice::(PENDING_TAIL_BYTES).unwrap()[2][0].as_array().unwrap().len(), 7, "a tail line is seven fields"); } +/// A tail line may carry the event it replies to at index 7, quoted as a delivery's is (Bifrost B3, map 16.20.10 K20): a quote decodes, +/// a defective one is dropped without failing the poll, and a line without it replies to nothing and encodes as seven fields again. +#[test] +fn a_tail_line_carries_the_event_it_replies_to() { + let mut value: serde_json::Value = rmp_serde::from_slice(PENDING_TAIL_BYTES).unwrap(); + value[2][1].as_array_mut().unwrap().push(serde_json::json!(["$e0", "@meta:gamecult.org", "earlier"])); + value[2][0].as_array_mut().unwrap().push(serde_json::json!(["$q", "@a:b", 5])); + let tail = decode::(&rmp_serde::to_vec(&value).unwrap()).unwrap().tail.unwrap(); + assert_eq!(tail[1].reply_to, ReplyLink::Quote(ReplyTo { event_id: "$e0".into(), sender: "@meta:gamecult.org".into(), excerpt: Some("earlier".into()) })); + assert_eq!(tail[0].reply_to, ReplyLink::Dropped(ReplyViolation::ExcerptNotText), "a defective link drops that quote only"); + let plain: ChatPending = decode(PENDING_TAIL_BYTES).unwrap(); + assert!(plain.tail.unwrap().iter().all(|line| line.reply_to == ReplyLink::Absent), "seven fields reply to nothing"); + let reencoded: serde_json::Value = rmp_serde::from_slice(&encode(&decode::(&rmp_serde::to_vec(&value).unwrap()).unwrap()).unwrap()).unwrap(); + assert_eq!((reencoded[2][0].as_array().unwrap().len(), reencoded[2][1].as_array().unwrap().len()), (7, 8), "only a quote is sent"); +} + #[test] fn a_basis_or_form_this_side_does_not_know_does_not_decode() { let mut value: serde_json::Value = rmp_serde::from_slice(PENDING_TAG_BYTES).unwrap(); diff --git a/crates/mind-body/src/persona/tests.rs b/crates/mind-body/src/persona/tests.rs index c41d9f4..56795c3 100644 --- a/crates/mind-body/src/persona/tests.rs +++ b/crates/mind-body/src/persona/tests.rs @@ -59,7 +59,7 @@ const LIBBY: &str = "@libby:chat"; /// A line of a room the mouth holds, the `n`th, stamped so lines order by `n`. fn tl(room: &str, n: u64, sender: &str, body: &str) -> TailLine { - TailLine { room: room.into(), event_id: format!("$t{room}{n}"), at_unix_ms: 1_700_000_000_000 + n, sender: sender.into(), form: crate::chat_client::Form::Message, basis: None, body: body.into() } + TailLine { room: room.into(), event_id: format!("$t{room}{n}"), at_unix_ms: 1_700_000_000_000 + n, sender: sender.into(), form: crate::chat_client::Form::Message, basis: None, body: body.into(), reply_to: Default::default() } } fn no_one() -> Addressees { diff --git a/crates/mind-body/src/projection/harness.rs b/crates/mind-body/src/projection/harness.rs index a24f6b4..134631e 100644 --- a/crates/mind-body/src/projection/harness.rs +++ b/crates/mind-body/src/projection/harness.rs @@ -24,7 +24,7 @@ use serde_json::{Value, json}; use super::narrate::{Model, distinct_2}; use super::{MouthReader, Organs, PersonaSource, Request, Through, TurnKind, project}; use crate::address::{Addressee, localpart}; -use crate::chat_client::{Basis, ChatDelivery, ChatPending, Form, TailLine}; +use crate::chat_client::{Basis, ChatDelivery, ChatPending, Form, REPLY_EXCERPT_CHARS, ReplyLink, ReplyTo, TailLine}; use crate::heartbeat::{Addressing, HeartbeatView}; pub use super::simulate::simulate; @@ -174,6 +174,16 @@ impl Line { } } + /// The line it replies to, as the mouth quotes it (a `chat_pending.v4` delivery's `reply_to`, and a tail line's under + /// Bifrost B3): the event, its sender and its words up to the mouth's excerpt limit. Every Matrix client shows that + /// original, with its sender, above the reply (map 16.20.8 (D)); the simulated room shows it the same way. + fn reply_link(&self, lines: &[Line]) -> ReplyLink { + match self.reply_to.and_then(|at| lines.get(at).map(|replied| (at, replied))) { + Some((at, replied)) => ReplyLink::Quote(ReplyTo { event_id: format!("$fixture-{at}"), sender: replied.from.clone(), excerpt: Some(replied.says.chars().take(REPLY_EXCERPT_CHARS).collect()) }), + None => ReplyLink::Absent, + } + } + pub(crate) fn delivery(&self, index: usize, viewer: &str, lines: &[Line]) -> ChatDelivery { ChatDelivery { seq: index as u64 + 1, @@ -190,7 +200,7 @@ impl Line { image: None, basis: self.basis(viewer, lines), form: Form::Message, - reply_to: Default::default(), + reply_to: self.reply_link(lines), } } } @@ -204,7 +214,7 @@ pub(crate) fn pending_for(lines: &[Line], viewer: &str, rooms: &[String], new: i .iter() .enumerate() .filter(|(_, line)| hers(line)) - .map(|(at, line)| TailLine { room: line.room.clone(), event_id: format!("$fixture-{at}"), at_unix_ms: line.at_ms, sender: line.from.clone(), form: Form::Message, basis: line.basis(viewer, lines), body: line.says.clone() }) + .map(|(at, line)| TailLine { room: line.room.clone(), event_id: format!("$fixture-{at}"), at_unix_ms: line.at_ms, sender: line.from.clone(), form: Form::Message, basis: line.basis(viewer, lines), body: line.says.clone(), reply_to: line.reply_link(lines) }) .collect(); let tail = tail[tail.len().saturating_sub(TAIL_LINES)..].to_vec(); ChatPending { persona: localpart(viewer).to_owned(), deliveries, tail: Some(tail), tail_gaps: vec![] } diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs index e6fab8e..35cfafe 100644 --- a/crates/mind-body/src/projection/harness/tests.rs +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -136,6 +136,38 @@ says = "the kettle again" assert_eq!(for_her(&fixture), vec![true, true, false]); } +/// A reply records its relation (map 16.20.8 (D)): the delivery and the tail line both quote the line it replies to, with its +/// event, its sender and its words, as a Matrix client shows the original above the reply; a line that replies to nothing quotes +/// nothing. +#[test] +fn the_room_shows_the_line_a_reply_answers_above_it() { + let fixture = Fixture::parse( + r##" +her = "@eureka:gamecult.org" +kind = "chat" +now = "2026-11-20T15:00:00Z" +rooms = ["#minds:gamecult.org"] +[[message]] +from = "@meta:gamecult.org" +room = "#minds:gamecult.org" +ago = "1h" +says = "Back at six, tell Kiko the render can wait." +[[message]] +from = "@heimdall:gamecult.org" +room = "#minds:gamecult.org" +new = true +reply_to = 1 +says = "This is what she said before she left." +"##, + ) + .unwrap(); + let pending = MouthReader::pending(&fixture).unwrap(); + let quote = ReplyTo { event_id: "$fixture-0".into(), sender: "@meta:gamecult.org".into(), excerpt: Some("Back at six, tell Kiko the render can wait.".into()) }; + assert_eq!(pending.deliveries[0].reply_to, ReplyLink::Quote(quote.clone())); + let tail = pending.tail.unwrap(); + assert_eq!((&tail[0].reply_to, &tail[1].reply_to), (&ReplyLink::Absent, &ReplyLink::Quote(quote))); +} + #[test] fn a_fixture_that_is_not_a_room_is_refused_naming_what_is_wrong() { let head = "her = \"@eureka:gamecult.org\"\nkind = \"chat\"\nnow = \"2026-11-20T15:00:00Z\"\nrooms = [\"#a:x\"]\n"; diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index d082404..d9d7eb8 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -928,7 +928,7 @@ fn delivery_by(seq: u64, room: &str, sender: &str, display: &str, text: &str, me } fn mouth() -> ReadOnlyMouth { - let tail = vec![TailLine { room: "#swarm:gamecult.org".into(), event_id: "$t1".into(), at_unix_ms: u64::try_from((now() - chrono::Duration::hours(2)).timestamp_millis()).unwrap(), sender: "@kiko:gamecult.org".into(), form: Form::Message, basis: None, body: "pipeline again".into() }]; + let tail = vec![TailLine { room: "#swarm:gamecult.org".into(), event_id: "$t1".into(), at_unix_ms: u64::try_from((now() - chrono::Duration::hours(2)).timestamp_millis()).unwrap(), sender: "@kiko:gamecult.org".into(), form: Form::Message, basis: None, body: "pipeline again".into(), reply_to: Default::default() }]; let deliveries = vec![delivery(1, "#swarm:gamecult.org", "@libby:gamecult.org", "Libby", "Eureka, the door?", &[OWN]), delivery(2, "#general:gamecult.org", "@kiko:gamecult.org", "Kiko", "elsewhere", &[])]; ReadOnlyMouth { pending: ChatPending { persona: "eureka".into(), deliveries, tail: Some(tail), tail_gaps: vec![] }, reads: Cell::new(0) } } @@ -1275,7 +1275,7 @@ fn project_refuses_to_run_without_her_own_id() { #[test] fn the_room_filter_holds_on_the_tail() { - let tail = |room: &str, body: &str| TailLine { room: room.into(), event_id: format!("${body}"), at_unix_ms: u64::try_from((now() - chrono::Duration::hours(1)).timestamp_millis()).unwrap(), sender: "@kiko:gamecult.org".into(), form: Form::Message, basis: None, body: body.into() }; + let tail = |room: &str, body: &str| TailLine { room: room.into(), event_id: format!("${body}"), at_unix_ms: u64::try_from((now() - chrono::Duration::hours(1)).timestamp_millis()).unwrap(), sender: "@kiko:gamecult.org".into(), form: Form::Message, basis: None, body: body.into(), reply_to: Default::default() }; let report = project_over(vec![], vec![tail("#swarm:gamecult.org", "here-line"), tail("#general:gamecult.org", "there-line")], |request| Request { kind: TurnKind::Beat, room: Some("#swarm:gamecult.org".into()), ..request }); let stimulus = stimulus_of(&report); assert!(stimulus.contains("here-line") && !stimulus.contains("there-line"), "{stimulus}");