diff --git a/crates/mind-body/src/bin/mind-persona.rs b/crates/mind-body/src/bin/mind-persona.rs index 4d435fb..c66462f 100644 --- a/crates/mind-body/src/bin/mind-persona.rs +++ b/crates/mind-body/src/bin/mind-persona.rs @@ -56,7 +56,7 @@ use mind_body::chat_rudp::{RudpChat, connection_id}; use mind_body::cli::{instance_and_endpoint, instance_and_maybe_endpoint}; use mind_body::control::{CONTROL_ROOT, FileSource, control_path, swarm_path}; use mind_body::persona::{Config, carry}; -use mind_body::heartbeat::{FileStore, HeartbeatView, own_rooms_among}; +use mind_body::heartbeat::{FileStore, HeartbeatView, Store, own_rooms_among, woken_for}; use mind_body::queue::HuginnMind; use mind_body::projection::harness::{Fixture, Harness, batch, fixture_request, simulate, state_file}; use mind_body::projection::{DomainSource, HttpForge, HuginnPersonaSource, KitModel, LiveReads, MindDomain, Model, MouthReader, ReadSource, Organs as ProjectOrgans, PersonaSource, Request as ProjectRequest, TUNING_FILE, TUNING_ROOT, Through, TurnKind, connector_key, project}; @@ -212,6 +212,12 @@ fn run_project(args: &[String]) -> Result { }, _ => (vec![], None), }; + // The lines the heartbeat woke her for, from its handled keys; an unread state is said in the report, not read as none. + let (woken, woken_unread) = match heartbeat.as_ref().map(Store::load) { + Some(Ok(state)) => (state.map(|state| woken_for(&state, &instance.0)).unwrap_or_default(), None), + Some(Err(error)) => (vec![], Some(format!("the heartbeat state unread: {error}"))), + None => (vec![], None), + }; // Her reads by intent (map 16.22.3): her mind through the same endpoint, the forge when it is named, the web. let reads = address.map(|address| LiveReads::new(HuginnClient::new(address, instance.clone(), TIMEOUT), setting("MIND_PERSONA_FORGE_URL").map(|forge| HttpForge::new(&forge)))); let organs = ProjectOrgans { @@ -238,6 +244,7 @@ fn run_project(args: &[String]) -> Result { last_heard: vec![], recent: vec![], own_rooms: own, + woken_for: woken, last_outcome, last_wake: None, records: vec![], @@ -246,6 +253,9 @@ fn run_project(args: &[String]) -> Result { if let Some(unread) = own_unread { report["own_rooms_unread"] = unread.into(); } + if let Some(unread) = woken_unread { + report["woken_for_unread"] = unread.into(); + } let addressing = &report["addressing"]; // The addressing source is what decides "said to her": said first, where a person running this by hand sees it. eprintln!( diff --git a/crates/mind-body/src/heartbeat.rs b/crates/mind-body/src/heartbeat.rs index 7cf6ea3..18c9954 100644 --- a/crates/mind-body/src/heartbeat.rs +++ b/crates/mind-body/src/heartbeat.rs @@ -221,6 +221,12 @@ pub trait Store { fn save_addressing(&self, addressing: &Addressing) -> Result<()>; } +/// The lines the heartbeat woke `slug` for, as her handled keys name them (`slug:event`): what her packet reads as said to +/// her, so the wake and the packet cannot disagree about whose line it was. +pub fn woken_for(state: &HeartbeatState, slug: &str) -> Vec { + state.handled_mentions.iter().filter_map(|key| key.split_once(':').filter(|(who, _)| *who == slug).map(|(_, event)| event.to_owned())).collect() +} + /// What a Persona unit may read of `heartbeat.cc`: the swarm's addressing. Read-only. pub trait HeartbeatView { fn addressing(&self) -> Result>; diff --git a/crates/mind-body/src/heartbeat/tests.rs b/crates/mind-body/src/heartbeat/tests.rs index d2ecaba..1be3834 100644 --- a/crates/mind-body/src/heartbeat/tests.rs +++ b/crates/mind-body/src/heartbeat/tests.rs @@ -932,9 +932,8 @@ mod wake_rule { rig.turn(BOB, 4000); said_in_general(&rig, general(1, "@newcomer:chat", "hello everyone!")); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("ann".into())] }, "the beat is due: Ann takes the floor and the beat does not wake Bob on the same line"); - rig.end_unit(ANN); rig.advance(1); - assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "nor a poll later while the line is within the exchange window"); + assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "nor a poll later, Ann still in her turn, while the line is within the exchange window"); rig.advance(31 * 60); assert_eq!(rig.poll(), Outcome::Polled { launched: vec![Slug("bob".into())] }, "a Mind that holds only an old taken line is woken by the beat like any other"); } @@ -978,6 +977,16 @@ mod wake_rule { assert_eq!(rig.poll(), Outcome::Polled { launched: vec![] }, "a line that names Bob is Bob's, even while he is busy: Ann is not picked for it"); } + #[test] + fn the_lines_the_heartbeat_woke_her_for_are_read_from_her_own_handled_keys() { + 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())] }); + let state = rig.store.state.borrow().clone().unwrap(); + assert_eq!(woken_for(&state, "ann"), vec!["$e1".to_owned()], "the hello that woke her"); + assert!(woken_for(&state, "bob").is_empty(), "not the Mind who was not woken for it"); + } + #[test] fn a_minds_own_line_a_notice_an_edit_a_tag_or_an_operator_opens_no_floor() { for (line, why) in [ diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index b579187..678724b 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -219,6 +219,9 @@ pub struct Request { pub recent: Vec>, /// The rooms that are hers, by the key the lines carry: live from `heartbeat::own_rooms` (the rooms named for her stewarded repos), simulated from the plan. pub own_rooms: Vec, + /// The events the heartbeat woke her for (`heartbeat::woken_for`; a simulation's, the lines its floor gave her): said to her, + /// as the wake that named her decided, whoever they were and in whatever room. + pub woken_for: Vec, /// The record of her last turn, which her packet opens with what came of (`outcome::outcome_of`): the Interpreter's /// decision under `interpreter` and the receipts of the lines that went out under `posted`, or `failed`. A /// simulation carries each turn's to her next; a harness run takes a fixture's (`--last-outcome`). None live until @@ -299,8 +302,10 @@ fn read_room(chat: &dyn MouthReader, request: &Request, own: &Addressee) -> (Roo text: delivery.text.clone(), at_ms: delivery.at_unix_ms, // The heartbeat's rule, the one that woke her: never a second opinion here. A human's line in - // her own room always wakes her, so it is to her, as a direct message is (the plan's or unit's `own_rooms`). - to_her: is_for(own, delivery) || (request.own_rooms.contains(&delivery.room_alias) && delivery.sender != request.own_sender && !request.minds.contains(&delivery.sender)), + // her own room always wakes her, so it is to her, as a direct message is (the plan's or unit's `own_rooms`), and so + // is a line the heartbeat woke her for (an open hello she was picked to answer: `woken_for`). + to_her: is_for(own, delivery) + || (delivery.sender != request.own_sender && !request.minds.contains(&delivery.sender) && (request.own_rooms.contains(&delivery.room_alias) || request.woken_for.contains(&delivery.event_id))), event: delivery.event_id.clone(), reply: replied(&delivery.reply_to), }) diff --git a/crates/mind-body/src/projection/harness.rs b/crates/mind-body/src/projection/harness.rs index 8c7a461..eec80d1 100644 --- a/crates/mind-body/src/projection/harness.rs +++ b/crates/mind-body/src/projection/harness.rs @@ -364,6 +364,7 @@ pub fn fixture_request(harness: &Harness, fixture: &Fixture, state: Option, judge_against: Option) -> Request { - Request { instance: INSTANCE.into(), kind: TurnKind::Chat, state: None, room: Some("#swarm:gamecult.org".into()), seed: 5, renders, judge_against, operators: operators(), minds: minds(), own_sender: OWN.into(), tuning: dir.join(TUNING_FILE), through: Through::Projector, last_heard: vec![], recent: vec![], own_rooms: vec![], last_outcome: None, last_wake: None, records: vec![] } + Request { instance: INSTANCE.into(), kind: TurnKind::Chat, state: None, room: Some("#swarm:gamecult.org".into()), seed: 5, renders, judge_against, operators: operators(), minds: minds(), own_sender: OWN.into(), tuning: dir.join(TUNING_FILE), through: Through::Projector, last_heard: vec![], recent: vec![], own_rooms: vec![], woken_for: vec![], last_outcome: None, last_wake: None, records: vec![] } } /// A human's line in her own room is to her (the humans_pace ruling: it always wakes her), with no name and no tag: the arrival @@ -968,6 +968,29 @@ fn a_humans_line_in_her_own_room_is_to_her() { } } +/// A member's open hello in a public room wakes one Mind, the pick; the line is said to her, as a line in her own room is, so she +/// knows she was picked to answer. A Mind who was not woken for it meets it among the others, and a Mind's line is never to her +/// by this route. +#[test] +fn a_line_the_heartbeat_woke_her_for_is_to_her_and_one_it_did_not_is_not() { + let dir = tempfile::tempdir().unwrap(); + let stored = Stored(state()); + let general = "#general:gamecult.org"; + let mouth = |sender: &str| ReadOnlyMouth { pending: ChatPending { persona: "eureka".into(), deliveries: vec![delivery(1, general, sender, "Newcomer", "hello everyone!", &[])], tail: Some(vec![]), tail_gaps: vec![] }, reads: Cell::new(0) }; + let packet = |chat: &ReadOnlyMouth, woken_for: Vec| { + let organs = Organs { persona: Some(&stored), chat: Some(chat), addressing: None, model: None, domain: None, reads: None }; + project(&organs, &Request { room: Some(general.into()), woken_for, ..request(dir.path(), None, None) }, now()).unwrap()["packet"].clone() + }; + let hello = mouth("@newcomer:gamecult.org"); + let picked = packet(&hello, vec!["$e1".into()]); + assert!(picked["context"].as_str().unwrap().contains("turned to her"), "{picked}"); + assert!(picked["speaking"].as_str().unwrap().contains("is waiting on her"), "{picked}"); + let not_picked = packet(&hello, vec![]); + assert!(!not_picked["context"].as_str().unwrap().contains("turned to her") && !not_picked["speaking"].as_str().unwrap_or_default().contains("waiting on her"), "{not_picked}"); + let mind = packet(&mouth("@libby:gamecult.org"), vec!["$e1".into()]); + assert!(!mind["context"].as_str().unwrap().contains("turned to her"), "a Mind's line is to her only by is_for: {mind}"); +} + #[test] fn project_in_every_mode_writes_nothing() { let dir = tempfile::tempdir().unwrap();