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