agentic-codebase 0.3.0

Semantic code compiler for AI agents - transforms codebases into navigable concept graphs
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
//! Enhanced Code Prophecy — Invention 2.
//!
//! Simulate the future state of the codebase based on current trajectory
//! and proposed changes. "Should I refactor this?" answered with data.

use serde::{Deserialize, Serialize};

use crate::graph::CodeGraph;
use crate::types::CodeUnitType;

// ── Types ────────────────────────────────────────────────────────────────────

/// A prophecy about code evolution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CodeProphecy {
    /// What we're prophesying about.
    pub subject: ProphecySubject,
    /// Time horizon.
    pub horizon: ProphecyHorizon,
    /// Predicted outcomes.
    pub predictions: Vec<EnhancedPrediction>,
    /// Confidence in prophecy.
    pub confidence: f64,
    /// Evidence supporting prophecy.
    pub evidence: Vec<ProphecyEvidence>,
}

/// What the prophecy is about.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProphecySubject {
    /// Specific function/class.
    Node(u64),
    /// Entire module.
    Module(String),
    /// Architectural pattern.
    Pattern(String),
}

/// Time horizon for predictions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ProphecyHorizon {
    /// Next few changes.
    Immediate,
    /// Next sprint/week.
    ShortTerm,
    /// Next month.
    MediumTerm,
    /// Next quarter.
    LongTerm,
}

/// A single prediction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedPrediction {
    /// What will happen.
    pub outcome: String,
    /// Probability (0.0 - 1.0).
    pub probability: f64,
    /// Is this good or bad?
    pub sentiment: Sentiment,
    /// What triggers this outcome.
    pub trigger: String,
}

/// Sentiment of a prediction.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Sentiment {
    Positive,
    Neutral,
    Negative,
    Critical,
}

/// Evidence supporting a prophecy.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProphecyEvidence {
    /// Type of evidence.
    pub evidence_type: EvidenceType,
    /// The evidence.
    pub description: String,
    /// Weight in prediction.
    pub weight: f64,
}

/// Type of evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvidenceType {
    /// Historical pattern in this codebase.
    Historical,
    /// Structural analysis.
    Structural,
    /// Complexity metrics.
    Complexity,
    /// Dependency analysis.
    Dependency,
    /// Industry pattern.
    IndustryPattern,
}

// ── EnhancedProphecyEngine ───────────────────────────────────────────────────

/// Enhanced prophecy engine with evidence-backed predictions.
pub struct EnhancedProphecyEngine<'g> {
    graph: &'g CodeGraph,
}

impl<'g> EnhancedProphecyEngine<'g> {
    pub fn new(graph: &'g CodeGraph) -> Self {
        Self { graph }
    }

    /// Generate a prophecy for a given subject and horizon.
    pub fn prophecy(&self, subject: ProphecySubject, horizon: ProphecyHorizon) -> CodeProphecy {
        let (predictions, evidence) = match &subject {
            ProphecySubject::Node(id) => self.prophesy_node(*id, horizon),
            ProphecySubject::Module(name) => self.prophesy_module(name, horizon),
            ProphecySubject::Pattern(name) => self.prophesy_pattern(name, horizon),
        };

        let confidence = if evidence.is_empty() {
            0.3
        } else {
            let avg_weight: f64 =
                evidence.iter().map(|e| e.weight).sum::<f64>() / evidence.len() as f64;
            avg_weight.min(1.0)
        };

        CodeProphecy {
            subject,
            horizon,
            predictions,
            confidence,
            evidence,
        }
    }

    /// "What if" scenario analysis.
    pub fn prophecy_if(
        &self,
        subject: ProphecySubject,
        scenario: &str,
        horizon: ProphecyHorizon,
    ) -> CodeProphecy {
        let mut prophecy = self.prophecy(subject, horizon);

        // Add scenario-specific predictions
        prophecy.predictions.push(EnhancedPrediction {
            outcome: format!("If {}: additional changes likely needed", scenario),
            probability: 0.6,
            sentiment: Sentiment::Neutral,
            trigger: scenario.to_string(),
        });

        prophecy
    }

