mx 0.1.159

A Swiss army knife for Claude Code and multi-agent toolkits
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
use anyhow::Result;
use std::path::Path;

use crate::knowledge::KnowledgeEntry;
use crate::types::{
    Agent, ApplicabilityType, Category, ContentType, EntryType, Project, Relationship,
    RelationshipType, Session, SessionType, SourceType,
};

/// Agent context for privacy-aware queries
#[derive(Debug, Clone)]
pub struct AgentContext {
    /// Current agent ID (None = anonymous/public-only access)
    pub agent_id: Option<String>,
    /// Whether to include private entries (requires matching agent_id)
    pub include_private: bool,
}

impl AgentContext {
    /// Public-only access (no private entries visible)
    pub fn public_only() -> Self {
        Self {
            agent_id: None,
            include_private: false,
        }
    }

    /// Agent with full access to their private entries
    pub fn for_agent(id: impl Into<String>) -> Self {
        Self {
            agent_id: Some(id.into()),
            include_private: true,
        }
    }

    /// Agent but only viewing public entries
    pub fn public_for_agent(id: impl Into<String>) -> Self {
        Self {
            agent_id: Some(id.into()),
            include_private: false,
        }
    }
}

/// Filter for resonance-based queries
#[derive(Debug, Clone, Default)]
pub struct KnowledgeFilter {
    pub min_resonance: Option<i32>,
    pub max_resonance: Option<i32>,
    pub categories: Option<Vec<String>>,
}

/// Result of a wake-up cascade query
#[derive(Debug, Clone, serde::Serialize)]
pub struct WakeCascade {
    /// Layer 1: Foundational/transformative, resonance 8+
    pub core: Vec<crate::knowledge::KnowledgeEntry>,
    /// Layer 2: Last N days, sorted by resonance * recency
    pub recent: Vec<crate::knowledge::KnowledgeEntry>,
    /// Layer 3: Anchored to core/recent, resonance 5+
    pub bridges: Vec<crate::knowledge::KnowledgeEntry>,
}

impl WakeCascade {
    pub fn all_ids(&self) -> Vec<String> {
        self.core
            .iter()
            .chain(self.recent.iter())
            .chain(self.bridges.iter())
            .map(|e| e.id.clone())
            .collect()
    }
}

/// Result of an edit_content operation
#[derive(Debug, Clone)]
pub struct EditResult {
    /// Number of replacements made
    pub replacements: usize,
    /// The updated content (for display purposes)
    pub new_content: String,
}

/// Result of a reinforce operation
#[derive(Debug, Clone, serde::Serialize)]
pub struct ReinforcementResult {
    /// Entry ID that was reinforced
    pub id: String,
    /// Previous resonance value
    pub old_resonance: i32,
    /// New resonance value (after increment and cap)
    pub new_resonance: i32,
    /// Amount added (before cap)
    pub amount_added: i32,
    /// Whether the cap was hit
    pub capped: bool,
    /// New last_activated timestamp
    pub last_activated: String,
    /// New activation count
    pub activation_count: i32,
}

/// Abstract interface for knowledge storage backends
pub trait KnowledgeStore {
    // =========================================================================
    // KNOWLEDGE CRUD OPERATIONS
    // =========================================================================

    /// Upsert a knowledge entry (insert or update)
    fn upsert_knowledge(&self, entry: &KnowledgeEntry) -> Result<()>;

    /// Get a knowledge entry by ID
    fn get(&self, id: &str, ctx: &AgentContext) -> Result<Option<KnowledgeEntry>>;

    /// Delete a knowledge entry (respects visibility: agents can only delete entries they can see)
    fn delete(&self, id: &str, ctx: &AgentContext) -> Result<bool>;

    /// Search knowledge entries
    fn search(
        &self,
        query: &str,
        ctx: &AgentContext,
        filter: &KnowledgeFilter,
    ) -> Result<Vec<KnowledgeEntry>>;

