Skip to content

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

feat(core): add controller parameter contract verification helpers

Steven Enamakel32d7774c9fa1
diff --git a/crates/openhuman-core/src/core/schema_test_support.rs b/crates/openhuman-core/src/core/schema_test_support.rs
index 7641016c3f..9309db9a42 100644
--- a/crates/openhuman-core/src/core/schema_test_support.rs
+++ b/crates/openhuman-core/src/core/schema_test_support.rs
@@ -1,5 +1,86 @@
use super::*;
+/// Independent wire fixtures paired with the handler's real pure decoder.
+/// The runner checks parameter acceptance and never invokes the handler.
+#[derive(Clone)]
+pub(crate) struct ControllerParamContract {
+ pub method: &'static str,
+ pub required: &'static [&'static str],
+ pub handler: ControllerHandler,
+ pub examples: Vec<Value>,
+ pub parse: fn(Map<String, Value>) -> Result<(), String>,
+}
+
+/// Select controllers independently of the fixture table, so a missing fixture
+/// cannot silently remove its own controller from the check.
+pub(crate) fn verify_parameter_contracts(
+ controllers: &[RegisteredController],
+ contracts: &[ControllerParamContract],
+) -> Result<(), String> {
+ use std::collections::BTreeMap;
+
+ let mut by_method = BTreeMap::new();
+ for contract in contracts {
+ if by_method.insert(contract.method, contract).is_some() {
+ return Err(format!("duplicate contract `{}`", contract.method));
+ }
+ }
+ let mut registered = BTreeMap::new();
+ for controller in controllers {
+ let method = controller.schema.method_name();
+ if registered.insert(method.clone(), controller).is_some() {
+ return Err(format!("duplicate controller `{method}`"));
+ }
+ if !by_method.contains_key(method.as_str()) {
+ return Err(format!("missing contract `{method}`"));
+ }
+ }
+ for method in by_method.keys() {
+ if !registered.contains_key(*method) {
+ return Err(format!("extra contract `{method}`"));
+ }
+ }
+ if registered.is_empty() {
+ return Err("empty controller contract inventory".into());
+ }
+ for (method, controller) in registered {
+ let contract = by_method[method.as_str()];
+ if !std::ptr::fn_addr_eq(controller.handler, contract.handler) {
+ return Err(format!("{method} handler identity differs from its contract"));
+ }
+ verify_parameter_schema(&controller.schema, contract)?;
+ }
+ Ok(())
+}
+
+pub(crate) fn verify_parameter_schema(
+ schema: &ControllerSchema,
+ contract: &ControllerParamContract,
+) -> Result<(), String> {
+ if schema.method_name() != contract.method {
+ return Err(format!("{} contract method identity differs", contract.method));
+ }
+ if schema.description.trim().is_empty() {
+ return Err(format!("{} has no description", contract.method));
+ }
+ required_input_contract(schema, contract.required)?;
+ if contract.examples.is_empty() {
+ return Err(format!("{} has no examples", contract.method));
+ }
+ for (index, example) in contract.examples.iter().enumerate() {
+ let params = example.as_object().ok_or_else(|| {
+ format!("{} example {index} is not an object", contract.method)
+ })?;
+ validate_params(schema, params).map_err(|error| {
+ format!("{} schema rejects example {index}: {error}", contract.method)
+ })?;
+ (contract.parse)(params.clone()).map_err(|error| {
+ format!("{} pure parser rejects example {index}: {error}", contract.method)
+ })?;
+ }
+ Ok(())
+}
+
pub(crate) fn expect_schema_identity(schema: &ControllerSchema, namespace: &str, function: &str) {
assert_eq!(schema.namespace, namespace);
assert_eq!(schema.function, function);
@@ -131,3 +212,7 @@ pub(crate) fn assert_schema_controller_parity(
"expected a `{known_function}` controller schema to be registered",
);
}
+
+#[cfg(test)]
+#[path = "schema_test_support_tests.rs"]
+mod tests;