chio-store-sqlite 0.1.2

SQLite-backed persistence, query, and report implementations for Chio
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
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
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use chio_core::capability::scope::MonetaryAmount;
use chio_kernel::admission_operation::{
    AdmissionAttachment, AdmissionDigest, AdmissionIdentifier, AdmissionOperationBindingInputV1,
    AdmissionOperationBindingV1, AdmissionOperationCommand, AdmissionOperationKind,
    AdmissionOperationState, AdmissionOperationStore, AdmissionOperationV1,
    AdmissionParticipantRequirements, AdmissionRecoveryLease, AdmissionRequestBindingV1,
    AuthenticatedRequestNamespace, ProviderAttemptBindingV1, QualifiedAdmissionOperationStoreExt,
    SideEffectClass, StoreMutationFence,
};
use chio_kernel::tool_outcome::test_support::{
    prepared_evaluation, record_external_step, record_pure_step, resolve_with_blob, returned_value,
};
use chio_kernel::tool_outcome::{
    CanonicalResolvedOutputBlobV1, SettlementDispositionV1, ToolOutcomeInsertResultV1,
    ToolOutcomeStore, ToolOutcomeStoreError,
};
use tempfile::TempDir;

use super::*;
use crate::{SqliteAdmissionOperationStore, SqliteAuthorityStore};

struct Fixture {
    _temp: TempDir,
    database: PathBuf,
    lock_root: PathBuf,
    authority: SqliteAuthorityStore,
    operations: SqliteAdmissionOperationStore,
    outcomes: SqliteToolOutcomeStore,
    fence: StoreMutationFence,
}

fn fixture() -> Fixture {
    let temp = tempfile::tempdir().expect("tempdir");
    secure_temp_directory(temp.path());
    let database = temp.path().join("authority.db");
    let lock_root = temp.path().join("locks");
    fs::create_dir(&lock_root).expect("create lock root");
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&lock_root, std::fs::Permissions::from_mode(0o700))
            .expect("secure directory");
    }
    SqliteAuthorityStore::provision(&database, &lock_root).expect("provision authority");
    let authority =
        SqliteAuthorityStore::open_serving(&database, &lock_root).expect("open authority");
    let fence = authority.mutation_fence();
    let operations = authority.admission_operation_store();
    let outcomes = authority.tool_outcome_store();
    Fixture {
        _temp: temp,
        database,
        lock_root,
        authority,
        operations,
        outcomes,
        fence,
    }
}

fn now_ms() -> u64 {
    u64::try_from(
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock")
            .as_millis(),
    )
    .expect("millisecond clock")
}

fn id(field: &'static str, value: &str) -> AdmissionIdentifier {
    AdmissionIdentifier::try_new(field, value).expect("valid identifier")
}

fn digest(field: &'static str, byte: char) -> AdmissionDigest {
    AdmissionDigest::try_new(field, byte.to_string().repeat(64)).expect("valid digest")
}

fn prepared(fence: &StoreMutationFence, request_id: &str) -> AdmissionOperationV1 {
    let namespace = AuthenticatedRequestNamespace::for_local_system(id(
        "coordinator_authority_id",
        "tool-outcome-test-authority",
    ))
    .expect("request namespace");
    let requirements = AdmissionParticipantRequirements {
        broker_attempt: true,
        budget_capture: true,
        ..AdmissionParticipantRequirements::NONE
    };
    let binding = AdmissionOperationBindingV1::new(AdmissionOperationBindingInputV1 {
        kind: AdmissionOperationKind::ToolDispatch,
        namespace,
        request_id: id("request_id", request_id),
        capability_id: id("capability_id", "tool-outcome-capability"),
        authorization_capability_hash: digest("authorization_capability_hash", 'a'),
        request_binding: AdmissionRequestBindingV1::new(
            digest("immutable_request_hash", 'b'),
            requirements,
        )
        .expect("request binding"),
        policy_hash: digest("policy_hash", 'c'),
        effect_class: SideEffectClass::SideEffecting,
    })
    .expect("operation binding");
    AdmissionOperationV1::prepare(binding, fence.owner_epoch).expect("prepared operation")
}

fn claim(fixture: &Fixture, operation: &AdmissionOperationV1, at: u64) -> AdmissionRecoveryLease {
    fixture
        .operations
        .claim_recovery(
            operation.binding().operation_id(),
            operation.version(),
            &id("claimant_id", "tool-outcome-worker"),
            at,
            at + 60_000,
            &fixture.fence,
        )
        .expect("claim operation")
}

