rqmd 0.0.1

rqmd: command-line interface (binary `rqmd`)
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
//! `rqmd bench` — run a fixture of labelled queries through the four search
//! backends (bm25 / vector / hybrid / full) and report precision@k, recall,
//! recall@1/3/5, MRR, F1, and latency.
//!
//! Port of qmd's `src/bench/bench.ts` (the runner half; pure scoring + the
//! fixture/result types live in `rqmd_core::bench`).

use std::collections::HashSet;
use std::sync::Arc;
use std::time::Instant;

use anyhow::{Context, Result, bail};
use indexmap::IndexMap;

use rqmd_core::Store;
use rqmd_core::bench::{
    BackendResult, BenchmarkFixture, BenchmarkQuery, BenchmarkResult, QueryResult, SummaryStats,
    score_results,
};
use rqmd_core::llm::config::resolve_embed_model;
use rqmd_core::llm::traits::Llm;
use rqmd_core::store::path::now_rfc3339;
use rqmd_core::store::search::search_fts;
use rqmd_core::store_ops::{
    ExpandedQuery, ExpandedQueryType, HybridQueryOptions, StructuredSearchOptions, hybrid_query,
    search_vec, structured_search,
};

use crate::cli::BenchArgs;
use crate::state::IndexState;

/// Backends run for every query, in fixed order (also the JSON key order).
const BACKENDS: [&str; 4] = ["bm25", "vector", "hybrid", "full"];

pub async fn run(args: BenchArgs, state: &mut IndexState) -> Result<()> {
    // Load fixture. serde already rejects a fixture missing `queries`
    // (non-optional field) as a parse error.
    let raw = std::fs::read_to_string(&args.fixture)
        .with_context(|| format!("reading fixture {}", args.fixture))?;
    let fixture: BenchmarkFixture =
        serde_json::from_str(&raw).with_context(|| format!("parsing fixture {}", args.fixture))?;

    let json = args.json;
    // CLI flag overrides the fixture's default collection.
    let collection = args
        .collection
        .clone()
        .or_else(|| fixture.collection.clone());
    let embed_model = resolve_embed_model(None);

    // Borrow order (see vsearch.rs): take the owned `Arc<LlamaCpp>` first, then
    // re-borrow `&Store`. All backend fns take `&Store`, so the immutable borrow
    // is held across the whole loop without conflict.
    let llm = state.llama_cpp()?;
    let store: &Store = state.store_mut()?;
    let llm_dyn: Arc<dyn Llm> = llm;

    let mut results: Vec<QueryResult> = Vec::new();
    for query in &fixture.queries {
        let mut backends: IndexMap<String, BackendResult> = IndexMap::new();
        for backend in BACKENDS {
            if !json {
                eprint!("  {} / {}...", query.id, backend);
            }
            let br = run_query(
                store,
                &llm_dyn,
                backend,
                query,
                collection.as_deref(),
                &embed_model,
            )
            .await;
            if !json {
                eprintln!(" {}ms", br.latency_ms);
            }
            backends.insert(backend.to_string(), br);
        }
        results.push(QueryResult {
            id: query.id.clone(),
            query: query.query.clone(),
            r#type: query.r#type.clone(),
            backends,
        });
    }

    let summary = compute_summary(&results);
    let bench_result = BenchmarkResult {
        timestamp: make_timestamp(),
        fixture: args.fixture.clone(),
        results,
        summary,
    };

    if json {
        println!("{}", serde_json::to_string_pretty(&bench_result)?);
    } else {
        println!("\n{}", format_table(&bench_result.results));
        println!("Summary:");
        println!("{}", "-".repeat(70));
        for (name, s) in &bench_result.summary {
            println!(
                "  {} P@k={} R@1={} R@3={} R@5={} MRR={} F1={} Avg={}ms",
                pad(name, 8),
                num3(s.avg_precision),
                num3(s.avg_recall_at_1),
                num3(s.avg_recall_at_3),
                num3(s.avg_recall_at_5),
                num3(s.avg_mrr),
                num3(s.avg_f1),
                s.avg_latency_ms.round() as i64,
            );
        }
    }

    Ok(())
}

