acton-ai 0.26.0

An agentic AI framework where each agent is an actor
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
//! Persistence error types.
//!
//! Custom error types for all persistence operations, providing actionable
//! error messages and support for error classification.

use crate::types::AgentId;
use std::fmt;

/// Errors that can occur in persistence operations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PersistenceError {
    /// Optional agent ID if error is agent-specific
    pub agent_id: Option<AgentId>,
    /// The specific error that occurred (boxed for size efficiency)
    kind: Box<PersistenceErrorKind>,
}

/// Specific persistence error types.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PersistenceErrorKind {
    /// Failed to open or create database
    DatabaseOpen {
        /// Path to the database file
        path: String,
        /// Error message from database
        message: String,
    },
    /// Failed to initialize schema
    SchemaInit {
        /// Error message from database
        message: String,
    },
    /// Query execution failed
    QueryFailed {
        /// The operation that failed
        operation: String,
        /// Error message from database
        message: String,
    },
    /// Record not found
    NotFound {
        /// The type of entity not found
        entity: String,
        /// The ID of the entity
        id: String,
    },
    /// Serialization failed
    SerializationFailed {
        /// Error message
        message: String,
    },
    /// Deserialization failed
    DeserializationFailed {
        /// Error message
        message: String,
    },
    /// Store is shutting down
    ShuttingDown,
    /// Transaction failed
    TransactionFailed {
        /// Error message
        message: String,
    },
    /// Connection error
    ConnectionError {
        /// Error message
        message: String,
    },
    /// Embedding generation failed
    EmbeddingFailed {
        /// The embedding provider name
        provider: String,
        /// Error message
        message: String,
    },
    /// Embedding dimension mismatch
    EmbeddingDimensionMismatch {
        /// Expected dimension
        expected: usize,
        /// Actual dimension
        actual: usize,
    },
    /// Vector search failed
    VectorSearchFailed {
        /// Error message
        message: String,
    },
}

impl PersistenceError {
    /// Creates a new persistence error with the given kind.
    #[must_use]
    pub fn new(kind: PersistenceErrorKind) -> Self {
        Self {
            agent_id: None,
            kind: Box::new(kind),
        }
    }

    /// Creates a new persistence error associated with a specific agent.
    #[must_use]
    pub fn with_agent(agent_id: AgentId, kind: PersistenceErrorKind) -> Self {
        Self {
            agent_id: Some(agent_id),
            kind: Box::new(kind),
        }
    }

    /// Returns a reference to the error kind.
    #[must_use]
    pub fn kind(&self) -> &PersistenceErrorKind {
        &self.kind
    }

    /// Creates a database open error.
    #[must_use]
    pub fn database_open(path: impl Into<String>, message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::DatabaseOpen {
            path: path.into(),
            message: message.into(),
        })
    }

    /// Creates a schema initialization error.
    #[must_use]
    pub fn schema_init(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::SchemaInit {
            message: message.into(),
        })
    }

    /// Creates a query failed error.
    #[must_use]
    pub fn query_failed(operation: impl Into<String>, message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::QueryFailed {
            operation: operation.into(),
            message: message.into(),
        })
    }

    /// Creates a not found error.
    #[must_use]
    pub fn not_found(entity: impl Into<String>, id: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::NotFound {
            entity: entity.into(),
            id: id.into(),
        })
    }

    /// Creates a serialization failed error.
    #[must_use]
    pub fn serialization_failed(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::SerializationFailed {
            message: message.into(),
        })
    }

    /// Creates a deserialization failed error.
    #[must_use]
    pub fn deserialization_failed(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::DeserializationFailed {
            message: message.into(),
        })
    }

    /// Creates a shutting down error.
    #[must_use]
    pub fn shutting_down() -> Self {
        Self::new(PersistenceErrorKind::ShuttingDown)
    }

    /// Creates a transaction failed error.
    #[must_use]
    pub fn transaction_failed(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::TransactionFailed {
            message: message.into(),
        })
    }

    /// Creates a connection error.
    #[must_use]
    pub fn connection_error(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::ConnectionError {
            message: message.into(),
        })
    }

    /// Creates an embedding failed error.
    #[must_use]
    pub fn embedding_failed(provider: impl Into<String>, message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::EmbeddingFailed {
            provider: provider.into(),
            message: message.into(),
        })
    }

    /// Creates an embedding dimension mismatch error.
    #[must_use]
    pub fn embedding_dimension_mismatch(expected: usize, actual: usize) -> Self {
        Self::new(PersistenceErrorKind::EmbeddingDimensionMismatch { expected, actual })
    }

    /// Creates a vector search failed error.
    #[must_use]
    pub fn vector_search_failed(message: impl Into<String>) -> Self {
        Self::new(PersistenceErrorKind::VectorSearchFailed {
            message: message.into(),
        })
    }

    /// Returns true if this error is retriable.
    ///
    /// Connection errors and transaction failures are typically transient
    /// and may succeed on retry.
    #[must_use]
    pub fn is_retriable(&self) -> bool {
        matches!(
            *self.kind,
            PersistenceErrorKind::ConnectionError { .. }
                | PersistenceErrorKind::TransactionFailed { .. }
        )
    }

    /// Returns true if this error indicates a record was not found.
    #[must_use]
    pub fn is_not_found(&self) -> bool {
        matches!(*self.kind, PersistenceErrorKind::NotFound { .. })
    }

    /// Returns true if the store is shutting down.
    #[must_use]
    pub fn is_shutting_down(&self) -> bool {
        matches!(*self.kind, PersistenceErrorKind::ShuttingDown)
    }
}

