|
| 1 | +//! Gated reasoning-effort updates appended after accepted input. |
| 2 | +//! |
| 3 | +//! Only trusted harness items establish an effort. Updates append to surviving |
| 4 | +//! history without replacing the request-level reasoning effort. |
| 5 | +
|
| 6 | +use super::session::Session; |
| 7 | +use super::step_context::StepContext; |
| 8 | +use super::step_settings::ResolvedStepSettings; |
| 9 | +use codex_features::Feature; |
| 10 | +use codex_history::CodexHarnessMetadata; |
| 11 | +use codex_history::ResponseItemEnvelope; |
| 12 | +use codex_protocol::models::ConfigurationReasoning; |
| 13 | +use codex_protocol::models::ResponseItem; |
| 14 | +use codex_protocol::openai_models::ReasoningEffort; |
| 15 | + |
| 16 | +impl Session { |
| 17 | + /// Establishes the selected effort in surviving history, independent of replayed settings. |
| 18 | + pub(crate) async fn record_reasoning_effort_override(&self, step_context: &StepContext) { |
| 19 | + let settings = &step_context.settings; |
| 20 | + let Some(effort) = self.effort_for_configuration_update(settings).await else { |
| 21 | + return; |
| 22 | + }; |
| 23 | + let should_skip = { |
| 24 | + let state = self.state.lock().await; |
| 25 | + let established_effort = |
| 26 | + state |
| 27 | + .history |
| 28 | + .annotated_items() |
| 29 | + .iter() |
| 30 | + .rev() |
| 31 | + .find_map(|envelope| { |
| 32 | + if !envelope |
| 33 | + .metadata |
| 34 | + .as_ref() |
| 35 | + .is_some_and(|metadata| metadata.harness_authored_configuration) |
| 36 | + { |
| 37 | + return None; |
| 38 | + } |
| 39 | + match &envelope.item { |
| 40 | + ResponseItem::ConfigurationUpdate { reasoning } => { |
| 41 | + Some(&reasoning.effort) |
| 42 | + } |
| 43 | + _ => None, |
| 44 | + } |
| 45 | + }); |
| 46 | + established_effort == Some(&effort) |
| 47 | + }; |
| 48 | + if should_skip { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + self.record_annotated_conversation_items( |
| 53 | + step_context.turn.as_ref(), |
| 54 | + vec![ResponseItemEnvelope { |
| 55 | + item: ResponseItem::ConfigurationUpdate { |
| 56 | + reasoning: ConfigurationReasoning { effort }, |
| 57 | + }, |
| 58 | + metadata: Some(CodexHarnessMetadata { |
| 59 | + harness_authored_configuration: true, |
| 60 | + ..Default::default() |
| 61 | + }), |
| 62 | + }], |
| 63 | + ) |
| 64 | + .await; |
| 65 | + } |
| 66 | + |
| 67 | + async fn effort_for_configuration_update( |
| 68 | + &self, |
| 69 | + settings: &ResolvedStepSettings, |
| 70 | + ) -> Option<ReasoningEffort> { |
| 71 | + if !self.enabled(Feature::ReasoningEffortOverride) |
| 72 | + || !settings.model_info.use_responses_lite |
| 73 | + || !self.provider().await.is_openai() |
| 74 | + { |
| 75 | + return None; |
| 76 | + } |
| 77 | + let effort = settings |
| 78 | + .model_info |
| 79 | + .resolve_reasoning_effort(settings.effective_reasoning_effort()?); |
| 80 | + // Persistent normalizes to "disabled". Keep unknown custom values out of |
| 81 | + // durable updates so injected items stay bounded to known backend modes. |
| 82 | + if matches!(&effort, ReasoningEffort::Custom(value) if value != "disabled") { |
| 83 | + return None; |
| 84 | + } |
| 85 | + Some(effort) |
| 86 | + } |
| 87 | +} |
0 commit comments