awaken-stores 0.4.0

Storage backends (memory, file, PostgreSQL, SQLite mailbox) for Awaken agent state
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//! NATS JetStream + KV implementation of `MailboxStore`.
//!
//! Dispatch records live in the `dispatch-state` KV bucket (source of truth)
//! while JetStream `DISPATCH` serves as the delivery signal. An in-memory
//! index populated by `kv.watch_all()` answers list queries; point reads use
//! the authoritative KV record so control paths are not gated on watcher lag.
//!
//! # Example
//!
//! ```no_run
//! use awaken_stores::{NatsMailboxConfig, NatsMailboxStore};
//!
//! # async fn wire() -> Result<(), Box<dyn std::error::Error>> {
//! let config = NatsMailboxConfig::new("nats://localhost:4222");
//! let store = NatsMailboxStore::connect(config).await?;
//! // Use `store` wherever a `MailboxStore` is expected.
//! # store.shutdown().await?;
//! # Ok(())
//! # }
//! ```

mod claim_guard;
mod codec;
mod config;
mod index;
mod keys;
mod kv_helpers;
mod metrics;
mod ops_claim;
mod ops_interrupt;
mod ops_maintenance;
mod ops_query;
mod ops_write;
mod sweeper;

pub use config::NatsMailboxConfig;

use std::sync::Arc;

use async_trait::async_trait;
use awaken_contract::contract::mailbox::{
    DispatchSignalEntry, DispatchSignalReceipt, LiveCommandReceipt, LiveDeliveryOutcome,
    LiveRunCommand, LiveRunCommandEntry, LiveRunCommandStream, LiveRunTarget, MailboxInterrupt,
    MailboxInterruptDetails, MailboxStore, RunDispatch, RunDispatchResult, RunDispatchStatus,
};
use awaken_contract::contract::storage::StorageError;
use bytes::Bytes;
use futures::StreamExt;

use async_nats::jetstream::{consumer, kv, stream};

use index::DispatchIndex;

#[doc(hidden)]
pub fn __test_encode_dispatch(dispatch: &RunDispatch) -> Vec<u8> {
    codec::encode(dispatch).unwrap().to_vec()
}

pub struct NatsMailboxStore {
    #[allow(dead_code)]
    pub(crate) client: async_nats::Client,
    pub(crate) jetstream: async_nats::jetstream::Context,
    pub(crate) kv_dispatch: async_nats::jetstream::kv::Store,
    pub(crate) kv_epoch: async_nats::jetstream::kv::Store,
    pub(crate) kv_thread_index: async_nats::jetstream::kv::Store,
    pub(crate) consumer: async_nats::jetstream::consumer::PullConsumer,
    #[allow(dead_code)]
    pub(crate) stream_name: String,
    pub(crate) authoritative_scan_timeout: std::time::Duration,
    pub(crate) live_request_timeout: std::time::Duration,
    pub(crate) index: Arc<tokio::sync::RwLock<DispatchIndex>>,
    pub(crate) shutdown_tx: tokio::sync::watch::Sender<bool>,
    pub(crate) _watcher: tokio::task::JoinHandle<()>,
    pub(crate) _sweeper: tokio::task::JoinHandle<()>,
}

