trusty-console 0.12.2

Web console that detects and surfaces running trusty services as a home page with service cards
Documentation
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
//! Tests for the durable NDJSON event log (issue #6848 slice 3b).

use std::path::Path;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;

use chrono::{NaiveDate, Utc};
use trusty_common::control_bus::{EventId, HarnessEvent, HarnessPayload, HarnessSource};

use super::config::{LogConfig, day_file_name, parse_day_file_name};
use super::format::{encode_line, read_events};
use super::recovery::{earliest_seq, list_log_files, recover_next_seq};
use super::replay::{ReplayItem, replay_since};
use super::retention::{enforce_retention, files_to_delete};
use super::writer::{DurableLog, rotate, write_line};

fn date(y: i32, m: u32, d: u32) -> NaiveDate {
    NaiveDate::from_ymd_opt(y, m, d).expect("valid test date")
}

/// Build a `HarnessEvent` fixture with a given `seq`, everything else fixed.
fn event(seq: u64) -> HarnessEvent {
    HarnessEvent {
        source: HarnessSource::Mpm,
        session: None,
        seq,
        at: Utc::now(),
        payload: HarnessPayload::Ping,
        id: EventId::new(),
        parent_id: None,
    }
}

/// Write `events` to `dir`'s day file for `d`, each as one NDJSON line —
/// bypasses the writer task entirely, for tests that need direct control over
/// on-disk content (a specific seq sequence, a deliberately corrupt line).
async fn seed_file(dir: &Path, d: NaiveDate, events: &[HarnessEvent]) {
    tokio::fs::create_dir_all(dir).await.expect("create dir");
    let mut content = Vec::new();
    for e in events {
        content.extend_from_slice(&encode_line(e));
    }
    tokio::fs::write(dir.join(day_file_name(d)), content)
        .await
        .expect("seed day file");
}

/// Append a raw, possibly-invalid line to `dir`'s day file for `d`.
async fn append_raw_line(dir: &Path, d: NaiveDate, line: &str) {
    tokio::fs::create_dir_all(dir).await.expect("create dir");
    use tokio::io::AsyncWriteExt as _;
    let mut f = tokio::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(dir.join(day_file_name(d)))
        .await
        .expect("open day file");
    f.write_all(line.as_bytes()).await.expect("write raw line");
}

// ─── config: filename format ────────────────────────────────────────────

#[test]
fn day_file_name_round_trips() {
    let d = date(2026, 9, 8);
    let name = day_file_name(d);
    assert_eq!(name, "2026-09-08.ndjson");
    assert_eq!(parse_day_file_name(&name), Some(d));
}

#[test]
fn parse_day_file_name_rejects_a_non_matching_name() {
    assert_eq!(parse_day_file_name("not-a-date.ndjson"), None);
    assert_eq!(parse_day_file_name("2026-09-08.txt"), None);
    assert_eq!(parse_day_file_name("README.md"), None);
}

// ─── format: tolerant whole-file decode ─────────────────────────────────

#[tokio::test]
async fn read_events_returns_empty_for_an_empty_file() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let path = tmp.path().join("empty.ndjson");
    tokio::fs::write(&path, b"").await.expect("write empty");
    let events = read_events(&path).await.expect("read");
    assert!(events.is_empty());
}

#[tokio::test]
async fn read_events_skips_a_truncated_final_line() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let path = tmp.path().join("truncated.ndjson");
    let good = event(1);
    let mut content = encode_line(&good);
    content.extend_from_slice(b"{\"source\":\"mpm\",\"seq\":2,\"at\":\"trunc");
    tokio::fs::write(&path, &content).await.expect("write");

    let events = read_events(&path).await.expect("read");
    assert_eq!(
        events.len(),
        1,
        "the truncated final line is skipped, not fatal"
    );
    assert_eq!(events[0].id, good.id);
}

