greplm-core 0.6.0

Core indexing and search engine for greplm: a trigram code index for LLM 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
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
//! Task-driven context packs.
//!
//! Given a natural-language task and a token budget, greplm assembles the most
//! relevant slice of the codebase an agent needs to act — ranked by lexical
//! relevance, call-graph centrality, and (when built with the `semantic`
//! feature) meaning — and packs it to fit the budget. This is the "give me
//! exactly the code for this task" surface that keeps agents off the
//! grep-then-read-whole-files treadmill.
//!
//! The ranking helpers here are pure; [`crate::search::Searcher::context_pack`]
//! drives them over the index.

use serde::{Deserialize, Serialize};

/// One unit of packed context: a symbol, its signature, and a code snippet.
///
/// The snippet body is a single `code` blob (lines joined by `\n`) beginning at
/// `snippet_start`, rather than an array of per-line `{line, text}` objects.
/// Line numbers are implicit (`snippet_start + i`), so the field names and line
/// numbers are not repeated on the wire — the dominant cost in a packed bundle.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackItem {
    pub path: String,
    pub lang: String,
    pub name: String,
    pub kind: String,
    pub line_start: u32,
    pub line_end: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
    /// First line number of `code` (usually equals `line_start`).
    pub snippet_start: u32,
    /// Snippet body: the symbol's lines joined by `\n`, possibly truncated.
    pub code: String,
    /// Why this item was included (e.g. "match", "central", "callee of X").
    pub reason: String,
    pub score: f32,
}

/// A budget-bounded bundle of context for a task.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextPack {
    pub task: String,
    pub budget_tokens: u64,
    pub used_tokens: u64,
    /// True if relevant items were dropped to stay within budget.
    pub truncated: bool,
    pub items: Vec<PackItem>,
}

/// Conservative chars-per-token estimate (matches the savings accounting).
pub const CHARS_PER_TOKEN: u64 = 4;

/// Estimate the token cost of a string.
pub fn est_tokens(chars: u64) -> u64 {
    chars / CHARS_PER_TOKEN
}

/// Tokenize a free-form task into lowercased search terms: split on
/// non-identifier characters, then split camelCase/snake_case, dropping
/// stopwords and 1-character noise.
pub fn tokenize(task: &str) -> Vec<String> {
    let mut terms: Vec<String> = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for raw in task.split(|c: char| !(c.is_alphanumeric() || c == '_')) {
        if raw.is_empty() {
            continue;
        }
        for tok in split_identifier(raw) {
            if tok.len() < 2 || is_stopword(&tok) {
                continue;
            }
            if seen.insert(tok.clone()) {
                terms.push(tok);
            }
        }
    }
    terms
}

fn is_stopword(t: &str) -> bool {
    matches!(
        t,
        "the"
            | "a"
            | "an"
            | "of"
            | "to"
            | "in"
            | "is"
            | "for"
            | "and"
            | "or"
            | "how"
            | "where"
            | "what"
            | "does"
            | "do"
            | "with"
            | "on"
            | "by"
            | "this"
            | "that"
            | "it"
            | "be"
            | "as"
            | "at"
            | "we"
            | "i"
            | "add"
            | "fix"
            | "use"
            | "using"
            | "make"
            | "get"
            | "set"
            | "all"
            | "when"
            | "from"
            | "into"
            | "via"
            | "can"
            | "should"
            | "code"
            | "function"
            | "method"
    )
}

/// Lexical relevance of a symbol to the task terms.
///
/// Convenience wrapper for one-off scoring. The full-repo scan in
/// [`crate::search::Searcher::context_pack`] instead uses
/// [`path_term_bonus`] + [`lexical_score_with`], which hoist the per-document
/// work out of the per-symbol loop and reuse their scratch buffers.
pub fn lexical_score(
    name: &str,
    kind: &str,
    signature: Option<&str>,
    container: Option<&str>,
    path: &str,
    terms: &[String],
) -> f32 {
    if terms.is_empty() {
        return 0.0;
    }
    let mut scratch = ScoreScratch::default();
    let path_bonus = path_term_bonus(path, terms, &mut scratch);
    lexical_score_with(
        name,
        kind,
        signature,
        container,
        path_bonus,
        terms,
        &mut scratch,
    )
}

