mahbot 0.4.0

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
//! Backed by `fff-search` — an indexed search engine with fuzzy file
//! name matching, content grep, frecency ranking, constraint filtering,
//! and pagination.
//!
//! All agents searching the same workspace share a single engine instance
//! managed by [`crate::search_engine`]. Background scanning starts eagerly.

use crate::Tool;
use crate::search_engine;
use async_trait::async_trait;
use fff_search::file_picker::FuzzySearchOptions;
use fff_search::grep::{GrepMode, GrepSearchOptions};
use fff_search::parse_grep_query;
use fff_search::{Constraint, GitStatusFilter};
use serde_json::json;
use std::fmt::Write;
use std::sync::LazyLock;

const DEFAULT_MAX_RESULTS: usize = 50;
const MAX_RESULTS_LIMIT: usize = 500;

/// Canonical list of argument aliases for the `"query"` key.
///
/// Tools accept `"pattern"`, `"search"`, `"search_term"`, and `"grep_search"`
/// as aliases for the primary `"query"` argument. This constant is the single
/// source of truth for those aliases, used by [`normalize_search_args`] and
/// [`resolve_query`].
///
/// # Priority ordering
///
/// The iteration order defines alias priority in [`normalize_search_args`]:
/// the first alias present in the argument map wins. Currently:
/// 1. `"pattern"`
/// 2. `"search"`
/// 3. `"search_term"`
/// 4. `"grep_search"`
///
/// [`resolve_query`] intentionally excludes `"grep_search"` — see its
/// documentation for rationale. Do not change this ordering without
/// updating both callers' priority semantics.
pub(crate) const QUERY_ALIAS_KEYS: &[&str] = &["pattern", "search", "search_term", "grep_search"];

/// Valid parameter keys (schema + normalization aliases).
///
/// Used for unknown-parameter detection in the execute path.
///
/// This is the union of:
/// - Schema keys: `mode`, `query`, `grep_mode`, `case_sensitive`,
///   `max_results`, `offset`, `context_lines`
/// - Query aliases: see [`QUERY_ALIAS_KEYS`]
/// - Constraint params: `path`, `ext`
/// - Mode switch: `file_pattern`
///
/// Built at runtime via [`LazyLock`] so that adding a new alias to
/// [`QUERY_ALIAS_KEYS`] automatically propagates here — no manual
/// duplication needed.
static KNOWN_KEYS: LazyLock<Vec<&str>> = LazyLock::new(|| {
    let mut keys = vec![
        // schema keys
        "mode",
        "query",
        "grep_mode",
        "case_sensitive",
        "max_results",
        "offset",
        "context_lines",
    ];
    // query aliases (see QUERY_ALIAS_KEYS)
    keys.extend_from_slice(QUERY_ALIAS_KEYS);
    // mode-switching alias + constraint params
    keys.extend_from_slice(&["file_pattern", "path", "ext"]);
    keys
});

/// Repair common agent mistakes in search tool arguments before execution.
fn normalize_search_args(args: &mut serde_json::Value) {
    let Some(obj) = args.as_object_mut() else {
        return;
    };

    // Query aliases — only when canonical key is absent.
    if !obj.contains_key("query") {
        for alias in QUERY_ALIAS_KEYS {
            if let Some(v) = obj.remove(*alias) {
                obj.insert("query".to_string(), v);
                break;
            }
        }
    }

    // file_pattern → files-mode query when obvious.
    if !obj.contains_key("query")
        && let Some(fp) = obj.remove("file_pattern")
    {
        obj.insert("query".to_string(), fp);
        if !obj.contains_key("mode") {
            obj.insert("mode".to_string(), json!("files"));
        }
    }

    // Top-level mode conflated with grep_mode.
    if let Some(mode) = obj.get("mode").and_then(|v| v.as_str()).map(str::to_string)
        && matches!(mode.as_str(), "plain_text" | "regex" | "content" | "code")
    {
        obj.insert("mode".to_string(), json!("grep"));
        match mode.as_str() {
            "plain_text" => {
                obj.entry("grep_mode".to_string())
                    .or_insert(json!("plain_text"));
            }
            "regex" => {
                obj.entry("grep_mode".to_string()).or_insert(json!("regex"));
            }
            "code" => {
                obj.entry("grep_mode".to_string()).or_insert(json!("fuzzy"));
            }
            _ => {} // "content" — no grep_mode default
        }
    }

    // Invalid grep_mode values observed in production telemetry.
    if let Some(gm) = obj
        .get("grep_mode")
        .and_then(|v| v.as_str())
        .map(str::to_string)
    {
        match gm.as_str() {
            "exact" => {
                obj.insert("grep_mode".to_string(), json!("plain_text"));
            }
            "files" => {
                obj.remove("grep_mode");
                obj.insert("mode".to_string(), json!("files"));
            }
            _ => {}
        }
    }
}

