git-slop 0.11.4

A deterministic repository token-defragmenter for humans and AI agents.
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
use super::*;

fn plan_candidates(report: &Value, context: &Value) -> Vec<Value> {
    let mut slices = vec![anchor_plan_slice(report, context)];
    let selector_kind = string(value_at(context, &["selector", "kind"]));
    let record_type = string(value_at(context, &["target", "record_type"]));
    match selector_kind.as_str() {
        "path" => {
            let relationship_limit = if record_type == "folder" {
                usize::MAX
            } else {
                3
            };
            for relationship in array_at(context, &["supporting_relationships"])
                .iter()
                .take(relationship_limit)
            {
                if let Some(slice) = relationship_plan_slice(report, context, relationship) {
                    slices.push(slice);
                }
            }
            let cluster_limit = if record_type == "folder" {
                usize::MAX
            } else {
                2
            };
            let mut emitted = 0;
            for cluster in array_at(context, &["supporting_clusters"]) {
                if let Some(slice) = cluster_plan_slice(report, context, cluster) {
                    slices.push(slice);
                    emitted += 1;
                    if emitted >= cluster_limit {
                        break;
                    }
                }
            }
        }
        "cluster" => {
            let mut emitted = 0;
            for relationship in array_at(context, &["supporting_relationships"]) {
                if let Some(slice) = relationship_plan_slice(report, context, relationship) {
                    slices.push(slice);
                    emitted += 1;
                    if emitted >= 2 {
                        break;
                    }
                }
            }
        }
        _ => {
            let mut emitted = 0;
            for cluster in array_at(context, &["supporting_clusters"]) {
                if let Some(slice) = cluster_plan_slice(report, context, cluster) {
                    slices.push(slice);
                    emitted += 1;
                    if emitted >= 2 {
                        break;
                    }
                }
            }
        }
    }
    slices
}

fn merge_plan_slices(slices: Vec<Value>) -> Vec<Value> {
    let mut merged: Vec<Value> = Vec::new();
    for mut slice in slices {
        let scope = string_array(slice.get("scope_paths"));
        if let Some(existing) = merged
            .iter_mut()
            .find(|existing| string_array(existing.get("scope_paths")) == scope)
        {
            for key in [
                "out_of_scope_paths",
                "supporting_relationship_ids",
                "supporting_cluster_ids",
            ] {
                let values = unique_strings(
                    string_array(existing.get(key))
                        .into_iter()
                        .chain(string_array(slice.get(key))),
                );
                existing
                    .as_object_mut()
                    .expect("slice object")
                    .insert(key.to_string(), json!(values));
            }
            if usize_value(slice.get("_selector_class"))
                < usize_value(existing.get("_selector_class"))
            {
                for key in [
                    "_selector_class",
                    "id",
                    "title",
                    "why_this_slice",
                    "ranking_reason",
                ] {
                    existing.as_object_mut().expect("slice object").insert(
                        key.to_string(),
                        slice
                            .as_object_mut()
                            .expect("slice object")
                            .remove(key)
                            .unwrap_or(Value::Null),
                    );
                }
            }
        } else {
            merged.push(slice);
        }
    }
    merged
}

fn plan_top_score_sum(report: &Value, slice: &Value) -> f64 {
    let mut scores: Vec<f64> = string_array(slice.get("scope_paths"))
        .iter()
        .map(|path| record_slop_score(report, path))
        .collect();
    scores.sort_by(|left, right| cmp_f64_desc(*left, *right));
    scores.into_iter().take(3).sum()
}

