From 417e287dadb54f9ab8d387ca4ab294680a0ec0d0 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 01:07:48 +0100 Subject: [PATCH] persona drain: unread stewardships fail the start; a line the heartbeat woke her for is waking; the live state view and its failure are pinned Soul R21 p2 is the test. Kills mutants m08 (FileStore::woken_for none) and m11 (unread heartbeat state read as no claims). Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/heartbeat/tests.rs | 20 +++++ crates/mind-body/src/persona.rs | 14 +++- crates/mind-body/src/persona/drain_tests.rs | 84 +++++++++++++++++++++ 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/crates/mind-body/src/heartbeat/tests.rs b/crates/mind-body/src/heartbeat/tests.rs index 29ca4cc..e5f8779 100644 --- a/crates/mind-body/src/heartbeat/tests.rs +++ b/crates/mind-body/src/heartbeat/tests.rs @@ -1132,3 +1132,23 @@ fn a_hello_whose_takers_turn_failed_is_not_barred_from_the_other_minds() { *rig.launcher.fail_for.borrow_mut() = Some(Slug("ann".into())); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "Ann's turn failed and her unit will not start: Bob takes the hello"); } + +/// The binding the live unit reads: the file store gives back the lines the poll recorded for her, and only hers. +#[test] +fn the_file_store_view_reads_the_lines_the_poll_recorded_for_her() { + let dir = TempDir::new().unwrap(); + let store = FileStore::new(dir.path().join("heartbeat.cc")); + assert!(HeartbeatView::woken_for(&store, "ann").unwrap().is_empty(), "no state yet: no claims"); + store.save(&HeartbeatState { last_beat_at: at(0), handled_mentions: vec!["ann:$e1".into(), "bob:$e2".into(), "ann:$e3".into()], last_turn_at: BTreeMap::new() }).unwrap(); + assert_eq!(HeartbeatView::woken_for(&store, "ann").unwrap(), ["$e1", "$e3"]); + assert_eq!(HeartbeatView::woken_for(&store, "bob").unwrap(), ["$e2"]); +} + +/// An unreadable state is an error to the drain, never no claims. +#[test] +fn the_file_store_view_fails_on_an_unreadable_state() { + let dir = TempDir::new().unwrap(); + let path = dir.path().join("heartbeat.cc"); + std::fs::write(&path, b"not a cultcache file").unwrap(); + assert!(HeartbeatView::woken_for(&FileStore::new(path), "ann").is_err()); +} diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index 77d621b..4e9ded1 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -310,9 +310,13 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { }; let mut deliveries: Vec<&ChatDelivery> = pending.deliveries.iter().collect(); deliveries.sort_by_key(|delivery| delivery.seq); - // Her rooms and her addressee, as the heartbeat reads them: the rooms named for the repos she stewards (a failed read - // leaves her none), and the heartbeat's table row for her. - let repos = start.domain.as_ref().and_then(|domain| domain.stewarded().ok()).unwrap_or_default(); + // Her rooms and her addressee, as the heartbeat reads them: the rooms named for the repos she stewards, and the heartbeat's table + // row for her. Stewardships that cannot be read are no rooms she has none of: the start fails, and no line is answered as no + // one's or acked past on that guess. + let repos = match start.domain.as_ref().map(|domain| domain.stewarded()).transpose() { + Ok(repos) => repos.unwrap_or_default(), + Err(fault) => return failed("her stewardships were not read", fault), + }; 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, _) = heard_as(start.addressing.as_deref(), &config.own_sender, &public_name, began); @@ -326,10 +330,12 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { for (delivery, facts) in deliveries.iter().zip(&mut facts) { facts.open_floor &= claimed.contains(&delivery.event_id); } + // A line the heartbeat woke her for is waking whatever this start would have derived: the poll's claim is the one derivation, and a + // line it keeps waking her for that this start did not answer would be a loop. let waking_all: Vec = deliveries .iter() .zip(&facts) - .filter(|(delivery, facts)| wakes(&her, delivery, speaker(config, delivery), **facts).is_some()) + .filter(|(delivery, facts)| claimed.contains(&delivery.event_id) || wakes(&her, delivery, speaker(config, delivery), **facts).is_some()) .map(|(delivery, _)| Waking { seq: delivery.seq, room: delivery.room_alias.as_str(), event: delivery.event_id.as_str(), sender: if delivery.sender_display.is_empty() { delivery.sender.as_str() } else { delivery.sender_display.as_str() }, text: delivery.text.as_str() }) .collect(); let woken = !waking_all.is_empty(); diff --git a/crates/mind-body/src/persona/drain_tests.rs b/crates/mind-body/src/persona/drain_tests.rs index 99fbd3a..b24381e 100644 --- a/crates/mind-body/src/persona/drain_tests.rs +++ b/crates/mind-body/src/persona/drain_tests.rs @@ -738,3 +738,87 @@ fn t_l2_the_open_floor_is_hers_only_on_the_lines_the_heartbeat_woke_her_for() { assert_eq!(rig.run_woken(scripted(false), &["$e1"]), Outcome::Drained { turns: 1, acked_through: Some(1), put_refused: 0 }); assert_eq!(rig.mouth.sent.borrow().iter().map(|request| request.request_id.clone()).collect::>(), ["l$e1:s0"], "the heartbeat woke her for the hello: she answers it"); } + +/// Her stewardships, as the drain reads them: read, or the mind did not answer. +struct Stewards(bool); + +impl crate::projection::DomainSource for Stewards { + fn read(&self, _: chrono::DateTime, _: chrono::DateTime) -> Result { + Ok(crate::projection::DomainFacts::default()) + } + fn stewarded(&self) -> Result> { + if self.0 { bail!("the mind did not answer") } else { Ok(vec!["GameCult/Huginn".into()]) } + } +} + +/// A start whose heartbeat view is `view` and whose stewardships are `down` (unread) or read. +fn run_with(rig: &Rig, view: Box, down: bool) -> Outcome { + let ticks = rig.ticks.clone(); + let start = || { + Ok(Start { + config: Config { rooms: vec![MINDS.into()], operators: vec![META.into()], minds: vec!["@eureka:gamecult.org".into(), LIBBY.into()], own_sender: "@eureka:gamecult.org".into(), model: "test-model".into() }, + persona: Box::new(rig.store.clone()), + chat: rig.mouth.clone(), + trace: Box::new(rig.trace.clone()), + addressing: Some(view), + model: Box::new(scripted(false)), + domain: Some(Box::new(Stewards(down)) as Box), + reads: None, + tuning: rig.dir.path().join("persona-tuning.toml"), + clock: Box::new(move || { + ticks.set(ticks.get() + 1); + Utc.timestamp_millis_opt(i64::try_from(BASE_MS).unwrap()).unwrap() + Duration::seconds(ticks.get()) + }), + }) + }; + carry(&FileSource::new(rig.dir.path().join("control.cc")), &FileSource::new(rig.dir.path().join("swarm.cc")), &Slug("eureka".into()), start) +} + +fn sent_ids(rig: &Rig) -> Vec { + rig.mouth.sent.borrow().iter().map(|request| request.request_id.clone()).collect() +} + +/// Soul R21 p2: a member's untagged line in her own room, the floor closed by a Mind's line just before it; the heartbeat woke her +/// for it. With her stewardships read she answers it under its id; with them unread the start fails, and the line is neither taken +/// as a beat nor acked past as no one's. +#[test] +fn t_l2_an_unread_stewardship_fails_the_start_and_does_not_turn_her_own_room_line_into_a_beat() { + const OWN: &str = "#huginn:gamecult.org"; + let lines = || vec![line(1, OWN, LIBBY, "the build went red on main"), line(2, OWN, "@deru:gamecult.org", "is anyone looking at the red build?")]; + let up = Rig::new(lines()); + run_with(&up, Box::new(Woken(vec!["$e2".into()])), false); + assert_eq!(sent_ids(&up), ["l$e2:s0"], "read: she answers the line the heartbeat woke her for"); + let down = Rig::new(lines()); + assert!(matches!(run_with(&down, Box::new(Woken(vec!["$e2".into()])), true), Outcome::Failed(why) if why.contains("stewardships were not read"))); + assert!(sent_ids(&down).is_empty() && down.held_seqs() == [1, 2] && down.mouth.acks.borrow().is_empty(), "unread: nothing sent, nothing acked as no one's"); +} + +/// A line the heartbeat woke her for is the line this start answers, whatever the start derives for itself: here her stewardships +/// are none and the line is a member's in a room that is not hers, which no derived rule wakes her for. +#[test] +fn t_l2_a_line_the_heartbeat_woke_her_for_is_answered_by_the_start_it_woke() { + let lines = || vec![line(1, MINDS, "@deru:gamecult.org", "does anyone know how the ledger ages?")]; + let derived = Rig::new(lines()); + run_with(&derived, Box::new(Woken(vec![])), false); + assert!(sent_ids(&derived).iter().all(|id| id.starts_with('b')), "derived alone, it is not hers: {:?}", sent_ids(&derived)); + let woken = Rig::new(lines()); + run_with(&woken, Box::new(Woken(vec!["$e1".into()])), false); + assert_eq!(sent_ids(&woken), ["l$e1:s0"], "claimed, she answers it under its id"); +} + +/// A heartbeat state that cannot be read fails the start: her claimed lines are not guessed to be none. +#[test] +fn t_l2_an_unread_heartbeat_state_fails_the_start() { + struct Unread; + impl HeartbeatView for Unread { + fn addressing(&self) -> Result> { + Ok(None) + } + fn woken_for(&self, _: &str) -> Result> { + bail!("the state file is unreadable") + } + } + let rig = Rig::new(vec![line(1, &room(1), META, "hello")]); + assert!(matches!(run_with(&rig, Box::new(Unread), false), Outcome::Failed(why) if why.contains("heartbeat state was not read"))); + assert!(sent_ids(&rig).is_empty() && rig.held_seqs() == [1] && rig.mouth.acks.borrow().is_empty(), "nothing sent or acked"); +}