rab-agent 0.1.4

rab is a lightweight, extensible, Rust-based coding agent.
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
use crate::agent::extension::{Extension, ToolDefinition, ToolRenderContext, ToolRenderer};
use crate::tui::Theme;
use crate::tui::ThemeKey;
use async_trait::async_trait;
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::sync::Arc;

// ── Shared command execution ─────────────────────────────────────

/// Output of a shell command execution.
pub struct ExecOutput {
    pub stdout: String,
    pub stderr: String,
    pub exit_code: Option<i32>,
}

/// Run a shell command in the given working directory (shared helper for default ops).
async fn run_shell_command(command: &str, cwd: &Path) -> anyhow::Result<ExecOutput> {
    let output = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(command)
        .current_dir(cwd)
        .output()
        .await?;
    Ok(ExecOutput {
        stdout: String::from_utf8_lossy(&output.stdout).to_string(),
        stderr: String::from_utf8_lossy(&output.stderr).to_string(),
        exit_code: output.status.code(),
    })
}

// ── GrepOperations (pluggable) ──────────────────────────────────

/// Pluggable operations for the grep tool (matching pi's GrepOperations).
/// Override these to delegate command execution to remote systems.
#[async_trait]
pub trait GrepOperations: Send + Sync {
    /// Execute a shell command in the given working directory.
    /// Returns stdout, stderr, and exit code.
    async fn exec(&self, command: &str, cwd: &Path) -> anyhow::Result<ExecOutput>;
}

struct DefaultGrepOperations;

#[async_trait]
impl GrepOperations for DefaultGrepOperations {
    async fn exec(&self, command: &str, cwd: &Path) -> anyhow::Result<ExecOutput> {
        run_shell_command(command, cwd).await
    }
}

// ── FindOperations (pluggable) ───────────────────────────────────

/// Pluggable operations for the find tool (matching pi's FindOperations).
/// Override these to delegate command execution to remote systems.
#[async_trait]
pub trait FindOperations: Send + Sync {
    /// Execute a shell command in the given working directory.
    /// Returns stdout, stderr, and exit code.
    async fn exec(&self, command: &str, cwd: &Path) -> anyhow::Result<ExecOutput>;
}

struct DefaultFindOperations;

#[async_trait]
impl FindOperations for DefaultFindOperations {
    async fn exec(&self, command: &str, cwd: &Path) -> anyhow::Result<ExecOutput> {
        run_shell_command(command, cwd).await
    }
}

// ── LsOperations (pluggable) ─────────────────────────────────────

/// A directory entry for the ls tool.
pub struct DirEntry {
    pub name: String,
    pub is_dir: bool,
}

/// Pluggable operations for the ls tool (matching pi's LsOperations).
/// Override these to delegate directory listing to remote systems.
pub trait LsOperations: Send + Sync {
    /// List entries in a directory, returning name and type.
    fn read_dir(&self, path: &Path) -> anyhow::Result<Vec<DirEntry>>;
    /// Check if path is a directory.
    fn is_dir(&self, path: &Path) -> anyhow::Result<bool>;
    /// Check if path exists.
    fn path_exists(&self, path: &Path) -> anyhow::Result<bool>;
}

struct DefaultLsOperations;

impl LsOperations for DefaultLsOperations {
    fn read_dir(&self, path: &Path) -> anyhow::Result<Vec<DirEntry>> {
        let rd = std::fs::read_dir(path)?;
        let mut items: Vec<DirEntry> = rd
            .flatten()
            .map(|entry| DirEntry {
                name: entry.file_name().to_string_lossy().to_string(),
                is_dir: entry.file_type().map(|t| t.is_dir()).unwrap_or(false),
            })
            .collect();
        items.sort_by_key(|e| e.name.to_lowercase());
        Ok(items)
    }
    fn is_dir(&self, path: &Path) -> anyhow::Result<bool> {
        Ok(std::fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false))
    }
    fn path_exists(&self, path: &Path) -> anyhow::Result<bool> {
        Ok(path.exists())
    }
}

