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
759
760
761
762
763
764
765
766
767
use super::super::ToolExecutionRequest;
use super::super::assembly::public_tool_name_candidates;
use super::{
    DispatchMode, ToolRegistry, coerce_string_to_schema_type_in_place, configured_file_operation_max_payload_bytes,
    enforce_file_operation_payload_limit, is_missing_required_arg, normalize_tool_args,
    parse_file_operation_max_payload_bytes, parse_string_as_schema_type, preflight_validate_call,
    preflight_validate_resolved_call, public_exec_validation_args, resolve_dispatch_target,
    resolve_internal_dispatch_tool,
};
use crate::config::constants::tools as tool_names;
use crate::tools::command_args::parse_indexed_command_parts;
use crate::tools::request_user_input::RequestUserInputTool;
use crate::tools::traits::Tool;
use anyhow::Result;
use serde_json::{Value, json};

async fn new_test_registry() -> (tempfile::TempDir, ToolRegistry) {
    let temp = tempfile::tempdir().expect("temp workspace");
    let registry = ToolRegistry::new(temp.path().to_path_buf()).await;
    (temp, registry)
}

#[tokio::test]
async fn internal_dispatch_preserves_normalization_and_candidate_order() {
    let (_temp, registry) = new_test_registry().await;
    for name in [
        "read_file",
        "  ReAd_FiLe  ",
        "functions.ReAd_FiLe",
        "tools.read-file",
        "READ FILE",
        "unknown:READ_FILE",
    ] {
        assert_eq!(resolve_internal_dispatch_tool(&registry, name).as_deref(), Some(tool_names::READ_FILE), "{name}");
        assert_eq!(resolve_dispatch_target(&registry, name, DispatchMode::Harness).unwrap(), tool_names::READ_FILE);
        assert!(resolve_dispatch_target(&registry, name, DispatchMode::ModelPublic).is_err(), "{name}");
    }
    for name in [tool_names::WRITE_FILE, tool_names::EDIT_FILE, tool_names::LIST_FILES] {
        assert_eq!(resolve_internal_dispatch_tool(&registry, name).as_deref(), Some(name));
    }
    assert_eq!(
        resolve_internal_dispatch_tool(&registry, "read_file<|channel|>WRITE_FILE").as_deref(),
        Some(tool_names::WRITE_FILE)
    );
    assert_eq!(
        resolve_internal_dispatch_tool(&registry, "read_file<|channel|>write_file:list_files").as_deref(),
        Some(tool_names::READ_FILE)
    );
}

#[tokio::test]
async fn internal_dispatch_rejects_hidden_tools_outside_allowlist_and_near_misses() {
    let (_temp, registry) = new_test_registry().await;
    assert!(registry.inventory.registration_for(tool_names::GET_ERRORS).is_some());
    for name in [
        "",
        " \t\u{2003}",
        "GET_ERRORS",
        "functions.get_errors",
        "read_files",
        "repo_browser.read_file",
        "READ_FİLE",
    ] {
        assert!(resolve_internal_dispatch_tool(&registry, name).is_none(), "{name:?}");
        assert!(resolve_dispatch_target(&registry, name, DispatchMode::Harness).is_err(), "{name:?}");
        assert!(resolve_dispatch_target(&registry, name, DispatchMode::ModelPublic).is_err(), "{name:?}");
    }
}

#[test]
fn normalize_elides_truncation_only_piped_verifier() {
    // The pipeline exit status belongs to the truncator, so the piped
    // form must never reach validation or execution: the standalone
    // verifier runs instead and its status is truthful.
    let args = json!({"action": "run", "command": "cargo check --locked 2>&1 | head -c 4000"});
    let normalized =
        normalize_tool_args(tool_names::UNIFIED_EXEC, &args, None).expect("rewrite must not fail normalization");
    assert_eq!(normalized.as_ref()["command"], "cargo check --locked 2>&1");
    // The caller's `cmd` spelling is left intact as the typed record.
    assert_eq!(normalized.as_ref()["cmd"], Value::Null);

    let via_cmd = json!({"action": "run", "cmd": "cargo nextest run 2>&1 | tail -15"});
    let via_cmd =
        normalize_tool_args(tool_names::UNIFIED_EXEC, &via_cmd, None).expect("rewrite must not fail normalization");
    assert_eq!(via_cmd.as_ref()["command"], "cargo nextest run 2>&1");
    assert_eq!(via_cmd.as_ref()["cmd"], "cargo nextest run 2>&1 | tail -15");
}

