diff --git a/crates/mind-body/src/persona.rs b/crates/mind-body/src/persona.rs index f0741c4..009e95d 100644 --- a/crates/mind-body/src/persona.rs +++ b/crates/mind-body/src/persona.rs @@ -557,7 +557,9 @@ fn interpreted(start: &Start, instance: &Slug, state: &Value, waked: json!({ "kind": "interpreter", "status": "ok", "put": landed.put, "applied": landed.applied.changes, "refused": landed.applied.refused, "pruned": landed.applied.pruned, "usage": usage }) } Err(missed) => { - if let Some(mut record) = written.borrow().clone().filter(|record| record["status"] == "pending") { + // A put whose reply was lost may have committed: its record stays pending, and the next wake's `owed_identity` + // settles it from her state. Only a put known not to have landed is voided. + if let Some(mut record) = written.borrow().clone().filter(|record| record["status"] == "pending" && !missed.unknown) { record["status"] = json!("void"); if let Err(fault) = start.trace.record(&diff_key, &record) { eprintln!("mind-persona: the identity record {diff_key} was not voided: {fault:#}"); @@ -686,6 +688,8 @@ pub(crate) struct Landed { pub(crate) struct Missed { pub applied: Applied, pub why: String, + /// The put's own call failed, so whether the store committed is not known; a refusal or a stale answer is known not to have. + pub unknown: bool, } /// The deltas of `turn` applied to her state and put under the `updatedAt` she was read at (`read`, the state the wake began @@ -702,23 +706,23 @@ fn put_applied(store: &dyn PersonaStore, read: &Value, turn: &Taken, key: &str, } }; let (next, applied) = attempt(read); - let next = next.map_err(|why| Missed { applied: applied.clone(), why })?; - ahead(&applied).map_err(|why| Missed { applied: applied.clone(), why })?; + let next = next.map_err(|why| Missed { applied: applied.clone(), why, unknown: false })?; + ahead(&applied).map_err(|why| Missed { applied: applied.clone(), why, unknown: false })?; match store.put(&next, read.get("updatedAt").and_then(Value::as_str), key) { Ok(Put::Committed) => return Ok(Landed { applied, put: "committed" }), Ok(Put::Stale) => {} - Ok(Put::Refused(why)) => return Err(Missed { applied, why }), - Err(fault) => return Err(Missed { applied, why: format!("{fault:#}") }), + Ok(Put::Refused(why)) => return Err(Missed { applied, why, unknown: false }), + Err(fault) => return Err(Missed { applied, why: format!("{fault:#}"), unknown: true }), } - let (fresh, _) = store.persona().map_err(|fault| Missed { applied: applied.clone(), why: format!("her state was not read again: {fault:#}") })?; + let (fresh, _) = store.persona().map_err(|fault| Missed { applied: applied.clone(), why: format!("her state was not read again: {fault:#}"), unknown: false })?; let (next, applied) = attempt(&fresh); - let next = next.map_err(|why| Missed { applied: applied.clone(), why })?; - ahead(&applied).map_err(|why| Missed { applied: applied.clone(), why })?; + let next = next.map_err(|why| Missed { applied: applied.clone(), why, unknown: false })?; + ahead(&applied).map_err(|why| Missed { applied: applied.clone(), why, unknown: false })?; match store.put(&next, fresh.get("updatedAt").and_then(Value::as_str), key) { Ok(Put::Committed) => Ok(Landed { applied, put: "stale_reapplied" }), - Ok(Put::Stale) => Err(Missed { applied, why: "stale again".to_owned() }), - Ok(Put::Refused(why)) => Err(Missed { applied, why }), - Err(fault) => Err(Missed { applied, why: format!("{fault:#}") }), + Ok(Put::Stale) => Err(Missed { applied, why: "stale again".to_owned(), unknown: false }), + Ok(Put::Refused(why)) => Err(Missed { applied, why, unknown: false }), + Err(fault) => Err(Missed { applied, why: format!("{fault:#}"), unknown: true }), } } diff --git a/crates/mind-body/src/persona/drain_tests.rs b/crates/mind-body/src/persona/drain_tests.rs index b45ac92..116f491 100644 --- a/crates/mind-body/src/persona/drain_tests.rs +++ b/crates/mind-body/src/persona/drain_tests.rs @@ -97,11 +97,13 @@ struct FakeStore { state: RefCell, puts: RefCell>, outside: RefCell>, + /// The store commits a put, and its reply is lost: the caller sees an error. + lose_reply: Cell, } impl FakeStore { fn new(state: Value) -> Self { - Self { state: RefCell::new(state), puts: RefCell::default(), outside: RefCell::default() } + Self { state: RefCell::new(state), puts: RefCell::default(), outside: RefCell::default(), lose_reply: Cell::default() } } } @@ -121,6 +123,9 @@ impl PersonaStore for Rc { } self.puts.borrow_mut().push(state.clone()); *self.state.borrow_mut() = state.clone(); + if self.lose_reply.get() { + bail!("the reply was lost"); + } Ok(Put::Committed) } } @@ -599,6 +604,24 @@ fn t_p2_identity_post_is_retried_next_wake() { assert_eq!(rig.mouth.sent.borrow().len(), 1, "a posted record is never posted again"); } +/// A put the store committed but whose reply was lost leaves its identity record pending, and the next wake posts it from her state (finding put-error-voids-diff). +#[test] +fn t_s1_a_lost_put_reply_leaves_the_identity_record_pending() { + const RENAME: &str = r#"{"self":{"identity":{"publicName":"Tobiah of the River"}}}"#; + let rig = three_rooms(); + *rig.review.borrow_mut() = Some("#review:gamecult.org".into()); + rig.store.lose_reply.set(true); + rig.run(Interprets(RENAME)); + assert_eq!(rig.store.puts.borrow()[0]["publicName"], "Tobiah of the River", "the store committed"); + let records = rig.all_records(); + assert_eq!((rows(&records, "interpreter")[0]["status"].clone(), rows(&records, "identity_diff")[0]["status"].clone()), (json!("error"), json!("pending")), "the record is not voided"); + assert!(rig.mouth.sent.borrow().is_empty()); + rig.store.lose_reply.set(false); + rig.run(Tally::default()); + assert_eq!(rig.mouth.sent.borrow().len(), 1, "the next wake posts it, her state holding the change"); + assert_eq!(rows(&rig.all_records(), "identity_diff")[0]["status"], "posted"); +} + /// A crash between the write-ahead and the put's outcome: the next wake posts what her state holds and voids what it does not. #[test] fn t_p2_a_pending_identity_record_is_settled_by_her_state() {