quiddity 0.2.3

Manufacturing feature recognition from STEP B-rep models: holes, bosses, pockets, slots, steps, patterns and turned features, with measured sizes.
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
//! The feature recognisers, ported family by family from the Python implementation, and the
//! per-run machinery they share.
//!
//! Each family module has a crate-private `recognise_*` (records only, exposed solely by
//! the unsupported crate tests) and a `discover` taking the run's `Context`, returning
//! [`Occurrence`]s carrying the faces that define each record. Shared analysis — the cylinder
//! inventory, the point classifier, the part's box — lives in the context so a run computes it
//! once however many families ask.

pub mod analytic_surfaces;
pub mod angled_steps;
pub mod bevel;
pub mod blend_view;
pub mod blends;
pub mod body;
pub mod bosses;
pub mod chamfers;
pub mod channels;
pub mod circular_blind_steps;
pub mod circular_face_patterns;
pub mod through_notches;
pub mod v_channels;
internal_entry! { pub mod context; }
pub mod annular_grooves;
pub mod countersinks;
pub mod cylinders;
pub mod cylindrical_channels;
pub mod cylindrical_end_surface;
pub mod cylindrical_passages;
pub mod cylindrical_pockets;
pub mod cylindrical_profiles;
pub mod cylindrical_seats;
pub mod discovery;
pub mod edge_open;
pub mod edge_open_circular;
pub mod edge_open_prismatic;
pub mod effective_surfaces;
pub mod entry_treatments;
pub mod evidence;
pub mod experimental_geometry;
pub mod face_grooves;
pub mod fillets;
pub mod flats;
pub mod freeform_surfaces;
pub mod geometry_evidence;
pub mod graph;
pub mod grooves;
pub mod gussets;
pub mod hole_patterns;
pub mod holes;
pub mod interior_voids;
pub mod levels;
pub mod modelled_threads;
pub mod oblique_through_steps;
pub mod oriented_chamfers;
pub mod oriented_slots;
pub mod outer_profile;
pub mod pads;
pub mod paired_ramp_steps;
pub mod passage_compat;
pub mod passages;
pub(crate) mod pattern_geometry;
pub mod placed;
pub mod plane_envelope_passages;
pub mod planes;
pub mod plates;
pub mod pockets;
pub mod policy;
pub mod polygonal_bosses;
pub mod prismatic_pockets;
pub mod probes;
pub mod profiled_bores;
pub mod recess_core;
pub mod recess_faces;
pub mod recess_obround;
pub mod recess_patterns;
pub mod recess_radii;
pub mod recess_records;
pub mod recess_reduce;
pub mod reconcile;
pub mod rectangular_blind_slots;
pub mod regions;
pub mod repeating_profiles;
pub mod rings;
pub mod round_bottom_slots;
pub mod section_passages;
pub mod section_recess;
pub mod section_recess_discovery;
pub mod section_recess_family;
pub mod section_recess_geometry;
pub mod sections;
pub mod semicircular_blind_slots;
pub mod sheet_metal;
pub mod slots;
pub mod solid_properties;
pub mod stacks;
pub mod support_patches;
pub mod thin_walls;
pub mod through_steps;
pub mod turned;
pub mod turned_bodies;
pub mod turned_steps;
pub mod volume_probe;
pub mod wire_seed;

internal_entry! { pub use context::Context; }
pub use evidence::{EvidenceError, Occurrence};

use std::collections::{BTreeMap, BTreeSet};

use serde::Serialize;

use crate::kernel::brep::Part;

