vtcode-core 0.185.0

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

#[tokio::test]
async fn close_reports_nested_child_failure() {
    let temp = TempDir::new().expect("tempdir");
    let parent = SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
        .await
        .unwrap();
    let nested = Arc::new(
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .unwrap(),
    );
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .unwrap();
    let handle = tokio::spawn(async { panic!("nested launch failed") });
    while !handle.is_finished() {
        tokio::task::yield_now().await;
    }
    let mut grandchild =
        test_child_record("grandchild", "grandchild-session", "child-session", &spec, SubagentStatus::Running, 2, None);
    grandchild.handle = Some(handle);
    nested.state.write().await.children.insert("grandchild".into(), grandchild);
    let mut child =
        test_child_record("child", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
    child.child_controller = Some(nested);
    parent.state.write().await.children.insert("child".into(), child);
    let error = parent.close("child").await.unwrap_err();
    assert!(format!("{error:#}").contains("grandchild"));
    assert!(parent.resume("child").await.unwrap_err().to_string().contains("closing"));
    parent.close("child").await.unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn close_retains_child_handle_after_timeout() {
    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");
    let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let handle = tokio::spawn(async move {
        let _ = entered_tx.send(());
        let _ = release_rx.recv();
        std::future::pending::<()>().await;
    });
    entered_rx.await.expect("child entered");
    let mut record =
        test_child_record("child", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
    record.handle = Some(handle);
    controller.state.write().await.children.insert("child".into(), record);

    assert!(
        tokio::time::timeout(Duration::from_millis(250), controller.close("child"))
            .await
            .is_err()
    );
    assert_eq!(controller.status_for("child").await.unwrap().status, SubagentStatus::Running);
    assert!(controller.shutdown_handles.lock().await.contains_key("child"));
    assert!(controller.state.read().await.children["child"].closing);
    let error = controller
        .send_input(SendInputRequest {
            target: "child".into(),
            message: Some("new work".into()),
            items: vec![],
            interrupt: true,
        })
        .await
        .unwrap_err();
    assert!(error.to_string().contains("closed"));
    controller.state.write().await.children.get_mut("child").unwrap().status = SubagentStatus::Completed;
    assert!(controller.resume("child").await.unwrap_err().to_string().contains("closing"));
    release_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), controller.close("child"))
        .await
        .expect("retry finishes")
        .unwrap();
    assert_eq!(controller.status_for("child").await.unwrap().status, SubagentStatus::Closed);
    assert!(controller.shutdown_handles.lock().await.is_empty());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn cancelled_restart_keeps_the_previous_launch_owned() {
    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");
    let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let handle = tokio::spawn(async move {
        let _ = entered_tx.send(());
        let _ = release_rx.recv();
        std::future::pending::<()>().await;
    });
    entered_rx.await.expect("child entered");
    let mut record =
        test_child_record("child", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
    record.handle = Some(handle);
    controller.state.write().await.children.insert("child".into(), record);
    let request = SendInputRequest {
        target: "child".into(),
        message: Some("new work".into()),
        items: vec![],
        interrupt: true,
    };
    assert!(
        tokio::time::timeout(Duration::from_millis(250), controller.send_input(request))
            .await
            .is_err()
    );
    assert!(controller.shutdown_handles.lock().await.contains_key("child"));
    assert!(controller.state.read().await.children["child"].handle.is_none());
    release_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), controller.close("child"))
        .await
        .expect("close finishes")
        .unwrap();
    assert_eq!(controller.status_for("child").await.unwrap().status, SubagentStatus::Closed);
    assert!(controller.shutdown_handles.lock().await.is_empty());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn shutdown_retains_and_waits_for_an_aborted_child_after_timeout() {
    struct DropSignal(Arc<AtomicBool>);
    impl Drop for DropSignal {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");
    let dropped = Arc::new(AtomicBool::new(false));
    let child_dropped = dropped.clone();
    let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let handle = tokio::spawn(async move {
        let _drop_signal = DropSignal(child_dropped);
        let _ = entered_tx.send(());
        let _ = release_rx.recv();
        std::future::pending::<()>().await;
    });
    entered_rx.await.expect("child entered its non-yielding section");
    let mut record =
        test_child_record("child", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
    record.handle = Some(handle);
    controller.state.write().await.children.insert("child".into(), record);

    assert!(
        tokio::time::timeout(Duration::from_millis(250), controller.signal_shutdown())
            .await
            .is_err()
    );
    assert!(!dropped.load(Ordering::SeqCst));
    assert_eq!(controller.shutdown_handles.lock().await.len(), 1, "the timed-out child remains owned");
    assert_eq!(controller.status_for("child").await.unwrap().status, SubagentStatus::Running);
    release_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), controller.signal_shutdown())
        .await
        .expect("retry observes child exit");
    assert!(dropped.load(Ordering::SeqCst));
    assert!(controller.shutdown_handles.lock().await.is_empty());
    assert_eq!(controller.status_for("child").await.unwrap().status, SubagentStatus::Closed);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn monitor_shutdown_keeps_the_handle_when_wait_is_cancelled() {
    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");
    controller.stop_background_completion_monitor().await;
    let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let monitor = tokio::spawn(async move {
        let _ = entered_tx.send(());
        let _ = release_rx.recv();
        std::future::pending::<()>().await;
    });
    entered_rx.await.expect("monitor entered its non-yielding section");
    *controller.background_completion_monitor.lock().await = Some(monitor);

    assert!(
        tokio::time::timeout(Duration::from_millis(250), controller.stop_background_completion_monitor())
            .await
            .is_err()
    );
    assert!(controller.background_completion_monitor.lock().await.is_some());
    release_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), controller.stop_background_completion_monitor())
        .await
        .expect("retry joins the monitor");
    assert!(controller.background_completion_monitor.lock().await.is_none());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn shutdown_closes_a_controller_attached_before_the_child_exits() {
    let temp = TempDir::new().expect("tempdir");
    let parent = Arc::new(
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("parent controller"),
    );
    let nested = Arc::new(
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("nested controller"),
    );
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");
    nested.state.write().await.children.insert(
        "grandchild".into(),
        test_child_record("grandchild", "grandchild-session", "child-session", &spec, SubagentStatus::Running, 2, None),
    );
    let (entered_tx, entered_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let child = tokio::spawn(async move {
        let _ = entered_tx.send(());
        let _ = release_rx.recv();
        std::future::pending::<()>().await;
    });
    entered_rx.await.unwrap();
    let mut record =
        test_child_record("child", "child-session", "parent-session", &spec, SubagentStatus::Running, 1, None);
    record.handle = Some(child);
    parent.state.write().await.children.insert("child".into(), record);
    let shutdown_parent = parent.clone();
    let shutdown = tokio::spawn(async move { shutdown_parent.signal_shutdown().await });
    tokio::time::timeout(Duration::from_secs(2), async {
        loop {
            if parent
                .state
                .read()
                .await
                .children
                .get("child")
                .is_some_and(|record| record.handle.is_none())
            {
                return;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("shutdown requested child abort");
    parent.state.write().await.children.get_mut("child").unwrap().child_controller = Some(nested.clone());
    release_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), shutdown).await.unwrap().unwrap();
    assert_eq!(nested.status_for("grandchild").await.unwrap().status, SubagentStatus::Closed);
    assert_eq!(parent.status_for("child").await.unwrap().status, SubagentStatus::Closed);
}

#[tokio::test]
async fn spawn_is_rejected_after_close_begins() {
    let temp = TempDir::new().expect("tempdir");
    let mut vt_cfg = VTCodeConfig::default();
    vt_cfg.subagents.max_depth = 2;
    let mut child_config = test_controller_config(temp.path().to_path_buf(), vt_cfg);
    child_config.depth = 1;
    let child_controller = SubagentController::new(child_config).await.expect("child controller");
    child_controller
        .set_turn_delegation_hints_from_input("delegate this task")
        .await;

    // Mark the controller as closing: this is exactly what `close_tree` does
    // to a child-scoped controller before aborting its subtree, so a still-
    // running child cannot spawn a grandchild after the descendant snapshot.
    child_controller.begin_close().await;

    let err = child_controller
        .spawn(SpawnAgentRequest {
            agent_type: Some("explorer".to_string()),
            message: Some("Inspect the codebase.".to_string()),
            ..SpawnAgentRequest::default()
        })
        .await
        .expect_err("spawn after close began must be rejected");

    assert!(err.to_string().contains("shutting down"));

    // Reopening the subtree clears the transient close flag, so a resumed
    // child can delegate again. Permanent `shutdown_requested` stays set only
    // for real shutdown, not for a subtree close.
    child_controller.end_close().await;
    child_controller
        .spawn(SpawnAgentRequest {
            agent_type: Some("explorer".to_string()),
            message: Some("Inspect the codebase.".to_string()),
            ..SpawnAgentRequest::default()
        })
        .await
        .expect("spawn after end_close must be allowed again");

    // Abort the queued child task so it cannot run provider work after the
    // temp workspace is removed.
    child_controller.signal_shutdown().await;
}
#[tokio::test]
async fn resume_rejected_after_shutdown() {
    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");

    controller.signal_shutdown().await;

    let err = controller
        .resume("some-agent")
        .await
        .expect_err("resume after shutdown must be rejected");

    assert!(err.to_string().contains("shutting down"));
}
#[tokio::test]
async fn resume_restores_closed_grandchildren() {
    let temp = TempDir::new().expect("tempdir");
    let parent = SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
        .await
        .expect("parent controller");

    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");

    // Child-scoped controller that has a grandchild under the child session id.
    let child_controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("child controller");
    let child_session_id = "child-session".to_string();
    {
        let mut state = child_controller.state.write().await;
        state.children.insert(
            "grandchild".to_string(),
            test_child_record(
                "grandchild",
                "session-grandchild",
                &child_session_id,
                &spec,
                SubagentStatus::Running,
                2,
                None,
            ),
        );
    }
    let child_controller = Arc::new(child_controller);
    {
        let mut state = parent.state.write().await;
        state.children.insert(
            "child".to_string(),
            test_child_record(
                "child",
                &child_session_id,
                "parent-session",
                &spec,
                SubagentStatus::Running,
                1,
                Some(child_controller.clone()),
            ),
        );
    }

    // Closing the child cascades to the grandchild...
    parent.close("child").await.expect("close child");
    let closed_grandchild = child_controller.status_for("grandchild").await.expect("grandchild status");
    assert_eq!(closed_grandchild.status, SubagentStatus::Closed, "grandchild must be closed with the child");

    // ...and resuming the child must reopen the grandchild too, not just clear
    // the close gate.
    parent.resume("child").await.expect("resume child");
    let resumed_grandchild = child_controller.status_for("grandchild").await.expect("grandchild status");
    assert!(
        matches!(resumed_grandchild.status, SubagentStatus::Queued | SubagentStatus::Running),
        "resumed grandchild must be re-queued, got {:?}",
        resumed_grandchild.status
    );

    // Abort restarted child tasks so they cannot run provider work after the
    // temp workspace is removed.
    parent.signal_shutdown().await;
}
#[tokio::test]
async fn resume_does_not_affect_sibling_grandchildren() {
    let temp = TempDir::new().expect("tempdir");
    let parent = SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
        .await
        .expect("parent controller");

    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "explorer")
        .expect("explorer");

    // Two siblings each with their own child-scoped controller and a grandchild.
    let controller_a =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller a");
    {
        let mut state = controller_a.state.write().await;
        state.children.insert(
            "a-grandchild".to_string(),
            test_child_record(
                "a-grandchild",
                "session-a-grandchild",
                "session-a",
                &spec,
                SubagentStatus::Running,
                2,
                None,
            ),
        );
    }
    let controller_b =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller b");
    {
        let mut state = controller_b.state.write().await;
        state.children.insert(
            "b-grandchild".to_string(),
            test_child_record(
                "b-grandchild",
                "session-b-grandchild",
                "session-b",
                &spec,
                SubagentStatus::Running,
                2,
                None,
            ),
        );
    }
    let controller_a = Arc::new(controller_a);
    let controller_b = Arc::new(controller_b);
    {
        let mut state = parent.state.write().await;
        state.children.insert(
            "a".to_string(),
            test_child_record(
                "a",
                "session-a",
                "parent-session",
                &spec,
                SubagentStatus::Running,
                1,
                Some(controller_a.clone()),
            ),
        );
        state.children.insert(
            "b".to_string(),
            test_child_record(
                "b",
                "session-b",
                "parent-session",
                &spec,
                SubagentStatus::Running,
                1,
                Some(controller_b.clone()),
            ),
        );
    }

    // Close both siblings' grandchildren, then resume only "a": "b"'s
    // grandchild must stay closed (resume_tree must respect subtree isolation).
    parent.close("a").await.expect("close a");
    parent.close("b").await.expect("close b");

    parent.resume("a").await.expect("resume a");

    let a_grandchild = controller_a.status_for("a-grandchild").await.expect("a-grandchild status");
    assert!(
        matches!(a_grandchild.status, SubagentStatus::Queued | SubagentStatus::Running),
        "resumed 'a''s grandchild must be re-queued, got {:?}",
        a_grandchild.status
    );
    let b_grandchild = controller_b.status_for("b-grandchild").await.expect("b-grandchild status");
    assert_eq!(
        b_grandchild.status,
        SubagentStatus::Closed,
        "resuming sibling 'a' must not reopen sibling 'b''s grandchildren"
    );

    // Abort restarted child tasks so they cannot run provider work after the
    // temp workspace is removed.
    parent.signal_shutdown().await;
}
#[tokio::test]
async fn wait_returns_first_terminal_child() {
    let temp = TempDir::new().expect("tempdir");
    let controller =
        SubagentController::new(test_controller_config(temp.path().to_path_buf(), VTCodeConfig::default()))
            .await
            .expect("controller");
    let spec = vtcode_config::builtin_subagents()
        .into_iter()
        .find(|spec| spec.name == "default")
        .expect("default");

    {
        let mut state = controller.state.write().await;
        for id in ["first", "second"] {
            state.children.insert(
                id.to_string(),
                ChildRecord {
                    id: id.to_string(),
                    session_id: format!("session-{id}"),
                    parent_thread_id: "parent-session".to_string(),
                    spec: spec.clone(),
                    display_label: subagent_display_label(&spec),
                    status: SubagentStatus::Running,
                    background: false,
                    depth: 1,
                    created_at: Utc::now(),
                    updated_at: Utc::now(),
                    completed_at: None,
                    summary: None,
                    error: None,
                    archive_metadata: None,
                    archive_path: None,
                    transcript_path: None,
                    effective_config: None,
                    stored_messages: Vec::new(),
                    last_prompt: None,
                    queued_prompts: VecDeque::new(),
                    max_turns: None,
                    model_override: None,
                    reasoning_override: None,
                    thread_handle: None,
                    handle: None,
                    closing: false,
                    notify: Arc::new(Notify::new()),
                    worktree_path: None,
                    child_controller: None,
                },
            );
        }
    }

    let controller_clone = controller.clone();
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_millis(20)).await;
        let mut state = controller_clone.state.write().await;
        let record = state.children.get_mut("second").expect("second child");
        record.status = SubagentStatus::Completed;
        record.summary = Some("done".to_string());
        record.completed_at = Some(Utc::now());
        record.updated_at = Utc::now();
        record.notify.notify_waiters();
    });

    let result = controller
        .wait(&["first".to_string(), "second".to_string()], Some(500))
        .await
        .expect("wait result")
        .expect("terminal child");
    assert_eq!(result.id, "second");
    assert_eq!(result.status, SubagentStatus::Completed);
}