diff --git a/CHANGELOG.md b/CHANGELOG.md index bab2b21..0c90c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Incrementally ratcheted clippy deny list (14 lints and counting) ### Fixed +- Hook responses are serialized in the calling agent's own wire format again — the CLI parsed stdin directly instead of reading it through polyhook, discarding the detected caller so every response used the legacy Claude Code shape (which also terminated the whole Claude Code session on a `PreToolUse` block instead of denying the single tool call) - `ack.sh` exits 0 with a message when the session is already complete - Unknown CLI arguments now exit 1 with a usage hint instead of silently doing nothing - `on_tool` is now optional in `config.toml` (omit to match any tool) diff --git a/core/src/bin/main.rs b/core/src/bin/main.rs index 97f54a2..ec8cd9e 100644 --- a/core/src/bin/main.rs +++ b/core/src/bin/main.rs @@ -7,7 +7,6 @@ use std::io::{self, Read}; use std::path::{Path, PathBuf}; use std::process; -use polyhook::parse; use steplock::{run, HookEvent, HookResponse}; fn main() { @@ -183,12 +182,12 @@ fn run_clean(dir: &Path) -> io::Result<()> { /// Parse the hook event from `reader`, run the gate, and return the polyhook response. /// Returns `Err(message)` when input is unreadable or the gate engine fails. fn run_app(mut reader: impl Read, repo_root: &Path) -> Result { - let mut bytes = Vec::new(); - reader - .read_to_end(&mut bytes) - .map_err(|e| format!("steplock: failed to read hook input: {e}"))?; - - let ph_event = parse::parse_event(&bytes) + // Must go through `polyhook::read_from` rather than `parse::parse_event`: + // reading is what records the detected caller and event type that + // `polyhook::respond` later needs to serialise the response in the calling + // agent's own wire format. Parsing the bytes directly leaves that context + // unset, so every response falls back to the legacy Claude Code shape. + let ph_event = polyhook::read_from(&mut reader) .map_err(|e| format!("steplock: failed to read hook input: {e}"))?; let event = polyhook_to_hook_event(ph_event); @@ -230,6 +229,7 @@ fn find_repo_root_from(start: &Path) -> Option { #[allow(clippy::unwrap_used)] mod tests { use super::*; + use polyhook::parse; use std::fs; use tempfile::TempDir; @@ -300,6 +300,54 @@ reset = "session" assert!(matches!(resp, polyhook::HookResponse::BlockResponse(_))); } + /// Serialize through the same path `run_hook` uses, so the assertion covers + /// the caller/event context that `run_app` is responsible for recording. + fn respond_json(resp: &polyhook::HookResponse) -> serde_json::Value { + let mut buf = Vec::new(); + polyhook::respond_to(&mut buf, resp).unwrap(); + serde_json::from_slice(&buf).unwrap() + } + + #[test] + fn block_uses_claude_code_pre_tool_use_deny_not_session_block() { + let tmp = TempDir::new().unwrap(); + setup_checklist(tmp.path()); + let stdin = claude_stdin("git push origin main", "s1"); + let resp = run_app(stdin.as_bytes(), tmp.path()).unwrap(); + + let json = respond_json(&resp); + // Top-level `decision: "block"` terminates the whole Claude Code session; + // a PreToolUse gate must deny only the single tool call. + assert!(json.get("decision").is_none(), "got session-killing {json}"); + let decision = json + .get("hookSpecificOutput") + .and_then(|o| o.get("permissionDecision")) + .and_then(|d| d.as_str()); + assert_eq!(decision, Some("deny"), "got {json}"); + } + + #[test] + fn block_is_serialized_in_the_calling_agents_format() { + let tmp = TempDir::new().unwrap(); + setup_checklist(tmp.path()); + // Cline wire shape — detected via `type` + `toolName`. + let stdin = serde_json::json!({ + "type": "beforeToolUse", + "toolName": "bash", + "args": { "command": "git push origin main" }, + "session": "cl1" + }) + .to_string(); + let resp = run_app(stdin.as_bytes(), tmp.path()).unwrap(); + + let json = respond_json(&resp); + assert_eq!( + json.get("approved").and_then(serde_json::Value::as_bool), + Some(false), + "got {json}" + ); + } + #[test] fn run_app_error_on_invalid_input() { let tmp = TempDir::new().unwrap();