liminal-server 0.3.0

Standalone server for the liminal messaging bus
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
//! R-D1 stage-8 credential-attach capacity and provenance-pruning tests.
//!
//! Each test drives the live dispatch seam with real wire frames over a real
//! on-disk store and pins one register-row refusal of credential attach's
//! exact five-scope receipt/provenance order (row 5662), plus the
//! request-time expiry rules: pruned fingerprints free capacity, an exact
//! old token past its window degrades to `StaleOrUnknownReceipt`, and an
//! unknown old token inside the window keeps the `StaleAuthority` no-commit
//! proof.

use std::error::Error;
use std::thread::sleep;
use std::time::Duration;

use liminal_protocol::wire::{
    AttachSecret, ConnectionIncarnation, ReceiptCapacityExceeded, ReceiptCapacityScope,
    ServerValue, StaleAuthority, StaleOrUnknownReceipt,
};

use super::ProductionParticipantHandler;
use super::tests::{dispatch, open_disk_store_for_tests, test_participant_config};
use super::tests_capacity::capacity_config;
use super::tests_receipts::{GEN_ONE, attach, attach_request, detach, enroll, generation};

/// Asserts one exact credential-attach `ReceiptCapacityExceeded` row.
fn assert_attach_receipt_refusal(
    value: &ServerValue,
    conversation_id: u64,
    scope: ReceiptCapacityScope,
    limit: u64,
    occupied: u64,
) -> Result<(), Box<dyn Error>> {
    let ServerValue::ReceiptCapacityExceeded(ReceiptCapacityExceeded::CredentialAttach {
        request,
        scope: got_scope,
        limit: got_limit,
        occupied: got_occupied,
    }) = value
    else {
        return Err(format!(
            "expected the credential-attach ReceiptCapacityExceeded row ({scope:?}), got: \
             {value:?}"
        )
        .into());
    };
    assert_eq!(request.conversation_id, conversation_id);
    assert_eq!(*got_scope, scope);
    assert_eq!(*got_limit, limit);
    assert_eq!(*got_occupied, occupied);
    Ok(())
}

/// `LiveReceiptServer`: the live enrollment receipt fills the whole server
/// cap, so the first rotation refuses at the first scope in the fixed order.
#[test]
fn attach_live_receipt_server_scope_refusal() -> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let incarnation = ConnectionIncarnation::new(75, 1);
    let store = open_disk_store_for_tests(&data_dir)?;
    let config = capacity_config(|c| c.max_live_attach_receipts_server = 1);
    let handler = ProductionParticipantHandler::new(store, config)?;
    let conversation_id = 741;

    let receipt = enroll(&handler, incarnation, conversation_id, [41; 16])?;
    let participant_id = receipt.participant_id();
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        GEN_ONE,
        [42; 16],
    )?;
    let refused = dispatch(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            GEN_ONE,
            receipt.attach_secret(),
            [43; 16],
        ),
    )?;
    assert_attach_receipt_refusal(
        &refused,
        conversation_id,
        ReceiptCapacityScope::LiveReceiptServer,
        1,
        1,
    )
}

/// `LiveReceiptParticipant`: the participant's own live enrollment receipt
/// fills its per-participant cap, so rotation refuses at the second scope
/// (the server scope has headroom).
#[test]
fn attach_live_receipt_participant_scope_refusal() -> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let incarnation = ConnectionIncarnation::new(76, 1);
    let store = open_disk_store_for_tests(&data_dir)?;
    let config = capacity_config(|c| c.max_live_attach_receipts_per_participant = 1);
    let handler = ProductionParticipantHandler::new(store, config)?;
    let conversation_id = 742;

    let receipt = enroll(&handler, incarnation, conversation_id, [44; 16])?;
    let participant_id = receipt.participant_id();
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        GEN_ONE,
        [45; 16],
    )?;
    let refused = dispatch(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            GEN_ONE,
            receipt.attach_secret(),
            [46; 16],
        ),
    )?;
    assert_attach_receipt_refusal(
        &refused,
        conversation_id,
        ReceiptCapacityScope::LiveReceiptParticipant,
        1,
        1,
    )
}

