oxicode-sdk 0.66.0

oxicode AI agent SDK — build isolated, multi-agent AI systems
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Shell-command [`HookRunner`] — the reference implementation of port #16.
//!
//! See spec at `docs/superpowers/specs/2026-08-04-hooks-system-design.md`.
//!
//! Each `HookSpec` is compiled at construction time: the `matcher` is split
//! on `|` into one `globset::Glob` per name, all of which are added to a
//! `globset::GlobSet`. At run time we filter by `event` + `tool_name`
//! and execute matching scripts sequentially through `sh -c`.

use std::pin::Pin;
use std::process::Stdio;
use std::time::Duration;

use globset::{Glob, GlobSet, GlobSetBuilder};
use serde::{Deserialize, Serialize};
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tokio::time::timeout;
use tracing::warn;

use crate::ports::{HookContext, HookEvent, HookOutcome, HookRunner, HookSpec};

/// Default per-invocation timeout when `HookSpec::timeout_secs` is `None`.
pub const DEFAULT_HOOK_TIMEOUT_SECS: u64 = 60;

/// Error constructing a [`CommandHookRunner`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HookConfigError(pub String);

impl std::fmt::Display for HookConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for HookConfigError {}

struct MatcherEntry {
    event: HookEvent,
    set: Option<GlobSet>, // None = match all
    timeout: Duration,
    command: String,
}

/// Reference [`HookRunner`] backed by a list of [`HookSpec`]s.
pub struct CommandHookRunner {
    specs: Vec<HookSpec>,
    matchers: Vec<MatcherEntry>,
}

impl std::fmt::Debug for CommandHookRunner {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommandHookRunner")
            .field("spec_count", &self.specs.len())
            .finish()
    }
}

impl CommandHookRunner {
    /// Compile the given specs. Returns an error if any `matcher` is an
    /// invalid glob.
    pub fn new(specs: Vec<HookSpec>) -> Result<Self, HookConfigError> {
        let mut matchers = Vec::with_capacity(specs.len());
        for spec in &specs {
            let set = match &spec.matcher {
                None => None,
                Some(pat) => {
                    let mut builder = GlobSetBuilder::new();
                    for piece in pat.split('|') {
                        let piece = piece.trim();
                        if piece.is_empty() {
                            return Err(HookConfigError(format!(
                                "empty matcher segment in `{}`",
                                pat
                            )));
                        }
                        let glob = Glob::new(piece).map_err(|e| {
                            HookConfigError(format!("invalid glob `{}`: {}", piece, e))
                        })?;
                        builder.add(glob);
                    }
                    Some(
                        builder
                            .build()
                            .map_err(|e| HookConfigError(format!("globset build failed: {}", e)))?,
                    )
                }
            };
            let timeout =
                Duration::from_secs(spec.timeout_secs.unwrap_or(DEFAULT_HOOK_TIMEOUT_SECS));
            matchers.push(MatcherEntry {
                event: spec.event,
                set,
                timeout,
                command: spec.command.clone(),
            });
        }
        Ok(Self { specs, matchers })
    }

    /// Borrow the original specs (read-only).
    pub fn specs(&self) -> &[HookSpec] {
        &self.specs
    }
}

impl HookRunner for CommandHookRunner {
    fn run<'a>(
        &'a self,
        event: HookEvent,
        ctx: &'a HookContext,
    ) -> Pin<Box<dyn Future<Output = HookOutcome> + Send + 'a>> {
        Box::pin(async move {
            let mut outcome = HookOutcome::default();

            for entry in &self.matchers {
                if entry.event != event {
                    continue;
                }
                // Matcher: None = all; Some(set) = tool_name must be is_match.
                let tool_name = ctx.tool_name.as_deref().unwrap_or("");
                if let Some(set) = &entry.set
                    && !set.is_match(tool_name)
                {
                    continue;
                }

                // Run this script. Fail-open: any error → log + continue.
                let script_outcome = run_one(&entry.command, entry.timeout, event, ctx).await;
                if script_outcome.block {
                    outcome.block = true;
                    outcome.reason = script_outcome.reason.or(outcome.reason);
                    // block short-circuits: stop processing further scripts.
                    return outcome;
                }
                if script_outcome.override_content.is_some() {
                    outcome.override_content = script_outcome.override_content;
                }
            }

            outcome
        })
    }
}

