diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index 2c13651..07719e3 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -113,16 +113,19 @@ pub struct Organs<'a> { } /// One thing she can do this turn, typed by what it does (map 16.6, 16.18). Derived by `affordances` from what the turn -/// runs with, never written in state or in guidance. C3b adds `Read` from the Interpreter's tool list and K12 adds -/// `Suggest`; neither is listed before the organ that backs it exists, so she is never told she can do what this turn -/// cannot. +/// actually holds, never written in state or in guidance, nor taken from what the request asks for (map 16.19.5). C3b +/// adds `Read` from the Interpreter's tool list and K12 adds `Suggest`; neither is listed before the organ that backs it +/// exists, so she is never told she can do what this turn cannot. #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)] #[serde(rename_all = "snake_case")] pub(crate) enum Affordance { /// The room reaches her as typed lines: the mouth's `RoomLine` carries text only, and Select renders no images. Perceive, - /// She says whole lines of her prose in the turn's room (`interpret`). + /// She sends a draft of hers in the turn's room (`interpret`). Speak, + /// A test's stand-in for the first affordance that builds, changes or ships something (map 16.19.6): none exists yet. + #[cfg(test)] + TestWrite, } impl Affordance { @@ -131,15 +134,22 @@ impl Affordance { pub(crate) fn writes(self) -> bool { match self { Self::Perceive | Self::Speak => false, + #[cfg(test)] + Self::TestWrite => true, } } } -/// What this turn can do, from the organs it runs with and the turn's room (`select::turn_room`): the room's lines reach -/// her when there is a mouth to read them, and she can speak when the turn has a room, the only one the Interpreter lets -/// her speak in. -pub(crate) fn affordances(organs: &Organs, room: Option<&str>) -> Vec { - [(organs.chat.is_some(), Affordance::Perceive), (room.is_some(), Affordance::Speak)].into_iter().filter_map(|(can, affordance)| can.then_some(affordance)).collect() +/// What this turn can do, from what it holds (map 16.19.5): +/// - `Perceive` when the room was read: the mouth's read returned (a harness fixture's and the simulated room's always +/// do). A failed read, or no mouth, and no room reaches her. +/// - `Speak` when the turn has a port her words go out through and a step that sends them: a mouth (the live mouth the +/// commit sends through, a fixture's, or the simulated room), a room to speak in (`select::turn_room`, the only one the +/// Interpreter lets her speak in), and the Interpreter running this turn. A run that stops at the Projector or the +/// Persona says nothing, and its packet says she can say nothing. +pub(crate) fn affordances(organs: &Organs, read: bool, room: Option<&str>, through: Through) -> Vec { + let speaks = organs.chat.is_some() && room.is_some() && through == Through::Interpreter; + [(read, Affordance::Perceive), (speaks, Affordance::Speak)].into_iter().filter_map(|(can, affordance)| can.then_some(affordance)).collect() } /// How far a render runs: the Projector's narration, or on through the Persona stage to her prose. @@ -359,7 +369,7 @@ pub fn project(organs: &Organs, request: &Request, now: DateTime) -> Result let history = History { recent: request.recent.clone(), ..History::default() }; let people = People { operators: &request.operators, minds: &request.minds, own_sender: &request.own_sender }; let inputs = Inputs { kind: request.kind, state: &state, room: &room, history: &history, observation: None, people, now, seed: request.seed, affordances: &[] }; - let can = affordances(organs, turn_room(&inputs)); + let can = affordances(organs, room_report["read"] == true, turn_room(&inputs), request.through); let inputs = Inputs { affordances: &can, ..inputs }; let mut report = json!({ diff --git a/crates/mind-body/src/projection/interpret/tests.rs b/crates/mind-body/src/projection/interpret/tests.rs index 1b83ffa..e5baa1f 100644 --- a/crates/mind-body/src/projection/interpret/tests.rs +++ b/crates/mind-body/src/projection/interpret/tests.rs @@ -370,7 +370,7 @@ fn the_interpreter_is_told_what_he_can_do_here_from_the_packets_affordances() { assert!(prompt.contains("missing_affordance when his thought reaches for something his body cannot do this turn"), "{prompt}"); let mut listening = packet(); listening.affordances = vec![Affordance::Perceive]; - assert!(speech_prompt(&listening, "x", "y", &Tuning::default()).contains("What he can do here:\nThis turn the room reaches him as typed lines; nothing"), "derived from the affordances, not a constant"); + assert!(speech_prompt(&listening, "x", "y", &Tuning::default()).contains("What he can do here:\nThis turn the room reaches him as typed lines, and he can say nothing; nothing"), "derived from the affordances, not a constant"); } #[test] diff --git a/crates/mind-body/src/projection/select.rs b/crates/mind-body/src/projection/select.rs index 53c0cc4..1a7cfec 100644 --- a/crates/mind-body/src/projection/select.rs +++ b/crates/mind-body/src/projection/select.rs @@ -1261,14 +1261,14 @@ pub(crate) fn capitalized(word: &str) -> String { /// Interpreter's "What she can do here" are this text. It ends with "nothing she does here builds, changes or ships /// anything" exactly when no affordance writes (`Affordance::writes`): a derived sentence, never a constant. pub(crate) fn turn_words(can: &[Affordance], her: Forms) -> String { - let clauses: Vec = can - .iter() - .map(|affordance| match affordance { - Affordance::Perceive => format!("the room reaches {} as typed lines", her.obj), - Affordance::Speak => format!("{} can answer in the turn's room by typing lines of {} own", her.subj, her.poss), - }) - .collect(); - let mut words = if clauses.is_empty() { format!("this turn no room reaches {} and {} can say nothing", her.obj, her.subj) } else { format!("this turn {}", clauses.join(", and ")) }; + let has = |affordance: Affordance| can.contains(&affordance); + let clauses = [ + if has(Affordance::Perceive) { format!("the room reaches {} as typed lines", her.obj) } else { format!("no room reaches {}", her.obj) }, + if has(Affordance::Speak) { format!("{} can answer in the turn's room by typing lines of {} own", her.subj, her.poss) } else { format!("{} can say nothing", her.subj) }, + ]; + #[cfg(test)] + let clauses: Vec = clauses.into_iter().chain(has(Affordance::TestWrite).then(|| format!("{} can write", her.subj))).collect(); + let mut words = format!("this turn {}", clauses.join(", and ")); if !can.iter().any(|affordance| affordance.writes()) { let does = if her.s.is_empty() { "do" } else { "does" }; let _ = write!(words, "; nothing {} {does} here builds, changes or ships anything", her.subj); diff --git a/crates/mind-body/src/projection/tests.rs b/crates/mind-body/src/projection/tests.rs index e5b80ea..bbaf05a 100644 --- a/crates/mind-body/src/projection/tests.rs +++ b/crates/mind-body/src/projection/tests.rs @@ -268,10 +268,14 @@ fn a_state_with_one_sense_renders_it_and_a_state_with_none_only_the_turn_line() fn the_turn_line_is_derived_from_the_affordances_alone() { let her = select::forms(&state()); assert_eq!(select::turn_words(&[Affordance::Perceive, Affordance::Speak], her), "this turn the room reaches her as typed lines, and she can answer in the turn's room by typing lines of her own; nothing she does here builds, changes or ships anything"); - assert_eq!(select::turn_words(&[Affordance::Perceive], her), "this turn the room reaches her as typed lines; nothing she does here builds, changes or ships anything"); - assert_eq!(select::turn_words(&[], her), "this turn no room reaches her and she can say nothing; nothing she does here builds, changes or ships anything"); + assert_eq!(select::turn_words(&[Affordance::Perceive], her), "this turn the room reaches her as typed lines, and she can say nothing; nothing she does here builds, changes or ships anything"); + assert_eq!(select::turn_words(&[Affordance::Speak], her), "this turn no room reaches her, and she can answer in the turn's room by typing lines of her own; nothing she does here builds, changes or ships anything"); + assert_eq!(select::turn_words(&[], her), "this turn no room reaches her, and she can say nothing; nothing she does here builds, changes or ships anything"); let them = select::forms(&json!({ "presentation": { "pronouns": "any" } })); assert!(select::turn_words(&[Affordance::Speak], them).ends_with("nothing they do here builds, changes or ships anything")); + // The sentence is derived (map 16.19.6): the first affordance of kind write removes it. + let writes = select::turn_words(&[Affordance::Perceive, Affordance::Speak, Affordance::TestWrite], her); + assert!(writes.ends_with(", and she can write") && !writes.contains("builds, changes or ships"), "{writes}"); // The longest turn line and the heading fit the least body guard the tuning allows. for forms in [her, them, select::forms(&json!({ "presentation": { "pronouns": "he/him" } }))] { let longest = format!("Their body:\n- {}.\n", select::turn_words(CAN, forms)); @@ -279,20 +283,45 @@ fn the_turn_line_is_derived_from_the_affordances_alone() { } } +/// A mouth whose read fails. +struct Deaf; + +impl MouthReader for Deaf { + fn pending(&self) -> Result { + anyhow::bail!("the mouth did not answer") + } +} + #[test] -fn what_the_turn_can_do_comes_from_its_organs_and_its_room() { +fn what_the_turn_can_do_comes_from_what_it_holds() { let chat = mouth(); let with = Organs { persona: None, chat: Some(&chat), addressing: None, model: None }; let without = Organs { chat: None, ..with }; - assert_eq!(affordances(&with, Some("#swarm:gamecult.org")), [Affordance::Perceive, Affordance::Speak]); - assert_eq!(affordances(&with, None), [Affordance::Perceive], "no room, nowhere to speak"); - assert_eq!(affordances(&without, Some("#swarm:gamecult.org")), [Affordance::Speak], "no mouth, no room reaches her"); - // Through `project`: the packet carries what its organs can do; the request's room is the turn's. + let room = Some("#swarm:gamecult.org"); + assert_eq!(affordances(&with, true, room, Through::Interpreter), [Affordance::Perceive, Affordance::Speak]); + assert_eq!(affordances(&with, true, None, Through::Interpreter), [Affordance::Perceive], "no room, nowhere to speak"); + assert_eq!(affordances(&with, false, room, Through::Interpreter), [Affordance::Speak], "a failed read: no room reaches her"); + assert_eq!(affordances(&without, false, room, Through::Interpreter), [], "a room and no send port: she can say nothing"); + for through in [Through::Projector, Through::Persona] { + assert_eq!(affordances(&with, true, room, through), [Affordance::Perceive], "{through:?}: no Interpreter runs, so nothing she writes goes out"); + } + // Through `project`: what the packet says she can do is what the turn held, not what the request asked for. let dir = tempfile::tempdir().unwrap(); let stored = Stored(state()); - let report = project(&Organs { persona: Some(&stored), ..without }, &request(dir.path(), None, None), now()).unwrap(); - assert!(report["packet"]["context"].as_str().unwrap().contains("Her body:\n- this turn she can answer in the turn's room by typing lines of her own;"), "{}", report["packet"]["context"]); - assert_eq!(report["packet"]["affordances"], json!(["speak"])); + let context = |organs: &Organs, through: Through| { + let report = project(&Organs { persona: Some(&stored), ..*organs }, &Request { through, ..request(dir.path(), None, None) }, now()).unwrap(); + (report["packet"]["context"].as_str().unwrap().to_owned(), report["packet"]["affordances"].clone()) + }; + let (body, can) = context(&without, Through::Interpreter); + assert!(body.contains("Her body:\n- this turn no room reaches her, and she can say nothing;"), "{body}"); + assert_eq!(can, json!([])); + let deaf = Deaf; + let (body, can) = context(&Organs { chat: Some(&deaf), ..with }, Through::Interpreter); + assert!(body.contains("Her body:\n- this turn no room reaches her, and she can answer in the turn's room by typing lines of her own;"), "{body}"); + assert_eq!(can, json!(["speak"])); + let (body, can) = context(&with, Through::Persona); + assert!(body.contains("Her body:\n- this turn the room reaches her as typed lines, and she can say nothing;"), "{body}"); + assert_eq!(can, json!(["perceive"])); } #[test]