#[test]
fn normalize_leaves_non_rewritable_pipelines_untouched() {
    for command in [
        "cargo check | grep error; true",
        "cargo check && rm -rf target | tail -5",
        "cargo check | tail -5; rm foo.txt",
        "rg -n 'pattern' src | head -20",
        "cargo check --locked",
    ] {
        let args = json!({"action": "run", "command": command});
        let normalized =
            normalize_tool_args(tool_names::UNIFIED_EXEC, &args, None).expect("normalization must succeed");
        assert_eq!(normalized.as_ref()["command"], Value::String(command.to_string()), "must run as typed: {command}");
    }
}

#[test]
fn exec_command_validation_path_rewrites_piped_verifier() {
    let args = json!({"cmd": "cargo check --locked | tail -8"});
    let Some(exec_args) =
        public_exec_validation_args(tool_names::EXEC_COMMAND, &args).expect("validation args must build")
    else {
        panic!("EXEC_COMMAND run must produce exec validation args");
    };
    assert_eq!(exec_args["command"], "cargo check --locked");
}

#[tokio::test]
async fn preflight_accepts_output_limit_for_legacy_strict_schemas() {
    let (_temp, registry) = new_test_registry().await;
    let outcome = preflight_validate_call(
        &registry,
        tool_names::CODE_SEARCH,
        &json!({"query": "ToolRegistry", "max_output_tokens": 37}),
    )
    .expect("a valid integer output limit should pass preflight");
    assert_eq!(outcome.effective_args["max_output_tokens"], 37);
}

#[tokio::test]
async fn preflight_uses_default_output_limit_when_omitted() {
    let (_temp, registry) = new_test_registry().await;
    let outcome = preflight_validate_call(&registry, tool_names::CODE_SEARCH, &json!({"query": "ToolRegistry"}))
        .expect("omitted output limits should remain dispatchable");

    assert_eq!(
        crate::tools::output_limits::max_output_tokens(&outcome.effective_args)
            .expect("default output limit should be valid"),
        vtcode_utility_tool_specs::DEFAULT_MAX_OUTPUT_TOKENS
    );
}

#[tokio::test]
async fn preflight_rejects_invalid_output_limits_before_dispatch() {
    let (_temp, registry) = new_test_registry().await;
    let error = preflight_validate_call(
        &registry,
        tool_names::CODE_SEARCH,
        &json!({"query": "ToolRegistry", "max_output_tokens": "37"}),
    )
    .expect_err("string output limits must be rejected");
    assert!(error.to_string().contains("max_output_tokens must be an integer"));
}

// --- schema-aware string-type coercion -------------------------------------
//
// Regression coverage for the checkpoint-observed defect where the model
// emitted JSON-encoded strings (`"[\"path\"]"`, `"10"`) for array/integer
// fields and retried the same malformed call six times in one turn.

