mempill-core 0.2.0

Core engine for mempill — a bi-temporal, append-only claim store with a deterministic adjudication gate and oracle resolution for temporally-correct AI-agent memory
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
//! EngineHandle — the sole public async entry point for mempill.
//!
//! Owns `Arc<impl Port>` references plus the per-agent_id write lock map.
//! Every public method:
//!   1. Reads the clock ONCE at the async boundary (`now = Utc::now()`).
//!   2. Acquires the per-agent_id write lock for write operations.
//!   3. Delegates to the sync use-case via `tokio::task::spawn_blocking`.
//!   4. Maps `JoinError` → `MemError::SpawnBlocking`.
//!
//! The use-case layer is fully synchronous — no async code below this file.
//!
//! # Pending-adjudication port
//!
//! `EngineHandle` carries an optional `Arc<dyn ErasedPendingStore>` for the oracle queue.
//! Use `EngineHandle::new` for the standard case (no pending store) and
//! `EngineHandle::new_with_pending_store` when wiring in a concrete adapter.
//! The type-erasure lets `EngineHandle<P, O, V>` keep its existing 3-param signature.

use std::sync::Arc;

use chrono::Utc;
use tokio::task;

use crate::{
    application::{
        audit::AuditUseCase,
        dto::{
            AuditQueryRequest, AuditQueryResponse, IngestClaimRequest, IngestClaimResponse,
            QueryHistoryRequest, QueryHistoryResponse, QueryMemoryRequest, QueryMemoryResponse,
            ReconcileRequest, ReconcileResponse,
        },
        ingest_claim::IngestClaimUseCase,
        query_history::QueryHistoryUseCase,
        query_memory::QueryMemoryUseCase,
        reconcile::ReconcileUseCase,
        submit_adjudication::SubmitAdjudicationUseCase,
        sweep_adjudications::SweepAdjudicationsUseCase,
    },
    concurrency::agent_lock::AgentWriteLockMap,
    config::EngineConfig,
    error::MemError,
    ports::{OraclePort, PendingAdjudicationPort, PersistencePort, VectorPort},
};

// ── Type-erased pending store ─────────────────────────────────────────────────
//
// `PendingAdjudicationPort` is NOT object-safe in its generic form because `Self::Error`
// is an associated type. We introduce a thin object-safe erasing wrapper that boxes errors.

/// Object-safe erasing wrapper for `PendingAdjudicationPort`.
///
/// Adapters implement `PendingAdjudicationPort`; this wrapper is created via
/// `ErasedPendingStoreAdapter::new(concrete_store)` and stored as `Arc<dyn ErasedPendingStore>`.
#[allow(missing_docs)]
pub trait ErasedPendingStore: Send + Sync + 'static {
    fn insert_pending_erased(
        &self,
        row: &crate::ports::pending_adjudication::PendingAdjudicationRow,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn get_pending_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<Option<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn list_pending_erased(
        &self,
        agent_id: Option<&mempill_types::AgentId>,
    ) -> Result<Vec<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn list_expired_erased(
        &self,
        now: chrono::DateTime<chrono::Utc>,
    ) -> Result<Vec<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn mark_resolved_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn mark_expired_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>;

    fn list_queued_orphan_claims_erased(
        &self,
    ) -> Result<Vec<crate::ports::pending_adjudication::OrphanedQueuedClaim>, Box<dyn std::error::Error + Send + Sync + 'static>>;
}

/// Adapter that wraps a concrete `PendingAdjudicationPort` impl as `dyn ErasedPendingStore`.
pub struct ErasedPendingStoreAdapter<S: PendingAdjudicationPort> {
    inner: S,
}

impl<S: PendingAdjudicationPort> ErasedPendingStoreAdapter<S> {
    /// Wrap a concrete `PendingAdjudicationPort` impl as `dyn ErasedPendingStore`.
    pub fn new(inner: S) -> Self {
        Self { inner }
    }
}

impl<S: PendingAdjudicationPort> ErasedPendingStore for ErasedPendingStoreAdapter<S> {
    fn insert_pending_erased(
        &self,
        row: &crate::ports::pending_adjudication::PendingAdjudicationRow,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.insert_pending(row).map_err(|e| Box::new(e) as _)
    }