/// Every ported family's records for one part, computed in one run that shares its analysis.
/// `recognise` gives the records the Python aggregate's cross-family reconciliation
/// (`_reconcile_existing`, `reconcile`) accepts, with patterns derived from accepted members
/// only; `inventory` keeps every candidate and the decisions.
#[derive(Clone, Debug, Default, Serialize)]
pub struct Features {
    /// The completed run, independent of the thread-local retry flag after recognition.
    #[serde(skip)]
    pub run_mode: evidence::RunMode,
    /// Source faces of refused discovery candidates, keyed by their refusal family.
    #[serde(skip)]
    pub discovery_refusals: BTreeMap<&'static str, Vec<discovery::DiscoveryRefusal>>,
    /// Strict proof on each occurrence's defining AND consulted faces, kept through filtering.
    #[serde(skip)]
    pub evidence_status: BTreeMap<&'static str, Vec<evidence::EvidenceStatus>>,
    pub fillets: Vec<fillets::Fillet>,
    /// Residual surface projection after all accepted claims (#127), omitted when absent.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub cylindrical_profiles: Vec<cylindrical_profiles::CylindricalProfile>,
    pub chamfers: Vec<chamfers::Chamfer>,
    pub bosses: Vec<bosses::BossRecord>,
    pub angled_steps: Vec<angled_steps::AngledStep>,
    pub flats: Vec<flats::Flat>,
    pub paired_ramp_steps: Vec<paired_ramp_steps::PairedRampStep>,
    pub oriented_chamfers: Vec<oriented_chamfers::OrientedChamfer>,
    pub circular_face_patterns: Vec<circular_face_patterns::CircularFacePattern>,
    pub oblique_through_steps: Vec<oblique_through_steps::ObliqueThroughStep>,
    pub circular_blind_steps: Vec<circular_blind_steps::CircularBlindStep>,
    pub holes: Vec<holes::HoleRecord>,
    pub countersinks: Vec<countersinks::CounterSink>,
    pub hole_patterns: Vec<hole_patterns::HolePattern>,
    pub gusset_ribs: Vec<gussets::GussetRib>,
    pub gusset_rib_patterns: Vec<gussets::GussetRibPattern>,
    pub thin_wall_bodies: Vec<thin_walls::ThinWallBody>,
    pub interior_voids: Vec<interior_voids::InteriorVoid>,
    /// Refusals retain bounded-air measurements in the document, without feature claims.
    #[serde(skip)]
    pub interior_void_refusals: Vec<interior_voids::InteriorVoidRefusal>,
    pub through_steps: Vec<through_steps::ThroughStep>,
    pub turned_steps: Vec<turned_steps::TurnedStep>,
    pub grooves: Vec<grooves::Groove>,
    /// Annular face recesses, omitted when absent for existing readers.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub annular_grooves: Vec<annular_grooves::AnnularGroove>,
    /// Intrinsic face recesses on otherwise unclaimed faces (for #123).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub face_grooves: Vec<face_grooves::FaceGroove>,
    pub plates: Vec<plates::Plate>,
    /// Complete analytic turned solids (#123), co-owning faces with their features.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub turned_bodies: Vec<turned_bodies::TurnedBody>,
    pub round_bottom_blind_slots: Vec<round_bottom_slots::RoundBottomBlindSlot>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub semicircular_blind_slots: Vec<semicircular_blind_slots::SemicircularBlindSlot>,
    pub rectangular_blind_slots: Vec<rectangular_blind_slots::RectangularBlindSlot>,
    pub double_d_bores: Vec<profiled_bores::DoubleDBore>,
    pub edge_open_circular_pockets: Vec<edge_open_circular::EdgeOpenCircularPocket>,
    pub edge_open_prismatic_recesses: Vec<edge_open_prismatic::EdgeOpenPrismaticRecess>,
    pub blends: Vec<blends::Blend>,
    pub sheet_metal_bodies: Vec<sheet_metal::SheetMetalBody>,
    pub slots: Vec<slots::Slot>,
    pub pockets: Vec<pockets::Pocket>,
    pub channels: Vec<channels::Channel>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub v_channels: Vec<v_channels::VChannel>,
    /// Rectangular notches through a plate's edge, omitted when absent.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub rectangular_through_notches: Vec<through_notches::RectangularThroughNotch>,
    pub slot_patterns: Vec<recess_patterns::SlotPattern>,
    pub pocket_patterns: Vec<recess_patterns::PocketPattern>,
    pub repeating_radial_profiles: Vec<repeating_profiles::RepeatingRadialProfile>,
    pub freeform_surfaces: Vec<freeform_surfaces::FreeformSurface>,
    /// Measured helical threads. Omitted when absent to retain legacy output bytes.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub modelled_threads: Vec<modelled_threads::ModelledThread>,
    /// Refusals publish as document records, without feature fingerprints or face claims.
    #[serde(skip)]
    pub modelled_thread_refusals: Vec<modelled_threads::ModelledThreadRefusal>,
    pub polygonal_bosses: Vec<polygonal_bosses::PolygonalBoss>,
    pub polygonal_stock: Vec<polygonal_bosses::PolygonalStock>,
    /// Python's legacy inventory field (`_LegacyRecognitionResult.section_passages`): every
    /// accepted section passage (reconciliation rejects a passage a slot or an oriented slot
    /// dimensions).
    pub section_passages: Vec<passages::SectionPassage>,
    pub prismatic_pockets: Vec<prismatic_pockets::PrismaticPocket>,
    pub oriented_slots: Vec<oriented_slots::OrientedSlot>,
    pub oriented_slot_patterns: Vec<oriented_slots::OrientedSlotPattern>,
    pub pads: Vec<pads::RaisedPad>,
    /// The aggregate's step levels (`RecognitionResult.step_levels`, `_discover_step_levels`),
    /// unproved like every family here (Python's proof: `levels::proved`).
    pub step_levels: Vec<levels::FaceLevel>,
    /// The aggregate's risers (`RecognitionResult.risers`, `_discover_risers`), less those
    /// reconciliation rejects for a thin-wall body.
    pub risers: Vec<levels::RiserEvidence>,
    /// The aggregate's section recesses (`RecognitionResult.section_recesses`, projected by
    /// `_project_result` from the same run: [`section_recess_family`]), in Python's order, each
    /// defined by its record's own defining faces. `recognise` fills them; the inventory's
    /// candidates have none. Published alongside the source families in the recognition
    /// document (maintainer decision, 2026-10-11).
    pub section_recesses: Vec<section_recess::SectionRecess>,
    /// Patterns over the published recess indices, from the same reconciled projection.
    #[serde(skip)]
    pub section_recess_patterns: Vec<section_recess::SectionRecessPattern>,
    /// Accepted source candidates that cannot publish unified geometry, in Python's order.
    /// The recognition document appends these to the section-recess family.
    #[serde(skip)]
    pub section_recess_refusals: Vec<section_recess::SectionRecessRefusal>,
    /// The section-recess projection's refusal, where `recognise` carries one (and so has no
    /// section recesses).
    #[serde(skip)]
    pub section_recess_refusal: Option<section_recess_family::SectionRecessFamilyError>,
    /// Each family's defining faces, record by record, under the family's field name. Derived
    /// families (hole, gusset rib, slot, pocket and oriented slot patterns) have none of their own: their
    /// members' faces are theirs ([`crate::correspondence`]).
    #[serde(skip)]
    pub defining: Defining,
}

