reflex-search 1.5.1

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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Changelog generation: product-level summary of recent development activity
//!
//! Extracts recent git commits, groups them via LLM into high-level changelog
//! entries, and renders clean prose without commit hashes or file paths.
//! Falls back to a structural commit list when no LLM is available.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::process::Command;

use super::git_intel;

/// A complete changelog
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Changelog {
    pub entries: Vec<ChangelogEntry>,
    pub raw_commits: Vec<ChangelogCommit>,
    pub branch: String,
    pub narrated: bool,
}

/// A single high-level changelog entry (rendered output)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangelogEntry {
    pub title: String,
    pub description: String,
}

/// A raw commit — used as LLM input context, NOT rendered directly
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangelogCommit {
    pub hash: String,
    pub author: String,
    pub timestamp: i64,
    pub date: String,
    pub subject: String,
    pub files_changed: Vec<String>,
}

/// Extract recent commits and branch name from git.
///
/// Returns `(commits, branch_name)`.
pub fn extract_changelog_commits(
    root: &Path,
    count: usize,
) -> Result<(Vec<ChangelogCommit>, String)> {
    // Get branch name
    let branch_output = Command::new("git")
        .arg("-C")
        .arg(root)
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .output()
        .context("Failed to run git rev-parse")?;

    let branch = if branch_output.status.success() {
        String::from_utf8_lossy(&branch_output.stdout)
            .trim()
            .to_string()
    } else {
        "unknown".to_string()
    };

    // Get commits with file lists
    let output = Command::new("git")
        .arg("-C")
        .arg(root)
        .args([
            "log",
            &format!("-{}", count),
            "--format=%H|%an|%at|%s",
            "--name-only",
        ])
        .output()
        .context("Failed to run git log")?;

    if !output.status.success() {
        return Ok((vec![], branch));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut commits: Vec<ChangelogCommit> = Vec::new();

    for line in stdout.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        // Try to parse as a commit header line (has | separators)
        let parts: Vec<&str> = trimmed.splitn(4, '|').collect();
        if parts.len() == 4 && parts[0].len() == 40 {
            let timestamp: i64 = parts[2].parse().unwrap_or(0);
            commits.push(ChangelogCommit {
                hash: parts[0].to_string(),
                author: parts[1].to_string(),
                timestamp,
                date: git_intel::epoch_to_date_string(timestamp),
                subject: parts[3].to_string(),
                files_changed: Vec::new(),
            });
        } else if let Some(last) = commits.last_mut() {
            // It's a file path belonging to the most recent commit
            last.files_changed.push(trimmed.to_string());
        }
    }

    Ok((commits, branch))
}

/// Top-level extraction: get commits + build a Changelog struct (structural, no narration).
pub fn extract_changelog(root: &Path, count: usize) -> Result<Changelog> {
    let (commits, branch) = extract_changelog_commits(root, count)?;
    let entries = generate_structural_entries(&commits);

    Ok(Changelog {
        entries,
        raw_commits: commits,
        branch,
        narrated: false,
    })
}

/// Build the context string sent to the LLM (the user never sees this directly).
pub fn build_changelog_context(commits: &[ChangelogCommit], branch: &str) -> String {
    let mut ctx = String::new();
    ctx.push_str(&format!("Branch: {}\n", branch));
    ctx.push_str(&format!("{} most recent commits:\n\n", commits.len()));

    for (i, commit) in commits.iter().enumerate() {
        ctx.push_str(&format!(
            "{}. \"{}\" ({}, {})\n",
            i + 1,
            commit.subject,
            commit.author,
            commit.date,
        ));

        if !commit.files_changed.is_empty() {
            // Group file paths by top-level directory for brevity
            let areas: Vec<&str> = commit
                .files_changed
                .iter()
                .filter_map(|f| {
                    let parts: Vec<&str> = f.splitn(3, '/').collect();
                    if parts.len() >= 2 {
                        Some(parts[..2].join("/").leak() as &str)
                    } else {
                        Some(f.as_str())
                    }
                })
                .collect();

            // Deduplicate
            let mut unique_areas: Vec<&str> = areas.clone();
            unique_areas.sort();
            unique_areas.dedup();
            let display: Vec<&str> = unique_areas.into_iter().take(5).collect();

            ctx.push_str(&format!("   Areas: {}\n", display.join(", ")));
        }
        ctx.push('\n');
    }

    ctx
}

