tact-memory 0.6.2

Bounded local and remote memory for Tact
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
//! Storage contract, backend selection, and shared failures.

#[cfg(feature = "local")]
mod local;
#[cfg(feature = "client")]
mod remote;

#[cfg(all(feature = "client", feature = "local"))]
use crate::{MemoryAccess, MemorySource, secrets::contains_likely_secret};
use crate::{
    MemoryKey, MemoryLimits, MemoryRecord, MemoryScan,
    server::protocol::{self, ExportCursor, SyncReport},
};
#[cfg(feature = "local")]
pub use local::LocalMemoryStore;
#[cfg(feature = "client")]
pub use remote::{RemoteClientError, RemoteMemoryClient, RemoteToken};
#[cfg(all(feature = "client", feature = "local"))]
use std::path::PathBuf;
#[cfg(feature = "local")]
use std::time::{SystemTime, UNIX_EPOCH};
use std::{error::Error, future::Future};
use thiserror::Error;

/// Ordinary operations shared by local and authenticated remote memory backends.
///
/// Implementations own their namespace and storage boundary. Returned records are ordered
/// deterministically by the implementation, direct mutations use key versions as compare-and-swap
/// preconditions, and each implementation captures its authoritative operation time internally.
/// Dropping a returned future requests cancellation. Implementations may finish an already-started
/// atomic storage transaction after cancellation.
pub trait MemoryStore: Clone + Send + Sync + 'static {
    /// Searches visible records and records scan telemetry.
    fn scan(
        &self,
        query: &str,
        limit: usize,
    ) -> impl Future<Output = Result<MemoryScan, MemoryError>> + Send;

    /// Reads unversioned IDs and versioned keys, recording use telemetry.
    fn read(
        &self,
        ids: &[i64],
        keys: &[MemoryKey],
    ) -> impl Future<Output = Result<Vec<MemoryRecord>, MemoryError>> + Send;

    /// Lists a deterministic window that excludes expired probationary records.
    ///
    /// Implementations return at most [`MemoryLimits::records`] records so interactive inspection
    /// does not grow with the complete shared corpus. Full transfer uses paginated export instead.
    fn list(&self) -> impl Future<Output = Result<Vec<MemoryRecord>, MemoryError>> + Send;

    /// Inserts content or compare-and-swap replaces `replacement`.
    fn put(
        &self,
        content: &str,
        replacement: Option<MemoryKey>,
    ) -> impl Future<Output = Result<MemoryRecord, MemoryError>> + Send;

    /// Compare-and-swap deletes `key`.
    ///
    /// Deleting an already-absent key succeeds, making safe request replay idempotent. An existing
    /// record with a different version returns [`MemoryError::Conflict`].
    fn delete(&self, key: MemoryKey) -> impl Future<Output = Result<(), MemoryError>> + Send;

    /// Atomically applies an authoritative full snapshot to the owned namespace.
    ///
    /// Records absent from `memories` are deleted. Input is validated before commit, IDs remain
    /// stable, and future insertions must not reuse IDs observed in the snapshot.
    fn sync(
        &self,
        memories: &[MemoryRecord],
    ) -> impl Future<Output = Result<SyncReport, MemoryError>> + Send;

    /// Exports one page in stable `(namespace, id)` order after `cursor`.
    ///
    /// The page is a point-in-time transaction snapshot. `limit` is clamped to the protocol bound;
    /// callers continue only with the exact returned cursor. Cancellation before storage work starts
    /// prevents the operation; an already-started transaction may finish.
    fn export_page(
        &self,
        namespaces: Option<&[String]>,
        cursor: Option<&ExportCursor>,
        limit: usize,
    ) -> impl Future<Output = Result<(Vec<MemoryRecord>, Option<ExportCursor>), MemoryError>> + Send;

    /// Collects a complete bounded export through the paginated storage contract.
    ///
    /// The collector rejects non-progressing cursors and stops before retaining more records or
    /// content than a local store can import.
    fn export_all(
        &self,
        namespaces: Option<&[String]>,
    ) -> impl Future<Output = Result<Vec<MemoryRecord>, MemoryError>> + Send {
        async move {
            let mut cursor = None;
            let mut records = Vec::new();
            let mut content_bytes = 0usize;
            loop {
                let (page, next_cursor) = self
                    .export_page(
                        namespaces,
                        cursor.as_ref(),
                        protocol::MAX_EXPORT_PAGE_RECORDS,
                    )
                    .await?;
                let next_record_count = records
                    .len()
                    .checked_add(page.len())
                    .ok_or(MemoryError::InvalidPagination)?;
                let page_bytes = page.iter().try_fold(0usize, |total, record| {
                    total.checked_add(record.content.len())
                });
                content_bytes = content_bytes
                    .checked_add(page_bytes.ok_or(MemoryError::InvalidPagination)?)
                    .ok_or(MemoryError::InvalidPagination)?;
                if next_record_count > MemoryLimits::PRODUCTION.records
                    || content_bytes > MemoryLimits::PRODUCTION.total_content_bytes
                    || next_cursor
                        .as_ref()
                        .is_some_and(|next| cursor.as_ref() == Some(next))
                    || (page.is_empty() && next_cursor.is_some())
                {
                    return Err(MemoryError::InvalidPagination);
                }
                records.extend(page);
                match next_cursor {
                    Some(next) => cursor = Some(next),
                    None => return Ok(records),
                }
            }
        }
    }
}