/// Defining faces by family field name, then record.
pub type Defining = BTreeMap<&'static str, Vec<Vec<usize>>>;

internal_entry! {
/// Recognise every ported family on *part* with default options, as Python's aggregate does:
/// holes carry their countersinks, every candidate the reconciliation rejects is dropped (with
/// its defining faces), and patterns are found among the accepted holes, gusset ribs, slots,
/// pockets and oriented slots (`_take_inventory_once`); the section recesses are projected from
/// the same run (`_project_result`). Where Python's strict run refuses an unproved hole on a
/// part whose solids are not all valid, the run is a locally degraded one, as Python's retry
/// is (`take_inventory`). A reconciliation refusal is retained as a whole-run refusal,
/// covering the input faces without issuing feature claims. Section-recess projection
/// refusals are carried in [`Features::section_recess_refusal`].
pub fn recognise(part: &Part) -> Features {
    take_inventory(part, recognise_in).unwrap_or_else(|error| Features {
        discovery_refusals: BTreeMap::from([("reconciliation_refusals", vec![
            discovery::DiscoveryRefusal::new(error, 0..part.faces.len())
        ])]),
        ..Features::default()
    })
}
}

/// `recognise` over one completed run.
fn recognise_in(ctx: &Context<'_>, inventory: Inventory) -> Features {
    let projection = section_recess_family::section_recess_projection_in(ctx, &inventory);
    let constituents = |family: &str| -> BTreeMap<Vec<usize>, Vec<usize>> {
        inventory.physical.defining[family]
            .iter()
            .cloned()
            .zip(inventory.constituent[family].iter().cloned())
            .collect()
    };
    let hole_constituents = constituents("holes");
    let boss_constituents = constituents("polygonal_bosses");
    let mut features = inventory.accepted();
    let (section_recesses, refusals, patterns, refusal) = match projection {
        Ok(p) => (p.section_recesses, p.refusals, p.patterns, None),
        Err(e) => (Vec::new(), Vec::new(), Vec::new(), Some(e)),
    };
    let defining = section_recesses
        .iter()
        .map(|r| r.evidence().defining_faces().to_vec())
        .collect();
    features.defining.insert("section_recesses", defining);
    features.evidence_status.insert(
        "section_recesses",
        section_recesses
            .iter()
            .map(|r| {
                evidence::strict_status(
                    ctx.part,
                    r.evidence().defining_faces(),
                    r.evidence().constituent_faces(),
                )
            })
            .collect(),
    );
    features.section_recesses = section_recesses;
    features.section_recess_refusals = refusals;
    features.section_recess_patterns = patterns;
    features.section_recess_refusal = refusal;
    // Complete published face claims only after every recognition, reconciliation
    // and projection decision. Constituent geometry must not alter family selection.
    for (record, faces) in features
        .holes
        .iter()
        .zip(features.defining.get_mut("holes").unwrap())
    {
        if let Some(constituent) = hole_constituents.get(faces) {
            holes::claim_drill_point(ctx.part, record, faces, constituent);
        }
    }
    for (record, faces) in features
        .slots
        .iter()
        .zip(features.defining.get_mut("slots").unwrap())
    {
        slots::claim_rectangular_end_walls(ctx.part, record, faces);
    }
    for (record, faces) in features
        .polygonal_bosses
        .iter()
        .zip(features.defining.get_mut("polygonal_bosses").unwrap())
    {
        if let Some(constituent) = boss_constituents.get(faces) {
            polygonal_bosses::claim_terminal_cap(ctx.part, record, faces, constituent);
        }
    }
    cylindrical_profiles::append(ctx, &mut features);
    turned_bodies::suppress_od_bosses(ctx.part, &mut features);
    face_grooves::append(ctx, &mut features);
    features
}

