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
+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!(
(