fn advance(
    fixture: &Fixture,
    operation: AdmissionOperationV1,
    next: AdmissionOperationState,
    attachments: Vec<AdmissionAttachment>,
    at: u64,
) -> AdmissionOperationV1 {
    let lease = claim(fixture, &operation, at);
    let command = AdmissionOperationCommand::new(
        operation.binding().operation_id().clone(),
        operation.version(),
        lease,
        attachments,
        Some(next),
        None,
        None,
    )
    .expect("operation command");
    fixture
        .operations
        .compare_and_swap(&command, at + 1)
        .expect("advance operation")
        .into_operation()
}

fn committed(fixture: &Fixture, request_id: &str, begun_at: u64) -> AdmissionOperationV1 {
    let mut operation = prepared(&fixture.fence, request_id);
    fixture
        .operations
        .begin(&operation, &fixture.fence, begun_at)
        .expect("begin operation");
    let attempt = ProviderAttemptBindingV1 {
        operation_id: operation.binding().operation_id().as_str().to_owned(),
        attempt_id: format!("attempt:{}", operation.binding().operation_id().as_str()),
        transport_id: "tool-outcome-test-transport".to_owned(),
        transport_key_epoch: fixture.fence.owner_epoch,
    };
    let transitions = [
        (
            AdmissionOperationState::BrokerAttemptRegistered,
            vec![AdmissionAttachment::BrokerAttempt(attempt)],
        ),
        (
            AdmissionOperationState::BudgetAuthorized,
            vec![AdmissionAttachment::BudgetHoldId(id(
                "budget_hold_id",
                "tool-outcome-test-hold",
            ))],
        ),
        (AdmissionOperationState::ReadyToDispatch, Vec::new()),
        (AdmissionOperationState::CapturePending, Vec::new()),
        (AdmissionOperationState::DispatchCommitted, Vec::new()),
    ];
    for (index, (next, attachments)) in transitions.into_iter().enumerate() {
        operation = advance(
            fixture,
            operation,
            next,
            attachments,
            begun_at + 1 + u64::try_from(index).expect("transition index") * 2,
        );
    }
    operation
}

fn record_return(
    fixture: &Fixture,
    operation: &AdmissionOperationV1,
    at: u64,
) -> (
    AdmissionOperationV1,
    chio_kernel::tool_outcome::ToolOutcomeRecordV1,
) {
    let (blob, outcome) = returned_value(
        operation,
        fixture.fence.clone(),
        at,
        serde_json::json!({"completed": true}),
        Some(MonetaryAmount {
            units: 25,
            currency: "USD".to_owned(),
        }),
    )
    .expect("returned outcome");
    let lease = claim(fixture, operation, at);
    let inserted = fixture
        .outcomes
        .record_tool_returned(operation, &lease, &blob, &outcome, &fixture.fence, at + 1)
        .expect("record tool return");
    let (stored, finalizing) = inserted.into_parts();
    (finalizing, stored)
}

#[test]
fn tool_return_atomically_persists_blob_and_advances_operation() {
    let fixture = fixture();
    let begun_at = now_ms();
    let operation = committed(&fixture, "atomic-return", begun_at);
    let at = begun_at + 20;
    let (blob, outcome) = returned_value(
        &operation,
        fixture.fence.clone(),
        at,
        serde_json::json!({"completed": true}),
        None,
    )
    .expect("returned outcome");
    let mut stale_fence = fixture.fence.clone();
    stale_fence.owner_epoch += 1;
    let lease = claim(&fixture, &operation, at);
    assert_eq!(
        fixture.outcomes.record_tool_returned(
            &operation,
            &lease,
            &blob,
            &outcome,
            &stale_fence,
            at + 1,
        ),
        Err(ToolOutcomeStoreError::Fenced)
    );
    let counts: (i64, i64) = fixture
        .outcomes
        .connection()
        .expect("connection")
        .query_row(
            "SELECT (SELECT COUNT(*) FROM tool_outcome_blobs), (SELECT COUNT(*) FROM tool_outcomes)",
            [],
            |row| Ok((row.get(0)?, row.get(1)?)),
        )
        .expect("outcome counts");
    assert_eq!(counts, (0, 0));

    let inserted = fixture
        .outcomes
        .record_tool_returned(&operation, &lease, &blob, &outcome, &fixture.fence, at + 1)
        .expect("atomic return commit");
    let ToolOutcomeInsertResultV1::Inserted {
        outcome: stored,
        operation: finalizing,
    } = inserted
    else {
        panic!("first return must insert");
    };
    assert_eq!(stored, outcome);
    assert_eq!(finalizing.state(), AdmissionOperationState::Finalizing);
    assert_eq!(finalizing.tool_outcome_id(), Some(outcome.outcome_id()));
    assert_eq!(
        fixture
            .outcomes
            .load_raw_invocation_by_operation(operation.binding().operation_id())
            .expect("load raw outcome")
            .expect("raw outcome")
            .canonical_blob()
            .expect("canonical raw blob"),
        blob
    );
    assert_eq!(
        fixture
            .operations
            .load_by_operation_id(operation.binding().operation_id())
            .expect("load operation"),
        Some(finalizing)
    );
}

