ripr 0.10.0

Find static mutation-exposure gaps before expensive mutation testing
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
use crate::config::RiprConfig;
use crate::domain::{Finding, LanguageId, LanguageStatus};
use crate::output::agent_seam_packets::{
    allowed_edit_surface_for_gap_route, gap_record_packet_do_not_do,
};
use crate::output::next_step::reconcile_next_step;
use crate::output::path::display_path;
use crate::output::perl_preview_card::{PerlPreviewCard, PerlRawEvidenceRef, perl_preview_card};
use crate::output::preview_actionability::{
    PreviewActionability, PreviewRawEvidenceRef, preview_actionability_for,
};
use crate::output::python_repair_card::{PythonRepairCard, python_repair_card};
use crate::output::typescript_packet_projection::typescript_gap_record_for;
use crate::output::typescript_preview_card::{
    TypeScriptPreviewCard, bun_cross_language_advisory_packet, stable_byte_proof_mode,
    typescript_preview_card,
};

use super::evidence_lines::{evidence_path_lines, weakness_lines};

pub(crate) fn render_finding_with_config(finding: &Finding, config: &RiprConfig) -> String {
    let mut out = String::new();
    let severity = config.severity().for_exposure(&finding.class).as_str();
    out.push_str(&format!(
        "{} {}:{}\n",
        severity.to_ascii_uppercase(),
        display_path(&finding.probe.location.file),
        finding.probe.location.line
    ));

    out.push_str("\nChanged\n");
    if let Some(before) = &finding.probe.before {
        out.push_str(&format!("  before: {before}\n"));
    }
    if let Some(after) = &finding.probe.after {
        out.push_str(&format!("  after:  {after}\n"));
    } else {
        out.push_str(&format!("  expr:   {}\n", finding.probe.expression));
    }

    out.push_str("\nProbe\n");
    out.push_str(&format!(
        "  family: {}\n  delta:  {}\n",
        finding.probe.family.as_str(),
        finding.probe.delta.as_str()
    ));
    if let Some(owner) = &finding.probe.owner {
        out.push_str(&format!("  owner:  {owner}\n"));
    }
    if let Some(gap) = &finding.canonical_gap {
        out.push_str(&format!("  canonical gap: {}\n", gap.id));
    }

    if should_render_language_metadata(finding) {
        out.push_str("\nLanguage\n");
        if let Some(language) = finding.language {
            out.push_str(&format!("  language: {}\n", language.as_str()));
        }
        if let Some(status) = finding.language_status {
            out.push_str(&format!("  status: {}\n", status.as_str()));
        }
        if let Some(owner_kind) = finding.owner_kind {
            out.push_str(&format!("  owner kind: {}\n", owner_kind.as_str()));
        }
    }

    if let Some(actionability) = preview_actionability_for(finding) {
        push_preview_actionability(&mut out, &actionability);
    }

    out.push_str("\nStatic exposure\n");
    out.push_str(&format!(
        "  {} ({}, confidence {:.2})\n",
        finding.class.as_str(),
        severity,
        finding.confidence
    ));

    out.push_str("\nEvidence\n");
    for line in evidence_path_lines(finding) {
        out.push_str(&format!("  - {line}\n"));
    }

    let weakness = weakness_lines(finding);
    if !weakness.is_empty() {
        out.push_str("\nWeakness\n");
        for line in weakness {
            out.push_str(&format!("  - {line}\n"));
        }
    }

    let stop_reasons = finding.effective_stop_reasons();
    if !stop_reasons.is_empty() {
        out.push_str("\nStop reasons:\n");
        for reason in &stop_reasons {
            out.push_str(&format!("  - {}\n", reason.as_str()));
        }
    }

    if let Some(card) = python_repair_card(finding) {
        push_python_repair_card(&mut out, &card);
    } else if let Some(card) = typescript_preview_card(finding) {
        push_typescript_preview_card(&mut out, &card);
        // §PR8 (RIPR-SPEC-0088): surface the full work-packet field-note when
        // actionable, or the named limitation when blocked. Emitted after the
        // preview card so it reads as a separate operator-facing section.
        push_typescript_repair_packet_field_note(&mut out, finding);
    } else if let Some(card) = perl_preview_card(finding) {
        push_perl_preview_card(&mut out, &card);
    } else if let Some(placement) = repair_placement_from_evidence(finding) {
        out.push_str("\nRepair placement\n");
        out.push_str(&format!("  suggested file: {}\n", placement.test_file));
        out.push_str(&format!("  suggested test: {}\n", placement.test_name));
        if let Some(node_id) = placement.test_node_id {
            out.push_str(&format!("  pytest node: {node_id}\n"));
        }
        out.push_str(&format!(
            "  verify: {} ({})\n",
            placement.verify_command, placement.verify_confidence
        ));
    }

    if finding.recommended_next_step.is_some() {
        out.push_str("\nNext step\n");
        out.push_str(&format!("  {}\n", reconcile_next_step(finding)));
    }

    out
}

