trusty-search 0.27.2

Machine-wide hybrid code search service: BM25 + vector + KG, zero cold-start, MCP server
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
//! Selective/lazy warm-boot: cold-index store and on-demand load (issue #993).
//!
//! Why: trusty-search currently warm-boots ALL persisted indexes at startup,
//! even when the operator only uses a handful regularly. At 100+ registered
//! indexes, startup takes minutes and exposes TCC-denial hang paths (#718) for
//! every index. `TRUSTY_WARMBOOT_MAX_INDEXES` lets operators limit the number of
//! indexes that are eagerly loaded; the rest are parked here as "cold" and loaded
//! transparently on the first query that touches them.
//!
//! Architecture:
//!   - `env` — `warmboot_max_indexes()`, `cold_reload_timeout()`, and
//!     `LAST_QUERIED_WRITE_INTERVAL_SECS`.
//!   - `store` — `ColdIndexStore` + `select_warmboot_entries()`.
//!   - `loader` — `get_or_load_index()` + `LazyLoadError`.
//!
//! Back-compat: when `TRUSTY_WARMBOOT_MAX_INDEXES` is unset, `select_warmboot_entries`
//! returns all entries as eager and the cold store is empty — exact same behaviour
//! as the pre-#993 daemon.
//!
//! Test: `select_warmboot_entries_*`, `cold_reload_timeout_parses_env`,
//!       `warmboot_max_indexes_parses_env`, `get_or_load_index_*`.

mod env;
mod loader;
mod store;

