agent-source-repository 0.1.0

Agent Source Repository local context registry for coding agents
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
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};

use regex::Regex;

use crate::model::{Chunk, MatchLine, SearchResult};
use crate::ranking::penalties::file_path_penalty;

const EXACT_SCORE_BASE: f64 = 10_000.0;
const MAX_MATCH_LINES: usize = 8;
const REGEX_PREFIX: &str = "re:";

type Trigram = [u8; 3];

/// Byte-trigram exact search candidate index.
///
/// This index is deliberately only a candidate filter. It may produce false
/// positives, but every result is verified against chunk source text before it
/// is returned. The verifier is the only source of match truth.
pub(crate) struct ExactIndex {
    postings: HashMap<Trigram, Vec<usize>>,
}

impl ExactIndex {
    pub(crate) fn new(chunks: &[Chunk]) -> Self {
        let mut postings: HashMap<Trigram, Vec<usize>> = HashMap::new();

        for (idx, chunk) in chunks.iter().enumerate() {
            let mut seen = HashSet::new();
            for trigram in trigrams(chunk.content.as_bytes()) {
                if seen.insert(trigram) {
                    postings.entry(trigram).or_default().push(idx);
                }
            }
        }

        for list in postings.values_mut() {
            list.sort_unstable();
            list.dedup();
        }

        Self { postings }
    }

    pub(crate) fn search(
        &self,
        query: &str,
        chunks: &[Chunk],
        top_k: usize,
        selector: Option<&[usize]>,
    ) -> Vec<SearchResult> {
        let query = query.trim();
        if query.is_empty() || top_k == 0 {
            return Vec::new();
        }

        if let Some(pattern) = query.strip_prefix(REGEX_PREFIX) {
            return self.search_regex(pattern, chunks, top_k, selector);
        }

        self.search_literal(query, chunks, top_k, selector)
    }

    fn search_literal(
        &self,
        needle: &str,
        chunks: &[Chunk],
        top_k: usize,
        selector: Option<&[usize]>,
    ) -> Vec<SearchResult> {
        let required = unique_trigrams(needle.as_bytes());
        if required.is_empty() {
            return Vec::new();
        }

        let candidates = self.candidate_chunks(&required, selector);
        let mut results = Vec::new();

        for idx in candidates {
            let chunk = &chunks[idx];
            if !chunk.content.contains(needle) {
                continue;
            }
            let match_lines = literal_match_lines(chunk, needle);
            results.push(SearchResult {
                chunk: chunk.clone(),
                score: EXACT_SCORE_BASE + match_lines.len() as f64,
                match_lines,
            });
        }

        sort_and_limit(results, top_k)
    }

    fn search_regex(
        &self,
        pattern: &str,
        chunks: &[Chunk],
        top_k: usize,
        selector: Option<&[usize]>,
    ) -> Vec<SearchResult> {
        let literals = match mandatory_literals_for_safe_regex(pattern) {
            Some(literals) if !literals.is_empty() => literals,
            _ => return Vec::new(),
        };

        let mut required = Vec::new();
        for literal in &literals {
            required.extend(unique_trigrams(literal.as_bytes()));
        }
        required.sort_unstable();
        required.dedup();
        if required.is_empty() {
            return Vec::new();
        }

        let regex = match Regex::new(pattern) {
            Ok(regex) => regex,
            Err(_) => return Vec::new(),
        };

        let candidates = self.candidate_chunks(&required, selector);
        let mut results = Vec::new();

        for idx in candidates {
            let chunk = &chunks[idx];
            if !regex.is_match(&chunk.content) {
                continue;
            }
            let match_lines = regex_match_lines(chunk, &regex);
            results.push(SearchResult {
                chunk: chunk.clone(),
                score: EXACT_SCORE_BASE + 100.0 + match_lines.len() as f64,
                match_lines,
            });
        }

        sort_and_limit(results, top_k)
    }

