nitpicker-agent 0.1.1

Reusable agentic loop (file/git tools + parallel subagents) extracted from nitpicker
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
use eyre::Result;
use glob::glob;
use regex::Regex;
use rig_core::completion::ToolDefinition;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use tokio::fs;
use tracing::debug;

/// Find a valid UTF-8 character boundary at or before the given position.
/// This is a polyfill for `str::floor_char_boundary` which requires Rust 1.91.
pub fn floor_char_boundary(s: &str, pos: usize) -> usize {
    let pos = pos.min(s.len());
    // UTF-8 continuation bytes start with 10xxxxxx (0x80-0xBF)
    // We need to find a byte that is NOT a continuation byte
    let bytes = s.as_bytes();
    for i in (0..=pos).rev() {
        // `i == s.len()` is the end-of-string boundary (matches std's `str::floor_char_boundary`)
        // and must be checked before indexing, since `bytes[s.len()]` is out of bounds.
        if i == s.len() || i == 0 || (bytes[i] & 0xC0) != 0x80 {
            return i;
        }
    }
    0
}

/// Allowlisted read-only git subcommands. Ref listing is served by the `for-each-ref`/`show-ref`
/// plumbing (no ref-creating/deleting mode → safe by construction for any args), deliberately not
/// the `branch`/`tag` porcelain whose read and write modes are indistinguishable by arguments.
const ALLOWED_GIT_SUBCOMMANDS: &[&str] = &[
    "diff",
    "log",
    "show",
    "blame",
    "status",
    "rev-parse",
    "shortlog",
    "ls-files",
    "for-each-ref",
    "show-ref",
];

/// True if `token` is a long option `--<stem>` (optionally `--<stem>=value`) whose stem is a
/// non-empty prefix of `name`. git accepts unambiguous long-option abbreviations, so `--out`
/// matches `output` and `--no-i` matches `no-index`.
fn long_flag_matches(token: &str, name: &str) -> bool {
    long_flag_stem(token).is_some_and(|stem| name.starts_with(stem))
}

fn long_flag_stem(token: &str) -> Option<&str> {
    token
        .strip_prefix("--")
        .map(|long| long.split('=').next().unwrap_or(long))
        .filter(|stem| !stem.is_empty())
}

fn blocked_fs_reading_flag_matches(token: &str, name: &str) -> bool {
    if name == "ignore-revs-file" && long_flag_stem(token) == Some("ignore-rev") {
        return false;
    }
    long_flag_matches(token, name)
}

/// Reject git invocations that aren't genuinely read-only or that escape the repository. The
/// subcommand allowlist alone is not enough — `read_file`/`grep`/`glob` confine reads to `work_dir`
/// via canonicalize, but several git flags read straight from the filesystem and would bypass that
/// sandbox. Three layers: (1) no argument may reference a path outside the repo (absolute or `..`,
/// including Windows-style paths), (2) filesystem-reading flags are denied outright, and (3) the
/// write-to-file `--output`/`-o` on `diff`/`log`/`show`. Returns `Err(message)` when rejected.
fn ensure_readonly_git(subcommand: &str, rest: &[&str]) -> std::result::Result<(), String> {
    // Layer 1 (all subcommands): an argument value may not reference a path outside the repo. The
    // value is the token itself, or the part after `=` for a `--flag=value`; a bare `--flag` whose
    // path is the *next* token is caught when that token is examined on its own. Refs (`HEAD~1`,
    // `origin/main..HEAD`), ranges, and `--format=%H` have no leading `/` and no `..` component.
    for token in rest {
        let value = match token.starts_with('-') {
            true => token.split_once('=').map(|(_, v)| v).unwrap_or(""),
            false => token,
        };
        if looks_like_external_path(value) {
            return Err(format!(
                "Error: git argument '{token}' references a path outside the repository"
            ));
        }
    }
    // Layer 2: flags that read a file by path, bypassing the object database entirely. They have no
    // role in reviewing repo history, so deny them by name (incl. unambiguous abbreviations and the
    // `=`-glued form) — this also closes a relative-but-symlinked path that would slip past layer 1.
    let fs_reading_flags: &[&str] = match subcommand {
        "diff" => &["no-index"],
        "blame" => &["contents", "ignore-revs-file"],
        _ => &[],
    };
    for flag in fs_reading_flags {
        if rest
            .iter()
            .any(|token| blocked_fs_reading_flag_matches(token, flag))
        {
            return Err(format!(
                "Error: git --{flag} reads files outside the repository and is not allowed"
            ));
        }
    }
    // Layer 3: `--output`/`-o` is a write-to-file flag only on `diff`/`log`/`show` (which share
    // diff's output machinery). On other subcommands `-o` means something else and is read-only —
    // notably `ls-files -o` (= `--others`) — so gate only these three to avoid blocking legit reads.
    if matches!(subcommand, "diff" | "log" | "show") {
        for token in rest {
            if long_flag_matches(token, "output")
                || *token == "-o"
                || (token.starts_with("-o") && !token.starts_with("--") && token.len() > 2)
            {
                return Err(
                    "Error: writing git output to a file (--output/-o) is not allowed".into(),
                );
            }
        }
    }
    Ok(())
}

