do-memory-core 0.1.31

Core episodic learning system for AI agents with pattern extraction, reward scoring, and dual storage backend
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
//! AgentFS external signal provider implementation
//!
//! This module provides a stub implementation for AgentFS SDK integration.
//! The actual `agentfs-sdk` crate exists on crates.io (v0.6.4) but is not
//! currently integrated as a dependency in this project.
//!
//! ## Current Status
//!
//! - **SDK Integrated**: No (stub implementation)
//! - **Functional**: Returns empty signals when enabled (no real data)
//! - **Error Handling**: Returns `SdkUnavailable` error if enabled without SDK
//!
//! ## Future Integration
//!
//! To enable full AgentFS integration:
//! 1. Add `agentfs-sdk = { version = "0.6.4", optional = true }` to Cargo.toml
//! 2. Update feature flag: `agentfs = ["dep:agentfs-sdk"]`
//! 3. Replace stub code with actual SDK calls
//!
//! See ADR-050 for full integration plan.

use async_trait::async_trait;
use chrono::Utc;
use std::collections::HashMap;

use super::{
    ExternalSignalError, ExternalSignalProvider, ExternalSignalSet, ProviderHealth, Result,
    ToolSignal,
};

/// Configuration for AgentFS provider
#[derive(Debug, Clone)]
pub struct AgentFsConfig {
    /// Path to AgentFS SQLite database
    pub db_path: String,
    /// Enable the provider
    pub enabled: bool,
    /// Weight for this provider's signals (0.0-1.0)
    pub external_weight: f32,
    /// Minimum sample size for correlation
    pub min_correlation_samples: usize,
    /// Sanitize parameters before storing
    pub sanitize_parameters: bool,
}

impl AgentFsConfig {
    /// Load configuration from environment variables
    pub fn from_env() -> Result<Self> {
        use std::env;

        let db_path = env::var("AGENTFS_DB_PATH").map_err(|_| {
            ExternalSignalError::ConfigMissing(
                "AGENTFS_DB_PATH environment variable not set".to_string(),
            )
        })?;

        let enabled = env::var("AGENTFS_ENABLED")
            .map(|v| v == "true")
            .unwrap_or(false);

        let external_weight: f32 = env::var("AGENTFS_WEIGHT")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(0.3);

        let min_correlation_samples = env::var("AGENTFS_MIN_SAMPLES")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(10);

        let sanitize_parameters = env::var("AGENTFS_SANITIZE")
            .map(|v| v == "true")
            .unwrap_or(true);

        Ok(Self {
            db_path,
            enabled,
            external_weight: external_weight.clamp(0.0, 1.0),
            min_correlation_samples,
            sanitize_parameters,
        })
    }
}

impl Default for AgentFsConfig {
    fn default() -> Self {
        Self {
            db_path: String::new(),
            enabled: false,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        }
    }
}

/// AgentFS external signal provider
pub struct AgentFsProvider {
    config: AgentFsConfig,
}

impl AgentFsProvider {
    /// Create a new AgentFS provider with configuration
    pub fn new(config: AgentFsConfig) -> Self {
        Self { config }
    }

    /// Get the configuration
    pub fn config(&self) -> &AgentFsConfig {
        &self.config
    }

    /// Sanitize parameters for privacy
    #[allow(dead_code)] // Privacy utility for future external reward API
    fn sanitize_parameters(&self, params: &serde_json::Value) -> serde_json::Value {
        if !self.config.sanitize_parameters {
            return params.clone();
        }

        match params {
            serde_json::Value::Object(map) => {
                let mut sanitized = serde_json::Map::new();
                for (key, _) in map {
                    // Keep keys, redact values
                    sanitized.insert(
                        key.clone(),
                        serde_json::Value::String("[REDACTED]".to_string()),
                    );
                }
                serde_json::Value::Object(sanitized)
            }
            _ => serde_json::Value::String("[REDACTED]".to_string()),
        }
    }
}

