vetto 0.2.14

Daemon-less sandbox + security layer for AI coding agents (Landlock/Seatbelt, TUI statusline, post-session audit 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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
//! `vetto diff` CLI command implementation (Autonomous Agent Session Review).
//!
//! Compares current project workspace against a session snapshot archive to surface
//! all filesystem changes (added, modified, deleted) and integrates security telemetry
//! (blocked file reads, blocked network egress, contacted domains).

use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::rescue::snapshot::{self, SnapshotMetadata};

/// Arguments for `vetto diff`.
#[derive(clap::Args, Debug, Clone)]
pub struct DiffArgs {
    /// Session ID to inspect (defaults to latest session for current workspace)
    #[arg(value_name = "SESSION_ID")]
    pub session_id: Option<String>,

    /// Display only summary statistics (+/- lines, file counts, security events)
    #[arg(short = 's', long = "stat")]
    pub stat: bool,

    /// Output review in structured JSON format
    #[arg(long = "json")]
    pub json: bool,

    /// Filter diff to a specific relative file or folder path
    #[arg(short = 'p', long = "path", value_name = "PATH")]
    pub path: Option<String>,
}

/// Security telemetry collected from session logs and reports.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SecurityTelemetry {
    pub blocked_file_count: u64,
    pub blocked_file_paths: Vec<String>,
    pub blocked_network_count: u64,
    pub blocked_network_destinations: Vec<String>,
    pub allowed_egress: Vec<String>,
}

/// Kind of file modification observed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangeType {
    Added,
    Modified,
    Deleted,
}

/// Details of a single file change between snapshot and working tree.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileChange {
    pub path: String,
    pub change_type: ChangeType,
    pub lines_added: usize,
    pub lines_deleted: usize,
    pub is_binary: bool,
    #[serde(skip_serializing)]
    pub color_patch: String,
    pub patch: String,
}

/// Completed session review data model.
#[derive(Debug, Clone)]
pub struct SessionReview {
    pub session_id: String,
    pub project_dir: PathBuf,
    pub security: SecurityTelemetry,
    pub files: Vec<FileChange>,
}

/// Structured JSON output representation of a session review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffReviewJson {
    pub session_id: String,
    pub project_dir: String,
    pub security: DiffSecurityJson,
    pub files: DiffFilesSummaryJson,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub diffs: Vec<DiffFilePatchJson>,
}

/// Security section in JSON review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffSecurityJson {
    pub blocked_file_reads: u64,
    pub blocked_file_paths: Vec<String>,
    pub blocked_network: u64,
    pub blocked_network_destinations: Vec<String>,
    pub allowed_egress: Vec<String>,
}

/// Files change summary section in JSON review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffFilesSummaryJson {
    pub added: Vec<DiffFileStatJson>,
    pub modified: Vec<DiffFileStatJson>,
    pub deleted: Vec<DiffFileStatJson>,
    pub total_changed: usize,
    pub total_lines_added: usize,
    pub total_lines_deleted: usize,
}

/// Single file statistic entry in JSON review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffFileStatJson {
    pub path: String,
    pub lines_added: usize,
    pub lines_deleted: usize,
    pub is_binary: bool,
}

/// Unified diff patch entry in JSON review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffFilePatchJson {
    pub path: String,
    pub change_type: String,
    pub patch: String,
}

impl SessionReview {
    /// Convert internal review to serializable JSON format.
    pub fn to_json(&self, include_patches: bool) -> DiffReviewJson {
        let mut added = Vec::new();
        let mut modified = Vec::new();
        let mut deleted = Vec::new();
        let mut diffs = Vec::new();

        let mut total_lines_added = 0;
        let mut total_lines_deleted = 0;

        for f in &self.files {
            total_lines_added += f.lines_added;
            total_lines_deleted += f.lines_deleted;

            let stat = DiffFileStatJson {
                path: f.path.clone(),
                lines_added: f.lines_added,
                lines_deleted: f.lines_deleted,
                is_binary: f.is_binary,
            };

            match f.change_type {
                ChangeType::Added => added.push(stat),
                ChangeType::Modified => modified.push(stat),
                ChangeType::Deleted => deleted.push(stat),
            }

            if include_patches && !f.patch.is_empty() {
                diffs.push(DiffFilePatchJson {
                    path: f.path.clone(),
                    change_type: match f.change_type {
                        ChangeType::Added => "added".to_string(),
                        ChangeType::Modified => "modified".to_string(),
                        ChangeType::Deleted => "deleted".to_string(),
                    },
                    patch: f.patch.clone(),
                });
            }
        }

        DiffReviewJson {
            session_id: self.session_id.clone(),
            project_dir: self.project_dir.display().to_string(),
            security: DiffSecurityJson {
                blocked_file_reads: self.security.blocked_file_count,
                blocked_file_paths: self.security.blocked_file_paths.clone(),
                blocked_network: self.security.blocked_network_count,
                blocked_network_destinations: self.security.blocked_network_destinations.clone(),
                allowed_egress: self.security.allowed_egress.clone(),
            },
            files: DiffFilesSummaryJson {
                added,
                modified,
                deleted,
                total_changed: self.files.len(),
                total_lines_added,
                total_lines_deleted,
            },
            diffs,
        }
    }
}