/// One query against one backend, with latency + scoring. A backend error
/// (e.g. vector search with no embeddings) degrades to an all-zero result
/// instead of aborting the run — mirrors the TS `runQuery` try/catch.
async fn run_query(
    store: &Store,
    llm: &Arc<dyn Llm>,
    backend: &str,
    query: &BenchmarkQuery,
    collection: Option<&str>,
    embed_model: &str,
) -> BackendResult {
    let limit = query.expected_in_top_k.max(10);
    let total_expected = query.expected_files.len();
    let start = Instant::now();
    let outcome = run_backend(backend, store, llm, query, limit, collection, embed_model).await;
    let latency_ms = start.elapsed().as_millis();

    match outcome {
        Ok(files) => {
            let scores = score_results(&files, &query.expected_files, query.expected_in_top_k);
            let top_files = files.iter().take(10).cloned().collect();
            BackendResult::from_scores(scores, total_expected, latency_ms, top_files)
        }
        Err(_) => BackendResult::zeroed(total_expected, latency_ms, query.expected_files.clone()),
    }
}

/// Run one backend, returning the ordered list of result file paths.
async fn run_backend(
    backend: &str,
    store: &Store,
    llm: &Arc<dyn Llm>,
    query: &BenchmarkQuery,
    limit: usize,
    collection: Option<&str>,
    embed_model: &str,
) -> Result<Vec<String>> {
    let structured = parse_structured_query(&query.query)?;
    match backend {
        // BM25: lex-only when structured, else the raw query.
        "bm25" => {
            if let Some(s) = &structured {
                let mut files = Vec::new();
                for sub in s
                    .searches
                    .iter()
                    .filter(|q| q.type_ == ExpandedQueryType::Lex)
                {
                    let hits = store
                        .with_connection(|c| search_fts(c, &sub.query, Some(limit), collection))?;
                    files.extend(hits.iter().map(|r| r.doc.filepath.clone()));
                }
                Ok(unique_files(files, limit))
            } else {
                let hits = store
                    .with_connection(|c| search_fts(c, &query.query, Some(limit), collection))?;
                Ok(hits.iter().map(|r| r.doc.filepath.clone()).collect())
            }
        }
        // Vector: vec/hyde-only when structured, else the raw query.
        "vector" => {
            if let Some(s) = &structured {
                let mut files = Vec::new();
                for sub in s
                    .searches
                    .iter()
                    .filter(|q| matches!(q.type_, ExpandedQueryType::Vec | ExpandedQueryType::Hyde))
                {
                    let hits = search_vec(
                        store,
                        llm.clone(),
                        &sub.query,
                        embed_model,
                        limit,
                        collection,
                        None,
                    )
                    .await?;
                    files.extend(hits.iter().map(|r| r.doc.filepath.clone()));
                }
                Ok(unique_files(files, limit))
            } else {
                let hits = search_vec(
                    store,
                    llm.clone(),
                    &query.query,
                    embed_model,
                    limit,
                    collection,
                    None,
                )
                .await?;
                Ok(hits.iter().map(|r| r.doc.filepath.clone()).collect())
            }
        }
        // Hybrid (RRF, no rerank) / full (with LLM rerank).
        "hybrid" | "full" => {
            let skip_rerank = backend == "hybrid";
            let hits = if let Some(s) = &structured {
                structured_search(
                    store,
                    llm.clone(),
                    &s.searches,
                    StructuredSearchOptions {
                        collections: collection.map(|c| vec![c.to_string()]),
                        limit: Some(limit),
                        intent: s.intent.clone(),
                        skip_rerank,
                        ..Default::default()
                    },
                )
                .await?
            } else {
                hybrid_query(
                    store,
                    llm.clone(),
                    &query.query,
                    HybridQueryOptions {
                        collection: collection.map(|c| c.to_string()),
                        limit: Some(limit),
                        skip_rerank,
                        ..Default::default()
                    },
                )
                .await?
            };
            Ok(hits.iter().map(|r| r.file.clone()).collect())
        }
        other => bail!("unknown backend: {other}"),
    }
}

/// A parsed multi-line structured query.
#[derive(Debug)]
struct ParsedStructuredQuery {
    searches: Vec<ExpandedQuery>,
    intent: Option<String>,
}

