runglass-core 0.3.0

Core command observation, reporting, storage, and revert logic for RunGlass.
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
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};

use crate::collectors::{hash_bytes, simple_unified_diff};
use crate::storage::report_run_dir;
use crate::{
    FileChange, FileChangeType, RevertConflictPolicy, RevertFileState, RevertFileStatus,
    RevertOptions, RevertPreview, RunReport,
};

pub fn render_reverse_patch(report: &RunReport) -> Result<String> {
    let mut lines = vec![
        "# RunGlass Reverse Patch".to_string(),
        format!("# Receipt: {}", report.run.id),
        format!("# Command: {}", report.run.command_display),
        String::new(),
    ];

    let mut emitted = false;
    for file in &report.files {
        if !file.is_text {
            lines.push(format!("# Skipped non-text file {}", file.path));
            continue;
        }

        match file.change_type {
            FileChangeType::Modified => {
                let before = load_artifact_text(report, file.before_artifact_path.as_deref())?;
                let after = load_artifact_text(report, file.after_artifact_path.as_deref())?;
                let (Some(before), Some(after)) = (before, after) else {
                    lines.push(format!("# Missing stored text snapshot for {}", file.path));
                    continue;
                };
                lines.extend(git_style_patch(&file.path, Some(&after), Some(&before)));
                emitted = true;
            }
            FileChangeType::Created => {
                let after = load_artifact_text(report, file.after_artifact_path.as_deref())?;
                let Some(after) = after else {
                    lines.push(format!("# Missing stored text snapshot for {}", file.path));
                    continue;
                };
                lines.extend(git_style_patch(&file.path, Some(&after), None));
                emitted = true;
            }
            FileChangeType::Deleted => {
                let before = load_artifact_text(report, file.before_artifact_path.as_deref())?;
                let Some(before) = before else {
                    lines.push(format!("# Missing stored text snapshot for {}", file.path));
                    continue;
                };
                lines.extend(git_style_patch(&file.path, None, Some(&before)));
                emitted = true;
            }
        }
    }

    if !emitted {
        lines.push("# No reversible text patch content was available.".to_string());
    }

    Ok(lines.join("\n"))
}

pub fn preview_revert(
    report: &RunReport,
    selected_paths: Option<&[String]>,
) -> Result<RevertPreview> {
    let targets = select_revert_targets(report, selected_paths)?;
    let cwd = PathBuf::from(&report.run.cwd);
    let mut preview = RevertPreview {
        receipt_id: report.run.id.clone(),
        target_count: targets.len(),
        restore_modified: 0,
        delete_created: 0,
        restore_deleted: 0,
        safe: Vec::new(),
        conflicts: Vec::new(),
        already_reverted: Vec::new(),
        missing_artifacts: Vec::new(),
    };

    for file in targets {
        match file.change_type {
            FileChangeType::Modified => preview.restore_modified += 1,
            FileChangeType::Created => preview.delete_created += 1,
            FileChangeType::Deleted => preview.restore_deleted += 1,
        }

        let status = evaluate_revert_status(file, &cwd)?;
        match status.status {
            RevertFileState::Safe => preview.safe.push(status),
            RevertFileState::ChangedSinceReceipt => preview.conflicts.push(status),
            RevertFileState::AlreadyReverted => preview.already_reverted.push(status),
            RevertFileState::MissingArtifacts => preview.missing_artifacts.push(status),
        }
    }

    Ok(preview)
}

