selfware 0.6.3

Your personal AI workshop — software you own, software that lasts
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
use super::*;
use tempfile::tempdir;

#[test]
fn test_task_checkpoint_new() {
    let checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    assert_eq!(checkpoint.task_id, "task_123");
    assert_eq!(checkpoint.task_description, "Test task");
    assert_eq!(checkpoint.status, TaskStatus::InProgress);
    assert_eq!(checkpoint.current_step, 0);
    assert_eq!(checkpoint.current_iteration, 0);
}

#[test]
fn test_task_checkpoint_to_summary() {
    let checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    let summary = checkpoint.to_summary();
    assert_eq!(summary.task_id, "task_123");
    assert_eq!(summary.task_description, "Test task");
    assert_eq!(summary.status, TaskStatus::InProgress);
}

#[test]
fn test_task_checkpoint_log_tool_call() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    let log = ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "file_read".to_string(),
        arguments: r#"{"path": "test.txt"}"#.to_string(),
        result: Some("content".to_string()),
        success: true,
        duration_ms: Some(100),
    };
    checkpoint.log_tool_call(log);
    assert_eq!(checkpoint.tool_calls.len(), 1);
}

#[test]
fn test_task_checkpoint_log_error() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    checkpoint.log_error(1, "Test error".to_string(), true);
    assert_eq!(checkpoint.errors.len(), 1);
    assert!(checkpoint.errors[0].recovered);
}

#[test]
fn test_task_checkpoint_set_step() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    checkpoint.set_step(5);
    assert_eq!(checkpoint.current_step, 5);
}

#[test]
fn test_task_checkpoint_set_iteration() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    checkpoint.set_iteration(12);
    assert_eq!(checkpoint.current_iteration, 12);
}

#[test]
fn test_task_checkpoint_set_status() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    checkpoint.set_status(TaskStatus::Completed);
    assert_eq!(checkpoint.status, TaskStatus::Completed);
}

#[test]
fn test_task_status_serde() {
    let status = TaskStatus::InProgress;
    let json = serde_json::to_string(&status).unwrap();
    assert_eq!(json, "\"in_progress\"");

    let parsed: TaskStatus = serde_json::from_str(&json).unwrap();
    assert_eq!(parsed, TaskStatus::InProgress);
}

#[test]
#[cfg(unix)]
fn checkpoint_dir_0700_and_files_0600() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempdir().unwrap();
    let cpdir = dir.path().join("cps");
    let manager = CheckpointManager::new(cpdir.clone()).unwrap();

    let dmode = fs::metadata(&cpdir).unwrap().permissions().mode() & 0o777;
    assert_eq!(dmode, 0o700, "dir should be 0700, got {:o}", dmode);

    let cp = TaskCheckpoint::new("perm-test".to_string(), "d".to_string());
    manager.save(&cp).unwrap();

    let file = fs::read_dir(&cpdir)
        .unwrap()
        .filter_map(|e| e.ok())
        .find(|e| e.path().extension().is_some_and(|x| x == "json"))
        .expect("a checkpoint json file should exist");
    let fmode = fs::metadata(file.path()).unwrap().permissions().mode() & 0o777;
    assert_eq!(
        fmode, 0o600,
        "checkpoint file should be 0600, got {:o}",
        fmode
    );
}

#[test]
fn test_checkpoint_manager_new() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();
    assert!(manager.checkpoints_dir().exists());
}

#[test]
fn test_checkpoint_manager_save_load() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    manager.save(&checkpoint).unwrap();

    let loaded = manager.load("task_123").unwrap();
    assert_eq!(loaded.task_id, "task_123");
    assert_eq!(loaded.task_description, "Test task");
}

#[test]
fn test_checkpoint_manager_list_tasks() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let checkpoint1 = TaskCheckpoint::new("task_1".to_string(), "Task 1".to_string());
    let checkpoint2 = TaskCheckpoint::new("task_2".to_string(), "Task 2".to_string());

    manager.save(&checkpoint1).unwrap();
    manager.save(&checkpoint2).unwrap();

    let tasks = manager.list_tasks().unwrap();
    assert_eq!(tasks.len(), 2);
}

#[test]
fn test_checkpoint_manager_delete() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    manager.save(&checkpoint).unwrap();
    assert!(manager.exists("task_123"));

    manager.delete("task_123").unwrap();
    assert!(!manager.exists("task_123"));
}

#[test]
fn test_checkpoint_manager_exists() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    assert!(!manager.exists("nonexistent"));

    let checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    manager.save(&checkpoint).unwrap();
    assert!(manager.exists("task_123"));
}

#[test]
fn test_checkpoint_serialization_round_trip() {
    let mut checkpoint = TaskCheckpoint::new("task_123".to_string(), "Test task".to_string());
    checkpoint.set_step(5);
    checkpoint.set_iteration(9);
    checkpoint.set_status(TaskStatus::Paused);
    checkpoint.messages.push(Message::user("Hello"));
    checkpoint.log_tool_call(ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "file_read".to_string(),
        arguments: "{}".to_string(),
        result: Some("content".to_string()),
        success: true,
        duration_ms: Some(50),
    });

    let json = serde_json::to_string_pretty(&checkpoint).unwrap();
    let loaded: TaskCheckpoint = serde_json::from_str(&json).unwrap();

    assert_eq!(loaded.task_id, checkpoint.task_id);
    assert_eq!(loaded.current_step, 5);
    assert_eq!(loaded.current_iteration, 9);
    assert_eq!(loaded.status, TaskStatus::Paused);
    assert_eq!(loaded.messages.len(), 1);
    assert_eq!(loaded.tool_calls.len(), 1);
}

