arcan-lago 0.3.0

Bridge between Arcan agent runtime and Lago event-sourced persistence
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
//! Sandbox event sinks for the Lago persistence layer (BRO-257).
//!
//! # Sinks
//!
//! - [`LagoSandboxEventSink`] — writes `SandboxEvent`s to a Postgres database
//!   (the `sandbox_events`, `sandbox_instances`, and `sandbox_snapshots` tables
//!   created by migration `0001_sandbox_metadata.sql`).  Events are sent to a
//!   background tokio task via an unbounded channel so `emit()` is always
//!   synchronous and cheap.
//! - [`LagoSandboxEventSink::spawn_with_manifest`] — full sink with blob store
//!   and manifest sync for `FileWritten` events.
//!
//! # Schema
//!
//! Migration:
//! `apps/chatOS/packages/db/drizzle/0001_sandbox_metadata.sql`

use std::sync::{Arc, Mutex};

use arcan_sandbox::{SandboxEvent, SandboxEventKind, SandboxEventSink, SandboxProvider};
use lago_store::BlobStore;
use sqlx::PgPool;
use tokio::sync::mpsc;
use tracing::{debug, warn};

use crate::sandbox_manifest::{FileWrittenParams, SandboxManifest, sync_file_written};

// ── LagoSandboxEventSink ──────────────────────────────────────────────────────

/// An [`arcan_sandbox::SandboxEventSink`] that persists events to Postgres
/// and optionally syncs file content to the Lago blob store.
///
/// Uses an unbounded `mpsc` channel so `emit()` never blocks.  The background
/// task runs sqlx queries against the three sandbox metadata tables.
///
/// # Construction
///
/// - [`LagoSandboxEventSink::spawn`] — Postgres persistence only.
/// - [`LagoSandboxEventSink::spawn_with_manifest`] — Postgres + blob store
///   + in-memory file manifest sync for `FileWritten` events.
pub struct LagoSandboxEventSink {
    tx: mpsc::UnboundedSender<SandboxEvent>,
}

impl LagoSandboxEventSink {
    /// Spawn the background writer and return the sink.
    pub fn spawn(pool: PgPool) -> Self {
        let (tx, mut rx) = mpsc::unbounded_channel::<SandboxEvent>();

        tokio::spawn(async move {
            while let Some(event) = rx.recv().await {
                if let Err(e) = write_event(&pool, &event).await {
                    warn!(
                        sandbox_id = %event.sandbox_id,
                        kind = ?event.kind,
                        error = %e,
                        "sandbox event write failed"
                    );
                } else {
                    debug!(
                        sandbox_id = %event.sandbox_id,
                        kind = ?event.kind,
                        "sandbox event persisted"
                    );
                }
            }
        });

        Self { tx }
    }

    /// Spawn a full sink with Postgres persistence + Lago blob store +
    /// in-memory file manifest.
    ///
    /// On `FileWritten` events the background task calls
    /// `provider.read_file()` and stores the content in `blob_store`
    /// (content-addressed, deduplicated). If `read_file` is not supported by
    /// the provider, the manifest entry is still recorded using the SHA-256
    /// hash pre-computed by [`JournaledSandboxProvider`].
    ///
    /// Returns both the sink and a shared handle to the manifest so callers
    /// can query it.
    pub fn spawn_with_manifest(
        pool: PgPool,
        blob_store: Arc<BlobStore>,
        provider: Arc<dyn SandboxProvider>,
    ) -> (Self, Arc<Mutex<SandboxManifest>>) {
        let manifest = Arc::new(Mutex::new(SandboxManifest::new()));
        let manifest_bg = Arc::clone(&manifest);
        let (tx, mut rx) = mpsc::unbounded_channel::<SandboxEvent>();

        tokio::spawn(async move {
            while let Some(event) = rx.recv().await {
                // Always persist to Postgres.
                if let Err(e) = write_event(&pool, &event).await {
                    warn!(
                        sandbox_id = %event.sandbox_id,
                        kind = ?event.kind,
                        error = %e,
                        "sandbox event write failed"
                    );
                }

                // For FileWritten, also sync to blob store + manifest.
                if let SandboxEventKind::FileWritten {
                    ref path,
                    size_bytes,
                    ref sha256,
                    mode,
                } = event.kind
                {
                    let entry = sync_file_written(FileWrittenParams {
                        sandbox_id: &event.sandbox_id,
                        session_id: &event.session_id,
                        path,
                        size_bytes,
                        sha256,
                        mode,
                        provider: &provider,
                        blob_store: &blob_store,
                        provider_name: &event.provider,
                    })
                    .await;

                    manifest_bg
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .upsert(entry);
                }
            }
        });

        (Self { tx }, manifest)
    }
}

