Add recovery guidance to content-filter retries (#49119)

## Why

Responses stopped by `content_filter` previously followed generic stream-error retry handling without guidance about the block. Retried requests need instructions to explain the limitation and offer a permitted alternative.

## What changed

- Recognize content-filter failures as a distinct error while preserving retryability and the existing public error representation.
- Append developer guidance to the conversation after each sampling block, retaining delivered output and using the existing retry handling.
- Support a model catalog override through `model_messages.content_filter_guidance`. Missing, null, blank, or values exceeding 512 UTF-8 bytes fall back to bundled guidance.
- The bundled guidance tells the model to avoid reproducing or working around blocked content, briefly explain the limitation, offer a permitted alternative, and continue unrelated authorized work.

## Testing

Add a scenario and request-history snapshot covering repeated blocks, catalog guidance on each retry, retained partial output, and eventual completion. Add error-mapping assertions for retryability, public error classification, and error text.

GitOrigin-RevId: ba0c201b3a5a3acf5cfa971332027240e0690225
This commit is contained in:
Won Park
2026-09-29 01:14:41 +00:00
committed by copyberry
parent 6c49240565
commit 8bd5a136ff
23 changed files with 225 additions and 13 deletions
@@ -31,6 +31,7 @@ async fn catalog_permission_message_loaded_from_remote_models_is_sent() -> Resul
let model_slug = "remote-catalog-permissions-model";
let mut model = model_info_from_slug(model_slug);
model.model_messages = Some(ModelMessages {
content_filter_guidance: None,
persistent_instructions: None,
tools: None,
instructions_template: None,
@@ -74,6 +74,7 @@ fn model_with_collaboration_messages(
) -> codex_protocol::openai_models::ModelInfo {
let mut model = model_info_from_slug(slug);
let model_messages = model.model_messages.get_or_insert(ModelMessages {
content_filter_guidance: None,
persistent_instructions: None,
tools: None,
instructions_template: None,
@@ -643,6 +643,7 @@ fn test_remote_model(slug: &str, priority: i32) -> ModelInfo {
available_access_programs: None,
upgrade: None,
model_messages: Some(ModelMessages {
content_filter_guidance: None,
persistent_instructions: None,
tools: None,
instructions_template: Some("base instructions".to_string()),
+3
View File
@@ -118,6 +118,9 @@ mod guardian_heartbeat;
#[path = "scenarios_preparation.rs"]
mod preparation;
#[path = "scenarios_content_filter.rs"]
mod content_filter;
#[path = "scenarios_shared_instructions.rs"]
mod shared_instructions;
@@ -0,0 +1,72 @@
//! Content-filter retries retain delivered output and append catalog guidance after each block.
use super::*;
use pretty_assertions::assert_eq;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn content_filter_guidance_is_appended_after_each_block() -> Result<()> {
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
let test = test_codex()
.with_config(|config| {
configure_scenario_catalog(config);
config.model_provider.stream_max_retries = Some(2);
})
.with_model_info_override("gpt-5.5", |model| {
model
.model_messages
.get_or_insert_default()
.content_filter_guidance = Some(
"Your previous response was blocked. Offer a permitted alternative.".to_string(),
);
})
.build_with_auto_env(&server)
.await?;
let blocked = json!({
"type": "response.incomplete",
"response": { "incomplete_details": { "reason": "content_filter" } }
});
let mock = mount_sse_sequence(
&server,
vec![
sse(vec![
ev_response_created("blocked-response"),
ev_assistant_message("partial-message", "I can help with this topic."),
blocked.clone(),
]),
sse(vec![ev_response_created("blocked-again-response"), blocked]),
sse(vec![
ev_response_created("recovery-response"),
ev_assistant_message(
"alternative-message",
"I can help with a permitted alternative.",
),
ev_completed("recovery-response"),
]),
],
)
.await;
test.codex
.start_or_steer_turn(TurnInputRequest::user_input(vec![UserInput::Text {
text: "Help me with this topic.".to_string(),
text_elements: Vec::new(),
}]))
.await?;
wait_for_event(&test.codex, |event| {
assert!(!matches!(event, EventMsg::Error(_)), "{event:?}");
matches!(event, EventMsg::TurnComplete(_))
})
.await;
let requests = mock.requests();
assert_eq!(requests.len(), 3);
insta::assert_snapshot!(
"content_filter_guidance",
context_snapshot::format_request_history_snapshot(
"Repeated content-filter blocks append catalog guidance and retain delivered output.",
&requests,
&ContextSnapshotOptions::default().rewrite_known_segments(),
)
);
Ok(())
}
@@ -0,0 +1,26 @@
---
source: core/tests/suite/scenarios_content_filter.rs
expression: "context_snapshot::format_request_history_snapshot(\"Repeated content-filter blocks append catalog guidance and retain delivered output.\",\n&requests, &ContextSnapshotOptions::default().rewrite_known_segments(),)"
---
Scenario: Repeated content-filter blocks append catalog guidance and retain delivered output.
## Window 1
-- request 1 (turn) --
00:message/developer:
<PERMISSIONS_INSTRUCTIONS>
01:message/user:
<ENVIRONMENT_CONTEXT>
02:message/user:
Help me with this topic.
-- request 2 (turn) --
03:message/assistant:
I can help with this topic.
04:message/developer:
<content_filter_guidance>
Your previous response was blocked. Offer a permitted alternative.
</content_filter_guidance>
-- request 3 (turn) --
05:message/developer:
<content_filter_guidance>
Your previous response was blocked. Offer a permitted alternative.
</content_filter_guidance>