llm-memory-graph 0.1.0

Graph-based context-tracking and prompt-lineage database for LLM systems
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
//! Integration tests for the plugin system
//!
//! Tests plugin registration, lifecycle management, hook execution,
//! and interaction with the core memory graph functionality.

use async_trait::async_trait;
use llm_memory_graph::plugin::{
    HookExecutor, HookPoint, Plugin, PluginBuilder, PluginContext, PluginError, PluginManager,
    PluginMetadata, PluginRegistry,
};
use serde_json::json;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;

// Test plugin that tracks hook calls
struct CountingPlugin {
    metadata: PluginMetadata,
    before_create_node_count: Arc<AtomicUsize>,
    after_create_node_count: Arc<AtomicUsize>,
}

impl CountingPlugin {
    fn new(name: &str) -> Self {
        let metadata = PluginBuilder::new(name, "1.0.0")
            .author("Test")
            .description("Counting plugin for tests")
            .capability("testing")
            .build();

        Self {
            metadata,
            before_create_node_count: Arc::new(AtomicUsize::new(0)),
            after_create_node_count: Arc::new(AtomicUsize::new(0)),
        }
    }

    fn get_before_count(&self) -> usize {
        self.before_create_node_count.load(Ordering::Relaxed)
    }

    fn get_after_count(&self) -> usize {
        self.after_create_node_count.load(Ordering::Relaxed)
    }
}

#[async_trait]
impl Plugin for CountingPlugin {
    fn metadata(&self) -> &PluginMetadata {
        &self.metadata
    }

    async fn before_create_node(&self, _context: &PluginContext) -> Result<(), PluginError> {
        self.before_create_node_count
            .fetch_add(1, Ordering::Relaxed);
        Ok(())
    }

    async fn after_create_node(&self, _context: &PluginContext) -> Result<(), PluginError> {
        self.after_create_node_count.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }
}

// Test plugin that fails on demand
struct FailingPlugin {
    metadata: PluginMetadata,
    should_fail: Arc<AtomicUsize>,
}

impl FailingPlugin {
    fn new(name: &str) -> Self {
        let metadata = PluginBuilder::new(name, "1.0.0")
            .author("Test")
            .description("Failing plugin for tests")
            .capability("testing")
            .build();

        Self {
            metadata,
            should_fail: Arc::new(AtomicUsize::new(0)),
        }
    }

    fn set_fail(&self, should_fail: bool) {
        self.should_fail
            .store(should_fail as usize, Ordering::Relaxed);
    }
}

#[async_trait]
impl Plugin for FailingPlugin {
    fn metadata(&self) -> &PluginMetadata {
        &self.metadata
    }

    async fn before_create_node(&self, _context: &PluginContext) -> Result<(), PluginError> {
        if self.should_fail.load(Ordering::Relaxed) != 0 {
            Err(PluginError::HookFailed("Intentional failure".to_string()))
        } else {
            Ok(())
        }
    }
}

// ============================================================================
// Plugin Manager Tests
// ============================================================================

#[tokio::test]
async fn test_plugin_registration() {
    let mut manager = PluginManager::new();
    let plugin: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("test_plugin"));

    assert!(manager.register(Arc::clone(&plugin)).await.is_ok());
    assert!(manager.get_state("test_plugin").is_some());
}

#[tokio::test]
async fn test_plugin_duplicate_registration() {
    let mut manager = PluginManager::new();
    let plugin1: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("test_plugin"));
    let plugin2: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("test_plugin"));

    assert!(manager.register(plugin1).await.is_ok());
    assert!(manager.register(plugin2).await.is_err());
}

#[tokio::test]
async fn test_plugin_lifecycle() {
    let mut manager = PluginManager::new();
    let plugin: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("test_plugin"));

    manager.register(Arc::clone(&plugin)).await.unwrap();
    assert!(manager.get_state("test_plugin").is_some());

    manager.initialize("test_plugin").await.unwrap();
    manager.enable("test_plugin").unwrap();
    assert!(manager.is_enabled("test_plugin"));

    manager.disable("test_plugin").unwrap();
    assert!(!manager.is_enabled("test_plugin"));
}

#[tokio::test]
async fn test_multiple_plugins() {
    let mut manager = PluginManager::new();
    let plugin1: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("plugin1"));
    let plugin2: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("plugin2"));

    manager.register(Arc::clone(&plugin1)).await.unwrap();
    manager.register(Arc::clone(&plugin2)).await.unwrap();

    manager.init_all().await.unwrap();
    manager.enable_all().unwrap();

    let active = manager.active_plugins();
    assert_eq!(active.len(), 2);
}

