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

+125
View File
@@ -0,0 +1,125 @@
//! Append-only JSONL session tree. Each entry has `id` + `parent`; the head is the last appended id.
//! Forking = appending with an older parent. Resume = walk parents from the head.
use hx_ai::Message;
use serde::{Deserialize, Serialize};
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entry {
pub id: u64,
pub parent: Option<u64>,
pub message: Message,
}
pub struct Session {
path: PathBuf,
entries: Vec<Entry>,
head: Option<u64>,
}
impl Session {
/// Open (or create) a session file; head = last entry.
pub fn open(path: impl AsRef<Path>) -> std::io::Result<Self> {
let path = path.as_ref().to_path_buf();
if let Some(d) = path.parent() {
std::fs::create_dir_all(d)?;
}
let mut entries = vec![];
if let Ok(f) = std::fs::File::open(&path) {
for l in std::io::BufReader::new(f).lines() {
let l = l?;
// skip a torn last line from a crash rather than losing the session
if let Ok(e) = serde_json::from_str::<Entry>(&l) {
entries.push(e);
}
}
}
let head = entries.last().map(|e| e.id);
Ok(Self {
path,
entries,
head,
})
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn head(&self) -> Option<u64> {
self.head
}
pub fn append(&mut self, message: Message) -> std::io::Result<u64> {
let id = self.entries.last().map_or(0, |e| e.id + 1);
let e = Entry {
id,
parent: self.head,
message,
};
let mut f = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&self.path)?;
writeln!(
f,
"{}",
serde_json::to_string(&e).map_err(std::io::Error::other)?
)?;
self.entries.push(e);
self.head = Some(id);
Ok(id)
}
/// Move head to `id` so the next append branches from it.
pub fn fork(&mut self, id: u64) -> bool {
let ok = self.entries.iter().any(|e| e.id == id);
if ok {
self.head = Some(id);
}
ok
}
/// Messages on the path root..=head.
pub fn messages(&self) -> Vec<Message> {
let mut out = vec![];
let mut cur = self.head;
while let Some(id) = cur {
let Some(e) = self.entries.iter().find(|e| e.id == id) else {
break;
};
out.push(e.message.clone());
cur = e.parent;
}
out.reverse();
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn branch_and_resume() {
let d = tempfile::tempdir().unwrap();
let p = d.path().join("s/a.jsonl");
let mut s = Session::open(&p).unwrap();
let a = s.append(Message::user("a")).unwrap();
s.append(Message::user("b")).unwrap();
assert!(s.fork(a));
s.append(Message::user("c")).unwrap();
let txt = |s: &Session| s.messages().iter().map(|m| m.text()).collect::<Vec<_>>();
assert_eq!(txt(&s), ["a", "c"]);
std::fs::OpenOptions::new()
.append(true)
.open(&p)
.unwrap()
.write_all(b"{torn")
.unwrap();
let s2 = Session::open(&p).unwrap();
assert_eq!(txt(&s2), ["a", "c"]);
assert!(!Session::open(&p).unwrap().fork(99));
}
}