kbolt-core 0.1.1

Core engine for kbolt local-first retrieval
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
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

use kbolt_types::{EvalCase, EvalDataset, EvalImportReport, EvalJudgment, KboltError};
use serde::Deserialize;

use crate::Result;

const DEFAULT_SPACE: &str = "bench";
const CORPUS_DIRNAME: &str = "corpus";
const MANIFEST_FILENAME: &str = "eval.toml";

#[derive(Debug, Deserialize)]
struct BeirCorpusRecord {
    #[serde(rename = "_id")]
    id: String,
    #[serde(default)]
    title: Option<String>,
    text: String,
}

#[derive(Debug, Deserialize)]
struct BeirQueryRecord {
    #[serde(rename = "_id")]
    id: String,
    text: String,
}

pub fn import_beir(
    dataset: &str,
    source: &Path,
    output: &Path,
    collection: Option<&str>,
) -> Result<EvalImportReport> {
    let dataset = normalize_import_name("dataset", dataset)?;
    let collection = normalize_import_name("collection", collection.unwrap_or(dataset))?;
    let layout = validate_beir_layout(dataset, source)?;
    prepare_output_dir(output)?;

    let corpus_dir = output.join(CORPUS_DIRNAME);
    fs::create_dir_all(&corpus_dir)?;

    let document_ids = materialize_beir_corpus(dataset, &layout.corpus, &corpus_dir)?;
    let queries = load_beir_queries(dataset, &layout.queries)?;
    let (judgments_by_query, judgment_count) =
        load_beir_qrels(dataset, collection, &layout.qrels, &document_ids, &queries)?;
    let eval_dataset = build_eval_dataset(dataset, collection, queries, judgments_by_query)?;
    let query_count = eval_dataset.cases.len();
    let manifest_path = output.join(MANIFEST_FILENAME);
    fs::write(&manifest_path, toml::to_string_pretty(&eval_dataset)?)?;

    Ok(EvalImportReport {
        dataset: dataset.to_string(),
        source: source.display().to_string(),
        output_dir: output.display().to_string(),
        corpus_dir: corpus_dir.display().to_string(),
        manifest_path: manifest_path.display().to_string(),
        default_space: DEFAULT_SPACE.to_string(),
        collection: collection.to_string(),
        document_count: document_ids.len(),
        query_count,
        judgment_count,
    })
}

struct BeirLayout {
    corpus: PathBuf,
    queries: PathBuf,
    qrels: PathBuf,
}

fn validate_beir_layout(dataset: &str, source: &Path) -> Result<BeirLayout> {
    if !source.is_dir() {
        return Err(KboltError::InvalidInput(format!(
            "{dataset} source must be a directory: {}",
            source.display()
        ))
        .into());
    }

    let corpus = source.join("corpus.jsonl");
    let queries = source.join("queries.jsonl");
    let qrels = source.join("qrels").join("test.tsv");

    for (label, path) in [
        ("corpus.jsonl", &corpus),
        ("queries.jsonl", &queries),
        ("qrels/test.tsv", &qrels),
    ] {
        if !path.is_file() {
            return Err(KboltError::InvalidInput(format!(
                "invalid {dataset} source {}: missing {label}",
                source.display()
            ))
            .into());
        }
    }

    Ok(BeirLayout {
        corpus,
        queries,
        qrels,
    })
}

fn prepare_output_dir(output: &Path) -> Result<()> {
    if output.exists() {
        if !output.is_dir() {
            return Err(KboltError::InvalidInput(format!(
                "eval import output must be a directory: {}",
                output.display()
            ))
            .into());
        }
        if fs::read_dir(output)?.next().transpose()?.is_some() {
            return Err(KboltError::InvalidInput(format!(
                "eval import output directory must be empty: {}",
                output.display()
            ))
            .into());
        }
        return Ok(());
    }

    fs::create_dir_all(output)?;
    Ok(())
}

