|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/sageox/ox/internal/config" |
| 12 | + "github.com/sageox/ox/internal/ledger" |
| 13 | +) |
| 14 | + |
| 15 | +// checkLedgerRedactionDebt surfaces sessions that the pre-push secret |
| 16 | +// gate auto-quarantined because it could not redact them through the |
| 17 | +// canonical chokepoint. See cmd/ox/prepush_autoredact.go for the |
| 18 | +// quarantine pipeline: bytes are moved to |
| 19 | +// <ledger>/.sageox/cache/quarantine/<session>/<file> and a JSON marker |
| 20 | +// is written under <ledger>/.sageox/cache/redaction-debt/<session>.json. |
| 21 | +// |
| 22 | +// This check is read-only and local-only. It does not re-scan content; |
| 23 | +// the markers are authoritative — they were produced by a fresh scan at |
| 24 | +// quarantine time. If the user manually moves a quarantined file back |
| 25 | +// to its in-place ledger path or removes the marker, the check |
| 26 | +// gracefully reflects the new state. |
| 27 | +// |
| 28 | +// --fix is intentionally a no-op: the recovery action is either |
| 29 | +// `ox session redact <session>` (interactive cleanup) or the user |
| 30 | +// manually moving the quarantined file back. The doctor command cannot |
| 31 | +// safely choose between those for the user; it surfaces the state and |
| 32 | +// gets out of the way. |
| 33 | +func checkLedgerRedactionDebt(fix bool) checkResult { |
| 34 | + name := "Ledger redaction debt" |
| 35 | + _ = fix // recovery is intentionally user-driven; see doc above. |
| 36 | + |
| 37 | + gitRoot := findGitRoot() |
| 38 | + if gitRoot == "" { |
| 39 | + return SkippedCheck(name, "not in git repo", "") |
| 40 | + } |
| 41 | + localCfg, err := config.LoadLocalConfig(gitRoot) |
| 42 | + if err != nil { |
| 43 | + return SkippedCheck(name, "config error", "") |
| 44 | + } |
| 45 | + ledgerPath := resolveLedgerPathForAudit(localCfg) |
| 46 | + if ledgerPath == "" { |
| 47 | + return SkippedCheck(name, "no ledger configured", "") |
| 48 | + } |
| 49 | + if !ledger.Exists(ledgerPath) { |
| 50 | + return SkippedCheck(name, "ledger directory does not exist", "") |
| 51 | + } |
| 52 | + |
| 53 | + debtDir := filepath.Join(ledgerPath, ".sageox", "cache", "redaction-debt") |
| 54 | + if _, err := os.Stat(debtDir); err != nil { |
| 55 | + if os.IsNotExist(err) { |
| 56 | + return PassedCheck(name, "no quarantined sessions") |
| 57 | + } |
| 58 | + return FailedCheck(name, fmt.Sprintf("stat debt dir: %v", err), "") |
| 59 | + } |
| 60 | + summaries, malformed := readDebtSummaries(debtDir) |
| 61 | + |
| 62 | + if len(summaries) == 0 && len(malformed) == 0 { |
| 63 | + return PassedCheck(name, "no quarantined sessions") |
| 64 | + } |
| 65 | + |
| 66 | + var b strings.Builder |
| 67 | + fmt.Fprintf(&b, "%d session(s) quarantined; bytes preserved under .sageox/cache/quarantine/", |
| 68 | + len(summaries)) |
| 69 | + if len(malformed) > 0 { |
| 70 | + fmt.Fprintf(&b, "; %d marker(s) unreadable", len(malformed)) |
| 71 | + } |
| 72 | + msg := b.String() |
| 73 | + |
| 74 | + var detail strings.Builder |
| 75 | + for _, s := range summaries { |
| 76 | + fmt.Fprintf(&detail, " %s — %d finding(s) across %d file(s); detectors: %s\n", |
| 77 | + s.session, s.findings, s.files, strings.Join(s.detectors, ", ")) |
| 78 | + } |
| 79 | + detail.WriteString("\nNext steps for each session:\n") |
| 80 | + detail.WriteString(" 1. Inspect bytes at .sageox/cache/quarantine/<session>/\n") |
| 81 | + detail.WriteString(" 2. Run `ox session redact <session>` for interactive cleanup, OR\n") |
| 82 | + detail.WriteString(" manually scrub the file and move it back to sessions/<session>/\n") |
| 83 | + detail.WriteString(" 3. Re-stage and commit; the next push will publish the cleaned bytes\n") |
| 84 | + if len(malformed) > 0 { |
| 85 | + detail.WriteString("\nUnreadable markers (remove and re-run if no quarantined bytes exist):\n") |
| 86 | + for _, m := range malformed { |
| 87 | + fmt.Fprintf(&detail, " .sageox/cache/redaction-debt/%s\n", m) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Use WarningCheck (passed=true, warning=true) rather than |
| 92 | + // FailedCheck — debt is a state the user opted into by attempting to |
| 93 | + // push something the gate couldn't clean, and the system already |
| 94 | + // handled it gracefully (rest of push proceeded). Doctor shouldn't |
| 95 | + // treat this as an error; it's a reminder. |
| 96 | + return WarningCheck(name, msg, detail.String()) |
| 97 | +} |
| 98 | + |
| 99 | +// debtSummary is the per-marker shape produced by readDebtSummaries. |
| 100 | +// Aggregates the parts of redactionDebtRecord the doctor surface uses; |
| 101 | +// hides the full record from the caller (markers carry filenames and |
| 102 | +// line numbers, never matched bytes — ox-zyg7). |
| 103 | +type debtSummary struct { |
| 104 | + session string |
| 105 | + marker string |
| 106 | + findings int |
| 107 | + files int |
| 108 | + detectors []string |
| 109 | +} |
| 110 | + |
| 111 | +// readDebtSummaries walks debtDir and parses every *.json marker into |
| 112 | +// a debtSummary. Returns (summaries, malformed) — malformed lists |
| 113 | +// filenames whose contents could not be parsed as a redactionDebtRecord. |
| 114 | +// Separated from checkLedgerRedactionDebt so a unit test can exercise |
| 115 | +// the parse + aggregation logic without needing a full project / |
| 116 | +// ledger-config harness. |
| 117 | +func readDebtSummaries(debtDir string) (summaries []debtSummary, malformed []string) { |
| 118 | + entries, err := os.ReadDir(debtDir) |
| 119 | + if err != nil { |
| 120 | + return nil, nil |
| 121 | + } |
| 122 | + for _, entry := range entries { |
| 123 | + if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") { |
| 124 | + continue |
| 125 | + } |
| 126 | + markerAbs := filepath.Join(debtDir, entry.Name()) |
| 127 | + buf, err := os.ReadFile(markerAbs) |
| 128 | + if err != nil { |
| 129 | + malformed = append(malformed, entry.Name()) |
| 130 | + continue |
| 131 | + } |
| 132 | + var rec redactionDebtRecord |
| 133 | + if err := json.Unmarshal(buf, &rec); err != nil { |
| 134 | + malformed = append(malformed, entry.Name()) |
| 135 | + continue |
| 136 | + } |
| 137 | + detSet := map[string]struct{}{} |
| 138 | + for _, f := range rec.Findings { |
| 139 | + detSet[f.Detector] = struct{}{} |
| 140 | + } |
| 141 | + dets := make([]string, 0, len(detSet)) |
| 142 | + for d := range detSet { |
| 143 | + dets = append(dets, d) |
| 144 | + } |
| 145 | + sort.Strings(dets) |
| 146 | + summaries = append(summaries, debtSummary{ |
| 147 | + session: rec.SessionName, |
| 148 | + marker: entry.Name(), |
| 149 | + findings: len(rec.Findings), |
| 150 | + files: len(rec.QuarantinePaths), |
| 151 | + detectors: dets, |
| 152 | + }) |
| 153 | + } |
| 154 | + sort.Slice(summaries, func(i, j int) bool { |
| 155 | + return summaries[i].session < summaries[j].session |
| 156 | + }) |
| 157 | + return summaries, malformed |
| 158 | +} |
0 commit comments