vtcode 0.136.4

A Rust-based terminal coding agent with modular architecture supporting multiple LLM providers
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
use serde_json::{Value, json};
use std::borrow::Cow;
use std::time::Duration;
use vtcode_core::config::constants::tools as tool_names;
use vtcode_core::tools::registry::ToolRegistry;
use vtcode_core::tools::tool_intent;

use crate::agent::runloop::unified::tool_reads::{is_read_file_style_call, read_file_path_arg};

pub(super) use crate::agent::runloop::unified::tool_reads::spool_chunk_read_path;

// Read-offset / read-limit field aliases are the canonical vocabulary owned by
// `tool_outcomes::read_extent` — the single source of truth shared with the
// cross-turn dedup normalizer and the summarization gate. The family-cap slice
// suffix distinguishes `off=` from `lim=`, so it consumes the two lists
// separately. Previously each site kept its own drifting copy; delegating here
// guarantees they can never diverge (which used to falsely collapse paginated
// reads into one family key and deadlock the agent on large files).
use crate::agent::runloop::unified::turn::tool_outcomes::read_extent::{LIMIT_KEYS, OFFSET_KEYS};

fn compact_loop_key_part(value: &str, max_chars: usize) -> String {
    value.trim().chars().take(max_chars).collect()
}

fn compact_loop_text(value: &str, max_chars: usize) -> String {
    let collapsed = value.split_whitespace().fold(String::new(), |mut acc, s| {
        if !acc.is_empty() {
            acc.push(' ');
        }
        acc.push_str(s);
        acc
    });
    compact_loop_key_part(&collapsed, max_chars)
}

fn normalize_shell_command_text(value: &str, max_chars: usize) -> String {
    compact_loop_text(&value.chars().filter(|ch| !matches!(ch, '\'' | '"')).collect::<String>(), max_chars)
}

fn normalized_shell_command_arg(args: &Value, max_chars: usize) -> Option<String> {
    vtcode_core::tools::command_args::command_text(args)
        .ok()
        .flatten()
        .map(|command| normalize_shell_command_text(&command, max_chars))
        .filter(|command| !command.is_empty())
}

fn first_arg_value_by_keys<'a>(args: &'a Value, keys: &[&str]) -> Option<&'a Value> {
    keys.iter().find_map(|key| args.get(*key))
}

fn has_any_arg_by_keys(args: &Value, keys: &[&str]) -> bool {
    first_arg_value_by_keys(args, keys).is_some()
}

fn read_file_has_offset_arg(args: &Value) -> bool {
    has_any_arg_by_keys(args, OFFSET_KEYS)
}

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

fn read_file_has_limit_arg(args: &Value) -> bool {
    has_any_arg_by_keys(args, LIMIT_KEYS)
}

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

/// Read-only flag. `unified_file`/`read_file` honor `raw` (bypass LLM
/// summarization). Two reads of the same path + same slice but different `raw`
/// modes return *different* payloads, so they must not be treated as the same
/// family call. `true`/`false`/absent are all distinct suffixes.
fn read_file_raw_flag(args: &Value) -> Option<bool> {
    args.get("raw").and_then(Value::as_bool)
}

/// Build a slice descriptor for a read-file call so the family-cap can
/// distinguish paginated reads of the same path.
///
/// The cap exists to stop true retry loops (same path + same slice, repeated
/// verbatim). When the model paginates — same path, different `offset`/`limit`
/// — or flips `raw` to bypass summarization, those are *different* logical
/// reads, not retries. Without this suffix, four reads of one large file with
/// four different `offset`/`limit` pairs all collapse into one family key and
/// trip the cap at 4, even though no slice was read twice. That forces a
/// tool-free recovery pass that produces a garbage final answer.
///
/// The suffix includes only fields that change *what* is read:
/// - `offset`/`offset_lines`/`offset_bytes` -> `off=<n>`
/// - `limit`/`page_size_lines`/`max_lines`/`chunk_lines` -> `lim=<n>`
/// - `raw` -> `raw=<bool>`
///
/// Fields that are absent contribute nothing, so a bare default read
/// (`{path}` with no offset/limit/raw) still produces the same key it did
/// before — only paginated/raw-flipped reads gain suffixes.
fn read_file_slice_suffix(args: &Value) -> String {
    let mut parts: Vec<String> = Vec::new();
    if let Some(offset) = read_file_offset_value(args) {
        parts.push(format!("off={offset}"));
    }
    if let Some(limit) = read_file_limit_value(args) {
        parts.push(format!("lim={limit}"));
    }
    if let Some(raw) = read_file_raw_flag(args) {
        parts.push(format!("raw={raw}"));
    }
    if parts.is_empty() {
        String::new()
    } else {
        format!("::{}", parts.join("::"))
    }
}

