diff --git a/crates/mind-body/src/heartbeat.rs b/crates/mind-body/src/heartbeat.rs index 13a5b78..01755e2 100644 --- a/crates/mind-body/src/heartbeat.rs +++ b/crates/mind-body/src/heartbeat.rs @@ -857,6 +857,14 @@ pub fn poll(organs: &Organs, config: &Config) -> Result { rows.push(row); } + // A claim whose turn did not finish its line is given back. A drain acks every line she decided and every line it set aside, + // so a line still pending in the chat of a Mind whose unit is not running is one her turn left: it failed, or died before it + // drained, or never started. Her key is dropped, so the line wakes her again by the ordinary rule and the open floor judges it + // afresh: a taken hello does not stay barred from every other Mind for the window. + state.handled_mentions.retain(|key| { + key.split_once(':').is_none_or(|(who, event)| !rows.iter().any(|row| row.slug().0 == who && row.idle() && row.pending.iter().any(|delivery| delivery.event_id == event))) + }); + let beat_s = config.beat_s; let mut launches: Vec = Vec::new(); let mut launched: Vec = Vec::new(); diff --git a/crates/mind-body/src/heartbeat/tests.rs b/crates/mind-body/src/heartbeat/tests.rs index 4c1d900..29ca4cc 100644 --- a/crates/mind-body/src/heartbeat/tests.rs +++ b/crates/mind-body/src/heartbeat/tests.rs @@ -270,6 +270,12 @@ impl Rig { self.launcher.kill_as(&Slug(NAMES[who].into()), RunTurn::PersonaTurn); } + /// Her turn finished: her unit ends and her drain acked what was pending, so her chat holds none of it. + fn finish_turn(&self, who: usize) { + self.end_unit(who); + self.chats[who].deliveries.borrow_mut().clear(); + } + /// Her unit is active. fn in_a_turn(&self, who: usize) { self.launcher.revive_as(&Slug(NAMES[who].into()), RunTurn::PersonaTurn); @@ -390,7 +396,7 @@ fn the_operator_naming_a_persona_wakes_her_at_once_off_the_beat_and_once_per_mes assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "the beat has just fallen, and the operator does not wait for the next"); assert_eq!(rig.store.state.borrow().as_ref().unwrap().last_beat_at, rig.now(), "an off-beat wake does not move the beat"); // Her turn ends without acking; the same message does not wake her again. - rig.end_unit(BOB); + rig.finish_turn(BOB); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }); assert_eq!(rig.launched(), vec![Slug("bob".into())]); // A tag in m.mentions counts as naming. @@ -405,7 +411,7 @@ fn another_mind_tagging_a_persona_wakes_her_once_off_the_beat_and_a_name_in_pros assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "a name in a Mind's prose wakes no one"); rig.say(BOB, delivery(2, "@ann:chat", "a word", &["@bob:chat"])); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "a Mind of the roster tagging her wakes her at once"); - rig.end_unit(BOB); + rig.finish_turn(BOB); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "the same message polled again wakes nobody"); } @@ -458,7 +464,7 @@ fn the_operator_naming_a_project_untagged_wakes_its_idle_steward_once_off_the_be steward(&rig, ANN, "GameCult/BadDaySimulator", &["BDS"]); rig.say(ANN, delivery(1, "@op:chat", "BDS is down", &[])); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); - rig.end_unit(ANN); + rig.finish_turn(ANN); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "the same message polled again wakes nobody"); rig.say(ANN, delivery(2, "@op:chat", "BDSX is down", &[])); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "a substring of a name is not the name"); @@ -835,17 +841,17 @@ mod wake_rule { assert_eq!(woken(&dm, Speaker::Member, &[]), Some(WakeCause::AddressedByHuman)); } - fn general(seq: u64, sender: &str, text: &str) -> ChatDelivery { + pub(super) fn general(seq: u64, sender: &str, text: &str) -> ChatDelivery { ChatDelivery { room_alias: "#general:chat".into(), at_unix_ms: now_ms() + 1000 * seq, ..delivery(seq, sender, text, &[]) } } /// The rig's clock, in the unit the lines carry: a line said just now is within the exchange window of it. - fn now_ms() -> u64 { + pub(super) fn now_ms() -> u64 { u64::try_from(at(1_000_000).timestamp_millis()).unwrap() } - fn said_in_general(rig: &Rig, line: ChatDelivery) { + pub(super) fn said_in_general(rig: &Rig, line: ChatDelivery) { for who in [ANN, BOB] { rig.say(who, line.clone()); } @@ -865,7 +871,7 @@ mod wake_rule { rig.turn(ANN, 5); said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "the longer waiter takes it"); - rig.end_unit(BOB); + rig.finish_turn(BOB); rig.advance(1); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "once per line: the Mind who took it is not woken again and no other is picked for it"); } @@ -901,7 +907,7 @@ mod wake_rule { let rig = quiet_rig(); said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); - rig.end_unit(ANN); + rig.finish_turn(ANN); rig.advance(60); let answered = vec![crate::chat_client::ContextLine { sender: "@ann:chat".into(), text: "welcome".into(), at_unix_ms: now_ms() + 1500 }]; said_in_general(&rig, ChatDelivery { context: answered, ..general(2, "@other:chat", "me too, hello") }); @@ -921,7 +927,7 @@ mod wake_rule { said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); said_in_general(&rig, general(2, "@other:chat", "hello, I am new too")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); - rig.end_unit(ANN); + rig.finish_turn(ANN); rig.advance(1); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "her one turn took both lines: Bob is not picked for the second"); let rig = quiet_rig(); @@ -936,7 +942,7 @@ mod wake_rule { said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "Ann wins the pick, her unit will not start, and Bob takes the line in the same poll"); assert!(rig.notes().iter().any(|note| note.contains("start failed")), "the refusal is still said"); - rig.end_unit(BOB); + rig.finish_turn(BOB); for poll in 0..10 { rig.advance(1); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "poll {poll}: Bob took it; Ann's broken unit is not tried for it again"); @@ -974,7 +980,7 @@ mod wake_rule { said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); assert_eq!(rig.store.state.borrow().as_ref().unwrap().handled_mentions.len(), MENTIONS_KEPT, "the handled keys keep their bound"); - rig.end_unit(ANN); + rig.finish_turn(ANN); rig.with_state(|state| state.handled_mentions.clear()); rig.advance(31 * 60); rig.beat_fell(); @@ -1089,3 +1095,40 @@ mod wake_rule { assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "the poll reads the same room as hers"); } } + +/// Soul R21 p1, pinned: a line the poll woke her for whose turn then failed stays pending, and the next poll wakes her for it again; it +/// does not wait for an unrelated start. Once her turn finishes the line (the drain acked it) she is not woken for it again. +#[test] +fn a_line_whose_turn_failed_wakes_her_again_on_the_next_poll_and_a_finished_one_does_not() { + let rig = quiet_rig(); + rig.say(ANN, delivery(1, "@op:chat", "Ann, please", &[])); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); + rig.in_a_turn(ANN); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "her unit is running: the claim stands"); + rig.end_unit(ANN); + rig.advance(120); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }, "her turn left the line pending: she is woken for it again"); + rig.finish_turn(ANN); + rig.advance(120); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "the drain acked it: once per message holds"); +} + +/// A crash between the save of the claim and the start of her unit leaves a key and no turn. +#[test] +fn a_claim_saved_before_a_start_that_never_happened_does_not_strand_the_line() { + let rig = quiet_rig(); + rig.say(ANN, delivery(1, "@op:chat", "Ann, please", &[])); + rig.with_state(|state| state.handled_mentions.push("ann:$e1".into())); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); +} + +#[test] +fn a_hello_whose_takers_turn_failed_is_not_barred_from_the_other_minds() { + let rig = quiet_rig(); + wake_rule::said_in_general(&rig, wake_rule::general(1, "@newcomer:chat", "hello everyone!")); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }); + rig.end_unit(ANN); + rig.advance(60); + *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"); +}