hf2q 0.1.10

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
//! Crash-recoverable publication for one text GGUF and its projector.
//!
//! The text GGUF is always promoted last.  A durable journal plus
//! same-filesystem backups makes every earlier state recoverable. The text
//! rename becomes the commit marker only after its destination and source
//! directories have both been synchronized.

use std::fs::{self, OpenOptions};
use std::io::Write;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};

use crate::core::paired_artifact::{
    canonical_parent, file_name, journal_path, path_entry_exists, read_pair_journal,
    sync_directory, sync_path, FileIdentity, PairArtifactError, PairJournalMember, PairLock,
    PairMemberRole, PairTextLock, PairTransactionJournal, PAIR_JOURNAL_SCHEMA_VERSION,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum PublishFailpoint {
    AfterJournal,
    AfterBackup(PairMemberRole),
    AfterPromoteRename(PairMemberRole),
    AfterPromote(PairMemberRole),
}

pub(super) struct PairWorkspace {
    transaction_id: String,
    parent: PathBuf,
    root: PathBuf,
    text: PathBuf,
}

impl PairWorkspace {
    pub(super) fn create(text: &Path) -> Result<Self, PairArtifactError> {
        recover_pending(text)?;
        let parent = canonical_parent(text)?;
        let text = parent.join(file_name(text)?);
        let transaction_id = uuid::Uuid::new_v4().to_string();
        let root = parent.join(format!(".hf2q-pair-{transaction_id}"));
        fs::create_dir(&root).map_err(|source| PairArtifactError::Io {
            path: root.clone(),
            source,
        })?;
        fs::set_permissions(&root, fs::Permissions::from_mode(0o700)).map_err(|source| {
            PairArtifactError::Io {
                path: root.clone(),
                source,
            }
        })?;
        let backup = root.join("backup");
        fs::create_dir(&backup).map_err(|source| PairArtifactError::Io {
            path: backup.clone(),
            source,
        })?;
        fs::set_permissions(&backup, fs::Permissions::from_mode(0o700)).map_err(|source| {
            PairArtifactError::Io {
                path: backup.clone(),
                source,
            }
        })?;
        sync_directory(&root)?;
        sync_directory(&parent)?;
        Ok(Self {
            transaction_id,
            parent,
            root,
            text,
        })
    }

    pub(super) fn transaction_id(&self) -> &str {
        &self.transaction_id
    }

    pub(super) fn staged_path(&self, role: PairMemberRole) -> PathBuf {
        self.root.join(role.private_name())
    }

    pub(super) fn discard_unpublished(&self) {
        if !path_entry_exists(&journal_path(&self.text)).unwrap_or(true) {
            if let Err(error) = remove_transaction_root(&self.parent, &self.root) {
                tracing::warn!(error = %error, root = %self.root.display(), "could not remove unpublished pair workspace");
            }
        }
    }

    pub(super) fn publish(
        &self,
        destinations: &[(PairMemberRole, PathBuf)],
    ) -> Result<(), PairArtifactError> {
        self.publish_inner(destinations, None, false)
    }

    #[cfg(test)]
    fn publish_crash_at(
        &self,
        destinations: &[(PairMemberRole, PathBuf)],
        failpoint: PublishFailpoint,
    ) -> Result<(), PairArtifactError> {
        self.publish_inner(destinations, Some(failpoint), true)
    }

    fn publish_inner(
        &self,
        destinations: &[(PairMemberRole, PathBuf)],
        failpoint: Option<PublishFailpoint>,
        simulate_crash: bool,
    ) -> Result<(), PairArtifactError> {
        let _lock = PairLock::exclusive(&self.text)?;
        let pre_recovery_text_lock = PairTextLock::exclusive_if_present(&self.text)?;
        recover_locked(&self.text, &self.parent)?;
        // Recovery may replace the final text inode (candidate -> prior) or
        // recreate it from backup. Release the pre-recovery inode only after
        // recovery, then lock whatever inode is current before starting the
        // new journal/mutation. A fallback reader that wins this handoff can
        // only make us wait; no publication mutation has begun yet.
        drop(pre_recovery_text_lock);
        let _current_text_lock = PairTextLock::exclusive_if_present(&self.text)?;
        let journal_file = journal_path(&self.text);
        if path_entry_exists(&journal_file)? {
            return Err(PairArtifactError::Invalid(
                "pair transaction journal still exists after recovery".into(),
            ));
        }
        validate_destination_order(destinations)?;

        let parent_metadata =
            fs::metadata(&self.parent).map_err(|source| PairArtifactError::Io {
                path: self.parent.clone(),
                source,
            })?;
        let mut members = Vec::with_capacity(destinations.len());
        for (role, destination) in destinations {
            if canonical_parent(destination)? != self.parent {
                return Err(PairArtifactError::Invalid(
                    "all pair destinations must share the canonical text directory".into(),
                ));
            }
            let staged = self.staged_path(*role);
            let candidate = FileIdentity::from_path(&staged)?;
            if let Some(candidate) = candidate.as_ref() {
                sync_path(&staged)?;
                let staged_metadata =
                    fs::symlink_metadata(&staged).map_err(|source| PairArtifactError::Io {
                        path: staged.clone(),
                        source,
                    })?;
                if !staged_metadata.is_file()
                    || staged_metadata.uid() != rustix::process::geteuid().as_raw()
                    || staged_metadata.nlink() != 1
                    || staged_metadata.dev() != parent_metadata.dev()
                    || &FileIdentity::from_path(&staged)?.ok_or_else(|| {
                        PairArtifactError::Invalid(format!(
                            "staged pair member disappeared: {}",
                            staged.display()
                        ))
                    })? != candidate
                {
                    return Err(PairArtifactError::Invalid(format!(
                        "staged pair member is not one stable owned same-filesystem regular file: {}",
                        staged.display()
                    )));
                }
            } else if matches!(*role, PairMemberRole::Projector | PairMemberRole::Text) {
                return Err(PairArtifactError::Invalid(format!(
                    "staged required pair member is missing: {}",
                    staged.display()
                )));
            }
            let final_path = self.parent.join(file_name(destination)?);
            let prior = FileIdentity::from_path(&final_path)?;
            if let Some(prior_metadata) = fs::symlink_metadata(&final_path).ok() {
                if !prior_metadata.is_file()
                    || prior_metadata.file_type().is_symlink()
                    || prior_metadata.uid() != rustix::process::geteuid().as_raw()
                    || prior_metadata.nlink() != 1
                    || prior_metadata.dev() != parent_metadata.dev()
                {
                    return Err(PairArtifactError::Invalid(format!(
                        "existing pair destination is not one owned same-filesystem regular file: {}",
                        final_path.display()
                    )));
                }
            }
            let final_name = file_name(destination)?
                .to_str()
                .ok_or_else(|| {
                    PairArtifactError::Invalid(
                        "paired conversion destinations must have UTF-8 filenames".into(),
                    )
                })?
                .to_owned();
            members.push(PairJournalMember {
                role: *role,
                final_name,
                prior,
                candidate,
            });
        }
        let journal = PairTransactionJournal {
            schema_version: PAIR_JOURNAL_SCHEMA_VERSION,
            transaction_id: self.transaction_id.clone(),
            transaction_root: file_name(&self.root)?
                .to_str()
                .ok_or_else(|| {
                    PairArtifactError::Invalid("pair transaction root is not UTF-8".into())
                })?
                .to_owned(),
            members,
        };
        journal.validate()?;
        write_journal(&journal_file, &journal)?;

        let mut text_commit_durable = false;
        let mut candidate_text_lock = None;
        let mutation_result = (|| {
            if failpoint == Some(PublishFailpoint::AfterJournal) {
                return Err(PairArtifactError::Invalid(
                    "injected failure after pair journal".into(),
                ));
            }
            for member in &journal.members {
                if member.prior.is_some() {
                    let final_path = journal.final_path(&self.parent, member);
                    let backup_path = journal.backup_path(&self.parent, member.role);
                    fs::rename(&final_path, &backup_path).map_err(|source| {
                        PairArtifactError::Io {
                            path: final_path,
                            source,
                        }
                    })?;
                    sync_directory(&self.root.join("backup"))?;
                    sync_directory(&self.parent)?;
                }
                if failpoint == Some(PublishFailpoint::AfterBackup(member.role)) {
                    return Err(PairArtifactError::Invalid(
                        "injected failure after pair backup".into(),
                    ));
                }
            }

            for member in &journal.members {
                if member.candidate.is_none() {
                    continue;
                }
                let staged = journal.staged_path(&self.parent, member.role);
                let final_path = journal.final_path(&self.parent, member);
                if member.role == PairMemberRole::Text {
                    // Lock the private candidate inode before rename. The
                    // lock follows that inode into its final name, leaving no
                    // visibility gap for read-only/cross-user readers.
                    candidate_text_lock = Some(PairTextLock::exclusive(&staged)?);
                }
                fs::rename(&staged, &final_path).map_err(|source| PairArtifactError::Io {
                    path: final_path.clone(),
                    source,
                })?;
                if failpoint == Some(PublishFailpoint::AfterPromoteRename(member.role)) {
                    return Err(PairArtifactError::Invalid(format!(
                        "injected failure after pair {:?} rename",
                        member.role
                    )));
                }
                sync_directory(&self.parent)?;
                sync_directory(&self.root)?;
                if member.role == PairMemberRole::Text {
                    text_commit_durable = true;
                }
                if failpoint == Some(PublishFailpoint::AfterPromote(member.role)) {
                    return Err(PairArtifactError::Invalid(format!(
                        "injected failure after pair {:?} promotion",
                        member.role
                    )));
                }
            }
            Ok(())
        })();

        if let Err(error) = mutation_result {
            if !simulate_crash {
                let recovery = if text_commit_durable {
                    cleanup_committed(&journal_file, &self.parent, &journal)
                } else {
                    rollback(&journal_file, &self.parent, &journal)
                };
                if let Err(recovery) = recovery {
                    return Err(PairArtifactError::Invalid(format!(
                        "pair publication failed ({error}); immediate recovery also failed ({recovery})"
                    )));
                }
            }
            return Err(error);
        }

        if let Err(error) = cleanup_committed(&journal_file, &self.parent, &journal) {
            tracing::warn!(
                error = %error,
                journal = %journal_file.display(),
                "pair committed; journal cleanup remains recoverable"
            );
        }
        drop(candidate_text_lock);
        Ok(())
    }
}

pub(super) fn recover_pending(text: &Path) -> Result<(), PairArtifactError> {
    let parent = canonical_parent(text)?;
    let text = parent.join(file_name(text)?);
    let _lock = PairLock::exclusive(&text)?;
    let _text_lock = PairTextLock::exclusive_if_present(&text)?;
    recover_locked(&text, &parent)
}

fn recover_locked(text: &Path, parent: &Path) -> Result<(), PairArtifactError> {
    let journal_file = journal_path(text);
    if !path_entry_exists(&journal_file)? {
        return Ok(());
    }
    let journal = read_pair_journal(&journal_file)?;
    validate_recovery_binding(text, &journal)?;
    if journal.committed(parent)? {
        cleanup_committed(&journal_file, parent, &journal)
    } else if journal.rolled_back(parent)? {
        cleanup_terminal(&journal_file, parent, &journal)
    } else {
        validate_recovery_root(parent, &journal)?;
        rollback(&journal_file, parent, &journal)
    }
}

fn validate_recovery_binding(
    text: &Path,
    journal: &PairTransactionJournal,
) -> Result<(), PairArtifactError> {
    let text_name = file_name(text)?
        .to_str()
        .ok_or_else(|| PairArtifactError::Invalid("pair text filename is not UTF-8".into()))?;
    if journal
        .members
        .iter()
        .find(|member| member.role == PairMemberRole::Text)
        .map(|member| member.final_name.as_str())
        != Some(text_name)
    {
        return Err(PairArtifactError::Invalid(
            "pair journal text member does not match its journal filename".into(),
        ));
    }
    Ok(())
}

fn validate_recovery_root(
    parent: &Path,
    journal: &PairTransactionJournal,
) -> Result<(), PairArtifactError> {
    let parent_metadata = fs::metadata(parent).map_err(|source| PairArtifactError::Io {
        path: parent.to_path_buf(),
        source,
    })?;
    for directory in [
        journal.root_path(parent),
        journal.root_path(parent).join("backup"),
    ] {
        let metadata =
            fs::symlink_metadata(&directory).map_err(|source| PairArtifactError::Io {
                path: directory.clone(),
                source,
            })?;
        if !metadata.is_dir()
            || metadata.uid() != rustix::process::geteuid().as_raw()
            || metadata.dev() != parent_metadata.dev()
            || metadata.mode() & 0o7777 != 0o700
        {
            return Err(PairArtifactError::Invalid(format!(
                "pair recovery directory is not an owned private same-filesystem directory: {}",
                directory.display()
            )));
        }
    }
    Ok(())
}

fn rollback(
    journal_file: &Path,
    parent: &Path,
    journal: &PairTransactionJournal,
) -> Result<(), PairArtifactError> {
    for member in journal.members.iter().rev() {
        let final_path = journal.final_path(parent, member);
        match FileIdentity::from_path(&final_path)? {
            Some(identity) if member.candidate.as_ref() == Some(&identity) => {
                fs::remove_file(&final_path).map_err(|source| PairArtifactError::Io {
                    path: final_path.clone(),
                    source,
                })?;
                sync_directory(parent)?;
            }
            Some(identity) if member.prior.as_ref() == Some(&identity) => {}
            Some(_) => {
                return Err(PairArtifactError::Invalid(format!(
                    "refusing to recover over an unknown pair member: {}",
                    final_path.display()
                )));
            }
            None => {}
        }
    }

    for member in &journal.members {
        let final_path = journal.final_path(parent, member);
        let backup_path = journal.backup_path(parent, member.role);
        match member.prior.as_ref() {
            Some(prior) => {
                if prior.matches_path(&final_path)? {
                    continue;
                }
                if !prior.matches_path(&backup_path)? {
                    return Err(PairArtifactError::Invalid(format!(
                        "pair recovery cannot find the prior {:?} member",
                        member.role
                    )));
                }
                if final_path.exists() {
                    return Err(PairArtifactError::Invalid(format!(
                        "pair recovery found an unexpected final member: {}",
                        final_path.display()
                    )));
                }
                fs::rename(&backup_path, &final_path).map_err(|source| PairArtifactError::Io {
                    path: final_path.clone(),
                    source,
                })?;
                sync_directory(parent)?;
                sync_directory(&journal.root_path(parent).join("backup"))?;
            }
            None => {
                if FileIdentity::from_path(&final_path)?.is_some()
                    || FileIdentity::from_path(&backup_path)?.is_some()
                {
                    return Err(PairArtifactError::Invalid(format!(
                        "pair recovery found a member absent from the baseline: {}",
                        final_path.display()
                    )));
                }
            }
        }
    }
    cleanup_terminal(journal_file, parent, journal)
}

fn cleanup_committed(
    journal_file: &Path,
    parent: &Path,
    journal: &PairTransactionJournal,
) -> Result<(), PairArtifactError> {
    if !journal.committed(parent)? {
        return Err(PairArtifactError::Invalid(
            "cannot clean forward an uncommitted pair transaction".into(),
        ));
    }
    cleanup_terminal(journal_file, parent, journal)
}

fn cleanup_terminal(
    journal_file: &Path,
    parent: &Path,
    journal: &PairTransactionJournal,
) -> Result<(), PairArtifactError> {
    // The journal is the recovery obligation. Remove and persist it before
    // deleting private staging/backups: a crash can then leave only a safe
    // orphan, never a journal whose required recovery state has vanished.
    remove_journal(journal_file, parent)?;
    if let Err(error) = remove_transaction_root(parent, &journal.root_path(parent)) {
        tracing::warn!(
            error = %error,
            root = %journal.root_path(parent).display(),
            "pair terminal state is durable; private transaction orphan could not be removed"
        );
    }
    Ok(())
}

fn remove_journal(path: &Path, parent: &Path) -> Result<(), PairArtifactError> {
    match fs::remove_file(path) {
        Ok(()) => sync_directory(parent),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(source) => Err(PairArtifactError::Io {
            path: path.to_path_buf(),
            source,
        }),
    }
}

fn remove_transaction_root(parent: &Path, root: &Path) -> Result<(), PairArtifactError> {
    if root.parent() != Some(parent)
        || !file_name(root)?
            .to_str()
            .is_some_and(|name| name.starts_with(".hf2q-pair-"))
    {
        return Err(PairArtifactError::Invalid(
            "refusing to remove a non-transaction directory".into(),
        ));
    }
    let metadata = match fs::symlink_metadata(root) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(source) => {
            return Err(PairArtifactError::Io {
                path: root.to_path_buf(),
                source,
            });
        }
    };
    let parent_metadata = fs::metadata(parent).map_err(|source| PairArtifactError::Io {
        path: parent.to_path_buf(),
        source,
    })?;
    if !metadata.is_dir()
        || metadata.uid() != rustix::process::geteuid().as_raw()
        || metadata.dev() != parent_metadata.dev()
        || metadata.mode() & 0o7777 != 0o700
    {
        return Err(PairArtifactError::Invalid(
            "refusing to remove an unowned pair transaction directory".into(),
        ));
    }
    fs::remove_dir_all(root).map_err(|source| PairArtifactError::Io {
        path: root.to_path_buf(),
        source,
    })?;
    sync_directory(parent)
}

