vtcode-core 0.181.8

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

#[tokio::test]
async fn executes_prevalidated_tool_path() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    registry
        .register_tool(ToolRegistration::from_tool_instance(
            CUSTOM_TOOL_NAME,
            CapabilityLevel::CodeSearch,
            CustomEchoTool,
        ))
        .await?;
    registry.allow_all_tools().await?;

    let args = json!({"input": "value"});
    let response = registry.execute_tool_ref_prevalidated(CUSTOM_TOOL_NAME, &args).await?;
    assert!(response["success"].as_bool().unwrap_or(false));

    Ok(())
}
#[tokio::test]
async fn harness_exec_reuses_public_output_normalization() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    let response = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf vtcode",
            "tty": false,
            "yield_time_ms": 1000
        }))
        .await?;

    assert_eq!(response["output"].as_str(), Some("vtcode"));
    assert!(response.get("stdout").is_none());

    Ok(())
}
#[tokio::test]
async fn harness_terminal_runs_retain_completed_sessions_until_close() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    let active_pty_sessions = Arc::new(AtomicUsize::new(0));
    registry.set_active_pty_sessions(Arc::clone(&active_pty_sessions));

    let response = registry
        .execute_harness_command_session_terminal_run(json!({
            "action": "run",
            "command": ["/bin/sh", "-lc", "printf vtcode-terminal"],
            "tty": true,
            "yield_time_ms": 200,
        }))
        .await?;

    let session_id = response["session_id"]
        .as_str()
        .expect("terminal run should expose session_id")
        .to_string();
    assert_eq!(response["exit_code"], 0);
    assert_eq!(response["output"].as_str(), Some("vtcode-terminal"));
    // A completed command releases its foreground-PTY count immediately; the
    // retention contract applies to the session record, not the live count.
    assert_eq!(active_pty_sessions.load(Ordering::Relaxed), 0);
    assert_eq!(registry.harness_exec_session_completed(&session_id).await?, Some(0));

    registry.close_harness_exec_session(&session_id).await?;
    assert_eq!(active_pty_sessions.load(Ordering::Relaxed), 0);
    registry.harness_exec_session_completed(&session_id).await.unwrap_err();

    Ok(())
}
#[tokio::test]
async fn background_terminal_runs_do_not_mark_the_ui_busy() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    let active_pty_sessions = Arc::new(AtomicUsize::new(0));
    registry.set_active_pty_sessions(Arc::clone(&active_pty_sessions));

    let response = registry
        .execute_harness_command_session_terminal_run(json!({
            "action": "run",
            "command": ["/bin/sh", "-lc", "sleep 5"],
            "tty": true,
            "background": true,
            "yield_time_ms": 200,
        }))
        .await?;
    let session_id = response["session_id"]
        .as_str()
        .expect("background terminal run should expose session_id");

    assert_eq!(response["background"], true);
    assert_eq!(active_pty_sessions.load(Ordering::Relaxed), 0);

    registry.close_harness_exec_session(session_id).await?;
    assert_eq!(active_pty_sessions.load(Ordering::Relaxed), 0);
    Ok(())
}
#[tokio::test]
async fn background_exec_returns_reusable_session_metadata_and_drains_on_wait() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    registry.allow_all_tools().await?;

    let initial = tokio::time::timeout(
        Duration::from_secs(3),
        registry.execute_harness_command_session(json!({
            "action": "run",
            "command": "printf first; sleep 0.4; printf second",
            "tty": false,
            "background": true,
            "yield_time_ms": 250,
        })),
    )
    .await??;
    let session_id = initial["session_id"]
        .as_str()
        .expect("background run should expose session_id")
        .to_string();

    assert_eq!(initial["background"], true);
    assert_eq!(initial["lifecycle_state"], "running");
    assert!(initial["child_pid"].as_u64().is_some_and(|pid| pid > 0));
    assert_eq!(initial["next_wait_args"]["session_id"], session_id);
    assert_eq!(initial["next_continue_args"]["session_id"], session_id);
    assert!(initial["output"].as_str().unwrap_or_default().contains("first"));

    let completed = tokio::time::timeout(
        Duration::from_secs(5),
        registry.execute_harness_command_session(json!({
            "action": "wait",
            "session_id": session_id,
            "wait_timeout_seconds": 3,
        })),
    )
    .await??;
    assert_eq!(completed["background"], true);
    assert_eq!(completed["exit_code"], 0);
    assert!(completed["output"].as_str().unwrap_or_default().contains("second"));

    registry
        .execute_harness_command_session(json!({
            "action": "close",
            "session_id": session_id,
        }))
        .await?;
    Ok(())
}
#[tokio::test]
async fn prevalidated_exec_mode_settles_noninteractive_run() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    registry.allow_all_tools().await?;

    let response = registry
        .execute_public_tool_ref_prevalidated_with_mode(
            tools::EXEC_COMMAND,
            &delayed_exec_args(false, 50),
            ExecSettlementMode::SettleNonInteractive,
        )
        .await?;

    let output = response["output"].as_str().expect("settled exec output should be text");
    assert!(output.contains("first"));
    assert!(output.contains("second"));
    assert_eq!(response["exit_code"], 0);
    assert!(response.get("next_continue_args").is_none());

    Ok(())
}
#[tokio::test]
async fn prevalidated_exec_mode_settles_pipe_poll_until_exit() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    registry.allow_all_tools().await?;

    let initial = registry.execute_harness_command_session(delayed_exec_args(false, 50)).await?;
    let session_id = initial["session_id"]
        .as_str()
        .expect("partial run should expose session_id")
        .to_string();
    let initial_output = initial["output"].as_str().unwrap_or_default().to_string();
    if initial.get("next_continue_args").is_none() {
        assert_eq!(initial["exit_code"], 0);
        assert!(initial_output.contains("first"));
        assert!(initial_output.contains("second"));
        return Ok(());
    }
    assert!(initial.get("exit_code").is_none());

    let response = registry
        .execute_public_tool_ref_prevalidated_with_mode(
            tools::WRITE_STDIN,
            &json!({
                "session_id": session_id,
                "chars": "",
                "yield_time_ms": 50,
            }),
            ExecSettlementMode::SettleNonInteractive,
        )
        .await?;

    assert_eq!(response["exit_code"], 0);
    let settled_output = response["output"].as_str().expect("settled poll output should be text");
    assert!(initial_output.contains("second") || settled_output.contains("second"));
    assert!(response.get("next_continue_args").is_none());

    Ok(())
}
#[tokio::test]
async fn prevalidated_exec_mode_keeps_interactive_runs_manual() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    registry.allow_all_tools().await?;

    let response = registry
        .execute_public_tool_ref_prevalidated_with_mode(
            tools::EXEC_COMMAND,
            &long_running_exec_args(true, 50),
            ExecSettlementMode::SettleNonInteractive,
        )
        .await?;

    assert!(response.get("next_continue_args").is_some());
    assert!(response.get("exit_code").is_none());

    let session_id = response["session_id"]
        .as_str()
        .expect("interactive run should expose session_id")
        .to_string();
    registry
        .execute_harness_command_session(json!({
            "action": "close",
            "session_id": session_id,
        }))
        .await?;

    Ok(())
}
#[tokio::test]
async fn command_session_run_preserves_requested_session_id_for_follow_up_calls() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    // This regression isolates the legacy transport/diff contract.
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);
    registry.allow_all_tools().await?;

    let mut run_args = long_running_exec_args(true, 10);
    run_args
        .as_object_mut()
        .expect("run args should be an object")
        .insert("session_id".to_string(), json!("check_sh"));

    let initial = registry.execute_harness_command_session(run_args).await?;
    assert_eq!(initial["session_id"], "check_sh");
    assert_eq!(initial["next_continue_args"], json!({ "session_id": "check_sh" }));

    let response = registry
        .execute_harness_command_session(json!({
            "action": "poll",
            "session_id": "check_sh",
            "yield_time_ms": 10,
        }))
        .await?;

    assert!(response.get("output").is_some());
    assert!(response.get("exit_code").is_some() || response.get("next_continue_args").is_some());

    if response.get("exit_code").is_none() {
        registry
            .execute_harness_command_session(json!({
                "action": "close",
                "session_id": "check_sh",
            }))
            .await?;
    }

    Ok(())
}

