vtcode-core 0.181.9

Core library for VT Code - a Rust-based terminal 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
use super::FileOpsTool;
use super::is_image_path;
use super::path_policy::PathSuggestionKind;
mod legacy;
mod logging;
mod segments;
use crate::config::constants::tools;
use crate::telemetry::perf::PerfSpan;
use crate::tools::builder::ToolResponseBuilder;
use crate::tools::cache::{FILE_CACHE, file_read_cache_config};
use crate::tools::continuation::{DEFAULT_NEXT_READ_LIMIT, ReadChunkContinuationArgs};
use crate::tools::handlers::read_file::{ReadFileArgs, ReadFileHandler, ReadFileOutcome};
use crate::tools::traits::FileTool;
use crate::tools::types::{Input, PathArgs};
use anyhow::{Context, Result, anyhow};
use serde::Deserialize;
use serde_json::{Value, json};
use std::path::Path;
use std::time::UNIX_EPOCH;

pub(crate) const PATCH_READ_CACHE_NONCE: &str = "__vtcode_patch_read_nonce";

const SPOOL_CHUNK_DEFAULT_LIMIT_LINES: usize = DEFAULT_NEXT_READ_LIMIT;
const SPOOL_CHUNK_MAX_LIMIT_LINES: usize = 50;
const SPOOL_CHUNK_SENTINEL_MAX_TOKENS: usize = 4096;

#[derive(Clone, Copy, Debug)]
struct SpoolChunkPlan {
    offset: usize,
    limit: usize,
}

fn is_legacy_read_request(args: &Value, is_image: bool) -> bool {
    if is_image {
        return true;
    }

    if let Some(obj) = args.as_object() {
        obj.keys().any(|key| {
            matches!(
                key.as_str(),
                "offset_lines"
                    | "page_size_lines"
                    | "max_bytes"
                    | "max_lines"
                    | "chunk_lines"
                    | "encoding"
                    | "max_tokens"
            )
        })
    } else {
        false
    }
}

fn is_new_read_request(args: &Value) -> bool {
    let new_keys = [
        "mode",
        "indentation",
        "offset",
        "limit",
        "o",
        "l",
        "offset_bytes",
        "page_size_bytes",
        "length",
    ];
    new_keys.iter().any(|key| args.get(*key).is_some())
}

fn looks_like_patch_payload(args: &Value) -> bool {
    use crate::tools::editing::looks_like_vte_patch;

    args.get("patch").and_then(Value::as_str).is_some_and(looks_like_vte_patch)
        || args.get("input").and_then(Value::as_str).is_some_and(looks_like_vte_patch)
        || args.as_str().is_some_and(looks_like_vte_patch)
}

