diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index fc48c8c..c8d3569 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -46,7 +46,7 @@ use crate::address::{Addressee, addresses, is_for, localpart}; use crate::chat_client::{Basis, Chat, ChatDelivery, TailLine}; use crate::control::{ControlSource, Effective, HeldReason, derive}; use crate::heartbeat::{HeartbeatView, Speaker, own_rooms, pending_facts, read_pending, wakes}; -use crate::projection::{DomainSource, HuginnPersonaSource, Model, Mouth, Organs, Pass, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, decide, decided, deliver, history, owing, own_addressee as heard_as}; +use crate::projection::{DomainSource, HuginnPersonaSource, Model, Mouth, Organs, Pass, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, decide, decided, deliver, history, owing, untold_rooms, own_addressee as heard_as}; /// The admitting agent's name on what her Persona admitted; `self_run::inbox` still reads her briefs by it. pub const AGENT: &str = "mind-persona"; @@ -368,7 +368,8 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { // The key of this start's records in her trace: how long it is, so a record that landed is never written over. let stem = format!("n{}", trace.len()); let owed: Vec = trace.iter().filter(|record| record.get("failed").is_none() && record["owed"].as_array().is_some_and(|owed| !owed.is_empty())).cloned().collect(); - let mut stalled: BTreeSet = BTreeSet::new(); + // The rooms where a send went unknown and she has not been told: nothing newer goes in until a real turn of hers has told her. + let mut stalled: BTreeSet = untold_rooms(&trace); // The rooms to take a turn in, oldest waking line first; a start no line woke is the beat's. let mut rooms: Vec<&str> = Vec::new(); for line in &waking { diff --git a/crates/mind-body/src/persona/drain_tests.rs b/crates/mind-body/src/persona/drain_tests.rs index 40739c6..d543fde 100644 --- a/crates/mind-body/src/persona/drain_tests.rs +++ b/crates/mind-body/src/persona/drain_tests.rs @@ -1154,3 +1154,31 @@ fn t_l3_the_snapshot_reader_creates_no_lock_file() { assert!(TraceSnapshot::new(&path).records().is_ok() && !lock.exists(), "a snapshot creates no lock file"); assert!(TraceSnapshot::new(dir.path().join("absent.cc")).records().unwrap().is_empty()); } + +/// A record of hers as written ahead, owing `sends` (id, room, text), `age_s` seconds before the rig's clock begins. +fn owing_record(rig: &Rig, key: &str, age_s: i64, sends: &[(&str, &str, &str)]) { + let at = Utc.timestamp_millis_opt(i64::try_from(BASE_MS).unwrap()).unwrap() - Duration::seconds(age_s); + let owed: Vec = sends.iter().map(|(id, room, text)| json!({ "id": id, "room": room, "text": text })).collect(); + rig.trace.rows.borrow_mut().push((key.into(), json!({ "key": key, "room": sends[0].1, "held": [], "owed": owed, "posted": [], "refused_sends": [], "edited": [], "at": at.to_rfc3339() }))); +} + +/// Soul s7 newer-after-unknown. When an older send in a room turns unknown in a start, no newer line goes into that room until she has +/// been told, which is at her next real turn; another room is not held. Then they go out, in the order she decided them. +#[test] +fn t_l3_no_newer_line_goes_into_a_room_after_an_older_send_there_turned_unknown_until_she_is_told() { + let rig = Rig::new(vec![]); + owing_record(&rig, "n0-t0", 2500, &[("lold:s0", &room(1), "the old line")]); + owing_record(&rig, "n1-t0", 100, &[("lnew:s0", &room(1), "the newer line"), ("lelse:s0", &room(2), "elsewhere")]); + rig.run(Never); + assert!(rig.trace.rows.borrow()[0].1["unknown_sends"][0]["text"] == "the old line", "the older send is unknown"); + assert!(texts(&rig, &room(1)).is_empty(), "the newer line did not go into the room: {:?}", texts(&rig, &room(1))); + assert_eq!(texts(&rig, &room(2)), ["elsewhere"], "another room's send is not held"); + // She is started, as the poll starts her for the line still owed, and a line wakes her: her turn tells her. Nothing goes in before it. + rig.mouth.held.borrow_mut().push(line(1, &room(1), META, "hello")); + rig.run(scripted(false)); + assert!(texts(&rig, &room(1)).is_empty(), "the start that held it goes by: {:?}", texts(&rig, &room(1))); + rig.run(Never); + let said = texts(&rig, &room(1)); + assert_eq!(said.len(), 2, "told, so the newer line goes in, and her turn's after it: {said:?}"); + assert_eq!(said[0], "the newer line"); +} diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index 57b83f6..c833419 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -50,7 +50,7 @@ pub use narrate::{Answer, Continued, Failed, KitModel, Model, Reasoning, Spent, pub use read::{LiveReads, ReadFailure, ReadFixture, ReadRequest, ReadSource}; pub use select::TurnKind; pub(crate) use commit::apply as apply_commit; -pub(crate) use turn::{Mouth, Pass, Taken, decide, decided, deliver, failed_run, history, owes, owing}; +pub(crate) use turn::{Mouth, Pass, Taken, decide, decided, deliver, failed_run, history, owes, owing, untold_rooms}; pub use tuning::{TUNING_FILE, TUNING_ROOT}; use narrate::{cap_prose, cap_words, distinct_2, field_names, heard_overlap, judge_prompt, judged, offers_options, persona_prompt, projector_prompt, word_budget, words}; diff --git a/crates/mind-body/src/projection/turn.rs b/crates/mind-body/src/projection/turn.rs index a95ed6c..bada4b8 100644 --- a/crates/mind-body/src/projection/turn.rs +++ b/crates/mind-body/src/projection/turn.rs @@ -151,8 +151,9 @@ pub(crate) fn decide(organs: &Organs, request: &Request, key: &str, now: DateTim /// /// A send is made under its recorded id, so the homeserver answers a repeat with the original event. `Pass::Past` (the record is older /// than the homeserver remembers an id) sends nothing: each owed send is recorded in `unknown_sends`, neither posted nor refused, and -/// she is told it may or may not have gone out. `stalled` holds the rooms whose earlier send got no answer in this start: nothing is -/// sent into them after it, so her lines never arrive out of order, and a send not sent stays owed. +/// she is told it may or may not have gone out. `stalled` holds the rooms whose earlier send got no answer or went unknown in this start: nothing +/// is sent into them after it, so her lines never arrive out of order and none goes in before she is told, and a send not sent stays owed. A send past +/// the window is unknown whether or not its room is stalled: it cannot be repeated, so waiting would owe it for ever. pub(crate) fn deliver(actuator: &dyn Actuator, record: &mut Value, pass: Pass, stalled: &mut BTreeSet) { let owed: Vec = record["owed"].as_array().cloned().unwrap_or_default(); let mut still = Vec::new(); @@ -169,6 +170,7 @@ pub(crate) fn deliver(actuator: &dyn Actuator, record: &mut Value, pass: Pass, s Some(unknowns) => unknowns.push(unknown), None => record["unknown_sends"] = json!([unknown]), } + stalled.insert(room.to_owned()); continue; } if stalled.contains(room) { @@ -264,6 +266,11 @@ fn unknown(records: &[Value]) -> Vec { told } +/// The rooms of the sends her trace shows unknown and not yet told (`unknown`): no newer line goes into them until a real turn of hers has told her. +pub(crate) fn untold_rooms(records: &[Value]) -> BTreeSet { + unknown(records).iter().filter_map(|sent| sent["room"].as_str().map(str::to_owned)).collect() +} + /// Her `records`, oldest first, read. pub(crate) fn history(records: &[Value]) -> Past { let selected = |record: &Value| record["selected"].as_array().map(|ids| ids.iter().filter_map(Value::as_str).map(str::to_owned).collect::>());