trusty-search 0.26.0

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
//! Tests for issue #1073: content-hash-incremental reindex + in-place relocation.
//!
//! Why: three independent bugs were fixed together: (1) root-move on colocated
//! indexes cleared the hash cache unnecessarily; (2) warm-restart hash-skip
//! missed relative keys (absolute vs. relative mismatch in the DashMap);
//! (3) no in-place relocation primitive existed (`PATCH /indexes/:id`).
//! What: this module verifies each fix with a focused unit test that does not
//! require a running daemon or a real embedder.
//! Test: run with `cargo test -p trusty-search tests_1073`.

use super::*;
use crate::core::embed::Embedder;
use crate::core::registry::IndexRegistry;
use axum::extract::State;
use axum::http::StatusCode;
use axum::Json;
use std::sync::Arc;

// ── Test 1: PATCH /indexes/:id returns 404 for an unknown index id ───────────

/// `PATCH /indexes/:id` with an unregistered id must return `404 Not Found`.
///
/// Why: ensures the handler's "index not found" guard works and doesn't panic.
/// What: calls `relocate_index_handler` with a state that has no registered
/// indexes; asserts the response status is `404`.
/// Test: this test (pure in-memory, no network or embedder required).
#[tokio::test]
async fn relocate_index_returns_404_for_unknown_id() {
    use super::indexes_relocate::{relocate_index_handler, RelocateIndexRequest};
    use axum::body::to_bytes;
    use axum::extract::Path;

    let state = SearchAppState::new(IndexRegistry::new());
    let embedder: Arc<dyn Embedder> = Arc::new(crate::core::embed::MockEmbedder::new(8));
    state.install_embedder(embedder).await;
    let state_arc = Arc::new(state);

    let resp = relocate_index_handler(
        State(Arc::clone(&state_arc)),
        Path("no-such-index-xyz".to_string()),
        Json(RelocateIndexRequest {
            root_path: std::path::PathBuf::from("/tmp"),
        }),
    )
    .await;
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    let body = to_bytes(resp.into_body(), 4096).await.expect("body");
    let v: serde_json::Value = serde_json::from_slice(&body).expect("json");
    let err = v.get("error").and_then(|x| x.as_str()).unwrap_or("");
    assert!(
        err.contains("no-such-index-xyz"),
        "error should name the id: {err}"
    );
}

// ── Test 2: PATCH /indexes/:id updates root_path in the registry ─────────────

/// A registered index can be relocated to a new directory without re-embedding.
///
/// Why: core correctness test for issue #1073 Change 3.
/// What: (1) creates a real tempdir as the initial root; (2) registers an index
/// at that path; (3) creates a second tempdir as the new root; (4) calls
/// `PATCH /indexes/:id`; (5) asserts the handle's `root_path` in the registry
/// reflects the new path and the response carries `"relocated": true`.
/// Test: this test.
#[tokio::test]
async fn relocate_index_updates_root_path() {
    use super::indexes_relocate::{relocate_index_handler, RelocateIndexRequest};
    use super::router::CreateIndexRequest;
    use axum::body::to_bytes;
    use axum::extract::Path;

    let state = SearchAppState::new(IndexRegistry::new());
    let embedder: Arc<dyn Embedder> = Arc::new(crate::core::embed::MockEmbedder::new(8));
    state.install_embedder(embedder).await;
    let state_arc = Arc::new(state);

    // Build the initial and target directories under target/ (never in the
    // denylist), using RAII TempDir for cleanup.
    let cwd = std::env::current_dir().expect("cwd");
    let base = cwd.join("target");
    std::fs::create_dir_all(&base).expect("create target/");
    let old_dir = tempfile::Builder::new()
        .prefix("ts-relocate-old-")
        .tempdir_in(&base)
        .expect("create old_dir");
    let new_dir = tempfile::Builder::new()
        .prefix("ts-relocate-new-")
        .tempdir_in(&base)
        .expect("create new_dir");

    let old_root = old_dir.path().canonicalize().expect("canonicalize old_dir");
    let new_root = new_dir.path().canonicalize().expect("canonicalize new_dir");

    // Step 1: register the index at old_root.
    let create_resp = super::indexes::create_index_handler(
        State(Arc::clone(&state_arc)),
        Json(CreateIndexRequest {
            id: "relocate-test".into(),
            root_path: old_root.clone(),
            include_paths: None,
            exclude_globs: None,
            extensions: None,
            domain_terms: None,
            path_filter: None,
            include_docs: None,
            respect_gitignore: None,
            lexical_only: None,
            skip_kg: None,
            defer_embed: None,
            extra_skip_dirs: None,
            data_file_max_bytes: None,
        }),
    )
    .await;
    assert_eq!(
        create_resp.status(),
        StatusCode::OK,
        "initial create must succeed"
    );

    // Step 2: relocate to new_root.
    let patch_resp = relocate_index_handler(
        State(Arc::clone(&state_arc)),
        Path("relocate-test".to_string()),
        Json(RelocateIndexRequest {
            root_path: new_root.clone(),
        }),
    )
    .await;
    assert_eq!(patch_resp.status(), StatusCode::OK, "relocate must succeed");

    let body = to_bytes(patch_resp.into_body(), 4096).await.expect("body");
    let v: serde_json::Value = serde_json::from_slice(&body).expect("json");
    assert_eq!(
        v.get("relocated").and_then(|x| x.as_bool()),
        Some(true),
        "response must carry relocated:true"
    );
    assert_eq!(
        v.get("id").and_then(|x| x.as_str()),
        Some("relocate-test"),
        "response must echo the index id"
    );

    // Step 3: assert the in-memory registry reflects the new root.
    let handle = state_arc
        .registry
        .get(&crate::core::registry::IndexId::new("relocate-test"))
        .expect("handle must still be in registry after relocate");
    assert_eq!(
        handle.root_path, new_root,
        "handle.root_path must point at the new directory after relocate"
    );
    assert_ne!(
        handle.root_path, old_root,
        "handle.root_path must not retain the old directory"
    );
}

