Route hx limit stops to on_budget, accept _hx/ methods, send catches

A blocked step result with reason "limit" and no catch now takes the same
path as an orchestrator budget stop. Other blocked reasons keep the normal
catch path. hx/step context now lists the state's catch names so hx can pick
a valid one.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
johannesandClaude Sonnet 5.5 committed 2026-11-14 13:13:49 +01:00
1 parent bf609c2e0e
commit 1f2486a599
3 files changed
+107 -10

No files matched your search

+91 -10
View File
@@ -184,6 +184,20 @@ impl<'a> Ctl<'a> {
}
}
/// Goes to `on_budget`, or ends the session as failed when the machine has none.
fn stop_for_budget(&self, detail: String) {
self.set_stop(match &self.on_budget {
Some(s) => Stop::Goto {
state: s.clone(),
why: "budget".into(),
},
None => Stop::End {
status: "failed",
reason: format!("budget: {detail}"),
},
});
}
fn stopped(&self) -> bool {
self.stop.borrow().is_some()
}
@@ -275,16 +289,7 @@ impl<'a> Ctl<'a> {
"budget",
json!({"what": x.what, "used": x.used, "limit": x.limit}),
)?;
self.set_stop(match &self.on_budget {
Some(s) => Stop::Goto {
state: s.clone(),
why: "budget".into(),
},
None => Stop::End {
status: "failed",
reason: format!("budget: {} {} > {}", x.what, x.used, x.limit),
},
});
self.stop_for_budget(format!("{} {} > {}", x.what, x.used, x.limit));
p.interrupt = true;
}
}
@@ -661,6 +666,7 @@ fn run_agent(cx: &mut Ctx, st: &State, step: &Step) -> Result<StepRes> {
loop {
let context = json!({
"state": st.name, "last_failure": last_failure(cx.db, &cx.s.id)?, "hint": hint,
"catches": st.catch.keys().collect::<Vec<_>>(),
});
let mut sink = DbSink {
db: cx.db,
@@ -699,6 +705,16 @@ fn run_agent(cx: &mut Ctx, st: &State, step: &Step) -> Result<StepRes> {
out: r.summary,
catch: None,
}),
"blocked" if r.catch.is_none() && r.reason.as_deref() == Some("limit") => {
// hx stopped on a limit from hx/step: same path as an orchestrator budget
cx.db.emit(
&cx.s.id,
"budget",
json!({"what": "hx limit", "used": "reached", "limit": r.summary}),
)?;
cx.ctl.stop_for_budget(format!("hx limit: {}", r.summary));
Err(Stopped.into())
}
"blocked" => Ok(StepRes {
ok: false,
exit: None,
@@ -1422,6 +1438,10 @@ next = "done"
.filter(|m| m["method"] == "hx/step")
.collect();
assert_eq!(steps.len(), 3);
assert_eq!(
steps[0]["params"]["context"]["catches"],
json!(["PlanInvalid"])
);
let ctx = &steps[2]["params"]["context"]["last_failure"];
assert_eq!(ctx["state"], "build");
assert!(ctx["output"].as_str().unwrap().contains("boom-output"));
@@ -1896,6 +1916,67 @@ next = "done"
let _ = std::fs::remove_dir_all(&e.dir);
}
const LIMIT_MACHINE: &str = r#"name = "m"
on_abort = "aborted"
on_budget = "stuck"
[[state]]
name = "a"
kind = "start"
steps = [{ name = "ag", run = "agent", prompt = "implement" }]
next = "done"
"#;
#[test]
fn hx_blocked_with_reason_limit_goes_to_on_budget() {
let e = env("hx-limit");
let m = ctl_machine(LIMIT_MACHINE);
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
let p = fake("FAKE_MODE=limit");
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &p).unwrap(),
"escalated"
);
let ev = e.db.events(Some("s1"), 0).unwrap();
assert_eq!(ev.iter().filter(|v| v.kind == "budget").count(), 1);
let g = ev.iter().find(|v| v.kind == "goto").unwrap();
assert_eq!(
(g.payload["to"].as_str(), g.payload["why"].as_str()),
(Some("stuck"), Some("budget"))
);
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn hx_blocked_with_another_reason_is_not_a_budget() {
let e = env("hx-error");
let m = ctl_machine(LIMIT_MACHINE);
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
let p = fake("FAKE_MODE=error");
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &p).unwrap(),
"failed"
);
let ev = e.db.events(Some("s1"), 0).unwrap();
assert!(ev.iter().all(|v| v.kind != "budget" && v.kind != "goto"));
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn underscore_prefixed_hx_methods_are_accepted() {
let e = env("underscore");
let m = one_step(r#"{ name = "ag", run = "agent", prompt = "implement" }"#);
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
let p = fake("FAKE_UNDERSCORE=1");
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &p).unwrap(),
"merged"
);
let kinds = kinds(&e.db, "s1");
assert!(kinds.contains(&"intent".to_string()) && kinds.contains(&"edit".to_string()));
assert_eq!(e.db.decision_count("s1").unwrap(), 1);
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn a_budget_without_on_budget_fails_the_session() {
let e = env("budget-fail");