0082. Generator cache keys and provenance headers
- Status: Accepted
- Date: 2026-11-01
- Plan: M9.5 Step 5.6
- Review: Making Tenon plugin-first
- Builds on: ADR-0075, ADR-0078, ADR-0081
Context
ADR-0075 hashes item bodies shallowly and signatures omit newtype representations. A generated companion must be checked against every input its generator can observe, while remaining deterministic and verifiable offline. ADR-0078 reserves E0968–E0971 for missing, stale, edited and orphaned generated files.
Decision
The checked-in companion is the cache; there is no second cache store. Compute its key as SHA-256 over this exact UTF-8 preimage, including the terminal LF:
"harness-gen-key-1\n" + GEN_ABI + "\n" + toolchain-version + "\n"
+ generator-id + "\n" + closure-hash + "\n" + target-sig-hash + "\n"
+ reflect-literal + "\n"
GEN_ABI is the constant 1.
generator-id is the canonical provider identity and
item:
<package>::<module.path>.<item>,
using the canonical package name and the source root stem as the
root module path (ADR-0075). The header spells that identity with
:: replaced by .. Compute
closure-hash as SHA-256 of
"harness-gen-closure-1\n", followed for each item
sorted by canonical item key by
sig_form + "\n" + body_form + "\n". Include every
item in the generator module dependency closure, including private
and transitive helpers and generated helpers.
reflect-literal includes all generator-visible
reflection data, including newtype representations and derived
flags. The target signature hash and reflection literal are both
explicit key fields. Bump the generator ABI when reflection,
validation or formatting changes; the toolchain version alone is
not sufficient.
The provenance header records generator, target, toolchain/ABI,
key and SHA-256 of the formatted output bytes after the header.
For marked generated files, validate the header and output
integrity before parsing generated source. Unreadable/non-UTF-8
files, edited bytes or a malformed header are E0970; a well-formed
intact header with an outdated generator, target, toolchain/ABI or
key is E0969. Unmarked handwritten companions with lexical/parse
failures are E0970; syntactically valid ones may first produce
ordinary binding or semantic diagnostics, but cannot pass check
without valid provenance. E0968 means no companion; E0971 means a
header-proven generated orphan, including under a deleted module's
directory. Reuse is fresh-only: harness gen skips
execution only when both key and output integrity match.
check and gen --check never write.
For a missing or stale stage-closure companion, give explicit
recovery: run harness gen <deriving-module.hn>
first from that module's package root. Running gen at the main
module cannot bootstrap a missing/stale generator-closure
companion; dependencies are verified, not regenerated.
Generation finds the nearest ancestor
harness.toml, falling back to the entry file's
directory without a manifest. A nested input such as
a/b.hn retains module identity a.b and
outputs under the package's gen/a/b/. Stage and
candidate loads use that same package directory. An intact
writable-package orphan must not block stage loading: the outer
generation transaction removes it after validation. Existing
unrelated modules' companions are preserved.
Checked edit/rename candidates tolerate E0969 only, then validate the whole source graph, including generator-closure bodies and references. The edit lands before explicit regeneration; missing, bad or orphaned companions and all other diagnostics still block it.
Graphs without derives do no generator canonicalization or key hashing. In a derive-bearing graph, rewrite and canonicalize the loaded graph once and share those forms across reflection and all jobs. Hash only each generator's module closure, the target's signature/type closure and its reflection literal; cache the closure hash per provider module. Unrelated runtime bodies are canonicalized but never enter generator-key hashes. Pin scaling to one forms computation per graph, independent of the number of derive jobs.
Alternatives considered
- Hash only the generator body/signature: misses private or transitive helper edits and newtype representations observable through reflection.
- Hash formatted source: comments and layout would cause needless invalidation, while canonical post-rewrite forms encode meaning.
- Automatically rebuild during check/build: executes generators and writes during verification, breaking explicit offline generation and fresh-only validation.
Consequences
Companions are reproducible, tamper-evident content-addressed outputs. The loader must compute complete generator closures and validate headers before parsing; diagnostics distinguish absent, stale, edited and orphaned files. Comment-only edits leave keys fresh, while any generator-visible input change invalidates them.