    /// Semantic search using vector similarity
    fn semantic_search(
        &self,
        query_embedding: &[f32],
        ctx: &AgentContext,
        filter: &KnowledgeFilter,
        limit: usize,
    ) -> Result<Vec<KnowledgeEntry>>;

    /// List entries by category
    fn list_by_category(
        &self,
        category: &str,
        ctx: &AgentContext,
        filter: &KnowledgeFilter,
    ) -> Result<Vec<KnowledgeEntry>>;

    /// Count entries by category (fast path — single COUNT query, no row hydration).
    /// Avoids the N+1 pattern of `list_by_category(..)?.len()` which fetches every
    /// row's full body plus follow-up queries for tags/applicability per entry.
    fn count_by_category(
        &self,
        category: &str,
        ctx: &AgentContext,
        filter: &KnowledgeFilter,
    ) -> Result<usize>;

    /// List all entries
    fn list_all(&self, ctx: &AgentContext) -> Result<Vec<KnowledgeEntry>>;

    /// Count total entries
    fn count(&self) -> Result<usize>;

    /// Wake-up cascade query (three-layer resonance)
    fn wake_cascade(
        &self,
        ctx: &AgentContext,
        limit: usize,
        min_resonance: Option<i32>,
        days: i64,
    ) -> Result<WakeCascade>;

    /// Update activation counts for loaded blooms, resetting last_activated timestamp.
    /// Use for intentional single-entry access (e.g. `show`, `fact-session`).
    fn update_activations(&self, ids: &[String]) -> Result<()>;

    /// Update only the summary field of a knowledge entry (targeted update, bypasses SCHEMAFULL UPSERT)
    /// Respects visibility: agents can only update summaries on entries they can see.
    /// Returns Ok(false) for entries that don't exist OR that the agent can't see
    /// (to avoid leaking existence of private entries).
    ///
    /// # Arguments
    /// * `id` - Entry ID, with or without "kn-" prefix (normalized internally)
    /// * `summary` - New summary value to set
    /// * `ctx` - Agent context for visibility filtering
    fn update_summary(&self, id: &str, summary: &str, ctx: &AgentContext) -> Result<bool>;

    /// Increment activation_count only — does NOT reset last_activated.
    /// Use for passive bulk surfacing (wake cascade, for-session view) so entries
    /// continue decaying at their normal rate.
    fn increment_activation_count(&self, ids: &[String]) -> Result<()>;

    /// Query recent ephemeral facts with decay computation
    fn query_recent_facts(&self, days: i32) -> Result<Vec<KnowledgeEntry>>;

    /// Query recent facts across ALL resonance types (ephemeral, foundational, transformative, etc.)
    /// with decay computation. Foundational/transformative entries are exempt from decay.
    fn query_recent_facts_all_types(&self, days: i32) -> Result<Vec<KnowledgeEntry>>;

    /// Reinforce a knowledge entry (increment resonance, update last_activated, increment activation_count)
    /// Respects visibility: agents can only reinforce entries they can see.
    /// Returns Ok(None) for entries that don't exist OR that the agent can't see
    /// (to avoid leaking existence of private entries).
    ///
    /// # Arguments
    /// * `id` - Entry ID to reinforce
    /// * `amount` - Amount to increase resonance by
    /// * `cap` - Maximum resonance value (None = no cap)
    /// * `ctx` - Agent context for visibility filtering
    ///
    /// # Returns
    /// Result containing the old/new values and whether cap was hit, or None if not found/not visible
    fn reinforce(
        &self,
        id: &str,
        amount: i32,
        cap: Option<i32>,
        ctx: &AgentContext,
    ) -> Result<Option<ReinforcementResult>>;

    // =========================================================================
    // CONTENT PATCH OPERATIONS
    // =========================================================================

