ADR-0016 — Closures from outside the tree are entry-point plug-ins on the Python seams, behind the user-code gate¶
Status: Accepted (the maintainer's choices, 2026-11-25: entry-point
discovery, every out-of-tree closure behind --allow-user-code, a heat-transfer
plug-in as the proof). Implemented for the heat family; the friction and porous
families keep their kind = "python" dotted path and can take the same
mechanism by the same three edits.
Context¶
Adding a closure kind to aeolus1d means editing the tree.
closures/registry.py names the places: the schema's Literal arms, the
@njit dispatch chain, the builder's attach function, the GUI saver. Three of
them cannot be generated from a runtime registry. A user with their own wall
heat-transfer correlation, from a test bench or a paper, has had two options:
fork the solver, or write a kind = "python" closure with a dotted import path.
The second exists only for friction and porous media (closures/user_code.py),
and it ties a case file to a module path on the author's machine.
The roadmap's "Later" list asked for "closures and boundary conditions from outside the tree", with an ADR first and one working out-of-tree closure as its proof. The maintainer pulled it into the feature phase on 2026-11-24.
Three facts constrain the design.
- The kernels are compiled ahead of any registry. A plug-in cannot add a
branch to an
@njitdispatch chain. It can only supply Python that the solver calls through an existing Python seam. Those seams arepipe.py_friction,pipe.py_heatandpipe.py_porous. The timeloop routes a pipe that sets one through the quasi-1D overlay, and the implicit driver differentiates it by finite differences. Built-in kinds such asflow_nuandconstant_twallalready use these seams, so they are exercised paths rather than a new one. - Running code a case names is arbitrary code execution. That is why
kind = "python"is gated by--allow-user-code(AEOLUS_ALLOW_USER_CODE). The gate is process-level on purpose: a permission that the case file could grant itself would not be a permission. - A closure needs the owner's temperature, not a re-derived one.
user_code.PipeStateexists because recoveringTasp / (rho R)is wrong on a per-cell-gamma or real-gas pipe. That cost 26 % of a heat release once (ADR-0013). A plug-in gets the samePipeState.
Decision¶
Discovery: Python entry points. A package registers closures under the
entry-point group aeolus1d.closures. Each entry point names a ClosurePlugin:
a family ("heat" today), a dict of parameters with their units and defaults,
and a factory that turns the case's parameter values into a callable. A case
selects one by name:
[pipe.heat]
kind = "plugin"
plugin = "petukhov"
[pipe.heat.params]
T_wall = 450.0
D_h = 0.05
kind = "plugin" is the schema's one static arm for all plug-ins, so the
Literal stays a closed list that a type checker can read. The plug-in's own
parameter list is checked when the case is built:
- an unknown parameter is refused;
- a missing required parameter is refused;
- a default is filled in;
- each value must be a finite number.
The parameters live in a table the saver writes back unchanged, so a case survives a save from the GUI.
Trust: every out-of-tree closure is gated. Discovering plug-ins, and the
import their entry point performs, happens only with --allow-user-code. A
case that names a plug-in without the flag is refused, naming the plug-in and
the flag, exactly as kind = "python" is. Installing a package with pip is not
consent to run it from any case file someone else wrote. The settings banner
reports user code on, as it already does.
The seam: the plug-in returns per-cell values from a PipeState. For heat,
the factory returns a function from PipeState to the energy source per
interior cell, in W/m³. The builder adapts it to pipe.py_heat(rho, u, p) with
the machinery kind = "python" uses, and checks its shape and finiteness each
call. The kernel's own heat kind stays none.
Proof: examples/plugins/aeolus1d_petukhov/, a package of about fifty lines.
It provides the Petukhov (1970) turbulent-pipe Nusselt correlation,
Nu = (f/8) Re Pr / (1.07 + 12.7 sqrt(f/8) (Pr^(2/3) - 1)) with
f = (0.790 ln Re - 1.64)^-2, which the tree does not have. The tests install
it the way pip would: its entry_points.txt sits in a .dist-info on
sys.path. They run a case with it, and check it against the correlation
evaluated by hand.
Consequences¶
- A new heat correlation is a package of its own, versioned by its author. The solver needs no edit and no fork.
- A plug-in runs on the Python seam, so it costs what
flow_nucosts, not what an@njitkind costs. On the quasi-steady driver, Python closures are folded to constants per step, and they are refused in its circuit, as before. - The plug-in's parameters are numbers only. A plug-in that needs a table, a file or a string takes it from its own configuration, not from the case. That keeps the case format closed.
- The GUI lists
pluginas a heat kind and shows its parameter table as key/value fields. It does not import the plug-in to validate them; the build does that. - Boundary conditions are not in this ADR. A BC encodes ghost states through the gas model and the ends' contracts (ADR-0012, ADR-0013, and the per-cell rows of real gas with species). An out-of-tree BC needs a ghost seam that does not exist yet. That is the next step, and it gets its own ADR.
Alternatives considered¶
- Dotted path only (
kind = "python", extended to heat): rejected by the maintainer. A case would then name a module path, which does not travel with the case. There would also be no parameter declaration, so the loader could not refuse a misspelt parameter. - Trusting installed plug-ins without the flag: rejected by the maintainer. Opening a stranger's case would then run whatever they named, if that package happened to be installed.
- Compiled plug-ins (
@njitfirst-class functions passed to the kernels): not now. Numba's first-class function types would change every kernel signature that takes a closure, and the Python seam already exists and is tested. If plug-ins turn out to be hot, ADR-0015's compiled step is where a compiled seam would go.