pub(super) fn shell_run_signature(canonical_tool_name: &str, args: &Value) -> Option<String> {
    // The family-key prefix must stay stable across internal tool renames
    // (e.g. `command_session_internal` -> `command_session`) so loop detection
    // keeps working. Use the stable label.
    if !tool_intent::is_command_run_tool_call(canonical_tool_name, args) {
        return None;
    }

    let command = normalized_shell_command_arg(args, 200)?;
    Some(format!("command_session::{}", command))
}

pub(super) fn maybe_apply_spool_read_offset_hint(
    tool_registry: &mut ToolRegistry,
    canonical_tool_name: &str,
    args: &Value,
) -> Value {
    if !is_read_file_style_call(canonical_tool_name, args) {
        return args.clone();
    }

    let Some(path) = spool_chunk_read_path(canonical_tool_name, args) else {
        return args.clone();
    };

    let Some((next_offset, chunk_limit)) =
        tool_registry.find_recent_read_file_spool_progress(path, Duration::from_secs(180))
    else {
        return args.clone();
    };

    let requested_offset = read_file_offset_value(args);
    let should_advance_offset = match requested_offset {
        Some(existing) => existing < next_offset,
        None => true,
    };
    let should_fill_offset = !read_file_has_offset_arg(args);

    let mut adjusted = args.clone();
    let keep_existing_limit = read_file_has_limit_arg(&adjusted);
    if let Some(obj) = adjusted.as_object_mut() {
        if should_fill_offset || should_advance_offset {
            obj.insert("offset".to_string(), json!(next_offset));
        }
        if !keep_existing_limit {
            obj.insert("limit".to_string(), json!(chunk_limit));
        }
        if should_fill_offset || should_advance_offset || !keep_existing_limit {
            tracing::debug!(
                tool = canonical_tool_name,
                path = path,
                requested_offset = requested_offset.unwrap_or(0),
                next_offset,
                chunk_limit,
                "Applied spool read continuation hint to avoid repeated identical chunk reads"
            );
        }
    }
    adjusted
}

pub(super) fn task_tracker_create_signature(tool_name: &str, args: &Value) -> Option<String> {
    if tool_name != tool_names::TASK_TRACKER {
        return None;
    }

    let action = args.get("action").and_then(Value::as_str)?;
    if action != "create" {
        return None;
    }

    #[derive(serde::Serialize)]
    struct TaskTrackerCreateSignature<'a> {
        title: Option<&'a Value>,
        items: Option<&'a Value>,
        notes: Option<&'a Value>,
    }

    let payload = TaskTrackerCreateSignature {
        title: args.get("title"),
        items: args.get("items"),
        notes: args.get("notes"),
    };
    let payload_str = serde_json::to_string(&payload).ok()?;
    let mut signature = String::with_capacity("task_tracker::create::".len() + payload_str.len());
    signature.push_str("task_tracker::create::");
    signature.push_str(&payload_str);

    Some(signature)
}
/// Map an incoming tool name to the stable family-label prefix used by
/// loop/duplicate detection. Internal dispatcher names (e.g.
/// `command_session_internal`) are renamed to their stable public-facing
/// labels so the family contract survives internal renames. Legacy short
/// aliases (`file_operation`, `search_dispatch`, `command_session`) are also
/// accepted so callers/tests that predate the rename keep working.
fn stable_family_label(name: &str) -> Cow<'_, str> {
    match name {
        tool_names::UNIFIED_EXEC | "command_session" => Cow::Borrowed("unified_exec"),
        tool_names::UNIFIED_FILE | "file_operation" => Cow::Borrowed("unified_file"),
        tool_names::UNIFIED_SEARCH | "search_dispatch" => Cow::Borrowed("search_dispatch"),
        _ => Cow::Borrowed(name),
    }
}

