reflex-search 1.3.4

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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! Glossary: product-level vocabulary
//!
//! The glossary surfaces ~10-15 *product concepts* — high-level noun phrases
//! that describe what the software does (capabilities, data ideas, workflows),
//! not specific Rust types or function names.
//!
//! Unlike v2, we do **not** rank symbols from the cache. Instead we collect
//! a compact "structural evidence" bundle (module paths + a handful of anchor
//! symbol names per module) and hand it to the LLM in a single narration
//! task. The LLM is responsible for selecting concepts, writing definitions,
//! grouping into categories, and anchoring each concept to the modules that
//! implement it. We then render the result as a card-based markdown page.
//!
//! In `--no-llm` mode (or if the LLM call fails), the page falls back to a
//! minimal message directing the user to re-run with LLM enabled, plus a
//! module list derived from the same structural evidence.

use anyhow::{Context, Result};
use rusqlite::Connection;
use serde::Deserialize;
use std::collections::HashMap;

use crate::cache::CacheManager;
use crate::models::SearchResult;

/// How many anchor symbol names to pull per module as LLM evidence.
const ANCHOR_SYMBOLS_PER_MODULE: usize = 5;

/// Maximum number of modules to include in the evidence bundle. Keeps the
/// prompt bounded on very wide repositories.
const MAX_MODULES_IN_EVIDENCE: usize = 25;

/// A single product-level concept, as decided and written by the LLM.
#[derive(Debug, Clone)]
pub struct Concept {
    /// Human-readable concept name (e.g. "Trigram Index").
    pub name: String,
    /// 1-3 sentence plain-language definition.
    pub definition: String,
    /// Module paths (e.g. "src/index", "src/query") that the LLM anchored
    /// this concept to. Used to render wiki links in the card footer.
    pub related_modules: Vec<String>,
    /// LLM-assigned category bucket (e.g. "Core Capabilities", "Data Model").
    pub category: Option<String>,
}

/// Full glossary data rendered on the `/glossary/` page.
#[derive(Debug, Clone, Default)]
pub struct GlossaryData {
    pub concepts: Vec<Concept>,
    /// 2-3 sentence LLM-written intro paragraph for the page.
    pub intro: Option<String>,
}

/// Summary of one module for the LLM evidence bundle.
#[derive(Debug, Clone)]
pub struct ModuleEvidence {
    /// Module path (e.g. "src/pulse").
    pub path: String,
    /// Number of files in the module.
    pub file_count: usize,
    /// Top-N anchor symbol names (strings only, no kind or location).
    pub anchor_symbols: Vec<String>,
}

/// Structural evidence handed to the LLM to let it pick product concepts.
#[derive(Debug, Clone, Default)]
pub struct GlossaryEvidence {
    pub total_files: usize,
    pub total_lines: usize,
    pub language_mix: Vec<(String, usize)>,
    pub dependency_edges: usize,
    pub hotspot_files: Vec<String>,
    pub modules: Vec<ModuleEvidence>,
}

