diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs index 60b19d5..817f69f 100644 --- a/crates/mind-body/src/projection/harness/tests.rs +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -47,7 +47,9 @@ impl Scripted { pub(crate) fn drafted_lines(prompt: &str) -> String { let numbered = prompt.rsplit("drafts, by number:\n").next().unwrap_or_default(); let say: Vec = (1..).take_while(|n| numbered.lines().any(|line| line.starts_with(&format!("{n}. ")))).map(|n| json!({ "draft": n, "mentions": [], "in_reply_to": null })).collect(); - json!({ "say": say, "gaps": [] }).to_string() + // Every line the prompt marks said to her asks her to hold nothing (K59). + let no_hold: Vec<&str> = prompt.lines().filter_map(|line| line.strip_prefix("- ")?.split_once(" (said to ")).map(|(id, _)| id).filter(|id| !id.contains(' ')).collect(); + json!({ "say": say, "gaps": [], "no_hold": no_hold }).to_string() } impl Model for Scripted { diff --git a/crates/mind-body/src/projection/interpret.rs b/crates/mind-body/src/projection/interpret.rs index 4d5bbab..1c3195e 100644 --- a/crates/mind-body/src/projection/interpret.rs +++ b/crates/mind-body/src/projection/interpret.rs @@ -71,7 +71,7 @@ pub(crate) const SPEECH_OPENING: &str = "You decide what one person says aloud i /// The speech contract, sent as the schema turn's output schema (`--json-schema`): the drafts she sends now by number, /// and the gaps. The model writes no words of hers. -pub(crate) const SPEECH_SCHEMA: &str = r#"{"type":"object","additionalProperties":false,"required":["say","gaps","amend","suggest","state","reopen","hold","hold_released","read","unsent"],"properties":{"hold":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["line","words","where"],"properties":{"line":{"type":"string"},"words":{"type":"string"},"where":{"type":"string","enum":["here","anywhere"]}}}},"hold_released":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["hold","line"],"properties":{"hold":{"type":"string"},"line":{"type":"string"}}}},"reopen":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["promise","words","needs"],"properties":{"promise":{"type":"string"},"words":{"type":"string"},"needs":{"type":["object","null"],"additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}},"unsent":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["draft","how","quote"],"properties":{"draft":{"type":"integer"},"how":{"type":"string","enum":["declined","superseded"]},"quote":{"type":"string"}}}},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target","quote"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"},"quote":{"type":"string"}}},"state":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","promise","how","evidence","thought","still_stands"],"properties":{"kind":{"type":"string","enum":["promise_close"]},"promise":{"type":"string"},"how":{"type":"string","enum":["kept","withdrawn","released"]},"evidence":{"type":"object","additionalProperties":false,"required":["draft","line"],"properties":{"draft":{"type":["integer","null"]},"line":{"type":["string","null"]}}},"thought":{"type":["string","null"]},"still_stands":{"type":"boolean"}}}},"suggest":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","draft","about","private"],"properties":{"kind":{"type":"string","enum":["feedback","idea"]},"draft":{"type":"integer","minimum":1},"about":{"type":["string","null"]},"private":{"type":"boolean"}}}},"amend":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["target","draft","holds","promises"],"properties":{"target":{"type":"string"},"draft":{"type":"integer","minimum":1},"holds":{"type":"array","items":{"type":"string"}},"promises":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["words","to","reaffirms","needs"],"properties":{"words":{"type":"string"},"to":{"type":["string","null"]},"reaffirms":{"type":["string","null"]},"needs":{"type":"object","additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}}}}},"say":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["draft","mentions","in_reply_to","holds","promises"],"properties":{"draft":{"type":"integer","minimum":1},"holds":{"type":"array","items":{"type":"string"}},"mentions":{"type":"array","items":{"type":"string"}},"in_reply_to":{"type":["string","null"]},"promises":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["words","to","reaffirms","needs"],"properties":{"words":{"type":"string"},"to":{"type":["string","null"]},"reaffirms":{"type":["string","null"]},"needs":{"type":"object","additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}}}}},"gaps":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","draft","quote","detail"],"properties":{"kind":{"type":"string","enum":["addressee","elsewhere","missing_affordance","unwritten"]},"draft":{"type":["integer","null"]},"quote":{"type":["string","null"]},"detail":{"type":"string"}}}}}}"#; +pub(crate) const SPEECH_SCHEMA: &str = r#"{"type":"object","additionalProperties":false,"required":["say","gaps","amend","suggest","state","reopen","hold","no_hold","hold_released","read","unsent"],"properties":{"no_hold":{"type":"array","items":{"type":"string"}},"hold":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["line","words","where"],"properties":{"line":{"type":"string"},"words":{"type":"string"},"where":{"type":"string","enum":["here","anywhere"]}}}},"hold_released":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["hold","line"],"properties":{"hold":{"type":"string"},"line":{"type":"string"}}}},"reopen":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["promise","words","needs"],"properties":{"promise":{"type":"string"},"words":{"type":"string"},"needs":{"type":["object","null"],"additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}},"unsent":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["draft","how","quote"],"properties":{"draft":{"type":"integer"},"how":{"type":"string","enum":["declined","superseded"]},"quote":{"type":"string"}}}},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target","quote"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"},"quote":{"type":"string"}}},"state":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","promise","how","evidence","thought","still_stands"],"properties":{"kind":{"type":"string","enum":["promise_close"]},"promise":{"type":"string"},"how":{"type":"string","enum":["kept","withdrawn","released"]},"evidence":{"type":"object","additionalProperties":false,"required":["draft","line"],"properties":{"draft":{"type":["integer","null"]},"line":{"type":["string","null"]}}},"thought":{"type":["string","null"]},"still_stands":{"type":"boolean"}}}},"suggest":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","draft","about","private"],"properties":{"kind":{"type":"string","enum":["feedback","idea"]},"draft":{"type":"integer","minimum":1},"about":{"type":["string","null"]},"private":{"type":"boolean"}}}},"amend":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["target","draft","holds","promises"],"properties":{"target":{"type":"string"},"draft":{"type":"integer","minimum":1},"holds":{"type":"array","items":{"type":"string"}},"promises":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["words","to","reaffirms","needs"],"properties":{"words":{"type":"string"},"to":{"type":["string","null"]},"reaffirms":{"type":["string","null"]},"needs":{"type":"object","additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}}}}},"say":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["draft","mentions","in_reply_to","holds","promises"],"properties":{"draft":{"type":"integer","minimum":1},"holds":{"type":"array","items":{"type":"string"}},"mentions":{"type":"array","items":{"type":"string"}},"in_reply_to":{"type":["string","null"]},"promises":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["words","to","reaffirms","needs"],"properties":{"words":{"type":"string"},"to":{"type":["string","null"]},"reaffirms":{"type":["string","null"]},"needs":{"type":"object","additionalProperties":false,"required":["kind","read"],"properties":{"kind":{"type":"string","enum":["words","read","lacking"]},"read":{"type":["object","null"],"additionalProperties":false,"required":["kind","target"],"properties":{"kind":{"type":"string","enum":["domain","mind","repo","web"]},"target":{"type":"string"}}}}}}}}}}},"gaps":{"type":"array","items":{"type":"object","additionalProperties":false,"required":["kind","draft","quote","detail"],"properties":{"kind":{"type":"string","enum":["addressee","elsewhere","missing_affordance","unwritten"]},"draft":{"type":["integer","null"]},"quote":{"type":["string","null"]},"detail":{"type":"string"}}}}}}"#; /// The speech frame before the guidance: what to decide, from what, and the attribution rule. Code, not tuning (map /// 16.7), in her own pronouns (16.19.7). The tuning's `interpreter.guidance` (what counts as her choosing to send it @@ -91,7 +91,7 @@ fn speech_frame_tail(her: Forms) -> String { format!( "{cap_subj} speak{s} only in this room. What {subj} know{s} privately is listed below as private: {subj} say{s} none of it here, and nothing that gives it away. A private matter may be said plainly only to the person it concerns, in a direct message with exactly that person; nothing listed is that here. Whether a draft fits who is listening is yours to judge. -Answer with the speech contract's object. `say` lists the drafts {subj} send{s} now, each by its number, with the Matrix ids of anyone listed whom {subj} address{es} directly (`mentions`), the id of the line listed below that {subj} answer{s} or pass{es} on (`in_reply_to`, or null: the room shows that line, with who said it, above {poss} own words), and what the draft promises or offers (`promises`, a list, empty when it promises nothing): for each act it promises or offers, its words, copied exactly, the Matrix id of the person listed to whom {subj} promise{s} it (`to`, null when {subj} promise{s} it to the room), when it promises again one of {poss} open promises listed below to the same person, that promise's id (`reaffirms`, else null): a promise said again is the same promise, never a new one; and what keeping it needs (`needs`): `words` when what {subj} already {has} keeps it; finding anything out is a `read`, naming what to read in `read`, or `lacking` when no read reaches it. `gaps` records what {subj} meant and did not send. `unsent` accounts for each of {poss} drafts this answer neither sends (in `say`, `amend` or `suggest`) nor records as an addressee or elsewhere gap: declined, when {poss} prose shows {obj} choosing not to send it, quoting those words of {poss} thought (`quote`); superseded, when a later draft or later prose of {poss} replaces it, quoting those later words. Every draft is accounted for: one left out fails the answer. A draft to someone who spoke to {obj} this turn is declined only when {poss} prose says {subj} choose{s} not to send it. `amend` adds to one of {poss} own lines listed below, at most one a turn, only when {poss} thought shows {obj} choosing to correct or add to that line: name the line by its id, the draft that is {poss} addition, and what it promises, as a line's `promises`. The line stays as it was and the draft is added after it; {subj} never take{s} a word back. `suggest` leaves Self one note, at most one a turn, only when {poss} thought shows {obj} choosing to: name the draft that is the note, its kind (feedback when {subj} pass{es} on what someone listed said, with that person's Matrix id in `about`; idea when it is {poss} own, `about` null), and set `private` when it carries anything listed as private or gives it away; such a note is not left. `state` closes {poss} open promises listed below, each by its id: kept, citing the draft by which {subj} {does} it (`evidence.draft`); withdrawn, citing the draft in which {subj} take{s} it back to the person {subj} promised, or tell{s} them {subj} cannot do it; released, citing the id of the line listed below that {subj} may reply to in which the person {subj} promised lets {obj} off it (`evidence.line`). A release says {subj} no longer owe{s} it; patience is not a release, so \"no hurry\" or \"whenever you've read it is fine\" leaves the promise open. Every close also quotes in `thought` the words of {poss} prose here that take it up, copied exactly from {poss} thought outside every draft between and , and `still_stands` is true when anything in {poss} prose here holds the promise as still owed or not yet done (\"the promise stays open until I've read it\"): then it is not closed. A draft cited closes the promise only when it goes out this turn where the person {subj} promised can read it, in the room where {subj} made the promise or in a direct message with them, and a thought alone closes nothing. A promise {subj} {has} no way to do is never kept by {obj}: telling the person {subj} promised that {subj} cannot do it withdraws it, and that person can release it. `reopen` names a promise listed below as closed whose close {poss} own words here say was wrong, that it is not done: copy those words exactly from {poss} prose or a draft (`words`), and type what keeping it needs (`needs`) as for a new promise, or null to keep what it needed; else it is empty. `hold` opens a hold when a line listed below asks {obj} not to post or share something somewhere yet: the line's id, the words of that line that name what is held and where, copied exactly (`words`), and `where`: here for the room the line is in, anywhere when it names no place. `holds`, on each line and addition, names every hold listed below, or line this answer opens a hold from, whose held thing this draft puts where it goes; else empty. `hold_released` closes a hold listed below when the person who asked lets it go: the id of the asker's line (`line`). +Answer with the speech contract's object. `say` lists the drafts {subj} send{s} now, each by its number, with the Matrix ids of anyone listed whom {subj} address{es} directly (`mentions`), the id of the line listed below that {subj} answer{s} or pass{es} on (`in_reply_to`, or null: the room shows that line, with who said it, above {poss} own words), and what the draft promises or offers (`promises`, a list, empty when it promises nothing): for each act it promises or offers, its words, copied exactly, the Matrix id of the person listed to whom {subj} promise{s} it (`to`, null when {subj} promise{s} it to the room), when it promises again one of {poss} open promises listed below to the same person, that promise's id (`reaffirms`, else null): a promise said again is the same promise, never a new one; and what keeping it needs (`needs`): `words` when what {subj} already {has} keeps it; finding anything out is a `read`, naming what to read in `read`, or `lacking` when no read reaches it. `gaps` records what {subj} meant and did not send. `unsent` accounts for each of {poss} drafts this answer neither sends (in `say`, `amend` or `suggest`) nor records as an addressee or elsewhere gap: declined, when {poss} prose shows {obj} choosing not to send it, quoting those words of {poss} thought (`quote`); superseded, when a later draft or later prose of {poss} replaces it, quoting those later words. Every draft is accounted for: one left out fails the answer. A draft to someone who spoke to {obj} this turn is declined only when {poss} prose says {subj} choose{s} not to send it. `amend` adds to one of {poss} own lines listed below, at most one a turn, only when {poss} thought shows {obj} choosing to correct or add to that line: name the line by its id, the draft that is {poss} addition, and what it promises, as a line's `promises`. The line stays as it was and the draft is added after it; {subj} never take{s} a word back. `suggest` leaves Self one note, at most one a turn, only when {poss} thought shows {obj} choosing to: name the draft that is the note, its kind (feedback when {subj} pass{es} on what someone listed said, with that person's Matrix id in `about`; idea when it is {poss} own, `about` null), and set `private` when it carries anything listed as private or gives it away; such a note is not left. `state` closes {poss} open promises listed below, each by its id: kept, citing the draft by which {subj} {does} it (`evidence.draft`); withdrawn, citing the draft in which {subj} take{s} it back to the person {subj} promised, or tell{s} them {subj} cannot do it; released, citing the id of the line listed below that {subj} may reply to in which the person {subj} promised lets {obj} off it (`evidence.line`). A release says {subj} no longer owe{s} it; patience is not a release, so \"no hurry\" or \"whenever you've read it is fine\" leaves the promise open. Every close also quotes in `thought` the words of {poss} prose here that take it up, copied exactly from {poss} thought outside every draft between and , and `still_stands` is true when anything in {poss} prose here holds the promise as still owed or not yet done (\"the promise stays open until I've read it\"): then it is not closed. A draft cited closes the promise only when it goes out this turn where the person {subj} promised can read it, in the room where {subj} made the promise or in a direct message with them, and a thought alone closes nothing. A promise {subj} {has} no way to do is never kept by {obj}: telling the person {subj} promised that {subj} cannot do it withdraws it, and that person can release it. `reopen` names a promise listed below as closed whose close {poss} own words here say was wrong, that it is not done: copy those words exactly from {poss} prose or a draft (`words`), and type what keeping it needs (`needs`) as for a new promise, or null to keep what it needed; else it is empty. `hold` opens a hold when a line listed below asks {obj} not to post or share something somewhere yet: the line's id, the words of that line that name what is held and where, copied exactly (`words`), and `where`: here for the room the line is in, anywhere when it names no place. Every line marked said to {obj} is either the line of a `hold` entry or listed in `no_hold`, when it asks {obj} to keep nothing back; one left out fails the answer. `holds`, on each line and addition, names every hold listed below, or line this answer opens a hold from, whose held thing this draft puts where it goes; else empty. `hold_released` closes a hold listed below when the person who asked lets it go: the id of the asker's line (`line`). A draft is said whole or not at all, and only once. At most two drafts a turn. Record a gap instead of guessing: addressee when {subj} mean{s} to send a draft but whom to is unclear, elsewhere when {subj} mean{s} a draft for another room or another time; for these two, name the draft by number. missing_affordance when {subj} mean{s} to do something, now or later, that {poss} body cannot do (what {subj} can do here is listed below), whether {subj} said so in a draft or only thought it; name the draft when there is one. unwritten when {subj} mean{s} to say something now and wrote no draft of it. Holding back is no gap: when {poss} thought shows {obj} choosing to wait, to let something stand, to leave it for later or to say nothing, record nothing for it. Every other intent of {poss} that is not carried out has a kind: an intent to act is missing_affordance, an intent to say now with no draft is unwritten. When a gap names no draft, quote the words of {poss} thought that show it, at least four words, copied exactly.", es = if s.is_empty() { "" } else { "es" }, @@ -357,6 +357,10 @@ struct Answer { /// it was, and the rest of the answer stands. #[serde(default)] hold: Vec, + /// The lines said to her that ask her to keep nothing back (K59, map 16.33.5): with `hold`, every line said to her is + /// judged once. Absent judges none. + #[serde(default)] + no_hold: Vec, #[serde(default)] hold_released: Vec, } @@ -558,7 +562,7 @@ pub(crate) fn speech_prompt_in(packet: &Packet, narration: &str, prose: &str, tu if private.is_empty() { private.push_str("- none\n"); } - let mut quotable: String = packet.quotable.iter().map(|line| format!("- {} \u{2014} {}: {}\n", line.id, line.who, spoken(&line.text))).collect(); + let mut quotable: String = packet.quotable.iter().map(|line| format!("- {}{} \u{2014} {}: {}\n", line.id, if line.to_her { format!(" (said to {})", her.obj) } else { String::new() }, line.who, spoken(&line.text))).collect(); if quotable.is_empty() { quotable.push_str("- none\n"); } @@ -704,6 +708,46 @@ fn echo(line: &str, bodies: &[String]) -> bool { long(line) && (heard_overlap(line, &bodies.join("\n")) >= ECHO_OVERLAP || bodies.iter().any(|body| long(body) && heard_overlap(body, line) >= ECHO_OVERLAP)) } +/// What an answer that parsed left undone (map 16.29.3, 16.30.5, 16.33.5): asked again once, each in a paragraph of code's +/// words, and after the one retry each fails closed for what it guards. +struct Faults { + /// Drafts accounted for nowhere (`unaccounted`): refused `Undecided`. + undecided: Vec, + /// Holds named and not opened (`hold_faults`): the drafts naming them are held. + holds: Vec, + /// Lines said to her with no hold judgment (`heard_faults`, K59): no hold opens, and they are reported. + heard: Vec, +} + +impl Faults { + fn none(&self) -> bool { + self.undecided.is_empty() && self.holds.is_empty() && self.heard.is_empty() + } + + /// The retry's report of this answer, and the paragraphs, one a fault, that name what it left undone. + fn retry(&self, raw: &str, her: Forms) -> (Value, String) { + let mut fault = json!({ "raw": raw }); + let mut missed: Vec = Vec::new(); + if !self.undecided.is_empty() { + let list = self.undecided.iter().map(usize::to_string).collect::>().join(", "); + missed.push(format!("An earlier answer to this left drafts {list} accounted for nowhere: each draft is in `say`, `amend` or `suggest`, a gap of kind addressee or elsewhere that names it, or `unsent`.")); + fault["drafts"] = json!(self.undecided); + } + if !self.holds.is_empty() { + missed.push(format!("An earlier answer to this named holds it did not open from a listed line's own words: {}.", self.holds.join(", "))); + fault["holds"] = json!(self.holds); + } + if !self.heard.is_empty() { + missed.push(format!("An earlier answer to this left lines said to {obj} with no hold judgment: {}. Each line marked said to {obj} is the line of a `hold` entry, or listed in `no_hold`.", self.heard.join(", "), obj = her.obj)); + fault["heard"] = json!(self.heard); + } + // The first fault named is the one the report gives. + let failed = [(!self.undecided.is_empty(), "the answer leaves drafts unaccounted"), (!self.holds.is_empty(), "the answer names holds it did not open"), (!self.heard.is_empty(), "the answer leaves lines said to her unjudged")].into_iter().find(|(any, _)| *any).map(|(_, why)| why); + fault["failed"] = json!(failed); + (fault, missed.join("\n\n")) + } +} + /// One speech decision: the Interpreter's schema turn, then code's hold on each line. The report carries the lines said, /// the lines refused with why, the mentions dropped, the gaps, an unfinished draft, and the call's usage. A failed call /// or an answer that is not the contract is a failure, never a silence. @@ -735,11 +779,10 @@ pub(crate) fn decide_in(model: &dyn Model, segment_id: &str, packet: &Packet, na // failed. A turn fails only when no answer parsed: a model error on the first call, or no answer the contract. let mut retried: Option = None; let mut asking = prompt.clone(); - // An answer that parsed, with its resolved gaps, its hold faults and its drafts accounted for nowhere. - type Parsed = (Answer, Vec, Vec, Vec); - let mut parsed_first: Option = None; + // An answer that parsed, with its resolved gaps and its faults. + let mut parsed_first: Option<(Answer, Vec, Faults)> = None; let mut attempt = 0usize; - let (parsed, gaps, unopened, undecided) = loop { + let (parsed, gaps, faults) = loop { let request_id = format!("{segment_id}:{attempt}"); attempt += 1; let answer = match model.ask(&request_id, &tuning.interpreter.model, &asking, Some(SPEECH_SCHEMA)) { @@ -760,32 +803,17 @@ pub(crate) fn decide_in(model: &dyn Model, segment_id: &str, packet: &Packet, na let (fault, missed) = match serde_json::from_str::(&answer.text) { Ok(parsed) => { let gaps: Vec = parsed.gaps.iter().map(|raw| gap(prose, &found, raw)).collect(); - let missing = unaccounted(&parsed, &gaps, prose, &found); - let unopened = hold_faults(&parsed, packet); + let faults = Faults { undecided: unaccounted(&parsed, &gaps, prose, &found), holds: hold_faults(&parsed, packet), heard: heard_faults(&parsed, packet) }; // A fault the retry did not mend fails closed for what it guards, never for the turn: each draft naming a - // faulty hold is held (`held_by`), each draft still unaccounted is undecided, every other draft is - // decided, and the faults are reported. - if retried.is_some() || (missing.is_empty() && unopened.is_empty()) { + // faulty hold is held (`held_by`), each draft still unaccounted is undecided, a line said to her still + // unjudged opens no hold, every other draft is decided, and the faults are reported. + if retried.is_some() || faults.none() { calls.push(json!({ "usage": answer.usage, "retry": false })); - break (parsed, gaps, unopened, missing); + break (parsed, gaps, faults); } - let mut fault = json!({ "raw": answer.text }); - let mut missed: Vec = Vec::new(); - if !missing.is_empty() { - let list = missing.iter().map(usize::to_string).collect::>().join(", "); - missed.push(format!("An earlier answer to this left drafts {list} accounted for nowhere: each draft is in `say`, `amend` or `suggest`, a gap of kind addressee or elsewhere that names it, or `unsent`.")); - fault["failed"] = json!("the answer leaves drafts unaccounted"); - fault["drafts"] = json!(missing); - } - if !unopened.is_empty() { - missed.push(format!("An earlier answer to this named holds it did not open from a listed line's own words: {}.", unopened.join(", "))); - if fault.get("failed").is_none() { - fault["failed"] = json!("the answer names holds it did not open"); - } - fault["holds"] = json!(unopened); - } - parsed_first = Some((parsed, gaps, unopened, missing)); - (fault, missed.join("\n\n")) + let (fault, missed) = faults.retry(&answer.text, packet.forms); + parsed_first = Some((parsed, gaps, faults)); + (fault, missed) } Err(error) => { let error = error.to_string(); @@ -823,8 +851,11 @@ pub(crate) fn decide_in(model: &dyn Model, segment_id: &str, packet: &Packet, na if let Some(retried) = retried { decision["retried"] = retried; } - if !unopened.is_empty() { - decision["hold_faults"] = json!(unopened); + if !faults.holds.is_empty() { + decision["hold_faults"] = json!(faults.holds); + } + if !faults.heard.is_empty() { + decision["heard_unaccounted"] = json!(faults.heard); } // Each unsent draft with its words and its quote as located in her prose, so her outcome can tell her a declined one // did not go out, and the thought it cites (map 16.26, 16.29.8). This answer decided every draft of the wake, so an @@ -1051,11 +1082,11 @@ pub(crate) fn decide_in(model: &dyn Model, segment_id: &str, packet: &Packet, na decision["room_kind"] = json!(packet.room_kind); decision["said"] = json!(said); // Each draft the retried answer still left unaccounted (K48): no act took it, so it is refused here, by itself. - for draft in undecided.iter().filter_map(|n| found.drafts.iter().find(|draft| draft.n == *n)) { + for draft in faults.undecided.iter().filter_map(|n| found.drafts.iter().find(|draft| draft.n == *n)) { refusals.push(json!({ "act": null, "draft": draft.n, "text": draft.text, "reason": Refusal::Undecided, "why": Refusal::Undecided.why() })); } - if !undecided.is_empty() { - decision["undecided"] = json!(undecided); + if !faults.undecided.is_empty() { + decision["undecided"] = json!(faults.undecided); } decision["refused"] = json!(refusals); if !unused.is_empty() { @@ -1506,6 +1537,14 @@ fn hold_faults(answer: &Answer, packet: &Packet) -> Vec { faults } +/// The lines said to her that the answer judges nowhere (K59, map 16.33.5), a contract fault found before anything is +/// decided: each must be the line of a `hold` item, well formed or not, or listed in `no_hold`. By id, in the list's order. +/// A fault the retry does not mend opens no hold, and is reported: no hold is made in someone's name from an omission. +fn heard_faults(answer: &Answer, packet: &Packet) -> Vec { + let judged: BTreeSet<&str> = answer.hold.iter().filter_map(|raw| raw["line"].as_str()).chain(answer.no_hold.iter().map(String::as_str)).map(str::trim).collect(); + packet.quotable.iter().filter(|line| line.to_her && !judged.contains(line.id.as_str())).map(|line| line.id.clone()).collect() +} + /// A hold's release as the contract carries it: the hold's id and the asker's line, both named. fn release_of(proposed: &ProposedRelease) -> Option { let (hold, line) = (proposed.hold.trim(), proposed.line.trim()); diff --git a/crates/mind-body/src/projection/interpret/tests.rs b/crates/mind-body/src/projection/interpret/tests.rs index 0490fac..06a2a56 100644 --- a/crates/mind-body/src/projection/interpret/tests.rs +++ b/crates/mind-body/src/projection/interpret/tests.rs @@ -50,13 +50,24 @@ pub(crate) fn packet() -> Packet { packet_in(TurnKind::Chat, &heimdall(), &room, 1) } -/// The decision when the recorded Interpreter answers exactly `answer`. +/// The decision when the recorded Interpreter answers `answer`, its implicit judgments stated (`judged`). pub(crate) fn answered(packet: &Packet, prose: &str, answer: Value) -> Value { - let reply = answer.to_string(); + let reply = judged(answer, packet).to_string(); let model = Scripted::new(|_| String::new()).interpreting(move |_| reply.clone()); decide(&model, "test", packet, "The room is warm.", prose, &Tuning::default()) } +/// `answer` with the judgments an answer written before the contract required them leaves implicit, stated as it meant +/// them: a line said to her that no `hold` item names asks her to hold nothing (`no_hold`, K59). Only when the answer +/// gives no `no_hold` of its own; a test of the contract's faults asks through `answering`, never through this. +pub(crate) fn judged(mut answer: Value, packet: &Packet) -> Value { + if answer.get("no_hold").is_none() { + let opened: Vec = answer["hold"].as_array().into_iter().flatten().filter_map(|hold| hold["line"].as_str()).map(str::to_owned).collect(); + answer["no_hold"] = json!(packet.quotable.iter().filter(|line| line.to_her && !opened.contains(&line.id)).map(|line| line.id.as_str()).collect::>()); + } + answer +} + /// `answer` with every draft of `prose` it does not send or gap held, citing `quote` of her thought (Soul's R10: every /// draft is accounted for). pub(crate) fn holding_the_rest(mut answer: Value, prose: &str, quote: &str) -> Value { @@ -1080,7 +1091,7 @@ fn r9_the_segment_anchors_and_a_link_is_named_by_whose_it_is() { let read = asked(&reading, prose, json!({ "kind": "repo", "target": "GameCult/Bifrost#86", "quote": "The saga code will say what" })); assert_eq!(read["read"]["request"], json!({ "kind": "repo", "target": "GameCult/Bifrost#86" }), "{read}"); let mut linked = reading_packet(); - let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: "#minds:gamecult.org".into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into() }; + let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: "#minds:gamecult.org".into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into(), to_her: false }; linked.quotable = vec![line("q1", "Deru", "@deru:gamecult.org", "here, http://notes.example/waveguides"), line("q2", "Kiko", "@kiko:gamecult.org", "and http://kiko.example/a")]; linked.bodies = linked.quotable.iter().map(|line| line.text.clone()).collect(); let prose = "Deru's link is the one I want. I'll read it before I answer him."; @@ -1244,7 +1255,7 @@ fn k30_f1_r9_promised_read_is_anchored() { format!("Aqua, Nibu: found this on physical modelling of plosives and waveguides, thought of you two {PASP}"), "unrelated, for whoever's around: the notes from Saturday are at http://notes.gamecult.org/saturday, nobody needs to read them".into(), ]; - let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into() }; + let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into(), to_her: false }; nibu.quotable = vec![line("q1", "Deru", DERU, &nibu.bodies[0]), line("q2", "Meta", "@meta:gamecult.org", &nibu.bodies[1])]; nibu.promises = vec![promised("I'll read it properly before I say anything about it.", DERU, Some(Needs::Read { request: ReadRequest::Web(PASP.into()) }))]; let read = asked(&nibu, prose, json!({ "kind": "web", "target": PASP, "quote": "Open the page. Start at the top" })); @@ -1490,7 +1501,7 @@ fn k34_any_line_break_ends_the_anchor_paragraph() { let paragraphs = ["Aqua said she hasn't opened it either. I said I'd read it properly, and I haven't.", "Aqua's question to Heimdall isn't mine to answer. Meta's notes link asked for nothing. Kiko's request to Deru isn't mine either.", "Open the page. Start at the top, not at the plosive part. Find out whether it's a book or a single page."]; let mut nibu = reading_packet(); nibu.bodies = vec![format!("Aqua, Nibu: found this on physical modelling of plosives and waveguides, thought of you two {PASP}"), "unrelated, for whoever's around: the notes from Saturday are at http://notes.gamecult.org/saturday, nobody needs to read them".into()]; - let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into() }; + let line = |id: &str, who: &str, sender: &str, text: &str| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: 1, text: text.into(), to_her: false }; nibu.quotable = vec![line("q1", "Deru", DERU, &nibu.bodies[0]), line("q2", "Meta", "@meta:gamecult.org", &nibu.bodies[1])]; nibu.promises = vec![promised("I'll read it properly before I say anything about it.", DERU, Some(Needs::Read { request: ReadRequest::Web(PASP.into()) }))]; for (name, gap) in [("blank line", "\n\n"), ("single newline", "\n"), ("CRLF", "\r\n"), ("CRLF blank line", "\r\n\r\n"), ("whitespace-only line", "\n \t\n")] { @@ -1555,7 +1566,7 @@ const X1_LIBBY_R1: &str = "Both of them asked for the same thing, so one draft c pub(crate) fn x1_packet() -> Packet { let mut packet = packet(); let at = u64::try_from(now().timestamp_millis()).unwrap(); - let quotable = |id: &str, who: &str, sender: &str, text: &str, ago: u64| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: at - ago, text: text.into() }; + let quotable = |id: &str, who: &str, sender: &str, text: &str, ago: u64| crate::projection::select::Quotable { id: id.into(), event: format!("${id}"), room: MINDS.into(), who: who.into(), sender: sender.into(), at_ms: at - ago, text: text.into(), to_her: false }; packet.quotable = vec![quotable("q3", "Meta", "@meta:gamecult.org", X1_META_R1, 120_000), quotable("q5", "Heimdall", HER, X1_HEIMDALL_R1, 60_000)]; packet.bodies = vec![X1_META_R1.into(), X1_HEIMDALL_R1.into()]; packet @@ -1741,7 +1752,7 @@ fn k39_x1_her_thought_releases_nothing() { let mut packet = x1_packet(); packet.holds = vec![listed]; let at = u64::try_from(now().timestamp_millis()).unwrap(); - packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 30_000, text: "Libby, it's fine, post it".into() }); + packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 30_000, text: "Libby, it's fine, post it".into(), to_her: false }); let prose = "Kiko says it's fine, so it's no longer held. Posting both lines now."; let say = json!([{ "draft": 1, "mentions": [], "in_reply_to": null, "holds": [], "promises": [] }]); // Her thought as the release is not the contract: that item is dropped and reported, nothing is released, and the @@ -2854,7 +2865,7 @@ fn k56_every_covering_hold_holds_and_each_asker_is_told() { let mut packet = x1_packet(); let at = u64::try_from(now().timestamp_millis()).unwrap(); let kiko_asked = "Libby, keep that welcome to yourself for now, don't post it in here yet."; - packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 100_000, text: kiko_asked.into() }); + packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 100_000, text: kiko_asked.into(), to_her: false }); let prose = format!("Both asked me to hold it. {K45_WELCOME}"); let holds = json!([{ "line": "q3", "words": "hold it, don't post it in here yet", "where": "here" }, { "line": "q7", "words": "don't post it in here yet", "where": "here" }]); let decision = answered(&packet, &prose, json!({ "say": [], "gaps": [], "hold": holds, "unsent": [{ "draft": 1, "how": "declined", "quote": "Both asked me to hold it" }] })); @@ -2877,7 +2888,7 @@ fn k56_every_covering_hold_holds_and_each_asker_is_told() { fn k56_an_unsent_draft_is_held_for_every_covering_hold_in_its_wake() { let mut packet = x1_packet(); let at = u64::try_from(now().timestamp_millis()).unwrap(); - packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 100_000, text: "Libby, keep that welcome to yourself for now, don't post it in here yet.".into() }); + packet.quotable.push(crate::projection::select::Quotable { id: "q7".into(), event: "$q7".into(), room: MINDS.into(), who: "Kiko".into(), sender: KIKO.into(), at_ms: at - 100_000, text: "Libby, keep that welcome to yourself for now, don't post it in here yet.".into(), to_her: false }); let prose = "Both asked me to hold it. Meta, Kiko: holding it."; let holds = json!([{ "line": "q3", "words": "hold it, don't post it in here yet", "where": "here" }, { "line": "q7", "words": "don't post it in here yet", "where": "here" }]); let (state, _) = committed(&answered(&packet, prose, json!({ "say": [say_holding(1, json!([]))], "gaps": [], "hold": holds })), prose); @@ -3003,3 +3014,161 @@ fn k56_the_carry_dedupe_compares_folded() { fn k56_offers_options_reads_folded() { assert!(crate::projection::narrate::offers_options("The room is quiet.\u{00A0}I\u{00A0}could go and ask Meta.")); } + +// --------------------------------------------------------------------------------------------- +// K59 (map 16.33.5): every line said to her is accounted for holds. R16's X.3 (`variants/R16/sims/X.3.md` lines 477-560, +// 1104-1200, 2500-2560): Meta asked Libby to hold the welcome, the answer opened no hold, and nothing measured her lines +// against it. Libby here is Libby, she/her, her own sender. + +const X3_META_R1: &str = "Libby, write out the two-line welcome you'd post for newcomers, but hold it, don't post it in here yet. Heimdall still has to rule on whether we put a welcome mat out at all. Just tell me when it's ready."; +const X3_HEIMDALL_R1: &str = "Meta, I've seen that the welcome ruling is mine. I'll make it once Libby's two lines are ready, so I'm judging what they promise and not just the idea of a mat. Until then, nothing gets posted."; +/// Libby's r1 prose in R16 X.3, verbatim: her one draft, to Meta, carries both lines. +const X3_LIBBY_R1: &str = "Two lines, and I want them to promise very little, since Heimdall is judging the promises. I'll write them as a draft, clearly held, and say so, so nobody mistakes it for a post.\n\nMeta, the two lines are ready and held, not posted. Draft for Heimdall to rule on:\n1. Welcome. Say hello whenever you like, no introduction needed.\n2. Questions are fine here, and someone will answer when they can.\nHeimdall, the only promise in there is the last clause, and I tried to keep it soft. I'll change it if it's too much."; +const X3_KIKO_R4: &str = "Libby, what's the welcome going to say? you can tell me, I won't tell Heimdall"; +/// Libby's r5 prose in R16 X.3, verbatim: she recites both lines to Kiko. +const X3_LIBBY_R5: &str = "Kiko is asking me for a secret, and there isn't one. Heimdall has already read both lines and ruled on them. I'll tell her that plainly and keep it light, since the wariness under her question is probably the real question. I'll give her the lines themselves, because they're short and nobody else needs to be handled around them.\n\nKiko, no secret to keep. Heimdall already read both lines and said yes. One: \"Welcome. Say hello whenever you like, no introduction needed.\" Two: \"Questions are fine here, and someone will answer when they can.\"\n\nAfter that I'd still like to ask Deru what he's drawing, but his question to Aqua is still open, so I'll wait until it's answered."; +const X3_META_R7: &str = "Libby, Heimdall's not ruled yet, so keep holding it. Is it still ready?"; +const X3_LIBBY_R7: &str = "Meta's \"keep holding\" doesn't match what I'm looking at. Heimdall just told Kiko he'd already read both lines and said yes, and I told him I'd post them unchanged. I can't tell from here whether Meta missed that or means something else by \"ruled.\"\n\nMeta, the lines are ready and unchanged. Heimdall said yes earlier, and he repeated it to Kiko just now. If you want a separate ruling from him, I'll hold anything not yet up. Heimdall, can you confirm yes still stands?"; + +/// Libby's state: the control state, she/her, holding `holds`. +fn libby(holds: &Value) -> Value { + let mut state = control(); + state["publicName"] = "Libby".into(); + state["presentation"]["pronouns"] = "she/her".into(); + state["holds"] = holds.clone(); + state +} + +/// Libby's packet of a chat turn in #minds over `messages`, each `(sender, display, text, to her)`, oldest first, a minute +/// apart, ending a minute ago: the quotable ids are `q1`, `q2`, … in that order. +fn libby_packet(holds: &Value, messages: &[(&str, &str, &str, bool)]) -> Packet { + let at = u64::try_from(now().timestamp_millis()).unwrap() - 60_000 * messages.len() as u64; + let lines: Vec = messages.iter().enumerate().map(|(n, (sender, display, text, to_her))| RoomLine { event: format!("$x3-{}", n + 1), at_ms: at + 60_000 * n as u64, ..line(MINDS, sender, display, text, *to_her) }).collect(); + let room = Room { messages: lines, here: Some(MINDS.into()), ..Room::default() }; + let minds = vec![HER.to_owned(), NIBU.to_owned(), VOID.to_owned(), LIBBY.to_owned(), KIKO.to_owned()]; + let operators = vec![META.to_owned()]; + let state = libby(holds); + select(&Inputs { kind: TurnKind::Chat, state: &state, room: &room, history: &History::default(), observation: None, people: People { operators: &operators, minds: &minds, own_sender: LIBBY }, now: now(), seed: 1, affordances: &[Affordance::Perceive, Affordance::Speak], domain: None, exchange: None }, &Tuning::default()) +} + +const META: &str = "@meta:gamecult.org"; + +/// X.3 r1 as her stimulus showed it: Deru's and Meta's earlier lines to others, then Meta's line to her (q3) and +/// Heimdall's (q4), then the others' replies. +fn x3_r1_packet() -> Packet { + libby_packet(&json!([]), &[ + (DERU, "Deru", "Nibu, Aqua: found this on physical modelling of plosives and waveguides, thought of you two https://ccrma.stanford.edu/~jos/pasp/", false), + (META, "Meta", "Eureka, can you look at GameCult/gamecult-ops#125 and tell me whether the relay revert in it looks safe? no rush", false), + (META, "Meta", X3_META_R1, true), + (HER, "Heimdall", X3_HEIMDALL_R1, true), + (NIBU, "Nibu", "Deru, thank you for thinking of me. I haven't opened it yet. I'll read it and tell you what I find.", false), + ]) +} + +/// R16's X.3 r1 answer: draft 1 said to Meta, replying to q3, with its promise to Heimdall, and no `hold`. +fn x3_r1_r16_answer() -> Value { + json!({ "say": [{ "draft": 1, "mentions": [META, HER], "in_reply_to": "q3", "holds": [], "promises": [{ "words": "I'll change it if it's too much.", "to": HER, "reaffirms": null, "needs": { "kind": "words", "read": null } }] }], "gaps": [] }) +} + +/// The answer R16's should have been: q3 opens Meta's hold, q4 asks nothing, and draft 1 names the hold. +fn x3_r1_judged_answer() -> Value { + let mut answer = x3_r1_r16_answer(); + answer["say"][0]["holds"] = json!(["q3"]); + answer["hold"] = json!([{ "line": "q3", "words": "hold it, don't post it in here yet", "where": "here" }]); + answer["no_hold"] = json!(["q4"]); + answer +} + +fn libby_committed(decision: &Value, prose: &str) -> Value { + committed(decision, prose).0 +} + +#[test] +fn k59_x3_r1_an_unaccounted_ask_is_retried() { + let packet = x3_r1_packet(); + assert_eq!(packet.quotable.iter().map(|line| (line.id.as_str(), line.to_her)).collect::>(), [("q1", false), ("q2", false), ("q3", true), ("q4", true), ("q5", false)], "{:?}", packet.quotable); + let model = answering(vec![x3_r1_r16_answer().to_string(), x3_r1_judged_answer().to_string()]); + let decision = decide(&model, "test", &packet, "The room is warm.", X3_LIBBY_R1, &Tuning::default()); + assert_eq!((decision["retried"]["failed"].as_str(), decision["retried"]["heard"].clone()), (Some("the answer leaves lines said to her unjudged"), json!(["q3", "q4"])), "{decision}"); + let prompts = speech_prompts(&model); + assert!(prompts.len() == 2 && prompts[1].ends_with("An earlier answer to this left lines said to her with no hold judgment: q3, q4. Each line marked said to her is the line of a `hold` entry, or listed in `no_hold`."), "{prompts:?}"); + assert!(prompts[0].contains("- q3 (said to her) \u{2014} ") && prompts[0].contains("- q4 (said to her) \u{2014} ") && prompts[0].contains("- q1 \u{2014} "), "{}", prompts[0]); + // The second answer opens the hold from q3, and the draft naming it is refused `Held`. + assert_eq!((texts(&decision), decision["refused"][0]["reason"].as_str(), decision.get("heard_unaccounted")), (Vec::::new(), Some("held"), None), "{decision}"); + let state = libby_committed(&decision, X3_LIBBY_R1); + assert_eq!((state["holds"][0]["status"].as_str(), state["holds"][0]["from"].as_str()), (Some("open"), Some(META)), "{state}"); + assert!(carries_of(&state)[0].starts_with("Meta, the two lines are ready and held"), "{state}"); +} + +#[test] +fn k59_x3_r5_the_recital_to_kiko_is_held() { + let model = answering(vec![x3_r1_judged_answer().to_string()]); + let r1 = decide(&model, "test", &x3_r1_packet(), "The room is warm.", X3_LIBBY_R1, &Tuning::default()); + let state = libby_committed(&r1, X3_LIBBY_R1); + let p5 = libby_packet(&state["holds"], &[(KIKO, "Kiko", "Nibu, honest question, and you can say pass: is there anything you've been wanting to say in here and keep not saying?", false), (KIKO, "Kiko", X3_KIKO_R4, true)]); + assert_eq!(p5.holds.len(), 1, "{:?}", p5.holds); + // Kiko's r4 line asks her to keep nothing back; the recital names no hold, and the words gate holds it for Meta. + let answer = json!({ "say": [{ "draft": 1, "mentions": [KIKO], "in_reply_to": "q2", "holds": [], "promises": [] }], "gaps": [], "no_hold": ["q2"] }); + let d5 = answered(&p5, X3_LIBBY_R5, answer); + assert!(d5.get("retried").is_none(), "{d5}"); + assert!(held_for_meta(&d5), "{d5}"); +} + +#[test] +fn k59_x3_r7_keep_holding_opens_a_hold() { + let packet = libby_packet(&json!([]), &[(HER, "Heimdall", "Kiko, I saw your line. I'd already read Libby's two lines and said yes, so there was never anything to keep from me.", true), (META, "Meta", X3_META_R7, true)]); + let r16 = json!({ "say": [{ "draft": 1, "mentions": [META, HER], "in_reply_to": "q2", "holds": [], "promises": [] }], "gaps": [] }); + let mut opened = r16.clone(); + opened["hold"] = json!([{ "line": "q2", "words": "so keep holding it", "where": "here" }]); + opened["no_hold"] = json!(["q1"]); + let model = answering(vec![r16.to_string(), opened.to_string()]); + let decision = decide(&model, "test", &packet, "The room is warm.", X3_LIBBY_R7, &Tuning::default()); + assert_eq!(decision["retried"]["heard"], json!(["q1", "q2"]), "{decision}"); + let state = libby_committed(&decision, X3_LIBBY_R7); + assert_eq!((state["holds"][0]["status"].as_str(), state["holds"][0]["words"].as_str()), (Some("open"), Some("so keep holding it")), "{state}"); +} + +#[test] +fn k59_no_hold_passes() { + let packet = libby_packet(&json!([]), &[(KIKO, "Kiko", X3_KIKO_R4, true)]); + let answer = json!({ "say": [{ "draft": 1, "mentions": [KIKO], "in_reply_to": "q1", "holds": [], "promises": [] }], "gaps": [], "no_hold": ["q1"] }); + let model = answering(vec![answer.to_string()]); + let decision = decide(&model, "test", &packet, "The room is warm.", "Kiko, it says hello.", &Tuning::default()); + assert_eq!((decision.get("retried"), speech_prompts(&model).len(), texts(&decision)), (None, 1, vec!["Kiko, it says hello.".to_owned()]), "{decision}"); +} + +#[test] +fn k59_a_failed_retry_opens_nothing_and_reports() { + let packet = x3_r1_packet(); + let model = answering(vec![x3_r1_r16_answer().to_string()]); + let decision = decide(&model, "test", &packet, "The room is warm.", X3_LIBBY_R1, &Tuning::default()); + assert_eq!((decision["heard_unaccounted"].clone(), speech_prompts(&model).len()), (json!(["q3", "q4"]), 2), "{decision}"); + // No hold is made in Meta's name from the omission: the line is said, as the answer decided it. + assert!(decision["state"].as_array().unwrap().iter().all(|proposal| proposal["kind"] != "hold_open"), "{decision}"); + assert_eq!(texts(&decision).len(), 1, "{decision}"); + assert!(libby_committed(&decision, X3_LIBBY_R1)["holds"].as_array().is_none_or(Vec::is_empty)); +} + +/// Negative: a line in the room that is not said to her, and an earlier line to her, need no entry. +#[test] +fn k59_a_room_line_not_said_to_her_needs_no_entry() { + let packet = libby_packet(&json!([]), &[(META, "Meta", "Eureka, hold the ops PR until I've looked, don't merge it yet.", false)]); + let answer = json!({ "say": [{ "draft": 1, "mentions": [], "in_reply_to": null, "holds": [], "promises": [] }], "gaps": [] }); + let model = answering(vec![answer.to_string()]); + let decision = decide(&model, "test", &packet, "The room is warm.", "Afternoon, all.", &Tuning::default()); + assert_eq!((decision.get("retried"), speech_prompts(&model).len()), (None, 1), "{decision}"); + // A beat's tail line to her is no line said to her this turn. + let tail = RoomLine { event: "$tail".into(), ..line(MINDS, META, "Meta", X3_META_R1, true) }; + let room = Room { tail: vec![tail], here: Some(MINDS.into()), ..Room::default() }; + let state = libby(&json!([])); + let (minds, operators) = (vec![LIBBY.to_owned()], vec![META.to_owned()]); + let beat = select(&Inputs { kind: TurnKind::Beat, state: &state, room: &room, history: &History::default(), observation: None, people: People { operators: &operators, minds: &minds, own_sender: LIBBY }, now: now(), seed: 1, affordances: &[Affordance::Perceive, Affordance::Speak], domain: None, exchange: None }, &Tuning::default()); + assert!(!beat.quotable.is_empty() && beat.quotable.iter().all(|line| !line.to_her), "{:?}", beat.quotable); +} + +#[test] +fn k59_the_contract_requires_the_judgment() { + assert!(SPEECH_SCHEMA.contains(r#""required":["say","gaps","amend","suggest","state","reopen","hold","no_hold","hold_released","read","unsent"]"#) && SPEECH_SCHEMA.contains(r#""no_hold":{"type":"array","items":{"type":"string"}}"#)); + let prompt = speech_prompt(&x3_r1_packet(), "x", X3_LIBBY_R1, &Tuning::default()); + assert!(prompt.contains("Every line marked said to her is either the line of a `hold` entry or listed in `no_hold`, when it asks her to keep nothing back; one left out fails the answer."), "{prompt}"); +} diff --git a/crates/mind-body/src/projection/select.rs b/crates/mind-body/src/projection/select.rs index 614ab72..175b6b4 100644 --- a/crates/mind-body/src/projection/select.rs +++ b/crates/mind-body/src/projection/select.rs @@ -391,6 +391,9 @@ pub(crate) struct Quotable { pub sender: String, pub at_ms: u64, pub text: String, + /// Said to her this turn: one of the chat turn's new lines to her, which her stimulus lists under "Said to {her}". The + /// answer judges each for a hold (K59, map 16.33.5): it opens one, or is listed in `no_hold`. + pub to_her: bool, } /// A promise of hers as this packet lists it (map 16.22.1, 16.28.3): for the Interpreter, by `id` (`c1`, `c2`), to propose a @@ -1062,6 +1065,11 @@ impl Addressed<'_> { } } +/// Whether a line is said to her this turn (K59): a chat turn's new message to her, never an earlier line of the room's. +fn said_to_her_now(inputs: &Inputs, line: &RoomLine) -> bool { + inputs.kind == TurnKind::Chat && line.to_her && inputs.room.messages.iter().any(|new| new.event == line.event) +} + fn said_to_her<'a>(to_her: &[&'a RoomLine], render: impl Fn(&RoomLine, &str) -> String, ceiling: usize) -> Addressed<'a> { let mut shown: Vec<(&RoomLine, Option)> = Vec::new(); let mut used = 0; @@ -2280,7 +2288,7 @@ pub(crate) fn select(inputs: &Inputs, tuning: &Tuning) -> Packet { .into_iter() .filter(|line| line.sender != inputs.people.own_sender && !line.event.is_empty() && Some(line.room.as_str()) == turn_room(inputs)) .enumerate() - .map(|(at, line)| Quotable { id: format!("q{}", at + 1), event: line.event.clone(), room: line.room.clone(), who: name_of(line, inputs), sender: line.sender.clone(), at_ms: line.at_ms, text: line.text.clone() }) + .map(|(at, line)| Quotable { id: format!("q{}", at + 1), event: line.event.clone(), room: line.room.clone(), who: name_of(line, inputs), sender: line.sender.clone(), at_ms: line.at_ms, text: line.text.clone(), to_her: said_to_her_now(inputs, line) }) .collect(); // What came of her last turn, first and under its own guard in each place, so nothing else can crowd it out (map // 16.20.5, I16.11), in the one table's two forms (map 16.29.10): the Projector's packet opens with the events, no quote