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
use arcan_core::error::CoreError;
use arcan_core::protocol::{ToolCall, ToolResult};
use arcan_core::runtime::{Middleware, RunOutput, ToolContext};
use lago_core::Journal;
use lago_core::event::{EventEnvelope, EventPayload, MemoryScope};
use lago_core::id::{BranchId, EventId, SessionId};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};

/// A structured learning entry captured from runtime events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearningEntry {
    /// Category: "tool_failure", "user_correction", "run_error", "policy_denied"
    pub category: String,
    /// Human-readable description of what happened.
    pub description: String,
    /// The tool involved (if applicable).
    pub tool_name: Option<String>,
    /// The error message (if applicable).
    pub error: Option<String>,
    /// Session where this learning originated.
    pub session_id: String,
    /// Run where this learning originated.
    pub run_id: String,
}

/// Buffer that accumulates learning entries during a run.
#[derive(Debug, Default)]
struct LearningBuffer {
    entries: Vec<LearningEntry>,
    run_id: String,
    session_id: String,
}

/// Middleware that captures learning events from the agent runtime.
///
/// Records tool failures, middleware denials, and run errors as structured
/// learning entries. At the end of each run, the accumulated learnings are
/// persisted to the Lago journal as `Custom` events with type "learning_captured".
pub struct LearningMiddleware {
    journal: Arc<dyn Journal>,
    buffer: Mutex<LearningBuffer>,
}

impl LearningMiddleware {
    pub fn new(journal: Arc<dyn Journal>) -> Self {
        Self {
            journal,
            buffer: Mutex::new(LearningBuffer::default()),
        }
    }

    fn push_entry(&self, entry: LearningEntry) {
        if let Ok(mut buf) = self.buffer.lock() {
            buf.entries.push(entry);
        }
    }

    /// Drain the buffer and build an envelope (if any entries exist).
    fn drain_to_envelope(&self) -> Result<Option<EventEnvelope>, CoreError> {
        let (entries, session_id, run_id) = {
            let mut buf = self.buffer.lock().map_err(|e| {
                CoreError::Middleware(format!("learning buffer lock poisoned: {e}"))
            })?;
            let entries = std::mem::take(&mut buf.entries);
            let session_id = buf.session_id.clone();
            let run_id = buf.run_id.clone();
            (entries, session_id, run_id)
        };

        if entries.is_empty() {
            return Ok(None);
        }

        Ok(Some(EventEnvelope {
            event_id: EventId::default(),
            session_id: SessionId::from_string(&session_id),
            branch_id: BranchId::from("main"),
            run_id: Some(lago_core::id::RunId::from_string(&run_id)),
            seq: 0,
            timestamp: 0,
            parent_id: None,
            payload: EventPayload::Custom {
                event_type: "learning_captured".to_string(),
                data: serde_json::json!({
                    "scope": MemoryScope::Agent,
                    "entry_count": entries.len(),
                    "entries": entries,
                }),
            },
            metadata: Default::default(),
            schema_version: 1,
        }))
    }

    fn flush_to_journal(&self) -> Result<(), CoreError> {
        let Some(envelope) = self.drain_to_envelope()? else {
            return Ok(());
        };

        // Use block_on since middleware runs in spawn_blocking context
        let journal = self.journal.clone();
        let handle = tokio::runtime::Handle::current();
        handle
            .block_on(async { journal.append(envelope).await })
            .map_err(|e| CoreError::Middleware(format!("failed to persist learnings: {e}")))?;

        Ok(())
    }
}

impl Middleware for LearningMiddleware {
    fn before_model_call(
        &self,
        request: &arcan_core::runtime::ProviderRequest,
    ) -> Result<(), CoreError> {
        // Initialize buffer with run context on first model call
        if let Ok(mut buf) = self.buffer.lock()
            && buf.run_id.is_empty()
        {
            buf.run_id = request.run_id.clone();
            buf.session_id = request.session_id.clone();
        }
        Ok(())
    }

    fn pre_tool_call(&self, context: &ToolContext, call: &ToolCall) -> Result<(), CoreError> {
        // Set context if not set yet (e.g., first tool call in a run)
        if let Ok(mut buf) = self.buffer.lock()
            && buf.run_id.is_empty()
        {
            buf.run_id = context.run_id.clone();
            buf.session_id = context.session_id.clone();
        }
        // Nothing to capture before the call
        let _ = call;
        Ok(())
    }