// ── Test 3: warm-restart hash-skip — relative keys match after load ───────────

/// After a daemon restart the hash cache loaded from redb must be queryable
/// using relative `PathBuf` keys (the same representation produced during
/// reindex), not absolute keys.
///
/// Why: this is the latent bug fixed by issue #1073 Change 2. The in-process
/// DashMap used to be populated with ABSOLUTE keys by `prepare_batch_payload`,
/// while `hash_cache::load_into_cache` inserts RELATIVE keys from redb. After
/// a restart every hash lookup missed, causing a full re-embed.
/// What: inserts a relative-key entry into the DashMap (simulating what
/// `load_into_cache` does after a restart), then looks it up via a relative
/// key (simulating what the fixed `prepare_batch_payload` now does). Asserts
/// the lookup hits.
/// Test: this test.
#[test]
fn hash_cache_relative_key_matches_after_load() {
    let map: dashmap::DashMap<std::path::PathBuf, String> = dashmap::DashMap::new();

    // Simulate what `hash_cache::load_into_cache` inserts: a RELATIVE key.
    let rel_path = std::path::PathBuf::from("src/main.rs");
    let hash_value = "abc123def456".to_string(); // pragma: allowlist secret
    map.insert(rel_path.clone(), hash_value.clone());

    // Simulate what the FIXED `prepare_batch_payload` looks up: ALSO a relative key.
    let lookup_key = std::path::PathBuf::from("src/main.rs");
    let got = map.get(&lookup_key).map(|v| v.clone());

    assert_eq!(
        got.as_deref(),
        Some(hash_value.as_str()),
        "relative-key lookup must hit the relative-key entry in the DashMap"
    );

    // Confirm that an absolute key would NOT have matched (to demonstrate the
    // original bug: absolute keys silently missed all redb-loaded entries).
    let abs_key = std::path::PathBuf::from("/some/project/root/src/main.rs");
    let miss = map.get(&abs_key).map(|v| v.clone());
    assert!(
        miss.is_none(),
        "absolute-key lookup must NOT match a relative-key entry (old bug)"
    );
}

// ── Test 4: colocated fallback is false on missing/unreadable disk entry ─────

