package services import ( "regexp" "strings" ) type evidenceRule struct { Area string Check string SummaryContains string Statuses []string } type statusRule struct { Area string Checks []string } type incidentPolicy struct { Scope string Target string TargetPattern *regexp.Regexp DoctorArguments []string NextCommand string HealthySummary string StatusRules []statusRule EvidenceRules []evidenceRule RemediationArguments []string Action string Summary string RiskLevel string Preconditions []string RiskNotes []string Rollback string Validation []string } var incidentPolicies = []incidentPolicy{ { Scope: "opi5", Target: "k3s-storage", DoctorArguments: []string{"opi5", "--json"}, NextCommand: "./jeannie doctor opi5 --details", HealthySummary: "SSD/K3s storage is healthy. No recovery action is applicable.", StatusRules: []statusRule{{ Area: "Orange Pi", Checks: []string{"K3s runtime", "K3s storage mount"}, }}, EvidenceRules: []evidenceRule{{ Area: "Orange Pi diagnostics", Check: "K3s storage evidence", SummaryContains: "Stale read-only K3s mount", Statuses: []string{"warn", "fail"}, }}, RemediationArguments: []string{"opi5", "--repair-k3s"}, Action: "./jeannie doctor opi5 --repair-k3s", Summary: "Recover the stale Orange Pi K3s runtime only after the guarded storage workflow revalidates the mounted device and pinned cluster identity.", RiskLevel: "high", Preconditions: []string{ "Revalidate that the cited stale read-only mount evidence is current.", "Require failed or inactive K3s and exactly one unmounted USB ext4 candidate.", "Verify the candidate filesystem against the pinned K3s CA before replacing the mount.", "Supply an operator-reviewed rollback procedure.", }, RiskNotes: []string{ "The recovery stops private K3s workloads and changes host mount state.", "A wrong storage identity could expose an unrelated filesystem, so device names are never trusted.", "The workflow refuses busy mounts, ambiguous candidates, and cluster identity mismatches.", }, Rollback: "No automatic rollback is assumed. Keep K3s stopped and the candidate unmounted if any identity or readiness validation fails.", Validation: []string{"./jeannie doctor opi5", "./jeannie status cluster"}, }, { Scope: "cluster", TargetPattern: kubernetesApplicationName, DoctorArguments: []string{"cluster", "{target}", "--json"}, NextCommand: "./jeannie doctor cluster {target} --details", HealthySummary: "No warning or failing application evidence supports a remediation proposal.", StatusRules: []statusRule{{Area: "Kubernetes apps"}}, EvidenceRules: []evidenceRule{{Statuses: []string{"warn", "fail"}}}, RemediationArguments: []string{"cluster", "{target}", "--ai"}, Action: "./jeannie doctor cluster {target} --ai", Summary: "Ask the focused Jeannie Doctor and Holmes workflow to diagnose, apply a supported targeted repair, and validate the result.", RiskLevel: "high", Preconditions: []string{ "Authenticate an authorized operator.", "Revalidate that the cited evidence execution is current.", "Confirm Jeannie still advertises the exact Doctor command and arguments.", "Establish a target-specific backup and rollback procedure.", }, RiskNotes: []string{ "The Doctor/Holmes workflow can change Kubernetes runtime state.", "AI root-cause hypotheses are not proof and may be incomplete.", "The affected application may be temporarily unavailable during repair.", }, Rollback: "No target-specific rollback has been established automatically. Approval remains disabled until an operator supplies and validates one.", Validation: []string{"./jeannie doctor cluster {target} --details", "./jeannie status cluster"}, }, } func incidentPathForStatus(row StatusRow) string { for _, candidate := range incidentPolicies { for _, rule := range candidate.StatusRules { if row.Area != rule.Area || !statusCheckMatches(rule.Checks, row.Check) { continue } target := candidate.Target if target == "" && candidate.TargetPattern != nil && candidate.TargetPattern.MatchString(row.Check) { target = row.Check } if target != "" { return "/incidents/" + candidate.Scope + "/" + target } } } return "" } func statusCheckMatches(checks []string, check string) bool { if len(checks) == 0 { return true } for _, candidate := range checks { if candidate == check { return true } } return false } func incidentPolicyFor(scope string, target string) (incidentPolicy, bool) { for _, policy := range incidentPolicies { if policy.Scope != scope { continue } if policy.Target != "" && policy.Target == target { return renderIncidentPolicy(policy, target), true } if policy.TargetPattern != nil && policy.TargetPattern.MatchString(target) { return renderIncidentPolicy(policy, target), true } } return incidentPolicy{}, false } func renderIncidentPolicy(policy incidentPolicy, target string) incidentPolicy { replace := func(value string) string { return strings.ReplaceAll(value, "{target}", target) } renderSlice := func(values []string) []string { rendered := make([]string, len(values)) for index, value := range values { rendered[index] = replace(value) } return rendered } policy.DoctorArguments = renderSlice(policy.DoctorArguments) policy.RemediationArguments = renderSlice(policy.RemediationArguments) policy.Validation = renderSlice(policy.Validation) policy.NextCommand = replace(policy.NextCommand) policy.Action = replace(policy.Action) return policy } func (rule evidenceRule) matches(evidence IncidentEvidence) bool { if rule.Area != "" && evidence.Area != rule.Area { return false } if rule.Check != "" && evidence.Check != rule.Check { return false } if rule.SummaryContains != "" && !strings.Contains(evidence.Summary, rule.SummaryContains) { return false } if len(rule.Statuses) == 0 { return true } for _, status := range rule.Statuses { if evidence.Status == status { return true } } return false } func (policy incidentPolicy) matchingEvidenceIDs(evidence []IncidentEvidence) []string { ids := []string{} for _, item := range evidence { for _, rule := range policy.EvidenceRules { if rule.matches(item) { ids = append(ids, item.ID) break } } } return ids }