fn build_read_handler_args(args: &Value, canonical_path: &Path) -> Value {
    let mut handler_args_json = args.clone();
    if let Some(obj) = handler_args_json.as_object_mut() {
        for alias in ["path", "filepath", "target_path", "file"] {
            obj.remove(alias);
        }
        obj.insert("file_path".to_string(), json!(canonical_path.to_string_lossy()));

        if let Some(mode_value) = obj.get("mode").cloned() {
            let normalized_mode = match mode_value {
                Value::Null => None,
                Value::String(raw) => {
                    let trimmed = raw.trim();
                    if trimmed.eq_ignore_ascii_case("indentation") {
                        Some("indentation".to_string())
                    } else {
                        Some("slice".to_string())
                    }
                }
                _ => Some("slice".to_string()),
            };

            if let Some(mode) = normalized_mode {
                obj.insert("mode".to_string(), json!(mode));
            } else {
                obj.remove("mode");
            }
        }

        if let Some(indentation_value) = obj.get("indentation").cloned() {
            match indentation_value {
                Value::Bool(true) => {
                    obj.insert("mode".to_string(), json!("indentation"));
                    obj.insert("indentation".to_string(), json!({}));
                }
                Value::Bool(false) | Value::Null => {
                    obj.remove("indentation");
                    if obj
                        .get("mode")
                        .and_then(Value::as_str)
                        .is_some_and(|mode| mode.eq_ignore_ascii_case("indentation"))
                    {
                        obj.insert("mode".to_string(), json!("slice"));
                    }
                }
                Value::String(text) if text.eq_ignore_ascii_case("true") => {
                    obj.insert("mode".to_string(), json!("indentation"));
                    obj.insert("indentation".to_string(), json!({}));
                }
                Value::String(text) if text.eq_ignore_ascii_case("false") => {
                    obj.remove("indentation");
                    if obj
                        .get("mode")
                        .and_then(Value::as_str)
                        .is_some_and(|mode| mode.eq_ignore_ascii_case("indentation"))
                    {
                        obj.insert("mode".to_string(), json!("slice"));
                    }
                }
                Value::Object(_) => {
                    obj.insert("mode".to_string(), json!("indentation"));
                }
                _ => {}
            }
        }

        if let Some(src) = obj.get("offset_lines").or_else(|| obj.get("o")).cloned() {
            obj.entry("offset".to_string()).or_insert(src);
        }
        if let Some(src) = obj.get("page_size_lines").or_else(|| obj.get("l")).cloned() {
            obj.entry("limit".to_string()).or_insert(src);
        }

        // Map `length` alias to `page_size_bytes` for byte-range reads
        if let Some(src) = obj.get("length").cloned() {
            obj.entry("page_size_bytes".to_string()).or_insert(src);
        }

        // Map start_line/end_line to offset/limit when offset is not already set.
        if let Some(start) = obj.get("start_line").and_then(parse_usize_value) {
            obj.entry("offset".to_string()).or_insert(json!(start));
            if let Some(end) = obj.get("end_line").and_then(parse_usize_value)
                && end >= start
                && !obj.contains_key("limit")
            {
                obj.insert("limit".to_string(), json!(end - start + 1));
            }
        }

        if obj.get("offset").and_then(parse_usize_value) == Some(0) {
            obj.insert("offset".to_string(), json!(1));
        }
        if obj.get("limit").and_then(parse_usize_value) == Some(0) {
            obj.remove("limit");
        }
    }

    handler_args_json
}

/// Locate a verbatim line slice using the selected reader's coordinate system.
pub(crate) fn source_line_offset(args: &Value) -> Option<usize> {
    if !is_new_read_request(args) && is_legacy_read_request(args, false) {
        // Legacy line paging is zero-based; modern slice offsets are one-based.
        let input: Input = serde_json::from_value(args.clone()).ok()?;
        return Some(input.offset_lines.unwrap_or(0));
    }
    let normalized = build_read_handler_args(args, Path::new("source"));
    let read: ReadFileArgs = serde_json::from_value(normalized).ok()?;
    if read.offset_bytes.is_some()
        || read.page_size_bytes.is_some()
        || !matches!(read.mode, crate::tools::handlers::read_file::ReadMode::Slice)
    {
        return None;
    }
    read.offset.checked_sub(1)
}

/// Admit recovery only for an explicit line range the selected reader honors.
pub(crate) fn bounded_line_read_path(args: &Value, max_lines: usize) -> Option<String> {
    let path_args: PathArgs = serde_json::from_value(args.clone()).ok()?;
    let path = Path::new(&path_args.path);
    if is_image_path(path) {
        return None;
    }
    if is_new_read_request(args) || !is_legacy_read_request(args, false) {
        let normalized = build_read_handler_args(args, path);
        // A missing/zero bound uses the handler default, not a targeted range.
        normalized.get("limit").and_then(parse_usize_value)?;
        let read: ReadFileArgs = serde_json::from_value(normalized).ok()?;
        if !(1..=max_lines).contains(&read.limit)
            || read.effective_line_limit() > max_lines
            || read.offset == 0
            || read.offset_bytes.is_some()
            || read.page_size_bytes.is_some()
            || !matches!(read.mode, crate::tools::handlers::read_file::ReadMode::Slice)
        {
            return None;
        }
    } else {
        let read: Input = serde_json::from_value(args.clone()).ok()?;
        let limit = read.page_size_lines?;
        if !(1..=max_lines).contains(&limit) || read.offset_bytes.is_some() || read.page_size_bytes.is_some() {
            return None;
        }
    }
    Some(path_args.path)
}