/// Extension providing grep, find, and ls tools.
pub struct FileSearchExtension {
    cwd: PathBuf,
    grep_operations: Arc<dyn GrepOperations>,
    find_operations: Arc<dyn FindOperations>,
    ls_operations: Arc<dyn LsOperations>,
}

impl FileSearchExtension {
    pub fn new(cwd: PathBuf) -> Self {
        Self {
            cwd,
            grep_operations: Arc::new(DefaultGrepOperations),
            find_operations: Arc::new(DefaultFindOperations),
            ls_operations: Arc::new(DefaultLsOperations),
        }
    }

    /// Set custom grep operations (e.g. for SSH targets).
    pub fn with_grep_operations(mut self, ops: Arc<dyn GrepOperations>) -> Self {
        self.grep_operations = ops;
        self
    }

    /// Set custom find operations (e.g. for SSH targets).
    pub fn with_find_operations(mut self, ops: Arc<dyn FindOperations>) -> Self {
        self.find_operations = ops;
        self
    }

    /// Set custom ls operations (e.g. for SSH targets).
    pub fn with_ls_operations(mut self, ops: Arc<dyn LsOperations>) -> Self {
        self.ls_operations = ops;
        self
    }
}

impl Extension for FileSearchExtension {
    fn name(&self) -> Cow<'static, str> {
        "file_search".into()
    }

    fn tools(&self) -> Vec<ToolDefinition> {
        vec![
            ToolDefinition {
                tool: Box::new(GrepTool {
                    cwd: self.cwd.clone(),
                    operations: self.grep_operations.clone(),
                }),
                snippet: "Search file contents for patterns (respects .gitignore)",
                guidelines: &["Use grep for searching file contents with patterns"],
                prepare_arguments: None,
                before_tool_call: None,
                after_tool_call: None,
                renderer: Some(std::sync::Arc::new(ListRenderer::grep())),
            },
            ToolDefinition {
                tool: Box::new(FindTool {
                    cwd: self.cwd.clone(),
                    operations: self.find_operations.clone(),
                }),
                snippet: "Find files by glob pattern (respects .gitignore)",
                guidelines: &["Use find for locating files by pattern"],
                prepare_arguments: None,
                before_tool_call: None,
                after_tool_call: None,
                renderer: Some(std::sync::Arc::new(ListRenderer::find())),
            },
            ToolDefinition {
                tool: Box::new(LsTool {
                    cwd: self.cwd.clone(),
                    operations: self.ls_operations.clone(),
                }),
                snippet: "List directory contents",
                guidelines: &["Use ls for exploring directory structure"],
                prepare_arguments: None,
                before_tool_call: None,
                after_tool_call: None,
                renderer: Some(std::sync::Arc::new(ListRenderer::ls())),
            },
        ]
    }
}

// ── Constants ────────────────────────────────────────────────────

const GREP_DEFAULT_LIMIT: u64 = 100;
const GREP_MAX_LINE_LENGTH: usize = 500;
const FIND_DEFAULT_LIMIT: u64 = 1000;
const LS_DEFAULT_LIMIT: u64 = 500;

// =====================================================================
// grep tool
// =====================================================================

struct GrepTool {
    cwd: PathBuf,
    operations: Arc<dyn GrepOperations>,
}

