exeora-cli 0.8.4

Native Exeora CLI and local tool executor
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
use super::path::{relative_string, resolve_in_project};
use crate::{
    error::{ErrorCode, ExeoraError},
    protocol::{MAX_GREP_MATCHES, MAX_LIST_ENTRIES, MAX_READ_BYTES},
};
use aho_corasick::{AhoCorasick, AhoCorasickKind, Input, MatchKind};
use cap_std::{
    ambient_authority,
    fs::{Dir, OpenOptions},
};
use globset::{Glob, GlobMatcher};
use grep_matcher::{Match, Matcher, NoCaptures, NoError};
use grep_searcher::{BinaryDetection, Searcher, SearcherBuilder, sinks};
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use memchr::{memchr, memmem};
use regex_syntax::{
    ParserBuilder,
    hir::literal::{ExtractKind, Extractor, Literal},
};
use regress::Regex;
use serde::Deserialize;
use serde_json::{Value, json};
use similar::TextDiff;
use std::{
    collections::VecDeque,
    fs,
    io::{Cursor, Read, Write},
    path::{Path, PathBuf},
    sync::Arc,
};
use unicode_normalization::UnicodeNormalization;

const ALWAYS_SKIP: [&str; 5] = [".git", "node_modules", ".wrangler", "dist", ".astro"];

#[derive(Deserialize)]
struct ReadArgs {
    path: String,
    offset: Option<usize>,
    limit: Option<usize>,
}

pub async fn read_file(root: &Path, args: Value) -> Result<Value, ExeoraError> {
    let root = root.to_owned();
    tokio::task::spawn_blocking(move || {
        let args: ReadArgs = parse(args)?;
        let (real_root, relative) = resolve_in_project(&root, &args.path)?;
        let dir = open_root(&real_root)?;
        let mut file = dir.open(&relative).map_err(|error| {
            ExeoraError::tool(format!(
                "Could not read {}: {:?}.",
                relative_string(&relative),
                error.kind()
            ))
        })?;
        let size = file
            .metadata()
            .map(|metadata| metadata.len())
            .unwrap_or_default();
        let mut prefix = [0u8; 8192];
        let prefix_len = file
            .read(&mut prefix)
            .map_err(|error| ExeoraError::tool(error.to_string()))?;
        if memchr(0, &prefix[..prefix_len]).is_some() {
            return Err(ExeoraError::tool(format!(
                "{} is a binary file ({size} bytes). read_file only returns text.",
                relative_string(&relative)
            )));
        }
        let offset = args.offset.unwrap_or(1);
        if offset == 0 {
            return Err(ExeoraError::new(
                ErrorCode::InvalidArguments,
                "offset must be at least 1.",
            ));
        }
        let start = offset - 1;
        let window = scan_text_window(
            Cursor::new(&prefix[..prefix_len]).chain(file),
            start,
            args.limit,
            MAX_READ_BYTES,
        )
        .map_err(|error| ExeoraError::tool(error.to_string()))?;
        if start > 0 && start >= window.total_lines {
            return Err(ExeoraError::tool(format!(
                "Offset {offset} is past the end of {}, which has {} lines.",
                relative_string(&relative),
                window.total_lines
            )));
        }
        let requested_end = args
            .limit
            .map_or(usize::MAX, |limit| start.saturating_add(limit));
        Ok(json!({
            "path": relative_string(&relative),
            "content": window.content,
            "truncated": window.byte_limit_reached || requested_end < window.total_lines,
            "totalLines": window.total_lines,
        }))
    })
    .await
    .map_err(join_error)?
}

#[derive(Deserialize)]
struct ListArgs {
    path: Option<String>,
    recursive: Option<bool>,
    glob: Option<String>,
}

