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
fix(progress_tracing): handle missing span metadata in langfuse export
Steven Enamakel9d407496d124
diff --git a/crates/openhuman-core/src/agent/progress_tracing/langfuse.rs b/crates/openhuman-core/src/agent/progress_tracing/langfuse.rsindex 501a3288db..203af6d02d 100644--- a/crates/openhuman-core/src/agent/progress_tracing/langfuse.rs+++ b/crates/openhuman-core/src/agent/progress_tracing/langfuse.rs@@ -23,16 +23,12 @@ mod environment;mod ingestion_batch;#[allow(dead_code)] // Child-run rooting is still shared with the OTLP path.mod journal_export;-#[allow(dead_code)] // Legacy batch exporter remains for compatibility tests.-mod span_export;pub(crate) use environment::{environment_for_base, ingestion_url, skip_push};pub(crate) use journal_export::journal_push_ready;#[cfg(test)]pub(crate) use journal_export::push_observations;pub(crate) use journal_export::root_subagent_observations;-#[cfg(test)]-pub(crate) use span_export::push_spans;use super::{SpanStatus, TraceContext, TraceSpan};diff --git a/crates/openhuman-core/src/agent/progress_tracing/langfuse/span_export.rs b/crates/openhuman-core/src/agent/progress_tracing/langfuse/span_export.rsdeleted file mode 100644index b196bc96aa..0000000000--- a/crates/openhuman-core/src/agent/progress_tracing/langfuse/span_export.rs+++ /dev/null@@ -1,330 +0,0 @@-//! Span-tree export: converts the in-memory [`TraceSpan`] tree into a Langfuse-//! ingestion batch (trace-create + one observation per span, usage promoted to-//! `generation` observations) and pushes it to the backend proxy.--use serde_json::{json, Map, Value};--use crate::config::Config;-use crate::security::credentials::jwt::bearer_authorization_value;-use crate::security::credentials::session_support::direct_backend_credential;--use super::ingestion_batch::{iso_millis, new_event_id};-use super::{environment_for_base, ingestion_url, skip_push, LOG_TARGET, PUSH_TIMEOUT};-use super::{SpanStatus, TraceSpan};--/// Langfuse observation level for a span status. Only `Error` is elevated so-/// failed tool calls / turns surface in the Langfuse UI.-fn level_for(status: SpanStatus) -> &'static str {- match status {- SpanStatus::Error => "ERROR",- SpanStatus::Ok | SpanStatus::Unset => "DEFAULT",- }-}--/// Build the Langfuse `metadata` object from the span's (secret-free)-/// attributes plus its structured kind.-fn langfuse_metadata(span: &TraceSpan) -> Value {- let mut map = Map::new();- for (key, value) in &span.attributes {- map.insert(key.clone(), value.clone());- }- if let Ok(kind) = serde_json::to_value(span.kind) {- map.insert("kind".to_string(), kind);- }- Value::Object(map)-}--/// Convert finished spans into a Langfuse `/api/public/ingestion` batch payload:-/// a single `trace-create` for the shared trace id followed by one-/// `span-create` observation per span. Field names are Langfuse's camelCase-/// (`traceId`, `startTime`, `parentObservationId`); timestamps are ISO strings.-/// `environment` lands as the trace's top-level Langfuse environment.-pub(crate) fn spans_to_langfuse_batch(- spans: &[TraceSpan],- include_content: bool,- environment: &str,-) -> Value {- let mut batch: Vec<Value> = Vec::with_capacity(spans.len() + 1);-- // One trace-create for the run, keyed by the shared trace id. Prefer the- // root (parentless) span for the trace name/start; fall back to the first.- if let Some(root) = spans- .iter()- .find(|s| s.parent_span_id.is_none())- .or_else(|| spans.first())- {- let mut trace_body = json!({- "id": root.trace_id,- "name": root.name,- "timestamp": iso_millis(root.start_unix_ms),- // Top-level Langfuse trace fields (not metadata): deployment- // environment + the core release that produced the trace.- "environment": environment,- "release": env!("CARGO_PKG_VERSION"),- });- // Attribute the trace to the user and group per-turn traces under the- // conversation via Langfuse's native `userId`/`sessionId` (read from the- // turn span's stamped attributes). Every trace gets a sessionId: the- // stamped thread.id when present, else the trace id itself.- if let Some(user) = root.attributes.get("user.id").and_then(Value::as_str) {- trace_body["userId"] = json!(user);- }- let session = root- .attributes- .get("thread.id")- .and_then(Value::as_str)- .unwrap_or(root.trace_id.as_str());- trace_body["sessionId"] = json!(session);- // Trace-level metadata: transport client, agent attribution, run- // origin, and the core version — all secret-free identifiers.- let mut trace_meta = Map::new();- for key in ["client.id", "agent.id", "channel.source", "gen_ai.provider"] {- if let Some(value) = root.attributes.get(key) {- trace_meta.insert(key.to_string(), value.clone());- }- }- trace_meta.insert("app.version".to_string(), json!(env!("CARGO_PKG_VERSION")));- // Run-type tags so traces filter by kind of run in the Langfuse UI:- // `run:<type>` (interactive_chat / autonomous_task /- // channel_inbound) plus `source:<channel.source>` when known.- let mut tags: Vec<String> = Vec::with_capacity(2);- if let Some(run_type) = root.attributes.get("run.type").and_then(Value::as_str) {- tags.push(format!("run:{run_type}"));- trace_meta.insert("run_type".to_string(), json!(run_type));- }- if let Some(source) = root- .attributes- .get("channel.source")- .and_then(Value::as_str)- {- tags.push(format!("source:{source}"));- }- if !tags.is_empty() {- trace_body["tags"] = json!(tags);- }- trace_body["metadata"] = Value::Object(trace_meta);- // Trace-level input/output mirror the root turn span's content so the- // Langfuse trace list shows the prompt/reply at a glance. Same opt-out- // gate as the observations.- if include_content {- if let Some(input) = &root.input {- trace_body["input"] = input.clone();- }- if let Some(output) = &root.output {- trace_body["output"] = output.clone();- }- }- batch.push(json!({- "id": new_event_id(),- "type": "trace-create",- "timestamp": iso_millis(root.start_unix_ms),- "body": trace_body,- }));- }-- for span in spans {- let mut body = json!({- "id": span.span_id,- "traceId": span.trace_id,- "name": span.name,- "startTime": iso_millis(span.start_unix_ms),- "metadata": langfuse_metadata(span),- "level": level_for(span.status),- });- if let Some(end) = span.end_unix_ms {- body["endTime"] = json!(iso_millis(end));- }- if let Some(parent) = &span.parent_span_id {- body["parentObservationId"] = json!(parent);- }- // Failed spans surface their captured error text as the Langfuse- // statusMessage (the collector already truncated + content-gated it).- if let Some(message) = span.attributes.get("error.message").and_then(Value::as_str) {- body["statusMessage"] = json!(message);- }- // Prompt/reply content is transmitted only when the caller opted in- // (`observability.agent_tracing.capture_content`); otherwise it never- // leaves the device even though it may sit on the in-memory span.- if include_content {- if let Some(input) = &span.input {- body["input"] = input.clone();- }- if let Some(output) = &span.output {- body["output"] = output.clone();- }- }- // A span carrying `gen_ai.usage.*` attributes (today only the root turn- // span) is emitted as a Langfuse `generation` so the UI renders native- // token usage + cost instead of burying them in metadata. Token counts- // and cost are non-PII, so this promotion is unconditional.- let event_type = if apply_usage_fields(&mut body, span) {- "generation-create"- } else {- "span-create"- };- batch.push(json!({- "id": new_event_id(),- "type": event_type,- "timestamp": iso_millis(span.start_unix_ms),- "body": body,- }));- }-- json!({ "batch": batch })-}--/// Promote a span's `gen_ai.usage.*` / `gen_ai.request.model` attributes into-/// Langfuse's native `model` / `usageDetails` / `costDetails` fields so the-/// trace surfaces real token counts and cost (Langfuse only renders these on-/// `generation` observations). Returns `true` when usage was found, so the-/// caller emits the span as a `generation-create`. Only token/cost figures are-/// touched — never prompt text or PII.-fn apply_usage_fields(body: &mut Value, span: &TraceSpan) -> bool {- let attrs = &span.attributes;- let input = attrs- .get("gen_ai.usage.input_tokens")- .and_then(Value::as_u64);- let output = attrs- .get("gen_ai.usage.output_tokens")- .and_then(Value::as_u64);- if input.is_none() && output.is_none() {- return false;- }- let input = input.unwrap_or(0);- let output = output.unwrap_or(0);- let cached = attrs- .get("gen_ai.usage.cached_input_tokens")- .and_then(Value::as_u64)- .unwrap_or(0);- // #4454: `input_tokens` is INCLUSIVE of cached prompt tokens (cost.rs treats- // cached as a subset of input). Langfuse sums `usageDetails` components as- // disjoint buckets, so emit the NON-cached input (input - cached) — the- // components (non_cached_input + cache_read + output) are then disjoint and- // reconcile to `total` = input_tokens + output_tokens.- let non_cached_input = input.saturating_sub(cached);- let mut usage = Map::new();- usage.insert("input".to_string(), json!(non_cached_input));- usage.insert("output".to_string(), json!(output));- usage.insert("total".to_string(), json!(input.saturating_add(output)));- // Cache reads always flow into usageDetails (0 included) so the figure is- // explicit rather than absent when no cache was hit.- usage.insert("cache_read_input_tokens".to_string(), json!(cached));- // Reasoning + cache-write tokens ride along whenever the span carries them- // (the collector stamps them when > 0). Langfuse accepts arbitrary- // usageDetails keys.- if let Some(reasoning) = attrs- .get("gen_ai.usage.reasoning_tokens")- .and_then(Value::as_u64)- {- usage.insert("reasoning_tokens".to_string(), json!(reasoning));- }- if let Some(cache_write) = attrs- .get("gen_ai.usage.cache_creation_tokens")- .and_then(Value::as_u64)- {- usage.insert(- "cache_creation_input_tokens".to_string(),- json!(cache_write),- );- }- body["usageDetails"] = Value::Object(usage);- if let Some(model) = attrs.get("gen_ai.request.model").and_then(Value::as_str) {- body["model"] = json!(model);- }- if let Some(cost) = attrs.get("gen_ai.usage.cost_usd").and_then(Value::as_f64) {- body["costDetails"] = json!({ "total": cost });- }- true-}--/// Push `spans` to the co-hosted Langfuse server. Resolves the endpoint from the-/// current backend host and authenticates with the live session bearer. Returns-/// `Err` (for the caller to log + fall back) when there is no live session, the-/// host is unresolvable, the request fails, or Langfuse rejects the batch.-pub(crate) async fn push_spans(config: &Config, spans: &[TraceSpan]) -> Result<(), String> {- if spans.is_empty() {- return Ok(());- }- let url = ingestion_url(config);- // Ahead of the URL check, the session lookup and the request: a skipped- // environment must cost nothing per turn. An unresolvable URL lands in- // `environment_for_base`'s catch-all, which is `external` — so a garbage- // host skips rather than erroring, which is the right way round.- let environment = environment_for_base(&url);- if skip_push(environment) {- return Ok(());- }- if !url.starts_with("http") {- return Err(format!(- "could not resolve Langfuse ingestion URL from backend host (got {url:?})"- ));- }- // No TinyHumans connection, or no usable credential (signed out, offline- // local session): a configured state, so skip quietly rather than failing- // every turn's push.- let token = match direct_backend_credential(config, "langfuse span push") {- Some(crate::security::credentials::session_support::BackendCredential::Session(token)) => {- token- }- _ => return Ok(()),- };- let include_content = config.observability.agent_tracing.capture_content;- let batch = spans_to_langfuse_batch(spans, include_content, environment);- let span_count = spans.len();-- tracing::debug!(- target: LOG_TARGET,- "[agent-tracing] pushing {span_count} spans to Langfuse at {url}"- );-- // `ingestion_url` resolves to the backend's own Langfuse proxy route on the- // backend host, authenticated with a TinyHumans session token — backend- // traffic, so it rides the transport's client, which carries the host's- // attribution headers (product identity, versions).- let client = crate::backend::resolve_backend_transport()- .map_err(|err| format!("Langfuse push has no backend transport: {err}"))?- .http_client(crate::backend::TransportProfile::Api);- let response = client- .post(&url)- .header(- reqwest::header::AUTHORIZATION,- bearer_authorization_value(&token),- )- .timeout(PUSH_TIMEOUT)- .json(&batch)- .send()- .await- .map_err(|err| format!("POST {url} failed: {err}"))?;-- let status = response.status();- let body = response.text().await.unwrap_or_default();- if !status.is_success() {- let excerpt: String = body.chars().take(200).collect();- return Err(format!("Langfuse ingestion returned {status}: {excerpt}"));- }- // Langfuse returns 207 Multi-Status even when individual events are rejected- // — the failures live in the response `errors` array, not the HTTP status.- // Surface them (a partial rejection is logged but never fails the turn).- let rejected = serde_json::from_str::<Value>(&body)- .ok()- .and_then(|v| v.get("errors").and_then(Value::as_array).cloned())- .filter(|errs| !errs.is_empty());- if let Some(errs) = rejected {- let excerpt: String = serde_json::to_string(&errs)- .unwrap_or_default()- .chars()- .take(400)- .collect();- tracing::warn!(- target: LOG_TARGET,- "[agent-tracing] Langfuse ({status}) rejected {} of {span_count} span event(s): {excerpt}",- errs.len()- );- } else {- tracing::debug!(- target: LOG_TARGET,- "[agent-tracing] pushed {span_count} spans to Langfuse ({status})"- );- }- Ok(())-}diff --git a/crates/openhuman-core/src/flows/ops_engine_compatibility_tests.rs b/crates/openhuman-core/src/flows/ops_engine_compatibility_tests.rsindex 24a591a69f..2932707606 100644--- a/crates/openhuman-core/src/flows/ops_engine_compatibility_tests.rs+++ b/crates/openhuman-core/src/flows/ops_engine_compatibility_tests.rs@@ -1,72 +1,5 @@use super::*;-#[test]-fn engine_compatibility_distinguishes_nested_from_safe_fan_ins() {- let risky = structurally_valid_graph(nested_conditional_fan_in_graph());- let errors = engine_compatibility_errors(&risky);- assert_eq!(errors.len(), 1);- assert_eq!(errors[0].code, UNSUPPORTED_NESTED_CONDITIONAL_FAN_IN);- assert_eq!(errors[0].node_id.as_deref(), Some("m"));-- let one_level = structurally_valid_graph(json!({- "name": "one-level-mixed-fan-in",- "nodes": [- { "id": "start", "kind": "trigger", "name": "Trigger" },- { "id": "cond", "kind": "condition", "name": "Condition", "config": { "field": "flag" } },- { "id": "a", "kind": "output_parser", "name": "A" },- { "id": "other", "kind": "output_parser", "name": "Other" },- { "id": "c", "kind": "output_parser", "name": "C" },- { "id": "m", "kind": "merge", "name": "Merge" }- ],- "edges": [- { "from_node": "start", "from_port": "main", "to_node": "cond" },- { "from_node": "start", "from_port": "main", "to_node": "c" },- { "from_node": "cond", "from_port": "true", "to_node": "a" },- { "from_node": "cond", "from_port": "false", "to_node": "other" },- { "from_node": "a", "from_port": "main", "to_node": "m" },- { "from_node": "c", "from_port": "main", "to_node": "m" }- ]- }));- assert!(engine_compatibility_errors(&one_level).is_empty());-- let nested_without_fan_in = structurally_valid_graph(json!({- "name": "nested-without-fan-in",- "nodes": [- { "id": "start", "kind": "trigger", "name": "Trigger" },- { "id": "outer", "kind": "condition", "name": "Outer", "config": { "field": "outer" } },- { "id": "inner", "kind": "condition", "name": "Inner", "config": { "field": "inner" } },- { "id": "outer_else", "kind": "output_parser", "name": "Outer else" },- { "id": "a", "kind": "output_parser", "name": "A" },- { "id": "inner_else", "kind": "output_parser", "name": "Inner else" }- ],- "edges": [- { "from_node": "start", "from_port": "main", "to_node": "outer" },- { "from_node": "outer", "from_port": "true", "to_node": "inner" },- { "from_node": "outer", "from_port": "false", "to_node": "outer_else" },- { "from_node": "inner", "from_port": "true", "to_node": "a" },- { "from_node": "inner", "from_port": "false", "to_node": "inner_else" }- ]- }));- assert!(engine_compatibility_errors(&nested_without_fan_in).is_empty());-- let unconditional = structurally_valid_graph(json!({- "name": "unconditional-fan-in",- "nodes": [- { "id": "start", "kind": "trigger", "name": "Trigger" },- { "id": "a", "kind": "output_parser", "name": "A" },- { "id": "c", "kind": "output_parser", "name": "C" },- { "id": "m", "kind": "merge", "name": "Merge" }- ],- "edges": [- { "from_node": "start", "from_port": "main", "to_node": "a" },- { "from_node": "start", "from_port": "main", "to_node": "c" },- { "from_node": "a", "from_port": "main", "to_node": "m" },- { "from_node": "c", "from_port": "main", "to_node": "m" }- ]- }));- assert!(engine_compatibility_errors(&unconditional).is_empty());-}-#[test]fn engine_compatibility_rejects_main_label_on_conditional_fan_in_path() {let graph = structurally_valid_graph(main_port_conditional_fan_in_graph());