keyhog-core 0.5.43

keyhog-core: shared data model and detector specifications for the KeyHog secret scanner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//! Reporting logic for scan results.

pub(crate) mod csv;
pub(crate) mod escape;
pub(crate) mod github_annotations;
pub(crate) mod gitlab_sast;
pub(crate) mod html;
pub(crate) mod json;
pub(crate) mod junit;
pub(crate) mod sarif;
mod style;
pub(crate) mod text;

#[path = "report/sarif_uri.rs"]
pub(crate) mod sarif_uri;

use std::collections::BTreeMap;
use std::io::Write;

use crate::VerifiedFinding;

/// Serialize redacted companion values deterministically for report formats
/// that expose a scalar details field instead of the native JSON object.
pub(crate) fn companions_json(finding: &VerifiedFinding) -> Result<String, ReportError> {
    let companions: BTreeMap<&str, &str> = finding
        .companions_redacted
        .iter()
        .map(|(key, value)| (key.as_str(), value.as_str()))
        .collect();
    Ok(serde_json::to_string(&companions)?)
}

/// Serialize the canonical Tier-B remediation projection for scalar report
/// formats. Keeping this beside companion serialization prevents CSV and other
/// adapters from inventing provider-specific remediation logic.
pub(crate) fn remediation_json(finding: &VerifiedFinding) -> Result<String, ReportError> {
    let remediation =
        crate::auto_fix::remediation_for(&finding.detector_id, &finding.service, finding.severity);
    Ok(serde_json::to_string(&remediation)?)
}

/// Common error type used by all reporters.
pub use anyhow::Error as ReportError;

/// Terminal state carried by detached scan artifacts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScanCompletionStatus {
    /// The requested input completed without coverage gaps.
    Success,
    /// Every requested byte completed, with one or more backend ranges replayed
    /// after a visible recoverable runtime fault.
    CompleteAfterRecovery,
    /// The artifact completed, but one or more requested inputs were not fully scanned.
    Partial,
    /// The operator or host interrupted the scan before completion.
    Cancelled,
    /// The scan failed before it could produce a trustworthy complete result.
    Failed,
}

impl Default for ScanCompletionStatus {
    fn default() -> Self {
        Self::Success
    }
}

impl ScanCompletionStatus {
    /// Derive the normal terminal state from the coverage summary.
    #[must_use]
    pub fn from_coverage_gaps(has_gaps: bool) -> Self {
        if has_gaps {
            Self::Partial
        } else {
            Self::Success
        }
    }

    /// Resolve metadata and observed coverage into one terminal state.
    ///
    /// A non-empty coverage summary upgrades an optimistic `success` metadata
    /// value to `partial`, while explicit `cancelled` and `failed` states are
    /// preserved even when no gap counter was recorded.
    #[must_use]
    pub fn resolve(metadata: Option<Self>, has_gaps: bool) -> Self {
        match metadata {
            Some(Self::Cancelled) => Self::Cancelled,
            Some(Self::Failed) => Self::Failed,
            Some(Self::Partial) => Self::Partial,
            Some(Self::CompleteAfterRecovery) if has_gaps => Self::Partial,
            Some(Self::CompleteAfterRecovery) => Self::CompleteAfterRecovery,
            Some(Self::Success) if has_gaps => Self::Partial,
            Some(status) => status,
            None => Self::from_coverage_gaps(has_gaps),
        }
    }
}

/// Stable, machine-diffable description of the resolved detection mode.
///
/// The preset names the shipped base (`default`, `fast`, `deep`, or
/// `precision`). `effective` contains the scalar and list-identity values the
/// scanner actually used, while `overrides` names values that refine that
/// preset. Maps are ordered so equivalent scans serialize byte-for-byte
/// identically across report formats and benchmark runs.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ResolvedScanManifest {
    /// Version of this manifest contract. Additive fields require a new minor
    /// report schema, while a breaking manifest change increments this value.
    pub schema_version: u16,
    /// Shipped base preset selected for the scan.
    pub preset: String,
    /// Resolved detection settings encoded as stable string values.
    pub effective: BTreeMap<String, String>,
    /// Settings that differ from the selected preset base.
    pub overrides: Vec<String>,
}