fn materialize_beir_corpus(
    dataset: &str,
    corpus_file: &Path,
    corpus_dir: &Path,
) -> Result<HashSet<String>> {
    let records = read_jsonl::<BeirCorpusRecord>(corpus_file, "corpus")?;
    if records.is_empty() {
        return Err(KboltError::InvalidInput(format!(
            "{dataset} corpus is empty: {}",
            corpus_file.display()
        ))
        .into());
    }

    let mut ids = HashSet::with_capacity(records.len());
    for record in records {
        validate_record_id("corpus document", &record.id)?;
        if !ids.insert(record.id.clone()) {
            return Err(KboltError::InvalidInput(format!(
                "duplicate corpus document id '{}'",
                record.id
            ))
            .into());
        }
        let document_path = corpus_dir.join(format!("{}.md", record.id));
        fs::write(document_path, render_corpus_document(&record))?;
    }

    Ok(ids)
}

fn render_corpus_document(record: &BeirCorpusRecord) -> String {
    let title = record.title.as_deref().map(str::trim).unwrap_or_default();
    let text = record.text.trim();
    if title.is_empty() {
        format!("{text}\n")
    } else {
        format!("# {title}\n\n{text}\n")
    }
}

fn load_beir_queries(dataset: &str, queries_file: &Path) -> Result<Vec<BeirQueryRecord>> {
    let queries = read_jsonl::<BeirQueryRecord>(queries_file, "queries")?;
    if queries.is_empty() {
        return Err(KboltError::InvalidInput(format!(
            "{dataset} queries are empty: {}",
            queries_file.display()
        ))
        .into());
    }

    let mut seen = HashSet::with_capacity(queries.len());
    for query in &queries {
        validate_record_id("query", &query.id)?;
        if !seen.insert(query.id.clone()) {
            return Err(
                KboltError::InvalidInput(format!("duplicate query id '{}'", query.id)).into(),
            );
        }
        if query.text.trim().is_empty() {
            return Err(
                KboltError::InvalidInput(format!("query '{}' has empty text", query.id)).into(),
            );
        }
    }

    Ok(queries)
}

fn load_beir_qrels(
    dataset: &str,
    collection: &str,
    qrels_file: &Path,
    document_ids: &HashSet<String>,
    queries: &[BeirQueryRecord],
) -> Result<(HashMap<String, Vec<EvalJudgment>>, usize)> {
    let query_ids = queries
        .iter()
        .map(|query| query.id.as_str())
        .collect::<HashSet<_>>();
    let file = fs::File::open(qrels_file)?;
    let reader = BufReader::new(file);
    let mut judgments_by_query: HashMap<String, Vec<EvalJudgment>> = HashMap::new();
    let mut seen_pairs = HashSet::new();
    let mut judgment_count = 0;

    for (index, line) in reader.lines().enumerate() {
        let line_number = index + 1;
        let line = line?;
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        if line_number == 1 && trimmed.eq_ignore_ascii_case("query-id\tcorpus-id\tscore") {
            continue;
        }

        let fields = trimmed.split('\t').collect::<Vec<_>>();
        if fields.len() != 3 {
            return Err(KboltError::InvalidInput(format!(
                "invalid {dataset} qrels line {} in {}: expected 3 tab-separated fields",
                line_number,
                qrels_file.display()
            ))
            .into());
        }

        let query_id = fields[0].trim();
        let document_id = fields[1].trim();
        let relevance = fields[2].trim().parse::<u8>().map_err(|err| {
            KboltError::InvalidInput(format!(
                "invalid relevance '{}' on qrels line {} in {}: {err}",
                fields[2].trim(),
                line_number,
                qrels_file.display()
            ))
        })?;

        validate_record_id("query", query_id)?;
        validate_record_id("corpus document", document_id)?;

        if !query_ids.contains(query_id) {
            return Err(KboltError::InvalidInput(format!(
                "qrels references unknown query id '{}' in {}",
                query_id,
                qrels_file.display()
            ))
            .into());
        }
        if !document_ids.contains(document_id) {
            return Err(KboltError::InvalidInput(format!(
                "qrels references unknown corpus document id '{}' in {}",
                document_id,
                qrels_file.display()
            ))
            .into());
        }
        if relevance == 0 {
            continue;
        }
        if !seen_pairs.insert((query_id.to_string(), document_id.to_string())) {
            return Err(KboltError::InvalidInput(format!(
                "duplicate qrels pair '{} -> {}' in {}",
                query_id,
                document_id,
                qrels_file.display()
            ))
            .into());
        }

        judgments_by_query
            .entry(query_id.to_string())
            .or_default()
            .push(EvalJudgment {
                path: format!("{collection}/{document_id}.md"),
                relevance,
            });
        judgment_count += 1;
    }

    Ok((judgments_by_query, judgment_count))
}

