M1: agent loop, tools, sessions, context, skills
ci / check (push) Canceled after 0s

- hx-core: Tool trait + registry (schema validation, parallel calls), event bus with Block/Mutate/Inject hooks,
  agent loop (abort, max turns, AgentEnd inject), JSONL session tree (fork, resume, torn-line tolerant),
  AGENTS.md/CLAUDE.md hierarchy, skills index (bodies on demand)
- hx-tools: read (ranges, binary, images, caps), write/edit (exact match, ambiguity, CRLF, diff, read-guard), bash (merged output, timeout kills process group, tail truncation to temp file)
- hx-cli: -p runs full agent loop, --session / --continue, Ctrl-C abort, HX_BASE_URL override
- tests/e2e.sh: scripted fake Anthropic server drives write -> bash -> answer

Closes #6, closes #7, closes #8, closes #9, closes #10, closes #11, closes #12, closes #13, closes #14
This commit is contained in:
hermes committed 2026-09-30 09:36:16 +00:00
1 parent da9d5d1ae0
commit 21d4d2bb3d
15 files changed
+1752 -52

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
//! Event bus and hooks. Lens, MCP and extensions all plug in here.
use async_trait::async_trait;
use hx_ai::{Message, StreamEvent};
use serde_json::Value;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum Event {
SessionStart,
TurnStart,
/// Before a tool runs. `Block` or `Mutate` apply.
ToolCall {
id: String,
name: String,
args: Value,
},
ToolResult {
id: String,
name: String,
args: Value,
content: String,
is_error: bool,
},
/// After all tools of a turn ran. `Inject` adds a user message before the next request.
TurnEnd,
/// Model finished without tool calls. `Inject` makes the agent continue.
AgentEnd,
/// Streaming output from the model (UI).
Stream(StreamEvent),
Message(Message),
}
#[derive(Debug, Clone, PartialEq)]
pub enum HookAction {
Continue,
Block(String),
Mutate(Value),
Inject(String),
}
#[async_trait]
pub trait Hook: Send + Sync {
async fn on(&self, ev: &Event) -> HookAction;
}
#[derive(Default, Clone)]
pub struct Bus {
hooks: Vec<Arc<dyn Hook>>,
}
/// Aggregate outcome of dispatching one event to all hooks.
#[derive(Debug, Default, PartialEq)]
pub struct Outcome {
pub blocked: Option<String>,
pub args: Option<Value>,
pub inject: Vec<String>,
}
impl Bus {
pub fn add(&mut self, h: impl Hook + 'static) -> &mut Self {
self.hooks.push(Arc::new(h));
self
}
/// Hooks run in registration order. First `Block` wins; `Mutate` is visible to later hooks.
pub async fn emit(&self, ev: Event) -> Outcome {
let mut ev = ev;
let mut o = Outcome::default();
for h in &self.hooks {
match h.on(&ev).await {
HookAction::Continue => {}
HookAction::Block(r) => {
o.blocked = Some(r);
break;
}
HookAction::Mutate(v) => {
if let Event::ToolCall { args, .. } = &mut ev {
*args = v.clone();
o.args = Some(v);
}
}
HookAction::Inject(m) => o.inject.push(m),
}
}
o
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
struct NoRm;
#[async_trait]
impl Hook for NoRm {
async fn on(&self, ev: &Event) -> HookAction {
match ev {
Event::ToolCall { args, .. }
if args["cmd"].as_str().is_some_and(|c| c.contains("rm -rf")) =>
{
HookAction::Block("no".into())
}
Event::ToolCall { args, .. } if args["cmd"] == "ls" => {
HookAction::Mutate(json!({"cmd": "ls -la"}))
}
Event::TurnEnd => HookAction::Inject("diag".into()),
_ => HookAction::Continue,
}
}
}
#[tokio::test]
async fn block_mutate_inject() {
let mut b = Bus::default();
b.add(NoRm);
let call = |c: &str| Event::ToolCall {
id: "1".into(),
name: "bash".into(),
args: json!({"cmd": c}),
};
assert_eq!(
b.emit(call("rm -rf /")).await.blocked.as_deref(),
Some("no")
);
assert_eq!(
b.emit(call("ls")).await.args,
Some(json!({"cmd": "ls -la"}))
);
assert_eq!(b.emit(Event::TurnEnd).await.inject, ["diag"]);
assert_eq!(b.emit(Event::SessionStart).await, Outcome::default());
}
}