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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 01:07:48 +01:00
commit 417e287dad
3 changed files with 114 additions and 4 deletions

View file

@ -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());
}

View file

@ -310,9 +310,13 @@ fn drain<C: Chat>(instance: &Slug, start: &Start<C>) -> 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<C: Chat>(instance: &Slug, start: &Start<C>) -> 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<Waking> = 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();

View file

@ -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::<Vec<_>>(), ["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<Utc>, _: chrono::DateTime<Utc>) -> Result<crate::projection::DomainFacts> {
Ok(crate::projection::DomainFacts::default())
}
fn stewarded(&self) -> Result<Vec<String>> {
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<dyn HeartbeatView>, 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<dyn crate::projection::DomainSource>),
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<String> {
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<Option<crate::heartbeat::Addressing>> {
Ok(None)
}
fn woken_for(&self, _: &str) -> Result<Vec<String>> {
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");
}