impl SandboxEventSink for LagoSandboxEventSink {
    fn emit(&self, event: SandboxEvent) {
        if let Err(e) = self.tx.send(event) {
            warn!("LagoSandboxEventSink: background channel closed: {e}");
        }
    }
}

// ── write_event ───────────────────────────────────────────────────────────────

/// Write a single `SandboxEvent` to the database.
///
/// Always inserts a row into `sandbox_events`.  Additionally upserts
/// `sandbox_instances` or appends to `sandbox_snapshots` depending on kind.
async fn write_event(pool: &PgPool, event: &SandboxEvent) -> Result<(), sqlx::Error> {
    let (exit_code, duration_ms, snapshot_ref, error_msg) = match &event.kind {
        SandboxEventKind::ExecCompleted {
            exit_code,
            duration_ms,
        } => (
            Some(*exit_code),
            Some(*duration_ms as i64),
            None::<String>,
            None::<String>,
        ),
        SandboxEventKind::Snapshotted { snapshot_id } => {
            (None, None, Some(snapshot_id.clone()), None)
        }
        SandboxEventKind::Resumed { from_snapshot } => {
            (None, None, Some(from_snapshot.clone()), None)
        }
        SandboxEventKind::Failed { reason } => (None, None, None, Some(reason.clone())),
        _ => (None, None, None, None),
    };

    let kind_str = event_kind_str(&event.kind);

    // 1. Insert into sandbox_events (always).
    sqlx::query(
        r#"
        INSERT INTO sandbox_events
            (sandbox_id, agent_id, session_id, organization_id, provider,
             event_kind, exit_code, duration_ms, snapshot_id, error_message,
             occurred_at)
        VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
        "#,
    )
    .bind(&event.sandbox_id.0)
    .bind(&event.agent_id)
    .bind(&event.session_id)
    .bind(&event.agent_id) // organization_id: use agent_id as surrogate until org_id is in SandboxEvent
    .bind(&event.provider)
    .bind(kind_str)
    .bind(exit_code)
    .bind(duration_ms)
    .bind(&snapshot_ref)
    .bind(&error_msg)
    .bind(event.timestamp)
    .execute(pool)
    .await?;

    // 2. Update sandbox_instances based on event kind.
    match &event.kind {
        SandboxEventKind::Created => {
            sqlx::query(
                r#"
                INSERT INTO sandbox_instances
                    (sandbox_id, agent_id, session_id, organization_id,
                     provider, status, created_at)
                VALUES ($1, $2, $3, $4, $5, 'starting', $6)
                ON CONFLICT (sandbox_id) DO NOTHING
                "#,
            )
            .bind(&event.sandbox_id.0)
            .bind(&event.agent_id)
            .bind(&event.session_id)
            .bind(&event.agent_id)
            .bind(&event.provider)
            .bind(event.timestamp)
            .execute(pool)
            .await?;
        }
        SandboxEventKind::Started => {
            sqlx::query("UPDATE sandbox_instances SET status = 'running' WHERE sandbox_id = $1")
                .bind(&event.sandbox_id.0)
                .execute(pool)
                .await?;
        }
        SandboxEventKind::ExecCompleted { .. } => {
            sqlx::query("UPDATE sandbox_instances SET last_exec_at = $2 WHERE sandbox_id = $1")
                .bind(&event.sandbox_id.0)
                .bind(event.timestamp)
                .execute(pool)
                .await?;
        }
        SandboxEventKind::Snapshotted { snapshot_id } => {
            sqlx::query(
                "UPDATE sandbox_instances SET status = 'snapshotted' WHERE sandbox_id = $1",
            )
            .bind(&event.sandbox_id.0)
            .execute(pool)
            .await?;

            sqlx::query(
                r#"
                INSERT INTO sandbox_snapshots
                    (sandbox_id, snapshot_id, trigger, created_at)
                VALUES ($1, $2, 'idle_reaper', $3)
                "#,
            )
            .bind(&event.sandbox_id.0)
            .bind(snapshot_id)
            .bind(event.timestamp)
            .execute(pool)
            .await?;
        }
        SandboxEventKind::Resumed { from_snapshot } => {
            sqlx::query("UPDATE sandbox_instances SET status = 'running' WHERE sandbox_id = $1")
                .bind(&event.sandbox_id.0)
                .execute(pool)
                .await?;

            sqlx::query(
                r#"
                INSERT INTO sandbox_snapshots
                    (sandbox_id, snapshot_id, trigger, created_at)
                VALUES ($1, $2, 'resumed', $3)
                "#,
            )
            .bind(&event.sandbox_id.0)
            .bind(from_snapshot)
            .bind(event.timestamp)
            .execute(pool)
            .await?;
        }
        SandboxEventKind::Destroyed => {
            sqlx::query(
                r#"UPDATE sandbox_instances
                   SET status = 'stopped', destroyed_at = $2
                   WHERE sandbox_id = $1"#,
            )
            .bind(&event.sandbox_id.0)
            .bind(event.timestamp)
            .execute(pool)
            .await?;
        }
        SandboxEventKind::Failed { reason } => {
            sqlx::query(
                r#"UPDATE sandbox_instances
                   SET status = 'failed',
                       metadata = metadata || jsonb_build_object('error', $2::text)
                   WHERE sandbox_id = $1"#,
            )
            .bind(&event.sandbox_id.0)
            .bind(reason)
            .execute(pool)
            .await?;
        }
        SandboxEventKind::FileWritten { .. } => {
            // File manifest tracking is handled by the manifest sync path;
            // no sandbox_instances status change needed.
        }
    }

    Ok(())
}