#[test]
fn post_return_evaluation_is_fenced_staged_and_finalized_by_cas() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "evaluation-journal", begun_at);
    let (operation, outcome) = record_return(&fixture, &committed, begun_at + 20);
    let prepared =
        prepared_evaluation(&operation, &outcome, begun_at + 22).expect("prepared evaluation");
    let lease = claim(&fixture, &operation, begun_at + 22);
    assert_eq!(
        fixture
            .outcomes
            .begin_post_return_evaluation(&lease, &prepared, &fixture.fence, begun_at + 23,)
            .expect("begin evaluation"),
        prepared
    );
    let pure = record_pure_step(&prepared).expect("pure result");
    assert_eq!(
        fixture
            .outcomes
            .stage_post_return_evaluation(
                operation.binding().operation_id(),
                prepared.version(),
                &lease,
                &pure,
                &fixture.fence,
                begun_at + 24,
            )
            .expect("stage pure result"),
        pure
    );
    assert_eq!(
        fixture.outcomes.stage_post_return_evaluation(
            operation.binding().operation_id(),
            prepared.version(),
            &lease,
            &pure,
            &fixture.fence,
            begun_at + 25,
        ),
        Err(ToolOutcomeStoreError::CasConflict)
    );
    let external = record_external_step(&pure, begun_at + 25).expect("external result");
    fixture
        .outcomes
        .stage_post_return_evaluation(
            operation.binding().operation_id(),
            pure.version(),
            &lease,
            &external,
            &fixture.fence,
            begun_at + 25,
        )
        .expect("stage external result");
    let (terminal_evaluation, terminal_outcome, resolved_blob) =
        resolve_with_blob(&outcome, &external, SettlementDispositionV1::NotApplicable)
            .expect("terminal records");
    assert!(matches!(
        fixture.outcomes.finalize_post_return(
            operation.binding().operation_id(),
            external.version(),
            &lease,
            &terminal_evaluation,
            outcome.version(),
            &terminal_outcome,
            None,
            &fixture.fence,
            begun_at + 26,
        ),
        Err(ToolOutcomeStoreError::Invariant(_))
    ));
    let substituted_blob =
        CanonicalResolvedOutputBlobV1::from_signing_preimage(b"substitute".to_vec())
            .expect("substituted blob");
    assert!(matches!(
        fixture.outcomes.finalize_post_return(
            operation.binding().operation_id(),
            external.version(),
            &lease,
            &terminal_evaluation,
            outcome.version(),
            &terminal_outcome,
            Some(&substituted_blob),
            &fixture.fence,
            begun_at + 26,
        ),
        Err(ToolOutcomeStoreError::Invariant(_))
    ));
    assert_eq!(
        fixture
            .outcomes
            .lookup_post_return_evaluation(operation.binding().operation_id())
            .expect("lookup uncommitted evaluation"),
        Some(external.clone())
    );
    assert_eq!(
        fixture
            .outcomes
            .lookup_by_operation(operation.binding().operation_id())
            .expect("lookup uncommitted outcome"),
        Some(outcome.clone())
    );
    assert_eq!(
        fixture
            .outcomes
            .load_resolved_output_by_operation(operation.binding().operation_id())
            .expect("lookup uncommitted resolved output"),
        None
    );
    let finalized = fixture
        .outcomes
        .finalize_post_return(
            operation.binding().operation_id(),
            external.version(),
            &lease,
            &terminal_evaluation,
            outcome.version(),
            &terminal_outcome,
            Some(&resolved_blob),
            &fixture.fence,
            begun_at + 26,
        )
        .expect("finalize evaluation and outcome");
    assert_eq!(
        finalized,
        (terminal_evaluation.clone(), terminal_outcome.clone())
    );
    assert_eq!(
        fixture
            .outcomes
            .lookup_post_return_evaluation(operation.binding().operation_id())
            .expect("lookup evaluation"),
        Some(terminal_evaluation)
    );
    assert_eq!(
        fixture
            .outcomes
            .lookup_by_operation(operation.binding().operation_id())
            .expect("lookup outcome"),
        Some(terminal_outcome)
    );
    assert_eq!(
        fixture
            .outcomes
            .load_resolved_output_by_operation(operation.binding().operation_id())
            .expect("lookup resolved output"),
        Some(resolved_blob)
    );
}

