fallow-output 3.48.0

Output contract types for fallow reports
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
//! The `--lean` JSON profile.
//!
//! The pass runs on the serialized document, after every typed step, so the
//! default output and the typed API stay byte-identical. It acts only on the
//! envelopes in [`LEAN_PROFILE_KINDS`]. Other documents pass through unchanged.

use crate::lean_profile::{LEAN_PROFILE, LEAN_PROFILE_KINDS};
use serde_json::{Map, Value};

/// Default maximum rows per health table in a lean document.
pub const LEAN_TABLE_CAP: usize = 10;

/// Health tables that the lean profile caps. The engine sorts each one by
/// importance before output (hotspots by score, file scores worst first,
/// large functions by line count), so the first rows are the ones to keep.
const CAPPED_HEALTH_TABLES: [&str; 3] = ["hotspots", "file_scores", "large_functions"];

/// Action fields that hold text for a person or an agent to read.
const ACTION_TEXT_FIELDS: [&str; 3] = ["description", "note", "comment"];

/// A shared text must save at least one copy to be worth a `_meta` entry.
const MIN_SHARED_TEXT_USES: usize = 2;

/// Apply the lean profile to a serialized JSON document.
///
/// `table_cap` is the maximum number of rows per health table; `None` keeps
/// every row.
pub fn apply_lean_profile(document: &mut Value, table_cap: Option<usize>) {
    let Some(root) = document.as_object_mut() else {
        return;
    };
    let Some(kind) = root
        .get("kind")
        .and_then(Value::as_str)
        .filter(|kind| LEAN_PROFILE_KINDS.contains(kind))
        .map(str::to_owned)
    else {
        return;
    };
    match kind.as_str() {
        "dead-code" => trim_dead_code_body(root),
        "dead-code-grouped" => {
            drop_empty_arrays(root);
            for_each_group(root, drop_empty_arrays);
        }
        "health" => {
            trim_health_body(root, table_cap);
            for_each_group(root, |group| trim_health_body(group, table_cap));
        }
        "combined" => trim_combined(root, table_cap),
        // A kind in `LEAN_PROFILE_KINDS` without an arm here keeps its full
        // shape; `every_lean_kind_has_an_arm` fails first.
        _ => return,
    }
    group_workspace_diagnostics(root);
    move_shared_action_text(root);
    root.shift_insert(1, "profile".to_string(), Value::from(LEAN_PROFILE));
}

fn trim_dead_code_body(body: &mut Map<String, Value>) {
    drop_empty_arrays(body);
    if let Some(Value::Object(summary)) = body.get_mut("summary") {
        summary.retain(|_, count| !is_zero(count));
    }
}

fn trim_health_body(body: &mut Map<String, Value>, table_cap: Option<usize>) {
    if let Some(cap) = table_cap {
        for table in CAPPED_HEALTH_TABLES {
            cap_table(body, table, cap);
        }
    }
    drop_empty_arrays(body);
}

fn trim_combined(root: &mut Map<String, Value>, table_cap: Option<usize>) {
    drop_empty_arrays(root);
    if let Some(Value::Object(check)) = root.get_mut("check") {
        trim_dead_code_body(check);
        group_workspace_diagnostics(check);
    }
    if let Some(Value::Object(dupes)) = root.get_mut("dupes") {
        drop_empty_arrays(dupes);
        group_workspace_diagnostics(dupes);
    }
    if let Some(Value::Object(health)) = root.get_mut("health") {
        trim_health_body(health, table_cap);
        group_workspace_diagnostics(health);
    }
}

fn for_each_group(root: &mut Map<String, Value>, mut trim: impl FnMut(&mut Map<String, Value>)) {
    if let Some(Value::Array(groups)) = root.get_mut("groups") {
        for group in groups.iter_mut().filter_map(Value::as_object_mut) {
            trim(group);
        }
    }
}

fn drop_empty_arrays(object: &mut Map<String, Value>) {
    object.retain(|_, value| !matches!(value, Value::Array(items) if items.is_empty()));
}

fn is_zero(value: &Value) -> bool {
    value.as_f64() == Some(0.0)
}

