trusty-code 0.2.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
//! `search_code` tool: trusty-search-backed code discovery (PR A).
//!
//! Why: local `glob`/`grep`/`list_dir` (#2685) find code by literal text only;
//! the engineer routinely needs CONCEPTUAL discovery — "where is X", "how does
//! Y work", "what calls Z" — which literal search cannot answer. trusty-search
//! already indexes every registered project for semantic (embedding) and
//! call-graph (KG) retrieval and ships as an MCP binary. This tool makes that
//! the PRIMARY discovery path for the engineer while the local literal tools
//! stay as the graceful-degrade FALLBACK: when trusty-search is not installed,
//! not running, or its index is not ready, the tool returns a SUCCESSFUL
//! "unavailable — use grep/glob" result rather than an error that would derail
//! the agent loop (mirrors `recall_session`'s fail-open contract).
//! What: [`TrustySearchTool`] implements `ToolExecutor`. It lazily spawns
//! `trusty-search serve` via the SHARED `trusty_common::stdio_mcp_client`
//! (never a bespoke MCP/HTTP client — common-entry-point principle, mirroring
//! trusty-agents' `TrustySearchPlugin`), runs the MCP handshake, and resolves
//! the project's index id from `list_indexes`. A single `search_code` tool with
//! a `mode` param routes to trusty-search's real lanes: `semantic`
//! (`search_semantic`), `symbol` (`search_kg`), and `grep` (`grep`). One tool
//! keeps the advertised schema surface minimal and mirrors trusty-search's own
//! lane taxonomy. A `STAGE_NOT_READY` on the semantic/symbol lane (the vector
//! stage is still building on a freshly-indexed repo) transparently retries via
//! the always-available lexical lane and returns those degraded hits instead of
//! empty (issue #2783). Any remaining spawn/handshake/timeout/missing-index
//! condition is fail-open: a non-error `ToolResult` telling the model to use
//! the local `grep`/`glob` tools instead.
//! Test: `tests::pick_index_prefers_exact_match`,
//! `tests::pick_index_falls_back_to_longest_ancestor`,
//! `tests::execute_fail_open_when_binary_absent`,
//! `tests::execute_rejects_malformed_args_recoverably`,
//! `tests::schema_advertises_mode_and_query`, `tests::mcp_text_extracts_content`,
//! `tests::is_mcp_error_detects_error_flag`,
//! `tests::is_stage_not_ready_detects_meta_code`,
//! `tests::should_lexical_fallback_only_for_stage_not_ready`,
//! `tests::lexical_params_matches_search_lexical_schema`.

use std::path::{Path, PathBuf};

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use tokio::sync::{Mutex, OnceCell};
use tracing::warn;
use trusty_common::stdio_mcp_client::StdioMcpClient;

use crate::tools::traits::{ToolExecutor, ToolResult};

/// The tool's registered/advertised name.
pub const SEARCH_CODE_TOOL_NAME: &str = "search_code";

/// Binary spawned in MCP stdio mode (`trusty-search serve`).
const SEARCH_BINARY: &str = "trusty-search";

/// `clientInfo.name` advertised during the MCP handshake.
const CLIENT_NAME: &str = "trusty-code";

/// Default `top_k` when the model omits it.
const DEFAULT_TOP_K: usize = 10;

/// Hard cap on `top_k` regardless of what the model requests.
const MAX_TOP_K: usize = 25;

/// The fail-open message steering the model back to the local literal tools.
const FALLBACK_HINT: &str =
    "trusty-search is unavailable; use the local `grep` and `glob` tools for this query instead.";

/// Parsed `search_code` tool-call arguments.
#[derive(Debug, Deserialize)]
struct SearchArgs {
    query: String,
    #[serde(default)]
    mode: Option<String>,
    #[serde(default)]
    top_k: Option<usize>,
}

/// A live trusty-search MCP session plus the resolved project index.
///
/// Why: MCP requests are not concurrency-safe (one in-flight request per stdio
/// pair), so the client sits behind an async `Mutex`. `index_id` is resolved
/// once at spawn from `list_indexes` because the semantic/KG lanes require it.
/// What: `index_id` is `None` when no registered index covers the project root
/// — the search lanes then fail open rather than guess a wrong index.
/// Test: exercised via `execute_*` and `pick_index_*` in `tests`.
struct SearchSession {
    client: Mutex<StdioMcpClient>,
    index_id: Option<String>,
}

