sem-core 0.8.0

Entity-level semantic diff engine. Extracts functions, classes, and methods from 28 languages via tree-sitter and diffs at the entity level.
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
//! Context budgeting: pack optimal entity context into a token budget.
//! Priority: target entity > direct dependencies > direct dependents > transitive dependencies >
//! transitive dependents.

use std::collections::{HashMap, HashSet, VecDeque};

use crate::model::entity::SemanticEntity;
use crate::parser::graph::EntityGraph;

#[derive(Debug, Clone)]
pub struct ContextEntry {
    pub entity_id: String,
    pub entity_name: String,
    pub entity_type: String,
    pub file_path: String,
    pub role: String,
    pub content: String,
    pub estimated_tokens: usize,
}

#[derive(Debug, Clone, Default)]
pub struct ContextResult {
    pub entries: Vec<ContextEntry>,
    pub total_tokens: usize,
    pub truncated: bool,
    pub target_omitted: bool,
}

/// Estimate token count from content. Rough heuristic: ~1.3 tokens per whitespace-separated word.
fn estimate_tokens(content: &str) -> usize {
    let words = content.split_whitespace().count();
    words * 13 / 10
}

/// Extract just the first line (signature) of an entity's content.
fn signature_only(content: &str) -> String {
    content.lines().next().unwrap_or("").to_string()
}

/// Build a context set for a target entity within a token budget.
///
/// Greedy knapsack by priority:
/// 1. Target entity (full content)
/// 2. Direct dependencies (full content, signature fallback)
/// 3. Direct dependents (full content, signature fallback)
/// 4. Transitive dependencies (signature only)
/// 5. Transitive dependents (signature only)
pub fn build_context(
    graph: &EntityGraph,
    entity_id: &str,
    all_entities: &[SemanticEntity],
    token_budget: usize,
) -> Vec<ContextEntry> {
    build_context_result(graph, entity_id, all_entities, token_budget).entries
}

/// Build a context set plus budget metadata for a target entity.
pub fn build_context_result(
    graph: &EntityGraph,
    entity_id: &str,
    all_entities: &[SemanticEntity],
    token_budget: usize,
) -> ContextResult {
    // Build content lookup: entity_id -> SemanticEntity
    let entity_lookup: HashMap<&str, &SemanticEntity> =
        all_entities.iter().map(|e| (e.id.as_str(), e)).collect();

    let mut result = ContextResult::default();
    let mut included_ids = HashSet::new();

    // 1. Target entity. Keep the budget strict: if even the signature does not fit,
    // omit the target and return an empty result instead of overspending.
    if let Some(entity) = entity_lookup.get(entity_id) {
        let full_tokens = estimate_tokens(&entity.content);
        if full_tokens <= token_budget {
            push_entry(
                &mut result,
                entity,
                "target",
                entity.content.clone(),
                full_tokens,
                &mut included_ids,
            );
        } else {
            result.truncated = true;
            let sig = signature_only(&entity.content);
            let sig_tokens = estimate_tokens(&sig);
            if sig_tokens <= token_budget {
                push_entry(
                    &mut result,
                    entity,
                    "target",
                    sig,
                    sig_tokens,
                    &mut included_ids,
                );
            } else {
                // Strict context budget contract: no related entries are useful if the
                // requested target cannot be represented inside the budget.
                result.target_omitted = true;
                return result;
            }
        };
    }

    let direct_dependencies = graph.get_dependencies(entity_id);
    for dep_info in &direct_dependencies {
        add_full_or_signature(
            &mut result,
            &entity_lookup,
            dep_info.id.as_str(),
            "direct_dependency",
            token_budget,
            &mut included_ids,
        );
    }

    let direct_dependents = graph.get_dependents(entity_id);
    for dep_info in &direct_dependents {
        add_full_or_signature(
            &mut result,
            &entity_lookup,
            dep_info.id.as_str(),
            "direct_dependent",
            token_budget,
            &mut included_ids,
        );
    }

    let direct_dependency_ids: HashSet<&str> =
        direct_dependencies.iter().map(|d| d.id.as_str()).collect();
    let direct_dependent_ids: HashSet<&str> =
        direct_dependents.iter().map(|d| d.id.as_str()).collect();

    for dep_info in collect_reachable_related(graph, entity_id, &graph.dependencies) {
        if direct_dependency_ids.contains(dep_info.id.as_str()) {
            continue;
        }
        add_signature(
            &mut result,
            &entity_lookup,
            dep_info.id.as_str(),
            "transitive_dependency",
            token_budget,
            &mut included_ids,
        );
    }

    for dep_info in collect_reachable_related(graph, entity_id, &graph.dependents) {
        if direct_dependent_ids.contains(dep_info.id.as_str()) {
            continue;
        }
        add_signature(
            &mut result,
            &entity_lookup,
            dep_info.id.as_str(),
            "transitive_dependent",
            token_budget,
            &mut included_ids,
        );
    }

    result
}

