reflex-search 2.2.0

A local-first, structure-aware code search engine for AI 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! The changelog: one page per release, built from git tags, CHANGELOG.md and the API.
//!
//! A release is a semver tag (`v2.0.1`, `2.0.1`); "Unreleased" is everything after the
//! newest tag. Each release page shows, in order:
//! 1. its `CHANGELOG.md` section, verbatim, when one names the version;
//! 2. **API changes**: public items added, removed or with a changed signature between
//!    the previous tag and this one, found by extracting both versions of every changed
//!    source file (read from git in one `git cat-file --batch` process);
//! 3. the commits, grouped by conventional-commit type.

use crate::models::Language;
use crate::parsers::api::{self, ApiItem, Visibility};
use crate::pulse::changelog::ChangelogCommit;
use crate::pulse::extract::roles::{self, FileRole};
use std::collections::BTreeMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::Path;
use std::process::{Command, Stdio};

/// Releases shown, newest first (plus Unreleased).
pub const MAX_RELEASES: usize = 12;
/// API changes listed per kind (added, removed, changed) before "and N more".
pub const MAX_API_CHANGES: usize = 100;

#[derive(Debug, Clone)]
pub struct Release {
    /// `None` for Unreleased.
    pub tag: Option<String>,
    /// `2.0.1` or `Unreleased`.
    pub version: String,
    pub date: String,
    pub commits: Vec<ChangelogCommit>,
    /// CHANGELOG.md section: (markdown, first line, last line).
    pub notes: Option<(String, u32, u32)>,
    pub api: ApiDelta,
}

#[derive(Debug, Clone, Default, PartialEq)]
pub struct ApiDelta {
    pub added: Vec<ApiChange>,
    pub removed: Vec<ApiChange>,
    pub changed: Vec<(ApiChange, String)>,
    /// True counts; the lists hold at most [`MAX_API_CHANGES`] each.
    pub added_total: usize,
    pub removed_total: usize,
    pub changed_total: usize,
}

impl ApiDelta {
    pub fn is_empty(&self) -> bool {
        self.total() == 0
    }

    pub fn total(&self) -> usize {
        self.added_total + self.removed_total + self.changed_total
    }