    /// Compare prophecies of different approaches.
    pub fn prophecy_compare(
        &self,
        subject_a: ProphecySubject,
        subject_b: ProphecySubject,
        horizon: ProphecyHorizon,
    ) -> (CodeProphecy, CodeProphecy) {
        let a = self.prophecy(subject_a, horizon);
        let b = self.prophecy(subject_b, horizon);
        (a, b)
    }

    // ── Internal ─────────────────────────────────────────────────────────

    fn prophesy_node(
        &self,
        id: u64,
        _horizon: ProphecyHorizon,
    ) -> (Vec<EnhancedPrediction>, Vec<ProphecyEvidence>) {
        let mut predictions = Vec::new();
        let mut evidence = Vec::new();

        if let Some(unit) = self.graph.get_unit(id) {
            // Complexity analysis
            if unit.complexity > 15 {
                predictions.push(EnhancedPrediction {
                    outcome: "High risk of bugs due to complexity".to_string(),
                    probability: 0.7,
                    sentiment: Sentiment::Negative,
                    trigger: format!("Cyclomatic complexity: {}", unit.complexity),
                });
                evidence.push(ProphecyEvidence {
                    evidence_type: EvidenceType::Complexity,
                    description: format!("Complexity score: {} (threshold: 15)", unit.complexity),
                    weight: 0.8,
                });
            }

            // Change frequency analysis
            if unit.change_count > 10 {
                predictions.push(EnhancedPrediction {
                    outcome: "Frequently modified — likely needs refactoring".to_string(),
                    probability: 0.6,
                    sentiment: Sentiment::Negative,
                    trigger: format!("{} changes recorded", unit.change_count),
                });
                evidence.push(ProphecyEvidence {
                    evidence_type: EvidenceType::Historical,
                    description: format!("Changed {} times", unit.change_count),
                    weight: 0.7,
                });
            }

            // Stability analysis
            if unit.stability_score < 0.3 {
                predictions.push(EnhancedPrediction {
                    outcome: "Unstable code — expect more changes".to_string(),
                    probability: 0.8,
                    sentiment: Sentiment::Negative,
                    trigger: format!("Stability score: {:.2}", unit.stability_score),
                });
                evidence.push(ProphecyEvidence {
                    evidence_type: EvidenceType::Structural,
                    description: format!("Stability score: {:.2}", unit.stability_score),
                    weight: 0.8,
                });
            }

            // Dependency analysis
            let incoming = self.graph.edges_to(id).len();
            let outgoing = self.graph.edges_from(id).len();
            if incoming > 10 {
                predictions.push(EnhancedPrediction {
                    outcome: "High coupling — changes here affect many dependents".to_string(),
                    probability: 0.75,
                    sentiment: Sentiment::Critical,
                    trigger: format!("{} incoming dependencies", incoming),
                });
                evidence.push(ProphecyEvidence {
                    evidence_type: EvidenceType::Dependency,
                    description: format!("{} dependents, {} dependencies", incoming, outgoing),
                    weight: 0.9,
                });
            }

            // Default positive prediction if nothing concerning
            if predictions.is_empty() {
                predictions.push(EnhancedPrediction {
                    outcome: "Code appears stable with manageable complexity".to_string(),
                    probability: 0.7,
                    sentiment: Sentiment::Positive,
                    trigger: "No risk factors detected".to_string(),
                });
            }
        }

        (predictions, evidence)
    }