#[tokio::test]
async fn read_events_skips_a_corrupt_interior_line_and_keeps_reading() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let path = tmp.path().join("corrupt.ndjson");
    let first = event(1);
    let third = event(3);
    let mut content = encode_line(&first);
    content.extend_from_slice(b"not json at all\n");
    content.extend_from_slice(&encode_line(&third));
    tokio::fs::write(&path, &content).await.expect("write");

    let events = read_events(&path).await.expect("read");
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].id, first.id);
    assert_eq!(events[1].id, third.id);
}

// ─── recovery: file listing, seq high-water mark, earliest retained ─────

#[tokio::test]
async fn list_log_files_ignores_non_matching_names() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    tokio::fs::create_dir_all(tmp.path()).await.expect("dir");
    tokio::fs::write(tmp.path().join("README.md"), b"hello")
        .await
        .expect("write stray file");
    seed_file(tmp.path(), date(2026, 9, 8), &[event(1)]).await;

    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(files.len(), 1);
    assert_eq!(files[0].0, date(2026, 9, 8));
}

#[tokio::test]
async fn list_log_files_sorts_ascending_by_date() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 8), &[event(2)]).await;
    seed_file(tmp.path(), date(2026, 9, 6), &[event(1)]).await;
    seed_file(tmp.path(), date(2026, 9, 7), &[event(2)]).await;

    let files = list_log_files(tmp.path()).await.expect("list");
    let dates: Vec<_> = files.iter().map(|(d, _)| *d).collect();
    assert_eq!(
        dates,
        vec![date(2026, 9, 6), date(2026, 9, 7), date(2026, 9, 8)]
    );
}

#[tokio::test]
async fn recover_next_seq_starts_at_one_with_no_files() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(recover_next_seq(&files).await.expect("recover"), 1);
}

#[tokio::test]
async fn recover_next_seq_continues_from_the_newest_non_empty_file() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 6), &[event(1), event(2)]).await;
    seed_file(
        tmp.path(),
        date(2026, 9, 7),
        &[event(3), event(4), event(5)],
    )
    .await;
    // A day rolled over right before a crash: today's file exists but is
    // empty. Recovery must not stop at the newest FILE, only the newest
    // NON-EMPTY one.
    seed_file(tmp.path(), date(2026, 9, 8), &[]).await;

    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(recover_next_seq(&files).await.expect("recover"), 6);
}

#[tokio::test]
async fn recover_next_seq_tolerates_a_truncated_final_line() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 8), &[event(1), event(2)]).await;
    append_raw_line(
        tmp.path(),
        date(2026, 9, 8),
        "{\"source\":\"mpm\",\"seq\":3,\"trunc",
    )
    .await;

    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(
        recover_next_seq(&files).await.expect("recover"),
        3,
        "the truncated seq-3 write never counted; numbering resumes at 3, not 4"
    );
}

#[tokio::test]
async fn earliest_seq_reads_the_oldest_retained_file() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 6), &[event(5), event(6)]).await;
    seed_file(tmp.path(), date(2026, 9, 7), &[event(7)]).await;

    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(earliest_seq(&files).await.expect("earliest"), Some(5));
}

#[tokio::test]
async fn earliest_seq_is_none_for_an_empty_log() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let files = list_log_files(tmp.path()).await.expect("list");
    assert_eq!(earliest_seq(&files).await.expect("earliest"), None);
}

// ─── retention ───────────────────────────────────────────────────────────

#[test]
fn files_to_delete_keeps_exactly_retain_days() {
    let files = vec![
        (date(2026, 9, 1), std::path::PathBuf::from("1")),
        (date(2026, 9, 5), std::path::PathBuf::from("5")),
        (date(2026, 9, 6), std::path::PathBuf::from("6")),
    ];
    // retain_days=2 from today=9/6 keeps 9/5 and 9/6; 9/1 is expired.
    let doomed = files_to_delete(&files, date(2026, 9, 6), 2);
    assert_eq!(doomed.len(), 1);
    assert_eq!(doomed[0].0, date(2026, 9, 1));
}

