Skip to content

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

refactor(prompts): tidy prompt composition and test workspace handling

Steven Enamakel36baf089b5a5
diff --git a/crates/openhuman-core/src/agent/prompts/agents_md.rs b/crates/openhuman-core/src/agent/prompts/agents_md.rs
index 8110ef9704..0099f323e9 100644
--- a/crates/openhuman-core/src/agent/prompts/agents_md.rs
+++ b/crates/openhuman-core/src/agent/prompts/agents_md.rs
@@ -3,7 +3,7 @@
use std::path::Path;
pub use tinyagents_harness::context::{
- AGENTS_MD_FILENAME, AgentsMdContent, MAX_AGENTS_MD_READ_BYTES,
+ AgentsMdContent, AGENTS_MD_FILENAME, MAX_AGENTS_MD_READ_BYTES,
};
/// Load one instruction file after applying OpenHuman's path-root policy.
diff --git a/crates/openhuman-core/src/agent/prompts/mod_tests_builder_sections_tests.rs b/crates/openhuman-core/src/agent/prompts/mod_tests_builder_sections_tests.rs
index 0a7e0ed9aa..09d9ec622d 100644
--- a/crates/openhuman-core/src/agent/prompts/mod_tests_builder_sections_tests.rs
+++ b/crates/openhuman-core/src/agent/prompts/mod_tests_builder_sections_tests.rs
@@ -120,7 +120,7 @@ fn identity_section_creates_missing_workspace_files() {
let tools: Vec<Box<dyn Tool>> = vec![];
let prompt_tools = PromptTool::from_tools(&tools);
let ctx = PromptContext {
- workspace_dir: &workspace,
+ workspace_dir: workspace.path(),
model_name: "test-model",
agent_id: "",
tools: &prompt_tools,
@@ -165,8 +165,6 @@ fn identity_section_creates_missing_workspace_files() {
include_str!("SOUL.md"),
"seeded SOUL.md must be the checked-in template verbatim"
);
-
- let _ = std::fs::remove_dir_all(workspace);
}
#[test]
@@ -702,15 +700,11 @@ fn composed_prompt_keeps_context_and_volatile_system_message_boundaries() {
))
}
- let workspace = std::env::temp_dir().join(format!(
- "openhuman_prompt_composition_{}",
- uuid::Uuid::new_v4()
- ));
- std::fs::create_dir_all(&workspace).unwrap();
+ let workspace = tempfile::tempdir().unwrap();
let custom_style = "# User style\nKeep this exact custom style.";
- std::fs::write(workspace.join("STYLE.md"), custom_style).unwrap();
+ std::fs::write(workspace.path().join("STYLE.md"), custom_style).unwrap();
let ctx = PromptContext {
- workspace_dir: &workspace,
+ workspace_dir: workspace.path(),
model_name: "test-model",
agent_id: "",
tools: &[],
@@ -732,9 +726,20 @@ fn composed_prompt_keeps_context_and_volatile_system_message_boundaries() {
.unwrap();
assert_eq!(RENDERS.load(std::sync::atomic::Ordering::SeqCst), 1);
assert!(!rendered.breakpoints.is_empty());
- assert!(rendered.breakpoints.iter().all(|offset| rendered.text.is_char_boundary(*offset)));
+ assert!(rendered
+ .breakpoints
+ .iter()
+ .all(|offset| rendered.text.is_char_boundary(*offset)));
+ let stable_boundary = rendered.breakpoints[0];
+ assert!(rendered.text[..stable_boundary].contains("Stable πŸ“"));
+ assert!(rendered.text[..stable_boundary].contains(custom_style));
+ assert!(!rendered.text[..stable_boundary].contains("Context instructions 🧭."));
let messages = rendered.system_messages();
- assert_eq!(messages.len(), 2, "Stable+Context remain first, Volatile second");
+ assert_eq!(
+ messages.len(),
+ 2,
+ "Stable+Context remain first, Volatile second"
+ );
assert!(messages[0].contains("Product grounding."));
assert!(messages[0].contains("Context instructions 🧭."));
assert!(messages[0].contains("Stable πŸ“"));
@@ -743,7 +748,11 @@ fn composed_prompt_keeps_context_and_volatile_system_message_boundaries() {
assert_eq!(messages[0].matches("## Grounding and tool use").count(), 1);
assert!(messages[1].contains("Volatile identity."));
assert!(!messages[1].contains(custom_style));
- let _ = std::fs::remove_dir_all(workspace);
+ assert_eq!(
+ std::fs::read_to_string(workspace.path().join("STYLE.md")).unwrap(),
+ custom_style,
+ "composition must preserve the user-edited STYLE.md bytes"
+ );
}
#[test]
@@ -751,13 +760,9 @@ fn unmarked_dynamic_body_stays_volatile_after_stable_and_context_messages() {
fn dynamic_prompt(_ctx: &PromptContext<'_>) -> anyhow::Result<String> {
Ok("Unmarked dynamic UTF-8 πŸ“Ž body.".to_string())
}
- let workspace = std::env::temp_dir().join(format!(
- "openhuman_unmarked_prompt_{}",
- uuid::Uuid::new_v4()
- ));
- std::fs::create_dir_all(&workspace).unwrap();
+ let workspace = tempfile::tempdir().unwrap();
let ctx = PromptContext {
- workspace_dir: &workspace,
+ workspace_dir: workspace.path(),
model_name: "test-model",
agent_id: "",
tools: &[],
@@ -776,13 +781,16 @@ fn unmarked_dynamic_body_stays_volatile_after_stable_and_context_messages() {
.build_tiered(&ctx)
.unwrap();
let messages = rendered.system_messages();
- assert_eq!(messages.len(), 2, "the stable prefix and unmarked volatile body stay separate");
+ assert_eq!(
+ messages.len(),
+ 2,
+ "the stable prefix and unmarked volatile body stay separate"
+ );
assert!(rendered.parts.iter().any(|(tier, part)| {
*tier == PromptTier::Volatile && part.contains("Unmarked dynamic UTF-8 πŸ“Ž body.")
}));
assert!(messages[0].contains("## Grounding and tool use"));
assert!(messages[1].contains("Unmarked dynamic UTF-8 πŸ“Ž body."));
- let _ = std::fs::remove_dir_all(workspace);
}
#[test]
diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs b/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs
index 433c4a1254..7353e97b2c 100644
--- a/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs
+++ b/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs
@@ -4,9 +4,9 @@
//! [`SystemPromptBuilder::for_subagent`](crate::prompts::SystemPromptBuilder::for_subagent)
//! for callers that only hold indices into the parent's tool vec.
-use crate::prompts::GLOBAL_STYLE_SUFFIX;
use super::types::*;
use crate::prompts::sections::GROUNDING_BODY;
+use crate::prompts::GLOBAL_STYLE_SUFFIX;
use crate::prompts::{inject_workspace_file, write_agents_md_blocks};
use std::fmt::Write;
use std::path::Path;
@@ -161,7 +161,10 @@ pub fn render_subagent_system_prompt_with_format(
parameters: tool.parameters_schema(),
}));
if !tool_specs.is_empty() {
- out.push_str(&dialect::render_prompt_instructions(tool_call_format, &tool_specs));
+ out.push_str(&dialect::render_prompt_instructions(
+ tool_call_format,
+ &tool_specs,
+ ));
}
out.push_str("\nUse the provided tools to accomplish the task. Reply with a concise, dense final answer when you have one β€” the parent agent will weave it back into the user-visible response.\n\n");
diff --git a/crates/openhuman-core/src/agent/prompts/types.rs b/crates/openhuman-core/src/agent/prompts/types.rs
index fc6ac61dc9..3d54dccd20 100644
--- a/crates/openhuman-core/src/agent/prompts/types.rs
+++ b/crates/openhuman-core/src/agent/prompts/types.rs
@@ -7,8 +7,8 @@ use tinyskills::Skill as Workflow;
use tinytools::Tool;
pub use tinyagents_harness::prompt::{
- DynamicPromptSection, PromptTier, TieredPromptPart, PROMPT_TIER_CONTEXT_MARKER,
- PROMPT_TIER_VOLATILE_MARKER, split_prompt_tiers,
+ split_prompt_tiers, DynamicPromptSection, PromptTier, TieredPromptPart,
+ PROMPT_TIER_CONTEXT_MARKER, PROMPT_TIER_VOLATILE_MARKER,
};
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/crates/openhuman-core/src/prompts/composition.rs b/crates/openhuman-core/src/prompts/composition.rs
index 9e2cf2df82..1a986132ed 100644
--- a/crates/openhuman-core/src/prompts/composition.rs
+++ b/crates/openhuman-core/src/prompts/composition.rs
@@ -1,10 +1,8 @@
//! OpenHuman's product-specific system-prompt selection and additions.
-use crate::agent::prompts::types::{
- PromptContext, PromptTier, TieredPromptPart,
-};
+use crate::agent::prompts::types::{PromptContext, PromptTier, TieredPromptPart};
use crate::prompts::sections::*;
-use anyhow::{Result, anyhow};
+use anyhow::{anyhow, Result};
use std::path::Path;
use tinyagents_harness::prompt::{
DynamicPromptSection as OwnerDynamicPromptSection, PromptComposer,
@@ -51,7 +49,10 @@ impl<'a> OwnerDynamicPromptSection<PromptContext<'a>> for ProductSection {
}
}
- fn render(&self, context: &PromptContext<'a>) -> tinyagents_harness::Result<Vec<TieredPromptPart>> {
+ fn render(
+ &self,
+ context: &PromptContext<'a>,
+ ) -> tinyagents_harness::Result<Vec<TieredPromptPart>> {
match self {
Self::Archetype(body) => ArchetypePromptSection::new(body.clone()).render(context),
Self::Dynamic(builder) => {
@@ -78,14 +79,15 @@ impl<'a> OwnerDynamicPromptSection<PromptContext<'a>> for OpenHumanPromptContent
"openhuman_prompt_content"
}
- fn render(&self, context: &PromptContext<'a>) -> tinyagents_harness::Result<Vec<TieredPromptPart>> {
+ fn render(
+ &self,
+ context: &PromptContext<'a>,
+ ) -> tinyagents_harness::Result<Vec<TieredPromptPart>> {
let mut parts = Vec::new();
let mut has_grounding = false;
for section in &self.sections {
for part in section.render(context)? {
- if !part.content.trim().is_empty()
- && part.content.contains(GROUNDING_HEADING)
- {
+ if !part.content.trim().is_empty() && part.content.contains(GROUNDING_HEADING) {
has_grounding = true;
}
parts.push(part);
@@ -236,4 +238,3 @@ impl SystemPromptBuilder {
})
}
}
-
diff --git a/crates/openhuman-core/src/prompts/mod.rs b/crates/openhuman-core/src/prompts/mod.rs
index ecf18de178..750e3b0316 100644
--- a/crates/openhuman-core/src/prompts/mod.rs
+++ b/crates/openhuman-core/src/prompts/mod.rs
@@ -1,7 +1,7 @@
//! Prompt product copy and rendering helpers used by agent and channel prompts.
-pub mod sections;
pub mod composition;
+pub mod sections;
pub use composition::{SystemPromptBuilder, TieredPrompt, GLOBAL_STYLE_SUFFIX};
#[path = "section_renderers.rs"]
diff --git a/crates/openhuman-core/src/prompts/sections.rs b/crates/openhuman-core/src/prompts/sections.rs
index c9d7f918ca..7463f83d6b 100644
--- a/crates/openhuman-core/src/prompts/sections.rs
+++ b/crates/openhuman-core/src/prompts/sections.rs
@@ -500,9 +500,9 @@ macro_rules! implement_owner_prompt_section {
&self,
context: &PromptContext<'a>,
) -> tinyagents_harness::Result<Vec<TieredPromptPart>> {
- let content = self
- .build(context)
- .map_err(|error| tinyagents_harness::TinyAgentsError::Model(error.to_string()))?;
+ let content = self.build(context).map_err(|error| {
+ tinyagents_harness::TinyAgentsError::Model(error.to_string())
+ })?;
Ok(vec![TieredPromptPart::new($tier, content)])
}
}