#[test]
fn budget_caps_survive_serialization_round_trip() {
    let mut checkpoint = TaskCheckpoint::new("task_caps".to_string(), "Capped task".to_string());
    checkpoint.max_budget_tokens = Some(500_000);
    checkpoint.max_wall_secs = Some(21_600);
    checkpoint.max_cost_usd = Some(4.50);

    let json = serde_json::to_string(&checkpoint).unwrap();
    let loaded: TaskCheckpoint = serde_json::from_str(&json).unwrap();

    assert_eq!(loaded.max_budget_tokens, Some(500_000));
    assert_eq!(loaded.max_wall_secs, Some(21_600));
    assert_eq!(loaded.max_cost_usd, Some(4.50));
}

#[test]
fn budget_caps_default_to_none_on_legacy_checkpoint() {
    // A checkpoint written before the caps existed (no cap keys in the JSON)
    // must deserialize with the caps as None via serde(default), not fail.
    let checkpoint = TaskCheckpoint::new("old".to_string(), "d".to_string());
    let mut value = serde_json::to_value(&checkpoint).unwrap();
    let obj = value.as_object_mut().unwrap();
    obj.remove("max_budget_tokens");
    obj.remove("max_wall_secs");
    obj.remove("max_cost_usd");

    let loaded: TaskCheckpoint = serde_json::from_value(value).unwrap();
    assert_eq!(loaded.max_budget_tokens, None);
    assert_eq!(loaded.max_wall_secs, None);
    assert_eq!(loaded.max_cost_usd, None);
}

#[test]
fn test_checkpoint_deserialize_without_iteration_defaults_zero() {
    let json = r#"{
            "task_id":"task_old",
            "task_description":"legacy",
            "created_at":"2026-01-01T00:00:00Z",
            "updated_at":"2026-01-01T00:00:00Z",
            "status":"in_progress",
            "current_step":2,
            "messages":[],
            "memory_entries":[],
            "estimated_tokens":0,
            "tool_calls":[],
            "errors":[],
            "git_checkpoint":null
        }"#;

    let loaded: TaskCheckpoint = serde_json::from_str(json).unwrap();
    assert_eq!(loaded.current_step, 2);
    assert_eq!(loaded.current_iteration, 0);
}

#[test]
fn test_git_checkpoint_info_serde() {
    let info = GitCheckpointInfo {
        branch: "main".to_string(),
        commit_hash: "abc123".to_string(),
        dirty: true,
        staged_files: vec!["file1.rs".to_string()],
        modified_files: vec!["file2.rs".to_string()],
    };

    let json = serde_json::to_string(&info).unwrap();
    let loaded: GitCheckpointInfo = serde_json::from_str(&json).unwrap();

    assert_eq!(loaded.branch, "main");
    assert_eq!(loaded.commit_hash, "abc123");
    assert!(loaded.dirty);
}

#[test]
fn test_task_status_completed_serde() {
    let status = TaskStatus::Completed;
    let json = serde_json::to_string(&status).unwrap();
    assert_eq!(json, "\"completed\"");
}

#[test]
fn test_task_status_failed_serde() {
    let status = TaskStatus::Failed;
    let json = serde_json::to_string(&status).unwrap();
    assert_eq!(json, "\"failed\"");
}

#[test]
fn test_task_status_paused_serde() {
    let status = TaskStatus::Paused;
    let json = serde_json::to_string(&status).unwrap();
    assert_eq!(json, "\"paused\"");
}

#[test]
fn test_memory_entry_struct() {
    let entry = MemoryEntry {
        timestamp: "2024-01-01T00:00:00Z".to_string(),
        role: "user".to_string(),
        content: "test content".to_string(),
        token_estimate: 100,
    };
    assert_eq!(entry.role, "user");
    assert_eq!(entry.token_estimate, 100);
}

#[test]
fn test_tool_call_log_struct() {
    let log = ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "file_read".to_string(),
        arguments: "{}".to_string(),
        result: None,
        success: false,
        duration_ms: None,
    };
    assert_eq!(log.tool_name, "file_read");
    assert!(!log.success);
}

#[test]
fn test_error_log_struct() {
    let log = ErrorLog {
        timestamp: Utc::now(),
        step: 5,
        error: "something failed".to_string(),
        recovered: false,
    };
    assert_eq!(log.step, 5);
    assert!(!log.recovered);
}

#[test]
fn test_task_summary_struct() {
    let summary = TaskSummary {
        task_id: "abc".to_string(),
        task_description: "desc".to_string(),
        status: TaskStatus::InProgress,
        current_step: 3,
        created_at: Utc::now(),
        updated_at: Utc::now(),
        tool_call_count: 10,
        error_count: 2,
    };
    assert_eq!(summary.current_step, 3);
    assert_eq!(summary.tool_call_count, 10);
}

#[test]
fn test_checkpoint_set_messages() {
    let mut checkpoint = TaskCheckpoint::new("task_1".to_string(), "Test".to_string());
    let messages = vec![Message::system("system"), Message::user("user msg")];
    checkpoint.set_messages(messages);
    assert_eq!(checkpoint.messages.len(), 2);
}

#[test]
fn test_checkpoint_multiple_tool_calls() {
    let mut checkpoint = TaskCheckpoint::new("task_1".to_string(), "Test".to_string());
    for i in 0..5 {
        checkpoint.log_tool_call(ToolCallLog {
            timestamp: Utc::now(),
            tool_name: format!("tool_{}", i),
            arguments: "{}".to_string(),
            result: Some("ok".to_string()),
            success: true,
            duration_ms: Some(i as u64 * 10),
        });
    }
    assert_eq!(checkpoint.tool_calls.len(), 5);
}

