Session ops and ContextPolicy in hx-core

This commit is contained in:
hermes committed 2026-10-09 19:34:59 +00:00
1 parent 69d5ae4581
commit be74dd9fec
7 files changed
+205 -105

No files matched your search

+24 -30
View File
@@ -614,14 +614,19 @@ fn sessions_key(app: &mut App, k: KeyEvent) -> Option<String> {
}
/// Replace the agent's session (resume or new) and rebuild the transcript.
fn switch_session(app: &mut App, a: &mut Agent, sess: hx_core::session::Session) {
fn switch_session(
app: &mut App,
a: &mut Agent,
op: impl FnOnce(&mut Agent) -> Result<(), hx_core::agent::Error>,
) -> Result<(), hx_core::agent::Error> {
op(a)?;
app.t = Transcript::default();
let msgs = sess.messages();
let msgs = a.session().messages();
sessions::history(&mut app.t, &msgs);
app.title = hx_core::session::title(&msgs);
app.session_path = sess.path().to_path_buf();
a.replace_session(sess);
app.session_path = a.session().path().to_path_buf();
app.st.ctx_tokens = a.context_tokens();
Ok(())
}
/// Picker keys. Returns a line to submit when an entry runs.
@@ -1262,21 +1267,20 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
};
show(app, "lsp", l);
}
"resume" if !arg.is_empty() => match hx_core::session::Session::open(arg) {
Ok(sess) => {
switch_session(app, &mut *agent.lock().await, sess);
app.t.push(Item::Info(format!("resumed {arg}")));
"resume" if !arg.is_empty() => {
let r = switch_session(app, &mut *agent.lock().await, |a| {
a.resume(std::path::Path::new(arg))
});
match r {
Ok(()) => app.t.push(Item::Info(format!("resumed {arg}"))),
Err(e) => app.t.push(Item::Error(format!("/resume: {e}"))),
}
Err(e) => app.t.push(Item::Error(format!("/resume: {e}"))),
},
}
"sessions" | "resume" => open_sessions_picker(app),
"new" | "clear" => {
let p = hx_core::session::new_path(sessions_dir(app));
match hx_core::session::Session::open(&p) {
Ok(sess) => {
switch_session(app, &mut *agent.lock().await, sess);
app.t.push(Item::Info("new session".into()));
}
let r = switch_session(app, &mut *agent.lock().await, Agent::new_session_here);
match r {
Ok(()) => app.t.push(Item::Info("new session".into())),
Err(e) => app.t.push(Item::Error(format!("/new: {e}"))),
}
}
@@ -1339,7 +1343,7 @@ async fn submit(app: &mut App, mut line: String, c: &Ctx<'_>) {
let r = agent
.lock()
.await
.compact(4)
.compact_now()
.await
.map_err(|e| e.to_string());
let _ = tx.send(Ui::Compacted(r));
@@ -2084,12 +2088,7 @@ mod tests {
since: Instant::now(),
quit: false,
hint: String::new(),
sw: Switcher::new(
Kind::Claude,
vec![],
Box::new(|_| Ok(Arc::new(NoProvider))),
true,
),
sw: Switcher::new(Kind::Claude, vec![], Box::new(|_| Ok(Arc::new(NoProvider)))),
specs: cmd::builtins(&["sonnet", "opus"]),
popup: None,
picker: None,
@@ -2377,19 +2376,14 @@ mod tests {
let mut app = test_app();
let fail: MakeProvider =
Box::new(|_| Err(hx_ai::Error::Http("not logged in to Codex".into())));
app.sw = Switcher::new(Kind::Claude, models.clone(), fail, true);
app.sw = Switcher::new(Kind::Claude, models.clone(), fail);
// codex not logged in: error shown, footer state unchanged
submit(&mut app, "/model gpt-5.6-terra".into(), &ctx).await;
assert_eq!((app.st.kind, app.st.model.as_str()), (Kind::Claude, "m1"));
assert!(matches!(app.t.items.last(), Some(Item::Error(e)) if e.contains("not logged in")));
app.sw = Switcher::new(
Kind::Claude,
models,
Box::new(|_| Ok(Arc::new(NoProvider))),
true,
);
app.sw = Switcher::new(Kind::Claude, models, Box::new(|_| Ok(Arc::new(NoProvider))));
app.st.quota = Some(Default::default());
submit(&mut app, "/model gpt-5.6-terra".into(), &ctx).await;
assert_eq!(