Support model-owned token budget defaults (#35608)

## What changed

- Add token-budget settings to model catalog messages and apply them when the
  feature is enabled without explicit token-budget configuration.
- Keep explicit user settings authoritative, reject invalid catalog defaults,
  and preserve resolved defaults in exported config locks.
- Manage context-window guidance through world state so it updates once when
  the active model changes while retaining prior conversation history.

## Testing

- Cover catalog defaults, explicit overrides, disabled features, invalid
  values, config-lock replay, and model switching.

GitOrigin-RevId: 54544fefaa14b09f5ef5bad9967a13c52b87b0fe
This commit is contained in:
rka-oai
2026-07-27 12:52:37 +00:00
committed by copyberry
parent 16d7daad7c
commit fbe65995bb
17 changed files with 742 additions and 72 deletions
@@ -0,0 +1,54 @@
use super::PreviousSectionState;
use super::WorldStateSection;
use crate::context::ContextWindowGuidance;
use crate::context::ContextualUserFragment;
/// Model-visible guidance for managing the current context window.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ContextWindowGuidanceState {
message: String,
}
impl ContextWindowGuidanceState {
pub(crate) fn new(message: &str) -> Self {
Self {
message: message.to_string(),
}
}
}
impl WorldStateSection for ContextWindowGuidanceState {
const ID: &'static str = "context_window_guidance";
type Snapshot = String;
fn snapshot(&self) -> Self::Snapshot {
self.message.clone()
}
fn matches_legacy_fragment(role: &str, text: &str) -> bool {
role == "developer" && ContextWindowGuidance::matches_text(text)
}
fn has_retained_fragment_matcher() -> bool {
true
}
fn matches_retained_fragment(role: &str, text: &str) -> bool {
Self::matches_legacy_fragment(role, text)
}
fn render_diff(
&self,
previous: PreviousSectionState<'_, Self::Snapshot>,
) -> Option<Box<dyn ContextualUserFragment>> {
if matches!(previous, PreviousSectionState::Known(message) if message == &self.message) {
return None;
}
Some(Box::new(ContextWindowGuidance::new(&self.message)))
}
}
#[cfg(test)]
#[path = "context_window_guidance_tests.rs"]
mod tests;
@@ -0,0 +1,52 @@
use super::ContextWindowGuidanceState;
use crate::context::ContextWindowGuidance;
use crate::context::ContextualUserFragment;
use crate::context::world_state::WorldState;
use codex_protocol::models::ResponseItem;
use pretty_assertions::assert_eq;
#[test]
fn changed_guidance_is_rendered_once() {
let mut original = WorldState::default();
original.add_section(ContextWindowGuidanceState::new("original guidance"));
let snapshot = original.snapshot();
let mut unchanged = WorldState::default();
unchanged.add_section(ContextWindowGuidanceState::new("original guidance"));
assert!(unchanged.render_diff(&snapshot).is_empty());
let mut refreshed = WorldState::default();
refreshed.add_section(ContextWindowGuidanceState::new("refreshed guidance"));
let fragments = refreshed.render_diff(&snapshot);
assert_eq!(fragments.len(), 1);
assert_eq!(
fragments[0].render(),
ContextWindowGuidance::new("refreshed guidance").render()
);
}
#[test]
fn legacy_guidance_is_reconciled_once() {
let mut state = WorldState::default();
state.add_section(ContextWindowGuidanceState::new("current guidance"));
let retained: ResponseItem =
ContextualUserFragment::into(ContextWindowGuidance::new("previous guidance"));
let fragments =
state.render_history_diff(/*previous*/ None, std::slice::from_ref(&retained));
assert_eq!(fragments.len(), 1);
assert_eq!(
fragments[0].render(),
ContextWindowGuidance::new("current guidance").render()
);
let snapshot = state.snapshot();
let reconciled: ResponseItem =
ContextualUserFragment::into(ContextWindowGuidance::new("current guidance"));
assert!(
state
.render_history_diff(Some(&snapshot), &[retained, reconciled])
.is_empty()
);
}
@@ -1,6 +1,7 @@
mod agents_md;
mod apps_instructions;
mod collaboration_mode;
mod context_window_guidance;
mod environment;
mod environments_instructions;
mod model;
@@ -32,6 +33,7 @@ use std::fmt;
pub(crate) use agents_md::AgentsMdState;
pub(crate) use apps_instructions::AppsInstructionsState;
pub(crate) use collaboration_mode::CollaborationModeState;
pub(crate) use context_window_guidance::ContextWindowGuidanceState;
pub(crate) use environment::EnvironmentsState;
pub(crate) use environments_instructions::EnvironmentsInstructionsState;
pub(crate) use model::ModelInstructionsState;