mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Preserve Guardian authorization evidence until request budgeting (#44570)
## Why Transcript limits could shorten user instructions or omit later messages before Guardian evaluated the complete request budget, losing restrictions or prior approvals even when they would fit. ## What changed - Keep user messages and manual approvals complete and in source order through transcript collection and retention. - Preserve historical instructions while synchronous review can still compact history. If the final request still cannot fit, discard optional evidence before shortening older historical entries with truncation markers, preserving later restrictions where possible. - Warn that shortened instructions and approvals are incomplete and that missing evidence does not authorize actions. - Let asynchronous review defer to synchronous review when complete instructions exceed its budget. ## Testing Add coverage for complete instruction retention, approval and restriction ordering, marked truncation of oversized Unicode text, compaction before truncation, and asynchronous fallback when instructions do not fit. GitOrigin-RevId: e6159e876231347986b937ee7e07f944b83bfbe6
This commit is contained in:
1 parent
9c4879f3a5
commit
287e4f7dbf
21 files changed
+575
-335
No files matched your search
@@ -1,11 +1,13 @@
|
||||
//! Fits newly composed evidence into the remaining complete-request allowance.
|
||||
//! Required evidence is never truncated. Optional content is removed in a stable
|
||||
//! order, and a host-owned omission fragment is reserved before any removal.
|
||||
//! Required action evidence is never truncated. Optional evidence leaves first;
|
||||
//! hosts may shorten historical instructions after compaction cannot make room.
|
||||
//! Every reduction reserves an omission notice and preserves source order.
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use codex_protocol::models::ContentItem;
|
||||
use codex_protocol::models::ImageDetail;
|
||||
use codex_protocol::protocol::TruncationPolicy;
|
||||
|
||||
use crate::ComposedContext;
|
||||
use crate::RequestBudget;
|
||||
@@ -42,6 +44,13 @@ impl<T> Budgeted<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn historical(content: T) -> Self {
|
||||
Self {
|
||||
content,
|
||||
retention: Retention::Historical,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn optional(content: T, priority: BudgetPriority) -> Self {
|
||||
Self {
|
||||
content,
|
||||
@@ -62,9 +71,19 @@ impl<T> std::fmt::Debug for Budgeted<T> {
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum Retention {
|
||||
Required,
|
||||
Historical,
|
||||
Optional(BudgetPriority),
|
||||
}
|
||||
|
||||
/// Whether the host has reached the final attempt to fit historical evidence.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum HistoryTruncation {
|
||||
/// Preserve original instructions while the host can still compact history.
|
||||
Preserve,
|
||||
/// Shorten older historical entries only after optional evidence is exhausted.
|
||||
Allow,
|
||||
}
|
||||
|
||||
impl ComposedContext {
|
||||
/// Applies host image admission before aggregate selection, preserving section
|
||||
/// identity and each retained item's selection policy.
|
||||
@@ -77,12 +96,13 @@ impl ComposedContext {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns complete evidence that fits, or no context at all. The host owns
|
||||
/// the bounded omission fragment and the existing reviewer history/checkpoint.
|
||||
/// Fits evidence according to the host recovery phase, or returns no context.
|
||||
/// The host owns the bounded omission fragment and existing reviewer history.
|
||||
pub fn enforce_budget(
|
||||
mut self,
|
||||
budget: RequestBudget,
|
||||
omission_notice: String,
|
||||
history_truncation: HistoryTruncation,
|
||||
) -> Result<Self, SectionError> {
|
||||
let remaining = budget
|
||||
.max_input_tokens
|
||||
@@ -130,6 +150,62 @@ impl ComposedContext {
|
||||
remaining_items[section_index] = content.len();
|
||||
}
|
||||
}
|
||||
let mut history_truncated = false;
|
||||
if history_truncation == HistoryTruncation::Allow && required_tokens > remaining {
|
||||
// Source order makes older evidence yield first. Keep each source's
|
||||
// label, both ends and the standard marker; later restrictions stay
|
||||
// complete whenever older entries can supply the needed space.
|
||||
'history: for section in &mut self.sections {
|
||||
let SectionDelivery::UserContent(content) = &mut section.delivery else {
|
||||
continue;
|
||||
};
|
||||
for item in content {
|
||||
if item.retention != Retention::Historical {
|
||||
continue;
|
||||
}
|
||||
let original_tokens = content_tokens(&item.content);
|
||||
let ContentItem::InputText { text } = &mut item.content else {
|
||||
continue;
|
||||
};
|
||||
let target = original_tokens.saturating_sub(required_tokens - remaining);
|
||||
let mut upper = TruncationPolicy::Bytes(text.len()).token_budget();
|
||||
let mut lower = 32.min(upper);
|
||||
let mut shortened = crate::truncate_text(text, lower);
|
||||
while lower < upper {
|
||||
let mid = lower + (upper - lower).div_ceil(2);
|
||||
let candidate = crate::truncate_text(text, mid);
|
||||
if content_tokens(&ContentItem::InputText {
|
||||
text: candidate.clone(),
|
||||
}) <= target
|
||||
{
|
||||
lower = mid;
|
||||
shortened = candidate;
|
||||
} else {
|
||||
upper = mid - 1;
|
||||
}
|
||||
}
|
||||
let saved =
|
||||
original_tokens.saturating_sub(content_tokens(&ContentItem::InputText {
|
||||
text: shortened.clone(),
|
||||
}));
|
||||
if saved == 0 {
|
||||
continue;
|
||||
}
|
||||
self.truncations.push(TruncationObservation {
|
||||
component: section.id,
|
||||
original_bytes: text.len(),
|
||||
retained_bytes: shortened.len(),
|
||||
});
|
||||
*text = shortened;
|
||||
history_truncated = true;
|
||||
required_tokens = required_tokens.saturating_sub(saved);
|
||||
needed = needed.saturating_sub(saved);
|
||||
if required_tokens <= remaining {
|
||||
break 'history;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Evidence that cannot fit beside the required content and notice must
|
||||
// leave first, without evicting useful smaller entries on its behalf.
|
||||
let optional_allowance =
|
||||
@@ -162,7 +238,7 @@ impl ComposedContext {
|
||||
}
|
||||
needed = needed.saturating_sub(remove(section_index, index, tokens));
|
||||
}
|
||||
if removed.is_empty() {
|
||||
if removed.is_empty() && !history_truncated {
|
||||
return Err(SectionError::EvidenceLimitExceeded {
|
||||
section: "request_budget",
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user