runact 1.0.0

A Rust-native actor runtime with BEAM-style processes
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
# Runact Persistence

## Overview

Persistence in Runact ensures that runtime state survives restarts. Critical transactions must have recoverable state. All persisted formats must support migrations.

## What to Persist

### Required

- Runtime metadata
- Process registry
- Extensions
- Configuration
- Audit information

### Not Persisted (Initially)

- Process state (except agents)
- Mailbox contents
- In-flight messages
- Temporary data

## Storage Architecture

```
┌─────────────────────────────────────┐
│           Storage API               │
│    (trait-based abstraction)        │
└──────────────┬──────────────────────┘
               │
    ┌──────────┼──────────┐
    ▼          ▼          ▼
 SQLite    SledDB    Filesystem
```

### Storage Trait

```rust
#[async_trait]
pub trait Storage: Send + Sync {
    async fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, StorageError>;
    async fn put(&self, key: &[u8], value: &[u8]) -> Result<(), StorageError>;
    async fn delete(&self, key: &[u8]) -> Result<(), StorageError>;
    async fn exists(&self, key: &[u8]) -> Result<bool, StorageError>;
    async fn list(&self, prefix: &[u8]) -> Result<Vec<Vec<u8>>, StorageError>;

    async fn transaction<F, R>(&self, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&mut Transaction) -> R + Send;

    fn snapshot(&self) -> Box<dyn Storage>;
}
```

### Storage Error

```rust
pub enum StorageError {
    NotFound(Vec<u8>),
    AlreadyExists(Vec<u8>),
    SerializationError(String),
    IoError(std::io::Error),
    TransactionError(String),
}
```

## Schema Versioning

### Versioned Schemas

Every persisted format has a version.

```rust
pub struct VersionedSchema<T> {
    pub version: SchemaVersion,
    pub data: T,
}

pub struct SchemaVersion {
    pub major: u16,
    pub minor: u16,
}
```

### Schema Registry

```rust
pub struct SchemaRegistry {
    schemas: HashMap<String, Box<dyn Schema>>,
}

pub trait Schema: Send + Sync {
    fn name(&self) -> &str;
    fn version(&self) -> SchemaVersion;
    fn migrate_from(&self, from: &dyn Schema, data: Vec<u8>) -> Result<Vec<u8>, MigrationError>;
}
```

## Migrations

### Migration Chain

```
v1 → v2 → v3 → v4 (current)
```

### Migration Implementation

```rust
pub struct MigrationChain {
    migrations: Vec<Box<dyn Migration>>,
}

pub trait Migration: Send + Sync {
    fn from_version(&self) -> SchemaVersion;
    fn to_version(&self) -> SchemaVersion;
    fn migrate(&self, data: Vec<u8>) -> Result<Vec<u8>, MigrationError>;
}

impl MigrationChain {
    pub fn migrate(&self, data: Vec<u8>, from: SchemaVersion) -> Result<Vec<u8>, MigrationError> {
        let mut current = data;
        let mut current_version = from;

        for migration in &self.migrations {
            if migration.from_version() == current_version {
                current = migration.migrate(current)?;
                current_version = migration.to_version();
            }
        }

        Ok(current)
    }
}
```

### Example Migration

```rust
pub struct V1ToV2Migration;

impl Migration for V1ToV2Migration {
    fn from_version(&self) -> SchemaVersion {
        SchemaVersion { major: 1, minor: 0 }
    }

    fn to_version(&self) -> SchemaVersion {
        SchemaVersion { major: 2, minor: 0 }
    }

    fn migrate(&self, data: Vec<u8>) -> Result<Vec<u8>, MigrationError> {
        let v1: RuntimeV1 = bincode::deserialize(&data)?;
        let v2 = RuntimeV2 {
            id: v1.id,
            name: v1.name,
            created_at: v1.created_at,
            // New field with default
            configuration: RuntimeConfig::default(),
        };
        Ok(bincode::serialize(&v2)?)
    }
}
```

## Persisted Types