    /// Edit content by finding and replacing text
    ///
    /// Returns an error if:
    /// - Entry not found
    /// - Entry has no body content
    /// - `old_text` is not found in the content
    /// - `old_text` appears multiple times and neither `replace_all` nor `nth` is specified
    ///
    /// # Arguments
    /// * `id` - Entry ID to update
    /// * `ctx` - Agent context for privacy filtering
    /// * `old_text` - Text to find in the content
    /// * `new_text` - Replacement text
    /// * `replace_all` - If true, replace all occurrences
    /// * `nth` - If Some(n), replace only the nth occurrence (1-indexed)
    fn edit_content(
        &self,
        id: &str,
        ctx: &AgentContext,
        old_text: &str,
        new_text: &str,
        replace_all: bool,
        nth: Option<usize>,
    ) -> Result<EditResult>;

    /// Append content to the end of an entry's body
    ///
    /// Adds the new content after the existing content, separated by a newline.
    /// If the entry has no body, the new content becomes the body.
    fn append_content(&self, id: &str, ctx: &AgentContext, content: &str) -> Result<()>;

    /// Prepend content to the start of an entry's body
    ///
    /// Adds the new content before the existing content, separated by a newline.
    /// If the entry has no body, the new content becomes the body.
    fn prepend_content(&self, id: &str, ctx: &AgentContext, content: &str) -> Result<()>;

    // =========================================================================
    // BACKUP OPERATIONS (Issue #206)
    // =========================================================================

    /// Create a pre-mutation backup of entry content
    fn backup_content(
        &self,
        entry: &KnowledgeEntry,
        operation: &str,
        agent: Option<&str>,
    ) -> Result<String>;

    /// List backups for a specific entry, newest first
    fn list_backups(&self, entry_id: &str) -> Result<Vec<crate::types::MemoryBackup>>;

    /// Get the most recent backup for an entry
    fn latest_backup(&self, entry_id: &str) -> Result<Option<crate::types::MemoryBackup>>;

    /// Purge old backups, keeping the most recent `keep` per entry
    fn purge_backups(&self, entry_id: &str, keep: usize) -> Result<()>;

    // =========================================================================
    // TAG OPERATIONS
    // =========================================================================

    /// Get tags for an entry
    fn get_tags_for_entry(&self, entry_id: &str) -> Result<Vec<String>>;

    /// Set tags for an entry (replaces all)
    fn set_tags_for_entry(&self, entry_id: &str, tags: &[String]) -> Result<()>;

    /// List all distinct tags, optionally filtered by category
    fn list_all_tags(&self, category: Option<&str>) -> Result<Vec<String>>;

    // =========================================================================
    // APPLICABILITY OPERATIONS
    // =========================================================================

    /// Get applicability for an entry
    fn get_applicability_for_entry(&self, entry_id: &str) -> Result<Vec<String>>;

    /// Set applicability for an entry (replaces all)
    fn set_applicability_for_entry(&self, entry_id: &str, ids: &[String]) -> Result<()>;

    /// List all applicability types
    fn list_applicability_types(&self) -> Result<Vec<ApplicabilityType>>;

    /// Upsert applicability type
    fn upsert_applicability_type(&self, atype: &ApplicabilityType) -> Result<()>;

    // =========================================================================
    // CATEGORY OPERATIONS
    // =========================================================================

    /// List all categories
    fn list_categories(&self) -> Result<Vec<Category>>;

    /// Get category by ID
    fn get_category(&self, id: &str) -> Result<Option<Category>>;

    /// Upsert category
    fn upsert_category(&self, category: &Category) -> Result<()>;

    /// Delete category (fails if entries use it)
    fn delete_category(&self, id: &str) -> Result<bool>;

    // =========================================================================
    // PROJECT OPERATIONS
    // =========================================================================

    /// List all projects
    fn list_projects(&self, active_only: bool) -> Result<Vec<Project>>;

    /// Get project by ID
    fn get_project(&self, id: &str) -> Result<Option<Project>>;

    /// Upsert project
    fn upsert_project(&self, project: &Project) -> Result<()>;

