Skip to content

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

14.3 KB
Raw Blame
//! Single-arm execution: dispatch one `agent.run_turn` through the
//! native bus, parse the reply, and classify any failure so the
//! fallback chain can decide what to do next.

use std::sync::Arc;
use std::time::Instant;

use anyhow::anyhow;

use crate::agent::bus::{AgentTurnRequest, AgentTurnResponse, AGENT_RUN_TURN_METHOD};
use crate::agent_registry::host_definition::AgentDefinitionRegistry;
use crate::config::MultimodalConfig;
use crate::core::bus::BUS;
use tinyagents_harness::retry::{decide, FailoverDecision, FailoverReason, FailoverState};
use tinyagents_session::transcript::TranscriptMessage;
use tinybus::NativeRequestError;
use tinyinference_providers::{is_budget_message, BudgetMatch};

use super::super::decision::parse_triage_decision;
use super::super::envelope::TriggerEnvelope;
use super::super::routing::ResolvedProvider;
use super::outcome::{TriageResolutionPath, TriageRun};
use super::prompt::{extract_inline_prompt, format_parse_error, render_user_message};

/// Agent definition id for the built-in triage classifier.
pub const TRIGGER_TRIAGE_AGENT_ID: &str = "trigger_triage";

/// Single-arm execution result. `Retryable` lets the orchestrator
/// decide whether to sleep + retry on the same arm (cloud) or to fall
/// through (local). `Fatal` short-circuits the whole chain.
pub(crate) enum ArmError {
    /// 429 / 5xx / timeout / connection — the kind of failure where
    /// trying again later might help.
    Retryable {
        retry_after_ms: Option<u64>,
        source: anyhow::Error,
    },
    /// Auth failure, missing model, prompt parse error, registry
    /// missing, etc. — retry / fallback would not change the result.
    Fatal(anyhow::Error),
    /// Cloud upstream rejected the call because the user is out of
    /// budget / credits. Retrying the cloud arm would just burn the
    /// same wall, but the local arm has no upstream cost — so we
    /// skip cloud retry, try local, and defer if local also fails.
    /// This is **not** a fatal error: the user takes an explicit
    /// action (top up) to fix it, so it must not page Sentry.
    BudgetExhausted(anyhow::Error),
    /// Our prompt-injection guard (`agent::bus`'s `enforce_prompt_input`,
    /// also used by `agent::session_host::runtime`) flagged the
    /// incoming content as adversarial / unsafe and refused to dispatch
    /// the turn. The guard runs *before* either model is contacted, so
    /// trying the same prompt again — on cloud or local — produces the
    /// same verdict. This is **not** a fatal error: the guard is doing
    /// its job (OPENHUMAN-TAURI-X regression: an adversarial Gmail
    /// message reliably trips the guard, and every fire paged Sentry).
    /// Route the same way as `BudgetExhausted` so the local-arm fallthrough
    /// lands in `TriageOutcome::Deferred` rather than `Err(_)`.
    SafetyFlagged(anyhow::Error),
}

