rings-node 1.0.0-pre.6

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Versioned, bounded operator observability for a running node.
//!
//! [`Observability`](crate::observability::Observability) is the synchronous adapter installed at
//! the core swarm boundary. It keeps
//! process-lifetime counters, a fixed-size recent-event ring, and bounded DHT lookup correlation
//! state. The native HTTP layer combines that state with live session, mailbox, peer-rating, and
//! overlay snapshots in the authenticated `/status?view=observability` JSON response. No payload,
//! key material, message identifier, mailbox identifier, or lookup correlation key crosses the
//! export boundary.

use std::collections::BTreeMap;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;

use rings_core::message::MessageCategory;
use rings_core::swarm::observer::LookupCorrelation;
use rings_core::swarm::observer::LookupKind;
use rings_core::swarm::observer::LookupOutcome;
use rings_core::swarm::observer::MessageActivity;
use rings_core::swarm::observer::MessageObservation;
use rings_core::swarm::observer::ObservationOutcome;
use rings_core::swarm::observer::SwarmObserver;
use rings_core::utils::SharedClock;
use rings_core::utils::SystemClock;
use serde::Serialize;

/// Version of the observability object nested in the HTTP status response.
pub const OPERATOR_SCHEMA_VERSION: u16 = 1;
/// Maximum number of recent message records retained in process memory and one status response.
pub const RECENT_MESSAGE_CAPACITY: usize = 32;
/// Maximum number of peer-rating records returned by one structured snapshot.
pub const PEER_RATING_CAPACITY: usize = 32;
/// Maximum number of lookup correlations retained until completion or timeout.
const LOOKUP_CAPACITY: usize = 1024;
/// Lookup age at which a scrape classifies an unanswered operation as timed out.
const LOOKUP_TIMEOUT_MS: u128 = 30_000;
/// Finite cumulative histogram boundaries for local DHT lookup latency.
const LOOKUP_LATENCY_BUCKETS_MS: [u64; 12] = [
    5, 10, 25, 50, 100, 250, 500, 1_000, 2_500, 5_000, 10_000, 30_000,
];

/// Process-local observability recorder shared by the processor and core swarm.
pub struct Observability {
    /// Unix epoch millisecond at which this recorder was constructed.
    process_started_at_ms: u128,
    /// Injected epoch clock capability for event and lookup correlation timestamps.
    clock: SharedClock,
    /// Complete bounded state protected for one short synchronous transition.
    state: Mutex<RuntimeState>,
}

/// Mutable state behind [`Observability`].
struct RuntimeState {
    /// Monotonic sequence assigned to bounded recent message records.
    next_event_sequence: u64,
    /// Counters indexed by activity, traffic category, then outcome.
    message_counts: [[[u64; 2]; 4]; 4],
    /// Oldest-first bounded message record ring.
    recent_messages: VecDeque<RecentMessageEvent>,
    /// Correlated lookup starts, never exported with their keys.
    lookups: BTreeMap<(LookupKind, LookupCorrelation), LookupStart>,
    /// Cumulative process-lifetime lookup totals and latency buckets.
    lookup_totals: LookupTotals,
}

/// Internal start witness for one in-flight lookup.
#[derive(Clone, Copy)]
struct LookupStart {
    /// Epoch millisecond of the local start observation.
    started_at_ms: u128,
}

/// Mutable cumulative DHT lookup counters.
#[derive(Clone, Default)]
struct LookupTotals {
    /// Lookup rounds begun during this process lifetime.
    started: u64,
    /// Lookup rounds that accepted an answer.
    succeeded: u64,
    /// Lookup rounds that failed before an answer.
    failed: u64,
    /// Lookup rounds classified as timed out by bounded retention.
    timed_out: u64,
    /// Sum of completed lookup latencies in milliseconds.
    latency_sum_ms: u128,
    /// Number of terminal lookups represented in the latency histogram.
    latency_count: u64,
    /// Cumulative histogram buckets in the order of [`LOOKUP_LATENCY_BUCKETS_MS`].
    latency_buckets: [u64; LOOKUP_LATENCY_BUCKETS_MS.len()],
}