/// `ToolExecutor` for the `search_code` discovery tool (PR A).
///
/// Why: gives the engineer a semantic/call-graph discovery path backed by
/// trusty-search, scoped to the delegating run's project root.
/// What: holds the project root (for index resolution + scoping) and lazily
/// initialises one [`SearchSession`] on first use, caching the (possibly
/// absent) session in a `OnceCell` so a missing/unreachable daemon is probed at
/// most once per tool instance.
/// Test: `tests::execute_fail_open_when_binary_absent`.
pub struct TrustySearchTool {
    project: PathBuf,
    binary: String,
    session: OnceCell<Option<SearchSession>>,
}

impl TrustySearchTool {
    /// Construct a tool scoped to `project`, spawning the real `trusty-search`.
    ///
    /// Why: the engineer's registry builds one per delegation, scoped to the
    /// project working tree.
    /// What: defers all process spawning to first `execute` — construction is
    /// cheap and side-effect-free.
    /// Test: `tests::schema_advertises_mode_and_query`.
    pub fn new(project: impl Into<PathBuf>) -> Self {
        Self {
            project: project.into(),
            binary: SEARCH_BINARY.to_string(),
            session: OnceCell::new(),
        }
    }

    /// Override the spawned binary name (tests point this at a name that is
    /// guaranteed absent to exercise the fail-open path deterministically).
    ///
    /// Why: the daemon-absent path must be unit-testable without depending on
    /// whether `trusty-search` happens to be on the dev machine's PATH.
    /// What: replaces the binary the lazy spawn invokes.
    /// Test: `tests::execute_fail_open_when_binary_absent`.
    #[cfg(test)]
    pub fn with_binary(mut self, binary: impl Into<String>) -> Self {
        self.binary = binary.into();
        self
    }

    /// Lazily spawn + handshake + resolve the index, caching the result.
    ///
    /// Why: probing a missing/broken daemon on every call would add per-turn
    /// latency; caching the `Option` probes at most once.
    /// What: returns `&None` when spawn or handshake fails (fail-open).
    /// Test: `tests::execute_fail_open_when_binary_absent`.
    async fn session(&self) -> &Option<SearchSession> {
        self.session
            .get_or_init(|| spawn_session(&self.binary, &self.project))
            .await
    }
}

/// Spawn `trusty-search serve`, run the handshake, and resolve the index id.
///
/// Why: isolates the fallible startup so `session` stays a thin cache and the
/// whole flow degrades to `None` on the first failure.
/// What: returns `None` if the binary is absent, the handshake fails, or the
/// process cannot start; otherwise a [`SearchSession`] whose `index_id` may
/// still be `None` when no index covers the project.
/// Test: covered opportunistically — a missing binary yields `None`, asserted
/// via `tests::execute_fail_open_when_binary_absent`.
async fn spawn_session(binary: &str, project: &Path) -> Option<SearchSession> {
    let mut client = match StdioMcpClient::spawn(binary, &["serve"], CLIENT_NAME).await {
        Ok(c) => c,
        Err(e) => {
            warn!(error = %e, "search_code: trusty-search spawn failed (fail-open)");
            return None;
        }
    };
    if let Err(e) = client.initialize().await {
        warn!(error = %e, "search_code: trusty-search handshake failed (fail-open)");
        return None;
    }
    let index_id = resolve_index_id(&mut client, project).await;
    Some(SearchSession {
        client: Mutex::new(client),
        index_id,
    })
}

/// Resolve the project's index id from `list_indexes`.
///
/// Why: the semantic/KG lanes require an `index_id`; deriving it from the
/// project root avoids making the model call `list_indexes` and guess.
/// What: calls the `list_indexes` MCP tool, parses the `{ "indexes": [...] }`
/// detail payload, and matches the project root against each index `root_path`
/// via [`pick_index`]. Returns `None` on any RPC/parse failure or no match.
/// Test: `tests::pick_index_prefers_exact_match`.
async fn resolve_index_id(client: &mut StdioMcpClient, project: &Path) -> Option<String> {
    let result = client.call_tool("list_indexes", json!({})).await.ok()?;
    let text = mcp_text(&result)?;
    let body: Value = serde_json::from_str(&text).ok()?;
    let indexes = body.get("indexes")?.as_array()?;
    pick_index(indexes, project)
}