/// Execute the `vetto diff` command.
pub fn run_diff(args: &DiffArgs) -> Result<()> {
    let snapshot_opt = resolve_session_and_snapshot(args.session_id.as_deref())?;

    let Some(snapshot_meta) = snapshot_opt else {
        let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
        let in_git = cwd.join(".git").exists()
            || crate::policy::conditions::detect_git_branch(&cwd).is_some();

        if args.json {
            let empty_review = DiffReviewJson {
                session_id: args
                    .session_id
                    .clone()
                    .unwrap_or_else(|| "none".to_string()),
                project_dir: cwd.display().to_string(),
                security: DiffSecurityJson {
                    blocked_file_reads: 0,
                    blocked_file_paths: Vec::new(),
                    blocked_network: 0,
                    blocked_network_destinations: Vec::new(),
                    allowed_egress: Vec::new(),
                },
                files: DiffFilesSummaryJson {
                    added: Vec::new(),
                    modified: Vec::new(),
                    deleted: Vec::new(),
                    total_changed: 0,
                    total_lines_added: 0,
                    total_lines_deleted: 0,
                },
                diffs: Vec::new(),
            };
            let json_text = serde_json::to_string_pretty(&empty_review)?;
            println!("{json_text}");
            return Ok(());
        }

        println!(
            "vetto diff: no snapshot found for current workspace. \
             Snapshots are created automatically when running agents under vetto."
        );

        if in_git {
            if args.stat {
                let _ = std::process::Command::new("git")
                    .args(["status", "--short"])
                    .current_dir(&cwd)
                    .status();
            } else {
                let _ = std::process::Command::new("git")
                    .args(["diff"])
                    .current_dir(&cwd)
                    .status();
            }
        }

        return Ok(());
    };

    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let project_dir = if snapshot_meta.project_dir.is_dir() {
        snapshot_meta.project_dir.clone()
    } else {
        cwd
    };

    let archive_path = if snapshot_meta.archive_file.is_file() {
        snapshot_meta.archive_file.clone()
    } else {
        let root = snapshot::snapshots_root_dir()?;
        let fallback = root.join(&snapshot_meta.session_id).join("snapshot.tar");
        if fallback.is_file() {
            fallback
        } else {
            anyhow::bail!(
                "snapshot archive '{}' not found",
                snapshot_meta.archive_file.display()
            );
        }
    };

    let telemetry = load_security_telemetry(&snapshot_meta.session_id, &project_dir);

    let review = compare_snapshot_against_disk(
        &archive_path,
        &project_dir,
        args.path.as_deref(),
        &telemetry,
        &snapshot_meta.session_id,
    )?;

    if args.json {
        let json_val = review.to_json(!args.stat);
        let serialized = serde_json::to_string_pretty(&json_val)?;
        println!("{serialized}");
        return Ok(());
    }

    render_human_review(&review, args.stat);
    Ok(())
}

/// Print formatted human-readable terminal review.
fn render_human_review(review: &SessionReview, stat_only: bool) {
    let security = &review.security;

    let file_reads_str = if security.blocked_file_count == 0 {
        "0 (none)".to_string()
    } else {
        let paths_str = if security.blocked_file_paths.is_empty() {
            "none".to_string()
        } else if security.blocked_file_paths.len() > 4 {
            format!("{}, ...", security.blocked_file_paths[..4].join(", "))
        } else {
            security.blocked_file_paths.join(", ")
        };
        format!("{} ({})", security.blocked_file_count, paths_str)
    };

    let net_blocked_str = if security.blocked_network_count == 0 {
        "0 (none)".to_string()
    } else {
        let dests_str = if security.blocked_network_destinations.is_empty() {
            "none".to_string()
        } else if security.blocked_network_destinations.len() > 4 {
            format!(
                "{}, ...",
                security.blocked_network_destinations[..4].join(", ")
            )
        } else {
            security.blocked_network_destinations.join(", ")
        };
        format!("{} ({})", security.blocked_network_count, dests_str)
    };

    let egress_str = if security.allowed_egress.is_empty() {
        "none".to_string()
    } else if security.allowed_egress.len() > 5 {
        format!("{}, ...", security.allowed_egress[..5].join(", "))
    } else {
        security.allowed_egress.join(", ")
    };

    println!("=== Vetto Session Review: {} ===", review.session_id);
    println!("Project: {}", review.project_dir.display());
    println!("Security Interceptions:");
    println!("  • Blocked file reads: {file_reads_str}");
    println!("  • Blocked network:    {net_blocked_str}");
    println!("  • Allowed egress:     {egress_str}");
    println!("Files Modified:");

    if review.files.is_empty() {
        println!("  (no files modified)");
    } else {
        for change in &review.files {
            match change.change_type {
                ChangeType::Added => {
                    if change.is_binary {
                        println!("\x1b[32m  + {} (binary)\x1b[0m", change.path);
                    } else {
                        println!(
                            "\x1b[32m  + {} (+{} lines)\x1b[0m",
                            change.path, change.lines_added
                        );
                    }
                }
                ChangeType::Modified => {
                    if change.is_binary {
                        println!("\x1b[33m  ~ {} (binary)\x1b[0m", change.path);
                    } else {
                        println!(
                            "\x1b[33m  ~ {} (+{} / -{} lines)\x1b[0m",
                            change.path, change.lines_added, change.lines_deleted
                        );
                    }
                }
                ChangeType::Deleted => {
                    if change.is_binary {
                        println!("\x1b[31m  - {} (binary)\x1b[0m", change.path);
                    } else {
                        println!(
                            "\x1b[31m  - {} (-{} lines)\x1b[0m",
                            change.path, change.lines_deleted
                        );
                    }
                }
            }
        }
    }

    if stat_only {
        let total_added: usize = review.files.iter().map(|f| f.lines_added).sum();
        let total_deleted: usize = review.files.iter().map(|f| f.lines_deleted).sum();
        println!(
            "\nSummary: {} file(s) changed (+{} lines, -{} lines)",
            review.files.len(),
            total_added,
            total_deleted
        );
        return;
    }

    // Print clean unified color diff for modified/added files
    for change in &review.files {
        if !change.color_patch.is_empty() {
            println!();
            print!("{}", change.color_patch);
        }
    }
}

