Preserve structured output format during remote compaction (#63320)

## Why

Remote compaction v2 dropped the turn's final-answer schema, so its
request could lose the cached prompt prefix shared with sampling even
when both requests used the same cache key.

## What changed

Carry the turn's JSON schema and sampling strictness into compaction
requests, including the non-strict guardian verdict format. The Responses
API still generates compaction summaries independently of that schema.

## Testing

- Check that automatic compaction on Bedrock and GCP uses the same
  `text.format` and `prompt_cache_key` as sampling, with and without a schema.
- Check that guardian reviews retain their verdict format across compaction
  and retries, and that an approval after compaction allows the command to run.

GitOrigin-RevId: 2cb5df2539358e3d74f1dce51baac2806b6219eb
This commit is contained in:
jif
2026-11-23 12:17:37 +00:00
committed by copyberry
parent 3af06341bd
commit 9ee4539162
3 changed files with 104 additions and 9 deletions
@@ -192,8 +192,12 @@ pub(super) async fn run_remote_compact_v2_attempt(
},
parallel_tool_calls: true,
base_instructions,
output_schema: None,
output_schema_strict: true,
// Preserve the cached output-format prefix. Responses generates compaction
// summaries independently of this final-answer schema.
output_schema: turn_context.final_output_json_schema.clone(),
output_schema_strict: !crate::guardian::is_basic_session_source(
&turn_context.session_source,
),
prompt_cache_ttl: turn_context.prompt_cache_ttl,
cyber_access_program: turn_context.cyber_access_program,
});
+65 -6
View File
@@ -8,6 +8,7 @@ use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use codex_core::RequestCountry;
use codex_core::StartThreadOptions;
use codex_core::TurnInputRequest;
use codex_core::TurnStartOptions;
use codex_extension_api::ExtensionRegistryBuilder;
use codex_features::Feature;
use codex_history::CodexHarnessMetadata;
@@ -568,11 +569,14 @@ enum CompactionProvider {
Gcp,
}
#[test_case(CompactionProvider::AmazonBedrock; "bedrock")]
#[test_case(CompactionProvider::Gcp; "gcp")]
#[test_case(CompactionProvider::AmazonBedrock, false; "bedrock")]
#[test_case(CompactionProvider::Gcp, false; "gcp")]
#[test_case(CompactionProvider::AmazonBedrock, true; "bedrock with output schema")]
#[test_case(CompactionProvider::Gcp, true; "gcp with output schema")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn provider_automatic_compaction_uses_v2_responses_endpoint(
provider: CompactionProvider,
with_output_schema: bool,
) -> Result<()> {
skip_if_no_network!(Ok(()));
@@ -625,7 +629,10 @@ async fn provider_automatic_compaction_uses_v2_responses_endpoint(
&server,
vec![
sse(vec![
responses::ev_assistant_message("message-1", "before automatic compaction"),
responses::ev_assistant_message(
"message-1",
r#"{"result":"before automatic compaction"}"#,
),
responses::ev_completed_with_tokens("response-1", /*total_tokens*/ 500),
]),
sse(vec![
@@ -639,18 +646,70 @@ async fn provider_automatic_compaction_uses_v2_responses_endpoint(
responses::ev_completed("response-compact"),
]),
sse(vec![
responses::ev_assistant_message("message-2", "after automatic compaction"),
responses::ev_assistant_message(
"message-2",
r#"{"result":"after automatic compaction"}"#,
),
responses::ev_completed("response-2"),
]),
],
)
.await;
test.submit_turn("before automatic compact").await?;
test.submit_turn("after automatic compact").await?;
let output_schema = with_output_schema.then(|| {
json!({
"type": "object",
"properties": {"result": {"type": "string"}},
"required": ["result"],
"additionalProperties": false,
})
});
for text in ["before automatic compact", "after automatic compact"] {
test.codex
.start_or_steer_turn(
TurnInputRequest::user_input(vec![UserInput::Text {
text: text.to_string(),
text_elements: Vec::new(),
}])
.on_start(TurnStartOptions {
final_output_json_schema: output_schema.clone(),
..Default::default()
}),
)
.await?;
wait_for_turn_complete(&test.codex).await;
}
let response_requests = response_mock.requests();
assert_eq!(response_requests.len(), 3);
let expected_format = output_schema.map_or(Value::Null, |schema| {
json!({
"type": "json_schema",
"name": "codex_output_schema",
"strict": true,
"schema": schema,
})
});
assert_eq!(
response_requests
.iter()
.map(|request| request.body_json()["text"]["format"].clone())
.collect::<Vec<_>>(),
vec![expected_format; 3],
"sampling and compaction must use the same output format"
);
let cache_key = response_requests[0].body_json()["prompt_cache_key"]
.as_str()
.expect("session cache key")
.to_string();
assert_eq!(
response_requests
.iter()
.map(|request| request.body_json()["prompt_cache_key"].clone())
.collect::<Vec<_>>(),
vec![json!(cache_key); 3],
"compaction must preserve the session cache key"
);
assert!(
response_requests
.iter()
+33 -1
View File
@@ -699,7 +699,7 @@ async fn guardian_review_recovers_after_compaction(
.await;
}
let command = json!({
"cmd": "true",
"cmd": "printf POST_COMPACTION_APPROVED",
"sandbox_permissions": "require_escalated",
"justification": "Read the internal samples the user authorized.",
});
@@ -782,6 +782,20 @@ async fn guardian_review_recovers_after_compaction(
})
.collect::<Vec<_>>();
assert_eq!(guardian_requests.len(), 3);
let verdict_format = json!({
"type": "json_schema",
"name": "codex_output_schema",
"strict": false,
"schema": codex_guardian_reviewer::guardian_output_schema(),
});
assert_eq!(
guardian_requests
.iter()
.map(|request| request.body_json()["text"]["format"].clone())
.collect::<Vec<_>>(),
vec![verdict_format.clone(); guardian_requests.len()],
"reviews before and after compaction must retain the verdict format"
);
assert_eq!(
guardian_requests[0].body_json()["client_metadata"]["thread_id"]
== guardian_requests[1].body_json()["client_metadata"]["thread_id"],
@@ -804,6 +818,14 @@ async fn guardian_review_recovers_after_compaction(
compact_requests.len(),
usize::from(!with_parent_checkpoint) + compaction_errors.len(),
);
assert_eq!(
compact_requests
.iter()
.map(|request| request.body_json()["text"]["format"].clone())
.collect::<Vec<_>>(),
vec![verdict_format; compact_requests.len()],
"compaction and retries must preserve the verdict format"
);
if let Some(compact_request) = compact_requests.first() {
assert!(
compact_request
@@ -882,6 +904,16 @@ async fn guardian_review_recovers_after_compaction(
}
}
assert!(
requests
.last()
.expect("parent continuation after the reviews")
.function_call_output("exec-second")
.to_string()
.contains("POST_COMPACTION_APPROVED"),
"the verdict after compaction must approve execution"
);
Ok(())
}