-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathregistry.rs
More file actions
591 lines (527 loc) · 20.3 KB
/
registry.rs
File metadata and controls
591 lines (527 loc) · 20.3 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::process::Child;
/// Type of process being tracked
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ProcessType {
AgentRun { agent_id: i64, agent_name: String },
ClaudeSession { session_id: String },
McpServe,
}
/// Information about a running agent process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessInfo {
pub run_id: i64,
pub process_type: ProcessType,
pub pid: u32,
pub started_at: DateTime<Utc>,
pub project_path: String,
pub task: String,
pub model: String,
}
/// Information about a running process with handle
#[allow(dead_code)]
pub struct ProcessHandle {
pub info: ProcessInfo,
pub child: Arc<Mutex<Option<Child>>>,
pub live_output: Arc<Mutex<String>>,
}
/// Registry for tracking active agent processes
pub struct ProcessRegistry {
processes: Arc<Mutex<HashMap<i64, ProcessHandle>>>, // run_id -> ProcessHandle
next_id: Arc<Mutex<i64>>, // Auto-incrementing ID for non-agent processes
}
impl ProcessRegistry {
pub fn new() -> Self {
Self {
processes: Arc::new(Mutex::new(HashMap::new())),
next_id: Arc::new(Mutex::new(1000000)), // Start at high number to avoid conflicts
}
}
/// Generate a unique ID for non-agent processes
pub fn generate_id(&self) -> Result<i64, String> {
let mut next_id = self.next_id.lock().map_err(|e| e.to_string())?;
let id = *next_id;
*next_id += 1;
Ok(id)
}
/// Register a new running agent process
pub fn register_process(
&self,
run_id: i64,
agent_id: i64,
agent_name: String,
pid: u32,
project_path: String,
task: String,
model: String,
child: Child,
) -> Result<(), String> {
let process_info = ProcessInfo {
run_id,
process_type: ProcessType::AgentRun {
agent_id,
agent_name,
},
pid,
started_at: Utc::now(),
project_path,
task,
model,
};
self.register_process_internal(run_id, process_info, child)
}
/// Register a new running agent process using sidecar (similar to register_process but for sidecar children)
#[allow(dead_code)]
pub fn register_sidecar_process(
&self,
run_id: i64,
agent_id: i64,
agent_name: String,
pid: u32,
project_path: String,
task: String,
model: String,
) -> Result<(), String> {
let process_info = ProcessInfo {
run_id,
process_type: ProcessType::AgentRun {
agent_id,
agent_name,
},
pid,
started_at: Utc::now(),
project_path,
task,
model,
};
// For sidecar processes, we register without the child handle since it's managed differently
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
let process_handle = ProcessHandle {
info: process_info,
child: Arc::new(Mutex::new(None)), // No tokio::process::Child handle for sidecar
live_output: Arc::new(Mutex::new(String::new())),
};
processes.insert(run_id, process_handle);
Ok(())
}
/// Register a new Claude session (without child process - handled separately)
pub fn register_claude_session(
&self,
session_id: String,
pid: u32,
project_path: String,
task: String,
model: String,
) -> Result<i64, String> {
let run_id = self.generate_id()?;
let process_info = ProcessInfo {
run_id,
process_type: ProcessType::ClaudeSession { session_id },
pid,
started_at: Utc::now(),
project_path,
task,
model,
};
// Register without child - Claude sessions use ClaudeProcessState for process management
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
let process_handle = ProcessHandle {
info: process_info,
child: Arc::new(Mutex::new(None)), // No child handle for Claude sessions
live_output: Arc::new(Mutex::new(String::new())),
};
processes.insert(run_id, process_handle);
Ok(run_id)
}
/// Register a long-running MCP serve process (stores PID only, no child handle)
///
/// NOTE: Only ONE MCP serve process should run at a time (singleton pattern).
/// This method enforces the singleton by checking atomically within the lock.
pub fn register_mcp_serve_process(&self, pid: u32) -> Result<i64, String> {
// Acquire lock first to make check-and-register atomic (prevents race conditions)
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
// Check if MCP serve process already exists (atomic with registration)
let existing_mcp = processes.values().find(|p| {
matches!(p.info.process_type, ProcessType::McpServe)
});
if let Some(existing) = existing_mcp {
return Err(format!(
"MCP server already running (PID: {})",
existing.info.pid
));
}
// Now safe to register new MCP serve process
let run_id = self.generate_id()?;
let process_info = ProcessInfo {
run_id,
process_type: ProcessType::McpServe,
pid,
started_at: Utc::now(),
project_path: "".to_string(),
task: "claude mcp serve".to_string(),
model: "".to_string(),
};
let process_handle = ProcessHandle {
info: process_info,
child: Arc::new(Mutex::new(None)),
live_output: Arc::new(Mutex::new(String::new())),
};
processes.insert(run_id, process_handle);
Ok(run_id)
}
/// Get the currently running MCP serve process if any
pub fn get_running_mcp_serve(&self) -> Result<Option<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes
.values()
.find(|handle| matches!(handle.info.process_type, ProcessType::McpServe))
.map(|handle| handle.info.clone()))
}
/// Internal method to register any process
fn register_process_internal(
&self,
run_id: i64,
process_info: ProcessInfo,
child: Child,
) -> Result<(), String> {
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
let process_handle = ProcessHandle {
info: process_info,
child: Arc::new(Mutex::new(Some(child))),
live_output: Arc::new(Mutex::new(String::new())),
};
processes.insert(run_id, process_handle);
Ok(())
}
/// Get all running Claude sessions
pub fn get_running_claude_sessions(&self) -> Result<Vec<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes
.values()
.filter_map(|handle| match &handle.info.process_type {
ProcessType::ClaudeSession { .. } => Some(handle.info.clone()),
_ => None,
})
.collect())
}
/// Get a specific Claude session by session ID
pub fn get_claude_session_by_id(
&self,
session_id: &str,
) -> Result<Option<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes
.values()
.find(|handle| match &handle.info.process_type {
ProcessType::ClaudeSession { session_id: sid } => sid == session_id,
_ => false,
})
.map(|handle| handle.info.clone()))
}
/// Unregister a process (called when it completes)
#[allow(dead_code)]
pub fn unregister_process(&self, run_id: i64) -> Result<(), String> {
let mut processes = self.processes.lock().map_err(|e| e.to_string())?;
processes.remove(&run_id);
Ok(())
}
/// Get all running processes
#[allow(dead_code)]
pub fn get_running_processes(&self) -> Result<Vec<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes
.values()
.map(|handle| handle.info.clone())
.collect())
}
/// Get all running agent processes
pub fn get_running_agent_processes(&self) -> Result<Vec<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes
.values()
.filter_map(|handle| match &handle.info.process_type {
ProcessType::AgentRun { .. } => Some(handle.info.clone()),
_ => None,
})
.collect())
}
/// Get a specific running process
#[allow(dead_code)]
pub fn get_process(&self, run_id: i64) -> Result<Option<ProcessInfo>, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
Ok(processes.get(&run_id).map(|handle| handle.info.clone()))
}
/// Kill a running process with proper cleanup
pub async fn kill_process(&self, run_id: i64) -> Result<bool, String> {
use log::{error, info, warn};
// First check if the process exists and get its PID
let (pid, child_arc) = {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
if let Some(handle) = processes.get(&run_id) {
(handle.info.pid, handle.child.clone())
} else {
warn!("Process {} not found in registry", run_id);
return Ok(false); // Process not found
}
};
info!(
"Attempting graceful shutdown of process {} (PID: {})",
run_id, pid
);
// Send kill signal to the process
let kill_sent = {
let mut child_guard = child_arc.lock().map_err(|e| e.to_string())?;
if let Some(child) = child_guard.as_mut() {
match child.start_kill() {
Ok(_) => {
info!("Successfully sent kill signal to process {}", run_id);
true
}
Err(e) => {
error!("Failed to send kill signal to process {}: {}", run_id, e);
// Don't return error here, try fallback method
false
}
}
} else {
warn!(
"No child handle available for process {} (PID: {}), attempting system kill",
run_id, pid
);
false // Process handle not available, try fallback
}
};
// If direct kill didn't work, try system command as fallback
if !kill_sent {
info!(
"Attempting fallback kill for process {} (PID: {})",
run_id, pid
);
match self.kill_process_by_pid(run_id, pid) {
Ok(true) => return Ok(true),
Ok(false) => warn!(
"Fallback kill also failed for process {} (PID: {})",
run_id, pid
),
Err(e) => error!("Error during fallback kill: {}", e),
}
// Continue with the rest of the cleanup even if fallback failed
}
// Wait for the process to exit (with timeout)
let wait_result = tokio::time::timeout(tokio::time::Duration::from_secs(5), async {
loop {
// Check if process has exited
let status = {
let mut child_guard = child_arc.lock().map_err(|e| e.to_string())?;
if let Some(child) = child_guard.as_mut() {
match child.try_wait() {
Ok(Some(status)) => {
info!("Process {} exited with status: {:?}", run_id, status);
*child_guard = None; // Clear the child handle
Some(Ok::<(), String>(()))
}
Ok(None) => {
// Still running
None
}
Err(e) => {
error!("Error checking process status: {}", e);
Some(Err(e.to_string()))
}
}
} else {
// Process already gone
Some(Ok(()))
}
};
match status {
Some(result) => return result,
None => {
// Still running, wait a bit
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
}
}
}
})
.await;
match wait_result {
Ok(Ok(_)) => {
info!("Process {} exited gracefully", run_id);
}
Ok(Err(e)) => {
error!("Error waiting for process {}: {}", run_id, e);
}
Err(_) => {
warn!("Process {} didn't exit within 5 seconds after kill", run_id);
// Force clear the handle
if let Ok(mut child_guard) = child_arc.lock() {
*child_guard = None;
}
// One more attempt with system kill
let _ = self.kill_process_by_pid(run_id, pid);
}
}
// Remove from registry after killing
self.unregister_process(run_id)?;
Ok(true)
}
/// Kill a process by PID using system commands (fallback method)
pub fn kill_process_by_pid(&self, run_id: i64, pid: u32) -> Result<bool, String> {
use log::{error, info, warn};
info!("Attempting to kill process {} by PID {}", run_id, pid);
let kill_result = if cfg!(target_os = "windows") {
std::process::Command::new("taskkill")
.args(["/F", "/PID", &pid.to_string()])
.output()
} else {
// First try SIGTERM
let term_result = std::process::Command::new("kill")
.args(["-TERM", &pid.to_string()])
.output();
match &term_result {
Ok(output) if output.status.success() => {
info!("Sent SIGTERM to PID {}", pid);
// Give it 2 seconds to exit gracefully
std::thread::sleep(std::time::Duration::from_secs(2));
// Check if still running
let check_result = std::process::Command::new("kill")
.args(["-0", &pid.to_string()])
.output();
if let Ok(output) = check_result {
if output.status.success() {
// Still running, send SIGKILL
warn!(
"Process {} still running after SIGTERM, sending SIGKILL",
pid
);
std::process::Command::new("kill")
.args(["-KILL", &pid.to_string()])
.output()
} else {
term_result
}
} else {
term_result
}
}
_ => {
// SIGTERM failed, try SIGKILL directly
warn!("SIGTERM failed for PID {}, trying SIGKILL", pid);
std::process::Command::new("kill")
.args(["-KILL", &pid.to_string()])
.output()
}
}
};
match kill_result {
Ok(output) => {
if output.status.success() {
info!("Successfully killed process with PID {}", pid);
// Remove from registry
self.unregister_process(run_id)?;
Ok(true)
} else {
let error_msg = String::from_utf8_lossy(&output.stderr);
warn!("Failed to kill PID {}: {}", pid, error_msg);
Ok(false)
}
}
Err(e) => {
error!("Failed to execute kill command for PID {}: {}", pid, e);
Err(format!("Failed to execute kill command: {}", e))
}
}
}
/// Check if a process is still running by trying to get its status
#[allow(dead_code)]
pub async fn is_process_running(&self, run_id: i64) -> Result<bool, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
if let Some(handle) = processes.get(&run_id) {
let child_arc = handle.child.clone();
drop(processes); // Release the lock before async operation
let mut child_guard = child_arc.lock().map_err(|e| e.to_string())?;
if let Some(ref mut child) = child_guard.as_mut() {
match child.try_wait() {
Ok(Some(_)) => {
// Process has exited
*child_guard = None;
Ok(false)
}
Ok(None) => {
// Process is still running
Ok(true)
}
Err(_) => {
// Error checking status, assume not running
*child_guard = None;
Ok(false)
}
}
} else {
Ok(false) // No child handle
}
} else {
Ok(false) // Process not found in registry
}
}
/// Append to live output for a process
pub fn append_live_output(&self, run_id: i64, output: &str) -> Result<(), String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
if let Some(handle) = processes.get(&run_id) {
let mut live_output = handle.live_output.lock().map_err(|e| e.to_string())?;
live_output.push_str(output);
live_output.push('\n');
}
Ok(())
}
/// Get live output for a process
pub fn get_live_output(&self, run_id: i64) -> Result<String, String> {
let processes = self.processes.lock().map_err(|e| e.to_string())?;
if let Some(handle) = processes.get(&run_id) {
let live_output = handle.live_output.lock().map_err(|e| e.to_string())?;
Ok(live_output.clone())
} else {
Ok(String::new())
}
}
/// Cleanup finished processes
#[allow(dead_code)]
pub async fn cleanup_finished_processes(&self) -> Result<Vec<i64>, String> {
let mut finished_runs = Vec::new();
let processes_lock = self.processes.clone();
// First, identify finished processes
{
let processes = processes_lock.lock().map_err(|e| e.to_string())?;
let run_ids: Vec<i64> = processes.keys().cloned().collect();
drop(processes);
for run_id in run_ids {
if !self.is_process_running(run_id).await? {
finished_runs.push(run_id);
}
}
}
// Then remove them from the registry
{
let mut processes = processes_lock.lock().map_err(|e| e.to_string())?;
for run_id in &finished_runs {
processes.remove(run_id);
}
}
Ok(finished_runs)
}
}
impl Default for ProcessRegistry {
fn default() -> Self {
Self::new()
}
}
/// Global process registry state
pub struct ProcessRegistryState(pub Arc<ProcessRegistry>);
impl Default for ProcessRegistryState {
fn default() -> Self {
Self(Arc::new(ProcessRegistry::new()))
}
}