plan mode (/plan, exit_plan shows full plan) #187

Merged
hermes merged 5 commits from feat/plan-mode into main 2026-11-14 11:45:20 +00:00
6 changed files with 78 additions and 6 deletions
Showing only changes of commit 8a9d8fe607 - Show all commits

No files matched your search

+53
View File
@@ -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<Value> {
@@ -227,6 +261,7 @@ async fn ask(cx: &ToolCtx, input: &Value) -> Option<Value> {
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;
+2
View File
@@ -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<String>,
}
/// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed.
+2
View File
@@ -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") => {
+2
View File
@@ -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();
+6 -1
View File
@@ -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);
+13 -5
View File
@@ -21,6 +21,8 @@ pub enum Item {
started: Instant,
took: Option<Duration>,
},
/// 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<Line<'static>>)
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<Line<'static>>)
}
}
fn plan_block(plan: &str, width: usize, out: &mut Vec<Line<'static>>) {
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<Line<'static>>) {