Skip to content

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

feat(web_chat): replace boolean fork flag with a three-way CheckoutPolicy enum

Steven Enamakel36d3a78617c8
diff --git a/crates/openhuman-core/src/web_chat/ops/system_turn.rs b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
index 0934a93441..287fb9f52d 100644
--- a/crates/openhuman-core/src/web_chat/ops/system_turn.rs
+++ b/crates/openhuman-core/src/web_chat/ops/system_turn.rs
@@ -22,7 +22,7 @@ use crate::config::rpc as config_rpc;
use super::super::run_task::turn_error_poisons_session;
use super::super::session::{
- checkin_session_agent_if_vacant, checkout_session_agent, CheckedOutSession,
+ checkin_session_agent_if_vacant, checkout_session_agent, CheckedOutSession, CheckoutPolicy,
};
/// Client id stamped on host-authored turns. Never a real socket, so it never
@@ -61,7 +61,7 @@ pub async fn run_system_turn_on_thread(
None,
None,
None,
- /* fork */ false,
+ CheckoutPolicy::AdoptCached,
"",
)
.await
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index 963cf8f160..23af36607d 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -14,6 +14,7 @@ use super::ops::BudgetCorrelation;
use super::progress_bridge::spawn_progress_bridge;
use super::session::{
checkin_session_agent, checkout_session_agent, normalize_model_override, CheckedOutSession,
+ CheckoutPolicy,
};
use super::types::{ChatRequestMetadata, WebChatTaskResult};
use super::web_errors::{
@@ -108,7 +109,11 @@ pub(crate) async fn run_chat_task(
model_override,
temperature,
locale.as_deref(),
- fork,
+ if fork {
+ CheckoutPolicy::Fork
+ } else {
+ CheckoutPolicy::Exact
+ },
message,
)
.await?;
diff --git a/crates/openhuman-core/src/web_chat/session.rs b/crates/openhuman-core/src/web_chat/session.rs
index f7a22e233a..702b947186 100644
--- a/crates/openhuman-core/src/web_chat/session.rs
+++ b/crates/openhuman-core/src/web_chat/session.rs
@@ -139,6 +139,23 @@ pub(super) fn build_session_fingerprint(
}
}
+/// How `checkout_session_agent` treats the thread's cached entry.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub(crate) enum CheckoutPolicy {
+ /// A user turn: reuse the cached agent only when its
+ /// `SessionCacheFingerprint` matches this turn's model/temperature/agent,
+ /// otherwise rebuild (and cold-boot resume) with the new settings.
+ Exact,
+ /// A host-authored turn: reuse whatever agent the thread has, under the
+ /// settings the user's last turn chose, and hand it back with that same
+ /// fingerprint. The turn has no settings of its own, and rebuilding on a
+ /// mismatch would evict the warm session for nothing.
+ AdoptCached,
+ /// A parallel fork: never take or return the cached agent; build fresh from
+ /// the thread's durable history.
+ Fork,
+}
+
/// A session agent checked out of the per-thread cache for exactly one turn.
///
/// Every turn that runs on a conversation thread — a user turn, a
@@ -158,8 +175,7 @@ pub(crate) struct CheckedOutSession {
///
/// The entry is *removed* from the cache for the duration of the turn so two
/// turns can never drive one agent; `checkin_session_agent` /
-/// `checkin_session_agent_if_vacant` put it back. A `fork` (parallel turn)
-/// never touches the cache and always builds fresh from the history snapshot.
+/// `checkin_session_agent_if_vacant` put it back.
pub(crate) async fn checkout_session_agent(
config: &Config,
client_id: &str,
@@ -167,7 +183,7 @@ pub(crate) async fn checkout_session_agent(
model_override: Option<String>,
temperature: Option<f64>,
locale: Option<&str>,
- fork: bool,
+ policy: CheckoutPolicy,
// The message this turn is about to send, so a cold-boot seed from the
// conversation log can drop it when the client already stored it. Empty for
// a host-authored turn, whose notice is never in the store.
@@ -186,22 +202,24 @@ pub(crate) async fn checkout_session_agent(
// A forked (parallel) turn never reuses or evicts the shared cached agent —
// it always builds fresh from the history snapshot below.
- let prior = if fork {
+ let prior = if policy == CheckoutPolicy::Fork {
None
} else {
let mut sessions = super::ops::THREAD_SESSIONS.lock().await;
sessions.remove(&map_key)
};
- let (mut agent, was_built_fresh) = match prior {
- Some(entry) if entry.fingerprint == fingerprint => {
+ let (mut agent, fingerprint, was_built_fresh) = match prior {
+ Some(entry)
+ if entry.fingerprint == fingerprint || policy == CheckoutPolicy::AdoptCached =>
+ {
log::info!(
"[web-channel] reusing cached session agent id={} for client={} thread={}",
- target_agent_id,
+ entry.fingerprint.target_agent_id,
client_id,
thread_id
);
- (entry.agent, false)
+ (entry.agent, entry.fingerprint, false)
}
Some(prior_entry) => {
log::info!(
@@ -225,6 +243,7 @@ pub(crate) async fn checkout_session_agent(
temperature,
locale,
)?,
+ fingerprint,
true,
)
}
@@ -238,6 +257,7 @@ pub(crate) async fn checkout_session_agent(
temperature,
locale,
)?,
+ fingerprint,
true,
),
};
diff --git a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
index c65fe69cbb..6bda608a56 100644
--- a/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
+++ b/crates/openhuman-core/src/web_chat/session_checkout_tests.rs
@@ -12,7 +12,7 @@ use tinyagents_session::transcript::{write_transcript, TranscriptMeta};
use super::{
checkin_session_agent, checkin_session_agent_if_vacant, checkout_session_agent,
- CheckedOutSession,
+ CheckedOutSession, CheckoutPolicy,
};
use crate::agent::messages::{ChatMessage, ConversationMessage};
use crate::agent::OpenHumanSessionHost;
@@ -122,7 +122,7 @@ async fn checkout_cold_boots_from_the_thread_transcript_and_checkin_keeps_it_war
None,
None,
None,
- false,
+ CheckoutPolicy::AdoptCached,
"",
)
.await
@@ -149,7 +149,7 @@ async fn checkout_cold_boots_from_the_thread_transcript_and_checkin_keeps_it_war
None,
None,
None,
- false,
+ CheckoutPolicy::Exact,
"so lets do 20-30 days then?",
)
.await