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.

Lift remediation-plan rollback into investigation records

rcourtmana2c3dc77f169
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 02d465d3be..c13a5d7b15 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -265,7 +265,17 @@ runtime cost control, and shared AI transport surfaces.
Description and Recommendation: AddFromAlert backfills empty Impact
on existing findings; AddFromAI overwrites existing Impact when the
incoming finding carries one. Unknown alert types must return an
- empty impact rather than synthesizing generic copy. When `/api/ai/chat` receives `finding_id`, the
+ empty impact rather than synthesizing generic copy.
+ Investigation-record `Rollback` is sourced from the canonical
+ `RemediationPlan` when one exists for the finding:
+ `AggregatePlanRollbackSteps` in
+ `internal/ai/investigation_records.go` flattens
+ `RemediationStep.Rollback` strings into a deduplicated record-level
+ slice, and the patrol findings build site
+ (`internal/ai/patrol_findings.go`) populates `record.Rollback` from
+ `remediationEngine.GetPlanForFinding` when the engine and an active
+ plan are present. Rollback must remain absent rather than fabricated
+ when no plan exists, mirroring the impact rule. When `/api/ai/chat` receives `finding_id`, the
runtime must enrich the provider turn from that durable record while
preserving the user's authored prompt as the persisted conversation
message; the model-only handoff may persist as session metadata so
diff --git a/internal/ai/investigation_records.go b/internal/ai/investigation_records.go
index 5374bae45c..80eacdfe0b 100644
--- a/internal/ai/investigation_records.go
+++ b/internal/ai/investigation_records.go
@@ -149,6 +149,22 @@ func investigationRecordFixFromSession(fix *aicontracts.Fix) *aicontracts.Invest
return &recordFix
}
+// AggregatePlanRollbackSteps lifts the rollback strings authored on a
+// remediation plan's steps into a flat, deduplicated slice suitable for the
+// record-level InvestigationRecord.Rollback field. Empty rollback steps are
+// dropped, and nil plans return an empty slice. The caller is responsible
+// for calling this only when a remediation plan exists for the finding.
+func AggregatePlanRollbackSteps(plan *aicontracts.RemediationPlan) []string {
+ if plan == nil {
+ return []string{}
+ }
+ steps := make([]string, 0, len(plan.Steps))
+ for _, step := range plan.Steps {
+ steps = append(steps, step.Rollback)
+ }
+ return uniqueNonEmptyStrings(steps)
+}
+
func investigationRecordVerificationNotes(outcome aicontracts.InvestigationOutcome) []string {
switch outcome {
case aicontracts.OutcomeResolved, aicontracts.OutcomeFixVerified:
diff --git a/internal/ai/investigation_records_test.go b/internal/ai/investigation_records_test.go
index 117b728573..d910da9c44 100644
--- a/internal/ai/investigation_records_test.go
+++ b/internal/ai/investigation_records_test.go
@@ -106,6 +106,52 @@ func TestEmptyInvestigationRecord_NormalizesRollback(t *testing.T) {
}
}
+func TestAggregatePlanRollbackSteps(t *testing.T) {
+ t.Run("nil plan returns empty slice", func(t *testing.T) {
+ got := AggregatePlanRollbackSteps(nil)
+ if got == nil || len(got) != 0 {
+ t.Fatalf("expected empty slice for nil plan, got %#v", got)
+ }
+ })
+
+ t.Run("aggregates rollback strings from steps and drops empties", func(t *testing.T) {
+ plan := &aicontracts.RemediationPlan{
+ Steps: []aicontracts.RemediationStep{
+ {Order: 1, Description: "Restart service", Rollback: "systemctl stop unit && restore prior config"},
+ {Order: 2, Description: "Reseat datastore", Rollback: ""}, // empty rollback skipped
+ {Order: 3, Description: "Re-enable cron", Rollback: "Disable cron and revert maintenance window"},
+ },
+ }
+ got := AggregatePlanRollbackSteps(plan)
+ want := []string{
+ "systemctl stop unit && restore prior config",
+ "Disable cron and revert maintenance window",
+ }
+ if len(got) != len(want) {
+ t.Fatalf("expected %d rollback steps, got %d (%#v)", len(want), len(got), got)
+ }
+ for i, w := range want {
+ if got[i] != w {
+ t.Fatalf("step %d: expected %q, got %q", i, w, got[i])
+ }
+ }
+ })
+
+ t.Run("deduplicates identical rollback strings", func(t *testing.T) {
+ plan := &aicontracts.RemediationPlan{
+ Steps: []aicontracts.RemediationStep{
+ {Order: 1, Rollback: "rollback-a"},
+ {Order: 2, Rollback: "rollback-a"}, // duplicate
+ {Order: 3, Rollback: "rollback-b"},
+ },
+ }
+ got := AggregatePlanRollbackSteps(plan)
+ if len(got) != 2 {
+ t.Fatalf("expected 2 unique rollback steps, got %#v", got)
+ }
+ })
+}
+
func TestFindingsStore_UpdateInvestigationRecord(t *testing.T) {
store := NewFindingsStore()
store.Add(&Finding{
diff --git a/internal/ai/patrol_findings.go b/internal/ai/patrol_findings.go
index ea54bca762..9582d070c9 100644
--- a/internal/ai/patrol_findings.go
+++ b/internal/ai/patrol_findings.go
@@ -1250,6 +1250,16 @@ func (p *PatrolService) MaybeInvestigateFinding(f *Finding) {
latestInvestigation = orchestrator.GetInvestigationByFinding(latest.ID)
}
if record := BuildFindingInvestigationRecord(latest, latestInvestigation); record != nil {
+ // When a remediation plan exists for this finding, lift its
+ // per-step rollback strings into record.Rollback so the
+ // operator-facing investigation surface answers
+ // "what's the undo for the proposed fix?" at the record root
+ // rather than only in nested per-step payload.
+ if engine := p.remediationEngine; engine != nil {
+ if plan := engine.GetPlanForFinding(latest.ID); plan != nil {
+ record.Rollback = AggregatePlanRollbackSteps(plan)
+ }
+ }
if p.findings.UpdateInvestigationRecord(latest.ID, record) {
if refreshed := p.findings.Get(latest.ID); refreshed != nil {
latest = refreshed