fn looks_like_external_path(value: &str) -> bool {
    if value.is_empty() {
        return false;
    }
    Path::new(value).is_absolute()
        || value.starts_with('\\')
        || has_windows_drive_prefix(value)
        || value.split(['/', '\\']).any(|component| component == "..")
}

fn has_windows_drive_prefix(value: &str) -> bool {
    let bytes = value.as_bytes();
    bytes.len() >= 3
        && bytes[0].is_ascii_alphabetic()
        && bytes[1] == b':'
        && matches!(bytes.get(2).copied(), Some(b'/' | b'\\'))
}

pub trait Tool: Send + Sync {
    fn name(&self) -> String;
    fn definition(&self) -> ToolDefinition;
    fn call(
        &self,
        args: Value,
        work_dir: PathBuf,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<String>> + Send>>;
}

pub fn all_tools() -> HashMap<String, Arc<dyn Tool>> {
    let tools: Vec<Arc<dyn Tool>> = vec![
        Arc::new(ReadFileTool),
        Arc::new(GlobTool),
        Arc::new(GrepTool),
        Arc::new(GitTool),
    ];
    tools.into_iter().map(|tool| (tool.name(), tool)).collect()
}

pub fn reflect_tools() -> HashMap<String, Arc<dyn Tool>> {
    // Reflection has no repo checkout to run git against, so it gets every tool except git.
    let mut tools = all_tools();
    tools.remove("git");
    tools
}

pub fn tool_definitions(tools: &HashMap<String, Arc<dyn Tool>>) -> Vec<ToolDefinition> {
    tools.values().map(|tool| tool.definition()).collect()
}

pub struct ReadFileTool;

impl Tool for ReadFileTool {
    fn name(&self) -> String {
        "read_file".to_string()
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "read_file".to_string(),
            description:
                "Read a text file inside the workspace and return numbered lines. Use this after glob or grep to inspect specific files; prefer start_line/end_line for focused reads."
                    .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "Workspace-relative file path to read."
                    },
                    "start_line": {
                        "type": "integer",
                        "minimum": 1,
                        "description": "First line to include. Omit to start at line 1."
                    },
                    "end_line": {
                        "type": "integer",
                        "minimum": 1,
                        "description": "Last line to include. Omit to read to the end of the file."
                    }
                },
                "required": ["path"],
                "additionalProperties": false
            }),
        }
    }

    fn call(
        &self,
        args: Value,
        work_dir: PathBuf,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<String>> + Send>> {
        Box::pin(async move {
            let path = args
                .get("path")
                .and_then(|value| value.as_str())
                .ok_or_else(|| eyre::eyre!("missing path"))?;
            let start_line = args
                .get("start_line")
                .and_then(|value| value.as_u64())
                .unwrap_or(1) as usize;
            let end_line = args
                .get("end_line")
                .and_then(|value| value.as_u64())
                .map(|value| value as usize);
            let full_path = work_dir.join(path);
            let full_path = full_path.canonicalize().map_err(|e| {
                eyre::eyre!(
                    "cannot resolve path {path:?}: {e}. Only files within {} are accessible.",
                    work_dir.display()
                )
            })?;
            if !full_path.starts_with(&work_dir) {
                eyre::bail!(
                    "access denied: {path:?} is outside the allowed workspace ({}). Only project files are accessible.",
                    work_dir.display()
                );
            }
            let content = match fs::read_to_string(&full_path).await {
                Ok(content) => content,
                Err(err) if err.kind() == std::io::ErrorKind::InvalidData => {
                    eyre::bail!("file is not valid UTF-8; read_file only supports text files")
                }
                Err(err) => return Err(err.into()),
            };
            let lines = content.lines().collect::<Vec<_>>();
            let total = lines.len();
            let start = start_line.max(1).min(total.max(1));
            let end = end_line.unwrap_or(total).max(start).min(total);
            let relative = full_path
                .strip_prefix(&work_dir)
                .unwrap_or(&full_path)
                .display()
                .to_string();
            let mut output = format!("File: {relative}\nLines: {start}-{end} of {total}\n\n");
            for (idx, line) in lines.iter().enumerate() {
                let line_num = idx + 1;
                if line_num < start || line_num > end {
                    continue;
                }
                output.push_str(&format!("{line_num:>4} {line}\n"));
            }
            Ok(output)
        })
    }
}

