Skip to content

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

feat(tinyagents): add response shape validation for agent replies

Steven Enamakel12890b8b7f02
diff --git a/crates/openhuman-core/src/agent/tinyagents/response_shape.rs b/crates/openhuman-core/src/agent/tinyagents/response_shape.rs
new file mode 100644
index 0000000000..9c866b58e4
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/response_shape.rs
@@ -0,0 +1,159 @@
+//! Per-turn response shape for a library host's turn: a response format and
+//! an output cap applied to every model call of the turn, and a report of the
+//! final call (finish reason, answering model, reasoning tokens).
+//!
+//! Carried as a task-local rather than through the session, because the
+//! session host's turn plumbing is shared by every product path and none of
+//! them need this. `inference::host_runtime::ops::agent_chat_reply_for` scopes
+//! it around the turn for an `AgentChatTarget::Definition` that asked for it;
+//! the turn runner reads it **once**, on the turn's own task, when it builds a
+//! root turn's harness ([`install`]), and the middleware holds the `Arc` from
+//! then on, so a model call that runs on another task still sees it.
+//! Sub-agent turns never install it: the shape is the host's statement about
+//! *its* turn's answer.
+
+use std::sync::{Arc, Mutex};
+
+use async_trait::async_trait;
+use tinyagents_harness::context::RunContext;
+use tinyagents_harness::error::Result as TaResult;
+use tinyagents_harness::middleware::Middleware;
+use tinyagents_harness::runtime::AgentHarness;
+use tinyinference_llm::model::{ModelRequest, ModelResponse, ResponseFormat};
+
+use crate::agent::tinyagents::host::OpenHumanRunContext;
+
+/// What a host asks of every model call in one turn.
+#[derive(Debug, Clone, Default, PartialEq)]
+pub struct ResponseShape {
+ /// Sent as `response_format` on every call of the turn's tool loop.
+ pub response_format: Option<ResponseFormat>,
+ /// Replaces the turn's per-call output cap.
+ pub max_output_tokens: Option<u32>,
+}
+
+/// What the turn's final model call reported.
+#[derive(Debug, Clone, Default, PartialEq, Eq)]
+pub struct FinalResponse {
+ /// The provider's finish reason for the last call (`stop`, `length`, ...).
+ pub finish_reason: Option<String>,
+ /// The model the provider says answered the last call.
+ pub answered_model: Option<String>,
+ /// Reasoning tokens summed over every call of the turn.
+ pub reasoning_tokens: u64,
+}
+
+/// A shape and the report slot its turn fills.
+#[derive(Debug, Default)]
+pub struct ResponseShapeScope {
+ shape: ResponseShape,
+ report: Mutex<FinalResponse>,
+}
+
+impl ResponseShapeScope {
+ /// A scope asking for `shape`.
+ #[must_use]
+ pub fn new(shape: ResponseShape) -> Arc<Self> {
+ Arc::new(Self {
+ shape,
+ report: Mutex::default(),
+ })
+ }
+
+ /// What the turn's calls have reported so far.
+ #[must_use]
+ pub fn report(&self) -> FinalResponse {
+ self.report
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .clone()
+ }
+}
+
+tokio::task_local! {
+ static RESPONSE_SHAPE: Arc<ResponseShapeScope>;
+}
+
+/// Run `fut` with `scope` as its turn's response shape.
+pub async fn with_response_shape<F: std::future::Future>(
+ scope: Arc<ResponseShapeScope>,
+ fut: F,
+) -> F::Output {
+ RESPONSE_SHAPE.scope(scope, Box::pin(fut)).await
+}
+
+/// Push the shaping middleware onto a root turn's harness, when a shape is
+/// scoped around the turn.
+pub(super) fn install(harness: &mut AgentHarness<(), OpenHumanRunContext>, root: bool) {
+ if !root {
+ return;
+ }
+ if let Ok(scope) = RESPONSE_SHAPE.try_with(Arc::clone) {
+ log::debug!(
+ "[tinyagents] response shape installed: format={} max_output_tokens={:?}",
+ scope.shape.response_format.is_some(),
+ scope.shape.max_output_tokens
+ );
+ harness.push_middleware(Arc::new(ResponseShapeMiddleware(scope)));
+ }
+}
+
+struct ResponseShapeMiddleware(Arc<ResponseShapeScope>);
+
+#[async_trait]
+impl Middleware<(), OpenHumanRunContext> for ResponseShapeMiddleware {
+ fn name(&self) -> &str {
+ "openhuman_response_shape"
+ }
+
+ async fn before_model(
+ &self,
+ _ctx: &mut RunContext<OpenHumanRunContext>,
+ _state: &(),
+ request: &mut ModelRequest,
+ ) -> TaResult<()> {
+ let shape = &self.0.shape;
+ if let Some(format) = &shape.response_format {
+ request.response_format = Some(format.clone());
+ }
+ if let Some(cap) = shape.max_output_tokens {
+ request.max_tokens = Some(cap);
+ }
+ Ok(())
+ }
+
+ async fn after_model(
+ &self,
+ _ctx: &mut RunContext<OpenHumanRunContext>,
+ _state: &(),
+ response: &mut ModelResponse,
+ ) -> TaResult<()> {
+ record(&self.0, response);
+ Ok(())
+ }
+}
+
+/// Fold one completed call into the report: the last call's finish reason
+/// and model win; reasoning tokens add up (a cache replay spent none).
+fn record(scope: &ResponseShapeScope, response: &ModelResponse) {
+ let mut report = scope
+ .report
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ report.finish_reason = response.finish_reason.clone();
+ report.answered_model = response
+ .resolved_route
+ .as_ref()
+ .map(|route| route.model.clone())
+ .or_else(|| response.resolved_model.as_ref().map(|m| m.name.clone()))
+ .filter(|model| !model.trim().is_empty());
+ if !response.served_from_cache {
+ if let Some(usage) = &response.usage {
+ report.reasoning_tokens += usage.reasoning_tokens;
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "response_shape_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/response_shape_tests.rs b/crates/openhuman-core/src/agent/tinyagents/response_shape_tests.rs
new file mode 100644
index 0000000000..956bf5f82f
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/response_shape_tests.rs
@@ -0,0 +1,43 @@
+use super::*;
+use tinyinference_llm::message::AssistantMessage;
+use tinyinference_llm::usage::Usage;
+
+fn response(finish: &str, reasoning: u64, cached: bool) -> ModelResponse {
+ ModelResponse {
+ message: AssistantMessage::default(),
+ usage: Some(Usage {
+ reasoning_tokens: reasoning,
+ ..Default::default()
+ }),
+ finish_reason: Some(finish.to_string()),
+ served_from_cache: cached,
+ ..Default::default()
+ }
+}
+
+#[test]
+fn the_last_call_reports_and_reasoning_adds_up() {
+ let scope = ResponseShapeScope::new(ResponseShape::default());
+ record(&scope, &response("tool_calls", 3, false));
+ record(&scope, &response("stop", 4, false));
+ record(&scope, &response("stop", 100, true));
+
+ let report = scope.report();
+ assert_eq!(report.finish_reason.as_deref(), Some("stop"));
+ assert_eq!(report.reasoning_tokens, 7, "a cache replay spent nothing");
+}
+
+#[tokio::test]
+async fn install_needs_a_scoped_shape_and_a_root_turn() {
+ let mut harness: AgentHarness<(), OpenHumanRunContext> = AgentHarness::new();
+ install(&mut harness, true);
+ let scope = ResponseShapeScope::new(ResponseShape {
+ max_output_tokens: Some(64),
+ ..Default::default()
+ });
+ with_response_shape(scope, async {
+ install(&mut harness, false);
+ install(&mut harness, true);
+ })
+ .await;
+}