cloacina 0.11.1

A Rust library for resilient task execution and orchestration.
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
/*
 *  Copyright 2025-2026 Colliery Software
 *
 *  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.
 */

//! Unified Delivery Outbox DAL (substrate — CLOACI-S-0012 / A-0006, task T-0625).
//!
//! The delivery outbox is the **durable system of record** for the interservice
//! communication substrate: a row is enqueued (ideally in the same transaction
//! as the state change that produced it), pushed to its addressed recipient over
//! WebSocket, and acked. State machine: `pending → delivered → acked`, with
//! `delivered → pending` as the redelivery path (sweeper reclaim / reconnect
//! resync).
//!
//! Distinct from the historical `task_outbox` (a transient competing-consumer
//! scheduler→executor claim queue, removed in CLOACI-T-0914). Rows here are
//! addressed and retained until acked.
//!
//! State transitions are implemented as **atomic compare-and-set** updates
//! (filter on the expected current state; zero rows affected ⇒ the transition
//! was not permitted from the row's actual state ⇒ [`ValidationError::InvalidStateTransition`]).
//! The substrate is Postgres-only at runtime; the SQLite arms exist for unified
//! schema parity and test coverage.

use super::models::{NewUnifiedDeliveryOutbox, UnifiedDeliveryOutbox};
use super::DAL;
use crate::database::schema::unified::delivery_outbox;
use crate::database::universal_types::{UniversalBinary, UniversalTimestamp};
use crate::error::ValidationError;
use crate::models::delivery_outbox::{DeliveryOutbox, NewDeliveryOutbox};
use diesel::prelude::*;

const STATE_PENDING: &str = "pending";
const STATE_DELIVERED: &str = "delivered";
const STATE_ACKED: &str = "acked";

/// Data access layer for delivery-outbox operations with runtime backend selection.
#[derive(Clone)]
pub struct DeliveryOutboxDAL<'a> {
    dal: &'a DAL,
}

fn to_domain(r: UnifiedDeliveryOutbox) -> DeliveryOutbox {
    DeliveryOutbox {
        id: r.id,
        recipient: r.recipient,
        kind: r.kind,
        tenant_id: r.tenant_id,
        payload: r.payload.into_inner(),
        delivery_state: r.delivery_state,
        delivery_attempts: r.delivery_attempts,
        created_at: r.created_at,
        delivered_at: r.delivered_at,
        acked_at: r.acked_at,
    }
}

impl<'a> DeliveryOutboxDAL<'a> {
    pub fn new(dal: &'a DAL) -> Self {
        Self { dal }
    }

    // ── enqueue ────────────────────────────────────────────────────

    /// Enqueues a new outbox row in the `pending` state.
    ///
    /// For the atomicity guarantee (REQ-1.1.1), producing code should perform
    /// the equivalent insert inside its own transaction; this convenience
    /// method opens its own connection and is used standalone and in tests.
    pub async fn enqueue(&self, new: NewDeliveryOutbox) -> Result<DeliveryOutbox, ValidationError> {
        let row = build_insert(new);
        let result: UnifiedDeliveryOutbox = crate::interact_on_backend!(self.dal, |conn| {
            diesel::insert_into(delivery_outbox::table)
                .values(&row)
                .get_result(conn)
        })?;
        Ok(to_domain(result))
    }

    // ── state transitions (atomic compare-and-set) ─────────────────

    /// `pending → delivered`: record that the row was pushed to its recipient.
    /// Increments `delivery_attempts` and stamps `delivered_at`.
    pub async fn mark_delivered(&self, id: i64) -> Result<(), ValidationError> {
        let now = UniversalTimestamp::now();
        let affected = crate::interact_on_backend!(self.dal, |conn| {
            diesel::update(
                delivery_outbox::table
                    .filter(delivery_outbox::id.eq(id))
                    .filter(delivery_outbox::delivery_state.eq(STATE_PENDING)),
            )
            .set((
                delivery_outbox::delivery_state.eq(STATE_DELIVERED),
                delivery_outbox::delivered_at.eq(Some(now)),
                delivery_outbox::delivery_attempts.eq(delivery_outbox::delivery_attempts + 1),
            ))
            .execute(conn)
        })
        .map_err(ValidationError::from)?;
        transition_result(id, STATE_PENDING, STATE_DELIVERED, affected)
    }