    fn candidate_chunks(&self, required: &[Trigram], selector: Option<&[usize]>) -> Vec<usize> {
        let mut lists: Vec<&Vec<usize>> = Vec::new();
        for trigram in required {
            let Some(list) = self.postings.get(trigram) else {
                return Vec::new();
            };
            lists.push(list);
        }

        lists.sort_by_key(|list| list.len());
        let Some(first) = lists.first() else {
            return Vec::new();
        };

        let mut current = (*first).clone();
        for list in lists.iter().skip(1) {
            current = intersect_sorted(&current, list);
            if current.is_empty() {
                break;
            }
        }

        if let Some(selector) = selector {
            current = intersect_sorted(&current, selector);
        }

        current
    }
}

fn trigrams(bytes: &[u8]) -> impl Iterator<Item = Trigram> + '_ {
    bytes.windows(3).map(|w| [w[0], w[1], w[2]])
}

fn unique_trigrams(bytes: &[u8]) -> Vec<Trigram> {
    if bytes.len() < 3 {
        return Vec::new();
    }
    let mut values: Vec<Trigram> = trigrams(bytes).collect();
    values.sort_unstable();
    values.dedup();
    values
}

fn intersect_sorted(left: &[usize], right: &[usize]) -> Vec<usize> {
    let mut out = Vec::new();
    let mut i = 0;
    let mut j = 0;

    while i < left.len() && j < right.len() {
        match left[i].cmp(&right[j]) {
            Ordering::Less => i += 1,
            Ordering::Greater => j += 1,
            Ordering::Equal => {
                out.push(left[i]);
                i += 1;
                j += 1;
            }
        }
    }

    out
}

fn literal_match_lines(chunk: &Chunk, needle: &str) -> Vec<MatchLine> {
    let mut lines = Vec::new();

    for (offset, line) in chunk.content.lines().enumerate() {
        if line.contains(needle) {
            lines.push(MatchLine {
                line: chunk.start_line + offset,
                content: line.trim().to_string(),
            });
            if lines.len() >= MAX_MATCH_LINES {
                return lines;
            }
        }
    }

    if lines.is_empty() {
        if let Some(byte_idx) = chunk.content.find(needle) {
            lines.push(MatchLine {
                line: byte_to_line(&chunk.content, byte_idx, chunk.start_line),
                content: preview_at(&chunk.content, byte_idx),
            });
        }
    }

    lines
}

fn regex_match_lines(chunk: &Chunk, regex: &Regex) -> Vec<MatchLine> {
    let mut lines = Vec::new();

    for mat in regex.find_iter(&chunk.content) {
        lines.push(MatchLine {
            line: byte_to_line(&chunk.content, mat.start(), chunk.start_line),
            content: preview_at(&chunk.content, mat.start()),
        });
        if lines.len() >= MAX_MATCH_LINES {
            break;
        }
    }

    lines
}

fn byte_to_line(content: &str, byte_idx: usize, start_line: usize) -> usize {
    start_line + content[..byte_idx].bytes().filter(|&b| b == b'\n').count()
}

fn preview_at(content: &str, byte_idx: usize) -> String {
    let before = content[..byte_idx].rfind('\n').map_or(0, |idx| idx + 1);
    let after = content[byte_idx..]
        .find('\n')
        .map_or(content.len(), |idx| byte_idx + idx);
    content[before..after].trim().to_string()
}

/// Extract literals that are mandatory for a deliberately narrow regex subset.
///
/// The function returns None instead of guessing when regex syntax can make a
/// literal optional or alternative. This preserves the no-false-negative
/// invariant for indexed regex candidates.
fn mandatory_literals_for_safe_regex(pattern: &str) -> Option<Vec<String>> {
    if pattern.contains('|')
        || pattern.contains('?')
        || pattern.contains('*')
        || pattern.contains('{')
    {
        return None;
    }

    let mut literals = Vec::new();
    let mut current = String::new();
    let mut chars = pattern.chars().peekable();
    let mut in_class = false;

    while let Some(ch) = chars.next() {
        if in_class {
            if ch == ']' {
                in_class = false;
            }
            flush_literal(&mut literals, &mut current);
            continue;
        }

        match ch {
            '[' => {
                in_class = true;
                flush_literal(&mut literals, &mut current);
            }
            '\\' => match chars.next() {
                Some(escaped) if escaped.is_ascii_alphanumeric() => {
                    flush_literal(&mut literals, &mut current);
                }
                Some(escaped) => current.push(escaped),
                None => return None,
            },
            '.' | '^' | '$' | '(' | ')' | '+' => {
                flush_literal(&mut literals, &mut current);
            }
            _ => current.push(ch),
        }
    }

    if in_class {
        return None;
    }
    flush_literal(&mut literals, &mut current);

    literals.retain(|literal| literal.len() >= 3);
    literals.sort();
    literals.dedup();
    Some(literals)
}