    fn get_pending_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<Option<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.get_pending(handle_id).map_err(|e| Box::new(e) as _)
    }

    fn list_pending_erased(
        &self,
        agent_id: Option<&mempill_types::AgentId>,
    ) -> Result<Vec<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.list_pending(agent_id).map_err(|e| Box::new(e) as _)
    }

    fn list_expired_erased(
        &self,
        now: chrono::DateTime<chrono::Utc>,
    ) -> Result<Vec<crate::ports::pending_adjudication::PendingAdjudicationRow>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.list_expired(now).map_err(|e| Box::new(e) as _)
    }

    fn mark_resolved_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.mark_resolved(handle_id).map_err(|e| Box::new(e) as _)
    }

    fn mark_expired_erased(
        &self,
        handle_id: uuid::Uuid,
    ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.mark_expired(handle_id).map_err(|e| Box::new(e) as _)
    }

    fn list_queued_orphan_claims_erased(
        &self,
    ) -> Result<Vec<crate::ports::pending_adjudication::OrphanedQueuedClaim>, Box<dyn std::error::Error + Send + Sync + 'static>> {
        self.inner.list_queued_orphan_claims().map_err(|e| Box::new(e) as _)
    }
}

// ── EngineHandle ──────────────────────────────────────────────────────────────

/// The sole public async entry point for mempill.
///
/// Callers: mempill-py, mempill-node, mempill-mcp, integration tests.
/// Cloneable: all fields are `Arc`-wrapped; clones share the same lock map and port state.
pub struct EngineHandle<P, O, V>
where
    P: PersistencePort + Send + Sync + 'static,
    O: OraclePort + Send + Sync + 'static,
    V: VectorPort + Send + Sync + 'static,
{
    persistence: Arc<P>,
    oracle: Option<Arc<O>>,
    vector: Option<Arc<V>>,
    /// Type-erased pending-adjudication store. `None` when no oracle queue is configured.
    pending_store: Option<Arc<dyn ErasedPendingStore>>,
    config: EngineConfig,
    write_locks: AgentWriteLockMap,
    /// Store-level write lock: serializes ALL writes across agent_ids to prevent
    /// concurrent SQLite transactions from different agents on the same connection.
    /// Reads (query_memory, query_audit) never acquire this lock.
    store_write_lock: Arc<tokio::sync::Mutex<()>>,
}

