Skip to content

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

fix(subagent-host): reconcile driver job status with durable outcome

Steven Enamakel46d07e20c88a
diff --git a/crates/openhuman-core/src/agent/subagent_host/lifecycle.rs b/crates/openhuman-core/src/agent/subagent_host/lifecycle.rs
index 5dc51ef6ba..416236afc4 100644
--- a/crates/openhuman-core/src/agent/subagent_host/lifecycle.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/lifecycle.rs
@@ -537,20 +537,116 @@ pub(super) fn decode_driver_job_result(
bundle: &OpenHumanDriverBundle,
task_key: &SubagentTaskKey,
) -> Result<String, SubagentRunError> {
+ let (public_id, status) = decode_driver_job_reference(tool_result, bundle, task_key)?;
+ if let Some(status) = status {
+ let expected_status = bundle
+ .host_outcome
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .get(task_key)
+ .and_then(|outcome| neutral_status_for_host(&outcome.status));
+ if expected_status != Some(status.as_str()) {
+ return Err(SubagentRunError::Provider(anyhow::anyhow!(
+ "driver-backed async spawn failed: lifecycle status did not match its host outcome"
+ )));
+ }
+ }
+ Ok(public_id)
+}
+
+async fn resolve_inline_driver_outcome(
+ tool_result: tinytools::ToolResult,
+ bundle: &OpenHumanDriverBundle,
+ task_key: &SubagentTaskKey,
+ fallback_agent_id: &str,
+) -> Result<(SubagentRunOutcome, String), SubagentRunError> {
+ let (public_job_id, declared_status) =
+ decode_driver_job_reference(tool_result, bundle, task_key)?;
+ if let Some(error) = bundle_failure_for_key(bundle, task_key) {
+ return Err(error);
+ }
+ let cached = bundle
+ .host_outcome
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .remove(task_key);
+
+ // The driver's persistence CAS result is authoritative. Another
+ // invocation can win terminal/pause persistence while this executor's
+ // host-side cache still contains its local loser. Read the same scoped
+ // record the driver returned before projecting a host result.
+ let persisted = if let Some(outcome) = bundle
+ .persistence
+ .load_terminal(task_key)
+ .await
+ .map_err(map_lifecycle_error)?
+ {
+ Some((
+ outcome,
+ cached.as_ref().map_or(
+ SubagentPersistenceDisposition::TerminalExisting,
+ |host| host.persistence_disposition,
+ ),
+ ))
+ } else if let Some(outcome) = bundle
+ .persistence
+ .load_pause(task_key)
+ .await
+ .map_err(map_lifecycle_error)?
+ {
+ Some((
+ outcome,
+ cached.as_ref().map_or(
+ SubagentPersistenceDisposition::PauseExisting,
+ |host| host.persistence_disposition,
+ ),
+ ))
+ } else {
+ None
+ };
+
+ if let Some((outcome, disposition)) = persisted {
+ if declared_status.as_deref().is_some_and(|status| {
+ neutral_status_for_outcome(&outcome.status) != Some(status)
+ }) {
+ return Err(SubagentRunError::Provider(anyhow::anyhow!(
+ "driver-backed async spawn failed: lifecycle status did not match its durable outcome"
+ )));
+ }
+ let host = outcome_to_host(
+ SubagentRunResult::new(outcome, disposition),
+ cached,
+ &bundle.persistence,
+ task_key,
+ fallback_agent_id,
+ );
+ return Ok((host, public_job_id));
+ }
+
+ if let Some(outcome) = cached.filter(|host| {
+ host.persistence_disposition == SubagentPersistenceDisposition::ObserverCancelled
+ }) {
+ return Ok((outcome, public_job_id));
+ }
+ Err(SubagentRunError::Provider(anyhow::anyhow!(
+ "driver-backed subagent returned without a durable host outcome"
+ )))
+}
+
+fn decode_driver_job_reference(
+ tool_result: tinytools::ToolResult,
+ bundle: &OpenHumanDriverBundle,
+ task_key: &SubagentTaskKey,
+) -> Result<(String, Option<String>), SubagentRunError> {
if tool_result.is_error {
if let Some(error) = bundle_failure_for_key(bundle, task_key) {
return Err(error);
}
}
- let reply = match tool_result.content.first() {
- Some(tinytools::ToolContent::Json { data }) if !tool_result.is_error => data.clone(),
- // The generic dispatcher encodes non-success lifecycle snapshots
- // (awaiting input, incomplete, cancelled) as a JSON text error so the
- // calling model sees the stop. For this host adapter, accept only that
- // exact typed snapshot when the driver also produced the corresponding
- // host outcome. Arbitrary text errors and persistence failures remain
- // errors; in particular, a host executor outcome alone is not enough
- // to override a failed driver call.
+ let (reply, status) = match tool_result.content.first() {
+ Some(tinytools::ToolContent::Json { data }) if !tool_result.is_error => {
+ (data.clone(), None)
+ }
Some(tinytools::ToolContent::Text { text }) if tool_result.is_error => {
let reply: serde_json::Value = serde_json::from_str(text).map_err(|_| {
SubagentRunError::Provider(anyhow::anyhow!(format!(
@@ -558,26 +654,18 @@ pub(super) fn decode_driver_job_result(
tool_result.output()
)))
})?;
- let expected_status = bundle
- .host_outcome
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner())
- .get(task_key)
- .and_then(|outcome| match &outcome.status {
- SubagentRunStatus::AwaitingUser { .. } => Some("awaiting_input"),
- SubagentRunStatus::Incomplete { .. } => Some("incomplete"),
- SubagentRunStatus::Cancelled => Some("cancelled"),
- SubagentRunStatus::Completed => None,
- });
- if !expected_status.is_some_and(|status| {
- reply.get("status").and_then(serde_json::Value::as_str) == Some(status)
- }) {
- return Err(SubagentRunError::Provider(anyhow::anyhow!(format!(
- "driver-backed async spawn failed: {}",
- tool_result.output()
- ))));
- }
- reply
+ let status = reply
+ .get("status")
+ .and_then(serde_json::Value::as_str)
+ .filter(|status| matches!(*status, "awaiting_input" | "incomplete" | "cancelled"))
+ .map(str::to_owned)
+ .ok_or_else(|| {
+ SubagentRunError::Provider(anyhow::anyhow!(format!(
+ "driver-backed async spawn failed: {}",
+ tool_result.output()
+ )))
+ })?;
+ (reply, Some(status))
}
_ => {
return Err(SubagentRunError::Provider(anyhow::anyhow!(format!(
@@ -586,7 +674,7 @@ pub(super) fn decode_driver_job_result(
))));
}
};
- reply
+ let public_id = reply
.get("task_id")
.and_then(serde_json::Value::as_str)
.filter(|task_id| !task_id.is_empty())
@@ -595,7 +683,26 @@ pub(super) fn decode_driver_job_result(
SubagentRunError::Provider(anyhow::anyhow!(
"driver-backed async spawn omitted its registered job id"
))
- })
+ })?;
+ Ok((public_id, status))
+}
+
+fn neutral_status_for_host(status: &SubagentRunStatus) -> Option<&'static str> {
+ match status {
+ SubagentRunStatus::AwaitingUser { .. } => Some("awaiting_input"),
+ SubagentRunStatus::Incomplete { .. } => Some("incomplete"),
+ SubagentRunStatus::Cancelled => Some("cancelled"),
+ SubagentRunStatus::Completed => None,
+ }
+}
+
+fn neutral_status_for_outcome(status: &SubagentOutcomeKind) -> Option<&'static str> {
+ match status {
+ SubagentOutcomeKind::AwaitingInput(_) => Some("awaiting_input"),
+ SubagentOutcomeKind::Incomplete(_) => Some("incomplete"),
+ SubagentOutcomeKind::Cancelled => Some("cancelled"),
+ SubagentOutcomeKind::Completed => None,
+ }
}
fn bundle_failure_for_key(