Add opt-in elastic budgeting for asynchronous Guardian requests (#61345)

## What changed

- Add `async_elastic_budgeting` and `async_elastic_sections` under `[features.guardianv2]`, independently of synchronous budgeting and disabled by default.
- Apply section limits and elastic allocation to new classifier evidence after accounting for setup and retained conversation history. Preserve the configured elastic policy when applying model defaults.
- Keep permission context whole during elastic allocation and record truncation observations when image preparation rejects an image.

## Testing

Add coverage for configuration validation, fixed and elastic conversation budgeting, retained history and cursor preservation, and complete classifier requests that retain evidence beyond the fixed entry limit.

GitOrigin-RevId: 2955f0482f7c3e18325e296fc2ccb6592ffcb73a
This commit is contained in:
ioanbudea-OAI
2026-11-13 18:46:45 +00:00
committed by copyberry
parent c33002259e
commit a655b50de9
29 changed files with 448 additions and 50 deletions
+1
View File
@@ -49,6 +49,7 @@ core_test_support = { workspace = true }
insta = { workspace = true }
pretty_assertions = { workspace = true }
tempfile = { workspace = true }
test-case = "3.3.1"
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
wiremock = { workspace = true }
zstd = { workspace = true }
@@ -74,6 +74,7 @@ impl GuardianV2Config {
configured.transcript_mode = Some(config.guardian_transcript_mode);
let mut resolved = Self::from_overrides(configured.clone())?;
resolved.transcript.elastic = config.guardian_async_elastic_budget.clone();
// Config.features can be changed after loading, including for reviewer threads.
let legacy = FeatureToml::Config(GuardianV2ConfigToml {
enabled: Some(config.features.enabled(codex_features::Feature::GuardianV2)),
@@ -173,6 +174,7 @@ impl GuardianV2Config {
let mut resolved = Self::from_overrides(configured)?;
resolved.local_overrides = self.local_overrides.clone();
resolved.transcript.elastic = self.transcript.elastic.clone();
resolved.policy = self.policy.clone();
resolved.review_multi_agent_calls = self.review_multi_agent_calls;
resolved.multi_agent_namespace = self.multi_agent_namespace.clone();
@@ -291,6 +293,7 @@ impl GuardianV2Config {
max_parent_compaction_tokens,
policy,
transcript: TranscriptConfig {
elastic: None,
format: configured.transcript_mode.unwrap_or_default(),
sources: transcript_config
.and_then(|transcript| transcript.sources.clone())
@@ -12,6 +12,7 @@ use crate::async_scorer::sampler::tests::sampler_config;
use crate::async_scorer::score::PendingScore;
use crate::async_scorer::transcript::ContextInput;
use codex_extension_api::ThreadStopInput;
use codex_guardian_context::ContextBudgeting;
use codex_guardian_context::ContextTarget;
use core_test_support::context_snapshot::ContextSnapshotOptions;
use core_test_support::context_snapshot::SnapshotEntry;
@@ -79,17 +80,23 @@ async fn pending_request(
))
}
#[test_case::test_case(ContextBudgeting::Fixed; "fixed")]
#[test_case::test_case(ContextBudgeting::Elastic; "elastic")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn configured_reset_limit_rebuilds_history_without_rejecting_fresh_evidence() -> Result<()> {
async fn configured_reset_limit_rebuilds_history_without_rejecting_fresh_evidence(
budgeting: ContextBudgeting,
) -> Result<()> {
use crate::async_scorer::decisions::DecisionsSampler;
use codex_http_client::HttpClientFactory;
use codex_http_client::OutboundProxyPolicy;
skip_if_no_network!(Ok(()));
let elastic = budgeting == ContextBudgeting::Elastic;
for (limit, reused) in [(1, false), (100_000, true)] {
let server = responses::start_mock_server().await;
let fixture = GuardianFailureFixture::with_config(&format!(
"[features]\nguardianv2_decisions_comparison = true\n[features.guardianv2]\nasync_classifier_mode = 'conversation'\nasync_classifier_conversation_token_limit = {limit}"
"[features]\nguardianv2_decisions_comparison = true\n[features.guardianv2]\nasync_classifier_mode = 'conversation'\nasync_classifier_conversation_token_limit = {limit}\nasync_elastic_budgeting = {elastic}\n{}",
core_test_support::guardian::async_elastic_sections_toml(),
))
.await?;
let store = fixture.test.codex.thread_extension_data();
@@ -1,8 +1,12 @@
//! Admits classifier requests before moving committed history, without advancing the cursor.
//! The complete immutable prefix, setup and new evidence share the classifier input limit.
//! Instruction and sync-review delivery metadata stay with history. Previously delivered
//! evidence is omitted before budgeting, using the same instruction filtering as sync.
//! evidence is omitted before budgeting. Elastic allocation selects only the new evidence,
//! after accounting for setup, checkpoint and the complete immutable prefix.
use codex_context_fragments::ContextualUserFragment;
use codex_core::context::GuardianBudgetOmission;
use codex_guardian_context::RequestBudget;
use codex_guardian_context::TranscriptCursor;
use codex_guardian_context::TruncationObservation;
use codex_guardian_context::estimate_input_tokens;
@@ -44,21 +48,47 @@ impl LunaSamplingRequest {
Vec::new()
};
let (mut context, _) = evidence.compose(cursor, history).ok()?;
context.retain_new_instructions(history);
if evidence.elastic.is_none() {
context.retain_new_instructions(history);
}
context.retain_new_images(history.iter().map(|item| &item.item), |image, detail| {
*detail = None;
Some(Cow::Borrowed(image))
});
let truncations = std::mem::take(&mut context.truncations);
let section_costs = context.section_costs().collect();
let existing_context_tokens = history
.iter()
.map(|item| estimate_input_tokens(&item.item))
.fold(0usize, usize::saturating_add);
let setup_tokens = setup
.iter()
.map(|item| estimate_input_tokens(&item.item))
.fold(0usize, usize::saturating_add);
let maximum = max_input_tokens.saturating_sub(INPUT_TOKEN_MARGIN);
if let Some(policy) = &evidence.elastic {
context = context
.prepare_elastic_entries(policy)
.ok()?
.prepare_delivery(history)
.ok()?
.enforce_budget(
RequestBudget {
max_input_tokens: maximum,
existing_context_tokens: existing_context_tokens
.saturating_add(setup_tokens),
},
GuardianBudgetOmission
.render()
.into_text()
.unwrap_or_default(),
)
.ok()?;
}
let truncations = std::mem::take(&mut context.truncations);
let section_costs = context.section_costs().collect();
let (mut input, new_input_tokens) =
LunaSampler::finish_input(self, setup, context.into_annotated_messages());
let input_tokens = existing_context_tokens.saturating_add(new_input_tokens);
if input_tokens > max_input_tokens.saturating_sub(INPUT_TOKEN_MARGIN) {
if input_tokens > maximum {
return None;
}
if let Some(mut history) = conversation.take_history() {
@@ -3,6 +3,7 @@ use crate::async_scorer::transcript::CollectedTranscript;
use crate::async_scorer::transcript::ContextInput;
use crate::async_scorer::transcript::TranscriptConfig;
use codex_extension_api::ConversationHistorySnapshot;
use codex_guardian_context::ContextBudgeting;
use codex_guardian_context::ContextTarget;
use codex_guardian_context::PlannedAction;
use codex_guardian_context::PlannedActionKind;
@@ -32,7 +33,11 @@ impl ConversationHistorySnapshot for History {
}
}
fn collect(history: &History, images: &[ContentItem]) -> CollectedTranscript {
fn collect(
history: &History,
images: &[ContentItem],
mode: ContextBudgeting,
) -> CollectedTranscript {
let action = PlannedAction {
tool_descriptions: None,
kind: PlannedActionKind::Command {
@@ -46,7 +51,12 @@ fn collect(history: &History, images: &[ContentItem]) -> CollectedTranscript {
fragment: "Approved reading README.md only.".to_owned(),
}])
.unwrap();
TranscriptConfig::default()
let config = TranscriptConfig {
elastic: (mode == ContextBudgeting::Elastic)
.then(core_test_support::guardian::elastic_budget_config),
..TranscriptConfig::default()
};
config
.collect_context(ContextInput {
target: ContextTarget::Async,
history,
@@ -63,8 +73,11 @@ fn collect(history: &History, images: &[ContentItem]) -> CollectedTranscript {
.unwrap()
}
#[test]
fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_images() {
#[test_case::test_case(ContextBudgeting::Fixed; "fixed")]
#[test_case::test_case(ContextBudgeting::Elastic; "elastic")]
fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_images(
mode: ContextBudgeting,
) {
let native = ResponseItem::AgentMessage {
id: Some(ResponseItemId::from_server("amsg_parent".to_owned())),
author: "/root".to_owned(),
@@ -115,8 +128,9 @@ fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_image
}
let mut state = ConversationState::default();
let config = sample_request("parent");
let evidence = collect(&history, &images[..1], mode);
let first = config
.prepare_retained(&collect(&history, &images[..1]), &mut state, INPUT_BUDGET)
.prepare_retained(&evidence, &mut state, INPUT_BUDGET)
.unwrap();
assert_eq!(
input_images(&first.input),
@@ -155,12 +169,13 @@ fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_image
*detail = Some(ImageDetail::High);
}
}
let evidence = collect(&history, &new_images);
let evidence = collect(&history, &new_images, mode);
// Fits three distinct images, but not another copy of the two retained images.
let second = config
.prepare_retained(&evidence, &mut state, /*max_input_tokens*/ 40_000)
.unwrap();
assert_eq!(&second.input[..committed.len()], committed);
assert!(second.truncations.is_empty());
assert!(state.snapshot().is_none());
assert_eq!(
second.input_tokens,
@@ -192,6 +207,7 @@ fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_image
)
.unwrap();
assert!(delta.contains("TRANSCRIPT DELTA START"));
assert!(!delta.contains("<guardian_context_omission>"));
assert!(delta.contains("[4] user: Inspect LICENSE next."));
assert!(!delta.contains("[1] user: Inspect README.md."));
assert!(delta.contains("Do not write files."));
@@ -216,8 +232,9 @@ fn preparation_preserves_committed_prefix_and_selects_only_new_entries_and_image
);
}
#[test]
fn rejected_preparation_preserves_committed_history_and_cursor() {
#[test_case::test_case(ContextBudgeting::Fixed; "fixed")]
#[test_case::test_case(ContextBudgeting::Elastic; "elastic")]
fn rejected_preparation_preserves_committed_history_and_cursor(mode: ContextBudgeting) {
let mut history = History {
items: vec![responses::user_message_item("Inspect README.md.")],
version: 1,
@@ -225,7 +242,7 @@ fn rejected_preparation_preserves_committed_history_and_cursor() {
let mut state = ConversationState::default();
let config = sample_request("parent");
let first = config
.prepare_retained(&collect(&history, &[]), &mut state, INPUT_BUDGET)
.prepare_retained(&collect(&history, &[], mode), &mut state, INPUT_BUDGET)
.unwrap();
let limit = first.input_tokens + 256;
let mut committed = first.input;
@@ -234,14 +251,14 @@ fn rejected_preparation_preserves_committed_history_and_cursor() {
state.commit_snapshot(committed.clone());
assert!(
config
.prepare_retained(&collect(&history, &[]), &mut state, limit)
.prepare_retained(&collect(&history, &[], mode), &mut state, limit)
.is_none()
);
assert_eq!(state.cursor(), Some(first.cursor));
assert_eq!(state.snapshot().unwrap().history(), &committed);
config
.prepare_retained(
&collect(&history, &[]),
&collect(&history, &[], mode),
&mut ConversationState::default(),
limit,
)
@@ -249,20 +266,21 @@ fn rejected_preparation_preserves_committed_history_and_cursor() {
history.version += 1;
assert!(
config
.prepare_retained(&collect(&history, &[]), &mut state, INPUT_BUDGET)
.prepare_retained(&collect(&history, &[], mode), &mut state, INPUT_BUDGET)
.is_none()
);
state.reset_transcript();
assert!(
config
.prepare_retained(&collect(&history, &[]), &mut state, INPUT_BUDGET)
.prepare_retained(&collect(&history, &[], mode), &mut state, INPUT_BUDGET)
.is_none()
);
assert_eq!(state.snapshot().unwrap().history(), &committed);
}
#[test]
fn complete_budget_counts_setup_checkpoint_images_and_trusted_evidence() {
#[test_case::test_case(ContextBudgeting::Fixed; "fixed")]
#[test_case::test_case(ContextBudgeting::Elastic; "elastic")]
fn complete_budget_counts_setup_checkpoint_images_and_trusted_evidence(mode: ContextBudgeting) {
let history = History {
items: vec![responses::user_message_item("Inspect README.md.")],
version: 1,
@@ -280,7 +298,7 @@ fn complete_budget_counts_setup_checkpoint_images_and_trusted_evidence() {
detail: None,
}];
let mut state = ConversationState::default();
let evidence = collect(&history, &images);
let evidence = collect(&history, &images, mode);
let request = config
.prepare_retained(&evidence, &mut state, INPUT_BUDGET)
.unwrap();
@@ -58,6 +58,7 @@ pub(crate) struct ContextInput<'a> {
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct TranscriptConfig {
pub(crate) format: TranscriptFormat,
pub(crate) elastic: Option<codex_guardian_context::ElasticBudgetConfig>,
pub(crate) sources: Vec<TranscriptSource>,
pub(crate) include_images: bool,
pub(crate) max_message_entry_tokens: usize,
@@ -71,6 +72,7 @@ impl Default for TranscriptConfig {
fn default() -> Self {
Self {
format: TranscriptFormat::Line,
elastic: None,
sources: vec![TranscriptSource::ToolCalls, TranscriptSource::ToolOutputs],
include_images: ContextProfile::asynchronous().include_images,
max_message_entry_tokens: MAX_MESSAGE_ENTRY_TOKENS,
@@ -149,6 +151,7 @@ impl TranscriptConfig {
context,
profile,
history_version,
elastic: self.elastic.clone(),
})
}
}
@@ -158,6 +161,7 @@ pub(super) struct CollectedTranscript {
context: CollectedContext,
profile: ContextProfile,
history_version: u64,
pub(super) elastic: Option<codex_guardian_context::ElasticBudgetConfig>,
}
impl CollectedTranscript {
@@ -186,10 +190,16 @@ impl CollectedTranscript {
&self.profile,
offset,
presentation,
ContextBudgeting::Fixed,
if self.elastic.is_some() {
ContextBudgeting::Elastic
} else {
ContextBudgeting::Fixed
},
reviewer_history,
)?;
context.retain_new_native_messages(reviewer_history)?;
if self.elastic.is_none() {
context.retain_new_native_messages(reviewer_history)?;
}
Ok((context, next))
}
}