From a7f8d9d2e95a07886a7fac46300f74d936f448bd Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 21:15:05 +0100 Subject: [PATCH] persona: ack after each turn, so a start killed between turns does not replay an answered message; fix compile Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/lib.rs | 2 +- crates/mind-body/src/persona.rs | 30 ++++++++++--------- crates/mind-body/src/persona/drain_tests.rs | 24 ++++++++++++--- crates/mind-body/src/projection/turn/tests.rs | 4 +-- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/crates/mind-body/src/lib.rs b/crates/mind-body/src/lib.rs index 1fb7441..f922800 100644 --- a/crates/mind-body/src/lib.rs +++ b/crates/mind-body/src/lib.rs @@ -2,7 +2,7 @@ //! //! Today that is `control`, the operator's brakes, and `cli`, the operator's //! hand on them. The brakes are the operator's consent, not her body: the store -//! is root-owned and no organ of hers writes it. +//! is root-owned and nothing of hers writes it. //! //! `launch` holds her units under systemd and the reads of the runs a Self run //! holds; `queue` is her port to her mind and what an open question holds. diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index a331006..0e15fa8 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -5,14 +5,14 @@ //! touched. A released one reads her persona state and her pending deliveries once, takes one turn in each room that //! holds a waking line (`heartbeat::wakes`, the one rule), oldest waking line first, at most `TURNS_PER_START` of them //! (a start the beat woke, with no waking line, takes one turn in the room of the newest pending line, else her first -//! room), and acks once. Each turn is `projection::take`, the simulator's turn: the Interpreter's decision through the +//! room), and acks after each turn. Each turn is `projection::take`, the simulator's turn: the Interpreter's decision through the //! mouth, the commit's application of her proposals, one put of her state, one record appended to her trace. //! //! The put is `persona_put` under the `updatedAt` she was read at. A stale one (Self or Life put between) reads her //! again, applies the same record again and puts once more; a second refusal is `put_refused` in the record and on //! stderr, and her lines have already gone out. The record is appended after the put, refused or not. //! -//! The ack rule: after the turns, ack through the highest seq below the first waking delivery whose room's turn did not +//! The ack rule: after each turn (the unit has a runtime bound, and a start killed between turns must not replay an answered message), ack through the highest seq below the first waking delivery whose room's turn did not //! record a decision (it failed, or was not taken); every line below it is acked, the ones that woke no one as ambient. //! A failed turn leaves its room's waking lines pending, so her next wake shows them again. The heartbeat never acks. //! @@ -35,7 +35,7 @@ use crate::address::{Addressee, addresses, is_for, localpart}; use crate::chat_client::{Basis, Chat, ChatDelivery, TailLine}; use crate::control::{ControlSource, Effective, HeldReason, derive}; use crate::heartbeat::{HeartbeatView, Speaker, own_rooms, pending_facts, wakes}; -use crate::projection::{DomainSource, Model, Mouth, Organs, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, history, own_addressee, take}; +use crate::projection::{DomainSource, Model, Mouth, Organs, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, history, own_addressee as heard_as, take}; /// The admitting agent's name on what her Persona admitted; `self_run::inbox` still reads her briefs by it. pub const AGENT: &str = "mind-persona"; @@ -274,7 +274,7 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { let repos = start.domain.as_ref().and_then(|domain| domain.stewarded().ok()).unwrap_or_default(); let own = own_rooms(repos.iter().map(String::as_str), deliveries.iter().map(|delivery| delivery.room_alias.as_str())); let public_name = state.get("publicName").and_then(Value::as_str).unwrap_or_default().to_owned(); - let (her, _) = own_addressee(start.addressing.as_deref(), &config.own_sender, &public_name, began); + let (her, _) = heard_as(start.addressing.as_deref(), &config.own_sender, &public_name, began); let facts = pending_facts(&deliveries, &config.own_sender, &config.minds, &own); let waking: Vec<(u64, &str)> = deliveries .iter() @@ -306,6 +306,7 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { let mouth = Mouth { chat: &start.chat, persona: instance.0.clone(), seen, since_unix_ms: began_ms }; let organs = Organs { persona: None, chat: Some(&start.chat), addressing: start.addressing.as_deref(), model: Some(start.model.as_ref()), domain: start.domain.as_deref(), reads: start.reads.as_deref() }; let mut taken: Vec = Vec::new(); + let mut acked: Option = None; for (index, room) in rooms.iter().enumerate() { let key = format!("{began_ms}-t{index}"); let now = (start.clock)(); @@ -351,17 +352,18 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { eprintln!("mind-persona: the record of turn {key} was not appended: {fault:#}"); } taken.push(Turn { room: (*room).to_owned(), decided: turn.record.get("failed").is_none() && recorded.is_ok(), put_refused }); - } - // The ack: through the highest seq below the first waking line whose room has no decision. - let blocked = waking.iter().find(|(_, room)| !taken.iter().any(|turn| turn.decided && turn.room == *room)).map(|(seq, _)| *seq); - let acked_through = deliveries.iter().map(|delivery| delivery.seq).filter(|seq| blocked.is_none_or(|limit| *seq < limit)).max().filter(|seq| match start.chat.ack(*seq) { - Ok(()) => true, - Err(fault) => { - eprintln!("mind-persona: the ack through {seq} failed: {fault:#}"); - false + // The ack follows each turn's commit, so a start killed between turns does not replay a message she answered: through + // the highest seq below the first waking line whose room has no decision yet. + let blocked = waking.iter().find(|(_, room)| !taken.iter().any(|turn| turn.decided && turn.room == *room)).map(|(seq, _)| *seq); + let through = deliveries.iter().map(|delivery| delivery.seq).filter(|seq| blocked.is_none_or(|limit| *seq < limit)).max().filter(|seq| acked.is_none_or(|held| *seq > held)); + if let Some(seq) = through { + match start.chat.ack(seq) { + Ok(()) => acked = Some(seq), + Err(fault) => eprintln!("mind-persona: the ack through {seq} failed: {fault:#}"), + } } - }); - Outcome::Drained { turns: taken.len(), acked_through, put_refused: taken.iter().filter(|turn| turn.put_refused).count() } + } + Outcome::Drained { turns: taken.len(), acked_through: acked, put_refused: taken.iter().filter(|turn| turn.put_refused).count() } } /// Who sent a delivery, as the wake rule sees them. diff --git a/crates/mind-body/src/persona/drain_tests.rs b/crates/mind-body/src/persona/drain_tests.rs index 41217da..da63b18 100644 --- a/crates/mind-body/src/persona/drain_tests.rs +++ b/crates/mind-body/src/persona/drain_tests.rs @@ -214,11 +214,10 @@ impl Rig { fn t_l2_drain_acks_after_decision() { let rig = Rig::new(vec![line(1, &room(1), META, "morning"), line(2, &room(2), META, "evening"), line(3, &room(1), META, "again")]); assert_eq!(rig.run(scripted(false)), Outcome::Drained { turns: 2, acked_through: Some(3), put_refused: 0 }); - assert_eq!(*rig.mouth.acks.borrow(), [3], "one ack through the highest pending seq"); + assert_eq!(*rig.mouth.acks.borrow(), [1, 3], "each ack stops below the first waking line of a room not yet decided"); let log = rig.log(); - assert_eq!(log.last().map(String::as_str), Some("ack"), "the ack comes last: {log:?}"); - assert_eq!(log.iter().filter(|event| *event == "trace").count(), 2, "{log:?}"); - assert!(log.iter().position(|event| event == "ack") > log.iter().rposition(|event| event == "trace"), "after both trace appends: {log:?}"); + let events: Vec<&str> = log.iter().map(String::as_str).filter(|event| *event != "put").collect(); + assert_eq!(events, ["send", "trace", "ack", "send", "trace", "ack"], "each ack follows its turn's trace append: {log:?}"); let ids: Vec = rig.mouth.sent.borrow().iter().map(|request| request.request_id.clone()).collect(); assert_eq!(ids.len(), 2); assert!(ids[0].ends_with("-t0:s0") && ids[1].ends_with("-t1:s0"), "one key per turn, one id per send: {ids:?}"); @@ -227,6 +226,23 @@ fn t_l2_drain_acks_after_decision() { assert!(rig.held_seqs().is_empty()); } +/// The unit has a runtime bound: a start killed between turn 1 and turn 2 must not replay turn 1's message. +#[test] +fn t_l2_a_start_killed_between_turns_does_not_replay_the_answered_message() { + struct Dies(Scripted); + impl Model for Dies { + fn ask(&self, request_id: &str, model: &str, prompt: &str, schema: Option<&str>) -> std::result::Result { + assert!(!prompt.contains("KILLME"), "killed"); + self.0.ask(request_id, model, prompt, schema) + } + } + let rig = Rig::new(vec![line(1, &room(1), META, "alpha"), line(2, &room(2), META, "KILLME bravo")]); + let died = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| rig.run(Dies(scripted(false))))); + assert!(died.is_err(), "the start was killed in turn 2"); + assert_eq!(rig.mouth.sent.borrow().len(), 1, "turn 1 answered"); + assert_eq!(rig.held_seqs(), [2], "turn 1's message is acked and not replayed; the killed turn's stays"); +} + #[test] fn t_l2_failed_room_stays_pending() { let rig = Rig::new(vec![line(1, &room(1), META, "alpha is fine"), line(2, &room(2), META, "FAILME bravo"), line(3, &room(1), META, "alpha again")]); diff --git a/crates/mind-body/src/projection/turn/tests.rs b/crates/mind-body/src/projection/turn/tests.rs index f1b9c9e..cd11f4f 100644 --- a/crates/mind-body/src/projection/turn/tests.rs +++ b/crates/mind-body/src/projection/turn/tests.rs @@ -82,8 +82,8 @@ fn t_l1_mouth_sends_the_text_that_went_and_records_the_receipt() { #[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 mouth_in_dm = 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_in_dm); 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");