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
//! The load-bearing test here is [`turn_request_field_names_match_the_controller`].
//!
//! Everything else in this file checks the facade's own logic; that one checks
//! the thing that actually breaks in the field. `AgentChatParams` has no
//! `#[serde(rename_all)]`, so its wire names are Rust field names — an upstream
//! rename produces no compile error anywhere, and a hand-written `json!` keeps
//! sending the old key while the controller reads `None`. Pinning our field
//! names against the *registered schema* turns that into a test failure.
use super::*;
use serde_json::json;
/// Every key [`TurnRequest`] serializes must be a declared input of the
/// controller it is sent to.
#[test]
fn turn_request_field_names_match_the_controller() {
let schema = openhuman_core::core::all::all_registered_controllers()
.into_iter()
.find(|controller| {
controller.schema.namespace == "inference" && controller.schema.function == "agent_chat"
})
.expect("inference.agent_chat is a registered controller");
let schema = schema.schema;
let declared: std::collections::HashSet<&str> =
schema.inputs.iter().map(|field| field.name).collect();
// Fully populated so `skip_serializing_if` hides nothing.
let request = TurnRequest {
message: "hi".into(),
model_override: Some("m".into()),
temperature: Some(0.5),
thread_id: Some("t".into()),
cwd: Some("/tmp".into()),
inference_url: Some("https://example.invalid/v1".into()),
api_key: Some("k".into()),
inference_headers: vec![("x-worker".into(), "a".into())],
inference_provider_options: Some(serde_json::json!({"provider": {"sort": "latency"}})),
inference_reasoning_effort: Some("high".into()),
agent_id: Some("a".into()),
};
let serde_json::Value::Object(encoded) =
serde_json::to_value(&request).expect("TurnRequest encodes")
else {
panic!("TurnRequest must encode to an object");
};
for key in encoded.keys() {
assert!(
declared.contains(key.as_str()),
"TurnRequest sends `{key}`, which inference.agent_chat does not declare. \
Either the controller renamed a field or the facade invented one; \
declared inputs are {declared:?}"
);
}
// And the reverse: a param the controller declares but the facade cannot
// reach is a capability we silently dropped.
for field in &schema.inputs {
assert!(
encoded.contains_key(field.name),
"inference.agent_chat declares `{}`, which TurnRequest cannot send",
field.name
);
}
}
#[test]
fn turn_request_omits_absent_options() {
// `None` must not become `null`: the controller's `Option<String>` would
// accept it, but sending keys the caller never set makes the wire payload
// depend on facade internals rather than on what was asked for.
let encoded = serde_json::to_value(TurnRequest::new("hi")).expect("encodes");
assert_eq!(encoded, json!({ "message": "hi" }));
}
#[test]
fn typed_dispatch_refusal_precedes_structured_error_mapping_and_keeps_usage() {
let refusal =
crate::budget::DispatchRefusal::new(crate::budget::DispatchRefusalReason::CallLimit);
let error = anyhow::Error::new(refusal);
let mapped = map_agent_chat_error(
error,
Some(tinyagents_harness::middleware::ResponseUsage {
input_tokens: 13,
output_tokens: 5,
cached_tokens: 2,
reasoning_tokens: 1,
cost_usd: Some(0.004),
..Default::default()
}),
);
match mapped {
CoreError::DispatchAdmission { source, usage, .. } => {
assert!(CoreError::DispatchAdmission {
method: AGENT_CHAT,
source,
usage: None,
}
.is_expected_user_state());
assert_eq!(source, refusal);
let usage = usage.expect("the completed invalid response remains accounted");
assert_eq!(usage.input_tokens, 13);
assert_eq!(usage.output_tokens, 5);
assert_eq!(usage.cost_usd, Some(0.004));
}
other => panic!("expected typed dispatch refusal, got {other:?}"),
}
}
#[test]
fn a_route_sets_both_halves_or_neither() {
// The core ignores the route unless both arrive non-blank, so the type
// makes supplying one alone unrepresentable.
let turn_request = {
let mut r = TurnRequest::new("hi");
let route = Route::openai_compatible("https://example.invalid/v1", "sk-test");
r.inference_url = Some(route.base_url);
r.api_key = Some(route.api_key);
r
};
assert_eq!(
turn_request.inference_url.as_deref(),
Some("https://example.invalid/v1")
);
assert_eq!(turn_request.api_key.as_deref(), Some("sk-test"));
}
#[test]
fn blank_per_turn_route_is_rejected_before_dispatch() {
let mut request = TurnRequest::new("hi");
request.inference_url = Some("https://api.example/v1".into());
request.api_key = Some(" ".into());
assert!(matches!(
validate_route(&request),
Err(CoreError::InvalidRoute { method: AGENT_CHAT })
));
}
#[test]
fn absolute_leaves_absolute_paths_alone() {
let already = std::path::Path::new("/tmp/example");
assert_eq!(absolute(already).expect("absolute"), already);
}
#[test]
fn absolute_roots_a_relative_path_at_the_cwd() {
let resolved = absolute("sub/dir").expect("absolute");
assert!(resolved.is_absolute());
assert!(resolved.ends_with("sub/dir"));
}
#[test]
fn route_debug_redacts_bearer_and_url_embedded_credentials() {
let route = Route::openai_compatible(
"https://user:[email protected]/v1?key=leaky#frag",
"sk-bearer",
);
let debug = format!("{route:?}");
assert!(debug.contains("api.example"), "origin stays readable");
assert!(
!debug.contains("topsecret") && !debug.contains("sk-bearer") && !debug.contains("leaky"),
"credentials leaked into Route Debug: {debug}"
);
assert!(debug.contains("redacted"));
}
#[test]
fn is_safe_endpoint_for_bearer_accepts_tls_and_loopback() {
// TLS is always safe for a bearer.
assert!(is_safe_endpoint_for_bearer("https://api.example.com/v1"));
assert!(is_safe_endpoint_for_bearer("https://127.0.0.1:8443"));
// Cleartext is refused for remote hosts...
assert!(!is_safe_endpoint_for_bearer("http://api.example.com/v1"));
assert!(!is_safe_endpoint_for_bearer("http://192.168.1.10/v1"));
// ...but accepted for loopback, where the credential never leaves the host.
assert!(is_safe_endpoint_for_bearer("http://127.0.0.1:8080/v1"));
assert!(is_safe_endpoint_for_bearer("http://localhost:8080/v1"));
assert!(is_safe_endpoint_for_bearer("http://[::1]:8080/v1"));
assert!(is_safe_endpoint_for_bearer("http://127.0.0.2:8080/v1"));
// Unparseable or non-URL values are refused, not silently allowed, so a
// bearer can never ride a cleartext or ambiguous channel.
assert!(!is_safe_endpoint_for_bearer("api.example.com"));
assert!(!is_safe_endpoint_for_bearer("ftp://api.example.com"));
assert!(!is_safe_endpoint_for_bearer("http://api.example.com:8080"));
}
#[test]
fn insecure_route_error_names_the_redacted_endpoint() {
// The guard sanitizes the endpoint before it lands in the error, so the
// message names the origin but never the credential-bearing parts.
let endpoint = sanitize_url_for_display("http://user:[email protected]/v1?key=leaky");
let err = CoreError::InsecureRoute {
method: AGENT_CHAT,
endpoint,
};
let msg = err.to_string();
assert!(msg.contains("non-HTTPS"), "classifies the reason: {msg}");
assert!(!msg.contains("user"), "userinfo not leaked: {msg}");
assert!(
!msg.contains("leaky"),
"query credentials not leaked: {msg}"
);
assert!(msg.contains("api.example"), "origin stays readable: {msg}");
}
#[test]
fn sanitize_redacts_unparseable_urls() {
// A value that does not parse as an absolute URL cannot be proven free of
// credential-bearing components (e.g. protocol-relative userinfo), so it
// must never be echoed verbatim into diagnostics.
assert_eq!(
sanitize_url_for_display("//user:sk-secret@host/path"),
"<redacted>"
);
assert_eq!(sanitize_url_for_display("not a url at all"), "<redacted>");
// A parseable absolute URL is normalized, not dropped.
let normalized = sanitize_url_for_display("https://user:[email protected]/v1?k=leaky");
assert!(normalized.contains("api.example"));
assert!(!normalized.contains("sk-secret"));
assert!(!normalized.contains("leaky"));
}
/// `agent_chat` declares no input a seeded history could travel in.
///
/// This is why [`Turn::seed`] rides the target rather than [`TurnRequest`],
/// and why the `TurnTarget::Runtime` arm refuses a seed instead of dropping
/// it: that path speaks to the controller over these fields and there is
/// nowhere in them to put history. If a future controller gains such an input,
/// this test fails and the refusal can be reconsidered on purpose rather than
/// discovered by an agent answering with no memory of the conversation.
#[test]
fn the_controller_declares_nowhere_for_a_seed_to_travel() {
let schema = openhuman_core::core::all::all_registered_controllers()
.into_iter()
.find(|controller| {
controller.schema.namespace == "inference" && controller.schema.function == "agent_chat"
})
.expect("inference.agent_chat is a registered controller");
let schema = schema.schema;
// An allowlist, not a denylist of suspicious names: a future input called
// `context` or `transcript` could carry history just as well, and a
// heuristic that guesses at names would pass it silently. Anything new
// fails here until someone decides whether a seed could ride it.
const KNOWN: [&str; 11] = [
"message",
"model_override",
"temperature",
"thread_id",
"cwd",
"inference_url",
"inference_headers",
"inference_provider_options",
"inference_reasoning_effort",
"api_key",
"agent_id",
];
let unknown: Vec<&str> = schema
.inputs
.iter()
.map(|field| field.name)
.filter(|name| !KNOWN.contains(name))
.collect();
assert!(
unknown.is_empty(),
"agent_chat gained input(s) {unknown:?}; `Turn::seed` refuses the Runtime \
path on the assumption none of them can carry history. Decide, then \
add them here."
);
}
/// A turn outcome reports what it spent, and is not `Eq`.
///
/// The type-level half of the metering contract. `LastTurnUsage` carries a
/// cost in dollars, so [`TurnOutcome`] cannot derive `Eq` -- this asserts the
/// field is reachable and comparable the way a host meters it, and fails to
/// compile if `usage` is dropped or retyped.
#[test]
fn a_turn_outcome_carries_what_the_turn_spent() {
let spent = openhuman_core::agent::tinyagents::host::LastTurnUsage {
input_tokens: 1_200,
output_tokens: 340,
cost_usd: Some(0.0042),
..Default::default()
};
let outcome = TurnOutcome {
reply: "done".into(),
session_id: "s".into(),
usage: Some(spent.clone()),
structured: None,
finish_reason: None,
answered_model: None,
unapplied_steers: Vec::new(),
};
let metered = outcome.usage.as_ref().expect("a metered turn");
assert_eq!(metered.input_tokens, 1_200);
assert_eq!(metered.output_tokens, 340);
assert!((metered.cost_usd.expect("a priced turn") - 0.0042).abs() < f64::EPSILON);
// Unmetered is representable and distinct, which is what the RPC path and
// a session that reported nothing both produce.
let unmetered = TurnOutcome {
usage: None,
..outcome.clone()
};
assert_ne!(unmetered, outcome);
}
#[test]
fn approval_event_correlation_uses_the_web_chat_origin_without_replacing_session_identity() {
let origin = AgentTurnOrigin::WebChat {
thread_id: "approval-thread".into(),
client_id: "client".into(),
request_id: None,
};
let session = "durable-session";
assert_eq!(event_thread_id(Some(&origin), session), "approval-thread");
assert_eq!(
event_thread_id(Some(&AgentTurnOrigin::Cli), session),
session
);
assert_eq!(event_thread_id(None, session), session);
}
#[tokio::test]
async fn runtime_tool_events_preserve_order_and_discard_tool_payloads() {
let hub = crate::events::EventHub::new(8);
let mut events = hub.subscribe();
let agent = Some("a".to_owned());
let private = "private-tool-content";
let started = crate::AgentProgress::ToolCallStarted {
call_id: private.into(),
tool_name: "host_tool".into(),
arguments: serde_json::json!({"input": private}),
iteration: 1,
display_label: Some(private.into()),
display_detail: Some(private.into()),
};
let completed = crate::AgentProgress::ToolCallCompleted {
call_id: private.into(),
tool_name: "host_tool".into(),
success: true,
output_chars: private.len(),
output: private.into(),
arguments: Some(serde_json::json!({"input": private})),
elapsed_ms: 1,
iteration: 1,
failure: None,
display_label: Some(private.into()),
display_detail: Some(private.into()),
structured: Some(serde_json::json!({"output": private})),
};
observe_progress(&hub, &agent, "turn-a", &started);
observe_progress(&hub, &agent, "turn-a", &completed);
for expected in [
crate::RuntimeEventKind::ToolStarted {
tool_name: "host_tool".into(),
},
crate::RuntimeEventKind::ToolEnded {
tool_name: "host_tool".into(),
success: true,
},
] {
let event = events.recv().await.unwrap();
assert_eq!(event.agent_id.as_deref(), Some("a"));
assert_eq!(event.turn_id.as_deref(), Some("turn-a"));
assert_eq!(event.kind, expected);
assert!(!serde_json::to_string(&event).unwrap().contains(private));
}
}