/// One privacy-safe recent message record.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct RecentMessageEvent {
    /// Process-local order, useful for polling without a message identifier.
    pub sequence: u64,
    /// Unix epoch millisecond at which the operation completed locally.
    pub observed_at_ms: u128,
    /// `sent`, `received`, `forwarded`, or `stored`.
    pub action: &'static str,
    /// Finite scheduling category such as `application` or `dht_control`.
    pub category: &'static str,
    /// Compile-time protocol variant name; never an application topic or payload value.
    pub message_class: &'static str,
    /// `succeeded` or `failed`.
    pub outcome: &'static str,
}

/// Process-lifetime message counters with node-local semantics.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub struct MessageTotals {
    /// Successful locally originated operations accepted by the next hop.
    pub sent: u64,
    /// Successful logical inbound operations accepted by this node.
    pub received: u64,
    /// Successful relays accepted by their next hop.
    pub forwarded: u64,
    /// Successful offline-message holds written to a relay inbox.
    pub stored: u64,
    /// Failed operations across every action above.
    pub failed: u64,
}

/// Public DHT lookup counters and latency distribution.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct DhtLookupSnapshot {
    /// Lookup rounds begun during this process lifetime.
    pub total: u64,
    /// Lookup rounds that accepted a local or authenticated remote answer.
    pub succeeded: u64,
    /// Lookup rounds that terminated with an explicit local failure.
    pub failed: u64,
    /// Lookup rounds retained past the thirty-second observation bound.
    pub timed_out: u64,
    /// Lookup rounds still awaiting an answer.
    pub in_flight: u64,
    /// Sum of terminal lookup latency in milliseconds.
    pub latency_sum_ms: u128,
    /// Number of terminal lookups in the latency distribution.
    pub latency_count: u64,
    /// Cumulative latency buckets for external rate and threshold calculations.
    pub latency_buckets: Vec<LatencyBucket>,
}

/// One cumulative DHT latency histogram bucket.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct LatencyBucket {
    /// Inclusive upper bound in milliseconds.
    pub le_ms: u64,
    /// Number of completed lookups at or below the bound.
    pub count: u64,
}

/// Active session-key lifecycle without key material or key-derived identifiers.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct SessionKeySnapshot {
    /// Whether the active delegation is valid at snapshot time.
    pub valid: bool,
    /// Delegation creation time in Unix epoch milliseconds.
    pub created_at_ms: u128,
    /// Delegation expiry in Unix epoch milliseconds.
    pub expires_at_ms: u128,
    /// Saturating time until expiry in milliseconds.
    pub remaining_ms: u128,
    /// Successful runtime rotations during this process lifetime.
    pub rotation_succeeded_total: u64,
    /// Failed runtime rotations during this process lifetime.
    pub rotation_failed_total: u64,
    /// Whether this process can replace its signing delegation without restart.
    pub runtime_rotation_supported: bool,
}

/// Aggregate live relay-mailbox state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize)]
pub struct MailboxSnapshot {
    /// Live relay-inbox carriers retained by this node.
    pub registered: u64,
    /// Live held messages across all retained inboxes.
    pub held_messages: u64,
    /// Successful holds during this process lifetime.
    pub stored_total: u64,
}

/// Bounded structured local assessment of one peer.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct PeerRatingSnapshot {
    /// Public peer DID, present only in this bounded authenticated JSON response.
    pub peer: String,
    /// Advisory `healthy`, `unknown`, or `degraded` local reliability class.
    pub reliability: &'static str,
    /// Local resource-priority multiplier, not a network-wide reputation score.
    pub credit_score: f64,
    /// Successful recent sends in the active reliability epoch.
    pub sent: u64,
    /// Failed recent sends in the active reliability epoch.
    pub failed_to_send: u64,
    /// Successful recent receives in the active reliability epoch.
    pub received: u64,
    /// Failed recent receives in the active reliability epoch.
    pub failed_to_receive: u64,
}

/// Explicit separation of HTTP-process liveness and overlay readiness.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub struct HealthSnapshot {
    /// True because producing the response proves the authenticated API task is serving.
    pub process_api_healthy: bool,
    /// Number of active routable transport peers in the current snapshot.
    pub admitted_peer_count: u64,
    /// Whether the node currently has at least one admitted transport peer.
    pub has_admitted_peer: bool,
    /// Whether the local DHT currently has a successor.
    pub has_successor: bool,
    /// Whether the local DHT currently has a predecessor.
    pub has_predecessor: bool,
    /// Conservative participation signal: an admitted peer and a successor are both present,
    /// and the node's own delegation is before its signing horizon, so its frames still leave
    /// (#1063).
    pub overlay_ready: bool,
}

