maproom 0.1.0

Semantic code search powered by embeddings and SQLite
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
//! Context assembler implementation for building intelligent code context bundles.

use anyhow::{Context as AnyhowContext, Result};
use std::sync::Arc;
use tracing::{debug, warn};

use super::budget::SharedBudgetManager;
use super::cache::{CacheConfig, ContextCache};
use super::file_loader::FileLoader;
use super::graph::{load_relationships_parallel, RelatedChunk};
use super::token_counter::TokenCounter;
use super::types::{ContextBundle, ContextItem, ExpandOptions, LineRange};
use crate::db::SqliteStore;
use crate::profile_scope;

/// Chunk metadata retrieved from the database.
#[derive(Debug, Clone)]
pub struct ChunkMetadata {
    pub id: i64,
    pub file_relpath: String,
    pub worktree_path: String,
    pub symbol_name: Option<String>,
    pub kind: String,
    pub start_line: i32,
    pub end_line: i32,
    pub signature: Option<String>,
    pub docstring: Option<String>,
}

/// Trait for assembling context bundles from chunk IDs.
///
/// Implementations are responsible for:
/// - Retrieving chunk metadata from the database
/// - Loading file content from the filesystem
/// - Counting tokens accurately
/// - Assembling a ContextBundle within the specified budget
#[async_trait::async_trait]
pub trait ContextAssembler: Send + Sync {
    /// Assemble a context bundle for the specified chunk.
    ///
    /// # Arguments
    ///
    /// * `chunk_id` - The ID of the primary chunk to assemble context for
    /// * `budget` - Maximum number of tokens allowed in the bundle
    /// * `options` - Options for expanding context beyond the primary chunk
    ///
    /// # Returns
    ///
    /// A ContextBundle containing the primary chunk and any related context
    /// that fits within the token budget.
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Chunk ID is not found in the database
    /// - Database query fails
    /// - File cannot be read
    /// - Token counting fails
    async fn assemble(
        &self,
        chunk_id: i64,
        budget: usize,
        options: ExpandOptions,
    ) -> Result<ContextBundle>;
}

/// Basic context assembler that retrieves and formats a single chunk.
///
/// This is the foundational implementation that:
/// - Queries the database for chunk metadata
/// - Loads file content from the worktree
/// - Extracts the specified line range
/// - Counts tokens accurately
/// - Returns a simple ContextBundle with just the primary chunk
/// - Caches assembled bundles for improved performance
///
/// Future implementations will add:
/// - Relationship traversal (callers, callees, tests)
/// - Budget allocation across multiple items
/// - Truncation strategies for large chunks
/// - Priority-based context selection
///
/// # Example
///
/// ```ignore
/// use maproom::context::{BasicContextAssembler, ContextAssembler, ExpandOptions};
/// use maproom::context::cache::CacheConfig;
/// use maproom::db::create_pool;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///     let pool = create_pool().await?;
///     let cache_config = CacheConfig::default();
///     let assembler = BasicContextAssembler::new(pool, cache_config);
///
///     let bundle = assembler.assemble(
///         12345,
///         6000,
///         ExpandOptions::primary_only()
///     ).await?;
///
///     println!("Assembled {} items, {} tokens", bundle.items.len(), bundle.total_tokens);
///     Ok(())
/// }
/// ```
pub struct BasicContextAssembler {
    #[allow(dead_code)] // Will be used when get_chunk_metadata is implemented (IDXABS-4001)
    store: Arc<SqliteStore>,
    token_counter: TokenCounter,
    cache: Arc<ContextCache>,
}

impl BasicContextAssembler {
    /// Create a new basic context assembler with the specified cache configuration.
    pub fn new(store: Arc<SqliteStore>, cache_config: CacheConfig) -> Self {
        let cache = Arc::new(ContextCache::new(store.clone(), cache_config));
        Self {
            store,
            token_counter: TokenCounter::new(),
            cache,
        }
    }

    /// Create a new basic context assembler with caching disabled.
    pub fn new_without_cache(store: Arc<SqliteStore>) -> Self {
        let cache_config = CacheConfig {
            enabled: false,
            ..Default::default()
        };
        Self::new(store, cache_config)
    }

    /// Get a reference to the cache for statistics and management.
    pub fn cache(&self) -> &Arc<ContextCache> {
        &self.cache
    }

    /// Retrieve chunk metadata from the database by ID.
    pub async fn get_chunk_metadata(&self, _chunk_id: i64) -> Result<ChunkMetadata> {
        profile_scope!("get_chunk_metadata");
        // TODO: Implement using SqliteStore methods in IDXABS-4001
        anyhow::bail!("get_chunk_metadata not yet implemented for SQLite")
    }