    /// `pending|delivered → acked`: recipient confirmed receipt.
    ///
    /// Accepts BOTH `pending` and `delivered` as valid source states to handle
    /// the relay-vs-recipient race: on localhost the recipient can process a
    /// push frame and ack it before the relay's own `mark_delivered` CAS
    /// lands (the sink's `try_send` returns before the relay's UPDATE runs).
    /// Restricting to `delivered → acked` only would cause those races to
    /// silently drop the ack and leave the row stuck in `delivered` until the
    /// sweeper reclaimed it. Either source state is fine semantically: the
    /// recipient saw the row, whichever bookkeeping order the server reached.
    pub async fn mark_acked(&self, id: i64) -> Result<(), ValidationError> {
        let now = UniversalTimestamp::now();
        let affected = crate::interact_on_backend!(self.dal, |conn| {
            diesel::update(
                delivery_outbox::table
                    .filter(delivery_outbox::id.eq(id))
                    .filter(delivery_outbox::delivery_state.ne(STATE_ACKED)),
            )
            .set((
                delivery_outbox::delivery_state.eq(STATE_ACKED),
                delivery_outbox::acked_at.eq(Some(now)),
            ))
            .execute(conn)
        })
        .map_err(ValidationError::from)?;
        transition_result(id, "pending|delivered", STATE_ACKED, affected)
    }

    /// `delivered → pending`: redelivery path (sweeper reclaim / reconnect resync).
    pub async fn reset_to_pending(&self, id: i64) -> Result<(), ValidationError> {
        let affected = crate::interact_on_backend!(self.dal, |conn| {
            diesel::update(
                delivery_outbox::table
                    .filter(delivery_outbox::id.eq(id))
                    .filter(delivery_outbox::delivery_state.eq(STATE_DELIVERED)),
            )
            .set(delivery_outbox::delivery_state.eq(STATE_PENDING))
            .execute(conn)
        })
        .map_err(ValidationError::from)?;
        transition_result(id, STATE_DELIVERED, STATE_PENDING, affected)
    }

    /// Reset every `delivered`-state row addressed to `(recipient, tenant_id)`
    /// back to `pending`. Called on WS reconnect (T-0627) so the relay
    /// re-pushes the stuck-delivered set through its normal sink path,
    /// avoiding a separate handler-side resync that would race the relay.
    /// Returns the number of rows reset.
    ///
    /// `tenant_id = None` matches rows where the column is NULL.
    pub async fn reset_delivered_to_pending_for_recipient(
        &self,
        recipient: &str,
        tenant_id: Option<&str>,
    ) -> Result<usize, ValidationError> {
        let recipient = recipient.to_string();
        let tenant_id = tenant_id.map(|s| s.to_string());
        crate::interact_on_backend!(self.dal, |conn| {
            // diesel: branch on the tenant filter because Option doesn't auto-map to IS NULL.
            let base = delivery_outbox::table
                .filter(delivery_outbox::recipient.eq(recipient))
                .filter(delivery_outbox::delivery_state.eq(STATE_DELIVERED));
            match tenant_id {
                Some(t) => diesel::update(base.filter(delivery_outbox::tenant_id.eq(t)))
                    .set(delivery_outbox::delivery_state.eq(STATE_PENDING))
                    .execute(conn),
                None => diesel::update(base.filter(delivery_outbox::tenant_id.is_null()))
                    .set(delivery_outbox::delivery_state.eq(STATE_PENDING))
                    .execute(conn),
            }
        })
        .map_err(ValidationError::from)
    }

    /// Reassign every non-`acked` row from `from_recipient` to `to_recipient`,
    /// resetting them to `pending` (and clearing `delivered_at`) so the relay
    /// re-pushes them to the new recipient (CLOACI-T-0634 dead-agent reclaim).
    ///
    /// Called by the fleet heartbeat sweeper when an agent is evicted: its
    /// in-flight work packets (addressed to `agent:<deadId>`) are re-targeted
    /// to a live agent rather than left pinned to a recipient whose connection
    /// is gone (which would otherwise spin on `NoRoute` forever). The work
    /// keeps its `task_execution_id`, so the `FleetExecutor`'s awaiting
    /// rendezvous receives the new agent's result without any change.
    ///
    /// Returns the number of rows reassigned. OQ-2: if the "dead" agent is
    /// actually partitioned-but-alive, the re-addressed work may double-execute
    /// — this matches the at-least-once posture the thread executor has on
    /// heartbeat-loss (deliberate; see initiative OQ-2).
    pub async fn reassign_open_rows(
        &self,
        from_recipient: &str,
        to_recipient: &str,
    ) -> Result<usize, ValidationError> {
        let from_recipient = from_recipient.to_string();
        let to_recipient = to_recipient.to_string();
        crate::interact_on_backend!(self.dal, |conn| {
            diesel::update(
                delivery_outbox::table
                    .filter(delivery_outbox::recipient.eq(from_recipient))
                    .filter(delivery_outbox::delivery_state.ne(STATE_ACKED)),
            )
            .set((
                delivery_outbox::recipient.eq(to_recipient),
                delivery_outbox::delivery_state.eq(STATE_PENDING),
                delivery_outbox::delivered_at
                    .eq(None::<crate::database::universal_types::UniversalTimestamp>),
            ))
            .execute(conn)
        })
        .map_err(ValidationError::from)
    }