fn push_preview_actionability(out: &mut String, actionability: &PreviewActionability) {
    out.push_str("\nPreview actionability\n");
    out.push_str(&format!(
        "  authority: {}\n",
        actionability.authority_boundary
    ));
    out.push_str(&format!("  gap state: {}\n", actionability.gap_state));
    out.push_str(&format!(
        "  category: {}\n",
        actionability.actionability_category
    ));
    out.push_str(&format!(
        "  repair packet ready: {}\n",
        actionability.repair_packet_ready
    ));
    // RIPR-SPEC-0088 §PR8: an actionable finding must not emit
    // "why not actionable" / "evidence needed" / blocked-case messaging — that
    // contradicts the complete packet. Relabel to "why actionable" and the
    // actual repair action, and drop the "evidence needed" line.
    if actionability.repair_packet_ready {
        out.push_str(&format!(
            "  why actionable: {}\n",
            actionability.why_not_actionable
        ));
        out.push_str(&format!(
            "  repair action: {}\n",
            actionability.repair_route
        ));
    } else {
        out.push_str(&format!(
            "  why not actionable: {}\n",
            actionability.why_not_actionable
        ));
        out.push_str(&format!("  repair route: {}\n", actionability.repair_route));
    }
    if !actionability.missing_actionability_fields.is_empty() {
        out.push_str(&format!(
            "  missing fields: {}\n",
            actionability.missing_actionability_fields.join(", ")
        ));
    }
    if !actionability.missing_graph_legs.is_empty() {
        out.push_str(&format!(
            "  missing graph legs: {}\n",
            actionability.missing_graph_legs.join(", ")
        ));
    }
    if let Some(unlock_condition) = &actionability.unlock_condition {
        out.push_str(&format!("  unlock condition: {unlock_condition}\n"));
    }
    if !actionability.repair_packet_ready && !actionability.evidence_needed_to_promote.is_empty() {
        out.push_str(&format!(
            "  evidence needed: {}\n",
            actionability.evidence_needed_to_promote
        ));
    }
    for raw_ref in &actionability.raw_evidence_refs {
        out.push_str("  raw evidence: ");
        push_raw_ref(out, raw_ref);
        out.push('\n');
    }
}

fn push_raw_ref(out: &mut String, raw_ref: &PreviewRawEvidenceRef) {
    if let (Some(file), Some(line)) = (raw_ref.file.as_deref(), raw_ref.line) {
        if let Some(leg) = &raw_ref.leg {
            out.push_str(&format!("{leg} "));
        }
        out.push_str(&format!("{file}:{line}"));
        if let Some(kind) = &raw_ref.kind {
            out.push_str(&format!(" ({kind})"));
        }
        if let Some(source_id) = &raw_ref.source_id {
            out.push_str(&format!(" source={source_id}"));
        }
        if let Some(owner) = &raw_ref.owner {
            out.push_str(&format!(" owner={owner}"));
        }
        if let Some(sample) = &raw_ref.sample {
            out.push_str(&format!(" sample={sample}"));
        }
    } else {
        out.push_str(&raw_ref.raw);
    }
}