/// Parse a structured query (lines prefixed `lex:` / `vec:` / `hyde:` /
/// `intent:`). Returns `None` for a plain single-line query. Port of
/// `parseStructuredQuery` (`bench.ts:46-94`); errors mirror the TS messages.
fn parse_structured_query(query: &str) -> Result<Option<ParsedStructuredQuery>> {
    // (1-based line number, trimmed text) for non-blank lines.
    let lines: Vec<(usize, &str)> = query
        .split('\n')
        .enumerate()
        .map(|(idx, line)| (idx + 1, line.trim()))
        .filter(|(_, t)| !t.is_empty())
        .collect();

    if lines.is_empty() {
        return Ok(None);
    }

    let mut searches: Vec<ExpandedQuery> = Vec::new();
    let mut intent: Option<String> = None;

    for (number, trimmed) in &lines {
        let lower = trimmed.to_lowercase();

        if lower.starts_with("intent:") {
            if intent.is_some() {
                bail!("Line {number}: only one intent: line is allowed per benchmark query.");
            }
            let text = trimmed["intent:".len()..].trim();
            if text.is_empty() {
                bail!("Line {number}: intent: must include text.");
            }
            intent = Some(text.to_string());
            continue;
        }

        if let Some((type_, prefix)) = match_prefix(&lower) {
            let text = trimmed[prefix.len()..].trim();
            if text.is_empty() {
                let name = &prefix[..prefix.len() - 1]; // drop the ':'
                bail!("Line {number} ({name}:) must include text.");
            }
            searches.push(ExpandedQuery {
                type_,
                query: text.to_string(),
                line: Some(*number),
            });
            continue;
        }

        // A lone line with no prefix is a plain query, not structured.
        if lines.len() == 1 {
            return Ok(None);
        }

        bail!("Line {number} is missing a lex:/vec:/hyde:/intent: prefix.");
    }

    if intent.is_some() && searches.is_empty() {
        bail!("intent: cannot appear alone. Add at least one lex:, vec:, or hyde: line.");
    }

    Ok(if searches.is_empty() {
        None
    } else {
        Some(ParsedStructuredQuery { searches, intent })
    })
}

/// Match a `lex:` / `vec:` / `hyde:` prefix on an already-lowercased line.
fn match_prefix(lower: &str) -> Option<(ExpandedQueryType, &'static str)> {
    if lower.starts_with("lex:") {
        Some((ExpandedQueryType::Lex, "lex:"))
    } else if lower.starts_with("vec:") {
        Some((ExpandedQueryType::Vec, "vec:"))
    } else if lower.starts_with("hyde:") {
        Some((ExpandedQueryType::Hyde, "hyde:"))
    } else {
        None
    }
}

/// Deduplicate file paths (first occurrence wins) and cap at `limit`.
fn unique_files(files: Vec<String>, limit: usize) -> Vec<String> {
    let mut seen: HashSet<String> = HashSet::new();
    let mut out: Vec<String> = Vec::new();
    for f in files {
        if seen.insert(f.clone()) {
            out.push(f);
            if out.len() >= limit {
                break;
            }
        }
    }
    out
}