#[tokio::test]
async fn test_hook_execution() {
    let mut manager = PluginManager::new();
    let counter_plugin = Arc::new(CountingPlugin::new("test_plugin"));
    let plugin: Arc<dyn Plugin> = Arc::clone(&counter_plugin) as Arc<dyn Plugin>;

    manager.register(Arc::clone(&plugin)).await.unwrap();
    manager.initialize("test_plugin").await.unwrap();
    manager.enable("test_plugin").unwrap();

    let context = PluginContext::new("test", json!({"content": "test"}));

    manager
        .execute_before_hooks("before_create_node", &context)
        .await
        .unwrap();

    assert_eq!(counter_plugin.get_before_count(), 1);
}

#[tokio::test]
async fn test_hook_execution_failure() {
    let mut manager = PluginManager::new();
    let failing_plugin = Arc::new(FailingPlugin::new("failing_plugin"));
    let plugin: Arc<dyn Plugin> = Arc::clone(&failing_plugin) as Arc<dyn Plugin>;

    manager.register(Arc::clone(&plugin)).await.unwrap();
    manager.initialize("failing_plugin").await.unwrap();
    manager.enable("failing_plugin").unwrap();

    failing_plugin.set_fail(true);

    let context = PluginContext::new("test", json!({"content": "test"}));

    let result = manager
        .execute_before_hooks("before_create_node", &context)
        .await;

    assert!(result.is_err());
}

#[tokio::test]
async fn test_shutdown_all() {
    let mut manager = PluginManager::new();
    let plugin1: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("plugin1"));
    let plugin2: Arc<dyn Plugin> = Arc::new(CountingPlugin::new("plugin2"));

    manager.register(plugin1).await.unwrap();
    manager.register(plugin2).await.unwrap();

    manager.shutdown_all().await.unwrap();

    // After shutdown, no plugins should be available
    assert_eq!(manager.active_plugins().len(), 0);
}

// ============================================================================
// Plugin Registry Tests
// ============================================================================

#[test]
fn test_registry_registration() {
    let mut registry = PluginRegistry::new();
    let metadata = PluginBuilder::new("test_plugin", "1.0.0")
        .author("Test")
        .description("Test plugin")
        .capability("testing")
        .build();

    assert!(registry.register(metadata, None).is_ok());
    assert!(registry.contains("test_plugin"));
}

#[test]
fn test_registry_find_by_capability() {
    let mut registry = PluginRegistry::new();

    let metadata1 = PluginBuilder::new("plugin1", "1.0.0")
        .capability("validation")
        .build();

    let metadata2 = PluginBuilder::new("plugin2", "1.0.0")
        .capability("validation")
        .capability("enrichment")
        .build();

    let metadata3 = PluginBuilder::new("plugin3", "1.0.0")
        .capability("enrichment")
        .build();

    registry.register(metadata1, None).unwrap();
    registry.register(metadata2, None).unwrap();
    registry.register(metadata3, None).unwrap();

    let validation_plugins = registry.find_by_capability("validation");
    assert_eq!(validation_plugins.len(), 2);

    let enrichment_plugins = registry.find_by_capability("enrichment");
    assert_eq!(enrichment_plugins.len(), 2);
}

#[test]
fn test_registry_tags() {
    let mut registry = PluginRegistry::new();
    let metadata = PluginBuilder::new("test_plugin", "1.0.0").build();

    registry.register(metadata, None).unwrap();
    registry.add_tag("test_plugin", "production").unwrap();
    registry.add_tag("test_plugin", "critical").unwrap();

    let tagged = registry.find_by_tag("production");
    assert_eq!(tagged.len(), 1);

    registry.remove_tag("test_plugin", "production").unwrap();
    let tagged = registry.find_by_tag("production");
    assert_eq!(tagged.len(), 0);
}

#[test]
fn test_registry_stats() {
    let mut registry = PluginRegistry::new();

    let metadata1 = PluginBuilder::new("plugin1", "1.0.0")
        .capability("validation")
        .build();

    let metadata2 = PluginBuilder::new("plugin2", "1.0.0")
        .capability("validation")
        .capability("enrichment")
        .build();

    registry.register(metadata1, None).unwrap();
    registry.register(metadata2, None).unwrap();

    registry.mark_loaded("plugin1").unwrap();

    let stats = registry.stats();
    assert_eq!(stats.total_plugins, 2);
    assert_eq!(stats.loaded_plugins, 1);
    assert_eq!(stats.unloaded_plugins, 1);
    assert_eq!(*stats.capabilities.get("validation").unwrap(), 2);
    assert_eq!(*stats.capabilities.get("enrichment").unwrap(), 1);
}

// ============================================================================
// Hook Executor Tests
// ============================================================================

