- 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.
62 lines
1.7 KiB
Rust
62 lines
1.7 KiB
Rust
//! hx core: tools, hooks, agent loop, sessions, context.
|
|
|
|
pub mod agent;
|
|
pub mod command;
|
|
pub mod compact;
|
|
pub mod context;
|
|
pub mod ext;
|
|
pub mod hook;
|
|
pub mod mask;
|
|
pub mod session;
|
|
pub mod stats;
|
|
pub mod switch;
|
|
pub mod tool;
|
|
|
|
use std::collections::HashMap;
|
|
use std::path::PathBuf;
|
|
use std::sync::Mutex;
|
|
use std::time::SystemTime;
|
|
|
|
/// Asks the user a yes/no question; resolves to their answer.
|
|
pub type Ask = std::sync::Arc<
|
|
dyn Fn(String) -> std::pin::Pin<Box<dyn std::future::Future<Output = bool> + Send>>
|
|
+ Send
|
|
+ Sync,
|
|
>;
|
|
|
|
/// Cross-tool shared state.
|
|
#[derive(Default)]
|
|
pub struct State {
|
|
/// File -> mtime when the agent last read or wrote it. Used by the read-guard.
|
|
pub seen: Mutex<HashMap<PathBuf, Option<SystemTime>>>,
|
|
/// Set by interactive frontends; None = nobody to ask (print mode, mcp-serve).
|
|
pub ask: std::sync::OnceLock<Ask>,
|
|
}
|
|
|
|
impl State {
|
|
/// Ask the user; false when no interactive frontend is attached.
|
|
pub async fn confirm(&self, question: String) -> bool {
|
|
match self.ask.get() {
|
|
Some(a) => a(question).await,
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
pub fn mark_seen(&self, p: &std::path::Path) {
|
|
let m = std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
|
self.seen
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.insert(p.to_path_buf(), m);
|
|
}
|
|
/// True when the file was read/written by the agent and not changed on disk since.
|
|
pub fn is_fresh(&self, p: &std::path::Path) -> bool {
|
|
let now = std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
|
self.seen
|
|
.lock()
|
|
.unwrap_or_else(|e| e.into_inner())
|
|
.get(p)
|
|
.is_some_and(|m| *m == now)
|
|
}
|
|
}
|