Skip to content

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

refactor(cron): update session key usage in origin delivery

Steven Enamakel3469cf18225d
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_context.rs b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
index ca25b30ed7..be6cd403ae 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_context.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
@@ -100,10 +100,8 @@ async fn origin_messages(
&crate::agent::session_store::agent_key_for(&tenant, &agent_id),
&config.workspace_dir,
);
- let session = tinyagents_session::transcript::SessionRef::scoped(
- session_key,
- agent_id,
- );
+ let session =
+ tinyagents_session::transcript::SessionRef::scoped(session_key, agent_id);
let head = locator.head_generation(&session);
locator
.read_session_transcript(&head)
@@ -136,7 +134,7 @@ async fn origin_messages(
/// Validate persisted channel identity claims against the current callback
/// tenant and the config-owned channel-to-agent selection.
-fn validated_channel_session(
+pub(super) fn validated_channel_session(
ctx: &DispatchCtx,
config: &Config,
channel: &str,
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
index 480cc7498d..d5afc367f5 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_delivery.rs
@@ -39,7 +39,7 @@ pub(crate) fn origin_request_id(job_id: &str, run_id: &str) -> String {
/// Everything the web agent-transcript append needs about one delivered reply.
pub(crate) struct TranscriptAppend<'a> {
- pub thread_id: &'a str,
+ pub session_key: &'a str,
/// The agent that owns the thread's session (its transcript is keyed by
/// thread id and agent id).
pub agent_id: &'a str,
@@ -75,7 +75,7 @@ pub(crate) async fn append_to_origin_transcript(
append_background_message_in, BackgroundAppend, BackgroundAppendOutcome, SessionRef,
TranscriptMessage,
};
- let session = SessionRef::scoped(append.thread_id.to_string(), append.agent_id.to_string());
+ let session = SessionRef::scoped(append.session_key.to_string(), append.agent_id.to_string());
let options = BackgroundAppend::new(
append.idempotency_key,
serde_json::json!({
@@ -191,6 +191,14 @@ pub(crate) async fn deliver_to_origin(
})
.await
.map_err(|e| anyhow!("the host could not deliver the reply: {e}"))?;
+ if let JobOrigin::Channel {
+ channel,
+ channel_session: Some(identity),
+ ..
+ } = origin
+ {
+ append_channel_transcript(ctx, config, channel, identity, job, run_id, text).await?;
+ }
return Ok(DeliveryStatus::Delivered);
}
@@ -214,10 +222,17 @@ pub(crate) async fn deliver_to_origin(
JobOrigin::Channel {
channel,
reply_target,
- history_key,
+ channel_session,
thread_id,
..
} => {
+ let identity = channel_session
+ .as_deref()
+ .ok_or_else(|| anyhow!("channel origin has no durable session identity"))?;
+ let (session_key, agent_id, _tenant) =
+ super::origin_context::validated_channel_session(ctx, config, channel, identity)
+ .map_err(|error| anyhow!("invalid channel origin: {error}"))?
+ .ok_or_else(|| anyhow!("channel origin has no durable session identity"))?;
let text = strip_openhuman_link_markup(text);
channel_bridge::send_to_channel(
channel,
@@ -228,7 +243,20 @@ pub(crate) async fn deliver_to_origin(
)
.await
.map_err(|e| anyhow!(e))?;
- channel_bridge::append_assistant_message(history_key, &text);
+ let transcripts = origin_channel_transcripts(ctx, config, identity, &agent_id)?;
+ append_to_origin_transcript(
+ transcripts.as_ref(),
+ &TranscriptAppend {
+ session_key: &session_key,
+ agent_id: &agent_id,
+ text: &text,
+ idempotency_key: &origin_request_id(&job.id, run_id),
+ job_id: &job.id,
+ run_id,
+ },
+ )
+ .await
+ .map_err(|error| anyhow!("could not persist channel origin reply: {error}"))?;
tracing::debug!(
job_id = %job.id,
run_id = %run_id,
@@ -240,6 +268,57 @@ pub(crate) async fn deliver_to_origin(
}
}
+async fn append_channel_transcript(
+ ctx: &DispatchCtx,
+ config: &Config,
+ channel: &str,
+ identity: &tinyflows_bus::schedule::types::ChannelSessionIdentity,
+ job: &CronJob,
+ run_id: &str,
+ text: &str,
+) -> Result<()> {
+ let (session_key, agent_id, _) =
+ super::origin_context::validated_channel_session(ctx, config, channel, identity)
+ .map_err(|error| anyhow!("invalid channel origin: {error}"))?
+ .ok_or_else(|| anyhow!("channel origin has no durable session identity"))?;
+ let transcripts = origin_channel_transcripts(ctx, config, identity, &agent_id)?;
+ append_to_origin_transcript(
+ transcripts.as_ref(),
+ &TranscriptAppend {
+ session_key: &session_key,
+ agent_id: &agent_id,
+ text,
+ idempotency_key: &origin_request_id(&job.id, run_id),
+ job_id: &job.id,
+ run_id,
+ },
+ )
+ .await
+ .map_err(|error| anyhow!("could not persist channel origin reply: {error}"))
+}
+
+fn origin_channel_transcripts(
+ ctx: &DispatchCtx,
+ config: &Config,
+ identity: &tinyflows_bus::schedule::types::ChannelSessionIdentity,
+ agent_id: &str,
+) -> Result<std::sync::Arc<dyn tinyagents_session::transcript::TranscriptLocator>> {
+ let core = ctx
+ .core()
+ .ok_or_else(|| anyhow!("channel origin has no resolved core context"))?;
+ let tenant = ctx
+ .tenant()
+ .map_err(|error| anyhow!("could not resolve channel origin tenant: {error}"))?;
+ if identity.tenant_key != crate::core::runtime::tenant_key(&tenant, "") {
+ return Err(anyhow!("channel origin belongs to a different tenant"));
+ }
+ Ok(crate::agent::session_store::transcripts_or_files_in(
+ core,
+ &crate::agent::session_store::agent_key_for(&tenant, agent_id),
+ &config.workspace_dir,
+ ))
+}
+
async fn deliver_to_web_thread(
ctx: &DispatchCtx,
config: &Config,
@@ -304,7 +383,7 @@ async fn deliver_to_web_thread(
if let Err(e) = append_to_origin_transcript(
transcripts.as_ref(),
&TranscriptAppend {
- thread_id,
+ session_key: thread_id,
agent_id: &agent_id,
text,
idempotency_key: &request_id,