Skip to content

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

refactor(subagent_sessions): route session updates through store

Steven Enamakele4cd188ab13c
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
index d58c86ead6..62d83c2db7 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -238,22 +238,12 @@ fn update_session<F>(
update: F,
) -> Result<bool, String>
where
- F: FnOnce(&mut DurableSubagentSession, String),
+ F: FnMut(&mut DurableSubagentSession, String) + Send,
{
let _guard = store_write_lock()
.lock()
.map_err(|_| "subagent session store lock poisoned".to_string())?;
- let mut sessions = store.load()?;
- let now = chrono::Utc::now().to_rfc3339();
- if let Some(session) = sessions
- .iter_mut()
- .find(|session| session.subagent_session_id == subagent_session_id)
- {
- update(session, now);
- store.save(&sessions)?;
- return Ok(true);
- }
- Ok(false)
+ store.update_session(subagent_session_id, update)
}
fn store_write_lock() -> &'static Mutex<()> {
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
index 3daca14f75..24c9dfdad8 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
@@ -56,4 +56,19 @@ impl SubagentSessionStore {
self.import_legacy(&docs)?;
docs.save(sessions).map_err(|err| format!("{err:#}"))
}
+
+ pub fn update_session<F>(
+ &self,
+ subagent_session_id: &str,
+ update: F,
+ ) -> Result<bool, String>
+ where
+ F: FnMut(&mut DurableSubagentSession, String) + Send,
+ {
+ let docs = super::store_documents::current(&self.workspace_dir)
+ .map_err(|err| format!("{err:#}"))?;
+ self.import_legacy(&docs)?;
+ docs.update_session(subagent_session_id, update)
+ .map_err(|err| format!("{err:#}"))
+ }
}
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
index cbcab9fb6a..0da9efe196 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -198,6 +198,35 @@ impl Docs {
.collect()
}
+ /// Atomically mutate one session from its current stored value. Live
+ /// lifecycle changes use this path so millisecond timestamp precision
+ /// cannot discard an equal-millisecond state transition. Whole-list saves
+ /// remain guarded by `write_if_newer` for stale snapshot/import protection.
+ pub(super) fn update_session<F>(
+ &self,
+ subagent_session_id: &str,
+ mut update: F,
+ ) -> Result<bool>
+ where
+ F: FnMut(&mut DurableSubagentSession, String) + Send,
+ {
+ let id = key_of(subagent_session_id)?;
+ self.0.run(|docs| async move {
+ let repo = tinystoragedrivers::Repo::<Value>::new(&docs, SESSIONS)?;
+ repo.update(&id, move |current| {
+ let Some(current) = current else {
+ return Ok(None);
+ };
+ let stored: StoredSession = serde_json::from_value(current)?;
+ let mut session = DurableSubagentSession::from(stored);
+ update(&mut session, chrono::Utc::now().to_rfc3339());
+ Ok(Some(serde_json::to_value(StoredSession::from(&session))?))
+ })
+ .await
+ .map(|updated| updated.is_some())
+ })
+ }
+
/// Whether the one-time legacy file import finished in this scope.
pub(super) fn legacy_imported(&self, source: &std::path::Path) -> Result<bool> {
import_state::is_done(&self.0, OWNER, &import_step(source))