/// Resolves snapshot metadata from requested session ID or latest in current workspace.
pub fn resolve_session_and_snapshot(
    requested_session: Option<&str>,
) -> Result<Option<SnapshotMetadata>> {
    match snapshot::snapshots_root_dir() {
        Ok(dir) => resolve_session_and_snapshot_in(&dir, requested_session),
        // Same as before: unreadable HOME means "no snapshots", not an error.
        Err(_) => Ok(None),
    }
}

/// Same as [`resolve_session_and_snapshot`], but rooted at an explicit
/// directory. Production passes the real store root; tests pass a fresh
/// temp dir, which keeps them hermetic against the shared per-user store.
pub fn resolve_session_and_snapshot_in(
    snapshots_root: &Path,
    requested_session: Option<&str>,
) -> Result<Option<SnapshotMetadata>> {
    let snapshots = snapshot::list_snapshots_in(snapshots_root)?;

    if let Some(req_id) = requested_session {
        if let Some(s) = snapshots.iter().find(|s| {
            s.session_id == req_id
                || s.session_id.contains(req_id)
                || s.session_id.strip_prefix("session-") == Some(req_id)
        }) {
            return Ok(Some(s.clone()));
        }

        let direct_path = Path::new(req_id);
        if direct_path.is_file() {
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            return Ok(Some(SnapshotMetadata {
                session_id: req_id.to_string(),
                created_at: String::new(),
                project_dir: cwd,
                archive_file: direct_path.to_path_buf(),
                file_count: 0,
                total_size_bytes: 0,
            }));
        }

        let candidate = snapshots_root.join(req_id).join("snapshot.tar");
        if candidate.is_file() {
            let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
            return Ok(Some(SnapshotMetadata {
                session_id: req_id.to_string(),
                created_at: String::new(),
                project_dir: cwd,
                archive_file: candidate,
                file_count: 0,
                total_size_bytes: 0,
            }));
        }

        return Ok(None);
    }

    if snapshots.is_empty() {
        return Ok(None);
    }

    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let cwd_canonical = cwd.canonicalize().unwrap_or_else(|_| cwd.clone());

    let matching = snapshots.iter().find(|s| {
        s.project_dir == cwd
            || s.project_dir
                .canonicalize()
                .map(|p| p == cwd_canonical)
                .unwrap_or(false)
    });

    Ok(Some(matching.unwrap_or(&snapshots[0]).clone()))
}

/// Compare snapshot tar archive against disk files.
pub fn compare_snapshot_against_disk(
    archive_path: &Path,
    project_dir: &Path,
    path_filter: Option<&str>,
    telemetry: &SecurityTelemetry,
    session_id: &str,
) -> Result<SessionReview> {
    let snapshot_files = read_tar_archive(archive_path)?;
    let mut disk_files = scan_disk_files(project_dir)?;
    if let Ok(rel_archive) = archive_path.strip_prefix(project_dir) {
        let rel_str = rel_archive.to_string_lossy().replace('\\', "/");
        disk_files.remove(&rel_str);
    }

    let snapshot_keys: BTreeSet<&String> = snapshot_files.keys().collect();
    let disk_keys: BTreeSet<&String> = disk_files.keys().collect();

    let mut files = Vec::new();

    // 1. Added files: on disk, absent in snapshot
    for path in disk_keys.difference(&snapshot_keys) {
        if !matches_path_filter(path, path_filter) {
            continue;
        }
        let disk_bytes = &disk_files[*path];
        let is_bin = is_binary(disk_bytes);
        let (added, deleted, color_patch, plain_patch) = if is_bin {
            (
                0,
                0,
                format!("Binary file added: {}\n", path),
                format!("Binary file added: {}\n", path),
            )
        } else {
            let text = String::from_utf8_lossy(disk_bytes);
            let lines: Vec<&str> = text.lines().collect();
            format_unified_diff(path, &[], &lines, true, false)
        };

        files.push(FileChange {
            path: (*path).clone(),
            change_type: ChangeType::Added,
            lines_added: added,
            lines_deleted: deleted,
            is_binary: is_bin,
            color_patch,
            patch: plain_patch,
        });
    }

    // 2. Modified files: present in both, content differ
    for path in disk_keys.intersection(&snapshot_keys) {
        if !matches_path_filter(path, path_filter) {
            continue;
        }
        let snap_bytes = &snapshot_files[*path];
        let disk_bytes = &disk_files[*path];

        if snap_bytes == disk_bytes {
            continue;
        }

        let is_bin = is_binary(snap_bytes) || is_binary(disk_bytes);
        let (added, deleted, color_patch, plain_patch) = if is_bin {
            (
                0,
                0,
                format!("Binary files differ: {}\n", path),
                format!("Binary files differ: {}\n", path),
            )
        } else {
            let snap_text = String::from_utf8_lossy(snap_bytes);
            let disk_text = String::from_utf8_lossy(disk_bytes);
            let old_lines: Vec<&str> = snap_text.lines().collect();
            let new_lines: Vec<&str> = disk_text.lines().collect();
            format_unified_diff(path, &old_lines, &new_lines, false, false)
        };

        files.push(FileChange {
            path: (*path).clone(),
            change_type: ChangeType::Modified,
            lines_added: added,
            lines_deleted: deleted,
            is_binary: is_bin,
            color_patch,
            patch: plain_patch,
        });
    }

    // 3. Deleted files: present in snapshot, absent on disk
    for path in snapshot_keys.difference(&disk_keys) {
        if !matches_path_filter(path, path_filter) {
            continue;
        }
        let snap_bytes = &snapshot_files[*path];
        let is_bin = is_binary(snap_bytes);
        let (added, deleted, color_patch, plain_patch) = if is_bin {
            (
                0,
                0,
                format!("Binary file deleted: {}\n", path),
                format!("Binary file deleted: {}\n", path),
            )
        } else {
            let text = String::from_utf8_lossy(snap_bytes);
            let lines: Vec<&str> = text.lines().collect();
            format_unified_diff(path, &lines, &[], false, true)
        };

        files.push(FileChange {
            path: (*path).clone(),
            change_type: ChangeType::Deleted,
            lines_added: added,
            lines_deleted: deleted,
            is_binary: is_bin,
            color_patch,
            patch: plain_patch,
        });
    }

    files.sort_by(|a, b| a.path.cmp(&b.path));

    Ok(SessionReview {
        session_id: session_id.to_string(),
        project_dir: project_dir.to_path_buf(),
        security: telemetry.clone(),
        files,
    })
}

