diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index a687a1b..08f938c 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -422,7 +422,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni // Never cut inside a draft (Soul's R5): a draft the cap cut off is told to her as unfinished, cut by the cap. let capped = cap_prose(&first.answer.text, tuning.persona.words); let mut prose = capped.text; - let mut cut_drafts: Vec = capped.cut_draft.into_iter().collect(); + let mut cut_drafts: Vec = capped.cut_drafts; // Each continuation's call, its usage under `usage` so the report's totals count it (Soul: 72 of 76 calls counted). let mut continuations: Vec = Vec::new(); render["persona"] = json!({ "words": words(&prose), "cut_at_budget": capped.cut, "usage": first.answer.usage, "prose": prose }); @@ -481,7 +481,7 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni }; let capped = cap_prose(&continued.answer.text, tuning.persona.words); let segment = capped.text; - cut_drafts.extend(capped.cut_draft); + cut_drafts.extend(capped.cut_drafts); continuations.push(json!({ "usage": continued.answer.usage, "words": words(&segment), "cut_at_budget": capped.cut })); turns.push(Turn::Assistant { text: segment.clone(), states: continued.states }); before = format!("{before}{}\n\n{bridge}\n\n", &prose[from..]); @@ -499,8 +499,8 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni decision = next; } // A draft the cap cut off before it closed: never in her stream, and told to her as unfinished, cut by the cap. - if let Some(last) = cut_drafts.last().filter(|_| decision.get("failed").is_none()) { - decision["unfinished"] = json!(last); + // Every draft the cap cut off: never in her stream, each told to her as cut by the length limit (`outcome`). + if !cut_drafts.is_empty() && decision.get("failed").is_none() { decision["cut_by_cap"] = json!(cut_drafts); } if let Some(cap) = ended { diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs index 8c266af..0b10092 100644 --- a/crates/mind-body/src/projection/harness/tests.rs +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -510,7 +510,7 @@ fn k24_the_reply_after_a_read_gets_her_whole_budget_and_a_runaway_draft_is_cut_b assert_eq!(decision["reads"][0]["ok"], true, "a first segment at her whole budget still leaves the reply its own: {decision}"); assert_eq!(persona["words"].as_u64(), Some(words as u64), "her first segment whole; the runaway draft is not in her stream: {persona}"); assert!(!persona["prose"].as_str().unwrap().contains(""), "no unclosed draft in her stream: {persona}"); - assert!(decision["unfinished"].as_str().is_some_and(|text| text.starts_with("Deru, I opened it")) && decision["cut_by_cap"].is_array(), "{decision}"); + assert!(decision["cut_by_cap"][0].as_str().is_some_and(|text| text.starts_with("Deru, I opened it")), "{decision}"); let told = crate::projection::outcome::outcome_of(&json!({ "interpreter": decision, "posted": [] })).unwrap().lines(crate::projection::select::forms(&control())); assert!(told.iter().any(|line| line.contains("finished writing")), "she is told: {told:?}"); } @@ -560,10 +560,10 @@ fn r5_the_cap_never_cuts_inside_a_draft() { let list: String = (1..=60).map(|n| format!("item{n} is fine; ")).collect(); let answer = format!("Meta asked for the list, so here it is. {}Meta, the list: {list}that is all. Done.", "thinking ".repeat(20)); let capped = crate::projection::narrate::cap_prose(&answer, words); - assert!(capped.text.ends_with("that is all.") && capped.cut_draft.is_none(), "the draft whole: {}", capped.text); + assert!(capped.text.ends_with("that is all.") && capped.cut_drafts.is_empty(), "the draft whole: {}", capped.text); let runaway = format!("Lead-in words here. Meta, {}", "word ".repeat(2 * words + 10)); let cut = crate::projection::narrate::cap_prose(&format!("{runaway}"), words); - assert_eq!((cut.text.as_str(), cut.cut_draft.as_deref().is_some_and(|draft| draft.starts_with("Meta, word"))), ("Lead-in words here.", true), "{cut:?}"); + assert_eq!((cut.text.as_str(), cut.cut_drafts.first().is_some_and(|draft| draft.starts_with("Meta, word"))), ("Lead-in words here.", true), "{cut:?}"); // Through the turn: the list is said whole. let fixture = room("operator-warm.toml"); let reply = answer.clone(); @@ -583,10 +583,10 @@ fn r6_no_draft_is_dropped_silently_at_the_cap() { let draft: String = (1..=62).map(|n| format!("w{n} ")).collect(); let late = format!("{thought} Meta, {draft}done"); let capped = crate::projection::narrate::cap_prose(&late, words); - assert!(capped.text.ends_with("done") && capped.cut_draft.is_none(), "kept whole to its closing mark: {capped:?}"); + assert!(capped.text.ends_with("done") && capped.cut_drafts.is_empty(), "kept whole to its closing mark: {capped:?}"); let after: String = (1..=190).map(|n| format!("t{n} ")).collect(); let beyond = crate::projection::narrate::cap_prose(&format!("{after}Meta, later words."), words); - assert_eq!((beyond.text.contains(""), beyond.cut_draft.as_deref()), (false, Some("Meta, later words.")), "{beyond:?}"); + assert_eq!((beyond.text.contains(""), beyond.cut_drafts.first().map(String::as_str)), (false, Some("Meta, later words.")), "{beyond:?}"); // Through the turn: she is told the length limit cut it. let fixture = room("operator-warm.toml"); let reply = format!("{after}Meta, later words."); @@ -595,3 +595,20 @@ fn r6_no_draft_is_dropped_silently_at_the_cap() { let told = crate::projection::outcome::outcome_of(&json!({ "interpreter": report["renders"][0]["interpreter"], "posted": [] })).unwrap().lines(crate::projection::select::forms(&control())); assert!(told.iter().any(|line| line.contains("the length limit cut it before he finished writing it") && line.contains("Meta, later words.")), "{told:?}"); } + +/// Soul's R7: a back-off point inside a closed draft moves to its closing mark, so a short finished draft is said whole; +/// and every draft the cut leaves out is told, not only the first. +#[test] +fn r7_the_cap_never_backs_off_into_a_closed_draft_and_tells_every_draft_it_cuts() { + let words = crate::projection::tuning::Tuning::default().persona.words; + let thought: String = (1..=150).map(|n| format!("t{n} ")).collect(); + let tail: String = (1..=60).map(|n| format!("u{n} ")).collect(); + let text = format!("{thought}Meta, it went out. The file is there. {tail}"); + let capped = crate::projection::narrate::cap_prose(&text, words); + assert!(capped.text.ends_with("The file is there.") && capped.cut_drafts.is_empty(), "the draft whole: {capped:?}"); + let many: String = (1..=190).map(|n| format!("t{n} ")).collect(); + let cut = crate::projection::narrate::cap_prose(&format!("{many}First late one. and Second late one."), words); + assert_eq!(cut.cut_drafts, ["First late one.", "Second late one."]); + let told = crate::projection::outcome::outcome_of(&json!({ "interpreter": { "room": "#minds:gamecult.org", "said": [], "refused": [], "gaps": [], "heard": [], "cut_by_cap": cut.cut_drafts }, "posted": [] })).unwrap().lines(crate::projection::select::forms(&control())); + assert_eq!(told.iter().filter(|line| line.contains("the length limit cut it")).count(), 2, "{told:?}"); +} diff --git a/crates/mind-body/src/projection/narrate.rs b/crates/mind-body/src/projection/narrate.rs index 2ce1320..d001eb8 100644 --- a/crates/mind-body/src/projection/narrate.rs +++ b/crates/mind-body/src/projection/narrate.rs @@ -393,19 +393,19 @@ pub(crate) fn cap_words(text: &str, max: usize) -> String { /// Her prose held to her word budget without ever ending inside a draft (map 16.19.1; Soul's R5): a cut never leaves an /// unclosed `` in her stream, so a continuation never runs on inside one. When the budget falls inside a draft, /// the prose runs on to that draft's closing mark, up to a hard ceiling of twice the budget; past the ceiling, or with no -/// closing mark, it is cut before the draft opens, and the draft's words so far are `cut_draft`: unfinished, cut by the +/// closing mark, it is cut before the draft opens, and every draft the cut leaves out is in `cut_drafts`: unfinished, cut by the /// cap, which she is told. #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) struct Capped { pub text: String, /// Whether the budget cut anything. pub cut: bool, - pub cut_draft: Option, + pub cut_drafts: Vec, } pub(crate) fn cap_prose(text: &str, max: usize) -> Capped { let text = text.trim(); - let Some(&over) = word_starts(text).get(max) else { return Capped { text: text.to_owned(), cut: false, cut_draft: None } }; + let Some(&over) = word_starts(text).get(max) else { return Capped { text: text.to_owned(), cut: false, cut_drafts: Vec::new() } }; // The draft boundary is decided on the raw text at the word cut (Soul's R6 P1), before any sentence back-off. let open_at_cut = text[..over].rfind(DRAFT_OPEN).filter(|open| !text[*open..over].contains(DRAFT_CLOSE)); let end = match open_at_cut { @@ -414,20 +414,37 @@ pub(crate) fn cap_prose(text: &str, max: usize) -> Capped { .map(|at| open + at + DRAFT_CLOSE.len()) .filter(|close| words(&text[..*close]) <= max.saturating_mul(2)) .unwrap_or_else(|| text[..open].trim_end().len()), - // Outside a draft: back off to a sentence end as `cap_words` does, never past a draft that opens before the cut. + // Outside a draft: back off to a sentence end as `cap_words` does, never past a draft that opens before the cut, and + // never into a closed draft (Soul's R7): a back-off point inside one moves to its closing mark, which lies before + // the cut and so inside the ceiling. None => { let within = text[..over].trim_end(); match last_sentence_end(within) { - Some(end) if words(&within[..end]) as f64 >= SENTENCE_CUT_FLOOR * max as f64 && !within[end..].contains(DRAFT_OPEN) => end, + Some(end) if words(&within[..end]) as f64 >= SENTENCE_CUT_FLOOR * max as f64 && !within[end..].contains(DRAFT_OPEN) => { + let inside = text[..end].rfind(DRAFT_OPEN).filter(|open| !text[*open..end].contains(DRAFT_CLOSE)); + match inside.and_then(|open| text[open..].find(DRAFT_CLOSE).map(|at| (open, open + at + DRAFT_CLOSE.len()))) { + Some((_, close)) if words(&text[..close]) <= max.saturating_mul(2) => close, + Some((open, _)) => text[..open].trim_end().len(), + None => end, + } + } _ => within.len(), } } }; - // The first draft the cut leaves out, opened before the cut or after it: never dropped silently, but told to her as cut - // by the length limit, its words up to her budget. - let rest = &text[end..]; - let cut_draft = rest.find(DRAFT_OPEN).map(|open| cap_words(rest[open + DRAFT_OPEN.len()..].split(DRAFT_CLOSE).next().unwrap_or_default(), max)).filter(|words| !words.is_empty()); - Capped { text: text[..end].trim_end().to_owned(), cut: end < text.len(), cut_draft } + // Every draft the cut leaves out, opened before the cut or after it: never dropped silently, but each told to her as + // cut by the length limit, its words up to her budget (Soul's R7 #10). + let mut cut_drafts = Vec::new(); + let mut rest = &text[end..]; + while let Some(open) = rest.find(DRAFT_OPEN) { + let body = &rest[open + DRAFT_OPEN.len()..]; + let words = cap_words(body.split(DRAFT_CLOSE).next().unwrap_or_default(), max); + if !words.is_empty() { + cut_drafts.push(words); + } + rest = body.find(DRAFT_CLOSE).map_or("", |close| &body[close + DRAFT_CLOSE.len()..]); + } + Capped { text: text[..end].trim_end().to_owned(), cut: end < text.len(), cut_drafts } } /// The byte just past the last sentence end in `text`: a `.`, `!`, `?` or `…` with any closing quotes or brackets after diff --git a/crates/mind-body/src/projection/outcome.rs b/crates/mind-body/src/projection/outcome.rs index 0f7b230..b7e69e2 100644 --- a/crates/mind-body/src/projection/outcome.rs +++ b/crates/mind-body/src/projection/outcome.rs @@ -253,8 +253,11 @@ pub(crate) fn outcome_of(record: &Value) -> Option { } } if let Some(text) = words(&decision["unfinished"]) { - let by_limit = decision["cut_by_cap"].as_array().is_some_and(|cut| !cut.is_empty()); - outcome.parts.push(Part::Unfinished { room: room.clone(), text, by_limit }); + outcome.parts.push(Part::Unfinished { room: room.clone(), text, by_limit: false }); + } + // Each draft the length limit cut off, told as that (Soul's R7 #10: every one, not only the first). + for cut in decision["cut_by_cap"].as_array().into_iter().flatten().filter_map(|text| words(text)) { + outcome.parts.push(Part::Unfinished { room: room.clone(), text: cut, by_limit: true }); } // What the commit did with her promises (`commit::apply`): each close it applied, and each close it refused for a // reason she can be told. An open is told by the promise's own section from her next packet on.