swarmy 0.2.12

Real-time analytics dashboard for coding agents (Claude Code, Antigravity, Codex): live swarm, history, API-equivalent spend, weekly limits, and a vault for OAuth logins
Documentation
//! Claude Code's hook input: the JSON it passes on stdin to a `PreToolUse` command hook (an
//! anti-corruption layer, like `statusline::input`). Only what the guard uses; every field is
//! optional, and input that cannot be read is empty input.

use serde::Deserialize;

#[derive(Debug, Default, Deserialize)]
pub struct HookInput {
    #[serde(default)]
    pub session_id: Option<String>,
    /// The subagent (or teammate) the hook fires in; none in the session's main thread.
    #[serde(default)]
    pub agent_id: Option<String>,
}

impl HookInput {
    /// Who an answer is for: the session, and the agent in it (`main` for the main thread).
    pub fn who(&self) -> (String, String) {
        (
            safe(self.session_id.as_deref().unwrap_or("none")),
            safe(self.agent_id.as_deref().unwrap_or("main")),
        )
    }
}

pub fn parse(text: &str) -> HookInput {
    serde_json::from_str(text).unwrap_or_default()
}

/// A part of a file name: letters, digits, `-` and `_` only, at most 64 of them.
fn safe(s: &str) -> String {
    let kept: String = s
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
        .take(64)
        .collect();
    if kept.is_empty() { "none".into() } else { kept }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn reads_what_it_needs_and_survives_the_rest() {
        let i = parse(
            r#"{"session_id":"s-1","agent_id":"a1","hook_event_name":"PreToolUse",
                "tool_name":"Bash","tool_input":{"command":"ls"},"cwd":"/x"}"#,
        );
        assert_eq!(i.who(), ("s-1".into(), "a1".into()));
        let main = parse(r#"{"session_id":"s-1"}"#);
        assert_eq!(main.who(), ("s-1".into(), "main".into()));
        assert_eq!(parse("not json").who(), ("none".into(), "main".into()));
    }

    #[test]
    fn ids_cannot_reach_outside_the_guard_dir() {
        let i = parse(r#"{"session_id":"../../etc/passwd","agent_id":"/"}"#);
        assert_eq!(i.who(), ("etcpasswd".into(), "none".into()));
    }
}