Track code-mode cell lifecycle and result buffering time (#55653)

## What changed

- Add `CodeModeCellTiming` notifications for yielded executions, terminal waits, and unavailable cells, with thread, call, and cell identifiers.
- Measure how long completed results remain buffered before retrieval and carry `result_buffered_for` through the runtime, stdio protocol, and gRPC response metadata. Immediate delivery reports zero; waits without a completed result report no buffering measurement.
- Default missing buffering data to zero for compatibility with older hosts.

## Testing

Extend runtime tests to verify buffering duration with a paused clock and retrieval through termination. Add gRPC conversion tests for nanosecond precision, optional metadata, and the distinction between immediate delivery and no completed result.

GitOrigin-RevId: 60c0c5a3affa69ac3779bf1812fca50f5748af76
This commit is contained in:
Ruslan Nigmatullin
2026-10-20 18:53:06 +00:00
committed by copyberry
parent 801bd4d773
commit 87f3c3284e
38 changed files with 397 additions and 123 deletions
@@ -144,7 +144,12 @@ impl CodeModeExecuteHandler {
code_cell_trace.record_initial_response(&response);
// Yielded cells keep running, so terminal lifecycle is only emitted
// here when the first response also ended the runtime.
if !matches!(response, codex_code_mode::RuntimeResponse::Yielded { .. }) {
if matches!(response, codex_code_mode::RuntimeResponse::Yielded { .. }) {
telemetry.record_cell_timing(
&cell_id,
codex_extension_api::CodeModeCellTiming::ExecYielded,
);
} else {
code_cell_trace.record_ended(&response);
exec.session
.services
@@ -87,6 +87,21 @@ impl CodeModeToolCallGuard {
"code-mode host operation completed"
);
}
pub(super) fn record_cell_timing(
&self,
cell_id: &codex_code_mode::CellId,
timing: codex_extension_api::CodeModeCellTiming,
) {
for observer in self.extensions.tool_lifecycle_contributors() {
observer.on_code_mode_cell_timing(
&self.thread_id,
&self.call_id,
cell_id.as_str(),
timing,
);
}
}
}
impl Drop for CodeModeToolCallGuard {
@@ -9,6 +9,8 @@ use crate::tools::registry::CoreToolRuntime;
use crate::tools::registry::PostToolUsePayload;
use crate::tools::registry::PreToolUsePayload;
use crate::tools::registry::ToolExecutor;
use codex_code_mode::RuntimeResponse;
use codex_extension_api::CodeModeCellTiming;
use codex_tools::ToolName;
use codex_tools::ToolSpec;
@@ -128,7 +130,7 @@ impl CodeModeWaitHandler {
exec.session
.services
.code_mode_service
.terminate(cell_id)
.terminate(cell_id.clone())
.await
} else {
exec.session
@@ -136,7 +138,7 @@ impl CodeModeWaitHandler {
.code_mode_service
.wait(
codex_code_mode::WaitRequest {
cell_id,
cell_id: cell_id.clone(),
yield_time_ms: args.yield_time_ms,
},
step_context.preempt.clone(),
@@ -149,9 +151,9 @@ impl CodeModeWaitHandler {
})?;
if let codex_code_mode::WaitOutcome::LiveCell(response) = &wait_response {
let runtime_cell_id = match response {
codex_code_mode::RuntimeResponse::Yielded { cell_id, .. }
| codex_code_mode::RuntimeResponse::Terminated { cell_id, .. }
| codex_code_mode::RuntimeResponse::Result { cell_id, .. } => cell_id,
RuntimeResponse::Yielded { cell_id, .. }
| RuntimeResponse::Terminated { cell_id, .. }
| RuntimeResponse::Result { cell_id, .. } => cell_id,
};
tracing::Span::current().record("cell.id", trace_id(runtime_cell_id.as_str()));
telemetry.cell_id = Some(runtime_cell_id.to_string());
@@ -159,7 +161,15 @@ impl CodeModeWaitHandler {
.services
.executed_tool_calls
.register_cell(runtime_cell_id, &call_id);
if !matches!(response, codex_code_mode::RuntimeResponse::Yielded { .. }) {
if !matches!(response, RuntimeResponse::Yielded { .. }) {
telemetry.record_cell_timing(
runtime_cell_id,
CodeModeCellTiming::WaitFinished {
result_buffered_for: wait_response
.result_buffered_for()
.unwrap_or_default(),
},
);
exec.session
.services
.rollout_thread_trace
@@ -183,6 +193,8 @@ impl CodeModeWaitHandler {
},
);
}
} else {
telemetry.record_cell_timing(&cell_id, CodeModeCellTiming::CellUnavailable);
}
if let Some(code_mode_host_duration) = wait_response.code_mode_host_duration() {
telemetry.record_code_mode_host_duration(code_mode_host_duration);