#[async_trait]
impl yoagent::types::AgentTool for GrepTool {
    fn name(&self) -> &str {
        "grep"
    }
    fn label(&self) -> &str {
        "grep"
    }
    fn description(&self) -> &str {
        "Search file contents for a pattern. Returns matching lines with file paths and line numbers. \
         Respects .gitignore. Output is truncated to 100 matches. \
         Long lines are truncated to 500 chars."
    }
    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "required": ["pattern"],
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Search pattern (regex or literal string)"
                },
                "path": {
                    "type": "string",
                    "description": "Directory or file to search (default: current directory)"
                },
                "glob": {
                    "type": "string",
                    "description": "Filter files by glob pattern, e.g. '*.rs' or '**/*.spec.rs'"
                },
                "ignoreCase": {
                    "type": "boolean",
                    "description": "Case-insensitive search (default: false)"
                },
                "literal": {
                    "type": "boolean",
                    "description": "Treat pattern as literal string instead of regex (default: false)"
                },
                "context": {
                    "type": "number",
                    "description": "Number of lines to show before and after each match (default: 0)"
                },
                "limit": {
                    "type": "number",
                    "description": "Maximum number of matches to return (default: 100)"
                }
            }
        })
    }

    async fn execute(
        &self,
        params: serde_json::Value,
        ctx: yoagent::types::ToolContext,
    ) -> Result<yoagent::types::ToolResult, yoagent::types::ToolError> {
        let pattern = params["pattern"].as_str().ok_or_else(|| {
            yoagent::types::ToolError::InvalidArgs("Missing 'pattern' argument".into())
        })?;
        let search_path = params["path"].as_str().unwrap_or(".");
        let search_owned = resolve_path(search_path, &self.cwd);
        let abs_search = &search_owned;

        let glob = params["glob"].as_str();
        let ignore_case = params["ignoreCase"].as_bool().unwrap_or(false);
        let literal = params["literal"].as_bool().unwrap_or(false);
        let context = params["context"].as_u64().unwrap_or(0);
        let limit = params["limit"].as_u64().unwrap_or(GREP_DEFAULT_LIMIT);

        if !abs_search.exists() {
            return Err(yoagent::types::ToolError::Failed(format!(
                "Path not found: {}",
                abs_search.display()
            )));
        }

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        // Try ripgrep first, fall back to grep
        let output = if let Some(rg) = which("rg") {
            run_rg_with_ops(
                self.operations.as_ref(),
                &self.cwd,
                &rg,
                pattern,
                abs_search,
                glob,
                ignore_case,
                literal,
                context,
                limit,
            )
            .await?
        } else {
            run_grep_with_ops(
                self.operations.as_ref(),
                &self.cwd,
                pattern,
                abs_search,
                ignore_case,
                literal,
                context,
                limit,
            )
            .await?
        };

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        Ok(yoagent::types::ToolResult {
            content: vec![yoagent::types::Content::Text { text: output }],
            details: serde_json::Value::Null,
        })
    }
}

/// Build a ripgrep command string and execute via operations.
#[allow(clippy::too_many_arguments)]
async fn run_rg_with_ops(
    ops: &dyn GrepOperations,
    cwd: &Path,
    rg: &Path,
    pattern: &str,
    search_path: &Path,
    glob: Option<&str>,
    ignore_case: bool,
    literal: bool,
    context: u64,
    limit: u64,
) -> Result<String, yoagent::types::ToolError> {
    let mut cmd_parts: Vec<String> = vec![
        rg.to_string_lossy().to_string(),
        "--json".into(),
        "--line-number".into(),
        "--color=never".into(),
        "--hidden".into(),
    ];
    if ignore_case {
        cmd_parts.push("--ignore-case".into());
    }
    if literal {
        cmd_parts.push("--fixed-strings".into());
    }
    if let Some(g) = glob {
        cmd_parts.push("--glob".into());
        cmd_parts.push(g.to_string());
    }
    if context > 0 {
        cmd_parts.push("-C".into());
        cmd_parts.push(context.to_string());
    }
    cmd_parts.push("--max-count".into());
    cmd_parts.push(limit.to_string());
    cmd_parts.push("--".into());
    cmd_parts.push(pattern.to_string());
    cmd_parts.push(search_path.to_string_lossy().to_string());

    let command = cmd_parts.join(" ");
    let exec_output = ops
        .exec(&command, cwd)
        .await
        .map_err(|e| yoagent::types::ToolError::Failed(format!("Failed to run rg: {}", e)))?;

    let exit_code = exec_output.exit_code.unwrap_or(-1);
    if exit_code == 2 {
        return Err(yoagent::types::ToolError::Failed(format!(
            "ripgrep error: {}",
            exec_output.stderr.trim()
        )));
    }

    let stdout = &exec_output.stdout;
    let mut results: Vec<String> = Vec::new();
    let mut line_count = 0u64;

    for line in stdout.lines() {
        if line.trim().is_empty() {
            continue;
        }
        if line_count >= limit {
            break;
        }

        if let Ok(event) = serde_json::from_str::<serde_json::Value>(line)
            && event["type"] == "match"
            && let (Some(file_path), Some(line_number), Some(line_text)) = (
                event["data"]["path"]["text"].as_str(),
                event["data"]["line_number"].as_u64(),
                event["data"]["lines"]["text"].as_str(),
            )
        {
            let relative = relativize_path(file_path, search_path);
            let sanitized = line_text
                .replace('\r', "")
                .trim_end_matches('\n')
                .to_string();
            results.push(format!(
                "{}:{}: {}",
                relative,
                line_number,
                truncate_line(&sanitized, GREP_MAX_LINE_LENGTH)
            ));
            line_count += 1;
        }
    }

    if results.is_empty() {
        return Ok("No matches found".to_string());
    }

    Ok(results.join("\n"))
}

