Skip to main content

brep_kernel/offset/offset_shell/
pipeline.rs

1use crate::{KernelRefusal, KernelStage, OrRefuse};
2use super::*;
3
4/// Put every face on one normal convention (surface normal on the CCW
5/// parameter-winding side) and then restore outward normals. The offset shell's
6/// source and offset skins are assembled as independent components whose
7/// per-face `same_sense` can follow opposite conventions; `validate()` only
8/// checks coedge-direction coherence (the `forward` flags), so a joined,
9/// watertight, coedge-coherent manifold can still be normal-incoherent and
10/// report the wrong volume. Re-deriving `same_sense` from the loop winding makes
11/// the coedge-coherent manifold normal-coherent; the signed-volume flip then
12/// orients it outward. Only invoked after a non-coplanar rim weld, so the
13/// coplanar cylinder/prism shells keep their exact assembled orientation.
14/// `BREP_OFFSET_ONE_CALL_IMPRINT` (lane P3b row 6, 2026-10-26): one
15/// `build_imprints` over a composite of every carrier instead of one call per
16/// pair. On by default (promoted 2026-10-27 after the 98 + 144 case oracle
17/// census); `0`/`off` retains the per-pair calls. Re-pinned by independent
18/// oracles, not byte-identical (piece and vertex numbering is the single
19/// call's).
20fn one_call_imprint_enabled() -> bool {
21    switch!("BREP_OFFSET_ONE_CALL_IMPRINT", true)
22}
23
24thread_local! {
25    static ONE_CALL_RUNS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
26}
27/// Shell or offset operations whose imprint the one call produced on this thread since the last call.
28pub(super) fn take_one_call_runs() -> u64 {
29    ONE_CALL_RUNS.with(|count| count.replace(0))
30}
31
32
33fn coheres_face_normals(solid: &mut BrepSolid) -> Result<(), KernelRefusal> {
34    for face in solid.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
35        face.same_sense = parameter_space_area(face).or_refuse(KernelStage::Sew, "parameter_space_area")? > 0.0;
36    }
37    // Outward is a PER-SHELL property: a through-hole shelled at both ends
38    // legitimately produces two disjoint closed components (the outer wall
39    // tube and the hole wall tube), and a single global signed-volume flip
40    // cannot orient both.
41    for shell_index in 0..solid.shells.len() {
42        let single = BrepSolid {
43            mass_properties_cache: Default::default(),
44            cells: Default::default(),
45            id: solid.id,
46            vertices: solid.vertices.clone(),
47            edges: solid.edges.clone(),
48            shells: vec![solid.shells[shell_index].clone()],
49            genus: 0,
50            kind: crate::BodyKind::Solid,
51        };
52        // Read for its SIGN only, through the per-face term memo
53        // (`solid_signed_volume_for_sign`: the verdict against the bar is the
54        // exact integral's, and a face read about the same reference before —
55        // the next feature's push, the boolean assembler's own check — is
56        // served its term). The exact read here cost 0.26 s on the shelled-box
57        // report's 34-face shell, 42 of its faces fitted.
58        if crate::signed_volume_against(&single, 0.0).or_refuse(KernelStage::Sew, "solid_signed_volume")? < 0.0 {
59            flip_shell_faces(&mut solid.shells[shell_index])?;
60        }
61    }
62    Ok(())
63}
64
65/// Mint a vertex at every SELF-TOUCH of the source's faces before the shell
66/// reads their trims.
67///
68/// A face whose loops touch at an EDGE INTERIOR is a pinched region: the
69/// reported 2026-09-09 document's r = 6 through-hole kisses both r = 4 fillet
70/// setbacks on the top face, at (16, 20, 10) and (10, 20, 16), with no vertex
71/// there.  The fragment stage's 2D arrangement resolves such a pinch by
72/// carving the touched-off corner into its own region, whose loop walks the
73/// closed hole chain in a PARTIAL run — which `build_loop` can only assemble
74/// when the touch is a vertex of both touching edges, and otherwise refuses
75/// with "chain N fragmented into an incomplete run".
76///
77/// The boolean lane mints that vertex inside `build_imprints`
78/// (`imprint/self_touch.rs`).  Nothing minted it here: the shell fragments
79/// its SOURCE against an EMPTY imprint, and its offset carriers — whose trims
80/// are copied from the source faces — deliberately keep their boundaries
81/// un-split, so both arrived at the arrangement pinched.  Splitting the
82/// source once, before the carriers are built from it, fixes both: every
83/// carrier inherits the split trim by construction.
84///
85/// Returns `None` when no face self-touches, so every part that was never
86/// pinched keeps its exact previous path.  Escape hatch:
87/// `BREP_SELF_TOUCH_SPLIT=0`, the same one the imprint lane honours.
88fn split_self_touching_source(source: &BrepSolid) -> Result<Option<BrepSolid>, KernelRefusal> {
89    if std::env::var("BREP_SELF_TOUCH_SPLIT").as_deref() == Ok("0") {
90        return Ok(None);
91    }
92    // The same derivation `offset_shell_impl` uses for every other band, so
93    // the source scan and the carrier-pair scans agree on what "touching"
94    // means.
95    let scale = crate::solid_scale(source);
96    let tolerance = 1e-7f64.max(scale * 1e-9);
97    let edge_splits = crate::imprint::self_touch_edge_splits(source, SOURCE_OPERAND, tolerance, scale)?;
98    if edge_splits.is_empty() {
99        return Ok(None);
100    }
101    os_debug!(
102        "self-touch: source split on {} edge(s): {:?}",
103        edge_splits.len(),
104        edge_splits
105            .iter()
106            .map(|split| split.edge_id)
107            .collect::<Vec<_>>(),
108    );
109    let imprint = ImprintResultRecord {
110        edge_splits,
111        ..empty_imprint()
112    };
113    Ok(Some(apply_edge_splits(source, SOURCE_OPERAND, &imprint)?))
114}
115
116/// How far past the grown outline a planar opening wall that meets a RULED
117/// opening is padded, in units of the offset-skin band the wall selection reads
118/// (see the wall construction).
119///
120/// Measured on the torus wedge through the pipeline, grown by 0.05, 0.1, 0.5
121/// and 0.999 (band 2e-3): a clearance of 2 or 2.5 bands still shreds the cap
122/// into 22 to 36 faces with the area 5% to 11% high; from 4 bands on every
123/// member builds its 6 faces, and from 4 to 32 bands each member's volume moves
124/// by at most 1.2e-7. 16 sits four times above the first clean reading.
125const RULED_NEIGHBOUR_CLEARANCE_BANDS: f64 = 16.0;
126
127/// How far past its opening plane an inward carrier is grown, as a fraction of
128/// the shell distance (see the fall-short reach in the carrier loop).
129const REACH_CLEARANCE: f64 = 0.25;
130
131/// What the pipeline assembles.
132#[derive(Clone, Copy, Debug, Eq, PartialEq)]
133pub(super) enum Body {
134    /// A shell: the retained source faces, their offset skins and the walls
135    /// at the openings, bounding the material between source and offset.
136    Shell,
137    /// The offset body itself: the offset skins alone, closed by their
138    /// mutual sections; `opening_face_ids` are HELD faces, copied at
139    /// distance zero as walls, with no source face assembled and no skin
140    /// flipped. The whole-solid offset and the boolean fallback's offset
141    /// body are built this way.
142    Whole,
143}
144
145
146#[cfg(not(test))]
147fn forced_direct_refusal() -> Option<KernelRefusal> {
148    None
149}
150
151/// `BREP_OFFSET_EVERY_SKIN_PIECE` (default on; `=0` keeps the seed's one
152/// piece): see EVERY SKIN PIECE in the offset selection.
153fn every_skin_piece_enabled() -> bool {
154    crate::switch!("BREP_OFFSET_EVERY_SKIN_PIECE", true)
155}
156
157/// `BREP_OFFSET_EVERY_SEEDED_SKIN_PIECE` (default on; `=0` restores): when an
158/// inward shell's assembly fails, EVERY SKIN PIECE is retried on the seeded
159/// path too. Past 2.0 a reported box and pin model's bore skin comes apart into one piece
160/// per corner channel and the seed answered for one of them. Only as a retry:
161/// a shell the seed's choice closes keeps that answer.
162fn every_seeded_skin_piece_enabled() -> bool {
163    crate::switch!("BREP_OFFSET_EVERY_SEEDED_SKIN_PIECE", true)
164}
165
166pub fn offset_shell(
167    source: &BrepSolid,
168    opening_face_ids: &[u64],
169    distance: f64,
170) -> Result<OffsetShellResultRecord, KernelRefusal> {
171    let direct = match forced_direct_refusal() {
172        Some(forced) => Err(forced),
173        None => offset_body(source, opening_face_ids, distance, Body::Shell, "offset_shell"),
174    };
175    let mut shelled = offset_shell_lanes(source, opening_face_ids, distance, direct)?;
176    if membrane_shells_on() {
177        drop_folded_shells(&mut shelled.solid, distance);
178        let membranes = membrane_shell_indices(&shelled.solid, distance);
179        let paired = paired_face_ids(&shelled.solid);
180        if !membranes.is_empty() || !paired.is_empty() {
181            for (first, second) in &paired {
182                let shell = shelled.solid.shells.iter().position(|shell| shell.faces.iter().any(|face| face.id == *first));
183                os_debug!(
184                    "membrane pair: faces {first} and {second} in shell {shell:?} of {:?} faces",
185                    shell.map(|index| shelled.solid.shells[index].faces.len())
186                );
187            }
188            return Err(KernelRefusal::unsupported(KernelStage::Validate, "offset_shell_membrane", format!(
189                "offset_shell: at this distance the result has {} shell(s) enclosing no volume and \
190                 {} pair(s) of faces bounded by the same edges{} (zero-thickness membranes \
191                 beside the cavity), which no solid carries",
192                membranes.len(),
193                paired.len(),
194                paired.first().map(|(a, b)| format!(", first faces {a} and {b}")).unwrap_or_default()
195            )));
196        }
197    }
198    Ok(shelled)
199}
200
201/// `BREP_OFFSET_MEMBRANE_SHELLS` (default on; `=0` restores): a shell of the
202/// result that encloses nothing (made only of FOLDED faces, each loop running
203/// along its edges once in each direction, closed by point edges; or of zero
204/// volume over its area; or a bag, its faces pairing up on the same edges) is
205/// removed with the edges and vertices only it used, when the
206/// rest still validates. Otherwise, and for two faces of one real shell bounded
207/// by the same edges (a zero-thickness fin), the shell is refused as a
208/// membrane.
209///
210/// Cause (a reported box and pin model): at 2.38 and 2.40 the result reported
211/// three shells, two of them single folded faces of about 6.5e-6 mm^2 at the
212/// floor/wall corner; at 2.46 (with the corner-debris switch) six more, folded
213/// faces and two-face bags of volume 1e-8 over area 4e-3. None shares an edge
214/// with the cavity's shell. `validate()` accepts them all and the volume is
215/// right, but the shell count is not, nor is anything that walks the shells.
216///
217/// Every test is scaled by the offset distance, since a real piece of the shell
218/// is about |distance| thick: a folded or bag shell is removed only when its
219/// volume is at most 1% of |distance| × its area (a sphere face, whose seam
220/// also runs both ways, encloses the sealed cavity of a sphere-cut shell), and
221/// the zero-volume bar never exceeds 1e-3 × |distance| (a shell 1e-7 thick is
222/// a real sliver, not a membrane).
223fn membrane_shells_on() -> bool {
224    switch!("BREP_OFFSET_MEMBRANE_SHELLS", true)
225}
226
227/// Whether every non-degenerate edge of `face`'s loops is used once forward
228/// and once reversed by the face itself.
229fn face_is_folded(face: &FaceRecord, edges: &HashMap<u64, &EdgeRecord>) -> bool {
230    let mut balance: HashMap<u64, (usize, usize)> = HashMap::default();
231    for coedge in face.loops.iter().flat_map(|l| &l.coedges) {
232        if edges.get(&coedge.edge_id).is_some_and(|edge| edge.degenerate) {
233            continue;
234        }
235        let entry = balance.entry(coedge.edge_id).or_default();
236        if coedge.forward {
237            entry.0 += 1;
238        } else {
239            entry.1 += 1;
240        }
241    }
242    !balance.is_empty() && balance.values().all(|&(forward, reversed)| forward == 1 && reversed == 1)
243}
244
245/// Whether every face of `shell` shares its whole set of non-degenerate edges
246/// with exactly one other face of it: two-sided sheets with nothing between.
247fn shell_is_bag(shell: &ShellRecord, edges: &HashMap<u64, &EdgeRecord>) -> bool {
248    let mut sets: HashMap<Vec<u64>, usize> = HashMap::default();
249    for face in &shell.faces {
250        let mut set: Vec<u64> = face
251            .loops
252            .iter()
253            .flat_map(|l| &l.coedges)
254            .map(|coedge| coedge.edge_id)
255            .filter(|id| !edges.get(id).is_some_and(|edge| edge.degenerate))
256            .collect();
257        set.sort_unstable();
258        set.dedup();
259        if set.is_empty() {
260            return false;
261        }
262        *sets.entry(set).or_default() += 1;
263    }
264    !sets.is_empty() && sets.values().all(|&count| count == 2)
265}
266
267/// Removes the shells made only of folded faces, then the edges and vertices
268/// no remaining face uses. Keeps `solid` unchanged when that would leave no
269/// shell or a result that does not validate. A folded or bag shell qualifies
270/// only when its volume is at most 1% of |distance| × its area.
271pub(crate) fn drop_folded_shells(solid: &mut BrepSolid, distance: f64) {
272    let edges: HashMap<u64, &EdgeRecord> = solid.edges.iter().map(|edge| (edge.id, edge)).collect();
273    let empty = membrane_shell_indices(solid, distance);
274    let folded: Vec<usize> = (0..solid.shells.len())
275        .filter(|&index| {
276            let shell = &solid.shells[index];
277            empty.contains(&index)
278                || ((shell.faces.iter().all(|face| face_is_folded(face, &edges)) || shell_is_bag(shell, &edges))
279                    && shell_volume_and_area(solid, index)
280                        .is_some_and(|(volume, area)| volume.abs() <= 1e-2 * distance.abs() * area))
281        })
282        .collect();
283    if folded.is_empty() || folded.len() == solid.shells.len() {
284        return;
285    }
286    let mut trimmed = solid.clone();
287    let mut index = 0;
288    trimmed.shells.retain(|_| {
289        let keep = !folded.contains(&index);
290        index += 1;
291        keep
292    });
293    let used_edges: HashSet<u64> = trimmed
294        .shells
295        .iter()
296        .flat_map(|shell| &shell.faces)
297        .flat_map(|face| &face.loops)
298        .flat_map(|l| &l.coedges)
299        .map(|coedge| coedge.edge_id)
300        .collect();
301    trimmed.edges.retain(|edge| used_edges.contains(&edge.id));
302    let used_vertices: HashSet<u64> = trimmed
303        .edges
304        .iter()
305        .flat_map(|edge| [edge.start_vertex_id, edge.end_vertex_id])
306        .collect();
307    trimmed.vertices.retain(|vertex| used_vertices.contains(&vertex.id));
308    crate::boolean::refresh_derived_genus(&mut trimmed);
309    let issues = trimmed.validate();
310    if !issues.is_empty() {
311        os_debug!("folded shells {folded:?} kept: without them the result does not validate: {issues:?}");
312        return;
313    }
314    os_debug!("dropped folded shells {folded:?}");
315    *solid = trimmed;
316}
317
318/// Pairs of faces of one shell bounded by exactly the same non-degenerate
319/// edges: two sides of a zero-thickness fin. By face id.
320pub(crate) fn paired_face_ids(solid: &BrepSolid) -> Vec<(u64, u64)> {
321    let mut pairs = Vec::new();
322    let degenerate: HashSet<u64> = solid.edges.iter().filter(|edge| edge.degenerate).map(|edge| edge.id).collect();
323    for shell in &solid.shells {
324        let mut seen: HashMap<Vec<u64>, u64> = HashMap::default();
325        for face in &shell.faces {
326            let mut edges: Vec<u64> = face
327                .loops
328                .iter()
329                .flat_map(|l| &l.coedges)
330                .map(|c| c.edge_id)
331                .filter(|id| !degenerate.contains(id))
332                .collect();
333            edges.sort_unstable();
334            if edges.len() < 2 {
335                continue;
336            }
337            match seen.get(&edges) {
338                Some(&other) => pairs.push((other, face.id)),
339                None => {
340                    seen.insert(edges, face.id);
341                }
342            }
343        }
344    }
345    pairs
346}
347
348/// The volume and area of shell `index` of `solid` on its own.
349fn shell_volume_and_area(solid: &BrepSolid, index: usize) -> Option<(f64, f64)> {
350    let mut alone = solid.clone();
351    alone.shells = vec![solid.shells[index].clone()];
352    crate::solid_mass_properties(&alone).ok().map(|mass| (mass.volume, mass.surface_area))
353}
354
355/// The shells of `solid` that enclose no volume, by index: |V| at most
356/// min(1e-6 × the bounding-box diagonal, 1e-3 × |distance|) × the shell's
357/// area. A shell whose mass properties cannot be measured is never reported.
358pub(crate) fn membrane_shell_indices(solid: &BrepSolid, distance: f64) -> Vec<usize> {
359    if solid.vertices.is_empty() {
360        return Vec::new();
361    }
362    let mut lo = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
363    let mut hi = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
364    for vertex in &solid.vertices {
365        lo = Vec3::new(lo.x.min(vertex.point.x), lo.y.min(vertex.point.y), lo.z.min(vertex.point.z));
366        hi = Vec3::new(hi.x.max(vertex.point.x), hi.y.max(vertex.point.y), hi.z.max(vertex.point.z));
367    }
368    let thickness = (1e-6 * hi.sub(lo).length()).min(1e-3 * distance.abs());
369    (0..solid.shells.len())
370        .filter(|&index| {
371            shell_volume_and_area(solid, index).is_some_and(|(volume, area)| volume.abs() <= thickness * area)
372        })
373        .collect()
374}
375
376fn offset_shell_lanes(
377    source: &BrepSolid,
378    opening_face_ids: &[u64],
379    distance: f64,
380    direct: Result<OffsetShellResultRecord, KernelRefusal>,
381) -> Result<OffsetShellResultRecord, KernelRefusal> {
382    match direct {
383        Ok(result) => Ok(result),
384        // The boolean lane answers the direct construction's own named
385        // refusals; when it refuses too, the direct refusal is the one
386        // reported, because it names the cause.
387        Err(direct) if super::solid::direct_refusal_takes_the_boolean_lane(&direct) => {
388            os_debug!("direct pipeline refused ({direct}); taking the boolean lane");
389            match super::solid::offset_shell_by_boolean(source, opening_face_ids, distance) {
390                Ok(result) => Ok(result),
391                Err(boolean) => {
392                    os_debug!("boolean lane refused too ({boolean})");
393                    Err(direct)
394                }
395            }
396        }
397        Err(direct) => Err(direct),
398    }
399}
400
401/// The pipeline's one timed entry: `offset_shell` and `offset_solid` both run
402/// through it, so the `timing.*` split and the acceptance step are the same.
403/// Lane O row 38's switch, default on since batch 17 (`0`/`off` restores the fit): it asks the offset pipeline's
404/// imprints for the planar-iso lane's exact pcurves (`ImprintOptions::
405/// exact_iso_pcurves`, parity slice 1): a section the lane answers is an
406/// iso-curve of the ruled carrier and an affine image on the plane, so its
407/// pcurves are written down instead of fitted. Booleans keep their own
408/// default; `BREP_EXACT_ISO_PCURVES` still overrides every imprint.
409fn offset_exact_iso_pcurves_on() -> bool {
410    switch!("BREP_OFFSET_EXACT_ISO_PCURVES", true)
411}
412
413
414pub(super) fn offset_body(
415    source: &BrepSolid,
416    opening_face_ids: &[u64],
417    distance: f64,
418    body: Body,
419    entry: &str,
420) -> Result<OffsetShellResultRecord, KernelRefusal> {
421    profile::reset();
422    // Lane P3b row 3: one stderr write per operation for every profile line
423    // emitted below (`crate::imprint::ProfileSink`).
424    let _profile_sink = crate::imprint::ProfileSink::open();
425    let started = web_time::Instant::now();
426    let result = offset_shell_any_lane(source, opening_face_ids, distance, body).and_then(|result| {
427        let mut clock = web_time::Instant::now();
428        let connected = outward_shell_is_connected(source, distance, &result);
429        profile::lap(&mut clock, profile::ACCEPTANCE);
430        connected.map(|()| result)
431    });
432    profile::add(profile::TOTAL, started.elapsed());
433    profile::report(entry);
434    result
435}
436
437/// Every shell of an OUTWARD result must bound volume. The shell is the grown
438/// body less the source with the openings held, so it can be disconnected
439/// (opening a cylinder's lateral face leaves its two grown caps as two
440/// slabs), but each of its shells encloses material. A shell that encloses
441/// none, a lone face that happened to close, is a fragment of an assembly that
442/// went wrong and still validated. Measured on 2026-09-26: a free-form D whose
443/// fitted opening wall sat within the fit tolerance of its carriers' sections
444/// returned its body plus 62 single-face shells of volume under 3e-24, each
445/// valid, with the right total volume. So this refuses by name for EVERY
446/// outward shell, not only for the lane that produced those.
447pub(super) fn outward_shell_is_connected(
448    source: &BrepSolid,
449    distance: f64,
450    result: &OffsetShellResultRecord,
451) -> Result<(), KernelRefusal> {
452    if distance >= 0.0 || result.solid.shells.len() <= 1 {
453        return Ok(());
454    }
455    let scale = crate::solid_scale(source).max(1.0);
456    let volumes = result
457        .solid
458        .shells
459        .iter()
460        .map(|shell| {
461            let mut one = result.solid.clone();
462            one.shells = vec![shell.clone()];
463            crate::solid_mass_properties(&one)
464                .map(|properties| properties.volume)
465                .or_refuse(KernelStage::Validate, "solid_mass_properties")
466        })
467        .collect::<Result<Vec<f64>, KernelRefusal>>()?;
468    let total = volumes.iter().map(|volume| volume.abs()).sum::<f64>();
469    let floor = (1e-9 * total).max(1e-12 * scale * scale * scale);
470    let empty = volumes.iter().filter(|volume| volume.abs() <= floor).count();
471    if empty == 0 {
472        return Ok(());
473    }
474    Err(refusals::empty_shell(result.solid.shells.len(), empty, floor).into())
475}
476
477fn offset_shell_any_lane(
478    source: &BrepSolid,
479    opening_face_ids: &[u64],
480    distance: f64,
481    body: Body,
482) -> Result<OffsetShellResultRecord, KernelRefusal> {
483    // Pinched source faces are un-modellable by the arrangement until the
484    // touch is a vertex; do it once here so BOTH `offset_shell_impl` attempts
485    // and every carrier built from the source see the same topology.
486    let prepared = split_self_touching_source(source)?;
487    let source = prepared.as_ref().unwrap_or(source);
488    // The revolved-wedge lane answers a SHELL question (the material between
489    // a revolve and its grown image); the offset body takes the pipeline.
490    if body == Body::Whole {
491        // Row 36 (`BREP_OFFSET_SPHERE_BODY`, default off): the offset body of a
492        // ball is the ball scaled about its centre (`sphere_body.rs`); it
493        // declines to the pipeline on anything else.
494        if opening_face_ids.is_empty() {
495            if let Some(result) = sphere_body::sphere_body(source, distance) {
496                return Ok(result);
497            }
498            // `BREP_OFFSET_TORUS_APPLE` (default on; `=0` restores): a whole torus grown
499            // past its hole is the revolved apple (`torus_body.rs`).
500            if let Some(result) = super::torus_body::torus_apple(source, distance) {
501                return Ok(result);
502            }
503        }
504        return imprint_attempts(source, opening_face_ids, distance, body);
505    }
506    // An outward shell of the one circular face of a partial revolve is planar
507    // in the meridian half-plane, fold band and all: build it there and revolve
508    // it once (`revolved_wedge.rs`). Anything else declines and takes the
509    // imprint pipeline, unchanged.
510    match revolved_wedge::recognize(source, opening_face_ids, distance) {
511        Ok(revolved_wedge::Recognition::Wedge(wedge)) => {
512            os_debug!("revolved wedge lane: {}", wedge.report());
513            return revolved_wedge::build(&wedge, source);
514        }
515        Ok(revolved_wedge::Recognition::Declined(reason)) => {
516            os_debug!("revolved wedge lane declined: {reason}");
517        }
518        // A source the lane cannot even read is not one it takes; the pipeline
519        // owns every refusal.
520        Err(error) => {
521            os_debug!("revolved wedge lane declined: could not read the source ({error})");
522        }
523    }
524    // `BREP_OFFSET_SPHERE_CUT_SHELL` (default on; `=0` restores): an inward shell of a ball cut by
525    // planes is the source less the moved ball cut by the moved planes
526    // (`sphere_cut_shell.rs`); it declines to the pipeline on anything else.
527    if let Some(result) = sphere_cut_shell::sphere_cut_shell(source, opening_face_ids, distance) {
528        return Ok(result);
529    }
530    // Row 32 (`BREP_OFFSET_PLANAR_SHELL`, default on; `=0` restores): an outward shell of a
531    // convex polyhedron in closed form (`planar_shell.rs`); it declines to the
532    // pipeline on anything else.
533    if let Some(result) = planar_shell::planar_shell(source, opening_face_ids, distance) {
534        return Ok(result);
535    }
536    imprint_attempts(source, opening_face_ids, distance, body)
537}
538
539
540fn imprint_attempts(
541    source: &BrepSolid,
542    opening_face_ids: &[u64],
543    distance: f64,
544    body: Body,
545) -> Result<OffsetShellResultRecord, KernelRefusal> {
546    // First try with oblique hole-wall carriers extended past their opening
547    // planes (the lane that closes tilted/conical through-holes). If that
548    // extension fired but the arrangement could not assemble the harder
549    // configuration it produced (e.g. the extended bore genuinely intersects
550    // other offset walls), fall back to the un-extended pipeline so every
551    // previously-supported shape keeps its exact path and every refusal keeps
552    // the canonical honesty-gate message.
553    let mut extension_fired = false;
554    let mut reflex_rebuilds = 0usize;
555    let mut shadowed_points = Vec::new();
556    let mut omitted = HashSet::default();
557    let mut vacated_notes = Vec::new();
558    let result = match offset_shell_impl(
559        source,
560        opening_face_ids,
561        distance,
562        body,
563        true,
564        &mut extension_fired,
565        &mut reflex_rebuilds,
566        &HashSet::default(),
567        &HashSet::default(),
568        false,
569        &mut shadowed_points,
570        &mut omitted,
571        &mut vacated_notes,
572    ) {
573        Ok(result) => {
574            // Attribution marker: which pipeline produced this result. A
575            // retry-produced Ok can silently mask a broken extension lane —
576            // always say which attempt won. The reflex-rebuild count rides
577            // along because that lane fires on BOTH attempts: "extension
578            // idle" alone would misread as "the plain pipeline did this".
579            os_debug!(
580                "offset_shell result: FIRST attempt (extension {}, reflex-rebuilt {})",
581                if extension_fired { "fired" } else { "idle" },
582                reflex_rebuilds,
583            );
584            Ok(result)
585        }
586        Err(error) if extension_fired => {
587            os_debug!("extended pipeline failed ({error}); retrying without carrier extension");
588            let mut unused = false;
589            let mut retry_reflex = 0usize;
590            shadowed_points.clear();
591            omitted.clear();
592            let retried = offset_shell_impl(
593                source,
594                opening_face_ids,
595                distance,
596                body,
597                false,
598                &mut unused,
599                &mut retry_reflex,
600                &HashSet::default(),
601                &HashSet::default(),
602                false,
603                &mut shadowed_points,
604                &mut omitted,
605                &mut vacated_notes,
606            );
607            os_debug!(
608                "offset_shell result: RETRY without extension ({}, reflex-rebuilt {retry_reflex})",
609                if retried.is_ok() { "ok" } else { "err" },
610            );
611            retried
612        }
613        Err(error) => Err(error),
614    };
615    // A refusal from a shell whose selection saw two offsets CROSS inside
616    // material thinner than twice the distance says so first: a rib thinner
617    // than 2d shelled inward is that configuration, and the assembly's own
618    // message names only the symptom. A refusal that already names its cause
619    // keeps it. The crossing is looked for here, on the failure path only.
620    match result {
621        // A refusal that already names its CAUSE keeps its own sentence; one
622        // that names a symptom ("connector edge left its plane", an assembly
623        // message) is worth a cause in front of it. Reading the whole prefix as
624        // "anything this module wrote" was too wide: the twin bosses came out
625        // led by a notch sentence when their cause was the shadow cascade, and
626        // the partial cone lost its junction cause behind a connector's.
627        // Each cause is put IN FRONT of the symptom with `with_message`, so the
628        // symptom's class (an assembly's `NonIntegralGenus`, …) survives.
629        Err(error) if !refusal_names_its_cause(&error.message) => {
630            let crossing = name_offset_crossing(source, opening_face_ids, distance, &shadowed_points);
631            let junction = crossing
632                .is_none()
633                .then(|| name_retained_opening_junction(source, opening_face_ids, distance))
634                .flatten();
635            Err(cause_in_front(error, crossing, junction, vacated_notes.first()))
636        }
637        result => result,
638    }
639}
640
641/// The symptom `error` with the first named cause IN FRONT of it: an offset
642/// crossing, else a retained/opening junction, else a vacated rim's note.
643/// Each prefix is a `with_message`, so the symptom's CLASS (an assembly's
644/// `NonIntegralGenus`, …) survives the sentence put before it.
645pub(super) fn cause_in_front(
646    error: KernelRefusal,
647    crossing: Option<String>,
648    junction: Option<String>,
649    note: Option<&String>,
650) -> KernelRefusal {
651    if let Some(crossing) = crossing {
652        return error.with_message(|error| {
653            format!(
654                "offset_shell: {crossing}; a skin partly shadowed that way is not yet \
655                 assembled ({error})"
656            )
657        });
658    }
659    if let Some(junction) = junction {
660        return error.with_message(|error| format!("offset_shell: {junction} ({error})"));
661    }
662    // A rim the closure could not bridge, with what it measured, in front of
663    // whatever the assembly then said about the hole it left.
664    match note {
665        Some(note) => error.with_message(|error| {
666            format!(
667                "offset_shell: a rim vacated by an omitted support was left open — {note} \
668                 ({error})"
669            )
670        }),
671        None => error,
672    }
673}
674
675/// The refusals this module's own cause-naming lanes produce. Anything else is
676/// a symptom another stage reported, and takes a cause in front of it.
677fn refusal_names_its_cause(error: &str) -> bool {
678    [
679        " coincide near ",
680        " touch tangentially near ",
681        "is sealed off from the opening",
682        "cannot tell whether",
683        "omitting the shadowed offsets of source faces",
684        "no retained face keeps any offset skin",
685        "is shadowed at every one of its",
686        "a rim vacated by an omitted support was left open",
687        "junction, so its offset ends inside the cavity",
688        "cross near",
689        // The opening-wall lane's own named refusals, so a cause of mine never
690        // arrives in front of one of theirs.
691        "folds where the carve",
692        "is curved and not a ruled surface",
693        "cannot be built",
694    ]
695    .iter()
696    .any(|name| error.contains(name))
697}
698
699fn offset_shell_impl(
700    source: &BrepSolid,
701    opening_face_ids: &[u64],
702    distance: f64,
703    body: Body,
704    extend_oblique_carriers: bool,
705    extension_fired: &mut bool,
706    reflex_rebuilds: &mut usize,
707    shadowed_supports: &HashSet<u64>,
708    cavity_bounds: &HashSet<u64>,
709    every_seeded_piece: bool,
710    shadowed_points: &mut Vec<(Vec3, u64)>,
711    omitted_supports: &mut HashSet<u64>,
712    vacated_notes: &mut Vec<String>,
713) -> Result<OffsetShellResultRecord, KernelRefusal> {
714    profile::attempt();
715    let _sharp_rims = super::carriers::SharpOpeningRims::set(if super::carriers::ruled_miter_on() {
716        super::carriers::sharp_opening_rims(source, opening_face_ids)
717    } else {
718        HashSet::default()
719    });
720    let mut clock = web_time::Instant::now();
721    omitted_supports.clone_from(shadowed_supports);
722    vacated_notes.clear();
723    if !distance.is_finite() || distance.abs() <= MINIMUM_DISTANCE {
724        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_distance", "offset_shell: distance must be finite and non-zero"));
725    }
726    if opening_face_ids.is_empty() && body == Body::Shell {
727        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_no_opening", "offset_shell: at least one opening face is required"));
728    }
729    let source_faces = source
730        .shells
731        .iter()
732        .flat_map(|shell| &shell.faces)
733        .collect::<Vec<_>>();
734    let opening_set = opening_face_ids.iter().copied().collect::<HashSet<_>>();
735    if opening_set
736        .iter()
737        .any(|id| !source_faces.iter().any(|face| face.id == *id))
738    {
739        return Err(KernelRefusal::input(KernelStage::Collect, "offset_shell_foreign_opening", "offset_shell: opening face does not belong to source"));
740    }
741    let retained = source_faces
742        .iter()
743        .filter(|face| !opening_set.contains(&face.id))
744        .map(|face| face.id)
745        .collect::<Vec<_>>();
746    if retained.is_empty() {
747        return Err(refusals::no_retained_face().into());
748    }
749    let mut carriers = Vec::new();
750    let mut open_section_reach = false;
751    if distance < 0.0 && std::env::var("BREP_OFFSET_OPEN_SECTION_REACH").as_deref() != Ok("0") {
752        for face in &source_faces {
753            if retained.contains(&face.id) && open_profile_has_construction_headroom(face)? {
754                open_section_reach = true;
755                break;
756            }
757        }
758    }
759    // Faces whose folded part was carved away, by source face id. Later stages
760    // that read a retained face's TRIM — the apex-cap scan below is the one —
761    // must read the carved trim, not the one the source still carries.
762    let mut carved_supports = HashMap::<u64, FaceRecord>::default();
763    // Lane O item 705: (neighbour face, opening face, rim edge) of every
764    // tangent junction with a curved opening the neighbour cannot reach.
765    let mut tangent_rim_rounds: Vec<(u64, u64, u64)> = Vec::new();
766    let mut shielded_cusps = HashSet::<u64>::default();
767
768    for face_id in &retained {
769        let face = source_faces.iter().find(|face| face.id == *face_id).unwrap();
770        profile::lap(&mut clock, profile::CARRIER_TRIM);
771        let collapsed = profile::detail("offset_support_collapsed", || offset_support_collapsed(face, distance))?;
772        profile::lap(&mut clock, profile::CARRIER_REGULARITY);
773        if collapsed {
774            os_debug!("omitting collapsed offset support for source face {face_id}");
775            continue;
776        }
777        // A support a previous pass found SHADOWED contributes no skin, and its
778        // carrier must not imprint its neighbours either — see the selection.
779        if shadowed_supports.contains(face_id) {
780            os_debug!("omitting shadowed offset support for source face {face_id}");
781            continue;
782        }
783        // A planar carrier whose source face meets a neighbour at a REFLEX
784        // (concave) edge extends its trim by |distance|: at such a junction
785        // the offset skin must GROW past the source footprint to meet the
786        // neighbour's offset (a pocket wall's source [5,15] → skin
787        // [3.5,16.5]); a source-sized trim leaves a gap no miter can bridge.
788        // The surplus at the face's convex ends is clipped by the imprints +
789        // on-skin filter. Convex-only faces keep their exact source-sized
790        // trim — extending them perturbs delicately-selected fragment
791        // landscapes for no benefit.
792        let miter = source_faces
793            .iter()
794            .find(|face| face.id == *face_id)
795            .map(|face| face_reflex_miter_tan(source, face))
796            .transpose()?
797            .flatten();
798        // The offsets of two faces joined at a reflex edge meet d·tan(θn/2)
799        // past the source rim — d only for a perpendicular join. Scale the
800        // extension to the worst sampled miter (×1.5 headroom so the true
801        // intersection lands strictly INSIDE the extended trim, never on its
802        // boundary where the arrangement drops boundary-coincident cuts) and
803        // cap it: a near-tangential join would ask for an unbounded trim.
804        let reflex_extension = miter.map(|worst_tan| {
805            (distance.abs() * worst_tan * 1.5)
806                .max(distance.abs())
807                .min(distance.abs() * 12.0)
808        });
809        if let Some(extension) = reflex_extension {
810            os_debug!("carrier for src face {face_id}: reflex-extended by {extension:.4}");
811        }
812        // OUTWARD: every retained carrier grows by |distance| across its
813        // sharp and opening-facing sides (the offsets of a convex junction
814        // meet past both source rims; a wall plane cuts the surplus), and
815        // by nothing across a smooth junction into a retained neighbour —
816        // see `outward_carrier_extension`. A reflex junction needs no
817        // outward growth at all (those offsets cross inside the trims).
818        let mut curved_reach = None;
819        let planar_reach = planar_opening_reach(face, &source_faces, &opening_set, distance)?;
820        let extension = if distance < 0.0 {
821            outward_carrier_extension(source, face, &source_faces, &opening_set, distance.abs())?
822        } else {
823            let mut inward = crate::CarrierExtension::uniform(reflex_extension.unwrap_or(0.0));
824            // `BREP_OFFSET_THIN_SKIN_PAD` (default on; `=0` restores): a skin thinner than
825            // the fragmenter's graze band is padded past its source rims
826            // (`thin_skin_pad`), so its sections end far from its corners.
827            if let Some(pad) = thin_skin_pad(source, face, distance)? {
828                inward = crate::CarrierExtension::uniform(inward.u_min.max(pad));
829                os_debug!("carrier for src face {face_id}: thin skin padded by {pad:.6}");
830            }
831            if let Some(reach) = planar_reach {
832                let grown = reach + REACH_CLEARANCE * distance;
833                inward = crate::CarrierExtension::uniform(inward.u_min.max(grown));
834                os_debug!("carrier for src face {face_id}: planar reach to its opening {reach:.6}");
835            }
836            if reflex_extension.is_some() {
837                let [u_min, u_max, v_min, v_max] =
838                    apex_corner_reach(source, face, &source_faces, &opening_set, distance)?;
839                if u_min.max(u_max).max(v_min).max(v_max) > 0.0 {
840                    inward.u_min = inward.u_min.max(u_min);
841                    inward.u_max = inward.u_max.max(u_max);
842                    inward.v_min = inward.v_min.max(v_min);
843                    inward.v_max = inward.v_max.max(v_max);
844                    os_debug!(
845                        "carrier for src face {face_id}: apex corner reach u=({:.4},{:.4}) v=({:.4},{:.4})",
846                        inward.u_min, inward.u_max, inward.v_min, inward.v_max,
847                    );
848                }
849            }
850            // An INWARD carrier that falls short of the planar opening it meets
851            // is grown until it crosses that plane, so the imprint cuts the
852            // skin there instead of leaving a rim for the ruled weld to cork
853            // below the opening (`fallshort_opening_reach`).
854            if let Some((at_v_max, reach)) =
855                fallshort_opening_reach(source, face, &source_faces, &opening_set, distance)?
856            {
857                // Past the plane by a quarter of the shell's own thickness, so
858                // the section is strictly inside the carrier's trim rather than
859                // on its boundary. Measured 2026-09-17 over the offset-shell
860                // suite: every fraction from 0.01 to 2.0 of |d| gives the same
861                // 73 passing members, while a margin sized in COINCIDENCE BANDS
862                // (1 to 32 of them, 2e-5 each here) breaks the sphere-under-
863                // frustum member by 9.4e-2 — the fragile margins are the ones
864                // far below the construction's own length.
865                let grown = reach + REACH_CLEARANCE * distance.abs();
866                if at_v_max {
867                    inward.v_max = inward.v_max.max(grown);
868                } else {
869                    inward.v_min = inward.v_min.max(grown);
870                }
871                os_debug!(
872                    "carrier for src face {face_id}: inward reach to its opening plane {reach:.6}                      at v_{} ",
873                    if at_v_max { "max" } else { "min" },
874                );
875            }
876            // A CURVED opening has no such construction, and the pipeline
877            // closes the cavity with whatever completion reaches for: measured
878            // 7.6% short on the hemisphere cup shelled through its dome.
879            if let Some((opening, gap)) =
880                offset_falls_short_of_curved_opening(source, face, &source_faces, &opening_set, distance)?
881            {
882                if std::env::var("BREP_OFFSET_CURVED_REACH").as_deref() != Ok("0") {
883                    curved_reach = curved_opening_reach_source(source, face, distance)?;
884                }
885                // `BREP_OFFSET_CURVED_RULED_REACH` (default on; `=0` restores): where the
886                // rectangle continuation declines (a u-closed band, a tangent
887                // junction), a v-ruled carrier is grown along its rulings
888                // until it crosses the curved opening, as the planar reach
889                // does for a plane (`curved_fallshort_reach`).
890                let mut ruled_reach = false;
891                if curved_reach.is_none() {
892                    if let Some((at_v_max, reach)) =
893                        curved_fallshort_reach(source, face, &source_faces, &opening_set, distance)?
894                    {
895                        let grown = reach + REACH_CLEARANCE * distance.abs();
896                        if at_v_max {
897                            inward.v_max = inward.v_max.max(grown);
898                        } else {
899                            inward.v_min = inward.v_min.max(grown);
900                        }
901                        ruled_reach = true;
902                        os_debug!(
903                            "carrier for src face {face_id}: ruled reach to its curved opening {opening} {reach:.6} at v_{}",
904                            if at_v_max { "max" } else { "min" },
905                        );
906                    }
907                }
908                // Lane O item 705 (`BREP_OFFSET_TANGENT_RIM_ROUND`, default off):
909                // a face that meets its curved opening TANGENTIALLY never
910                // reaches it — its skin runs parallel to the opening at
911                // exactly d — and the wall there is a round of radius d about
912                // the rim (`rim_round.rs`). Record the rim; the round carrier
913                // is emitted beside the apex caps, and this face's carrier is
914                // built as it is.
915                if curved_reach.is_none() && !ruled_reach && super::rim_round::tangent_rim_round_enabled() {
916                    for coedge in face.loops.iter().flat_map(|loop_record| &loop_record.coedges) {
917                        let Some((mate, mate_use)) = junction_mate(&source_faces, face, coedge.edge_id) else {
918                            continue;
919                        };
920                        if mate.id != opening || !uses_are_tangent(face, coedge, mate, mate_use)? {
921                            continue;
922                        }
923                        os_debug!("tangent rim round: source face {face_id} meets opening {opening} tangentially along edge {}", coedge.edge_id);
924                        tangent_rim_rounds.push((*face_id, opening, coedge.edge_id));
925                    }
926                }
927                if curved_reach.is_none() && !ruled_reach && !tangent_rim_rounds.iter().any(|(id, _, _)| id == face_id) {
928                    return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_curved_opening_short", format!(
929                        "offset_shell: the offset skin of source face {face_id} stops {gap:.3e} short of \
930                         curved opening face {opening}; only a planar opening's skin is grown to meet it"
931                    )));
932                }
933            }
934            inward
935        };
936        if distance < 0.0 {
937            os_debug!(
938                "carrier for src face {face_id}: outward extension u=({:.3},{:.3}) v=({:.3},{:.3})",
939                extension.u_min,
940                extension.u_max,
941                extension.v_min,
942                extension.v_max,
943            );
944        }
945        // CARVE: a support whose offset folds over PART of its trim keeps the
946        // regular part and drops the rest, instead of being built whole with
947        // the fold in it. The carrier is built over the CARVED source, so the
948        // trim it clones stops at the fold locus — and, because that trim no
949        // longer reaches past the offset's pinch, `offset_surface`'s apex-cone
950        // pinch retrim does not fire and the cloned pcurves still name the
951        // pointwise offset.
952        //
953        // A carve that cannot be made is not a licence to build the folded
954        // support anyway: the refusal is the answer, because a carrier with an
955        // inverted patch in it welds into a shell that validates and bounds the
956        // wrong solid.
957        profile::lap(&mut clock, profile::CARRIER_TRIM);
958        let carved = profile::detail("carve_folded_support", || carve_shell_support(source, face, distance, opening_face_ids))?;
959        profile::lap(&mut clock, profile::CARRIER_REGULARITY);
960        // A CUT RIM — the face's rim with a planar opening running across
961        // its surface rather than along a v end — has the trim rebuilt over
962        // the surface's full rectangle so the opening plane truncates the
963        // offset skin (`cut_rim_full_rectangle_source`); like the carve, a
964        // substitute source the carrier is built over.
965        let cut_rim = if carved.is_none() && curved_reach.is_none() {
966            cut_rim_full_rectangle_source(source, face, &source_faces, &opening_set, distance)?
967        } else {
968            None
969        };
970        // A curved face's SHARP outward u-side: the carrier is built over the
971        // surface continued past it, so it reaches the mitre with its sharp
972        // neighbour's offset (`sharp_side_continued_source`). Its INWARD
973        // reflex u-side widens the trim inside the surface's own domain
974        // instead (`reflex_side_widened_source`).
975        let sharp_side = if carved.is_none() && cut_rim.is_none() && curved_reach.is_none() {
976            match sharp_side_continued_source(source, face, &source_faces, &opening_set, distance, &extension)? {
977                Some(continued) => Some(continued),
978                None => match band_reach_continued_source(source, face, distance, &extension)? {
979                    Some(widened) => Some(widened),
980                    None => reflex_side_widened_source(source, face, &source_faces, &opening_set, distance, &extension)?,
981                },
982            }
983        } else {
984            None
985        };
986        let carrier_source = match (&carved, &cut_rim) {
987            (None, None) if curved_reach.is_some() => curved_reach.as_ref().unwrap(),
988            (None, None) if sharp_side.is_some() => sharp_side.as_ref().unwrap(),
989            (None, Some(rebuilt)) => rebuilt,
990            (Some(carved), _) => {
991                os_debug!(
992                    "carving the fold off source face {face_id}: {:.1}% of its trim kept, \
993                     {} new boundary pcurve(s)",
994                    carved.kept_fraction * 100.0,
995                    carved.fold_boundary
996                );
997                carved_supports.insert(*face_id, carved.face.clone());
998                if carved.shielded_cusp {
999                    shielded_cusps.insert(*face_id);
1000                }
1001                &carved.solid
1002            }
1003            (None, None) => source,
1004        };
1005        profile::lap(&mut clock, profile::CARRIER_TRIM);
1006        let (mut carrier, lane) = carrier_solid_sided_lane(carrier_source, *face_id, distance, &extension)?;
1007        os_debug!(
1008            "carrier for src face {face_id}: lane {lane:?} carved {} surface u {:?} v {:?} source face u {:?} v {:?}",
1009            carved.is_some(),
1010            carrier.shells[0].faces[0].surface.domain_u().ok(),
1011            carrier.shells[0].faces[0].surface.domain_v().ok(),
1012            carrier_source.shells.iter().flat_map(|shell| &shell.faces).find(|f| f.id == *face_id).and_then(|f| f.surface.domain_u().ok()),
1013            carrier_source.shells.iter().flat_map(|shell| &shell.faces).find(|f| f.id == *face_id).and_then(|f| f.surface.domain_v().ok()),
1014        );
1015        profile::carrier_built(lane, clock.elapsed());
1016        clock = web_time::Instant::now();
1017        if (distance < 0.0 || reflex_extension.is_some()) && face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
1018            // PADDED planes: the pad slides the plane's net while the trim's
1019            // loops are cloned in uv, so an INTERIOR loop (a bore or boss rim
1020            // in the face) is carried outward with the grown outline instead
1021            // of following the neighbour's offset.
1022            //   OUTWARD: a bore's rim grew (r=4.68 → 5.15) while the shrunk
1023            //   offset bore (r=3.68) that must hole this plane fell inside the
1024            //   enlarged void, so neither the imprint nor the orphan-rim weld
1025            //   could cut it.
1026            //   INWARD, reflex-extended: the same slide moves a REFLEX hole
1027            //   rim the wrong way — a sphere sitting under a frustum's base
1028            //   annulus (the user's 2026-09-07 document) had its r=8.3 rim
1029            //   scaled to 9.22 while the shrunk offset sphere (r=7.3) crosses
1030            //   the offset plane at r=7.23, inside the enlarged hole: the
1031            //   pair imprint found nothing and the rim stayed one-use.
1032            // Drop the interior loops (as the wall carriers do): the
1033            // neighbour's carrier imprints — or its rim, welded in as a
1034            // hole — cut the true opening, and the seeded selection drops
1035            // the disk over it. A CONVEX interior rim on the same face (a
1036            // through-bore in a pocket floor) is safe to drop too: its
1037            // offset bore imprints at r+d and the ring inside is rejected by
1038            // class (Out) or by the on-skin filter, no seed involved.
1039            drop_wall_interior_loops(&mut carrier)?;
1040            // A free-form outline grown uniformly outward is trimmed by its
1041            // outline OFFSET, not the slid net's scaled trim
1042            // (`outline_offset_carrier`).
1043            let pads = [extension.u_min, extension.u_max, extension.v_min, extension.v_max];
1044            let widest = pads.iter().copied().fold(f64::NEG_INFINITY, f64::max);
1045            let narrowest = pads.iter().copied().fold(f64::INFINITY, f64::min);
1046            // `BREP_OFFSET_CONVEX_POLYGON_OUTLINE`: pads equal but for their last
1047            // bits (a tetrahedron's turned sides read 2.121 four ways, not
1048            // bit-equal) are the uniform pad too.
1049            let uniform = (extension.u_min == extension.u_max
1050                && extension.u_min == extension.v_min
1051                && extension.u_min == extension.v_max)
1052                || (convex_polygon_outline_enabled() && widest - narrowest <= 1e-9 * widest.abs());
1053            if distance < 0.0 && uniform {
1054                // Past the walls' sections by the same clearance a planar
1055                // neighbour of a ruled wall gets, so each section lies
1056                // strictly INSIDE the fitted trim rather than on it.
1057                let clearance =
1058                    RULED_NEIGHBOUR_CLEARANCE_BANDS * offset_skin_band(crate::solid_scale(source), distance);
1059                if let Some(rebuilt) = outline_offset_carrier(source, face, widest, clearance, &carrier)? {
1060                    os_debug!(
1061                        "carrier for src face {face_id}: outline trimmed by its offset by {:.4}",
1062                        widest + clearance
1063                    );
1064                    carrier = rebuilt;
1065                }
1066            }
1067        }
1068        // An inward reflex junction needs a construction plane extending PAST
1069        // the source rim. Sliding the net under a free-form trim scales that
1070        // trim instead: a notched revolve cap's notch grows, and clips away the
1071        // sections with the box's offsets. Even its curved convex boundary can
1072        // fall inside the neighbouring offset section after that scaling.
1073        // Use the extended plane's full rectangle and let the neighbouring
1074        // carriers cut the skin. The source face and the offset surface stay
1075        // unchanged. Circular outlines need the same construction: scaling a
1076        // reflex arc can also clip away its offset cylinder's section. Unlike
1077        // an outline offset, this needs no approximation of
1078        // the planar rim's parallel curve (which may collapse at a tight turn).
1079        if distance > 0.0
1080            && reflex_extension.is_some()
1081            && face.surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")?
1082            && outline_has_curved_element(source, face)?
1083        {
1084            carrier = full_rectangle_face(&carrier, &carrier.shells[0].faces[0])?;
1085        }
1086        if planar_reach.is_some() {
1087            // A chamfer's trimmed polygon can cut diagonally across its
1088            // patch. Scaling that polygon with the net can still clip the
1089            // opening section. Let the opening and neighbouring offsets
1090            // trim the grown construction rectangle instead.
1091            carrier = full_rectangle_face(&carrier, &carrier.shells[0].faces[0])?;
1092        }
1093        if open_section_reach {
1094            if let Some(continued) = open_section_construction(source, face, &carrier, &extension, distance)? {
1095                carrier = continued;
1096                os_debug!("carrier for src face {face_id}: open section uses its natural construction rectangle");
1097            }
1098        }
1099        // The rim a shadowed support's removal VACATED in this carrier's trim:
1100        // a slot narrower than 2|d| that runs out through this face leaves a
1101        // notch its own skin has to close, because the dilation fills the slot.
1102        // The source face keeps its notch; only the carrier is bridged.
1103        if distance < 0.0 && !shadowed_supports.is_empty() {
1104            let report = close_vacated_rims(
1105                &mut carrier,
1106                source_faces
1107                    .iter()
1108                    .find(|face| face.id == *face_id)
1109                    .copied()
1110                    .unwrap_or(face),
1111                &source_faces,
1112                shadowed_supports,
1113            )?;
1114            if report.closed > 0 {
1115                os_debug!(
1116                    "carrier for src face {face_id}: {} vacated rim run(s) bridged",
1117                    report.closed
1118                );
1119            }
1120            for declined in &report.declined {
1121                os_debug!("carrier for src face {face_id}: vacated rim left alone — {declined}");
1122                vacated_notes.push(format!("source face {face_id}: {declined}"));
1123            }
1124        }
1125        carriers.push(Carrier {
1126            solid: carrier,
1127            source_face_id: *face_id,
1128            kind: OffsetFaceRole::Offset,
1129        });
1130    }
1131    profile::lap(&mut clock, profile::CARRIER_TRIM);
1132    // APEX-CAP carriers: a retained face whose DEGENERATE edge (an apex
1133    // point, e.g. a conic crater's tip) offsets into a RING is an EXTERIOR
1134    // cone offset — the parallel surface truncates at the ring and the exact
1135    // rolling-ball offset closes it with a SPHERE of radius |distance| about
1136    // the apex. Emit that sphere as an extra carrier: the ring imprint
1137    // (sphere × offset frustum) splits it, the on-skin filter keeps exactly
1138    // the cap sector (its points are |d| from the apex and ≥|d| from the
1139    // rest of the face), and the ring becomes two-use.
1140    let mut apex_cap_sources = HashSet::default();
1141    let mut apex_cap_pairs: Vec<(usize, usize)> = Vec::new();
1142    {
1143        let retained_count = carriers.len();
1144        for index in 0..retained_count {
1145            // A CARVED support's trim is the carved one. Its coedges name edges
1146            // that live only in the carved clone, so the `source.edges` lookup
1147            // below finds nothing and the apex-cap lane skips the face — which
1148            // is right: the carve already ended that carrier at the offset's own
1149            // pinch, and there is no apex ring left to cap.
1150            let carved_face = carved_supports.get(&carriers[index].source_face_id);
1151            let source_face = carved_face
1152                .or_else(|| source_by_id_lookup(&source_faces, carriers[index].source_face_id));
1153            let Some(source_face) = source_face else {
1154                continue;
1155            };
1156            let carrier_surface = carriers[index].solid.shells[0].faces[0].surface.clone();
1157            for coedge in source_face
1158                .loops
1159                .iter()
1160                .flat_map(|loop_record| &loop_record.coedges)
1161            {
1162                let Some(edge) = source
1163                    .edges
1164                    .iter()
1165                    .find(|edge| edge.id == coedge.edge_id)
1166                else {
1167                    continue;
1168                };
1169                if !edge.degenerate {
1170                    continue;
1171                }
1172                // Image of the degenerate edge on the offset surface.
1173                let [p0, p1] = coedge.pcurve.domain().or_refuse(KernelStage::Sew, "domain")?;
1174                let mut ring = Vec::new();
1175                for sample in 0..=8 {
1176                    let uv = coedge
1177                        .pcurve
1178                        .evaluate(p0 + (p1 - p0) * sample as f64 / 8.0).or_refuse(KernelStage::Sew, "evaluate")?;
1179                    ring.push(carrier_surface.evaluate(uv.x, uv.y).or_refuse(KernelStage::Sew, "evaluate")?);
1180                }
1181                let spread = ring
1182                    .iter()
1183                    .map(|point| point.sub(ring[0]).length())
1184                    .fold(0.0f64, f64::max);
1185                if spread <= distance.abs() * 1e-2 {
1186                    continue;
1187                }
1188                let apex = source
1189                    .vertices
1190                    .iter()
1191                    .find(|vertex| vertex.id == edge.start_vertex_id)
1192                    .map(|vertex| vertex.point)
1193                    .ok_or_else(|| "offset_shell: apex vertex missing".to_string()).or_refuse(KernelStage::Sew, "offset_shell_apex_vertex_missing")?;
1194                let centroid = ring
1195                    .iter()
1196                    .fold(Vec3::default(), |sum, point| sum.add(*point))
1197                    .scale(1.0 / ring.len() as f64);
1198                let axis = apex.sub(centroid);
1199                let axis = if axis.length() > 1e-9 {
1200                    axis.normalized().or_refuse(KernelStage::Sew, "normalized")?
1201                } else {
1202                    Vec3::new(0.0, 0.0, 1.0)
1203                };
1204                os_debug!(
1205                    "apex-cap carrier for src face {} at ({:.3},{:.3},{:.3}) r={:.3}",
1206                    carriers[index].source_face_id,
1207                    apex.x,
1208                    apex.y,
1209                    apex.z,
1210                    distance.abs(),
1211                );
1212                apex_cap_sources.insert(index);
1213                apex_cap_sources.insert(carriers.len());
1214                apex_cap_pairs.push((index, carriers.len()));
1215                carriers.push(Carrier {
1216                    solid: crate::make_sphere_brep(apex, distance.abs(), axis).or_refuse(KernelStage::Sew, "make_sphere_brep")?,
1217                    source_face_id: carriers[index].source_face_id,
1218                    kind: OffsetFaceRole::Offset,
1219                });
1220            }
1221        }
1222    }
1223    // Lane O item 705: the ROUND carriers about tangent opening rims, emitted
1224    // as the apex caps are (an extra Offset carrier of the neighbour face) and
1225    // handled by the same exemptions and coincident-edge splits for now.
1226    if !tangent_rim_rounds.is_empty() {
1227        let tolerance = 1e-7f64.max(crate::solid_scale(source) * 1e-9);
1228        for (neighbour_id, opening_id, edge_id) in &tangent_rim_rounds {
1229            let Some(neighbour_index) = carriers.iter().position(|carrier| carrier.source_face_id == *neighbour_id && matches!(carrier.kind, OffsetFaceRole::Offset)) else {
1230                continue;
1231            };
1232            let Some(edge) = source.edges.iter().find(|edge| edge.id == *edge_id) else { continue };
1233            let Some(opening_face) = source_by_id_lookup(&source_faces, *opening_id) else { continue };
1234            match super::rim_round::circular_rim_round(edge, opening_face, distance, tolerance)? {
1235                Some(round) => {
1236                    os_debug!("tangent rim round: torus about edge {edge_id} (neighbour {neighbour_id}, opening {opening_id}) as carrier {}", carriers.len());
1237                    apex_cap_sources.insert(neighbour_index);
1238                    apex_cap_sources.insert(carriers.len());
1239                    apex_cap_pairs.push((neighbour_index, carriers.len()));
1240                    carriers.push(Carrier { solid: round, source_face_id: *neighbour_id, kind: OffsetFaceRole::Offset });
1241                }
1242                None => {
1243                    return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_tangent_rim_round", format!(
1244                        "offset_shell: the round about the tangent rim (edge {edge_id}) between source face {neighbour_id} and \
1245                         curved opening face {opening_id} is built only for a circular rim inside its own circle"
1246                    )));
1247                }
1248            }
1249        }
1250    }
1251    profile::lap(&mut clock, profile::APEX_CAPS);
1252    // A fold the carve's probe did not see (`outward_fold_the_carve_missed`):
1253    // a band too thin for its grid is built whole, and the torus wedge grown by
1254    // 1.001 then held the reflected lemon beyond its axis. Asked of every
1255    // retained face the carve neither carved nor omitted, on every outward
1256    // shell: a ruled wall is what first let the pipeline CLOSE such a shape,
1257    // but the geometry is the trigger. Refused by name, the tangent onset
1258    // included; a partial revolve of that kind is the revolved-wedge lane's
1259    // question, and that lane runs in front of this pipeline.
1260    if distance < 0.0 {
1261        for face_id in &retained {
1262            if carved_supports.contains_key(face_id)
1263                || !carriers.iter().any(|carrier| carrier.source_face_id == *face_id)
1264            {
1265                continue;
1266            }
1267            let face = source_by_id_lookup(&source_faces, *face_id).unwrap();
1268            if let Some(fold) = profile::detail("outward_fold_the_carve_missed", || outward_fold_the_carve_missed(face, distance))? {
1269                return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_outward_fold_band", format!(
1270                    "offset_shell: the outward offset of source face {face_id} folds where the carve \
1271                     saw no fold (area factor {:.3e} at u={:.6}, v={:.6}); a fold band that thin is \
1272                     not built",
1273                    fold.factor, fold.u, fold.v
1274                )));
1275            }
1276        }
1277    }
1278    profile::lap(&mut clock, profile::CARRIER_REGULARITY);
1279    // Wall carriers built as a RULED opening face extended along itself, by
1280    // carrier index: the in-front guard reads their sides off the surface
1281    // normal where it would read a plane's.
1282    let mut ruled_walls = HashSet::default();
1283    // Inward walls at reflex rims need construction area beyond the source
1284    // opening too. Track them for source imprints and material-side selection.
1285    let mut inward_extended_walls = HashSet::default();
1286    let mut reflex_wall_neighbors = HashMap::default();
1287    // OUTWARD through a CURVED opening. A ruled one — the cone a revolved
1288    // straight profile edge sweeps, a cylinder — has its wall built as the
1289    // opening face held in place and EXTENDED along itself, exactly as a plane
1290    // is padded (`extended_ruled_wall`); the pad is the plane's, read off the
1291    // same convex junctions. Built first, so the planar walls know which
1292    // neighbours are ruled walls.
1293    //
1294    // Any other curved opening is refused by name. Its wall used to be the
1295    // unpadded source face, which never reaches the grown offset: on a torus
1296    // wedge whose chord revolves to a cone, the pair offset x cone cut 0
1297    // pieces, the cone's one fragment was dropped as a void cross-section, and
1298    // the result read "completion produced non-integral genus" with 12 open
1299    // edges. No outward shell in the kernel suites or the case corpus closed
1300    // through that wall (census, 2026-09-17).
1301    let mut ruled_built = HashMap::<u64, BrepSolid>::default();
1302    if distance < 0.0 {
1303        for face_id in opening_face_ids {
1304            let source_face = source_by_id_lookup(&source_faces, *face_id).unwrap();
1305            if source_face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
1306                continue;
1307            }
1308            if !ruled_opening(source_face)? {
1309                return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_curved_opening", format!(
1310                    "offset_shell: opening face {face_id} is curved and not a ruled surface; an \
1311                     outward wall is built only on a planar or ruled opening"
1312                )));
1313            }
1314            let pad = outward_wall_pad(
1315                source,
1316                source_face,
1317                &source_faces,
1318                &opening_set,
1319                distance.abs(),
1320            )?;
1321            let tolerance = 1e-7f64.max(crate::solid_scale(source) * 1e-9);
1322            match extended_ruled_wall(source, source_face, pad, tolerance)? {
1323                Ok(wall) => {
1324                    os_debug!(
1325                        "wall for opening face {face_id}: ruled, pad {pad:.4}, sides reach {:.4?}",
1326                        wall.reached
1327                    );
1328                    ruled_built.insert(*face_id, wall.solid);
1329                }
1330                Err(reason) => {
1331                    return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_ruled_wall", format!(
1332                        "offset_shell: the outward wall of ruled opening face {face_id} cannot be \
1333                         built: {reason}"
1334                    )));
1335                }
1336            }
1337        }
1338    }
1339    let ruled_openings = ruled_built
1340        .keys()
1341        .filter_map(|id| source_by_id_lookup(&source_faces, *id))
1342        .collect::<Vec<_>>();
1343    for face_id in opening_face_ids {
1344        let source_face = source_faces
1345            .iter()
1346            .find(|face| face.id == *face_id)
1347            .unwrap();
1348        if let Some(wall) = ruled_built.remove(face_id) {
1349            ruled_walls.insert(carriers.len());
1350            carriers.push(Carrier {
1351                solid: wall,
1352                source_face_id: *face_id,
1353                kind: OffsetFaceRole::Wall,
1354            });
1355            continue;
1356        }
1357        if distance > 0.0
1358            && source_face.surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")?
1359        {
1360            if let Some((pad, neighbors)) =
1361                inward_wall_pad(source, source_face, &source_faces, &opening_set, distance)?
1362            {
1363                // Scaling a notched/free-form outline can still clip the new
1364                // rim. Use the whole plane rectangle; source and offset
1365                // imprints below recover both exact boundaries of the wall.
1366                let padded = carrier_solid(source, *face_id, 0.0, pad)?;
1367                let wall = full_rectangle_face(&padded, &padded.shells[0].faces[0])?;
1368                inward_extended_walls.insert(carriers.len());
1369                reflex_wall_neighbors.insert(carriers.len(), neighbors);
1370                carriers.push(Carrier {
1371                    solid: wall,
1372                    source_face_id: *face_id,
1373                    kind: OffsetFaceRole::Wall,
1374                });
1375                continue;
1376            }
1377        }
1378        carriers.push(Carrier {
1379            solid: if distance < 0.0 && source_face.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
1380                let pad = outward_wall_pad(
1381                    source,
1382                    source_face,
1383                    &source_faces,
1384                    &opening_set,
1385                    distance.abs(),
1386                )?;
1387                // A plane meeting a RULED opening is padded past the grown
1388                // outline, not onto it. Padded by exactly the pad, its trim's
1389                // arc lands ON the grown offset's section wherever the join is
1390                // perpendicular, and the ruled wall's section then ENDS on that
1391                // coincident pair: on the torus wedge the cap shredded into 60
1392                // slivers along the grown circle. The clearance is counted in
1393                // the band that decides whether the strip beyond the grown
1394                // outline is wall or void, so the strip always resolves as its
1395                // own fragment and is dropped as void.
1396                let pad = if ruled_openings
1397                    .iter()
1398                    .any(|ruled| source_faces_adjacent(source_face, ruled))
1399                {
1400                    pad + RULED_NEIGHBOUR_CLEARANCE_BANDS
1401                        * offset_skin_band(crate::solid_scale(source), distance)
1402                } else if outline_has_free_element(source, source_face)? {
1403                    // A free-form outline's wall is a FIT: without the same
1404                    // clearance its outline sits within the fit tolerance of
1405                    // the carriers' own sections on the plane, and the
1406                    // near-coincident curves shred the wall's imprint (26 to
1407                    // 121 fragments on the free-form D band, 2026-09-26). With
1408                    // it, the surplus ring is cut away cleanly.
1409                    pad + RULED_NEIGHBOUR_CLEARANCE_BANDS * offset_skin_band(crate::solid_scale(source), distance)
1410                } else {
1411                    pad
1412                };
1413                os_debug!("wall for opening face {face_id}: outward pad {pad:.4}");
1414                // The wall is the opening's outline OFFSET by the pad in its
1415                // own plane (`outline_offset.rs`). The pad used to SCALE that
1416                // outline about the patch centre, which is the offset only for
1417                // a rectangle filling its patch or a circle centred on it: an
1418                // L-shaped opening's notch edges pass through the centre and
1419                // did not move at all, so the notch faces' grown offsets
1420                // crossed the plane outside the wall ("non-integral genus",
1421                // `l-step-open-bottom` at d = −1.5 in the membrane census).
1422                //
1423                // An outline this module does not offset keeps the scaled wall,
1424                // but only when that wall still COVERS the offset outline along
1425                // every edge a retained face meets — which is what the sections
1426                // have to land inside. Otherwise the shell refuses by name
1427                // rather than assembling an open rim.
1428                let mut scaled = carrier_solid(source, *face_id, 0.0, pad)?;
1429                drop_wall_interior_loops(&mut scaled)?;
1430                let mut wall = match profile::detail("offset_outline_wall", || offset_outline_wall(source, source_face, pad))? {
1431                    Ok(outline) => {
1432                        // Where the scaled wall already trims the offset
1433                        // outline's own region — a rectangle filling its patch,
1434                        // a circle centred on it — it is kept, so no shell that
1435                        // was right before is rebuilt on a different patch.
1436                        let same = profile::detail("walls_trim_the_same_region", || walls_trim_the_same_region(
1437                            &outline,
1438                            &scaled,
1439                            1e-9f64.max(crate::solid_scale(source) * 1e-11),
1440                        ))?;
1441                        if same {
1442                            os_debug!("wall for opening face {face_id}: scaled, which IS its outline offset");
1443                            scaled
1444                        } else {
1445                            os_debug!("wall for opening face {face_id}: outline offset by {pad:.4}");
1446                            outline
1447                        }
1448                    }
1449                    Err(reason) => {
1450                        let carrier_surfaces = carriers
1451                            .iter()
1452                            .filter(|carrier| matches!(carrier.kind, OffsetFaceRole::Offset))
1453                            .map(|carrier| (carrier.source_face_id, &carrier.solid.shells[0].faces[0].surface))
1454                            .collect::<Vec<_>>();
1455                        if let Err(uncovered) = scaled_wall_covers_offset(
1456                            source,
1457                            source_face,
1458                            &scaled,
1459                            pad,
1460                            &source_faces,
1461                            &opening_set,
1462                            &carrier_surfaces,
1463                        )? {
1464                            return Err(KernelRefusal::unsupported(KernelStage::Refine, "offset_shell_opening_wall_coverage", format!(
1465                                "offset_shell: opening face {face_id}'s outline is not offset \
1466                                 ({reason}), and its scaled outward wall does not contain every \
1467                                 retained neighbour's grown offset section (a point of one lies \
1468                                 {uncovered:.3e} outside)"
1469                            )));
1470                        }
1471                        os_debug!(
1472                            "wall for opening face {face_id}: scaled, which covers its offset ({reason})"
1473                        );
1474                        scaled
1475                    }
1476                };
1477                // The pad extends the wall by STRETCHING the plane's 2x2 net
1478                // while the trim pcurves are cloned in UV, so every loop
1479                // scales about the patch centre by (w+2|d|)/w. For the OUTER
1480                // loop of a full-domain rectangle that lands exactly on the
1481                // grown outline — the intended wall extent. But an INTERIOR
1482                // loop (a carve outline: a bore, gouge or crater rim in the
1483                // opening face) is scaled OUTWARD too, and the enlarged hole
1484                // then swallows both circles the wall must be cut by (the
1485                // source carve rim from the wall x source imprint and the
1486                // shrunk rim from the wall x offset-carrier imprint) — the
1487                // imprints clip to nothing and the rims dangle one-use. Drop
1488                // interior loops instead: the wall covers the carve mouth,
1489                // the imprints cut the true rims, and selection (void-wall
1490                // guard + on-skin filter) drops the fragments over the void.
1491                drop_wall_interior_loops(&mut wall)?;
1492                wall
1493            } else {
1494                standalone_face(source, *face_id)?
1495            },
1496            source_face_id: *face_id,
1497            kind: OffsetFaceRole::Wall,
1498        });
1499    }
1500    if carriers.len() >= SOURCE_OPERAND as usize {
1501        return Err(KernelRefusal::unsupported(KernelStage::Collect, "offset_shell_carrier_count", "offset_shell: too many carrier faces for current ABI"));
1502    }
1503    profile::lap(&mut clock, profile::WALLS);
1504    // The characteristic length every tolerance below is derived from.  This
1505    // was `max ‖vertex‖` — the distance from the WORLD ORIGIN — until audit
1506    // slice 0: that form made every band widen as the part was moved away from
1507    // the origin while ignoring the part's actual size, so the same shell
1508    // solved to different tolerances depending only on where it was modelled.
1509    // `solid_scale` is the bbox diagonal, floored at 1.0 exactly as the old
1510    // fold was, so this changes ONE thing: placement-dependence.
1511    let scale = crate::solid_scale(source);
1512    crate::report_scale_migration("offset_shell", scale, || {
1513        source
1514            .vertices
1515            .iter()
1516            .map(|vertex| vertex.point.length())
1517            .fold(1.0, f64::max)
1518    });
1519    let smooth = synchronize_smooth_offset_boundaries(&mut carriers, &source_faces, scale)?;
1520    // Reflex-rim rebuilds run on BOTH attempts: the fallback for a broken
1521    // extension is an honest refusal, but the fallback for a skipped reflex
1522    // rebuild is a silently WRONG (membrane-capped) shell.
1523    let reflex_rebuilt = rebuild_reflex_rim_carriers(
1524        &mut carriers,
1525        source,
1526        &source_faces,
1527        &smooth.pairs,
1528        scale,
1529        distance,
1530    )?;
1531    *reflex_rebuilds = reflex_rebuilt.len();
1532    if extend_oblique_carriers {
1533        let oblique_extended = extend_offset_carriers_past_open_hole_rims(
1534            &mut carriers,
1535            source,
1536            &source_faces,
1537            &opening_set,
1538            &smooth.pairs,
1539            scale,
1540        )?;
1541        let mut already_extended = oblique_extended.clone();
1542        already_extended.extend(reflex_rebuilt.iter().copied());
1543        let fallshort_extended = extend_fallshort_curved_carriers(
1544            &mut carriers,
1545            source,
1546            &source_faces,
1547            &opening_set,
1548            &smooth.pairs,
1549            &already_extended,
1550            scale,
1551            distance,
1552        )?;
1553        *extension_fired = !oblique_extended.is_empty() || fallshort_extended > 0;
1554        os_debug!(
1555            "extended {} oblique + {fallshort_extended} fall-short carriers past their opening planes",
1556            oblique_extended.len(),
1557        );
1558    }
1559    profile::lap(&mut clock, profile::EXTENSION);
1560    let tolerance = 1e-7f64.max(scale * 1e-9);
1561    let pair_tolerance = (scale * 1e-8).max(2e-6);
1562    let reach = distance.abs() * 4.0 + pair_tolerance;
1563    let carrier_samples = carriers
1564        .iter()
1565        .map(|carrier| carrier_extent_points(&carrier.solid))
1566        .collect::<Result<Vec<_>, _>>()?;
1567    let carrier_bounds = carrier_samples
1568        .iter()
1569        .map(|samples| Bounds::from_nonempty_points(samples))
1570        .collect::<Option<Vec<_>>>()
1571        .ok_or_else(|| "offset_shell: carrier has no boundary samples".to_string()).or_refuse(KernelStage::Sew, "offset_shell_carrier_has_no_boundary_samples")?;
1572    let source_by_id = source_faces
1573        .iter()
1574        .map(|face| (face.id, *face))
1575        .collect::<HashMap<_, _>>();
1576    let source_samples = source_faces
1577        .iter()
1578        .map(|face| {
1579            let standalone = standalone_face(source, face.id)?;
1580            Ok((face.id, edge_sample_points(&standalone)?))
1581        })
1582        .collect::<Result<HashMap<_, _>, KernelRefusal>>()?;
1583    if debug_enabled() {
1584        for (index, carrier) in carriers.iter().enumerate() {
1585            let face = &carrier.solid.shells[0].faces[0];
1586            let samples = &carrier_samples[index];
1587            let bounds =
1588                samples
1589                    .iter()
1590                    .fold((Vec3::default(), Vec3::default()), |(low, high), point| {
1591                        (
1592                            Vec3 {
1593                                x: low.x.min(point.x),
1594                                y: low.y.min(point.y),
1595                                z: low.z.min(point.z),
1596                            },
1597                            Vec3 {
1598                                x: high.x.max(point.x),
1599                                y: high.y.max(point.y),
1600                                z: high.z.max(point.z),
1601                            },
1602                        )
1603                    });
1604            let surface = &face.surface;
1605            let rational = surface
1606                .control_points
1607                .iter()
1608                .flatten()
1609                .any(|point| (point.w - 1.0).abs() > 1e-12);
1610            os_debug!(
1611                "carrier[{index}] kind={:?} source_face={} loops={} deg=({},{}) net={}x{} rational={rational} bounds=({:.3},{:.3},{:.3})..({:.3},{:.3},{:.3})",
1612                carrier.kind, carrier.source_face_id, face.loops.len(),
1613                surface.degree_u, surface.degree_v,
1614                surface.control_points.len(),
1615                surface.control_points.first().map(|row| row.len()).unwrap_or(0),
1616                bounds.0.x, bounds.0.y, bounds.0.z, bounds.1.x, bounds.1.y, bounds.1.z,
1617            );
1618        }
1619        for (index, carrier) in carriers.iter().enumerate() {
1620            if !matches!(carrier.kind, OffsetFaceRole::Offset) {
1621                continue;
1622            }
1623            let source_face = source_by_id[&carrier.source_face_id];
1624            let carrier_surface = &carrier.solid.shells[0].faces[0].surface;
1625            let (ku, kv) = (
1626                crate::KnotVector::new(
1627                    source_face.surface.knots_u.clone(),
1628                    source_face.surface.degree_u,
1629                ),
1630                crate::KnotVector::new(
1631                    source_face.surface.knots_v.clone(),
1632                    source_face.surface.degree_v,
1633                ),
1634            );
1635            if let (Ok(ku), Ok(kv)) = (ku, kv) {
1636                let [u0, u1] = ku.domain();
1637                let [v0, v1] = kv.domain();
1638                let mut worst = 0.0f64;
1639                let mut worst_at = (0.0f64, 0.0f64);
1640                for iu in 0..=24 {
1641                    for iv in 0..=24 {
1642                        let u = u0 + (u1 - u0) * iu as f64 / 24.0;
1643                        let v = v0 + (v1 - v0) * iv as f64 / 24.0;
1644                        let expected = face_offsets(source_face)
1645                            .at(u, v, -distance)
1646                            .map(|sample| sample.point);
1647                        let actual = carrier_surface.evaluate(u, v);
1648                        if let (Ok(expected), Ok(actual)) = (expected, actual) {
1649                            let error = expected.sub(actual).length();
1650                            if error > worst {
1651                                worst = error;
1652                                worst_at = (u, v);
1653                            }
1654                        }
1655                    }
1656                }
1657                let mut normal_swing = 0.0f64;
1658                let mut previous: Option<Vec3> = None;
1659                for iu in 0..=48 {
1660                    let u = u0 + (u1 - u0) * iu as f64 / 48.0;
1661                    let v = (v0 + v1) / 2.0;
1662                    if let Ok(normal) = face_normal(source_face, u, v) {
1663                        if let Some(previous) = previous {
1664                            normal_swing = normal_swing.max(previous.sub(normal).length());
1665                        }
1666                        previous = Some(normal);
1667                    }
1668                }
1669                let weights = source_face
1670                    .surface
1671                    .control_points
1672                    .iter()
1673                    .flatten()
1674                    .map(|point| point.w)
1675                    .fold((f64::MAX, f64::MIN), |(low, high), w| {
1676                        (low.min(w), high.max(w))
1677                    });
1678                os_debug!(
1679                    "carrier[{index}] offset fit error max={worst:.6} at=({:.4},{:.4}) domain_u=({u0:.4},{u1:.4}) normal_step_max={normal_swing:.4} src_net={}x{} src_deg=({},{}) src_w=({:.4},{:.4}) knots_u={:?}",
1680                    worst_at.0, worst_at.1,
1681                    source_face.surface.control_points.len(),
1682                    source_face.surface.control_points.first().map(|row| row.len()).unwrap_or(0),
1683                    source_face.surface.degree_u, source_face.surface.degree_v,
1684                    weights.0, weights.1,
1685                    &source_face.surface.knots_u,
1686                );
1687            }
1688        }
1689        os_debug!("smooth pairs: {:?}", smooth.pairs);
1690    }
1691    let mut imprint = empty_imprint();
1692    let mut next_piece_id = 1;
1693    let mut next_vertex_id = 1;
1694    // Lane P3b row 2 (2026-10-26): where the IMPRINT phase spends its time
1695    // outside `build_imprints`, one `offset_shell.imprint_split` line under
1696    // BREP_PROFILE: the carrier-pair builds and merges, the wall x source
1697    // builds, their standalone face copies and merges, the apex ring splits,
1698    // the smooth-split sync and the boundary-coincidence filter.
1699    let split_profile = crate::imprint::profile_enabled();
1700    let mut split_ms = [0.0f64; 9];
1701    let mut split_clock = split_profile.then(web_time::Instant::now);
1702    let mut split_lap = |slot: usize, clock: &mut Option<web_time::Instant>| {
1703        if let Some(started) = clock {
1704            let now = web_time::Instant::now();
1705            split_ms[slot] += (now - *started).as_secs_f64() * 1e3;
1706            *started = now;
1707        }
1708    };
1709    // The pair skip rules, one closure for both imprint paths below (the
1710    // per-pair calls and row 6's one-call composite).
1711    let pair_admitted = |first: usize, second: usize| -> bool {
1712            let first_source = source_by_id[&carriers[first].source_face_id];
1713            let second_source = source_by_id[&carriers[second].source_face_id];
1714            if matches!(carriers[first].kind, OffsetFaceRole::Wall)
1715                && matches!(carriers[second].kind, OffsetFaceRole::Wall)
1716            {
1717                // Unextended inward walls are the opening faces themselves:
1718                // they never overlap; adjacent ones only touch along their
1719                // shared edge, so there is nothing to imprint. OUTWARD walls
1720                // are the opening planes EXTENDED by |distance|: the walls of
1721                // two ADJACENT openings cross along the line of their shared
1722                // source edge, and each wall ring must stop there (the sheet
1723                // past that line lies in front of the other opening). Without
1724                // the cut the wall × source chain below stays open on the
1725                // shared-edge side (its neighbour there is an opening, not a
1726                // retained face), the wall never fragments into its ring, and
1727                // every source rim along both openings dangles one-use.
1728                // Extended inward walls need those mutual cuts as well.
1729                if (distance > 0.0
1730                    && !inward_extended_walls.contains(&first)
1731                    && !inward_extended_walls.contains(&second))
1732                    || !source_faces_adjacent(first_source, second_source)
1733                {
1734                    return false;
1735                }
1736            }
1737            if smooth.pairs.contains(&(first, second)) {
1738                os_debug!("pair ({first},{second}) skipped: smooth");
1739                return false;
1740            }
1741            if !carrier_bounds[first].intersects_upper(carrier_bounds[second], pair_tolerance) {
1742                os_debug!("pair ({first},{second}) skipped: bounds");
1743                return false;
1744            }
1745            if !source_faces_adjacent(first_source, second_source)
1746                && sample_separation(
1747                    &source_samples[&first_source.id],
1748                    &source_samples[&second_source.id],
1749                ) > reach
1750            {
1751                os_debug!("pair ({first},{second}) skipped: separation");
1752                return false;
1753            }
1754            true
1755    };
1756    // The wall x source pairs: an extended opening wall (outward, or inward
1757    // at a reflex rim) against each retained source face adjacent to its
1758    // opening (see the note at the per-pair loop below).
1759    let mut wall_source_pairs: Vec<(usize, u64)> = Vec::new();
1760    if body == Body::Shell && (distance < 0.0 || !inward_extended_walls.is_empty()) {
1761        for index in 0..carriers.len() {
1762            if !matches!(carriers[index].kind, OffsetFaceRole::Wall) {
1763                continue;
1764            }
1765            if distance > 0.0 && !inward_extended_walls.contains(&index) {
1766                continue;
1767            }
1768            let opening = source_by_id[&carriers[index].source_face_id];
1769            for retained_id in &retained {
1770                if source_faces_adjacent(opening, source_by_id[retained_id]) {
1771                    wall_source_pairs.push((index, *retained_id));
1772                }
1773            }
1774        }
1775    }
1776    let one_call = one_call_imprint_enabled();
1777    let mut one_call_done = false;
1778    if one_call {
1779        // ONE-CALL IMPRINT (`BREP_OFFSET_ONE_CALL_IMPRINT`, lane P3b row 6):
1780        // every carrier and every retained source face a wall is cut against
1781        // go into one open sheet solid (ids renumbered per member), one
1782        // `build_imprints` processes exactly the admitted pairs, and its
1783        // post-passes run once over the whole piece set. The record comes
1784        // back in the shell's operand space. A failure of the single call
1785        // falls back to the per-pair path below, which tolerates a failed
1786        // pair on its own.
1787        let mut members: Vec<(u8, &BrepSolid)> = carriers.iter().enumerate().map(|(index, carrier)| (index as u8, &carrier.solid)).collect();
1788        let mut source_members: Vec<(u64, BrepSolid)> = Vec::new();
1789        for (_, retained_id) in &wall_source_pairs {
1790            if !source_members.iter().any(|(id, _)| id == retained_id) {
1791                source_members.push((*retained_id, standalone_face(source, *retained_id)?));
1792            }
1793        }
1794        for (_, solid) in &source_members {
1795            members.push((SOURCE_OPERAND, solid));
1796        }
1797        let composite = compose_imprint_operands(&members);
1798        let mut admitted: HashSet<(usize, usize)> = HashSet::default();
1799        for first in 0..carriers.len() {
1800            for second in first + 1..carriers.len() {
1801                if pair_admitted(first, second) {
1802                    admitted.insert((composite.face_index[first], composite.face_index[second]));
1803                }
1804            }
1805        }
1806        for (index, retained_id) in &wall_source_pairs {
1807            let member = carriers.len() + source_members.iter().position(|(id, _)| id == retained_id).unwrap();
1808            let (a, b) = (composite.face_index[*index], composite.face_index[member]);
1809            admitted.insert((a.min(b), a.max(b)));
1810        }
1811        split_lap(0, &mut split_clock);
1812        match crate::imprint::build_carrier_imprints_within(
1813            &composite.solid,
1814            &|first, second| admitted.contains(&(first, second)),
1815            &ImprintOptions {
1816                tolerance,
1817                maximum_fit_points: 96,
1818                local_fit: false,
1819                fit_chunk_points: None,
1820                maximum_ssi_step: Some((scale * 0.0005).max(tolerance * 100.0)),
1821                span_budget: None,
1822                hermite_sections: false,
1823                tangent_curve_lane: false,
1824                exact_iso_pcurves: offset_exact_iso_pcurves_on(),
1825                march_starts: false,
1826                two_radius_junctions: false,
1827                tangent_curve_imprint: false,
1828            },
1829            !shielded_cusps.is_empty(),
1830        ) {
1831            Ok(record) => {
1832                split_lap(1, &mut split_clock);
1833                imprint = translate_composite_imprint(record, &composite, tolerance, scale)?;
1834                next_piece_id = imprint.pieces.iter().map(|piece| piece.id).max().unwrap_or(0) + 1;
1835                next_vertex_id = imprint.vertices.iter().map(|vertex| vertex.id).max().unwrap_or(0) + 1;
1836                one_call_done = true;
1837                ONE_CALL_RUNS.with(|count| count.set(count.get() + 1));
1838                split_lap(2, &mut split_clock);
1839                os_debug!(
1840                    "one-call imprint: {} members, {} admitted pairs, {} pieces",
1841                    members.len(),
1842                    admitted.len(),
1843                    imprint.pieces.len(),
1844                );
1845            }
1846            Err(error) => {
1847                os_debug!("one-call imprint failed, per-pair path: {error}");
1848            }
1849        }
1850    }
1851    for first in 0..carriers.len() {
1852        for second in first + 1..carriers.len() {
1853            if one_call_done {
1854                break;
1855            }
1856            if !pair_admitted(first, second) {
1857                continue;
1858            }
1859            split_lap(0, &mut split_clock);
1860            let pair = match crate::imprint::build_carrier_imprints(
1861                &carriers[first].solid,
1862                &carriers[second].solid,
1863                &ImprintOptions {
1864                    tolerance,
1865                    maximum_fit_points: 96,
1866                    // The GLOBAL interpolation lane. `local_fit: true` until
1867                    // 2026-09-13, which cost three orders on every smooth
1868                    // section: measured on the apex cone's base circle, the
1869                    // local lane needs 3,070 control points to reach 2.0e-5
1870                    // where the global one reaches 1.4e-8 with 384. What the
1871                    // local lane is for — a rim through a corner, where its
1872                    // monotonicity limiter holds a kink the global cubic rings
1873                    // across, 4.9x measured — is now the fit's own fallback:
1874                    // `fit_polyline` re-runs the other lane whenever the asked
1875                    // one cannot meet the tolerance and returns the better
1876                    // MEASURED curve, so this flag chooses which ladder is
1877                    // paid for first, never which curve is delivered.
1878                    local_fit: false,
1879                    fit_chunk_points: None,
1880                    // Offset carriers can meet in short, tightly curved
1881                    // branches. The general SSI default (diagonal / 15) is
1882                    // too coarse here and lets independently fitted branches
1883                    // miss a shared junction when thickness crosses one of
1884                    // those branches.
1885                    maximum_ssi_step: Some((scale * 0.0005).max(tolerance * 100.0)),
1886                    span_budget: None,
1887                    hermite_sections: false,
1888                    tangent_curve_lane: false,
1889                    exact_iso_pcurves: offset_exact_iso_pcurves_on(),
1890                    march_starts: false,
1891                    two_radius_junctions: false,
1892                    tangent_curve_imprint: false,
1893                },
1894                !shielded_cusps.is_empty(),
1895            ) {
1896                Ok(pair) => pair,
1897                // A failed pair imprint (e.g. the marching SSI exhausting its
1898                // step budget on a long closed sphere/plane intersection) is
1899                // not fatal to the shell: the affected rim simply stays
1900                // unsplit and the rim-weld passes below get to close it. The
1901                // final watertightness gate still refuses anything the welds
1902                // cannot reach, so tolerating the miss only enlarges the
1903                // honest success set.
1904                Err(error) => {
1905                    os_debug!("pair ({first},{second}) imprint failed: {error}");
1906                    continue;
1907                }
1908            };
1909            split_lap(1, &mut split_clock);
1910            let pieces_before = imprint.pieces.len();
1911            merge_pair_imprint(
1912                &mut imprint,
1913                pair,
1914                first as u8,
1915                second as u8,
1916                &mut next_piece_id,
1917                &mut next_vertex_id,
1918                tolerance,
1919                scale,
1920            );
1921            split_lap(2, &mut split_clock);
1922            os_debug!(
1923                "pair ({first},{second}) src=({},{}) pieces+={}",
1924                carriers[first].source_face_id,
1925                carriers[second].source_face_id,
1926                imprint.pieces.len() - pieces_before,
1927            );
1928        }
1929    }
1930    if body == Body::Shell && (distance < 0.0 || !inward_extended_walls.is_empty()) {
1931        // An extended opening-wall carrier (outward, or inward at a reflex
1932        // rim) no longer carries the source outline. OUTWARD it has the GROWN
1933        // outline. Its inner boundary — where the wall ring stops at the
1934        // source solid — is the source outline, and the retained SOURCE faces
1935        // are not carriers, so no carrier×carrier pair ever imprints it.
1936        // Un-cut, the wall stays one whole-plane fragment covering the
1937        // opening void, and the source faces' rim edges dangle one-use.
1938        // Imprint each wall carrier against the retained source faces that
1939        // share an edge with its opening: the wall then fragments into the
1940        // ring (kept) and the opening hole (dropped by the void guard).
1941        for index in 0..carriers.len() {
1942            if one_call_done {
1943                break;
1944            }
1945            if !matches!(carriers[index].kind, OffsetFaceRole::Wall) {
1946                continue;
1947            }
1948            if distance > 0.0 && !inward_extended_walls.contains(&index) {
1949                continue;
1950            }
1951            let opening = source_by_id[&carriers[index].source_face_id];
1952            for retained_id in &retained {
1953                let retained_face = source_by_id[retained_id];
1954                if !source_faces_adjacent(opening, retained_face) {
1955                    continue;
1956                }
1957                split_lap(0, &mut split_clock);
1958                let neighbor = standalone_face(source, *retained_id)?;
1959                split_lap(3, &mut split_clock);
1960                let pair = match build_imprints(
1961                    &carriers[index].solid,
1962                    &neighbor,
1963                    &ImprintOptions {
1964                        tolerance,
1965                        maximum_fit_points: 96,
1966                        // The global lane, as above.
1967                        local_fit: false,
1968                        fit_chunk_points: None,
1969                        maximum_ssi_step: Some((scale * 0.0005).max(tolerance * 100.0)),
1970                        span_budget: None,
1971                        hermite_sections: false,
1972                        tangent_curve_lane: false,
1973                        exact_iso_pcurves: offset_exact_iso_pcurves_on(),
1974                        march_starts: false,
1975                        two_radius_junctions: false,
1976                        tangent_curve_imprint: false,
1977                    },
1978                ) {
1979                    Ok(pair) => pair,
1980                    Err(error) => {
1981                        os_debug!(
1982                            "wall×source pair ({index},{retained_id}) imprint failed: {error}"
1983                        );
1984                        continue;
1985                    }
1986                };
1987                split_lap(4, &mut split_clock);
1988                let pieces_before = imprint.pieces.len();
1989                merge_pair_imprint(
1990                    &mut imprint,
1991                    pair,
1992                    index as u8,
1993                    SOURCE_OPERAND,
1994                    &mut next_piece_id,
1995                    &mut next_vertex_id,
1996                    tolerance,
1997                    scale,
1998                );
1999                split_lap(5, &mut split_clock);
2000                os_debug!(
2001                    "wall×source pair ({index},{retained_id}) pieces+={}",
2002                    imprint.pieces.len() - pieces_before,
2003                );
2004            }
2005        }
2006    }
2007    // APEX-CAP ring splits: the cap × cone section lies ON the cone
2008    // carrier's promoted ring edge (an overlap, not a crossing), so
2009    // `build_imprints` mints no edge splits for the cone side. Without them
2010    // the cone keeps ONE closed ring edge while the cap carries the section
2011    // as arcs — mismatched segmentation, both sides one-use. Record the arc
2012    // junctions as splits on the cone's coincident edge.
2013    for (cone_index, cap_index) in &apex_cap_pairs {
2014        let cap_face_id = carriers[*cap_index].solid.shells[0].faces[0].id;
2015        let piece_ids = imprint
2016            .by_face
2017            .iter()
2018            .find(|entry| entry.operand == *cap_index as u8 && entry.face_id == cap_face_id)
2019            .map(|entry| entry.piece_ids.clone())
2020            .unwrap_or_default();
2021        let coincidence_band = 2e-5f64.max(scale * 1e-7);
2022        for piece_id in piece_ids {
2023            let Some((curve, t0, t1)) = imprint
2024                .pieces
2025                .iter()
2026                .find(|piece| piece.id == piece_id)
2027                .map(|piece| (piece.curve.clone(), piece.t0, piece.t1))
2028            else {
2029                continue;
2030            };
2031            for endpoint in [curve.evaluate(t0).or_refuse(KernelStage::Sew, "evaluate")?, curve.evaluate(t1).or_refuse(KernelStage::Sew, "evaluate")?] {
2032                for edge in &carriers[*cone_index].solid.edges {
2033                    if edge.degenerate {
2034                        continue;
2035                    }
2036                    let projection = crate::project_point_to_curve(&edge.curve, endpoint).or_refuse(KernelStage::Sew, "project_point_to_curve")?;
2037                    if projection.distance > coincidence_band
2038                        || projection.u < edge.t0 + 1e-9
2039                        || projection.u > edge.t1 - 1e-9
2040                    {
2041                        continue;
2042                    }
2043                    let parameter = projection.u;
2044                    if let Some(existing) = imprint.edge_splits.iter_mut().find(|split| {
2045                        split.operand == *cone_index as u8 && split.edge_id == edge.id
2046                    }) {
2047                        if !existing
2048                            .parameters
2049                            .iter()
2050                            .any(|value| (*value - parameter).abs() <= 1e-8)
2051                        {
2052                            existing.parameters.push(parameter);
2053                        }
2054                    } else {
2055                        imprint.edge_splits.push(EdgeSplitRecord {
2056                            operand: *cone_index as u8,
2057                            edge_id: edge.id,
2058                            parameters: vec![parameter],
2059                        });
2060                    }
2061                    os_debug!(
2062                        "apex-cap ring split: cone carrier {cone_index} edge {} at t={parameter:.6}",
2063                        edge.id,
2064                    );
2065                }
2066            }
2067        }
2068    }
2069    if debug_enabled() {
2070        for piece in &imprint.pieces {
2071            let start = piece.curve.evaluate(piece.t0);
2072            let mid = piece.curve.evaluate((piece.t0 + piece.t1) * 0.5);
2073            let end = piece.curve.evaluate(piece.t1);
2074            if let (Ok(start), Ok(mid), Ok(end)) = (start, mid, end) {
2075                os_debug!(
2076                    "piece[{}] ({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4}) on {:?}",
2077                    piece.id,
2078                    start.x,
2079                    start.y,
2080                    start.z,
2081                    mid.x,
2082                    mid.y,
2083                    mid.z,
2084                    end.x,
2085                    end.y,
2086                    end.z,
2087                    piece.support_faces,
2088                );
2089                for pcurve in &piece.pcurves {
2090                    let uv = |t: f64| pcurve.pcurve.evaluate(t).map(|p| (p.x, p.y)).unwrap_or((f64::NAN, f64::NAN));
2091                    let [a, b] = pcurve.pcurve.domain().unwrap_or([0.0, 1.0]);
2092                    os_debug!(
2093                        "  pcurve operand={} face={} uv {:?}..{:?}..{:?}",
2094                        pcurve.operand, pcurve.face_id, uv(a), uv(0.5 * (a + b)), uv(b),
2095                    );
2096                }
2097            }
2098        }
2099    }
2100    split_lap(6, &mut split_clock);
2101    synchronize_smooth_edge_splits(&mut imprint, &smooth.edge_pairs);
2102    split_lap(7, &mut split_clock);
2103    // This decides whether an imprint is genuinely the carrier's existing
2104    // trim, not whether independently fitted curves can later be sewn. The
2105    // looser assembly tolerance can erase a short separating branch while it
2106    // is still departing a boundary.
2107    let boundary_coincidence_tolerance = 2e-5f64.max(scale * 1e-7);
2108    let piece_geometry = imprint
2109        .pieces
2110        .iter()
2111        .map(|piece| {
2112            (
2113                piece.id,
2114                FragmentEdgeGeometry {
2115                    curve: piece.curve.clone(),
2116                    t0: piece.t0,
2117                    t1: piece.t1,
2118                },
2119            )
2120        })
2121        .collect::<HashMap<_, _>>();
2122    for (index, carrier) in carriers.iter().enumerate() {
2123        if !matches!(carrier.kind, OffsetFaceRole::Offset) {
2124            continue;
2125        }
2126        let boundaries = carrier
2127            .solid
2128            .edges
2129            .iter()
2130            .map(|edge| FragmentEdgeGeometry {
2131                curve: edge.curve.clone(),
2132                t0: edge.t0,
2133                t1: edge.t1,
2134            })
2135            .collect::<Vec<_>>();
2136        if let Some(by_face) = imprint
2137            .by_face
2138            .iter_mut()
2139            .find(|entry| entry.operand == index as u8)
2140        {
2141            let before = by_face.piece_ids.clone();
2142            by_face.piece_ids.retain(|piece_id| {
2143                let piece = &piece_geometry[piece_id];
2144                !boundaries.iter().any(|boundary| {
2145                    fragment_edge_lies_on(piece, boundary, boundary_coincidence_tolerance)
2146                        .unwrap_or(false)
2147                })
2148            });
2149            // Separate retained planes can imprint overlapping portions of the
2150            // same line on this carrier. Arrange that cut once: duplicate
2151            // segments otherwise erase the region between a blend section
2152            // and the line. Preserve each piece on its other carrier.
2153            let candidates = by_face.piece_ids.clone();
2154            by_face.piece_ids.retain(|id| {
2155                let edge = &piece_geometry[id];
2156                if edge.curve.degree != 1 || edge.curve.control_points.len() != 2 {
2157                    return true;
2158                }
2159                !candidates.iter().any(|other_id| {
2160                    if id == other_id { return false; }
2161                    let other = &piece_geometry[other_id];
2162                    if other.curve.degree != 1 || other.curve.control_points.len() != 2 {
2163                        return false;
2164                    }
2165                    fragment_edge_lies_on(edge, other, tolerance).unwrap_or(false)
2166                        && (other_id < id
2167                            || !fragment_edge_lies_on(other, edge, tolerance).unwrap_or(false))
2168                })
2169            });
2170            if debug_enabled() && before.len() != by_face.piece_ids.len() {
2171                os_debug!(
2172                    "carrier[{index}] dropped boundary-coincident pieces: {:?} -> {:?}",
2173                    before,
2174                    by_face.piece_ids,
2175                );
2176            }
2177        }
2178    }
2179
2180    if debug_enabled() {
2181        for entry in &imprint.by_face {
2182            os_debug!(
2183                "by_face operand={} face={} pieces={:?}",
2184                entry.operand,
2185                entry.face_id,
2186                entry.piece_ids,
2187            );
2188        }
2189    }
2190    split_lap(8, &mut split_clock);
2191    if split_profile {
2192        profile_line!(
2193            "offset_shell.imprint_split skips={:.3} pair_build={:.3} pair_merge={:.3} standalone={:.3} wall_build={:.3} wall_merge={:.3} ring={:.3} smooth={:.3} filter={:.3}",
2194            split_ms[0], split_ms[1], split_ms[2], split_ms[3], split_ms[4], split_ms[5], split_ms[6], split_ms[7], split_ms[8],
2195        );
2196    }
2197    profile::lap(&mut clock, profile::IMPRINT);
2198    let mut split_carriers = Vec::new();
2199    let mut fragments_by_carrier = Vec::new();
2200    for (index, carrier) in carriers.iter().enumerate() {
2201        // Offset carriers keep their boundary un-split (their trims never
2202        // coincide with imprints — the boundary-coincidence filter drops such
2203        // pieces instead). An apex-cap ring breaks that assumption: the
2204        // frustum's ring is its TRIM boundary while the cap crosses it as TWO
2205        // imprint arcs — without splitting, the two sides segment differently
2206        // and both stay one-use. Split exactly the cap-affected carriers.
2207        let split = if matches!(carrier.kind, OffsetFaceRole::Offset)
2208            && !apex_cap_sources.contains(&index)
2209        {
2210            carrier.solid.clone()
2211        } else {
2212            apply_edge_splits(&carrier.solid, index as u8, &imprint)?
2213        };
2214        let fragment_clock = web_time::Instant::now();
2215        let fragments = fragment_solid(&split, index as u8, &imprint)?;
2216        os_debug!(
2217            "carrier[{index}] kind={:?} src={} fragments={} ({:.3} ms)",
2218            carrier.kind,
2219            carrier.source_face_id,
2220            fragments.len(),
2221            fragment_clock.elapsed().as_secs_f64() * 1e3,
2222        );
2223        if debug_enabled() {
2224            for (fragment_index, fragment) in fragments.iter().enumerate() {
2225                let mut boundary_count = 0usize;
2226                let mut imprint_pieces = Vec::new();
2227                let mut derived_count = 0usize;
2228                for coedge in fragment
2229                    .loops
2230                    .iter()
2231                    .flat_map(|loop_record| &loop_record.coedges)
2232                {
2233                    match &coedge.source {
2234                        FragmentEdgeSource::Boundary { .. }
2235                        | FragmentEdgeSource::SharedBoundary { .. } => boundary_count += 1,
2236                        FragmentEdgeSource::Imprint { piece_id } => imprint_pieces.push(*piece_id),
2237                        FragmentEdgeSource::Derived { .. } => derived_count += 1,
2238                    }
2239                }
2240                os_debug!(
2241                    "  frag[{index}.{fragment_index}] test=({:.3},{:.3},{:.3}) uv=({:.3},{:.3}) boundary={boundary_count} derived={derived_count} pieces={imprint_pieces:?}",
2242                    fragment.test_point.x, fragment.test_point.y, fragment.test_point.z,
2243                    fragment.test_uv.x, fragment.test_uv.y,
2244                );
2245            }
2246        }
2247        split_carriers.push(split);
2248        fragments_by_carrier.push(fragments);
2249    }
2250    profile::lap(&mut clock, profile::FRAGMENT);
2251    let mut assembly_sources = [(SOURCE_OPERAND, source)]
2252        .into_iter()
2253        .collect::<HashMap<_, _>>();
2254    for (index, carrier) in split_carriers.iter().enumerate() {
2255        assembly_sources.insert(index as u8, carrier);
2256    }
2257
2258    let source_edge_by_id = source
2259        .edges
2260        .iter()
2261        .map(|edge| (edge.id, edge))
2262        .collect::<HashMap<_, _>>();
2263    let retained_faces_with_edges = retained
2264        .iter()
2265        .map(|face_id| {
2266            let face = source_by_id[face_id];
2267            let edges = face
2268                .loops
2269                .iter()
2270                .flat_map(|loop_record| &loop_record.coedges)
2271                .filter_map(|coedge| source_edge_by_id.get(&coedge.edge_id).copied())
2272                .collect::<Vec<_>>();
2273            (face, edges)
2274        })
2275        .collect::<Vec<_>>();
2276    let mut offset_skin_tolerance = offset_skin_band(scale, distance);
2277    // `BREP_OFFSET_THIN_SKIN_PAD`: the band's 2e-3 floor is a fixed length;
2278    // on a shell thinner than it (box-paper-thin, d = 1e-3) no point was ever
2279    // shadowed (`separation < d - 2e-3` cannot hold), so the opening's 1e-3
2280    // corner squares, holding no wall seed, read as the void and were left
2281    // out of the rim. A thin shell's band is held to a quarter of it.
2282    if thin_skin_pad_enabled() && distance > 0.0 {
2283        offset_skin_tolerance = offset_skin_tolerance.min(0.25 * distance.abs());
2284    }
2285    // OUTWARD shells: a wall fragment lying IN FRONT of another opening —
2286    // past that opening's plane on its void side, within the extent of its
2287    // (extended) wall carrier — is not material: an opening removes
2288    // everything in front of it. Such a fragment is the sheet of this wall
2289    // between the source outline and the grown outline on the far side of an
2290    // ADJACENT opening's shared edge (the x∈[−1,0] strip of an opened −Z
2291    // face's wall, in front of the opened −X face). It sits within |distance|
2292    // of a retained face's corner edge, so the void-cross-section guard reads
2293    // it as wall thickness; only the other opening's plane tells it apart.
2294    //
2295    // A RULED opening's wall is curved, so it has no one plane: its front is
2296    // read at the point's foot on the wall carrier, along the carrier's normal
2297    // turned to the opening face's outward side. On the torus wedge that is
2298    // what drops the start and end caps' strips beyond the extended chord,
2299    // which lie within |distance| of the torus and in front of the cone.
2300    let mut opening_planes: Vec<(usize, Vec3, Vec3)> = Vec::new();
2301    let mut opening_ruled: Vec<(usize, f64)> = Vec::new();
2302    if distance < 0.0 {
2303        for (index, carrier) in carriers.iter().enumerate() {
2304            if !matches!(carrier.kind, OffsetFaceRole::Wall) {
2305                continue;
2306            }
2307            let opening = source_by_id[&carrier.source_face_id];
2308            let [u0, u1] = opening.surface.domain_u().or_refuse(KernelStage::Sew, "domain_u")?;
2309            let [v0, v1] = opening.surface.domain_v().or_refuse(KernelStage::Sew, "domain_v")?;
2310            let (u, v) = ((u0 + u1) * 0.5, (v0 + v1) * 0.5);
2311            if ruled_walls.contains(&index) {
2312                let wall = &split_carriers[index].shells[0].faces[0];
2313                let probe = project_point_to_surface(&wall.surface, opening.surface.evaluate(u, v).or_refuse(KernelStage::Sew, "evaluate")?).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
2314                let side = face_normal(wall, probe.u, probe.v)?.dot(face_normal(opening, u, v)?);
2315                opening_ruled.push((index, if side < 0.0 { -1.0 } else { 1.0 }));
2316                continue;
2317            }
2318            if !opening.surface.is_affine().or_refuse(KernelStage::Sew, "is_affine")? {
2319                continue;
2320            }
2321            opening_planes.push((
2322                index,
2323                opening.surface.evaluate(u, v).or_refuse(KernelStage::Sew, "evaluate")?,
2324                face_normal(opening, u, v)?,
2325            ));
2326        }
2327    }
2328    let in_front_of_other_opening = |wall_index: usize, point: Vec3| -> Result<bool, KernelRefusal> {
2329        let band = 2e-3f64.max(scale * 5e-5);
2330        for (index, plane_point, outward) in &opening_planes {
2331            if *index == wall_index || point.sub(*plane_point).dot(*outward) <= band {
2332                continue;
2333            }
2334            let face = &split_carriers[*index].shells[0].faces[0];
2335            let projection = project_point_to_surface(&face.surface, point).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
2336            let uv = Vec2 {
2337                x: projection.u,
2338                y: projection.v,
2339            };
2340            if parameter_point_in_face(face, uv, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside {
2341                return Ok(true);
2342            }
2343        }
2344        for (index, side) in &opening_ruled {
2345            if *index == wall_index {
2346                continue;
2347            }
2348            let face = &split_carriers[*index].shells[0].faces[0];
2349            let projection = project_point_to_surface(&face.surface, point).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
2350            let outward = face_normal(face, projection.u, projection.v)?.scale(*side);
2351            if point.sub(projection.point).dot(outward) <= band {
2352                continue;
2353            }
2354            let uv = Vec2 {
2355                x: projection.u,
2356                y: projection.v,
2357            };
2358            if parameter_point_in_face(face, uv, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside {
2359                return Ok(true);
2360            }
2361        }
2362        Ok(false)
2363    };
2364
2365    let mut chosen_offsets = Vec::new();
2366    let mut chosen_walls = Vec::new();
2367    let mut newly_shadowed = HashSet::<u64>::default();
2368    for (index, carrier) in carriers.iter().enumerate() {
2369        let raw_seeds = source_seeds(source, carrier.source_face_id)?;
2370        let mut seeds = Vec::with_capacity(raw_seeds.len());
2371        for raw_seed in raw_seeds {
2372            let seed = if matches!(carrier.kind, OffsetFaceRole::Offset) {
2373                offset_seed(
2374                    source_by_id[&carrier.source_face_id],
2375                    raw_seed,
2376                    &split_carriers[index].shells[0].faces[0].surface,
2377                    distance,
2378                    2e-3f64.max(scale * 5e-5),
2379                )?
2380                .unwrap_or(raw_seed)
2381            } else if inward_extended_walls.contains(&index) {
2382                offset_seed(
2383                    source_by_id[&carrier.source_face_id],
2384                    raw_seed,
2385                    &split_carriers[index].shells[0].faces[0].surface,
2386                    0.0,
2387                    2e-3f64.max(scale * 5e-5),
2388                )?
2389                .unwrap_or(raw_seed)
2390            } else {
2391                raw_seed
2392            };
2393            if seed.sub(raw_seed).length() > 1e-9 {
2394                os_debug!(
2395                    "  seed[{index}] moved from ({:.4},{:.4}) to the offset image ({:.4},{:.4})",
2396                    raw_seed.x,
2397                    raw_seed.y,
2398                    seed.x,
2399                    seed.y
2400                );
2401            }
2402            seeds.push(seed);
2403        }
2404        let seed = seeds[0];
2405        if matches!(carrier.kind, OffsetFaceRole::Offset) {
2406            // Fragments whose test point sits at the full offset distance
2407            // from every retained source face are the true offset-skin
2408            // regions. Fragments closer to some other face are shadowed
2409            // pockets/strips that another carrier owns.
2410            // A fragment whose offset lies ON another retained face's offset
2411            // (material exactly twice the shell distance thick: a slot, fin or
2412            // floor at t = 2d) reads as SHADOWED by that face. One hair
2413            // thinner, the same fragment reads shadowed on its own and is
2414            // dropped by the on-skin filter, or omits its carrier by the
2415            // skinless rule below, so the answer is the limit of that
2416            // construction: the census's L∞ forms are continuous across the
2417            // onset. Without this the fragment read On, was selected, and the
2418            // shell refused as "a cavity pinched to zero thickness". The
2419            // coincidence test is `coincident_offset_face`'s (the other face's
2420            // distance within `offset_skin_tolerance` of |d|, its foot on the
2421            // opposite side of the point from this face's own), taken only on
2422            // the THIN side of the onset.
2423            let mut readings = Vec::with_capacity(fragments_by_carrier[index].len());
2424            // The construction band allows a carrier's own fit residual. It
2425            // must not also preserve pockets buried by neighboring offsets:
2426            // that coverage is what justified admitting a finite cusp. Keep
2427            // the own-face reading unchanged, and measure other faces at the
2428            // fit tolerance for this construction.
2429            let cusp_neighbors = if shielded_cusps.is_empty() {
2430                Vec::new()
2431            } else {
2432                retained_faces_with_edges.iter()
2433                    .filter(|(face, _)| face.id != carrier.source_face_id)
2434                    .cloned().collect::<Vec<_>>()
2435            };
2436            for fragment in &fragments_by_carrier[index] {
2437                let reading = offset_skin_reading(
2438                    fragment.test_point,
2439                    &retained_faces_with_edges,
2440                    distance,
2441                    offset_skin_tolerance,
2442                );
2443                let reading = if matches!(&reading, SkinReading::On) && !cusp_neighbors.is_empty() {
2444                    offset_skin_reading(
2445                        fragment.test_point,
2446                        &cusp_neighbors,
2447                        distance,
2448                        4.0 * KernelTolerances::for_solid(source, 1e-7).intersection_fit,
2449                    )
2450                } else {
2451                    reading
2452                };
2453                let reading = match reading {
2454                    SkinReading::On => match coincident_offset_face(
2455                        fragment.test_point,
2456                        carrier.source_face_id,
2457                        &retained_faces_with_edges,
2458                        distance,
2459                        offset_skin_tolerance,
2460                    )? {
2461                        // ONE-SIDED and tight: only material no thicker than
2462                        // 2|d| (to 10× the model tolerance) takes the thin
2463                        // construction, which is exact there. A wall inside
2464                        // the skin band but genuinely thicker than 2|d| keeps
2465                        // its sliver cavity's named refusal below; taking the
2466                        // thin construction there would fill a real cavity.
2467                        Some((face, separation)) if separation <= distance.abs() + tolerance * 10.0 => {
2468                            // A coincidence the fragment's other interior
2469                            // points do not share is a tangent TOUCH: the
2470                            // cavity pinches to a line there, and dropping the
2471                            // fragment as a sheet would discard real skin.
2472                            if tangent_touch_on() {
2473                                if let Some((farther, far)) = tangent_touch(
2474                                    fragment,
2475                                    face,
2476                                    &retained_faces_with_edges,
2477                                    distance,
2478                                    offset_skin_tolerance,
2479                                )? {
2480                                    return Err(KernelRefusal::unsupported(KernelStage::Select, refusals::TANGENT_OFFSETS, format!(
2481                                        "offset_shell: the offsets of source faces {} and {face} touch tangentially near \
2482                                         ({:.4}, {:.4}, {:.4}) — the material between those faces is exactly twice the \
2483                                         shell distance thick along that touch (separation {separation:.6} against {:.6}) \
2484                                         and thicker beside it (the offset reads {far:.4} from face {face} at ({:.4}, {:.4}, {:.4})), so the cavity would \
2485                                         pinch to a line there; a cavity pinched to a line is not supported — use a \
2486                                         shell distance a little below {:.4}",
2487                                        carrier.source_face_id,
2488                                        fragment.test_point.x,
2489                                        fragment.test_point.y,
2490                                        fragment.test_point.z,
2491                                        distance.abs(),
2492                                        farther.x,
2493                                        farther.y,
2494                                        farther.z,
2495                                        distance.abs(),
2496                                    )));
2497                                }
2498                            }
2499                            SkinReading::Shadowed { face, separation }
2500                        }
2501                        _ => SkinReading::On,
2502                    },
2503                    other => other,
2504                };
2505                // One test point cannot speak for a skin that bounds a cavity
2506                // the solid demonstrably has: see `cavity_witness`.
2507                let reading = match reading {
2508                    SkinReading::Shadowed { .. } if cavity_bounds.contains(&carrier.source_face_id) => SkinReading::On,
2509                    other => other,
2510                };
2511                readings.push(reading);
2512            }
2513            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
2514                if let SkinReading::Unmeasured { face, reason } = reading {
2515                    return Err(unmeasured_skin_refusal(fragment.test_point, *face, reason));
2516                }
2517            }
2518            // EVERY fragment measured nearer than the distance to some other
2519            // retained face: this support's offset is SHADOWED over its whole
2520            // extended carrier — the side walls of a slot narrower than 2|d|
2521            // grown outward, a pocket floor within 2d of the face under it.
2522            // It owns no skin. It used to switch the filter off and keep its
2523            // seeded fragment anyway, which built a closed, validating shell
2524            // with an inverted box inside it (+90 on a width-2 slot grown by
2525            // 1.5, with the 90 of material at its centre missing). Dropping
2526            // only its fragments is not enough: its carrier has already cut
2527            // its neighbours, and the top skin over that slot keeps a hole
2528            // nothing fills. So the pass is re-run with the support omitted
2529            // before the imprint, like a collapsed one. An apex-cap sphere
2530            // shares its cone's source id and is not judged here.
2531            //
2532            // Only the FIRST pass omits. A test point speaks for its fragment
2533            // because every other carrier's imprint bounds it; omitting a
2534            // carrier removes cuts, merges fragments and moves test points,
2535            // but changes no distance, so a support that reads shadowed only
2536            // AFTER an omission is reading a test point that no longer stands
2537            // for its fragment. Measured on two bosses 1 apart grown outward
2538            // by 1.5: once the facing boss walls were omitted, the plate's
2539            // grown top — one uncut fragment, mostly skin — read shadowed at
2540            // its single test point.
2541            //
2542            // "Every fragment" counts a fragment the selection would drop by
2543            // CLASS as not skin, as the selection does: the extended carrier's
2544            // surplus past a slot's end reads at least the distance from every
2545            // face, lies inside the source, and on a slot 1 wide grown outward
2546            // left the slot floor unjudged until the side walls were gone.
2547            // ADRIFT: a fragment whose test point is farther from its OWN face
2548            // than the distance, in the mitered metric, is not that face's skin
2549            // — it is inside the cavity, past where its neighbours' offsets cut
2550            // it. A rib, a pin or a divider thinner than 2d shelled inward
2551            // keeps such fragments, and the assembly then fails on them.
2552            let adrift = fragments_by_carrier[index]
2553                .iter()
2554                .map(|fragment| {
2555                    point_adrift_of_its_own_offset(
2556                        fragment.test_point,
2557                        carrier.source_face_id,
2558                        &retained_faces_with_edges,
2559                        distance,
2560                        offset_skin_tolerance,
2561                    )
2562                })
2563                .collect::<Vec<_>>();
2564            let mut skinless = readings
2565                .iter()
2566                .any(|reading| matches!(reading, SkinReading::Shadowed { .. }))
2567                || adrift.iter().any(|flag| *flag);
2568            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
2569                if !skinless {
2570                    break;
2571                }
2572                if matches!(reading, SkinReading::On)
2573                    && !adrift[fragments_by_carrier[index]
2574                        .iter()
2575                        .position(|other| std::ptr::eq(other, fragment))
2576                        .unwrap_or(0)]
2577                {
2578                    let class = classify_point(fragment.test_point, source, tolerance * 10.0).or_refuse(KernelStage::Sew, "classify_point")?.class;
2579                    skinless = (distance > 0.0 && class == PointClass::Out)
2580                        || (distance < 0.0 && class == PointClass::In);
2581                }
2582            }
2583            if skinless && !apex_cap_sources.contains(&index) {
2584                if body == Body::Shell && distance > 0.0 && cascade_omission_enabled() {
2585                    let held = fragments_holding_true_skin(
2586                        source_by_id[&carrier.source_face_id],
2587                        source,
2588                        &fragments_by_carrier[index],
2589                        &retained_faces_with_edges,
2590                        distance,
2591                        offset_skin_tolerance,
2592                    )?;
2593                    if !held.is_empty() {
2594                        os_debug!("  select[{index}] src={}: {} fragment(s) hold true skin", carrier.source_face_id, held.len());
2595                        chosen_offsets.extend(held);
2596                        continue;
2597                    }
2598                }
2599                if !shadowed_supports.is_empty() && !(body == Body::Shell && distance > 0.0 && cascade_omission_enabled()) {
2600                    let mut omitted = shadowed_supports.iter().copied().collect::<Vec<_>>();
2601                    omitted.sort_unstable();
2602                    return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_shadow_cascade", format!(
2603                        "offset_shell: omitting the shadowed offsets of source faces {omitted:?} \
2604                         leaves the offset of source face {} shadowed at every one of its {} \
2605                         fragment test points, which no longer stand for their fragments once \
2606                         those carriers stop cutting it",
2607                        carrier.source_face_id,
2608                        readings.len()
2609                    )));
2610                }
2611                os_debug!(
2612                    "  select[{index}] src={} SHADOWED over all {} fragment(s): {:?}",
2613                    carrier.source_face_id,
2614                    readings.len(),
2615                    readings
2616                );
2617                newly_shadowed.insert(carrier.source_face_id);
2618                continue;
2619            }
2620            // Partly shadowed: keep a bounded sample of the shadowed test
2621            // points, so a refusal can say where two offsets cross.
2622            for (fragment, reading) in fragments_by_carrier[index].iter().zip(&readings) {
2623                if shadowed_points.len() >= MAXIMUM_CROSSING_READS {
2624                    break;
2625                }
2626                if matches!(reading, SkinReading::Shadowed { .. }) {
2627                    shadowed_points.push((fragment.test_point, carrier.source_face_id));
2628                }
2629            }
2630            let mut on_skin = readings
2631                .iter()
2632                .zip(&adrift)
2633                .map(|(reading, adrift)| matches!(reading, SkinReading::On) && !adrift)
2634                .collect::<Vec<_>>();
2635            if open_section_reach {
2636                if let Some(ends) = open_profile_end_neighbours(index, &carriers, &source_faces)? {
2637                    // A continued variable profile can approach its original
2638                    // end cap inside the distance band. Its sharp boundary is
2639                    // the section with the OFFSET end plane, not that old cap.
2640                    for (keep, fragment) in on_skin.iter_mut().zip(&fragments_by_carrier[index]) {
2641                        *keep = !outside_continued_neighbours(fragment.test_point, &ends, &carriers, tolerance)?;
2642                    }
2643                    os_debug!("  select[{index}] open profile kept {} central regions", on_skin.iter().filter(|keep| **keep).count());
2644                }
2645            }
2646            // A synthetic apex sphere has no source-face seed of its own to
2647            // fall back to. When its whole cap is hidden by retained offsets,
2648            // keep its cuts but contribute no skin. Otherwise the fallback
2649            // selects the uncut sphere as a stray patch inside the material.
2650            // The parent cone remains subject to the ordinary selection.
2651            let skin_filter_active = on_skin.iter().any(|flag| *flag)
2652                || apex_cap_pairs.iter().any(|(_, cap)| *cap == index);
2653            let opening_limits = if inward_extended_walls.is_empty() {
2654                Vec::new()
2655            } else {
2656                let openings = inward_extended_walls
2657                    .iter()
2658                    .map(|index| source_by_id[&carriers[*index].source_face_id])
2659                    .collect::<Vec<_>>();
2660                opening_trim_halfspaces(source_by_id[&carrier.source_face_id], &openings, tolerance)?
2661            };
2662            let mut classified = Vec::new();
2663            let mut viable = Vec::new();
2664            for (fragment_index, fragment) in fragments_by_carrier[index].iter().enumerate() {
2665                let class = classify_point(fragment.test_point, source, tolerance * 10.0).or_refuse(KernelStage::Sew, "classify_point")?.class;
2666                let excluded_by_class = (distance > 0.0 && class == PointClass::Out)
2667                    || (distance < 0.0 && class == PointClass::In);
2668                let contact = if excluded_by_class {
2669                    false
2670                } else {
2671                    distance > 0.0
2672                        && fragment_has_sustained_source_contact(
2673                            fragment,
2674                            &source_faces,
2675                            &assembly_sources,
2676                            &imprint,
2677                            2e-3f64.max(scale * 5e-5),
2678                        )?
2679                };
2680                os_debug!(
2681                    "  select[{index}.{fragment_index}] class={class:?} contact={contact} on_skin={} reading={:?} adrift={} seed_in={:?} uv=({:.3},{:.3})",
2682                    on_skin[fragment_index],
2683                    readings[fragment_index],
2684                    adrift[fragment_index],
2685                    parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8),
2686                    fragment.test_uv.x, fragment.test_uv.y,
2687                );
2688                let past_opening = opening_limits.iter().any(|(origin, normal)| {
2689                    fragment.test_point.sub(*origin).dot(*normal) < -tolerance * 10.0
2690                });
2691                if excluded_by_class
2692                    || past_opening
2693                    || (skin_filter_active && !on_skin[fragment_index])
2694                {
2695                    continue;
2696                }
2697                classified.push(fragment.clone());
2698                if contact {
2699                    continue;
2700                }
2701                viable.push(fragment.clone());
2702            }
2703            // One skin per REGION of the source face (see `source_seeds`):
2704            // every region of a pinched face is offset like a face of its
2705            // own, and the selection below answers each region's seed. An
2706            // ordinary face has the one seed and the one answer it always had.
2707            let mut region_skins = Vec::new();
2708            let continued_neighbours = if distance > 0.0 {
2709                continued_sphere_neighbours(index, &carriers, &source_faces, scale)?
2710            } else { None };
2711            if let Some(neighbours) = &continued_neighbours {
2712                for fragment in &classified {
2713                    if outside_continued_neighbours(fragment.test_point, neighbours, &carriers, tolerance)? {
2714                        region_skins.push(fragment.clone());
2715                    }
2716                }
2717                os_debug!("  select[{index}] continued sphere kept {} regions", region_skins.len());
2718            }
2719            for &seed in seeds.iter().filter(|_| continued_neighbours.is_none()) {
2720                os_debug!("  select[{index}] seed=({:.4},{:.4})", seed.x, seed.y);
2721                let seeded = viable
2722                    .iter()
2723                    .filter(|fragment| {
2724                        parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8)
2725                            .is_ok_and(|class| class != PolygonClass::Outside)
2726                    })
2727                    .cloned()
2728                    .collect::<Vec<_>>();
2729                let seeded_classified = classified
2730                    .iter()
2731                    .filter(|fragment| {
2732                        parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8)
2733                            .is_ok_and(|class| class != PolygonClass::Outside)
2734                    })
2735                    .cloned()
2736                    .collect::<Vec<_>>();
2737                let selected = if viable.is_empty() {
2738                    if !skin_filter_active {
2739                        // NOTHING on this carrier reads on-skin: every classified
2740                        // fragment's test point is measurably CLOSER than the
2741                        // offset distance to some retained face — a buried strip
2742                        // another carrier owns. Forcing the largest one in anyway
2743                        // plants a wrong skin patch that collides with the
2744                        // completion phase's reconstruction (3-use edges).
2745                        // Contribute nothing: `complete_opening_boundary_cycles`
2746                        // rebuilds the true skin from the carrier surface, and the
2747                        // watertight gate still refuses if it cannot.
2748                        None
2749                    } else {
2750                        let mut by_area = classified
2751                            .iter()
2752                            .cloned()
2753                            .map(|fragment| {
2754                                let area = parameter_space_area(&fragment_as_trim(&fragment)).or_refuse(KernelStage::Sew, "parameter_space_area")?.abs();
2755                                Ok((fragment, area))
2756                            })
2757                            .collect::<Result<Vec<_>, KernelRefusal>>()?;
2758                        by_area.sort_by(|a, b| b.1.total_cmp(&a.1));
2759                        by_area.into_iter().next().map(|entry| entry.0)
2760                    }
2761                } else if !seeded_classified.is_empty() {
2762                    let mut candidates = seeded_classified;
2763                    candidates.sort_by(|a, b| {
2764                        a.test_uv
2765                            .sub(seed)
2766                            .length()
2767                            .total_cmp(&b.test_uv.sub(seed).length())
2768                    });
2769                    candidates.into_iter().next()
2770                } else if viable.len() > 2 {
2771                    let mut by_area = viable
2772                        .iter()
2773                        .cloned()
2774                        .map(|fragment| {
2775                            let area = parameter_space_area(&fragment_as_trim(&fragment)).or_refuse(KernelStage::Sew, "parameter_space_area")?.abs();
2776                            Ok((fragment, area))
2777                        })
2778                        .collect::<Result<Vec<_>, KernelRefusal>>()?;
2779                    by_area.sort_by(|a, b| b.1.total_cmp(&a.1));
2780                    by_area.into_iter().next().map(|entry| entry.0)
2781                } else {
2782                    let mut candidates = if seeded.is_empty() { viable.clone() } else { seeded };
2783                    candidates.sort_by(|a, b| {
2784                        a.test_uv
2785                            .sub(seed)
2786                            .length()
2787                            .total_cmp(&b.test_uv.sub(seed).length())
2788                    });
2789                    candidates.into_iter().next()
2790                };
2791                let mut drop_debris = false;
2792                if let Some(fragment) = &selected {
2793                    os_debug!(
2794                        "  select[{index}] src={} chosen test point departs its own face's offset \
2795                         distance by {:?}",
2796                        carrier.source_face_id,
2797                        own_offset_departure(
2798                            fragment.test_point,
2799                            carrier.source_face_id,
2800                            &retained_faces_with_edges,
2801                            distance
2802                        )
2803                    );
2804                    os_debug!(
2805                        "  select[{index}] src={} chosen test point departs in the MITER metric by {:?}",
2806                        carrier.source_face_id,
2807                        own_miter_departure(
2808                            fragment.test_point,
2809                            carrier.source_face_id,
2810                            &retained_faces_with_edges,
2811                            distance
2812                        )
2813                    );
2814                    let coincidence = coincident_offset_face(
2815                        fragment.test_point,
2816                        carrier.source_face_id,
2817                        &retained_faces_with_edges,
2818                        distance,
2819                        offset_skin_tolerance,
2820                    )?;
2821                    let coincidence = match coincidence {
2822                        Some((other, separation))
2823                            if coincide_debris_on()
2824                                && fragment_is_band_debris(fragment, offset_skin_tolerance)? =>
2825                        {
2826                            os_debug!(
2827                                "  select[{index}] src={} drops corner debris coincident with {other} \
2828                                 (separation {separation}) at {:?}",
2829                                carrier.source_face_id,
2830                                fragment.test_point
2831                            );
2832                            drop_debris = true;
2833                            None
2834                        }
2835                        reading => reading,
2836                    };
2837                    if let Some((other, separation)) = coincidence {
2838                        return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_coincident_offsets", format!(
2839                            "offset_shell: the offsets of source faces {} and {other} coincide near \
2840                             ({:.4}, {:.4}, {:.4}) — the material between those faces is twice the \
2841                             shell distance thick (separation {separation:.6} against {:.6}, band \
2842                             {offset_skin_tolerance:.1e}), so the cavity between them has no \
2843                             thickness there; a cavity pinched to zero thickness is not supported",
2844                            carrier.source_face_id,
2845                            fragment.test_point.x,
2846                            fragment.test_point.y,
2847                            fragment.test_point.z,
2848                            distance.abs(),
2849                        )));
2850                    }
2851                }
2852                let selected = if drop_debris { None } else { selected };
2853                if let Some(fragment) = selected {
2854                    // A pinched face's seeds each find their own region; a
2855                    // fallback that ignores the seed can answer the same
2856                    // fragment twice, and one skin must enter once.
2857                    if !region_skins
2858                        .iter()
2859                        .any(|other: &FaceFragmentRecord| {
2860                            other.test_point.sub(fragment.test_point).length() <= 1e-12
2861                        })
2862                    {
2863                        region_skins.push(fragment);
2864                    }
2865                }
2866            }
2867            // EVERY SKIN PIECE (`BREP_OFFSET_EVERY_SKIN_PIECE`, default on):
2868            // one source region's offset skin can come out in several pieces
2869            // when another offset cuts it (the drilled block's bore skin,
2870            // radius 4, runs into the wall skin x = 19 and parts it at
2871            // y = 10 ± √15/2) or the carrier's seam parts it (the bore skin
2872            // itself, at its seam). The seed answers one piece; every
2873            // classified fragment already reads on the skin, inside the source
2874            // and short of the openings, so all of them are the skin, as for
2875            // the extended walls below. Left out, the pieces the seed missed
2876            // left their outlines one-use (shell/hole-skin-merging-wall).
2877            // Only where the seed could not choose: an inward shell whose every
2878            // on-skin fragment touches the source (`viable` empty), so the
2879            // largest stood in for the region; an outward shell's fragments and
2880            // a seeded choice keep the seed's answer. And only a piece whose
2881            // test point lies the distance from its OWN trimmed face: "on the
2882            // skin" says no retained face is nearer, which a point inside the
2883            // offset body also reads (the cylinder's offset past a reflex
2884            // junction, inside the box's offset wall: 0.139 from the trimmed
2885            // cylinder against 0.1, reflex_junction_under_a_filleted_top).
2886            if every_skin_piece_enabled()
2887                && distance > 0.0
2888                && (viable.is_empty() || every_seeded_piece)
2889                && skin_filter_active
2890                && inward_extended_walls.is_empty()
2891                && classified.len() > region_skins.len()
2892            {
2893                for fragment in &classified {
2894                    if region_skins.iter().any(|other: &FaceFragmentRecord| other.test_point.sub(fragment.test_point).length() <= 1e-12) {
2895                        continue;
2896                    }
2897                    let departure = own_offset_departure(fragment.test_point, carrier.source_face_id, &retained_faces_with_edges, distance);
2898                    os_debug!("  select[{index}] every skin piece: test point ({:.4},{:.4},{:.4}) departs its own face's offset by {departure:?}", fragment.test_point.x, fragment.test_point.y, fragment.test_point.z);
2899                    if departure.is_some_and(|departure| departure.abs() <= offset_skin_tolerance) {
2900                        region_skins.push(fragment.clone());
2901                    }
2902                }
2903            }
2904            if !inward_extended_walls.is_empty() && skin_filter_active {
2905                // The extended opening cuts beyond the source footprint.
2906                // Other finite carriers can subdivide a connected offset
2907                // region there (the box/revolve report splits both box caps
2908                // at x=1). A single source seed keeps only one of those cells.
2909                // Keep every cell already proved inside the source and on
2910                // the offset skin, then merge their shared boundaries.
2911                region_skins = classified;
2912            }
2913            chosen_offsets.extend(region_skins);
2914        } else if body == Body::Whole {
2915            // A HELD face of the offset body: its carrier is the face at
2916            // distance zero, cut by the neighbouring offset skins; the body
2917            // keeps the region the face's own seed lies in, whole. There is
2918            // no source rim to stop at and no void in front of it.
2919            for (fragment_index, fragment) in fragments_by_carrier[index].iter().enumerate() {
2920                if seeds.iter().any(|seed| {
2921                    parameter_point_in_face(&fragment_as_trim(fragment), *seed, 1e-8)
2922                        .is_ok_and(|class| class != PolygonClass::Outside)
2923                }) {
2924                    os_debug!("  held[{index}.{fragment_index}] kept: contains the face's seed");
2925                    chosen_walls.push(fragment.clone());
2926                }
2927            }
2928        } else {
2929            // Keep arrangement order stable. Besides making the result
2930            // reproducible, this matches the reference kernel's Set insertion
2931            // order when the final manifold-wall guard admits fragments.
2932            let opening = source_by_id[&carrier.source_face_id];
2933            let wall_seeds = opening_wall_seeds(
2934                opening,
2935                &split_carriers[index].shells[0].faces[0],
2936                source,
2937                &opening_set,
2938                distance,
2939            )?;
2940            // The wall-seed exemptions below rest on "the coplanar-rim weld
2941            // cuts the void hole later" — which only ever happens on PLANAR
2942            // opening carriers. A curved carrier (e.g. the sphere face used
2943            // directly as the opening) keeps its un-split fragment forever:
2944            // exempting it would seal the opening with the source face itself
2945            // and fabricate a closed hollow solid. Curved carriers therefore
2946            // get the strict void guard.
2947            let carrier_planar =
2948                surface_is_planar(&split_carriers[index].shells[0].faces[0].surface, tolerance)
2949                    .unwrap_or(false);
2950            let extended_inward = inward_extended_walls.contains(&index);
2951            let outside_source = |point| -> Result<bool, KernelRefusal> {
2952                Ok(extended_inward
2953                    && classify_point(point, source, tolerance * 10.0)
2954                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::Out)
2955            };
2956            let wall_on_skin = |point| {
2957                if extended_inward {
2958                    let inside_source = classify_point(point, source, tolerance * 10.0)
2959                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::In;
2960                    point_on_inward_opening_skin(
2961                        point,
2962                        &retained_faces_with_edges,
2963                        &reflex_wall_neighbors[&index],
2964                        inside_source,
2965                        distance,
2966                        offset_skin_tolerance,
2967                    )
2968                } else {
2969                    point_on_offset_skin(
2970                        point,
2971                        &retained_faces_with_edges,
2972                        distance,
2973                        offset_skin_tolerance,
2974                    )
2975                }
2976            };
2977            let mut selected_indices = Vec::new();
2978            for (fragment_index, fragment) in fragments_by_carrier[index].iter().enumerate() {
2979                if outside_source(fragment.test_point)? {
2980                    continue;
2981                }
2982                if parameter_point_in_face(&fragment_as_trim(fragment), seed, 1e-8).or_refuse(KernelStage::Sew, "parameter_point_in_face")?
2983                    == PolygonClass::Outside
2984                {
2985                    if in_front_of_other_opening(index, fragment.test_point)? {
2986                        os_debug!(
2987                            "  wall[{index}.{fragment_index}] skipped: in front of another \
2988                             opening at ({:.3},{:.3},{:.3})",
2989                            fragment.test_point.x,
2990                            fragment.test_point.y,
2991                            fragment.test_point.z,
2992                        );
2993                        continue;
2994                    }
2995                    // Wall = the material cross-section exposed at the
2996                    // opening: every point of a genuine wall fragment lies
2997                    // WITHIN the offset distance of some retained face (it is
2998                    // the thickness between that face and its offset skin). A
2999                    // fragment at ≥ distance from EVERY retained face — and
3000                    // containing no expected wall seed (an un-split cap
3001                    // fragment can span both the void AND the wall annulus;
3002                    // the coplanar-rim weld cuts its hole later) — is the
3003                    // core's own cross-section: a HOLE in the opening (e.g.
3004                    // the corner void where a carve's offset circle passes
3005                    // just inside the opening's corner). Welding it shut
3006                    // would fabricate a wall over the void.
3007                    if wall_on_skin(fragment.test_point)? && !(carrier_planar
3008                        && !extended_inward
3009                        && wall_seeds.iter().any(|wall_seed| {
3010                            parameter_point_in_face(&fragment_as_trim(fragment), *wall_seed, 1e-8)
3011                                .is_ok_and(|class| class != PolygonClass::Outside)
3012                        }))
3013                    {
3014                        os_debug!(
3015                            "  wall[{index}.{fragment_index}] skipped: void cross-section at \
3016                             ({:.3},{:.3},{:.3})",
3017                            fragment.test_point.x,
3018                            fragment.test_point.y,
3019                            fragment.test_point.z,
3020                        );
3021                        continue;
3022                    }
3023                    // A SURPLUS fragment beyond a sharp convex corner: it
3024                    // touches the wall's own outline, no edge of it was cut by
3025                    // the SOURCE (the rim every genuine wall region borders),
3026                    // and it holds no wall seed. The void test above reads it
3027                    // as material, because it measures a planar neighbour in the
3028                    // MITER metric (its extended plane, 0.277 from this region
3029                    // on the free-form D turning 83.7° at d = 0.3). The
3030                    // opening wall's clearance cuts such a region off between
3031                    // the carriers' tails past the mitre, and kept, it
3032                    // assembled as "non-integral genus" (8 of the D band's 42
3033                    // cells, 2026-09-26).
3034                    if distance < 0.0
3035                        && wall_surplus_beyond_the_sections(fragment, index as u8, |piece_id| {
3036                            imprint.pieces.iter().find(|piece| piece.id == piece_id).is_some_and(|piece| {
3037                                piece.support_faces.iter().any(|key| key.operand == SOURCE_OPERAND)
3038                            })
3039                        })
3040                        && !wall_seeds.iter().any(|wall_seed| {
3041                            parameter_point_in_face(&fragment_as_trim(fragment), *wall_seed, 1e-8)
3042                                .is_ok_and(|class| class != PolygonClass::Outside)
3043                        })
3044                    {
3045                        os_debug!(
3046                            "  wall[{index}.{fragment_index}] skipped: surplus beyond the sections at \
3047                             ({:.3},{:.3},{:.3}), touching the wall outline and no source rim",
3048                            fragment.test_point.x,
3049                            fragment.test_point.y,
3050                            fragment.test_point.z,
3051                        );
3052                        continue;
3053                    }
3054                    selected_indices.push(fragment_index);
3055                }
3056            }
3057            for wall_seed in wall_seeds {
3058                let fragments = &fragments_by_carrier[index];
3059                let nearest_containing = fragments
3060                    .iter()
3061                    .enumerate()
3062                    .filter(|(_, fragment)| {
3063                        parameter_point_in_face(&fragment_as_trim(fragment), wall_seed, 1e-8)
3064                            .is_ok_and(|class| class != PolygonClass::Outside)
3065                    })
3066                    .min_by(|(_, first), (_, second)| {
3067                        first
3068                            .test_uv
3069                            .sub(wall_seed)
3070                            .length()
3071                            .total_cmp(&second.test_uv.sub(wall_seed).length())
3072                    })
3073                    .map(|(fragment_index, _)| fragment_index);
3074                // Arrangement fitting can leave the seed just outside every
3075                // fragment by a small amount. Match the reference kernel's
3076                // geometric fallback instead of silently omitting the wall
3077                // fragment and leaving the assembled shell open.
3078                let nearest = if nearest_containing.is_some() {
3079                    nearest_containing
3080                } else {
3081                    let seed_point = split_carriers[index].shells[0].faces[0]
3082                        .surface
3083                        .evaluate(wall_seed.x, wall_seed.y).or_refuse(KernelStage::Sew, "evaluate")?;
3084                    fragments
3085                        .iter()
3086                        .enumerate()
3087                        .min_by(|(_, first), (_, second)| {
3088                            first
3089                                .test_point
3090                                .sub(seed_point)
3091                                .length()
3092                                .total_cmp(&second.test_point.sub(seed_point).length())
3093                        })
3094                        .map(|(fragment_index, _)| fragment_index)
3095                };
3096                if let Some(fragment_index) = nearest {
3097                    let candidate = &fragments_by_carrier[index][fragment_index];
3098                    if outside_source(candidate.test_point)?
3099                        || in_front_of_other_opening(index, candidate.test_point)?
3100                    {
3101                        continue;
3102                    }
3103                    // Void-cross-section guard. On a PLANAR carrier it applies
3104                    // to the GEOMETRIC FALLBACK only: a fragment that
3105                    // genuinely contains this wall seed is wall content by
3106                    // construction (even when un-split it also spans void —
3107                    // the rim weld cuts the hole later), but a merely-nearest
3108                    // fragment must not resurrect a hole in the opening. On a
3109                    // CURVED carrier no later weld cuts the hole, so even a
3110                    // seed-containing fragment gets the strict guard.
3111                    // Extended inward walls are cut by the source as well:
3112                    // a seed must not resurrect the opposite side of a
3113                    // reflex rim merely because its unsigned distance is small.
3114                    if (nearest_containing.is_none() || !carrier_planar || extended_inward)
3115                        && wall_on_skin(candidate.test_point)?
3116                    {
3117                        os_debug!(
3118                            "  wall[{index}.{fragment_index}] seed-hit skipped: void \
3119                             cross-section (planar={carrier_planar}) at ({:.3},{:.3},{:.3})",
3120                            candidate.test_point.x,
3121                            candidate.test_point.y,
3122                            candidate.test_point.z,
3123                        );
3124                        continue;
3125                    }
3126                    let existing = chosen_offsets.iter().cloned().chain(
3127                        selected_indices
3128                            .iter()
3129                            .map(|selected| fragments_by_carrier[index][*selected].clone()),
3130                    );
3131                    if !selected_indices.contains(&fragment_index)
3132                        && !would_overuse_existing_boundary(
3133                            candidate,
3134                            existing,
3135                            &assembly_sources,
3136                            &imprint,
3137                            2e-3,
3138                        )?
3139                    {
3140                        selected_indices.push(fragment_index);
3141                    }
3142                }
3143            }
3144            for fragment_index in selected_indices.iter().copied() {
3145                let mut fragment = fragments_by_carrier[index][fragment_index].clone();
3146                // Past a reflex rim the wall closes the cavity on the other
3147                // side of the opening plane, so its normal reverses too.
3148                if extended_inward
3149                    && classify_point(fragment.test_point, source, tolerance * 10.0)
3150                        .or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::In
3151                {
3152                    flip_fragment(&mut fragment)?;
3153                }
3154                chosen_walls.push(fragment);
3155            }
3156        }
3157    }
3158    if debug_enabled() {
3159        for fragment in &chosen_offsets {
3160            os_debug!(
3161                "chosen offset: operand={} src={} test=({:.3},{:.3},{:.3})",
3162                fragment.operand,
3163                carriers[fragment.operand as usize].source_face_id,
3164                fragment.test_point.x,
3165                fragment.test_point.y,
3166                fragment.test_point.z,
3167            );
3168            for (loop_index, loop_record) in fragment.loops.iter().enumerate() {
3169                for coedge in &loop_record.coedges {
3170                    let surface = &fragment.surface;
3171                    let [c0, c1] = coedge.pcurve.domain().unwrap_or([0.0, 0.0]);
3172                    let uv0 = coedge.pcurve.evaluate(c0);
3173                    let uv1 = coedge.pcurve.evaluate(c1);
3174                    if let (Ok(uv0), Ok(uv1)) = (uv0, uv1) {
3175                        let p0 = surface.evaluate(uv0.x, uv0.y);
3176                        let p1 = surface.evaluate(uv1.x, uv1.y);
3177                        if let (Ok(p0), Ok(p1)) = (p0, p1) {
3178                            let kind = match &coedge.source {
3179                                FragmentEdgeSource::Boundary { edge_id, .. } => {
3180                                    format!("bnd:{edge_id}")
3181                                }
3182                                FragmentEdgeSource::SharedBoundary { edge_id, .. } => {
3183                                    format!("sbnd:{edge_id}")
3184                                }
3185                                FragmentEdgeSource::Imprint { piece_id } => {
3186                                    format!("imp:{piece_id}")
3187                                }
3188                                FragmentEdgeSource::Derived { .. } => "derived".to_string(),
3189                            };
3190                            os_debug!(
3191                                "    loop{loop_index} {kind} ({:.4},{:.4},{:.4})..({:.4},{:.4},{:.4})",
3192                                p0.x, p0.y, p0.z, p1.x, p1.y, p1.z,
3193                            );
3194                        }
3195                    }
3196                }
3197            }
3198        }
3199        for fragment in &chosen_walls {
3200            os_debug!(
3201                "chosen wall: operand={} src={} test=({:.3},{:.3},{:.3})",
3202                fragment.operand,
3203                carriers[fragment.operand as usize].source_face_id,
3204                fragment.test_point.x,
3205                fragment.test_point.y,
3206                fragment.test_point.z,
3207            );
3208        }
3209    }
3210    if !newly_shadowed.is_empty() {
3211        let mut omitted = shadowed_supports.clone();
3212        omitted.extend(newly_shadowed.iter().copied());
3213        os_debug!(
3214            "re-running without shadowed offset supports {:?} (omitted before: {:?})",
3215            newly_shadowed,
3216            shadowed_supports
3217        );
3218        // What this pass sampled belongs to carriers the next pass may not
3219        // build.
3220        shadowed_points.clear();
3221        profile::lap(&mut clock, profile::SELECT);
3222        let cascaded = !shadowed_supports.is_empty();
3223        let rebuilt = offset_shell_impl(
3224            source,
3225            opening_face_ids,
3226            distance,
3227            body,
3228            extend_oblique_carriers,
3229            extension_fired,
3230            reflex_rebuilds,
3231            &omitted,
3232            cavity_bounds,
3233            every_seeded_piece,
3234            shadowed_points,
3235            omitted_supports,
3236            vacated_notes,
3237        );
3238        let rebuilt = rebuilt?;
3239        if cascaded {
3240            refuse_a_cascade_that_kept_the_cavity(source, opening_face_ids, distance, &rebuilt.solid)?;
3241        }
3242        return Ok(rebuilt);
3243    }
3244    if chosen_offsets.is_empty() && !shadowed_supports.is_empty() {
3245        if matches!(body, Body::Shell) {
3246            if let Some(witness) = cavity_witness(source, opening_face_ids, distance)? {
3247                if !witness.bounds.is_subset(cavity_bounds) {
3248                    let mut bounds = cavity_bounds.clone();
3249                    bounds.extend(witness.bounds.iter().copied());
3250                    os_debug!(
3251                        "every skin read shadowed, but ({:.4}, {:.4}, {:.4}) lies {:.6} from every retained face: re-running with faces {:?} kept",
3252                        witness.point.x, witness.point.y, witness.point.z, witness.clearance, bounds
3253                    );
3254                    shadowed_points.clear();
3255                    profile::lap(&mut clock, profile::SELECT);
3256                    return offset_shell_impl(
3257                        source,
3258                        opening_face_ids,
3259                        distance,
3260                        body,
3261                        extend_oblique_carriers,
3262                        extension_fired,
3263                        reflex_rebuilds,
3264                        &HashSet::default(),
3265                        &bounds,
3266                        every_seeded_piece,
3267                        shadowed_points,
3268                        omitted_supports,
3269                        vacated_notes,
3270                    );
3271                }
3272                return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_cavity_unbuilt", format!(
3273                    "offset_shell: a cavity remains at this distance (the point ({:.4}, {:.4}, {:.4}) is {:.4} from \
3274                     every retained face, past the shell distance {distance}), but every offset skin around it \
3275                     reads as covered by another face, so it could not be built; returning the solid unchanged \
3276                     would be wrong — use a smaller shell distance",
3277                    witness.point.x, witness.point.y, witness.point.z, witness.clearance
3278                )));
3279            }
3280            if let Some(unchanged) = empty_cavity_shell(source, opening_face_ids, distance)? {
3281                return Ok(unchanged);
3282            }
3283        }
3284        let mut omitted = shadowed_supports.iter().copied().collect::<Vec<_>>();
3285        omitted.sort_unstable();
3286        return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_all_shadowed", format!(
3287            "offset_shell: no retained face keeps any offset skin once the shadowed offsets of \
3288             source faces {omitted:?} are omitted — the solid is nowhere thicker than twice the \
3289             shell distance, so there is no cavity to shell"
3290        )));
3291    }
3292    if chosen_offsets.is_empty() {
3293        // NO SKIN, NONE SHADOWED (`BREP_OFFSET_SHELL_EMPTY_CAVITY`, default on; `=0` restores): every offset
3294        // skin lies wholly outside the solid, so none was selected, though no support read
3295        // shadowed (a cone r 3 h 4 open at its base at wall 2.5: its offset cone's own apex falls
3296        // below the opening plane). That is the empty cavity the shadowed branch above answers
3297        // with the solid unchanged; it is answered the same way, on the same two readings: no
3298        // witness of a cavity, and no point of the solid farther than the wall from every
3299        // retained face.
3300        if matches!(body, Body::Shell) && empty_cavity_any_enabled() && cavity_witness(source, opening_face_ids, distance)?.is_none() {
3301            if let Some(unchanged) = empty_cavity_shell(source, opening_face_ids, distance)? {
3302                os_debug!("no offset skin was selected and no cavity remains: the solid is its own shell");
3303                return Ok(unchanged);
3304            }
3305        }
3306        return Err(KernelRefusal::internal(KernelStage::Select, "offset_shell_no_boundary", "offset_shell: carrier intersections produced no offset boundary"));
3307    }
3308    // Opening-wall fragments can overlap at dense carrier junctions. The
3309    // seed-time guard above handles geometric containment; this final pass
3310    // mirrors the topology-level rule and counts exact fragment edge sources.
3311    let mut boundary_use_count = HashMap::<[i64; 9], usize>::default();
3312    for fragment in &chosen_offsets {
3313        for coedge in fragment
3314            .loops
3315            .iter()
3316            .flat_map(|loop_record| &loop_record.coedges)
3317        {
3318            let key = fragment_edge_segment_key(&coedge.source, &assembly_sources, &imprint)?;
3319            *boundary_use_count.entry(key).or_default() += 1;
3320        }
3321    }
3322    let mut manifold_walls = Vec::new();
3323    for fragment in chosen_walls {
3324        let keys = fragment
3325            .loops
3326            .iter()
3327            .flat_map(|loop_record| &loop_record.coedges)
3328            .map(|coedge| fragment_edge_segment_key(&coedge.source, &assembly_sources, &imprint))
3329            .collect::<Result<Vec<_>, _>>()?;
3330        if !keys
3331            .iter()
3332            .any(|key| boundary_use_count.get(key).copied().unwrap_or(0) >= 2)
3333        {
3334            for key in keys {
3335                *boundary_use_count.entry(key).or_default() += 1;
3336            }
3337            manifold_walls.push(fragment);
3338        }
3339    }
3340    os_debug!(
3341        "manifold walls kept: {} operands={:?}",
3342        manifold_walls.len(),
3343        manifold_walls
3344            .iter()
3345            .map(|fragment| fragment.operand)
3346            .collect::<Vec<_>>(),
3347    );
3348    let mut chosen_walls = manifold_walls;
3349    // A part of the selected cavity skin with no path to an opening wall is a
3350    // SEALED hollow; named only if the assembly then refuses.
3351    let sealed = if sealed_hollow_on() && body == Body::Shell {
3352        sealed_skins(&chosen_offsets, &chosen_walls, &assembly_sources, &imprint, |fragment| {
3353            carriers[fragment.operand as usize].source_face_id
3354        })?
3355    } else {
3356        Vec::new()
3357    };
3358    // EVERY SKIN SEALED (`BREP_OFFSET_SHELL_SEALED_OPENINGS_WHOLE`, default on; `=0` restores): no selected
3359    // skin reaches an opening, so the cavity is a closed void below them and each opening face
3360    // stays as it is. Its wall fragments were cut by the skins' carriers PAST the cavity's own
3361    // end (the 8 x 8 pyramid cut at 4, shelled open at its top at 1.9: the sides' offsets cross
3362    // at z 3.75 and their mirrored continuations cut the opening in a square 0.25 across, which
3363    // the wall selection dropped as the cavity's mouth and nothing filled). The opening faces
3364    // are kept whole from the source instead of as wall fragments.
3365    // A skin with an edge cut by an opening's wall carrier reaches that opening, whatever the
3366    // fragment connectivity reads (a pinched lemon shelled through both caps reads sealed by it).
3367    let reaches_an_opening = || {
3368        chosen_offsets.iter().flat_map(|fragment| fragment.loops.iter().flat_map(|loop_record| &loop_record.coedges)).any(|coedge| {
3369            let FragmentEdgeSource::Imprint { piece_id } = &coedge.source else { return false };
3370            imprint.pieces.iter().find(|piece| piece.id == *piece_id).is_some_and(|piece| {
3371                piece.support_faces.iter().any(|key| carriers.get(key.operand as usize).is_some_and(|carrier| carrier.kind == OffsetFaceRole::Wall))
3372            })
3373        })
3374    };
3375    // ... or whose edge lies on an opening face (a carrier reaching its opening plane meets it
3376    // along its own boundary, not an imprint).
3377    let touches_an_opening = || -> Result<bool, KernelRefusal> {
3378        let openings = source.shells.iter().flat_map(|shell| &shell.faces).filter(|face| opening_face_ids.contains(&face.id)).collect::<Vec<_>>();
3379        let band = 1e-7 * crate::solid_scale(source).max(1.0);
3380        for (_, middle) in sealed.iter().flat_map(|skin| &skin.edge_middles) {
3381            for face in &openings {
3382                let Ok(foot) = crate::project_point_to_surface(&face.surface, *middle) else { continue };
3383                if foot.distance <= band
3384                    && parameter_point_in_face(face, Vec2 { x: foot.u, y: foot.v }, 1e-9).or_refuse(KernelStage::Select, "parameter_point_in_face")? != PolygonClass::Outside
3385                {
3386                    return Ok(true);
3387                }
3388            }
3389        }
3390        Ok(false)
3391    };
3392    let all_sealed = sealed_openings_whole_on()
3393        && !chosen_offsets.is_empty()
3394        && sealed.iter().map(|skin| skin.fragments).sum::<usize>() == chosen_offsets.len()
3395        && !reaches_an_opening()
3396        && !touches_an_opening()?;
3397    if all_sealed {
3398        os_debug!("every selected skin is sealed: the openings stay whole");
3399        chosen_walls.clear();
3400    }
3401    if !sealed.is_empty() {
3402        os_debug!(
3403            "sealed skins: {:?}",
3404            sealed.iter().map(|skin| (&skin.sources, skin.centre, skin.edge_middles.len())).collect::<Vec<_>>()
3405        );
3406    }
3407
3408    profile::lap(&mut clock, profile::SELECT);
3409    let stage_clock = web_time::Instant::now();
3410    // The offset BODY has no source face in it: it is the skins (and the held
3411    // faces) alone, oriented as the source is, so nothing is flipped either.
3412    let source_fragments = if body == Body::Whole {
3413        Vec::new()
3414    } else {
3415        fragment_solid(source, SOURCE_OPERAND, &empty_imprint())?
3416    };
3417    os_debug!("stage: source fragments {:.3}s", stage_clock.elapsed().as_secs_f64());
3418    let mut selected = source_fragments
3419        .into_iter()
3420        .filter(|fragment| retained.contains(&fragment.source_face_id) || (all_sealed && opening_face_ids.contains(&fragment.source_face_id)))
3421        .collect::<Vec<_>>();
3422    let mut face_images = selected
3423        .iter()
3424        .map(|fragment| OffsetShellFaceImageRecord {
3425            role: OffsetFaceRole::Source,
3426            source_face_id: fragment.source_face_id,
3427        })
3428        .collect::<Vec<_>>();
3429    if distance < 0.0 {
3430        for fragment in &mut selected {
3431            flip_fragment(fragment)?;
3432        }
3433    }
3434    if distance > 0.0 && body == Body::Shell {
3435        for fragment in &mut chosen_offsets {
3436            flip_fragment(fragment)?;
3437        }
3438    }
3439    face_images.extend(
3440        chosen_offsets
3441            .iter()
3442            .map(|fragment| OffsetShellFaceImageRecord {
3443                role: OffsetFaceRole::Offset,
3444                source_face_id: carriers[fragment.operand as usize].source_face_id,
3445            }),
3446    );
3447    face_images.extend(
3448        chosen_walls
3449            .iter()
3450            .map(|fragment| OffsetShellFaceImageRecord {
3451                role: OffsetFaceRole::Wall,
3452                source_face_id: carriers[fragment.operand as usize].source_face_id,
3453            }),
3454    );
3455    selected.extend(chosen_offsets);
3456    selected.extend(chosen_walls);
3457    // `BREP_OFFSET_THIN_SKIN_PAD`: the same fixed 2e-3 floor as the skin band
3458    // welded the 1e-3 rim of box-paper-thin into points (every rim edge
3459    // degenerate, the rim eight one-face shells, the volume short by the
3460    // rim's 0.072); a thin inward shell's weld is held to a quarter of it.
3461    let mut assembly_weld = 2e-3f64.max(scale * 5e-5);
3462    if thin_skin_pad_enabled() && distance > 0.0 {
3463        assembly_weld = assembly_weld.min(0.25 * distance.abs());
3464    }
3465    let mut solid = profile::detail("assemble_open_fragments", || assemble_open_fragments(
3466        selected,
3467        &assembly_sources,
3468        &imprint,
3469        assembly_weld,
3470    ))?;
3471    os_debug!("stage: assembled open fragments {:.3}s", stage_clock.elapsed().as_secs_f64());
3472    profile::detail("orient_open_solid_faces", || orient_open_solid_faces(&mut solid))?;
3473    os_debug!("stage: oriented {:.3}s", stage_clock.elapsed().as_secs_f64());
3474    let connector_images = profile::detail("complete_sharp_offset_connectors", || complete_sharp_offset_connectors(
3475        &mut solid,
3476        source,
3477        &face_images,
3478        distance,
3479        2e-3f64.max(scale * 5e-5),
3480    ))?;
3481    os_debug!(
3482        "sharp connector completion added {} faces ({:.3}s)",
3483        connector_images.len(),
3484        stage_clock.elapsed().as_secs_f64()
3485    );
3486    if !connector_images.is_empty() {
3487        // Wall fragments were arranged before the sharp seam was closed, so
3488        // their prospective inner boundary was an open chain. Rebuild the
3489        // wall from the now-closed source/offset cycles instead of retaining
3490        // that pre-connector fragment.
3491        for shell in &mut solid.shells {
3492            shell.faces.retain(|face| {
3493                !matches!(
3494                    face_images
3495                        .get(face.id.saturating_sub(1) as usize)
3496                        .map(|image| image.role),
3497                    Some(OffsetFaceRole::Wall)
3498                )
3499            });
3500        }
3501        solid.shells.retain(|shell| !shell.faces.is_empty());
3502    }
3503    face_images.extend(connector_images);
3504    profile::detail("orient_open_solid_faces", || orient_open_solid_faces(&mut solid))?;
3505    // Weld curved-face offset rims that the wall fragmentation left orphaned
3506    // into their coplanar opening cap (cylinder / straight-hole shells). This
3507    // is the seam-rim consistency step the offset pipeline previously lacked.
3508    let welded_rims = profile::detail("weld_coplanar_orphan_rims", || weld_coplanar_orphan_rims(&mut solid, 2e-3f64.max(scale * 5e-5)))?;
3509    os_debug!("welded {welded_rims} coplanar orphan rims into opening caps");
3510    // A rim that is not a circle: a free-form or non-iso rim whose offset image
3511    // falls short of the opening. It runs after every earlier weld because it
3512    // only ever sees rims none of them closed.
3513    let rim_band = profile::detail("weld_free_form_rim_bands", || weld_free_form_rim_bands(
3514        &mut solid,
3515        &mut face_images,
3516        &carriers,
3517        source,
3518        2e-3f64.max(scale * 5e-5),
3519    ))?;
3520    let rim_bands = rim_band.welded;
3521    os_debug!("welded {rim_bands} free-form rim bands");
3522    // Weld leftover coaxial coplanar rim PAIRS (a through-hole's exposed wall
3523    // thickness at each opening) into new planar annulus faces.
3524    let pair_rims =
3525        profile::detail("weld_coplanar_rim_pair_annuli", || weld_coplanar_rim_pair_annuli(&mut solid, &mut face_images, 2e-3f64.max(scale * 5e-5)))?;
3526    os_debug!("welded {pair_rims} coplanar rim pairs into annulus walls");
3527    if debug_enabled() {
3528        for (shell_index, shell) in solid.shells.iter().enumerate() {
3529            for face in &shell.faces {
3530                os_debug!(
3531                    "POSTWELD shell {} face {} same_sense={} loops={}",
3532                    shell_index,
3533                    face.id,
3534                    face.same_sense,
3535                    face.loops.len()
3536                );
3537                for (loop_index, loop_record) in face.loops.iter().enumerate() {
3538                    let walk = loop_record
3539                        .coedges
3540                        .iter()
3541                        .map(|coedge| {
3542                            let spin = (|| -> Result<f64, KernelRefusal> {
3543                                let [d0, d1] = coedge.pcurve.domain().or_refuse(KernelStage::Sew, "domain")?;
3544                                let a = coedge.pcurve.evaluate(d0 + (d1 - d0) * 0.45).or_refuse(KernelStage::Sew, "evaluate")?;
3545                                let b = coedge.pcurve.evaluate(d0 + (d1 - d0) * 0.55).or_refuse(KernelStage::Sew, "evaluate")?;
3546                                let pa = face.surface.evaluate(a.x, a.y).or_refuse(KernelStage::Sew, "evaluate")?;
3547                                let pb = face.surface.evaluate(b.x, b.y).or_refuse(KernelStage::Sew, "evaluate")?;
3548                                Ok(pa.cross(pb).z)
3549                            })()
3550                            .unwrap_or(f64::NAN);
3551                            format!(
3552                                "{}{}(spin{:+.0})",
3553                                if coedge.forward { "+" } else { "-" },
3554                                coedge.edge_id,
3555                                spin.signum()
3556                            )
3557                        })
3558                        .collect::<Vec<_>>();
3559                    os_debug!("    loop {} {:?}", loop_index, walk);
3560                }
3561            }
3562        }
3563    }
3564    let completion_carriers = carriers.iter().collect::<Vec<_>>();
3565    let completion_tolerance = 2e-3f64.max(scale * 5e-5);
3566    // Unify geometric duplicate arc records BEFORE completion: a ring that is
3567    // already covered by both its faces (frustum + apex cap) must not read as
3568    // an open boundary, or completion papers over it with duplicate faces.
3569    let duplicate_welds = profile::detail("weld_duplicate_one_use_arcs", || weld_duplicate_one_use_arcs(&mut solid, completion_tolerance))?;
3570    os_debug!("welded {duplicate_welds} duplicate one-use boundary arcs");
3571    if debug_enabled() {
3572        let mut use_counts = HashMap::<u64, usize>::default();
3573        for coedge in solid
3574            .shells
3575            .iter()
3576            .flat_map(|shell| &shell.faces)
3577            .flat_map(|face| &face.loops)
3578            .flat_map(|loop_record| &loop_record.coedges)
3579        {
3580            *use_counts.entry(coedge.edge_id).or_default() += 1;
3581        }
3582        let open = use_counts.values().filter(|count| **count == 1).count();
3583        os_debug!(
3584            "assembled: faces={} open_edges={open}",
3585            solid
3586                .shells
3587                .iter()
3588                .map(|shell| shell.faces.len())
3589                .sum::<usize>()
3590        );
3591    }
3592    let completed_images = profile::detail("complete_opening_boundary_cycles", || complete_opening_boundary_cycles(
3593        &mut solid,
3594        &completion_carriers,
3595        &face_images,
3596        completion_tolerance,
3597    ))?;
3598    os_debug!("completion added {} faces", completed_images.len());
3599    face_images.extend(completed_images);
3600    let solid = profile::detail("merge_same_surface_faces_open", || merge_same_surface_faces_open(&solid, (1e-7f64).max(scale * 1e-9)))?;
3601    let assembly_tolerance = 2e-3f64.max(scale * 5e-5);
3602    // No faceted fallback here: an offset shell must be made of real analytic
3603    // surfaces. Failing loudly beats silently returning a tessellated BREP
3604    // with destroyed face provenance.
3605    // A shell the rim band lane recognized but could not close fails here as it
3606    // always did; the refusal now carries the lane's reason.
3607    let finalized = profile::detail("finalize_assembled_solid", || finalize_assembled_solid(solid, assembly_tolerance)).map_err(|error| {
3608        match &rim_band.decline {
3609            Some(reason) => error.with_message(|message| format!("{message}; offset_shell: {reason}")),
3610            None => error,
3611        }
3612    });
3613    let mut solid = match finalized {
3614        Ok(solid) => solid,
3615        Err(error) => {
3616            os_debug!("finalize_assembled_solid: {}", error.message);
3617            if !every_seeded_piece && body == Body::Shell && distance > 0.0 && every_seeded_skin_piece_enabled() {
3618                let retried = offset_shell_impl(
3619                    source,
3620                    opening_face_ids,
3621                    distance,
3622                    body,
3623                    extend_oblique_carriers,
3624                    extension_fired,
3625                    reflex_rebuilds,
3626                    shadowed_supports,
3627                    cavity_bounds,
3628                    true,
3629                    shadowed_points,
3630                    omitted_supports,
3631                    vacated_notes,
3632                );
3633                if retried.is_ok() {
3634                    return retried;
3635                }
3636            }
3637            if let Some((index, own, other, thickness)) = sealing_distance(
3638                &sealed,
3639                source,
3640                &retained_faces_with_edges,
3641                distance,
3642                offset_skin_tolerance,
3643            )? {
3644                let skin = &sealed[index];
3645                let bounding = skin.sources.iter().map(u64::to_string).collect::<Vec<_>>().join(", ");
3646                return Err(KernelRefusal::unsupported(KernelStage::Sew, refusals::SEALED_HOLLOW, format!(
3647                    "offset_shell: at this distance the cavity splits — a hollow bounded by the offsets of \
3648                     source faces {bounding} near ({:.4}, {:.4}, {:.4}) is sealed off from the opening, and a \
3649                     sealed hollow could not be assembled here. It is closed off where the offsets of source \
3650                     faces {own} and {other} cross, which they do from {:.4} up ({thickness:.4} of material \
3651                     between them); below {:.4} it opens into the rest of the cavity, so use a shell distance \
3652                     below that",
3653                    skin.centre.x,
3654                    skin.centre.y,
3655                    skin.centre.z,
3656                    0.5 * thickness,
3657                    0.5 * thickness,
3658                )));
3659            }
3660            return Err(error);
3661        }
3662    };
3663    profile::detail("reconstruct_analytic_shell", || crate::offset_retrim::reconstruct_analytic_shell(&mut solid))?;
3664    if rim_bands + pair_rims > 0 || !inward_extended_walls.is_empty() {
3665        // Rim welds and extended reflex walls join independently-oriented skins;
3666        // put the whole finalized manifold on one normal convention so the
3667        // shell reports its exact wall volume (coplanar-only shells keep
3668        // their assembled sense).
3669        profile::detail("coheres_face_normals", || coheres_face_normals(&mut solid))?;
3670    }
3671    let solid = solid;
3672    profile::lap(&mut clock, profile::ASSEMBLY);
3673    face_images = solid
3674        .shells
3675        .iter()
3676        .flat_map(|shell| &shell.faces)
3677        .map(|face| {
3678            face_images
3679                .get(face.id.saturating_sub(1) as usize)
3680                .cloned()
3681                .ok_or_else(|| "offset_shell: merged face lost provenance".to_string())
3682        })
3683        .collect::<Result<Vec<_>, _>>().or_refuse(KernelStage::Sew, "offset_shell_merged_face_lost_provenance")?;
3684    // A bore's end ring carries the bore wall's provenance whichever
3685    // construction built it (the opening's fragmentation or the rim-pair
3686    // weld), so its name does not depend on the path the rims took.
3687    let bore_rings = claim_bore_end_rings(&solid, &mut face_images)?;
3688    os_debug!("{bore_rings} bore end rings took the bore wall's provenance");
3689    // Honesty gate: a real rim left one-use is a hole in the shell that the
3690    // degenerate marking hides from `validate()` (degenerate edges are allowed
3691    // to be face-local). Refuse such a result instead of shipping a
3692    // non-watertight solid as valid. A rim is a CLOSED edge whose interior
3693    // sweeps measurably away from its seam vertex — a genuine circle, not a
3694    // legitimate pole point placeholder (which stays one-use by design).
3695    let mut rim_use_counts = HashMap::<u64, usize>::default();
3696    for coedge in solid
3697        .shells
3698        .iter()
3699        .flat_map(|shell| &shell.faces)
3700        .flat_map(|face| &face.loops)
3701        .flat_map(|loop_record| &loop_record.coedges)
3702    {
3703        *rim_use_counts.entry(coedge.edge_id).or_default() += 1;
3704    }
3705    if debug_enabled() {
3706        for (shell_index, shell) in solid.shells.iter().enumerate() {
3707            for face in &shell.faces {
3708                let role = face_images
3709                    .get(face.id.saturating_sub(1) as usize)
3710                    .map(|image| image.role);
3711                let c = face.surface.evaluate(0.5, 0.5).unwrap_or_default();
3712                os_debug!(
3713                    "DUMPFACE shell {} face {} affine={} role={:?} loops={} center=({:.3},{:.3},{:.3})",
3714                    shell_index, face.id, face.surface.is_affine().unwrap_or(false), role,
3715                    face.loops.len(), c.x, c.y, c.z
3716                );
3717                for (li, lp) in face.loops.iter().enumerate() {
3718                    let ids = lp.coedges.iter().map(|c| c.edge_id).collect::<Vec<_>>();
3719                    os_debug!("    loop {} edges {:?}", li, ids);
3720                }
3721            }
3722        }
3723        for edge in &solid.edges {
3724            let uses = rim_use_counts.get(&edge.id).copied().unwrap_or(0);
3725            let s = edge.curve.evaluate(edge.t0).or_refuse(KernelStage::Sew, "evaluate")?;
3726            let m = edge.curve.evaluate((edge.t0 + edge.t1) * 0.5).or_refuse(KernelStage::Sew, "evaluate")?;
3727            let q = edge.curve.evaluate(edge.t0 + (edge.t1 - edge.t0) * 0.25).or_refuse(KernelStage::Sew, "evaluate")?;
3728            os_debug!(
3729                "DUMPEDGE edge {} uses={} closed={} deg={} cpts={} cdeg={} knots={:?} start=({:.3},{:.3},{:.3}) q=({:.3},{:.3},{:.3}) mid=({:.3},{:.3},{:.3})",
3730                edge.id, uses, edge.start_vertex_id == edge.end_vertex_id, edge.degenerate,
3731                edge.curve.control_points.len(), edge.curve.degree, edge.curve.knots,
3732                s.x, s.y, s.z, q.x, q.y, q.z, m.x, m.y, m.z
3733            );
3734        }
3735    }
3736    for edge in &solid.edges {
3737        if rim_use_counts.get(&edge.id).copied().unwrap_or(0) != 1
3738            || edge.start_vertex_id != edge.end_vertex_id
3739        {
3740            continue;
3741        }
3742        let anchor = edge.curve.evaluate(edge.t0).or_refuse(KernelStage::Sew, "evaluate")?;
3743        let sweep_threshold = 1e-4f64.max(scale * 1e-6);
3744        let sweeps = [0.25, 0.5, 0.75].into_iter().any(|fraction| {
3745            edge.curve
3746                .evaluate(edge.t0 + (edge.t1 - edge.t0) * fraction)
3747                .map(|point| point.sub(anchor).length() > sweep_threshold)
3748                .unwrap_or(false)
3749        });
3750        if sweeps {
3751            if debug_enabled() {
3752                let rim_mid = edge.curve.evaluate((edge.t0 + edge.t1) * 0.5).or_refuse(KernelStage::Sew, "evaluate")?;
3753                os_debug!(
3754                    "PROBE orphan rim edge {} anchor=({:.4},{:.4},{:.4}) mid=({:.4},{:.4},{:.4})",
3755                    edge.id,
3756                    anchor.x,
3757                    anchor.y,
3758                    anchor.z,
3759                    rim_mid.x,
3760                    rim_mid.y,
3761                    rim_mid.z
3762                );
3763                for (shell_index, shell) in solid.shells.iter().enumerate() {
3764                    for face in &shell.faces {
3765                        let uses_rim = face
3766                            .loops
3767                            .iter()
3768                            .flat_map(|l| &l.coedges)
3769                            .any(|c| c.edge_id == edge.id);
3770                        let affine = face.surface.is_affine().unwrap_or(false);
3771                        let on = edge_on_surface(edge, &face.surface, 2e-3f64.max(scale * 5e-5))
3772                            .unwrap_or(false);
3773                        let proj = project_point_to_surface(&face.surface, rim_mid).or_refuse(KernelStage::Sew, "project_point_to_surface")?;
3774                        let contains = if on {
3775                            parameter_point_in_face(
3776                                face,
3777                                Vec2 {
3778                                    x: proj.u,
3779                                    y: proj.v,
3780                                },
3781                                2e-3f64.max(scale * 5e-5),
3782                            ).or_refuse(KernelStage::Sew, "parameter_point_in_face")? != PolygonClass::Outside
3783                        } else {
3784                            false
3785                        };
3786                        if on || uses_rim {
3787                            os_debug!(
3788                                "  face {} shell {} affine={} uses_rim={} on_surf={} proj_dist={:.5} contains={}",
3789                                face.id, shell_index, affine, uses_rim, on, proj.distance, contains
3790                            );
3791                        }
3792                    }
3793                }
3794            }
3795            return Err(refusals::unwelded_curved_rim(edge.id, anchor).into());
3796        }
3797    }
3798    // The shell's single exit. An offset skin that crosses another wall, or
3799    // folds through itself where the source turned tighter than the distance,
3800    // leaves a body that validates and measures — so it is asked here, and
3801    // repaired or refused by name rather than returned.
3802    let mut solid = solid;
3803    // Face-local rim construction and planar merging can reuse wire IDs.
3804    // Reserve every existing ID, then replace only duplicates so the completed
3805    // body can cross the arena/buffer ingestion boundary. These IDs have no
3806    // external references; face/edge identities and provenance stay intact.
3807    let mut loop_ids = HashSet::default();
3808    let mut coedge_ids = HashSet::default();
3809    for face in solid.shells.iter().flat_map(|shell| &shell.faces) {
3810        for wire in &face.loops {
3811            loop_ids.insert(wire.id);
3812            coedge_ids.extend(wire.coedges.iter().map(|coedge| coedge.id));
3813        }
3814    }
3815    let mut seen_loops = HashSet::default();
3816    let mut seen_coedges = HashSet::default();
3817    let mut next_loop = 1;
3818    let mut next_coedge = 1;
3819    for face in solid.shells.iter_mut().flat_map(|shell| &mut shell.faces) {
3820        for wire in &mut face.loops {
3821            if !seen_loops.insert(wire.id) {
3822                while !loop_ids.insert(next_loop) { next_loop += 1; }
3823                wire.id = next_loop;
3824            }
3825            for coedge in &mut wire.coedges {
3826                if !seen_coedges.insert(coedge.id) {
3827                    while !coedge_ids.insert(next_coedge) { next_coedge += 1; }
3828                    coedge.id = next_coedge;
3829                }
3830            }
3831        }
3832    }
3833    let solid = profile::detail("accept_offset_result", || super::acceptance::accept_offset_result(solid))?;
3834    profile::lap(&mut clock, profile::ACCEPTANCE);
3835    Ok(OffsetShellResultRecord { solid, face_images })
3836}
3837
3838/// Run offset shell with stable counters and validation diagnostics suitable
3839/// for automated regressions and UI presentation.
3840pub fn offset_shell_with_diagnostics(
3841    source: &BrepSolid,
3842    opening_face_ids: &[u64],
3843    distance: f64,
3844    tolerances: Option<KernelTolerances>,
3845) -> Result<KernelOutcome<OffsetShellResultRecord>, KernelRefusal> {
3846    let policy = tolerances.unwrap_or_else(|| KernelTolerances::for_solid(source, 1e-7));
3847    policy.check().or_refuse(KernelStage::Sew, "check")?;
3848    let mut diagnostics = KernelDiagnostics::default();
3849    diagnostics.count_n(
3850        "collect.source_faces",
3851        source
3852            .shells
3853            .iter()
3854            .map(|shell| shell.faces.len() as u64)
3855            .sum(),
3856    );
3857    diagnostics.count_n("collect.opening_faces", opening_face_ids.len() as u64);
3858    diagnostics.measure_max("offset.distance", distance.abs());
3859    diagnostics.measure_max("tolerance.model", policy.model);
3860    // Tally every pcurve fit the shell makes (its own re-trims and any boolean
3861    // it runs) so `pcurve.unmet_floor` reports a trim that stopped at the
3862    // sample ceiling instead of passing it off as one that met the floor.
3863    let fit_scope = crate::PcurveFitScope::open();
3864    // Same for the polyline fits of its carrier×carrier sections, which is
3865    // where a marched rim's accuracy is decided (`geometry/fit.rs`).
3866    let polyline_scope = crate::PolylineFitScope::open();
3867    let result = offset_shell(source, opening_face_ids, distance)?;
3868    fit_scope.close().report_into(&mut diagnostics);
3869    polyline_scope.close().report_into(&mut diagnostics);
3870    // The phase split of the run that just finished (`profile.rs`), in the
3871    // boolean's `timing.<phase>_ms` shape so one reader serves both.
3872    let timings = profile::snapshot();
3873    diagnostics.count_n("shell.attempts", timings.attempts);
3874    for (phase, count) in &timings.carriers {
3875        diagnostics.count_n(format!("shell.{phase}"), *count);
3876    }
3877    for (phase, ms) in &timings.ms {
3878        diagnostics.measure_max(format!("timing.{phase}_ms"), *ms);
3879    }
3880    diagnostics.count_n(
3881        "sew.output_faces",
3882        result
3883            .solid
3884            .shells
3885            .iter()
3886            .map(|shell| shell.faces.len() as u64)
3887            .sum(),
3888    );
3889    diagnostics.count_n("sew.face_images", result.face_images.len() as u64);
3890    let validation = result.solid.validate_detailed(&policy);
3891    diagnostics.measure_max("validate.max_pcurve_error", validation.max_pcurve_error);
3892    diagnostics.count_n("validate.issues", validation.issues.len() as u64);
3893    diagnostics.count_n(
3894        "validate.wire_warnings",
3895        validation.wire_warnings.len() as u64,
3896    );
3897    for warning in validation.wire_warnings {
3898        diagnostics.event(
3899            DiagnosticSeverity::Warning,
3900            KernelStage::Validate,
3901            "validate.uv_wire",
3902            warning.message,
3903        );
3904    }
3905    if !validation.issues.is_empty() {
3906        for issue in &validation.issues {
3907            diagnostics.event(
3908                DiagnosticSeverity::Error,
3909                KernelStage::Validate,
3910                "validate.brep",
3911                issue.message.clone(),
3912            );
3913        }
3914        return Err(KernelRefusal::internal(KernelStage::Validate, "offset_shell_diagnostic_result", format!(
3915            "offset_shell: invalid diagnostic result: {:?}",
3916            validation.issues
3917        )));
3918    }
3919    Ok(KernelOutcome {
3920        value: result,
3921        diagnostics,
3922    })
3923}
3924
3925/// Whether a wall fragment lies wholly in the wall's surplus: it has an edge on
3926/// the wall carrier's own outline (`Boundary` of operand `wall_operand`) and no
3927/// edge imprinted by the SOURCE solid, whose rim every genuine opening-wall
3928/// region borders. `cut_by_source` answers whether an imprint piece has the
3929/// source among its two support faces.
3930pub(super) fn wall_surplus_beyond_the_sections(
3931    fragment: &FaceFragmentRecord,
3932    wall_operand: u8,
3933    cut_by_source: impl Fn(u64) -> bool,
3934) -> bool {
3935    let mut on_outline = false;
3936    for coedge in fragment.loops.iter().flat_map(|record| &record.coedges) {
3937        match &coedge.source {
3938            FragmentEdgeSource::Boundary { operand, .. } if *operand == wall_operand => on_outline = true,
3939            FragmentEdgeSource::Boundary { operand, .. } | FragmentEdgeSource::SharedBoundary { operand, .. }
3940                if *operand == SOURCE_OPERAND =>
3941            {
3942                return false;
3943            }
3944            FragmentEdgeSource::Imprint { piece_id } => {
3945                if cut_by_source(*piece_id) {
3946                    return false;
3947                }
3948            }
3949            _ => {}
3950        }
3951    }
3952    on_outline
3953}
3954
3955
3956/// `BREP_OFFSET_SHELL_CASCADE_OMISSION` (default on; `=0` restores): an inward
3957/// shell judges a carrier that reads shadowed at every fragment's test point
3958/// by its true skin instead ([`fragments_holding_true_skin`]). One test point
3959/// cannot speak for a fragment that is partly skin: a reported cylinder boss model's
3960/// root fillets bound the cavity's bulge toward the boss mouth at every
3961/// distance past the boss limit (checked with
3962/// `scripts/offset_cavity_volume.py`), and were dropped as shadowed. A carrier
3963/// holding no skin at all may then be omitted even after an earlier omission.
3964fn cascade_omission_enabled() -> bool {
3965    switch!("BREP_OFFSET_SHELL_CASCADE_OMISSION", true)
3966}
3967
3968/// A cascade of omissions must leave the deepest point of the true cavity
3969/// hollow: a carrier omitted on a test point that did not stand for its
3970/// fragment would show as material there.
3971fn refuse_a_cascade_that_kept_the_cavity(
3972    source: &BrepSolid,
3973    opening_face_ids: &[u64],
3974    distance: f64,
3975    result: &BrepSolid,
3976) -> Result<(), KernelRefusal> {
3977    let Some(witness) = cavity_witness(source, opening_face_ids, distance)? else {
3978        return Ok(());
3979    };
3980    let scale = crate::solid_scale(source).max(1.0);
3981    let class = classify_point(witness.point, result, 1e-8 * scale).or_refuse(KernelStage::Select, "classify_point")?.class;
3982    if class == PointClass::In {
3983        return Err(KernelRefusal::unsupported(KernelStage::Select, "offset_shell_shadow_cascade", format!(
3984            "offset_shell: omitting offsets that read shadowed only after earlier omissions leaves material at \
3985             ({:.4}, {:.4}, {:.4}), which is {:.4} from every retained face, past the shell distance {distance}; \
3986             the cavity there could not be built — use a smaller shell distance",
3987            witness.point.x, witness.point.y, witness.point.z, witness.clearance
3988        )));
3989    }
3990    Ok(())
3991}
3992
3993/// `BREP_OFFSET_SHELL_CAVITY_WITNESS` (default on; `=0` restores): [`cavity_witness`].
3994fn retained_faces_of<'a>(retained: &[(&'a FaceRecord, Vec<&'a EdgeRecord>)]) -> Vec<&'a FaceRecord> {
3995    retained.iter().map(|(face, _)| *face).collect()
3996}
3997
3998fn sealed_openings_whole_on() -> bool {
3999    switch!("BREP_OFFSET_SHELL_SEALED_OPENINGS_WHOLE", true)
4000}
4001
4002fn empty_cavity_any_enabled() -> bool {
4003    switch!("BREP_OFFSET_SHELL_EMPTY_CAVITY", true)
4004}
4005
4006fn cavity_witness_enabled() -> bool {
4007    switch!("BREP_OFFSET_SHELL_CAVITY_WITNESS", true)
4008}
4009
4010/// A point of the solid farther than the shell distance from every retained
4011/// face, and the faces nearest it, whose skins bound that cavity.
4012#[derive(Debug)]
4013pub(super) struct CavityWitness {
4014    pub(super) point: Vec3,
4015    pub(super) clearance: f64,
4016    pub(super) bounds: HashSet<u64>,
4017}
4018
4019/// Looks for the cavity the shadow reading says is not there. A fragment's
4020/// single test point can read shadowed while most of the fragment is real skin
4021/// (a reported cylinder boss model at 4.8: the r 0.2 bore of a cylinder r 5, one uncut
4022/// fragment whose test point lies 4.72 from a boss wall), and a cavity that
4023/// thin falls between the 12³ cells of [`empty_cavity_shell`], which then
4024/// returned the source as a success.
4025///
4026/// Distance to the retained faces is 1-Lipschitz, so any cavity's maximum is
4027/// climbed to from the samples that read highest: the best cell centres start
4028/// a pattern search that stays inside the solid. It is a search, not a proof;
4029/// what it cannot miss is a cavity whose ridge those starts climb.
4030pub(super) fn cavity_witness(
4031    source: &BrepSolid,
4032    opening_face_ids: &[u64],
4033    distance: f64,
4034) -> Result<Option<CavityWitness>, KernelRefusal> {
4035    if !cavity_witness_enabled() || distance <= 0.0 || opening_face_ids.is_empty() {
4036        return Ok(None);
4037    }
4038    let faces = source.shells.iter().flat_map(|shell| &shell.faces).collect::<Vec<_>>();
4039    let retained = faces.iter().filter(|face| !opening_face_ids.contains(&face.id)).copied().collect::<Vec<_>>();
4040    if retained.is_empty() {
4041        return Ok(None);
4042    }
4043    // Each face's own edges: a foot off the trim falls back on the edges it is
4044    // given, and the whole solid's would read a wall as near as any rim.
4045    let edge_by_id = source.edges.iter().map(|edge| (edge.id, edge)).collect::<HashMap<_, _>>();
4046    let retained = retained
4047        .into_iter()
4048        .map(|face| {
4049            let edges = face
4050                .loops
4051                .iter()
4052                .flat_map(|loop_record| &loop_record.coedges)
4053                .filter_map(|coedge| edge_by_id.get(&coedge.edge_id).copied())
4054                .collect::<Vec<_>>();
4055            (face, edges)
4056        })
4057        .collect::<Vec<_>>();
4058    let Some((low, high)) = control_hull(source, &faces) else { return Ok(None) };
4059    let scale = crate::solid_scale(source).max(1.0);
4060    let classify_tolerance = 1e-8 * scale;
4061    let margin = 1e-6 * scale;
4062    let readings = |point: Vec3| -> Result<Vec<(u64, f64)>, KernelRefusal> {
4063        retained
4064            .iter()
4065            .map(|(face, edges)| {
4066                // `BREP_OFFSET_RULED_MITER`: a ruled face reads the mitered metric the skin test
4067                // reads it by, or the witness finds a cavity past a rim the skins never reach.
4068                let ruled = if super::carriers::ruled_miter_on() { super::carriers::ruled_miter_distance(face, edges, point, &retained_faces_of(&retained))? } else { None };
4069                Ok((face.id, match ruled {
4070                    Some(mitered) => mitered,
4071                    None => distance_to_trimmed_face(face, edges, point)?,
4072                }))
4073            })
4074            .collect()
4075    };
4076    let clearance = |values: &[(u64, f64)]| values.iter().map(|(_, value)| *value).fold(f64::INFINITY, f64::min);
4077    let inside = |point: Vec3| -> Result<bool, KernelRefusal> {
4078        Ok(classify_point(point, source, classify_tolerance).or_refuse(KernelStage::Select, "classify_point")?.class == PointClass::In)
4079    };
4080    const CELLS: usize = 12;
4081    const STARTS: usize = 24;
4082    const MOVES: usize = 160;
4083    let span = high.sub(low);
4084    let mut starts = Vec::new();
4085    for i in 0..CELLS {
4086        for j in 0..CELLS {
4087            for k in 0..CELLS {
4088                let at = |index: usize| (index as f64 + 0.5) / CELLS as f64;
4089                let point = Vec3::new(low.x + span.x * at(i), low.y + span.y * at(j), low.z + span.z * at(k));
4090                if inside(point)? {
4091                    starts.push((clearance(&readings(point)?), point));
4092                }
4093            }
4094        }
4095    }
4096    starts.sort_by(|a, b| b.0.total_cmp(&a.0));
4097    starts.truncate(STARTS);
4098    const DIRECTIONS: [(f64, f64, f64); 14] = [
4099        (1.0, 0.0, 0.0), (-1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, -1.0, 0.0), (0.0, 0.0, 1.0), (0.0, 0.0, -1.0),
4100        (1.0, 1.0, 1.0), (-1.0, -1.0, -1.0), (1.0, 1.0, -1.0), (-1.0, -1.0, 1.0),
4101        (1.0, -1.0, 1.0), (-1.0, 1.0, -1.0), (-1.0, 1.0, 1.0), (1.0, -1.0, -1.0),
4102    ];
4103    let first_step = span.x.max(span.y).max(span.z) / CELLS as f64;
4104    let mut footless = None;
4105    for (start_clearance, start) in starts {
4106        let (mut point, mut best, mut step) = (start, start_clearance, first_step);
4107        for _ in 0..MOVES {
4108            if step < 1e-6 * scale {
4109                break;
4110            }
4111            let mut moved = false;
4112            for (x, y, z) in DIRECTIONS {
4113                let length = (x * x + y * y + z * z).sqrt();
4114                let next = Vec3::new(point.x + step * x / length, point.y + step * y / length, point.z + step * z / length);
4115                let value = clearance(&readings(next)?);
4116                if value > best && inside(next)? {
4117                    (point, best, moved) = (next, value, true);
4118                    break;
4119                }
4120            }
4121            if !moved {
4122                step *= 0.5;
4123            }
4124        }
4125        if best > distance + margin {
4126            // The climb ends on the ridge, where every face around it ties.
4127            // Nudged off the ridge, still inside the cavity, each point has one
4128            // nearest face, which counts if its foot lands inside its trim (a
4129            // tie through a face's rim is that edge, not its offset). The faces
4130            // that wrap the cavity collect the votes; a fillet seen only
4131            // through the mouth of a boss does not.
4132            let nudge = 0.5 * (best - distance);
4133            let mut votes = HashMap::<u64, usize>::default();
4134            for (x, y, z) in DIRECTIONS {
4135                let length = (x * x + y * y + z * z).sqrt();
4136                let near = Vec3::new(point.x + nudge * x / length, point.y + nudge * y / length, point.z + nudge * z / length);
4137                let values = readings(near)?;
4138                let Some((id, _)) = values.iter().min_by(|a, b| a.1.total_cmp(&b.1)).copied() else { continue };
4139                let Some((face, _)) = retained.iter().find(|(face, _)| face.id == id) else { continue };
4140                if foot_inside_trim(face, near)? {
4141                    *votes.entry(id).or_default() += 1;
4142                }
4143            }
4144            let most = votes.values().copied().max().unwrap_or(0);
4145            let bounds = votes.iter().filter(|(_, count)| **count * 2 >= most).map(|(id, _)| *id).collect::<HashSet<_>>();
4146            let witness = CavityWitness { point, clearance: best, bounds };
4147            if !witness.bounds.is_empty() {
4148                return Ok(Some(witness));
4149            }
4150            footless = Some(witness);
4151        }
4152    }
4153    Ok(footless)
4154}
4155
4156/// The box of the solid's vertices and face control points; `None` when a
4157/// control point cannot be read.
4158fn control_hull(source: &BrepSolid, faces: &[&FaceRecord]) -> Option<(Vec3, Vec3)> {
4159    let mut low = Vec3::new(f64::INFINITY, f64::INFINITY, f64::INFINITY);
4160    let mut high = Vec3::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
4161    for vertex in &source.vertices {
4162        low = Vec3::new(low.x.min(vertex.point.x), low.y.min(vertex.point.y), low.z.min(vertex.point.z));
4163        high = Vec3::new(high.x.max(vertex.point.x), high.y.max(vertex.point.y), high.z.max(vertex.point.z));
4164    }
4165    for control in faces.iter().flat_map(|face| face.surface.control_points.iter().flatten()) {
4166        let point = control.point().ok()?;
4167        low = Vec3::new(low.x.min(point.x), low.y.min(point.y), low.z.min(point.z));
4168        high = Vec3::new(high.x.max(point.x), high.y.max(point.y), high.z.max(point.z));
4169    }
4170    Some((low, high))
4171}
4172
4173/// `BREP_OFFSET_SHELL_EMPTY_CAVITY` (default on; `=0` restores): [`empty_cavity_shell`].
4174fn empty_cavity_enabled() -> bool {
4175    switch!("BREP_OFFSET_SHELL_EMPTY_CAVITY", true)
4176}
4177
4178
4179/// An INWARD shell whose every retained face's offset skin is shadowed: the
4180/// cavity, the points of the solid farther than `distance` from every retained
4181/// face, is empty, so the shell is the solid itself, every face its own
4182/// `Source` image (brep-bench shell/box-walls-meeting: the box 10 x 8 x 6
4183/// shelled by 4 open at its top, whose y walls' offsets meet in the plane
4184/// y = 4, was refused "no retained face keeps any offset skin ... there is no
4185/// cavity to shell"; the case reads the box, 480 and 376).
4186///
4187/// The shadow reading is checked before the solid is returned: on a 12³ grid
4188/// of cell centres over the solid's control hull, no point the solid
4189/// classifies Inside may lie farther than `distance` (plus 1e-9 of the
4190/// solid's size) from every retained face. A point that does means the
4191/// cavity is not empty, and the refusal stands.
4192fn empty_cavity_shell(
4193    source: &BrepSolid,
4194    opening_face_ids: &[u64],
4195    distance: f64,
4196) -> Result<Option<OffsetShellResultRecord>, KernelRefusal> {
4197    if !empty_cavity_enabled() || distance <= 0.0 || opening_face_ids.is_empty() {
4198        return Ok(None);
4199    }
4200    let faces = source.shells.iter().flat_map(|shell| &shell.faces).collect::<Vec<_>>();
4201    let retained = faces.iter().filter(|face| !opening_face_ids.contains(&face.id)).copied().collect::<Vec<_>>();
4202    if retained.is_empty() {
4203        return Ok(None);
4204    }
4205    let edges = source.edges.iter().collect::<Vec<_>>();
4206    let Some((low, high)) = control_hull(source, &faces) else { return Ok(None) };
4207    let scale = crate::solid_scale(source).max(1.0);
4208    let tolerance = 1e-9 * scale;
4209    const CELLS: usize = 12;
4210    let span = high.sub(low);
4211    let mut inside = 0usize;
4212    for i in 0..CELLS {
4213        for j in 0..CELLS {
4214            for k in 0..CELLS {
4215                let at = |index: usize| (index as f64 + 0.5) / CELLS as f64;
4216                let point = Vec3::new(low.x + span.x * at(i), low.y + span.y * at(j), low.z + span.z * at(k));
4217                let class = classify_point(point, source, tolerance * 10.0).or_refuse(KernelStage::Select, "classify_point")?.class;
4218                if class != PointClass::In {
4219                    continue;
4220                }
4221                inside += 1;
4222                let mut nearest = f64::INFINITY;
4223                for face in &retained {
4224                    nearest = nearest.min(distance_to_trimmed_face(face, &edges, point)?);
4225                    if nearest <= distance + tolerance {
4226                        break;
4227                    }
4228                }
4229                if nearest > distance + tolerance {
4230                    os_debug!(
4231                        "empty cavity declined: ({:.4}, {:.4}, {:.4}) lies {nearest:.6} from every retained face",
4232                        point.x, point.y, point.z
4233                    );
4234                    return Ok(None);
4235                }
4236            }
4237        }
4238    }
4239    if inside == 0 {
4240        return Ok(None);
4241    }
4242    os_debug!("empty cavity: every retained skin is shadowed and {inside} sampled interior points lie within the distance");
4243    let face_images = faces
4244        .iter()
4245        .map(|face| OffsetShellFaceImageRecord { role: OffsetFaceRole::Source, source_face_id: face.id })
4246        .collect();
4247    Ok(Some(OffsetShellResultRecord { solid: source.clone(), face_images }))
4248}
4249
4250/// `BREP_OFFSET_THIN_SKIN_PAD` (default on; `=0` restores): [`thin_skin_pad`].
4251fn thin_skin_pad_enabled() -> bool {
4252    switch!("BREP_OFFSET_THIN_SKIN_PAD", true)
4253}
4254
4255
4256/// The pad of an INWARD planar carrier whose shell is thinner than the
4257/// fragmenter's graze band: `None` otherwise.
4258///
4259/// Cause (the 10 × 8 × 6 box shelled 0.001 open at its top, brep-bench
4260/// shell/box-paper-thin, read 0.1653 against 0.2959 with an open mesh): each
4261/// carrier is its source face moved 0.001, and its neighbours' skins cut it
4262/// 0.001 inside its rim, with both ends 0.001 from the carrier's corners. The
4263/// imprint's pcurves are right (v = 0.001), but `fragment_face_arranged` lets a
4264/// CUT endpoint join a boundary corner image within `arrangement_tolerance ·
4265/// 100` (1.2e-3 on a 10 × 8 chart) with no 3D check, so both ends moved onto
4266/// the corners, the cut lay on the rim and was dropped as its duplicate
4267/// ("drop cut chain 6 as duplicate of boundary chain 0"). Padded past its rims,
4268/// the carrier's corners are far from every section's ends; the surplus
4269/// strips beyond the sections are outside the source or shadowed by the
4270/// neighbours and are dropped by the ordinary selection.
4271///
4272/// Thin: `distance` under 2e-3 of the solid's size (the band is 1e-4 of a
4273/// chart's diagonal, a chart at most the solid's size). The pad is 4e-3 of
4274/// the size plus the distance.
4275///
4276/// The same switch holds the selection's skin band and the assembly's weld to
4277/// a quarter of an inward distance: both had a fixed 2e-3 floor, twice this
4278/// shell's thickness (see `offset_skin_tolerance` and `assembly_weld`).
4279fn thin_skin_pad(source: &BrepSolid, face: &FaceRecord, distance: f64) -> Result<Option<f64>, KernelRefusal> {
4280    if !thin_skin_pad_enabled() || distance <= 0.0 || !face.surface.is_affine().or_refuse(KernelStage::Refine, "is_affine")? {
4281        return Ok(None);
4282    }
4283    let size = crate::solid_scale(source).max(1e-9);
4284    if distance >= 2e-3 * size {
4285        return Ok(None);
4286    }
4287    Ok(Some(4e-3 * size + distance))
4288}