engram-core 0.21.1

AI Memory Infrastructure - Persistent memory for AI agents with semantic 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
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
// MCP tool definitions for Engram

use serde_json::json;

use super::protocol::{ToolAnnotations, ToolDefinition};

/// Tool exposure tier for progressive discovery.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ToolTier {
    /// ~20 core tools every agent needs. Always exposed.
    Essential,
    /// ~50 common tools for standard workflows.
    Standard,
    /// ~110 advanced/specialized tools.
    Advanced,
}

/// Structured tool definition with MCP 2025-11-25 annotations.
pub struct ToolDef {
    pub name: &'static str,
    pub description: &'static str,
    pub schema: &'static str,
    pub annotations: ToolAnnotations,
    pub tier: ToolTier,
}

/// All tool definitions for Engram
pub const TOOL_DEFINITIONS: &[ToolDef] = include!("registry.rs");

/// Returns `false` for tool names whose feature flag is not compiled in.
///
/// Called by both `get_tool_definitions` and `get_tool_definitions_tiered` so
/// that `tools/list` never advertises a tool that `tools/call` would reject with
/// "Unknown tool".
fn tool_feature_available(name: &str) -> bool {
    match name {
        // langfuse feature
        "langfuse_connect"
        | "langfuse_sync"
        | "langfuse_sync_status"
        | "langfuse_extract_patterns"
        | "memory_from_trace" => cfg!(feature = "langfuse"),

        // meilisearch feature
        "meilisearch_search"
        | "meilisearch_reindex"
        | "meilisearch_status"
        | "meilisearch_config" => cfg!(feature = "meilisearch"),

        // emergent-graph feature
        "memory_auto_link"
        | "memory_list_auto_links"
        | "memory_auto_link_stats"
        | "memory_cluster"
        | "memory_get_cluster"
        | "memory_list_clusters" => cfg!(feature = "emergent-graph"),

        // multimodal feature (memory_sync_media also needs cloud)
        "memory_sync_media" => cfg!(all(feature = "multimodal", feature = "cloud")),
        "memory_describe_image"
        | "memory_transcribe_audio"
        | "memory_capture_screenshot"
        | "memory_process_video"
        | "memory_list_media"
        | "memory_search_by_image" => cfg!(feature = "multimodal"),

        // duckdb-graph feature
        "memory_graph_path" | "memory_temporal_snapshot" | "memory_scope_snapshot" => {
            cfg!(feature = "duckdb-graph")
        }

        // dream-phase feature
        "dream_run_now"
        | "dream_create"
        | "dream_get"
        | "dream_list"
        | "dream_cancel"
        | "dream_archive"
        | "dream_candidates_list"
        | "dream_candidate_get"
        | "dream_candidate_review"
        | "dream_candidate_apply"
        | "dream_eval_run" => cfg!(feature = "dream-phase"),

        // agent-portability feature
        "attestation_log"
        | "attestation_verify"
        | "attestation_chain_verify"
        | "attestation_list"
        | "snapshot_create"
        | "snapshot_load"
        | "snapshot_inspect" => cfg!(feature = "agent-portability"),

        _ => true,
    }
}

/// Get all tool definitions as ToolDefinition structs.
///
/// Only returns tools whose feature flag is compiled in so that `tools/list`
/// never advertises a tool that `tools/call` would reject.
pub fn get_tool_definitions() -> Vec<ToolDefinition> {
    iter_tool_definitions()
        .map(|def| ToolDefinition {
            name: def.name.to_string(),
            description: def.description.to_string(),
            input_schema: serde_json::from_str(def.schema).unwrap_or(json!({})),
            annotations: Some(def.annotations.clone()),
        })
        .collect()
}

/// Get tool definitions filtered by maximum tier level.
///
/// - `Some("essential")` → only Essential tools + discover_tools
/// - `Some("standard")` → Essential + Standard tools + discover_tools
/// - `Some("all")` or `None` → all tools (backward compatible)
pub fn get_tool_definitions_tiered(max_tier: Option<&str>) -> Vec<ToolDefinition> {
    let max = match max_tier {
        Some("essential") => ToolTier::Essential,
        Some("standard") => ToolTier::Standard,
        _ => ToolTier::Advanced, // "all" or None = everything
    };

    iter_tool_definitions()
        .filter(|def| {
            // discover_tools is always included regardless of tier
            if def.name == "discover_tools" {
                return true;
            }
            // Tier filtering
            matches!(
                (max, def.tier),
                (ToolTier::Essential, ToolTier::Essential)
                    | (ToolTier::Standard, ToolTier::Essential | ToolTier::Standard)
                    | (ToolTier::Advanced, _)
            )
        })
        .map(|def| ToolDefinition {
            name: def.name.to_string(),
            description: def.description.to_string(),
            input_schema: serde_json::from_str(def.schema).unwrap_or(json!({})),
            annotations: Some(def.annotations.clone()),
        })
        .collect()
}