/// Pick the index whose `root_path` best covers `project`.
///
/// Why: a worktree/subdirectory of a registered repo should resolve to that
/// repo's index even when the paths are not byte-identical.
/// What: prefers an exact canonical match; otherwise the registered index whose
/// canonical `root_path` is the longest ancestor of `project`. `None` when no
/// index covers the project.
/// Test: `tests::pick_index_prefers_exact_match`,
/// `tests::pick_index_falls_back_to_longest_ancestor`.
fn pick_index(indexes: &[Value], project: &Path) -> Option<String> {
    let target = canonicalize_best_effort(project);
    let mut best: Option<(usize, String)> = None;
    for entry in indexes {
        let Some(id) = entry.get("id").and_then(Value::as_str) else {
            continue;
        };
        let Some(root) = entry.get("root_path").and_then(Value::as_str) else {
            continue;
        };
        let root = canonicalize_best_effort(Path::new(root));
        if root == target {
            return Some(id.to_string());
        }
        if target.starts_with(&root) {
            let depth = root.components().count();
            if best.as_ref().is_none_or(|(d, _)| depth > *d) {
                best = Some((depth, id.to_string()));
            }
        }
    }
    best.map(|(_, id)| id)
}

/// Canonicalise `p`, falling back to the path as given when it does not exist.
///
/// Why: index roots and the project path may be symlinked (worktrees under
/// `$HOME`); canonicalising both makes ancestor comparison robust.
/// What: `std::fs::canonicalize` with a lossless fallback.
/// Test: exercised via `pick_index_*`.
fn canonicalize_best_effort(p: &Path) -> PathBuf {
    std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf())
}

/// Extract the first text frame from an MCP `tools/call` result.
///
/// Why: trusty-search wraps every payload as `content[0].text` (a JSON string).
/// What: returns that string, or `None` when the frame is missing.
/// Test: `tests::mcp_text_extracts_content`.
fn mcp_text(result: &Value) -> Option<String> {
    result
        .get("content")
        .and_then(|c| c.as_array())
        .and_then(|items| items.first())
        .and_then(|item| item.get("text"))
        .and_then(|t| t.as_str())
        .map(str::to_string)
}

/// Whether an MCP result carries the in-band `isError` flag (e.g.
/// `STAGE_NOT_READY` when the semantic/KG lane is not yet built).
///
/// Why: trusty-search reports stage-not-ready as `isError: true` content rather
/// than a JSON-RPC error, so `call_tool` returns `Ok`; we must inspect the flag
/// to fail open.
/// What: `true` when `result.isError == true`.
/// Test: `tests::is_mcp_error_detects_error_flag`.
fn is_mcp_error(result: &Value) -> bool {
    result
        .get("isError")
        .and_then(Value::as_bool)
        .unwrap_or(false)
}

/// Whether an MCP error result specifically signals a not-yet-ready index stage.
///
/// Why: on a freshly-indexed large repo the semantic (Stage 2) and KG (Stage 3)
/// lanes can stay building for many minutes while the lexical lane is already
/// serving. That transient warm-up window is recoverable via a lexical retry,
/// unlike a genuinely missing index or a dead daemon — so it must be told apart
/// from other in-band errors (issue #2783).
/// What: `true` when trusty-search's structured `_meta.error_code` is
/// `STAGE_NOT_READY` — the machine-readable contract from issue #138. (The
/// human-readable text never carries the literal code, so `_meta` is the only
/// reliable signal.)
/// Test: `tests::is_stage_not_ready_detects_meta_code`.
fn is_stage_not_ready(result: &Value) -> bool {
    result
        .get("_meta")
        .and_then(|m| m.get("error_code"))
        .and_then(Value::as_str)
        == Some("STAGE_NOT_READY")
}

