persona drain: only a failure that is the line's is a strike; an outage, quota stop or connector failure is retried

belongs_to_the_line names, beside the code that writes them, the failed reasons that are the line's (an answer that is
not the speech contract, a refusal, a context or size limit); the record already carries the reason, so no field is
added. The poison-room tests fail on a refusal and a new set fail on outages and a mixed run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 22:16:12 +01:00
commit f5cc11b30c
4 changed files with 81 additions and 8 deletions

View file

@ -41,7 +41,7 @@ use crate::address::{Addressee, addresses, is_for, localpart};
use crate::chat_client::{Basis, Chat, ChatDelivery, TailLine};
use crate::control::{ControlSource, Effective, HeldReason, derive};
use crate::heartbeat::{HeartbeatView, Speaker, own_rooms, pending_facts, read_pending, wakes};
use crate::projection::{DomainSource, HuginnPersonaSource, Model, Mouth, Organs, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, history, own_addressee as heard_as, take};
use crate::projection::{belongs_to_the_line, DomainSource, HuginnPersonaSource, Model, Mouth, Organs, PersonaStore, Put, ReadSource, Request, Taken, Through, TurnKind, apply_commit, history, own_addressee as heard_as, take};
/// The admitting agent's name on what her Persona admitted; `self_run::inbox` still reads her briefs by it.
pub const AGENT: &str = "mind-persona";
@ -281,9 +281,10 @@ fn done_events(records: &[Value]) -> BTreeSet<&str> {
done
}
/// How many of her turns that held `event` failed.
/// How many of her turns that held `event` failed for a reason that is the line's (`belongs_to_the_line`): an outage, quota stop or
/// connector failure is the body's, retried, and no strike.
fn strikes(records: &[Value], event: &str) -> usize {
records.iter().filter(|record| record.get("failed").is_some() && record["held"].as_array().is_some_and(|held| held.iter().any(|id| id == event))).count()
records.iter().filter(|record| record["failed"].as_str().is_some_and(belongs_to_the_line) && record["held"].as_array().is_some_and(|held| held.iter().any(|id| id == event))).count()
}
fn drain<C: Chat>(instance: &Slug, start: &Start<C>) -> Outcome {

View file

@ -150,16 +150,22 @@ impl Trace for Rc<MemTrace> {
}
}
/// A failure that is the line's: the model answered, and refused it.
const REFUSED: &str = "the model's turn ended early: Refusal";
/// A failure that is the body's: no answer came.
const OUTAGE: &str = "the connector did not answer: unavailable";
/// The recorded model, failing any call whose prompt carries `marker`: a turn that holds the marked line fails.
struct Flaky {
inner: Scripted,
marker: &'static str,
reason: &'static str,
}
impl Model for Flaky {
fn ask(&self, request_id: &str, model: &str, prompt: &str, schema: Option<&str>) -> std::result::Result<Answer, Failed> {
if prompt.contains(self.marker) {
return Err(Failed { reason: "the provider failed the call".into(), usage: None });
return Err(Failed { reason: self.reason.into(), usage: None });
}
self.inner.ask(request_id, model, prompt, schema)
}
@ -263,7 +269,7 @@ fn t_l2_a_start_killed_between_turns_does_not_replay_the_answered_message() {
#[test]
fn t_l2_failed_room_stays_pending() {
let rig = Rig::new(vec![line(1, &room(1), META, "alpha is fine"), line(2, &room(2), META, "FAILME bravo"), line(3, &room(1), META, "alpha again")]);
let model = Flaky { inner: scripted(false), marker: "FAILME" };
let model = Flaky { inner: scripted(false), marker: "FAILME", reason: REFUSED };
assert_eq!(rig.run(model), Outcome::Drained { turns: 2, acked_through: Some(1), put_refused: 0 });
assert_eq!(rig.held_seqs(), [2, 3], "the ack stops below the failed room's first waking line, and the later line stays with it");
let rows = rig.trace.rows.borrow();
@ -375,7 +381,7 @@ fn sends_to(rig: &Rig, room: &str) -> usize {
#[test]
fn t_l2_a_poison_room_does_not_take_the_answered_rooms_again_and_is_set_aside_after_two_failures() {
let rig = Rig::new(vec![line(1, &room(1), META, "alpha is fine"), line(2, &room(2), META, "FAILME bravo"), line(3, &room(1), META, "alpha again")]);
let failing = || Flaky { inner: scripted(false), marker: "FAILME" };
let failing = || Flaky { inner: scripted(false), marker: "FAILME", reason: REFUSED };
rig.run(failing());
assert_eq!(rig.held_seqs(), [2, 3], "start 1: room 1 answered, room 2 failed once");
rig.run(failing());
@ -396,7 +402,7 @@ fn t_l2_a_poison_room_does_not_take_the_answered_rooms_again_and_is_set_aside_af
fn t_l2_she_is_told_the_next_wake_that_her_turn_on_the_line_failed_and_it_was_set_aside() {
let rig = Rig::new(vec![line(1, &room(2), META, "FAILME bravo")]);
for _ in 0..3 {
rig.run(Flaky { inner: scripted(false), marker: "FAILME" });
rig.run(Flaky { inner: scripted(false), marker: "FAILME", reason: REFUSED });
}
assert!(rig.held_seqs().is_empty());
rig.mouth.held.borrow_mut().push(line(2, &room(1), META, "hello again"));
@ -460,7 +466,7 @@ fn t_l2_a_turn_that_changed_nothing_puts_nothing_and_one_that_did_puts_once() {
#[test]
fn t_l2_an_ack_goes_through_a_seq_once_and_a_failed_ack_is_not_reported_as_acked() {
let rig = Rig::new(vec![line(1, &room(1), META, "alpha is fine"), line(2, &room(2), META, "FAILME bravo")]);
rig.run(Flaky { inner: scripted(false), marker: "FAILME" });
rig.run(Flaky { inner: scripted(false), marker: "FAILME", reason: REFUSED });
assert_eq!(*rig.mouth.acks.borrow(), [1], "the failed turn's settling does not ack through 1 a second time");
let rig = Rig::new(vec![line(1, &room(1), META, "hello")]);
rig.mouth.ack_fails.set(true);
@ -490,3 +496,58 @@ fn t_l2_the_drain_reads_every_page_of_what_is_pending() {
assert_eq!(rig.run(scripted(false)), Outcome::Drained { turns: 3, acked_through: Some(3), put_refused: 0 });
assert_eq!(rig.held_seqs(), [4, 5], "three turns from five lines on three pages: a first page alone would hold two rooms");
}
fn failing(reason: &'static str) -> Flaky {
Flaky { inner: scripted(false), marker: "FAILME", reason }
}
fn set_aside(rig: &Rig) -> bool {
rig.trace.rows.borrow().iter().any(|(_, record)| record.get("set_aside").is_some())
}
/// A provider outage, quota stop or connector failure is the body's: the line stays pending and is tried on every start.
#[test]
fn t_l2_two_starts_that_fail_on_an_outage_set_nothing_aside() {
let rig = Rig::new(vec![line(1, &room(1), META, "FAILME hello")]);
for _ in 0..4 {
rig.run(failing(OUTAGE));
}
assert!(!set_aside(&rig), "an outage is no strike, however many starts it lasts");
assert_eq!(rig.held_seqs(), [1], "the line is still pending");
assert_eq!(rig.trace.rows.borrow().iter().filter(|(_, record)| record.get("failed").is_some()).count(), 4, "each start took its turn");
rig.run(scripted(false));
assert_eq!(sends_to(&rig, &room(1)), 1, "when the outage ends she answers it");
}
#[test]
fn t_l2_two_starts_that_fail_on_a_refused_answer_set_the_line_aside() {
let rig = Rig::new(vec![line(1, &room(1), META, "FAILME hello")]);
rig.run(failing(REFUSED));
rig.run(failing("the answer is not the speech contract"));
assert!(!set_aside(&rig) && rig.held_seqs() == [1]);
rig.run(failing(REFUSED));
assert!(set_aside(&rig) && rig.held_seqs().is_empty(), "two strikes, and the third start sets it aside");
}
#[test]
fn t_l2_an_outage_and_a_refusal_are_one_strike() {
let rig = Rig::new(vec![line(1, &room(1), META, "FAILME hello")]);
rig.run(failing(OUTAGE));
rig.run(failing(REFUSED));
rig.run(failing(OUTAGE));
assert!(!set_aside(&rig) && rig.held_seqs() == [1], "one strike so far");
rig.run(failing(REFUSED));
rig.run(failing(OUTAGE));
assert!(set_aside(&rig) && rig.held_seqs().is_empty(), "the second refusal is the second strike");
}
#[test]
fn t_l2_only_the_lines_own_failures_are_strikes() {
use crate::projection::belongs_to_the_line as line_own;
for reason in ["the answer is not the speech contract", "the model's turn ended early: Refusal", "the model's turn ended early: ContextWindow", "the provider failed the call: InvalidRequest", "the provider failed the call: ResultSize"] {
assert!(line_own(reason), "{reason}");
}
for reason in ["the provider failed the call: RateLimited", "the provider failed the call: Unavailable", "the provider failed the call: Timeout", "the provider failed the call: Authentication", "the provider failed the call: Provider", "the connector did not answer: x", "the connector refused the call: Y", "something new"] {
assert!(!line_own(reason), "{reason}");
}
}

View file

@ -47,6 +47,7 @@ use crate::heartbeat::HeartbeatView;
pub use domain::{DomainFacts, DomainSource, HttpForge, MindDomain};
pub use narrate::{Answer, Continued, Failed, KitModel, Model, Reasoning, Spent, Turn, connector_key};
pub(crate) use narrate::belongs_to_the_line;
pub use read::{LiveReads, ReadFailure, ReadFixture, ReadRequest, ReadSource};
pub use select::TurnKind;
pub(crate) use commit::apply as apply_commit;

View file

@ -78,6 +78,16 @@ impl std::fmt::Display for Failed {
}
}
/// Whether a failed turn's reason (the `failed` of its record) belongs to the line it held: the model answered and the answer, or
/// the input, was the fault (an answer that is not the speech contract, a refusal, an over-long input or output). Transport,
/// connector, quota, timeout and unavailability are the body's, not the line's: that turn is retried, never counted. The
/// reasons are the ones this module and the Interpreter write; one not named here is the body's.
pub(crate) fn belongs_to_the_line(reason: &str) -> bool {
reason == "the answer is not the speech contract"
|| ["Refusal", "ContextWindow", "MaxOutputTokens"].iter().any(|stop| reason == format!("the model's turn ended early: {stop}"))
|| ["ResultSize", "InvalidRequest"].iter().any(|kind| reason == format!("the provider failed the call: {kind}"))
}
impl Failed {
fn without_usage(reason: impl Into<String>) -> Self {
Self { reason: reason.into(), usage: None }