pub struct GlobTool;

impl Tool for GlobTool {
    fn name(&self) -> String {
        "glob".to_string()
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "glob".to_string(),
            description:
                "Find workspace-relative file paths by glob pattern. Use this when you know the file name or extension pattern but not the exact path; returns at most 200 matches."
                    .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "pattern": {
                        "type": "string",
                        "description": "Workspace-relative glob such as 'src/**/*.rs' or '**/*.toml'."
                    }
                },
                "required": ["pattern"],
                "additionalProperties": false
            }),
        }
    }

    fn call(
        &self,
        args: Value,
        work_dir: PathBuf,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<String>> + Send>> {
        Box::pin(async move {
            let pattern = args
                .get("pattern")
                .and_then(|value| value.as_str())
                .ok_or_else(|| eyre::eyre!("missing pattern"))?;
            let pattern_path = Path::new(pattern);
            if pattern_path.is_absolute()
                || pattern_path
                    .components()
                    .any(|c| c == std::path::Component::ParentDir)
            {
                eyre::bail!(
                    "access denied: glob pattern {pattern:?} must be relative to the workspace ({}). Absolute paths and parent-directory traversal are not allowed.",
                    work_dir.display()
                );
            }
            let mut results = Vec::new();
            let full_pattern = work_dir.join(pattern);
            let full_pattern = full_pattern.to_string_lossy();
            for entry in glob(&full_pattern)? {
                if let Ok(path) = entry {
                    if let Ok(relative) = path.strip_prefix(&work_dir) {
                        results.push(relative.display().to_string());
                    }
                }
                if results.len() >= 200 {
                    break;
                }
            }
            if results.is_empty() {
                return Ok(format!("No files matched pattern: {pattern}"));
            }
            Ok(results.join("\n"))
        })
    }
}

pub struct GrepTool;

