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(orchestration): remove unused task ledger and event bridge files
Steven Enamakel4e1f7f9f057e
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger.rsnew file mode 100644index 0000000000..d379c8e8aa--- /dev/null+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/task_ledger.rs@@ -0,0 +1,374 @@+//! Durable per-workspace task store plumbing and the typed lifecycle ledger+//! that mirrors each detached sub-agent's status into it (issue #4249).+//!+//! The caching and the durable→memory fallback are TinyAgents'+//! (`TaskStoreRegistry` / `open_jsonl_task_store_or_memory`): opening a second+//! store over the same append log would give two writers with independently+//! replayed state, and a workspace that cannot be written should degrade to an+//! in-memory ledger rather than take orchestration down. What stays here is the+//! path layout, the record shape, and the reconciliation of orphaned tasks on+//! boot.++use std::path::{Path, PathBuf};+use std::sync::{Arc, OnceLock};++use crate::agent::tinyagents::orchestration::{+ open_jsonl_task_store_or_memory, reconcile_orphaned_tasks, InMemoryTaskStore,+ OrchestrationTaskFilter, OrchestrationTaskKind, OrchestrationTaskRecord,+ OrchestrationTaskResult, OrchestrationTaskSpec, OrchestrationTaskStatus, TaskStore,+ TaskStoreRegistry,+};+use tinyagents_harness::ids::TaskId;++use super::registry::DETACHED_LEDGER_TIMEOUT_MS;+use super::wait::{WaitError, WaitOutcome};+use super::SubagentStatus;++/// Where a workspace's detached-task ledger lives.+///+/// A product path, not a generic one: TinyAgents opens whatever file it is+/// given, and this is where OpenHuman keeps it.+fn task_store_path(workspace_dir: &Path) -> PathBuf {+ workspace_dir+ .join(".openhuman")+ .join("orchestration_tasks.jsonl")+}++#[cfg(test)]+fn default_task_store_workspace() -> PathBuf {+ crate::config::default_root_openhuman_dir()+ .map(|root| root.join("workspace"))+ .unwrap_or_else(|_| PathBuf::from(".openhuman").join("workspace"))+}++/// Process-wide typed lifecycle ledger for detached sub-agents (issue #4249),+/// one durable store per workspace.+static TASK_STORES: OnceLock<TaskStoreRegistry<PathBuf>> = OnceLock::new();++fn task_stores() -> &'static TaskStoreRegistry<PathBuf> {+ TASK_STORES.get_or_init(|| {+ TaskStoreRegistry::new(|workspace_dir: &PathBuf| {+ open_jsonl_task_store_or_memory(&task_store_path(workspace_dir))+ })+ })+}++/// The ledger for `workspace_dir`, opening it on first use.+///+/// A poisoned registry lock is degraded to a throwaway in-memory store rather+/// than propagated: every caller here is a best-effort bookkeeping path, and a+/// panic in an unrelated task must not turn sub-agent spawning into a second+/// panic.+pub(crate) fn task_store_for_workspace(workspace_dir: &Path) -> Arc<dyn TaskStore> {+ let key = workspace_dir.to_path_buf();+ match task_stores().get_or_open(&key) {+ Ok(store) => store,+ Err(err) => {+ log::warn!(+ "[running_subagents] task store registry unavailable for {}; using a detached in-memory ledger: {}",+ workspace_dir.display(),+ err+ );+ Arc::new(InMemoryTaskStore::new())+ }+ }+}++#[cfg(test)]+fn task_store() -> Arc<dyn TaskStore> {+ let workspace = default_task_store_workspace();+ task_store_for_workspace(&workspace)+}++/// Record a freshly-spawned sub-agent in the store (`Pending` → `Running`).+/// Insert errors (e.g. a re-used task id across tests) are intentionally ignored.+pub(crate) fn record_spawned(+ task_id: &str,+ agent_id: &str,+ parent_session: &str,+ session_parent_prefix: Option<&str>,+ subagent_session_id: Option<&str>,+ workspace_dir: &Path,+ parent_thread_id: Option<&str>,+) {+ let store = task_store_for_workspace(workspace_dir);+ let root_run_id = session_parent_prefix+ .and_then(|prefix| prefix.split("__").next())+ .filter(|root| !root.is_empty())+ .unwrap_or(parent_session);+ let mut spec = OrchestrationTaskSpec::new(+ task_id.to_string(),+ OrchestrationTaskKind::SubAgent {+ agent: agent_id.to_string(),+ },+ )+ .with_lineage(parent_session.to_string(), root_run_id.to_string())+ .with_timeout_ms(DETACHED_LEDGER_TIMEOUT_MS)+ .with_metadata("parentSession", parent_session.to_string())+ .with_metadata("rootSession", root_run_id.to_string())+ .with_metadata(+ "defaultWaitTimeoutMs",+ DETACHED_LEDGER_TIMEOUT_MS.to_string(),+ )+ .with_metadata("workspaceDir", workspace_dir.display().to_string());+ if let Some(session_parent_prefix) = session_parent_prefix {+ spec = spec.with_metadata("sessionParentPrefix", session_parent_prefix.to_string());+ }+ if let Some(parent_thread_id) = parent_thread_id {+ spec = spec+ .with_thread(parent_thread_id.to_string())+ .with_metadata("parentThreadId", parent_thread_id.to_string());+ }+ if let Some(subagent_session_id) = subagent_session_id {+ spec = spec.with_metadata("subagentSessionId", subagent_session_id.to_string());+ }+ let _ = store.insert(spec);+ let _ = store.mark_running(&TaskId::new(task_id));+}++/// Mirror a child's published [`SubagentStatus`] into the typed store. Transition+/// errors (already terminal / cancelled) are ignored — first writer wins.+pub(crate) fn record_status(workspace_dir: &Path, task_id: &str, status: &SubagentStatus) {+ let store = task_store_for_workspace(workspace_dir);+ let id = TaskId::new(task_id);+ log::debug!(+ "[running_subagents] recording task status task_id={} workspace_dir={} terminal={}",+ task_id,+ workspace_dir.display(),+ status.is_terminal()+ );+ match status {+ SubagentStatus::Completed { output, .. } => {+ let _ = store.complete(&id, OrchestrationTaskResult::text(output.clone()));+ }+ SubagentStatus::Failed { error } => {+ let _ = store.fail(&id, error.clone());+ }+ SubagentStatus::AwaitingUser { .. } => {+ let _ = store.mark_awaiting(&id);+ }+ SubagentStatus::Running => {}+ }+}++/// Record a cancellation (`CancelRequested` → `Cancelled`) for `task_id`.+pub(crate) fn record_cancelled(workspace_dir: &Path, task_id: &str) {+ let store = task_store_for_workspace(workspace_dir);+ let id = TaskId::new(task_id);+ log::debug!(+ "[running_subagents] recording task cancellation task_id={} workspace_dir={}",+ task_id,+ workspace_dir.display()+ );+ let _ = store.request_cancel(&id);+ let _ = store.mark_cancelled(&id);+}++pub(crate) fn list_task_records(workspace_dir: &Path) -> Vec<OrchestrationTaskRecord> {+ let store = task_store_for_workspace(workspace_dir);+ store.list(OrchestrationTaskFilter::default().with_kind("sub_agent"))+}++/// Restart/resume reconciliation for detached sub-agents (issue #4249 / 07.2+/// steps 2 & 4).+///+/// A detached sub-agent runs as a `tokio` task owned by the process that spawned+/// it. When the core restarts, that task — and its live [`tokio::task::AbortHandle`] /+/// [`tinyagents_harness::CancellationToken`] — is gone, but the durable+/// `JsonlTaskStore` still holds a non-terminal (`Pending`/`Running`/`Awaiting`/+/// `CancelRequested`) record for it. Such a record is **orphaned**: there is no+/// live executor to re-attach to (OpenHuman spawns child processes, so an+/// in-flight run from a dead parent cannot be resumed), and the run-ledger+/// finalizer never observed a terminal event, so it would otherwise render as a+/// perpetual "running" entry.+///+/// This scans the workspace-scoped store for those orphans and reconciles each+/// to a terminal state — `Cancelled` if a cancel had been requested, otherwise+/// `Failed` with an "orphaned by restart" reason — then emits the existing typed+/// terminal lifecycle event ([`crate::agent::orchestration::subagent_events::publish_subagent_failed`])+/// so the run ledger finalizes. Best-effort and non-fatal: per-task transition+/// errors (e.g. a record that raced to terminal) are logged and skipped, and a+/// store-open failure simply reconciles nothing. Returns the count reconciled.+pub(crate) fn reconcile_orphaned_tasks_on_boot(workspace_dir: &Path) -> usize {+ let store = task_store_for_workspace(workspace_dir);++ // The sweep itself — which statuses are live, and which terminal state each+ // becomes — is TinyAgents'. What stays here is the reason a *sub-agent*+ // orphan carries, and the lifecycle event that finalizes OpenHuman's run+ // ledger afterwards.+ let report = reconcile_orphaned_tasks(+ store.as_ref(),+ OrchestrationTaskFilter::default().with_kind("sub_agent"),+ &|record| orphaned_subagent_reason(record.status),+ );++ if report.is_empty() {+ log::debug!(+ "[running_subagents] reconcile found no orphaned sub-agent tasks workspace_dir={}",+ workspace_dir.display()+ );+ return 0;+ }++ for task in report.settled() {+ let task_id = task.task_id.as_str().to_string();+ let prior = task_status_label(task.prior_status);+ let reason = orphaned_subagent_reason(task.prior_status);+ let parent_session = record_parent_session(&task.record)+ .unwrap_or_default()+ .to_string();+ let agent_id = record_agent_id(&task.record);+ // Reuse the 05.2 typed terminal lifecycle helper so the run ledger+ // finalizes exactly as it would for a live failure.+ crate::agent::orchestration::subagent_events::publish_subagent_failed(+ parent_session,+ task_id.clone(),+ agent_id,+ reason,+ );+ log::info!(+ "[running_subagents] reconciled orphaned sub-agent task_id={} prior_status={} -> terminal",+ task_id,+ prior+ );+ }++ let reconciled = report.reconciled_count();+ log::info!(+ "[running_subagents] reconciled {reconciled} orphaned sub-agent task(s) on boot workspace_dir={} errors={}",+ workspace_dir.display(),+ report.error_count()+ );+ reconciled+}++/// The reason an orphaned sub-agent record is settled with.+///+/// Built in one place because it is written twice — into the store by the+/// reconciler, and into the lifecycle event the run ledger reads. If those two+/// ever disagreed, the ledger would explain a failure differently from the+/// record behind it.+fn orphaned_subagent_reason(prior_status: OrchestrationTaskStatus) -> String {+ format!(+ "sub-agent orphaned by core restart (was `{}`)",+ task_status_label(prior_status)+ )+}++pub(crate) fn record_parent_session(record: &OrchestrationTaskRecord) -> Option<&str> {+ record+ .spec+ .metadata+ .get("parentSession")+ .map(String::as_str)+}++pub(crate) fn record_subagent_session_id(record: &OrchestrationTaskRecord) -> Option<&str> {+ record+ .spec+ .metadata+ .get("subagentSessionId")+ .map(String::as_str)+}++pub(crate) fn record_agent_id(record: &OrchestrationTaskRecord) -> String {+ match &record.spec.kind {+ OrchestrationTaskKind::SubAgent { agent } => agent.clone(),+ _ => "subagent".to_string(),+ }+}++pub(crate) fn task_record_for_task_in_workspace(+ workspace_dir: &Path,+ task_id: &str,+ parent_session: &str,+) -> Result<OrchestrationTaskRecord, WaitError> {+ let id = TaskId::new(task_id);+ let Some(record) = task_store_for_workspace(workspace_dir).get(&id) else {+ return Err(WaitError::Unknown);+ };+ if !matches!(record.spec.kind, OrchestrationTaskKind::SubAgent { .. }) {+ return Err(WaitError::Unknown);+ }+ if record_parent_session(&record) != Some(parent_session) {+ return Err(WaitError::NotOwned);+ }+ Ok(record)+}++pub(crate) fn record_to_status(record: OrchestrationTaskRecord) -> WaitOutcome {+ match record.status {+ OrchestrationTaskStatus::Completed => {+ let output = record+ .result+ .and_then(|result| {+ result+ .text+ .or_else(|| result.output.map(|output| output.to_string()))+ })+ .unwrap_or_default();+ WaitOutcome::Terminal(SubagentStatus::Completed {+ output,+ iterations: 0,+ })+ }+ OrchestrationTaskStatus::Awaiting => WaitOutcome::Terminal(SubagentStatus::AwaitingUser {+ question: record.error.unwrap_or_else(|| {+ "sub-agent is awaiting user input; no clarification text was available from the durable task store".to_string()+ }),+ }),+ OrchestrationTaskStatus::Failed+ | OrchestrationTaskStatus::TimedOut+ | OrchestrationTaskStatus::Abandoned => WaitOutcome::Terminal(SubagentStatus::Failed {+ error: record.error.unwrap_or_else(|| {+ format!(+ "sub-agent reached durable task status `{}`",+ task_status_label(record.status)+ )+ }),+ }),+ OrchestrationTaskStatus::Cancelled => WaitOutcome::Terminal(SubagentStatus::Failed {+ error: "sub-agent was cancelled".to_string(),+ }),+ OrchestrationTaskStatus::Pending+ | OrchestrationTaskStatus::Running+ | OrchestrationTaskStatus::CancelRequested => WaitOutcome::TimedOut(SubagentStatus::Running),+ }+}++pub(crate) fn task_status_label(status: OrchestrationTaskStatus) -> &'static str {+ match status {+ OrchestrationTaskStatus::Pending => "pending",+ OrchestrationTaskStatus::Running => "running",+ OrchestrationTaskStatus::Awaiting => "awaiting",+ OrchestrationTaskStatus::Completed => "completed",+ OrchestrationTaskStatus::Failed => "failed",+ OrchestrationTaskStatus::CancelRequested => "cancel_requested",+ OrchestrationTaskStatus::Cancelled => "cancelled",+ OrchestrationTaskStatus::TimedOut => "timed_out",+ OrchestrationTaskStatus::Abandoned => "abandoned",+ }+}++/// Snapshot the typed lifecycle records, optionally scoped to a `parent_session`.+#[cfg(test)]+pub(crate) fn task_records(parent_session: Option<&str>) -> Vec<OrchestrationTaskRecord> {+ let _ = task_store();+ let stores: Vec<Arc<dyn TaskStore>> = task_stores().values().unwrap_or_default();+ let all: Vec<OrchestrationTaskRecord> = stores+ .into_iter()+ .flat_map(|store| store.list(OrchestrationTaskFilter::default()))+ .collect();+ log::trace!(+ "[running_subagents] task_records loaded records={} parent_session_filter={:?}",+ all.len(),+ parent_session+ );+ match parent_session {+ Some(ps) => all+ .into_iter()+ .filter(|r| r.spec.metadata.get("parentSession").map(String::as_str) == Some(ps))+ .collect(),+ None => all,+ }+}diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rsnew file mode 100644index 0000000000..a0613e4c07--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs@@ -0,0 +1,1002 @@+//! [`OpenhumanEventBridge`] — the [`EventListener`] that mirrors the+//! `tinyagents` harness event stream onto openhuman's [`AgentProgress`] ++//! cost tracker (issue #4249). See the parent module doc for the overview.++use std::sync::atomic::Ordering;+use std::sync::{Arc, Mutex};++use tokio::sync::mpsc::Sender;++use tinyagents_harness::cache::CacheLayoutEvent;+use tinyagents_harness::events::{AgentEvent, EventListener, EventRecord};+use tinyinference::usage::Usage;++use crate::agent::progress::AgentProgress;+use crate::inference::provider::UsageInfo;+use crate::tools::traits::humanize_tool_name;++use super::cap_pauser::{IterationCursor, ProviderUsageCarry, SubagentScope, ToolFailureMap, ToolNameMap};++#[derive(Default)]+struct BridgeState {+ input_tokens: u64,+ output_tokens: u64,+ cached_input_tokens: u64,+ charged_amount_usd: f64,+ /// Local response-cache hits observed on this turn (issue #4249, 03.2). A hit+ /// means the harness served a model call from its [`ResponseCache`] without+ /// invoking the provider. Additive counters — a follow-up (coordinated with+ /// workstream 06) wires these into the cost-footer DTO; today they are logged+ /// with a grep-friendly `[cache]` prefix and exposed via [`OpenhumanEventBridge::cache_counts`].+ cache_hits: u64,+ /// Local response-cache misses observed on this turn (provider *was* invoked).+ cache_misses: u64,+}++/// Per-model-call figures `record_usage` resolved from the provider-usage+/// carry (charged>estimate cost precedence + cache/reasoning breakdown),+/// keyed by iteration so the subsequent `ModelCompleted` projection reports+/// the same numbers as the wallet accounting.+#[derive(Clone, Copy, Debug)]+struct ResolvedCallFigures {+ cost_usd: f64,+ cache_creation_tokens: u64,+ reasoning_tokens: u64,+}++/// An [`EventListener`] that mirrors harness events onto openhuman's progress+/// sink and cost tracker.+pub(crate) struct OpenhumanEventBridge {+ on_progress: Option<Sender<AgentProgress>>,+ model: String,+ /// Telemetry provider id (`"managed"`, `"openai"`, …) — from+ /// [`Provider::telemetry_provider_id`](crate::inference::provider::Provider::telemetry_provider_id).+ /// Rides on `ModelCallCompleted` so trace exporters render the Langfuse+ /// model as `{provider_id}.{model}`.+ provider_id: String,+ max_iterations: u32,+ /// `None` for a parent turn; `Some` to emit child-scoped `Subagent*` events.+ scope: Option<SubagentScope>,+ /// Shared with the model adapter so thinking deltas line up with the+ /// model call (iteration) they belong to.+ cursor: IterationCursor,+ /// Shared `call_id → tool_name` map written by the model adapter's+ /// `ThinkingForwarder` on tool-call start; read here to label the+ /// incremental tool-argument fragments projected off the crate stream.+ tool_names: ToolNameMap,+ /// Shared `call_id → (success, failure, elapsed_ms, output_chars)`+ /// side-channel written by `ToolOutcomeCaptureMiddleware`; read when+ /// projecting `ToolCallCompleted`.+ failure_map: ToolFailureMap,+ /// Shared FIFO carry of the per-call provider `UsageInfo` the model adapter+ /// observed; drained in `record_usage` to restore backend-charged USD ++ /// context-window + cache-creation/reasoning tokens the crate `Usage` drops.+ usage_carry: ProviderUsageCarry,+ /// Model-call iterations whose `UsageRecorded` has already been folded into+ /// the global cost tracker (W2-budget-dedupe). A single model call can now+ /// surface **two** `UsageRecorded` events — one from the harness runtime+ /// (`agent_loop`, always) and one from the observe-only crate+ /// `BudgetMiddleware::after_model` — both carrying identical usage and both+ /// delivered to this bridge. Keyed on the run-scoped model-call identity (the+ /// iteration cursor, bumped once per `ModelStarted`) so a given call's usage+ /// is recorded exactly once. See [`OpenhumanEventBridge::record_usage`].+ recorded_iterations: Mutex<std::collections::HashSet<u32>>,+ /// Per-iteration figures resolved by `record_usage` (see+ /// [`ResolvedCallFigures`]); taken by the `ModelCompleted` arm.+ resolved_calls: Mutex<std::collections::HashMap<u32, ResolvedCallFigures>>,+ /// `call_id → start instant` for in-flight tool calls, written on+ /// `ToolStarted` and taken on `ToolCompleted` so the projected completion+ /// event carries a real `elapsed_ms` (the crate event has no timing).+ tool_started_at: Mutex<std::collections::HashMap<String, std::time::Instant>>,+ state: Mutex<BridgeState>,+ /// Ordered overflow buffer for progress events that hit backpressure+ /// (channel `Full`). Once ANY event spills here, `draining` stays set and+ /// every subsequent event queues here too — a single spawned forwarder+ /// drains them to the channel in FIFO order — so a later fast-path+ /// `try_send` can never jump ahead of an earlier spilled event and scramble+ /// start/completed ordering (which would leave a tool row stuck `running`+ /// when a `ToolCallCompleted` overtakes its `ToolCallStarted`) (#4466).+ overflow: Arc<Mutex<OverflowState>>,+}++/// Backpressure overflow state guarded by a single mutex so the "are we+/// draining?" decision and the queue mutation stay atomic together.+#[derive(Default)]+struct OverflowState {+ queue: std::collections::VecDeque<AgentProgress>,+ draining: bool,+}++impl OpenhumanEventBridge {+ /// Build a parent-scoped bridge for `model`.+ pub(crate) fn new(+ on_progress: Option<Sender<AgentProgress>>,+ model: impl Into<String>,+ max_iterations: usize,+ ) -> Arc<Self> {+ Self::with_scope(+ on_progress,+ model,+ "custom",+ max_iterations,+ None,+ Arc::default(),+ Arc::default(),+ Arc::default(),+ Arc::default(),+ )+ }++ /// Build a bridge, optionally child-scoped, sharing `cursor` (iteration+ /// attribution) and `tool_names` (tool-call name lookup for the streamed+ /// argument fragments) with the model adapter.+ pub(crate) fn with_scope(+ on_progress: Option<Sender<AgentProgress>>,+ model: impl Into<String>,+ provider_id: impl Into<String>,+ max_iterations: usize,+ scope: Option<SubagentScope>,+ cursor: IterationCursor,+ tool_names: ToolNameMap,+ failure_map: ToolFailureMap,+ usage_carry: ProviderUsageCarry,+ ) -> Arc<Self> {+ Arc::new(Self {+ on_progress,+ model: model.into(),+ provider_id: provider_id.into(),+ max_iterations: max_iterations as u32,+ scope,+ cursor,+ tool_names,+ failure_map,+ usage_carry,+ recorded_iterations: Mutex::new(std::collections::HashSet::new()),+ resolved_calls: Mutex::new(std::collections::HashMap::new()),+ tool_started_at: Mutex::new(std::collections::HashMap::new()),+ state: Mutex::new(BridgeState::default()),+ overflow: Arc::default(),+ })+ }++ /// Cumulative `(input_tokens, output_tokens, charged_usd)` observed so far.+ fn totals(&self) -> (u64, u64, f64) {+ let s = self.state.lock().unwrap();+ (s.input_tokens, s.output_tokens, s.charged_amount_usd)+ }++ /// Cumulative `(input_tokens, output_tokens, cached_input_tokens, charged_usd)`+ /// observed so far — the full accounting the turn persists (transcript cost /+ /// session meters), so a normal turn no longer records `$0` and zero cached+ /// tokens despite real usage.+ pub(crate) fn totals_with_cost(&self) -> (u64, u64, u64, f64) {+ let s = self.state.lock().unwrap();+ (+ s.input_tokens,+ s.output_tokens,+ s.cached_input_tokens,+ s.charged_amount_usd,+ )+ }++ /// Cumulative `(cache_hits, cache_misses)` observed so far (issue #4249,+ /// 03.2). Exposed so the turn loop can surface response-cache effectiveness;+ /// the cost-footer DTO wiring is a follow-up (workstream 06).+ pub(crate) fn cache_counts(&self) -> (u64, u64) {+ let s = self.state.lock().unwrap();+ (s.cache_hits, s.cache_misses)+ }++ /// Forward a progress event without ever silently dropping it under+ /// backpressure (#4466). The crate `EventListener::on_event` callback is+ /// **synchronous**, so we cannot `.await` a bounded `send()` inline the way+ /// the legacy streaming path did. Fast path: `try_send`, which succeeds (and+ /// stays fully synchronous + ordered) whenever the downstream channel has+ /// room — the common case. Only when the channel is momentarily **full** do+ /// we fall back to an awaited `send()` on a spawned task so the delta is+ /// delivered under backpressure instead of being dropped (the old bug). A+ /// `Closed` channel means the receiver is gone (turn tore down), where+ /// dropping is correct.+ fn send(&self, progress: AgentProgress) {+ use tokio::sync::mpsc::error::TrySendError;+ let Some(tx) = &self.on_progress else {+ return;+ };+ // Hold the overflow lock across the ordering decision so "are we+ // draining?" and the queue mutation are atomic (try_send is+ // non-blocking, so holding a std mutex across it is fine).+ let mut ov = self.overflow.lock().unwrap_or_else(|p| p.into_inner());+ if ov.draining {+ // Already spilling: queue in order; the single forwarder delivers it.+ ov.queue.push_back(progress);+ return;+ }+ match tx.try_send(progress) {+ Ok(()) => {}+ Err(TrySendError::Closed(_)) => {}+ Err(TrySendError::Full(progress)) => {+ // Backpressure, not capacity loss. Enter ordered-drain mode:+ // queue this event and spawn ONE forwarder that awaits `send()`+ // per event in FIFO order. `draining` stays set (so every later+ // event also queues here) until the buffer fully drains — that is+ // what stops a later `try_send` from overtaking a spilled earlier+ // event and scrambling start/completed ordering.+ if let Ok(handle) = tokio::runtime::Handle::try_current() {+ ov.queue.push_back(progress);+ ov.draining = true;+ let overflow = Arc::clone(&self.overflow);+ let tx = tx.clone();+ drop(ov);+ handle.spawn(async move {+ loop {+ let next = {+ let mut ov = overflow.lock().unwrap_or_else(|p| p.into_inner());+ match ov.queue.pop_front() {+ Some(item) => item,+ None => {+ ov.draining = false;+ break;+ }+ }+ };+ if tx.send(next).await.is_err() {+ // Receiver gone: stop draining, discard the rest.+ let mut ov = overflow.lock().unwrap_or_else(|p| p.into_inner());+ ov.queue.clear();+ ov.draining = false;+ break;+ }+ }+ });+ } else {+ tracing::debug!(+ model = %self.model,+ "[tinyagents] progress channel full and no runtime to defer send; dropping one delta"+ );+ }+ }+ }+ }++ fn iteration(&self) -> u32 {+ self.cursor.load(Ordering::SeqCst)+ }++ /// Accumulate a usage block, feed the global cost tracker, and emit a+ /// `TurnCostUpdated` so the UI footer stays live.+ fn record_usage(&self, usage: &Usage) {+ let iteration = self.iteration();+ // Dedupe guard (W2-budget-dedupe): record a given model call's usage into+ // the global cost tracker **exactly once**. Installing the observe-only+ // crate `BudgetMiddleware` makes each model call emit two `UsageRecorded`+ // events (the runtime's own at `agent_loop` + the middleware's+ // `after_model` re-emit), both reaching this listener with identical+ // usage. The two events have *distinct* stable ids, so an event-id key+ // would not collapse them — instead we key on the run-scoped model-call+ // identity: the iteration cursor, bumped once per `ModelStarted`. This+ // bridge instance is per-run (parent or child scope), so the set is+ // naturally (run, turn)-scoped. First writer for an iteration records;+ // any later `UsageRecorded` for the same iteration is a duplicate.+ {+ let mut seen = self+ .recorded_iterations+ .lock()+ .unwrap_or_else(|p| p.into_inner());+ if !seen.insert(iteration) {+ tracing::debug!(+ iteration,+ model = %self.model,+ child = self.scope.is_some(),+ "[budget] duplicate UsageRecorded for model call — skipping double record"+ );+ return;+ }+ }+ // Drain the provider-usage side-channel the model adapter fed for this+ // model call (FIFO, one push per provider response). The crate `Usage`+ // the harness surfaces carries only token counts, so the backend-charged+ // USD, the model's context window, and the cache-creation/reasoning+ // breakdown ride this out-of-band carry instead (#4467, item 1). Popped+ // AFTER the dedupe guard above so the duplicate `UsageRecorded` re-emit+ // (crate `BudgetMiddleware`) does not consume a second entry.+ let carried = self+ .usage_carry+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .pop_front();++ // Estimate as the floor via the tier-aware `agent::cost` table (managed+ // handles like `chat-v1`/`burst-v1` + the vendor catalog + heuristics —+ // the catalog-only lookup priced every managed-tier call as $0); prefer+ // the provider's own charged amount when it reported one (charged >+ // estimate precedence, so credit-metered backends surface real billing+ // rather than a token-rate estimate).+ let estimate = Self::estimate_call_cost(&self.model, usage);+ let call_cost = carried+ .as_ref()+ .map(|u| u.charged_amount_usd)+ .filter(|c| c.is_finite() && *c > 0.0)+ .unwrap_or(estimate);+ // The context window + cache-creation/reasoning breakdown only exist on+ // the carried provider usage (the crate `Usage` mapping drops them); fall+ // back to the catalogue window and the crate token counts when absent.+ let context_window = carried+ .as_ref()+ .map(|u| u.context_window)+ .filter(|w| *w > 0)+ .unwrap_or_else(|| {+ crate::platform::cost::catalog::lookup(&self.model)+ .map(|p| u64::from(p.context_window))+ .unwrap_or(0)+ });+ let cache_creation_tokens = carried+ .as_ref()+ .map(|u| u.cache_creation_tokens)+ .filter(|t| *t > 0)+ .unwrap_or(usage.cache_creation_tokens);+ let reasoning_tokens = carried+ .as_ref()+ .map(|u| u.reasoning_tokens)+ .filter(|t| *t > 0)+ .unwrap_or(usage.reasoning_tokens);+ tracing::trace!(+ model = %self.model,+ iteration,+ charged_from_provider = carried+ .as_ref()+ .map(|u| u.charged_amount_usd > 0.0)+ .unwrap_or(false),+ call_cost,+ context_window,+ "[cost] recording per-call usage (charged>estimate precedence via provider carry)"+ );+ // Stash the resolved per-call figures so the `ModelCompleted` arm (which+ // fires right after this event and emits the `ModelCallCompleted`+ // generation telemetry) reports the SAME cost/cache/reasoning numbers as+ // the wallet accounting, instead of re-deriving a bare estimate.+ self.resolved_calls+ .lock()+ .unwrap_or_else(|p| p.into_inner())+ .insert(+ iteration,+ ResolvedCallFigures {+ cost_usd: call_cost,+ cache_creation_tokens,+ reasoning_tokens,+ },+ );+ let (input, output, cached, charged) = {+ let mut s = self.state.lock().unwrap();+ s.input_tokens += usage.input_tokens;+ s.output_tokens += usage.output_tokens;+ s.cached_input_tokens += usage.cache_read_tokens;+ s.charged_amount_usd += call_cost;+ (+ s.input_tokens,+ s.output_tokens,+ s.cached_input_tokens,+ s.charged_amount_usd,+ )+ };++ // Feed the authoritative global cost tracker (same call the legacy+ // observer made), so the wallet/cost surfaces stay accurate.+ let usage_info = UsageInfo {+ input_tokens: usage.input_tokens,+ output_tokens: usage.output_tokens,+ context_window,+ cached_input_tokens: usage.cache_read_tokens,+ cache_creation_tokens,+ reasoning_tokens,+ charged_amount_usd: call_cost,+ };+ if reasoning_tokens > 0 || cache_creation_tokens > 0 {+ log::debug!(+ "[cost] recording reasoning/cache-creation tokens model={} reasoning_tokens={} cache_creation_tokens={}",+ self.model,+ reasoning_tokens,+ cache_creation_tokens+ );+ }+ crate::platform::cost::record_provider_usage(&self.model, &usage_info);++ // The cost footer is a top-level surface; for a child run the global+ // cost tracker feed above is the authoritative accounting and the parent+ // emits its own footer, so suppress the per-child `TurnCostUpdated`.+ // Per-call generation telemetry (`ModelCallCompleted`) is emitted from+ // the `AgentEvent::ModelCompleted` arm instead — that event fires after+ // `UsageRecorded` and is the only one carrying the captured request+ // messages + completion, so the generation gets usage AND content in+ // one shot (for parent and child scopes alike).+ if self.scope.is_none() {+ self.send(AgentProgress::TurnCostUpdated {+ model: self.model.clone(),+ iteration,+ input_tokens: input,+ output_tokens: output,+ cached_input_tokens: cached,+ total_usd: charged,+ });+ }+ }++ /// Estimate one call's USD cost. Uses the tier-aware+ /// [`agent::cost`](crate::agent::cost) table (managed handles+ /// like `chat-v1`/`burst-v1` + the vendor catalog + heuristics) — the+ /// previous `cost::catalog::estimate_cost_usd` only knew concrete vendor+ /// ids, so every managed-tier call priced as $0 in traces and the footer.+ fn estimate_call_cost(model: &str, usage: &Usage) -> f64 {+ crate::agent::cost::estimate_call_cost_usd(+ model,+ &UsageInfo {+ input_tokens: usage.input_tokens,+ output_tokens: usage.output_tokens,+ context_window: 0,+ cached_input_tokens: usage.cache_read_tokens,+ cache_creation_tokens: usage.cache_creation_tokens,+ reasoning_tokens: usage.reasoning_tokens,+ charged_amount_usd: 0.0,+ },+ )+ }+}++impl EventListener for OpenhumanEventBridge {+ fn on_event(&self, record: &EventRecord) {+ match &record.event {+ AgentEvent::ModelStarted { .. } => {+ let iteration = self.cursor.fetch_add(1, Ordering::SeqCst) + 1;+ match &self.scope {+ None => self.send(AgentProgress::IterationStarted {+ iteration,+ max_iterations: self.max_iterations,+ }),+ Some(s) => self.send(AgentProgress::SubagentIterationStarted {+ agent_id: s.agent_id.clone(),+ task_id: s.task_id.clone(),+ iteration,+ max_iterations: self.max_iterations,+ extended_policy: s.extended_policy,+ }),+ }+ }+ AgentEvent::ModelDelta { delta, .. } => {+ let iteration = self.iteration();+ if !delta.text.is_empty() {+ match &self.scope {+ None => self.send(AgentProgress::TextDelta {+ delta: delta.text.clone(),+ iteration,+ }),+ Some(s) => self.send(AgentProgress::SubagentTextDelta {+ agent_id: s.agent_id.clone(),+ task_id: s.task_id.clone(),+ delta: delta.text.clone(),+ iteration,+ }),+ }+ }+ if !delta.reasoning.is_empty() {+ match &self.scope {+ None => self.send(AgentProgress::ThinkingDelta {+ delta: delta.reasoning.clone(),+ iteration,+ }),+ Some(s) => self.send(AgentProgress::SubagentThinkingDelta {+ agent_id: s.agent_id.clone(),+ task_id: s.task_id.clone(),+ delta: delta.reasoning.clone(),+ iteration,+ }),+ }+ }+ // Tool-call **start** + **argument** fragments both ride the crate+ // stream (`MessageDelta.tool_call`) now — the out-of-band+ // `ThinkingForwarder` is gone. The call-opening delta carries the+ // tool name (crate `ToolDelta::tool_name`, G2) with empty content;+ // argument fragments carry content with no name. We record the+ // name on the opening delta so subsequent fragments can be+ // labelled, and project both onto the `ToolCallArgsDelta` the UI+ // timeline consumes so the model can be shown composing the call+ // before it executes.+ if let Some(tool_call) = &delta.tool_call {+ // Record the tool name as soon as the call opens (matching the+ // legacy forwarder's `note_tool_call`), and emit the start+ // marker — an empty-delta `ToolCallArgsDelta` — top-level+ // regardless of scope, exactly as the forwarder did.+ if let Some(name) = tool_call.tool_name.as_deref().filter(|n| !n.is_empty()) {+ self.tool_names+ .lock()+ .unwrap()+ .insert(tool_call.call_id.clone(), name.to_string());+ if tool_call.content.is_empty() {+ self.send(AgentProgress::ToolCallArgsDelta {+ call_id: tool_call.call_id.clone(),+ tool_name: name.to_string(),+ delta: String::new(),+ iteration,+ });+ }+ }+ // Argument fragments are parent-only: there is no `Subagent*`+ // tool-arg variant, and an UNSCOPED top-level `ToolCallArgsDelta`+ // emitted from a child run would render the child's argument+ // composition as the *parent's* own timeline activity (#4467,+ // item 6; v0.58.7 dropped child arg fragments). A child run's+ // Started/Completed rows already carry the final arguments+ // under the `Subagent*` scope.+ if self.scope.is_none() && !tool_call.content.is_empty() {+ let tool_name = tool_call+ .tool_name+ .as_deref()+ .filter(|n| !n.is_empty())+ .map(str::to_string)+ .unwrap_or_else(|| {+ self.tool_names+ .lock()+ .unwrap()+ .get(&tool_call.call_id)+ .cloned()+ .unwrap_or_default()+ });+ tracing::trace!(+ call_id = tool_call.call_id.as_str(),+ tool_name = tool_name.as_str(),+ len = tool_call.content.len(),+ "[stream] projecting crate tool-arg fragment onto ToolCallArgsDelta"+ );+ self.send(AgentProgress::ToolCallArgsDelta {+ call_id: tool_call.call_id.clone(),+ tool_name,+ delta: tool_call.content.clone(),+ iteration,+ });+ }+ }+ }+ // `UsageRecorded` carries the authoritative per-call usage and fires+ // exactly once per model call; prefer it over `ModelCompleted`'s+ // optional usage to avoid double counting.+ AgentEvent::UsageRecorded { usage } => self.record_usage(usage),+ // Per-call generation telemetry. `ModelCompleted` fires exactly once+ // per model call, after `UsageRecorded`, and is the only event+ // carrying the captured request messages (incl. the system prompt)+ // + completion (`RunPolicy.capture.model_io`, enabled in+ // `run_policy_for`). Emitted for parent AND child scopes — the+ // 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 = resolved+ .as_ref()+ .map(|r| r.cost_usd)+ .unwrap_or_else(|| 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={:.6} 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::CostRecorded { cost } => {+ tracing::debug!(+ cost = ?cost,+ "[tinyagents] cost event observed without OpenHuman accounting side effect"+ );+ }+ AgentEvent::BudgetReserved {+ estimated_input_tokens,+ } => {+ tracing::debug!(+ estimated_input_tokens,+ "[tinyagents] budget reserved estimated input tokens"+ );+ }+ AgentEvent::BudgetReconciled {+ estimated_input_tokens,+ actual_input_tokens,+ } => {+ tracing::debug!(+ estimated_input_tokens,+ actual_input_tokens,+ "[tinyagents] budget reservation reconciled"+ );+ }+ AgentEvent::BudgetWarning { reason } => {+ tracing::debug!(+ reason,+ "[tinyagents] budget warning observed without run interruption"+ );+ }+ AgentEvent::BudgetExceeded { reason, blocked } => {+ tracing::debug!(+ reason,+ blocked,+ "[tinyagents] budget exceeded event observed"+ );+ }+ AgentEvent::Steered {+ command_kind,+ accepted,+ } => {+ // Grep-friendly `[steering]` projection of every drained steering+ // command (issue #4249, 07.3). A rejected command means the run's+ // `SteeringPolicy` refused the kind and the crate is aborting the+ // run with `TinyAgentsError::Steering`, so surface it louder. The+ // bespoke ack plumbing in `harness/run_queue/` stays live (gated:+ // web-channel followup/parallel still need a local owner); UI+ // projection of this event remains pending.+ if *accepted {+ tracing::debug!(+ command_kind = command_kind.as_str(),+ accepted,+ "[steering] command applied at safe boundary"+ );+ } else {+ tracing::warn!(+ command_kind = command_kind.as_str(),+ accepted,+ "[steering] command rejected by run policy"+ );+ }+ }+ AgentEvent::ToolsFiltered {+ by,+ excluded,+ remaining,+ } => {+ tracing::debug!(+ policy = by.as_str(),+ excluded_tools = ?excluded,+ remaining,+ "[tinyagents] model-visible tools filtered"+ );+ }+ AgentEvent::Compressed {+ from_tokens,+ to_tokens,+ } => {+ tracing::debug!(+ from_tokens,+ to_tokens,+ saved_tokens = from_tokens.saturating_sub(*to_tokens),+ "[tinyagents] context compressed before model call"+ );+ }+ AgentEvent::UnknownToolCall {+ call_id,+ requested_name,+ arguments,+ recovery,+ } => {+ tracing::debug!(+ call_id = call_id.as_str(),+ requested_tool = requested_name.as_str(),+ recovery = recovery.as_str(),+ arguments = %arguments,+ "[tinyagents] recovered unknown tool call without executing a tool"+ );+ // #4118: surface the *attempted* unavailable tool on the timeline+ // as a failed call so the UI shows what the agent tried (and+ // recovered from) rather than silently dropping it — the crate+ // recovers the call without ever emitting Started/Completed for it,+ // so nothing else in this bridge projects it. Two rows (start ++ // failed-complete) keyed by the same call_id, mirroring a real+ // tool call. Classified `Unknown` (recoverable) — the model got the+ // "valid tools: [...]" corrective and can retry a real tool.+ let iteration = self.iteration();+ let failure = Some(crate::tools::status::describe(+ crate::tools::status::ToolFailureClass::Unknown,+ ));+ let label = format!("{} (unavailable)", humanize_tool_name(requested_name));+ match &self.scope {+ None => {+ self.send(AgentProgress::ToolCallStarted {+ call_id: call_id.as_str().to_string(),+ tool_name: requested_name.clone(),+ arguments: arguments.clone(),+ iteration,+ display_label: Some(label),+ display_detail: Some("tool not available".to_string()),+ });+ self.send(AgentProgress::ToolCallCompleted {+ call_id: call_id.as_str().to_string(),+ tool_name: requested_name.clone(),+ success: false,+ output_chars: 0,+ output: String::new(),+ arguments: Some(arguments.clone()),+ elapsed_ms: 0,+ iteration,+ failure,+ });+ }+ 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: requested_name.clone(),+ arguments: arguments.clone(),+ iteration,+ display_label: Some(label),+ display_detail: Some("tool not available".to_string()),+ });+ 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: requested_name.clone(),+ success: false,+ output_chars: 0,+ output: String::new(),+ arguments: Some(arguments.clone()),+ elapsed_ms: 0,+ iteration,+ failure,+ });+ }+ }+ }+ AgentEvent::ToolStarted { call_id, tool_name } => {+ // Unknown/invisible tool calls no longer produce a sentinel-named+ // Started event: the migration replaced `UNKNOWN_TOOL_SENTINEL` ++ // `UnknownToolRewriteMiddleware` with the crate+ // `UnknownToolPolicy::ReturnToolError` path (01.2), which recovers+ // the call and emits `AgentEvent::UnknownToolCall` (handled above)+ // instead of a rewritten ToolStarted. So this arm fires only for+ // real, model-visible tools and needs no sentinel guard.+ 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());+ match &self.scope {+ None => self.send(AgentProgress::ToolCallStarted {+ call_id: call_id.as_str().to_string(),+ tool_name: tool_name.clone(),+ arguments: serde_json::Value::Null,+ iteration,+ display_label: Some(humanize_tool_name(tool_name)),+ display_detail: None,+ }),+ 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: serde_json::Value::Null,+ iteration,+ display_label: Some(humanize_tool_name(tool_name)),+ display_detail: None,+ }),+ }+ }+ AgentEvent::ToolCompleted {+ call_id,+ tool_name,+ input,+ output,+ // `started_at_ms`/`duration_ms`/`output_bytes`/`error` now ride+ // the crate event (tinyagents 1.7 / tinyagents#18). The bridge+ // still reads its richer side channels below to preserve current+ // success/duration/size behavior; adopting crate fields directly+ // is C4 slice S1.+ ..+ } => {+ let iteration = self.iteration();+ // The crate event carries no success/error, so read what the+ // outcome-capture middleware classified for this call. Absent →+ // the event was projected before the middleware ran; assume+ // success (never worse than the previous hardcoded `true`).+ let outcome = self+ .failure_map+ .lock()+ .ok()+ .and_then(|mut m| m.remove(call_id.as_str()));+ let success = outcome.as_ref().map(|(ok, ..)| *ok).unwrap_or(true);+ // 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(),+ };+ let output_chars = outcome+ .as_ref()+ .map(|(_, _, _, c)| *c)+ .filter(|c| *c > 0)+ .unwrap_or_else(|| output_text.chars().count());+ // 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.+ let failure = outcome.and_then(|(_, f, _, _)| f);+ 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,+ }),+ 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,+ }),+ }+ }+ // 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 —+ // interactive chat never attaches a cache). Counters are additive; the+ // cost-footer DTO wiring is a follow-up (workstream 06).+ AgentEvent::CacheHit { call_id, key } => {+ {+ let mut s = self.state.lock().unwrap();+ s.cache_hits += 1;+ }+ tracing::debug!(+ model = %self.model,+ call_id = call_id.as_str(),+ key = key.as_str(),+ "[cache] response-cache hit — provider call skipped"+ );+ }+ AgentEvent::CacheMiss { call_id, key } => {+ {+ let mut s = self.state.lock().unwrap();+ s.cache_misses += 1;+ }+ tracing::debug!(+ model = %self.model,+ call_id = call_id.as_str(),+ key = key.as_str(),+ "[cache] response-cache miss — invoking provider and storing result"+ );+ }+ // Retry/fallback parity (issue #4249, Workstream 02.2). These surface the+ // SDK-owned reliability decisions on the observability bridge so they are+ // no longer silently dropped by the catch-all below. `RetryScheduled` is+ // emitted by the crate's model-retry loop; with the retry pin at a single+ // attempt (`RunPolicy.retry.max_attempts = 1`, pending `ReliableProvider`+ // removal) it will not fire on the live path yet, but the bridge is wired+ // for when it does. `FallbackSelected` is emitted by+ // [`FallbackObserverMiddleware`](super::routes::FallbackObserverMiddleware)+ // whenever the harness fails over to a sibling workload-tier route.+ AgentEvent::RetryScheduled { call_id, attempt } => {+ tracing::info!(+ model = %self.model,+ call_id = call_id.as_str(),+ attempt,+ "[models] SDK scheduled a model-call retry after a retryable provider error"+ );+ }+ AgentEvent::FallbackSelected { from, to } => {+ tracing::info!(+ model = %self.model,+ from = from.as_str(),+ to = to.as_str(),+ "[fallback] SDK failed over to a cross-route fallback model"+ );+ }+ other => {+ // Not projected into `AgentProgress` (run lifecycle, sub-agent+ // boundaries — reconstructed from the orchestration tools'+ // manual emits — middleware, workspace, memory, limits). Trace+ // the kind so a dropped-event hypothesis is checkable from+ // logs instead of reading this match.+ tracing::trace!(+ model = %self.model,+ kind = ?std::mem::discriminant(other),+ "[tinyagents:bridge] event observed but not forwarded to UI progress"+ );+ }+ }+ }+}++/// Surface the crate `PromptCacheGuardMiddleware`'s recorded+/// [`CacheLayoutEvent`]s as structured `[cache]` warnings (issue #4249, 03.2).+///+/// The guard records a layout event whenever the cacheable prompt prefix changes+/// between turns (volatile content — a timestamp, uuid, injected memory, etc. —+/// silently busting the provider KV-cache prefix). This is the crate-native+/// replacement for the deleted `CacheAlignMiddleware` free-text warn-log:+/// instead of a token-pattern heuristic it reports the exact before/after+/// cacheable segment ids. Drained by the turn loop after the run and logged+/// here. The warn-only `CacheAlignMiddleware` shadow was deleted in C3; this+/// guard is now the sole owner of KV-cache-prefix drift detection.+pub(crate) fn surface_cache_layout_events(model: &str, events: &[CacheLayoutEvent]) {+ for event in events {+ tracing::warn!(+ model,+ changed_prefix = event.changed_prefix,+ volatile_only = event.volatile_only,+ segments_before = ?event.segment_ids_before,+ segments_after = ?event.segment_ids_after,+ "[cache] prompt-cache prefix changed across turns — KV-cache prefix may not hit; keep dynamic content out of the system prompt / stable tool set"+ );+ }+}