Deepen Agent, compaction, switching and TUI modules
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

- Agent: make session/usage/provider/model/system/max_tokens/compact_at
  private; add set_provider, set_model, replace_session, fork, clear,
  fit_window. Fixes stale context-token reading after resume/new session.
- hx-core::compact: ContextMeter, plan/apply and estimate_tokens split out
  of agent.rs, testable without provider or session.
- hx-core::switch: Switcher shared by TUI and RPC for model/provider
  switches; RPC set_model now also refits the compaction threshold.
- hx-tui: status.rs (footer, /usage as pure functions), popup.rs, and
  Picker::key returning an Act.
- tests/tui_smoke.py: resolve the temp dir (macOS /var symlink) and match
  the resume via the OSC window title instead of split screen text.
This commit is contained in:
johannes committed 2026-10-02 00:10:41 +02:00
1 parent fe308e0f36
commit eaa6e22fbd
11 files changed
+1407 -632

No files matched your search

+28 -20
View File
@@ -7,6 +7,7 @@ use hx_core::agent::Agent;
use hx_core::context;
use hx_core::hook::{Event, Hook, HookAction};
use hx_core::session::Session;
use hx_core::switch::Switcher;
use hx_core::tool::ToolCtx;
use std::io::Write;
@@ -448,8 +449,12 @@ async fn main() -> Result<()> {
Session::open(&path)?,
cx,
);
agent.model = model;
agent.system = context::system_prompt(cfg.system_prompt.as_deref(), &context::hx_home(), &cwd);
agent.set_model(model);
agent.set_system(context::system_prompt(
cfg.system_prompt.as_deref(),
&context::hx_home(),
&cwd,
));
let mcp = Arc::new(
hx_mcp::Hub::start(
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?,
@@ -459,8 +464,7 @@ async fn main() -> Result<()> {
mcp.register(&mut agent.tools);
let idx = mcp.system_prompt();
if !idx.is_empty() {
agent.system.push_str("\n\n");
agent.system.push_str(&idx);
agent.append_system(&idx);
}
for (n, s) in &mcp.servers {
if let Some(e) = &s.error {
@@ -468,14 +472,12 @@ async fn main() -> Result<()> {
}
}
if let Some(n) = cfg.max_tokens {
agent.max_tokens = n;
agent.set_max_tokens(n);
}
match cfg.compact_at {
Some(n) => agent.set_compact_at(n),
None => agent.fit_window(hx_ai::models::window(&models, agent.model())),
}
agent.compact_at = cfg.compact_at.unwrap_or_else(|| {
hx_ai::models::compact_threshold(
hx_ai::models::window(&models, &agent.model),
agent.max_tokens,
)
});
for e in &cfg.extensions {
let x = hx_core::ext::Extension::start(e, &cwd)
.await
@@ -546,8 +548,12 @@ async fn main() -> Result<()> {
specs,
files: hx_tui::project_files(&cwd),
session_dir: dir,
models: models.clone(),
make_provider: provider,
switcher: Switcher::new(
kind,
models.clone(),
Box::new(provider),
cfg.compact_at.is_none(),
),
},
)
.await;
@@ -560,13 +566,15 @@ async fn main() -> Result<()> {
let Some(prompt) = cli.print else {
hx_tui::run(hx_tui::Setup {
agent,
kind,
make_provider: Box::new(provider),
switcher: Switcher::new(
kind,
models.clone(),
Box::new(provider),
cfg.compact_at.is_none(),
),
pricing,
specs,
open_sessions,
models: models.clone(),
auto_compact: cfg.compact_at.is_none(),
commands: {
let mcp = mcp.clone();
Box::new(move |name: &str, arg: &str| {
@@ -644,11 +652,11 @@ async fn main() -> Result<()> {
if cli.mode == Mode::Json {
rpc::emit(
&serde_json::json!({"type": "done", "error": r.as_ref().err().map(|e| e.to_string()),
"usage": agent.usage, "session": path}),
"usage": agent.usage(), "session": path}),
);
}
let u = &agent.usage;
let cost = pricing(&agent.model)
let u = &agent.usage();
let cost = pricing(agent.model())
.map(|p| format!(" ~${:.4}", u.cost(&p)))
.unwrap_or_default();
eprintln!(