rust-memex 0.6.5

Operator CLI + MCP server: canonical corpus second: semantic index second to aicx
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
//! Tests for the preprocessing module

use super::*;

#[test]
fn test_default_config() {
    let config = PreprocessingConfig::default();
    assert!(config.remove_tool_artifacts);
    assert!(config.remove_cli_output);
    // CRITICAL: remove_metadata defaults to false to preserve timestamps!
    assert!(!config.remove_metadata);
    assert_eq!(config.min_content_length, 50);
    assert_eq!(config.dedupe_threshold, 0.95);
}

#[test]
fn test_timestamps_preserved_by_default() {
    // P0 FIX VERIFICATION: Timestamps MUST be preserved by default!
    let preprocessor = Preprocessor::with_defaults();

    let input = "Meeting at 2024-11-13T10:30:00Z with UUID 550e8400-e29b-41d4-a716-446655440000.";
    let result = preprocessor.extract_semantic_content(input);

    // Timestamps should be preserved (NOT replaced with [TIMESTAMP])
    assert!(
        result.contains("2024-11-13T10:30:00Z"),
        "Timestamp should be preserved by default"
    );
    assert!(
        !result.contains("[TIMESTAMP]"),
        "Timestamps should NOT be sanitized by default"
    );

    // UUIDs should also be preserved
    assert!(
        result.contains("550e8400-e29b-41d4-a716-446655440000"),
        "UUID should be preserved by default"
    );
    assert!(
        !result.contains("[UUID]"),
        "UUIDs should NOT be sanitized by default"
    );
}

#[test]
fn test_remove_tool_artifacts_function_calls() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"Here is some context. <function_calls><invoke name="test"><parameter>value</parameter></invoke></function_calls> And here is the result."#;
    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("function_calls"));
    assert!(!result.contains("invoke"));
    assert!(result.contains("Here is some context"));
    assert!(result.contains("And here is the result"));
}

#[test]
fn test_remove_tool_artifacts_antml_tags() {
    let preprocessor = Preprocessor::with_defaults();

    // Build the test string to avoid interpretation
    let tag_open = format!("<{}:{} name=\"test\">", "antml", "invoke");
    let tag_close = format!("</{}:{}>", "antml", "invoke");
    let input = format!("Before {}content inside{} After", tag_open, tag_close);

    let result = preprocessor.extract_semantic_content(&input);

    assert!(!result.contains("antml"));
    assert!(result.contains("Before"));
    assert!(result.contains("After"));
}

#[test]
fn test_remove_git_status_output() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"Looking at the repo:
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  modified:   src/lib.rs
  modified:   Cargo.toml

Now let me fix that."#;

    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("On branch"));
    assert!(!result.contains("Your branch is"));
    assert!(!result.contains("Changes not staged"));
    assert!(!result.contains("modified:"));
    assert!(result.contains("Looking at the repo"));
    assert!(result.contains("Now let me fix that"));
}

#[test]
fn test_remove_cargo_output() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"Building the project:
   Compiling rust-memex v0.1.0
   Compiling tokio v1.0.0
    Finished release [optimized] target(s) in 2.34s
     Running target/release/rust_memex

Build complete!"#;

    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("Compiling"));
    assert!(!result.contains("Finished"));
    assert!(!result.contains("Running"));
    assert!(result.contains("Building the project"));
    assert!(result.contains("Build complete"));
}

#[test]
fn test_remove_file_listing() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"Directory contents:
total 42
drwxr-xr-x  5 user staff  160 Dec 24 10:00 src
-rw-r--r--  1 user staff 1234 Dec 24 09:00 Cargo.toml
-rw-r--r--  1 user staff  567 Dec 24 08:00 README.md

That's what we have."#;

    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("total 42"));
    assert!(!result.contains("drwxr-xr-x"));
    assert!(result.contains("Directory contents"));
    assert!(result.contains("That's what we have"));
}