#[test]
fn files_to_delete_keeps_everything_within_the_window() {
    let files = vec![
        (date(2026, 9, 5), std::path::PathBuf::from("5")),
        (date(2026, 9, 6), std::path::PathBuf::from("6")),
    ];
    let doomed = files_to_delete(&files, date(2026, 9, 6), 7);
    assert!(doomed.is_empty());
}

#[tokio::test]
async fn enforce_retention_deletes_only_the_expired_files() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 1), &[event(1)]).await;
    seed_file(tmp.path(), date(2026, 9, 6), &[event(2)]).await;

    let survivors = enforce_retention(tmp.path(), date(2026, 9, 6), 2)
        .await
        .expect("enforce");
    assert_eq!(survivors.len(), 1);
    assert_eq!(survivors[0].0, date(2026, 9, 6));
    assert!(!tmp.path().join(day_file_name(date(2026, 9, 1))).exists());
    assert!(tmp.path().join(day_file_name(date(2026, 9, 6))).exists());
}

// ─── replay ────────────────────────────────────────────────────────────

#[tokio::test]
async fn replay_since_zero_returns_everything_in_order() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 6), &[event(1), event(2)]).await;
    seed_file(tmp.path(), date(2026, 9, 7), &[event(3)]).await;

    let items = replay_since(tmp.path(), 0, Some(1)).await.expect("replay");
    let seqs: Vec<u64> = items
        .into_iter()
        .map(|i| match i {
            ReplayItem::Event(e) => e.seq,
            ReplayItem::Gap { .. } => panic!("no gap expected"),
        })
        .collect();
    assert_eq!(seqs, vec![1, 2, 3]);
}

#[tokio::test]
async fn replay_since_a_mid_seq_returns_only_the_remainder() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(
        tmp.path(),
        date(2026, 9, 6),
        &[event(1), event(2), event(3)],
    )
    .await;

    let items = replay_since(tmp.path(), 1, Some(1)).await.expect("replay");
    let seqs: Vec<u64> = items
        .into_iter()
        .map(|i| match i {
            ReplayItem::Event(e) => e.seq,
            ReplayItem::Gap { .. } => panic!("no gap expected"),
        })
        .collect();
    assert_eq!(seqs, vec![2, 3]);
}

#[tokio::test]
async fn replay_since_caught_up_returns_no_events_and_no_gap() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(tmp.path(), date(2026, 9, 6), &[event(1), event(2)]).await;

    let items = replay_since(tmp.path(), 2, Some(1)).await.expect("replay");
    assert!(items.is_empty());
}

#[tokio::test]
async fn replay_since_before_retention_yields_a_leading_gap() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    // Retention already discarded seq 1-4; the oldest retained file starts
    // at seq 5.
    seed_file(tmp.path(), date(2026, 9, 7), &[event(5), event(6)]).await;

    let items = replay_since(tmp.path(), 0, Some(5)).await.expect("replay");
    match &items[0] {
        ReplayItem::Gap {
            after_seq,
            before_seq,
        } => {
            assert_eq!(*after_seq, 0);
            assert_eq!(*before_seq, 5);
        }
        ReplayItem::Event(_) => panic!("expected a leading gap marker"),
    }
    assert_eq!(items.len(), 3, "gap marker plus the two retained events");
}

#[tokio::test]
async fn replay_since_across_a_backpressure_drop_yields_an_interior_gap() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    // seq 2 was never written — dropped under backpressure at ingest time.
    seed_file(tmp.path(), date(2026, 9, 6), &[event(1), event(3)]).await;

    let items = replay_since(tmp.path(), 0, Some(1)).await.expect("replay");
    assert_eq!(items.len(), 3, "event 1, a gap for 2, then event 3");
    assert!(matches!(items[0], ReplayItem::Event(ref e) if e.seq == 1));
    match &items[1] {
        ReplayItem::Gap {
            after_seq,
            before_seq,
        } => {
            assert_eq!(*after_seq, 1);
            assert_eq!(*before_seq, 3);
        }
        ReplayItem::Event(_) => panic!("expected an interior gap marker"),
    }
    assert!(matches!(items[2], ReplayItem::Event(ref e) if e.seq == 3));
}

