From e1143f0b8b573c571e5a564421ed1acbca019492 Mon Sep 17 00:00:00 2001 From: hermes Date: Tue, 6 Oct 2026 12:15:17 +0000 Subject: [PATCH] Remember thinking level in last.json too --- crates/hx-cli/src/main.rs | 18 +++++++++++++---- crates/hx-core/src/switch.rs | 39 ++++++++++++++++++++++++++---------- crates/hx-tui/src/lib.rs | 3 +++ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 3ac7731..865b9c2 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -502,7 +502,9 @@ async fn main() -> Result<()> { _ => kind, }; // the remembered model applies only to the provider it was chosen on - let last_model = last.filter(|l| l.provider == kind).map(|l| l.model); + let last = last.filter(|l| l.provider == kind); + let last_effort = last.as_ref().and_then(|l| l.effort); + let last_model = last.map(|l| l.model); let model = cli.model.or(cfg.model).or(last_model).unwrap_or_else(|| { match kind { Kind::Claude => cfg.claude_model.clone(), @@ -543,8 +545,16 @@ async fn main() -> Result<()> { Session::open(&path)?, cx, ); + let effort = last_effort + .filter(|e| hx_ai::Effort::levels(kind).contains(e)) + .unwrap_or(hx_ai::Effort::default_for(kind)); + let start = hx_core::switch::Last { + provider: kind, + model: model.clone(), + effort: Some(effort), + }; agent.set_model(model); - agent.set_effort(hx_ai::Effort::default_for(kind)); + agent.set_effort(effort); agent.set_system(context::system_prompt( cfg.system_prompt.as_deref(), &context::hx_home(), @@ -682,7 +692,7 @@ async fn main() -> Result<()> { Box::new(provider), cfg.compact_at.is_none(), ) - .remember_in(last_path.clone()), + .remember_in(last_path.clone(), start.clone()), }, ) .await; @@ -701,7 +711,7 @@ async fn main() -> Result<()> { Box::new(provider), cfg.compact_at.is_none(), ) - .remember_in(last_path), + .remember_in(last_path, start), pricing, specs, open_sessions, diff --git a/crates/hx-core/src/switch.rs b/crates/hx-core/src/switch.rs index 6e2c0f4..c33b645 100644 --- a/crates/hx-core/src/switch.rs +++ b/crates/hx-core/src/switch.rs @@ -24,10 +24,13 @@ pub struct Switched { } /// Last chosen provider + model, kept in `$HX_HOME/last.json` so the next start reuses it. -#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq, Clone)] pub struct Last { pub provider: Kind, pub model: String, + /// Thinking level; absent in files written before it was saved. + #[serde(default)] + pub effort: Option, } impl Last { @@ -38,7 +41,7 @@ impl Last { } pub struct Switcher { - remember: Option, + remember: Option<(std::path::PathBuf, Last)>, kind: Kind, models: Vec, make_provider: MakeProvider, @@ -64,11 +67,20 @@ impl Switcher { } /// Write every successful switch to `path` (best effort). - pub fn remember_in(mut self, path: std::path::PathBuf) -> Self { - self.remember = Some(path); + /// `start` is what the agent starts with. + pub fn remember_in(mut self, path: std::path::PathBuf, start: Last) -> Self { + self.remember = Some((path, start)); self } + /// Save a new thinking level next to the model (best effort). + pub fn remember_effort(&mut self, e: hx_ai::Effort) { + if let Some((p, l)) = &mut self.remember { + l.effort = Some(e); + let _ = serde_json::to_vec(&*l).map(|b| std::fs::write(&*p, b)); + } + } + /// The provider the agent currently uses. pub fn kind(&self) -> Kind { self.kind @@ -103,13 +115,11 @@ impl Switcher { } else { agent.set_model(model.as_str()); } - if let Some(p) = &self.remember { - let last = Last { - provider: kind, - model: model.clone(), - }; + if let Some((p, l)) = &mut self.remember { + l.provider = kind; + l.model = model.clone(); // ponytail: failing to save must not fail the switch - let _ = serde_json::to_vec(&last).map(|b| std::fs::write(p, b)); + let _ = serde_json::to_vec(&*l).map(|b| std::fs::write(&*p, b)); } let window = models::window(&self.models, &model); if self.auto_compact { @@ -254,11 +264,18 @@ mod tests { let (sw, mut a, _m, d) = setup(true, false); let f = d.path().join("last.json"); assert_eq!(Last::load(&f), None); - let mut sw = sw.remember_in(f.clone()); + let start = Last { + provider: Kind::Claude, + model: "m".into(), + effort: None, + }; + let mut sw = sw.remember_in(f.clone(), start); sw.set_model(&mut a, "gpt-x").unwrap(); + sw.remember_effort(hx_ai::Effort::High); let want = Last { provider: Kind::Codex, model: "gpt-x".into(), + effort: Some(hx_ai::Effort::High), }; assert_eq!(Last::load(&f), Some(want)); std::fs::write(&f, "junk").unwrap(); diff --git a/crates/hx-tui/src/lib.rs b/crates/hx-tui/src/lib.rs index 681b1c3..a50d60a 100644 --- a/crates/hx-tui/src/lib.rs +++ b/crates/hx-tui/src/lib.rs @@ -186,6 +186,7 @@ impl App { // keep the level when the new provider has it, else its first (Codex has no "off") if !hx_ai::Effort::levels(sw.kind).contains(&self.st.effort) { self.st.effort = hx_ai::Effort::levels(sw.kind)[0]; + self.sw.remember_effort(self.st.effort); } if sw.provider_changed { self.st.quota = None; @@ -913,6 +914,7 @@ fn on_key(app: &mut App, k: KeyEvent, abort: &AbortHandle) -> Option { KeyCode::Down => app.ed.down(), KeyCode::Tab if app.ed.buf.is_empty() => { app.st.effort = app.st.effort.next(app.st.kind); + app.sw.remember_effort(app.st.effort); app.hint = format!("thinking: {}", app.st.effort.name()); } KeyCode::Tab => { @@ -1070,6 +1072,7 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) { "effort" => match arg.parse::() { Ok(e) if hx_ai::Effort::levels(app.st.kind).contains(&e) => { app.st.effort = e; + app.sw.remember_effort(e); app.t.push(Item::Info(format!("thinking → {}", e.name()))); } Ok(_) => app.t.push(Item::Error(format!( -- 2.54.1