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
//! Graph traversal module for chunk relationships
//!
//! Provides recursive traversal of chunk_edges for:
//! - Caller/callee relationships (function calls)
//! - Import relationships (module imports)
//! - Extension relationships (class inheritance)
//!
//! Uses SQLite recursive CTEs for transitive closure queries
//! with cycle detection to handle circular dependencies.

use anyhow::Result;
use rusqlite::{params, Connection};

/// Default maximum depth for graph traversal
pub const DEFAULT_MAX_DEPTH: usize = 3;

/// Hard maximum depth to prevent runaway recursion
pub const HARD_MAX_DEPTH: usize = 10;

pub use crate::db::types::GraphResult;
pub use crate::db::types::ImportDirection;

/// Parse path string into vector of chunk IDs
///
/// Path format: "/id1/id2/id3" where each ID is separated by /
fn parse_path(path_str: &str) -> Vec<i64> {
    path_str
        .trim_matches('/')
        .split('/')
        .filter(|s| !s.is_empty())
        .filter_map(|s| s.parse().ok())
        .collect()
}

/// Find all chunks that call the target chunk (directly or transitively)
///
/// # Arguments
/// * `conn` - SQLite connection
/// * `target_chunk_id` - The chunk to find callers for
/// * `max_depth` - Maximum traversal depth (default 3, max 10)
///
/// # Returns
/// Vector of GraphResult ordered by depth (closest first)
pub fn find_callers(
    conn: &Connection,
    target_chunk_id: i64,
    max_depth: Option<usize>,
) -> Result<Vec<GraphResult>> {
    let depth = max_depth.unwrap_or(DEFAULT_MAX_DEPTH).min(HARD_MAX_DEPTH);

    let sql = r#"
        WITH RECURSIVE callers(chunk_id, depth, path) AS (
            -- Base case: direct callers
            SELECT src_chunk_id, 1, '/' || src_chunk_id
            FROM chunk_edges
            WHERE dst_chunk_id = ?1 AND type = 'calls'

            UNION ALL

            -- Recursive case: callers of callers
            SELECT e.src_chunk_id, c.depth + 1,
                   c.path || '/' || e.src_chunk_id
            FROM chunk_edges e
            JOIN callers c ON e.dst_chunk_id = c.chunk_id
            WHERE c.depth < ?2
              AND e.type = 'calls'
              -- Cycle detection: don't revisit chunks in path
              AND c.path NOT LIKE '%/' || e.src_chunk_id || '/%'
              AND c.path NOT LIKE '%/' || e.src_chunk_id
        )
        SELECT DISTINCT chunk_id, depth, path
        FROM callers
        ORDER BY depth, chunk_id
    "#;

    let mut stmt = conn.prepare(sql)?;
    let results = stmt.query_map(params![target_chunk_id, depth], |row| {
        let chunk_id: i64 = row.get(0)?;
        let depth: i64 = row.get(1)?;
        let path_str: String = row.get(2)?;
        Ok(GraphResult {
            chunk_id,
            depth: depth as usize,
            path: parse_path(&path_str),
            edge_type: "calls".to_string(),
        })
    })?;

    results
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| anyhow::anyhow!("{}", e))
}