impl Tool for GrepTool {
    fn name(&self) -> String {
        "grep".to_string()
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "grep".to_string(),
            description: "Search text files for a regex and return path:line:content matches. Use this to locate relevant code before calling read_file; optionally limit by path or file_glob, and expect at most 100 matches."
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "pattern": {
                        "type": "string",
                        "description": "Regular expression to search for in file contents."
                    },
                    "path": {
                        "type": "string",
                        "description": "Optional workspace-relative file or directory to search within."
                    },
                    "file_glob": {
                        "type": "string",
                        "description": "Optional filename filter such as '*.rs'; matched against file names, not full paths."
                    }
                },
                "required": ["pattern"],
                "additionalProperties": false
            }),
        }
    }

    fn call(
        &self,
        args: Value,
        work_dir: PathBuf,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<String>> + Send>> {
        Box::pin(async move {
            let pattern = args
                .get("pattern")
                .and_then(|value| value.as_str())
                .ok_or_else(|| eyre::eyre!("missing pattern"))?;
            let base_path = args
                .get("path")
                .and_then(|value| value.as_str())
                .map(|value| {
                    let p = work_dir.join(value);
                    // canonicalize to resolve symlinks and `..` before the workspace check
                    p.canonicalize()
                        .map_err(|e| eyre::eyre!("cannot resolve path {value:?}: {e}. Only files within {} are accessible.", work_dir.display()))
                })
                .transpose()?
                .unwrap_or_else(|| work_dir.clone());
            if !base_path.starts_with(&work_dir) {
                let path_arg = args.get("path").and_then(|v| v.as_str()).unwrap_or("?");
                eyre::bail!(
                    "access denied: {path_arg:?} is outside the allowed workspace ({}). Only project files are accessible.",
                    work_dir.display()
                );
            }
            let file_glob = args
                .get("file_glob")
                .and_then(|value| value.as_str())
                .map(glob_to_regex)
                .transpose()?;
            let regex =
                Regex::new(pattern).map_err(|e| eyre::eyre!("invalid regex {pattern:?}: {e}"))?;
            let mut results = Vec::new();
            let mut skipped_files = 0usize;
            if base_path.is_file() {
                if let Err(e) = search_file(&base_path, &regex, &work_dir, &mut results).await {
                    debug!("skipping file {}: {e}", base_path.display());
                    skipped_files += 1;
                }
            } else {
                let mut stack = vec![base_path];
                while let Some(path) = stack.pop() {
                    let entries = match fs::read_dir(&path).await {
                        Ok(entries) => entries,
                        Err(e) => {
                            debug!("skipping unreadable dir {}: {e}", path.display());
                            continue;
                        }
                    };
                    let mut entries = entries;
                    while let Ok(Some(entry)) = entries.next_entry().await {
                        let entry_path = entry.path();
                        let file_type = match entry.file_type().await {
                            Ok(file_type) => file_type,
                            Err(e) => {
                                debug!("skipping {}: {e}", entry_path.display());
                                continue;
                            }
                        };
                        let name = entry.file_name();
                        let name = name.to_string_lossy();
                        if file_type.is_dir() {
                            if name.starts_with('.') || name == "target" || name == "node_modules" {
                                continue;
                            }
                            stack.push(entry_path);
                        } else if file_type.is_file() {
                            if let Some(filter) = &file_glob {
                                if !filter.is_match(&name) {
                                    continue;
                                }
                            }
                            match search_file(&entry_path, &regex, &work_dir, &mut results).await {
                                Ok(_) => {}
                                Err(e) => {
                                    debug!("skipping file {}: {e}", entry_path.display());
                                    skipped_files += 1;
                                }
                            }
                            if results.len() >= 100 {
                                break;
                            }
                        }
                        if results.len() >= 100 {
                            break;
                        }
                    }
                    if results.len() >= 100 {
                        break;
                    }
                }
            }
            if results.is_empty() {
                let mut output = format!("No matches for regex: {pattern}");
                if skipped_files > 0 {
                    output.push_str(&format!("\nSkipped unreadable files: {skipped_files}"));
                }
                return Ok(output);
            }
            let mut output = format!("Matches: {}\n", results.len());
            if skipped_files > 0 {
                output.push_str(&format!("Skipped unreadable files: {skipped_files}\n"));
            }
            output.push('\n');
            output.push_str(&results.join("\n"));
            Ok(output)
        })
    }
}