/// One run's physical candidates and the aggregate's decisions on them (Python's
/// `InventoryProduct.physical`, its evidence and its `reconciliation`).
#[derive(Clone, Debug)]
pub struct Inventory {
    /// Every family's candidates before reconciliation, each with its defining faces. The
    /// derived pattern families are empty: Python derives them from accepted members only.
    pub physical: Features,
    /// What the rules read beyond the records.
    pub evidence: reconcile::Evidence,
    /// Every decision the rules make, in rule order (no default acceptances).
    pub dispositions: Vec<reconcile::Disposition>,
    /// Each family's constituent faces, candidate by candidate under the family's field name
    /// (Python's `EvidenceIndex.constituent_of`): its defining faces and the further faces its
    /// proof consulted, sorted. The section-recess projection reads them
    /// ([`section_recess_family`]).
    pub constituent: Defining,
}

impl Inventory {
    internal_entry! {
        /// The accepted inventory (`accepted_set` on every family) with its patterns derived
        /// (`_derive_patterns`): each rejected candidate's record and defining faces dropped, in
        /// order. Section recesses are not projected here (`recognise` does, from the run).
        pub fn accepted(self) -> Features {
            let mut f = self.physical;
            let mut rejected: BTreeMap<reconcile::Family, BTreeSet<usize>> = BTreeMap::new();
            for d in &self.dispositions {
                if d.outcome == reconcile::Outcome::Rejected {
                    rejected
                        .entry(d.candidate.family)
                        .or_default()
                        .insert(d.candidate.index);
                }
            }
            for (family, indices) in &rejected {
                // Risers have no Family::field; use the same slot as their special arm below.
                let field = family.field().unwrap_or("risers");
                let statuses = f
                    .evidence_status
                    .get_mut(field)
                    .expect("every candidate has a status");
                drop_rejected(statuses, indices);
                use reconcile::Family as F;
                match family {
                    F::AngledSteps => drop_rejected(&mut f.angled_steps, indices),
                    F::Blends => drop_rejected(&mut f.blends, indices),
                    F::Bosses => drop_rejected(&mut f.bosses, indices),
                    F::Chamfers => drop_rejected(&mut f.chamfers, indices),
                    F::CircularBlindSteps => drop_rejected(&mut f.circular_blind_steps, indices),
                    F::DoubleDBores => drop_rejected(&mut f.double_d_bores, indices),
                    F::EdgeOpenCircularPockets => {
                        drop_rejected(&mut f.edge_open_circular_pockets, indices)
                    }
                    F::Fillets => drop_rejected(&mut f.fillets, indices),
                    F::Grooves => drop_rejected(&mut f.grooves, indices),
                    F::Holes => drop_rejected(&mut f.holes, indices),
                    F::OrientedSlots => drop_rejected(&mut f.oriented_slots, indices),
                    F::Passages => drop_rejected(&mut f.section_passages, indices),
                    F::Plates => drop_rejected(&mut f.plates, indices),
                    F::Pockets => drop_rejected(&mut f.pockets, indices),
                    F::PrismaticPockets => drop_rejected(&mut f.prismatic_pockets, indices),
                    F::RectangularBlindSlots => drop_rejected(&mut f.rectangular_blind_slots, indices),
                    F::Risers => {
                        // Not a reconciliation field (`Family::field`), so dropped here.
                        drop_rejected(&mut f.risers, indices);
                        let defining = f.defining.get_mut("risers").expect("risers are carried");
                        drop_rejected(defining, indices);
                        continue;
                    }
                    F::Slots => drop_rejected(&mut f.slots, indices),
                    F::ThinWallBodies => drop_rejected(&mut f.thin_wall_bodies, indices),
                    F::TurnedSteps => drop_rejected(&mut f.turned_steps, indices),
                }
                let field = family.field().expect("a carried family has a field");
                let defining = f
                    .defining
                    .get_mut(field)
                    .expect("every family has defining faces");
                drop_rejected(defining, indices);
            }
            // A notch is cut through its plate: none survives the plate's rejection.
            if let Some(plates) = rejected.get(&reconcile::Family::Plates)
                && let Some(defining) = f.defining.get_mut("rectangular_through_notches")
            {
                let gone: BTreeSet<usize> = f
                    .rectangular_through_notches
                    .iter()
                    .enumerate()
                    .filter(|(_, n)| plates.contains(&n.plate))
                    .map(|(i, _)| i)
                    .collect();
                drop_rejected(&mut f.rectangular_through_notches, &gone);
                drop_rejected(defining, &gone);
                if let Some(statuses) = f.evidence_status.get_mut("rectangular_through_notches") {
                    drop_rejected(statuses, &gone);
                }
            }
            f.hole_patterns = hole_patterns::recognise_hole_patterns(&f.holes);
            f.gusset_rib_patterns = gussets::recognise_gusset_rib_patterns(&f.gusset_ribs);
            f.slot_patterns = recess_patterns::recognise_slot_patterns(&f.slots);
            f.pocket_patterns = recess_patterns::recognise_pocket_patterns(&f.pockets);
            f.oriented_slot_patterns =
                oriented_slots::recognise_oriented_slot_patterns(&f.oriented_slots);
            f
        }
    }
}

