Skip to content

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

fix(agent_chat): hold resolved core lease for the duration of a turn

Steven Enamakel781a1c749630
diff --git a/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs b/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs
index 6b9958b3db..ad5280535d 100644
--- a/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs
+++ b/crates/openhuman-core/src/agent_runs/agent_chat/controllers.rs
@@ -134,11 +134,15 @@ fn handle_agent_chat(
Box::pin(async move {
let params = deserialize_params::<InferenceAgentChatParams>(params)?;
let mut config = ctx.load_config().await?;
+ // Keep the resolved owner alive until the one-shot turn (including
+ // spawned tools) has settled. The run context carries only a weak
+ // reference so a persisted session cannot pin a tenant indefinitely.
+ let core_lease = ctx.resolved();
let target = match params.agent_id.as_deref().map(str::trim) {
Some(id) if !id.is_empty() => super::AgentChatTarget::AgentId(id),
_ => super::AgentChatTarget::Orchestrator,
};
- super::session_turn::agent_chat_reply_for_with_headless(
+ let reply = super::session_turn::agent_chat_reply_for_with_headless(
&mut config,
target,
&params.common.message,
@@ -158,15 +162,16 @@ fn handle_agent_chat(
}),
super::AgentChatEnv {
host: crate::agent::turn_env::HostTurnEnv {
- core: ctx.downgrade(),
+ core: core_lease.as_ref().map(std::sync::Arc::downgrade),
..Default::default()
},
..Default::default()
},
ctx.is_headless_agent_chat(),
)
- .await?
- .into_rpc_json()
+ .await?;
+ drop(core_lease);
+ reply.into_rpc_json()
})
}
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 7c956a9c9e..bac5990290 100644
--- a/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
+++ b/crates/openhuman-core/src/web_chat/ops/turn_guards.rs
@@ -241,12 +241,13 @@ where
request_id,
});
let deadline = resolve_web_turn_deadline(ctx).await;
+ // Keep the resolved owner alive through the complete delegated run. The
+ // run context receives only a weak handle, so stored sessions do not pin
+ // tenant contexts after the request ends.
+ let core_lease = ctx.resolved();
let (mut env, turn_cancellation) = turn_host_env(deadline);
- // The turn context crosses task boundaries with this value. Carry its
- // owner explicitly so tools (especially sub-agent persistence) do not
- // depend on a task-local CoreContext surviving the web-chat spawn.
- env.core = ctx.downgrade();
- run_until_stopped(
+ env.core = core_lease.as_ref().map(std::sync::Arc::downgrade);
+ let result = run_until_stopped(
cancel_token,
turn_cancellation,
drive_turn_with_deadline(
@@ -266,7 +267,9 @@ where
},
),
)
- .await
+ .await;
+ drop(core_lease);
+ result
}
/// Drive `turn` until it finishes or `cancel_token` fires.