#[test]
fn test_checkpoint_multiple_errors() {
    let mut checkpoint = TaskCheckpoint::new("task_1".to_string(), "Test".to_string());
    checkpoint.log_error(1, "error 1".to_string(), true);
    checkpoint.log_error(2, "error 2".to_string(), false);
    checkpoint.log_error(3, "error 3".to_string(), true);
    assert_eq!(checkpoint.errors.len(), 3);
    assert!(!checkpoint.errors[1].recovered);
}

#[test]
fn test_checkpoint_manager_load_nonexistent_recovers_fresh() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();
    // With recovery, loading a nonexistent task creates a fresh checkpoint
    let result = manager.load("nonexistent_task").unwrap();
    assert_eq!(result.task_id, "nonexistent_task");
    assert_eq!(result.task_description, "");
    assert_eq!(result.status, TaskStatus::InProgress);
}

#[test]
fn test_checkpoint_manager_delete_nonexistent() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();
    // Should not error when deleting nonexistent
    let result = manager.delete("nonexistent_task");
    assert!(result.is_ok());
}

#[test]
fn test_checkpoint_manager_list_empty() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();
    let tasks = manager.list_tasks().unwrap();
    assert!(tasks.is_empty());
}

#[test]
fn test_git_checkpoint_info_empty_files() {
    let info = GitCheckpointInfo {
        branch: "feature".to_string(),
        commit_hash: "def456".to_string(),
        dirty: false,
        staged_files: vec![],
        modified_files: vec![],
    };
    assert!(!info.dirty);
    assert!(info.staged_files.is_empty());
    assert!(info.modified_files.is_empty());
}

#[test]
fn test_checkpoint_with_git_state() {
    let mut checkpoint = TaskCheckpoint::new("task_1".to_string(), "Test".to_string());
    checkpoint.git_checkpoint = Some(GitCheckpointInfo {
        branch: "main".to_string(),
        commit_hash: "abc123def456".to_string(),
        dirty: true,
        staged_files: vec!["src/main.rs".to_string()],
        modified_files: vec![],
    });
    assert!(checkpoint.git_checkpoint.is_some());
    assert_eq!(checkpoint.git_checkpoint.as_ref().unwrap().branch, "main");
}

#[test]
fn test_checkpoint_estimated_tokens() {
    let mut checkpoint = TaskCheckpoint::new("task_1".to_string(), "Test".to_string());
    checkpoint.estimated_tokens = 5000;
    assert_eq!(checkpoint.estimated_tokens, 5000);
}

#[test]
fn test_checkpoint_delta_round_trip() {
    let mut base = TaskCheckpoint::new("task_delta".to_string(), "Delta test".to_string());
    base.set_messages(vec![Message::user("hello")]);
    base.set_step(1);

    let mut next = base.clone();
    next.set_iteration(2);
    next.log_tool_call(ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "file_read".to_string(),
        arguments: "{}".to_string(),
        result: Some("ok".to_string()),
        success: true,
        duration_ms: Some(10),
    });

    let delta = next.compute_delta(&base).unwrap();
    let mut hydrated = base.clone();
    hydrated.apply_delta(&delta).unwrap();

    assert_eq!(hydrated.current_iteration, next.current_iteration);
    assert_eq!(hydrated.tool_calls.len(), next.tool_calls.len());
    assert_eq!(hydrated.version, next.version);
}

#[test]
fn test_checkpoint_manager_replays_delta_log() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let mut checkpoint =
        TaskCheckpoint::new("task_delta_mgr".to_string(), "Delta manager".to_string());
    let mut large_messages = Vec::new();
    for i in 0..30 {
        large_messages.push(Message::user(format!("message-{} {}", i, "x".repeat(120))));
    }
    checkpoint.set_messages(large_messages);
    manager.save(&checkpoint).unwrap();

    checkpoint.set_step(2);
    checkpoint.set_iteration(3);
    checkpoint.log_tool_call(ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "shell_exec".to_string(),
        arguments: "{\"command\":\"true\"}".to_string(),
        result: Some("ok".to_string()),
        success: true,
        duration_ms: Some(1),
    });
    manager.save(&checkpoint).unwrap();

    let delta_path = manager.checkpoint_delta_path("task_delta_mgr").unwrap();
    assert!(delta_path.exists(), "expected delta log to exist");

    let loaded = manager.load("task_delta_mgr").unwrap();
    assert_eq!(loaded.current_step, 2);
    assert_eq!(loaded.current_iteration, 3);
    assert_eq!(loaded.tool_calls.len(), 1);
}

#[test]
fn test_capture_git_state() {
    // We're in a git repo, so this should work
    let state = capture_git_state(".");
    assert!(state.is_some());
    let state = state.unwrap();
    assert!(!state.branch.is_empty());
    assert!(!state.commit_hash.is_empty());
}

#[test]
fn test_capture_git_state_nonexistent_repo() {
    // This should return None for a non-repo directory
    let state = capture_git_state("/tmp");
    // /tmp may or may not be a git repo, so just check it doesn't panic
    // The function should handle this gracefully
    let _ = state;
}

#[test]
fn test_dirs_home_function() {
    let home = dirs_home();
    // Should return a valid path
    assert!(!home.as_os_str().is_empty());
}

#[test]
fn test_checkpoint_manager_creates_nested_dir() {
    let dir = tempdir().unwrap();
    let nested = dir.path().join("a").join("b").join("c");
    let manager = CheckpointManager::new(nested.clone()).unwrap();
    assert!(nested.exists());
    assert!(manager.checkpoints_dir().exists());
}

#[test]
fn test_checkpoint_manager_path() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();
    let expected = dir.path().join("task_test.json");
    assert_eq!(manager.checkpoint_path("task_test").unwrap(), expected);
}

