draftwright 0.6.0

Dimensioned engineering drawings from STEP, with SVG, PDF and editable DXF output.
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
use super::*;
use crate::haecceity::Part;
use crate::lint::{self, LintIssue};
use crate::model::{ANALYSIS_SCHEMA, Analysis, Plan, Problem, Spec};
use crate::output::Drawing;
use crate::text::Font;
use serde_json::{Value, json};
use std::path::Path;

/// Drawing extensions replaced on an output path, DXF included.
const EXTENSIONS: [&str; 4] = ["svg", "dxf", "pdf", "png"];

/// Where an analysis takes the part's features from.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Features<'a> {
    /// None: no dimensions, sections or lint that need features.
    None,
    /// Python quiddity's recognition JSON for the same file ([`crate::bridge`]).
    Python(&'a Path),
    /// Rust quiddity's recognition, run on the part ([`model::analyse_native`]). Refused when
    /// its document is refused; the families it does not give are in the Analysis's
    /// `unrecognised` and the plan's problems.
    Native,
}

impl<'a> From<Option<&'a Path>> for Features<'a> {
    fn from(features: Option<&'a Path>) -> Features<'a> {
        features.map_or(Features::None, Features::Python)
    }
}

/// The part at *step* (part *part* of an assembly alone, when given) and its analysis, with its
/// features from *features*.
pub(crate) fn analysed(
    step: &Path,
    part: Option<usize>,
    features: Features,
) -> Result<(Part, Analysis), Error> {
    Ok(match features {
        Features::None => model::analyse_with_part(step, None, part)?,
        Features::Python(json) => model::analyse_with_part(step, Some(json), part)?,
        Features::Native if part.is_none() => {
            crate::plan_cache::analyse_native_in(crate::plan_cache::dir().as_deref(), step)?
        }
        Features::Native => model::analyse_native_part(step, part)?,
    })
}

/// The distinct parts of the STEP file at *step*, in the order a part is chosen by
/// ([`model::analyse_step_part`]): each one's index, name, product id, face count and number of
/// placements.
pub fn parts(step: &Path) -> Result<Vec<model::FilePart>, Error> {
    let bytes = model::read_step_file(step).map_err(Error::invalid)?;
    Ok(model::parts(step, &bytes)?)
}

/// The Analysis of the STEP file at *step*, with the features in *features*, Python quiddity's
/// recognition JSON for the same file ([`crate::bridge`]), when given.
pub fn analyse(step: &Path, features: Option<&Path>) -> Result<Analysis, Error> {
    analyse_from(step, features.into())
}

/// The Analysis of the STEP file at *step*, with its features from *features*.
pub fn analyse_from(step: &Path, features: Features) -> Result<Analysis, Error> {
    analyse_part(step, None, features)
}

/// [`analyse_from`] of part *part* of an assembly alone, when given.
pub fn analyse_part(
    step: &Path,
    part: Option<usize>,
    features: Features,
) -> Result<Analysis, Error> {
    Ok(analysed(step, part, features)?.1)
}

/// The Plan for the STEP file at *step* (with *features*, as for [`analyse`]) under *spec*.
pub fn plan(step: &Path, features: Option<&Path>, spec: &Spec) -> Result<Plan, Error> {
    plan_from(step, features.into(), spec)
}

/// The Plan for the STEP file at *step*, with its features from *features*, under *spec*.
pub fn plan_from(step: &Path, features: Features, spec: &Spec) -> Result<Plan, Error> {
    plan_part(step, None, features, spec)
}

/// [`plan_from`] of part *part* of an assembly alone, when given.
pub fn plan_part(
    step: &Path,
    part: Option<usize>,
    features: Features,
    spec: &Spec,
) -> Result<Plan, Error> {
    let (part, analysis) = analysed(step, selected_part(step, part, spec)?, features)?;
    let _geometry = crate::engine_pins::prepare_read(&part, &analysis, spec);
    drop(part);
    Ok(planned(&analysis, spec)?)
}

/// The Plan for *analysis* (made by [`analyse`]) under *spec*. No STEP file is read; a document
/// of a version [`model::plan`] does not read is refused. Every plan here has draftwright's
/// layout recovery applied ([`model::plan_recovered`]).
pub fn plan_analysed(analysis: &Analysis, spec: &Spec) -> Result<Plan, Error> {
    Ok(planned(analysis, spec)?)
}

/// [`model::plan_recovered`], through the plan cache that `DRAFTWRIGHT_PLAN_CACHE` names, when
/// set ([`crate::plan_cache`]).
fn planned(analysis: &Analysis, spec: &Spec) -> Result<Plan, model::Invalid> {
    crate::plan_cache::plan_recovered_in(crate::plan_cache::dir().as_deref(), analysis, spec)
}

/// Resolve a persistent Spec identity before recognition, and refuse conflicting selectors.
pub(crate) fn selected_part(
    step: &Path,
    request: Option<usize>,
    spec: &Spec,
) -> Result<Option<usize>, Error> {
    let selected = spec
        .part
        .as_ref()
        .map(|choice| {
            let parts = if matches!(choice, crate::part_choice::PartChoice::Key(_)) {
                parts(step)?
            } else {
                Vec::new()
            };
            choice.resolve(&parts).map_err(Error::from)
        })
        .transpose()?;
    Ok(crate::part_choice::reconcile(request, selected)?)
}

