agentis-ctx 0.2.1

Fast CLI tool that generates AI-ready context from your codebase, with built-in code intelligence
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
658
659
660
661
662
663
664
665
666
667
668
669
//! Analytics module using DuckDB for complex graph queries.
//!
//! This module provides:
//! - Recursive call graph traversal
//! - Impact analysis (reverse call graph)
//! - Module dependency analysis
//! - Codebase statistics aggregations

use std::path::Path;

use duckdb::{params, Connection, Result};

use super::{CallGraphNode, ComplexityResult, FileStats, ImpactNode};
use crate::index::{CTX_DIR, DB_FILE};

/// DuckDB analytics engine for code intelligence.
pub struct Analytics {
    conn: Connection,
}

impl Analytics {
    /// Open DuckDB and attach the SQLite database.
    pub fn open(root: &Path) -> Result<Self> {
        let ctx_dir = root.join(CTX_DIR);
        let sqlite_path = ctx_dir.join(DB_FILE);

        // Create in-memory DuckDB and attach SQLite
        let conn = Connection::open_in_memory()?;

        // Attach SQLite database with properly escaped path
        // DuckDB uses single quotes for strings, so we need to escape any single quotes in the path
        let path_str = sqlite_path.display().to_string();
        let escaped_path = path_str.replace('\'', "''");
        conn.execute(
            &format!("ATTACH '{}' AS code (TYPE sqlite, READ_ONLY)", escaped_path),
            [],
        )?;

        // Create materialized views for better performance
        let analytics = Self { conn };
        analytics.create_materialized_views()?;

        Ok(analytics)
    }

    /// Create materialized views for common analytical queries.
    fn create_materialized_views(&self) -> Result<()> {
        // Call graph view - joins edges with symbols for fast traversal
        self.conn.execute_batch(
            r#"
            CREATE OR REPLACE VIEW call_graph AS
            SELECT 
                e.source_id,
                e.target_name,
                e.target_id,
                e.kind as edge_kind,
                e.line,
                e.context,
                s.file_path as source_file,
                s.name as source_name,
                s.kind as source_kind,
                t.file_path as target_file,
                t.name as target_name_resolved,
                t.kind as target_kind
            FROM code.edges e
            JOIN code.symbols s ON e.source_id = s.id
            LEFT JOIN code.symbols t ON e.target_id = t.id
            WHERE e.kind = 'calls';

            -- File statistics view
            CREATE OR REPLACE VIEW file_stats AS
            SELECT 
                file_path,
                COUNT(*) as symbol_count,
                COUNT(*) FILTER (WHERE kind IN ('function', 'method')) as functions,
                COUNT(*) FILTER (WHERE kind = 'struct') as structs,
                COUNT(*) FILTER (WHERE kind = 'enum') as enums,
                COUNT(*) FILTER (WHERE visibility = 'public') as public_symbols
            FROM code.symbols
            GROUP BY file_path;

            -- Symbol summary view
            CREATE OR REPLACE VIEW symbol_summary AS
            SELECT 
                kind,
                COUNT(*) as count,
                COUNT(*) FILTER (WHERE visibility = 'public') as public_count
            FROM code.symbols
            GROUP BY kind
            ORDER BY count DESC;
            "#,
        )?;

        Ok(())
    }

