Skip to content

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

fix(subagent_sessions): guard terminal writes against stale task ids

Steven Enamakelbfa011e4f977
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
index ff40a547aa..ad304db7bf 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/ops.rs
@@ -194,8 +194,7 @@ pub fn mark_finished(
run_status: &SubagentRunStatus,
history: Vec<TranscriptMessage>,
) -> Result<(), String> {
- let updated = update_session(store, subagent_session_id, |session, now| {
- session.current_task_id = Some(task_id.to_string());
+ let updated = update_session_for_task(store, subagent_session_id, task_id, |session, now| {
session.status = DurableSubagentStatus::from_run_status(run_status);
session.latest_history = Some(history);
session.latest_error = None;
@@ -217,8 +216,7 @@ pub fn mark_failed(
task_id: &str,
error: String,
) -> Result<(), String> {
- let updated = update_session(store, subagent_session_id, |session, now| {
- session.current_task_id = Some(task_id.to_string());
+ let updated = update_session_for_task(store, subagent_session_id, task_id, |session, now| {
session.status = DurableSubagentStatus::Failed;
session.latest_error = Some(error);
session.updated_at = now.clone();
@@ -296,6 +294,39 @@ where
Ok(false)
}
+/// Apply a terminal write only while it still belongs to the current task.
+/// The check and write share the store lock so a late completion cannot
+/// overwrite a reused session or revive one that has already been closed.
+fn update_session_for_task<F>(
+ store: &SubagentSessionStore,
+ subagent_session_id: &str,
+ task_id: &str,
+ update: F,
+) -> Result<bool, String>
+where
+ F: FnOnce(&mut DurableSubagentSession, String),
+{
+ let _guard = store_write_lock()
+ .lock()
+ .map_err(|_| "subagent session store lock poisoned".to_string())?;
+ let mut sessions = store.load()?;
+ let now = chrono::Utc::now().to_rfc3339();
+ if let Some(session) = sessions
+ .iter_mut()
+ .find(|session| session.subagent_session_id == subagent_session_id)
+ {
+ if session.status == DurableSubagentStatus::Closed
+ || session.current_task_id.as_deref() != Some(task_id)
+ {
+ return Ok(false);
+ }
+ update(session, now);
+ store.save(&sessions)?;
+ return Ok(true);
+ }
+ Ok(false)
+}
+
fn store_write_lock() -> &'static Mutex<()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle_outcome_tests.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle_outcome_tests.rs
index 30c096a4b9..f778c42c80 100644
--- a/crates/openhuman-core/src/agent/subagent_host/lifecycle_outcome_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle_outcome_tests.rs
@@ -50,3 +50,39 @@ fn paused_outcomes_preserve_answer_choices_and_worker_resume_options() {
assert_eq!(pause.resume.metadata["skill_filter_override"], "docs");
assert_eq!(pause.resume.metadata["model_override"], "worker-model");
}
+
+#[test]
+fn host_outcome_terminal_record_round_trips_through_json_bytes() {
+ let definitions =
+ crate::agent_registry::host_definition::AgentDefinitionRegistry::builtins_only();
+ let definition = definitions.get("orchestrator").unwrap();
+ let options = SubagentRunOptions {
+ worker_thread_id: Some("worker-thread".into()),
+ model_override: Some("worker-model".into()),
+ ..SubagentRunOptions::default()
+ };
+ let outcome = SubagentRunOutcome {
+ task_id: "task-123".into(),
+ agent_id: definition.id.clone(),
+ output: "durable".into(),
+ iterations: 2,
+ elapsed: std::time::Duration::from_millis(25),
+ mode: crate::agent::subagent_host::SubagentMode::Typed,
+ status: SubagentRunStatus::Completed,
+ final_history: Vec::new(),
+ usage: SubagentUsage::default(),
+ artifact_paths: vec![],
+ persistence_disposition:
+ tinyagents_orchestration::subagent::SubagentPersistenceDisposition::TerminalInserted,
+ };
+
+ let neutral = host_outcome_to_neutral(outcome, definition, &options);
+ let encoded = serde_json::to_vec(&neutral).unwrap();
+ let decoded: tinyagents_orchestration::subagent::SubagentOutcome =
+ serde_json::from_slice(&encoded)
+ .expect("terminal outcomes written as bytes must deserialize");
+ assert_eq!(decoded.task_id, "task-123");
+ assert_eq!(decoded.worker_thread_id.as_deref(), Some("worker-thread"));
+ assert_eq!(decoded.model_override.as_deref(), Some("worker-model"));
+ assert_eq!(decoded.tier.as_deref(), Some("orchestrator"));
+}