/// Reusable buffers for [`lexical_score_with`].
///
/// Scoring one symbol needs a lowercased name, a lowercased signature, and the
/// identifier tokens of the name and container. Allocating those per symbol
/// dominated `context_pack` on large trees (one scan touches every symbol in
/// the repository), so the buffers are hoisted into a struct the caller keeps
/// across the whole scan. The values computed are identical to the naive
/// version; only the allocations are amortized.
///
/// The path and name buffers are separate on purpose. Sharing one would work
/// today — [`path_term_bonus`] returns its result before a name overwrites the
/// buffer — but it would break silently if a caller ever interleaved the two.
#[derive(Default)]
pub struct ScoreScratch {
    path_lower: String,
    name_lower: String,
    sig_lower: String,
    name_tokens: Vec<String>,
    cont_tokens: Vec<String>,
}

/// Overwrite `buf` with the ASCII-lowercased form of `s`, reusing its capacity.
fn ascii_lower_into(s: &str, buf: &mut String) {
    buf.clear();
    buf.push_str(s);
    buf.make_ascii_lowercase();
}

/// The path component of a symbol's lexical score: `2.0` per task term that
/// appears in the path. This depends only on the document, so `context_pack`
/// computes it once per file rather than once per symbol.
pub fn path_term_bonus(path: &str, terms: &[String], scratch: &mut ScoreScratch) -> f32 {
    ascii_lower_into(path, &mut scratch.path_lower);
    let mut bonus = 0.0f32;
    for term in terms {
        if scratch.path_lower.contains(term.as_str()) {
            bonus += 2.0;
        }
    }
    bonus
}

/// Lexical relevance of a symbol, given its document's precomputed
/// [`path_term_bonus`]. Allocation-free once `scratch`'s buffers are warm.
pub fn lexical_score_with(
    name: &str,
    kind: &str,
    signature: Option<&str>,
    container: Option<&str>,
    path_bonus: f32,
    terms: &[String],
    scratch: &mut ScoreScratch,
) -> f32 {
    if terms.is_empty() {
        return 0.0;
    }
    ascii_lower_into(name, &mut scratch.name_lower);
    let n_name = split_identifier_into(name, &mut scratch.name_tokens);
    match signature {
        Some(s) => ascii_lower_into(s, &mut scratch.sig_lower),
        None => scratch.sig_lower.clear(),
    }
    let n_cont = match container {
        Some(c) => split_identifier_into(c, &mut scratch.cont_tokens),
        None => 0,
    };

    let name_lower = &scratch.name_lower;
    let mut score = path_bonus;
    for term in terms {
        if name_lower == term {
            score += 20.0;
        } else if scratch.name_tokens[..n_name].iter().any(|t| t == term) {
            score += 12.0;
        } else if name_lower.contains(term.as_str()) {
            score += 6.0;
        }
        if scratch.cont_tokens[..n_cont].iter().any(|t| t == term) {
            score += 4.0;
        }
        if signature.is_some() && scratch.sig_lower.contains(term.as_str()) {
            score += 3.0;
        }
    }
    if score > 0.0 && is_priority_kind(kind) {
        score += 2.0;
    }
    score
}

fn is_priority_kind(kind: &str) -> bool {
    matches!(
        kind,
        "function"
            | "method"
            | "struct"
            | "class"
            | "trait"
            | "interface"
            | "enum"
            | "type"
            | "constructor"
            | "module"
    )
}

/// Split an identifier into lowercase tokens on camelCase and snake/kebab.
pub fn split_identifier(s: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let n = split_identifier_into(s, &mut tokens);
    tokens.truncate(n);
    tokens
}