pub(crate) fn iter_tool_definitions() -> impl Iterator<Item = &'static ToolDef> {
    TOOL_DEFINITIONS
        .iter()
        .filter(|def| tool_feature_available(def.name))
}

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

    #[test]
    fn test_tool_definitions_all_parseable() {
        let tools = get_tool_definitions();
        assert!(!tools.is_empty(), "TOOL_DEFINITIONS must not be empty");
        for tool in &tools {
            assert!(!tool.name.is_empty(), "tool name must not be empty");
            assert!(
                !tool.description.is_empty(),
                "tool description must not be empty"
            );
            assert!(
                tool.input_schema.is_object(),
                "tool '{}' schema must be a JSON object",
                tool.name
            );
        }
    }

    #[test]
    fn test_read_only_tools_have_annotation() {
        let tools = get_tool_definitions();
        let read_only_names = ["memory_get", "memory_list", "memory_search", "memory_stats"];
        for name in read_only_names {
            let tool = tools
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{}' not found", name));
            let ann = tool
                .annotations
                .as_ref()
                .expect("annotations must be present");
            assert_eq!(
                ann.read_only_hint,
                Some(true),
                "tool '{}' should have readOnlyHint=true",
                name
            );
        }
    }

    #[test]
    fn test_destructive_tools_have_annotation() {
        let tools = get_tool_definitions();
        let destructive_names = [
            "memory_delete",
            "memory_cleanup_expired",
            "embedding_cache_clear",
        ];
        for name in destructive_names {
            let tool = tools
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{}' not found", name));
            let ann = tool
                .annotations
                .as_ref()
                .expect("annotations must be present");
            assert_eq!(
                ann.destructive_hint,
                Some(true),
                "tool '{}' should have destructiveHint=true",
                name
            );
        }
    }

    #[test]
    fn test_idempotent_tools_have_annotation() {
        let tools = get_tool_definitions();
        let idempotent_names = [
            "memory_extract_entities",
            "memory_rebuild_embeddings",
            "memory_rebuild_crossrefs",
            "lifecycle_run",
            "retention_policy_apply",
        ];
        for name in idempotent_names {
            let tool = tools
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{}' not found", name));
            let ann = tool
                .annotations
                .as_ref()
                .expect("annotations must be present");
            assert_eq!(
                ann.idempotent_hint,
                Some(true),
                "tool '{}' should have idempotentHint=true",
                name
            );
        }
    }

    #[test]
    fn test_tier_distribution() {
        let essential = TOOL_DEFINITIONS
            .iter()
            .filter(|t| t.tier == ToolTier::Essential)
            .count();
        let standard = TOOL_DEFINITIONS
            .iter()
            .filter(|t| t.tier == ToolTier::Standard)
            .count();
        let advanced = TOOL_DEFINITIONS
            .iter()
            .filter(|t| t.tier == ToolTier::Advanced)
            .count();
        // Ranges widened after the dispatch/registry reconciliation that
        // advertised ~59 previously-hidden tools (registry grew to ~250).
        assert!((18..=25).contains(&essential), "essential: {}", essential);
        assert!((60..=130).contains(&standard), "standard: {}", standard);
        // Advanced count depends on feature flags (feature-gated tools are Advanced)
        assert!(advanced >= 80, "advanced: {}", advanced);
        assert_eq!(essential + standard + advanced, TOOL_DEFINITIONS.len());
    }

    /// Guard against the dispatch/registry drift bug class: every tool routed
    /// in `handlers::dispatch` must have a `TOOL_DEFINITIONS` entry, and every
    /// advertised tool must be routable. A new dispatch arm without a registry
    /// entry (or vice-versa) fails CI here.
    ///
    /// Text-level + feature-independent: both sources are parsed as text, so
    /// `#[cfg]`-gated arms and entries appear in both and cancel out.
    #[test]
    fn test_dispatch_registry_parity() {
        use std::collections::BTreeSet;

        let mod_src = include_str!("../handlers/mod.rs");

        // Dispatch arm names: inside `pub fn dispatch`, up to the catch-all.
        let d_start = mod_src
            .find("pub fn dispatch")
            .expect("dispatch fn present");
        let d_end = mod_src[d_start..]
            .find(r#"_ => json!({"error": format!("Unknown tool"#)
            .map(|i| d_start + i)
            .expect("dispatch catch-all present");
        let mut dispatch: BTreeSet<&str> = BTreeSet::new();
        for line in mod_src[d_start..d_end].lines() {
            let t = line.trim();
            if t.starts_with('"') && t.contains("=>") {
                if let Some(name) = t[1..].split('"').next() {
                    dispatch.insert(name);
                }
            }
        }

        // Registry names: from the composed source, mirroring old behavior for cfg-gated tools.
        let registry_src = include_str!("registry.rs");
        let mut registry: BTreeSet<&str> = BTreeSet::new();
        for line in registry_src.lines() {
            let t = line.trim();
            if let Some(rest) = t.strip_prefix("name:") {
                if let Some(inner) = rest.trim().strip_prefix('"') {
                    if let Some(name) = inner.split('"').next() {
                        registry.insert(name);
                    }
                }
            }
        }

        // Legitimate asymmetries — keep empty; document any future addition.
        const DISPATCH_ONLY_OK: &[&str] = &[];
        const REGISTRY_ONLY_OK: &[&str] = &[];

        // Sanity: the parser actually found the tables.
        assert!(
            dispatch.len() > 100,
            "parsed too few dispatch arms: {}",
            dispatch.len()
        );
        assert!(
            registry.len() > 100,
            "parsed too few registry defs: {}",
            registry.len()
        );

        let d_only: Vec<&str> = dispatch
            .difference(&registry)
            .filter(|n| !DISPATCH_ONLY_OK.contains(n))
            .copied()
            .collect();
        let r_only: Vec<&str> = registry
            .difference(&dispatch)
            .filter(|n| !REGISTRY_ONLY_OK.contains(n))
            .copied()
            .collect();

        assert!(
            d_only.is_empty(),
            "Tools CALLABLE but NOT advertised — add a TOOL_DEFINITIONS entry: {:?}",
            d_only
        );
        assert!(
            r_only.is_empty(),
            "Tools ADVERTISED but NOT callable — add a dispatch arm or remove from registry: {:?}",
            r_only
        );
    }

    #[test]
    fn test_essential_tools_present() {
        let essential_names = [
            "memory_create",
            "context_seed",
            "memory_get",
            "memory_update",
            "memory_delete",
            "memory_list",
            "memory_search",
            "memory_search_compact",
            "memory_expand",
            "memory_get_injection_prompt",
            "memory_link",
            "memory_related",
            "memory_traverse",
            "memory_stats",
            "workspace_list",
            "session_index",
            "session_list",
            "identity_create",
            "identity_resolve",
        ];
        for name in essential_names {
            let tool = TOOL_DEFINITIONS
                .iter()
                .find(|t| t.name == name)
                .unwrap_or_else(|| panic!("tool '{}' not found", name));
            assert_eq!(
                tool.tier,
                ToolTier::Essential,
                "tool '{}' should be Essential",
                name
            );
        }
    }

    #[test]
    fn test_annotations_serialize_with_camel_case_keys() {
        let tools = get_tool_definitions();
        let memory_get = tools.iter().find(|t| t.name == "memory_get").unwrap();
        let json = serde_json::to_string(memory_get).expect("serialization must succeed");
        assert!(
            json.contains("readOnlyHint"),
            "should serialize as readOnlyHint"
        );
        assert!(
            !json.contains("read_only_hint"),
            "must not use snake_case key"
        );
    }

    #[test]
    fn test_recent_activity_tool_is_defined_as_read_only() {
        let tools = get_tool_definitions();
        let tool = tools
            .iter()
            .find(|t| t.name == "recent_activity")
            .expect("recent_activity tool must be defined");
        let ann = tool
            .annotations
            .as_ref()
            .expect("annotations must be present");
        assert_eq!(
            ann.read_only_hint,
            Some(true),
            "recent_activity should have readOnlyHint=true"
        );
    }

    #[test]
    fn test_mutating_tool_has_no_hints() {
        let tools = get_tool_definitions();
        let memory_create = tools.iter().find(|t| t.name == "memory_create").unwrap();
        let ann = memory_create
            .annotations
            .as_ref()
            .expect("annotations must be present");
        assert!(ann.read_only_hint.is_none());
        assert!(ann.destructive_hint.is_none());
        assert!(ann.idempotent_hint.is_none());
        // Serialized form should omit None fields
        let json = serde_json::to_string(ann).expect("serialization must succeed");
        // mutating annotations serialize as empty object since all fields are None
        assert_eq!(json, "{}");
    }
}