fn rank_plan_slices(report: &Value, context: &Value, mut slices: Vec<Value>) -> Vec<Value> {
    let path_selector = string(value_at(context, &["selector", "kind"])) == "path";
    slices.sort_by(|left, right| {
        if path_selector {
            let left_anchor = usize_value(left.get("_selector_class")) != 0;
            let right_anchor = usize_value(right.get("_selector_class")) != 0;
            left_anchor
                .cmp(&right_anchor)
                .then_with(|| {
                    cmp_f64_desc(
                        plan_top_score_sum(report, left),
                        plan_top_score_sum(report, right),
                    )
                })
                .then_with(|| {
                    string_array(left.get("out_of_scope_paths"))
                        .len()
                        .cmp(&string_array(right.get("out_of_scope_paths")).len())
                })
                .then_with(|| {
                    string_array(left.get("scope_paths"))
                        .cmp(&string_array(right.get("scope_paths")))
                })
        } else {
            usize_value(left.get("_selector_class"))
                .cmp(&usize_value(right.get("_selector_class")))
                .then_with(|| {
                    string_array(right.get("supporting_relationship_ids"))
                        .len()
                        .cmp(&string_array(left.get("supporting_relationship_ids")).len())
                })
                .then_with(|| {
                    string_array(right.get("supporting_cluster_ids"))
                        .len()
                        .cmp(&string_array(left.get("supporting_cluster_ids")).len())
                })
                .then_with(|| {
                    string_array(left.get("out_of_scope_paths"))
                        .len()
                        .cmp(&string_array(right.get("out_of_scope_paths")).len())
                })
                .then_with(|| {
                    cmp_f64_desc(
                        plan_top_score_sum(report, left),
                        plan_top_score_sum(report, right),
                    )
                })
                .then_with(|| {
                    string_array(left.get("scope_paths"))
                        .cmp(&string_array(right.get("scope_paths")))
                })
        }
    });
    let mut kept: Vec<Value> = Vec::new();
    for slice in slices {
        let scope: BTreeSet<String> = string_array(slice.get("scope_paths")).into_iter().collect();
        let relationships: BTreeSet<String> =
            string_array(slice.get("supporting_relationship_ids"))
                .into_iter()
                .collect();
        let clusters: BTreeSet<String> = string_array(slice.get("supporting_cluster_ids"))
            .into_iter()
            .collect();
        let suppress = kept.iter().any(|existing| {
            let existing_scope: BTreeSet<String> = string_array(existing.get("scope_paths"))
                .into_iter()
                .collect();
            scope.is_subset(&existing_scope)
                && scope != existing_scope
                && relationships.is_subset(
                    &string_array(existing.get("supporting_relationship_ids"))
                        .into_iter()
                        .collect(),
                )
                && clusters.is_subset(
                    &string_array(existing.get("supporting_cluster_ids"))
                        .into_iter()
                        .collect(),
                )
        });
        if !suppress {
            kept.push(slice);
        }
    }
    kept
}

fn plan_evidence_summary(slice: &Value) -> String {
    let relationships = string_array(slice.get("supporting_relationship_ids"));
    let clusters = string_array(slice.get("supporting_cluster_ids"));
    let mut parts = Vec::new();
    if !relationships.is_empty() {
        parts.push(format!("{} relationship(s)", relationships.len()));
    }
    if !clusters.is_empty() {
        parts.push(format!("{} cluster(s)", clusters.len()));
    }
    if parts.is_empty() {
        parts.push("anchor detector evidence".to_string());
    }
    format!(
        "{} Evidence: {}. Scope: {}.",
        string(slice.get("why_this_slice")),
        parts.join(", "),
        {
            let scope = string_array(slice.get("scope_paths"));
            if scope.is_empty() {
                "none".to_string()
            } else {
                scope.join(", ")
            }
        },
    )
}