/// *items* without those at *indices*, in order.
fn drop_rejected<T>(items: &mut Vec<T>, indices: &BTreeSet<usize>) {
    let mut at = 0;
    items.retain(|_| {
        let keep = !indices.contains(&at);
        at += 1;
        keep
    });
}

internal_entry! {
/// Every ported family's candidates on *part* with default options and the reconciliation's
/// decisions on them, or the reconciliation's refusal; locally degraded where Python's
/// aggregate retries so (`take_inventory`).
pub fn inventory(part: &Part) -> Result<Inventory, reconcile::ReconcileError> {
    take_inventory(part, |_, inventory| inventory)
}
}

/// Python's `_take_inventory` with its bounded fallback: one strict run, or, where that run's
/// hole evidence refuses for want of one valid solid on a part whose solids are not all valid
/// ([`holes::strict_refusal_retries`]), one run again from scratch with `local_degradation`
/// set ([`evidence::locally_degraded`]). *then* reads the completed run (its context and
/// inventory) inside the same mode, so a projection of it answers as that run's graph does.
///
/// The port's strict run proves no other family's evidence, so this hole refusal is the only
/// strict refusal it retries on, as Python's aggregate retries on no other.
pub(crate) fn take_inventory<'p, T>(
    part: &'p Part,
    then: impl FnOnce(&Context<'p>, Inventory) -> T,
) -> Result<T, reconcile::ReconcileError> {
    if !evidence::local_degradation() {
        let ctx = Context::new(part);
        if let Some(inventory) = inventory_in(&ctx)? {
            return Ok(then(&ctx, inventory));
        }
    }
    evidence::locally_degraded(|| {
        let ctx = Context::new(part);
        let inventory = inventory_in(&ctx)?.expect("a locally degraded run does not retry");
        Ok(then(&ctx, inventory))
    })
}

/// *found* less, in a locally degraded run, the occurrences *proved* fails: the records such a
/// run skips rather than refuses. A strict run keeps them all (it publishes unproved).
fn degraded_skip<R>(
    found: Vec<Occurrence<R>>,
    proved: impl Fn(&Occurrence<R>) -> bool,
) -> Vec<Occurrence<R>> {
    if !evidence::local_degradation() {
        return found;
    }
    found.into_iter().filter(|o| proved(o)).collect()
}

/// [`degraded_skip`] on each occurrence's defining and consulted faces together
/// ([`evidence::locally_proved`]): the check most of Python's families make on a degraded graph.
fn skip_unproved<R>(part: &Part, found: Vec<Occurrence<R>>) -> Vec<Occurrence<R>> {
    degraded_skip(found, |o| evidence::locally_proved(part, o))
}

