Remember thinking level in last.json too

This commit is contained in:
hermes committed 2026-10-06 12:15:17 +00:00
1 parent ab2af421a4
commit e1143f0b8b
3 files changed
+45 -15

No files matched your search

+14 -4
View File
@@ -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,
+28 -11
View File
@@ -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<hx_ai::Effort>,
}
impl Last {
@@ -38,7 +41,7 @@ impl Last {
}
pub struct Switcher {
remember: Option<std::path::PathBuf>,
remember: Option<(std::path::PathBuf, Last)>,
kind: Kind,
models: Vec<ModelInfo>,
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();
+3
View File
@@ -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<String> {
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::<hx_ai::Effort>() {
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!(