hawdb-storage 0.12.0

An embedded Rust graph database for local data-plane workloads
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
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
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;
use hawdb_core::{
    RuntimeCancellationToken, RuntimeIoWaveController, RuntimeIoWaveError, RuntimeIoWavePermit,
    RuntimeIoWaveTryAcquire, RuntimeMemoryError,
};
use hawdb_qos::{
    IoConcurrencyBudget, RuntimeGovernor, RuntimeGovernorConfig, RuntimeMemorySnapshot,
    RuntimeResourceBudget, RuntimeResourceSnapshot, RuntimeWorkRequest,
};
use std::sync::atomic::{AtomicUsize, Ordering};

#[path = "tests/projection_memory.rs"]
mod projection_memory;

#[derive(Debug)]
struct CountingWaves {
    admitted: RuntimeTaskContext,
    calls: AtomicUsize,
    cancel_at: usize,
    cancellation: RuntimeCancellationToken,
}

impl CountingWaves {
    fn observe(&self) {
        if self.calls.fetch_add(1, Ordering::SeqCst) + 1 == self.cancel_at {
            self.cancellation.cancel();
        }
    }
}

impl RuntimeIoWaveController for CountingWaves {
    fn acquire(
        &self,
        slots: NonZeroUsize,
        _task: &RuntimeTaskContext,
    ) -> std::result::Result<Box<dyn RuntimeIoWavePermit>, RuntimeIoWaveError> {
        let permit = self
            .admitted
            .acquire_io_wave(slots)?
            .expect("real governor I/O lease");
        self.observe();
        Ok(permit)
    }

    fn try_acquire(
        &self,
        slots: NonZeroUsize,
        _task: &RuntimeTaskContext,
    ) -> std::result::Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError> {
        match self.admitted.try_acquire_io_wave(slots)? {
            RuntimeIoWaveTryAcquire::Acquired(Some(permit)) => {
                self.observe();
                Ok(Some(permit))
            }
            RuntimeIoWaveTryAcquire::Pending => Ok(None),
            RuntimeIoWaveTryAcquire::Acquired(None) => {
                panic!("the counting adapter must delegate to a real governor I/O lease")
            }
        }
    }
}

fn governor(bytes: u64) -> RuntimeGovernor {
    RuntimeGovernor::new(
        RuntimeGovernorConfig {
            memory_budget_bytes: Some(bytes),
            background_task_limit: Some(NonZeroUsize::MIN),
            ..RuntimeGovernorConfig::shared_host()
        },
        RuntimeResourceSnapshot::from_parts(
            RuntimeResourceBudget::from_limits(NonZeroUsize::MIN, None, None),
            RuntimeMemorySnapshot::from_limits(Some(1 << 30), Some(1 << 30), None, None, None),
        ),
        IoConcurrencyBudget::new(2, 1),
    )
}

#[test]
fn checkpoint_units_wal_framing_actual_suffix_denies_memory_and_fully_retries() {
    let directory = std::env::temp_dir().join(format!(
        "hawdb-checkpoint-wal-framing-{}",
        hawdb_core::generate_uuidv7().unwrap()
    ));
    let mut catalog = Catalog::default();
    let mut store = GraphStore::open(&directory, &mut catalog).unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([("id".into(), Value::Int(1))]),
        )
        .unwrap();
    let source = store.checkpoint_source();
    let source_catalog = catalog.clone();
    let source_identity = source.checkpoint_source_identity();
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([
                ("id".into(), Value::Int(2)),
                ("payload".into(), Value::String("\0界🙂".repeat(30_000))),
            ]),
        )
        .unwrap();
    let identity = store.checkpoint_source_identity();
    let durable = store.durable.as_ref().unwrap();
    let wal_path = durable.wal_path.clone();
    let manifest_path = durable.manifest_path().to_path_buf();
    let wal = std::fs::read(&wal_path).unwrap();
    let manifest = std::fs::read(&manifest_path).unwrap();
    let expected = store
        .node_records_owned()
        .collect::<crate::Result<Vec<_>>>()
        .unwrap();

    let denied = governor(1);
    let permit = denied
        .try_admit(RuntimeWorkRequest::background_maintenance(1).with_io_wave_slots(1))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let result = candidate.catch_up_with_task_context(&store, &task);
    assert!(
        result.is_err(),
        "the framed suffix must be admitted before allocation"
    );
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("remaining reservation"));
    assert_eq!(store.checkpoint_source_identity(), identity);
    assert_eq!(std::fs::read(&wal_path).unwrap(), wal);
    assert_eq!(std::fs::read(&manifest_path).unwrap(), manifest);
    store.ensure_usable().unwrap();
    drop(candidate);
    drop(task);
    drop(permit);
    let closed = denied.snapshot();
    assert_eq!(closed.active_background_io_slots, 0);
    assert_eq!(closed.active_cpu_slots, 0);
    assert_eq!(closed.active_background_tasks, 0);
    assert_eq!(closed.admitted_memory_bytes, 0);

    // Recreate the same pinned base and replay the full suffix. Ordinary
    // publication and reopen are the complete-result references.
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    let allowed = governor(2 * 1024 * 1024);
    let permit = allowed
        .try_admit(
            RuntimeWorkRequest::background_maintenance(2 * 1024 * 1024).with_io_wave_slots(1),
        )
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    candidate.catch_up_with_task_context(&store, &task).unwrap();
    store
        .publish_checkpoint_candidate(&mut candidate, None, &Default::default())
        .unwrap();
    drop(candidate);
    drop(task);
    drop(permit);
    assert_eq!(allowed.snapshot().admitted_memory_bytes, 2 * 1024 * 1024);
    assert_eq!(source.checkpoint_source_identity(), source_identity);
    drop(source);
    drop(store);
    assert_eq!(allowed.snapshot().admitted_memory_bytes, 0);
    let recovered = GraphStore::open(&directory, &mut catalog).unwrap();
    assert_eq!(
        recovered
            .node_records_owned()
            .collect::<crate::Result<Vec<_>>>()
            .unwrap(),
        expected
    );
    drop(recovered);
    std::fs::remove_dir_all(directory).unwrap();
}