pub fn apply_revert(
    report: &RunReport,
    selected_paths: Option<&[String]>,
    options: RevertOptions,
) -> Result<RevertPreview> {
    let preview = preview_revert(report, selected_paths)?;
    if !preview.missing_artifacts.is_empty() {
        return Err(anyhow!(
            "receipt is missing stored file snapshots for: {}",
            preview
                .missing_artifacts
                .iter()
                .map(|item| item.path.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }
    if !preview.conflicts.is_empty() && matches!(options.policy, RevertConflictPolicy::Abort) {
        return Err(anyhow!(
            "some files changed after the receipt finished: {}",
            preview
                .conflicts
                .iter()
                .map(|item| item.path.as_str())
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }

    let targets = select_revert_targets(report, selected_paths)?;
    let cwd = PathBuf::from(&report.run.cwd);
    for file in targets {
        let status = evaluate_revert_status(file, &cwd)?;
        if matches!(status.status, RevertFileState::AlreadyReverted) {
            continue;
        }
        if matches!(status.status, RevertFileState::ChangedSinceReceipt)
            && matches!(options.policy, RevertConflictPolicy::SkipChanged)
        {
            continue;
        }
        apply_revert_file(report, file, &cwd)?;
    }

    preview_revert(report, selected_paths)
}

fn select_revert_targets<'a>(
    report: &'a RunReport,
    selected_paths: Option<&[String]>,
) -> Result<Vec<&'a FileChange>> {
    if let Some(paths) = selected_paths {
        if paths.is_empty() {
            return Ok(report.files.iter().collect());
        }
        let mut selected = Vec::new();
        for path in paths {
            let file = report
                .files
                .iter()
                .find(|file| file.path == *path)
                .ok_or_else(|| anyhow!("receipt does not include file change {}", path))?;
            selected.push(file);
        }
        return Ok(selected);
    }
    Ok(report.files.iter().collect())
}

fn artifact_path(report: &RunReport, relative: &str) -> Result<PathBuf> {
    let cwd_local = PathBuf::from(&report.run.cwd)
        .join(".runglass")
        .join("reports")
        .join(&report.run.id)
        .join(relative);
    if cwd_local.exists() {
        return Ok(cwd_local);
    }
    Ok(report_run_dir(&report.run.id)?.join(relative))
}

fn load_artifact_bytes(report: &RunReport, relative: Option<&str>) -> Result<Option<Vec<u8>>> {
    let Some(relative) = relative else {
        return Ok(None);
    };
    let path = artifact_path(report, relative)?;
    Ok(Some(fs::read(&path).with_context(|| {
        format!("failed to read artifact {}", path.display())
    })?))
}

fn load_artifact_text(report: &RunReport, relative: Option<&str>) -> Result<Option<String>> {
    let Some(bytes) = load_artifact_bytes(report, relative)? else {
        return Ok(None);
    };
    Ok(String::from_utf8(bytes).ok())
}

fn current_file_hash(path: &Path) -> Result<Option<String>> {
    if !path.exists() {
        return Ok(None);
    }
    let bytes = fs::read(path).with_context(|| format!("failed to read {}", path.display()))?;
    Ok(Some(hash_bytes(&bytes)))
}

fn evaluate_revert_status(file: &FileChange, cwd: &Path) -> Result<RevertFileStatus> {
    let current_path = cwd.join(&file.path);
    let current_hash = current_file_hash(&current_path)?;

    let status = match file.change_type {
        FileChangeType::Modified => {
            if file.before_artifact_path.is_none() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::MissingArtifacts,
                    detail: "Missing stored before snapshot.".to_string(),
                }
            } else if current_hash.as_deref() == file.after_hash.as_deref() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::Safe,
                    detail: "Current file still matches the receipt's after-run version."
                        .to_string(),
                }
            } else if current_hash.as_deref() == file.before_hash.as_deref() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::AlreadyReverted,
                    detail: "Current file already matches the stored before-run version."
                        .to_string(),
                }
            } else {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::ChangedSinceReceipt,
                    detail: "File contents changed after the receipt finished.".to_string(),
                }
            }
        }
        FileChangeType::Created => {
            if current_hash.is_none() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::AlreadyReverted,
                    detail: "Created file is already gone.".to_string(),
                }
            } else if current_hash.as_deref() == file.after_hash.as_deref() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::Safe,
                    detail:
                        "Created file still matches the receipt version and can be deleted safely."
                            .to_string(),
                }
            } else {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::ChangedSinceReceipt,
                    detail: "Created file changed after the receipt finished.".to_string(),
                }
            }
        }
        FileChangeType::Deleted => {
            if file.before_artifact_path.is_none() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::MissingArtifacts,
                    detail: "Missing stored before snapshot.".to_string(),
                }
            } else if current_hash.is_none() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::Safe,
                    detail: "Deleted file is still absent and can be restored safely.".to_string(),
                }
            } else if current_hash.as_deref() == file.before_hash.as_deref() {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::AlreadyReverted,
                    detail: "Deleted file already matches the stored before-run version."
                        .to_string(),
                }
            } else {
                RevertFileStatus {
                    path: file.path.clone(),
                    change_type: file.change_type.clone(),
                    status: RevertFileState::ChangedSinceReceipt,
                    detail: "A newer file now exists at this path.".to_string(),
                }
            }
        }
    };

    Ok(status)
}

fn apply_revert_file(report: &RunReport, file: &FileChange, cwd: &Path) -> Result<()> {
    let path = cwd.join(&file.path);
    match file.change_type {
        FileChangeType::Modified | FileChangeType::Deleted => {
            let bytes = load_artifact_bytes(report, file.before_artifact_path.as_deref())?
                .ok_or_else(|| anyhow!("missing stored before snapshot for {}", file.path))?;
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::write(&path, bytes)?;
            set_executable_flag(&path, file.before_executable.unwrap_or(false))?;
        }
        FileChangeType::Created => {
            if path.exists() {
                fs::remove_file(&path)
                    .with_context(|| format!("failed to delete {}", path.display()))?;
            }
        }
    }
    Ok(())
}

