From 02521bc36d79ab4b1a5bd730c6b83262923aec21 Mon Sep 17 00:00:00 2001 From: Droid Date: Mon, 26 Jan 2026 22:03:39 +0400 Subject: [PATCH] fix(engine): pass CLI temperature override to session Fixes bounty issue #1502 The --temperature CLI flag was being validated but never passed to the session's CompletionRequest. This fix: 1. Adds temperature field to Config and ConfigOverrides structs 2. Passes temperature from run_cmd.rs to ConfigOverrides 3. Uses config.temperature in session.rs instead of hardcoded 0.7 Now CLI temperature overrides take precedence, falling back to 0.7 if not specified. --- cortex-cli/src/run_cmd.rs | 33 +++++++++++++++++++-------------- cortex-engine/src/config/mod.rs | 8 ++++++++ cortex-engine/src/session.rs | 3 ++- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/cortex-cli/src/run_cmd.rs b/cortex-cli/src/run_cmd.rs index a88bc612..991b1ee8 100644 --- a/cortex-cli/src/run_cmd.rs +++ b/cortex-cli/src/run_cmd.rs @@ -241,15 +241,17 @@ impl RunCli { pub async fn run(self) -> Result<()> { // Validate temperature if provided if let Some(temp) = self.temperature - && !(0.0..=2.0).contains(&temp) { - bail!("Temperature must be between 0.0 and 2.0, got {temp}"); - } + && !(0.0..=2.0).contains(&temp) + { + bail!("Temperature must be between 0.0 and 2.0, got {temp}"); + } // Validate top_p if provided if let Some(top_p) = self.top_p - && !(0.0..=1.0).contains(&top_p) { - bail!("top-p must be between 0.0 and 1.0, got {top_p}"); - } + && !(0.0..=1.0).contains(&top_p) + { + bail!("top-p must be between 0.0 and 1.0, got {top_p}"); + } // Check if original message args are all whitespace (before quote-wrapping) // This catches cases like `cortex run " "` which would otherwise pass validation @@ -420,6 +422,7 @@ impl RunCli { let overrides = cortex_engine::ConfigOverrides { model: self.model.clone(), cwd: self.cwd.clone(), + temperature: self.temperature, ..Default::default() }; let config = cortex_engine::Config::load_sync(overrides).unwrap_or_default(); @@ -460,9 +463,10 @@ impl RunCli { SessionMode::New { title } => { let id = uuid::Uuid::new_v4().to_string(); if let Some(ref t) = title - && self.verbose { - eprintln!("New session: {id} (title: {t})"); - } + && self.verbose + { + eprintln!("New session: {id} (title: {t})"); + } id } }; @@ -558,11 +562,12 @@ impl RunCli { while let Ok(event) = handle.event_rx.recv().await { // Check timeout if let Some(timeout) = timeout_duration - && start_time.elapsed() > timeout { - eprintln!("Timeout reached after {} seconds", self.timeout); - error_occurred = true; - break; - } + && start_time.elapsed() > timeout + { + eprintln!("Timeout reached after {} seconds", self.timeout); + error_occurred = true; + break; + } event_count += 1; diff --git a/cortex-engine/src/config/mod.rs b/cortex-engine/src/config/mod.rs index 566e5eea..fb4ec3a3 100644 --- a/cortex-engine/src/config/mod.rs +++ b/cortex-engine/src/config/mod.rs @@ -81,6 +81,9 @@ pub struct Config { /// Small model for lightweight tasks (title generation, summaries). /// Format: "provider/model" (e.g., "openai/gpt-4o-mini"). pub small_model: Option, + /// Temperature for generation (0.0-2.0). + /// CLI override takes precedence over agent default. + pub temperature: Option, } impl Default for Config { @@ -110,6 +113,7 @@ impl Default for Config { current_agent: None, permission: PermissionConfig::default(), small_model: None, // Auto-detected based on available providers + temperature: None, } } } @@ -225,6 +229,8 @@ impl Config { current_agent: toml.current_agent, permission: toml.permission, small_model: toml.small_model, + // CLI temperature override takes precedence + temperature: overrides.temperature, } } } @@ -238,4 +244,6 @@ pub struct ConfigOverrides { pub approval_policy: Option, pub sandbox_mode: Option, pub additional_writable_roots: Vec, + /// Temperature override from CLI (0.0-2.0). + pub temperature: Option, } diff --git a/cortex-engine/src/session.rs b/cortex-engine/src/session.rs index b3a5a567..ea3af4a0 100644 --- a/cortex-engine/src/session.rs +++ b/cortex-engine/src/session.rs @@ -760,7 +760,8 @@ impl Session { model: self.config.model.clone(), messages: self.messages.clone(), max_tokens: Some(4096), - temperature: Some(0.7), + // Use CLI-provided temperature or default to 0.7 + temperature: Some(self.config.temperature.unwrap_or(0.7)), tools, stream: true, };