#[test]
fn checkpoint_units_wal_framing_actual_suffix_cancels_every_io_wave_and_retries_same_admission() {
    let directory = std::env::temp_dir().join(format!(
        "hawdb-checkpoint-wal-io-cuts-{}",
        hawdb_core::generate_uuidv7().unwrap()
    ));
    let mut catalog = Catalog::default();
    let mut store = GraphStore::open(&directory, &mut catalog).unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([("id".into(), Value::Int(1))]),
        )
        .unwrap();
    let source = store.checkpoint_source();
    let source_catalog = catalog.clone();
    for id in 2..=3 {
        store
            .create_node(
                &mut catalog,
                "Memory",
                BTreeMap::from([
                    ("id".into(), Value::Int(id)),
                    ("payload".into(), Value::String("\0界🙂".repeat(30_000))),
                ]),
            )
            .unwrap();
    }
    let identity = store.checkpoint_source_identity();
    let durable = store.durable.as_ref().unwrap();
    let wal_path = durable.wal_path.clone();
    let manifest_path = durable.manifest_path().to_path_buf();
    let wal = std::fs::read(&wal_path).unwrap();
    let manifest = std::fs::read(&manifest_path).unwrap();
    let expected = store
        .node_records_owned()
        .collect::<crate::Result<Vec<_>>>()
        .unwrap();
    let ceiling = 2 * 1024 * 1024;
    let baseline_governor = governor(ceiling);
    let baseline_permit = baseline_governor
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling).with_io_wave_slots(1))
        .unwrap();
    let counter = Arc::new(CountingWaves {
        admitted: baseline_permit.bind_task_context(RuntimeTaskContext::default()),
        calls: AtomicUsize::new(0),
        cancel_at: usize::MAX,
        cancellation: RuntimeCancellationToken::new(),
    });
    let task = counter
        .admitted
        .clone()
        .with_io_wave_controller(counter.clone());
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    candidate.catch_up_with_task_context(&store, &task).unwrap();
    let waves = counter.calls.load(Ordering::SeqCst);
    assert!(
        waves > 10,
        "large framed records must release I/O between bounded writes"
    );
    drop(candidate);
    drop(task);
    drop(counter);
    drop(baseline_permit);
    assert_eq!(baseline_governor.snapshot().admitted_memory_bytes, 0);

    let mut serving = Some(store);
    for stop in 1..=waves {
        let store = serving.as_mut().unwrap();
        let governor = governor(ceiling);
        let permit = governor
            .try_admit(RuntimeWorkRequest::background_maintenance(ceiling).with_io_wave_slots(1))
            .unwrap();
        let cancellation = RuntimeCancellationToken::new();
        let counter = Arc::new(CountingWaves {
            admitted: permit
                .bind_task_context(RuntimeTaskContext::without_deadline(cancellation.clone())),
            calls: AtomicUsize::new(0),
            cancel_at: stop,
            cancellation,
        });
        let task = counter
            .admitted
            .clone()
            .with_io_wave_controller(counter.clone());
        let mut candidate = source
            .prepare_checkpoint_candidate(&source_catalog)
            .unwrap()
            .unwrap();
        let error = candidate
            .catch_up_with_task_context(store, &task)
            .unwrap_err();
        assert!(error.to_string().contains("stopped"), "{error:?}");
        assert_eq!(counter.calls.load(Ordering::SeqCst), stop);
        assert_eq!(governor.snapshot().active_background_io_slots, 0);
        assert_eq!(store.checkpoint_source_identity(), identity);
        assert_eq!(std::fs::read(&wal_path).unwrap(), wal);
        assert_eq!(std::fs::read(&manifest_path).unwrap(), manifest);
        store.ensure_usable().unwrap();
        drop(candidate);
        drop(task);
        drop(counter);

        let fresh = permit.bind_task_context(RuntimeTaskContext::default());
        assert!(
            matches!(fresh.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes == ceiling)
        );
        let mut candidate = source
            .prepare_checkpoint_candidate(&source_catalog)
            .unwrap()
            .unwrap();
        let replay = candidate.catch_up_with_task_context(store, &fresh).unwrap();
        assert_eq!(replay.entries, 2);
        assert_eq!(replay.captured_commit_epoch, store.commit_epoch());
        assert_eq!(
            candidate
                .store
                .as_ref()
                .unwrap()
                .node_records_owned()
                .collect::<crate::Result<Vec<_>>>()
                .unwrap(),
            expected
        );
        let retained_payload_bytes = 2 * "\0界🙂".len() as u64 * 30_000;
        assert!(
            matches!(fresh.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes <= ceiling - retained_payload_bytes)
        );
        assert_eq!(governor.snapshot().admissions, 1);
        if stop == waves {
            store
                .publish_checkpoint_candidate(&mut candidate, None, &Default::default())
                .unwrap();
        }
        drop(candidate);
        if stop == waves {
            // Final publication moved the decoded owners into the serving runtime.
            drop(serving.take());
        }
        assert!(
            matches!(fresh.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes == ceiling)
        );
        drop(fresh);
        drop(permit);
        let closed = governor.snapshot();
        assert_eq!(closed.active_background_io_slots, 0);
        assert_eq!(closed.active_background_tasks, 0);
        assert_eq!(closed.admitted_memory_bytes, 0);
    }
    drop(source);
    drop(serving);
    let recovered = GraphStore::open(&directory, &mut catalog).unwrap();
    assert_eq!(
        recovered
            .node_records_owned()
            .collect::<crate::Result<Vec<_>>>()
            .unwrap(),
        expected
    );
    assert_eq!(recovered.commit_epoch(), 3);
    drop(recovered);
    std::fs::remove_dir_all(directory).unwrap();
}