fn build_eval_dataset(
    dataset: &str,
    collection: &str,
    queries: Vec<BeirQueryRecord>,
    mut judgments_by_query: HashMap<String, Vec<EvalJudgment>>,
) -> Result<EvalDataset> {
    let mut cases = Vec::new();
    for query in queries {
        let Some(mut judgments) = judgments_by_query.remove(&query.id) else {
            continue;
        };
        judgments.sort_by(|left, right| {
            right
                .relevance
                .cmp(&left.relevance)
                .then_with(|| left.path.cmp(&right.path))
        });
        cases.push(EvalCase {
            query: query.text.trim().to_string(),
            space: Some(DEFAULT_SPACE.to_string()),
            collections: vec![collection.to_string()],
            judgments,
        });
    }

    if cases.is_empty() {
        return Err(KboltError::InvalidInput(format!(
            "{dataset} qrels did not produce any positive judged queries"
        ))
        .into());
    }

    Ok(EvalDataset { cases })
}

fn read_jsonl<T>(path: &Path, label: &str) -> Result<Vec<T>>
where
    T: for<'de> Deserialize<'de>,
{
    let file = fs::File::open(path)?;
    let reader = BufReader::new(file);
    let mut records = Vec::new();
    for (index, line) in reader.lines().enumerate() {
        let line_number = index + 1;
        let line = line?;
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let record = serde_json::from_str(trimmed).map_err(|err| {
            KboltError::InvalidInput(format!(
                "invalid {label} jsonl line {} in {}: {err}",
                line_number,
                path.display()
            ))
        })?;
        records.push(record);
    }
    Ok(records)
}

fn validate_record_id(kind: &str, value: &str) -> Result<()> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(KboltError::InvalidInput(format!("{kind} id must not be empty")).into());
    }
    if trimmed == "." || trimmed == ".." || trimmed.contains('/') || trimmed.contains('\\') {
        return Err(KboltError::InvalidInput(format!(
            "{kind} id '{}' is not a valid filesystem-safe identifier",
            value
        ))
        .into());
    }
    Ok(())
}