impl<P, O, V> EngineHandle<P, O, V>
where
    P: PersistencePort + Send + Sync + 'static,
    O: OraclePort + Send + Sync + 'static,
    V: VectorPort + Send + Sync + 'static,
{
    /// Create an `EngineHandle` without a pending-adjudication store.
    ///
    /// QueuedForAdjudication claims will still be committed with the correct disposition,
    /// but no `pending_adjudications` row will be written. Suitable for tests that don't
    /// exercise oracle queue persistence, and for the `DefaultEngine` alias.
    pub fn new(
        persistence: Arc<P>,
        oracle: Option<Arc<O>>,
        vector: Option<Arc<V>>,
        config: EngineConfig,
    ) -> Self {
        Self {
            persistence,
            oracle,
            vector,
            pending_store: None,
            config,
            write_locks: AgentWriteLockMap::new(),
            store_write_lock: Arc::new(tokio::sync::Mutex::new(())),
        }
    }

    /// Create an `EngineHandle` with a concrete pending-adjudication store.
    ///
    /// The store is type-erased via [`ErasedPendingStoreAdapter`] so `EngineHandle` keeps
    /// its 3-param signature unchanged.
    ///
    /// Typical usage in adapter crates (e.g. mempill-sqlite):
    /// ```rust,ignore
    /// let engine = EngineHandle::new_with_pending_store(
    ///     Arc::new(persistence_store),
    ///     Some(Arc::new(oracle)),
    ///     None::<Arc<NoOpVector>>,
    ///     Arc::new(ErasedPendingStoreAdapter::new(sqlite_pending_store)),
    ///     EngineConfig::default(),
    /// );
    /// ```
    pub fn new_with_pending_store<S>(
        persistence: Arc<P>,
        oracle: Option<Arc<O>>,
        vector: Option<Arc<V>>,
        pending_store: Arc<dyn ErasedPendingStore>,
        config: EngineConfig,
    ) -> Self {
        Self {
            persistence,
            oracle,
            vector,
            pending_store: Some(pending_store),
            config,
            write_locks: AgentWriteLockMap::new(),
            store_write_lock: Arc::new(tokio::sync::Mutex::new(())),
        }
    }

    /// Write path: async, acquires per-agent_id lock, delegates to IngestClaimUseCase.
    ///
    /// Clock is read ONCE here (DETERMINISM): `now` flows into the use-case as a parameter.
    ///
    /// Locking order (must be consistent across all write methods to avoid deadlock):
    ///   1. store_write_lock  — serializes all cross-agent SQLite writes (conditional; Postgres skips)
    ///   2. per-agent lock    — preserves same-agent serial semantics + Postgres compat
    pub async fn ingest_claim(
        &self,
        req: IngestClaimRequest,
    ) -> Result<IngestClaimResponse, MemError> {
        let now = Utc::now(); // clock read ONCE at the async boundary
        // Acquire global write lock only when the adapter requires it (SQLite=yes, Postgres=no).
        let _store_lock = if self.persistence.requires_global_write_serialization() {
            Some(self.store_write_lock.lock().await)
        } else {
            None
        };
        let _guard = self.write_locks.acquire(&req.agent_id).await;
        let uc = IngestClaimUseCase::new(
            Arc::clone(&self.persistence),
            self.oracle.clone(),
            self.pending_store.clone(),
            self.config.clone(),
        );
        task::spawn_blocking(move || uc.execute_with_time(req, now))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Read path: no write lock needed. Delegates to QueryMemoryUseCase.
    ///
    /// Clock read ONCE here; passed into the sync use-case.
    pub async fn query_memory(
        &self,
        req: QueryMemoryRequest,
    ) -> Result<QueryMemoryResponse, MemError> {
        let now = Utc::now();
        let uc = QueryMemoryUseCase::new(
            Arc::clone(&self.persistence),
            self.vector.clone(),
            self.config.clone(),
        );
        task::spawn_blocking(move || uc.execute_with_time(req, now))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// History read path: no write lock needed. Delegates to QueryHistoryUseCase.
    ///
    /// Returns the full ordered timeline for a (subject, predicate) subject-line.
    /// Each entry is tagged `Current` or `Superseded` using the same canonical fold
    /// as `query_memory` — so `history.current().value == recall primary value`.
    ///
    /// Clock read ONCE here; passed into the sync use-case (DETERMINISM).
    pub async fn query_history(
        &self,
        req: QueryHistoryRequest,
    ) -> Result<QueryHistoryResponse, MemError> {
        let now = Utc::now();
        let uc = QueryHistoryUseCase::new(
            Arc::clone(&self.persistence),
            self.vector.clone(),
            self.config.clone(),
        );
        task::spawn_blocking(move || uc.execute_with_time(req, now))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Reconcile path: acquires write lock per agent_id in the request.
    ///
    /// Locking order matches ingest_claim: store_write_lock first (conditional), then per-agent lock.
    pub async fn reconcile(
        &self,
        req: ReconcileRequest,
    ) -> Result<ReconcileResponse, MemError> {
        // Acquire global write lock only when the adapter requires it (SQLite=yes, Postgres=no).
        let _store_lock = if self.persistence.requires_global_write_serialization() {
            Some(self.store_write_lock.lock().await)
        } else {
            None
        };
        let _guard = self.write_locks.acquire(&req.agent_id).await;
        let uc = ReconcileUseCase::new(
            Arc::clone(&self.persistence),
            self.oracle.clone(),
            self.config.clone(),
        );
        task::spawn_blocking(move || uc.execute(req))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Audit read path: no write lock.
    pub async fn query_audit(
        &self,
        req: AuditQueryRequest,
    ) -> Result<AuditQueryResponse, MemError> {
        let uc = AuditUseCase::new(Arc::clone(&self.persistence));
        task::spawn_blocking(move || uc.execute(req))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Oracle resolution path: deliver an oracle verdict and apply it atomically.
    ///
    /// Acquires locks in the SAME ORDER as `ingest_claim` to prevent deadlock:
    ///   1. `store_write_lock`  — serializes all cross-agent SQLite writes (conditional).
    ///   2. per-agent lock      — keyed on the `agent_id` retrieved from the pending row.
    ///
    /// # Postgres / async-runtime safety
    ///
    /// The postgres sync crate (`postgres 0.19`) wraps `tokio-postgres` and calls `block_on`
    /// in `Client::drop`. Dropping a postgres `Client` while a tokio runtime is active on the
    /// current thread panics with "Cannot start a runtime from within a runtime".
    ///
    /// ALL pending-store I/O (including the agent_id resolution read) is therefore performed
    /// inside `spawn_blocking` so no `postgres::Client` is ever created or dropped on the
    /// async executor thread. This is the same discipline used by `ingest_claim`.
    ///
    /// # Protocol
    ///
    /// 1. `spawn_blocking` — resolve `agent_id` from the pending row (DB read, safe).
    /// 2. Acquire `store_write_lock` (SQLite-only) + per-agent write lock (async).
    /// 3. `spawn_blocking` — run `SubmitAdjudicationUseCase::execute` (all DB writes).
    ///
    /// # Errors
    ///
    /// - `MemError::AdjudicationHandleNotFound` — handle unknown, expired, or stale.
    /// - `MemError::PendingStore` — pending-store I/O error.
    /// - `MemError::Persistence` — DB write error during verdict apply.
    /// - `MemError::SpawnBlocking` — tokio task join error.
    pub async fn submit_adjudication(
        &self,
        handle_id: uuid::Uuid,
        response: mempill_types::AdjudicationResponse,
    ) -> Result<mempill_types::AdjudicationOutcome, MemError> {
        let now = Utc::now(); // clock read ONCE at the async boundary (DETERMINISM)

        // ── Step 1: Resolve agent_id via spawn_blocking (NO async-context DB access) ──
        //
        // The postgres sync crate calls `block_on` in `Client::drop`. Reading the pending
        // store directly in the async context would drop a postgres connection on the tokio
        // thread, causing a panic. `spawn_blocking` moves the drop to a dedicated OS thread
        // where no tokio runtime is active. The use-case re-reads the row inside its own
        // spawn_blocking (Step 3) for the authoritative state-guard.
        let pending_store = self.pending_store.as_ref()
            .ok_or(MemError::AdjudicationHandleNotFound { handle_id })?;
        let pending_store_arc = Arc::clone(pending_store);

        let resolve_result = task::spawn_blocking(move || {
            let row = pending_store_arc
                .get_pending_erased(handle_id)
                .map_err(|e| MemError::PendingStore { source: e })?
                .ok_or(MemError::AdjudicationHandleNotFound { handle_id })?;
            Ok::<_, MemError>(row)
        })
        .await
        .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })??;

        let row = resolve_result;

        // Note: TTL expiry is handled authoritatively inside SubmitAdjudicationUseCase
        // (which also writes the AdjudicationExpired ledger entry). Do NOT early-reject
        // here — the use-case must run so the audit trail is complete.
        let agent_id = row.agent_id.clone();

        // ── Step 2: Acquire locks in the same order as ingest_claim ──────────────
        let _store_lock = if self.persistence.requires_global_write_serialization() {
            Some(self.store_write_lock.lock().await)
        } else {
            None
        };
        let _guard = self.write_locks.acquire(&agent_id).await;

        // ── Step 3: Dispatch to sync use-case via spawn_blocking ─────────────────
        let pending_store_arc2 = Arc::clone(pending_store);
        let uc = SubmitAdjudicationUseCase::new(
            Arc::clone(&self.persistence),
            pending_store_arc2,
        );
        task::spawn_blocking(move || uc.execute(handle_id, response, now))
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Read path: list all pending-adjudication rows for an agent (or all agents).
    ///
    /// This is a read-only operation — no write lock is acquired.  All DB access
    /// is performed inside `spawn_blocking` so no `postgres::Client` is created or
    /// dropped on the async executor thread (same invariant as `submit_adjudication`).
    ///
    /// Returns `Ok(vec![])` when no pending store is configured.
    pub async fn list_pending_adjudications(
        &self,
        agent_id: Option<mempill_types::AgentId>,
    ) -> Result<Vec<crate::ports::pending_adjudication::PendingAdjudicationRow>, MemError> {
        let pending_store = match &self.pending_store {
            Some(ps) => Arc::clone(ps),
            None => return Ok(vec![]),
        };

        task::spawn_blocking(move || {
            pending_store
                .list_pending_erased(agent_id.as_ref())
                .map_err(|e| MemError::PendingStore { source: e })
        })
        .await
        .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })?
    }

    /// Sweep all expired pending-adjudication rows and orphaned QueuedForAdjudication claims.
    ///
    /// For each expired pending row (expires_at <= now):
    ///   1. Acquires store_write_lock + per-agent write lock (same order as ingest_claim).
    ///   2. Atomically reverts the challenger QueuedForAdjudication → Contested + ledger entry.
    ///   3. Marks the pending row expired.
    ///
    /// Then sweeps orphan claims (QueuedForAdjudication with no pending row):
    ///   4. Per orphan: acquires locks, reverts challenger → Contested + ledger entry.
    ///
    /// Returns the total count of claims reverted (expired + orphan).
    ///
    /// The engine MUST NOT spawn a background task — the host calls this on its own schedule.
    ///
    /// If no pending store is configured, returns `Ok(0)` (sweep is a no-op without oracle queue).
    ///
    /// # Postgres / async-runtime safety
    ///
    /// ALL pending-store reads (`list_expired`, `list_queued_orphan_claims`) are performed
    /// inside `spawn_blocking` so no `postgres::Client` is created or dropped on the tokio
    /// executor thread (same invariant as `submit_adjudication`).
    pub async fn sweep_expired_adjudications(&self) -> Result<usize, MemError> {
        let now = Utc::now();

        let pending_store = match &self.pending_store {
            Some(ps) => Arc::clone(ps),
            None => return Ok(0),
        };

        // ── Phase 1: Collect expired rows via spawn_blocking (NO async-context DB access) ──
        let ps_for_list = Arc::clone(&pending_store);
        let expired_rows = task::spawn_blocking(move || {
            ps_for_list
                .list_expired_erased(now)
                .map_err(|e| MemError::PendingStore { source: e })
        })
        .await
        .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })??;

        let mut swept = 0usize;

        for row in expired_rows {
            let agent_id = row.agent_id.clone();

            let _store_lock = if self.persistence.requires_global_write_serialization() {
                Some(self.store_write_lock.lock().await)
            } else {
                None
            };
            let _guard = self.write_locks.acquire(&agent_id).await;

            let persistence = Arc::clone(&self.persistence);
            let ps = Arc::clone(&pending_store);
            let row_clone = row.clone();

            let result = task::spawn_blocking(move || {
                let uc = SweepAdjudicationsUseCase::new(persistence, ps);
                uc.revert_expired_row(&row_clone, now)
            })
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })??;

            if result {
                swept += 1;
            }
        }

        // ── Phase 2: Collect orphans via spawn_blocking (NO async-context DB access) ──
        // Detect QueuedForAdjudication claims with no matching pending row.
        let ps_for_orphans = Arc::clone(&pending_store);
        let orphans = task::spawn_blocking(move || {
            ps_for_orphans
                .list_queued_orphan_claims_erased()
                .map_err(|e| MemError::PendingStore { source: e })
        })
        .await
        .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })??;

        for orphan in orphans {
            let agent_id = orphan.agent_id.clone();

            let _store_lock = if self.persistence.requires_global_write_serialization() {
                Some(self.store_write_lock.lock().await)
            } else {
                None
            };
            let _guard = self.write_locks.acquire(&agent_id).await;

            let persistence = Arc::clone(&self.persistence);
            let ps = Arc::clone(&pending_store);
            let orphan_clone = orphan.clone();

            let result = task::spawn_blocking(move || {
                let uc = SweepAdjudicationsUseCase::new(persistence, ps);
                uc.revert_orphan(&orphan_clone, now)
            })
            .await
            .map_err(|e| MemError::SpawnBlocking { reason: e.to_string() })??;

            if result {
                swept += 1;
            }
        }

        Ok(swept)
    }
}

impl<P, O, V> Clone for EngineHandle<P, O, V>
where
    P: PersistencePort + Send + Sync + 'static,
    O: OraclePort + Send + Sync + 'static,
    V: VectorPort + Send + Sync + 'static,
{
    fn clone(&self) -> Self {
        Self {
            persistence: Arc::clone(&self.persistence),
            oracle: self.oracle.clone(),
            vector: self.vector.clone(),
            pending_store: self.pending_store.clone(),
            config: self.config.clone(),
            write_locks: self.write_locks.clone(),
            store_write_lock: Arc::clone(&self.store_write_lock),
        }
    }
}