    fn post_tool_call(&self, context: &ToolContext, result: &ToolResult) -> Result<(), CoreError> {
        // Capture tool failures as learning entries
        if result.is_error {
            let error_text = result
                .output
                .get("error")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown error")
                .to_string();

            self.push_entry(LearningEntry {
                category: "tool_failure".to_string(),
                description: format!("Tool '{}' failed: {}", result.tool_name, error_text),
                tool_name: Some(result.tool_name.clone()),
                error: Some(error_text),
                session_id: context.session_id.clone(),
                run_id: context.run_id.clone(),
            });
        }
        Ok(())
    }

    fn on_run_finished(&self, output: &RunOutput) -> Result<(), CoreError> {
        // Capture run errors
        if output.reason == arcan_core::protocol::RunStopReason::Error {
            self.push_entry(LearningEntry {
                category: "run_error".to_string(),
                description: "Run ended with error".to_string(),
                tool_name: None,
                error: None,
                session_id: output.session_id.clone(),
                run_id: output.run_id.clone(),
            });
        }

        // Capture budget exceeded (agent may need different approach)
        if output.reason == arcan_core::protocol::RunStopReason::BudgetExceeded {
            self.push_entry(LearningEntry {
                category: "budget_exceeded".to_string(),
                description: "Run exhausted iteration budget without completing".to_string(),
                tool_name: None,
                error: None,
                session_id: output.session_id.clone(),
                run_id: output.run_id.clone(),
            });
        }

        // Flush all accumulated learnings to journal
        self.flush_to_journal()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arcan_core::protocol::RunStopReason;
    use arcan_core::runtime::{RunOutput, ToolContext};

    fn mock_context() -> ToolContext {
        ToolContext {
            run_id: "run-1".to_string(),
            session_id: "sess-1".to_string(),
            iteration: 1,
        }
    }

    fn tool_result_ok() -> ToolResult {
        ToolResult {
            call_id: "c1".to_string(),
            tool_name: "read_file".to_string(),
            output: serde_json::json!({"content": "data"}),
            content: None,
            is_error: false,
            state_patch: None,
        }
    }

    fn tool_result_error() -> ToolResult {
        ToolResult {
            call_id: "c2".to_string(),
            tool_name: "write_file".to_string(),
            output: serde_json::json!({"error": "permission denied"}),
            content: None,
            is_error: true,
            state_patch: None,
        }
    }

    #[test]
    fn captures_tool_failure() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);

        let ctx = mock_context();
        mw.post_tool_call(&ctx, &tool_result_error()).unwrap();