fn write_journal(path: &Path, journal: &PairTransactionJournal) -> Result<(), PairArtifactError> {
    let parent = canonical_parent(path)?;
    let temporary = parent.join(format!(
        ".hf2q-pair-journal-{}.partial",
        journal.transaction_id
    ));
    let mut file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o600)
        .custom_flags(libc::O_CLOEXEC | libc::O_NOFOLLOW)
        .open(&temporary)
        .map_err(|source| PairArtifactError::Io {
            path: temporary.clone(),
            source,
        })?;
    serde_json::to_writer_pretty(&mut file, journal)?;
    file.write_all(b"\n")
        .map_err(|source| PairArtifactError::Io {
            path: temporary.clone(),
            source,
        })?;
    drop(file);
    sync_path(&temporary)?;
    fs::rename(&temporary, path).map_err(|source| PairArtifactError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    sync_directory(&parent)
}

fn validate_destination_order(
    destinations: &[(PairMemberRole, PathBuf)],
) -> Result<(), PairArtifactError> {
    if destinations.is_empty()
        || destinations.last().map(|(role, _)| *role) != Some(PairMemberRole::Text)
        || !destinations
            .iter()
            .any(|(role, _)| *role == PairMemberRole::Projector)
    {
        return Err(PairArtifactError::Invalid(
            "pair publication requires projector and text-last ordering".into(),
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::os::unix::fs::symlink;

    fn write(path: &Path, value: &[u8]) {
        fs::write(path, value).unwrap();
    }

    fn destinations(dir: &Path) -> Vec<(PairMemberRole, PathBuf)> {
        vec![
            (PairMemberRole::Projector, dir.join("model-mmproj.gguf")),
            (
                PairMemberRole::ProjectorReceipt,
                dir.join("model-mmproj.gguf.receipt.json"),
            ),
            (
                PairMemberRole::ProjectorTensorReceipt,
                dir.join("model-mmproj.gguf.tensor-conversion.json"),
            ),
            (
                PairMemberRole::TextReceipt,
                dir.join("model.gguf.receipt.json"),
            ),
            (
                PairMemberRole::TextTensorReceipt,
                dir.join("model.gguf.tensor-conversion.json"),
            ),
            (PairMemberRole::Text, dir.join("model.gguf")),
        ]
    }

    fn seed_old_and_new(workspace: &PairWorkspace, destinations: &[(PairMemberRole, PathBuf)]) {
        for (role, final_path) in destinations {
            write(final_path, format!("old-{role:?}").as_bytes());
            write(
                &workspace.staged_path(*role),
                format!("new-{role:?}").as_bytes(),
            );
        }
    }

    #[test]
    fn every_precommit_crash_restores_the_complete_old_pair() {
        let failpoints = [
            PublishFailpoint::AfterJournal,
            PublishFailpoint::AfterBackup(PairMemberRole::Projector),
            PublishFailpoint::AfterBackup(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::TextTensorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::Text),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::Projector),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::TextTensorReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::Projector),
            PublishFailpoint::AfterPromote(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::TextTensorReceipt),
        ];
        for failpoint in failpoints {
            let dir = tempfile::tempdir().unwrap();
            let text = dir.path().join("model.gguf");
            let workspace = PairWorkspace::create(&text).unwrap();
            let destinations = destinations(dir.path());
            seed_old_and_new(&workspace, &destinations);
            workspace
                .publish_crash_at(&destinations, failpoint)
                .unwrap_err();
            recover_pending(&text).unwrap();
            for (role, final_path) in &destinations {
                assert_eq!(
                    fs::read(final_path).unwrap(),
                    format!("old-{role:?}").as_bytes(),
                    "failpoint {failpoint:?} role {role:?}"
                );
            }
            assert!(!journal_path(&text).exists());
        }
    }

    #[test]
    fn every_precommit_in_process_failure_immediately_restores_the_complete_old_pair() {
        let failpoints = [
            PublishFailpoint::AfterJournal,
            PublishFailpoint::AfterBackup(PairMemberRole::Projector),
            PublishFailpoint::AfterBackup(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::TextTensorReceipt),
            PublishFailpoint::AfterBackup(PairMemberRole::Text),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::Projector),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::TextTensorReceipt),
            PublishFailpoint::AfterPromoteRename(PairMemberRole::Text),
            PublishFailpoint::AfterPromote(PairMemberRole::Projector),
            PublishFailpoint::AfterPromote(PairMemberRole::ProjectorReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::ProjectorTensorReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::TextReceipt),
            PublishFailpoint::AfterPromote(PairMemberRole::TextTensorReceipt),
        ];
        for failpoint in failpoints {
            let dir = tempfile::tempdir().unwrap();
            let text = dir.path().join("model.gguf");
            let workspace = PairWorkspace::create(&text).unwrap();
            let destinations = destinations(dir.path());
            seed_old_and_new(&workspace, &destinations);
            workspace
                .publish_inner(&destinations, Some(failpoint), false)
                .unwrap_err();
            for (role, final_path) in &destinations {
                assert_eq!(
                    fs::read(final_path).unwrap(),
                    format!("old-{role:?}").as_bytes(),
                    "failpoint {failpoint:?} role {role:?}"
                );
            }
            assert!(!journal_path(&text).exists());
        }
    }

    #[test]
    fn crash_after_text_commit_cleans_forward_to_the_complete_new_pair() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        seed_old_and_new(&workspace, &destinations);
        workspace
            .publish_crash_at(
                &destinations,
                PublishFailpoint::AfterPromote(PairMemberRole::Text),
            )
            .unwrap_err();
        recover_pending(&text).unwrap();
        for (role, final_path) in &destinations {
            assert_eq!(
                fs::read(final_path).unwrap(),
                format!("new-{role:?}").as_bytes()
            );
        }
        assert!(!journal_path(&text).exists());
    }

    #[test]
    fn fresh_destination_precommit_crash_returns_to_absent() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        for (role, _) in &destinations {
            write(
                &workspace.staged_path(*role),
                format!("new-{role:?}").as_bytes(),
            );
        }
        workspace
            .publish_crash_at(
                &destinations,
                PublishFailpoint::AfterPromote(PairMemberRole::TextTensorReceipt),
            )
            .unwrap_err();
        recover_pending(&text).unwrap();
        for (_, final_path) in &destinations {
            assert!(!final_path.exists());
        }
    }

    #[test]
    fn committed_local_pair_removes_prior_remote_receipts() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        for (role, final_path) in &destinations {
            write(final_path, format!("old-{role:?}").as_bytes());
            if matches!(role, PairMemberRole::Projector | PairMemberRole::Text) {
                write(
                    &workspace.staged_path(*role),
                    format!("new-{role:?}").as_bytes(),
                );
            }
        }

        workspace.publish(&destinations).unwrap();

        for (role, final_path) in &destinations {
            match role {
                PairMemberRole::Projector | PairMemberRole::Text => assert_eq!(
                    fs::read(final_path).unwrap(),
                    format!("new-{role:?}").as_bytes()
                ),
                PairMemberRole::ProjectorReceipt
                | PairMemberRole::ProjectorTensorReceipt
                | PairMemberRole::TextReceipt
                | PairMemberRole::TextTensorReceipt => assert!(!final_path.exists()),
            }
        }
        assert!(!journal_path(&text).exists());
    }

    #[test]
    fn committed_terminal_journal_recovers_after_backup_directory_is_gone() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        seed_old_and_new(&workspace, &destinations);
        workspace
            .publish_crash_at(
                &destinations,
                PublishFailpoint::AfterPromote(PairMemberRole::Text),
            )
            .unwrap_err();
        fs::remove_dir_all(workspace.root.join("backup")).unwrap();

        recover_pending(&text).unwrap();

        for (role, final_path) in &destinations {
            assert_eq!(
                fs::read(final_path).unwrap(),
                format!("new-{role:?}").as_bytes()
            );
        }
        assert!(!journal_path(&text).exists());
    }

    #[test]
    fn rolled_back_terminal_journal_recovers_after_transaction_root_is_gone() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        seed_old_and_new(&workspace, &destinations);
        workspace
            .publish_crash_at(&destinations, PublishFailpoint::AfterJournal)
            .unwrap_err();
        fs::remove_dir_all(&workspace.root).unwrap();

        recover_pending(&text).unwrap();

        for (role, final_path) in &destinations {
            assert_eq!(
                fs::read(final_path).unwrap(),
                format!("old-{role:?}").as_bytes()
            );
        }
        assert!(!journal_path(&text).exists());
    }

    #[test]
    fn recovery_rejects_a_symlink_journal_without_touching_its_target() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let sibling = dir.path().join("do-not-touch.json");
        write(&text, b"old-text");
        write(&sibling, b"sibling");
        symlink(&sibling, journal_path(&text)).unwrap();

        let error = recover_pending(&text).unwrap_err().to_string();

        assert!(error.contains("pair I/O"), "unexpected error: {error}");
        assert_eq!(fs::read(&text).unwrap(), b"old-text");
        assert_eq!(fs::read(&sibling).unwrap(), b"sibling");
    }

    #[test]
    fn recovery_rejects_a_journal_bound_to_another_text_name() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let sibling = dir.path().join("do-not-touch.gguf");
        write(&text, b"old-text");
        write(&sibling, b"sibling");
        let workspace = PairWorkspace::create(&text).unwrap();
        write(
            &workspace.staged_path(PairMemberRole::Projector),
            b"new-projector",
        );
        write(&workspace.staged_path(PairMemberRole::Text), b"new-text");
        let journal = PairTransactionJournal {
            schema_version: PAIR_JOURNAL_SCHEMA_VERSION,
            transaction_id: workspace.transaction_id.clone(),
            transaction_root: file_name(&workspace.root)
                .unwrap()
                .to_str()
                .unwrap()
                .to_owned(),
            members: vec![
                PairJournalMember {
                    role: PairMemberRole::Projector,
                    final_name: "model-mmproj.gguf".into(),
                    prior: None,
                    candidate: FileIdentity::from_path(
                        &workspace.staged_path(PairMemberRole::Projector),
                    )
                    .unwrap(),
                },
                PairJournalMember {
                    role: PairMemberRole::Text,
                    final_name: "do-not-touch.gguf".into(),
                    prior: FileIdentity::from_path(&sibling).unwrap(),
                    candidate: FileIdentity::from_path(
                        &workspace.staged_path(PairMemberRole::Text),
                    )
                    .unwrap(),
                },
            ],
        };
        write_journal(&journal_path(&text), &journal).unwrap();

        let error = recover_pending(&text).unwrap_err().to_string();

        assert!(
            error.contains("does not match"),
            "unexpected error: {error}"
        );
        assert_eq!(fs::read(&text).unwrap(), b"old-text");
        assert_eq!(fs::read(&sibling).unwrap(), b"sibling");
    }

    #[test]
    fn publication_rejects_symlink_destinations_without_touching_the_target() {
        let dir = tempfile::tempdir().unwrap();
        let text = dir.path().join("model.gguf");
        let sibling = dir.path().join("do-not-touch.gguf");
        let workspace = PairWorkspace::create(&text).unwrap();
        let destinations = destinations(dir.path());
        for (role, _) in &destinations {
            write(
                &workspace.staged_path(*role),
                format!("new-{role:?}").as_bytes(),
            );
        }
        write(&sibling, b"sibling");
        let projector = destinations
            .iter()
            .find(|(role, _)| *role == PairMemberRole::Projector)
            .unwrap()
            .1
            .clone();
        symlink(&sibling, &projector).unwrap();

        let error = workspace.publish(&destinations).unwrap_err().to_string();

        assert!(error.contains("regular file"), "unexpected error: {error}");
        assert_eq!(fs::read(&sibling).unwrap(), b"sibling");
        assert!(!journal_path(&text).exists());
    }
}