/// Complete JSON response returned by the v1 operator endpoint.
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct OperatorSnapshot {
    /// Schema version for compatibility-sensitive consumers.
    pub schema_version: u16,
    /// Unix epoch millisecond at which this snapshot was assembled.
    pub generated_at_ms: u128,
    /// Unix epoch millisecond at which process-local counters began.
    pub process_started_at_ms: u128,
    /// Explicit scope of message and lookup counters.
    pub counter_scope: &'static str,
    /// Process-lifetime node-local message totals.
    pub messages: MessageTotals,
    /// Oldest-first bounded recent message activity.
    pub recent_messages: Vec<RecentMessageEvent>,
    /// Session delegation lifecycle.
    pub session_key: SessionKeySnapshot,
    /// Aggregate local relay-mailbox state.
    pub mailboxes: MailboxSnapshot,
    /// Process-lifetime DHT lookup outcomes and latency.
    pub dht_lookups: DhtLookupSnapshot,
    /// Bounded local peer assessments.
    pub peer_ratings: Vec<PeerRatingSnapshot>,
    /// Process/API and overlay health as separate propositions.
    pub health: HealthSnapshot,
}

impl Default for RuntimeState {
    fn default() -> Self {
        Self {
            next_event_sequence: 0,
            message_counts: [[[0; 2]; 4]; 4],
            recent_messages: VecDeque::with_capacity(RECENT_MESSAGE_CAPACITY),
            lookups: BTreeMap::new(),
            lookup_totals: LookupTotals::default(),
        }
    }
}

impl Observability {
    /// Construct an empty process-local recorder at the current wall-clock instant.
    pub fn new() -> Self {
        Self::new_with_clock(Arc::new(SystemClock))
    }

    /// Construct an empty process-local recorder bound to `clock`.
    pub fn new_with_clock(clock: SharedClock) -> Self {
        let process_started_at_ms = clock.now_ms();
        Self {
            process_started_at_ms,
            clock,
            state: Mutex::new(RuntimeState::default()),
        }
    }

    /// Return the beginning of the process-local counter scope.
    pub const fn process_started_at_ms(&self) -> u128 {
        self.process_started_at_ms
    }

    /// Produce message and lookup state, expiring stale correlations as timeouts first.
    pub fn runtime_snapshot(&self, observed_at_ms: u128) -> RuntimeSnapshot {
        let mut state = lock_or_recover(&self.state);
        expire_lookups(&mut state, observed_at_ms);
        RuntimeSnapshot {
            messages: message_totals(&state.message_counts),
            recent_messages: state.recent_messages.iter().cloned().collect(),
            dht_lookups: lookup_snapshot(&state),
        }
    }
}

impl Default for Observability {
    fn default() -> Self {
        Self::new()
    }
}

/// Recorder-owned part of an operator snapshot.
pub struct RuntimeSnapshot {
    /// Process-lifetime message totals.
    pub messages: MessageTotals,
    /// Bounded recent message records.
    pub recent_messages: Vec<RecentMessageEvent>,
    /// Bounded lookup state and cumulative outcomes.
    pub dht_lookups: DhtLookupSnapshot,
}

impl SwarmObserver for Observability {
    fn observe_message(&self, observation: MessageObservation) {
        let observed_at_ms = self.clock.now_ms();
        let mut state = lock_or_recover(&self.state);
        let activity = activity_index(observation.activity);
        let category = category_index(observation.category);
        let outcome = outcome_index(observation.outcome);
        if let Some(count) = state
            .message_counts
            .get_mut(activity)
            .and_then(|categories| categories.get_mut(category))
            .and_then(|outcomes| outcomes.get_mut(outcome))
        {
            *count = count.saturating_add(1);
        }
        let sequence = state.next_event_sequence;
        state.next_event_sequence = state.next_event_sequence.saturating_add(1);
        if state.recent_messages.len() == RECENT_MESSAGE_CAPACITY {
            state.recent_messages.pop_front();
        }
        state.recent_messages.push_back(RecentMessageEvent {
            sequence,
            observed_at_ms,
            action: activity_name(observation.activity),
            category: category_name(observation.category),
            message_class: observation.message_class,
            outcome: outcome_name(observation.outcome),
        });
    }

