Files
Pulse/internal/api/telemetry_pulse_intelligence_test.go
rcourtman c5b6d05107 feat(telemetry): add schema v26 map topology API/CLI/MCP telemetry
I added Schema v26 content-free Infrastructure Map topology read telemetry for external live GET /api/agent/topology requests (total read days, CLI days, MCP days, anonymized days, and an explicit availability flag) alongside hardened opt-out zero-write boundaries, credential-binding checks, and read-only SQLite/config guards.

Key changes:
- Added a dedicated root-scoped daily tally store (infrastructure_map_topology_tally.json) with strict schema validation, fail-closed degraded sidecar accounting, and strict zero-write guarantees when telemetry is disabled by settings or PULSE_TELEMETRY=false.
- Added X-Pulse-Agent-Surface attribution for pulse-cli and MCP clients while filtering out first-party browser traffic, mock-mode snapshots, and unverified monitor states.
- Updated Settings payload preview, localization copy, shipped PRIVACY.md disclosure, integration specs, and schema parity tooling for Schema v26.
- Hardened host/agent token hash binding, self-uninstall live authority and durable hash-scoped tombstones, Patrol investigation replay-floor provenance, bounded non-blocking regular-file config reads, and cold read-only legacy SQLite history queries.
2026-12-02 01:41:25 +00:00

731 lines
33 KiB
Go

