Kernel abstraction

From andymai/brepjs · View interactive version

Kernel abstraction is an AI agent skill from the GitHub repository andymai/brepjs. This skill should be used when working on the brepjs kernel abstraction layer or across multiple kernels — adding a kernel method, wiring or writing an adapter, switching or registering kernels, or reasoning about kernel capability differences. Trigger phrases include "add a kernel method", "new ker

How to use this skill

Skill file

Kernel abstraction layer and multi-kernel work

Mental model

src/kernel/ is Layer 0 — it imports nothing else in the tree (see the architecture-navigation skill for layer rules and the .wrapped/.oc bans). Everything above it calls geometry through getKernel().method(...) against the KernelAdapter interface.

Four in-tree adapters:

| id | Class | File | Backing package | Nature |

| --------------------- | ----------------- | ---------------------------------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------- |

| occt-wasm (default) | OcctWasmAdapter | src/kernel/occtWasm/occtWasmAdapter.ts | occt-wasm | exact B-rep, arena/u32 handles |

| occt | DefaultAdapter | src/kernel/occt/defaultAdapter.ts | brepjs-opencascade | exact B-rep, Emscripten objects |

| brepkit | BrepkitAdapter | src/kernel/brepkit/brepkitAdapter.ts | brepkit-wasm | exact B-rep |

| manifold | ManifoldAdapter | src/kernel/manifold/manifoldAdapter.ts | manifold-3d (loaded via packages/brepjs-manifold) | mesh CSG, approximate; caches an OCCT "replay" B-rep per handle for exact export |

Registry (src/kernel/index.ts):

Optional backends load through importOptionalBackend(specifier) (src/kernel/optionalBackend.ts) using a variable specifier so bundlers cannot statically analyze the import() (mechanics in the wasm-interop skill).

Switching kernels safely

Adding a kernel method

Full walkthrough with a real cross-adapter example: references/adapter-wiring.md. The checklist:

  1. Declare the method in the matching sub-interface under src/kernel/interfaces/ (booleanOps, primitiveOps, modifierOps, ioOps, ...; core.ts is the mandatory surface). KernelAdapter is the intersection of these 15 files plus Kernel2DCapability (src/kernel/interfaces/index.ts); src/kernel/types.ts merely re-exports it. Do not add methods to types.ts. Note: the /new-kernel-method command (.claude/commands/new-kernel-method.md) predates this split — where it says "add to src/kernel/types.ts" and names an OcShape type, read "add to src/kernel/interfaces/<domain>Ops.ts>" and KernelShape/KernelType (src/kernel/types.ts).
  2. Implement per adapter as a free function in src/kernel/<adapter>/*Ops.ts, receiving the raw instance (oc, bk, ...) as the first parameter — never via getKernel() inside kernel code. All kernel methods are synchronous and return plain JS values or opaque KernelShape handles (docs/kernel-swap.md, "What Must Each Method Return?"). Delete Emscripten intermediates manually (maker.delete()); see the memory-and-disposal and wasm-interop skills for handle lifetime and the enum-extraction gotcha.
  3. Wire into the adapters. occt, brepkit, and manifold adapters have no body-level methods — add the method to the relevant make*Ops() factory's returned object (and its satisfies Pick<KernelAdapter, ...> union). A compile-time guard at the bottom of each adapter file errors with the exact missing-method list if any factory forgets one. OcctWasmAdapter is the exception: a conventional class — add a real method body.
  4. Stub adapters that cannot support it. Two accepted idioms: the occt adapter's makeBrepkitOnlyStubs() (src/kernel/occt/defaultAdapter.ts, uniform '<name> is only available with the brepkit kernel' throw) and occt-wasm's notImplemented(method) ('occt-wasm: <method> is not yet implemented'). A throwing stub is correct; silently returning a wrong answer is not.
  5. Surface through a *Fns.ts function that calls getKernel().method(...) — see the adding-operations skill for the Fns → api.ts → facade pipeline.
  6. Test through the Layer 2 functional API, not the adapter directly. Register divergence entries for kernels that skip (writing-tests skill), then run npm run conformance:generate.

Capability and feature detection — three distinct systems

| Need | Use |

| ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

| Route by what a kernel _is_ (exact vs mesh, B-rep export, tessellation model) | getKernelCapabilities(id?) → KernelCapabilities (src/kernel/capabilities.ts; EXACT_BREP_CAPABILITIES for the B-rep kernels, all-false + 'build-time' on manifold). Routing is caller-side today — only the quality layer branches on tessellationModel internally |

| Optional method _groups_ on an adapter | Type guards: supportsProjection / supportsConstraintSketch (src/kernel/types.ts), supportsKernel2D (src/kernel/kernel2dTypes.ts, used by getKernel2D) |

| Test matrix + generated conformance doc | kernelConfigs[].capabilities in tests/helpers/kernelRegistry.ts — pure data, no src imports (consumed by vitest.config.ts at config-load time) |

Do not conflate them: the runtime flags describe kernel _nature_, the guards describe _interface presence_, the registry booleans describe _test expectations_.

Inside adapters, detect optional native features rather than version-checking:

Multi-kernel testing and conformance

Symptom → cause → fix

| Symptom | Cause | Fix |

| ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |

| BindingError: Cannot pass deleted object from occt-wasm | Adapter built from new OcctWasmAdapter(kernel.getRawModule(), kernel.getRawKernel()) — the OcctKernel wrapper's FinalizationRegistry freed the raw kernel on GC | Build with OcctWasmAdapter.fromKernel(kernel), which retains the wrapper (retainedKernelOwner) |

| brepjs kernel not initialized | getKernel() before any registration | await init() (or registerKernel/initFromOC) first; in tests use initKernel() from tests/setup.ts |

| brepjs: kernel 'X' is not registered | withKernel/getKernel(id) with an id that was never registered this session | Register it (e.g. initFromManifold) before switching |

| withKernel() callback returned a Promise | Async callback passed to withKernel/withQuality/withTier | Use getKernel(id) directly in async code |

| X is only available with the brepkit kernel / occt-wasm: X is not yet implemented | Intentional throwing stub on this adapter | Switch kernels for that call, or implement the method (recipe above) |

| Wrong kernel's divergences applied when running vitest without a project | currentKernelId in kernelDivergences.ts defaults to 'occt', while kernelInit.ts defaults to 'occt-wasm' | Always run via a --project flag or set TEST_KERNEL explicitly |

| Build fails resolving occt-wasm/brepkit-wasm in a consumer bundle | importOptionalBackend was replaced with a literal import() | Restore the variable-specifier indirection in src/kernel/optionalBackend.ts |

| 2D ops work on occt-wasm despite the raw kernel having no 2D API | 2D is fulfilled by the shared pure-TS engine src/kernel/geometry2d.ts (also used by brepkit) | Nothing to fix — implement 2D features there, not per-kernel |

Known-stale docs (trust code over these spots): docs/kernel-swap.md "tests run against all three kernels in CI" + its hardcoded 3-project vitest snippet; .claude/commands/new-kernel-method.md (types.ts, two adapters, OcShape); src/kernel/README.md "three adapters" (manifold missing).

Additional resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.