#[tokio::test]
async fn pipe_run_with_monitor_filter_attaches_only_matches() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    let run = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf 'ok one\\nerror: bad\\nok two\\n'",
            "tty": false,
            "yield_time_ms": 1000,
            "monitor_filter": "error"
        }))
        .await?;

    let digest = &run["monitor_digest"];
    assert!(digest.is_object(), "run response must carry the digest: {run}");
    assert_eq!(digest["text"].as_str(), Some("error: bad"));
    assert_eq!(digest["total_seen"], 1);
    assert_eq!(digest["dropped_lines"], 0);

    // Asymmetric control: the same command without a filter omits the field.
    let plain = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf 'ok one\\nerror: bad\\nok two\\n'",
            "tty": false,
            "yield_time_ms": 1000
        }))
        .await?;
    assert!(plain.get("monitor_digest").is_none(), "unfiltered runs must omit the digest: {plain}");

    Ok(())
}

#[tokio::test]
async fn monitor_digest_drains_once_across_polls() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    let run = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf 'error: one\\n'; sleep 30",
            "tty": false,
            "background": true,
            "yield_time_ms": 1000,
            "monitor_filter": "error"
        }))
        .await?;
    assert_eq!(run["monitor_digest"]["text"].as_str(), Some("error: one"));
    let session_id = run["session_id"]
        .as_str()
        .expect("background run must expose session_id")
        .to_string();

    let poll = registry
        .execute_harness_command_session(json!({
            "action": "poll",
            "session_id": session_id,
            "yield_time_ms": 10,
        }))
        .await?;
    assert!(poll.get("monitor_digest").is_none(), "drained window must not reappear: {poll}");

    registry
        .execute_harness_command_session(json!({"action": "close", "session_id": session_id}))
        .await?;

    Ok(())
}