/// One run of `inventory` on a context the caller keeps, so a projection of it reads the same
/// analysis, in the current mode ([`evidence::local_degradation`]). `None` where a strict run
/// would retry ([`take_inventory`]): as Python's raises, the run stops at its holes. A locally
/// degraded run skips the records Python's families skip in that mode where the port has the
/// family's check: holes, pockets, slots, channels, fillets, prismatic pockets and edge-open
/// prismatic and circular recesses (on defining and consulted faces, [`skip_unproved`]), step
/// levels and risers (on their defining faces, as `levels::proved`), plates (in
/// [`plates::discover`]) and rectangular pads (in [`pads::discover`]).
pub(crate) fn inventory_in(
    ctx: &Context<'_>,
) -> Result<Option<Inventory>, reconcile::ReconcileError> {
    let part = ctx.part;
    let seats = countersinks::discover(ctx);
    let found_holes = holes::discover(ctx, &seats);
    if !evidence::local_degradation() && holes::strict_refusal_retries(part, &seats, &found_holes) {
        return Ok(None);
    }
    let found_holes = skip_unproved(part, found_holes);
    let mut defining = Kept {
        part,
        claimed: Defining::new(),
        defining: Defining::new(),
        constituent: Defining::new(),
        evidence_status: BTreeMap::new(),
    };
    let holes = kept(&mut defining, "holes", found_holes);
    let countersinks = kept(&mut defining, "countersinks", seats);
    let gusset_ribs = kept(&mut defining, "gusset_ribs", gussets::discover(ctx));
    let slots = kept(
        &mut defining,
        "slots",
        skip_unproved(part, slots::discover(ctx)),
    );
    let pocket_occurrences = skip_unproved(part, pockets::discover(ctx));
    let thin_wall_bodies =
        kept_shared(&mut defining, "thin_wall_bodies", thin_walls::discover(ctx));
    let mut discovery_refusals = BTreeMap::new();
    let passages = match passages::discover(ctx) {
        Ok(found) => found,
        Err(error) => {
            let faces = passages::proposal_faces(ctx);
            discovery_refusals.insert(
                "section_passage_refusals",
                vec![discovery::DiscoveryRefusal::new(error, faces)],
            );
            Vec::new()
        }
    };
    // Reconciliation must read the same completed passages; a refused family has no
    // compatibility views and cannot suppress a pocket or slot.
    let compatibility = if discovery_refusals.contains_key("section_passage_refusals") {
        Vec::new()
    } else {
        passages::compatibility_views(ctx)?
    };
    let evidence = reconcile::Evidence::with_passage_views(ctx, &pocket_occurrences, compatibility);
    let pockets = kept(&mut defining, "pockets", pocket_occurrences);
    let oriented_slots = kept(
        &mut defining,
        "oriented_slots",
        match oriented_slots::discover(ctx, &passages) {
            Ok(found) => found,
            Err(error) => {
                discovery_refusals.insert(
                    "oriented_slot_refusals",
                    vec![discovery::DiscoveryRefusal::new(
                        error,
                        passages
                            .iter()
                            .flat_map(|p| p.defining.iter().chain(&p.context))
                            .copied(),
                    )],
                );
                Vec::new()
            }
        },
    );
    let thread_analysis = ctx.modelled_threads();
    let claimed: BTreeSet<_> = thread_analysis
        .threads
        .iter()
        .flat_map(|o| o.defining.iter().copied())
        .collect();
    let modelled_threads = if thread_analysis.threads.is_empty() {
        Vec::new()
    } else {
        kept(
            &mut defining,
            "modelled_threads",
            thread_analysis.threads.clone(),
        )
    };
    let freeform_surfaces = kept_shared(
        &mut defining,
        "freeform_surfaces",
        freeform_surfaces::discover(ctx, &thin_wall_bodies)
            .into_iter()
            .filter(|o| !claimed.contains(&o.record.face))
            .collect(),
    );
    // A degraded run's risers carry the step levels it kept, as Python's carry that run's
    // completed levels (`levels::proved` skips only in a degraded run).
    let step_level_occurrences = degraded_skip(levels::discover_step_levels(ctx), |o| {
        evidence::locally_valid_solid(part, &o.defining).is_some()
    });
    let found_risers = degraded_skip(levels::discover_risers(ctx, &step_level_occurrences), |o| {
        evidence::locally_valid_solid(part, &o.defining).is_some()
    });
    let risers = kept(&mut defining, "risers", found_risers);
    let step_levels = kept(&mut defining, "step_levels", step_level_occurrences);
    let void_analysis = interior_voids::analyse(ctx);
    let plate_occurrences = plates::discover(ctx, &Default::default());
    let channel_occurrences = skip_unproved(part, channels::discover(ctx));
    let notches = through_notches::discover(ctx, &plate_occurrences, &channel_occurrences);
    let boss_analysis = polygonal_bosses::analyse(ctx, &Default::default());
    if !boss_analysis.refusals.is_empty() {
        discovery_refusals.insert("polygonal_boss_refusals", boss_analysis.refusals);
    }
    let pad_analysis = pads::analyse(ctx, &Default::default());
    let prismatic_analysis = prismatic_pockets::analyse(ctx);
    if !pad_analysis.refusals.is_empty() {
        discovery_refusals.insert("pad_refusals", pad_analysis.refusals);
    }
    if !prismatic_analysis.refusals.is_empty() {
        discovery_refusals.insert("prismatic_pocket_refusals", prismatic_analysis.refusals);
    }
    let mut physical = Features {
        discovery_refusals,
        run_mode: if evidence::local_degradation() {
            evidence::RunMode::LocallyDegraded
        } else {
            evidence::RunMode::Strict
        },
        cylindrical_profiles: Vec::new(),
        chamfers: kept(
            &mut defining,
            "chamfers",
            chamfers::discover(ctx, &Default::default()),
        ),
        bosses: kept(&mut defining, "bosses", bosses::discover(ctx)),
        angled_steps: kept(&mut defining, "angled_steps", angled_steps::discover(ctx)),
        flats: kept(&mut defining, "flats", flats::discover(ctx)),
        paired_ramp_steps: kept(
            &mut defining,
            "paired_ramp_steps",
            paired_ramp_steps::discover(ctx),
        ),
        oriented_chamfers: kept(
            &mut defining,
            "oriented_chamfers",
            oriented_chamfers::discover(ctx, &Default::default()),
        ),
        circular_face_patterns: kept(
            &mut defining,
            "circular_face_patterns",
            circular_face_patterns::discover(ctx),
        ),
        oblique_through_steps: kept(
            &mut defining,
            "oblique_through_steps",
            oblique_through_steps::discover(ctx),
        ),
        circular_blind_steps: kept(
            &mut defining,
            "circular_blind_steps",
            circular_blind_steps::discover(ctx),
        ),
        hole_patterns: Vec::new(),
        holes,
        gusset_rib_patterns: Vec::new(),
        gusset_ribs,
        countersinks,
        thin_wall_bodies,
        interior_voids: kept(&mut defining, "interior_voids", void_analysis.accepted),
        interior_void_refusals: void_analysis.refusals,
        through_steps: kept(&mut defining, "through_steps", through_steps::discover(ctx)),
        turned_steps: kept(
            &mut defining,
            "turned_steps",
            turned_steps::sorted_occurrences(turned_steps::discover(ctx)),
        ),
        grooves: kept(&mut defining, "grooves", grooves::discover(ctx)),
        annular_grooves: {
            let found = annular_grooves::discover(ctx);
            if found.is_empty() {
                Vec::new()
            } else {
                kept(&mut defining, "annular_grooves", found)
            }
        },
        face_grooves: Vec::new(),
        plates: kept_shared(&mut defining, "plates", plate_occurrences),
        turned_bodies: {
            let found = turned_bodies::discover(ctx);
            if found.is_empty() {
                Vec::new()
            } else {
                kept_shared(&mut defining, "turned_bodies", found)
            }
        },
        round_bottom_blind_slots: kept(
            &mut defining,
            "round_bottom_blind_slots",
            round_bottom_slots::discover(ctx),
        ),
        semicircular_blind_slots: {
            let found = semicircular_blind_slots::discover(ctx);
            if found.is_empty() {
                Vec::new()
            } else {
                kept(&mut defining, "semicircular_blind_slots", found)
            }
        },
        rectangular_blind_slots: kept(
            &mut defining,
            "rectangular_blind_slots",
            rectangular_blind_slots::discover(ctx),
        ),
        double_d_bores: kept(
            &mut defining,
            "double_d_bores",
            profiled_bores::discover(ctx),
        ),
        edge_open_circular_pockets: kept(
            &mut defining,
            "edge_open_circular_pockets",
            skip_unproved(part, edge_open_circular::discover(ctx)),
        ),
        edge_open_prismatic_recesses: kept(
            &mut defining,
            "edge_open_prismatic_recesses",
            skip_unproved(part, edge_open_prismatic::discover(ctx)),
        ),
        blends: kept_shared(&mut defining, "blends", blends::discover(ctx)),
        sheet_metal_bodies: kept_shared(
            &mut defining,
            "sheet_metal_bodies",
            sheet_metal::discover(ctx),
        ),
        slot_patterns: Vec::new(),
        slots,
        pocket_patterns: Vec::new(),
        pockets,
        v_channels: {
            let found = v_channels::discover(ctx, &passages);
            if found.is_empty() {
                Vec::new()
            } else {
                kept(&mut defining, "v_channels", found)
            }
        },
        channels: kept(&mut defining, "channels", channel_occurrences),
        rectangular_through_notches: if notches.is_empty() {
            Vec::new()
        } else {
            kept(&mut defining, "rectangular_through_notches", notches)
        },
        repeating_radial_profiles: kept(
            &mut defining,
            "repeating_radial_profiles",
            repeating_profiles::discover(ctx),
        ),
        freeform_surfaces,
        modelled_threads,
        modelled_thread_refusals: thread_analysis.refusals.clone(),
        polygonal_bosses: kept(&mut defining, "polygonal_bosses", boss_analysis.accepted),
        polygonal_stock: kept_shared(
            &mut defining,
            "polygonal_stock",
            polygonal_bosses::discover_stock(ctx, &Default::default()),
        ),
        section_passages: kept(&mut defining, "section_passages", passages),
        prismatic_pockets: kept(
            &mut defining,
            "prismatic_pockets",
            skip_unproved(part, prismatic_analysis.accepted),
        ),
        oriented_slot_patterns: Vec::new(),
        oriented_slots,
        pads: kept(&mut defining, "pads", pad_analysis.accepted),
        step_levels,
        risers,
        section_recesses: Vec::new(),
        section_recess_refusals: Vec::new(),
        section_recess_patterns: Vec::new(),
        section_recess_refusal: None,
        fillets: kept_shared(
            &mut defining,
            "fillets",
            skip_unproved(part, fillets::discover(ctx, &Default::default())),
        ),
        defining: defining.defining,
        evidence_status: defining.evidence_status,
    };
    // The thin-wall rule reads the risers this inventory carries, in its order.
    let mut evidence = evidence;
    evidence.risers = physical.defining["risers"].clone();
    let mut dispositions = reconcile::reconcile(&physical, &evidence)?;
    // Specific accepted features reserve their defining faces before edge rounds.
    // Rejected proposals cannot suppress a fillet. Body classifications and generic
    // surface fallbacks use kept_shared; derived patterns inherit their members' claims.
    let rejected: BTreeSet<_> = dispositions
        .iter()
        .filter(|d| d.outcome == reconcile::Outcome::Rejected)
        .map(|d| {
            (
                d.candidate.family.field().unwrap_or("risers"),
                d.candidate.index,
            )
        })
        .collect();
    let claimed: BTreeSet<_> = defining
        .claimed
        .iter()
        .flat_map(|(family, records)| {
            records
                .iter()
                .enumerate()
                .filter(|(i, _)| !rejected.contains(&(*family, *i)))
                .flat_map(|(_, faces)| faces.iter().copied())
        })
        .collect();
    let gone: BTreeSet<_> = physical.defining["fillets"]
        .iter()
        .enumerate()
        // Existing precedence decisions already exclude these proposals and keep their
        // reconciliation evidence. Only otherwise-surviving fillets need filtering.
        .filter(|(i, faces)| {
            !rejected.contains(&("fillets", *i)) && faces.iter().any(|f| claimed.contains(f))
        })
        .map(|(i, _)| i)
        .collect();
    if !gone.is_empty() {
        drop_rejected(&mut physical.fillets, &gone);
        drop_rejected(physical.defining.get_mut("fillets").unwrap(), &gone);
        drop_rejected(physical.evidence_status.get_mut("fillets").unwrap(), &gone);
        drop_rejected(defining.constituent.get_mut("fillets").unwrap(), &gone);
        // A suppressed fillet cannot defeat a blend; indices follow the filtered records.
        dispositions = reconcile::reconcile(&physical, &evidence)?;
    }
    let mut inventory = Inventory {
        physical,
        evidence,
        dispositions,
        constituent: defining.constituent,
    };
    // For #127, new inside rounds also respect the complete defining claims of the
    // section-recess projection. Those claims include cavity treatments that the seed
    // pocket only consulted. Existing convex records retain their established ownership.
    if inventory
        .physical
        .fillets
        .iter()
        .any(|f| f.side == "concave")
        && let Ok(projection) = section_recess_family::section_recess_projection_in(ctx, &inventory)
    {
        let reserved: BTreeSet<_> = projection
            .section_recesses
            .iter()
            .flat_map(|r| r.evidence().defining_faces().iter().copied())
            .collect();
        let gone: BTreeSet<_> = inventory
            .physical
            .fillets
            .iter()
            .enumerate()
            .filter(|(i, f)| {
                f.side == "concave"
                    && inventory.physical.defining["fillets"][*i]
                        .iter()
                        .any(|f| reserved.contains(f))
            })
            .map(|(i, _)| i)
            .collect();
        if !gone.is_empty() {
            drop_rejected(&mut inventory.physical.fillets, &gone);
            drop_rejected(
                inventory.physical.defining.get_mut("fillets").unwrap(),
                &gone,
            );
            drop_rejected(
                inventory
                    .physical
                    .evidence_status
                    .get_mut("fillets")
                    .unwrap(),
                &gone,
            );
            drop_rejected(inventory.constituent.get_mut("fillets").unwrap(), &gone);
            inventory.dispositions =
                reconcile::reconcile(&inventory.physical, &inventory.evidence)?;
        }
    }
    Ok(Some(inventory))
}