#[tokio::test]
async fn test_hook_executor() {
    let executor = HookExecutor::new();
    let counter_plugin = Arc::new(CountingPlugin::new("test_plugin"));
    let plugins: Vec<Arc<dyn Plugin>> = vec![Arc::clone(&counter_plugin) as Arc<dyn Plugin>];

    let context = PluginContext::new("test", json!({}));

    executor
        .execute_before(HookPoint::BeforeCreateNode, &plugins, &context)
        .await
        .unwrap();

    assert_eq!(counter_plugin.get_before_count(), 1);
}

#[tokio::test]
async fn test_hook_executor_fail_fast() {
    let executor = HookExecutor::new();
    let plugin1 = Arc::new(CountingPlugin::new("plugin1"));
    let failing_plugin = Arc::new(FailingPlugin::new("plugin2"));
    let plugin3 = Arc::new(CountingPlugin::new("plugin3"));

    failing_plugin.set_fail(true);

    let plugins: Vec<Arc<dyn Plugin>> = vec![
        Arc::clone(&plugin1) as Arc<dyn Plugin>,
        Arc::clone(&failing_plugin) as Arc<dyn Plugin>,
        Arc::clone(&plugin3) as Arc<dyn Plugin>,
    ];

    let context = PluginContext::new("test", json!({}));

    let result = executor
        .execute_before(HookPoint::BeforeCreateNode, &plugins, &context)
        .await;

    assert!(result.is_err());

    // First plugin should have executed
    assert_eq!(plugin1.get_before_count(), 1);

    // Third plugin should NOT have executed (fail-fast)
    assert_eq!(plugin3.get_before_count(), 0);
}

#[tokio::test]
async fn test_hook_executor_after_hooks() {
    let executor = HookExecutor::new();
    let plugin1 = Arc::new(CountingPlugin::new("plugin1"));
    let failing_plugin = Arc::new(FailingPlugin::new("plugin2"));
    let plugin3 = Arc::new(CountingPlugin::new("plugin3"));

    failing_plugin.set_fail(true);

    let plugins: Vec<Arc<dyn Plugin>> = vec![
        Arc::clone(&plugin1) as Arc<dyn Plugin>,
        Arc::clone(&failing_plugin) as Arc<dyn Plugin>,
        Arc::clone(&plugin3) as Arc<dyn Plugin>,
    ];

    let context = PluginContext::new("test", json!({}));

    // After hooks should not fail even if a plugin fails
    let result = executor
        .execute_after(HookPoint::AfterCreateNode, &plugins, &context)
        .await;

    assert!(result.is_ok());

    // All plugins should have been called
    assert_eq!(plugin1.get_after_count(), 1);
    assert_eq!(plugin3.get_after_count(), 1);
}

// ============================================================================
// Concurrent Access Tests
// ============================================================================

#[tokio::test]
async fn test_concurrent_plugin_manager() {
    let manager = Arc::new(RwLock::new(PluginManager::new()));
    let counter_plugin = Arc::new(CountingPlugin::new("concurrent_plugin"));
    let plugin: Arc<dyn Plugin> = Arc::clone(&counter_plugin) as Arc<dyn Plugin>;

    {
        let mut mgr = manager.write().await;
        mgr.register(Arc::clone(&plugin)).await.unwrap();
        mgr.initialize("concurrent_plugin").await.unwrap();
        mgr.enable("concurrent_plugin").unwrap();
    }

    // Spawn multiple tasks that execute hooks concurrently
    let mut handles = Vec::new();

    for i in 0..10 {
        let mgr = Arc::clone(&manager);
        let handle = tokio::spawn(async move {
            let manager = mgr.read().await;
            let context = PluginContext::new(
                format!("test_{}", i),
                json!({"content": format!("test_{}", i)}),
            );

            manager
                .execute_before_hooks("before_create_node", &context)
                .await
        });
        handles.push(handle);
    }

    // Wait for all tasks
    for handle in handles {
        handle.await.unwrap().unwrap();
    }

    assert_eq!(counter_plugin.get_before_count(), 10);
}

// ============================================================================
// Plugin Context Tests
// ============================================================================

#[test]
fn test_plugin_context_metadata() {
    let context = PluginContext::new("test", json!({"key": "value"}))
        .with_metadata("user", "test_user")
        .with_metadata("environment", "test");

    assert_eq!(context.operation(), "test");
    assert_eq!(context.get_metadata("user"), Some(&"test_user".to_string()));
    assert_eq!(
        context.get_metadata("environment"),
        Some(&"test".to_string())
    );
    assert_eq!(context.get_metadata("nonexistent"), None);
}

#[test]
fn test_plugin_context_data() {
    let data = json!({
        "content": "test content",
        "metadata": {
            "key": "value"
        }
    });

    let context = PluginContext::new("test", data);

    assert_eq!(
        context.data().get("content").and_then(|v| v.as_str()),
        Some("test content")
    );
    assert!(context.data().get("metadata").is_some());
}