#[tokio::test]
async fn monitor_inspect_peeks_without_draining() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    let run = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf 'error: one\\n'; sleep 2; printf 'error: two\\n'; sleep 30",
            "tty": false,
            "background": true,
            "yield_time_ms": 1000,
            "monitor_filter": "error"
        }))
        .await?;
    assert_eq!(run["monitor_digest"]["text"].as_str(), Some("error: one"));
    let session_id = run["session_id"]
        .as_str()
        .expect("background run must expose session_id")
        .to_string();

    let inspect = registry
        .execute_harness_command_session(json!({
            "action": "inspect",
            "session_id": session_id,
            "yield_time_ms": 3000,
        }))
        .await?;
    assert_eq!(inspect["monitor_digest"]["text"].as_str(), Some("error: two"));

    // Inspect must not consume: a later poll still sees the same window.
    let poll = registry
        .execute_harness_command_session(json!({
            "action": "poll",
            "session_id": session_id,
            "yield_time_ms": 10,
        }))
        .await?;
    assert_eq!(poll["monitor_digest"]["text"].as_str(), Some("error: two"));

    registry
        .execute_harness_command_session(json!({"action": "close", "session_id": session_id}))
        .await?;

    Ok(())
}

#[tokio::test]
async fn monitor_filter_on_pty_run_is_rejected() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;
    registry.set_inspection_mode(vtcode_config::core::agent::InspectionMode::Off);

    let result = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "sleep 1",
            "tty": true,
            "yield_time_ms": 200,
            "monitor_filter": "error"
        }))
        .await;

    match result {
        Err(error) => assert!(format!("{error:?}").contains("pipe-only"), "{error:?}"),
        Ok(value) => assert!(value.to_string().contains("pipe-only"), "{value}"),
    }

    Ok(())
}

#[tokio::test]
async fn monitor_filter_rejects_empty_value_before_spawn() -> Result<()> {
    let temp_dir = TempDir::new()?;
    let registry = ToolRegistry::new(temp_dir.path().to_path_buf()).await;

    let result = registry
        .execute_harness_command_session(json!({
            "action": "run",
            "command": "printf hi",
            "tty": false,
            "yield_time_ms": 1000,
            "monitor_filter": ""
        }))
        .await;

    match result {
        Err(error) => assert!(format!("{error:?}").contains("monitor_filter"), "{error:?}"),
        Ok(value) => assert!(value.to_string().contains("monitor_filter"), "{value}"),
    }

    Ok(())
}