    /// Create a ContextItem from chunk metadata.
    async fn create_context_item(
        &self,
        metadata: ChunkMetadata,
        role: &str,
        reason: &str,
    ) -> Result<ContextItem> {
        profile_scope!("create_context_item");
        // Load file content
        let file_loader = FileLoader::new(&metadata.worktree_path);
        let range = LineRange::new(metadata.start_line, metadata.end_line);

        let content = file_loader
            .load_range(&metadata.file_relpath, range)
            .await
            .with_context(|| {
                format!(
                    "Failed to load file content: {} (lines {}-{})",
                    metadata.file_relpath, metadata.start_line, metadata.end_line
                )
            })?;

        // Count tokens
        let tokens = self
            .token_counter
            .count(&content)
            .context("Failed to count tokens")?;

        debug!(
            "Created context item: {} lines {}-{}, {} tokens",
            metadata.file_relpath, metadata.start_line, metadata.end_line, tokens
        );

        Ok(ContextItem {
            relpath: metadata.file_relpath,
            range,
            role: role.to_string(),
            reason: reason.to_string(),
            content,
            tokens,
        })
    }
}

#[async_trait::async_trait]
impl ContextAssembler for BasicContextAssembler {
    async fn assemble(
        &self,
        chunk_id: i64,
        budget: usize,
        options: ExpandOptions,
    ) -> Result<ContextBundle> {
        profile_scope!("context_assemble");
        debug!(
            "Assembling context for chunk {} with budget {} tokens",
            chunk_id, budget
        );

        // Try to get from cache first
        if let Some(cached_bundle) = self.cache.get(chunk_id, &options).await? {
            debug!("Returning cached bundle for chunk {}", chunk_id);
            return Ok(cached_bundle);
        }

        // Cache miss - assemble the bundle
        debug!("Cache miss for chunk {}, assembling...", chunk_id);

        // Retrieve chunk metadata
        let metadata = self
            .get_chunk_metadata(chunk_id)
            .await
            .context("Failed to retrieve chunk metadata")?;

        // Create context item for the primary chunk
        let reason = if let Some(ref name) = metadata.symbol_name {
            format!("Primary chunk: {} ({})", name, metadata.kind)
        } else {
            format!("Primary chunk ({})", metadata.kind)
        };

        let item = self
            .create_context_item(metadata, "primary", &reason)
            .await
            .context("Failed to create context item")?;

        // Check if it fits within budget
        let mut bundle = ContextBundle::new();
        if item.tokens > budget {
            warn!(
                "Primary chunk ({} tokens) exceeds budget ({} tokens), truncating",
                item.tokens, budget
            );
            bundle.truncated = true;
            // TODO: Implement intelligent truncation in future ticket
            // For now, include it anyway and mark as truncated
        }

        bundle.add_item(item);

        debug!(
            "Assembled context bundle: {} items, {} tokens, truncated: {}",
            bundle.items.len(),
            bundle.total_tokens,
            bundle.truncated
        );

        // Store in cache for future use
        if let Err(e) = self.cache.put(chunk_id, &options, &bundle).await {
            // Log cache error but don't fail the request
            warn!("Failed to cache bundle for chunk {}: {}", chunk_id, e);
        }

        Ok(bundle)
    }
}

/// Parallel context assembler that uses tokio::join! for concurrent operations.
///
/// This assembler extends the basic assembler with parallel loading capabilities:
/// - Concurrent chunk metadata and file content loading
/// - Parallel relationship queries (callers, callees, tests)
/// - Thread-safe budget management for parallel operations
/// - Graceful error handling with partial results
///
/// Performance improvements over BasicContextAssembler:
/// - ~60-70% latency reduction for complex contexts with relationships
/// - Maintains correctness through thread-safe budget tracking
/// - Falls back gracefully if relationship queries fail
///
/// # Example
///
/// ```ignore
/// use maproom::context::{ParallelContextAssembler, ContextAssembler, ExpandOptions};
/// use maproom::context::cache::CacheConfig;
/// use maproom::db::create_pool;
///
/// #[tokio::main]
/// async fn main() -> anyhow::Result<()> {
///     let pool = create_pool().await?;
///     let cache_config = CacheConfig::default();
///     let assembler = ParallelContextAssembler::new(pool, cache_config);
///
///     let options = ExpandOptions {
///         include_tests: true,
///         include_callers: true,
///         include_callees: true,
///         max_depth: 2,
///         ..Default::default()
///     };
///
///     let bundle = assembler.assemble(12345, 6000, options).await?;
///     println!("Assembled {} items in parallel", bundle.items.len());
///     Ok(())
/// }
/// ```
pub struct ParallelContextAssembler {
    store: Arc<SqliteStore>,
    token_counter: TokenCounter,
    cache: Arc<ContextCache>,
}