fn push_python_repair_card(out: &mut String, card: &PythonRepairCard) {
    out.push_str("\nPython repair card (preview/advisory)\n");
    out.push_str(&format!("  card version: {}\n", card.card_version));
    out.push_str(&format!("  canonical gap: {}\n", card.canonical_gap_id));
    out.push_str(&format!(
        "  authority: {} ({}/{})\n",
        card.authority_boundary, card.language, card.language_status
    ));
    out.push_str(&format!("  repair action: {}\n", card.repair_action));
    out.push_str(&format!("  changed owner: {}\n", card.changed_owner));
    out.push_str(&format!("  changed behavior: {}\n", card.changed_behavior));
    out.push_str(&format!(
        "  current test evidence: {}\n",
        card.current_test_evidence
    ));
    out.push_str(&format!(
        "  missing discriminator: {}\n",
        card.missing_discriminator
    ));
    out.push_str(&format!(
        "  recommended test shape: {}\n",
        card.recommended_test_shape
    ));
    out.push_str(&format!(
        "  suggested assertion: {}\n",
        card.suggested_assertion
    ));
    out.push_str(&format!("  suggested file: {}\n", card.suggested_test_file));
    out.push_str(&format!("  suggested test: {}\n", card.suggested_test_name));
    if let Some(node_id) = &card.suggested_test_node_id {
        out.push_str(&format!("  pytest node: {node_id}\n"));
    }
    out.push_str(&format!(
        "  verify: {} ({})\n",
        card.verify_command, card.verify_command_confidence
    ));
    if let Some(command) = &card.receipt_command {
        out.push_str(&format!("  receipt command: {command}\n"));
    } else {
        out.push_str(&format!("  receipt: {}\n", card.receipt_status));
    }
    out.push_str(&format!("  receipt guidance: {}\n", card.receipt_guidance));
    out.push_str("  stop conditions:\n");
    for condition in &card.stop_conditions {
        out.push_str(&format!("    - {condition}\n"));
    }
    out.push_str("  limits:\n");
    for limit in &card.limits {
        out.push_str(&format!("    - {limit}\n"));
    }
}

