context-footprint 0.1.0

A static analysis tool for measuring architectural context exposure in codebases.
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
use crate::domain::edge::EdgeKind;
use crate::domain::graph::ContextGraph;
use crate::domain::node::Node;
use crate::domain::policy::{PruningDecision, PruningPolicy};
use petgraph::visit::EdgeRef;

/// Academic baseline pruning policy
/// Uses type completeness + documentation presence check
pub struct AcademicBaseline {
    doc_threshold: f32,
}

impl Default for AcademicBaseline {
    fn default() -> Self {
        Self::new(0.5)
    }
}

impl AcademicBaseline {
    pub fn new(doc_threshold: f32) -> Self {
        Self { doc_threshold }
    }

    /// Check if a function is an "abstract factory" - returns an abstract type
    /// This identifies the Abstract Factory design pattern where a function/method
    /// returns an interface/protocol, hiding concrete implementation details
    fn is_abstract_factory(&self, function_node: &Node, graph: &ContextGraph) -> bool {
        // Find the NodeIndex for this function node by comparing node IDs
        let func_node_id = function_node.core().id;
        let mut func_idx = None;
        for &idx in graph.symbol_to_node.values() {
            if graph.node(idx).core().id == func_node_id {
                func_idx = Some(idx);
                break;
            }
        }

        if let Some(idx) = func_idx {
            // Check all outgoing edges from this function
            for edge_ref in graph.graph.edges(idx) {
                if matches!(edge_ref.weight(), EdgeKind::ReturnType) {
                    let target_idx = edge_ref.target();
                    let target_node = graph.node(target_idx);

                    // If return type is an abstract type (Protocol/Interface/Trait)
                    // with sufficient documentation, this is an abstract factory
                    if matches!(target_node, Node::Type(t) if t.is_abstract && t.core.doc_score >= self.doc_threshold)
                    {
                        return true;
                    }
                }
            }
        }
        false
    }
}

impl PruningPolicy for AcademicBaseline {
    fn evaluate(
        &self,
        source: &Node,
        target: &Node,
        edge_kind: &EdgeKind,
        graph: &ContextGraph,
    ) -> PruningDecision {
        // Special handling for dynamic expansion edges
        match edge_kind {
            EdgeKind::SharedStateWrite => {
                // Shared-state write edges always traverse (no boundary stops them)
                return PruningDecision::Transparent;
            }
            EdgeKind::CallIn => {
                // Call-in edges traverse only when source lacks complete specification
                if let Node::Function(f) = source {
                    let sig_complete = f.typed_param_count == f.param_count && f.has_return_type;
                    if sig_complete && f.core.doc_score >= self.doc_threshold {
                        return PruningDecision::Boundary;
                    } else {
                        return PruningDecision::Transparent;
                    }
                }
                // If not a function, something is weird, but let's be conservative
                return PruningDecision::Transparent;
            }
            _ => {}
        }

        // External dependencies are always boundaries
        if target.core().is_external {
            return PruningDecision::Boundary;
        }

        match target {
            Node::Type(t) => {
                // Type boundary: must be abstract (interface/protocol) and well-documented
                if t.is_abstract && t.core.doc_score >= self.doc_threshold {
                    PruningDecision::Boundary
                } else {
                    PruningDecision::Transparent
                }
            }
            Node::Function(f) => {
                // Check if this function is an "abstract factory"
                // (returns an abstract type, following the Abstract Factory pattern)
                if self.is_abstract_factory(target, graph) {
                    // Abstract factories are boundaries - we care about the interface they return,
                    // not the concrete implementation details inside
                    return PruningDecision::Boundary;
                }

                // Function boundary: signature complete and well-documented
                let sig_complete = f.typed_param_count == f.param_count && f.has_return_type;
                if sig_complete && f.core.doc_score >= self.doc_threshold {
                    PruningDecision::Boundary
                } else {
                    PruningDecision::Transparent
                }
            }
            Node::Variable(_) => {
                // Variables are always transparent (need to see type definition)
                PruningDecision::Transparent
            }
        }
    }