    fn prophesy_module(
        &self,
        module_name: &str,
        _horizon: ProphecyHorizon,
    ) -> (Vec<EnhancedPrediction>, Vec<ProphecyEvidence>) {
        let mut predictions = Vec::new();
        let mut evidence = Vec::new();

        // Find units in this module
        let module_units: Vec<_> = self
            .graph
            .units()
            .iter()
            .filter(|u| u.qualified_name.starts_with(module_name))
            .collect();

        if module_units.is_empty() {
            predictions.push(EnhancedPrediction {
                outcome: format!("Module '{}' not found in codebase", module_name),
                probability: 1.0,
                sentiment: Sentiment::Neutral,
                trigger: "Module not indexed".to_string(),
            });
            return (predictions, evidence);
        }

        let avg_complexity: f64 = module_units
            .iter()
            .map(|u| u.complexity as f64)
            .sum::<f64>()
            / module_units.len() as f64;
        let total_changes: u32 = module_units.iter().map(|u| u.change_count).sum();
        let function_count = module_units
            .iter()
            .filter(|u| u.unit_type == CodeUnitType::Function)
            .count();

        evidence.push(ProphecyEvidence {
            evidence_type: EvidenceType::Structural,
            description: format!(
                "{} units, {} functions, avg complexity: {:.1}",
                module_units.len(),
                function_count,
                avg_complexity
            ),
            weight: 0.7,
        });

        if avg_complexity > 10.0 {
            predictions.push(EnhancedPrediction {
                outcome: "Module complexity is growing — consider refactoring".to_string(),
                probability: 0.65,
                sentiment: Sentiment::Negative,
                trigger: format!("Average complexity: {:.1}", avg_complexity),
            });
        }

        if total_changes > 50 {
            predictions.push(EnhancedPrediction {
                outcome: "Hotspot module — high change velocity".to_string(),
                probability: 0.7,
                sentiment: Sentiment::Negative,
                trigger: format!("{} total changes across module", total_changes),
            });
        }

        if predictions.is_empty() {
            predictions.push(EnhancedPrediction {
                outcome: "Module appears healthy".to_string(),
                probability: 0.7,
                sentiment: Sentiment::Positive,
                trigger: "No risk factors detected".to_string(),
            });
        }

        (predictions, evidence)
    }

    fn prophesy_pattern(
        &self,
        _pattern_name: &str,
        _horizon: ProphecyHorizon,
    ) -> (Vec<EnhancedPrediction>, Vec<ProphecyEvidence>) {
        // Pattern-level prophecy is heuristic-based
        let predictions = vec![EnhancedPrediction {
            outcome: "Pattern analysis requires more data points".to_string(),
            probability: 0.5,
            sentiment: Sentiment::Neutral,
            trigger: "Insufficient pattern data".to_string(),
        }];
        let evidence = vec![ProphecyEvidence {
            evidence_type: EvidenceType::IndustryPattern,
            description: "Pattern-level predictions require historical commit data".to_string(),
            weight: 0.3,
        }];
        (predictions, evidence)
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{CodeUnit, CodeUnitType, Language, Span};
    use std::path::PathBuf;

    fn test_graph() -> CodeGraph {
        let mut graph = CodeGraph::with_default_dimension();
        let mut unit = CodeUnit::new(
            CodeUnitType::Function,
            Language::Rust,
            "complex_func".to_string(),
            "mod::complex_func".to_string(),
            PathBuf::from("src/complex.rs"),
            Span::new(1, 0, 100, 0),
        );
        unit.complexity = 25;
        unit.change_count = 15;
        unit.stability_score = 0.2;
        graph.add_unit(unit);
        graph
    }

    #[test]
    fn prophecy_detects_complexity() {
        let graph = test_graph();
        let engine = EnhancedProphecyEngine::new(&graph);
        let prophecy = engine.prophecy(ProphecySubject::Node(0), ProphecyHorizon::ShortTerm);
        assert!(!prophecy.predictions.is_empty());
        assert!(prophecy
            .predictions
            .iter()
            .any(|p| p.sentiment == Sentiment::Negative));
    }

    #[test]
    fn prophecy_has_evidence() {
        let graph = test_graph();
        let engine = EnhancedProphecyEngine::new(&graph);
        let prophecy = engine.prophecy(ProphecySubject::Node(0), ProphecyHorizon::MediumTerm);
        assert!(!prophecy.evidence.is_empty());
    }

    #[test]
    fn prophecy_compare_returns_pair() {
        let graph = test_graph();
        let engine = EnhancedProphecyEngine::new(&graph);
        let (a, b) = engine.prophecy_compare(
            ProphecySubject::Node(0),
            ProphecySubject::Module("mod".to_string()),
            ProphecyHorizon::LongTerm,
        );
        assert!(!a.predictions.is_empty());
        assert!(!b.predictions.is_empty());
    }
}