Skip to main content

vtcode_core/prompts/
static_prompts.rs

1//! Static prompt text, constants, and cached static prompt builders.
2
3use std::sync::OnceLock;
4
5use crate::config::types::SystemPromptMode;
6use crate::prompts::runtime_guidance::{RUNTIME_GUIDANCE_MAX_ESTIMATED_TOKENS, RUNTIME_GUIDANCE_SECTION};
7use crate::prompts::system::{
8    DEFAULT_OPERATING_PROFILE_DELTA, DEFAULT_SPECIFIC_LINES, LIGHTWEIGHT_OPERATING_PROFILE_DELTA,
9    MINIMAL_OPERATING_PROFILE_DELTA, MINIMAL_SPECIFIC_LINES, PROMPT_INTRO, PROMPT_ROLE_PARAGRAPH, PROMPT_TITLE,
10    SHARED_CONTRACT_LINES, SPECIALIZED_OPERATING_PROFILE_DELTA,
11};
12use vtcode_commons::estimate_tokens;
13
14/// Agent identity labels for the system prompt.
15/// Maps agent names to human-readable identity strings that combine VT Code
16/// with the active agent mode, so the LLM knows its role.
17pub fn agent_identity_label(agent_name: &str) -> String {
18    match agent_name {
19        "build" => "VT Code (Build mode)".to_string(),
20        "auto" => "VT Code (Auto mode)".to_string(),
21        "coordinator" => "VT Code (Coordinator mode)".to_string(),
22        "duck" => "VT Code (Duck mode)".to_string(),
23        "plan" => "VT Code (Plan mode)".to_string(),
24        "explorer" => "VT Code (Explorer mode)".to_string(),
25        "worker" => "VT Code (Worker mode)".to_string(),
26        other => format!("VT Code ({other})"),
27    }
28}
29
30static DEFAULT_SYSTEM_PROMPT: OnceLock<String> = OnceLock::new();
31static MINIMAL_SYSTEM_PROMPT: OnceLock<String> = OnceLock::new();
32static DEFAULT_LIGHTWEIGHT_PROMPT: OnceLock<String> = OnceLock::new();
33static DEFAULT_SPECIALIZED_PROMPT: OnceLock<String> = OnceLock::new();
34
35fn append_runtime_guidance(prompt: &mut String) {
36    assert!(estimate_tokens(RUNTIME_GUIDANCE_SECTION) <= RUNTIME_GUIDANCE_MAX_ESTIMATED_TOKENS);
37    prompt.push_str(RUNTIME_GUIDANCE_SECTION);
38}
39
40/// Append the shared and per-mode working-style rules to the open rules list.
41fn append_contract_block(prompt: &mut String, specific_lines: &[&str]) {
42    for line in SHARED_CONTRACT_LINES.iter().chain(specific_lines) {
43        prompt.push_str("- ");
44        prompt.push_str(line);
45        prompt.push('\n');
46    }
47    prompt.push('\n');
48}
49
50fn build_static_prompt(include_role: bool, specific_lines: &[&str], operating_delta: &str) -> String {
51    let mut prompt = String::new();
52    prompt.push_str(PROMPT_TITLE);
53    prompt.push_str("\n\n");
54    prompt.push_str(PROMPT_INTRO);
55    prompt.push_str("\n\n");
56    if include_role {
57        prompt.push_str(PROMPT_ROLE_PARAGRAPH);
58        prompt.push_str("\n\n");
59    }
60    // One rules list: universal guidance first, then the per-mode working
61    // style. A separate heading only split the same rules across two sections.
62    append_runtime_guidance(&mut prompt);
63    append_contract_block(&mut prompt, specific_lines);
64    prompt.push_str(operating_delta);
65    prompt
66}
67
68pub fn default_system_prompt() -> &'static str {
69    static_profile_prompt(SystemPromptMode::Default)
70}
71
72pub fn minimal_system_prompt() -> &'static str {
73    static_profile_prompt(SystemPromptMode::Minimal)
74}
75
76pub fn default_lightweight_prompt() -> &'static str {
77    static_profile_prompt(SystemPromptMode::Lightweight)
78}
79
80pub fn specialized_system_prompt() -> &'static str {
81    static_profile_prompt(SystemPromptMode::Specialized)
82}
83
84pub fn minimal_instruction_text() -> String {
85    minimal_system_prompt().to_string()
86}
87
88pub fn lightweight_instruction_text() -> String {
89    default_lightweight_prompt().to_string()
90}
91
92pub fn specialized_instruction_text() -> String {
93    specialized_system_prompt().to_string()
94}
95
96pub fn static_profile_prompt(prompt_mode: SystemPromptMode) -> &'static str {
97    match prompt_mode {
98        SystemPromptMode::Default => DEFAULT_SYSTEM_PROMPT
99            .get_or_init(|| build_static_prompt(true, DEFAULT_SPECIFIC_LINES, DEFAULT_OPERATING_PROFILE_DELTA)),
100        SystemPromptMode::Minimal => MINIMAL_SYSTEM_PROMPT
101            .get_or_init(|| build_static_prompt(false, MINIMAL_SPECIFIC_LINES, MINIMAL_OPERATING_PROFILE_DELTA)),
102        SystemPromptMode::Lightweight => DEFAULT_LIGHTWEIGHT_PROMPT
103            .get_or_init(|| build_static_prompt(false, DEFAULT_SPECIFIC_LINES, LIGHTWEIGHT_OPERATING_PROFILE_DELTA)),
104        SystemPromptMode::Specialized => DEFAULT_SPECIALIZED_PROMPT
105            .get_or_init(|| build_static_prompt(true, DEFAULT_SPECIFIC_LINES, SPECIALIZED_OPERATING_PROFILE_DELTA)),
106    }
107}
108
109#[cfg(test)]
110mod tests {
111    use super::*;
112    use crate::config::constants::prompt_budget;
113    use crate::prompts::runtime_guidance::RUNTIME_GUIDANCE_SECTION;
114
115    #[test]
116    fn static_prompts_include_required_sections() {
117        let modes = [
118            (SystemPromptMode::Default, default_system_prompt()),
119            (SystemPromptMode::Minimal, minimal_system_prompt()),
120            (SystemPromptMode::Lightweight, default_lightweight_prompt()),
121            (SystemPromptMode::Specialized, specialized_system_prompt()),
122        ];
123        for (mode, prompt) in modes {
124            assert!(prompt.contains(PROMPT_TITLE), "{mode:?} missing title");
125            assert!(prompt.contains("## Rules"), "{mode:?} missing rules section");
126            assert!(prompt.contains("AGENTS.md"), "{mode:?} missing AGENTS.md ref");
127            assert_eq!(
128                prompt.matches(RUNTIME_GUIDANCE_SECTION).count(),
129                1,
130                "{mode:?} should include runtime guidance exactly once"
131            );
132            assert!(
133                prompt.find(RUNTIME_GUIDANCE_SECTION) < prompt.find("## Operating Profile"),
134                "{mode:?} should place runtime guidance before the contract"
135            );
136            assert!(
137                (estimate_tokens(prompt) as u64) <= prompt_budget::DEFAULT_MAX_SYSTEM_PROMPT_TOKENS,
138                "{mode:?} static prompt should fit the default budget"
139            );
140        }
141    }
142
143    #[test]
144    fn shared_contract_lines_and_runtime_bullets_appear_exactly_once_per_profile() {
145        let runtime_bullets = RUNTIME_GUIDANCE_SECTION
146            .lines()
147            .filter(|line| line.starts_with("- "))
148            .collect::<Vec<_>>();
149        assert!(runtime_bullets.len() >= 10, "runtime guidance bullets were not found");
150        let profiles = [
151            (SystemPromptMode::Default, DEFAULT_SPECIFIC_LINES),
152            (SystemPromptMode::Minimal, MINIMAL_SPECIFIC_LINES),
153            (SystemPromptMode::Lightweight, DEFAULT_SPECIFIC_LINES),
154            (SystemPromptMode::Specialized, DEFAULT_SPECIFIC_LINES),
155        ];
156
157        for (mode, specific_lines) in profiles {
158            let prompt = static_profile_prompt(mode);
159            for line in SHARED_CONTRACT_LINES.iter().chain(specific_lines) {
160                let bullet = format!("- {line}\n");
161                assert_eq!(
162                    prompt.matches(line).count(),
163                    1,
164                    "{mode:?} should state contract line {line:?} exactly once"
165                );
166                assert_eq!(
167                    prompt.matches(&bullet).count(),
168                    1,
169                    "{mode:?} should render {line:?} as one contract bullet"
170                );
171            }
172            for bullet in &runtime_bullets {
173                assert_eq!(
174                    prompt.matches(bullet).count(),
175                    1,
176                    "{mode:?} should state runtime guidance bullet {bullet:?} exactly once"
177                );
178            }
179        }
180    }
181
182    #[test]
183    fn static_prompts_do_not_embed_maintainer_files() {
184        let prompts = [
185            default_system_prompt(),
186            minimal_system_prompt(),
187            default_lightweight_prompt(),
188            specialized_system_prompt(),
189        ];
190        let maintainer_only_markers = [
191            "Keep this file concise and under 150 lines",
192            "vtcode-exec-events::ThreadEvent",
193            "RUSTFLAGS: \"-D warnings\"",
194            "Cargo workspace, ~30 crates",
195            "Root summary",
196        ];
197
198        for prompt in prompts {
199            for marker in maintainer_only_markers {
200                assert!(!prompt.contains(marker), "static prompt unexpectedly contains {marker:?}");
201            }
202        }
203    }
204}