### Runtime

```rust
#[derive(Serialize, Deserialize)]
pub struct RuntimePersisted {
    pub id: RuntimeId,
    pub name: String,
    pub processes: Vec<ProcessId>,
    pub extensions: Vec<ExtensionId>,
    pub configuration: RuntimeConfig,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
    pub schema_version: SchemaVersion,
}
```

### Process

```rust
#[derive(Serialize, Deserialize)]
pub struct ProcessPersisted {
    pub id: ProcessId,
    pub name: Option<String>,
    pub parent: Option<ProcessId>,
    pub state: ProcessState,
    pub created_at: Timestamp,
    pub schema_version: SchemaVersion,
}
```

### Buffer

```rust
#[derive(Serialize, Deserialize)]
pub struct BufferPersisted {
    pub id: BufferId,
    pub project_id: ProjectId,
    pub name: String,
    pub path: Option<PathBuf>,
    pub language: Option<String>,
    pub encoding: Encoding,
    pub content: String,  // Serialized rope
    pub is_dirty: bool,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
    pub schema_version: SchemaVersion,
}
```

### Agent

```rust
#[derive(Serialize, Deserialize)]
pub struct AgentPersisted {
    pub id: AgentId,
    pub name: String,
    pub capabilities: Vec<Capability>,
    pub context: AgentContext,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,
    pub schema_version: SchemaVersion,
}
```

### Audit Log

```rust
#[derive(Serialize, Deserialize)]
pub struct AuditEntryPersisted {
    pub entry_id: MessageId,
    pub transaction_id: TransactionId,
    pub agent_id: Option<AgentId>,
    pub action: AuditAction,
    pub timestamp: Timestamp,
    pub details: String,
    pub schema_version: SchemaVersion,
}
```

## Recovery

### Recovery Process

1. Load persisted state
2. Validate schema version
3. Run migrations if needed
4. Reconstruct runtime state
5. Verify consistency

```rust
impl Runtime {
    pub async fn recover(&mut self) -> Result<(), RecoveryError> {
        // Load runtime
        let runtime_data = self.storage.get(b"runtime")?;
        let runtime: RuntimePersisted = if let Some(data) = runtime_data {
            let migrated = self.schema_registry.migrate("runtime", data)?;
            bincode::deserialize(&migrated)?
        } else {
            RuntimePersisted::new()
        };

        // Load processes
        for process_id in &runtime.processes {
            let process_data = self.storage.get(&process_id.to_bytes())?;
            if let Some(data) = process_data {
                let migrated = self.schema_registry.migrate("process", data)?;
                let process: ProcessPersisted = bincode::deserialize(&migrated)?;
                self.restore_process(process)?;
            }
        }

        // Load extensions
        for extension_id in &runtime.extensions {
            let extension_data = self.storage.get(&extension_id.to_bytes())?;
            if let Some(data) = extension_data {
                let migrated = self.schema_registry.migrate("extension", data)?;
                let extension: ExtensionPersisted = bincode::deserialize(&migrated)?;
                self.restore_extension(extension)?;
            }
        }

        // Load audit log
        self.load_audit_log()?;

        Ok(())
    }
}
```

### Incomplete Transaction Recovery

```rust
impl Runtime {
    async fn recover_incomplete_transactions(&mut self) -> Result<(), RecoveryError> {
        let incomplete = self.storage.list(b"transaction:pending:")?;

        for tx_data in incomplete {
            let tx: TransactionPersisted = bincode::deserialize(&tx_data)?;

            match tx.status {
                TransactionStatus::Committing => {
                    // Transaction was interrupted during commit
                    // Rollback to be safe
                    tracing::warn!("Rolling back incomplete transaction: {}", tx.id);
                    self.rollback_transaction(tx.id).await?;
                }
                TransactionStatus::Pending => {
                    // Transaction was never started
                    // Discard
                    tracing::warn!("Discarding pending transaction: {}", tx.id);
                    self.discard_transaction(tx.id).await?;
                }
                _ => {}
            }
        }

        Ok(())
    }
}
```

