chaotic_semantic_memory 0.3.6

AI memory systems with hyperdimensional vectors and chaotic reservoirs
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
use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

#[derive(Parser, Debug)]
#[command(name = "csm")]
#[command(about = "Chaotic Semantic Memory CLI", long_about = None)]
#[command(version)]
pub struct CliArgs {
    #[command(subcommand)]
    pub command: Commands,

    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
    pub verbose: u8,

    /// Path to database file. If not specified, uses git-local storage when in a git repo.
    #[arg(short, long, global = true, value_name = "PATH")]
    pub database: Option<PathBuf>,

    /// Force git-local storage (.git/memory-index/csm.db).
    /// Creates "never committed, per-clone" storage inside the .git directory.
    /// Error if not in a git repository.
    #[arg(long, global = true)]
    pub git_local: bool,

    /// Override the default git-local index path.
    /// Only used when --git-local is specified or no database is given in a git repo.
    #[arg(long, global = true, value_name = "PATH")]
    pub index_path: Option<PathBuf>,

    #[arg(long, global = true, value_enum, default_value = "table")]
    pub output_format: OutputFormat,

    /// Namespace for isolation (default: _default).
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
    Json,
    Table,
    Quiet,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    Inject(InjectArgs),
    Probe(ProbeArgs),
    Query(QueryArgs),
    Associate(AssociateArgs),
    Export(ExportArgs),
    Import(ImportArgs),
    Version(VersionArgs),
    Completions(CompletionsArgs),
    /// Index JSONL file content into memory.
    IndexJsonl(IndexJsonlArgs),
    /// Index Markdown files from directory into memory.
    IndexDir(IndexDirArgs),
    /// Delete a concept from memory.
    Delete(DeleteArgs),
    /// Get concept details by ID.
    Get(GetArgs),
    /// Update concept vector or metadata.
    Update(UpdateArgs),
    /// Remove association(s) from a concept.
    Disassociate(DisassociateArgs),
    /// List associations for a concept.
    Associations(AssociationsArgs),
    /// Traverse graph from a starting concept.
    Traverse(TraverseArgs),
    /// Find shortest path between two concepts.
    Path(PathArgs),
    /// Similarity search with metadata filter.
    ProbeFiltered(ProbeFilteredArgs),
    /// Database statistics.
    Stats(StatsArgs),
    /// Framework performance metrics.
    Metrics(MetricsArgs),
    /// Watch for real-time memory events.
    Watch(WatchArgs),
    /// GraphRAG retrieval: similarity + graph traversal hybrid.
    ProbeGraph(ProbeGraphArgs),
    /// MCP server commands.
    #[cfg(feature = "mcp")]
    #[command(subcommand)]
    Mcp(crate::cli::mcp::McpCommands),
}

#[derive(Args, Debug, Clone)]
pub struct InjectArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    #[arg(required = true)]
    pub concept_id: String,

    #[arg(short, long)]
    pub from_file: Option<PathBuf>,

    #[arg(long, default_value = "random", value_enum)]
    pub vector_source: VectorSource,

    /// Text to encode into a vector.
    #[arg(short, long)]
    pub text: Option<String>,

    /// Use external embedding model if configured.
    #[arg(long)]
    pub use_embeddings: bool,

    /// Embedding provider: 'hdc', 'fastembed[:model]', 'openai[:model]', 'voyage[:model]'.
    #[arg(long, value_name = "PROVIDER")]
    pub provider: Option<String>,

    #[arg(short, long, value_name = "JSON")]
    pub metadata: Option<String>,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum VectorSource {
    Random,
    File,
    Stdin,
    Text,
}

#[derive(Args, Debug, Clone)]
pub struct ProbeArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    #[arg(required = true)]
    pub concept_id: String,

    #[arg(short = 'k', long, default_value = "10")]
    pub top_k: usize,

    #[arg(short, long)]
    pub threshold: Option<f64>,
}