// Re-export all public symbols so callers that use
// `crate::service::lazy_loader::*` need no changes.
pub use env::{cold_reload_timeout, warmboot_max_indexes, LAST_QUERIED_WRITE_INTERVAL_SECS};
pub use loader::{get_or_load_index, LazyLoadError};
pub use store::{select_warmboot_entries, ColdIndexStore};

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;
    use std::time::Duration;

    use crate::core::registry::{IndexId, IndexRegistry};
    use crate::service::persistence::PersistedIndex;

    fn mk_entry(id: &str, q: Option<u64>, i: Option<u64>) -> PersistedIndex {
        PersistedIndex {
            id: id.to_string(),
            root_path: PathBuf::from(format!("/tmp/{id}")),
            last_queried_unix: q,
            last_indexed_unix: i,
            ..Default::default()
        }
    }

    /// Create a minimal `IndexHandle` for tests without touching the filesystem.
    fn build_mock_handle(id: &str) -> crate::core::registry::IndexHandle {
        use std::sync::Arc;
        use tokio::sync::RwLock;

        let index_id = IndexId::new(id.to_string());
        let root_path = PathBuf::from(format!("/tmp/test-{id}"));
        let indexer = Arc::new(RwLock::new(crate::core::indexer::CodeIndexer::new(
            id, &root_path,
        )));
        crate::core::registry::IndexHandle::bare(index_id, indexer, root_path)
    }

    // ── warmboot_max_indexes ─────────────────────────────────────────────────

    /// Why: env var absent → None (back-compat warm-boot-all).
    /// Test: this test.
    #[test]
    #[serial_test::serial]
    fn warmboot_max_indexes_unset_returns_none() {
        unsafe { std::env::remove_var("TRUSTY_WARMBOOT_MAX_INDEXES") };
        assert!(warmboot_max_indexes().is_none());
    }

    /// Why: `0` → lazy-load everything.
    /// Test: this test.
    #[test]
    #[serial_test::serial]
    fn warmboot_max_indexes_zero_returns_some_zero() {
        unsafe { std::env::set_var("TRUSTY_WARMBOOT_MAX_INDEXES", "0") };
        assert_eq!(warmboot_max_indexes(), Some(0));
        unsafe { std::env::remove_var("TRUSTY_WARMBOOT_MAX_INDEXES") };
    }

    /// Why: valid positive value parses correctly.
    /// Test: this test.
    #[test]
    #[serial_test::serial]
    fn warmboot_max_indexes_parses_env() {
        unsafe { std::env::set_var("TRUSTY_WARMBOOT_MAX_INDEXES", "10") };
        assert_eq!(warmboot_max_indexes(), Some(10));
        unsafe { std::env::remove_var("TRUSTY_WARMBOOT_MAX_INDEXES") };
    }

    // ── cold_reload_timeout ──────────────────────────────────────────────────

    /// Why: env var absent → 30 s default.
    /// Test: this test.
    #[test]
    #[serial_test::serial]
    fn cold_reload_timeout_default_is_30s() {
        unsafe { std::env::remove_var("TRUSTY_INDEX_COLD_RELOAD_TIMEOUT_SECS") };
        assert_eq!(cold_reload_timeout(), Duration::from_secs(30));
    }

    /// Why: explicit value parses correctly.
    /// Test: this test.
    #[test]
    #[serial_test::serial]
    fn cold_reload_timeout_parses_env() {
        unsafe { std::env::set_var("TRUSTY_INDEX_COLD_RELOAD_TIMEOUT_SECS", "15") };
        assert_eq!(cold_reload_timeout(), Duration::from_secs(15));
        unsafe { std::env::remove_var("TRUSTY_INDEX_COLD_RELOAD_TIMEOUT_SECS") };
    }

    // ── select_warmboot_entries ──────────────────────────────────────────────

    /// Why: `None` cap → all eager, nothing cold (back-compat).
    /// Test: this test.
    #[test]
    fn select_all_eager_when_no_cap() {
        let entries = vec![mk_entry("a", None, None), mk_entry("b", Some(100), None)];
        let (eager, cold) = select_warmboot_entries(entries.clone(), None);
        assert_eq!(eager.len(), 2);
        assert!(cold.is_empty());
    }

    /// Why: cap 0 → nothing eager, all cold.
    /// Test: this test.
    #[test]
    fn select_all_cold_when_cap_zero() {
        let entries = vec![mk_entry("a", None, None), mk_entry("b", Some(100), None)];
        let (eager, cold) = select_warmboot_entries(entries, Some(0));
        assert!(eager.is_empty());
        assert_eq!(cold.len(), 2);
    }

    /// Why: cap >= len → all eager, nothing cold.
    /// Test: this test.
    #[test]
    fn select_all_eager_when_cap_exceeds_count() {
        let entries = vec![mk_entry("a", Some(1), None), mk_entry("b", Some(2), None)];
        let (eager, cold) = select_warmboot_entries(entries, Some(10));
        assert_eq!(eager.len(), 2);
        assert!(cold.is_empty());
    }

    /// Why: top-N by recency is selected correctly; sort is deterministic.
    /// Test: this test.
    #[test]
    fn select_top_n_by_recency() {
        // a: sort_key=0 (no activity), b: 200, c: 300, d: 150
        let entries = vec![
            mk_entry("a", None, None),
            mk_entry("b", Some(200), None),
            mk_entry("c", Some(300), None),
            mk_entry("d", None, Some(150)),
        ];
        let (eager, cold) = select_warmboot_entries(entries, Some(2));
        assert_eq!(eager.len(), 2);
        assert_eq!(cold.len(), 2);
        // Top-2 by descending sort_key: c(300), b(200).
        let eager_ids: Vec<&str> = eager.iter().map(|e| e.id.as_str()).collect();
        assert!(
            eager_ids.contains(&"c"),
            "c (sort_key=300) must be in eager: {eager_ids:?}"
        );
        assert!(
            eager_ids.contains(&"b"),
            "b (sort_key=200) must be in eager: {eager_ids:?}"
        );
    }

    /// Why: tie-break by id ascending is deterministic across restarts.
    /// Test: this test.
    #[test]
    fn select_tie_breaks_by_id_ascending() {
        // All three have sort_key=100; tie-break by id: "aaa" < "bbb" < "ccc".
        let entries = vec![
            mk_entry("ccc", Some(100), None),
            mk_entry("aaa", Some(100), None),
            mk_entry("bbb", Some(100), None),
        ];
        let (eager, cold) = select_warmboot_entries(entries, Some(2));
        let eager_ids: Vec<&str> = eager.iter().map(|e| e.id.as_str()).collect();
        // aaa and bbb win the tie-break (alpha ascending).
        assert!(eager_ids.contains(&"aaa"), "aaa expected in eager");
        assert!(eager_ids.contains(&"bbb"), "bbb expected in eager");
        let cold_ids: Vec<&str> = cold.iter().map(|e| e.id.as_str()).collect();
        assert!(cold_ids.contains(&"ccc"), "ccc expected in cold");
    }

    // ── ColdIndexStore ───────────────────────────────────────────────────────

    /// Why: register, contains, len sanity checks.
    /// Test: this test.
    #[test]
    fn cold_store_register_and_contains() {
        let store = ColdIndexStore::new();
        assert!(store.is_empty());
        let entries = vec![
            mk_entry("idx1", None, None),
            mk_entry("idx2", Some(1), None),
        ];
        store.register_cold_entries(entries);
        assert_eq!(store.len(), 2);
        assert!(store.contains(&IndexId::new("idx1".to_string())));
        assert!(store.contains(&IndexId::new("idx2".to_string())));
        assert!(!store.contains(&IndexId::new("unknown".to_string())));
    }

    /// Why: `mark_loaded` removes the entry from the cold store.
    /// Test: this test.
    #[test]
    fn cold_store_len() {
        let store = ColdIndexStore::new();
        store.register_cold_entries(vec![mk_entry("a", None, None)]);
        assert_eq!(store.len(), 1);
        store.mark_loaded(&IndexId::new("a".to_string()));
        assert_eq!(store.len(), 0);
    }

    // ── get_or_load_index ────────────────────────────────────────────────────

    /// Why: hot-path fast path — index already in registry returns immediately.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_hot_path() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("hot-idx".to_string());
        registry.register(build_mock_handle("hot-idx"));

        let result = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| async {
            false // should never be called
        })
        .await;
        assert!(result.is_ok(), "hot-path should return Ok");
    }

    /// Why: unknown id (neither hot nor cold) returns NotFound.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_not_found() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("no-such".to_string());

        let result = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| async {
            false
        })
        .await;
        assert!(
            matches!(result, Err(LazyLoadError::NotFound)),
            "unknown id must return NotFound"
        );
    }

    /// Why: cold index loads on demand and returns the handle.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_loads_cold_index() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("cold-idx".to_string());
        cold.register_cold_entries(vec![mk_entry("cold-idx", None, None)]);

        // Restore function: register the handle then return true.
        let registry_clone = registry.clone();
        let result = get_or_load_index(
            &id,
            &registry,
            &cold,
            Duration::from_secs(5),
            move |_e| async move {
                registry_clone.register(build_mock_handle("cold-idx"));
                true
            },
        )
        .await;
        assert!(result.is_ok(), "cold index should load successfully");
        // After load, cold store should no longer contain the id.
        assert!(!cold.contains(&id), "cold store must be cleared after load");
    }

    /// PR #1103 TOCTOU: when `loading_gate` returns `None` (concurrent
    /// `mark_loaded` removed the cold entry between the cold-check and the gate
    /// call), `get_or_load_index` must re-check the hot registry and return the
    /// handle if it is now there — NOT return `NotFound`.
    ///
    /// Why: without this fix, a concurrent load that races ahead causes a
    /// spurious 404 for an index that actually just became hot.
    /// What: simulate the race by: (1) register the cold entry; (2) start
    /// `get_or_load_index`; (3) before the restore_fn runs, concurrently call
    /// `mark_loaded` + register the handle directly; (4) assert the call
    /// returns Ok (not NotFound).
    ///
    /// Note: because `mark_loaded` is called inside the restore_fn here, the
    /// single-threaded async executor serialises them. We test the post-gate
    /// re-check path (step 4 in the doc) where the index is hot after the gate
    /// is acquired.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_gate_none_but_index_just_became_hot() {
        // Simulate the race: load the cold entry, but before `loading_gate` is
        // called, another task calls `mark_loaded` and registers the handle in
        // the hot registry. We model this by calling `mark_loaded` inside the
        // restore_fn (which runs AFTER the gate is acquired), so by the time the
        // post-load `registry.get(id)` runs, the handle is already there.
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("race-idx".to_string());
        cold.register_cold_entries(vec![mk_entry("race-idx", None, None)]);

        let registry_clone = registry.clone();
        let cold_clone = cold.clone();
        let id_clone = id.clone();
        let result = get_or_load_index(
            &id,
            &registry,
            &cold,
            Duration::from_secs(5),
            move |_e| async move {
                // Simulate: another task already loaded the index; it registered
                // the handle and called mark_loaded. We do both here to ensure
                // the post-gate hot re-check returns the handle.
                registry_clone.register(build_mock_handle("race-idx"));
                cold_clone.mark_loaded(&id_clone);
                true
            },
        )
        .await;
        // The restore_fn returned true and registered the handle, so we get Ok.
        assert!(
            result.is_ok(),
            "index loaded by restore_fn must return Ok, not NotFound"
        );
    }

    /// Why: timeout returns Loading error with retry_after_secs.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_returns_loading_on_timeout() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("slow-idx".to_string());
        cold.register_cold_entries(vec![mk_entry("slow-idx", None, None)]);

        let result = get_or_load_index(
            &id,
            &registry,
            &cold,
            Duration::from_millis(50), // very short timeout
            |_e| async {
                tokio::time::sleep(Duration::from_secs(5)).await;
                true
            },
        )
        .await;
        assert!(
            matches!(result, Err(LazyLoadError::Loading { .. })),
            "timeout must return Loading error"
        );
    }

    // ── issue #1106: mark_failed / is_failed / failed_len ───────────────────

    /// Why: `mark_failed` must remove the entry from `entries` so `len()` and
    /// `contains()` return the honest state after a failed restore.
    /// Test: this test.
    #[test]
    fn cold_store_mark_failed_evicts_from_entries() {
        let store = ColdIndexStore::new();
        store.register_cold_entries(vec![mk_entry("f1", None, None)]);
        let id = IndexId::new("f1".to_string());

        assert!(store.contains(&id), "must be in entries before mark_failed");
        assert_eq!(store.len(), 1);
        assert!(!store.is_failed(&id));

        store.mark_failed(&id);

        assert!(
            !store.contains(&id),
            "must NOT be in entries after mark_failed"
        );
        assert_eq!(store.len(), 0, "entries len must decrease to 0");
        assert!(store.is_failed(&id), "must appear in failed_entries");
    }

    /// Why: `failed_len()` must count permanently-failed entries separately
    /// from `len()` (pending) so the health metric is honest.
    /// Test: this test.
    #[test]
    fn cold_store_mark_failed_failed_len() {
        let store = ColdIndexStore::new();
        store.register_cold_entries(vec![mk_entry("fa", None, None), mk_entry("fb", None, None)]);
        assert_eq!(store.failed_len(), 0);
        assert_eq!(store.len(), 2);

        store.mark_failed(&IndexId::new("fa".to_string()));
        assert_eq!(store.failed_len(), 1);
        assert_eq!(store.len(), 1);

        store.mark_failed(&IndexId::new("fb".to_string()));
        assert_eq!(store.failed_len(), 2);
        assert_eq!(store.len(), 0);
    }

    /// Why: when `restore_fn` returns `false`, `get_or_load_index` must (a)
    /// return `RestoreFailed`, (b) move the entry to `failed_entries` so
    /// `len()` decreases and `is_failed()` returns `true`, and (c) NOT leave
    /// the entry in `entries` (which would cause the next call to re-enter the
    /// expensive restore path — the bug described in issue #1106).
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_restore_false_marks_failed() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("fail-idx".to_string());
        cold.register_cold_entries(vec![mk_entry("fail-idx", None, None)]);

        // First call: restore_fn returns false.
        let result = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| async {
            false
        })
        .await;

        assert!(
            matches!(result, Err(LazyLoadError::RestoreFailed)),
            "restore_fn=false must return RestoreFailed"
        );
        assert!(
            !cold.contains(&id),
            "entry must be evicted from entries after restore failure"
        );
        assert!(
            cold.is_failed(&id),
            "entry must appear in failed_entries after restore failure"
        );
        assert_eq!(
            cold.len(),
            0,
            "indexes_lazy must be 0 after restore failure"
        );
        assert_eq!(cold.failed_len(), 1, "indexes_failed must be 1");
    }

    /// Issue #1125 TOCTOU: after the loading gate is acquired, if `is_failed`
    /// returns `true` (a concurrent thread already called `mark_failed` on the
    /// same id), `get_or_load_index` must return `RestoreFailed` immediately
    /// without invoking `restore_fn` again.
    ///
    /// Why: without this re-check, a second concurrent caller that arrives after
    /// the first `mark_failed` could still slip through the gate and call
    /// `restore_fn` a second time for the same first-failure event — potentially
    /// hammering a blocked volume or deleted root_path again.
    ///
    /// Simulation: because real concurrency in a single-threaded test executor is
    /// hard to orchestrate, we simulate the observable post-gate state directly:
    /// register the cold entry, then call `mark_failed` manually to move it to
    /// `failed_entries` (mirroring what a concurrent thread would have done
    /// before releasing the gate), then invoke `get_or_load_index`. The loader
    /// should short-circuit at step 4b (post-gate `is_failed` re-check) and
    /// return `RestoreFailed` without calling `restore_fn`.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_gate_recheck_is_failed_short_circuits() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("toctou-failed-idx".to_string());
        cold.register_cold_entries(vec![mk_entry("toctou-failed-idx", None, None)]);

        // Simulate: another thread already failed and called mark_failed,
        // but the entry is still in `failed_entries`. We do this before calling
        // get_or_load_index to model the state the gate re-check would observe.
        cold.mark_failed(&id);

        // At this point `entries` is empty (mark_failed removed it) and
        // `failed_entries` contains the id. `get_or_load_index` will see
        // `entries.get(id) == None` at step 2 and return NotFound immediately
        // (before even reaching the gate). But we also verify restore_fn is
        // never invoked.
        let mut restore_called = 0u32;
        let result = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| {
            restore_called += 1;
            async { false }
        })
        .await;

        // Step 2 short-circuits: entries is empty, so NotFound is returned.
        // restore_fn must NOT have been called.
        assert!(
            matches!(
                result,
                Err(LazyLoadError::NotFound) | Err(LazyLoadError::RestoreFailed)
            ),
            "must short-circuit without calling restore_fn when already failed"
        );
        assert_eq!(
            restore_called, 0,
            "restore_fn must not be called when entry is already in failed_entries"
        );
        assert!(cold.is_failed(&id), "id must still be in failed_entries");
    }

    /// Why: after a restore failure, subsequent calls must not re-attempt the
    /// expensive restore path. `cold_store.contains()` returns `false` so
    /// `get_or_load_index` returns `NotFound` immediately (the caller's
    /// `is_failed` check in the search handler catches this before calling
    /// `get_or_load_index`). This test verifies the loader's own behavior: the
    /// second call sees neither `entries` nor a gate, so it returns `NotFound`
    /// rather than calling restore_fn again.
    /// Test: this test.
    #[tokio::test]
    async fn get_or_load_index_second_call_after_failure_does_not_retry() {
        let registry = IndexRegistry::default();
        let cold = ColdIndexStore::new();
        let id = IndexId::new("fail-idx2".to_string());
        cold.register_cold_entries(vec![mk_entry("fail-idx2", None, None)]);

        let mut restore_call_count = 0u32;

        // First call — restore fails.
        let _ = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| {
            restore_call_count += 1;
            async { false }
        })
        .await;

        // Second call — entry is no longer in cold store; restore_fn must NOT
        // be called again. The result is NotFound because `is_failed` is not
        // checked inside `get_or_load_index` (that's the search handler's job).
        let result2 = get_or_load_index(&id, &registry, &cold, Duration::from_secs(5), |_e| {
            restore_call_count += 1;
            async { false }
        })
        .await;

        assert!(
            matches!(result2, Err(LazyLoadError::NotFound)),
            "second call after failure must return NotFound (not re-attempt restore)"
        );
        assert_eq!(
            restore_call_count, 1,
            "restore_fn must be called exactly once (not re-attempted after failure)"
        );
    }
}