From 6fc5c5576828c1e80b4aaf22a87fd0536d188732 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 01:49:18 -0600 Subject: [PATCH 1/2] fix(id): separate future clock skew from the presence TTL (#8865) Presence accepted a heartbeat stamped up to a full PRESENCE_TTL_SECS (75s) in the future and computed liveness, typing, roster, and pruning from that sender stamp, so a future-dated heartbeat stayed live for up to ~150s after receipt and roster last_seen could be future-dated. - Add MAX_FUTURE_SKEW_SECS = 10, documented separately from the TTL: heartbeats stamped further ahead are refused in update_at. - Expiry now runs from an effective stamp, min(sender ts, local receipt): an accepted future-dated heartbeat lives at most one TTL from receipt, a past-stamped one keeps its sender stamp so replay cannot extend it, and a redelivered future heartbeat keeps its earliest effective stamp. - Roster last_seen reports the effective stamp, never a future time. Tests: boundary at the skew allowance (expiry from receipt, typing and prune on the same basis), just beyond it (refused, including the old receipt+75 stamp), and redelivery keeping the first effective stamp. cargo test -p id --lib chat_presence: 22 passed. --- pkgs/id/src/chat_presence.rs | 173 +++++++++++++++++++++++++++++++---- 1 file changed, 153 insertions(+), 20 deletions(-) diff --git a/pkgs/id/src/chat_presence.rs b/pkgs/id/src/chat_presence.rs index 7b39852bdc..7b2ec490ce 100644 --- a/pkgs/id/src/chat_presence.rs +++ b/pkgs/id/src/chat_presence.rs @@ -9,6 +9,20 @@ //! its last heartbeat ages past [`PRESENCE_TTL_SECS`]; when it resumes, the next //! heartbeat makes it live again. //! +//! # Clock skew and the expiry basis (#8865) +//! +//! A sender's clock is not the receiver's clock. Expiry is therefore NOT +//! measured from the sender-provided stamp alone: a heartbeat is accepted +//! only when its stamp is at most [`MAX_FUTURE_SKEW_SECS`] ahead of the +//! receiver's clock — a small allowance documented separately from +//! [`PRESENCE_TTL_SECS`] — and the entry's expiry runs from its *effective* +//! stamp, `min(sender stamp, local receipt time)`. An accepted future-dated +//! heartbeat thus stays live for at most one TTL *from receipt*, never a +//! TTL plus its skew, and roster `last_seen` reports the effective stamp, +//! so it is never future-dated. A heartbeat stamped in the past keeps its +//! sender stamp as its effective stamp, so re-delivery cannot extend its +//! life either (see [`PresenceTable::update_at`]). +//! //! # Never journaled //! //! Presence is **never** written to the signed event journal, event sync, or replay. @@ -62,6 +76,20 @@ use crate::waiting::AuthorKey; /// #2809 specifies. A node silent for longer than this reads as gone. pub const PRESENCE_TTL_SECS: u64 = 75; +/// Maximum future clock skew accepted on a heartbeat, in seconds: 10. +/// +/// This allowance is deliberately separate from — and much smaller than — +/// [`PRESENCE_TTL_SECS`]: the TTL is how long silence may last before a node +/// reads as gone, while this bound is only how far a sender's clock may run +/// ahead of the receiver's before its stamps stop being trusted at all. +/// 10 s is generous for NTP-disciplined clocks and well under one +/// [`HEARTBEAT_INTERVAL`], so a node whose clock is merely fast still lands +/// two to three heartbeats inside every TTL window. A heartbeat stamped +/// further ahead is refused outright (see [`PresenceTable::update_at`]), +/// and an accepted future stamp never extends liveness past one TTL from +/// receipt, because expiry runs from the receipt-clamped effective stamp. +pub const MAX_FUTURE_SKEW_SECS: u64 = 10; + /// How often a node broadcasts its presence heartbeat. Two to three heartbeats land /// inside one TTL window, so a single dropped gossip message never flickers a live /// node stale. @@ -190,18 +218,27 @@ pub struct RosterEntry { /// Whether the member is live in the channel right now. pub liveness: Liveness, /// Unix seconds of the member's most recent accepted heartbeat for the channel, - /// or `None` if the member has never been seen. + /// or `None` if the member has never been seen. This is the entry's + /// *effective* stamp — the sender stamp clamped to the local receipt + /// time — so a future-dated sender stamp never makes `last_seen` read + /// as a time in the future (#8865). pub last_seen: Option, /// Whether the member's typing claim for the channel is currently live. pub typing: bool, } -/// The latest accepted heartbeat for one node. Expiry is computed from the -/// heartbeat's own stamp, never from a local arrival time, so re-delivering a -/// captured heartbeat cannot extend its life (see [`PresenceTable::update_at`]). +/// The latest accepted heartbeat for one node, plus its *effective* stamp: +/// `min(heartbeat.ts, local receipt time)` at acceptance. Expiry (liveness, +/// typing, pruning) is computed from the effective stamp, never from the raw +/// sender stamp and never from a later arrival time: a past-stamped +/// heartbeat keeps its own stamp as the basis, so re-delivering a captured +/// heartbeat cannot extend its life, and a future-stamped heartbeat accepted +/// inside [`MAX_FUTURE_SKEW_SECS`] is clamped to its receipt time, so its +/// skew cannot extend its life either (see [`PresenceTable::update_at`]). #[derive(Clone, Debug)] struct Entry { heartbeat: PresenceHeartbeat, + effective_ts: u64, } /// Thread-safe in-memory table of the latest presence heartbeat per node. @@ -253,14 +290,17 @@ impl PresenceTable { /// /// - **Freshness bound.** A heartbeat stamped older than [`PRESENCE_TTL_SECS`] /// relative to `now` is refused — replaying a captured heartbeat cannot keep a - /// departed node live — and so is one stamped more than the TTL in the future, - /// which could otherwise poison the ordering check below. + /// departed node live — and so is one stamped more than + /// [`MAX_FUTURE_SKEW_SECS`] in the future (#8865: the skew allowance is + /// separate from, and much smaller than, the TTL), which could otherwise + /// poison the ordering check below and stretch liveness past the TTL. /// - **Ordering.** A heartbeat older than the stored one for the same node is /// dropped: out-of-order delivery never regresses the table. Equal timestamps /// are a tie; the most recent arrival wins — but because expiry is computed - /// from the stamp (`ts + PRESENCE_TTL_SECS`), a duplicate delivery of the - /// same heartbeat never extends its life, and no claim can outlive its - /// stamp's freshness bound no matter when copies of it arrive. + /// from the *effective* stamp (`min(ts, receipt time)` at first acceptance), + /// a duplicate delivery of the same heartbeat never extends its life, and + /// no claim can outlive `effective_ts + PRESENCE_TTL_SECS` — at most one + /// TTL from receipt — no matter when copies of it arrive. /// - **Scoping.** Channels (and typing claims) the sender is not a member of, /// per `is_member`, are filtered out before the heartbeat is stored, and /// duplicates are dropped: a wire-decoded heartbeat bypasses @@ -275,7 +315,7 @@ impl PresenceTable { is_member: &dyn Fn(&AuthorKey, &Hash) -> bool, ) -> bool { if now.saturating_sub(heartbeat.ts) > PRESENCE_TTL_SECS - || heartbeat.ts.saturating_sub(now) > PRESENCE_TTL_SECS + || heartbeat.ts.saturating_sub(now) > MAX_FUTURE_SKEW_SECS { return false; } @@ -308,7 +348,24 @@ impl PresenceTable { { return false; } - entries.insert(node, Entry { heartbeat }); + // The effective stamp (#8865): a past stamp stands as-is; a future + // stamp inside the skew allowance is clamped to the receipt time, + // so neither replay nor skew can push expiry past its basis. A + // duplicate delivery of the same stamp keeps the earliest + // effective stamp that stamp ever earned. + let mut effective_ts = heartbeat.ts.min(now); + if let Some(existing) = entries.get(&node) + && heartbeat.ts == existing.heartbeat.ts + { + effective_ts = effective_ts.min(existing.effective_ts); + } + entries.insert( + node, + Entry { + heartbeat, + effective_ts, + }, + ); true } @@ -330,7 +387,7 @@ impl PresenceTable { match entries.get(node) { Some(entry) if entry.heartbeat.channels.contains(channel) - && now.saturating_sub(entry.heartbeat.ts) <= PRESENCE_TTL_SECS => + && now.saturating_sub(entry.effective_ts) <= PRESENCE_TTL_SECS => { Liveness::Live } @@ -348,7 +405,7 @@ impl PresenceTable { .iter() .filter(|(_, entry)| { entry.heartbeat.channels.contains(channel) - && now.saturating_sub(entry.heartbeat.ts) <= PRESENCE_TTL_SECS + && now.saturating_sub(entry.effective_ts) <= PRESENCE_TTL_SECS }) .map(|(node, _)| *node) .collect(); @@ -382,7 +439,7 @@ impl PresenceTable { .get(&node) .filter(|entry| entry.heartbeat.channels.contains(channel)); let liveness = match entry { - Some(entry) if now.saturating_sub(entry.heartbeat.ts) <= PRESENCE_TTL_SECS => { + Some(entry) if now.saturating_sub(entry.effective_ts) <= PRESENCE_TTL_SECS => { Liveness::Live } _ => Liveness::Offline, @@ -390,12 +447,12 @@ impl PresenceTable { RosterEntry { node, liveness, - last_seen: entry.map(|entry| entry.heartbeat.ts), + last_seen: entry.map(|entry| entry.effective_ts), typing: match entry { Some(entry) => { liveness == Liveness::Live && entry.heartbeat.is_typing_in(channel) - && now.saturating_sub(entry.heartbeat.ts) <= TYPING_TTL_SECS + && now.saturating_sub(entry.effective_ts) <= TYPING_TTL_SECS } None => false, }, @@ -434,15 +491,15 @@ impl PresenceTable { Some(self.roster_at(channel, members, now)) } - /// Removes entries whose last heartbeat arrived more than [`PRESENCE_TTL_SECS`] - /// before `now`, returning how many were removed. Stale entries already read as - /// gone; pruning only bounds the table's size. + /// Removes entries whose last heartbeat's effective stamp is more than + /// [`PRESENCE_TTL_SECS`] before `now`, returning how many were removed. + /// Stale entries already read as gone; pruning only bounds the table's size. pub fn prune_at(&self, now: u64) -> usize { let Ok(mut entries) = self.entries.write() else { return 0; }; let before = entries.len(); - entries.retain(|_, entry| now.saturating_sub(entry.heartbeat.ts) <= PRESENCE_TTL_SECS); + entries.retain(|_, entry| now.saturating_sub(entry.effective_ts) <= PRESENCE_TTL_SECS); before - entries.len() } @@ -689,6 +746,82 @@ mod tests { assert!(!table.update_at(heartbeat(alice, vec![chan], u64::MAX), 6_100, &|_, _| true)); } + #[test] + fn future_skew_at_the_allowance_expires_from_receipt_not_from_the_stamp() { + // #8865 oracle, boundary at the allowance: a heartbeat stamped exactly + // MAX_FUTURE_SKEW_SECS ahead of receipt is accepted, but its expiry + // runs from receipt — live at receipt + TTL, gone one second later, + // NOT live until stamp + TTL. Typing rides the same basis, and + // last_seen reports the receipt-clamped effective stamp. + let table = PresenceTable::new(); + let chan = channel("general"); + let alice = key(1); + let receipt = 1_000; + let mut hb = heartbeat(alice, vec![chan], receipt + MAX_FUTURE_SKEW_SECS); + hb.typing = vec![chan]; + assert!(table.update_at(hb, receipt, &|_, _| true)); + assert_eq!(table.liveness_at(&alice, &chan, receipt), Liveness::Live); + assert_eq!( + table.liveness_at(&alice, &chan, receipt + PRESENCE_TTL_SECS), + Liveness::Live + ); + assert_eq!( + table.liveness_at(&alice, &chan, receipt + PRESENCE_TTL_SECS + 1), + Liveness::Offline, + "a future-dated heartbeat must not stay live past one TTL from receipt" + ); + // Typing expires on the typing TTL from receipt, not from the stamp. + let roster = table.roster_at(&chan, &[alice], receipt + TYPING_TTL_SECS); + assert!(roster[0].typing); + assert_eq!(roster[0].last_seen, Some(receipt)); + let roster = table.roster_at(&chan, &[alice], receipt + TYPING_TTL_SECS + 1); + assert!(!roster[0].typing); + // Pruning uses the same basis. + assert_eq!(table.prune_at(receipt + PRESENCE_TTL_SECS + 1), 1); + assert_eq!(table.count(), 0); + } + + #[test] + fn future_skew_just_beyond_the_allowance_is_refused() { + // #8865 oracle, boundary just beyond the allowance — including the + // stamp the old TTL-sized allowance used to accept (receipt + 75): + // both are refused outright, so neither can extend liveness at all. + let table = PresenceTable::new(); + let chan = channel("general"); + let alice = key(1); + let receipt = 2_000; + assert!(!table.update_at( + heartbeat(alice, vec![chan], receipt + MAX_FUTURE_SKEW_SECS + 1), + receipt, + &|_, _| true + )); + assert!(!table.update_at( + heartbeat(alice, vec![chan], receipt + PRESENCE_TTL_SECS), + receipt, + &|_, _| true + )); + assert_eq!(table.count(), 0); + assert_eq!(table.liveness_at(&alice, &chan, receipt), Liveness::Offline); + } + + #[test] + fn redelivered_future_heartbeat_keeps_its_first_effective_stamp() { + // Re-delivery inside the skew window must not re-clamp to the later + // receipt and so extend the claim: the earliest effective stamp wins. + let table = PresenceTable::new(); + let chan = channel("general"); + let alice = key(1); + let hb = heartbeat(alice, vec![chan], 3_010); + assert!(table.update_at(hb.clone(), 3_000, &|_, _| true)); + assert!(table.update_at(hb, 3_010, &|_, _| true)); + assert_eq!( + table.liveness_at(&alice, &chan, 3_000 + PRESENCE_TTL_SECS + 1), + Liveness::Offline + ); + let roster = table.roster_at(&chan, &[alice], 3_010); + assert_eq!(roster[0].last_seen, Some(3_000)); + } + #[test] fn frames_require_the_presence_magic() { let chan = channel("general"); From d7619c5fe6c6f7a54709216fadbdc1cc443471d7 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 01:56:59 -0600 Subject: [PATCH 2/2] fix(id): retain presence entries as replay watermarks until the sender stamp expires (#8865) Codex P2 on PR #8881: pruning on the effective (receipt-clamped) stamp deleted replay protection while a future-dated sender stamp still passed the freshness bound, so a replay could resurrect the node past its receipt-based expiry. prune_at now retains an entry until its sender stamp ages past PRESENCE_TTL_SECS, while liveness/typing keep expiring on the effective stamp. New regression test covers replay-after-expiry, watermark retention, and freshness refusal after the watermark is pruned. --- pkgs/id/src/chat_presence.rs | 67 +++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/pkgs/id/src/chat_presence.rs b/pkgs/id/src/chat_presence.rs index 7b2ec490ce..18fc8a7f68 100644 --- a/pkgs/id/src/chat_presence.rs +++ b/pkgs/id/src/chat_presence.rs @@ -18,7 +18,9 @@ //! [`PRESENCE_TTL_SECS`] — and the entry's expiry runs from its *effective* //! stamp, `min(sender stamp, local receipt time)`. An accepted future-dated //! heartbeat thus stays live for at most one TTL *from receipt*, never a -//! TTL plus its skew, and roster `last_seen` reports the effective stamp, +//! TTL plus its skew (pruning retains the entry a little longer, purely as +//! a replay watermark — see [`PresenceTable::prune_at`]), and roster +//! `last_seen` reports the effective stamp, //! so it is never future-dated. A heartbeat stamped in the past keeps its //! sender stamp as its effective stamp, so re-delivery cannot extend its //! life either (see [`PresenceTable::update_at`]). @@ -228,8 +230,8 @@ pub struct RosterEntry { } /// The latest accepted heartbeat for one node, plus its *effective* stamp: -/// `min(heartbeat.ts, local receipt time)` at acceptance. Expiry (liveness, -/// typing, pruning) is computed from the effective stamp, never from the raw +/// `min(heartbeat.ts, local receipt time)` at acceptance. Expiry (liveness +/// and typing) is computed from the effective stamp, never from the raw /// sender stamp and never from a later arrival time: a past-stamped /// heartbeat keeps its own stamp as the basis, so re-delivering a captured /// heartbeat cannot extend its life, and a future-stamped heartbeat accepted @@ -491,15 +493,21 @@ impl PresenceTable { Some(self.roster_at(channel, members, now)) } - /// Removes entries whose last heartbeat's effective stamp is more than - /// [`PRESENCE_TTL_SECS`] before `now`, returning how many were removed. - /// Stale entries already read as gone; pruning only bounds the table's size. + /// Removes entries whose last heartbeat can no longer be replayed into + /// the table, returning how many were removed. Liveness expires on the + /// effective stamp, but an entry is retained as a replay watermark until + /// its *sender* stamp also ages past [`PRESENCE_TTL_SECS`]: pruning on + /// the effective stamp alone would delete a future-dated entry while + /// its stamp still passes the freshness bound, and a replay would then + /// re-insert it with a later effective stamp and resurrect the node + /// past its receipt-based expiry (#8865). Stale entries already read as + /// gone; pruning only bounds the table's size. pub fn prune_at(&self, now: u64) -> usize { let Ok(mut entries) = self.entries.write() else { return 0; }; let before = entries.len(); - entries.retain(|_, entry| now.saturating_sub(entry.effective_ts) <= PRESENCE_TTL_SECS); + entries.retain(|_, entry| now.saturating_sub(entry.heartbeat.ts) <= PRESENCE_TTL_SECS); before - entries.len() } @@ -776,8 +784,49 @@ mod tests { assert_eq!(roster[0].last_seen, Some(receipt)); let roster = table.roster_at(&chan, &[alice], receipt + TYPING_TTL_SECS + 1); assert!(!roster[0].typing); - // Pruning uses the same basis. - assert_eq!(table.prune_at(receipt + PRESENCE_TTL_SECS + 1), 1); + // Pruning retains the entry past its liveness expiry as a replay + // watermark (see the replay test below); it is removed once the + // sender stamp itself ages past the TTL. + assert_eq!(table.prune_at(receipt + PRESENCE_TTL_SECS + 1), 0); + assert_eq!( + table.prune_at(receipt + MAX_FUTURE_SKEW_SECS + PRESENCE_TTL_SECS + 1), + 1 + ); + assert_eq!(table.count(), 0); + } + + #[test] + fn replay_after_receipt_expiry_does_not_resurrect_a_future_dated_heartbeat() { + // #8865, Codex finding on the fix: a future-dated heartbeat pruned on + // its effective stamp could be replayed while its sender stamp is + // still fresh, re-entering with a later effective stamp. The entry + // is therefore retained as a watermark: the replay is a tie that + // keeps the first effective stamp, liveness stays Offline, and once + // the watermark itself is pruned the stamp is too old to replay. + let table = PresenceTable::new(); + let chan = channel("general"); + let alice = key(1); + let hb = heartbeat(alice, vec![chan], 1_010); + assert!(table.update_at(hb.clone(), 1_000, &|_, _| true)); + let after_expiry = 1_000 + PRESENCE_TTL_SECS + 1; + assert_eq!( + table.liveness_at(&alice, &chan, after_expiry), + Liveness::Offline + ); + assert_eq!(table.prune_at(after_expiry), 0, "watermark retained"); + assert!(table.update_at(hb.clone(), after_expiry, &|_, _| true)); + assert_eq!( + table.liveness_at(&alice, &chan, after_expiry), + Liveness::Offline, + "a replay must not resurrect the node past its receipt-based expiry" + ); + let roster = table.roster_at(&chan, &[alice], after_expiry); + assert_eq!(roster[0].last_seen, Some(1_000)); + // Once the sender stamp ages past the TTL the watermark is pruned, + // and the same replay is refused by the freshness bound. + let stamp_expired = 1_010 + PRESENCE_TTL_SECS + 1; + assert_eq!(table.prune_at(stamp_expired), 1); + assert!(!table.update_at(hb, stamp_expired, &|_, _| true)); assert_eq!(table.count(), 0); }