From f28c4d8fa38e2d9cfd283282305d7fdb56bc0151 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 22:58:01 +0100 Subject: [PATCH] B7: a closed entry with no closedAt is stamped at the first commit that sees it and pruned seven days later Co-Authored-By: Claude Opus 5.5 --- crates/mind-body/src/projection/commit.rs | 8 +++++++- crates/mind-body/src/projection/commit/tests.rs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/mind-body/src/projection/commit.rs b/crates/mind-body/src/projection/commit.rs index 2e32f95..e5a5dcb 100644 --- a/crates/mind-body/src/projection/commit.rs +++ b/crates/mind-body/src/projection/commit.rs @@ -212,12 +212,18 @@ pub(crate) struct Applied { /// The one applier: her state with one turn's proposals applied, as deltas to `state` as it stands at commit, and what /// was applied and refused. `key` names the turn (`-t`); `now` is the commit's time. A failed turn applies -/// nothing. Closed entries older than `CLOSED_KEPT_DAYS` are removed first. +/// nothing. Closed entries older than `CLOSED_KEPT_DAYS` are removed first; a closed entry with no `closedAt` (an outside +/// put that closed it without one) is stamped `now` at the first commit that sees it, and removed `CLOSED_KEPT_DAYS` after +/// that (B7), so none is kept forever. pub(crate) fn apply(state: &Value, record: &Value, prose: &str, key: &str, now: DateTime) -> (Value, Applied) { let mut state = state.clone(); let mut applied = Applied::default(); let kept_until = now - Duration::days(CLOSED_KEPT_DAYS); if let Some(entries) = state.get_mut("commitments").and_then(Value::as_array_mut) { + let closed_at = |entry: &Value| entry["closedAt"].as_str().and_then(|at| DateTime::parse_from_rfc3339(at).ok()); + for entry in entries.iter_mut().filter(|entry| closed(entry["status"].as_str().unwrap_or_default()) && closed_at(entry).is_none()) { + entry["closedAt"] = json!(now.to_rfc3339()); + } let before = entries.len(); entries.retain(|entry| !closed(entry["status"].as_str().unwrap_or_default()) || entry["closedAt"].as_str().and_then(|at| DateTime::parse_from_rfc3339(at).ok()).is_none_or(|at| at.with_timezone(&Utc) > kept_until)); applied.pruned = before - entries.len(); diff --git a/crates/mind-body/src/projection/commit/tests.rs b/crates/mind-body/src/projection/commit/tests.rs index 3e4cb03..77cb384 100644 --- a/crates/mind-body/src/projection/commit/tests.rs +++ b/crates/mind-body/src/projection/commit/tests.rs @@ -216,3 +216,19 @@ fn b6_an_outside_put_wins_on_any_field_it_changed() { let (state, _) = apply(&holding("open"), &record(json!([close(How::Withdrawn, Evidence::Draft(1))]), shown("open"), true), PROSE, "r1-t2", now()); assert_eq!(state["commitments"][0]["status"], "withdrawn"); } + +/// B7: a closed entry with no `closedAt` is stamped at the first commit that sees it and removed seven days after that. +#[test] +fn b7_a_closed_entry_without_a_close_time_is_stamped_then_pruned_a_week_later() { + let mut no_at = holding("kept"); + no_at["commitments"][0]["closedAt"] = Value::Null; + let (stamped, applied) = apply(&no_at, &record(json!([]), json!([]), false), PROSE, "r1-t9", now()); + assert_eq!((stamped["commitments"][0]["closedAt"].as_str(), applied.pruned), (Some(now().to_rfc3339().as_str()), 0), "{stamped}"); + let (kept, applied) = apply(&stamped, &record(json!([]), json!([]), false), PROSE, "r1-t10", now() + Duration::days(6)); + assert_eq!((kept["commitments"].as_array().map(Vec::len), applied.pruned), (Some(1), 0)); + let (gone, applied) = apply(&stamped, &record(json!([]), json!([]), false), PROSE, "r1-t10", now() + Duration::days(8)); + assert_eq!((gone["commitments"].as_array().map(Vec::len), applied.pruned), (Some(0), 1)); + // A failed turn stamps too: the stamp is the commit's, not the turn's. + let (failed, _) = apply(&no_at, &json!({ "failed": "x" }), PROSE, "r1-t9", now()); + assert!(failed["commitments"][0]["closedAt"].is_string()); +}