/// Where an operation that draws takes the part's Analysis from.
#[derive(Clone, Copy)]
enum Given<'a> {
    /// Analysed from the STEP file (one part of it alone, when given), with its features from
    /// these.
    Features(Features<'a>, Option<usize>),
    /// The caller's, checked against the STEP file.
    Analysis(&'a Analysis),
}

/// The part at *step* and its analysis, as *given*.
fn read(step: &Path, given: Given, spec: &Spec) -> Result<(Part, Analysis), Error> {
    match given {
        Given::Features(features, part) => {
            analysed(step, selected_part(step, part, spec)?, features)
        }
        Given::Analysis(analysis) => Ok((bound(step, analysis)?, analysis.clone())),
    }
}

/// Whether *analysis* was made from the *part* a request names: any, when it names none. Part 0
/// of a file of one part is the whole file, which its analysis binds (no `part_index`).
fn made_of_part(analysis: &Analysis, part: Option<usize>) -> bool {
    part.is_none()
        || part == analysis.binding.part_index
        || (part == Some(0) && analysis.binding.part_index.is_none() && analysis.parts.is_empty())
}

/// The part at *step*, read without recognition, when *analysis* is of this version and was made
/// from that very file: one part is never drawn with another's analysis. The part of an assembly
/// it was made from alone (`binding.part_index`) is read alone again.
fn bound(step: &Path, analysis: &Analysis) -> Result<Part, Error> {
    if analysis.schema != ANALYSIS_SCHEMA {
        return Err(Error {
            choices: vec![ANALYSIS_SCHEMA.to_string()],
            ..Error::invalid(format!(
                "The analysis is {:?}, which this draftwright does not draw from; analyse {} again.",
                analysis.schema,
                step.display()
            ))
        });
    }
    let (part, read) = model::analyse_with_part(step, None, analysis.binding.part_index)?;
    let (given, found) = (&analysis.binding, &read.binding);
    if given.source_sha256 != found.source_sha256 || given.face_count != found.face_count {
        return Err(Error::invalid(format!(
            "The analysis was made from another file (sha256 {}, {} faces), not {} (sha256 {}, \
             {} faces); analyse {} and pass that analysis, or omit it.",
            given.source_sha256,
            given.face_count,
            step.display(),
            found.source_sha256,
            found.face_count,
            step.display()
        )));
    }
    Ok(part)
}

/// *spec* with the edit operations *ops* applied.
pub fn edit(spec: &Spec, ops: &Value) -> Result<Spec, Error> {
    Ok(model::edit(spec, ops)?)
}

/// The analysis, the plan and the drawing of the STEP file at *step* under *spec*. The plan's
/// problems include the drawing's: what could not be drawn as planned.
fn drawn(step: &Path, given: Given, spec: &Spec) -> Result<(Analysis, Plan, Vec<Drawing>), Error> {
    let (part, analysis) = read(step, given, spec)?;
    let _geometry = crate::engine_pins::prepare_read(&part, &analysis, spec);
    let mut plan = planned(&analysis, spec)?;
    let font = Font::new(MONO.to_vec()).expect("the bundled font parses");
    let drawings = model::render_sheets(&part, &analysis, &plan, &font).map_err(|e| {
        Error::failed(format!(
            "cannot draw {}: the kernel did not resolve its {e} on reading",
            step.display()
        ))
    })?;
    plan.problems
        .extend(drawings.iter().flat_map(|d| d.problems.iter().cloned()));
    plan.status = crate::completeness::status(&plan.problems);
    Ok((analysis, plan, drawings))
}

/// The plan (its problems with the drawing's), and the drawing in each of *formats* (from
/// [`FORMATS`]) as bytes, each carrying the spec; a PNG at [`DPI`].
pub fn render(
    step: &Path,
    features: Option<&Path>,
    spec: &Spec,
    formats: &[&str],
) -> Result<(Plan, Files), Error> {
    render_at(step, features, spec, formats, DPI)
}

/// [`render`], with a PNG at *dpi* pixels per inch: refused, as draftwright refuses it, when a
/// PNG is asked for and *dpi* is not positive.
pub fn render_at(
    step: &Path,
    features: Option<&Path>,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    render_from(step, features.into(), spec, formats, dpi)
}

/// [`render_at`], with the part's features from *features*.
pub fn render_from(
    step: &Path,
    features: Features,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    render_part(step, None, features, spec, formats, dpi)
}

/// [`render_from`] of part *part* of an assembly alone, when given.
pub fn render_part(
    step: &Path,
    part: Option<usize>,
    features: Features,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    rendered(step, Given::Features(features, part), spec, formats, dpi)
}

/// [`render_at`] from *analysis* (made by [`analyse`] from the STEP file at *step*) in place of
/// analysing it again: refused when *analysis* is of another version, or was made from another
/// file (its binding's SHA-256 or face count differs).
pub fn render_analysed(
    step: &Path,
    analysis: &Analysis,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    rendered(step, Given::Analysis(analysis), spec, formats, dpi)
}

fn rendered(
    step: &Path,
    given: Given,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    if let Some(other) = formats.iter().find(|f| !FORMATS.contains(f)) {
        return Err(Error {
            choices: FORMATS.map(String::from).to_vec(),
            ..Error::invalid(format!("{other:?} is not a format."))
        });
    }
    if formats.contains(&"png") && !(dpi > 0.0 && dpi.is_finite()) {
        return Err(Error::invalid(format!(
            "png export needs dpi > 0, got {dpi}"
        )));
    }
    let (_, plan, drawings) = drawn(step, given, spec)?;
    let files = encode_drawings(&drawings, formats, dpi)?;
    Ok((plan, files))
}

/// Internal entry point serving the Python binding: render a caller's native shape
/// and semantic PMI, using an Analysis of that shape.
/// PMI anchors are native face/edge indices. No PMI file or AP242 write/read is needed.
/// The caller owns the shape/analysis relationship, as with `model::render_sheets`.
/// General tolerance tables fail; model notes and knurls are not read.
#[doc(hidden)]
pub fn render_model(
    part: &Part,
    analysis: &Analysis,
    pmi: &haecceity::pmi::PartPmi,
    spec: &Spec,
    formats: &[&str],
    dpi: f64,
) -> Result<(Plan, Files), Error> {
    if formats.is_empty() || formats.iter().any(|f| !FORMATS.contains(f)) {
        return Err(Error::invalid("choose formats from svg, pdf, png, dxf"));
    }
    if formats.contains(&"png") && !(dpi > 0. && dpi.is_finite()) {
        return Err(Error::invalid("PNG dpi must be positive and finite"));
    }
    if part.faces.len() != analysis.binding.face_count {
        return Err(Error::invalid(
            "caller shape and analysis have different face counts",
        ));
    }
    let analysis = with_model(part, analysis, pmi)?;
    let _geometry = crate::engine_pins::prepare_read(part, &analysis, spec);
    let mut plan = planned(&analysis, spec)?;
    let font = Font::new(MONO.to_vec()).expect("bundled font");
    let drawings = model::render_sheets(part, &analysis, &plan, &font)
        .map_err(|e| Error::failed(e.to_string()))?;
    plan.problems
        .extend(drawings.iter().flat_map(|d| d.problems.iter().cloned()));
    plan.status = crate::completeness::status(&plan.problems);
    let files = encode_drawings(&drawings, formats, dpi)?;
    Ok((plan, files))
}

fn encode_drawings(drawings: &[Drawing], formats: &[&str], dpi: f64) -> Result<Files, Error> {
    let mut files = Vec::new();
    for &format in formats {
        if format == "pdf" {
            files.push((
                format.into(),
                Drawing::pdf_pages(drawings, &|name| match name {
                    "IBMPlexMono-Regular.ttf" => Some(MONO.to_vec()),
                    crate::title_block::FONT => Some(crate::title_block::FONT_DATA.to_vec()),
                    _ => None,
                }),
            ));
        } else {
            for (i, drawing) in drawings.iter().enumerate() {
                let suffix = if i == 0 {
                    format.into()
                } else {
                    format!("sheet{}.{format}", i + 1)
                };
                let bytes = if format == "svg" {
                    drawing.svg().into_bytes()
                } else if format == "dxf" {
                    drawing.dxf()
                } else {
                    drawing.png(dpi).map_err(Error::invalid)?
                };
                files.push((suffix, bytes));
            }
        }
    }
    Ok(files)
}

/// Writes each rendered file at *out* with its format's extension appended (`part.rev2` →
/// `part.rev2.svg`; an *out* already ending in a drawing format's extension has it replaced, as
/// draftwright's `_output_prefix` does), and returns the paths written. Each is written to a
/// uniquely and exclusively created sibling, then atomically replaces its destination. A failed
/// write/rename does not replace that destination and removes only this invocation's staged copy
/// (ADR5-10). Concurrent writers use last-writer-wins per file (the last successful rename).
/// A multi-format export is not transactional: formats may come from different writers, and a
/// later failure leaves already published formats in place.
pub fn write(out: &Path, files: &[(String, Vec<u8>)]) -> Result<Vec<String>, Error> {
    let stem = match out.extension().and_then(|e| e.to_str()) {
        Some(e) if EXTENSIONS.contains(&e) => out.with_extension(""),
        _ => out.to_path_buf(),
    };
    let named = |suffix: &str| {
        let mut name = stem.clone().into_os_string();
        name.push(format!(".{suffix}"));
        std::path::PathBuf::from(name)
    };
    let mut written = Vec::new();
    for (format, bytes) in files {
        let path = named(format);
        write_file(&path, |file| {
            use std::io::Write;
            file.write_all(bytes)
        })?;
        written.push(path.display().to_string());
    }
    Ok(written)
}

static EXPORT_STAGED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

fn write_file(
    path: &Path,
    write: impl FnOnce(&mut std::fs::File) -> std::io::Result<()>,
) -> Result<(), Error> {
    let (staged, mut file) = loop {
        let staged = path.with_file_name(format!(
            ".draftwright-export.{}.{}.part",
            std::process::id(),
            EXPORT_STAGED.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        match std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&staged)
        {
            Ok(file) => break (staged, file),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(e) => {
                return Err(Error::failed(format!(
                    "cannot write {}: {e}",
                    staged.display()
                )));
            }
        }
    };
    let discard = || {
        let _ = std::fs::remove_file(&staged);
    };
    let result = write(&mut file);
    drop(file);
    result.map_err(|e| {
        discard();
        Error::failed(format!("cannot write {}: {e}", staged.display()))
    })?;
    std::fs::rename(&staged, path).map_err(|e| {
        discard();
        Error::failed(format!("cannot write {}: {e}", path.display()))
    })
}

/// A drawing to look at: the plan rendered as SVG, with its problems and lint's findings on the
/// drawing.
#[derive(Clone, Debug, Serialize)]
pub struct Review {
    /// Counts across problems, lint and structural, each finding counted once.
    pub summary: crate::finding::Summary,
    pub svg: String,
    /// Physical holes whose bore sizes actually reached these rendered sheets.
    pub drawing_consumer: crate::evaluation::DrawingConsumer,
    /// SVGs for sheets 2..m, in document order.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub sheets: Vec<String>,
    /// The plan's problems, and the drawing's: what could not be drawn as planned.
    pub problems: Vec<Problem>,
    /// What lint finds on the rendered drawing, judged against the part's analysis alone
    /// ([`crate::lint`]): e.g. a hole whose sizes the sheet does not state.
    pub lint: Vec<LintIssue>,
    /// What lint's structural checks find on the drawn sheet ([`crate::lint::structural`]):
    /// e.g. ink past the border. draftwright's lint runs them first, in the same list; they are
    /// kept apart here so `lint` stays coverage alone.
    pub structural: Vec<LintIssue>,
}

/// The drawing of the STEP file at *step* under *spec*, as SVG text, with its problems and
/// lint's findings.
pub fn review(step: &Path, features: Option<&Path>, spec: &Spec) -> Result<Review, Error> {
    review_from(step, features.into(), spec)
}

/// [`review`], with the part's features from *features*.
pub fn review_from(step: &Path, features: Features, spec: &Spec) -> Result<Review, Error> {
    review_part(step, None, features, spec)
}

/// [`review_from`] of part *part* of an assembly alone, when given.
pub fn review_part(
    step: &Path,
    part: Option<usize>,
    features: Features,
    spec: &Spec,
) -> Result<Review, Error> {
    reviewed(step, Given::Features(features, part), spec)
}

/// [`review`] from *analysis* in place of analysing the STEP file again, refused as
/// [`render_analysed`] refuses it.
pub fn review_analysed(step: &Path, analysis: &Analysis, spec: &Spec) -> Result<Review, Error> {
    reviewed(step, Given::Analysis(analysis), spec)
}

fn reviewed(step: &Path, given: Given, spec: &Spec) -> Result<Review, Error> {
    let (analysis, plan, drawings) = drawn(step, given, spec)?;
    let drawing = &drawings[0];
    // Lint judges the drawing against the requirements the plan drew from: the file's PMI with
    // the spec's datums and controls merged in ([`crate::spec_gdt`]); the plan accepted them.
    let merged = crate::spec_gdt::effective(&analysis, &plan.spec).ok();
    let analysis = merged.as_deref().unwrap_or(&analysis);
    // Preserve the existing rendering/evaluation order; labels are bookkeeping afterward.
    let svg = drawing.svg();
    let drawing_consumer = crate::evaluation::drawing_consumer(analysis, Ok(&drawings));
    let sheets = drawings.iter().skip(1).map(Drawing::svg).collect();
    let lint = lint::lint_sheets(analysis, &drawings);
    let structural = lint::structural::check_sheets(&drawings);
    let summary = crate::finding::Summary::new(
        plan.problems
            .iter()
            .map(Problem::labels)
            .chain(lint.iter().chain(&structural).map(LintIssue::labels)),
    );
    Ok(Review {
        summary,
        svg,
        drawing_consumer,
        sheets,
        problems: plan.problems,
        lint,
        structural,
    })
}

/// The spec embedded in the drawing (SVG, DXF, PDF or PNG) at *drawing*.
pub fn spec(drawing: &Path) -> Result<Spec, Error> {
    let bytes = std::fs::read(drawing)
        .map_err(|e| Error::invalid(format!("cannot read {}: {e}", drawing.display())))?;
    model::embedded_spec(&bytes)
        .map_err(|e| {
            Error::invalid(format!(
                "cannot read the spec in {}: {e}",
                drawing.display()
            ))
        })?
        .ok_or_else(|| Error::invalid(format!("{} carries no draftwright spec", drawing.display())))
}

/// Operation *op* with JSON arguments *args*, its result as JSON:
///
/// | op | arguments | result |
/// | --- | --- | --- |
/// | `analyse` | `step`, `features`? | the Analysis |
/// | `plan` | `step` and `features`?, or `analysis` (and `step`?); `spec`? | the Plan |
/// | `edit` | `spec`, `ops` | the edited Spec |
/// | `render` | `step`, `features`? or `analysis`?, `spec`?, `output`, `formats`?, `dpi`? | `{written, problems}` |
/// | `review` | `step`, `features`? or `analysis`?, `spec`? | `{svg, problems, lint}` |
/// | `spec` | `drawing` | the Spec embedded in the drawing |
/// | `pack` | `step` or `part_ref`, `spec`?, `formats`? | `{report, files: [{name, mimeType, blob}]}` (base64) |
/// | `parts` | `step` | `{parts}`: the file's distinct parts ([`parts`]) |
///
/// `analyse`, `plan`, `render` and `review` also take `part`, the index of one part of an
/// assembly (as `parts` lists them) to draw alone, in its own frame and with its PMI only.
/// Without it the whole file is drawn as one part, and an assembly's plan warns
/// (`assembly_drawn_whole`), unless every body coincides with one part's (`coincident_bodies`)
/// or lies within the largest body's box (`assembly_principal_drawn`), when that part is drawn
/// alone. An `analysis` already names its part (`binding.part_index`): a
/// `part` beside it must be the same.
///
/// `step`, `features`, `output` and `drawing` are file paths; `features` is Python quiddity's
/// recognition JSON for the STEP file ([`crate::bridge`]). `analysis` is the Analysis document
/// itself, as `analyse` returned it, in place of analysing `step` again (so not beside
/// `features`, which it already carries): `plan` then needs no `step`, and when `step` is given
/// the analysis must have been made from it, as for `render` and `review`. An absent `spec` is
/// the default one; absent `formats` are [`DEFAULT_FORMATS`], and an absent `dpi` (the PNG's) is
/// [`DPI`]. A panic inside an operation is answered as `failed`, so a long-lived adapter outlives
/// one bad part.
pub fn call(op: &str, args: &Value) -> Result<Value, Error> {
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| dispatch(op, args))).unwrap_or_else(
        |panic| {
            let why = panic
                .downcast_ref::<&str>()
                .map(|s| s.to_string())
                .or_else(|| panic.downcast_ref::<String>().cloned())
                .unwrap_or_default();
            Err(Error::failed(format!("{op} stopped: {why}")))
        },
    )
}

