vtcode-core 0.136.1

Core library for VT Code - a Rust-based terminal coding agent
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
//! Builtin tool pack implementations.
//!
//! Each struct below implements [`ToolPack`] for a logical group of related
//! tools. The `register()` method batch-registers its tools into the inventory
//! in a single pass.
//!
//! The `linkme::distributed_slice` macro uses `link_section` internally,
//! which triggers the `unsafe_code` lint. This is inherent to the crate's
//! mechanism and cannot be avoided at the call site.
#![allow(unsafe_code)]

use std::sync::Arc;

use linkme::distributed_slice;

use crate::config::constants::tools;
use crate::config::types::CapabilityLevel;
use crate::tool_policy::ToolPolicy;
use crate::tools::defuddle::{DEFUDDLE_FETCH_DESCRIPTION, DefuddleTool};
use crate::tools::handlers::{FinishPlanningTool, PlanningWorkflowState, StartPlanningTool, TaskTrackerTool};
use crate::tools::native_memory;
use crate::tools::registry::distributed::tool_config;
use crate::tools::registry::pack::BUILTIN_PACKS;
use crate::tools::registry::pack::{ToolPack, batch_register};
use crate::tools::registry::registration::ToolRegistration;
use crate::tools::registry::{ToolInventory, ToolRegistry, native_cgp_tool_factory};
use crate::tools::request_user_input::RequestUserInputTool;
use crate::tools::web_fetch::{WEB_FETCH_DESCRIPTION, WebFetchTool};
use crate::tools::web_search::{WEB_SEARCH_DESCRIPTION, WebSearchTool};
use serde_json::json;
use vtcode_utility_tool_specs::{
    agent_parameters, apply_patch_parameters, code_search_parameters, cron_parameters, exec_command_parameters,
    list_files_parameters, mcp_parameters, write_stdin_parameters,
};

// ===========================================================================
// HITL Pack
// ===========================================================================

#[derive(Default)]
pub struct HitlPack;