fn flush_literal(literals: &mut Vec<String>, current: &mut String) {
    if current.len() >= 3 {
        literals.push(std::mem::take(current));
    } else {
        current.clear();
    }
}

fn sort_and_limit(mut results: Vec<SearchResult>, top_k: usize) -> Vec<SearchResult> {
    results.sort_by(|a, b| {
        exact_rank_score(b)
            .partial_cmp(&exact_rank_score(a))
            .unwrap_or(Ordering::Equal)
            .then_with(|| b.score.partial_cmp(&a.score).unwrap_or(Ordering::Equal))
            .then_with(|| a.chunk.file_path.cmp(&b.chunk.file_path))
            .then_with(|| a.chunk.start_line.cmp(&b.chunk.start_line))
            .then_with(|| a.chunk.end_line.cmp(&b.chunk.end_line))
    });
    results.truncate(top_k);
    results
}

fn exact_rank_score(result: &SearchResult) -> f64 {
    result.score * file_path_penalty(&result.chunk.file_path)
}

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

    fn chunk(path: &str, content: &str) -> Chunk {
        Chunk::new(
            content.to_string(),
            path.to_string(),
            1,
            content.lines().count(),
            Some("rust".to_string()),
        )
    }

    #[test]
    fn literal_search_verifies_candidates() {
        let chunks = vec![
            chunk("src/retry.rs", "pub fn retry_backoff() -> u64 { 100 }\n"),
            chunk("src/other.rs", "pub fn retry_then_wait() {}\n"),
        ];
        let index = ExactIndex::new(&chunks);

        let results = index.search("retry_backoff", &chunks, 5, None);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].chunk.file_path, "src/retry.rs");
        assert!(results[0].match_lines[0].content.contains("retry_backoff"));
    }

    #[test]
    fn safe_regex_uses_verified_match_truth() {
        let chunks = vec![chunk(
            "src/retry.rs",
            "pub fn retry_backoff() -> u64 { 100 }\n",
        )];
        let index = ExactIndex::new(&chunks);

        let results = index.search("re:retry_\\w+", &chunks, 5, None);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].chunk.file_path, "src/retry.rs");
    }

    #[test]
    fn exact_symbol_search_prefers_implementation_over_script_fixture() {
        let chunks = vec![
            chunk(
                "scripts/fixture.sh",
                "cat > main.rs <<'EOF'\nfn calculate_invoice_00() {}\nEOF\n",
            ),
            chunk("src/feature.rs", "pub fn calculate_invoice_00() {}\n"),
            chunk(
                "tests/feature_test.rs",
                "#[test]\nfn duplicate_calculate_invoice_00_test() {}\n",
            ),
        ];
        let index = ExactIndex::new(&chunks);

        let results = index.search("calculate_invoice_00", &chunks, 3, None);

        assert_eq!(results[0].chunk.file_path, "src/feature.rs");
    }

    #[test]
    fn broad_or_ambiguous_regex_does_not_guess() {
        let chunks = vec![chunk(
            "src/retry.rs",
            "pub fn retry_backoff() -> u64 { 100 }\n",
        )];
        let index = ExactIndex::new(&chunks);

        let results = index.search("re:retry|backoff", &chunks, 5, None);

        assert!(results.is_empty());
    }
}