/// When the on-disk `indexes.toml` entry is absent or unreadable, the
/// fallback for `colocated` must be `false` (central-store / non-colocated),
/// NOT `true`.
///
/// Why (issue #1097): the old `unwrap_or(true)` would assume colocated on any
/// IO error, re-introducing the #1088 data-wipe for central-store indexes. The
/// safe default is `false` — it routes to the global data directory and cannot
/// destroy colocated project data. This test pins the fallback by verifying
/// that `load_index_registry_at` on a non-existent path gives `Err`, and that
/// the `ok().and_then(...).map(...).unwrap_or(false)` chain resolves to `false`.
///
/// What: simulates the registry-load-failure path without touching the real
/// production `indexes.toml` — calls `load_index_registry_at` on an
/// impossible path and asserts the fallback chain would yield `false`.
///
/// Test: this test (issue #1097 / #1088 guard).
#[test]
fn colocated_fallback_is_false_when_disk_entry_absent() {
    use crate::service::persistence::load_index_registry_at;
    use std::path::PathBuf;

    // Simulate an unreadable / absent indexes.toml.
    let missing = PathBuf::from("/tmp/nonexistent-trusty-search-test-xyz/indexes.toml");
    let on_disk_colocated = load_index_registry_at(&missing)
        .ok()
        .and_then(|entries| entries.into_iter().find(|e| e.id == "any-index"))
        .map(|e| e.colocated)
        // This is the exact fallback expression from indexes_relocate.rs.
        .unwrap_or(false);
    assert!(
        !on_disk_colocated,
        "colocated fallback must be false when disk entry is absent/unreadable (issue #1097)"
    );

    // Also verify: if an entry IS found with colocated=true, it IS returned.
    let tmp = tempfile::tempdir().expect("tempdir");
    let toml_path = tmp.path().join("indexes.toml");
    crate::service::persistence::upsert_index_registry_entry_at(
        &toml_path,
        crate::service::persistence::PersistedIndex {
            id: "existing-colocated".to_string(),
            root_path: PathBuf::from("/some/root"),
            colocated: true,
            ..crate::service::persistence::PersistedIndex::default()
        },
    )
    .expect("write entry");
    let found = load_index_registry_at(&toml_path)
        .ok()
        .and_then(|entries| entries.into_iter().find(|e| e.id == "existing-colocated"))
        .map(|e| e.colocated)
        .unwrap_or(false);
    assert!(
        found,
        "colocated must be true when the disk entry explicitly says so"
    );
}

// ── Test 5: PATCH preserves LRU timestamps (PR #1103 fix) ────────────────────

/// `PATCH /indexes/:id` must preserve `last_queried_unix` and `last_indexed_unix`
/// from the on-disk entry so relocation does not silently demote a
/// heavily-used index to the cold-store tail on the next selective warm-boot.
///
/// Why (PR #1103 review finding): the handler set both fields to `None` even
/// though the comment said "preserve them". Zero-ing the LRU sort key for an
/// actively-queried index would cause `TRUSTY_WARMBOOT_MAX_INDEXES` to deprioritize
/// it on the next restart.
/// What: writes an entry with non-None timestamps to indexes.toml, then verifies
/// that the `on_disk_last_queried` / `on_disk_last_indexed` reading logic
/// recovers the correct values.
/// Test: this test.
#[test]
fn relocate_preserves_lru_timestamps() {
    use crate::service::persistence::{
        load_index_registry_at, upsert_index_registry_entry_at, PersistedIndex,
    };
    use std::path::PathBuf;

    let tmp = tempfile::tempdir().expect("tempdir");
    let toml_path = tmp.path().join("indexes.toml");

    // Write an entry with known timestamps.
    let entry = PersistedIndex {
        id: "lru-relocate-test".to_string(),
        root_path: PathBuf::from("/projects/lru-relocate-test"),
        last_queried_unix: Some(1_700_000_000),
        last_indexed_unix: Some(1_699_000_000),
        ..PersistedIndex::default()
    };
    upsert_index_registry_entry_at(&toml_path, entry).expect("write entry");

    // Simulate the handler's "load once, extract fields" pattern.
    let on_disk = load_index_registry_at(&toml_path)
        .ok()
        .and_then(|entries| entries.into_iter().find(|e| e.id == "lru-relocate-test"));

    let on_disk_last_queried = on_disk.as_ref().and_then(|e| e.last_queried_unix);
    let on_disk_last_indexed = on_disk.as_ref().and_then(|e| e.last_indexed_unix);

    assert_eq!(
        on_disk_last_queried,
        Some(1_700_000_000),
        "last_queried_unix must be preserved after a PATCH (PR #1103)"
    );
    assert_eq!(
        on_disk_last_indexed,
        Some(1_699_000_000),
        "last_indexed_unix must be preserved after a PATCH (PR #1103)"
    );

    // Also verify None timestamps are handled gracefully.
    let entry_no_ts = PersistedIndex {
        id: "lru-no-ts-test".to_string(),
        root_path: PathBuf::from("/projects/lru-no-ts"),
        ..PersistedIndex::default()
    };
    upsert_index_registry_entry_at(&toml_path, entry_no_ts).expect("write entry-no-ts");
    let on_disk2 = load_index_registry_at(&toml_path)
        .ok()
        .and_then(|entries| entries.into_iter().find(|e| e.id == "lru-no-ts-test"));
    assert!(
        on_disk2
            .as_ref()
            .and_then(|e| e.last_queried_unix)
            .is_none(),
        "None timestamps must remain None after round-trip"
    );
}

