Enable conversation-scoped speed selection for Cloud turns (#61178)

## What changed

- Stage Cloud speed choices for the next new turn in the conversation, alongside pending model and effort selections. Update the displayed conversation speed after admission without changing the local default.
- Offer speed commands supported by the pending model. Clear an incompatible pending speed change with a warning when switching models.
- Carry `service_tier` through Cloud message requests, preserving omission to inherit the thread tier and explicit null to clear it. Translate `default` to null and reject tier overrides on queued requests.

## Testing

Add tests for Cloud speed toggling, model-dependent speed choices, admission-time updates, and preserving the conversation tier when selecting a model. Extend request and wire tests to cover service-tier values, explicit clearing, and queue rejection.

GitOrigin-RevId: b4daa701f1fe29593d90a938e60cc20cb980298a
This commit is contained in:
Benjamin Carlsson
2026-11-12 20:56:33 +00:00
committed by copyberry
parent 72532ecae5
commit b279cfb170
20 changed files with 583 additions and 73 deletions
@@ -1,6 +1,12 @@
//! Route composer input and distinguish admission, rejection, and uncertainty.
//! A duplicate or an unconfirmed response must never trigger an automatic resend.
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::MutexGuard;
use std::sync::PoisonError;
use codex_app_server_client::AppServerRequestHandle;
use codex_app_server_client::CloudUserMessageReceipt;
use codex_app_server_client::TypedRequestError;
@@ -10,6 +16,8 @@ use codex_app_server_protocol::CodexErrorInfo as AppServerCodexErrorInfo;
use codex_app_server_protocol::TurnError as AppServerTurnError;
use codex_app_server_protocol::TurnStartParams;
use codex_protocol::ThreadId;
use codex_protocol::config_types::SERVICE_TIER_DEFAULT_REQUEST_VALUE;
use codex_protocol::openai_models::ModelPreset;
use codex_protocol::openai_models::ReasoningEffort;
use codex_protocol::turn_input::CyberAccessProgram;
use codex_utils_absolute_path::AbsolutePathBuf;
@@ -83,7 +91,50 @@ pub(crate) struct CloudModelSelection {
pub(crate) effort: Option<ReasoningEffort>,
}
#[derive(Clone, Default)]
pub(crate) struct CloudTurnSettings {
pub(crate) model: Option<CloudModelSelection>,
pub(crate) service_tier: Option<String>,
}
impl CloudTurnSettings {
pub(super) fn apply(&self, params: &mut TurnStartParams) {
if let Some(model) = &self.model {
params.model = Some(model.model.clone());
params.effort.clone_from(&model.effort);
}
params.service_tier = self.service_tier.clone().map(Some);
}
}
/// Pending choices are scoped to one Cloud backend and keyed by conversation.
#[derive(Clone, Default)]
pub(crate) struct PendingCloudTurnSettings(Arc<Mutex<HashMap<ThreadId, CloudTurnSettings>>>);
impl PendingCloudTurnSettings {
fn lock(&self) -> MutexGuard<'_, HashMap<ThreadId, CloudTurnSettings>> {
self.0.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn model(&self, thread_id: ThreadId) -> Option<String> {
self.lock()
.get(&thread_id)?
.model
.as_ref()
.map(|selection| selection.model.clone())
}
}
fn same_cloud_speed(first: &str, second: &str) -> bool {
first == second || matches!((first, second), ("fast", "priority") | ("priority", "fast"))
}
impl AppServerSession {
pub(crate) fn pending_cloud_turn_settings(&self) -> Option<PendingCloudTurnSettings> {
(self.thread_params_mode() == ThreadParamsMode::Cloud)
.then(|| self.cloud_turn_settings.clone())
}
fn cloud_model_catalog_is_current(&self) -> bool {
matches!(&self.request_handle,
AppServerRequestHandle::Cloud(handle) if handle.model_catalog_is_current()
@@ -93,15 +144,47 @@ impl AppServerSession {
pub(crate) fn stage_cloud_model_selection(
&mut self,
thread_id: ThreadId,
model: String,
preset: &ModelPreset,
effort: Option<ReasoningEffort>,
) -> bool {
) -> Result<Option<String>, ()> {
if !self.cloud_model_catalog_is_current() {
return false;
return Err(());
}
self.cloud_model_selections
.insert(thread_id, CloudModelSelection { model, effort });
true
let mut pending = self.cloud_turn_settings.lock();
let settings = pending.entry(thread_id).or_default();
settings.model = Some(CloudModelSelection {
model: preset.model.clone(),
effort,
});
let dropped_tier = settings.service_tier.take_if(|tier| {
tier.as_str() != SERVICE_TIER_DEFAULT_REQUEST_VALUE
&& !preset
.service_tiers
.iter()
.any(|available| same_cloud_speed(&available.id, tier))
});
Ok(dropped_tier)
}
pub(crate) fn toggle_cloud_service_tier(
&mut self,
thread_id: ThreadId,
current: Option<&str>,
tier: String,
) -> Option<String> {
if !self.cloud_model_catalog_is_current() {
return None;
}
let mut pending = self.cloud_turn_settings.lock();
let settings = pending.entry(thread_id).or_default();
let current = settings.service_tier.as_deref().or(current);
let selected = if current.is_some_and(|current| same_cloud_speed(current, &tier)) {
SERVICE_TIER_DEFAULT_REQUEST_VALUE.to_owned()
} else {
tier
};
settings.service_tier = Some(selected.clone());
Some(selected)
}
pub(crate) async fn submit_user_message<'a>(
@@ -112,7 +195,7 @@ impl AppServerSession {
active_turn: &mut Option<String>,
preflight: impl FnOnce(&str) -> Result<Option<CyberAccessProgram>, String>,
start_settings: impl FnOnce() -> TurnStartSettings<'a>,
) -> Result<(UserMessageAdmission, Option<CloudModelSelection>), UserMessageSubmissionFailure>
) -> Result<(UserMessageAdmission, Option<CloudTurnSettings>), UserMessageSubmissionFailure>
{
let AppCommand::UserTurn {
turn_trigger,
@@ -180,46 +263,42 @@ impl AppServerSession {
let selection =
if cloud && *turn_trigger == "user" && intent != CloudSubmissionIntent::UseKnownTurn {
self.cloud_model_selections.get(&thread_id).cloned()
self.cloud_turn_settings.lock().get(&thread_id).cloned()
} else {
None
};
if selection.is_some() && !self.cloud_model_catalog_is_current() {
self.cloud_model_selections.remove(&thread_id);
self.cloud_turn_settings.lock().remove(&thread_id);
return Err(UserMessageSubmissionFailure::Preflight(
"The Cloud account changed. Reopen the conversation before sending.".into(),
));
}
let selected_model = selection
.as_ref()
.and_then(|selection| selection.model.as_ref())
.map_or(model.as_str(), |selection| selection.model.as_str());
let program = preflight(selected_model).map_err(UserMessageSubmissionFailure::Preflight)?;
let take_selection = |session: &mut Self| {
selection.as_ref()?;
session
.cloud_model_selections
.remove(&thread_id)
.filter(|_| session.cloud_model_catalog_is_current())
let settings = session.cloud_turn_settings.lock().remove(&thread_id);
settings.filter(|_| session.cloud_model_catalog_is_current())
};
if cloud
&& intent == CloudSubmissionIntent::StartOrSteer
&& let AppServerRequestHandle::Cloud(handle) = &self.request_handle
{
let (model, effort) = selection.as_ref().map_or((None, None), |selection| {
(Some(selection.model.clone()), selection.effort.clone())
});
let result = handle
.submit_user_message(TurnStartParams {
thread_id: thread_id.to_string(),
turn_trigger: model.as_ref().map(|_| turn_trigger.to_string()),
client_user_message_id: Some(client_user_message_id.clone()),
input: items.clone(),
output_schema: final_output_json_schema.clone(),
model,
effort,
..Default::default()
})
.await;
let mut params = TurnStartParams {
thread_id: thread_id.to_string(),
client_user_message_id: Some(client_user_message_id.clone()),
input: items.clone(),
output_schema: final_output_json_schema.clone(),
..Default::default()
};
if let Some(selection) = &selection {
params.turn_trigger = Some(turn_trigger.to_string());
selection.apply(&mut params);
}
let result = handle.submit_user_message(params).await;
let admission = match result {
Ok(CloudUserMessageReceipt::Started(turn)) => {
Ok(UserMessageAdmission::Started(turn.id))