/// Bounded, non-secret summary of one completed automatic backend recovery.
///
/// Exact dispatch-local ranges remain available on the daemon wire. Detached
/// reports carry stable aggregates because scanner chunk indices restart for
/// each batch and therefore are not durable source identities.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScanBackendRecoverySummary {
    /// Number of recovery events represented by this summary row.
    pub events: usize,
    /// Selected backend that faulted, or `autoroute-invalid` when recovery was
    /// required before a trustworthy route could be selected.
    pub failed_backend: String,
    /// Backend that completed the unprocessed stable input ranges.
    pub recovery_backend: String,
    /// Number of canonical disjoint ranges recovered in this event.
    pub recovered_ranges: usize,
    /// Number of distinct scanner chunks containing those ranges.
    pub recovered_chunks: usize,
    /// Total recovered source bytes across the canonical ranges.
    pub recovered_bytes: u64,
    /// Non-secret runtime fault diagnostic.
    pub reason: String,
    /// Canonical command that repairs the quarantined autoroute identity.
    pub repair_command: String,
}

/// Format-neutral operator-visible metadata for a scan report.
///
/// The metadata belongs to the report, not to one renderer. Individual output
/// formats project the fields they can represent: HTML renders the complete
/// object, while schema-constrained formats retain their established fields.
/// Keeping this model in `keyhog-core` prevents the CLI and a single reporter
/// from growing competing definitions of scan identity and timing.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ScanReportMetadata {
    /// Stable non-secret identifier shared by artifacts from one scan run.
    /// Missing values deserialize as empty for reports produced before this
    /// field was introduced; current producers always populate it.
    #[serde(default)]
    pub scan_id: String,
    /// Terminal state for detached artifacts. Older reports default to success
    /// because they predate this explicit field and have no state to recover.
    #[serde(default)]
    pub scan_status: ScanCompletionStatus,
    /// Completed automatic backend recovery events. Empty means the scan did
    /// not recover a selected backend fault.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub backend_recoveries: Vec<ScanBackendRecoverySummary>,
    /// KeyHog crate/binary version that produced the report.
    pub keyhog_version: String,
    /// Git identity of the binary that produced the report.
    pub git_hash: String,
    /// Digest of the embedded detector set compiled into the binary.
    pub detector_digest: String,
    /// Digest of the effective scan configuration, when the orchestrator had
    /// a resolved configuration identity available.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub config_digest: Option<String>,
    /// Stable resolved preset and override manifest for mode comparisons.
    /// Absent only for reports produced by callers that do not have a CLI scan
    /// configuration (for example a library-created report).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved_scan: Option<ResolvedScanManifest>,
    /// UTC generation timestamp formatted as `YYYY-MM-DDTHH:MM:SS`.
    pub generated_at: String,
    /// UTC scan start timestamp formatted as `YYYY-MM-DDTHH:MM:SS`.
    pub scan_started_at: String,
    /// UTC scan finish timestamp formatted as `YYYY-MM-DDTHH:MM:SS`.
    pub scan_finished_at: String,
    /// Wall-clock scan duration in milliseconds.
    pub duration_ms: u128,
    /// Redacted operator-visible target labels for the requested scan sources.
    pub targets: Vec<String>,
    /// Number of source chunks the scanner consumed for this report.
    pub source_chunks_scanned: usize,
    /// Number of source bytes the scanner consumed for this report.
    pub source_bytes_scanned: u64,
    /// Number of loaded detector specs used by this scan.
    pub detector_count: usize,
}

/// Current major version for the versioned JSON report envelope.
pub const JSON_REPORT_SCHEMA_MAJOR: u16 = 1;
/// Current minor version for the versioned JSON report envelope.
pub const JSON_REPORT_SCHEMA_MINOR: u16 = 7;
/// Current minor version for the versioned JSONL stream contract.
pub const JSONL_REPORT_SCHEMA_MINOR: u16 = 8;

/// Version marker carried by every versioned JSON report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct JsonReportSchemaVersion {
    /// Incompatible schema generation.
    pub major: u16,
    /// Backward-compatible additive revision.
    pub minor: u16,
}

