git-slop 0.15.0

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
fn timestamp_slug(generated_at: &str) -> String {
    if let Ok(timestamp) = DateTime::parse_from_rfc3339(generated_at) {
        return timestamp
            .with_timezone(&Utc)
            .format("%Y%m%dT%H%M%SZ")
            .to_string();
    }
    let normalized = generated_at
        .chars()
        .map(|character| {
            if character.is_ascii_alphanumeric() {
                character
            } else {
                '-'
            }
        })
        .collect::<String>();
    let normalized = normalized.trim_matches('-');
    if normalized.is_empty() {
        Utc::now().format("%Y%m%dT%H%M%SZ").to_string()
    } else {
        normalized.to_string()
    }
}

fn temporary_directory(parent: &Path, label: &str) -> PathBuf {
    let sequence = TEMP_SEQUENCE.fetch_add(1, AtomicOrdering::Relaxed);
    parent.join(format!(".{label}-{}-{sequence}.tmp", std::process::id()))
}

fn unique_run_root(runs_root: &Path, preferred_slug: &str) -> PathBuf {
    let preferred = runs_root.join(preferred_slug);
    if !preferred.exists() {
        return preferred;
    }
    for suffix in 2..10_000 {
        let candidate = runs_root.join(format!("{preferred_slug}-{suffix}"));
        if !candidate.exists() {
            return candidate;
        }
    }
    runs_root.join(format!(
        "{preferred_slug}-{}",
        TEMP_SEQUENCE.fetch_add(1, AtomicOrdering::Relaxed)
    ))
}

fn write_bundle_files(
    root: &Path,
    report_json: &str,
    report_yaml: Option<&str>,
    summary: &str,
    health: &str,
    compressed: Option<(&str, &[u8])>,
) -> Result<()> {
    fs::create_dir_all(root)
        .with_context(|| format!("failed to create report directory {}", root.display()))?;
    let mut files = vec![
        ("report.json", report_json),
        ("summary.md", summary),
        ("health.md", health),
    ];
    if let Some(report_yaml) = report_yaml {
        files.push(("report.yaml", report_yaml));
    }
    for (name, content) in files {
        let path = root.join(name);
        fs::write(&path, content).with_context(|| format!("failed to write {}", path.display()))?;
    }
    if let Some((name, bytes)) = compressed {
        let path = root.join(name);
        fs::write(&path, bytes).with_context(|| format!("failed to write {}", path.display()))?;
    }
    Ok(())
}

fn replace_latest_from_run(
    latest: &Path,
    run_root: &Path,
    yaml_enabled: bool,
    compressed_name: Option<&str>,
) -> Result<()> {
    let parent = latest.parent().ok_or_else(|| {
        anyhow!(
            "latest report directory has no parent: {}",
            latest.display()
        )
    })?;
    let temporary = temporary_directory(parent, "latest");
    let backup = temporary_directory(parent, "latest-backup");
    fs::create_dir_all(&temporary)?;
    let mut names = vec!["report.json", "summary.md", "health.md"];
    if yaml_enabled {
        names.push("report.yaml");
    }
    if let Some(name) = compressed_name {
        names.push(name);
    }
    for name in names {
        let source = run_root.join(name);
        let target = temporary.join(name);
        if fs::hard_link(&source, &target).is_err() {
            fs::copy(&source, &target)
                .with_context(|| format!("failed to materialize {}", target.display()))?;
        }
    }
    if latest.exists() {
        fs::rename(latest, &backup)?;
    }
    if let Err(error) = fs::rename(&temporary, latest) {
        if backup.exists() && !latest.exists() {
            let _ = fs::rename(&backup, latest);
        }
        return Err(error).with_context(|| {
            format!(
                "failed to publish latest report directory {}",
                latest.display()
            )
        });
    }
    if backup.exists() {
        fs::remove_dir_all(backup)?;
    }
    Ok(())
}

fn retained_directory_size(path: &Path) -> Result<u64> {
    let mut bytes = 0u64;
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let metadata = fs::symlink_metadata(entry.path())?;
        bytes = bytes.saturating_add(if metadata.is_dir() {
            retained_directory_size(&entry.path())?
        } else {
            metadata.len()
        });
    }
    Ok(bytes)
}

fn enforce_retention(runs_root: &Path, keep: usize, max_bytes: u64) -> Result<()> {
    let mut runs = fs::read_dir(runs_root)?
        .filter_map(Result::ok)
        .filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
        .map(|entry| {
            let bytes = retained_directory_size(&entry.path())?;
            Ok((entry, bytes))
        })
        .collect::<Result<Vec<_>>>()?;
    runs.sort_by_key(|(entry, _)| std::cmp::Reverse(entry.file_name()));
    let mut retained_bytes = 0u64;
    for (index, (entry, bytes)) in runs.into_iter().enumerate() {
        let retain_newest_even_if_oversized = index == 0 && keep > 0;
        if retain_newest_even_if_oversized
            || (index < keep && retained_bytes.saturating_add(bytes) <= max_bytes)
        {
            retained_bytes = retained_bytes.saturating_add(bytes);
            continue;
        }
        fs::remove_dir_all(entry.path())?;
    }
    Ok(())
}