fn push_entry(
    result: &mut ContextResult,
    entity: &SemanticEntity,
    role: &str,
    content: String,
    tokens: usize,
    included_ids: &mut HashSet<String>,
) {
    result.entries.push(ContextEntry {
        entity_id: entity.id.clone(),
        entity_name: entity.name.clone(),
        entity_type: entity.entity_type.clone(),
        file_path: entity.file_path.clone(),
        role: role.to_string(),
        content,
        estimated_tokens: tokens,
    });
    result.total_tokens += tokens;
    included_ids.insert(entity.id.clone());
}

fn add_full_or_signature(
    result: &mut ContextResult,
    entity_lookup: &HashMap<&str, &SemanticEntity>,
    entity_id: &str,
    role: &str,
    token_budget: usize,
    included_ids: &mut HashSet<String>,
) {
    if included_ids.contains(entity_id) {
        return;
    }

    let Some(entity) = entity_lookup.get(entity_id) else {
        return;
    };

    let full_tokens = estimate_tokens(&entity.content);
    if result.total_tokens + full_tokens <= token_budget {
        push_entry(
            result,
            entity,
            role,
            entity.content.clone(),
            full_tokens,
            included_ids,
        );
        return;
    }

    result.truncated = true;
    add_signature(
        result,
        entity_lookup,
        entity_id,
        role,
        token_budget,
        included_ids,
    );
}

fn add_signature(
    result: &mut ContextResult,
    entity_lookup: &HashMap<&str, &SemanticEntity>,
    entity_id: &str,
    role: &str,
    token_budget: usize,
    included_ids: &mut HashSet<String>,
) {
    if included_ids.contains(entity_id) {
        return;
    }

    let Some(entity) = entity_lookup.get(entity_id) else {
        return;
    };

    let sig = signature_only(&entity.content);
    let tokens = estimate_tokens(&sig);
    if result.total_tokens + tokens <= token_budget {
        push_entry(result, entity, role, sig, tokens, included_ids);
    } else {
        result.truncated = true;
    }
}

