use crate::command::chat::app::types::PlanDecision;
use crate::command::chat::app::{AskOption, AskQuestion, AskRequest};
use crate::command::chat::tools::{Tool, ToolResult, schema_to_tool_params};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;
use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex, atomic::AtomicBool, mpsc};
pub struct PendingPlanApproval {
pub agent_name: String,
pub plan_content: String,
pub plan_name: String,
decision: Arc<(Mutex<Option<PlanDecision>>, Condvar)>,
}
impl PendingPlanApproval {
pub fn new(agent_name: String, plan_content: String, plan_name: String) -> Arc<Self> {
Arc::new(Self {
agent_name,
plan_content,
plan_name,
decision: Arc::new((Mutex::new(None), Condvar::new())),
})
}
pub fn wait_for_decision(&self, timeout_secs: u64) -> PlanDecision {
let (lock, cvar) = &*self.decision;
let guard = lock.lock().unwrap();
let (mut guard, _timed_out) = cvar
.wait_timeout_while(guard, std::time::Duration::from_secs(timeout_secs), |d| {
d.is_none()
})
.unwrap();
if guard.is_none() {
*guard = Some(PlanDecision::Reject);
}
guard.clone().unwrap_or(PlanDecision::Reject)
}
pub fn resolve(&self, decision: PlanDecision) {
let (lock, cvar) = &*self.decision;
let mut d = lock.lock().unwrap();
*d = Some(decision);
cvar.notify_one();
}
#[allow(dead_code)]
pub fn is_decided(&self) -> bool {
self.decision.0.lock().unwrap().is_some()
}
}
pub struct PlanApprovalQueue {
pending: Mutex<VecDeque<Arc<PendingPlanApproval>>>,
}
impl Default for PlanApprovalQueue {
fn default() -> Self {
Self::new()
}
}
impl PlanApprovalQueue {
pub fn new() -> Self {
Self {
pending: Mutex::new(VecDeque::new()),
}
}
pub fn request_blocking(&self, req: Arc<PendingPlanApproval>) -> PlanDecision {
{
let mut q = self.pending.lock().unwrap();
q.push_back(Arc::clone(&req));
}
req.wait_for_decision(120)
}
pub fn pop_pending(&self) -> Option<Arc<PendingPlanApproval>> {
self.pending.lock().unwrap().pop_front()
}
pub fn deny_all(&self) {
let mut q = self.pending.lock().unwrap();
for req in q.drain(..) {
req.resolve(PlanDecision::Reject);
}
}
}
struct PlanModeInner {
active: bool,
plan_file_path: Option<String>,
}
pub struct PlanModeState {
inner: Mutex<PlanModeInner>,
}
impl Default for PlanModeState {
fn default() -> Self {
Self::new()
}
}
impl PlanModeState {
pub fn new() -> Self {
Self {
inner: Mutex::new(PlanModeInner {
active: false,
plan_file_path: None,
}),
}
}
pub fn is_active(&self) -> bool {
self.inner.lock().map(|g| g.active).unwrap_or(false)
}
pub fn enter(&self, path: String) -> Result<(), String> {
match self.inner.lock() {
Ok(mut guard) => {
if guard.active {
return Err("Already in plan mode. Use ExitPlanMode to exit.".to_string());
}
guard.active = true;
guard.plan_file_path = Some(path);
Ok(())
}
Err(e) => Err(format!("Lock poisoned: {}", e)),
}
}
pub fn exit(&self) {
if let Ok(mut guard) = self.inner.lock() {
guard.active = false;
guard.plan_file_path = None;
}
}
pub fn get_state(&self) -> (bool, Option<String>) {
match self.inner.lock() {
Ok(guard) => (guard.active, guard.plan_file_path.clone()),
Err(_) => (false, None),
}
}
pub fn get_plan_file_path(&self) -> Option<String> {
self.inner.lock().ok()?.plan_file_path.clone()
}
}
pub const PLAN_MODE_WHITELIST: &[&str] = &[
"Read",
"Glob",
"Grep",
"WebFetch",
"WebSearch",
"Ask",
"Compact",
"TodoRead",
"TodoWrite",
"TaskOutput",
"Task",
"EnterPlanMode",
"ExitPlanMode",
"EnterWorktree",
"ExitWorktree",
];
pub fn is_allowed_in_plan_mode(tool_name: &str) -> bool {
PLAN_MODE_WHITELIST.contains(&tool_name)
}
fn sanitize_filename(s: &str) -> String {
s.chars()
.filter(|c| c.is_alphanumeric() || *c == '_' || *c == '-' || *c > '\u{4e00}')
.collect::<String>()
.trim()
.to_string()
}
#[derive(Deserialize, JsonSchema)]
struct EnterPlanModeParams {
#[serde(default)]
description: Option<String>,
}
pub struct EnterPlanModeTool {
pub plan_state: Arc<PlanModeState>,
}
impl EnterPlanModeTool {
pub const NAME: &'static str = "EnterPlanMode";
}
impl Tool for EnterPlanModeTool {
fn name(&self) -> &str {
Self::NAME
}
fn description(&self) -> &str {
r#"
Enter plan mode to explore the codebase and design an implementation approach before writing code.
In plan mode, only read-only tools (Read, Glob, Grep, WebFetch, WebSearch, Ask, etc.) are available.
Write tools (Bash, Write, Edit, etc.) will be blocked until plan mode is exited.
Use this proactively before starting non-trivial implementation tasks. Prefer using EnterPlanMode when ANY of these apply:
- New feature implementation with architectural decisions
- Multiple valid approaches exist and user should choose
- Code modifications that affect existing behavior
- Multi-file changes (touching more than 2-3 files)
- Unclear requirements that need exploration first
Do NOT use for: single-line fixes, typos, or purely research/exploration tasks.
The `description` parameter is used as the plan file name (e.g. "add-auth" → plan-add-auth.md).
If a plan file with the same name already exists, you will be warned so you can choose a different name.
Plan files are preserved after exiting plan mode for future reference.
"#
}
fn parameters_schema(&self) -> Value {
schema_to_tool_params::<EnterPlanModeParams>()
}
fn execute(&self, arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
let params: EnterPlanModeParams =
serde_json::from_str(arguments).unwrap_or(EnterPlanModeParams { description: None });
let description = params
.description
.as_deref()
.unwrap_or("implementation-plan");
let plan_dir = crate::command::chat::permission::JcliConfig::ensure_config_dir()
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default().join(".jcli"));
let plans_dir = plan_dir.join("plans");
let _ = std::fs::create_dir_all(&plans_dir);
let safe_name = sanitize_filename(description);
let file_name = if safe_name.is_empty() {
format!("plan-{}.md", std::process::id())
} else {
format!("plan-{}.md", safe_name)
};
let plan_file = plans_dir.join(&file_name);
let plan_path = plan_file.display().to_string();
let mut warning = String::new();
if plan_file.exists() {
match std::fs::read_to_string(&plan_file) {
Ok(existing) => {
let first_line = existing.lines().next().unwrap_or("");
warning = format!(
"⚠️ Plan file already exists: {} (content starts with: {})\n\
The existing file will be overwritten. Consider using a different description to avoid this.\n\n",
plan_path, first_line
);
}
Err(_) => {
warning = format!(
"⚠️ Plan file already exists: {}\n\
The existing file will be overwritten. Consider using a different description to avoid this.\n\n",
plan_path
);
}
}
}
let template = format!("# Plan: {}\n\n## Steps\n\n1. \n\n## Notes\n\n", description);
let _ = std::fs::write(&plan_file, &template);
match self.plan_state.enter(plan_path.clone()) {
Ok(()) => ToolResult {
output: format!(
"{}Entered plan mode. Plan file: {}\n\
In plan mode, only read-only tools are available.\n\
Write your plan to the plan file, then use ExitPlanMode when ready for user approval.\n\
Plan files are preserved after exit for future reference.",
warning, plan_path
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::None,
},
Err(msg) => ToolResult {
output: msg,
is_error: false,
images: vec![],
plan_decision: PlanDecision::None,
},
}
}
fn requires_confirmation(&self) -> bool {
false
}
}
#[derive(Deserialize, JsonSchema)]
#[allow(dead_code)]
struct ExitPlanModeParams {
#[serde(default)]
#[serde(rename = "allowedPrompts")]
allowed_prompts: Option<Vec<AllowedPrompt>>,
}
#[derive(Deserialize, JsonSchema)]
#[allow(dead_code)]
struct AllowedPrompt {
#[serde(default)]
tool: Option<String>,
#[serde(default)]
prompt: Option<String>,
}
pub struct ExitPlanModeTool {
pub plan_state: Arc<PlanModeState>,
pub ask_tx: mpsc::Sender<AskRequest>,
pub plan_approval_queue: Option<Arc<PlanApprovalQueue>>,
}
impl ExitPlanModeTool {
pub const NAME: &'static str = "ExitPlanMode";
}
impl Tool for ExitPlanModeTool {
fn name(&self) -> &str {
Self::NAME
}
fn description(&self) -> &str {
r#"
Exit plan mode and submit the plan for user approval.
Reads the plan file and presents it to the user for review.
If approved, plan mode is deactivated and write tools become available again.
If rejected, plan mode remains active so you can revise the plan.
"#
}
fn parameters_schema(&self) -> Value {
schema_to_tool_params::<ExitPlanModeParams>()
}
fn execute(&self, _arguments: &str, _cancelled: &Arc<AtomicBool>) -> ToolResult {
if !self.plan_state.is_active() {
return ToolResult {
output: "Not in plan mode. Use EnterPlanMode first.".to_string(),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
};
}
let plan_content = match self.plan_state.get_plan_file_path() {
Some(path) => match std::fs::read_to_string(&path) {
Ok(content) => content,
Err(e) => {
return ToolResult {
output: format!("Failed to read plan file: {}", e),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
};
}
},
None => {
return ToolResult {
output: "No plan file path set.".to_string(),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
};
}
};
let agent_name = crate::command::chat::teammate::current_agent_name();
if agent_name != "Main" {
if let Some(ref queue) = self.plan_approval_queue {
let plan_file_path = self.plan_state.get_plan_file_path().unwrap_or_default();
let plan_name = std::path::Path::new(&plan_file_path)
.file_stem()
.and_then(|s| s.to_str())
.and_then(|s| s.strip_prefix("plan-"))
.unwrap_or("Plan")
.to_string();
let req = PendingPlanApproval::new(agent_name, plan_content.clone(), plan_name);
let decision = queue.request_blocking(req);
match decision {
PlanDecision::Approve => {
let plan_file_path = self.plan_state.get_plan_file_path();
self.plan_state.exit();
let preserved_msg = plan_file_path
.as_deref()
.map(|p| format!("\nPlan file preserved at: {}", p))
.unwrap_or_default();
ToolResult {
output: format!(
"Plan approved! Exited plan mode. You can now proceed with implementation.{}",
preserved_msg
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::Approve,
}
}
PlanDecision::ApproveAndClearContext => {
let plan_file_path = self.plan_state.get_plan_file_path();
self.plan_state.exit();
let preserved_msg = plan_file_path
.as_deref()
.map(|p| format!("\nPlan file preserved at: {}", p))
.unwrap_or_default();
ToolResult {
output: format!(
"Plan approved with context clear! Exited plan mode.{}",
preserved_msg
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::ApproveAndClearContext,
}
}
PlanDecision::Reject | PlanDecision::None => {
ToolResult {
output: "Plan was not approved. Still in plan mode. Please revise your plan and try ExitPlanMode again.".to_string(),
is_error: false,
images: vec![],
plan_decision: PlanDecision::Reject,
}
}
}
} else {
ToolResult {
output: "Plan approval not available in sub-agent mode (no queue). Add permission rules to avoid plan mode in teammates.".to_string(),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
}
}
} else {
self.execute_via_ask_tx(&plan_content)
}
}
fn requires_confirmation(&self) -> bool {
false
}
}
impl ExitPlanModeTool {
fn execute_via_ask_tx(&self, plan_content: &str) -> ToolResult {
let (response_tx, response_rx) = mpsc::channel::<String>();
let question_text = format!("请审阅以下实施计划,选择操作:\n\n{}", plan_content);
let plan_file_path = self.plan_state.get_plan_file_path().unwrap_or_default();
let plan_name = std::path::Path::new(&plan_file_path)
.file_stem()
.and_then(|s| s.to_str())
.and_then(|s| s.strip_prefix("plan-"))
.unwrap_or("Plan");
let ask_request = AskRequest {
questions: vec![AskQuestion {
question: question_text,
header: plan_name.to_string(),
options: vec![
AskOption {
label: "批准计划".to_string(),
description: "批准此计划,保留当前上下文,开始实施".to_string(),
},
AskOption {
label: "批准并清空上下文".to_string(),
description: "批准计划并清空探索过程中的对话上下文,仅保留计划内容继续实施"
.to_string(),
},
AskOption {
label: "驳回计划".to_string(),
description: "拒绝此计划,留在 Plan Mode 中修改方案".to_string(),
},
],
multi_select: false,
}],
response_tx,
};
if self.ask_tx.send(ask_request).is_err() {
return ToolResult {
output: "Failed to send approval request (main thread may have exited)".to_string(),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
};
}
match response_rx.recv() {
Ok(response) => {
if response.contains("批准并清空上下文") {
let plan_file_path = self.plan_state.get_plan_file_path();
self.plan_state.exit();
let preserved_msg = plan_file_path
.as_deref()
.map(|p| format!("\nPlan file preserved at: {}", p))
.unwrap_or_default();
ToolResult {
output: format!(
"Plan approved with context clear! Exited plan mode.{}",
preserved_msg
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::ApproveAndClearContext,
}
} else if response.contains("批准") {
let plan_file_path = self.plan_state.get_plan_file_path();
self.plan_state.exit();
let preserved_msg = plan_file_path
.as_deref()
.map(|p| format!("\nPlan file preserved at: {}", p))
.unwrap_or_default();
ToolResult {
output: format!(
"Plan approved! Exited plan mode. You can now proceed with implementation.{}",
preserved_msg
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::Approve,
}
} else {
ToolResult {
output: format!(
"Plan was not approved. Still in plan mode. User response: {}\nPlease revise your plan and try ExitPlanMode again.",
response
),
is_error: false,
images: vec![],
plan_decision: PlanDecision::Reject,
}
}
}
Err(_) => ToolResult {
output: "Connection lost while waiting for approval".to_string(),
is_error: true,
images: vec![],
plan_decision: PlanDecision::None,
},
}
}
}