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

+63
View File
@@ -26,6 +26,9 @@ create index if not exists events_session on events(session_id, id);
create table if not exists decisions(
session_id text not null, text text not null, state text not null, at integer not null,
vetoed integer not null default 0);
create table if not exists commands(
id integer primary key autoincrement, session_id text not null, kind text not null,
payload text not null default '{}', at integer not null, done integer not null default 0);
create table if not exists merge_queue(
ticket integer primary key autoincrement, session_id text not null unique);
create table if not exists needs(
@@ -223,6 +226,48 @@ impl Db {
Ok(())
}
/// A command for a running session (`message`, `interrupt`, `goto`, `abort`, `veto`). The runner, which may be
/// another process, picks it up the next time it polls.
pub fn add_command(&self, session: &str, kind: &str, payload: &Value) -> Result<i64> {
self.conn.execute(
"insert into commands(session_id, kind, payload, at) values (?1, ?2, ?3, ?4)",
params![
session,
kind,
payload.to_string(),
crate::state::now_ms() as i64
],
)?;
Ok(self.conn.last_insert_rowid())
}
/// Takes the session's unprocessed commands, oldest first, and marks them processed.
pub fn take_commands(&self, session: &str) -> Result<Vec<(String, Value)>> {
let mut q = self.conn.prepare(
"select id, kind, payload from commands where session_id = ?1 and done = 0 order by id",
)?;
let rows: Vec<(i64, String, String)> = q
.query_map([session], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))?
.collect::<rusqlite::Result<_>>()?;
for (id, ..) in &rows {
self.conn
.execute("update commands set done = 1 where id = ?1", [id])?;
}
Ok(rows
.into_iter()
.map(|(_, k, p)| (k, serde_json::from_str(&p).unwrap_or(Value::Null)))
.collect())
}
/// Marks a decision as vetoed; false if it does not exist or was already vetoed.
pub fn veto_decision(&self, id: i64) -> Result<bool> {
let n = self.conn.execute(
"update decisions set vetoed = 1 where rowid = ?1 and vetoed = 0",
[id],
)?;
Ok(n == 1)
}
pub fn set_goal(&self, text: &str) -> Result<()> {
self.conn.execute("delete from goal", [])?;
self.conn.execute(
@@ -540,6 +585,24 @@ mod tests {
db.queue_leave("s1").unwrap();
assert_eq!(db.queue_head().unwrap().as_deref(), Some("s2"));
db.queue_leave("s2").unwrap();
// commands: taken once, in order, per session
db.add_command("s1", "message", &json!({"text": "hi"}))
.unwrap();
db.add_command("s1", "interrupt", &json!({})).unwrap();
db.add_command("s2", "abort", &json!({})).unwrap();
let cmds = db.take_commands("s1").unwrap();
assert_eq!(
cmds.iter().map(|c| c.0.as_str()).collect::<Vec<_>>(),
["message", "interrupt"]
);
assert_eq!(cmds[0].1["text"], "hi");
assert!(db.take_commands("s1").unwrap().is_empty());
assert_eq!(db.take_commands("s2").unwrap().len(), 1);
// a decision can be vetoed once
let d = db.decisions().unwrap()[0].id;
assert!(db.veto_decision(d).unwrap());
assert!(!db.veto_decision(d).unwrap());
assert!(db.decisions().unwrap()[0].vetoed);
// needs queue: pending until answered, the first answer wins, finished items disappear
let n = db
.add_need("s1", "review", &json!({"key": "review/ok", "title": "t"}))