/// Find all chunks called by the source chunk (directly or transitively)
///
/// # Arguments
/// * `conn` - SQLite connection
/// * `source_chunk_id` - The chunk to find callees for
/// * `max_depth` - Maximum traversal depth (default 3, max 10)
///
/// # Returns
/// Vector of GraphResult ordered by depth (closest first)
pub fn find_callees(
    conn: &Connection,
    source_chunk_id: i64,
    max_depth: Option<usize>,
) -> Result<Vec<GraphResult>> {
    let depth = max_depth.unwrap_or(DEFAULT_MAX_DEPTH).min(HARD_MAX_DEPTH);

    let sql = r#"
        WITH RECURSIVE callees(chunk_id, depth, path) AS (
            -- Base case: direct callees
            SELECT dst_chunk_id, 1, '/' || dst_chunk_id
            FROM chunk_edges
            WHERE src_chunk_id = ?1 AND type = 'calls'

            UNION ALL

            -- Recursive case: callees of callees
            SELECT e.dst_chunk_id, c.depth + 1,
                   c.path || '/' || e.dst_chunk_id
            FROM chunk_edges e
            JOIN callees c ON e.src_chunk_id = c.chunk_id
            WHERE c.depth < ?2
              AND e.type = 'calls'
              -- Cycle detection: don't revisit chunks in path
              AND c.path NOT LIKE '%/' || e.dst_chunk_id || '/%'
              AND c.path NOT LIKE '%/' || e.dst_chunk_id
        )
        SELECT DISTINCT chunk_id, depth, path
        FROM callees
        ORDER BY depth, chunk_id
    "#;

    let mut stmt = conn.prepare(sql)?;
    let results = stmt.query_map(params![source_chunk_id, depth], |row| {
        let chunk_id: i64 = row.get(0)?;
        let depth: i64 = row.get(1)?;
        let path_str: String = row.get(2)?;
        Ok(GraphResult {
            chunk_id,
            depth: depth as usize,
            path: parse_path(&path_str),
            edge_type: "calls".to_string(),
        })
    })?;

    results
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| anyhow::anyhow!("{}", e))
}

/// Find import relationships for a chunk
///
/// # Arguments
/// * `conn` - SQLite connection
/// * `chunk_id` - The chunk to find imports for
/// * `direction` - Incoming (who imports this) or Outgoing (what this imports)
/// * `max_depth` - Maximum traversal depth (default 3, max 10)
///
/// # Returns
/// Vector of GraphResult ordered by depth (closest first)
pub fn find_imports(
    conn: &Connection,
    chunk_id: i64,
    direction: ImportDirection,
    max_depth: Option<usize>,
) -> Result<Vec<GraphResult>> {
    let depth = max_depth.unwrap_or(DEFAULT_MAX_DEPTH).min(HARD_MAX_DEPTH);

    let sql = match direction {
        ImportDirection::Incoming => {
            // Find chunks that import the target
            r#"
                WITH RECURSIVE importers(chunk_id, depth, path) AS (
                    -- Base case: direct importers
                    SELECT src_chunk_id, 1, '/' || src_chunk_id
                    FROM chunk_edges
                    WHERE dst_chunk_id = ?1 AND type = 'imports'

                    UNION ALL

                    -- Recursive case: importers of importers
                    SELECT e.src_chunk_id, i.depth + 1,
                           i.path || '/' || e.src_chunk_id
                    FROM chunk_edges e
                    JOIN importers i ON e.dst_chunk_id = i.chunk_id
                    WHERE i.depth < ?2
                      AND e.type = 'imports'
                      -- Cycle detection
                      AND i.path NOT LIKE '%/' || e.src_chunk_id || '/%'
                      AND i.path NOT LIKE '%/' || e.src_chunk_id
                )
                SELECT DISTINCT chunk_id, depth, path
                FROM importers
                ORDER BY depth, chunk_id
            "#
        }
        ImportDirection::Outgoing => {
            // Find chunks that the source imports
            r#"
                WITH RECURSIVE imported(chunk_id, depth, path) AS (
                    -- Base case: direct imports
                    SELECT dst_chunk_id, 1, '/' || dst_chunk_id
                    FROM chunk_edges
                    WHERE src_chunk_id = ?1 AND type = 'imports'

                    UNION ALL

                    -- Recursive case: imports of imports
                    SELECT e.dst_chunk_id, i.depth + 1,
                           i.path || '/' || e.dst_chunk_id
                    FROM chunk_edges e
                    JOIN imported i ON e.src_chunk_id = i.chunk_id
                    WHERE i.depth < ?2
                      AND e.type = 'imports'
                      -- Cycle detection
                      AND i.path NOT LIKE '%/' || e.dst_chunk_id || '/%'
                      AND i.path NOT LIKE '%/' || e.dst_chunk_id
                )
                SELECT DISTINCT chunk_id, depth, path
                FROM imported
                ORDER BY depth, chunk_id
            "#
        }
    };

    let mut stmt = conn.prepare(sql)?;
    let results = stmt.query_map(params![chunk_id, depth], |row| {
        let chunk_id: i64 = row.get(0)?;
        let depth: i64 = row.get(1)?;
        let path_str: String = row.get(2)?;
        Ok(GraphResult {
            chunk_id,
            depth: depth as usize,
            path: parse_path(&path_str),
            edge_type: "imports".to_string(),
        })
    })?;

    results
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| anyhow::anyhow!("{}", e))
}