#[test]
fn test_checkpoint_list_tasks_sorted_by_date() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Create checkpoints with different times
    let mut cp1 = TaskCheckpoint::new("old".to_string(), "Old task".to_string());
    cp1.updated_at = chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
        .unwrap()
        .with_timezone(&Utc);
    manager.save(&cp1).unwrap();

    let mut cp2 = TaskCheckpoint::new("new".to_string(), "New task".to_string());
    cp2.updated_at = chrono::DateTime::parse_from_rfc3339("2024-01-01T00:00:00Z")
        .unwrap()
        .with_timezone(&Utc);
    manager.save(&cp2).unwrap();

    let tasks = manager.list_tasks().unwrap();
    assert_eq!(tasks.len(), 2);
    // Most recent should be first
    assert_eq!(tasks[0].task_id, "new");
    assert_eq!(tasks[1].task_id, "old");
}

#[test]
fn test_checkpoint_list_ignores_invalid_json() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint
    let cp = TaskCheckpoint::new("valid".to_string(), "Valid".to_string());
    manager.save(&cp).unwrap();

    // Write invalid JSON file
    std::fs::write(dir.path().join("invalid.json"), "not valid json").unwrap();

    // Write non-JSON file (should be ignored by extension check)
    std::fs::write(dir.path().join("readme.txt"), "some text").unwrap();

    let tasks = manager.list_tasks().unwrap();
    // Should only have the valid checkpoint
    assert_eq!(tasks.len(), 1);
    assert_eq!(tasks[0].task_id, "valid");
}

#[test]
fn test_checkpoint_list_tasks_nonexistent_dir() {
    // Create manager then remove the directory
    let dir = tempdir().unwrap();
    let path = dir.path().to_path_buf();
    let manager = CheckpointManager::new(path.clone()).unwrap();

    // Remove the directory
    std::fs::remove_dir_all(&path).unwrap();

    // list_tasks should return empty, not error
    let tasks = manager.list_tasks().unwrap();
    assert!(tasks.is_empty());
}

#[test]
fn test_memory_entry_serde() {
    let entry = MemoryEntry {
        timestamp: "2024-01-01T00:00:00Z".to_string(),
        role: "assistant".to_string(),
        content: "Hello there".to_string(),
        token_estimate: 50,
    };
    let json = serde_json::to_string(&entry).unwrap();
    let loaded: MemoryEntry = serde_json::from_str(&json).unwrap();
    assert_eq!(loaded.role, "assistant");
    assert_eq!(loaded.token_estimate, 50);
}

#[test]
fn test_tool_call_log_serde() {
    let log = ToolCallLog {
        timestamp: Utc::now(),
        tool_name: "shell_exec".to_string(),
        arguments: r#"{"command": "ls"}"#.to_string(),
        result: Some("file1\nfile2".to_string()),
        success: true,
        duration_ms: Some(150),
    };
    let json = serde_json::to_string(&log).unwrap();
    let loaded: ToolCallLog = serde_json::from_str(&json).unwrap();
    assert_eq!(loaded.tool_name, "shell_exec");
    assert!(loaded.success);
}

#[test]
fn test_error_log_serde() {
    let log = ErrorLog {
        timestamp: Utc::now(),
        step: 10,
        error: "connection timeout".to_string(),
        recovered: true,
    };
    let json = serde_json::to_string(&log).unwrap();
    let loaded: ErrorLog = serde_json::from_str(&json).unwrap();
    assert_eq!(loaded.step, 10);
    assert!(loaded.recovered);
}

// ---- Checkpoint integrity tests ----

#[test]
fn test_checkpoint_envelope_round_trip() {
    let payload = serde_json::json!({"task_id": "test", "data": "hello"});
    let envelope = CheckpointEnvelope::wrap(payload.clone()).unwrap();
    assert!(!envelope.sha256.is_empty());
    assert_eq!(envelope.payload, payload);
    assert!(envelope.verify().is_ok());
}

#[test]
fn test_checkpoint_envelope_detects_tampering() {
    let payload = serde_json::json!({"task_id": "test", "data": "hello"});
    let mut envelope = CheckpointEnvelope::wrap(payload).unwrap();
    // Tamper with the payload
    envelope.payload = serde_json::json!({"task_id": "test", "data": "TAMPERED"});
    assert!(envelope.verify().is_err());
}

#[test]
fn test_checkpoint_envelope_detects_bad_hash() {
    let payload = serde_json::json!({"task_id": "test"});
    let mut envelope = CheckpointEnvelope::wrap(payload).unwrap();
    // Corrupt the hash
    envelope.sha256 =
        "0000000000000000000000000000000000000000000000000000000000000000".to_string();
    assert!(envelope.verify().is_err());
}

#[test]
fn test_save_load_with_integrity() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let checkpoint =
        TaskCheckpoint::new("integrity_test".to_string(), "Integrity test".to_string());
    manager.save(&checkpoint).unwrap();

    // Load should succeed and verify integrity
    let loaded = manager.load("integrity_test").unwrap();
    assert_eq!(loaded.task_id, "integrity_test");
    assert_eq!(loaded.task_description, "Integrity test");
}

#[test]
fn test_load_detects_corrupted_file_and_recovers() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint
    let checkpoint = TaskCheckpoint::new("corrupt_test".to_string(), "Corruption test".to_string());
    manager.save(&checkpoint).unwrap();

    // Corrupt the file by modifying the payload while keeping envelope structure
    let path = dir.path().join("corrupt_test.json");
    let content = std::fs::read_to_string(&path).unwrap();
    let mut envelope: serde_json::Value = serde_json::from_str(&content).unwrap();
    envelope["payload"]["task_description"] = serde_json::Value::String("TAMPERED".to_string());
    std::fs::write(&path, serde_json::to_string_pretty(&envelope).unwrap()).unwrap();

    // Load should detect corruption and recover with a fresh checkpoint
    // (no backup exists, so recovery creates a new empty one)
    let result = manager.load("corrupt_test").unwrap();
    assert_eq!(result.task_id, "corrupt_test");
    // The description is empty because recovery created a fresh checkpoint
    assert_eq!(result.task_description, "");
}

