Skip to content

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

feat(embed): add turn control and turn types modules

Steven Enamakel7b03dc10fff2
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index e57b2266c7..c8b15b13d6 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -148,6 +148,7 @@ pub mod secret_storage;
mod turn;
mod turn_cancellation;
mod turn_meter;
+mod turn_steering;
/// Core internals for `openhuman-tinyhumans` and `openhuman-rpc` only; see
/// the module docs. Not part of the host-facing API.
@@ -265,6 +266,7 @@ pub use complete::{
pub use session_store::{InMemorySessionStores, SessionStoreProvider};
pub use turn::{absolute, Route, Turn, TurnOutcome, TurnRequest};
pub use turn_cancellation::TurnCancellation;
+pub use turn_steering::{SteerRefused, TurnSteering};
use std::sync::Arc;
diff --git a/crates/openhuman-embed/src/turn.rs b/crates/openhuman-embed/src/turn.rs
index 0387c7eb53..38d29c98a0 100644
--- a/crates/openhuman-embed/src/turn.rs
+++ b/crates/openhuman-embed/src/turn.rs
@@ -93,6 +93,7 @@ pub struct Turn {
require_tool_call: bool,
untrusted_input: bool,
cancellation: Option<crate::TurnCancellation>,
+ steering: Option<crate::TurnSteering>,
timeout: Option<std::time::Duration>,
control_deadline: Option<tokio::time::Instant>,
observer: Option<Arc<dyn crate::observe::TurnObserver>>,
@@ -126,6 +127,7 @@ impl Turn {
require_tool_call: false,
untrusted_input: false,
cancellation: None,
+ steering: None,
timeout: None,
control_deadline: None,
observer: None,
diff --git a/crates/openhuman-embed/src/turn_control.rs b/crates/openhuman-embed/src/turn_control.rs
index 0e5675a4c5..c92bcacc5e 100644
--- a/crates/openhuman-embed/src/turn_control.rs
+++ b/crates/openhuman-embed/src/turn_control.rs
@@ -265,7 +265,25 @@ impl Turn {
.clone()
}
+ /// Obtain a cloneable handle that adds messages to this turn while it
+ /// runs. Acquire it before moving the turn to `send()`.
+ ///
+ /// Each [`TurnSteering::steer`](crate::TurnSteering::steer) is applied at
+ /// the agent loop's next safe checkpoint: before the first model call,
+ /// after a tool batch, or in place of finishing when the model has just
+ /// written its final answer, which earns the turn one more model call.
+ /// Steers the turn did not apply come back on
+ /// [`TurnOutcome::unapplied_steers`] (or, when the turn fails,
+ /// [`TurnSteering::take_unapplied`](crate::TurnSteering::take_unapplied)),
+ /// and a steer sent after the turn ended is refused with its text.
+ pub fn steering_handle(&mut self) -> crate::TurnSteering {
+ self.steering.get_or_insert_with(Default::default).clone()
+ }
+
async fn send_inner(mut self, usage: &UsageSink) -> Result<TurnOutcome, CoreError> {
+ // Declared first so it drops last, after the dispatch future: by then
+ // every steer the turn did not apply is back in the queue.
+ let steering = crate::turn_steering::SteeringScope::new(self.steering.take());
// The core neither mints nor returns a session id, so continuing a
// conversation would otherwise be impossible without the caller
// inventing an id scheme — which every embedder has then done
@@ -352,6 +370,7 @@ impl Turn {
},
..Default::default()
},
+ run_queue: steering.queue(),
};
let options = AgentTurnOptions {
chat_env,
@@ -436,6 +455,13 @@ impl Turn {
);
let report = report.unwrap_or_default();
+ let unapplied_steers = steering.close();
+ if !unapplied_steers.is_empty() {
+ log::debug!(
+ "[embed][agent] turn session={session_id} ended with {} unapplied steer(s)",
+ unapplied_steers.len()
+ );
+ }
Ok(TurnOutcome {
reply,
session_id,
@@ -446,6 +472,7 @@ impl Turn {
structured,
finish_reason: report.finish_reason,
answered_model: report.answered_model,
+ unapplied_steers,
})
}
diff --git a/crates/openhuman-embed/src/turn_types.rs b/crates/openhuman-embed/src/turn_types.rs
index 0fc8e52e4c..9712ce296f 100644
--- a/crates/openhuman-embed/src/turn_types.rs
+++ b/crates/openhuman-embed/src/turn_types.rs
@@ -241,4 +241,9 @@ pub struct TurnOutcome {
/// The model the provider says answered the final call. `None` when the
/// provider did not say, or on a caller-built runtime's orchestrator.
pub answered_model: Option<String>,
+ /// Messages sent through [`Turn::steering_handle`](crate::Turn::steering_handle)
+ /// that the turn ended without applying, in the order they were sent. The
+ /// turn's reply never saw them; run them as the next turn if they still
+ /// matter. Empty when every steer was applied or none was sent.
+ pub unapplied_steers: Vec<String>,
}