From 3b8bd2ee7e95b058192151b3468093a5a67655f8 Mon Sep 17 00:00:00 2001 From: tongzhou wang Date: Sat, 14 Nov 2026 23:33:39 +0000 Subject: [PATCH] Add request IDs and attempt numbers to model request hooks (#61587) ## Why Model request hooks need to correlate attempts belonging to the same generation across retries and model fallback. ## What changed Add `request_id` and one-based `attempt_number` fields to `PreModelRequest` and `PostModelRequest` inputs and their generated schema. Keep the request ID stable across retries and increment the attempt number for each call to open a response stream. ## Testing Extend hook tests to verify matching request IDs and increasing attempt numbers across retries. Extend the background compaction scenario to verify that its pre/post hooks share an ID distinct from the ordinary sampling request. GitOrigin-RevId: 2d85ad6fd630505a89a712cebab6ca0f731610c9 --- codex-rs/core/src/hook_runtime/sampling.rs | 17 ++++++++++++++--- codex-rs/core/tests/suite/hooks.rs | 14 +++++++++++--- codex-rs/core/tests/suite/scenarios.rs | 15 +++++++++++++-- .../model-request.command.input.schema.json | 12 ++++++++++++ codex-rs/hooks/src/events/model_request.rs | 4 ++++ 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/codex-rs/core/src/hook_runtime/sampling.rs b/codex-rs/core/src/hook_runtime/sampling.rs index eddb452f4b..0fe8d95989 100644 --- a/codex-rs/core/src/hook_runtime/sampling.rs +++ b/codex-rs/core/src/hook_runtime/sampling.rs @@ -1,5 +1,6 @@ -//! Each provider attempt pairs its own hooks. Transport recovery within an -//! attempt does not start another hook pair. +//! A sampling loop keeps one request ID across retries. Each call to open a +//! response stream pairs its own hooks; transport recovery within that call +//! does not start another hook pair. use super::emit_hook_completed_events; use super::emit_hook_started_events; @@ -24,14 +25,21 @@ use std::sync::Arc; use std::time::Instant; use tokio::sync::oneshot; use tokio_util::sync::CancellationToken; +use uuid::Uuid; pub(crate) struct SamplingRequest { + id: Uuid, + attempts: u32, kind: SamplingKind, } impl SamplingRequest { pub(crate) fn new(kind: SamplingKind) -> Self { - Self { kind } + Self { + id: Uuid::new_v4(), + attempts: 0, + kind, + } } pub(crate) fn open( @@ -54,6 +62,7 @@ impl SamplingRequest { owner_cancellation: &CancellationToken, send: impl Future>, ) -> Result { + self.attempts += 1; let sampling_turn = match self.kind { SamplingKind::Sampling => Some(Arc::clone(&step.turn)), SamplingKind::ForegroundCompaction | SamplingKind::BackgroundCompaction => None, @@ -69,6 +78,8 @@ impl SamplingRequest { let input = ModelRequestHookInput { lifecycle_id: uuid::Uuid::new_v4(), kind: self.kind, + request_id: self.id.to_string(), + attempt_number: self.attempts, session_id: session.session_id().to_string(), thread_id: session.thread_id().to_string(), turn_id: step.turn.sub_id.clone(), diff --git a/codex-rs/core/tests/suite/hooks.rs b/codex-rs/core/tests/suite/hooks.rs index f821e09476..3223cc4704 100644 --- a/codex-rs/core/tests/suite/hooks.rs +++ b/codex-rs/core/tests/suite/hooks.rs @@ -6135,10 +6135,18 @@ async fn model_request_hooks_report_failures_and_retries_but_cannot_veto() -> Re wait_for_hook_inputs(test.codex_home_path(), "PostModelRequest", /*count*/ 2).await?; assert_eq!(pre.len(), 2); assert_eq!(post.len(), 2); + let request_id = pre[0]["request_id"] + .as_str() + .context("sampling request id")?; let thread_id = test.session_configured.thread_id.to_string(); - for hook in pre.iter().chain(&post) { - assert_eq!(hook["thread_id"], thread_id); - assert_eq!(hook["kind"], "sampling"); + assert!(!request_id.is_empty()); + for (index, pair) in pre.iter().zip(&post).enumerate() { + for hook in [pair.0, pair.1] { + assert_eq!(hook["request_id"], request_id); + assert_eq!(hook["attempt_number"], index + 1); + assert_eq!(hook["thread_id"], thread_id); + assert_eq!(hook["kind"], "sampling"); + } } assert_eq!(post[0]["status"], "failed"); assert!(post[0]["error"].is_string()); diff --git a/codex-rs/core/tests/suite/scenarios.rs b/codex-rs/core/tests/suite/scenarios.rs index 9fd8443652..05c0ed2048 100644 --- a/codex-rs/core/tests/suite/scenarios.rs +++ b/codex-rs/core/tests/suite/scenarios.rs @@ -4621,8 +4621,8 @@ async fn async_compaction_reuses_pending_work( "type": "mcp_tool", "server": "compaction-observer", "tool": tool, "input": input }]}]); let hooks = json!({ - "PreModelRequest": hook("pre", json!({"kind": "${kind}"})), - "PostModelRequest": hook("post", json!({"kind": "${kind}", "response_id": "${response_id}", "token_usage": "${token_usage}"})), + "PreModelRequest": hook("pre", json!({"kind": "${kind}", "request_id": "${request_id}"})), + "PostModelRequest": hook("post", json!({"kind": "${kind}", "request_id": "${request_id}", "response_id": "${response_id}", "token_usage": "${token_usage}"})), }); let config = json!({"hooks": hooks, "mcp_servers": {"compaction-observer": { "url": url, "environment_id": super::rmcp_client::remote_aware_environment_id() @@ -4835,10 +4835,21 @@ async fn async_compaction_reuses_pending_work( (1, 1), "adopting the candidate must not run a second attempt" ); + let background_id = post[0]["request_id"] + .as_str() + .expect("background request ID"); + assert_eq!(pre[0]["request_id"], background_id); assert_eq!(pre[0]["kind"], "background_compaction"); assert_eq!(post[0]["kind"], pre[0]["kind"]); assert_eq!(post[0]["response_id"], "background"); assert_eq!(post[0]["token_usage"]["total_tokens"], BACKGROUND_TOKENS); + let seed_id = calls + .iter() + .map(|call| &call["arguments"]) + .find(|input| input["response_id"] == "seed") + .and_then(|input| input["request_id"].as_str()) + .expect("ordinary seed request ID"); + assert_ne!(seed_id, background_id); } assert_eq!( ( diff --git a/codex-rs/hooks/schema/generated/model-request.command.input.schema.json b/codex-rs/hooks/schema/generated/model-request.command.input.schema.json index 467b2d46b8..a78e125a11 100644 --- a/codex-rs/hooks/schema/generated/model-request.command.input.schema.json +++ b/codex-rs/hooks/schema/generated/model-request.command.input.schema.json @@ -130,6 +130,12 @@ "agent_type": { "type": "string" }, + "attempt_number": { + "description": "One-based attempt number within this generation.", + "format": "uint32", + "minimum": 0.0, + "type": "integer" + }, "cwd": { "$ref": "#/definitions/AbsolutePathBuf" }, @@ -139,6 +145,10 @@ "model": { "type": "string" }, + "request_id": { + "description": "Identifies a generation across harness retries and model fallback.", + "type": "string" + }, "session_id": { "type": "string" }, @@ -154,9 +164,11 @@ } }, "required": [ + "attempt_number", "cwd", "kind", "model", + "request_id", "session_id", "thread_id", "transcript_path", diff --git a/codex-rs/hooks/src/events/model_request.rs b/codex-rs/hooks/src/events/model_request.rs index 783819c1f2..e65d64e410 100644 --- a/codex-rs/hooks/src/events/model_request.rs +++ b/codex-rs/hooks/src/events/model_request.rs @@ -122,6 +122,10 @@ pub struct ModelRequestHookInput { #[serde(skip)] pub lifecycle_id: uuid::Uuid, pub kind: SamplingKind, + /// Identifies a generation across harness retries and model fallback. + pub request_id: String, + /// One-based attempt number within this generation. + pub attempt_number: u32, pub session_id: String, /// Identifies the issuing thread within its shared session. pub thread_id: String,