Track plugin metrics for background unified exec commands (#38276)

## Why

Unified exec can yield while a command is still running. Plugin measurement
collection must remain active until that background command exits, including
when its item completion arrives after the turn has completed.

## What changed

- Keep the plugin metrics sidecar with the stored process and let either the
  exit watcher or a poll that observes completion finalize it exactly once.
- Retain completed turn analytics state while tool items are pending so late
  command completion events can be emitted without duplicating the turn event.

## Testing

- Verify that a background command completed after its turn emits a command
  execution event and does not emit a second turn event.

GitOrigin-RevId: ecf715b3e047aa29ca9a417d12d955257fed8557
This commit is contained in:
Kyle Brown authored and copyberry committed 2026-08-13 00:04:57 +00:00
1 parent cbb7e82a8b
commit 96e8afbfb8
8 files changed
+158 -8

No files matched your search

+13
View File
@@ -43,6 +43,7 @@ use crate::session::turn_context::TurnContext;
use crate::session::turn_context::TurnEnvironment;
use crate::shell::ShellType;
use crate::tools::network_approval::DeferredNetworkApproval;
use codex_core_plugins::PluginMetricsSidecar;
mod async_watcher;
mod errors;
@@ -167,6 +168,7 @@ impl Default for UnifiedExecProcessManager {
struct ProcessEntry {
process: Arc<UnifiedExecProcess>,
plugin_metrics_sidecar: Option<SharedPluginMetricsSidecar>,
call_id: String,
process_id: i32,
cwd: PathUri,
@@ -178,6 +180,17 @@ struct ProcessEntry {
last_used: tokio::time::Instant,
}
type SharedPluginMetricsSidecar = Arc<std::sync::Mutex<Option<PluginMetricsSidecar>>>;
fn take_plugin_metrics_sidecar(
sidecar: &SharedPluginMetricsSidecar,
) -> Option<PluginMetricsSidecar> {
sidecar
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.take()
}
pub(crate) fn clamp_yield_time(yield_time_ms: u64) -> u64 {
let yield_time_ms = if cfg!(windows) {
yield_time_ms.max(WINDOWS_INITIAL_EXEC_YIELD_TIME_FLOOR_MS)