/// Keep the first `cap` rows of `table` and write the number of dropped rows
/// to `<table>_omitted`, directly after the table.
fn cap_table(body: &mut Map<String, Value>, table: &str, cap: usize) {
    let Some(Value::Array(rows)) = body.get_mut(table) else {
        return;
    };
    if rows.len() <= cap {
        return;
    }
    let omitted = rows.len() - cap;
    rows.truncate(cap);
    let position = body
        .keys()
        .position(|key| key == table)
        .unwrap_or(body.len());
    body.shift_insert(
        position + 1,
        format!("{table}_omitted"),
        Value::from(omitted),
    );
}

/// Replace the diagnostics array with one entry per `kind`, in first-seen
/// order. Each entry is the first diagnostic of its kind plus a `count`, and
/// `degrades_analysis` is true when any diagnostic of the kind degrades it.
fn group_workspace_diagnostics(object: &mut Map<String, Value>) {
    let Some(Value::Array(entries)) = object.get_mut("workspace_diagnostics") else {
        return;
    };
    let mut groups: Vec<(Option<String>, Value, usize, bool)> = Vec::new();
    for entry in entries.drain(..) {
        let kind = entry.get("kind").and_then(Value::as_str).map(str::to_owned);
        let degrades = entry.get("degrades_analysis") == Some(&Value::Bool(true));
        // An entry without a kind cannot be grouped, so it stays on its own.
        let existing = kind.as_ref().and_then(|kind| {
            groups
                .iter_mut()
                .find(|(group_kind, ..)| group_kind.as_ref() == Some(kind))
        });
        match existing {
            Some((_, _, count, group_degrades)) => {
                *count += 1;
                *group_degrades |= degrades;
            }
            None => groups.push((kind, entry, 1, degrades)),
        }
    }
    for (kind, mut first, count, degrades) in groups {
        if let (Some(_), Value::Object(fields)) = (kind, &mut first) {
            if degrades {
                fields.insert("degrades_analysis".to_string(), Value::Bool(true));
            }
            // An absent count means 1, so a kind with one diagnostic costs
            // no more bytes than in the full document.
            if count > 1 {
                fields.insert("count".to_string(), Value::from(count));
            }
        }
        entries.push(first);
    }
}

/// How often one action type carries one text field, and with which values.
#[derive(Default)]
struct FieldTally {
    /// Actions of the type that have this field as text.
    present: usize,
    /// Each distinct text with its number of uses, in first-seen order.
    values: Vec<(String, usize)>,
}

struct TypeTally {
    action_type: String,
    /// All actions of the type.
    actions: usize,
    fields: [FieldTally; ACTION_TEXT_FIELDS.len()],
}

/// Move the most common text of each (action type, text field) pair to
/// `_meta.actions[type][field]` and remove it from the actions that carry it.
///
/// The move is lossless: `action.field ?? _meta.actions[type].field` gives
/// the original text. A field moves only when every action of the type has
/// it, because an action without the field would otherwise read the shared
/// text as its own.
fn move_shared_action_text(root: &mut Map<String, Value>) {
    let mut tallies: Vec<TypeTally> = Vec::new();
    for_each_action(root, &mut |action| tally_action(&mut tallies, action));

    let mut shared = Map::new();
    for tally in &tallies {
        let mut fields = Map::new();
        for (field, field_tally) in ACTION_TEXT_FIELDS.iter().zip(&tally.fields) {
            if field_tally.present != tally.actions {
                continue;
            }
            if let Some(text) = most_common(&field_tally.values) {
                fields.insert((*field).to_string(), Value::from(text));
            }
        }
        if !fields.is_empty() {
            shared.insert(tally.action_type.clone(), Value::Object(fields));
        }
    }
    if shared.is_empty() {
        return;
    }

    for_each_action(root, &mut |action| {
        let Some(action_type) = action.get("type").and_then(Value::as_str) else {
            return;
        };
        let Some(Value::Object(defaults)) = shared.get(action_type) else {
            return;
        };
        let moved: Vec<String> = defaults
            .iter()
            .filter(|(field, text)| action.get(field.as_str()) == Some(*text))
            .map(|(field, _)| field.clone())
            .collect();
        for field in moved {
            action.shift_remove(&field);
        }
    });

    let meta = root
        .entry("_meta")
        .or_insert_with(|| Value::Object(Map::new()));
    if let Value::Object(meta) = meta {
        meta.insert("actions".to_string(), Value::Object(shared));
    }
}