/// Versioned machine-readable JSON report.
///
/// A reader must reject an unsupported `major` and may accept any `minor`
/// under a supported major because minor revisions only add optional fields.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct JsonReportEnvelope {
    /// Version marker used to select the reader contract.
    pub schema_version: JsonReportSchemaVersion,
    /// Terminal state for the detached artifact, independent of process exit status.
    #[serde(default)]
    pub scan_status: ScanCompletionStatus,
    /// Optional scan-wide metadata supplied by the producer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<ScanReportMetadata>,
    /// Non-zero source or scanner coverage gaps observed during the scan.
    #[serde(default)]
    pub coverage_gap_summary: Vec<JsonReportCoverageGap>,
    /// Findings in the same redacted shape used by the legacy array.
    pub findings: Vec<VerifiedFinding>,
}

/// One scan-wide coverage gap preserved in a versioned JSON report.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct JsonReportCoverageGap {
    /// Stable machine-readable reason shared with SARIF/HTML projections.
    pub reason: String,
    /// Number of affected files, chunks, or invariant events.
    pub count: usize,
}

impl JsonReportEnvelope {
    /// Parse and validate a versioned JSON report.
    pub fn parse(input: &str) -> Result<Self, ReportError> {
        let report: Self = serde_json::from_str(input)?;
        if report.schema_version.major != JSON_REPORT_SCHEMA_MAJOR {
            anyhow::bail!(
                "unsupported JSON report schema major {}; this reader supports major {}",
                report.schema_version.major,
                JSON_REPORT_SCHEMA_MAJOR
            );
        }
        Ok(report)
    }
}

/// Header written as the first record of a versioned JSONL stream.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct JsonlStreamHeader {
    /// Distinguishes the stream header from a finding record.
    pub record_type: String,
    /// Version marker used to select the JSONL reader contract.
    pub schema_version: JsonReportSchemaVersion,
    /// Optional scan-wide metadata supplied by the producer.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<ScanReportMetadata>,
}

impl JsonlStreamHeader {
    /// Construct a stream header for the current schema.
    #[must_use]
    pub fn new(metadata: Option<&ScanReportMetadata>) -> Self {
        Self {
            record_type: "header".to_string(),
            schema_version: JsonReportSchemaVersion {
                major: JSON_REPORT_SCHEMA_MAJOR,
                minor: JSONL_REPORT_SCHEMA_MINOR,
            },
            metadata: metadata.cloned(),
        }
    }

    /// Parse and validate one JSONL header record.
    pub fn parse(input: &str) -> Result<Self, ReportError> {
        let header: Self = serde_json::from_str(input)?;
        if header.record_type != "header" {
            anyhow::bail!(
                "invalid JSONL stream header record_type {:?}",
                header.record_type
            );
        }
        if header.schema_version.major != JSON_REPORT_SCHEMA_MAJOR {
            anyhow::bail!(
                "unsupported JSONL report schema major {}; this reader supports major {}",
                header.schema_version.major,
                JSON_REPORT_SCHEMA_MAJOR
            );
        }
        Ok(header)
    }
}

/// One validated segment of a JSONL input. Concatenated streams produce one
/// segment per header, so boundaries remain explicit instead of being inferred
/// from finding content.
#[derive(Debug, Clone)]
pub struct JsonlStream {
    /// Header that governed this segment.
    pub header: JsonlStreamHeader,
    /// Terminal summary when the producer completed normally. None means
    /// the input ended before completion and must not be treated as complete.
    pub summary: Option<JsonlStreamSummary>,
    /// Findings following the header until the next header or end of input.
    pub findings: Vec<VerifiedFinding>,
}

/// Terminal record written when a versioned JSONL stream completes.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct JsonlStreamSummary {
    /// Distinguishes the terminal record from headers and findings.
    pub record_type: String,
    /// Completion state for the stream.
    pub status: String,
    /// Terminal scan state; `status` remains the transport completion marker.
    #[serde(default)]
    pub scan_status: ScanCompletionStatus,
    /// Number of finding records written before this summary.
    pub finding_count: usize,
    /// Coverage gaps observed during the stream.
    #[serde(default)]
    pub coverage_gap_summary: Vec<JsonReportCoverageGap>,
}

