reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Daemon Process Manager
//!
//! Handles lifecycle management for the MCP daemon process.
//!
//! # Design
//!
//! Uses a safe subprocess approach instead of fork() to comply with
//! the project's `#![deny(unsafe_code)]` policy. The daemon is spawned
//! as a detached child process running in "serve-daemon" mode.

use crate::error::{Error, Result};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::time::Duration;
use tracing::{info, warn};

/// Daemon status
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DaemonStatus {
    /// Daemon is running
    Running { pid: u32, uptime_secs: u64 },
    /// Daemon is stopped
    Stopped,
    /// PID file exists but process is dead
    Stale,
}

/// Daemon process manager
pub struct DaemonManager {
    pid_file: PathBuf,
    socket_path: PathBuf,
    log_path: PathBuf,
}

impl DaemonManager {
    /// Create new daemon manager
    pub fn new() -> Result<Self> {
        let data_dir = Self::get_data_dir()?;

        Ok(Self {
            pid_file: data_dir.join("daemon.pid"),
            socket_path: Self::get_socket_path()?,
            log_path: data_dir.join("logs").join("mcp-daemon.log"),
        })
    }

    /// Get data directory (XDG compliant)
    fn get_data_dir() -> Result<PathBuf> {
        let data_dir = dirs::data_local_dir()
            .ok_or_else(|| Error::config("Failed to get data directory"))?
            .join("reasonkit")
            .join("mcp");

        std::fs::create_dir_all(&data_dir)?;
        Ok(data_dir)
    }

    /// Get socket path (platform-specific)
    #[cfg(unix)]
    pub fn get_socket_path() -> Result<PathBuf> {
        use std::env;

        // Try XDG_RUNTIME_DIR first (systemd standard)
        let runtime_dir = env::var("XDG_RUNTIME_DIR")
            .ok()
            .map(PathBuf::from)
            .or_else(dirs::runtime_dir)
            .unwrap_or_else(env::temp_dir);

        let socket_dir = runtime_dir.join("reasonkit");
        std::fs::create_dir_all(&socket_dir)?;

        Ok(socket_dir.join("mcp.sock"))
    }

    #[cfg(windows)]
    pub fn get_socket_path() -> Result<PathBuf> {
        // Windows uses named pipes, but return path for consistency
        Ok(PathBuf::from(r"\\.\pipe\reasonkit-mcp"))
    }

    /// Get named pipe name (Windows only)
    #[cfg(windows)]
    pub fn get_pipe_name() -> String {
        format!(r"\\.\pipe\reasonkit-mcp-{}", whoami::username())
    }

    /// Start the daemon
    pub async fn start(&self) -> Result<()> {
        // Check if already running
        if matches!(self.status().await, DaemonStatus::Running { .. }) {
            return Err(Error::daemon("Daemon already running"));
        }

        info!("Starting MCP daemon...");

        // Platform-specific daemon spawn
        #[cfg(unix)]
        self.daemonize_unix()?;

        #[cfg(windows)]
        self.spawn_detached_windows()?;

        // Wait for daemon to start (check PID file)
        self.wait_for_start().await?;

        info!("MCP daemon started successfully");
        Ok(())
    }

    /// Stop the daemon
    pub async fn stop(&self) -> Result<()> {
        let status = self.status().await;

        match status {
            DaemonStatus::Running { pid, .. } => {
                info!("Stopping MCP daemon (PID {})...", pid);

                // Send shutdown signal
                self.send_shutdown_signal(pid)?;

                // Wait for graceful shutdown
                self.wait_for_shutdown(Duration::from_secs(10)).await?;

                // Cleanup
                self.cleanup()?;

                info!("MCP daemon stopped successfully");
                Ok(())
            }
            DaemonStatus::Stale => {
                warn!("Cleaning up stale PID file");
                self.cleanup()?;
                Ok(())
            }
            DaemonStatus::Stopped => {
                warn!("Daemon is not running");
                Ok(())
            }
        }
    }

    /// Restart the daemon
    pub async fn restart(&self) -> Result<()> {
        info!("Restarting MCP daemon...");
        self.stop().await?;
        tokio::time::sleep(Duration::from_secs(1)).await;
        self.start().await
    }

