Normalize Bedrock Mantle commentary before replay (#62095)

## Why

Bedrock Mantle's reasoning prefix omits empty `annotations` on assistant commentary. Replayed commentary needs to preserve that form to keep the prefix stable.

## What changed

Add a provider hook to normalize completed output items before they enter model history. For Mantle, omit empty `annotations` from assistant commentary text while preserving nonempty annotations, final answers, and Bedrock Runtime output.

## Testing

Add regression coverage for Mantle and Runtime that checks commentary replay across a tool continuation and a subsequent turn, including reasoning and tool item ordering and preservation of final-answer content.

GitOrigin-RevId: ecd5b9b5dca8c24956a6379c9d0fb643e4427af3
This commit is contained in:
Adnan Hajar
2026-11-17 18:55:30 +00:00
committed by copyberry
parent 04cfdee311
commit cb64621bf3
4 changed files with 122 additions and 0 deletions
+1
View File
@@ -2962,6 +2962,7 @@ where
};
let event = match event {
Ok(ResponseEvent::OutputItemDone(mut item)) => {
provider.normalize_completed_output_item(&mut item);
// Assign IDs after interceptors, which may replace provider items.
// Reuse the added event's ID so tracing and the harness agree.
if item.id().is_none_or(|id| id.is_empty()) {
+92
View File
@@ -75,6 +75,7 @@ use core_test_support::responses::ev_completed_with_tokens;
use core_test_support::responses::ev_function_call;
use core_test_support::responses::ev_message_item_added;
use core_test_support::responses::ev_output_text_delta;
use core_test_support::responses::ev_reasoning_item;
use core_test_support::responses::ev_response_created;
use core_test_support::responses::mount_sse_once;
use core_test_support::responses::mount_sse_once_match;
@@ -260,6 +261,97 @@ async fn responses_request_uses_implicit_default_tier_for_bedrock() -> anyhow::R
Ok(())
}
#[test_case::test_case(AMAZON_BEDROCK_PROVIDER_ID; "mantle")]
#[test_case::test_case(AMAZON_BEDROCK_RUNTIME_PROVIDER_ID; "runtime")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn bedrock_function_continuation_replays_assistant_commentary(
provider_id: &'static str,
) -> anyhow::Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
let mut commentary = ev_assistant_message("msg_progress", "Checking.");
commentary["item"]["phase"] = json!("commentary");
commentary["item"]["content"] = json!([
{"type": "output_text", "text": "Checking.", "annotations": [], "logprobs": []},
{"type": "output_text", "text": "Source", "annotations": [{
"type": "url_citation", "url": "https://example.org", "title": "Source",
"start_index": 0, "end_index": 6
}]}
]);
let mut expected_content = commentary["item"]["content"].clone();
if provider_id == AMAZON_BEDROCK_PROVIDER_ID {
expected_content[0]
.as_object_mut()
.expect("output text part")
.remove("annotations");
}
let reasoning = ev_reasoning_item("rs_plan", &[], &[]);
let mut answer = ev_assistant_message("msg_answer", "Checked.");
answer["item"]["phase"] = json!("final_answer");
answer["item"]["content"][0]["annotations"] = json!([]);
let mock = mount_sse_sequence(
&server,
vec![
sse(vec![
commentary,
reasoning.clone(),
ev_function_call(
"plan",
"update_plan",
r#"{"plan":[{"step":"Check","status":"completed"}]}"#,
),
ev_completed("tool"),
]),
sse(vec![answer.clone(), ev_completed("answer")]),
sse(vec![ev_completed("next")]),
],
)
.await;
let test = test_codex()
.with_auth(CodexAuth::BedrockApiKey(BedrockApiKeyAuth {
api_key: "dummy".to_string(),
region: "us-east-1".to_string(),
}))
.with_config(move |config| {
let base_url = config.model_provider.base_url.clone();
config.model_provider_id = provider_id.to_string();
config.model_provider = built_in_model_providers(/*openai_base_url*/ None)
.remove(provider_id)
.expect("built-in Bedrock provider");
config.model_provider.base_url = base_url;
config.model_provider.supports_websockets = false;
config.update_plan_enabled = true;
})
.build_with_auto_env(&server)
.await?;
test.submit_text_turn("Check the plan.").await?;
test.submit_text_turn("Continue.").await?;
let requests = mock.requests();
assert_eq!(requests.len(), 3);
for request in &requests[1..] {
let input = request.input();
let progress = input
.iter()
.position(|item| item["id"] == "msg_progress")
.expect("replayed commentary");
assert_eq!(input[progress]["content"], expected_content);
assert_eq!(
input[progress + 1]["encrypted_content"],
reasoning["item"]["encrypted_content"]
);
assert_eq!(input[progress + 2]["type"], "function_call");
assert_eq!(input[progress + 3]["type"], "function_call_output");
}
let input = requests[2].input();
let replayed_answer = input
.iter()
.find(|item| item["id"] == "msg_answer")
.expect("replayed final answer");
assert_eq!(replayed_answer["content"], answer["item"]["content"]);
Ok(())
}
fn rollout_response_item(item: ResponseItem) -> RolloutItem {
RolloutItem::ResponseItem(item.into())
}
@@ -28,6 +28,9 @@ use codex_models_manager::manager::StaticModelsManager;
use codex_protocol::account::ProviderAccount;
use codex_protocol::error::CodexErr;
use codex_protocol::error::Result;
use codex_protocol::models::ContentItem;
use codex_protocol::models::MessagePhase;
use codex_protocol::models::ResponseItem;
use codex_protocol::openai_models::ModelsResponse;
use crate::ProviderCapabilities;
@@ -277,6 +280,28 @@ impl ModelProvider for AmazonBedrockModelProvider {
&self.info
}
fn normalize_completed_output_item(&self, item: &mut ResponseItem) {
// Mantle's reasoning prefix omits empty annotations on commentary.
// Capture that form before the first replay so the prefix stays stable.
if self.endpoint == BedrockEndpoint::Mantle
&& let ResponseItem::Message {
role,
phase: Some(MessagePhase::Commentary),
content,
..
} = item
&& role == "assistant"
{
for part in content {
if let ContentItem::OutputText { annotations, .. } = part
&& annotations.as_ref().is_some_and(Vec::is_empty)
{
*annotations = None;
}
}
}
}
fn capabilities(&self) -> ProviderCapabilities {
ProviderCapabilities {
reasoning_summaries: true,
+4
View File
@@ -25,6 +25,7 @@ use codex_models_manager::manager::StaticModelsManager;
use codex_protocol::account::ProviderAccount;
use codex_protocol::auth::RefreshTokenFailedError;
use codex_protocol::error::CodexErr;
use codex_protocol::models::ResponseItem;
use codex_protocol::openai_models::ModelsResponse;
use codex_runtime_agent_instance::RuntimeAuthManager;
@@ -221,6 +222,9 @@ pub trait ModelProvider: fmt::Debug + Send + Sync {
codex_api::map_api_error(error)
}
/// Normalizes a completed output item before it enters model history.
fn normalize_completed_output_item(&self, _item: &mut ResponseItem) {}
/// Returns provider configuration adapted for the API client.
fn api_provider(&self) -> ModelProviderFuture<'_, codex_protocol::error::Result<Provider>> {
Box::pin(async move {