#[allow(clippy::too_many_arguments)]
async fn run_grep_with_ops(
    ops: &dyn GrepOperations,
    cwd: &Path,
    pattern: &str,
    search_path: &Path,
    ignore_case: bool,
    literal: bool,
    context: u64,
    limit: u64,
) -> Result<String, yoagent::types::ToolError> {
    let mut cmd_parts: Vec<String> = vec![
        "grep".into(),
        "--line-number".into(),
        "--color=never".into(),
        "--binary-files=without-match".into(),
    ];
    if ignore_case {
        cmd_parts.push("-i".into());
    }
    if literal {
        cmd_parts.push("-F".into());
    }
    if context > 0 {
        cmd_parts.push("-C".into());
        cmd_parts.push(context.to_string());
    }
    cmd_parts.push("--max-count".into());
    cmd_parts.push(limit.to_string());
    cmd_parts.push("-r".into());
    cmd_parts.push("--".into());
    cmd_parts.push(pattern.to_string());
    cmd_parts.push(search_path.to_string_lossy().to_string());

    let command = cmd_parts.join(" ");
    let exec_output = ops
        .exec(&command, cwd)
        .await
        .map_err(|e| yoagent::types::ToolError::Failed(format!("Failed to run grep: {}", e)))?;

    let exit_code = exec_output.exit_code.unwrap_or(-1);
    if exit_code == 2 {
        return Err(yoagent::types::ToolError::Failed(format!(
            "grep error: {}",
            exec_output.stderr.trim()
        )));
    }

    let trimmed = exec_output.stdout.trim();
    if trimmed.is_empty() {
        return Ok("No matches found".to_string());
    }

    let lines: Vec<&str> = trimmed.lines().collect();
    let truncated: Vec<String> = lines
        .iter()
        .take(limit as usize)
        .map(|l| truncate_line(l, GREP_MAX_LINE_LENGTH))
        .collect();

    Ok(truncated.join("\n"))
}

// =====================================================================
// find tool
// =====================================================================

struct FindTool {
    cwd: PathBuf,
    operations: Arc<dyn FindOperations>,
}

#[async_trait]
impl yoagent::types::AgentTool for FindTool {
    fn name(&self) -> &str {
        "find"
    }
    fn label(&self) -> &str {
        "find"
    }
    fn description(&self) -> &str {
        "Search for files by glob pattern. Returns matching file paths relative to the search directory. \
         Respects .gitignore. Output is truncated to 1000 results."
    }
    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "required": ["pattern"],
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Glob pattern to match files, e.g. '*.rs', '**/*.json', or 'src/**/*.spec.rs'"
                },
                "path": {
                    "type": "string",
                    "description": "Directory to search in (default: current directory)"
                },
                "limit": {
                    "type": "number",
                    "description": "Maximum number of results (default: 1000)"
                }
            }
        })
    }

    async fn execute(
        &self,
        params: serde_json::Value,
        ctx: yoagent::types::ToolContext,
    ) -> Result<yoagent::types::ToolResult, yoagent::types::ToolError> {
        let pattern = params["pattern"].as_str().ok_or_else(|| {
            yoagent::types::ToolError::InvalidArgs("Missing 'pattern' argument".into())
        })?;
        let search_path = params["path"].as_str().unwrap_or(".");
        let search_owned = resolve_path(search_path, &self.cwd);
        let abs_search = &search_owned;
        let limit = params["limit"].as_u64().unwrap_or(FIND_DEFAULT_LIMIT);

        if !abs_search.exists() {
            return Err(yoagent::types::ToolError::Failed(format!(
                "Path not found: {}",
                abs_search.display()
            )));
        }

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        let output = if let Some(fd_path) = which("fd") {
            run_fd_with_ops(
                self.operations.as_ref(),
                &self.cwd,
                &fd_path,
                pattern,
                abs_search,
                limit,
            )
            .await?
        } else {
            run_find_with_ops(
                self.operations.as_ref(),
                &self.cwd,
                pattern,
                abs_search,
                limit,
            )
            .await?
        };

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        Ok(yoagent::types::ToolResult {
            content: vec![yoagent::types::Content::Text { text: output }],
            details: serde_json::Value::Null,
        })
    }
}