impl ParallelContextAssembler {
    /// Create a new parallel context assembler with the specified cache configuration.
    pub fn new(store: Arc<SqliteStore>, cache_config: CacheConfig) -> Self {
        let cache = Arc::new(ContextCache::new(store.clone(), cache_config));
        Self {
            store,
            token_counter: TokenCounter::new(),
            cache,
        }
    }

    /// Create a new parallel context assembler with caching disabled.
    pub fn new_without_cache(store: Arc<SqliteStore>) -> Self {
        let cache_config = CacheConfig {
            enabled: false,
            ..Default::default()
        };
        Self::new(store, cache_config)
    }

    /// Get a reference to the cache for statistics and management.
    pub fn cache(&self) -> &Arc<ContextCache> {
        &self.cache
    }

    /// Retrieve chunk metadata from the database by ID (same as BasicContextAssembler).
    async fn get_chunk_metadata(&self, _chunk_id: i64) -> Result<ChunkMetadata> {
        // TODO: Implement using SqliteStore methods in IDXABS-4001
        anyhow::bail!("get_chunk_metadata not yet implemented for SQLite")
    }

    /// Create a ContextItem from chunk metadata.
    async fn create_context_item(
        &self,
        metadata: ChunkMetadata,
        role: &str,
        reason: &str,
    ) -> Result<ContextItem> {
        profile_scope!("create_context_item");
        // Load file content
        let file_loader = FileLoader::new(&metadata.worktree_path);
        let range = LineRange::new(metadata.start_line, metadata.end_line);

        let content = file_loader
            .load_range(&metadata.file_relpath, range)
            .await
            .with_context(|| {
                format!(
                    "Failed to load file content: {} (lines {}-{})",
                    metadata.file_relpath, metadata.start_line, metadata.end_line
                )
            })?;

        // Count tokens
        let tokens = self
            .token_counter
            .count(&content)
            .context("Failed to count tokens")?;

        debug!(
            "Created context item: {} lines {}-{}, {} tokens",
            metadata.file_relpath, metadata.start_line, metadata.end_line, tokens
        );

        Ok(ContextItem {
            relpath: metadata.file_relpath,
            range,
            role: role.to_string(),
            reason: reason.to_string(),
            content,
            tokens,
        })
    }

    /// Convert RelatedChunk to ContextItem with budget checking.
    async fn related_chunk_to_item(
        &self,
        chunk: RelatedChunk,
        role: &str,
        budget: &SharedBudgetManager,
    ) -> Option<ContextItem> {
        // Early budget check
        if budget.remaining() == 0 {
            debug!("Budget exhausted, skipping chunk {}", chunk.id);
            return None;
        }

        // Convert to ChunkMetadata
        let metadata = ChunkMetadata {
            id: chunk.id,
            file_relpath: chunk.relpath.clone(),
            worktree_path: String::new(), // Will be resolved in create_context_item
            symbol_name: chunk.symbol_name.clone(),
            kind: chunk.kind.clone(),
            start_line: chunk.start_line,
            end_line: chunk.end_line,
            signature: None,
            docstring: None,
        };

        let reason = if let Some(ref name) = chunk.symbol_name {
            format!(
                "{}: {} (depth {}, relevance {:.2})",
                role, name, chunk.depth, chunk.relevance
            )
        } else {
            format!(
                "{} (depth {}, relevance {:.2})",
                role, chunk.depth, chunk.relevance
            )
        };

        // Try to create the item
        match self.create_context_item(metadata, role, &reason).await {
            Ok(item) => {
                // Try to reserve budget
                if budget.try_reserve(role, item.tokens) {
                    Some(item)
                } else {
                    debug!("Insufficient budget for {} chunk {}", role, chunk.id);
                    None
                }
            }
            Err(e) => {
                warn!(
                    "Failed to create context item for chunk {}: {}",
                    chunk.id, e
                );
                None
            }
        }
    }

    /// Load related chunks and convert to context items in parallel.
    async fn load_related_items(
        &self,
        chunks: Vec<RelatedChunk>,
        role: &str,
        budget: SharedBudgetManager,
        max_items: usize,
    ) -> Vec<ContextItem> {
        // Limit parallelism to avoid overwhelming the system
        let chunks_to_load: Vec<_> = chunks.into_iter().take(max_items).collect();

        // Load all chunks in parallel
        let mut handles = vec![];
        for chunk in chunks_to_load {
            let budget_clone = budget.clone();
            let role_str = role.to_string();
            let self_clone = self.clone_for_parallel();

            let handle = tokio::spawn(async move {
                self_clone
                    .related_chunk_to_item(chunk, &role_str, &budget_clone)
                    .await
            });

            handles.push(handle);
        }

        // Collect results
        let mut items = Vec::new();
        for handle in handles {
            if let Ok(Some(item)) = handle.await {
                items.push(item);
            }
        }

        items
    }

