0295.
Zero-Step Implicit Cached derive Materialization
- Status: Accepted (2026-11-28)
- Date: 2026-11-28
- Plan: Tenon Top-20 Agent-Native Leap (Wave 1, Rank 12, Idea
#03; extends
GEN_ABI 2offline code generation and ADR-0293)
Context
Tenon's GEN_ABI 2 derive pipeline
(derive Json, derive Args,
derive Config, derive Actions,
derive ToolSchema, etc.) guarantees pure, sandboxed,
deterministic code generation with cryptographic provenance
headers (~1g). Historically, however, every edit to a
derived struct, enum, or
newtype required an explicit tenon gen
step to write
gen/<module>/<Target>.<Gen>.tn on
disk before tenon check, tenon test,
tenon run, tenon interp,
tenon build, or tenon doc --card could
succeed (E0968 when missing, E0969 when
stale).
In addition, generator output validation (E0967)
rejected companion imports of packages declared as dependencies of
the generator package itself (for example,
app_actions_gen referencing
app_actions.contract.ActionSpec), forcing host
modules to maintain dummy unused imports solely to satisfy
generated companions.
For interactive human development and AI coding agents performing rapid edit-check-test loops, this created two sources of friction:
- Extra manual
tenon genround-trips and noisygen/diffs on every data-model change. - Dummy unused imports in host modules to satisfy generator runtime types.
Decision
Two-Tier Content-Addressed Gen Cache (
crates/tenon-compiler/src/gen/cache.rs):- Key every
GEN_ABI 2companion by its existing SHA-256job.key(coveringGEN_ABI, toolchainVERSION, generator identity, generator closure hash, canonical target AST signature hash, and reflection literal). - Tier 1 (Memory): Process-wide
BTreeMap<key, text>for O(1) hits within a run with zero disk I/O or interpreter execution. - Tier 2 (Disk): Package-local
.tenon/cache/gen/<ast+gen-sha256>/<Target>.<Gen>.tn, written atomically viafs_stage::write_atomic(best-effort on read-only directories; bypassed whenTENON_NO_CACHE=1). - Every cache read is verified via
gen::verify(job, &text)before acceptance; corrupted or mismatched entries are ignored and regenerated.
- Key every
Automatic Implicit Materialization During Module Loading (
modules/graph.rs,modules/companions.rs,gen.rs):- During
LoadMode::OrdinaryandLoadMode::Canonical(coveringtenon check,test,run,interp,build,eval,write,edit,deps, andhashes), the loader preserves its zero-overhead fast path when on-diskgen/companions exist and passgen::verify. - Whenever a
derivetarget has a missing (E0968) or stale (E0969) on-disk companion — and no corrupted/hand-edited companion (E0970) — the loader invokesgen::materialize_companionsand reloads once with the materialized companions supplied as in-memory overlays without creating or mutating on-diskgen/. - Setting
TENON_STRICT_GEN=1disables implicit materialization for strict CI environments that require committed on-diskgen/files. tenon gen --checkcontinues to verify on-diskgen/freshness against the materialized outputs, ensuring 100% backward compatibility with repositories that commitgen/.tenon doc --card(crates/tenon/src/doc_card/extract.rs) also merges implicit cached companions for modules declaringderiveitems when on-diskgen/is absent or stale.
- During
Hygienic Generator Runtime Imports (
crates/tenon-compiler/src/gen.rs):- Allow
GEN_ABI 2generator output to import modules from any package declared in the generator package's owntenon.tomldependencies (packages.dependencies(gen_pkg)), in addition tostd.reflect, host modules, the generator package itself, and same-generator derived packages. - Host modules no longer need dummy unused imports solely for
derives (such as importing
ActionSpecsolely forderive Actions). Arbitrary undeclared third-party package imports remain rejected withE0967.
- Allow
Inspection & Ejection CLI Surface (
surface_cmd.rs,cli/pkg.rs):tenon view --expanded <file.tn>expands the module alongside its materialized implicit/generated companions in deterministic filename order (rendered in readable ordinary Tenon syntax).tenon gen --eject [path]materializes all writable companions (reusing.tenon/cache/gen/in O(1)) and atomically publishes them to on-diskgen/.
Reserved Diagnostic Code:
E1160: Implicit derive materialization tool failure (e.g. I/O or internal staging error during implicit load).
Alternatives Considered
- Removing on-disk
gen/support entirely: Rejected because existing repositories, audits, andtenon gen --checkworkflows benefit from optional committedgen/files. Supporting on-diskgen/as priority 1 and.tenon/cache/gen/as automatic fallback preserves full backward compatibility. - Caching by file mtime instead of AST + generator
SHA-256: Rejected because mtime-based caches are brittle
across git operations, in-memory overrides
(
tenon write/edit), and multi-item files where unrelated functions change.
Consequences
- Editing a
structorenumwithderive Json,derive Actions, or anyGEN_ABI 2generator passescheck,test,run,interp,build, anddoc --cardimmediately with zero manualtenon geninvocation. - Repeated checks with unchanged target AST and generator closure hit the cache in O(1) without running the generator interpreter.
- Developers and agents can inspect expanded output
deterministically via
tenon view --expanded <file.tn>or materialize on-diskgen/files on demand viatenon gen --eject [path]. - Existing repositories with committed
gen/directories continue working with zero regressions.