symbi-runtime 1.10.0

Agent Runtime System for the Symbi platform
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Integration tests for knowledge-reasoning bridge.
//!
//! Uses a mock `ContextManager` (the knowledge/context trait) with canned
//! responses to verify:
//! 1. Knowledge injection before reasoning
//! 2. `recall_knowledge` tool call handling
//! 3. `store_knowledge` tool call handling
//! 4. Backward compatibility without knowledge bridge
//! 5. Post-loop persistence of learnings

use std::collections::HashMap;
use std::sync::Arc;
use std::time::SystemTime;

use async_trait::async_trait;
use tokio::sync::Mutex;

use symbi_runtime::context::types::*;
use symbi_runtime::context::ContextManager;
use symbi_runtime::reasoning::circuit_breaker::CircuitBreakerRegistry;
use symbi_runtime::reasoning::context_manager::DefaultContextManager;
use symbi_runtime::reasoning::conversation::{Conversation, ConversationMessage};
use symbi_runtime::reasoning::executor::DefaultActionExecutor;
use symbi_runtime::reasoning::inference::*;
use symbi_runtime::reasoning::knowledge_bridge::{KnowledgeBridge, KnowledgeConfig};
use symbi_runtime::reasoning::loop_types::{BufferedJournal, LoopConfig, TerminationReason};
use symbi_runtime::reasoning::policy_bridge::DefaultPolicyGate;
use symbi_runtime::reasoning::reasoning_loop::ReasoningLoopRunner;
use symbi_runtime::types::AgentId;

// ---------------------------------------------------------------------------
// Mock ContextManager
// ---------------------------------------------------------------------------

/// Tracks calls made to the mock and returns canned responses.
struct MockKnowledgeContextManager {
    /// Canned knowledge items returned by search_knowledge
    knowledge_items: Vec<KnowledgeItem>,
    /// Canned context items returned by query_context
    context_items: Vec<ContextItem>,
    /// Records calls to add_knowledge
    added_knowledge: Mutex<Vec<Knowledge>>,
    /// Records calls to update_memory
    memory_updates: Mutex<Vec<Vec<MemoryUpdate>>>,
}

impl MockKnowledgeContextManager {
    fn new() -> Self {
        Self {
            knowledge_items: vec![],
            context_items: vec![],
            added_knowledge: Mutex::new(vec![]),
            memory_updates: Mutex::new(vec![]),
        }
    }

    fn with_knowledge(mut self, items: Vec<KnowledgeItem>) -> Self {
        self.knowledge_items = items;
        self
    }

    fn with_context(mut self, items: Vec<ContextItem>) -> Self {
        self.context_items = items;
        self
    }
}

#[async_trait]
impl ContextManager for MockKnowledgeContextManager {
    async fn store_context(
        &self,
        _agent_id: AgentId,
        _context: AgentContext,
    ) -> Result<ContextId, ContextError> {
        Ok(ContextId::new())
    }

    async fn retrieve_context(
        &self,
        _agent_id: AgentId,
        _session_id: Option<SessionId>,
    ) -> Result<Option<AgentContext>, ContextError> {
        Ok(None)
    }

    async fn query_context(
        &self,
        _agent_id: AgentId,
        _query: ContextQuery,
    ) -> Result<Vec<ContextItem>, ContextError> {
        Ok(self.context_items.clone())
    }

    async fn update_memory(
        &self,
        _agent_id: AgentId,
        memory_updates: Vec<MemoryUpdate>,
    ) -> Result<(), ContextError> {
        self.memory_updates.lock().await.push(memory_updates);
        Ok(())
    }

    async fn add_knowledge(
        &self,
        _agent_id: AgentId,
        knowledge: Knowledge,
    ) -> Result<KnowledgeId, ContextError> {
        self.added_knowledge.lock().await.push(knowledge);
        Ok(KnowledgeId::new())
    }

    async fn search_knowledge(
        &self,
        _agent_id: AgentId,
        _query: &str,
        _limit: usize,
    ) -> Result<Vec<KnowledgeItem>, ContextError> {
        Ok(self.knowledge_items.clone())
    }