    /// Helper to clone necessary components for parallel tasks.
    fn clone_for_parallel(&self) -> Self {
        Self {
            store: self.store.clone(),
            token_counter: self.token_counter.clone(),
            cache: self.cache.clone(),
        }
    }
}

impl Clone for ParallelContextAssembler {
    fn clone(&self) -> Self {
        Self {
            store: self.store.clone(),
            token_counter: self.token_counter.clone(),
            cache: self.cache.clone(),
        }
    }
}

#[async_trait::async_trait]
impl ContextAssembler for ParallelContextAssembler {
    async fn assemble(
        &self,
        chunk_id: i64,
        budget: usize,
        options: ExpandOptions,
    ) -> Result<ContextBundle> {
        debug!(
            "Assembling context for chunk {} with budget {} tokens (parallel mode)",
            chunk_id, budget
        );

        // Try to get from cache first
        if let Some(cached_bundle) = self.cache.get(chunk_id, &options).await? {
            debug!("Returning cached bundle for chunk {}", chunk_id);
            return Ok(cached_bundle);
        }

        debug!(
            "Cache miss for chunk {}, assembling in parallel...",
            chunk_id
        );

        // Create shared budget manager
        let budget_mgr = SharedBudgetManager::new(budget);
        let allocation = budget_mgr.allocate().unwrap();

        // Phase 1: Load primary chunk metadata and relationships in parallel
        let (metadata_result, relationships) =
            tokio::join!(self.get_chunk_metadata(chunk_id), async {
                if options.callers || options.callees || options.tests {
                    load_relationships_parallel(&self.store, chunk_id, options.max_depth).await
                } else {
                    (Vec::new(), Vec::new(), Vec::new())
                }
            });

        let metadata = metadata_result.context("Failed to retrieve chunk metadata")?;
        let (callers, callees, tests) = relationships;

        // Phase 2: Load primary chunk
        let reason = if let Some(ref name) = metadata.symbol_name {
            format!("Primary chunk: {} ({})", name, metadata.kind)
        } else {
            format!("Primary chunk ({})", metadata.kind)
        };

        let primary_item = self
            .create_context_item(metadata, "primary", &reason)
            .await
            .context("Failed to create primary context item")?;

        // Reserve budget for primary
        let mut bundle = ContextBundle::new();
        if !budget_mgr.try_reserve("primary", primary_item.tokens) {
            warn!(
                "Primary chunk ({} tokens) exceeds budget ({} tokens)",
                primary_item.tokens, budget
            );
            bundle.truncated = true;
        }
        bundle.add_item(primary_item);

        // Phase 3: Load related items in parallel (if budget allows)
        if budget_mgr.remaining() > 0 {
            let (test_items, caller_items, callee_items) = tokio::join!(
                async {
                    if options.tests && !tests.is_empty() {
                        self.load_related_items(
                            tests,
                            "test",
                            budget_mgr.clone(),
                            allocation.tests / 400, // Estimate ~400 tokens per item
                        )
                        .await
                    } else {
                        Vec::new()
                    }
                },
                async {
                    if options.callers && !callers.is_empty() {
                        self.load_related_items(
                            callers,
                            "caller",
                            budget_mgr.clone(),
                            allocation.callers / 400,
                        )
                        .await
                    } else {
                        Vec::new()
                    }
                },
                async {
                    if options.callees && !callees.is_empty() {
                        self.load_related_items(
                            callees,
                            "callee",
                            budget_mgr.clone(),
                            allocation.callees / 400,
                        )
                        .await
                    } else {
                        Vec::new()
                    }
                }
            );

            // Add all items to bundle
            for item in test_items {
                bundle.add_item(item);
            }
            for item in caller_items {
                bundle.add_item(item);
            }
            for item in callee_items {
                bundle.add_item(item);
            }
        }

        debug!(
            "Assembled context bundle (parallel): {} items, {} tokens, truncated: {}",
            bundle.items.len(),
            bundle.total_tokens,
            bundle.truncated
        );

        // Store in cache for future use
        if let Err(e) = self.cache.put(chunk_id, &options, &bundle).await {
            warn!("Failed to cache bundle for chunk {}: {}", chunk_id, e);
        }

        Ok(bundle)
    }
}

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

    #[test]
    fn test_chunk_metadata_creation() {
        let metadata = ChunkMetadata {
            id: 1,
            file_relpath: "src/main.rs".to_string(),
            worktree_path: "/workspace".to_string(),
            symbol_name: Some("main".to_string()),
            kind: "func".to_string(),
            start_line: 1,
            end_line: 10,
            signature: Some("fn main()".to_string()),
            docstring: None,
        };

        assert_eq!(metadata.id, 1);
        assert_eq!(metadata.file_relpath, "src/main.rs");
        assert_eq!(metadata.symbol_name, Some("main".to_string()));
    }

    // Note: Database integration tests are in tests/context/assembler_test.rs
}