diff --git a/crates/mind-body/src/projection/simulate.rs b/crates/mind-body/src/projection/simulate.rs index f57d0db..57e15ea 100644 --- a/crates/mind-body/src/projection/simulate.rs +++ b/crates/mind-body/src/projection/simulate.rs @@ -4,8 +4,9 @@ //! //! Who wakes is the heartbeat's: `heartbeat::wakes` under the plan's `WakePolicy`, over each persona's addressee as the //! heartbeat's addressing table holds it (her stewarded projects' names included) when the plan names the table. The -//! beat is the heartbeat's too: when it falls, one Persona wakes, the winner of `tempo::pick`. Those woken in one round -//! go in an order drawn from the seed, each against the room as it stands at her turn. +//! beat is the heartbeat's too: when it falls, one Persona wakes, the winner of `tempo::pick`. A persona woken in several +//! rooms in one round takes one turn in each. The turns of a round go in an order drawn from the seed, each against the +//! room as it stands at that turn. //! //! Her state does not move during a run: the Interpreter (C3b) and the commit (C3c) are not built. `state_after_turn` //! is the seam where they will. An explicit addressee (the Interpreter naming whom she speaks to) cannot be simulated @@ -205,8 +206,10 @@ fn in_exchange(lines: &[Line], at: usize, her: &str, window: Duration) -> bool { lines[..at].iter().any(|earlier| earlier.room == line.room && earlier.from == her && earlier.at_ms >= since) } +/// Her wakes: one per room a new line woke her in, in the order of each room's first waking line. A room's turn takes +/// the strongest cause among its lines, so no room that woke her, a direct message least of all, is passed over. #[allow(clippy::too_many_arguments, reason = "one wake decision's inputs, each a plan or room fact")] -fn woken(lines: &[Line], runs: &[usize], seen: usize, member: &Member, operators: &[String], cast: &[String], policy: WakePolicy, gap: Duration) -> Option { +fn woken(lines: &[Line], runs: &[usize], seen: usize, member: &Member, operators: &[String], cast: &[String], policy: WakePolicy, gap: Duration) -> Vec { let mut found: Vec<(usize, WakeCause)> = Vec::new(); for (at, line) in lines.iter().enumerate().skip(seen) { if !line.visible_to(&member.entry.id, &member.rooms) { @@ -218,16 +221,28 @@ fn woken(lines: &[Line], runs: &[usize], seen: usize, member: &Member, operators found.push((at, cause)); } } - let cause = found.iter().map(|(_, cause)| *cause).min()?; - let line = found.iter().rev().find(|(_, found)| *found == cause).map_or(0, |(at, _)| *at); - if cause != WakeCause::AddressedByMind { - return Some(Woken { cause, line, how: None, by: vec![] }); + let mut rooms: Vec<&str> = Vec::new(); + for (at, _) in &found { + if !rooms.contains(&lines[*at].room.as_str()) { + rooms.push(&lines[*at].room); + } } - let addressing: Vec = found.iter().filter(|(_, cause)| *cause == WakeCause::AddressedByMind).map(|(at, _)| *at).collect(); - let rank = |how: &str| ["mention or reply", "id as text", "name in text"].iter().position(|one| *one == how).unwrap_or(3); - let how = addressing.iter().map(|at| how_addressed(&lines[*at], *at, member, lines)).min_by_key(|how| rank(how)); - let by = addressing.iter().map(|at| json!({ "from": lines[*at].from, "how": how_addressed(&lines[*at], *at, member, lines), "says": lines[*at].says })).collect(); - Some(Woken { cause, line, how, by }) + rooms + .into_iter() + .filter_map(|room| { + let found: Vec<(usize, WakeCause)> = found.iter().copied().filter(|(at, _)| lines[*at].room == room).collect(); + let cause = found.iter().map(|(_, cause)| *cause).min()?; + let line = found.iter().rev().find(|(_, found)| *found == cause).map_or(0, |(at, _)| *at); + if cause != WakeCause::AddressedByMind { + return Some(Woken { cause, line, how: None, by: vec![] }); + } + let addressing: Vec = found.iter().filter(|(_, cause)| *cause == WakeCause::AddressedByMind).map(|(at, _)| *at).collect(); + let rank = |how: &str| ["mention or reply", "id as text", "name in text"].iter().position(|one| *one == how).unwrap_or(3); + let how = addressing.iter().map(|at| how_addressed(&lines[*at], *at, member, lines)).min_by_key(|how| rank(how)); + let by = addressing.iter().map(|at| json!({ "from": lines[*at].from, "how": how_addressed(&lines[*at], *at, member, lines), "says": lines[*at].says })).collect(); + Some(Woken { cause, line, how, by }) + }) + .collect() } /// The wire spelling of a cause, and of the beat. @@ -333,7 +348,9 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) for round in 1..=plan.rounds { scripted(round, &mut lines, &mut rounds_of, &mut causes)?; let round_at = at_round(round); - let mut woke: Vec<(usize, Option)> = members.iter().enumerate().filter_map(|(at, member)| woken(&lines, &mind_runs(&lines, &cast, member), seen[at], member, &plan.operators, &cast, plan.wake, Duration::minutes(plan.exchange_minutes)).map(|why| (at, Some(why)))).collect(); + // One turn per room that woke her, each in its room; all of hers this round read from where the round found her. + let round_seen = seen.clone(); + let mut woke: Vec<(usize, Option)> = members.iter().enumerate().flat_map(|(at, member)| woken(&lines, &mind_runs(&lines, &cast, member), seen[at], member, &plan.operators, &cast, plan.wake, Duration::minutes(plan.exchange_minutes)).into_iter().map(move |why| (at, Some(why)))).collect(); // The beat, as the heartbeat's: when it has fallen, one more wake for the winner of `tempo::pick` among those // not already woken. if plan.beat_s > 0 && (round_at - last_beat).num_seconds() >= i64::from(plan.beat_s) { @@ -358,13 +375,13 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) counter += 1; let member = &members[at]; let id = member.entry.id.clone(); - let fresh: Vec<&Line> = lines[seen[at]..].iter().filter(|line| line.from != id && line.visible_to(&id, &member.rooms)).collect(); + let fresh: Vec<&Line> = lines[round_seen[at]..].iter().filter(|line| line.from != id && line.visible_to(&id, &member.rooms)).collect(); // The turn is in the room of the line that woke her; a beat's, in the room of the newest line she has not seen. let woke_on = why.as_ref().map(|why| &lines[why.line]).or(fresh.last().copied()); let here = woke_on.map_or_else(|| member.rooms[0].clone(), |line| line.room.clone()); // Woken by a direct message, she answers in it: to its sender alone. let dm_to = woke_on.filter(|line| line.dm_to.as_deref() == Some(id.as_str())).map(|line| line.from.clone()); - let kind = if fresh.is_empty() { TurnKind::Beat } else { TurnKind::Chat }; + let kind = if fresh.iter().any(|line| line.room == here) { TurnKind::Chat } else { TurnKind::Beat }; let now = round_at + Duration::seconds(30 + counter as i64); let request = Request { instance: localpart(&id).to_owned(), @@ -383,7 +400,7 @@ fn run(harness: &Harness, plan: &SimFile, members: &mut [Member], repeat: usize) recent: recent[at].clone(), own_rooms: member.entry.own_rooms.clone(), }; - let view = View { lines: &lines, viewer: &id, rooms: &member.rooms, seen: seen[at] }; + let view = View { lines: &lines, viewer: &id, rooms: &member.rooms, seen: round_seen[at] }; let names = Names(&member.addressee); let organs = Organs { persona: None, chat: Some(&view), addressing: Some(&names), model: harness.model }; let report = project(&organs, &request, now).unwrap_or_else(|fault| json!({ "error": format!("{fault:#}") })); diff --git a/crates/mind-body/src/projection/simulate/tests.rs b/crates/mind-body/src/projection/simulate/tests.rs index d735f98..fcbe149 100644 --- a/crates/mind-body/src/projection/simulate/tests.rs +++ b/crates/mind-body/src/projection/simulate/tests.rs @@ -364,3 +364,23 @@ fn the_turn_is_in_the_room_of_the_line_that_woke_her() { let (_, run) = run_plan(&path, &silent()); assert_eq!(run["turns"][0]["room"], "#tobiah:gamecult.org", "{}", run["turns"]); } + +#[test] +fn soul_r7_woken_in_a_direct_message_and_a_room_at_once_she_takes_a_turn_in_each() { + let dir = tempfile::tempdir().unwrap(); + let path = plan(dir.path(), "\"ruled\"", 1, ""); + let line = |room: &str, dm: bool, says: &str| format!("[[message]]\nround = 1\nfrom = \"@meta:gamecult.org\"\nname = \"Meta\"\nroom = \"{room}\"\n{}says = \"{says}\"\n", if dm { "dm = true\nto = \"@libby:gamecult.org\"\n" } else { "" }); + let opening = "[[message]]\nfrom = \"@meta:gamecult.org\"\nname = \"Meta\"\nroom = \"#minds:gamecult.org\"\nsays = \"good afternoon, everyone\"\n"; + let text = std::fs::read_to_string(&path).unwrap().replace(opening, &format!("{}{}", line("!dm-libby:gamecult.org", true, "just you: how are you, really?"), line("#minds:gamecult.org", false, "Libby, how was the gig?"))); + std::fs::write(&path, text).unwrap(); + let (_, run) = run_plan(&path, &silent()); + let mut libby: Vec<(String, String)> = run["turns"].as_array().unwrap().iter().filter(|turn| turn["persona"] == "Libby").map(|turn| (turn["room"].as_str().unwrap().to_owned(), turn["kind"].as_str().unwrap().to_owned())).collect(); + libby.sort(); + assert_eq!(libby, vec![("!dm-libby:gamecult.org".to_owned(), "chat".to_owned()), ("#minds:gamecult.org".to_owned(), "chat".to_owned())], "{}", run["turns"]); + // Each turn read its own room's line, and no other's. + for turn in run["turns"].as_array().unwrap().iter().filter(|turn| turn["persona"] == "Libby") { + let stimulus = turn["report"]["renders"][0].to_string() + &turn["report"].to_string(); + let (own, other) = if turn["room"] == "#minds:gamecult.org" { ("how was the gig", "how are you, really") } else { ("how are you, really", "how was the gig") }; + assert!(stimulus.contains(own) && !stimulus.contains(other), "{}: {stimulus}", turn["room"]); + } +}