#[async_trait]
impl ExternalSignalProvider for AgentFsProvider {
    fn name(&self) -> &'static str {
        "agentfs"
    }

    async fn get_signals(&self, episode: &crate::episode::Episode) -> Result<ExternalSignalSet> {
        // Disabled provider returns empty signals (graceful degradation)
        if !self.config.enabled {
            return Ok(ExternalSignalSet::empty("agentfs"));
        }

        // SDK not integrated - return error if enabled
        // This prevents misleading placeholder data from being used
        return Err(ExternalSignalError::SdkUnavailable(
            "agentfs-sdk not integrated - enable feature and add SDK dependency to use".to_string(),
        ));

        // The following code would be used when SDK is integrated:
        // if self.config.db_path.is_empty() {
        //     return Err(ExternalSignalError::ConfigMissing(
        //         "AgentFS database path not configured".to_string(),
        //     ));
        // }
        // let tool_signals = self.fetch_tool_stats(episode);
        // ... calculate confidence and quality ...
    }

    async fn health_check(&self) -> ProviderHealth {
        // Disabled provider is healthy (graceful degradation)
        if !self.config.enabled {
            return ProviderHealth::Healthy;
        }

        // SDK not integrated - report degraded status with clear message
        // This is "Degraded" not "Unhealthy" because the system still works
        // without external signals; it just lacks ground truth validation
        return ProviderHealth::Degraded(
            "SDK not integrated - stub implementation, no real signal data available".to_string(),
        );

        // The following code would be used when SDK is integrated:
        // if self.config.db_path.is_empty() {
        //     return ProviderHealth::Unhealthy("Database path not configured".to_string());
        // }
        // match tokio::fs::metadata(&self.config.db_path).await { ... }
    }

    fn validate_config(&self) -> Result<()> {
        if self.config.db_path.is_empty() && self.config.enabled {
            return Err(ExternalSignalError::ConfigMissing(
                "AgentFS database path required when enabled".to_string(),
            ));
        }

        if self.config.external_weight < 0.0 || self.config.external_weight > 1.0 {
            return Err(ExternalSignalError::InvalidConfig(format!(
                "Weight must be between 0.0 and 1.0, got {}",
                self.config.external_weight
            )));
        }

        Ok(())
    }
}

impl AgentFsProvider {
    /// Fetch tool statistics for an episode
    ///
    /// **STUB IMPLEMENTATION**: This method is not functional without SDK integration.
    /// When `agentfs-sdk` is integrated, this would query the AgentFS database
    /// for toolcall statistics matching the episode's tools.
    ///
    /// Returns empty vector (no real data available from stub).
    #[allow(dead_code)] // Not used until SDK integrated
    fn fetch_tool_stats(&self, _episode: &crate::episode::Episode) -> Vec<ToolSignal> {
        // Stub: No real data available without SDK
        // Real implementation would:
        // let tc = agentfs_sdk::ToolCalls::new(&self.config.db_path).await?;
        // let stats = tc.stats_for(&step.tool).await?;
        Vec::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_defaults() {
        let config = AgentFsConfig::default();
        assert!(!config.enabled, "Default config should be disabled");
        assert_eq!(config.external_weight, 0.3);
        assert!(config.db_path.is_empty(), "Default db_path should be empty");
        assert!(config.sanitize_parameters);
    }

    #[test]
    fn test_provider_creation() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: false,
            external_weight: 0.5,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        assert_eq!(provider.name(), "agentfs");
        assert_eq!(provider.config().external_weight, 0.5);
    }