package api
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/actionlifecycle"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/telemetry"
unifiedresources "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
func TestGetPulseIntelligenceActionTelemetry_SummarizesActionGovernanceOnly(t *testing.T) {
now := time.Date(2026, 6, 20, 9, 30, 0, 0, time.UTC)
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
mtPersistence := config.NewMultiTenantPersistence(dataDir)
if err := mtPersistence.SaveOrganization(&models.Organization{ID: "tenant-a", DisplayName: "Tenant A"}); err != nil {
t.Fatalf("SaveOrganization: %v", err)
}
router := &Router{
multiTenant: mtPersistence,
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
defaultStore, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("default getStore: %v", err)
}
tenantStore, err := router.resourceHandlers.getStore("tenant-a")
if err != nil {
t.Fatalf("tenant getStore: %v", err)
}
approvedCompleted := pulseTelemetryActionRecord("approved-completed", now.Add(-35*time.Minute), unifiedresources.ActionStateCompleted, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodUI, Timestamp: now.Add(-34 * time.Minute), Actor: "operator"},
})
approvedCompleted.Result = &unifiedresources.ExecutionResult{Success: true}
approvedCompleted.VerificationOutcome = unifiedresources.VerificationOutcome{Status: unifiedresources.VerificationVerified}
records := map[unifiedresources.ResourceStore][]unifiedresources.ActionAuditRecord{
defaultStore: {
pulseTelemetryActionRecord("plan-only", now.Add(-time.Hour), unifiedresources.ActionStatePlanned, false, nil),
pulseTelemetryActionRecord("pending", now.Add(-50*time.Minute), unifiedresources.ActionStatePending, true, nil),
pulseTelemetryActionRecord("approved-executing", now.Add(-40*time.Minute), unifiedresources.ActionStateExecuting, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodUI, Timestamp: now.Add(-39 * time.Minute), Actor: "operator"},
}),
approvedCompleted,
pulseTelemetryActionRecord("old-approved", since.Add(-time.Minute), unifiedresources.ActionStateCompleted, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodUI, Timestamp: since.Add(-2 * time.Minute), Actor: "operator"},
}),
},
tenantStore: {
pulseTelemetryActionRecord("tenant-approved-failed", now.Add(-30*time.Minute), unifiedresources.ActionStateFailed, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodAPI, Timestamp: now.Add(-29 * time.Minute), Actor: "operator"},
}),
pulseTelemetryActionRecord("tenant-rejected", now.Add(-20*time.Minute), unifiedresources.ActionStateRejected, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeRejected, Method: unifiedresources.MethodUI, Timestamp: now.Add(-19 * time.Minute), Actor: "operator"},
}),
},
}
for store, storeRecords := range records {
for _, record := range storeRecords {
if err := store.RecordActionAudit(record); err != nil {
t.Fatalf("RecordActionAudit(%s): %v", record.ID, err)
}
}
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ActionPlans30d != 6 {
t.Fatalf("ActionPlans30d = %d, want 6", got.ActionPlans30d)
}
if got.ApprovalRequests30d != 5 {
t.Fatalf("ApprovalRequests30d = %d, want 5", got.ApprovalRequests30d)
}
if got.RejectedActionDecisions30d != 1 {
t.Fatalf("RejectedActionDecisions30d = %d, want 1", got.RejectedActionDecisions30d)
}
if got.ApprovedActionDecisions30d != 3 {
t.Fatalf("ApprovedActionDecisions30d = %d, want 3", got.ApprovedActionDecisions30d)
}
if got.ApprovedActionAttempts30d != 3 {
t.Fatalf("ApprovedActionAttempts30d = %d, want 3", got.ApprovedActionAttempts30d)
}
if got.ApprovedActionSuccesses30d != 1 {
t.Fatalf("ApprovedActionSuccesses30d = %d, want 1", got.ApprovedActionSuccesses30d)
}
}
func TestGetPulseIntelligenceActionTelemetry_CountsApprovedLifecycleAttemptsInsideWindow(t *testing.T) {
now := time.Date(2026, 6, 20, 10, 30, 0, 0, time.UTC)
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
store, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("getStore: %v", err)
}
currentApproved := pulseTelemetryActionRecord("current-approved-executed", now.Add(-2*time.Hour), unifiedresources.ActionStateApproved, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodUI, Timestamp: now.Add(-110 * time.Minute), Actor: "operator"},
})
if err := store.RecordActionAudit(currentApproved); err != nil {
t.Fatalf("RecordActionAudit(currentApproved): %v", err)
}
started, startEvent, err := unifiedresources.BeginActionExecution(currentApproved, "operator", now.Add(-90*time.Minute))
if err != nil {
t.Fatalf("BeginActionExecution(currentApproved): %v", err)
}
if err := store.RecordActionExecutionStart(started, startEvent); err != nil {
t.Fatalf("RecordActionExecutionStart(currentApproved): %v", err)
}
completed, doneEvent, err := unifiedresources.CompleteActionExecution(started, &unifiedresources.ExecutionResult{Success: true, Verification: &unifiedresources.ActionVerificationResult{Ran: true, Success: true, RanAt: now.Add(-80 * time.Minute)}}, "operator", now.Add(-80*time.Minute))
if err != nil {
t.Fatalf("CompleteActionExecution(currentApproved): %v", err)
}
if err := store.RecordActionExecutionResult(completed, doneEvent); err != nil {
t.Fatalf("RecordActionExecutionResult(currentApproved): %v", err)
}
oldCreatedAt := since.Add(-time.Minute)
oldApproved := pulseTelemetryActionRecord("old-approved-executed", oldCreatedAt, unifiedresources.ActionStateApproved, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodAPI, Timestamp: oldCreatedAt.Add(time.Minute), Actor: "operator"},
})
oldApproved.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(oldApproved); err != nil {
t.Fatalf("RecordActionAudit(oldApproved): %v", err)
}
started, startEvent, err = unifiedresources.BeginActionExecution(oldApproved, "operator", now.Add(-60*time.Minute))
if err != nil {
t.Fatalf("BeginActionExecution(oldApproved): %v", err)
}
if err := store.RecordActionExecutionStart(started, startEvent); err != nil {
t.Fatalf("RecordActionExecutionStart(oldApproved): %v", err)
}
oldRefused := pulseTelemetryActionRecord("old-approved-refused", oldCreatedAt, unifiedresources.ActionStateApproved, true, []unifiedresources.ActionApprovalRecord{
{Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodAPI, Timestamp: oldCreatedAt.Add(time.Minute), Actor: "operator"},
})
oldRefused.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(oldRefused); err != nil {
t.Fatalf("RecordActionAudit(oldRefused): %v", err)
}
if _, err := actionlifecycle.RecordRefusedExecution(store, oldRefused, "operator", now.Add(-30*time.Minute), unifiedresources.ErrActionPlanDrift); err != nil {
t.Fatalf("recordRefusedActionExecution(oldRefused): %v", err)
}
unapprovedRefused := pulseTelemetryActionRecord("old-unapproved-refused", oldCreatedAt, unifiedresources.ActionStatePlanned, false, nil)
unapprovedRefused.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(unapprovedRefused); err != nil {
t.Fatalf("RecordActionAudit(unapprovedRefused): %v", err)
}
if _, err := actionlifecycle.RecordRefusedExecution(store, unapprovedRefused, "operator", now.Add(-15*time.Minute), unifiedresources.ErrActionPlanDrift); err != nil {
t.Fatalf("recordRefusedActionExecution(unapprovedRefused): %v", err)
}
oldRejected := pulseTelemetryActionRecord("old-rejected-decision", oldCreatedAt, unifiedresources.ActionStatePending, true, nil)
oldRejected.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(oldRejected); err != nil {
t.Fatalf("RecordActionAudit(oldRejected): %v", err)
}
rejectedAt := now.Add(-10 * time.Minute)
rejected, rejectedEvent, err := unifiedresources.ApplyActionDecision(oldRejected, boundActionTestDecisionApproval(oldRejected.ID, oldRejected.Plan.PlanHash, "operator", unifiedresources.OutcomeRejected, rejectedAt), rejectedAt)
if err != nil {
t.Fatalf("ApplyActionDecision(oldRejected): %v", err)
}
if err := store.RecordActionDecision(rejected, rejectedEvent); err != nil {
t.Fatalf("RecordActionDecision(oldRejected): %v", err)
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ActionPlans30d != 1 {
t.Fatalf("ActionPlans30d = %d, want 1", got.ActionPlans30d)
}
if got.ApprovalRequests30d != 1 {
t.Fatalf("ApprovalRequests30d = %d, want 1", got.ApprovalRequests30d)
}
if got.RejectedActionDecisions30d != 1 {
t.Fatalf("RejectedActionDecisions30d = %d, want 1", got.RejectedActionDecisions30d)
}
if got.ApprovedActionDecisions30d != 1 {
t.Fatalf("ApprovedActionDecisions30d = %d, want 1", got.ApprovedActionDecisions30d)
}
if got.ApprovedActionAttempts30d != 3 {
t.Fatalf("ApprovedActionAttempts30d = %d, want 3", got.ApprovedActionAttempts30d)
}
if got.ApprovedActionSuccesses30d != 1 {
t.Fatalf("ApprovedActionSuccesses30d = %d, want 1", got.ApprovedActionSuccesses30d)
}
}
func TestGetPulseIntelligenceActionTelemetry_RequiresVerifiedOutcomeForApprovedSuccess(t *testing.T) {
now := time.Date(2026, 6, 20, 11, 30, 0, 0, time.UTC)
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
store, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("getStore: %v", err)
}
approved := []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: now.Add(-50 * time.Minute),
Actor: "operator",
}}
successOnly := pulseTelemetryActionRecord("success-only", now.Add(-55*time.Minute), unifiedresources.ActionStateCompleted, true, approved)
successOnly.Result = &unifiedresources.ExecutionResult{Success: true}
outcomeVerified := pulseTelemetryActionRecord("outcome-verified", now.Add(-45*time.Minute), unifiedresources.ActionStateCompleted, true, approved)
outcomeVerified.Result = &unifiedresources.ExecutionResult{Success: true}
outcomeVerified.VerificationOutcome = unifiedresources.VerificationOutcome{Status: unifiedresources.VerificationVerified}
legacyVerification := pulseTelemetryActionRecord("legacy-verification", now.Add(-35*time.Minute), unifiedresources.ActionStateCompleted, true, approved)
legacyVerification.Result = &unifiedresources.ExecutionResult{Success: true}
legacyVerification.Verification = &unifiedresources.ActionVerificationResult{
Ran: true,
Success: true,
RanAt: now.Add(-30 * time.Minute),
}
for _, record := range []unifiedresources.ActionAuditRecord{successOnly, outcomeVerified, legacyVerification} {
if err := store.RecordActionAudit(record); err != nil {
t.Fatalf("RecordActionAudit(%s): %v", record.ID, err)
}
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ApprovedActionSuccesses30d != 2 {
t.Fatalf("ApprovedActionSuccesses30d = %d, want 2 verified outcomes; bare successful execution must not count", got.ApprovedActionSuccesses30d)
}
}
func TestGetPulseIntelligenceActionTelemetry_AttributesApprovedActionFailureCauses(t *testing.T) {
// Cause classification measures staleness against the wall clock, so this
// test anchors records to real time instead of a fixed date.
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
store, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("getStore: %v", err)
}
approvedAt := func(at time.Time) []unifiedresources.ActionApprovalRecord {
return []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: at,
Actor: "operator",
}}
}
// Approved, then terminally refused before dispatch (plan drift).
refused := pulseTelemetryActionRecord("refused-pre-dispatch", now.Add(-3*time.Hour), unifiedresources.ActionStateApproved, true, approvedAt(now.Add(-3*time.Hour)))
refused.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(refused); err != nil {
t.Fatalf("RecordActionAudit(refused): %v", err)
}
if _, err := actionlifecycle.RecordRefusedExecution(store, refused, "operator", now.Add(-170*time.Minute), unifiedresources.ErrActionPlanDrift); err != nil {
t.Fatalf("RecordRefusedExecution(refused): %v", err)
}
// Approved, dispatched, execution failed on the agent.
execFailed := pulseTelemetryActionRecord("exec-failed", now.Add(-2*time.Hour), unifiedresources.ActionStateFailed, true, approvedAt(now.Add(-2*time.Hour)))
execFailed.Result = &unifiedresources.ExecutionResult{Success: false, ErrorMessage: "image pull failed"}
if err := store.RecordActionAudit(execFailed); err != nil {
t.Fatalf("RecordActionAudit(execFailed): %v", err)
}
// Approved, execution succeeded, but no verification evidence confirmed it.
unverified := pulseTelemetryActionRecord("completed-unverified", now.Add(-100*time.Minute), unifiedresources.ActionStateCompleted, true, approvedAt(now.Add(-100*time.Minute)))
unverified.Result = &unifiedresources.ExecutionResult{Success: true}
if err := store.RecordActionAudit(unverified); err != nil {
t.Fatalf("RecordActionAudit(unverified): %v", err)
}
// Approved and abandoned in executing state well past the dispatch window.
stuck := pulseTelemetryActionRecord("stuck-executing", now.Add(-5*time.Hour), unifiedresources.ActionStateExecuting, true, approvedAt(now.Add(-5*time.Hour)))
if err := store.RecordActionAudit(stuck); err != nil {
t.Fatalf("RecordActionAudit(stuck): %v", err)
}
// Approved and still legitimately in flight: no failure bucket.
inFlight := pulseTelemetryActionRecord("in-flight", now.Add(-5*time.Minute), unifiedresources.ActionStateExecuting, true, approvedAt(now.Add(-5*time.Minute)))
if err := store.RecordActionAudit(inFlight); err != nil {
t.Fatalf("RecordActionAudit(inFlight): %v", err)
}
// Approved and verified success: not a failure.
verified := pulseTelemetryActionRecord("verified-success", now.Add(-90*time.Minute), unifiedresources.ActionStateCompleted, true, approvedAt(now.Add(-90*time.Minute)))
verified.Result = &unifiedresources.ExecutionResult{Success: true}
verified.VerificationOutcome = unifiedresources.VerificationOutcome{Status: unifiedresources.VerificationVerified}
if err := store.RecordActionAudit(verified); err != nil {
t.Fatalf("RecordActionAudit(verified): %v", err)
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ApprovedActionAttempts30d != 6 {
t.Fatalf("ApprovedActionAttempts30d = %d, want 6", got.ApprovedActionAttempts30d)
}
if got.ApprovedActionSuccesses30d != 1 {
t.Fatalf("ApprovedActionSuccesses30d = %d, want 1", got.ApprovedActionSuccesses30d)
}
if got.ApprovedActionFailuresPreDispatch30d != 1 {
t.Fatalf("ApprovedActionFailuresPreDispatch30d = %d, want 1", got.ApprovedActionFailuresPreDispatch30d)
}
if got.ApprovedActionFailuresExecution30d != 1 {
t.Fatalf("ApprovedActionFailuresExecution30d = %d, want 1", got.ApprovedActionFailuresExecution30d)
}
if got.ApprovedActionFailuresUnverified30d != 1 {
t.Fatalf("ApprovedActionFailuresUnverified30d = %d, want 1", got.ApprovedActionFailuresUnverified30d)
}
if got.ApprovedActionStuckExecuting30d != 1 {
t.Fatalf("ApprovedActionStuckExecuting30d = %d, want 1", got.ApprovedActionStuckExecuting30d)
}
if got.ApprovedActionInFlight30d != 1 {
t.Fatalf("ApprovedActionInFlight30d = %d, want 1", got.ApprovedActionInFlight30d)
}
if got.ApprovedActionUnclassified30d != 0 {
t.Fatalf("ApprovedActionUnclassified30d = %d, want 0", got.ApprovedActionUnclassified30d)
}
if got.ApprovedActionRefusalsPlanStale30d != 1 ||
got.ApprovedActionRefusalsPolicy30d != 0 ||
got.ApprovedActionRefusalsCapability30d != 0 ||
got.ApprovedActionRefusalsTargetChanged30d != 0 ||
got.ApprovedActionRefusalsPrerequisite30d != 0 ||
got.ApprovedActionRefusalsContract30d != 0 ||
got.ApprovedActionRefusalsUncoded30d != 0 ||
got.ApprovedActionRefusalsOther30d != 0 {
t.Fatalf(
"refusal categories = %d/%d/%d/%d/%d/%d/%d/%d, want 1/0/0/0/0/0/0/0",
got.ApprovedActionRefusalsPlanStale30d,
got.ApprovedActionRefusalsPolicy30d,
got.ApprovedActionRefusalsCapability30d,
got.ApprovedActionRefusalsTargetChanged30d,
got.ApprovedActionRefusalsPrerequisite30d,
got.ApprovedActionRefusalsContract30d,
got.ApprovedActionRefusalsUncoded30d,
got.ApprovedActionRefusalsOther30d,
)
}
// The completed-unverified record is the most recent failure; the legacy
// row carries no canonical reason code, so the sanitized fallback applies.
if got.ApprovedActionLastFailureReason30d != "verification_unconfirmed" {
t.Fatalf("ApprovedActionLastFailureReason30d = %q, want %q", got.ApprovedActionLastFailureReason30d, "verification_unconfirmed")
}
}
func TestGetPulseIntelligenceActionTelemetry_CountsOnlyIndependentPatrolFindingResolutions(t *testing.T) {
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: t.TempDir()}),
}
store, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("getStore: %v", err)
}
approved := []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved, Method: unifiedresources.MethodUI,
Timestamp: now.Add(-time.Hour), Actor: "operator",
}}
makeCompleted := func(id string, evidenceClass unifiedresources.ActionEvidenceClass, origin *unifiedresources.ActionOrigin) unifiedresources.ActionAuditRecord {
record := pulseTelemetryActionRecord(id, now.Add(-time.Hour), unifiedresources.ActionStateCompleted, true, approved)
truth := apiActionResultTruth(t, unifiedresources.ActionExecutionSucceeded, unifiedresources.ActionVerificationConfirmed, evidenceClass)
record.Result = &unifiedresources.ExecutionResult{ActionResultV2: &truth}
record.Origin = origin
return record
}
for _, record := range []unifiedresources.ActionAuditRecord{
makeCompleted("linked-independent", unifiedresources.ActionEvidenceIndependent, &unifiedresources.ActionOrigin{
Surface: "patrol", FindingID: "finding-1", InvestigationID: "investigation-1",
}),
makeCompleted("linked-agent-attested", unifiedresources.ActionEvidenceAgentAttested, &unifiedresources.ActionOrigin{
Surface: "patrol", FindingID: "finding-2", InvestigationID: "investigation-2",
}),
makeCompleted("independent-without-finding", unifiedresources.ActionEvidenceIndependent, nil),
} {
if err := store.RecordActionAudit(record); err != nil {
t.Fatalf("RecordActionAudit(%s): %v", record.ID, err)
}
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ApprovedActionAttempts30d != 3 || got.ApprovedActionSuccesses30d != 3 {
t.Fatalf("attempts/successes = %d/%d, want 3/3", got.ApprovedActionAttempts30d, got.ApprovedActionSuccesses30d)
}
if got.PatrolActionPlans30d != 2 || got.PatrolApprovalRequests30d != 2 {
t.Fatalf("Patrol-origin plans/approval requests = %d/%d, want 2/2", got.PatrolActionPlans30d, got.PatrolApprovalRequests30d)
}
if got.PatrolRejectedActionDecisions30d != 0 || got.PatrolApprovedActionDecisions30d != 2 {
t.Fatalf("Patrol-origin rejected/approved decisions = %d/%d, want 0/2", got.PatrolRejectedActionDecisions30d, got.PatrolApprovedActionDecisions30d)
}
if got.PatrolApprovedActionAttempts30d != 2 || got.PatrolApprovedActionSuccesses30d != 2 {
t.Fatalf("Patrol-origin attempts/successes = %d/%d, want 2/2", got.PatrolApprovedActionAttempts30d, got.PatrolApprovedActionSuccesses30d)
}
if got.VerifiedFindingResolutions30d != 1 {
t.Fatalf("VerifiedFindingResolutions30d = %d, want 1", got.VerifiedFindingResolutions30d)
}
}
func TestGetPulseIntelligenceActionTelemetry_LastFailureReasonUsesCanonicalReasonCode(t *testing.T) {
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
store, err := router.resourceHandlers.getStore("default")
if err != nil {
t.Fatalf("getStore: %v", err)
}
refused := pulseTelemetryActionRecord("refused-plan-drift", now.Add(-time.Hour), unifiedresources.ActionStateApproved, true, []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: now.Add(-time.Hour),
Actor: "operator",
}})
refused.Plan.ExpiresAt = now.Add(time.Hour)
if err := store.RecordActionAudit(refused); err != nil {
t.Fatalf("RecordActionAudit(refused): %v", err)
}
if _, err := actionlifecycle.RecordRefusedExecution(store, refused, "operator", now.Add(-30*time.Minute), unifiedresources.ErrActionPlanDrift); err != nil {
t.Fatalf("RecordRefusedExecution(refused): %v", err)
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ApprovedActionFailuresPreDispatch30d != 1 {
t.Fatalf("ApprovedActionFailuresPreDispatch30d = %d, want 1", got.ApprovedActionFailuresPreDispatch30d)
}
if got.ApprovedActionLastFailureReason30d != "plan_drift" {
t.Fatalf("ApprovedActionLastFailureReason30d = %q, want %q", got.ApprovedActionLastFailureReason30d, "plan_drift")
}
}
func TestPulseIntelligenceApprovedActionRefusalCategory(t *testing.T) {
tests := map[string]string{
"plan_drift": "plan_stale",
"action_plan_expired": "plan_stale",
"action_replan_required": "plan_stale",
"resource_remediation_locked": "policy",
"policy_authorization_expired": "policy",
"action_emergency_stop": "policy",
"action_dry_run_only": "capability",
"action_execution_unavailable": "capability",
"agent_capability_unavailable": "capability",
"target_state_changed": "target_changed",
"target_precondition_failed": "target_changed",
"package_inventory_changed": "target_changed",
"cleanup_inventory_changed": "target_changed",
"target_inspection_unavailable": "prerequisite",
"package_manager_busy": "prerequisite",
"package_index_refresh_failed": "prerequisite",
"package_preflight_failed": "prerequisite",
"package_manager_unhealthy": "prerequisite",
"cleanup_preflight_failed": "prerequisite",
"image_pull_failed": "prerequisite",
"container_stop_failed": "prerequisite",
"container_rename_failed": "prerequisite",
"image_unchanged": "target_changed",
"action_contract_invalid": "contract",
"preflight_refused": "uncoded",
"pre_dispatch_refused": "uncoded",
"future_reason": "other",
}
for reason, want := range tests {
if got := pulseIntelligenceApprovedActionRefusalCategory(reason); got != want {
t.Errorf("reason %q category = %q, want %q", reason, got, want)
}
}
}
func pulseTelemetryActionRecord(
id string,
createdAt time.Time,
state unifiedresources.ActionState,
requiresApproval bool,
approvals []unifiedresources.ActionApprovalRecord,
) unifiedresources.ActionAuditRecord {
approvalPolicy := unifiedresources.ApprovalNone
if requiresApproval {
approvalPolicy = unifiedresources.ApprovalAdmin
}
return unifiedresources.ActionAuditRecord{
ID: id,
CreatedAt: createdAt,
UpdatedAt: createdAt,
State: state,
Request: unifiedresources.ActionRequest{
RequestID: "req-" + id,
ResourceID: "vm:101",
CapabilityName: "pulse_exec",
Reason: "test",
RequestedBy: "pulse_assistant",
Actor: unifiedresources.ActionActor{SubjectID: "pulse_assistant", Kind: unifiedresources.ActionActorService, CredentialID: "service:test", OrgID: "default"},
},
Plan: unifiedresources.ActionPlan{
ActionID: id,
RequestID: "req-" + id,
Allowed: true,
RequiresApproval: requiresApproval,
ApprovalPolicy: approvalPolicy,
ApprovalRequirement: unifiedresources.ApprovalRequirementForFloor(approvalPolicy),
PlannedAt: createdAt,
ExpiresAt: createdAt.Add(time.Hour),
PlanHash: "sha256:" + id,
},
Approvals: approvals,
}
}
// Round 40 Medium #3 regression: action telemetry must keep a cold SQLite
// read-only handle open through Patrol attribution and outcome classification
// so an action created before the 30-day window with a recent approval/attempt
// inside the window is still fetched via GetActionAudit.
func TestRound40_ActionTelemetryKeepsColdStoreOpenForOlderActionWithRecentTransition(t *testing.T) {
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
// Seed a Patrol-origin action created 45 days ago (outside GetActionAudits creation
// window) that was approved and executed cleanly inside the last 30 days.
writer, err := unifiedresources.NewSQLiteResourceStore(dataDir, "default")
if err != nil {
t.Fatalf("NewSQLiteResourceStore: %v", err)
}
oldCreated := now.AddDate(0, 0, -45)
recentTransition := now.Add(-2 * time.Hour)
rec := pulseTelemetryActionRecord("older-patrol-recent-exec", oldCreated, unifiedresources.ActionStateCompleted, true, []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: recentTransition,
Actor: "operator",
}})
rec.UpdatedAt = recentTransition
rec.Origin = &unifiedresources.ActionOrigin{
Surface: "patrol",
FindingID: "finding-r40-cold",
InvestigationID: "inv-r40-cold",
}
truth := apiActionResultTruth(t, unifiedresources.ActionExecutionSucceeded, unifiedresources.ActionVerificationConfirmed, unifiedresources.ActionEvidenceIndependent)
rec.Result = &unifiedresources.ExecutionResult{
ActionResultV2: &truth,
}
if err := writer.RecordActionAudit(rec); err != nil {
_ = writer.Close()
t.Fatalf("RecordActionAudit: %v", err)
}
if err := writer.RecordActionLifecycleEvent(unifiedresources.ActionLifecycleEvent{
ActionID: rec.ID,
State: unifiedresources.ActionStateApproved,
Timestamp: recentTransition,
Actor: "operator",
}); err != nil {
_ = writer.Close()
t.Fatalf("RecordActionLifecycleEvent(approved): %v", err)
}
if err := writer.RecordActionLifecycleEvent(unifiedresources.ActionLifecycleEvent{
ActionID: rec.ID,
State: unifiedresources.ActionStateCompleted,
Timestamp: recentTransition.Add(time.Minute),
Actor: "operator",
}); err != nil {
_ = writer.Close()
t.Fatalf("RecordActionLifecycleEvent(completed): %v", err)
}
if err := writer.Close(); err != nil {
t.Fatalf("close writer: %v", err)
}
// Cold QueryService: store is NOT in cache, so StoreReadOnly opens a cold read-only handle.
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ApprovedActionDecisions30d != 1 || got.ApprovedActionAttempts30d != 1 || got.ApprovedActionSuccesses30d != 1 {
t.Fatalf("expected 1 recent decision/attempt/success for older action, got %+v", got)
}
if got.PatrolApprovedActionDecisions30d != 1 || got.PatrolApprovedActionAttempts30d != 1 || got.PatrolApprovedActionSuccesses30d != 1 {
t.Fatalf("cold store closed before GetActionAudit Patrol attribution: got %+v", got)
}
}
// Round 43 High #7 regression: outbound approved_action_last_failure_reason_30d
// must only emit closed-allowlist machine codes and never leak hostnames or IPs.
func TestRound43_ActionFailureReasonClosedAllowlistBlocksHostnamesAndIPs(t *testing.T) {
for _, identifying := range []string{"pve-prod-01.corp.lan", "192.168.1.42", "node-1.internal", "10.0.0.5"} {
if got := pulseIntelligenceSanitizedReasonCode(identifying, "execution_failed"); got != "execution_failed" {
t.Fatalf("identifying string %q leaked through allowlist as %q", identifying, got)
}
}
if got := pulseIntelligenceSanitizedReasonCode("plan_drift", "pre_dispatch_other"); got != "plan_drift" {
t.Fatalf("known reason code rejected: got %q", got)
}
}
// Round 43 Medium #7 regression: cross-org most-recent failure reason selection
// must preserve the globally newest failure regardless of organization order.
func TestRound43_ActionTelemetryCrossOrgMostRecentFailureOrderIndependent(t *testing.T) {
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
for _, order := range [][]string{{"org-newer", "org-older"}, {"org-older", "org-newer"}} {
dataDir := t.TempDir()
makeStore := func(orgID, actionID string, refusalErr error, at time.Time) {
st, err := unifiedresources.NewSQLiteResourceStore(dataDir, orgID)
if err != nil {
t.Fatalf("NewSQLiteResourceStore(%s): %v", orgID, err)
}
rec := pulseTelemetryActionRecord(actionID, at.Add(-time.Minute), unifiedresources.ActionStateApproved, true, []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: at.Add(-time.Minute),
Actor: "operator",
}})
rec.Plan.ExpiresAt = now.Add(time.Hour)
if err := st.RecordActionAudit(rec); err != nil {
_ = st.Close()
t.Fatalf("RecordActionAudit(%s): %v", orgID, err)
}
if _, err := actionlifecycle.RecordRefusedExecution(st, rec, "operator", at, refusalErr); err != nil {
_ = st.Close()
t.Fatalf("RecordRefusedExecution(%s): %v", orgID, err)
}
if err := st.Close(); err != nil {
t.Fatalf("Close(%s): %v", orgID, err)
}
}
makeStore("org-newer", "act-new", unifiedresources.ErrActionPlanDrift, now.Add(-1*time.Hour))
makeStore("org-older", "act-old", unifiedresources.ErrResourceRemediationLocked, now.Add(-5*time.Hour))
// Accumulate directly using the exact shared lastFailureAt loop in both orders.
rh := NewResourceHandlers(&config.Config{DataPath: dataDir})
var snap telemetry.PulseIntelligenceActionSnapshot
var lastFailureAt time.Time
for _, orgID := range order {
st, closeFn, err := rh.StoreReadOnly(orgID)
if err != nil || st == nil {
t.Fatalf("StoreReadOnly(%s): st=%v err=%v", orgID, st, err)
}
recs, err := st.GetActionAudits("", since, 0)
if err != nil {
closeFn()
t.Fatalf("GetActionAudits(%s): %v", orgID, err)
}
byID := map[string]unifiedresources.ActionAuditRecord{}
attempts := map[string]struct{}{}
for _, r := range recs {
byID[r.ID] = r
attempts[r.ID] = struct{}{}
}
if err := accumulatePulseIntelligenceApprovedActionOutcomes(&snap, &lastFailureAt, st, orgID, attempts, map[string]struct{}{}, byID, now); err != nil {
closeFn()
t.Fatalf("accumulate(%s): %v", orgID, err)
}
closeFn()
}
if snap.ApprovedActionLastFailureReason30d != "plan_drift" {
t.Fatalf("order %v: ApprovedActionLastFailureReason30d = %q, want plan_drift", order, snap.ApprovedActionLastFailureReason30d)
}
}
}
// Round 43 Medium #1 & #2 regression: if an org's SQLite store has an active
// external -wal (ErrReadOnlyStoreUnavailable), StoreReadOnly propagates the
// error and GetPulseIntelligenceActionTelemetry suppresses partial counts.
func TestRound43_ActionTelemetrySuppressesCountsWhenStoreHasExternalWAL(t *testing.T) {
now := time.Now().UTC()
since := now.Add(-telemetry.PulseIntelligenceTelemetryWindow)
dataDir := t.TempDir()
st, err := unifiedresources.NewSQLiteResourceStore(dataDir, "default")
if err != nil {
t.Fatalf("NewSQLiteResourceStore: %v", err)
}
rec := pulseTelemetryActionRecord("act-wal-1", now.Add(-1*time.Hour), unifiedresources.ActionStateFailed, true, []unifiedresources.ActionApprovalRecord{{
Outcome: unifiedresources.OutcomeApproved,
Method: unifiedresources.MethodUI,
Timestamp: now.Add(-1 * time.Hour),
Actor: "operator",
}})
if err := st.RecordActionAudit(rec); err != nil {
_ = st.Close()
t.Fatalf("RecordActionAudit: %v", err)
}
if err := st.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
// Find the actual seeded .db file and place a non-empty external -wal next to it.
var dbPath string
_ = filepath.Walk(dataDir, func(path string, info os.FileInfo, err error) error {
if err == nil && info != nil && !info.IsDir() && strings.HasSuffix(path, ".db") {
dbPath = path
}
return nil
})
if dbPath == "" {
t.Fatal("expected seeded resource store .db file")
}
if err := os.WriteFile(dbPath+"-wal", []byte("active-external-wal"), 0o600); err != nil {
t.Fatalf("seed wal: %v", err)
}
router := &Router{
resourceHandlers: NewResourceHandlers(&config.Config{DataPath: dataDir}),
}
got := router.GetPulseIntelligenceActionTelemetry(since)
if got.ActionPlans30d != 0 || got.ApprovedActionAttempts30d != 0 {
t.Fatalf("expected suppressed zero counts when store has active external WAL, got %+v", got)
}
}