/// Execute one hook script. Never panics; never returns `Err`. The result
/// is translated to the script's effect on the agent loop (block / override).
async fn run_one(
    command: &str,
    timeout_dur: Duration,
    event: HookEvent,
    ctx: &HookContext,
) -> HookOutcome {
    let session_cwd = ctx
        .session_cwd
        .as_ref()
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_default();

    let mut child = match Command::new("sh")
        .arg("-c")
        .arg(command)
        .env("OXICODE_HOOK_EVENT", event_to_str(event))
        .env(
            "OXICODE_HOOK_TOOL_NAME",
            ctx.tool_name.as_deref().unwrap_or(""),
        )
        .env(
            "OXICODE_HOOK_SESSION_ID",
            ctx.session_id.as_deref().unwrap_or(""),
        )
        .env("OXICODE_HOOK_SESSION_CWD", &session_cwd)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .kill_on_drop(true)
        .spawn()
    {
        Ok(c) => c,
        Err(e) => {
            warn!(command, error = %e, "hook script failed to spawn (fail-open)");
            return HookOutcome::default();
        }
    };

    // Write the JSON context to stdin.
    let stdin_payload = match serde_json::to_string(ctx) {
        Ok(s) => s,
        Err(e) => {
            warn!(error = %e, "failed to serialise hook context (fail-open)");
            return HookOutcome::default();
        }
    };
    if let Some(mut stdin) = child.stdin.take() {
        if let Err(e) = stdin.write_all(stdin_payload.as_bytes()).await {
            warn!(error = %e, "failed to write hook stdin (fail-open)");
        }
        drop(stdin);
    }

    // Wait with timeout. On timeout, kill the child (kill_on_drop handles
    // the case where the future is dropped).
    let result = timeout(timeout_dur, child.wait_with_output()).await;
    let output = match result {
        Ok(Ok(o)) => o,
        Ok(Err(e)) => {
            warn!(command, error = %e, "hook wait failed (fail-open)");
            return HookOutcome::default();
        }
        Err(_) => {
            warn!(command, ?timeout_dur, "hook timed out (fail-open)");
            return HookOutcome::default();
        }
    };

    // Exit code 2 → block. The optional JSON on stdout can override
    if output.status.code() == Some(2) {
        return HookOutcome {
            block: true,
            reason: extract_reason(&output.stderr),
            override_content: None,
        };
    }

    // Non-2, non-0 → log + pass.
    if !output.status.success()
        && let Some(code) = output.status.code()
    {
        warn!(
            command,
            code,
            stderr = %String::from_utf8_lossy(&output.stderr),
            "hook script exited non-zero (fail-open)"
        );
    }

    // Best-effort: parse stdout JSON for override / reason. Unknown shape
    // is ignored silently (Claude Code permits a wide variety).
    if let Ok(parsed) = serde_json::from_slice::<serde_json::Value>(&output.stdout) {
        let override_content = parsed
            .get("override_content")
            .or_else(|| parsed.get("continue"))
            .and_then(|v| v.as_str())
            .map(String::from);
        let reason = parsed
            .get("reason")
            .or_else(|| parsed.get("message"))
            .and_then(|v| v.as_str())
            .map(String::from);
        return HookOutcome {
            block: false,
            reason,
            override_content,
        };
    }

    HookOutcome::default()
}

fn event_to_str(e: HookEvent) -> &'static str {
    match e {
        HookEvent::PreToolUse => "PreToolUse",
        HookEvent::PostToolUse => "PostToolUse",
        HookEvent::Stop => "Stop",
        HookEvent::SubagentStop => "SubagentStop",
        HookEvent::SessionStart => "SessionStart",
        HookEvent::SessionEnd => "SessionEnd",
        HookEvent::Notification => "Notification",
    }
}

