diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 8008aaf..8582e7c 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -1016,7 +1016,7 @@ async fn main() -> Result<()> { { // children get the parent's tools as of now (no `task`, so no recursion) let mut general = agent.tools.clone(); - general.retain(|n| n != "ask_user"); + general.retain(|n| n != "ask_user" && n != "exit_plan"); let mut explore = agent.tools.clone(); explore.retain(|n| hx_core::subagent::EXPLORE_TOOLS.contains(&n)); let role = |model: String, tools, base: &str| hx_core::subagent::Role { diff --git a/crates/hx-core/src/command.rs b/crates/hx-core/src/command.rs index 96c7bfe..7d1ee3a 100644 --- a/crates/hx-core/src/command.rs +++ b/crates/hx-core/src/command.rs @@ -82,6 +82,11 @@ pub fn builtins(models: &[&str]) -> Vec { "auto permissions: refuse dangerous calls, run safe ones", Enum(vec!["on".into(), "off".into(), "log".into()]), ), + b( + "plan", + "plan mode: read-only research, then the full plan for approval", + Enum(vec!["on".into(), "off".into()]), + ), b("yolo", "toggle: run every tool without asking", None), b( "sandbox", diff --git a/crates/hx-core/src/permit.rs b/crates/hx-core/src/permit.rs index f66e3e4..ef5a011 100644 --- a/crates/hx-core/src/permit.rs +++ b/crates/hx-core/src/permit.rs @@ -328,6 +328,8 @@ pub struct Allow { all_edits: bool, yolo: bool, auto: bool, + /// Plan mode: read-only until the user approves a plan. + plan: bool, /// Last calls auto mode refused (newest last), for `/auto`. blocks: Vec, file: Option, @@ -345,6 +347,7 @@ impl Allow { all_edits: false, yolo: false, auto: false, + plan: false, blocks: Vec::new(), file: Some(file), } @@ -381,6 +384,14 @@ impl Allow { self.yolo } + pub fn plan_on(&self) -> bool { + self.plan + } + + pub fn set_plan(&mut self, on: bool) { + self.plan = on; + } + pub fn auto_on(&self) -> bool { self.auto } diff --git a/crates/hx-core/src/tool.rs b/crates/hx-core/src/tool.rs index e08793e..c5171c9 100644 --- a/crates/hx-core/src/tool.rs +++ b/crates/hx-core/src/tool.rs @@ -93,6 +93,16 @@ pub async fn gate_why(cx: &ToolCtx, req: crate::permit::Request) -> Result<(), S use crate::danger::{Verdict, classify}; use crate::permit::Decision; const DENIED: &str = "denied by the user; do not retry, ask what to do instead"; + if matches!(req.tool.as_str(), "bash" | "write" | "edit") + && cx + .state + .allow + .lock() + .unwrap_or_else(|e| e.into_inner()) + .plan_on() + { + return Err("blocked by plan mode: read-only until the plan is approved. Explore with read/grep/ls, then call exit_plan with the full plan.".into()); + } let Some(ask) = cx.state.permit.get() else { return Ok(()); }; @@ -263,6 +273,27 @@ mod tests { assert!(d.is_error && d.content.contains("denied")); } + #[tokio::test] + async fn plan_mode_blocks_writes_even_without_prompt() { + let c = cx(); + c.state.allow.lock().unwrap().set_plan(true); + let e = gate_why( + &c, + crate::permit::request("bash", &json!({"command":"touch x"})), + ) + .await; + assert!(e.unwrap_err().contains("plan mode")); + c.state.allow.lock().unwrap().set_plan(false); + assert!( + gate_why( + &c, + crate::permit::request("bash", &json!({"command":"touch x"})) + ) + .await + .is_ok() + ); + } + #[tokio::test] async fn auto_mode_blocks_allows_judges_without_prompting() { use crate::danger::Judge; diff --git a/crates/hx-tools/src/lib.rs b/crates/hx-tools/src/lib.rs index 45e1f9c..4f3282d 100644 --- a/crates/hx-tools/src/lib.rs +++ b/crates/hx-tools/src/lib.rs @@ -66,6 +66,7 @@ pub fn registry() -> Registry { .add(Edit) .add(Bash) .add(AskUser) + .add(ExitPlan) .add(MemoryWrite) .add(MemoryForget) .add(skill::Skill); @@ -324,6 +325,51 @@ impl Tool for AskUser { } } +/// Plan mode's end: the TUI shows the plan in full, the user approves or sends feedback. +pub struct ExitPlan; +#[async_trait] +impl Tool for ExitPlan { + fn spec(&self) -> ToolSpec { + spec( + "exit_plan", + "Only in plan mode: present your finished plan (markdown, complete) for approval. Plan mode ends once the user approves.", + json!({"type":"object","properties":{"plan":{"type":"string"}},"required":["plan"]}), + ) + } + async fn call(&self, _: Value, cx: &ToolCtx) -> ToolResult { + let on = || cx.state.allow.lock().unwrap_or_else(|e| e.into_inner()); + if !on().plan_on() { + return ToolResult::ok("not in plan mode; just do the work"); + } + let q = hx_core::Question { + text: "Implement this plan?".into(), + options: vec![ + ("Implement".into(), "leave plan mode and start".into()), + ( + "Keep planning".into(), + "stay read-only; type feedback below".into(), + ), + ], + multi: false, + other: true, + }; + match cx.state.choose(q).await { + Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => { + on().set_plan(false); + ToolResult::ok("plan approved; plan mode is off, implement it now") + } + Ok(Some(v)) if v.first().is_some_and(|s| s != "Keep planning") => { + ToolResult::ok(format!( + "plan not approved, user feedback: {}. Revise and call exit_plan again.", + v.join(", ") + )) + } + // no frontend (print mode) or dismissed: stay in plan mode + _ => ToolResult::ok("plan not approved; keep planning and ask what to change"), + } + } +} + /// Bytes of running bash output kept for the live view. const LIVE_TAIL: usize = 2048; diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index 1bc517b..e237734 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -908,7 +908,10 @@ fn chat_buf(app: &App) -> (Buffer, Vec) { let lines = app.t.lines(app.expanded, w); let mut cont = vec![]; for l in &lines { - let n = Paragraph::new(l.clone()).wrap(Wrap { trim: false }).line_count(w).max(1); + let n = Paragraph::new(l.clone()) + .wrap(Wrap { trim: false }) + .line_count(w) + .max(1); cont.push(false); cont.extend(std::iter::repeat(true).take(n - 1)); } @@ -1334,7 +1337,7 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { let (c, arg) = cmd .split_once(' ') .map_or((cmd, ""), |(a, b)| (a, b.trim())); - if app.busy && !matches!(c, "quit" | "btw" | "yolo" | "auto") { + if app.busy && !matches!(c, "quit" | "btw" | "yolo" | "auto" | "plan") { app.t.push(Item::Error("busy; esc to abort first".into())); return; } @@ -1486,6 +1489,24 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { drop(al); app.t.push(Item::Info(m)); } + "plan" => { + let mut al = app.state.allow.lock().unwrap_or_else(|e| e.into_inner()); + let on = match arg { + "on" => true, + "off" => false, + _ => !al.plan_on(), + }; + al.set_plan(on); + drop(al); + app.t.push(Item::Info( + if on { + "plan on: read-only; the plan is shown in full for approval when ready" + } else { + "plan off" + } + .into(), + )); + } "yolo" => { let on = app .state @@ -1683,6 +1704,17 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { ))); line = r.prompt; } + if app + .state + .allow + .lock() + .unwrap_or_else(|e| e.into_inner()) + .plan_on() + { + line = format!( + "[plan mode: read-only. Research, then call exit_plan with the complete plan.]\n{line}" + ); + } app.busy = true; app.since = Instant::now(); app.run_tokens = (0, 0); diff --git a/crates/hx-tui/src/transcript.rs b/crates/hx-tui/src/transcript.rs index 5134d62..8c737ab 100644 --- a/crates/hx-tui/src/transcript.rs +++ b/crates/hx-tui/src/transcript.rs @@ -351,6 +351,13 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec>) Item::Info(s) => out.push(Line::styled(format!("{} {s}", th.sym.info), th.dim())), Item::Lens(texts) => lens_block(texts, expanded, width, out), Item::Error(s) => out.push(Line::styled(format!("{} {s}", th.sym.err), th.err())), + Item::Tool { name, args, .. } if name == "exit_plan" => { + let mut md = vec![]; + markdown(args["plan"].as_str().unwrap_or_default(), &mut md); + out.push(Line::styled(th.rule("plan", width), th.accent())); + lead(md, " ", th.plain(), out); + out.push(Line::styled(th.rule("", width), th.dim())); + } Item::Tool { name, args,