#[test]
fn checkpoint_units_wal_payload_actual_suffix_denies_unaccounted_overlap_and_fully_retries() {
    let directory = std::env::temp_dir().join(format!(
        "hawdb-checkpoint-wal-payload-{}",
        hawdb_core::generate_uuidv7().unwrap()
    ));
    let mut catalog = Catalog::default();
    let mut store = GraphStore::open(&directory, &mut catalog).unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([("id".into(), Value::Int(1))]),
        )
        .unwrap();
    let source = store.checkpoint_source();
    let source_catalog = catalog.clone();
    let source_identity = source.checkpoint_source_identity();
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([
                ("id".into(), Value::Int(2)),
                ("payload".into(), Value::String("\0界🙂".repeat(30_000))),
            ]),
        )
        .unwrap();
    let identity = store.checkpoint_source_identity();
    let durable = store.durable.as_ref().unwrap();
    let wal_path = durable.wal_path.clone();
    let manifest_path = durable.manifest_path().to_path_buf();
    let wal = std::fs::read(&wal_path).unwrap();
    let manifest = std::fs::read(&manifest_path).unwrap();
    let expected = store
        .node_records_owned()
        .collect::<crate::Result<Vec<_>>>()
        .unwrap();

    let mut cursor = match WalRecordCursor::open(&wal_path, None).unwrap() {
        WalOpenOutcome::Cursor(cursor) => cursor,
        _ => panic!("the complete source WAL must have a valid header"),
    };
    let mut last = None;
    loop {
        match cursor.next().unwrap() {
            WalCursorEvent::Entry { entry, .. } => last = Some(entry),
            WalCursorEvent::Eof => break,
            _ => panic!("complete source must contain only committed records"),
        }
    }
    drop(cursor);
    let payload = encode_binary_wal_record(&last.unwrap(), store.commit_epoch()).unwrap();
    let ordinary = frame_binary_wal_record(
        candidate.prepared.as_ref().unwrap().generation,
        &payload,
        candidate.candidate_wal_bytes - WAL_BINARY_FILE_HEADER_BYTES as u64,
    );
    // The complete framed output fits, but its simultaneously live encoded
    // payload also needs admission. The old encoder leaves that copy uncharged.
    let ceiling = ordinary.len() as u64 + 24;
    let denied = governor(ceiling);
    let permit = denied
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling).with_io_wave_slots(1))
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    let result = candidate.catch_up_with_task_context(&store, &task);
    assert!(
        result.is_err(),
        "the encoded payload and framed suffix must both be admitted before allocation"
    );
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("remaining reservation"));
    assert_eq!(store.checkpoint_source_identity(), identity);
    assert_eq!(std::fs::read(&wal_path).unwrap(), wal);
    assert_eq!(std::fs::read(&manifest_path).unwrap(), manifest);
    store.ensure_usable().unwrap();
    drop(candidate);
    drop(task);
    drop(permit);
    let closed = denied.snapshot();
    assert_eq!(closed.active_background_io_slots, 0);
    assert_eq!(closed.active_cpu_slots, 0);
    assert_eq!(closed.active_background_tasks, 0);
    assert_eq!(closed.admitted_memory_bytes, 0);

    // Recreate the same pinned base and replay the full suffix. Ordinary
    // publication and reopen are the complete-result references.
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    let allowed = governor(2 * 1024 * 1024);
    let permit = allowed
        .try_admit(
            RuntimeWorkRequest::background_maintenance(2 * 1024 * 1024).with_io_wave_slots(1),
        )
        .unwrap();
    let task = permit.bind_task_context(RuntimeTaskContext::default());
    candidate.catch_up_with_task_context(&store, &task).unwrap();
    store
        .publish_checkpoint_candidate(&mut candidate, None, &Default::default())
        .unwrap();
    drop(candidate);
    drop(task);
    drop(permit);
    assert_eq!(allowed.snapshot().admitted_memory_bytes, 2 * 1024 * 1024);
    assert_eq!(source.checkpoint_source_identity(), source_identity);
    drop(source);
    drop(store);
    assert_eq!(allowed.snapshot().admitted_memory_bytes, 0);
    let recovered = GraphStore::open(&directory, &mut catalog).unwrap();
    assert_eq!(
        recovered
            .node_records_owned()
            .collect::<crate::Result<Vec<_>>>()
            .unwrap(),
        expected
    );
    drop(recovered);
    std::fs::remove_dir_all(directory).unwrap();
}