fn parse_usize_value(value: &Value) -> Option<usize> {
    value
        .as_u64()
        .and_then(|n| usize::try_from(n).ok())
        .or_else(|| value.as_str().and_then(|s| s.parse::<usize>().ok()))
}

fn has_explicit_limit(args: &Value) -> bool {
    ["limit", "l", "page_size_lines", "max_lines", "chunk_lines", "end_line"]
        .iter()
        .any(|key| args.get(*key).is_some())
}

fn has_explicit_offset(args: &Value) -> bool {
    ["offset", "o", "offset_lines", "offset_bytes", "start_line"]
        .iter()
        .any(|key| args.get(*key).is_some())
}

fn is_full_text_read(args: &Value, is_spool_output: bool) -> bool {
    !is_spool_output && !has_explicit_offset(args) && !has_explicit_limit(args)
}

use super::restore_exact_text_content;

fn response_size_bytes(response: &Value) -> Option<u64> {
    response
        .get("metadata")
        .and_then(|metadata| metadata.get("data"))
        .and_then(|data| data.get("size_bytes"))
        .and_then(Value::as_u64)
}

fn apply_spool_chunk_defaults(handler_args_json: &mut Value, raw_args: &Value) -> SpoolChunkPlan {
    let mut offset = 1usize;
    let mut limit = SPOOL_CHUNK_DEFAULT_LIMIT_LINES;

    if let Some(obj) = handler_args_json.as_object_mut() {
        offset = obj.get("offset").and_then(parse_usize_value).unwrap_or(1).max(1);

        let requested_limit = if has_explicit_limit(raw_args) {
            raw_args
                .get("limit")
                .or_else(|| raw_args.get("l"))
                .or_else(|| raw_args.get("page_size_lines"))
                .or_else(|| raw_args.get("max_lines"))
                .or_else(|| raw_args.get("chunk_lines"))
                .and_then(parse_usize_value)
                .unwrap_or(SPOOL_CHUNK_DEFAULT_LIMIT_LINES)
        } else {
            SPOOL_CHUNK_DEFAULT_LIMIT_LINES
        };

        limit = requested_limit.clamp(1, SPOOL_CHUNK_MAX_LIMIT_LINES);

        obj.insert("offset".to_string(), json!(offset));
        obj.insert("limit".to_string(), json!(limit));
        // Preserve narrow chunking behavior by bypassing MIN_BATCH_LIMIT expansion
        // in ReadFileHandler::handle (which only applies when max_tokens is absent).
        obj.entry("max_tokens".to_string())
            .or_insert_with(|| json!(SPOOL_CHUNK_SENTINEL_MAX_TOKENS));
    }

    SpoolChunkPlan { offset, limit }
}

fn path_contains_components(path: &Path, needle: &[&str]) -> bool {
    // Fail closed on empty needle: vacuous-true would mark every path as
    // privileged, and `windows(0)` would panic without this guard.
    if needle.is_empty() {
        return false;
    }
    use std::ffi::OsStr;
    let components: Vec<&OsStr> = path.components().map(|c| c.as_os_str()).collect();
    components
        .windows(needle.len())
        .any(|window| window.iter().zip(needle.iter()).all(|(hay, ndl)| *hay == OsStr::new(ndl)))
}

fn is_history_jsonl(path: &Path) -> bool {
    // Fast path: avoid component iteration if extension doesn't match
    if path.extension().and_then(|ext| ext.to_str()) != Some("jsonl") {
        return false;
    }

    path_contains_components(path, &[".vtcode", "history"])
}

fn is_tool_output_spool_path(path: &Path) -> bool {
    crate::tools::SpooledOutputReference::recognizes_path(path)
}

