Add loop detector, budgets, and live session control

Milestone 8: the loop detector alerts when test failures repeat, budget
caps limit context/cost/wall-time per session with compaction support,
and live control lets you interrupt, message, or kill a running session
from the TUI. Decisions can now be vetoed, which sends hx/message and
rewinds the session to code. All 82 tests pass.

- src/budget.rs: pure functions for active time, budgets and loop detect
- src/runner.rs: Ctl control object polls for commands ~100ms, handles
  interrupt/message/abort/stop at every blocking point, re-runs on
  hints, and compacts when context is high
- src/db.rs: commands table for TUI→runner control
- src/detail.rs, src/goal.rs, src/tui/: loop detector box, budget caps,
  session keys (i/m/s/k/r), goal keys (v for veto), state forcing
- tests/fake_acp_hx.py: slow mode and hx/message, hx/compact log
- examples/machines/task.toml: [budget] with wall_secs, cost, compact_at

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
johannesandClaude Haiku 4.5 committed 2026-11-14 12:55:44 +01:00
1 parent 7bf0986640
commit bf609c2e0e
16 files changed
+1694 -55

No files matched your search

+37
View File
@@ -70,12 +70,26 @@ pub struct State {
pub only_on: Vec<String>,
}
/// Per-session limits. Exceeding `wall_secs` (time spent working, not waiting for the user) or `cost` emits a
/// `budget` event and sends the session to `on_budget`. Past `compact_at` percent of the context window hx is
/// told to compact. The retry limit is the `retry` table of the state that loops.
#[derive(Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct BudgetCfg {
pub wall_secs: Option<u64>,
/// US dollars.
pub cost: Option<f64>,
pub compact_at: Option<u8>,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct Machine {
pub name: String,
pub on_abort: Option<String>,
pub on_budget: Option<String>,
#[serde(default)]
pub budget: BudgetCfg,
#[serde(rename = "state")]
pub states: Vec<State>,
}
@@ -138,6 +152,12 @@ impl Machine {
);
Ok(())
};
if let Some(c) = self.budget.compact_at {
ensure!(
(1..=100).contains(&c),
"budget.compact_at is a percent, 1 to 100"
);
}
for (what, to) in [("on_abort", &self.on_abort), ("on_budget", &self.on_budget)] {
if let Some(to) = to {
target("<machine>", what, to)?;
@@ -262,6 +282,15 @@ mod tests {
fn task_machine_details() {
let m = example("task");
assert_eq!(m.start().name, "plan");
assert_eq!(
m.budget,
BudgetCfg {
wall_secs: Some(5400),
cost: Some(5.0),
compact_at: Some(85)
}
);
assert_eq!(example("docs").budget, BudgetCfg::default());
let t = m.state("test").unwrap();
assert_eq!(t.catch.keys().next().unwrap(), "TestsFailed");
assert_eq!(t.retry["TestsFailed"].max, 3);
@@ -293,6 +322,14 @@ end = "done"
#[test]
fn rejects_bad_machines() {
assert!(
err(&OK.replace("name = \"m\"", "name = \"m\"\n[budget]\ncompact_at = 0"))
.contains("percent")
);
assert!(
err(&OK.replace("name = \"m\"", "name = \"m\"\n[budget]\nbogus = 1"))
.contains("unknown field")
);
assert!(err(&OK.replace("kind = \"start\"", "")).contains("exactly one start"));
assert!(
err(&OK.replace("kind = \"exit\"", "kind = \"start\"")).contains("exactly one start")