// #6848: regression for the CRITICAL gap-detection bug the code-critic
// review found — `previous_seq` used to start at `None` and was only seeded
// inside the leading-retention-gap branch, so a drop on the FIRST seq after
// `since_seq` (the ordinary reconnect case, no retention gap involved) was
// never checked against `since_seq` and produced no gap marker at all. Day
// file holds seq [1, 2, 4, 5] — seq 3 was dropped under backpressure — and a
// client reconnects from `since_seq = 2`, already inside the retained
// window. This must fail before the `replay.rs` fix (no `Gap` before event
// 4) and pass after it.
#[tokio::test]
async fn replay_since_a_drop_on_the_first_replayed_seq_yields_a_gap() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    seed_file(
        tmp.path(),
        date(2026, 9, 6),
        &[event(1), event(2), event(4), event(5)],
    )
    .await;

    let items = replay_since(tmp.path(), 2, Some(1)).await.expect("replay");
    assert_eq!(
        items.len(),
        3,
        "a gap for seq 3 immediately, then events 4 and 5"
    );
    match &items[0] {
        ReplayItem::Gap {
            after_seq,
            before_seq,
        } => {
            assert_eq!(*after_seq, 2);
            assert_eq!(*before_seq, 4);
        }
        ReplayItem::Event(_) => {
            panic!("expected a gap marker for the seq dropped immediately after since_seq")
        }
    }
    assert!(matches!(items[1], ReplayItem::Event(ref e) if e.seq == 4));
    assert!(matches!(items[2], ReplayItem::Event(ref e) if e.seq == 5));
}

#[tokio::test]
async fn replay_since_on_an_empty_log_returns_nothing() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let items = replay_since(tmp.path(), 0, None)
        .await
        .expect("replay an empty log");
    assert!(items.is_empty(), "no files, no gap, no events");
}

// ─── DurableLog / writer task ────────────────────────────────────────────

fn config(dir: &Path) -> LogConfig {
    LogConfig {
        dir: dir.to_path_buf(),
        retain_days: 7,
    }
}

async fn wait_until(budget: std::time::Duration, mut predicate: impl FnMut() -> bool) {
    let deadline = tokio::time::Instant::now() + budget;
    loop {
        if predicate() {
            return;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "condition did not become true within {budget:?}"
        );
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
    }
}

#[tokio::test]
async fn open_recovers_next_seq_from_an_existing_log() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let today = Utc::now().date_naive();
    seed_file(tmp.path(), today, &[event(1), event(2)]).await;

    let (_log, recovered) = DurableLog::open(config(tmp.path()))
        .await
        .expect("open existing log");
    assert_eq!(recovered.next_seq, 3);
}

#[tokio::test]
async fn events_written_are_readable_back_in_order() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let (log, _recovered) = DurableLog::open(config(tmp.path()))
        .await
        .expect("open fresh log");

    assert!(log.enqueue(event(1)));
    assert!(log.enqueue(event(2)));
    wait_until(std::time::Duration::from_secs(1), || log.written() >= 2).await;

    let items = log.replay_since(0).await.expect("replay");
    let seqs: Vec<u64> = items
        .into_iter()
        .map(|i| match i {
            ReplayItem::Event(e) => e.seq,
            ReplayItem::Gap { .. } => panic!("no gap expected"),
        })
        .collect();
    assert_eq!(seqs, vec![1, 2]);
}

#[tokio::test]
async fn backpressure_drops_are_counted_and_do_not_block_enqueue() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    // Capacity 1: the second `enqueue`, issued with no `.await` in between,
    // races the writer task for the single slot. On this crate's default
    // (current-thread) `#[tokio::test]` runtime the writer cannot have been
    // polled yet at this point, so the race is deterministic — the same
    // reasoning `event_bus::tests::connections_beyond_the_limit_wait_for_a_free_slot`
    // relies on for its own semaphore-saturation proof.
    let (log, _recovered) = DurableLog::open_with_capacity(config(tmp.path()), 1)
        .await
        .expect("open fresh log");

    let first = log.enqueue(event(1));
    let second = log.enqueue(event(2));
    assert!(first, "the first enqueue fills the only slot");
    assert!(
        !second,
        "the second enqueue must not block or panic; it reports backpressure instead"
    );
}