/// [`split_identifier`] into a caller-owned buffer: writes the tokens to
/// `out[..n]` and returns `n`, reusing the `String` allocations already there.
///
/// `out` may come back longer than `n` (stale tokens from a previous call sit
/// past the end); only the returned prefix is valid. This is what lets the
/// per-symbol scan paths run without allocating.
fn split_identifier_into(s: &str, out: &mut Vec<String>) -> usize {
    let mut n = 0usize;
    let mut prev_lower = false;
    // Whether a token is currently being built at `out[n]`. Mirrors the
    // `!cur.is_empty()` guards of the allocating version: a token becomes
    // non-empty as soon as its first character is pushed.
    let mut open = false;
    for ch in s.chars() {
        if ch == '_' || ch == '-' || ch == ' ' {
            if open {
                n += 1;
                open = false;
            }
            prev_lower = false;
            continue;
        }
        if ch.is_uppercase() && prev_lower && open {
            n += 1;
            open = false;
        }
        if !open {
            if out.len() == n {
                out.push(String::new());
            } else {
                out[n].clear();
            }
            open = true;
        }
        out[n].extend(ch.to_lowercase());
        prev_lower = ch.is_lowercase() || ch.is_numeric();
    }
    if open {
        n += 1;
    }
    n
}

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

    #[test]
    fn tokenizes_and_drops_stopwords() {
        let t = tokenize("How does the SegmentWriter flush to disk?");
        assert!(t.contains(&"segment".to_string()));
        assert!(t.contains(&"writer".to_string()));
        assert!(t.contains(&"flush".to_string()));
        assert!(t.contains(&"disk".to_string()));
        assert!(!t.contains(&"the".to_string()));
        assert!(!t.contains(&"how".to_string()));
    }

    #[test]
    fn scores_name_matches_highest() {
        let terms = tokenize("flush segment writer");
        let exact = lexical_score("flush", "function", None, None, "src/a.rs", &terms);
        let unrelated = lexical_score("zebra", "function", None, None, "src/a.rs", &terms);
        assert!(exact > unrelated);
        assert_eq!(unrelated, 0.0);
    }

    /// Inputs exercising every branch of the tokenizer: separators, camelCase
    /// boundaries, digits, leading/trailing/repeated separators, non-ASCII
    /// (whose `to_lowercase` may expand), and the empty string.
    const IDENTS: &[&str] = &[
        "",
        "x",
        "flush",
        "loadConfig",
        "load_config",
        "kebab-case-name",
        "with space",
        "__leading",
        "trailing__",
        "a__b",
        "HTTPServer",
        "parseHTTP2Frame",
        "v2Handler",
        "snake_And_Camel",
        "ÄÖÜ_grüß",
        "İstanbul",
        "ALLCAPS",
    ];

    /// The reusable-buffer tokenizer must agree with the allocating one, and
    /// must not leak stale tokens from a previous, longer call into the next
    /// result.
    #[test]
    fn split_into_matches_allocating_version_and_reuses_buffer() {
        let mut buf: Vec<String> = Vec::new();
        for s in IDENTS {
            let want = split_identifier(s);
            let n = split_identifier_into(s, &mut buf);
            assert_eq!(n, want.len(), "token count for {s:?}");
            assert_eq!(&buf[..n], &want[..], "tokens for {s:?}");
        }
        // Same sequence again in reverse, so every call inherits a buffer left
        // over from a different (often longer) input.
        for s in IDENTS.iter().rev() {
            let want = split_identifier(s);
            let n = split_identifier_into(s, &mut buf);
            assert_eq!(&buf[..n], &want[..], "tokens for {s:?} after reuse");
        }
    }

    /// A `ScoreScratch` carried across many symbols must produce exactly the
    /// scores a fresh one would — the property `context_pack` depends on.
    #[test]
    fn scratch_reuse_does_not_change_scores() {
        let terms = tokenize("write back page cache to disk");
        /// `(name, kind, signature, container, path)`.
        type Case<'a> = (&'a str, &'a str, Option<&'a str>, Option<&'a str>, &'a str);
        let cases: &[Case] = &[
            ("writeback", "function", None, None, "mm/page-writeback.c"),
            (
                "write_back_pages",
                "function",
                Some("int write_back_pages(struct page *p)"),
                Some("PageCache"),
                "fs/read_write.c",
            ),
            ("zebra", "struct", None, None, "drivers/zoo.c"),
            (
                "cache",
                "method",
                Some("void cache(void)"),
                None,
                "mm/cache.c",
            ),
            (
                "İstanbul",
                "function",
                None,
                Some("ÄÖÜ_grüß"),
                "i18n/ünicode.c",
            ),
            ("x", "field", Some("disk"), Some("page"), "a.c"),
            ("PageWriteback", "class", None, None, "include/linux/page.h"),
        ];
        let mut shared = ScoreScratch::default();
        for (name, kind, sig, cont, path) in cases {
            // Fresh scratch per call: the reference behavior.
            let mut fresh = ScoreScratch::default();
            let want = lexical_score_with(
                name,
                kind,
                *sig,
                *cont,
                path_term_bonus(path, &terms, &mut fresh),
                &terms,
                &mut fresh,
            );
            let got = lexical_score_with(
                name,
                kind,
                *sig,
                *cont,
                path_term_bonus(path, &terms, &mut shared),
                &terms,
                &mut shared,
            );
            assert_eq!(got, want, "score for {name:?} in {path:?}");
            // And the public one-shot wrapper must agree too.
            assert_eq!(
                lexical_score(name, kind, *sig, *cont, path, &terms),
                want,
                "wrapper score for {name:?}"
            );
        }
    }

    /// `Some("")` must behave like the original: an empty signature still takes
    /// the `signature.is_some()` branch, and an empty container tokenizes to
    /// nothing. Also checks that a stale longer value in a reused buffer cannot
    /// leak into a later empty one.
    #[test]
    fn empty_and_none_signature_container_are_distinguished() {
        let terms = tokenize("alpha beta");
        let mut sh = ScoreScratch::default();
        // Prime the buffers with long values so any leak would show up.
        let _ = lexical_score_with(
            "alpha_beta_gamma",
            "function",
            Some("fn alpha(beta: Beta) -> Gamma"),
            Some("AlphaContainer"),
            0.0,
            &terms,
            &mut sh,
        );
        let cases: &[(Option<&str>, Option<&str>)] = &[
            (None, None),
            (Some(""), None),
            (None, Some("")),
            (Some(""), Some("")),
            (Some("alpha"), Some("beta")),
        ];
        for (sig, cont) in cases {
            let mut fresh = ScoreScratch::default();
            let want = lexical_score_with("zzz", "other", *sig, *cont, 0.0, &terms, &mut fresh);
            let got = lexical_score_with("zzz", "other", *sig, *cont, 0.0, &terms, &mut sh);
            assert_eq!(got, want, "sig={sig:?} cont={cont:?}");
            assert_eq!(
                lexical_score("zzz", "other", *sig, *cont, "x.rs", &terms),
                want,
                "wrapper disagrees for sig={sig:?} cont={cont:?}"
            );
        }
    }

    /// The path contribution is 2.0 per matching term and must be independent
    /// of the symbol, since `context_pack` hoists it per document.
    #[test]
    fn path_bonus_counts_each_matching_term_once() {
        let terms = tokenize("page cache writeback");
        let mut s = ScoreScratch::default();
        assert_eq!(path_term_bonus("mm/nothing.c", &terms, &mut s), 0.0);
        assert_eq!(path_term_bonus("mm/PAGE.c", &terms, &mut s), 2.0);
        assert_eq!(path_term_bonus("mm/page-writeback.c", &terms, &mut s), 4.0);
        // A path match alone lifts an otherwise-unrelated symbol above zero,
        // which is why the scan cannot prune on the name column alone.
        assert!(lexical_score("zebra", "other", None, None, "mm/page.c", &terms) > 0.0);
    }
}