# Kernel examples

The modeling kernel run from Node, with no modeler and no browser.

```sh
npm ci
npm run example:kernel                            # a curved panel, laid flat
npm run example:kernel:step                       # STEP interop: the built-in fixture
npm run example:kernel:step -- part.step out.step # STEP interop: your own file
```

## `paper-sheet.ts`

Makes a sheet swept from a curved rail along a straight direction (a paper
surface: it bends from flat stock without stretching), pushes it to a 0.05
thick panel, cuts a window through the panel, and unfolds every face of the
result with exact pieces only. It prints the sheet's area on the surface and
on the page, which agree, the Boolean engine that cut the window, how many
faces laid flat exactly, and what the validator says.

What to look for:

- **The flat piece is the surface, not an approximation of it.** The
  unfold's piece is an isometry of the face: its strain is 0 and its area on
  the page is the area integrated on the surface.
- **Thickening and cutting keep the faces paper.** The panel's far side is
  the exact offset of the sheet, the walls between the two sides are ruled, and the window's cut
  faces stay on the panel's own surfaces, so every face still lays flat.
- **Each step commits or refuses.** A step is a verb of the package, the one
  the host's tool calls on commit, with the postcondition gate on. It either
  leaves a mesh the validator accepts, or returns `refused [code] message`
  and the model as it was. `unfold` with `exactOnly` refuses a face that
  would have to stretch to lie flat, rather than approximating it.

The script imports one package by name, `@commonplace-technologies/kernel`,
and nothing else from this tree.

## `step-cut-push.ts`: STEP interop

STEP import is for placing manufactured parts, bought from a catalog rather
than modeled (plumbing fixtures, furniture), in an architectural model for
context; export sends parts to fabrication, such as cabinet parts to a CNC
mill. This script exercises both ends on a small machined part.

Reads a STEP part, subtracts a box from it, pushes one flat face of the
result, undoes and redoes the push, and writes STEP back. It prints, after
each step, the face count by surface kind, the area and volume integrated
on the faces' own surfaces, and what the validator says.

With no arguments it reads `test-models/import/step/occt/box-blind-hole.step`,
a part built by OpenCASCADE whose reference volume and area sit beside it in
`box-blind-hole.json` (in mm; the kernel reports meters).

What to look for:

- **Each step commits or refuses.** A step is a verb of the package, the one
  the host's tool calls on commit, with the postcondition gate on. It either
  leaves a mesh the validator accepts, or returns `refused [code] message`
  and the model as it was. Try a part or a distance that should fail.
- **Curved faces stay on their surfaces.** The hole's wall is read as an
  exact face, not facets, and is still one after the cut. The line the STEP
  writer prints at the end counts faces and edges written `exact`, `fitted`
  and `skipped`.
- **Undo is the mesh's own history.** `meshVerbContext(mesh)` gives the mesh
  an undo stack; every verb is one step on it.
- **The exact engine answers first.** The subtract prints which engine made
  its result and how many faces, if any, were left as flat facets.

This script imports two packages by name and nothing else from this tree:
`@commonplace-technologies/kernel` (the mesh, the verbs, validation, mass properties) and
`@commonplace-technologies/kernel-step` (`readStep`, `writeStep`). In this repository the
names resolve to `src/kernel` and `src/kernel-step`; `npm run build:kernel`
builds the packages themselves, and `npm run test:kernel-package` installs
them into an empty project and runs them there (ADR-132,
plan 132).

## The testbed

A page that runs a script against the two packages in the browser and draws
what the script shows, with no server. The presets start with paper: a
curved panel laid flat, a sheet creased and folded, a conic strip, a box as
a net and a printable paper model, an opening in a curved wall. A
showcase of paper forms follows, rebuilt through the public verbs: Dürer's
nets, the Melencolia solid, a trompe after de l'Orme with its stones'
templates, a folding screen, a turned lamp, a chair in one ribbon of board
and a canopy blade. Then
Offset and a dragged corner with undo, `canRun` and a refusal, save and
load, and last, under Interop, a STEP round trip.

```sh
npm run testbed:kernel        # http://localhost:3310
npm run build:kernel-testbed  # static files in dist-kernel-testbed/
```

A script is the body of an async function. `kernel` and `step` are the two
packages, `show(mesh, label?)` draws a mesh from the kernel's own
tessellation, and `print(...)` writes a line. `showFlat(x, label?)` draws
an `unfold()` pattern, a `paperModel()` (its first page) or a page of one,
or an SVG string, in the Flat view beside the model (`testbed/flat.ts`;
valley folds dashed, mountain folds dash-dot, rulings gray), and
`download(name, data)` offers a file in Output. Under the view, each verb call
is listed as it happens (committed, refused with its code and message, or a
`dryRun` preview), and each mesh shown is described by its faces by surface
kind, its area and volume, and what the validator says. An error names the
script line it came from.

The editor is Monaco, loaded after the first run. It checks the script as
JavaScript against the two packages' own declarations
(`scripts/testbed-types.mjs` builds them when a source is newer, on
start and on build) and the helpers in `testbed/globals.d.ts.txt`, so
completion and hover show the API's JSDoc. Type errors never stop a run.
`src/core/integration/kernel-testbed-types.test.ts` holds every preset to
no type errors, as the editor checks them.

A click in the view picks a face, edge or corner through the worker
(`kernelQuery(mesh).pickEdge` / `pickFace`), and the Inspect tab shows what
`kernelQuery` says of it, each answer under the call that reads it.

The script runs in a Web Worker (`testbed/run.worker.ts`), never in the
page: a fresh worker per run, with the next one already loading the kernel.
The worker owns the kernel and every mesh and sends the page only what it
draws. Stop (or Esc) and the time limit (10 s, or 30 s or 60 s) terminate
it, so a script that never returns cannot hang the page. The worker's
Content Security Policy (`RUN_WORKER_CSP` in
`vite.kernel-testbed.config.ts`) blocks the network; the dev and preview
servers send it, and a static host that can set headers should send it for
`assets/run.worker-*.js`.
The hosted copy, at
https://commonplace-technologies.github.io/kernel/testbed/, is built and
published from main by `.github/workflows/site.yml`. GitHub Pages cannot
set headers: the page's own policy is in a meta tag, but the run worker's
is not sent there, so a script in the hosted testbed can reach the network.
A visitor's script runs on the site's origin, so the site stays
cookie-free with no sign-in and nothing stored.
The page's look is one stylesheet of its own (`testbed/testbed.css`):
neutral ink on a neutral ground, a system sans for the page and mono for
the script and its output, one blue for hover and selection and one red for
errors, and a dark scheme that follows the system's.
The preset scripts are in `testbed/examples.ts`; they run in Node on every
pull request through the page's own runner
(`src/core/integration/kernel-testbed.test.ts`), each within 2 s and
refusing only where it means to (`refusals`).

## Measuring it

| Command | What it prints |
|---|---|
| `npm run limitations` | The index of known limitations, each with its regression test |
| `npm run check:kernel-package` | That the kernel package imports nothing outside itself and type-checks alone |

The soundness and interop runs on public CAD corpora (OpenCASCADE's
Boolean tests, DeepCAD, MFCAD, NIST), each case compared with its record
and with OpenCASCADE as the reference for Boolean results, run in the
kernel's source tree and are not part of this repository.
