Skip to content

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

refactor(tinymemes): extract service module

Steven Enamakel3dd3a99d7609
diff --git a/crates/openhuman-core/src/tinymemes/service.rs b/crates/openhuman-core/src/tinymemes/service.rs
index 6ee81297f6..fad885f6fd 100644
--- a/crates/openhuman-core/src/tinymemes/service.rs
+++ b/crates/openhuman-core/src/tinymemes/service.rs
@@ -92,6 +92,33 @@ pub(crate) async fn install(connection: &Connection) -> tinybus::Result<()> {
.await
}
+/// One foreground remix at a time per workspace and acting identity.
+///
+/// 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 identity = crate::memory::scope::current().unwrap_or_default();
+ let key = format!(
+ "{}\0{}\0{}",
+ config.workspace_dir.display(),
+ identity.agent_id.as_deref().unwrap_or("-"),
+ identity.team.as_deref().unwrap_or("-")
+ );
+ 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(