/// Per-backend averaged metrics, in first-seen backend order.
fn compute_summary(results: &[QueryResult]) -> IndexMap<String, SummaryStats> {
    let mut names: Vec<String> = Vec::new();
    for r in results {
        for name in r.backends.keys() {
            if !names.iter().any(|n| n == name) {
                names.push(name.clone());
            }
        }
    }

    let mut summary: IndexMap<String, SummaryStats> = IndexMap::new();
    for name in names {
        let (mut p, mut rc, mut r1, mut r3, mut r5, mut mrr, mut f1, mut lat) =
            (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
        let mut count = 0u32;
        for r in results {
            if let Some(br) = r.backends.get(&name) {
                p += br.precision_at_k;
                rc += br.recall;
                r1 += br.recall_at_1;
                r3 += br.recall_at_3;
                r5 += br.recall_at_5;
                mrr += br.mrr;
                f1 += br.f1;
                lat += br.latency_ms as f64;
                count += 1;
            }
        }
        if count > 0 {
            let c = f64::from(count);
            summary.insert(
                name,
                SummaryStats {
                    avg_precision: p / c,
                    avg_recall: rc / c,
                    avg_recall_at_1: r1 / c,
                    avg_recall_at_3: r3 / c,
                    avg_recall_at_5: r5 / c,
                    avg_mrr: mrr / c,
                    avg_f1: f1 / c,
                    avg_latency_ms: lat / c,
                },
            );
        }
    }
    summary
}

/// ASCII results table. Reproduces qmd's `formatTable` (`bench.ts:209-229`)
/// padding exactly: header columns padded, data metrics `{:.2}` right-padded
/// to 5, latency right-padded to 7 + `ms`.
fn format_table(results: &[QueryResult]) -> String {
    let mut lines: Vec<String> = Vec::new();
    lines.push(format!(
        "{} {} {} {} {} {} {} {} {}",
        pad("Query", 25),
        pad("Backend", 8),
        pad("P@k", 6),
        pad("R@1", 6),
        pad("R@3", 6),
        pad("R@5", 6),
        pad("MRR", 6),
        pad("F1", 6),
        pad("ms", 8),
    ));
    lines.push("-".repeat(88));
    for r in results {
        for (backend, br) in &r.backends {
            lines.push(format!(
                "{} {} {} {} {} {} {} {} {:>7}ms",
                pad(&r.id, 25),
                pad(backend, 8),
                num2(br.precision_at_k),
                num2(br.recall_at_1),
                num2(br.recall_at_3),
                num2(br.recall_at_5),
                num2(br.mrr),
                num2(br.f1),
                br.latency_ms,
            ));
        }
        lines.push(String::new());
    }
    lines.join("\n")
}

/// `s.slice(0, n).padEnd(n)` — truncate to `n` chars then right-pad with spaces.
fn pad(s: &str, n: usize) -> String {
    let truncated: String = s.chars().take(n).collect();
    let len = truncated.chars().count();
    if len >= n {
        truncated
    } else {
        format!("{truncated}{}", " ".repeat(n - len))
    }
}

/// `x.toFixed(2).padStart(5)`.
fn num2(x: f64) -> String {
    let s = format!("{x:.2}");
    if s.len() >= 5 {
        s
    } else {
        format!("{}{s}", " ".repeat(5 - s.len()))
    }
}

/// `x.toFixed(3).padStart(6)`.
fn num3(x: f64) -> String {
    let s = format!("{x:.3}");
    if s.len() >= 6 {
        s
    } else {
        format!("{}{s}", " ".repeat(6 - s.len()))
    }
}

/// `new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)`.
fn make_timestamp() -> String {
    let iso = now_rfc3339(); // YYYY-MM-DDTHH:MM:SS.sssZ
    iso.chars()
        .filter(|c| *c != ':' && *c != '.')
        .take(15)
        .collect()
}

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

    #[test]
    fn plain_single_line_is_not_structured() {
        assert!(parse_structured_query("API versioning").unwrap().is_none());
    }

    #[test]
    fn blank_query_is_not_structured() {
        assert!(parse_structured_query("   \n  ").unwrap().is_none());
    }

    #[test]
    fn parses_prefixes_and_intent() {
        let parsed = parse_structured_query("lex: auth\nvec: secure sessions\nintent: security")
            .unwrap()
            .expect("structured");
        assert_eq!(parsed.searches.len(), 2);
        assert_eq!(parsed.searches[0].type_, ExpandedQueryType::Lex);
        assert_eq!(parsed.searches[0].query, "auth");
        assert_eq!(parsed.searches[1].type_, ExpandedQueryType::Vec);
        assert_eq!(parsed.intent.as_deref(), Some("security"));
    }

    #[test]
    fn prefix_is_case_insensitive() {
        let parsed = parse_structured_query("HYDE: expanded text")
            .unwrap()
            .expect("structured");
        assert_eq!(parsed.searches[0].type_, ExpandedQueryType::Hyde);
        assert_eq!(parsed.searches[0].query, "expanded text");
    }

    #[test]
    fn duplicate_intent_errors() {
        let err = parse_structured_query("intent: a\nintent: b").unwrap_err();
        assert!(err.to_string().contains("only one intent:"));
    }

    #[test]
    fn intent_only_errors() {
        let err = parse_structured_query("intent: security").unwrap_err();
        assert!(err.to_string().contains("intent: cannot appear alone"));
    }

    #[test]
    fn multiline_missing_prefix_errors() {
        let err = parse_structured_query("lex: auth\nplain line").unwrap_err();
        assert!(
            err.to_string()
                .contains("missing a lex:/vec:/hyde:/intent: prefix")
        );
    }

    #[test]
    fn empty_prefix_text_errors() {
        let err = parse_structured_query("lex:   \nvec: x").unwrap_err();
        assert!(err.to_string().contains("(lex:) must include text"));
    }

    #[test]
    fn unique_files_dedups_and_caps() {
        let files = vec![
            "a".to_string(),
            "b".to_string(),
            "a".to_string(),
            "c".to_string(),
        ];
        assert_eq!(unique_files(files.clone(), 2), vec!["a", "b"]);
        assert_eq!(unique_files(files, 10), vec!["a", "b", "c"]);
    }

    #[test]
    fn pad_truncates_and_fills() {
        assert_eq!(pad("abc", 5), "abc  ");
        assert_eq!(pad("abcdef", 3), "abc");
    }

    #[test]
    fn num_formatting_matches_tofixed_padstart() {
        assert_eq!(num2(1.0), " 1.00");
        assert_eq!(num2(0.5), " 0.50");
        assert_eq!(num3(1.0), " 1.000");
        assert_eq!(num3(0.333_333), " 0.333");
    }
}