#[test]
fn test_remove_uuid() {
    // Must explicitly enable remove_metadata to test UUID sanitization
    let preprocessor = Preprocessor::new(PreprocessingConfig {
        remove_metadata: true,
        ..Default::default()
    });

    let input = "Session 550e8400-e29b-41d4-a716-446655440000 started. Working on the task.";
    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("550e8400-e29b-41d4-a716-446655440000"));
    assert!(result.contains("[UUID]"));
    assert!(result.contains("Session"));
    assert!(result.contains("started"));
}

#[test]
fn test_remove_timestamps() {
    // Must explicitly enable remove_metadata to test timestamp sanitization
    let preprocessor = Preprocessor::new(PreprocessingConfig {
        remove_metadata: true,
        ..Default::default()
    });

    let input = "Created at 2024-12-24T10:30:00Z. Last modified 2024-12-24T11:00:00+01:00.";
    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("2024-12-24T"));
    assert!(result.contains("[TIMESTAMP]"));
    assert!(result.contains("Created at"));
    assert!(result.contains("Last modified"));
}

#[test]
fn test_remove_session_id() {
    // Must explicitly enable remove_metadata to test session ID sanitization
    let preprocessor = Preprocessor::new(PreprocessingConfig {
        remove_metadata: true,
        ..Default::default()
    });

    let input = r#"Request with session_id: abc123xyz. Also sessionId="def456"."#;
    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains("session_id: abc123xyz"));
    assert!(!result.contains("sessionId=\"def456\""));
    assert!(result.contains("Request with"));
}