/// Arguments for text-based similarity query.
#[derive(Args, Debug, Clone)]
#[command(alias = "probe-text")]
pub struct QueryArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Text to encode and search for similar concepts.
    #[arg(required = true)]
    pub text: String,

    /// Maximum number of results to return.
    #[arg(short = 'k', long, default_value = "10")]
    pub top_k: usize,

    /// Minimum similarity score (0.0-1.0) for results.
    #[arg(short, long, default_value = "0.0")]
    pub min_score: f64,

    /// Use code-aware encoding for source code content.
    #[arg(long)]
    pub code_aware: bool,

    /// Compact output: trim long text to 200 characters.
    #[arg(long)]
    pub compact: bool,

    /// Disable hybrid mode (use semantic-only HDC search).
    #[arg(long)]
    pub semantic_only: bool,

    /// Use keyword-only BM25 search (no semantic matching).
    #[arg(long)]
    pub keyword_only: bool,

    /// Override automatic keyword weight (0.0-1.0).
    /// Default is query-length-dependent: 0.9 for 1-2 tokens, 0.7 for 3-4,
    /// 0.4 for 5-8, 0.2 for 9+.
    #[arg(long, value_name = "WEIGHT")]
    pub keyword_weight: Option<f64>,

    /// Embedding provider: 'hdc', 'fastembed[:model]', 'openai[:model]', 'voyage[:model]'.
    #[arg(long, value_name = "PROVIDER")]
    pub provider: Option<String>,
}

#[derive(Args, Debug, Clone)]
pub struct AssociateArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    #[arg(required = true)]
    pub source_id: String,

    #[arg(required = true)]
    pub target_id: String,

    #[arg(short, long, default_value = "1.0")]
    pub strength: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ExportFormat {
    Json,
    Binary,
}

#[derive(Args, Debug, Clone)]
pub struct ExportArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    #[arg(short, long, default_value = "export.json")]
    pub output: PathBuf,

    #[arg(long, value_enum, default_value = "json")]
    pub format: ExportFormat,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ImportFormat {
    Json,
    Binary,
    Auto,
}

#[derive(Args, Debug, Clone)]
pub struct ImportArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    #[arg(required = true)]
    pub input: PathBuf,

    #[arg(long, value_enum, default_value = "auto")]
    pub format: ImportFormat,

    #[arg(long)]
    pub merge: bool,
}

#[derive(Args, Debug, Clone)]
pub struct VersionArgs {
    #[arg(long)]
    pub detailed: bool,
}

#[derive(Args, Debug, Clone)]
pub struct CompletionsArgs {
    #[arg(value_enum)]
    pub shell: clap_complete::Shell,

    #[arg(short, long, value_name = "FILE")]
    pub output: Option<PathBuf>,
}

/// Arguments for indexing JSONL files.
#[derive(Args, Debug, Clone)]
pub struct IndexJsonlArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Path to JSONL file to index.
    #[arg(short = 'F', long, value_name = "FILE")]
    pub file: PathBuf,

    /// Field name containing text to encode (default: "text").
    #[arg(short, long, default_value = "text")]
    pub field: String,

    /// Field name containing unique ID (optional).
    #[arg(long, value_name = "FIELD")]
    pub id_field: Option<String>,

    /// Field name containing comma-separated tags (optional).
    #[arg(long, value_name = "FIELD")]
    pub tag_field: Option<String>,

    /// Use code-aware encoding for source code content.
    #[arg(long)]
    pub code_aware: bool,
}

/// Arguments for indexing Markdown directory.
#[derive(Args, Debug, Clone)]
pub struct IndexDirArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Glob pattern(s) for files to index (can be repeated).
    #[arg(short, long, required = true, value_name = "PATTERN")]
    pub glob: Vec<String>,

    /// Minimum heading level to chunk (default: 2 for ## sections).
    #[arg(long, default_value = "2", value_name = "LEVEL")]
    pub heading_level: usize,

    /// Use code-aware encoding for source code content.
    #[arg(long)]
    pub code_aware: bool,
}

/// Arguments for the delete command.
#[derive(Args, Debug, Clone)]
pub struct DeleteArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Concept ID to delete.
    #[arg(required = true)]
    pub concept_id: String,

    /// Skip confirmation prompt.
    #[arg(short, long)]
    pub force: bool,
}

/// Arguments for the get command.
#[derive(Args, Debug, Clone)]
pub struct GetArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Concept ID to retrieve.
    #[arg(required = true)]
    pub concept_id: String,
}

