Add optional process metrics sampling for code-mode hosts (#57841)

## What changed

- Add `GetProcessMetrics` and provider sampling APIs for process ID, start time, cumulative CPU time, and resident memory, exposed through `ThreadManager` without creating a thread or session.
- Distinguish unsupported collection, a locally absent host, and sampling failures. Process-owned providers never start or replace a host when sampling.
- Bound gRPC sampling to one second without retries or explicit connection resets. Run optional collectors on a detached worker with at most one collection in flight, so stalled collection cannot block the HTTP/2 driver or runtime shutdown.

## Testing

Add coverage for snapshots, collector errors and panics, deadlines, overload, and shutdown with a stuck collector. Extend lifecycle tests to check that sampling preserves session state and keepalive behavior without starting or restarting local hosts.

GitOrigin-RevId: eeafe275891d0464636f28a29345a87aed1a12dc
This commit is contained in:
aakash-oai
2026-10-28 21:34:25 +00:00
committed by copyberry
parent 7ce5fa7bdc
commit d373d64967
18 changed files with 559 additions and 15 deletions
+17 -1
View File
@@ -44,13 +44,16 @@ use self::delegate::RemoteDelegate;
use self::peer::HostPeer;
pub use self::grpc::GrpcCodeModeHost;
pub use self::process_metrics::CodeModeProcessMetricsCollector;
pub use self::transport::DEFAULT_LISTEN_URL;
pub use codex_code_mode_protocol::CodeModeProcessMetrics;
mod delegate;
mod grpc;
mod grpc_stdio;
mod grpc_transport;
mod peer;
mod process_metrics;
mod transport;
const MAX_IN_FLIGHT_REQUESTS: usize = 256;
@@ -95,7 +98,20 @@ impl HostLimits {
fields(otel.name = "code_mode_host.run_main")
)]
pub async fn run_main(listen_url: &str) -> Result<()> {
transport::run_transport(listen_url).await
transport::run_transport(listen_url, GrpcCodeModeHost::new()).await
}
/// Runs with an optional gRPC metrics collector initialized before sandbox entry.
/// The legacy stdio transport remains unchanged.
pub async fn run_main_with_process_metrics(
listen_url: &str,
collector: CodeModeProcessMetricsCollector,
) -> Result<()> {
transport::run_transport(
listen_url,
GrpcCodeModeHost::with_process_metrics(collector),
)
.await
}
/// Runs one code-mode host connection over the process standard streams.