#[test]
fn outcome_journal_survives_owner_rotation_and_detects_tampering() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "outcome-restart", begun_at);
    let (operation, outcome) = record_return(&fixture, &committed, begun_at + 20);
    let operation_id = operation.binding().operation_id().clone();
    let database = fixture.database.clone();
    let lock_root = fixture.lock_root.clone();
    let Fixture {
        _temp,
        authority,
        operations,
        outcomes,
        ..
    } = fixture;
    drop(outcomes);
    drop(operations);
    drop(authority);

    let reopened = SqliteAuthorityStore::open_serving(&database, &lock_root)
        .expect("reopen outcome authority");
    assert_eq!(
        reopened
            .tool_outcome_store()
            .lookup_by_operation(&operation_id)
            .expect("lookup after reopen"),
        Some(outcome)
    );
    drop(reopened);

    let connection = Connection::open(&database).expect("open database for tamper");
    connection
        .execute_batch(
            "UPDATE tool_outcomes
             SET participant_digest =
                    '0000000000000000000000000000000000000000000000000000000000000000',
                 outcome_version = outcome_version + 1,
                 store_uuid = (SELECT store_uuid FROM chio_serving_owner WHERE singleton = 1),
                 store_lease_id = (SELECT lease_id FROM chio_serving_owner WHERE singleton = 1),
                 store_owner_epoch = (SELECT owner_epoch FROM chio_serving_owner WHERE singleton = 1);",
        )
        .expect("tamper outcome commitment");
    drop(connection);
    assert!(SqliteAuthorityStore::open_serving(&database, &lock_root).is_err());
    drop(_temp);
}

fn mark_operation_terminal(fixture: &Fixture, operation_id: &AdmissionOperationId) {
    // Reaching a terminal state through the real API requires the signed
    // terminal-projection machinery; this isolates the compaction gate, which
    // reads only `admission_operations.terminal`. The version bump keeps the
    // `admission_operations_versioned_body` trigger satisfied.
    let connection = fixture.outcomes.connection().expect("connection");
    let changed = connection
        .execute(
            "UPDATE admission_operations
             SET terminal = 1, version = version + 1
             WHERE operation_id = ?1",
            [operation_id.as_str()],
        )
        .expect("mark operation terminal");
    assert_eq!(changed, 1);
}

fn blob_state(fixture: &Fixture, digest: &str) -> (i64, bool) {
    let connection = fixture.outcomes.connection().expect("connection");
    connection
        .query_row(
            "SELECT blob_size_bytes, canonical_bytes IS NOT NULL
             FROM tool_outcome_blobs WHERE digest = ?1",
            [digest],
            |row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)? != 0)),
        )
        .expect("blob row")
}

#[test]
fn compaction_clears_a_terminal_blob_and_reports_compacted_reads() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "compaction-terminal", begun_at);
    let (operation, outcome) = record_return(&fixture, &committed, begun_at + 20);
    let operation_id = operation.binding().operation_id().clone();
    let digest = outcome.raw_output_digest().as_str().to_owned();

    let (size_before, present_before) = blob_state(&fixture, &digest);
    assert!(present_before, "blob payload is present before compaction");

    mark_operation_terminal(&fixture, &operation_id);
    let summary = fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at + 100, &fixture.fence, begun_at + 200)
        .expect("compact terminal blob");
    assert_eq!(summary.compacted, 1);
    assert_eq!(summary.retained_live, 0);

    let (size_after, present_after) = blob_state(&fixture, &digest);
    assert!(!present_after, "blob payload is cleared after compaction");
    assert_eq!(size_after, size_before, "blob size is retained");

    let error = fixture
        .outcomes
        .load_raw_invocation_by_operation(&operation_id)
        .expect_err("a compacted raw invocation must not load silently");
    assert!(
        matches!(&error, ToolOutcomeStoreError::Invariant(message) if message.contains("compacted")),
        "compacted read reports a defined error, got {error:?}"
    );

    assert_eq!(
        fixture
            .outcomes
            .lookup_by_operation(&operation_id)
            .expect("outcome record survives compaction"),
        Some(outcome)
    );

    // A second pass is idempotent: the payload is already cleared.
    let repeat = fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at + 100, &fixture.fence, begun_at + 201)
        .expect("second compaction pass");
    assert_eq!(repeat.compacted, 0);
    assert_eq!(repeat.retained_live, 0);
}

