agentroot-core 0.1.1

Core library for agentroot - semantic search engine with AST-aware chunking and hybrid 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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Integration test for LLM-generated metadata system
//!
//! This test creates a real collection with markdown and code files,
//! generates metadata using the LLM, and verifies search quality.

use agentroot_core::{Database, LlamaMetadataGenerator, MetadataGenerator, SearchOptions};
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Creates test files with realistic content
fn create_test_files(dir: &TempDir) -> Vec<PathBuf> {
    let mut files = Vec::new();

    // 1. Rust tutorial document
    let rust_guide = dir.path().join("rust-getting-started.md");
    fs::write(
        &rust_guide,
        r#"# Getting Started with Rust

Rust is a systems programming language that provides memory safety without garbage collection.
It achieves this through a unique ownership system that the compiler checks at compile time.

## Installation

Install Rust using rustup:

```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

## Your First Program

Create a new project:

```bash
cargo new hello_world
cd hello_world
cargo run
```

This creates a simple "Hello, world!" program that you can build and run.

## Key Concepts

- **Ownership**: Each value has a single owner
- **Borrowing**: References allow you to access data without taking ownership
- **Lifetimes**: Ensure references are always valid

Rust is perfect for systems programming, web servers, and command-line tools.
"#,
    )
    .unwrap();
    files.push(rust_guide);

    // 2. Advanced Rust document
    let async_rust = dir.path().join("async-programming.md");
    fs::write(
        &async_rust,
        r#"# Async Programming in Rust

Asynchronous programming allows you to write concurrent code using async/await syntax.
Rust's async model is zero-cost and provides excellent performance.

## Tokio Runtime

The most popular async runtime is Tokio:

```rust
#[tokio::main]
async fn main() {
    let result = fetch_data().await;
    println!("Result: {:?}", result);
}
```

## Key Concepts

- **Futures**: Lazy computations that produce a value
- **Async/Await**: Syntax for writing asynchronous code
- **Runtime**: Executes futures and manages task scheduling

This is considered an intermediate to advanced topic in Rust.
"#,
    )
    .unwrap();
    files.push(async_rust);

    // 3. Python tutorial
    let python_guide = dir.path().join("python-basics.md");
    fs::write(
        &python_guide,
        r#"# Python Basics for Beginners

Python is a high-level, interpreted programming language known for its simplicity.
It's perfect for beginners and widely used in data science, web development, and automation.

## Hello World

```python
print("Hello, World!")
```

## Variables and Types

```python
name = "Alice"
age = 30
is_student = False
```

Python uses dynamic typing, so you don't need to declare variable types.

## Lists and Loops

```python
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)
```

Python is beginner-friendly and great for learning programming concepts.
"#,
    )
    .unwrap();
    files.push(python_guide);

    // 4. Configuration file example
    let config_doc = dir.path().join("config-reference.md");
    fs::write(
        &config_doc,
        r#"# Configuration Reference

This document describes all configuration options for the application.

## Database Settings

- `database.url`: Connection string for the database
- `database.pool_size`: Maximum number of connections (default: 10)
- `database.timeout`: Connection timeout in seconds (default: 30)

## Server Settings

- `server.host`: Host address to bind to (default: 127.0.0.1)
- `server.port`: Port number to listen on (default: 8080)
- `server.workers`: Number of worker threads (default: 4)

## Example Configuration

```toml
[database]
url = "postgres://localhost/mydb"
pool_size = 20

[server]
host = "0.0.0.0"
port = 3000
```
"#,
    )
    .unwrap();
    files.push(config_doc);

    // 5. Rust code file
    let rust_code = dir.path().join("example.rs");
    fs::write(
        &rust_code,
        r#"//! Example Rust module demonstrating ownership and borrowing

/// Calculates the length of a string without taking ownership
pub fn calculate_length(s: &String) -> usize {
    s.len()
}

/// Takes ownership and returns a modified string
pub fn add_suffix(mut s: String, suffix: &str) -> String {
    s.push_str(suffix);
    s
}

/// A simple struct demonstrating Rust's type system
#[derive(Debug, Clone)]
pub struct User {
    pub name: String,
    pub age: u32,
    pub email: String,
}

impl User {
    /// Creates a new user
    pub fn new(name: String, age: u32, email: String) -> Self {
        Self { name, age, email }
    }

    /// Checks if the user is an adult
    pub fn is_adult(&self) -> bool {
        self.age >= 18
    }
}

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

    #[test]
    fn test_calculate_length() {
        let s = String::from("hello");
        assert_eq!(calculate_length(&s), 5);
    }

    #[test]
    fn test_is_adult() {
        let user = User::new("Alice".to_string(), 25, "alice@example.com".to_string());
        assert!(user.is_adult());
    }
}
"#,
    )
    .unwrap();
    files.push(rust_code);

    files
}

#[tokio::test]
async fn test_end_to_end_metadata_generation() {
    // Create temporary directory with test files
    let temp_dir = TempDir::new().unwrap();
    let files = create_test_files(&temp_dir);

    assert_eq!(files.len(), 5, "Should create 5 test files");

    // Create temporary database
    let db_dir = TempDir::new().unwrap();
    let db_path = db_dir.path().join("test.sqlite");
    let db = Database::open(&db_path).unwrap();
    db.initialize().unwrap();

    // Add collection (use * pattern to match all files in root)
    let collection_name = "test-docs";
    db.add_collection(
        collection_name,
        temp_dir.path().to_str().unwrap(),
        "*",
        "file",
        Some(r#"{"exclude_hidden":"false"}"#),
    )
    .unwrap();

    // Create metadata generator (will use fallback if LLM not available)
    let generator_result = LlamaMetadataGenerator::from_default();
    let generator = generator_result.ok();

    // Index with metadata
    let indexed_count = if let Some(ref gen) = generator {
        db.reindex_collection_with_metadata(collection_name, Some(gen as &dyn MetadataGenerator))
            .await
            .unwrap()
    } else {
        db.reindex_collection_with_metadata(collection_name, None)
            .await
            .unwrap()
    };

    assert_eq!(indexed_count, 5, "Should index all 5 files");

    // Verify documents are in database
    let collections = db.list_collections().unwrap();
    assert_eq!(collections.len(), 1);
    assert_eq!(collections[0].name, collection_name);
    assert_eq!(collections[0].document_count, 5);

    // Use search to get documents with metadata
    let search_opts = SearchOptions {
        limit: 10,
        min_score: 0.0,
        collection: Some(collection_name.to_string()),
        provider: None,
        full_content: false,
    };

    // Search for common words to get all documents
    let all_results = db
        .search_fts("a OR the OR is OR Rust", &search_opts)
        .unwrap();
    assert!(
        all_results.len() >= 4,
        "Should have at least 4 documents with common words"
    );

    let mut metadata_count = 0;
    for result in &all_results {
        if result.llm_summary.is_some() {
            metadata_count += 1;

            // Verify metadata quality
            let summary = result.llm_summary.as_ref().unwrap();
            assert!(
                !summary.is_empty(),
                "Summary should not be empty for {}",
                result.filepath
            );

            if let Some(title) = &result.llm_title {
                assert!(!title.is_empty(), "Title should not be empty");
            }

            if let Some(category) = &result.llm_category {
                assert!(!category.is_empty(), "Category should not be empty");
            }

            if let Some(keywords) = &result.llm_keywords {
                assert!(
                    !keywords.is_empty(),
                    "Keywords should not be empty for {}",
                    result.filepath
                );
            }
        }
    }

    println!(
        "Generated metadata for {} out of {} documents",
        metadata_count,
        all_results.len()
    );

    if generator.is_some() {
        // If LLM model is available, metadata should be generated
        println!("LLM model available - verifying metadata was generated");
        assert!(
            metadata_count >= 4,
            "Should generate metadata for most documents when LLM is available"
        );
    } else {
        // Without LLM model, metadata won't be generated, but indexing should still work
        println!("LLM model not available - metadata generation skipped (expected)");
    }
}

#[tokio::test]
async fn test_metadata_improves_search_quality() {
    // Create temporary directory with test files
    let temp_dir = TempDir::new().unwrap();
    let _files = create_test_files(&temp_dir);

    // Create temporary database
    let db_dir = TempDir::new().unwrap();
    let db_path = db_dir.path().join("test.sqlite");
    let db = Database::open(&db_path).unwrap();
    db.initialize().unwrap();

    // Add collection and index with metadata
    let collection_name = "search-test";
    db.add_collection(
        collection_name,
        temp_dir.path().to_str().unwrap(),
        "*",
        "file",
        Some(r#"{"exclude_hidden":"false"}"#),
    )
    .unwrap();

    let generator_result = LlamaMetadataGenerator::from_default();
    let generator = generator_result.ok();

    if let Some(ref gen) = generator {
        db.reindex_collection_with_metadata(collection_name, Some(gen as &dyn MetadataGenerator))
            .await
            .unwrap();
    } else {
        db.reindex_collection_with_metadata(collection_name, None)
            .await
            .unwrap();
    }

    // Test BM25 search with metadata
    let search_opts = SearchOptions {
        limit: 10,
        min_score: 0.0,
        collection: Some(collection_name.to_string()),
        provider: None,
        full_content: false,
    };

    // Search for "Rust programming beginners"
    let mut results = db.search_fts("Rust programming", &search_opts).unwrap();
    if results.is_empty() {
        // If no results, try a simpler query
        results = db.search_fts("Rust", &search_opts).unwrap();
    }
    assert!(!results.is_empty(), "Should find results for Rust query");

    // The beginner Rust guide should rank highly
    let has_rust_guide = results.iter().any(|r| {
        r.filepath.contains("rust-getting-started")
            || r.title.to_lowercase().contains("getting started")
    });
    assert!(has_rust_guide, "Should find the Rust getting started guide");

    // Search for "asynchronous" or "async"
    let mut results = db.search_fts("asynchronous", &search_opts).unwrap();
    if results.is_empty() {
        results = db.search_fts("async", &search_opts).unwrap();
    }
    assert!(!results.is_empty(), "Should find results for async query");

    let has_async_doc = results.iter().any(|r| {
        r.filepath.contains("async-programming") || r.title.to_lowercase().contains("async")
    });
    assert!(has_async_doc, "Should find the async programming document");

    // Search for "Python"
    let results = db.search_fts("Python", &search_opts).unwrap();
    assert!(!results.is_empty(), "Should find results for Python query");

    let has_python_guide = results
        .iter()
        .any(|r| r.filepath.contains("python-basics") || r.title.to_lowercase().contains("python"));
    assert!(has_python_guide, "Should find the Python basics guide");

    // Search for "configuration"
    let results = db.search_fts("configuration", &search_opts).unwrap();
    assert!(!results.is_empty(), "Should find results for config query");

    let has_config_doc = results.iter().any(|r| {
        r.filepath.contains("config-reference") || r.title.to_lowercase().contains("config")
    });
    assert!(has_config_doc, "Should find the configuration reference");

    println!("Search quality test passed - metadata enhances discoverability");
}

#[tokio::test]
async fn test_metadata_fields_in_search_results() {
    // Create temporary directory with test files
    let temp_dir = TempDir::new().unwrap();
    let _files = create_test_files(&temp_dir);

    // Create temporary database
    let db_dir = TempDir::new().unwrap();
    let db_path = db_dir.path().join("test.sqlite");
    let db = Database::open(&db_path).unwrap();
    db.initialize().unwrap();

    // Add collection and index
    let collection_name = "metadata-fields-test";
    db.add_collection(
        collection_name,
        temp_dir.path().to_str().unwrap(),
        "*",
        "file",
        Some(r#"{"exclude_hidden":"false"}"#),
    )
    .unwrap();

    let generator_result = LlamaMetadataGenerator::from_default();
    let generator = generator_result.ok();

    if let Some(ref gen) = generator {
        db.reindex_collection_with_metadata(collection_name, Some(gen as &dyn MetadataGenerator))
            .await
            .unwrap();
    } else {
        db.reindex_collection_with_metadata(collection_name, None)
            .await
            .unwrap();
    }

    // Search and verify metadata in results
    let search_opts = SearchOptions {
        limit: 10,
        min_score: 0.0,
        collection: Some(collection_name.to_string()),
        provider: None,
        full_content: false,
    };

    let results = db.search_fts("Rust", &search_opts).unwrap();
    assert!(!results.is_empty(), "Should find Rust-related documents");

    // Check that at least one result has metadata (if LLM was available)
    let has_metadata = results.iter().any(|r| {
        r.llm_summary.is_some()
            || r.llm_title.is_some()
            || r.llm_keywords.is_some()
            || r.llm_category.is_some()
            || r.llm_difficulty.is_some()
    });

    if generator.is_some() {
        assert!(
            has_metadata,
            "Search results should include metadata fields when LLM is available"
        );
    } else {
        println!("LLM model not available - skipping metadata field checks");
    }

    // Verify metadata quality for results with metadata
    for result in &results {
        if let Some(summary) = &result.llm_summary {
            assert!(summary.len() >= 20, "Summary should be substantial");
        }

        if let Some(keywords) = &result.llm_keywords {
            assert!(!keywords.is_empty(), "Keywords should not be empty");
        }

        if let Some(difficulty) = &result.llm_difficulty {
            assert!(
                ["beginner", "intermediate", "advanced"].contains(&difficulty.as_str()),
                "Difficulty should be a valid level"
            );
        }
    }

    println!("Metadata fields successfully included in search results");
}

#[tokio::test]
async fn test_metadata_cache_functionality() {
    // Create temporary directory with one test file
    let temp_dir = TempDir::new().unwrap();
    let test_file = temp_dir.path().join("test.md");
    fs::write(
        &test_file,
        "# Test Document\n\nThis is a test document for cache verification.",
    )
    .unwrap();

    // Create temporary database
    let db_dir = TempDir::new().unwrap();
    let db_path = db_dir.path().join("test.sqlite");
    let db = Database::open(&db_path).unwrap();
    db.initialize().unwrap();

    // Add collection
    let collection_name = "cache-test";
    db.add_collection(
        collection_name,
        temp_dir.path().to_str().unwrap(),
        "**/*.md",
        "file",
        Some(r#"{"exclude_hidden":"false"}"#),
    )
    .unwrap();

    // Index first time
    let generator_result = LlamaMetadataGenerator::from_default();
    let generator = generator_result.ok();

    if let Some(ref gen) = generator {
        db.reindex_collection_with_metadata(collection_name, Some(gen as &dyn MetadataGenerator))
            .await
            .unwrap();
    } else {
        db.reindex_collection_with_metadata(collection_name, None)
            .await
            .unwrap();
    }

    // Get search results to check metadata
    let search_opts = SearchOptions {
        limit: 10,
        min_score: 0.0,
        collection: Some(collection_name.to_string()),
        provider: None,
        full_content: false,
    };

    let docs = db.search_fts("test OR document", &search_opts).unwrap();
    assert_eq!(docs.len(), 1);
    let first_summary = docs[0].llm_summary.clone();

    // Re-index (should use cache)
    if let Some(ref gen) = generator {
        db.reindex_collection_with_metadata(collection_name, Some(gen as &dyn MetadataGenerator))
            .await
            .unwrap();
    } else {
        db.reindex_collection_with_metadata(collection_name, None)
            .await
            .unwrap();
    }

    // Verify document still has the same metadata
    let docs_after = db.search_fts("test OR document", &search_opts).unwrap();
    assert_eq!(docs_after.len(), 1);
    assert_eq!(
        docs_after[0].llm_summary, first_summary,
        "Metadata should be preserved through cache"
    );

    println!("Metadata caching works correctly");
}