diff --git a/crates/hx-core/src/approve.rs b/crates/hx-core/src/approve.rs index 509fb3e..ddcffed 100644 --- a/crates/hx-core/src/approve.rs +++ b/crates/hx-core/src/approve.rs @@ -168,6 +168,8 @@ pub async fn serve(path: &Path, cx: ToolCtx, bus: Bus, mode: Unanswered) -> std: Err(r) } else if let Err(r) = unprompted(&cx) { Err(r) + } else if tool == "ExitPlanMode" { + exit_plan(&cx, v["input"]["plan"].as_str().unwrap_or_default()).await } else if v["tool_name"] == "AskUserQuestion" { // claude's own question tool: show hx's popup, hand the answers back as input input = ask(&cx, &v["input"]).await; @@ -210,6 +212,38 @@ pub async fn serve(path: &Path, cx: ToolCtx, bus: Bus, mode: Unanswered) -> std: } } +/// claude's `ExitPlanMode`: the TUI shows the whole plan, then asks. Implement ends hx plan mode too. +async fn exit_plan(cx: &ToolCtx, plan: &str) -> Result<(), String> { + let q = crate::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, + plan: Some(plan.into()), + }; + match cx.state.choose(q).await { + Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => { + cx.state + .allow + .lock() + .unwrap_or_else(|e| e.into_inner()) + .set_plan(false); + Ok(()) + } + Ok(Some(v)) if v.first().is_some_and(|s| s != "Keep planning") => Err(format!( + "plan not approved, user feedback: {}. Revise and present the plan again.", + v.join(", ") + )), + _ => Err("plan not approved; keep planning and ask what to change".into()), + } +} + /// Run each `AskUserQuestion` question through the popup; `input` plus `answers` (question -> label(s)). /// None = no frontend or dismissed. async fn ask(cx: &ToolCtx, input: &Value) -> Option { @@ -227,6 +261,7 @@ async fn ask(cx: &ToolCtx, input: &Value) -> Option { options: options.collect(), multi: q["multiSelect"].as_bool().unwrap_or(false), other: true, + plan: None, }) .await .ok()??; @@ -362,6 +397,24 @@ mod tests { assert_eq!(ask(&cx, &inp).await.unwrap()["answers"]["Which?"], "B"); } + #[tokio::test] + async fn exit_plan_shows_plan_and_implement_ends_plan_mode() { + let cx = ToolCtx { + cwd: ".".into(), + state: Default::default(), + }; + cx.state.allow.lock().unwrap().set_plan(true); + let seen = std::sync::Arc::new(std::sync::Mutex::new(None)); + let s = seen.clone(); + let _ = cx.state.choose.set(std::sync::Arc::new(move |q| { + *s.lock().unwrap() = q.plan.clone(); + Box::pin(async { Some(vec!["Keep planning".to_string()]) }) + })); + assert!(exit_plan(&cx, "# P\nstep 1").await.is_err()); + assert_eq!(seen.lock().unwrap().as_deref(), Some("# P\nstep 1")); + assert!(cx.state.allow.lock().unwrap().plan_on()); + } + #[tokio::test] async fn socket_round_trip_allows_and_denies() { use crate::permit::Decision; diff --git a/crates/hx-core/src/lib.rs b/crates/hx-core/src/lib.rs index 7e86f13..f1f6ca8 100644 --- a/crates/hx-core/src/lib.rs +++ b/crates/hx-core/src/lib.rs @@ -43,6 +43,8 @@ pub struct Question { pub multi: bool, /// offer a free-text "Other" row pub other: bool, + /// Long markdown (a plan) the frontend shows in full in the transcript before asking. + pub plan: Option, } /// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed. diff --git a/crates/hx-tools/src/lib.rs b/crates/hx-tools/src/lib.rs index 4f3282d..e840c72 100644 --- a/crates/hx-tools/src/lib.rs +++ b/crates/hx-tools/src/lib.rs @@ -314,6 +314,7 @@ impl Tool for AskUser { options, multi: a["multi"].as_bool().unwrap_or(false), other: true, + plan: None, }; match cx.state.choose(q).await { Err(e) => ToolResult::err(e), @@ -352,6 +353,7 @@ impl Tool for ExitPlan { ], multi: false, other: true, + plan: None, }; match cx.state.choose(q).await { Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => { diff --git a/crates/hx-tui/src/choose.rs b/crates/hx-tui/src/choose.rs index 518fca6..c671c9d 100644 --- a/crates/hx-tui/src/choose.rs +++ b/crates/hx-tui/src/choose.rs @@ -286,6 +286,7 @@ mod tests { ], multi, other: true, + plan: None, }) } fn k(c: KeyCode) -> KeyEvent { @@ -405,6 +406,7 @@ mod tests { ], multi: false, other: true, + plan: None, }); let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(200, 50)).unwrap(); term.draw(|f| c.draw(f, 45)).unwrap(); diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index e237734..96de950 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -539,7 +539,12 @@ pub async fn run(setup: Setup) -> std::io::Result<()> { match u { Ui::Ask(q, reply) => app.ask = Some((q, reply)), Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)), - Ui::Choose(q, reply) => app.choose = Some((choose::Choose::new(q), reply)), + Ui::Choose(q, reply) => { + if let Some(p) = &q.plan { + app.t.push(Item::Plan(p.clone())); + } + app.choose = Some((choose::Choose::new(q), reply)) + } Ui::Ev(e) => { if let Event::SubUsage(u) = &e { app.st.usage.add(u); diff --git a/crates/hx-tui/src/transcript.rs b/crates/hx-tui/src/transcript.rs index 8c737ab..1099c6e 100644 --- a/crates/hx-tui/src/transcript.rs +++ b/crates/hx-tui/src/transcript.rs @@ -21,6 +21,8 @@ pub enum Item { started: Instant, took: Option, }, + /// A finished plan, shown in full. + Plan(String), Info(String), Error(String), /// Hook reports injected at turn end (`[lens] …`), shown as the lens block. @@ -352,12 +354,9 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec>) 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())); + plan_block(args["plan"].as_str().unwrap_or_default(), width, out) } + Item::Plan(p) => plan_block(p, width, out), Item::Tool { name, args, @@ -379,6 +378,15 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec>) } } +fn plan_block(plan: &str, width: usize, out: &mut Vec>) { + let th = theme::t(); + let mut md = vec![]; + markdown(plan, &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())); +} + /// `┄ lens ┄┄┄` rule, then per report a summary line (errors red, all-pass green) and, /// collapsed, the first findings; `ctrl-o` shows all. fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec>) {