package execution import ( "encoding/json" "fmt" "strings" "go.laravel.com/db" "go.laravel.com/inertia" "go.laravel.com/model" "go.laravel.com/web" "app/models" "app/services" ) func Show(request *web.Request) *web.Response { id, err := request.PathInt("execution") if err != nil { return web.Fail(err) } execution, err := model.Query[models.OperationExecution]().Find(request, id) if err != nil { return web.Fail(err) } proposal, err := model.Query[models.RemediationProposal]().Find(request, execution.ProposalID) if err != nil { return web.Fail(err) } if execution.JeannieExecutionID == "" { return inertia.Render("Execution", inertia.Props{ "view": inertia.Props{"execution": execution, "proposal": proposal}, }) } remote, err := services.From(request.Context()).Jeannie.Execution(request.Context(), execution.JeannieExecutionID) if err != nil { return web.Fail(err) } if err := model.Update(request, &execution, db.Values{ "status": remote.Status, "output": remote.Output, "error": remote.Error, }); err != nil { return web.Fail(err) } execution.Status, execution.Output, execution.Error = remote.Status, remote.Output, remote.Error if remote.Status == "succeeded" || remote.Status == "failed" { if err := model.Update(request, &proposal, db.Values{"status": remote.Status}); err != nil { return web.Fail(err) } if execution.ReportID == 0 { reportID, err := createReport(request, &execution, proposal) if err != nil { return web.Fail(err) } execution.ReportID = reportID } } return inertia.Render("Execution", inertia.Props{ "view": inertia.Props{"execution": execution, "proposal": proposal}, }) } func createReport(request *web.Request, execution *models.OperationExecution, proposal models.RemediationProposal) (int64, error) { client := services.From(request.Context()).Jeannie after, validationErr := client.Doctor(request.Context(), proposal.Scope, proposal.Target) afterJSON := "[]" remaining := 0 firstCause := "No warning or failing evidence remained after the action." if validationErr == nil { encoded, err := json.Marshal(after.Evidence) if err != nil { return 0, err } afterJSON = string(encoded) for _, evidence := range after.Evidence { if evidence.Status == "warn" || evidence.Status == "fail" { remaining++ if firstCause == "No warning or failing evidence remained after the action." && strings.TrimSpace(evidence.Cause) != "" { firstCause = evidence.Cause } } } } outcome := "validated" validation := fmt.Sprintf("A fresh focused Doctor run found %d warning or failing evidence item(s).", remaining) if validationErr != nil { outcome = "validation-failed" validation = "Fresh focused Doctor validation could not complete: " + validationErr.Error() } else if execution.Status != "succeeded" || remaining > 0 { outcome = "needs-attention" } arguments := proposal.ArgumentsJSON findings := fmt.Sprintf("Proposal %d cited evidence fingerprint %s. Jeannie execution %s finished with status %s.", proposal.ID, proposal.EvidenceHash, execution.JeannieExecutionID, execution.Status) diagnostic := "Before the action, focused Doctor evidence supported the reviewed proposal. After the action: " + firstCause whys, _ := json.Marshal([]string{ "Why was an operation considered? The stored focused Doctor run contained warning or failing evidence.", "Why was this exact operation selected? It was the immutable Jeannie command and argument set attached to the proposal.", "Why was it allowed to run? A logged-in operator approved the proposal after its evidence fingerprint was revalidated.", "Why is the result considered " + outcome + "? The terminal execution state and fresh Doctor evidence produced that outcome.", "Why can recurrence be acted on? Keep the focused Doctor check, rollback procedure, and this before/after report as the repeatable control.", }) report := models.IncidentReport{ ExecutionID: execution.ID, ProposalID: proposal.ID, Scope: proposal.Scope, Target: proposal.Target, Outcome: outcome, Findings: findings, Diagnostic: diagnostic, FiveWhysJSON: string(whys), Mitigation: fmt.Sprintf("Jeannie executed %s with arguments %s. The operator-supplied rollback procedure was: %s", proposal.Command, arguments, proposal.Rollback), Validation: validation, Recommendation: "If the same evidence returns, compare it with this report before approving another action; update the focused Doctor check when it cannot distinguish desired from actual state.", BeforeEvidenceJSON: proposal.EvidenceJSON, AfterEvidenceJSON: afterJSON, ExecutionOutput: execution.Output, ApprovedBy: proposal.CreatedBy, } if err := model.Create(request, &report); err != nil { return 0, err } if err := model.Update(request, execution, db.Values{"report_id": report.ID}); err != nil { return 0, err } return report.ID, nil }