    // ── queries ────────────────────────────────────────────────────

    /// Open (un-acked) rows addressed to `recipient`, ordered by id for
    /// deterministic replay (relay drain / reconnect resync).
    pub async fn list_open_for_recipient(
        &self,
        recipient: &str,
        limit: i64,
    ) -> Result<Vec<DeliveryOutbox>, ValidationError> {
        let recipient = recipient.to_string();
        let rows: Vec<UnifiedDeliveryOutbox> = crate::interact_on_backend!(self.dal, |conn| {
            delivery_outbox::table
                .filter(delivery_outbox::recipient.eq(recipient))
                .filter(delivery_outbox::delivery_state.ne(STATE_ACKED))
                .order(delivery_outbox::id.asc())
                .limit(limit)
                .load(conn)
        })?;
        Ok(rows.into_iter().map(to_domain).collect())
    }

    /// Rows in the `pending` state across all recipients, oldest first — the
    /// relay's drain query (T-0626). Delivered-but-unacked rows are excluded
    /// (they await ack or sweeper reclaim, not (re)delivery on a fresh wake).
    pub async fn list_pending(&self, limit: i64) -> Result<Vec<DeliveryOutbox>, ValidationError> {
        let rows: Vec<UnifiedDeliveryOutbox> = crate::interact_on_backend!(self.dal, |conn| {
            delivery_outbox::table
                .filter(delivery_outbox::delivery_state.eq(STATE_PENDING))
                .order(delivery_outbox::id.asc())
                .limit(limit)
                .load(conn)
        })?;
        Ok(rows.into_iter().map(to_domain).collect())
    }

    /// Open rows enqueued before `cutoff`, oldest first — the safety-net
    /// sweeper's stuck-row scan (T-0628).
    pub async fn list_stuck(
        &self,
        cutoff: UniversalTimestamp,
        limit: i64,
    ) -> Result<Vec<DeliveryOutbox>, ValidationError> {
        let rows: Vec<UnifiedDeliveryOutbox> = crate::interact_on_backend!(self.dal, |conn| {
            delivery_outbox::table
                .filter(delivery_outbox::delivery_state.ne(STATE_ACKED))
                .filter(delivery_outbox::created_at.lt(cutoff))
                .order(delivery_outbox::created_at.asc())
                .limit(limit)
                .load(conn)
        })?;
        Ok(rows.into_iter().map(to_domain).collect())
    }

    /// Count of open (un-acked) rows — outbox-depth signal for monitoring (T-0628).
    pub async fn count_open(&self) -> Result<i64, ValidationError> {
        let count: i64 = crate::interact_on_backend!(self.dal, |conn| {
            delivery_outbox::table
                .filter(delivery_outbox::delivery_state.ne(STATE_ACKED))
                .count()
                .get_result(conn)
        })?;
        Ok(count)
    }
}

/// Builds the insertable row for a new `pending` outbox entry.
fn build_insert(new: NewDeliveryOutbox) -> NewUnifiedDeliveryOutbox {
    NewUnifiedDeliveryOutbox {
        recipient: new.recipient,
        kind: new.kind,
        tenant_id: new.tenant_id,
        payload: UniversalBinary::from(new.payload),
        delivery_state: STATE_PENDING.to_string(),
        delivery_attempts: 0,
        created_at: UniversalTimestamp::now(),
    }
}