impl NatsMailboxStore {
    /// Connect to NATS, create/verify stream + buckets + consumer.
    pub async fn connect(config: NatsMailboxConfig) -> Result<Self, StorageError> {
        let client =
            crate::nats_connect::connect(&config.url, config.credentials.as_deref()).await?;
        let jetstream = async_nats::jetstream::new(client.clone());

        // Stream for dispatch delivery signals.
        let stream_config = stream::Config {
            name: config.stream_name.clone(),
            subjects: vec!["dispatch.>".to_string()],
            retention: stream::RetentionPolicy::WorkQueue,
            duplicate_window: config.dedup_window,
            ..Default::default()
        };
        let stream = jetstream
            .get_or_create_stream(stream_config)
            .await
            .map_err(|e| StorageError::Io(format!("create stream: {e}")))?;

        // Durable pull consumer.
        let consumer_config = consumer::pull::Config {
            durable_name: Some(config.consumer_name.clone()),
            filter_subject: "dispatch.>".to_string(),
            ack_policy: consumer::AckPolicy::Explicit,
            ..Default::default()
        };
        let consumer = stream
            .get_or_create_consumer(&config.consumer_name, consumer_config)
            .await
            .map_err(|e| StorageError::Io(format!("create consumer: {e}")))?;

        let kv_dispatch = Self::get_or_create_bucket(&jetstream, &config.dispatch_bucket).await?;
        let kv_epoch = Self::get_or_create_bucket(&jetstream, &config.epoch_bucket).await?;
        let kv_thread_index =
            Self::get_or_create_bucket(&jetstream, &config.thread_index_bucket).await?;
        let watcher_initial_scan_timeout = config.watcher_initial_scan_timeout;
        let sweeper_republish_after = config.sweeper_republish_after;
        let authoritative_scan_timeout = config.authoritative_scan_timeout;
        let live_request_timeout = config.nats_request_timeout;

        let index = Arc::new(tokio::sync::RwLock::new(DispatchIndex::default()));
        let (shutdown_tx, _shutdown_rx) = tokio::sync::watch::channel(false);

        // Spawn KV watcher to keep index in sync.
        let watcher_shutdown_rx = shutdown_tx.subscribe();
        let (watcher_ready_tx, watcher_ready_rx) = tokio::sync::oneshot::channel();
        let watcher_handle = index::spawn_watcher(
            kv_dispatch.clone(),
            kv_thread_index.clone(),
            Arc::clone(&index),
            watcher_shutdown_rx,
            watcher_ready_tx,
        );
        match tokio::time::timeout(watcher_initial_scan_timeout, watcher_ready_rx).await {
            Err(_) => {
                watcher_handle.abort();
                return Err(StorageError::Io(format!(
                    "mailbox index initial scan timed out after {watcher_initial_scan_timeout:?}"
                )));
            }
            Ok(ready) => match ready {
                Ok(Ok(())) => {}
                Ok(Err(error)) => {
                    return Err(StorageError::Io(format!(
                        "mailbox index initial scan: {error}"
                    )));
                }
                Err(_) => {
                    return Err(StorageError::Io(
                        "mailbox index watcher exited before initial scan".to_string(),
                    ));
                }
            },
        }

        let sweeper_shutdown_rx = shutdown_tx.subscribe();
        let sweeper_handle = sweeper::spawn_sweeper(
            jetstream.clone(),
            Arc::clone(&index),
            sweeper_shutdown_rx,
            config.sweeper_interval,
            sweeper_republish_after,
        );

        Ok(Self {
            client,
            jetstream,
            kv_dispatch,
            kv_epoch,
            kv_thread_index,
            consumer,
            stream_name: config.stream_name,
            authoritative_scan_timeout,
            live_request_timeout,
            index,
            shutdown_tx,
            _watcher: watcher_handle,
            _sweeper: sweeper_handle,
        })
    }

    async fn get_or_create_bucket(
        jetstream: &async_nats::jetstream::Context,
        name: &str,
    ) -> Result<kv::Store, StorageError> {
        match jetstream.get_key_value(name).await {
            Ok(store) => Ok(store),
            Err(_) => jetstream
                .create_key_value(kv::Config {
                    bucket: name.to_string(),
                    history: 1,
                    ..Default::default()
                })
                .await
                .map_err(|e| StorageError::Io(format!("create bucket {name}: {e}"))),
        }
    }

    /// Signal background tasks to shut down.
    pub async fn shutdown(&self) -> Result<(), StorageError> {
        let _ = self.shutdown_tx.send(true);
        Ok(())
    }

    #[doc(hidden)]
    pub fn kv_dispatch(&self) -> &async_nats::jetstream::kv::Store {
        &self.kv_dispatch
    }

    #[doc(hidden)]
    pub async fn index_contains(&self, dispatch_id: &str) -> bool {
        self.index.read().await.get(dispatch_id).is_some()
    }

    #[doc(hidden)]
    pub async fn __test_remove_dispatch_from_index(&self, dispatch_id: &str) {
        self.index.write().await.remove(dispatch_id);
    }

    #[doc(hidden)]
    pub async fn __test_purge_thread_index(&self, thread_id: &str) -> Result<(), StorageError> {
        self.kv_thread_index
            .purge(&keys::thread_index_key(thread_id))
            .await
            .map(|_| ())
            .map_err(|e| StorageError::Io(format!("purge thread index: {e}")))
    }

    #[doc(hidden)]
    pub async fn __test_purge_thread_claim(&self, thread_id: &str) -> Result<(), StorageError> {
        self.kv_thread_index
            .purge(&keys::thread_claim_key(thread_id))
            .await
            .map(|_| ())
            .map_err(|e| StorageError::Io(format!("purge thread claim: {e}")))
    }