impl JsonlStreamSummary {
    /// Construct a complete summary for a stream.
    #[must_use]
    pub fn complete(finding_count: usize, coverage_gap_summary: &[(String, usize)]) -> Self {
        Self::complete_with_status(
            finding_count,
            ScanCompletionStatus::from_coverage_gaps(!coverage_gap_summary.is_empty()),
            coverage_gap_summary,
        )
    }

    /// Construct a complete summary using an explicitly recorded terminal
    /// state from the scan metadata.
    #[must_use]
    pub fn complete_with_status(
        finding_count: usize,
        scan_status: ScanCompletionStatus,
        coverage_gap_summary: &[(String, usize)],
    ) -> Self {
        Self {
            record_type: "summary".to_string(),
            status: "complete".to_string(),
            scan_status,
            finding_count,
            coverage_gap_summary: coverage_gap_summary
                .iter()
                .map(|(reason, count)| JsonReportCoverageGap {
                    reason: reason.clone(),
                    count: *count,
                })
                .collect(),
        }
    }

    fn parse(input: &str) -> Result<Self, ReportError> {
        let summary: Self = serde_json::from_str(input)?;
        if summary.record_type != "summary" || summary.status != "complete" {
            anyhow::bail!(
                "invalid JSONL stream summary: record_type={:?}, status={:?}",
                summary.record_type,
                summary.status
            );
        }
        Ok(summary)
    }
}

impl JsonlStream {
    /// Whether the stream has a validated terminal summary.
    #[must_use]
    pub fn is_complete(&self) -> bool {
        self.summary.is_some()
    }
}

/// Parse one or more concatenated, versioned JSONL streams.
pub fn parse_jsonl_stream(input: &str) -> Result<Vec<JsonlStream>, ReportError> {
    let mut streams = Vec::new();
    let mut current: Option<JsonlStream> = None;

    for (index, line) in input.lines().enumerate() {
        let line_number = index + 1;
        if line.trim().is_empty() {
            anyhow::bail!("JSONL line {line_number} is empty; remove blank records");
        }
        let value: serde_json::Value = serde_json::from_str(line)
            .map_err(|error| anyhow::anyhow!("invalid JSONL line {line_number}: {error}"))?;
        let is_header =
            value.get("record_type").and_then(serde_json::Value::as_str) == Some("header");
        if is_header {
            if let Some(stream) = current.take() {
                streams.push(stream);
            }
            current = Some(JsonlStream {
                header: JsonlStreamHeader::parse(line)?,
                summary: None,
                findings: Vec::new(),
            });
            continue;
        }

        let stream = current.as_mut().ok_or_else(|| {
            anyhow::anyhow!("JSONL line {line_number} precedes its stream header")
        })?;
        let is_summary =
            value.get("record_type").and_then(serde_json::Value::as_str) == Some("summary");
        if is_summary {
            if stream.summary.is_some() {
                anyhow::bail!("JSONL line {line_number} repeats the terminal summary");
            }
            let summary = JsonlStreamSummary::parse(line)?;
            if summary.finding_count != stream.findings.len() {
                anyhow::bail!(
                    "JSONL summary count {} does not match {} finding records",
                    summary.finding_count,
                    stream.findings.len()
                );
            }
            stream.summary = Some(summary);
            continue;
        }
        if stream.summary.is_some() {
            anyhow::bail!("JSONL line {line_number} follows the terminal summary");
        }
        let finding = serde_json::from_value(value).map_err(|error| {
            anyhow::anyhow!("invalid finding on JSONL line {line_number}: {error}")
        })?;
        stream.findings.push(finding);
    }

    if let Some(stream) = current {
        streams.push(stream);
    }
    if streams.is_empty() {
        anyhow::bail!("JSONL stream is empty; expected a versioned header record");
    }
    Ok(streams)
}

/// Compatibility name for callers that used the original HTML-only type.
///
/// New code should use [`ScanReportMetadata`]. The alias is intentionally kept
/// so a report-format migration does not break library consumers.
pub type HtmlScanMetadata = ScanReportMetadata;