#[test]
fn test_try_load_from_path_detects_integrity_error() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint
    let checkpoint = TaskCheckpoint::new("direct_test".to_string(), "Direct load test".to_string());
    manager.save(&checkpoint).unwrap();

    // Corrupt the file
    let path = dir.path().join("direct_test.json");
    let content = std::fs::read_to_string(&path).unwrap();
    let mut envelope: serde_json::Value = serde_json::from_str(&content).unwrap();
    envelope["payload"]["task_description"] = serde_json::Value::String("TAMPERED".to_string());
    std::fs::write(&path, serde_json::to_string_pretty(&envelope).unwrap()).unwrap();

    // try_load_from_path should fail with integrity error
    let result = manager.try_load_from_path(&path);
    assert!(result.is_err());
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("integrity"),
        "Expected integrity error, got: {}",
        err_msg
    );
}

#[test]
fn test_load_legacy_format_backward_compatible() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Write a legacy-format checkpoint (bare JSON without envelope)
    let checkpoint = TaskCheckpoint::new("legacy_test".to_string(), "Legacy format".to_string());
    let bare_json = serde_json::to_string_pretty(&checkpoint).unwrap();
    let path = dir.path().join("legacy_test.json");
    std::fs::write(&path, bare_json).unwrap();

    // Load should succeed via legacy fallback
    let loaded = manager.load("legacy_test").unwrap();
    assert_eq!(loaded.task_id, "legacy_test");
    assert_eq!(loaded.task_description, "Legacy format");
}

#[test]
fn test_save_redacts_secrets_in_messages() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let mut checkpoint =
        TaskCheckpoint::new("redact_test".to_string(), "Secret redaction".to_string());
    checkpoint.messages.push(Message::user(
        "Use api_key=sk-secretkey12345678901234567890 to connect",
    ));
    manager.save(&checkpoint).unwrap();

    // Read the raw file and verify secrets are redacted
    let path = dir.path().join("redact_test.json");
    let raw = std::fs::read_to_string(&path).unwrap();
    assert!(
        !raw.contains("sk-secretkey12345678901234567890"),
        "API key should have been redacted in checkpoint file"
    );
    assert!(raw.contains("[REDACTED]"));
}

#[test]
fn test_list_tasks_handles_envelope_format() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save with new envelope format
    let cp1 = TaskCheckpoint::new("env_task".to_string(), "Envelope task".to_string());
    manager.save(&cp1).unwrap();

    // Also write a legacy bare-format file
    let cp2 = TaskCheckpoint::new("bare_task".to_string(), "Bare task".to_string());
    let bare_json = serde_json::to_string_pretty(&cp2).unwrap();
    std::fs::write(dir.path().join("bare_task.json"), bare_json).unwrap();

    let tasks = manager.list_tasks().unwrap();
    assert_eq!(tasks.len(), 2);
    let ids: Vec<&str> = tasks.iter().map(|t| t.task_id.as_str()).collect();
    assert!(ids.contains(&"env_task"));
    assert!(ids.contains(&"bare_task"));
}

// ---- Corruption recovery tests ----

#[test]
fn test_recover_from_corruption_uses_backup() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint (this also creates the primary file)
    let checkpoint = TaskCheckpoint::new("recover_bak".to_string(), "Backup recovery".to_string());
    manager.save(&checkpoint).unwrap();

    // Manually create a backup copy of the valid file
    let primary = dir.path().join("recover_bak.json");
    let backup = dir.path().join("recover_bak.json.bak");
    std::fs::copy(&primary, &backup).unwrap();

    // Now corrupt the primary file
    std::fs::write(&primary, "THIS IS NOT JSON").unwrap();

    // Load should recover from the backup
    let loaded = manager.load("recover_bak").unwrap();
    assert_eq!(loaded.task_id, "recover_bak");
    assert_eq!(loaded.task_description, "Backup recovery");
}

#[test]
fn test_recover_from_corruption_creates_fresh_when_no_backup() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Write a corrupt primary file with no backup
    let primary = dir.path().join("no_bak.json");
    std::fs::write(&primary, "CORRUPT DATA").unwrap();

    // Load should create a fresh checkpoint
    let loaded = manager.load("no_bak").unwrap();
    assert_eq!(loaded.task_id, "no_bak");
    assert_eq!(loaded.task_description, "");
    assert_eq!(loaded.status, TaskStatus::InProgress);
}

#[test]
fn test_recover_from_corruption_creates_fresh_when_backup_also_corrupt() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Write corrupt primary and corrupt backup
    let primary = dir.path().join("both_bad.json");
    let backup = dir.path().join("both_bad.json.bak");
    std::fs::write(&primary, "CORRUPT").unwrap();
    std::fs::write(&backup, "ALSO CORRUPT").unwrap();

    // Load should create a fresh checkpoint
    let loaded = manager.load("both_bad").unwrap();
    assert_eq!(loaded.task_id, "both_bad");
    assert_eq!(loaded.task_description, "");
}

#[test]
fn test_recover_from_corruption_resaves_recovered() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint
    let checkpoint = TaskCheckpoint::new(
        "resave_test".to_string(),
        "Resave after recovery".to_string(),
    );
    manager.save(&checkpoint).unwrap();

    // Create backup, then corrupt primary
    let primary = dir.path().join("resave_test.json");
    let backup = dir.path().join("resave_test.json.bak");
    std::fs::copy(&primary, &backup).unwrap();
    std::fs::write(&primary, "CORRUPT").unwrap();

    // First load triggers recovery
    let loaded = manager.load("resave_test").unwrap();
    assert_eq!(loaded.task_description, "Resave after recovery");

    // Remove backup; second load should succeed from re-saved primary
    std::fs::remove_file(&backup).unwrap();
    let loaded2 = manager.load("resave_test").unwrap();
    assert_eq!(loaded2.task_description, "Resave after recovery");
}

