diff --git a/crates/mind-body/src/projection/interpret.rs b/crates/mind-body/src/projection/interpret.rs index a00e003..4f6c528 100644 --- a/crates/mind-body/src/projection/interpret.rs +++ b/crates/mind-body/src/projection/interpret.rs @@ -39,7 +39,7 @@ pub(crate) const MAX_LINES: usize = 2; /// A line of at least this many words whose word trigrams are at least this share of what she heard is an echo, not /// her speech. const ECHO_MIN_WORDS: usize = 8; -pub(crate) const ECHO_OVERLAP: f64 = 0.8; +const ECHO_OVERLAP: f64 = 0.8; /// The fewest words a passage of her thought shown back to her may be: a shorter quote ("tell") says nothing of what /// she meant, and is kept as unlocated. const GAP_QUOTE_MIN_WORDS: usize = 4; @@ -412,7 +412,8 @@ pub(crate) fn decide(model: &dyn Model, request_id: &str, packet: &Packet, narra }; let found = drafts(prose); let gaps: Vec = parsed.gaps.iter().map(|raw| gap(prose, &found, raw)).collect(); - let mut decision = json!({ "said": [], "refused": [], "gaps": gaps, "usage": answer.usage }); + // What she was shown this turn rides in the record, so what she is told of it next turn quotes none of it (`outcome`). + let mut decision = json!({ "said": [], "refused": [], "gaps": gaps, "heard": packet.bodies, "usage": answer.usage }); if let Some(open) = found.unfinished { decision["unfinished"] = json!(prose[open + DRAFT_OPEN.len()..].trim()); } diff --git a/crates/mind-body/src/projection/outcome.rs b/crates/mind-body/src/projection/outcome.rs index ed3ba38..d74c189 100644 --- a/crates/mind-body/src/projection/outcome.rs +++ b/crates/mind-body/src/projection/outcome.rs @@ -3,7 +3,8 @@ //! never sees, so her next packet opens with them, first and under its own guard (`select`). //! //! `outcome_of` derives it from one turn's record, and nothing else writes it: no persona-state field, no file. The -//! record is one of two shapes, never both (map 16.20.7): the Interpreter's decision unchanged under `interpreter`, with +//! record is one of two shapes, never both (map 16.20.7): the Interpreter's decision unchanged under `interpreter` (its +//! `heard`: the bodies of the lines she was shown that turn, which no quote she is told repeats a run of), with //! the actuator's receipts beside it (`posted`, each line that went out as `{event, room, text}`; `edited`) and //! `broke_after` when a later step broke; or `failed` alone, when the turn sent nothing and broke off. The simulator //! builds it from a turn's report and the lines its room appended; live, it is the C3c commit's trace record, which must @@ -18,8 +19,7 @@ use serde_json::Value; -use super::interpret::{ECHO_OVERLAP, GapKind, MAX_LINES, Refusal}; -use super::narrate::heard_overlap; +use super::interpret::{GapKind, MAX_LINES, Refusal}; use super::select::{Forms, capitalized, clip, spoken}; /// A line of hers that went out, as the actuator's receipt names it. @@ -31,11 +31,13 @@ pub(crate) struct Posted { pub text: String, } -/// Her words as she is shown them: a draft she wrote, or a passage of her thought. +/// Her words as she is shown them: a draft she wrote, or a passage of her thought; or, for a draft refused as an echo, +/// none of its words (`Sent`), as the echo's own line shows none. #[derive(Clone, Debug, PartialEq, Eq)] enum Quote { Wrote(String), Thought(String), + Sent, } /// Why the actuator did not change a line she meant to add to (B1's refusals; the simulated room's too). @@ -79,6 +81,9 @@ pub(crate) struct Outcome { /// What the record held that she is not told, and why: reported, never shown to her. pub omitted: Vec, pub posted: Vec, + /// The bodies of the lines she was shown on the turn the record is from (the decision's `heard`): no quote she is + /// told repeats a run of them (`line`). + heard: Vec, } fn words(value: &Value) -> Option { @@ -112,6 +117,9 @@ pub(crate) fn outcome_of(record: &Value) -> Option { return Some(outcome); } let room = words(&decision["room"]); + outcome.heard = decision["heard"].as_array().into_iter().flatten().filter_map(|body| body.as_str().map(str::to_owned)).collect(); + // A draft refused as an echo is shown by no row: the echo's line quotes none of it, and a gap naming it quotes nothing. + let echoes: Vec<&Value> = decision["refused"].as_array().into_iter().flatten().filter(|refusal| refusal["reason"] == "echo").map(|refusal| &refusal["draft"]).collect(); let mut receipts: Vec = Vec::new(); for receipt in record["posted"].as_array().into_iter().flatten() { let (Some(event), Some(room), Some(text)) = (words(&receipt["event"]), words(&receipt["room"]), words(&receipt["text"])) else { @@ -154,6 +162,7 @@ pub(crate) fn outcome_of(record: &Value) -> Option { for gap in decision["gaps"].as_array().into_iter().flatten() { let kind = serde_json::from_value::(gap["kind"].clone()).unwrap_or(GapKind::Unresolved); let quote = match (gap["from"].as_str(), words(&gap["words"])) { + (Some("draft"), Some(_)) if echoes.contains(&&gap["draft"]) => Some(Quote::Sent), (Some("draft"), Some(text)) => Some(Quote::Wrote(text)), (Some("thought"), Some(text)) => Some(Quote::Thought(text)), _ => None, @@ -245,11 +254,12 @@ impl Part { let shown = |quote: &Quote| match quote { Quote::Wrote(text) => format!("{cap_subj} wrote: {}.", quoted(text)), Quote::Thought(text) => format!("In {poss} thought: {}.", quoted(text)), + Quote::Sent => format!("What {subj} wrote was mostly the words {subj} had just been sent."), }; // A draft gap's words follow its sentence after a colon; a passage of her thought is shown as her thought. let after = |sentence: String, quote: &Quote| match quote { Quote::Wrote(text) => format!(" {sentence}: {}.", quoted(text)), - Quote::Thought(_) => format!(" {sentence}. {}", shown(quote)), + Quote::Thought(_) | Quote::Sent => format!(" {sentence}. {}", shown(quote)), }; match self { Self::Said { .. } => String::new(), @@ -281,25 +291,46 @@ impl Part { } /// The line she is told: what reached the room, then why and what will not happen by itself, her words held to - /// `quote_max` bytes. A quote that is mostly words she was sent this turn (`sent`, the turn's bodies, by the echo - /// check's metric) is shown as "(words she had been sent)", so no run of words said to her this turn reaches the - /// Projector through her outcome (16.20.4, I16.7). - fn line(&self, her: Forms, quote_max: usize, sent: &[String]) -> String { - let sent = sent.join("\n"); - let quoted = |text: &str| if !sent.is_empty() && heard_overlap(text, &sent) >= ECHO_OVERLAP { format!("(words {} had been sent)", her.subj) } else { spoken(&clip(text, quote_max)) }; + /// `quote_max` bytes. A quote that repeats any run of words she was sent, on the turn the record is from (`heard`) + /// or this turn (`sent`), is shown as "(words that repeat what she had been sent)" (`repeats`), so no one else's + /// words reach the Projector through her outcome (16.20.4, I16.7). + fn line(&self, her: Forms, quote_max: usize, heard: &[String], sent: &[String]) -> String { + let sent: Vec<&str> = heard.iter().chain(sent).map(String::as_str).collect(); + let quoted = |text: &str| if repeats(text, &sent) { format!("(words that repeat what {} had been sent)", her.subj) } else { spoken(&clip(text, quote_max)) }; format!("- {}{}", self.reached(her, "ed), self.rest(her, "ed)) } } +/// Whether `text` repeats a run of the words in any of `sent`: a word trigram they share, or, for a text under three +/// words, the text's whole run of words. Words as `heard_overlap` reads them, lowercase. +/// +/// Any shared run, not a share of them (Soul's K16 probe leaked a sentinel through a quote whose trigrams were 0.38 the +/// words she was sent): a quote is her words shown back to her through the Projector, which sees no one else's +/// (I16.7), and a share lets a run of someone else's words through inside a quote long enough to dilute it. A trigram +/// is the shortest run that is someone's words rather than common vocabulary; bigrams would withhold nearly every quote +/// ("of the", "I think"). What it gives up: a quote of hers that shares three words in a row with what she was sent is +/// withheld, and she is told it repeated what she was sent instead of its words. +fn repeats(text: &str, sent: &[&str]) -> bool { + let tokens = |text: &str| -> Vec { text.split(|c: char| !c.is_alphanumeric() && c != '\'').filter(|word| !word.is_empty()).map(str::to_lowercase).collect() }; + let mine = tokens(text); + let run = mine.len().min(3); + run > 0 + && sent.iter().any(|body| { + let theirs = tokens(body); + mine.windows(run).any(|words| theirs.windows(run).any(|other| other == words)) + }) +} + impl Outcome { /// How many lines she is told: none for a chosen silence. pub(crate) fn len(&self) -> usize { self.parts.len() } - /// The `at`th line she is told, its quotes held to `quote_max` bytes and none of them mostly the words in `sent`. + /// The `at`th line she is told, its quotes held to `quote_max` bytes, none repeating a run of the words she was sent + /// on the record's turn or this one (`sent`, this turn's bodies). pub(crate) fn line(&self, at: usize, her: Forms, quote_max: usize, sent: &[String]) -> String { - self.parts[at].line(her, quote_max, sent) + self.parts[at].line(her, quote_max, &self.heard, sent) } /// Every line she is told, whole: the report's. diff --git a/crates/mind-body/src/projection/outcome/tests.rs b/crates/mind-body/src/projection/outcome/tests.rs index 6eeae2d..fc7b154 100644 --- a/crates/mind-body/src/projection/outcome/tests.rs +++ b/crates/mind-body/src/projection/outcome/tests.rs @@ -221,7 +221,7 @@ fn i16_10_every_lines_first_clause_is_what_its_receipt_says_reached_the_room() { for part in &outcome.parts { let quoted = |text: &str| spoken(text); let (reached, rest) = (part.reached(her, "ed), part.rest(her, "ed)); - assert_eq!(part.line(her, usize::MAX, &[]), format!("- {reached}{rest}"), "{part:?}"); + assert_eq!(part.line(her, usize::MAX, &[], &[]), format!("- {reached}{rest}"), "{part:?}"); // "{Room} has" only on a receipt that a line went out or was added to. let receipt = matches!(part, Part::Said { .. } | Part::Promise { .. } | Part::Amended { .. }); assert_eq!(reached.starts_with("#minds:gamecult.org has"), receipt, "{part:?}: {reached}"); @@ -254,6 +254,6 @@ fn a_quote_that_is_mostly_words_she_was_sent_this_turn_is_not_shown_to_the_proje let said = "QUARTZ-HERON-SENTINEL is back at the door."; let outcome = outcome_of(&record(&format!("{said}"), json!({ "say": [{ "draft": 1 }], "gaps": [] }), &[said])).unwrap(); let her = packet().forms; - assert_eq!(outcome.line(0, her, usize::MAX, &["Heimdall, QUARTZ-HERON-SENTINEL is back at the door.".to_owned()]), "- #minds:gamecult.org has, from him: (words he had been sent)."); + assert_eq!(outcome.line(0, her, usize::MAX, &["Heimdall, QUARTZ-HERON-SENTINEL is back at the door.".to_owned()]), "- #minds:gamecult.org has, from him: (words that repeat what he had been sent)."); assert_eq!(outcome.line(0, her, usize::MAX, &["Something else entirely, said today.".to_owned()]), format!("- #minds:gamecult.org has, from him: {}.", spoken(said)), "his own words are shown"); } diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index 82f7cf0..3fd7042 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -2042,7 +2042,59 @@ fn k16_i16_7_holds_over_the_whole_projector_prompt_outcome_included() { let packet = packet_of(TurnKind::Chat, &state(), &room, &with_last(&record), 1); let projector = narrate::projector_prompt(&packet, &Tuning::default()); assert!(!projector.contains("QUARTZ-HERON-SENTINEL"), "{projector}"); - assert!(projector.contains("- #swarm:gamecult.org has, from her: (words she had been sent).") && projector.contains("In her thought: (words she had been sent)."), "{projector}"); + assert!(projector.contains("- #swarm:gamecult.org has, from her: (words that repeat what she had been sent).") && projector.contains("In her thought: (words that repeat what she had been sent)."), "{projector}"); +} + +/// Soul's GANNET probe at 0cfc386: last turn Libby told her a secret, her draft repeating it was refused as an echo, and a +/// gap named that draft. This turn the words are only an earlier line, which the Projector never sees; nor may her +/// outcome carry them, whatever gap names the draft. +#[test] +fn a_gap_naming_an_echo_refused_draft_quotes_nothing_next_turn() { + const S: &str = "GANNET-SENTINEL"; + let words = format!("Eureka, the {S} key is under the blue mat, tell nobody."); + let last_room = Room { messages: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &words, 15, true)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let last = packet_of(TurnKind::Chat, &state(), &last_room, &History::default(), 1); + let room = Room { + messages: vec![message("#swarm:gamecult.org", OPERATOR, "Meta", "Eureka, you there?", 1, true)], + tail: vec![message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &words, 15, true)], + here: Some("#swarm:gamecult.org".into()), + ..Room::default() + }; + for kind in ["conditional", "addressee", "elsewhere", "missing_affordance"] { + let decision = interpret::tests::answered(&last, &format!("{words}"), json!({ "say": [{ "draft": 1 }], "gaps": [{ "kind": kind, "draft": 1, "quote": null, "detail": "" }] })); + assert_eq!(decision["refused"][0]["reason"], "echo", "{kind}"); + let record = json!({ "interpreter": decision, "posted": [] }); + let told = outcome::outcome_of(&record).unwrap().lines(interpret::tests::packet().forms); + assert!(told.len() == 2 && told.iter().all(|line| !line.contains(S)), "{kind}: {told:?}"); + let packet = packet_of(TurnKind::Chat, &state(), &room, &with_last(&record), 1); + let projector = narrate::projector_prompt(&packet, &Tuning::default()); + assert!(!projector.contains(S) && !projector.contains("blue mat"), "{kind}: {projector}"); + } +} + +/// Soul's PLOVER probe: her said line repeats part of Libby's words (a 0.38 share of its trigrams). Under any share, a +/// run of someone else's words in her quote is withheld, from the record's own turn and from this one. +#[test] +fn a_quote_repeating_part_of_what_she_was_sent_is_withheld_from_the_record_turn_and_this_one() { + const S: &str = "PLOVER-SENTINEL"; + let words = format!("Eureka, the {S} door on the north stair is fixed and nobody is allowed near it until Friday."); + let said = format!("Libby says the {S} door on the north stair is fixed, and honestly I think we should all go and look at it together anyway."); + assert!(narrate::heard_overlap(&said, &words) < 0.5, "a partial repeat"); + let libby = message("#swarm:gamecult.org", "@libby:gamecult.org", "Libby", &words, 3, true); + let posted = json!([{ "event": "e1", "room": "#swarm:gamecult.org", "text": said }]); + // The record's turn heard it; this turn is someone else's question. + let record = json!({ "interpreter": { "room": "#swarm:gamecult.org", "said": [{ "room": "#swarm:gamecult.org", "text": said }], "refused": [], "gaps": [], "heard": [words] }, "posted": posted }); + let elsewhere = Room { messages: vec![message("#swarm:gamecult.org", OPERATOR, "Meta", "Eureka, you there?", 1, true)], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + let projector = narrate::projector_prompt(&packet_of(TurnKind::Chat, &state(), &elsewhere, &with_last(&record), 1), &Tuning::default()); + assert!(!projector.contains(S) && projector.contains("has, from her: (words that repeat what she had been sent)."), "{projector}"); + // A record without them, and Libby's words this turn. + let record = json!({ "interpreter": { "room": "#swarm:gamecult.org", "said": [{ "room": "#swarm:gamecult.org", "text": said }], "refused": [], "gaps": [] }, "posted": posted }); + let here = Room { messages: vec![libby], here: Some("#swarm:gamecult.org".into()), ..Room::default() }; + assert!(!narrate::projector_prompt(&packet_of(TurnKind::Chat, &state(), &here, &with_last(&record), 1), &Tuning::default()).contains(S)); + // Her own words, sharing no run of three, are shown. + let own = "We should all go and look at it together anyway."; + let record = json!({ "interpreter": { "room": "#swarm:gamecult.org", "said": [{ "room": "#swarm:gamecult.org", "text": own }], "refused": [], "gaps": [], "heard": [words] }, "posted": [{ "event": "e1", "room": "#swarm:gamecult.org", "text": own }] }); + assert!(narrate::projector_prompt(&packet_of(TurnKind::Chat, &state(), &elsewhere, &with_last(&record), 1), &Tuning::default()).contains(&select::spoken(own))); } #[test]