/// `ProvenanceServer` / `ProvenanceConversation` / `ProvenanceParticipant`:
/// the retained enrollment fingerprint fills each scope's cap in turn; the
/// refusal names exactly the first full scope in the fixed order.
#[test]
fn attach_provenance_scope_refusals_follow_the_fixed_order() -> Result<(), Box<dyn Error>> {
    type Mutator = fn(&mut crate::config::types::ParticipantConfig);
    let cases: [(u64, Mutator, ReceiptCapacityScope); 3] = [
        (
            743,
            |c| c.max_receipt_provenance_server = 1,
            ReceiptCapacityScope::ProvenanceServer,
        ),
        (
            744,
            |c| c.max_receipt_provenance_per_conversation = 1,
            ReceiptCapacityScope::ProvenanceConversation,
        ),
        (
            745,
            |c| c.max_receipt_provenance_per_participant = 1,
            ReceiptCapacityScope::ProvenanceParticipant,
        ),
    ];
    for (conversation_id, mutate, scope) in cases {
        let home = tempfile::tempdir()?;
        let data_dir = home.path().join("durability");
        let incarnation = ConnectionIncarnation::new(77, 1);
        let store = open_disk_store_for_tests(&data_dir)?;
        let handler = ProductionParticipantHandler::new(store, capacity_config(mutate))?;

        let receipt = enroll(&handler, incarnation, conversation_id, [47; 16])?;
        let participant_id = receipt.participant_id();
        detach(
            &handler,
            incarnation,
            conversation_id,
            participant_id,
            GEN_ONE,
            [48; 16],
        )?;
        let refused = dispatch(
            &handler,
            incarnation,
            attach_request(
                conversation_id,
                participant_id,
                GEN_ONE,
                receipt.attach_secret(),
                [49; 16],
            ),
        )?;
        assert_attach_receipt_refusal(&refused, conversation_id, scope, 1, 1)?;
    }
    Ok(())
}

/// Full-cap refusal and the in-window no-commit proof, with LONG windows so
/// no scheduler jitter exists: the participant's two in-window fingerprints
/// (enrollment + first rotation) fill the per-participant cap and the second
/// rotation refuses at `ProvenanceParticipant`; an unknown token at the old
/// generation is provably absent from the complete in-window fingerprint set
/// and keeps the `StaleAuthority` no-commit proof.
#[test]
fn full_provenance_participant_scope_refuses_and_in_window_unknown_is_stale_authority()
-> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let incarnation = ConnectionIncarnation::new(78, 1);
    let store = open_disk_store_for_tests(&data_dir)?;
    // Default long windows (60s receipt / 600s provenance); cap 2 holds the
    // enrollment fingerprint plus one rotation fingerprint.
    let config = capacity_config(|c| c.max_receipt_provenance_per_participant = 2);
    let handler = ProductionParticipantHandler::new(store, config)?;
    let conversation_id = 746;

    let receipt = enroll(&handler, incarnation, conversation_id, [50; 16])?;
    let participant_id = receipt.participant_id();
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        GEN_ONE,
        [51; 16],
    )?;
    let first = attach(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            GEN_ONE,
            receipt.attach_secret(),
            [52; 16],
        ),
    )?;
    assert_eq!(first.capability_generation(), generation(2)?);

    // The participant's in-window fingerprints (enrollment + rotation) fill
    // the cap: the second rotation refuses at ProvenanceParticipant.
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        generation(2)?,
        [53; 16],
    )?;
    let refused = dispatch(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            generation(2)?,
            first.attach_secret(),
            [54; 16],
        ),
    )?;
    assert_attach_receipt_refusal(
        &refused,
        conversation_id,
        ReceiptCapacityScope::ProvenanceParticipant,
        2,
        2,
    )?;

    // IN window: an unknown token at the old generation is provably absent
    // from the complete in-window fingerprint set — StaleAuthority.
    let unknown_in_window = dispatch(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            GEN_ONE,
            AttachSecret::new([0xAB; 32]),
            [55; 16],
        ),
    )?;
    let ServerValue::StaleAuthority(StaleAuthority::Live {
        current_generation, ..
    }) = unknown_in_window
    else {
        return Err(format!(
            "an unknown old token inside the fingerprint window must keep the StaleAuthority \
             no-commit proof, got: {unknown_in_window:?}"
        )
        .into());
    };
    assert_eq!(current_generation, generation(2)?);
    Ok(())
}

/// Out-of-model over-limit refusal with true numbers on the attach arm: two
/// retained enrollment fingerprints, then a restart whose server provenance
/// cap was lowered to 1 BENEATH that durable occupancy. Every earlier scope
/// in the frozen five-scope order has headroom, so the reconnecting attach
/// refuses at `ProvenanceServer` with the lowered limit and the true
/// occupancy — never admitting past the signed cap.
#[test]
fn attach_over_limit_scope_refuses_with_true_numbers() -> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let conversation_id = 748;
    let participant_id;
    let secret;

    {
        let store = open_disk_store_for_tests(&data_dir)?;
        let handler = ProductionParticipantHandler::new(store, test_participant_config())?;
        let incarnation = ConnectionIncarnation::new(82, 1);
        let receipt = enroll(&handler, incarnation, conversation_id, [61; 16])?;
        participant_id = receipt.participant_id();
        secret = receipt.attach_secret();
        enroll(&handler, incarnation, 749, [62; 16])?;
    }

    // RESTART with the server provenance cap lowered beneath the two
    // retained in-window fingerprints; the client reconnects and attaches.
    let store = open_disk_store_for_tests(&data_dir)?;
    let config = capacity_config(|c| c.max_receipt_provenance_server = 1);
    let handler = ProductionParticipantHandler::new(store, config)?;
    let refused = dispatch(
        &handler,
        ConnectionIncarnation::new(82, 2),
        attach_request(conversation_id, participant_id, GEN_ONE, secret, [63; 16]),
    )?;
    assert_attach_receipt_refusal(
        &refused,
        conversation_id,
        ReceiptCapacityScope::ProvenanceServer,
        1,
        2,
    )
}

