Skip to content

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

refactor(channels): extract configured_control_channel logic

Steven Enamakelcca7fdc8454a
diff --git a/crates/openhuman-core/src/channels/routes.rs b/crates/openhuman-core/src/channels/routes.rs
index e4be8ed7c3..0906b945a7 100644
--- a/crates/openhuman-core/src/channels/routes.rs
+++ b/crates/openhuman-core/src/channels/routes.rs
@@ -171,6 +171,8 @@ where
let history_len =
durable_channel_history_len(ctx, &current, &msg.channel, durable_session_key.as_deref())
.await;
+ let (route_scope, configured_channel) =
+ configured_control_channel(ctx, reset_scope.as_ref());
let client = super::context::module_client(ctx).await;
let operation = async {
let lease = crate::modules::channel_runtime::ChannelRuntimeLease::open(
@@ -178,7 +180,7 @@ where
RouteCallbacks::new(
ctx.clone(),
super::runtime::session::channel_session(),
- reset_scope
+ route_scope
.clone()
.zip(durable_session_key)
.map(|(scope, durable_session_key)| {
@@ -204,21 +206,8 @@ where
model: current.model,
},
history_len,
- session_scope: reset_scope.clone().map(Box::new),
- configured_channel: reset_scope.as_ref().and_then(|scope| {
- let account = scope.channel.account_id.as_deref()?;
- let channel = ctx.channels_by_name.get(account)?;
- (channel.name() == account
- && tinychannels_bus::provider_id(channel.name()) == scope.channel.id)
- .then(|| {
- Box::new(
- tinychannels_bus::module_control::ConfiguredControlChannel {
- name: channel.name().to_owned(),
- capabilities: channel.capabilities(),
- },
- )
- })
- }),
+ session_scope: route_scope.map(Box::new),
+ configured_channel,
providers: provider_descriptors(),
},),
)
@@ -293,6 +282,43 @@ where
true
}
+/// Resolve the configured instance for a route callback. Provider envelopes
+/// can omit an account id; in that case we may infer it only when the host has
+/// exactly one configured instance for the exact provider id. Ambiguous or
+/// mismatched scopes stay fail-closed.
+fn configured_control_channel(
+ ctx: &ChannelRuntimeContext,
+ scope: Option<&tinychannels_bus::module_remote::RemoteSessionScope>,
+) -> (
+ Option<tinychannels_bus::module_remote::RemoteSessionScope>,
+ Option<Box<tinychannels_bus::module_control::ConfiguredControlChannel>>,
+) {
+ let Some(scope) = scope else {
+ return (None, None);
+ };
+ let account = scope.channel.account_id.as_deref();
+ let mut candidates = ctx.channels_by_name.values().filter(|channel| {
+ tinychannels_bus::provider_id(channel.name()) == scope.channel.id
+ && account.is_none_or(|account| channel.name() == account)
+ });
+ let Some(channel) = candidates.next() else {
+ return (Some(scope.clone()), None);
+ };
+ if candidates.next().is_some() {
+ return (Some(scope.clone()), None);
+ }
+
+ let mut trusted_scope = scope.clone();
+ trusted_scope.channel.account_id = Some(channel.name().to_owned());
+ let configured = Box::new(
+ tinychannels_bus::module_control::ConfiguredControlChannel {
+ name: channel.name().to_owned(),
+ capabilities: channel.capabilities(),
+ },
+ );
+ (Some(trusted_scope), Some(configured))
+}
+
async fn durable_channel_history_len(
ctx: &ChannelRuntimeContext,
route: &ChannelRouteSelection,