/// Arguments for the update command.
#[derive(Args, Debug, Clone)]
pub struct UpdateArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Concept ID to update.
    #[arg(required = true)]
    pub concept_id: String,

    /// Generate new vector from text encoding.
    #[arg(long, value_name = "TEXT")]
    pub vector_from_text: Option<String>,

    /// Use code-aware encoding for vector generation.
    #[arg(long)]
    pub code_aware: bool,

    /// Update metadata with JSON object (merged with existing).
    #[arg(short, long, value_name = "JSON")]
    pub metadata: Option<String>,

    /// Replace metadata entirely instead of merging.
    #[arg(long)]
    pub replace_metadata: bool,
}

/// Arguments for the disassociate command.
#[derive(Args, Debug, Clone)]
pub struct DisassociateArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Source concept ID (association owner).
    #[arg(required = true)]
    pub from: String,

    /// Target concept ID to remove association to.
    /// If omitted, clears all associations from source.
    #[arg(required = false)]
    pub to: Option<String>,
}

/// Arguments for listing associations of a concept.
#[derive(Args, Debug, Clone)]
pub struct AssociationsArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Concept ID to query associations for.
    #[arg(required = true)]
    pub concept_id: String,

    /// List incoming associations (reverse direction) instead of outbound.
    #[arg(short, long)]
    pub reverse: bool,
}

/// Arguments for BFS traversal from a starting concept.
#[derive(Args, Debug, Clone)]
pub struct TraverseArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Starting concept ID for traversal.
    #[arg(required = true)]
    pub start: String,

    /// Maximum number of hops to traverse.
    #[arg(long, default_value = "3")]
    pub depth: usize,

    /// Minimum edge strength to follow (0.0-1.0).
    #[arg(short, long, default_value = "0.0")]
    pub min_strength: f64,
}

/// Arguments for finding shortest path between two concepts.
#[derive(Args, Debug, Clone)]
pub struct PathArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Starting concept ID.
    #[arg(required = true)]
    pub from: String,

    /// Target concept ID.
    #[arg(required = true)]
    pub to: String,

    /// Use weighted Dijkstra (stronger edges = lower cost) instead of hop count.
    #[arg(short, long)]
    pub weighted: bool,
}

/// Arguments for filtered similarity probe.
#[derive(Args, Debug, Clone)]
pub struct ProbeFilteredArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Concept ID to use as query vector.
    #[arg(required = true)]
    pub concept_id: String,

    /// Maximum number of results to return.
    #[arg(short = 'k', long, default_value = "10")]
    pub top_k: usize,

    /// JSON metadata filter expression.
    /// Examples: '{"Eq":["type","document"]}' '{"And":[{"Eq":["tag","rust"]},{"Exists":["title"]}]}'
    #[arg(short, long, value_name = "JSON")]
    pub filter: String,
}

/// Arguments for stats command.
#[derive(Args, Debug, Clone)]
pub struct StatsArgs;

/// Arguments for metrics command.
#[derive(Args, Debug, Clone)]
pub struct MetricsArgs {
    /// Reset metrics counters (not yet implemented).
    #[arg(long)]
    pub reset: bool,
}

/// Arguments for watch command.
#[derive(Args, Debug, Clone)]
pub struct WatchArgs {
    /// Event type filter: all, injected, updated, deleted, associated, disassociated.
    #[arg(short, long, default_value = "all")]
    pub filter: String,
}

/// Arguments for GraphRAG retrieval.
#[derive(Args, Debug, Clone)]
pub struct ProbeGraphArgs {
    #[arg(long, global = true, default_value = "_default")]
    pub namespace: String,
    /// Text query for similarity search.
    #[arg(required = true)]
    pub text: String,

    /// Number of anchor concepts from initial probe.
    #[arg(long, default_value = "5")]
    pub anchors: usize,

    /// Maximum hops to traverse from each anchor.
    #[arg(long, default_value = "2")]
    pub hops: usize,

    /// Minimum association strength to follow.
    #[arg(long, default_value = "0.0")]
    pub min_strength: f32,

    /// Weight for similarity score component.
    #[arg(long, default_value = "0.6")]
    pub similarity_weight: f32,

    /// Weight for graph distance component.
    #[arg(long, default_value = "0.4")]
    pub graph_weight: f32,

    /// Final top-K results to return.
    #[arg(short = 'k', long, default_value = "20")]
    pub top_k: usize,
}