/// Frozen first-full precedence across the model boundary on the attach
/// arm: `LiveReceiptServer` is exactly full IN model (two live enrollment
/// receipts against a cap of 2) while the LATER `ProvenanceServer` scope is
/// over-limit (cap lowered to 1 beneath 2 retained fingerprints). The
/// contract's suborder says the first full scope answers and no later
/// occupancy is disclosed, so the refusal must name `LiveReceiptServer`
/// with the live-receipt numbers — never the later provenance scope's.
#[test]
fn attach_mixed_full_and_over_limit_refuses_the_earlier_full_scope() -> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let conversation_id = 750;
    let participant_id;
    let secret;

    {
        let store = open_disk_store_for_tests(&data_dir)?;
        let handler = ProductionParticipantHandler::new(store, test_participant_config())?;
        let incarnation = ConnectionIncarnation::new(83, 1);
        let receipt = enroll(&handler, incarnation, conversation_id, [64; 16])?;
        participant_id = receipt.participant_id();
        secret = receipt.attach_secret();
        enroll(&handler, incarnation, 751, [65; 16])?;
    }

    // RESTART: LiveReceiptServer exactly full in model (both enrollment
    // receipts are still inside their 60s window), ProvenanceServer
    // over-limit out of model.
    let store = open_disk_store_for_tests(&data_dir)?;
    let config = capacity_config(|c| {
        c.max_live_attach_receipts_server = 2;
        c.max_receipt_provenance_server = 1;
    });
    let handler = ProductionParticipantHandler::new(store, config)?;
    let refused = dispatch(
        &handler,
        ConnectionIncarnation::new(83, 2),
        attach_request(conversation_id, participant_id, GEN_ONE, secret, [66; 16]),
    )?;
    assert_attach_receipt_refusal(
        &refused,
        conversation_id,
        ReceiptCapacityScope::LiveReceiptServer,
        2,
        2,
    )
}

/// Request-time expiry, late-safe half (sleeping longer only strengthens the
/// preconditions): once every provenance window has passed, the request-time
/// checks prune the retained fingerprints — a rotation that a full cap would
/// otherwise refuse ADMITS, and the exact old token degrades to the
/// intentionally ambiguous `StaleOrUnknownReceipt`, never the false
/// no-commit proof `StaleAuthority`.
#[test]
fn expired_provenance_prunes_freeing_capacity_and_degrading_exact_old_tokens()
-> Result<(), Box<dyn Error>> {
    let home = tempfile::tempdir()?;
    let data_dir = home.path().join("durability");
    let incarnation = ConnectionIncarnation::new(79, 1);
    let store = open_disk_store_for_tests(&data_dir)?;
    // Short windows (1s receipt / 1.2s provenance) waited out below; the cap
    // of 2 would refuse the second rotation if the expired fingerprints were
    // still counted.
    let config = capacity_config(|c| {
        c.attach_receipt_ttl_ms = 1_000;
        c.receipt_provenance_ttl_ms = 1_200;
        c.max_receipt_provenance_per_participant = 2;
    });
    let handler = ProductionParticipantHandler::new(store, config)?;
    let conversation_id = 747;

    let receipt = enroll(&handler, incarnation, conversation_id, [56; 16])?;
    let participant_id = receipt.participant_id();
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        GEN_ONE,
        [57; 16],
    )?;
    let first_request = attach_request(
        conversation_id,
        participant_id,
        GEN_ONE,
        receipt.attach_secret(),
        [58; 16],
    );
    let first = attach(&handler, incarnation, first_request.clone())?;
    assert_eq!(first.capability_generation(), generation(2)?);
    detach(
        &handler,
        incarnation,
        conversation_id,
        participant_id,
        generation(2)?,
        [59; 16],
    )?;

    // Wait out every provenance window; the request-time checks prune the
    // retained fingerprints, freeing the participant's capacity.
    sleep(Duration::from_millis(2_000));
    let second = attach(
        &handler,
        incarnation,
        attach_request(
            conversation_id,
            participant_id,
            generation(2)?,
            first.attach_secret(),
            [60; 16],
        ),
    )?;
    assert_eq!(second.capability_generation(), generation(3)?);

    // The pruned exact old token is now intentionally indistinguishable from
    // an unknown one: StaleOrUnknownReceipt, never StaleAuthority.
    let exact_old = dispatch(&handler, incarnation, first_request)?;
    let ServerValue::StaleOrUnknownReceipt(StaleOrUnknownReceipt {
        presented_generation,
        current_generation,
        ..
    }) = exact_old
    else {
        return Err(format!(
            "a pruned exact old token must answer StaleOrUnknownReceipt, got: {exact_old:?}"
        )
        .into());
    };
    assert_eq!(presented_generation, GEN_ONE);
    assert_eq!(current_generation, generation(3)?);
    Ok(())
}