0078. Generators: signature, trigger, explicit gen, staged edges
- Status: Accepted; generated-import restriction superseded by ADR-0084
- Date: 2026-11-01
- Plan: M9.5–M9.6
- Review: Making Tenon plugin-first, Data, schemas and wire formats
Context
ADR-0006 requires a fixed generator trigger and readable output without grammar plugins. ADR-0064 modules cannot load generated imports back into their host without cycles; ADR-0069 protects newtype representations by file. ADR-0075 body hashes are shallow and newtype signatures omit representations, so neither alone is a complete generator key.
Decision
- Signature (D1): a generator is an ordinary
pub fn X(t: std.reflect.Type) -> result<str, str>. It returns source text, validated by the existing parser and formatter, rather than a second public AST API. - Reflection (D2): embed
std.reflectviainclude_str!, matching that exact import only. Reserve dependency keystd(E0932); reject localstd/reflect.hn(E0934). Build reflection from the post-Rewrite AST before host type checking.Type { target, refs, named }is a flat indexed table: primitive/container/Named/Unsupported refs; named structs, enums and newtypes carry names, module, visibility, same-generatorderived, signature hash and shape. Deterministic post-order includes the target's type closure; render an expression literal for a synthetic entry, avoiding Rust-value marshalling. - Trigger (D3): top-level
derive Ident for Identends like a newtype.deriveis contextual only beforeIdent for; malformed or public derives are E0117. Resolve the generator through normal file import bindings, by provider ModuleId and item. The target must be a non-generic struct, enum or newtype declared in the same file (E0962); foreign files cannot grant themselves newtype authority. - Companions (D3): write
<package>/gen/<module-or-root-stem-path>/<T>.<X>.hn. A companion shares the host ModuleId but has its own file index, sees host bindings and its own imports/declarations, and contributes declarations visible to the host. Identical imports are deduplicated. Extend ADR-0064/0069 withLoaded.file_ownersfor host newtype access only; package ceilings remain owned by the deriving file's package. - Stages (D4): Rewrite records uses. An edge whose selected names are used only as derive generators is generator-only. Keep those modules loaded for hashing but omit exclusively stage-reachable modules from runtime lowering. A generator closure depending on its deriving module is E0963. Generator packages may derive with fresh checked-in companions.
- Sandbox (D5): use a pure interpreter entry
returning a value on a worker thread, no capabilities, I/O or
clock. Runtime platform/print/exit guards reject hand-built
forbidden IR as compiler-bug tool errors (exit 3). Fixed limits
are 50,000,000 fuel, 256 MiB fresh heap growth (not views), and 16
MiB output; no manifest override. Heap exhaustion traps
out of memory. Generator closures are exempt from capability-ceiling reporting (ADR-0074 is declared intent); security review remains M16.3. - Explicit execution (D6): only
harness gen <file.hn> [--check]executes generators or writes companions; check/build/run/interp/emission only verify freshness. Extend ADR-0072's explicit-write rule: no lock/network writes, dependencies verified but regenerated only at their own root. Load without companions, construct synthetic stage roots under package rules, execute in ModuleId/source order, validate/format/header output, and check the whole program in memory before atomic publication and header-proven orphan deletion. Failure leaves the tree untouched;--checkwrites nothing. - Keys (D7): checked-in
gen/is the cache, with no second store. SHA-256 over domainharness-gen-key-1\n, GEN_ABI, toolchain version, generator identity, sorted canonical signature/body forms of its entire module closure (private/transitive helpers included), target signature hash and reflection literal covers every observed input, including newtype reprs and derived flags. Bump ABI for reflection/validation/format changes. A v1 magic header records generator, target, toolchain/ABI, key and formatted-output hashes. Distinguish stale (E0969), missing (E0968), edited/bad header (E0970), and orphan (E0971). - Determinism/validation (D8–D9): canonical formatting, insertion-ordered maps and absent ambient access give byte-identical output across runs/cwds. Output must parse and contain neither derive nor main (E0967); imports resolve under the deriving package's rules, limited to its own package or std. Duplicate derives are E0972; E0960–E0966 cover binding/signature/target/cycle/trap/limit/returned-error failures; E0973–E0979 stay reserved.
- Tools (D10): fmt/rename walkers skip magic-header files; direct fmt and edit replace-fn refuse them. Outline lists derives and marks generated files. Rename refuses generated references with regeneration instructions. Generated declarations are ordinary host items for hashes/compatibility; strip derives before canonical forms and merging.
Owner defaults are adopted: refuse generated-reference renames
(park rename --regen); same-file targets only; reject
generic targets; JSON integers stay numbers even beyond 2^53 (use
a str newtype for interop); option keys are required with null for
none; codecs remain path dependencies until M9.8; stale files fail
checks; limits are fixed pending M10.5; always commit gen/;
reserve exact std.reflect pending M0.9 naming. Schema evolution
and file-input DSL generators are deferred.
Alternatives considered
- Automatic generation during builds — introduces execution and writes into offline, reviewable checks and obscures stale inputs.
- Ordinary generated modules — import cycles and file-private newtypes prevent the required owner-authorized generated API.
- Shallow body/signature-only cache — misses helper edits, newtype reprs and other reflected observations.
Consequences
One grammar and source-based plugin authoring survive; staged
reachability and companion authority require explicit loader
support. M9.5 lands parser, sandbox, companions, reflection,
execution, provenance, tools and spec in that order; M9.6 adds
codecs and the necessary vec
ADR reservations: 0079 sandbox limits (Step 5.2) then 0080 companions (5.3) follow landing order. The reflection record lands before cache/provenance, so this owner-assigned sequence is 0081 reflection (5.4), 0082 cache/provenance (5.6), and 0083 codec wire rules (6.1). ADR-0076 belongs to parallel M9.3 work. At 8a46e18, canon exposes forms/CanonicalItem.sig_form/body_form and the loader exposes Loaded.program/visible_interfaces/capability_ceilings; M9.3 Steps 2–5 and cp/9.2-packages are absent. Recheck those integration points after they land, before closing M9.5/CP-9.3.