#[test]
fn reinserting_verified_bytes_rehydrates_a_compacted_blob() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "compaction-rehydrate", begun_at);
    let at = begun_at + 20;
    let (blob, outcome) = returned_value(
        &committed,
        fixture.fence.clone(),
        at,
        serde_json::json!({"completed": true}),
        None,
    )
    .expect("returned outcome");
    let bytes = blob.bytes().to_vec();
    let lease = claim(&fixture, &committed, at);
    let inserted = fixture
        .outcomes
        .record_tool_returned(&committed, &lease, &blob, &outcome, &fixture.fence, at + 1)
        .expect("record tool return");
    let (_, finalizing) = inserted.into_parts();
    let operation_id = finalizing.binding().operation_id().clone();
    let digest = outcome.raw_output_digest().as_str().to_owned();

    mark_operation_terminal(&fixture, &operation_id);
    fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at + 100, &fixture.fence, begun_at + 200)
        .expect("compact terminal blob");
    assert!(!blob_state(&fixture, &digest).1, "blob starts compacted");

    let rehydrated_at = begun_at + 300;
    let mut connection = fixture.outcomes.connection().expect("connection");
    let transaction = fixture
        .outcomes
        .begin_write(&mut connection, &fixture.fence, rehydrated_at)
        .expect("begin rehydration write");
    insert_blob_bytes_tx(&transaction, &digest, &bytes, &fixture.fence, rehydrated_at)
        .expect("rehydrate verified bytes");
    fixture
        .outcomes
        .commit_write(transaction)
        .expect("commit rehydration");
    drop(connection);

    assert!(blob_state(&fixture, &digest).1, "blob payload is restored");
    assert!(
        fixture
            .outcomes
            .load_raw_invocation_by_operation(&operation_id)
            .expect("load rehydrated invocation")
            .is_some(),
        "the newly supplied canonical bytes can be loaded"
    );
}

#[test]
fn compaction_is_refused_while_an_owning_operation_is_live() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "compaction-live", begun_at);
    let (operation, _outcome) = record_return(&fixture, &committed, begun_at + 20);
    let operation_id = operation.binding().operation_id().clone();
    let digest = fixture
        .outcomes
        .lookup_by_operation(&operation_id)
        .expect("lookup outcome")
        .expect("outcome present")
        .raw_output_digest()
        .as_str()
        .to_owned();

    let summary = fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at + 100, &fixture.fence, begun_at + 200)
        .expect("compaction pass over a live operation");
    assert_eq!(summary.compacted, 0, "a live operation is never compacted");
    assert_eq!(summary.retained_live, 1);

    let (_size, present) = blob_state(&fixture, &digest);
    assert!(present, "the live operation keeps its raw payload");
    assert!(
        fixture
            .outcomes
            .load_raw_invocation_by_operation(&operation_id)
            .expect("load raw invocation")
            .is_some(),
        "the raw invocation still reads back while the operation is live"
    );
}

#[test]
fn compaction_respects_the_retention_cutoff() {
    let fixture = fixture();
    let begun_at = now_ms();
    let committed = committed(&fixture, "compaction-cutoff", begun_at);
    let (operation, outcome) = record_return(&fixture, &committed, begun_at + 20);
    let operation_id = operation.binding().operation_id().clone();
    let digest = outcome.raw_output_digest().as_str().to_owned();
    mark_operation_terminal(&fixture, &operation_id);

    // The blob is recorded at `begun_at + 21`; a cutoff before it retains it.
    let early = fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at, &fixture.fence, begun_at + 200)
        .expect("compaction below the cutoff");
    assert_eq!(
        early.compacted, 0,
        "a payload newer than the cutoff is kept"
    );
    assert_eq!(early.retained_live, 0);
    assert!(
        blob_state(&fixture, &digest).1,
        "payload retained below cutoff"
    );

    let due = fixture
        .outcomes
        .compact_retained_invocation_blobs(begun_at + 100, &fixture.fence, begun_at + 201)
        .expect("compaction at the cutoff");
    assert_eq!(due.compacted, 1, "a payload past the cutoff is compacted");
    assert!(
        !blob_state(&fixture, &digest).1,
        "payload cleared past cutoff"
    );
}

fn secure_temp_directory(path: &std::path::Path) {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
            .expect("secure temp directory");
    }
    #[cfg(not(unix))]
    let _ = path;
}