fn cleanup_abandoned_publication_state(slop_root: &Path) -> Vec<String> {
    let mut warnings = Vec::new();
    let latest = slop_root.join("latest");
    let mut backups = Vec::new();
    let Ok(entries) = fs::read_dir(slop_root) else {
        return warnings;
    };
    for entry in entries.filter_map(Result::ok) {
        let name = entry.file_name().to_string_lossy().into_owned();
        if name.starts_with(".latest-backup-") && name.ends_with(".tmp") {
            backups.push(entry.path());
        } else if (name.starts_with(".latest-") || name.starts_with(".run-"))
            && name.ends_with(".tmp")
        {
            if let Err(error) = fs::remove_dir_all(entry.path()) {
                warnings.push(format!(
                    "failed to remove abandoned publication temporary {}: {error}",
                    entry.path().display()
                ));
            }
        }
    }
    backups.sort();
    if !latest.exists() {
        if let Some(recovery) = backups.pop() {
            if let Err(error) = fs::rename(&recovery, &latest) {
                warnings.push(format!(
                    "failed to recover latest report from {}: {error}",
                    recovery.display()
                ));
            }
        }
    }
    for backup in backups {
        if let Err(error) = fs::remove_dir_all(&backup) {
            warnings.push(format!(
                "failed to remove abandoned latest backup {}: {error}",
                backup.display()
            ));
        }
    }
    if let Ok(entries) = fs::read_dir(slop_root.join("runs")) {
        for entry in entries.filter_map(Result::ok) {
            let name = entry.file_name().to_string_lossy().into_owned();
            if name.starts_with(".run-") && name.ends_with(".tmp") {
                if let Err(error) = fs::remove_dir_all(entry.path()) {
                    warnings.push(format!(
                        "failed to remove abandoned run temporary {}: {error}",
                        entry.path().display()
                    ));
                }
            }
        }
    }
    warnings
}

fn write_run_atomically(
    run_root: &Path,
    report_json: &str,
    report_yaml: Option<&str>,
    summary: &str,
    health: &str,
    compressed: Option<(&str, &[u8])>,
) -> Result<()> {
    let parent = run_root
        .parent()
        .ok_or_else(|| anyhow!("run report directory has no parent: {}", run_root.display()))?;
    let temporary = temporary_directory(parent, "run");
    if let Err(error) = write_bundle_files(
        &temporary,
        report_json,
        report_yaml,
        summary,
        health,
        compressed,
    ) {
        let _ = fs::remove_dir_all(&temporary);
        return Err(error);
    }
    if let Err(error) = fs::rename(&temporary, run_root) {
        let _ = fs::remove_dir_all(&temporary);
        return Err(error).with_context(|| {
            format!(
                "failed to publish timestamped report directory {}",
                run_root.display()
            )
        });
    }
    Ok(())
}

pub fn write_report_bundle(analysis: &Analysis, health: &HealthRollup) -> Result<FindResult> {
    fs::create_dir_all(&analysis.output_root)
        .with_context(|| format!("failed to create {}", analysis.output_root.display()))?;
    let runs_root = analysis.output_root.join("runs");
    fs::create_dir_all(&runs_root)
        .with_context(|| format!("failed to create {}", runs_root.display()))?;
    let retention = config::pointer_u64(&analysis.config, "/output/retention_runs", 20) as usize;
    let retention_bytes =
        config::pointer_u64(&analysis.config, "/output/retention_bytes", 2_147_483_648);
    let mut warnings = cleanup_abandoned_publication_state(&analysis.output_root);
    let retention_warning =
        enforce_retention(&runs_root, retention.saturating_sub(1), retention_bytes)
            .err()
            .map(|error| format!("old report retention could not be completed: {error:#}"));
    warnings.extend(retention_warning);
    let mut report = assemble_report(analysis, health);
    apply_report_profile(&mut report, &analysis.report_profile);
    if !warnings.is_empty() {
        report["diagnostics"]["warnings"] = json!(warnings);
    }
    let pretty_json = config::pointer_bool(&analysis.config, "/output/pretty_json", false);
    let yaml_enabled = config::pointer_bool(&analysis.config, "/output/yaml", false);
    let mut previous_sizes = (0usize, 0usize);
    for _ in 0..4 {
        let json_bytes = if pretty_json {
            serde_json::to_string_pretty(&report)?.len() + 1
        } else {
            serde_json::to_string(&report)?.len() + 1
        };
        let yaml_bytes = if yaml_enabled {
            serde_yaml::to_string(&report)?.len()
        } else {
            0
        };
        if (json_bytes, yaml_bytes) == previous_sizes {
            break;
        }
        previous_sizes = (json_bytes, yaml_bytes);
        report["diagnostics"]["report_sizes"] = json!({
            "report_json_bytes": json_bytes,
            "report_yaml_bytes": yaml_bytes,
            "logical_artifact_bytes": json_bytes.saturating_add(yaml_bytes),
            "physical_storage_semantics": "latest may hard-link immutable run artifacts; do not sum logical paths to estimate allocated disk bytes",
        });
    }
    let report_json = if pretty_json {
        serde_json::to_string_pretty(&report)
    } else {
        serde_json::to_string(&report)
    }
    .context("failed to render report JSON")?
        + "\n";
    let report_yaml = yaml_enabled
        .then(|| serde_yaml::to_string(&report).context("failed to render report YAML"))
        .transpose()?;
    let summary = render_compatibility_summary(&report);
    let health_markdown = render_health_from_report(&report)?;
    let terminal = render_terminal(&report);
    let compressed = compressed_bytes(&analysis.compression, report_json.as_bytes())?;

    let run_root = unique_run_root(&runs_root, &timestamp_slug(&analysis.generated_at));
    write_run_atomically(
        &run_root,
        &report_json,
        report_yaml.as_deref(),
        &summary,
        &health_markdown,
        compressed
            .as_ref()
            .map(|(name, bytes)| (name.as_str(), bytes.as_slice())),
    )?;
    let newest_run_bytes = retained_directory_size(&run_root)?;
    if newest_run_bytes > retention_bytes {
        eprintln!(
            "warning: newest immutable run uses {newest_run_bytes} bytes, exceeding output.retention_bytes={retention_bytes}; retained newest run and pruned only older runs"
        );
    }
    let latest = analysis.output_root.join("latest");
    replace_latest_from_run(
        &latest,
        &run_root,
        yaml_enabled,
        compressed.as_ref().map(|(name, _)| name.as_str()),
    )?;
    enforce_retention(&runs_root, retention, retention_bytes)?;
    let compressed_report = compressed.map(|(name, _)| latest.join(name));

    Ok(FindResult {
        report,
        report_json: latest.join("report.json"),
        report_yaml: latest.join("report.yaml"),
        summary_md: latest.join("summary.md"),
        health_md: latest.join("health.md"),
        compressed_report,
        terminal,
        elapsed_ms: 0,
    })
}