#[test]
fn test_recover_detects_integrity_failure_and_falls_back() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a valid checkpoint
    let checkpoint = TaskCheckpoint::new(
        "integrity_recover".to_string(),
        "Integrity recovery".to_string(),
    );
    manager.save(&checkpoint).unwrap();

    // Create a good backup
    let primary = dir.path().join("integrity_recover.json");
    let backup = dir.path().join("integrity_recover.json.bak");
    std::fs::copy(&primary, &backup).unwrap();

    // Tamper with primary envelope payload (valid JSON but bad hash)
    let content = std::fs::read_to_string(&primary).unwrap();
    let mut envelope: serde_json::Value = serde_json::from_str(&content).unwrap();
    envelope["payload"]["task_description"] = serde_json::Value::String("TAMPERED".to_string());
    std::fs::write(&primary, serde_json::to_string_pretty(&envelope).unwrap()).unwrap();

    // Load should detect integrity failure and recover from backup
    let loaded = manager.load("integrity_recover").unwrap();
    assert_eq!(loaded.task_description, "Integrity recovery");
}

// ---- Retry logic tests ----

#[test]
fn test_save_with_retry_succeeds_immediately() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let checkpoint = TaskCheckpoint::new("retry_ok".to_string(), "Retry success".to_string());
    manager.save_with_retry(&checkpoint).unwrap();

    let loaded = manager.load("retry_ok").unwrap();
    assert_eq!(loaded.task_id, "retry_ok");
}

#[test]
#[cfg(not(target_os = "windows"))] // On Windows, set_readonly on directories doesn't prevent file creation inside them
fn test_save_with_retry_fails_on_readonly_dir() {
    // Create a directory and make it read-only so saves fail
    let dir = tempdir().unwrap();
    let readonly_dir = dir.path().join("readonly_checkpoints");
    std::fs::create_dir_all(&readonly_dir).unwrap();
    let manager = CheckpointManager::new(readonly_dir.clone()).unwrap();

    // Make directory read-only
    let mut perms = std::fs::metadata(&readonly_dir).unwrap().permissions();
    #[allow(clippy::permissions_set_readonly_false)]
    {
        perms.set_readonly(true);
    }
    std::fs::set_permissions(&readonly_dir, perms.clone()).unwrap();

    let checkpoint = TaskCheckpoint::new(
        "retry_fail".to_string(),
        "Should fail all retries".to_string(),
    );
    let result = manager.save_with_retry(&checkpoint);
    assert!(result.is_err());

    // Restore permissions so tempdir cleanup works
    #[allow(clippy::permissions_set_readonly_false)]
    {
        perms.set_readonly(false);
    }
    std::fs::set_permissions(&readonly_dir, perms).unwrap();
}

#[test]
#[cfg(target_os = "windows")]
fn test_save_with_retry_fails_on_readonly_dir() {
    // On Windows, directory readonly attributes don't prevent file creation.
    // Instead, we create a CheckpointManager whose base_dir is under a
    // regular file (an impossible path), so saves physically cannot succeed.
    let dir = tempdir().unwrap();
    let blocker_file = dir.path().join("blocker");
    std::fs::write(&blocker_file, "not a directory").unwrap();

    // Manually construct a manager pointing to a path that can never work:
    // "blocker" is a file, so "blocker/checkpoints" can't be a directory.
    let impossible_dir = blocker_file.join("checkpoints");
    let manager = CheckpointManager {
        checkpoints_dir: impossible_dir,
    };

    let checkpoint = TaskCheckpoint::new(
        "retry_fail".to_string(),
        "Should fail all retries".to_string(),
    );
    let result = manager.save_with_retry(&checkpoint);
    assert!(result.is_err());
}

// ---- Path traversal security tests ----

#[test]
fn test_sanitize_task_id_rejects_dotdot() {
    assert!(sanitize_task_id("../evil").is_err());
    assert!(sanitize_task_id("foo/../../bar").is_err());
    assert!(sanitize_task_id("..\\..\\evil").is_err());
    assert!(sanitize_task_id("..").is_err());
}

#[test]
fn test_sanitize_task_id_replaces_separators() {
    let id = sanitize_task_id("foo/bar").unwrap();
    assert_eq!(id, "foo_bar");
    assert!(!id.contains('/'));

    let id = sanitize_task_id("foo\\bar").unwrap();
    assert_eq!(id, "foo_bar");
    assert!(!id.contains('\\'));
}

#[test]
fn test_sanitize_task_id_preserves_safe_id() {
    let id = sanitize_task_id("task_123").unwrap();
    assert_eq!(id, "task_123");
}

#[test]
fn test_sanitize_task_id_rejects_empty() {
    assert!(sanitize_task_id("").is_err());
    assert!(sanitize_task_id("   ").is_err());
    assert!(sanitize_task_id("..").is_err());
}

#[test]
fn test_checkpoint_path_traversal_rejected() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // A task_id with "../" should be rejected, not produce a path outside dir.
    let result = manager.checkpoint_path("../evil");
    assert!(
        result.is_err(),
        "checkpoint_path should reject path traversal"
    );

    let result = manager.checkpoint_delta_path("../evil");
    assert!(
        result.is_err(),
        "checkpoint_delta_path should reject path traversal"
    );

    // Saving with a traversal task_id should also fail.
    let checkpoint = TaskCheckpoint::new("../evil".to_string(), "evil".to_string());
    let result = manager.save(&checkpoint);
    assert!(result.is_err(), "save should reject path traversal task_id");

    // Loading with a traversal task_id should also fail.
    let result = manager.load("../evil");
    assert!(result.is_err(), "load should reject path traversal task_id");

    // Deleting with a traversal task_id should also fail.
    let result = manager.delete("../evil");
    assert!(
        result.is_err(),
        "delete should reject path traversal task_id"
    );
}