fn event_kind_str(kind: &SandboxEventKind) -> &'static str {
    match kind {
        SandboxEventKind::Created => "created",
        SandboxEventKind::Started => "started",
        SandboxEventKind::ExecCompleted { .. } => "exec_completed",
        SandboxEventKind::Snapshotted { .. } => "snapshotted",
        SandboxEventKind::Resumed { .. } => "resumed",
        SandboxEventKind::Destroyed => "destroyed",
        SandboxEventKind::Failed { .. } => "failed",
        SandboxEventKind::FileWritten { .. } => "file_written",
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use arcan_sandbox::{SandboxEventKind, SandboxId};

    fn make_event(kind: SandboxEventKind) -> SandboxEvent {
        SandboxEvent::now(
            SandboxId("test-sandbox".into()),
            "agent-1",
            "sess-1",
            kind,
            "local",
        )
    }

    #[test]
    fn event_kind_str_covers_all_variants() {
        assert_eq!(event_kind_str(&SandboxEventKind::Created), "created");
        assert_eq!(event_kind_str(&SandboxEventKind::Started), "started");
        assert_eq!(
            event_kind_str(&SandboxEventKind::ExecCompleted {
                exit_code: 0,
                duration_ms: 1
            }),
            "exec_completed"
        );
        assert_eq!(
            event_kind_str(&SandboxEventKind::Snapshotted {
                snapshot_id: "s".into()
            }),
            "snapshotted"
        );
        assert_eq!(
            event_kind_str(&SandboxEventKind::Resumed {
                from_snapshot: "s".into()
            }),
            "resumed"
        );
        assert_eq!(event_kind_str(&SandboxEventKind::Destroyed), "destroyed");
        assert_eq!(
            event_kind_str(&SandboxEventKind::Failed {
                reason: "oom".into()
            }),
            "failed"
        );
        assert_eq!(
            event_kind_str(&SandboxEventKind::FileWritten {
                path: "/f".into(),
                size_bytes: 0,
                sha256: "abc".into(),
                mode: 0o644,
            }),
            "file_written"
        );
    }

    #[tokio::test]
    async fn emit_does_not_panic_without_pool() {
        // Verify that the sink can be used without a real DB.
        // We test this by calling emit on a custom sink that just discards.
        use arcan_sandbox::sink::NoopSink;
        let sink = NoopSink;
        sink.emit(make_event(SandboxEventKind::Created));
        sink.emit(make_event(SandboxEventKind::Destroyed));
    }

    #[tokio::test]
    async fn file_written_event_updates_manifest() {
        use arcan_sandbox::{
            ExecRequest, ExecResult, SandboxCapabilitySet, SandboxHandle, SandboxInfo, SandboxSpec,
            SnapshotId,
        };
        use async_trait::async_trait;

        struct StubReadProvider;

        #[async_trait]
        impl SandboxProvider for StubReadProvider {
            fn name(&self) -> &'static str {
                "stub"
            }
            fn capabilities(&self) -> SandboxCapabilitySet {
                SandboxCapabilitySet::FILESYSTEM_READ | SandboxCapabilitySet::FILESYSTEM_WRITE
            }
            async fn create(
                &self,
                _: SandboxSpec,
            ) -> Result<SandboxHandle, arcan_sandbox::SandboxError> {
                unreachable!("not called in test")
            }
            async fn resume(
                &self,
                _: &SandboxId,
            ) -> Result<SandboxHandle, arcan_sandbox::SandboxError> {
                unreachable!("not called in test")
            }
            async fn run(
                &self,
                _: &SandboxId,
                _: ExecRequest,
            ) -> Result<ExecResult, arcan_sandbox::SandboxError> {
                unreachable!("not called in test")
            }
            async fn snapshot(
                &self,
                _: &SandboxId,
            ) -> Result<SnapshotId, arcan_sandbox::SandboxError> {
                unreachable!("not called in test")
            }
            async fn destroy(&self, _: &SandboxId) -> Result<(), arcan_sandbox::SandboxError> {
                Ok(())
            }
            async fn list(&self) -> Result<Vec<SandboxInfo>, arcan_sandbox::SandboxError> {
                Ok(vec![])
            }
            async fn read_file(
                &self,
                _: &SandboxId,
                _: &str,
            ) -> Result<Vec<u8>, arcan_sandbox::SandboxError> {
                Ok(b"content".to_vec())
            }
        }

        let dir = tempfile::tempdir().unwrap();
        let blob_store = Arc::new(BlobStore::open(dir.path().join("blobs")).unwrap());
        let provider: Arc<dyn SandboxProvider> = Arc::new(StubReadProvider);
        // Use a pool-less path: test only the manifest sync by creating
        // a logging-only background task that doesn't need Postgres.
        let manifest = Arc::new(Mutex::new(SandboxManifest::new()));
        let manifest_bg = Arc::clone(&manifest);
        let blob_store_bg = Arc::clone(&blob_store);
        let (tx, mut rx) = mpsc::unbounded_channel::<SandboxEvent>();

        tokio::spawn(async move {
            while let Some(event) = rx.recv().await {
                if let SandboxEventKind::FileWritten {
                    ref path,
                    size_bytes,
                    ref sha256,
                    mode,
                } = event.kind
                {
                    let entry = sync_file_written(FileWrittenParams {
                        sandbox_id: &event.sandbox_id,
                        session_id: &event.session_id,
                        path,
                        size_bytes,
                        sha256,
                        mode,
                        provider: &provider,
                        blob_store: &blob_store_bg,
                        provider_name: &event.provider,
                    })
                    .await;

                    manifest_bg
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .upsert(entry);
                }
            }
        });

        tx.send(SandboxEvent::now(
            SandboxId("box-1".into()),
            "agent-1",
            "sess-1",
            SandboxEventKind::FileWritten {
                path: "/workspace/hello.py".into(),
                size_bytes: 7,
                sha256: "some-hash".into(),
                mode: 0o644,
            },
            "stub",
        ))
        .unwrap();

        // Allow the background task to process.
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        let m = manifest.lock().unwrap();
        let entry = m.get(&SandboxId("box-1".into()), "/workspace/hello.py");
        assert!(entry.is_some());
        let entry = entry.unwrap();
        assert_eq!(entry.path, "/workspace/hello.py");
        assert_eq!(entry.session_id, "sess-1");
        assert!(entry.blob_hash.is_some());
    }
}