    fn lookup_started(&self, kind: LookupKind, correlation: LookupCorrelation) {
        let observed_at_ms = self.clock.now_ms();
        let mut state = lock_or_recover(&self.state);
        expire_lookups(&mut state, observed_at_ms);
        if let Some(previous) = state.lookups.remove(&(kind, correlation)) {
            finish_lookup(&mut state.lookup_totals, previous, observed_at_ms, None);
        }
        while state.lookups.len() >= LOOKUP_CAPACITY {
            let Some(oldest) = state
                .lookups
                .iter()
                .min_by_key(|(_, start)| start.started_at_ms)
                .map(|(key, _)| *key)
            else {
                break;
            };
            if let Some(start) = state.lookups.remove(&oldest) {
                finish_lookup(&mut state.lookup_totals, start, observed_at_ms, None);
            }
        }
        state.lookup_totals.started = state.lookup_totals.started.saturating_add(1);
        state.lookups.insert((kind, correlation), LookupStart {
            started_at_ms: observed_at_ms,
        });
    }

    fn lookup_finished(
        &self,
        kind: LookupKind,
        correlation: LookupCorrelation,
        outcome: LookupOutcome,
    ) {
        let observed_at_ms = self.clock.now_ms();
        let mut state = lock_or_recover(&self.state);
        expire_lookups(&mut state, observed_at_ms);
        if let Some(start) = state.lookups.remove(&(kind, correlation)) {
            finish_lookup(
                &mut state.lookup_totals,
                start,
                observed_at_ms,
                Some(outcome),
            );
        }
    }
}

/// Return the current Unix epoch time in milliseconds for unit tests.
#[cfg(test)]
fn now_ms() -> u128 {
    rings_core::utils::get_epoch_ms()
}

/// Acquire a recorder lock, preserving collected state after an earlier panic.
fn lock_or_recover<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Map a finite message activity to its counter-array index.
const fn activity_index(activity: MessageActivity) -> usize {
    match activity {
        MessageActivity::Sent => 0,
        MessageActivity::Received => 1,
        MessageActivity::Forwarded => 2,
        MessageActivity::Stored => 3,
    }
}

/// Map a finite message category to its counter-array index.
const fn category_index(category: MessageCategory) -> usize {
    match category {
        MessageCategory::DhtControl => 0,
        MessageCategory::Storage => 1,
        MessageCategory::E2e => 2,
        MessageCategory::Application => 3,
    }
}

/// Map a finite operation outcome to its counter-array index.
const fn outcome_index(outcome: ObservationOutcome) -> usize {
    match outcome {
        ObservationOutcome::Succeeded => 0,
        ObservationOutcome::Failed => 1,
    }
}

/// Return the stable lowercase export name for a message activity.
const fn activity_name(activity: MessageActivity) -> &'static str {
    match activity {
        MessageActivity::Sent => "sent",
        MessageActivity::Received => "received",
        MessageActivity::Forwarded => "forwarded",
        MessageActivity::Stored => "stored",
    }
}

/// Return the stable lowercase export name for a scheduling category.
const fn category_name(category: MessageCategory) -> &'static str {
    match category {
        MessageCategory::DhtControl => "dht_control",
        MessageCategory::Storage => "storage",
        MessageCategory::E2e => "e2e",
        MessageCategory::Application => "application",
    }
}

/// Return the stable lowercase export name for an operation outcome.
const fn outcome_name(outcome: ObservationOutcome) -> &'static str {
    match outcome {
        ObservationOutcome::Succeeded => "succeeded",
        ObservationOutcome::Failed => "failed",
    }
}

/// Collapse category-specific counters into the public message totals.
fn message_totals(counts: &[[[u64; 2]; 4]; 4]) -> MessageTotals {
    let successful = |activity: MessageActivity| {
        counts
            .get(activity_index(activity))
            .into_iter()
            .flatten()
            .fold(0_u64, |total, category| {
                total.saturating_add(category.first().copied().unwrap_or_default())
            })
    };
    let failed = counts
        .iter()
        .flat_map(|categories| categories.iter())
        .fold(0_u64, |total, outcomes| {
            total.saturating_add(outcomes.get(1).copied().unwrap_or_default())
        });
    MessageTotals {
        sent: successful(MessageActivity::Sent),
        received: successful(MessageActivity::Received),
        forwarded: successful(MessageActivity::Forwarded),
        stored: successful(MessageActivity::Stored),
        failed,
    }
}