/// The format-neutral input shared by every report renderer.
///
/// Renderers borrow findings so constructing a report does not copy a large
/// finding set. Metadata is optional for the legacy [`write_report`] wrapper;
/// production scan paths should pass it through [`write_scan_report`].
#[derive(Debug, Clone, Copy)]
pub struct ScanReport<'a> {
    /// Findings after all scan filtering, suppression, and verification.
    pub findings: &'a [VerifiedFinding],
    /// Common scan identity and timing metadata, when the caller has it.
    pub metadata: Option<&'a ScanReportMetadata>,
}

impl<'a> ScanReport<'a> {
    /// Create a report without optional metadata.
    pub fn new(findings: &'a [VerifiedFinding]) -> Self {
        Self {
            findings,
            metadata: None,
        }
    }

    /// Attach the common scan metadata used by format projections.
    #[must_use]
    pub fn with_metadata(mut self, metadata: &'a ScanReportMetadata) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

/// Output format and formatter options for [`write_report`].
pub enum ReportFormat {
    /// Human-oriented terminal output.
    Text {
        /// Emit ANSI color escapes.
        color: bool,
        /// Number of example suppression hints to include.
        example_suppressions: usize,
        /// Include dogfood telemetry hints in the text report.
        dogfood_active: bool,
    },
    /// JSON array output.
    Json,
    /// Versioned JSON envelope output and its scan-wide coverage summary.
    JsonEnvelope {
        /// Non-zero source or scanner coverage gaps observed during the scan.
        coverage_gap_summary: Vec<(String, usize)>,
    },
    /// Newline-delimited JSON output.
    Jsonl,
    /// Versioned newline-delimited JSON output with a stream header.
    JsonlEnvelope {
        /// Non-zero source or scanner coverage gaps observed during the scan.
        coverage_gap_summary: Vec<(String, usize)>,
    },
    /// SARIF output.
    Sarif {
        /// Operator-visible scan coverage-gap summary entries.
        skip_summary: Vec<(String, usize)>,
    },
    /// CSV output.
    Csv,
    /// GitHub Actions workflow command annotations.
    GithubAnnotations,
    /// GitHub Actions annotations with a terminal scan coverage notice.
    GithubAnnotationsCoverage {
        /// Non-zero source or scanner coverage gaps observed during the scan.
        skip_summary: Vec<(String, usize)>,
    },
    /// GitLab SAST security report JSON.
    GitlabSast {
        /// UTC scan start time formatted as `YYYY-MM-DDTHH:MM:SS`.
        scan_started_at: String,
        /// UTC scan end time formatted as `YYYY-MM-DDTHH:MM:SS`.
        scan_finished_at: String,
    },
    /// GitLab SAST output with scan-wide coverage status.
    GitlabSastCoverage {
        /// UTC scan start time formatted as `YYYY-MM-DDTHH:MM:SS`.
        scan_started_at: String,
        /// UTC scan end time formatted as `YYYY-MM-DDTHH:MM:SS`.
        scan_finished_at: String,
        /// Non-zero source or scanner coverage gaps observed during the scan.
        skip_summary: Vec<(String, usize)>,
    },
    /// Self-contained HTML output.
    Html {
        /// Operator-visible scan coverage-gap summary entries (same data the
        /// SARIF report surfaces), rendered as a "coverage" panel so the report
        /// never reads as a clean bill of health when files went unscanned.
        skip_summary: Vec<(String, usize)>,
        /// Scan identity, timing, target, and size metadata for the report hero.
        metadata: Option<HtmlScanMetadata>,
    },
    /// JUnit XML output.
    Junit,
    /// JUnit XML output with deterministic scan coverage properties.
    JunitCoverage {
        /// Non-zero source or scanner coverage gaps observed during the scan.
        skip_summary: Vec<(String, usize)>,
    },
}

/// Write a complete findings report in the requested format.
pub fn write_report<W: Write + Send>(
    writer: W,
    format: ReportFormat,
    findings: &[VerifiedFinding],
) -> Result<(), ReportError> {
    write_scan_report(writer, format, ScanReport::new(findings))
}

/// Write a complete report from the shared scan model.
///
/// [`write_report`] remains as a compatibility wrapper for callers that only
/// have findings. New scan paths should use this entrypoint so every renderer
/// receives the same report object and metadata cannot be wired only to HTML.
pub fn write_scan_report<W: Write + Send>(
    writer: W,
    format: ReportFormat,
    report: ScanReport<'_>,
) -> Result<(), ReportError> {
    let findings = report.findings;
    let report_metadata = report.metadata;
    match format {
        ReportFormat::Text {
            color,
            example_suppressions,
            dogfood_active,
        } => {
            let mut reporter = text::TextReporter::with_color(writer, color);
            reporter.set_example_suppressions(example_suppressions);
            reporter.set_dogfood_active(dogfood_active);
            finish_reporter(reporter, findings)
        }
        ReportFormat::Json => finish_reporter(json::JsonArrayReporter::new(writer)?, findings),
        ReportFormat::JsonEnvelope {
            coverage_gap_summary,
        } => finish_reporter(
            json::JsonEnvelopeReporter::new(writer, report_metadata, &coverage_gap_summary)?,
            findings,
        ),
        ReportFormat::Jsonl => finish_reporter(json::JsonlReporter::new(writer), findings),
        ReportFormat::JsonlEnvelope {
            coverage_gap_summary,
        } => finish_reporter(
            json::JsonlEnvelopeReporter::new(writer, report_metadata, &coverage_gap_summary)?,
            findings,
        ),
        ReportFormat::Sarif { skip_summary } => finish_reporter(
            sarif::SarifReporter::new(writer)
                .with_skip_summary(skip_summary.clone())
                .with_scan_status(resolve_report_status(report_metadata, &skip_summary))
                .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::Csv => finish_reporter(csv::CsvReporter::new(writer)?, findings),
        ReportFormat::GithubAnnotations => finish_reporter(
            github_annotations::GithubAnnotationsReporter::new(writer)
                .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::GithubAnnotationsCoverage { skip_summary } => finish_reporter(
            github_annotations::GithubAnnotationsReporter::new(writer)
                .with_skip_summary(skip_summary.clone())
                .with_scan_status(resolve_report_status(report_metadata, &skip_summary))
                .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::GitlabSast {
            scan_started_at,
            scan_finished_at,
        } => finish_reporter(
            gitlab_sast::GitlabSastReporter::new(
                writer,
                report_time(
                    report_metadata,
                    scan_started_at,
                    |metadata| &metadata.scan_started_at,
                    "scan_started_at",
                )?,
                report_time(
                    report_metadata,
                    scan_finished_at,
                    |metadata| &metadata.scan_finished_at,
                    "scan_finished_at",
                )?,
            )
            .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::GitlabSastCoverage {
            scan_started_at,
            scan_finished_at,
            skip_summary,
        } => finish_reporter(
            gitlab_sast::GitlabSastReporter::new(
                writer,
                report_time(
                    report_metadata,
                    scan_started_at,
                    |metadata| &metadata.scan_started_at,
                    "scan_started_at",
                )?,
                report_time(
                    report_metadata,
                    scan_finished_at,
                    |metadata| &metadata.scan_finished_at,
                    "scan_finished_at",
                )?,
            )
            .with_skip_summary(skip_summary.clone())
            .with_scan_status(resolve_report_status(report_metadata, &skip_summary))
            .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::Html {
            skip_summary,
            metadata,
        } => finish_reporter(
            html::HtmlReporter::new(writer)
                .with_skip_summary(skip_summary)
                .with_metadata(merge_html_metadata(metadata, report_metadata)?),
            findings,
        ),
        ReportFormat::Junit => finish_reporter(
            junit::JunitReporter::new(writer)
                .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
        ReportFormat::JunitCoverage { skip_summary } => finish_reporter(
            junit::JunitReporter::new(writer)
                .with_skip_summary(skip_summary.clone())
                .with_scan_status(resolve_report_status(report_metadata, &skip_summary))
                .with_backend_recoveries(report_recoveries(report_metadata)),
            findings,
        ),
    }
}

fn report_recoveries(metadata: Option<&ScanReportMetadata>) -> Vec<ScanBackendRecoverySummary> {
    metadata
        .map(|value| value.backend_recoveries.clone())
        .unwrap_or_default() // LAW10: absent report metadata means no recovery rows; findings and coverage status are unchanged
}

fn resolve_report_status(
    metadata: Option<&ScanReportMetadata>,
    coverage_gap_summary: &[(String, usize)],
) -> ScanCompletionStatus {
    ScanCompletionStatus::resolve(
        metadata.map(|value| value.scan_status),
        !coverage_gap_summary.is_empty(),
    )
}

/// Write a CSV scan artifact with a self-describing scan-status preamble.
///
/// This dedicated entrypoint keeps the legacy [`ReportFormat::Csv`] enum
/// variant and its header-first byte contract unchanged for library callers,
/// while CLI scan artifacts can retain coverage state even when no finding row
/// exists.
pub fn write_csv_coverage_report<W: Write + Send>(
    writer: W,
    report: ScanReport<'_>,
    coverage_gap_summary: &[(String, usize)],
) -> Result<(), ReportError> {
    finish_reporter(
        csv::CsvReporter::with_scan_metadata(writer, report.metadata, coverage_gap_summary)?,
        report.findings,
    )
}

fn report_time(
    metadata: Option<&ScanReportMetadata>,
    explicit: String,
    select: fn(&ScanReportMetadata) -> &String,
    field: &str,
) -> Result<String, ReportError> {
    let Some(metadata) = metadata else {
        return Ok(explicit);
    };
    let canonical = select(metadata);
    if explicit != *canonical {
        anyhow::bail!(
            "report metadata conflict for {field}: format options and ScanReport disagree; pass one canonical value"
        );
    }
    Ok(explicit)
}

fn merge_html_metadata(
    explicit: Option<ScanReportMetadata>,
    report: Option<&ScanReportMetadata>,
) -> Result<Option<ScanReportMetadata>, ReportError> {
    match (explicit, report) {
        (Some(explicit), Some(report)) if explicit != *report => {
            anyhow::bail!(
                "report metadata conflict for HTML: format options and ScanReport disagree; pass one canonical value"
            );
        }
        (Some(explicit), _) => Ok(Some(explicit)),
        (None, report) => Ok(report.cloned()),
    }
}

fn finish_reporter<R: Reporter>(
    mut reporter: R,
    findings: &[VerifiedFinding],
) -> Result<(), ReportError> {
    for finding in findings {
        reporter.report(finding)?;
    }
    reporter.finish()?;
    Ok(())
}

/// Common trait for all finding reporters.
pub(crate) trait Reporter: Send {
    /// Report a single finding.
    fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError>;

    /// Finalize the report and flush buffered bytes.
    fn finish(&mut self) -> Result<(), ReportError>;
}

trait WriterBackedReporter {
    type Writer: Write;

    fn writer_mut(&mut self) -> &mut Self::Writer;

    fn flush_writer(&mut self) -> Result<(), ReportError> {
        self.writer_mut().flush()?;
        Ok(())
    }
}

/// Implements [`WriterBackedReporter`] for a reporter whose only state behind
/// the trait is a single `writer: W` field. Every reporter in this module is
/// generic over `W: Write + Send` and exposes its writer identically, so the
/// impl is purely mechanical, the macro keeps all nine reporters from drifting
/// to nine subtly different spellings of the same three lines. Invoked as
/// `impl_writer_backed!(CsvReporter);` inside each reporter's module, where both
/// `Write` and `WriterBackedReporter` are already in scope.
macro_rules! impl_writer_backed {
    ($reporter:ident) => {
        impl<W: Write + Send> WriterBackedReporter for $reporter<W> {
            type Writer = W;
            fn writer_mut(&mut self) -> &mut Self::Writer {
                &mut self.writer
            }
        }
    };
}
pub(crate) use impl_writer_backed;

// `BufferedFindingReporter` was the legacy buffer-everything trait. The
// SARIF reporter now streams results directly to its writer (audit
// 2026-04-26 audit), so the trait has no callers and is removed. Other
// reporters that still buffer (text, JSON-array) keep their state inline.