/// Parse the LLM's JSON response into changelog entries.
///
/// Expected format: `{ "entries": [{ "title": "...", "description": "..." }] }`
/// Falls back to structural entries on parse failure.
pub fn parse_changelog_response(
    response: &str,
    commits: &[ChangelogCommit],
) -> Vec<ChangelogEntry> {
    // Strip markdown fences if present
    let cleaned = response
        .trim()
        .trim_start_matches("```json")
        .trim_start_matches("```")
        .trim_end_matches("```")
        .trim();

    #[derive(Deserialize)]
    struct LlmResponse {
        entries: Vec<LlmEntry>,
    }

    #[derive(Deserialize)]
    struct LlmEntry {
        title: String,
        description: String,
    }

    match serde_json::from_str::<LlmResponse>(cleaned) {
        Ok(parsed) => parsed
            .entries
            .into_iter()
            .map(|e| ChangelogEntry {
                title: e.title,
                description: e.description,
            })
            .collect(),
        Err(e) => {
            log::warn!("Failed to parse changelog LLM response: {}", e);
            generate_structural_entries(commits)
        }
    }
}

/// Generate simple entries from raw commits (no-LLM fallback).
///
/// One entry per commit: title = subject, description = date + author.
pub fn generate_structural_entries(commits: &[ChangelogCommit]) -> Vec<ChangelogEntry> {
    commits
        .iter()
        .map(|c| ChangelogEntry {
            title: c.subject.clone(),
            description: format!("{}{}", c.author, c.date),
        })
        .collect()
}

/// Format a "YYYY-MM-DD" date into a short human-readable form.
///
/// - Same year as `reference_year`: "Apr 8"
/// - Different year: "Apr 8, 2025"
fn format_date_short(date_str: &str, reference_year: i32) -> String {
    const MONTHS: [&str; 12] = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];

    let parts: Vec<&str> = date_str.split('-').collect();
    if parts.len() != 3 {
        return date_str.to_string();
    }

    let year: i32 = parts[0].parse().unwrap_or(0);
    let month: usize = parts[1].parse().unwrap_or(1);
    let day: u32 = parts[2].parse().unwrap_or(1);

    let month_name = MONTHS.get(month.wrapping_sub(1)).unwrap_or(&"???");

    if year == reference_year {
        format!("{} {}", month_name, day)
    } else {
        format!("{} {}, {}", month_name, day, year)
    }
}

/// Compute the date range string from raw commits.
///
/// Returns `None` if commits is empty. Collapses to a single date when
/// earliest == latest. Uses "Mon DD" for same-year, "Mon DD, YYYY" otherwise.
fn format_commit_date_range(commits: &[ChangelogCommit]) -> Option<String> {
    if commits.is_empty() {
        return None;
    }

    let dates: Vec<&str> = commits.iter().map(|c| c.date.as_str()).collect();
    let earliest = dates.iter().min().unwrap();
    let latest = dates.iter().max().unwrap();

    let ref_year: i32 = latest
        .split('-')
        .next()
        .and_then(|y| y.parse().ok())
        .unwrap_or(0);

    if earliest == latest {
        Some(format_date_short(earliest, ref_year))
    } else {
        let start = format_date_short(earliest, ref_year);
        let end = format_date_short(latest, ref_year);
        Some(format!("{}{}", start, end))
    }
}