fn dispatch(op: &str, args: &Value) -> Result<Value, Error> {
    if matches!(op, "spec_schema" | "item_options") {
        // These discovery operations share MCP's published argument and result shapes.
        // Service envelopes are transport metadata, never tool arguments.
        let mut args = args.get("arguments").unwrap_or(args).clone();
        if let Some(fields) = args.as_object_mut() {
            fields.remove("op");
            if op == "spec_schema" {
                fields.remove("id");
            }
        }
        return crate::mcp::call(&crate::stateless::Cache::environment(), op, &args);
    }
    let text = |key: &str| {
        args.get(key)
            .and_then(Value::as_str)
            .map(Path::new)
            .ok_or_else(|| Error::invalid(format!("{op} needs {key:?}, a file path")))
    };
    let features = match args.get("features") {
        None | Some(Value::Null) => None,
        Some(Value::String(path)) => Some(Path::new(path.as_str())),
        Some(_) => {
            return Err(Error::invalid(format!(
                "{op}'s \"features\" is a file path"
            )));
        }
    };
    let given = || -> Result<Spec, Error> {
        match args.get("spec") {
            None | Some(Value::Null) => Ok(Spec::default()),
            Some(s) => serde_json::from_value(s.clone())
                .map_err(|e| Error::invalid(format!("the spec is not valid: {e}"))),
        }
    };
    let part = request_part(op, args)?;
    // The caller's Analysis, if given: only in place of features, which it already carries, and
    // of a part, which it names.
    let analysis = || -> Result<Option<Analysis>, Error> {
        let analysis: Option<Analysis> = match args.get("analysis") {
            None | Some(Value::Null) => None,
            Some(_) if features.is_some() => {
                return Err(Error::invalid(format!(
                    "{op} takes \"analysis\" or \"features\", not both: the analysis already \
                     carries the features it was made with."
                )));
            }
            Some(a) => Some(
                serde_json::from_value(a.clone())
                    .map_err(|e| Error::invalid(format!("the analysis is not valid: {e}")))?,
            ),
        };
        if let Some(a) = &analysis {
            let spec = given()?;
            crate::part_choice::check(a, &spec)?;
            let selected = spec
                .part
                .as_ref()
                .map(|c| crate::part_choice::selected(a, c))
                .transpose()?;
            crate::part_choice::reconcile(part, selected)?;
        }
        if let Some(a) = &analysis
            && !made_of_part(a, part)
        {
            return Err(Error::invalid(format!(
                "{op}'s \"part\" ({}) is not the part the analysis was made from ({}); omit \
                 \"part\", or analyse that part again.",
                part.map_or("none".into(), |k| k.to_string()),
                a.binding
                    .part_index
                    .map_or("the whole file".into(), |k| format!("part {k}"))
            )));
        }
        Ok(analysis)
    };
    let features = Features::from(features);
    match op {
        "analyse" => Ok(json_of(&analyse_part(text("step")?, part, features)?)),
        "parts" => Ok(json!({ "parts": parts(text("step")?)? })),
        "pack" => {
            let spec = args
                .get("spec")
                .filter(|v| !v.is_null())
                .map(|v| serde_json::from_value::<crate::pack::PackSpec>(v.clone()))
                .transpose()
                .map_err(|e| Error::invalid(format!("invalid pack spec: {e}")))?
                .unwrap_or_default();
            let formats = match args.get("formats") {
                None | Some(Value::Null) => crate::pack::FORMATS.to_vec(),
                Some(Value::Array(list)) => list
                    .iter()
                    .map(|v| {
                        v.as_str()
                            .ok_or_else(|| Error::invalid("\"formats\" is a list of format names"))
                    })
                    .collect::<Result<Vec<_>, _>>()?,
                Some(_) => return Err(Error::invalid("\"formats\" is a list of format names")),
            };
            let step = args.get("step").filter(|v| !v.is_null());
            let part_ref = args.get("part_ref").filter(|v| !v.is_null());
            if step.is_some() && part_ref.is_some() {
                return Err(Error::invalid("pack takes step or part_ref, not both"));
            }
            let source = step.or(part_ref).and_then(Value::as_str).ok_or_else(|| {
                Error::invalid("pack needs step (a path or registered part ref) or part_ref")
            })?;
            let registered = part_ref.is_some()
                || source.split_once('-').is_some_and(|(key, _)| {
                    key.len() >= 64 && key.bytes().take(64).all(|b| b.is_ascii_hexdigit())
                });
            let (report, files) = if registered {
                let (name, bytes) = crate::stateless::Cache::environment().source(source)?;
                crate::pack::render_bytes(&name, &bytes, &spec, &formats)?
            } else {
                crate::pack::render(Path::new(source), &spec, &formats)?
            };
            let files: Vec<_> = files.into_iter().map(|(format, bytes)| json!({
                "name": format!("pack.{format}"),
                "mimeType": match format.as_str() { "pdf" => "application/pdf", "csv" => "text/csv", _ => "application/json" },
                "blob": crate::mcp::base64(&bytes),
            })).collect();
            Ok(json!({"report": report, "files": files}))
        }
        "plan" => match analysis()? {
            Some(analysis) => {
                let spec = given()?;
                let _geometry = if args.get("step").is_some_and(|s| !s.is_null()) {
                    let part = bound(text("step")?, &analysis)?;
                    Some(crate::engine_pins::prepare_read(&part, &analysis, &spec))
                } else {
                    None
                };
                Ok(json_of(&plan_analysed(&analysis, &spec)?))
            }
            None => Ok(json_of(&plan_part(
                text("step")?,
                part,
                features,
                &given()?,
            )?)),
        },
        "edit" => {
            let ops = args
                .get("ops")
                .ok_or_else(|| Error::invalid("edit needs \"ops\", a list of operations"))?;
            Ok(json_of(&edit(&given()?, ops)?))
        }
        "render" => {
            let formats: Vec<&str> = match args.get("formats") {
                None | Some(Value::Null) => DEFAULT_FORMATS.to_vec(),
                Some(Value::Array(list)) => list
                    .iter()
                    .map(|f| {
                        f.as_str()
                            .ok_or_else(|| Error::invalid("\"formats\" is a list of format names"))
                    })
                    .collect::<Result<_, _>>()?,
                Some(_) => return Err(Error::invalid("\"formats\" is a list of format names")),
            };
            let dpi = match args.get("dpi") {
                None | Some(Value::Null) => DPI,
                Some(Value::Number(n)) => n.as_f64().unwrap_or(f64::NAN),
                Some(_) => return Err(Error::invalid("\"dpi\" is a number of pixels per inch")),
            };
            let output = text("output")?;
            let (plan, files) = match analysis()? {
                Some(a) => render_analysed(text("step")?, &a, &given()?, &formats, dpi)?,
                None => render_part(text("step")?, part, features, &given()?, &formats, dpi)?,
            };
            Ok(json!({ "written": write(output, &files)?, "problems": plan.problems }))
        }
        "review" => Ok(json_of(&match analysis()? {
            Some(a) => review_analysed(text("step")?, &a, &given()?)?,
            None => review_part(text("step")?, part, features, &given()?)?,
        })),
        "spec" => Ok(json_of(&spec(text("drawing")?)?)),
        other => Err(Error {
            choices: OPERATIONS.map(String::from).to_vec(),
            ..Error::invalid(format!("{other:?} is not an operation."))
        }),
    }
}