/// Raw JSON shape returned by the LLM. Deserialized then lifted into
/// [`GlossaryData`].
#[derive(Debug, Clone, Deserialize)]
pub struct ConceptsResponse {
    #[serde(default)]
    pub intro: Option<String>,
    #[serde(default)]
    pub concepts: Vec<RawConcept>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct RawConcept {
    pub name: String,
    #[serde(default)]
    pub definition: String,
    #[serde(default)]
    pub category: Option<String>,
    #[serde(default)]
    pub related_modules: Vec<String>,
}

impl From<RawConcept> for Concept {
    fn from(raw: RawConcept) -> Self {
        Concept {
            name: raw.name,
            definition: raw.definition,
            category: raw.category,
            related_modules: raw.related_modules,
        }
    }
}

impl From<ConceptsResponse> for GlossaryData {
    fn from(resp: ConceptsResponse) -> Self {
        GlossaryData {
            concepts: resp.concepts.into_iter().map(Into::into).collect(),
            intro: resp.intro,
        }
    }
}

/// Derive a top-two-segment module path from a file path.
///
/// - `src/models.rs` → `src`
/// - `src/pulse/wiki.rs` → `src/pulse`
/// - `src/parsers/rust/mod.rs` → `src/parsers`
fn module_of(file_path: &str) -> String {
    let parts: Vec<&str> = file_path.split('/').collect();
    match parts.len() {
        0 | 1 => String::new(),
        2 => parts[0].to_string(),
        _ => format!("{}/{}", parts[0], parts[1]),
    }
}

/// Convert a module path like `src/pulse` into its wiki slug (`src-pulse`).
fn module_slug(module_path: &str) -> String {
    module_path.replace('/', "-")
}

/// Relative "weight" used only to sort anchor symbols within a module so that
/// type-like names (Struct, Trait, Enum) come before Functions before
/// Variables. This is *not* a filter — every non-Variable kind may contribute
/// anchor names.
fn anchor_priority(kind: &str) -> u8 {
    match kind.to_lowercase().as_str() {
        "struct" | "class" | "trait" | "interface" | "enum" | "type" | "typedef" => 0,
        "function" | "method" | "macro" | "module" => 1,
        "constant" | "property" | "event" | "attribute" | "export" => 2,
        // Variables, imports, and unknowns get the lowest priority; the plan
        // explicitly notes variables clutter the evidence.
        _ => 3,
    }
}

/// Collect the structural evidence bundle that will be handed to the LLM for
/// concept selection. Cheap: a handful of SQL queries plus symbol-name
/// extraction, no tree-sitter parsing.
///
/// Returns `Ok(None)` if the cache exists but has no symbols table (nothing
/// to anchor concepts to).
pub fn collect_glossary_evidence(cache: &CacheManager) -> Result<Option<GlossaryEvidence>> {
    let db_path = cache.path().join("meta.db");
    let conn = Connection::open(&db_path).context("Failed to open meta.db")?;

    let has_symbols: bool = conn
        .query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='symbols'",
            [],
            |row| row.get::<_, i64>(0),
        )
        .map(|c| c > 0)
        .unwrap_or(false);

    if !has_symbols {
        return Ok(None);
    }

    let total_files: usize = conn
        .query_row("SELECT COUNT(*) FROM files", [], |r| r.get(0))
        .unwrap_or(0);
    let total_lines: usize = conn
        .query_row("SELECT COALESCE(SUM(line_count), 0) FROM files", [], |r| {
            r.get(0)
        })
        .unwrap_or(0);

    // Language mix (top 10).
    let mut language_mix: Vec<(String, usize)> = Vec::new();
    if let Ok(mut stmt) = conn.prepare(
        "SELECT COALESCE(language, 'other'), COUNT(*) FROM files \
         GROUP BY language ORDER BY COUNT(*) DESC LIMIT 10",
    ) {
        if let Ok(rows) =
            stmt.query_map([], |row| Ok((row.get::<_, String>(0)?, row.get::<_, usize>(1)?)))
        {
            language_mix = rows.flatten().collect();
        }
    }

    // Dependency edge count (best-effort; may be 0 if table absent).
    let dependency_edges: usize = conn
        .query_row::<usize, _, _>(
            "SELECT COUNT(*) FROM file_dependencies WHERE resolved_file_id IS NOT NULL",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0);

    // Top hotspot files (most-imported) — good anchor hints for the LLM.
    let mut hotspot_files: Vec<String> = Vec::new();
    if dependency_edges > 0 {
        if let Ok(mut stmt) = conn.prepare(
            "SELECT f.path, COUNT(DISTINCT fd.file_id) as dep_count \
             FROM file_dependencies fd JOIN files f ON fd.resolved_file_id = f.id \
             GROUP BY fd.resolved_file_id ORDER BY dep_count DESC LIMIT 8",
        ) {
            if let Ok(rows) = stmt.query_map([], |row| row.get::<_, String>(0)) {
                hotspot_files = rows.flatten().collect();
            }
        }
    }