#[tokio::test]
async fn rotation_opens_a_new_file_and_keeps_seq_continuity() {
    // Simulates the day boundary having already crossed once: a "yesterday"
    // file holds the tail of a prior day's numbering, and recovery must
    // resume from it rather than restarting at 1 just because today's file
    // does not exist yet.
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let yesterday = Utc::now().date_naive().pred_opt().expect("valid date");
    // Seq starts at 1 (as it always does for a genuinely fresh log) so
    // `earliest_retained_seq` is legitimately 1 and `replay_since(0)` sees no
    // retention gap — a log that started numbering higher would make
    // `replay_since` (correctly) report seq 1 onward as lost to retention,
    // which is not what this test is exercising.
    seed_file(tmp.path(), yesterday, &[event(1), event(2)]).await;

    let (log, recovered) = DurableLog::open(config(tmp.path()))
        .await
        .expect("open log spanning a day boundary");
    assert_eq!(recovered.next_seq, 3);

    assert!(log.enqueue(event(3)));
    wait_until(std::time::Duration::from_secs(1), || log.written() >= 1).await;

    let today = Utc::now().date_naive();
    assert!(
        tmp.path().join(day_file_name(today)).exists(),
        "the writer rotates to today's file rather than appending to yesterday's"
    );
    let items = log
        .replay_since(0)
        .await
        .expect("replay across the boundary");
    let seqs: Vec<u64> = items
        .into_iter()
        .map(|i| match i {
            ReplayItem::Event(e) => e.seq,
            ReplayItem::Gap { .. } => panic!("no gap expected"),
        })
        .collect();
    assert_eq!(
        seqs,
        vec![1, 2, 3],
        "seq stays continuous across the file boundary"
    );
}

// #6848: regression for the CRITICAL missing-fsync-at-rotation finding.
// Drives `rotate` directly (rather than waiting for a real UTC day change)
// so the outgoing day's file can be dropped WITHOUT the writer task's own
// graceful-shutdown sync ever running — the exact "crash before shutdown"
// scenario the fsync-at-rotation fix protects. Proves both that `rotate`
// syncs the outgoing handle without erroring and that the seq high-water
// mark recovers correctly across both files afterward.
#[tokio::test]
async fn rotation_fsyncs_the_outgoing_file_before_opening_the_next_one() {
    let tmp = tempfile::TempDir::new().expect("tempdir");
    let day1 = date(2026, 1, 1);
    let day2 = date(2026, 1, 2);
    let earliest_retained_seq = Arc::new(AtomicU64::new(0));

    let (mut file1, path1) = rotate(&config(tmp.path()), day1, &earliest_retained_seq, None)
        .await
        .expect("open day1's file");
    write_line(&mut file1, &path1, &event(1))
        .await
        .expect("write seq 1 to day1");

    // The rotation itself fsyncs `file1` before opening day2's file.
    let (mut file2, path2) = rotate(
        &config(tmp.path()),
        day2,
        &earliest_retained_seq,
        Some(&mut file1),
    )
    .await
    .expect("rotate to day2's file");
    write_line(&mut file2, &path2, &event(2))
        .await
        .expect("write seq 2 to day2");

    // Ungraceful drop — no writer-task shutdown sync runs. If rotation's own
    // fsync did not happen, this is exactly the scenario that would lose
    // day1's data on a real crash.
    drop(file1);
    drop(file2);

    let recovered = recover_next_seq(&[(day1, path1), (day2, path2)])
        .await
        .expect("recover across both files");
    assert_eq!(
        recovered, 3,
        "both seqs are readable back after an ungraceful drop post-rotation"
    );
}