/// Runtime-selected local-or-remote memory backend.
#[cfg(all(feature = "client", feature = "local"))]
#[derive(Clone, Debug)]
pub enum SelectedMemoryStore {
    /// Private local SQLite storage.
    Local(LocalMemoryStore),
    /// Authenticated namespaced HTTP storage.
    Remote(RemoteMemoryClient),
}

#[cfg(all(feature = "client", feature = "local"))]
impl SelectedMemoryStore {
    /// Selects a private local SQLite backend.
    pub fn local(path: impl Into<PathBuf>) -> Self {
        Self::Local(LocalMemoryStore::new(path))
    }

    /// Selects an authenticated remote HTTP backend.
    pub const fn remote(client: RemoteMemoryClient) -> Self {
        Self::Remote(client)
    }

    /// Returns the selected backend kind without performing I/O.
    pub const fn source(&self) -> MemorySource {
        match self {
            Self::Local(_) => MemorySource::Local,
            Self::Remote(_) => MemorySource::Remote,
        }
    }

    /// Returns backend provenance and negotiated remote authorization.
    pub async fn access(&self) -> Result<MemoryAccess, MemoryError> {
        match self {
            Self::Local(_) => Ok(MemoryAccess {
                source: MemorySource::Local,
                namespace: None,
                role: None,
            }),
            Self::Remote(client) => Ok(MemoryAccess {
                source: MemorySource::Remote,
                namespace: Some(client.namespace().to_owned()),
                role: Some(client.session().await?),
            }),
        }
    }
}

#[cfg(all(feature = "client", feature = "local"))]
impl MemoryStore for SelectedMemoryStore {
    fn scan(
        &self,
        query: &str,
        limit: usize,
    ) -> impl Future<Output = Result<MemoryScan, MemoryError>> + Send {
        async move {
            match self {
                Self::Local(store) => MemoryStore::scan(store, query, limit).await,
                Self::Remote(client) => MemoryStore::scan(client, query, limit).await,
            }
        }
    }
    fn read(
        &self,
        ids: &[i64],
        keys: &[MemoryKey],
    ) -> impl Future<Output = Result<Vec<MemoryRecord>, MemoryError>> + Send {
        async move {
            match self {
                Self::Local(store) => MemoryStore::read(store, ids, keys).await,
                Self::Remote(client) => MemoryStore::read(client, ids, keys).await,
            }
        }
    }
    fn list(&self) -> impl Future<Output = Result<Vec<MemoryRecord>, MemoryError>> + Send {
        async move {
            match self {
                Self::Local(store) => MemoryStore::list(store).await,
                Self::Remote(client) => MemoryStore::list(client).await,
            }
        }
    }
    fn put(
        &self,
        content: &str,
        replacement: Option<MemoryKey>,
    ) -> impl Future<Output = Result<MemoryRecord, MemoryError>> + Send {
        async move {
            reject_unsafe(content)?;
            match self {
                Self::Local(store) => MemoryStore::put(store, content, replacement).await,
                Self::Remote(client) => MemoryStore::put(client, content, replacement).await,
            }
        }
    }
    fn delete(&self, key: MemoryKey) -> impl Future<Output = Result<(), MemoryError>> + Send {
        async move {
            match self {
                Self::Local(store) => MemoryStore::delete(store, key).await,
                Self::Remote(client) => MemoryStore::delete(client, key).await,
            }
        }
    }
    fn sync(
        &self,
        memories: &[MemoryRecord],
    ) -> impl Future<Output = Result<SyncReport, MemoryError>> + Send {
        async move {
            for memory in memories {
                reject_unsafe(&memory.content)?;
            }
            match self {
                Self::Local(store) => MemoryStore::sync(store, memories).await,
                Self::Remote(client) => MemoryStore::sync(client, memories).await,
            }
        }
    }
    fn export_page(
        &self,
        namespaces: Option<&[String]>,
        cursor: Option<&ExportCursor>,
        limit: usize,
    ) -> impl Future<Output = Result<(Vec<MemoryRecord>, Option<ExportCursor>), MemoryError>> + Send
    {
        async move {
            match self {
                Self::Local(store) => {
                    MemoryStore::export_page(store, namespaces, cursor, limit).await
                }
                Self::Remote(client) => {
                    MemoryStore::export_page(client, namespaces, cursor, limit).await
                }
            }
        }
    }
}

#[cfg(all(feature = "client", feature = "local"))]
fn reject_unsafe(content: &str) -> Result<(), MemoryError> {
    if contains_likely_secret(content) {
        return Err(MemoryError::SecretRejected);
    }
    Ok(())
}

#[cfg(feature = "local")]
fn current_time_ms() -> i64 {
    let milliseconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis();
    i64::try_from(milliseconds).unwrap_or(i64::MAX)
}

