diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 33387e620c..71a3c52411 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -3346,6 +3346,7 @@ dependencies = [ "thiserror 2.0.18", "tokio", "tracing", + "uuid", ] [[package]] diff --git a/codex-rs/core/src/guardian/tests.rs b/codex-rs/core/src/guardian/tests.rs index 9aa8ac0752..aa108f5fd0 100644 --- a/codex-rs/core/src/guardian/tests.rs +++ b/codex-rs/core/src/guardian/tests.rs @@ -67,6 +67,8 @@ use core_test_support::PathBufExt; use core_test_support::TempDirExt; use core_test_support::context_snapshot; use core_test_support::context_snapshot::ContextSnapshotOptions; +use core_test_support::responses::assert_parent_turn; +use core_test_support::responses::assert_root_turn; use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; use core_test_support::responses::ev_response_created; @@ -3300,6 +3302,10 @@ async fn guardian_ephemeral_retry_preserves_parallel_trunk_and_fork_history() -> .await; let (session, turn) = guardian_test_session_and_turn_with_base_url(server.uri()).await; + turn.turn_metadata_state + .set_parent_turn_id("upstream-parent-turn".to_string()); + turn.turn_metadata_state + .set_root_turn_id("causal-root-turn".to_string()); seed_guardian_parent_history(&session, &turn).await; let initial_request = GuardianApprovalRequest::ExecCommand { @@ -3418,6 +3424,8 @@ async fn guardian_ephemeral_retry_preserves_parallel_trunk_and_fork_history() -> ) .await; + // A conflicting input removes the known root while the trunk review is in flight. + turn.turn_metadata_state.mark_root_turn_ambiguous(); let third_decision = review_approval_request( &session, &turn, @@ -3432,11 +3440,27 @@ async fn guardian_ephemeral_retry_preserves_parallel_trunk_and_fork_history() -> assert_eq!(third_decision, ReviewDecision::Approved); let requests = server.requests().await; assert_eq!(requests.len(), 4); + let first_request_body = serde_json::from_slice::(&requests[0])?; let second_request_body = serde_json::from_slice::(&requests[1])?; let failed_ephemeral_request_body = serde_json::from_slice::(&requests[2])?; let retried_ephemeral_request_body = serde_json::from_slice::(&requests[3])?; + let mut reviewer_turn_ids = std::collections::BTreeSet::new(); + for (body, expected_root) in [ + (&first_request_body, Some("causal-root-turn")), + (&second_request_body, Some("causal-root-turn")), + (&failed_ephemeral_request_body, None), + (&retried_ephemeral_request_body, None), + ] { + assert_parent_turn(body, Some(turn.sub_id.as_str()))?; + assert_root_turn(body, expected_root)?; + assert_ne!(body["client_metadata"]["turn_id"], turn.sub_id); + reviewer_turn_ids.insert( + body["client_metadata"]["turn_id"].as_str().expect("reviewer turn id") + ); + } + assert_eq!(reviewer_turn_ids.len(), 4); assert_eq!( second_request_body["prompt_cache_key"], failed_ephemeral_request_body["prompt_cache_key"], diff --git a/codex-rs/core/src/tools/lifecycle.rs b/codex-rs/core/src/tools/lifecycle.rs index 17108f4bb3..bea620e154 100644 --- a/codex-rs/core/src/tools/lifecycle.rs +++ b/codex-rs/core/src/tools/lifecycle.rs @@ -28,6 +28,7 @@ pub(crate) async fn notify_tool_start( } let thread_store = &invocation.session.services.thread_extension_data; let conversation_history = invocation.session.conversation_history_snapshot().await; + let root_turn_id = invocation.turn.turn_metadata_state.root_turn_id(); for contributor in contributors { contributor @@ -36,6 +37,7 @@ pub(crate) async fn notify_tool_start( thread_store, turn_store: invocation.turn.extension_data.as_ref(), turn_id: invocation.turn.sub_id.as_str(), + root_turn_id: root_turn_id.as_deref(), call_id: invocation.call_id.as_str(), tool_name: &invocation.tool_name, mcp_tool, diff --git a/codex-rs/core/src/tools/registry_tests.rs b/codex-rs/core/src/tools/registry_tests.rs index 798dba3652..b862988bec 100644 --- a/codex-rs/core/src/tools/registry_tests.rs +++ b/codex-rs/core/src/tools/registry_tests.rs @@ -136,6 +136,7 @@ enum RecordedToolLifecycle { Start { call_id: String, tool_name: codex_tools::ToolName, + root_turn_id: Option, }, Finish { call_id: String, @@ -157,6 +158,7 @@ impl codex_extension_api::ToolLifecycleContributor for ToolLifecycleRecorder { let record = RecordedToolLifecycle::Start { call_id: input.call_id.to_string(), tool_name: input.tool_name.clone(), + root_turn_id: input.root_turn_id.map(str::to_owned), }; Box::pin(async move { records @@ -640,6 +642,8 @@ fn post_tool_use_feedback_output_keeps_code_mode_result_typed() { #[tokio::test] async fn dispatch_uses_canonical_tool_names_for_lifecycle_contributors() -> anyhow::Result<()> { let (mut session, turn) = crate::session::tests::make_session_and_context().await; + turn.turn_metadata_state + .set_root_turn_id("root-turn".to_string()); let records = Arc::new(std::sync::Mutex::new(Vec::new())); let mut builder = codex_extension_api::ExtensionRegistryBuilder::::new(); builder.tool_lifecycle_contributor(Arc::new(ToolLifecycleRecorder { @@ -672,6 +676,7 @@ async fn dispatch_uses_canonical_tool_names_for_lifecycle_contributors() -> anyh /*terminal_outcome_reached*/ None, ) .await?; + turn.turn_metadata_state.mark_root_turn_ambiguous(); let err = match registry .dispatch_any_with_terminal_outcome( test_invocation( @@ -693,6 +698,7 @@ async fn dispatch_uses_canonical_tool_names_for_lifecycle_contributors() -> anyh RecordedToolLifecycle::Start { call_id: "ok-call".to_string(), tool_name: ok_tool.clone().with_default_namespace(), + root_turn_id: Some("root-turn".to_string()), }, RecordedToolLifecycle::Finish { call_id: "ok-call".to_string(), @@ -702,6 +708,7 @@ async fn dispatch_uses_canonical_tool_names_for_lifecycle_contributors() -> anyh RecordedToolLifecycle::Start { call_id: "failing-call".to_string(), tool_name: failing_tool.clone(), + root_turn_id: None, }, RecordedToolLifecycle::Finish { call_id: "failing-call".to_string(), diff --git a/codex-rs/core/tests/suite/guardian_review.rs b/codex-rs/core/tests/suite/guardian_review.rs index 592a855a44..f40ff66038 100644 --- a/codex-rs/core/tests/suite/guardian_review.rs +++ b/codex-rs/core/tests/suite/guardian_review.rs @@ -910,6 +910,11 @@ async fn guardian_session_is_reused_for_consecutive_tool_reviews_without_prewarm ), ev_completed("resp-guardian-first-assessment"), ]), + sse(vec![ + ev_response_created("resp-parent-first-done"), + ev_assistant_message("msg-parent-first-done", "first done"), + ev_completed("resp-parent-first-done"), + ]), sse(vec![ ev_response_created("resp-parent-second-tool"), ev_function_call( @@ -966,7 +971,7 @@ async fn guardian_session_is_reused_for_consecutive_tool_reviews_without_prewarm test.codex .start_or_steer_turn( TurnInputRequest::user_input(vec![UserInput::Text { - text: "run two commands that require Guardian review".into(), + text: "run the first command that requires Guardian review".into(), text_elements: Vec::new(), }]) .with_thread_settings(ThreadSettingsOverrides { @@ -981,6 +986,8 @@ async fn guardian_session_is_reused_for_consecutive_tool_reviews_without_prewarm matches!(event, EventMsg::TurnComplete(_)) }) .await; + test.submit_text_turn("run the second command that requires Guardian review") + .await?; let requests = responses.requests(); assert!( requests @@ -997,6 +1004,33 @@ async fn guardian_session_is_reused_for_consecutive_tool_reviews_without_prewarm assert_eq!(guardian_requests.len(), 3); let first_guardian_request = guardian_requests[0].body_json(); let second_guardian_request = guardian_requests[2].body_json(); + let first_parent_request = requests[0].body_json(); + let second_parent_request = requests[4].body_json(); + let first_parent_turn_id = first_parent_request["client_metadata"]["turn_id"] + .as_str() + .expect("first owning turn id"); + let second_parent_turn_id = second_parent_request["client_metadata"]["turn_id"] + .as_str() + .expect("second owning turn id"); + assert_ne!(first_parent_turn_id, second_parent_turn_id); + for (request, parent_turn_id) in guardian_requests.iter().zip([ + first_parent_turn_id, + first_parent_turn_id, + second_parent_turn_id, + ]) { + let body = request.body_json(); + assert_parent_turn(&body, Some(parent_turn_id))?; + assert_root_turn(&body, Some(parent_turn_id))?; + assert_ne!(body["client_metadata"]["turn_id"], parent_turn_id); + assert_eq!( + body["client_metadata"]["session_id"], + first_parent_request["client_metadata"]["session_id"] + ); + } + assert_ne!( + first_guardian_request["client_metadata"]["turn_id"], + second_guardian_request["client_metadata"]["turn_id"] + ); let first_guardian_thread_id = first_guardian_request["client_metadata"]["thread_id"] .as_str() .expect("first Guardian review should have a thread id"); diff --git a/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs b/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs index cdf42a920d..7842805c83 100644 --- a/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs +++ b/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs @@ -123,6 +123,8 @@ pub struct ToolStartInput<'a> { pub turn_store: &'a ExtensionData, /// Current turn submission id. pub turn_id: &'a str, + /// Trusted causal root of the owning turn, absent when unknown or ambiguous. + pub root_turn_id: Option<&'a str>, /// Model-visible tool call id. pub call_id: &'a str, /// Tool name as routed by the host. diff --git a/codex-rs/ext/guardian-v2/Cargo.toml b/codex-rs/ext/guardian-v2/Cargo.toml index 7a746581a4..3e28970a36 100644 --- a/codex-rs/ext/guardian-v2/Cargo.toml +++ b/codex-rs/ext/guardian-v2/Cargo.toml @@ -30,6 +30,7 @@ serde_json = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = ["time"] } tracing = { workspace = true } +uuid = { workspace = true } [dev-dependencies] anyhow = { workspace = true } diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs index 39cd90f99a..97d5000dab 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs @@ -409,6 +409,7 @@ impl GuardianV2Extension { let event_sink = Arc::clone(&self.event_sink); let thread_id = input.thread_store.level_id().to_owned(); let turn_id = input.turn_id.to_owned(); + let root_turn_id = input.root_turn_id.map(str::to_owned); let thread_context: Result<_, String> = async { let parsed_thread_id = ThreadId::from_string(&thread_id).map_err(|error| error.to_string())?; @@ -681,7 +682,8 @@ impl GuardianV2Extension { parent_compaction, parent_compaction_hash, reasoning_effort: guardian_config.reasoning_effort.clone(), - turn_id: turn_id.clone(), + parent_turn_id: turn_id.clone(), + root_turn_id, }) .await { diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs index f46f6c5c89..670f8f7e15 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs @@ -227,6 +227,7 @@ async fn installed_extension_reconnects_after_auth_refresh() -> Result<()> { thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id, tool_name: &tool_name, mcp_tool: None, @@ -276,6 +277,20 @@ async fn installed_extension_reconnects_after_auth_refresh() -> Result<()> { .collect::>(), expected_requests ); + let requests = server + .connections() + .into_iter() + .flatten() + .map(|request| request.body_json()) + .collect::>(); + for request in &requests { + responses::assert_parent_turn(request, Some("turn-1"))?; + responses::assert_root_turn(request, /*expected*/ None)?; + } + assert_ne!( + requests[0]["client_metadata"]["turn_id"], + requests[1]["client_metadata"]["turn_id"] + ); Ok(()) } @@ -439,6 +454,7 @@ async fn sandboxed_shell_classification_respects_review_scope() -> Result<()> { thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "call-2", tool_name: &tool_name, mcp_tool: None, @@ -518,6 +534,7 @@ async fn computer_use_only_scores_cannot_approve_other_actions() -> Result<()> { thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "ordinary-call", tool_name: &ordinary_tool, mcp_tool: None, @@ -952,6 +969,7 @@ async fn sample_configured_conversation_history_with_source( thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: Some("root-turn"), call_id: "call-1", tool_name: &tool_name, mcp_tool: None, @@ -1027,6 +1045,7 @@ impl GuardianFailureFixture { thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "call-1", tool_name: &tool_name, mcp_tool: None, @@ -1876,6 +1895,9 @@ async fn contributor_samples_tool_calls_with_the_existing_luna_pool() -> Result< .as_str() .expect("classifier thread ID"); assert_ne!(ThreadId::from_string(classifier_thread_id)?, thread_id); + let classifier_turn_id = request["client_metadata"]["turn_id"] + .as_str() + .expect("classifier turn ID"); let turn_metadata: serde_json::Value = serde_json::from_str( request["client_metadata"]["x-codex-turn-metadata"] .as_str() @@ -1887,7 +1909,9 @@ async fn contributor_samples_tool_calls_with_the_existing_luna_pool() -> Result< "session_id": request["client_metadata"]["session_id"], "thread_id": classifier_thread_id, "guardian_classifier_source_thread_id": thread_id.to_string(), - "turn_id": "turn-1", + "turn_id": classifier_turn_id, + "parent_turn_id": "turn-1", + "root_turn_id": "root-turn", "thread_source": "guardian_classifier", }) ); @@ -1896,7 +1920,8 @@ async fn contributor_samples_tool_calls_with_the_existing_luna_pool() -> Result< request["client_metadata"]["x-codex-window-id"], format!("{classifier_thread_id}:0") ); - assert_eq!(request["client_metadata"]["turn_id"], "turn-1"); + assert_eq!(request["client_metadata"]["parent_turn_id"], "turn-1"); + assert_eq!(request["client_metadata"]["root_turn_id"], "root-turn"); assert_eq!(request["reasoning"]["effort"], "low"); assert_eq!(request["reasoning"]["context"], "all_turns"); assert!(request.get("text").is_none()); @@ -2205,6 +2230,7 @@ async fn contributor_skips_required_models_in_standard_scope() -> Result<()> { thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "protected.md", tool_name: &tool_name, mcp_tool: None, @@ -2286,6 +2312,7 @@ async fn contributor_counts_failed_thread_lookups_toward_score_lag() -> Result<( thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "missing.md", tool_name: &tool_name, mcp_tool: None, @@ -2674,6 +2701,7 @@ async fn contributor_reuses_the_latest_compatible_parent_compaction() -> Result< thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "call-1", tool_name: &tool_name, mcp_tool: None, @@ -2744,6 +2772,7 @@ async fn contributor_reuses_the_latest_compatible_parent_compaction() -> Result< thread_store, turn_store: &turn_store, turn_id: "turn-1", + root_turn_id: None, call_id: "call-2", tool_name: &tool_name, mcp_tool: None, diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/sampler.rs b/codex-rs/ext/guardian-v2/src/async_scorer/sampler.rs index b2dcdcbf2a..f7f5aa411b 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/sampler.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/sampler.rs @@ -44,6 +44,7 @@ use thiserror::Error; use tokio::sync::OwnedSemaphorePermit; use tokio::sync::Semaphore; use tokio::sync::oneshot; +use uuid::Uuid; use super::trusted_skills::GuardianTrustedSkillsFragment; use super::trusted_tools::GuardianTrustedToolFragment; @@ -107,8 +108,10 @@ pub struct LunaSamplingRequest { pub parent_compaction_hash: Option, /// Reasoning budget explicitly selected for this request. pub reasoning_effort: ReasoningEffort, - /// Owning turn identifier used for request attribution. - pub turn_id: String, + /// Owning turn that initiated this classification, not the classifier turn. + pub parent_turn_id: String, + /// Trusted causal root of the owning turn, absent when unknown or ambiguous. + pub root_turn_id: Option, } /// Failures returned while connecting or sampling the Luna model. @@ -429,7 +432,10 @@ impl LunaSampler { /// Sends one tool-less classification request on an exclusively leased WebSocket. pub async fn sample(&self, request: LunaSamplingRequest) -> Result { - let turn_id = request.turn_id; + // A classification is its own inference turn; retries keep that identity. + let turn_id = Uuid::now_v7().to_string(); + let parent_turn_id = request.parent_turn_id; + let root_turn_id = request.root_turn_id; let mut input = vec![ ResponseItem::AdditionalTools { id: None, @@ -587,24 +593,30 @@ impl LunaSampler { self.config.service_tier.clone() }; let thread_id = &lease.connection.thread_id; - let turn_metadata = json!({ + let mut turn_metadata = json!({ "session_id": self.config.session_id, "thread_id": thread_id, "guardian_classifier_source_thread_id": self.config.thread_id, "turn_id": turn_id, + "parent_turn_id": parent_turn_id, "thread_source": "guardian_classifier", - }) - .to_string(); - request.client_metadata = Some(HashMap::from([ + }); + let mut client_metadata = HashMap::from([ ("session_id".to_owned(), self.config.session_id.clone()), ("thread_id".to_owned(), thread_id.clone()), ("turn_id".to_owned(), turn_id.clone()), + ("parent_turn_id".to_owned(), parent_turn_id.clone()), ("x-openai-subagent".to_owned(), "guardian".to_owned()), // Classifier requests do not advance their own context window. ("x-codex-window-id".to_owned(), format!("{thread_id}:0")), (RESPONSES_LITE_METADATA_KEY.to_owned(), "true".to_owned()), - (TURN_METADATA_KEY.to_owned(), turn_metadata), - ])); + ]); + if let Some(root_turn_id) = &root_turn_id { + client_metadata.insert("root_turn_id".to_owned(), root_turn_id.clone()); + turn_metadata["root_turn_id"] = json!(root_turn_id); + } + client_metadata.insert(TURN_METADATA_KEY.to_owned(), turn_metadata.to_string()); + request.client_metadata = Some(client_metadata); let mut stream = match lease .connection .connection diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/sampler_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/sampler_tests.rs index 674315dcc0..f797aad3c0 100644 --- a/codex-rs/ext/guardian-v2/src/async_scorer/sampler_tests.rs +++ b/codex-rs/ext/guardian-v2/src/async_scorer/sampler_tests.rs @@ -33,6 +33,7 @@ use std::sync::atomic::Ordering; use std::time::Duration; use tokio::net::TcpListener; use tokio::net::TcpStream; +use uuid::Uuid; use super::CLASSIFICATION_TOKEN_USAGE_METRIC; use super::INITIAL_WEBSOCKET_CONNECTIONS; @@ -55,7 +56,10 @@ impl LunaSampler { } } -fn assert_connection_metadata(server: &responses::WebSocketTestServer) -> Result { +fn assert_connection_metadata( + server: &responses::WebSocketTestServer, + expected_lineage: &[(&str, Option<&str>)], +) -> Result { let handshake = server.single_handshake(); let thread_id = handshake.header("thread-id").expect("classifier thread ID"); ThreadId::from_string(&thread_id)?; @@ -72,32 +76,43 @@ fn assert_connection_metadata(server: &responses::WebSocketTestServer) -> Result Some(format!("{thread_id}:0")), ] ); - for request in server.single_connection() { + let requests = server.single_connection(); + assert_eq!(requests.len(), expected_lineage.len()); + for (request, (parent_turn_id, root_turn_id)) in requests.iter().zip(expected_lineage) { let mut metadata = request.body_json()["client_metadata"].clone(); - let turn_id = metadata["turn_id"].clone(); + let turn_id = metadata["turn_id"] + .as_str() + .expect("classifier turn ID") + .to_owned(); + assert_eq!(Uuid::parse_str(&turn_id)?.get_version_num(), 7); + assert_ne!(turn_id.as_str(), *parent_turn_id); metadata["x-codex-turn-metadata"] = serde_json::from_str( metadata["x-codex-turn-metadata"] .as_str() .expect("serialized turn metadata"), )?; - assert_eq!( - metadata, - json!({ + let mut expected = json!({ + "session_id": "session-1", + "thread_id": thread_id, + "turn_id": turn_id, + "parent_turn_id": parent_turn_id, + "x-openai-subagent": "guardian", + "x-codex-window-id": format!("{thread_id}:0"), + "ws_request_header_x_openai_internal_codex_responses_lite": "true", + "x-codex-turn-metadata": { "session_id": "session-1", "thread_id": thread_id, + "guardian_classifier_source_thread_id": "thread-1", "turn_id": turn_id, - "x-openai-subagent": "guardian", - "x-codex-window-id": format!("{thread_id}:0"), - "ws_request_header_x_openai_internal_codex_responses_lite": "true", - "x-codex-turn-metadata": { - "session_id": "session-1", - "thread_id": thread_id, - "guardian_classifier_source_thread_id": "thread-1", - "turn_id": turn_id, - "thread_source": "guardian_classifier", - }, - }) - ); + "parent_turn_id": parent_turn_id, + "thread_source": "guardian_classifier", + }, + }); + if let Some(root_turn_id) = root_turn_id { + expected["root_turn_id"] = json!(root_turn_id); + expected["x-codex-turn-metadata"]["root_turn_id"] = json!(root_turn_id); + } + assert_eq!(metadata, expected); } Ok(thread_id) } @@ -174,7 +189,7 @@ async fn connect_sampler(config: LunaSamplerConfig) -> Result { Ok(sampler) } -fn sample_request(turn_id: &str) -> LunaSamplingRequest { +fn sample_request(parent_turn_id: &str) -> LunaSamplingRequest { LunaSamplingRequest { instructions: "Return high for high risk or low for low risk.".to_owned(), trusted_review_evidence: Vec::new(), @@ -185,7 +200,8 @@ fn sample_request(turn_id: &str) -> LunaSamplingRequest { parent_compaction: None, parent_compaction_hash: None, reasoning_effort: ReasoningEffort::None, - turn_id: turn_id.to_owned(), + parent_turn_id: parent_turn_id.to_owned(), + root_turn_id: None, } } @@ -359,8 +375,14 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications ], ]; let idle_server = responses::start_websocket_server(vec![scripted_requests.clone()]).await; - let refreshed = - responses::start_websocket_server(vec![vec![scripted_requests[1].clone()]]).await; + let refreshed = responses::start_websocket_server(vec![vec![ + scripted_requests[1].clone(), + vec![ + ev_assistant_message("sample-3", "low"), + ev_completed("response-3"), + ], + ]]) + .await; let server = responses::start_websocket_server(vec![scripted_requests]).await; let base_url = proxy_websocket_servers_with_prewarm_limit( &[&idle_server, &server, &refreshed], @@ -435,7 +457,8 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications parent_compaction: None, parent_compaction_hash: None, reasoning_effort: ReasoningEffort::None, - turn_id: "turn-1".to_owned(), + parent_turn_id: "turn-1".to_owned(), + root_turn_id: Some("turn-1".to_owned()), }) .await?; tokio::time::timeout(Duration::from_secs(2), async { @@ -462,12 +485,15 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications parent_compaction: None, parent_compaction_hash: None, reasoning_effort: ReasoningEffort::Medium, - turn_id: "turn-2".to_owned(), + parent_turn_id: "turn-2".to_owned(), + root_turn_id: Some("root-2".to_owned()), }) .await?; assert_eq!(first, "low"); assert_eq!(second, "high"); + // Reuse the same socket after a nested owner, now with unknown root lineage. + assert_eq!(sampler.sample(sample_request("turn-3")).await?, "low"); let mut requests = server.single_connection(); assert_eq!(requests.len(), 1); assert_eq!( @@ -475,8 +501,13 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications Some("Bearer refreshed".to_owned()) ); requests.extend(refreshed.single_connection()); - assert_eq!(requests.len(), 2); - let thread_id = assert_connection_metadata(&server)?; + assert_eq!(requests.len(), 3); + let thread_id = assert_connection_metadata(&server, &[("turn-1", Some("turn-1"))])?; + assert_connection_metadata(&refreshed, &[("turn-2", Some("root-2")), ("turn-3", None)])?; + assert_ne!( + requests[1].body_json()["client_metadata"]["turn_id"], + requests[2].body_json()["client_metadata"]["turn_id"] + ); assert_ne!( Some(thread_id), idle_server.single_handshake().header("thread-id") @@ -496,12 +527,8 @@ async fn preconnected_sampler_reuses_authenticated_websocket_for_classifications assert_eq!(request["tool_choice"], "none"); assert!(request.get("text").is_none()); assert_eq!(request["prompt_cache_key"], "guardian-v2:thread-1"); - assert_eq!( - request["client_metadata"]["turn_id"], - format!("turn-{}", index + 1) - ); assert!(request.get("tools").is_none()); - let effort = if index == 0 { "none" } else { "medium" }; + let effort = if index == 1 { "medium" } else { "none" }; assert_eq!(request["reasoning"]["effort"], effort); assert_eq!(request["reasoning"]["context"], "all_turns"); } @@ -631,7 +658,8 @@ async fn sampler_returns_classification_token_before_terminal_response_events() parent_compaction: None, parent_compaction_hash: None, reasoning_effort: ReasoningEffort::None, - turn_id: "turn-1".to_owned(), + parent_turn_id: "turn-1".to_owned(), + root_turn_id: Some("turn-1".to_owned()), }), ) .await??; @@ -744,9 +772,13 @@ async fn sampler_grows_its_pool_for_overlapping_requests() -> Result<()> { )) .await?; + let mut first_request = sample_request("turn-1"); + first_request.root_turn_id = Some("root-1".to_owned()); + let mut second_request = sample_request("turn-2"); + second_request.root_turn_id = Some("root-2".to_owned()); let outputs = tokio::try_join!( - sampler.sample(sample_request("turn-1")), - sampler.sample(sample_request("turn-2")), + sampler.sample(first_request), + sampler.sample(second_request), sampler.sample(sample_request("turn-3")), )?; @@ -756,18 +788,26 @@ async fn sampler_grows_its_pool_for_overlapping_requests() -> Result<()> { ); let mut thread_ids = HashSet::new(); let mut turn_ids = HashSet::new(); + let mut parent_turn_ids = HashSet::new(); for server in [&first, &second, &third] { assert_eq!(server.single_connection().len(), 1); - assert!(thread_ids.insert(assert_connection_metadata(server)?)); - turn_ids.insert( - server.single_connection()[0].body_json()["client_metadata"]["turn_id"] - .as_str() - .expect("turn ID") - .to_owned(), - ); + let metadata = server.single_connection()[0].body_json()["client_metadata"].clone(); + let parent_turn_id = metadata["parent_turn_id"].as_str().expect("owning turn ID"); + let expected_root = match parent_turn_id { + "turn-1" => Some("root-1"), + "turn-2" => Some("root-2"), + "turn-3" => None, + other => panic!("unexpected owning turn: {other}"), + }; + assert!(thread_ids.insert(assert_connection_metadata( + server, + &[(parent_turn_id, expected_root)] + )?)); + assert!(turn_ids.insert(metadata["turn_id"].as_str().expect("turn ID").to_owned())); + parent_turn_ids.insert(parent_turn_id.to_owned()); } assert_eq!( - turn_ids, + parent_turn_ids, HashSet::from_iter([ "turn-1".to_owned(), "turn-2".to_owned(), @@ -907,6 +947,7 @@ async fn sampler_retries_expired_websockets_on_another_warm_connection() -> Resu let mut request = sample_request("turn-1"); request.trusted_review_evidence = vec!["trusted review".to_owned()]; request.trusted_skill_paths = vec!["/skills/review/SKILL.md".to_owned()]; + request.root_turn_id = Some("root-turn".to_owned()); let output = sampler.sample(request).await?; assert_eq!(output, "low"); @@ -915,13 +956,16 @@ async fn sampler_retries_expired_websockets_on_another_warm_connection() -> Resu assert_eq!(expired_requests.len(), 1); assert_eq!(healthy_requests.len(), 1); assert_ne!( - assert_connection_metadata(&expired)?, - assert_connection_metadata(&healthy)? + assert_connection_metadata(&expired, &[("turn-1", Some("root-turn"))])?, + assert_connection_metadata(&healthy, &[("turn-1", Some("root-turn"))])? ); let mut expired_request = expired_requests[0].body_json(); let mut healthy_request = healthy_requests[0].body_json(); + assert_eq!( + expired_request["client_metadata"]["turn_id"], + healthy_request["client_metadata"]["turn_id"], + ); for request in [&mut expired_request, &mut healthy_request] { - assert_eq!(request["client_metadata"]["turn_id"], "turn-1"); request .as_object_mut() .expect("request object") @@ -969,10 +1013,11 @@ async fn sampler_assigns_a_fresh_identity_when_replacing_aged_connections() -> R } assert_eq!(sampler.sample(sample_request("turn-2")).await?, "low"); - let thread_ids = [&first, &second, &replacement] - .into_iter() - .map(assert_connection_metadata) - .collect::>>()?; + let thread_ids = HashSet::from([ + assert_connection_metadata(&first, &[])?, + assert_connection_metadata(&second, &[("turn-1", None)])?, + assert_connection_metadata(&replacement, &[("turn-2", None)])?, + ]); assert_eq!(thread_ids.len(), 3); assert_eq!(second.single_connection().len(), 1); assert_eq!(replacement.single_connection().len(), 1);