/// Read entries and contents from a snapshot tar archive.
pub fn read_tar_archive(path: &Path) -> Result<BTreeMap<String, Vec<u8>>> {
    let file = File::open(path)
        .with_context(|| format!("failed to open snapshot archive {}", path.display()))?;
    read_tar_entries(file)
}

/// Read tar entries from an arbitrary byte reader.
pub fn read_tar_entries<R: Read>(mut reader: R) -> Result<BTreeMap<String, Vec<u8>>> {
    let mut entries = BTreeMap::new();
    loop {
        let mut header = [0u8; 512];
        let n = reader.read(&mut header)?;
        if n < 512 || header.iter().all(|&b| b == 0) {
            break;
        }

        let (name, size) = snapshot::parse_tar_header(&header)?;
        if name.is_empty() {
            break;
        }

        let mut data = vec![0u8; size as usize];
        reader.read_exact(&mut data)?;

        let padding = (512 - (size % 512)) % 512;
        if padding > 0 {
            let mut pad_buf = vec![0u8; padding as usize];
            reader.read_exact(&mut pad_buf)?;
        }

        let clean_path = Path::new(&name);
        if !clean_path.is_absolute()
            && !clean_path
                .components()
                .any(|c| matches!(c, std::path::Component::ParentDir))
        {
            let normalized = name.replace('\\', "/");
            entries.insert(normalized, data);
        }
    }
    Ok(entries)
}

/// Scan current project directory, ignoring transient / toolchain folders.
pub fn scan_disk_files(root: &Path) -> Result<BTreeMap<String, Vec<u8>>> {
    let mut files = BTreeMap::new();
    let mut queue = vec![root.to_path_buf()];

    while let Some(dir) = queue.pop() {
        let Ok(entries) = std::fs::read_dir(&dir) else {
            continue;
        };

        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name().to_string_lossy().to_string();

            if path.is_dir() {
                if !crate::policy::secretscan::is_ignored_directory(&name) {
                    queue.push(path);
                }
            } else if path.is_file() {
                if let Ok(rel) = path.strip_prefix(root) {
                    let rel_str = rel.to_string_lossy().replace('\\', "/");
                    if let Ok(bytes) = std::fs::read(&path) {
                        files.insert(rel_str, bytes);
                    }
                }
            }
        }
    }

    Ok(files)
}

/// Read and aggregate security telemetry for session from logs, reports, and history.
pub fn load_security_telemetry(session_id: &str, project_dir: &Path) -> SecurityTelemetry {
    let mut telemetry = SecurityTelemetry::default();
    let stripped = session_id.strip_prefix("session-").unwrap_or(session_id);

    let home = std::env::var_os("HOME")
        .or_else(|| std::env::var_os("USERPROFILE"))
        .map(PathBuf::from);

    // 1. Try JSONL log files
    let mut log_candidates = Vec::new();
    if let Some(ref h) = home {
        let logs_dir = h.join(".vetto").join("logs");
        log_candidates.push(logs_dir.join(format!("{session_id}.jsonl")));
        log_candidates.push(logs_dir.join(format!("{stripped}.jsonl")));
        log_candidates.push(logs_dir.join(format!("session-{session_id}.jsonl")));
        log_candidates.push(logs_dir.join(format!("session-{stripped}.jsonl")));

        if logs_dir.is_dir() {
            if let Ok(entries) = std::fs::read_dir(&logs_dir) {
                for entry in entries.flatten() {
                    let p = entry.path();
                    let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
                    if (name.contains(session_id) || name.contains(stripped))
                        && name.ends_with(".jsonl")
                    {
                        log_candidates.push(p);
                    }
                }
            }
        }
    }

    for log_path in log_candidates {
        if log_path.is_file() {
            parse_jsonl_telemetry(&log_path, &mut telemetry);
            break;
        }
    }

    // 2. Try JSON report files in project and home
    let mut report_candidates = vec![
        project_dir.join(".vetto").join("reports"),
        project_dir.join(".vetto-reports"),
    ];
    if let Some(ref h) = home {
        report_candidates.push(h.join(".vetto").join("reports"));
    }

    for rep_dir in report_candidates {
        if !rep_dir.is_dir() {
            continue;
        }
        if let Ok(entries) = std::fs::read_dir(&rep_dir) {
            for entry in entries.flatten() {
                let p = entry.path();
                let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("");
                if (name.contains(session_id) || name.contains(stripped)) && name.ends_with(".json")
                {
                    parse_json_report_telemetry(&p, &mut telemetry);
                }
            }
        }
    }

    // 3. Fallback to audit history if needed
    if telemetry.blocked_file_count == 0 && telemetry.blocked_network_count == 0 {
        if let Ok(detail) = crate::audit::history::inspect_session(session_id) {
            for d in detail.filesystem_denials {
                telemetry.blocked_file_count += d.count;
                if !telemetry.blocked_file_paths.contains(&d.path) {
                    telemetry.blocked_file_paths.push(d.path);
                }
            }
            for n in detail.blocked_network {
                telemetry.blocked_network_count += n.count;
                if !telemetry
                    .blocked_network_destinations
                    .contains(&n.destination)
                {
                    telemetry.blocked_network_destinations.push(n.destination);
                }
            }
        }
    }

    telemetry.blocked_file_paths.sort();
    telemetry.blocked_file_paths.dedup();
    telemetry.blocked_network_destinations.sort();
    telemetry.blocked_network_destinations.dedup();
    telemetry.allowed_egress.sort();
    telemetry.allowed_egress.dedup();

    telemetry
}