/// Whether a not-ready lane error should be retried on the lexical lane.
///
/// Why: only the semantic/symbol lanes gate on a warm-up stage; the `grep` lane
/// is itself lexical, so a `grep` error is never a stage-not-ready condition to
/// re-route. Isolating the decision keeps `execute` linear and unit-testable
/// without a live daemon (issue #2783).
/// What: `true` when the lane errored with STAGE_NOT_READY and `mode` is not
/// already `grep`.
/// Test: `tests::should_lexical_fallback_only_for_stage_not_ready`.
fn should_lexical_fallback(mode: &str, result: &Value) -> bool {
    mode != "grep" && is_stage_not_ready(result)
}

/// Build the `search_lexical` MCP arguments for a stage-not-ready retry.
///
/// Why: the lexical lane is always available on any indexed project, so it is
/// the safe degraded path while embeddings finish building (issue #2783).
/// What: mirrors trusty-search's `search_lexical` schema (`index_id`, `query`,
/// `top_k`).
/// Test: `tests::lexical_params_matches_search_lexical_schema`.
fn lexical_params(index_id: &str, query: &str, top_k: usize) -> Value {
    json!({ "index_id": index_id, "query": query, "top_k": top_k })
}

/// Retry a not-ready semantic/symbol query on the lexical lane.
///
/// Why: rather than returning empty and pushing the model back to ad-hoc grep
/// (which a daily-driver probe showed derails discovery onto lookalike
/// functions), transparently serve the always-available lexical results during
/// the embedding warm-up window (issue #2783).
/// What: calls `search_lexical`; returns the result text on success, or `None`
/// when the call fails or itself errors (so the caller falls through to the
/// grep/glob hint).
/// Test: exercised end-to-end where a live daemon is available; the decision and
/// argument shaping are covered by `should_lexical_fallback_*` /
/// `lexical_params_*`.
async fn lexical_fallback(
    client: &mut StdioMcpClient,
    index_id: &str,
    query: &str,
    top_k: usize,
) -> Option<String> {
    let result = client
        .call_tool("search_lexical", lexical_params(index_id, query, top_k))
        .await
        .ok()?;
    if is_mcp_error(&result) {
        return None;
    }
    mcp_text(&result)
}

/// Build the MCP arguments for `mode`, returning the tool name + params.
///
/// Why: keeps `execute` linear — each mode maps to one trusty-search lane with
/// its own argument shape (`grep` takes `pattern`/`max_results` and an optional
/// `index_id`; the semantic/KG lanes take `index_id`/`query`/`top_k`).
/// What: returns `Err` with a fail-open message when a semantic/KG lane is
/// requested but no index covers the project.
/// Test: `tests::execute_fail_open_when_binary_absent` (mode routing is covered
/// end-to-end where a live daemon is available).
fn build_call(
    mode: &str,
    query: &str,
    top_k: usize,
    index_id: Option<&str>,
) -> Result<(&'static str, Value), String> {
    match mode {
        "grep" => {
            let mut params = json!({ "pattern": query, "max_results": top_k });
            if let Some(id) = index_id {
                params["index_id"] = json!(id);
            }
            Ok(("grep", params))
        }
        "symbol" => {
            let id = index_id.ok_or_else(|| {
                format!("no trusty-search index covers this project. {FALLBACK_HINT}")
            })?;
            Ok((
                "search_kg",
                json!({ "index_id": id, "query": query, "top_k": top_k }),
            ))
        }
        // Default and "semantic".
        _ => {
            let id = index_id.ok_or_else(|| {
                format!("no trusty-search index covers this project. {FALLBACK_HINT}")
            })?;
            Ok((
                "search_semantic",
                json!({ "index_id": id, "query": query, "top_k": top_k }),
            ))
        }
    }
}

#[async_trait]
impl ToolExecutor for TrustySearchTool {
    fn name(&self) -> &str {
        SEARCH_CODE_TOOL_NAME
    }