fn normalize_import_name<'a>(kind: &str, value: &'a str) -> Result<&'a str> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return Err(KboltError::InvalidInput(format!("{kind} name must not be empty")).into());
    }
    validate_record_id(kind, trimmed)?;
    Ok(trimmed)
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::Path;

    use tempfile::tempdir;

    use crate::eval_store::load_eval_dataset_with_file;

    use super::import_beir;

    #[test]
    fn import_beir_materializes_corpus_and_manifest() {
        let tmp = tempdir().expect("create tempdir");
        let source = tmp.path().join("source");
        let output = tmp.path().join("output");
        write_beir_fixture(&source);

        let report = import_beir("scifact", &source, &output, None).expect("import benchmark");

        assert_eq!(report.dataset, "scifact");
        assert_eq!(report.default_space, "bench");
        assert_eq!(report.collection, "scifact");
        assert_eq!(report.document_count, 2);
        assert_eq!(report.query_count, 2);
        assert_eq!(report.judgment_count, 3);
        assert_eq!(
            fs::read_to_string(output.join("corpus/10.md")).expect("read corpus doc"),
            "# Alpha Evidence\n\nAlpha evidence text.\n"
        );

        let dataset = load_eval_dataset_with_file(tmp.path(), Some(&output.join("eval.toml")))
            .expect("load imported manifest");
        assert_eq!(dataset.cases.len(), 2);
        assert_eq!(dataset.cases[0].space.as_deref(), Some("bench"));
        assert_eq!(dataset.cases[0].collections, vec!["scifact".to_string()]);
        assert_eq!(dataset.cases[0].judgments[0].path, "scifact/10.md");
        assert_eq!(dataset.cases[0].judgments[0].relevance, 2);
    }

    #[test]
    fn import_beir_honors_collection_override() {
        let tmp = tempdir().expect("create tempdir");
        let source = tmp.path().join("source");
        let output = tmp.path().join("output");
        write_beir_fixture(&source);

        let report =
            import_beir("fiqa", &source, &output, Some("finance")).expect("import benchmark");

        assert_eq!(report.dataset, "fiqa");
        assert_eq!(report.collection, "finance");

        let dataset = load_eval_dataset_with_file(tmp.path(), Some(&output.join("eval.toml")))
            .expect("load imported manifest");
        assert_eq!(dataset.cases[0].collections, vec!["finance".to_string()]);
        assert_eq!(dataset.cases[0].judgments[0].path, "finance/10.md");
    }

    #[test]
    fn import_beir_accepts_missing_titles() {
        let tmp = tempdir().expect("create tempdir");
        let source = tmp.path().join("source");
        let output = tmp.path().join("output");
        write_beir_fixture_with_missing_title(&source);

        import_beir("fiqa", &source, &output, None).expect("import benchmark");

        assert_eq!(
            fs::read_to_string(output.join("corpus/10.md")).expect("read corpus doc"),
            "Alpha evidence text.\n"
        );
    }

    #[test]
    fn import_beir_rejects_nonempty_output_directory() {
        let tmp = tempdir().expect("create tempdir");
        let source = tmp.path().join("source");
        let output = tmp.path().join("output");
        write_beir_fixture(&source);
        fs::create_dir_all(&output).expect("create output");
        fs::write(output.join("keep.txt"), "existing").expect("write sentinel");

        let err =
            import_beir("fiqa", &source, &output, None).expect_err("nonempty output should fail");

        assert!(
            err.to_string()
                .contains("eval import output directory must be empty"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn import_beir_rejects_missing_test_split() {
        let tmp = tempdir().expect("create tempdir");
        let source = tmp.path().join("source");
        fs::create_dir_all(source.join("qrels")).expect("create qrels dir");
        fs::write(
            source.join("corpus.jsonl"),
            "{\"_id\":\"10\",\"title\":\"Alpha Evidence\",\"text\":\"Alpha evidence text.\"}\n",
        )
        .expect("write corpus");
        fs::write(
            source.join("queries.jsonl"),
            "{\"_id\":\"q1\",\"text\":\"alpha evidence\"}\n",
        )
        .expect("write queries");

        let err = import_beir("fiqa", &source, &tmp.path().join("output"), None)
            .expect_err("missing qrels/test.tsv should fail");

        assert!(
            err.to_string().contains("missing qrels/test.tsv"),
            "unexpected error: {err}"
        );
    }

    fn write_beir_fixture(root: &Path) {
        fs::create_dir_all(root.join("qrels")).expect("create qrels dir");
        fs::write(
            root.join("corpus.jsonl"),
            concat!(
                "{\"_id\":\"10\",\"title\":\"Alpha Evidence\",\"text\":\"Alpha evidence text.\"}\n",
                "{\"_id\":\"20\",\"title\":\"Beta Study\",\"text\":\"Beta study text.\"}\n"
            ),
        )
        .expect("write corpus");
        fs::write(
            root.join("queries.jsonl"),
            concat!(
                "{\"_id\":\"q1\",\"text\":\"alpha evidence\"}\n",
                "{\"_id\":\"q2\",\"text\":\"beta study\"}\n"
            ),
        )
        .expect("write queries");
        fs::write(
            root.join("qrels").join("test.tsv"),
            concat!(
                "query-id\tcorpus-id\tscore\n",
                "q1\t10\t2\n",
                "q1\t20\t1\n",
                "q2\t20\t1\n",
                "q2\t10\t0\n"
            ),
        )
        .expect("write qrels");
    }

    fn write_beir_fixture_with_missing_title(root: &Path) {
        fs::create_dir_all(root.join("qrels")).expect("create qrels dir");
        fs::write(
            root.join("corpus.jsonl"),
            "{\"_id\":\"10\",\"text\":\"Alpha evidence text.\"}\n",
        )
        .expect("write corpus");
        fs::write(
            root.join("queries.jsonl"),
            "{\"_id\":\"q1\",\"text\":\"alpha evidence\"}\n",
        )
        .expect("write queries");
        fs::write(
            root.join("qrels").join("test.tsv"),
            concat!("query-id\tcorpus-id\tscore\n", "q1\t10\t2\n"),
        )
        .expect("write qrels");
    }
}