From 566758483d8d7d2c7becfef8822fafb452291bd5 Mon Sep 17 00:00:00 2001 From: damocles Date: Wed, 18 Nov 2026 13:35:54 +0100 Subject: [PATCH] hive-agent: count a todo miss only on a turn that ran to the end --- hive-agent/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/hive-agent/src/main.rs b/hive-agent/src/main.rs index 7c1cc6681..e1a714945 100644 --- a/hive-agent/src/main.rs +++ b/hive-agent/src/main.rs @@ -153,8 +153,10 @@ struct TurnControl { /// `Some(called)` when this turn was driven by a todo wake (`from == /// "todo"`) — `called` is whether the turn actually invoked /// `mcp__hyperhive__get_loose_ends` at some point. `None` for every - /// other wake source, so the serve loop's miss-streak only reacts to - /// todo-driven turns. See `TODO_MISS_PAUSE_THRESHOLD`. + /// other wake source, and for a todo wake whose turn failed without + /// calling it (see [`todo_wake_checked`]), so the serve loop's + /// miss-streak only reacts to todo-driven turns that ran. See + /// `TODO_MISS_PAUSE_THRESHOLD`. todo_wake_checked: Option, } @@ -992,6 +994,18 @@ fn apply_todo_wake_checked(checked: Option, todo_miss_streak: &mut u32, bu } } +/// What a finished turn tells the todo-miss streak. A todo-driven turn that +/// called `get_loose_ends` is `Some(true)` however it ended. One that did not +/// is a miss, `Some(false)`, only when it ran to the end: a turn that failed +/// first (an API stall, a rate limit, a login or session error, an +/// interruption) never had the chance, and counting it would pause an agent for +/// a fault of the host and not of the agent. Any other wake source is `None`. +fn todo_wake_checked(from: &str, called: bool, completed: bool) -> Option { + (from == "todo") + .then_some(called) + .filter(|called| *called || completed) +} + /// Drive a single turn: emit boot-of-turn events, run claude, ack on /// success / requeue on rate-limit-or-401 / log `turn failed` on a hard /// failure (the line the `hh-agent-turn-failed` Grafana rule alerts on), @@ -1037,8 +1051,11 @@ async fn handle_turn( // invoked. Used both for the OTEL tool counts below and for the // todo-miss-streak signal returned to the serve loop. let tool_calls = bus.take_tool_calls(); - let todo_wake_checked = - (from == "todo").then(|| tool_calls.contains_key("mcp__hyperhive__get_loose_ends")); + let todo_wake_checked = todo_wake_checked( + &from, + tool_calls.contains_key("mcp__hyperhive__get_loose_ends"), + outcome.is_ok(), + ); // Read-and-clear once per turn (same rationale as `take_tool_calls` // above — the harness gets exactly one chance to observe this flag): the // OTEL turn-metrics exporter's session-boundary counter needs it. @@ -1274,4 +1291,20 @@ mod tests { ); }); } + + /// A miss counts only when the turn ran to the end: a turn that failed + /// without calling `get_loose_ends` says nothing about the agent, and a + /// turn that called it resets the streak however it ended. + #[test] + fn a_failed_todo_turn_is_not_a_miss_and_a_call_is_a_check_however_it_ended() { + use super::todo_wake_checked; + assert_eq!(todo_wake_checked("todo", false, true), Some(false)); + assert_eq!(todo_wake_checked("todo", false, false), None); + assert_eq!(todo_wake_checked("todo", true, true), Some(true)); + assert_eq!(todo_wake_checked("todo", true, false), Some(true)); + for other in ["operator", "reminder", "mara"] { + assert_eq!(todo_wake_checked(other, false, true), None, "{other}"); + assert_eq!(todo_wake_checked(other, true, true), None, "{other}"); + } + } }