fn push_typescript_preview_card(out: &mut String, card: &TypeScriptPreviewCard) {
    out.push_str("\nTypeScript preview card (advisory)\n");
    out.push_str(&format!("  card version: {}\n", card.card_version));
    out.push_str(&format!(
        "  authority: {} ({}/{})\n",
        card.authority_boundary, card.language, card.language_status
    ));
    out.push_str(&format!("  owner: {}\n", card.owner));
    if let Some(owner_kind) = &card.owner_kind {
        out.push_str(&format!("  owner kind: {owner_kind}\n"));
    }
    out.push_str(&format!("  changed behavior: {}\n", card.changed_behavior));
    if let Some(test) = &card.related_test {
        out.push_str(&format!(
            "  related test: {}:{} {}\n",
            test.file, test.line, test.name
        ));
    }
    out.push_str(&format!(
        "  oracle: {} ({})\n",
        card.oracle_kind, card.oracle_strength
    ));
    if let Some(grip) = &card.bun_cross_language_grip {
        out.push_str("  Bun cross-language grip:\n");
        out.push_str(&format!("    state: {}\n", grip.state));
        out.push_str(&format!(
            "    Rust seam: {} owner={} boundary={}\n",
            grip.rust_file, grip.rust_owner, grip.rust_boundary
        ));
        out.push_str(&format!(
            "    TypeScript evidence: {} verdict={} confidence={}\n",
            grip.ts_test_file, grip.ts_verdict, grip.bridge_confidence
        ));
        if !grip.missing_discriminators.is_empty() {
            out.push_str(&format!(
                "    missing discriminators: {}\n",
                grip.missing_discriminators.join(", ")
            ));
        }
        if !grip.missing_graph_legs.is_empty() {
            out.push_str(&format!(
                "    missing graph legs: {}\n",
                grip.missing_graph_legs.join(", ")
            ));
        }
        if let Some(unlock_condition) = &grip.unlock_condition {
            out.push_str(&format!("    unlock condition: {unlock_condition}\n"));
        }
        out.push_str(&format!(
            "    limitation category: {}\n",
            grip.limitation_category
        ));
        out.push_str(&format!("    repair route: {}\n", grip.repair_route));
        out.push_str(&format!("    action: {}\n", grip.action));
        out.push_str(&format!(
            "    suggested test file: {}\n",
            grip.suggested_test_file
        ));
        if let Some(placement) = &grip.placement {
            out.push_str(&format!(
                "    placement: rank {} {}\n",
                placement.rank, placement.suggested_test_file
            ));
            out.push_str(&format!("    placement reason: {}\n", placement.reason));
        }
        let proof_mode = stable_byte_proof_mode(grip);
        out.push_str(&format!("    proof mode: {}\n", proof_mode.mode));
        out.push_str(&format!("    proof mode reason: {}\n", proof_mode.reason));
        out.push_str(&format!(
            "    proof execution: runtime={} mutation={} miri={} proof_claim={}\n",
            proof_mode.runtime_execution,
            proof_mode.mutation_execution,
            proof_mode.miri_execution,
            proof_mode.proof_claim
        ));
        let advisory_packet = bun_cross_language_advisory_packet(grip);
        out.push_str("    advisory packet:\n");
        out.push_str(&format!(
            "      version: {}\n",
            advisory_packet.packet_version
        ));
        out.push_str(&format!(
            "      next action: {}\n",
            advisory_packet.next_action
        ));
        out.push_str(&format!(
            "      ts test file: {}\n",
            advisory_packet
                .ts_test_file
                .as_deref()
                .unwrap_or("not_applicable")
        ));
        out.push_str(&format!(
            "      suggested shape: {}\n",
            advisory_packet.suggested_shape
        ));
        out.push_str(&format!(
            "      stop condition: {}\n",
            advisory_packet.stop_condition
        ));
        out.push_str(&format!(
            "      must not change: {}\n",
            advisory_packet.must_not_change.join(", ")
        ));
        out.push_str(&format!(
            "      public repair packet: {}\n",
            advisory_packet.public_repair_packet
        ));
        out.push_str(&format!(
            "      repair packet ready: {}\n",
            advisory_packet.repair_packet_ready
        ));
        out.push_str(&format!("    authority: {}\n", grip.authority_boundary));
        out.push_str(&format!(
            "    repair packet ready: {}\n",
            grip.repair_packet_ready
        ));
    }
    if let Some(discriminator) = &card.missing_discriminator {
        out.push_str(&format!("  missing discriminator: {discriminator}\n"));
    }
    out.push_str(&format!(
        "  suggested assertion shape: {}\n",
        card.suggested_assertion_shape
    ));
    if !card.static_limits.is_empty() {
        out.push_str(&format!(
            "  static limits: {}\n",
            card.static_limits.join(", ")
        ));
    }
    if let Some(command) = &card.verify_command {
        out.push_str(&format!("  verify: {command}\n"));
    }
    out.push_str(&format!(
        "  repair packet ready: {}\n",
        card.repair_packet_ready
    ));
    // RIPR-SPEC-0088 §PR8: relabel for the actionable case so the card does not
    // emit blocked-case "why not actionable" / "only after available" text that
    // contradicts the complete packet.
    if card.repair_packet_ready {
        out.push_str(&format!("  why actionable: {}\n", card.why_not_actionable));
        out.push_str(&format!("  repair action: {}\n", card.repair_route));
    } else {
        out.push_str(&format!(
            "  why not actionable: {}\n",
            card.why_not_actionable
        ));
        out.push_str(&format!("  repair route: {}\n", card.repair_route));
    }
    out.push_str("  limits:\n");
    for limit in &card.limits {
        out.push_str(&format!("    - {limit}\n"));
    }
}

