trusty-search 0.20.3

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
551
552
553
554
555
556
557
558
559
//! Auto-discovery of Claude Code, git, and trusty-tools projects at daemon startup.
//!
//! Why: agents and users shouldn't need to manually index every project — the
//!      daemon should find and index Claude Code projects automatically on
//!      startup. Users with hundreds of repos under `~/Projects` get a working
//!      index registry without typing a single command. Projects that follow the
//!      trusty-tools convention (`.trusty-tools/` directory at the root) are
//!      now also discoverable (#470), making the broader trusty-tools ecosystem
//!      visible to trusty-search without any extra configuration.
//! What: scans configured `scan_paths` (from `GlobalConfig`) plus sensible
//!       defaults for projects with `.claude/`, `CLAUDE.md`, `.git/`, or
//!       `.trusty-tools/` markers, then registers + reindexes any not already
//!       known to the daemon. The function is intentionally best-effort: errors
//!       talking to the daemon or filesystem are logged at warn and never
//!       propagated, because auto-discovery must never crash the daemon startup
//!       path.
//! Test: unit tests cover project detection signals
//!       (`detects_claude_dir`, `detects_claude_md`, `detects_git`,
//!       `detects_trusty_tools_dir`, `skips_when_no_markers`); the end-to-end
//!       pipeline is exercised by the existing daemon integration tests once
//!       auto-discovery is wired in.

use super::daemon_utils::daemon_base_url;
use super::reindex_engine::register_index_with_daemon;
use crate::config::GlobalConfig;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// The `.trusty-tools/` directory name used as a project-discovery signal.
///
/// Why: defines the constant in one place so the detection logic and tests
///      agree on the exact spelling. Matches the convention established by
///      `trusty-memory`'s `project_root::TRUSTY_TOOLS_DIR`.
/// What: `".trusty-tools"` — a directory at a project root indicates the
///       project participates in the trusty-tools ecosystem.
/// Test: `detect_project_marker_trusty_tools_dir` in the tests module below.
const TRUSTY_TOOLS_DIR: &str = ".trusty-tools";

/// Signal that identifies a directory as worth indexing.
///
/// Why: priority matters — a `.claude/` directory is the strongest signal that
///      this project is being worked on with Claude Code and should be indexed
///      first. `.git/` and `.trusty-tools/` are weaker but still useful hints.
///      `.trusty-tools/` is added here (#470) so projects following the
///      trusty-tools convention are discovered at startup alongside Claude Code
///      and git projects, with a single marginal `path.exists()` stat per
///      iterated subdirectory — no additional tree walk.
/// What: ordered by strength; `Claude` > `ClaudeMd` > `Git` > `TrustyTools`.
///       `None` means skip.
/// Test: `detect_project_marker_*` unit tests below.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProjectMarker {
    Claude,
    ClaudeMd,
    Git,
    /// A `.trusty-tools/` directory is present — the project follows the
    /// trusty-tools ecosystem convention (#470).
    TrustyTools,
    None,
}

/// Inspect a directory for the strongest project marker present.
///
/// Why: keeps the detection rules in one place so the priority order stays
///      consistent between the scanner and any future caller (doctor, MCP,
///      tests). Each probe is a single `is_dir`/`is_file`/`exists` call —
///      O(1) stat, no recursion — so the marginal cost per subdirectory in
///      `auto_discover_and_index` is negligible.
/// What: probes `.claude/` first, then `CLAUDE.md`, then `.git/`, then
///       `.trusty-tools/`, returning the first match.
/// Test: see `detect_project_marker_*` below.
fn detect_project_marker(dir: &Path) -> ProjectMarker {
    if dir.join(".claude").is_dir() {
        return ProjectMarker::Claude;
    }
    if dir.join("CLAUDE.md").is_file() {
        return ProjectMarker::ClaudeMd;
    }
    if dir.join(".git").exists() {
        return ProjectMarker::Git;
    }
    if dir.join(TRUSTY_TOOLS_DIR).is_dir() {
        return ProjectMarker::TrustyTools;
    }
    ProjectMarker::None
}