    /// Get daemon status
    pub async fn status(&self) -> DaemonStatus {
        match self.read_pid() {
            Ok(pid) => {
                if self.process_exists(pid) {
                    let uptime = self.get_uptime().unwrap_or(0);
                    DaemonStatus::Running {
                        pid,
                        uptime_secs: uptime,
                    }
                } else {
                    DaemonStatus::Stale
                }
            }
            Err(_) => DaemonStatus::Stopped,
        }
    }

    /// Read PID from file
    fn read_pid(&self) -> Result<u32> {
        let content = std::fs::read_to_string(&self.pid_file)
            .map_err(|_| Error::daemon("PID file not found"))?;

        content
            .trim()
            .parse::<u32>()
            .map_err(|_| Error::daemon("Invalid PID in file"))
    }

    /// Write PID to file
    fn write_pid(&self, pid: u32) -> Result<()> {
        std::fs::write(&self.pid_file, pid.to_string())?;
        Ok(())
    }

    /// Check if process exists (safe implementation using /proc on Unix)
    #[cfg(unix)]
    fn process_exists(&self, pid: u32) -> bool {
        // Check /proc/{pid} directory exists (Linux)
        let proc_path = PathBuf::from(format!("/proc/{}", pid));
        if proc_path.exists() {
            return true;
        }

        // Fallback: try to read process info via ps command
        Command::new("ps")
            .args(["-p", &pid.to_string()])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map(|s| s.success())
            .unwrap_or(false)
    }

    #[cfg(windows)]
    fn process_exists(&self, pid: u32) -> bool {
        // Use tasklist command to check if process exists (safe, no unsafe)
        Command::new("tasklist")
            .args(["/FI", &format!("PID eq {}", pid)])
            .stdout(Stdio::piped())
            .stderr(Stdio::null())
            .output()
            .map(|o| {
                let stdout = String::from_utf8_lossy(&o.stdout);
                stdout.contains(&pid.to_string())
            })
            .unwrap_or(false)
    }

    /// Get daemon uptime (seconds)
    fn get_uptime(&self) -> Result<u64> {
        let metadata = std::fs::metadata(&self.pid_file)?;
        let created = metadata.created()?;
        let elapsed = std::time::SystemTime::now()
            .duration_since(created)
            .unwrap_or_default();
        Ok(elapsed.as_secs())
    }

    /// Spawn daemon as detached subprocess (safe, no fork)
    ///
    /// This uses `std::process::Command` to spawn the daemon as a child process
    /// with redirected stdio, avoiding the need for unsafe fork() calls.
    #[cfg(unix)]
    fn daemonize_unix(&self) -> Result<()> {
        use std::os::unix::process::CommandExt;

        // Get current executable
        let exe = std::env::current_exe()?;

        // Create log directory if needed
        if let Some(parent) = self.log_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        // Open log file for stdout/stderr
        let log_file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.log_path)?;

        let log_stderr = log_file.try_clone()?;

        // Spawn daemon as detached process
        let child = Command::new(&exe)
            .arg("mcp")
            .arg("serve-daemon")
            .arg("--socket")
            .arg(&self.socket_path)
            .stdin(Stdio::null())
            .stdout(Stdio::from(log_file))
            .stderr(Stdio::from(log_stderr))
            .current_dir("/") // Don't lock any mount points
            .process_group(0) // New process group (detach from terminal)
            .spawn()
            .map_err(|e| Error::daemon(format!("Failed to spawn daemon: {}", e)))?;

        // Write PID file
        self.write_pid(child.id())?;