fn enrich_plan_slice(report: &Value, context: &Value, mut slice: Value) -> Value {
    slice
        .as_object_mut()
        .expect("slice object")
        .remove("_selector_class");
    let evidence_summary = plan_evidence_summary(&slice);
    let rationale = string(slice.get("why_this_slice"));
    let scope_paths = string_array(slice.get("scope_paths"));
    let scope_path_count = scope_paths.len();
    let out_of_scope_paths = string_array(slice.get("out_of_scope_paths"));
    let relationship_ids = string_array(slice.get("supporting_relationship_ids"));
    let cluster_ids = string_array(slice.get("supporting_cluster_ids"));
    let baseline_command = "git-slop baseline create --name plan";
    let rerun_command = "git-slop find && git-slop compare --baseline plan --head .slop/latest/report.json --detail summary --fail-on-regression";
    let top_score = string_array(slice.get("scope_paths"))
        .iter()
        .map(|path| record_slop_score(report, path))
        .fold(0.0, f64::max);
    let priority = if top_score >= 75.0 {
        "Now"
    } else if top_score >= 40.0 {
        "Next"
    } else {
        "Later"
    };
    let target_score = (top_score - (top_score * 0.05).max(1.0)).max(0.0);
    let plan_type = if top_score >= 40.0 || !relationship_ids.is_empty() {
        "intervention"
    } else {
        "investigation"
    };
    let relationship_labels = relationship_ids
        .iter()
        .filter_map(|id| relationship_by_id(report, id))
        .map(|relationship| {
            json!({
                "id": relationship.get("id").cloned().unwrap_or(Value::Null),
                "kind": relationship.get("kind").cloned().unwrap_or(Value::Null),
                "paths": [
                    relationship.get("source_path").cloned().unwrap_or(Value::Null),
                    relationship.get("target_path").cloned().unwrap_or(Value::Null),
                ],
            })
        })
        .collect::<Vec<_>>();
    let repository_paths = array_at(report, &["files"])
        .iter()
        .filter_map(|record| record.get("path").and_then(Value::as_str))
        .map(str::to_owned)
        .collect::<BTreeSet<_>>();
    let configured_commands = string_array(report.pointer("/config/verification/commands"));
    let verification_commands = super::super::verification::from_report_paths(
        &repository_paths,
        &scope_paths,
        &configured_commands,
    );
    let verification_classes = scope_paths
        .iter()
        .filter_map(|path| {
            array_at(report, &["files"])
                .iter()
                .find(|record| record.get("path").and_then(Value::as_str) == Some(path))
        })
        .filter_map(|record| record.get("classification").and_then(Value::as_str))
        .map(|classification| match classification {
            "docs" => "documentation",
            "config" => "configuration",
            "workflow" => "workflow",
            "tool" => "workflow_or_tooling",
            "test" => "test",
            _ => "source",
        })
        .collect::<BTreeSet<_>>();
    let backlog = json!({
        "mutation_policy": "preview_only",
        "proposed_issue_title": format!("Maintenance: {}", string(slice.get("title"))),
        "issue_type": "maintenance",
        "suggested_labels": ["maintenance"],
        "priority_hint": priority,
        "evidence_summary": evidence_summary,
        "acceptance_criteria": [
            format!("Change no more than {scope_path_count} scoped paths unless the plan is regenerated."),
            format!("Reduce the highest scoped slop score from {top_score:.3} to at most {target_score:.3}."),
            "Produce zero native compare regressions and pass every discovered verification command.",
        ],
        "source": {
            "command": "git slop plan",
            "selector": context.get("selector").cloned().unwrap_or(Value::Null),
            "report_schema_version": report.get("schema_version").cloned().unwrap_or(Value::Null),
        },
    });
    let object = slice.as_object_mut().expect("slice object");
    object.remove("why_this_slice");
    object.insert(
        "objective".to_string(),
        json!(format!(
            "Reduce the highest scoped slop score from {top_score:.3} to at most {target_score:.3} across {}, introduce zero native compare regressions, and pass every discovered verification command without expanding the reviewed scope.",
            render_limited(&scope_paths, 5)
        )),
    );
    object.insert("plan_type".to_string(), json!(plan_type));
    object.insert("rationale".to_string(), json!(rationale));
    object.insert(
        "evidence".to_string(),
        json!({
            "summary": evidence_summary,
            "relationship_ids": relationship_ids,
            "relationships": relationship_labels,
            "cluster_ids": cluster_ids,
        }),
    );
    object.insert(
        "assumptions".to_string(),
        json!([
            "The cited detector report is the source of truth for scope and ranking.",
            "A human reviews the proposed slice before any repository mutation.",
            if verification_commands.is_empty() { "No repository-native verification command was discoverable from tracked manifest paths." } else { "Discovered verification commands are inferred from tracked manifest paths and must be reviewed before execution." },
        ]),
    );
    object.insert(
        "boundaries".to_string(),
        json!({"in_scope": scope_paths, "out_of_scope": out_of_scope_paths}),
    );
    object.insert(
        "verification".to_string(),
        json!({
            "classes": verification_classes,
            "discovered_commands": verification_commands,
            "required_checks": [
                "Run focused tests for every changed path.",
                "Rerun git-slop and compare the new report with the saved baseline.",
                "Confirm no unrelated finding or public contract regressed.",
            ]
        }),
    );
    object.insert(
        "expected_outcome".to_string(),
        json!({
            "maximum_scope_paths": scope_path_count,
            "baseline_top_slop_score": top_score,
            "target_top_slop_score": target_score,
            "required": [
                "No new native compare regression.",
                format!("Highest scoped slop score is at most {target_score:.3}."),
                "All reviewed verification commands pass."
            ]
        }),
    );
    object.insert("baseline_command".to_string(), json!(baseline_command));
    object.insert("rerun_command".to_string(), json!(rerun_command));
    object.insert(
        "abandonment_condition".to_string(),
        json!("Stop and abandon or re-scope this slice if preserving behavior requires an out-of-scope change, verification cannot be identified, or the native comparison reports a regression."),
    );
    object.insert(
        "rollback".to_string(),
        json!("Revert only the explicitly reviewed code changes; report and prompt artifacts are advisory and can be regenerated."),
    );
    object.insert("backlog_handoff".to_string(), backlog);
    slice
}

