Skip to content

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

chore: files changed crates/openhuman-core/src/channels/bus.rs,crates/openhuman-core/src/agent/harne

Steven Enamakel12d4b6d8d74b
diff --git a/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph/dispatch.rs b/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph/dispatch.rs
new file mode 100644
index 0000000000..8b4edba774
--- /dev/null
+++ b/crates/openhuman-core/src/agent/harness/subagent_runner/ops/graph/dispatch.rs
@@ -0,0 +1,636 @@
+//! The sub-agent turn dispatch: driving one graph turn through the shared
+//! tinyagents seam, building its context middleware, and folding a cap-hit
+//! checkpoint summary back into the result.
+
+use std::collections::HashSet;
+use std::sync::Arc;
+
+use crate::agent::harness::agent_graph::{AgentTurnRequest, AgentTurnResult, AgentTurnUsage};
+use crate::agent::harness::subagent_runner::types::SubagentRunError;
+use crate::agent::messages::{ChatMessage, ConversationMessage};
+use crate::agent::progress::AgentProgress;
+use crate::agent::tinyagents::{run_turn_via_tinyagents_shared, SubagentScope};
+use crate::inference::tokenjuice::AgentTokenjuiceCompression;
+use crate::tools::{Tool, ToolSpec};
+use tinyagents_harness::workspace::WorkspaceDescriptor;
+
+use super::transcript::persist_subagent_transcript;
+use super::worker_mirror::mirror_worker_thread;
+
+/// Cumulative usage stats gathered across a sub-agent graph run.
+#[derive(Debug, Clone, Default)]
+pub(in super::super) struct AggregatedUsage {
+ pub(in super::super) input_tokens: u64,
+ pub(in super::super) output_tokens: u64,
+ pub(in super::super) cached_input_tokens: u64,
+ pub(in super::super) charged_amount_usd: f64,
+}
+
+/// Run an assembled custom per-agent turn through the shared default sub-agent
+/// leaf. Bespoke `AgentGraph::Custom` graphs use this after their own routing
+/// nodes so transcript persistence, worker-thread mirroring, progress events,
+/// handoff middleware, cap summaries, and usage aggregation stay byte-for-byte
+/// on the default path.
+pub(crate) async fn run_agent_turn_request_via_default_graph(
+ req: AgentTurnRequest,
+) -> Result<AgentTurnResult, SubagentRunError> {
+ let AgentTurnRequest {
+ turn_model_source,
+ model,
+ temperature,
+ mut history,
+ parent_tools,
+ dynamic_tools,
+ specs,
+ allowed_names,
+ max_iterations,
+ run_queue,
+ on_progress,
+ agent_id,
+ task_id,
+ extended_policy,
+ worker_thread_id,
+ workspace_dir,
+ workspace_descriptor,
+ max_output_tokens,
+ model_vision,
+ transcript_stem,
+ provider_label,
+ handoff_cache,
+ tokenjuice_compression,
+ config,
+ } = req;
+
+ let (output, iterations, usage, early_exit_tool, hit_cap, breaker_halt) =
+ run_subagent_via_graph(
+ turn_model_source,
+ &model,
+ temperature,
+ &mut history,
+ parent_tools,
+ dynamic_tools,
+ specs,
+ allowed_names,
+ max_iterations,
+ run_queue,
+ on_progress,
+ &agent_id,
+ &task_id,
+ extended_policy,
+ worker_thread_id,
+ workspace_dir,
+ workspace_descriptor,
+ max_output_tokens,
+ model_vision,
+ &transcript_stem,
+ &provider_label,
+ handoff_cache,
+ tokenjuice_compression,
+ config.as_deref(),
+ )
+ .await?;
+
+ Ok(AgentTurnResult {
+ history,
+ output,
+ iterations,
+ usage: AgentTurnUsage {
+ input_tokens: usage.input_tokens,
+ output_tokens: usage.output_tokens,
+ cached_input_tokens: usage.cached_input_tokens,
+ charged_amount_usd: usage.charged_amount_usd,
+ },
+ early_exit_tool,
+ hit_cap,
+ breaker_halt,
+ })
+}
+
+/// Drive a sub-agent turn on the tinyagents harness. Returns
+/// `(text, model_calls, AggregatedUsage, early_exit_tool, hit_cap)` — `hit_cap`
+/// is `true` when the run stopped at the model-call cap with work still pending
+/// (the caller surfaces this as `SubagentRunStatus::Incomplete`, #4096).
+#[allow(clippy::too_many_arguments)]
+pub(in super::super) async fn run_subagent_via_graph(
+ source: crate::agent::tinyagents::TurnModelSource,
+ model: &str,
+ temperature: f64,
+ history: &mut Vec<ChatMessage>,
+ parent_tools: Arc<Vec<Box<dyn Tool>>>,
+ dynamic_tools: Vec<Box<dyn Tool>>,
+ specs: Vec<ToolSpec>,
+ allowed_names: HashSet<String>,
+ max_iterations: usize,
+ run_queue: Option<Arc<crate::agent::harness::run_queue::RunQueue>>,
+ on_progress: Option<tokio::sync::mpsc::Sender<AgentProgress>>,
+ agent_id: &str,
+ task_id: &str,
+ extended_policy: bool,
+ worker_thread_id: Option<String>,
+ workspace_dir: std::path::PathBuf,
+ workspace_descriptor: Option<WorkspaceDescriptor>,
+ max_output_tokens: u32,
+ model_vision: bool,
+ // Transcript-persistence provenance: the resolved child transcript stem
+ // (`{parent_chain}__{child_session_key}`) and the provider label (the parent
+ // turn's event channel) so the child's raw transcript lands in `session_raw`
+ // with the right stem + provider/model meta — parity with the removed
+ // `SubagentObserver::persist_transcript`.
+ transcript_stem: &str,
+ provider_label: &str,
+ // Progressive-disclosure handoff cache (integrations_agent with a resolved
+ // toolkit); `Some` installs the `HandoffMiddleware` that stashes oversized
+ // tool results and shares the cache with the `extract_from_result` tool.
+ handoff_cache: Option<
+ std::sync::Arc<crate::agent::harness::subagent_runner::ResultHandoffCache>,
+ >,
+ // Agent-level TokenJuice profile (`definition.effective_tokenjuice_compression()`,
+ // #4466). Threaded into the sub-agent `TurnContextMiddleware` so sub-agent
+ // tool outputs get the same content-aware compaction the chat path applies
+ // instead of a blunt byte-cap truncation.
+ tokenjuice_compression: AgentTokenjuiceCompression,
+ // Host config for the `[context]` middleware knobs. Passed in rather than
+ // loaded here (plan-agents Phase 3): this function is slated to move into
+ // TinyAgents, where there is no config file. `None` yields the safe
+ // byte-cap-only defaults.
+ config: Option<&crate::config::Config>,
+) -> Result<
+ (
+ String,
+ usize,
+ AggregatedUsage,
+ Option<String>,
+ bool,
+ // Breaker-halt reason (#4466): `Some` when the repeated-failure /
+ // repeat-progress circuit breaker stopped the run; the caller reports
+ // `Incomplete` instead of `Completed`.
+ Option<String>,
+ ),
+ SubagentRunError,
+> {
+ tracing::info!(
+ model,
+ max_iterations,
+ agent_id,
+ task_id,
+ model_vision,
+ observed = on_progress.is_some(),
+ "[subagent_runner:graph] routing sub-agent turn through tinyagents harness"
+ );
+ // `specs` is derived from the registry inside the runner; the tinyagents
+ // adapters advertise each tool via its own `spec()`, so it's unused here.
+ let _ = &specs;
+
+ // Child-progress attribution: mirror this sub-agent's iterations / tool calls
+ // / text + thinking deltas as `Subagent*` events scoped to (`agent_id`,
+ // `task_id`) so the parent thread can nest them under the live subagent row.
+ // Always set (not gated on `on_progress`): the scope also tells the shared
+ // seam this is a sub-agent turn, so the unknown-tool recovery uses the
+ // sub-agent wording. With no progress sink the scoped events simply have
+ // nowhere to go, which is harmless.
+ let subagent_scope = Some(SubagentScope {
+ agent_id: agent_id.to_string(),
+ task_id: task_id.to_string(),
+ extended_policy,
+ });
+
+ // A standalone summarizer model for the cap-hit checkpoint call below (the
+ // turn's own model set is consumed by the run). Built off the same source, so
+ // the checkpoint invokes a crate `ChatModel` without naming `Provider`
+ // (issue #4249, Phase 3 / Motion A).
+ let summary_model = source.build_summarizer(model, temperature)?;
+
+ // Resolve the sub-agent model's effective context window so the harness runs
+ // the context-window summarization step (issue #4249) on sub-agent turns too.
+ // A long-running / resumed sub-agent (worker threads, durable sessions) can
+ // accumulate a transcript past its own window; summarize before each model
+ // call rather than relying solely on the parent's one-time trim.
+ let context_window = source.effective_context_window(model).await;
+
+ // Build the child turn's crate `ChatModel` set from the source; capability
+ // reads (vision/native-tools) + telemetry id now come off the built bundle,
+ // so the sub-agent path names crate model types only.
+ let turn_models = source.build(model, temperature, context_window)?;
+
+ // Vision forwarding (parity with the legacy `run_inner_loop`): rehydrate
+ // `[IMAGE:…]` placeholders in the sub-agent's history when either the model
+ // advertises vision or the sub-agent model is user-flagged as vision-capable
+ // (BYOK/custom). The expanded copy is provider-only — the persisted `history`
+ // written back below keeps the original markers.
+ let dispatch_history = if (turn_models.supports_vision() || model_vision)
+ && crate::agent::multimodal::has_image_placeholders(history)
+ {
+ crate::agent::multimodal::rehydrate_image_placeholders(history)
+ } else {
+ history.clone()
+ };
+
+ // Build the sub-agent's context middleware from the live `[context]` config +
+ // the agent's TokenJuice profile (#4466), matching how the chat path wires
+ // `TurnContextMiddleware` (session/turn/core.rs). The migrated sub-agent path
+ // had regressed to `TurnContextMiddleware::defaults()` — compression Off — so
+ // sub-agent tool outputs took a blunt 16 KiB truncation instead of the
+ // content-aware TokenJuice compaction the definition asked for. Honor the
+ // `[context]` enabled / autocompact opt-outs, microcompact keep-recent, and
+ // per-result byte budget too, so a sub-agent turn compacts like a chat turn.
+ let context_mw = build_subagent_context_mw(tokenjuice_compression, config);
+
+ // Live transcript snapshot sink (#4466): the harness owns the working message
+ // vector and drops it on a mid-run `Err`, so a failed sub-agent run used to
+ // persist NOTHING (breaking `learning/transcript_ingest`) and leave an empty
+ // worker thread. Attach a snapshot middleware that mirrors each `before_model`
+ // request's transcript here, so the error path below can still persist the
+ // rounds that completed before the failure.
+ let transcript_snapshot: crate::agent::tinyagents::TranscriptSnapshotSink =
+ std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
+
+ // A sub-agent turn runs *nested inside* the parent agent's turn (parent
+ // harness → spawn_subagent tool → here), so the child's full
+ // `run_turn_via_tinyagents_shared` future would otherwise sit on the parent's
+ // poll stack. Heap-allocate it (as the legacy `run_inner_loop` did) so the
+ // parent+child harness drives don't overflow the stack.
+ // Native-tool support drives the durable-history suffix dispatcher; capture it
+ // (and the telemetry id) before `turn_models` is moved into the runner.
+ let native_tools = turn_models.native_tools();
+ let provider_id = turn_models.provider_id().to_string();
+ let run_result = Box::pin(run_turn_via_tinyagents_shared(
+ turn_models,
+ provider_id,
+ model,
+ dispatch_history,
+ // Dynamic (per-spawn) tools first so a dynamic tool that intentionally
+ // shadows a parent-registry tool of the same name is the one that
+ // *executes* — matching the advertisement order (`dedup_tool_specs_by_name`
+ // lists dynamic specs before parent specs in `runner.rs`). The shared
+ // adapter resolves a name by scanning the sets in order, so a
+ // parent-first order would run the parent impl for a shadowed name.
+ vec![Arc::new(dynamic_tools), parent_tools],
+ // Fail-closed (issue #4452): a sub-agent ALWAYS carries a concrete,
+ // resolved allowlist (`allowed_names`), so pass it as `Some(..)`. An empty
+ // set is therefore a genuine deny-all — a tool-less agent
+ // (`ToolScope::Named([])`), a zero-match `skill_filter`, or a `named` list
+ // that resolved to nothing registers ZERO tools instead of implicitly
+ // inheriting the parent's full surface (shell/file-write/spawn).
+ Some(allowed_names),
+ max_iterations,
+ // Parent's progress sink — child events ride it, scoped below.
+ on_progress,
+ subagent_scope,
+ // Resolved above — drives the sub-agent context-window summarization step.
+ context_window,
+ // Mid-flight steering: forward queued steer messages into the run.
+ run_queue,
+ // Pause + checkpoint when the child asks the user a clarifying question.
+ &["ask_user_clarification"],
+ // Pause gracefully at the model-call cap so we can summarize a resumable
+ // checkpoint (below) instead of erroring — legacy cap-summary parity.
+ true,
+ // Bound the sub-agent's per-call output at its configured budget.
+ Some(max_output_tokens),
+ // Context middlewares (#4466): config-sourced TokenJuice compaction +
+ // tool-result byte cap + microcompact + summarization opt-outs (built
+ // above), plus the progressive-disclosure handoff when a cache is
+ // attached, plus the live transcript-snapshot sink for error recovery.
+ {
+ let mut mw = context_mw;
+ if let Some(cache) = handoff_cache {
+ mw.handoff = Some(crate::agent::tinyagents::HandoffConfig {
+ cache,
+ agent_id: agent_id.to_string(),
+ task_id: task_id.to_string(),
+ });
+ }
+ mw.transcript_snapshot = Some(transcript_snapshot.clone());
+ mw
+ },
+ // Sub-agents gate via their own SubagentToolSource policy path, not the
+ // session `.tool_policy()`; no enforcement threaded here.
+ None,
+ // Isolated worker descriptor, when worktree isolation prepared one.
+ workspace_descriptor,
+ // Sub-agent turns run tools with external effects; not a deterministic
+ // internal run, so response caching stays off (safe default).
+ false,
+ // #4457 (defect C): irrelevant for sub-agents — they carry a
+ // `subagent_scope`, so the seam never emits a top-level `TurnCompleted`
+ // (they report via `Subagent*` events). Pass `false` for clarity.
+ false,
+ ))
+ .await;
+
+ let mut outcome = match run_result {
+ Ok(outcome) => outcome,
+ Err(err) => {
+ // #4466: the harness dropped its partial transcript, but the snapshot
+ // middleware mirrored every completed round. Persist those rounds to
+ // `session_raw` (so `learning/transcript_ingest` can still read a
+ // failed run) and mirror them onto the worker thread, THEN surface the
+ // error. Previously the `?`-return skipped both persistence steps, so
+ // a failed run left no transcript and an empty worker thread.
+ let mapped = map_tinyagents_subagent_error(err);
+ let recovered = transcript_snapshot
+ .lock()
+ .map(|g| g.clone())
+ .unwrap_or_default();
+ tracing::warn!(
+ agent_id,
+ task_id,
+ error = %mapped,
+ recovered_rounds = recovered.len(),
+ "[subagent_runner:graph] sub-agent run errored; persisting recovered transcript before returning (#4466)"
+ );
+ super::transcript::persist_failed_run(
+ &workspace_dir,
+ transcript_stem,
+ agent_id,
+ task_id,
+ provider_label,
+ model,
+ &recovered,
+ context_window.unwrap_or(0),
+ if native_tools { "native" } else { "xml" },
+ worker_thread_id.as_deref(),
+ &mapped,
+ );
+ return Err(mapped);
+ }
+ };
+
+ // Write the final conversation back so the caller can checkpoint / persist.
+ // Keep the original (un-expanded) prior turns and append only this turn's typed
+ // suffix, serialized with the matching dispatcher so a native tool round
+ // persists as the `{content, tool_calls}` / `{tool_call_id, content}` envelope
+ // (re-parsed by `convert::chat_message_to_message` next turn) instead of an
+ // assistant with no `tool_calls` followed by an orphan `tool` row. Appending
+ // the typed `outcome.conversation` (messages-since-last-user) also avoids
+ // indexing a post-trim `outcome.history` with the pre-trim length, and the
+ // durable `[IMAGE:…]` markers stay put since the prior user turns are untouched.
+ use crate::agent::dispatcher::ToolDispatcher;
+ let suffix = if native_tools {
+ crate::agent::dispatcher::NativeToolDispatcher.to_provider_messages(&outcome.conversation)
+ } else {
+ crate::agent::dispatcher::XmlToolDispatcher.to_provider_messages(&outcome.conversation)
+ };
+ history.extend(suffix);
+
+ let mut usage = AggregatedUsage {
+ input_tokens: outcome.input_tokens,
+ output_tokens: outcome.output_tokens,
+ // Carry the child's cached-prefix tokens + estimated cost (the turn
+ // outcome now reports both) so sub-agent spend rolls into the parent
+ // instead of being recorded as uncached and $0.
+ cached_input_tokens: outcome.cached_input_tokens,
+ charged_amount_usd: outcome.charged_amount_usd,
+ };
+
+ // Cap hit with work still pending: summarize the run-so-far into a resumable
+ // checkpoint (the delegating agent continues from partial progress) rather
+ // than surfacing an empty/partial answer — the legacy `SubagentCheckpoint`.
+ if outcome.hit_cap {
+ let digest = build_cap_digest(&outcome.conversation, &outcome.tool_outcomes);
+ let strategy = super::checkpoint::SubagentCheckpoint {
+ chat_model: summary_model.clone(),
+ agent_id: agent_id.to_string(),
+ // The checkpoint summary call's output cap. #4469 item 5: honour this
+ // sub-agent definition's own per-call output budget (the same
+ // `max_output_tokens` bounding every task model call above) instead of
+ // the process-global `AGENT_TURN_MAX_OUTPUT_TOKENS` floor, so a
+ // definition that raised or lowered its output cap is respected by the
+ // cap-summary call too.
+ max_output_tokens,
+ };
+ match strategy.summarize_cap_hit(&digest, max_iterations).await {
+ Ok(co) => {
+ if let Some(u) = co.usage {
+ // Fold ALL four token fields (the legacy cap-summary folded
+ // cached tokens too, not just input/output), then price the
+ // call and feed the global cost tracker directly (#4467,
+ // item 2). The checkpoint summary call bypasses the harness so
+ // the observability bridge never sees it — without this record
+ // its cached tokens are lost and it costs $0 in the footer /
+ // transcript meta / cost dashboard.
+ usage.input_tokens += u.input_tokens;
+ usage.output_tokens += u.output_tokens;
+ usage.cached_input_tokens += u.cached_input_tokens;
+ let call_cost =
+ if u.charged_amount_usd.is_finite() && u.charged_amount_usd > 0.0 {
+ u.charged_amount_usd
+ } else {
+ crate::platform::cost::catalog::estimate_cost_usd(
+ model,
+ u.input_tokens,
+ u.output_tokens,
+ u.cached_input_tokens,
+ )
+ };
+ usage.charged_amount_usd += call_cost;
+ crate::platform::cost::record_provider_usage(
+ model,
+ &crate::inference::provider::UsageInfo {
+ input_tokens: u.input_tokens,
+ output_tokens: u.output_tokens,
+ context_window: u.context_window,
+ cached_input_tokens: u.cached_input_tokens,
+ cache_creation_tokens: u.cache_creation_tokens,
+ reasoning_tokens: u.reasoning_tokens,
+ charged_amount_usd: call_cost,
+ },
+ );
+ tracing::debug!(
+ agent_id,
+ input_tokens = u.input_tokens,
+ output_tokens = u.output_tokens,
+ cached_input_tokens = u.cached_input_tokens,
+ call_cost,
+ "[subagent] cap-hit summary call folded + priced + recorded into cost tracker (#4467, item 2)"
+ );
+ }
+ outcome.text = co.text;
+ }
+ Err(e) => return Err(SubagentRunError::Provider(e)),
+ }
+ }
+
+ // Persist the sub-agent's raw transcript to `session_raw` (parity with the
+ // removed `SubagentObserver::persist_transcript`). The graph runner replaced
+ // the observer but only mirrored to the worker thread, so per-child
+ // transcripts stopped being written — breaking downstream learning ingestion
+ // (`learning/transcript_ingest`, which reads `session_raw/*.jsonl`).
+ // On a cap-hit / early-exit, `outcome.text` is the checkpoint (or clarifying
+ // question) that stands in for a final assistant turn — append it so the
+ // persisted transcript reflects the actual final state, not the pre-checkpoint
+ // history. `history` already carries this turn's typed suffix.
+ let transcript_history;
+ let history_for_transcript: &[ChatMessage] = if (outcome.hit_cap
+ || outcome.early_exit_tool.is_some())
+ && !outcome.text.trim().is_empty()
+ {
+ transcript_history = {
+ let mut messages = history.clone();
+ messages.push(ChatMessage::assistant(outcome.text.clone()));
+ messages
+ };
+ &transcript_history
+ } else {
+ history.as_slice()
+ };
+ persist_subagent_transcript(
+ &workspace_dir,
+ transcript_stem,
+ agent_id,
+ task_id,
+ provider_label,
+ model,
+ history_for_transcript,
+ &usage,
+ context_window.unwrap_or(0),
+ // Match the dispatcher the history was actually serialized with (text-mode
+ // integrations turns write XML), and the real iteration count.
+ if native_tools { "native" } else { "xml" },
+ outcome.model_calls as u32,
+ );
+
+ // Mirror this turn's conversation to the spawn's worker thread (when one is
+ // attached), matching the legacy `SubagentObserver`: assistant intents +
+ // final answer as `agent` messages, tool results as `user` messages. The
+ // initial user prompt was already written when the worker thread was created.
+ if let Some(thread_id) = worker_thread_id {
+ mirror_worker_thread(
+ &workspace_dir,
+ &thread_id,
+ agent_id,
+ task_id,
+ &outcome.conversation,
+ // On a cap/early-exit, `outcome.text` is the checkpoint/question that
+ // replaced (or stands in for) a final assistant turn.
+ if outcome.hit_cap || outcome.early_exit_tool.is_some() {
+ Some(outcome.text.as_str())
+ } else {
+ None
+ },
+ );
+ }
+
+ // On an early-exit (`ask_user_clarification`), `outcome.text` is the question
+ // and the runner checkpoints + returns AwaitingUser. `None` = ran to a final
+ // answer (or a cap-hit checkpoint summary).
+ Ok((
+ outcome.text,
+ outcome.model_calls,
+ usage,
+ outcome.early_exit_tool,
+ outcome.hit_cap,
+ // #4466: propagate a circuit-breaker halt so the runner reports Incomplete.
+ outcome.breaker_halt,
+ ))
+}
+
+/// Build the sub-agent turn's [`TurnContextMiddleware`] from the live
+/// `[context]` config and the agent's TokenJuice profile (#4466), mirroring the
+/// chat path (`session/turn/core.rs`). Falls back to
+/// [`TurnContextMiddleware::defaults`] when the config can't be loaded so a
+/// config glitch degrades to the safe (byte-cap-only) behavior rather than
+/// erroring the run.
+fn build_subagent_context_mw(
+ tokenjuice_compression: AgentTokenjuiceCompression,
+ config: Option<&crate::config::Config>,
+) -> crate::agent::tinyagents::TurnContextMiddleware {
+ let mut mw = crate::agent::tinyagents::TurnContextMiddleware::defaults();
+ // Always thread the agent's compression profile — even on the config-default
+ // path — so the definition's TokenJuice choice is honored.
+ mw.tokenjuice_compression = tokenjuice_compression;
+ match config {
+ Some(config) => {
+ let ctx = &config.context;
+ // TokenJuice content-aware compaction gates on the same master
+ // `[context].compaction_enabled` the chat path reads
+ // (`ContextManager::compaction_enabled`).
+ mw.tokenjuice_compaction_enabled = ctx.compaction_enabled;
+ mw.tool_result_budget_bytes = ctx.tool_result_budget_bytes;
+ // Microcompact keep-recent is `0` (disabled) unless microcompact is on.
+ mw.microcompact_keep_recent = if ctx.microcompact_enabled {
+ ctx.microcompact_keep_recent
+ } else {
+ 0
+ };
+ // Summarization step honors the `[context].enabled` + autocompact
+ // opt-outs, same as `ContextManager::autocompact_enabled`.
+ mw.autocompact_enabled = ctx.enabled && ctx.autocompact_enabled;
+ tracing::debug!(
+ tokenjuice_compaction_enabled = mw.tokenjuice_compaction_enabled,
+ compression = ?mw.tokenjuice_compression,
+ tool_result_budget_bytes = mw.tool_result_budget_bytes,
+ microcompact_keep_recent = mw.microcompact_keep_recent,
+ autocompact_enabled = mw.autocompact_enabled,
+ "[subagent_runner:graph] built sub-agent context middleware from config (#4466)"
+ );
+ }
+ None => {
+ tracing::debug!(
+ "[subagent_runner:graph] no config available building sub-agent context mw; using defaults + compression profile"
+ );
+ }
+ }
+ mw
+}
+
+fn map_tinyagents_subagent_error(err: anyhow::Error) -> SubagentRunError {
+ match err.downcast::<SubagentRunError>() {
+ Ok(run_err) => run_err,
+ Err(err) => SubagentRunError::Provider(err),
+ }
+}
+
+/// Build the `tool → outcome` digest the cap-hit summary call summarizes, in the
+/// legacy `- {name} [{ok|failed}]: {output}` format (engine `run_tool_digest`),
+/// pairing each tool result back to its call by id. Per-tool success is derived
+/// from the turn's captured [`ToolCallOutcome`]s (#4467, item 7) rather than
+/// reported optimistically as `ok`: a result whose call has no captured outcome
+/// — e.g. a hallucinated/unknown tool the crate recovered without running
+/// `after_tool` — is marked `failed`, so the summary no longer tells the model
+/// every call succeeded.
+fn build_cap_digest(
+ conversation: &[ConversationMessage],
+ tool_outcomes: &[crate::agent::tinyagents::ToolCallOutcome],
+) -> String {
+ use std::collections::HashMap;
+ use std::fmt::Write as _;
+
+ // call_id -> tool name, from this turn's assistant tool-call rounds.
+ let mut names: HashMap<&str, &str> = HashMap::new();
+ for msg in conversation {
+ if let ConversationMessage::AssistantToolCalls { tool_calls, .. } = msg {
+ for call in tool_calls {
+ names.insert(call.id.as_str(), call.name.as_str());
+ }
+ }
+ }
+
+ // call_id -> success, from the captured per-call outcomes.
+ let success_by_id: HashMap<&str, bool> = tool_outcomes
+ .iter()
+ .map(|o| (o.call_id.as_str(), o.success))
+ .collect();
+
+ let mut out = String::new();
+ for msg in conversation {
+ if let ConversationMessage::ToolResults(results) = msg {
+ for r in results {
+ let name = names
+ .get(r.tool_call_id.as_str())
+ .copied()
+ .unwrap_or("tool");
+ // Missing outcome → `false` (unknown/hallucinated tool): honest
+ // failed status rather than an optimistic `[ok]`.
+ let ok = success_by_id
+ .get(r.tool_call_id.as_str())
+ .copied()
+ .unwrap_or(false);
+ let tag = if ok { "ok" } else { "failed" };
+ let body = crate::util::truncate_with_ellipsis(&r.content, 800);
+ let _ = writeln!(out, "- {name} [{tag}]: {body}");
+ }
+ }
+ }
+ out.trim_end().to_string()
+}
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs
new file mode 100644
index 0000000000..abace3aaa4
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs
@@ -0,0 +1,128 @@
+//! Resolving a durable `subagent_session_id` (or a transient `task_id`) back
+//! to the live registry entry or, failing that, the durable task-store record
+//! — used by tool/control paths that only know the stable session id a
+//! sub-agent is addressed by.
+
+use std::path::Path;
+
+use tinyagents_harness::ids::TaskId;
+
+use crate::agent::tinyagents::orchestration::OrchestrationTaskRecord;
+
+use super::registry::{registry, SubagentResumeRef};
+use super::task_ledger::{
+ list_task_records, record_agent_id, record_parent_session, record_subagent_session_id,
+ task_record_for_task_in_workspace,
+};
+use super::wait::WaitError;
+
+/// Resolve a durable `subagent_session_id` to the currently-running transient
+/// `task_id`, enforcing parent-session ownership.
+pub(crate) fn task_id_for_session(
+ subagent_session_id: &str,
+ parent_session: &str,
+) -> Result<String, WaitError> {
+ let mut saw_unowned = false;
+ let mut owned_terminal: Option<String> = None;
+ for snapshot in registry()
+ .snapshots(None)
+ .expect("detached task registry lock poisoned")
+ .into_iter()
+ .filter(|snapshot| {
+ snapshot.metadata.subagent_session_id.as_deref() == Some(subagent_session_id)
+ })
+ {
+ if snapshot.owner_id != parent_session {
+ saw_unowned = true;
+ continue;
+ }
+ let task_id = snapshot.task_id.as_str().to_string();
+ if !snapshot.status.is_terminal() {
+ return Ok(task_id);
+ }
+ owned_terminal.get_or_insert(task_id);
+ }
+ if let Some(task_id) = owned_terminal {
+ return Ok(task_id);
+ }
+ if saw_unowned {
+ return Err(WaitError::NotOwned);
+ }
+ Err(WaitError::Unknown)
+}
+
+pub(crate) fn task_id_for_session_in_workspace(
+ subagent_session_id: &str,
+ parent_session: &str,
+ workspace_dir: &Path,
+) -> Result<String, WaitError> {
+ match task_id_for_session(subagent_session_id, parent_session) {
+ Ok(task_id) => return Ok(task_id),
+ Err(WaitError::NotOwned) => return Err(WaitError::NotOwned),
+ Err(WaitError::Unknown) => {}
+ }
+
+ let mut saw_unowned = false;
+ let mut matches: Vec<OrchestrationTaskRecord> = list_task_records(workspace_dir)
+ .into_iter()
+ .filter(|record| record_subagent_session_id(record) == Some(subagent_session_id))
+ .collect();
+ matches.sort_by_key(|item| std::cmp::Reverse(item.updated_at));
+
+ for record in matches {
+ if record_parent_session(&record) != Some(parent_session) {
+ saw_unowned = true;
+ continue;
+ }
+ let task_id = record.spec.task_id.as_str().to_string();
+ log::debug!(
+ "[running_subagents] resolved session from task store subagent_session_id={} task_id={} workspace_dir={}",
+ subagent_session_id,
+ task_id,
+ workspace_dir.display()
+ );
+ return Ok(task_id);
+ }
+ if saw_unowned {
+ return Err(WaitError::NotOwned);
+ }
+ Err(WaitError::Unknown)
+}
+
+pub(crate) fn resume_ref_for_task(
+ task_id: &str,
+ parent_session: &str,
+) -> Result<SubagentResumeRef, WaitError> {
+ let snapshot = registry()
+ .snapshot(&TaskId::new(task_id), parent_session)
+ .map_err(super::wait::wait_error_from_registry)?;
+ Ok(SubagentResumeRef {
+ task_id: task_id.to_string(),
+ agent_id: snapshot.metadata.agent_id,
+ subagent_session_id: snapshot.metadata.subagent_session_id,
+ })
+}
+
+pub(crate) fn resume_ref_for_task_in_workspace(
+ task_id: &str,
+ parent_session: &str,
+ workspace_dir: &Path,
+) -> Result<SubagentResumeRef, WaitError> {
+ match resume_ref_for_task(task_id, parent_session) {
+ Ok(reference) => return Ok(reference),
+ Err(WaitError::NotOwned) => return Err(WaitError::NotOwned),
+ Err(WaitError::Unknown) => {}
+ }
+
+ let record = task_record_for_task_in_workspace(workspace_dir, task_id, parent_session)?;
+ log::debug!(
+ "[running_subagents] resolved resume ref from task store task_id={} workspace_dir={}",
+ task_id,
+ workspace_dir.display()
+ );
+ Ok(SubagentResumeRef {
+ task_id: task_id.to_string(),
+ agent_id: record_agent_id(&record),
+ subagent_session_id: record_subagent_session_id(&record).map(ToOwned::to_owned),
+ })
+}
diff --git a/crates/openhuman-core/src/channels/bus.rs b/crates/openhuman-core/src/channels/bus.rs
index 60bda9d8ec..7e23b79d49 100644
--- a/crates/openhuman-core/src/channels/bus.rs
+++ b/crates/openhuman-core/src/channels/bus.rs
@@ -4,6 +4,31 @@
//! by the socket transport layer. It runs the agent inference loop via the web
//! channel provider and sends the reply back through the REST API.
+mod delivery;
+mod draft;
+mod filler;
+mod progressive_ui;
+mod streaming_state;
+mod subscriber;
+mod thinking;
+mod thread_id;
+
+pub use subscriber::ChannelInboundSubscriber;
+
+// Re-exported at module scope (rather than left as private `pub(super)`
+// items reached only through their owning submodule) so the `#[path]`
+// test modules below — which share this module's scope via `super::*` —
+// can see them exactly as they could when this was one unsplit file.
+pub(crate) use thread_id::{derive_inbound_client_id, derive_inbound_thread_id};
+use delivery::channel_message_body_with_idempotency;
+use draft::extract_message_id;
+use progressive_ui::{
+ channel_edits_unsupported, channel_supports_progressive_ui, classify_edit_failure,
+ edit_capability_key, mark_channel_edits_unsupported, EditFailure,
+};
+use streaming_state::StreamingState;
+use thinking::latest_thinking_snippet;
+
#[cfg(test)]
#[path = "bus_inbound_thread_id_tests_tests.rs"]
mod inbound_thread_id_tests;
@@ -15,5 +40,3 @@ mod tests;
#[cfg(any(test, debug_assertions))]
#[path = "bus_test_support_tests.rs"]
pub mod test_support;
-include!("bus_part_01.rs");
-include!("bus_part_02.rs");
diff --git a/crates/openhuman-core/src/inference/provider/ops/http_error/policy_rejection.rs b/crates/openhuman-core/src/inference/provider/ops/http_error/policy_rejection.rs
new file mode 100644
index 0000000000..a7c2cd0831
--- /dev/null
+++ b/crates/openhuman-core/src/inference/provider/ops/http_error/policy_rejection.rs
@@ -0,0 +1,248 @@
+//! Deterministic provider/config/policy rejection classification: custom
+//! OpenAI-compatible upstream envelopes, provider access-policy denials,
+//! backend-owned error codes, external content-moderation rejections, and
+//! provider configuration rejections (unknown model, bad temperature, …).
+
+use super::*;
+
+/// Whether a custom OpenAI-compatible proxy returned the known generic
+/// upstream 400 envelope:
+/// `{"error":{"message":"Bad request to upstream provider","type":"upstream_error","status":400}}`.
+///
+/// This shape is deterministic provider/user-state (endpoint-model mismatch,
+/// unsupported schema, provider-side validation) and does not provide
+/// actionable signal for OpenHuman Sentry triage.
+pub fn is_custom_openai_upstream_bad_request_http_400(
+ provider: &str,
+ status: reqwest::StatusCode,
+ body: &str,
+) -> bool {
+ if provider != "custom_openai" || status != reqwest::StatusCode::BAD_REQUEST {
+ return false;
+ }
+ let lower = body.to_ascii_lowercase();
+ lower.contains("bad request to upstream provider") && lower.contains("upstream_error")
+}
+
+pub fn log_custom_openai_upstream_bad_request_http_400(
+ operation: &str,
+ provider: &str,
+ model: Option<&str>,
+ status: reqwest::StatusCode,
+) {
+ tracing::info!(
+ domain = "llm_provider",
+ operation = operation,
+ provider = provider,
+ model = model.unwrap_or(""),
+ status = status.as_u16(),
+ failure = "non_2xx",
+ kind = "provider_user_state",
+ reason = "custom_openai_upstream_bad_request",
+ "[llm_provider] {operation} custom_openai upstream 400 — not reporting to Sentry"
+ );
+}
+
+/// Whether a provider non-2xx response is a deterministic provider-policy
+/// denial (not a product bug) that should be demoted from Sentry.
+///
+/// Canonical example: Kimi's coding endpoint rejects non-agent clients with
+/// HTTP 403 + `access_terminated_error` and a message like:
+/// "currently only available for Coding Agents …".
+pub fn is_provider_access_policy_denied_http_403(status: reqwest::StatusCode, body: &str) -> bool {
+ if status != reqwest::StatusCode::FORBIDDEN {
+ return false;
+ }
+ let lower = body.to_ascii_lowercase();
+ lower.contains("access_terminated_error")
+ || lower.contains("currently only available for coding agents")
+}
+
+pub fn log_provider_access_policy_denied_http_403(
+ operation: &str,
+ provider: &str,
+ model: Option<&str>,
+ status: reqwest::StatusCode,
+) {
+ tracing::info!(
+ domain = "llm_provider",
+ operation = operation,
+ provider = provider,
+ model = model.unwrap_or(""),
+ status = status.as_u16(),
+ failure = "non_2xx",
+ kind = "provider_access_policy",
+ "[llm_provider] {operation} provider access-policy 403 — not reporting to Sentry"
+ );
+}
+
+/// Whether this provider response carries a managed-backend `errorCode` (#870)
+/// that the backend already owns — so the FE must not double-report (F2/F4).
+///
+/// Gated on `provider == `[`openhuman_backend_model::PROVIDER_LABEL`]: an `errorCode`
+/// is only trustworthy on the **managed backend**. A BYO / direct-provider body
+/// that merely contains an `errorCode`-shaped field must NOT be treated as
+/// backend-owned (CodeRabbit) — those keep reaching Sentry via the status gate.
+///
+/// Returns `false` for a backend-flagged **malformed** `BAD_REQUEST`: that one
+/// `errorCode` case is a client-built payload the backend couldn't parse, and
+/// the FE *does* page for it (F8). Delegates to the single-source decision in
+/// [`crate::inference::provider::backend_error_code_skips_sentry`]
+/// so the provider layer, the higher-layer re-report classifier, and the
+/// Sentry `before_send` filter can't drift.
+pub fn is_backend_error_code_owned(provider: &str, body: &str) -> bool {
+ provider == openhuman_backend_model::PROVIDER_LABEL
+ && crate::inference::provider::backend_error_code_skips_sentry(body)
+}
+
+pub fn log_backend_error_code_owned(
+ operation: &str,
+ provider: &str,
+ model: Option<&str>,
+ status: reqwest::StatusCode,
+ body: &str,
+) {
+ let code = crate::inference::provider::extract_backend_error_code_token(body)
+ .unwrap_or_default();
+ tracing::info!(
+ domain = "llm_provider",
+ operation = operation,
+ provider = provider,
+ model = model.unwrap_or(""),
+ status = status.as_u16(),
+ failure = "non_2xx",
+ kind = "backend_error_code",
+ error_code = %code,
+ "[llm_provider] {operation} backend errorCode={code} ({status}) — backend owns \
+ this error, not reporting to Sentry"
+ );
+}
+
+/// Whether a provider non-2xx response is an **external content-moderation
+/// rejection** — the user routes their provider (here: ollama) through a
+/// third-party safety/moderation proxy that refuses the prompt with a `400`
+/// and a verdict envelope, e.g.
+/// `{"error":"Message rejected by Ombudsman","score":80}` (TAURI-RUST-ECR,
+/// 4,517 events from a single looping triage-agent machine).
+///
+/// This is `genuinely-unpreventable` from OpenHuman's side: the request is
+/// well-formed, the rejection comes from an external guard we neither own nor
+/// configure, and there is no client lever to reshape the request into one the
+/// proxy will accept. The triage agent re-issues the same prompt every turn, so
+/// the raw 400 floods `report_error` (400 ∉ the transient set in
+/// [`super::dispatch::should_report_provider_http_failure`]). Demote to an
+/// info log while the error still propagates so retry/fallback runs
+/// unchanged — the same classify-and-backpressure answer as the 5MV / A3T /
+/// 8S3 precedent.
+///
+/// Anchored on the moderation-verdict shape — the rejection wording
+/// (`message rejected` / `ombudsman`) or the `"score"` verdict field — none of
+/// which OpenHuman's own backend or a normal provider 400 (malformed request,
+/// schema error) emits, so a genuine bug still reaches Sentry. Covered by a
+/// verbatim-body test so a proxy wording drift fails CI instead of silently
+/// leaking events.
+pub fn is_provider_moderation_rejection_http_400(status: reqwest::StatusCode, body: &str) -> bool {
+ if status != reqwest::StatusCode::BAD_REQUEST {
+ return false;
+ }
+ let lower = body.to_ascii_lowercase();
+ lower.contains("message rejected")
+ || lower.contains("ombudsman")
+ // The moderation proxy returns its confidence as a `"score"` JSON field
+ // alongside the verdict; anchor on the quoted key shape (not a bare
+ // `score`) so an unrelated 400 mentioning the word in prose isn't
+ // swallowed.
+ || lower.contains("\"score\"")
+}
+
+pub fn log_provider_moderation_rejection(
+ operation: &str,
+ provider: &str,
+ model: Option<&str>,
+ status: reqwest::StatusCode,
+) {
+ tracing::info!(
+ domain = "llm_provider",
+ operation = operation,
+ provider = provider,
+ model = model.unwrap_or(""),
+ status = status.as_u16(),
+ failure = "non_2xx",
+ kind = "external_moderation_rejection",
+ "[llm_provider] {operation} external content-moderation rejection ({status}) — request \
+ refused by a third-party moderation proxy (no client lever), not reporting to Sentry"
+ );
+}
+
+/// Whether a provider non-2xx response is a deterministic
+/// **configuration-rejection** user-state error (unknown model id,
+/// abstract tier leaked to a custom provider, model-specific temperature
+/// constraint) that should be demoted from Sentry to an info log.
+///
+/// Provider-aware (inverted polarity vs. the 401/403 backend rule): for
+/// most config-rejection phrases the same body from the OpenHuman
+/// **backend** stays Sentry-actionable — that would mean we sent our own
+/// backend a bad request (a regression, e.g. #2079). Restricted to the
+/// observed shapes (400 invalid-param / unknown-model, 404
+/// model-does-not-exist, 422 unprocessable); 408/429 are transient and
+/// handled separately.
+///
+/// **Exception: OpenAI-compatible "unknown model"** (`Model 'X' is not
+/// available. Use GET /openai/v1/models …`). The OpenHuman backend now
+/// emits this exact body for user-configured unknown model ids, so it is
+/// user-state regardless of provider — the polarity guard is dropped for
+/// this specific shape (TAURI-RUST-2Z1). See
+/// [`super::is_openai_compatible_unknown_model_message`].
+pub fn is_provider_config_rejection_http(
+ status: reqwest::StatusCode,
+ provider: &str,
+ body: &str,
+) -> bool {
+ // 403 is included for the Ollama Cloud subscription gate:
+ // `{"error":"this model requires a subscription, upgrade for access: …"}`.
+ // That is deterministic user-state (paid-tier model, free account) — the
+ // same class as the 400/404/422 config-rejection shapes above. See
+ // TAURI-RUST-4XK. The general `is_backend_auth_failure` polarity guard
+ // still fires first (backend 401/403 → SessionExpired), so this branch
+ // is only reachable for non-backend providers. The phrase-level polarity
+ // guard below (`provider != openhuman_backend_model::PROVIDER_LABEL`) provides
+ // a second layer of defence for the non-OpenAI-compat shapes.
+ if !matches!(status.as_u16(), 400 | 403 | 404 | 422) {
+ return false;
+ }
+ if !crate::inference::provider::is_provider_config_rejection_message(body) {
+ return false;
+ }
+ // OpenAI-compatible "unknown model" body is user-state regardless of
+ // provider — both third-party `custom_openai` upstreams and our own
+ // OpenHuman backend now emit it for user-configured model ids that
+ // aren't in the registry (TAURI-RUST-2Z1).
+ if crate::inference::provider::is_openai_compatible_unknown_model_message(body) {
+ return true;
+ }
+ // Remaining config-rejection phrases (DeepSeek `supported api model
+ // names are`, Moonshot `invalid temperature`, litellm envelopes, …)
+ // are intrinsically scoped to third-party providers — keep the
+ // polarity guard so a regression where our own backend emits one of
+ // those still reaches Sentry.
+ provider != openhuman_backend_model::PROVIDER_LABEL
+}
+
+pub fn log_provider_config_rejection(
+ operation: &str,
+ provider: &str,
+ model: Option<&str>,
+ status: reqwest::StatusCode,
+) {
+ tracing::info!(
+ domain = "llm_provider",
+ operation = operation,
+ provider = provider,
+ model = model.unwrap_or(""),
+ status = status.as_u16(),
+ failure = "non_2xx",
+ kind = "provider_config_rejection",
+ "[llm_provider] {operation} provider config-rejection ({status}) — \
+ user model/param configuration, not reporting to Sentry"
+ );
+}