fn pty_session_id_from_tool_output_path(path: &Path) -> Option<String> {
    let file_name = path.file_name()?.to_str()?;
    let session_file = file_name.strip_suffix(".txt")?;
    let session_id = session_file.strip_prefix("write_stdin_").unwrap_or(session_file);
    if session_id.starts_with("run-")
        && session_id.len() > "run-".len()
        && session_id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
    {
        Some(session_id.to_string())
    } else {
        None
    }
}

fn build_history_cache_key(path: &Path, metadata: &std::fs::Metadata, args: &Value) -> Option<String> {
    let modified = metadata.modified().ok()?;
    let mtime = modified.duration_since(UNIX_EPOCH).ok()?.as_millis();
    let size = metadata.len();

    let mode = args.get("mode").and_then(Value::as_str).unwrap_or("legacy");
    let indentation = args.get("indentation").and_then(Value::as_str).unwrap_or("");
    let offset = args
        .get("offset")
        .or_else(|| args.get("o"))
        .or_else(|| args.get("offset_lines"))
        .or_else(|| args.get("offset_bytes"))
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let limit = args
        .get("limit")
        .or_else(|| args.get("l"))
        .or_else(|| args.get("page_size_lines"))
        .or_else(|| args.get("page_size_bytes"))
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let max_bytes = args.get("max_bytes").and_then(Value::as_u64).unwrap_or(0);
    let max_tokens = args.get("max_tokens").and_then(Value::as_u64).unwrap_or(0);
    let encoding = args.get("encoding").and_then(Value::as_str).unwrap_or("");

    Some(format!(
        "read_file:history:{}:{}:{}:mode={mode}|indent={indentation}|offset={offset}|limit={limit}|max_bytes={max_bytes}|max_tokens={max_tokens}|encoding={encoding}",
        path.display(),
        size,
        mtime
    ))
}

impl FileOpsTool {
    async fn track_read_snapshot(&self, path: &Path) {
        if let Err(err) = self.edited_file_monitor.track_read(path).await {
            tracing::warn!(
                path = %path.display(),
                error = %err,
                "Failed to record edited-file read snapshot"
            );
        }
    }

    fn track_read_text_snapshot(&self, path: &Path, content: &str) {
        if let Err(err) = self.edited_file_monitor.record_read_text(path, content) {
            tracing::warn!(
                path = %path.display(),
                error = %err,
                "Failed to record edited-file read snapshot"
            );
        }
    }

    async fn track_exact_text_snapshot(&self, path: &Path, content: &str, size_bytes: u64) {
        if let Some(exact_content) = restore_exact_text_content(content, size_bytes) {
            self.track_read_text_snapshot(path, &exact_content);
        } else {
            self.track_read_snapshot(path).await;
        }
    }

    async fn track_cached_read_snapshot(&self, path: &Path, args: &Value, response: &Value) {
        if is_full_text_read(args, false)
            && let Some(content) = response.get("content").and_then(Value::as_str)
            && let Some(size_bytes) = response_size_bytes(response)
        {
            self.track_exact_text_snapshot(path, content, size_bytes).await;
            return;
        }

        self.track_read_snapshot(path).await;
    }

    pub async fn read_file(&self, args: Value) -> Result<Value> {
        let use_cache = args.get(PATCH_READ_CACHE_NONCE).is_none();
        self.read_file_with_cache(args, use_cache).await
    }