/// Complete all retained lookups older than the observation timeout.
fn expire_lookups(state: &mut RuntimeState, observed_at_ms: u128) {
    let expired = state
        .lookups
        .iter()
        .filter(|(_, start)| {
            observed_at_ms.saturating_sub(start.started_at_ms) >= LOOKUP_TIMEOUT_MS
        })
        .map(|(key, _)| *key)
        .collect::<Vec<_>>();
    for key in expired {
        if let Some(start) = state.lookups.remove(&key) {
            finish_lookup(&mut state.lookup_totals, start, observed_at_ms, None);
        }
    }
}

/// Record one terminal lookup outcome and its cumulative latency buckets.
fn finish_lookup(
    totals: &mut LookupTotals,
    start: LookupStart,
    observed_at_ms: u128,
    outcome: Option<LookupOutcome>,
) {
    let latency_ms = observed_at_ms.saturating_sub(start.started_at_ms);
    match outcome {
        Some(LookupOutcome::Succeeded) => totals.succeeded = totals.succeeded.saturating_add(1),
        Some(LookupOutcome::Failed) => totals.failed = totals.failed.saturating_add(1),
        None => totals.timed_out = totals.timed_out.saturating_add(1),
    }
    totals.latency_sum_ms = totals.latency_sum_ms.saturating_add(latency_ms);
    totals.latency_count = totals.latency_count.saturating_add(1);
    for (count, bound) in totals
        .latency_buckets
        .iter_mut()
        .zip(LOOKUP_LATENCY_BUCKETS_MS)
    {
        if latency_ms <= u128::from(bound) {
            *count = count.saturating_add(1);
        }
    }
}