/// Failure from local storage, remote transport, validation, or optimistic concurrency.
#[derive(Debug, Error)]
pub enum MemoryError {
    /// Content is empty after trimming.
    #[error("memory content is empty")]
    EmptyContent,
    /// One record exceeds its byte bound.
    #[error("memory content exceeds the {maximum_bytes}-byte limit")]
    ContentTooLarge {
        /// Configured maximum bytes per record.
        maximum_bytes: usize,
    },
    /// A scan query exceeds its byte bound.
    #[error("memory query exceeds the {maximum_bytes}-byte limit")]
    QueryTooLarge {
        /// Configured maximum query bytes.
        maximum_bytes: usize,
    },
    /// The record-count bound is exhausted.
    #[error("memory record capacity of {maximum} was reached")]
    RecordCapacity {
        /// Configured maximum records.
        maximum: usize,
    },
    /// The aggregate content-byte bound is exhausted.
    #[error("memory content capacity of {maximum_bytes} bytes was reached")]
    ContentCapacity {
        /// Configured maximum aggregate bytes.
        maximum_bytes: usize,
    },
    /// The backing store reached its configured storage bound.
    #[error("memory storage capacity was reached")]
    StorageCapacity,
    /// Content was rejected as a likely credential or secret.
    #[error("memory content was rejected as a likely secret")]
    SecretRejected,
    /// Equivalent normalized content already exists.
    #[error("an equivalent memory already exists")]
    Duplicate,
    /// The requested record does not exist.
    #[error("memory was not found")]
    NotFound,
    /// A key version or snapshot state is stale.
    #[error("memory changed since it was read")]
    Conflict,
    /// A mutation targeted a namespace not owned by this backend.
    #[error("memories from other namespaces are read-only")]
    RemoteReadOnly,
    /// A store returned an invalid or unbounded pagination sequence.
    #[error("memory store returned invalid pagination")]
    InvalidPagination,
    /// Database schema is newer than this implementation supports.
    #[error(
        "memory schema version {found} is unsupported; this build supports version {supported}"
    )]
    UnsupportedSchemaVersion {
        /// Schema version found on disk.
        found: i64,
        /// Newest schema supported by this implementation.
        supported: i64,
    },
    /// A backend-specific operation failed.
    #[error("memory backend operation failed")]
    Backend {
        /// Backend-specific failure.
        #[source]
        source: Box<dyn Error + Send + Sync>,
    },
    /// A backend-specific operation failed transiently and may be retried.
    #[error("memory backend is temporarily unavailable")]
    Unavailable {
        /// Backend-specific transient failure.
        #[source]
        source: Box<dyn Error + Send + Sync>,
    },
}

impl MemoryError {
    /// Wraps a permanent or unclassified backend-specific failure.
    pub fn backend(source: impl Error + Send + Sync + 'static) -> Self {
        Self::Backend {
            source: Box::new(source),
        }
    }

    /// Wraps a transient backend-specific failure that may succeed when retried.
    pub fn unavailable(source: impl Error + Send + Sync + 'static) -> Self {
        Self::Unavailable {
            source: Box::new(source),
        }
    }

    /// Returns whether the backend classified this failure as transient.
    pub fn is_retryable(&self) -> bool {
        matches!(self, Self::Unavailable { .. })
    }
}

#[cfg(feature = "client")]
impl From<RemoteClientError> for MemoryError {
    fn from(source: RemoteClientError) -> Self {
        match source {
            error @ (RemoteClientError::Transport | RemoteClientError::Unavailable) => {
                Self::unavailable(error)
            }
            RemoteClientError::ReadOnly | RemoteClientError::NamespaceMismatch => {
                Self::RemoteReadOnly
            }
            RemoteClientError::Rejected { code } => match code {
                protocol::RemoteErrorCode::QueryTooLarge => Self::QueryTooLarge {
                    maximum_bytes: MemoryLimits::PRODUCTION.query_bytes,
                },
                protocol::RemoteErrorCode::ContentTooLarge => Self::ContentTooLarge {
                    maximum_bytes: MemoryLimits::PRODUCTION.content_bytes,
                },
                protocol::RemoteErrorCode::RecordCapacity => Self::RecordCapacity {
                    maximum: MemoryLimits::PRODUCTION.records,
                },
                protocol::RemoteErrorCode::ContentCapacity => Self::ContentCapacity {
                    maximum_bytes: MemoryLimits::PRODUCTION.total_content_bytes,
                },
                protocol::RemoteErrorCode::Duplicate => Self::Duplicate,
                protocol::RemoteErrorCode::NotFound => Self::NotFound,
                protocol::RemoteErrorCode::Conflict => Self::Conflict,
                protocol::RemoteErrorCode::Forbidden
                | protocol::RemoteErrorCode::NamespaceMismatch => Self::RemoteReadOnly,
                protocol::RemoteErrorCode::Unavailable => {
                    Self::unavailable(RemoteClientError::Rejected { code })
                }
                _ => Self::backend(RemoteClientError::Rejected { code }),
            },
            error => Self::backend(error),
        }
    }
}