fn json_of(document: &impl Serialize) -> Value {
    serde_json::to_value(document).expect("a document serialises")
}

/// A request's `part`: the index of one part of an assembly, or none.
pub(crate) fn request_part(op: &str, args: &Value) -> Result<Option<usize>, Error> {
    match args.get("part") {
        None | Some(Value::Null) => Ok(None),
        Some(v) => v
            .as_u64()
            .and_then(|k| usize::try_from(k).ok())
            .map(Some)
            .ok_or_else(|| {
                Error::invalid(format!(
                    "{op}'s \"part\" is a part's index (0, 1, …), as the parts operation lists them"
                ))
            }),
    }
}

/// Additional operations on the connection's one live part; batch operations still work.
pub const SESSION_OPERATIONS: [&str; 8] = [
    "open", "apply", "undo", "redo", "item", "items", "problems", "options",
];

#[derive(Default)]
pub struct Service {
    session: Option<crate::session::Session>,
}
impl Service {
    pub fn call(&mut self, op: &str, args: &Value) -> Result<Value, Error> {
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| self.dispatch(op, args)))
            .unwrap_or_else(|_| Err(Error::failed(format!("{op} stopped"))))
    }
    fn dispatch(&mut self, op: &str, args: &Value) -> Result<Value, Error> {
        if matches!(op, "item_options" | "mcp_item_options")
            && !args.get("arguments").is_some_and(Value::is_object)
        {
            return Err(Error::invalid(
                r#"serve item_options requires a nested arguments object: put the item id in arguments.id and keep the request id at the top level (for example {"id":1,"op":"item_options","arguments":{"part_ref":"…","id":"sheet"}})."#,
            ));
        }
        if let Some(tool) = op.strip_prefix("mcp_") {
            let nested = args.get("arguments");
            let mut args = nested.unwrap_or(args).clone();
            if nested.is_none()
                && let Some(fields) = args.as_object_mut()
            {
                fields.remove("op");
                fields.remove("id");
            }
            return crate::mcp::call(&crate::stateless::Cache::environment(), tool, &args);
        }
        if op == "stateless_apply" || op == "stateless_preview" {
            let cache = crate::stateless::Cache::environment();
            let part_ref = args["part_ref"]
                .as_str()
                .ok_or_else(|| Error::invalid("provide part_ref from inspect_part"))?;
            let spec: Spec =
                serde_json::from_value(args.get("spec").cloned().unwrap_or_else(|| json!({})))
                    .map_err(|e| Error::invalid(e.to_string()))?;
            let ops: Vec<crate::items::Op> = serde_json::from_value(args["ops"].clone())
                .map_err(|e| Error::invalid(e.to_string()))?;
            return if op == "stateless_apply" {
                Ok(json_of(&cache.apply(part_ref, &spec, &ops)?))
            } else {
                Ok(json_of(&cache.preview(part_ref, &spec, &ops)?))
            };
        }
        if op == "open" {
            let step = args
                .get("step")
                .and_then(Value::as_str)
                .ok_or_else(|| Error::invalid("open needs \"step\", a file path"))?;
            let spec = args
                .get("spec")
                .filter(|v| !v.is_null())
                .map(|v| serde_json::from_value(v.clone()))
                .transpose()
                .map_err(|e| Error::invalid(format!("invalid spec: {e}")))?
                .unwrap_or_default();
            let part = request_part(op, args)?;
            let session = if let Some(a) = args.get("analysis").filter(|a| !a.is_null()) {
                if args.get("features").is_some_and(|v| !v.is_null()) {
                    return Err(Error::invalid("open takes analysis or features, not both"));
                }
                let analysis: Analysis = serde_json::from_value(a.clone())
                    .map_err(|e| Error::invalid(format!("invalid analysis: {e}")))?;
                if !made_of_part(&analysis, part) {
                    return Err(Error::invalid(
                        "open's part is not the part the analysis was made from; omit part",
                    ));
                }
                crate::session::Session::new(bound(Path::new(step), &analysis)?, analysis, spec)?
            } else {
                let features = match args.get("features") {
                    None | Some(Value::Null) => Features::Native,
                    Some(Value::String(s)) if s == "native" => Features::Native,
                    Some(Value::String(s)) if s == "none" => Features::None,
                    Some(Value::String(s)) => Features::Python(Path::new(s)),
                    _ => {
                        return Err(Error::invalid(
                            "open's features is native, none or a JSON file path",
                        ));
                    }
                };
                crate::session::Session::open_part(Path::new(step), part, features, spec)?
            };
            let result = json_of(session.state());
            self.session = Some(session);
            return Ok(result);
        }
        if !SESSION_OPERATIONS.contains(&op) {
            return call(op, args);
        }
        let session = self
            .session
            .as_mut()
            .ok_or_else(|| Error::invalid("open a part before using session operations"))?;
        let id = || {
            args.get("item")
                .and_then(Value::as_str)
                .ok_or_else(|| Error::invalid(format!("{op} needs \"item\", a stable item ID")))
        };
        match op {
            "apply" => {
                let ops = args
                    .get("ops")
                    .ok_or_else(|| Error::invalid("apply needs ops, a list of Spec operations"))?;
                let ops: Vec<crate::items::Op> = serde_json::from_value(ops.clone())
                    .map_err(|e| Error::invalid(format!("invalid ops: {e}")))?;
                let commit = match args.get("commit") {
                    None => true,
                    Some(Value::Bool(b)) => *b,
                    _ => return Err(Error::invalid("commit is a boolean")),
                };
                Ok(json_of(&session.apply(&ops, commit)?))
            }
            "undo" => Ok(json_of(&session.undo()?)),
            "redo" => Ok(json_of(&session.redo()?)),
            "item" => Ok(json_of(&session.item(id()?)?)),
            "options" => Ok(json_of(&session.options(id()?)?)),
            "items" => {
                let filter = serde_json::from_value(
                    args.get("filter").cloned().unwrap_or_else(|| json!({})),
                )
                .map_err(|e| Error::invalid(format!("invalid item filter: {e}")))?;
                Ok(json_of(&session.items(&filter)))
            }
            "problems" => {
                let filter = serde_json::from_value(
                    args.get("filter").cloned().unwrap_or_else(|| json!({})),
                )
                .map_err(|e| Error::invalid(format!("invalid problem filter: {e}")))?;
                Ok(json_of(&session.problems(&filter)))
            }
            _ => unreachable!(),
        }
    }
}

