Skip to main content

safe_chains/targets/
droid.rs

1use std::path::{Path, PathBuf};
2
3use serde::Deserialize;
4use serde_json::{Map, Value, json};
5
6use super::{HookFormat, HookInput, HookResponse, InstallOutcome, ParseError, Target, allow_reason};
7use crate::verdict::Verdict;
8
9pub struct DroidTarget;
10
11impl Target for DroidTarget {
12    fn name(&self) -> &'static str {
13        "droid"
14    }
15
16    fn display_name(&self) -> &'static str {
17        "Factory Droid"
18    }
19
20    fn detect_paths(&self, home: &Path) -> Vec<PathBuf> {
21        vec![home.join(".factory")]
22    }
23
24    fn install(&self, home: &Path) -> Result<InstallOutcome, String> {
25        let dir = home.join(".factory");
26        if !dir.exists() {
27            return Ok(InstallOutcome::Skipped {
28                reason: format!(
29                    "~/.factory not found at {} (Factory Droid not installed)",
30                    dir.display()
31                ),
32            });
33        }
34
35        let path = dir.join("settings.json");
36        // Droid docs require absolute paths for hook commands. We
37        // discover the absolute path of the running binary and embed
38        // it in the config; falls back to bare "safe-chains hook
39        // droid" if discovery fails (and the install message warns).
40        let resolved = std::env::current_exe()
41            .ok()
42            .and_then(|p| p.canonicalize().ok())
43            .map(|p| format!("{} hook droid", p.display()))
44            .unwrap_or_else(|| "safe-chains hook droid".to_string());
45        let binary = resolved.as_str();
46
47        if path.exists() {
48            let contents = std::fs::read_to_string(&path)
49                .map_err(|e| format!("Could not read {}: {e}", path.display()))?;
50            let mut settings: Value = serde_json::from_str(&contents)
51                .map_err(|e| format!("Could not parse {}: {e}", path.display()))?;
52
53            if has_safe_chains_hook(&settings) {
54                return Ok(InstallOutcome::AlreadyConfigured { path });
55            }
56
57            add_hook(&mut settings, binary);
58            let output = serde_json::to_string_pretty(&settings).expect("serializing valid JSON");
59            std::fs::write(&path, format!("{output}\n"))
60                .map_err(|e| format!("Could not write {}: {e}", path.display()))?;
61            Ok(InstallOutcome::Installed { path })
62        } else {
63            let mut settings = Value::Object(Map::new());
64            add_hook(&mut settings, binary);
65            let output = serde_json::to_string_pretty(&settings).expect("serializing valid JSON");
66            std::fs::write(&path, format!("{output}\n"))
67                .map_err(|e| format!("Could not write {}: {e}", path.display()))?;
68            Ok(InstallOutcome::Installed { path })
69        }
70    }
71
72    fn hook_format(&self) -> Option<&dyn HookFormat> {
73        Some(&DroidHookFormat)
74    }
75}
76
77struct DroidHookFormat;
78
79#[derive(Deserialize)]
80struct ToolInput {
81    command: String,
82}
83
84#[derive(Deserialize)]
85struct DroidHookEnvelope {
86    tool_input: ToolInput,
87    #[serde(default)]
88    cwd: Option<String>,
89}
90
91impl HookFormat for DroidHookFormat {
92    fn parse_input(&self, stdin: &str) -> Result<HookInput, ParseError> {
93        let envelope: DroidHookEnvelope = serde_json::from_str(stdin).map_err(|e| ParseError {
94            message: e.to_string(),
95        })?;
96        Ok(HookInput {
97            command: envelope.tool_input.command,
98            cwd: envelope.cwd,
99        })
100    }
101
102    fn render_response(&self, verdict: Verdict) -> HookResponse {
103        if verdict.is_allowed() {
104            let reason = allow_reason(verdict);
105            // Droid mirrors Claude Code's hookSpecificOutput envelope.
106            let body = json!({
107                "hookSpecificOutput": {
108                    "hookEventName": "PreToolUse",
109                    "permissionDecision": "allow",
110                    "permissionDecisionReason": reason,
111                }
112            });
113            HookResponse {
114                stdout: serde_json::to_string(&body).unwrap_or_default(),
115                exit_code: 0,
116            }
117        } else {
118            HookResponse {
119                stdout: String::new(),
120                exit_code: 0,
121            }
122        }
123    }
124}
125
126fn hook_entry(binary: &str) -> Value {
127    // Droid's bash tool name is `Execute`, not `Bash`. timeout is in
128    // seconds (different from Qwen/Gemini ms).
129    json!({
130        "matcher": "Execute",
131        "hooks": [{
132            "type": "command",
133            "command": binary,
134            "timeout": 60,
135        }]
136    })
137}
138
139fn has_safe_chains_hook(settings: &Value) -> bool {
140    settings
141        .get("hooks")
142        .and_then(|h| h.get("PreToolUse"))
143        .and_then(|arr| arr.as_array())
144        .is_some_and(|entries| {
145            entries.iter().any(|entry| {
146                entry
147                    .get("hooks")
148                    .and_then(|h| h.as_array())
149                    .is_some_and(|hooks| {
150                        hooks.iter().any(|hook| {
151                            hook.get("command")
152                                .and_then(|c| c.as_str())
153                                .is_some_and(|cmd| cmd.contains("safe-chains"))
154                        })
155                    })
156            })
157        })
158}
159
160fn add_hook(settings: &mut Value, binary: &str) {
161    if !settings.is_object() {
162        *settings = json!({});
163    }
164    let Some(obj) = settings.as_object_mut() else {
165        unreachable!("settings was just set to an object");
166    };
167    let hooks = obj
168        .entry("hooks")
169        .or_insert_with(|| json!({}))
170        .as_object_mut()
171        .expect("hooks key was created above as an object");
172    let pre_tool_use = hooks
173        .entry("PreToolUse")
174        .or_insert_with(|| json!([]))
175        .as_array_mut()
176        .expect("PreToolUse was created above as an array");
177    pre_tool_use.push(hook_entry(binary));
178}
179
180#[cfg(test)]
181mod tests {
182    use super::*;
183    use crate::verdict::SafetyLevel;
184
185    fn target() -> DroidTarget {
186        DroidTarget
187    }
188
189    /// Verbatim shape from the Factory Droid hooks reference. Note
190    /// tool_name is "Execute", not "Bash".
191    const DROID_DOCS_SAMPLE: &str = r#"{
192        "session_id": "abc123",
193        "transcript_path": "/Users/me/.factory/projects/p/uuid.jsonl",
194        "cwd": "/Users/me/project",
195        "permission_mode": "off",
196        "hook_event_name": "PreToolUse",
197        "tool_name": "Execute",
198        "tool_input": {"command": "ls -la"}
199    }"#;
200
201    #[test]
202    fn install_no_factory_dir_skips() {
203        let dir = tempfile::tempdir().unwrap();
204        let outcome = target().install(dir.path()).unwrap();
205        assert!(matches!(outcome, InstallOutcome::Skipped { .. }));
206    }
207
208    #[test]
209    fn install_creates_settings_file() {
210        let dir = tempfile::tempdir().unwrap();
211        std::fs::create_dir(dir.path().join(".factory")).unwrap();
212        let outcome = target().install(dir.path()).unwrap();
213        assert!(matches!(outcome, InstallOutcome::Installed { .. }));
214        let contents = std::fs::read_to_string(dir.path().join(".factory/settings.json")).unwrap();
215        let settings: Value = serde_json::from_str(&contents).unwrap();
216        assert!(has_safe_chains_hook(&settings));
217    }
218
219    #[test]
220    fn install_uses_execute_matcher_not_bash() {
221        // Droid's bash tool is `Execute` — wiring a `Bash` matcher
222        // wouldn't fire on shell calls.
223        let dir = tempfile::tempdir().unwrap();
224        std::fs::create_dir(dir.path().join(".factory")).unwrap();
225        target().install(dir.path()).unwrap();
226        let contents = std::fs::read_to_string(dir.path().join(".factory/settings.json")).unwrap();
227        assert!(contents.contains("\"matcher\": \"Execute\""));
228    }
229
230    #[test]
231    fn install_uses_absolute_path_to_binary() {
232        // Droid docs explicitly require absolute paths for hook
233        // commands. We resolve via env::current_exe.
234        let dir = tempfile::tempdir().unwrap();
235        std::fs::create_dir(dir.path().join(".factory")).unwrap();
236        target().install(dir.path()).unwrap();
237        let contents = std::fs::read_to_string(dir.path().join(".factory/settings.json")).unwrap();
238        let settings: Value = serde_json::from_str(&contents).unwrap();
239        let cmd = settings
240            .pointer("/hooks/PreToolUse/0/hooks/0/command")
241            .and_then(|s| s.as_str())
242            .unwrap_or("");
243        // Either an absolute path or the fallback bare invocation.
244        assert!(
245            cmd.starts_with('/') || cmd == "safe-chains hook droid",
246            "unexpected command: {cmd}",
247        );
248        assert!(cmd.ends_with(" hook droid") || cmd == "safe-chains hook droid");
249    }
250
251    #[test]
252    fn install_idempotent() {
253        let dir = tempfile::tempdir().unwrap();
254        std::fs::create_dir(dir.path().join(".factory")).unwrap();
255        target().install(dir.path()).unwrap();
256        let outcome = target().install(dir.path()).unwrap();
257        assert!(matches!(outcome, InstallOutcome::AlreadyConfigured { .. }));
258    }
259
260    #[test]
261    fn parse_input_extracts_command() {
262        let parsed = DroidHookFormat.parse_input(DROID_DOCS_SAMPLE).unwrap();
263        assert_eq!(parsed.command, "ls -la");
264        assert_eq!(parsed.cwd.as_deref(), Some("/Users/me/project"));
265    }
266
267    #[test]
268    fn parse_input_rejects_garbage() {
269        assert!(DroidHookFormat.parse_input("not json").is_err());
270        assert!(DroidHookFormat.parse_input("{}").is_err());
271    }
272
273    #[test]
274    fn render_response_emits_claude_shaped_envelope() {
275        let r = DroidHookFormat.render_response(Verdict::Allowed(SafetyLevel::Inert));
276        let v: Value = serde_json::from_str(&r.stdout).unwrap();
277        assert_eq!(
278            v.pointer("/hookSpecificOutput/permissionDecision")
279                .and_then(|d| d.as_str()),
280            Some("allow"),
281        );
282    }
283
284    #[test]
285    fn render_response_deny_emits_empty_body() {
286        let r = DroidHookFormat.render_response(Verdict::Denied);
287        assert_eq!(r.stdout, "");
288    }
289}