Skip to content

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

feat(tinymemes): add TinyMemes reply remixing module

Steven Enamakeldb75c64634bf
diff --git a/crates/openhuman-core/src/config/schema/memes.rs b/crates/openhuman-core/src/config/schema/memes.rs
new file mode 100644
index 0000000000..6cdfcadb84
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/memes.rs
@@ -0,0 +1,41 @@
+//! `[memes]` — tuning for TinyMemes reply remixing.
+//!
+//! Whether remixing runs at all is `[components] memes`; which threads are in
+//! the treatment arm is the `OPENHUMAN_TINYMEMES` flag (`off`, `on`, `ab`,
+//! `ab:NN`, with `OPENHUMAN_TINYMEMES_DEFAULT` as the build-time default). The
+//! environment variables below stay as operator overrides and win over this
+//! section when set:
+//!
+//! | setting | override |
+//! | --- | --- |
+//! | `timeout_ms` | `OPENHUMAN_TINYMEMES_TIMEOUT_MS` |
+//! | `meme_cooldown` | `OPENHUMAN_TINYMEMES_MEME_COOLDOWN` |
+
+use schemars::JsonSchema;
+use serde::{Deserialize, Serialize};
+
+pub const DEFAULT_TIMEOUT_MS: u64 = 20_000;
+pub const DEFAULT_MEME_COOLDOWN: u32 = 3;
+
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
+#[serde(default)]
+pub struct MemesConfig {
+ /// Time budget for one remix, in milliseconds. The original reply is
+ /// delivered when it runs out.
+ pub timeout_ms: u64,
+ /// Replies between memes.
+ pub meme_cooldown: u32,
+}
+
+impl Default for MemesConfig {
+ fn default() -> Self {
+ Self {
+ timeout_ms: DEFAULT_TIMEOUT_MS,
+ meme_cooldown: DEFAULT_MEME_COOLDOWN,
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "memes_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/config/schema/memes_tests.rs b/crates/openhuman-core/src/config/schema/memes_tests.rs
new file mode 100644
index 0000000000..5ae09e2420
--- /dev/null
+++ b/crates/openhuman-core/src/config/schema/memes_tests.rs
@@ -0,0 +1,15 @@
+use super::*;
+
+#[test]
+fn defaults_match_the_documented_values() {
+ let c = MemesConfig::default();
+ assert_eq!(c.timeout_ms, 20_000);
+ assert_eq!(c.meme_cooldown, 3);
+}
+
+#[test]
+fn a_partial_section_keeps_the_other_defaults() {
+ let c: MemesConfig = toml::from_str("timeout_ms = 5000").unwrap();
+ assert_eq!(c.timeout_ms, 5000);
+ assert_eq!(c.meme_cooldown, DEFAULT_MEME_COOLDOWN);
+}
diff --git a/crates/openhuman-core/src/modules/registry/records_memes.rs b/crates/openhuman-core/src/modules/registry/records_memes.rs
new file mode 100644
index 0000000000..d3aa1abe7c
--- /dev/null
+++ b/crates/openhuman-core/src/modules/registry/records_memes.rs
@@ -0,0 +1,80 @@
+//! Registry record for the `tinymemes` module.
+
+use crate::modules::types::{LoadPolicy, ModuleRecord, PlatformAsset, SourceSpec};
+
+/// The `tinymemes` module: final-reply slang and meme remixing, loaded on
+/// first use. The host serves the callbacks it asks for (inference, Jev,
+/// search, index storage, delivery).
+pub(crate) const TINYMEMES: ModuleRecord = ModuleRecord {
+ id: "tinymemes",
+ description: "Final-reply slang and meme remixing through TinyMemes",
+ bus_name: tinymemes_bus::STATE_INTERFACE,
+ object_path: tinymemes_bus::STATE_PATH,
+ version: "0.4.1",
+ release_url: "https://github.com/tinyhumansai/tinymemes/releases/tag/v0.4.1",
+ assets: &[
+ PlatformAsset {
+ host_key: "macos-15-arm64",
+ archive: "tinymemes-module-0.4.1-macos-15-arm64.tar.gz",
+ sha256: "9ef332dfb479260567b4fbf12734d21d635cd6182b338474a5895cafa2093c19",
+ },
+ PlatformAsset {
+ host_key: "macos-15-x86_64",
+ archive: "tinymemes-module-0.4.1-macos-15-x86_64.tar.gz",
+ sha256: "dac379e4fa92152644b1e22191614e5424b58ad04cce9f3baadbb9c9e8463a56",
+ },
+ PlatformAsset {
+ host_key: "macos-26-arm64",
+ archive: "tinymemes-module-0.4.1-macos-26-arm64.tar.gz",
+ sha256: "7023133d6f88bceea185850da3a9c52a9a0a1ce0f3a065e1095087ac3303b564",
+ },
+ PlatformAsset {
+ host_key: "macos-26-x86_64",
+ archive: "tinymemes-module-0.4.1-macos-26-x86_64.tar.gz",
+ sha256: "372eb95b30bc7ce3accc893b3a7cdbe6812bfd2a7bbe65c62c19f07ef7d8f10c",
+ },
+ PlatformAsset {
+ host_key: "ubuntu-22.04-arm64",
+ archive: "tinymemes-module-0.4.1-ubuntu-22.04-arm64.tar.gz",
+ sha256: "92dc4a49608aaca0a83f6c50da4a6da5e726b60665a9424441e40cb1d616f652",
+ },
+ PlatformAsset {
+ host_key: "ubuntu-22.04-x86_64",
+ archive: "tinymemes-module-0.4.1-ubuntu-22.04-x86_64.tar.gz",
+ sha256: "55060d251d04b62683f0443994f9376b12295f3ac0848258a6faa202fffef781",
+ },
+ PlatformAsset {
+ host_key: "ubuntu-24.04-arm64",
+ archive: "tinymemes-module-0.4.1-ubuntu-24.04-arm64.tar.gz",
+ sha256: "c978dcd8e5ed355449785d94817cdaf009208cf4a1a4e7d8c8bc687bffcc0e40",
+ },
+ PlatformAsset {
+ host_key: "ubuntu-24.04-x86_64",
+ archive: "tinymemes-module-0.4.1-ubuntu-24.04-x86_64.tar.gz",
+ sha256: "8612106d61a42c59a7e74e0476705285cf0ad917aa5b6ea26e86c05f4d666c76",
+ },
+ PlatformAsset {
+ host_key: "windows-11-arm64",
+ archive: "tinymemes-module-0.4.1-windows-11-arm64.zip",
+ sha256: "bac64bbe575a7053232293051a6a45de6e4503d77a83da15de8b957123cf361b",
+ },
+ PlatformAsset {
+ host_key: "windows-2022-x86_64",
+ archive: "tinymemes-module-0.4.1-windows-2022-x86_64.zip",
+ sha256: "a00ddc84ca3232e04613a658bd2766b0bbe2fda77d7b07285706cf65d1a85a03",
+ },
+ PlatformAsset {
+ host_key: "windows-2025-x86_64",
+ archive: "tinymemes-module-0.4.1-windows-2025-x86_64.zip",
+ sha256: "8279337613b7984340ba3e0b1dfdfe17bd046e013b467152d21fa151933a1639",
+ },
+ ],
+ load: LoadPolicy::Lazy,
+ source: SourceSpec {
+ submodule: "tinymemes",
+ manifest: "crates/tinymemes-module/Cargo.toml",
+ package: "tinymemes-module",
+ lib_name: "tinymemes_module",
+ features: &[],
+ },
+};
diff --git a/crates/openhuman-core/src/tinymemes/arm.rs b/crates/openhuman-core/src/tinymemes/arm.rs
new file mode 100644
index 0000000000..1413031418
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/arm.rs
@@ -0,0 +1,126 @@
+//! Which turns get remixed: the `OPENHUMAN_TINYMEMES` flag and its A/B split.
+//!
+//! | value | behaviour |
+//! | --- | --- |
+//! | unset, `off`, `0`, `false` | disabled; no tinymemes work at all |
+//! | `on`, `1`, `true` | every thread is in the treatment arm |
+//! | `ab` | 50% of threads are treatment, the rest control |
+//! | `ab:NN` | NN% of threads are treatment |
+//!
+//! Assignment is by a stable hash of the thread id, so a thread keeps its arm
+//! for its whole life and across restarts. The flag is read on every turn, so
+//! it can change without a restart.
+//!
+//! A build can ship with a different default: set `OPENHUMAN_TINYMEMES_DEFAULT`
+//! (same values) in the environment of the `cargo build` that produces the
+//! release, and it is baked in. The runtime `OPENHUMAN_TINYMEMES` still wins
+//! whenever it is set, so a deployment can always be switched back off.
+
+pub(crate) const FLAG_ENV: &str = "OPENHUMAN_TINYMEMES";
+
+/// Default baked in at build time (`OPENHUMAN_TINYMEMES_DEFAULT` during
+/// `cargo build`); `None` means off.
+const BUILD_DEFAULT: Option<&str> = option_env!("OPENHUMAN_TINYMEMES_DEFAULT");
+
+/// Parsed flag value.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum Mode {
+ Off,
+ On,
+ Ab { percent: u8 },
+}
+
+/// The arm a thread is in.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum Arm {
+ /// Feature off: nothing runs and nothing is logged per turn.
+ Disabled,
+ /// In an experiment, not remixed. Logged for the throughput comparison.
+ Control,
+ /// Remixed.
+ Treatment,
+}
+
+impl Arm {
+ pub(crate) fn as_str(self) -> &'static str {
+ match self {
+ Arm::Disabled => "disabled",
+ Arm::Control => "control",
+ Arm::Treatment => "treatment",
+ }
+ }
+}
+
+pub(crate) fn mode_from(value: Option<&str>) -> Mode {
+ let Some(raw) = value.map(|v| v.trim().to_ascii_lowercase()) else {
+ return Mode::Off;
+ };
+ match raw.as_str() {
+ "" | "off" | "0" | "false" | "no" => Mode::Off,
+ "on" | "1" | "true" | "yes" => Mode::On,
+ "ab" => Mode::Ab { percent: 50 },
+ // Any all-digit percentage above 100 means 100, including one too
+ // large for `u64`.
+ other => match other.strip_prefix("ab:").map(str::trim) {
+ Some(digits) if !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()) => {
+ Mode::Ab {
+ percent: digits.parse::<u64>().map_or(100, |n| n.min(100)) as u8,
+ }
+ }
+ _ => {
+ log::warn!("[tinymemes] unrecognised {FLAG_ENV} value; treating as off");
+ Mode::Off
+ }
+ },
+ }
+}
+
+pub(crate) fn mode() -> Mode {
+ resolve_mode(std::env::var(FLAG_ENV).ok().as_deref(), BUILD_DEFAULT)
+}
+
+/// The runtime value when set, else the build-time default, else off.
+pub(crate) fn resolve_mode(runtime: Option<&str>, build_default: Option<&str>) -> Mode {
+ match runtime {
+ Some(value) => mode_from(Some(value)),
+ None => mode_from(build_default),
+ }
+}
+
+/// Stable 0..100 bucket for a thread (FNV-1a, so it does not change between
+/// builds or platforms the way `DefaultHasher` may).
+pub(crate) fn bucket(thread_id: &str) -> u8 {
+ let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
+ for byte in thread_id.as_bytes() {
+ hash ^= u64::from(*byte);
+ hash = hash.wrapping_mul(0x0100_0000_01b3);
+ }
+ (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,
+ Mode::On => Arm::Treatment,
+ Mode::Ab { percent } => {
+ if bucket(thread_id) < percent {
+ Arm::Treatment
+ } else {
+ Arm::Control
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "arm_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/arm_tests.rs b/crates/openhuman-core/src/tinymemes/arm_tests.rs
new file mode 100644
index 0000000000..a8612684ea
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/arm_tests.rs
@@ -0,0 +1,70 @@
+use super::*;
+
+#[test]
+fn flag_values_parse() {
+ assert_eq!(mode_from(None), Mode::Off);
+ assert_eq!(mode_from(Some("off")), Mode::Off);
+ assert_eq!(mode_from(Some(" ON ")), Mode::On);
+ assert_eq!(mode_from(Some("1")), Mode::On);
+ assert_eq!(mode_from(Some("ab")), Mode::Ab { percent: 50 });
+ assert_eq!(mode_from(Some("ab:20")), Mode::Ab { percent: 20 });
+ assert_eq!(mode_from(Some("ab:300")), Mode::Ab { percent: 100 });
+ assert_eq!(mode_from(Some("ab:99999")), Mode::Ab { percent: 100 });
+ assert_eq!(
+ mode_from(Some("ab:18446744073709551616")),
+ Mode::Ab { percent: 100 }
+ );
+ assert_eq!(mode_from(Some("ab:")), Mode::Off);
+ assert_eq!(mode_from(Some("ab:-5")), Mode::Off);
+ assert_eq!(mode_from(Some("ab:250")), Mode::Ab { percent: 100 });
+ assert_eq!(mode_from(Some("ab:100")), Mode::Ab { percent: 100 });
+ assert_eq!(mode_from(Some("sometimes")), Mode::Off);
+}
+
+#[test]
+fn assignment_is_stable_and_respects_the_split() {
+ assert_eq!(assign(Mode::Off, "t"), Arm::Disabled);
+ assert_eq!(assign(Mode::On, "t"), Arm::Treatment);
+ assert_eq!(assign(Mode::Ab { percent: 0 }, "t"), Arm::Control);
+ assert_eq!(assign(Mode::Ab { percent: 100 }, "t"), Arm::Treatment);
+
+ let a = assign(Mode::Ab { percent: 50 }, "thread-abc");
+ for _ in 0..10 {
+ assert_eq!(assign(Mode::Ab { percent: 50 }, "thread-abc"), a);
+ }
+
+ let treated = (0..2000)
+ .filter(|i| assign(Mode::Ab { percent: 30 }, &format!("thread-{i}")) == Arm::Treatment)
+ .count();
+ assert!((450..750).contains(&treated), "treated={treated}");
+}
+
+#[test]
+fn bucket_is_fixed_across_builds() {
+ // FNV-1a is specified, so this value must never change; a change would
+ // silently move live threads between arms.
+ assert_eq!(bucket("thread-abc"), 56);
+ assert_eq!(bucket(""), (0xcbf2_9ce4_8422_2325_u64 % 100) as u8);
+}
+
+#[test]
+fn runtime_flag_overrides_the_build_default() {
+ assert_eq!(resolve_mode(None, None), Mode::Off);
+ assert_eq!(resolve_mode(None, Some("on")), Mode::On);
+ assert_eq!(resolve_mode(None, Some("ab:30")), Mode::Ab { percent: 30 });
+ // A deployment can always switch a shipped default back off.
+ assert_eq!(resolve_mode(Some("off"), Some("on")), Mode::Off);
+ assert_eq!(
+ resolve_mode(Some("ab"), Some("on")),
+ Mode::Ab { percent: 50 }
+ );
+}
+
+#[test]
+fn arms_have_log_names() {
+ assert_eq!(Arm::Disabled.as_str(), "disabled");
+ assert_eq!(Arm::Control.as_str(), "control");
+ assert_eq!(Arm::Treatment.as_str(), "treatment");
+ // The live flag resolves without panicking whatever the environment holds.
+ let _ = mode();
+}
diff --git a/crates/openhuman-core/src/tinymemes/callbacks.rs b/crates/openhuman-core/src/tinymemes/callbacks.rs
new file mode 100644
index 0000000000..cf2a2b9c64
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/callbacks.rs
@@ -0,0 +1,305 @@
+//! What the module asks of the host while it works on a remix: one
+//! completion, one Jev evaluation, one web search, and the learned-state
+//! documents ([`super::docs`]).
+//!
+//! Every callback carries the correlation id of the remix it belongs to (the
+//! module echoes the one the host put in the request). A [`Registry`] entry
+//! per id holds that remix's workspace, so remixes for different workspaces
+//! run side by side and a callback with no live id is refused. An entry lives
+//! until the module reports the request finished with `End` (background
+//! research can outlast the call), or is swept after [`MAX_AGE`].
+
+use std::collections::HashMap;
+use std::future::Future;
+use std::sync::{Arc, LazyLock, Mutex};
+use std::time::{Duration, Instant};
+
+use serde::de::DeserializeOwned;
+use serde_json::Value;
+use tinyinference_decisions::{Client, EvaluationRequest};
+use tinymemes_bus::{
+ host_methods, CompleteRequest, EvaluateEnvelope, HostFailure, HostResult, IndexKind,
+ LoadIndexRequest, SaveIndexRequest, SearchRequest,
+};
+
+use super::{docs, inference, jev, search};
+use crate::config::Config;
+use crate::core::runtime::{CoreContext, DispatchCtx};
+
+/// How long an entry may live without its `End`.
+const MAX_AGE: Duration = Duration::from_secs(15 * 60);
+
+/// The workspace one remix runs for, and who it runs for.
+///
+/// Callbacks arrive on the module bus, outside the task that asked for the
+/// remix, so the caller's context is captured when the remix is registered and
+/// re-entered for each callback ([`Session::enter`]). A remix acts for the
+/// host, never for an agent, so there is no memory identity to carry.
+/// Only the caller's own scope counts, so in SaaS an unscoped caller's
+/// callbacks fail closed instead of acting as the operator.
+pub(super) struct Session {
+ config: Config,
+ jev: tokio::sync::OnceCell<Option<Arc<Client>>>,
+ context: Option<Arc<CoreContext>>,
+}
+
+impl Session {
+ /// The dispatch context the registering caller runs under; the host's own
+ /// when it had none (a single-user process), which fails closed in SaaS.
+ pub(super) fn dispatch_ctx(&self) -> DispatchCtx {
+ match self.context.clone() {
+ Some(context) => DispatchCtx::new(context),
+ None => DispatchCtx::host_default(),
+ }
+ }
+
+ pub(super) fn new(ctx: &DispatchCtx, config: &Config) -> Self {
+ Self {
+ config: config.clone(),
+ jev: tokio::sync::OnceCell::new(),
+ // Only the caller's own context is carried. Without one, a
+ // single-user process falls back to its default context on the
+ // callback side as anywhere else, and SaaS fails closed.
+ context: ctx.core().cloned(),
+ }
+ }
+
+ /// Run `fut` as the registering caller.
+ pub(super) async fn enter<F: Future>(&self, fut: F) -> F::Output {
+ let fut = Box::pin(fut);
+ match self.context.clone() {
+ Some(context) => CoreContext::scope(context, fut).await,
+ None => fut.await,
+ }
+ }
+}
+
+struct Entry {
+ session: Arc<Session>,
+ registered: Instant,
+}
+
+static REGISTRY: LazyLock<Mutex<HashMap<String, Entry>>> = LazyLock::new(Mutex::default);
+
+fn registry() -> std::sync::MutexGuard<'static, HashMap<String, Entry>> {
+ REGISTRY.lock().unwrap_or_else(|e| e.into_inner())
+}
+
+/// A registered remix. Dropping it forgets the remix unless it was
+/// [`kept`](Self::keep_for_background) for research that runs on after the
+/// call returns.
+pub(super) struct Registration {
+ correlation: String,
+ keep: bool,
+}
+
+impl Registration {
+ /// The id to put in the request.
+ pub(super) fn correlation(&self) -> &str {
+ &self.correlation
+ }
+
+ /// The call succeeded: leave the entry for the module's `End`.
+ pub(super) fn keep_for_background(mut self) {
+ self.keep = true;
+ }
+}
+
+impl Drop for Registration {
+ fn drop(&mut self) {
+ if !self.keep {
+ registry().remove(&self.correlation);
+ }
+ }
+}
+
+/// Register a remix for `config`'s workspace under a fresh correlation id.
+pub(super) fn register(ctx: &DispatchCtx, config: &Config) -> Registration {
+ let correlation = uuid::Uuid::new_v4().to_string();
+ let mut sessions = registry();
+ sessions.retain(|_, entry| entry.registered.elapsed() < MAX_AGE);
+ sessions.insert(
+ correlation.clone(),
+ Entry {
+ session: Arc::new(Session::new(ctx, config)),
+ registered: Instant::now(),
+ },
+ );
+ Registration {
+ correlation,
+ keep: false,
+ }
+}
+
+/// The session a callback answers for, if its remix is still registered.
+pub(super) fn session(correlation: &str) -> Option<Arc<Session>> {
+ registry()
+ .get(correlation)
+ .map(|entry| entry.session.clone())
+}
+
+/// The module finished this remix: forget it.
+pub(super) fn end(correlation: &str) {
+ if registry().remove(correlation).is_some() {
+ log::debug!("[tinymemes] remix ended correlation={correlation}");
+ }
+}
+
+/// How many registered remixes belong to `workspace`; tests use a unique
+/// workspace so parallel tests cannot disturb the count.
+#[cfg(test)]
+pub(super) fn live_for(workspace: &std::path::Path) -> usize {
+ registry()
+ .values()
+ .filter(|entry| entry.session.config.workspace_dir == workspace)
+ .count()
+}
+
+/// The correlation id a callback payload carries (`[ { "correlation": .. } ]`).
+pub(super) fn correlation_of(payload: &Value) -> Option<&str> {
+ payload
+ .as_array()?
+ .first()?
+ .get("correlation")?
+ .as_str()
+ .filter(|c| !c.is_empty())
+}
+
+fn args<T: DeserializeOwned>(value: Value) -> Result<T, tinybus::Error> {
+ let (request,): (T,) = serde_json::from_value(value)
+ .map_err(|_| tinybus::Error::failed("invalid tinymemes host request"))?;
+ Ok(request)
+}
+
+fn reply<T: serde::Serialize>(result: HostResult<T>) -> Result<Value, tinybus::Error> {
+ serde_json::to_value(result)
+ .map_err(|_| tinybus::Error::failed("invalid tinymemes host response"))
+}
+
+/// Answer one host callback for `session`.
+pub(super) async fn dispatch(
+ session: &Session,
+ member: &str,
+ payload: Value,
+) -> Result<Value, tinybus::Error> {
+ log::debug!("[tinymemes] host callback member={member}");
+ session.enter(answer(session, member, payload)).await
+}
+
+async fn answer(session: &Session, member: &str, payload: Value) -> Result<Value, tinybus::Error> {
+ match member {
+ host_methods::COMPLETE => reply(complete(session, args(payload)?).await),
+ host_methods::EVALUATE => reply(evaluate(session, args(payload)?).await),
+ host_methods::SEARCH => reply(search(session, args(payload)?).await),
+ host_methods::LOAD_INDEX => reply(load_index(session, args(payload)?).await),
+ host_methods::SAVE_INDEX => reply(save_index(session, args(payload)?).await),
+ // The remixed text is returned to the caller, which delivers it
+ // itself; nothing is handed over through the module.
+ host_methods::DELIVER => reply(HostResult::Ok(())),
+ host_methods::END => {
+ if let Some(correlation) = correlation_of(&payload) {
+ end(correlation);
+ }
+ reply(HostResult::Ok(()))
+ }
+ _ => Err(tinybus::Error::failed("unknown tinymemes host operation")),
+ }
+}
+
+async fn complete(session: &Session, request: CompleteRequest) -> HostResult<String> {
+ inference::complete(
+ &session.dispatch_ctx(),
+ &session.config,
+ &request.system,
+ &request.user,
+ )
+ .await
+ .map_err(|error| {
+ log::warn!("[tinymemes] complete callback failed: {error}");
+ HostFailure::Backend
+ })
+}
+
+async fn evaluate(session: &Session, envelope: EvaluateEnvelope) -> HostResult<EvaluateEnvelope> {
+ let Some(client) = session
+ .jev
+ .get_or_init(|| async { jev::managed(&session.config) })
+ .await
+ else {
+ log::debug!("[tinymemes] evaluate callback: managed jev unavailable");
+ return Err(HostFailure::Unsupported);
+ };
+ let request: EvaluationRequest =
+ serde_json::from_value(envelope.body).map_err(|_| HostFailure::Unsupported)?;
+ let result = client.evaluate(&request).await.map_err(|error| {
+ log::warn!("[tinymemes] evaluate callback failed: {error}");
+ HostFailure::Backend
+ })?;
+ let body = serde_json::to_value(result.response).map_err(|_| HostFailure::Backend)?;
+ Ok(EvaluateEnvelope {
+ correlation: String::new(),
+ body,
+ })
+}
+
+async fn search(
+ session: &Session,
+ request: SearchRequest,
+) -> HostResult<Vec<tinymemes_bus::SearchResult>> {
+ let Some(backend) =
+ search::OpenHumanSearch::available(&session.dispatch_ctx(), &session.config)
+ else {
+ return Err(HostFailure::Unsupported);
+ };
+ backend
+ .search(&request.query, request.max_results as usize)
+ .await
+ .map_err(|error| {
+ // The query is derived from the chat, so only the error is logged.
+ log::warn!("[tinymemes] search callback failed: {error}");
+ HostFailure::Backend
+ })
+}
+
+async fn load_index(session: &Session, request: LoadIndexRequest) -> HostResult<Option<String>> {
+ let config = session.config.clone();
+ let kind: IndexKind = request.kind;
+ match session
+ .dispatch_ctx()
+ .spawn_blocking(move || docs::load(&config, kind))
+ .await
+ {
+ Ok(Ok(document)) => Ok(document),
+ Ok(Err(error)) => {
+ log::warn!("[tinymemes] cannot read index: {error}");
+ Err(HostFailure::Backend)
+ }
+ Err(error) => {
+ log::warn!("[tinymemes] index read task failed: {error}");
+ Err(HostFailure::Backend)
+ }
+ }
+}
+
+async fn save_index(session: &Session, request: SaveIndexRequest) -> HostResult<()> {
+ let config = session.config.clone();
+ match session
+ .dispatch_ctx()
+ .spawn_blocking(move || docs::save(&config, request.kind, request.json))
+ .await
+ {
+ Ok(Ok(())) => Ok(()),
+ Ok(Err(error)) => {
+ log::warn!("[tinymemes] cannot save index: {error}");
+ Err(HostFailure::Backend)
+ }
+ Err(error) => {
+ log::warn!("[tinymemes] index write task failed: {error}");
+ Err(HostFailure::Backend)
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "callbacks_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/callbacks_tests.rs b/crates/openhuman-core/src/tinymemes/callbacks_tests.rs
new file mode 100644
index 0000000000..c89614746e
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/callbacks_tests.rs
@@ -0,0 +1,187 @@
+use super::*;
+use serde_json::json;
+
+fn config_in(dir: &std::path::Path) -> Config {
+ let mut config = Config::default();
+ config.config_path = dir.join("config.toml");
+ config.workspace_dir = dir.join("workspace");
+ config.secrets.encrypt = false;
+ config
+}
+
+fn decode<T: DeserializeOwned>(value: Value) -> HostResult<T> {
+ serde_json::from_value(value).unwrap()
+}
+
+#[tokio::test]
+async fn an_index_saved_through_the_host_loads_back_per_kind() {
+ let dir = tempfile::tempdir().unwrap();
+ let session = Session::new(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+
+ let missing = dispatch(
+ &session,
+ host_methods::LOAD_INDEX,
+ json!([{ "kind": "slang" }]),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<Option<String>>(missing), Ok(None));
+
+ for (kind, body) in [
+ ("slang", "{\"terms\":1}"),
+ ("memes", "{\"memes\":2}"),
+ ("remixed", "[\"m1\"]"),
+ ] {
+ let saved = dispatch(
+ &session,
+ host_methods::SAVE_INDEX,
+ json!([{ "kind": kind, "json": body }]),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<()>(saved), Ok(()));
+ }
+ for (kind, body) in [
+ ("slang", "{\"terms\":1}"),
+ ("memes", "{\"memes\":2}"),
+ ("remixed", "[\"m1\"]"),
+ ] {
+ let loaded = dispatch(
+ &session,
+ host_methods::LOAD_INDEX,
+ json!([{ "kind": kind }]),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<Option<String>>(loaded), Ok(Some(body.into())));
+ }
+}
+
+#[tokio::test]
+async fn without_managed_jev_evaluate_is_unsupported() {
+ let dir = tempfile::tempdir().unwrap();
+ let session = Session::new(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let request = json!([{ "body": { "questions": {}, "messages": [] } }]);
+ let result = dispatch(&session, host_methods::EVALUATE, request)
+ .await
+ .unwrap();
+ assert_eq!(
+ decode::<EvaluateEnvelope>(result),
+ Err(HostFailure::Unsupported)
+ );
+}
+
+#[tokio::test]
+async fn search_switched_off_is_unsupported() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = config_in(dir.path());
+ config.search.enabled = Some(false);
+ let session = Session::new(&crate::core::runtime::DispatchCtx::host_default(), &config);
+ let result = dispatch(
+ &session,
+ host_methods::SEARCH,
+ json!([{ "query": "q", "max_results": 3 }]),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ decode::<Vec<tinymemes_bus::SearchResult>>(result),
+ Err(HostFailure::Unsupported)
+ );
+}
+
+#[tokio::test]
+async fn deliver_is_acknowledged_and_unknown_members_are_refused() {
+ let dir = tempfile::tempdir().unwrap();
+ let session = Session::new(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let delivered = dispatch(
+ &session,
+ host_methods::DELIVER,
+ json!([{ "reply": "hi", "remixed": true }]),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<()>(delivered), Ok(()));
+ assert!(dispatch(&session, "Nope", json!([])).await.is_err());
+ assert!(dispatch(&session, host_methods::COMPLETE, json!("bad"))
+ .await
+ .is_err());
+}
+
+#[test]
+fn registrations_are_independent_and_forgotten_on_drop() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ let a = register(&crate::core::runtime::DispatchCtx::host_default(), &config);
+ let b = register(&crate::core::runtime::DispatchCtx::host_default(), &config);
+ assert_ne!(a.correlation(), b.correlation());
+ assert!(session(a.correlation()).is_some() && session(b.correlation()).is_some());
+ let a_id = a.correlation().to_owned();
+ drop(a);
+ assert!(
+ session(&a_id).is_none(),
+ "a failed call leaves nothing behind"
+ );
+ assert!(session(b.correlation()).is_some());
+ assert!(session("").is_none() && session("unknown").is_none());
+}
+
+#[test]
+fn a_kept_registration_lives_until_the_module_reports_the_end() {
+ let dir = tempfile::tempdir().unwrap();
+ let registration = register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let id = registration.correlation().to_owned();
+ registration.keep_for_background();
+ assert!(
+ session(&id).is_some(),
+ "background research may still call back"
+ );
+ end(&id);
+ assert!(session(&id).is_none());
+ end(&id);
+}
+
+#[test]
+fn the_correlation_id_is_read_from_the_first_argument_only() {
+ assert_eq!(
+ correlation_of(&json!([{ "correlation": "c1", "kind": "slang" }])),
+ Some("c1")
+ );
+ for bad in [
+ json!([{ "correlation": "" }]),
+ json!([{ "correlation": 7 }]),
+ json!([{}]),
+ json!([]),
+ json!({ "correlation": "c1" }),
+ json!("c1"),
+ ] {
+ assert_eq!(correlation_of(&bad), None, "{bad}");
+ }
+}
+
+#[tokio::test]
+async fn a_remix_session_answers_under_the_context_that_registered_it() {
+ use crate::core::runtime::{CoreContext, DispatchCtx, DomainSet};
+ let dir = tempfile::tempdir().unwrap();
+ let caller = CoreContext::for_test(DomainSet::full(), None);
+ let other = CoreContext::for_test(DomainSet::full(), None);
+ let session = Session::new(
+ &DispatchCtx::new(Arc::clone(&caller)),
+ &config_in(dir.path()),
+ );
+ // A callback arrives on the module bus while some other context is active.
+ let seen = CoreContext::scope(Arc::clone(&other), async { session.dispatch_ctx() }).await;
+ assert!(Arc::ptr_eq(seen.core().expect("bound"), &caller));
+}
diff --git a/crates/openhuman-core/src/tinymemes/docs.rs b/crates/openhuman-core/src/tinymemes/docs.rs
new file mode 100644
index 0000000000..35a95dac42
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/docs.rs
@@ -0,0 +1,117 @@
+//! The learned-state documents the module asks the host to keep: the slang
+//! index, the meme index and the ring of remixed message ids.
+//!
+//! They live on the storage document port ([`crate::storage`]) under the
+//! acting scope, in `memes.state`, one document per [`IndexKind`]. The host
+//! never interprets them: the formats, the ring and its cap belong to the
+//! module, and the text-to-body mapping to `tinymemes_bus::storage`
+//! (`state_body` / `state_text`), which also declares the step that brought
+//! the v1 `tinymemes_documents` rows (`{"json": "<text>"}`) here.
+//!
+//! Older installs kept them as `<workspace>/tinymemes/{slang-index,
+//! meme-index,remixed}.json`. The first load of a kind with no stored document
+//! adopts that file's content once and renames the file to `<name>.migrated`,
+//! so nothing is lost and the file is never read again.
+
+use std::path::{Path, PathBuf};
+
+use anyhow::{anyhow, Result};
+use tinymemes_bus::storage::{state_body, state_id, state_text, STATE_COLLECTION as COLLECTION};
+use tinymemes_bus::IndexKind;
+use tinystoragedrivers::{CollectionSpec, Precondition};
+
+use crate::config::Config;
+use crate::storage::documents::Repo;
+
+const DOMAIN: &str = "tinymemes::docs";
+/// Directory of the pre-storage files, under the workspace.
+const LEGACY_DIR: &str = "tinymemes";
+
+fn collections() -> Vec<CollectionSpec> {
+ vec![CollectionSpec::new(COLLECTION)]
+}
+
+fn doc_id(kind: IndexKind) -> &'static str {
+ state_id(kind)
+}
+
+fn legacy_file(kind: IndexKind) -> &'static str {
+ match kind {
+ IndexKind::Slang => "slang-index.json",
+ IndexKind::Memes => "meme-index.json",
+ IndexKind::Remixed => "remixed.json",
+ }
+}
+
+fn legacy_path(workspace_dir: &Path, kind: IndexKind) -> PathBuf {
+ workspace_dir.join(LEGACY_DIR).join(legacy_file(kind))
+}
+
+fn repo(config: &Config) -> Result<Repo> {
+ Repo::current_for(&config.workspace_dir, DOMAIN, collections)
+}
+
+/// The stored document for `kind`, adopting the legacy file once.
+pub(super) fn load(config: &Config, kind: IndexKind) -> Result<Option<String>> {
+ let repo = repo(config)?;
+ let id = doc_id(kind);
+ let stored = repo.run(move |docs| async move { docs.get(COLLECTION, id).await })?;
+ if let Some(stored) = stored {
+ return Ok(Some(state_text(kind, &stored.doc)));
+ }
+ let path = legacy_path(&config.workspace_dir, kind);
+ // Only an absent file means "nothing to adopt". Any other failure
+ // (permissions, bad UTF-8) is an error: answering `None` would let the
+ // module start empty and save over state the old file still holds.
+ let legacy = match std::fs::read_to_string(&path) {
+ Ok(legacy) => legacy,
+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
+ Err(e) => return Err(anyhow!("read {}: {e}", path.display())),
+ };
+ // `Absent`: a concurrent save wins over the old file's content.
+ let adopted = state_body(kind, &legacy);
+ let written = repo.run(move |docs| async move {
+ docs.put(COLLECTION, id, adopted, Precondition::Absent)
+ .await
+ .map(|_| ())
+ });
+ match written {
+ Ok(()) => {
+ let migrated = path.with_extension("json.migrated");
+ match std::fs::rename(&path, &migrated) {
+ Ok(()) => log::info!(
+ "[tinymemes] adopted {} into storage (kept as {})",
+ legacy_file(kind),
+ migrated.display()
+ ),
+ Err(e) => log::warn!("[tinymemes] cannot retire {}: {e}", path.display()),
+ }
+ Ok(Some(legacy))
+ }
+ // Somebody stored a document in between; theirs is the truth. When no
+ // document exists the put failed for a real reason, which is reported.
+ Err(error) => {
+ let stored = repo.run(move |docs| async move { docs.get(COLLECTION, id).await })?;
+ match stored {
+ Some(stored) => Ok(Some(state_text(kind, &stored.doc))),
+ None => Err(error),
+ }
+ }
+ }
+}
+
+/// Replace the stored document for `kind`.
+pub(super) fn save(config: &Config, kind: IndexKind, json: String) -> Result<()> {
+ let repo = repo(config)?;
+ let id = doc_id(kind);
+ repo.run(move |docs| async move {
+ docs.put(COLLECTION, id, state_body(kind, &json), Precondition::None)
+ .await
+ .map(|_| ())
+ })
+ .map_err(|e| anyhow!("save {id}: {e}"))
+}
+
+#[cfg(test)]
+#[path = "docs_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/docs_tests.rs b/crates/openhuman-core/src/tinymemes/docs_tests.rs
new file mode 100644
index 0000000000..d8187b737f
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/docs_tests.rs
@@ -0,0 +1,119 @@
+use super::*;
+
+fn config_in(dir: &Path) -> Config {
+ let mut config = Config::default();
+ config.config_path = dir.join("config.toml");
+ config.workspace_dir = dir.join("workspace");
+ config.secrets.encrypt = false;
+ config
+}
+
+fn write_legacy(config: &Config, kind: IndexKind, content: &str) {
+ let path = legacy_path(&config.workspace_dir, kind);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(path, content).unwrap();
+}
+
+#[test]
+fn a_saved_document_loads_back_and_kinds_are_separate() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ assert_eq!(load(&config, IndexKind::Slang).unwrap(), None);
+ save(&config, IndexKind::Slang, "{\"terms\":1}".into()).unwrap();
+ assert_eq!(
+ load(&config, IndexKind::Slang).unwrap().as_deref(),
+ Some("{\"terms\":1}")
+ );
+ assert_eq!(load(&config, IndexKind::Memes).unwrap(), None);
+ assert_eq!(load(&config, IndexKind::Remixed).unwrap(), None);
+ save(&config, IndexKind::Slang, "{\"terms\":2}".into()).unwrap();
+ assert_eq!(
+ load(&config, IndexKind::Slang).unwrap().as_deref(),
+ Some("{\"terms\":2}")
+ );
+}
+
+#[test]
+fn the_legacy_remixed_json_is_adopted_once_and_retired() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ write_legacy(&config, IndexKind::Remixed, "[\"m1\",\"m2\"]");
+
+ assert_eq!(
+ load(&config, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"m1\",\"m2\"]")
+ );
+ let legacy = legacy_path(&config.workspace_dir, IndexKind::Remixed);
+ assert!(!legacy.exists(), "the old file is retired");
+ assert!(legacy.with_extension("json.migrated").is_file());
+
+ // The module's later saves are the truth; a stray file is not re-adopted.
+ save(&config, IndexKind::Remixed, "[\"m1\",\"m2\",\"m3\"]".into()).unwrap();
+ write_legacy(&config, IndexKind::Remixed, "[\"stale\"]");
+ assert_eq!(
+ load(&config, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"m1\",\"m2\",\"m3\"]")
+ );
+}
+
+#[test]
+fn every_legacy_index_file_is_adopted() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ write_legacy(&config, IndexKind::Slang, "{\"slang\":true}");
+ write_legacy(&config, IndexKind::Memes, "{\"memes\":true}");
+ assert_eq!(
+ load(&config, IndexKind::Slang).unwrap().as_deref(),
+ Some("{\"slang\":true}")
+ );
+ assert_eq!(
+ load(&config, IndexKind::Memes).unwrap().as_deref(),
+ Some("{\"memes\":true}")
+ );
+}
+
+#[test]
+fn a_stored_document_wins_over_a_legacy_file() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ save(&config, IndexKind::Memes, "{\"new\":1}".into()).unwrap();
+ write_legacy(&config, IndexKind::Memes, "{\"old\":1}");
+ assert_eq!(
+ load(&config, IndexKind::Memes).unwrap().as_deref(),
+ Some("{\"new\":1}")
+ );
+ assert!(legacy_path(&config.workspace_dir, IndexKind::Memes).exists());
+}
+
+#[cfg(unix)]
+#[test]
+fn an_unreadable_legacy_file_is_an_error_not_an_empty_start() {
+ use std::os::unix::fs::PermissionsExt;
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ write_legacy(&config, IndexKind::Remixed, "[\"keep\"]");
+ let path = legacy_path(&config.workspace_dir, IndexKind::Remixed);
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o000)).unwrap();
+ if std::fs::read_to_string(&path).is_ok() {
+ // Running as a user who can read anything (root): nothing to assert.
+ return;
+ }
+ assert!(load(&config, IndexKind::Remixed).is_err());
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
+ assert_eq!(
+ load(&config, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"keep\"]"),
+ "the state is still there once the file can be read"
+ );
+}
+
+#[test]
+fn an_invalid_utf8_legacy_file_is_an_error() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ let path = legacy_path(&config.workspace_dir, IndexKind::Slang);
+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();
+ std::fs::write(&path, [0xff, 0xfe, 0x00]).unwrap();
+ assert!(load(&config, IndexKind::Slang).is_err());
+ assert!(path.exists(), "an unreadable file is left where it is");
+}
diff --git a/crates/openhuman-core/src/tinymemes/e2e_tests.rs b/crates/openhuman-core/src/tinymemes/e2e_tests.rs
new file mode 100644
index 0000000000..e99743ee44
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/e2e_tests.rs
@@ -0,0 +1,150 @@
+//! The host and the real compiled `tinymemes` module over the bus.
+//!
+//! Runs where a module library exists (`node scripts/modules/build.mjs
+//! tinymemes` with `OPENHUMAN_MODULE_SOURCE_DIR`, or the released modules the CI
+//! installer stages under `OPENHUMAN_MODULE_PATH`); skipped, with a message,
+//! otherwise. The library is loaded through a path override, the same way a
+//! developer points a host at a local build.
+
+use super::*;
+use service::MODULE_ID;
+use tinymemes_bus::IndexKind;
+
+fn config_in(dir: &std::path::Path, artifact: &std::path::Path) -> Config {
+ let mut config = Config::default();
+ config.config_path = dir.join("config.toml");
+ config.workspace_dir = dir.join("workspace");
+ config.secrets.encrypt = false;
+ config.components.memes = true;
+ config.modules.allow_download = false;
+ config
+ .modules
+ .overrides
+ .push(crate::config::schema::ModuleOverride::with_path(
+ MODULE_ID,
+ artifact.to_str().unwrap(),
+ ));
+ config
+}
+
+fn artifact() -> Option<std::path::PathBuf> {
+ let artifact = crate::modules::local_artifact(MODULE_ID);
+ if artifact.is_none() {
+ eprintln!(
+ "SKIPPED (not run, not asserted): no tinymemes library. Run \
+ `node scripts/modules/build.mjs tinymemes` and set OPENHUMAN_MODULE_SOURCE_DIR, \
+ or point OPENHUMAN_MODULE_PATH at the released modules"
+ );
+ }
+ artifact
+}
+
+fn request(reply: &str) -> RemixRequest {
+ serde_json::from_value(serde_json::json!({
+ "conversation": [{ "role": "user", "text": "ok the report is done", "id": "u-1" }],
+ "reply": reply,
+ "region": "global",
+ "timeout_ms": 20_000,
+ "reply_id": "run-1:reply",
+ }))
+ .unwrap()
+}
+
+fn write_legacy(config: &Config, kind_file: &str, content: &str) {
+ let dir = config.workspace_dir.join("tinymemes");
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::write(dir.join(kind_file), content).unwrap();
+}
+
+/// With no Jev, no inference provider and no search on this host, the module
+/// still answers: it asks, is told it is unsupported, and fails open to the
+/// original reply. Its document reads reach the host's callbacks with its
+/// correlation id, which is visible as the legacy file being adopted.
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn the_real_module_fails_open_and_reads_its_documents_through_the_host() {
+ let Some(artifact) = artifact() else { return };
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path(), &artifact);
+ write_legacy(&config, "remixed.json", "[\"old-1\"]");
+
+ let response = service::remix(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ request("That's done."),
+ )
+ .await
+ .expect("the module answered");
+ assert!(!response.remixed, "{response:?}");
+ assert_eq!(response.reply, "That's done.");
+ // The module reported the request finished, so nothing is kept for it.
+ assert_eq!(callbacks::live_for(&config.workspace_dir), 0);
+
+ let legacy = config.workspace_dir.join("tinymemes/remixed.json");
+ assert!(
+ !legacy.exists(),
+ "the module's LoadIndex adopted the old file"
+ );
+ assert_eq!(
+ docs::load(&config, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"old-1\"]")
+ );
+}
+
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn a_disabled_request_passes_through_without_a_callback() {
+ let Some(artifact) = artifact() else { return };
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path(), &artifact);
+ write_legacy(&config, "remixed.json", "[\"untouched\"]");
+ let mut disabled = request("hello");
+ disabled.enabled = false;
+ let response = service::remix(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ disabled,
+ )
+ .await
+ .unwrap();
+ assert_eq!(response.reply, "hello");
+ assert_eq!(response.skipped, Some(SkipReason::Disabled));
+ assert_eq!(
+ callbacks::live_for(&config.workspace_dir),
+ 0,
+ "a disabled request keeps no registration"
+ );
+ assert!(
+ config.workspace_dir.join("tinymemes/remixed.json").exists(),
+ "a disabled request reads nothing"
+ );
+}
+
+/// Two workspaces remix at once. Each callback is answered for the workspace
+/// of its own correlation id, and neither waits behind a process-wide gate.
+#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
+async fn concurrent_remixes_for_two_workspaces_stay_in_their_own_documents() {
+ let Some(artifact) = artifact() else { return };
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let (a, b) = (
+ config_in(dir_a.path(), &artifact),
+ config_in(dir_b.path(), &artifact),
+ );
+ write_legacy(&a, "remixed.json", "[\"from-a\"]");
+ write_legacy(&b, "remixed.json", "[\"from-b\"]");
+
+ let host = crate::core::runtime::DispatchCtx::host_default();
+ let (ra, rb) = tokio::join!(
+ service::remix(&host, &a, request("reply a")),
+ service::remix(&host, &b, request("reply b"))
+ );
+ assert_eq!(ra.unwrap().reply, "reply a");
+ assert_eq!(rb.unwrap().reply, "reply b");
+
+ assert_eq!(
+ docs::load(&a, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"from-a\"]")
+ );
+ assert_eq!(
+ docs::load(&b, IndexKind::Remixed).unwrap().as_deref(),
+ Some("[\"from-b\"]")
+ );
+}
diff --git a/crates/openhuman-core/src/tinymemes/history.rs b/crates/openhuman-core/src/tinymemes/history.rs
new file mode 100644
index 0000000000..b448088271
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/history.rs
@@ -0,0 +1,57 @@
+//! The thread as the user saw it, in the contract's vocabulary.
+
+use tinymemes_bus::{WireRole, WireTurn};
+
+use crate::threads::store::ConversationMessage;
+
+/// Whether a finished reply is worth remixing at all.
+pub(super) fn should_remix(reply: &str, placeholder: &str) -> bool {
+ reply != placeholder && !reply.trim().is_empty()
+}
+
+/// The thread as the user saw it, oldest first: delivered replies (remixed or
+/// not) and the user's messages. `current_user_message` is appended unless it
+/// is already the last stored turn (the store may have persisted it first).
+pub(super) fn history_turns(
+ messages: &[ConversationMessage],
+ current_user_message: &str,
+) -> Vec<WireTurn> {
+ let mut turns: Vec<WireTurn> = messages
+ .iter()
+ .filter(|m| !m.content.trim().is_empty())
+ .filter_map(|m| match m.sender.as_str() {
+ "user" => Some(WireTurn {
+ role: WireRole::User,
+ text: m.content.clone(),
+ remixed: false,
+ id: Some(m.id.clone()),
+ }),
+ "agent" | "assistant" => Some(WireTurn {
+ role: WireRole::Assistant,
+ text: m.content.clone(),
+ // Whether it went out remixed is the module's to say: it keeps
+ // the ring of remixed ids and matches them by this id.
+ remixed: false,
+ id: Some(m.id.clone()),
+ }),
+ _ => None,
+ })
+ .collect();
+ let current = current_user_message.trim();
+ let already_there = turns
+ .last()
+ .is_some_and(|t| t.role == WireRole::User && t.text.trim() == current);
+ if !current.is_empty() && !already_there {
+ turns.push(WireTurn {
+ role: WireRole::User,
+ text: current.to_owned(),
+ remixed: false,
+ id: None,
+ });
+ }
+ turns
+}
+
+#[cfg(test)]
+#[path = "history_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/history_tests.rs b/crates/openhuman-core/src/tinymemes/history_tests.rs
new file mode 100644
index 0000000000..3634380ac8
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/history_tests.rs
@@ -0,0 +1,56 @@
+use super::*;
+
+fn msg(id: &str, sender: &str, content: &str) -> ConversationMessage {
+ ConversationMessage {
+ id: id.to_owned(),
+ content: content.to_owned(),
+ message_type: "text".to_owned(),
+ extra_metadata: serde_json::Value::Null,
+ sender: sender.to_owned(),
+ created_at: "2026-10-09T00:00:00Z".to_owned(),
+ }
+}
+
+#[test]
+fn only_real_answers_are_remixed() {
+ assert!(should_remix("a real answer", "budget exhausted"));
+ assert!(!should_remix("budget exhausted", "budget exhausted"));
+ assert!(!should_remix(" \n", "budget exhausted"));
+}
+
+#[test]
+fn history_carries_message_ids_and_appends_the_pending_message() {
+ let messages = vec![
+ msg("u1", "user", "bhai build fail"),
+ msg("r1", "agent", "Arre yaar, moye moye"),
+ msg("r2", "agent", "Plain reply"),
+ msg("s1", "system", "ignored"),
+ msg("u2", "user", " "),
+ ];
+ let turns = history_turns(&messages, "phir se fail");
+ assert_eq!(turns.len(), 4);
+ assert_eq!(turns[0].role, WireRole::User);
+ assert_eq!(turns[1].role, WireRole::Assistant);
+ // The module owns the ring of remixed ids; the host only names the turns.
+ assert_eq!(turns[1].id.as_deref(), Some("r1"));
+ assert_eq!(turns[2].id.as_deref(), Some("r2"));
+ assert!(turns.iter().all(|t| !t.remixed));
+ assert_eq!(turns[3].text, "phir se fail");
+ assert_eq!(turns[3].id, None);
+}
+
+#[test]
+fn pending_message_is_not_duplicated_when_already_stored() {
+ let messages = vec![msg("u1", "user", "hello there")];
+ let turns = history_turns(&messages, "hello there ");
+ assert_eq!(turns.len(), 1);
+}
+
+#[test]
+fn a_new_message_repeating_an_earlier_one_is_still_appended() {
+ // user("hello"), agent("reply"), then a new "hello": a distinct message.
+ let messages = vec![msg("u1", "user", "hello"), msg("r1", "agent", "reply")];
+ let turns = history_turns(&messages, "hello");
+ assert_eq!(turns.len(), 3);
+ assert_eq!(turns[2].text, "hello");
+}
diff --git a/crates/openhuman-core/src/tinymemes/inference.rs b/crates/openhuman-core/src/tinymemes/inference.rs
new file mode 100644
index 0000000000..48f9f85c03
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/inference.rs
@@ -0,0 +1,82 @@
+//! OpenHuman's own inference, served to the module's `Complete` callback.
+//!
+//! Every call goes through the provider OpenHuman is configured with for the
+//! `summarization` role (managed backend, BYOK cloud, or local), the same
+//! path thread titles and follow-up suggestions use. Reasoning is switched off
+//! for these calls only; a rewrite needs none, and reasoning models otherwise
+//! spend ~10 s per call. The rest of OpenHuman keeps its configured reasoning.
+
+use tinyinference_llm::message::Message;
+use tinyinference_llm::model::ModelRequest;
+
+use crate::config::Config;
+use crate::core::runtime::DispatchCtx;
+use crate::inference::provider;
+
+/// Workload role whose provider tinymemes borrows.
+pub(crate) const ROLE: &str = "summarization";
+const TEMPERATURE: f64 = 0.7;
+
+/// One system + user completion on the `summarization` provider.
+pub(crate) async fn complete(
+ ctx: &DispatchCtx,
+ config: &Config,
+ system: &str,
+ user: &str,
+) -> Result<String, String> {
+ // Built per call so a provider change in Settings applies without a
+ // restart; construction is cheap and synchronous.
+ let (model, resolved) =
+ provider::create_chat_model_with_model_id_in(ctx, ROLE, config, TEMPERATURE)
+ .map_err(|e| format!("openhuman inference unavailable: {e}"))?;
+ // Reasoning off for tinymemes' calls only (the rest of OpenHuman keeps
+ // its configured reasoning). The managed backend honours the
+ // `without_reasoning` flag; a BYOK route to OpenRouter needs
+ // OpenRouter's own switch. Other endpoints are left untouched, since an
+ // unknown field can be rejected.
+ let mut request = crate::inference::registry::provider_request_without_reasoning(
+ crate::inference::provider::factory::PROVIDER_OPENHUMAN,
+ ModelRequest::new(vec![Message::system(system), Message::user(user)])
+ .with_temperature(TEMPERATURE),
+ );
+ if routes_to_openrouter(config) {
+ request.provider_options = serde_json::json!({ "reasoning": { "enabled": false } });
+ }
+ let response = model
+ .invoke(&(), request)
+ .await
+ .map_err(|e| format!("openhuman inference failed model={resolved}: {e}"))?;
+ let text = response.text();
+ if text.trim().is_empty() {
+ return Err(format!(
+ "openhuman inference returned no text model={resolved}"
+ ));
+ }
+ Ok(text)
+}
+
+/// Whether tinymemes' role resolves to an OpenRouter endpoint: a BYOK
+/// `openrouter:` route, or a custom inference URL on openrouter.ai.
+pub(crate) fn routes_to_openrouter(config: &Config) -> bool {
+ let route_is_openrouter = config
+ .memory_provider
+ .as_deref()
+ .map(|p| p.trim().to_ascii_lowercase())
+ .is_some_and(|p| p.starts_with("openrouter:") || p == "openrouter");
+ let url_is_openrouter = config
+ .inference_url
+ .as_deref()
+ .is_some_and(|u| u.to_ascii_lowercase().contains("openrouter.ai"));
+ route_is_openrouter || url_is_openrouter
+}
+
+/// The model id OpenHuman would use for tinymemes, for the startup log line.
+pub(crate) fn resolved_model(ctx: &DispatchCtx, config: &Config) -> String {
+ provider::create_chat_model_with_model_id_in(ctx, ROLE, config, TEMPERATURE)
+ .map(|(_, id)| id)
+ .unwrap_or_else(|e| format!("unavailable ({e})"))
+}
+
+#[cfg(test)]
+#[path = "inference_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/inference_tests.rs b/crates/openhuman-core/src/tinymemes/inference_tests.rs
new file mode 100644
index 0000000000..96eaa121d2
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/inference_tests.rs
@@ -0,0 +1,32 @@
+use super::*;
+
+#[test]
+fn openrouter_routes_are_detected() {
+ let mut config = Config::default();
+ assert!(!routes_to_openrouter(&config));
+ config.inference_url = Some("https://openrouter.ai/api/v1".into());
+ assert!(routes_to_openrouter(&config));
+ config.inference_url = Some("https://OpenRouter.AI/api/v1".into());
+ assert!(routes_to_openrouter(&config));
+ config.inference_url = Some("https://api.openai.com/v1".into());
+ assert!(!routes_to_openrouter(&config));
+ config.memory_provider = Some("openrouter:deepseek/deepseek-v4-flash".into());
+ assert!(routes_to_openrouter(&config));
+ config.memory_provider = Some("ollama:llama3.1:8b".into());
+ assert!(!routes_to_openrouter(&config));
+}
+
+#[test]
+fn the_model_label_never_fails() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.config_path = dir.path().join("config.toml");
+ config.workspace_dir = dir.path().join("workspace");
+ config.secrets.encrypt = false;
+ // Either the resolved model id or an `unavailable (...)` label.
+ assert!(
+ !resolved_model(&crate::core::runtime::DispatchCtx::host_default(), &config)
+ .trim()
+ .is_empty()
+ );
+}
diff --git a/crates/openhuman-core/src/tinymemes/jev.rs b/crates/openhuman-core/src/tinymemes/jev.rs
new file mode 100644
index 0000000000..27e20db9a5
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/jev.rs
@@ -0,0 +1,44 @@
+//! Jev as OpenHuman manages it.
+//!
+//! The module uses the OpenHuman-managed Jev: a signed-in TinyHumans session
+//! reaches Jev through the backend's System One proxy, billed to the account.
+//! There is no other OpenHuman-side route.
+//!
+//! `None` means managed Jev is unavailable (signed out, or the offline local
+//! session, which has no backend account); the `Evaluate` callback then
+//! reports `Unsupported` and the module returns the original reply.
+
+use std::sync::Arc;
+
+use tinyinference_decisions::{Client, ClientConfig};
+
+use crate::config::Config;
+use crate::security::credentials::session_support::{
+ is_local_session_token, resolve_backend_credential, BackendCredential,
+};
+
+pub(crate) fn managed(config: &Config) -> Option<Arc<Client>> {
+ // Only a session token can be the offline local session; an API key is
+ // never classified by its shape.
+ let secret = match resolve_backend_credential(config).ok()? {
+ BackendCredential::Session(token) if is_local_session_token(&token) => return None,
+ BackendCredential::Session(token) | BackendCredential::ApiKey(token) => token,
+ };
+ let base = config.resolved_endpoints.control_plane_url.clone()?;
+ let mut client = ClientConfig::tinyhumans_openrouter(secret);
+ client.base_url = base;
+ if let Some(identity) = config.resolved_endpoints.product_identity.clone() {
+ client = client.with_sdk_name(&identity);
+ }
+ match Client::new(client) {
+ Ok(c) => Some(Arc::new(c)),
+ Err(e) => {
+ log::warn!("[tinymemes] managed jev client build failed: {e}");
+ None
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "jev_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/jev_tests.rs b/crates/openhuman-core/src/tinymemes/jev_tests.rs
new file mode 100644
index 0000000000..797cf90659
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/jev_tests.rs
@@ -0,0 +1,24 @@
+use super::*;
+
+fn config_in(dir: &std::path::Path) -> Config {
+ let mut config = Config::default();
+ config.config_path = dir.join("config.toml");
+ config.workspace_dir = dir.join("workspace");
+ config.secrets.encrypt = false;
+ config
+}
+
+#[test]
+fn without_a_backend_credential_managed_jev_is_unavailable() {
+ let dir = tempfile::tempdir().unwrap();
+ assert!(managed(&config_in(dir.path())).is_none());
+}
+
+#[test]
+fn an_api_key_reaches_managed_jev() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = config_in(dir.path());
+ config.resolved_endpoints.control_plane_url = Some("https://api.example.test".into());
+ crate::security::credentials::api_key::store_api_key(&config, "th_live_test").unwrap();
+ assert!(managed(&config).is_some());
+}
diff --git a/crates/openhuman-core/src/tinymemes/mod.rs b/crates/openhuman-core/src/tinymemes/mod.rs
new file mode 100644
index 0000000000..bbcef290bb
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/mod.rs
@@ -0,0 +1,351 @@
+//! TinyMemes host adapter: remixes the orchestrator's final reply with
+//! region slang and memes when the chat can take it.
+//!
+//! The engine (Jev reading, rating, rewrite, slang and meme indexes) runs in
+//! the `tinymemes` module, reached over its `tinymemes-bus` contract. This
+//! domain keeps what is the host's:
+//!
+//! - the `OPENHUMAN_TINYMEMES` flag and the A/B split ([`arm`]);
+//! - the thread as the user saw it, each message named by its id
+//! ([`history`]); the module keeps the ring of remixed ids itself and tags
+//! earlier remixed replies from it;
+//! - the callbacks the module asks for while it works: OpenHuman inference,
+//! managed Jev, web search and the learned-state documents ([`callbacks`],
+//! [`docs`], served over the bus by [`service`]). Each carries the remix's
+//! correlation id, so remixes for different workspaces run side by side.
+//!
+//! The module also owns the `TINYMEMES_*` environment overrides (OpenRouter key
+//! and model, Jev route), the LLM fallback when this host has no Jev, and the
+//! slang and meme research it runs in the background after delivery.
+//!
+//! Behaviour:
+//!
+//! - Runs after the turn, on `full_response` only. Sub-agent output never
+//! reaches it.
+//! - The agent's own transcript keeps the original wording, so the
+//! orchestrator never sees its replies in slang.
+//! - Fails open: any error, refusal or timeout delivers the original reply.
+//!
+//! Every turn in an experiment logs one grep-friendly `[tinymemes] turn` line
+//! (arm, turn time, remix time, rating, outcome) for the A/B comparison. No
+//! user content is logged.
+
+mod arm;
+mod callbacks;
+mod docs;
+mod history;
+mod inference;
+mod jev;
+mod search;
+pub(crate) mod service;
+
+use crate::config::Config;
+use crate::core::runtime::DispatchCtx;
+use async_trait::async_trait;
+use std::time::{Duration, Instant};
+use tinymemes_bus::{RemixOptions, RemixRequest, RemixResponse, SkipReason};
+
+pub(crate) use arm::Arm;
+pub(crate) use arm::FLAG_ENV;
+
+/// Whether an `OPENHUMAN_TINYMEMES` value turns remixing on in any form.
+pub(crate) fn flag_enabled(value: &str) -> bool {
+ arm::mode_from(Some(value)) != arm::Mode::Off
+}
+
+const TIMEOUT_ENV: &str = "OPENHUMAN_TINYMEMES_TIMEOUT_MS";
+const COOLDOWN_ENV: &str = "OPENHUMAN_TINYMEMES_MEME_COOLDOWN";
+
+/// 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(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
+/// tests.
+#[async_trait]
+pub(crate) trait Remixer: Send + Sync {
+ async fn remix(&self, request: RemixRequest) -> Result<RemixResponse, String>;
+}
+
+/// The loadable `tinymemes` module, with this workspace's callbacks served.
+struct ModuleRemixer<'a> {
+ ctx: &'a DispatchCtx,
+ config: &'a Config,
+}
+
+#[async_trait]
+impl Remixer for ModuleRemixer<'_> {
+ async fn remix(&self, request: RemixRequest) -> Result<RemixResponse, String> {
+ service::remix(self.ctx, self.config, request).await
+ }
+}
+
+/// Remix a finished task's reply in place, when it is a real answer (not
+/// empty, not the `placeholder` sent when the inference budget ran out) and
+/// the thread is in the treatment arm. Fails open: the reply is left
+/// untouched on any error.
+pub(crate) async fn remix_task_reply(
+ ctx: &DispatchCtx,
+ config: &Config,
+ thread_id: &str,
+ request_id: &str,
+ user_message: &str,
+ full_response: &mut String,
+ placeholder: &str,
+ turn_elapsed: Duration,
+) {
+ if !history::should_remix(full_response, placeholder) {
+ return;
+ }
+ if let Some(remixed) = remix_final_reply(
+ arm::arm_for(config, arm::mode(), thread_id),
+ ctx,
+ config,
+ thread_id,
+ request_id,
+ user_message,
+ full_response,
+ turn_elapsed,
+ )
+ .await
+ {
+ *full_response = remixed;
+ }
+}
+
+/// Remix a finished turn's reply. Returns the text to deliver instead, or
+/// `None` to deliver the original. `turn_elapsed` is the agent turn's own
+/// duration, logged for both arms so throughput can be compared.
+async fn remix_final_reply(
+ arm: Arm,
+ ctx: &DispatchCtx,
+ config: &Config,
+ thread_id: &str,
+ request_id: &str,
+ user_message: &str,
+ reply: &str,
+ turn_elapsed: Duration,
+) -> Option<String> {
+ let turn = TurnInfo {
+ request_id,
+ bucket: arm::bucket(thread_id),
+ turn_ms: turn_elapsed.as_millis(),
+ };
+ if !gate(arm, &turn) {
+ return None;
+ }
+ let started = Instant::now();
+ // Fail open: without the thread, the reading would judge the reply out of
+ // context, so the original goes out instead.
+ let Some(messages) = load(ctx, config, thread_id).await else {
+ log_outcome(&turn, started, "history_unavailable", None);
+ return None;
+ };
+ let remixer = ModuleRemixer { ctx, config };
+ remix_with(
+ &remixer,
+ options(ctx, config),
+ &messages,
+ &turn,
+ started,
+ user_message,
+ reply,
+ budget(config),
+ )
+ .await
+}
+
+/// Per-turn identifiers for the A/B log line.
+#[derive(Clone, Copy)]
+struct TurnInfo<'a> {
+ request_id: &'a str,
+ bucket: u8,
+ turn_ms: u128,
+}
+
+/// Whether the arm runs the remix. The control arm logs its turn for the
+/// throughput comparison; a disabled flag does nothing at all.
+fn gate(arm: Arm, turn: &TurnInfo<'_>) -> bool {
+ match arm {
+ Arm::Disabled => false,
+ Arm::Control => {
+ log::info!(
+ "[tinymemes] turn arm={} bucket={} request_id={} turn_ms={} remix_ms=0 \
+ outcome=not_remixed",
+ arm.as_str(),
+ turn.bucket,
+ turn.request_id,
+ turn.turn_ms
+ );
+ false
+ }
+ Arm::Treatment => true,
+ }
+}
+
+/// The thread as stored. Blocking file work, so it runs off the async
+/// runtime. `None` when the thread cannot be read.
+async fn load(
+ ctx: &DispatchCtx,
+ config: &Config,
+ thread_id: &str,
+) -> Option<Vec<crate::threads::store::ConversationMessage>> {
+ let config = config.clone();
+ let thread_id = thread_id.to_owned();
+ let ctx = ctx.clone();
+ ctx.clone()
+ .spawn_blocking(move || {
+ crate::threads::store::get_messages(&ctx, config.workspace_dir.clone(), &thread_id)
+ .map_err(|e| log::warn!("[tinymemes] thread history unavailable: {e}"))
+ .ok()
+ })
+ .await
+ .ok()
+ .flatten()
+}
+
+/// What the host decides for the module: the meme cooldown
+/// (`OPENHUMAN_TINYMEMES_MEME_COOLDOWN` over `[memes] meme_cooldown`) and
+/// whether web search can serve research at all.
+fn options(ctx: &DispatchCtx, config: &Config) -> RemixOptions {
+ options_with(
+ std::env::var(COOLDOWN_ENV).ok().as_deref(),
+ config.memes.meme_cooldown,
+ search::OpenHumanSearch::available(ctx, config).is_some(),
+ )
+}
+
+fn options_with(env: Option<&str>, configured: u32, research: bool) -> RemixOptions {
+ RemixOptions {
+ meme_cooldown_turns: Some(
+ env.and_then(|v| v.trim().parse::<u32>().ok())
+ .unwrap_or(configured),
+ ),
+ research,
+ }
+}
+
+/// 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`.
+#[allow(clippy::too_many_arguments)]
+async fn remix_with(
+ remixer: &dyn Remixer,
+ options: RemixOptions,
+ messages: &[crate::threads::store::ConversationMessage],
+ turn: &TurnInfo<'_>,
+ started: Instant,
+ user_message: &str,
+ reply: &str,
+ budget: Duration,
+) -> Option<String> {
+ let request = RemixRequest {
+ conversation: history::history_turns(messages, user_message),
+ reply: reply.to_owned(),
+ region: None,
+ enabled: true,
+ timeout_ms: u64::try_from(budget.as_millis()).unwrap_or(u64::MAX),
+ deliver: false,
+ // Set by the service, which owns the registry of live remixes.
+ correlation: String::new(),
+ // The module remembers this id when the reply goes out remixed.
+ reply_id: Some(crate::threads::store::run_reply_message_id(turn.request_id)),
+ options,
+ };
+ // The module enforces `timeout_ms` itself; this outer bound also covers
+ // module load, callback serving and the bus hop.
+ let response = match tokio::time::timeout(budget, remixer.remix(request)).await {
+ Ok(Ok(response)) => response,
+ Ok(Err(error)) => {
+ log::warn!(
+ "[tinymemes] remix unavailable request_id={}: {error}",
+ turn.request_id
+ );
+ log_outcome(turn, started, "error", None);
+ return None;
+ }
+ Err(_) => {
+ log_outcome(turn, started, "timeout", None);
+ return None;
+ }
+ };
+
+ // Whether the delivered reply actually changed.
+ let changed = response.remixed && response.reply.trim() != reply.trim();
+ let result = if changed {
+ "remixed"
+ } else {
+ match response.skipped {
+ Some(SkipReason::Timeout) => "timeout",
+ Some(SkipReason::Failed) => "error",
+ _ => "unchanged",
+ }
+ };
+ log_outcome(turn, started, result, Some(&response));
+ changed.then_some(response.reply)
+}
+
+fn log_outcome(
+ turn: &TurnInfo<'_>,
+ started: Instant,
+ result: &str,
+ response: Option<&RemixResponse>,
+) {
+ log::info!(
+ "{}",
+ turn_line(turn, started.elapsed().as_millis(), result, response)
+ );
+}
+
+/// The treatment arm's per-turn log line. No user content goes in it.
+fn turn_line(
+ turn: &TurnInfo<'_>,
+ remix_ms: u128,
+ result: &str,
+ response: Option<&RemixResponse>,
+) -> String {
+ let TurnInfo {
+ request_id,
+ bucket,
+ turn_ms,
+ } = *turn;
+ format!(
+ "[tinymemes] turn arm=treatment bucket={bucket} request_id={request_id} turn_ms={turn_ms} \
+ remix_ms={remix_ms} outcome={result} score={} skipped={} wants_more_memes={}",
+ response.and_then(|r| r.score).map_or(-1, i32::from),
+ response
+ .and_then(|r| r.skipped)
+ .map_or("none", |s| match s {
+ SkipReason::Disabled => "disabled",
+ SkipReason::BelowThreshold => "below_threshold",
+ SkipReason::Timeout => "timeout",
+ SkipReason::Failed => "failed",
+ }),
+ response.is_some_and(|r| r.wants_more_memes),
+ )
+}
+
+#[cfg(test)]
+#[path = "mod_tests.rs"]
+mod tests;
+
+#[cfg(test)]
+#[path = "e2e_tests.rs"]
+mod e2e_tests;
diff --git a/crates/openhuman-core/src/tinymemes/mod_tests.rs b/crates/openhuman-core/src/tinymemes/mod_tests.rs
new file mode 100644
index 0000000000..831f14a573
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/mod_tests.rs
@@ -0,0 +1,321 @@
+use super::*;
+
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+use crate::threads::store::ConversationMessage;
+
+/// Answers every remix with a fixed response, after an optional delay.
+struct ScriptedRemixer {
+ response: Result<RemixResponse, String>,
+ delay: Duration,
+ calls: AtomicUsize,
+ last: std::sync::Mutex<Option<RemixRequest>>,
+}
+
+impl ScriptedRemixer {
+ fn new(response: Result<RemixResponse, String>) -> Self {
+ Self {
+ response,
+ delay: Duration::ZERO,
+ calls: AtomicUsize::new(0),
+ last: std::sync::Mutex::new(None),
+ }
+ }
+}
+
+#[async_trait]
+impl Remixer for ScriptedRemixer {
+ async fn remix(&self, request: RemixRequest) -> Result<RemixResponse, String> {
+ self.calls.fetch_add(1, Ordering::SeqCst);
+ *self.last.lock().unwrap() = Some(request);
+ tokio::time::sleep(self.delay).await;
+ self.response.clone()
+ }
+}
+
+fn remixed_response(reply: &str) -> RemixResponse {
+ RemixResponse {
+ reply: reply.to_owned(),
+ remixed: true,
+ skipped: None,
+ score: Some(7),
+ wants_more_memes: false,
+ }
+}
+
+fn skipped_response(reply: &str, reason: SkipReason) -> RemixResponse {
+ RemixResponse {
+ reply: reply.to_owned(),
+ remixed: false,
+ skipped: Some(reason),
+ score: None,
+ wants_more_memes: false,
+ }
+}
+
+fn turn(request_id: &str) -> TurnInfo<'_> {
+ TurnInfo {
+ request_id,
+ bucket: 7,
+ turn_ms: 1234,
+ }
+}
+
+fn msg(id: &str, sender: &str, content: &str) -> ConversationMessage {
+ ConversationMessage {
+ id: id.to_owned(),
+ content: content.to_owned(),
+ message_type: "text".to_owned(),
+ extra_metadata: serde_json::Value::Null,
+ sender: sender.to_owned(),
+ created_at: "2026-10-09T00:00:00Z".to_owned(),
+ }
+}
+
+fn thread() -> Vec<ConversationMessage> {
+ vec![
+ msg("u1", "user", "bhai build fail ho gaya"),
+ msg("r1", "agent", "Arre yaar, moye moye"),
+ ]
+}
+
+async fn run(remixer: &ScriptedRemixer, request_id: &str, budget: Duration) -> Option<String> {
+ remix_with(
+ remixer,
+ options_with(None, 3, true),
+ &thread(),
+ &turn(request_id),
+ Instant::now(),
+ "phir se fail",
+ "Build failed again.",
+ budget,
+ )
+ .await
+}
+
+#[test]
+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]
+fn only_the_treatment_arm_passes_the_gate() {
+ let t = turn("req");
+ assert!(!gate(Arm::Disabled, &t));
+ assert!(!gate(Arm::Control, &t));
+ assert!(gate(Arm::Treatment, &t));
+}
+
+#[tokio::test]
+async fn a_remixed_reply_is_delivered_and_named_for_the_module_to_remember() {
+ let remixer = ScriptedRemixer::new(Ok(remixed_response("Arre yaar, build phir fail!")));
+ let out = run(&remixer, "req-ok", Duration::from_secs(5)).await;
+ assert_eq!(out.as_deref(), Some("Arre yaar, build phir fail!"));
+
+ // The module remembers remixed ids itself: it is told which id this reply
+ // will have, and the thread's turns carry theirs.
+ let request = remixer.last.lock().unwrap().clone().unwrap();
+ assert!(request.enabled && !request.deliver);
+ assert_eq!(request.reply, "Build failed again.");
+ assert_eq!(request.timeout_ms, 5000);
+ assert_eq!(request.conversation.last().unwrap().text, "phir se fail");
+ assert_eq!(
+ request.reply_id,
+ Some(crate::threads::store::run_reply_message_id("req-ok"))
+ );
+ assert_eq!(request.conversation[1].id.as_deref(), Some("r1"));
+ assert_eq!(request.options.meme_cooldown_turns, Some(3));
+ assert!(request.options.research);
+}
+
+#[tokio::test]
+async fn an_unchanged_reply_sends_the_original() {
+ for reason in [
+ SkipReason::BelowThreshold,
+ SkipReason::Failed,
+ SkipReason::Timeout,
+ ] {
+ let remixer = ScriptedRemixer::new(Ok(skipped_response("Build failed again.", reason)));
+ assert_eq!(
+ run(&remixer, "req-skip", Duration::from_secs(5)).await,
+ None
+ );
+ }
+}
+
+#[tokio::test]
+async fn a_remix_that_returns_the_same_text_is_not_a_change() {
+ let remixer = ScriptedRemixer::new(Ok(remixed_response("Build failed again. ")));
+ assert_eq!(
+ run(&remixer, "req-same", Duration::from_secs(5)).await,
+ None
+ );
+}
+
+#[tokio::test]
+async fn a_module_error_sends_the_original() {
+ let remixer = ScriptedRemixer::new(Err("module unavailable".into()));
+ assert_eq!(run(&remixer, "req-err", Duration::from_secs(5)).await, None);
+}
+
+#[tokio::test]
+async fn a_slow_remix_times_out_to_the_original() {
+ let mut remixer = ScriptedRemixer::new(Ok(remixed_response("too late")));
+ remixer.delay = Duration::from_secs(30);
+ let out = run(&remixer, "req-slow", Duration::from_millis(50)).await;
+ assert_eq!(out, None);
+}
+
+#[tokio::test]
+async fn a_disabled_flag_leaves_the_task_reply_alone() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.config_path = dir.path().join("config.toml");
+ config.workspace_dir = dir.path().join("workspace");
+ let mut reply = String::from("Build failed again.");
+ // Disabled arm: no module is touched and nothing changes.
+ let out = remix_final_reply(
+ Arm::Disabled,
+ &DispatchCtx::ambient(),
+ &config,
+ "thread",
+ "req",
+ "msg",
+ &reply,
+ Duration::from_millis(5),
+ )
+ .await;
+ assert_eq!(out, None);
+ remix_task_reply(
+ &DispatchCtx::ambient(),
+ &config,
+ "thread",
+ "req",
+ "msg",
+ &mut reply,
+ "placeholder",
+ Duration::ZERO,
+ )
+ .await;
+ assert_eq!(reply, "Build failed again.");
+}
+
+#[test]
+fn the_turn_line_reports_the_outcome_without_user_content() {
+ let t = turn("req-1");
+ let none = turn_line(&t, 5, "error", None);
+ assert!(none.contains("outcome=error") && none.contains("score=-1"));
+ let some = turn_line(&t, 9, "remixed", Some(&remixed_response("secret text")));
+ assert!(some.contains("score=7") && some.contains("request_id=req-1"));
+ assert!(!some.contains("secret text"));
+ let skipped = turn_line(
+ &t,
+ 9,
+ "unchanged",
+ Some(&skipped_response("x", SkipReason::BelowThreshold)),
+ );
+ assert!(skipped.contains("skipped=below_threshold"));
+}
+
+#[test]
+fn flag_values_enable_remixing() {
+ assert!(flag_enabled("on"));
+ assert!(flag_enabled("ab:30"));
+ assert!(!flag_enabled("off"));
+}
+
+#[tokio::test]
+async fn component_off_refuses_the_remix_before_any_module_load() {
+ let mut config = Config::default();
+ config.components.memes = false;
+ let request = RemixRequest {
+ conversation: Vec::new(),
+ reply: "hi".to_owned(),
+ region: None,
+ enabled: true,
+ timeout_ms: 100,
+ deliver: false,
+ correlation: String::new(),
+ reply_id: None,
+ options: RemixOptions::default(),
+ };
+ let err = service::remix(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ request,
+ )
+ .await
+ .unwrap_err();
+ assert!(err.contains("component is off"), "{err}");
+}
+
+#[test]
+fn the_cooldown_env_beats_config_and_bad_values_fall_back() {
+ assert_eq!(
+ options_with(Some("5"), 3, true).meme_cooldown_turns,
+ Some(5)
+ );
+ assert_eq!(
+ options_with(Some("0"), 3, true).meme_cooldown_turns,
+ Some(0)
+ );
+ assert_eq!(
+ options_with(Some("often"), 3, true).meme_cooldown_turns,
+ Some(3)
+ );
+ assert_eq!(options_with(None, 4, false).meme_cooldown_turns, Some(4));
+ assert!(!options_with(None, 4, false).research);
+}
diff --git a/crates/openhuman-core/src/tinymemes/search.rs b/crates/openhuman-core/src/tinymemes/search.rs
new file mode 100644
index 0000000000..f7f120313c
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/search.rs
@@ -0,0 +1,68 @@
+//! OpenHuman's web search as the module's `Search` callback.
+//!
+//! Runs the same checks the `web_search` tool does before calling the search
+//! module: search enabled in settings, the LocalOnly egress block, and at
+//! least one provider able to serve the search role.
+
+use serde_json::json;
+
+use crate::config::Config;
+use crate::core::runtime::DispatchCtx;
+
+const TOOL: &str = tinysearch_bus::tools::WEB_SEARCH;
+
+pub(crate) struct OpenHumanSearch {
+ ctx: DispatchCtx,
+ config: Config,
+}
+
+impl OpenHumanSearch {
+ /// A search backend, or `None` when OpenHuman cannot search right now.
+ pub(crate) fn available(ctx: &DispatchCtx, config: &Config) -> Option<Self> {
+ if !crate::search::is_active(config) {
+ return None;
+ }
+ if crate::search::tools::local_only_search_block(TOOL).is_some() {
+ return None;
+ }
+ let role = tinysearch_bus::role_for_tool(TOOL)?;
+ let resolved = crate::search::providers::resolve(config);
+ if crate::search::providers::effective_role_providers(&resolved, config, role).is_empty() {
+ return None;
+ }
+ Some(Self {
+ ctx: ctx.clone(),
+ config: config.clone(),
+ })
+ }
+}
+
+impl OpenHumanSearch {
+ /// Run one search through the search module.
+ pub(crate) async fn search(
+ &self,
+ query: &str,
+ max_results: usize,
+ ) -> Result<Vec<tinymemes_bus::SearchResult>, String> {
+ let request = tinysearch_bus::ExecuteToolRequest {
+ name: TOOL.to_string(),
+ arguments: json!({ "query": query, "max_results": max_results.clamp(1, 20) }),
+ };
+ let response = crate::modules::search::execute_tool(&self.ctx, &self.config, request)
+ .await
+ .map_err(|e| crate::search::tools::user_facing_error(&e))?;
+ Ok(response
+ .results
+ .into_iter()
+ .map(|r| tinymemes_bus::SearchResult {
+ title: r.title,
+ url: r.url,
+ snippet: r.snippet,
+ })
+ .collect())
+ }
+}
+
+#[cfg(test)]
+#[path = "search_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/search_tests.rs b/crates/openhuman-core/src/tinymemes/search_tests.rs
new file mode 100644
index 0000000000..e01c29f90c
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/search_tests.rs
@@ -0,0 +1,25 @@
+use super::*;
+
+#[test]
+fn search_switched_off_in_settings_is_unavailable() {
+ let mut config = Config::default();
+ config.search.enabled = Some(false);
+ assert!(OpenHumanSearch::available(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config
+ )
+ .is_none());
+}
+
+#[test]
+fn availability_follows_the_configured_providers() {
+ // Whatever the default providers resolve to, a backend is returned only
+ // when search is on; it never panics on a default config.
+ let mut config = Config::default();
+ config.search.enabled = Some(true);
+ if let Some(search) =
+ OpenHumanSearch::available(&crate::core::runtime::DispatchCtx::host_default(), &config)
+ {
+ assert!(search.config.search.is_enabled());
+ }
+}
diff --git a/crates/openhuman-core/src/tinymemes/service.rs b/crates/openhuman-core/src/tinymemes/service.rs
new file mode 100644
index 0000000000..dadd69a5a3
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/service.rs
@@ -0,0 +1,162 @@
+//! The bus half of the `tinymemes` module: the host callbacks it is served
+//! (`Host` interface) and the client call that starts a remix.
+
+use async_trait::async_trait;
+use tinybus::{CallContext, Connection, InterfaceName, MemberName, ObjectPath};
+use tinymemes_bus::{
+ host_methods, remix_methods, RemixRefusal, RemixRequest, RemixResponse, RemixResult,
+ HOST_INTERFACE, HOST_METHODS, HOST_PATH, REMIX_INTERFACE, REMIX_PATH, STATE_INTERFACE,
+};
+
+use super::callbacks;
+use crate::config::Config;
+
+/// Registry id of the module the callbacks serve.
+pub(crate) const MODULE_ID: &str = "tinymemes";
+
+struct HostService {
+ connection: Connection,
+}
+
+#[async_trait]
+impl tinybus::Interface for HostService {
+ fn name(&self) -> InterfaceName {
+ InterfaceName::new(HOST_INTERFACE).expect("contract interface name is well formed")
+ }
+
+ fn members(&self) -> Vec<MemberName> {
+ HOST_METHODS
+ .iter()
+ .filter_map(|member| MemberName::new(*member).ok())
+ .collect()
+ }
+
+ async fn call(
+ &self,
+ _member: &MemberName,
+ _args: serde_json::Value,
+ ) -> tinybus::Result<serde_json::Value> {
+ // Without the broker's context there is no caller to authorize.
+ Err(tinybus::Error::failed("unauthenticated tinymemes caller"))
+ }
+
+ async fn call_with_context(
+ &self,
+ member: &MemberName,
+ args: serde_json::Value,
+ context: &CallContext,
+ ) -> tinybus::Result<serde_json::Value> {
+ // Only the loaded `tinymemes` module may ask: its bus name's owner
+ // must be the authenticated sender.
+ let Some(sender) = context.authenticated_sender() else {
+ return Err(tinybus::Error::failed("unauthenticated tinymemes caller"));
+ };
+ let owner = self
+ .connection
+ .name_owner(tinymemes_bus::STATE_INTERFACE)
+ .await
+ .ok()
+ .flatten();
+ if owner.as_ref() != Some(sender) {
+ log::warn!("[tinymemes] refused a host callback from a peer that is not the module");
+ return Err(tinybus::Error::failed("unknown tinymemes caller"));
+ }
+ // The module is the only caller, but it serves many remixes: the
+ // correlation id says which workspace this callback answers for.
+ let correlation = callbacks::correlation_of(&args).map(str::to_owned);
+ if member.as_str() == host_methods::END {
+ // Forgetting an unknown id is harmless, so `End` needs no session.
+ if let Some(correlation) = &correlation {
+ callbacks::end(correlation);
+ }
+ return serde_json::to_value(tinymemes_bus::HostResult::<()>::Ok(()))
+ .map_err(|_| tinybus::Error::failed("invalid tinymemes host response"));
+ }
+ let Some(session) = correlation.as_deref().and_then(callbacks::session) else {
+ log::debug!("[tinymemes] host callback with no live remix for its correlation id");
+ return Err(tinybus::Error::failed("no such tinymemes remix"));
+ };
+ callbacks::dispatch(&session, member.as_str(), args).await
+ }
+}
+
+/// Serve the module's host callbacks on the module bus.
+pub(crate) async fn install(connection: &Connection) -> tinybus::Result<()> {
+ connection
+ .serve_at(
+ ObjectPath::new(HOST_PATH)?,
+ HostService {
+ connection: connection.clone(),
+ },
+ )
+ .await
+}
+
+/// One foreground remix at a time per workspace.
+///
+/// The module reads its learned-state documents at the start of a remix and
+/// writes them at the end, so two remixes for one scope at once would each
+/// save a stale snapshot over the other. Different scopes never wait for each
+/// other. Background research that outlives a call is not covered: it can only
+/// lose counters, never a remixed id (the module serializes those itself).
+static FOREGROUND: std::sync::LazyLock<
+ std::sync::Mutex<std::collections::HashMap<String, std::sync::Arc<tokio::sync::Mutex<()>>>>,
+> = std::sync::LazyLock::new(Default::default);
+
+fn scope_lock(config: &Config) -> std::sync::Arc<tokio::sync::Mutex<()>> {
+ let key = config.workspace_dir.display().to_string();
+ FOREGROUND
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .entry(key)
+ .or_default()
+ .clone()
+}
+
+/// Remix one reply through the loaded module, serving its callbacks for
+/// `config`'s workspace meanwhile. `request.correlation` is set here.
+pub(crate) async fn remix(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ mut request: RemixRequest,
+) -> Result<RemixResponse, String> {
+ if !config.components.memes {
+ return Err("the memes component is off".to_owned());
+ }
+ crate::modules::ensure_loaded(config, MODULE_ID).await?;
+ let runtime = crate::modules::runtime::runtime()
+ .await
+ .map_err(|error| format!("the module bus is not running: {error}"))?;
+ let proxy = runtime
+ .connection()
+ .proxy(STATE_INTERFACE, REMIX_PATH, REMIX_INTERFACE)
+ .map_err(|error| error.to_string())?;
+ let registration = callbacks::register(ctx, config);
+ request.correlation = registration.correlation().to_owned();
+ // A disabled request makes no callbacks and starts no research, so there
+ // is nothing to keep the registration for.
+ let may_research = request.enabled;
+ // One foreground remix at a time per scope; see [`FOREGROUND`].
+ let lock = scope_lock(config);
+ let _foreground = lock.lock().await;
+ let outcome: tinybus::Result<RemixResult> = proxy.call(remix_methods::REMIX, (request,)).await;
+ match outcome {
+ Ok(Ok(response)) => {
+ // Background research may still call back; the module's `End`
+ // (or the age sweep) retires the entry.
+ if may_research {
+ registration.keep_for_background();
+ }
+ Ok(response)
+ }
+ Ok(Err(refusal)) => Err(match refusal {
+ RemixRefusal::Closing => "the tinymemes module is closing".to_owned(),
+ RemixRefusal::Unauthenticated => "the tinymemes module refused the caller".to_owned(),
+ }),
+ Err(error) => Err(format!("tinymemes remix call failed: {error}")),
+ }
+}
+
+#[cfg(test)]
+#[path = "service_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/tinymemes/service_tests.rs b/crates/openhuman-core/src/tinymemes/service_tests.rs
new file mode 100644
index 0000000000..63c0c7f439
--- /dev/null
+++ b/crates/openhuman-core/src/tinymemes/service_tests.rs
@@ -0,0 +1,262 @@
+use super::*;
+use serde_json::{json, Value};
+use tinybus::{broker::Broker, transport::memory::MemoryBus};
+use tinymemes_bus::{HostFailure, HostResult};
+
+fn config_in(dir: &std::path::Path) -> Config {
+ let mut config = Config::default();
+ config.config_path = dir.join("config.toml");
+ config.workspace_dir = dir.join("workspace");
+ config.secrets.encrypt = false;
+ config
+}
+
+/// A host connection serving the callbacks, the peer that owns the module's
+/// bus name, and a peer that does not.
+struct Bus {
+ host: Connection,
+ module: Connection,
+ intruder: Connection,
+}
+
+async fn bus() -> Bus {
+ let bus = MemoryBus::new();
+ Broker::new().spawn(bus.clone());
+ let connect = || async {
+ Connection::connect(bus.connect().await.unwrap())
+ .await
+ .unwrap()
+ };
+ let (host, module, intruder) = (connect().await, connect().await, connect().await);
+ install(&host).await.unwrap();
+ module.request_name(STATE_INTERFACE).await.unwrap();
+ Bus {
+ host,
+ module,
+ intruder,
+ }
+}
+
+impl Bus {
+ async fn call(
+ &self,
+ from: &Connection,
+ member: &str,
+ payload: Value,
+ ) -> tinybus::Result<Value> {
+ let host = self.host.unique_name().unwrap();
+ from.proxy(host.as_str(), HOST_PATH, HOST_INTERFACE)
+ .unwrap()
+ .call(member, (payload,))
+ .await
+ }
+}
+
+fn decode<T: serde::de::DeserializeOwned>(value: Value) -> HostResult<T> {
+ serde_json::from_value(value).unwrap()
+}
+
+#[tokio::test]
+async fn only_the_module_may_call_back_and_only_for_a_live_remix() {
+ let dir = tempfile::tempdir().unwrap();
+ let bus = bus().await;
+ let registration = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let correlation = registration.correlation().to_owned();
+ let deliver = json!({ "correlation": correlation, "reply": "hi", "remixed": true });
+
+ // The module, for its own live remix.
+ let ok = bus
+ .call(&bus.module, host_methods::DELIVER, deliver.clone())
+ .await
+ .unwrap();
+ assert_eq!(decode::<()>(ok), Ok(()));
+
+ // A peer that does not own the module's bus name, with a valid id.
+ let refused = bus
+ .call(&bus.intruder, host_methods::DELIVER, deliver)
+ .await;
+ assert!(refused.is_err(), "a stranger must not reach the callbacks");
+
+ // The module, with an id nobody registered, or none at all.
+ for payload in [
+ json!({ "correlation": "not-a-live-remix", "reply": "hi", "remixed": true }),
+ json!({ "reply": "hi", "remixed": true }),
+ json!({ "correlation": "", "reply": "hi", "remixed": true }),
+ ] {
+ let refused = bus.call(&bus.module, host_methods::DELIVER, payload).await;
+ assert!(refused.is_err(), "no live remix, no answer");
+ }
+}
+
+#[tokio::test]
+async fn a_direct_call_without_the_brokers_context_is_unauthenticated() {
+ let bus = bus().await;
+ let service = HostService {
+ connection: bus.host.clone(),
+ };
+ let error = tinybus::Interface::call(
+ &service,
+ &MemberName::new(host_methods::DELIVER).unwrap(),
+ json!([{}]),
+ )
+ .await
+ .unwrap_err();
+ assert!(error.to_string().contains("unauthenticated"), "{error}");
+}
+
+#[tokio::test]
+async fn remixes_for_different_workspaces_are_served_at_once_each_for_its_own() {
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let bus = bus().await;
+ let a = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir_a.path()),
+ );
+ let b = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir_b.path()),
+ );
+ assert_ne!(a.correlation(), b.correlation());
+
+ // Both are live together: neither waits for the other.
+ let save = |registration: &callbacks::Registration, json: &str| {
+ let payload =
+ json!({ "correlation": registration.correlation(), "kind": "slang", "json": json });
+ bus.call(&bus.module, host_methods::SAVE_INDEX, payload)
+ };
+ let (saved_a, saved_b) = tokio::join!(save(&a, "{\"a\":1}"), save(&b, "{\"b\":1}"));
+ assert_eq!(decode::<()>(saved_a.unwrap()), Ok(()));
+ assert_eq!(decode::<()>(saved_b.unwrap()), Ok(()));
+
+ let load = |registration: &callbacks::Registration| {
+ let payload = json!({ "correlation": registration.correlation(), "kind": "slang" });
+ bus.call(&bus.module, host_methods::LOAD_INDEX, payload)
+ };
+ assert_eq!(
+ decode::<Option<String>>(load(&a).await.unwrap()),
+ Ok(Some("{\"a\":1}".into()))
+ );
+ assert_eq!(
+ decode::<Option<String>>(load(&b).await.unwrap()),
+ Ok(Some("{\"b\":1}".into()))
+ );
+}
+
+#[tokio::test]
+async fn end_retires_the_remix_and_is_harmless_when_repeated_or_unknown() {
+ let dir = tempfile::tempdir().unwrap();
+ let bus = bus().await;
+ let registration = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let correlation = registration.correlation().to_owned();
+ registration.keep_for_background();
+
+ let ended = bus
+ .call(
+ &bus.module,
+ host_methods::END,
+ json!({ "correlation": correlation }),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<()>(ended), Ok(()));
+ let after = bus
+ .call(
+ &bus.module,
+ host_methods::EVALUATE,
+ json!({ "correlation": correlation, "body": {} }),
+ )
+ .await;
+ assert!(after.is_err(), "an ended remix answers nothing");
+
+ for correlation in [correlation.as_str(), "never-registered"] {
+ let again = bus
+ .call(
+ &bus.module,
+ host_methods::END,
+ json!({ "correlation": correlation }),
+ )
+ .await
+ .unwrap();
+ assert_eq!(decode::<()>(again), Ok(()));
+ }
+ // A stranger cannot end somebody else's remix.
+ let dir2 = tempfile::tempdir().unwrap();
+ let live = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir2.path()),
+ );
+ let refused = bus
+ .call(
+ &bus.intruder,
+ host_methods::END,
+ json!({ "correlation": live.correlation() }),
+ )
+ .await;
+ assert!(refused.is_err());
+ assert!(callbacks::session(live.correlation()).is_some());
+}
+
+#[tokio::test]
+async fn an_unavailable_capability_is_reported_not_faked() {
+ let dir = tempfile::tempdir().unwrap();
+ let bus = bus().await;
+ let registration = callbacks::register(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config_in(dir.path()),
+ );
+ let reply = bus
+ .call(
+ &bus.module,
+ host_methods::EVALUATE,
+ json!({ "correlation": registration.correlation(), "body": { "questions": {} } }),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ decode::<tinymemes_bus::EvaluateEnvelope>(reply),
+ Err(HostFailure::Unsupported)
+ );
+}
+
+#[tokio::test]
+async fn component_off_refuses_before_any_module_load() {
+ let mut config = Config::default();
+ config.components.memes = false;
+ let request: RemixRequest =
+ serde_json::from_str(r#"{"conversation":[],"reply":"hi"}"#).unwrap();
+ let error = remix(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ request,
+ )
+ .await
+ .unwrap_err();
+ assert!(error.contains("component is off"), "{error}");
+}
+
+#[test]
+fn foreground_remixes_share_a_lock_per_workspace_and_never_across_workspaces() {
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let (a, b) = (config_in(dir_a.path()), config_in(dir_b.path()));
+ assert!(std::sync::Arc::ptr_eq(&scope_lock(&a), &scope_lock(&a)));
+ assert!(!std::sync::Arc::ptr_eq(&scope_lock(&a), &scope_lock(&b)));
+}
+
+#[tokio::test]
+async fn a_second_remix_for_one_workspace_waits_for_the_first_but_another_workspace_does_not() {
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let (a, b) = (config_in(dir_a.path()), config_in(dir_b.path()));
+ let held = scope_lock(&a);
+ let _first = held.lock().await;
+ assert!(scope_lock(&a).try_lock().is_err(), "same workspace waits");
+ assert!(
+ scope_lock(&b).try_lock().is_ok(),
+ "another workspace does not"
+ );
+}