Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion atomic-agent/src/hooks/claude_code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ const HOOK_DEFS: &[(&str, &str, &str)] = &[
fn install_hooks_into(hooks: &mut ClaudeHooks, _settings_path: &Path) -> AgentResult<usize> {
let mut count = 0;
for (_label, matcher, verb) in HOOK_DEFS {
let command = format!("test -d .atomic && {} {} || true", ATOMIC_HOOK_PREFIX, verb);
let command =
crate::hooks::guarded_hook_command(&format!("{} {}", ATOMIC_HOOK_PREFIX, verb));

let matchers = match *verb {
"session-start" => &mut hooks.session_start,
Expand Down
6 changes: 5 additions & 1 deletion atomic-agent/src/hooks/claude_code/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,17 @@ mod tests {
assert!(is_atomic_hook(
"atomic agent hooks claude-code user-prompt-submit"
));
// Guarded format (current)
// Guarded format (legacy, `.atomic`-only)
assert!(is_atomic_hook(
"test -d .atomic && atomic agent hooks claude-code stop || true"
));
assert!(is_atomic_hook(
"test -d .atomic && atomic agent hooks claude-code user-prompt-submit || true"
));
// Sandbox-aware guarded format (current)
assert!(is_atomic_hook(
"test -d .atomic || test -f .atomic-sandbox && atomic agent hooks claude-code stop || true"
));
// Non-atomic commands
assert!(!is_atomic_hook("entire hooks claude-code stop"));
assert!(!is_atomic_hook("some other command"));
Expand Down
21 changes: 10 additions & 11 deletions atomic-agent/src/hooks/codex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,8 @@ fn install_hooks_at(path: &Path, force: bool) -> AgentResult<usize> {

let mut installed = 0;
for spec in CODEX_HOOK_DEFS {
if add_hook(
&mut config.hooks,
spec.event,
spec.command,
spec.status_message,
) {
let command = crate::hooks::guarded_hook_command(spec.command);
if add_hook(&mut config.hooks, spec.event, &command, spec.status_message) {
installed += 1;
}
}
Expand Down Expand Up @@ -615,30 +611,33 @@ struct HookDef {
status_message: Option<&'static str>,
}

/// Codex hook definitions. `command` holds the bare `atomic agent hooks …`
/// invocation; the shell guard is applied by [`crate::hooks::guarded_hook_command`]
/// at install time so the guard stays in one place across all agents.
const CODEX_HOOK_DEFS: &[HookDef] = &[
HookDef {
event: "SessionStart",
command: "test -d .atomic && atomic agent hooks codex session-start || true",
command: "atomic agent hooks codex session-start",
status_message: Some("Atomic: tracking session"),
},
HookDef {
event: "UserPromptSubmit",
command: "test -d .atomic && atomic agent hooks codex user-prompt-submit || true",
command: "atomic agent hooks codex user-prompt-submit",
status_message: None,
},
HookDef {
event: "Stop",
command: "test -d .atomic && atomic agent hooks codex stop || true",
command: "atomic agent hooks codex stop",
status_message: None,
},
HookDef {
event: "PreToolUse",
command: "test -d .atomic && atomic agent hooks codex pre-tool || true",
command: "atomic agent hooks codex pre-tool",
status_message: None,
},
HookDef {
event: "PostToolUse",
command: "test -d .atomic && atomic agent hooks codex post-tool || true",
command: "atomic agent hooks codex post-tool",
status_message: None,
},
];
Expand Down
9 changes: 7 additions & 2 deletions atomic-agent/src/hooks/gemini_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ impl GeminiCliHook {

let mut count = 0;
for (event_key, matcher, verb, hook_name) in &hook_defs {
let command = format!("test -d .atomic && {} {} || true", ATOMIC_HOOK_PREFIX, verb);
let command =
crate::hooks::guarded_hook_command(&format!("{} {}", ATOMIC_HOOK_PREFIX, verb));

let matchers = match *event_key {
"session_start" => &mut hooks.session_start,
Expand Down Expand Up @@ -1153,13 +1154,17 @@ mod tests {
"atomic agent hooks gemini-cli session-start"
));
assert!(is_atomic_hook("atomic agent hooks gemini-cli after-agent"));
// Guarded format
// Guarded format (legacy, `.atomic`-only)
assert!(is_atomic_hook(
"test -d .atomic && atomic agent hooks gemini-cli session-start || true"
));
assert!(is_atomic_hook(
"test -d .atomic && atomic agent hooks gemini-cli after-agent || true"
));
// Sandbox-aware guarded format (current)
assert!(is_atomic_hook(
"test -d .atomic || test -f .atomic-sandbox && atomic agent hooks gemini-cli session-start || true"
));
// Non-atomic
assert!(!is_atomic_hook("my-custom-hook --on-stop"));
assert!(!is_atomic_hook("atomic agent hooks claude-code stop"));
Expand Down
4 changes: 2 additions & 2 deletions atomic-agent/src/hooks/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
//! "command_prefix": "atomic agent hooks codex",
//! "hooks": {
//! "SessionStart": [
//! { "hooks": [ { "type": "command", "command": "test -d .atomic && atomic agent hooks codex session-start || true", "statusMessage": "Atomic: tracking session" } ] }
//! { "hooks": [ { "type": "command", "command": "test -d .atomic || test -f .atomic-sandbox && atomic agent hooks codex session-start || true", "statusMessage": "Atomic: tracking session" } ] }
//! ],
//! "Stop": [
//! { "hooks": [ { "type": "command", "command": "test -d .atomic && atomic agent hooks codex stop || true" } ] }
//! { "hooks": [ { "type": "command", "command": "test -d .atomic || test -f .atomic-sandbox && atomic agent hooks codex stop || true" } ] }
//! ]
//! }
//! }
Expand Down
29 changes: 29 additions & 0 deletions atomic-agent/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ use std::path::Path;
use crate::error::{AgentError, AgentResult};
use crate::event::{HookType, TurnEvent};

// Hook command guard

/// Wrap an `atomic agent hooks …` invocation in the shell guard that installed
/// hooks run behind.
///
/// The hook must only fire where Atomic's graph is reachable, but it must fire
/// in **both** kinds of working tree:
///
/// * a canonical checkout, which has a `.atomic/` directory; and
/// * an agent sandbox, which has **no** `.atomic/` of its own — only a
/// `.atomic-sandbox` pointer file to the canonical graph.
///
/// Guarding on `.atomic` alone silently drops all provenance for sandboxed
/// agents (the guard is false, so the recorder never runs). Adding the sandbox
/// arm lets the hook fire there too; `atomic agent hooks` then resolves the
/// pointer and records into the canonical graph.
///
/// POSIX `&&`/`||` are equal-precedence and left-associative, so
/// `test -d .atomic || test -f .atomic-sandbox && CMD || true` parses as
/// `(((test -d .atomic || test -f .atomic-sandbox) && CMD) || true)` — the
/// recorder runs when either marker is present, and a recorder failure is
/// swallowed so the agent turn is never blocked.
pub(crate) fn guarded_hook_command(inner: &str) -> String {
format!(
"test -d .atomic || test -f {pointer} && {inner} || true",
pointer = atomic_repository::SANDBOX_POINTER,
)
}

// AgentHook Trait

/// Trait for agent hook adapters.
Expand Down
Loading