heartbeat: keep slot 3 declared as a retired slot so a deployed four-slot state still saves; pin the turn room's overflow count and acks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-29 22:00:05 +01:00
commit 266e31a2ff
3 changed files with 63 additions and 5 deletions

View file

@ -145,6 +145,10 @@ pub struct HeartbeatState {
/// When the heartbeat last started each Persona's turn, by instance slug.
#[cultcache(key = 2, default)]
pub last_turn_at: BTreeMap<String, DateTime<Utc>>,
/// Retired slot 3, once the owed waker's count of starts. Nothing reads or writes it beyond the default; it stays declared because deployed state
/// files hold four slots and the store refuses to save over a record with a slot its type does not declare (read-only), so the heartbeat could not save.
#[cultcache(key = 3, default)]
pub retired_slot_3: BTreeMap<String, u32>,
}
const STATE_KEY: &str = "state";
@ -846,7 +850,7 @@ pub fn poll(organs: &Organs, config: &Config) -> Result<Outcome> {
}
let now = organs.clock.now();
let stored = organs.store.load()?;
let mut state = stored.clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![], last_turn_at: BTreeMap::new() });
let mut state = stored.clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![], last_turn_at: BTreeMap::new(), retired_slot_3: BTreeMap::new() });
let mut eventful = false;
// The table as the last poll left it; unreadable counts as none, is noted in the trace, and is not overwritten.
let (stored_addressing, addressing_unread) = match organs.store.load_addressing() {

View file

@ -264,7 +264,7 @@ impl Rig {
/// The stored state, made if there is none, changed by `change`.
fn with_state(&self, change: impl FnOnce(&mut HeartbeatState)) {
let mut state = self.store.state.borrow().clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![], last_turn_at: BTreeMap::new() });
let mut state = self.store.state.borrow().clone().unwrap_or(HeartbeatState { last_beat_at: DateTime::<Utc>::UNIX_EPOCH, handled_mentions: vec![], last_turn_at: BTreeMap::new(), retired_slot_3: BTreeMap::new() });
change(&mut state);
*self.store.state.borrow_mut() = Some(state);
}
@ -670,7 +670,7 @@ fn trace_at(seconds: i64, tempo_s: u32) -> PollTrace {
}
fn state_at(milliseconds: i64) -> HeartbeatState {
HeartbeatState { last_beat_at: crate::testkit::at(milliseconds), handled_mentions: vec!["ann:e1".into()], last_turn_at: BTreeMap::from([("ann".to_string(), crate::testkit::at(3))]) }
HeartbeatState { last_beat_at: crate::testkit::at(milliseconds), handled_mentions: vec!["ann:e1".into()], last_turn_at: BTreeMap::from([("ann".to_string(), crate::testkit::at(3))]), retired_slot_3: BTreeMap::new() }
}
#[test]
@ -713,9 +713,42 @@ fn a_state_written_before_the_cut_reads_with_no_turns() {
cache.pull_all_backing_stores().unwrap();
cache.put(STATE_KEY, &StateBeforeTheCut { last_beat_at: crate::testkit::at(7), handled_mentions: vec!["ann:e1".into()] }).unwrap();
let state = FileStore::new(&path).load().unwrap().expect("the old state reads");
assert_eq!(state, HeartbeatState { last_beat_at: crate::testkit::at(7), handled_mentions: vec!["ann:e1".into()], last_turn_at: BTreeMap::new() });
assert_eq!(state, HeartbeatState { last_beat_at: crate::testkit::at(7), handled_mentions: vec!["ann:e1".into()], last_turn_at: BTreeMap::new(), retired_slot_3: BTreeMap::new() });
}
/// The state as the e951555 build wrote it, and as deployed files still hold it: four slots, the last the owed waker's count.
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
#[cultcache(type = "eureka.heartbeat.state.v1", schema = "HeartbeatState")]
struct StateWithOwedStarts {
#[cultcache(key = 0)]
last_beat_at: DateTime<Utc>,
#[cultcache(key = 1)]
handled_mentions: Vec<String>,
#[cultcache(key = 2, default)]
last_turn_at: BTreeMap<String, DateTime<Utc>>,
#[cultcache(key = 3, default)]
owed_starts: BTreeMap<String, u32>,
}
/// Soul s2 state-slot3-readonly: a deployed four-slot file loads, and the poll's save over it succeeds instead of refusing it read-only.
#[test]
fn a_four_slot_state_from_the_deployed_build_loads_and_saves() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("heartbeat.cc");
let mut cache = CultCache::new();
cache.register_entry_type::<StateWithOwedStarts>().unwrap();
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
cache.pull_all_backing_stores().unwrap();
cache.put(STATE_KEY, &StateWithOwedStarts { last_beat_at: crate::testkit::at(7), handled_mentions: vec!["ann:e1".into()], last_turn_at: BTreeMap::new(), owed_starts: BTreeMap::from([("ann".to_string(), 2)]) }).unwrap();
drop(cache);
let store = FileStore::new(&path);
let mut state = store.load().unwrap().expect("the four-slot state reads");
assert_eq!(state.handled_mentions, ["ann:e1"]);
state.last_beat_at = crate::testkit::at(9);
store.save(&state).expect("the save over a four-slot file is not refused");
assert_eq!(store.load().unwrap().unwrap().last_beat_at, crate::testkit::at(9));
}
#[test]
fn an_old_heartbeat_store_fails_closed_naming_the_file_and_is_never_migrated() {
let dir = TempDir::new().unwrap();
@ -1178,7 +1211,7 @@ fn the_file_store_view_reads_the_lines_the_poll_recorded_for_her() {
let dir = TempDir::new().unwrap();
let store = FileStore::new(dir.path().join("heartbeat.cc"));
assert!(HeartbeatView::woken_for(&store, "ann").unwrap().is_empty(), "no state yet: no claims");
store.save(&HeartbeatState { last_beat_at: at(0), handled_mentions: vec!["ann:$e1".into(), "bob:$e2".into(), "ann:$e3".into()], last_turn_at: BTreeMap::new() }).unwrap();
store.save(&HeartbeatState { last_beat_at: at(0), handled_mentions: vec!["ann:$e1".into(), "bob:$e2".into(), "ann:$e3".into()], last_turn_at: BTreeMap::new(), retired_slot_3: BTreeMap::new() }).unwrap();
assert_eq!(HeartbeatView::woken_for(&store, "ann").unwrap(), ["$e1", "$e3"]);
assert_eq!(HeartbeatView::woken_for(&store, "bob").unwrap(), ["$e2"]);
}

View file

@ -414,3 +414,24 @@ fn t_p1_a_notice_in_another_room_does_not_hold_the_ack() {
let rig = Rig::new(vec![line(1, &room(1), META, "hello"), line(2, &room(2), META, "and hello"), notice, line(4, &room(1), META, "once more")]);
assert_eq!(rig.run(Tally::default()), Outcome::Drained { wakes: 1, acked_through: Some(4) });
}
/// Soul s2 turn-room-overflow-held: a turn room too long for the page tells her how many lines said to her it left out, and every line the
/// turn holds is acked, once, in one ack that reaches the newest of them.
#[test]
fn t_p1_an_overflowed_turn_room_tells_her_the_count_and_acks_every_held_line_once() {
let lines: Vec<ChatDelivery> = (1..=200).map(|seq| line(seq, &room(1), META, &format!("line {seq} {}", "z".repeat(3990)))).collect();
let rig = Rig::new(lines);
let tally = Tally::default();
assert!(matches!(rig.run(tally.clone()), Outcome::Drained { wakes: 1, .. }));
let prompt = tally.0.borrow()[1].clone();
let told: usize = prompt.split("(").find_map(|part| part.split_once(" more said to ").map(|(n, _)| n.trim().parse().unwrap())).expect("she is told the count");
let held: Vec<u64> = rig.records()[0]["held"].as_array().unwrap().iter().map(|event| event.as_str().unwrap().trim_start_matches("$e").parse().unwrap()).collect();
assert!(told > 0 && told < 200, "{told}");
let acks = rig.acks();
let mut once = acks.clone();
once.sort_unstable();
once.dedup();
assert_eq!(once, acks, "each ack is made once, in order: {acks:?}");
assert_eq!(acks.last().copied(), held.iter().max().copied(), "the ack reaches the newest line the turn holds");
assert!(held.iter().all(|seq| acks.last().is_some_and(|last| seq <= last)), "every held line is acked: {held:?}");
}