fn tally_action(tallies: &mut Vec<TypeTally>, action: &Map<String, Value>) {
    let Some(action_type) = action.get("type").and_then(Value::as_str) else {
        return;
    };
    let index = match tallies
        .iter()
        .position(|tally| tally.action_type == action_type)
    {
        Some(index) => index,
        None => {
            tallies.push(TypeTally {
                action_type: action_type.to_string(),
                actions: 0,
                fields: Default::default(),
            });
            tallies.len() - 1
        }
    };
    let tally = &mut tallies[index];
    tally.actions += 1;
    for (field, field_tally) in ACTION_TEXT_FIELDS.iter().zip(tally.fields.iter_mut()) {
        let Some(text) = action.get(*field).and_then(Value::as_str) else {
            continue;
        };
        field_tally.present += 1;
        match field_tally
            .values
            .iter_mut()
            .find(|(value, _)| value == text)
        {
            Some((_, uses)) => *uses += 1,
            None => field_tally.values.push((text.to_string(), 1)),
        }
    }
}

/// The text with the most uses; the first seen wins a tie.
fn most_common(values: &[(String, usize)]) -> Option<&str> {
    let mut best: Option<&(String, usize)> = None;
    for candidate in values {
        if best.is_none_or(|(_, uses)| candidate.1 > *uses) {
            best = Some(candidate);
        }
    }
    best.filter(|(_, uses)| *uses >= MIN_SHARED_TEXT_USES)
        .map(|(text, _)| text.as_str())
}

/// Visit every action object in every `actions` array, outside root `_meta`.
fn for_each_action(root: &mut Map<String, Value>, visit: &mut impl FnMut(&mut Map<String, Value>)) {
    for (key, value) in root.iter_mut() {
        if key != "_meta" {
            visit_actions(key, value, visit);
        }
    }
}