    // Walk the symbols table once and bucket symbol names by module path.
    // For each module we keep up to `ANCHOR_SYMBOLS_PER_MODULE` names, sorted
    // by anchor priority (types before functions before constants, etc.).
    let mut stmt = conn.prepare(
        "SELECT s.symbols_json, f.path, f.line_count \
         FROM symbols s JOIN files f ON s.file_id = f.id",
    )?;
    let rows: Vec<(String, String, usize)> = stmt
        .query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, usize>(2).unwrap_or(0),
            ))
        })?
        .filter_map(|r| r.ok())
        .collect();

    #[derive(Default)]
    struct ModuleBucket {
        file_count: usize,
        // (priority, name) — kept in a Vec so we can dedupe then truncate.
        candidates: Vec<(u8, String)>,
    }

    let mut by_module: HashMap<String, ModuleBucket> = HashMap::new();

    for (symbols_json, file_path, _line_count) in rows {
        let module = module_of(&file_path);
        if module.is_empty() {
            continue;
        }
        let bucket = by_module.entry(module.clone()).or_default();
        bucket.file_count += 1;

        let symbols: Vec<SearchResult> = match serde_json::from_str(&symbols_json) {
            Ok(s) => s,
            Err(_) => continue,
        };

        for sr in symbols {
            let Some(name) = sr.symbol else { continue };
            if name.len() < 3 {
                continue;
            }
            let kind_str = sr.kind.to_string();
            // Skip the noisiest kinds outright.
            let kl = kind_str.to_lowercase();
            if kl == "variable" || kl == "import" || kl == "export" || kl == "unknown" {
                continue;
            }
            let priority = anchor_priority(&kind_str);
            bucket.candidates.push((priority, name));
        }
    }

    // Build per-module evidence. Sort candidates by priority then by name for
    // determinism, dedupe, and truncate.
    let mut modules: Vec<ModuleEvidence> = by_module
        .into_iter()
        .map(|(path, mut bucket)| {
            bucket
                .candidates
                .sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
            let mut anchors: Vec<String> = Vec::new();
            let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
            for (_, name) in bucket.candidates {
                if seen.insert(name.clone()) {
                    anchors.push(name);
                    if anchors.len() >= ANCHOR_SYMBOLS_PER_MODULE {
                        break;
                    }
                }
            }
            ModuleEvidence {
                path,
                file_count: bucket.file_count,
                anchor_symbols: anchors,
            }
        })
        .collect();

    // Largest modules first, then alphabetical. Cap at MAX_MODULES_IN_EVIDENCE.
    modules.sort_by(|a, b| b.file_count.cmp(&a.file_count).then_with(|| a.path.cmp(&b.path)));
    modules.truncate(MAX_MODULES_IN_EVIDENCE);

    Ok(Some(GlossaryEvidence {
        total_files,
        total_lines,
        language_mix,
        dependency_edges,
        hotspot_files,
        modules,
    }))
}

/// Build the structural evidence block that will be concatenated onto the
/// concepts system prompt. The format is plain text with labeled sections
/// because the LLM parses it as free-form evidence, not structured data.
pub fn build_concepts_context(evidence: &GlossaryEvidence, project_name: &str) -> String {
    let mut ctx = String::new();

    ctx.push_str(&format!("Project: {}\n", project_name));
    ctx.push_str(&format!(
        "Scale: {} files, {} lines, {} modules, {} dependency edges\n",
        evidence.total_files,
        evidence.total_lines,
        evidence.modules.len(),
        evidence.dependency_edges,
    ));

    if !evidence.language_mix.is_empty() {
        let langs: Vec<String> = evidence
            .language_mix
            .iter()
            .map(|(lang, count)| format!("{} ({})", lang, count))
            .collect();
        ctx.push_str(&format!("Languages: {}\n", langs.join(", ")));
    }
    ctx.push('\n');

    ctx.push_str("Top-level modules (with anchor symbol names):\n");
    for m in &evidence.modules {
        if m.anchor_symbols.is_empty() {
            ctx.push_str(&format!("- {} ({} files)\n", m.path, m.file_count));
        } else {
            ctx.push_str(&format!(
                "- {} ({} files) — key symbols: {}\n",
                m.path,
                m.file_count,
                m.anchor_symbols.join(", ")
            ));
        }
    }
    ctx.push('\n');

    if !evidence.hotspot_files.is_empty() {
        ctx.push_str("Dependency hotspots (most-imported files):\n");
        for path in &evidence.hotspot_files {
            ctx.push_str(&format!("- {}\n", path));
        }
        ctx.push('\n');
    }

    ctx
}