#[test]
fn test_checkpoint_path_stays_in_dir() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    let path = manager.checkpoint_path("normal_task").unwrap();
    assert!(
        path.starts_with(dir.path()),
        "path should stay within checkpoints dir"
    );
    assert_eq!(path.file_name().unwrap(), "normal_task.json");
}

// ---- Retention pruning tests ----

#[test]
fn test_prune_old_checkpoints_caps_files() {
    // Use a small cap by creating many checkpoints and verifying pruning
    // keeps at most MAX_CHECKPOINT_FILES.  We can't easily change the const,
    // but we can test the pruning logic directly by creating more files
    // than the cap and calling save (which triggers pruning).
    //
    // Since MAX_CHECKPOINT_FILES is 500, we test with a smaller number
    // by creating files directly and calling the private method via save.
    // Instead, we'll verify that the prune function correctly handles
    // the case where we have exactly the cap (no deletion) and more than
    // the cap (deletion of oldest).
    //
    // For a practical test, we create a few checkpoint files with
    // controlled mtimes and verify the oldest get pruned.

    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Save a few checkpoints normally.
    for i in 0..3 {
        let cp = TaskCheckpoint::new(format!("task_{}", i), format!("Task {}", i));
        manager.save(&cp).unwrap();
        // Small delay so mtimes differ.
        std::thread::sleep(std::time::Duration::from_millis(20));
    }

    // All 3 should still exist (well under cap of 500).
    for i in 0..3 {
        assert!(
            manager.exists(&format!("task_{}", i)),
            "task_{} should still exist after pruning",
            i
        );
    }
}

#[test]
fn test_prune_old_checkpoints_deletes_oldest() {
    // This test directly exercises the pruning logic by creating more
    // checkpoint files than MAX_CHECKPOINT_FILES would allow, but since
    // we can't change the const at runtime, we instead test that
    // prune_old_checkpoints correctly identifies and would delete old
    // files.  We test the core behavior: after saving, the directory
    // doesn't grow unboundedly.
    //
    // We create checkpoint files directly and verify the pruning
    // mechanism works by simulating it with a controlled setup:
    // create many .json files, then trigger save() and verify the
    // count stays at or below the cap.
    //
    // Given MAX_CHECKPOINT_FILES=500, we create 3 checkpoints and
    // verify none are pruned.  This validates the "under cap" path.

    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Create 3 checkpoints with staggered mtimes.
    for i in 0..3 {
        let cp = TaskCheckpoint::new(format!("prune_{}", i), format!("Prune {}", i));
        manager.save(&cp).unwrap();
        std::thread::sleep(std::time::Duration::from_millis(15));
    }

    let tasks = manager.list_tasks().unwrap();
    assert_eq!(tasks.len(), 3, "all 3 checkpoints should exist");

    // Saving the same checkpoint again should not cause pruning of others.
    let cp = TaskCheckpoint::new("prune_0".to_string(), "Prune 0 updated".to_string());
    manager.save(&cp).unwrap();

    let tasks = manager.list_tasks().unwrap();
    assert_eq!(tasks.len(), 3, "still 3 checkpoints after re-save");
}

#[test]
fn test_prune_with_large_count_stays_within_cap() {
    // Create more than MAX_CHECKPOINT_FILES (500) checkpoint files and
    // verify that after a save, the count is at or below the cap.
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Create 502 checkpoint files directly (faster than full save).
    for i in 0..502 {
        let task_id = format!("bulk_{}", i);
        let path = dir.path().join(format!("{}.json", task_id));
        let cp = TaskCheckpoint::new(task_id, format!("Bulk {}", i));
        let json_value = serde_json::to_value(&cp).unwrap();
        let envelope = CheckpointEnvelope::wrap(json_value).unwrap();
        let json = serde_json::to_string_pretty(&envelope).unwrap();
        std::fs::write(&path, json).unwrap();
    }

    // Count files before pruning.
    let count_before: usize = std::fs::read_dir(dir.path())
        .unwrap()
        .filter(|e| {
            e.as_ref()
                .map(|e| e.path().extension().and_then(|s| s.to_str()) == Some("json"))
                .unwrap_or(false)
        })
        .count();
    assert_eq!(count_before, 502);

    // Save one more checkpoint — this triggers prune_old_checkpoints.
    let cp = TaskCheckpoint::new("trigger_prune".to_string(), "Trigger".to_string());
    manager.save(&cp).unwrap();

    // Count files after pruning — should be at most MAX_CHECKPOINT_FILES + 1
    // (the +1 is the just-saved "trigger_prune.json").
    let count_after: usize = std::fs::read_dir(dir.path())
        .unwrap()
        .filter(|e| {
            e.as_ref()
                .map(|e| e.path().extension().and_then(|s| s.to_str()) == Some("json"))
                .unwrap_or(false)
        })
        .count();

    assert!(
        count_after <= MAX_CHECKPOINT_FILES + 1,
        "after pruning, file count {} should be at most {} (cap + 1 for the just-saved file)",
        count_after,
        MAX_CHECKPOINT_FILES + 1
    );
    assert!(
        count_after >= MAX_CHECKPOINT_FILES,
        "after pruning, should still have at least {} files (the cap), got {}",
        MAX_CHECKPOINT_FILES,
        count_after
    );
}