/// Normalize search arguments by resolving the effective query string.
///
/// Handles common agent input mistakes:
/// - `"pattern"` as an alias for `"query"`
/// - Standalone `"path"` key → trailing-slash path segment constraint
/// - Standalone `"ext"` key → glob extension constraint
///
/// Returns `None` when no query components are present.
fn resolve_query(args: &serde_json::Value) -> Option<String> {
    let raw_query = super::get_opt_str(args, "query").or_else(|| {
        // grep_search is intentionally excluded — it is only a
        // normalization alias (consumed by normalize_search_args),
        // not a query alias that resolve_query should recognize.
        // See QUERY_ALIAS_KEYS doc comment for the full list.
        QUERY_ALIAS_KEYS
            .iter()
            .filter(|k| **k != "grep_search")
            .find_map(|alias| super::get_opt_str(args, alias))
    });

    let path_constraint = super::get_opt_str(args, "path")
        .filter(|p| !p.is_empty() && *p != "/")
        .map(|p| {
            let trimmed = p.trim_end_matches('/');
            format!("{trimmed}/")
        });

    let ext_constraint = super::get_opt_str(args, "ext")
        .filter(|e| !e.is_empty())
        .map(|e| {
            // Strip leading dots and asterisks (agents may copy-paste from docs)
            let trimmed = e.trim_start_matches(['.', '*']);
            format!("*.{trimmed}")
        });

    let mut query = String::new();
    if let Some(q) = raw_query.filter(|q| !q.is_empty()) {
        query.push_str(q);
    }
    if let Some(ref p) = path_constraint {
        if !query.is_empty() {
            query.push(' ');
        }
        query.push_str(p);
    }
    if let Some(ref e) = ext_constraint {
        if !query.is_empty() {
            query.push(' ');
        }
        query.push_str(e);
    }

    if query.is_empty() { None } else { Some(query) }
}

/// Resolve a workspace's search engine. Ephemeral per-run workspaces (freshly
/// created research folders with zero files) downgrade the empty-index
/// scan error to a warning — their index legitimately starts empty; real
/// workspaces keep the hard error.
async fn resolve_workspace_engine(
    ws: &crate::Workspace,
) -> anyhow::Result<std::sync::Arc<search_engine::SearchEngineEntry>> {
    match search_engine::resolve_engine(&ws.name, &ws.path, "", ws.ephemeral).await {
        Ok(entry) => Ok(entry),
        Err(e) if ws.ephemeral && search_engine::is_empty_index_error(&e) => {
            tracing::warn!(ws = %ws.name, "Ephemeral workspace has an empty search index — searching a not-yet-written folder");
            // `get_or_init_engine` returns the SAME registered entry (Arc
            // dedup by name) — this recovers the handle to search on, it does
            // NOT re-initialize. Residual (accepted): an index built while the
            // folder was empty stays empty for the run's lifetime — the scan
            // never re-runs, so a coder searching its own run_root after
            // writing prototypes sees no results.
            search_engine::get_or_init_engine(
                &ws.name,
                std::path::Path::new(&ws.path),
                ws.ephemeral,
            )
            .map_err(|e| anyhow::anyhow!(e))
        }
        Err(e) => Err(anyhow::anyhow!(e)),
    }
}

/// Unified workspace search tool.
///
/// - `mode = "files"` — fuzzy file/path name search (replaces `glob`)
/// - `mode = "grep"`  — file content search (replaces `rg`)
#[derive(Default)]
pub struct SearchTool;