/// Parse the LLM's JSON response into a [`ConceptsResponse`].
///
/// Accepts a raw response that may be wrapped in markdown code fences
/// (```json ... ```) — we strip them before feeding to `serde_json` because
/// models occasionally violate the "no code fences" instruction in the
/// system prompt.
pub fn parse_concepts_response(raw: &str) -> Result<ConceptsResponse> {
    let trimmed = raw.trim();

    // Strip ```json ... ``` or ``` ... ``` if the LLM wrapped its output.
    let cleaned: &str = if let Some(rest) = trimmed.strip_prefix("```json") {
        rest.trim_start().trim_end_matches("```").trim()
    } else if let Some(rest) = trimmed.strip_prefix("```") {
        rest.trim_start().trim_end_matches("```").trim()
    } else {
        trimmed
    };

    // If there's leading/trailing prose, try to extract the JSON object by
    // looking for the first '{' and the matching last '}'.
    let slice = if cleaned.starts_with('{') {
        cleaned
    } else if let (Some(start), Some(end)) = (cleaned.find('{'), cleaned.rfind('}')) {
        &cleaned[start..=end]
    } else {
        cleaned
    };

    serde_json::from_str::<ConceptsResponse>(slice)
        .context("Failed to parse concepts JSON response from LLM")
}

/// Render the full glossary page markdown. When concepts are present, this
/// emits the card-based layout; when empty, it emits the "no concepts"
/// fallback message (used in LLM-failure paths).
pub fn render_glossary_markdown(data: &GlossaryData) -> String {
    if data.concepts.is_empty() {
        return "*Concepts are generated by the LLM narration pipeline. \
                Re-run `rfx pulse generate` with LLM enabled to populate this page.*\n"
            .to_string();
    }

    let mut md = String::new();

    if let Some(ref intro) = data.intro {
        md.push_str(intro.trim());
        md.push_str("\n\n");
    }

    // Group concepts by category preserving first-seen order so the page
    // reflects whatever ordering the LLM chose.
    let mut order: Vec<String> = Vec::new();
    let mut grouped: HashMap<String, Vec<&Concept>> = HashMap::new();
    for concept in &data.concepts {
        let cat = concept
            .category
            .clone()
            .unwrap_or_else(|| "Concepts".to_string());
        if !grouped.contains_key(&cat) {
            order.push(cat.clone());
        }
        grouped.entry(cat).or_default().push(concept);
    }

    md.push_str(&format!(
        "**{}** core concepts across {} {}.\n\n",
        data.concepts.len(),
        order.len(),
        if order.len() == 1 { "category" } else { "categories" },
    ));

    for cat in &order {
        md.push_str(&format!("## {}\n\n", cat));
        if let Some(items) = grouped.get(cat) {
            for concept in items {
                md.push_str(&format!("### {}\n\n", concept.name));

                // Blockquoted definition.
                for line in concept.definition.trim().lines() {
                    md.push_str("> ");
                    md.push_str(line);
                    md.push('\n');
                }
                md.push('\n');

                if !concept.related_modules.is_empty() {
                    let links: Vec<String> = concept
                        .related_modules
                        .iter()
                        .map(|m| {
                            format!("[`{}`](/wiki/{}/)", m.trim(), module_slug(m.trim()))
                        })
                        .collect();
                    md.push_str(&format!("*Implemented in {}*\n\n", links.join(", ")));
                }
            }
        }
    }

    md
}