/// Collect related entities reachable from `entity_id`, excluding the starting entity.
fn collect_reachable_related<'a>(
    graph: &'a EntityGraph,
    entity_id: &str,
    relationships: &'a HashMap<String, Vec<String>>,
) -> Vec<&'a crate::parser::graph::EntityInfo> {
    const MAX_VISITED: usize = 10_000;

    let mut visited: HashSet<&str> = HashSet::new();
    let mut queue: VecDeque<&str> = VecDeque::new();
    let mut result = Vec::new();

    let start_key = match graph.entities.get_key_value(entity_id) {
        Some((key, _)) => key.as_str(),
        None => return result,
    };

    queue.push_back(start_key);
    visited.insert(start_key);

    while let Some(current) = queue.pop_front() {
        if result.len() >= MAX_VISITED {
            break;
        }

        if let Some(next_ids) = relationships.get(current) {
            for next_id in next_ids {
                if visited.insert(next_id.as_str()) {
                    if let Some(info) = graph.entities.get(next_id.as_str()) {
                        result.push(info);
                        if result.len() >= MAX_VISITED {
                            return result;
                        }
                    }
                    queue.push_back(next_id.as_str());
                }
            }
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::graph::{EntityGraph, EntityInfo, EntityRef, RefType};
    use std::collections::HashMap;

    #[test]
    fn test_estimate_tokens() {
        assert_eq!(estimate_tokens("hello world"), 2); // 2 * 13 / 10 = 2
        assert_eq!(estimate_tokens("fn foo(a: i32, b: i32) -> bool {"), 10); // 8 words * 13 / 10 = 10
    }

    #[test]
    fn test_signature_only() {
        assert_eq!(
            signature_only("fn foo(a: i32) {\n    a + 1\n}"),
            "fn foo(a: i32) {"
        );
    }

    #[test]
    fn test_target_omitted_when_signature_exceeds_budget() {
        let entities = vec![entity(
            "a.py::function::helper_b",
            "helper_b",
            "def helper_b():\n    return 1",
        )];
        let graph = graph_from_entities(&entities, vec![]);

        let result = build_context_result(&graph, "a.py::function::helper_b", &entities, 1);

        assert!(result.entries.is_empty());
        assert_eq!(result.total_tokens, 0);
        assert!(result.truncated);
        assert!(result.target_omitted);
    }

    #[test]
    fn test_target_signature_respects_budget() {
        let entities = vec![entity(
            "a.py::function::helper_b",
            "helper_b",
            "def helper_b():\n    return expensive_value()",
        )];
        let graph = graph_from_entities(&entities, vec![]);

        let result = build_context_result(&graph, "a.py::function::helper_b", &entities, 2);

        assert_eq!(result.total_tokens, 2);
        assert!(result.truncated);
        assert!(!result.target_omitted);
        assert_eq!(result.entries.len(), 1);
        assert_eq!(result.entries[0].role, "target");
        assert_eq!(result.entries[0].content, "def helper_b():");
    }

    #[test]
    fn test_context_includes_dependencies_before_dependents() {
        let entities = vec![
            entity(
                "a.py::function::main",
                "main",
                "def main():\n    return helper_a() + helper_b()",
            ),
            entity(
                "a.py::function::helper_a",
                "helper_a",
                "def helper_a():\n    return leaf()",
            ),
            entity(
                "a.py::function::helper_b",
                "helper_b",
                "def helper_b():\n    return 2",
            ),
            entity("a.py::function::leaf", "leaf", "def leaf():\n    return 1"),
            entity(
                "a.py::class::Caller",
                "Caller",
                "class Caller:\n    def go(self):\n        return main()",
            ),
            entity(
                "a.py::class::Outer",
                "Outer",
                "class Outer:\n    def go(self):\n        return Caller().go()",
            ),
        ];
        let graph = graph_from_entities(
            &entities,
            vec![
                edge("a.py::function::main", "a.py::function::helper_a"),
                edge("a.py::function::main", "a.py::function::helper_b"),
                edge("a.py::function::helper_a", "a.py::function::leaf"),
                edge("a.py::class::Caller", "a.py::function::main"),
                edge("a.py::class::Outer", "a.py::class::Caller"),
            ],
        );

        let result = build_context_result(&graph, "a.py::function::main", &entities, 999);
        let roles_and_names: Vec<(&str, &str)> = result
            .entries
            .iter()
            .map(|entry| (entry.role.as_str(), entry.entity_name.as_str()))
            .collect();

        assert_eq!(
            roles_and_names,
            vec![
                ("target", "main"),
                ("direct_dependency", "helper_a"),
                ("direct_dependency", "helper_b"),
                ("direct_dependent", "Caller"),
                ("transitive_dependency", "leaf"),
                ("transitive_dependent", "Outer"),
            ]
        );
        assert!(!result.truncated);
        assert!(!result.target_omitted);
        assert!(result.total_tokens <= 999);
    }

    #[test]
    fn test_collect_transitive_caps_results() {
        let mut entities = Vec::new();
        let mut edges = Vec::new();

        for index in 0..=10_001 {
            let id = format!("a.py::function::helper_{index}");
            entities.push(entity(
                &id,
                &format!("helper_{index}"),
                "def helper():\n    return 1",
            ));
            if index > 0 {
                edges.push(edge(&format!("a.py::function::helper_{}", index - 1), &id));
            }
        }

        let graph = graph_from_entities(&entities, edges);
        let result = collect_reachable_related(
            &graph,
            "a.py::function::helper_0",
            &graph.dependencies,
        );

        assert_eq!(result.len(), 10_000);
    }

    fn entity(id: &str, name: &str, content: &str) -> SemanticEntity {
        SemanticEntity {
            id: id.to_string(),
            file_path: "a.py".to_string(),
            entity_type: id.split("::").nth(1).unwrap_or("function").to_string(),
            name: name.to_string(),
            parent_id: None,
            content: content.to_string(),
            content_hash: String::new(),
            structural_hash: None,
            start_line: 1,
            end_line: content.lines().count(),
            metadata: None,
        }
    }

    fn edge(from_entity: &str, to_entity: &str) -> EntityRef {
        EntityRef {
            from_entity: from_entity.to_string(),
            to_entity: to_entity.to_string(),
            ref_type: RefType::Calls,
        }
    }

    fn graph_from_entities(entities: &[SemanticEntity], edges: Vec<EntityRef>) -> EntityGraph {
        let entity_infos: HashMap<String, EntityInfo> = entities
            .iter()
            .map(|entity| {
                (
                    entity.id.clone(),
                    EntityInfo {
                        id: entity.id.clone(),
                        name: entity.name.clone(),
                        entity_type: entity.entity_type.clone(),
                        file_path: entity.file_path.clone(),
                        parent_id: entity.parent_id.clone(),
                        start_line: entity.start_line,
                        end_line: entity.end_line,
                    },
                )
            })
            .collect();

        EntityGraph::from_parts(entity_infos, edges)
    }
}