claudy 0.5.0

Modern multi-provider launcher for Claude CLI
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
//! Self-scheduling ingestion (R1).
//!
//! Installs an OS-level periodic job that runs `claudy analytics ingest` once
//! per hour, so the analytics DB never silently freezes when the operator stops
//! invoking ingest by hand.
//!
//! This reuses the same proven mechanisms as the channel `ServiceManager`
//! (atomic file writes via `config::atomic`, generated plist/unit files,
//! `launchctl`/`systemctl` invocation, an enriched `PATH`), but is kept as a
//! focused, separate module rather than overloading the channel trait: a
//! periodic one-shot job (macOS `StartInterval`+`RunAtLoad`, Linux `.timer`)
//! has different lifecycle semantics from a long-running daemon
//! (`KeepAlive`/`Restart`). See SPEC R1 design decision.

use crate::domain::commands::ScheduleAction;
use crate::domain::context::Context;
use std::path::PathBuf;
use std::process::Command;

/// Hourly cadence, in seconds.
const INTERVAL_SECS: u64 = 3600;
#[cfg(target_os = "macos")]
const LAUNCH_LABEL: &str = "com.claudy.analytics.ingest";
#[cfg(target_os = "linux")]
const SYSTEMD_UNIT: &str = "claudy-analytics-ingest";

pub fn run_schedule(ctx: &mut Context, action: ScheduleAction) -> anyhow::Result<i32> {
    let claudy_bin = std::env::current_exe()?;
    // A dev build (run via `cargo run`) resolves to target/<profile>/claudy; a
    // later `cargo clean` or rebuild would silently break the pinned job. Warn
    // at install time so the operator knows to reinstall from a stable binary.
    if matches!(action, ScheduleAction::Install)
        && claudy_bin.to_string_lossy().contains("/target/")
    {
        println!(
            "warning: scheduler is pinned to a dev build under target/ ({}). \
             Re-run `claudy analytics schedule install` after `cargo install` or a \
             release upgrade, or the hourly job will fail.",
            claudy_bin.display()
        );
    }
    let log_dir = analytics_log_dir(ctx);

    match action {
        ScheduleAction::Install => install(&claudy_bin, &log_dir),
        ScheduleAction::Uninstall => uninstall(),
        ScheduleAction::Status => status(),
    }
}

fn analytics_log_dir(ctx: &Context) -> PathBuf {
    PathBuf::from(&ctx.paths.claudy_home).join("logs")
}