pub fn plan_payload(report: &Value, selector: PlanSelector, max_slices: usize) -> Result<Value> {
    require_report_schema(report, "plan")?;
    if max_slices == 0 {
        bail!("--max-slices must be greater than zero.");
    }
    let context = match selector {
        PlanSelector::Path(path) => plan_context_for_path(report, &path)?,
        PlanSelector::Cluster(id) => plan_context_for_cluster(report, &id)?,
        PlanSelector::Relationship(id) => plan_context_for_relationship(report, &id)?,
    };
    let candidates = plan_candidates(report, &context);
    let merged = merge_plan_slices(candidates);
    let ranked = rank_plan_slices(report, &context, merged);
    let slices: Vec<Value> = ranked
        .into_iter()
        .take(max_slices)
        .map(|slice| enrich_plan_slice(report, &context, slice))
        .collect();
    let selector_kind = string(value_at(&context, &["selector", "kind"]));
    Ok(json!({
        "schema_version": PLAN_SCHEMA_VERSION,
        "report_schema_version": report.get("schema_version").cloned().unwrap_or(Value::Null),
        "command": "plan",
        "selector": context.get("selector").cloned().unwrap_or(Value::Null),
        "target": context.get("target").cloned().unwrap_or(Value::Null),
        "proposed_slices": slices,
        "ranking_basis": {
            "anchor_first": true,
            "relationship_slices_before_cluster_slices": selector_kind != "path",
            "max_slice_files": MAX_SLICE_FILES,
            "secondary_sort": if selector_kind == "path" {
                "top-three-slop-score-sum, out-of-scope-count, path"
            } else {
                "relationship-count, cluster-count, out-of-scope-count, top-three-slop-score-sum, path"
            },
        },
        "backlog_handoff": {
            "mutation_policy": "preview_only",
            "candidate_count": slices.len(),
            "source_selector": context.get("selector").cloned().unwrap_or(Value::Null),
            "canonical_format": "provider_neutral_maintenance_plan",
            "optional_adapters": ["github_issues", "linear", "project_management_plugin"],
        },
        "boundary_note": PLAN_BOUNDARY_NOTE,
    }))
}

fn render_limited(values: &[String], limit: usize) -> String {
    if values.is_empty() {
        return "none".to_string();
    }
    let preview = values.iter().take(limit).cloned().collect::<Vec<_>>();
    let rendered = preview.join(", ");
    if values.len() > preview.len() {
        format!("{rendered} (+{} more)", values.len() - preview.len())
    } else {
        rendered
    }
}

pub fn render_plan_text(payload: &Value) -> String {
    let safe = |value: Option<&Value>| crate::text::visible_controls(&string(value));
    let safe_array = |value: Option<&Value>| {
        string_array(value)
            .into_iter()
            .map(|value| crate::text::visible_controls(&value))
            .collect::<Vec<_>>()
    };
    let target = payload.get("target").unwrap_or(&Value::Null);
    let header = match string(target.get("kind")).as_str() {
        "path" => format!(
            "Plan: path {} [{}]",
            safe(target.get("path")),
            safe(target.get("record_type"))
        ),
        "cluster" => format!(
            "Plan: cluster {} [{}]",
            safe(target.get("id")),
            safe(target.get("cluster_kind"))
        ),
        _ => format!(
            "Plan: relationship {} [{}]",
            safe(target.get("id")),
            safe(target.get("relationship_kind"))
        ),
    };
    let mut lines = vec![header];
    for (index, slice) in array_at(payload, &["proposed_slices"]).iter().enumerate() {
        lines.extend([
            String::new(),
            format!("{}. {}", index + 1, safe(slice.get("title"))),
            format!(
                "   scope: {}",
                render_limited(&safe_array(slice.get("scope_paths")), usize::MAX)
            ),
            format!("   objective: {}", safe(slice.get("objective"))),
            format!("   plan_type: {}", safe(slice.get("plan_type"))),
            format!("   rationale: {}", safe(slice.get("rationale"))),
            format!(
                "   evidence: {}; relationships={}",
                safe(value_at(slice, &["evidence", "summary"])),
                array_at(slice, &["evidence", "relationships"])
                    .iter()
                    .map(|relationship| format!(
                        "{}({})",
                        safe(relationship.get("id")),
                        render_limited(&safe_array(relationship.get("paths")), 2)
                    ))
                    .collect::<Vec<_>>()
                    .join(", ")
            ),
            format!(
                "   expected_outcome: highest scoped score is at most {}; no native compare regression; verification passes",
                json_scalar_text(value_at(slice, &["expected_outcome", "target_top_slop_score"]))
            ),
            format!(
                "   verification: {}",
                render_limited(
                    &safe_array(value_at(slice, &["verification", "discovered_commands"])),
                    5
                )
            ),
            format!("   rerun: {}", safe(slice.get("rerun_command"))),
            format!(
                "   abandon_if: {}",
                safe(slice.get("abandonment_condition"))
            ),
            format!("   rollback: {}", safe(slice.get("rollback"))),
            format!(
                "   backlog: {} priority={} policy=preview_only",
                safe(value_at(
                    slice,
                    &["backlog_handoff", "proposed_issue_title"]
                )),
                safe(value_at(slice, &["backlog_handoff", "priority_hint"])),
            ),
            format!(
                "   out_of_scope: {}",
                render_limited(&safe_array(slice.get("out_of_scope_paths")), 5)
            ),
        ]);
    }
    lines.extend([String::new(), safe(payload.get("boundary_note"))]);
    lines.join("\n")
}