brainos-core 0.5.0

Configuration and bootstrapping for Brain OS cognitive engine
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
559
560
561
use std::collections::HashMap;

use figment::{providers::Serialized, Figment};

use super::loader::expand_tilde;
use super::*;

#[test]
fn test_default_config() {
    let config = BrainConfig::default();
    assert_eq!(config.brain.data_dir, "~/.brain");
    // Legacy field is #[deprecated] (Issue 40) but still populated by the
    // default; read explicitly suppressed.
    #[allow(deprecated)]
    let provider = config.llm.provider.clone();
    assert_eq!(provider, "ollama");
    assert_eq!(config.embedding.dimensions, 768);
    assert!(!config.encryption.enabled);
    assert_eq!(
        config.actions.web_search.provider,
        WebSearchProvider::DuckDuckGo
    );
    assert_eq!(config.actions.scheduling.mode, SchedulingMode::PersistOnly);
    // Proactivity defaults are synced to `default.yaml` (Issue 36).
    assert!(config.proactivity.enabled);
    assert_eq!(config.proactivity.max_per_day, 2);
    assert_eq!(config.proactivity.quiet_hours.start, "20:00");
    assert_eq!(config.proactivity.quiet_hours.end, "10:00");
    assert!(config.adapters.http.enabled);
}

/// `observability` defaults: the struct `Default`, the embedded `default.yaml`
/// section, and the `#[serde(default)]` fallback for an absent section must all
/// agree. Guards against yaml/struct drift the way the proactivity defaults do.
#[test]
fn observability_defaults_are_synced() {
    use figment::providers::{Format, Yaml};

    // Struct defaults.
    let config = BrainConfig::default();
    assert_eq!(config.observability.resource_sample_secs, 30);
    assert_eq!(config.observability.thresholds.rss_mb, 2048);
    assert_eq!(config.observability.thresholds.cpu_pct, 90.0);
    assert_eq!(config.observability.thresholds.disk_mb, 10_240);

    // Embedded default.yaml parses to the same values.
    let from_yaml: BrainConfig = Figment::new()
        .merge(Yaml::string(DEFAULT_CONFIG))
        .extract()
        .expect("default.yaml must parse");
    assert_eq!(from_yaml.observability.resource_sample_secs, 30);
    assert_eq!(from_yaml.observability.thresholds.rss_mb, 2048);
    assert_eq!(from_yaml.observability.thresholds.cpu_pct, 90.0);
    assert_eq!(from_yaml.observability.thresholds.disk_mb, 10_240);

    // An absent `observability:` section falls back to the struct defaults
    // via `#[serde(default)]` (every field is individually defaulted).
    let absent: ObservabilityConfig = Figment::new().extract().expect("serde defaults");
    assert_eq!(absent.resource_sample_secs, 30);
    assert_eq!(absent.thresholds.disk_mb, 10_240);
}

/// Issue 40 regression: `validate()` must warn when the legacy
/// `llm.provider` is set alongside `llm.providers[]`, so operators
/// notice the deprecated single-shape field that `providers[]` now
/// supersedes.
#[test]
fn validate_warns_when_legacy_provider_overlaps_providers_array() {
    let mut config = BrainConfig::default();
    config.brain.data_dir = std::env::temp_dir()
        .join("brain-legacy-llm-test")
        .to_string_lossy()
        .to_string();
    // Default already has legacy `provider = "ollama"`. Add a providers[]
    // entry so the two shapes overlap.
    config.llm.providers.push(ProviderEntry {
        name: "primary".into(),
        kind: "ollama".into(),
        base_url: "http://localhost:11434".into(),
        api_key: String::new(),
        model: "qwen2.5-coder:7b".into(),
        api_key_file: None,
        preferred_models: Vec::new(),
    });

    let warnings = config.validate().expect("validate must succeed");
    assert!(
        warnings.iter().any(|w| w.contains("Legacy `llm.provider`")),
        "expected legacy provider warning, got: {warnings:?}"
    );
}

/// Issue 40: `validate()` is silent when only the new `providers[]`
/// shape is configured (legacy field empty) — the warning fires
/// purely on overlap, not on the legacy shape itself.
#[test]
fn validate_silent_when_legacy_provider_empty() {
    let mut config = BrainConfig::default();
    config.brain.data_dir = std::env::temp_dir()
        .join("brain-no-legacy-llm-test")
        .to_string_lossy()
        .to_string();
    #[allow(deprecated)]
    {
        config.llm.provider.clear();
    }
    config.llm.providers.push(ProviderEntry {
        name: "primary".into(),
        kind: "ollama".into(),
        base_url: "http://localhost:11434".into(),
        api_key: String::new(),
        model: "qwen2.5-coder:7b".into(),
        api_key_file: None,
        preferred_models: Vec::new(),
    });

    let warnings = config.validate().expect("validate must succeed");
    assert!(
        !warnings.iter().any(|w| w.contains("Legacy `llm.provider`")),
        "no legacy warning expected when only providers[] is set, got: {warnings:?}"
    );
}

