The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(observability): extract model and tool call projection into submodules
Steven Enamakel6219d4f04ca7
diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rsindex 2ad659f180..0db3a10681 100644--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs@@ -11,6 +11,9 @@ use tinytools::humanize_tool_name;use super::event_bridge::OpenhumanEventBridge;+#[path = "event_projection/tool_calls.rs"]+mod tool_calls;+impl EventListener for OpenhumanEventBridge {fn on_event(&self, record: &EventRecord) {if self.project_supplemental(record) {@@ -151,68 +154,7 @@ impl EventListener for OpenhumanEventBridge {// child call carries its owning `subagent_task_id` so the trace// exporter nests the generation under the subagent span (this is// what makes the Context Scout's model calls visible in Langfuse).- AgentEvent::ModelCompleted {- usage,- input,- output,- ..- } => {- let iteration = self.iteration();- let usage = usage.unwrap_or_default();- // Prefer the figures `record_usage` resolved for this call- // (charged>estimate cost + carried cache/reasoning breakdown —- // `UsageRecorded` fires before `ModelCompleted`), so the- // generation telemetry matches the wallet accounting exactly;- // fall back to a bare tier-aware estimate when absent.- let resolved = self- .resolved_calls- .lock()- .unwrap_or_else(|p| p.into_inner())- .remove(&iteration);- let call_cost = match resolved.as_ref() {- Some(r) => r.cost_usd,- None => Self::estimate_call_cost(&self.model, &usage),- };- let cache_creation_tokens = resolved- .as_ref()- .map(|r| r.cache_creation_tokens)- .unwrap_or(usage.cache_creation_tokens);- let reasoning_tokens = resolved- .as_ref()- .map(|r| r.reasoning_tokens)- .unwrap_or(usage.reasoning_tokens);- log::debug!(- "[tinyagents][usage] model_call_completed model={} provider={} iteration={} \- child={} in={} out={} cache_read={} cache_write={} reasoning={} \- cost_usd={:?} input_captured={} output_captured={}",- self.model,- self.provider_id,- iteration,- self.scope.is_some(),- usage.input_tokens,- usage.output_tokens,- usage.cache_read_tokens,- cache_creation_tokens,- reasoning_tokens,- call_cost,- input.is_some(),- output.is_some(),- );- self.send(AgentProgress::ModelCallCompleted {- model: self.model.clone(),- provider_id: self.provider_id.clone(),- subagent_task_id: self.scope.as_ref().map(|s| s.task_id.clone()),- input: input.clone(),- output: output.clone(),- iteration,- input_tokens: usage.input_tokens,- output_tokens: usage.output_tokens,- cached_input_tokens: usage.cache_read_tokens,- cache_creation_tokens,- reasoning_tokens,- cost_usd: call_cost,- });- }+ AgentEvent::ModelCompleted { .. } => self.project_model_completed(&record.event),AgentEvent::CostRecorded { cost } => {tracing::debug!(cost = ?cost,@@ -450,202 +392,8 @@ impl EventListener for OpenhumanEventBridge {}}}- AgentEvent::ToolStarted {- call_id,- tool_name,- input,- ..- } => {- // Unknown tool calls reach this arm too: the crate's- // `UnknownToolPolicy::ReturnToolError` path emits- // `AgentEvent::UnknownToolCall` (handled above) and then answers- // the call through `recover_tool_call`, which emits this- // `ToolStarted` under the same call id and the requested name.- // The call id recorded above gives it the "unavailable" label.- let iteration = self.iteration();- // Stamp the start instant so the completion event carries a real- // elapsed_ms (the crate's ToolCompleted has no timing payload).- self.tool_started_at- .lock()- .unwrap_or_else(|p| p.into_inner())- .insert(call_id.as_str().to_string(), std::time::Instant::now());- // The harness start event now carries the captured call- // input when payload capture is on (tinyagents#211); fall- // back to `Value::Null` when it's off, same as before. A- // tool whose label doesn't depend on its arguments (the- // common case: a policy label, or a name-derived default)- // already reads correctly with real input; one whose detail- // DOES depend on args (e.g. a search query) is recomputed- // with the real arguments on `ToolCallCompleted` below and- // forwarded on the wire as- // `tool_display_label`/`tool_display_detail` there too.- let arguments = input.clone().unwrap_or(serde_json::Value::Null);- let (display_label, display_detail) = match self.unknown_tool_display(call_id) {- Some(display) => display,- None => self.resolve_display(tool_name, &arguments),- };- match &self.scope {- None => self.send(AgentProgress::ToolCallStarted {- call_id: call_id.as_str().to_string(),- tool_name: tool_name.clone(),- arguments,- iteration,- display_label,- display_detail,- }),- Some(s) => self.send(AgentProgress::SubagentToolCallStarted {- agent_id: s.agent_id.clone(),- task_id: s.task_id.clone(),- call_id: call_id.as_str().to_string(),- tool_name: tool_name.clone(),- arguments,- iteration,- display_label,- display_detail,- }),- }- }- AgentEvent::ToolCompleted {- call_id,- tool_name,- input,- output,- error,- // `started_at_ms`/`duration_ms`/`output_bytes` ride the crate- // event, but the bridge reads the outcome-capture side channel- // below: it also carries the classified failure and the tool's- // structured metadata, which the crate event does not.- ..- } => {- let iteration = self.iteration();- // Read what the outcome-capture middleware classified for this- // call. Absent → the middleware did not see it (e.g. a call the- // harness answered without running a tool); fall back to the- // crate event's own `error`.- let outcome = self- .failure_map- .lock()- .ok()- .and_then(|mut m| m.remove(call_id.as_str()));- let unknown_display = self.unknown_tool_display(call_id);- let was_unknown = self- .unknown_calls- .lock()- .unwrap_or_else(|p| p.into_inner())- .remove(call_id.as_str())- .is_some();- let success = !was_unknown- && outcome- .as_ref()- .map(|(ok, ..)| *ok)- .unwrap_or(error.is_none());- // Real execution duration + output size the capture middleware- // recorded off the `ToolResult` (#4467, item 4). Fall back to- // the bridge's own ToolStarted stamp for duration, and to the- // captured payload for size, when the middleware ran late.- let stamped_elapsed = self- .tool_started_at- .lock()- .unwrap_or_else(|p| p.into_inner())- .remove(call_id.as_str())- .map(|t| t.elapsed().as_millis() as u64)- .unwrap_or(0);- let elapsed_ms = outcome- .as_ref()- .map(|(_, _, e, ..)| *e)- .filter(|e| *e > 0)- .unwrap_or(stamped_elapsed);- // Tool result text, captured by the harness when- // `RunPolicy.capture.tool_io` is on (the loop emits it as a- // JSON string). Empty when capture is off.- let output_text = match output {- Some(serde_json::Value::String(s)) => s.clone(),- Some(v) => v.to_string(),- None => String::new(),- };- // An unknown-tool answer's output is the corrective text; when- // payload capture is off it still rides `error`.- let output_text = match (&unknown_display, output_text.is_empty(), error) {- (Some(_), true, Some(text)) => text.clone(),- _ => output_text,- };- let output_chars = outcome- .as_ref()- .map(|(_, _, _, c, _)| *c)- .filter(|c| *c > 0)- .unwrap_or_else(|| output_text.chars().count());- // Structured, tool-specific result payload the middleware- // copied from `ToolResult.metadata` (e.g. web search results).- let structured = outcome.as_ref().and_then(|(.., s)| s.clone());- // Carry the classified failure onto whichever completion event- // this projects — main-agent OR sub-agent (#4459). Previously- // the sub-agent branch dropped it on the floor.- // An unknown tool is `NotFound` from the typed event itself- // (#6277), whatever the text sniff made of the echoed names.- let failure = if was_unknown {- Some(tinytools::status::describe(- tinytools::status::ToolFailureClass::NotFound,- ))- } else {- match outcome {- Some((_, f, ..)) => f,- None => error- .as_deref()- .map(|text| tinytools::status::classify(text, false)),- }- };- // Recompute the label/detail with the REAL call arguments- // (unlike `ToolCallStarted`, this event's `input` is the- // actual arguments the harness captured), so a tool whose- // detail depends on its args — a search query, a target- // email — surfaces it here even when the started event- // couldn't.- let args_for_display = input.clone().unwrap_or(serde_json::Value::Null);- let (display_label, display_detail) = match unknown_display {- Some(display) => display,- None => self.resolve_display(tool_name, &args_for_display),- };- tracing::debug!(- call_id = call_id.as_str(),- tool_name = tool_name.as_str(),- success,- elapsed_ms,- has_structured = structured.is_some(),- "[tool-presentation] projecting ToolCallCompleted with resolved label/detail"- );- match &self.scope {- None => self.send(AgentProgress::ToolCallCompleted {- call_id: call_id.as_str().to_string(),- tool_name: tool_name.clone(),- success,- output_chars,- output: output_text,- arguments: input.clone(),- elapsed_ms,- iteration,- failure,- display_label,- display_detail,- structured,- }),- Some(s) => self.send(AgentProgress::SubagentToolCallCompleted {- agent_id: s.agent_id.clone(),- task_id: s.task_id.clone(),- call_id: call_id.as_str().to_string(),- tool_name: tool_name.clone(),- success,- output_chars,- output: output_text,- arguments: input.clone(),- elapsed_ms,- iteration,- failure,- display_label,- display_detail,- structured,- }),- }- }+ AgentEvent::ToolStarted { .. } => self.project_tool_started(&record.event),+ AgentEvent::ToolCompleted { .. } => self.project_tool_completed(&record.event),// Response-cache accounting (issue #4249, 03.2). A hit means the// harness served this model call from its local `ResponseCache`// without invoking the provider (deterministic internal runs only —@@ -719,6 +467,9 @@ impl EventListener for OpenhumanEventBridge {}}+#[path = "event_projection/model_projection.rs"]+mod model_projection;+impl OpenhumanEventBridge {/// The "unavailable" label/detail pair for a call the crate answered as an/// unknown tool, or `None` for an ordinary call.diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/model_projection.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/model_projection.rsnew file mode 100644index 0000000000..696ab92bc3--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/model_projection.rs@@ -0,0 +1,71 @@+use super::*;++impl OpenhumanEventBridge {+ pub(super) fn project_model_completed(&self, event: &AgentEvent) {+ match event {+ AgentEvent::ModelCompleted {+ usage,+ input,+ output,+ ..+ } => {+ let iteration = self.iteration();+ let usage = usage.unwrap_or_default();+ // Prefer the figures `record_usage` resolved for this call+ // (charged>estimate cost + carried cache/reasoning breakdown —+ // `UsageRecorded` fires before `ModelCompleted`), so the+ // generation telemetry matches the wallet accounting exactly;+ // fall back to a bare tier-aware estimate when absent.+ let resolved = self+ .resolved_calls+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .remove(&iteration);+ let call_cost = match resolved.as_ref() {+ Some(r) => r.cost_usd,+ None => Self::estimate_call_cost(&self.model, &usage),+ };+ let cache_creation_tokens = resolved+ .as_ref()+ .map(|r| r.cache_creation_tokens)+ .unwrap_or(usage.cache_creation_tokens);+ let reasoning_tokens = resolved+ .as_ref()+ .map(|r| r.reasoning_tokens)+ .unwrap_or(usage.reasoning_tokens);+ log::debug!(+ "[tinyagents][usage] model_call_completed model={} provider={} iteration={} \+ child={} in={} out={} cache_read={} cache_write={} reasoning={} \+ cost_usd={:?} input_captured={} output_captured={}",+ self.model,+ self.provider_id,+ iteration,+ self.scope.is_some(),+ usage.input_tokens,+ usage.output_tokens,+ usage.cache_read_tokens,+ cache_creation_tokens,+ reasoning_tokens,+ call_cost,+ input.is_some(),+ output.is_some(),+ );+ self.send(AgentProgress::ModelCallCompleted {+ model: self.model.clone(),+ provider_id: self.provider_id.clone(),+ subagent_task_id: self.scope.as_ref().map(|s| s.task_id.clone()),+ input: input.clone(),+ output: output.clone(),+ iteration,+ input_tokens: usage.input_tokens,+ output_tokens: usage.output_tokens,+ cached_input_tokens: usage.cache_read_tokens,+ cache_creation_tokens,+ reasoning_tokens,+ cost_usd: call_cost,+ });+ }+ _ => {}+ }+ }+}diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/tool_calls.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/tool_calls.rsnew file mode 100644index 0000000000..c1a43a2035--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection/tool_calls.rs@@ -0,0 +1,212 @@+use super::*;++impl OpenhumanEventBridge {+ pub(super) fn project_tool_started(&self, event: &AgentEvent) {+ match event {+ AgentEvent::ToolStarted {+ call_id,+ tool_name,+ input,+ ..+ } => {+ // Unknown tool calls reach this arm too: the crate's+ // `UnknownToolPolicy::ReturnToolError` path emits+ // `AgentEvent::UnknownToolCall` (handled above) and then answers+ // the call through `recover_tool_call`, which emits this+ // `ToolStarted` under the same call id and the requested name.+ // The call id recorded above gives it the "unavailable" label.+ let iteration = self.iteration();+ // Stamp the start instant so the completion event carries a real+ // elapsed_ms (the crate's ToolCompleted has no timing payload).+ self.tool_started_at+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .insert(call_id.as_str().to_string(), std::time::Instant::now());+ // The harness start event now carries the captured call+ // input when payload capture is on (tinyagents#211); fall+ // back to `Value::Null` when it's off, same as before. A+ // tool whose label doesn't depend on its arguments (the+ // common case: a policy label, or a name-derived default)+ // already reads correctly with real input; one whose detail+ // DOES depend on args (e.g. a search query) is recomputed+ // with the real arguments on `ToolCallCompleted` below and+ // forwarded on the wire as+ // `tool_display_label`/`tool_display_detail` there too.+ let arguments = input.clone().unwrap_or(serde_json::Value::Null);+ let (display_label, display_detail) = match self.unknown_tool_display(call_id) {+ Some(display) => display,+ None => self.resolve_display(tool_name, &arguments),+ };+ match &self.scope {+ None => self.send(AgentProgress::ToolCallStarted {+ call_id: call_id.as_str().to_string(),+ tool_name: tool_name.clone(),+ arguments,+ iteration,+ display_label,+ display_detail,+ }),+ Some(s) => self.send(AgentProgress::SubagentToolCallStarted {+ agent_id: s.agent_id.clone(),+ task_id: s.task_id.clone(),+ call_id: call_id.as_str().to_string(),+ tool_name: tool_name.clone(),+ arguments,+ iteration,+ display_label,+ display_detail,+ }),+ }+ }+ _ => {}+ }+ }++ pub(super) fn project_tool_completed(&self, event: &AgentEvent) {+ match event {+ AgentEvent::ToolCompleted {+ call_id,+ tool_name,+ input,+ output,+ error,+ // `started_at_ms`/`duration_ms`/`output_bytes` ride the crate+ // event, but the bridge reads the outcome-capture side channel+ // below: it also carries the classified failure and the tool's+ // structured metadata, which the crate event does not.+ ..+ } => {+ let iteration = self.iteration();+ // Read what the outcome-capture middleware classified for this+ // call. Absent → the middleware did not see it (e.g. a call the+ // harness answered without running a tool); fall back to the+ // crate event's own `error`.+ let outcome = self+ .failure_map+ .lock()+ .ok()+ .and_then(|mut m| m.remove(call_id.as_str()));+ let unknown_display = self.unknown_tool_display(call_id);+ let was_unknown = self+ .unknown_calls+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .remove(call_id.as_str())+ .is_some();+ let success = !was_unknown+ && outcome+ .as_ref()+ .map(|(ok, ..)| *ok)+ .unwrap_or(error.is_none());+ // Real execution duration + output size the capture middleware+ // recorded off the `ToolResult` (#4467, item 4). Fall back to+ // the bridge's own ToolStarted stamp for duration, and to the+ // captured payload for size, when the middleware ran late.+ let stamped_elapsed = self+ .tool_started_at+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .remove(call_id.as_str())+ .map(|t| t.elapsed().as_millis() as u64)+ .unwrap_or(0);+ let elapsed_ms = outcome+ .as_ref()+ .map(|(_, _, e, ..)| *e)+ .filter(|e| *e > 0)+ .unwrap_or(stamped_elapsed);+ // Tool result text, captured by the harness when+ // `RunPolicy.capture.tool_io` is on (the loop emits it as a+ // JSON string). Empty when capture is off.+ let output_text = match output {+ Some(serde_json::Value::String(s)) => s.clone(),+ Some(v) => v.to_string(),+ None => String::new(),+ };+ // An unknown-tool answer's output is the corrective text; when+ // payload capture is off it still rides `error`.+ let output_text = match (&unknown_display, output_text.is_empty(), error) {+ (Some(_), true, Some(text)) => text.clone(),+ _ => output_text,+ };+ let output_chars = outcome+ .as_ref()+ .map(|(_, _, _, c, _)| *c)+ .filter(|c| *c > 0)+ .unwrap_or_else(|| output_text.chars().count());+ // Structured, tool-specific result payload the middleware+ // copied from `ToolResult.metadata` (e.g. web search results).+ let structured = outcome.as_ref().and_then(|(.., s)| s.clone());+ // Carry the classified failure onto whichever completion event+ // this projects — main-agent OR sub-agent (#4459). Previously+ // the sub-agent branch dropped it on the floor.+ // An unknown tool is `NotFound` from the typed event itself+ // (#6277), whatever the text sniff made of the echoed names.+ let failure = if was_unknown {+ Some(tinytools::status::describe(+ tinytools::status::ToolFailureClass::NotFound,+ ))+ } else {+ match outcome {+ Some((_, f, ..)) => f,+ None => error+ .as_deref()+ .map(|text| tinytools::status::classify(text, false)),+ }+ };+ // Recompute the label/detail with the REAL call arguments+ // (unlike `ToolCallStarted`, this event's `input` is the+ // actual arguments the harness captured), so a tool whose+ // detail depends on its args — a search query, a target+ // email — surfaces it here even when the started event+ // couldn't.+ let args_for_display = input.clone().unwrap_or(serde_json::Value::Null);+ let (display_label, display_detail) = match unknown_display {+ Some(display) => display,+ None => self.resolve_display(tool_name, &args_for_display),+ };+ tracing::debug!(+ call_id = call_id.as_str(),+ tool_name = tool_name.as_str(),+ success,+ elapsed_ms,+ has_structured = structured.is_some(),+ "[tool-presentation] projecting ToolCallCompleted with resolved label/detail"+ );+ match &self.scope {+ None => self.send(AgentProgress::ToolCallCompleted {+ call_id: call_id.as_str().to_string(),+ tool_name: tool_name.clone(),+ success,+ output_chars,+ output: output_text,+ arguments: input.clone(),+ elapsed_ms,+ iteration,+ failure,+ display_label,+ display_detail,+ structured,+ }),+ Some(s) => self.send(AgentProgress::SubagentToolCallCompleted {+ agent_id: s.agent_id.clone(),+ task_id: s.task_id.clone(),+ call_id: call_id.as_str().to_string(),+ tool_name: tool_name.clone(),+ success,+ output_chars,+ output: output_text,+ arguments: input.clone(),+ elapsed_ms,+ iteration,+ failure,+ display_label,+ display_detail,+ structured,+ }),+ }+ }+ _ => {}+ }+ }++}