Normalize saved chat settings after model catalog refreshes (#59111)

## Why

Existing chats can retain reasoning effort and service tier selections that a refreshed provider catalog no longer supports.

## What changed

- Apply fresh provider catalogs to saved settings in open and resumed chats, and normalize incoming settings before capturing the next turn.
- Replace unsupported ranked reasoning efforts with the highest supported level at or below the selection. For unsupported `ultra` or `persistent` selections, use the model's supported default when available. Reset unsupported `priority` and `ultrafast` service tiers to `default`.
- Retire incompatible reasoning effort pins using each step's captured model metadata, preserving running steps and the live pin during compaction.
- Publish catalog updates only from successful provider responses for the current identity, and refresh retained originator catalogs through the periodic refresh worker.

## Testing

Add coverage for setting correction, reasoning effort pins, resumed chats, stale queued settings, retained history, refresh failures, identity changes, and isolated originator notifications.

GitOrigin-RevId: 2ef4605c4acba98db3fe9d0328e2752084e900a2
This commit is contained in:
dpatel-oai
2026-11-03 10:01:36 +00:00
committed by copyberry
parent a805143486
commit 33ffb6f905
14 changed files with 766 additions and 34 deletions
+18
View File
@@ -463,6 +463,11 @@ pub(super) async fn submission_loop(
) {
// Session shutdown and tree shutdown both use the existing teardown handler.
let mut shutdown_received = false;
let mut catalog_updates = sess.services.models_manager.model_catalog_updates();
if let Some(updates) = &mut catalog_updates {
// A resumed chat must also consume a catalog fetched before it was opened.
updates.mark_changed();
}
let mut mailbox = sess.input_queue.mailbox_updates();
if let Some(updates) = &mut mailbox {
updates.mark_changed();
@@ -474,6 +479,19 @@ pub(super) async fn submission_loop(
shutdown_received = shutdown(&sess, super::new_submission_id()).await;
break;
}
update = async {
match &mut catalog_updates {
Some(updates) => updates.changed().await,
None => std::future::pending().await,
}
} => {
if update.is_err() {
catalog_updates = None;
} else {
super::model_catalog_settings::apply(&sess).await;
}
continue;
}
sub = rx_sub.recv() => match sub {
Ok(sub) => sub,
Err(_) => break,
+13 -8
View File
@@ -264,6 +264,7 @@ mod mcp;
mod mcp_prewarm;
mod mcp_refresh;
mod mcp_runtime;
mod model_catalog_settings;
pub(crate) mod multi_agents;
mod plugin_selection;
mod realtime_history;
@@ -2087,14 +2088,18 @@ impl Session {
credential_masking_warning,
) = {
let mut state = self.state.lock().await;
let updated = match self.apply_session_settings(&state.session_configuration, &updates)
{
Ok(updated) => updated,
Err(err) => {
warn!("rejected session settings update: {err}");
return Err(err);
}
};
let mut updated =
match self.apply_session_settings(&state.session_configuration, &updates) {
Ok(updated) => updated,
Err(err) => {
warn!("rejected session settings update: {err}");
return Err(err);
}
};
if let Some(catalog) = self.services.models_manager.refreshed_model_catalog() {
model_catalog_settings::normalize(&mut updated.step_settings, &catalog);
}
if !should_commit(&state.session_configuration, &updated) {
return Ok(None);
@@ -0,0 +1,88 @@
//! Keep saved chat choices within the latest successfully fetched provider catalog.
//! Running steps retain their captured settings; changes apply to the next turn.
use super::session::Session;
use super::session::SessionSettingsUpdate;
use super::step_settings::StepSettings;
use super::thread_settings;
use codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
use codex_protocol::openai_models::ModelInfo;
use codex_protocol::openai_models::ReasoningEffort;
use std::sync::Arc;
pub(super) async fn apply(session: &Session) {
let _settings_guard = thread_settings::acquire_persistence_lock(session).await;
let Ok(Some(commit)) = session
.update_settings_if(SessionSettingsUpdate::default(), |current, updated| {
current.step_settings != updated.step_settings
})
.await
else {
return;
};
thread_settings::emit_applied(session, super::new_submission_id(), commit.snapshot).await;
}
pub(super) fn normalize(settings: &mut Arc<StepSettings>, catalog: &[ModelInfo]) {
let Some(model) = catalog
.iter()
.find(|model| model.slug == settings.collaboration_mode.model())
else {
return;
};
let effort = settings.collaboration_mode.reasoning_effort();
let allowed = &model.supported_reasoning_levels;
if let Some(effort) = effort
&& !allowed.is_empty()
&& !allowed.iter().any(|preset| preset.effort == effort)
{
let replacement = effort_rank(&effort)
.and_then(|previous| {
allowed
.iter()
.filter_map(|preset| {
effort_rank(&preset.effort)
.filter(|rank| *rank <= previous)
.map(|rank| (rank, &preset.effort))
})
.max_by_key(|(rank, _)| *rank)
.map(|(_, effort)| effort)
})
.or_else(|| {
matches!(effort, ReasoningEffort::Ultra | ReasoningEffort::Persistent)
.then(|| model.default_reasoning_level.as_ref())
.flatten()
.filter(|default| allowed.iter().any(|preset| &preset.effort == *default))
});
if let Some(replacement) = replacement {
Arc::make_mut(settings)
.collaboration_mode
.settings
.reasoning_effort = Some(replacement.clone());
}
}
if let Some(tier) = settings.service_tier.as_deref()
&& matches!(tier, "priority" | "ultrafast")
&& !model.supports_service_tier(tier)
{
Arc::make_mut(settings).service_tier = Some(SERVICE_TIER_DEFAULT_REQUEST_VALUE.into());
}
}
fn effort_rank(effort: &ReasoningEffort) -> Option<usize> {
match effort {
ReasoningEffort::None => Some(0),
ReasoningEffort::Minimal => Some(1),
ReasoningEffort::Low => Some(2),
ReasoningEffort::Medium => Some(3),
ReasoningEffort::High => Some(4),
ReasoningEffort::XHigh => Some(5),
ReasoningEffort::Max => Some(6),
ReasoningEffort::Ultra | ReasoningEffort::Persistent | ReasoningEffort::Custom(_) => None,
}
}
#[cfg(test)]
#[path = "model_catalog_settings_tests.rs"]
mod tests;
@@ -0,0 +1,66 @@
//! The catalog correction policy preserves valid choices and avoids guessing special modes.
use super::normalize;
use crate::session::tests::make_session_configuration_for_tests;
use codex_models_manager::bundled_models_response;
use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::openai_models::ReasoningEffortPreset;
use pretty_assertions::assert_eq;
use std::sync::Arc;
#[test_case::test_case("lower")]
#[test_case::test_case("valid")]
#[test_case::test_case("missing-model")]
#[test_case::test_case("no-efforts")]
#[test_case::test_case("only-higher")]
#[test_case::test_case("special")]
#[test_case::test_case("invalid-persistent")]
#[test_case::test_case("invalid-ultra")]
#[test_case::test_case("unknown")]
#[tokio::test]
async fn correction_changes_only_supported_bounded_choices(scenario: &str) {
let mut settings = make_session_configuration_for_tests().await.step_settings;
let previous = match scenario {
"special" | "invalid-persistent" => ReasoningEffort::Persistent,
"invalid-ultra" => ReasoningEffort::Ultra,
"unknown" => ReasoningEffort::Custom("future-effort".into()),
_ => ReasoningEffort::High,
};
Arc::make_mut(&mut settings)
.collaboration_mode
.settings
.reasoning_effort = Some(previous);
Arc::make_mut(&mut settings).service_tier = Some("priority".into());
let mut expected = settings.clone();
let mut model = bundled_models_response().unwrap().models.remove(0);
model.slug = settings.collaboration_mode.model().into();
model.service_tiers.clear();
model.default_reasoning_level = Some(ReasoningEffort::Low);
let allowed = match scenario {
"valid" => ReasoningEffort::High,
"only-higher" => ReasoningEffort::Max,
"special" => ReasoningEffort::Persistent,
_ => ReasoningEffort::Low,
};
model.supported_reasoning_levels = vec![ReasoningEffortPreset {
description: allowed.to_string(),
effort: allowed,
}];
if scenario == "missing-model" {
model.slug = "another-model".into();
}
if scenario == "no-efforts" {
model.supported_reasoning_levels.clear();
}
if matches!(scenario, "lower" | "invalid-persistent" | "invalid-ultra") {
Arc::make_mut(&mut expected)
.collaboration_mode
.settings
.reasoning_effort = Some(ReasoningEffort::Low);
}
if scenario != "missing-model" {
Arc::make_mut(&mut expected).service_tier = Some("default".into());
}
normalize(&mut settings, &[model]);
assert_eq!(settings, expected);
}
+20 -10
View File
@@ -5,6 +5,7 @@
//! Successful compaction retires the overrides and allows a fresh request baseline.
//! Fixed-effort workers always use their selected request-level effort.
//! Unsupported models use selected request effort without rewriting saved updates.
//! A provider catalog can retire a request pin that is invalid for a new step.
use super::session::Session;
use super::step_context::StepContext;
@@ -14,6 +15,7 @@ use codex_history::CodexHarnessMetadata;
use codex_history::ResponseItemEnvelope;
use codex_protocol::models::ConfigurationReasoning;
use codex_protocol::models::ResponseItem;
use codex_protocol::openai_models::ModelOrigin;
use codex_protocol::openai_models::ReasoningEffort;
/// Sampling can establish a pin; compaction must not change live state before it succeeds.
@@ -107,18 +109,26 @@ impl Session {
}
return selected_effort;
}
if usage == RequestEffortUsage::Compaction
&& let Some(pinned) = self
.state
.lock()
.await
.reasoning_effort_pin
.get(&settings.model_info.slug)
{
return Some(pinned);
}
let effort = self.effort_for_configuration_update(settings);
let mut state = self.state.lock().await;
if let Some(pinned) = state.reasoning_effort_pin.get(&settings.model_info.slug) {
let model = &settings.model_info;
let disallowed = model.origin == ModelOrigin::Provider
&& !model.supported_reasoning_levels.is_empty()
&& !model
.supported_reasoning_levels
.iter()
.any(|preset| model.resolve_reasoning_effort(preset.effort.clone()) == pinned);
if disallowed {
// Check the step's captured metadata, so a refresh cannot change a running
// step. Failed compaction must not mutate the live request baseline.
if usage == RequestEffortUsage::Sampling {
state.reasoning_effort_pin = ReasoningEffortPin::Unset;
}
} else if usage == RequestEffortUsage::Compaction {
return Some(pinned);
}
}
let Some(effort) = effort else {
if usage == RequestEffortUsage::Sampling {
state.reasoning_effort_pin = ReasoningEffortPin::Unset;
@@ -8,11 +8,99 @@ use codex_history::InitialHistory;
use codex_history::ResumedHistory;
use codex_login::CodexAuth;
use codex_protocol::ThreadId;
use codex_protocol::openai_models::ModelOrigin;
use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::openai_models::ReasoningEffortPreset;
use pretty_assertions::assert_eq;
use std::sync::Arc;
use test_case::test_case;
#[test_case(ReasoningEffort::High, ReasoningEffort::Low; "disallowed_high")]
#[test_case(ReasoningEffort::Ultra, ReasoningEffort::Low; "valid_ultra")]
#[test_case(ReasoningEffort::Persistent, ReasoningEffort::Low; "valid_persistent")]
#[tokio::test]
async fn fresh_step_metadata_retires_only_incompatible_pins(
previous: ReasoningEffort,
selected: ReasoningEffort,
) {
let (session, turn, _events) = make_session_and_context_with_auth_and_config_and_rx(
CodexAuth::from_api_key("Test API Key"),
Vec::new(),
|config| {
config
.features
.enable(Feature::ReasoningEffortOverride)
.unwrap();
},
)
.await;
let mut model = (*turn.initial_settings.model_info).clone();
model.origin = ModelOrigin::Provider;
model.supports_reasoning_effort_updates = true;
model.supported_reasoning_levels = [previous.clone(), selected.clone(), ReasoningEffort::Max]
.into_iter()
.map(|effort| ReasoningEffortPreset {
description: effort.to_string(),
effort,
})
.collect();
let original = model.resolve_reasoning_effort(previous.clone());
let mut choice = turn.initial_settings.selected().clone();
choice.collaboration_mode.settings.reasoning_effort = Some(previous.clone());
let old_step = ResolvedStepSettings::new(
Arc::new(choice.clone()),
Arc::new(model.clone()),
&Default::default(),
);
assert_eq!(
session
.reasoning_effort_for_request(&old_step, RequestEffortUsage::Sampling)
.await,
Some(original.clone())
);
if previous == ReasoningEffort::High {
model.supported_reasoning_levels = vec![ReasoningEffortPreset {
description: selected.to_string(),
effort: selected.clone(),
}];
}
choice.collaboration_mode.settings.reasoning_effort = Some(selected.clone());
let expected = if previous == ReasoningEffort::High {
model.resolve_reasoning_effort(selected)
} else {
original.clone()
};
let fresh_step =
ResolvedStepSettings::new(Arc::new(choice), Arc::new(model), &Default::default());
assert_eq!(
session
.reasoning_effort_for_request(&fresh_step, RequestEffortUsage::Compaction)
.await,
Some(expected.clone())
);
assert_eq!(
session
.state
.lock()
.await
.reasoning_effort_pin
.get(&old_step.model_info.slug),
Some(original.clone())
);
assert_eq!(
session
.reasoning_effort_for_request(&old_step, RequestEffortUsage::Sampling)
.await,
Some(original)
);
assert_eq!(
session
.reasoning_effort_for_request(&fresh_step, RequestEffortUsage::Sampling)
.await,
Some(expected)
);
}
#[test_case(InitialHistory::Forked(Vec::new()); "fork")]
#[test_case(InitialHistory::Resumed(ResumedHistory {
history_revision: None,
+15 -9
View File
@@ -1210,6 +1210,21 @@ impl Session {
options: NewTurnContextOptions,
should_start: impl FnOnce(&SessionConfiguration, &SessionConfiguration) -> bool + Send,
) -> CodexResult<Option<(Arc<TurnContext>, ThreadSettingsSnapshot)>> {
let refresh_client = {
let state = self.state.lock().await;
let configuration = &state.session_configuration;
(!crate::guardian::is_basic_session_source(&configuration.session_source)).then(|| {
self.build_effective_session_config(configuration)
.http_client_factory()
})
};
// An auth refresh may change allowed settings; finish it before capturing this turn.
if let Some(http_client_factory) = refresh_client {
self.services
.models_manager
.refresh_after_auth_change(http_client_factory)
.await;
}
let service_tier_for_turn = updates.service_tier_for_turn.clone();
let commit = match self.update_settings_if(updates, should_start).await {
Ok(Some(commit)) => commit,
@@ -1233,15 +1248,6 @@ impl Session {
if let Some(service_tier) = service_tier_for_turn {
Arc::make_mut(&mut configuration.step_settings).service_tier = Some(service_tier);
}
if !crate::guardian::is_basic_session_source(&configuration.session_source) {
self.services
.models_manager
.refresh_after_auth_change(
self.build_effective_session_config(&configuration)
.http_client_factory(),
)
.await;
}
let turn_environments = self.activate_turn_environments(&configuration).await;
let turn_context = self
.new_turn_from_configuration(sub_id, configuration, turn_environments, options)