Skip to content

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

refactor(agent): extract prefix preparation into a helper

Steven Enamakelee1fb3fe8af1
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index 34971cfd56..441bc99b32 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -864,16 +864,15 @@ impl OpenHumanSessionHost {
);
options.run_context.data.memory_turn = memory_turn;
request.input = user_message_from_text(&enriched);
- // Imported conversations can already be durable while
- // lacking the prefix that the first host turn must
- // commit. The runtime exposes that distinction
- // separately from `resumed`; only an authoritative or
- // inferred frozen prefix suppresses prompt preparation.
- let needs_prefix = !view.prefix_is_frozen;
- let mut preparation =
- prelude.prepare(new_session || needs_prefix).await.map_err(
- |error| tinyagents_runtime::RuntimeError::Driver(error.to_string()),
- )?;
+ let mut preparation = prelude
+ .prepare(turn::prepare_prefix_for_turn(
+ new_session,
+ view.prefix_is_frozen,
+ ))
+ .await
+ .map_err(|error| {
+ tinyagents_runtime::RuntimeError::Driver(error.to_string())
+ })?;
prelude.refresh_permanent_prefix(&mut preparation, view.prefix);
if turn.overrides.suppress_tools {
// One-off tool-less turn: must not become the
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
index ab37b2fe7b..9427e61ff4 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_turn.rs
@@ -6,6 +6,10 @@ use crate::agent::{
tinyagents::host::OpenHumanRunContext,
};
+pub(super) fn prepare_prefix_for_turn(new_session: bool, prefix_is_frozen: bool) -> bool {
+ new_session || !prefix_is_frozen
+}
+
impl OpenHumanSessionHost {
/// Dispatch a turn with authority supplied by its owning entry point.
pub async fn turn_with_origin(
diff --git a/crates/openhuman-core/src/agent/turn_host.rs b/crates/openhuman-core/src/agent/turn_host.rs
index a4dd3e7295..8eff06c192 100644
--- a/crates/openhuman-core/src/agent/turn_host.rs
+++ b/crates/openhuman-core/src/agent/turn_host.rs
@@ -71,7 +71,10 @@ pub(crate) enum WorkspaceBinding {
}
impl WorkspaceBinding {
- fn apply(self, current: Option<tinytools::WorkspaceDescriptor>) -> Option<tinytools::WorkspaceDescriptor> {
+ fn apply(
+ self,
+ current: Option<tinytools::WorkspaceDescriptor>,
+ ) -> Option<tinytools::WorkspaceDescriptor> {
match self {
Self::Preserve => current,
Self::Clear => None,
@@ -104,9 +107,7 @@ impl AgentTurnHost {
.turn_deadline
.map(tinyagents_harness::turn_deadline::TurnDeadline::starting_now);
session.set_host_env(host_env);
- let workspace = request
- .workspace
- .apply(session.live_workspace_descriptor());
+ let workspace = request.workspace.apply(session.live_workspace_descriptor());
session.set_workspace_descriptor(workspace);
// Keep the large agent future behind a pointer while the origin scope
diff --git a/crates/openhuman-core/src/agent/turn_host_tests.rs b/crates/openhuman-core/src/agent/turn_host_tests.rs
new file mode 100644
index 0000000000..c6a311be83
--- /dev/null
+++ b/crates/openhuman-core/src/agent/turn_host_tests.rs
@@ -0,0 +1,23 @@
+use super::*;
+
+#[test]
+fn workspace_binding_clears_a_previous_turns_workspace() {
+ let previous = tinytools::WorkspaceDescriptor::new("/previous/thread");
+
+ assert_eq!(WorkspaceBinding::Clear.apply(Some(previous)), None);
+}
+
+#[test]
+fn workspace_binding_can_preserve_or_replace_the_session_workspace() {
+ let previous = tinytools::WorkspaceDescriptor::new("/configured");
+ let requested = tinytools::WorkspaceDescriptor::new("/thread");
+
+ assert_eq!(
+ WorkspaceBinding::Preserve.apply(Some(previous.clone())),
+ Some(previous)
+ );
+ assert_eq!(
+ WorkspaceBinding::Set(requested.clone()).apply(None),
+ Some(requested)
+ );
+}