diff --git a/crates/mind-body/src/projection/interpret.rs b/crates/mind-body/src/projection/interpret.rs index cf0a814..95d9848 100644 --- a/crates/mind-body/src/projection/interpret.rs +++ b/crates/mind-body/src/projection/interpret.rs @@ -727,16 +727,26 @@ pub(crate) fn decide_in(model: &dyn Model, request_id: &str, packet: &Packet, na // one paragraph in code's words naming that answer's fault (Low 5). The gaps are resolved before the accounting (Low // 3): a draft is accounted for by a gap only where the resolved gap names it. After the one retry every fault is per // item and fails closed for that item (K44, K48): a hold fault holds the drafts naming it, and a draft still accounted - // for nowhere is refused `Undecided`. A turn fails only on a model error, or on an answer that is not the contract - // twice. + // for nowhere is refused `Undecided`. The last answer that parsed stands (map 16.31.6, K54): when the retry is off + // the contract or its call fails, the first answer is decided with its faults, and `retried` reports how the retry + // 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(); + let mut parsed_first: Option<(Answer, Vec, Vec, Vec)> = None; let (parsed, gaps, unopened, undecided) = loop { let answer = match model.ask(request_id, &tuning.interpreter.model, &asking, Some(SPEECH_SCHEMA)) { Ok(answer) => answer, Err(failed) => { calls.push(json!({ "usage": failed.usage, "retry": false })); - return json!({ "failed": failed.reason, "calls": calls, "retried": retried }); + let mut fault = json!({ "failed": failed.reason }); + if let Some(first) = parsed_first.take() { + fault["retried"] = retried.take().unwrap_or_default(); + retried = Some(fault); + break first; + } + fault["calls"] = json!(calls); + fault["retried"] = json!(retried); + return fault; } }; let (fault, missed) = match serde_json::from_str::(&answer.text) { @@ -766,6 +776,7 @@ pub(crate) fn decide_in(model: &dyn Model, request_id: &str, packet: &Packet, na } fault["holds"] = json!(unopened); } + parsed_first = Some((parsed, gaps, unopened, missing)); (fault, missed.join("\n\n")) } Err(error) => { @@ -779,6 +790,11 @@ pub(crate) fn decide_in(model: &dyn Model, request_id: &str, packet: &Packet, na calls.push(json!({ "usage": answer.usage, "retry": false })); let mut fault = fault; fault["retried"] = retried.take().unwrap_or_default(); + // A failed retry is a report on the decision, not the decision: an earlier answer that parsed stands. + if let Some(first) = parsed_first.take() { + retried = Some(fault); + break first; + } fault["calls"] = json!(calls); return fault; } diff --git a/crates/mind-body/src/projection/interpret/tests.rs b/crates/mind-body/src/projection/interpret/tests.rs index e83957a..cfb52f9 100644 --- a/crates/mind-body/src/projection/interpret/tests.rs +++ b/crates/mind-body/src/projection/interpret/tests.rs @@ -2464,3 +2464,58 @@ fn k51_a_revision_beside_a_superseded_draft_is_held_under_an_open_hold() { assert!(held_for_meta(&d), "{d}"); assert_eq!(d["refused"][0]["draft"].as_u64(), Some(2), "{d}"); } + +// --------------------------------------------------------------------------------------------- +// K54 (map 16.31.6): the last answer that parsed stands. A failed retry is a report on the decision, not the decision. + +/// Soul's `soul_e`, inverted: a first answer that is the contract but forgets draft 2, then a retry that is not the +/// contract. The first answer stands: draft 1 is said to Meta, draft 2 is `undecided`, and `retried` says how the retry +/// failed, with the first answer's fault under it. +#[test] +fn k54_soul_e_a_usable_first_answer_stands() { + let forgets = json!({ "say": [say_holding(1, json!([]))], "gaps": [] }).to_string(); + let model = answering(vec![forgets, "not the contract".to_owned()]); + let decision = decide(&model, "test", &x1_packet(), "The room is warm.", P3_PROSE, &Tuning::default()); + assert!(decision.get("failed").is_none(), "{decision}"); + assert_eq!((texts(&decision), decision["undecided"].clone()), (vec!["Meta, the two lines are ready and held.".to_owned()], json!([2])), "{decision}"); + assert_eq!((decision["retried"]["failed"].as_str(), decision["retried"]["raw"].as_str(), decision["retried"]["retried"]["drafts"].clone()), (Some("the answer is not the speech contract"), Some("not the contract"), json!([2])), "{decision}"); + assert_eq!((speech_prompts(&model).len(), decision["calls"].as_array().map(Vec::len)), (2, Some(2)), "{decision}"); +} + +/// A model error on the retry keeps the first answer, and `retried` carries the model's reason. +#[test] +fn k54_a_model_error_on_retry_keeps_the_first_answer() { + struct Once(std::cell::Cell); + impl Model for Once { + fn ask(&self, _request_id: &str, _model: &str, _prompt: &str, _schema: Option<&str>) -> std::result::Result { + self.0.set(self.0.get() + 1); + if self.0.get() == 1 { + Ok(super::super::Answer { text: json!({ "say": [say_holding(1, json!([]))], "gaps": [] }).to_string(), usage: None }) + } else { + Err(super::super::Failed { reason: "the connector timed out".into(), usage: None }) + } + } + } + let decision = decide(&Once(std::cell::Cell::new(0)), "test", &x1_packet(), "The room is warm.", P3_PROSE, &Tuning::default()); + assert!(decision.get("failed").is_none(), "{decision}"); + assert_eq!((texts(&decision), decision["undecided"].clone(), decision["retried"]["failed"].as_str()), (vec!["Meta, the two lines are ready and held.".to_owned()], json!([2]), Some("the connector timed out")), "{decision}"); + // Negative: a model error on the first call, with no answer before it, still fails the turn. + struct Never; + impl Model for Never { + fn ask(&self, _request_id: &str, _model: &str, _prompt: &str, _schema: Option<&str>) -> std::result::Result { + Err(super::super::Failed { reason: "the connector timed out".into(), usage: None }) + } + } + let failed = decide(&Never, "test", &x1_packet(), "The room is warm.", P3_PROSE, &Tuning::default()); + assert_eq!((failed["failed"].as_str(), failed.get("said")), (Some("the connector timed out"), None), "{failed}"); + // Negative: an answer off the contract, then a retry whose call fails, fails the turn: no answer parsed. + struct OffThenError(std::cell::Cell); + impl Model for OffThenError { + fn ask(&self, _request_id: &str, _model: &str, _prompt: &str, _schema: Option<&str>) -> std::result::Result { + self.0.set(self.0.get() + 1); + if self.0.get() == 1 { Ok(super::super::Answer { text: "not the contract".into(), usage: None }) } else { Err(super::super::Failed { reason: "the connector timed out".into(), usage: None }) } + } + } + let failed = decide(&OffThenError(std::cell::Cell::new(0)), "test", &x1_packet(), "The room is warm.", P3_PROSE, &Tuning::default()); + assert_eq!((failed["failed"].as_str(), failed["retried"]["failed"].as_str(), failed.get("said")), (Some("the connector timed out"), Some("the answer is not the speech contract"), None), "{failed}"); +}