## Consistency

### Write-Ahead Log

Critical operations use write-ahead logging.

```rust
pub struct WriteAheadLog {
    storage: Box<dyn Storage>,
}

impl WriteAheadLog {
    pub async fn begin(&mut self, operation_id: OperationId) -> Result<(), WALError> {
        self.storage.put(
            &format!("wal:{}", operation_id).as_bytes(),
            b"pending",
        ).await
    }

    pub async fn commit(&mut self, operation_id: OperationId) -> Result<(), WALError> {
        self.storage.delete(&format!("wal:{}", operation_id).as_bytes()).await
    }

    pub async fn rollback(&mut self, operation_id: OperationId) -> Result<(), WALError> {
        self.storage.delete(&format!("wal:{}", operation_id).as_bytes()).await
    }

    pub async fn recover(&self) -> Result<Vec<OperationId>, WALError> {
        let pending = self.storage.list(b"wal:")?;
        Ok(pending.into_iter().map(|k| OperationId::from_bytes(&k)).collect())
    }
}
```

### Checkpointing

Periodic snapshots for fast recovery.

```rust
pub struct Checkpointer {
    storage: Box<dyn Storage>,
    interval: Duration,
}

impl Checkpointer {
    pub async fn checkpoint(&self, state: &RuntimeState) -> Result<(), CheckpointError> {
        let snapshot = state.snapshot()?;
        let data = bincode::serialize(&snapshot)?;

        self.storage.put(b"checkpoint:latest", &data).await?;

        Ok(())
    }

    pub async fn restore(&self) -> Result<Option<RuntimeState>, CheckpointError> {
        let data = self.storage.get(b"checkpoint:latest")?;

        match data {
            Some(bytes) => {
                let snapshot: RuntimeSnapshot = bincode::deserialize(&bytes)?;
                Ok(Some(snapshot.restore()))
            }
            None => Ok(None),
        }
    }
}
```

## Export/Import

### Export

```rust
impl Runtime {
    pub async fn export(&self, path: &Path) -> Result<(), ExportError> {
        let export_data = ExportData {
            workspace: self.workspace.persist()?,
            projects: self.projects.iter().map(|p| p.persist()).collect(),
            agents: self.agents.iter().map(|a| a.persist()).collect(),
            audit_log: self.audit_log.export()?,
            schema_version: CURRENT_SCHEMA_VERSION,
        };

        let data = bincode::serialize(&export_data)?;
        tokio::fs::write(path, data).await?;

        Ok(())
    }
}
```

### Import

```rust
impl Runtime {
    pub async fn import(&mut self, path: &Path) -> Result<(), ImportError> {
        let data = tokio::fs::read(path).await?;
        let export_data: ExportData = bincode::deserialize(&data)?;

        // Migrate if needed
        let migrated = self.schema_registry.migrate_export(export_data)?;

        // Import workspace
        self.workspace = Workspace::from_persisted(migrated.workspace)?;

        // Import projects
        for project_data in migrated.projects {
            let project = Project::from_persisted(project_data)?;
            self.projects.insert(project.id, project);
        }

        // Import agents
        for agent_data in migrated.agents {
            let agent = Agent::from_persisted(agent_data)?;
            self.agents.insert(agent.id, agent);
        }

        // Import audit log
        self.audit_log.import(migrated.audit_log)?;

        Ok(())
    }
}
```

## Observability

### Persistence Metrics

```rust
pub struct PersistenceMetrics {
    pub reads: u64,
    pub writes: u64,
    pub deletes: u64,
    pub transactions: u64,
    pub migrations: u64,
    pub checkpoints: u64,
    pub recovery_time: Duration,
}
```

### Storage Health

```rust
pub struct StorageHealth {
    pub total_keys: usize,
    pub total_bytes: usize,
    pub fragmentation: f64,
    pub last_checkpoint: Option<Timestamp>,
    pub pending_wal_entries: usize,
}
```