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
//! Memory configuration presets and tunable parameters.
use serde::{Deserialize, Serialize};
use crate::embeddings::EmbeddingConfig;
use crate::entities::EntityConfig;
/// Configuration for LLM-based triple extraction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TripleConfig {
/// Enable triple extraction during consolidation
pub enabled: bool,
/// Number of memories to process per consolidation cycle
pub batch_size: usize,
/// Maximum extraction attempts before skipping a memory
pub max_retries: u32,
/// Override model for triple extraction (None = use extractor default)
pub model: Option<String>,
}
/// Configuration for knowledge promotion detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromotionConfig {
/// Enable promotion detection (default: false, opt-in)
pub enabled: bool,
/// Minimum core_strength for a memory to be considered (default: 0.6)
pub min_core_strength: f64,
/// Minimum Hebbian link weight to count as connected (default: 0.3)
pub min_hebbian_weight: f64,
/// Minimum cluster size (default: 3)
pub min_cluster_size: usize,
/// Minimum time span in days across cluster members (default: 2.0)
pub min_time_span_days: f64,
/// Minimum average importance across cluster members (default: 0.4)
pub min_avg_importance: f64,
}
impl Default for PromotionConfig {
fn default() -> Self {
Self {
enabled: false,
min_core_strength: 0.6,
min_hebbian_weight: 0.3,
min_cluster_size: 3,
min_time_span_days: 2.0,
min_avg_importance: 0.4,
}
}
}
impl Default for TripleConfig {
fn default() -> Self {
Self {
enabled: false,
batch_size: 10,
max_retries: 3,
model: None,
}
}
}
/// Configuration for write-time association discovery (multi-signal Hebbian).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssociationConfig {
/// Enable/disable write-time association discovery
pub enabled: bool,
/// Weight for entity overlap signal
pub w_entity: f64,
/// Weight for embedding similarity signal
pub w_embedding: f64,
/// Weight for temporal proximity signal
pub w_temporal: f64,
/// Combined score threshold for link creation
pub link_threshold: f64,
/// Maximum new links per memory write
pub max_links_per_memory: usize,
/// Maximum candidates to evaluate
pub candidate_limit: usize,
/// Temporal window in days for candidate selection
pub temporal_window_days: u64,
/// Initial strength for write-time discovered links
pub initial_strength: f64,
/// Decay rate for co-recall links
pub decay_corecall: f64,
/// Decay rate for multi-signal links
pub decay_multi: f64,
/// Decay rate for single-signal links
pub decay_single: f64,
}
impl Default for AssociationConfig {
fn default() -> Self {
Self {
enabled: false,
w_entity: 0.3,
w_embedding: 0.5,
w_temporal: 0.2,
link_threshold: 0.4,
max_links_per_memory: 5,
candidate_limit: 50,
temporal_window_days: 7,
initial_strength: 0.5,
decay_corecall: 0.95,
decay_multi: 0.90,
decay_single: 0.85,
}
}
}
/// All tunable parameters for the Engram memory system.
///
/// Default values come from neuroscience literature (ACT-R, Memory Chain Model,
/// Ebbinghaus forgetting curve).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
// === Consolidation (Memory Chain Model) ===
/// Working memory decay rate (per day). Higher = faster decay.
pub mu1: f64,
/// Core memory decay rate (per day). Higher = faster decay.
pub mu2: f64,
/// Consolidation transfer rate (working → core per day)
pub alpha: f64,
/// Fraction of archived memories replayed per cycle
pub interleave_ratio: f64,
/// Core strength boost per replayed archived memory (base)
pub replay_boost: f64,
// Layer rebalancing thresholds
pub promote_threshold: f64,
pub demote_threshold: f64,
pub archive_threshold: f64,
// === Activation (ACT-R) ===
/// Base-level activation decay parameter (d in t^-d)
pub actr_decay: f64,
/// Context spreading activation weight
pub context_weight: f64,
/// Importance weight in retrieval activation
pub importance_weight: f64,
/// Contradiction penalty in activation
pub contradiction_penalty: f64,
// === Forgetting ===
/// Spacing effect multiplier
pub spacing_factor: f64,
/// Importance floor in stability
pub importance_floor: f64,
/// Consolidation bonus per consolidation count
pub consolidation_bonus: f64,
/// Effective strength threshold for pruning
pub forget_threshold: f64,
// === Reward ===
/// Default reward magnitude
pub reward_magnitude: f64,
// === Downscaling ===
/// Global downscaling factor per consolidation cycle
pub downscale_factor: f64,
// === Hebbian learning ===
/// Enable Hebbian link formation
pub hebbian_enabled: bool,
/// Number of co-activations before link forms
pub hebbian_threshold: i32,
/// Link strength decay per consolidation cycle
pub hebbian_decay: f64,
// === STDP (causal inference) ===
/// Enable temporal direction tracking
pub stdp_enabled: bool,
/// Forward/backward ratio threshold for causal inference
pub stdp_causal_threshold: f64,
/// Minimum observations before STDP inference
pub stdp_min_observations: i32,
// === Embedding ===
/// Embedding provider configuration
pub embedding: EmbeddingConfig,
/// Weight for FTS exact matching in hybrid recall (0.0-1.0)
/// Recommended: 0.15 for 15% FTS contribution
pub fts_weight: f64,
/// Weight for embedding similarity in recall scoring (0.0-1.0)
/// Recommended: 0.60 for 60% semantic similarity contribution
pub embedding_weight: f64,
/// Weight for ACT-R activation in recall scoring (0.0-1.0)
/// Recommended: 0.25 for 25% recency/frequency contribution
/// Note: fts_weight + embedding_weight + actr_weight should sum to ~1.0
pub actr_weight: f64,
/// Sigmoid center for ACT-R activation normalization.
/// Controls the "midpoint age" — memories with activation near this value
/// get normalized to ~0.5. Default -5.5 ≈ 1-day-old single-access memory.
/// Lower values shift the curve to favor older memories.
#[serde(default = "default_actr_sigmoid_center")]
pub actr_sigmoid_center: f64,
/// Sigmoid scale for ACT-R activation normalization.
/// Controls steepness: smaller = sharper transition, larger = gentler.
/// Default 1.5 gives good discrimination across the 1min–30day range.
#[serde(default = "default_actr_sigmoid_scale")]
pub actr_sigmoid_scale: f64,
// === Entity extraction ===
/// Entity extraction configuration
#[serde(default)]
pub entity_config: EntityConfig,
/// Weight for entity matches in hybrid recall scoring (0.0-1.0)
#[serde(default = "default_entity_weight")]
pub entity_weight: f64,
// === Dedup on write ===
/// Enable dedup checking on write (default: true)
#[serde(default = "default_dedup_enabled")]
pub dedup_enabled: bool,
/// Cosine similarity threshold for considering memories as duplicates (default: 0.95)
#[serde(default = "default_dedup_threshold")]
pub dedup_threshold: f64,
// === Auto-extraction importance cap ===
/// Maximum importance for auto-extracted memories (default: 0.7).
/// Prevents LLM extractor from assigning high importance to noise.
/// Only affects memories stored via extraction pipeline, not manual add().
#[serde(default = "default_auto_extract_importance_cap")]
pub auto_extract_importance_cap: f64,
// === Dedup on recall ===
/// Enable dedup of recall results (default: true)
#[serde(default = "default_recall_dedup_enabled")]
pub recall_dedup_enabled: bool,
/// Cosine similarity threshold for recall result dedup (default: 0.85)
#[serde(default = "default_recall_dedup_threshold")]
pub recall_dedup_threshold: f64,
// === Multi-retrieval fusion ===
/// Weight for temporal channel in hybrid recall (0.0-1.0)
/// Only meaningful when query has temporal indicators
#[serde(default = "default_temporal_weight")]
pub temporal_weight: f64,
/// Weight for Hebbian graph channel in hybrid recall (0.0-1.0)
#[serde(default = "default_hebbian_recall_weight")]
pub hebbian_recall_weight: f64,
/// Enable query-type adaptive weight adjustment (default: true)
#[serde(default = "default_adaptive_weights")]
pub adaptive_weights: bool,
/// Write-time association discovery configuration
#[serde(default)]
pub association: AssociationConfig,
/// LLM triple extraction configuration
#[serde(default)]
pub triple: TripleConfig,
/// Knowledge promotion configuration
#[serde(default)]
pub promotion: PromotionConfig,
}
fn default_entity_weight() -> f64 {
0.15
}
fn default_actr_sigmoid_center() -> f64 {
-5.5
}
fn default_actr_sigmoid_scale() -> f64 {
1.5
}
fn default_dedup_enabled() -> bool {
true
}
fn default_dedup_threshold() -> f64 {
0.95
}
fn default_auto_extract_importance_cap() -> f64 {
0.7
}
fn default_recall_dedup_enabled() -> bool {
true
}
fn default_recall_dedup_threshold() -> f64 {
0.85
}
fn default_temporal_weight() -> f64 {
0.10
}
fn default_hebbian_recall_weight() -> f64 {
0.10
}
fn default_adaptive_weights() -> bool {
true
}
impl Default for MemoryConfig {
/// Literature-based defaults.
fn default() -> Self {
Self {
mu1: 0.15,
mu2: 0.005,
alpha: 0.08,
interleave_ratio: 0.3,
replay_boost: 0.01,
promote_threshold: 0.25,
demote_threshold: 0.05,
archive_threshold: 0.15,
actr_decay: 0.5,
context_weight: 1.5,
importance_weight: 2.0,
contradiction_penalty: 3.0,
spacing_factor: 0.5,
importance_floor: 0.5,
consolidation_bonus: 0.2,
forget_threshold: 0.01,
reward_magnitude: 0.15,
downscale_factor: 0.95,
hebbian_enabled: true,
hebbian_threshold: 3,
hebbian_decay: 0.95,
stdp_enabled: true,
stdp_causal_threshold: 2.0,
stdp_min_observations: 3,
embedding: EmbeddingConfig::default(),
fts_weight: 0.15, // 15% exact matching
embedding_weight: 0.60, // 60% semantic similarity
actr_weight: 0.25, // 25% recency/frequency/importance
actr_sigmoid_center: default_actr_sigmoid_center(),
actr_sigmoid_scale: default_actr_sigmoid_scale(),
entity_config: EntityConfig::default(),
entity_weight: default_entity_weight(),
dedup_enabled: default_dedup_enabled(),
dedup_threshold: default_dedup_threshold(),
recall_dedup_enabled: default_recall_dedup_enabled(),
recall_dedup_threshold: default_recall_dedup_threshold(),
auto_extract_importance_cap: default_auto_extract_importance_cap(),
temporal_weight: default_temporal_weight(),
hebbian_recall_weight: default_hebbian_recall_weight(),
adaptive_weights: default_adaptive_weights(),
association: AssociationConfig::default(),
triple: TripleConfig::default(),
promotion: PromotionConfig::default(),
}
}
}
impl MemoryConfig {
/// Preset for conversational chatbots.
///
/// High replay, slow decay — optimized for long conversations.
pub fn chatbot() -> Self {
Self {
mu1: 0.08,
mu2: 0.003,
alpha: 0.12,
interleave_ratio: 0.4,
replay_boost: 0.015,
actr_decay: 0.4,
context_weight: 2.0,
downscale_factor: 0.96,
reward_magnitude: 0.2,
forget_threshold: 0.005,
..Default::default()
}
}
/// Preset for short-lived task agents.
///
/// Fast decay, low replay — focus on recent task context.
pub fn task_agent() -> Self {
Self {
mu1: 0.25,
mu2: 0.01,
alpha: 0.05,
interleave_ratio: 0.1,
replay_boost: 0.005,
actr_decay: 0.6,
promote_threshold: 0.35,
archive_threshold: 0.2,
downscale_factor: 0.90,
forget_threshold: 0.02,
..Default::default()
}
}
/// Preset for long-term personal assistants.
///
/// Very slow core decay — remember preferences for months.
pub fn personal_assistant() -> Self {
Self {
mu1: 0.12,
mu2: 0.001,
alpha: 0.10,
interleave_ratio: 0.3,
replay_boost: 0.02,
actr_decay: 0.45,
importance_weight: 0.7,
promote_threshold: 0.20,
demote_threshold: 0.03,
downscale_factor: 0.97,
forget_threshold: 0.005,
..Default::default()
}
}
/// Preset for research agents.
///
/// Minimal forgetting — everything might be relevant later.
pub fn researcher() -> Self {
Self {
mu1: 0.05,
mu2: 0.001,
alpha: 0.15,
interleave_ratio: 0.5,
replay_boost: 0.025,
actr_decay: 0.35,
context_weight: 2.0,
importance_weight: 0.3,
promote_threshold: 0.15,
demote_threshold: 0.02,
archive_threshold: 0.10,
downscale_factor: 0.98,
forget_threshold: 0.001,
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_triple_config_defaults() {
let config = TripleConfig::default();
assert!(!config.enabled);
assert_eq!(config.batch_size, 10);
assert_eq!(config.max_retries, 3);
assert!(config.model.is_none());
}
#[test]
fn test_triple_config_serde_roundtrip() {
let original = TripleConfig {
enabled: true,
batch_size: 20,
max_retries: 5,
model: Some("claude-haiku-4-5-20251001".to_string()),
};
let json = serde_json::to_string(&original).expect("serialize");
let deserialized: TripleConfig = serde_json::from_str(&json).expect("deserialize");
assert!(deserialized.enabled);
assert_eq!(deserialized.batch_size, 20);
assert_eq!(deserialized.max_retries, 5);
assert_eq!(deserialized.model.as_deref(), Some("claude-haiku-4-5-20251001"));
}
#[test]
fn test_memory_config_has_triple() {
let config = MemoryConfig::default();
assert!(!config.triple.enabled);
assert_eq!(config.triple.batch_size, 10);
}
#[test]
fn test_association_config_defaults() {
let config = AssociationConfig::default();
assert!(!config.enabled);
assert!((config.w_entity - 0.3).abs() < f64::EPSILON);
assert!((config.w_embedding - 0.5).abs() < f64::EPSILON);
assert!((config.w_temporal - 0.2).abs() < f64::EPSILON);
assert!((config.link_threshold - 0.4).abs() < f64::EPSILON);
assert_eq!(config.max_links_per_memory, 5);
assert_eq!(config.candidate_limit, 50);
assert_eq!(config.temporal_window_days, 7);
assert!((config.initial_strength - 0.5).abs() < f64::EPSILON);
assert!((config.decay_corecall - 0.95).abs() < f64::EPSILON);
assert!((config.decay_multi - 0.90).abs() < f64::EPSILON);
assert!((config.decay_single - 0.85).abs() < f64::EPSILON);
}
#[test]
fn test_memory_config_has_association() {
let config = MemoryConfig::default();
// Association should be present and disabled by default
assert!(!config.association.enabled);
assert_eq!(config.association.candidate_limit, 50);
}
#[test]
fn test_association_config_serde_roundtrip() {
let original = AssociationConfig::default();
let json = serde_json::to_string(&original).expect("serialize");
let deserialized: AssociationConfig = serde_json::from_str(&json).expect("deserialize");
assert_eq!(original.enabled, deserialized.enabled);
assert!((original.w_entity - deserialized.w_entity).abs() < f64::EPSILON);
assert!((original.w_embedding - deserialized.w_embedding).abs() < f64::EPSILON);
assert!((original.w_temporal - deserialized.w_temporal).abs() < f64::EPSILON);
assert!((original.link_threshold - deserialized.link_threshold).abs() < f64::EPSILON);
assert_eq!(original.max_links_per_memory, deserialized.max_links_per_memory);
assert_eq!(original.candidate_limit, deserialized.candidate_limit);
assert_eq!(original.temporal_window_days, deserialized.temporal_window_days);
assert!((original.initial_strength - deserialized.initial_strength).abs() < f64::EPSILON);
assert!((original.decay_corecall - deserialized.decay_corecall).abs() < f64::EPSILON);
assert!((original.decay_multi - deserialized.decay_multi).abs() < f64::EPSILON);
assert!((original.decay_single - deserialized.decay_single).abs() < f64::EPSILON);
}
#[test]
fn test_association_config_serde_custom_values() {
let custom = AssociationConfig {
enabled: true,
w_entity: 0.5,
w_embedding: 0.3,
w_temporal: 0.2,
link_threshold: 0.6,
max_links_per_memory: 10,
candidate_limit: 100,
temporal_window_days: 14,
initial_strength: 0.7,
decay_corecall: 0.99,
decay_multi: 0.95,
decay_single: 0.80,
};
let json = serde_json::to_string(&custom).expect("serialize");
let deserialized: AssociationConfig = serde_json::from_str(&json).expect("deserialize");
assert!(deserialized.enabled);
assert!((deserialized.w_entity - 0.5).abs() < f64::EPSILON);
assert_eq!(deserialized.candidate_limit, 100);
assert_eq!(deserialized.temporal_window_days, 14);
}
#[test]
fn test_memory_config_serde_roundtrip_with_association() {
let mut config = MemoryConfig::default();
config.association.enabled = true;
config.association.link_threshold = 0.6;
let json = serde_json::to_string(&config).expect("serialize");
let deserialized: MemoryConfig = serde_json::from_str(&json).expect("deserialize");
assert!(deserialized.association.enabled);
assert!((deserialized.association.link_threshold - 0.6).abs() < f64::EPSILON);
// Other fields preserved
assert!((deserialized.mu1 - config.mu1).abs() < f64::EPSILON);
}
}