async fn run_fd_with_ops(
    ops: &dyn FindOperations,
    cwd: &Path,
    fd: &Path,
    pattern: &str,
    search_path: &Path,
    limit: u64,
) -> Result<String, yoagent::types::ToolError> {
    // Build effective pattern for fd
    let effective_pattern = if pattern.contains('/') {
        if !pattern.starts_with('/') && !pattern.starts_with("**/") && pattern != "**" {
            format!("**/{}", pattern)
        } else {
            pattern.to_string()
        }
    } else {
        pattern.to_string()
    };

    // Build fd command string
    let mut cmd_parts: Vec<String> = vec![
        fd.to_string_lossy().to_string(),
        "--glob".into(),
        "--color=never".into(),
        "--hidden".into(),
        "--no-require-git".into(),
        "--max-results".into(),
        limit.to_string(),
    ];
    if pattern.contains('/') {
        cmd_parts.push("--full-path".into());
    }
    cmd_parts.push("--".into());
    cmd_parts.push(effective_pattern);
    cmd_parts.push(search_path.to_string_lossy().to_string());

    let command = cmd_parts.join(" ");
    let exec_output = ops
        .exec(&command, cwd)
        .await
        .map_err(|e| yoagent::types::ToolError::Failed(format!("Failed to run fd: {}", e)))?;

    let exit_code = exec_output.exit_code.unwrap_or(-1);
    if exit_code != 0 && exit_code != 1 && exec_output.stdout.trim().is_empty() {
        return Err(yoagent::types::ToolError::Failed(format!(
            "fd error: {}",
            exec_output.stderr.trim()
        )));
    }

    let results: Vec<String> = exec_output
        .stdout
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    if results.is_empty() {
        return Ok("No files found matching pattern".to_string());
    }

    let relativized: Vec<String> = results
        .into_iter()
        .map(|line| relativize_path(&line, search_path))
        .collect();

    let mut output = relativized.join("\n");
    if relativized.len() >= limit as usize {
        output.push_str(&format!(
            "\n\n[{} results limit reached. Use limit={} for more, or refine pattern]",
            limit,
            limit * 2,
        ));
    }

    Ok(output)
}

async fn run_find_with_ops(
    ops: &dyn FindOperations,
    cwd: &Path,
    pattern: &str,
    search_path: &Path,
    limit: u64,
) -> Result<String, yoagent::types::ToolError> {
    let name_pattern = pattern.trim_start_matches("**/").trim_start_matches("*/");

    let cmd_parts: Vec<String> = vec![
        "find".into(),
        search_path.to_string_lossy().to_string(),
        "-name".into(),
        name_pattern.to_string(),
        "-not".into(),
        "-path".into(),
        "*/node_modules/*".into(),
        "-not".into(),
        "-path".into(),
        "*/.git/*".into(),
    ];

    let command = cmd_parts.join(" ");
    let exec_output = ops
        .exec(&command, cwd)
        .await
        .map_err(|e| yoagent::types::ToolError::Failed(format!("Failed to run find: {}", e)))?;

    let exit_code = exec_output.exit_code.unwrap_or(-1);
    if exit_code != 0 && exit_code != 1 {
        return Err(yoagent::types::ToolError::Failed(format!(
            "find error: {}",
            exec_output.stderr.trim()
        )));
    }

    let lines: Vec<String> = exec_output
        .stdout
        .lines()
        .map(|l| l.trim().to_string())
        .filter(|l| !l.is_empty())
        .collect();

    if lines.is_empty() {
        return Ok("No files found matching pattern".to_string());
    }

    let relativized: Vec<String> = lines
        .into_iter()
        .take(limit as usize)
        .map(|line| relativize_path(&line, search_path))
        .collect();

    let mut output = relativized.join("\n");
    if relativized.len() >= limit as usize {
        output.push_str(&format!(
            "\n\n[{} results limit reached. Use limit={} for more, or refine pattern]",
            limit,
            limit * 2,
        ));
    }

    Ok(output)
}