fn parse_jsonl_telemetry(path: &Path, telemetry: &mut SecurityTelemetry) {
    let Ok(file) = File::open(path) else {
        return;
    };
    let reader = BufReader::new(file);

    for line in reader.lines().map_while(Result::ok) {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        let Ok(val) = serde_json::from_str::<serde_json::Value>(trimmed) else {
            continue;
        };

        let event_type = val
            .get("event")
            .or_else(|| val.get("type"))
            .and_then(|v| v.as_str())
            .unwrap_or("");

        match event_type {
            "blocked_attempt" | "BlockedAttempt" => {
                let path = val.get("path").and_then(|v| v.as_str()).unwrap_or("");
                let source = val.get("source").and_then(|v| v.as_str()).unwrap_or("");
                let count = val.get("count").and_then(|v| v.as_u64()).unwrap_or(1);
                let is_syscall = source.contains("seccomp")
                    || path.starts_with("syscall:")
                    || source == "syscall";

                if !is_syscall && !path.is_empty() {
                    telemetry.blocked_file_count += count;
                    let path_s = path.to_string();
                    if !telemetry.blocked_file_paths.contains(&path_s) {
                        telemetry.blocked_file_paths.push(path_s);
                    }
                }
            }
            "net_request" | "NetRequest" => {
                let host = val.get("host").and_then(|v| v.as_str()).unwrap_or("");
                let port = val.get("port").and_then(|v| v.as_u64()).unwrap_or(0) as u16;
                let allowed = val.get("allowed").and_then(|v| v.as_bool()).unwrap_or(true);

                if !allowed {
                    telemetry.blocked_network_count += 1;
                    let dest = if port > 0 {
                        format!("{host}:{port}")
                    } else {
                        host.to_string()
                    };
                    if !dest.is_empty() && !telemetry.blocked_network_destinations.contains(&dest) {
                        telemetry.blocked_network_destinations.push(dest);
                    }
                } else if !host.is_empty() {
                    let host_s = host.to_string();
                    if !telemetry.allowed_egress.contains(&host_s) {
                        telemetry.allowed_egress.push(host_s);
                    }
                }
            }
            "net_quota_exceeded" | "NetQuotaExceeded" => {
                let host = val.get("host").and_then(|v| v.as_str()).unwrap_or("");
                telemetry.blocked_network_count += 1;
                if !host.is_empty() {
                    let host_s = host.to_string();
                    if !telemetry.blocked_network_destinations.contains(&host_s) {
                        telemetry.blocked_network_destinations.push(host_s);
                    }
                }
            }
            "dns_resolution" | "DnsResolution" | "egress_observed" | "EgressObserved" => {
                let host = val.get("host").and_then(|v| v.as_str()).unwrap_or("");
                if !host.is_empty() {
                    let host_s = host.to_string();
                    if !telemetry.allowed_egress.contains(&host_s) {
                        telemetry.allowed_egress.push(host_s);
                    }
                }
            }
            _ => {}
        }
    }
}

fn parse_json_report_telemetry(path: &Path, telemetry: &mut SecurityTelemetry) {
    let Ok(content) = std::fs::read_to_string(path) else {
        return;
    };
    let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) else {
        return;
    };

    if let Some(arr) = val.get("blocked_attempts").and_then(|v| v.as_array()) {
        for b in arr {
            let p = b.get("path").and_then(|v| v.as_str()).unwrap_or("");
            let count = b.get("count").and_then(|v| v.as_u64()).unwrap_or(1);
            if !p.is_empty() {
                telemetry.blocked_file_count += count;
                let path_s = p.to_string();
                if !telemetry.blocked_file_paths.contains(&path_s) {
                    telemetry.blocked_file_paths.push(path_s);
                }
            }
        }
    }

    if let Some(arr) = val.get("net_requests").and_then(|v| v.as_array()) {
        for n in arr {
            let host = n.get("host").and_then(|v| v.as_str()).unwrap_or("");
            let port = n.get("port").and_then(|v| v.as_u64()).unwrap_or(0) as u16;
            let allowed = n.get("allowed").and_then(|v| v.as_bool()).unwrap_or(true);
            if !allowed {
                telemetry.blocked_network_count += 1;
                let dest = if port > 0 {
                    format!("{host}:{port}")
                } else {
                    host.to_string()
                };
                if !dest.is_empty() && !telemetry.blocked_network_destinations.contains(&dest) {
                    telemetry.blocked_network_destinations.push(dest);
                }
            } else if !host.is_empty() {
                let host_s = host.to_string();
                if !telemetry.allowed_egress.contains(&host_s) {
                    telemetry.allowed_egress.push(host_s);
                }
            }
        }
    }

    if let Some(map) = val.get("network_summary").and_then(|v| v.as_object()) {
        for k in map.keys() {
            if !k.is_empty() && !telemetry.allowed_egress.contains(k) {
                telemetry.allowed_egress.push(k.clone());
            }
        }
    }

    if let Some(arr) = val.get("egress_connections").and_then(|v| v.as_array()) {
        for e in arr {
            let host = e.get("host").and_then(|v| v.as_str()).unwrap_or("");
            if !host.is_empty() {
                let host_s = host.to_string();
                if !telemetry.allowed_egress.contains(&host_s) {
                    telemetry.allowed_egress.push(host_s);
                }
            }
        }
    }
}