/// Copy internal bounded lookup state into its identifier-free public form.
fn lookup_snapshot(state: &RuntimeState) -> DhtLookupSnapshot {
    DhtLookupSnapshot {
        total: state.lookup_totals.started,
        succeeded: state.lookup_totals.succeeded,
        failed: state.lookup_totals.failed,
        timed_out: state.lookup_totals.timed_out,
        in_flight: u64::try_from(state.lookups.len()).unwrap_or(u64::MAX),
        latency_sum_ms: state.lookup_totals.latency_sum_ms,
        latency_count: state.lookup_totals.latency_count,
        latency_buckets: LOOKUP_LATENCY_BUCKETS_MS
            .iter()
            .copied()
            .zip(state.lookup_totals.latency_buckets)
            .map(|(le_ms, count)| LatencyBucket { le_ms, count })
            .collect(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn observation(activity: MessageActivity, outcome: ObservationOutcome) -> MessageObservation {
        MessageObservation {
            activity,
            category: MessageCategory::Application,
            message_class: "CustomMessage",
            outcome,
        }
    }

    #[test]
    fn recent_activity_is_bounded_and_payload_free() {
        let recorder = Observability::new();
        for _ in 0..(RECENT_MESSAGE_CAPACITY + 7) {
            recorder.observe_message(observation(
                MessageActivity::Received,
                ObservationOutcome::Succeeded,
            ));
        }
        let snapshot = recorder.runtime_snapshot(now_ms());
        assert_eq!(snapshot.recent_messages.len(), RECENT_MESSAGE_CAPACITY);
        assert_eq!(
            snapshot.messages.received,
            (RECENT_MESSAGE_CAPACITY + 7) as u64
        );
        let json = serde_json::to_string(&snapshot.recent_messages).expect("serialize events");
        assert!(!json.contains("payload"));
        assert!(!json.contains("transaction"));
    }

    #[test]
    fn timed_out_lookup_leaves_no_in_flight_identifier() {
        let recorder = Observability::new();
        let correlation = LookupCorrelation::Transaction(uuid::Uuid::new_v4());
        recorder.lookup_started(LookupKind::Successor, correlation);
        let future = now_ms().saturating_add(LOOKUP_TIMEOUT_MS);
        let snapshot = recorder.runtime_snapshot(future);
        assert_eq!(snapshot.dht_lookups.total, 1);
        assert_eq!(snapshot.dht_lookups.timed_out, 1);
        assert_eq!(snapshot.dht_lookups.in_flight, 0);
    }

    #[test]
    fn production_bounded_snapshot_fixture_stays_under_32_kib() {
        /// Test-only wire shape matching the compact HTTP status projection.
        #[derive(Serialize)]
        struct CompactStatusFixture {
            /// Conservative version string included in every compact response.
            version: &'static str,
            /// Maximum-width observability fixture under the production collection caps.
            observability: OperatorSnapshot,
        }

        let message_class = "ConservativeSixtyFourCharacterProtocolMessageClassNamePlaceholderXX";
        let peer = "did:ring:conservative-sixty-four-character-peer-identifier-placeholder";
        let snapshot = OperatorSnapshot {
            schema_version: OPERATOR_SCHEMA_VERSION,
            generated_at_ms: 2,
            process_started_at_ms: 1,
            counter_scope: "node_local_process_lifetime",
            messages: MessageTotals {
                sent: u64::MAX,
                received: u64::MAX,
                forwarded: u64::MAX,
                stored: u64::MAX,
                failed: u64::MAX,
            },
            recent_messages: (0..RECENT_MESSAGE_CAPACITY)
                .map(|index| RecentMessageEvent {
                    sequence: u64::try_from(index).unwrap_or(u64::MAX),
                    observed_at_ms: u128::MAX.saturating_sub(index as u128),
                    action: "forwarded",
                    category: "application",
                    message_class,
                    outcome: "succeeded",
                })
                .collect(),
            session_key: SessionKeySnapshot {
                valid: true,
                created_at_ms: u128::MAX,
                expires_at_ms: u128::MAX,
                remaining_ms: u128::MAX,
                rotation_succeeded_total: u64::MAX,
                rotation_failed_total: u64::MAX,
                runtime_rotation_supported: false,
            },
            mailboxes: MailboxSnapshot {
                registered: u64::MAX,
                held_messages: u64::MAX,
                stored_total: u64::MAX,
            },
            dht_lookups: DhtLookupSnapshot {
                total: u64::MAX,
                succeeded: u64::MAX,
                failed: u64::MAX,
                timed_out: u64::MAX,
                in_flight: u64::MAX,
                latency_sum_ms: u128::MAX,
                latency_count: u64::MAX,
                latency_buckets: (0..LOOKUP_LATENCY_BUCKETS_MS.len())
                    .map(|_| LatencyBucket {
                        le_ms: u64::MAX,
                        count: u64::MAX,
                    })
                    .collect(),
            },
            peer_ratings: (0..PEER_RATING_CAPACITY)
                .map(|_| PeerRatingSnapshot {
                    peer: peer.to_string(),
                    reliability: "degraded",
                    credit_score: f64::MAX,
                    sent: u64::MAX,
                    failed_to_send: u64::MAX,
                    received: u64::MAX,
                    failed_to_receive: u64::MAX,
                })
                .collect(),
            health: HealthSnapshot {
                process_api_healthy: true,
                admitted_peer_count: u64::MAX,
                has_admitted_peer: true,
                has_successor: true,
                has_predecessor: false,
                overlay_ready: true,
            },
        };

        let response = CompactStatusFixture {
            version: "0.30.0-conservative-version-placeholder",
            observability: snapshot,
        };
        let encoded = serde_json::to_vec(&response).expect("serialize compact status response");
        assert!(
            encoded.len() < 32 * 1024,
            "compact status response was {} bytes",
            encoded.len()
        );
    }

    #[test]
    fn injected_manual_clock_drives_event_and_lookup_timestamps() {
        use rings_core::utils::Clock;

        let manual = rings_core::utils::ManualClock::new(1_700_000_000_000);
        let shared: SharedClock = Arc::new(manual.clone());
        let recorder = Observability::new_with_clock(shared);
        assert_eq!(recorder.process_started_at_ms(), 1_700_000_000_000);

        manual.advance(125);
        recorder.observe_message(observation(
            MessageActivity::Sent,
            ObservationOutcome::Succeeded,
        ));
        let correlation = LookupCorrelation::Transaction(uuid::Uuid::new_v4());
        recorder.lookup_started(LookupKind::Successor, correlation);

        manual.advance(250);
        recorder.lookup_finished(LookupKind::Successor, correlation, LookupOutcome::Succeeded);

        let snapshot = recorder.runtime_snapshot(manual.now_ms());
        assert_eq!(
            snapshot.recent_messages[0].observed_at_ms,
            1_700_000_000_125
        );
        assert_eq!(snapshot.dht_lookups.succeeded, 1);
        assert_eq!(snapshot.dht_lookups.latency_sum_ms, 250);
    }
}