Skip to main content

vtcode_core/planning/
mod.rs

1//! Canonical plan-mode enter/exit phrase sets and matchers.
2//!
3//! Plan-mode intent detection lives in two consumers — the runloop
4//! (`detect_planning_intent`) and the Codex app-server bridge
5//! (`normalize_planning_input`). Both must agree on which phrases enter,
6//! exit, or stay in plan mode, so the phrase literals live here as the single
7//! source of truth. Each consumer keeps its own matching strategy (the runloop
8//! matches natural-language substrings; the Codex bridge matches bare tokens
9//! exactly) but draws the literals from this module so they cannot drift apart.
10
11/// Short imperative commands that exit plan mode and start implementing.
12/// Matched exactly (whole-trimmed) by the runloop, and recognized by the Codex
13/// bridge as implementation aliases.
14pub const EXIT_DIRECT_COMMANDS: &[&str] = &[
15    "implement",
16    "yes",
17    "go",
18    "start",
19    "approve",
20    "approved",
21    "implement now",
22    "start implementing",
23    "start implementation",
24    "execute plan",
25    "execute the plan",
26    "execute this plan",
27    "switch to agent mode",
28    "exit planning workflow",
29    "exit planning workflow and implement",
30];
31
32/// Whole-word approval tokens (so "disapprove" does not match "approve").
33pub const APPROVAL_WORDS: &[&str] = &["approve", "approved", "lgtm", "accepted", "accept"];
34
35/// Multi-word approval phrases.
36pub const APPROVAL_PHRASES: &[&str] = &[
37    "approve the plan",
38    "approve this plan",
39    "approve the proposed plan",
40    "looks good",
41    "ship it",
42    "accept the plan",
43    "accept this plan",
44];
45
46/// Longer exit trigger phrases (matched as substrings).
47pub const EXIT_TRIGGER_PHRASES: &[&str] = &[
48    "start implement",
49    "start implementation",
50    "start implementing",
51    "implement now",
52    "implement the plan",
53    "implement this plan",
54    "begin implement",
55    "begin implementation",
56    "begin coding",
57    "proceed to implement",
58    "proceed with implementation",
59    "proceed to coding",
60    "proceed with coding",
61    "execute the plan",
62    "execute this plan",
63    "let s implement",
64    "lets implement",
65    "go ahead and implement",
66    "go ahead and code",
67    "ready to implement",
68    "start coding",
69    "start building",
70    "switch to agent mode",
71    "exit planning workflow",
72    "exit planning workflow and implement",
73];
74
75/// Phrases that explicitly keep the user in plan mode (highest priority).
76/// `edit` and `revise` are the short-form replies to the yes/no/edit HITL
77/// prompt surfaced when `request_user_input` is unavailable — they route the
78/// user back into planning revision without requiring the longer "keep
79/// planning" phrase (checkpoint turn_725).
80pub const STAY_PHRASES: &[&str] = &[
81    "stay in planning workflow",
82    "keep in planning workflow",
83    "continue planning",
84    "keep planning",
85    "do not implement",
86    "don t implement",
87    "not ready to implement",
88    "don t exit planning workflow",
89    "do not exit planning workflow",
90    "edit the plan",
91    "edit plan",
92    "revise the plan",
93    "revise plan",
94];
95
96/// Phrases that enter plan mode (does not include the `/plan` slash command,
97/// which callers check separately).
98pub const ENTER_PHRASES: &[&str] = &[
99    "make a plan",
100    "create a plan",
101    "write a plan",
102    "come up with a plan",
103    "plan this",
104    "use plan mode",
105    "use planning mode",
106    "enter plan mode",
107    "switch to plan mode",
108    "stay in planning workflow",
109    "keep planning",
110    "continue planning",
111    "before you implement make a plan",
112    "before implementing make a plan",
113    "outline the implementation plan",
114];
115
116/// Assistant cues that indicate a recent implementation prompt.
117pub const IMPLEMENTATION_CUES: &[&str] = &[
118    "implement this plan",
119    "implement the plan",
120    "ready to implement",
121    "exit planning workflow",
122    "execute the plan",
123    "switch out of planning workflow",
124    "start implementation",
125    "start implementing",
126    "start coding",
127];
128
129/// Aliases that clear the planning-active flag AND switch to execution mode.
130/// Used by the Codex bridge's bare-token exact matching. Includes the
131/// implementation intents that the runloop recognizes so the two paths cannot
132/// drift (previously the bridge silently missed `approve`/`lgtm`/`ship it`).
133pub const EXECUTION_MODE_ALIASES: &[&str] = &[
134    "implement",
135    "continue",
136    "go",
137    "start",
138    "yes",
139    "approve",
140    "approved",
141    "lgtm",
142    "accepted",
143    "accept",
144    "ship it",
145    "implement now",
146    "start implementing",
147    "start implementation",
148    "execute plan",
149    "execute the plan",
150    "execute this plan",
151    "switch to agent mode",
152    "exit planning workflow and implement",
153];
154
155/// Aliases that clear the planning-active flag but are NOT implementation
156/// intents — they pass through verbatim rather than being rewritten to the
157/// execution prompt.
158pub const FLAG_CLEARING_ONLY_ALIASES: &[&str] = &["exit planning workflow"];
159
160/// Normalize user text for plan-mode intent matching: lowercase and replace
161/// non-alphanumeric characters with spaces for flexible substring matching.
162pub fn normalize_plan_intent(text: &str) -> String {
163    text.chars()
164        .map(|c| {
165            if c.is_alphanumeric() {
166                c.to_ascii_lowercase()
167            } else {
168                ' '
169            }
170        })
171        .collect()
172}
173
174fn is_plan_intent_negation(word: &str) -> bool {
175    matches!(word, "not" | "never" | "no" | "avoid" | "without" | "t")
176}
177
178fn has_unnegated_phrase(normalized: &str, phrase: &str, whole_word: bool) -> bool {
179    normalized.match_indices(phrase).any(|(index, _)| {
180        let Some(prefix) = normalized.get(..index) else {
181            return false;
182        };
183        let Some(suffix) = normalized.get(index..).and_then(|rest| rest.strip_prefix(phrase)) else {
184            return false;
185        };
186        if whole_word
187            && (prefix.chars().next_back().is_some_and(|character| !character.is_whitespace())
188                || suffix.chars().next().is_some_and(|character| !character.is_whitespace()))
189        {
190            return false;
191        }
192        !prefix
193            .split_whitespace()
194            .rev()
195            .take_while(|word| !matches!(*word, "but" | "however" | "instead" | "then"))
196            .any(is_plan_intent_negation)
197    })
198}
199
200/// Whether the normalized text expresses an exit-and-implement intent.
201///
202/// Mirrors the runloop's detection priority: direct commands (exact), approval
203/// words (whole-word), approval phrases (substring), then trigger phrases
204/// (substring). The caller must check [`matches_stay_intent`] first so that
205/// stay phrases win.
206pub fn matches_exit_intent(normalized: &str) -> bool {
207    let trimmed = normalized.trim();
208    EXIT_DIRECT_COMMANDS.contains(&trimmed)
209        || APPROVAL_WORDS.iter().any(|word| has_unnegated_phrase(normalized, word, true))
210        || APPROVAL_PHRASES
211            .iter()
212            .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
213        || EXIT_TRIGGER_PHRASES
214            .iter()
215            .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
216}
217
218/// Whether the normalized text keeps the user in plan mode (highest priority).
219pub fn matches_stay_intent(normalized: &str) -> bool {
220    STAY_PHRASES.iter().any(|phrase| normalized.contains(phrase))
221}
222
223/// Whether the normalized text is an explicit request to enter plan mode
224/// (excluding the `/plan` slash command, which callers check separately).
225pub fn matches_enter_intent(normalized: &str) -> bool {
226    ENTER_PHRASES
227        .iter()
228        .any(|phrase| has_unnegated_phrase(normalized, phrase, false))
229}
230
231/// Whether the normalized assistant text contains an implementation prompt cue.
232pub fn contains_implementation_cue(normalized: &str) -> bool {
233    IMPLEMENTATION_CUES.iter().any(|cue| normalized.contains(cue))
234}
235
236/// Codex-bridge matcher: bare-token exact match against the execution/flag
237/// alias sets (preserves the bridge's verbatim-input contract).
238pub fn is_execution_mode_alias(input: &str) -> bool {
239    let normalized = input.trim().to_ascii_lowercase();
240    EXECUTION_MODE_ALIASES
241        .iter()
242        .chain(FLAG_CLEARING_ONLY_ALIASES.iter())
243        .any(|alias| *alias == normalized)
244}
245
246/// Whether a Codex-bridge alias is an implementation intent (rewrite to the
247/// execution prompt) vs. a flag-clearing-only alias (pass through verbatim).
248pub fn is_implementation_alias(input: &str) -> bool {
249    let normalized = input.trim().to_ascii_lowercase();
250    EXECUTION_MODE_ALIASES.iter().any(|alias| *alias == normalized)
251}
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    fn is_exit(text: &str) -> bool {
258        matches_exit_intent(&normalize_plan_intent(text))
259    }
260
261    fn is_enter(text: &str) -> bool {
262        matches_enter_intent(&normalize_plan_intent(text))
263    }
264
265    #[test]
266    fn normalization_splits_contractions_for_negation_matching() {
267        // The `t` negation entry depends on apostrophes splitting words.
268        assert_eq!(normalize_plan_intent("Don't!"), "don t ");
269    }
270
271    #[test]
272    fn exit_intent_matches_unnegated_approvals_and_triggers() {
273        for text in [
274            "approve",
275            "looks good to me",
276            "lgtm",
277            "please proceed to implement now",
278            "do not cancel but approve",
279        ] {
280            assert!(is_exit(text), "{text}");
281        }
282    }
283
284    #[test]
285    fn exit_intent_rejects_negated_approvals_and_lookalikes() {
286        for text in [
287            "disapprove",
288            "do not approve yet",
289            "don't approve",
290            "never approve without asking",
291            "no changes",
292        ] {
293            assert!(!is_exit(text), "{text}");
294        }
295    }
296
297    #[test]
298    fn enter_intent_matches_plan_mode_requests() {
299        for text in [
300            "use plan mode, refine readme",
301            "please make a plan first",
302            "switch to plan mode",
303        ] {
304            assert!(is_enter(text), "{text}");
305        }
306    }
307
308    #[test]
309    fn enter_intent_rejects_negated_requests() {
310        for text in ["don't use plan mode", "do not make a plan, just implement it"] {
311            assert!(!is_enter(text), "{text}");
312        }
313    }
314}