#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PlanTrigger {
AutoCreate(String),
Suggest(String),
None,
}
impl PlanTrigger {
pub fn should_plan(&self) -> bool {
!matches!(self, PlanTrigger::None)
}
pub fn is_auto_create(&self) -> bool {
matches!(self, PlanTrigger::AutoCreate(_))
}
pub fn reason(&self) -> Option<&str> {
match self {
PlanTrigger::AutoCreate(r) | PlanTrigger::Suggest(r) => Some(r),
PlanTrigger::None => None,
}
}
}
pub fn should_create_plan(user_message: &str) -> PlanTrigger {
let trimmed = user_message.trim();
if trimmed.is_empty() {
return PlanTrigger::None;
}
let lower = trimmed.to_ascii_lowercase();
if let Some(rest) = lower.strip_prefix("/plan auto") {
let reason = rest.trim();
return PlanTrigger::AutoCreate(if reason.is_empty() {
"explicit /plan auto".to_string()
} else {
reason.to_string()
});
}
if let Some(rest) = lower.strip_prefix("/plan suggest") {
let reason = rest.trim();
return PlanTrigger::Suggest(if reason.is_empty() {
"explicit /plan suggest".to_string()
} else {
reason.to_string()
});
}
if let Some(reason) = parse_plan_marker(trimmed, "PLAN_AUTO") {
return PlanTrigger::AutoCreate(reason);
}
if let Some(reason) = parse_plan_marker(trimmed, "PLAN_SUGGEST") {
return PlanTrigger::Suggest(reason);
}
PlanTrigger::None
}
fn parse_plan_marker(text: &str, marker: &str) -> Option<String> {
let upper = text.to_ascii_uppercase();
let open = format!("[{}", marker);
let start = upper.find(&open)?;
let rest = &text[start + open.len()..];
let end = rest.find(']')?;
let inside = rest[..end].trim();
if inside.is_empty() {
return Some(format!("explicit {}", marker.to_ascii_lowercase()));
}
if let Some(reason) = inside.strip_prefix(':') {
let reason = reason.trim();
if reason.is_empty() {
return Some(format!("explicit {}", marker.to_ascii_lowercase()));
}
return Some(reason.to_string());
}
None
}
pub fn get_plan_suggestion_prompt(trigger: &PlanTrigger) -> Option<String> {
match trigger {
PlanTrigger::Suggest(reason) => Some(format!(
"[SYSTEM] This looks like a {} that requires structured execution. \
FIRST call the track_requirements tool with the full checklist of concrete requirements \
for this request (include deliverables like sending a file). As you finish each one, call \
track_requirements again with that item marked 'completed'. For each step that modifies \
external state (deploys, publishes, sends, pushes), include a verification step that confirms \
the change was applied correctly. Check prerequisites (committed changes, installed \
dependencies, correct configuration) before executing mutations. Do not claim you are done \
while any item is still pending, and never claim success without verification.",
reason
)),
PlanTrigger::AutoCreate(_) => None,
PlanTrigger::None => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_suggest_hint_instructs_track_requirements() {
let hint = get_plan_suggestion_prompt(&PlanTrigger::Suggest("deployment".to_string()))
.expect("suggest trigger should yield a hint");
assert!(
hint.contains("track_requirements"),
"hint must steer the model to the checklist tool: {hint}"
);
assert!(get_plan_suggestion_prompt(&PlanTrigger::None).is_none());
}
#[test]
fn test_explicit_auto_command() {
let trigger = should_create_plan("/plan auto production deployment");
assert_eq!(
trigger,
PlanTrigger::AutoCreate("production deployment".to_string())
);
}
#[test]
fn test_explicit_suggest_command() {
let trigger = should_create_plan("/plan suggest split into phases");
assert_eq!(
trigger,
PlanTrigger::Suggest("split into phases".to_string())
);
}
#[test]
fn test_explicit_auto_marker_with_reason() {
let trigger = should_create_plan("Please do this. [PLAN_AUTO: high-risk change]");
assert_eq!(
trigger,
PlanTrigger::AutoCreate("high-risk change".to_string())
);
}
#[test]
fn test_explicit_suggest_marker_without_reason() {
let trigger = should_create_plan("Please walk through this [PLAN_SUGGEST]");
assert_eq!(
trigger,
PlanTrigger::Suggest("explicit plan_suggest".to_string())
);
}
#[test]
fn ordinary_language_never_routes_through_phrase_lists() {
assert_eq!(should_create_plan("Deploy the app"), PlanTrigger::None);
assert_eq!(should_create_plan("fix the bug"), PlanTrigger::None);
assert_eq!(should_create_plan("what time is it"), PlanTrigger::None);
assert_eq!(
should_create_plan(
"Publish the new version of the package to npm and update the changelog",
),
PlanTrigger::None
);
assert_eq!(
should_create_plan(
"First commit the changes, then build, verify, and deploy the release"
),
PlanTrigger::None
);
}
#[test]
fn test_plan_suggestion_prompt_content() {
let trigger = PlanTrigger::Suggest("multi-step task".to_string());
let prompt = get_plan_suggestion_prompt(&trigger);
assert!(prompt.is_some());
let text = prompt.unwrap();
assert!(text.contains("structured execution"));
assert!(text.contains("verification"));
assert!(text.contains("prerequisites"));
let trigger = PlanTrigger::None;
assert!(get_plan_suggestion_prompt(&trigger).is_none());
}
}