#[cfg(test)]
mod profile_tests {
    use super::*;

    #[test]
    fn compact_profile_keeps_an_exhaustive_index_and_resolvable_references() {
        let files = (0..300)
            .map(|index| json!({"path": format!("src/{index:03}.rs")}))
            .collect::<Vec<_>>();
        let mut report = json!({
            "files": files.clone(),
            "folders": [],
            "compare_index": {"files": files, "folders": []},
            "ranked_files": [{"path": "src/297.rs"}],
            "action_queue": [{"path": "src/298.rs"}],
            "health": {
                "findings": [{"path": "src/299.rs"}],
                "refactor_candidates": [],
                "watchlist": []
            },
            "overlays": {"organization_health": {
                "relationships": {"temporal_coupling_edges": [{
                    "source_path": "src/298.rs", "target_path": "src/299.rs", "confidence": "supported"
                }]},
                "clusters": {"duplicate_sets": [{
                    "member_paths": ["src/298.rs", "src/299.rs"]
                }]}
            }},
            "summary": {},
            "diagnostics": {},
            "collection_metadata": {}
        });
        apply_report_profile(&mut report, "compact");
        let retained = report["files"]
            .as_array()
            .expect("compact files")
            .iter()
            .filter_map(|record| record["path"].as_str())
            .collect::<BTreeSet<_>>();
        assert_eq!(retained.len(), 250);
        for path in ["src/297.rs", "src/298.rs", "src/299.rs"] {
            assert!(retained.contains(path), "missing referenced path {path}");
        }
        assert_eq!(
            report["compare_index"]["files"].as_array().map(Vec::len),
            Some(300)
        );
        assert_eq!(
            report["health"]["findings"].as_array().map(Vec::len),
            Some(1)
        );
        assert_eq!(report["action_queue"].as_array().map(Vec::len), Some(1));
        assert_eq!(
            report["overlays"]["organization_health"]["relationships"]["temporal_coupling_edges"]
                .as_array()
                .map(Vec::len),
            Some(1)
        );
    }

    #[test]
    fn standard_bounds_high_cardinality_evidence_while_full_evidence_does_not() {
        let relationships = (0..2_100)
            .map(|index| json!({"id": index, "confidence": "supported"}))
            .collect::<Vec<_>>();
        let report = json!({
            "diagnostics": {},
            "overlays": {"organization_health": {"relationships": {
                "temporal_coupling_edges": relationships
            }}}
        });
        let mut standard = report.clone();
        apply_report_profile(&mut standard, "standard");
        let mut full = report;
        apply_report_profile(&mut full, "full_evidence");
        assert_eq!(
            standard
                .pointer("/overlays/organization_health/relationships/temporal_coupling_edges")
                .and_then(Value::as_array)
                .map(Vec::len),
            Some(500)
        );
        assert_eq!(
            full.pointer("/overlays/organization_health/relationships/temporal_coupling_edges")
                .and_then(Value::as_array)
                .map(Vec::len),
            Some(2_100)
        );
    }
}