Compare commits
8
Commits
v0.1.6
...
6ebf1c3180
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ebf1c3180 | ||
|
|
15d42c279c | ||
|
|
7218d1433b | ||
|
|
fa0ff339f6 | ||
|
|
93bd2693c5 | ||
|
|
e6ac633bf3 | ||
|
|
ed9a9184d0 | ||
|
|
d4c7df4f96 |
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"version": "0.1.5",
|
||||
"version": "0.1.8",
|
||||
"sha256": {
|
||||
"aarch64-apple-darwin": "106d0f4156b687a7a5e1ef8cdd9fcf5f00408afdb320f93e885acdd892b353a1",
|
||||
"x86_64-unknown-linux-musl": "2a86641064692c52c4455b327856be772ff75f93572bdad5836e2a5124f6c3ba"
|
||||
"aarch64-apple-darwin": "89479288ebac3e60981095c5e7ffc0057bd9b8ecb2a108d1e49d41f777bbba45",
|
||||
"x86_64-unknown-linux-musl": "a1517f27cf9f57dbfb8e2072419a47c3b3d7e0c6fa13285e0152e5bd325f8831"
|
||||
}
|
||||
}
|
||||
+22
-11
@@ -474,6 +474,17 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
|
||||
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
|
||||
/// `codemode` from config: also offer the codemode tool to claude.
|
||||
static CODEMODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
/// claude_cli sees only the `hx mcp-serve` tools: MCP is script-only with codemode, else `mcp__hx__*`.
|
||||
fn via() -> hx_mcp::Via {
|
||||
match (
|
||||
CLAUDE_CLI.get().is_some(),
|
||||
CODEMODE.get().copied().unwrap_or(false),
|
||||
) {
|
||||
(true, true) => hx_mcp::Via::Codemode,
|
||||
(true, false) => hx_mcp::Via::ClaudeCli,
|
||||
_ => hx_mcp::Via::Direct,
|
||||
}
|
||||
}
|
||||
/// `claude_cli` from config: extra args for the `claude` binary.
|
||||
static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
|
||||
|
||||
@@ -749,21 +760,21 @@ async fn main() -> Result<()> {
|
||||
.permit
|
||||
.set(hx_core::approve::remote_permit(p.into()));
|
||||
}
|
||||
// claude keeps its own tools; hx's MCP servers are added directly, or only through `codemode`
|
||||
let cfg = Config::load(&cwd)?;
|
||||
let mut servers =
|
||||
hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?;
|
||||
// a server entry that is hx itself would spawn mcp-serve recursively
|
||||
servers.retain(|_, s| !s.args.iter().any(|a| a == "mcp-serve"));
|
||||
let hub = Arc::new(hx_mcp::Hub::start(servers).await);
|
||||
if std::env::var_os("HX_CODEMODE").is_some() {
|
||||
// claude keeps its own tools; only `codemode` is added, and scripts may call the hx tools
|
||||
let cfg = Config::load(&cwd)?;
|
||||
let hub = Arc::new(
|
||||
hx_mcp::Hub::start(
|
||||
hx_mcp::config::load(&context::hx_home(), &cwd)
|
||||
.map_err(anyhow::Error::msg)?,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
let mut all = hx_tools::registry();
|
||||
hx_tools::web::register(&mut all, web_search(&cfg));
|
||||
hub.register(&mut all);
|
||||
all.add(hx_lens::LspTool::new(mgr.clone()));
|
||||
reg.add(hx_tools::codemode::Codemode::new(&all));
|
||||
} else {
|
||||
hub.register(&mut reg);
|
||||
}
|
||||
hx_mcp::serve::serve(
|
||||
reg,
|
||||
@@ -917,7 +928,7 @@ async fn main() -> Result<()> {
|
||||
mcp.wait_all(std::time::Duration::from_secs(1)).await;
|
||||
trace("mcp");
|
||||
mcp.register(&mut agent.tools);
|
||||
let idx = mcp.system_prompt();
|
||||
let idx = mcp.system_prompt_for(via());
|
||||
if !idx.is_empty() {
|
||||
agent.append_system(&idx);
|
||||
}
|
||||
@@ -1151,7 +1162,7 @@ async fn main() -> Result<()> {
|
||||
.unwrap_or(context::DEFAULT_PROMPT),
|
||||
context::prompt_tail(&context::hx_home(), &cwd)
|
||||
);
|
||||
let idx = mcp.system_prompt();
|
||||
let idx = mcp.system_prompt_for(via());
|
||||
if !idx.is_empty() {
|
||||
sys.push_str("\n\n");
|
||||
sys.push_str(&idx);
|
||||
|
||||
@@ -417,6 +417,10 @@ impl Agent {
|
||||
/// An abort ends this run only; the next `run` starts un-aborted.
|
||||
pub async fn run(&mut self, prompt: impl Into<String>) -> Result<Message, Error> {
|
||||
let r = self.run_inner(prompt.into()).await;
|
||||
self.emit(Event::RunEnd {
|
||||
aborted: matches!(r, Err(Error::Aborted)),
|
||||
})
|
||||
.await;
|
||||
self.abort_tx
|
||||
.send_if_modified(|a| std::mem::replace(a, false));
|
||||
r
|
||||
|
||||
@@ -26,6 +26,10 @@ pub enum Event {
|
||||
TurnEnd,
|
||||
/// Model finished without tool calls. `Inject` makes the agent continue.
|
||||
AgentEnd,
|
||||
/// A run ended for any reason (done, error, abort). Fires after the last turn.
|
||||
RunEnd {
|
||||
aborted: bool,
|
||||
},
|
||||
/// Streaming output from the model (UI).
|
||||
Stream(StreamEvent),
|
||||
Message(Message),
|
||||
@@ -57,6 +61,7 @@ impl Event {
|
||||
"content": content, "is_error": is_error}),
|
||||
Event::TurnEnd => json!({"type": "turn_end"}),
|
||||
Event::AgentEnd => json!({"type": "agent_end"}),
|
||||
Event::RunEnd { aborted } => json!({"type": "run_end", "aborted": aborted}),
|
||||
Event::Stream(e) => json!({"type": "stream", "event": e}),
|
||||
Event::Message(m) => json!({"type": "message", "message": m}),
|
||||
Event::Info(t) => json!({"type": "info", "text": t}),
|
||||
@@ -74,6 +79,7 @@ impl Event {
|
||||
Event::ToolResult { .. } => "tool_result",
|
||||
Event::TurnEnd => "turn_end",
|
||||
Event::AgentEnd => "agent_end",
|
||||
Event::RunEnd { .. } => "run_end",
|
||||
Event::Stream(_) => "stream",
|
||||
Event::Message(_) => "message",
|
||||
Event::Info(_) => "info",
|
||||
|
||||
@@ -370,6 +370,21 @@ pub fn format_findings(v: &[&Finding]) -> String {
|
||||
#[async_trait]
|
||||
impl Hook for Lens {
|
||||
async fn on(&self, ev: &Event) -> HookAction {
|
||||
// Anything that changed on disk during the run is ours (bash, subagents, an aborted
|
||||
// tool that already wrote), not the user's. Re-baseline the read-guard too.
|
||||
if matches!(ev, Event::TurnEnd | Event::AgentEnd | Event::RunEnd { .. }) {
|
||||
for p in self.watch.scan(&self.seq, Source::Agent) {
|
||||
if let Some(st) = self.state.get() {
|
||||
st.mark_seen(&p);
|
||||
}
|
||||
if matches!(ev, Event::RunEnd { aborted: true }) {
|
||||
self.note(format!(
|
||||
"{} changed by your aborted previous run (may be half-done); re-read it",
|
||||
self.rel(&p)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
match ev {
|
||||
Event::ToolResult {
|
||||
name,
|
||||
@@ -395,7 +410,7 @@ impl Hook for Lens {
|
||||
}
|
||||
Event::TurnStart => {
|
||||
// edits made outside hx while the model was thinking
|
||||
self.watch.scan(&self.seq);
|
||||
self.watch.scan(&self.seq, Source::External);
|
||||
HookAction::Continue
|
||||
}
|
||||
Event::TurnEnd if self.mode == Mode::TurnEnd => self
|
||||
|
||||
@@ -10,6 +10,8 @@ pub enum Source {
|
||||
AgentEdit,
|
||||
Format,
|
||||
Autofix,
|
||||
/// Changed during an agent run by something other than a file tool (bash, subagent, aborted tool).
|
||||
Agent,
|
||||
External,
|
||||
}
|
||||
|
||||
|
||||
@@ -30,14 +30,14 @@ impl Watch {
|
||||
}
|
||||
|
||||
/// Record files whose mtime changed since last seen. Returns them.
|
||||
pub fn scan(&self, seq: &SeqLog) -> Vec<PathBuf> {
|
||||
pub fn scan(&self, seq: &SeqLog, src: Source) -> Vec<PathBuf> {
|
||||
let mut map = self.mtimes.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut changed = vec![];
|
||||
for (p, old) in map.iter_mut() {
|
||||
let now = mtime(p);
|
||||
if now != *old {
|
||||
*old = now;
|
||||
seq.bump(p, Source::External);
|
||||
seq.bump(p, src);
|
||||
changed.push(p.clone());
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ impl Watch {
|
||||
let mut t = tokio::time::interval(every);
|
||||
loop {
|
||||
t.tick().await;
|
||||
w.scan(&seq);
|
||||
w.scan(&seq, Source::External);
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -71,16 +71,31 @@ mod tests {
|
||||
std::fs::write(&f, "1").unwrap();
|
||||
let (w, s) = (Watch::default(), SeqLog::default());
|
||||
w.track(f.clone());
|
||||
assert!(w.scan(&s).is_empty());
|
||||
assert!(w.scan(&s, Source::External).is_empty());
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
std::fs::write(&f, "2").unwrap();
|
||||
assert_eq!(w.scan(&s), std::slice::from_ref(&f));
|
||||
assert!(w.scan(&s).is_empty());
|
||||
assert_eq!(w.scan(&s, Source::External), std::slice::from_ref(&f));
|
||||
assert!(w.scan(&s, Source::External).is_empty());
|
||||
assert_eq!(s.project(), 1);
|
||||
// our own write: re-baseline, no bump
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
std::fs::write(&f, "3").unwrap();
|
||||
w.track(f);
|
||||
assert!(w.scan(&s).is_empty());
|
||||
assert!(w.scan(&s, Source::External).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_scan_not_external() {
|
||||
let d = tempfile::tempdir().unwrap();
|
||||
let f = d.path().join("a.rs");
|
||||
std::fs::write(&f, "1").unwrap();
|
||||
let (w, s) = (Watch::default(), SeqLog::default());
|
||||
w.track(f.clone());
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
std::fs::write(&f, "2").unwrap(); // e.g. bash sed, or an aborted edit
|
||||
assert_eq!(w.scan(&s, Source::Agent).len(), 1);
|
||||
// later turn start: nothing left to call external
|
||||
assert!(w.scan(&s, Source::External).is_empty());
|
||||
assert!(s.since(0).len() == 1);
|
||||
}
|
||||
}
|
||||
@@ -135,6 +135,16 @@ async fn load(cfg: &Server) -> (State, Option<Arc<Client>>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// How the model reaches the lazy MCP tools.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Via {
|
||||
Direct,
|
||||
/// claude_cli + codemode: only inside `codemode` scripts
|
||||
Codemode,
|
||||
/// claude_cli without codemode: `mcp__hx__*` tools served by `hx mcp-serve`
|
||||
ClaudeCli,
|
||||
}
|
||||
|
||||
impl Hub {
|
||||
/// Connect to all servers in parallel and wait for every one (CLI commands, tests).
|
||||
pub async fn start(cfgs: BTreeMap<String, Server>) -> Self {
|
||||
@@ -395,10 +405,18 @@ impl Hub {
|
||||
}
|
||||
|
||||
pub fn system_prompt(&self) -> String {
|
||||
self.system_prompt_for(Via::Direct)
|
||||
}
|
||||
|
||||
pub fn system_prompt_for(&self, via: Via) -> String {
|
||||
let idx = self.index();
|
||||
let mut s = String::new();
|
||||
if !idx.is_empty() {
|
||||
s.push_str("# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n");
|
||||
s.push_str(match via {
|
||||
Via::Codemode => "# MCP tools\nNot top-level tools: reach them only through `codemode`. Inside a script: `await tools.mcp_call({server, tool, arguments})`; `await tools.mcp_describe({server, tool})` gives the argument schema (also `tools.mcp_read({server, uri})`).\n",
|
||||
Via::ClaudeCli => "# MCP tools\nCall with the `mcp__hx__mcp_call` tool (server, tool, arguments). Use `mcp__hx__mcp_describe` first to get the argument schema (`mcp__hx__mcp_read` reads resources).\n",
|
||||
Via::Direct => "# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n",
|
||||
});
|
||||
for (_, b) in idx {
|
||||
s.push_str(&b);
|
||||
}
|
||||
|
||||
@@ -36,6 +36,16 @@ async fn stdio_lazy_index_describe_call() {
|
||||
let sp = h.system_prompt();
|
||||
assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}");
|
||||
assert!(!sp.contains("second line"));
|
||||
let cm = h.system_prompt_for(crate::Via::Codemode);
|
||||
assert!(
|
||||
cm.contains("only through `codemode`") && cm.contains("- f.echo:"),
|
||||
"{cm}"
|
||||
);
|
||||
assert!(!sp.contains("codemode"));
|
||||
assert!(
|
||||
h.system_prompt_for(crate::Via::ClaudeCli)
|
||||
.contains("mcp__hx__mcp_call")
|
||||
);
|
||||
assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests."));
|
||||
let st = h.status();
|
||||
assert!(
|
||||
|
||||
@@ -86,7 +86,10 @@ impl Codemode {
|
||||
let mut names = BTreeMap::new();
|
||||
let mut list = String::new();
|
||||
let mut hidden = 0;
|
||||
for s in reg.specs() {
|
||||
// MCP tools first so builtins cannot push them out of the listing budget
|
||||
let mut specs = reg.specs();
|
||||
specs.sort_by_key(|s| !(s.name.contains("__") || s.name.starts_with("mcp_")));
|
||||
for s in specs {
|
||||
let id = ident(&s.name);
|
||||
let first = s.description.split(". ").next().unwrap_or("").trim();
|
||||
let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema));
|
||||
|
||||
Reference in new issue
Block a user