    /// Changes counted but not listed.
    pub fn omitted(&self) -> usize {
        self.total() - self.added.len() - self.removed.len() - self.changed.len()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct ApiChange {
    /// `Type::method` or `function`.
    pub name: String,
    pub kind: &'static str,
    pub file: String,
    pub signature: String,
}

fn git(root: &Path, args: &[&str]) -> Option<String> {
    let out = Command::new("git")
        .arg("-C")
        .arg(root)
        .args(args)
        .output()
        .ok()?;
    out.status
        .success()
        .then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}

/// `v1.2.3` / `1.2.3` / `v1.2.3-rc.1` → comparable version, else `None`.
pub fn semver(tag: &str) -> Option<(u64, u64, u64, String)> {
    let t = tag.strip_prefix('v').unwrap_or(tag);
    let (core, pre) = t.split_once('-').unwrap_or((t, ""));
    let mut it = core.split('.');
    let v = (
        it.next()?.parse().ok()?,
        it.next()?.parse().ok()?,
        it.next().unwrap_or("0").parse().ok()?,
    );
    if it.next().is_some() {
        return None;
    }
    Some((v.0, v.1, v.2, pre.to_string()))
}

/// Semver tags with their dates, newest version first.
pub fn tags(root: &Path) -> Vec<(String, String)> {
    let Some(out) = git(
        root,
        &[
            "for-each-ref",
            "--format=%(refname:short)|%(creatordate:short)",
            "refs/tags",
        ],
    ) else {
        return Vec::new();
    };
    let mut tags: Vec<(String, String)> = out
        .lines()
        .filter_map(|l| l.split_once('|'))
        .filter(|(t, _)| semver(t).is_some())
        .map(|(t, d)| (t.to_string(), d.to_string()))
        .collect();
    tags.sort_by(|a, b| {
        let (x, y) = (semver(&a.0).unwrap(), semver(&b.0).unwrap());
        // Newest first; a pre-release sorts before its release.
        (y.0, y.1, y.2, y.3.is_empty()).cmp(&(x.0, x.1, x.2, x.3.is_empty()))
    });
    tags
}

/// Commits in `from..to` (all of `to` when `from` is `None`), newest first.
pub fn commits(root: &Path, from: Option<&str>, to: &str, limit: usize) -> Vec<ChangelogCommit> {
    let range = match from {
        Some(f) => format!("{f}..{to}"),
        None => to.to_string(),
    };
    let fmt = "--format=%H%x1f%an%x1f%at%x1f%s";
    let n = format!("-{limit}");
    let Some(out) = git(root, &["log", &n, fmt, &range]) else {
        return Vec::new();
    };
    out.lines()
        .filter_map(|l| {
            let mut p = l.split('\u{1f}');
            let (hash, author, ts, subject) = (p.next()?, p.next()?, p.next()?, p.next()?);
            let timestamp: i64 = ts.parse().ok()?;
            Some(ChangelogCommit {
                hash: hash.to_string(),
                author: author.to_string(),
                timestamp,
                date: crate::pulse::dates::epoch_to_date_string(timestamp),
                subject: subject.to_string(),
                files_changed: Vec::new(),
            })
        })
        .collect()
}

/// CHANGELOG.md sections: (heading, markdown body, first line, last line).
pub fn changelog_sections(content: &str) -> Vec<(String, String, u32, u32)> {
    let mut out = Vec::new();
    let mut cur: Option<(String, Vec<&str>, u32)> = None;
    let lines: Vec<&str> = content.lines().collect();
    for (i, line) in lines.iter().enumerate() {
        if let Some(h) = line.strip_prefix("## ") {
            if let Some((head, body, start)) = cur.take() {
                out.push((head, body.join("\n").trim().to_string(), start, i as u32));
            }
            cur = Some((h.trim().to_string(), Vec::new(), i as u32 + 2));
        } else if let Some((_, body, _)) = cur.as_mut() {
            body.push(line);
        }
    }
    if let Some((head, body, start)) = cur {
        out.push((
            head,
            body.join("\n").trim().to_string(),
            start,
            lines.len() as u32,
        ));
    }
    out
}

/// The section whose heading names `version` as a whole token, or the bare
/// `Unreleased` section for `None`.
pub fn section_for(
    sections: &[(String, String, u32, u32)],
    version: Option<&str>,
) -> Option<(String, u32, u32)> {
    sections
        .iter()
        .find(|(head, body, _, _)| {
            !body.is_empty()
                && match version {
                    Some(v) => head
                        .split(|c: char| !(c.is_ascii_alphanumeric() || c == '.' || c == '-'))
                        .any(|tok| tok.trim_start_matches('v') == v),
                    None => {
                        let h = head.to_ascii_lowercase();
                        h.contains("unreleased") && !h.chars().any(|c| c.is_ascii_digit())
                    }
                }
        })
        .map(|(_, body, a, b)| (body.clone(), *a, *b))
}

/// Read many `rev:path` blobs through one `git cat-file --batch` process.
struct BlobReader {
    child: std::process::Child,
}

impl BlobReader {
    fn open(root: &Path) -> Option<Self> {
        let child = Command::new("git")
            .arg("-C")
            .arg(root)
            .args(["cat-file", "--batch"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .spawn()
            .ok()?;
        Some(Self { child })
    }

    fn read(&mut self, rev: &str, path: &str) -> Option<String> {
        let stdin = self.child.stdin.as_mut()?;
        writeln!(stdin, "{rev}:{path}").ok()?;
        stdin.flush().ok()?;
        let stdout = self.child.stdout.as_mut()?;
        let mut reader = BufReader::new(stdout);
        let mut header = String::new();
        reader.read_line(&mut header).ok()?;
        if header.trim_end().ends_with("missing") {
            return None;
        }
        let size: usize = header.split_whitespace().nth(2)?.parse().ok()?;
        let mut buf = vec![0u8; size + 1]; // content + trailing newline
        reader.read_exact(&mut buf).ok()?;
        buf.pop();
        Some(String::from_utf8_lossy(&buf).into_owned())
    }
}

impl Drop for BlobReader {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

/// Whether a Python file is a private module: a directory or file name in its path
/// starts with `_` (`pkg/_core.py`, `pkg/_impl/x.py`); `__init__.py` does not count.
fn python_private_module(path: &str) -> bool {
    path.split('/')
        .map(|s| s.strip_suffix(".py").unwrap_or(s))
        .any(|s| s.starts_with('_') && s != "__init__")
}

/// Public items of one file version, keyed by `Type::member` / `name` (`Type.member`
/// in Python).
///
/// Python: `__all__`, when present, lists the public top-level names. A private module
/// (`_core.py`) counts only through its own `__all__`: what a package re-exports from
/// it is decided in another file. Documented dunders (`__init__`, `__call__`, …) and
/// enum members count, so a constructor change is an API change.
fn public_items(
    lang: Language,
    path: &str,
    source: &str,
) -> BTreeMap<String, (String, &'static str)> {
    let mut out = BTreeMap::new();
    let Some(file) = api::extract(lang, source) else {
        return out;
    };
    let python = lang == Language::Python;
    let exports = file.exports.as_deref().filter(|_| python);
    if python && exports.is_none() && python_private_module(path) {
        return out;
    }
    let rules = Rules {
        sep: if python { "." } else { "::" },
        inherited: python,
    };
    struct Rules {
        sep: &'static str,
        /// `Visibility::Inherited` members are API (Python dunders, enum members).
        inherited: bool,
    }
    fn walk(
        items: &[ApiItem],
        owner: Option<&str>,
        rules: &Rules,
        exports: Option<&[String]>,
        out: &mut BTreeMap<String, (String, &'static str)>,
    ) {
        for it in items {
            let visible = match it.visibility {
                Visibility::Public => true,
                Visibility::Inherited => rules.inherited,
                _ => false,
            };
            if it.test_only || it.hidden || !visible {
                continue;
            }
            if exports.is_some_and(|list| !list.contains(&it.name)) {
                continue;
            }
            let owner_name = it
                .self_type
                .as_deref()
                .map(|t| t.split('<').next().unwrap_or(t).trim());
            let name = match owner.or(owner_name) {
                Some(o) => format!("{o}{}{}", rules.sep, it.name),
                None => it.name.clone(),
            };
            out.insert(
                name.clone(),
                (
                    crate::parsers::api::collapse_ws(&it.signature),
                    it.kind.label(),
                ),
            );
            if it.kind.is_type() || it.kind == api::ApiKind::Module {
                walk(&it.members, Some(&name), rules, None, out);
            }
        }
    }
    walk(&file.items, None, &rules, exports, &mut out);
    out
}

/// Public API differences between two revisions (`to` may be `HEAD`).
pub fn api_delta(root: &Path, from: &str, to: &str) -> ApiDelta {
    let mut delta = ApiDelta::default();
    let Some(names) = git(root, &["diff", "--name-only", "--no-renames", from, to]) else {
        return delta;
    };
    let Some(mut blobs) = BlobReader::open(root) else {
        return delta;
    };
    for path in names.lines() {
        let lang = Language::from_path(Path::new(path));
        if !api::has_extractor(lang) || roles::classify(path) != FileRole::Source {
            continue;
        }
        let old = blobs
            .read(from, path)
            .map(|s| public_items(lang, path, &s))
            .unwrap_or_default();
        let new = blobs
            .read(to, path)
            .map(|s| public_items(lang, path, &s))
            .unwrap_or_default();
        let change = |name: &str, sig: &str, kind| ApiChange {
            name: name.to_string(),
            kind,
            file: path.to_string(),
            signature: sig.to_string(),
        };
        for (name, (sig, kind)) in &new {
            match old.get(name) {
                None => {
                    delta.added_total += 1;
                    if delta.added.len() < MAX_API_CHANGES {
                        delta.added.push(change(name, sig, kind));
                    }
                }
                Some((old_sig, _)) if old_sig != sig => {
                    delta.changed_total += 1;
                    if delta.changed.len() < MAX_API_CHANGES {
                        delta
                            .changed
                            .push((change(name, sig, kind), old_sig.clone()));
                    }
                }
                _ => {}
            }
        }
        for (name, (sig, kind)) in &old {
            if !new.contains_key(name) {
                delta.removed_total += 1;
                if delta.removed.len() < MAX_API_CHANGES {
                    delta.removed.push(change(name, sig, kind));
                }
            }
        }
    }
    delta
}

/// Releases, newest first: Unreleased (if it has commits), then up to
/// [`MAX_RELEASES`] tags.
pub fn collect(root: &Path, changelog_md: Option<&str>) -> Vec<Release> {
    let tags = tags(root);
    let sections = changelog_md.map(changelog_sections).unwrap_or_default();
    let mut out = Vec::new();
    let newest = tags.first().map(|(t, _)| t.clone());
    let unreleased = commits(root, newest.as_deref(), "HEAD", 500);
    if !unreleased.is_empty() || newest.is_none() {
        let api = newest
            .as_deref()
            .map(|t| api_delta(root, t, "HEAD"))
            .unwrap_or_default();
        out.push(Release {
            tag: None,
            version: "Unreleased".into(),
            date: unreleased
                .first()
                .map(|c| c.date.clone())
                .unwrap_or_default(),
            commits: unreleased,
            notes: section_for(&sections, None),
            api,
        });
    }
    for (i, (tag, date)) in tags.iter().take(MAX_RELEASES).enumerate() {
        let prev = tags.get(i + 1).map(|(t, _)| t.as_str());
        let version = tag.strip_prefix('v').unwrap_or(tag).to_string();
        out.push(Release {
            tag: Some(tag.clone()),
            date: date.clone(),
            commits: commits(root, prev, tag, 500),
            notes: section_for(&sections, Some(&version)),
            api: prev.map(|p| api_delta(root, p, tag)).unwrap_or_default(),
            version,
        });
    }
    out
}

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

    #[test]
    fn semver_parsing_and_order() {
        assert_eq!(semver("v2.0.1"), Some((2, 0, 1, String::new())));
        assert_eq!(semver("1.4"), Some((1, 4, 0, String::new())));
        assert_eq!(semver("v1.2.3-rc.1"), Some((1, 2, 3, "rc.1".into())));
        assert_eq!(semver("latest"), None);
        assert_eq!(semver("pulse-runtime-abc"), None);
    }

    #[test]
    fn changelog_section_matching() {
        let md = "# Changelog\n\n## [Unreleased]\n\n- next\n\n## [Unreleased] - 2.0.0\n\n- two\n\n## [1.7.2] - 2026-09-22\n\n- one-seven-two\n\n## [1.7.20] - x\n\n- not it\n";
        let s = changelog_sections(md);
        assert_eq!(s.len(), 4);
        assert_eq!(section_for(&s, None).unwrap().0, "- next");
        assert_eq!(section_for(&s, Some("2.0.0")).unwrap().0, "- two");
        assert_eq!(section_for(&s, Some("1.7.2")).unwrap().0, "- one-seven-two");
        assert!(section_for(&s, Some("1.7")).is_none());
    }

    #[test]
    fn public_item_keys() {
        let src = "pub struct S;\nimpl S { pub fn a(&self) {} fn hidden(&self) {} }\npub fn f(x: u8) {}\nfn private() {}\n";
        let items = public_items(Language::Rust, "src/lib.rs", src);
        let keys: Vec<&str> = items.keys().map(String::as_str).collect();
        assert_eq!(keys, vec!["S", "S::a", "f"]);
        assert_eq!(items["f"].0, "pub fn f(x: u8)");
    }

    #[test]
    fn python_public_item_keys() {
        let src = "__all__ = [\"Engine\", \"run\"]\nclass Engine:\n    def __init__(self, url: str): ...\n    def __repr__(self): ...\n    def start(self): ...\n    def _step(self): ...\ndef run(): ...\ndef helper(): ...\ndef _private(): ...\n";
        let keys = |path: &str, src: &str| -> Vec<String> {
            public_items(Language::Python, path, src)
                .into_keys()
                .collect()
        };
        assert_eq!(
            keys("pkg/__init__.py", src),
            vec!["Engine", "Engine.__init__", "Engine.start", "run"],
            "__all__ decides top-level names; the constructor counts, __repr__ does not"
        );
        assert_eq!(
            keys("pkg/_core.py", src),
            keys("pkg/__init__.py", src),
            "a private module with __all__ counts through it"
        );
        let no_all = "def run(): ...\ndef _private(): ...\n";
        assert_eq!(keys("pkg/api.py", no_all), vec!["run"]);
        assert!(keys("pkg/_core.py", no_all).is_empty());
        assert!(keys("pkg/_impl/api.py", no_all).is_empty());
    }

    #[test]
    fn api_delta_between_commits() {
        let t = tempfile::TempDir::new().unwrap();
        let r = t.path();
        let run = |args: &[&str]| {
            assert!(
                Command::new("git")
                    .arg("-C")
                    .arg(r)
                    .args(args)
                    .output()
                    .unwrap()
                    .status
                    .success(),
                "{args:?}"
            );
        };
        run(&["init", "-q"]);
        run(&["config", "user.email", "t@t"]);
        run(&["config", "user.name", "t"]);
        std::fs::create_dir_all(r.join("src")).unwrap();
        std::fs::write(
            r.join("src/lib.rs"),
            "pub fn keep() {}\npub fn gone() {}\npub fn sig(a: u8) {}\n",
        )
        .unwrap();
        run(&["add", "."]);
        run(&["commit", "-qm", "feat: one"]);
        run(&["tag", "v1.0.0"]);
        std::fs::write(
            r.join("src/lib.rs"),
            "pub fn keep() {}\npub fn sig(a: u16) {}\npub fn new_one() {}\n",
        )
        .unwrap();
        run(&["commit", "-qam", "feat(api)!: two"]);
        run(&["tag", "v1.1.0"]);

        let d = api_delta(r, "v1.0.0", "v1.1.0");
        assert_eq!(
            d.added.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(),
            vec!["new_one"]
        );
        assert_eq!(
            d.removed
                .iter()
                .map(|c| c.name.as_str())
                .collect::<Vec<_>>(),
            vec!["gone"]
        );
        assert_eq!(d.changed[0].0.signature, "pub fn sig(a: u16)");
        assert_eq!(d.changed[0].1, "pub fn sig(a: u8)");

        let rel = collect(r, None);
        assert_eq!(
            rel.iter().map(|x| x.version.as_str()).collect::<Vec<_>>(),
            vec!["1.1.0", "1.0.0"]
        );
        assert_eq!(rel[0].commits.len(), 1);
        assert_eq!(rel[0].commits[0].subject, "feat(api)!: two");
    }
}