    async fn read_file_with_cache(&self, args: Value, use_cache: bool) -> Result<Value> {
        let mut perf = PerfSpan::new("vtcode.perf.read_file_ms");

        let path_args: PathArgs = PathArgs::deserialize(&args).map_err(|_e| {
            if looks_like_patch_payload(&args) {
                return anyhow!(
                    "Error: Patch content was sent to read_file.\n\
                    Use apply_patch with {{\"patch\":\"...\"}} \
                    (or {{\"input\":\"...\"}}).\n\
                    read_file requires a path parameter."
                );
            }
            let received = serde_json::to_string(&args).unwrap_or_else(|_| "{}".to_string());
            anyhow!(
                "Error: Invalid 'read_file' arguments. Missing required path parameter.\n\
                Received: {received}\n\
                Expected: {{\"path\": \"file/path\"}} or {{\"file_path\": \"file/path\"}}\n\
                Accepted path parameters: path, file_path, filepath, target_path, file, p\n\
                Optional params: offset_lines, limit, max_bytes, max_tokens"
            )
        })?;

        let path_str = &path_args.path;

        // Try to resolve the file path
        let potential_paths = self.resolve_file_path(path_str).await?;
        let missing_spool_candidate = potential_paths
            .iter()
            .find(|candidate| is_tool_output_spool_path(candidate.as_path()))
            .cloned();
        let mut directory_candidate = None;

        for candidate_path in &potential_paths {
            if !tokio::fs::try_exists(candidate_path).await? {
                continue;
            }

            let canonical = self.normalize_and_validate_candidate(candidate_path, path_str).await?;

            if self.should_exclude(&canonical).await {
                continue;
            }

            let metadata = tokio::fs::metadata(&canonical).await?;
            if !metadata.is_file() {
                if metadata.is_dir() && directory_candidate.is_none() {
                    directory_candidate = Some(canonical);
                }
                continue;
            }

            let size_bytes = metadata.len();
            let history_jsonl = is_history_jsonl(&canonical);
            perf.tag("path_class", if history_jsonl { "history_jsonl" } else { "other" });
            let is_image = is_image_path(&canonical);
            let is_spool_output = is_tool_output_spool_path(&canonical);
            let is_legacy_request = is_legacy_read_request(&args, is_image);
            let is_new_request = is_new_read_request(&args);

            let cache_config = file_read_cache_config();
            let cache_key = if use_cache
                && cache_config.enabled
                && history_jsonl
                && size_bytes >= cache_config.min_size_bytes as u64
                && size_bytes <= cache_config.max_size_bytes as u64
            {
                build_history_cache_key(&canonical, &metadata, &args)
            } else {
                None
            };

            if let Some(key) = cache_key.as_ref()
                && let Some(cached) = FILE_CACHE.get_file(key).await
            {
                perf.tag("cache", "hit");
                self.track_cached_read_snapshot(&canonical, &args, &cached).await;
                return Ok(cached);
            }
            perf.tag("cache", if cache_key.is_some() { "miss" } else { "skip" });

            if !is_image && (is_new_request || !is_legacy_request) {
                let mut handler_args_json = build_read_handler_args(&args, &canonical);
                let spool_plan = if is_spool_output {
                    Some(apply_spool_chunk_defaults(&mut handler_args_json, &args))
                } else {
                    None
                };
                match serde_json::from_value::<ReadFileArgs>(handler_args_json) {
                    Ok(read_args) => {
                        let requested_path = self.workspace_relative_display(&canonical);
                        let handler = ReadFileHandler;
                        let requested_offset = read_args.offset;
                        let ReadFileOutcome {
                            content,
                            lines_read: lines_returned,
                            has_more,
                            line_truncated,
                            capped_by_limit,
                            applied_limit,
                        } = handler.handle_detailed(read_args).await?;
                        let full_text_read = is_full_text_read(&args, is_spool_output);
                        let response_content = if full_text_read {
                            restore_exact_text_content(&content, size_bytes).unwrap_or_else(|| content.clone())
                        } else {
                            content.clone()
                        };

                        let mut builder = ToolResponseBuilder::new(tools::READ_FILE)
                            .success()
                            .message(format!("Successfully read file {requested_path}"))
                            .content(&response_content)
                            .field("path", json!(requested_path.clone()))
                            .field("no_spool", json!(true))
                            .field("content_kind", json!("text"))
                            .field("encoding", json!("utf8"))
                            .data("size_bytes", json!(size_bytes))
                            .data("content_kind", json!("text"))
                            .data("encoding", json!("utf8"));

                        if line_truncated {
                            builder = builder
                                .field("line_truncated", json!(true))
                                .field("truncated", json!(true))
                                .field("truncation_reason", json!("oversized_line"));
                        }

                        if let Some(plan) = spool_plan {
                            let next_offset = plan.offset.saturating_add(lines_returned);

                            builder = builder
                                .field("spool_chunked", json!(true))
                                .field("lines_returned", json!(lines_returned));
                            if has_more {
                                builder = builder.field("has_more", json!(true));
                                builder = builder.field(
                                    "next_read_args",
                                    ReadChunkContinuationArgs::new(requested_path.clone(), next_offset, plan.limit)
                                        .to_value(),
                                );
                                builder = builder.field(
                                    "next_action",
                                    json!(
                                        "Call file read again with the `next_read_args` object above verbatim as arguments to get the next chunk, or narrow with `exec_command.cmd` using `rg` and a specific pattern. Re-reading the same offset returns nothing new and counts toward the per-turn spool chunk cap, which blocks further reads."
                                    ),
                                );
                            }
                        }
                        // Absolute-cap continuation. Surface a structured way to
                        // keep reading after the per-call ceiling was reached.
                        //
                        // The condition is `has_more && applied_limit == cap` rather
                        // than `capped_by_limit && has_more`: on the first (clamped)
                        // page `applied_limit` equals the cap and `capped_by_limit`
                        // is true, but on every follow-up page the agent passes back
                        // `applied_limit` as its `limit`, so `effective_limit` is no
                        // longer *greater* than the cap and `capped_by_limit` becomes
                        // false. Using `applied_limit == cap` keeps the pagination
                        // chain alive on every page that actually hit the ceiling,
                        // instead of dying after a single hop.
                        // Byte-range reads set `applied_limit` to 0, so they are
                        // correctly excluded and use their own `offset_bytes`/
                        // `page_size_bytes` continuation path.
                        else if has_more && applied_limit == crate::tools::read_limits::absolute_line_cap() {
                            let next_offset = requested_offset.saturating_add(lines_returned);
                            let next_action = if capped_by_limit {
                                "Read clamped to max_read_lines. \
                                 Call file read again with `next_read_args` verbatim to get subsequent chunks; \
                                 do not re-read the same range."
                            } else {
                                "Call file read again with `next_read_args` verbatim to get subsequent chunks; \
                                 do not re-read the same range."
                            };
                            builder = builder
                                .field("has_more", json!(true))
                                .field("capped_by_limit", json!(capped_by_limit))
                                .field(
                                    "next_read_args",
                                    ReadChunkContinuationArgs::new(requested_path.clone(), next_offset, applied_limit)
                                        .to_value(),
                                )
                                .field("next_action", json!(next_action));
                        }

                        let response = builder.build_json();

                        if full_text_read {
                            self.track_exact_text_snapshot(&canonical, &response_content, size_bytes).await;
                        } else {
                            self.track_read_snapshot(&canonical).await;
                        }
                        if let Some(key) = cache_key.as_ref() {
                            FILE_CACHE.put_file(key.clone(), response.clone()).await;
                        }

                        return Ok(response);
                    }
                    Err(e) => {
                        if is_new_request {
                            return Err(anyhow!(
                                "Failed to parse arguments for read_file handler: {e}. Args: {args:?}"
                            ));
                        }
                    }
                }
            }

            // Legacy path
            let input: Input = serde_json::from_value(args.clone())
                .context("Error: Invalid 'read_file' arguments for legacy handler.")?;

            // Check if paging/offset is requested
            let use_paging = input.offset_bytes.is_some()
                || input.page_size_bytes.is_some()
                || input.offset_lines.is_some()
                || input.page_size_lines.is_some();

            let (content, metadata, truncated, line_truncated) = if use_paging {
                self.read_file_paged(&canonical, &input).await?
            } else {
                let (content, metadata, truncated) = self.read_file_legacy(&canonical, &input).await?;
                let line_truncated = metadata.get("line_truncated").and_then(Value::as_bool).unwrap_or(false);
                (content, metadata, truncated, line_truncated)
            };

            let mut builder = ToolResponseBuilder::new(tools::READ_FILE)
                .success()
                .message(format!(
                    "Successfully read {} bytes from {}",
                    size_bytes,
                    self.workspace_relative_display(&canonical)
                ))
                .content(&content)
                .field("path", json!(self.workspace_relative_display(&canonical)))
                .field("no_spool", json!(true));

            // Merge legacy metadata - extract actual data fields, not wrapper structure
            if let Some(obj) = metadata.as_object() {
                for (k, v) in obj {
                    builder = builder.data(k.clone(), v.clone());
                }
            }

            // Special legacy fields at top level
            if let Some(is_truncated) = metadata.get("is_truncated").and_then(Value::as_bool) {
                builder = builder.field("is_truncated", json!(is_truncated));
            }
            if line_truncated {
                builder = builder.field("line_truncated", json!(true));
            }
            if let Some(encoding) = metadata.get("encoding").and_then(Value::as_str) {
                builder = builder.field("encoding", json!(encoding));
            }
            if let Some(content_kind) = metadata.get("content_kind").and_then(Value::as_str) {
                builder = builder.field("content_kind", json!(content_kind));
                if matches!(content_kind, "binary" | "image") {
                    builder = builder.field("binary", json!(true));
                }
            }
            if let Some(mime_type) = metadata.get("mime_type").and_then(Value::as_str) {
                builder = builder.field("mime_type", json!(mime_type));
            }

            // Add paging information
            if use_paging {
                if let Some(offset_bytes) = input.offset_bytes {
                    builder = builder.field("offset_bytes", json!(offset_bytes));
                }
                if let Some(page_size_bytes) = input.page_size_bytes {
                    builder = builder.field("page_size_bytes", json!(page_size_bytes));
                }
                if let Some(offset_lines) = input.offset_lines {
                    builder = builder.field("offset_lines", json!(offset_lines));
                }
                if let Some(page_size_lines) = input.page_size_lines {
                    builder = builder.field("page_size_lines", json!(page_size_lines));
                }
                if truncated {
                    builder = builder.field("truncated", json!(true));
                    builder = builder.field(
                        "truncation_reason",
                        json!(if line_truncated {
                            "oversized_line"
                        } else {
                            "reached_end_of_file"
                        }),
                    );
                }
            }

            let content_kind = metadata.get("content_kind").and_then(Value::as_str).unwrap_or("text");
            let full_legacy_text_read = !use_paging && !truncated && content_kind == "text";
            let response = builder.build_json();
            if full_legacy_text_read {
                self.track_exact_text_snapshot(&canonical, &content, size_bytes).await;
            } else {
                self.track_read_snapshot(&canonical).await;
            }
            if let Some(key) = cache_key.as_ref() {
                FILE_CACHE.put_file(key.clone(), response.clone()).await;
            }

            return Ok(response);
        }

        if let Some(spool_path) = missing_spool_candidate {
            if let Some(session_id) = pty_session_id_from_tool_output_path(&spool_path) {
                return Err(anyhow!(
                    "Error: Session output file not found: {}. This looks like command session output. Use write_stdin with session_id=\"{}\" to poll or continue the active session.",
                    self.workspace_relative_display(&spool_path),
                    session_id,
                ));
            }
            return Err(anyhow!(
                "Error: Spool file not found (possibly expired): {}. Re-run the original tool command to regenerate this output.",
                self.workspace_relative_display(&spool_path),
            ));
        }

        if let Some(directory_path) = directory_candidate {
            let display_path = self.workspace_relative_display(&directory_path);
            return Err(anyhow!(
                "Error: Path '{display_path}' is a directory, not a file. Use `exec_command.cmd` with `find` or `ls` to inspect it.",
            ));
        }

        let parent_dir = Path::new(path_str)
            .parent()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|| ".".to_string());
        let suggestion = self.missing_path_suggestion_suffix(path_str, PathSuggestionKind::File).await;
        Err(anyhow!(
            "Error: File not found: {}. Tried paths: {}.{} Use `exec_command.cmd` with `find {}` to discover available files.",
            path_str,
            potential_paths
                .iter()
                .map(|p| self.workspace_relative_display(p))
                .collect::<Vec<_>>()
                .join(", "),
            suggestion,
            parent_dir
        ))
    }
}

#[cfg(test)]
mod read_tests;