Skip to content

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

feat(agent): add stage run context helper for delegation

Steven Enamakel8dc2fc308f05
diff --git a/crates/openhuman-core/src/agent/orchestration/delegation.rs b/crates/openhuman-core/src/agent/orchestration/delegation.rs
index 1f94b2855b..cf5f5e0965 100644
--- a/crates/openhuman-core/src/agent/orchestration/delegation.rs
+++ b/crates/openhuman-core/src/agent/orchestration/delegation.rs
@@ -23,6 +23,7 @@ use crate::agent::subagent_host::{
run_subagent_with_parent, SubagentRunOptions, SubagentRunStatus,
};
use crate::agent::tinyagents::host::delegation::run_or_resume_with_tracing;
+use crate::agent::tinyagents::host::OpenHumanRunContext;
use crate::config::Config;
use tinyagents_graph::checkpoint::Checkpointer;
use tinyagents_graph::delegation::{
@@ -34,6 +35,24 @@ use tinytools::WorkspaceDescriptor;
const LOG_TARGET: &str = "agent_orchestration::delegation";
+/// Derives one delegation stage's run context and host data from the stage
+/// parent. The host carrier is pointed at the stage run's own linked child
+/// token, so a graph cancel still reaches the stage through the parent while
+/// cancelling the stage leaves the graph (and its siblings) running.
+pub(crate) fn stage_run_contexts(
+ stage_parent: &RunContext<OpenHumanRunContext>,
+ workspace: Option<WorkspaceDescriptor>,
+) -> tinyagents_harness::Result<(RunContext<OpenHumanRunContext>, OpenHumanRunContext)> {
+ let mut stage_context = stage_parent.data.child();
+ stage_context.workspace = workspace.or(stage_context.workspace);
+ let stage_run = stage_parent.child(
+ RunConfig::new(format!("delegation-stage-{}", uuid::Uuid::new_v4())),
+ stage_context.clone(),
+ )?;
+ stage_context.cancellation = stage_run.cancellation.clone();
+ Ok((stage_run, stage_context))
+}
+
/// Typed live entrypoint for the durable delegation graph.
///
/// The graph does create a durable graph id for its checkpoints, but each
@@ -275,3 +294,7 @@ fn delegation_subagent_options(
run_queue: None,
}
}
+
+#[cfg(test)]
+#[path = "delegation_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/delegation_tests.rs b/crates/openhuman-core/src/agent/orchestration/delegation_tests.rs
new file mode 100644
index 0000000000..e5a99d62b7
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/delegation_tests.rs
@@ -0,0 +1,31 @@
+use super::*;
+
+fn root() -> RunContext<OpenHumanRunContext> {
+ OpenHumanRunContext::new().into_tinyagents(RunConfig::new("delegation-root"))
+}
+
+#[test]
+fn stage_host_data_carries_the_stage_runs_linked_token() {
+ let graph_parent = root();
+ let (stage_run, stage_context) = stage_run_contexts(&graph_parent, None).expect("stage");
+
+ assert_eq!(stage_run.depth(), 1);
+ // The graph cancelling reaches the stage's host carrier through the parent.
+ graph_parent.cancellation.cancel();
+ assert!(stage_run.cancellation.is_cancelled());
+ assert!(stage_context.cancellation.is_cancelled());
+}
+
+#[test]
+fn cancelling_a_stage_spares_the_graph_and_its_siblings() {
+ let graph_parent = root();
+ let (stage_a, ctx_a) = stage_run_contexts(&graph_parent, None).expect("stage a");
+ let (stage_b, ctx_b) = stage_run_contexts(&graph_parent, None).expect("stage b");
+
+ stage_a.cancellation.cancel();
+ assert!(ctx_a.cancellation.is_cancelled());
+ assert!(!graph_parent.cancellation.is_cancelled());
+ assert!(!graph_parent.data.cancellation.is_cancelled());
+ assert!(!stage_b.cancellation.is_cancelled());
+ assert!(!ctx_b.cancellation.is_cancelled());
+}