The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
Merge upstream main into resume-current-prompt
Shanu823b56e2e9e5
diff --cc crates/openhuman-core/src/agent/session_host/README.mdindex 1510d04bce,6277504eaf..c2c7eb90eb--- a/crates/openhuman-core/src/agent/session_host/README.md+++ b/crates/openhuman-core/src/agent/session_host/README.md@@@ -43,30 -39,16 +39,30 @@@ does not mint stems, seed history by haA resumed session's prompt and tool list are frozen: both were recorded onthe transcript, and restoring them verbatim is what keeps the provider's- prefix cache warm across a restart. `recorded_tools.rs` exists because of+ prefix cache warm across a restart. `integrations/recorded_tools.rs` exists because ofthat freeze: it turns previously recorded Composio actions back intodeferred executors instead of dropping them when a cache goes cold, and- `prelude_integrations.rs` re-fetches integrations on the first turn of every+ `integrations/prelude_integrations.rs` re-fetches integrations on the first turn of everysession instance rather than only on a brand-new thread.+An agent whose definition sets `refresh_prompt_on_resume` opts out of the+prompt half of that freeze. `runtime_session/prompt_refresh.rs` renders its+prompt again on every resumed turn and offers it as a refreshing prefix; the+runtime applies it only when it differs from the recorded one, and a+thread-bound session then seals the current generation and carries the+conversation into the next. That turn's user message opens with a one-time+`[instructions update]` note: the conversation before it was written under the+old prompt, and a model otherwise keeps to what it said there (that a feature+is "coming soon") over the new instructions. A verbatim prompt therefore costs+one uncached prompt per thread per edit. A composed one also refreshes when+the live state it renders changes, but never from integrations the session has+not read yet. A prompt that fails to render, or a recorded prompt of unknown+extent, keeps the recorded prompt. The recorded tool list is unaffected.+Compaction never erases history. It seals the current generation and opensthe next one, recording the sealed generation as the new one's parent; the- sealed file stays on disk, and `session_api.rs` is what lets a caller list a- long conversation's generations without reading the whole chain.+ sealed file stays on disk. TinyAgents owns the generation chain and its+ transcript lookup.## Where nextdiff --cc crates/openhuman-core/src/agent/session_host/runtime_session.rsindex c47e859742,ede5bf4bb1..067d464a85--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs@@@ -1,16 -1,21 +1,22 @@@//! The live OpenHuman composition over `tinyagents_runtime::Session`.//!- //! The mutex is intentionally narrow: it carries host preparation and- //! finalization observations only. Generic model history, transcript rows,- //! prefix reconciliation, tool snapshots, resume and persistence remain inside- //! the runtime session.+ //! Host hooks carry product state; `tinyagents_runtime` owns history,+ //! transcripts, prefixes, snapshots, resume, and persistence.- #[path = "runtime_session_attachment_input.rs"]- mod attachment_input;- mod memory_ingest;mod permanent;mod post_commit;+ #[path = "runtime_session/prelude.rs"]+ mod prelude;+ #[path = "runtime_session/prompt.rs"]+ mod prompt;+mod prompt_refresh;+ #[path = "runtime_session/prompt_surface.rs"]+ mod prompt_surface;+ #[path = "runtime_session_usage.rs"]+ mod usage;+ pub(super) use usage::{account_committed_turn_against_goal, holistic_last_turn_usage};+ #[path = "../../memory/lifecycle/session_hooks.rs"]+ mod session_hooks;mod tool_rules;mod transcript;#[path = "runtime_session_turn.rs"]diff --cc crates/openhuman-core/src/agent/session_host/runtime_session/prelude.rsindex 0000000000,1bf3127378..af95d4b36cmode 000000,100644..100644--- a/crates/openhuman-core/src/agent/session_host/runtime_session/prelude.rs+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/prelude.rs@@@ -1,0 -1,358 +1,367 @@@+ //! Host-specific model/tool/prompt preparation for one runtime turn.++ use super::*;++ impl OpenHumanTurnPrelude {+ #[allow(clippy::type_complexity)]+ pub(super) fn current_tool_source(+ &self,+ ) -> (+ Arc<Vec<Box<dyn tinytools::Tool>>>,+ Arc<Vec<Box<dyn tinytools::Tool>>>,+ tinytools::ToolPolicySession,+ String,+ ) {+ let surface = self+ .tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ (+ surface.tools.clone(),+ surface.synthesized_tools.clone(),+ surface.tool_policy_session.clone(),+ surface.event_channel.clone(),+ )+ }++ /// The session's deferred set for this turn; see+ /// `OpenHumanRunContext::deferred_tool_names`.+ pub(super) fn current_deferred_tool_names(&self) -> std::collections::HashSet<String> {+ self.tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .deferred_tool_names+ .clone()+ }++ pub(super) fn replace_tool_surface(&self, surface: OpenHumanTurnToolSurface) {+ *self+ .tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner) = surface;+ }++ pub(super) async fn prepare(&self, cold: bool) -> Result<TurnPreparation> {+ let tools = {+ let surface = self+ .tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ // The snapshot is the declaration set the session grants — the+ // driver reads it back as the harness allowlist — so it carries+ // the deferred tools beside the advertised ones. The harness+ // still advertises only the `Direct` registrations and reaches+ // the rest through its `tool_search` bridge.+ ToolSnapshot::new(+ surface+ .visible_tool_specs+ .iter()+ .chain(+ surface+ .tool_specs+ .iter()+ .filter(|spec| surface.deferred_tool_names.contains(&spec.name)),+ )+ .filter(|spec| {+ self.thread_id.is_some()+ || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(+ &spec.name,+ )+ })+ .map(|spec| spec.as_ref().clone())+ .collect(),+ )+ .map_err(|error| anyhow::anyhow!(error.to_string()))?+ };- let flow_archetype_text = super::prompt::flow_archetype_text(self, cold).await?;+ let prefix = if cold {- let tiered = self- .build_system_prompt_tiered_with_flow_archetype(flow_archetype_text.as_deref())?;- Some(tinyagents_runtime::PrefixSnapshot::new(- tiered- .system_messages()- .into_iter()- .map(Message::system)- .collect(),- ))++ Some(self.cold_prompt_prefix().await?)+ } else {+ None+ };+ Ok(TurnPreparation {+ prefix,+ tools: Some(tools),+ })+ }+++ /// The prompt a new session starts with, rendered now: one system++ /// message per cache tier, with the definition's flow archetype when it++ /// has one. A resumed thread that refreshes its prompt++ /// (`prompt_refresh.rs`) renders it here too, so an unchanged prompt++ /// compares equal to the one its transcript recorded.++ pub(super) async fn cold_prompt_prefix(&self) -> Result<tinyagents_runtime::PrefixSnapshot> {++ let flow_archetype_text = super::prompt::flow_archetype_text(self, true).await?;++ let tiered =++ self.build_system_prompt_tiered_with_flow_archetype(flow_archetype_text.as_deref())?;++ Ok(tinyagents_runtime::PrefixSnapshot::new(++ tiered++ .system_messages()++ .into_iter()++ .map(Message::system)++ .collect(),++ ))++ }+++ pub(super) fn build_system_prompt_tiered(&self) -> Result<crate::agent::prompts::TieredPrompt> {+ self.build_system_prompt_tiered_with_flow_archetype(None)+ }++ pub(super) fn build_system_prompt_tiered_with_flow_archetype(+ &self,+ flow_archetype_text: Option<&str>,+ ) -> Result<crate::agent::prompts::TieredPrompt> {+ use crate::agent::prompts::{PromptContext, PromptTool};+ let surface = self+ .tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let withheld = self.rule_withheld_tools(&surface);+ let specs = surface+ .visible_tool_specs+ .iter()+ .filter(|spec| !withheld.contains(&spec.name))+ .filter(|spec| {+ self.thread_id.is_some()+ || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(&spec.name)+ })+ .map(|spec| spec.as_ref().clone())+ .collect::<Vec<_>>();+ let instructions = self.tool_dispatcher.prompt_instructions(&specs);+ let tool_refs = surface+ .tools+ .iter()+ .chain(surface.synthesized_tools.iter())+ .filter(|tool| {+ self.thread_id.is_some()+ || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(+ tool.name(),+ )+ })+ .map(|tool| tool.as_ref())+ .collect::<Vec<_>>();+ let mut prompt_tools = PromptTool::from_tool_refs(tool_refs.iter().copied());+ prompt_tools.retain(|tool| !surface.permanent_tool_names.contains(tool.name.as_ref()));+ prompt_tools.retain(|tool| !withheld.contains(tool.name.as_ref()));+ let mut visible_tool_names = surface.tool_policy_session.visible_tool_names_for_prompt();+ visible_tool_names.retain(|name| !withheld.contains(name));+ visible_tool_names.retain(|name| !surface.permanent_tool_names.contains(name));+ if self.thread_id.is_none() {+ visible_tool_names.retain(|name| {+ !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(name)+ });+ }+ crate::agent::prompts::swap_deferred_for_discovery_bridge(+ &mut prompt_tools,+ &mut visible_tool_names,+ &surface.deferred_tool_names,+ );+ let agents_md = if self.config.agents_md_enabled {+ crate::agent::prompts::load_agents_md_layers(&self.workspace_dir, &self.action_dir)+ } else {+ Default::default()+ };+ let mutable = self+ .mutable+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let context = PromptContext {+ workspace_dir: &self.workspace_dir,+ model_name: &self.model_name,+ agent_id: &self.agent_definition_name,+ flow_archetype_text,+ tools: &prompt_tools,+ workflows: &mutable.workflows,+ dispatcher_instructions: &instructions,+ visible_tool_names: &visible_tool_names,+ tool_call_format: self.tool_dispatcher.tool_call_format(),+ connected_integrations: &mutable.connected_integrations,+ connected_identities_md: self.connected_identities_md(),+ user_identity: crate::security::credentials::identity::peek_credential_user_identity(),+ personality_roster: vec![],+ agents_md_global: agents_md.global,+ agents_md_local: agents_md.local,+ };+ self.prompt_builder.build_tiered(&context)+ }++ pub(super) fn drain_host_events(&self) -> bool {+ let mut mutable = self+ .mutable+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if mutable.composio_events.is_none() {+ mutable.composio_events = crate::core::bus::BUS.get().map(|bus| bus.receiver());+ }+ if mutable.skill_events.is_none() {+ mutable.skill_events = crate::core::bus::BUS.get().map(|bus| bus.receiver());+ }+ let drain = |receiver: &mut Option<+ tinybus::events::EventReceiver<crate::core::events::DomainEvent>,+ >| {+ let Some(receiver) = receiver.as_mut() else {+ return false;+ };+ let mut skills_changed = false;+ loop {+ use tinybus::TryRecvError;+ match receiver.try_recv() {+ Ok(crate::core::events::DomainEvent::WorkflowsChanged { .. }) => {+ skills_changed = true+ }+ Ok(crate::core::events::DomainEvent::ComposioIntegrationsChanged {+ ..+ })+ | Ok(_) => {}+ Err(TryRecvError::Empty) => break,+ Err(TryRecvError::Lagged(_)) => {+ skills_changed = true;+ break;+ }+ Err(TryRecvError::Closed) => break,+ }+ }+ skills_changed+ };+ let composio_skills_changed = drain(&mut mutable.composio_events);+ composio_skills_changed | drain(&mut mutable.skill_events)+ }++ /// Assemble every dynamic, host-owned user-turn addition after the runtime+ /// has selected/resumed its transcript. The returned message is then the+ /// sole input which `Session` appends to generic history.+ pub(super) async fn enrich_request(+ &self,+ original_user_message: &str,+ turn: &super::super::types::TurnInputs,+ run_context: &mut OpenHumanRunContext,+ ) -> String {+ let mut context = String::new();++ let active_goal = if turn.overrides.suppress_active_goal {+ None+ } else {+ let loaded = crate::agent::goals::runtime::load_for_thread(+ &self.workspace_dir,+ self.thread_id.as_deref(),+ )+ .await;+ match loaded {+ Some(goal) if matches!(goal.status, ThreadGoalStatus::Paused) => {+ crate::agent::goals::runtime::resume_for_thread(+ &self.workspace_dir,+ self.thread_id.as_deref(),+ )+ .await+ .unwrap_or(Some(goal))+ }+ other => other,+ }+ };+ if let Some(goal) = &active_goal {+ if let Some(block) = tinyagents_graph::goals::active_goal_context_block(goal) {+ context.push_str(&block);+ }+ if let Some(hook) = tinyagents_hooks::stop::GoalBudgetStopHook::for_goal(+ crate::storage::sessions::stores_or_ephemeral(&self.workspace_dir).kv,+ goal,+ ) {+ run_context.stop_hooks.push(Arc::new(hook));+ }+ }+ // Build the roster from this turn's *effective* visible tool set+ // (snapshotted under the tool-surface lock, then released), not the+ // definition's static scope: a hide or named restriction can narrow what+ // this turn can call, and the roster must not advertise a control it cannot invoke.+ let turn_fleet = {+ let visible = self+ .tool_surface+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner)+ .visible_tool_names+ .clone();+ if visible.is_empty() {+ crate::agent::orchestration::fleet_tools::FleetToolSet::for_parent(+ &self.agent_definition_id,+ )+ } else {+ crate::agent::orchestration::fleet_tools::FleetToolSet::from_visible_tool_names(+ &visible,+ )+ }+ };+ if let Some(block) =+ crate::agent::orchestration::running_subagents::active_subagents_context_block(+ &self.dispatch_ctx(),+ &self.event_session_id,+ &self.workspace_dir,+ &turn_fleet,+ )+ {+ context.push_str(&block);+ }++ let mut enriched = if context.is_empty() {+ original_user_message.to_string()+ } else {+ format!("{context}{original_user_message}")+ };+ {+ let mut mutable = self+ .mutable+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ mutable.last_memory_context = (!context.is_empty()).then_some(context);+ }+ run_context.response_shape = turn.response_shape.clone();+ run_context.attach_parent(self.parent_context(run_context.parent.as_ref()));+ self.apply_pending_announcements(&mut enriched);+ run_context.attachment_placeholders =+ Arc::new(extract_image_placeholders_in_text(original_user_message));+ run_context.dispatch = Some(Arc::new(+ crate::agent::tinyagents::host::TurnDispatchState::new(+ crate::agent::tinyagents::agent_turn_wall_clock_ms_for(+ self.runtime_config+ .as_deref()+ .is_some_and(crate::agent::tinyagents::chat_provider_is_local),+ )+ .map(std::time::Duration::from_millis),+ ),+ ));+ run_context.sandbox_mode = Some(self.sandbox_mode);+ // Same pin as `SessionDriver::run_turn`: the harness speaks the dialect+ // the prompt was composed for, so a text dialect keeps its schemas off+ // the wire and renders the catalogue itself (`ToolsSection` no longer+ // does), and a code call is recovered against the positional registry.+ run_context.tool_dialect = tinyagents_harness::dialect::dispatcher_for_format(+ self.tool_dispatcher.tool_call_format(),+ );+ format!("{}\n\n{enriched}", turn.preamble())+ }++ fn apply_pending_announcements(&self, enriched: &mut String) {+ let mut mutable = self+ .mutable+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ for note in [+ integration_announcement_note(&std::mem::take(+ &mut mutable.pending_integration_announcement,+ )),+ mcp_announcement_note(&std::mem::take(&mut mutable.pending_mcp_announcement)),+ skill_announcement_note(&std::mem::take(&mut mutable.pending_skill_announcement)),+ skill_retraction_note(&std::mem::take(&mut mutable.pending_skill_retraction)),+ ]+ .into_iter()+ .flatten()+ {+ *enriched = format!("{note}\n\n{enriched}");+ }+ }++ pub(super) async fn finalize_after_durable_commit(+ &self,+ receipt: &CommitReceipt<OpenHumanRunContext>,+ ) {+ self.memory_post_turn(receipt);+ }+ }diff --cc crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh.rsindex 39d4bab613,0000000000..1db2058385mode 100644,000000..100644--- a/crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh.rs+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh.rs@@@ -1,160 -1,0 +1,162 @@@+//! The system prompt a resumed thread is sent.+//!+//! A resumed thread's prefix is frozen: the runtime restores the system+//! messages its transcript recorded and sends them verbatim, which keeps the+//! provider's prefix cache warm across a restart. A turn can still offer a+//! replacement, which the runtime applies only when it differs from what is+//! recorded; a thread-bound session then seals the transcript generation it+//! was on and carries its conversation into the next. Two things offer one:+//!+//! - the permanent-tool catalogue (`permanent.rs`), whenever it changes;+//! - the whole prompt, rendered afresh, when the agent's definition sets+//! `refresh_prompt_on_resume`, so prompt edits reach existing threads too.+//!+//! Whatever is offered, the turn's transcript snapshot boundary moves with+//! it: the request starts with the offered prefix, not the recorded one.+//!+//! When the whole prompt is replaced, the user's message on that turn opens+//! with a one-time note saying so (`PROMPT_UPDATE_NOTE`). The conversation+//! before it was written under the old prompt, and a model keeps to what it+//! already said there (that something is "coming soon", say) over+//! instructions that changed since.++use std::sync::PoisonError;++use tinyagents_definition::PromptSource;+use tinyagents_runtime::{+ PrefixSnapshot, RuntimeError, SessionStateView, SessionTurnRequest, TurnPreparation,+};+use tinyinference_llm::message::Message;++use super::OpenHumanTurnPrelude;+use crate::agent::message_convert::{user_message_from_text, user_text_with_markers};+use crate::agent::tinyagents::middleware::turn_context::TranscriptSnapshotSink;++/// Prepended to the user's message on the turn a resumed thread's prompt is+/// replaced; the turn's record keeps it, so later turns see where the change+/// happened.+pub(super) const PROMPT_UPDATE_NOTE: &str = "[instructions update] Your system instructions changed since the earlier messages in this conversation. Where they differ from anything said before, including what you said you could or could not do, follow your current instructions. This is an update, not a request: respond to the user's message below.";++impl OpenHumanTurnPrelude {+ /// This turn's tool snapshot and, when it changes, its prefix: what+ /// `prepare` builds, plus the refreshes a resumed thread's recorded+ /// prefix may take (see the module docs). `snapshot`'s request boundary+ /// is rebased onto the prefix offered, and `request` carries the+ /// `PROMPT_UPDATE_NOTE` when the prompt is replaced.+ pub(super) async fn prepare_turn(+ &self,+ new_session: bool,+ view: &SessionStateView<'_>,+ snapshot: &TranscriptSnapshotSink,+ request: &mut SessionTurnRequest,+ ) -> Result<TurnPreparation, RuntimeError> {+ let mut preparation = self+ .prepare(new_session)+ .await+ .map_err(|error| RuntimeError::Driver(error.to_string()))?;+ let refresh_prompt = view.resumed && self.refreshes_prompt_on_resume();+ if refresh_prompt {- preparation.prefix = self.current_prompt_prefix(view);++ preparation.prefix = self.current_prompt_prefix(view).await;+ }+ self.refresh_permanent_prefix(&mut preparation, view.prefix);+ if let Some(prefix) = preparation.prefix.as_ref() {+ // The runtime sends `prefix` in place of the recorded one, so the+ // rounds this run produces start that much further on (or back).+ let mut snapshot = snapshot.lock().unwrap_or_else(PoisonError::into_inner);+ snapshot.request_base_len = snapshot+ .request_base_len+ .saturating_sub(view.prefix.messages().len())+ + prefix.messages().len();+ }+ if refresh_prompt {+ let changed = preparation+ .prefix+ .as_ref()+ .is_some_and(|prefix| prefix.messages() != view.prefix.messages());+ if changed {+ tracing::info!(+ thread_id = ?self.thread_id,+ recorded_segments = view.prefix.messages().len(),+ "[session] refresh_prompt_on_resume: the resumed thread's prompt is out of date; it continues under the current one"+ );+ let text = user_text_with_markers(&request.input);+ request.input = user_message_from_text(&format!("{PROMPT_UPDATE_NOTE}\n\n{text}"));+ } else {+ tracing::debug!(+ thread_id = ?self.thread_id,+ "[session] refresh_prompt_on_resume: the resumed thread keeps its prompt"+ );+ }+ }+ Ok(preparation)+ }++ /// Whether the agent's definition asks for a resumed thread to take up+ /// the prompt rendered now (`refresh_prompt_on_resume`).+ fn refreshes_prompt_on_resume(&self) -> bool {+ self.definition+ .as_ref()+ .is_some_and(|definition| definition.extensions.refresh_prompt_on_resume)+ }++ /// The prompt this agent renders now, offered in place of the prefix the+ /// resumed thread in `view` restored from its transcript; `None` keeps+ /// the recorded prompt.+ ///+ /// It is kept when:+ ///+ /// - that prefix is empty while the history still opens on system rows:+ /// the extent of the recorded prompt could not be recovered (a+ /// compacted legacy head), so its old instructions are still part of+ /// the history, and a new prompt beside them would leave them in place;+ /// - the prompt renders live state (anything but a verbatim prompt) and+ /// the connected integrations have not been read this session: a prompt+ /// rendered without them would replace a complete one;+ /// - the prompt cannot be rendered: the thread goes on under the one it+ /// has rather than failing its turn.- fn current_prompt_prefix(&self, view: &SessionStateView<'_>) -> Option<PrefixSnapshot> {++ ///++ /// It is rendered as a new session's is (`cold_prompt_prefix`), so an++ /// unchanged prompt is offered byte for byte as recorded and writes++ /// nothing.++ async fn current_prompt_prefix(&self, view: &SessionStateView<'_>) -> Option<PrefixSnapshot> {+ let unknown_extent = view.prefix.messages().is_empty()+ && matches!(view.history.first(), Some(Message::System(_)));+ if unknown_extent {+ tracing::warn!(+ thread_id = ?self.thread_id,+ "[session] refresh_prompt_on_resume: the resumed thread's recorded prompt has no known extent; keeping it"+ );+ return None;+ }+ let verbatim = self.definition.as_ref().is_some_and(|definition| {+ matches!(definition.system_prompt, PromptSource::Verbatim(_))+ });+ let hydrated = {+ let mutable = self.mutable.lock().unwrap_or_else(PoisonError::into_inner);+ mutable.connected_integrations_initialized+ && mutable.connected_integrations_authoritative+ };+ if !verbatim && !hydrated {+ tracing::debug!(+ thread_id = ?self.thread_id,+ "[session] refresh_prompt_on_resume: integrations not read yet; keeping the recorded prompt"+ );+ return None;+ }- match self.build_system_prompt_tiered() {- Ok(tiered) => {- Some(super::super::prefix_snapshot::tiered_prefix_snapshot(&tiered).refreshing())- }++ match self.cold_prompt_prefix().await {++ Ok(prefix) => Some(prefix.refreshing()),+ Err(error) => {+ tracing::warn!(+ thread_id = ?self.thread_id,+ %error,+ "[session] refresh_prompt_on_resume: the current prompt did not render; keeping the recorded one"+ );+ None+ }+ }+ }+}++#[cfg(test)]+#[path = "prompt_refresh_tests.rs"]+mod tests;diff --cc crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh_tests.rsindex fdb6d67f16,0000000000..e5200c64a2mode 100644,000000..100644--- a/crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh_tests.rs+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/prompt_refresh_tests.rs@@@ -1,493 -1,0 +1,493 @@@+//! A resumed thread whose agent sets `refresh_prompt_on_resume` is sent the+//! prompt the agent renders now; without it, it keeps the one it recorded.++use super::*;+use std::sync::Arc;+- use crate::agent::harness::host_definition::{AgentDefinition, AgentDefinitionRegistry};+use crate::agent::prompts::{PromptContext, SystemPromptBuilder};- use crate::agent::session_store::splice_fixture::memory_locator;++use crate::agent_registry::host_definition::{AgentDefinition, AgentDefinitionRegistry};++use crate::storage::sessions::splice_fixture_tests::memory_locator;+use tinyagents_harness::testkit::ScriptedModel;+use tinyagents_session::transcript::TranscriptLocator;++const THREAD: &str = "thread-prompt-refresh";++/// A thread-bound session over `locator` that renders its prompt with+/// `prompt` and answers from `model`, under the registry's definition as+/// `define` edits it: the definition the embed factory stamps on its+/// sessions.+fn host_with(+ locator: &Arc<dyn TranscriptLocator>,+ model: &Arc<ScriptedModel>,+ root: &std::path::Path,+ prompt: SystemPromptBuilder,+ define: impl FnOnce(&mut AgentDefinition),+) -> crate::agent::OpenHumanSessionHost {+ let _ = AgentDefinitionRegistry::init_global_builtins();+ let chat_model: Arc<dyn tinyinference_llm::model::ChatModel<()>> = model.clone();+ let mut host = crate::agent::SessionHostBuilder::new()+ .with_session_history_locator(Arc::clone(locator))+ .chat_model(chat_model)+ .tools(Vec::new())+ .prompt_builder(prompt)+ .workspace_dir(root.join("workspace"))+ .action_dir(root.to_path_buf())+ .tool_dispatcher(Box::new(tinytools_agent::dialect::NativeDialect))+ .build()+ .expect("session build");+ let mut definition = (*host.resolved_definition().expect("registry definition")).clone();+ define(&mut definition);+ host.definition = Some(Arc::new(definition));+ host.set_thread_id(Some(THREAD));+ host+}++/// As [`host_with`], for an agent whose whole prompt is `prompt`, verbatim+/// (what embed's `bare_prompt` builds), with `refresh_prompt_on_resume` set+/// to `refresh`.+fn host(+ locator: &Arc<dyn TranscriptLocator>,+ model: &Arc<ScriptedModel>,+ root: &std::path::Path,+ prompt: &str,+ refresh: bool,+) -> crate::agent::OpenHumanSessionHost {+ host_with(+ locator,+ model,+ root,+ SystemPromptBuilder::verbatim(prompt.to_string()),+ |definition| {+ definition.system_prompt = PromptSource::Verbatim(prompt.to_string());+ definition.extensions.refresh_prompt_on_resume = refresh;+ },+ )+}++/// A model that answers each turn with the next of `replies`. The native+/// dialect resolves from the model's profile; a profile-less scripted model+/// would be treated as text-only, like no production model is.+fn model(replies: Vec<&str>) -> Arc<ScriptedModel> {+ Arc::new(+ ScriptedModel::replies(replies).with_profile(tinyinference_llm::model::ModelProfile {+ tool_calling: true,+ ..Default::default()+ }),+ )+}++/// The system messages of the model's latest request, in order.+fn sent_prompt(model: &ScriptedModel) -> Vec<String> {+ let requests = model.requests();+ let sent = &requests.last().expect("a turn reached the model").messages;+ sent.iter()+ .filter(|message| matches!(message, Message::System(_)))+ .map(Message::text)+ .collect()+}++/// Whether the model's latest request carries a message of `role` whose text+/// contains `needle`.+fn sent_contains(model: &ScriptedModel, role: fn(&Message) -> bool, needle: &str) -> bool {+ let requests = model.requests();+ let sent = &requests.last().expect("a turn reached the model").messages;+ sent.iter()+ .any(|message| role(message) && message.text().contains(needle))+}++/// The text of the last user message in the model's latest request.+fn last_user_text(model: &ScriptedModel) -> String {+ let requests = model.requests();+ let sent = &requests.last().expect("a turn reached the model").messages;+ sent.iter()+ .rev()+ .find(|message| is_user(message))+ .map(Message::text)+ .unwrap_or_default()+}++fn is_user(message: &Message) -> bool {+ matches!(message, Message::User(_))+}++fn is_assistant(message: &Message) -> bool {+ matches!(message, Message::Assistant(_))+}++#[test]+fn a_resumed_thread_takes_up_the_current_prompt_only_when_its_agent_opts_in() {+ std::thread::Builder::new()+ .stack_size(crate::core::runtime::AGENT_WORKER_STACK_BYTES)+ .spawn(|| {+ tokio::runtime::Builder::new_current_thread()+ .enable_all()+ .build()+ .expect("test runtime")+ .block_on(resumed_thread_takes_up_the_current_prompt_only_when_opted_in());+ })+ .expect("test thread")+ .join()+ .expect("test thread panicked");+}++async fn resumed_thread_takes_up_the_current_prompt_only_when_opted_in() {+ let root = tempfile::tempdir().expect("tempdir");+ let locator = memory_locator();+ let model = model(vec![+ "first reply",+ "second reply",+ "third reply",+ "fourth reply",+ ]);++ // A new thread is sent the prompt of its first turn.+ let mut first = host(&locator, &model, root.path(), "You are release one.", true);+ assert_eq!(first.turn("first message").await.unwrap(), "first reply");+ let session = first.session.clone().expect("a thread-bound session");+ drop(first);+ assert_eq!(sent_prompt(&model), ["You are release one."]);+ assert!(!last_user_text(&model).contains(PROMPT_UPDATE_NOTE));+ assert_eq!(locator.session_chain(&session).len(), 1);++ // After a restart with an edited prompt, the resumed thread is sent the+ // current prompt alone, with its whole conversation, and the prompt+ // change opens the transcript's next generation.+ let mut second = host(&locator, &model, root.path(), "You are release two.", true);+ assert_eq!(second.turn("second message").await.unwrap(), "second reply");+ drop(second);+ assert_eq!(sent_prompt(&model), ["You are release two."]);+ assert!(sent_contains(&model, is_user, "first message"));+ assert!(sent_contains(&model, is_assistant, "first reply"));+ assert!(sent_contains(&model, is_user, "second message"));+ assert!(+ last_user_text(&model).starts_with(PROMPT_UPDATE_NOTE),+ "the turn the prompt changed on says so"+ );+ assert_eq!(+ locator.session_chain(&session).len(),+ 2,+ "a changed prompt seals the generation it replaced"+ );++ // An unchanged prompt is a no-op: same prompt, no new generation.+ let mut third = host(&locator, &model, root.path(), "You are release two.", true);+ assert_eq!(third.turn("third message").await.unwrap(), "third reply");+ drop(third);+ assert_eq!(sent_prompt(&model), ["You are release two."]);+ assert!(sent_contains(&model, is_assistant, "second reply"));+ assert!(!last_user_text(&model).contains(PROMPT_UPDATE_NOTE));+ assert!(+ sent_contains(&model, is_user, PROMPT_UPDATE_NOTE),+ "the note stays where the change happened"+ );+ assert_eq!(locator.session_chain(&session).len(), 2);++ // Without the opt-in the thread keeps the prompt it recorded.+ let mut fourth = host(+ &locator,+ &model,+ root.path(),+ "You are release three.",+ false,+ );+ assert_eq!(fourth.turn("fourth message").await.unwrap(), "fourth reply");+ assert_eq!(sent_prompt(&model), ["You are release two."]);+ assert!(!last_user_text(&model).contains(PROMPT_UPDATE_NOTE));+ assert_eq!(locator.session_chain(&session).len(), 2);+}++/// The prelude a turn of `host` is prepared by.+fn prelude_of(mut host: crate::agent::OpenHumanSessionHost) -> OpenHumanTurnPrelude {+ host.ensure_runtime_session().expect("runtime session");+ let state = host+ .runtime_state+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ state.prelude.clone().expect("prelude")+}++/// The prelude of a session rendering `prompt` verbatim, with `refresh` as+/// its definition's `refresh_prompt_on_resume`.+fn prelude(root: &std::path::Path, prompt: &str, refresh: bool) -> OpenHumanTurnPrelude {+ prelude_of(host(+ &memory_locator(),+ &model(Vec::new()),+ root,+ prompt,+ refresh,+ ))+}++/// The prelude of a session whose prompt is composed by `prompt` (the+/// registry definition's own, not verbatim), opted into the refresh.+fn composed_prelude(root: &std::path::Path, prompt: SystemPromptBuilder) -> OpenHumanTurnPrelude {+ prelude_of(host_with(+ &memory_locator(),+ &model(Vec::new()),+ root,+ prompt,+ |definition| definition.extensions.refresh_prompt_on_resume = true,+ ))+}++/// Marks `prelude`'s connected integrations as read this session.+fn hydrate(prelude: &OpenHumanTurnPrelude) {+ let mut mutable = prelude+ .mutable+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ mutable.connected_integrations_initialized = true;+ mutable.connected_integrations_authoritative = true;+}++/// A resumed thread's view: `prefix` restored, `history` decoded.+fn resumed<'a>(prefix: &'a PrefixSnapshot, history: &'a [Message]) -> SessionStateView<'a> {+ SessionStateView {+ history,+ raw_history: &[],+ prefix,+ transcript_target: None,+ committed_turns: 1,+ resumed: true,+ }+}++/// A transcript snapshot whose request boundary is `base`, as the hook seeds+/// it from the history before the turn is prepared.+fn snapshot(base: usize) -> TranscriptSnapshotSink {+ let sink = TranscriptSnapshotSink::default();+ sink.lock().unwrap().request_base_len = base;+ sink+}++fn current() -> PrefixSnapshot {+ PrefixSnapshot::new(vec![Message::system("current")])+}++/// The user's message this turn, as the hook hands it over.+fn request() -> SessionTurnRequest {+ SessionTurnRequest::new(Message::user("hi"))+}++#[tokio::test]+async fn a_resumed_thread_is_offered_the_current_prompt_as_a_refresh() {+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let recorded = PrefixSnapshot::new(vec![Message::system("recorded")]);+ let history = [+ Message::system("recorded"),+ Message::user("hi"),+ Message::assistant("hello"),+ ];+ let mut request = request();+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&recorded, &history),+ &snapshot(4),+ &mut request,+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current().refreshing()));+ assert!(prepared.tools.is_some());+ assert_eq!(+ request.input.text(),+ format!("{PROMPT_UPDATE_NOTE}\n\nhi"),+ "the turn says the prompt changed"+ );+}++#[tokio::test]+async fn a_current_prompt_is_offered_without_a_note() {+ // The runtime finds the offer identical to the recorded prompt and keeps+ // the thread as it is; nothing changed, so nothing is said.+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let history = [+ Message::system("current"),+ Message::user("earlier"),+ Message::assistant("hello"),+ ];+ let mut request = request();+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(¤t(), &history),+ &snapshot(4),+ &mut request,+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current().refreshing()));+ assert_eq!(request.input.text(), "hi");+}++#[tokio::test]+async fn without_the_opt_in_a_resumed_thread_keeps_its_recorded_prompt() {+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", false);+ let recorded = PrefixSnapshot::new(vec![Message::system("recorded")]);+ let history = [Message::system("recorded"), Message::user("hi")];+ let sink = snapshot(3);+ let mut request = request();+ let prepared = prelude+ .prepare_turn(false, &resumed(&recorded, &history), &sink, &mut request)+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, None);+ assert_eq!(sink.lock().unwrap().request_base_len, 3, "nothing moved");+ assert_eq!(request.input.text(), "hi");+}++#[tokio::test]+async fn a_new_session_gets_its_prompt_frozen_whatever_the_opt_in() {+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let empty = PrefixSnapshot::default();+ let view = SessionStateView {+ resumed: false,+ committed_turns: 0,+ ..resumed(&empty, &[])+ };+ // The hook seeds the boundary with the user message alone; the request+ // opens with the prompt in front of it.+ let sink = snapshot(1);+ let mut request = request();+ let prepared = prelude+ .prepare_turn(true, &view, &sink, &mut request)+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current()));+ assert_eq!(sink.lock().unwrap().request_base_len, 2);+ assert_eq!(+ request.input.text(),+ "hi",+ "a new thread has no earlier prompt"+ );+}++#[tokio::test]+async fn the_snapshot_boundary_moves_with_a_prompt_of_another_length() {+ // Recorded under a two-message prompt, now rendered as one: the request+ // is one message shorter, and so is the start of this run's rounds.+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let recorded = PrefixSnapshot::new(vec![+ Message::system("tier one"),+ Message::system("tier two"),+ ]);+ let history = [+ Message::system("tier one"),+ Message::system("tier two"),+ Message::user("hi"),+ Message::assistant("hello"),+ ];+ let sink = snapshot(history.len() + 1);+ let prepared = prelude+ .prepare_turn(false, &resumed(&recorded, &history), &sink, &mut request())+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current().refreshing()));+ assert_eq!(sink.lock().unwrap().request_base_len, 4);+}++#[tokio::test]+async fn a_recorded_prompt_of_unknown_extent_is_kept() {+ // A compacted legacy head restores no prefix, and its old instructions+ // are still in the history: a new prompt beside them would leave them.+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let unknown = PrefixSnapshot::default();+ let history = [+ Message::system("old instructions"),+ Message::system("summary of earlier turns"),+ Message::user("hi"),+ ];+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&unknown, &history),+ &snapshot(4),+ &mut request(),+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, None);+}++#[tokio::test]+async fn a_thread_recorded_without_a_prompt_takes_up_the_current_one() {+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = prelude(root.path(), "current", true);+ let none = PrefixSnapshot::default();+ let history = [Message::user("hi"), Message::assistant("hello")];+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&none, &history),+ &snapshot(3),+ &mut request(),+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current().refreshing()));+}++#[tokio::test]+async fn a_composed_prompt_waits_for_the_integrations_to_be_read() {+ // A composed prompt renders the connected integrations; rendered before+ // they are read it would replace a complete prompt with a bare one.+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = composed_prelude(root.path(), SystemPromptBuilder::verbatim("current".into()));+ let recorded = PrefixSnapshot::new(vec![Message::system("recorded")]);+ let history = [Message::system("recorded"), Message::user("hi")];+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&recorded, &history),+ &snapshot(3),+ &mut request(),+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, None);++ hydrate(&prelude);+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&recorded, &history),+ &snapshot(3),+ &mut request(),+ )+ .await+ .expect("prepared turn");+ assert_eq!(prepared.prefix, Some(current().refreshing()));+}++fn failing_prompt(_: &PromptContext<'_>) -> anyhow::Result<String> {+ anyhow::bail!("the prompt source is unavailable")+}++#[tokio::test]+async fn a_prompt_that_fails_to_render_keeps_the_recorded_one() {+ let root = tempfile::tempdir().expect("tempdir");+ let prelude = composed_prelude(+ root.path(),+ SystemPromptBuilder::from_dynamic(failing_prompt),+ );+ hydrate(&prelude);+ let recorded = PrefixSnapshot::new(vec![Message::system("recorded")]);+ let history = [Message::system("recorded"), Message::user("hi")];+ let prepared = prelude+ .prepare_turn(+ false,+ &resumed(&recorded, &history),+ &snapshot(3),+ &mut request(),+ )+ .await+ .expect("a resumed turn does not fail on its prompt");+ assert_eq!(prepared.prefix, None);+}diff --cc crates/openhuman-core/src/agent_registry/host_definition.rsindex 76a7698ecf,830a1ce6db..91b51ec968--- a/crates/openhuman-core/src/agent_registry/host_definition.rs+++ b/crates/openhuman-core/src/agent_registry/host_definition.rs@@@ -91,9 -78,9 +95,10 @@@ impl Default for AgentExtomit_memory_context: true,omit_safety_preamble: true,tokenjuice_compression: AgentTokenjuiceCompression::default(),+ refresh_prompt_on_resume: false,searches_connected_mcp: false,prompt_builder: None,+ tinyflows_prompt_owner: None,}}}