    #[test]
    fn test_config_validation_valid() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true,
            external_weight: 0.5,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        assert!(provider.validate_config().is_ok());
    }

    #[test]
    fn test_config_validation_invalid_weight() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true,
            external_weight: 1.5, // Invalid: > 1.0
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let result = provider.validate_config();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExternalSignalError::InvalidConfig(_)
        ));
    }

    #[test]
    fn test_config_validation_missing_path_when_enabled() {
        let config = AgentFsConfig {
            db_path: String::new(), // Empty
            enabled: true,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let result = provider.validate_config();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ExternalSignalError::ConfigMissing(_)
        ));
    }

    #[test]
    fn test_config_validation_disabled_no_path_required() {
        // Disabled provider doesn require db_path
        let config = AgentFsConfig {
            db_path: String::new(), // Empty OK when disabled
            enabled: false,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        assert!(provider.validate_config().is_ok());
    }

    #[tokio::test]
    async fn test_disabled_provider_returns_empty_signals() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: false, // Disabled
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let episode = crate::episode::Episode::new(
            "test-episode".to_string(),
            crate::types::TaskContext::default(),
            crate::types::TaskType::Testing,
        );

        let signals = provider.get_signals(&episode).await.unwrap();
        assert!(
            signals.tool_signals.is_empty(),
            "Disabled provider should return empty signals"
        );
        assert_eq!(signals.confidence, 0.0);
        assert_eq!(signals.provider, "agentfs");
    }

    #[tokio::test]
    async fn test_enabled_provider_returns_sdk_unavailable_error() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true, // Enabled but SDK not integrated
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let episode = crate::episode::Episode::new(
            "test-episode".to_string(),
            crate::types::TaskContext::default(),
            crate::types::TaskType::Testing,
        );

        let result = provider.get_signals(&episode).await;
        assert!(
            result.is_err(),
            "Enabled provider without SDK should return error"
        );
        let err = result.unwrap_err();
        assert!(
            matches!(err, ExternalSignalError::SdkUnavailable(_)),
            "Error should be SdkUnavailable"
        );
    }

    #[tokio::test]
    async fn test_health_check_disabled_returns_healthy() {
        let config = AgentFsConfig {
            db_path: String::new(),
            enabled: false,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let health = provider.health_check().await;
        assert!(health.is_healthy(), "Disabled provider should be healthy");
    }

    #[tokio::test]
    async fn test_health_check_enabled_returns_degraded() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true, // Enabled but SDK not integrated
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);
        let health = provider.health_check().await;

        // Degraded (not unhealthy) because system works without external signals
        assert!(
            matches!(health, ProviderHealth::Degraded(_)),
            "Enabled provider without SDK should be degraded"
        );
        assert!(health.is_operational(), "Degraded is still operational");
    }

    #[test]
    fn test_sanitize_parameters_object() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);

        let params = serde_json::json!({
            "query": "sensitive data",
            "api_key": "secret123",
            "limit": 10
        });

        let sanitized = provider.sanitize_parameters(&params);

        // Check structure preserved
        if let serde_json::Value::Object(map) = sanitized {
            assert!(map.contains_key("query"));
            assert!(map.contains_key("api_key"));
            assert!(map.contains_key("limit"));
            // All values should be redacted
            for (_, value) in map {
                assert_eq!(
                    value,
                    serde_json::Value::String("[REDACTED]".to_string()),
                    "All values should be redacted"
                );
            }
        } else {
            panic!("Expected sanitized result to be an object");
        }
    }

    #[test]
    fn test_sanitize_parameters_disabled() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: false, // Disabled sanitization
        };

        let provider = AgentFsProvider::new(config);

        let params = serde_json::json!({
            "query": "sensitive data"
        });

        let sanitized = provider.sanitize_parameters(&params);

        // Should return original when sanitization disabled
        assert_eq!(sanitized, params);
    }

    #[test]
    fn test_sanitize_parameters_non_object() {
        let config = AgentFsConfig {
            db_path: "/tmp/test.db".to_string(),
            enabled: true,
            external_weight: 0.3,
            min_correlation_samples: 10,
            sanitize_parameters: true,
        };

        let provider = AgentFsProvider::new(config);

        // Test non-object value
        let params = serde_json::json!("string value");
        let sanitized = provider.sanitize_parameters(&params);
        assert_eq!(
            sanitized,
            serde_json::Value::String("[REDACTED]".to_string())
        );
    }
}