Skip to content

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

fix(threads): purge session store data when deleting threads

Steven Enamakel5a8aa42bed40
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index 184534e669..583fd3d4f9 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -26,8 +26,8 @@ mod workspace;
pub use rebound::{rebind_all, ReboundSessionStores};
pub use workspace::{
- current_documents_for, ledger_for, provider_for_workspace, release_workspace_defaults,
- require_ledger, status_store_for, stores_for, stores_or_ephemeral, with_ledger,
+ current_documents_for, default_turn_states_for, ledger_for, provider_for_workspace, release_workspace_defaults,
+ require_ledger, status_store_for, stores_for, stores_or_ephemeral, turn_states_for, with_ledger,
with_ledger_async, WorkspaceSessionStores,
};
diff --git a/crates/openhuman-core/src/agent/session_store/workspace.rs b/crates/openhuman-core/src/agent/session_store/workspace.rs
index 29e6991078..54f1d577b1 100644
--- a/crates/openhuman-core/src/agent/session_store/workspace.rs
+++ b/crates/openhuman-core/src/agent/session_store/workspace.rs
@@ -17,7 +17,7 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex, PoisonError};
-use tinyagents_session::port::{AgentStores, SessionStoreProvider};
+use tinyagents_session::port::{AgentStores, SessionStoreProvider, TurnStates};
use tinyagents_session::run_ledger::{LedgerError, LedgerResult, RunLedgerStore};
use tinyagents_session::DriverSessionStores;
use tinystoragedrivers::{DocumentStore, MemoryStorage, StorageBackend};
@@ -136,6 +136,24 @@ pub fn stores_or_ephemeral(workspace_dir: &Path) -> AgentStores {
})
}
+/// The calling tenant's turn-state snapshots for `workspace_dir`: the
+/// `TurnStates` port of its session stores. A call with no tenant gets a
+/// throwaway in-memory store.
+#[must_use]
+pub fn turn_states_for(workspace_dir: &Path) -> Arc<dyn TurnStates> {
+ stores_or_ephemeral(workspace_dir).turn_states
+}
+
+/// The operator's ([`DEFAULT_AGENT`](super::DEFAULT_AGENT)) turn-state
+/// snapshots for `workspace_dir`, independent of the calling task's tenant —
+/// for boot-time recovery sweeps that run before any tenant is in scope.
+#[must_use]
+pub fn default_turn_states_for(workspace_dir: &Path) -> Arc<dyn TurnStates> {
+ provider_for_workspace(workspace_dir)
+ .for_agent(super::DEFAULT_AGENT)
+ .turn_states
+}
+
/// The calling tenant's run ledger for `workspace_dir`.
///
/// The installed provider's when it keeps one, else the workspace default's.
diff --git a/crates/openhuman-core/src/threads/ops/crud.rs b/crates/openhuman-core/src/threads/ops/crud.rs
index cfe2541eab..7021120fd3 100644
--- a/crates/openhuman-core/src/threads/ops/crud.rs
+++ b/crates/openhuman-core/src/threads/ops/crud.rs
@@ -443,7 +443,7 @@ async fn thread_delete_inner(
// mirrors conversation-derived state) remains on disk; the
// thread row itself is already gone at this point so the caller
// sees a partial failure they can act on instead of silent drift.
- turn_state::store::delete(dir.clone(), &request.thread_id).map_err(|err| {
+ crate::agent::session_store::turn_states_for(&dir).delete(&request.thread_id).map_err(|err| {
format!(
"thread {} deleted but turn-snapshot cleanup failed: {err}",
request.thread_id
diff --git a/crates/openhuman-core/src/threads/ops/purge.rs b/crates/openhuman-core/src/threads/ops/purge.rs
index 1649ab959d..bd71eee1cf 100644
--- a/crates/openhuman-core/src/threads/ops/purge.rs
+++ b/crates/openhuman-core/src/threads/ops/purge.rs
@@ -48,7 +48,7 @@ async fn threads_purge_inner(
// snapshot files (which `list()` would warn-and-skip) are also
// removed — a destructive cleanup must not leave behind anything
// it failed to deserialize. Failures surface as RPC errors.
- turn_state::store::clear_all(dir.clone())
+ crate::agent::session_store::turn_states_for(&dir).clear_all()
.map_err(|err| format!("threads purged but turn-snapshot cleanup failed: {err}"))?;
Ok(envelope(
PurgeConversationThreadsResponse {
diff --git a/crates/openhuman-core/src/threads/ops/turn_state_ops.rs b/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
index 421dbefe61..4bc1b47d5a 100644
--- a/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
+++ b/crates/openhuman-core/src/threads/ops/turn_state_ops.rs
@@ -7,14 +7,14 @@ use crate::threads::turn_state::{
GetTurnStateRequest, GetTurnStateResponse, ListTurnStatesResponse,
};
use crate::threads::{ApiEnvelope, EmptyRequest};
-use tinyagents_session::turn_state;
+use crate::agent::session_store::turn_states_for;
/// Returns the persisted in-flight turn snapshot for a thread, if any.
pub async fn turn_state_get(
request: GetTurnStateRequest,
) -> Result<Outcome<ApiEnvelope<GetTurnStateResponse>>, String> {
let dir = workspace_dir().await?;
- let turn_state = turn_state::store::get(dir, &request.thread_id)?;
+ let turn_state = turn_states_for(&dir).get(&request.thread_id)?;
let present = turn_state.is_some();
Ok(envelope(
GetTurnStateResponse { turn_state },
@@ -29,7 +29,7 @@ pub async fn turn_state_list(
_request: EmptyRequest,
) -> Result<Outcome<ApiEnvelope<ListTurnStatesResponse>>, String> {
let dir = workspace_dir().await?;
- let turn_states = turn_state::store::list(dir)?;
+ let turn_states = turn_states_for(&dir).list()?;
let count = turn_states.len();
Ok(envelope(
ListTurnStatesResponse { turn_states, count },
@@ -44,7 +44,7 @@ pub async fn turn_state_history(
request: GetTurnStateRequest,
) -> Result<Outcome<ApiEnvelope<ListTurnStatesResponse>>, String> {
let dir = workspace_dir().await?;
- let turn_states = turn_state::store::list_thread(dir, &request.thread_id)?;
+ let turn_states = turn_states_for(&dir).list_thread(&request.thread_id)?;
let count = turn_states.len();
Ok(envelope(
ListTurnStatesResponse { turn_states, count },
@@ -60,7 +60,7 @@ pub async fn turn_state_get_turn(
request: GetTurnStateForRequestRequest,
) -> Result<Outcome<ApiEnvelope<GetTurnStateResponse>>, String> {
let dir = workspace_dir().await?;
- let turn_state = turn_state::store::get_turn(dir, &request.thread_id, &request.request_id)?;
+ let turn_state = turn_states_for(&dir).get_turn(&request.thread_id, &request.request_id)?;
let present = turn_state.is_some();
Ok(envelope(
GetTurnStateResponse { turn_state },
@@ -75,6 +75,6 @@ pub async fn turn_state_clear(
request: ClearTurnStateRequest,
) -> Result<Outcome<ApiEnvelope<ClearTurnStateResponse>>, String> {
let dir = workspace_dir().await?;
- let cleared = turn_state::store::delete(dir, &request.thread_id)?;
+ let cleared = turn_states_for(&dir).delete(&request.thread_id)?;
Ok(envelope(ClearTurnStateResponse { cleared }, None, None))
}