/// Render a changelog as markdown.
pub fn render_markdown(changelog: &Changelog) -> String {
    let mut md = String::new();
    let date_range = format_commit_date_range(&changelog.raw_commits);

    if changelog.narrated {
        // Date range subtitle before entries
        if let Some(range) = &date_range {
            md.push_str(&format!("*{}*\n\n", range));
        }

        // LLM-narrated: clean prose entries
        for entry in &changelog.entries {
            md.push_str(&format!("## {}\n\n", entry.title));
            md.push_str(&entry.description);
            md.push_str("\n\n");
        }
    } else {
        // Structural: compact commit list with date range
        let date_suffix = date_range
            .as_ref()
            .map(|r| format!(" · {}", r))
            .unwrap_or_default();

        md.push_str(&format!(
            "*Recent activity on `{}`{} ({} commits)*\n\n",
            changelog.branch,
            date_suffix,
            changelog.raw_commits.len(),
        ));

        for entry in &changelog.entries {
            md.push_str(&format!("- **{}** — {}\n", entry.title, entry.description));
        }
        md.push('\n');
    }

    md
}

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

    #[test]
    fn test_generate_structural_entries() {
        let commits = vec![
            ChangelogCommit {
                hash: "abc123".into(),
                author: "Alice".into(),
                timestamp: 1712880000,
                date: "2024-04-12".into(),
                subject: "Added search feature".into(),
                files_changed: vec!["src/search.rs".into()],
            },
            ChangelogCommit {
                hash: "def456".into(),
                author: "Bob".into(),
                timestamp: 1712793600,
                date: "2024-04-11".into(),
                subject: "Fixed login bug".into(),
                files_changed: vec!["src/auth.rs".into()],
            },
        ];

        let entries = generate_structural_entries(&commits);
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].title, "Added search feature");
        assert!(entries[0].description.contains("Alice"));
        assert!(entries[0].description.contains("2024-04-12"));
    }

    #[test]
    fn test_parse_changelog_response_valid() {
        let json =
            r#"{"entries": [{"title": "New search", "description": "Added full-text search."}]}"#;
        let entries = parse_changelog_response(json, &[]);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].title, "New search");
    }

    #[test]
    fn test_parse_changelog_response_with_fences() {
        let json =
            "```json\n{\"entries\": [{\"title\": \"Test\", \"description\": \"Desc\"}]}\n```";
        let entries = parse_changelog_response(json, &[]);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].title, "Test");
    }

    #[test]
    fn test_parse_changelog_response_invalid_fallback() {
        let commits = vec![ChangelogCommit {
            hash: "abc".into(),
            author: "Alice".into(),
            timestamp: 0,
            date: "2024-01-01".into(),
            subject: "Fallback commit".into(),
            files_changed: vec![],
        }];
        let entries = parse_changelog_response("not valid json", &commits);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].title, "Fallback commit");
    }

    #[test]
    fn test_render_markdown_structural() {
        let changelog = Changelog {
            entries: vec![ChangelogEntry {
                title: "Added search".into(),
                description: "Alice — 2024-04-12".into(),
            }],
            raw_commits: vec![ChangelogCommit {
                hash: "abc".into(),
                author: "Alice".into(),
                timestamp: 0,
                date: "2024-04-12".into(),
                subject: "Added search".into(),
                files_changed: vec![],
            }],
            branch: "main".into(),
            narrated: false,
        };

        let md = render_markdown(&changelog);
        assert!(md.contains("Recent activity on `main`"));
        assert!(md.contains("Apr 12"));
        assert!(md.contains("**Added search**"));
    }

    #[test]
    fn test_render_markdown_structural_date_range() {
        let changelog = Changelog {
            entries: vec![
                ChangelogEntry {
                    title: "Added search".into(),
                    description: "Alice — 2024-04-12".into(),
                },
                ChangelogEntry {
                    title: "Fixed bug".into(),
                    description: "Bob — 2024-04-08".into(),
                },
            ],
            raw_commits: vec![
                ChangelogCommit {
                    hash: "abc".into(),
                    author: "Alice".into(),
                    timestamp: 0,
                    date: "2024-04-12".into(),
                    subject: "Added search".into(),
                    files_changed: vec![],
                },
                ChangelogCommit {
                    hash: "def".into(),
                    author: "Bob".into(),
                    timestamp: 0,
                    date: "2024-04-08".into(),
                    subject: "Fixed bug".into(),
                    files_changed: vec![],
                },
            ],
            branch: "main".into(),
            narrated: false,
        };

        let md = render_markdown(&changelog);
        assert!(
            md.contains("Apr 8 – Apr 12"),
            "Should contain date range, got: {}",
            md
        );
    }

    #[test]
    fn test_render_markdown_narrated() {
        let changelog = Changelog {
            entries: vec![ChangelogEntry {
                title: "Full-text search added".into(),
                description: "The documentation site now includes search.".into(),
            }],
            raw_commits: vec![ChangelogCommit {
                hash: "abc".into(),
                author: "Alice".into(),
                timestamp: 0,
                date: "2024-04-10".into(),
                subject: "Added search".into(),
                files_changed: vec![],
            }],
            branch: "main".into(),
            narrated: true,
        };

        let md = render_markdown(&changelog);
        assert!(
            md.contains("*Apr 10*"),
            "Should contain date subtitle, got: {}",
            md
        );
        assert!(md.contains("## Full-text search added"));
        assert!(md.contains("documentation site"));
    }

    #[test]
    fn test_format_date_short_same_year() {
        assert_eq!(format_date_short("2026-04-08", 2026), "Apr 8");
        assert_eq!(format_date_short("2026-12-25", 2026), "Dec 25");
        assert_eq!(format_date_short("2026-01-01", 2026), "Jan 1");
    }

    #[test]
    fn test_format_date_short_different_year() {
        assert_eq!(format_date_short("2025-04-08", 2026), "Apr 8, 2025");
    }

    #[test]
    fn test_format_commit_date_range_empty() {
        assert_eq!(format_commit_date_range(&[]), None);
    }

    #[test]
    fn test_format_commit_date_range_single() {
        let commits = vec![ChangelogCommit {
            hash: "abc".into(),
            author: "Alice".into(),
            timestamp: 0,
            date: "2026-04-10".into(),
            subject: "Test".into(),
            files_changed: vec![],
        }];
        assert_eq!(
            format_commit_date_range(&commits),
            Some("Apr 10".to_string())
        );
    }

    #[test]
    fn test_format_commit_date_range_span() {
        let commits = vec![
            ChangelogCommit {
                hash: "a".into(),
                author: "A".into(),
                timestamp: 0,
                date: "2026-04-08".into(),
                subject: "First".into(),
                files_changed: vec![],
            },
            ChangelogCommit {
                hash: "b".into(),
                author: "B".into(),
                timestamp: 0,
                date: "2026-04-13".into(),
                subject: "Last".into(),
                files_changed: vec![],
            },
        ];
        assert_eq!(
            format_commit_date_range(&commits),
            Some("Apr 8 – Apr 13".to_string())
        );
    }

    #[test]
    fn test_format_commit_date_range_cross_year() {
        let commits = vec![
            ChangelogCommit {
                hash: "a".into(),
                author: "A".into(),
                timestamp: 0,
                date: "2025-12-28".into(),
                subject: "First".into(),
                files_changed: vec![],
            },
            ChangelogCommit {
                hash: "b".into(),
                author: "B".into(),
                timestamp: 0,
                date: "2026-01-03".into(),
                subject: "Last".into(),
                files_changed: vec![],
            },
        ];
        assert_eq!(
            format_commit_date_range(&commits),
            Some("Dec 28, 2025 – Jan 3".to_string())
        );
    }

    #[test]
    fn test_build_changelog_context() {
        let commits = vec![ChangelogCommit {
            hash: "abc123".repeat(7)[..40].to_string(),
            author: "Alice".into(),
            timestamp: 1712880000,
            date: "2024-04-12".into(),
            subject: "Added search".into(),
            files_changed: vec!["src/pulse/search.rs".into(), "src/pulse/site.rs".into()],
        }];

        let ctx = build_changelog_context(&commits, "feature/search");
        assert!(ctx.contains("Branch: feature/search"));
        assert!(ctx.contains("\"Added search\""));
        assert!(ctx.contains("Areas:"));
    }
}