// =====================================================================
// ls tool
// =====================================================================

struct LsTool {
    cwd: PathBuf,
    operations: Arc<dyn LsOperations>,
}

#[async_trait]
impl yoagent::types::AgentTool for LsTool {
    fn name(&self) -> &str {
        "ls"
    }
    fn label(&self) -> &str {
        "ls"
    }
    fn description(&self) -> &str {
        "List directory contents. Returns entries sorted alphabetically, with '/' suffix for directories. \
         Includes dotfiles. Output is truncated to 500 entries."
    }
    fn parameters_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Directory to list (default: current directory)"
                },
                "limit": {
                    "type": "number",
                    "description": "Maximum number of entries to return (default: 500)"
                }
            }
        })
    }

    async fn execute(
        &self,
        params: serde_json::Value,
        ctx: yoagent::types::ToolContext,
    ) -> Result<yoagent::types::ToolResult, yoagent::types::ToolError> {
        let search_path = params["path"].as_str().unwrap_or(".");
        let limit = params["limit"].as_u64().unwrap_or(LS_DEFAULT_LIMIT);

        let abs_path = resolve_path(search_path, &self.cwd);

        if !self.operations.path_exists(&abs_path).unwrap_or(false) {
            return Err(yoagent::types::ToolError::Failed(format!(
                "Path not found: {}",
                abs_path.display()
            )));
        }
        if !self.operations.is_dir(&abs_path).unwrap_or(false) {
            return Err(yoagent::types::ToolError::Failed(format!(
                "Not a directory: {}",
                abs_path.display()
            )));
        }

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        let entries: Vec<String> = match self.operations.read_dir(&abs_path) {
            Ok(items) => {
                let mut items: Vec<(String, bool)> = items
                    .into_iter()
                    .map(|entry| (entry.name, entry.is_dir))
                    .collect();
                items.sort_by_key(|(a, _)| a.to_lowercase());
                items
                    .into_iter()
                    .take(limit as usize)
                    .map(
                        |(name, is_dir)| {
                            if is_dir { format!("{}/", name) } else { name }
                        },
                    )
                    .collect()
            }
            Err(e) => {
                return Err(yoagent::types::ToolError::Failed(format!(
                    "Cannot read directory: {}",
                    e
                )));
            }
        };

        if ctx.cancel.is_cancelled() {
            return Err(yoagent::types::ToolError::Cancelled);
        }

        if entries.is_empty() {
            return Ok(yoagent::types::ToolResult {
                content: vec![yoagent::types::Content::Text {
                    text: "(empty directory)".to_string(),
                }],
                details: serde_json::Value::Null,
            });
        }

        let mut output = entries.join("\n");
        if entries.len() >= limit as usize {
            output.push_str(&format!(
                "\n\n[{} entries limit reached. Use limit={} for more]",
                limit,
                limit * 2,
            ));
        }

        Ok(yoagent::types::ToolResult {
            content: vec![yoagent::types::Content::Text { text: output }],
            details: serde_json::Value::Null,
        })
    }
}

// =====================================================================
// Helpers
// =====================================================================

fn which(name: &str) -> Option<PathBuf> {
    std::process::Command::new("which")
        .arg(name)
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|_| PathBuf::from(name))
}

fn resolve_path(path: &str, cwd: &Path) -> PathBuf {
    if Path::new(path).is_absolute() {
        Path::new(path).to_path_buf()
    } else {
        cwd.join(path)
    }
}

fn relativize_path(path: &str, search_root: &Path) -> String {
    let p = Path::new(path);
    if let Ok(rel) = p.strip_prefix(search_root) {
        rel.to_string_lossy().replace('\\', "/")
    } else {
        p.file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_else(|| path.to_string())
    }
}

