O5: safety - allowlist guard, credential separation, audit log (#13)

This commit was merged in pull request #13.
This commit is contained in:
hermes committed 2026-11-13 00:09:31 +00:00
1 parent 06662a085f
commit bea07e82bb
9 files changed
+890 -29

No files matched your search

+36
View File
@@ -1,5 +1,6 @@
mod adapter;
mod lock;
mod policy;
mod profile;
mod run;
mod state;
@@ -36,6 +37,11 @@ enum Cmd {
#[arg(long)]
limit: Option<usize>,
},
/// hx extension that enforces a run's tool allowlist (started by hx, not by hand).
#[command(hide = true)]
Guard { policy: PathBuf },
/// Print the audit log, optionally only one run's lines.
Audit { id: Option<String> },
/// List runs, or show one.
Status { id: Option<String> },
/// Print the raw log (hx events, stderr, gate output) of a run.
@@ -136,6 +142,21 @@ fn main() -> Result<std::process::ExitCode> {
});
Ok(code(ok.load(Ordering::SeqCst)))
}
Cmd::Guard { policy } => {
policy::guard(&policy)?;
Ok(0.into())
}
Cmd::Audit { id } => {
let log = std::fs::read_to_string(policy::audit_path()).unwrap_or_default();
let pat = id.map(|i| format!("\"run\":{}", serde_json::json!(i)));
for l in log
.lines()
.filter(|l| pat.as_ref().is_none_or(|p| l.contains(p.as_str())))
{
println!("{l}");
}
Ok(0.into())
}
Cmd::Status { id: Some(id) } => {
println!("{}", serde_json::to_string_pretty(&state::Run::load(&id)?)?);
Ok(0.into())
@@ -176,7 +197,22 @@ fn process(
src: Option<&dyn adapter::Source>,
) -> Result<state::Run> {
let mut r = run::new_run(t, repo, profile)?;
if p.allow.is_none() && !p.sandbox {
eprintln!(
"warning: profile {profile} has no `allow` list and no sandbox: hx may use every tool"
);
}
let id = r.id.clone();
let audit = |ev: &str, d: serde_json::Value| policy::audit(&policy::audit_path(), &id, ev, d);
audit(
"run_start",
serde_json::json!({"profile": profile, "repo": repo, "allow": p.allow, "sandbox": p.sandbox, "yolo": p.yolo, "task": t.id}),
);
run::execute(&mut r, p, t)?;
audit(
"run_end",
serde_json::json!({"status": r.status, "reason": r.reason, "attempts": r.attempts}),
);
if r.status == state::Status::Done {
match adapter::sink(p.sink.as_ref()).and_then(|s| s.publish(t, &r)) {
Ok(note) => r.published = note,