    #[doc(hidden)]
    pub async fn __test_purge_dedupe_lock(
        &self,
        thread_id: &str,
        dedupe_key: &str,
    ) -> Result<(), StorageError> {
        self.kv_thread_index
            .purge(&keys::dedupe_lock_key(thread_id, dedupe_key))
            .await
            .map(|_| ())
            .map_err(|e| StorageError::Io(format!("purge dedupe lock: {e}")))
    }

    #[doc(hidden)]
    pub async fn __test_delete_dispatch_record(
        &self,
        dispatch_id: &str,
    ) -> Result<(), StorageError> {
        self.kv_dispatch
            .delete(&keys::dispatch_key(dispatch_id))
            .await
            .map(|_| ())
            .map_err(|e| StorageError::Io(format!("delete dispatch record: {e}")))
    }

    /// Test-only: force a dedupe lock into the KV bucket without going
    /// through `enqueue`, so integration tests can reproduce the
    /// crash-between-create-and-put orphan scenario.
    #[doc(hidden)]
    pub async fn __test_force_dedupe_lock(
        &self,
        thread_id: &str,
        dedupe_key: &str,
        holder_dispatch_id: &str,
    ) -> Result<(), StorageError> {
        let key = keys::dedupe_lock_key(thread_id, dedupe_key);
        let value = codec::encode_dedupe_lock(&codec::DedupeLockRecord {
            dispatch_id: holder_dispatch_id.to_string(),
            created_at: 0,
        })?;
        self.kv_thread_index
            .create(&key, value)
            .await
            .map(|_| ())
            .map_err(|e| StorageError::Io(format!("force lock: {e}")))
    }

    /// Test-only: attempt to release a dedupe lock as a specific dispatch
    /// owner. Used to reproduce delayed-release races.
    #[doc(hidden)]
    pub async fn __test_release_dedupe_lock_as(
        &self,
        thread_id: &str,
        dedupe_key: &str,
        holder_dispatch_id: &str,
    ) {
        ops_write::release_dedupe_lock(self, thread_id, dedupe_key, holder_dispatch_id).await;
    }

    #[doc(hidden)]
    pub async fn __test_dedupe_lock_holder(
        &self,
        thread_id: &str,
        dedupe_key: &str,
    ) -> Result<Option<String>, StorageError> {
        let key = keys::dedupe_lock_key(thread_id, dedupe_key);
        let entry = self
            .kv_thread_index
            .entry(&key)
            .await
            .map_err(|e| StorageError::Io(format!("dedupe lock entry: {e}")))?;
        entry
            .filter(|entry| !kv_helpers::is_tombstone(entry))
            .map(|entry| codec::decode_dedupe_lock(&entry.value).map(|lock| lock.dispatch_id))
            .transpose()
    }

    /// Test-only: plant a dispatch in the authoritative KV store WITHOUT
    /// publishing the JetStream delivery signal. Reproduces the
    /// partial-failure path where `enqueue` committed to KV but the JS
    /// publish later failed — the sweeper should detect the missing
    /// signal and re-publish.
    #[doc(hidden)]
    pub async fn __test_plant_dispatch_without_publish(
        &self,
        dispatch: &RunDispatch,
    ) -> Result<(), StorageError> {
        let mut stamped = dispatch.clone();
        stamped.dispatch_epoch = match self
            .kv_epoch
            .entry(&keys::epoch_key(&stamped.thread_id))
            .await
            .map_err(|e| StorageError::Io(format!("kv entry: {e}")))?
        {
            Some(entry) if kv_helpers::is_tombstone(&entry) => 0,
            Some(entry) => codec::decode_epoch(&entry.value)?,
            None => 0,
        };
        ops_write::append_thread_index(self, &stamped.thread_id, &stamped.dispatch_id).await?;
        let bytes = codec::encode(&stamped)?;
        let revision = self
            .kv_dispatch
            .put(keys::dispatch_key(&stamped.dispatch_id), bytes)
            .await
            .map_err(|e| StorageError::Io(format!("kv put: {e}")))?;
        self.index
            .write()
            .await
            .upsert_with_revision(stamped, revision);
        Ok(())
    }

