Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs,crate

Steven Enamakel55222c57f008
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
index a13f1e28fe..80cd0a5e21 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/repeated_failure.rs
@@ -330,6 +330,17 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
true
}
+ /// A new model response starts a new tool batch for the stop latch.
+ async fn after_model(
+ &self,
+ _ctx: &mut RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,
+ _state: &(),
+ _response: &mut tinyinference_llm::model::ModelResponse,
+ ) -> TaResult<()> {
+ self.stop.start_batch();
+ Ok(())
+ }
+
/// Forget the run's stop-latch state (see [`StopLatch::finish_run`]).
async fn after_agent(
&self,
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/stop_latch.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/stop_latch.rs
index 56f276cf42..c771b44c90 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/stop_latch.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/stop_latch.rs
@@ -16,6 +16,7 @@
//! [`super::repeat_loop_stop`]), and takes the relabel back when the rest of
//! the batch shows progress.
+use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Mutex;
use tinyagents_harness::steering::{SteeringCommand, SteeringHandle};
@@ -38,6 +39,10 @@ pub(super) struct StopLatch {
/// The classified note a repeat-loop relabel replaced. A success later in
/// the batch shows progress and puts it back.
relabelled_from: Mutex<Option<String>>,
+ /// Whether a call in the current model response's tool batch succeeded.
+ /// A batch that produced anything new is never relabelled as a repeat
+ /// loop, whatever order its results fold back in.
+ batch_progress: AtomicBool,
}
impl StopLatch {
@@ -50,6 +55,7 @@ impl StopLatch {
witness: RepeatLoopWitness::default(),
candidate: Mutex::default(),
relabelled_from: Mutex::default(),
+ batch_progress: AtomicBool::new(false),
}
}
@@ -135,6 +141,7 @@ impl StopLatch {
/// explained as a repeat loop: a pending relabel is dropped and a relabel
/// already made is taken back.
pub(super) fn record_success(&self) {
+ self.batch_progress.store(true, Ordering::SeqCst);
self.witness.record_success();
if let Ok(mut candidate) = self.candidate.lock() {
*candidate = None;
@@ -148,6 +155,12 @@ impl StopLatch {
}
}
+ /// A model response asked for a new tool batch: nothing in it has
+ /// succeeded yet.
+ pub(super) fn start_batch(&self) {
+ self.batch_progress.store(false, Ordering::SeqCst);
+ }
+
/// End of a run: forget its latch state.
pub(super) fn finish_run(&self) {
for state in [&self.own, &self.relabelled_from] {
@@ -163,6 +176,12 @@ impl StopLatch {
/// Replace this breaker's latched note with the repeat-loop `summary`,
/// keeping the classified note so progress can restore it.
fn relabel(&self, summary: String) {
+ if self.batch_progress.load(Ordering::SeqCst) {
+ tracing::debug!(
+ "[tinyagents::mw] the stop's batch made progress; keeping the classified stop note"
+ );
+ return;
+ }
if let Some(original) = self.replace_own_note(summary) {
tracing::debug!(
"[tinyagents::mw] classified stop came from a repeat loop; relabelled its stop note"
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
index 8f69924d2a..329fca598d 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_stop_cause_tests.rs
@@ -144,6 +144,10 @@ async fn re_read_until_stopped(batch: &[(&str, &str)], rounds: usize) -> (StopRe
.after_model(&mut ctx, &(), &mut response_with(&calls))
.await
.unwrap();
+ breaker
+ .after_model(&mut ctx, &(), &mut response_with(&calls))
+ .await
+ .unwrap();
let mut results = Vec::new();
for call in &mut calls {
let result = match guard.before_tool(&mut ctx, &(), call).await {
@@ -487,3 +491,33 @@ async fn a_success_after_a_relabelled_stop_restores_the_classified_note() {
"{note}"
);
}
+
+/// A success that folds back before the refusal and the stop in the same
+/// batch still counts: the batch produced something new, so the stop is not
+/// relabelled as a repeat loop.
+#[tokio::test]
+async fn a_success_earlier_in_the_batch_keeps_the_stop_as_is() {
+ let handle = SteeringHandle::allow_all();
+ let slot = std::sync::Arc::new(std::sync::Mutex::new(None));
+ let breaker = RepeatedToolFailureMiddleware::new(handle.clone(), 3, slot.clone());
+ let mut ctx = ctx();
+ fold(&breaker, &mut ctx, "a1", "file_read", "src/lbi.rs", false).await;
+ let calls = [TaToolCall::new(
+ "next",
+ "file_read",
+ json!({ "path": "src/lbi.rs" }),
+ )];
+ breaker
+ .after_model(&mut ctx, &(), &mut response_with(&calls))
+ .await
+ .unwrap();
+ fold(&breaker, &mut ctx, "g1", "grep", "src", true).await;
+ fold_refusal(&breaker, &mut ctx).await;
+ fold(&breaker, &mut ctx, "a2", "file_read", "src/lbi.rs", false).await;
+ let note = slot.lock().unwrap().clone().unwrap();
+ assert_eq!(
+ StopReasonKind::from_stop_note(&note),
+ StopReasonKind::NotFound,
+ "{note}"
+ );
+}