#[test]
fn test_remove_empty_content_json() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"Response: {"content": [], "status": "ok"}. Done."#;
    let result = preprocessor.extract_semantic_content(input);

    assert!(!result.contains(r#""content": []"#));
    assert!(result.contains("Response"));
    assert!(result.contains("Done"));
}

#[test]
fn test_preserve_semantic_content() {
    let preprocessor = Preprocessor::with_defaults();

    let input = r#"The preprocessing module filters noise from conversation exports.
    
It removes tool artifacts, CLI output, and metadata while preserving
the actual semantic content that should be embedded for search.

Key features:
- Pattern-based filtering using regex
- Deduplication with configurable threshold
- Minimum content length requirement"#;

    let result = preprocessor.extract_semantic_content(input);

    // All meaningful content should be preserved
    assert!(result.contains("preprocessing module"));
    assert!(result.contains("filters noise"));
    assert!(result.contains("conversation exports"));
    assert!(result.contains("Key features"));
    assert!(result.contains("Pattern-based filtering"));
    assert!(result.contains("Deduplication"));
}

#[test]
fn test_filter_message_below_min_length() {
    let mut preprocessor = Preprocessor::with_defaults();

    // Very short content should be filtered
    let result = preprocessor.filter_message("Hi");
    assert!(result.is_none());

    // Content at or above min length should pass
    let long_content = "This is a meaningful message with enough content to be indexed properly.";
    let result = preprocessor.filter_message(long_content);
    assert!(result.is_some());
}

#[test]
fn test_filter_message_duplicates() {
    let mut preprocessor = Preprocessor::new(PreprocessingConfig {
        min_content_length: 10,
        dedupe_threshold: 0.95,
        ..Default::default()
    });

    let content = "This is a test message that we will see twice.";

    // First occurrence should pass
    let result1 = preprocessor.filter_message(content);
    assert!(result1.is_some());

    // Exact duplicate should be filtered
    let result2 = preprocessor.filter_message(content);
    assert!(result2.is_none());
}

#[test]
fn test_filter_conversation() {
    let mut preprocessor = Preprocessor::new(PreprocessingConfig {
        min_content_length: 20,
        ..Default::default()
    });

    let messages = vec![
        Message::new("user", "How do I implement the preprocessing module?"),
        Message::new("assistant", "On branch main\nmodified: src/lib.rs"), // CLI output
        Message::new("assistant", "Let me explain the preprocessing approach..."),
        Message::new("user", ""), // Empty
        Message::new(
            "assistant",
            "Here's the implementation with detailed explanation of the patterns.",
        ),
    ];

    let (filtered, stats) = preprocessor.filter_conversation(messages);

    assert_eq!(stats.total_input, 5);
    assert!(stats.total_output < 5);
    assert!(!filtered.iter().any(|m| m.content.contains("On branch")));
    assert!(!filtered.iter().any(|m| m.content.is_empty()));
}

#[test]
fn test_is_mostly_tool_artifact() {
    let preprocessor = Preprocessor::with_defaults();

    // Build tool artifact content
    let _mostly_artifact = format!(
        "<{}>Some tool call with lots of data and parameters</{}>",
        "function_calls", "function_calls"
    );

    // This should be detected as mostly artifact if over 80%
    // Since we're testing the ratio, let's use a message that's clearly mostly tool output
    let mixed_content = "Brief intro. Here's the actual content that matters.";

    assert!(!preprocessor.is_mostly_tool_artifact(mixed_content));
}

#[test]
fn test_is_mostly_cli_output() {
    let preprocessor = Preprocessor::with_defaults();

    let cli_heavy = r#"On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
  modified:   src/lib.rs
  modified:   Cargo.toml
nothing to commit"#;

    let mixed = r#"Looking at the code:
On branch main
That's the current state."#;

    assert!(preprocessor.is_mostly_cli_output(cli_heavy));
    assert!(!preprocessor.is_mostly_cli_output(mixed));
}

#[test]
fn test_content_similarity() {
    // Identical content
    assert_eq!(content_similarity("hello world", "hello world"), 1.0);

    // Completely different
    assert_eq!(content_similarity("hello world", "foo bar baz"), 0.0);

    // Partial overlap
    let sim = content_similarity("hello world test", "hello world foo");
    assert!(sim > 0.0 && sim < 1.0);
}

#[test]
fn test_preprocessing_stats() {
    let stats = PreprocessingStats {
        total_input: 100,
        filtered_tool_artifacts: 10,
        filtered_cli_output: 15,
        filtered_metadata: 5,
        filtered_empty: 8,
        filtered_duplicates: 2,
        filtered_below_min_length: 5,
        total_output: 55,
    };

    assert_eq!(stats.total_filtered(), 45);
    assert!((stats.filter_rate() - 0.45).abs() < 0.01);
}

#[test]
fn test_whitespace_normalization() {
    let preprocessor = Preprocessor::with_defaults();

    let input = "Text with    multiple    spaces\n\n\n\nand many newlines.";
    let result = preprocessor.extract_semantic_content(input);

    // Multiple spaces should be reduced to single
    assert!(!result.contains("    "));
    // Multiple newlines should be reduced to double
    assert!(!result.contains("\n\n\n"));
}

#[test]
fn test_config_serialization() {
    let config = PreprocessingConfig::default();

    // Should serialize to TOML
    let toml_str = toml::to_string(&config).unwrap();
    assert!(toml_str.contains("remove_tool_artifacts = true"));
    // Verify remove_metadata defaults to false
    assert!(toml_str.contains("remove_metadata = false"));

    // Should deserialize back
    let parsed: PreprocessingConfig = toml::from_str(&toml_str).unwrap();
    assert_eq!(parsed.min_content_length, config.min_content_length);
    assert!(
        !parsed.remove_metadata,
        "remove_metadata should default to false"
    );
}

#[test]
fn test_reset_dedupe_cache() {
    let mut preprocessor = Preprocessor::new(PreprocessingConfig {
        min_content_length: 10,
        dedupe_threshold: 0.95,
        ..Default::default()
    });

    let content = "This is a test message that we will see again.";

    // First occurrence
    assert!(preprocessor.filter_message(content).is_some());

    // Duplicate - should be filtered
    assert!(preprocessor.filter_message(content).is_none());

    // Reset cache
    preprocessor.reset_dedupe_cache();

    // Now it should pass again
    assert!(preprocessor.filter_message(content).is_some());
}