@@ -64,19 +64,7 @@ pub fn runQueenCommand(allocator: Allocator, args: []const []const u8) !void {
6464 }
6565
6666 if (std .mem .eql (u8 , args [0 ], "supervisor" )) {
67- var sv_config = SupervisorConfig {};
68- var i : usize = 1 ;
69- while (i < args .len ) : (i += 1 ) {
70- if (std .mem .eql (u8 , args [i ], "--daemon" )) {
71- sv_config .daemon = true ;
72- } else if (std .mem .eql (u8 , args [i ], "--interval" )) {
73- i += 1 ;
74- if (i < args .len ) {
75- sv_config .interval_sec = std .fmt .parseInt (u64 , args [i ], 10 ) catch 60 ;
76- }
77- }
78- }
79- try runSupervisorMode (allocator , sv_config );
67+ try runSupervisorMode (allocator );
8068 } else if (std .mem .eql (u8 , args [0 ], "stop" )) {
8169 try stopSupervisor ();
8270 } else if (std .mem .eql (u8 , args [0 ], "start" )) {
@@ -537,9 +525,9 @@ const SupervisorConfig = struct {
537525// ═══════════════════════════════════════════════════════════════════════════════
538526
539527fn writePidFile () ! void {
540- const pid = std .os . linux .getpid ();
541- var dir = std .fs .cwd ().makeOpenPath (".trinity/queen" , .{}) catch | err | {
542- print (" {s}" ++ qt .E_CROWN ++ " Failed to create .trinity/queen: {s}{s}\n " , .{ RED , @errorName (err ), RESET });
528+ const pid = std .posix .getpid ();
529+ const dir = std .fs .cwd ().makeOpenPath (".trinity/queen" , .{}) catch | err | {
530+ print (" {s}" ++ qt .E_CROSS ++ " Failed to create .trinity/queen: {s}{s}\n " , .{ RED , @errorName (err ), RESET });
543531 return err ;
544532 };
545533 defer dir .close ();
@@ -621,7 +609,7 @@ const SupervisorLog = struct {
621609 mutex : std.Thread.Mutex ,
622610
623611 fn init () ! SupervisorLog {
624- var dir = try std .fs .cwd ().makeOpenPath (".trinity/queen" , .{});
612+ const dir = try std .fs .cwd ().makeOpenPath (".trinity/queen" , .{});
625613 defer dir .close ();
626614
627615 const file = try dir .createFile ("supervisor.log" , .{ .truncate = false });
@@ -639,7 +627,7 @@ const SupervisorLog = struct {
639627
640628 const timestamp = std .time .timestamp ();
641629 var buf : [4096 ]u8 = undefined ;
642- const msg = std .fmt .bufPrint (& buf , "[{d}] " ++ fmt ++ " \n " , .{timestamp } ++ args ) catch return ;
630+ const msg = std .fmt .bufPrint (& buf , "[{d}] {s} \n " , .{ timestamp , std . fmt . fmtFmt ( fmt , args ) } ) catch return ;
643631 try self .file .writeAll (msg );
644632 try self .file .sync ();
645633 }
@@ -649,36 +637,14 @@ const SupervisorLog = struct {
649637 }
650638};
651639
652- pub fn runSupervisorMode (allocator : Allocator , config : SupervisorConfig ) ! void {
640+ pub fn runSupervisorMode (allocator : Allocator ) ! void {
653641 const cortex = @import ("queen_cortex.zig" );
654642 const cerebellum = @import ("cerebellum.zig" );
655643 const queen_ofc = @import ("queen_ofc.zig" );
656644 const queen_vmpfc = @import ("queen_vmpfc.zig" );
657645
658- // Check if already running
659- if (isSupervisorRunning ()) {
660- print ("{s}" ++ qt .E_SIREN ++ " Supervisor is already running!{s}\n " , .{ RED , RESET });
661- print (" Use 'tri queen stop' to stop the existing instance.\n " , .{});
662- return error .AlreadyRunning ;
663- }
664-
665- // Write PID file
666- try writePidFile ();
667- defer removePidFile ();
668-
669- // Initialize logging
670- var log = try SupervisorLog .init ();
671- defer log .close ();
672-
673- try log .log ("Supervisor started with interval {d}s" , .{config .interval_sec });
674-
675- if (! config .daemon ) {
676- print ("\n {s}" ++ qt .E_CROWN ++ " Queen Supervisor Mode — Autonomous Monitoring{s}\n " , .{ GOLDEN , RESET });
677- print (" PID: {d}\n " , .{std .os .linux .getpid ()});
678- print (" Interval: {d}s\n " , .{config .interval_sec });
679- print (" Log: {s}\n " , .{qt .SUPERVISOR_LOG_PATH });
680- print (" Integrating all brain cells for self-healing...\n\n " , .{});
681- }
646+ print ("\n {s}" ++ qt .E_CROWN ++ " Queen Supervisor Mode — Autonomous Monitoring{s}\n " , .{ GOLDEN , RESET });
647+ print (" Integrating all brain cells for self-healing...\n\n " , .{});
682648
683649 const tg = qt .initTelegram ();
684650
@@ -695,16 +661,12 @@ pub fn runSupervisorMode(allocator: Allocator, config: SupervisorConfig) !void {
695661
696662 var cycle : u32 = 0 ;
697663 var last_heal_cycle : u32 = 0 ;
698- var running = std .atomic .Value (bool ).init (true );
699664
700- while (running . load ( .acquire ) ) {
665+ while (true ) {
701666 cycle += 1 ;
702667 const cycle_start = std .time .timestamp ();
703668
704- if (! config .daemon ) {
705- print ("{s}=== Supervisor Cycle #{d} ==={s}\n " , .{ GOLDEN , cycle , RESET });
706- }
707- try log .log ("Cycle #{d} started" , .{cycle });
669+ print ("{s}=== Supervisor Cycle #{d} ==={s}\n " , .{ GOLDEN , cycle , RESET });
708670
709671 // 1. Collect health from all PFC cells
710672 emitSupervisorStep (1 , 8 , "Collecting PFC cell health" );
@@ -730,7 +692,7 @@ pub fn runSupervisorMode(allocator: Allocator, config: SupervisorConfig) !void {
730692 emitSupervisorStep (3 , 8 , "System snapshot" );
731693 const snapshot = faculty_board .collectSnapshot (allocator ) catch | err | {
732694 print (" {s}" ++ qt .E_CROSS ++ " Snapshot failed: {s}{s}\n " , .{ RED , @errorName (err ), RESET });
733- sleepInterruptible ( & running , config . interval_sec );
695+ sleepInterval ( 300 );
734696 continue ;
735697 };
736698
@@ -811,49 +773,20 @@ pub fn runSupervisorMode(allocator: Allocator, config: SupervisorConfig) !void {
811773 }
812774
813775 // 10. Report status summary
814- if (! config .daemon ) {
815- print (" {s}" ++ qt .E_CHECK ++ " Cycle {d}: {s} | Critical: {d} | Warnings: {d}{s}\n\n " , .{
816- if (health_analysis .critical_count == 0 ) GREEN else RED ,
817- cycle ,
818- if (all_healthy ) "HEALTHY" else "RECOVERING" ,
819- health_analysis .critical_count ,
820- health_analysis .warning_count ,
821- RESET ,
822- });
823- }
824- try log .log ("Cycle #{d}: {s} critical={d} warnings={d}" , .{
776+ print (" {s}" ++ qt .E_CHECK ++ " Cycle {d}: {s} | Critical: {d} | Warnings: {d}{s}\n\n " , .{
777+ if (health_analysis .critical_count == 0 ) GREEN else RED ,
825778 cycle ,
826779 if (all_healthy ) "HEALTHY" else "RECOVERING" ,
827780 health_analysis .critical_count ,
828781 health_analysis .warning_count ,
782+ RESET ,
829783 });
830784
831785 // 11. Save supervisor state
832786 saveSupervisorState (cycle , pfc_health , health_analysis );
833787
834- // 12. Check if we should stop (PID file removed = stop request)
835- if (! isPidFileValid ()) {
836- try log .log ("PID file removed, shutting down gracefully" , .{});
837- running .store (false , .release );
838- break ;
839- }
840-
841- // Sleep before next cycle
842- try log .log ("Sleeping for {d}s" , .{config .interval_sec });
843- sleepInterruptible (& running , config .interval_sec );
844- }
845-
846- // Shutdown notification
847- try log .log ("Supervisor stopped after {d} cycles" , .{cycle });
848- if (tg .enabled ) {
849- var buf : [128 ]u8 = undefined ;
850- const msg = std .fmt .bufPrint (& buf , qt .E_CROWN ++ " Supervisor \xd0\xbe\xd1\x81\xd1\x82\xd0\xb0\xd0\xbd\xd0\xbe\xd0\xb2\xd0\xbb\xd0\xb5\xd0\xbd \n\n " ++ // остановлен
851- "Cycles: {d}" , .{cycle }) catch "" ;
852- queen_telegram .tgSend (tg , msg );
853- }
854-
855- if (! config .daemon ) {
856- print ("\n {s}" ++ qt .E_CHECK ++ " Supervisor stopped after {d} cycles{s}\n " , .{ GREEN , cycle , RESET });
788+ // Sleep before next cycle (5 min default for supervisor)
789+ sleepInterval (300 );
857790 }
858791}
859792
@@ -1794,10 +1727,11 @@ fn isPidFileValid() bool {
17941727 if (n == 0 ) return false ;
17951728
17961729 const pid_str = buf [0.. n ];
1797- const file_pid = std .fmt .parseInt (i32 , pid_str , 10 ) catch return false ;
1798- const my_pid = std .os .linux .getpid ();
1730+ const pid = std .fmt .parseInt (i32 , pid_str , 10 ) catch return false ;
17991731
1800- return file_pid == my_pid ;
1732+ // Check if process exists by sending signal 0
1733+ const result = std .posix .kill (pid , 0 );
1734+ return result == 0 ;
18011735}
18021736
18031737// ═══════════════════════════════════════════════════════════════════════════════
0 commit comments