/// Defining and constituent faces by family field name, then record.
struct Kept<'p> {
    part: &'p Part,
    claimed: Defining,
    defining: Defining,
    constituent: Defining,
    evidence_status: BTreeMap<&'static str, Vec<evidence::EvidenceStatus>>,
}

/// The records of a family's occurrences, their defining and constituent faces kept under the
/// family's name.
fn kept<R>(kept: &mut Kept<'_>, family: &'static str, found: Vec<Occurrence<R>>) -> Vec<R> {
    kept.claimed
        .insert(family, found.iter().map(|o| o.defining.clone()).collect());
    kept_shared(kept, family, found)
}

/// Whole-body classifications and generic surface fallbacks retain evidence without
/// reserving their faces against a specific feature discovered later.
fn kept_shared<R>(kept: &mut Kept<'_>, family: &'static str, found: Vec<Occurrence<R>>) -> Vec<R> {
    kept.evidence_status.insert(
        family,
        found
            .iter()
            .map(|o| evidence::strict_status(kept.part, &o.defining, &o.context))
            .collect(),
    );
    kept.defining
        .insert(family, found.iter().map(|o| o.defining.clone()).collect());
    kept.constituent.insert(
        family,
        found
            .iter()
            .map(|o| {
                let faces: BTreeSet<usize> = o.defining.iter().chain(&o.context).copied().collect();
                faces.into_iter().collect()
            })
            .collect(),
    );
    records(found)
}

internal_entry! {
/// The records of a family's occurrences, in order.
pub fn records<R>(found: Vec<Occurrence<R>>) -> Vec<R> {
    found.into_iter().map(|o| o.record).collect()
}
}