pub async fn list_files(root: &Path, args: Value) -> Result<Value, ExeoraError> {
    let root = root.to_owned();
    tokio::task::spawn_blocking(move || {
        let args: ListArgs = parse(args)?;
        let (real_root, start) = resolve_in_project(&root, args.path.as_deref().unwrap_or("."))?;
        let glob = compile_glob(args.glob.as_deref())?;
        let mut seen = 0usize;
        let mut output = Vec::new();
        for entry in walk(&real_root, &start, args.recursive.unwrap_or(false), MAX_LIST_ENTRIES + 1)? {
            if glob.as_ref().is_some_and(|glob| !glob.is_match(&entry.relative)) { continue; }
            seen += 1;
            if output.len() >= MAX_LIST_ENTRIES { continue; }
            let mut value = json!({
                "path": relative_string(&entry.relative),
                "type": if entry.symlink { "symlink" } else if entry.directory { "directory" } else { "file" },
            });
            if !entry.directory
                && let Ok(metadata) = fs::metadata(&entry.absolute) { value["size"] = json!(metadata.len()); }
            output.push(value);
        }
        Ok(json!({ "path": if start.as_os_str().is_empty() { ".".to_owned() } else { relative_string(&start) }, "entries": output, "truncated": seen > MAX_LIST_ENTRIES }))
    }).await.map_err(join_error)?
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct GrepArgs {
    pattern: String,
    path: Option<String>,
    glob: Option<String>,
    case_insensitive: Option<bool>,
    max_results: Option<usize>,
}

pub async fn grep(root: &Path, args: Value) -> Result<Value, ExeoraError> {
    let root = root.to_owned();
    tokio::task::spawn_blocking(move || {
        let args: GrepArgs = parse(args)?;
        let (real_root, start) = resolve_in_project(&root, args.path.as_deref().unwrap_or("."))?;
        let matcher = RegressMatcher::new(&args.pattern, args.case_insensitive.unwrap_or(false))?;
        let glob = compile_glob(args.glob.as_deref())?;
        let limit = args.max_results.unwrap_or(MAX_GREP_MATCHES);
        let mut rows = Vec::new();
        let mut truncated = false;
        // One searcher for the whole walk: it owns the line buffer, and a repo
        // is thousands of files to build and throw one away for.
        let mut searcher = SearcherBuilder::new()
            .line_number(true)
            .bom_sniffing(false)
            .binary_detection(BinaryDetection::quit(b'\0'))
            .build();
        for entry in walk(&real_root, &start, true, 50_000)? {
            if entry.directory
                || entry.symlink
                || glob
                    .as_ref()
                    .is_some_and(|glob| !glob.is_match(&entry.relative))
            {
                continue;
            }

            let Ok((file_rows, exceeded)) =
                search_path(&mut searcher, &matcher, &entry, limit - rows.len())
            else {
                continue;
            };
            rows.extend(file_rows);
            if exceeded {
                truncated = true;
                break;
            }
        }
        Ok(json!({ "matches": rows, "truncated": truncated }))
    })
    .await
    .map_err(join_error)?
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EditArgs {
    path: String,
    old_string: String,
    new_string: String,
}

pub async fn edit_file(root: &Path, args: Value) -> Result<Value, ExeoraError> {
    let root = root.to_owned();
    tokio::task::spawn_blocking(move || {
        let args: EditArgs = parse(args)?;
        let (real_root, relative) = resolve_in_project(&root, &args.path)?;
        let dir = open_root(&real_root)?;
        let mut raw = String::new();
        dir.open(&relative)
            .and_then(|mut file| file.read_to_string(&mut raw))
            .map_err(|error| {
                ExeoraError::tool(format!(
                    "Could not edit {}: {:?}.",
                    relative_string(&relative),
                    error.kind()
                ))
            })?;
        let (bom, content) = raw
            .strip_prefix('\u{feff}')
            .map_or(("", raw.as_str()), |text| ("\u{feff}", text));
        let crlf = content
            .find("\r\n")
            .is_some_and(|crlf| content.find('\n') == Some(crlf + 1));
        let normalized = content.replace("\r\n", "\n").replace('\r', "\n");
        let old = args.old_string.replace("\r\n", "\n").replace('\r', "\n");
        let (base, index, length) = unique_match(&normalized, &old, &relative)?;
        let mut changed = base.clone();
        changed.replace_range(index..index + length, &args.new_string);
        let restored = if crlf {
            changed.replace('\n', "\r\n")
        } else {
            changed.clone()
        };
        let mut options = OpenOptions::new();
        options.write(true).truncate(true);
        let mut file = dir
            .open_with(&relative, &options)
            .map_err(|error| ExeoraError::tool(error.to_string()))?;
        file.write_all(format!("{bom}{restored}").as_bytes())
            .map_err(|error| ExeoraError::tool(error.to_string()))?;
        let path = relative_string(&relative);
        let diff = TextDiff::from_lines(&base, &changed)
            .unified_diff()
            .header(&path, &path)
            .to_string();
        Ok(json!({ "path": path, "replacements": 1, "diff": diff }))
    })
    .await
    .map_err(join_error)?
}

#[derive(Deserialize)]
struct WriteArgs {
    path: String,
    content: String,
}

pub async fn write_file(root: &Path, args: Value) -> Result<Value, ExeoraError> {
    let root = root.to_owned();
    tokio::task::spawn_blocking(move || {
        let args: WriteArgs = parse(args)?;
        let (real_root, relative) = resolve_in_project(&root, &args.path)?;
        let dir = open_root(&real_root)?;
        let existed = dir.metadata(&relative).is_ok();
        if let Some(parent) = relative.parent() { dir.create_dir_all(parent).map_err(|error| ExeoraError::tool(error.to_string()))?; }
        let mut options = OpenOptions::new();
        options.write(true).create(true).truncate(true);
        let mut file = dir.open_with(&relative, &options)
            .map_err(|error| ExeoraError::tool(error.to_string()))?;
        file.write_all(args.content.as_bytes()).map_err(|error| ExeoraError::tool(error.to_string()))?;
        Ok(json!({ "path": relative_string(&relative), "bytesWritten": args.content.len(), "created": !existed }))
    }).await.map_err(join_error)?
}

/**
 * The JavaScript regex the contract promises, with something to skip on.
 *
 * `regress` matches what a browser would, which is the whole reason it is here,
 * but it is a backtracking engine with no literal prefilter: a pattern with no
 * anchor is attempted at every byte of every line. `prefilter` holds the set of
 * literals a match has to start with, when that can be proven, so a buffer with
 * none of them is skipped in one SIMD pass instead of being walked.
 */
#[derive(Clone)]
struct RegressMatcher {
    regex: Arc<Regex>,
    prefilter: Option<Arc<Prefilter>>,
}

/**
 * One required literal, or a set of them.
 *
 * The split is about what it costs to build, not to search. A grep call
 * compiles its prefilter and throws it away, and an Aho-Corasick automaton for
 * a single needle costs more to construct than the scan it saves; `memmem`
 * builds in the length of the needle.
 */
enum Prefilter {
    /// Boxed: a `Finder` carries its shift table, and the enum is behind an
    /// `Arc` cloned into every search anyway.
    Single(Box<memmem::Finder<'static>>),
    Set(AhoCorasick),
}

impl Prefilter {
    fn find(&self, haystack: &[u8], at: usize) -> Option<usize> {
        match self {
            Self::Single(finder) => finder.find(&haystack[at..]).map(|start| at + start),
            Self::Set(set) => set
                .find(Input::new(haystack).span(at..haystack.len()))
                .map(|found| found.start()),
        }
    }
}

impl RegressMatcher {
    fn new(pattern: &str, case_insensitive: bool) -> Result<Self, ExeoraError> {
        let flags = if case_insensitive { "i" } else { "" };
        let regex = Regex::with_flags(pattern, flags).map_err(|error| {
            ExeoraError::new(
                ErrorCode::InvalidArguments,
                format!("Not a valid regular expression: {error}"),
            )
        })?;
        Ok(Self {
            regex: Arc::new(regex),
            prefilter: prefilter(pattern, case_insensitive).map(Arc::new),
        })
    }

    /// Where the next match could begin, or `None` when there cannot be one.
    fn candidate(&self, text: &str, at: usize) -> Option<usize> {
        let Some(prefilter) = &self.prefilter else {
            return Some(at);
        };
        // A literal is valid UTF-8, so its first byte is never a continuation
        // byte, so a hit is always on a character boundary of the haystack.
        prefilter.find(text.as_bytes(), at)
    }
}

impl Matcher for RegressMatcher {
    type Captures = NoCaptures;
    type Error = NoError;
    fn find_at(&self, haystack: &[u8], at: usize) -> Result<Option<Match>, NoError> {
        match std::str::from_utf8(haystack) {
            Ok(text) => {
                let Some(from) = self.candidate(text, at) else {
                    return Ok(None);
                };
                Ok(self
                    .regex
                    .find_from(text, from)
                    .next()
                    .map(|found| Match::new(found.start(), found.end())))
            }
            Err(_) => {
                let text = String::from_utf8_lossy(&haystack[at..]);
                Ok(self
                    .regex
                    .find(&text)
                    .map(|_| Match::new(at, haystack.len())))
            }
        }
    }
    fn new_captures(&self) -> Result<NoCaptures, NoError> {
        Ok(NoCaptures::new())
    }
}

/**
 * Literals every match has to begin with, when `regex-syntax` can prove a set.
 *
 * This never changes what matches, only what is skipped, which rests on the set
 * being an over-approximation of the JavaScript one. Where the two engines read
 * a pattern differently, Rust reads it wider: `\d` is every Unicode digit
 * rather than `[0-9]`, `\w` and `.` likewise. The one exception is `\s`, which
 * in JavaScript also covers U+FEFF; `limit_class` is pinned below the 25
 * characters of Unicode `White_Space`, so a pattern starting with `\s` is
 * declined here rather than prefiltered against a set missing a character.
 *
 * Everything unparseable is declined the same way: JavaScript lookbehind and
 * backreferences do not parse at all, and an unbounded prefix yields no
 * literals. Both leave `regress` searching every buffer, as it did before.
 */
fn prefilter(pattern: &str, case_insensitive: bool) -> Option<Prefilter> {
    if !escapes_agree(pattern) {
        return None;
    }
    let hir = ParserBuilder::new().build().parse(pattern).ok()?;
    let sequence = Extractor::new()
        .kind(ExtractKind::Prefix)
        .limit_class(10)
        .extract(&hir);
    let literals = sequence.literals()?;
    if literals.is_empty() || literals.iter().any(|literal| literal.is_empty()) {
        return None;
    }
    // Without the `u` flag, JavaScript never folds a non-ASCII character onto an
    // ASCII one, so ASCII-insensitive matching is exact for an ASCII literal and
    // the only case to decline is a literal that is not.
    if case_insensitive && !literals.iter().all(|literal| literal.as_bytes().is_ascii()) {
        return None;
    }

    if let ([literal], false) = (literals, case_insensitive) {
        return Some(Prefilter::Single(Box::new(
            memmem::Finder::new(literal.as_bytes()).into_owned(),
        )));
    }
    AhoCorasick::builder()
        .match_kind(MatchKind::LeftmostFirst)
        // Building a DFA costs more than the scan it saves for a filter that
        // lives exactly as long as one call.
        .kind(Some(AhoCorasickKind::ContiguousNFA))
        .ascii_case_insensitive(case_insensitive)
        .build(literals.iter().map(Literal::as_bytes))
        .ok()
        .map(Prefilter::Set)
}

/**
 * Whether every escape in the pattern means the same to both parsers.
 *
 * The over-approximation the prefilter rests on only holds where the Rust
 * parse reads a construct the same way or wider. Escapes are where it does
 * neither: without the `u` flag JavaScript reads `\a`, `\A`, `\z`, `\<` and
 * `\>` as the character itself, while `regex-syntax` reads them as a bell, two
 * anchors and two word boundaries. The prefix extracted from the Rust parse is
 * then a literal the JavaScript pattern never requires, and grep answers
 * nothing at all for `\<div`. The braced `\u{...}`, `\x{...}` and `\b{...}`
 * diverge the same way.
 *
 * Only the escapes that agree are allowed through. `\d`, `\w`, `\s` and their
 * negations stay because Rust reads them as Unicode classes far too large for
 * `limit_class`, which declines them before they can narrow anything.
 */
fn escapes_agree(pattern: &str) -> bool {
    let bytes = pattern.as_bytes();
    let mut index = 0;

    while let Some(offset) = memchr(b'\\', &bytes[index..]) {
        let escape = index + offset + 1;
        let Some(&character) = bytes.get(escape) else {
            return false;
        };
        let braced = bytes.get(escape + 1) == Some(&b'{');
        let agrees = match character {
            b'd' | b'D' | b'w' | b'W' | b's' | b'S' | b'n' | b'r' | b't' | b'f' | b'v' | b'B' => {
                true
            }
            b'b' | b'u' | b'x' => !braced,
            b'<' | b'>' => false,
            // Escaping punctuation is the character itself on both sides; a
            // letter or a digit is a construct one of them has and the other
            // reads as the letter, and a digit is a backreference besides.
            _ => !character.is_ascii_alphanumeric(),
        };
        if !agrees {
            return false;
        }
        index = escape + 1;
    }
    true
}

struct WalkEntry {
    absolute: PathBuf,
    relative: PathBuf,
    directory: bool,
    symlink: bool,
}

fn walk(
    root: &Path,
    start: &Path,
    recursive: bool,
    limit: usize,
) -> Result<Vec<WalkEntry>, ExeoraError> {
    let ignores = load_ignore(root);
    let mut queue = VecDeque::from([root.join(start)]);
    let mut output = Vec::new();
    while let Some(directory) = queue.pop_front() {
        let Ok(entries) = fs::read_dir(&directory) else {
            continue;
        };
        for result in entries {
            if output.len() >= limit {
                return Ok(output);
            }
            let Ok(entry) = result else {
                continue;
            };
            let name = entry.file_name();
            if ALWAYS_SKIP.iter().any(|skip| name == *skip) {
                continue;
            }
            let absolute = entry.path();
            let relative = absolute
                .strip_prefix(root)
                .unwrap_or(&absolute)
                .to_path_buf();
            let Ok(kind) = entry.file_type() else {
                continue;
            };
            let directory_entry = kind.is_dir();
            if ignores
                .matched_path_or_any_parents(&relative, directory_entry)
                .is_ignore()
            {
                continue;
            }
            output.push(WalkEntry {
                absolute: absolute.clone(),
                relative,
                directory: directory_entry,
                symlink: kind.is_symlink(),
            });
            if recursive && directory_entry && !kind.is_symlink() {
                queue.push_back(absolute);
            }
        }
    }
    Ok(output)
}

fn load_ignore(root: &Path) -> Gitignore {
    let mut builder = GitignoreBuilder::new(root);
    let _ = builder.add(root.join(".gitignore"));
    builder.build().unwrap_or_else(|_| Gitignore::empty())
}

fn search_path(
    searcher: &mut Searcher,
    matcher: &RegressMatcher,
    entry: &WalkEntry,
    limit: usize,
) -> Result<(Vec<Value>, bool), ExeoraError> {
    let path = relative_string(&entry.relative);
    let mut rows = Vec::new();
    let mut exceeded = false;
    let sink = sinks::Bytes(|line_number: u64, bytes: &[u8]| {
        if rows.len() >= limit {
            exceeded = true;
            return Ok(false);
        }
        let bytes = bytes.strip_suffix(b"\n").unwrap_or(bytes);
        let text = String::from_utf8_lossy(bytes);
        rows.push(json!({ "path": path, "line": line_number, "text": utf16_prefix(&text, 500) }));
        Ok(true)
    });
    searcher
        .search_path(matcher, &entry.absolute, sink)
        .map_err(|error| ExeoraError::tool(error.to_string()))?;
    Ok((rows, exceeded))
}

fn unique_match(
    content: &str,
    old: &str,
    path: &Path,
) -> Result<(String, usize, usize), ExeoraError> {
    let finder = memmem::Finder::new(old);
    let hits: Vec<usize> = finder.find_iter(content.as_bytes()).take(2).collect();
    if hits.len() == 1 {
        return Ok((content.to_owned(), hits[0], old.len()));
    }
    let fuzzy_content: String = content
        .nfkc()
        .collect::<String>()
        .lines()
        .map(str::trim_end)
        .collect::<Vec<_>>()
        .join("\n");
    let fuzzy_old: String = old
        .nfkc()
        .collect::<String>()
        .lines()
        .map(str::trim_end)
        .collect::<Vec<_>>()
        .join("\n");
    let fuzzy_hits: Vec<usize> = memmem::Finder::new(&fuzzy_old)
        .find_iter(fuzzy_content.as_bytes())
        .take(2)
        .collect();
    match fuzzy_hits.as_slice() {
        [index] => Ok((fuzzy_content, *index, fuzzy_old.len())),
        [] => Err(ExeoraError::tool(format!(
            "Could not find the requested text in {}.",
            relative_string(path)
        ))),
        _ => Err(ExeoraError::tool(format!(
            "The requested text appears more than once in {}. Include surrounding lines to make it unique.",
            relative_string(path)
        ))),
    }
}

struct TextWindow {
    content: String,
    total_lines: usize,
    byte_limit_reached: bool,
}

/// Scans the entire file to preserve an exact line count while retaining only
/// the requested, bounded window in memory.
fn scan_text_window(
    mut reader: impl Read,
    start: usize,
    limit: Option<usize>,
    max_bytes: usize,
) -> std::io::Result<TextWindow> {
    let end = limit.map_or(usize::MAX, |limit| start.saturating_add(limit));
    let mut buffer = [0u8; 8192];
    let mut selected = Vec::with_capacity(max_bytes.min(8192));
    let mut line = 0usize;
    let mut bytes_read = 0usize;
    let mut newline_count = 0usize;
    let mut ended_with_newline = false;
    let mut byte_limit_reached = false;

    loop {
        let count = reader.read(&mut buffer)?;
        if count == 0 {
            break;
        }
        for &byte in &buffer[..count] {
            bytes_read += 1;
            if line >= start && line < end && !byte_limit_reached {
                if selected.len() < max_bytes {
                    selected.push(byte);
                } else {
                    byte_limit_reached = true;
                }
            }
            if byte == b'\n' {
                newline_count += 1;
                line += 1;
                ended_with_newline = true;
            } else {
                ended_with_newline = false;
            }
        }
    }

    if byte_limit_reached {
        selected.truncate(memchr::memrchr(b'\n', &selected).unwrap_or(0));
    } else if selected.last() == Some(&b'\n') {
        selected.pop();
    }

    Ok(TextWindow {
        content: String::from_utf8_lossy(&selected).into_owned(),
        total_lines: if bytes_read == 0 {
            0
        } else {
            newline_count + usize::from(!ended_with_newline)
        },
        byte_limit_reached,
    })
}

fn utf16_prefix(text: &str, max: usize) -> String {
    let mut units = 0;
    text.chars()
        .take_while(|ch| {
            let next = units + ch.len_utf16();
            if next > max {
                false
            } else {
                units = next;
                true
            }
        })
        .collect()
}
fn compile_glob(pattern: Option<&str>) -> Result<Option<GlobMatcher>, ExeoraError> {
    pattern
        .map(|value| {
            Glob::new(value)
                .map(|glob| glob.compile_matcher())
                .map_err(|error| ExeoraError::new(ErrorCode::InvalidArguments, error.to_string()))
        })
        .transpose()
}
fn open_root(root: &Path) -> Result<Dir, ExeoraError> {
    Dir::open_ambient_dir(root, ambient_authority())
        .map_err(|error| ExeoraError::tool(error.to_string()))
}
fn parse<T: for<'de> Deserialize<'de>>(value: Value) -> Result<T, ExeoraError> {
    serde_json::from_value(value)
        .map_err(|error| ExeoraError::new(ErrorCode::InvalidArguments, error.to_string()))
}
fn join_error(error: tokio::task::JoinError) -> ExeoraError {
    ExeoraError::tool(error.to_string())
}

#[cfg(test)]
mod tests {
    use super::scan_text_window;
    use std::io::Cursor;

    #[test]
    fn scans_only_the_requested_lines_but_counts_the_whole_file() {
        let window = scan_text_window(Cursor::new(b"one\ntwo\nthree\n"), 1, Some(1), 100)
            .expect("scan succeeds");
        assert_eq!(window.content, "two");
        assert_eq!(window.total_lines, 3);
        assert!(!window.byte_limit_reached);
    }

    #[test]
    fn a_byte_limit_returns_only_complete_lines() {
        let input = format!("short\n{}\nlast", "x".repeat(50));
        let window = scan_text_window(Cursor::new(input), 0, None, 10).expect("scan succeeds");
        assert_eq!(window.content, "short");
        assert_eq!(window.total_lines, 3);
        assert!(window.byte_limit_reached);
    }
}