/// Render the TypeScript repair packet field-note when actionable, or a named
/// limitation when blocked. Called from `render_finding_with_config` after the
/// preview card when the finding is TypeScript/JavaScript preview.
///
/// RIPR-SPEC-0088 §PR8: the full work-packet is surfaced ONLY when
/// `repair_packet_ready: true`; blocked findings get the named limitation.
pub(crate) fn push_typescript_repair_packet_field_note(out: &mut String, finding: &Finding) {
    // Only emit for TypeScript/JavaScript preview findings.
    if !matches!(
        finding.language,
        Some(LanguageId::TypeScript | LanguageId::JavaScript)
    ) {
        return;
    }

    let maybe_record = typescript_gap_record_for(finding);

    match maybe_record {
        Some(record) => {
            // Actionable — render the full work-packet field-note.
            out.push_str("\nTypeScript repair packet (advisory)\n");
            out.push_str(&format!("  canonical gap: {}\n", record.canonical_gap_id));
            // Source / owner from anchor
            if let Some(anchor) = &record.anchor {
                let file = anchor.file.as_deref().unwrap_or("unknown");
                let line = anchor
                    .line
                    .map(|l| l.to_string())
                    .unwrap_or_else(|| "?".to_string());
                let owner = anchor.owner.as_deref().unwrap_or("unknown");
                out.push_str(&format!("  source: {owner} at {file}:{line}\n"));
            }
            // Related test + oracle from repair_route
            if let Some(route) = &record.repair_route {
                if let Some(related) = &route.related_test {
                    out.push_str(&format!("  related test: {related}\n"));
                }
                if let Some(shape) = &route.assertion_shape {
                    out.push_str(&format!("  oracle: {shape}\n"));
                }
                // Allowed edit surface via shared fn
                let edit_surface = allowed_edit_surface_for_gap_route(route);
                if !edit_surface.is_empty() {
                    out.push_str(&format!("  edit surface: {}\n", edit_surface.join(", ")));
                }
            }
            // Verify + receipt commands
            if let Some(verify) = record.verification_commands.first() {
                out.push_str(&format!("  verify: {verify}\n"));
            }
            if let Some(receipt) = record.receipt_command.as_deref() {
                out.push_str(&format!("  receipt: {receipt}\n"));
            }
            // must_not_change via shared fn
            let must_not_change = gap_record_packet_do_not_do(&record);
            if !must_not_change.is_empty() {
                out.push_str("  must not change:\n");
                for item in &must_not_change {
                    out.push_str(&format!("    - {item}\n"));
                }
            }
            out.push_str("  why actionable: complete-contract TypeScript finding — package root, runner, owner, oracle all resolved; no blocking limitation\n");
            out.push_str("  authority: preview_advisory_only\n");
        }
        None => {
            // Blocked — name the limitation, never emit a partial packet.
            // Only render this subsection if we have at least some preview
            // actionability data to surface (otherwise stay silent).
            let why = evidence_value(finding, "why_not_actionable: ")
                .or_else(|| evidence_value(finding, "gap_state: "))
                .unwrap_or("TypeScript preview; repair packet not yet complete");
            let next_capability = evidence_value(finding, "evidence_needed_to_promote: ")
                .unwrap_or("no further capability information");
            out.push_str("\nTypeScript repair packet (advisory)\n");
            out.push_str("  status: not actionable\n");
            out.push_str(&format!("  limitation: {why}\n"));
            out.push_str(&format!("  next capability needed: {next_capability}\n"));
        }
    }
}

