diff --git a/codex-rs/core/src/tools/handlers/subagent.rs b/codex-rs/core/src/tools/handlers/subagent.rs index b59b16bc27..90da04f91c 100644 --- a/codex-rs/core/src/tools/handlers/subagent.rs +++ b/codex-rs/core/src/tools/handlers/subagent.rs @@ -17,6 +17,8 @@ use codex_protocol::protocol::ExecCommandSource; use serde::Deserialize; use serde::Serialize; use serde::de::DeserializeOwned; +use serde_json::Map; +use serde_json::Value; use serde_json::json; use crate::codex::Session; @@ -133,12 +135,6 @@ impl InvocationContext { } } -enum LogStrategy { - Always(Vec), - OnSuccess(Vec), - None, -} - struct ExecEventLogger { session: Arc, turn: Arc, @@ -505,64 +501,40 @@ fn display_label_or_metadata(label: &Option, meta: Option<&SubagentMetad async fn run_with_logging( ctx: &InvocationContext, - strategy: LogStrategy, + command: Vec, op: F, ) -> Result where F: FnOnce() -> Fut, Fut: Future>, { - match strategy { - LogStrategy::None => op().await, - LogStrategy::Always(command) => { - let logger = if ctx.is_root_agent { - Some( - ExecEventLogger::new( - ctx.session.clone(), - ctx.turn.clone(), - ctx.call_id.clone(), - command, - ) - .await, - ) - } else { - None - }; + let logger = if ctx.is_root_agent { + Some( + ExecEventLogger::new( + ctx.session.clone(), + ctx.turn.clone(), + ctx.call_id.clone(), + command, + ) + .await, + ) + } else { + None + }; - let result = op().await; + let result = op().await; - if let Some(ref logger) = logger { - match &result { - Ok(out) => { - let summary = summarize_tool_output(&ctx.tool_name, &ctx.arguments, out); - logger.success(&summary).await; - } - Err(err) => logger.failure(&err.to_string()).await, - } + if let Some(ref logger) = logger { + match &result { + Ok(out) => { + let summary = summarize_tool_output(&ctx.tool_name, &ctx.arguments, out); + logger.success(&summary).await; } - - result - } - LogStrategy::OnSuccess(command) => { - let result = op().await; - - if let Ok(out) = &result { - if ctx.is_root_agent { - let logger = ExecEventLogger::new( - ctx.session.clone(), - ctx.turn.clone(), - ctx.call_id.clone(), - command, - ) - .await; - let summary = summarize_tool_output(&ctx.tool_name, &ctx.arguments, out); - logger.success(&summary).await; - } - } - - result + Err(err) => logger.failure(&err.to_string()).await, } } + + result } #[derive(Serialize)] @@ -693,8 +665,7 @@ async fn handle_spawn(ctx: &InvocationContext) -> Result Result Result Result Result Result Result()); Ok(ToolOutput::Function { content: response.to_string(), @@ -1019,17 +990,13 @@ async fn handle_list(ctx: &InvocationContext) -> Result Result Result(&ctx.arguments).unwrap_or_default(); let manager = ctx.manager.clone(); - run_with_logging( - ctx, - LogStrategy::Always(vec!["Pruned subagents".to_string()]), - move || { - let manager = manager.clone(); - async move { - let report = manager - .prune(request) - .await - .map_err(|err| map_manager_error(err, None))?; + run_with_logging(ctx, vec!["Pruned subagents".to_string()], move || { + let manager = manager.clone(); + async move { + let report = manager + .prune(request) + .await + .map_err(|err| map_manager_error(err, None))?; - let response = json!({ - "request": request_echo, - "pruned": report.pruned, - "skipped_active": report.skipped_active, - "unknown": report.unknown, - "errors": report.errors, - "counts": { - "pruned": report.pruned.len(), - "skipped_active": report.skipped_active.len(), - "unknown": report.unknown.len(), - "errors": report.errors.len(), - } - }); + let response = json!({ + "request": request_echo, + "pruned": report.pruned, + "skipped_active": report.skipped_active, + "unknown": report.unknown, + "errors": report.errors, + "counts": { + "pruned": report.pruned.len(), + "skipped_active": report.skipped_active.len(), + "unknown": report.unknown.len(), + "errors": report.errors.len(), + } + }); - Ok(ToolOutput::Function { - content: response.to_string(), - content_items: None, - success: Some(report.errors.is_empty()), - }) - } - }, - ) + Ok(ToolOutput::Function { + content: response.to_string(), + content_items: None, + success: Some(report.errors.is_empty()), + }) + } + }) .await } @@ -1204,7 +1167,7 @@ async fn handle_logs(ctx: &InvocationContext) -> Result Result, - payload: Option, + extras: impl IntoIterator, ) -> Value { let mut map = Map::new(); map.insert("session_id".to_string(), json!(metadata.session_id)); @@ -1349,11 +1311,8 @@ fn build_subagent_response( ); map.insert("origin".to_string(), json!(metadata.origin)); map.insert("status".to_string(), json!(metadata.status)); - if let Some(model) = model_value { - map.insert("model".to_string(), model); - } - if let Some(payload_value) = payload { - map.insert("payload".to_string(), payload_value); + for (key, value) in extras { + map.insert(key.to_string(), value); } map.insert("label".to_string(), json!(metadata.label)); map.insert("summary".to_string(), json!(metadata.summary));