/// Find extension/inheritance relationships for a chunk
///
/// # Arguments
/// * `conn` - SQLite connection
/// * `chunk_id` - The chunk to find extensions for
/// * `direction` - Incoming (what extends this) or Outgoing (what this extends)
/// * `max_depth` - Maximum traversal depth (default 3, max 10)
///
/// # Returns
/// Vector of GraphResult ordered by depth (closest first)
pub fn find_extensions(
    conn: &Connection,
    chunk_id: i64,
    direction: ImportDirection, // Reusing enum for direction
    max_depth: Option<usize>,
) -> Result<Vec<GraphResult>> {
    let depth = max_depth.unwrap_or(DEFAULT_MAX_DEPTH).min(HARD_MAX_DEPTH);

    let sql = match direction {
        ImportDirection::Incoming => {
            // Find chunks that extend the target (subclasses)
            r#"
                WITH RECURSIVE subclasses(chunk_id, depth, path) AS (
                    -- Base case: direct subclasses
                    SELECT src_chunk_id, 1, '/' || src_chunk_id
                    FROM chunk_edges
                    WHERE dst_chunk_id = ?1 AND type = 'extends'

                    UNION ALL

                    -- Recursive case: subclasses of subclasses
                    SELECT e.src_chunk_id, s.depth + 1,
                           s.path || '/' || e.src_chunk_id
                    FROM chunk_edges e
                    JOIN subclasses s ON e.dst_chunk_id = s.chunk_id
                    WHERE s.depth < ?2
                      AND e.type = 'extends'
                      -- Cycle detection
                      AND s.path NOT LIKE '%/' || e.src_chunk_id || '/%'
                      AND s.path NOT LIKE '%/' || e.src_chunk_id
                )
                SELECT DISTINCT chunk_id, depth, path
                FROM subclasses
                ORDER BY depth, chunk_id
            "#
        }
        ImportDirection::Outgoing => {
            // Find chunks that the source extends (superclasses)
            r#"
                WITH RECURSIVE superclasses(chunk_id, depth, path) AS (
                    -- Base case: direct superclasses
                    SELECT dst_chunk_id, 1, '/' || dst_chunk_id
                    FROM chunk_edges
                    WHERE src_chunk_id = ?1 AND type = 'extends'

                    UNION ALL

                    -- Recursive case: superclasses of superclasses
                    SELECT e.dst_chunk_id, s.depth + 1,
                           s.path || '/' || e.dst_chunk_id
                    FROM chunk_edges e
                    JOIN superclasses s ON e.src_chunk_id = s.chunk_id
                    WHERE s.depth < ?2
                      AND e.type = 'extends'
                      -- Cycle detection
                      AND s.path NOT LIKE '%/' || e.dst_chunk_id || '/%'
                      AND s.path NOT LIKE '%/' || e.dst_chunk_id
                )
                SELECT DISTINCT chunk_id, depth, path
                FROM superclasses
                ORDER BY depth, chunk_id
            "#
        }
    };

    let mut stmt = conn.prepare(sql)?;
    let results = stmt.query_map(params![chunk_id, depth], |row| {
        let chunk_id: i64 = row.get(0)?;
        let depth: i64 = row.get(1)?;
        let path_str: String = row.get(2)?;
        Ok(GraphResult {
            chunk_id,
            depth: depth as usize,
            path: parse_path(&path_str),
            edge_type: "extends".to_string(),
        })
    })?;

    results
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| anyhow::anyhow!("{}", e))
}

