diff --git a/crates/mind-body/src/projection/commit.rs b/crates/mind-body/src/projection/commit.rs index 6700c98..dd2902c 100644 --- a/crates/mind-body/src/projection/commit.rs +++ b/crates/mind-body/src/projection/commit.rs @@ -427,10 +427,20 @@ fn clear_receipt(entry: &mut Value) { } /// When the entry's receipt says what it needs was read (map 16.26, 16.27): `readAt` a time in RFC 3339, and `readOf`, -/// the normal form of what was read, a read that answers the entry's need (`read::answers`). Anything else is no receipt. +/// the normal form of what was read, a read that answers the entry's need (`read::answers`), returned after her latest +/// reopen of it (`reopened_at`). Anything else is no receipt. pub(crate) fn receipt(entry: &Value) -> Option> { let at = DateTime::parse_from_rfc3339(entry["readAt"].as_str()?).ok()?.with_timezone(&Utc); - answers(&needed_read(entry)?, entry["readOf"].as_str()?).then_some(at) + let fresh = reopened_at(entry).is_none_or(|reopened| at > reopened); + (fresh && answers(&needed_read(entry)?, entry["readOf"].as_str()?)).then_some(at) +} + +/// When her own words last reopened the entry (map 16.28.3, Self's ruling on K36 finding 1): a reopen says the read +/// behind the close was not enough, so a read that returned before it is spent for this entry. Only a read that returned +/// after it stands behind a keep: a receipt written after it, or a read of this wake. A conversation read carries no time, +/// so after a reopen none counts. +fn reopened_at(entry: &Value) -> Option> { + entry["reopened"].as_array()?.iter().filter_map(|said| DateTime::parse_from_rfc3339(said["at"].as_str()?).ok()).map(|at| at.with_timezone(&Utc)).max() } /// The receipt of a read on the promise that needs it (map 16.26, 16.27): when a read that answers an open promise's @@ -496,11 +506,12 @@ fn read_key(read: &Value) -> Option { /// Whether a read that answers the need `key` (`read::answers`: the thing itself, never a search) stands behind a keep /// citing `draft` (map 16.25.2, 16.26, 16.27): the entry's own receipt (`receipt`), one of the conversation's reads that -/// found it (`conversation_reads`, from her `already_read`, K26), or one of this wake's that came back before she typed +/// found it (`conversation_reads`, from her `already_read`, K26) unless she reopened the entry (`reopened_at`), or one of this wake's that came back before she typed /// the draft: her thought after a read begins at its `after_byte`, and a draft typed before that was written without it. fn read_behind(entry: &Value, key: &str, record: &Value, prose: &str, draft: &Evidence) -> bool { let decision = &record["interpreter"]; - if receipt(entry).is_some() || decision["conversation_reads"].as_array().into_iter().flatten().any(|read| read["found"] == true && read["key"].as_str().is_some_and(|read| answers(key, read))) { + let conversation = reopened_at(entry).is_none().then(|| decision["conversation_reads"].as_array()).flatten(); + if receipt(entry).is_some() || conversation.into_iter().flatten().any(|read| read["found"] == true && read["key"].as_str().is_some_and(|read| answers(key, read))) { return true; } let Evidence::Draft(n) = draft else { return false }; diff --git a/crates/mind-body/src/projection/commit/tests.rs b/crates/mind-body/src/projection/commit/tests.rs index 11269b5..832af41 100644 --- a/crates/mind-body/src/projection/commit/tests.rs +++ b/crates/mind-body/src/projection/commit/tests.rs @@ -972,3 +972,33 @@ fn k37_a_released_hold_is_pruned_an_open_one_is_not() { let (after, applied) = apply(&state, &hearing(&[], json!([]), json!([])), "x", "r1-t30", now()); assert_eq!((after["holds"].as_array().map(Vec::len), after["holds"][0]["id"].as_str(), applied.pruned), (Some(1), Some("h-1"), 1)); } + +/// Self's ruling on K36 finding 1: after a reopen, a keep counts only reads that returned after it. F.2 reopened with +/// its need still #85: a re-keep on this conversation's #85 read, or on a receipt from before the reopen, is refused +/// `needs_unread`; a read returned after the reopen keeps it. +#[test] +fn k36_a_reopen_spends_the_read_that_kept_it() { + let (state, _) = f2_r9(&f2_after_r8("kept"), json!([reopening(F2_R9_WORDS, None, true)])); + let later = now() + Duration::minutes(10); + let conversation = json!([{ "key": "repo:gamecult/bifrost#85", "found": true }]); + let mut rekeep = keeping_c1(&state, json!([])); + rekeep["interpreter"]["conversation_reads"] = conversation.clone(); + let (after, applied) = apply(&state, &rekeep, F2_PROSE, "r1-t19", later); + assert_eq!((after["commitments"][0]["status"].as_str(), reasons(&applied)), (Some("open"), vec![json!("needs_unread")]), "{applied:?}"); + // A receipt from before the reopen is no receipt. + let mut stale = state.clone(); + stale["commitments"][0]["readAt"] = json!((now() - Duration::minutes(6)).to_rfc3339()); + stale["commitments"][0]["readId"] = json!("r1-t16-r1"); + stale["commitments"][0]["readOf"] = json!("repo:gamecult/bifrost#85"); + assert!(receipt(&stale["commitments"][0]).is_none()); + assert_eq!(reasons(&apply(&stale, &keeping_c1(&stale, json!([])), F2_PROSE, "r1-t19", later).1), vec![json!("needs_unread")]); + // A read returned after the reopen: this wake's, or a receipt written after it. + let mut fresh = keeping_c1(&state, json!([returned(1, "GameCult/Bifrost#85", "#85 Passkeys, the diff", Some(0))])); + fresh["interpreter"]["conversation_reads"] = conversation; + assert_eq!(apply(&state, &fresh, F2_PROSE, "r1-t19", later).0["commitments"][0]["status"], "kept"); + let mut received = state.clone(); + received["commitments"][0]["readAt"] = json!((now() + Duration::minutes(5)).to_rfc3339()); + received["commitments"][0]["readId"] = json!("r1-t19-r1"); + received["commitments"][0]["readOf"] = json!("repo:gamecult/bifrost#85"); + assert_eq!(apply(&received, &keeping_c1(&received, json!([])), F2_PROSE, "r1-t20", later).0["commitments"][0]["status"], "kept"); +}