Skip to content

fix(id): separate future clock skew from the presence TTL (#8865) - #8881

Merged
dezren39 merged 2 commits into
mainfrom
queue/15092-8865-presence-skew
Nov 12, 2026
Merged

dezren39 merged 2 commits into
mainfrom
queue/15092-8865-presence-skew

Conversation

@dezren39

@dezren39 dezren39 commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Fixes #8865. Queue task 15092 (repo-health-code-pilot, lane code-d).

Problem

Presence accepted a heartbeat stamped up to a full PRESENCE_TTL_SECS (75s) into the future and computed liveness, typing, roster, and pruning from that sender stamp: receipt at now=1000 with ts=1075 stayed live through now=1150 (~150s after receipt), and roster last_seen could be future-dated.

Change (pkgs/id/src/chat_presence.rs only)

  • New MAX_FUTURE_SKEW_SECS = 10, documented separately from PRESENCE_TTL_SECS: update_at now refuses a heartbeat stamped more than 10s ahead of the receiver's clock (the old bound was the full TTL). 10s is generous for NTP-disciplined clocks and well under one heartbeat interval.
  • Expiry basis is explicit and receipt-bounded: each entry stores an effective stamp = min(sender ts, local receipt time). Liveness, typing, roster, live-nodes, and pruning all compute age from the effective stamp, so an accepted future-dated heartbeat lives at most one TTL from receipt. A past-stamped heartbeat keeps its sender stamp as its basis, so re-delivery still cannot extend its life; a redelivered future heartbeat keeps the earliest effective stamp it earned.
  • last_seen reports the effective stamp, so it is never future-dated.
  • Ordering and replay protections unchanged (sender ts still orders entries; stale and far-future stamps are refused).

Tests

New boundary tests (oracle: expiry measured from receipt):

  • at the allowance (ts = receipt + 10): accepted, live at receipt + TTL, offline at receipt + TTL + 1; typing expires at receipt + TYPING_TTL; last_seen == receipt; prune on the same basis.
  • just beyond the allowance (receipt + 11, and the old receipt + 75 stamp): refused outright.
  • redelivery inside the skew window keeps the first effective stamp.

cargo test -p id --lib chat_presence (from pkgs/id): 22 passed, including all pre-existing presence tests (u64::MAX rejection, out-of-order/replay, duplicate-delivery expiry). The changed file is rustfmt-clean.

Note: a full cargo test -p id build currently fails to compile the unrelated integration test tests/probe_attestation_sign.rs (calls sign_claim with 5 args; the function in crates/id-events takes 6) — that error is in files this PR does not touch and exists at the base commit; the lib itself compiles and its tests pass.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

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.
@dezren39
dezren39 requested a review from a team as a code owner November 12, 2026 07:49
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 23 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 43ca566b-bb97-4e87-a447-294b0d6f0895
Commits

Reviewing files that changed from the base of the PR and between 593e266 and d7619c5.

Files selected for processing: 1

  • pkgs/id/src/chat_presence.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T07:59:01.797963Z d7619c5 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6fc5c55768

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkgs/id/src/chat_presence.rs Outdated
};
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain replay protection until the sender stamp expires

For an accepted future-dated heartbeat, pruning now deletes its replay protection while its sender stamp still passes the freshness check. For example, a heartbeat stamped 1010 and received at 1000 is pruned at 1076; replaying it at 1076 then inserts it with effective_ts = 1010, resurrecting the departed node through 1085 instead of preserving the receipt-based expiry at 1075. The periodic cleanup in commands/serve.rs calls this method, so this can occur during normal operation. Retain the entry or a replay watermark until the sender stamp itself exceeds the freshness bound, while continuing to calculate liveness from the effective stamp.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d7619c5 — thank you, this was a real hole in the first version.

Disposition: prune_at no longer prunes on the effective (receipt-clamped) stamp. An entry is now retained as a replay watermark until its sender stamp ages past PRESENCE_TTL_SECS — exactly the retention option the finding suggests — while liveness and typing continue to expire on the effective stamp. A replay inside that window is an equal-stamp tie that keeps the first effective stamp, so it cannot resurrect the node; once the watermark is pruned, the same replay fails the freshness bound.

New regression test replay_after_receipt_expiry_does_not_resurrect_a_future_dated_heartbeat covers the finding's exact scenario (stamp 1010 received at 1000, replay at 1076: still Offline, last_seen stays 1000; watermark pruned at 1086 and the replay then refused). cargo test -p id --lib chat_presence: 23 passed.

…r 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.
@dezren39
dezren39 merged commit 5f31d7a into main Nov 12, 2026
9 of 27 checks passed
@dezren39
dezren39 deleted the queue/15092-8865-presence-skew branch November 12, 2026 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

id(chat-presence): separate future clock skew from the presence TTL

1 participant