diff --git a/Cargo.lock b/Cargo.lock index 76adbd6..7089357 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1103,6 +1103,7 @@ dependencies = [ "futures-util", "libc", "maestro-config", + "maestro-hooks", "maestro-names", "maestro-protocol", "reqwest", diff --git a/crates/maestro-daemon/Cargo.toml b/crates/maestro-daemon/Cargo.toml index 24c5c52..7785c96 100644 --- a/crates/maestro-daemon/Cargo.toml +++ b/crates/maestro-daemon/Cargo.toml @@ -18,6 +18,7 @@ dirs.workspace = true futures-util.workspace = true libc.workspace = true maestro-config.workspace = true +maestro-hooks.workspace = true maestro-names.workspace = true maestro-protocol.workspace = true serde.workspace = true diff --git a/crates/maestro-daemon/src/cli.rs b/crates/maestro-daemon/src/cli.rs index 04f2077..c0b8a3f 100644 --- a/crates/maestro-daemon/src/cli.rs +++ b/crates/maestro-daemon/src/cli.rs @@ -47,6 +47,20 @@ async fn run(config_override: Option) -> Result<(), Box> { let _ = default_state_dir(); std::fs::create_dir_all(config.sidecar_dir())?; std::fs::create_dir_all(config.fifo_dir())?; + + // First-start hook auto-install (#106). Best-effort, before serving: a + // session spawned right away should already carry the hook. + if config.hooks_auto_install { + match crate::hooks::auto_install(&config.home_dir) { + crate::hooks::AutoInstallOutcome::Installed => { + tracing::info!("installed claude-code activity hook (#106)"); + } + crate::hooks::AutoInstallOutcome::Failed => { + tracing::warn!("hooks auto-install failed; run `maestro hooks install` manually"); + } + _ => {} + } + } // No worktree dir to pre-create: without an override the worktrees live // under their base repos, created on demand (#56). diff --git a/crates/maestro-daemon/src/config.rs b/crates/maestro-daemon/src/config.rs index 31bf2c5..3fc9a66 100644 --- a/crates/maestro-daemon/src/config.rs +++ b/crates/maestro-daemon/src/config.rs @@ -88,6 +88,10 @@ pub struct DaemonConfig { /// Project-discovery depth (#54): `[spawn].project_scan_depth`, default /// [`DEFAULT_PROJECT_SCAN_DEPTH`]. pub project_scan_depth: u32, + /// Whether the daemon installs the Claude Code activity hook on first + /// start when none is present (#106): `[hooks].auto_install`, default + /// `true`. + pub hooks_auto_install: bool, } impl DaemonConfig { @@ -197,6 +201,7 @@ pub fn load_config(path: &Path, env: EnvOverrides) -> Result String { mod tests { use super::*; + #[test] + fn hooks_auto_install_defaults_true_and_is_opt_out() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("config.toml"); + + std::fs::write(&path, "").unwrap(); + let cfg = load_config(&path, EnvOverrides::default()).unwrap(); + assert!(cfg.hooks_auto_install); + + std::fs::write( + &path, + r#" +[hooks] +auto_install = false +"#, + ) + .unwrap(); + let cfg = load_config(&path, EnvOverrides::default()).unwrap(); + assert!(!cfg.hooks_auto_install); + } + #[test] fn empty_file_defaults_everything() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/maestro-daemon/src/hooks.rs b/crates/maestro-daemon/src/hooks.rs new file mode 100644 index 0000000..1eba19b --- /dev/null +++ b/crates/maestro-daemon/src/hooks.rs @@ -0,0 +1,76 @@ +//! First-start hook auto-install (#106). +//! +//! The daemon runs [`auto_install`] once at startup: when the user's +//! `~/.claude/settings.json` carries no maestro hook yet (and +//! `[hooks].auto_install` hasn't opted out — checked by the caller), the same +//! installer `maestro hooks install` uses writes it. Best-effort: a failure is +//! warned about and never blocks startup. The same global hook serves every +//! Claude Code session on the machine, so nothing here targets repositories +//! or worktrees. + +use std::path::Path; + +/// Install the Claude Code activity hook for `home_dir` unless one is already +/// present or the home dir is unresolvable (`""`). Returns what happened. +pub fn auto_install(home_dir: &str) -> AutoInstallOutcome { + if home_dir.is_empty() { + return AutoInstallOutcome::Skipped; + } + let settings = maestro_hooks::settings_path(Path::new(home_dir)); + if maestro_hooks::is_installed(&settings) { + return AutoInstallOutcome::AlreadyPresent; + } + match maestro_hooks::install_for_home(Path::new(home_dir)) { + Ok(_) => AutoInstallOutcome::Installed, + Err(e) => { + tracing::warn!(error = %e, "hooks auto-install failed (non-fatal)"); + AutoInstallOutcome::Failed + } + } +} + +/// What [`auto_install`] did, so callers can log and tests can assert. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AutoInstallOutcome { + Installed, + AlreadyPresent, + Failed, + Skipped, +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Everything happens under a temp `$HOME`; the developer's own + /// `~/.claude/settings.json` is never read or written. + #[test] + fn installs_when_settings_carry_no_maestro_hook() { + let dir = tempfile::tempdir().unwrap(); + let outcome = auto_install(dir.path().to_str().unwrap()); + assert_eq!(outcome, AutoInstallOutcome::Installed); + let settings = dir.path().join(".claude/settings.json"); + assert!(maestro_hooks::is_installed(&settings)); + } + + #[test] + fn second_start_is_a_noop() { + let dir = tempfile::tempdir().unwrap(); + let home = dir.path().to_str().unwrap(); + auto_install(home); + // A foreign edit between starts survives; presence alone skips. + let settings = dir.path().join(".claude/settings.json"); + let before = std::fs::read_to_string(&settings).unwrap(); + assert_eq!(auto_install(home), AutoInstallOutcome::AlreadyPresent); + assert_eq!( + before, + std::fs::read_to_string(&settings).unwrap(), + "already-installed settings must not be rewritten" + ); + } + + #[test] + fn unresolvable_home_is_skipped() { + assert_eq!(auto_install(""), AutoInstallOutcome::Skipped); + } +} diff --git a/crates/maestro-daemon/src/lib.rs b/crates/maestro-daemon/src/lib.rs index b92689e..8c76df9 100644 --- a/crates/maestro-daemon/src/lib.rs +++ b/crates/maestro-daemon/src/lib.rs @@ -14,6 +14,7 @@ pub mod cli; pub mod config; pub mod dirlist; pub mod gitroot; +pub mod hooks; pub mod local_api; pub mod panes; pub mod proc;