fn set_executable_flag(path: &Path, executable: bool) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let mut perms = fs::metadata(path)?.permissions();
        let mode = if executable { 0o755 } else { 0o644 };
        perms.set_mode(mode);
        fs::set_permissions(path, perms)?;
    }

    #[cfg(not(unix))]
    {
        let _ = (path, executable);
    }

    Ok(())
}

fn git_style_patch(path: &str, before: Option<&str>, after: Option<&str>) -> Vec<String> {
    let mut lines = Vec::new();
    lines.push(format!("diff --git a/{path} b/{path}"));
    match (before, after) {
        (Some(_), None) => {
            lines.push("deleted file mode 100644".to_string());
            lines.push(format!("--- a/{path}"));
            lines.push("+++ /dev/null".to_string());
            lines.push(simple_unified_diff(before.unwrap_or_default(), ""));
        }
        (None, Some(_)) => {
            lines.push("new file mode 100644".to_string());
            lines.push("--- /dev/null".to_string());
            lines.push(format!("+++ b/{path}"));
            lines.push(simple_unified_diff("", after.unwrap_or_default()));
        }
        (Some(before), Some(after)) => {
            lines.push(format!("--- a/{path}"));
            lines.push(format!("+++ b/{path}"));
            lines.push(simple_unified_diff(before, after));
        }
        (None, None) => {}
    }
    lines.push(String::new());
    lines
}

#[cfg(test)]
mod tests {
    use std::sync::{Mutex, OnceLock};
    use std::time::{SystemTime, UNIX_EPOCH};
    use std::{env, fs};

    use chrono::Utc;

    use super::{apply_revert, preview_revert, render_reverse_patch};
    use crate::collectors::hash_bytes;
    use crate::{
        FileChange, FileChangeType, ObservationMode, RevertConflictPolicy, RevertOptions,
        RiskLevel, RunMeta, RunReport, RunStatus, Summary,
    };

