From 3d442259b0a123d2bd71e94e51480e9f27bfe25e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 21:12:31 +0100 Subject: [PATCH] turn: take names each send's id and the Mouth's basis is per room (L1 findings mouth-key-second-source, mouth-send-index-unpinned, mouth-basis-one-room) The actuator's send takes the id take names, {key}:s{index}, so the key has one source; the Mouth holds the newest event of each room her input held and sends the basis of the room it speaks in. A two-line turn pins distinct request ids. Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/projection/simulate.rs | 2 +- .../src/projection/simulate/tests.rs | 2 +- crates/mind-body/src/projection/turn.rs | 30 +++++++++---------- crates/mind-body/src/projection/turn/tests.rs | 26 ++++++++++++++-- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/crates/mind-body/src/projection/simulate.rs b/crates/mind-body/src/projection/simulate.rs index ffc4a24..3c85320 100644 --- a/crates/mind-body/src/projection/simulate.rs +++ b/crates/mind-body/src/projection/simulate.rs @@ -190,7 +190,7 @@ struct SimRoom<'a> { } impl Actuator for SimRoom<'_> { - fn send(&self, room: &str, text: &str, in_reply_to: Option<&str>, mentions: &[String]) -> Sent { + fn send(&self, _id: &str, room: &str, text: &str, in_reply_to: Option<&str>, mentions: &[String]) -> Sent { let offset = self.sent.replace(self.sent.get() + 1); let mut lines = self.lines.borrow_mut(); let reply_to = match in_reply_to { diff --git a/crates/mind-body/src/projection/simulate/tests.rs b/crates/mind-body/src/projection/simulate/tests.rs index e0d0854..8849838 100644 --- a/crates/mind-body/src/projection/simulate/tests.rs +++ b/crates/mind-body/src/projection/simulate/tests.rs @@ -1136,7 +1136,7 @@ fn t_l1_sim_and_mouth_records_same_shape() { bail!("unused") } } - let mouth = Mouth { chat: &Chat1, persona: "eureka".into(), key: "r1-t1".into(), basis: None, sent: Cell::new(0) }; + let mouth = Mouth { chat: &Chat1, persona: "eureka".into(), seen: Default::default(), since_unix_ms: 0 }; let live = take(&organs, &request, &mouth, "r1-t1", fixture.now); let keys = |record: &Value| record.as_object().unwrap().keys().cloned().collect::>(); assert_eq!(keys(&simulated.record), keys(&live.record), "one record shape, simulated or live"); diff --git a/crates/mind-body/src/projection/turn.rs b/crates/mind-body/src/projection/turn.rs index 5938c65..03f3214 100644 --- a/crates/mind-body/src/projection/turn.rs +++ b/crates/mind-body/src/projection/turn.rs @@ -3,7 +3,7 @@ //! the turn the Minds take on chat. Her records are her trace: what she last did, what she showed lately, what she has //! said in this conversation and when she last woke are read back from them (`history`), and kept nowhere else. -use std::cell::Cell; +use std::collections::BTreeMap; use chrono::{DateTime, Utc}; use serde_json::{Value, json}; @@ -27,30 +27,30 @@ pub(crate) type Edited = Option<&'static str>; /// What a turn's decision reaches the world through. A refusal is an answer the record keeps, never a fault. pub(crate) trait Actuator { - /// Say `text` in `room`, replying to the line `in_reply_to` and waking `mentions`, where the actuator carries them. - fn send(&self, room: &str, text: &str, in_reply_to: Option<&str>, mentions: &[String]) -> Sent; + /// Say `text` in `room`, replying to the line `in_reply_to` and waking `mentions`, where the actuator carries them. `id` + /// names this send, `{key}:s{index}` of the turn's key and the line's place in what she decided: the one source of the + /// mouth's transaction id, so the same turn sent again is the same send and no two sends of a turn share one. + fn send(&self, id: &str, room: &str, text: &str, in_reply_to: Option<&str>, mentions: &[String]) -> Sent; /// Add `added` to her line `event`. fn edit(&self, event: &str, added: &str) -> Edited; } /// The mouth as the actuator. `chat_send_request.v2` carries no mentions and no reply link, so her line goes out plain and /// the receipt records the text that went, not the decision; the mouth has no edit, so an amendment is refused -/// `no_edit_at_the_mouth` and told to her as not amended. Each send is its own transaction, `{key}:s{index}`, so the -/// same turn sent again is the same send. -#[allow(dead_code, reason = "the live drain (map 16.36.5, cut L2) is its first caller")] +/// `no_edit_at_the_mouth` and told to her as not amended. Each send is its own transaction, the id `take` names. pub(crate) struct Mouth<'a> { pub chat: &'a dyn Chat, pub persona: String, - pub key: String, - /// The newest event of the room her input held, and when her process began. - pub basis: Option, - pub sent: Cell, + /// The newest event of each room her input held, by the room's alias; a room her input did not hold has none. + pub seen: BTreeMap, + /// When her process began. + pub since_unix_ms: u64, } impl Actuator for Mouth<'_> { - fn send(&self, room: &str, text: &str, _in_reply_to: Option<&str>, _mentions: &[String]) -> Sent { - let index = self.sent.replace(self.sent.get() + 1); - let request = ChatSendRequest { request_id: format!("{}:s{index}", self.key), persona: self.persona.clone(), room_alias: room.to_owned(), text: text.to_owned(), basis: self.basis.clone() }; + fn send(&self, id: &str, room: &str, text: &str, _in_reply_to: Option<&str>, _mentions: &[String]) -> Sent { + let basis = SendBasis { seen_event_id: self.seen.get(room).cloned(), since_unix_ms: self.since_unix_ms }; + let request = ChatSendRequest { request_id: id.to_owned(), persona: self.persona.clone(), room_alias: room.to_owned(), text: text.to_owned(), basis: Some(basis) }; match self.chat.send(&request) { Ok(receipt) => match (receipt.event_id, receipt.refusal) { (_, Some(refusal)) => Sent::Refused(refusal.name().to_owned()), @@ -86,12 +86,12 @@ pub(crate) fn take(organs: &Organs, request: &Request, actuator: &dyn Actuator, None => { // What she says is the Interpreter's decision alone. let (mut posted, mut refused_sends, mut edited) = (Vec::new(), Vec::new(), Vec::new()); - for decided in render["interpreter"]["said"].as_array().into_iter().flatten() { + for (index, decided) in render["interpreter"]["said"].as_array().into_iter().flatten().enumerate() { let room = decided["room"].as_str().unwrap_or(&here); let text = decided["text"].as_str().unwrap_or_default(); let mentions: Vec = decided["mentions"].as_array().into_iter().flatten().filter_map(Value::as_str).map(str::to_owned).collect(); let reply = decided["in_reply_to"]["event"].as_str(); - match actuator.send(room, text, reply, &mentions) { + match actuator.send(&format!("{key}:s{index}"), room, text, reply, &mentions) { Sent::Posted(event) => posted.push(json!({ "event": event, "room": room, "text": text })), Sent::Refused(refusal) => refused_sends.push(match reply { Some(event) => json!({ "room": room, "text": text, "in_reply_to": event, "refusal": refusal }), diff --git a/crates/mind-body/src/projection/turn/tests.rs b/crates/mind-body/src/projection/turn/tests.rs index b47e0db..f1b9c9e 100644 --- a/crates/mind-body/src/projection/turn/tests.rs +++ b/crates/mind-body/src/projection/turn/tests.rs @@ -50,13 +50,18 @@ impl Chat for FakeChat { } fn mouth(chat: &FakeChat) -> Mouth<'_> { - Mouth { chat, persona: "eureka".into(), key: "run-t1".into(), basis: None, sent: Cell::new(0) } + Mouth { chat, persona: "eureka".into(), seen: BTreeMap::new(), since_unix_ms: 7 } } /// One turn from the operator-warm fixture, she saying one line in reply, through `actuator`. fn warm_turn(actuator: &dyn Actuator) -> Taken { + warm_turn_saying("He smiles at the screen, then types. Missed you too, friend.", actuator) +} + +/// The same turn, her prose being `prose`. +fn warm_turn_saying(prose: &'static str, actuator: &dyn Actuator) -> Taken { let dir = tempfile::tempdir().unwrap(); - let model = Scripted::new(|_| "He smiles at the screen, then types. Missed you too, friend.".into()); + let model = Scripted::new(move |_| prose.into()); let fixture = room("operator-warm.toml"); let harness = harness(dir.path(), None, Some(&model)); let organs = Organs { persona: None, chat: Some(&fixture), addressing: fixture.addressee(), model: harness.model, domain: None, reads: None }; @@ -74,6 +79,23 @@ fn t_l1_mouth_sends_the_text_that_went_and_records_the_receipt() { assert_eq!(taken.record["posted"], json!([{ "event": "$sent-1", "room": "!dm-meta:gamecult.org", "text": "Missed you too, friend." }])); } +#[test] +fn t_l2_each_send_of_a_turn_has_its_own_id_and_its_own_rooms_basis() { + let chat = FakeChat::new(None); + let mouth = Mouth { seen: BTreeMap::from([("!dm-meta:gamecult.org".to_owned(), "$newest".to_owned())]), ..mouth(&chat) }; + let taken = warm_turn_saying("He types. First thing. Then more. Second, and different.", &mouth); + let sent = chat.sent.borrow(); + let ids: Vec<&str> = sent.iter().map(|request| request.request_id.as_str()).collect(); + assert_eq!(ids, ["run-t1:s0", "run-t1:s1"], "one id per said line, named by the turn's key and the line's place"); + assert_eq!(taken.record["posted"].as_array().unwrap().len(), 2); + let basis = |request: &ChatSendRequest| request.basis.clone().unwrap(); + assert_eq!(basis(&sent[0]), SendBasis { seen_event_id: Some("$newest".into()), since_unix_ms: 7 }, "the basis is the room's own newest event"); + let elsewhere = Mouth { seen: BTreeMap::from([("#other:gamecult.org".to_owned(), "$elsewhere".to_owned())]), ..mouth(&chat) }; + chat.sent.borrow_mut().clear(); + let _ = elsewhere.send("k:s0", "!dm-meta:gamecult.org", "hi", None, &[]); + assert_eq!(basis(&chat.sent.borrow()[0]), SendBasis { seen_event_id: None, since_unix_ms: 7 }, "another room's newest event is not this room's basis"); +} + #[test] fn t_l1_mouth_refusal_is_a_refused_send_and_no_posted_row() { let chat = FakeChat::new(Some(Refusal::RoomMoved));