Skip to main content

vtcode_core/prompts/
runtime_contract.rs

1use std::fmt::Write as _;
2
3use super::system::{
4    PLANNING_WORKFLOW_EXIT_INSTRUCTION_LINE, PLANNING_WORKFLOW_INTERVIEW_POLICY_LINE,
5    PLANNING_WORKFLOW_NO_AUTO_EXIT_LINE, PLANNING_WORKFLOW_NO_REQUEST_USER_INPUT_POLICY_LINE,
6    PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE, PLANNING_WORKFLOW_PLAN_POLICY_LINE,
7    PLANNING_WORKFLOW_PLAN_QUALITY_LINE, PLANNING_WORKFLOW_READ_ONLY_HEADER, PLANNING_WORKFLOW_READ_ONLY_NOTICE_LINE,
8    PLANNING_WORKFLOW_RESEARCH_SCOPE_LINE, PLANNING_WORKFLOW_TASK_TRACKER_LINE,
9};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
12pub struct RuntimePromptContract {
13    pub full_auto: bool,
14    pub planning_active: bool,
15    pub request_user_input_enabled: bool,
16}
17
18pub fn append_runtime_mode_sections(prompt: &mut String, contract: RuntimePromptContract) {
19    if contract.full_auto {
20        append_full_auto_notice(prompt, contract);
21    }
22
23    if contract.planning_active {
24        append_planning_workflow_notice(prompt, contract.request_user_input_enabled);
25    }
26}
27
28fn append_full_auto_notice(prompt: &mut String, contract: RuntimePromptContract) {
29    let header = if contract.planning_active {
30        "# FULL-AUTO (PLANNING WORKFLOW): Work autonomously within planning workflow constraints."
31    } else {
32        "# FULL-AUTO: Complete task autonomously until done or blocked."
33    };
34
35    if prompt.contains(header) {
36        return;
37    }
38
39    let _ = writeln!(prompt, "\n{header}");
40    let _ = writeln!(prompt, "- Stay within the exposed tool list and adapt when a tool is unavailable or denied.");
41    // Checkpoint guidance is already in the operating profiles; omit here to
42    // avoid token waste from duplication.
43    if !contract.request_user_input_enabled {
44        let _ = writeln!(
45            prompt,
46            "- `request_user_input` is unavailable in this runtime; make reasonable assumptions and continue with the available context."
47        );
48    }
49}
50
51fn append_planning_workflow_notice(prompt: &mut String, request_user_input_enabled: bool) {
52    if prompt.contains(PLANNING_WORKFLOW_READ_ONLY_HEADER) {
53        if !request_user_input_enabled && !prompt.contains(PLANNING_WORKFLOW_NO_REQUEST_USER_INPUT_POLICY_LINE) {
54            let _ = writeln!(prompt, "{PLANNING_WORKFLOW_NO_REQUEST_USER_INPUT_POLICY_LINE}");
55        }
56        return;
57    }
58
59    prompt.push('\n');
60    prompt.push_str(PLANNING_WORKFLOW_READ_ONLY_HEADER);
61    prompt.push('\n');
62    prompt.push_str(PLANNING_WORKFLOW_READ_ONLY_NOTICE_LINE);
63    prompt.push('\n');
64    prompt.push_str(PLANNING_WORKFLOW_EXIT_INSTRUCTION_LINE);
65    prompt.push('\n');
66    prompt.push_str(PLANNING_WORKFLOW_PLAN_PERSISTENCE_POLICY_LINE);
67    prompt.push('\n');
68    prompt.push_str(PLANNING_WORKFLOW_PLAN_POLICY_LINE);
69    prompt.push('\n');
70    prompt.push_str(PLANNING_WORKFLOW_PLAN_QUALITY_LINE);
71    prompt.push('\n');
72    prompt.push_str(PLANNING_WORKFLOW_RESEARCH_SCOPE_LINE);
73    prompt.push('\n');
74    prompt.push_str(PLANNING_WORKFLOW_INTERVIEW_POLICY_LINE);
75    prompt.push('\n');
76    if !request_user_input_enabled {
77        prompt.push_str(PLANNING_WORKFLOW_NO_REQUEST_USER_INPUT_POLICY_LINE);
78        prompt.push('\n');
79    }
80    prompt.push_str(PLANNING_WORKFLOW_NO_AUTO_EXIT_LINE);
81    prompt.push('\n');
82    prompt.push_str(PLANNING_WORKFLOW_TASK_TRACKER_LINE);
83    prompt.push('\n');
84}
85
86#[cfg(test)]
87mod tests {
88    use super::{RuntimePromptContract, append_runtime_mode_sections};
89    use crate::prompts::system::{
90        PLANNING_WORKFLOW_INTERVIEW_POLICY_LINE, PLANNING_WORKFLOW_READ_ONLY_HEADER,
91        PLANNING_WORKFLOW_RESEARCH_SCOPE_LINE,
92    };
93
94    #[test]
95    fn planning_workflow_uses_plan_policy_unconditionally() {
96        for request_user_input_enabled in [true, false] {
97            let mut prompt = "Base prompt".to_string();
98
99            append_runtime_mode_sections(
100                &mut prompt,
101                RuntimePromptContract {
102                    planning_active: true,
103                    request_user_input_enabled,
104                    ..RuntimePromptContract::default()
105                },
106            );
107
108            assert!(prompt.contains(PLANNING_WORKFLOW_READ_ONLY_HEADER));
109            assert!(prompt.contains(PLANNING_WORKFLOW_INTERVIEW_POLICY_LINE));
110            assert!(prompt.contains("Emit exactly one final `<proposed_plan>` block"));
111            assert!(
112                prompt.contains("Do not use shell commands or file-writing tools to create or modify `.vtcode/plans/`")
113            );
114            assert!(prompt.contains("runtime owns plan/tracker persistence and validation"));
115            assert!(prompt.contains("approval controls only after successful persistence"));
116        }
117    }
118
119    #[test]
120    fn full_auto_notice_mentions_missing_request_user_input_when_disabled() {
121        let mut prompt = "Base prompt".to_string();
122
123        append_runtime_mode_sections(
124            &mut prompt,
125            RuntimePromptContract {
126                full_auto: true,
127                request_user_input_enabled: false,
128                ..RuntimePromptContract::default()
129            },
130        );
131
132        assert!(prompt.contains("# FULL-AUTO: Complete task autonomously until done or blocked."));
133        assert!(prompt.contains("`request_user_input` is unavailable in this runtime"));
134    }
135
136    /// Regression guard for checkpoint turn_647: a simple planning request
137    /// burned 70+ tool calls until the wall-clock budget was exhausted with
138    /// no plan delivered. The research-scope line must always be present
139    /// while planning is active, regardless of `request_user_input`
140    /// availability, so the model has a concrete signal to stop researching
141    /// and draft.
142    #[test]
143    fn planning_workflow_always_includes_research_scope_guidance() {
144        for request_user_input_enabled in [true, false] {
145            let mut prompt = "Base prompt".to_string();
146            append_runtime_mode_sections(
147                &mut prompt,
148                RuntimePromptContract {
149                    planning_active: true,
150                    request_user_input_enabled,
151                    ..RuntimePromptContract::default()
152                },
153            );
154            assert!(prompt.contains(PLANNING_WORKFLOW_RESEARCH_SCOPE_LINE));
155            assert!(!prompt.contains("tool-call research floor"));
156        }
157    }
158
159    #[test]
160    fn planning_workflow_warns_about_dynamic_find_expansion() {
161        let mut prompt = "Base prompt".to_string();
162
163        append_runtime_mode_sections(
164            &mut prompt,
165            RuntimePromptContract {
166                planning_active: true,
167                ..RuntimePromptContract::default()
168            },
169        );
170
171        assert!(prompt.contains("for `find`, use literal paths and quoted patterns only"));
172        assert!(prompt.contains("prefer `rg --files`"));
173        assert!(prompt.contains(
174            "keep them free of `$()`, backticks, variable or brace expansion, and dynamically spliced options"
175        ));
176    }
177}