// ── Test 6: cross-index PATCH does not strip manually-added fields ────────────

/// A PATCH to index A must NOT strip manually-edited fields (e.g.
/// `exclude_globs`) from index B's on-disk entry.
///
/// Why: this is the #1089 completeness regression test. `upsert_index_registry_entry`
/// loads ALL entries from `indexes.toml`, overwrites only the entry matching the
/// supplied id, then saves all entries. If it accidentally serialised from
/// in-memory state (ignoring the other entries on disk), manually-added fields
/// like `exclude_globs` on index B would be silently stripped when A is PATCHed.
///
/// What: (1) writes two entries to a temp `indexes.toml` — index-a (plain) and
/// index-b (with `exclude_globs = ["**/vendor/**"]`); (2) calls
/// `upsert_index_registry_entry_at` for index-a with a changed `root_path`;
/// (3) reloads the file and asserts index-b's `exclude_globs` is still
/// `["**/vendor/**"]`.
///
/// Test: this test (issue #1089 completeness, issue #1097).
#[test]
fn patch_index_a_does_not_strip_exclude_globs_of_index_b() {
    use crate::service::persistence::load_index_registry_at;
    use crate::service::persistence::{upsert_index_registry_entry_at, PersistedIndex};
    use std::path::PathBuf;

    let tmp = tempfile::tempdir().expect("tempdir");
    let toml_path = tmp.path().join("indexes.toml");

    // Write initial state: index-a (no extra fields) and index-b with exclude_globs.
    let entry_a = PersistedIndex {
        id: "index-a".to_string(),
        root_path: PathBuf::from("/projects/index-a"),
        ..PersistedIndex::default()
    };
    let entry_b = PersistedIndex {
        id: "index-b".to_string(),
        root_path: PathBuf::from("/projects/index-b"),
        exclude_globs: vec!["**/vendor/**".to_string(), "*.generated.ts".to_string()],
        ..PersistedIndex::default()
    };
    upsert_index_registry_entry_at(&toml_path, entry_a).expect("write entry-a");
    upsert_index_registry_entry_at(&toml_path, entry_b).expect("write entry-b");

    // PATCH index-a: change its root_path (simulate PATCH /indexes/index-a).
    let patched_a = PersistedIndex {
        id: "index-a".to_string(),
        root_path: PathBuf::from("/projects/index-a-new"),
        ..PersistedIndex::default()
    };
    upsert_index_registry_entry_at(&toml_path, patched_a).expect("patch entry-a");

    // Reload and assert index-b's exclude_globs survived the patch of index-a.
    let entries = load_index_registry_at(&toml_path).expect("reload");
    let b = entries
        .iter()
        .find(|e| e.id == "index-b")
        .expect("index-b must still be present after patching index-a");
    assert_eq!(
        b.exclude_globs,
        vec!["**/vendor/**".to_string(), "*.generated.ts".to_string()],
        "index-b's exclude_globs must survive a PATCH to index-a (issue #1089)"
    );

    // Also verify index-a's root_path was updated correctly.
    let a = entries
        .iter()
        .find(|e| e.id == "index-a")
        .expect("index-a must still be present");
    assert_eq!(
        a.root_path,
        PathBuf::from("/projects/index-a-new"),
        "index-a's root_path must reflect the PATCH"
    );
}