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
This commit is contained in:
tongzhou wang
2026-11-15 00:39:00 +00:00
committed by copyberry
parent eaa25e1f43
commit 3b8bd2ee7e
5 changed files with 54 additions and 8 deletions
+14 -3
View File
@@ -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<Output = Result<OpenedModelResponse>>,
) -> Result<SamplingStream> {
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(),
+11 -3
View File
@@ -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());
+13 -2
View File
@@ -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!(
(
@@ -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",
@@ -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,