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(agent): remove unused imports in three modules
Steven Enamakel75de33c5a92f
diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rsnew file mode 100644index 0000000000..846f71be96--- /dev/null+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs@@ -0,0 +1,723 @@+//! [`assemble_turn_harness`] — register the provider model, every shared+//! tool, and the full middleware stack in the intended order for one turn.++use std::collections::HashSet;+use std::sync::Arc;++use tinyagents_harness::cache::InMemoryResponseCache;+use tinyagents_harness::middleware::{+ BudgetLimits, BudgetMiddleware, ContextCompressionMiddleware, PromptCacheGuardMiddleware,+ ToolPolicyMiddleware as TaToolPolicyMiddleware,+};+use tinyagents_harness::runtime::AgentHarness;+use tinyagents_harness::steering::SteeringHandle;+use tinyagents_harness::workspace::WorkspaceDescriptor;+use tinyagents_registry::{CapabilityRegistry, RegistryDiagnostic, RegistrySnapshot};+use tinyinference::model::CapabilitySet;+use tokio::sync::mpsc::Sender;++use crate::agent::harness::tool_result_artifacts::ToolResultArtifactIndexStore;+use crate::agent::progress::AgentProgress;+use crate::agent::tinyagents::harness_tool_registration::register_turn_tools_and_agents;+use crate::agent::tinyagents::middleware::{self, TurnContextMiddleware};+use crate::agent::tinyagents::observability::{+ IterationCursor, ProviderUsageCarry, SubagentScope, ToolFailureMap, ToolNameMap,+};+use crate::agent::tinyagents::orchestration;+use crate::agent::tinyagents::routes;+use crate::agent::tinyagents::stop_hooks;+use crate::agent::tinyagents::summarize;+use crate::agent::tinyagents::tools::{EarlyExitHook, SharedToolAdapter as _};+use crate::agent::tinyagents::turn_models::TurnModels;+use crate::agent::tinyagents::turn_outcome::{HaltSummarySlot, ToolOutcomeSink};+use crate::agent::tinyagents::turn_policy::{run_policy_for, REPEATED_TOOL_FAILURE_THRESHOLD};++use super::ToolPolicyEnforcement;++/// Everything [`assemble_turn_harness`] wires up for one turn: the configured+/// harness plus the shared slots/handles the run loop reads after the drive+/// future returns.+pub(super) struct AssembledTurnHarness {+ /// The fully assembled harness: model, tools, and middleware registered in+ /// the intended order.+ pub(super) harness: AgentHarness<()>,+ /// Shared 1-based model-call cursor (event bridge advances, model adapter+ /// reads for out-of-band thinking attribution).+ pub(super) cursor: IterationCursor,+ /// Shared `call_id → tool_name` map: the model adapter's `ThinkingForwarder`+ /// writes it on tool-call start; the event bridge reads it to label the+ /// tool-argument fragments it now projects off the crate stream.+ pub(super) tool_names: ToolNameMap,+ /// Shared `call_id → (success, failure, elapsed_ms, output_chars)`+ /// side-channel: the tool-outcome capture middleware classifies each outcome+ /// + records its duration/output size; the event bridge reads it to project+ ///+ /// Real success + a user-facing failure + timing onto `ToolCallCompleted`.+ pub(super) failure_map: ToolFailureMap,+ /// Shared FIFO carry of per-call provider `UsageInfo` (charged USD + context+ /// window): the model adapter pushes, the event bridge pops when recording+ /// usage — restores charged-USD precedence on the tinyagents path (#4467).+ pub(super) provider_usage_carry: ProviderUsageCarry,+ /// Recovers the original (downcastable) provider error on run failure.+ pub(super) error_slot: crate::agent::tinyagents::model::ModelErrorSlot,+ /// Root-cause summary recorded by the repeated-tool-failure breaker.+ pub(super) halt_summary: HaltSummarySlot,+ /// Per-call tool success/content capture for honest `ToolCallRecord`s.+ pub(super) tool_outcome_sink: ToolOutcomeSink,+ /// The shared steering handle (mid-flight steer, early-exit, cap, stop-hook+ /// pauses).+ pub(super) handle: Option<SteeringHandle>,+ /// Records the first early-exit tool round, when early-exit tools exist.+ pub(super) early_exit_hook: Option<EarlyExitHook>,+ /// Set by [`FinalCallWrapUpMiddleware`] when it turned the last permitted+ /// model call into the turn's conclusion (issue #6014). `None` when the+ /// middleware is not installed (a run that does not pause at its cap).+ ///+ /// A flag rather than an inference off the run, because this turn now ends+ /// the way a finished one does — the model returns text and requests no+ /// tools — so `final_response.is_none()` no longer tells the two apart.+ pub(super) wrap_up_fired: Option<Arc<std::sync::atomic::AtomicBool>>,+ /// Number of callable tools registered.+ pub(super) tool_count: usize,+ /// TinyAgents named-capability projection for this turn. The live run still+ /// uses the harness registries above; this snapshot makes the projected+ /// model/tool/graph inventory inspectable without changing dispatch.+ pub(super) registry_snapshot: RegistrySnapshot,+ /// Health diagnostics from the projected registry.+ pub(super) registry_diagnostics: Vec<RegistryDiagnostic>,+ /// TinyAgents store index for OpenHuman action-dir tool-result artifacts.+ pub(super) tool_result_artifact_index: Option<Arc<ToolResultArtifactIndexStore>>,+ /// Concrete handle to the installed [`ContextCompressionMiddleware`], when the+ /// summarization step is active. Drained after the run to surface each+ /// compaction's [`CompressionProvenance`][tinyagents_harness::summarization::CompressionProvenance]+ /// (source ids + before/after token estimates) via the observability path.+ pub(super) compression_mw: Option<Arc<ContextCompressionMiddleware>>,+ /// Crate prompt-cache guard (issue #4249, 03.2). Records a `CacheLayoutEvent`+ /// whenever the cacheable prompt prefix (system prompt + tool set) changes+ /// across model calls. Drained after the run and surfaced via+ /// [`observability::surface_cache_layout_events`](crate::agent::tinyagents::observability::surface_cache_layout_events) —+ /// the crate-native replacement for the deleted `CacheAlignMiddleware`+ /// warn-log (C3).+ pub(super) prompt_cache_guard: Arc<PromptCacheGuardMiddleware>,+}++/// Assemble the turn harness for [`run_turn_via_tinyagents_shared`](super::run_turn_via_tinyagents_shared):+/// register the provider model, every shared tool, and the full middleware+/// stack in the intended order. Split out of the runner so the adapter+/// inventory is directly testable (issue #4249, Phase 11) — the returned+/// [`AssembledTurnHarness`] exposes the harness registries without driving a+/// run.+#[allow(clippy::too_many_arguments)]+pub(super) fn assemble_turn_harness(+ turn_models: TurnModels,+ model: &str,+ tool_sets: Vec<Arc<Vec<Box<dyn crate::tools::Tool>>>>,+ allowed: Option<HashSet<String>>,+ max_iterations: usize,+ _on_progress: Option<Sender<AgentProgress>>,+ subagent_scope: Option<SubagentScope>,+ context_window: Option<u64>,+ early_exit_tools: &[&str],+ context_mw: TurnContextMiddleware,+ tool_policy: Option<ToolPolicyEnforcement>,+ required_capabilities: Option<CapabilitySet>,+ deterministic_cacheable: bool,+ // Whether this run pauses gracefully at its model-call cap (issue #6014).+ // Only such a run gets the in-loop conclusion: a run that errors at its cap+ // instead (the channel/CLI path, which maps the stop to+ // `MaxIterationsExceeded`) must keep doing that, and handing it a wrap-up+ // would silently convert a documented error into an answer.+ pause_at_cap: bool,+) -> AssembledTurnHarness {+ let mut harness: AgentHarness<()> = AgentHarness::new();+ // Cross-route fallback ownership (issue #4249, Workstream 02.2): populate the+ // SDK `RunPolicy.fallback` with the ordered same-family route chain for this+ // turn's primary model so the harness fails over to a sibling workload tier+ // (e.g. chat-v1 → burst-v1) when the primary route errors. Retry stays pinned+ // to a single attempt (see `run_policy_for`) — fallback and retry are+ // independent knobs, and only fallback is enabled here because `ReliableProvider`+ // (still wrapped) does not fail over across the registered tier routes.+ let mut policy = run_policy_for(max_iterations, deterministic_cacheable);+ let route_fallback = routes::route_fallback_policy(model);+ policy.fallback = route_fallback.clone();+ tracing::debug!(+ model,+ fallback_chain = ?route_fallback.as_ref().map(|f| &f.models),+ "[models] assembling turn harness with SDK retry/fallback policy"+ );+ harness.with_policy(policy);+ // Deterministic internal runs (summarizer/triage/memory-scoring style) may+ // reuse a prior identical model response; attach an in-memory response cache+ // so the agent loop can short-circuit a recurring provider call and emit+ // `CacheHit`/`CacheMiss` (issue #4249, 03.2). NEVER attached for interactive+ // chat turns — a live user turn must never be served a cached response.+ if deterministic_cacheable {+ harness.with_response_cache(Arc::new(InMemoryResponseCache::new()));+ tracing::debug!(+ model,+ "[cache] response cache attached (deterministic internal run)"+ );+ }+ let mut capability_registry: CapabilityRegistry<()> = CapabilityRegistry::new();++ let cursor: IterationCursor = Arc::default();+ // Shared `call_id → tool_name` map: the forwarder records the name on+ // tool-call start (the crate `ToolDelta` carries none), the bridge reads it+ // to label the argument fragments now streamed via `MessageDelta.tool_call`.+ let tool_names: ToolNameMap = Arc::default();+ // Shared FIFO carry of per-call provider `UsageInfo`: `UsageCarryMiddleware`+ // pushes each response's usage (charged USD + context window ++ // cache-creation/reasoning tokens, read off the response via G1), the event+ // bridge pops it when recording that call's usage (#4467, item 1). The carry+ // is produced by a wrap-model middleware now, not the adapter, so route models+ // carry no usage side-channel (Phase 5).+ let provider_usage_carry: ProviderUsageCarry = Arc::default();+ // The turn's models are pre-built by `build_turn_models` (the single+ // `native model adapter` construction site) and handed in as crate `ChatModel`s —+ // the assembly no longer touches the raw provider (issue #4249, Phase 5).+ let TurnModels {+ primary,+ routes,+ summarizer: summarizer_model,+ error_slot,+ // Provider metadata (id/context-window/caps) is consumed by the turn-path+ // caller before dispatch, not by harness assembly.+ ..+ } = turn_models;+ capability_registry.replace_model(model, primary.clone());+ harness+ .register_model(model, primary)+ .set_default_model(model);++ // Project the full workload-route set into the registry (issue #4249,+ // Workstream 02.1). Each route is an additive registry entry carrying its+ // per-route capability profile; `set_default_model` above keeps the turn's+ // effective model as the dispatch target, so behavior is preserved until+ // fallback/selection (02.2) chooses among the routes. `build_turn_models`+ // already skipped the turn's own model, so we don't shadow the default.+ for (name, route_model) in routes {+ capability_registry.replace_model(name.as_str(), route_model.clone());+ harness.register_model(name, route_model);+ }++ // Cost usage capture (issue #4249, Phase 5): feed the event bridge's usage+ // carry from a wrap-model middleware that reads the full `UsageInfo` off each+ // response, instead of every `native model adapter` pushing it. Installed+ // unconditionally — usage flows on every turn — and shares the same carry the+ // bridge drains on `UsageRecorded`.+ harness.push_model_middleware(Arc::new(routes::UsageCarryMiddleware::new(+ provider_usage_carry.clone(),+ )));++ // Per-call capability gate (issue #4249, Workstream 02.1): when the turn has+ // derivable capability needs (today: vision for a `vision-v1` turn), stamp+ // them onto every `ModelRequest` via `with_required_capabilities` so an unfit+ // model is rejected pre-dispatch (and, once 02.2 lands, a capable fallback is+ // selected) instead of failing at the provider.+ if let Some(required) = required_capabilities {+ harness.push_model_middleware(Arc::new(routes::RequiredCapabilitiesMiddleware::new(+ required,+ )));+ }++ // Fallback event parity (issue #4249, Workstream 02.2): the crate's+ // registry-backed `RunPolicy.fallback` traversal (wired above) performs the+ // cross-route swap silently — it emits no `AgentEvent::FallbackSelected`. Wrap+ // the model-resolving core with an observer that surfaces the parity event+ // whenever the resolved model differs from the primary, so a fallback is+ // visible on the OpenHuman progress/observability bridge (and grep-logged under+ // `[fallback]`). Installed only when a fallback chain exists; it never re-issues+ // the call, so it adds no provider dispatch (no double-fallback).+ if route_fallback.is_some() {+ harness.push_model_middleware(Arc::new(routes::FallbackObserverMiddleware::new(model)));+ }++ // Capture context settings before `install` consumes `context_mw`.+ let autocompact_enabled = context_mw.autocompact_enabled;+ // Captured for the same reason `autocompact_enabled` is — `install` consumes+ // `context_mw` — and used to site microcompact below, after compression.+ let microcompact_keep_recent = context_mw.microcompact_keep_recent;+ let tool_result_artifact_index = context_mw+ .artifact_store+ .as_ref()+ .map(|_| Arc::new(ToolResultArtifactIndexStore::new()));++ // Snapshot the installed stop hooks while the `CURRENT_STOP_HOOKS`+ // task-local is in scope (the harness drive future runs inline on this+ // task, but capturing here keeps the wiring robust). When present they fire+ // via `StopHookMiddleware` and pause through the shared steering handle.+ let stop_hooks_installed = crate::agent::stop_hooks::current_stop_hooks();++ // A single steering handle drives mid-flight steering (run queue), the+ // early-exit pause, the model-call-cap pause, and stop-hook pauses, so they+ // all reach the same loop. Created when any of them is active.+ // A steering handle is always created now: besides run-queue steering, the+ // early-exit / cap / stop-hook pauses, the repeated-tool-failure breaker+ // (below) also pauses through it, and it wants to fire on every path+ // (including plain channel turns that set none of the other flags). An idle+ // handle is a no-op — the loop just drains an empty steering channel.+ // Tighten the steering allowlist by run class: a live interactive chat turn+ // keeps the InjectMessage/Pause allowlist exactly, while a detached+ // sub-agent run (identified by its `subagent_scope`) additionally accepts+ // graceful control-flow steering (Resume/Cancel/Redirect). `subagent_scope`+ // is the only run-class signal available at this steer site.+ let steering_run_class = if subagent_scope.is_some() {+ orchestration::SteeringRunClass::Background+ } else {+ orchestration::SteeringRunClass::Interactive+ };+ let handle = Some(orchestration::openhuman_steering_handle(steering_run_class));++ // Memory protocol (issue #4116): observe the read → dedupe → write →+ // update-index cycle and append a corrective note when a write skips the+ // dedupe read or leaves the index stale. Pushed first / outermost so its+ // `after_tool` runs *after* the byte-cap truncation, keeping the note.+ harness.push_middleware(Arc::new(middleware::MemoryProtocolMiddleware::new()));++ // Repeated-failure circuit breaker: pause the run when a tool returns the same+ // error `REPEATED_TOOL_FAILURE_THRESHOLD` times in a row, so a deterministic+ // security/approval denial or terminal tool error surfaces its root cause+ // instead of burning the whole iteration budget (legacy ProgressGuard parity).+ let halt_summary: HaltSummarySlot = std::sync::Arc::new(std::sync::Mutex::new(None));+ if let Some(handle) = &handle {+ harness.push_middleware(Arc::new(middleware::RepeatedToolFailureMiddleware::new(+ handle.clone(),+ REPEATED_TOOL_FAILURE_THRESHOLD,+ halt_summary.clone(),+ )));+ }++ // Repeat-progress breaker (issue #4463, restoring #4088 / #4095): the failure+ // breaker above resets on every success, so a model looping on a *successful*+ // no-op tool or re-emitting an identical narration+call never trips it. This+ // guard halts on identical successful `(tool, args)` batches / identical+ // outputs, sharing the same halt-summary slot + steering handle. Polling tools+ // (`wait_subagent`) stay exempt.+ if let Some(handle) = &handle {+ harness.push_middleware(Arc::new(middleware::RepeatProgressMiddleware::new(+ handle.clone(),+ halt_summary.clone(),+ )));+ }++ // Policy-driven stop hooks (budget cap, thread-goal budget, ad-hoc iteration+ // ceiling): fire after each model call and pause the run on the first stop+ // vote. Replaces the legacy tool-call-loop firing point.+ if let Some(handle) = &handle {+ if !stop_hooks_installed.is_empty() {+ harness.push_middleware(Arc::new(stop_hooks::StopHookMiddleware::new(+ handle.clone(),+ model,+ max_iterations,+ stop_hooks_installed,+ )));+ }+ }+ let early_exit_set: HashSet<&str> = early_exit_tools.iter().copied().collect();+ // One hook per run, shared by every early-exit adapter (records the first+ // early-exit and pauses). Requires the steering handle.+ let early_exit_hook = handle+ .as_ref()+ .filter(|_| !early_exit_set.is_empty())+ .map(|h| EarlyExitHook::new(h.clone()));++ let is_subagent_run = subagent_scope.is_some();+ let (tool_count, registry_diagnostics, registry_snapshot) = register_turn_tools_and_agents(+ &mut harness,+ &mut capability_registry,+ &tool_sets,+ &allowed,+ &early_exit_set,+ early_exit_hook.as_ref(),+ is_subagent_run,+ );++ // SHADOW tool-exposure layer (issue #4249, 01.3 — dynamic exposure). Compose+ // the OpenHuman exposure policy as a crate-native selection layer+ // (ToolAllowlistMiddleware + a ContextualToolSelectionMiddleware built via+ // `inheriting`) and run it in shadow: it emits the exposure decision+ // event-native (`AgentEvent::ToolsFiltered`) and logs any divergence between+ // the crate layer's decision and the set OpenHuman actually registered as+ // callable — WITHOUT changing the callable set (byte-identical to today). The+ // ownership flip + deletion of `tool_filter.rs`/`tool_prep.rs` is the gated+ // follow-up once the `[tool-exposure]` divergence logs show parity. Tags encode+ // the OpenHuman run context (agent id / channel / scope) for the flip; the+ // name-based `inheriting` predicate does not consult them yet.+ let candidate_names: Vec<String> = tool_sets+ .iter()+ .flat_map(|set| set.iter())+ .map(|tool| tool.name().to_string())+ .collect();+ let mut seen_candidates: HashSet<String> = HashSet::new();+ let candidate_names: Vec<String> = candidate_names+ .into_iter()+ .filter(|name| seen_candidates.insert(name.clone()))+ .collect();+ let exposure_tags: Vec<String> = {+ let mut tags = vec![if subagent_scope.is_some() {+ "scope:subagent".to_string()+ } else {+ "scope:root".to_string()+ }];+ if let Some(scope) = &subagent_scope {+ tags.push(format!("agent:{}", scope.agent_id));+ tags.push(format!("task:{}", scope.task_id));+ }+ if let Some(enforcement) = &tool_policy {+ tags.push(format!("channel:{}", enforcement.channel));+ tags.push(format!("agent_def:{}", enforcement.agent_definition_id));+ }+ tags+ };+ harness.push_middleware(Arc::new(+ middleware::OpenHumanToolExposureShadowMiddleware::new(+ &candidate_names,+ allowed.as_ref(),+ exposure_tags,+ ),+ ));++ // Prompt-cache prefix protection (issue #4249, 03.2). First declare the turn's+ // stable prefix (system prompt + tool schemas) as `PromptSegment`s, then let+ // the crate `PromptCacheGuardMiddleware` diff the cacheable prefix across model+ // calls and record a `CacheLayoutEvent` when volatile content busts it.+ // `before_model` hooks run in registration order, so the segment stamper must+ // precede the guard; both run before the context middlewares below (they only+ // touch the volatile tail / tool bodies, never the stable prefix). The guard is+ // returned so the run loop can drain its events into the observability bridge —+ // the crate-native replacement for the deleted `CacheAlignMiddleware` warn-log+ // (C3: the warn-only shadow is gone; this guard is the sole owner).+ harness.push_middleware(Arc::new(middleware::PromptCacheSegmentMiddleware));+ let prompt_cache_guard = Arc::new(PromptCacheGuardMiddleware::new());+ harness.push_middleware(prompt_cache_guard.clone());++ // openhuman context concerns as graph middlewares (issue #4249): microcompact+ // tool-body clearing and the after-tool byte cap / payload summarizer.+ // Installed before the summarization/trim block below so `before_model` hooks+ // run microcompact → compress → trim. (KV-cache-prefix drift is handled above+ // by the crate `PromptCacheGuardMiddleware`; the warn-only CacheAlign shadow+ // was deleted in C3.) Tool-result caps read the SDK registry policy snapshot,+ // not the OpenHuman-side tool lookup.+ // Capture each tool call's real success + content before the harness folds the+ // result into a `Message::tool` that drops the failure flag, so the turn can+ // build honest per-call `ToolCallRecord`s (post-turn hooks + cap checkpoint).+ //+ // REVERSE-ORDER RULE (issue #4464): the crate runs `after_tool` in REVERSE+ // registration order, so the LATER a middleware is pushed the EARLIER its+ // `after_tool` runs. This capture must observe the FINAL (summarized/capped)+ // content, so it is pushed BEFORE `context_mw.install` (which registers the+ // handoff + tool-output budget/caps) — that way its `after_tool` runs AFTER+ // those caps, not before. Registering it after `install` (the pre-#4464 bug)+ // made its `after_tool` run first and record the full raw payload of every+ // call, bloating the per-turn sink and feeding failure classification /+ // `ToolCallRecord.output_summary` pre-cap content.+ let tool_outcome_sink: ToolOutcomeSink = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));+ let failure_map: ToolFailureMap = Arc::default();+ harness.push_middleware(Arc::new(middleware::ToolOutcomeCaptureMiddleware::new(+ tool_outcome_sink.clone(),+ failure_map.clone(),+ )));++ let tool_policies = harness.tools().policies();+ context_mw.install(&mut harness, tool_policies);++ // Observe-only crate `BudgetMiddleware` (W2-budget-dedupe / workstream 06).+ // Installed with empty `BudgetLimits` so it NEVER enforces or halts: its+ // `before_model` preflight has no configured limit to trip, and its+ // `after_model` only folds each call's usage into its shared `BudgetTracker`.+ // It also re-emits `AgentEvent::UsageRecorded` per call (on top of the+ // runtime's own emit); the event bridge dedupes those by model-call iteration+ // so the global cost tracker still records each call exactly once (see+ // `observability::OpenhumanEventBridge::record_usage`). Enforcement STAYS with+ // the local `CostBudgetMiddleware` below (authoritative: reads the global+ // daily/monthly `CostTracker`).+ //+ // FLIP CRITERIA — what must hold before the crate `BudgetMiddleware` becomes+ // the enforcing owner and the local `CostBudgetMiddleware` + the+ // `agent/harness/turn_subagent_usage.rs` task-local are DELETED (deletion+ // ledger row: "crate-internal CostBudgetMiddleware + turn_subagent_usage.rs+ // task-local", `docs/tinyagents-full-migration-plan/99-deletion-ledger.md`):+ // 1. ≥ 500 production turns across BOTH parent and sub-agent runs with+ // ZERO `[budget_shadow]` divergence log lines — proving the crate+ // tracker's per-run token accounting matches the authoritative runtime+ // `AgentRun.usage` on every model call.+ // 2. A pricing table wired via `BudgetMiddleware::with_pricing(..)` at+ // parity with `cost::catalog::estimate_cost_usd`, so the crate can own+ // MONEY (USD) budgets. Today the shadow compares TOKENS only (the+ // observe-only crate middleware has no pricing, so its cost stays $0)+ // and the local gate is the sole money-budget authority.+ // 3. Run-tree rollup wired: the same shared `BudgetTracker` handed to every+ // sub-agent harness so a parent budget halts a recursive run pre-spend —+ // replacing the `turn_subagent_usage` parent-turn rollup (06-cost step 3+ // / 07.2 TaskStore rollup).+ // Until all three hold, this middleware is observe-only and the local gate+ // enforces.+ let shadow_budget = Arc::new(BudgetMiddleware::new(BudgetLimits::default()));+ let shadow_budget_tracker = shadow_budget.tracker();+ harness.push_middleware(shadow_budget);++ // Pre-call cost budget gate (issue #4249, Phase 5) — AUTHORITATIVE+ // enforcement: fail before a model call when OpenHuman's daily/monthly cost+ // budget is already exceeded. Self-gating — a no-op unless cost budgets are+ // configured. Demoted to a divergence-logging shadow owner (W2-budget-dedupe):+ // it keeps enforcing exactly as before, but ALSO compares its per-run token+ // accounting against the observe-only crate `BudgetMiddleware` above at end of+ // run and logs `[budget_shadow]` parity/divergence.+ harness.push_middleware(Arc::new(middleware::CostBudgetMiddleware::with_shadow(+ shadow_budget_tracker,+ )));+ // Autocompaction parity: when the provider's context window is known, install+ // the two-stage context-management step (issue #4249).+ //+ // 1. `ContextCompressionMiddleware` — the **summarization** step. Once the+ // running token estimate crosses `window * SUMMARIZE_THRESHOLD_FRACTION`+ // (90% of *this model's* context window), it folds the older slice of the+ // transcript into a single LLM-generated system summary (keeping system+ // messages + the recent window verbatim). This is keyed to whatever model+ // the turn is running on, preserving the legacy context threshold.+ // 2. `ImageAwareMessageTrimMiddleware` — a deterministic, no-extra-LLM-call+ // hard cap (issue #4462; replaces the crate `MessageTrimMiddleware`).+ // Pushed **after** compression (so `before_model` runs compression first),+ // it front-trims to the legacy proportional budget only as a last resort+ // when even the summary + recent window still overflow — image markers+ // priced flat, system messages never dropped, evictions logged.+ //+ // The LLM summarization step honors the `[context].enabled` /+ // `autocompact_enabled` opt-outs (a disabled config must not spend summarizer+ // tokens or rewrite history); the deterministic trim backstop always installs+ // when a window is known, matching the legacy always-on `trim_history` cap.+ // Concrete handle to the compression middleware (when installed), retained so+ // the run loop can drain its `records()` after the drive future returns and+ // surface each compaction's provenance (source ids + before/after token+ // estimates) — the `AgentEvent::Compressed` projection only carries the token+ // deltas, so provenance would otherwise be dropped (issue #4249, 03.1 item 6).+ let mut compression_mw: Option<Arc<ContextCompressionMiddleware>> = None;+ if let Some(window) = context_window.filter(|w| *w > 0) {+ if autocompact_enabled {+ let policy = summarize::summarization_policy(window);+ // Wrap the LLM-backed summarizer in a fault-tolerant, per-turn-caching+ // adapter (issue #4461): a summarizer failure must no longer abort the+ // turn (warn + circuit-breaker + deterministic trim instead), and an+ // identical re-issued input slice must not re-run the summarizer LLM.+ let summarizer = summarize::FaultTolerantCachingSummarizer::new(+ Box::new(summarize::ModelSummarizer::new(summarizer_model, model)),+ &policy,+ );+ let mw = Arc::new(ContextCompressionMiddleware::with_summarizer(+ policy,+ Box::new(summarizer),+ ));+ harness.push_middleware(mw.clone());+ compression_mw = Some(mw);+ }++ // Deterministic hard-cap trim (issue #4462). The crate+ // `MessageTrimMiddleware` regressed three legacy `token_budget.rs`+ // guards: it priced a base64 image at ~2M tokens (chars/4) and could+ // evict system messages, it reordered system messages to the front, and+ // its budget was the fixed `window − AGENT_TURN_MAX_OUTPUT_TOKENS`+ // (floored 1024) that collapses an 8k local model's input budget from+ // ~7373 to 1024. Our seam-owned `ImageAwareMessageTrimMiddleware`+ // restores all three: image markers priced at a flat cost, the+ // proportional reply reserve, system messages always kept in place, and a+ // grep-able warn with drop/token counts on any eviction.+ }++ // ── The context ladder, cheapest sufficient step first (issue #6014) ──────+ //+ // `before_model` runs in registration order, so what follows IS the firing+ // order, and each step only matters when the one above was not enough:+ // 1. compression (above) folds the older slice into a task-aware summary;+ // 2. microcompact blanks older tool bodies if still over;+ // 3. the wrap-up undoes (2) for a capped turn's concluding call;+ // 4. trim evicts oldest whole messages if still over.+ //+ // The order used to be 2 -> 1 -> 4, because `context_mw.install` registered+ // microcompact: the summarizer was handed a transcript whose tool bodies+ // were already `CLEARED_PLACEHOLDER` and asked, by its own prompt, for "key+ // results/outputs" it could no longer see. Nothing recovered them.+ if microcompact_keep_recent > 0 {+ // The token-budget gate the crate added for this (#4755) and this call+ // site never used. Constructed bare, microcompact blanks on EVERY call+ // past `keep_recent` — not only under pressure — so a ten-call turn+ // answered from the last five results while well inside a window with+ // room for all ten. Not a capped-turn problem: every turn. The budget+ // matches the trim's allowance, putting microcompact strictly behind+ // compression. With no window there is nothing to size against and+ // nothing else bounding growth, so always-blank stays as the backstop.+ let microcompact = tinyagents_harness::middleware::MicrocompactMiddleware::new(+ microcompact_keep_recent,+ crate::agent::context::CLEARED_PLACEHOLDER,+ );+ let microcompact = match context_window.filter(|w| *w > 0) {+ Some(window) => {+ microcompact.with_token_budget(middleware::legacy_max_input_tokens(window).max(1))+ }+ None => microcompact,+ };+ // Emit `AgentEvent::Compressed` when a body is cleared. Off by default —+ // the middleware was built as "a silent transcript rewrite" — which is+ // why blanking was, until now, the one reduction step nobody could see+ // happening: compression logs its provenance, the trim warns on every+ // eviction, and the per-result artifact store logs each persist. Only+ // this one destroyed content without saying so, which is how it went+ // unnoticed that it was doing it on every call.+ harness.push_middleware(Arc::new(microcompact.with_events(true)));+ }++ // Issue #6014: make the last permitted model call the turn's conclusion,+ // rather than leaving the answer to an extra out-of-band call after the loop+ // has exited.+ //+ // **Top-level turns only**, the same cut `CapPauser`'s dispatch guard takes.+ // A sub-agent reaching its own cap is a routine outcome, not a user-visible+ // dead end: it summarises, hands the result back to its parent, and+ // `subagent_runner` already owns that checkpoint. The harm this fixes — a+ // person left with a status line where an answer should be — belongs to the+ // turn that answers a human. It also leaves a child's budget alone: the+ // conclusion costs a call, and turning every delegated run's N tool rounds+ // into N-1 is not a trade to impose as a side effect.+ // One allowance, split between the two things that add to the request+ // (CodeRabbit on #6068). Computed independently they were each bounded and+ // the pair was not: restoration could fill the whole allowance and the+ // contents list add its tenth on top. `0` when no window is advertised.+ let trim_allowance = context_window+ .filter(|w| *w > 0)+ .map(|w| middleware::legacy_max_input_tokens(w).max(1))+ .unwrap_or(0);+ let (toc_allowance, restore_allowance) = middleware::split_input_allowance(trim_allowance);++ let wrap_up_mw = (pause_at_cap && subagent_scope.is_none()).then(|| {+ Arc::new(middleware::FinalCallWrapUpMiddleware::new(+ crate::agent::harness::session::turn_checkpoint::MAX_ITER_CHECKPOINT_INSTRUCTION,+ tool_outcome_sink.clone(),+ // What is left after the contents list's share, so restoration+ // stops short of provoking an eviction (see the middleware).+ restore_allowance,+ ))+ });+ let wrap_up_fired = wrap_up_mw.as_ref().map(|mw| mw.fired());+ if let Some(mw) = wrap_up_mw {+ harness.push_middleware(mw);+ }++ // Issue #6014: say where the offloaded results went, from the index rather+ // than the transcript. After the reduction steps so it renders what+ // survived them, before the trim so its message is counted in that budget.+ // Its share of the allowance split above: the list is a system message, so+ // nothing downstream can shrink it (see the middleware).+ harness.push_middleware(Arc::new(middleware::ArtifactIndexTocMiddleware::new(+ toc_allowance,+ )));++ if let Some(window) = context_window.filter(|w| *w > 0) {+ // Deterministic hard-cap trim (issue #4462), last in the ladder: it drops+ // whole messages, so it runs only once summarizing and blanking have both+ // failed to fit the window. See `ImageAwareMessageTrimMiddleware` for the+ // three legacy guards it restores over the crate trim.+ harness.push_middleware(Arc::new(+ middleware::ImageAwareMessageTrimMiddleware::for_context_window(window),+ ));+ }++ // SDK-owned tool-policy projection (issue #4249 / tinyagents-full-migration+ // 01.1). Keep this narrow for now: enforce sandbox requirements declared by+ // adapter policies without enabling classification/approval/result-byte+ // gates yet. `require_classification(true)` would currently reject an+ // unregistered hallucinated tool in `before_tool` before+ // `RunPolicy::unknown_tool` can return a recoverable tool error, while+ // OpenHuman's existing wrappers still own HITL approval and output caps.+ harness.push_middleware(Arc::new(+ TaToolPolicyMiddleware::new(harness.tools().policies()).require_sandbox(true),+ ));++ // Human-in-the-loop approval as a named tool middleware (issue #4249,+ // Phase 1): an external-effect tool intercepts through the global+ // `ApprovalGate`, a denial short-circuits with a model-consumable result, and+ // an approved call records a terminal audit row. Replaces the inline approval+ // block that used to live in `execute_openhuman_tool`.+ harness.push_tool_middleware(Arc::new(middleware::ApprovalSecurityMiddleware::new(+ tool_sets.clone(),+ )));++ // CLI/RPC-only scope gate — a tool restricted to explicit CLI/RPC invocation+ // must not run from the model loop. Intrinsic to the tool, so installed on+ // every path (channel/session/sub-agent).+ harness.push_tool_middleware(Arc::new(middleware::CliRpcOnlyMiddleware::new(+ tool_sets.clone(),+ )));++ // Builder-configured tool policy (`.tool_policy()`), enforced at the tool+ // boundary. The in-house engine ran this in `agent_tool_exec`; the tinyagents+ // path bypassed it, so a deny/require-approval silently no-opped (security+ // regression). Installed only when the caller threads an enforcement context+ // (the session chat path); channel/CLI + sub-agent paths pass `None`.+ if let Some(enforcement) = tool_policy {+ harness.push_tool_middleware(Arc::new(middleware::ToolPolicyMiddleware::new(+ enforcement.policy,+ enforcement.session,+ tool_sets.clone(),+ enforcement.session_id,+ enforcement.channel,+ enforcement.agent_definition_id,+ )));+ }++ // Credential scrubbing (issue #4453): redact credential-shaped secrets out of+ // every tool result. The legacy engine ran `scrub_credentials` over every+ // tool output before it entered model context; the tinyagents path dropped+ // that call site. Installed as the **innermost** tool wrap (pushed last) so+ // it scrubs the RAW tool result before any outer wrap, the `after_tool`+ // chain (summarization/caps), the transcript push, or the tool-outcome+ // capture sink can observe the unredacted content — covering the parent,+ // sub-agent, persisted-transcript, and `ToolCallOutcome` surfaces by+ // construction since every path shares this seam.+ harness.push_tool_middleware(Arc::new(middleware::CredentialScrubMiddleware::new()));++ // Malformed-argument recovery (`before_tool`): repair a call's non-object+ // arguments before the crate's schema gate — decode JSON-encoded-string args+ // (optionally markdown-fenced) to an object, or coerce to `{}` only when the+ // tool schema has no required fields (engine parity). A non-object against a+ // required-field schema is left untouched so the crate's+ // `InvalidArgsPolicy::ReturnToolError` admission path reports the original+ // validation error. It never reaches approval/policy wrappers or the tool.+ harness.push_middleware(Arc::new(middleware::ArgRecoveryMiddleware::new(+ tool_sets.clone(),+ )));++ // Embedder tool lifecycle hooks. Registered AFTER `ArgRecoveryMiddleware`:+ // `before_tool` runs in registration order, so a hook installed earlier would+ // observe the provider's raw (possibly JSON-encoded-string / non-object)+ // arguments instead of the recovered object that `ToolHookContext` documents.+ // The middleware caches the normalized pre-call arguments by `call_id` and+ // replays them into the `PostToolUse` context. Its `after_tool` is+ // observation-only (never mutates the result), so running first in the+ // reverse-order `after_tool` chain is safe — it cannot perturb the+ // summarization/cap or tool-outcome capture layers.+ let embedder_tool_hooks = crate::agent::hooks::embedder_tool_hooks();+ if !embedder_tool_hooks.is_empty() {+ harness.push_middleware(Arc::new(middleware::EmbedderToolHooksMiddleware::new(+ embedder_tool_hooks,+ )));+ }++ AssembledTurnHarness {+ harness,+ cursor,+ tool_names,+ failure_map,+ provider_usage_carry,+ error_slot,+ halt_summary,+ tool_outcome_sink,+ handle,+ early_exit_hook,+ wrap_up_fired,+ tool_count,+ registry_snapshot,+ registry_diagnostics,+ tool_result_artifact_index,+ compression_mw,+ prompt_cache_guard,+ }+}diff --git a/crates/openhuman-core/src/channels/runtime/startup/relay.rs b/crates/openhuman-core/src/channels/runtime/startup/relay.rsnew file mode 100644index 0000000000..4f50e4d03a--- /dev/null+++ b/crates/openhuman-core/src/channels/runtime/startup/relay.rs@@ -0,0 +1,80 @@+//! The TinyChannels relay runtime: inbound handler wiring and connect/handshake.++use super::super::dispatch::RuntimeChannelMessage;+use anyhow::Result;+use async_trait::async_trait;+use std::sync::Arc;+use tokio::sync::mpsc;++pub(super) struct RelayInboundMessageHandler {+ tx: mpsc::Sender<RuntimeChannelMessage>,+}++impl RelayInboundMessageHandler {+ pub(super) fn new(tx: mpsc::Sender<RuntimeChannelMessage>) -> Self {+ Self { tx }+ }+}++#[async_trait]+impl tinychannels::relay::RelayInboundHandler for RelayInboundMessageHandler {+ async fn handle(+ &self,+ event: tinychannels::relay::AuthenticatedRelayInboundEvent,+ ) -> Result<(), tinychannels::relay::RelayTransportError> {+ let envelope: tinychannels::ChannelInboundEnvelope = serde_json::from_value(event.event)+ .map_err(|error| {+ tinychannels::relay::RelayTransportError::Handler(format!(+ "invalid inbound envelope: {error}"+ ))+ })?;+ let msg = tinychannels::legacy_message_from_inbound_envelope(&envelope, 0);+ self.tx+ .send(RuntimeChannelMessage::with_inbound_envelope(msg, envelope))+ .await+ .map_err(|_| tinychannels::relay::RelayTransportError::Closed)+ }+}++pub(super) struct RelayRuntimeHandle {+ pub(super) _transport: Arc<tinychannels::relay::RelayTransport>,+ pub(super) _reconnect: tinychannels::relay::RelayReconnectHandle,+}++pub(super) async fn start_relay_runtime(+ relay: &tinychannels::config::RelayRuntimeConfig,+ tx: mpsc::Sender<RuntimeChannelMessage>,+) -> Result<RelayRuntimeHandle> {+ anyhow::ensure!(+ relay.is_listener_configured(),+ "relay runtime requires non-empty url and at least one identity"+ );++ let websocket_config = tinychannels::relay::WebSocketRelayConfig::from(relay);+ let io = tinychannels::relay::connect_websocket_relay_io(&websocket_config).await?;+ let transport = Arc::new(tinychannels::relay::RelayTransport::new(+ relay.relay_identities(),+ Arc::new(io),+ relay.timeouts,+ ));+ transport+ .set_inbound_handler(Arc::new(RelayInboundMessageHandler::new(tx)))+ .await;+ transport.connect().await?;+ let descriptor = transport.handshake().await?;+ tracing::info!(+ label = %descriptor.label,+ max_message_length = descriptor.max_message_length,+ "[channels][relay] connected relay runtime"+ );+ crate::channels::relay_runtime::register_relay_transport(transport.clone());++ let dialer = Arc::new(tinychannels::relay::WebSocketRelayDialer::new(+ websocket_config,+ ));+ let reconnect = transport.spawn_reconnect_supervisor(dialer, relay.reconnect);+ Ok(RelayRuntimeHandle {+ _transport: transport,+ _reconnect: reconnect,+ })+}diff --git a/crates/openhuman-core/src/memory/sources/rpc/cost_reporting.rs b/crates/openhuman-core/src/memory/sources/rpc/cost_reporting.rsnew file mode 100644index 0000000000..9df5145d93--- /dev/null+++ b/crates/openhuman-core/src/memory/sources/rpc/cost_reporting.rs@@ -0,0 +1,233 @@+//! Sync audit log, per-source cost estimation, and the monthly cost summary.++use super::coding_sessions::unserved;+use crate::config::rpc as config_rpc;+use crate::memory::api::provider::sync::SyncAuditEntry;+use crate::memory::sources::readers;+use crate::memory::sources::registry;+use crate::rpc::RpcOutcome;++// ── Sync Audit Log ──++#[derive(Debug, serde::Serialize)]+pub struct SyncAuditLogResponse {+ pub entries: Vec<SyncAuditEntry>,+}++/// Past sync runs, newest first.+///+/// # This is now the driver's most recent rows, not the whole log+///+/// The engine call this replaced returned every line in+/// `<workspace>/memory_tree/sync_audit.jsonl`. `sync_audit_log` is capped:+/// `None` means "the driver's own ceiling", explicitly **not** unbounded,+/// because the log is append-only for the life of a workspace and an unbounded+/// read eventually cannot cross a frame at all. The panel that renders this+/// shows recent runs, so the cap is not a visible reduction there — but it is a+/// real one, and `monthly_cost_summary_rpc` below is where it has to be said+/// out loud rather than absorbed.+///+/// A read failure is now an error rather than an empty log. The engine wrapper+/// ended in `unwrap_or_default()`, so an unreadable file was reported as "no+/// syncs have run" — the one answer a caller cannot distinguish from the truth.+pub async fn sync_audit_log_rpc() -> Result<RpcOutcome<SyncAuditLogResponse>, String> {+ tracing::debug!("[memory_sources] sync_audit_log_rpc: entry");+ let config = config_rpc::load_config_with_timeout().await?;+ let binding = crate::memory::binding::for_config(&config)?;+ let Some(sync) = binding.provider().as_source_sync() else {+ return Err(unserved(&binding, "source sync", "sync_audit_log"));+ };++ // `None` = the driver's own cap. A caller cannot raise it by asking for+ // more, so passing a number here would only be this host inventing a+ // ceiling the driver then clamps anyway.+ let entries = sync+ .sync_audit_log(None)+ .await+ .map_err(|error| format!("sync audit log: {error}"))?;++ tracing::debug!(+ driver = %binding.driver_id(),+ entries = entries.len(),+ "[memory_sources] sync_audit_log_rpc: exit"+ );+ Ok(RpcOutcome::new(SyncAuditLogResponse { entries }, vec![]))+}++// ── Estimate Sync Cost ──++#[derive(Debug, serde::Deserialize)]+pub struct EstimateSyncCostRequest {+ pub source_id: String,+}++#[derive(Debug, serde::Serialize)]+pub struct EstimateSyncCostResponse {+ pub source_id: String,+ pub item_count: u32,+ pub estimated_tokens: u64,+ pub estimated_cost_usd: f64,+ pub budget_max_cost_usd: Option<f64>,+ pub budget_max_tokens: Option<u64>,+}++/// Project what syncing one source would cost.+///+/// The item count and the token estimate are this host's (they come from the+/// reader's listing and from the per-item allowances below); the **price** is+/// the driver's, asked through `estimate_sync_cost_usd`. That split is the+/// whole point of the member — see the module docs. A driver that serves no+/// sync family has no price to quote, and this refuses rather than quoting+/// `0.0`, which would read as "syncing this is free".+pub async fn estimate_sync_cost_rpc(+ req: EstimateSyncCostRequest,+) -> Result<RpcOutcome<EstimateSyncCostResponse>, String> {+ tracing::debug!(source_id = %req.source_id, "[memory_sources] estimate_sync_cost_rpc: entry");++ let source = registry::get_source(&req.source_id)+ .await?+ .ok_or_else(|| format!("source '{}' not found", req.source_id))?;++ let config = config_rpc::load_config_with_timeout().await?;+ let binding = crate::memory::binding::for_config(&config)?;+ let Some(sync) = binding.provider().as_source_sync() else {+ return Err(unserved(&binding, "source sync", "estimate_sync_cost"));+ };++ let reader = readers::reader_for(&source.kind);+ let items = reader.list_items(&source, &config).await?;++ let item_count = items.len() as u32;+ // estimated_tokens includes both input (500/item) and output (100/item)+ // to be consistent with the cost calculation below.+ let estimated_input_tokens = item_count as u64 * 500;+ let estimated_output_tokens = item_count as u64 * 100;+ let estimated_tokens = estimated_input_tokens + estimated_output_tokens;+ let estimated_cost_usd = sync+ .estimate_sync_cost_usd(estimated_input_tokens, estimated_output_tokens)+ .await+ .map_err(|error| format!("estimate sync cost: {error}"))?;++ tracing::debug!(+ driver = %binding.driver_id(),+ source_id = %req.source_id,+ item_count,+ estimated_tokens,+ estimated_cost_usd,+ "[memory_sources] estimate_sync_cost_rpc: exit"+ );+ Ok(RpcOutcome::new(+ EstimateSyncCostResponse {+ source_id: req.source_id,+ item_count,+ estimated_tokens,+ estimated_cost_usd,+ budget_max_cost_usd: source.max_cost_per_sync_usd,+ budget_max_tokens: source.max_tokens_per_sync,+ },+ vec![],+ ))+}++// ── Monthly Cost Summary ──++#[derive(Debug, Clone, PartialEq, serde::Serialize)]+pub struct MonthlyCostSummaryResponse {+ pub month: String,+ pub total_cost_usd: f64,+ pub total_syncs: u32,+ pub total_items: u32,+ pub total_input_tokens: u64,+ pub total_output_tokens: u64,+ /// Whether the audit read reached back past the start of `month`.+ ///+ /// `sync_audit_log` is capped by the driver, so a workspace that synced+ /// more times this month than the cap allows would silently total only the+ /// newest of them. `true` means at least one row *older* than `month` came+ /// back, which proves every row inside `month` was in the read; `false`+ /// means the read ran out first and the totals above are a **floor**.+ ///+ /// Deliberately conservative in one direction: a driver that simply has no+ /// older rows also reports `false`, so this can say "possibly short" when+ /// the totals are in fact exact. It never says "complete" when they are+ /// not, which is the direction that matters for a money figure.+ pub totals_complete: bool,+}++/// Total one month of audit rows.+///+/// Pure, so the cap-versus-boundary rule above is unit-testable without a+/// driver. Order-independent on purpose: the contract promises newest-first and+/// stopping at the first older row would be cheaper, but the list is already+/// bounded by the driver's cap, and a scan that does not depend on the ordering+/// cannot silently under-count if a driver ever returns rows out of order.+///+/// `pub(super)` so `rpc`'s sibling test module can reach it through+/// `super::*`.+pub(super) fn summarise_month(entries: &[SyncAuditEntry], month: &str) -> MonthlyCostSummaryResponse {+ let mut summary = MonthlyCostSummaryResponse {+ month: month.to_string(),+ total_cost_usd: 0.0,+ total_syncs: 0,+ total_items: 0,+ total_input_tokens: 0,+ total_output_tokens: 0,+ // An empty log has no rows the cap could have hidden.+ totals_complete: entries.is_empty(),+ };++ for entry in entries {+ let entry_month = entry.timestamp.format("%Y-%m").to_string();+ // `%Y-%m` is zero-padded and fixed-width, so lexicographic order is+ // chronological order and this needs no date arithmetic.+ if entry_month.as_str() < month {+ summary.totals_complete = true;+ continue;+ }+ // Not `else` — a row stamped in a *later* month (clock skew) is skipped+ // rather than counted, exactly as the engine-backed filter did.+ if entry_month != month {+ continue;+ }+ summary.total_cost_usd += entry.effective_cost_usd();+ // Saturating: the counters are `u32` on the wire and an overflow here+ // would be a debug-build panic inside a read-only reporting RPC.+ summary.total_syncs = summary.total_syncs.saturating_add(1);+ summary.total_items = summary.total_items.saturating_add(entry.items_fetched);+ summary.total_input_tokens = summary+ .total_input_tokens+ .saturating_add(entry.input_tokens);+ summary.total_output_tokens = summary+ .total_output_tokens+ .saturating_add(entry.output_tokens);+ }++ summary+}++pub async fn monthly_cost_summary_rpc() -> Result<RpcOutcome<MonthlyCostSummaryResponse>, String> {+ tracing::debug!("[memory_sources] monthly_cost_summary_rpc: entry");+ let config = config_rpc::load_config_with_timeout().await?;+ let binding = crate::memory::binding::for_config(&config)?;+ let Some(sync) = binding.provider().as_source_sync() else {+ return Err(unserved(&binding, "source sync", "monthly_cost_summary"));+ };++ let entries = sync+ .sync_audit_log(None)+ .await+ .map_err(|error| format!("sync audit log: {error}"))?;++ let month = chrono::Utc::now().format("%Y-%m").to_string();+ let summary = summarise_month(&entries, &month);++ tracing::debug!(+ driver = %binding.driver_id(),+ month = %summary.month,+ rows_read = entries.len(),+ syncs = summary.total_syncs,+ totals_complete = summary.totals_complete,+ "[memory_sources] monthly_cost_summary_rpc: exit"+ );+ Ok(RpcOutcome::new(summary, vec![]))+}