#[cfg(target_os = "macos")]
fn install(claudy_bin: &std::path::Path, log_dir: &std::path::Path) -> anyhow::Result<i32> {
    // Ensure the log dir exists so launchd can open StandardOutPath/ErrorPath.
    std::fs::create_dir_all(log_dir)?;
    let plist_path = launch_plist_path()?;
    let plist = generate_plist(claudy_bin, log_dir);
    if let Some(parent) = plist_path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    crate::config::atomic::write_atomic(&plist_path.to_string_lossy(), plist.as_bytes(), 0o644)?;

    // Best-effort: unload any previous copy, then load the new one.
    let _ = Command::new("launchctl")
        .args(["unload", &plist_path.to_string_lossy()])
        .output();
    let out = Command::new("launchctl")
        .args(["load", "-w", &plist_path.to_string_lossy()])
        .output()?;
    if !out.status.success() {
        anyhow::bail!(
            "launchctl load failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }
    println!("Installed hourly ingestion scheduler ({LAUNCH_LABEL}).");
    println!(
        "Logs: {}/analytics-ingest.stdout.log (stderr: analytics-ingest.stderr.log)",
        log_dir.display(),
    );
    Ok(0)
}

#[cfg(target_os = "macos")]
fn uninstall() -> anyhow::Result<i32> {
    let plist_path = launch_plist_path()?;
    if plist_path.exists() {
        let _ = Command::new("launchctl")
            .args(["unload", &plist_path.to_string_lossy()])
            .output();
        std::fs::remove_file(&plist_path).ok();
        println!("Removed ingestion scheduler ({LAUNCH_LABEL}).");
    } else {
        println!("Ingestion scheduler is not installed.");
    }
    Ok(0)
}

#[cfg(target_os = "macos")]
fn status() -> anyhow::Result<i32> {
    let plist_path = launch_plist_path()?;
    let installed = plist_path.exists();
    let loaded = if installed {
        Command::new("launchctl")
            .args(["list", LAUNCH_LABEL])
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    } else {
        false
    };
    println!(
        "ingestion scheduler: {} ({})",
        if loaded {
            "loaded & scheduled"
        } else if installed {
            "installed (not loaded)"
        } else {
            "not installed"
        },
        LAUNCH_LABEL,
    );
    Ok(0)
}

#[cfg(target_os = "macos")]
fn launch_plist_path() -> anyhow::Result<PathBuf> {
    let home =
        dirs::home_dir().ok_or_else(|| anyhow::anyhow!("cannot determine home directory"))?;
    Ok(home
        .join("Library")
        .join("LaunchAgents")
        .join(format!("{LAUNCH_LABEL}.plist")))
}

#[cfg(target_os = "macos")]
fn generate_plist(claudy_bin: &std::path::Path, log_dir: &std::path::Path) -> String {
    let bin = claudy_bin.display();
    let stdout_log = log_dir
        .join("analytics-ingest.stdout.log")
        .to_string_lossy()
        .to_string();
    let stderr_log = log_dir
        .join("analytics-ingest.stderr.log")
        .to_string_lossy()
        .to_string();
    let env_path = build_service_path();
    let home_dir = dirs::home_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_default();
    format!(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>{LAUNCH_LABEL}</string>
    <key>ProgramArguments</key>
    <array>
        <string>{bin}</string>
        <string>analytics</string>
        <string>ingest</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>{INTERVAL_SECS}</integer>
    <key>StandardOutPath</key>
    <string>{stdout_log}</string>
    <key>StandardErrorPath</key>
    <string>{stderr_log}</string>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>{env_path}</string>
        <key>HOME</key>
        <string>{home_dir}</string>
    </dict>
</dict>
</plist>
"#
    )
}

#[cfg(target_os = "linux")]
fn install(claudy_bin: &std::path::Path, log_dir: &std::path::Path) -> anyhow::Result<i32> {
    // Ensure the log dir exists so the oneshot service can append its log.
    std::fs::create_dir_all(log_dir)?;
    let (service_path, timer_path) = systemd_unit_paths()?;
    std::fs::create_dir_all(service_path.parent().unwrap_or(std::path::Path::new(".")))?;
    let service = generate_service(claudy_bin, log_dir);
    let timer = generate_timer();
    crate::config::atomic::write_atomic(
        &service_path.to_string_lossy(),
        service.as_bytes(),
        0o644,
    )?;
    crate::config::atomic::write_atomic(&timer_path.to_string_lossy(), timer.as_bytes(), 0o644)?;

    let _ = Command::new("systemctl")
        .args(["--user", "daemon-reload"])
        .output();
    let out = Command::new("systemctl")
        .args([
            "--user",
            "enable",
            "--now",
            &format!("{SYSTEMD_UNIT}.timer"),
        ])
        .output()?;
    if !out.status.success() {
        anyhow::bail!(
            "systemctl enable failed: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }
    println!("Installed hourly ingestion scheduler ({SYSTEMD_UNIT}.timer).");
    println!("Logs: {}/analytics-ingest.log", log_dir.display());
    Ok(0)
}

#[cfg(target_os = "linux")]
fn uninstall() -> anyhow::Result<i32> {
    let (service_path, timer_path) = systemd_unit_paths()?;
    let _ = Command::new("systemctl")
        .args([
            "--user",
            "disable",
            "--now",
            &format!("{SYSTEMD_UNIT}.timer"),
        ])
        .output();
    let mut removed = false;
    if timer_path.exists() {
        std::fs::remove_file(&timer_path).ok();
        removed = true;
    }
    if service_path.exists() {
        std::fs::remove_file(&service_path).ok();
        removed = true;
    }
    if removed {
        let _ = Command::new("systemctl")
            .args(["--user", "daemon-reload"])
            .output();
        println!("Removed ingestion scheduler ({SYSTEMD_UNIT}.timer).");
    } else {
        println!("Ingestion scheduler is not installed.");
    }
    Ok(0)
}

#[cfg(target_os = "linux")]
fn status() -> anyhow::Result<i32> {
    let (_, timer_path) = systemd_unit_paths()?;
    let enabled = Command::new("systemctl")
        .args(["--user", "is-enabled", &format!("{SYSTEMD_UNIT}.timer")])
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    println!(
        "ingestion scheduler: {} ({SYSTEMD_UNIT}.timer, installed={})",
        if enabled { "enabled" } else { "not enabled" },
        timer_path.exists(),
    );
    Ok(0)
}

#[cfg(target_os = "linux")]
fn systemd_unit_paths() -> anyhow::Result<(PathBuf, PathBuf)> {
    let home =
        dirs::home_dir().ok_or_else(|| anyhow::anyhow!("cannot determine home directory"))?;
    let dir = home.join(".config").join("systemd").join("user");
    Ok((
        dir.join(format!("{SYSTEMD_UNIT}.service")),
        dir.join(format!("{SYSTEMD_UNIT}.timer")),
    ))
}

#[cfg(target_os = "linux")]
fn generate_service(claudy_bin: &std::path::Path, log_dir: &std::path::Path) -> String {
    let exec = claudy_bin.display();
    let env_path = build_service_path();
    let log_file = log_dir
        .join("analytics-ingest.log")
        .to_string_lossy()
        .to_string();
    format!(
        r#"[Unit]
Description=Claudy analytics ingestion (hourly)
After=network.target

[Service]
Type=oneshot
ExecStart={exec} analytics ingest
Environment=PATH={env_path}
StandardOutput=append:{log_file}
StandardError=append:{log_file}
"#
    )
}

#[cfg(target_os = "linux")]
fn generate_timer() -> String {
    let interval_min = INTERVAL_SECS / 60;
    format!(
        r#"[Unit]
Description=Run Claudi analytics ingestion hourly

[Timer]
OnBootSec=2min
OnUnitActiveSec={interval_min}min
Persistent=true

[Install]
WantedBy=timers.target
"#
    )
}

// ── Unsupported platform fallback ──

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn install(_claudy_bin: &std::path::Path, _log_dir: &std::path::Path) -> anyhow::Result<i32> {
    unsupported()
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn uninstall() -> anyhow::Result<i32> {
    unsupported()
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn status() -> anyhow::Result<i32> {
    unsupported()
}

#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn unsupported() -> anyhow::Result<i32> {
    println!("Scheduled ingestion is not yet supported on this platform.");
    println!("Run 'claudy analytics ingest' manually, or via your OS task scheduler:");
    println!("  claudy analytics ingest");
    Ok(1)
}

/// Build a PATH string including common binary locations (mirrors the channel
/// service path builder so the scheduler can locate the Claude CLI if needed).
/// macOS-only: the LaunchAgent plist needs an explicit PATH; systemd units
/// inherit the user environment.
/// Build a PATH string including common binary locations (mirrors the channel
/// service path builder so the scheduler can locate the Claude CLI if needed).
/// Unix-only: the LaunchAgent plist and the systemd unit both need an explicit
/// PATH, and the `:` separator is a Unix convention.
#[cfg(unix)]
fn build_service_path() -> String {
    let home = dirs::home_dir()
        .map(|p| p.display().to_string())
        .unwrap_or_default();
    let current_path = std::env::var("PATH").unwrap_or_default();
    let mut paths: Vec<String> = current_path.split(':').map(|s| s.to_string()).collect();
    let extras = [
        format!("{home}/.local/bin"),
        format!("{home}/bin"),
        "/usr/local/bin".to_string(),
        "/opt/homebrew/bin".to_string(),
    ];
    for extra in extras {
        if !paths.contains(&extra) {
            paths.push(extra);
        }
    }
    paths.join(":")
}

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

    #[cfg(target_os = "macos")]
    #[test]
    fn test_plist_is_periodic_not_daemon() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let bin = tmp.path().join("claudy");
        let log_dir = tmp.path().join("logs");
        let plist = generate_plist(&bin, &log_dir);

        // Periodic one-shot markers (R1 intent), not a KeepAlive daemon.
        assert!(plist.contains("<key>Label</key>"));
        assert!(plist.contains(LAUNCH_LABEL));
        assert!(plist.contains("<key>StartInterval</key>"));
        assert!(plist.contains(&format!("<integer>{INTERVAL_SECS}</integer>")));
        assert!(plist.contains("<key>RunAtLoad</key>"));
        assert!(!plist.contains("KeepAlive"), "timer must not set KeepAlive");
        assert!(plist.contains("analytics"));
        assert!(plist.contains("ingest"));
        assert!(plist.contains("EnvironmentVariables"));
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn test_systemd_units_are_periodic() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let bin = tmp.path().join("claudy");
        let log_dir = tmp.path().join("logs");
        let service = generate_service(&bin, &log_dir);
        let timer = generate_timer();

        assert!(service.contains("Type=oneshot"));
        assert!(service.contains("ExecStart="));
        assert!(service.contains("analytics ingest"));
        assert!(timer.contains("OnUnitActiveSec="));
        assert!(timer.contains("Persistent=true"));
        assert!(timer.contains("WantedBy=timers.target"));
    }
}