microscope-memory 1.2.0

Pure binary cognitive memory engine with REST API bridge for LLM integration. Zero-JSON core, mmap-based, hierarchical, sub-microsecond search.
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
use crate::types::ProjectId;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct HooksConfig {
    pub read_only: bool,
    pub write_enabled: bool,
}

impl Default for HooksConfig {
    fn default() -> Self {
        Self {
            read_only: true,
            write_enabled: false,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
    pub paths: Paths,
    pub index: Index,
    pub search: Search,
    pub memory_layers: MemoryLayers,
    pub performance: Performance,
    pub logging: Logging,
    #[serde(default)]
    pub embedding: Embedding,
    #[serde(default)]
    pub server: Server,
    #[serde(default)]
    pub federation: Federation,
    #[serde(default)]
    pub hooks: HooksConfig,
    #[serde(default = "default_project_id")]
    pub project_id: ProjectId,
    #[serde(default)]
    pub epistemic: EpistemicConfig,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Paths {
    pub layers_dir: String,
    pub output_dir: String,
    pub temp_dir: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Index {
    pub max_depth: u8,
    pub header_size: usize,
    #[serde(default = "default_auto_rebuild")]
    pub auto_rebuild: bool,
    #[serde(default = "default_auto_rebuild_entries")]
    pub auto_rebuild_entries: usize,
    /// Maximum persisted entries per layer. Zero keeps the full history.
    #[serde(default = "default_layer_retention_entries")]
    pub layer_retention_entries: usize,
    /// Maximum blocks in the index before low-score blocks are evicted.
    /// Zero keeps the index unbounded.
    #[serde(default = "default_max_blocks")]
    pub max_blocks: usize,
    /// Blocks with importance >= this value are never evicted.
    #[serde(default = "default_protect_min_importance")]
    pub protect_min_importance: u8,
    /// Minimum Hebbian recall energy for automatic importance promotion.
    #[serde(default = "default_promote_energy_threshold")]
    pub promote_energy_threshold: f32,
}

fn default_auto_rebuild() -> bool {
    true
}

fn default_layer_retention_entries() -> usize {
    2000
}

fn default_max_blocks() -> usize {
    0
}

fn default_protect_min_importance() -> u8 {
    8
}

fn default_promote_energy_threshold() -> f32 {
    0.35
}

fn default_auto_rebuild_entries() -> usize {
    25
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Search {
    pub default_k: usize,
    pub zoom_weight: f32,
    pub keyword_boost: f32,
    #[serde(default)]
    pub semantic_weight: f32,
    /// How much the cosine counts in the *ranking*, as a distance bonus.
    ///
    /// Separate from `semantic_weight`, which is a 0..1 blend deciding how far a
    /// query's spatial coordinates lean toward its embedding. Those are
    /// different units with different meanings, and sharing one knob made the
    /// two impossible to tell apart: setting it to 2.0 to test whether the
    /// semantic term could lead also moved the coordinates, and the measurement
    /// credited both effects to the same number.
    ///
    /// The default is the value both halves independently measured best at; see
    /// docs/measurements/ for the split curves. The two were never measured
    /// apart before this field existed.
    #[serde(default = "default_semantic_rank_gain")]
    pub semantic_rank_gain: f32,
    #[serde(default)]
    pub emotional_bias_weight: f32,
    #[serde(default)]
    pub emotion_21d_weight: f32,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MemoryLayers {
    pub layers: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Performance {
    pub use_mmap: bool,
    pub cache_size: usize,
    pub build_workers: usize,
    #[serde(default)]
    pub use_gpu: bool,
    #[serde(default)]
    pub compression: bool,
    #[serde(default = "default_cache_ttl")]
    pub cache_ttl_secs: u64,
}

fn default_cache_ttl() -> u64 {
    300
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Embedding {
    #[serde(default = "default_provider")]
    pub provider: String,
    #[serde(default = "default_model")]
    pub model: String,
    #[serde(default = "default_dim")]
    pub dim: usize,
    #[serde(default = "default_max_depth")]
    pub max_depth: u8,
    #[serde(default)]
    pub onnx_model_path: Option<String>,
    #[serde(default)]
    pub tokenizer_path: Option<String>,
    #[serde(default)]
    pub use_gpu: bool,
}

fn default_provider() -> String {
    "mock".to_string()
}
fn default_model() -> String {
    "sentence-transformers/all-MiniLM-L6-v2".to_string()
}
fn default_dim() -> usize {
    384
}
fn default_max_depth() -> u8 {
    // D5 holds 15..39 character statements (auto_depth), which is where short
    // personal facts are stored. Embedding only to D4 left those facts without
    // any vector, making them unreachable from the semantic path.
    5
}
fn default_semantic_rank_gain() -> f32 {
    // Set from the measured curve, not chosen: see WHITEPAPER 11.9. The
    // knee is at 2.0 -- 1.0 costs about 1.7 points of R@1 and nothing above 2.0
    // adds any.
    2.0
}

impl Default for Embedding {
    fn default() -> Self {
        Self {
            provider: default_provider(),
            model: default_model(),
            dim: default_dim(),
            max_depth: default_max_depth(),
            onnx_model_path: None,
            tokenizer_path: None,
            use_gpu: false,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Server {
    #[serde(default = "default_port")]
    pub port: u16,
    #[serde(default)]
    pub cors_origin: Option<String>,
    #[serde(default)]
    pub openai_api_key: Option<String>,
    #[serde(default)]
    pub gemini_api_key: Option<String>,
    /// Optional shared API key for inbound HTTP requests. Empty = no auth
    /// (single-user localhost default). When set, every bridge request must
    /// carry `X-API-Key: <key>`. Required when binding on a non-loopback host.
    #[serde(default)]
    pub api_key: Option<String>,
}

fn default_port() -> u16 {
    6060
}

impl Default for Server {
    fn default() -> Self {
        Self {
            port: default_port(),
            cors_origin: None,
            openai_api_key: None,
            gemini_api_key: None,
            api_key: None,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Logging {
    pub level: String,
    pub file: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Federation {
    #[serde(default)]
    pub indices: Vec<FederatedIndex>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct EpistemicConfig {
    /// Gate importance promotion for unsupported Inference/Hypothesis.
    #[serde(default = "default_true")]
    pub gate_promotion: bool,
    /// Minimum independent sources needed for non-zero confidence.
    #[serde(default = "default_min_sources")]
    pub min_independent_sources: u32,
    /// Cosine similarity threshold for echo detection.
    #[serde(default = "default_sim_threshold")]
    pub sim_threshold: f32,
    /// Near-duplicate scan depth for link operations.
    #[serde(default = "default_search_k")]
    pub search_k: usize,
    /// Confidence formula weights.
    #[serde(default)]
    pub confidence_weights: ConfidenceWeights,
    /// Bias added to eviction score from confidence (0 = disabled).
    #[serde(default)]
    pub eviction_evidence_bias: f32,
}

fn default_true() -> bool {
    true
}
fn default_min_sources() -> u32 {
    1
}
fn default_sim_threshold() -> f32 {
    0.85
}
fn default_search_k() -> usize {
    8
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ConfidenceWeights {
    #[serde(default = "default_obs_w")]
    pub observation_w: i32,
    #[serde(default = "default_src_w")]
    pub source_w: i32,
    #[serde(default = "default_refute_w")]
    pub refute_w: i32,
    #[serde(default = "default_age_pen")]
    pub age_penalty: i32,
}

impl Default for ConfidenceWeights {
    fn default() -> Self {
        Self {
            observation_w: 30,
            source_w: 18,
            refute_w: 25,
            age_penalty: 5,
        }
    }
}

fn default_obs_w() -> i32 {
    30
}
fn default_src_w() -> i32 {
    18
}
fn default_refute_w() -> i32 {
    25
}
fn default_age_pen() -> i32 {
    5
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FederatedIndex {
    pub name: String,
    pub config_path: String,
    #[serde(default = "default_weight")]
    pub weight: f32,
}

fn default_weight() -> f32 {
    1.0
}

fn default_project_id() -> ProjectId {
    ProjectId::GLOBAL
}

impl Config {
    /// Loads config from a TOML file.
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn std::error::Error>> {
        if !path.as_ref().exists() {
            return Ok(Self::default());
        }

        let content = fs::read_to_string(path)?;
        let config: Config = toml::from_str(&content)?;
        Ok(config)
    }

    /// Saves the current config to a TOML file.
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn std::error::Error>> {
        let content = toml::to_string_pretty(self)?;
        let tmp_path = path.as_ref().with_extension("toml.tmp");
        fs::write(&tmp_path, &content)?;
        fs::rename(&tmp_path, path.as_ref())?;
        Ok(())
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            paths: Paths {
                layers_dir: "./layers".to_string(),
                output_dir: "./output".to_string(),
                temp_dir: "./tmp".to_string(),
            },
            index: Index {
                max_depth: 8,
                header_size: 50,
                auto_rebuild: default_auto_rebuild(),
                auto_rebuild_entries: default_auto_rebuild_entries(),
                layer_retention_entries: default_layer_retention_entries(),
                max_blocks: default_max_blocks(),
                protect_min_importance: default_protect_min_importance(),
                promote_energy_threshold: default_promote_energy_threshold(),
            },
            search: Search {
                default_k: 10,
                zoom_weight: 3.0,   // boost recent memories more
                keyword_boost: 0.4, // tuned for better recall precision
                semantic_weight: 0.0,
                semantic_rank_gain: default_semantic_rank_gain(),
                emotional_bias_weight: 0.0,
                emotion_21d_weight: 0.0,
            },
            memory_layers: MemoryLayers {
                layers: vec![
                    "long_term".to_string(),
                    "short_term".to_string(),
                    "session".to_string(),
                    "associative".to_string(),
                    "echo_cache".to_string(),
                ],
            },
            performance: Performance {
                use_mmap: true,
                cache_size: 64,
                build_workers: 4,
                use_gpu: false,
                compression: false,
                cache_ttl_secs: default_cache_ttl(),
            },
            logging: Logging {
                level: "info".to_string(),
                file: Some("microscope.log".to_string()),
            },
            embedding: Embedding::default(),
            server: Server::default(),
            federation: Federation::default(),
            hooks: HooksConfig::default(),
            project_id: ProjectId::GLOBAL,
            epistemic: EpistemicConfig::default(),
        }
    }
}