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.

Keep Patrol and AI data inside the organization that owns it

rcourtmanc97fea5e27e0
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index fcfd0b1b43..9f78283502 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -6290,6 +6290,23 @@ even for a resource ID that also exists in another organization.
`TestSummarizeToolReadsOnlyItsOrganizationsMetrics` and
`TestReportEngineReadsOnlyTheRequestOrganizationsMetrics` hold this.
+Patrol's organization-scoped surfaces resolve their sources from the Patrol's
+own organization, never the default organization's. Baseline learning reads
+the organization's monitor inventory and metrics history on every pass,
+`/api/ai/intelligence/anomalies` joins the request organization's baselines to
+that organization's read state, new-finding notifications use the
+organization's own notification manager, guest reachability probes discover
+and dispatch only to the organization's own agents, and `finding.created` agent
+events reach only that organization's subscribers. The instance relay's mobile
+push carries only the default organization's findings, as it carries only the
+default organization's alerts. An organization whose monitor or AI service
+cannot be resolved gets nothing rather than default-organization data.
+`TestRoutedAnomaliesUseRequestOrganizationResources`,
+`TestLearnBaselinesUsesOrganizationInventory`,
+`TestPatrolFindingNotificationManagerNeverFallsBackToDefault`,
+`TestStartPatrolForContextKeepsTenantFindingsOffInstanceRelay` and
+`TestAgentExecProber_StaysInsideItsOrganization` hold this.
+
The same canonical AI runtime now also records user-chat token
usage to the cost ledger. `chat.Service.ExecuteStream` was a
long-standing gap: the agentic loop accumulated token counts via
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 22be03f5d2..0d6116fa0a 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -3324,6 +3324,11 @@ a new API state machine, queue contract, or verification-accounting field.
scheduler health resolve the request organization's monitor and read state
on every request; an organization whose monitor cannot be resolved gets no
updates and no queued update check, never the default organization's.
+ Agent resource context, fleet context and Patrol-control status read active
+ findings through one provider that resolves the request organization's
+ Patrol per read (`ActiveFindingsForResource(resourceID, orgID)`), and
+ deploy candidates and preflights read the request organization's cluster
+ nodes; neither falls back to the default organization.
Deploy
preflights and jobs share `handleDeployJobStatus` /
`handleDeployJobEvents` in `internal/api/deploy_handlers.go`; recovery
diff --git a/internal/ai/patrol_prober.go b/internal/ai/patrol_prober.go
index 93774a73b4..f08aa45cc4 100644
--- a/internal/ai/patrol_prober.go
+++ b/internal/ai/patrol_prober.go
@@ -23,12 +23,15 @@ type guestPingProbeResult struct {
// agentExecProber implements GuestProber using the agent execution server.
type agentExecProber struct {
- server *agentexec.Server
+ server *agentexec.Server
+ organizationID string
}
-// NewAgentExecProber creates a GuestProber that runs ping commands via connected agents.
-func NewAgentExecProber(server *agentexec.Server) GuestProber {
- return &agentExecProber{server: server}
+// NewAgentExecProber creates a GuestProber that runs ping commands via
+// organizationID's connected agents. It never discovers or dispatches to
+// another organization's agents; empty means the default organization.
+func NewAgentExecProber(server *agentexec.Server, organizationID string) GuestProber {
+ return &agentExecProber{server: server, organizationID: organizationID}
}
// GetAgentForHost returns the agent ID for a given hostname, if connected.
@@ -36,7 +39,7 @@ func (p *agentExecProber) GetAgentForHost(hostname string) (string, bool) {
if p.server == nil {
return "", false
}
- return p.server.GetAgentForHost(hostname)
+ return p.server.GetAgentForHostForOrganization(p.organizationID, hostname)
}
// PingGuests pings a list of IPs from a specific agent and returns results.
@@ -82,7 +85,7 @@ func (p *agentExecProber) PingGuests(ctx context.Context, agentID string, ips []
return
}
- result, err := p.server.ExecuteCommand(ctx, agentID, agentexec.ExecuteCommandPayload{
+ result, err := p.server.ExecuteCommand(agentexec.WithOrganizationID(ctx, p.organizationID), agentID, agentexec.ExecuteCommandPayload{
RequestID: uuid.New().String(),
Command: pingCommandForIP(ip),
TargetType: "agent",
diff --git a/internal/ai/patrol_prober_test.go b/internal/ai/patrol_prober_test.go
index 669720d308..97bd654f13 100644
--- a/internal/ai/patrol_prober_test.go
+++ b/internal/ai/patrol_prober_test.go
@@ -71,7 +71,7 @@ func dialAndRegisterAgent(t *testing.T, serverURL, agentID, hostname string) *we
}
func TestAgentExecProber_PingGuestsNilServer(t *testing.T) {
- prober := NewAgentExecProber(nil)
+ prober := NewAgentExecProber(nil, "")
_, err := prober.PingGuests(context.Background(), "agent-1", []string{"10.0.0.1"})
if err == nil || !strings.Contains(err.Error(), "not available") {
t.Fatalf("expected nil-server error, got: %v", err)
@@ -79,7 +79,7 @@ func TestAgentExecProber_PingGuestsNilServer(t *testing.T) {
}
func TestAgentExecProber_PingGuestsEmptyIPs(t *testing.T) {
- prober := NewAgentExecProber(agentexec.NewServer(func(string, string, string) bool { return true }))
+ prober := NewAgentExecProber(agentexec.NewServer(func(string, string, string) bool { return true }), "")
results, err := prober.PingGuests(context.Background(), "agent-1", nil)
if err != nil {
t.Fatalf("PingGuests with empty ips returned error: %v", err)
@@ -99,7 +99,7 @@ func TestAgentExecProber_RoundTripViaAgentExecServer(t *testing.T) {
conn := dialAndRegisterAgent(t, ts.URL, "agent-1", "node-a")
defer conn.Close()
- prober := NewAgentExecProber(server)
+ prober := NewAgentExecProber(server, "")
if agentID, ok := prober.GetAgentForHost("node-a"); !ok || agentID != "agent-1" {
t.Fatalf("GetAgentForHost(node-a) = (%q, %v), want (agent-1, true)", agentID, ok)
}
@@ -189,8 +189,31 @@ func TestAgentExecProber_RoundTripViaAgentExecServer(t *testing.T) {
}
}
+func TestAgentExecProber_StaysInsideItsOrganization(t *testing.T) {
+ server := agentexec.NewServer(func(string, string, string) bool { return true })
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ server.HandleWebSocket(w, r)
+ }))
+ defer ts.Close()
+
+ conn := dialAndRegisterAgent(t, ts.URL, "agent-1", "node-a")
+ defer conn.Close()
+
+ prober := NewAgentExecProber(server, "org-a")
+ if agentID, ok := prober.GetAgentForHost("node-a"); ok {
+ t.Fatalf("org-a resolved a default-organization agent %q", agentID)
+ }
+ // The agent never replies, so a dispatch that reached it would fail by
+ // timing out; only the organization check reports it as not connected.
+ ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+ defer cancel()
+ if _, err := prober.PingGuests(ctx, "agent-1", []string{"10.0.0.1"}); err == nil || !strings.Contains(err.Error(), "agent-1 not connected") {
+ t.Fatalf("expected org-a not to reach the default-organization agent, got %v", err)
+ }
+}
+
func TestAgentExecProber_RejectsInvalidPingTargets(t *testing.T) {
- prober := NewAgentExecProber(agentexec.NewServer(func(string, string, string) bool { return true }))
+ prober := NewAgentExecProber(agentexec.NewServer(func(string, string, string) bool { return true }), "")
_, err := prober.PingGuests(context.Background(), "agent-1", []string{"10.0.0.1; rm -rf /"})
if err == nil || !strings.Contains(err.Error(), "invalid ping target") {
diff --git a/internal/api/agent_events.go b/internal/api/agent_events.go
index 82b166c7fb..4a4a298c2a 100644
--- a/internal/api/agent_events.go
+++ b/internal/api/agent_events.go
@@ -363,10 +363,13 @@ func writeAgentSSEEvent(w http.ResponseWriter, event AgentEvent) {
// finding is added; the implementation enriches the event with the
// canonical timestamp and a reference to the broadcaster, keeping
// the call site free of broadcaster plumbing.
-func (b *AgentEventBroadcaster) PublishFindingCreated(payload AgentEventFindingCreatedPayload) {
+// The event reaches only orgID's subscribers: a finding belongs to the
+// organization whose Patrol raised it.
+func (b *AgentEventBroadcaster) PublishFindingCreated(orgID string, payload AgentEventFindingCreatedPayload) {
b.Publish(AgentEvent{
Kind: AgentEventFindingCreated,
Payload: payload,
+ orgID: normalizeAgentEventOrgID(orgID),
})
}
diff --git a/internal/api/agent_events_auth_test.go b/internal/api/agent_events_auth_test.go
index 8ed7ce0aa0..d34c43cae9 100644
--- a/internal/api/agent_events_auth_test.go
+++ b/internal/api/agent_events_auth_test.go
@@ -420,7 +420,7 @@ func TestAgentEventTokenLifecycleMCPConsumer(t *testing.T) {
case <-ctx.Done():
t.Fatal("MCP stream never connected")
}
- router.agentEventBroadcaster.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "before-revocation"})
+ router.agentEventBroadcaster.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "before-revocation"})
select {
case n := <-notifications:
if n.Method != "notifications/finding.created" || !strings.Contains(string(n.Params), "before-revocation") {
diff --git a/internal/api/agent_events_test.go b/internal/api/agent_events_test.go
index 2b33b9f6c5..f016691a03 100644
--- a/internal/api/agent_events_test.go
+++ b/internal/api/agent_events_test.go
@@ -19,7 +19,7 @@ func TestAgentEventBroadcaster_SubscribeReceivesPublishedEvents(t *testing.T) {
events, unsubscribe := b.Subscribe()
defer unsubscribe()
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{
FindingID: "f1",
ResourceID: "vm:101",
Severity: "warning",
@@ -50,7 +50,7 @@ func TestAgentEventBroadcaster_UnsubscribeStopsDelivery(t *testing.T) {
// After unsubscribe the channel must be closed; receive should
// drain whatever was buffered then return zero-value with !ok.
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "f-after"})
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "f-after"})
select {
case _, open := <-events:
@@ -69,7 +69,7 @@ func TestAgentEventBroadcaster_FanOutToMultipleSubscribers(t *testing.T) {
a2, unsub2 := b.Subscribe()
defer unsub2()
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "fan-out"})
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "fan-out"})
deadline := time.After(time.Second)
for _, ch := range []<-chan AgentEvent{a1, a2} {
@@ -98,7 +98,7 @@ func TestAgentEventBroadcaster_DropsForSlowSubscriber(t *testing.T) {
// not block; if it did the goroutine would not finish and
// the test would time out.
for i := 0; i < 200; i++ {
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "spam"})
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "spam"})
}
close(done)
}()
@@ -117,7 +117,7 @@ func TestAgentEventBroadcaster_AssignsMonotonicIDs(t *testing.T) {
defer unsub()
for i := 0; i < 3; i++ {
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "mono"})
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "mono"})
}
var ids []uint64
@@ -192,7 +192,7 @@ func TestHandleAgentEvents_StreamsConnectedAndPublishedEvents(t *testing.T) {
// Publish a finding event so the read loop sees content beyond
// the connected event.
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{
FindingID: "f-stream",
Severity: "critical",
Title: "Streamed",
diff --git a/internal/api/agent_resource_context.go b/internal/api/agent_resource_context.go
index c88b45f3e8..a7655d7e49 100644
--- a/internal/api/agent_resource_context.go
+++ b/internal/api/agent_resource_context.go
@@ -293,32 +293,33 @@ type AgentResourceCapabilities struct {
// AgentFindingsProvider returns active findings as agent-stable snapshots and
// count-only aggregate evidence. The implementation lives outside this package
// (the patrol service holds the canonical findings store); this interface keeps
-// the api layer free of an `internal/ai` import.
+// the api layer free of an `internal/ai` import. Every read names the request
+// organization; implementations return only that organization's findings.
type AgentFindingsProvider interface {
- ActiveFindingsForResource(resourceID string) []AgentResourceFindingSnapshot
+ ActiveFindingsForResource(resourceID, orgID string) []AgentResourceFindingSnapshot
}
type AgentAggregateFindingsProvider interface {
- ActiveFindingCount() int
+ ActiveFindingCount(orgID string) int
}
type agentFindingsProvider struct {
- activeForResource func(resourceID string) []AgentResourceFindingSnapshot
- activeCount func() int
+ activeForResource func(resourceID, orgID string) []AgentResourceFindingSnapshot
+ activeCount func(orgID string) int
}
-func (p agentFindingsProvider) ActiveFindingsForResource(resourceID string) []AgentResourceFindingSnapshot {
+func (p agentFindingsProvider) ActiveFindingsForResource(resourceID, orgID string) []AgentResourceFindingSnapshot {
if p.activeForResource == nil {
return nil
}
- return p.activeForResource(resourceID)
+ return p.activeForResource(resourceID, orgID)
}
-func (p agentFindingsProvider) ActiveFindingCount() int {
+func (p agentFindingsProvider) ActiveFindingCount(orgID string) int {
if p.activeCount == nil {
return 0
}
- return p.activeCount()
+ return p.activeCount(orgID)
}
// AgentApprovalsProvider returns still-pending approvals as
@@ -532,7 +533,7 @@ func (h *AgentContextHandler) HandleResourceContext(w http.ResponseWriter, r *ht
// Active findings — in-memory lookup via the patrol findings store
// adapter wired at startup.
if h.findingsProvider != nil {
- bundle.ActiveFindings = h.findingsProvider.ActiveFindingsForResource(resourceID)
+ bundle.ActiveFindings = h.findingsProvider.ActiveFindingsForResource(resourceID, orgID)
if bundle.ActiveFindings == nil {
bundle.ActiveFindings = []AgentResourceFindingSnapshot{}
}
@@ -754,7 +755,7 @@ func (h *AgentContextHandler) HandleFleetContext(w http.ResponseWriter, r *http.
summary.MaintenanceWindowActive = state.IsInMaintenanceAt(now)
}
if h.findingsProvider != nil {
- findings := h.findingsProvider.ActiveFindingsForResource(canonical)
+ findings := h.findingsProvider.ActiveFindingsForResource(canonical, orgID)
summary.Findings = countFleetFindingsBySeverity(findings)
}
if count, ok := pendingByResource[canonical]; ok {
@@ -899,14 +900,14 @@ func (h *AgentContextHandler) HandleOperationsLoopStatus(w http.ResponseWriter,
for _, resource := range registry.List() {
canonical := unified.CanonicalResourceID(resource.ID)
if h.findingsProvider != nil {
- status.ActiveFindingCount += len(h.findingsProvider.ActiveFindingsForResource(canonical))
+ status.ActiveFindingCount += len(h.findingsProvider.ActiveFindingsForResource(canonical, orgID))
}
if pending := pendingByResource[canonical]; pending > 0 {
status.PendingApprovalCount += pending
}
}
if aggregateProvider, ok := h.findingsProvider.(AgentAggregateFindingsProvider); ok {
- if aggregateActive := aggregateProvider.ActiveFindingCount(); aggregateActive > status.ActiveFindingCount {
+ if aggregateActive := aggregateProvider.ActiveFindingCount(orgID); aggregateActive > status.ActiveFindingCount {
status.ActiveFindingCount = aggregateActive
}
}
diff --git a/internal/api/agent_resource_context_test.go b/internal/api/agent_resource_context_test.go
index 9f3e899b6a..1719393c45 100644
--- a/internal/api/agent_resource_context_test.go
+++ b/internal/api/agent_resource_context_test.go
@@ -24,14 +24,14 @@ type staticFindingsProvider struct {
activeCount *int
}
-func (s staticFindingsProvider) ActiveFindingsForResource(resourceID string) []AgentResourceFindingSnapshot {
+func (s staticFindingsProvider) ActiveFindingsForResource(resourceID, _ string) []AgentResourceFindingSnapshot {
if s.byResource == nil {
return nil
}
return s.byResource[resourceID]
}
-func (s staticFindingsProvider) ActiveFindingCount() int {
+func (s staticFindingsProvider) ActiveFindingCount(string) int {
if s.activeCount != nil {
return *s.activeCount
}
diff --git a/internal/api/ai_intelligence_handlers.go b/internal/api/ai_intelligence_handlers.go
index c2faafe8bb..43ee5fbf1a 100644
--- a/internal/api/ai_intelligence_handlers.go
+++ b/internal/api/ai_intelligence_handlers.go
@@ -720,7 +720,10 @@ func (h *AISettingsHandler) HandleGetAnomalies(w http.ResponseWriter, r *http.Re
// ReadState is the canonical resource read surface for AI intelligence endpoints.
// If this isn't wired, we can't reliably join baselines to live resources.
- rs := h.readState
+ // Resolve it for the request organization: the baselines above belong to
+ // that organization's Patrol, and another organization's resources must
+ // never be joined to them.
+ rs := h.readStateForOrg(GetOrgID(r.Context()))
if rs == nil {
if err := utils.WriteJSONResponse(w, map[string]interface{}{
"anomalies": []interface{}{},
diff --git a/internal/api/ai_intelligence_handlers_test.go b/internal/api/ai_intelligence_handlers_test.go
index 55e82dfbae..152ffc7143 100644
--- a/internal/api/ai_intelligence_handlers_test.go
+++ b/internal/api/ai_intelligence_handlers_test.go
@@ -500,3 +500,31 @@ func TestHandleGetBaselines_ResourceIDFilter(t *testing.T) {
t.Fatal("expected baselines field in response")
}
}
+
+// Anomalies join the request organization's baselines to that organization's
+// resources only, never the default organization's inventory.
+func TestRoutedAnomaliesUseRequestOrganizationResources(t *testing.T) {
+ rig := newTenantPatrolRig(t)
+ defaultNodeID := readStateNodeID(t, rig.defaultMonitor)
+ tenantNodeID := readStateNodeID(t, rig.tenantMonitor)
+
+ // The tenant's store holds a baseline for a default-org resource ID too,
+ // as a colliding source ID would; only its own node may be reported.
+ store := ai.NewBaselineStore(ai.BaselineConfig{MinSamples: 1})
+ addBaseline(t, store, tenantNodeID, "node", "cpu", 10)
+ addBaseline(t, store, defaultNodeID, "node", "cpu", 10)
+ rig.tenantService.SetBaselineStore(store)
+
+ var payload struct {
+ Anomalies []map[string]any `json:"anomalies"`
+ }
+ rig.get(t, "/api/ai/intelligence/anomalies", "org-a", &payload)
+ if len(payload.Anomalies) == 0 {
+ t.Fatalf("expected the tenant node's anomaly, got none")
+ }
+ for _, anomaly := range payload.Anomalies {
+ if anomaly["resource_id"] != tenantNodeID || anomaly["resource_name"] != "org-a-node" {
+ t.Fatalf("tenant anomalies leaked another organization's resource: %+v", payload.Anomalies)
+ }
+ }
+}
diff --git a/internal/api/ai_patrol_tenant_scope_test.go b/internal/api/ai_patrol_tenant_scope_test.go
new file mode 100644
index 0000000000..3828e37191
--- /dev/null
+++ b/internal/api/ai_patrol_tenant_scope_test.go
@@ -0,0 +1,269 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/agentcapabilities"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai"
+ "github.com/rcourtman/pulse-go-rewrite/internal/alerts"
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+ "github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
+ "github.com/rcourtman/pulse-go-rewrite/internal/notifications"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+)
+
+func monitorWithNode(t *testing.T, nodeID string, cpu float64) *monitoring.Monitor {
+ t.Helper()
+ monitor, _, _ := newTestMonitor(t)
+ adapter := unifiedresources.NewMonitorAdapter(nil)
+ adapter.PopulateFromSnapshot(models.StateSnapshot{Nodes: []models.Node{{ID: nodeID, Name: nodeID, Status: "online", CPU: cpu}}})
+ setUnexportedField(t, monitor, "resourceStore", monitoring.ResourceStoreInterface(adapter))
+ return monitor
+}
+
+func readStateNodeID(t *testing.T, monitor *monitoring.Monitor) string {
+ t.Helper()
+ nodes := monitor.GetUnifiedReadState().Nodes()
+ if len(nodes) != 1 {
+ t.Fatalf("expected one node, got %d", len(nodes))
+ }
+ return nodes[0].ID()
+}
+
+func patrolServiceWithFindings(t *testing.T, orgID string, resourceIDs ...string) *ai.Service {
+ t.Helper()
+ svc := ai.NewService(nil, nil)
+ svc.SetOrgID(orgID)
+ svc.SetStateProvider(&MockStateProvider{})
+ setUnexportedField(t, svc, "cfg", &config.AIConfig{Enabled: true})
+ setUnexportedField(t, svc, "provider", &stubProvider{})
+ for _, resourceID := range resourceIDs {
+ svc.GetPatrolService().GetFindings().Add(&ai.Finding{
+ ID: orgID + "-" + resourceID,
+ ResourceID: resourceID,
+ Title: orgID + " finding",
+ Severity: ai.FindingSeverityWarning,
+ DetectedAt: time.Now(),
+ LastSeenAt: time.Now(),
+ })
+ }
+ return svc
+}
+
+type tenantPatrolRig struct {
+ router *Router
+ server *httptest.Server
+ token string
+ defaultMonitor *monitoring.Monitor
+ tenantMonitor *monitoring.Monitor
+ defaultService *ai.Service
+ tenantService *ai.Service
+}
+
+// newTenantPatrolRig serves a real router for two organizations, each with
+// its own monitor and Patrol, behind the real auth and tenant middleware.
+func newTenantPatrolRig(t *testing.T) tenantPatrolRig {
+ t.Helper()
+ SetMultiTenantEnabled(true)
+ t.Cleanup(func() { SetMultiTenantEnabled(false) })
+ t.Setenv("PULSE_DEV", "true")
+
+ const rawToken = "patrol-tenant-scope-token-123.12345678"
+ record, err := config.NewAPITokenRecord(rawToken, "two-org-token", []string{config.ScopeMonitoringRead, config.ScopeAIExecute})
+ if err != nil {
+ t.Fatalf("create api token record: %v", err)
+ }
+ record.OrgIDs = []string{"default", "org-a"}
+
+ dataDir := t.TempDir()
+ cfg := &config.Config{DataPath: dataDir, ConfigPath: dataDir, APITokens: []config.APITokenRecord{*record}}
+ cfg.SortAPITokens()
+ if err := config.NewMultiTenantPersistence(dataDir).SaveOrganization(&models.Organization{ID: "org-a", DisplayName: "ORG-A"}); err != nil {
+ t.Fatalf("save organization: %v", err)
+ }
+
+ router := newMultiTenantRouter(t, cfg)
+ defaultMonitor := monitorWithNode(t, "default-node", 0.9)
+ tenantMonitor := monitorWithNode(t, "org-a-node", 0.9)
+ router.monitor = defaultMonitor
+ mtm := &monitoring.MultiTenantMonitor{}
+ setUnexportedField(t, mtm, "monitors", map[string]*monitoring.Monitor{"default": defaultMonitor, "org-a": tenantMonitor})
+ router.mtMonitor = mtm
+ router.aiSettingsHandler.SetMultiTenantMonitor(mtm)
+ router.aiSettingsHandler.SetReadState(defaultMonitor.GetUnifiedReadState())
+ router.resourceHandlers.SetTenantStateProvider(NewMultiTenantStateProvider(mtm, defaultMonitor))
+
+ defaultService := patrolServiceWithFindings(t, "default", "default-1", "default-2")
+ tenantService := patrolServiceWithFindings(t, "org-a", "org-a-1")
+ router.aiSettingsHandler.stateMu.Lock()
+ router.aiSettingsHandler.defaultAIService = defaultService
+ router.aiSettingsHandler.stateMu.Unlock()
+ router.aiSettingsHandler.aiServicesMu.Lock()
+ router.aiSettingsHandler.aiServices["org-a"] = tenantService
+ router.aiSettingsHandler.aiServicesMu.Unlock()
+
+ server := httptest.NewServer(router.Handler())
+ t.Cleanup(server.Close)
+ return tenantPatrolRig{
+ router: router,
+ server: server,
+ token: rawToken,
+ defaultMonitor: defaultMonitor,
+ tenantMonitor: tenantMonitor,
+ defaultService: defaultService,
+ tenantService: tenantService,
+ }
+}
+
+func (rig tenantPatrolRig) get(t *testing.T, path, orgID string, out any) {
+ t.Helper()
+ req, err := http.NewRequest(http.MethodGet, rig.server.URL+path, nil)
+ if err != nil {
+ t.Fatalf("create request: %v", err)
+ }
+ req.Header.Set("X-Pulse-Org-ID", orgID)
+ req.Header.Set("Authorization", "Bearer "+rig.token)
+ res, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatalf("org %s %s: request failed: %v", orgID, path, err)
+ }
+ defer res.Body.Close()
+ if res.StatusCode != http.StatusOK {
+ t.Fatalf("org %s %s: status %d", orgID, path, res.StatusCode)
+ }
+ if err := json.NewDecoder(res.Body).Decode(out); err != nil {
+ t.Fatalf("org %s %s: decode: %v", orgID, path, err)
+ }
+}
+
+// Agent Patrol-control status counts only the request organization's
+// findings, whichever organization's Patrol was wired last.
+func TestRoutedAgentPatrolStatusCountsRequestOrganizationFindings(t *testing.T) {
+ rig := newTenantPatrolRig(t)
+
+ for orgID, want := range map[string]int{"default": 2, "org-a": 1} {
+ var status AgentOperationsLoopStatus
+ rig.get(t, agentcapabilities.PatrolControlStatusCapabilityPath, orgID, &status)
+ if status.ActiveFindingCount != want {
+ t.Fatalf("org %s: expected %d active findings, got %d", orgID, want, status.ActiveFindingCount)
+ }
+ }
+}
+
+func TestPatrolFindingNotificationManagerNeverFallsBackToDefault(t *testing.T) {
+ defaultMonitor, _, _ := newTestMonitor(t)
+ tenantMonitor, _, _ := newTestMonitor(t)
+ defaultManager := notifications.NewNotificationManagerWithDataDir("", t.TempDir())
+ tenantManager := notifications.NewNotificationManagerWithDataDir("", t.TempDir())
+ t.Cleanup(defaultManager.Stop)
+ t.Cleanup(tenantManager.Stop)
+ setUnexportedField(t, defaultMonitor, "notificationMgr", defaultManager)
+ setUnexportedField(t, tenantMonitor, "notificationMgr", tenantManager)
+ mtm := &monitoring.MultiTenantMonitor{}
+ setUnexportedField(t, mtm, "monitors", map[string]*monitoring.Monitor{"default": defaultMonitor, "org-a": tenantMonitor})
+ router := &Router{monitor: defaultMonitor, mtMonitor: mtm}
+
+ if got := router.patrolFindingNotificationManager("org-a"); got != tenantManager {
+ t.Fatalf("expected org-a findings to use org-a's notification manager")
+ }
+ if got := router.patrolFindingNotificationManager("default"); got != defaultManager {
+ t.Fatalf("expected default findings to use the default notification manager")
+ }
+ if got := (&Router{monitor: defaultMonitor}).patrolFindingNotificationManager("org-a"); got != nil {
+ t.Fatalf("expected no notification manager for an unresolved organization")
+ }
+}
+
+func TestLearnBaselinesUsesOrganizationInventory(t *testing.T) {
+ defaultMonitor, defaultState, defaultHistory := newTestMonitor(t)
+ tenantMonitor, tenantState, tenantHistory := newTestMonitor(t)
+ defaultState.Nodes = []models.Node{{ID: "default-node", Name: "default-node"}}
+ tenantState.Nodes = []models.Node{{ID: "org-a-node", Name: "org-a-node"}}
+ syncTestResourceStore(t, defaultMonitor, defaultState)
+ syncTestResourceStore(t, tenantMonitor, tenantState)
+ now := time.Now()
+ // The tenant's history also holds the default node's ID, and only the
+ // tenant's history holds its own node: learning from the default inventory
+ // or from the default history both show up below.
+ defaultHistory.AddNodeMetric("default-node", "cpu", 0.5, now)
+ tenantHistory.AddNodeMetric("default-node", "cpu", 0.5, now)
+ tenantHistory.AddNodeMetric("org-a-node", "cpu", 0.5, now)
+ mtm := &monitoring.MultiTenantMonitor{}
+ setUnexportedField(t, mtm, "monitors", map[string]*monitoring.Monitor{"default": defaultMonitor, "org-a": tenantMonitor})
+ router := &Router{monitor: defaultMonitor, mtMonitor: mtm}
+
+ store := ai.NewBaselineStore(ai.BaselineConfig{MinSamples: 1})
+ router.learnBaselines(store, router.resolveMonitorForOrg("org-a"))
+ if _, ok := store.GetBaseline("org-a-node", "cpu"); !ok {
+ t.Fatalf("expected org-a to learn its own node")
+ }
+ if _, ok := store.GetBaseline("default-node", "cpu"); ok {
+ t.Fatalf("org-a learned a default-organization resource")
+ }
+}
+
+func TestDeployCandidatesReadRequestOrganizationMonitor(t *testing.T) {
+ defaultMonitor := monitorWithNode(t, "default-node", 0.1)
+ handler := &DeployHandlers{monitor: defaultMonitor}
+ handler.SetMonitorResolver(func(ctx context.Context) *monitoring.Monitor {
+ if GetOrgID(ctx) == "default" {
+ return defaultMonitor
+ }
+ return nil
+ })
+
+ rr := httptest.NewRecorder()
+ handler.HandleCandidates(rr, withOrg(httptest.NewRequest(http.MethodGet, "/api/clusters/c1/agent-deploy/candidates", nil), "org-a"))
+ if rr.Code != http.StatusServiceUnavailable {
+ t.Fatalf("expected an unresolved organization not to read the default cluster, got %d", rr.Code)
+ }
+}
+
+// A tenant's Patrol notifies through its own channels: it gets the finding
+// notification callback but never the instance relay's mobile push.
+func TestStartPatrolForContextKeepsTenantFindingsOffInstanceRelay(t *testing.T) {
+ setMockModeForTest(t, true)
+
+ mtp := config.NewMultiTenantPersistence(t.TempDir())
+ defaultMonitor, _, _ := newTestMonitor(t)
+ tenantMonitor, _, _ := newTestMonitor(t)
+ tenantMonitor.SetResourceStore(unifiedresources.NewMonitorAdapter(unifiedresources.NewRegistry(nil)))
+ // Finding callbacks are wired only once the unified store exists, which
+ // needs the organization's alert manager.
+ alertManager := alerts.NewManagerWithDataDir(t.TempDir())
+ t.Cleanup(alertManager.Stop)
+ setUnexportedField(t, tenantMonitor, "alertManager", alertManager)
+ mtm := &monitoring.MultiTenantMonitor{}
+ setUnexportedField(t, mtm, "monitors", map[string]*monitoring.Monitor{"default": defaultMonitor, "tenant-1": tenantMonitor})
+
+ router := &Router{
+ monitor: defaultMonitor,
+ mtMonitor: mtm,
+ multiTenant: mtp,
+ aiSettingsHandler: NewAISettingsHandler(mtp, mtm, nil),
+ aiHandler: &AIHandler{},
+ startedPatrolOrgs: make(map[string]bool),
+ monitorResourceAdapters: make(map[string]*unifiedresources.MonitorAdapter),
+ }
+ router.aiSettingsHandler.SetStateProvider(&stubStateProvider{})
+ defer router.ShutdownAIIntelligence()
+
+ ctx := context.WithValue(context.Background(), OrgIDContextKey, "tenant-1")
+ if ok := router.startPatrolForContext(ctx, "tenant-1"); !ok {
+ t.Fatal("expected tenant-1 patrol start to succeed")
+ }
+ patrol := reflect.ValueOf(router.aiSettingsHandler.GetAIService(ctx).GetPatrolService()).Elem()
+ if patrol.FieldByName("findingNotifyCallback").IsNil() {
+ t.Fatal("expected tenant-1 findings to be wired to notifications")
+ }
+ if !patrol.FieldByName("pushNotifyCallback").IsNil() {
+ t.Fatal("tenant-1 findings must not be pushed through the instance relay")
+ }
+}
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 35a91325bb..6312ad05b6 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -19245,8 +19245,8 @@ func TestContract_AgentEventsStreamPublishesOnFindingCreated(t *testing.T) {
t.Fatalf("read router.go: %v", err)
}
src := string(source)
- if !strings.Contains(src, "PublishFindingCreated(AgentEventFindingCreatedPayload{") {
- t.Error("router.go must publish finding.created events to the agent stream when a new finding is added")
+ if !strings.Contains(src, "PublishFindingCreated(orgID, AgentEventFindingCreatedPayload{") {
+ t.Error("router.go must publish finding.created events, scoped to the Patrol's organization, to the agent stream when a new finding is added")
}
if !strings.Contains(src, "isNew && r.agentEventBroadcaster != nil && f.DismissedReason == \"\"") {
t.Error("publish gate must fire only when isNew, broadcaster wired, and finding not auto-dismissed by operator-state — otherwise the stream contradicts the suppression contract")
@@ -23097,9 +23097,9 @@ func TestContract_AgentOperationsLoopStatusEndpointSurfacesStableShape(t *testin
"return telemetry.ClassifyPulseIntelligencePatrolControlProof(telemetry.PulseIntelligencePatrolControlProofInput{",
"ContextualCollaborationCount: agentOperationsLoopContextualCollaborationCount(status)",
"type AgentAggregateFindingsProvider interface {",
- "ActiveFindingCount() int",
+ "ActiveFindingCount(orgID string) int",
"if aggregateProvider, ok := h.findingsProvider.(AgentAggregateFindingsProvider); ok {",
- "aggregateProvider.ActiveFindingCount()",
+ "aggregateProvider.ActiveFindingCount(orgID)",
"case status.PendingApprovalCount > 0:",
"Review pending Patrol approvals before treating previous verified work as current.",
"case status.ActiveFindingCount > 0 && hasVerifiedOutcome:",
@@ -28754,7 +28754,8 @@ func TestContract_PulseMCPReadmeIntroNamesPublishedReadTools(t *testing.T) {
}
// Change events reach only subscribers in the organization whose monitor
-// recorded them; untagged events still reach everyone.
+// recorded them, and finding events only those in the organization whose
+// Patrol raised them.
func TestContract_AgentChangeEventsStayInsideTheirOrganization(t *testing.T) {
b := NewAgentEventBroadcaster()
producer, _ := newTestAgentChangeEventProducer(b)
@@ -28782,7 +28783,9 @@ func TestContract_AgentChangeEventsStayInsideTheirOrganization(t *testing.T) {
producer.observer("tenant-b", nil)(unifiedresources.ResourceChange{ID: "foreign", ResourceID: "vm-b", Kind: unifiedresources.ChangeAlertFired})
producer.observer("", nil)(unifiedresources.ResourceChange{ID: "default-org", ResourceID: "vm-d", Kind: unifiedresources.ChangeAlertFired})
producer.observer("tenant-a", nil)(unifiedresources.ResourceChange{ID: "own", ResourceID: "vm-a", Kind: unifiedresources.ChangeAlertFired})
- b.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "untagged-finding"})
+ b.PublishFindingCreated("tenant-b", AgentEventFindingCreatedPayload{FindingID: "foreign-finding"})
+ b.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "default-finding"})
+ b.PublishFindingCreated("tenant-a", AgentEventFindingCreatedPayload{FindingID: "own-finding"})
got := make(chan string, 1)
go func() {
@@ -28791,7 +28794,7 @@ func TestContract_AgentChangeEventsStayInsideTheirOrganization(t *testing.T) {
for scanner.Scan() {
collected.WriteString(scanner.Text())
collected.WriteString("\n")
- if strings.Contains(collected.String(), "untagged-finding") {
+ if strings.Contains(collected.String(), "own-finding") {
got <- collected.String()
return
}
@@ -28802,7 +28805,7 @@ func TestContract_AgentChangeEventsStayInsideTheirOrganization(t *testing.T) {
if !strings.Contains(stream, "event: alert.fired") || !strings.Contains(stream, `"changeId":"own"`) {
t.Fatalf("own organization's alert missing from stream:\n%s", stream)
}
- for _, foreign := range []string{`"changeId":"foreign"`, `"changeId":"default-org"`} {
+ for _, foreign := range []string{`"changeId":"foreign"`, `"changeId":"default-org"`, "foreign-finding", "default-finding"} {
if strings.Contains(stream, foreign) {
t.Fatalf("stream leaked %s:\n%s", foreign, stream)
}
diff --git a/internal/api/deploy_handlers.go b/internal/api/deploy_handlers.go
index d624661b18..b32b91140e 100644
--- a/internal/api/deploy_handlers.go
+++ b/internal/api/deploy_handlers.go
@@ -28,6 +28,10 @@ type DeployHandlers struct {
store *deploy.Store
monitor *monitoring.Monitor
execServer *agentexec.Server
+ // tenantMonitor resolves the request organization's monitor. The router
+ // sets it so candidates and preflights never read another organization's
+ // cluster nodes; without it the fixed monitor serves every request.
+ tenantMonitor func(context.Context) *monitoring.Monitor
// resolvePublicURL derives the Pulse URL for agent reachability checks.
resolvePublicURL func(req *http.Request) string
@@ -69,6 +73,19 @@ func NewDeployHandlers(
}
}
+// SetMonitorResolver makes candidates and preflights read the cluster nodes of
+// each request's organization.
+func (h *DeployHandlers) SetMonitorResolver(resolve func(context.Context) *monitoring.Monitor) {
+ h.tenantMonitor = resolve
+}
+
+func (h *DeployHandlers) monitorFor(r *http.Request) *monitoring.Monitor {
+ if h.tenantMonitor != nil {
+ return h.tenantMonitor(r.Context())
+ }
+ return h.monitor
+}
+
// --- Candidates ---
// candidateNode is the per-node response in the candidates list.
@@ -109,7 +126,7 @@ func (h *DeployHandlers) HandleCandidates(w http.ResponseWriter, r *http.Request
return
}
- readState := h.monitor.GetUnifiedReadStateOrSnapshot()
+ readState := h.monitorFor(r).GetUnifiedReadStateOrSnapshot()
if readState == nil {
writeErrorResponse(w, http.StatusServiceUnavailable, "state_unavailable", "Resource state is unavailable", nil)
return
@@ -249,7 +266,7 @@ func (h *DeployHandlers) HandleCreatePreflight(w http.ResponseWriter, r *http.Re
}
// Resolve cluster nodes from read-state.
- readState := h.monitor.GetUnifiedReadStateOrSnapshot()
+ readState := h.monitorFor(r).GetUnifiedReadStateOrSnapshot()
if readState == nil {
writeErrorResponse(w, http.StatusServiceUnavailable, "state_unavailable", "Resource state is unavailable", nil)
return
diff --git a/internal/api/route_inventory_test.go b/internal/api/route_inventory_test.go
index 9445efcd81..c437ad998a 100644
--- a/internal/api/route_inventory_test.go
+++ b/internal/api/route_inventory_test.go
@@ -963,7 +963,7 @@ func TestAgentEventTokenLifecycleDeleteThroughHTTP(t *testing.T) {
if err != nil || len(persisted) != 1 || persisted[0].ID == id {
t.Fatalf("revocation was not persisted: %v, %d tokens", err, len(persisted))
}
- router.agentEventBroadcaster.PublishFindingCreated(AgentEventFindingCreatedPayload{FindingID: "after-durable-revoke"})
+ router.agentEventBroadcaster.PublishFindingCreated("", AgentEventFindingCreatedPayload{FindingID: "after-durable-revoke"})
primary.wantClosed(t)
req, _ = http.NewRequest(http.MethodGet, server.URL+"/api/agent/events", nil)
diff --git a/internal/api/router.go b/internal/api/router.go
index b16874978a..2052b39325 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -49,6 +49,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/mock"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
+ "github.com/rcourtman/pulse-go-rewrite/internal/notifications"
"github.com/rcourtman/pulse-go-rewrite/internal/relay"
"github.com/rcourtman/pulse-go-rewrite/internal/telemetry"
"github.com/rcourtman/pulse-go-rewrite/internal/truenas"
@@ -569,6 +570,13 @@ func (r *Router) setupRoutes() {
// cross-tenant pending requests don't leak into an agent context
// read for the wrong org.
r.agentContextHandler.SetApprovalsProvider(agentApprovalStoreProvider{})
+ // Wire active findings the same way: each read resolves the request
+ // organization's Patrol, so one organization never sees another's
+ // findings or counts.
+ r.agentContextHandler.SetFindingsProvider(agentFindingsProvider{
+ activeForResource: r.agentActiveFindingsForResource,
+ activeCount: r.agentActiveFindingCount,
+ })
r.agentEventBroadcaster = NewAgentEventBroadcaster()
r.agentChangeEvents = newAgentChangeEventProducer(r.agentEventBroadcaster, defaultResourceStatusEventWindow)
if r.resourceHandlers != nil && r.agentEventBroadcaster != nil {
@@ -711,6 +719,9 @@ func (r *Router) setupRoutes() {
r.deployStore = deployStore
if r.monitor != nil {
r.deployHandlers = NewDeployHandlers(deployStore, r.monitor, r.agentExecServer, r.resolvePublicURL, r.config, r.persistence)
+ r.deployHandlers.SetMonitorResolver(func(ctx context.Context) *monitoring.Monitor {
+ return r.resolveMonitorForOrg(GetOrgID(ctx))
+ })
}
}
@@ -2029,7 +2040,9 @@ func (r *Router) patrolCtx(ctx context.Context, orgID string) context.Context {
if ctx == nil {
return context.WithValue(context.Background(), OrgIDContextKey, orgID)
}
- if existing := strings.TrimSpace(GetOrgID(ctx)); existing != "" {
+ // GetOrgID reports "default" for an untagged context, so check for the tag
+ // itself: an untagged context must take orgID, not the default organization.
+ if existing, ok := ctx.Value(OrgIDContextKey).(string); ok && strings.TrimSpace(existing) != "" {
return ctx
}
return context.WithValue(ctx, OrgIDContextKey, orgID)
@@ -2273,7 +2286,7 @@ func (r *Router) startPatrolForContext(ctx context.Context, orgID string) bool {
aiService.SetBaselineStore(baselineStore)
// Start background baseline learning loop
- go r.startBaselineLearning(ctx, baselineStore, metricsHistory)
+ go r.startBaselineLearning(ctx, orgID, baselineStore)
}
}
}
@@ -2388,7 +2401,7 @@ func (r *Router) startPatrolForContext(ctx context.Context, orgID string) bool {
// quiet about this resource. Fire-and-forget — slow
// subscribers don't block the patrol loop.
if isNew && r.agentEventBroadcaster != nil && f.DismissedReason == "" {
- r.agentEventBroadcaster.PublishFindingCreated(AgentEventFindingCreatedPayload{
+ r.agentEventBroadcaster.PublishFindingCreated(orgID, AgentEventFindingCreatedPayload{
FindingID: f.ID,
ResourceID: f.ResourceID,
ResourceName: f.ResourceName,
@@ -2402,66 +2415,6 @@ func (r *Router) startPatrolForContext(ctx context.Context, orgID string) bool {
patrol.SetUnifiedFindingResolver(func(findingID string) {
unifiedStore.Resolve(findingID)
})
- // Wire the agent-consumable findings adapter so the bundled
- // context endpoint can return active findings without the
- // api package importing internal/ai. The closure captures
- // the patrol service and projects each Finding into the
- // agent-stable snapshot shape; agents see the situated
- // picture without any internal type leakage.
- if r.agentContextHandler != nil {
- r.agentContextHandler.SetFindingsProvider(
- agentFindingsProvider{
- activeForResource: func(resourceID string) []AgentResourceFindingSnapshot {
- if patrol == nil {
- return nil
- }
- findings := patrol.GetFindings()
- if findings == nil {
- return nil
- }
- active := findings.GetByResource(resourceID)
- if len(active) == 0 {
- return []AgentResourceFindingSnapshot{}
- }
- out := make([]AgentResourceFindingSnapshot, 0, len(active))
- for _, f := range active {
- snapshot := AgentResourceFindingSnapshot{
- ID: f.ID,
- Title: f.Title,
- Severity: string(f.Severity),
- Category: string(f.Category),
- Description: f.Description,
- Impact: f.Impact,
- Recommendation: f.Recommendation,
- RegressionCount: f.RegressionCount,
- PreviousResolvedFixSummary: f.PreviousResolvedFixSummary,
- }
- if !f.DetectedAt.IsZero() {
- snapshot.DetectedAt = f.DetectedAt.Format(time.RFC3339)
- }
- if !f.LastSeenAt.IsZero() {
- snapshot.LastSeenAt = f.LastSeenAt.Format(time.RFC3339)
- }
- if f.InvestigationRecord != nil && f.InvestigationRecord.Confidence != "" {
- snapshot.Confidence = string(f.InvestigationRecord.Confidence)
- }
- out = append(out, snapshot)
- }
- return out
- },
- activeCount: func() int {
- if patrol == nil {
- return 0
- }
- findings := patrol.GetFindings()
- if findings == nil {
- return 0
- }
- return len(findings.GetActive(ai.FindingSeverityWarning))
- },
- },
- )
- }
// Wire per-resource operator-state into the findings runtime so
// new findings raised against a resource the operator has
// flagged (maintenance window or intentionally offline) get
@@ -2472,17 +2425,21 @@ func (r *Router) startPatrolForContext(ctx context.Context, orgID string) bool {
// `internal/unifiedresources` import.
patrol.GetFindings().SetResourceOperatorStateProvider(r.patrolResourceOperatorStateProvider(orgID))
- // Wire push notifications: patrol findings → relay client (best-effort)
- patrol.SetPushNotifyCallback(func(n relay.PushNotificationPayload) {
- r.relayMu.RLock()
- client := r.relayClient
- r.relayMu.RUnlock()
- if client != nil {
- if err := client.SendPushNotification(n); err != nil {
- log.Debug().Err(err).Str("type", n.Type).Msg("Push notification send failed")
+ // Wire push notifications: patrol findings → relay client (best-effort).
+ // Relay pairs mobile devices with the instance and, like alert
+ // pushes, carries only the default organization's events.
+ if orgID == "default" {
+ patrol.SetPushNotifyCallback(func(n relay.PushNotificationPayload) {
+ r.relayMu.RLock()
+ client := r.relayClient
+ r.relayMu.RUnlock()
+ if client != nil {
+ if err := client.SendPushNotification(n); err != nil {
+ log.Debug().Err(err).Str("type", n.Type).Msg("Push notification send failed")
+ }
}
- }
- })
+ })
+ }
// Wire finding notifications: new warning+ patrol findings go to
// the operator's alert notification channels (email, webhooks,
@@ -2496,10 +2453,7 @@ func (r *Router) startPatrolForContext(ctx context.Context, orgID string) bool {
if cfg == nil || !cfg.PatrolFindingTriggersNotification(string(f.Severity)) {
return
}
- if r.monitor == nil {
- return
- }
- if nm := r.monitor.GetNotificationManager(); nm != nil {
+ if nm := r.patrolFindingNotificationManager(orgID); nm != nil {
nm.SendAlert(patrolFindingNotificationAlert(f))
}
})
@@ -2743,7 +2697,7 @@ func (r *Router) initializeAIIntelligenceServices(ctx context.Context, orgID, da
// Wire guest prober for pre-patrol reachability checks via Unified Agents
if r.agentExecServer != nil {
- patrol.SetGuestProber(ai.NewAgentExecProber(r.agentExecServer))
+ patrol.SetGuestProber(ai.NewAgentExecProber(r.agentExecServer, orgID))
}
// NOTE: Unified finding callback is wired in StartPatrol after findings persistence is loaded
@@ -3849,10 +3803,89 @@ func (r *Router) handleGetRelayStatus(w http.ResponseWriter, req *http.Request)
json.NewEncoder(w).Encode(client.Status())
}
+// patrolFindingNotificationManager returns the notification manager that
+// delivers orgID's Patrol findings: that organization's own, never the default
+// organization's when orgID's monitor cannot be resolved.
+func (r *Router) patrolFindingNotificationManager(orgID string) *notifications.NotificationManager {
+ monitor := r.resolveMonitorForOrg(orgID)
+ if monitor == nil {
+ return nil
+ }
+ return monitor.GetNotificationManager()
+}
+
+// patrolFindingsForOrg returns orgID's Patrol findings store, or nil when that
+// organization has no AI service yet. It never constructs a service and never
+// falls back to the default organization's.
+func (r *Router) patrolFindingsForOrg(orgID string) *ai.FindingsStore {
+ if r == nil || r.aiSettingsHandler == nil {
+ return nil
+ }
+ service := r.aiSettingsHandler.ExistingAIService(r.patrolCtx(context.Background(), orgID))
+ if service == nil {
+ return nil
+ }
+ patrol := service.GetPatrolService()
+ if patrol == nil {
+ return nil
+ }
+ return patrol.GetFindings()
+}
+
+// agentActiveFindingsForResource projects orgID's active findings for one
+// resource into the agent-stable snapshot shape, so the bundled context
+// endpoint can return them without the api package exposing internal/ai types.
+func (r *Router) agentActiveFindingsForResource(resourceID, orgID string) []AgentResourceFindingSnapshot {
+ findings := r.patrolFindingsForOrg(orgID)
+ if findings == nil {
+ return nil
+ }
+ active := findings.GetByResource(resourceID)
+ if len(active) == 0 {
+ return []AgentResourceFindingSnapshot{}
+ }
+ out := make([]AgentResourceFindingSnapshot, 0, len(active))
+ for _, f := range active {
+ snapshot := AgentResourceFindingSnapshot{
+ ID: f.ID,
+ Title: f.Title,
+ Severity: string(f.Severity),
+ Category: string(f.Category),
+ Description: f.Description,
+ Impact: f.Impact,
+ Recommendation: f.Recommendation,
+ RegressionCount: f.RegressionCount,
+ PreviousResolvedFixSummary: f.PreviousResolvedFixSummary,
+ }
+ if !f.DetectedAt.IsZero() {
+ snapshot.DetectedAt = f.DetectedAt.Format(time.RFC3339)
+ }
+ if !f.LastSeenAt.IsZero() {
+ snapshot.LastSeenAt = f.LastSeenAt.Format(time.RFC3339)
+ }
+ if f.InvestigationRecord != nil && f.InvestigationRecord.Confidence != "" {
+ snapshot.Confidence = string(f.InvestigationRecord.Confidence)
+ }
+ out = append(out, snapshot)
+ }
+ return out
+}
+
+// agentActiveFindingCount counts orgID's active warning-or-worse findings.
+func (r *Router) agentActiveFindingCount(orgID string) int {
+ findings := r.patrolFindingsForOrg(orgID)
+ if findings == nil {
+ return 0
+ }
+ return len(findings.GetActive(ai.FindingSeverityWarning))
+}
+
// startBaselineLearning runs a background loop that learns baselines from metrics history
-// This enables anomaly detection by understanding what "normal" looks like for each resource
-func (r *Router) startBaselineLearning(ctx context.Context, store *ai.BaselineStore, metricsHistory *monitoring.MetricsHistory) {
- if store == nil || metricsHistory == nil {
+// This enables anomaly detection by understanding what "normal" looks like for each resource.
+// Each pass resolves orgID's monitor, so an organization learns only from its
+// own inventory and metrics history, never the default organization's.
+func (r *Router) startBaselineLearning(ctx context.Context, orgID string, store *ai.BaselineStore) {
+ if store == nil {
return
}
@@ -3868,7 +3901,7 @@ func (r *Router) startBaselineLearning(ctx context.Context, store *ai.BaselineSt
case <-ctx.Done():
return
case <-initialDelay.C:
- r.learnBaselines(store, metricsHistory)
+ r.learnBaselines(store, r.resolveMonitorForOrg(orgID))
}
log.Info().Msg("Baseline learning loop started")
@@ -3883,21 +3916,22 @@ func (r *Router) startBaselineLearning(ctx context.Context, store *ai.BaselineSt
log.Info().Msg("Baseline learning loop stopped")
return
case <-ticker.C:
- r.learnBaselines(store, metricsHistory)
+ r.learnBaselines(store, r.resolveMonitorForOrg(orgID))
}
}
}
-// learnBaselines updates baselines for all resources from metrics history.
-// Uses ReadState typed views — the legacy GetState fallback was removed
-// after ReadState became the sole state access mechanism (SRC-03e+).
-func (r *Router) learnBaselines(store *ai.BaselineStore, metricsHistory *monitoring.MetricsHistory) {
- if r.monitor == nil {
+// learnBaselines updates baselines for all of monitor's resources from its
+// metrics history. Uses ReadState typed views — the legacy GetState fallback
+// was removed after ReadState became the sole state access mechanism (SRC-03e+).
+func (r *Router) learnBaselines(store *ai.BaselineStore, monitor *monitoring.Monitor) {
+ if store == nil || monitor == nil {
return
}
- readState := r.monitor.GetUnifiedReadState()
- if readState == nil {
+ readState := monitor.GetUnifiedReadState()
+ metricsHistory := monitor.GetMetricsHistory()
+ if readState == nil || metricsHistory == nil {
return
}
diff --git a/internal/api/router_misc_additional_test.go b/internal/api/router_misc_additional_test.go
index 2428bd5559..adaa2843ac 100644
--- a/internal/api/router_misc_additional_test.go
+++ b/internal/api/router_misc_additional_test.go
@@ -455,7 +455,7 @@ func TestLearnBaselines_WithData(t *testing.T) {
router := &Router{monitor: monitor}
store := ai.NewBaselineStore(ai.BaselineConfig{MinSamples: 1})
- router.learnBaselines(store, history)
+ router.learnBaselines(store, monitor)
if store.ResourceCount() == 0 {
t.Fatalf("expected baselines to be learned")