    fn doc_threshold(&self) -> f32 {
        self.doc_threshold
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::edge::EdgeKind;
    use crate::domain::graph::ContextGraph;
    use crate::domain::node::{
        FunctionNode, Mutability, Node, NodeCore, SourceSpan, TypeKind, TypeNode, VariableKind,
        VariableNode, Visibility,
    };

    fn make_core(id: u32, name: &str, doc_score: f32, is_external: bool) -> NodeCore {
        NodeCore::new(
            id,
            name.to_string(),
            None,
            10,
            SourceSpan {
                start_line: 0,
                start_column: 0,
                end_line: 1,
                end_column: 5,
            },
            doc_score,
            is_external,
            "test.py".to_string(),
        )
    }

    fn well_documented_function() -> Node {
        let core = make_core(0, "f", 0.8, false);
        Node::Function(FunctionNode {
            core,
            param_count: 2,
            typed_param_count: 2,
            has_return_type: true,
            is_async: false,
            is_generator: false,
            visibility: Visibility::Public,
        })
    }

    fn poorly_documented_function() -> Node {
        let core = make_core(0, "g", 0.0, false);
        Node::Function(FunctionNode {
            core,
            param_count: 1,
            typed_param_count: 0,
            has_return_type: false,
            is_async: false,
            is_generator: false,
            visibility: Visibility::Public,
        })
    }

    fn abstract_type_with_doc() -> Node {
        let core = make_core(0, "I", 0.6, false);
        Node::Type(TypeNode {
            core,
            type_kind: TypeKind::Interface,
            is_abstract: true,
            type_param_count: 0,
        })
    }

    fn concrete_type() -> Node {
        let core = make_core(0, "C", 0.9, false);
        Node::Type(TypeNode {
            core,
            type_kind: TypeKind::Class,
            is_abstract: false,
            type_param_count: 0,
        })
    }

    fn variable_node() -> Node {
        let core = make_core(0, "v", 1.0, false);
        Node::Variable(VariableNode {
            core,
            has_type_annotation: true,
            mutability: Mutability::Immutable,
            variable_kind: VariableKind::Global,
        })
    }

    fn external_node() -> Node {
        let core = make_core(0, "ext", 0.0, true);
        Node::Function(FunctionNode {
            core,
            param_count: 0,
            typed_param_count: 0,
            has_return_type: false,
            is_async: false,
            is_generator: false,
            visibility: Visibility::Public,
        })
    }

    fn empty_graph() -> ContextGraph {
        ContextGraph::new()
    }

    #[test]
    fn test_external_node_is_boundary() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = external_node();
        let source = well_documented_function();
        let edge = EdgeKind::Call;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Boundary));
    }

    #[test]
    fn test_well_documented_function_is_boundary() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = well_documented_function();
        let source = poorly_documented_function();
        let edge = EdgeKind::Call;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Boundary));
    }

    #[test]
    fn test_poorly_documented_function_is_transparent() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = poorly_documented_function();
        let source = well_documented_function();
        let edge = EdgeKind::Call;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Transparent));
    }

    #[test]
    fn test_abstract_type_with_doc_is_boundary() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = abstract_type_with_doc();
        let source = well_documented_function();
        let edge = EdgeKind::ParamType;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Boundary));
    }

    #[test]
    fn test_concrete_type_is_transparent() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = concrete_type();
        let source = well_documented_function();
        let edge = EdgeKind::ParamType;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Transparent));
    }

    #[test]
    fn test_variable_always_transparent() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let target = variable_node();
        let source = well_documented_function();
        let edge = EdgeKind::Read;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Transparent));
    }

    #[test]
    fn test_shared_state_write_always_transparent() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let source = well_documented_function();
        let target = well_documented_function();
        let edge = EdgeKind::SharedStateWrite;
        let d = policy.evaluate(&source, &target, &edge, &graph);
        assert!(matches!(d, PruningDecision::Transparent));
    }

    #[test]
    fn test_call_in_depends_on_signature_completeness() {
        let policy = AcademicBaseline::default();
        let graph = empty_graph();
        let edge = EdgeKind::CallIn;
        let well = well_documented_function();
        let poor = poorly_documented_function();
        let d_well = policy.evaluate(&well, &poor, &edge, &graph);
        let d_poor = policy.evaluate(&poor, &well, &edge, &graph);
        assert!(matches!(d_well, PruningDecision::Boundary));
        assert!(matches!(d_poor, PruningDecision::Transparent));
    }

    #[test]
    fn test_doc_threshold_below_returns_transparent() {
        let policy = AcademicBaseline::new(0.9);
        let mut target = well_documented_function();
        target.core_mut().doc_score = 0.6;
        let graph = empty_graph();
        let source = poorly_documented_function();
        let d = policy.evaluate(&source, &target, &EdgeKind::Call, &graph);
        assert!(matches!(d, PruningDecision::Transparent));
    }

    #[test]
    fn test_abstract_factory_is_boundary() {
        let policy = AcademicBaseline::default();

        // Create a factory function that returns an abstract type
        let factory_core = make_core(0, "get_service", 0.8, false);
        let factory = Node::Function(FunctionNode {
            core: factory_core,
            param_count: 0,
            typed_param_count: 0,
            has_return_type: true,
            is_async: false,
            is_generator: false,
            visibility: Visibility::Public,
        });

        // Create an abstract protocol it returns
        let protocol_core = make_core(1, "ServicePort", 0.9, false);
        let protocol = Node::Type(TypeNode {
            core: protocol_core,
            type_kind: TypeKind::Interface,
            is_abstract: true,
            type_param_count: 0,
        });

        // Build graph with ReturnType edge from factory to protocol
        let mut graph = ContextGraph::new();
        let factory_idx = graph.add_node("get_service".to_string(), factory.clone());
        let protocol_idx = graph.add_node("ServicePort".to_string(), protocol.clone());
        graph.add_edge(factory_idx, protocol_idx, EdgeKind::ReturnType);

        // Test: factory function should be recognized as boundary (abstract factory pattern)
        let caller = poorly_documented_function();
        let d = policy.evaluate(&caller, &factory, &EdgeKind::Call, &graph);
        assert!(matches!(d, PruningDecision::Boundary));
    }

    #[test]
    fn test_poorly_documented_factory_returning_abstract_becomes_boundary() {
        let policy = AcademicBaseline::default();

        // Create a factory function with incomplete docs that returns abstract type
        // Normally this would be Transparent, but abstract factory rule makes it Boundary
        let factory_core = make_core(0, "get_service", 0.3, false); // Low doc score
        let factory = Node::Function(FunctionNode {
            core: factory_core,
            param_count: 1,
            typed_param_count: 0, // Incomplete signature
            has_return_type: true,
            is_async: false,
            is_generator: false,
            visibility: Visibility::Public,
        });

        // Create an abstract protocol it returns
        let protocol_core = make_core(1, "ServicePort", 0.9, false);
        let protocol = Node::Type(TypeNode {
            core: protocol_core,
            type_kind: TypeKind::Interface,
            is_abstract: true,
            type_param_count: 0,
        });

        // Build graph with ReturnType edge
        let mut graph = ContextGraph::new();
        let factory_idx = graph.add_node("get_service".to_string(), factory.clone());
        let protocol_idx = graph.add_node("ServicePort".to_string(), protocol.clone());
        graph.add_edge(factory_idx, protocol_idx, EdgeKind::ReturnType);

        // Test: Even though factory has poor docs and incomplete signature,
        // it becomes Boundary because it returns an abstract type (abstract factory pattern)
        let caller = well_documented_function();
        let d = policy.evaluate(&caller, &factory, &EdgeKind::Call, &graph);
        assert!(matches!(d, PruningDecision::Boundary));
    }
}