@@ -355,9 +355,71 @@ fn sigterm_then_sigkill(pids: &[i32]) {
355355 }
356356}
357357
358+ /// Resolve orphan candidate PIDs to their actual process group IDs, dedupe,
359+ /// and signal the groups. An orphaned grandchild (e.g. `goose` or `buzz-dev-mcp`)
360+ /// whose harness has exited retains the harness's PGID — signaling that PGID
361+ /// kills the entire orphaned subtree. Falls back to the candidate PID itself
362+ /// when PGID resolution fails (process may have exited between detection and
363+ /// kill).
364+ #[ cfg( target_os = "macos" ) ]
365+ fn resolve_pgids_and_kill ( candidate_pids : & [ i32 ] ) {
366+ let candidate_set: std:: collections:: HashSet < i32 > = candidate_pids. iter ( ) . copied ( ) . collect ( ) ;
367+ let mut pgids = std:: collections:: HashSet :: new ( ) ;
368+ for & pid in candidate_pids {
369+ let pgid = unsafe { libc:: getpgid ( pid) } ;
370+ if pgid > 0 {
371+ pgids. insert ( pgid) ;
372+ } else {
373+ // Process may have exited; try signaling it directly as a group.
374+ pgids. insert ( pid) ;
375+ }
376+ }
377+ // PID-recycling guard: if a resolved PGID is alive but isn't one of our
378+ // orphan candidates, the old harness PID was recycled by a new process
379+ // that called setsid() — skip it to avoid killing an unrelated group.
380+ pgids. retain ( |& pgid| {
381+ if candidate_set. contains ( & pgid) {
382+ return true ;
383+ }
384+ let alive = unsafe { libc:: kill ( pgid, 0 ) } == 0 ;
385+ !alive
386+ } ) ;
387+ let unique: Vec < i32 > = pgids. into_iter ( ) . collect ( ) ;
388+ sigterm_then_sigkill ( & unique) ;
389+ }
390+
391+ /// Resolve orphan candidate PIDs to their actual process group IDs, dedupe,
392+ /// and signal the groups. Linux variant reads PGID from /proc/<pid>/stat.
393+ #[ cfg( all( unix, not( target_os = "macos" ) ) ) ]
394+ fn resolve_pgids_and_kill ( candidate_pids : & [ i32 ] ) {
395+ let candidate_set: std:: collections:: HashSet < i32 > = candidate_pids. iter ( ) . copied ( ) . collect ( ) ;
396+ let mut pgids = std:: collections:: HashSet :: new ( ) ;
397+ for & pid in candidate_pids {
398+ if let Some ( pgid) = read_pgid_linux ( pid as u32 ) {
399+ pgids. insert ( pgid as i32 ) ;
400+ } else {
401+ // Process may have exited; try signaling it directly as a group.
402+ pgids. insert ( pid) ;
403+ }
404+ }
405+ // PID-recycling guard: if a resolved PGID is alive but isn't one of our
406+ // orphan candidates, the old harness PID was recycled by a new process
407+ // that called setsid() — skip it to avoid killing an unrelated group.
408+ pgids. retain ( |& pgid| {
409+ if candidate_set. contains ( & pgid) {
410+ return true ;
411+ }
412+ let alive = unsafe { libc:: kill ( pgid, 0 ) } == 0 ;
413+ !alive
414+ } ) ;
415+ let unique: Vec < i32 > = pgids. into_iter ( ) . collect ( ) ;
416+ sigterm_then_sigkill ( & unique) ;
417+ }
418+
358419/// Kill orphaned agent processes using PID file receipts. Reads all files from
359420/// `agent-pids/`, verifies each PID still belongs to a known agent binary,
360- /// then kills the process group. Deletes the PID file after killing.
421+ /// then resolves each candidate's actual PGID and signals the process group.
422+ /// Deletes the PID file after killing.
361423///
362424/// `skip_pids` are PIDs already handled by the tracked-agent path.
363425#[ cfg( unix) ]
@@ -379,7 +441,7 @@ pub(crate) fn sweep_orphaned_agent_processes(app: &AppHandle, skip_pids: &[u32])
379441 . collect ( ) ;
380442
381443 if !targets. is_empty ( ) {
382- sigterm_then_sigkill ( & targets) ;
444+ resolve_pgids_and_kill ( & targets) ;
383445 }
384446
385447 // Clean up PID files for processes we just killed or that are already gone.
@@ -503,6 +565,13 @@ pub(crate) fn sweep_system_agent_processes(instance_id: &str, skip_pids: &[u32])
503565 if skip_pids. contains ( & info. pbi_ppid ) {
504566 continue ;
505567 }
568+ // Grandchild check: the harness is spawned with process_group(0), so
569+ // all descendants share its PGID. If this process's PGID matches a
570+ // tracked harness PID, it's a live descendant — not an orphan.
571+ let pgid = unsafe { libc:: getpgid ( pid) } ;
572+ if pgid > 0 && skip_pids. contains ( & ( pgid as u32 ) ) {
573+ continue ;
574+ }
506575 if !process_has_buzz_marker ( upid, instance_id) {
507576 continue ;
508577 }
@@ -514,7 +583,7 @@ pub(crate) fn sweep_system_agent_processes(instance_id: &str, skip_pids: &[u32])
514583 "buzz-desktop: system sweep found {} orphaned agent process(es), cleaning up" ,
515584 orphans. len( )
516585 ) ;
517- sigterm_then_sigkill ( & orphans) ;
586+ resolve_pgids_and_kill ( & orphans) ;
518587 }
519588}
520589
@@ -530,6 +599,17 @@ fn read_ppid_linux(pid: u32) -> Option<u32> {
530599 ppid_str. parse :: < u32 > ( ) . ok ( )
531600}
532601
602+ /// Read the process group ID from /proc/<pid>/stat. Same parsing strategy as
603+ /// `read_ppid_linux` — field 3 after the closing ')' is the PGID.
604+ #[ cfg( all( unix, not( target_os = "macos" ) ) ) ]
605+ fn read_pgid_linux ( pid : u32 ) -> Option < u32 > {
606+ let stat = std:: fs:: read_to_string ( format ! ( "/proc/{pid}/stat" ) ) . ok ( ) ?;
607+ let after_comm = stat. rsplit_once ( ')' ) ?. 1 ;
608+ // Fields after ')': " S ppid pgid ..."
609+ let pgid_str = after_comm. split_whitespace ( ) . nth ( 2 ) ?;
610+ pgid_str. parse :: < u32 > ( ) . ok ( )
611+ }
612+
533613#[ cfg( all( unix, not( target_os = "macos" ) ) ) ]
534614pub ( crate ) fn sweep_system_agent_processes ( instance_id : & str , skip_pids : & [ u32 ] ) {
535615 let my_uid = unsafe { libc:: getuid ( ) } ;
@@ -575,6 +655,14 @@ pub(crate) fn sweep_system_agent_processes(instance_id: &str, skip_pids: &[u32])
575655 continue ;
576656 }
577657 }
658+ // Grandchild check: the harness is spawned with process_group(0), so
659+ // all descendants share its PGID. If this process's PGID matches a
660+ // tracked harness PID, it's a live descendant — not an orphan.
661+ if let Some ( pgid) = read_pgid_linux ( upid) {
662+ if skip_pids. contains ( & pgid) {
663+ continue ;
664+ }
665+ }
578666 orphans. push ( pid) ;
579667 }
580668
@@ -583,7 +671,7 @@ pub(crate) fn sweep_system_agent_processes(instance_id: &str, skip_pids: &[u32])
583671 "buzz-desktop: system sweep found {} orphaned agent process(es), cleaning up" ,
584672 orphans. len( )
585673 ) ;
586- sigterm_then_sigkill ( & orphans) ;
674+ resolve_pgids_and_kill ( & orphans) ;
587675 }
588676}
589677
@@ -613,7 +701,7 @@ pub(crate) fn sweep_system_agent_processes_with_grace(
613701 "buzz-desktop: periodic sweep confirmed {} orphaned agent process(es), cleaning up" ,
614702 confirmed. len( )
615703 ) ;
616- sigterm_then_sigkill ( & confirmed) ;
704+ resolve_pgids_and_kill ( & confirmed) ;
617705 }
618706 current
619707}
@@ -694,6 +782,13 @@ pub(crate) fn collect_same_instance_orphans(
694782 if skip_pids. contains ( & info. pbi_ppid ) {
695783 continue ;
696784 }
785+ // Grandchild check: the harness is spawned with process_group(0), so
786+ // all descendants share its PGID. If this process's PGID matches a
787+ // tracked harness PID, it's a live descendant — not an orphan.
788+ let pgid = unsafe { libc:: getpgid ( pid) } ;
789+ if pgid > 0 && skip_pids. contains ( & ( pgid as u32 ) ) {
790+ continue ;
791+ }
697792 if process_has_buzz_marker ( upid, instance_id) {
698793 orphans. insert ( upid) ;
699794 }
@@ -747,6 +842,14 @@ pub(crate) fn collect_same_instance_orphans(
747842 continue ;
748843 }
749844 }
845+ // Grandchild check: the harness is spawned with process_group(0), so
846+ // all descendants share its PGID. If this process's PGID matches a
847+ // tracked harness PID, it's a live descendant — not an orphan.
848+ if let Some ( pgid) = read_pgid_linux ( upid) {
849+ if skip_pids. contains ( & pgid) {
850+ continue ;
851+ }
852+ }
750853 orphans. insert ( upid) ;
751854 }
752855 orphans
@@ -1141,7 +1244,7 @@ pub(crate) fn reap_dead_instance_agents(our_instance_id: &str, skip_pids: &[u32]
11411244 "buzz-desktop: reaping {} orphaned agent(s) from dead instance '{instance_id}'" ,
11421245 agent_pids. len( )
11431246 ) ;
1144- sigterm_then_sigkill ( agent_pids) ;
1247+ resolve_pgids_and_kill ( agent_pids) ;
11451248 }
11461249}
11471250
@@ -1199,7 +1302,7 @@ pub(crate) fn reap_dead_instance_agents(our_instance_id: &str, skip_pids: &[u32]
11991302 "buzz-desktop: reaping {} orphaned agent(s) from dead instance '{instance_id}'" ,
12001303 agent_pids. len( )
12011304 ) ;
1202- sigterm_then_sigkill ( agent_pids) ;
1305+ resolve_pgids_and_kill ( agent_pids) ;
12031306 }
12041307}
12051308
0 commit comments