    async fn share_knowledge(
        &self,
        _from_agent: AgentId,
        _to_agent: AgentId,
        _knowledge_id: KnowledgeId,
        _access_level: AccessLevel,
    ) -> Result<(), ContextError> {
        Ok(())
    }

    async fn get_shared_knowledge(
        &self,
        _agent_id: AgentId,
    ) -> Result<Vec<SharedKnowledgeRef>, ContextError> {
        Ok(vec![])
    }

    async fn archive_context(
        &self,
        _agent_id: AgentId,
        _before: SystemTime,
    ) -> Result<u32, ContextError> {
        Ok(0)
    }

    async fn get_context_stats(&self, _agent_id: AgentId) -> Result<ContextStats, ContextError> {
        Ok(ContextStats {
            total_memory_items: 0,
            total_knowledge_items: 0,
            total_conversations: 0,
            total_episodes: 0,
            memory_size_bytes: 0,
            last_activity: SystemTime::now(),
            retention_status: RetentionStatus {
                items_to_archive: 0,
                items_to_delete: 0,
                next_cleanup: SystemTime::now(),
            },
        })
    }

    async fn shutdown(&self) -> Result<(), ContextError> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Mock Inference Provider
// ---------------------------------------------------------------------------

struct MockProvider {
    responses: std::sync::Mutex<Vec<InferenceResponse>>,
}

impl MockProvider {
    fn new(responses: Vec<InferenceResponse>) -> Self {
        Self {
            responses: std::sync::Mutex::new(responses),
        }
    }
}

#[async_trait]
impl InferenceProvider for MockProvider {
    async fn complete(
        &self,
        _conversation: &Conversation,
        _options: &InferenceOptions,
    ) -> Result<InferenceResponse, InferenceError> {
        let mut responses = self.responses.lock().unwrap();
        if responses.is_empty() {
            Ok(InferenceResponse {
                content: "Done.".into(),
                tool_calls: vec![],
                finish_reason: FinishReason::Stop,
                usage: Usage::default(),
                model: "mock".into(),
            })
        } else {
            Ok(responses.remove(0))
        }
    }