async fn search_file(
    path: &PathBuf,
    regex: &Regex,
    work_dir: &Path,
    results: &mut Vec<String>,
) -> Result<()> {
    use tokio::io::AsyncReadExt;

    // Open file and check first 8KB for binary content before reading full file
    let mut file = fs::File::open(path).await?;
    let mut buffer = [0u8; 8192];
    let bytes_read = file.read(&mut buffer).await?;

    // Check for null bytes in the sample (binary file indicator)
    if buffer[..bytes_read].contains(&0) {
        return Ok(()); // Skip binary files silently
    }

    // Read the rest of the file
    let mut remaining = Vec::new();
    file.read_to_end(&mut remaining).await?;

    // Combine sample + remaining into full content
    let mut full_content = Vec::with_capacity(bytes_read + remaining.len());
    full_content.extend_from_slice(&buffer[..bytes_read]);
    full_content.extend_from_slice(&remaining);

    // Convert to string and search
    let content = String::from_utf8_lossy(&full_content);
    let relative = path.strip_prefix(work_dir).unwrap_or(path);
    for (idx, line) in content.lines().enumerate() {
        if regex.is_match(line) {
            results.push(format!("{}:{}:{}", relative.display(), idx + 1, line));
            if results.len() >= 100 {
                break;
            }
        }
    }
    Ok(())
}

fn glob_to_regex(pattern: &str) -> Result<Regex> {
    let mut escaped = String::new();
    for ch in pattern.chars() {
        match ch {
            '.' => escaped.push_str("\\."),
            '*' => escaped.push_str(".*"),
            '?' => escaped.push('.'),
            other => escaped.push(other),
        }
    }
    Regex::new(&format!("^{}$", escaped))
        .map_err(|e| eyre::eyre!("invalid file_glob {pattern:?}: {e}"))
}

/// Check if a file is binary by reading the first 8 KiB and checking for null bytes.
/// Returns `true` if binary, `false` if text, or an error if the file cannot be read.
pub async fn is_binary_file(path: &Path) -> std::io::Result<bool> {
    use tokio::io::AsyncReadExt;
    let mut file = fs::File::open(path).await?;
    let mut buffer = [0u8; 8192];
    let bytes_read = file.read(&mut buffer).await?;
    Ok(buffer[..bytes_read].contains(&0)) // null byte = binary
}

pub struct GitTool;

impl Tool for GitTool {
    fn name(&self) -> String {
        "git".to_string()
    }

    fn definition(&self) -> ToolDefinition {
        ToolDefinition {
            name: "git".to_string(),
            description: "Run an allowlisted read-only git command for review context: diff, log, show, blame, status, rev-parse, shortlog, ls-files, for-each-ref, show-ref. To list or query branches/tags use for-each-ref (e.g. `for-each-ref --contains <sha> refs/heads/`), not branch/tag. Use this for repository history or patch context, not for general file search."
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "command": {
                        "type": "string",
                        "description": "Read-only git command to run, for example 'diff --stat HEAD~1' or 'log --oneline -n 20'."
                    }
                },
                "required": ["command"],
                "additionalProperties": false
            }),
        }
    }

    fn call(
        &self,
        args: Value,
        work_dir: PathBuf,
    ) -> Pin<Box<dyn std::future::Future<Output = Result<String>> + Send>> {
        Box::pin(async move {
            let command = args
                .get("command")
                .and_then(|value| value.as_str())
                .ok_or_else(|| eyre::eyre!("missing command"))?;
            let tokens = command.split_whitespace().collect::<Vec<_>>();
            let Some((subcommand, rest)) = tokens.split_first() else {
                return Ok("Error: empty git command".to_string());
            };
            if !ALLOWED_GIT_SUBCOMMANDS.contains(subcommand) {
                return Ok(format!("Error: git subcommand '{subcommand}' not allowed"));
            }
            if let Err(msg) = ensure_readonly_git(subcommand, rest) {
                return Ok(msg);
            }
            // GIT_OPTIONAL_LOCKS=0 keeps even nominally-read commands side-effect-free: it stops
            // e.g. `git status` from refreshing/rewriting `.git/index` stat caches and avoids
            // index.lock contention when running against the user's repo in `pr` in-place mode.
            let output = tokio::process::Command::new("git")
                .args(tokens)
                .env("GIT_OPTIONAL_LOCKS", "0")
                .current_dir(&work_dir)
                .output()
                .await?;
            let mut stdout = String::from_utf8_lossy(&output.stdout).to_string();
            if stdout.len() > 50_000 {
                let original_len = stdout.len();
                let boundary = floor_char_boundary(&stdout, 50_000);
                stdout.truncate(boundary);
                stdout.push_str(&format!(
                    "\n... truncated after 50,000 chars; {} chars omitted",
                    original_len.saturating_sub(boundary)
                ));
            }
            if !output.status.success() {
                let stderr = String::from_utf8_lossy(&output.stderr);
                return Ok(format!("Error: {stderr}"));
            }
            Ok(stdout)
        })
    }
}