        info!("Daemon spawned with PID {}", child.id());
        Ok(())
    }

    /// Spawn detached process on Windows
    #[cfg(windows)]
    fn spawn_detached_windows(&self) -> Result<()> {
        use std::os::windows::process::CommandExt;

        // CREATE_NO_WINDOW = 0x08000000 (constant to avoid winapi dependency)
        const CREATE_NO_WINDOW: u32 = 0x08000000;
        const DETACHED_PROCESS: u32 = 0x00000008;

        let exe = std::env::current_exe()?;

        // Create log directory if needed
        if let Some(parent) = self.log_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        // Open log file for stdout/stderr
        let log_file = std::fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.log_path)?;

        let log_stderr = log_file.try_clone()?;

        let child = Command::new(exe)
            .arg("mcp")
            .arg("serve-daemon")
            .arg("--socket")
            .arg(&self.socket_path)
            .stdin(Stdio::null())
            .stdout(Stdio::from(log_file))
            .stderr(Stdio::from(log_stderr))
            .creation_flags(CREATE_NO_WINDOW | DETACHED_PROCESS)
            .spawn()
            .map_err(|e| Error::daemon(format!("Failed to spawn daemon: {}", e)))?;

        // Write PID
        self.write_pid(child.id())?;

        info!("Daemon spawned with PID {}", child.id());
        Ok(())
    }

    /// Send shutdown signal to process (safe implementation using kill command)
    #[cfg(unix)]
    fn send_shutdown_signal(&self, pid: u32) -> Result<()> {
        // Use kill command instead of libc kill()
        let status = Command::new("kill")
            .args(["-TERM", &pid.to_string()])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map_err(|e| Error::daemon(format!("Failed to run kill command: {}", e)))?;

        if !status.success() {
            return Err(Error::daemon(format!(
                "kill -TERM {} failed with exit code: {:?}",
                pid,
                status.code()
            )));
        }

        Ok(())
    }

    #[cfg(windows)]
    fn send_shutdown_signal(&self, pid: u32) -> Result<()> {
        // Use taskkill command instead of Windows API
        let status = Command::new("taskkill")
            .args(["/PID", &pid.to_string(), "/T"]) // /T = terminate child processes
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .map_err(|e| Error::daemon(format!("Failed to run taskkill: {}", e)))?;

        if !status.success() {
            // Try forceful termination
            let status = Command::new("taskkill")
                .args(["/PID", &pid.to_string(), "/F", "/T"])
                .stdout(Stdio::null())
                .stderr(Stdio::null())
                .status()
                .map_err(|e| Error::daemon(format!("Failed to run taskkill /F: {}", e)))?;

            if !status.success() {
                return Err(Error::daemon(format!("taskkill failed for PID {}", pid)));
            }
        }

        Ok(())
    }

    /// Wait for daemon to start
    async fn wait_for_start(&self) -> Result<()> {
        for _ in 0..20 {
            // Check every 500ms, max 10s
            tokio::time::sleep(Duration::from_millis(500)).await;

            if matches!(self.status().await, DaemonStatus::Running { .. }) {
                return Ok(());
            }
        }

        Err(Error::daemon("Daemon failed to start within 10 seconds"))
    }

    /// Wait for daemon to shutdown
    async fn wait_for_shutdown(&self, timeout: Duration) -> Result<()> {
        let start = std::time::Instant::now();

        while start.elapsed() < timeout {
            if matches!(self.status().await, DaemonStatus::Stopped) {
                return Ok(());
            }

            tokio::time::sleep(Duration::from_millis(100)).await;
        }

        Err(Error::daemon("Daemon did not shut down gracefully"))
    }

    /// Cleanup PID file and socket
    fn cleanup(&self) -> Result<()> {
        // Remove PID file
        if self.pid_file.exists() {
            std::fs::remove_file(&self.pid_file).ok();
        }

        // Remove socket (Unix only)
        #[cfg(unix)]
        {
            if self.socket_path.exists() {
                std::fs::remove_file(&self.socket_path).ok();
            }
        }

        Ok(())
    }
}

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

    #[test]
    fn test_get_data_dir() {
        let dir = DaemonManager::get_data_dir().unwrap();
        assert!(dir.to_str().unwrap().contains("reasonkit"));
    }

    #[test]
    fn test_get_socket_path() {
        let path = DaemonManager::get_socket_path().unwrap();
        assert!(
            path.to_str().unwrap().contains("reasonkit") || path.to_str().unwrap().contains("pipe")
        );
    }

    #[tokio::test]
    async fn test_daemon_status_stopped() {
        let manager = DaemonManager::new().unwrap();
        let status = manager.status().await;
        assert_eq!(status, DaemonStatus::Stopped);
    }
}