/// Maps a compare-and-set affected-row count to a transition result: exactly
/// one row affected means the transition applied; zero means the row was not
/// in the expected `from` state (or does not exist), which we reject.
fn transition_result(
    id: i64,
    from: &str,
    to: &str,
    affected: usize,
) -> Result<(), ValidationError> {
    if affected == 1 {
        Ok(())
    } else {
        Err(ValidationError::InvalidStateTransition {
            id,
            from: from.to_string(),
            to: to.to_string(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::database::Database;

    #[cfg(feature = "sqlite")]
    async fn unique_dal() -> DAL {
        let url = format!(
            "file:delivery_outbox_test_{}?mode=memory&cache=shared",
            uuid::Uuid::new_v4()
        );
        let db = Database::new(&url, "", 5);
        db.run_migrations()
            .await
            .expect("migrations should succeed");
        DAL::new(db)
    }

    #[cfg(feature = "sqlite")]
    fn new_row(recipient: &str) -> NewDeliveryOutbox {
        NewDeliveryOutbox {
            recipient: recipient.to_string(),
            kind: "work".to_string(),
            tenant_id: None,
            payload: b"hello".to_vec(),
        }
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_enqueue_starts_pending() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();
        assert_eq!(row.delivery_state, STATE_PENDING);
        assert_eq!(row.delivery_attempts, 0);
        assert_eq!(row.payload, b"hello".to_vec());
        assert!(row.delivered_at.is_none());
        assert!(row.acked_at.is_none());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_reassign_open_rows_retargets_and_resets() {
        // CLOACI-T-0634 dead-agent reclaim: a delivered-but-unacked row for a
        // dead agent is re-targeted to a live agent and reset to pending.
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:dead"))
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();

        // An acked row for the same dead agent must NOT be reassigned.
        let acked = dal
            .delivery_outbox()
            .enqueue(new_row("agent:dead"))
            .await
            .unwrap();
        dal.delivery_outbox()
            .mark_delivered(acked.id)
            .await
            .unwrap();
        dal.delivery_outbox().mark_acked(acked.id).await.unwrap();

        let moved = dal
            .delivery_outbox()
            .reassign_open_rows("agent:dead", "agent:live")
            .await
            .unwrap();
        assert_eq!(moved, 1, "only the non-acked row is reassigned");

        // The reassigned row is now pending + addressed to the live agent.
        let live_open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:live", 10)
            .await
            .unwrap();
        assert_eq!(live_open.len(), 1);
        assert_eq!(live_open[0].id, row.id);
        assert_eq!(live_open[0].delivery_state, STATE_PENDING);
        assert!(live_open[0].delivered_at.is_none());

        // The dead recipient has no remaining open rows.
        let dead_open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:dead", 10)
            .await
            .unwrap();
        assert!(dead_open.is_empty());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_full_lifecycle_pending_delivered_acked() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();

        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();
        dal.delivery_outbox().mark_acked(row.id).await.unwrap();

        // Acked rows leave the open set.
        let open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap();
        assert!(open.is_empty());
        assert_eq!(dal.delivery_outbox().count_open().await.unwrap(), 0);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_mark_delivered_increments_attempts_and_stamps() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();

        let open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap();
        assert_eq!(open.len(), 1);
        assert_eq!(open[0].delivery_state, STATE_DELIVERED);
        assert_eq!(open[0].delivery_attempts, 1);
        assert!(open[0].delivered_at.is_some());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_redelivery_resets_then_redelivers_incrementing_attempts() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();
        dal.delivery_outbox()
            .reset_to_pending(row.id)
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();

        let open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap();
        assert_eq!(open[0].delivery_attempts, 2);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_ack_on_pending_succeeds_for_relay_recipient_race() {
        // Race: on localhost the recipient can ack a pushed row before the
        // relay's own `mark_delivered` CAS lands. `mark_acked` therefore
        // accepts BOTH `pending` and `delivered` as valid source states —
        // either way the recipient saw the row, which is the only thing the
        // ack is asserting.
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();
        dal.delivery_outbox().mark_acked(row.id).await.unwrap();

        // Row is `acked` and stops appearing in the open set.
        assert!(dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap()
            .is_empty());
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_invalid_transition_rejected() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();

        // Resetting a pending row is not permitted (reset is `delivered → pending` only).
        let err = dal
            .delivery_outbox()
            .reset_to_pending(row.id)
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidStateTransition { .. }
        ));

        // Once acked, the row is terminal — further state changes are rejected.
        dal.delivery_outbox().mark_acked(row.id).await.unwrap();
        let err = dal.delivery_outbox().mark_acked(row.id).await.unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidStateTransition { .. }
        ));
        let err = dal
            .delivery_outbox()
            .mark_delivered(row.id)
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidStateTransition { .. }
        ));
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_list_open_for_recipient_isolates_and_orders() {
        let dal = unique_dal().await;
        let r1 = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();
        let _r2 = dal
            .delivery_outbox()
            .enqueue(new_row("agent:2"))
            .await
            .unwrap();
        let r3 = dal
            .delivery_outbox()
            .enqueue(new_row("agent:1"))
            .await
            .unwrap();

        let open = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap();
        assert_eq!(open.len(), 2);
        // ordered by id ascending
        assert_eq!(open[0].id, r1.id);
        assert_eq!(open[1].id, r3.id);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_reset_delivered_to_pending_isolates_by_recipient_and_tenant() {
        let dal = unique_dal().await;
        let make = |recipient: &str, tenant: Option<&str>| NewDeliveryOutbox {
            recipient: recipient.to_string(),
            kind: "work".to_string(),
            tenant_id: tenant.map(|s| s.to_string()),
            payload: b"x".to_vec(),
        };

        let target = dal
            .delivery_outbox()
            .enqueue(make("agent:1", Some("t1")))
            .await
            .unwrap();
        let other_recipient = dal
            .delivery_outbox()
            .enqueue(make("agent:2", Some("t1")))
            .await
            .unwrap();
        let other_tenant = dal
            .delivery_outbox()
            .enqueue(make("agent:1", Some("t2")))
            .await
            .unwrap();
        let global = dal
            .delivery_outbox()
            .enqueue(make("agent:1", None))
            .await
            .unwrap();

        // Mark all four delivered (simulating a prior push that was never acked).
        for id in [target.id, other_recipient.id, other_tenant.id, global.id] {
            dal.delivery_outbox().mark_delivered(id).await.unwrap();
        }

        // Reset only ("agent:1", "t1") — the auth context for a hypothetical reconnect.
        let n = dal
            .delivery_outbox()
            .reset_delivered_to_pending_for_recipient("agent:1", Some("t1"))
            .await
            .unwrap();
        assert_eq!(
            n, 1,
            "only the matching (recipient, tenant) row should reset"
        );

        // Inspect all "agent:1" rows: target -> pending; other_tenant and global -> still delivered.
        let agent1_rows = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:1", 10)
            .await
            .unwrap();
        assert_eq!(agent1_rows.len(), 3);
        let by_id = |id: i64| agent1_rows.iter().find(|r| r.id == id).unwrap();
        assert_eq!(by_id(target.id).delivery_state, STATE_PENDING);
        assert_eq!(by_id(other_tenant.id).delivery_state, STATE_DELIVERED);
        assert_eq!(by_id(global.id).delivery_state, STATE_DELIVERED);

        // Other recipient is untouched.
        let agent2_rows = dal
            .delivery_outbox()
            .list_open_for_recipient("agent:2", 10)
            .await
            .unwrap();
        assert_eq!(agent2_rows.len(), 1);
        assert_eq!(agent2_rows[0].delivery_state, STATE_DELIVERED);
    }

    #[cfg(feature = "sqlite")]
    #[tokio::test]
    async fn test_reset_delivered_to_pending_matches_null_tenant() {
        let dal = unique_dal().await;
        let row = dal
            .delivery_outbox()
            .enqueue(NewDeliveryOutbox {
                recipient: "global:1".to_string(),
                kind: "work".to_string(),
                tenant_id: None,
                payload: b"x".to_vec(),
            })
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row.id).await.unwrap();

        let n = dal
            .delivery_outbox()
            .reset_delivered_to_pending_for_recipient("global:1", None)
            .await
            .unwrap();
        assert_eq!(n, 1, "reset must match NULL tenant via IS NULL, not = NULL");

        // And Some("anything") must NOT match a NULL-tenant row.
        let row2 = dal
            .delivery_outbox()
            .enqueue(NewDeliveryOutbox {
                recipient: "global:2".to_string(),
                kind: "work".to_string(),
                tenant_id: None,
                payload: b"x".to_vec(),
            })
            .await
            .unwrap();
        dal.delivery_outbox().mark_delivered(row2.id).await.unwrap();
        let n = dal
            .delivery_outbox()
            .reset_delivered_to_pending_for_recipient("global:2", Some("t1"))
            .await
            .unwrap();
        assert_eq!(n, 0, "Some(tenant) must not match a NULL-tenant row");
    }
}