Skip to content

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

fix(relay): defer legacy history import until session host runs

Steven Enamakelfd1adb9af8f8
diff --git a/crates/openhuman-core/src/agent/turn_host.rs b/crates/openhuman-core/src/agent/turn_host.rs
index ae6a3f1d81..7677dd2e28 100644
--- a/crates/openhuman-core/src/agent/turn_host.rs
+++ b/crates/openhuman-core/src/agent/turn_host.rs
@@ -34,6 +34,7 @@ pub(crate) struct TrustedAgentTurn {
pub(crate) approval: ApprovalTurnContext,
pub(crate) relay_thread: Option<String>,
pub(crate) imported_history: Vec<tinyagents_session::transcript::TranscriptMessage>,
+ pub(crate) legacy_history: Option<crate::channels::providers::relay::LegacyHistoryImport>,
/// Captured configured compiled-module target for native turns. The
/// progressive handle itself is opened at runner checkout per admitted
/// request, so queued follow-ups never reuse a finished handle.
@@ -49,6 +50,7 @@ impl std::fmt::Debug for TrustedAgentTurn {
.field("session_scoped", &!self.session_key.is_empty())
.field("relay_thread", &self.relay_thread)
.field("imported_history_rows", &self.imported_history.len())
+ .field("has_legacy_history", &self.legacy_history.is_some())
.field("native_target", &self.native_target.is_some())
.finish_non_exhaustive()
}
diff --git a/crates/openhuman-core/src/channels/providers/relay/mod.rs b/crates/openhuman-core/src/channels/providers/relay/mod.rs
index 3788f23a2b..0c408159d4 100644
--- a/crates/openhuman-core/src/channels/providers/relay/mod.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/mod.rs
@@ -29,6 +29,7 @@ mod store;
pub use channel::{RelayChannel, CHANNEL_OUTBOUND_EVENT};
pub use ops::channel_relay_inbound;
+pub(crate) use ops::LegacyHistoryImport;
pub use params::{
validate_channel, validate_id, RelayInboundParams, DEFAULT_RELAY_CLIENT_ID, MAX_CHANNEL_LEN,
MAX_ID_LEN, MAX_SENDER_NAME_CHARS, MAX_TEXT_BYTES, RESERVED_CHANNELS,
diff --git a/crates/openhuman-core/src/channels/providers/relay/ops.rs b/crates/openhuman-core/src/channels/providers/relay/ops.rs
index 3a9072da33..76ab32ac0e 100644
--- a/crates/openhuman-core/src/channels/providers/relay/ops.rs
+++ b/crates/openhuman-core/src/channels/providers/relay/ops.rs
@@ -2,6 +2,7 @@
use std::collections::HashMap;
use std::sync::Arc;
+use std::path::PathBuf;
use serde_json::{json, Value};
@@ -17,6 +18,52 @@ use crate::config::Config;
use crate::core::Outcome;
use crate::security::SecurityPolicy;
+/// Deferred one-time import of the relay's older conversation-row history.
+/// The durable TinyAgents session is checked first by the session host, so a
+/// failure in this legacy store cannot block an already-established session.
+#[derive(Clone, Debug)]
+pub(crate) struct LegacyHistoryImport {
+ workspace_dir: PathBuf,
+ thread_id: String,
+ message_id: String,
+ limit: usize,
+}
+
+impl LegacyHistoryImport {
+ pub(crate) fn new(
+ workspace_dir: PathBuf,
+ thread_id: String,
+ message_id: String,
+ limit: usize,
+ ) -> Self {
+ Self { workspace_dir, thread_id, message_id, limit }
+ }
+
+ pub(crate) async fn load(
+ &self,
+ ctx: &crate::core::runtime::DispatchCtx,
+ ) -> Result<Vec<tinyagents_session::transcript::TranscriptMessage>, String> {
+ let rows = store::prior_turns(
+ ctx,
+ &self.workspace_dir,
+ &self.thread_id,
+ &self.message_id,
+ self.limit,
+ )
+ .await?;
+ Ok(rows
+ .into_iter()
+ .map(|row| match row.role.as_str() {
+ "user" => tinyagents_session::transcript::TranscriptMessage::user(row.content),
+ "assistant" => {
+ tinyagents_session::transcript::TranscriptMessage::assistant(row.content)
+ }
+ _ => unreachable!("prior_turns only returns user and assistant rows"),
+ })
+ .collect())
+ }
+}
+
/// Accept one relayed message: validate it, record it on its thread under the
/// caller's workspace, and start its turn in the background. Answers at once,
/// with the thread and request ids the replies will carry.
@@ -224,50 +271,6 @@ pub(crate) async fn run_relay_turn(
return channel.sent();
}
};
- let imported_history = match store::prior_turns(
- &dispatch_ctx,
- &workspace_dir,
- &thread_id,
- &params.message_id,
- MAX_CHANNEL_HISTORY,
- )
- .await
- {
- Ok(prior) => prior
- .into_iter()
- .map(|row| match row.role.as_str() {
- "user" => tinyagents_session::transcript::TranscriptMessage::user(row.content),
- "assistant" => {
- tinyagents_session::transcript::TranscriptMessage::assistant(row.content)
- }
- _ => unreachable!("prior_turns only returns user and assistant rows"),
- })
- .collect(),
- Err(error) => {
- tracing::warn!(
- thread_id = %thread_id,
- %error,
- "[channels::relay] could not read the thread's history; refusing to start a session without it"
- );
- let reply =
- "⚠️ This chat's history could not be loaded. Please send your message again.";
- let _ = tinychannels_runtime::Channel::send(
- channel.as_ref(),
- &crate::channels::SendMessage::new(reply, &params.chat_id),
- )
- .await;
- record(
- &dispatch_ctx,
- &workspace_dir,
- &params,
- &thread_id,
- &channel.sent(),
- )
- .await;
- return channel.sent();
- }
- };
-
let tenant = match dispatch_ctx.tenant() {
Ok(tenant) => tenant,
Err(error) => {
@@ -299,7 +302,12 @@ pub(crate) async fn run_relay_turn(
);
let mut runtime_message = crate::channels::runtime::RuntimeChannelMessage::from(message);
runtime_message.prepared_inbound = Some(prepared);
- runtime_message.imported_history = imported_history;
+ runtime_message.legacy_history = Some(LegacyHistoryImport::new(
+ workspace_dir.clone(),
+ thread_id.clone(),
+ params.message_id.clone(),
+ MAX_CHANNEL_HISTORY,
+ ));
runtime_message.progressive_target = Some(progressive_target);
let turn = process_channel_runtime_message(ctx, runtime_message);
if let Some(context) = dispatch_ctx.core() {
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
index 463a8edf2d..92d0805b11 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/host_agent/turn.rs
@@ -35,6 +35,7 @@ pub(crate) struct HostChannelTurn {
pub(crate) turn_deadline: Option<std::time::Duration>,
/// Legacy SaaS relay history to adopt into the durable session once.
pub(crate) imported_history: Vec<tinyagents_session::transcript::TranscriptMessage>,
+ pub(crate) legacy_history: Option<crate::channels::providers::relay::LegacyHistoryImport>,
}
/// Run `turn` as `host`: its definition and host tools, inside its context,
@@ -56,6 +57,7 @@ pub(crate) async fn run_host_agent_turn(
cancellation,
turn_deadline,
imported_history,
+ legacy_history,
} = turn;
let mut config = host.config.clone();
let profile_ceiling = host
@@ -98,6 +100,7 @@ pub(crate) async fn run_host_agent_turn(
cancellation,
turn_deadline,
&imported_history,
+ legacy_history,
)
.await?;
Ok(AgentTurnResponse::with_resolved_route(
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
index e51a5a0cd0..3ec17ea840 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
@@ -186,6 +186,7 @@ impl TurnHost<QueuedPayload> for NativeHost {
cancellation,
turn_deadline: None,
imported_history: trusted.imported_history.clone(),
+ legacy_history: trusted.legacy_history.clone(),
},
)
.await
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
index 2ea7771459..5c055e5106 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor.rs
@@ -34,6 +34,7 @@ pub(crate) struct RuntimeChannelMessage {
pub(crate) authenticated_relay: bool,
pub(crate) prepared_inbound: Option<tinychannels_bus::module_inbound::PreparedChannelInbound>,
pub(crate) imported_history: Vec<tinyagents_session::transcript::TranscriptMessage>,
+ pub(crate) legacy_history: Option<crate::channels::providers::relay::LegacyHistoryImport>,
/// Trusted per-turn callback target installed by a hosted relay. Native
/// configured channels resolve their target from the prepared envelope.
pub(crate) progressive_target: Option<Arc<progressive::NativeProgressiveTarget>>,
@@ -50,6 +51,7 @@ impl RuntimeChannelMessage {
authenticated_relay: false,
prepared_inbound: None,
imported_history: Vec::new(),
+ legacy_history: None,
progressive_target: None,
}
}
@@ -65,6 +67,7 @@ impl RuntimeChannelMessage {
authenticated_relay: delivery.delivered_via_authenticated_relay,
prepared_inbound: None,
imported_history: Vec::new(),
+ legacy_history: None,
progressive_target: None,
}
}
@@ -78,6 +81,7 @@ impl From<ChannelMessage> for RuntimeChannelMessage {
authenticated_relay: false,
prepared_inbound: None,
imported_history: Vec::new(),
+ legacy_history: None,
progressive_target: None,
}
}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index adec973290..632d60833b 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -33,6 +33,7 @@ pub(crate) async fn process_channel_runtime_message(
authenticated_relay,
prepared_inbound,
imported_history,
+ legacy_history,
progressive_target,
} = runtime_msg;
let prepared = match prepared_inbound {
@@ -396,6 +397,7 @@ pub(crate) async fn process_channel_runtime_message(
approval: approval.clone(),
relay_thread: ctx.relay_thread.clone(),
imported_history,
+ legacy_history,
native_target: native_target.clone(),
});
super::super::native_runner::run(