pub(crate) fn low_signal_family_key(canonical_tool_name: &str, args: &Value) -> Option<String> {
    let label = stable_family_label(canonical_tool_name);
    match canonical_tool_name {
        tool_names::READ_FILE => read_file_path_arg(args)
            .map(|path| format!("{label}::{}{}", compact_loop_key_part(path, 120), read_file_slice_suffix(args),)),
        tool_names::UNIFIED_FILE => {
            let action = tool_intent::file_operation_action(args).unwrap_or("read");
            if !action.eq_ignore_ascii_case("read") {
                return None;
            }
            read_file_path_arg(args).map(|path| {
                format!("{label}::read::{}{}", compact_loop_key_part(path, 120), read_file_slice_suffix(args),)
            })
        }
        tool_names::UNIFIED_EXEC => {
            normalized_shell_command_arg(args, 160).map(|command| format!("{label}::run::{command}"))
        }
        tool_names::CODE_SEARCH => vtcode_core::tools::normalised_code_search_loop_identity(args)
            .map(|identity| format!("{canonical_tool_name}::{identity}")),
        tool_names::UNIFIED_SEARCH => {
            let action = args.get("action").and_then(Value::as_str).unwrap_or("grep");
            let mut key = format!("{label}::{action}");
            if matches!(action, "grep" | "structural") {
                if let Some(pattern) = args
                    .get("pattern")
                    .and_then(Value::as_str)
                    .map(|p| compact_loop_text(p, 80))
                    .filter(|p| !p.is_empty())
                {
                    key.push_str("::pat=");
                    key.push_str(&pattern);
                }
            }
            let path = args
                .get("path")
                .and_then(Value::as_str)
                .map(|value| compact_loop_key_part(value, 120))
                .unwrap_or_else(|| ".".to_string());
            key.push_str("::");
            key.push_str(&path);
            Some(key)
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{
        read_file_has_limit_arg, read_file_has_offset_arg, read_file_limit_value, read_file_offset_value,
        read_file_raw_flag, read_file_slice_suffix,
    };
    use crate::agent::runloop::unified::turn::tool_outcomes::handlers::low_signal_family_key;
    use serde_json::json;
    use vtcode_core::config::constants::tools as tool_names;

    #[test]
    fn read_file_offset_value_accepts_alias_keys() {
        assert_eq!(read_file_offset_value(&json!({"offset": 7})), Some(7));
        assert_eq!(read_file_offset_value(&json!({"offset_lines": "8"})), Some(8));
        assert_eq!(read_file_offset_value(&json!({"offset_bytes": 9})), Some(9));
    }

    #[test]
    fn read_file_has_offset_arg_accepts_alias_keys() {
        assert!(read_file_has_offset_arg(&json!({"offset_lines": 1})));
        assert!(read_file_has_offset_arg(&json!({"offset_bytes": 1})));
        assert!(!read_file_has_offset_arg(&json!({"path": "src/main.rs"})));
    }

    #[test]
    fn read_file_has_limit_arg_accepts_alias_keys() {
        assert!(read_file_has_limit_arg(&json!({"limit": 10})));
        assert!(read_file_has_limit_arg(&json!({"page_size_lines": 10})));
        assert!(read_file_has_limit_arg(&json!({"max_lines": 10})));
        assert!(read_file_has_limit_arg(&json!({"chunk_lines": 10})));
        assert!(!read_file_has_limit_arg(&json!({"path": "src/main.rs"})));
    }

    #[test]
    fn read_file_limit_value_accepts_alias_keys() {
        assert_eq!(read_file_limit_value(&json!({"limit": 10})), Some(10));
        assert_eq!(read_file_limit_value(&json!({"page_size_lines": "20"})), Some(20));
        assert_eq!(read_file_limit_value(&json!({"max_lines": 5})), Some(5));
        assert_eq!(read_file_limit_value(&json!({"chunk_lines": 3})), Some(3));
        assert_eq!(read_file_limit_value(&json!({"path": "src/main.rs"})), None);
    }

    #[test]
    fn read_file_raw_flag_reads_optional_bool() {
        assert_eq!(read_file_raw_flag(&json!({"raw": true})), Some(true));
        assert_eq!(read_file_raw_flag(&json!({"raw": false})), Some(false));
        assert_eq!(read_file_raw_flag(&json!({"path": "x"})), None);
    }

    #[test]
    fn read_file_slice_suffix_is_empty_when_unpaginated() {
        // A bare read with no offset/limit/raw must keep the legacy key
        // unchanged so true retry loops (same path, no slice) still collide.
        assert_eq!(read_file_slice_suffix(&json!({"path": "src/lib.rs"})), "");
    }

    #[test]
    fn read_file_slice_suffix_distinguishes_offsets() {
        let off0 = read_file_slice_suffix(&json!({"path": "x", "offset": 0}));
        let off80 = read_file_slice_suffix(&json!({"path": "x", "offset": 80}));
        assert_eq!(off0, "::off=0");
        assert_eq!(off80, "::off=80");
        assert_ne!(off0, off80);
    }

    #[test]
    fn read_file_slice_suffix_distinguishes_limits() {
        let lim100 = read_file_slice_suffix(&json!({"path": "x", "limit": 100}));
        let lim200 = read_file_slice_suffix(&json!({"path": "x", "limit": 200}));
        assert_eq!(lim100, "::lim=100");
        assert_eq!(lim200, "::lim=200");
        assert_ne!(lim100, lim200);
    }

    #[test]
    fn read_file_slice_suffix_distinguishes_raw_flag() {
        let no_raw = read_file_slice_suffix(&json!({"path": "x"}));
        let raw_true = read_file_slice_suffix(&json!({"path": "x", "raw": true}));
        let raw_false = read_file_slice_suffix(&json!({"path": "x", "raw": false}));
        assert_eq!(no_raw, "");
        assert_eq!(raw_true, "::raw=true");
        assert_eq!(raw_false, "::raw=false");
        assert_ne!(raw_true, raw_false);
        assert_ne!(raw_true, no_raw);
    }

    #[test]
    fn read_file_slice_suffix_combines_all_present_fields() {
        let suffix = read_file_slice_suffix(&json!({
            "path": "x",
            "offset": 80,
            "limit": 200,
            "raw": true
        }));
        assert_eq!(suffix, "::off=80::lim=200::raw=true");
    }

    #[test]
    fn low_signal_family_key_distinguishes_paginated_reads_of_same_path() {
        // Reproduces turn_613: four reads of the same file with different
        // offset/limit/raw must produce four distinct family keys, so the
        // per-turn family cap does not trip on legitimate pagination.
        let base =
            low_signal_family_key(tool_names::UNIFIED_FILE, &json!({"action": "read", "path": "src/cli/update.rs"}));
        let off81 = low_signal_family_key(
            tool_names::UNIFIED_FILE,
            &json!({"action": "read", "path": "src/cli/update.rs", "offset": 81, "limit": 229}),
        );
        let off80 = low_signal_family_key(
            tool_names::UNIFIED_FILE,
            &json!({"action": "read", "path": "src/cli/update.rs", "offset": 80, "limit": 200}),
        );
        let raw = low_signal_family_key(
            tool_names::UNIFIED_FILE,
            &json!({
                "action": "read",
                "path": "src/cli/update.rs",
                "offset": 80,
                "limit": 200,
                "raw": true
            }),
        );

        let keys = [base.clone(), off81.clone(), off80.clone(), raw.clone()];
        let unique: std::collections::HashSet<_> = keys.iter().cloned().collect();
        assert_eq!(unique.len(), 4, "paginated reads must have distinct family keys, got: {keys:?}");

        // Sanity: the bare read keeps the legacy key (no slice suffix).
        assert_eq!(base.as_deref(), Some("unified_file::read::src/cli/update.rs"));
        assert!(off81.unwrap().ends_with("::off=81::lim=229"));
        assert!(off80.unwrap().ends_with("::off=80::lim=200"));
        assert!(raw.unwrap().ends_with("::off=80::lim=200::raw=true"));
    }

    #[test]
    fn low_signal_family_key_collides_for_identical_slice_retry() {
        // True retry loop: same path + same slice must still collide so the
        // cap can stop it. This is the guard's reason for existing.
        let first = low_signal_family_key(
            tool_names::UNIFIED_FILE,
            &json!({
                "action": "read",
                "path": "src/lib.rs",
                "offset": 0,
                "limit": 100
            }),
        );
        let second = low_signal_family_key(
            tool_names::UNIFIED_FILE,
            &json!({
                "action": "read",
                "path": "src/lib.rs",
                "offset": 0,
                "limit": 100
            }),
        );
        assert_eq!(first, second, "identical slice retries must share a family key");
    }

    #[test]
    fn low_signal_family_key_read_file_distinguishes_paginated_reads() {
        // read_file (not unified_file) must also be slice-aware.
        let off0 = low_signal_family_key(tool_names::READ_FILE, &json!({"path": "src/lib.rs", "offset": 0}));
        let off80 = low_signal_family_key(tool_names::READ_FILE, &json!({"path": "src/lib.rs", "offset": 80}));
        assert_ne!(off0, off80, "different offsets must produce different keys");
        assert!(off0.unwrap().ends_with("::off=0"));
        assert!(off80.unwrap().ends_with("::off=80"));
    }
}