mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
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:
@@ -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(),
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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!(
|
||||
(
|
||||
|
||||
Reference in New Issue
Block a user