mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
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:
@@ -0,0 +1,30 @@
|
||||
//! Bounded recovery guidance for a response blocked by an unspecified content filter.
|
||||
|
||||
use super::ContextualUserFragment;
|
||||
use codex_protocol::models::ContentItemKind;
|
||||
|
||||
pub(crate) struct ContentFilterGuidance {
|
||||
pub(crate) text: String,
|
||||
}
|
||||
|
||||
impl ContextualUserFragment for ContentFilterGuidance {
|
||||
fn content_kind(&self) -> ContentItemKind {
|
||||
ContentItemKind("generic.content_filter_guidance".to_string())
|
||||
}
|
||||
|
||||
fn role(&self) -> &'static str {
|
||||
"developer"
|
||||
}
|
||||
|
||||
fn markers(&self) -> (&'static str, &'static str) {
|
||||
Self::type_markers()
|
||||
}
|
||||
|
||||
fn type_markers() -> (&'static str, &'static str) {
|
||||
("<content_filter_guidance>", "</content_filter_guidance>")
|
||||
}
|
||||
|
||||
fn body(&self) -> String {
|
||||
format!("\n{}\n", self.text)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ mod apps_instructions;
|
||||
mod available_plugins_instructions;
|
||||
mod base_instructions;
|
||||
mod compaction_summary;
|
||||
mod content_filter_guidance;
|
||||
mod contextual_user_message;
|
||||
mod current_time_reminder;
|
||||
mod developer_instructions;
|
||||
@@ -69,6 +70,7 @@ pub(crate) use codex_guardian_context::PermissionContext as GuardianPermissionCo
|
||||
pub(crate) use codex_prompts::MultiAgentRoleInstructions;
|
||||
pub use codex_prompts::PermissionsInstructions;
|
||||
pub(crate) use compaction_summary::CompactionSummary;
|
||||
pub(crate) use content_filter_guidance::ContentFilterGuidance;
|
||||
pub(crate) use contextual_user_message::is_contextual_user_fragment;
|
||||
pub(crate) use contextual_user_message::is_guardian_context_message;
|
||||
pub(crate) use contextual_user_message::is_user_authorization_message;
|
||||
|
||||
@@ -11,6 +11,7 @@ use crate::compact::InitialContextInjection;
|
||||
use crate::compact::run_inline_auto_compact_task;
|
||||
use crate::compact_remote_v2::run_inline_remote_auto_compact_task as run_inline_remote_auto_compact_task_v2;
|
||||
use crate::connectors;
|
||||
use crate::context::ContentFilterGuidance;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::UserVerificationNotice;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
@@ -1697,15 +1698,33 @@ async fn run_sampling_request(
|
||||
|
||||
let original_input = original_input.get_or_insert(prompt.input);
|
||||
|
||||
let retry = handle_response_stream_error(
|
||||
&mut retry_state,
|
||||
max_retries,
|
||||
err,
|
||||
client_session,
|
||||
&sess,
|
||||
&turn_context,
|
||||
ResponsesStreamRequest::Sampling,
|
||||
)
|
||||
let retry = async {
|
||||
if matches!(err.details(), CodexErrorDetails::ContentFilter) {
|
||||
let model_info = &step_context.settings.model_info;
|
||||
let guidance = ContentFilterGuidance {
|
||||
text: codex_prompts::ResolvedModelMessages::from_model(model_info)
|
||||
.content_filter_guidance()
|
||||
.to_string(),
|
||||
};
|
||||
sess.record_conversation_items(
|
||||
&turn_context,
|
||||
model_info,
|
||||
&[ContextualUserFragment::into(guidance)],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
handle_response_stream_error(
|
||||
&mut retry_state,
|
||||
max_retries,
|
||||
err,
|
||||
client_session,
|
||||
&sess,
|
||||
&turn_context,
|
||||
ResponsesStreamRequest::Sampling,
|
||||
)
|
||||
.await
|
||||
}
|
||||
.or_cancel(&preempt)
|
||||
.or_cancel(&cancellation_token)
|
||||
.await?;
|
||||
|
||||
@@ -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()),
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
+26
@@ -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>
|
||||
Reference in New Issue
Block a user