/// Render the `--no-llm` fallback page: a short explanation plus a bullet
/// list of modules from the evidence bundle so the page still shows useful
/// structure even without LLM narration.
pub fn render_glossary_no_llm(evidence: &GlossaryEvidence) -> String {
    let mut md = String::new();
    md.push_str(
        "*Concepts are generated by the LLM narration pipeline. \
         Re-run `rfx pulse generate` with LLM enabled to populate this page.*\n\n",
    );

    if evidence.modules.is_empty() {
        return md;
    }

    md.push_str("**Modules in this codebase:**\n\n");
    for m in &evidence.modules {
        md.push_str(&format!(
            "- [`{}`](/wiki/{}/) ({} files)\n",
            m.path,
            module_slug(&m.path),
            m.file_count
        ));
    }
    md.push('\n');
    md
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cache::CacheManager;
    use tempfile::TempDir;

    fn empty_cache() -> (TempDir, CacheManager) {
        let tmp = TempDir::new().unwrap();
        let cache = CacheManager::new(tmp.path().to_str().unwrap());
        cache.init().unwrap();
        (tmp, cache)
    }

    #[test]
    fn test_module_of() {
        assert_eq!(module_of("src/models.rs"), "src");
        assert_eq!(module_of("src/pulse/wiki.rs"), "src/pulse");
        assert_eq!(module_of("src/parsers/rust/mod.rs"), "src/parsers");
        assert_eq!(module_of("README.md"), "");
    }

    #[test]
    fn test_module_slug() {
        assert_eq!(module_slug("src"), "src");
        assert_eq!(module_slug("src/pulse"), "src-pulse");
        assert_eq!(module_slug("src/parsers/rust"), "src-parsers-rust");
    }

    #[test]
    fn test_anchor_priority_orders_types_first() {
        assert!(anchor_priority("struct") < anchor_priority("function"));
        assert!(anchor_priority("trait") < anchor_priority("constant"));
        assert!(anchor_priority("enum") < anchor_priority("variable"));
    }

    #[test]
    fn test_collect_glossary_evidence_empty_cache() {
        let (_tmp, cache) = empty_cache();
        let result = collect_glossary_evidence(&cache).unwrap();
        // No symbols table → None.
        assert!(result.is_none());
    }

    #[test]
    fn test_build_concepts_context_includes_modules() {
        let evidence = GlossaryEvidence {
            total_files: 120,
            total_lines: 18_500,
            language_mix: vec![("rust".to_string(), 110), ("toml".to_string(), 10)],
            dependency_edges: 340,
            hotspot_files: vec!["src/models.rs".to_string()],
            modules: vec![
                ModuleEvidence {
                    path: "src".to_string(),
                    file_count: 42,
                    anchor_symbols: vec![
                        "Cli".to_string(),
                        "SearchResult".to_string(),
                        "run".to_string(),
                    ],
                },
                ModuleEvidence {
                    path: "src/pulse".to_string(),
                    file_count: 18,
                    anchor_symbols: vec![
                        "generate_site".to_string(),
                        "PulseReport".to_string(),
                    ],
                },
                ModuleEvidence {
                    path: "src/query".to_string(),
                    file_count: 9,
                    anchor_symbols: vec!["QueryEngine".to_string()],
                },
            ],
        };
        let ctx = build_concepts_context(&evidence, "Reflex");

        assert!(ctx.contains("Project: Reflex"));
        assert!(ctx.contains("120 files"));
        assert!(ctx.contains("src (42 files)"));
        assert!(ctx.contains("src/pulse"));
        assert!(ctx.contains("src/query"));
        assert!(ctx.contains("SearchResult"));
        assert!(ctx.contains("QueryEngine"));
        assert!(ctx.contains("Languages: rust (110)"));
        assert!(ctx.contains("Dependency hotspots"));
    }

    #[test]
    fn test_parse_concepts_response_valid_json() {
        let raw = r#"{
            "intro": "Reflex catalogs search primitives and indexing building blocks.",
            "concepts": [
                {
                    "name": "Trigram Index",
                    "category": "Core Capabilities",
                    "definition": "A fast inverted index built from three-character substrings.",
                    "related_modules": ["src/index", "src/query"]
                },
                {
                    "name": "Symbol Cache",
                    "category": "Data Model",
                    "definition": "A persistent store of parsed language symbols keyed by content hash.",
                    "related_modules": ["src/cache"]
                }
            ]
        }"#;

        let parsed = parse_concepts_response(raw).expect("should parse");
        assert_eq!(parsed.concepts.len(), 2);
        assert_eq!(parsed.concepts[0].name, "Trigram Index");
        assert_eq!(parsed.concepts[0].related_modules, vec!["src/index", "src/query"]);
        assert!(parsed.intro.as_ref().unwrap().contains("search primitives"));
    }

    #[test]
    fn test_parse_concepts_response_strips_markdown_fence() {
        let raw = "```json\n{\"intro\":\"x\",\"concepts\":[]}\n```";
        let parsed = parse_concepts_response(raw).expect("should parse");
        assert_eq!(parsed.concepts.len(), 0);
        assert_eq!(parsed.intro.as_deref(), Some("x"));
    }

    #[test]
    fn test_parse_concepts_response_extracts_embedded_json() {
        let raw = "Here is the output you requested:\n\
                   {\"intro\":\"y\",\"concepts\":[{\"name\":\"X\",\"definition\":\"d\"}]}\n\
                   Hope that helps!";
        let parsed = parse_concepts_response(raw).expect("should parse");
        assert_eq!(parsed.concepts.len(), 1);
        assert_eq!(parsed.concepts[0].name, "X");
    }

    #[test]
    fn test_parse_concepts_response_rejects_malformed() {
        let raw = "this is definitely not JSON at all";
        assert!(parse_concepts_response(raw).is_err());
    }

    #[test]
    fn test_render_with_concepts() {
        let data = GlossaryData {
            intro: Some(
                "Reflex catalogs the core pieces of a local code-search engine."
                    .to_string(),
            ),
            concepts: vec![
                Concept {
                    name: "Trigram Index".to_string(),
                    definition: "A fast inverted index built from three-character substrings."
                        .to_string(),
                    category: Some("Core Capabilities".to_string()),
                    related_modules: vec!["src/index".to_string(), "src/query".to_string()],
                },
                Concept {
                    name: "Symbol Cache".to_string(),
                    definition: "A persistent store of parsed language symbols.".to_string(),
                    category: Some("Data Model".to_string()),
                    related_modules: vec!["src/cache".to_string()],
                },
            ],
        };

        let md = render_glossary_markdown(&data);

        // Structural assertions
        assert!(md.contains("Reflex catalogs"));
        assert!(md.contains("## Core Capabilities"));
        assert!(md.contains("## Data Model"));
        assert!(md.contains("### Trigram Index"));
        assert!(md.contains("### Symbol Cache"));
        assert!(md.contains("> A fast inverted index"));
        assert!(md.contains("[`src/index`](/wiki/src-index/)"));
        assert!(md.contains("[`src/query`](/wiki/src-query/)"));
        assert!(md.contains("Implemented in"));

        // Must NOT contain v2 artifacts:
        assert!(!md.contains("```rust"), "no signature code blocks");
        assert!(!md.contains(":1"), "no file:line markers (cheap check)");
        assert!(!md.contains("| Symbol | Kind"), "no flat table");
    }

    #[test]
    fn test_render_no_llm_fallback() {
        let data = GlossaryData::default();
        let md = render_glossary_markdown(&data);
        assert!(md.contains("LLM narration pipeline"));
        assert!(md.contains("rfx pulse generate"));
    }

    #[test]
    fn test_render_no_llm_fallback_with_evidence_lists_modules() {
        let evidence = GlossaryEvidence {
            total_files: 10,
            total_lines: 500,
            language_mix: vec![],
            dependency_edges: 0,
            hotspot_files: vec![],
            modules: vec![
                ModuleEvidence {
                    path: "src".to_string(),
                    file_count: 5,
                    anchor_symbols: vec![],
                },
                ModuleEvidence {
                    path: "src/pulse".to_string(),
                    file_count: 3,
                    anchor_symbols: vec![],
                },
            ],
        };
        let md = render_glossary_no_llm(&evidence);
        assert!(md.contains("LLM narration pipeline"));
        assert!(md.contains("[`src`](/wiki/src/)"));
        assert!(md.contains("[`src/pulse`](/wiki/src-pulse/)"));
        assert!(md.contains("(5 files)"));
    }

    #[test]
    fn test_concepts_response_into_glossary_data() {
        let resp = ConceptsResponse {
            intro: Some("hi".to_string()),
            concepts: vec![RawConcept {
                name: "Concept".to_string(),
                definition: "def".to_string(),
                category: Some("Cat".to_string()),
                related_modules: vec!["src".to_string()],
            }],
        };
        let data: GlossaryData = resp.into();
        assert_eq!(data.concepts.len(), 1);
        assert_eq!(data.concepts[0].name, "Concept");
        assert_eq!(data.intro.as_deref(), Some("hi"));
    }
}