@@ -21,6 +21,7 @@ const lsp = @import("lsp.zig");
2121const pcre2 = @import ("pcre2.zig" );
2222const watcher = @import ("watcher.zig" );
2323const pidfile = @import ("pidfile.zig" );
24+ const watcher_mgmt = @import ("watcher_mgmt.zig" );
2425const progress_mod = @import ("progress.zig" );
2526const fs_watch = @import ("fs_watch.zig" );
2627const weights = @import ("weights.zig" );
@@ -960,6 +961,110 @@ pub fn main() !void {
960961 }
961962 try stdout .flush ();
962963 },
964+ .list = > {
965+ var watchers = watcher_mgmt .discoverWatchers (allocator ) catch | err | {
966+ try stdout .print ("error: failed to discover watchers: {}\n " , .{err });
967+ try stdout .flush ();
968+ std .process .exit (1 );
969+ };
970+ defer {
971+ for (watchers .items ) | * w | w .deinit (allocator );
972+ watchers .deinit (allocator );
973+ }
974+
975+ if (watchers .items .len == 0 ) {
976+ try stdout .print ("No codescan watchers running.\n " , .{});
977+ try stdout .flush ();
978+ } else {
979+ // Get active cwds to mark orphans
980+ var cwds = watcher_mgmt .getActiveCwds (allocator ) catch std .ArrayListUnmanaged (watcher_mgmt .LsofEntry ){};
981+ defer {
982+ for (cwds .items ) | e | e .deinit (allocator );
983+ cwds .deinit (allocator );
984+ }
985+ watcher_mgmt .markActiveWatchers (watchers .items , cwds .items );
986+
987+ try stdout .print ("{s:<8}{s:<7}{s:<14}{s:<6}{s}\n " , .{ "PID" , "CPU%" , "UPTIME" , "USED" , "ROOT" });
988+ for (watchers .items ) | w | {
989+ try stdout .print ("{:<8}{s:<7}{s:<14}{s:<6}{s}\n " , .{
990+ @as (u32 , @intCast (w .pid )),
991+ w .cpu_pct ,
992+ w .elapsed ,
993+ if (w .active ) "yes" else "no" ,
994+ w .root ,
995+ }); }
996+ var orphan_count : usize = 0 ;
997+ for (watchers .items ) | w | {
998+ if (! w .active ) orphan_count += 1 ;
999+ }
1000+ try stdout .print ("\n {d} watcher{s}, {d} orphaned\n " , .{
1001+ watchers .items .len ,
1002+ if (watchers .items .len != 1 ) "s" else "" ,
1003+ orphan_count ,
1004+ });
1005+ try stdout .flush ();
1006+ }
1007+ },
1008+ .prune = > {
1009+ var watchers = watcher_mgmt .discoverWatchers (allocator ) catch | err | {
1010+ try stdout .print ("error: failed to discover watchers: {}\n " , .{err });
1011+ try stdout .flush ();
1012+ std .process .exit (1 );
1013+ };
1014+ defer {
1015+ for (watchers .items ) | * w | w .deinit (allocator );
1016+ watchers .deinit (allocator );
1017+ }
1018+
1019+ var cwds = watcher_mgmt .getActiveCwds (allocator ) catch std .ArrayListUnmanaged (watcher_mgmt .LsofEntry ){};
1020+ defer {
1021+ for (cwds .items ) | e | e .deinit (allocator );
1022+ cwds .deinit (allocator );
1023+ }
1024+ watcher_mgmt .markActiveWatchers (watchers .items , cwds .items );
1025+
1026+ var orphan_count : usize = 0 ;
1027+ for (watchers .items ) | w | {
1028+ if (! w .active ) orphan_count += 1 ;
1029+ }
1030+
1031+ if (orphan_count == 0 ) {
1032+ try stdout .print ("No orphaned watchers found.\n " , .{});
1033+ try stdout .flush ();
1034+ } else if (! parsed .confirm ) {
1035+ try stdout .print ("Orphaned watchers (no active sessions):\n " , .{});
1036+ for (watchers .items ) | w | {
1037+ if (! w .active ) {
1038+ try stdout .print (" PID {d} {s}\n " , .{ w .pid , w .root });
1039+ }
1040+ }
1041+ try stdout .print ("\n Run with --confirm to stop {d} orphaned watcher{s}.\n " , .{
1042+ orphan_count ,
1043+ if (orphan_count != 1 ) "s" else "" ,
1044+ });
1045+ try stdout .flush ();
1046+ } else {
1047+ var stopped : usize = 0 ;
1048+ for (watchers .items ) | w | {
1049+ if (! w .active ) {
1050+ if (watcher_mgmt .stopWatcher (w .pid )) {
1051+ try stdout .print ("Stopped watcher for {s} (PID {d})\n " , .{ w .root , w .pid });
1052+ stopped += 1 ;
1053+ } else {
1054+ try stdout .print ("Failed to stop watcher for {s} (PID {d})\n " , .{ w .root , w .pid });
1055+ }
1056+ }
1057+ }
1058+ const remaining = watchers .items .len - stopped ;
1059+ try stdout .print ("\n Stopped {d} orphaned watcher{s}. {d} active watcher{s} remain.\n " , .{
1060+ stopped ,
1061+ if (stopped != 1 ) "s" else "" ,
1062+ remaining ,
1063+ if (remaining != 1 ) "s" else "" ,
1064+ });
1065+ try stdout .flush ();
1066+ }
1067+ },
9631068 .run = > {
9641069 try ensureParentDir (settings .db_path );
9651070 // Open existing DB or create new one (don't destroy existing index)
0 commit comments