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.

Make Patrol readiness reflect model checks

rcourtman98bf51efa418
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 3b644a7f33..2edb6068c5 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -6313,6 +6313,18 @@ synthetic `ai-service` runtime failure just as a successful full Patrol run
does, without loosening ordinary scoped finding reconciliation for
infrastructure issues. A soft-warning preflight where the provider responds but
the model does not emit a tool call is not sufficient recovery evidence.
+The same selected-route preflight is part of the live Patrol readiness gate,
+not merely settings-page diagnostics. A completed matching billing, quota,
+authentication, connection, unsupported-tool, or timeout failure makes Patrol
+not ready and disables new runs; a matching accepted response without a tool
+call remains a warning; and a matching observed tool call clears a static
+gateway "unverified" warning unless the deeper model-readiness advisor has
+stronger contrary evidence. Preflight evidence must match both provider and
+bare model ID before it can influence runtime status or UI copy. Any Patrol
+transport change clears the cache and advances a generation fence, so an old
+slow check cannot finish later and overwrite evidence for the restored or newly
+selected route. An interrupted check is not provider evidence and must not
+block Patrol by itself.
Because those findings represent Patrol blindness rather than operator-triaged
infrastructure noise, the Patrol runtime must also reject manual acknowledge,
snooze, dismiss, resolve, and suppress actions against synthetic `ai-service`
diff --git a/internal/ai/patrol_preflight.go b/internal/ai/patrol_preflight.go
index 76212cda3b..9994c711db 100644
--- a/internal/ai/patrol_preflight.go
+++ b/internal/ai/patrol_preflight.go
@@ -69,12 +69,19 @@ type patrolPreflightCache struct {
mu sync.RWMutex
result *PatrolPreflightResult
recordedAt time.Time
+ generation uint64
}
// CachedPatrolPreflight returns the most recent preflight result and the
// time it was recorded, or nil + zero time if preflight has never run on
// this Service instance.
func (s *Service) CachedPatrolPreflight() (*PatrolPreflightResult, time.Time) {
+ if s == nil {
+ return nil, time.Time{}
+ }
+ s.mu.RLock()
+ cfg := s.cfg
+ s.mu.RUnlock()
s.patrolPreflightCache.mu.RLock()
defer s.patrolPreflightCache.mu.RUnlock()
if s.patrolPreflightCache.result == nil {
@@ -82,20 +89,61 @@ func (s *Service) CachedPatrolPreflight() (*PatrolPreflightResult, time.Time) {
}
// Return a defensive copy so callers can't mutate the cache.
clone := *s.patrolPreflightCache.result
+ if cfg != nil && !patrolPreflightMatchesConfig(&clone, cfg) {
+ return nil, time.Time{}
+ }
return &clone, s.patrolPreflightCache.recordedAt
}
// recordPatrolPreflight stores the result in the cache. Called after
// every RunPatrolToolPreflight invocation so the most recent outcome is
// always observable, including failures.
-func (s *Service) recordPatrolPreflight(result PatrolPreflightResult, at time.Time) {
+func (s *Service) recordPatrolPreflight(result PatrolPreflightResult, at time.Time, generation uint64) {
+ s.mu.RLock()
+ cfg := s.cfg
+ s.mu.RUnlock()
s.patrolPreflightCache.mu.Lock()
defer s.patrolPreflightCache.mu.Unlock()
+ if generation != s.patrolPreflightCache.generation || cfg != nil && !patrolPreflightMatchesConfig(&result, cfg) {
+ return
+ }
clone := result
s.patrolPreflightCache.result = &clone
s.patrolPreflightCache.recordedAt = at
}
+func (s *Service) patrolPreflightGeneration() uint64 {
+ s.patrolPreflightCache.mu.RLock()
+ defer s.patrolPreflightCache.mu.RUnlock()
+ return s.patrolPreflightCache.generation
+}
+
+// InvalidatePatrolPreflight clears lightweight route evidence and advances a
+// generation fence. Any in-flight check started against the previous
+// transport can finish, but it cannot overwrite evidence for the new route.
+func (s *Service) InvalidatePatrolPreflight() {
+ if s == nil {
+ return
+ }
+ s.patrolPreflightCache.mu.Lock()
+ s.patrolPreflightCache.result = nil
+ s.patrolPreflightCache.recordedAt = time.Time{}
+ s.patrolPreflightCache.generation++
+ s.patrolPreflightCache.mu.Unlock()
+}
+
+func patrolPreflightMatchesConfig(result *PatrolPreflightResult, cfg *config.AIConfig) bool {
+ if result == nil || cfg == nil {
+ return false
+ }
+ selected := strings.TrimSpace(cfg.GetPatrolModel())
+ if selected == "" {
+ selected = strings.TrimSpace(cfg.GetChatModel())
+ }
+ provider, model := config.ParseModelString(selected)
+ return strings.EqualFold(provider, strings.TrimSpace(result.Provider)) && model == strings.TrimSpace(result.Model)
+}
+
// TriggerPatrolPreflightAsync runs RunPatrolToolPreflight in the
// background so callers (notably the settings save handler) don't block
// on a 5-10 second LLM round-trip. The result populates the preflight
@@ -103,8 +151,10 @@ func (s *Service) recordPatrolPreflight(result PatrolPreflightResult, at time.Ti
// patrol_preflight snapshot. RunPatrolToolPreflight owns the route-aware
// deadline so the asynchronous and manual entrypoints cannot drift.
func (s *Service) TriggerPatrolPreflightAsync(provider, model string) {
+ s.InvalidatePatrolPreflight()
+ generation := s.patrolPreflightGeneration()
go func() {
- s.RunPatrolToolPreflight(context.Background(), provider, model)
+ s.runPatrolToolPreflight(context.Background(), provider, model, generation)
}()
}
@@ -124,6 +174,12 @@ func (s *Service) TriggerPatrolPreflightAsync(provider, model string) {
// billed-feature-shaped, and should not pollute the AI usage dashboard.
// This is the only intentional exception to the cost-recording audit.
func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, model string) PatrolPreflightResult {
+ return s.runPatrolToolPreflight(ctx, providerName, model, s.patrolPreflightGeneration())
+}
+
+// cost-recording-exempt: connectivity/tool-call self-test, not user workload;
+// this internal implementation preserves the public preflight exemption.
+func (s *Service) runPatrolToolPreflight(ctx context.Context, providerName, model string, generation uint64) PatrolPreflightResult {
started := time.Now()
s.mu.RLock()
@@ -141,7 +197,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Title = "Pulse Patrol: Preflight succeeded"
result.Summary = "Demo mode simulates Patrol's tool-call check; no provider was contacted"
result.DurationMs = time.Since(started).Milliseconds()
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
@@ -151,7 +207,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Summary = "Pulse Intelligence settings could not be loaded"
result.Recommendation = "Confirm Pulse settings persistence is healthy, then re-run preflight."
result.DurationMs = time.Since(started).Milliseconds()
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
if !cfg.Enabled {
@@ -160,7 +216,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Summary = "Pulse Intelligence is turned off"
result.Recommendation = "Turn on Pulse Intelligence on the Provider & Models settings page, then run Check Patrol model again."
result.DurationMs = time.Since(started).Milliseconds()
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
@@ -174,7 +230,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Summary = "Patrol has no model selected"
result.Recommendation = "Select a Patrol model in Patrol settings. If no models are listed, add a provider API key or an Ollama server on the Provider & Models settings page first."
result.DurationMs = time.Since(started).Milliseconds()
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
@@ -199,7 +255,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
if err != nil {
applyPatrolPreflightDiagnostic(&result, err)
result.DurationMs = time.Since(started).Milliseconds()
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
@@ -239,7 +295,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
if err != nil {
applyPatrolPreflightDiagnostic(&result, err)
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
@@ -249,7 +305,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Cause = PatrolFailureCauseNone
result.Title = "Pulse Patrol: Preflight succeeded"
result.Summary = "Provider accepted the preflight request and the model emitted a tool call."
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
s.resolvePatrolRuntimeFailureAfterToolPreflight(result)
return result
}
@@ -262,7 +318,7 @@ func (s *Service) RunPatrolToolPreflight(ctx context.Context, providerName, mode
result.Title = "Pulse Patrol: Model did not emit a tool call during preflight"
result.Summary = "Provider accepted the preflight request but the model did not emit a tool call. Patrol may still work in practice."
result.Recommendation = "Trigger a real Patrol run to confirm tool calling. If that fails, switch to a model with stronger tool-following behaviour."
- s.recordPatrolPreflight(result, time.Now())
+ s.recordPatrolPreflight(result, time.Now(), generation)
return result
}
diff --git a/internal/ai/patrol_readiness.go b/internal/ai/patrol_readiness.go
index f51f799081..02d618aa85 100644
--- a/internal/ai/patrol_readiness.go
+++ b/internal/ai/patrol_readiness.go
@@ -185,6 +185,24 @@ func (s *Service) PatrolRuntimeReadiness() PatrolConfigReadiness {
if !base.Ready || cfg == nil {
return base
}
+ if preflight, _ := s.CachedPatrolPreflight(); preflight != nil {
+ if !preflight.Success {
+ // Cancellation is not evidence about the route. Every completed
+ // failure is: Patrol must not claim it is ready while the exact
+ // selected provider/model cannot pass the lightweight tool check.
+ if preflight.Cause != PatrolFailureCauseInterrupted {
+ cause := preflight.Cause
+ if cause == PatrolFailureCauseNone || cause == "" {
+ cause = PatrolFailureCauseModelToolSupportUnverified
+ }
+ return patrolConfigReadiness(base.Provider, base.Model, PatrolReadinessNotReady, cause, patrolPreflightReadinessSummary(preflight))
+ }
+ } else if !preflight.ToolCallObserved {
+ return patrolConfigReadiness(base.Provider, base.Model, PatrolReadinessWarning, PatrolFailureCauseModelToolSupportUnverified, patrolPreflightReadinessSummary(preflight))
+ } else if base.Cause == PatrolFailureCauseModelToolSupportUnverified {
+ base = patrolConfigReadiness(base.Provider, base.Model, PatrolReadinessReady, PatrolFailureCauseNone, "The selected Patrol model passed the live tool-call check.")
+ }
+ }
advisor, _ := s.CachedPatrolModelReadiness()
if advisor == nil {
return base
@@ -216,3 +234,16 @@ func (s *Service) PatrolRuntimeReadiness() PatrolConfigReadiness {
return patrolConfigReadiness(advisor.Provider, advisor.Model, PatrolReadinessNotReady, cause, suitability.Summary)
}
}
+
+func patrolPreflightReadinessSummary(result *PatrolPreflightResult) string {
+ if result == nil {
+ return "The selected Patrol model has not passed the live tool-call check."
+ }
+ if summary := strings.TrimSpace(result.Summary); summary != "" {
+ return summary
+ }
+ if title := strings.TrimSpace(result.Title); title != "" {
+ return title
+ }
+ return "The selected Patrol model has not passed the live tool-call check."
+}
diff --git a/internal/ai/patrol_readiness_test.go b/internal/ai/patrol_readiness_test.go
index 36486d6581..4adda6d59d 100644
--- a/internal/ai/patrol_readiness_test.go
+++ b/internal/ai/patrol_readiness_test.go
@@ -2,6 +2,7 @@ package ai
import (
"testing"
+ "time"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
@@ -149,6 +150,99 @@ func TestEvaluatePatrolConfigReadiness_AssignsStableCause(t *testing.T) {
}
}
+func TestPatrolRuntimeReadinessUsesMatchingPreflightEvidence(t *testing.T) {
+ cfg := config.NewDefaultAIConfig()
+ cfg.Enabled = true
+ cfg.OpenRouterAPIKey = "test-key"
+ cfg.PatrolModel = "openrouter:test/model"
+
+ tests := []struct {
+ name string
+ preflight PatrolPreflightResult
+ wantStatus string
+ wantCause PatrolFailureCause
+ wantReady bool
+ }{
+ {
+ name: "completed billing failure blocks Patrol",
+ preflight: PatrolPreflightResult{
+ Provider: "openrouter", Model: "test/model",
+ Cause: PatrolFailureCauseProviderBilling, Summary: "Provider billing or quota issue",
+ },
+ wantStatus: PatrolReadinessNotReady,
+ wantCause: PatrolFailureCauseProviderBilling,
+ wantReady: false,
+ },
+ {
+ name: "accepted request without a tool remains a warning",
+ preflight: PatrolPreflightResult{
+ Success: true, Provider: "openrouter", Model: "test/model",
+ Cause: PatrolFailureCauseModelToolSupportUnverified, Summary: "The model returned text instead of a tool call.",
+ },
+ wantStatus: PatrolReadinessWarning,
+ wantCause: PatrolFailureCauseModelToolSupportUnverified,
+ wantReady: true,
+ },
+ {
+ name: "observed tool call clears the generic gateway warning",
+ preflight: PatrolPreflightResult{
+ Success: true, ToolCallObserved: true, Provider: "openrouter", Model: "test/model",
+ },
+ wantStatus: PatrolReadinessReady,
+ wantCause: PatrolFailureCauseNone,
+ wantReady: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ service := &Service{cfg: cfg}
+ service.recordPatrolPreflight(tt.preflight, time.Now(), service.patrolPreflightGeneration())
+
+ readiness := service.PatrolRuntimeReadiness()
+ if readiness.Status != tt.wantStatus || readiness.Cause != tt.wantCause || readiness.Ready != tt.wantReady {
+ t.Fatalf("readiness = %+v, want status=%q cause=%q ready=%t", readiness, tt.wantStatus, tt.wantCause, tt.wantReady)
+ }
+ })
+ }
+}
+
+func TestPatrolPreflightEvidenceCannotCrossRoutesOrInvalidationGeneration(t *testing.T) {
+ cfg := config.NewDefaultAIConfig()
+ cfg.Enabled = true
+ cfg.OpenRouterAPIKey = "test-key"
+ cfg.PatrolModel = "openrouter:test/model"
+ service := &Service{cfg: cfg}
+
+ staleRoute := PatrolPreflightResult{
+ Provider: "ollama", Model: "qwen3:8b",
+ Cause: PatrolFailureCauseLatencyUnsuitable, Summary: "Local model timed out.",
+ }
+ service.recordPatrolPreflight(staleRoute, time.Now(), service.patrolPreflightGeneration())
+ if cached, _ := service.CachedPatrolPreflight(); cached != nil {
+ t.Fatalf("preflight from another route entered the selected-route cache: %+v", cached)
+ }
+
+ matchingFailure := PatrolPreflightResult{
+ Provider: "openrouter", Model: "test/model",
+ Cause: PatrolFailureCauseProviderBilling, Summary: "Old credentials failed.",
+ }
+ oldGeneration := service.patrolPreflightGeneration()
+ service.InvalidatePatrolPreflight()
+ service.recordPatrolPreflight(matchingFailure, time.Now(), oldGeneration)
+ if cached, _ := service.CachedPatrolPreflight(); cached != nil {
+ t.Fatalf("preflight from an invalidated generation replaced current evidence: %+v", cached)
+ }
+
+ matchingSuccess := PatrolPreflightResult{
+ Success: true, ToolCallObserved: true, Provider: "openrouter", Model: "test/model",
+ }
+ service.recordPatrolPreflight(matchingSuccess, time.Now(), service.patrolPreflightGeneration())
+ if cached, _ := service.CachedPatrolPreflight(); cached == nil || !cached.ToolCallObserved {
+ t.Fatalf("current-generation matching preflight was not cached: %+v", cached)
+ }
+}
+
func TestEvaluatePatrolConfigReadiness_NilConfigUsesAssistantPatrolSettingsCopy(t *testing.T) {
readiness := EvaluatePatrolConfigReadiness(nil)