diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index be119f9..d8e8a43 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -1071,17 +1071,19 @@ impl SessionInbox for PersonaInbox<'_, C, S> }; match step { Step::Answered(started) => { - // The one ack rule: through the highest answered seq, never past a delivery still - // told and unanswered (a retold batch, or a line written and not yet echoed). + // The one ack rule: through the highest answered seq, never past a delivery still told + // and unanswered (a retold batch, or a line written and not yet echoed). Deliveries are + // told in seq order, so while one is unanswered below an answered seq, everything + // answered lies above it and nothing is acked; the ack follows its answer. self.answered_through = self.answered_through.max(batch.iter().map(|delivery| delivery.seq).max().unwrap_or(0)); let unanswered = self.retell.iter().flat_map(|(retold, _)| retold).chain(self.written.iter().flat_map(|written| &written.batch)).map(|delivery| delivery.seq).min(); - let through = unanswered.map_or(self.answered_through, |lowest| self.answered_through.min(lowest.saturating_sub(1))); - if through > self.progress.acked { - if self.organs.chat.ack(through).is_err() { + let past_unanswered = unanswered.is_some_and(|lowest| lowest <= self.answered_through); + if !past_unanswered && self.answered_through > self.progress.acked { + if self.organs.chat.ack(self.answered_through).is_err() { self.progress.failure.get_or_insert(Failure::Ack); return Ok((false, true)); } - self.progress.acked = through; + self.progress.acked = self.answered_through; } self.refusals = 0; if started.is_some() { diff --git a/crates/mind-body/src/persona/tests.rs b/crates/mind-body/src/persona/tests.rs index e0753da..e3d9170 100644 --- a/crates/mind-body/src/persona/tests.rs +++ b/crates/mind-body/src/persona/tests.rs @@ -2462,7 +2462,8 @@ fn a_session_that_ends_with_a_batch_retold_leaves_it_unacked_for_the_next_proces let writes: Rc>> = Rc::default(); let second = Scripted { writes: writes.clone(), script: VecDeque::from([(1, echo_line()), (1, result_line(reply_saying("all three"))), (1, Read::Exited(Some(0)))]), deliveries: None, asked: 0 }; let acked = carry_scripted(&rig, vec![first, second]); - let opening = writes.borrow().first().cloned().unwrap_or_default(); + let first_line: serde_json::Value = serde_json::from_str(writes.borrow().first().expect("the next process was told a line")).unwrap(); + let opening = first_line["message"]["content"].as_str().unwrap(); assert!(opening.contains(r#"text="three""#) && opening.contains(r#"text="four""#) && opening.contains(r#"text="five""#), "the next process is told 3, 4 and 5: {opening}"); assert_eq!(acked, [5], "the first process acked nothing; the second answered all three: {acked:?}"); }