Skip to content

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

feat(web_chat): add turn guards and turn close handling

Steven Enamakel19ea81b330a3
diff --git a/crates/openhuman-core/src/agent/schemas.rs b/crates/openhuman-core/src/agent/schemas.rs
index 9ec66822eb..329f62f74d 100644
--- a/crates/openhuman-core/src/agent/schemas.rs
+++ b/crates/openhuman-core/src/agent/schemas.rs
@@ -289,6 +289,7 @@ fn handle_chat_simple(ctx: DispatchCtx, params: Map<String, Value>) -> Controlle
let config = ctx.load_config().await?;
to_json(
crate::inference::host_runtime::rpc::agent_chat_simple(
+ &ctx,
&config,
&p.message,
p.model_override,
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
index 58cfe40ab6..8038323c95 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/agent_chat.rs
@@ -628,6 +628,7 @@ pub async fn agent_chat_reply_for(
/// A simplified chat interface that does not update the base configuration.
pub async fn agent_chat_simple(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
message: &str,
model_override: Option<String>,
@@ -666,7 +667,8 @@ pub async fn agent_chat_simple(
.map_err(|e| e.to_string())?;
(backend, resolved_model)
} else {
- providers::create_chat_model_with_model_id(
+ providers::create_chat_model_with_model_id_in(
+ ctx,
"chat",
&effective,
effective.default_temperature,
diff --git a/crates/openhuman-core/src/inference/host_runtime/schemas.rs b/crates/openhuman-core/src/inference/host_runtime/schemas.rs
index a088eba570..26fe30c3b9 100644
--- a/crates/openhuman-core/src/inference/host_runtime/schemas.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/schemas.rs
@@ -256,6 +256,7 @@ fn handle_agent_chat_simple(ctx: DispatchCtx, params: Map<String, Value>) -> Con
let config = ctx.load_config().await?;
to_json(
crate::inference::host_runtime::ops::agent_chat_simple(
+ &ctx,
&config,
&p.message,
p.model_override,
diff --git a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
index 5b7da8c692..88e03b89bf 100644
--- a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
@@ -95,6 +95,7 @@ enum Stop {
/// harness wind-down. `run_chat_task` taps the turn's progress stream into the
/// [`TurnActivity`] built here and handed to the turn.
async fn drive_turn_with_deadline<B, F>(
+ ctx: &crate::core::runtime::DispatchCtx,
deadline: Option<Duration>,
idle_window: Option<Duration>,
ids: Option<TurnIds>,
@@ -172,6 +173,7 @@ where
stuck,
config,
locale,
+ ctx: ctx.clone(),
};
let close = super::super::turn_close::close_reply(stopped);
match tokio::time::timeout(CLOSE_REPLY_BUDGET, close).await {
@@ -244,6 +246,7 @@ where
cancel_token,
turn_cancellation,
drive_turn_with_deadline(
+ ctx,
deadline,
turn_activity::configured_idle_window(),
ids,
diff --git a/crates/openhuman-core/src/web_chat/ops/turn_guards_tests.rs b/crates/openhuman-core/src/web_chat/ops/turn_guards_tests.rs
index e4cd7043e5..97a350f154 100644
--- a/crates/openhuman-core/src/web_chat/ops/turn_guards_tests.rs
+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards_tests.rs
@@ -43,7 +43,8 @@ fn guarded_turn_env_shares_its_cancellation_with_the_stop_handle() {
/// error, which is classified to user-facing copy, not silence.
#[tokio::test(start_paused = true)]
async fn backstop_firing_yields_a_classified_turn_timeout_error() {
- let err = drive_turn_with_deadline(Some(Duration::from_secs(900)), None, None, |_| async {
+ let err = drive_turn_with_deadline(
+ &crate::core::runtime::DispatchCtx::host_default(),Some(Duration::from_secs(900)), None, None, |_| async {
std::future::pending::<()>().await;
Ok(result("never"))
})
@@ -72,7 +73,8 @@ const CEILING: Option<Duration> = Some(Duration::from_secs(3_600));
/// ceiling.
#[tokio::test(start_paused = true)]
async fn a_turn_that_keeps_progressing_outlives_the_idle_window() {
- let res = drive_turn_with_deadline(CEILING, IDLE, None, |activity| async move {
+ let res = drive_turn_with_deadline(
+ &crate::core::runtime::DispatchCtx::host_default(),CEILING, IDLE, None, |activity| async move {
for _ in 0..90 {
tokio::time::sleep(Duration::from_secs(17)).await;
activity.observe(&text_delta());
@@ -89,7 +91,8 @@ async fn a_turn_that_keeps_progressing_outlives_the_idle_window() {
#[tokio::test(start_paused = true)]
async fn a_silent_turn_ends_after_the_idle_window() {
let started = tokio::time::Instant::now();
- let err = drive_turn_with_deadline(CEILING, IDLE, None, |_| async {
+ let err = drive_turn_with_deadline(
+ &crate::core::runtime::DispatchCtx::host_default(),CEILING, IDLE, None, |_| async {
std::future::pending::<()>().await;
Ok(result("never"))
})
@@ -108,7 +111,8 @@ async fn a_silent_turn_ends_after_the_idle_window() {
/// A turn that stays busy forever is still bounded by the fixed ceiling.
#[tokio::test(start_paused = true)]
async fn a_turn_busy_past_the_ceiling_hits_the_backstop() {
- let err = drive_turn_with_deadline(CEILING, IDLE, None, |activity| async move {
+ let err = drive_turn_with_deadline(
+ &crate::core::runtime::DispatchCtx::host_default(),CEILING, IDLE, None, |activity| async move {
loop {
tokio::time::sleep(Duration::from_secs(30)).await;
activity.observe(&text_delta());
diff --git a/crates/openhuman-core/src/web_chat/turn_close.rs b/crates/openhuman-core/src/web_chat/turn_close.rs
index 3c8d211f02..e17a190391 100644
--- a/crates/openhuman-core/src/web_chat/turn_close.rs
+++ b/crates/openhuman-core/src/web_chat/turn_close.rs
@@ -90,6 +90,8 @@ pub(crate) struct StoppedTurn<'a> {
pub(crate) config: Option<std::sync::Arc<crate::config::Config>>,
/// The turn's reply locale, so the close is written in its language.
pub(crate) locale: Option<String>,
+ /// The context the stopped turn ran under, for the closing model call.
+ pub(crate) ctx: crate::core::runtime::DispatchCtx,
}
impl StoppedTurn<'_> {
@@ -240,6 +242,7 @@ fn close_request(turn: &StoppedTurn<'_>, evidence: &Evidence) -> ModelRequest {
}
async fn model_reply(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &crate::config::Config,
turn: &StoppedTurn<'_>,
evidence: &Evidence,
@@ -251,7 +254,7 @@ async fn model_reply(
}
let request_id = turn.request_id;
let (chat_model, model) =
- match provider::create_chat_model_with_model_id("summarization", config, 0.2) {
+ match provider::create_chat_model_with_model_id_in(ctx, "summarization", config, 0.2) {
Ok(resolved) => resolved,
Err(error) => {
log::warn!("{LOG_PREFIX} provider init failed request_id={request_id}: {error}");
@@ -327,7 +330,7 @@ pub(crate) async fn close_reply(turn: StoppedTurn<'_>) -> Option<WebChatTaskResu
)
.await;
let evidence = Evidence::from_snapshot(snapshot.as_ref());
- let reply = match model_reply(&config, &turn, &evidence).await {
+ let reply = match model_reply(&turn.ctx, &config, &turn, &evidence).await {
Some(reply) => reply,
None => plain_reply(&turn, &evidence),
};
diff --git a/crates/openhuman-core/src/web_chat/turn_close_tests.rs b/crates/openhuman-core/src/web_chat/turn_close_tests.rs
index 610266be02..d31bf0827c 100644
--- a/crates/openhuman-core/src/web_chat/turn_close_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_close_tests.rs
@@ -48,6 +48,7 @@ fn stopped(cause: StopCause, stuck: StuckPoint) -> StoppedTurn<'static> {
stuck,
config: None,
locale: None,
+ ctx: crate::core::runtime::DispatchCtx::host_default(),
}
}