    fn provider_name(&self) -> &str {
        "mock"
    }
    fn default_model(&self) -> &str {
        "mock-model"
    }
    fn supports_native_tools(&self) -> bool {
        true
    }
    fn supports_structured_output(&self) -> bool {
        true
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn make_runner(
    provider: Arc<dyn InferenceProvider>,
    knowledge_bridge: Option<Arc<KnowledgeBridge>>,
) -> ReasoningLoopRunner {
    ReasoningLoopRunner {
        provider,
        policy_gate: Arc::new(DefaultPolicyGate::permissive()),
        executor: Arc::new(DefaultActionExecutor::default()),
        context_manager: Arc::new(DefaultContextManager::default()),
        circuit_breakers: Arc::new(CircuitBreakerRegistry::default()),
        journal: Arc::new(BufferedJournal::new(1000)),
        knowledge_bridge,
    }
}

fn make_knowledge_item(content: &str, knowledge_type: KnowledgeType) -> KnowledgeItem {
    KnowledgeItem {
        id: KnowledgeId::new(),
        content: content.to_string(),
        knowledge_type,
        confidence: 0.9,
        relevance_score: 0.8,
        source: KnowledgeSource::Experience,
        created_at: SystemTime::now(),
    }
}

fn simple_response(content: &str) -> InferenceResponse {
    InferenceResponse {
        content: content.into(),
        tool_calls: vec![],
        finish_reason: FinishReason::Stop,
        usage: Usage {
            prompt_tokens: 10,
            completion_tokens: 5,
            total_tokens: 15,
        },
        model: "mock".into(),
    }
}

fn tool_call_response(call_id: &str, name: &str, arguments: &str) -> InferenceResponse {
    InferenceResponse {
        content: String::new(),
        tool_calls: vec![ToolCallRequest {
            id: call_id.into(),
            name: name.into(),
            arguments: arguments.into(),
        }],
        finish_reason: FinishReason::ToolCalls,
        usage: Usage {
            prompt_tokens: 10,
            completion_tokens: 10,
            total_tokens: 20,
        },
        model: "mock".into(),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

/// Test 1: Knowledge is injected as a system message before reasoning.
#[tokio::test]
async fn test_knowledge_injection() {
    let mock_cm = Arc::new(
        MockKnowledgeContextManager::new()
            .with_knowledge(vec![make_knowledge_item(
                "Rust is a systems programming language",
                KnowledgeType::Fact,
            )])
            .with_context(vec![ContextItem {
                id: ContextId::new(),
                content: "User prefers concise answers".to_string(),
                item_type: ContextItemType::Memory(MemoryType::Working),
                relevance_score: 0.85,
                timestamp: SystemTime::now(),
                metadata: HashMap::new(),
            }]),
    );

    let bridge = Arc::new(KnowledgeBridge::new(
        mock_cm,
        KnowledgeConfig {
            auto_persist: false,
            ..Default::default()
        },
    ));

    // The LLM should see the injected knowledge context.
    // We capture the conversation by having the mock just respond.
    let provider = Arc::new(MockProvider::new(vec![simple_response(
        "Rust is indeed a systems language.",
    )]));

    let runner = make_runner(provider, Some(bridge));

    let mut conv = Conversation::with_system("You are a test agent.");
    conv.push(ConversationMessage::user(
        "Tell me about Rust programming language",
    ));

    let result = runner
        .run(AgentId::new(), conv, LoopConfig::default())
        .await;

    assert!(matches!(
        result.termination_reason,
        TerminationReason::Completed
    ));
    assert_eq!(result.output, "Rust is indeed a systems language.");

    // Verify the knowledge context was injected into the conversation
    let has_knowledge_msg = result
        .conversation
        .messages()
        .iter()
        .any(|m| m.content.contains("[KNOWLEDGE_CONTEXT]"));
    assert!(
        has_knowledge_msg,
        "Knowledge context message should be present in conversation"
    );
}

/// Test 2: LLM calls `recall_knowledge` and gets results from mock context manager.
#[tokio::test]
async fn test_recall_tool_call() {
    let mock_cm =
        Arc::new(
            MockKnowledgeContextManager::new().with_knowledge(vec![make_knowledge_item(
                "The capital of France is Paris",
                KnowledgeType::Fact,
            )]),
        );

    let bridge = Arc::new(KnowledgeBridge::new(
        mock_cm,
        KnowledgeConfig {
            auto_persist: false,
            ..Default::default()
        },
    ));

    let provider = Arc::new(MockProvider::new(vec![
        // First: LLM calls recall_knowledge
        tool_call_response(
            "call_1",
            "recall_knowledge",
            r#"{"query": "capital of France"}"#,
        ),
        // Second: LLM responds with the answer
        simple_response("The capital of France is Paris."),
    ]));

    let runner = make_runner(provider, Some(bridge));

    let mut conv = Conversation::with_system("You are a geography expert.");
    conv.push(ConversationMessage::user("What is the capital of France?"));

    let result = runner
        .run(AgentId::new(), conv, LoopConfig::default())
        .await;

    assert!(matches!(
        result.termination_reason,
        TerminationReason::Completed
    ));
    assert_eq!(result.output, "The capital of France is Paris.");
    assert_eq!(result.iterations, 2);

    // Verify a tool result message was added for the recall
    // The recall_knowledge tool was called and the knowledge executor intercepted it.
    // Check that the conversation has tool results from call_1.
    let has_call_1_result = result
        .conversation
        .messages()
        .iter()
        .any(|m| m.tool_call_id.as_deref() == Some("call_1"));
    assert!(
        has_call_1_result,
        "Should have a tool result for call_1 (recall_knowledge)"
    );
}

/// Test 3: LLM calls `store_knowledge` and mock context manager receives the data.
#[tokio::test]
async fn test_store_tool_call() {
    let mock_cm = Arc::new(MockKnowledgeContextManager::new());
    let mock_cm_ref = mock_cm.clone();

    let bridge = Arc::new(KnowledgeBridge::new(
        mock_cm,
        KnowledgeConfig {
            auto_persist: false,
            ..Default::default()
        },
    ));

    let provider = Arc::new(MockProvider::new(vec![
        // First: LLM calls store_knowledge
        tool_call_response(
            "call_1",
            "store_knowledge",
            r#"{"subject": "Earth", "predicate": "has", "object": "one moon", "confidence": 0.95}"#,
        ),
        // Second: LLM responds
        simple_response("I've stored that fact."),
    ]));

    let runner = make_runner(provider, Some(bridge));

    let mut conv = Conversation::with_system("You are a science agent.");
    conv.push(ConversationMessage::user(
        "Remember that Earth has one moon",
    ));

    let result = runner
        .run(AgentId::new(), conv, LoopConfig::default())
        .await;

    assert!(matches!(
        result.termination_reason,
        TerminationReason::Completed
    ));
    assert_eq!(result.output, "I've stored that fact.");

    // Verify the knowledge was stored in the mock context manager
    let added = mock_cm_ref.added_knowledge.lock().await;
    assert_eq!(added.len(), 1, "One knowledge item should have been stored");
    match &added[0] {
        Knowledge::Fact(fact) => {
            assert_eq!(fact.subject, "Earth");
            assert_eq!(fact.predicate, "has");
            assert_eq!(fact.object, "one moon");
            assert!((fact.confidence - 0.95).abs() < f32::EPSILON);
        }
        _ => panic!("Expected a Fact knowledge item"),
    }
}

/// Test 4: Runner without knowledge bridge works identically to before.
#[tokio::test]
async fn test_backward_compat() {
    let provider = Arc::new(MockProvider::new(vec![simple_response(
        "No knowledge needed.",
    )]));

    let runner = make_runner(provider, None);

    let mut conv = Conversation::with_system("You are a test agent.");
    conv.push(ConversationMessage::user("Hello"));

    let result = runner
        .run(AgentId::new(), conv, LoopConfig::default())
        .await;

    assert!(matches!(
        result.termination_reason,
        TerminationReason::Completed
    ));
    assert_eq!(result.output, "No knowledge needed.");
    assert_eq!(result.iterations, 1);

    // No knowledge context message should be present
    let has_knowledge_msg = result
        .conversation
        .messages()
        .iter()
        .any(|m| m.content.contains("[KNOWLEDGE_CONTEXT]"));
    assert!(
        !has_knowledge_msg,
        "No knowledge context should be injected without a bridge"
    );
}

/// Test 5: After loop completion, episodic memory is persisted.
#[tokio::test]
async fn test_persist_learnings() {
    let mock_cm = Arc::new(MockKnowledgeContextManager::new());
    let mock_cm_ref = mock_cm.clone();

    let bridge = Arc::new(KnowledgeBridge::new(
        mock_cm,
        KnowledgeConfig {
            auto_persist: true,
            ..Default::default()
        },
    ));

    let provider = Arc::new(MockProvider::new(vec![simple_response(
        "The answer to everything is 42.",
    )]));

    let runner = make_runner(provider, Some(bridge));

    let mut conv = Conversation::with_system("You are a philosopher.");
    conv.push(ConversationMessage::user(
        "What is the answer to everything?",
    ));

    let result = runner
        .run(AgentId::new(), conv, LoopConfig::default())
        .await;

    assert!(matches!(
        result.termination_reason,
        TerminationReason::Completed
    ));

    // Verify that persist_learnings was called (update_memory was invoked)
    let updates = mock_cm_ref.memory_updates.lock().await;
    assert!(
        !updates.is_empty(),
        "Memory updates should have been persisted after loop completion"
    );

    // The persisted memory should contain the assistant's response
    let update = &updates[0][0];
    match &update.target {
        MemoryTarget::Working(key) => {
            assert_eq!(key, "last_conversation_summary");
        }
        other => panic!("Expected Working memory target, got {:?}", other),
    }
    assert!(
        matches!(update.operation, UpdateOperation::Add),
        "Expected Add operation"
    );

    // The data should contain the assistant's message
    let data_str = update.data.as_str().unwrap();
    assert!(
        data_str.contains("42"),
        "Persisted data should contain the assistant's response"
    );
}