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
feat(agent): add environment variable support for Langfuse tracing
Steven Enamakeld57ed2c97674
diff --git a/crates/openhuman-core/src/agent/progress_tracing/langfuse/environment.rs b/crates/openhuman-core/src/agent/progress_tracing/langfuse/environment.rsnew file mode 100644index 0000000000..0a9db5750b--- /dev/null+++ b/crates/openhuman-core/src/agent/progress_tracing/langfuse/environment.rs@@ -0,0 +1,149 @@+//! Backend-host resolution and the deployment-environment push gate for the+//! Langfuse exporter: where the ingestion proxy lives, which environment the+//! resolved host belongs to, and whether that environment may push at all.++use crate::api::config::effective_backend_api_url;+use crate::config::Config;++use super::LOG_TARGET;++/// Backend proxy route for Langfuse ingestion (relative to the backend origin).+/// The backend authenticates the caller's session JWT, injects the Langfuse+/// project keys, and forwards to Langfuse's real `/api/public/ingestion` — so+/// clients POST here, NOT to `/api/public/ingestion` (which is unexposed and+/// carries no keys).+const INGESTION_PATH: &str = "/telemetry/langfuse/ingestion";+/// Cap the push so a slow/hung Langfuse never stalls run teardown.+const PUSH_TIMEOUT: Duration = Duration::from_secs(10);++/// Resolve the Langfuse ingestion URL from the current backend host. Joins the+/// proxy path onto [`effective_backend_api_url`] — the exact base-server+/// resolution every other backend call uses — via the canonical+/// [`crate::api::config::api_url`] helper, which replaces any path the base+/// carried with the given absolute path. So the host always matches wherever the+/// app's domain calls go (staging, prod, or a custom `api_url` override).+pub(crate) fn ingestion_url(config: &Config) -> String {+ let base = effective_backend_api_url(&config.api_url);+ crate::api::config::api_url(&base, INGESTION_PATH)+}++/// Epoch-milliseconds → RFC 3339 / ISO-8601 string (Langfuse requires ISO++ }+ if let Ok(kind) = serde_json::to_value(span.kind) {+ map.insert("kind".to_string(), kind);+ }+ Value::Object(map)+}++/// The domain the deployed backends live under. A host outside it cannot be+/// one of ours, so it cannot be staging however it is spelled.+const DEPLOYMENT_DOMAIN: &str = "tinyhumans.ai";++/// Derive the Langfuse `environment` for a backend base URL. Chosen signal:+/// the resolved backend host is the single existing config-driven fact that+/// distinguishes deployments (there is no NODE_ENV-style flag in the core+/// config) — loopback/local → development, a staging host under+/// [`DEPLOYMENT_DOMAIN`] → staging, anything else → production.+///+/// # Why the host is parsed rather than substring-matched+///+/// This used to test `base.contains("staging")` and+/// `base.contains("localhost" | "127.0.0.1" | "0.0.0.0")` against the whole+/// URL, which was tolerable while the answer only labelled a payload. It is+/// not tolerable now that [`skip_push`] decides whether to push at all, so+/// both directions of the sloppiness became load-bearing:+///+/// - **Too broad on staging.** `https://staging-attacker.invalid` contains+/// `staging`, so it classified as staging and passed the push gate — a host+/// nothing in this tree owns, reached with a live session token. Anchoring+/// to [`DEPLOYMENT_DOMAIN`] is what makes the classifier fail *closed*: a+/// host that is not ours is production, and production does not push.+/// - **Too narrow on local.** An IPv6 loopback backend (`http://[::1]:7788`)+/// or a private LAN address matched none of the three literals and+/// classified as production, so a working development setup would have+/// silently stopped exporting the moment the gate landed.+///+/// Host classification is delegated to [`crate::api::config::host_is_local`],+/// which already parses the URL and handles IPv4 loopback/unspecified/private,+/// IPv6 loopback/unspecified, and `localhost` / `*.localhost`. Keeping one+/// definition matters more than the few lines it saves: two local-host+/// predicates that disagree is how a gate lets through exactly the case the+/// other one blocks.+///+/// An unparseable URL is production — the fail-closed default. `ingestion_url`+/// can return a non-URL placeholder when no backend host resolves, and the+/// caller checks `starts_with("http")` separately; classifying that as+/// anything pushable would defeat the gate.+pub(crate) fn environment_for_base(base: &str) -> &'static str {+ let Ok(parsed) = url::Url::parse(base) else {+ return "production";+ };+ if crate::api::config::host_is_local(&parsed) {+ return "development";+ }+ let Some(url::Host::Domain(host)) = parsed.host() else {+ // A public IP literal is not a deployment of ours.+ return "production";+ };+ let host = host.to_ascii_lowercase();+ let under_deployment_domain =+ host == DEPLOYMENT_DOMAIN || host.ends_with(&format!(".{DEPLOYMENT_DOMAIN}"));+ // The label test is on the leftmost label only, so `staging-api…` and+ // `staging…` match while `api-staging-mirror…` does not sneak in on a+ // substring.+ let leftmost_is_staging = host+ .split('.')+ .next()+ .is_some_and(|label| label == "staging" || label.starts_with("staging-"));+ if under_deployment_domain && leftmost_is_staging {+ "staging"+ } else {+ "production"+ }+}++/// The environments this client may push to Langfuse from.+///+/// An allowlist, not `!= "production"`, and it mirrors the backend's rule+/// (`backend:src/config/langfuseEnvironment.ts`) deliberately: the two gates+/// have to agree, and two negations drift more easily than two lists. Stating+/// the permitted set also makes the fail-closed property structural — if+/// [`environment_for_base`] ever grows a fourth bucket, that bucket does not+/// push until someone adds it here on purpose.+///+/// `test` appears in the backend's list but not here because there is no such+/// bucket on this side: [`environment_for_base`] maps loopback hosts to+/// `development`, and that is what the Rust suite resolves to.+const LANGFUSE_PUSH_ENVIRONMENTS: &[&str] = &["staging", "development"];++/// Whether a push is permitted for a resolved environment.+fn push_allowed(environment: &str) -> bool {+ LANGFUSE_PUSH_ENVIRONMENTS.contains(&environment)+}++/// Emitted at most once per process by [`skip_push`].+static SKIP_LOGGED: std::sync::Once = std::sync::Once::new();++/// Whether this push should be dropped before any work, logging the reason+/// once per process.+///+/// # Why skip at all, when the backend already refuses+///+/// Defence in depth, and latency. The backend answers `403 FEATURE_DISABLED`+/// outside staging (backend#1291), so nothing reaches Langfuse either way —+/// but a client that still asks pays a full authenticated round-trip on every+/// agent turn to be told no, and [`PUSH_TIMEOUT`] bounds that at ten seconds+/// when the host is slow. #5602 is that stall. The cheapest request is the one+/// not made.+///+/// # Why once per process, and at info+///+/// This is on the path of every completed run. A warning per turn would move+/// the noise rather than remove it, and a skip in production is the configured+/// outcome, not a fault — so it is `info`, said once, and then silence. The+/// caller receives `Ok(())`: skipping is a successful no-op, and returning+/// `Err` would make the caller log the same line on every turn, which is the+/// thing being avoided.+fn skip_push(environment: &str) -> bool {+ if push_allowed(environment) {