fn visit_actions(key: &str, value: &mut Value, visit: &mut impl FnMut(&mut Map<String, Value>)) {
    match value {
        Value::Array(items) if key == "actions" => {
            for action in items.iter_mut().filter_map(Value::as_object_mut) {
                visit(action);
            }
        }
        Value::Array(items) => {
            for item in items {
                visit_actions("", item, visit);
            }
        }
        Value::Object(map) => {
            for (child_key, child) in map.iter_mut() {
                visit_actions(child_key, child, visit);
            }
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn lean(mut document: Value, cap: Option<usize>) -> Value {
        apply_lean_profile(&mut document, cap);
        document
    }

    #[test]
    fn every_lean_kind_has_an_arm() {
        for kind in LEAN_PROFILE_KINDS {
            let document = lean(json!({ "kind": kind, "schema_version": 1 }), None);
            assert_eq!(document["profile"], LEAN_PROFILE, "kind {kind}");
        }
    }

    #[test]
    fn lean_kinds_match_the_schema_enum() {
        let kinds: Vec<&str> = LEAN_PROFILE_KINDS.to_vec();
        let schema_kinds: Vec<String> = [
            crate::LeanOutputKind::DeadCode,
            crate::LeanOutputKind::DeadCodeGrouped,
            crate::LeanOutputKind::Health,
            crate::LeanOutputKind::Combined,
        ]
        .iter()
        .map(|kind| {
            serde_json::to_value(kind)
                .expect("a lean kind serializes")
                .as_str()
                .expect("a lean kind is a string")
                .to_owned()
        })
        .collect();
        assert_eq!(kinds, schema_kinds);
    }

    #[test]
    fn other_kinds_stay_unchanged() {
        let document = json!({
            "kind": "dupes",
            "schema_version": 3,
            "clone_groups": [],
            "stats": { "clone_groups": 0 }
        });
        assert_eq!(lean(document.clone(), Some(LEAN_TABLE_CAP)), document);
        let no_kind = json!({ "error": true, "items": [] });
        assert_eq!(lean(no_kind.clone(), Some(LEAN_TABLE_CAP)), no_kind);
    }

    #[test]
    fn dead_code_drops_empty_arrays_and_zero_summary_counts() {
        let document = lean(
            json!({
                "kind": "dead-code",
                "schema_version": 7,
                "version": "3.0.0",
                "elapsed_ms": 0,
                "total_issues": 0,
                "summary": { "total_issues": 0, "unused_files": 0, "unused_exports": 2 },
                "unused_files": [],
                "unused_exports": [{ "name": "a" }, { "name": "b" }],
                "entry_points": { "total": 0 }
            }),
            Some(LEAN_TABLE_CAP),
        );
        assert_eq!(
            document,
            json!({
                "kind": "dead-code",
                "profile": "lean",
                "schema_version": 7,
                "version": "3.0.0",
                "elapsed_ms": 0,
                "total_issues": 0,
                "summary": { "unused_exports": 2 },
                "unused_exports": [{ "name": "a" }, { "name": "b" }],
                "entry_points": { "total": 0 }
            })
        );
    }

    #[test]
    fn grouped_dead_code_drops_empty_arrays_in_each_group() {
        let document = lean(
            json!({
                "kind": "dead-code-grouped",
                "schema_version": 7,
                "total_issues": 1,
                "groups": [
                    { "key": "src", "total_issues": 1, "unused_files": [{ "path": "a" }], "unused_exports": [] },
                    { "key": "lib", "total_issues": 0, "unused_files": [], "unused_exports": [] }
                ]
            }),
            Some(LEAN_TABLE_CAP),
        );
        assert_eq!(
            document["groups"],
            json!([
                { "key": "src", "total_issues": 1, "unused_files": [{ "path": "a" }] },
                { "key": "lib", "total_issues": 0 }
            ])
        );
    }

    #[test]
    fn workspace_diagnostics_group_by_kind_in_first_seen_order() {
        let document = lean(
            json!({
                "kind": "dead-code",
                "schema_version": 7,
                "workspace_diagnostics": [
                    { "path": "a", "kind": "redundant", "message": "first", "pattern": "x" },
                    { "path": ".", "kind": "missing", "message": "m", "degrades_analysis": true },
                    { "path": "b", "kind": "redundant", "message": "second", "degrades_analysis": true },
                    { "path": "c", "kind": "redundant", "message": "third" }
                ]
            }),
            Some(LEAN_TABLE_CAP),
        );
        assert_eq!(
            document["workspace_diagnostics"],
            json!([
                { "path": "a", "kind": "redundant", "message": "first", "pattern": "x", "degrades_analysis": true, "count": 3 },
                { "path": ".", "kind": "missing", "message": "m", "degrades_analysis": true }
            ])
        );
    }

    #[test]
    fn health_tables_keep_the_first_rows_and_count_the_rest() {
        let rows = |count: usize| -> Value {
            Value::Array((0..count).map(|index| json!({ "row": index })).collect())
        };
        let document = lean(
            json!({
                "kind": "health",
                "schema_version": 12,
                "findings": rows(4),
                "file_scores": rows(5),
                "hotspots": rows(2),
                "large_functions": rows(3),
                "targets": rows(5),
                "groups": [{ "key": "src", "file_scores": rows(4), "findings": [] }]
            }),
            Some(3),
        );
        assert_eq!(document["findings"], rows(4), "findings stay complete");
        assert_eq!(
            document["targets"],
            rows(5),
            "targets are not a capped table"
        );
        assert_eq!(document["file_scores"], rows(3));
        assert_eq!(document["file_scores_omitted"], 2);
        assert_eq!(document["hotspots"], rows(2));
        assert!(document.get("hotspots_omitted").is_none());
        assert_eq!(document["large_functions"], rows(3));
        assert!(document.get("large_functions_omitted").is_none());
        assert_eq!(
            document["groups"],
            json!([{ "key": "src", "file_scores": rows(3), "file_scores_omitted": 1 }])
        );
        let keys: Vec<&String> = document.as_object().expect("object").keys().collect();
        let scores = keys.iter().position(|key| *key == "file_scores");
        let omitted = keys.iter().position(|key| *key == "file_scores_omitted");
        assert_eq!(
            omitted,
            scores.map(|index| index + 1),
            "the omitted count follows its table"
        );
    }

    #[test]
    fn health_tables_stay_whole_without_a_cap() {
        let rows = Value::Array((0..20).map(|index| json!({ "row": index })).collect());
        let document = lean(
            json!({ "kind": "health", "schema_version": 12, "hotspots": rows }),
            None,
        );
        assert_eq!(document["hotspots"], rows);
        assert!(document.get("hotspots_omitted").is_none());
    }

    #[test]
    fn combined_trims_each_section() {
        let rows = Value::Array((0..12).map(|index| json!({ "row": index })).collect());
        let document = lean(
            json!({
                "kind": "combined",
                "schema_version": 1,
                "_meta": { "telemetry": { "analysis_run_id": "run_1" } },
                "check": {
                    "total_issues": 0,
                    "summary": { "unused_files": 0, "unused_exports": 0 },
                    "unused_files": []
                },
                "dupes": { "clone_groups": [], "stats": { "clone_groups": 0 } },
                "health": { "findings": [], "summary": { "files_analyzed": 0 }, "hotspots": rows },
                "workspace_diagnostics": [
                    { "path": ".", "kind": "missing", "message": "m" },
                    { "path": ".", "kind": "missing", "message": "n" }
                ]
            }),
            Some(10),
        );
        assert_eq!(document["profile"], "lean");
        assert_eq!(
            document["check"],
            json!({ "total_issues": 0, "summary": {} })
        );
        assert_eq!(document["dupes"], json!({ "stats": { "clone_groups": 0 } }));
        assert_eq!(
            document["health"]["summary"],
            json!({ "files_analyzed": 0 }),
            "health summary zeros are real values"
        );
        assert!(document["health"].get("findings").is_none());
        assert_eq!(
            document["health"]["hotspots"].as_array().map(Vec::len),
            Some(10)
        );
        assert_eq!(document["health"]["hotspots_omitted"], 2);
        assert_eq!(
            document["workspace_diagnostics"],
            json!([{ "path": ".", "kind": "missing", "message": "m", "count": 2 }])
        );
        assert_eq!(
            document["_meta"]["telemetry"],
            json!({ "analysis_run_id": "run_1" }),
            "existing _meta keys stay"
        );
    }

    #[test]
    fn repeated_action_text_moves_to_meta_actions() {
        let document = lean(
            json!({
                "kind": "dead-code",
                "schema_version": 7,
                "unused_exports": [
                    { "name": "a", "actions": [
                        { "type": "remove-export", "auto_fixable": true, "description": "Remove it" },
                        { "type": "suppress-line", "auto_fixable": false, "description": "Suppress", "comment": "// x" }
                    ] },
                    { "name": "b", "actions": [
                        { "type": "remove-export", "auto_fixable": true, "description": "Remove it" },
                        { "type": "suppress-line", "auto_fixable": false, "description": "Suppress", "comment": "// x" }
                    ] },
                    { "name": "c", "actions": [
                        { "type": "remove-export", "auto_fixable": true, "description": "Other text" },
                        { "type": "suppress-line", "auto_fixable": false, "description": "Unique", "comment": "// y" }
                    ] }
                ]
            }),
            Some(LEAN_TABLE_CAP),
        );
        assert_eq!(
            document["_meta"]["actions"],
            json!({
                "remove-export": { "description": "Remove it" },
                "suppress-line": { "description": "Suppress", "comment": "// x" }
            })
        );
        assert_eq!(
            document["unused_exports"][0]["actions"],
            json!([
                { "type": "remove-export", "auto_fixable": true },
                { "type": "suppress-line", "auto_fixable": false }
            ])
        );
        assert_eq!(
            document["unused_exports"][2]["actions"],
            json!([
                { "type": "remove-export", "auto_fixable": true, "description": "Other text" },
                { "type": "suppress-line", "auto_fixable": false, "description": "Unique", "comment": "// y" }
            ]),
            "a different text stays on its action"
        );
    }

    #[test]
    fn action_text_stays_when_moving_it_would_lose_data() {
        let document = lean(
            json!({
                "kind": "health",
                "schema_version": 12,
                "findings": [
                    { "actions": [{ "type": "refactor-function", "description": "Refactor a", "note": "Split" }] },
                    { "actions": [{ "type": "refactor-function", "description": "Refactor b", "note": "Split" }] },
                    { "actions": [{ "type": "refactor-function", "description": "Refactor c" }] }
                ]
            }),
            Some(LEAN_TABLE_CAP),
        );
        // One action has no note. A shared note would give it one, so the
        // note stays on each action. Each description occurs once, so no
        // description moves either.
        assert!(document.get("_meta").is_none(), "{document}");
        assert_eq!(
            document["findings"][2]["actions"][0],
            json!({ "type": "refactor-function", "description": "Refactor c" })
        );
    }

    #[test]
    fn meta_actions_ignores_text_that_occurs_once() {
        let document = lean(
            json!({
                "kind": "dead-code",
                "schema_version": 7,
                "unused_files": [{ "actions": [{ "type": "delete-file", "auto_fixable": false, "description": "Delete" }] }]
            }),
            Some(LEAN_TABLE_CAP),
        );
        assert!(document.get("_meta").is_none());
        assert_eq!(
            document["unused_files"][0]["actions"][0]["description"],
            "Delete"
        );
    }
}