#[test]
fn prune_caps_per_task_subdirs() {
    let dir = tempdir().unwrap();
    let cpdir = dir.path().join("cps");
    let manager = CheckpointManager::new(cpdir.clone()).unwrap();

    // Create more than the cap of stale per-task subdirs (as the
    // failure_mode.json artifact writer does — one per run). These are
    // exactly what the flat-.json prune ignored, so they leaked unbounded.
    let over = MAX_CHECKPOINT_FILES + 20;
    for i in 0..over {
        let d = cpdir.join(format!("task-{i:04}"));
        std::fs::create_dir_all(&d).unwrap();
        std::fs::write(d.join("failure_mode.json"), "{}").unwrap();
    }

    // A save triggers prune_old_checkpoints -> prune_old_task_dirs even
    // though there are few .json files (the early-return case).
    let cp = TaskCheckpoint::new("live-task".to_string(), "d".to_string());
    manager.save(&cp).unwrap();

    let remaining = std::fs::read_dir(&cpdir)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
        .count();
    assert!(
        remaining <= MAX_CHECKPOINT_FILES,
        "per-task subdirs must be capped at {}, found {}",
        MAX_CHECKPOINT_FILES,
        remaining
    );
}

#[test]
fn task_checkpoint_budget_fields_roundtrip_and_default() {
    let mut cp = TaskCheckpoint::new("t1".to_string(), "desc".to_string());
    cp.cumulative_tokens = 12345;
    cp.elapsed_wall_secs = 678;
    cp.cumulative_cost_usd = 1.2345;
    cp.guard_counters = GuardCounters {
        consecutive_no_action_prompts: 3,
        mutation_gate_rejections: 5,
        prefill_400_count: 2,
    };
    let json = serde_json::to_string(&cp).unwrap();
    let back: TaskCheckpoint = serde_json::from_str(&json).unwrap();
    assert_eq!(back.cumulative_tokens, 12345);
    assert_eq!(back.elapsed_wall_secs, 678);
    assert_eq!(back.cumulative_cost_usd, 1.2345);
    assert_eq!(back.guard_counters.mutation_gate_rejections, 5);
    assert_eq!(back.guard_counters.prefill_400_count, 2);

    // Legacy checkpoints without these fields must default to 0, not fail.
    let mut legacy_value =
        serde_json::to_value(TaskCheckpoint::new("t2".to_string(), "d".to_string())).unwrap();
    // Remove the new budget fields to simulate a legacy checkpoint.
    if let serde_json::Value::Object(ref mut map) = legacy_value {
        map.remove("cumulative_tokens");
        map.remove("elapsed_wall_secs");
        map.remove("cumulative_cost_usd");
        map.remove("guard_counters");
    }
    let restored: TaskCheckpoint = serde_json::from_value(legacy_value).unwrap();
    assert_eq!(restored.cumulative_tokens, 0);
    assert_eq!(restored.elapsed_wall_secs, 0);
    assert_eq!(restored.cumulative_cost_usd, 0.0);
    assert_eq!(restored.guard_counters, GuardCounters::default());
}

#[test]
fn delta_carries_cumulative_budget_across_apply() {
    let mut base = TaskCheckpoint::new("t".to_string(), "d".to_string());
    base.cumulative_tokens = 100;
    base.elapsed_wall_secs = 30;
    base.cumulative_cost_usd = 0.10;
    // A newer checkpoint that consumed more budget.
    let mut newer = base.clone();
    newer.version = base.version + 1;
    newer.current_step = base.current_step + 1;
    newer.cumulative_tokens = 500;
    newer.elapsed_wall_secs = 90;
    newer.cumulative_cost_usd = 0.75;
    newer.guard_counters = GuardCounters {
        consecutive_no_action_prompts: 2,
        mutation_gate_rejections: 4,
        prefill_400_count: 1,
    };
    let delta = newer.compute_delta(&base).expect("delta should exist");
    assert_eq!(delta.cumulative_tokens, Some(500));
    assert_eq!(delta.elapsed_wall_secs, Some(90));
    assert_eq!(delta.cumulative_cost_usd, Some(0.75));
    assert_eq!(
        delta
            .guard_counters
            .as_ref()
            .unwrap()
            .mutation_gate_rejections,
        4
    );
    // Applying the delta to the base must update the budget (not keep it stale).
    let mut reconstructed = base.clone();
    reconstructed.apply_delta(&delta).unwrap();
    assert_eq!(reconstructed.cumulative_tokens, 500);
    assert_eq!(reconstructed.elapsed_wall_secs, 90);
    assert_eq!(reconstructed.cumulative_cost_usd, 0.75);
    // Guard counters carry across the delta apply so resume can't reset them.
    assert_eq!(reconstructed.guard_counters.mutation_gate_rejections, 4);
    assert_eq!(reconstructed.guard_counters.prefill_400_count, 1);
    assert_eq!(
        reconstructed.guard_counters.consecutive_no_action_prompts,
        2
    );
}

#[test]
fn save_final_makes_base_reflect_terminal_state() {
    let dir = tempdir().unwrap();
    let manager = CheckpointManager::new(dir.path().to_path_buf()).unwrap();

    // Base written mid-run: in-progress at step 1.
    let mut cp = TaskCheckpoint::new("t-final".to_string(), "d".to_string());
    cp.set_status(TaskStatus::InProgress);
    cp.set_step(1);
    manager.save(&cp).unwrap();

    // A delta advances progress (as periodic saves do).
    cp.set_step(4);
    manager.save(&cp).unwrap();

    // Finalize: full write so the base itself is terminal.
    cp.set_status(TaskStatus::Completed);
    cp.set_step(7);
    cp.set_iteration(3);
    manager.save_final(&cp).unwrap();

    let loaded = manager.load("t-final").unwrap();
    assert_eq!(loaded.status, TaskStatus::Completed);
    assert_eq!(loaded.current_step, 7);
    assert_eq!(loaded.current_iteration, 3);
}