Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Rewire Patrol investigation after chat restart

rcourtman870e2f7f97d0
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index c46333635b..aabbb29de4 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -2977,7 +2977,7 @@ a new API state machine, queue contract, or verification-accounting field.
a new public API payload shape.
22. Keep hosted billing-state quickstart grants retired from new shared API flows: `internal/api/hosted_entitlement_refresh.go`, hosted signup, and trial-state construction must not auto-grant or refresh quickstart inventory for new workspaces, while low-level billing-state readers may still preserve historical fields that already exist on disk.
23. Keep hosted AI settings bootstrap on the shared API contract as a retired path: `internal/api/ai_hosted_runtime.go`, `internal/api/ai_handlers.go`, `internal/api/ai_handler.go`, and `internal/api/contract_test.go` must treat a missing `ai.enc` in hosted mode as an unconfigured BYOK/local-provider state, not as a machine-owned `quickstart:pulse-hosted` bootstrap condition. Hosted tenant reads may inherit billing state for commercial authorization, but they must not create quickstart-backed AI config or call the quickstart bootstrap upstream route.
-24. Keep post-boot AI enablement contract-backed on the shared AI/mobile approval surface: `internal/api/ai_handler.go`, `internal/api/ai_handlers.go`, `internal/api/router_routes_ai_relay.go`, and `internal/api/contract_test.go` must turn the governed approvals-list API into the canonical empty-list payload as soon as settings-driven AI enablement succeeds, rather than leaving that surface on `503 Approval store not initialized` until some separate startup-only side effect happens.
+24. Keep post-boot AI enablement contract-backed on the shared AI/mobile approval surface: `internal/api/ai_handler.go`, `internal/api/ai_handlers.go`, `internal/api/router.go`, `internal/api/router_routes_ai_relay.go`, and `internal/api/contract_test.go` must turn the governed approvals-list API into the canonical empty-list payload as soon as settings-driven AI enablement succeeds, rather than leaving that surface on `503 Approval store not initialized` until some separate startup-only side effect happens. The same post-boot lifecycle owns the investigation surface: a successful in-process chat start or restart must reapply the same live dependency wiring as cold startup, including the Enterprise investigation orchestrator and Patrol circuit breaker, so enabling Intelligence after boot cannot leave finding investigations on `503 Investigation orchestrator not initialized` until the server restarts.
25. Keep infrastructure summary chart transport contract-backed on the shared API surface: `internal/api/router.go`, `internal/api/contract_test.go`, and frontend infrastructure summary consumers must normalize long-range mixed-cadence history into equal-time summary buckets before shipping the infrastructure charts API payload, so 7-day and 30-day summary cards do not expose compressed right-edge tails just because recent samples arrive at a finer storage resolution.
26. Keep long-range workload chart transport time-proportional on the shared API surface: `internal/api/router.go`, `internal/api/contract_test.go`, and workload chart consumers must cap mixed-cadence workload history by equal-time buckets rather than raw point index for the per-workload and aggregate workload chart APIs, so 7-day and 30-day workload cards do not bunch recent samples at the right edge just because recent telemetry is stored more densely.
27. Keep chart timestamp precision canonical on that same shared API surface: when `internal/api/router.go` serializes monitoring history into infrastructure or workload chart payloads, it must preserve canonical millisecond timestamps from the shared monitoring timeline instead of rounding through whole-second conversion, so seeded mock history and live appends collapse onto one operator-visible timeline instead of appearing as duplicated tail samples.
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 5cadfa5a3c..bc3e027d12 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -94,6 +94,28 @@ func TestContractPatrolInternalBridgePreservesBoundedToolAuthority(t *testing.T)
}
}
+func TestContractAIChatRestartReappliesLiveRuntimeWiring(t *testing.T) {
+ mockSvc := &MockAIService{}
+ mockSvc.On("Restart", tmock.Anything, tmock.Anything).Return(nil)
+ mockSvc.On("IsRunning").Return(true)
+
+ handler := &AIHandler{}
+ setUnexportedField(t, handler, "defaultService", mockSvc)
+ initializerCalls := 0
+ handler.SetServiceInitializer(func(context.Context, AIService) {
+ initializerCalls++
+ })
+ initializerCalls = 0
+
+ router := &Router{aiHandler: handler}
+ router.RestartAIChat(context.Background())
+
+ if initializerCalls == 0 {
+ t.Fatal("expected successful chat restart to reapply live runtime wiring")
+ }
+ mockSvc.AssertExpectations(t)
+}
+
func TestContract_ConfigTransferRoutesUseCanonicalPreHandlerAuthorization(t *testing.T) {
routesSource, err := os.ReadFile(filepath.Clean("router_routes_registration.go"))
if err != nil {
diff --git a/internal/api/router.go b/internal/api/router.go
index 7f73071480..43105f0cbf 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -3091,27 +3091,38 @@ func (r *Router) StartAIChat(ctx context.Context) {
return
}
- // Ensure default-org chat service has org-scoped dependencies wired.
+ r.wireAIChatRuntimeAfterStart()
+}
+
+// wireAIChatRuntimeAfterStart applies the dependencies that require a live
+// chat service. Settings-driven hot enablement and provider restarts must use
+// the same wiring path as cold startup; otherwise Patrol detection can run
+// while investigation remains unavailable until the whole server restarts.
+func (r *Router) wireAIChatRuntimeAfterStart() {
+ if r == nil || r.aiHandler == nil {
+ return
+ }
+
defaultOrgCtx := context.WithValue(context.Background(), OrgIDContextKey, "default")
+ if !r.aiHandler.IsRunning(defaultOrgCtx) {
+ return
+ }
if service := r.aiHandler.GetService(defaultOrgCtx); service != nil {
r.wireAIChatDependenciesForService(defaultOrgCtx, service)
}
- // Wire up investigation orchestrator now that chat service is ready
- // This must happen after Start() because the orchestrator needs the chat service
+ // Investigation orchestration depends on the live chat service and must be
+ // rebuilt after both first enablement and an in-process provider restart.
if r.aiSettingsHandler != nil {
r.aiSettingsHandler.WireOrchestratorAfterChatStart()
- }
-
- // Wire circuit breaker for patrol if AI is running
- if r.aiHandler != nil && r.aiHandler.IsRunning(defaultOrgCtx) {
- if r.aiSettingsHandler != nil {
- if patrolSvc := r.aiSettingsHandler.GetAIService(defaultOrgCtx).GetPatrolService(); patrolSvc != nil {
- // Wire circuit breaker for resilient AI API calls
- if breaker := r.aiSettingsHandler.GetCircuitBreakerForOrg("default"); breaker != nil {
- patrolSvc.SetCircuitBreaker(breaker)
- log.Info().Msg("AI patrol circuit breaker wired")
- }
+ aiService := r.aiSettingsHandler.GetAIService(defaultOrgCtx)
+ if aiService == nil {
+ return
+ }
+ if patrolSvc := aiService.GetPatrolService(); patrolSvc != nil {
+ if breaker := r.aiSettingsHandler.GetCircuitBreakerForOrg("default"); breaker != nil {
+ patrolSvc.SetCircuitBreaker(breaker)
+ log.Info().Msg("AI patrol circuit breaker wired")
}
}
}
@@ -3789,6 +3800,7 @@ func (r *Router) RestartAIChat(ctx context.Context) {
if err := r.aiHandler.Restart(ctx); err != nil {
log.Error().Err(err).Msg("Failed to restart AI chat service")
} else {
+ r.wireAIChatRuntimeAfterStart()
log.Info().Msg("AI chat service restarted with new configuration")
}
}
diff --git a/internal/api/router_routes_additional_test.go b/internal/api/router_routes_additional_test.go
index 5a0426d6c8..80bac26108 100644
--- a/internal/api/router_routes_additional_test.go
+++ b/internal/api/router_routes_additional_test.go
@@ -526,6 +526,7 @@ func TestRouterAIChatControl(t *testing.T) {
mockSvc := &MockAIService{}
mockSvc.On("Stop", mock.Anything).Return(nil)
mockSvc.On("Restart", mock.Anything, mock.Anything).Return(nil)
+ mockSvc.On("IsRunning").Return(true)
handler := &AIHandler{}
setUnexportedField(t, handler, "defaultService", mockSvc)