Skip to content

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

feat(agent): validate chat_agent_id before persisting or routing

Steven Enamakelbcf271b2d236
diff --git a/crates/openhuman-core/src/agent/session_host/builder/factory.rs b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
index 22a773d4b7..f369282f5f 100644
--- a/crates/openhuman-core/src/agent/session_host/builder/factory.rs
+++ b/crates/openhuman-core/src/agent/session_host/builder/factory.rs
@@ -23,6 +23,14 @@ use tinytools_agent::dialect::{
};
impl OpenHumanSessionHost {
+ /// Returns whether `agent_id` resolves to a runnable definition for this
+ /// configuration. This is deliberately the same resolution path used by
+ /// [`Self::from_config_for_agent`], so configuration writers cannot save
+ /// a web-chat route that the session factory would later reject.
+ pub(crate) fn is_runnable_agent_id(config: &Config, agent_id: &str) -> bool {
+ resolve_target_definition(config, agent_id).is_ok()
+ }
+
/// Constructs an `OpenHumanSessionHost` instance from a global system configuration.
///
/// Thin wrapper around [`OpenHumanSessionHost::from_config_for_agent`] that always
diff --git a/crates/openhuman-core/src/config/ops/agent.rs b/crates/openhuman-core/src/config/ops/agent.rs
index 1c84e851c8..87fa054814 100644
--- a/crates/openhuman-core/src/config/ops/agent.rs
+++ b/crates/openhuman-core/src/config/ops/agent.rs
@@ -232,6 +232,13 @@ pub async fn apply_agent_settings(
if let Some(chat_agent_id) = update.chat_agent_id {
let trimmed = chat_agent_id.trim();
+ if !trimmed.is_empty()
+ && !crate::agent::OpenHumanSessionHost::is_runnable_agent_id(config, trimmed)
+ {
+ return Err(format!(
+ "chat_agent_id '{trimmed}' is not a runnable agent definition"
+ ));
+ }
config.agent.chat_agent_id = (!trimmed.is_empty()).then(|| trimmed.to_string());
log::debug!(
"[config][agent] chat_agent_id -> {:?}",
diff --git a/crates/openhuman-core/src/config/ops_agent_paths_tests.rs b/crates/openhuman-core/src/config/ops_agent_paths_tests.rs
index f5c5a9b480..ad4ef8dd80 100644
--- a/crates/openhuman-core/src/config/ops_agent_paths_tests.rs
+++ b/crates/openhuman-core/src/config/ops_agent_paths_tests.rs
@@ -49,6 +49,26 @@ async fn apply_agent_settings_none_leaves_timeout_unchanged() {
assert_eq!(cfg.agent.agent_timeout_secs, 250);
}
+#[tokio::test]
+async fn apply_agent_settings_rejects_unknown_chat_agent_id() {
+ let _g = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ let tmp = tempdir().unwrap();
+ let mut cfg = tmp_config(&tmp);
+
+ let err = apply_agent_settings(
+ &mut cfg,
+ AgentSettingsPatch {
+ chat_agent_id: Some("typoed_agent".into()),
+ ..AgentSettingsPatch::default()
+ },
+ )
+ .await
+ .expect_err("unknown agents must not be persisted as web-chat routes");
+
+ assert!(err.contains("not a runnable agent definition"), "{err}");
+ assert!(cfg.agent.chat_agent_id.is_none());
+}
+
// ── apply_agent_paths_settings (action_dir editable, issue #3240) ──────────────
#[tokio::test]
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index 83ce07e22b..57e637fcf1 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -33,14 +33,22 @@ pub(super) fn model_registry_signature(config: &Config) -> String {
/// not take chat down.
pub(super) fn pick_target_agent_id(config: &Config) -> String {
const DEFAULT_CHAT_AGENT_ID: &str = "orchestrator";
- config
+ let selected = config
.agent
.chat_agent_id
.as_deref()
.map(str::trim)
.filter(|id| !id.is_empty())
- .unwrap_or(DEFAULT_CHAT_AGENT_ID)
- .to_string()
+ .unwrap_or(DEFAULT_CHAT_AGENT_ID);
+
+ if OpenHumanSessionHost::is_runnable_agent_id(config, selected) {
+ return selected.to_string();
+ }
+
+ log::warn!(
+ "[web-channel] configured chat_agent_id={selected:?} is not a runnable definition; falling back to {DEFAULT_CHAT_AGENT_ID}"
+ );
+ DEFAULT_CHAT_AGENT_ID.to_string()
}
pub(crate) fn normalize_model_override(model_override: Option<String>) -> Option<String> {
diff --git a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
index 5f004387aa..f27ce042eb 100644
--- a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
+++ b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
@@ -505,6 +505,7 @@ fn fingerprint_diff_reports_every_differing_field() {
#[test]
fn chat_agent_id_selects_the_web_chat_agent_and_defaults_to_the_orchestrator() {
use super::pick_target_agent_id;
+ crate::agent::harness::AgentDefinitionRegistry::init_global_builtins().unwrap();
let mut config = crate::config::Config::default();
assert_eq!(
@@ -517,13 +518,13 @@ fn chat_agent_id_selects_the_web_chat_agent_and_defaults_to_the_orchestrator() {
"unset falls back to what the app runs"
);
- config.agent.chat_agent_id = Some("life_scenarios".to_string());
- assert_eq!(pick_target_agent_id(&config), "life_scenarios");
+ config.agent.chat_agent_id = Some("researcher".to_string());
+ assert_eq!(pick_target_agent_id(&config), "researcher");
// Padding is an operator typo in a hand-edited config.toml, not a request
// for an agent whose id has spaces in it.
- config.agent.chat_agent_id = Some(" life_scenarios ".to_string());
- assert_eq!(pick_target_agent_id(&config), "life_scenarios");
+ config.agent.chat_agent_id = Some(" researcher ".to_string());
+ assert_eq!(pick_target_agent_id(&config), "researcher");
// Blank is "unset", not "an agent named empty string": a turn routed at an
// id the registry cannot answer would fail chat outright.
@@ -535,4 +536,11 @@ fn chat_agent_id_selects_the_web_chat_agent_and_defaults_to_the_orchestrator() {
"blank {blank:?} falls back rather than routing nowhere"
);
}
+
+ config.agent.chat_agent_id = Some("typoed_agent".to_string());
+ assert_eq!(
+ pick_target_agent_id(&config),
+ "orchestrator",
+ "an unknown optional setting must not take web chat down"
+ );
}
diff --git a/scripts/life-scenarios/run.mjs b/scripts/life-scenarios/run.mjs
index e76bb10c53..5451df895c 100644
--- a/scripts/life-scenarios/run.mjs
+++ b/scripts/life-scenarios/run.mjs
@@ -102,7 +102,15 @@ function parseArgs(argv) {
if (a === "--only")
o.only = next().split(",").map((s) => s.trim()).filter(Boolean);
else if (a === "--driver") o.driver = next();
- else if (a === "--agent") o.agentId = next();
+ else if (a === "--agent") {
+ const agentId = next();
+ if (agentId && !/^[A-Za-z0-9_-]+$/.test(agentId)) {
+ throw new Error(
+ "--agent must contain only ASCII letters, digits, '_' or '-'",
+ );
+ }
+ o.agentId = agentId;
+ }
else if (a === "--model") o.model = next();
else if (a === "--inference-url") o.inferenceUrl = next();
else if (a === "--api-key") o.apiKey = next();