/// Default scan paths when the user has not set `scan_paths` in
/// `~/.config/trusty-search/config.yaml`.
///
/// Why: a fresh install needs to do something useful. Picking the three most
///      common project-root conventions covers nearly every developer setup
///      without over-eager filesystem walks.
/// What: returns `~/Projects`, `~/code`, `~/src`, filtered to those that
///       actually exist on the current machine. Returns empty when `$HOME` is
///       not set (unusual; only happens in restricted CI sandboxes).
/// Test: covered indirectly by `auto_discover_and_index_smoke` (when wired).
fn default_scan_paths() -> Vec<PathBuf> {
    let Some(home) = dirs::home_dir() else {
        return Vec::new();
    };
    ["Projects", "code", "src"]
        .iter()
        .map(|p| home.join(p))
        .filter(|p| p.is_dir())
        .collect()
}

/// Discover and index Claude Code, git, and trusty-tools projects on daemon startup.
///
/// Why: closes the "manual `trusty-search index` per repo" loop so the daemon
///      hydrates itself from the user's actual workspace. Runs once at
///      startup, after `restore_indexes()` has rehydrated the registry from
///      `indexes.toml`. Projects following the trusty-tools convention
///      (`.trusty-tools/` directory at the root — #470) are now included
///      alongside Claude Code and git projects; discovery cost is marginal
///      (one extra `is_dir` stat per subdirectory already being iterated).
/// What: loads `GlobalConfig`, resolves the scan list (config-supplied or
///       default), walks one level deep under each entry, and for every
///       directory with a project marker (`.claude/`, `CLAUDE.md`, `.git/`, or
///       `.trusty-tools/`) that is NOT already registered with the daemon,
///       calls `POST /indexes` followed by `POST /indexes/:id/reindex` via
///       the local HTTP API. The reindex POST includes `"background": true`
///       (issue #458) so these bulk startup tasks are routed through the
///       low-priority semaphore and cannot starve user-initiated indexing.
///       Directories whose `root_path` does not exist on disk are skipped
///       (issue #458 part 2) — dead/moved projects must not flood the queue.
///       All failures are logged at warn level and never propagated — auto-discovery
///       is best-effort.
/// Test: side-effect-only at this level. Unit tests cover the pure detection
///       logic (`detect_project_marker_*`, `default_scan_paths`,
///       `root_path_exists_before_reindex`); the integration smoke test lives
///       under `tests/integration_tests.rs`.
pub async fn auto_discover_and_index() {
    let cfg = match GlobalConfig::load() {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!("auto-discover: could not load global config: {e:#} — skipping");
            return;
        }
    };

    let scan_paths = if cfg.scan_paths.is_empty() {
        default_scan_paths()
    } else {
        cfg.scan_paths.clone()
    };

    if scan_paths.is_empty() {
        tracing::debug!("auto-discover: no scan paths configured and no defaults found — skipping");
        return;
    }

    let base = daemon_base_url();
    let client = match trusty_common::server::daemon_http_client() {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!("auto-discover: could not build HTTP client: {e:#} — skipping");
            return;
        }
    };

    // Wait briefly for the daemon's HTTP listener to come online. The discover
    // task is spawned in parallel with `run_daemon`, so the listener may not
    // be bound yet on the first iteration. Cap the wait so a daemon that
    // failed to bind doesn't leave the auto-discoverer spinning forever.
    if !wait_for_daemon_ready(&client, &base, Duration::from_secs(15)).await {
        tracing::warn!(
            "auto-discover: daemon at {base} did not become ready within 15s — skipping"
        );
        return;
    }

    let known: std::collections::HashSet<String> = match fetch_known_index_ids(&client, &base).await
    {
        Ok(s) => s,
        Err(e) => {
            tracing::warn!("auto-discover: could not list indexes: {e:#} — skipping");
            return;
        }
    };

    let mut discovered = 0usize;
    let mut indexed = 0usize;

    for root in &scan_paths {
        if !root.is_dir() {
            tracing::debug!(
                "auto-discover: skipping non-directory scan path {}",
                root.display()
            );
            continue;
        }
        let entries = match std::fs::read_dir(root) {
            Ok(it) => it,
            Err(e) => {
                tracing::warn!("auto-discover: could not read {}: {e}", root.display());
                continue;
            }
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if !path.is_dir() {
                continue;
            }
            let marker = detect_project_marker(&path);
            if marker == ProjectMarker::None {
                continue;
            }
            discovered += 1;

            let name = match path.file_name().and_then(|n| n.to_str()) {
                Some(n) if !n.is_empty() => n.to_string(),
                _ => {
                    tracing::debug!(
                        "auto-discover: skipping {} (no usable name)",
                        path.display()
                    );
                    continue;
                }
            };
            if known.contains(&name) {
                tracing::debug!(
                    "auto-discover: skipping {} (index '{}' already registered)",
                    path.display(),
                    name
                );
                continue;
            }

            tracing::info!(
                "auto-discover: indexing {} as '{}' (marker={:?})",
                path.display(),
                name,
                marker
            );

            match register_index_with_daemon(&name, &path).await {
                Ok((_created, true)) => {
                    // Issue #458 (part 2): skip reindex if the root path does
                    // not exist on disk. Dead/moved/unmounted project directories
                    // (e.g. an external volume that is not currently mounted)
                    // must not flood the background reindex queue. The index
                    // registration is intentionally kept so operators can still
                    // see it; only the auto-discover reindex trigger is skipped.
                    if !path.exists() {
                        tracing::info!(
                            "auto-discover: skipping reindex of '{}' — root path '{}' \
                             does not exist on disk (dead/moved project)",
                            name,
                            path.display()
                        );
                        continue;
                    }
                    let reindex_url = format!("{base}/indexes/{name}/reindex");
                    // Issue #458 (part 1): set `background: true` so the reindex
                    // request is routed through the low-priority semaphore. This
                    // prevents a large startup discovery (e.g. 44 projects) from
                    // starving a concurrent user-initiated `trusty-search index`.
                    match client
                        .post(&reindex_url)
                        .json(&serde_json::json!({ "background": true }))
                        .send()
                        .await
                    {
                        Ok(resp) if resp.status().is_success() => {
                            indexed += 1;
                        }
                        Ok(resp) => {
                            tracing::warn!(
                                "auto-discover: reindex of '{name}' returned HTTP {}",
                                resp.status()
                            );
                        }
                        Err(e) => {
                            tracing::warn!(
                                "auto-discover: could not POST reindex for '{name}': {e}"
                            );
                        }
                    }
                }
                Ok((_, false)) => {
                    tracing::warn!(
                        "auto-discover: daemon unreachable while registering '{name}' — aborting"
                    );
                    return;
                }
                Err(e) => {
                    tracing::warn!("auto-discover: could not register '{name}': {e:#}");
                }
            }
        }
    }

    if discovered > 0 {
        tracing::info!(
            "auto-discover: scanned {} root(s); discovered {} project(s); queued {} for indexing",
            scan_paths.len(),
            discovered,
            indexed
        );
    }
}