    /// Get the full call graph starting from a symbol (forward traversal).
    ///
    /// The `start_name` can be:
    /// - A simple name like "new" (matches first symbol with that name)
    /// - A qualified name like "LocalProvider::new"
    /// - A full ID like "src/embeddings/local.rs::LocalProvider::new@20"
    pub fn call_graph(&self, start_name: &str, max_depth: i32) -> Result<Vec<CallGraphNode>> {
        let mut stmt = self.conn.prepare(
            r#"
            WITH RECURSIVE start_symbol AS (
                -- Resolve the starting symbol by ID, qualified name, or name
                SELECT id, name, file_path, kind
                FROM code.symbols
                WHERE id = ?
                   OR qualified_name = ?
                   OR (name = ? AND NOT EXISTS (
                       SELECT 1 FROM code.symbols s2 
                       WHERE s2.id = ? OR s2.qualified_name = ?
                   ))
                ORDER BY file_path, line_start
                LIMIT 1
            ),
            graph AS (
                -- Base case: start from resolved symbol
                SELECT 
                    s.name,
                    s.file_path,
                    s.kind,
                    1 as depth,
                    s.id as current_id
                FROM code.symbols s
                JOIN start_symbol ss ON s.id = ss.id
                
                UNION ALL
                
                -- Recursive case: follow outgoing edges (only resolved edges)
                SELECT 
                    t.name,
                    t.file_path,
                    t.kind,
                    g.depth + 1 as depth,
                    t.id as current_id
                FROM graph g
                JOIN code.edges e ON e.source_id = g.current_id
                JOIN code.symbols t ON e.target_id = t.id
                WHERE g.depth < ?
                  AND e.kind = 'calls'
                  AND e.target_id IS NOT NULL
            )
            SELECT DISTINCT name, file_path, kind, MIN(depth) as depth
            FROM graph
            WHERE depth > 1  -- Exclude the starting symbol itself
            GROUP BY name, file_path, kind
            ORDER BY depth, name
            "#,
        )?;

        let rows = stmt.query_map(
            params![start_name, start_name, start_name, start_name, start_name, max_depth],
            |row| {
                Ok(CallGraphNode {
                    name: row.get(0)?,
                    file_path: row.get(1)?,
                    kind: row.get(2)?,
                    depth: row.get(3)?,
                })
            },
        )?;

        rows.collect()
    }

    /// Impact analysis: find all symbols that would be affected by changing the target.
    ///
    /// The `target_name` can be:
    /// - A simple name like "new" (matches first symbol with that name)
    /// - A qualified name like "LocalProvider::new"
    /// - A full ID like "src/embeddings/local.rs::LocalProvider::new@20"
    pub fn impact_analysis(&self, target_name: &str, max_depth: i32) -> Result<Vec<ImpactNode>> {
        let mut stmt = self.conn.prepare(
            r#"
            WITH RECURSIVE target_symbol AS (
                -- Resolve the target symbol by ID, qualified name, or name
                SELECT id, name, file_path
                FROM code.symbols
                WHERE id = ?
                   OR qualified_name = ?
                   OR (name = ? AND NOT EXISTS (
                       SELECT 1 FROM code.symbols s2 
                       WHERE s2.id = ? OR s2.qualified_name = ?
                   ))
                ORDER BY file_path, line_start
                LIMIT 1
            ),
            impact AS (
                -- Base case: find direct callers of the specific target symbol
                SELECT 
                    s.name,
                    s.file_path,
                    s.kind,
                    1 as distance,
                    s.id as current_id
                FROM code.edges e
                JOIN code.symbols s ON e.source_id = s.id
                JOIN target_symbol ts ON 
                    -- Only traverse resolved edges to avoid cross-file false positives
                    e.target_id IS NOT NULL AND e.target_id = ts.id
                WHERE e.kind = 'calls'
                
                UNION ALL
                
                -- Recursive case: find callers of callers (reverse traversal)
                SELECT 
                    s.name,
                    s.file_path,
                    s.kind,
                    i.distance + 1 as distance,
                    s.id as current_id
                FROM impact i
                JOIN code.edges e ON 
                    -- Only traverse resolved edges to avoid cross-file false positives
                    e.target_id IS NOT NULL AND e.target_id = i.current_id
                JOIN code.symbols s ON e.source_id = s.id
                WHERE i.distance < ?
                  AND e.kind = 'calls'
            )
            SELECT DISTINCT name, file_path, kind, MIN(distance) as distance
            FROM impact
            GROUP BY name, file_path, kind
            ORDER BY distance, name
            "#,
        )?;

        let rows = stmt.query_map(
            params![
                target_name,
                target_name,
                target_name,
                target_name,
                target_name,
                max_depth
            ],
            |row| {
                Ok(ImpactNode {
                    name: row.get(0)?,
                    file_path: row.get(1)?,
                    kind: row.get(2)?,
                    distance: row.get(3)?,
                })
            },
        )?;

        rows.collect()
    }