impl SearchTool {
    fn search_files(
        entry: &search_engine::SearchEngineEntry,
        query: &str,
        max_results: usize,
        offset: usize,
        constraints: &[Constraint<'_>],
    ) -> anyhow::Result<String> {
        let (paths, total_matched, total_files) =
            Self::fuzzy_file_search(entry, query, max_results, offset)?;
        if paths.is_empty() {
            return Ok(Self::format_files_zero_result(
                query,
                max_results,
                offset,
                constraints,
                total_matched,
                total_files,
            ));
        }

        let mut output = paths.join("\n");
        let total = paths.len();
        let _ = write!(output, "\n\nTotal: {total} files");
        Ok(output)
    }

    /// Run a single fuzzy file search, returning owned relative paths and
    /// match/file counts. Path mapping happens inside because `SearchResult`
    /// borrows from the picker behind the lock guard.
    fn fuzzy_file_search(
        entry: &search_engine::SearchEngineEntry,
        query: &str,
        max_results: usize,
        offset: usize,
    ) -> anyhow::Result<(Vec<String>, usize, usize)> {
        let fff_query = parse_grep_query(query);
        let search_opts = FuzzySearchOptions {
            max_threads: 4,
            current_file: None,
            project_path: None,
            combo_boost_score_multiplier: 10,
            min_combo_count: 2,
            pagination: fff_search::PaginationArgs {
                offset,
                limit: max_results,
            },
        };
        let guard = entry.picker.read().unwrap();
        let Some(picker) = guard.as_ref() else {
            anyhow::bail!("Search engine not yet initialized.")
        };
        let qt_guard = entry.query_tracker.read().unwrap();
        let qt_ref = qt_guard.as_ref();
        let result = picker.fuzzy_search(&fff_query, qt_ref, search_opts);
        let paths = result
            .items
            .iter()
            .map(|file| file.relative_path(picker))
            .collect();
        Ok((paths, result.total_matched, result.total_files))
    }

    fn format_files_zero_result(
        query: &str,
        max_results: usize,
        offset: usize,
        constraints: &[Constraint<'_>],
        total_matched: usize,
        total_files: usize,
    ) -> String {
        let mut diag = format!("No files matching '{query}' found in workspace.\nTotal: 0 files");
        let pagination_past_end = total_matched > 0 && total_matched <= offset;
        diag.push_str("\n── diagnostics ──\n");
        let _ = writeln!(diag, "  total_files: {total_files} (files in index)");
        let _ = writeln!(
            diag,
            "  total_matched: {total_matched} (matches before pagination)"
        );
        let _ = writeln!(diag, "  offset: {offset}  limit: {max_results}");
        if !constraints.is_empty() {
            let _ = writeln!(diag, "  constraints: {}", format_constraints(constraints));
        }
        if pagination_past_end {
            let _ = writeln!(
                diag,
                "  ⚠ offset={offset} exceeds total prefiltered files ({total_matched}) — no files to search. Try offset=0."
            );
        } else if total_files == 0 {
            diag.push_str("  ⚠ index has 0 files — workspace may not be scanned yet\n");
        } else if total_matched == 0 {
            diag.push_str(
                "  ℹ 0 files matched before pagination — try broader query or remove constraints\n",
            );
        }
        diag
    }

    /// Workspace file search helper for other tools (e.g. read path recovery).
    pub(crate) async fn find_file_paths(
        ws: &crate::Workspace,
        query: &str,
        max_results: usize,
    ) -> anyhow::Result<Vec<String>> {
        if !crate::search_engine::registry_initialized() {
            return Ok(vec![]);
        }
        let entry = resolve_workspace_engine(ws).await?;
        let (paths, _, _) = Self::fuzzy_file_search(&entry, query, max_results, 0)?;
        Ok(paths)
    }

    fn search_greps(
        entry: &search_engine::SearchEngineEntry,
        query: &str,
        max_results: usize,
        offset: usize,
        args: &serde_json::Value,
        constraints: &[Constraint<'_>],
    ) -> anyhow::Result<String> {
        let grep_mode_str = super::get_opt_str(args, "grep_mode").unwrap_or("fuzzy");

        let grep_mode = match grep_mode_str {
            "plain_text" => GrepMode::PlainText,
            "regex" => GrepMode::Regex,
            "fuzzy" => GrepMode::Fuzzy,
            _ => {
                anyhow::bail!(
                    "Invalid grep_mode '{grep_mode_str}'. Allowed values: fuzzy, plain_text, regex."
                );
            }
        };

        let case_sensitive = super::get_bool(args, "case_sensitive", true);

        let context_lines = super::get_usize(args, "context_lines", 0);

        let fff_query = parse_grep_query(query);

        let grep_opts = GrepSearchOptions {
            mode: grep_mode,
            smart_case: !case_sensitive,
            max_file_size: super::MAX_FILE_SIZE_BYTES,
            max_matches_per_file: 100,
            file_offset: offset,
            page_limit: max_results,
            time_budget_ms: 10_000, // 10 seconds
            before_context: context_lines,
            after_context: context_lines,
            classify_definitions: false,
            trim_whitespace: true,
            abort_signal: None,
        };

        let guard = entry.picker.read().unwrap();
        let Some(picker) = guard.as_ref() else {
            anyhow::bail!("Search engine not yet initialized.")
        };

        let result = picker.grep(&fff_query, &grep_opts);

        let regex_note = match &result.regex_fallback_error {
            Some(err) => format!(
                "\n\nNote: Regex pattern had an error: {err}. Fell back to plain-text matching."
            ),
            None => String::new(),
        };

        if result.matches.is_empty() {
            return Ok(build_grep_zero_diag(
                query,
                &result,
                max_results,
                offset,
                constraints,
                &regex_note,
            ));
        }

        // Format output (mirrors rg-style: file:line:content with context)
        let mut output = String::new();

        // Group matches by file_index so we can group them per file
        let mut last_file_index: Option<usize> = None;
        for grep_match in &result.matches {
            let rel_path = result.files[grep_match.file_index].relative_path(picker);

            // Separate match groups from different files with a blank line
            if let Some(last) = last_file_index
                && last != grep_match.file_index
            {
                output.push('\n');
            }
            last_file_index = Some(grep_match.file_index);

            let ctx_before_count = grep_match.context_before.len();
            for (i, ctx_line) in grep_match.context_before.iter().enumerate() {
                let offset_back = (ctx_before_count - i) as u64;
                let line_num = grep_match.line_number.saturating_sub(offset_back);
                let _ = writeln!(output, "{rel_path}-{line_num}-{ctx_line}");
            }

            let _ = writeln!(
                output,
                "{rel_path}:{}:{}",
                grep_match.line_number, grep_match.line_content
            );

            for (i, ctx_line) in grep_match.context_after.iter().enumerate() {
                let line_num = grep_match.line_number + (i + 1) as u64;
                let _ = writeln!(output, "{rel_path}-{line_num}-{ctx_line}");
            }
        }

        let _ = write!(
            output,
            "Total: {} matches in {} files{regex_note}",
            result.matches.len(),
            result.files_with_matches
        );

        // Pagination hint: surface next_file_offset for correct file-based pagination
        if result.next_file_offset > 0 {
            let _ = write!(
                output,
                "\n── pagination ──\n  next_file_offset: {} (pass as 'offset' for the next page)",
                result.next_file_offset
            );
        }

        Ok(output)
    }
}

#[async_trait]
impl Tool for SearchTool {
    fn name(&self) -> &'static str {
        "search"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        super::tool_params_schema(
            &json!({
                "mode": {
                    "type": "string",
                    "enum": ["files", "grep"],
                    "description": "'files' = fuzzy file/path name search (ranked, replaces glob). 'grep' = file content search (replaces rg). Note: 'mode' and 'grep_mode' are DIFFERENT parameters — 'mode' selects files vs grep; 'grep_mode' selects the matching strategy within grep."
                },
                "query": {
                    "type": "string",
                    "description": "The search query. Use inline constraint syntax like '*.rs' (extension) or 'src/' (path segment) for filtering — or use the standalone 'ext'/'path' parameters. For 'files': fuzzy text like 'lib.rs' or 'types/query'. For 'grep': pattern to find in file contents (supports regex, plain_text, or fuzzy via grep_mode)."
                },
                "grep_mode": {
                    "type": "string",
                    "enum": ["fuzzy", "plain_text", "regex"],
                    "description": "Grep matching mode (only used when mode='grep', NOT the same as the top-level 'mode' parameter). 'fuzzy' (default) — approximate/similar matching, the recommended starting mode for most searches. 'plain_text' — literal substring search, use for exact identifiers, variable names, or error strings. 'regex' — regex pattern matching, use for complex patterns (patterns with special chars like ., *, +, ?, [], (), {}, ^, $, |, ). Note: lookahead/lookbehind assertions ((?=...), (?!...), (?<=...), (?<!...)) are not supported and will return an error. Ignored when mode='files'."
                },
                "case_sensitive": {
                    "type": "boolean",
                    "description": "Grep mode only, default: true. If true, matches case-sensitively. If false, uses smart-case (case-insensitive when pattern is all-lowercase, case-sensitive otherwise). Ignored in 'files' mode."
                },
                "max_results": {
                    "type": "integer",
                    "description": "Max results to return (default: 50, max: 500). Use with offset for pagination."
                },
                "offset": {
                    "type": "integer",
                    "description": "Pagination offset (default: 0, 0-based). In 'files' mode: skips this many items (item-based). In 'grep' mode: skips this many *files*, not matches (file-based) — use 'next_file_offset' from the previous response for correct pagination."
                },
                "context_lines": {
                    "type": "integer",
                    "description": "Context lines before/after each match (grep mode only, default: 0). Ignored in 'files' mode."
                },
                "path": {
                    "type": "string",
                    "description": "Optional path segment filter. Appends a trailing-slash constraint (e.g. 'src' becomes 'src/') to restrict results to a directory or path prefix. Use this parameter or inline 'src/' syntax in the query — they are additive."
                },
                "ext": {
                    "type": "string",
                    "description": "Optional file extension filter. Appends a glob constraint (e.g. 'rs' becomes '*.rs') to restrict results by extension. Use this parameter or inline '*.rs' syntax in the query — they are additive."
                }
            }),
            &["query"],
        )
    }

    fn should_scrub_output(&self, _args: &serde_json::Value) -> bool {
        false // source code, not secrets
    }

    fn side_effects(&self) -> bool {
        false // indexed search, no mutations
    }

    async fn execute(
        &self,
        ws: &crate::Workspace,
        mut args: serde_json::Value,
    ) -> anyhow::Result<String> {
        normalize_search_args(&mut args);

        let mode = super::get_opt_str(&args, "mode")
            .unwrap_or("grep")
            .to_string();

        // --- Normalize: handle common agent input mistakes ---
        let query = resolve_query(&args).ok_or_else(|| {
            anyhow::anyhow!(
                "Missing required field: 'query'. \
                 Note: the parameter is called 'query' (not 'pattern' or 'search'). \
                 Use the 'ext'/'path' parameters or inline constraints like *.rs, src/, \
                 e.g. {{\"query\": \"my_search\", \"path\": \"src\", \"ext\": \"rs\"}}."
            )
        })?;

        // ── Lookaround detection for regex mode ──────────────────────────
        // The Rust `regex` crate does not support lookahead/lookbehind.
        // Detecting these before dispatch prevents silent plain-text fallback
        // that would return wrong results without the agent knowing.
        //
        // Note: substring scanning may false-positive on patterns like [(?=)]
        // where lookaround syntax appears inside a character class, but such
        // patterns are vanishingly rare in agent-generated regex.
        if mode == "grep" {
            let grep_mode = super::get_opt_str(&args, "grep_mode").unwrap_or("fuzzy");
            if grep_mode == "regex"
                && (query.contains("(?=")
                    || query.contains("(?!")
                    || query.contains("(?<=")
                    || query.contains("(?<!"))
            {
                anyhow::bail!(
                    "This regex pattern uses lookahead/lookbehind assertions which are not supported by the search tool's regex engine. Use plain_text or fuzzy mode instead, or reformulate the pattern without lookaround."
                );
            }
        }

        // ── Unknown parameter detection ────────────────────────────
        // Schema keys + normalization aliases that are valid in args.
        // Any other top-level key is an agent mistake worth warning about.
        let unknown_keys: Vec<&str> = args
            .as_object()
            .map(|obj| {
                obj.keys()
                    .filter_map(|k| {
                        if KNOWN_KEYS.contains(&k.as_str()) {
                            None
                        } else {
                            Some(k.as_str())
                        }
                    })
                    .collect()
            })
            .unwrap_or_default();

        let unknown_param_warning = if unknown_keys.is_empty() {
            None
        } else {
            let keys = unknown_keys.join(", ");
            Some(format!(
                "\n── note ──\n  Unrecognized parameter(s): {keys}. \
                 These were ignored. Valid parameters: {}.",
                KNOWN_KEYS.join(", ")
            ))
        };

        // ── Parse query constraints for diagnostics ─────────────────
        let fff_query = parse_grep_query(&query);
        let query_constraints: Vec<Constraint<'_>> = fff_query.constraints.clone();

        let max_results =
            super::get_usize(&args, "max_results", DEFAULT_MAX_RESULTS).min(MAX_RESULTS_LIMIT);

        if max_results == 0 {
            anyhow::bail!(
                "max_results must be at least 1. Got 0. \
                 Use the default (50) or a positive value."
            );
        }

        let offset = super::get_usize(&args, "offset", 0);

        let entry = resolve_workspace_engine(ws).await?;

        let mut output = match mode.as_str() {
            "files" => Self::search_files(&entry, &query, max_results, offset, &query_constraints),
            "grep" => Self::search_greps(
                &entry,
                &query,
                max_results,
                offset,
                &args,
                &query_constraints,
            ),
            _ => anyhow::bail!("Invalid mode '{mode}'. Allowed values: 'files', 'grep'."),
        }?;

        // Append unknown parameter warning to the result (after the main output,
        // so it doesn't disrupt valid results)
        if let Some(warning) = unknown_param_warning {
            output.push_str(&warning);
        }

        Ok(output)
    }
}

// ── Constraint formatting helpers ─────────────────────────────────────

/// Format a single constraint to human-readable text.
///
/// The `Constraint` enum only implements `Debug` (producing `Extension("rs")`,
/// `FilePath("src/")`, etc.), which is machine-oriented. This function
/// produces agent-friendly labels using the inline query syntax: `*.rs`,
/// `src/`, `!*.md`, etc.
fn format_constraint(constraint: &Constraint<'_>) -> String {
    match constraint {
        Constraint::Extension(s) => format!("*.{s}"),
        Constraint::Glob(s) => format!("glob:{s}"),
        Constraint::Parts(parts) => parts.join(" "),
        Constraint::Text(s) => (*s).to_string(),
        Constraint::Exclude(parts) => format!("!{}", parts.join(" ")),
        Constraint::PathSegment(s) | Constraint::FilePath(s) => format!("{s}/"),
        Constraint::FileType(s) => format!("type:{s}"),
        Constraint::GitStatus(status) => {
            let label = match status {
                GitStatusFilter::Modified => "modified",
                GitStatusFilter::Untracked => "untracked",
                GitStatusFilter::Staged => "staged",
                GitStatusFilter::Unmodified => "unmodified",
            };
            format!("status:{label}")
        }
        Constraint::Not(inner) => format!("!{}", format_constraint(inner)),
    }
}

/// Format a list of constraints as a comma-separated string.
fn format_constraints(constraints: &[Constraint<'_>]) -> String {
    constraints
        .iter()
        .map(|c| format_constraint(c))
        .collect::<Vec<_>>()
        .join(", ")
}

/// Build diagnostic output for a zero-result grep search.
///
/// Extracted from `search_greps` to keep the main function under the
/// clippy `too_many_lines` threshold while providing rich diagnostics
/// for agents to self-correct.
fn build_grep_zero_diag(
    query: &str,
    result: &fff_search::grep::GrepResult<'_>,
    max_results: usize,
    offset: usize,
    constraints: &[Constraint<'_>],
    regex_note: &str,
) -> String {
    let mut diag =
        format!("No matches found for '{query}'.\nTotal: 0 matches in 0 files{regex_note}");

    let pagination_past_end =
        result.next_file_offset == 0 && result.total_files_searched == 0 && offset > 0;

    diag.push_str("\n── diagnostics ──\n");
    let _ = writeln!(
        diag,
        "  total_files: {} (files in index)",
        result.total_files
    );
    let _ = writeln!(
        diag,
        "  total_files_searched: {} (files searched this call)",
        result.total_files_searched
    );
    let _ = writeln!(
        diag,
        "  filtered_file_count: {} (searchable files after filtering: \
         excludes binary, too-large, etc.)",
        result.filtered_file_count
    );
    let _ = writeln!(diag, "  files_with_matches: {}", result.files_with_matches);
    let _ = writeln!(diag, "  offset: {offset}  limit: {max_results}");
    let _ = writeln!(
        diag,
        "  next_file_offset: {} (0 = no more files to search)",
        result.next_file_offset
    );

    if !constraints.is_empty() {
        let _ = writeln!(diag, "  constraints: {}", format_constraints(constraints));
    }

    if pagination_past_end {
        let _ = writeln!(
            diag,
            "  ⚠ offset={offset} exceeds total prefiltered files ({total}) — no files to search. Try offset=0.",
            total = result.filtered_file_count
        );
    } else if result.next_file_offset > 0 {
        // There are more files to search but the current offset yielded no matches.
        // This can happen when the offset lands in a gap between matching files.
        let _ = writeln!(
            diag,
            "  ℹ offset={} covered the searched range but found no matches — \
             try offset={} to continue searching further files",
            offset, result.next_file_offset
        );
    } else if result.total_files == 0 {
        diag.push_str("  ⚠ index has 0 files — workspace may not be scanned yet\n");
    } else if result.filtered_file_count == 0
        && result.total_files_searched == 0
        && result.total_files > 0
    {
        diag.push_str(
            "  ℹ all files filtered out by constraints — try broader query or fewer constraints\n",
        );
    } else if offset > 0 && result.total_files_searched > 0 && result.next_file_offset == 0 {
        diag.push_str(
            "  ⚠ offset may be stale from a previous search — try offset=0 for a fresh search\n",
        );
    } else if result.files_with_matches == 0 {
        diag.push_str(
            "  ℹ 0 files matched — try broader query, different grep_mode, or remove constraints\n",
        );
    }

    diag
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    struct NormalizeCase {
        name: &'static str,
        input: serde_json::Value,
        expected: serde_json::Value,
    }

    struct ResolveCase {
        name: &'static str,
        input: serde_json::Value,
        expected: Option<&'static str>,
    }

    // ── normalize_search_args ──────────────────────────────────────────

    /// Assert that normalizing `input` produces `expected`.
    fn assert_normalize(input: &serde_json::Value, expected: &serde_json::Value, name: &str) {
        let mut args = input.clone();
        normalize_search_args(&mut args);
        assert_eq!(args, *expected, "case '{name}' failed");
    }

    /// Table-driven test for query alias resolution: pattern / search /
    /// search_term / grep_search → canonical "query" key, priority ordering,
    /// and the invariant that an existing "query" blocks alias consumption.
    #[test]
    fn normalize_search_args_alias_mapping() {
        let cases = [
            NormalizeCase {
                name: "pattern alias maps to query",
                input: json!({"pattern": "foo"}),
                expected: json!({"query": "foo"}),
            },
            NormalizeCase {
                name: "search alias maps to query",
                input: json!({"search": "bar"}),
                expected: json!({"query": "bar"}),
            },
            NormalizeCase {
                name: "search_term alias maps to query",
                input: json!({"search_term": "baz"}),
                expected: json!({"query": "baz"}),
            },
            NormalizeCase {
                name: "grep_search alias maps to query",
                input: json!({"grep_search": "qux"}),
                expected: json!({"query": "qux"}),
            },
            NormalizeCase {
                name: "pattern wins over other aliases",
                input: json!({
                    "pattern": "from_pattern",
                    "search": "from_search",
                    "search_term": "from_search_term",
                    "grep_search": "from_grep_search"
                }),
                expected: json!({
                    "query": "from_pattern",
                    // Non-winning aliases remain as orphan keys
                    "search": "from_search",
                    "search_term": "from_search_term",
                    "grep_search": "from_grep_search",
                }),
            },
            NormalizeCase {
                name: "search wins when pattern absent",
                input: json!({
                    "search": "from_search",
                    "search_term": "from_search_term"
                }),
                expected: json!({
                    "query": "from_search",
                    // search_term remains as orphan key
                    "search_term": "from_search_term",
                }),
            },
            NormalizeCase {
                name: "existing query blocks alias, alias stays as orphan",
                input: json!({"query": "existing", "pattern": "alias"}),
                expected: json!({"query": "existing", "pattern": "alias"}),
            },
        ];
        for c in &cases {
            assert_normalize(&c.input, &c.expected, c.name);
        }
    }

    /// Table-driven test for file_pattern → query mapping, including
    /// mode inference and the orphan-key behavior when query already exists.
    #[test]
    fn normalize_search_args_file_pattern() {
        let cases = [
            NormalizeCase {
                name: "file_pattern without existing mode sets mode=files",
                input: json!({"file_pattern": "*.rs"}),
                expected: json!({"query": "*.rs", "mode": "files"}),
            },
            NormalizeCase {
                name: "file_pattern preserves existing mode",
                input: json!({"file_pattern": "main.rs", "mode": "grep"}),
                expected: json!({"query": "main.rs", "mode": "grep"}),
            },
            NormalizeCase {
                name: "file_pattern orphaned when query already exists",
                input: json!({"query": "already", "file_pattern": "other.rs"}),
                expected: json!({"query": "already", "file_pattern": "other.rs"}),
            },
            NormalizeCase {
                name: "file_pattern blocked by prior alias",
                input: json!({"pattern": "my_query", "file_pattern": "*.rs"}),
                expected: json!({"query": "my_query", "file_pattern": "*.rs"}),
            },
        ];
        for c in &cases {
            assert_normalize(&c.input, &c.expected, c.name);
        }
    }

    /// Table-driven test for mode-value remapping.
    /// `plain_text`, `regex`, `content`, and `code` are all rewritten to
    /// `"grep"` (with an implied `grep_mode`), while `"files"` and unknown
    /// values pass through unchanged.
    #[test]
    fn normalize_search_args_mode_mapping() {
        let cases = [
            // ── plain_text ──────────────────────────────────────────────
            NormalizeCase {
                name: "plain_text sets mode=grep and grep_mode=plain_text",
                input: json!({"mode": "plain_text"}),
                expected: json!({"mode": "grep", "grep_mode": "plain_text"}),
            },
            NormalizeCase {
                name: "plain_text does not overwrite existing grep_mode",
                input: json!({"mode": "plain_text", "grep_mode": "fuzzy"}),
                expected: json!({"mode": "grep", "grep_mode": "fuzzy"}),
            },
            // ── regex ───────────────────────────────────────────────────
            NormalizeCase {
                name: "regex sets mode=grep and grep_mode=regex",
                input: json!({"mode": "regex"}),
                expected: json!({"mode": "grep", "grep_mode": "regex"}),
            },
            // ── content ─────────────────────────────────────────────────
            NormalizeCase {
                name: "content sets mode=grep without grep_mode",
                input: json!({"mode": "content"}),
                expected: json!({"mode": "grep"}),
            },
            // ── code ────────────────────────────────────────────────────
            NormalizeCase {
                name: "code sets mode=grep and grep_mode=fuzzy",
                input: json!({"mode": "code"}),
                expected: json!({"mode": "grep", "grep_mode": "fuzzy"}),
            },
            NormalizeCase {
                name: "code does not overwrite existing grep_mode",
                input: json!({"mode": "code", "grep_mode": "regex"}),
                expected: json!({"mode": "grep", "grep_mode": "regex"}),
            },
            // ── files (pass-through) ────────────────────────────────────
            NormalizeCase {
                name: "files mode unchanged",
                input: json!({"mode": "files", "query": "lib.rs"}),
                expected: json!({"mode": "files", "query": "lib.rs"}),
            },
            // ── unknown (pass-through) ──────────────────────────────────
            NormalizeCase {
                name: "unknown mode passes through unchanged",
                input: json!({"mode": "unknown_value"}),
                expected: json!({"mode": "unknown_value"}),
            },
        ];
        for c in &cases {
            assert_normalize(&c.input, &c.expected, c.name);
        }
    }

    /// Table-driven test for grep_mode rewrites.
    ///
    /// The first case covers the subtle double-mapping interaction:
    /// mode=plain_text sets grep_mode=plain_text (stage 1), then the
    /// grep_mode remapping sees the already-valid "plain_text" value and
    /// leaves it unchanged rather than rewriting the original "exact".
    #[test]
    fn normalize_search_args_grep_mode_mapping() {
        let cases = [
            // ════════════════════════════════════════════════════════════
            // Double-mapping: mode remapping runs BEFORE grep_mode
            // remapping.  "plain_text" mode first sets grep_mode to
            // "plain_text" (since grep_mode is absent), then the
            // grep_mode remapping sees the already-valid "plain_text"
            // and leaves it — it does NOT re-process the original "exact".
            // ════════════════════════════════════════════════════════════
            NormalizeCase {
                name: "plain_text mode + exact grep_mode — mode remaps first, grep_mode sees already-valid plain_text",
                input: json!({"mode": "plain_text", "grep_mode": "exact"}),
                expected: json!({"mode": "grep", "grep_mode": "plain_text"}),
            },
            // ── exact → plain_text ──────────────────────────────────────
            NormalizeCase {
                name: "exact grep_mode remapped to plain_text",
                input: json!({"grep_mode": "exact"}),
                expected: json!({"grep_mode": "plain_text"}),
            },
            // ── files → mode=files with grep_mode removed ──────────────
            NormalizeCase {
                name: "files grep_mode promotes mode to files and removes grep_mode",
                input: json!({"grep_mode": "files"}),
                expected: json!({"mode": "files"}),
            },
            NormalizeCase {
                name: "files grep_mode with existing mode=files is fine",
                input: json!({"mode": "files", "grep_mode": "files"}),
                expected: json!({"mode": "files"}),
            },
            NormalizeCase {
                name: "files grep_mode overrides non-files mode to files",
                input: json!({"mode": "grep", "grep_mode": "files"}),
                expected: json!({"mode": "files"}),
            },
        ];
        for c in &cases {
            assert_normalize(&c.input, &c.expected, c.name);
        }
    }

    /// Edge cases: non-object values and empty objects pass through unchanged.
    #[test]
    fn normalize_search_args_edge_cases() {
        let cases = [
            NormalizeCase {
                name: "non-object string passes through unchanged",
                input: json!("string_value"),
                expected: json!("string_value"),
            },
            NormalizeCase {
                name: "non-object number passes through unchanged",
                input: json!(42),
                expected: json!(42),
            },
            NormalizeCase {
                name: "non-object null passes through unchanged",
                input: json!(null),
                expected: json!(null),
            },
            NormalizeCase {
                name: "non-object array passes through unchanged",
                input: json!([1, 2, 3]),
                expected: json!([1, 2, 3]),
            },
            NormalizeCase {
                name: "empty object stays unchanged",
                input: json!({}),
                expected: json!({}),
            },
        ];
        for c in &cases {
            assert_normalize(&c.input, &c.expected, c.name);
        }
    }

    // ── QUERY_ALIAS_KEYS / KNOWN_KEYS consistency ──────────────────────

    #[test]
    fn known_keys_contains_all_expected_keys() {
        // Sanity check: KNOWN_KEYS should contain everything we expect.
        // If this test fails after adding new schema keys, update KNOWN_KEYS.
        let expected: std::collections::HashSet<&str> = std::collections::HashSet::from([
            // schema keys
            "mode",
            "query",
            "grep_mode",
            "case_sensitive",
            "max_results",
            "offset",
            "context_lines",
            // query aliases (must match QUERY_ALIAS_KEYS)
            "pattern",
            "search",
            "search_term",
            "grep_search",
            // mode-switching alias
            "file_pattern",
            // constraint params
            "path",
            "ext",
        ]);

        let actual: std::collections::HashSet<&str> = KNOWN_KEYS.iter().copied().collect();

        // Every expected key is present
        for key in &expected {
            assert!(
                actual.contains(key),
                "Expected KNOWN_KEYS to contain \"{key}\" but it is missing"
            );
        }

        // Every actual key is expected (catches stale entries after refactoring)
        for key in &actual {
            assert!(
                expected.contains(key),
                "KNOWN_KEYS contains unexpected key \"{key}\"; remove it or update the expected set"
            );
        }
    }

    // ── resolve_query ──────────────────────────────────────────────────

    /// Assert that resolving `input` produces `expected`.
    fn assert_resolve(input: &serde_json::Value, expected: Option<&str>, name: &str) {
        let result = resolve_query(input);
        assert_eq!(result.as_deref(), expected, "case '{name}' failed");
    }

    /// Table-driven test for query alias resolution: canonical "query"
    /// takes priority, aliases follow pattern > search > search_term,
    /// and grep_search is intentionally excluded from resolve_query.
    #[test]
    fn resolve_query_alias_priority() {
        let cases = [
            // ── Individual aliases ──────────────────────────────────────
            ResolveCase {
                name: "pattern alias resolves to query",
                input: json!({"pattern": "struct Foo"}),
                expected: Some("struct Foo"),
            },
            ResolveCase {
                name: "search alias resolves to query",
                input: json!({"search": "bar"}),
                expected: Some("bar"),
            },
            ResolveCase {
                name: "search_term alias resolves to query",
                input: json!({"search_term": "baz"}),
                expected: Some("baz"),
            },
            ResolveCase {
                name: "grep_search is NOT recognized by resolve_query",
                input: json!({"grep_search": "qux"}),
                expected: None,
            },
            // ── Priority ────────────────────────────────────────────────
            ResolveCase {
                name: "canonical query overrides all aliases",
                input: json!({
                    "query": "primary",
                    "pattern": "secondary",
                    "search": "tertiary",
                    "search_term": "quaternary",
                }),
                expected: Some("primary"),
            },
            ResolveCase {
                name: "pattern overrides search and search_term",
                input: json!({
                    "pattern": "from_pattern",
                    "search": "from_search",
                    "search_term": "from_term",
                }),
                expected: Some("from_pattern"),
            },
            ResolveCase {
                name: "search overrides search_term",
                input: json!({
                    "search": "from_search",
                    "search_term": "from_term",
                }),
                expected: Some("from_search"),
            },
        ];
        for c in &cases {
            assert_resolve(&c.input, c.expected, c.name);
        }
    }

    /// Table-driven test for path constraint normalization:
    /// trailing-slash enforcement and empty/root suppression.
    #[test]
    fn resolve_query_path_constraint() {
        let cases = [
            ResolveCase {
                name: "path gets trailing slash",
                input: json!({"path": "src"}),
                expected: Some("src/"),
            },
            ResolveCase {
                name: "path with trailing slash deduplicated",
                input: json!({"path": "src/"}),
                expected: Some("src/"),
            },
            ResolveCase {
                name: "empty path returns None",
                input: json!({"path": ""}),
                expected: None,
            },
            ResolveCase {
                name: "root path returns None",
                input: json!({"path": "/"}),
                expected: None,
            },
        ];
        for c in &cases {
            assert_resolve(&c.input, c.expected, c.name);
        }
    }

    /// Table-driven test for ext constraint normalization:
    /// glob prefix addition, leading-dot/asterisk stripping, empty suppression.
    #[test]
    fn resolve_query_ext_constraint() {
        let cases = [
            ResolveCase {
                name: "ext gets glob prefix",
                input: json!({"ext": "rs"}),
                expected: Some("*.rs"),
            },
            ResolveCase {
                name: "leading dot stripped from ext",
                input: json!({"ext": ".rs"}),
                expected: Some("*.rs"),
            },
            ResolveCase {
                name: "leading asterisk stripped from ext",
                input: json!({"ext": "*.rs"}),
                expected: Some("*.rs"),
            },
            ResolveCase {
                name: "multiple leading asterisks stripped from ext",
                input: json!({"ext": "**.rs"}),
                expected: Some("*.rs"),
            },
            ResolveCase {
                name: "empty ext returns None",
                input: json!({"ext": ""}),
                expected: None,
            },
        ];
        for c in &cases {
            assert_resolve(&c.input, c.expected, c.name);
        }
    }

    /// Table-driven test for constraint combinations and empty-query behavior.
    #[test]
    fn resolve_query_combinations_and_empty() {
        let cases = [
            // ── Constraint combinations ─────────────────────────────────
            ResolveCase {
                name: "query with path constraint",
                input: json!({"query": "foo", "path": "src"}),
                expected: Some("foo src/"),
            },
            ResolveCase {
                name: "query with ext constraint",
                input: json!({"query": "foo", "ext": "rs"}),
                expected: Some("foo *.rs"),
            },
            ResolveCase {
                name: "query with path and ext constraints",
                input: json!({"query": "foo", "path": "src", "ext": "rs"}),
                expected: Some("foo src/ *.rs"),
            },
            ResolveCase {
                name: "path and ext without explicit query",
                input: json!({"path": "src", "ext": "rs"}),
                expected: Some("src/ *.rs"),
            },
            // ── Empty query behavior ────────────────────────────────────
            ResolveCase {
                name: "no query components returns None",
                input: json!({}),
                expected: None,
            },
            ResolveCase {
                name: "explicit empty query string returns None",
                input: json!({"query": ""}),
                expected: None,
            },
            ResolveCase {
                name: "empty query with path returns path constraint only",
                input: json!({"query": "", "path": "src"}),
                expected: Some("src/"),
            },
            ResolveCase {
                name: "empty query with ext returns ext constraint only",
                input: json!({"query": "", "ext": "rs"}),
                expected: Some("*.rs"),
            },
        ];
        for c in &cases {
            assert_resolve(&c.input, c.expected, c.name);
        }
    }

    /// Non-string values are silently ignored (filtered by get_opt_str).
    #[test]
    fn resolve_query_non_string_values() {
        let cases = [
            ResolveCase {
                name: "non-string query returns None",
                input: json!({"query": 42}),
                expected: None,
            },
            ResolveCase {
                name: "non-string path silently skipped, query still resolves",
                input: json!({"query": "foo", "path": 42}),
                expected: Some("foo"),
            },
            ResolveCase {
                name: "non-string ext silently skipped, query still resolves",
                input: json!({"query": "foo", "ext": true}),
                expected: Some("foo"),
            },
        ];
        for c in &cases {
            assert_resolve(&c.input, c.expected, c.name);
        }
    }

    // ── format_files_zero_result ───────────────────────────────────────

    /// Lock the zero-result diagnostics format (byte-identical output).
    #[test]
    fn format_files_zero_result_diagnostics() {
        let constraints = [Constraint::Extension("rs")];
        let diag = SearchTool::format_files_zero_result("foo", 50, 0, &constraints, 0, 123);
        assert_eq!(
            diag,
            "No files matching 'foo' found in workspace.\n\
             Total: 0 files\n\
             ── diagnostics ──\n  \
             total_files: 123 (files in index)\n  \
             total_matched: 0 (matches before pagination)\n  \
             offset: 0  limit: 50\n  \
             constraints: *.rs\n  \
             ℹ 0 files matched before pagination — try broader query or remove constraints\n"
        );

        // Pagination-past-end branch: offset past the pre-pagination matches.
        let diag = SearchTool::format_files_zero_result("bar", 50, 40, &[], 30, 200);
        assert_eq!(
            diag,
            "No files matching 'bar' found in workspace.\n\
             Total: 0 files\n\
             ── diagnostics ──\n  \
             total_files: 200 (files in index)\n  \
             total_matched: 30 (matches before pagination)\n  \
             offset: 40  limit: 50\n  \
             ⚠ offset=40 exceeds total prefiltered files (30) — no files to search. Try offset=0.\n"
        );
    }
}