package findings import ( "fmt" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" ) // Syntax is the one-paragraph reporting instruction shared by the memory // section and the model prompt. const Syntax = "Report findings in an optional `# findings` section of output.md, one per line: a new one as `- : `; a status change to a ledger item as `- F3 fixed: `, `- F3 dismissed: ` or `- F3 settled: ` (`- F3 open: ` reopens a fixed one). The runner assigns IDs; reference them, and never re-raise a dismissed or settled finding." func line(item protocol.Finding) string { location := "" if item.Location != "" { location = " · " + item.Location } return fmt.Sprintf("%s · %s · %s%s — %s", item.ID, item.Severity, item.Origin, location, item.Summary) } // RenderMemory is the ledger section of a visit's essential memory: every open // finding (most severe first) and a short list of closed ones, bounded by // limit bytes. It is empty when the ledger is. func RenderMemory(ledger []protocol.Finding, limit int) string { if len(ledger) == 0 { return "" } counts := Counts(ledger) var b strings.Builder fmt.Fprintf(&b, "\n## Findings ledger (runner-owned)\n\n%d open, %d fixed, %d dismissed, %d settled. Full ledger: findings.md beside this file.\n\n", counts[protocol.FindingOpen], counts[protocol.FindingFixed], counts[protocol.FindingDismissed], counts[protocol.FindingSettled]) footer := "\n" + Syntax + "\n" budget := limit - b.Len() - len(footer) var body strings.Builder write := func(text string) bool { if body.Len()+len(text) > budget { return false } body.WriteString(text) return true } omitted := 0 if counts[protocol.FindingOpen] > 0 { write("Open:\n") for _, severity := range append(append([]string(nil), Severities...), "") { for _, item := range ledger { if item.Status != protocol.FindingOpen || (severity != "" && item.Severity != severity) || (severity == "" && SeverityRank(item.Severity) >= 0) { continue } if !write("- " + line(item) + "\n") { omitted++ } } } } closed := []string{} for _, item := range ledger { if item.Status == protocol.FindingDismissed || item.Status == protocol.FindingSettled { closed = append(closed, fmt.Sprintf("- %s %s — %s", item.ID, item.Status, capText(item.Summary, 80))) } } if len(closed) > 0 { write("\nClosed (do not re-raise):\n") for _, text := range closed { if !write(text + "\n") { omitted++ } } } var fixed []string for _, item := range ledger { if item.Status == protocol.FindingFixed { fixed = append(fixed, item.ID) } } if len(fixed) > 0 { if !write("\nFixed: " + strings.Join(fixed, ", ") + "\n") { omitted++ } } if omitted > 0 { fmt.Fprintf(&body, "- … %d more in findings.md\n", omitted) } b.WriteString(body.String()) b.WriteString(footer) return b.String() } // RenderFile is findings.md: the whole ledger with every item's history. func RenderFile(ledger []protocol.Finding) string { var b strings.Builder b.WriteString("# Findings ledger\n\nRunner-owned; IDs are stable for the run. " + Syntax + "\n") if len(ledger) == 0 { b.WriteString("\nNo findings yet.\n") return b.String() } for _, status := range []string{protocol.FindingOpen, protocol.FindingFixed, protocol.FindingDismissed, protocol.FindingSettled} { first := true for _, item := range ledger { if item.Status != status { continue } if first { fmt.Fprintf(&b, "\n## %s\n\n", strings.ToUpper(status[:1])+status[1:]) first = false } fmt.Fprintf(&b, "- %s (raised by %s, visit #%d)\n", line(item), item.RaisedBy, item.RaisedSequence) for _, event := range item.Events { if event.Sequence == item.RaisedSequence && event.Status == protocol.FindingOpen && event.Reason == "" { continue } reason := "" if event.Reason != "" { reason = ": " + event.Reason } fmt.Fprintf(&b, " - #%d %s → %s%s\n", event.Sequence, event.Stage, event.Status, reason) } } } return b.String() }