/// Stateless subprocess operations; tool calls use the same published MCP schemas.
pub const STATELESS_OPERATIONS: [&str; 16] = [
    "stateless_apply",
    "stateless_preview",
    "mcp_inspect_part",
    "mcp_list_items",
    "mcp_get_item",
    "mcp_list_problems",
    "mcp_item_options",
    "mcp_preview_ops",
    "mcp_apply_ops",
    "mcp_view_sheet",
    "mcp_render_pdf",
    "mcp_get_spec",
    "mcp_spec_schema",
    "mcp_analyse",
    "mcp_render",
    "mcp_parts",
];

#[cfg(test)]
mod export_tests {
    use super::*;
    use std::fs;
    use std::io::{self, Write};
    use std::path::PathBuf;
    use std::sync::{Barrier, Mutex, mpsc};
    use std::time::Duration;

    static LOCK: Mutex<()> = Mutex::new(());

    struct Scratch(PathBuf);
    impl Scratch {
        fn new(name: &str) -> Self {
            let path = std::env::temp_dir()
                .join(format!("draftwright-export-{name}-{}", std::process::id()));
            let _ = fs::remove_dir_all(&path);
            fs::create_dir(&path).unwrap();
            Self(path)
        }
        fn entries(&self) -> Vec<PathBuf> {
            let mut entries: Vec<_> = fs::read_dir(&self.0)
                .unwrap()
                .map(|e| e.unwrap().path())
                .collect();
            entries.sort();
            entries
        }
    }
    impl Drop for Scratch {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.0);
        }
    }

    #[test]
    fn simultaneous_exports_keep_complete_payloads_and_foreign_staging() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("simultaneous");
        let foreign = dir.0.join("part.rev2.svg.part");
        fs::write(&foreign, b"another invocation").unwrap();
        let barrier = Barrier::new(2);
        std::thread::scope(|scope| {
            let handles: Vec<_> = (0..2)
                .map(|writer| {
                    let dir = &dir;
                    let barrier = &barrier;
                    scope.spawn(move || {
                        let files: Files = FORMATS
                            .iter()
                            .enumerate()
                            .map(|(i, &format)| {
                                (
                                    format.into(),
                                    vec![b'A' + writer * 4 + i as u8; 65536 + writer as usize],
                                )
                            })
                            .collect();
                        barrier.wait();
                        (0..8)
                            .map(|_| write(&dir.0.join("part.rev2.pdf"), &files))
                            .collect::<Vec<_>>()
                    })
                })
                .collect();
            for handle in handles {
                for result in handle.join().unwrap() {
                    let written = result.unwrap();
                    assert_eq!(
                        written,
                        FORMATS.map(|f| dir.0.join(format!("part.rev2.{f}")).display().to_string())
                    );
                }
            }
        });
        for (i, format) in FORMATS.iter().enumerate() {
            let bytes = fs::read(dir.0.join(format!("part.rev2.{format}"))).unwrap();
            assert!(bytes == vec![b'A' + i as u8; 65536] || bytes == vec![b'E' + i as u8; 65537]);
        }
        assert_eq!(fs::read(&foreign).unwrap(), b"another invocation");
        assert_eq!(
            dir.entries().len(),
            FORMATS.len() + 1,
            "only destinations and the foreign staging file"
        );
    }

    #[test]
    fn last_rename_wins_without_touching_the_other_writers_stage() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("last-writer");
        let path = dir.0.join("part.svg");
        fs::write(&path, b"old destination").unwrap();
        let (ready_tx, ready_rx) = mpsc::channel();
        let (resume_tx, resume_rx) = mpsc::channel();
        std::thread::scope(|scope| {
            let first_path = &path;
            let first = scope.spawn(move || {
                write_file(first_path, |file| {
                    file.write_all(b"first payload")?;
                    ready_tx.send(()).unwrap();
                    resume_rx.recv_timeout(Duration::from_secs(10)).unwrap();
                    Ok(())
                })
            });
            ready_rx.recv_timeout(Duration::from_secs(10)).unwrap();
            let stage = dir.entries().into_iter().find(|p| p != &path).unwrap();
            let second = write_file(&path, |file| {
                file.write_all(b"second payload")?;
                assert_eq!(fs::read(&path).unwrap(), b"old destination");
                Ok(())
            });
            let intermediate = fs::read(&path);
            let retained = fs::read(&stage);
            resume_tx.send(()).unwrap();
            let first = first.join().unwrap();
            eprintln!(
                "H3 last-writer: completed={}, retained_stage={}",
                usize::from(first.is_ok()) + usize::from(second.is_ok()),
                usize::from(retained.as_ref().is_ok_and(|b| b == b"first payload"))
            );
            second.unwrap();
            first.unwrap();
            assert_eq!(intermediate.unwrap(), b"second payload");
            assert_eq!(retained.unwrap(), b"first payload");
        });
        assert_eq!(fs::read(&path).unwrap(), b"first payload");
        assert_eq!(dir.entries(), [path]);
    }

    #[test]
    fn failed_partial_write_cleans_only_its_owned_stage() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("write-failure");
        let path = dir.0.join("part.svg");
        fs::write(&path, b"old destination").unwrap();
        let (ready_tx, ready_rx) = mpsc::channel();
        let (resume_tx, resume_rx) = mpsc::channel();
        std::thread::scope(|scope| {
            let first_path = &path;
            let first = scope.spawn(move || {
                write_file(first_path, |file| {
                    file.write_all(b"successful payload")?;
                    ready_tx.send(()).unwrap();
                    resume_rx.recv_timeout(Duration::from_secs(10)).unwrap();
                    Ok(())
                })
            });
            ready_rx.recv_timeout(Duration::from_secs(10)).unwrap();
            let stage = dir.entries().into_iter().find(|p| p != &path).unwrap();
            let failed = write_file(&path, |file| {
                file.write_all(b"partial")?;
                Err(io::Error::other("injected write failure"))
            });
            let retained = fs::read(&stage);
            let destination = fs::read(&path);
            let entries = dir.entries();
            resume_tx.send(()).unwrap();
            let first = first.join().unwrap();
            eprintln!(
                "H3 partial-write failure: retained_stage={}",
                usize::from(retained.as_ref().is_ok_and(|b| b == b"successful payload"))
            );
            let error = failed.unwrap_err();
            assert_eq!(error.code, "failed");
            assert!(error.message.contains("injected write failure"));
            assert_eq!(destination.unwrap(), b"old destination");
            assert_eq!(retained.unwrap(), b"successful payload");
            assert_eq!(entries.len(), 2);
            first.unwrap();
        });
        assert_eq!(fs::read(&path).unwrap(), b"successful payload");
        assert_eq!(dir.entries(), [path]);
    }

    #[test]
    fn failed_rename_keeps_the_other_stage_and_destination() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("rename-failure");
        let path = dir.0.join("part.svg");
        fs::create_dir(&path).unwrap();
        fs::write(path.join("keep"), b"destination contents").unwrap();
        let (ready_tx, ready_rx) = mpsc::channel();
        let (resume_tx, resume_rx) = mpsc::channel();
        std::thread::scope(|scope| {
            let first_path = &path;
            let first = scope.spawn(move || {
                write_file(first_path, |file| {
                    file.write_all(b"successful payload")?;
                    ready_tx.send(()).unwrap();
                    resume_rx.recv_timeout(Duration::from_secs(10)).unwrap();
                    Ok(())
                })
            });
            ready_rx.recv_timeout(Duration::from_secs(10)).unwrap();
            let stage = dir.entries().into_iter().find(|p| p != &path).unwrap();
            let failed = write_file(&path, |file| file.write_all(b"failed payload"));
            let retained = fs::read(&stage);
            let destination = fs::read(path.join("keep"));
            let entries = dir.entries();
            fs::remove_dir_all(&path).unwrap();
            resume_tx.send(()).unwrap();
            let first = first.join().unwrap();
            eprintln!(
                "H3 rename failure: retained_stage={}",
                usize::from(retained.as_ref().is_ok_and(|b| b == b"successful payload"))
            );
            assert_eq!(failed.unwrap_err().code, "failed");
            assert_eq!(destination.unwrap(), b"destination contents");
            assert_eq!(retained.unwrap(), b"successful payload");
            assert_eq!(entries.len(), 2);
            first.unwrap();
        });
        assert_eq!(fs::read(&path).unwrap(), b"successful payload");
        assert_eq!(dir.entries(), [path]);
    }

    #[test]
    fn export_batch_failure_keeps_published_formats_and_foreign_files() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("batch");
        let foreign = dir.0.join("part.pdf.part");
        fs::write(&foreign, b"another invocation").unwrap();
        fs::write(dir.0.join("part.svg"), b"old SVG").unwrap();
        fs::write(dir.0.join("part.png"), b"unrequested PNG").unwrap();
        fs::create_dir(dir.0.join("part.pdf")).unwrap();
        let error = write(
            &dir.0.join("part"),
            &[
                ("svg".into(), b"new SVG".to_vec()),
                ("pdf".into(), b"new PDF".to_vec()),
                ("dxf".into(), b"not reached".to_vec()),
            ],
        )
        .unwrap_err();
        assert_eq!(error.code, "failed");
        assert_eq!(fs::read(dir.0.join("part.svg")).unwrap(), b"new SVG");
        assert_eq!(
            fs::read(dir.0.join("part.png")).unwrap(),
            b"unrequested PNG"
        );
        eprintln!(
            "H3 batch failure: retained_foreign={}",
            usize::from(fs::read(&foreign).is_ok_and(|b| b == b"another invocation"))
        );
        assert_eq!(fs::read(&foreign).unwrap(), b"another invocation");
        assert_eq!(dir.entries().len(), 4);
        assert!(!dir.0.join("part.dxf").exists());
    }
    #[test]
    fn exclusive_creation_skips_collisions_without_removing_foreign_entries() {
        let _guard = LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = Scratch::new("collisions");
        let path = dir.0.join("part.svg");
        fs::write(&path, b"old destination").unwrap();
        let next = EXPORT_STAGED.load(std::sync::atomic::Ordering::Relaxed);
        let candidate = |id| {
            dir.0.join(format!(
                ".draftwright-export.{}.{id}.part",
                std::process::id()
            ))
        };
        fs::write(candidate(next), b"foreign file").unwrap();
        fs::create_dir(candidate(next + 1)).unwrap();
        fs::write(candidate(next + 1).join("keep"), b"foreign directory").unwrap();
        #[cfg(unix)]
        std::os::unix::fs::symlink(&path, candidate(next + 2)).unwrap();
        #[cfg(not(unix))]
        fs::write(candidate(next + 2), b"foreign file").unwrap();
        write_file(&path, |file| {
            file.write_all(b"new destination")?;
            assert!(candidate(next + 3).is_file(), "collisions must be retried");
            assert_eq!(fs::read(&path).unwrap(), b"old destination");
            Ok(())
        })
        .unwrap();
        assert_eq!(fs::read(&path).unwrap(), b"new destination");
        assert_eq!(fs::read(candidate(next)).unwrap(), b"foreign file");
        assert_eq!(
            fs::read(candidate(next + 1).join("keep")).unwrap(),
            b"foreign directory"
        );
        assert!(fs::symlink_metadata(candidate(next + 2)).is_ok());
        assert_eq!(dir.entries().len(), 4);
    }
}