/// Issue 36 regression: the four `proactivity` fields the audit
/// flagged (enabled / max_per_day / quiet_hours.{start,end}) must
/// stay in sync between `BrainConfig::default()` and the embedded
/// `default.yaml`. The full-config round-trip currently surfaces
/// unrelated drift in `agents.*`, `llm.providers`, and `reflex.*`
/// — out of scope for this slice; pick those up in a follow-up
/// before any test widens to the whole shape.
#[test]
fn default_matches_yaml_for_proactivity() {
    let yaml_source = include_str!("../../default.yaml");
    let yaml_loaded: BrainConfig =
        serde_yaml::from_str(yaml_source).expect("embedded default.yaml must deserialize");
    let struct_default = BrainConfig::default();

    assert_eq!(
        yaml_loaded.proactivity.enabled, struct_default.proactivity.enabled,
        "proactivity.enabled drifted"
    );
    assert_eq!(
        yaml_loaded.proactivity.max_per_day, struct_default.proactivity.max_per_day,
        "proactivity.max_per_day drifted"
    );
    assert_eq!(
        yaml_loaded.proactivity.quiet_hours.start, struct_default.proactivity.quiet_hours.start,
        "proactivity.quiet_hours.start drifted"
    );
    assert_eq!(
        yaml_loaded.proactivity.quiet_hours.end, struct_default.proactivity.quiet_hours.end,
        "proactivity.quiet_hours.end drifted"
    );
}

#[test]
fn test_expand_tilde() {
    let expanded = expand_tilde("~/.brain");
    assert!(!expanded.to_str().unwrap().starts_with('~'));
    assert!(expanded.to_str().unwrap().ends_with(".brain"));
}

#[test]
fn test_data_dir_paths() {
    let config = BrainConfig::default();
    let data = config.data_dir();
    assert!(data.to_str().unwrap().ends_with(".brain"));
    assert!(config.sqlite_path().to_str().unwrap().ends_with("brain.db"));
    assert!(config
        .ruvector_path()
        .to_str()
        .unwrap()
        .ends_with("ruvector"));
}

#[test]
fn test_load_from_defaults() {
    let figment = Figment::new().merge(Serialized::defaults(BrainConfig::default()));
    let config: BrainConfig = figment.extract().unwrap();
    #[allow(deprecated)]
    let llm_model = config.llm.model.clone();
    assert_eq!(llm_model, "qwen2.5-coder:7b");
    assert_eq!(config.memory.search.rrf_k, 60);
    assert_eq!(config.memory.search.pre_fusion_limit, 50);
    assert!((config.memory.search.importance_weight - 0.3).abs() < f64::EPSILON);
    assert!((config.memory.search.recency_weight - 0.2).abs() < f64::EPSILON);
    assert!((config.memory.search.decay_rate - 0.01).abs() < f64::EPSILON);
}

fn writable_test_data_dir() -> String {
    std::env::temp_dir()
        .join("brain-core-tests")
        .to_string_lossy()
        .to_string()
}

fn validated_config() -> BrainConfig {
    let mut c = BrainConfig::default();
    c.brain.data_dir = writable_test_data_dir();
    c.access.api_keys = vec![ApiKeyConfig {
        key: "test-secure-api-key-12345".to_string(),
        name: "test".to_string(),
        permissions: vec!["read".to_string(), "write".to_string()],
        agent_id: None,
    }];
    c
}

