vtcode-core 0.181.1

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

#[tokio::test]
async fn planning_prompt_size_fixed_fixture() {
    use crate::core::agent::harness_kernel::SessionToolCatalogSnapshot;
    use crate::llm::provider::ToolDefinition;
    use crate::llm::providers::OpenAIProvider;
    use crate::prompts::{
        RuntimePromptContract, append_runtime_mode_sections, append_runtime_tool_prompt_sections_for_model,
    };

    struct PlanningPromptSizeCase {
        density: &'static str,
        prompt_budget_tokens: u64,
        max_prompt_bytes: usize,
        max_estimated_tokens: usize,
    }

    let workspace = tempfile::TempDir::new().expect("workspace");
    let provider = OpenAIProvider::new("offline-fixture".into());
    let names = [
        tools::EXEC_COMMAND,
        tools::CODE_SEARCH,
        tools::TASK_TRACKER,
        tools::REQUEST_USER_INPUT,
    ];
    let snapshot = SessionToolCatalogSnapshot::new(
        7,
        9,
        true,
        true,
        Some(std::sync::Arc::new(
            names
                .iter()
                .map(|name| {
                    ToolDefinition::function(
                        (*name).to_string(),
                        "Fixture tool".to_string(),
                        serde_json::json!({"type": "object"}),
                    )
                })
                .collect(),
        )),
        false,
    );
    // Source inspection adds one shared runtime sentence (about 60 tokens).
    // Retain the canonical output contract and HOVER guidance. Before the tracker
    // examples, Default/Minimal measured 9847/10180 bytes and 2107/2205 tokens.
    // Fresh completion guidance adds 248 bytes and about 45 estimated tokens.
    // Zero-on-success planning/runtime guidance adds 253 bytes and about 51 tokens.
    // Action-specific tracker examples measure 9997 bytes/2146 tokens for Default
    // and 10269 bytes/2216 tokens for Minimal; keep tight caps and presence checks.
    // Cancelled planning resumes on fresh input; retain a 50-byte allowance for this policy.
    // Terminal planning guidance adds 128 bytes and about 30 estimated tokens.
    // Partial-commit and conflict-reload runtime guidance adds 229 bytes and
    // about 45 estimated tokens (Default measures 11328 bytes/2398 tokens,
    // Minimal measures 11600 bytes/2468 tokens).
    // Scoped-verify planning guidance adds 149 bytes and about 35 estimated
    // tokens (Default measures 11477 bytes/2433 tokens, Minimal measures
    // 11749 bytes/2503 tokens).
    for case in [
        PlanningPromptSizeCase {
            density: "Default",
            prompt_budget_tokens: 100_000,
            max_prompt_bytes: 11_550,
            max_estimated_tokens: 2_470,
        },
        PlanningPromptSizeCase {
            density: "Minimal",
            prompt_budget_tokens: 1,
            max_prompt_bytes: 11_820,
            max_estimated_tokens: 2_540,
        },
    ] {
        let mut config = VTCodeConfig::default();
        config.agent.system_prompt_mode = SystemPromptMode::Minimal;
        config.agent.include_temporal_context = false;
        config.agent.include_working_directory = true;
        config.agent.instruction_max_bytes = 0;
        config.agent.shell_prompt_profile = ShellPromptProfile::UnixLike;
        let mut context = PromptContext {
            available_tools: names.iter().map(|name| (*name).to_string()).collect(),
            ..Default::default()
        };
        context.set_current_directory(PathBuf::from("/workspace"));
        let mut prompt = compose_system_instruction_text(workspace.path(), Some(&config), Some(&context)).await;
        append_runtime_mode_sections(
            &mut prompt,
            RuntimePromptContract {
                planning_active: true,
                request_user_input_enabled: true,
                ..Default::default()
            },
        );
        config.agent.max_system_prompt_tokens = case.prompt_budget_tokens;
        append_runtime_tool_prompt_sections_for_model(
            &mut prompt,
            &snapshot,
            true,
            ResolvedShellPromptProfile::UnixLike,
            &provider,
            crate::config::constants::models::openai::DEFAULT_MODEL,
            Some(&config),
        );
        eprintln!(
            "planning fixture {}: {} bytes, {} estimated tokens",
            case.density,
            prompt.len(),
            estimate_tokens(&prompt)
        );
        assert!(prompt.contains(PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE));
        assert!(prompt.contains("Checklists do not persist plans"));
        assert!(prompt.contains("Rejected plans stay blocked; repair before approval"));
        assert!(prompt.len() <= case.max_prompt_bytes, "{} planning fixture exceeded byte budget", case.density);
        assert!(
            estimate_tokens(&prompt) <= case.max_estimated_tokens,
            "{} planning fixture exceeded token budget",
            case.density
        );
    }
}
#[tokio::test]
async fn test_minimal_mode_selection() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Minimal;
    // Disable enhancements for base prompt size testing
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    // Minimal prompt should remain compact and deterministic without AGENTS.md injection.
    // Char bound is a smoke check only; tokens are authoritative.
    // Includes direct patch calls and bounded context-mismatch recovery.
    // Raised from 3700 for the reuse-reads and exact-whitespace patch-copy guidance (measured 3917).
    // Raised from 4700 for the fold-by-default delegation rule (measured 4719).
    // Raised for the partial-commit and conflict-reload runtime guidance (measured 4948).
    assert!(result.len() < 5010, "Minimal mode should produce <5.01K chars (was {} chars)", result.len());
    assert!(result.contains("VT Code") || result.contains("VT Code"), "Should contain VT Code identifier");
}
#[tokio::test]
async fn test_default_prompt_selection() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Default;
    // Disable enhancements for base prompt size testing
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    // Includes fresh completion evidence guidance (+248 bytes, about 45 tokens).
    // Generated verifier success guidance adds 114 bytes (about 25 tokens).
    // Includes patch recovery guidance; token tests stay authoritative.
    // Raised from 5400 for the reuse-reads runtime guidance bullet (measured 5496).
    // Named HOVER guidance measures 5616 bytes; retain a narrow byte allowance.
    // Raised from 6350 for the fold-by-default delegation rule (measured 6375).
    // Raised for the partial-commit and conflict-reload runtime guidance (measured 6604).
    assert!(
        result.len() <= 6670,
        "Default mode should stay sparse with runtime guidance (<=6670 bytes, was {} bytes)",
        result.len()
    );
    assert!(result.contains("`exec_command`, `write_stdin`, and `apply_patch`"));
    assert!(result.contains("## Shell Profile"));
    assert!(!result.contains("task_tracker"));
    assert!(!result.contains("@file"));
    assert!(result.contains("Planning workflow"));
}
#[tokio::test]
async fn test_lightweight_mode_selection() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Lightweight;
    // Disable enhancements for base prompt size testing
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    assert!(result.len() > 100, "Lightweight should be >100 chars");
    // Includes fresh completion evidence guidance (+248 bytes, about 45 tokens).
    // Generated verifier success guidance adds 114 bytes (about 25 tokens).
    // Includes patch recovery guidance; token tests stay authoritative.
    // Raised from 4800 for the reuse-reads runtime guidance bullet (measured 4862).
    // Raised for the partial-commit and conflict-reload runtime guidance (measured 5970).
    assert!(
        result.len() < 6040,
        "Lightweight should be compact with runtime guidance (<6.04K chars, was {} chars)",
        result.len()
    );
    assert!(result.contains("task_tracker"));
    assert!(!result.contains("@file"));
    assert!(result.contains("act directly in this thread"));
}
#[tokio::test]
async fn test_lightweight_mode_skips_structured_reasoning_by_default() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Lightweight;
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;
    config.agent.include_structured_reasoning_tags = None;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    assert!(
        !result.contains("## Structured Reasoning"),
        "Lightweight mode should omit structured reasoning by default"
    );
}
#[tokio::test]
async fn test_lightweight_mode_allows_explicit_structured_reasoning() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Lightweight;
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;
    config.agent.include_structured_reasoning_tags = Some(true);

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    assert!(
        result.contains("## Structured Reasoning"),
        "Lightweight mode should include structured reasoning when explicitly enabled"
    );
    assert!(result.contains("<reasoning_plan>"));
    assert!(!result.contains("`<plan>` steps"), "<plan> is reserved for approval artifacts");
}
#[tokio::test]
async fn test_default_prompt_omits_structured_reasoning_by_default() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Default;
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;
    config.agent.include_structured_reasoning_tags = None;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    assert!(
        !result.contains("## Structured Reasoning"),
        "Default mode should omit structured reasoning by default"
    );
}
#[tokio::test]
async fn test_specialized_prompt_omits_structured_reasoning_by_default() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Specialized;
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;
    config.agent.include_structured_reasoning_tags = None;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    assert!(
        !result.contains("## Structured Reasoning"),
        "Specialized mode should omit structured reasoning unless explicitly enabled"
    );
}
#[tokio::test]
async fn test_specialized_mode_selection() {
    let mut config = VTCodeConfig::default();
    config.agent.system_prompt_mode = SystemPromptMode::Specialized;
    // Disable enhancements for base prompt size testing
    config.agent.include_temporal_context = false;
    config.agent.include_working_directory = false;
    config.agent.instruction_max_bytes = 0;

    let result = compose_system_instruction_text(&PathBuf::from("."), Some(&config), None).await;

    // Includes fresh completion evidence guidance (+248 bytes, about 45 tokens).
    // Generated verifier success guidance adds 114 bytes (about 25 tokens).
    // Includes patch recovery guidance; token tests stay authoritative.
    // Raised from 5400 for the reuse-reads runtime guidance bullet (measured 5569).
    // Raised for the partial-commit and conflict-reload runtime guidance (measured 6677).
    assert!(
        result.len() <= 6740,
        "Specialized should stay sparse with runtime guidance (<=6.74K chars, was {} chars)",
        result.len()
    );
    assert!(result.contains("task_tracker"));
    assert!(result.contains("<proposed_plan>"));
    assert!(result.contains(
        "- When repo-wide invariants matter, also read the architecture documents the instruction map points to."
    ));
    // Shipped prompts serve any repository, so they name no VT Code-specific doc.
    assert!(!result.contains("ARCHITECTURAL_INVARIANTS"));
}
#[test]
fn test_prompt_mode_enum_parsing() {
    assert_eq!(SystemPromptMode::parse("minimal"), Some(SystemPromptMode::Minimal));
    assert_eq!(SystemPromptMode::parse("LIGHTWEIGHT"), Some(SystemPromptMode::Lightweight));
    assert_eq!(SystemPromptMode::parse("Default"), Some(SystemPromptMode::Default));
    assert_eq!(SystemPromptMode::parse("specialized"), Some(SystemPromptMode::Specialized));
    assert_eq!(SystemPromptMode::parse("invalid"), None);
}
#[test]
fn operating_profile_deltas_share_canonical_sentences() {
    assert!(MINIMAL_OPERATING_PROFILE_DELTA.contains(OPERATING_TASK_TRACKER));
    assert!(LIGHTWEIGHT_OPERATING_PROFILE_DELTA.contains(OPERATING_TASK_TRACKER));
    assert!(OPERATING_TASK_TRACKER.contains("use `task_tracker` updates over lists"));
    // `start_planning` guidance has one home: the tool-gated Active Tools
    // line, which is present only when the tool is.
    for delta in [
        DEFAULT_OPERATING_PROFILE_DELTA,
        MINIMAL_OPERATING_PROFILE_DELTA,
        LIGHTWEIGHT_OPERATING_PROFILE_DELTA,
        SPECIALIZED_OPERATING_PROFILE_DELTA,
    ] {
        assert!(!delta.contains("start_planning"), "{delta}");
    }
    assert!(!DEFAULT_OPERATING_PROFILE_DELTA.contains("task_tracker"));
}
/// Regression guard: `PLANNING_WORKFLOW_PLAN_QUALITY_LINE` must keep
/// instructing the model to write file:symbol references as plain text
/// / inline code, not as markdown links or editor/IDE URI schemes (a
/// model was observed emitting `vscode-file://` pseudo-links pointing at
/// the editor binary instead of the referenced repo file).
#[test]
fn plan_quality_line_requires_scope_section() {
    let line = PLANNING_WORKFLOW_PLAN_QUALITY_LINE;
    assert!(line.contains("`## Scope`"));
    assert!(line.contains("never tracker steps"));
}
#[test]
fn plan_quality_line_keeps_rejected_work_blocked_until_repaired() {
    assert!(PLANNING_WORKFLOW_PLAN_QUALITY_LINE.contains("Rejected plans stay blocked; repair before approval"));
}
#[test]
fn plan_quality_line_forbids_markdown_link_file_references() {
    let line = PLANNING_WORKFLOW_PLAN_QUALITY_LINE;
    assert!(line.contains("never as markdown links or editor/IDE URIs"));
    assert!(line.contains("vscode-file://"));
    assert!(line.contains("plain text or inline code"));
}
/// The initial prompt must show the exact step grammar the validator
/// enforces, not just describe it — the repair directive prints the
/// canonical example only after a first rejection (turn_912/913 failed
/// every implementation step on "lacks a concrete target or verification"
/// without the model ever seeing an example). Keep the inline literal in
/// sync with the validator's canonical format.
#[test]
fn plan_quality_line_shows_canonical_step_format() {
    assert!(
        PLANNING_WORKFLOW_PLAN_QUALITY_LINE
            .contains(crate::tools::handlers::planning_workflow::artifacts::CANONICAL_STEP_FORMAT),
        "quality line must embed artifacts::CANONICAL_STEP_FORMAT verbatim"
    );
}
#[test]
fn plan_quality_line_requires_concrete_verify_checks() {
    assert!(PLANNING_WORKFLOW_PLAN_QUALITY_LINE.contains("never as a semicolon chain"));
    assert!(PLANNING_WORKFLOW_PLAN_QUALITY_LINE.contains("npx markdownlint-cli2 README.md"));
    let line = PLANNING_WORKFLOW_PLAN_QUALITY_LINE;
    assert!(line.contains("must exit 0 on success"));
    assert!(line.contains("grep/rg exit 1 cannot satisfy completion"));
    assert!(line.contains("both present and absent inputs"));
    assert!(line.contains("one concrete `verify:`/`verification:` command or observable check"));
    assert!(
        line.contains("Scope each `verify:` command to the step's files"),
        "quality line must scope verify commands to the step's files so full-repo checks cannot fail on pre-existing out-of-scope findings"
    );
    assert!(line.contains("vague prose"));
    assert!(line.contains("comma-separated verify entries that are not commands or observable checks"));
    assert!(
        line.contains("Commas inside single or double quotes stay inside one verify item"),
        "quality line must state the quoted-comma bracket rule so first-attempt plans do not fragment shell patterns"
    );
    assert!(
        line.contains("sed -n") && line.contains("grep -n"),
        "quality line must name inspection commands that recovery synthesis naturally emits"
    );
    assert!(
        line.contains("`git log`")
            && line.contains("`git show`")
            && line.contains("`git diff`")
            && line.contains("`git blame`")
    );
    assert!(line.contains("Reuse evidence; focus output"));
}
#[test]
fn planning_workflow_persistence_policy_assigns_plan_lifecycle_to_runtime() {
    let line = PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE;
    assert!(line.contains("Emit exactly one final `<proposed_plan>` block"));
    assert!(line.contains("no surrounding prose"));
    assert!(line.contains("Do not use shell commands or file-writing tools to create or modify `.vtcode/plans/`"));
    assert!(line.contains("runtime owns plan/tracker persistence and validation"));
    assert!(line.contains("approval controls only after successful persistence"));
    assert!(line.contains("Checklists do not persist plans"));
    assert!(line.contains("Resume cancelled planning only on fresh input"));
    assert!(PLANNING_WORKFLOW_PLAN_POLICY_LINE.contains("Exhausted plan validation or recovery waits for retry"));
    assert!(PLANNING_WORKFLOW_PLAN_POLICY_LINE.contains("Explicit Plan mode requires approval even in full-auto"));
    assert!(PLANNING_WORKFLOW_PLAN_POLICY_LINE.contains("User answers never approve implementation"));
}
#[test]
fn test_minimal_prompt_token_count() {
    let approx_tokens = estimate_token_count(minimal_system_prompt());
    // Raised from 400: the shared runtime guidance is now full sentences with reasons.
    // Includes direct patch calls and one bounded context-mismatch recovery read.
    // Raised from 665 for the reuse-reads runtime guidance bullet (measured 688).
    // Raised to 725 for the hypothesis-loop runtime bullet (measured 710).
    // Raised to 890 for the fold-by-default delegation rule (measured 878).
    // Raised for the partial-commit and conflict-reload runtime guidance (measured 920).
    assert!(approx_tokens <= 955, "Minimal prompt should stay compact, got ~{approx_tokens}");
}