/// Poll the daemon's `/health` endpoint until it returns 200 or the deadline
/// fires.
///
/// Why: auto-discovery is spawned in parallel with `run_daemon`, so the HTTP
///      listener may not be ready when discovery starts probing. Without this,
///      the first `register_index_with_daemon` call would race and fail.
/// What: polls every 200 ms up to `timeout`. Returns true on first success,
///       false if the deadline elapses with no successful response.
/// Test: side-effect-only; covered indirectly by the daemon startup integration
///       tests.
async fn wait_for_daemon_ready(client: &reqwest::Client, base: &str, timeout: Duration) -> bool {
    let url = format!("{base}/health");
    let deadline = std::time::Instant::now() + timeout;
    loop {
        if client
            .get(&url)
            .send()
            .await
            .ok()
            .map(|r| r.status().is_success())
            .unwrap_or(false)
        {
            return true;
        }
        if std::time::Instant::now() >= deadline {
            return false;
        }
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
}

/// Fetch the set of currently-registered index ids from the daemon.
///
/// Why: we must not re-register a project that is already known — the daemon's
///      `POST /indexes` is idempotent but the follow-up reindex would still
///      run, wasting CPU and contending with whatever else the daemon is
///      doing.
/// What: GET `/indexes` and parse the `indexes` array of strings.
/// Test: covered indirectly by `auto_discover_and_index` integration runs.
async fn fetch_known_index_ids(
    client: &reqwest::Client,
    base: &str,
) -> anyhow::Result<std::collections::HashSet<String>> {
    let url = format!("{base}/indexes");
    let resp = client.get(&url).send().await?;
    if !resp.status().is_success() {
        anyhow::bail!("daemon returned {} for {url}", resp.status());
    }
    let body: serde_json::Value = resp.json().await?;
    let empty: Vec<serde_json::Value> = Vec::new();
    let set = body
        .get("indexes")
        .and_then(|v| v.as_array())
        .unwrap_or(&empty)
        .iter()
        .filter_map(|v| v.as_str().map(|s| s.to_string()))
        .collect();
    Ok(set)
}

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

    fn tempdir_unique(label: &str) -> PathBuf {
        let pid = std::process::id();
        let nanos = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos();
        let p = std::env::temp_dir().join(format!("trusty-discover-{label}-{pid}-{nanos}"));
        let _ = fs::remove_dir_all(&p);
        fs::create_dir_all(&p).unwrap();
        p
    }

    #[test]
    fn detect_project_marker_claude_dir_wins() {
        let dir = tempdir_unique("claude");
        fs::create_dir_all(dir.join(".claude")).unwrap();
        fs::write(dir.join("CLAUDE.md"), "x").unwrap();
        fs::create_dir_all(dir.join(".git")).unwrap();
        assert_eq!(detect_project_marker(&dir), ProjectMarker::Claude);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn detect_project_marker_claude_md_beats_git() {
        let dir = tempdir_unique("claudemd");
        fs::write(dir.join("CLAUDE.md"), "x").unwrap();
        fs::create_dir_all(dir.join(".git")).unwrap();
        assert_eq!(detect_project_marker(&dir), ProjectMarker::ClaudeMd);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn detect_project_marker_git_when_only_git() {
        let dir = tempdir_unique("git");
        fs::create_dir_all(dir.join(".git")).unwrap();
        assert_eq!(detect_project_marker(&dir), ProjectMarker::Git);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn detect_project_marker_none_when_empty() {
        let dir = tempdir_unique("empty");
        assert_eq!(detect_project_marker(&dir), ProjectMarker::None);
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn detect_project_marker_ignores_claude_md_as_dir() {
        // CLAUDE.md must be a file, not a directory.
        let dir = tempdir_unique("claudedir");
        fs::create_dir_all(dir.join("CLAUDE.md")).unwrap();
        assert_eq!(detect_project_marker(&dir), ProjectMarker::None);
        let _ = fs::remove_dir_all(&dir);
    }

    // ── Issue #470: .trusty-tools/ marker detection ──────────────────────────

    /// Why: a directory containing only `.trusty-tools/` (and none of the
    /// pre-existing markers) must be recognised as a project by
    /// `detect_project_marker` so `auto_discover_and_index` queues it for
    /// indexing. This is the primary acceptance criterion for #470.
    ///
    /// What: creates a temp dir with only a `.trusty-tools/` subdirectory,
    /// then asserts the marker variant is `TrustyTools`. Confirms the signal
    /// fires from a single `is_dir` stat — the function is pure/bounded and
    /// contains no recursion.
    ///
    /// Test: this test itself; bounded by structural inspection (the function
    /// has exactly four probe calls — see `detect_project_marker` source).
    #[test]
    fn detect_project_marker_trusty_tools_dir() {
        let dir = tempdir_unique("trustytools");
        fs::create_dir_all(dir.join(TRUSTY_TOOLS_DIR)).unwrap();
        assert_eq!(
            detect_project_marker(&dir),
            ProjectMarker::TrustyTools,
            ".trusty-tools/ dir must yield TrustyTools marker"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// Why: priority ordering must be preserved — `.claude/` must still win
    /// over `.trusty-tools/` when both are present (a Claude Code workspace
    /// may also follow the trusty-tools convention).
    ///
    /// What: creates a temp dir with both `.claude/` and `.trusty-tools/`,
    /// asserts `Claude` is returned.
    ///
    /// Test: this test itself.
    #[test]
    fn detect_project_marker_claude_wins_over_trusty_tools() {
        let dir = tempdir_unique("claude-plus-trusty");
        fs::create_dir_all(dir.join(".claude")).unwrap();
        fs::create_dir_all(dir.join(TRUSTY_TOOLS_DIR)).unwrap();
        assert_eq!(
            detect_project_marker(&dir),
            ProjectMarker::Claude,
            ".claude/ must take priority over .trusty-tools/"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// Why: `.trusty-tools` as a file (not a directory) must not trigger the
    /// marker — the convention requires a directory, matching the `is_dir`
    /// probe in `detect_project_marker`.
    ///
    /// What: writes a regular file named `.trusty-tools` and asserts `None`.
    ///
    /// Test: this test itself.
    #[test]
    fn detect_project_marker_trusty_tools_file_not_dir_is_none() {
        let dir = tempdir_unique("trustytools-file");
        fs::write(dir.join(TRUSTY_TOOLS_DIR), "not a dir").unwrap();
        assert_eq!(
            detect_project_marker(&dir),
            ProjectMarker::None,
            ".trusty-tools as a file must not trigger TrustyTools marker"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    /// Why: a directory with none of the four markers must still return `None`
    /// after the `.trusty-tools/` probe was added — regression guard ensuring
    /// we didn't accidentally widen detection.
    ///
    /// What: creates an empty temp dir and asserts `None`.
    ///
    /// Test: this test itself.
    #[test]
    fn detect_project_marker_none_when_no_markers_after_trusty_tools_added() {
        let dir = tempdir_unique("no-markers");
        // No .claude/, no CLAUDE.md, no .git, no .trusty-tools/
        assert_eq!(
            detect_project_marker(&dir),
            ProjectMarker::None,
            "directory with no markers must still return None"
        );
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn default_scan_paths_does_not_panic() {
        // We can't assert exact contents (depends on the user's $HOME) but the
        // call must always return cleanly.
        let _ = default_scan_paths();
    }

    // ── Issue #458: dead-root skip predicate ──────────────────────────────────

    /// Why: the auto-discover reindex skip decision is `path.exists()`. A
    /// non-existent path (unmounted volume, moved project) must produce `false`
    /// so the auto-discover loop skips the reindex trigger. This test verifies
    /// the predicate works correctly for both live and dead paths — the pure
    /// filesystem call is the factored-out decision point that `auto_discover_
    /// and_index` relies on.
    ///
    /// What: creates a real temporary directory (exists → true), then removes it
    /// and checks again (gone → false). Mirrors the exact check used in the
    /// auto-discover loop.
    ///
    /// Test: self-contained — no daemon needed; runs in normal `cargo test`.
    #[test]
    fn root_path_exists_true_for_real_dir() {
        let dir = tempdir_unique("exists");
        assert!(dir.exists(), "freshly-created dir must exist");
        let _ = fs::remove_dir_all(&dir);
    }

    #[test]
    fn root_path_exists_false_after_removal() {
        let dir = tempdir_unique("gone");
        fs::remove_dir_all(&dir).ok();
        assert!(
            !dir.exists(),
            "deleted dir must not exist — the dead-root skip predicate would fire"
        );
    }

    #[test]
    fn root_path_exists_false_for_never_created() {
        // A path that was never on disk: simulate a dead/moved project volume.
        let phantom =
            std::env::temp_dir().join("trusty-discover-phantom-path-that-will-never-exist-12345");
        let _ = fs::remove_dir_all(&phantom);
        assert!(
            !phantom.exists(),
            "phantom path must not exist — dead-root skip would fire for this index"
        );
    }
}