From 0d9e00199553dbf1b162f832f108cb643c20c8a9 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 1 Dec 2026 02:15:05 +0100 Subject: [PATCH] persona: the Interpreter runs after a completed agent turn, so a retried wake never applies her deltas twice; a settled-unknown say frees its room (Soul s1) --- crates/mind-body/src/persona.rs | 22 +++++++----- crates/mind-body/src/persona/drain_tests.rs | 36 +++++++++++++++++-- .../src/projection/agent/say/tests.rs | 14 ++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index 5942d0a..b5a2972 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -6,9 +6,9 @@ //! start handed the mouth and heard nothing back of (`settle_handed`: resent under its id inside `OWED_WINDOW_S`, else unknown; no model //! call); and takes one wake over every room that holds a waking line (`heartbeat::wakes`, the one rule), oldest waking line first (a start //! the beat woke, with no waking line, wakes in the room of the newest pending line, else her first room). The wake runs in this order, and -//! her trace holds it in this order: `projection::wake` runs the Projector once and the Persona once; the Interpreter puts her state and -//! writes its receipt; her agent takes one turn with her tools (`say`, `read`, `shell`, `request_tool`), each call's receipt a row; then -//! the wake record lands, and only then is anything acked. +//! her trace holds it in this order: `projection::wake` runs the Projector once and the Persona once; her agent takes one turn with her tools +//! (`say`, `read`, `shell`, `request_tool`), each call's receipt a row; once that turn completed, the Interpreter puts her state and writes +//! its receipt (a failed turn runs no Interpreter, so a retry never applies a delta twice); then the wake record lands, and only then is anything acked. //! //! Her trace alone says whether a line is decided (`projection::decided`, the reader the heartbeat's poll uses too): a line is done when a //! completed wake held it, or it is set aside. The mouth's pending list and its ack cursor are transport and decide nothing; the ack is cleanup, @@ -21,9 +21,9 @@ //! aside: one `set_aside` record, the line acked past, and her next wake is told so (`outcome`). A wake over several rooms strikes nothing. //! The heartbeat never acks. //! -//! After the Persona, the Interpreter reads her monologue and its deltas are put once (`put_applied`) under the `updatedAt` she was read +//! After a completed agent turn, the Interpreter reads her monologue and its deltas are put once (`put_applied`) under the `updatedAt` she was read //! at, stamped at the put and later than the stored one (P5); its receipt, and the post of any identity change to the review room, are rows in her trace. -//! Her agent then acts from the same stream, never from the Interpreter's output (map 16.38.12). A failure of her agent's turn fails the +//! Her agent acts from the same stream, never from the Interpreter's output (map 16.38.12). An Interpreter that fails after the agent acted fails nothing: its receipt says error and her state does not move. A failure of her agent's turn fails the //! wake as any stage's does: an outage the connector typed (unavailable, timed out, rate-limited, refused for her credit) strikes nothing. //! //! What the module holds besides the drain is what a chat line is made of: her `Config`, who a sender is to her @@ -508,12 +508,13 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { .filter(|delivery| delivery.room_alias == rooms[0] || rooms.contains(&delivery.room_alias.as_str()) && (delivery.sender == config.own_sender || delivery.form != Form::Message || waked.elsewhere_shown.contains(&delivery.event_id))) .map(|delivery| delivery.event_id.as_str()) .collect(); - // The stream, in order (map 16.38.2): the Projector and the Persona ran; then the Interpreter puts her state, then her agent - // acts on the world. Neither reads the other. A failed Projector or Persona runs neither. + // The stream, in order (map 16.38.2): the Projector and the Persona ran; then her agent acts on the world; then, only once the agent's + // turn completed, the Interpreter puts her state. Neither reads the other. A failed Projector, Persona or agent turn runs no + // Interpreter, so the retry of a failed wake never applies a delta twice; an Interpreter that fails after the agent acted fails + // nothing, because a retry would wake the agent again and risk saying a line twice. let mut fault = waked.failed.clone(); let mut acted = Value::Null; if fault.is_none() { - interpreted(start, instance, &state, &waked, &deliveries, &held, rooms[0], &stem, began_ms, &mouth); let name = if public_name.is_empty() { instance.0.clone() } else { public_name.clone() }; let input = agent_input(&waked, &deliveries, &held, &config.own_sender); let fresh = ready_intentions(&state, now); @@ -529,6 +530,9 @@ fn drain(instance: &Slug, start: &Start) -> Outcome { } Err(bug) => return failed("her agent could not be set up", bug), } + if fault.is_none() { + interpreted(start, instance, &state, &waked, &deliveries, &held, rooms[0], &stem, began_ms, &mouth); + } } let record_key = format!("{stem}-w"); let mut record = json!({ @@ -588,7 +592,7 @@ fn agent_input(waked: &Waked, deliveries: &[&ChatDelivery], held: &[&str], own_s input } -/// What the Interpreter does after a completed wake (map 16.38.7): one schema call over her narration and prose, its deltas applied +/// What the Interpreter does after a completed agent turn (map 16.38.7): one schema call over her narration and prose, its deltas applied /// by the commit and put once (`put_applied`), and one receipt row in her trace. When the put landed with an identity change, the /// change is posted with its diff to the review room through the mouth, as a code-written line. Nothing here fails the wake: her /// next wake hears when her state was not kept. diff --git a/crates/mind-body/src/persona/drain_tests.rs b/crates/mind-body/src/persona/drain_tests.rs index 62eadaf..2fad36a 100644 --- a/crates/mind-body/src/persona/drain_tests.rs +++ b/crates/mind-body/src/persona/drain_tests.rs @@ -753,7 +753,7 @@ fn t_p2_schema_refusal_no_put() { // --------------------------------------------------------------------------------------------- // A2: the wake runs its four stages in order, and her agent's acts are told to her once. -/// One wake: the Projector, the Persona, the Interpreter's put and receipt, her agent's calls and what they did, the wake record, +/// One wake: the Projector, the Persona, her agent's calls and what they did, the Interpreter's put and receipt, the wake record, /// then the ack, in that order, and in that order in her trace. #[test] fn t_a2_order() { @@ -763,7 +763,7 @@ fn t_a2_order() { let log = rig.log.all(); let first = |what: &str| log.iter().position(|done| done == what).unwrap_or_else(|| panic!("{what} never happened: {log:?}")); let last = |what: &str| log.iter().rposition(|done| done == what).unwrap(); - let order = [first("projector"), first("persona"), first("interpreter"), first("put"), first("row:interpreter"), first("agent"), first("row:say"), first("send"), last("row:receipt"), first("row:wake"), first("ack")]; + let order = [first("projector"), first("persona"), first("agent"), first("row:say"), first("send"), last("row:receipt"), first("interpreter"), first("put"), first("row:interpreter"), first("row:wake"), first("ack")]; assert!(order.windows(2).all(|pair| pair[0] < pair[1]), "{order:?} in {log:?}"); let sent = rig.mouth.sent.borrow(); assert_eq!((sent.len(), sent[0].text.as_str(), sent[0].room_alias.as_str()), (1, "Morning. I am here.", room(1).as_str())); @@ -771,12 +771,42 @@ fn t_a2_order() { let records = rig.all_records(); let seq = |found: &dyn Fn(&Value) -> bool| records.iter().find(|record| found(record)).and_then(|record| record["seq"].as_u64()).unwrap(); let (interpreter, say, wake) = (seq(&|record| record["kind"] == "interpreter"), seq(&|record| record["agent"] == "say"), seq(&|record| is_wake(record))); - assert!(interpreter < say && say < wake, "{records:?}"); + assert!(say < interpreter && interpreter < wake, "{records:?}"); let places: Vec = records.iter().filter_map(|record| record["seq"].as_u64()).collect(); assert_eq!(places, (0..records.len() as u64).collect::>(), "no two rows hold one place"); assert_eq!(records.iter().find(|record| record["agent"] == "say").unwrap()["status"], "ok"); } +/// Her agent's failed turn runs no Interpreter: the retry that completes puts her state once, not once per attempt. +#[test] +fn t_a2_a_retried_wake_puts_her_state_once() { + let rig = Rig::new(vec![line(1, &room(1), META, "morning, are you there")]); + rig.agent.answers.borrow_mut().push_back(Err(Failed { reason: "the provider failed the call: RateLimited".into(), outage: true, usage: None })); + assert_eq!(rig.run(Interprets(MOVES)), Outcome::Drained { wakes: 1, acked_through: None }); + assert!(rig.store.puts.borrow().is_empty(), "no put behind a failed agent turn"); + assert!(rows(&rig.all_records(), "interpreter").is_empty(), "and no receipt"); + assert_eq!(rig.run(Interprets(MOVES)), Outcome::Drained { wakes: 1, acked_through: Some(1) }); + let puts = rig.store.puts.borrow(); + assert_eq!(puts.len(), 1, "one put across both wakes"); + assert_eq!(puts[0]["thoughtMemory"]["shortTerm"].as_array().unwrap().len(), 1, "one note from the one line"); + assert_eq!(rows(&rig.all_records(), "interpreter").len(), 1); +} + +/// An Interpreter that fails after her agent acted fails nothing: the wake is recorded, its lines acked, her agent is not called again, +/// and the receipt says error. +#[test] +fn t_a2_an_interpreter_failure_after_the_agent_acted_fails_nothing() { + let rig = Rig::new(vec![line(1, &room(1), META, "morning, are you there")]); + rig.agent.answers.borrow_mut().push_back(says(&room(1), "Morning. I am here.")); + assert_eq!(rig.run(poisoned(INTERPRETER, OUTAGE)), Outcome::Drained { wakes: 1, acked_through: Some(1) }); + let records = rig.all_records(); + assert_eq!(records.iter().filter(|record| is_wake(record)).map(|record| record["status"].clone()).collect::>(), vec![json!("completed")]); + let receipt = rows(&records, "interpreter")[0]; + assert_eq!((receipt["status"].as_str(), receipt["put"].as_str()), (Some("error"), Some("none")), "{receipt}"); + assert!(rig.store.puts.borrow().is_empty()); + assert_eq!((rig.agent.seen.borrow().len(), rig.mouth.sent.borrow().len(), rig.acks()), (1, 1, vec![1]), "her agent ran and spoke once"); +} + /// A say the mouth refused is told at her next wake, to her and to her agent, and not at the one after. #[test] fn t_a2_told_once() { diff --git a/crates/mind-body/src/projection/agent/say/tests.rs b/crates/mind-body/src/projection/agent/say/tests.rs index 61dcb7b..cf11d57 100644 --- a/crates/mind-body/src/projection/agent/say/tests.rs +++ b/crates/mind-body/src/projection/agent/say/tests.rs @@ -171,6 +171,20 @@ fn t_a2_room_unsettled() { assert!(unsettled(&rig.trace.records().unwrap(), "#minds", u64::MAX)); } +/// A handed row that settles unknown at a start frees its room for the wake that follows: that wake's packet was read after the +/// settlement, so she has been told, and her say goes out. +#[test] +fn t_a2_a_settled_unknown_frees_its_room_for_the_next_wake() { + let rig = Rig::new(vec![posted("$after")]); + rig.trace.record("old", &json!({ "agent": "say", "key": "old", "id": "old:id", "room": "#minds", "text": "late", "status": "handed", "seq": 0, "at": (noon() - Duration::minutes(31)).to_rfc3339() })).unwrap(); + settle_handed(&rig.trace, &rig.mouth(), "n9", noon()); + assert_eq!(rig.says()[0]["status"], "unknown"); + let began = rig.trace.records().unwrap().len() as u64; + let receipt = rig.say("#minds", "now", began); + assert_eq!(receipt.status(), Status::Ok, "the room is free: {:?}", receipt.fields()); + assert_eq!(rig.sent().len(), 1); +} + #[test] fn t_a2_resend_inside_window() { let handed = |minutes: i64, key: &str| json!({ "agent": "say", "key": key, "id": format!("{key}:id"), "room": "#minds", "text": "late", "status": "handed", "seq": 0, "at": (noon() - Duration::minutes(minutes)).to_rfc3339() });