fn truncate_line(line: &str, max_chars: usize) -> String {
    if line.len() <= max_chars {
        line.to_string()
    } else {
        format!("{}... [truncated]", &line[..max_chars])
    }
}

fn shorten_path_str(path: &str) -> String {
    if let Ok(home) = std::env::var("HOME") {
        path.replacen(&home, "~", 1)
    } else if path == "." || path.is_empty() {
        ".".to_string()
    } else {
        path.to_string()
    }
}

// =====================================================================
// Shared list renderer (used by grep, find, ls)
// =====================================================================

struct ListRenderer {
    tool_name: &'static str,
    /// Format: "{pattern}" or "/{pattern}/" or empty for no pattern.
    pattern_format: &'static str,
    no_results_text: &'static str,
    collapsed_lines: usize,
    show_glob: bool,
}

impl ListRenderer {
    fn grep() -> Self {
        Self {
            tool_name: "grep",
            pattern_format: "/{}/ ",
            no_results_text: "No matches found",
            collapsed_lines: 15,
            show_glob: true,
        }
    }

    fn find() -> Self {
        Self {
            tool_name: "find",
            pattern_format: "{} in ",
            no_results_text: "No files found matching pattern",
            collapsed_lines: 20,
            show_glob: false,
        }
    }

    fn ls() -> Self {
        Self {
            tool_name: "ls",
            pattern_format: "",
            no_results_text: "(empty directory)",
            collapsed_lines: 20,
            show_glob: false,
        }
    }
}

impl ToolRenderer for ListRenderer {
    fn render_call(
        &self,
        args: &serde_json::Value,
        _width: usize,
        theme: &dyn Theme,
        _ctx: &ToolRenderContext,
    ) -> Vec<String> {
        let search_path = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");
        let limit = args.get("limit").and_then(|v| v.as_u64());
        let path_display = shorten_path_str(search_path);

        let mut text = format!(
            "{} {}{}",
            theme.fg_key(ThemeKey::ToolTitle, &theme.bold(self.tool_name)),
            if self.pattern_format.is_empty() {
                String::new()
            } else {
                let p = args.get("pattern").and_then(|v| v.as_str()).unwrap_or("");
                theme.fg_key(ThemeKey::Accent, &self.pattern_format.replace("{}", p))
            },
            theme.fg_key(ThemeKey::ToolOutput, &path_display),
        );

        if self.show_glob
            && let Some(g) = args.get("glob").and_then(|v| v.as_str())
        {
            text.push_str(&theme.fg_key(ThemeKey::ToolOutput, &format!(" ({})", g)));
        }
        if let Some(l) = limit {
            text.push_str(&theme.fg_key(ThemeKey::ToolOutput, &format!(" limit {}", l)));
        }

        vec![text]
    }

    fn render_result(
        &self,
        content: &str,
        _width: usize,
        theme: &dyn Theme,
        ctx: &ToolRenderContext,
    ) -> Vec<String> {
        if content.is_empty() {
            return vec![];
        }
        if !ctx.expanded && !ctx.is_error {
            return vec![];
        }

        let output = content.trim();
        if output.is_empty() || output == self.no_results_text {
            return vec![theme.fg_key(ThemeKey::ToolOutput, output)];
        }

        let lines: Vec<&str> = output.lines().collect();
        let max_lines = if ctx.expanded {
            usize::MAX
        } else {
            self.collapsed_lines
        };
        let display: Vec<&str> = lines.iter().copied().take(max_lines).collect();
        let remaining = lines.len().saturating_sub(display.len());

        let mut result = vec![String::new()];
        for line in &display {
            result.push(theme.fg_key(ThemeKey::ToolOutput, line));
        }
        if remaining > 0 {
            let hint = if !ctx.expand_key.is_empty() {
                format!(
                    "... ({} more lines, {} to expand)",
                    remaining, ctx.expand_key
                )
            } else {
                format!("... ({} more lines)", remaining)
            };
            result.push(theme.fg_key(ThemeKey::Muted, &hint));
        }
        result
    }
}