Skip to content

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

refactor(agent): remove split progress-tracing implementation files

Steven Enamakel6930e01c4518
diff --git a/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_01.rs b/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_01.rs
deleted file mode 100644
index d510b4c7a3..0000000000
--- a/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_01.rs
+++ /dev/null
@@ -1,433 +0,0 @@
-
-impl SpanCollector {
-
- pub fn new(ctx: TraceContext) -> Self {
- Self {
- ctx,
- spans: Vec::new(),
- next_span_seq: 0,
- id_prefix: uuid::Uuid::new_v4().simple().to_string(),
- turn_span_id: None,
- turn_span_index: None,
- current_iteration_span_id: None,
- current_iteration_index: None,
- open_tools: BTreeMap::new(),
- subagents: BTreeMap::new(),
- }
- }
-
- /// Opt into attaching content to spans (prompt/reply, generation
- /// request/completion, tool + subagent I/O). Wire this to
- /// `observability.agent_tracing.capture_content`. Equivalent to setting
- /// [`TraceContext::with_capture_content`] before construction — there is a
- /// single storage-level gate (`ctx.capture_content`), so content dropped
- /// here can never reach any exporter.
- pub fn with_content_capture(mut self, capture_content: bool) -> Self {
- self.ctx.capture_content = capture_content;
- self
- }
-
- /// All spans recorded so far (finished and in-flight).
- pub fn spans(&self) -> &[TraceSpan] {
- &self.spans
- }
-
- /// Index of the span with `span_id`, if any.
- fn span_index_by_id(&self, span_id: &str) -> Option<usize> {
- self.spans.iter().position(|sp| sp.span_id == span_id)
- }
-
- /// Consume the collector and return its spans.
- #[cfg(test)]
- pub fn into_spans(self) -> Vec<TraceSpan> {
- self.spans
- }
-
- /// OTel-style 16-hex span id derived from a monotonic sequence. Stable
- /// and deterministic within a run, which keeps the tests reproducible.
- fn mint_span_id(&mut self) -> String {
- self.next_span_seq += 1;
- // Nonce prefix keeps the id globally unique across turns (Langfuse
- // dedupes observations by id project-wide).
- format!("{}-{:016x}", self.id_prefix, self.next_span_seq)
- }
-
- fn open_span(
- &mut self,
- kind: SpanKind,
- name: impl Into<String>,
- parent_span_id: Option<String>,
- start_unix_ms: u64,
- attributes: BTreeMap<String, serde_json::Value>,
- ) -> (String, usize) {
- let span_id = self.mint_span_id();
- let index = self.spans.len();
- self.spans.push(TraceSpan {
- trace_id: self.ctx.session_id.clone(),
- span_id: span_id.clone(),
- parent_span_id,
- name: name.into(),
- kind,
- start_unix_ms,
- end_unix_ms: None,
- status: SpanStatus::Unset,
- attributes,
- input: None,
- output: None,
- });
- (span_id, index)
- }
-
- /// Seal a span: set its end timestamp + status and merge in any extra
- /// attributes. A no-op if `index` is out of range (defensive).
- fn close_span(
- &mut self,
- index: usize,
- end_unix_ms: u64,
- status: SpanStatus,
- extra: BTreeMap<String, serde_json::Value>,
- ) {
- if let Some(span) = self.spans.get_mut(index) {
- // Don't let a late event drag end before start.
- span.end_unix_ms = Some(end_unix_ms.max(span.start_unix_ms));
- span.status = status;
- span.attributes.extend(extra);
- }
- }
-
- /// Lazily open the root turn span so a stream that begins mid-flight
- /// (or never sends `TurnStarted`) still produces a correlated tree.
- fn ensure_turn_span(&mut self, start_unix_ms: u64) -> String {
- if let Some(id) = &self.turn_span_id {
- return id.clone();
- }
- let mut attrs = BTreeMap::new();
- attrs.insert(
- "session.id".to_string(),
- serde_json::Value::String(self.ctx.session_id.clone()),
- );
- if let Some(user) = &self.ctx.user_id {
- attrs.insert(
- "user.id".to_string(),
- serde_json::Value::String(user.clone()),
- );
- }
- if let Some(client) = &self.ctx.client_id {
- attrs.insert(
- "client.id".to_string(),
- serde_json::Value::String(client.clone()),
- );
- }
- if let Some(agent) = &self.ctx.agent_id {
- attrs.insert(
- "agent.id".to_string(),
- serde_json::Value::String(agent.clone()),
- );
- }
- if let Some(source) = &self.ctx.channel_source {
- attrs.insert(
- "channel.source".to_string(),
- serde_json::Value::String(source.clone()),
- );
- }
- attrs.insert(
- "run.type".to_string(),
- serde_json::Value::String(self.ctx.run_type.as_str().to_string()),
- );
- // Every trace must end up with a Langfuse sessionId: prefer the
- // explicit grouping key (thread/conversation id), else fall back to
- // the trace id itself so the trace is never left session-less.
- let group = self
- .ctx
- .session_group
- .clone()
- .unwrap_or_else(|| self.ctx.session_id.clone());
- attrs.insert("thread.id".to_string(), serde_json::Value::String(group));
- // Trace/root-span name carries agent attribution when known.
- let name = match &self.ctx.agent_id {
- Some(agent) => format!("agent.turn:{agent}"),
- None => "agent.turn".to_string(),
- };
- log::debug!(
- "[agent-tracing] opening turn span trace_id={} name={} user_attributed={} client_attributed={} source={:?}",
- self.ctx.session_id,
- name,
- self.ctx.user_id.is_some(),
- self.ctx.client_id.is_some(),
- self.ctx.channel_source,
- );
- let (id, index) = self.open_span(SpanKind::Turn, name, None, start_unix_ms, attrs);
- self.turn_span_id = Some(id.clone());
- self.turn_span_index = Some(index);
- id
- }
-
- /// The parent any iteration / tool / subagent span should hang off:
- /// the current iteration if one is open, else the turn root.
- fn active_parent_id(&mut self, now_unix_ms: u64) -> String {
- if let Some(id) = &self.current_iteration_span_id {
- return id.clone();
- }
- self.ensure_turn_span(now_unix_ms)
- }
-
- /// Record a tool call's arguments as the span's `input`, truncated to
- /// [`MAX_TOOL_CONTENT_CHARS`]. A no-op unless content capture is on
- /// (`observability.agent_tracing.capture_content`) — when off, tool I/O
- /// never even reaches the in-memory span. `Null` arguments are skipped.
- fn capture_tool_arguments(&mut self, index: usize, arguments: &serde_json::Value) {
- if !self.ctx.capture_content || arguments.is_null() {
- return;
- }
- let serialized = arguments.to_string();
- let chars = serialized.chars().count();
- if let Some(span) = self.spans.get_mut(index) {
- span.input = Some(serde_json::Value::String(truncate_capture_text(
- &serialized,
- )));
- log::trace!(
- "[agent-tracing] captured tool input span={} chars={chars} truncated={}",
- span.name,
- chars > MAX_TOOL_CONTENT_CHARS,
- );
- }
- }
-
- /// Record a tool call's result as the span's `output`, truncated to
- /// [`MAX_TOOL_CONTENT_CHARS`]. Same capture gate as
- /// [`Self::capture_tool_arguments`]. Empty output is skipped.
- fn capture_tool_output(&mut self, index: usize, output: &str) {
- if !self.ctx.capture_content || output.is_empty() {
- return;
- }
- let chars = output.chars().count();
- if let Some(span) = self.spans.get_mut(index) {
- span.output = Some(serde_json::Value::String(truncate_capture_text(output)));
- log::trace!(
- "[agent-tracing] captured tool output span={} chars={chars} truncated={}",
- span.name,
- chars > MAX_TOOL_CONTENT_CHARS,
- );
- }
- }
-
- /// Fold a per-call `ModelCallCompleted` into the tree:
- ///
- /// 1. emit a closed [`SpanKind::Generation`] span (name `llm.<model>`)
- /// parented under the current iteration — or, for a child call
- /// (`subagent_task_id` set), under the owning subagent's current
- /// iteration — carrying exact per-call model/usage/cost plus provenance
- /// (`gen_ai.provider`) and the pricing basis the local estimator uses;
- /// 2. record the captured request messages (incl. the system prompt) and
- /// completion as the generation's input/output, gated on
- /// `capture_content` and truncated to [`MAX_MODEL_CONTENT_CHARS`];
- /// 3. accumulate reasoning / cache-creation tokens onto the root turn
- /// span, which `TurnCostUpdated` (cumulative rollup) does not carry —
- /// and, for child calls, roll model + usage onto the subagent span so
- /// a delegation (e.g. the Context Scout) surfaces its model natively.
- ///
- /// The Langfuse-facing model label is `{provider_id}.{model}` (e.g.
- /// `managed.chat-v1`, `openai.gpt-4o`).
- ///
- /// Generation start is approximated by the enclosing iteration span's
- /// start (the iteration opens on `ModelStarted`); end is the observation
- /// time of the usage record.
- #[allow(clippy::too_many_arguments)]
- fn record_model_call(
- &mut self,
- model: &str,
- provider_id: &str,
- subagent_task_id: Option<&str>,
- input: Option<&serde_json::Value>,
- output: Option<&serde_json::Value>,
- iteration: u32,
- input_tokens: u64,
- output_tokens: u64,
- cached_input_tokens: u64,
- cache_creation_tokens: u64,
- reasoning_tokens: u64,
- cost_usd: f64,
- now_unix_ms: u64,
- ) {
- // Resolve the parent + start basis: a child call nests under its
- // subagent's current iteration (else the subagent span itself); a
- // parent call nests under the turn's current iteration (else root).
- let subagent_state = subagent_task_id.and_then(|id| self.subagents.get(id));
- let (parent, start_basis_index) = match subagent_state {
- Some(state) => match &state.current_iteration_span_id {
- Some(id) => {
- let idx = self.span_index_by_id(id);
- (id.clone(), idx)
- }
- None => (
- self.spans[state.span_index].span_id.clone(),
- Some(state.span_index),
- ),
- },
- None => {
- let parent = self.active_parent_id(now_unix_ms);
- (parent, self.current_iteration_index)
- }
- };
- let start_unix_ms = start_basis_index
- .and_then(|idx| self.spans.get(idx))
- .map(|span| span.start_unix_ms)
- .unwrap_or(now_unix_ms);
-
- let labeled_model = format!("{provider_id}.{model}");
- let pricing = crate::agent::cost::lookup_pricing(model);
-
- let mut attrs = BTreeMap::new();
- attrs.insert("gen_ai.request.model".to_string(), json_str(&labeled_model));
- attrs.insert("gen_ai.provider".to_string(), json_str(provider_id));
- attrs.insert("agent.iteration".to_string(), json_u32(iteration));
- attrs.insert(
- "gen_ai.usage.input_tokens".to_string(),
- json_u64(input_tokens),
- );
- attrs.insert(
- "gen_ai.usage.output_tokens".to_string(),
- json_u64(output_tokens),
- );
- // Cache reads always flow (even 0) so usageDetails stay complete.
- attrs.insert(
- "gen_ai.usage.cached_input_tokens".to_string(),
- json_u64(cached_input_tokens),
- );
- if cache_creation_tokens > 0 {
- attrs.insert(
- "gen_ai.usage.cache_creation_tokens".to_string(),
- json_u64(cache_creation_tokens),
- );
- }
- if reasoning_tokens > 0 {
- attrs.insert(
- "gen_ai.usage.reasoning_tokens".to_string(),
- json_u64(reasoning_tokens),
- );
- }
- attrs.insert("gen_ai.usage.cost_usd".to_string(), json_f64(cost_usd));
- // Pricing basis so Langfuse cost figures are auditable against the
- // client-side estimator (USD per million tokens).
- attrs.insert(
- "gen_ai.pricing.input_per_mtok_usd".to_string(),
- json_f64(pricing.input_per_mtok_usd),
- );
- attrs.insert(
- "gen_ai.pricing.cached_input_per_mtok_usd".to_string(),
- json_f64(pricing.cached_input_per_mtok_usd),
- );
- attrs.insert(
- "gen_ai.pricing.output_per_mtok_usd".to_string(),
- json_f64(pricing.output_per_mtok_usd),
- );
-
- log::debug!(
- "[agent-tracing] generation span model={labeled_model} \
- iteration={iteration} child={} in={input_tokens} out={output_tokens} \
- cost_usd={cost_usd:.6} input_captured={} output_captured={}",
- subagent_task_id.is_some(),
- input.is_some(),
- output.is_some(),
- );
- let (_, index) = self.open_span(
- SpanKind::Generation,
- format!("llm.{model}"),
- Some(parent),
- start_unix_ms,
- attrs,
- );
- // Captured request messages (incl. system prompt) + completion become
- // the generation's input/output — only while content capture is on,
- // truncated so one huge context window can't bloat the trace batch.
- if self.ctx.capture_content {
- if let Some(span) = self.spans.get_mut(index) {
- if let Some(value) = input {
- span.input = Some(capture_model_content(value));
- }
- if let Some(value) = output {
- span.output = Some(capture_model_content(value));
- }
- }
- }
- self.close_span(index, now_unix_ms, SpanStatus::Ok, BTreeMap::new());
-
- // Child call: roll model + usage + cost onto the owning subagent span
- // so the delegation row (e.g. `subagent.Context Scout`) natively shows
- // which model served it and what it cost.
- if let Some(state_index) = subagent_task_id
- .and_then(|id| self.subagents.get(id))
- .map(|state| state.span_index)
- {
- if let Some(span) = self.spans.get_mut(state_index) {
- span.attributes
- .insert("gen_ai.request.model".to_string(), json_str(&labeled_model));
- span.attributes
- .insert("gen_ai.provider".to_string(), json_str(provider_id));
- for (key, add) in [
- ("gen_ai.usage.input_tokens", input_tokens),
- ("gen_ai.usage.output_tokens", output_tokens),
- ("gen_ai.usage.cached_input_tokens", cached_input_tokens),
- ] {
- let prior = span
- .attributes
- .get(key)
- .and_then(serde_json::Value::as_u64)
- .unwrap_or(0);
- span.attributes
- .insert(key.to_string(), json_u64(prior.saturating_add(add)));
- }
- let prior_cost = span
- .attributes
- .get("gen_ai.usage.cost_usd")
- .and_then(serde_json::Value::as_f64)
- .unwrap_or(0.0);
- span.attributes.insert(
- "gen_ai.usage.cost_usd".to_string(),
- json_f64(prior_cost + cost_usd),
- );
- }
- return;
- }
-
- // Root rollup for the usage dimensions the cumulative TurnCostUpdated
- // event does not carry (reasoning / cache-creation), plus provenance
- // and the provider-labeled model (TurnCostUpdated only knows the raw
- // model handle and can fire before the first per-call event).
- let root = match self.turn_span_index {
- Some(idx) => idx,
- None => {
- self.ensure_turn_span(now_unix_ms);
- self.turn_span_index.expect("turn span just created")
- }
- };
- if let Some(span) = self.spans.get_mut(root) {
- span.attributes
- .insert("gen_ai.provider".to_string(), json_str(provider_id));
- span.attributes
- .insert("gen_ai.request.model".to_string(), json_str(&labeled_model));
- for (key, add) in [
- ("gen_ai.usage.reasoning_tokens", reasoning_tokens),
- ("gen_ai.usage.cache_creation_tokens", cache_creation_tokens),
- ] {
- if add == 0 && !span.attributes.contains_key(key) {
- continue;
- }
- let prior = span
- .attributes
- .get(key)
- .and_then(serde_json::Value::as_u64)
- .unwrap_or(0);
- span.attributes
- .insert(key.to_string(), json_u64(prior.saturating_add(add)));
- }
- }
- }
-
- fn close_current_iteration(&mut self, end_unix_ms: u64) {
- if let Some(index) = self.current_iteration_index.take() {
- self.close_span(index, end_unix_ms, SpanStatus::Ok, BTreeMap::new());
- }
- self.current_iteration_span_id = None;
- }
-}
diff --git a/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_02.rs b/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_02.rs
deleted file mode 100644
index 294c2e9dc0..0000000000
--- a/crates/openhuman-core/src/agent/progress_tracing_impl_01_part_02.rs
+++ /dev/null
@@ -1,469 +0,0 @@
-
-impl SpanCollector {
- /// Fold a single progress event into the span tree, stamped at
- /// `now_unix_ms` (the consumer's wall clock when it observed the event).
- pub fn record(&mut self, event: &AgentProgress, now_unix_ms: u64) {
- match event {
- AgentProgress::TurnStarted => {
- self.ensure_turn_span(now_unix_ms);
- }
-
- AgentProgress::IterationStarted {
- iteration,
- max_iterations,
- } => {
- self.close_current_iteration(now_unix_ms);
- let parent = self.ensure_turn_span(now_unix_ms);
- let mut attrs = BTreeMap::new();
- attrs.insert("agent.iteration".to_string(), json_u32(*iteration));
- attrs.insert(
- "agent.max_iterations".to_string(),
- json_u32(*max_iterations),
- );
- let (id, index) = self.open_span(
- SpanKind::Iteration,
- format!("agent.iteration#{iteration}"),
- Some(parent),
- now_unix_ms,
- attrs,
- );
- self.current_iteration_span_id = Some(id);
- self.current_iteration_index = Some(index);
- }
-
- AgentProgress::ToolCallStarted {
- call_id,
- tool_name,
- arguments,
- iteration,
- ..
- } => {
- let parent = self.active_parent_id(now_unix_ms);
- let mut attrs = BTreeMap::new();
- attrs.insert("tool.name".to_string(), json_str(tool_name));
- attrs.insert("tool.call_id".to_string(), json_str(call_id));
- attrs.insert("agent.iteration".to_string(), json_u32(*iteration));
- let (_, index) = self.open_span(
- SpanKind::Tool,
- format!("tool.{tool_name}"),
- Some(parent),
- now_unix_ms,
- attrs,
- );
- self.capture_tool_arguments(index, arguments);
- self.open_tools.insert(call_id.clone(), index);
- }
-
- AgentProgress::ToolCallCompleted {
- call_id,
- success,
- output_chars,
- output,
- arguments,
- elapsed_ms,
- failure,
- ..
- } => {
- if let Some(index) = self.open_tools.remove(call_id) {
- // The tinyagents path emits `Null` arguments on the started
- // event and the real captured arguments on completion —
- // backfill the span input when it's still empty.
- if self.spans[index].input.is_none() {
- if let Some(arguments) = arguments {
- self.capture_tool_arguments(index, arguments);
- }
- }
- self.capture_tool_output(index, output);
- let start = self.spans[index].start_unix_ms;
- let mut extra = BTreeMap::new();
- extra.insert(
- "tool.success".to_string(),
- serde_json::Value::Bool(*success),
- );
- extra.insert("tool.output_chars".to_string(), json_usize(*output_chars));
- extra.insert("tool.elapsed_ms".to_string(), json_u64(*elapsed_ms));
- // Failed tool calls surface a Langfuse statusMessage: the
- // classified plain-language cause, truncated, gated on
- // content capture (it can quote user data / paths).
- if let Some(failure) = failure {
- if self.ctx.capture_content {
- extra.insert(
- "error.message".to_string(),
- serde_json::Value::String(truncate_chars(
- &failure.cause_plain,
- MAX_ERROR_MESSAGE_CHARS,
- )),
- );
- }
- }
- self.close_span(index, start + elapsed_ms, status_of(*success), extra);
- }
- }
-
- AgentProgress::ModelCallCompleted {
- model,
- provider_id,
- subagent_task_id,
- input,
- output,
- iteration,
- input_tokens,
- output_tokens,
- cached_input_tokens,
- cache_creation_tokens,
- reasoning_tokens,
- cost_usd,
- } => {
- self.record_model_call(
- model,
- provider_id,
- subagent_task_id.as_deref(),
- input.as_ref(),
- output.as_ref(),
- *iteration,
- *input_tokens,
- *output_tokens,
- *cached_input_tokens,
- *cache_creation_tokens,
- *reasoning_tokens,
- *cost_usd,
- now_unix_ms,
- );
- }
-
- AgentProgress::SubagentSpawned {
- agent_id,
- task_id,
- mode,
- dedicated_thread,
- prompt_chars,
- prompt,
- display_name,
- ..
- } => {
- let parent = self.active_parent_id(now_unix_ms);
- let label = display_name.clone().unwrap_or_else(|| agent_id.clone());
- let mut attrs = BTreeMap::new();
- attrs.insert("subagent.agent_id".to_string(), json_str(agent_id));
- attrs.insert("subagent.task_id".to_string(), json_str(task_id));
- attrs.insert("subagent.mode".to_string(), json_str(mode));
- attrs.insert(
- "subagent.dedicated_thread".to_string(),
- serde_json::Value::Bool(*dedicated_thread),
- );
- attrs.insert(
- "subagent.prompt_chars".to_string(),
- json_usize(*prompt_chars),
- );
- if let Some(name) = display_name {
- attrs.insert("subagent.display_name".to_string(), json_str(name));
- }
- let (_, index) = self.open_span(
- SpanKind::Subagent,
- format!("subagent.{label}"),
- Some(parent),
- now_unix_ms,
- attrs,
- );
- // The delegated prompt is the subagent span's input (gated +
- // truncated like model content — scout prompts run 10k+ chars).
- if self.ctx.capture_content && !prompt.is_empty() {
- if let Some(span) = self.spans.get_mut(index) {
- span.input = Some(serde_json::Value::String(truncate_chars(
- prompt,
- MAX_MODEL_CONTENT_CHARS,
- )));
- }
- }
- self.subagents.insert(
- task_id.clone(),
- SubagentState {
- span_index: index,
- current_iteration_span_id: None,
- open_tools: BTreeMap::new(),
- },
- );
- }
-
- AgentProgress::SubagentIterationStarted {
- task_id,
- iteration,
- max_iterations,
- extended_policy,
- ..
- } => {
- // Resolve parent + prior child iteration up front so we don't
- // hold a borrow across the mutating open_span call.
- let (parent_id, prior_iteration_id) = match self.subagents.get(task_id) {
- Some(state) => (
- self.spans[state.span_index].span_id.clone(),
- state.current_iteration_span_id.clone(),
- ),
- None => return,
- };
- if let Some(prior) = prior_iteration_id {
- if let Some(idx) = self.span_index_by_id(&prior) {
- self.close_span(idx, now_unix_ms, SpanStatus::Ok, BTreeMap::new());
- }
- }
- let mut attrs = BTreeMap::new();
- attrs.insert("agent.iteration".to_string(), json_u32(*iteration));
- attrs.insert(
- "agent.max_iterations".to_string(),
- json_u32(*max_iterations),
- );
- attrs.insert(
- "agent.extended_policy".to_string(),
- serde_json::Value::Bool(*extended_policy),
- );
- let (id, _) = self.open_span(
- SpanKind::SubagentIteration,
- format!("subagent.iteration#{iteration}"),
- Some(parent_id),
- now_unix_ms,
- attrs,
- );
- if let Some(state) = self.subagents.get_mut(task_id) {
- state.current_iteration_span_id = Some(id);
- }
- }
-
- AgentProgress::SubagentToolCallStarted {
- task_id,
- call_id,
- tool_name,
- arguments,
- iteration,
- ..
- } => {
- let parent_id = match self.subagents.get(task_id) {
- Some(state) => match &state.current_iteration_span_id {
- Some(id) => id.clone(),
- None => self.spans[state.span_index].span_id.clone(),
- },
- None => return,
- };
- let mut attrs = BTreeMap::new();
- attrs.insert("tool.name".to_string(), json_str(tool_name));
- attrs.insert("tool.call_id".to_string(), json_str(call_id));
- attrs.insert("agent.iteration".to_string(), json_u32(*iteration));
- let (_, index) = self.open_span(
- SpanKind::Tool,
- format!("tool.{tool_name}"),
- Some(parent_id),
- now_unix_ms,
- attrs,
- );
- self.capture_tool_arguments(index, arguments);
- if let Some(state) = self.subagents.get_mut(task_id) {
- state.open_tools.insert(call_id.clone(), index);
- }
- }
-
- AgentProgress::SubagentToolCallCompleted {
- task_id,
- call_id,
- success,
- output_chars,
- output,
- arguments,
- elapsed_ms,
- ..
- } => {
- let Some(index) = self
- .subagents
- .get_mut(task_id)
- .and_then(|state| state.open_tools.remove(call_id))
- else {
- return;
- };
- if self.spans[index].input.is_none() {
- if let Some(arguments) = arguments {
- self.capture_tool_arguments(index, arguments);
- }
- }
- self.capture_tool_output(index, output);
- let start = self.spans[index].start_unix_ms;
- let mut extra = BTreeMap::new();
- extra.insert(
- "tool.success".to_string(),
- serde_json::Value::Bool(*success),
- );
- extra.insert("tool.output_chars".to_string(), json_usize(*output_chars));
- extra.insert("tool.elapsed_ms".to_string(), json_u64(*elapsed_ms));
- self.close_span(index, start + elapsed_ms, status_of(*success), extra);
- }
-
- AgentProgress::SubagentCompleted {
- task_id,
- elapsed_ms,
- iterations,
- output_chars,
- output,
- ..
- } => {
- let Some(state) = self.subagents.remove(task_id) else {
- return;
- };
- if let Some(id) = state.current_iteration_span_id.clone() {
- if let Some(idx) = self.span_index_by_id(&id) {
- self.close_span(idx, now_unix_ms, SpanStatus::Ok, BTreeMap::new());
- }
- }
- // The subagent's final assistant text is the span's output
- // (same gate + cap as its prompt input).
- if self.ctx.capture_content && !output.is_empty() {
- if let Some(span) = self.spans.get_mut(state.span_index) {
- span.output = Some(serde_json::Value::String(truncate_chars(
- output,
- MAX_MODEL_CONTENT_CHARS,
- )));
- }
- }
- let start = self.spans[state.span_index].start_unix_ms;
- let mut extra = BTreeMap::new();
- extra.insert("subagent.iterations".to_string(), json_u32(*iterations));
- extra.insert(
- "subagent.output_chars".to_string(),
- json_usize(*output_chars),
- );
- extra.insert("subagent.elapsed_ms".to_string(), json_u64(*elapsed_ms));
- self.close_span(state.span_index, start + elapsed_ms, SpanStatus::Ok, extra);
- }
-
- AgentProgress::SubagentFailed { task_id, error, .. } => {
- let Some(state) = self.subagents.remove(task_id) else {
- return;
- };
- if let Some(id) = state.current_iteration_span_id.clone() {
- if let Some(idx) = self.span_index_by_id(&id) {
- self.close_span(idx, now_unix_ms, SpanStatus::Error, BTreeMap::new());
- }
- }
- let mut extra = BTreeMap::new();
- // Always record that an error occurred and its length. The raw
- // error text (may embed paths / payloads) is recorded — truncated
- // — only when content capture is on, and surfaces in Langfuse as
- // the observation statusMessage.
- extra.insert("error".to_string(), serde_json::Value::Bool(true));
- extra.insert("error.length".to_string(), json_usize(error.len()));
- if self.ctx.capture_content {
- extra.insert(
- "error.message".to_string(),
- serde_json::Value::String(truncate_chars(error, MAX_ERROR_MESSAGE_CHARS)),
- );
- }
- self.close_span(state.span_index, now_unix_ms, SpanStatus::Error, extra);
- }
-
- AgentProgress::TurnCostUpdated {
- model,
- input_tokens,
- output_tokens,
- cached_input_tokens,
- total_usd,
- ..
- } => {
- // Cumulative cost/usage rides on the root turn span so a trace
- // viewer shows the whole-run total at the top.
- let index = match self.turn_span_index {
- Some(idx) => idx,
- None => {
- self.ensure_turn_span(now_unix_ms);
- self.turn_span_index.expect("turn span just created")
- }
- };
- if let Some(span) = self.spans.get_mut(index) {
- span.attributes
- .insert("gen_ai.request.model".to_string(), json_str(model));
- span.attributes.insert(
- "gen_ai.usage.input_tokens".to_string(),
- json_u64(*input_tokens),
- );
- span.attributes.insert(
- "gen_ai.usage.output_tokens".to_string(),
- json_u64(*output_tokens),
- );
- span.attributes.insert(
- "gen_ai.usage.cached_input_tokens".to_string(),
- json_u64(*cached_input_tokens),
- );
- span.attributes
- .insert("gen_ai.usage.cost_usd".to_string(), json_f64(*total_usd));
- }
- }
-
- AgentProgress::TurnContent { input, output } => {
- // Storage-level privacy gate (#4454): prompt/reply text is
- // attached to the span ONLY when content capture is opted in.
- // With the gate off (default), the content is dropped here so no
- // exporter — NDJSON file, app log, or Langfuse push — can ever
- // serialize it. This is the single choke point; the exporters
- // deliberately do not re-check the flag.
- if !self.ctx.capture_content {
- log::debug!(
- target: "agent-tracing",
- "[agent-tracing] TurnContent dropped at storage (capture_content=false)"
- );
- return;
- }
- let index = match self.turn_span_index {
- Some(idx) => idx,
- None => {
- self.ensure_turn_span(now_unix_ms);
- self.turn_span_index.expect("turn span just created")
- }
- };
- if let Some(span) = self.spans.get_mut(index) {
- if let Some(text) = input {
- span.input = Some(serde_json::Value::String(text.clone()));
- }
- if let Some(text) = output {
- span.output = Some(serde_json::Value::String(text.clone()));
- }
- log::debug!(
- target: "agent-tracing",
- "[agent-tracing] TurnContent attached to turn span (capture_content=true)"
- );
- }
- }
-
- AgentProgress::TurnCompleted { iterations } => {
- self.close_current_iteration(now_unix_ms);
- if let Some(index) = self.turn_span_index {
- let mut extra = BTreeMap::new();
- extra.insert("agent.iterations".to_string(), json_u32(*iterations));
- self.close_span(index, now_unix_ms, SpanStatus::Ok, extra);
- }
- }
-
- // Content-bearing / streaming events carry prompt text, tool
- // arguments, or model output — never exported (privacy rule).
- AgentProgress::TextDelta { .. }
- | AgentProgress::ThinkingDelta { .. }
- | AgentProgress::ToolCallArgsDelta { .. }
- | AgentProgress::SubagentTextDelta { .. }
- | AgentProgress::SubagentThinkingDelta { .. }
- | AgentProgress::SubagentAwaitingUser { .. }
- | AgentProgress::TaskBoardUpdated { .. } => {}
- }
- }
-
- /// Seal every span still open after the stream closes. Idempotent.
- pub fn finish(&mut self, now_unix_ms: u64) {
- let open: Vec<usize> = self
- .spans
- .iter()
- .enumerate()
- .filter(|(_, span)| span.end_unix_ms.is_none())
- .map(|(idx, _)| idx)
- .collect();
- for idx in open {
- self.close_span(idx, now_unix_ms, SpanStatus::Unset, BTreeMap::new());
- }
- self.current_iteration_span_id = None;
- self.current_iteration_index = None;
- self.open_tools.clear();
- self.subagents.clear();
- }
-}