diff --git a/crates/mind-body/src/projection.rs b/crates/mind-body/src/projection.rs index 210eee5..e2ba696 100644 --- a/crates/mind-body/src/projection.rs +++ b/crates/mind-body/src/projection.rs @@ -424,11 +424,15 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni let mut decision = interpret::decide(model, &request_id(instance, "interpreter", tag, packet.seed), packet, &narration, &prose, tuning); let mut reads: Vec = Vec::new(); let mut ended = None; + // Whether the newest segment was cut at her word budget: then a draft it left unfinished is code's cut, not hers. + let mut cut = false; while let (Some(reading), Some(read)) = (reading, decision.get("read").filter(|read| !read.is_null()).cloned()) { - // Caps (map 16.22.3): reads a wake, bytes a wake, and her prose across segments at most twice her word budget. + // Caps (map 16.22.3): reads a wake, bytes a wake, and her prose across segments at most twice her word budget. A + // read is performed only when the reply after it can have her whole word budget inside that, so the overall cap + // never cuts the reply short (Soul, F.1 r9). let words_left = (tuning.persona.words * 2).saturating_sub(words(&prose)); let bytes_left = tuning.interpreter.wake_read_bytes.saturating_sub(read_bytes); - if reads.len() >= tuning.interpreter.reads_per_wake || bytes_left == 0 || words_left == 0 { + if reads.len() >= tuning.interpreter.reads_per_wake || bytes_left == 0 || words_left < tuning.persona.words { ended = Some(if reads.len() >= tuning.interpreter.reads_per_wake { "reads_per_wake" } else if bytes_left == 0 { "wake_read_bytes" } else { "prose_words" }); break; } @@ -459,7 +463,8 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni break; } }; - let segment = cap_words(&continued.answer.text, tuning.persona.words.min(words_left)); + let segment = cap_words(&continued.answer.text, tuning.persona.words); + cut = words(&continued.answer.text) > tuning.persona.words; usage.push(json!(continued.answer.usage)); turns.push(Turn::Assistant { text: segment.clone(), states: continued.states }); before = format!("{before}{}\n\n{bridge}\n\n", &prose[from..]); @@ -472,10 +477,22 @@ fn narrate_one(model: &dyn Model, instance: &str, packet: &Packet, tuning: &tuni // applies (Soul p5); the failure, with what it cost, is recorded beside it. if next.get("failed").is_some() { decision["later_failed"] = json!({ "segment": reads.len() + 1, "failed": next["failed"], "raw": next.get("raw"), "usage": next["usage"] }); + cut = false; break; } decision = next; } + // A draft the word budget cut off after a read is code's cut, not a promise of hers that failed: it is reported, and + // she is not told of it as unfinished, nor of an unwritten gap in that segment. + if cut && !reads.is_empty() && decision.get("unfinished").is_some() { + decision["unfinished_cut_by_cap"] = decision["unfinished"].take(); + decision.as_object_mut().map(|map| map.remove("unfinished")); + for gap in decision["gaps"].as_array_mut().into_iter().flatten() { + if gap["kind"] == "unwritten" && gap["start_byte"].as_u64().is_some_and(|at| at as usize >= from) { + gap["from"] = json!("cut_by_cap"); + } + } + } if let Some(cap) = ended { decision["read_ended"] = json!(cap); } diff --git a/crates/mind-body/src/projection/harness/tests.rs b/crates/mind-body/src/projection/harness/tests.rs index cc1a236..1fb00ee 100644 --- a/crates/mind-body/src/projection/harness/tests.rs +++ b/crates/mind-body/src/projection/harness/tests.rs @@ -480,3 +480,30 @@ fn k24_a_failed_later_call_keeps_what_was_already_decided() { assert_eq!((decision["later_failed"]["segment"].as_u64(), decision["later_failed"]["failed"].as_str()), (Some(2), Some("the answer is not the speech contract")), "{decision}"); assert_eq!(decision["reads"][0]["ok"], true); } + +/// Soul at F.1 r9: the reply after a read gets her whole word budget, so a read is performed only when that fits the +/// wake's cap; and a draft the budget cuts off after a read is code's cut, not an unfinished line of hers. +#[test] +fn k24_the_reply_after_a_read_gets_her_whole_budget_and_a_cut_draft_is_not_hers() { + let fixture = reading_room("quiet-beat.toml"); + let words = Tuning::default().persona.words; + let model = Scripted::new(|prompt| { + if prompt.contains("LOOP-READ") { + format!("Deru, I opened it and {}", "more ".repeat(250)) + } else { + format!("I want to see what changed in GameCult/Lodur#12. {}", "filler ".repeat(200)) + } + }) + .interpreting(|prompt| { + let read = if prompt.contains("Earlier in his moment") { Value::Null } else { json!({ "kind": "repo", "target": "GameCult/Lodur#12", "quote": "I want to see what changed" }) }; + json!({ "say": [], "gaps": [], "read": read }).to_string() + }); + let report = wake_of(&fixture, &model); + let persona = &report["renders"][0]["persona"]; + let decision = &report["renders"][0]["interpreter"]; + 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(2 * words as u64), "her first segment and the whole reply after the read: {persona}"); + assert!(decision.get("unfinished").is_none() && decision["unfinished_cut_by_cap"].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")), "{told:?}"); +}