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
refactor(subagent): reindent spawn_async_subagent completion handler
Steven Enamakel87d774ba9ec0
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rsindex 57fcfb8887..4487b87b60 100644--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs@@ -187,7 +187,8 @@ impl SpawnAsyncSubagentTool {&session.subagent_session_id,follow_up_prompt.clone(),)- .await {+ .await+ {Ok(()) => {log::info!("[subagent_reuse] parent_thread_id={} subagent_session_id={} task_id={} agent_id={} reuse_decision={}",@@ -383,10 +384,17 @@ impl SpawnAsyncSubagentTool {let completion_core = dispatch.clone().into_core();let completion_origin = detached_parent.data.origin.clone();let completion_worker_thread_id = background_worker_thread_id.clone();+ let registration_progress = progress_sink.clone();+ let registration_completion_target = completion_target.clone();+ let registration_parent_session = parent_session.clone();+ let registration_agent_id = definition.id.clone();+ let registration_task_id = task_id.clone();let completion = move |_target: String,result: Result<SubagentRunOutcome, String>,emit_lifecycle_effects: bool|- -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>> {+ -> std::pin::Pin<+ Box<dyn std::future::Future<Output = ()> + Send>,+ > {let core = completion_core.clone();let origin = completion_origin.clone();let progress_sink = progress_sink.clone();@@ -402,53 +410,50 @@ impl SpawnAsyncSubagentTool {let background_parent_thread_id = background_parent_thread_id.clone();Box::pin(async move {let finish = async move {- let mut abort_report = AbortReport::arm(- progress_sink,- &background_agent_id,- &background_task_id,- );+ let mut abort_report =+ AbortReport::arm(progress_sink, &background_agent_id, &background_task_id);let result = result.map_err(anyhow::Error::msg);match result {- Ok(outcome) => {- match outcome.status {- SubagentRunStatus::Completed => {- if let Err(err) = subagent_sessions::mark_finished(- &background_store,- &background_subagent_session_id,- &outcome.task_id,- &outcome.status,- outcome.final_history.clone(),- ) {- log::warn!(+ Ok(outcome) => {+ match outcome.status {+ SubagentRunStatus::Completed => {+ if let Err(err) = subagent_sessions::mark_finished(+ &background_store,+ &background_subagent_session_id,+ &outcome.task_id,+ &outcome.status,+ outcome.final_history.clone(),+ ) {+ log::warn!("[subagent_reuse] mark_completed failed subagent_session_id={} task_id={} agent_id={} error={}",background_subagent_session_id,outcome.task_id,outcome.agent_id,err);- }- // A workflow proposal from the child's tool history is- // durable state, not prose: persist it into the parent chat- // thread (survives reload / reconnect) and carry the full- // payload in the delivery notice for the follow-up turn.- if emit_lifecycle_effects {- let delivery_summary = attach_workflow_proposal(- &background_dispatch,- &background_workspace_dir,- background_parent_thread_id.as_deref(),- &outcome.task_id,- &outcome.agent_id,- &outcome.final_history,- outcome.output.clone(),- );- completion_target- .completed(+ }+ // A workflow proposal from the child's tool history is+ // durable state, not prose: persist it into the parent chat+ // thread (survives reload / reconnect) and carry the full+ // payload in the delivery notice for the follow-up turn.+ if emit_lifecycle_effects {+ let delivery_summary = attach_workflow_proposal(+ &background_dispatch,+ &background_workspace_dir,+ background_parent_thread_id.as_deref(),&outcome.task_id,&outcome.agent_id,- delivery_summary,- )- .await;- crate::agent::orchestration::subagent_events::publish_subagent_completed(+ &outcome.final_history,+ outcome.output.clone(),+ );+ completion_target+ .completed(+ &outcome.task_id,+ &outcome.agent_id,+ delivery_summary,+ )+ .await;+ crate::agent::orchestration::subagent_events::publish_subagent_completed(background_parent_session,outcome.task_id.clone(),outcome.agent_id.clone(),@@ -456,68 +461,68 @@ impl SpawnAsyncSubagentTool {outcome.output.chars().count(),outcome.iterations,);- abort_report- .deliver(AgentProgress::SubagentCompleted {- agent_id: outcome.agent_id,- task_id: outcome.task_id,- elapsed_ms: outcome.elapsed.as_millis() as u64,- iterations: outcome.iterations as u32,- output_chars: outcome.output.chars().count(),- output: outcome.output.clone(),- // Detached by construction: this tool- // takes `detached_child()`, so this child's- // spend never reached the parent turn's- // ledger or `chat_done`. See the field docs.- usage: Some(outcome.usage),- worktree_path: None,- changed_files: Vec::new(),- dirty_status: None,- stop: None,- })- .await;+ abort_report+ .deliver(AgentProgress::SubagentCompleted {+ agent_id: outcome.agent_id,+ task_id: outcome.task_id,+ elapsed_ms: outcome.elapsed.as_millis() as u64,+ iterations: outcome.iterations as u32,+ output_chars: outcome.output.chars().count(),+ output: outcome.output.clone(),+ // Detached by construction: this tool+ // takes `detached_child()`, so this child's+ // spend never reached the parent turn's+ // ledger or `chat_done`. See the field docs.+ usage: Some(outcome.usage),+ worktree_path: None,+ changed_files: Vec::new(),+ dirty_status: None,+ stop: None,+ })+ .await;+ }}- }- SubagentRunStatus::Incomplete { ref reason } => {- // Async sub-agent stopped short (stuck halt / iteration- // cap). Mark the session finished and deliver the PARTIAL- // progress back to the parent, framed so it is not- // mistaken for a completed result (#4096).- if let Err(err) = subagent_sessions::mark_finished(- &background_store,- &background_subagent_session_id,- &outcome.task_id,- &outcome.status,- outcome.final_history.clone(),- ) {- log::warn!(+ SubagentRunStatus::Incomplete { ref reason } => {+ // Async sub-agent stopped short (stuck halt / iteration+ // cap). Mark the session finished and deliver the PARTIAL+ // progress back to the parent, framed so it is not+ // mistaken for a completed result (#4096).+ if let Err(err) = subagent_sessions::mark_finished(+ &background_store,+ &background_subagent_session_id,+ &outcome.task_id,+ &outcome.status,+ outcome.final_history.clone(),+ ) {+ log::warn!("[subagent_reuse] mark_incomplete failed subagent_session_id={} task_id={} agent_id={} error={}",background_subagent_session_id,outcome.task_id,outcome.agent_id,err);- }- let framed = format!(+ }+ let framed = format!("[SUBAGENT_INCOMPLETE] the sub-agent {reason} and did not finish. \Partial progress:\n{}",outcome.output);- // An incomplete run may still have produced a full- // proposal before stalling — preserve it durably too.- if emit_lifecycle_effects {- let framed = attach_workflow_proposal(- &background_dispatch,- &background_workspace_dir,- background_parent_thread_id.as_deref(),- &outcome.task_id,- &outcome.agent_id,- &outcome.final_history,- framed,- );- completion_target- .completed(&outcome.task_id, &outcome.agent_id, framed)- .await;- crate::agent::orchestration::subagent_events::publish_subagent_completed(+ // An incomplete run may still have produced a full+ // proposal before stalling — preserve it durably too.+ if emit_lifecycle_effects {+ let framed = attach_workflow_proposal(+ &background_dispatch,+ &background_workspace_dir,+ background_parent_thread_id.as_deref(),+ &outcome.task_id,+ &outcome.agent_id,+ &outcome.final_history,+ framed,+ );+ completion_target+ .completed(&outcome.task_id, &outcome.agent_id, framed)+ .await;+ crate::agent::orchestration::subagent_events::publish_subagent_completed(background_parent_session,outcome.task_id.clone(),outcome.agent_id.clone(),@@ -525,178 +530,173 @@ impl SpawnAsyncSubagentTool {outcome.output.chars().count(),outcome.iterations,);- abort_report- .deliver(AgentProgress::SubagentCompleted {- agent_id: outcome.agent_id,- task_id: outcome.task_id,- elapsed_ms: outcome.elapsed.as_millis() as u64,- iterations: outcome.iterations as u32,- output_chars: outcome.output.chars().count(),- output: outcome.output.clone(),- // Detached by construction: this tool- // takes `detached_child()`, so this child's- // spend never reached the parent turn's- // ledger or `chat_done`. See the field docs.- usage: Some(outcome.usage),- worktree_path: None,- changed_files: Vec::new(),- dirty_status: None,- stop: incomplete_stop(reason),- })- .await;+ abort_report+ .deliver(AgentProgress::SubagentCompleted {+ agent_id: outcome.agent_id,+ task_id: outcome.task_id,+ elapsed_ms: outcome.elapsed.as_millis() as u64,+ iterations: outcome.iterations as u32,+ output_chars: outcome.output.chars().count(),+ output: outcome.output.clone(),+ // Detached by construction: this tool+ // takes `detached_child()`, so this child's+ // spend never reached the parent turn's+ // ledger or `chat_done`. See the field docs.+ usage: Some(outcome.usage),+ worktree_path: None,+ changed_files: Vec::new(),+ dirty_status: None,+ stop: incomplete_stop(reason),+ })+ .await;+ }}- }- SubagentRunStatus::Cancelled => {- let error = "async sub-agent was cancelled".to_string();- if let Err(err) = subagent_sessions::mark_finished(- &background_store,- &background_subagent_session_id,- &outcome.task_id,- &outcome.status,- outcome.final_history.clone(),- ) {- log::warn!(+ SubagentRunStatus::Cancelled => {+ let error = "async sub-agent was cancelled".to_string();+ if let Err(err) = subagent_sessions::mark_finished(+ &background_store,+ &background_subagent_session_id,+ &outcome.task_id,+ &outcome.status,+ outcome.final_history.clone(),+ ) {+ log::warn!("[subagent_reuse] mark_cancelled failed subagent_session_id={} task_id={} agent_id={} error={}",background_subagent_session_id,outcome.task_id,outcome.agent_id,err);- }- if emit_lifecycle_effects {- completion_target- .failed(&outcome.task_id, &outcome.agent_id, &error)- .await;- crate::agent::orchestration::subagent_events::publish_subagent_failed(+ }+ if emit_lifecycle_effects {+ completion_target+ .failed(&outcome.task_id, &outcome.agent_id, &error)+ .await;+ crate::agent::orchestration::subagent_events::publish_subagent_failed(background_parent_session,outcome.task_id.clone(),outcome.agent_id.clone(),error.clone(),);- abort_report- .deliver(AgentProgress::SubagentFailed {- agent_id: outcome.agent_id,- task_id: outcome.task_id,- error,- })- .await;+ abort_report+ .deliver(AgentProgress::SubagentFailed {+ agent_id: outcome.agent_id,+ task_id: outcome.task_id,+ error,+ })+ .await;+ }}- }- SubagentRunStatus::AwaitingUser {- ref question,- ref checkpoint,- ..- } => {- if let Err(err) = subagent_sessions::mark_finished(- &background_store,- &background_subagent_session_id,- &outcome.task_id,- &outcome.status,- outcome.final_history.clone(),- ) {- log::warn!(+ SubagentRunStatus::AwaitingUser {+ ref question,+ ref checkpoint,+ ..+ } => {+ if let Err(err) = subagent_sessions::mark_finished(+ &background_store,+ &background_subagent_session_id,+ &outcome.task_id,+ &outcome.status,+ outcome.final_history.clone(),+ ) {+ log::warn!("[subagent_reuse] mark_awaiting_user failed subagent_session_id={} task_id={} agent_id={} error={}",background_subagent_session_id,outcome.task_id,outcome.agent_id,err);- }- // #4896: a detached child that pauses for input won't continue- // on its own — queue a framed notice so the parent chat learns it- // needs input, not "Accepted". Same idle-gated path as a success.- if emit_lifecycle_effects {- completion_target- .awaiting_input(- &outcome.task_id,- &outcome.agent_id,- question,- checkpoint.is_some(),- )- .await;- crate::agent::orchestration::subagent_events::publish_subagent_awaiting_user(+ }+ // #4896: a detached child that pauses for input won't continue+ // on its own — queue a framed notice so the parent chat learns it+ // needs input, not "Accepted". Same idle-gated path as a success.+ if emit_lifecycle_effects {+ completion_target+ .awaiting_input(+ &outcome.task_id,+ &outcome.agent_id,+ question,+ checkpoint.is_some(),+ )+ .await;+ crate::agent::orchestration::subagent_events::publish_subagent_awaiting_user(background_parent_session,outcome.task_id.clone(),outcome.agent_id.clone(),question.clone(),);- abort_report- .deliver(AgentProgress::SubagentAwaitingUser {- agent_id: outcome.agent_id,- task_id: outcome.task_id,- question: question.clone(),- worker_thread_id: background_worker_thread_id.clone(),- checkpoint_path: checkpoint- .as_ref()- .map(|path| path.to_string_lossy().to_string()),- })- .await;+ abort_report+ .deliver(AgentProgress::SubagentAwaitingUser {+ agent_id: outcome.agent_id,+ task_id: outcome.task_id,+ question: question.clone(),+ worker_thread_id: background_worker_thread_id+ .clone(),+ checkpoint_path: checkpoint+ .as_ref()+ .map(|path| path.to_string_lossy().to_string()),+ })+ .await;+ }}}}- }- Err(err) => {- let error = err.to_string();- if let Err(store_err) = subagent_sessions::mark_failed(- &background_store,- &background_subagent_session_id,- &background_task_id,- error.clone(),- ) {- log::warn!(+ Err(err) => {+ let error = err.to_string();+ if let Err(store_err) = subagent_sessions::mark_failed(+ &background_store,+ &background_subagent_session_id,+ &background_task_id,+ error.clone(),+ ) {+ log::warn!("[subagent_reuse] mark_failed failed subagent_session_id={} task_id={} agent_id={} error={}",background_subagent_session_id,background_task_id,background_agent_id,store_err);- }- // #4896: a detached child that errors previously only- // published an event — nothing reached chat, so the parent- // turn finalized on "Accepted" and the failure was lost.- // Queue a framed failure notice so background_delivery- // surfaces it as a follow-up chat turn.- if emit_lifecycle_effects {- completion_target- .failed(&background_task_id, &background_agent_id, &error)- .await;- crate::agent::orchestration::subagent_events::publish_subagent_failed(+ }+ // #4896: a detached child that errors previously only+ // published an event — nothing reached chat, so the parent+ // turn finalized on "Accepted" and the failure was lost.+ // Queue a framed failure notice so background_delivery+ // surfaces it as a follow-up chat turn.+ if emit_lifecycle_effects {+ completion_target+ .failed(&background_task_id, &background_agent_id, &error)+ .await;+ crate::agent::orchestration::subagent_events::publish_subagent_failed(background_parent_session,background_task_id.clone(),background_agent_id.clone(),error.clone(),);- abort_report- .deliver(AgentProgress::SubagentFailed {- agent_id: background_agent_id,- task_id: background_task_id,- error,- })- .await;+ abort_report+ .deliver(AgentProgress::SubagentFailed {+ agent_id: background_agent_id,+ task_id: background_task_id,+ error,+ })+ .await;+ }}}- }abort_report.disarm();};match core {- Some(core) => crate::core::runtime::CoreContext::scope_with_turn_origin(- core,- origin,- Box::pin(finish),- )- .await,+ Some(core) => {+ crate::core::runtime::CoreContext::scope_with_turn_origin(+ core,+ origin,+ Box::pin(finish),+ )+ .await+ }None => finish.await,}})};- let registration_progress = progress_sink.clone();- let registration_completion_target = completion_target.clone();- let registration_parent_session = parent_session.clone();- let registration_agent_id = definition.id.clone();- let registration_task_id = task_id.clone();- let registration_display_name = Some(definition.display_name().to_string());- let registration_worker_thread_id = worker_thread_id.clone();- let registration_prompt = prompt.clone();let background_prompt = add_background_contract(&prompt);let options = SubagentRunOptions {skill_filter_override: None,@@ -740,11 +740,7 @@ impl SpawnAsyncSubagentTool {}let message = error.to_string();registration_completion_target- .failed(- ®istration_task_id,- ®istration_agent_id,- &message,- )+ .failed(®istration_task_id, ®istration_agent_id, &message).await;crate::agent::orchestration::subagent_events::publish_subagent_failed(registration_parent_session,@@ -761,11 +757,6 @@ impl SpawnAsyncSubagentTool {}).await;}- let _ = (- registration_display_name,- registration_worker_thread_id,- registration_prompt,- );return Ok(ToolResult::error(format!("spawn_async_subagent: failed to register detached sub-agent: {error}")));