fn is_binary(bytes: &[u8]) -> bool {
    let probe_len = bytes.len().min(8192);
    if bytes[..probe_len].contains(&0) {
        return true;
    }
    std::str::from_utf8(bytes).is_err()
}

fn matches_path_filter(file_path: &str, filter: Option<&str>) -> bool {
    let Some(filter) = filter else {
        return true;
    };
    let norm_filter = filter
        .trim()
        .trim_start_matches("./")
        .trim_start_matches('/');
    if norm_filter.is_empty() {
        return true;
    }
    file_path == norm_filter
        || file_path.starts_with(&format!("{norm_filter}/"))
        || Path::new(file_path).starts_with(Path::new(norm_filter))
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DiffOp<'a> {
    Equal(&'a str),
    Delete(&'a str),
    Insert(&'a str),
}

fn format_unified_diff(
    path: &str,
    old_lines: &[&str],
    new_lines: &[&str],
    is_added: bool,
    is_deleted: bool,
) -> (usize, usize, String, String) {
    if is_added {
        let lines_added = new_lines.len();
        let mut color_patch = format!(
            "\x1b[1;37m--- /dev/null\n+++ b/{path}\x1b[0m\n\
             \x1b[36m@@ -0,0 +1,{lines_added} @@\x1b[0m\n"
        );
        let mut plain_patch = format!(
            "--- /dev/null\n+++ b/{path}\n\
             @@ -0,0 +1,{lines_added} @@\n"
        );
        for l in new_lines {
            color_patch.push_str("\x1b[32m+");
            color_patch.push_str(l);
            color_patch.push_str("\x1b[0m\n");
            plain_patch.push('+');
            plain_patch.push_str(l);
            plain_patch.push('\n');
        }
        return (lines_added, 0, color_patch, plain_patch);
    }

    if is_deleted {
        let lines_deleted = old_lines.len();
        let mut color_patch = format!(
            "\x1b[1;37m--- a/{path}\n+++ /dev/null\x1b[0m\n\
             \x1b[36m@@ -1,{lines_deleted} +0,0 @@\x1b[0m\n"
        );
        let mut plain_patch = format!(
            "--- a/{path}\n+++ /dev/null\n\
             @@ -1,{lines_deleted} +0,0 @@\n"
        );
        for l in old_lines {
            color_patch.push_str("\x1b[31m-");
            color_patch.push_str(l);
            color_patch.push_str("\x1b[0m\n");
            plain_patch.push('-');
            plain_patch.push_str(l);
            plain_patch.push('\n');
        }
        return (0, lines_deleted, color_patch, plain_patch);
    }

    let ops = compute_diff_ops(old_lines, new_lines);
    let lines_added = ops
        .iter()
        .filter(|op| matches!(op, DiffOp::Insert(_)))
        .count();
    let lines_deleted = ops
        .iter()
        .filter(|op| matches!(op, DiffOp::Delete(_)))
        .count();

    if lines_added == 0 && lines_deleted == 0 {
        return (0, 0, String::new(), String::new());
    }

    let (color_body, plain_body) = render_hunks(&ops);
    let color_patch = format!("\x1b[1;37m--- a/{path}\n+++ b/{path}\x1b[0m\n{color_body}");
    let plain_patch = format!("--- a/{path}\n+++ b/{path}\n{plain_body}");

    (lines_added, lines_deleted, color_patch, plain_patch)
}

fn compute_diff_ops<'a>(old_lines: &[&'a str], new_lines: &[&'a str]) -> Vec<DiffOp<'a>> {
    let n = old_lines.len();
    let m = new_lines.len();

    let mut prefix_len = 0;
    while prefix_len < n && prefix_len < m && old_lines[prefix_len] == new_lines[prefix_len] {
        prefix_len += 1;
    }

    let mut suffix_len = 0;
    while suffix_len < (n - prefix_len)
        && suffix_len < (m - prefix_len)
        && old_lines[n - 1 - suffix_len] == new_lines[m - 1 - suffix_len]
    {
        suffix_len += 1;
    }

    let a = &old_lines[prefix_len..n - suffix_len];
    let b = &new_lines[prefix_len..m - suffix_len];
    let len_a = a.len();
    let len_b = b.len();

    let mut middle_ops = Vec::new();

    if len_a == 0 {
        for &line in b {
            middle_ops.push(DiffOp::Insert(line));
        }
    } else if len_b == 0 {
        for &line in a {
            middle_ops.push(DiffOp::Delete(line));
        }
    } else {
        let max_edits = len_a + len_b;
        let limit_d = max_edits.min(2000);
        let offset = max_edits as isize;
        let mut v = vec![0usize; 2 * max_edits + 1];
        let mut trace = Vec::with_capacity(limit_d + 1);
        let mut solved_d = None;

        for d in 0..=limit_d {
            trace.push(v.clone());
            let mut k = -(d as isize);
            while k <= d as isize {
                let k_idx = (k + offset) as usize;
                let mut x = if k == -(d as isize)
                    || (k != d as isize
                        && v[(k - 1 + offset) as usize] < v[(k + 1 + offset) as usize])
                {
                    v[(k + 1 + offset) as usize]
                } else {
                    v[(k - 1 + offset) as usize] + 1
                };
                let mut y = (x as isize - k) as usize;
                while x < len_a && y < len_b && a[x] == b[y] {
                    x += 1;
                    y += 1;
                }
                v[k_idx] = x;
                if x >= len_a && y >= len_b {
                    solved_d = Some(d);
                    break;
                }
                k += 2;
            }
            if solved_d.is_some() {
                break;
            }
        }

        if let Some(mut d) = solved_d {
            let mut x = len_a;
            let mut y = len_b;
            while d > 0 {
                let k = x as isize - y as isize;
                let prev_v = &trace[d];
                let prev_k = if k == -(d as isize)
                    || (k != d as isize
                        && prev_v[(k - 1 + offset) as usize] < prev_v[(k + 1 + offset) as usize])
                {
                    k + 1
                } else {
                    k - 1
                };
                let prev_x = prev_v[(prev_k + offset) as usize];
                let prev_y = (prev_x as isize - prev_k) as usize;

                while x > prev_x && y > prev_y {
                    middle_ops.push(DiffOp::Equal(a[x - 1]));
                    x -= 1;
                    y -= 1;
                }
                if x == prev_x {
                    middle_ops.push(DiffOp::Insert(b[y - 1]));
                    y -= 1;
                } else {
                    middle_ops.push(DiffOp::Delete(a[x - 1]));
                    x -= 1;
                }
                d -= 1;
            }
            while x > 0 && y > 0 {
                middle_ops.push(DiffOp::Equal(a[x - 1]));
                x -= 1;
                y -= 1;
            }
            middle_ops.reverse();
        } else {
            for &line in a {
                middle_ops.push(DiffOp::Delete(line));
            }
            for &line in b {
                middle_ops.push(DiffOp::Insert(line));
            }
        }
    }

    let mut ops = Vec::with_capacity(n + m);
    for &line in &old_lines[..prefix_len] {
        ops.push(DiffOp::Equal(line));
    }
    ops.extend(middle_ops);
    for &line in &old_lines[n - suffix_len..] {
        ops.push(DiffOp::Equal(line));
    }

    ops
}

fn render_hunks(ops: &[DiffOp]) -> (String, String) {
    let mut change_indices = Vec::new();
    for (i, op) in ops.iter().enumerate() {
        if !matches!(op, DiffOp::Equal(_)) {
            change_indices.push(i);
        }
    }

    if change_indices.is_empty() {
        return (String::new(), String::new());
    }

    let mut clusters: Vec<(usize, usize)> = Vec::new();
    let mut cur_start = change_indices[0];
    let mut cur_end = change_indices[0];

    for &idx in &change_indices[1..] {
        if idx <= cur_end + 6 {
            cur_end = idx;
        } else {
            clusters.push((cur_start, cur_end));
            cur_start = idx;
            cur_end = idx;
        }
    }
    clusters.push((cur_start, cur_end));

    let mut color_out = String::new();
    let mut plain_out = String::new();

    for (c_start, c_end) in clusters {
        let h_start = c_start.saturating_sub(3);
        let h_end = (c_end + 4).min(ops.len());

        let old_start = 1 + ops[..h_start]
            .iter()
            .filter(|op| matches!(op, DiffOp::Equal(_) | DiffOp::Delete(_)))
            .count();
        let new_start = 1 + ops[..h_start]
            .iter()
            .filter(|op| matches!(op, DiffOp::Equal(_) | DiffOp::Insert(_)))
            .count();

        let old_count = ops[h_start..h_end]
            .iter()
            .filter(|op| matches!(op, DiffOp::Equal(_) | DiffOp::Delete(_)))
            .count();
        let new_count = ops[h_start..h_end]
            .iter()
            .filter(|op| matches!(op, DiffOp::Equal(_) | DiffOp::Insert(_)))
            .count();

        let _ = writeln!(
            color_out,
            "\x1b[36m@@ -{old_start},{old_count} +{new_start},{new_count} @@\x1b[0m"
        );
        let _ = writeln!(
            plain_out,
            "@@ -{old_start},{old_count} +{new_start},{new_count} @@"
        );

        for op in &ops[h_start..h_end] {
            match op {
                DiffOp::Equal(l) => {
                    color_out.push(' ');
                    color_out.push_str(l);
                    color_out.push('\n');
                    plain_out.push(' ');
                    plain_out.push_str(l);
                    plain_out.push('\n');
                }
                DiffOp::Delete(l) => {
                    color_out.push_str("\x1b[31m-");
                    color_out.push_str(l);
                    color_out.push_str("\x1b[0m\n");
                    plain_out.push('-');
                    plain_out.push_str(l);
                    plain_out.push('\n');
                }
                DiffOp::Insert(l) => {
                    color_out.push_str("\x1b[32m+");
                    color_out.push_str(l);
                    color_out.push_str("\x1b[0m\n");
                    plain_out.push('+');
                    plain_out.push_str(l);
                    plain_out.push('\n');
                }
            }
        }
    }

    (color_out, plain_out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn test_diff_empty_or_no_snapshot() {
        let args = DiffArgs {
            session_id: Some("nonexistent_session_id_404".to_string()),
            stat: false,
            json: false,
            path: None,
        };
        let result = run_diff(&args);
        assert!(
            result.is_ok(),
            "missing snapshot should be handled gracefully"
        );

        let args_none = DiffArgs {
            session_id: None,
            stat: true,
            json: true,
            path: None,
        };
        // The latest-snapshot branch depends on the shared per-user store
        // ($HOME/.vetto), which parallel tests and real dev-machine usage
        // pollute — asserting through run_diff here would be non-hermetic.
        // Exercise the resolver directly against a fresh empty store instead.
        let tmp = std::env::temp_dir().join(format!(
            "vetto-diff-test-store-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        std::fs::create_dir_all(&tmp).expect("empty snapshot store");
        let result_none = resolve_session_and_snapshot_in(&tmp, args_none.session_id.as_deref())
            .expect("empty store resolves");
        assert!(
            result_none.is_none(),
            "empty snapshot store must resolve to None"
        );
        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn test_diff_snapshot_comparison() {
        let temp_dir = std::env::temp_dir().join(format!(
            "vetto-diff-test-{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&temp_dir).unwrap();

        let initial_path = temp_dir.join("initial.txt");
        let removed_path = temp_dir.join("removed.txt");
        std::fs::write(&initial_path, "line 1\nline 2\nline 3\n").unwrap();
        std::fs::write(&removed_path, "delete me\n").unwrap();

        let archive_path = temp_dir.join("snapshot.tar");
        let mut tar_file = std::fs::File::create(&archive_path).unwrap();
        snapshot::write_tar_entry(
            &mut tar_file,
            "initial.txt",
            b"line 1\nline 2\nline 3\n",
            std::time::SystemTime::now(),
        )
        .unwrap();
        snapshot::write_tar_entry(
            &mut tar_file,
            "removed.txt",
            b"delete me\n",
            std::time::SystemTime::now(),
        )
        .unwrap();
        tar_file.write_all(&[0u8; 1024]).unwrap();
        tar_file.flush().unwrap();
        drop(tar_file);

        // Modify initial.txt: change line 2, append line 4
        std::fs::write(
            &initial_path,
            "line 1\nline 2 modified\nline 3\nline 4 added\n",
        )
        .unwrap();
        // Remove removed.txt
        std::fs::remove_file(&removed_path).unwrap();
        // Add created.txt
        let created_path = temp_dir.join("created.txt");
        std::fs::write(&created_path, "new file\nsecond line\n").unwrap();

        let telemetry = SecurityTelemetry::default();
        let review = compare_snapshot_against_disk(
            &archive_path,
            &temp_dir,
            None,
            &telemetry,
            "test_session_123",
        )
        .unwrap();

        assert_eq!(review.files.len(), 3);

        let added_file = review
            .files
            .iter()
            .find(|f| f.path == "created.txt")
            .unwrap();
        assert_eq!(added_file.change_type, ChangeType::Added);
        assert_eq!(added_file.lines_added, 2);
        assert_eq!(added_file.lines_deleted, 0);

        let modified_file = review
            .files
            .iter()
            .find(|f| f.path == "initial.txt")
            .unwrap();
        assert_eq!(modified_file.change_type, ChangeType::Modified);
        assert_eq!(modified_file.lines_added, 2);
        assert_eq!(modified_file.lines_deleted, 1);

        let deleted_file = review
            .files
            .iter()
            .find(|f| f.path == "removed.txt")
            .unwrap();
        assert_eq!(deleted_file.change_type, ChangeType::Deleted);
        assert_eq!(deleted_file.lines_added, 0);
        assert_eq!(deleted_file.lines_deleted, 1);

        // Test with path filter
        let filtered_review = compare_snapshot_against_disk(
            &archive_path,
            &temp_dir,
            Some("initial.txt"),
            &telemetry,
            "test_session_123",
        )
        .unwrap();
        assert_eq!(filtered_review.files.len(), 1);
        assert_eq!(filtered_review.files[0].path, "initial.txt");

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_diff_json_output() {
        let telemetry = SecurityTelemetry {
            blocked_file_count: 2,
            blocked_file_paths: vec!["/etc/shadow".to_string(), "/root/.ssh/id_rsa".to_string()],
            blocked_network_count: 1,
            blocked_network_destinations: vec!["1.2.3.4:443".to_string()],
            allowed_egress: vec!["api.anthropic.com".to_string()],
        };

        let review = SessionReview {
            session_id: "session-test-456".to_string(),
            project_dir: PathBuf::from("/home/user/myproject"),
            security: telemetry,
            files: vec![
                FileChange {
                    path: "src/main.rs".to_string(),
                    change_type: ChangeType::Modified,
                    lines_added: 5,
                    lines_deleted: 2,
                    is_binary: false,
                    color_patch: String::new(),
                    patch: "--- a/src/main.rs\n+++ b/src/main.rs\n".to_string(),
                },
                FileChange {
                    path: "scratch.py".to_string(),
                    change_type: ChangeType::Deleted,
                    lines_added: 0,
                    lines_deleted: 10,
                    is_binary: false,
                    color_patch: String::new(),
                    patch: "--- a/scratch.py\n+++ /dev/null\n".to_string(),
                },
            ],
        };

        let json_val = review.to_json(true);
        let serialized = serde_json::to_string_pretty(&json_val).unwrap();

        let parsed: serde_json::Value = serde_json::from_str(&serialized).unwrap();
        assert_eq!(parsed["session_id"], "session-test-456");
        assert_eq!(parsed["project_dir"], "/home/user/myproject");
        assert_eq!(parsed["security"]["blocked_file_reads"], 2);
        assert_eq!(parsed["security"]["blocked_file_paths"][0], "/etc/shadow");
        assert_eq!(parsed["security"]["blocked_network"], 1);
        assert_eq!(
            parsed["security"]["blocked_network_destinations"][0],
            "1.2.3.4:443"
        );
        assert_eq!(parsed["security"]["allowed_egress"][0], "api.anthropic.com");

        assert_eq!(parsed["files"]["total_changed"], 2);
        assert_eq!(parsed["files"]["total_lines_added"], 5);
        assert_eq!(parsed["files"]["total_lines_deleted"], 12);
        assert_eq!(parsed["files"]["modified"].as_array().unwrap().len(), 1);
        assert_eq!(parsed["files"]["deleted"].as_array().unwrap().len(), 1);
        assert_eq!(parsed["files"]["added"].as_array().unwrap().len(), 0);
        assert_eq!(parsed["diffs"].as_array().unwrap().len(), 2);
    }
}