fn extract_reason(stdout: &[u8]) -> Option<String> {
    serde_json::from_slice::<serde_json::Value>(stdout)
        .ok()
        .and_then(|v| {
            v.get("reason")
                .or_else(|| v.get("message"))
                .and_then(|r| r.as_str())
                .map(String::from)
        })
}

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

    fn spec(event: HookEvent, matcher: Option<&str>, command: &str) -> HookSpec {
        HookSpec {
            event,
            matcher: matcher.map(String::from),
            command: command.into(),
            timeout_secs: None,
        }
    }

    #[tokio::test]
    async fn no_match_runs_nothing() {
        let runner =
            CommandHookRunner::new(vec![spec(HookEvent::PreToolUse, Some("bash"), "false")])
                .unwrap();
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("read".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        assert!(!outcome.block);
    }

    #[tokio::test]
    async fn no_matcher_runs_for_any_tool() {
        let runner =
            CommandHookRunner::new(vec![spec(HookEvent::PreToolUse, None, "exit 0")]).unwrap();
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("anything".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        assert!(!outcome.block);
    }

    #[tokio::test]
    async fn exit_2_blocks() {
        let runner = CommandHookRunner::new(vec![spec(
            HookEvent::PreToolUse,
            Some("bash"),
            "echo '{\"reason\":\"nope\"}' >&2; exit 2",
        )])
        .unwrap();
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("bash".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        assert!(outcome.block);
        assert_eq!(outcome.reason.as_deref(), Some("nope"));
    }

    #[tokio::test]
    async fn nonzero_nonzero_2_fails_open() {
        let runner =
            CommandHookRunner::new(vec![spec(HookEvent::PreToolUse, Some("bash"), "exit 1")])
                .unwrap();
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("bash".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        // Exit 1 is NOT a block. Tool should proceed.
        assert!(!outcome.block);
    }

    #[tokio::test]
    async fn pipe_matcher_matches_either() {
        let runner = CommandHookRunner::new(vec![spec(
            HookEvent::PreToolUse,
            Some("bash|write"),
            "exit 2",
        )])
        .unwrap();
        for tool in ["bash", "write"] {
            let ctx = HookContext {
                event: HookEvent::PreToolUse,
                tool_name: Some(tool.into()),
                ..Default::default()
            };
            let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
            assert!(outcome.block, "expected block for tool={tool}");
        }
        // And a non-matching tool passes.
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("read".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        assert!(!outcome.block);
    }

    #[tokio::test]
    async fn stdout_json_overrides_content() {
        let runner = CommandHookRunner::new(vec![spec(
            HookEvent::PostToolUse,
            Some("read"),
            r#"echo '{"override_content":"replaced"}'"#,
        )])
        .unwrap();
        let ctx = HookContext {
            event: HookEvent::PostToolUse,
            tool_name: Some("read".into()),
            tool_result: Some("original".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PostToolUse, &ctx).await;
        assert_eq!(outcome.override_content.as_deref(), Some("replaced"));
    }

    #[tokio::test]
    async fn multiple_matching_scripts_run_sequentially() {
        let runner = CommandHookRunner::new(vec![
            spec(HookEvent::PreToolUse, Some("bash"), "exit 0"),
            spec(HookEvent::PreToolUse, Some("bash"), "exit 2"),
        ])
        .unwrap();
        let ctx = HookContext {
            event: HookEvent::PreToolUse,
            tool_name: Some("bash".into()),
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::PreToolUse, &ctx).await;
        // Second script blocks; first passes. We should see block=true.
        assert!(outcome.block);
    }

    #[tokio::test]
    async fn empty_matcher_segment_errors_at_construction() {
        let bad = vec![spec(HookEvent::PreToolUse, Some("bash||write"), "true")];
        let err = CommandHookRunner::new(bad).unwrap_err();
        assert!(err.0.contains("empty matcher"));
    }

    #[tokio::test]
    async fn invalid_glob_errors_at_construction() {
        let bad = vec![spec(HookEvent::PreToolUse, Some("["), "true")];
        assert!(CommandHookRunner::new(bad).is_err());
    }

    #[tokio::test]
    async fn event_must_match() {
        // A spec for PreToolUse should NOT fire for Stop.
        let runner =
            CommandHookRunner::new(vec![spec(HookEvent::PreToolUse, None, "exit 2")]).unwrap();
        let ctx = HookContext {
            event: HookEvent::Stop,
            ..Default::default()
        };
        let outcome = runner.run(HookEvent::Stop, &ctx).await;
        assert!(!outcome.block);
    }
}