    /// Test-only: plant a dispatch exactly as supplied. This is used to
    /// reproduce cross-node races where local indexes observe stale epoch
    /// dispatches that authoritative enqueue stamping would no longer create.
    #[doc(hidden)]
    pub async fn __test_plant_dispatch_exact(
        &self,
        dispatch: &RunDispatch,
    ) -> Result<(), StorageError> {
        ops_write::append_thread_index(self, &dispatch.thread_id, &dispatch.dispatch_id).await?;
        let bytes = codec::encode(dispatch)?;
        let revision = self
            .kv_dispatch
            .put(keys::dispatch_key(&dispatch.dispatch_id), bytes)
            .await
            .map_err(|e| StorageError::Io(format!("kv put: {e}")))?;
        self.index
            .write()
            .await
            .upsert_with_revision(dispatch.clone(), revision);
        Ok(())
    }

    /// Test-only: overwrite the local in-memory index without changing KV.
    /// This reproduces watcher-lag races where local state is older than
    /// the authoritative dispatch record.
    #[doc(hidden)]
    pub async fn __test_upsert_index_only(&self, dispatch: &RunDispatch) {
        self.index.write().await.force_upsert(dispatch.clone());
    }
}

#[async_trait]
impl MailboxStore for NatsMailboxStore {
    async fn enqueue(&self, dispatch: &RunDispatch) -> Result<(), StorageError> {
        ops_write::enqueue(self, dispatch).await
    }
    async fn claim(
        &self,
        thread_id: &str,
        consumer_id: &str,
        lease_ms: u64,
        now: u64,
        limit: usize,
    ) -> Result<Vec<RunDispatch>, StorageError> {
        ops_claim::claim(self, thread_id, consumer_id, lease_ms, now, limit).await
    }
    async fn claim_dispatch(
        &self,
        dispatch_id: &str,
        consumer_id: &str,
        lease_ms: u64,
        now: u64,
    ) -> Result<Option<RunDispatch>, StorageError> {
        ops_claim::claim_dispatch(self, dispatch_id, consumer_id, lease_ms, now).await
    }
    async fn ack(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        now: u64,
    ) -> Result<(), StorageError> {
        ops_write::ack(self, dispatch_id, claim_token, now).await
    }
    async fn record_dispatch_start(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        dispatch_instance_id: &str,
        now: u64,
    ) -> Result<(), StorageError> {
        ops_write::record_dispatch_start(self, dispatch_id, claim_token, dispatch_instance_id, now)
            .await
    }
    async fn record_run_result(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        result: &RunDispatchResult,
        now: u64,
    ) -> Result<(), StorageError> {
        ops_write::record_run_result(self, dispatch_id, claim_token, result, now).await
    }
    async fn nack(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        retry_at: u64,
        error: &str,
        now: u64,
    ) -> Result<(), StorageError> {
        ops_write::nack(self, dispatch_id, claim_token, retry_at, error, now).await
    }
    async fn dead_letter(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        error: &str,
        now: u64,
    ) -> Result<(), StorageError> {
        ops_write::dead_letter(self, dispatch_id, claim_token, error, now).await
    }
    async fn cancel(
        &self,
        dispatch_id: &str,
        now: u64,
    ) -> Result<Option<RunDispatch>, StorageError> {
        ops_write::cancel(self, dispatch_id, now).await
    }
    async fn extend_lease(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        extension_ms: u64,
        now: u64,
    ) -> Result<bool, StorageError> {
        ops_write::extend_lease(self, dispatch_id, claim_token, extension_ms, now).await
    }
    async fn interrupt(&self, thread_id: &str, now: u64) -> Result<MailboxInterrupt, StorageError> {
        self.interrupt_detailed(thread_id, now)
            .await
            .map(Into::into)
    }
    async fn interrupt_detailed(
        &self,
        thread_id: &str,
        now: u64,
    ) -> Result<MailboxInterruptDetails, StorageError> {
        ops_interrupt::interrupt(self, thread_id, now).await
    }
    async fn current_dispatch_epoch(&self, thread_id: &str) -> Result<u64, StorageError> {
        ops_write::current_thread_epoch(self, thread_id).await
    }
    async fn supersede_claimed(
        &self,
        dispatch_id: &str,
        claim_token: &str,
        now: u64,
        reason: &str,
    ) -> Result<Option<RunDispatch>, StorageError> {
        ops_write::supersede_claimed(self, dispatch_id, claim_token, now, reason).await
    }
    async fn load_dispatch(&self, dispatch_id: &str) -> Result<Option<RunDispatch>, StorageError> {
        ops_query::load_dispatch(self, dispatch_id).await
    }
    async fn list_dispatches(
        &self,
        thread_id: &str,
        status_filter: Option<&[RunDispatchStatus]>,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<RunDispatch>, StorageError> {
        ops_query::list_dispatches(self, thread_id, status_filter, limit, offset).await
    }
    async fn count_dispatches_by_status(
        &self,
        status: RunDispatchStatus,
    ) -> Result<usize, StorageError> {
        ops_query::count_dispatches_by_status(self, status).await
    }
    async fn list_terminal_dispatches(
        &self,
        limit: usize,
        offset: usize,
    ) -> Result<Vec<RunDispatch>, StorageError> {
        ops_query::list_terminal_dispatches(self, limit, offset).await
    }
    async fn reclaim_expired_leases(
        &self,
        now: u64,
        limit: usize,
    ) -> Result<Vec<RunDispatch>, StorageError> {
        ops_maintenance::reclaim_expired_leases(self, now, limit).await
    }
    async fn purge_terminal(&self, older_than: u64) -> Result<usize, StorageError> {
        ops_maintenance::purge_terminal(self, older_than).await
    }
    async fn queued_thread_ids(&self) -> Result<Vec<String>, StorageError> {
        ops_query::queued_thread_ids(self).await
    }

    fn supports_dispatch_signals(&self) -> bool {
        true
    }

    async fn pull_dispatch_signals(
        &self,
        max: usize,
        expires: std::time::Duration,
    ) -> Result<Vec<DispatchSignalEntry>, StorageError> {
        if max == 0 {
            return Ok(Vec::new());
        }

        let mut messages = self
            .consumer
            .fetch()
            .max_messages(max)
            .expires(expires)
            .messages()
            .await
            .map_err(|e| StorageError::Io(format!("dispatch signal fetch: {e}")))?;

        let mut entries = Vec::new();
        while let Some(next) = messages.next().await {
            let message =
                next.map_err(|e| StorageError::Io(format!("dispatch signal message: {e}")))?;
            let dispatch_id = match String::from_utf8(message.payload.to_vec()) {
                Ok(dispatch_id) if !dispatch_id.trim().is_empty() => dispatch_id,
                _ => {
                    let _ = message.ack().await;
                    continue;
                }
            };
            let Some(dispatch) = ops_query::load_dispatch(self, &dispatch_id).await? else {
                let _ = message.ack().await;
                continue;
            };
            if dispatch.status.is_terminal() {
                ops_write::cleanup_thread_index(self, &dispatch).await;
            } else {
                ops_write::append_thread_index(self, &dispatch.thread_id, &dispatch.dispatch_id)
                    .await?;
            }
            self.index.write().await.upsert(dispatch.clone());
            entries.push(DispatchSignalEntry {
                thread_id: dispatch.thread_id,
                dispatch_id,
                receipt: Box::new(NatsDispatchSignalReceipt { message }),
            });
        }
        Ok(entries)
    }

    // Live channel uses core NATS request-reply (not JetStream). `LiveRunCommand`
    // is ephemeral by contract: no persistence, no redelivery. Request-reply
    // gives the producer a subscriber-presence signal: `NoResponders` =
    // nobody listening and `TimedOut` = listener couldn't ack; both map to
    // `NoSubscriber` so callers fall back to durable dispatch. Other request
    // errors are surfaced because they do not prove the command was unobserved.

    async fn deliver_live(
        &self,
        thread_id: &str,
        cmd: LiveRunCommand,
    ) -> Result<LiveDeliveryOutcome, StorageError> {
        deliver_live_subject(self, keys::live_subject(thread_id), cmd).await
    }

    async fn deliver_live_to(
        &self,
        target: &LiveRunTarget,
        cmd: LiveRunCommand,
    ) -> Result<LiveDeliveryOutcome, StorageError> {
        deliver_live_subject(
            self,
            keys::live_target_subject(
                &target.thread_id,
                &target.run_id,
                target.dispatch_id.as_deref(),
            ),
            cmd,
        )
        .await
    }

    async fn open_live_channel(
        &self,
        thread_id: &str,
    ) -> Result<LiveRunCommandStream, StorageError> {
        open_live_subject(self, keys::live_subject(thread_id)).await
    }

    async fn open_live_channel_for(
        &self,
        target: &LiveRunTarget,
    ) -> Result<LiveRunCommandStream, StorageError> {
        open_live_subject(
            self,
            keys::live_target_subject(
                &target.thread_id,
                &target.run_id,
                target.dispatch_id.as_deref(),
            ),
        )
        .await
    }
}

async fn deliver_live_subject(
    store: &NatsMailboxStore,
    subject: String,
    cmd: LiveRunCommand,
) -> Result<LiveDeliveryOutcome, StorageError> {
    let payload = serde_json::to_vec(&cmd)
        .map_err(|e| StorageError::Serialization(format!("LiveRunCommand encode: {e}")))?;
    let result = match tokio::time::timeout(
        store.live_request_timeout,
        store.client.request(subject, Bytes::from(payload)),
    )
    .await
    {
        Err(_) => Ok(LiveDeliveryOutcome::NoSubscriber),
        Ok(result) => match result {
            Ok(_) => Ok(LiveDeliveryOutcome::Delivered),
            Err(err) => match err.kind() {
                async_nats::client::RequestErrorKind::NoResponders
                | async_nats::client::RequestErrorKind::TimedOut => {
                    Ok(LiveDeliveryOutcome::NoSubscriber)
                }
                _ => Err(StorageError::Io(format!("nats request: {err}"))),
            },
        },
    };
    metrics::inc_live_delivery(&result);
    result
}

async fn open_live_subject(
    store: &NatsMailboxStore,
    subject: String,
) -> Result<LiveRunCommandStream, StorageError> {
    let subscriber = store
        .client
        .subscribe(subject)
        .await
        .map_err(|e| StorageError::Io(format!("nats subscribe: {e}")))?;
    let client = store.client.clone();
    let stream = subscriber.filter_map(move |msg| {
        let client = client.clone();
        async move {
            // Decode BEFORE handing the reply subject to the consumer.
            // Malformed payload → drop and leave the producer without
            // an ack so its request times out and falls back to durable
            // dispatch.
            let command = match serde_json::from_slice::<LiveRunCommand>(&msg.payload) {
                Ok(cmd) => cmd,
                Err(err) => {
                    tracing::warn!(error = %err, "dropping malformed LiveRunCommand payload");
                    return None;
                }
            };
            let receipt: Box<dyn LiveCommandReceipt> = Box::new(NatsReplyReceipt {
                client,
                reply: msg.reply.clone(),
            });
            Some(LiveRunCommandEntry { command, receipt })
        }
    });
    Ok(Box::pin(stream))
}

/// Receipt backed by a NATS reply subject. `ack` publishes an empty
/// payload to the subject the producer's `client.request()` is awaiting
/// on. Dropping the receipt without ack leaves the request to time out →
/// producer observes `NoSubscriber`.
struct NatsReplyReceipt {
    client: async_nats::Client,
    reply: Option<async_nats::Subject>,
}

impl LiveCommandReceipt for NatsReplyReceipt {
    fn ack(self: Box<Self>) {
        let Some(reply) = self.reply else {
            return;
        };
        let client = self.client;
        tokio::spawn(async move {
            if let Err(err) = client.publish(reply, Bytes::new()).await {
                tracing::warn!(error = %err, "live-channel ack publish failed");
            }
        });
    }
}

struct NatsDispatchSignalReceipt {
    message: async_nats::jetstream::Message,
}

#[async_trait]
impl DispatchSignalReceipt for NatsDispatchSignalReceipt {
    fn redelivery_attempts(&self) -> Option<u64> {
        self.message
            .info()
            .ok()
            .and_then(|info| u64::try_from(info.delivered).ok())
    }

    async fn ack(self: Box<Self>) -> Result<(), StorageError> {
        self.message
            .ack()
            .await
            .map_err(|e| StorageError::Io(format!("dispatch signal ack: {e}")))
    }

    async fn nack(self: Box<Self>) -> Result<(), StorageError> {
        self.message
            .ack_with(async_nats::jetstream::AckKind::Nak(None))
            .await
            .map_err(|e| StorageError::Io(format!("dispatch signal nack: {e}")))
    }

    async fn nack_with_delay(
        self: Box<Self>,
        delay: std::time::Duration,
    ) -> Result<(), StorageError> {
        self.message
            .ack_with(async_nats::jetstream::AckKind::Nak(Some(delay)))
            .await
            .map_err(|e| StorageError::Io(format!("dispatch signal delayed nack: {e}")))
    }
}