    fn env_lock() -> &'static Mutex<()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
    }

    #[test]
    fn preview_and_apply_revert_cover_modified_created_and_deleted_files() {
        let _guard = env_lock().lock().expect("lock test env");
        let fixture = RevertFixture::new("revert-apply");

        let preview = preview_revert(&fixture.report, None).expect("preview");
        assert_eq!(preview.target_count, 3);
        assert_eq!(preview.safe.len(), 3);
        assert!(preview.conflicts.is_empty());

        let after_apply = apply_revert(
            &fixture.report,
            None,
            RevertOptions {
                policy: RevertConflictPolicy::Force,
            },
        )
        .expect("apply revert");

        assert_eq!(after_apply.already_reverted.len(), 3);
        assert_eq!(
            fs::read_to_string(fixture.workspace.join("modified.txt")).expect("modified content"),
            "before\n"
        );
        assert!(
            !fixture.workspace.join("created.txt").exists(),
            "created file should be removed"
        );
        assert_eq!(
            fs::read_to_string(fixture.workspace.join("deleted.txt")).expect("deleted restored"),
            "restore me\n"
        );
    }

    #[test]
    fn preview_detects_conflicts_and_skip_changed_leaves_newer_edits_in_place() {
        let _guard = env_lock().lock().expect("lock test env");
        let fixture = RevertFixture::new("revert-conflict");

        fs::write(fixture.workspace.join("modified.txt"), "changed again\n")
            .expect("write newer modified");
        fs::write(fixture.workspace.join("created.txt"), "changed created\n")
            .expect("write newer created");

        let preview = preview_revert(&fixture.report, None).expect("preview");
        assert_eq!(preview.conflicts.len(), 2);
        assert_eq!(preview.safe.len(), 1);

        let after_apply = apply_revert(
            &fixture.report,
            None,
            RevertOptions {
                policy: RevertConflictPolicy::SkipChanged,
            },
        )
        .expect("apply skip changed");

        assert_eq!(after_apply.conflicts.len(), 2);
        assert_eq!(
            fs::read_to_string(fixture.workspace.join("modified.txt")).expect("modified content"),
            "changed again\n"
        );
        assert_eq!(
            fs::read_to_string(fixture.workspace.join("created.txt")).expect("created content"),
            "changed created\n"
        );
        assert_eq!(
            fs::read_to_string(fixture.workspace.join("deleted.txt")).expect("deleted restored"),
            "restore me\n"
        );
    }

    #[test]
    fn reverse_patch_contains_git_style_operations() {
        let _guard = env_lock().lock().expect("lock test env");
        let fixture = RevertFixture::new("reverse-patch");

        let patch = render_reverse_patch(&fixture.report).expect("reverse patch");
        assert!(patch.contains("# RunGlass Reverse Patch"));
        assert!(patch.contains("diff --git a/modified.txt b/modified.txt"));
        assert!(patch.contains("deleted file mode 100644"));
        assert!(patch.contains("new file mode 100644"));
    }

    struct RevertFixture {
        workspace: std::path::PathBuf,
        report: RunReport,
    }

    impl RevertFixture {
        fn new(name: &str) -> Self {
            let root = unique_test_root(name);
            let workspace = root.join("workspace");
            let run_id = format!("{name}-{}", unique_suffix());
            let run_dir = workspace.join(".runglass").join("reports").join(&run_id);
            let artifacts_dir = run_dir.join("file-artifacts");

            fs::create_dir_all(&workspace).expect("workspace dir");
            fs::create_dir_all(&artifacts_dir).expect("artifacts dir");

            fs::write(workspace.join("modified.txt"), "after\n").expect("modified workspace");
            fs::write(workspace.join("created.txt"), "created\n").expect("created workspace");

            fs::write(artifacts_dir.join("001_modified-txt.before"), "before\n")
                .expect("modified before");
            fs::write(artifacts_dir.join("001_modified-txt.after"), "after\n")
                .expect("modified after");
            fs::write(artifacts_dir.join("002_created-txt.after"), "created\n")
                .expect("created after");
            fs::write(artifacts_dir.join("003_deleted-txt.before"), "restore me\n")
                .expect("deleted before");

            let report = RunReport {
                schema_version: "0.1.0".to_string(),
                ci: None,
                run: RunMeta {
                    id: run_id.clone(),
                    command_display: "sh -c 'test revert'".to_string(),
                    argv: vec![
                        "sh".to_string(),
                        "-c".to_string(),
                        "test revert".to_string(),
                    ],
                    cwd: workspace.display().to_string(),
                    shell: Some("/bin/sh".to_string()),
                    mode: ObservationMode::Normal,
                    started_at: Utc::now(),
                    ended_at: Some(Utc::now()),
                    duration_ms: Some(250),
                    exit_code: Some(0),
                    status: RunStatus::Completed,
                },
                summary: Summary {
                    files_changed: 3,
                    files_created: 1,
                    files_modified: 1,
                    files_deleted: 1,
                    processes_seen: 0,
                    network_hosts: 0,
                    ports_opened: 0,
                    docker_containers_created: 0,
                    docker_images_pulled: 0,
                    docker_volumes_created: 0,
                    risk_level: RiskLevel::Low,
                },
                events: Vec::new(),
                processes: Vec::new(),
                files: vec![
                    file_change(
                        "modified.txt",
                        FileChangeType::Modified,
                        Some("before\n"),
                        Some("after\n"),
                        Some("file-artifacts/001_modified-txt.before"),
                        Some("file-artifacts/001_modified-txt.after"),
                    ),
                    file_change(
                        "created.txt",
                        FileChangeType::Created,
                        None,
                        Some("created\n"),
                        None,
                        Some("file-artifacts/002_created-txt.after"),
                    ),
                    file_change(
                        "deleted.txt",
                        FileChangeType::Deleted,
                        Some("restore me\n"),
                        None,
                        Some("file-artifacts/003_deleted-txt.before"),
                        None,
                    ),
                ],
                network: Vec::new(),
                docker: None,
                risks: Vec::new(),
                stdout_path: None,
                stderr_path: None,
                stdout: None,
                stderr: None,
                limitations: vec!["test receipt".to_string()],
            };

            fs::create_dir_all(&run_dir).expect("run dir");
            fs::write(
                run_dir.join("report.json"),
                serde_json::to_vec_pretty(&report).expect("report json"),
            )
            .expect("write report json");

            Self { workspace, report }
        }
    }

    fn file_change(
        path: &str,
        change_type: FileChangeType,
        before: Option<&str>,
        after: Option<&str>,
        before_artifact_path: Option<&str>,
        after_artifact_path: Option<&str>,
    ) -> FileChange {
        FileChange {
            path: path.to_string(),
            change_type,
            before_hash: before.map(|value| hash_bytes(value.as_bytes())),
            after_hash: after.map(|value| hash_bytes(value.as_bytes())),
            before_size: before.map(|value| value.len() as u64),
            after_size: after.map(|value| value.len() as u64),
            is_text: true,
            diff: None,
            risk_tags: Vec::new(),
            before_artifact_path: before_artifact_path.map(ToString::to_string),
            after_artifact_path: after_artifact_path.map(ToString::to_string),
            before_executable: Some(false),
            after_executable: Some(false),
        }
    }

    fn unique_test_root(name: &str) -> std::path::PathBuf {
        let root = env::temp_dir().join(format!("runglass-{name}-{}", unique_suffix()));
        if root.exists() {
            fs::remove_dir_all(&root).expect("remove stale test root");
        }
        fs::create_dir_all(&root).expect("create test root");
        root
    }

    fn unique_suffix() -> String {
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("time")
            .as_nanos();
        format!("{}-{nanos}", std::process::id())
    }
}