    /// Get file statistics for all indexed files.
    pub fn file_statistics(&self) -> Result<Vec<FileStats>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT file_path, symbol_count, functions, structs, enums, public_symbols
            FROM file_stats
            ORDER BY symbol_count DESC
            "#,
        )?;

        let rows = stmt.query_map([], |row| {
            Ok(FileStats {
                file_path: row.get(0)?,
                symbol_count: row.get(1)?,
                functions: row.get(2)?,
                structs: row.get(3)?,
                enums: row.get(4)?,
                public_symbols: row.get(5)?,
            })
        })?;

        rows.collect()
    }

    /// Get symbol counts by kind.
    #[allow(dead_code)]
    pub fn symbol_summary(&self) -> Result<Vec<(String, i64, i64)>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT kind, count, public_count
            FROM symbol_summary
            "#,
        )?;

        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?;

        rows.collect()
    }

    /// Find if a path exists between two symbols (simplified version).
    ///
    /// Both `from_name` and `to_name` can be symbol IDs, qualified names, or simple names.
    #[allow(dead_code)]
    pub fn has_path(&self, from_name: &str, to_name: &str, max_depth: i32) -> Result<bool> {
        let mut stmt = self.conn.prepare(
            r#"
            WITH RECURSIVE source_symbol AS (
                SELECT id, name FROM code.symbols
                WHERE id = ? OR qualified_name = ? OR name = ?
                ORDER BY file_path
                LIMIT 1
            ),
            target_symbol AS (
                SELECT id, name FROM code.symbols
                WHERE id = ? OR qualified_name = ? OR name = ?
                ORDER BY file_path
                LIMIT 1
            ),
            reachable AS (
                -- Base case: start from source
                SELECT 
                    s.name,
                    s.id as current_id,
                    1 as depth
                FROM code.symbols s
                JOIN source_symbol src ON s.id = src.id
                
                UNION
                
                -- Follow edges using only resolved target_id to avoid cross-file false positives
                SELECT 
                    t.name,
                    t.id,
                    r.depth + 1
                FROM reachable r
                JOIN code.edges e ON e.source_id = r.current_id
                JOIN code.symbols t ON e.target_id = t.id
                WHERE r.depth < ?
                  AND e.kind = 'calls'
                  AND e.target_id IS NOT NULL
            )
            SELECT COUNT(*) > 0
            FROM reachable r
            JOIN target_symbol tgt ON r.current_id = tgt.id
            "#,
        )?;

        let result: bool = stmt.query_row(
            params![from_name, from_name, from_name, to_name, to_name, to_name, max_depth],
            |row| row.get(0),
        )?;
        Ok(result)
    }

    /// Get the most connected symbols (highest in/out degree).
    ///
    /// This correctly counts incoming edges per symbol ID, not by name,
    /// avoiding over-counting for common names like "new".
    pub fn most_connected(&self, limit: i32) -> Result<Vec<(String, String, i64, i64)>> {
        let mut stmt = self.conn.prepare(
            r#"
            WITH outgoing AS (
                SELECT source_id, COUNT(*) as out_degree
                FROM code.edges
                WHERE kind = 'calls'
                GROUP BY source_id
            ),
            incoming AS (
                -- Count incoming edges by target_id when available
                SELECT target_id as id, COUNT(*) as in_degree
                FROM code.edges
                WHERE kind = 'calls' AND target_id IS NOT NULL
                GROUP BY target_id
            )
            SELECT 
                s.name,
                s.file_path,
                COALESCE(o.out_degree, 0) as out_degree,
                COALESCE(i.in_degree, 0) as in_degree
            FROM code.symbols s
            LEFT JOIN outgoing o ON s.id = o.source_id
            LEFT JOIN incoming i ON s.id = i.id
            WHERE s.kind IN ('function', 'method')
            ORDER BY (COALESCE(o.out_degree, 0) + COALESCE(i.in_degree, 0)) DESC
            LIMIT ?
            "#,
        )?;

        let rows = stmt.query_map(params![limit], |row| {
            Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?))
        })?;

        rows.collect()
    }

    /// Check if there are any self-recursive functions.
    ///
    /// Uses target_id when available for accurate recursion detection.
    #[allow(dead_code)]
    pub fn find_recursive_functions(&self) -> Result<Vec<(String, String)>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT DISTINCT s.name, s.file_path
            FROM code.edges e
            JOIN code.symbols s ON e.source_id = s.id
            WHERE e.kind = 'calls'
              AND (
                  (e.target_id IS NOT NULL AND e.target_id = s.id)
                  OR (e.target_id IS NULL AND e.target_name = s.name)
              )
            ORDER BY s.name
            "#,
        )?;

        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))?;
        rows.collect()
    }

    /// Get dependency graph between files (module-level).
    ///
    /// Uses target_id when available for accurate file resolution.
    pub fn file_dependencies(&self) -> Result<Vec<(String, String, i64)>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT 
                s.file_path as source_file,
                COALESCE(t.file_path, 'external') as target_file,
                COUNT(*) as call_count
            FROM code.edges e
            JOIN code.symbols s ON e.source_id = s.id
            LEFT JOIN code.symbols t ON 
                (e.target_id IS NOT NULL AND e.target_id = t.id)
                OR (e.target_id IS NULL AND e.target_name = t.name)
            WHERE e.kind = 'calls'
              AND s.file_path != COALESCE(t.file_path, '')
            GROUP BY s.file_path, COALESCE(t.file_path, 'external')
            ORDER BY call_count DESC
            "#,
        )?;

        let rows = stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)))?;

        rows.collect()
    }

    /// Analyze code complexity based on fan-out (outgoing calls) and fan-in (incoming calls).
    ///
    /// This correctly counts incoming edges per symbol ID, not by name,
    /// avoiding over-counting for common names like "new".
    pub fn complexity_analysis(&self, threshold: i64) -> Result<Vec<ComplexityResult>> {
        let mut stmt = self.conn.prepare(
            r#"
            WITH outgoing AS (
                SELECT source_id, COUNT(*) as out_degree
                FROM code.edges
                WHERE kind = 'calls'
                GROUP BY source_id
            ),
            incoming AS (
                -- Count incoming edges by target_id when available
                SELECT target_id as id, COUNT(*) as in_degree
                FROM code.edges
                WHERE kind = 'calls' AND target_id IS NOT NULL
                GROUP BY target_id
            )
            SELECT 
                s.name,
                s.file_path,
                s.line_start,
                COALESCE(o.out_degree, 0) as fan_out,
                COALESCE(i.in_degree, 0) as fan_in,
                -- Complexity score: weighted combination of fan-out (more important) and fan-in
                (COALESCE(o.out_degree, 0) * 2 + COALESCE(i.in_degree, 0)) as complexity_score,
                CASE 
                    WHEN COALESCE(o.out_degree, 0) > 50 THEN 'critical'
                    WHEN COALESCE(o.out_degree, 0) > 30 THEN 'high'
                    WHEN COALESCE(o.out_degree, 0) > ? THEN 'medium'
                    ELSE 'low'
                END as severity
            FROM code.symbols s
            LEFT JOIN outgoing o ON s.id = o.source_id
            LEFT JOIN incoming i ON s.id = i.id
            WHERE s.kind IN ('function', 'method')
            ORDER BY complexity_score DESC
            "#,
        )?;

        let rows = stmt.query_map(params![threshold], |row| {
            Ok(ComplexityResult {
                name: row.get(0)?,
                file_path: row.get(1)?,
                line: row.get(2)?,
                fan_out: row.get(3)?,
                fan_in: row.get(4)?,
                complexity_score: row.get(5)?,
                severity: row.get(6)?,
            })
        })?;

        rows.collect()
    }

    /// Get the full call graph (all edges with resolved symbols).
    ///
    /// Uses target_id when available for accurate symbol resolution.
    pub fn full_call_graph(
        &self,
        _max_depth: i32,
    ) -> Result<Vec<(String, String, String, String)>> {
        let mut stmt = self.conn.prepare(
            r#"
            SELECT DISTINCT
                s.file_path as source_file,
                s.name as source_name,
                COALESCE(t.file_path, 'external') as target_file,
                COALESCE(t.name, e.target_name) as target_name
            FROM code.edges e
            JOIN code.symbols s ON e.source_id = s.id
            LEFT JOIN code.symbols t ON 
                (e.target_id IS NOT NULL AND e.target_id = t.id)
                OR (e.target_id IS NULL AND e.target_name = t.name)
            WHERE e.kind = 'calls'
            ORDER BY s.file_path, s.name
            "#,
        )?;

        let rows = stmt.query_map([], |row| {
            Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?))
        })?;

        rows.collect()
    }
}

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

    fn setup_test_db() -> Result<Connection> {
        let conn = Connection::open_in_memory()?;

        // Create schema
        conn.execute_batch(
            r#"
            CREATE SCHEMA IF NOT EXISTS code;
            
            CREATE TABLE code.symbols (
                id TEXT PRIMARY KEY,
                name TEXT NOT NULL,
                qualified_name TEXT,
                kind TEXT NOT NULL,
                file_path TEXT NOT NULL,
                line_start INTEGER NOT NULL,
                line_end INTEGER NOT NULL,
                visibility TEXT
            );
            
            CREATE TABLE code.edges (
                source_id TEXT NOT NULL,
                target_id TEXT,
                target_name TEXT NOT NULL,
                kind TEXT NOT NULL,
                context TEXT,
                FOREIGN KEY (source_id) REFERENCES code.symbols(id)
            );
            
            -- Insert test data: main -> run -> helper
            INSERT INTO code.symbols VALUES 
                ('main@1', 'main', NULL, 'function', 'test.rs', 1, 10, 'public'),
                ('run@11', 'run', NULL, 'function', 'test.rs', 11, 20, 'private'),
                ('helper@21', 'helper', NULL, 'function', 'test.rs', 21, 30, 'private');
            
            INSERT INTO code.edges VALUES 
                ('main@1', 'run@11', 'run', 'calls', NULL),
                ('run@11', 'helper@21', 'helper', 'calls', NULL);
            "#,
        )?;

        Ok(conn)
    }

    #[test]
    fn test_call_graph_syntax() {
        let conn = setup_test_db().expect("Failed to setup test db");
        let analytics = Analytics { conn };

        let result = analytics.call_graph("main", 5);
        assert!(
            result.is_ok(),
            "call_graph query failed: {:?}",
            result.err()
        );

        let nodes = result.unwrap();
        assert_eq!(nodes.len(), 2, "Expected 2 nodes (run, helper)");
        assert_eq!(nodes[0].name, "run");
        assert_eq!(nodes[1].name, "helper");
    }

    #[test]
    fn test_impact_analysis_syntax() {
        let conn = setup_test_db().expect("Failed to setup test db");
        let analytics = Analytics { conn };

        let result = analytics.impact_analysis("helper", 5);
        assert!(
            result.is_ok(),
            "impact_analysis query failed: {:?}",
            result.err()
        );

        let nodes = result.unwrap();
        assert_eq!(nodes.len(), 2, "Expected 2 nodes (run, main)");
        // run calls helper directly (distance 1)
        // main calls run which calls helper (distance 2)
        assert!(nodes.iter().any(|n| n.name == "run" && n.distance == 1));
        assert!(nodes.iter().any(|n| n.name == "main" && n.distance == 2));
    }

    #[test]
    fn test_has_path_syntax() {
        let conn = setup_test_db().expect("Failed to setup test db");
        let analytics = Analytics { conn };

        // main -> run -> helper (path exists)
        let result = analytics.has_path("main", "helper", 5);
        assert!(result.is_ok(), "has_path query failed: {:?}", result.err());
        assert!(result.unwrap(), "Expected path from main to helper");

        // helper -> main (no path in reverse direction)
        let result = analytics.has_path("helper", "main", 5);
        assert!(result.is_ok(), "has_path query failed: {:?}", result.err());
        assert!(!result.unwrap(), "Expected no path from helper to main");
    }

    #[test]
    fn test_sql_injection_in_path_escaping() {
        // Test that paths with special SQL characters are properly escaped
        // This is a unit test for the escaping logic - the actual open() function
        // requires a real SQLite file, so we test the escaping directly

        // Test paths that would cause SQL injection if not escaped
        let test_paths = [
            "normal/path.db",
            "path with spaces/file.db",
            "path'with'quotes/file.db",
            "path''with''double/file.db",
            "path;DROP TABLE code;/file.db",
            "path' OR '1'='1/file.db",
        ];

        for path in &test_paths {
            // Simulate the escaping done in Analytics::open()
            let escaped = path.replace('\'', "''");
            let sql = format!("ATTACH '{}' AS code", escaped);

            // The escaped SQL should have balanced quotes
            let quote_count = sql.chars().filter(|c| *c == '\'').count();
            assert_eq!(
                quote_count % 2,
                0,
                "SQL for path '{}' has unbalanced quotes: {}",
                path,
                sql
            );

            // Single quotes in the path should be doubled
            if path.contains('\'') {
                assert!(
                    escaped.contains("''"),
                    "Path with quote should have doubled quotes: {}",
                    escaped
                );
            }
        }
    }
}