#[test]
fn test_validate_generated_key_no_warning() {
    let mut config = BrainConfig::default();
    config.brain.data_dir = writable_test_data_dir();
    let warnings = config.validate().expect("default config should be valid");
    assert!(
        !warnings.iter().any(|w| w.contains("No API keys")),
        "should not have empty-keys warning with a generated key, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_no_api_keys_fails() {
    let mut config = BrainConfig::default();
    config.brain.data_dir = writable_test_data_dir();
    config.access.api_keys.clear();
    let err = config
        .validate()
        .expect_err("should fail with empty API keys");
    assert!(
        err.contains("No API keys configured"),
        "unexpected error message: {err}"
    );
}

#[test]
fn test_validate_port_conflict_is_hard_error() {
    let mut config = validated_config();
    config.adapters.ws.port = config.adapters.http.port;
    let err = config
        .validate()
        .expect_err("should fail with port conflict");
    assert!(
        err.contains("Port conflict"),
        "unexpected error message: {err}"
    );
}

#[test]
fn test_validate_bad_llm_url_is_hard_error() {
    let mut config = validated_config();
    #[allow(deprecated)]
    {
        config.llm.base_url = "ftp://invalid.example.com".to_string();
    }
    let err = config.validate().expect_err("should fail with bad URL");
    assert!(
        err.contains("Invalid LLM base_url"),
        "unexpected error: {err}"
    );
}

#[test]
fn test_validate_high_temperature_warning() {
    let mut config = validated_config();
    config.llm.temperature = 2.0;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings.iter().any(|w| w.contains("temperature")),
        "expected temperature warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_consolidation_interval_zero_warning() {
    let mut config = validated_config();
    config.memory.consolidation.enabled = true;
    config.memory.consolidation.interval_hours = 0;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings.iter().any(|w| w.contains("interval_hours")),
        "expected interval warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_actions_defaults_deserialize() {
    let config = BrainConfig::default();
    // On by default; DuckDuckGo HTML is the zero-config built-in so
    // web search works without Docker or an API key.
    assert!(config.actions.web_search.enabled);
    assert_eq!(
        config.actions.web_search.provider,
        WebSearchProvider::DuckDuckGo
    );
    assert_eq!(config.actions.web_search.default_top_k, 5);
    assert_eq!(config.actions.scheduling.mode, SchedulingMode::PersistOnly);
    assert!(!config.actions.messaging.enabled);
}

#[test]
fn test_validate_actions_warning_custom_without_endpoint() {
    let mut config = validated_config();
    config.actions.web_search.enabled = true;
    config.actions.web_search.provider = WebSearchProvider::Custom;
    config.actions.web_search.endpoint.clear();
    config.actions.messaging.enabled = true;
    config.actions.messaging.channels.clear();
    let warnings = config.validate().expect("config should still be valid");
    assert!(warnings.iter().any(|w| w.contains("'custom'")));
    assert!(warnings.iter().any(|w| w.contains("messaging")));
}

#[test]
fn test_validate_tavily_without_api_key_warning() {
    let mut config = validated_config();
    config.actions.web_search.enabled = true;
    config.actions.web_search.provider = WebSearchProvider::Tavily;
    config.actions.web_search.api_key.clear();
    let warnings = config.validate().expect("config should still be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("'tavily'") && w.contains("api_key")),
        "expected tavily api_key warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_searxng_no_web_search_warning() {
    let mut config = validated_config();
    config.actions.web_search.enabled = true;
    config.actions.web_search.provider = WebSearchProvider::Searxng;
    let warnings = config.validate().expect("config should still be valid");
    assert!(
        !warnings.iter().any(|w| w.contains("web_search")),
        "SearXNG with default endpoint should not trigger web_search warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_http_and_https_urls_accepted() {
    let mut config = validated_config();
    #[allow(deprecated)]
    {
        config.llm.base_url = "https://api.example.com/v1".to_string();
    }
    assert!(config.validate().is_ok());

    #[allow(deprecated)]
    {
        config.llm.base_url = "http://localhost:11434".to_string();
    }
    assert!(config.validate().is_ok());
}

#[test]
fn test_validate_all_unique_ports_ok() {
    let config = validated_config();
    assert!(config.validate().is_ok());
}

#[test]
fn test_validate_timeout_zero_warning() {
    let mut config = validated_config();
    config.actions.web_search.timeout_ms = 0;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("timeout_ms") && w.contains("0")),
        "expected timeout_ms=0 warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_timeout_too_high_warning() {
    let mut config = validated_config();
    config.actions.messaging.timeout_ms = 60_000;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("timeout_ms") && w.contains("60000")),
        "expected high timeout warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_resilience_max_retries_warning() {
    let mut config = validated_config();
    config.actions.resilience.max_retries = 15;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("max_retries") && w.contains("15")),
        "expected max_retries warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_validate_resilience_threshold_zero_warning() {
    let mut config = validated_config();
    config.actions.resilience.circuit_breaker_threshold = 0;
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("circuit_breaker_threshold")),
        "expected circuit_breaker_threshold=0 warning, got: {:?}",
        warnings
    );
}

#[test]
fn test_resilience_defaults() {
    let res = ResilienceConfig::default();
    assert_eq!(res.max_retries, 2);
    assert_eq!(res.retry_base_ms, 500);
    assert_eq!(res.circuit_breaker_threshold, 5);
    assert_eq!(res.circuit_breaker_cooldown_secs, 60);
}