/// Get all direct edges from or to a chunk (without recursion)
///
/// # Arguments
/// * `conn` - SQLite connection
/// * `chunk_id` - The chunk to find edges for
/// * `direction` - Incoming (edges pointing to chunk) or Outgoing (edges from chunk)
///
/// # Returns
/// Vector of GraphResult with depth=1 for all direct relationships
pub fn get_direct_edges(
    conn: &Connection,
    chunk_id: i64,
    direction: ImportDirection,
) -> Result<Vec<GraphResult>> {
    let sql = match direction {
        ImportDirection::Incoming => {
            "SELECT src_chunk_id, type FROM chunk_edges WHERE dst_chunk_id = ?1"
        }
        ImportDirection::Outgoing => {
            "SELECT dst_chunk_id, type FROM chunk_edges WHERE src_chunk_id = ?1"
        }
    };

    let mut stmt = conn.prepare(sql)?;
    let results = stmt.query_map(params![chunk_id], |row| {
        let related_chunk_id: i64 = row.get(0)?;
        let edge_type: String = row.get(1)?;
        Ok(GraphResult {
            chunk_id: related_chunk_id,
            depth: 1,
            path: vec![related_chunk_id],
            edge_type,
        })
    })?;

    results
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| anyhow::anyhow!("{}", e))
}

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

    #[test]
    fn test_parse_path_basic() {
        let path = parse_path("/1/2/3");
        assert_eq!(path, vec![1, 2, 3]);
    }

    #[test]
    fn test_parse_path_single() {
        let path = parse_path("/42");
        assert_eq!(path, vec![42]);
    }

    #[test]
    fn test_parse_path_empty() {
        let path = parse_path("");
        assert!(path.is_empty());
    }

    #[test]
    fn test_parse_path_no_slashes() {
        let path = parse_path("123");
        assert_eq!(path, vec![123]);
    }

    #[test]
    fn test_parse_path_trailing_slash() {
        let path = parse_path("/1/2/3/");
        assert_eq!(path, vec![1, 2, 3]);
    }

    #[test]
    fn test_parse_path_invalid_elements() {
        let path = parse_path("/1/abc/2");
        assert_eq!(path, vec![1, 2]); // abc is filtered out
    }

    #[test]
    fn test_default_max_depth() {
        assert_eq!(DEFAULT_MAX_DEPTH, 3);
    }

    #[test]
    fn test_hard_max_depth() {
        assert_eq!(HARD_MAX_DEPTH, 10);
    }

    #[test]
    fn test_import_direction_variants() {
        let incoming = ImportDirection::Incoming;
        let outgoing = ImportDirection::Outgoing;
        assert_ne!(incoming, outgoing);
    }

    #[test]
    fn test_graph_result_construction() {
        let result = GraphResult {
            chunk_id: 42,
            depth: 2,
            path: vec![1, 2, 42],
            edge_type: "calls".to_string(),
        };
        assert_eq!(result.chunk_id, 42);
        assert_eq!(result.depth, 2);
        assert_eq!(result.path.len(), 3);
        assert_eq!(result.edge_type, "calls");
    }

    #[test]
    fn test_depth_clamping() {
        // Test that depth is properly limited
        let clamped = Some(100_usize)
            .unwrap_or(DEFAULT_MAX_DEPTH)
            .min(HARD_MAX_DEPTH);
        assert_eq!(clamped, HARD_MAX_DEPTH);

        let default = None::<usize>
            .unwrap_or(DEFAULT_MAX_DEPTH)
            .min(HARD_MAX_DEPTH);
        assert_eq!(default, DEFAULT_MAX_DEPTH);
    }
}