    /// Get tags for a project
    fn get_tags_for_project(&self, project_id: &str) -> Result<Vec<String>>;

    /// Set tags for a project
    fn set_tags_for_project(&self, project_id: &str, tags: &[String]) -> Result<()>;

    /// Get applicability for a project
    fn get_applicability_for_project(&self, project_id: &str) -> Result<Vec<String>>;

    /// Set applicability for a project
    fn set_applicability_for_project(&self, project_id: &str, ids: &[String]) -> Result<()>;

    // =========================================================================
    // AGENT OPERATIONS
    // =========================================================================

    /// List all agents
    fn list_agents(&self) -> Result<Vec<Agent>>;

    /// Get agent by ID
    fn get_agent(&self, id: &str) -> Result<Option<Agent>>;

    /// Upsert agent
    fn upsert_agent(&self, agent: &Agent) -> Result<()>;

    // =========================================================================
    // RELATIONSHIP OPERATIONS
    // =========================================================================

    /// List relationships for an entry
    fn list_relationships_for_entry(&self, entry_id: &str) -> Result<Vec<Relationship>>;

    /// Add relationship between entries
    fn add_relationship(&self, from: &str, to: &str, rel_type: &str) -> Result<String>;

    /// Delete relationship
    fn delete_relationship(&self, id: &str) -> Result<bool>;

    /// Get facts extracted from a specific session
    fn get_facts_for_session(&self, session_id: &str) -> Result<Vec<String>>;

    /// Get the session a fact was extracted from
    fn get_session_for_fact(&self, fact_id: &str) -> Result<Option<String>>;

    // =========================================================================
    // SESSION OPERATIONS
    // =========================================================================

    /// List sessions
    fn list_sessions(&self, project_id: Option<&str>) -> Result<Vec<Session>>;

    /// Get session by ID
    fn get_session(&self, id: &str) -> Result<Option<Session>>;

    /// Upsert session
    fn upsert_session(&self, session: &Session) -> Result<()>;

    // =========================================================================
    // TYPE LOOKUP OPERATIONS
    // =========================================================================

    /// List all source types
    fn list_source_types(&self) -> Result<Vec<SourceType>>;

    /// List all entry types
    fn list_entry_types(&self) -> Result<Vec<EntryType>>;

    /// List all content types
    fn list_content_types(&self) -> Result<Vec<ContentType>>;

    /// List all session types
    fn list_session_types(&self) -> Result<Vec<SessionType>>;

    /// List all relationship types
    fn list_relationship_types(&self) -> Result<Vec<RelationshipType>>;

    // =========================================================================
    // WAKE SESSION OPERATIONS (server-side ritual state)
    // =========================================================================

    /// Create a new wake session record, return the session_id
    fn create_wake_session(&self, session: &crate::wake_token::WakeSession) -> Result<String>;

    /// Get a wake session by ID
    fn get_wake_session(&self, session_id: &str) -> Result<Option<crate::wake_token::WakeSession>>;

    /// Update an existing wake session (save mutated state)
    fn update_wake_session(&self, session: &crate::wake_token::WakeSession) -> Result<()>;

    /// Delete a wake session (cleanup after ritual completes)
    fn delete_wake_session(&self, session_id: &str) -> Result<()>;

    // =========================================================================
    // MIGRATION & INTROSPECTION
    // =========================================================================

    /// List tables (for migration status)
    fn list_tables(&self) -> Result<Vec<String>>;
}

/// Factory function to create the SurrealDB store
pub fn create_store(db_path: &Path) -> Result<Box<dyn KnowledgeStore>> {
    create_store_with_verbose(db_path, false)
}

/// Factory function with verbose control
pub fn create_store_with_verbose(db_path: &Path, verbose: bool) -> Result<Box<dyn KnowledgeStore>> {
    let surreal_path = db_path.with_extension("surreal");
    Ok(Box::new(
        crate::surreal_db::SurrealDatabase::open_with_verbose(surreal_path, verbose)?,
    ))
}