fn push_perl_preview_card(out: &mut String, card: &PerlPreviewCard) {
    out.push_str("\nPerl preview card (advisory)\n");
    out.push_str(&format!("  card version: {}\n", card.card_version));
    out.push_str(&format!(
        "  authority: {} ({}/{})\n",
        card.authority_boundary, card.language, card.language_status
    ));
    out.push_str(&format!("  surface scope: {}\n", card.surface_scope));
    out.push_str(&format!(
        "  public projection ready: {}\n",
        card.public_projection_ready
    ));
    out.push_str(&format!(
        "  public repair packet: {}\n",
        card.public_repair_packet
    ));
    out.push_str(&format!(
        "  repair packet ready: {}\n",
        card.repair_packet_ready
    ));
    out.push_str(&format!(
        "  agent packet ready: {}\n",
        card.agent_packet_ready
    ));
    out.push_str(&format!("  gate candidate: {}\n", card.gate_candidate));
    out.push_str(&format!("  badge candidate: {}\n", card.badge_candidate));
    out.push_str(&format!(
        "  RIPR Zero candidate: {}\n",
        card.ripr_zero_candidate
    ));
    out.push_str(&format!("  packet id: {}\n", card.packet_id));
    out.push_str(&format!("  canonical gap: {}\n", card.canonical_gap_id));
    out.push_str(&format!("  gap state: {}\n", card.gap_state));
    out.push_str(&format!("  changed owner: {}\n", card.changed_owner));
    out.push_str(&format!("  evidence class: {}\n", card.evidence_class));
    out.push_str(&format!("  repair route: {}\n", card.repair_route));
    out.push_str(&format!(
        "  current test evidence: {}\n",
        card.current_test_evidence
    ));
    out.push_str(&format!(
        "  missing discriminator: {}\n",
        card.missing_discriminator
    ));
    out.push_str(&format!(
        "  target test shape: {}\n",
        card.target_test_shape
    ));
    out.push_str(&format!(
        "  suggested location: {}\n",
        card.suggested_test_location
    ));
    out.push_str(&format!(
        "  suggested assertion: {}\n",
        card.suggested_assertion
    ));
    out.push_str(&format!(
        "  verify: {} (fact_only_not_delegated)\n",
        card.verify_command
    ));
    out.push_str("  receipt: available_not_delegated\n");
    out.push_str(&format!("  confidence: {}\n", card.confidence));
    for raw_ref in &card.raw_evidence_refs {
        out.push_str("  raw evidence: ");
        push_perl_raw_ref(out, raw_ref);
        out.push('\n');
    }
    if !card.stop_if.is_empty() {
        out.push_str("  stop if:\n");
        for condition in &card.stop_if {
            out.push_str(&format!("    - {condition}\n"));
        }
    }
    if !card.must_not_change.is_empty() {
        out.push_str("  must not change:\n");
        for boundary in &card.must_not_change {
            out.push_str(&format!("    - {boundary}\n"));
        }
    }
    out.push_str("  limits:\n");
    for limit in &card.limits {
        out.push_str(&format!("    - {limit}\n"));
    }
}

fn push_perl_raw_ref(out: &mut String, raw_ref: &PerlRawEvidenceRef) {
    out.push_str(&format!(
        "{} {}:{}",
        raw_ref.leg, raw_ref.file, raw_ref.line
    ));
    out.push_str(&format!(" ({})", raw_ref.kind));
    out.push_str(&format!(" source={}", raw_ref.source_id));
    out.push_str(&format!(" owner={}", raw_ref.owner));
    if let Some(sample) = &raw_ref.sample {
        out.push_str(&format!(" sample={sample}"));
    }
}

struct RepairPlacement<'a> {
    test_file: &'a str,
    test_name: &'a str,
    test_node_id: Option<&'a str>,
    verify_command: &'a str,
    verify_confidence: &'a str,
}

fn repair_placement_from_evidence(finding: &Finding) -> Option<RepairPlacement<'_>> {
    Some(RepairPlacement {
        test_file: evidence_value(finding, "suggested_test_file: ")?,
        test_name: evidence_value(finding, "suggested_test_name: ")?,
        test_node_id: evidence_value(finding, "suggested_test_node_id: "),
        verify_command: evidence_value(finding, "suggested_verify_command: ")?,
        verify_confidence: evidence_value(finding, "suggested_verify_command_confidence: ")?,
    })
}

fn evidence_value<'a>(finding: &'a Finding, prefix: &str) -> Option<&'a str> {
    finding
        .evidence
        .iter()
        .find_map(|entry| entry.strip_prefix(prefix))
        .map(str::trim)
        .filter(|value| !value.is_empty())
}

fn should_render_language_metadata(finding: &Finding) -> bool {
    finding
        .language
        .is_some_and(|language| language != LanguageId::Rust)
        || finding
            .language_status
            .is_some_and(|status| status != LanguageStatus::Stable)
        || finding.owner_kind.is_some()
}