Skip to content

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

fix(channels): scope voice turn facts by tenant and channel

Steven Enamakel185981cfbc96
diff --git a/crates/openhuman-core/src/channels/host/voice.rs b/crates/openhuman-core/src/channels/host/voice.rs
index be4b9ba82a..2d33737eeb 100644
--- a/crates/openhuman-core/src/channels/host/voice.rs
+++ b/crates/openhuman-core/src/channels/host/voice.rs
@@ -12,9 +12,10 @@ use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Instant;
use tinychannels_bus::{
- ChannelInboundEnvelope, ContentPart, HostSpeechSynthesisRequest, HostSpeechSynthesisResult,
- HostTranscriptionRequest, HostTranscriptionResult, HostVoiceReplyMode, HostVoiceReplyPolicy,
- HostVoiceScope, HostVoiceTranscriptionRequest,
+ ChannelInboundEnvelope, ChannelRef, ContentPart, ConversationRef,
+ HostSpeechSynthesisRequest, HostSpeechSynthesisResult, HostTranscriptionRequest,
+ HostTranscriptionResult, HostVoiceReplyMode, HostVoiceReplyPolicy, HostVoiceScope,
+ HostVoiceTranscriptionRequest,
};
const LOG_PREFIX: &str = "[channels_voice]";
@@ -29,15 +30,21 @@ pub(crate) struct VoiceTurnFacts {
pub explicitly_requested: bool,
}
-static TURNS: Mutex<VecDeque<(String, VoiceTurnFacts)>> = Mutex::new(VecDeque::new());
-
-fn turn_key(conversation_id: &str, turn_id: &str) -> String {
- format!("{conversation_id}\u{1f}{turn_id}")
+/// Full authenticated identity for one inbound voice-policy fact. Provider
+/// conversation and message ids are not globally unique, so neither may be
+/// used without the tenant and configured account that own them.
+#[derive(Debug, Clone, PartialEq, Eq)]
+struct VoiceTurnKey {
+ tenant: crate::core::runtime::Tenant,
+ channel: ChannelRef,
+ conversation: ConversationRef,
+ turn_id: String,
}
+static TURNS: Mutex<VecDeque<(VoiceTurnKey, VoiceTurnFacts)>> = Mutex::new(VecDeque::new());
+
/// Remember the facts of an inbound turn so the reply policy can read them.
-pub(crate) fn record_turn(conversation_id: &str, turn_id: &str, facts: VoiceTurnFacts) {
- let key = turn_key(conversation_id, turn_id);
+fn record_turn(key: VoiceTurnKey, facts: VoiceTurnFacts) {
let mut turns = TURNS.lock();
turns.retain(|(existing, _)| existing != &key);
if turns.len() >= MAX_TRACKED_TURNS {
@@ -46,8 +53,22 @@ pub(crate) fn record_turn(conversation_id: &str, turn_id: &str, facts: VoiceTurn
turns.push_back((key, facts));
}
-fn turn_facts(conversation_id: &str, turn_id: &str) -> VoiceTurnFacts {
- let key = turn_key(conversation_id, turn_id);
+fn turn_key(
+ tenant: &crate::core::runtime::Tenant,
+ channel: &ChannelRef,
+ conversation: &ConversationRef,
+ turn_id: &str,
+) -> VoiceTurnKey {
+ VoiceTurnKey {
+ tenant: tenant.clone(),
+ channel: channel.clone(),
+ conversation: conversation.clone(),
+ turn_id: turn_id.to_owned(),
+ }
+}
+
+fn turn_facts(scope: &HostVoiceScope, tenant: &crate::core::runtime::Tenant) -> VoiceTurnFacts {
+ let key = turn_key(tenant, &scope.channel, &scope.conversation, &scope.turn_id);
TURNS
.lock()
.iter()
@@ -58,14 +79,21 @@ fn turn_facts(conversation_id: &str, turn_id: &str) -> VoiceTurnFacts {
/// Record an inbound envelope's voice facts. The inbound message id is the
/// turn id the module echoes back in its voice scope.
-pub(crate) fn record_inbound_envelope(envelope: &ChannelInboundEnvelope) {
+pub(crate) fn record_inbound_envelope(
+ envelope: &ChannelInboundEnvelope,
+ tenant: &crate::core::runtime::Tenant,
+) {
let inbound_has_voice = envelope
.parts
.iter()
.any(|part| matches!(part, ContentPart::VoiceNote { .. }));
record_turn(
- &envelope.conversation.id,
- &envelope.message_id,
+ turn_key(
+ tenant,
+ &envelope.channel,
+ &envelope.conversation,
+ &envelope.message_id,
+ ),
VoiceTurnFacts {
inbound_has_voice,
explicitly_requested: false,
@@ -95,9 +123,10 @@ fn wire_mode(mode: VoiceReplyMode) -> HostVoiceReplyMode {
pub(crate) fn resolve_policy(
voice: &ChannelVoiceConfig,
scope: &HostVoiceScope,
+ tenant: &crate::core::runtime::Tenant,
) -> HostVoiceReplyPolicy {
let mode = voice.resolve_mode(&scope.configured_instance, thread_key(scope));
- let facts = turn_facts(&scope.conversation.id, &scope.turn_id);
+ let facts = turn_facts(scope, tenant);
HostVoiceReplyPolicy {
mode: wire_mode(mode),
inbound_has_voice: facts.inbound_has_voice,
@@ -242,8 +271,12 @@ impl ChannelVoiceHost {
}
/// Host-authoritative reply policy for the scoped turn.
- pub(crate) fn reply_policy(&self, scope: &HostVoiceScope) -> HostVoiceReplyPolicy {
- let policy = resolve_policy(&self.config.channel_voice, scope);
+ pub(crate) fn reply_policy(
+ &self,
+ scope: &HostVoiceScope,
+ tenant: &crate::core::runtime::Tenant,
+ ) -> HostVoiceReplyPolicy {
+ let policy = resolve_policy(&self.config.channel_voice, scope, tenant);
tracing::debug!(
"{LOG_PREFIX} policy channel={} mode={:?} inbound_voice={}",
scope.configured_instance,
diff --git a/crates/openhuman-core/src/channels/host/voice_tests.rs b/crates/openhuman-core/src/channels/host/voice_tests.rs
index 1b9fddfc2d..a8f0bdd251 100644
--- a/crates/openhuman-core/src/channels/host/voice_tests.rs
+++ b/crates/openhuman-core/src/channels/host/voice_tests.rs
@@ -20,6 +20,10 @@ fn scope(channel: &str, conversation: &str, turn: &str) -> HostVoiceScope {
fn envelope(conversation: &str, message: &str, voice: bool) -> ChannelInboundEnvelope {
let mut envelope = ChannelInboundEnvelope {
message_id: message.into(),
+ channel: ChannelRef {
+ id: "telegram".into(),
+ account_id: Some("telegram".into()),
+ },
conversation: ConversationRef {
id: conversation.into(),
..ConversationRef::default()
@@ -41,13 +45,14 @@ fn envelope(conversation: &str, message: &str, voice: bool) -> ChannelInboundEnv
#[test]
fn mirror_policy_reports_inbound_voice_from_recorded_envelope() {
- record_inbound_envelope(&envelope("c-voice", "m1", true));
- record_inbound_envelope(&envelope("c-text", "m2", false));
+ let tenant = crate::core::runtime::Tenant::default();
+ record_inbound_envelope(&envelope("c-voice", "m1", true), &tenant);
+ record_inbound_envelope(&envelope("c-text", "m2", false), &tenant);
let cfg = ChannelVoiceConfig::default();
- let voice = resolve_policy(&cfg, &scope("telegram", "c-voice", "m1"));
+ let voice = resolve_policy(&cfg, &scope("telegram", "c-voice", "m1"), &tenant);
assert_eq!(voice.mode, HostVoiceReplyMode::Mirror);
assert!(voice.inbound_has_voice);
- let text = resolve_policy(&cfg, &scope("telegram", "c-text", "m2"));
+ let text = resolve_policy(&cfg, &scope("telegram", "c-text", "m2"), &tenant);
assert!(!text.inbound_has_voice);
}
@@ -64,16 +69,19 @@ fn same_conversation_and_turn_ids_do_not_cross_voice_facts_between_accounts() {
account_id: Some("telegram:bob".into()),
};
- record_inbound_envelope(&voice);
- record_inbound_envelope(&text);
+ let tenant = crate::core::runtime::Tenant::default();
+ record_inbound_envelope(&voice, &tenant);
+ record_inbound_envelope(&text, &tenant);
let alice = resolve_policy(
&ChannelVoiceConfig::default(),
&scope("telegram:alice", "shared-conversation", "shared-turn"),
+ &tenant,
);
let bob = resolve_policy(
&ChannelVoiceConfig::default(),
&scope("telegram:bob", "shared-conversation", "shared-turn"),
+ &tenant,
);
assert!(alice.inbound_has_voice, "Alice's voice turn was overwritten by Bob's text turn");
assert!(!bob.inbound_has_voice);
@@ -81,7 +89,11 @@ fn same_conversation_and_turn_ids_do_not_cross_voice_facts_between_accounts() {
#[test]
fn unknown_turn_defaults_to_no_voice() {
- let policy = resolve_policy(&ChannelVoiceConfig::default(), &scope("t", "nope", "nope"));
+ let policy = resolve_policy(
+ &ChannelVoiceConfig::default(),
+ &scope("t", "nope", "nope"),
+ &crate::core::runtime::Tenant::default(),
+ );
assert!(!policy.inbound_has_voice);
assert!(!policy.explicitly_requested);
}
@@ -97,24 +109,32 @@ fn policy_mode_follows_thread_then_channel_overrides() {
},
);
assert_eq!(
- resolve_policy(&cfg, &scope("telegram", "c", "t")).mode,
+ resolve_policy(&cfg, &scope("telegram", "c", "t"), &crate::core::runtime::Tenant::default()).mode,
HostVoiceReplyMode::Always
);
cfg.threads.insert("c".into(), VoiceReplyMode::Never);
assert_eq!(
- resolve_policy(&cfg, &scope("telegram", "c", "t")).mode,
+ resolve_policy(&cfg, &scope("telegram", "c", "t"), &crate::core::runtime::Tenant::default()).mode,
HostVoiceReplyMode::Never
);
assert_eq!(
- resolve_policy(&cfg, &scope("slack", "other", "t")).mode,
+ resolve_policy(&cfg, &scope("slack", "other", "t"), &crate::core::runtime::Tenant::default()).mode,
HostVoiceReplyMode::Mirror
);
}
#[test]
fn registry_is_bounded() {
+ let tenant = crate::core::runtime::Tenant::default();
for index in 0..(MAX_TRACKED_TURNS + 10) {
- record_turn("bounded", &index.to_string(), VoiceTurnFacts::default());
+ let conversation = ConversationRef {
+ id: "bounded".into(),
+ ..ConversationRef::default()
+ };
+ record_turn(
+ turn_key(&tenant, &ChannelRef::default(), &conversation, &index.to_string()),
+ VoiceTurnFacts::default(),
+ );
}
assert!(TURNS.lock().len() <= MAX_TRACKED_TURNS);
}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index 632d60833b..f9a7705a31 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -81,7 +81,6 @@ pub(crate) async fn process_channel_runtime_message(
conversation: envelope.conversation.clone(),
session_key: canonical_session_key.clone(),
};
- crate::channels::host::voice::record_inbound_envelope(&envelope);
let native_target = if let Some(target) = progressive_target {
Some(target)
} else {
@@ -101,6 +100,7 @@ pub(crate) async fn process_channel_runtime_message(
return;
}
};
+ crate::channels::host::voice::record_inbound_envelope(&envelope, &tenant);
let session_key = crate::core::runtime::tenant::tenant_key(&tenant, &canonical_session_key);
println!(
diff --git a/crates/openhuman-core/src/channels/runtime/startup/callback.rs b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
index 85fd04510d..b1eb525735 100644
--- a/crates/openhuman-core/src/channels/runtime/startup/callback.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup/callback.rs
@@ -246,7 +246,7 @@ impl ChannelHostCallbacks {
request: HostVoiceReplyPolicyRequest,
) -> Result<HostVoiceReplyPolicy> {
Self::validate_callback_size(&request)?;
- Ok(self.voice().reply_policy(&request.scope))
+ Ok(self.voice().reply_policy(&request.scope, &self.tenant))
}
async fn publish_event(&self, publication: HostEventPublication) -> Result<()> {