khive-runtime 0.10.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
//! Runtime-owned sender transport persistence (ADR-105).
//!
//! Call these methods on the runtime bound to comm's assigned backend, exactly
//! as for ordinary comm note operations. Routing happens before this boundary;
//! the adapter never opens a database or supplies SQL. Receipt signatures are
//! checked against the owner's pinned recipient key before durable state changes.
use crate::error::RuntimeResult;
use crate::{KhiveRuntime, NamespaceToken};
use khive_channel::VerifiedRecipientReceipt;
use khive_db::stores::note::transport::SenderTransportStore;
pub use khive_db::stores::note::transport::{
    EnvelopeKey, FailureClass, HoldReason, PolicyMode, SenderAssurance, SenderEnvelope,
    SenderRecord, TransportState,
};

/// Sender-local delivery status, including the absence of a transport record.
/// Holds retain `Pending`; `Unknown` is never persisted as a transport state.
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum TransportStatus {
    Pending,
    RecipientStored,
    RecipientQuarantined,
    Failed,
    Unknown,
}

impl KhiveRuntime {
    fn sender_transport_store(&self) -> SenderTransportStore {
        SenderTransportStore::new(self.backend().pool_arc())
    }
    /// Read transport status in the token's primary namespace on the bound backend.
    /// This does not infer the internal inbound sibling or mutate retry metadata.
    pub async fn sender_transport_status(
        &self,
        token: &NamespaceToken,
        outbound_note_id: uuid::Uuid,
    ) -> RuntimeResult<TransportStatus> {
        let Some(row) = self
            .sender_transport_store()
            .get_by_outbound_note_id(token.namespace().as_str(), outbound_note_id)
            .await?
        else {
            return Ok(TransportStatus::Unknown);
        };
        if row.hold_reason.is_some() {
            return Ok(TransportStatus::Pending);
        }
        Ok(match row.state {
            TransportState::Pending => TransportStatus::Pending,
            TransportState::RecipientStored => TransportStatus::RecipientStored,
            TransportState::RecipientQuarantined => TransportStatus::RecipientQuarantined,
            TransportState::Failed => TransportStatus::Failed,
        })
    }
    /// Persist immutable bytes before first submission. An exact retry is a no-op;
    /// a different envelope with the same identity is refused. The namespace is
    /// caller attribution and is always taken from the token. Sender assurance
    /// is claimed until authenticated verification is available.
    pub async fn create_sender_transport(
        &self,
        token: &NamespaceToken,
        mut envelope: SenderEnvelope,
    ) -> RuntimeResult<SenderRecord> {
        envelope.namespace = token.namespace().as_str().to_owned();
        envelope.sender_assurance = SenderAssurance::Claimed;
        Ok(self
            .sender_transport_store()
            .create(envelope, false)
            .await?)
    }
    /// Re-encrypt only after the caller confirms the recipient key change. The
    /// old record must be on a key-change hold, and the new epoch must increase.
    /// Cryptographic construction and directory confirmation belong to the caller.
    /// Caller-supplied assurance is normalized to claimed as on initial creation.
    pub async fn reencrypt_sender_transport_after_confirmed_key_change(
        &self,
        token: &NamespaceToken,
        mut envelope: SenderEnvelope,
    ) -> RuntimeResult<SenderRecord> {
        envelope.namespace = token.namespace().as_str().to_owned();
        envelope.sender_assurance = SenderAssurance::Claimed;
        Ok(self.sender_transport_store().create(envelope, true).await?)
    }
    /// Read by durable identity. Absence means unknown; unknown is not persisted.
    pub async fn sender_transport(&self, key: EnvelopeKey) -> RuntimeResult<Option<SenderRecord>> {
        Ok(self.sender_transport_store().get(key).await?)
    }
    /// Due unheld submissions for the exact configured kind and slug. Times use
    /// Unix microseconds; choosing the backoff and jitter is the caller's job.
    pub async fn pending_sender_transports(
        &self,
        token: &NamespaceToken,
        kind: &str,
        slug: &str,
        now: i64,
        limit: u32,
    ) -> RuntimeResult<Vec<SenderRecord>> {
        Ok(self
            .sender_transport_store()
            .list_pending(token.namespace().as_str(), kind, slug, now, limit)
            .await?)
    }
    /// Record service admission time and schedule resubmission 600 seconds later.
    /// The timestamp is stored as Unix microseconds and survives restarts.
    pub async fn record_sender_transport_admission(
        &self,
        key: EnvelopeKey,
        admitted_at: chrono::DateTime<chrono::Utc>,
    ) -> RuntimeResult<()> {
        Ok(self
            .sender_transport_store()
            .record_admission(key, admitted_at.timestamp_micros())
            .await?)
    }
    /// Record a failed attempt. Authentication errors remain pending and do not
    /// advance the attempt count. This never modifies the envelope bytes.
    pub async fn record_sender_transport_failure(
        &self,
        key: EnvelopeKey,
        class: FailureClass,
        next_retry_at: Option<i64>,
    ) -> RuntimeResult<()> {
        Ok(self
            .sender_transport_store()
            .record_failure(key, class, next_retry_at)
            .await?)
    }
    /// Set/release credit or policy holds, or set a key-change hold. A policy hold
    /// carries the evaluated mode and revision. Key-change holds can only be
    /// superseded through explicit confirmed re-encryption.
    pub async fn hold_sender_transport(
        &self,
        key: EnvelopeKey,
        reason: Option<HoldReason>,
    ) -> RuntimeResult<()> {
        Ok(self.sender_transport_store().hold(key, reason).await?)
    }
    /// Accept an already signature-verified recipient receipt, including after a
    /// local failure. Every known binding field and target disposition must match
    /// durable data. This method performs no signature verification itself.
    ///
    /// An unverified payload cannot cross this boundary:
    ///
    /// ```compile_fail
    /// use khive_channel::DeliveryReceipt;
    /// use khive_runtime::{KhiveRuntime, comm_transport::{EnvelopeKey, TransportState}};
    /// async fn accept(runtime: &KhiveRuntime, key: EnvelopeKey, receipt: DeliveryReceipt) {
    ///     runtime.accept_verified_recipient_receipt(key, TransportState::RecipientStored, receipt)
    ///         .await.unwrap();
    /// }
    /// ```
    ///
    /// Its positive twin uses the channel constructor and the same runtime method:
    ///
    /// ```
    /// use khive_channel::{DeliveryReceipt, VerifiedRecipientReceipt};
    /// use khive_runtime::{KhiveRuntime, comm_transport::{EnvelopeKey, TransportState}};
    /// async fn accept(runtime: &KhiveRuntime, key: EnvelopeKey, receipt: DeliveryReceipt,
    ///     pinned_key: &[u8; 32]) {
    ///     let verified = VerifiedRecipientReceipt::verify(receipt, pinned_key).unwrap();
    ///     runtime.accept_verified_recipient_receipt(key, TransportState::RecipientStored, verified)
    ///         .await.unwrap();
    /// }
    /// ```
    pub async fn accept_verified_recipient_receipt(
        &self,
        key: EnvelopeKey,
        state: TransportState,
        receipt: VerifiedRecipientReceipt,
    ) -> RuntimeResult<()> {
        let receipt = serde_json::to_value(receipt.receipt()).map_err(|error| {
            crate::RuntimeError::InvalidInput(format!("invalid receipt: {error}"))
        })?;
        Ok(self
            .sender_transport_store()
            .accept_receipt(key, state, receipt)
            .await?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use khive_channel::{
        receipt_signing_input, DeliveryReceipt, DeliveryReceiptBinding, ReceiptDisposition,
    };
    use ring::signature::{Ed25519KeyPair, KeyPair};
    use uuid::Uuid;

    fn signed_receipt(
        mut receipt: DeliveryReceipt,
        seed: &[u8; 32],
    ) -> (DeliveryReceipt, [u8; 32]) {
        let key_pair = Ed25519KeyPair::from_seed_unchecked(seed).unwrap();
        let input = receipt_signing_input(&receipt).unwrap();
        receipt.signature = key_pair.sign(&input).as_ref().to_vec();
        let pinned_key = key_pair.public_key().as_ref().try_into().unwrap();
        (receipt, pinned_key)
    }

    fn envelope() -> SenderEnvelope {
        SenderEnvelope {
            namespace: "untrusted-envelope-attribution".into(),
            logical_message_id: Uuid::new_v4(),
            outbound_note_id: Uuid::new_v4(),
            kind: "khive".into(),
            slug: "device".into(),
            credential_ref: "keys/device".into(),
            recipient_address: format!("khive1:example/{}", Uuid::nil()),
            protocol_version: 1,
            sender_agent_id: Uuid::new_v4().to_string(),
            sender_assurance: SenderAssurance::DaemonBearer,
            recipient_agent_id: Uuid::nil().to_string(),
            recipient_device_id: Uuid::new_v4(),
            recipient_key_epoch: 1,
            contact_generation: 1,
            sender_key_epoch: 1,
            recipient_key_fingerprint: "ab".repeat(32),
            enc: vec![1; 32],
            ciphertext: vec![2, 0, 255],
        }
    }

    #[tokio::test]
    async fn verified_receipt_uses_bound_backend_and_token_attribution() {
        let runtime = KhiveRuntime::memory().unwrap();
        let unrelated = KhiveRuntime::memory().unwrap();
        let token = NamespaceToken::local();
        let envelope = envelope();
        let key = envelope.key();
        let stored = runtime
            .create_sender_transport(&token, envelope.clone())
            .await
            .unwrap();
        assert_eq!(stored.envelope.namespace, token.namespace().as_str());
        assert_eq!(stored.envelope.sender_assurance, SenderAssurance::Claimed);
        assert_eq!(
            runtime
                .sender_transport(key)
                .await
                .unwrap()
                .unwrap()
                .envelope
                .sender_assurance,
            SenderAssurance::Claimed
        );
        assert!(
            unrelated.sender_transport(key).await.unwrap().is_none(),
            "transport must use bound backend"
        );
        let hold = HoldReason::PolicyDenied {
            mode: PolicyMode::Enforce,
            revision: 7,
        };
        let admitted_at = chrono::Utc::now();
        runtime
            .record_sender_transport_admission(key, admitted_at)
            .await
            .unwrap();
        let admitted = runtime.sender_transport(key).await.unwrap().unwrap();
        assert_eq!(admitted.admitted_at, Some(admitted_at.timestamp_micros()));
        assert_eq!(
            admitted.next_retry_at,
            Some(admitted_at.timestamp_micros() + 600_000_000)
        );
        runtime
            .hold_sender_transport(key, Some(hold))
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport(key)
                .await
                .unwrap()
                .unwrap()
                .hold_reason,
            Some(hold)
        );
        let receipt = DeliveryReceipt {
            binding: DeliveryReceiptBinding {
                protocol_version: 1,
                logical_message_id: envelope.logical_message_id,
                sender_agent_id: envelope.sender_agent_id,
                recipient_agent_id: envelope.recipient_agent_id,
                recipient_device_id: envelope.recipient_device_id,
                recipient_key_epoch: 1,
                contact_generation: 1,
                delivery_attempt_id: Uuid::new_v4(),
            },
            disposition: ReceiptDisposition::Stored,
            signature: vec![7],
        };
        let (receipt, pinned_key) = signed_receipt(receipt, &[19; 32]);
        assert!(runtime
            .accept_verified_recipient_receipt(
                key,
                TransportState::RecipientQuarantined,
                VerifiedRecipientReceipt::verify(receipt.clone(), &pinned_key).unwrap()
            )
            .await
            .is_err());
        runtime
            .accept_verified_recipient_receipt(
                key,
                TransportState::RecipientStored,
                VerifiedRecipientReceipt::verify(receipt, &pinned_key).unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport(key)
                .await
                .unwrap()
                .unwrap()
                .hold_reason,
            None
        );
        assert_eq!(
            runtime.sender_transport(key).await.unwrap().unwrap().state,
            TransportState::RecipientStored
        );
    }
    #[tokio::test]
    async fn caller_sender_assurance_is_claimed() {
        for assurance in [
            SenderAssurance::DaemonBearer,
            SenderAssurance::ActorSignature,
        ] {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let mut envelope = envelope();
            envelope.sender_assurance = assurance;
            let first = runtime
                .create_sender_transport(&token, envelope.clone())
                .await
                .unwrap();
            assert_eq!(
                first.envelope.sender_assurance,
                SenderAssurance::Claimed,
                "create must normalize caller assurance"
            );
            assert_eq!(
                runtime.sender_transport(envelope.key()).await.unwrap(),
                Some(first)
            );
            runtime
                .hold_sender_transport(envelope.key(), Some(HoldReason::RecipientKeyChanged))
                .await
                .unwrap();
            envelope.recipient_key_epoch += 1;
            let next = runtime
                .reencrypt_sender_transport_after_confirmed_key_change(&token, envelope.clone())
                .await
                .expect("re-encryption must normalize caller assurance");
            assert_eq!(next.envelope.sender_assurance, SenderAssurance::Claimed);
            assert_eq!(
                runtime.sender_transport(envelope.key()).await.unwrap(),
                Some(next)
            );
        }
    }

    async fn accept_status_receipt(
        runtime: &KhiveRuntime,
        envelope: &SenderEnvelope,
        disposition: ReceiptDisposition,
    ) {
        let state = match disposition {
            ReceiptDisposition::Stored => TransportState::RecipientStored,
            ReceiptDisposition::Quarantined => TransportState::RecipientQuarantined,
        };
        let receipt = DeliveryReceipt {
            binding: DeliveryReceiptBinding {
                protocol_version: envelope.protocol_version,
                logical_message_id: envelope.logical_message_id,
                sender_agent_id: envelope.sender_agent_id.clone(),
                recipient_agent_id: envelope.recipient_agent_id.clone(),
                recipient_device_id: envelope.recipient_device_id,
                recipient_key_epoch: envelope.recipient_key_epoch,
                contact_generation: envelope.contact_generation,
                delivery_attempt_id: Uuid::new_v4(),
            },
            disposition,
            signature: vec![],
        };
        let (receipt, pinned_key) = signed_receipt(receipt, &[23; 32]);
        runtime
            .accept_verified_recipient_receipt(
                envelope.key(),
                state,
                VerifiedRecipientReceipt::verify(receipt, &pinned_key).unwrap(),
            )
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn transport_status_pending_admission_and_hold() {
        let runtime = KhiveRuntime::memory().unwrap();
        let token = NamespaceToken::local();
        let e = envelope();
        runtime
            .create_sender_transport(&token, e.clone())
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport_status(&token, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Pending,
            "a newly persisted envelope is pending"
        );
        runtime
            .record_sender_transport_admission(e.key(), chrono::Utc::now())
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport_status(&token, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Pending,
            "service admission is not recipient storage"
        );
        runtime
            .hold_sender_transport(e.key(), Some(HoldReason::InsufficientCredit))
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport_status(&token, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Pending,
            "an insufficient-credit hold is pending, never a further status"
        );
    }

    #[tokio::test]
    async fn transport_status_failed_and_verified_receipts() {
        let runtime = KhiveRuntime::memory().unwrap();
        let token = NamespaceToken::local();
        for (disposition, expected) in [
            (ReceiptDisposition::Stored, TransportStatus::RecipientStored),
            (
                ReceiptDisposition::Quarantined,
                TransportStatus::RecipientQuarantined,
            ),
        ] {
            let e = envelope();
            runtime
                .create_sender_transport(&token, e.clone())
                .await
                .unwrap();
            runtime
                .record_sender_transport_failure(e.key(), FailureClass::Permanent, None)
                .await
                .unwrap();
            assert_eq!(
                runtime
                    .sender_transport_status(&token, e.outbound_note_id)
                    .await
                    .unwrap(),
                TransportStatus::Failed,
                "a permanent local refusal is failed"
            );
            accept_status_receipt(&runtime, &e, disposition).await;
            assert_eq!(
                runtime
                    .sender_transport_status(&token, e.outbound_note_id)
                    .await
                    .unwrap(),
                expected,
                "a verified receipt supersedes the local failure"
            );
        }
        assert_eq!(
            runtime
                .sender_transport_status(&token, Uuid::new_v4())
                .await
                .unwrap(),
            TransportStatus::Unknown,
            "an absent outbound UUID is unknown"
        );
    }

    #[tokio::test]
    async fn transport_status_is_primary_namespace_and_backend_scoped() {
        let runtime = KhiveRuntime::memory().unwrap();
        let token_a = NamespaceToken::for_namespace(crate::Namespace::parse("sender-a").unwrap());
        let token_b = NamespaceToken::mint_with_visibility(
            crate::Namespace::parse("sender-b").unwrap(),
            vec![crate::Namespace::parse("sender-a").unwrap()],
            crate::ActorRef::anonymous(),
        );
        let e = envelope();
        runtime
            .create_sender_transport(&token_a, e.clone())
            .await
            .unwrap();
        assert_eq!(
            runtime
                .sender_transport(e.key())
                .await
                .unwrap()
                .unwrap()
                .envelope
                .namespace,
            "sender-a"
        );
        assert_eq!(
            runtime
                .sender_transport_status(&token_a, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Pending
        );
        assert_eq!(
            runtime
                .sender_transport_status(&token_b, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Unknown,
            "additional visible namespaces cannot reveal transport status"
        );
        let unrelated = KhiveRuntime::memory().unwrap();
        assert_eq!(
            unrelated
                .sender_transport_status(&token_a, e.outbound_note_id)
                .await
                .unwrap(),
            TransportStatus::Unknown,
            "a different bound backend has no record"
        );
    }

    #[tokio::test]
    async fn transport_status_read_preserves_every_sender_field() {
        let runtime = KhiveRuntime::memory().unwrap();
        let token = NamespaceToken::local();
        let e = envelope();
        runtime
            .create_sender_transport(&token, e.clone())
            .await
            .unwrap();
        let before = runtime.sender_transport(e.key()).await.unwrap().unwrap();
        assert_eq!(before.admitted_at, None);
        for _ in 0..3 {
            assert_eq!(
                runtime
                    .sender_transport_status(&token, e.outbound_note_id)
                    .await
                    .unwrap(),
                TransportStatus::Pending
            );
        }
        let after = runtime.sender_transport(e.key()).await.unwrap().unwrap();
        assert_eq!(
            after, before,
            "a status read preserves envelope bytes, holds, attempts and timestamps"
        );
    }
}