package runtime import ( "context" "time" "chorus/internal/logging" "chorus/pkg/health" "chorus/pkg/shutdown" "chorus/pubsub" ) // simpleLogger implements basic logging for shutdown and health systems type simpleLogger struct { logger logging.Logger } func (l *simpleLogger) Info(msg string, args ...interface{}) { l.logger.Info(msg, args...) } func (l *simpleLogger) Warn(msg string, args ...interface{}) { l.logger.Warn(msg, args...) } func (l *simpleLogger) Error(msg string, args ...interface{}) { l.logger.Error(msg, args...) } // StartAgentMode runs the autonomous agent with all standard behaviors func (r *SharedRuntime) StartAgentMode() error { // Announce capabilities and role go r.announceAvailability() go r.announceCapabilitiesOnChange() go r.announceRoleOnStartup() // Start status reporting go r.statusReporter() r.Logger.Info("🔍 Listening for peers on container network...") r.Logger.Info("📡 Ready for task coordination and meta-discussion") r.Logger.Info("🎯 HMMM collaborative reasoning enabled") // === Comprehensive Health Monitoring & Graceful Shutdown === shutdownManager := shutdown.NewManager(30*time.Second, &simpleLogger{logger: r.Logger}) healthManager := health.NewManager(r.Node.ID().ShortString(), AppVersion, &simpleLogger{logger: r.Logger}) healthManager.SetShutdownManager(shutdownManager) // Register health checks r.setupHealthChecks(healthManager) // Register components for graceful shutdown r.setupGracefulShutdown(shutdownManager, healthManager) // Start health monitoring if err := healthManager.Start(); err != nil { return err } r.HealthManager = healthManager r.Logger.Info("❤️ Health monitoring started") // Start health HTTP server if err := healthManager.StartHTTPServer(r.Config.Network.HealthPort); err != nil { r.Logger.Error("❌ Failed to start health HTTP server: %v", err) } else { r.Logger.Info("🏥 Health endpoints available at http://localhost:%d/health", r.Config.Network.HealthPort) } // Start shutdown manager shutdownManager.Start() r.ShutdownManager = shutdownManager r.Logger.Info("🛡️ Graceful shutdown manager started") r.Logger.Info("✅ CHORUS agent system fully operational with health monitoring") // Wait for graceful shutdown shutdownManager.Wait() r.Logger.Info("✅ CHORUS agent system shutdown completed") return nil } // announceAvailability broadcasts current working status for task assignment func (r *SharedRuntime) announceAvailability() { ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for ; ; <-ticker.C { currentTasks := r.TaskTracker.GetActiveTasks() maxTasks := r.TaskTracker.GetMaxTasks() isAvailable := len(currentTasks) < maxTasks status := "ready" if len(currentTasks) >= maxTasks { status = "busy" } else if len(currentTasks) > 0 { status = "working" } availability := map[string]interface{}{ "node_id": r.Node.ID().ShortString(), "available_for_work": isAvailable, "current_tasks": len(currentTasks), "max_tasks": maxTasks, "last_activity": time.Now().Unix(), "status": status, "timestamp": time.Now().Unix(), } if err := r.PubSub.PublishBzzzMessage(pubsub.AvailabilityBcast, availability); err != nil { r.Logger.Error("❌ Failed to announce availability: %v", err) } } } // statusReporter provides periodic status updates func (r *SharedRuntime) statusReporter() { ticker := time.NewTicker(60 * time.Second) defer ticker.Stop() for ; ; <-ticker.C { peers := r.Node.ConnectedPeers() r.Logger.Info("📊 Status: %d connected peers", peers) } } // announceCapabilitiesOnChange announces capabilities when they change func (r *SharedRuntime) announceCapabilitiesOnChange() { // Implementation from CHORUS would go here // For now, just log that capabilities would be announced r.Logger.Info("📢 Agent capabilities announcement enabled") } // announceRoleOnStartup announces role when the agent starts func (r *SharedRuntime) announceRoleOnStartup() { // Implementation from CHORUS would go here // For now, just log that role would be announced r.Logger.Info("🎭 Agent role announcement enabled") } func (r *SharedRuntime) setupHealthChecks(healthManager *health.Manager) { // Add BACKBEAT health check if r.BackbeatIntegration != nil { backbeatCheck := &health.HealthCheck{ Name: "backbeat", Description: "BACKBEAT timing integration health", Interval: 30 * time.Second, Timeout: 10 * time.Second, Enabled: true, Critical: false, Checker: func(ctx context.Context) health.CheckResult { healthInfo := r.BackbeatIntegration.GetHealth() connected, _ := healthInfo["connected"].(bool) result := health.CheckResult{ Healthy: connected, Details: healthInfo, Timestamp: time.Now(), } if connected { result.Message = "BACKBEAT integration healthy and connected" } else { result.Message = "BACKBEAT integration not connected" } return result }, } healthManager.RegisterCheck(backbeatCheck) } // Add other health checks (P2P, DHT, etc.) // Implementation from CHORUS would go here } func (r *SharedRuntime) setupGracefulShutdown(shutdownManager *shutdown.Manager, healthManager *health.Manager) { // Register components for graceful shutdown // Implementation would register all components that need graceful shutdown r.Logger.Info("🛡️ Graceful shutdown components registered") }