#[cfg(test)]
mod tests {
    use super::{ALLOWED_GIT_SUBCOMMANDS, ReadFileTool, Tool, ensure_readonly_git, floor_char_boundary};
    use serde_json::json;

    #[test]
    fn floor_char_boundary_handles_end_and_past_end() {
        // pos beyond the end must not panic and clamps to len (the end is a valid boundary).
        assert_eq!(floor_char_boundary("héllo", 100), "héllo".len());
        assert_eq!(floor_char_boundary("abc", 3), 3);
        assert_eq!(floor_char_boundary("", 5), 0);
        // 'é' occupies bytes 1..=2; pos 2 is mid-character and floors back to its start (1).
        assert_eq!(floor_char_boundary("héllo", 2), 1);
        assert_eq!(floor_char_boundary("héllo", 1), 1);
    }

    /// Mirror the two gates GitTool applies: subcommand allowlist, then read-only argument check.
    fn check(cmd: &str) -> Result<(), String> {
        let tokens: Vec<&str> = cmd.split_whitespace().collect();
        let (sub, rest) = tokens.split_first().expect("non-empty command");
        if !ALLOWED_GIT_SUBCOMMANDS.contains(sub) {
            return Err(format!("subcommand '{sub}' not allowed"));
        }
        ensure_readonly_git(sub, rest)
    }

    #[test]
    fn rejects_output_file_write() {
        // --output / -o turn a read-only command into a write-anywhere primitive
        assert!(check("diff HEAD~1 --output=/tmp/evil").is_err());
        assert!(check("diff --output ../escape.txt HEAD~1").is_err());
        assert!(check("log --oneline -o /tmp/x").is_err());
        assert!(check("show -o/tmp/glued HEAD").is_err());
        assert!(check("diff --out=/tmp/abbrev HEAD~1").is_err()); // long-option abbreviation
    }

    #[test]
    fn allows_plain_read_commands() {
        assert!(check("diff HEAD~1").is_ok());
        assert!(check("log --oneline -n 5").is_ok());
        assert!(check("show HEAD").is_ok());
        assert!(check("status --porcelain").is_ok());
        // --oneline must not be mistaken for the -o output flag
        assert!(check("log --oneline").is_ok());
        // -o is a write flag only on diff/log/show; on ls-files it means --others (read-only)
        assert!(check("ls-files -o --exclude-standard").is_ok());
        assert!(check("ls-files --others").is_ok());
        // refs, ranges, and in-repo pathspecs must survive the path-escape guard
        assert!(check("diff --stat HEAD~1 -- src/foo.rs").is_ok());
        assert!(check("log origin/main..HEAD").is_ok());
        assert!(check("blame -- src/foo.rs").is_ok());
        assert!(check("blame --ignore-rev HEAD -- src/foo.rs").is_ok());
        assert!(check("blame --ignore-rev=HEAD -- src/foo.rs").is_ok());
        assert!(check("show HEAD:src/foo.rs").is_ok());
        assert!(check("show v:src/foo.rs").is_ok());
        assert!(check("diff HEAD -- ./src").is_ok());
        assert!(check(r"diff HEAD -- src\foo.rs").is_ok());
    }

