vtcode-core 0.181.7

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
use super::*;

#[test]
fn build_child_config_does_not_inherit_coordinator_role_or_remove_discovery_permissions() {
    let mut parent = VTCodeConfig {
        default_primary_agent: "coordinator".into(),
        ..Default::default()
    };
    parent.permissions.allow = vec![tools::READ_FILE.into(), tools::CODE_SEARCH.into()];
    parent.permissions.deny = vec![tools::APPLY_PATCH.into()];
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.matches_name("explore"))
        .unwrap();

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);

    assert_eq!(child.default_primary_agent, "build");
    assert_eq!(child.permissions.allow, parent.permissions.allow);
    assert!(child.permissions.deny.contains(&tools::APPLY_PATCH.into()));
    assert!(child.permissions.deny.contains(&tools::SPAWN_AGENT.into()));
    assert_eq!(child.runtime_agent_permissions.as_ref(), Some(&spec.permissions));
    assert_eq!(parent.default_primary_agent, "coordinator");
}
#[tokio::test]
async fn subagent_restart_at_cap_waits_for_aborted_launch_before_restarting() {
    for interrupt in [false, true] {
        let root = TempDir::new().unwrap();
        let mut cfg = VTCodeConfig::default();
        cfg.subagents.max_concurrent = 1;
        let controller = SubagentController::new(test_controller_config(root.path().to_path_buf(), cfg))
            .await
            .unwrap();
        let spec = vtcode_config::builtin_subagents()
            .into_iter()
            .find(|spec| spec.name == "default")
            .unwrap();
        let mut child =
            test_child_record("existing", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
        let permit = controller.admission.clone().try_acquire_owned().unwrap();
        let (started, receiver) = tokio::sync::oneshot::channel();
        child.handle = Some(tokio::spawn(async move {
            let _permit = permit;
            started.send(()).unwrap();
            std::future::pending::<()>().await;
        }));
        controller.state.write().await.children.insert("existing".into(), child);
        receiver.await.unwrap();
        assert_eq!(controller.admission.available_permits(), 0);

        for message in ["Inspect source.rs", "Also inspect the second input"] {
            let waiting = controller
                .send_input(SendInputRequest {
                    target: "existing".into(),
                    message: Some(message.into()),
                    items: vec![],
                    interrupt: false,
                })
                .await
                .unwrap();
            assert_eq!(waiting.status, SubagentStatus::Waiting);
            assert_eq!(controller.admission.available_permits(), 0);
        }

        let resumed = tokio::time::timeout(Duration::from_millis(100), controller.resume("existing"))
            .await
            .expect("resuming a waiting child must not wait for its active launch")
            .unwrap();
        assert_eq!(resumed.status, SubagentStatus::Waiting);
        assert_eq!(controller.state.read().await.children["existing"].queued_prompts.len(), 2);
        assert_eq!(controller.admission.available_permits(), 0);

        let restarted = if interrupt {
            controller
                .send_input(SendInputRequest {
                    target: "existing".into(),
                    message: Some("Inspect source.rs".into()),
                    items: vec![],
                    interrupt: true,
                })
                .await
        } else {
            controller.close("existing").await.unwrap();
            assert_eq!(controller.admission.available_permits(), 1, "close must finish releasing the owned launch");
            controller.resume("existing").await
        };
        // Stop the replacement before it can execute provider work.
        controller.close("existing").await.unwrap();
        assert!(
            restarted.is_ok(),
            "a stopped child must be able to reuse its launch slot: interrupt={interrupt}, {restarted:?}"
        );
        tokio::task::yield_now().await;
        assert_eq!(controller.admission.available_permits(), 1);
        controller.signal_shutdown().await;
    }
}
#[tokio::test]
async fn subagent_restart_at_cap_failure_keeps_one_prompt_and_allows_later_resume() {
    let root = TempDir::new().unwrap();
    let mut cfg = VTCodeConfig::default();
    cfg.subagents.max_concurrent = 1;
    let controller = SubagentController::new(test_controller_config(root.path().to_path_buf(), cfg))
        .await
        .unwrap();
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "default")
        .unwrap();
    let mut child =
        test_child_record("existing", "child-session", "parent-session", &spec, SubagentStatus::Completed, 1, None);
    child.last_prompt = Some("Inspect source.rs".into());
    controller.state.write().await.children.insert("existing".into(), child);
    let occupied = controller.admission.clone().try_acquire_owned().unwrap();

    assert!(
        controller
            .resume("existing")
            .await
            .unwrap_err()
            .to_string()
            .contains("concurrency limit")
    );
    {
        let state = controller.state.read().await;
        let child = &state.children["existing"];
        assert_eq!(child.status, SubagentStatus::Failed, "failed admission must not leave a phantom queued launch");
        assert_eq!(child.queued_prompts.len(), 1);
        assert!(child.handle.is_none());
        assert!(child.error.as_deref().unwrap().contains("concurrency limit"));
    }
    drop(occupied);

    controller.resume("existing").await.unwrap();
    assert_eq!(controller.admission.available_permits(), 0);
    assert_eq!(controller.state.read().await.children["existing"].queued_prompts.len(), 1);
    controller.close("existing").await.unwrap();
    assert_eq!(controller.admission.available_permits(), 1);
    controller.signal_shutdown().await;
}
#[tokio::test]
async fn matrix_active_admission_rejects_child_resume_and_input_without_mutation() {
    let root = TempDir::new().unwrap();
    let controller =
        SubagentController::new(test_controller_config(root.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .unwrap();
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "default")
        .unwrap();
    controller.state.write().await.children.insert(
        "existing".into(),
        test_child_record("existing", "child-session", "parent-session", &spec, SubagentStatus::Completed, 1, None),
    );
    controller.matrix.executing.store(true, Ordering::Release);
    for result in [
        controller.resume("existing").await,
        controller
            .send_input(SendInputRequest {
                target: "existing".into(),
                message: Some("Inspect source.rs".into()),
                items: vec![],
                interrupt: false,
            })
            .await,
    ] {
        assert!(result.unwrap_err().to_string().contains("scheduler-owned workers"));
    }
    assert!(
        controller
            .launch_child("existing")
            .await
            .unwrap_err()
            .to_string()
            .contains("scheduler-owned workers")
    );
    let state = controller.state.read().await;
    let child = &state.children["existing"];
    assert_eq!(child.status, SubagentStatus::Completed);
    assert!(child.queued_prompts.is_empty());
    assert!(child.handle.is_none());
    assert_eq!(controller.admission.available_permits(), 3);
}
#[test]
fn build_child_config_intersects_allowed_tools_and_preserves_global_denies() {
    let mut parent = VTCodeConfig::default();
    parent.permissions.allow = vec![tools::READ_FILE.to_string(), tools::CODE_SEARCH.to_string()];
    parent.permissions.deny = vec![tools::UNIFIED_EXEC.to_string()];

    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.permissions = AgentPermissionsConfig {
        auto: vec!["Bash(*)".to_string()],
        ..AgentPermissionsConfig::new(PermissionDefault::Auto)
    };
    spec.tools = Some(vec![
        tools::SPAWN_AGENT.to_string(),
        tools::CODE_SEARCH.to_string(),
        tools::READ_FILE.to_string(),
    ]);

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);
    assert_eq!(child.runtime_agent_permissions.as_ref(), Some(&spec.permissions));
    assert_eq!(child.permissions.allow, vec![tools::READ_FILE.to_string(), tools::CODE_SEARCH.to_string()]);
    assert!(child.permissions.deny.contains(&tools::UNIFIED_EXEC.to_string()));
    assert!(child.permissions.deny.contains(&tools::SPAWN_AGENT.to_string()));
}
#[test]
fn build_child_config_allows_nested_delegation_keeps_agent_tools_out_of_deny() {
    let mut parent = VTCodeConfig::default();
    parent.permissions.allow = vec![
        tools::SPAWN_AGENT.to_string(),
        tools::WAIT_AGENT.to_string(),
        tools::CODE_SEARCH.to_string(),
    ];

    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.tools = Some(parent.permissions.allow.clone());

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, true);

    assert_eq!(
        child.permissions.allow,
        vec![
            tools::SPAWN_AGENT.to_string(),
            tools::WAIT_AGENT.to_string(),
            tools::CODE_SEARCH.to_string()
        ],
        "nested delegation keeps delegation tools in the allow-list"
    );
    assert!(
        !child.permissions.deny.contains(&tools::SPAWN_AGENT.to_string()),
        "spawn_agent must not be denied when nested delegation is allowed"
    );
    assert!(
        !child.permissions.deny.contains(&tools::AGENT.to_string()),
        "agent must not be denied when nested delegation is allowed"
    );
    assert!(
        child.permissions.deny.contains(&tools::SPAWN_BACKGROUND_SUBPROCESS.to_string()),
        "background subprocess alias stays blocked for children"
    );
}
#[test]
fn build_child_config_default_denies_all_subagent_tools() {
    let parent = VTCodeConfig::default();
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);

    assert!(child.permissions.deny.contains(&tools::SPAWN_AGENT.to_string()));
    assert!(child.permissions.deny.contains(&tools::AGENT.to_string()));
    assert!(child.permissions.deny.contains(&tools::SEND_INPUT.to_string()));
    assert!(child.permissions.deny.contains(&tools::WAIT_AGENT.to_string()));
    assert!(child.permissions.deny.contains(&tools::RESUME_AGENT.to_string()));
    assert!(child.permissions.deny.contains(&tools::CLOSE_AGENT.to_string()));
    assert!(child.permissions.deny.contains(&tools::SPAWN_BACKGROUND_SUBPROCESS.to_string()));
}
#[test]
fn build_child_config_preserves_subagent_lifecycle_stripping_and_hook_merging() {
    let mut parent = VTCodeConfig::default();
    parent.permissions.allow = vec![
        tools::SPAWN_AGENT.to_string(),
        tools::CODE_SEARCH.to_string(),
        tools::UNIFIED_EXEC.to_string(),
    ];

    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.tools = Some(parent.permissions.allow.clone());
    let mut hooks = HooksConfig::default();
    hooks.lifecycle.pre_tool_use.push(HookGroupConfig {
        matcher: Some("*".to_string()),
        hooks: vec![HookCommandConfig {
            command: "echo child".to_string(),
            ..HookCommandConfig::default()
        }],
    });
    spec.hooks = Some(hooks);

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);

    assert_eq!(child.permissions.allow, vec![tools::CODE_SEARCH.to_string(), tools::UNIFIED_EXEC.to_string()]);
    assert!(child.permissions.deny.contains(&tools::SPAWN_AGENT.to_string()));
    assert_eq!(child.hooks.lifecycle.pre_tool_use.len(), 1);
    assert_eq!(child.hooks.lifecycle.pre_tool_use[0].hooks[0].command, "echo child");
}
#[test]
fn prepare_child_runtime_config_uses_shared_view_for_model_and_reasoning() {
    let parent = VTCodeConfig::default();
    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.model = Some(models::openai::GPT_5_6_LUNA.to_string());
    spec.reasoning_effort = Some(ReasoningEffortLevel::High);

    let (resolved_model, child_reasoning_effort, child_cfg) = prepare_child_runtime_config(
        &parent,
        &spec,
        models::openai::GPT_5_6_SOL,
        "openai",
        ReasoningEffortLevel::Low,
        None,
        None,
        None,
        false,
        |_, parent_model, parent_provider, model_override, spec_model, agent_name| {
            assert_eq!(parent_model, models::openai::GPT_5_6_SOL);
            assert_eq!(parent_provider, "openai");
            assert_eq!(model_override, None);
            assert_eq!(spec_model, Some(models::openai::GPT_5_6_LUNA));
            assert_eq!(agent_name, "worker");
            Ok(models::openai::GPT_5_6_LUNA.parse::<ModelId>().expect("valid model"))
        },
    )
    .expect("prepared child runtime config");

    assert_eq!(resolved_model.as_str(), models::openai::GPT_5_6_LUNA);
    assert_eq!(child_cfg.agent.default_model, models::openai::GPT_5_6_LUNA);
    assert_eq!(child_reasoning_effort, ReasoningEffortLevel::High);
    assert_eq!(child_cfg.agent.reasoning_effort, ReasoningEffortLevel::High);
}
#[test]
fn subagent_instruction_composition_uses_shared_runtime_prompt_and_skill_appendix() {
    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.prompt = "Worker instructions".to_string();
    spec.skills = vec!["rust".to_string(), "repo".to_string()];

    let instructions = compose_subagent_instructions(&spec, Some("Memory appendix".to_string()));

    assert!(instructions.contains("Worker instructions"));
    assert!(instructions.contains("Preloaded skill names: rust, repo."));
    assert!(instructions.contains("Memory appendix"));
    assert!(instructions.contains("Return your final response using this exact Markdown contract"));
    // Writable children state the loop detector's real subagent limits.
    assert!(instructions.contains(&format!(
        "{} read-only calls in total",
        crate::core::loop_detector::SUBAGENT_MAX_TOTAL_READONLY_CALLS
    )));
    assert!(instructions.contains(&format!(
        "{} consecutive reads/searches",
        crate::core::loop_detector::SUBAGENT_NAVIGATION_HARD_STOP_STREAK
    )));
    assert!(!instructions.contains("CRITICAL"));
}
#[test]
fn final_response_contract_yields_to_agent_defined_format() {
    // The verifier's own format ends with a `Decision:` line the harness
    // parses; the generic contract must not compete with it.
    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");
    spec.name = "verifier".to_string();
    spec.prompt = "End with exactly one line, `Decision: APPROVED` or `Decision: REJECTED`.".to_string();

    let instructions = compose_subagent_instructions(&spec, None);

    assert!(instructions.contains(
        "If your agent instructions or the task define their own response format, follow that format instead."
    ));
}
#[test]
fn build_child_config_preserves_matching_rule_and_exact_tool_ids() {
    let mut parent = VTCodeConfig::default();
    parent.permissions.allow = vec![
        "Read(/docs/**)".to_string(),
        "mcp::context7::search".to_string(),
        tools::READ_FILE.to_string(),
    ];

    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.tools = Some(vec![
        "mcp::context7::search".to_string(),
        tools::UNIFIED_EXEC.to_string(),
        tools::READ_FILE.to_string(),
    ]);

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);

    assert_eq!(
        child.permissions.allow,
        vec![
            "Read(/docs/**)".to_string(),
            "mcp::context7::search".to_string(),
            tools::READ_FILE.to_string()
        ]
    );
}
#[test]
fn build_child_config_preserves_parent_rule_shaped_allowlist() {
    let mut parent = VTCodeConfig::default();
    parent.permissions.allow = vec!["Read".to_string()];

    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");
    spec.tools = Some(vec![
        tools::READ_FILE.to_string(),
        tools::CODE_SEARCH.to_string(),
        tools::UNIFIED_EXEC.to_string(),
    ]);

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);

    assert_eq!(child.permissions.allow, vec!["Read".to_string()]);
}
#[test]
fn build_child_config_promotes_single_turn_budget_to_recovery_budget() {
    let parent = VTCodeConfig::default();
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "worker")
        .expect("worker");

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, Some(1), false);

    assert_eq!(child.automation.full_auto.max_turns, SUBAGENT_MIN_MAX_TURNS);
}
#[test]
fn background_children_get_a_higher_turn_floor() {
    assert_eq!(normalize_background_child_max_turns(Some(2), true), Some(4));
    assert_eq!(normalize_background_child_max_turns(Some(3), true), Some(4));
    assert_eq!(normalize_background_child_max_turns(Some(4), true), Some(4));
}
#[test]
fn foreground_children_keep_the_existing_turn_floor() {
    assert_eq!(normalize_background_child_max_turns(Some(1), false), Some(SUBAGENT_MIN_MAX_TURNS));
    assert_eq!(normalize_background_child_max_turns(Some(2), false), Some(2));
    assert_eq!(normalize_background_child_max_turns(None, true), None);
}
#[test]
fn build_child_config_merges_inline_mcp_provider() {
    let parent = VTCodeConfig::default();
    let mut spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "default")
        .expect("default");
    spec.mcp_servers = vec![SubagentMcpServer::Inline(BTreeMap::from([(
        "playwright".to_string(),
        serde_json::json!({
            "type": "stdio",
            "command": "npx",
            "args": ["-y", "@playwright/mcp@latest"],
        }),
    )]))];

    let child = build_child_config(&parent, &spec, models::openai::GPT_5_6_SOL, None, false);
    let provider = child
        .mcp
        .providers
        .iter()
        .find(|provider| provider.name == "playwright")
        .expect("playwright provider");
    assert_eq!(provider.name, "playwright");
}
#[test]
fn explicit_delegation_request_detects_mentions_and_keywords() {
    let direct_mentions = extract_explicit_agent_mentions("@agent-worker fix the issue", &[]);
    assert!(contains_explicit_delegation_request("@agent-worker fix the issue", direct_mentions.as_slice()));
    let no_mentions = extract_explicit_agent_mentions("delegate this in parallel", &[]);
    assert!(contains_explicit_delegation_request("delegate this in parallel", no_mentions.as_slice()));
    let empty_mentions = extract_explicit_agent_mentions("review the repository", &[]);
    assert!(!contains_explicit_delegation_request("review the repository", empty_mentions.as_slice()));
}
#[test]
fn explicit_agent_mentions_detect_natural_language_selection() {
    let rust_engineer = read_only_test_spec("rust-engineer");
    assert_eq!(
        extract_explicit_agent_mentions("use rust-engineer agent to review current code", &[rust_engineer]),
        vec!["rust-engineer".to_string()]
    );
}
#[test]
fn explicit_agent_mentions_detect_looser_subagent_selection() {
    let background_demo = read_only_test_spec("background-demo");
    assert_eq!(
        extract_explicit_agent_mentions("use background-demo and run the subagent", &[background_demo]),
        vec!["background-demo".to_string()]
    );
}
#[test]
fn explicit_agent_mentions_detect_run_subagent_selection() {
    let rust_engineer = read_only_test_spec("rust-engineer");
    assert_eq!(
        extract_explicit_agent_mentions("run rust-engineer subagent and review changes", &[rust_engineer]),
        vec!["rust-engineer".to_string()]
    );
}
#[test]
fn explicit_agent_mentions_ignore_primary_only_agents() {
    let mut duck = read_only_test_spec("duck");
    duck.mode = vtcode_config::AgentMode::Primary;

    assert_eq!(
        extract_explicit_agent_mentions("@agent-duck discuss the task", &[duck.clone()]),
        Vec::<String>::new()
    );
    assert_eq!(
        extract_explicit_agent_mentions("run duck agent and discuss the task", &[duck]),
        Vec::<String>::new()
    );
}
#[test]
fn explicit_model_request_detects_aliases_and_full_ids() {
    assert!(contains_explicit_model_request("delegate this using gpt-5.6-luna", "gpt-5.6-luna"));
    assert!(contains_explicit_model_request("use the worker subagent with haiku", "haiku"));
    assert!(contains_explicit_model_request("run this with the small model", "small"));
    assert!(!contains_explicit_model_request("delegate this small cleanup task", "small"));
    assert!(!contains_explicit_model_request("delegate this task", "gpt-5.6-luna"));
}
#[test]
fn normalize_requested_model_override_drops_default_like_values() {
    assert_eq!(normalize_requested_model_override(Some("default".to_string()), "delegate this task"), None);
    assert_eq!(normalize_requested_model_override(Some(" inherit ".to_string()), "delegate this task"), None);
    assert_eq!(
        normalize_requested_model_override(Some(" inherit ".to_string()), "delegate this task using inherit"),
        Some("inherit".to_string())
    );
}
#[test]
fn sanitize_subagent_input_items_drops_empty_fields() {
    let mut items = vec![
        SubagentInputItem {
            item_type: Some("text".to_string()),
            text: Some("  Workspace: /tmp/repo  ".to_string()),
            path: Some(String::new()),
            name: Some(" ".to_string()),
            image_url: None,
        },
        SubagentInputItem {
            item_type: Some("text".to_string()),
            text: Some("   ".to_string()),
            path: Some(String::new()),
            name: None,
            image_url: None,
        },
    ];

    sanitize_subagent_input_items(&mut items);

    assert_eq!(items.len(), 1);
    assert_eq!(items[0].text.as_deref(), Some("Workspace: /tmp/repo"));
    assert!(items[0].path.is_none());
    assert!(items[0].name.is_none());
}
#[test]
fn subagent_input_item_reads_schema_type_field() {
    let item: SubagentInputItem =
        serde_json::from_value(serde_json::json!({"type": "path", "path": "src/lib.rs"})).expect("item");
    assert_eq!(item.item_type.as_deref(), Some("path"));
    assert_eq!(serde_json::to_value(&item).expect("serialize")["type"], serde_json::json!("path"));

    let legacy: SubagentInputItem =
        serde_json::from_value(serde_json::json!({"item_type": "text", "text": "x"})).expect("legacy item");
    assert_eq!(legacy.item_type.as_deref(), Some("text"));
}
#[test]
fn agent_schema_reasoning_effort_enum_matches_parser() {
    let schema = vtcode_utility_tool_specs::agent_parameters();
    let listed = schema["properties"]["reasoning_effort"]["enum"]
        .as_array()
        .expect("enum")
        .iter()
        .map(|value| value.as_str().expect("string enum value").to_string())
        .collect::<Vec<_>>();
    // Schema -> parser: every advertised value parses back to itself.
    for value in &listed {
        let parsed = ReasoningEffortLevel::parse(value).unwrap_or_else(|| panic!("schema value {value} must parse"));
        assert_eq!(parsed.as_str(), value);
    }
    // Parser -> schema: every named level is advertised. The exhaustive match
    // makes a new variant fail to compile here until it is listed.
    let named = [
        ReasoningEffortLevel::None,
        ReasoningEffortLevel::Minimal,
        ReasoningEffortLevel::Low,
        ReasoningEffortLevel::Medium,
        ReasoningEffortLevel::High,
        ReasoningEffortLevel::XHigh,
        ReasoningEffortLevel::Max,
    ];
    for level in named {
        match level {
            ReasoningEffortLevel::None
            | ReasoningEffortLevel::Minimal
            | ReasoningEffortLevel::Low
            | ReasoningEffortLevel::Medium
            | ReasoningEffortLevel::High
            | ReasoningEffortLevel::XHigh
            | ReasoningEffortLevel::Max
            | ReasoningEffortLevel::Unknown => {}
        }
        assert!(listed.iter().any(|value| value == level.as_str()), "{level} missing from schema enum");
    }
    assert_eq!(listed.len(), named.len());
}