    /// JSON schema for `search_code`.
    ///
    /// Why: the description is a first-class LLM prompt — it tells the model to
    /// prefer this tool for conceptual discovery and steers each `mode` to the
    /// right lane, reusing trusty-search's own lane wording.
    /// Test: `tests::schema_advertises_mode_and_query`.
    fn schema(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": SEARCH_CODE_TOOL_NAME,
                "description": "PRIMARY code-discovery tool, backed by trusty-search's semantic + call-graph index. Use it for CONCEPTUAL queries where you don't already know the exact text: \"where is X\", \"how does Y work\", \"find the code that does <behaviour>\", \"what calls Z\". For exact symbol names, literal strings, or filename/path globs you already know, prefer the local `grep`/`glob` tools. If this tool reports trusty-search is unavailable, silently fall back to `grep`/`glob` — do not retry.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "For semantic: a natural-language description of the behaviour/concept. For symbol: a function or type name to expand its callers/callees. For grep: a regex or literal pattern."
                        },
                        "mode": {
                            "type": "string",
                            "enum": ["semantic", "symbol", "grep"],
                            "default": "semantic",
                            "description": "semantic (default): find code by meaning. symbol: explore what calls / is called by a known symbol (call graph). grep: regex/literal search over indexed files."
                        },
                        "top_k": {
                            "type": "integer",
                            "description": "Maximum results (default 10, max 25)."
                        }
                    },
                    "required": ["query"],
                    "additionalProperties": false
                }
            }
        })
    }

    /// Execute a discovery query, fail-open on any unavailability.
    ///
    /// Why: an explicit model tool call must never derail the agent loop — a
    /// missing/unready trusty-search surfaces as a SUCCESSFUL "use grep/glob"
    /// result, not a tool error the model might retry-loop on.
    /// Test: `tests::execute_fail_open_when_binary_absent`,
    /// `tests::execute_rejects_malformed_args_recoverably`.
    async fn execute(&self, args: Value) -> ToolResult {
        let parsed: SearchArgs = match serde_json::from_value(args) {
            Ok(p) => p,
            Err(e) => {
                return ToolResult::err(format!(
                    "search_code arguments did not match the expected shape ({e}). \
                     'query' (string) is required."
                ));
            }
        };
        let top_k = parsed.top_k.unwrap_or(DEFAULT_TOP_K).clamp(1, MAX_TOP_K);
        let mode = parsed.mode.as_deref().unwrap_or("semantic");

        let Some(session) = self.session().await else {
            return ToolResult::ok(FALLBACK_HINT.to_string());
        };

        let (tool, params) =
            match build_call(mode, &parsed.query, top_k, session.index_id.as_deref()) {
                Ok(call) => call,
                Err(msg) => return ToolResult::ok(msg),
            };

        let mut client = session.client.lock().await;
        let result = match client.call_tool(tool, params).await {
            Ok(v) => v,
            Err(e) => {
                warn!(tool, error = %e, "search_code: trusty-search call failed (fail-open)");
                return ToolResult::ok(format!("trusty-search call failed. {FALLBACK_HINT}"));
            }
        };

        if is_mcp_error(&result) {
            let detail = mcp_text(&result).unwrap_or_default();
            // Warm-up window: the semantic/KG stage isn't built yet, but the
            // lexical lane is always available. Retry transparently and serve
            // the (degraded) lexical hits rather than empty — so discovery still
            // works while embeddings finish building (issue #2783).
            if should_lexical_fallback(mode, &result)
                && let Some(id) = session.index_id.as_deref()
                && let Some(text) = lexical_fallback(&mut client, id, &parsed.query, top_k).await
            {
                let lane_label = if mode == "symbol" {
                    "symbol graph"
                } else {
                    "semantic"
                };
                return ToolResult::ok(format!(
                    "trusty-search's {lane_label} index is still building; \
                     showing lexical (exact-match) results instead:\n{text}"
                ));
            }
            warn!(
                tool,
                detail, "search_code: trusty-search returned an error (fail-open)"
            );
            return ToolResult::ok(format!(
                "trusty-search could not serve this query (its index may still be building). \
                 {FALLBACK_HINT}"
            ));
        }

        match mcp_text(&result) {
            Some(text) => ToolResult::ok(format!("trusty-search ({mode}) results:\n{text}")),
            None => ToolResult::ok(format!(
                "trusty-search returned no results. {FALLBACK_HINT}"
            )),
        }
    }
}

#[cfg(test)]
#[path = "trusty_search_tests.rs"]
mod tests;