impl fmt::Display for PersistenceError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(ref id) = self.agent_id {
            write!(f, "agent '{}': ", id)?;
        }

        match self.kind.as_ref() {
            PersistenceErrorKind::DatabaseOpen { path, message } => {
                write!(
                    f,
                    "failed to open database at '{}': {}; check file permissions and path",
                    path, message
                )
            }
            PersistenceErrorKind::SchemaInit { message } => {
                write!(
                    f,
                    "failed to initialize database schema: {}; database may be corrupted",
                    message
                )
            }
            PersistenceErrorKind::QueryFailed { operation, message } => {
                write!(f, "{} query failed: {}", operation, message)
            }
            PersistenceErrorKind::NotFound { entity, id } => {
                write!(f, "{} with id '{}' not found", entity, id)
            }
            PersistenceErrorKind::SerializationFailed { message } => {
                write!(f, "failed to serialize data: {}", message)
            }
            PersistenceErrorKind::DeserializationFailed { message } => {
                write!(f, "failed to deserialize data: {}", message)
            }
            PersistenceErrorKind::ShuttingDown => {
                write!(
                    f,
                    "memory store is shutting down; cannot accept new requests"
                )
            }
            PersistenceErrorKind::TransactionFailed { message } => {
                write!(f, "transaction failed: {}; retry may succeed", message)
            }
            PersistenceErrorKind::ConnectionError { message } => {
                write!(f, "database connection error: {}", message)
            }
            PersistenceErrorKind::EmbeddingFailed { provider, message } => {
                write!(
                    f,
                    "embedding provider '{}' failed: {}; check provider configuration",
                    provider, message
                )
            }
            PersistenceErrorKind::EmbeddingDimensionMismatch { expected, actual } => {
                write!(
                    f,
                    "embedding dimension mismatch: expected {}, got {}; ensure consistent provider",
                    expected, actual
                )
            }
            PersistenceErrorKind::VectorSearchFailed { message } => {
                write!(f, "vector search failed: {}", message)
            }
        }
    }
}

impl std::error::Error for PersistenceError {}

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

    #[test]
    fn persistence_error_database_open_display() {
        let error = PersistenceError::database_open("/path/to/db", "permission denied");
        let msg = error.to_string();
        assert!(msg.contains("/path/to/db"));
        assert!(msg.contains("permission denied"));
    }

    #[test]
    fn persistence_error_connection_is_retriable() {
        let error = PersistenceError::connection_error("timeout");
        assert!(error.is_retriable());
    }

    #[test]
    fn persistence_error_transaction_is_retriable() {
        let error = PersistenceError::transaction_failed("deadlock");
        assert!(error.is_retriable());
    }

    #[test]
    fn persistence_error_not_found_is_not_retriable() {
        let error = PersistenceError::not_found("conversation", "conv_123");
        assert!(!error.is_retriable());
        assert!(error.is_not_found());
    }

    #[test]
    fn persistence_error_schema_init_display() {
        let error = PersistenceError::schema_init("table already exists");
        let msg = error.to_string();
        assert!(msg.contains("schema"));
        assert!(msg.contains("table already exists"));
    }

    #[test]
    fn persistence_error_query_failed_display() {
        let error = PersistenceError::query_failed("INSERT", "constraint violation");
        let msg = error.to_string();
        assert!(msg.contains("INSERT"));
        assert!(msg.contains("constraint violation"));
    }

    #[test]
    fn persistence_error_not_found_display() {
        let error = PersistenceError::not_found("conversation", "conv_abc");
        let msg = error.to_string();
        assert!(msg.contains("conversation"));
        assert!(msg.contains("conv_abc"));
        assert!(msg.contains("not found"));
    }

    #[test]
    fn persistence_error_serialization_display() {
        let error = PersistenceError::serialization_failed("invalid utf-8");
        let msg = error.to_string();
        assert!(msg.contains("serialize"));
        assert!(msg.contains("invalid utf-8"));
    }

    #[test]
    fn persistence_error_deserialization_display() {
        let error = PersistenceError::deserialization_failed("missing field");
        let msg = error.to_string();
        assert!(msg.contains("deserialize"));
        assert!(msg.contains("missing field"));
    }

    #[test]
    fn persistence_error_shutting_down() {
        let error = PersistenceError::shutting_down();
        assert!(error.is_shutting_down());
        let msg = error.to_string();
        assert!(msg.contains("shutting down"));
    }

    #[test]
    fn persistence_error_with_agent_display() {
        let agent_id = AgentId::new();
        let error = PersistenceError::with_agent(
            agent_id.clone(),
            PersistenceErrorKind::NotFound {
                entity: "state".to_string(),
                id: "state_123".to_string(),
            },
        );
        let msg = error.to_string();
        assert!(msg.contains(&agent_id.to_string()));
        assert!(msg.contains("state"));
    }

    #[test]
    fn persistence_error_kind_accessor() {
        let error = PersistenceError::connection_error("timeout");
        assert!(matches!(
            error.kind(),
            PersistenceErrorKind::ConnectionError { .. }
        ));
    }

    #[test]
    fn persistence_error_embedding_failed_display() {
        let error = PersistenceError::embedding_failed("openai", "rate limited");
        let msg = error.to_string();
        assert!(msg.contains("openai"));
        assert!(msg.contains("rate limited"));
    }

    #[test]
    fn persistence_error_embedding_dimension_mismatch_display() {
        let error = PersistenceError::embedding_dimension_mismatch(384, 768);
        let msg = error.to_string();
        assert!(msg.contains("384"));
        assert!(msg.contains("768"));
    }

    #[test]
    fn persistence_error_vector_search_failed_display() {
        let error = PersistenceError::vector_search_failed("index corrupted");
        let msg = error.to_string();
        assert!(msg.contains("vector search"));
        assert!(msg.contains("index corrupted"));
    }
}