#[async_trait::async_trait]
impl ToolPack for HitlPack {
    fn pack_id(&self) -> &'static str {
        "hitl"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![
            ToolRegistration::from_tool_instance(
                tools::REQUEST_USER_INPUT,
                CapabilityLevel::Basic,
                RequestUserInputTool,
            )
            .with_native_cgp_factory(native_cgp_tool_factory(|| RequestUserInputTool)),
            ToolRegistration::new(
                tools::MEMORY,
                CapabilityLevel::Basic,
                false,
                ToolRegistry::memory_executor,
            )
            .with_description(
                "Access VT Code persistent memory files under /memories. Use action=view to list available notes before reading or updating; writes are limited to preferences.md, repository-facts.md, and notes/**. Returns file listing or file content.",
            )
            .with_parameter_schema(native_memory::parameter_schema())
            .with_permission(ToolPolicy::Allow),
            ToolRegistration::new(
                tools::CRON,
                CapabilityLevel::Basic,
                false,
                ToolRegistry::cron_executor,
            )
            .with_description(
                "Create, list, or delete session-scoped scheduled prompts. Use action=create to schedule a prompt, action=list to show scheduled prompts, or action=delete to remove one by id. Do not schedule per-minute jobs because they exhaust the per-turn tool budget. Scheduled prompts end when the vtcode process exits.",
            )
            .with_parameter_schema(cron_parameters())
            .with_aliases([
                tools::CRON_CREATE,
                tools::CRON_LIST,
                tools::CRON_DELETE,
                "schedule_task",
                "loop_create",
                "scheduled_tasks",
                "cancel_scheduled_task",
            ]),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Planning Pack
// ===========================================================================

#[derive(Default)]
pub struct PlanningPack;

#[async_trait::async_trait]
impl ToolPack for PlanningPack {
    fn pack_id(&self) -> &'static str {
        "planning"
    }

    async fn register(&self, inventory: &ToolInventory, plan_state: &PlanningWorkflowState) {
        let plan_state = Clone::clone(plan_state);
        let factory_state = Arc::new(plan_state.clone());
        let start_factory = Arc::clone(&factory_state);
        let finish_factory = Arc::clone(&factory_state);
        let tracker_factory = Arc::clone(&factory_state);
        let registrations = vec![
            ToolRegistration::from_tool_instance(
                tools::START_PLANNING,
                CapabilityLevel::Basic,
                StartPlanningTool::new(plan_state.clone()),
            )
            .with_native_cgp_factory(native_cgp_tool_factory(move || {
                let state = Arc::clone(&start_factory);
                StartPlanningTool::new(state.as_ref().clone())
            })),
            ToolRegistration::from_tool_instance(
                tools::FINISH_PLANNING,
                CapabilityLevel::Basic,
                FinishPlanningTool::new(plan_state.clone()),
            )
            .with_native_cgp_factory(native_cgp_tool_factory(move || {
                let state = Arc::clone(&finish_factory);
                FinishPlanningTool::new(state.as_ref().clone())
            })),
            ToolRegistration::from_tool_instance(
                tools::TASK_TRACKER,
                CapabilityLevel::Basic,
                TaskTrackerTool::new(
                    plan_state.workspace_root().unwrap_or_default(),
                    plan_state,
                ),
            )
            .with_native_cgp_factory(native_cgp_tool_factory(move || {
                let state = Arc::clone(&tracker_factory);
                TaskTrackerTool::new(
                    state.as_ref().workspace_root().unwrap_or_else(std::path::PathBuf::new),
                    state.as_ref().clone(),
                )
            }))
            .with_description(
                "Track task progress through a single checklist API (action: create | update | list | add). Use task_tracker with action=create at the start of a multi-step plan; use action=update as work progresses; use action=list to review current state. Do NOT call action=create twice — subsequent calls update the existing checklist. Tracker state mirrors between `.vtcode/tasks/current_task.md` and active plan sidecar files when available.",
            )
            .with_aliases(["plan_manager", "track_tasks", "checklist"]),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Multi-Agent Pack
// ===========================================================================

#[derive(Default)]
pub struct MultiAgentPack;

#[async_trait::async_trait]
impl ToolPack for MultiAgentPack {
    fn pack_id(&self) -> &'static str {
        "multi_agent"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![ToolRegistration::new(
            tools::AGENT,
            CapabilityLevel::Basic,
            false,
            ToolRegistry::agent_executor,
        )
        .with_description(
            "Spawn and steer delegated child agents. Use action=spawn to delegate a scoped task, action=spawn_subprocess for a managed background process, action=send_input to continue a child, action=resume to reopen a completed child, action=wait for results, or action=close to cancel a child. Use exec_command for one-shot shell commands.",
        )
        .with_parameter_schema(agent_parameters())
        .with_aliases([
            tools::SPAWN_AGENT,
            tools::SPAWN_BACKGROUND_SUBPROCESS,
            tools::SEND_INPUT,
            tools::RESUME_AGENT,
            tools::WAIT_AGENT,
            tools::CLOSE_AGENT,
            "delegate",
            "subagent",
            "background_subagent",
            "launch_background_helper",
            "message_agent",
            "continue_agent",
            "resume_subagent",
            "wait_subagent",
            "close_subagent",
        ])];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Search Pack
// ===========================================================================

#[derive(Default)]
pub struct SearchPack;

#[async_trait::async_trait]
impl ToolPack for SearchPack {
    fn pack_id(&self) -> &'static str {
        "search"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![
            ToolRegistration::new(
                tools::CODE_SEARCH,
                CapabilityLevel::CodeSearch,
                false,
                ToolRegistry::code_search_executor,
            )
            .with_description(
                "Search workspace code with one literal query. Use optional path, file_types, result_types, and max_results filters to find definitions, syntactic usages, text matches, and matching paths.",
            )
            .with_parameter_schema(code_search_parameters())
            .with_permission(ToolPolicy::Allow),
            ToolRegistration::new(
                tools::MCP,
                CapabilityLevel::CodeSearch,
                false,
                ToolRegistry::mcp_executor,
            )
            .with_description(
                "Discover and manage Model Context Protocol capabilities. Use action=search_tools to find tools, action=get_tool_details to fetch one schema, action=list_servers to inspect configured servers, or action=connect and action=disconnect to manage a named server. Do not disconnect a server while one of its tool calls is active.",
            )
            .with_parameter_schema(mcp_parameters())
            .with_permission(ToolPolicy::Allow)
            .with_aliases([
                tools::MCP_SEARCH_TOOLS,
                tools::MCP_GET_TOOL_DETAILS,
                tools::MCP_LIST_SERVERS,
                tools::MCP_CONNECT_SERVER,
                tools::MCP_DISCONNECT_SERVER,
                "mcp_tool_search",
                "mcp_tool_details",
            ]),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Web Pack
// ===========================================================================

#[derive(Default)]
pub struct WebPack;

#[async_trait::async_trait]
impl ToolPack for WebPack {
    fn pack_id(&self) -> &'static str {
        "web"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let web_fetch = tool_config()
            .map(|snapshot| WebFetchTool::from_config(&snapshot.web_fetch))
            .unwrap_or_default();
        let web_fetch_for_factory = web_fetch.clone();
        let web_fetch_factory = native_cgp_tool_factory(move || web_fetch_for_factory.clone());

        let web_search =
            WebSearchTool::with_config(tool_config().map(|snapshot| snapshot.web_search.clone()).unwrap_or_default());
        let web_search_for_factory = web_search.clone();
        let web_search_factory = native_cgp_tool_factory(move || web_search_for_factory.clone());

        let defuddle = DefuddleTool::new();
        let defuddle_for_factory = defuddle.clone();
        let defuddle_factory = native_cgp_tool_factory(move || defuddle_for_factory.clone());

        let registrations = vec![
            ToolRegistration::from_tool_instance(tools::WEB_FETCH, CapabilityLevel::Basic, web_fetch)
                .with_native_cgp_factory(web_fetch_factory)
                .with_description(WEB_FETCH_DESCRIPTION)
                .with_parameter_schema(json!({
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "description": "URL to fetch (HTTPS required by default)"
                        },
                        "prompt": {
                            "type": "string",
                            "description": "Question or instruction for analyzing the fetched content. Omit for a default summary."
                        },
                        "max_bytes": {
                            "type": "integer",
                            "description": "Maximum response body size in bytes (default: 500000). The default is generous — most pages including llms.txt fit easily. Only set this if you need to cap a very large page."
                        },
                        "timeout_secs": {
                            "type": "integer",
                            "description": "Request timeout in seconds (default: 30)"
                        }
                    },
                    "required": ["url"],
                    "additionalProperties": false
                }))
                .with_permission(ToolPolicy::Prompt)
                .with_aliases(["fetch_url", "web"]),
            ToolRegistration::from_tool_instance(tools::WEB_SEARCH, CapabilityLevel::Basic, web_search)
                .with_native_cgp_factory(web_search_factory)
                .with_description(WEB_SEARCH_DESCRIPTION)
                .with_parameter_schema(json!({
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "The search query (a topic, question, or keywords)."
                        },
                        "max_results": {
                            "type": "integer",
                            "description": "Maximum number of results to return (default: 8, max: 20)."
                        }
                    },
                    "required": ["query"],
                    "additionalProperties": false
                }))
                .with_permission(ToolPolicy::Prompt)
                .with_aliases(["search_web", "websearch"]),
            ToolRegistration::from_tool_instance(tools::DEFUDDLE_FETCH, CapabilityLevel::Basic, defuddle)
                .with_native_cgp_factory(defuddle_factory)
                .with_description(DEFUDDLE_FETCH_DESCRIPTION)
                .with_parameter_schema(json!({
                    "type": "object",
                    "properties": {
                        "url": {
                            "type": "string",
                            "format": "uri",
                            "pattern": "^https?://",
                            "description": "REMOTE web page URL (http:// or https:// ONLY). Do NOT use for local file paths."
                        },
                        "max_bytes": {
                            "type": "integer",
                            "description": "Hard cap on the returned markdown size in bytes (default: 262144, max: 262144)."
                        }
                    },
                    "required": ["url"],
                    "additionalProperties": false
                }))
                .with_permission(ToolPolicy::Prompt)
                .with_aliases(["defuddle", "extract_markdown"])
                .with_llm_visibility(false),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Shell Pack
// ===========================================================================

#[derive(Default)]
pub struct ShellPack;

#[async_trait::async_trait]
impl ToolPack for ShellPack {
    fn pack_id(&self) -> &'static str {
        "shell"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![
            ToolRegistration::new(
                tools::EXEC_COMMAND,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::exec_command_executor,
            )
            .with_description(
                "Use this to execute a shell command through the active sandbox policy and permission checks. Put normal shell tools such as ls, rg, find, cat, sed, awk, build tools, and test tools in cmd. Returns output, exit status, and a reusable session id when the command is still running.",
            )
            .with_parameter_schema(exec_command_parameters())
            .with_permission(ToolPolicy::Allow),
            ToolRegistration::new(
                tools::EXEC_PTY_CMD,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::run_pty_cmd_executor,
            )
            .with_description(
                "Execute a shell command attached to a PTY (pseudo-terminal) so interactive and TTY-aware programs behave as in a real terminal. Use this when the command needs a controlling terminal (e.g. pagers, prompts, curses UIs). Returns output, exit status, and a reusable session id when the command is still running.",
            )
            .with_parameter_schema(exec_command_parameters())
            .with_permission(ToolPolicy::Allow)
            .with_llm_visibility(false),
            ToolRegistration::new(
                tools::WRITE_STDIN,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::write_stdin_executor,
            )
            .with_description("Write characters to an active exec_command session stdin, then poll for fresh output.")
            .with_parameter_schema(write_stdin_parameters())
            .with_permission(ToolPolicy::Allow),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Internal PTY Pack
// ===========================================================================

#[derive(Default)]
pub struct InternalPtyPack;

#[async_trait::async_trait]
impl ToolPack for InternalPtyPack {
    fn pack_id(&self) -> &'static str {
        "internal_pty"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![
            ToolRegistration::new(tools::READ_FILE, CapabilityLevel::CodeSearch, false, ToolRegistry::read_file_executor)
                .with_description(
                    "Read file contents with chunked ranges or indentation-aware block selection. Exposed as a first-class browse tool for the harness surface.",
                )
                .with_permission(ToolPolicy::Allow)
                .with_llm_visibility(false),
            ToolRegistration::new(tools::LIST_FILES, CapabilityLevel::CodeSearch, false, ToolRegistry::list_files_executor)
                .with_description(
                    "List files and directories with pagination. Exposed as a first-class browse tool for the harness surface.",
                )
                .with_parameter_schema(list_files_parameters())
                .with_permission(ToolPolicy::Allow)
                .with_llm_visibility(false),
            ToolRegistration::new(tools::WRITE_FILE, CapabilityLevel::Editing, false, ToolRegistry::write_file_executor)
                .with_description("Write or overwrite a file with new content. Internal file helper.")
                .with_llm_visibility(false),
            ToolRegistration::new(tools::EDIT_FILE, CapabilityLevel::Editing, false, ToolRegistry::edit_file_executor)
                .with_description("Apply a surgical text replacement in a file. Internal file helper.")
                .with_llm_visibility(false),
            ToolRegistration::new(tools::RUN_PTY_CMD, CapabilityLevel::Bash, false, ToolRegistry::run_pty_cmd_executor)
                .with_description("Run a one-shot PTY command. Internal execution helper.")
                .with_llm_visibility(false),
            ToolRegistration::new(tools::SEND_PTY_INPUT, CapabilityLevel::Bash, false, ToolRegistry::send_pty_input_executor)
                .with_description("Send stdin to an active PTY session. Internal execution helper.")
                .with_llm_visibility(false),
            ToolRegistration::new(
                tools::READ_PTY_SESSION,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::read_pty_session_executor,
            )
            .with_description("Read buffered output from a PTY session. Internal execution helper.")
            .with_llm_visibility(false),
            ToolRegistration::new(
                tools::CREATE_PTY_SESSION,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::create_pty_session_executor,
            )
            .with_description("Create an interactive PTY session. Internal execution helper.")
            .with_llm_visibility(false),
            ToolRegistration::new(
                tools::LIST_PTY_SESSIONS,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::list_pty_sessions_executor,
            )
            .with_description("List all active PTY sessions. Internal execution helper.")
            .with_llm_visibility(false),
            ToolRegistration::new(
                tools::CLOSE_PTY_SESSION,
                CapabilityLevel::Bash,
                false,
                ToolRegistry::close_pty_session_executor,
            )
            .with_description("Close a PTY session by ID. Internal execution helper.")
            .with_llm_visibility(false),
            ToolRegistration::new(tools::GET_ERRORS, CapabilityLevel::CodeSearch, false, ToolRegistry::get_errors_executor)
                .with_description(
                    "Retrieve compilation/lint errors from the most recent run. Internal — used by the harness surface.",
                )
                .with_llm_visibility(false),
        ];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Editing Pack
// ===========================================================================

#[derive(Default)]
pub struct EditingPack;

#[async_trait::async_trait]
impl ToolPack for EditingPack {
    fn pack_id(&self) -> &'static str {
        "editing"
    }

    async fn register(&self, inventory: &ToolInventory, _plan_state: &PlanningWorkflowState) {
        let registrations = vec![ToolRegistration::new(
            tools::APPLY_PATCH,
            CapabilityLevel::Editing,
            false,
            ToolRegistry::apply_patch_executor,
        )
        .with_description(crate::tools::apply_patch::with_semantic_anchor_guidance(
            "Apply patches to files after permission checks. IMPORTANT: Use VT Code patch format (*** Begin Patch, *** Update File: path, @@ hunks with -/+ lines, *** End Patch), NOT standard unified diff (---/+++ format).",
        ))
        .with_parameter_schema(apply_patch_parameters())
        .with_permission(ToolPolicy::Prompt)];
        batch_register(inventory, registrations);
    }
}

// ===========================================================================
// Pack factory functions (collected via linkme)
// ===========================================================================

#[distributed_slice(BUILTIN_PACKS)]
fn hitl_pack() -> Box<dyn ToolPack> {
    Box::new(HitlPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn planning_pack() -> Box<dyn ToolPack> {
    Box::new(PlanningPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn multi_agent_pack() -> Box<dyn ToolPack> {
    Box::new(MultiAgentPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn search_pack() -> Box<dyn ToolPack> {
    Box::new(SearchPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn web_pack() -> Box<dyn ToolPack> {
    Box::new(WebPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn shell_pack() -> Box<dyn ToolPack> {
    Box::new(ShellPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn internal_pty_pack() -> Box<dyn ToolPack> {
    Box::new(InternalPtyPack)
}

#[distributed_slice(BUILTIN_PACKS)]
fn editing_pack() -> Box<dyn ToolPack> {
    Box::new(EditingPack)
}