/// Run a single arm: dispatch the agent turn through the native bus
/// and parse the reply. Classifies any error so the caller can decide
/// what to do next.
pub(super) async fn try_arm(
    resolved: &ResolvedProvider,
    envelope: &TriggerEnvelope,
    intended_path: TriageResolutionPath,
) -> Result<TriageRun, ArmError> {
    let started = Instant::now();

    tracing::debug!(
        source = %envelope.source.slug(),
        label = %envelope.display_label,
        external_id = %envelope.external_id,
        provider = %resolved.provider_name,
        used_local = resolved.used_local,
        path = intended_path.as_str(),
        "[triage::evaluator] starting triage turn"
    );

    let registry = AgentDefinitionRegistry::current().ok_or_else(|| {
        ArmError::Fatal(anyhow!(
            "AgentDefinitionRegistry not initialised — did startup wiring \
             skip `init_global`?"
        ))
    })?;
    let definition = registry.get(TRIGGER_TRIAGE_AGENT_ID).ok_or_else(|| {
        ArmError::Fatal(anyhow!(
            "built-in `{TRIGGER_TRIAGE_AGENT_ID}` definition missing from registry"
        ))
    })?;

    let system_prompt = extract_inline_prompt(definition).ok_or_else(|| {
        ArmError::Fatal(anyhow!(
            "trigger_triage agent definition must ship an inline prompt body"
        ))
    })?;
    let user_message = render_user_message(envelope);
    let history = vec![
        TranscriptMessage::system(&system_prompt),
        TranscriptMessage::user(&user_message),
    ];

    let request = AgentTurnRequest {
        turn_model_source: resolved.turn_model_source.clone(),
        history,
        tools_registry: Arc::new(Vec::new()),
        provider_name: resolved.provider_name.clone(),
        model: resolved.model.clone(),
        temperature: definition.temperature,
        silent: true,
        channel_name: "triage".to_string(),
        multimodal: MultimodalConfig::default(),
        // Triage receives untrusted text from third-party channel
        // payloads (Slack/Telegram/Discord/WhatsApp). Disable
        // file-marker resolution outright so an attacker can't smuggle
        // `[FILE:/etc/passwd]` (or any other local-path marker) into
        // an inbound message and have triage exfiltrate the contents
        // into an LLM call. The hardened constructor sets max_files=0,
        // which TinyAgents' media request wrapper checks before its host
        // resolver can read anything. The same constructor is used at the
        // main channel-dispatch site in `channels::runtime::dispatch`.
        multimodal_files: crate::config::MultimodalFileConfig::for_untrusted_channel_input(),
        max_tool_iterations: 1,
        on_delta: None,
        target_agent_id: Some("trigger_triage".to_string()),
        visible_tool_names: None,
        extra_tools: Vec::new(),
        on_progress: None,
        // Triage processes untrusted inbound channel text. Label it as
        // ExternalChannel so the approval gate treats any external_effect
        // tool call originating from this turn as remote-attacker input
        // (the triage agent doesn't usually invoke such tools — it
        // classifies and routes — but label correctly for defense in depth).
        origin: crate::security::turn_origin::AgentTurnOrigin::ExternalChannel {
            sender_name: None,
            channel: envelope.source.slug().to_string(),
            // Triage runs over an upstream envelope (composio / webhook /
            // cron / external caller) that doesn't carry a per-user sender
            // at this layer. Leave it unset and let the gate apply the
            // strict per-channel TTL-deny default.
            sender: None,
            reply_target: envelope.display_label.clone(),
            message_id: envelope.external_id.clone(),
            history_key: None,
            channel_session: None,
        },
        // Triage is not an interactive chat turn: no approval surface.
        approval: Default::default(),
    };

    let response = match BUS
        .native()
        .request::<AgentTurnRequest, AgentTurnResponse>(AGENT_RUN_TURN_METHOD, request)
        .await
    {
        Ok(r) => r,
        Err(err) => {
            let message = match &err {
                NativeRequestError::HandlerFailed { message, .. } => message.clone(),
                other => format!("[agent.run_turn dispatch] {other}"),
            };
            tracing::warn!(
                error = %message,
                path = intended_path.as_str(),
                "[triage::evaluator] agent turn dispatch failed"
            );
            return Err(classify_error(message));
        }
    };

    let decision = match parse_triage_decision(&response.text) {
        Ok(d) => d,
        Err(parse_err) => {
            tracing::warn!(
                error = %parse_err,
                reply_chars = response.text.chars().count(),
                path = intended_path.as_str(),
                "[triage::evaluator] classifier reply did not parse"
            );
            // A parse failure means the model produced unusable
            // output. Retrying the same arm with the same prompt
            // won't usually help, but on the cloud arm one retry is
            // cheap enough because hosted models can be
            // non-deterministic across calls. If the cloud retry also
            // returns malformed output, let the outer chain fall
            // through to local/Deferred instead of surfacing Err to
            // background callers like Composio trigger triage.
            return Err(match intended_path {
                TriageResolutionPath::Cloud | TriageResolutionPath::CloudAfterRetry => {
                    ArmError::Retryable {
                        retry_after_ms: None,
                        source: anyhow!(
                            "classifier reply did not parse on {} arm: {}",
                            intended_path.as_str(),
                            format_parse_error(&parse_err)
                        ),
                    }
                }
                TriageResolutionPath::LocalFallback => ArmError::Fatal(anyhow!(
                    "classifier reply did not parse on {} arm: {}",
                    intended_path.as_str(),
                    format_parse_error(&parse_err)
                )),
            });
        }
    };

    let latency_ms = started.elapsed().as_millis() as u64;
    let used_local = matches!(intended_path, TriageResolutionPath::LocalFallback);
    tracing::info!(
        source = %envelope.source.slug(),
        action = %decision.action.as_str(),
        path = intended_path.as_str(),
        latency_ms = latency_ms,
        "[triage::evaluator] classifier decision produced"
    );

    Ok(TriageRun {
        decision,
        used_local,
        latency_ms,
        resolution_path: intended_path,
    })
}

