diff --git a/backend/src/dispatch/controllers/history.rs b/backend/src/dispatch/controllers/history.rs index e2f640de9..bf6804c14 100644 --- a/backend/src/dispatch/controllers/history.rs +++ b/backend/src/dispatch/controllers/history.rs @@ -92,20 +92,14 @@ pub(super) struct PreparedHistoryRequest { pub(in crate::dispatch) struct HistoryController; pub(in crate::dispatch) fn sanitize_cross_account_input(request: &mut CodexResponsesRequest) { - if !request.input().iter().any(is_known_response_output_item) { + if request.input().is_empty() { return; } let input = request .input() .iter() .cloned() - .filter_map(|item| { - if is_known_response_output_item(&item) { - sanitize_cross_account_output(item) - } else { - Some(item) - } - }) + .filter_map(sanitize_cross_account_item) .collect(); request.set_input(input); } @@ -501,7 +495,7 @@ fn transcript_for_turn( account_id: &str, ) -> LocalReplayTranscript { let mut transcript = LocalReplayTranscript::default(); - append_client_input(&mut transcript, input, account_id); + append_client_input(&mut transcript, input); append_account_output(&mut transcript, output, account_id); transcript } @@ -513,33 +507,14 @@ fn append_turn( account_id: &str, ) { project_transcript_to_account(transcript, account_id); - append_client_input(transcript, input, account_id); + append_client_input(transcript, input); append_account_output(transcript, output, account_id); } -fn append_client_input( - transcript: &mut LocalReplayTranscript, - input: Vec, - account_id: &str, -) { - transcript.items.extend(input.into_iter().map(|item| { - if is_known_response_output_item(&item) { - if has_account_bound_output_state(&item) { - LocalReplayItem::AccountOutput { - account_id: account_id.to_string(), - item, - } - } else { - LocalReplayItem::SanitizedOutput(item) - } - } else { - LocalReplayItem::ClientInput(item) - } - })); -} - -fn has_account_bound_output_state(item: &Value) -> bool { - item.get("id").is_some() || item.get("encrypted_content").is_some() +fn append_client_input(transcript: &mut LocalReplayTranscript, input: Vec) { + transcript + .items + .extend(input.into_iter().map(LocalReplayItem::ClientInput)); } fn append_account_output( @@ -566,7 +541,7 @@ fn project_transcript_to_account(transcript: &mut LocalReplayTranscript, account account_id: owner, item, } if owner != account_id => { - sanitize_cross_account_output(item).map(LocalReplayItem::SanitizedOutput) + sanitize_cross_account_item(item).map(LocalReplayItem::SanitizedOutput) } item => Some(item), }) @@ -586,7 +561,7 @@ fn replay_input_for_account(transcript: &LocalReplayTranscript, account_id: &str item, } if owner == account_id => Some(without_output_id(item.clone())), LocalReplayItem::AccountOutput { item, .. } => { - sanitize_cross_account_output(item.clone()) + sanitize_cross_account_item(item.clone()) } }) .collect() @@ -599,7 +574,7 @@ fn without_output_id(mut item: Value) -> Value { item } -fn sanitize_cross_account_output(mut item: Value) -> Option { +fn sanitize_cross_account_item(mut item: Value) -> Option { if let Value::Object(object) = &mut item { if matches!( object.get("type").and_then(Value::as_str), @@ -613,35 +588,6 @@ fn sanitize_cross_account_output(mut item: Value) -> Option { Some(item) } -fn is_known_response_output_item(item: &Value) -> bool { - let item_type = item.get("type").and_then(Value::as_str); - if item_type == Some("message") { - return item.get("role").and_then(Value::as_str) == Some("assistant"); - } - matches!( - item_type, - Some( - "agent_message" - | "reasoning" - | "local_shell_call" - | "function_call" - | "tool_search_call" - | "custom_tool_call" - | "computer_call" - | "web_search_call" - | "file_search_call" - | "code_interpreter_call" - | "image_generation_call" - | "mcp_call" - | "mcp_list_tools" - | "mcp_approval_request" - | "compaction" - | "compaction_summary" - | "context_compaction" - ) - ) -} - fn is_continuation_busy(facts: &UpstreamFailureFacts) -> bool { matches!( facts.kind, diff --git a/backend/src/dispatch/lifecycle/attempt.rs b/backend/src/dispatch/lifecycle/attempt.rs index eeff75de9..e86d58970 100644 --- a/backend/src/dispatch/lifecycle/attempt.rs +++ b/backend/src/dispatch/lifecycle/attempt.rs @@ -650,6 +650,7 @@ impl<'a> AttemptRunner<'a> { PendingAttempt::StreamResponse(stream) => { let PendingStreamResponse { account, + attempt_request, attempt, lease, response, @@ -660,6 +661,7 @@ impl<'a> AttemptRunner<'a> { } = *stream; EstablishedResponse::Stream(Box::new(EstablishedStream { context: self.take_established_context(account, attempt)?, + attempt_request, lease, response, decoder, diff --git a/backend/src/dispatch/lifecycle/contract.rs b/backend/src/dispatch/lifecycle/contract.rs index 87ceb0bfc..c12b4e853 100644 --- a/backend/src/dispatch/lifecycle/contract.rs +++ b/backend/src/dispatch/lifecycle/contract.rs @@ -173,6 +173,7 @@ impl TryFrom for EstablishedCompleteBody { pub(in crate::dispatch) struct EstablishedStream { pub context: EstablishedAttemptContext, + pub attempt_request: CodexResponsesRequest, pub lease: AccountLease, pub response: CodexBackendStreamingResponse, pub decoder: CanonicalStreamDecoder, diff --git a/backend/src/dispatch/lifecycle/finalizer.rs b/backend/src/dispatch/lifecycle/finalizer.rs index 69bc0a2f0..b37663765 100644 --- a/backend/src/dispatch/lifecycle/finalizer.rs +++ b/backend/src/dispatch/lifecycle/finalizer.rs @@ -27,6 +27,8 @@ pub(in crate::dispatch) struct StreamFinalizer { controllers: ControllerSet, controller_scope: ControllerRequestScope, controller_context: StreamControllerContext, + request_input: Vec, + continued_from_previous_response: bool, account_lease: AccountLease, } @@ -35,12 +37,16 @@ impl StreamFinalizer { controllers: ControllerSet, controller_scope: ControllerRequestScope, controller_context: StreamControllerContext, + request_input: Vec, + continued_from_previous_response: bool, account_lease: AccountLease, ) -> Self { Self { controllers, controller_scope, controller_context, + request_input, + continued_from_previous_response, account_lease, } } @@ -58,14 +64,11 @@ impl StreamFinalizer { } pub(in crate::dispatch) fn request_input(&self) -> &[serde_json::Value] { - self.controller_context.request.input() + &self.request_input } pub(in crate::dispatch) fn continued_from_previous_response(&self) -> bool { - self.controller_context - .request - .previous_response_id() - .is_some() + self.continued_from_previous_response } pub(in crate::dispatch) async fn finalize( @@ -79,6 +82,7 @@ impl StreamFinalizer { controller_scope, controller_context, account_lease, + .. } = self; let body = String::from_utf8_lossy(&summary.body).into_owned(); controllers diff --git a/backend/src/dispatch/stream/lifecycle.rs b/backend/src/dispatch/stream/lifecycle.rs index 8f1addee5..56cc25d3b 100644 --- a/backend/src/dispatch/stream/lifecycle.rs +++ b/backend/src/dispatch/stream/lifecycle.rs @@ -141,6 +141,7 @@ impl ResponseDispatchService { let EstablishedStream { context, + attempt_request, lease, response, decoder, @@ -159,6 +160,8 @@ impl ResponseDispatchService { .observe_models_etag(response.response_metadata.models_etag.as_deref()); let attempts = trace.attempts().to_vec(); let transport = response.transport; + let request_input = attempt_request.input().to_vec(); + let continued_from_previous_response = attempt_request.previous_response_id().is_some(); let finalizer = StreamFinalizer::new( controllers, controller_scope, @@ -185,6 +188,8 @@ impl ResponseDispatchService { attempts, started_at, }, + request_input, + continued_from_previous_response, lease, ); Ok(spawn_live_response_stream( diff --git a/backend/tests/api/client/responses/websocket.rs b/backend/tests/api/client/responses/websocket.rs index 3fc405d28..3f927a69e 100644 --- a/backend/tests/api/client/responses/websocket.rs +++ b/backend/tests/api/client/responses/websocket.rs @@ -45,7 +45,7 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() { for forbidden in [ "ConnectionReplayState", "PendingReplayUpdate", - "sanitize_cross_account_output", + "sanitize_cross_account_item", ".previous_response_id()", "dispatch::controllers", ] { @@ -61,7 +61,7 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() { "ConnectionReplayUpdate::Unavailable", "struct CrossAccountReplay", "LocalReplayItem::ClientInput", - "sanitize_cross_account_output", + "sanitize_cross_account_item", "project_transcript_to_account", "snapshot.last_response_id.as_deref() == Some(previous_response_id)", ] { @@ -83,7 +83,7 @@ fn responses_websocket_replay_policy_should_be_owned_by_history_controller() { ); } assert!(protocol.contains("enum LocalReplayItem")); - assert!(!protocol.contains("sanitize_cross_account_output")); + assert!(!protocol.contains("sanitize_cross_account_item")); } #[tokio::test] @@ -553,6 +553,15 @@ async fn responses_websocket_cross_account_replay_should_isolate_account_bound_s "text": "client replayed summary A" }] })); + first_payload["input"].as_array_mut().unwrap().push(json!({ + "type": "future_output_item", + "id": "future_output_id_a", + "encrypted_content": "future_output_encrypted_a", + "payload": { + "id": "future_nested_id", + "encrypted_content": "future_nested_secret" + } + })); first_payload["turnState"] = json!("turn_state_account_a"); first_payload["x-codex-installation-id"] = json!("client_installation_a"); first_payload["client_metadata"] = json!({ @@ -749,12 +758,9 @@ async fn responses_websocket_cross_account_replay_should_isolate_account_bound_s } let replay_input = cross_account_replay["input"].as_array().unwrap(); - assert_eq!(replay_input.len(), 6); - assert_eq!(replay_input[0]["id"], "client_input_id"); - assert_eq!( - replay_input[0]["encrypted_content"], - "client_semantic_secret" - ); + assert_eq!(replay_input.len(), 7); + assert!(replay_input[0].get("id").is_none()); + assert!(replay_input[0].get("encrypted_content").is_none()); assert_eq!( replay_input[0]["tool_arguments"]["id"], "client_tool_argument_id" @@ -767,18 +773,26 @@ async fn responses_websocket_cross_account_replay_should_isolate_account_bound_s ); assert!(replay_input[2].get("id").is_none()); assert!(replay_input[2].get("encrypted_content").is_none()); - assert_eq!(replay_input[2]["content"][0]["id"], "nested_content_id_a"); + assert_eq!(replay_input[2]["type"], "future_output_item"); + assert_eq!(replay_input[2]["payload"]["id"], "future_nested_id"); + assert_eq!( + replay_input[2]["payload"]["encrypted_content"], + "future_nested_secret" + ); assert!(replay_input[3].get("id").is_none()); assert!(replay_input[3].get("encrypted_content").is_none()); - assert_eq!(replay_input[3]["summary"][0]["id"], "nested_summary_id_a"); + assert_eq!(replay_input[3]["content"][0]["id"], "nested_content_id_a"); assert!(replay_input[4].get("id").is_none()); - assert_eq!(replay_input[4]["call_id"], "call_account_a"); - assert_eq!(replay_input[4]["caller"], "client"); + assert!(replay_input[4].get("encrypted_content").is_none()); + assert_eq!(replay_input[4]["summary"][0]["id"], "nested_summary_id_a"); + assert!(replay_input[5].get("id").is_none()); + assert_eq!(replay_input[5]["call_id"], "call_account_a"); + assert_eq!(replay_input[5]["caller"], "client"); assert_eq!( - replay_input[4]["arguments"], + replay_input[5]["arguments"], "{\"id\":\"tool_argument_id_a\"}" ); - assert_eq!(replay_input[5]["content"], "second"); + assert_eq!(replay_input[6]["content"], "second"); let cross_account_json = cross_account_replay.to_string(); for forbidden in [ "msg_account_a", @@ -790,6 +804,8 @@ async fn responses_websocket_cross_account_replay_should_isolate_account_bound_s "encrypted_compaction_a", "client_replayed_reasoning_id_a", "client_replayed_reasoning_encrypted_a", + "future_output_id_a", + "future_output_encrypted_a", ] { assert!( !cross_account_json.contains(forbidden), @@ -804,9 +820,15 @@ async fn responses_websocket_cross_account_replay_should_isolate_account_bound_s assert_eq!(continued_secondary["input"][0]["content"], "third"); assert!(same_account_replay.get("previous_response_id").is_none()); let same_account_json = same_account_replay.to_string(); + assert!(!same_account_json.contains("client_input_id")); + assert!(!same_account_json.contains("client_semantic_secret")); assert!(!same_account_json.contains("encrypted_message_a")); assert!(!same_account_json.contains("encrypted_reasoning_a")); assert!(!same_account_json.contains("client_replayed_reasoning_encrypted_a")); + assert!(!same_account_json.contains("future_output_id_a")); + assert!(!same_account_json.contains("future_output_encrypted_a")); + assert!(same_account_json.contains("future_nested_id")); + assert!(same_account_json.contains("future_nested_secret")); assert!(same_account_json.contains("encrypted_message_b")); assert!(same_account_json.contains("encrypted_reasoning_b")); assert!(!same_account_json.contains("msg_account_b")); diff --git a/backend/tests/dispatch/service/responses_recovery.rs b/backend/tests/dispatch/service/responses_recovery.rs index 89ec2e56b..4c07627e0 100644 --- a/backend/tests/dispatch/service/responses_recovery.rs +++ b/backend/tests/dispatch/service/responses_recovery.rs @@ -2722,9 +2722,15 @@ async fn responses_failover_should_replace_account_identity_and_preserve_session assert!(secondary.headers.get("x-codex-turn-state").is_none()); assert_eq!(secondary_metadata["safe"], "preserved"); assert_eq!( - secondary_body["input"][0], primary_body["input"][0], - "ordinary client input must remain verbatim" + secondary_body["input"][0]["role"], + primary_body["input"][0]["role"] ); + assert_eq!( + secondary_body["input"][0]["content"], + primary_body["input"][0]["content"] + ); + assert_eq!(primary_body["input"][0]["id"], "client_user_message_id"); + assert!(secondary_body["input"][0].get("id").is_none()); assert_eq!( secondary_body["input"][0]["tool_arguments"]["id"], "client_tool_argument_id"