#[test]
fn test_channel_config_old_format_compat() {
    let yaml = r#"
            enabled: false
            timeout_ms: 3000
            channels:
              alerts: "https://example.com/hook"
              ops: "https://slack.example.com/webhook"
        "#;
    let cfg: MessagingActionConfig =
        serde_yaml::from_str(yaml).expect("old format should deserialize");
    assert_eq!(cfg.channels.len(), 2);
    assert_eq!(cfg.channels["alerts"].url, "https://example.com/hook");
    assert!(cfg.channels["alerts"].body.is_empty());
    assert!(cfg.channels["alerts"].headers.is_empty());
}

#[test]
fn test_channel_config_new_format() {
    let yaml = r#"
            enabled: true
            timeout_ms: 3000
            channels:
              alerts:
                url: "https://hooks.slack.com/services/T/B/x"
                body: '{"text": "{{content}}"}'
                headers:
                  Authorization: "Bearer tok123"
        "#;
    let cfg: MessagingActionConfig =
        serde_yaml::from_str(yaml).expect("new format should deserialize");
    assert_eq!(cfg.channels.len(), 1);
    let ch = &cfg.channels["alerts"];
    assert_eq!(ch.url, "https://hooks.slack.com/services/T/B/x");
    assert_eq!(ch.body, r#"{"text": "{{content}}"}"#);
    assert_eq!(ch.headers["Authorization"], "Bearer tok123");
}

#[test]
fn test_channel_config_mixed_format() {
    let yaml = r#"
            enabled: true
            timeout_ms: 3000
            channels:
              simple: "https://example.com/hook"
              custom:
                url: "https://discord.com/api/webhooks/123/abc"
                body: '{"content": "{{content}}"}'
        "#;
    let cfg: MessagingActionConfig =
        serde_yaml::from_str(yaml).expect("mixed format should deserialize");
    assert_eq!(cfg.channels.len(), 2);
    assert_eq!(cfg.channels["simple"].url, "https://example.com/hook");
    assert!(cfg.channels["simple"].body.is_empty());
    let custom = &cfg.channels["custom"];
    assert_eq!(custom.url, "https://discord.com/api/webhooks/123/abc");
    assert!(!custom.body.is_empty());
    assert!(custom.headers.is_empty());
}

#[test]
fn agent_discovery_override_parses_capabilities() {
    let yaml = r#"
        binary: "/opt/homebrew/bin/claude"
        args: ["--print", "--task", "{task_id}"]
        capabilities:
          tags: ["code-edit", "rust"]
          languages: ["rust", "typescript"]
          max_concurrency: 4
          needs_network: true
    "#;
    let ov: AgentDiscoveryOverride =
        serde_yaml::from_str(yaml).expect("override with capabilities should deserialize");
    let caps = ov
        .capabilities
        .expect("capabilities block should round-trip");
    assert_eq!(caps.tags, vec!["code-edit".to_string(), "rust".to_string()]);
    assert_eq!(
        caps.languages,
        vec!["rust".to_string(), "typescript".to_string()]
    );
    assert_eq!(caps.max_concurrency, 4);
    assert!(caps.needs_network);
}

#[test]
fn agent_discovery_override_capabilities_default_concurrency() {
    let yaml = r#"
        capabilities:
          tags: ["plan"]
    "#;
    let ov: AgentDiscoveryOverride = serde_yaml::from_str(yaml).unwrap();
    let caps = ov.capabilities.unwrap();
    assert_eq!(caps.max_concurrency, 1, "default concurrency must be 1");
    assert!(!caps.needs_network);
    assert!(caps.languages.is_empty());
}

#[test]
fn agent_discovery_override_capabilities_omitted_stays_none() {
    let yaml = r#"
        binary: "/usr/local/bin/aider"
    "#;
    let ov: AgentDiscoveryOverride = serde_yaml::from_str(yaml).unwrap();
    assert!(
        ov.capabilities.is_none(),
        "omitted capabilities must stay None so the fingerprint default wins"
    );
}

#[test]
fn test_validate_channel_empty_url_warning() {
    let mut config = validated_config();
    config.actions.messaging.enabled = true;
    config.actions.messaging.channels.insert(
        "bad".into(),
        ChannelConfig {
            url: "".into(),
            body: String::new(),
            headers: HashMap::new(),
        },
    );
    let warnings = config.validate().expect("should be valid");
    assert!(
        warnings
            .iter()
            .any(|w| w.contains("channels.bad") && w.contains("url is empty")),
        "expected empty-url warning, got: {:?}",
        warnings
    );
}