/// Classify a handler-failure message string from the agent bus into
/// either a retryable (sleep + try again) or fatal (give up) error.
pub(crate) fn classify_error(message: String) -> ArmError {
    let err = anyhow!("{message}");
    // Budget-exceeded is technically a 400 (not 5xx/429), so the
    // generic owner classifier below won't catch it — but it is a
    // user-actionable upstream blocker, not a code bug, so we route
    // it through `BudgetExhausted` to avoid Sentry pages.
    if is_inference_budget_exceeded(&message) {
        return ArmError::BudgetExhausted(err);
    }
    // Prompt-guard rejection (`agent::bus::enforce_prompt_input` →
    // `ReviewBlocked` / `Blocked`). The guard fires *before* either
    // arm contacts a model, so the verdict is identical on cloud and
    // local — no point retrying. Treat as Deferred-eligible so the
    // chain ends in `TriageOutcome::Deferred` rather than Fatal,
    // which was paging Sentry for adversarial-email triage attempts
    // (OPENHUMAN-TAURI-X regression).
    if is_prompt_guard_rejection(&message) {
        return ArmError::SafetyFlagged(err);
    }
    let failure =
        tinyinference_llm::failure::classify_provider_failure_with(None, None, &message, |value| {
            crate::core::observability::is_session_expired_message(value)
                || tinyinference_llm::failure::is_context_window_exceeded_message(value)
                || tinyinference_llm::failure::body_indicates_quota_exhausted(value)
        });
    // Keep the harness's detailed failover reason mapping while using the
    // provider-neutral classifier to distinguish retryable and exhausted
    // rate limits and preserve terminal account/context failures.
    let reason = FailoverReason::classify_unstructured(&message);
    let decision = decide(
        reason,
        FailoverState {
            retryable: failure.is_retryable() && reason != FailoverReason::Unknown,
            attempts_remaining: true,
            larger_window_available: false,
        },
    );
    match decision {
        FailoverDecision::RetrySame => ArmError::Retryable {
            retry_after_ms: (reason == FailoverReason::RateLimit)
                .then(|| {
                    tinyinference_llm::failure::parse_retry_after_ms(&message).or_else(|| {
                        message.lines().find_map(|line| {
                            line.trim()
                                .strip_prefix("Retry-After:")
                                .or_else(|| line.trim().strip_prefix("retry-after:"))
                                .and_then(|value| {
                                    tinyinference_core::retry_after::parse_retry_after_ms(Some(
                                        value,
                                    ))
                                })
                        })
                    })
                })
                .flatten(),
            source: err,
        },
        FailoverDecision::Fallback | FailoverDecision::Surface => ArmError::Fatal(err),
    }
}

/// Returns `true` when `message` is the verbatim string our prompt-injection
/// guard returns when it rejects a turn before dispatch.
///
/// Canonical sources:
/// - `crates/openhuman-core/src/agent/bus.rs` — `Blocked` / `ReviewBlocked` arms of the
///   `enforce_prompt_input` decision (the path the triage evaluator hits via
///   `agent.run_turn`).
/// - `crates/openhuman-core/src/agent/session_host/runtime.rs` — same strings in the
///   tool-call loop, kept identical so this classifier covers both.
/// - `crates/openhuman-core/src/inference/local/ops.rs` — user-facing variants with the
///   `"Please rephrase clearly."` suffix; we match the leading phrase so
///   either form classifies.
///
/// Kept narrow on purpose: the guard's full output strings are private to
/// our code, so a substring match against the leading phrase will not collide
/// with anything coming back from upstream providers.
fn is_prompt_guard_rejection(message: &str) -> bool {
    let lower = message.to_ascii_lowercase();
    lower.contains("prompt flagged for security review")
        || lower.contains("prompt blocked by security policy")
}

/// Returns `true` when `message` signals that the upstream rejected the
/// call because the user's inference budget or credit balance is empty —
/// meaning a retry would hit the same wall.
///
/// Uses the shared matcher at [`BudgetMatch::Strict`]: whole-word needles only,
/// because a false positive would silently reclassify a real `Fatal` error as
/// `BudgetExhausted`, hiding it from Sentry. (`stop updating` must not read as
/// `top up`.) This is deliberately narrower than the chat surface's
/// `BudgetMatch::Managed`.
fn is_inference_budget_exceeded(message: &str) -> bool {
    is_budget_message(message, BudgetMatch::Strict)
}