Skip to content

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

feat(tinymemes): add arm module for tinymemes

Steven Enamakelbe9e49eb7227
diff --git a/crates/openhuman-core/src/tinymemes/arm.rs b/crates/openhuman-core/src/tinymemes/arm.rs
index 0a198c6612..1413031418 100644
--- a/crates/openhuman-core/src/tinymemes/arm.rs
+++ b/crates/openhuman-core/src/tinymemes/arm.rs
@@ -98,6 +98,15 @@ pub(crate) fn bucket(thread_id: &str) -> u8 {
(hash % 100) as u8
}
+/// The arm for a thread under `config`: the `[components] memes` switch is the
+/// master, and the flag's arm is honoured only while the component is on.
+pub(crate) fn arm_for(config: &crate::config::Config, mode: Mode, thread_id: &str) -> Arm {
+ if !config.components.memes {
+ return Arm::Disabled;
+ }
+ assign(mode, thread_id)
+}
+
pub(crate) fn assign(mode: Mode, thread_id: &str) -> Arm {
match mode {
Mode::Off => Arm::Disabled,
diff --git a/crates/openhuman-core/src/tinymemes/mod.rs b/crates/openhuman-core/src/tinymemes/mod.rs
index 8d5cfc7726..692b0689a1 100644
--- a/crates/openhuman-core/src/tinymemes/mod.rs
+++ b/crates/openhuman-core/src/tinymemes/mod.rs
@@ -47,13 +47,12 @@ pub(crate) fn flag_enabled(value: &str) -> bool {
}
const TIMEOUT_ENV: &str = "OPENHUMAN_TINYMEMES_TIMEOUT_MS";
-const DEFAULT_TIMEOUT: Duration = Duration::from_secs(20);
/// Whether this thread's answer text must be held back from streaming because
/// its final reply will be remixed (treatment arm). The UI then shows only the
/// remixed reply instead of the original streaming in and being replaced.
-pub(crate) fn holds_text_stream(thread_id: &str) -> bool {
- arm::assign(arm::mode(), thread_id) == Arm::Treatment
+pub(crate) fn holds_text_stream(config: &Config, thread_id: &str) -> bool {
+ arm::arm_for(config, arm::mode(), thread_id) == Arm::Treatment
}
/// Something that can remix one reply: the module in production, a script in
@@ -92,7 +91,7 @@ pub(crate) async fn remix_task_reply(
return;
}
if let Some(remixed) = remix_final_reply(
- arm::assign(arm::mode(), thread_id),
+ arm::arm_for(config, arm::mode(), thread_id),
config,
thread_id,
request_id,
@@ -146,7 +145,7 @@ async fn remix_final_reply(
started,
user_message,
reply,
- budget(),
+ budget(config),
)
.await
}
@@ -204,13 +203,20 @@ async fn load(
.flatten()
}
-/// The remix time budget: `OPENHUMAN_TINYMEMES_TIMEOUT_MS`, else 20 s.
-fn budget() -> Duration {
- std::env::var(TIMEOUT_ENV)
- .ok()
- .and_then(|v| v.parse::<u64>().ok())
- .map(Duration::from_millis)
- .unwrap_or(DEFAULT_TIMEOUT)
+/// The remix time budget: `OPENHUMAN_TINYMEMES_TIMEOUT_MS` when set, else
+/// `[memes] timeout_ms`.
+fn budget(config: &Config) -> Duration {
+ budget_with(
+ std::env::var(TIMEOUT_ENV).ok().as_deref(),
+ config.memes.timeout_ms,
+ )
+}
+
+fn budget_with(env: Option<&str>, configured_ms: u64) -> Duration {
+ Duration::from_millis(
+ env.and_then(|v| v.trim().parse::<u64>().ok())
+ .unwrap_or(configured_ms),
+ )
}
/// Read, rate, and remix `reply` against the thread, within `budget`.
diff --git a/crates/openhuman-core/src/tinymemes/mod_tests.rs b/crates/openhuman-core/src/tinymemes/mod_tests.rs
index f17c2255d5..6971805023 100644
--- a/crates/openhuman-core/src/tinymemes/mod_tests.rs
+++ b/crates/openhuman-core/src/tinymemes/mod_tests.rs
@@ -104,10 +104,52 @@ async fn run(
}
#[test]
-fn the_budget_defaults_to_twenty_seconds() {
- if std::env::var(TIMEOUT_ENV).is_err() {
- assert_eq!(budget(), DEFAULT_TIMEOUT);
+fn the_budget_defaults_to_the_configured_twenty_seconds() {
+ let config = Config::default();
+ assert_eq!(budget_with(None, config.memes.timeout_ms), Duration::from_secs(20));
+}
+
+#[test]
+fn the_budget_env_override_beats_config_and_bad_values_fall_back() {
+ assert_eq!(budget_with(Some("1500"), 20_000), Duration::from_millis(1500));
+ assert_eq!(budget_with(Some("soon"), 7_000), Duration::from_millis(7_000));
+}
+
+#[test]
+fn component_off_disables_every_arm() {
+ let mut config = Config::default();
+ config.components.memes = false;
+ for mode in [arm::Mode::On, arm::Mode::Ab { percent: 100 }] {
+ assert_eq!(arm::arm_for(&config, mode, "t1"), Arm::Disabled);
}
+ assert!(!holds_text_stream(&config, "t1"));
+}
+
+#[test]
+fn arm_is_honoured_only_when_the_component_is_on() {
+ let config = Config::default();
+ assert!(config.components.memes);
+ assert_eq!(arm::arm_for(&config, arm::Mode::Off, "t1"), Arm::Disabled);
+ assert_eq!(arm::arm_for(&config, arm::Mode::On, "t1"), Arm::Treatment);
+ assert_eq!(
+ arm::arm_for(&config, arm::Mode::Ab { percent: 0 }, "t1"),
+ Arm::Control
+ );
+}
+
+#[test]
+fn precedence_matrix_env_beats_build_default_and_component_gates_both() {
+ // env set wins over the build default; unset falls to it; neither is off.
+ assert_eq!(arm::resolve_mode(Some("off"), Some("on")), arm::Mode::Off);
+ assert_eq!(arm::resolve_mode(Some("on"), None), arm::Mode::On);
+ assert_eq!(arm::resolve_mode(None, Some("on")), arm::Mode::On);
+ assert_eq!(arm::resolve_mode(None, None), arm::Mode::Off);
+ let mut config = Config::default();
+ config.components.memes = false;
+ assert_eq!(
+ arm::arm_for(&config, arm::resolve_mode(Some("on"), Some("on")), "t"),
+ Arm::Disabled
+ );
}
#[test]
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index ef2de109bf..f3c8051754 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -167,7 +167,7 @@ pub(crate) async fn run_chat_task(
// original answer text is not streamed ahead of it.
#[cfg(feature = "tinymemes")]
{
- bridge_metadata.hold_text_stream = crate::tinymemes::holds_text_stream(thread_id);
+ bridge_metadata.hold_text_stream = crate::tinymemes::holds_text_stream(&config, thread_id);
}
let bridge = spawn_progress_bridge(
progress_rx,