#[derive(Debug)]
struct CursorMemoryWaves {
    admitted: RuntimeTaskContext,
    ceiling: u64,
    available_at_acquire: std::sync::Mutex<Vec<u64>>,
}

impl CursorMemoryWaves {
    fn observe(&self) {
        let available = match self.admitted.reserve_working_memory(self.ceiling) {
            Err(RuntimeMemoryError::ReservationExceeded {
                available_bytes, ..
            }) => available_bytes,
            _ => panic!("an audited full-budget request must expose remaining real reservation"),
        };
        self.available_at_acquire.lock().unwrap().push(available);
    }
}

impl RuntimeIoWaveController for CursorMemoryWaves {
    fn acquire(
        &self,
        slots: NonZeroUsize,
        _task: &RuntimeTaskContext,
    ) -> std::result::Result<Box<dyn RuntimeIoWavePermit>, RuntimeIoWaveError> {
        let permit = self
            .admitted
            .acquire_io_wave(slots)?
            .expect("real governor I/O lease");
        self.observe();
        Ok(permit)
    }

    fn try_acquire(
        &self,
        slots: NonZeroUsize,
        _task: &RuntimeTaskContext,
    ) -> std::result::Result<Option<Box<dyn RuntimeIoWavePermit>>, RuntimeIoWaveError> {
        match self.admitted.try_acquire_io_wave(slots)? {
            RuntimeIoWaveTryAcquire::Acquired(Some(permit)) => {
                self.observe();
                Ok(Some(permit))
            }
            RuntimeIoWaveTryAcquire::Pending => Ok(None),
            RuntimeIoWaveTryAcquire::Acquired(None) => {
                panic!("the memory audit must delegate to a real governor I/O lease")
            }
        }
    }
}