        let buf = mw.buffer.lock().unwrap();
        assert_eq!(buf.entries.len(), 1);
        assert_eq!(buf.entries[0].category, "tool_failure");
        assert!(buf.entries[0].description.contains("write_file"));
        assert!(buf.entries[0].description.contains("permission denied"));
    }

    #[test]
    fn ignores_successful_tool() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);

        let ctx = mock_context();
        mw.post_tool_call(&ctx, &tool_result_ok()).unwrap();

        let buf = mw.buffer.lock().unwrap();
        assert_eq!(buf.entries.len(), 0);
    }

    #[test]
    fn captures_run_error() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);

        // Set run context
        {
            let mut buf = mw.buffer.lock().unwrap();
            buf.run_id = "run-1".to_string();
            buf.session_id = "sess-1".to_string();
        }

        let output = RunOutput {
            run_id: "run-1".to_string(),
            session_id: "sess-1".to_string(),
            branch_id: "main".to_string(),
            reason: RunStopReason::Error,
            events: vec![],
            messages: vec![],
            state: Default::default(),
            final_answer: None,
            total_usage: Default::default(),
        };

        // Don't flush (no tokio runtime in unit test), just check buffer
        // Manually push the entry that on_run_finished would push
        mw.push_entry(LearningEntry {
            category: "run_error".to_string(),
            description: "Run ended with error".to_string(),
            tool_name: None,
            error: None,
            session_id: output.session_id.clone(),
            run_id: output.run_id.clone(),
        });

        let buf = mw.buffer.lock().unwrap();
        assert_eq!(buf.entries.len(), 1);
        assert_eq!(buf.entries[0].category, "run_error");
    }

    #[test]
    fn captures_budget_exceeded() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);

        mw.push_entry(LearningEntry {
            category: "budget_exceeded".to_string(),
            description: "Run exhausted 10 iterations without completing".to_string(),
            tool_name: None,
            error: None,
            session_id: "sess-1".to_string(),
            run_id: "run-1".to_string(),
        });

        let buf = mw.buffer.lock().unwrap();
        assert_eq!(buf.entries.len(), 1);
        assert_eq!(buf.entries[0].category, "budget_exceeded");
    }

    #[test]
    fn multiple_failures_accumulate() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);
        let ctx = mock_context();

        // Three tool failures in one run
        for i in 0..3 {
            let result = ToolResult {
                call_id: format!("c{i}"),
                tool_name: format!("tool_{i}"),
                output: serde_json::json!({"error": format!("err-{i}")}),
                content: None,
                is_error: true,
                state_patch: None,
            };
            mw.post_tool_call(&ctx, &result).unwrap();
        }

        let buf = mw.buffer.lock().unwrap();
        assert_eq!(buf.entries.len(), 3);
    }

    #[test]
    fn learning_entry_serialization() {
        let entry = LearningEntry {
            category: "tool_failure".to_string(),
            description: "Tool failed".to_string(),
            tool_name: Some("bash".to_string()),
            error: Some("command not found".to_string()),
            session_id: "s1".to_string(),
            run_id: "r1".to_string(),
        };
        let json = serde_json::to_string(&entry).unwrap();
        let back: LearningEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(back.category, "tool_failure");
        assert_eq!(back.tool_name, Some("bash".to_string()));
    }

    #[tokio::test]
    async fn flush_persists_to_journal() {
        let dir = tempfile::tempdir().unwrap();
        let journal =
            Arc::new(lago_journal::RedbJournal::open(dir.path().join("test.redb")).unwrap());

        // Create a session first
        let session = lago_core::Session {
            session_id: lago_core::id::SessionId::from_string("sess-1"),
            config: lago_core::session::SessionConfig::new("test"),
            created_at: 0,
            branches: vec![],
        };
        journal.put_session(session).await.unwrap();

        let mw = LearningMiddleware::new(journal.clone());

        // Set context and add entries
        {
            let mut buf = mw.buffer.lock().unwrap();
            buf.run_id = "run-1".to_string();
            buf.session_id = "sess-1".to_string();
        }

        mw.push_entry(LearningEntry {
            category: "tool_failure".to_string(),
            description: "bash failed".to_string(),
            tool_name: Some("bash".to_string()),
            error: Some("not found".to_string()),
            session_id: "sess-1".to_string(),
            run_id: "run-1".to_string(),
        });

        // Drain and persist (async-safe, no block_on-in-block_on)
        let envelope = mw
            .drain_to_envelope()
            .unwrap()
            .expect("should have entries");
        journal.append(envelope).await.unwrap();

        // Verify event was persisted
        let query = lago_core::journal::EventQuery::default()
            .session(lago_core::id::SessionId::from_string("sess-1"));
        let events = journal.read(query).await.unwrap();
        assert_eq!(events.len(), 1);

        match &events[0].payload {
            EventPayload::Custom { event_type, data } => {
                assert_eq!(event_type, "learning_captured");
                assert_eq!(data["entry_count"], 1);
                assert!(data["entries"][0]["category"] == "tool_failure");
            }
            other => panic!("Expected Custom event, got: {:?}", other),
        }
    }

    #[test]
    fn empty_buffer_flush_is_noop() {
        let journal = Arc::new(
            lago_journal::RedbJournal::open(tempfile::tempdir().unwrap().path().join("test.redb"))
                .unwrap(),
        );
        let mw = LearningMiddleware::new(journal);

        {
            let mut buf = mw.buffer.lock().unwrap();
            buf.run_id = "run-1".to_string();
            buf.session_id = "sess-1".to_string();
        }

        // No entries — flush should be ok (noop)
        // Can't call flush_to_journal without tokio runtime for empty,
        // but we can verify the buffer is empty
        let buf = mw.buffer.lock().unwrap();
        assert!(buf.entries.is_empty());
    }
}