Skip to content

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

fix(threads): preserve thread state when editing and reading transcripts

Steven Enamakel80f1ea6db91d
diff --git a/crates/openhuman-core/src/threads/ops/edit.rs b/crates/openhuman-core/src/threads/ops/edit.rs
index 32697a4752..d2c0b70366 100644
--- a/crates/openhuman-core/src/threads/ops/edit.rs
+++ b/crates/openhuman-core/src/threads/ops/edit.rs
@@ -90,7 +90,7 @@ pub async fn edit_message(
let dir = workspace_dir().await.map_err(ThreadsError::Message)?;
// The request's own tenant picks the store key, so a SaaS profile edits
// the transcript its turns were written to.
- let store = transcript_store(ctx).await.map_err(ThreadsError::Message)?;
+ let store = transcript_store(ctx, &thread_id).await.map_err(ThreadsError::Message)?;
crate::web_chat::cancel_chat_scoped(ctx, &client_id, &thread_id, None)
.await
@@ -157,7 +157,7 @@ pub async fn regenerate(
let dir = workspace_dir().await.map_err(ThreadsError::Message)?;
// The request's own tenant picks the store key, so a SaaS profile edits
// the transcript its turns were written to.
- let store = transcript_store(ctx).await.map_err(ThreadsError::Message)?;
+ let store = transcript_store(ctx, &thread_id).await.map_err(ThreadsError::Message)?;
crate::web_chat::cancel_chat_scoped(ctx, &client_id, &thread_id, None)
.await
diff --git a/crates/openhuman-core/src/threads/ops/support.rs b/crates/openhuman-core/src/threads/ops/support.rs
index 5fc06f86e5..48289667be 100644
--- a/crates/openhuman-core/src/threads/ops/support.rs
+++ b/crates/openhuman-core/src/threads/ops/support.rs
@@ -42,16 +42,21 @@ impl TranscriptStore {
}
}
-/// The transcript store of the request `ctx` carries, resolved exactly as the
-/// session host resolved it when it wrote the thread: the config's chat agent
-/// as the fallback id, and the request's own tenant (never the ambient one),
-/// so a SaaS profile's key keeps its profile prefix.
+/// The transcript store of `thread_id` for the request `ctx` carries: the key
+/// the thread's transcripts were first written under, recorded with the thread
+/// ([`ConversationStore::record_transcript_key`](crate::threads::store::ConversationStore::record_transcript_key)),
+/// so a later `chat_agent_id` change cannot strand its earlier turns. A thread
+/// with no record resolves as the session host would today: the config's chat
+/// agent as the fallback id, and the request's own tenant (never the ambient
+/// one), so a SaaS profile's key keeps its profile prefix.
///
/// # Errors
///
-/// The config cannot be loaded, or the request has no tenant in SaaS mode.
+/// The config cannot be loaded, the request has no tenant in SaaS mode, or the
+/// recorded key cannot be read.
pub(crate) async fn transcript_store(
ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
) -> Result<TranscriptStore, String> {
let config = ctx
.load_config()
@@ -62,7 +67,21 @@ pub(crate) async fn transcript_store(
ctx.resolved().as_deref(),
)
.map_err(|no_tenant| no_tenant.to_string())?;
- Ok(transcript_store_for(&config, &tenant))
+ let mut store = transcript_store_for(&config, &tenant);
+ let recorded = crate::threads::store::blocking::transcript_key(
+ ctx.clone(),
+ config.workspace_dir.clone(),
+ thread_id.to_string(),
+ )
+ .await?;
+ if let Some(key) = recorded {
+ log::debug!(
+ "[threads] transcript store key from the thread's recorded key (differs_from_current_rule={})",
+ key != store.key
+ );
+ store.key = key;
+ }
+ Ok(store)
}
/// [`transcript_store`] for an already-resolved config and tenant.
diff --git a/crates/openhuman-core/src/threads/ops/transcript.rs b/crates/openhuman-core/src/threads/ops/transcript.rs
index 76c3f99f6a..48b65c0efc 100644
--- a/crates/openhuman-core/src/threads/ops/transcript.rs
+++ b/crates/openhuman-core/src/threads/ops/transcript.rs
@@ -30,7 +30,7 @@ pub async fn transcript_get(
return Err("thread_id is required".to_string());
}
// Resolved here, in the request's scope; the projection itself is blocking.
- let locator = transcript_store(ctx).await?.locator();
+ let locator = transcript_store(ctx, &thread_id).await?.locator();
let cursor = request.cursor.clone();
let limit = request.limit;
let page = tokio::task::spawn_blocking(move || {
diff --git a/crates/openhuman-core/src/threads/ops/usage.rs b/crates/openhuman-core/src/threads/ops/usage.rs
index ec7e472af9..42183542f1 100644
--- a/crates/openhuman-core/src/threads/ops/usage.rs
+++ b/crates/openhuman-core/src/threads/ops/usage.rs
@@ -70,7 +70,7 @@ pub async fn token_usage(
request: ThreadTokenUsageRequest,
) -> Result<Outcome<ApiEnvelope<ThreadTokenUsageResponse>>, String> {
// Resolved here, in the request's scope; the read itself is blocking.
- let locator = transcript_store(ctx).await?.locator();
+ let locator = transcript_store(ctx, &request.thread_id).await?.locator();
let thread_id = request.thread_id.clone();
let spend = tokio::task::spawn_blocking(move || thread_spend_in(locator.as_ref(), &thread_id))
.await