#[test]
fn checkpoint_units_wal_cursor_actual_suffix_admits_read_buffer_before_payload_work() {
    let directory = std::env::temp_dir().join(format!(
        "hawdb-checkpoint-wal-cursor-memory-{}",
        hawdb_core::generate_uuidv7().unwrap()
    ));
    let mut catalog = Catalog::default();
    let mut store = GraphStore::open(&directory, &mut catalog).unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([("id".into(), Value::Int(1))]),
        )
        .unwrap();
    let source = store.checkpoint_source();
    let source_catalog = catalog.clone();
    let source_identity = source.checkpoint_source_identity();
    let mut candidate = source
        .prepare_checkpoint_candidate(&source_catalog)
        .unwrap()
        .unwrap();
    store
        .create_node(
            &mut catalog,
            "Memory",
            BTreeMap::from([
                ("id".into(), Value::Int(2)),
                ("payload".into(), Value::String("\0界🙂".repeat(30_000))),
            ]),
        )
        .unwrap();
    let identity = store.checkpoint_source_identity();
    let durable = store.durable.as_ref().unwrap();
    let wal_path = durable.wal_path.clone();
    let manifest_path = durable.manifest_path().to_path_buf();
    let wal = std::fs::read(&wal_path).unwrap();
    let manifest = std::fs::read(&manifest_path).unwrap();
    let expected = store
        .node_records_owned()
        .collect::<crate::Result<Vec<_>>>()
        .unwrap();
    let ceiling = 2 * 1024 * 1024;
    let governor = governor(ceiling);
    let permit = governor
        .try_admit(RuntimeWorkRequest::background_maintenance(ceiling).with_io_wave_slots(1))
        .unwrap();
    let audit = Arc::new(CursorMemoryWaves {
        admitted: permit.bind_task_context(RuntimeTaskContext::default()),
        ceiling,
        available_at_acquire: std::sync::Mutex::new(Vec::new()),
    });
    let task = audit
        .admitted
        .clone()
        .with_io_wave_controller(audit.clone());
    let replay = candidate.catch_up_with_task_context(&store, &task).unwrap();
    let observed = audit.available_at_acquire.lock().unwrap().clone();
    assert!(
        observed.len() >= 3,
        "the real candidate must open output/source and read a captured record"
    );
    // The third wave is at the captured-record read, before encoding/framing
    // can charge their output. A governor-bound cursor must already own its
    // physical 32 KiB read buffer. Later output reservations cannot mask it.
    assert!(
        observed[2] <= ceiling - crate::wal::frame::WAL_BLOCK_BYTES as u64,
        "the captured WAL read buffer is unaccounted before payload work: {observed:?}"
    );
    assert_eq!(replay.entries, 1);
    assert_eq!(replay.captured_commit_epoch, store.commit_epoch());
    assert_eq!(store.checkpoint_source_identity(), identity);
    assert_eq!(std::fs::read(&wal_path).unwrap(), wal);
    assert_eq!(std::fs::read(&manifest_path).unwrap(), manifest);
    assert_eq!(
        candidate
            .store
            .as_ref()
            .unwrap()
            .node_records_owned()
            .collect::<crate::Result<Vec<_>>>()
            .unwrap(),
        expected
    );
    // Decoded suffix data is now owned by the live candidate. The physical
    // reader and encoding scratch return, but the wide string stays charged.
    let retained_payload_bytes = "\0界🙂".len() as u64 * 30_000;
    assert!(
        matches!(task.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes <= ceiling - retained_payload_bytes)
    );
    store
        .publish_checkpoint_candidate(&mut candidate, None, &Default::default())
        .unwrap();
    drop(candidate);
    // Publication transfers these same data owners into the serving runtime.
    assert!(
        matches!(task.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes <= ceiling - retained_payload_bytes)
    );
    assert_eq!(source.checkpoint_source_identity(), source_identity);
    drop(source);
    drop(store);
    assert!(
        matches!(task.reserve_working_memory(ceiling), Err(RuntimeMemoryError::ReservationExceeded { available_bytes, .. }) if available_bytes == ceiling)
    );
    drop(task);
    drop(audit);
    drop(permit);
    let closed = governor.snapshot();
    assert_eq!(closed.active_background_io_slots, 0);
    assert_eq!(closed.active_cpu_slots, 0);
    assert_eq!(closed.active_background_tasks, 0);
    assert_eq!(closed.admitted_memory_bytes, 0);
    let recovered = GraphStore::open(&directory, &mut catalog).unwrap();
    assert_eq!(
        recovered
            .node_records_owned()
            .collect::<crate::Result<Vec<_>>>()
            .unwrap(),
        expected
    );
    drop(recovered);
    std::fs::remove_dir_all(directory).unwrap();
}