#[test]
fn parse_string_as_schema_type_coerces_array_and_integer() {
    assert_eq!(parse_string_as_schema_type(r#"["path"]"#, "array"), Some(json!(["path"])));
    assert_eq!(parse_string_as_schema_type("10", "integer"), Some(json!(10)));
}

#[test]
fn parse_string_as_schema_type_coerces_boolean_object_and_number() {
    assert_eq!(parse_string_as_schema_type("true", "boolean"), Some(json!(true)));
    assert_eq!(parse_string_as_schema_type("false", "boolean"), Some(json!(false)));
    assert_eq!(parse_string_as_schema_type(r#"{"a": 1}"#, "object"), Some(json!({"a": 1})));
    assert_eq!(parse_string_as_schema_type("3.5", "number"), Some(json!(3.5)));
    // An integer literal is also a valid JSON number.
    assert_eq!(parse_string_as_schema_type("3", "number"), Some(json!(3)));
}

#[test]
fn parse_string_as_schema_type_rejects_float_for_integer() {
    // "10.0" must not be silently coerced to an integer: that would be a
    // lossy reinterpretation. Let strict validation surface the type error.
    assert_eq!(parse_string_as_schema_type("10.0", "integer"), None);
}

#[test]
fn parse_string_as_schema_type_rejects_mismatched_and_malformed() {
    // Parsed type does not match schema type.
    assert_eq!(parse_string_as_schema_type(r#"{"a": 1}"#, "array"), None);
    assert_eq!(parse_string_as_schema_type(r#"["path"]"#, "object"), None);
    assert_eq!(parse_string_as_schema_type("10", "boolean"), None);
    // Malformed JSON stays a string and is left for strict validation.
    assert_eq!(parse_string_as_schema_type("not json", "array"), None);
    assert_eq!(parse_string_as_schema_type("[\"path\"", "array"), None);
    // Unknown schema type is never coerced.
    assert_eq!(parse_string_as_schema_type("anything", "string"), None);
}

#[test]
fn coerce_string_to_schema_type_in_place_only_touches_strings() {
    let mut already_array = json!(["path"]);
    let schema = json!({"type": "array"});
    assert!(!coerce_string_to_schema_type_in_place(&mut already_array, &schema));
    assert_eq!(already_array, json!(["path"]));

    let mut no_type = json!("keep me");
    let schema = json!({}); // no declared type → free-form string
    assert!(!coerce_string_to_schema_type_in_place(&mut no_type, &schema));
    assert_eq!(no_type, json!("keep me"));
}

#[test]
fn normalize_tool_args_coerces_code_search_stringified_args() {
    // Mirrors the exact checkpoint defect: result_types and max_results
    // arrive as JSON-encoded strings.
    let schema = json!({
        "type": "object",
        "properties": {
            "query": {"type": "string"},
            "result_types": {"type": "array", "items": {"type": "string", "enum": ["definition", "usage", "text", "path"]}},
            "max_results": {"type": "integer", "minimum": 1, "maximum": 100}
        }
    });
    let args = json!({
        "query": "turn_loop",
        "result_types": "[\"path\"]",
        "max_results": "10"
    });
    let normalized = normalize_tool_args(tool_names::CODE_SEARCH, &args, Some(&schema))
        .expect("code_search stringified args normalize");
    assert_eq!(normalized["result_types"], json!(["path"]));
    assert_eq!(normalized["max_results"], json!(10));
    assert_eq!(normalized["query"], json!("turn_loop"));
}

#[tokio::test]
async fn preflight_coerces_stringified_code_search_args_then_validates() {
    let (_temp, registry) = new_test_registry().await;
    // The exact malformed shape from the checkpoint should now pass preflight.
    let outcome = preflight_validate_call(
        &registry,
        tool_names::CODE_SEARCH,
        &json!({"query": "turn_loop", "result_types": "[\"path\"]", "max_results": "10"}),
    )
    .expect("stringified args should be coerced and pass preflight");
    assert_eq!(outcome.effective_args["result_types"], json!(["path"]));
    assert_eq!(outcome.effective_args["max_results"], json!(10));
}

#[tokio::test]
async fn preflight_coercion_still_enforces_bounds_and_enum() {
    let (_temp, registry) = new_test_registry().await;
    // Coerced to integer 999, then the max=100 bound must still reject it.
    let over_max = preflight_validate_call(
        &registry,
        tool_names::CODE_SEARCH,
        &json!({"query": "turn_loop", "max_results": "999"}),
    )
    .expect_err("coerced integer must still be bounds-checked");
    assert!(over_max.to_string().contains("max_results"), "msg: {over_max}");

    // Coerced to array ["bogus"], then the item enum must still reject it.
    let bad_enum = preflight_validate_call(
        &registry,
        tool_names::CODE_SEARCH,
        &json!({"query": "turn_loop", "result_types": "[\"bogus\"]"}),
    )
    .expect_err("coerced array items must still be enum-checked");
    assert!(bad_enum.to_string().contains("result_types"), "msg: {bad_enum}");
}

#[tokio::test]
async fn preflight_does_not_coerce_max_output_tokens_string() {
    // The output-budget field has a dedicated strict validator that must keep
    // rejecting strings even though schema-aware coercion exists for other
    // integer fields.
    let (_temp, registry) = new_test_registry().await;
    let error =
        preflight_validate_call(&registry, tool_names::CODE_SEARCH, &json!({"query": "x", "max_output_tokens": "100"}))
            .expect_err("max_output_tokens string must remain rejected");
    assert!(error.to_string().contains("max_output_tokens must be an integer"));
}
#[test]
fn patch_action_within_limit_is_allowed() {
    let mut failures = Vec::new();
    let args = json!({
        "action": "patch",
        "patch": "*** Begin Patch\n*** End Patch\n"
    });

    enforce_file_operation_payload_limit(tool_names::UNIFIED_FILE, &args, 1024, &mut failures);
    assert!(failures.is_empty());
}
#[test]
fn patch_action_over_limit_is_rejected() {
    let mut failures = Vec::new();
    let args = json!({
        "action": "patch",
        "patch": "x".repeat(512)
    });

    enforce_file_operation_payload_limit(tool_names::UNIFIED_FILE, &args, 128, &mut failures);
    assert_eq!(failures.len(), 1);
    assert!(failures[0].contains("payload too large"));
    assert!(failures[0].contains("Split the change"));
}
#[test]
fn edit_tool_over_limit_is_rejected() {
    let mut failures = Vec::new();
    let args = json!({
        "path": "file.txt",
        "old_str": "old",
        "new_str": "x".repeat(512)
    });

    enforce_file_operation_payload_limit(tool_names::EDIT_FILE, &args, 128, &mut failures);
    assert_eq!(failures.len(), 1);
    assert!(failures[0].contains("action='edit'"));
}

#[test]
fn read_action_is_not_limited() {
    let mut failures = Vec::new();
    let args = json!({
        "action": "read",
        "path": "README.md"
    });

    enforce_file_operation_payload_limit(tool_names::UNIFIED_FILE, &args, 1, &mut failures);
    assert!(failures.is_empty());
}

#[test]
fn edit_file_required_args_accept_legacy_key_names() {
    let args = json!({
        "path": "file.txt",
        "old_string": "old",
        "new_string": "new"
    });

    assert!(!is_missing_required_arg(tool_names::EDIT_FILE, &args, "path"));
    assert!(!is_missing_required_arg(tool_names::EDIT_FILE, &args, "old_str"));
    assert!(!is_missing_required_arg(tool_names::EDIT_FILE, &args, "new_str"));
}

#[test]
fn parse_payload_limit_accepts_safe_override() {
    let parsed = parse_file_operation_max_payload_bytes(Some("2048"));
    assert_eq!(parsed, Some(2048));
}

#[test]
fn parse_payload_limit_rejects_too_small_values() {
    let parsed = parse_file_operation_max_payload_bytes(Some("512"));
    assert_eq!(parsed, None);
}

#[test]
fn parse_payload_limit_rejects_invalid_values() {
    let parsed = parse_file_operation_max_payload_bytes(Some("not-a-number"));
    assert_eq!(parsed, None);
}

#[test]
fn configured_payload_limit_is_always_safe() {
    let configured = configured_file_operation_max_payload_bytes();
    assert!(configured >= 1024);
}

#[test]
fn apply_patch_required_arg_accepts_input_alias() {
    assert!(!is_missing_required_arg(tool_names::APPLY_PATCH, &json!({"input": ""}), "input"));
}

#[test]
fn apply_patch_required_arg_accepts_raw_string_payload() {
    assert!(!is_missing_required_arg(tool_names::APPLY_PATCH, &json!(""), "input"));
}

#[test]
fn run_pty_cmd_required_arg_accepts_zero_based_indexed_command() -> Result<()> {
    let input = json!({
        "command.0": "ls",
        "command.1": "-a"
    });
    let args = normalize_tool_args(tool_names::RUN_PTY_CMD, &input, None)?;

    assert!(!is_missing_required_arg(tool_names::RUN_PTY_CMD, args.as_ref(), "command"));
    assert_eq!(args.get("command").and_then(|value| value.as_str()), Some("ls -a"));
    Ok(())
}

#[test]
fn run_pty_cmd_required_arg_accepts_one_based_indexed_command() -> Result<()> {
    let input = json!({
        "command.1": "ls",
        "command.2": "-a"
    });
    let args = normalize_tool_args(tool_names::RUN_PTY_CMD, &input, None)?;

    assert!(!is_missing_required_arg(tool_names::RUN_PTY_CMD, args.as_ref(), "command"));
    assert_eq!(args.get("command").and_then(|value| value.as_str()), Some("ls -a"));
    Ok(())
}

#[test]
fn indexed_command_parts_require_zero_or_one_based_sequences() {
    assert_eq!(
        parse_indexed_command_parts(
            json!({
                "command.0": "ls",
                "command.1": "-a"
            })
            .as_object()
            .expect("object"),
        )
        .expect("valid indexed args"),
        Some(vec!["ls".to_string(), "-a".to_string()])
    );
    assert_eq!(
        parse_indexed_command_parts(
            json!({
                "command.1": "ls",
                "command.2": "-a"
            })
            .as_object()
            .expect("object"),
        )
        .expect("valid indexed args"),
        Some(vec!["ls".to_string(), "-a".to_string()])
    );
    assert_eq!(
        parse_indexed_command_parts(json!({"command.2": "ls"}).as_object().expect("object"))
            .expect("valid indexed args"),
        None
    );
}

#[test]
fn tool_name_candidates_extract_channel_suffix_alias() {
    let candidates = public_tool_name_candidates("assistant<|channel|>apply_patch");
    assert!(candidates.iter().any(|c| c == "apply_patch"));
}

#[test]
fn tool_name_candidates_normalize_humanized_name() {
    let candidates = public_tool_name_candidates("Read file");
    assert!(candidates.iter().any(|c| c == "read_file"));
}

#[test]
fn request_user_input_args_accept_details_alias() -> Result<()> {
    let schema = RequestUserInputTool.parameter_schema().expect("request_user_input schema");
    let args = json!({
        "questions": [{
            "id": "scope",
            "header": "Scope",
            "question": "Which direction should we take?",
            "options": [
                {
                    "label": "Minimal",
                    "details": "Ship the smallest viable slice."
                },
                {
                    "label": "Full",
                    "details": "Ship the full implementation."
                }
            ]
        }]
    });

    let normalized = normalize_tool_args(tool_names::REQUEST_USER_INPUT, &args, Some(&schema))?;
    let option = &normalized["questions"][0]["options"][0];
    assert_eq!(option.get("description").and_then(Value::as_str), Some("Ship the smallest viable slice."));
    assert!(option.get("details").is_none());
    Ok(())
}

#[test]
fn task_tracker_args_accept_details_alias() -> Result<()> {
    let schema = json!({
        "type": "object",
        "properties": {
            "action": { "type": "string" },
            "description": { "type": "string" }
        }
    });
    let args = json!({
        "action": "add",
        "details": "Add regression coverage"
    });

    let normalized = normalize_tool_args(tool_names::TASK_TRACKER, &args, Some(&schema))?;
    assert_eq!(normalized.get("description").and_then(Value::as_str), Some("Add regression coverage"));
    assert!(normalized.get("details").is_none());
    Ok(())
}

#[test]
fn details_alias_does_not_shadow_real_details_field() -> Result<()> {
    let schema = json!({
        "type": "object",
        "properties": {
            "description": { "type": "string" },
            "details": { "type": "string" }
        }
    });
    let args = json!({
        "details": "Keep the real details field."
    });

    let normalized = normalize_tool_args(tool_names::TASK_TRACKER, &args, Some(&schema))?;
    assert!(normalized.get("description").is_none());
    assert_eq!(normalized.get("details").and_then(Value::as_str), Some("Keep the real details field."));
    Ok(())
}

#[tokio::test]
async fn command_session_preflight_rejects_run_without_command() {
    let (_temp, registry) = new_test_registry().await;

    let err = preflight_validate_resolved_call(&registry, tool_names::UNIFIED_EXEC, &json!({"action": "run"}))
        .expect_err("missing command should fail preflight");

    assert!(err.to_string().contains("Missing required argument: command"));
}

#[tokio::test]
async fn exec_command_preflight_preserves_public_name_and_validates_as_run() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_call(
        &registry,
        tool_names::EXEC_COMMAND,
        &json!({"cmd": "rg --files", "workdir": ".", "tty": true}),
    )?;

    assert_eq!(result.normalized_tool_name, tool_names::EXEC_COMMAND);
    assert_eq!(result.effective_args["action"], "run");
    assert_eq!(result.effective_args["command"], "rg --files");
    assert_eq!(result.effective_args["workdir"], ".");
    assert_eq!(result.effective_args["tty"], true);
    assert!(result.readonly_classification);
    Ok(())
}

#[tokio::test]
async fn planning_preflight_rejects_mutating_suffixes_and_conflicting_raw_aliases() {
    let (_temp, registry) = new_test_registry().await;
    registry.enable_planning();
    for args in [
        json!({"cmd":"cargo check | sort", "args":["-o", "changed.txt"]}),
        json!({"cmd":"cat README.md", "raw_command":"printf changed > changed.txt"}),
    ] {
        let error = preflight_validate_call(&registry, tool_names::EXEC_COMMAND, &args)
            .expect_err("mutating invocation cannot acquire planning admission");
        assert_eq!(error.to_string(), super::agent_execution::PLANNING_DENIED_CONTEXT);
    }
    let safe =
        preflight_validate_call(&registry, tool_names::EXEC_COMMAND, &json!({"cmd":"sort", "args":["README.md"]}))
            .expect("safe appended operands remain readable in planning");
    assert!(safe.readonly_classification);
    assert!(safe.parallel_safe_after_preflight);
}

#[tokio::test]
async fn planning_preflight_allows_checkpoint_style_readonly_inspection() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;
    registry.enable_planning();
    let command = r#"sed -n '180,285p' src/main.rs; sed -n '60,285p' src/startup/mod.rs; sed -n '1,220p' src/main_helpers/bootstrap.rs; rg -n "\[profile|lto|codegen-units|strip" Cargo.toml"#;

    let result = preflight_validate_call(
        &registry,
        tool_names::EXEC_COMMAND,
        &json!({
            "cmd": command,
            "workdir": ".",
            "yield_time_ms": 10000,
            "max_output_tokens": 30000
        }),
    )?;

    assert!(result.readonly_classification);
    assert_eq!(result.effective_args["action"], "run");
    Ok(())
}

#[tokio::test]
async fn exec_command_preflight_rejects_dangerous_command() {
    let (_temp, registry) = new_test_registry().await;

    let err = preflight_validate_call(&registry, tool_names::EXEC_COMMAND, &json!({"cmd": "git reset --hard HEAD~1"}))
        .expect_err("dangerous exec_command should fail preflight");

    let text = err.to_string();
    assert!(text.contains("Tool preflight validation failed for 'exec_command'"));
    assert!(text.contains("Command security check failed"));
}

#[tokio::test]
async fn exec_command_approval_required_payload_is_seen_as_shell_run() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;
    let args = json!({
        "cmd": "cargo check",
        "sandbox_permissions": "require_escalated",
        "justification": "Need unsandboxed access for this check."
    });

    let reason = registry
        .shell_run_approval_reason(tool_names::EXEC_COMMAND, Some(&args))
        .await?;

    assert!(
        reason
            .as_deref()
            .is_some_and(|text| text.contains("without sandbox restrictions"))
    );
    Ok(())
}

#[tokio::test]
async fn direct_unsandboxed_exec_requires_operator_preapproval() {
    let (_temp, registry) = new_test_registry().await;
    let args = json!({
        "cmd": "printf guarded",
        "sandbox_permissions": "require_escalated",
        "justification": "Need unsandboxed access for this check."
    });

    let outcome = registry
        .execute_public_tool_request(ToolExecutionRequest::new(tool_names::EXEC_COMMAND, args))
        .await;
    let error = outcome.error.expect("direct escalation must be rejected");
    assert!(error.message.contains("requires an enforced operator approval decision"));
}

#[tokio::test]
async fn write_stdin_preflight_uses_session_write_validation() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_call(
        &registry,
        tool_names::WRITE_STDIN,
        &json!({
            "session_id": "run-1",
            "chars": "git reset --hard HEAD~1\n"
        }),
    )?;

    assert_eq!(result.normalized_tool_name, tool_names::WRITE_STDIN);
    assert_eq!(result.effective_args["action"], "write");
    assert_eq!(result.effective_args["input"], "git reset --hard HEAD~1\n");
    assert!(!result.readonly_classification);
    Ok(())
}

#[tokio::test]
async fn write_stdin_preflight_uses_session_poll_validation() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_call(
        &registry,
        tool_names::WRITE_STDIN,
        &json!({
            "session_id": "run-1",
            "chars": "",
            "yield_time_ms": 25,
            "max_output_tokens": 7
        }),
    )?;

    assert_eq!(result.normalized_tool_name, tool_names::WRITE_STDIN);
    assert_eq!(result.effective_args["action"], "poll");
    assert!(result.effective_args.get("input").is_none());
    assert_eq!(result.effective_args["yield_time_ms"], 25);
    assert_eq!(result.effective_args["max_output_tokens"], 7);
    assert!(result.readonly_classification);
    Ok(())
}

#[tokio::test]
async fn write_stdin_poll_preflight_rejects_non_string_session_id() {
    let (_temp, registry) = new_test_registry().await;

    let error = preflight_validate_call(&registry, tool_names::WRITE_STDIN, &json!({"session_id": 1, "chars": ""}))
        .expect_err("non-string session id should fail preflight");

    assert!(error.to_string().contains("write_stdin"));
    assert!(error.to_string().contains("Missing required argument: session_id"));
}

#[tokio::test]
async fn command_session_preflight_rejects_missing_action_without_inferable_args() {
    let (_temp, registry) = new_test_registry().await;

    let err = preflight_validate_resolved_call(&registry, tool_names::UNIFIED_EXEC, &json!({}))
        .expect_err("missing action should fail preflight");

    assert!(err.to_string().contains(&format!(
        "Invalid arguments for tool '{}': missing action; provide `action` or inferable exec arguments",
        tool_names::UNIFIED_EXEC
    )));
}

#[tokio::test]
async fn command_session_preflight_rejects_write_without_input() {
    let (_temp, registry) = new_test_registry().await;

    let err = preflight_validate_resolved_call(
        &registry,
        tool_names::UNIFIED_EXEC,
        &json!({"action": "write", "session_id": "run-1"}),
    )
    .expect_err("missing input should fail preflight");

    assert!(err.to_string().contains("Missing required argument: input or chars or text"));
}

#[tokio::test]
async fn command_session_preflight_rejects_poll_without_session_id() {
    let (_temp, registry) = new_test_registry().await;

    let err = preflight_validate_resolved_call(&registry, tool_names::UNIFIED_EXEC, &json!({"action": "poll"}))
        .expect_err("missing session_id should fail preflight");

    assert!(err.to_string().contains("Missing required argument: session_id"));
}

#[tokio::test]
async fn command_session_preflight_accepts_list_without_extra_args() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_resolved_call(&registry, tool_names::UNIFIED_EXEC, &json!({"action": "list"}))?;

    assert_eq!(result.normalized_tool_name, tool_names::UNIFIED_EXEC);
    Ok(())
}

#[tokio::test]
async fn command_session_preflight_accepts_inspect_with_spool_path() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_resolved_call(
        &registry,
        tool_names::UNIFIED_EXEC,
        &json!({"action": "inspect", "spool_path": ".vtcode/context/tool_outputs/out.log"}),
    )?;

    assert_eq!(result.normalized_tool_name, tool_names::UNIFIED_EXEC);
    Ok(())
}

#[tokio::test]
async fn file_operation_command_payload_preflight_remaps_to_command_session() -> Result<()> {
    let (_temp, registry) = new_test_registry().await;

    let result = preflight_validate_resolved_call(
        &registry,
        tool_names::UNIFIED_FILE,
        &json!({
            "command": "echo vtcode",
            "cwd": ".",
        }),
    )?;

    assert_eq!(result.normalized_tool_name, tool_names::UNIFIED_EXEC);
    assert_eq!(result.effective_args["action"], "run");
    assert_eq!(result.effective_args["command"], "echo vtcode");
    assert_eq!(result.effective_args["cwd"], ".");
    Ok(())
}