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

+92 -16
View File
@@ -38,18 +38,54 @@ pub fn draw(f: &mut Frame, d: &Detail, h: &Header) {
None => 3,
};
let files_h = (d.files.len().clamp(1, 6) + 2) as u16;
let [test, now, files, budget] = Layout::vertical([
// the loop detector box only exists while the session loops
let loop_h = if d.looping.is_some() { 7 } else { 0 };
let [loop_area, test, now, files, budget] = Layout::vertical([
Constraint::Length(loop_h),
Constraint::Length(test_h),
Constraint::Length(4),
Constraint::Length(files_h),
Constraint::Min(6),
])
.areas(right);
if let Some(l) = &d.looping {
loop_box(f, loop_area, d, l);
}
test_box(f, test, d.test.as_ref());
now_box(f, now, d);
files_box(f, files, d);
budget_box(f, budget, d);
f.render_widget(key_bar(), keys);
f.render_widget(key_bar(d.looping.is_some()), keys);
}
fn loop_box(f: &mut Frame, area: Rect, d: &Detail, l: &crate::detail::LoopView) {
let b = Block::bordered().border_style(fg(theme::WAIT));
let inner = b.inner(area);
f.render_widget(b, area);
let mut what = format!(
"The last {} test runs failed on the same {} tests.",
l.runs, l.tests
);
if let Some(file) = &l.file {
what.push_str(&format!(" The agent keeps editing {file}."));
}
// the sentence wraps (two lines fit); the keys get a line each so none is cut
f.render_widget(
Paragraph::new(vec![
Line::from(Span::styled(" ◆ Loop detector", bold(theme::WAIT))),
Line::from(Span::raw(format!(" {what}"))),
Line::from(Span::styled(
format!(
" [r] re-plan from {} [m] send hint to agent",
d.start.to_uppercase()
),
fg(theme::DIM),
)),
Line::from(Span::styled(" [k] kill and hand over", fg(theme::DIM))),
])
.wrap(ratatui::widgets::Wrap { trim: false }),
inner,
);
}
fn status_color(s: Status) -> Color {
@@ -416,6 +452,12 @@ fn budget_box(f: &mut Frame, area: Rect, d: &Detail) {
label("context"),
bar(u as usize, l as usize, 10, c),
Span::raw(format!(" {} / {}", k(u), k(l))),
Span::styled(
d.budget
.compact_at
.map_or(String::new(), |c| format!(" compact at {c}%")),
fg(theme::DIM),
),
])
}
None => Line::from(vec![label("context"), Span::styled("—", fg(theme::DIM))]),
@@ -431,28 +473,62 @@ fn budget_box(f: &mut Frame, area: Rect, d: &Detail) {
Span::styled("none yet", fg(theme::DIM)),
]),
});
lines.push(Line::from(vec![
label("wall time"),
Span::styled(ago(d.budget.wall_secs), fg(theme::RUN)),
Span::styled(" no cap set", fg(theme::DIM)),
]));
lines.push(Line::from(vec![
label("cost"),
Span::styled(format!("${:.2}", d.budget.cost), fg(theme::RUN)),
Span::styled(" no cap set", fg(theme::DIM)),
]));
// a cap turns the plain number into a bar; red once it is passed
let over = |used: f64, cap: f64| if used > cap { theme::ERR } else { theme::RUN };
lines.push(match d.budget.wall_cap {
Some(cap) => {
let c = over(d.budget.wall_secs as f64, cap as f64);
Line::from(vec![
label("wall time"),
bar(d.budget.wall_secs as usize, cap as usize, 10, c),
Span::raw(format!(" {} / {} cap", ago(d.budget.wall_secs), ago(cap))),
])
}
None => Line::from(vec![
label("wall time"),
Span::styled(ago(d.budget.wall_secs), fg(theme::RUN)),
Span::styled(" no cap set", fg(theme::DIM)),
]),
});
lines.push(match d.budget.cost_cap {
Some(cap) => {
let c = over(d.budget.cost, cap);
Line::from(vec![
label("cost"),
bar(
(d.budget.cost * 100.0) as usize,
(cap * 100.0) as usize,
10,
c,
),
Span::raw(format!(" ${:.2} / ${cap:.2}", d.budget.cost)),
])
}
None => Line::from(vec![
label("cost"),
Span::styled(format!("${:.2}", d.budget.cost), fg(theme::RUN)),
Span::styled(" no cap set", fg(theme::DIM)),
]),
});
f.render_widget(Paragraph::new(lines), inner);
}
fn key_bar() -> Line<'static> {
fn key_bar(looping: bool) -> Line<'static> {
let mut v = vec![Span::raw(" ")];
for (key, what) in [
("esc", "back"),
("t", "full test output"),
("d", "diff"),
("l", "agent transcript"),
("o", "open worktree in hx"),
] {
("l", "transcript"),
("i", "interrupt"),
("m", "message"),
("s", "state"),
("k", "kill"),
("o", "open in hx"),
]
.into_iter()
.chain(looping.then_some(("r", "re-plan")))
{
v.push(Span::styled(
format!(" {key} "),
Style::new()