    #[test]
    fn rejects_arbitrary_file_read() {
        // diff --no-index / blame --contents read straight from the filesystem, bypassing the
        // canonicalize sandbox that read_file/grep/glob enforce.
        assert!(check("diff --no-index /etc/passwd /dev/null").is_err());
        assert!(check("diff --no-i /etc/passwd /dev/null").is_err()); // abbreviation
        assert!(check("diff --no-index a b").is_err()); // flag denied even with relative paths
        assert!(check("blame --contents /etc/passwd -- a.txt").is_err());
        assert!(check("blame --contents=/etc/passwd -- a.txt").is_err());
        assert!(check("blame --contents secret.txt -- a.txt").is_err());
        // absolute paths and `..` traversal are rejected on any subcommand
        assert!(check("diff /etc/passwd").is_err());
        assert!(check("log -- ../../../etc/passwd").is_err());
        assert!(check("show ../outside").is_err());
        assert!(check("diff HEAD -- a/../../../etc/passwd").is_err());
    }

    #[test]
    fn rejects_windows_style_path_escapes() {
        assert!(check(r"diff C:\Users\victim\secret").is_err());
        assert!(check(r"diff C:/Users/victim\secret").is_err());
        assert!(check(r"diff \\server\share\secret").is_err());
        assert!(check(r"log -- ..\secret").is_err());
        assert!(check(r"show a\..\secret").is_err());
    }

    #[test]
    fn rejects_blame_revision_file_reads() {
        assert!(check("blame --ignore-revs-file README.md -- src/tools.rs").is_err());
        assert!(check("blame --ignore-revs-file=README.md -- src/tools.rs").is_err());
        assert!(check("blame --ignore-revs README.md -- src/tools.rs").is_err());
        assert!(check("blame --ignore-revs-f=README.md -- src/tools.rs").is_err());
    }

    #[tokio::test]
    async fn read_file_invalid_utf8_reports_text_only_feedback() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("bin.dat"), [0xff, 0xfe]).unwrap();
        let work_dir = dir.path().canonicalize().unwrap();

        let err = ReadFileTool
            .call(json!({ "path": "bin.dat" }), work_dir)
            .await
            .unwrap_err()
            .to_string();

        assert_eq!(
            err,
            "file is not valid UTF-8; read_file only supports text files"
        );
    }

    #[test]
    fn rejects_branch_and_tag_porcelain() {
        // branch/tag conflate read and write and are not on the allowlist at all; ref listing must
        // go through the for-each-ref/show-ref plumbing instead.
        assert!(check("branch -D some-branch").is_err());
        assert!(check("branch new-branch").is_err());
        assert!(check("branch --set-upstream-to=origin/main").is_err());
        assert!(check("branch -- newbranch").is_err());
        assert!(check("tag v9.9.9").is_err());
        assert!(check("tag -d v1").is_err());
        // even pure listing forms are rejected — the model is steered to for-each-ref
        assert!(check("branch --list").is_err());
    }

    #[test]
    fn reflect_tools_is_all_tools_minus_git() {
        use std::collections::HashSet;
        let all: HashSet<String> = super::all_tools().into_keys().collect();
        let reflect: HashSet<String> = super::reflect_tools().into_keys().collect();
        assert!(all.contains("git"));
        // Exact set equality pins the derivation: any tool added to all_tools() that isn't git must
        // also appear in reflect_tools(), or this fails.
        let expected: HashSet<String> = all.iter().filter(|k| *k != "git").cloned().collect();
        assert_eq!(reflect, expected);
    }

    #[test]
    fn allows_readonly_ref_plumbing() {
        // for-each-ref / show-ref have no ref-creating mode, so any arg shape is safe.
        assert!(check("for-each-ref refs/heads/").is_ok());
        assert!(check("for-each-ref --contains HEAD refs/heads/").is_ok());
        assert!(check("for-each-ref --format=%(refname) --sort=-creatordate").is_ok());
        assert!(check("show-ref --tags").is_ok());
        assert!(check("show-ref --heads").is_ok());
        // for-each-ref/show-ref have no --output flag, so git itself rejects a write attempt; our
        // layer doesn't need to gate them (the --output block is scoped to diff/log/show).
    }
}