From 2f7defe551512704dbe26c33a9827d75bae4a031 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 03:08:36 +0000 Subject: [PATCH] id-worlds: record join and revoke times in the world journal Issued and Revoked journal entries carry optional issued_at and revoked_at (journal version 12). The owner report counts joins and revokes in a window from them and says when the count is incomplete; `id world owner` prints the counts and times. Refs #11960 Co-Authored-By: Claude Opus 5.5 --- pkgs/id/benches/world_bench.rs | 1 + .../crates/id-cli/src/commands/world/owner.rs | 103 +++++++- pkgs/id/crates/id-worlds/src/world/actor.rs | 17 +- pkgs/id/crates/id-worlds/src/world/core.rs | 27 +- .../id-worlds/src/world/core/operations.rs | 23 ++ pkgs/id/crates/id-worlds/src/world/journal.rs | 28 +- pkgs/id/crates/id-worlds/src/world/mod.rs | 10 +- pkgs/id/crates/id-worlds/src/world/replica.rs | 10 +- pkgs/id/crates/id-worlds/src/world/tests.rs | 242 ++++++++++++++++-- .../id-worlds/src/world_owner_report.rs | 106 +++++++- .../id-worlds/src/world_owner_report_tests.rs | 183 +++++++++++++ .../id-worlds/src/world_session/tests.rs | 2 +- pkgs/id/crates/id-worlds/src/world_store.rs | 13 +- pkgs/id/tests/probe_restoreid0.rs | 1 + pkgs/id/tests/world_roles.rs | 4 +- 15 files changed, 720 insertions(+), 50 deletions(-) diff --git a/pkgs/id/benches/world_bench.rs b/pkgs/id/benches/world_bench.rs index 1cdc179567..9d8ec82e92 100644 --- a/pkgs/id/benches/world_bench.rs +++ b/pkgs/id/benches/world_bench.rs @@ -1236,6 +1236,7 @@ impl loadgen::Target for ReplayTarget { uses: None, used: 0, subject: None, + issued_at: None, }); for seq in 1..=self.events { journal.push(JournalEntry::Committed { diff --git a/pkgs/id/crates/id-cli/src/commands/world/owner.rs b/pkgs/id/crates/id-cli/src/commands/world/owner.rs index 61b9089fac..bca1f642a9 100644 --- a/pkgs/id/crates/id-cli/src/commands/world/owner.rs +++ b/pkgs/id/crates/id-cli/src/commands/world/owner.rs @@ -9,6 +9,7 @@ //! the live-only sections (connected members, program runs) are always //! unavailable here, and it writes nothing. +use std::fmt::Write as _; use std::fs::File; use std::io::{BufRead, BufReader, Read}; use std::path::Path; @@ -133,7 +134,33 @@ pub(super) fn render_text(report: &OwnerReport, since_secs: u64) -> String { .map_or_else(|| "unknown".to_owned(), |n| n.to_string()); out.push(format!("forked from: {from} ({inherited} joins inherited)")); } - out.push(format!("joins in window: {}", measured(&joins.in_window))); + out.push(format!( + "joins in window: {}{}", + measured(&joins.in_window), + if joins.in_window.value().is_some() && !joins.in_window_complete { + ", incomplete: the window starts before join times were recorded" + } else { + "" + } + )); + out.push(format!( + "join times recorded since: {}{}", + measured(&joins.available_since), + if joins.untimed > 0 { + format!(" ({} joins have no time)", joins.untimed) + } else { + String::new() + } + )); + out.push(format!( + "revokes in window: {}{}", + measured(&joins.revoked_in_window), + if joins.revoked_untimed > 0 && joins.revoked_in_window.value().is_some() { + format!(" ({} revokes have no time)", joins.revoked_untimed) + } else { + String::new() + } + )); out.push(format!( "active members: {}", measured(&report.members.active_count) @@ -171,11 +198,18 @@ pub(super) fn render_text(report: &OwnerReport, since_secs: u64) -> String { if join.revoked { tags.push("revoked"); } - if tags.is_empty() { + let mut label = if tags.is_empty() { join.participant_id.to_string() } else { format!("{} ({})", join.participant_id, tags.join(", ")) + }; + if let Some(at) = join.issued_at_unix_ms { + let _ = write!(label, " joined {at}"); + } + if let Some(at) = join.revoked_at_unix_ms { + let _ = write!(label, " revoked {at}"); } + label }) .collect(); out.push(format!("recent joins, newest first: {}", recent.join("; "))); @@ -250,9 +284,74 @@ mod tests { uses: None, used: 0, subject: None, + issued_at: None, } } + fn issued_at(id: u64, at: u64) -> JournalEntry { + let JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + .. + } = issued(id, None) + else { + unreachable!() + }; + JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + issued_at: Some(at), + } + } + + #[test] + fn text_form_shows_join_and_revoke_times() { + let v = id_worlds::world::JOURNAL_VERSION_JOIN_TIMES; + let dir = world_dir(&[ + JournalEntry::Created { + world_id: "w-cli".to_owned(), + version: v, + }, + issued(1, None), + issued_at(2, 4_000), + JournalEntry::Revoked { + participant_id: 2, + by: None, + revoked_at: Some(4_500), + }, + ]); + let report = owner_report(dir.path(), 3, 5_000).unwrap(); + let text = render_text(&report, 3); + assert!(text.contains("joins in window: 1, incomplete"), "{text}"); + assert!( + text.contains("join times recorded since: 4000 (1 joins have no time)"), + "{text}" + ); + assert!(text.contains("revokes in window: 1"), "{text}"); + assert!( + text.contains("recent joins, newest first: 2 (revoked) joined 4000 revoked 4500; 1"), + "{text}" + ); + let json = serde_json::to_value(&report).unwrap(); + assert_eq!(json["joins"]["available_since"]["value"], 4_000); + assert_eq!( + json["joins"]["recent"][1]["issued_at_unix_ms"], + serde_json::Value::Null + ); + } + #[test] fn reports_a_local_world_offline() { let dir = world_dir(&[created(), issued(1, None), issued(2, Some(1))]); diff --git a/pkgs/id/crates/id-worlds/src/world/actor.rs b/pkgs/id/crates/id-worlds/src/world/actor.rs index afe420f00a..6aa62e1a3d 100644 --- a/pkgs/id/crates/id-worlds/src/world/actor.rs +++ b/pkgs/id/crates/id-worlds/src/world/actor.rs @@ -1168,12 +1168,17 @@ impl WorldActor { let _ = reply.send(false); return; } - let revoked = self.core.revoke_tree_by(participant_id, by); + let revoked_at = Some(self.core.clock().now_ms()); + let revoked = self.core.revoke_tree_at(participant_id, by, revoked_at); let was_active = !revoked.is_empty(); if was_active { // Fail-closed: surface a persist failure rather than // reporting an audited success (design #835 phase 2). - let persisted = self.persist(JournalEntry::Revoked { participant_id, by }); + let persisted = self.persist(JournalEntry::Revoked { + participant_id, + by, + revoked_at, + }); for id in revoked { let _ = self.revocation_sender.send(id); } @@ -1211,7 +1216,10 @@ impl WorldActor { ); Ok(false) } else { - let revoked = self.core.revoke_tree_by(participant_id, Some(by)); + let revoked_at = Some(self.core.clock().now_ms()); + let revoked = + self.core + .revoke_tree_at(participant_id, Some(by), revoked_at); let was_active = !revoked.is_empty(); if was_active { // Fail-closed: the in-memory revocation and @@ -1222,6 +1230,7 @@ impl WorldActor { let persisted = self.persist(JournalEntry::Revoked { participant_id, by: Some(by), + revoked_at, }); for id in revoked { let _ = self.revocation_sender.send(id); @@ -2600,6 +2609,7 @@ mod replication_feed_tests { use proptest::prelude::*; prop_oneof![ any::().prop_map(|participant_id| JournalEntry::Revoked { + revoked_at: None, participant_id, by: None, }), @@ -2635,6 +2645,7 @@ mod replication_feed_tests { // the journal TOTAL still ranges past it via the entry count. prop_oneof![ any::().prop_map(|participant_id| JournalEntry::Revoked { + revoked_at: None, participant_id, by: None, }), diff --git a/pkgs/id/crates/id-worlds/src/world/core.rs b/pkgs/id/crates/id-worlds/src/world/core.rs index 5f8bde6a1a..5e02770368 100644 --- a/pkgs/id/crates/id-worlds/src/world/core.rs +++ b/pkgs/id/crates/id-worlds/src/world/core.rs @@ -623,6 +623,7 @@ impl WorldCore { uses, used, subject, + issued_at, } => { let digest = hex_decode_32(&digest).context("malformed capability digest")?; // #2972: participant 0 is the reserved host/system @@ -699,6 +700,7 @@ impl WorldCore { } let mut ancestor = parent; let mut revoked_by_ancestor = false; + let mut ancestor_revoked_at = None; while let Some(ancestor_id) = ancestor { let ancestor_record = core .capabilities @@ -706,6 +708,7 @@ impl WorldCore { .context("delegated capability ancestor disappeared during restore")?; if ancestor_record.revoked { revoked_by_ancestor = true; + ancestor_revoked_at = ancestor_record.revoked_at; break; } ancestor = ancestor_record.parent; @@ -744,16 +747,30 @@ impl WorldCore { bounds: CapabilityBounds { expires_at, uses }, used, subject, + issued_at, + revoked_at: ancestor_revoked_at, }, ); if revoked_by_ancestor { inherited_revocations.insert(issued_participant_id); } } - JournalEntry::Revoked { participant_id, by } => { - if !inherited_revocations.remove(&participant_id) { + JournalEntry::Revoked { + participant_id, + by, + revoked_at, + } => { + if inherited_revocations.remove(&participant_id) { + // Revoked with an ancestor before this checkpointed + // entry was read: keep the time the entry carries. + if let Some(record) = core.capabilities.get_mut(&participant_id) { + record.revoked_at = revoked_at; + } + } else { ensure!( - !core.revoke_tree_by(participant_id, by).is_empty(), + !core + .revoke_tree_at(participant_id, by, revoked_at) + .is_empty(), "journal revokes unknown or revoked participant {participant_id}" ); } @@ -1644,6 +1661,10 @@ impl WorldCore { .capabilities .get(&id) .and_then(|record| record.revoked_by), + revoked_at: self + .capabilities + .get(&id) + .and_then(|record| record.revoked_at), }); } } diff --git a/pkgs/id/crates/id-worlds/src/world/core/operations.rs b/pkgs/id/crates/id-worlds/src/world/core/operations.rs index d97b8b62d5..bc829fe7cd 100644 --- a/pkgs/id/crates/id-worlds/src/world/core/operations.rs +++ b/pkgs/id/crates/id-worlds/src/world/core/operations.rs @@ -212,6 +212,7 @@ impl WorldCore { uses: record.bounds.uses, used: record.used, subject: record.subject.clone(), + issued_at: record.issued_at, }) } @@ -487,6 +488,7 @@ impl WorldCore { let token = format!("{id}.{}", hex_encode(&secret)); let digest = token_digest(&token); let participant = Participant { id, display_name }; + let issued_at = Some(self.clock().now_ms()); self.capabilities.insert( id, CapabilityRecord { @@ -499,6 +501,8 @@ impl WorldCore { bounds, used: 0, subject, + issued_at, + revoked_at: None, }, ); Ok((participant, JoinCapability(token))) @@ -532,12 +536,31 @@ impl WorldCore { /// who ordered it; `None` for the admin) on the revoked root so a /// checkpoint keeps the audit actor (#11778). pub fn revoke_tree_by(&mut self, participant_id: u64, by: Option) -> Vec { + self.revoke_tree_at(participant_id, by, None) + } + + /// [`Self::revoke_tree_by`], also recording `at` (Unix ms) as the + /// revoke time of every capability it newly revokes (#11960), so the + /// owner report and a checkpoint keep when each one stopped working. + pub(crate) fn revoke_tree_at( + &mut self, + participant_id: u64, + by: Option, + at: Option, + ) -> Vec { let revoked = self.revoke_tree(participant_id); if !revoked.is_empty() && let Some(record) = self.capabilities.get_mut(&participant_id) { record.revoked_by = by; } + if at.is_some() { + for id in &revoked { + if let Some(record) = self.capabilities.get_mut(id) { + record.revoked_at = at; + } + } + } revoked } diff --git a/pkgs/id/crates/id-worlds/src/world/journal.rs b/pkgs/id/crates/id-worlds/src/world/journal.rs index 65ae1e778d..b91ad6a349 100644 --- a/pkgs/id/crates/id-worlds/src/world/journal.rs +++ b/pkgs/id/crates/id-worlds/src/world/journal.rs @@ -229,6 +229,12 @@ pub enum JournalEntry { /// Account the capability acts for; its ceiling and friendships bound it. #[serde(default, skip_serializing_if = "Option::is_none")] subject: Option, + /// Unix milliseconds at which the world issued the capability, from + /// the world clock (#11960). `None` for capabilities issued before + /// times were recorded; never inferred. Requires + /// [`JOURNAL_VERSION_JOIN_TIMES`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + issued_at: Option, }, /// A capability was revoked, with every capability delegated from it. /// Replay revokes the same tree. @@ -239,6 +245,11 @@ pub enum JournalEntry { /// from a moderator scope rather than the admin. `None` means admin. #[serde(default, skip_serializing_if = "Option::is_none")] by: Option, + /// Unix milliseconds at which the revoke took effect, from the world + /// clock (#11960). `None` for revokes recorded before times were. + /// Requires [`JOURNAL_VERSION_JOIN_TIMES`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + revoked_at: Option, }, /// A builder-scoped participant installed a program. Journal-only: it is /// the audit record for the builder authority, written alongside the @@ -735,10 +746,18 @@ pub const JOURNAL_VERSION_PORTALS: u32 = 10; /// journal at the header check. pub const JOURNAL_VERSION_PROGRAM_BUNDLE: u32 = 11; +/// Journal version 12 (#11960): `Issued` entries carry `issued_at` and +/// `Revoked` entries carry `revoked_at`. The fields are optional, so a +/// version-11 reader would parse such an entry, drop the time and lose it +/// at its next checkpoint; an entry that carries a time therefore requires +/// this version, and an older binary refuses the journal at the header +/// check instead. +pub const JOURNAL_VERSION_JOIN_TIMES: u32 = 12; + /// Newest journal version this binary can read. Journals with a higher /// header version are refused at the header check, before any entry is /// applied. -pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_PROGRAM_BUNDLE; +pub const MAX_JOURNAL_VERSION: u32 = JOURNAL_VERSION_JOIN_TIMES; /// The header version a journal must carry once it contains `entry`: version 2 /// for execution-profile entries, version 3 for region-handoff entries, @@ -879,6 +898,13 @@ fn entry_kind_version(entry: &JournalEntry) -> u32 { JOURNAL_VERSION_EXECUTION_PROFILE.max(budget_version(Some(profile))) } JournalEntry::BuilderInstalled { .. } => JOURNAL_VERSION_EXECUTION_PROFILE, + JournalEntry::Issued { + issued_at: Some(_), .. + } + | JournalEntry::Revoked { + revoked_at: Some(_), + .. + } => JOURNAL_VERSION_JOIN_TIMES, _ => JOURNAL_VERSION, } } diff --git a/pkgs/id/crates/id-worlds/src/world/mod.rs b/pkgs/id/crates/id-worlds/src/world/mod.rs index 37336a088b..1ef7f68119 100644 --- a/pkgs/id/crates/id-worlds/src/world/mod.rs +++ b/pkgs/id/crates/id-worlds/src/world/mod.rs @@ -82,8 +82,8 @@ pub use id_caps::portal::{PortalGrantState, PortalGrants, PortalRefusal}; pub(crate) use journal::JournalAdminHistory; pub use journal::{ CoordinatorJournalEntry, JOURNAL_VERSION, JOURNAL_VERSION_CHECKPOINT_PARTS, - JOURNAL_VERSION_EXECUTION_PROFILE, JOURNAL_VERSION_OPEN_PRESENCE, JOURNAL_VERSION_PORTALS, - JOURNAL_VERSION_PROGRAM_BUNDLE, JOURNAL_VERSION_REGION_HANDOFF, + JOURNAL_VERSION_EXECUTION_PROFILE, JOURNAL_VERSION_JOIN_TIMES, JOURNAL_VERSION_OPEN_PRESENCE, + JOURNAL_VERSION_PORTALS, JOURNAL_VERSION_PROGRAM_BUNDLE, JOURNAL_VERSION_REGION_HANDOFF, JOURNAL_VERSION_SCHEDULE_RECURRENCE, JOURNAL_VERSION_SIGNED_PLAY, JOURNAL_VERSION_WORLD_BUDGET, JOURNAL_VERSION_WRITE_LEASE, JournalEntry, JournalSignedMove, JournalSnapshot, JournalSnapshotSource, MAX_JOURNAL_VERSION, TrustBasis, TrustDecision, WorldJournal, @@ -1804,4 +1804,10 @@ pub(crate) struct CapabilityRecord { bounds: CapabilityBounds, used: u32, subject: Option, + /// When the world issued this capability (#11960); `None` when the + /// journal that restored it predates recorded times. + issued_at: Option, + /// When this capability was revoked, directly or with an ancestor + /// (#11960); `None` while active or when no time was recorded. + revoked_at: Option, } diff --git a/pkgs/id/crates/id-worlds/src/world/replica.rs b/pkgs/id/crates/id-worlds/src/world/replica.rs index d6ff3af8ab..4c5ab676f9 100644 --- a/pkgs/id/crates/id-worlds/src/world/replica.rs +++ b/pkgs/id/crates/id-worlds/src/world/replica.rs @@ -350,7 +350,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: world_id.to_owned(), - version: super::super::JOURNAL_VERSION, + version: super::super::MAX_JOURNAL_VERSION, }, source.issued_entry(owner.id).unwrap(), source.issued_entry(spectator.id).unwrap(), @@ -470,7 +470,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: world_id.to_owned(), - version: super::super::JOURNAL_VERSION, + version: super::super::MAX_JOURNAL_VERSION, }, source.issued_entry(owner.id).unwrap(), source.issued_entry(present.id).unwrap(), @@ -834,7 +834,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: world_id.to_owned(), - version: super::super::JOURNAL_VERSION, + version: super::super::MAX_JOURNAL_VERSION, }, core.issued_entry(ann.id).unwrap(), ]; @@ -1075,7 +1075,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: world_id.to_owned(), - version: super::super::JOURNAL_VERSION, + version: super::super::MAX_JOURNAL_VERSION, }, source.issued_entry(1).unwrap(), source.issued_entry(spectator.id).unwrap(), @@ -1133,7 +1133,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: world_id.to_owned(), - version: super::super::JOURNAL_VERSION, + version: super::super::MAX_JOURNAL_VERSION, }, core.issued_entry(1).unwrap(), ]; diff --git a/pkgs/id/crates/id-worlds/src/world/tests.rs b/pkgs/id/crates/id-worlds/src/world/tests.rs index 48b995a66d..1d4a69f1e1 100644 --- a/pkgs/id/crates/id-worlds/src/world/tests.rs +++ b/pkgs/id/crates/id-worlds/src/world/tests.rs @@ -104,9 +104,21 @@ mod tests { } } + /// Append like `FileJournal`: raise the `Created` header to the version + /// the entry requires, so restore sees the journal a writer would leave. + pub(super) fn push_bumped(entries: &mut Vec, entry: &JournalEntry) { + let required = journal_version_required_by(entry); + if let Some(JournalEntry::Created { version, .. }) = entries.first_mut() { + if *version < required { + *version = required; + } + } + entries.push(entry.clone()); + } + impl WorldJournal for TransferJournal { fn append(&mut self, entry: &JournalEntry) -> Result<()> { - self.0.lock().unwrap().push(entry.clone()); + push_bumped(&mut self.0.lock().unwrap(), entry); Ok(()) } } @@ -1679,7 +1691,8 @@ mod tests { // wiring, code#1068), version 9 for signed play (#4346) and // version 8 for recurring schedules (#1355); the lease entries // still require exactly 7. - assert_eq!(MAX_JOURNAL_VERSION, JOURNAL_VERSION_PROGRAM_BUNDLE); + // Version 12 (#11960) records join and revoke times. + assert_eq!(MAX_JOURNAL_VERSION, JOURNAL_VERSION_JOIN_TIMES); // A checkpoint carrying a non-bootstrap lease also requires // version 7; one without a lease keeps its existing version. let mut lease = WriteLease::bootstrap(); @@ -2493,10 +2506,7 @@ mod tests { let entries = core.checkpoint_entries(snapshot.clone()).unwrap(); assert!(matches!( entries.first(), - Some(JournalEntry::Created { - version: JOURNAL_VERSION_CHECKPOINT_PARTS, - .. - }) + Some(JournalEntry::Created { version, .. }) if *version >= JOURNAL_VERSION_CHECKPOINT_PARTS )); assert!(entries.iter().all(|entry| { serde_json::to_vec(entry).is_ok_and(|bytes| bytes.len() <= MAX_CHECKPOINT_PART_BYTES) @@ -3516,6 +3526,7 @@ mod tests { granted: vec!["time.now".to_owned()], }, JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: "ann".to_owned(), @@ -3553,6 +3564,7 @@ mod tests { let secret = "ef".repeat(32); let digest = hex_encode(&token_digest(&format!("{participant_id}.{secret}"))); JournalEntry::Issued { + issued_at: None, participant: Participant { id: participant_id, display_name: format!("ann-{participant_id}"), @@ -3660,6 +3672,7 @@ mod tests { let error = restore_author_entries(vec![ issued_entry(WorldScopes::GUEST.bits()), JournalEntry::Revoked { + revoked_at: None, participant_id: 1, by: None, }, @@ -3729,6 +3742,7 @@ mod tests { issued_entry(WorldScopes::GUEST.bits()), committed_event(1, 1, WorldEventKind::Chat("before revoke".to_owned())), JournalEntry::Revoked { + revoked_at: None, participant_id: 1, by: None, }, @@ -3772,6 +3786,7 @@ mod tests { let secret = "ef".repeat(32); let issued_with = |id: u64, scopes: u8, parent: Option, subject: &str| JournalEntry::Issued { + issued_at: None, participant: Participant { id, display_name: format!("delegate-{id}"), @@ -3961,6 +3976,7 @@ mod tests { version: JOURNAL_VERSION, }, JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: "ann".to_owned(), @@ -4107,10 +4123,7 @@ mod tests { let checkpoint = core.checkpoint_entries("snapshot".to_owned()).unwrap(); assert!(matches!( checkpoint.first(), - Some(JournalEntry::Created { - version: JOURNAL_VERSION_OPEN_PRESENCE, - .. - }) + Some(JournalEntry::Created { version, .. }) if *version >= JOURNAL_VERSION_OPEN_PRESENCE )); assert!(matches!( checkpoint.last(), @@ -4189,6 +4202,12 @@ mod tests { if let Some(JournalEntry::Checkpoint { open_presence, .. }) = legacy.last_mut() { *open_presence = None; } + // A journal from before join times never carried them. + for entry in &mut legacy { + if let JournalEntry::Issued { issued_at, .. } = entry { + *issued_at = None; + } + } let reopened = WorldCore::restore("presence-checkpoint", WorldLimits::default(), legacy).unwrap(); assert_eq!(reopened.replay.len(), 1); @@ -4243,6 +4262,12 @@ mod tests { if let Some(JournalEntry::Created { version, .. }) = legacy.first_mut() { *version = JOURNAL_VERSION_REGION_HANDOFF; } + // A journal from before join times never carried them. + for entry in &mut legacy { + if let JournalEntry::Issued { issued_at, .. } = entry { + *issued_at = None; + } + } if let Some(JournalEntry::Checkpoint { admin_history, open_presence, @@ -5998,6 +6023,7 @@ mod tests { version: JOURNAL_VERSION, }, JournalEntry::Issued { + issued_at: None, participant: owner.clone(), digest: hex_encode(&token_digest(token.expose())), scopes: WorldScopes::GUEST.bits(), @@ -6048,6 +6074,7 @@ mod tests { version: JOURNAL_VERSION, }, JournalEntry::Issued { + issued_at: None, participant: owner, digest, scopes: WorldScopes::GUEST.bits(), @@ -6092,6 +6119,7 @@ mod tests { version: JOURNAL_VERSION, }, JournalEntry::Issued { + issued_at: None, participant: owner, digest: hex_encode(&token_digest(owner_token.expose())), scopes: WorldScopes::GUEST.union(WorldScopes::DELEGATE).bits(), @@ -6102,6 +6130,7 @@ mod tests { subject: None, }, JournalEntry::Issued { + issued_at: None, participant: child, digest, scopes: WorldScopes::JOIN.bits(), @@ -6135,10 +6164,11 @@ mod tests { .unwrap(); let created = JournalEntry::Created { world_id: "test-world".to_owned(), - version: JOURNAL_VERSION, + version: MAX_JOURNAL_VERSION, }; let owner_issued = source.issued_entry(owner.id).unwrap(); let parent_revoked = JournalEntry::Revoked { + revoked_at: None, participant_id: owner.id, by: None, }; @@ -6175,6 +6205,7 @@ mod tests { parent_revoked, child_issued, JournalEntry::Revoked { + revoked_at: None, participant_id: child.id, by: None, }, @@ -6200,6 +6231,7 @@ mod tests { .issue_capability("Ada", WorldScopes::GUEST, CapabilityBounds::default()) .unwrap(); let legitimate = JournalEntry::Issued { + issued_at: None, participant: owner, digest: hex_encode(&token_digest(owner_token.expose())), scopes: WorldScopes::GUEST.bits(), @@ -6210,6 +6242,7 @@ mod tests { subject: None, }; let forged = JournalEntry::Issued { + issued_at: None, participant: Participant { id: 0, display_name: "host".to_owned(), @@ -6255,6 +6288,7 @@ mod tests { .issue_capability("Ada", WorldScopes::GUEST, CapabilityBounds::default()) .unwrap(); let legitimate = JournalEntry::Issued { + issued_at: None, participant: owner.clone(), digest: hex_encode(&token_digest(owner_token.expose())), scopes: WorldScopes::GUEST.bits(), @@ -6280,6 +6314,7 @@ mod tests { // restores fine: those journals predate the current sanitizer // and restored successfully before this check existed. let legacy = JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}".to_owned(), @@ -6303,6 +6338,7 @@ mod tests { // also restores fine: that journal was written by a live mint // path, and refusing it bricks the world. let boundary = JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: format!("{} ", "a".repeat(47)), @@ -6332,6 +6368,7 @@ mod tests { .unwrap(); assert_eq!(minted.display_name, "a".repeat(47)); let minted_entry = JournalEntry::Issued { + issued_at: None, participant: minted.clone(), digest: hex_encode(&token_digest(minted_token.expose())), scopes: WorldScopes::GUEST.bits(), @@ -6355,6 +6392,7 @@ mod tests { "\u{1}\u{2}".to_owned(), ] { let forged = JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: bad_name.clone(), @@ -6387,7 +6425,7 @@ mod tests { let mut source = world(8); let mut entries = vec![JournalEntry::Created { world_id: "test-world".to_owned(), - version: JOURNAL_VERSION, + version: MAX_JOURNAL_VERSION, }]; for name in ["Ada", "Bo", "Cy"] { let (participant, _) = join(&mut source, name); @@ -6417,7 +6455,7 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: "test-world".to_owned(), - version: JOURNAL_VERSION, + version: MAX_JOURNAL_VERSION, }, source.issued_entry(first.id).unwrap(), source.issued_entry(second.id).unwrap(), @@ -6445,11 +6483,12 @@ mod tests { let entries = vec![ JournalEntry::Created { world_id: "test-world".to_owned(), - version: JOURNAL_VERSION, + version: MAX_JOURNAL_VERSION, }, source.issued_entry(first.id).unwrap(), source.issued_entry(second.id).unwrap(), JournalEntry::Revoked { + revoked_at: None, participant_id: first.id, by: None, }, @@ -6537,13 +6576,178 @@ mod tests { /// #11778: a checkpoint keeps who revoked a capability. It wrote /// `by: None` for every revocation, so after compaction a moderator's /// revoke replayed as the admin's and the audit actor was lost. + /// #11960: mint stamps the world clock's time on each capability, + /// a revoke stamps every capability it revokes, and a checkpoint and a + /// restore keep both, delegated descendants included. + #[test] + fn join_and_revoke_times_survive_checkpoint_and_restore_11960() { + fn times(entries: &[JournalEntry]) -> Vec<(u64, Option, Option)> { + let mut issued: Vec<(u64, Option, Option)> = entries + .iter() + .filter_map(|entry| match entry { + JournalEntry::Issued { + participant, + issued_at, + .. + } => Some((participant.id, *issued_at, None)), + _ => None, + }) + .collect(); + for entry in entries { + if let JournalEntry::Revoked { + participant_id, + revoked_at, + .. + } = entry + && let Some(row) = issued.iter_mut().find(|row| row.0 == *participant_id) + { + row.2 = *revoked_at; + } + } + issued + } + let clock = Arc::new(SplitClock { + wall_ms: AtomicU64::new(1_000), + monotonic_ms: AtomicU64::new(0), + monotonic_origin: Instant::now(), + }); + let mut core = + WorldCore::new_with_clock("test-world", WorldLimits::default(), clock.clone()).unwrap(); + core.active_module_hash = Some("module".to_owned()); + let root = WorldScopes::GUEST.union(WorldScopes::DELEGATE); + let (owner, owner_token) = core + .issue_capability("Owner", root, CapabilityBounds::default()) + .unwrap(); + clock.wall_ms.store(2_000, Ordering::SeqCst); + let (child, _) = core + .delegate( + &owner_token, + WorldScopes::GUEST, + CapabilityBounds::default(), + "Child", + None, + ) + .unwrap(); + clock.wall_ms.store(3_000, Ordering::SeqCst); + let (kept, _) = join(&mut core, "Kept"); + let mut revoked = core.revoke_tree_at(owner.id, None, Some(4_000)); + revoked.sort_unstable(); + assert_eq!(revoked, vec![owner.id, child.id]); + // Revoking a revoked capability again changes no recorded time. + assert!(core.revoke_tree_at(owner.id, None, Some(9_999)).is_empty()); + + let expected = vec![ + (owner.id, Some(1_000), Some(4_000)), + (child.id, Some(2_000), Some(4_000)), + (kept.id, Some(3_000), None), + ]; + let compacted = core.checkpoint_entries("{}".to_owned()).unwrap(); + assert_eq!(times(&compacted), expected); + // The header names the version its timed entries require. + assert!(matches!( + compacted.first(), + Some(JournalEntry::Created { version, .. }) if *version >= JOURNAL_VERSION_JOIN_TIMES + )); + let restored = WorldCore::restore("test-world", WorldLimits::default(), compacted) + .unwrap() + .core; + assert_eq!( + times(&restored.checkpoint_entries("{}".to_owned()).unwrap()), + expected + ); + } + + /// #11960: an entry carrying a time requires version 12, one without + /// stays readable by older binaries, and a header older than the entry + /// is refused at restore. + #[test] + fn timed_entries_require_the_join_times_version_11960() { + let untimed = issued_entry_for(1, WorldScopes::GUEST.bits()); + assert_eq!(journal_version_required_by(&untimed), JOURNAL_VERSION); + let JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + .. + } = untimed + else { + unreachable!() + }; + let timed = JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + issued_at: Some(5), + }; + assert_eq!( + journal_version_required_by(&timed), + JOURNAL_VERSION_JOIN_TIMES + ); + let revoke = |revoked_at| JournalEntry::Revoked { + participant_id: 1, + by: None, + revoked_at, + }; + assert_eq!(journal_version_required_by(&revoke(None)), JOURNAL_VERSION); + assert_eq!( + journal_version_required_by(&revoke(Some(6))), + JOURNAL_VERSION_JOIN_TIMES + ); + let header = |version| JournalEntry::Created { + world_id: "test-world".to_owned(), + version, + }; + let old = WorldCore::restore( + "test-world", + WorldLimits::default(), + vec![header(JOURNAL_VERSION_PROGRAM_BUNDLE), timed.clone()], + ); + let error = old.err().expect("an older header refuses a timed entry"); + assert!( + error.to_string().contains("requires version 12"), + "{error:#}" + ); + let new = WorldCore::restore( + "test-world", + WorldLimits::default(), + vec![header(JOURNAL_VERSION_JOIN_TIMES), timed], + ) + .unwrap() + .core; + assert_eq!(new.capabilities.get(&1).unwrap().issued_at, Some(5)); + // An untimed entry still reads, and its time stays unknown, not zero. + let legacy = WorldCore::restore( + "test-world", + WorldLimits::default(), + vec![ + header(JOURNAL_VERSION), + issued_entry_for(1, WorldScopes::GUEST.bits()), + ], + ) + .unwrap() + .core; + assert_eq!(legacy.capabilities.get(&1).unwrap().issued_at, None); + } + #[test] fn checkpoint_keeps_the_revoking_moderator_11778() { fn revocations(entries: &[JournalEntry]) -> Vec<(u64, Option)> { entries .iter() .filter_map(|entry| match entry { - JournalEntry::Revoked { participant_id, by } => Some((*participant_id, *by)), + JournalEntry::Revoked { + participant_id, by, .. + } => Some((*participant_id, *by)), _ => None, }) .collect() @@ -6844,6 +7048,7 @@ mod tests { version: JOURNAL_VERSION, }, JournalEntry::Issued { + issued_at: None, participant: Participant { id: 1, display_name: "ann".to_owned(), @@ -6857,6 +7062,7 @@ mod tests { subject: None, }, JournalEntry::Issued { + issued_at: None, participant: Participant { id: 2, display_name: "bo".to_owned(), @@ -8090,7 +8296,7 @@ mod tests { fn signed_journal(core: &WorldCore) -> Vec { let mut entries = vec![JournalEntry::Created { world_id: "ttt-journal".to_owned(), - version: JOURNAL_VERSION_SIGNED_PLAY, + version: MAX_JOURNAL_VERSION, }]; let mut ids: Vec = core.author_keys.keys().copied().collect(); ids.sort_unstable(); @@ -9366,7 +9572,7 @@ mod schedule_tests { impl WorldJournal for Recorded { fn append(&mut self, entry: &JournalEntry) -> Result<()> { - self.0.lock().unwrap().push(entry.clone()); + super::tests::push_bumped(&mut self.0.lock().unwrap(), entry); Ok(()) } } @@ -9398,7 +9604,7 @@ mod schedule_tests { // the participant before it was issued -- a state no live path can // write, and restore refuses it (#2894). let issued = core.issued_entry(ann.id).unwrap(); - journal.0.lock().unwrap().push(issued); + super::tests::push_bumped(&mut journal.0.lock().unwrap(), &issued); let program = Scheduler { refuse_fires, ..Scheduler::default() diff --git a/pkgs/id/crates/id-worlds/src/world_owner_report.rs b/pkgs/id/crates/id-worlds/src/world_owner_report.rs index 36eb772eaa..c0b22f0734 100644 --- a/pkgs/id/crates/id-worlds/src/world_owner_report.rs +++ b/pkgs/id/crates/id-worlds/src/world_owner_report.rs @@ -16,10 +16,14 @@ //! issued or delegated. Compaction rewrites every capability ever //! issued (revoked ones with their `Revoked` entry), so the count is //! complete across checkpoints. -//! - No journal entry records *when* a capability was issued, so the -//! join time, the earliest answerable time and any count inside a time -//! window are [`UnavailableReason::JoinTimeNotRecorded`], never zero. -//! Recording them is a journal-format change, out of this scope. +//! - Since journal version 12 ([`crate::world::JOURNAL_VERSION_JOIN_TIMES`], +//! #11960) an `Issued` entry carries `issued_at` and a `Revoked` entry +//! carries `revoked_at`. Joins and revokes from before that carry no +//! time and are reported as such, never as zero. `available_since` is +//! the earliest time from which every later join carries one; a window +//! that starts before it is counted but marked incomplete. A journal +//! with no timed join at all reports join times as +//! [`UnavailableReason::JoinTimeNotRecorded`]. //! - Live membership and program activity come only from a running //! world: [`OwnerReport::with_live`] fills them from a [`LiveSnapshot`] //! (#11775), and without one they are unavailable. Program activity is @@ -52,6 +56,11 @@ pub const OWNER_REPORT_SCHEMA_VERSION: u32 = 1; pub enum UnavailableReason { /// The journal records no time for a capability's issue. JoinTimeNotRecorded, + /// Capabilities were revoked, and the journal records no time for any + /// of those revokes. + RevokeTimeNotRecorded, + /// No one has joined, so there is no join time to start from. + NoJoins, /// No live participant snapshot was supplied (the world is not /// running, or the caller did not ask it). LiveStateNotProvided, @@ -67,6 +76,8 @@ impl UnavailableReason { pub const fn describe(self) -> &'static str { match self { Self::JoinTimeNotRecorded => "the journal does not record when anyone joined", + Self::RevokeTimeNotRecorded => "the journal does not record when anyone was revoked", + Self::NoJoins => "no one has joined yet", Self::LiveStateNotProvided => "no live snapshot (the world is not running here)", Self::ProgramRunsNotRecorded => "program runs are not recorded by this journal", Self::StorageNotMeasured => "storage was not measured", @@ -195,6 +206,12 @@ pub struct JoinSample { pub delegated: bool, /// Whether the capability has since been revoked. pub revoked: bool, + /// When the capability was issued (Unix ms); `null` when the journal + /// entry predates recorded times. + pub issued_at_unix_ms: Option, + /// When it was revoked (Unix ms); `null` while active or when no time + /// was recorded. + pub revoked_at_unix_ms: Option, } /// Durable join facts. @@ -216,8 +233,19 @@ pub struct JoinsSection { pub inherited_from_fork: Option, /// Joins inside the requested window. pub in_window: Measured, - /// Earliest time from which joins are answerable. + /// Whether `in_window` covers the whole window: false when the window + /// starts before `available_since`, where untimed joins may belong. + pub in_window_complete: bool, + /// Earliest time from which joins are answerable: every join after it + /// in the journal carries a time. pub available_since: Measured, + /// Joins whose entry carries no time (issued before times were + /// recorded). + pub untimed: u64, + /// Revokes inside the requested window, counting those with a time. + pub revoked_in_window: Measured, + /// Revoked participants whose revoke carries no time. + pub revoked_untimed: u64, /// Whether every join in the journal was counted. pub complete: bool, /// The most recent joins, newest first, at most @@ -434,6 +462,8 @@ pub fn check_schema_version(report: &serde_json::Value) -> Result, + revoked_at: Option, } /// Read one line of at most `max` bytes. `Ok(None)` at end of input; @@ -491,6 +521,8 @@ pub fn project( let mut subjects: BTreeSet = BTreeSet::new(); let mut recent: VecDeque = VecDeque::with_capacity(limits.sample_limit); let mut inherited_from_fork = None; + // Earliest join time after the last join without one (#11960). + let mut timed_since: Option = None; let mut over_limit = false; let mut buf = Vec::new(); let mut line = 0_u64; @@ -536,6 +568,7 @@ pub fn project( participant, parent, subject, + issued_at, .. } => { if participants.contains_key(&participant.id) { @@ -550,8 +583,11 @@ pub fn project( ParticipantFlags { delegated: parent.is_some(), revoked: false, + issued_at, + revoked_at: None, }, ); + timed_since = issued_at.map(|at| timed_since.map_or(at, |since| since.min(at))); if let Some(subject) = subject { subjects.insert(subject); } @@ -562,9 +598,14 @@ pub fn project( recent.push_back(participant.id); } } - JournalEntry::Revoked { participant_id, .. } => { + JournalEntry::Revoked { + participant_id, + revoked_at, + .. + } => { if let Some(flags) = participants.get_mut(&participant_id) { flags.revoked = true; + flags.revoked_at = revoked_at; } } JournalEntry::Forked { from_world_id, .. } => { @@ -588,9 +629,54 @@ pub fn project( participant_id: *id, delegated: flags.delegated, revoked: flags.revoked, + issued_at_unix_ms: flags.issued_at, + revoked_at_unix_ms: flags.revoked_at, } }) .collect(); + let in_range = |at: Option| { + at.is_some_and(|at| (window.from_unix_ms..window.to_unix_ms).contains(&at)) + }; + let untimed = participants + .values() + .filter(|f| f.issued_at.is_none()) + .count() as u64; + let (in_window, in_window_complete, available_since) = match timed_since { + _ if count == 0 => ( + Measured::Known(0), + true, + Measured::Unavailable(UnavailableReason::NoJoins), + ), + Some(since) => ( + Measured::Known( + participants + .values() + .filter(|f| in_range(f.issued_at)) + .count() as u64, + ), + window.from_unix_ms >= since, + Measured::Known(since), + ), + None => ( + Measured::Unavailable(UnavailableReason::JoinTimeNotRecorded), + false, + Measured::Unavailable(UnavailableReason::JoinTimeNotRecorded), + ), + }; + let revoked_timed = participants + .values() + .filter(|f| f.revoked && f.revoked_at.is_some()) + .count() as u64; + let revoked_in_window = if revoked > 0 && revoked_timed == 0 { + Measured::Unavailable(UnavailableReason::RevokeTimeNotRecorded) + } else { + Measured::Known( + participants + .values() + .filter(|f| f.revoked && in_range(f.revoked_at)) + .count() as u64, + ) + }; let joins = JoinsSection { count, host_issued: count - delegated, @@ -598,8 +684,12 @@ pub fn project( revoked, distinct_subjects: subjects.len() as u64, inherited_from_fork, - in_window: Measured::Unavailable(UnavailableReason::JoinTimeNotRecorded), - available_since: Measured::Unavailable(UnavailableReason::JoinTimeNotRecorded), + in_window, + in_window_complete, + available_since, + untimed, + revoked_in_window, + revoked_untimed: revoked - revoked_timed, complete: !over_limit && !coverage.torn_tail, recent, }; diff --git a/pkgs/id/crates/id-worlds/src/world_owner_report_tests.rs b/pkgs/id/crates/id-worlds/src/world_owner_report_tests.rs index e4d4825b51..c8f9bbd67a 100644 --- a/pkgs/id/crates/id-worlds/src/world_owner_report_tests.rs +++ b/pkgs/id/crates/id-worlds/src/world_owner_report_tests.rs @@ -47,6 +47,7 @@ fn issued(id: u64, parent: Option, subject: Option<&str>) -> JournalEntry { uses: None, used: 0, subject: subject.map(str::to_owned), + issued_at: None, } } @@ -54,6 +55,44 @@ fn revoked(id: u64) -> JournalEntry { JournalEntry::Revoked { participant_id: id, by: None, + revoked_at: None, + } +} + +/// An `Issued` entry stamped at `at` (journal version 12, #11960). +fn issued_at(id: u64, at: u64) -> JournalEntry { + let JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + .. + } = issued(id, None, None) + else { + unreachable!() + }; + JournalEntry::Issued { + participant, + digest, + scopes, + parent, + expires_at, + uses, + used, + subject, + issued_at: Some(at), + } +} + +fn revoked_at(id: u64, at: u64) -> JournalEntry { + JournalEntry::Revoked { + participant_id: id, + by: None, + revoked_at: Some(at), } } @@ -458,3 +497,147 @@ proptest! { prop_assert_eq!(serde_json::to_string(&report).unwrap(), serde_json::to_string(&again).unwrap()); } } + +// #11960: join and revoke times. The window is [1_799_000_000_000, +// 1_800_000_000_000); `BEFORE` and `INSIDE` fall either side of its start. +const BEFORE: u64 = 1_798_000_000_000; +const INSIDE: u64 = 1_799_500_000_000; + +#[test] +fn timed_joins_give_exact_window_counts_and_since() { + let v = crate::world::JOURNAL_VERSION_JOIN_TIMES; + let report = run(&jsonl(&[ + header(v), + issued_at(1, BEFORE), + issued_at(2, INSIDE), + issued_at(3, INSIDE + 1), + revoked_at(2, INSIDE + 2), + revoked_at(1, BEFORE + 1), + ])) + .unwrap(); + let joins = &report.joins; + assert_eq!(joins.count, 3); + assert_eq!(joins.untimed, 0); + assert_eq!(joins.in_window, Measured::Known(2)); + assert_eq!(joins.available_since, Measured::Known(BEFORE)); + assert!(joins.in_window_complete); + assert_eq!(joins.revoked_in_window, Measured::Known(1)); + assert_eq!(joins.revoked_untimed, 0); + assert_eq!(joins.recent[0].issued_at_unix_ms, Some(INSIDE + 1)); + assert_eq!(joins.recent[1].revoked_at_unix_ms, Some(INSIDE + 2)); + // The window's end is exclusive and its start inclusive. + let mut edge = request(); + edge.window = ReportWindow { + from_unix_ms: INSIDE, + to_unix_ms: INSIDE + 1, + }; + let bytes = jsonl(&[header(v), issued_at(1, INSIDE), issued_at(2, INSIDE + 1)]); + let report = project(Cursor::new(bytes), &edge, None).unwrap(); + assert_eq!(report.joins.in_window, Measured::Known(1)); +} + +#[test] +fn joins_from_before_the_upgrade_stay_untimed_and_mark_the_window_incomplete() { + let v = crate::world::JOURNAL_VERSION_JOIN_TIMES; + let report = run(&jsonl(&[ + header(v), + issued(1, None, None), + issued(2, None, None), + revoked(2), + issued_at(3, INSIDE), + issued_at(4, INSIDE + 5), + ])) + .unwrap(); + let joins = &report.joins; + assert_eq!(joins.untimed, 2); + // Every join after the last untimed one carries a time. + assert_eq!(joins.available_since, Measured::Known(INSIDE)); + assert_eq!(joins.in_window, Measured::Known(2)); + // The window starts before `available_since`, where untimed joins sit. + assert!(!joins.in_window_complete); + assert_eq!( + joins.revoked_in_window.reason(), + Some(UnavailableReason::RevokeTimeNotRecorded) + ); + assert_eq!(joins.revoked_untimed, 1); + let json = serde_json::to_value(&report).unwrap(); + let recent = &json["joins"]["recent"]; + assert_eq!(recent[3]["issued_at_unix_ms"], serde_json::Value::Null); + assert_eq!(recent[0]["issued_at_unix_ms"], INSIDE + 5); + assert_eq!(check_schema_version(&json), Ok(OWNER_REPORT_SCHEMA_VERSION)); +} + +#[test] +fn an_untimed_join_after_timed_ones_moves_available_since() { + let v = crate::world::JOURNAL_VERSION_JOIN_TIMES; + let report = run(&jsonl(&[ + header(v), + issued_at(1, BEFORE), + issued(2, None, None), + issued_at(3, INSIDE), + ])) + .unwrap(); + assert_eq!(report.joins.available_since, Measured::Known(INSIDE)); + assert_eq!(report.joins.in_window, Measured::Known(1)); + assert!(!report.joins.in_window_complete); +} + +#[test] +fn no_joins_is_a_measured_zero_with_no_since() { + let report = run(&jsonl(&[header(1)])).unwrap(); + assert_eq!(report.joins.in_window, Measured::Known(0)); + assert_eq!( + report.joins.available_since.reason(), + Some(UnavailableReason::NoJoins) + ); + assert_eq!(report.joins.revoked_in_window, Measured::Known(0)); +} + +#[test] +fn a_timed_entry_under_an_older_header_is_refused() { + let error = run(&jsonl(&[ + header(crate::world::JOURNAL_VERSION_PROGRAM_BUNDLE), + issued_at(1, INSIDE), + ])) + .unwrap_err(); + assert!( + matches!(error, OwnerReportError::EntryNewerThanHeader { line: 2, required } if required == crate::world::JOURNAL_VERSION_JOIN_TIMES), + "{error}" + ); +} + +proptest! { + /// Window counts agree with a direct count over the generated times, + /// and untimed joins are never counted as zero-time joins. + #[test] + fn window_counts_match_a_direct_count( + times in proptest::collection::vec(proptest::option::of(0_u64..10_000), 0..40), + from in 0_u64..10_000, + span in 0_u64..10_000, + ) { + let v = crate::world::JOURNAL_VERSION_JOIN_TIMES; + let mut entries = vec![header(v)]; + for (i, at) in times.iter().enumerate() { + let id = i as u64 + 1; + entries.push(at.map_or_else(|| issued(id, None, None), |at| issued_at(id, at))); + } + let mut req = request(); + req.window = ReportWindow { from_unix_ms: from, to_unix_ms: from + span }; + let report = project(Cursor::new(jsonl(&entries)), &req, None).unwrap(); + let expected = times.iter().filter(|t| t.is_some_and(|t| t >= from && t < from + span)).count() as u64; + let untimed = times.iter().filter(|t| t.is_none()).count() as u64; + prop_assert_eq!(report.joins.untimed, untimed); + let last_untimed = times.iter().rposition(Option::is_none); + let after: Vec = times[last_untimed.map_or(0, |i| i + 1)..].iter().flatten().copied().collect(); + if times.is_empty() { + prop_assert_eq!(report.joins.in_window.clone(), Measured::Known(0)); + } else if after.is_empty() { + prop_assert_eq!(report.joins.in_window.reason(), Some(UnavailableReason::JoinTimeNotRecorded)); + } else { + let since = *after.iter().min().unwrap(); + prop_assert_eq!(report.joins.available_since.clone(), Measured::Known(since)); + prop_assert_eq!(report.joins.in_window.clone(), Measured::Known(expected)); + prop_assert_eq!(report.joins.in_window_complete, from >= since); + } + } +} diff --git a/pkgs/id/crates/id-worlds/src/world_session/tests.rs b/pkgs/id/crates/id-worlds/src/world_session/tests.rs index 9fbaa76239..8803b4d0c8 100644 --- a/pkgs/id/crates/id-worlds/src/world_session/tests.rs +++ b/pkgs/id/crates/id-worlds/src/world_session/tests.rs @@ -3774,7 +3774,7 @@ mod tests { assert!( entries.iter().any(|entry| matches!( entry, - crate::world::JournalEntry::Revoked { participant_id, by: Some(by) } + crate::world::JournalEntry::Revoked { participant_id, by: Some(by), .. } if *participant_id == victim.id && *by == moderator.id )), "revoke must be journal-attributed to the moderator: {entries:?}" diff --git a/pkgs/id/crates/id-worlds/src/world_store.rs b/pkgs/id/crates/id-worlds/src/world_store.rs index daaec89d6f..7904ed2aba 100644 --- a/pkgs/id/crates/id-worlds/src/world_store.rs +++ b/pkgs/id/crates/id-worlds/src/world_store.rs @@ -1883,8 +1883,9 @@ mod tests { async fn offline_replica_serves_friend_delegation_from_directory_facts_without_writer() { use crate::directory::Actor; use crate::world::{ - DurableReceiver, JOURNAL_VERSION, JournalEntry, ReplicationEnvelope, - SignedReplicationEnvelope, WorldCore, frame_digest, journal_entry_line, + DurableReceiver, JournalEntry, MAX_JOURNAL_VERSION as JOURNAL_VERSION, + ReplicationEnvelope, SignedReplicationEnvelope, WorldCore, frame_digest, + journal_entry_line, }; use ed25519_dalek::SigningKey; @@ -2048,9 +2049,9 @@ mod tests { use crate::directory::Actor; use crate::world::{ - DurableReceiver, JOURNAL_VERSION, JournalEntry, ReplicationEnvelope, - SignedReplicationEnvelope, WorldCore, WorldEvent, WorldEventKind, frame_digest, - journal_entry_line, + DurableReceiver, JournalEntry, MAX_JOURNAL_VERSION as JOURNAL_VERSION, + ReplicationEnvelope, SignedReplicationEnvelope, WorldCore, WorldEvent, WorldEventKind, + frame_digest, journal_entry_line, }; let mut writer_core = WorldCore::new(world_id, WorldLimits::default()).unwrap(); @@ -2739,6 +2740,7 @@ mod tests { .unwrap(); journal .append(&JournalEntry::Issued { + issued_at: None, participant, digest: "00".repeat(32), scopes: WorldScopes::GUEST.bits(), @@ -2872,6 +2874,7 @@ mod tests { drop(FileJournal::open(&path, "w").unwrap()); let created = std::fs::read(&path).unwrap(); let revoked = serde_json::to_vec(&JournalEntry::Revoked { + revoked_at: None, participant_id: 1, by: None, }) diff --git a/pkgs/id/tests/probe_restoreid0.rs b/pkgs/id/tests/probe_restoreid0.rs index 21bf5e67f0..7c89554a28 100644 --- a/pkgs/id/tests/probe_restoreid0.rs +++ b/pkgs/id/tests/probe_restoreid0.rs @@ -42,6 +42,7 @@ fn issued(id: u64, name: &str) -> JournalEntry { uses: None, used: 0, subject: None, + issued_at: None, } } diff --git a/pkgs/id/tests/world_roles.rs b/pkgs/id/tests/world_roles.rs index 9c05673448..32b770f6d6 100644 --- a/pkgs/id/tests/world_roles.rs +++ b/pkgs/id/tests/world_roles.rs @@ -159,7 +159,7 @@ async fn a_moderator_revoke_is_audited() { assert!( entries.iter().any(|entry| matches!( entry, - JournalEntry::Revoked { participant_id, by: Some(by) } + JournalEntry::Revoked { participant_id, by: Some(by), .. } if *participant_id == 1 && *by == 2 )), "expected a Revoked audit record naming the moderator: {entries:?}" @@ -222,7 +222,7 @@ async fn a_moderator_capability_revokes_and_a_guest_capability_is_denied() { assert!( entries.iter().any(|entry| matches!( entry, - JournalEntry::Revoked { participant_id, by: Some(by) } + JournalEntry::Revoked { participant_id, by: Some(by), .. } if *participant_id == victim.id && *by == moderator.id )), "expected a Revoked audit record naming the derived moderator: {entries:?}"