On this page

0295. Zero-Step Implicit Cached derive Materialization

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:

  1. Extra manual tenon gen round-trips and noisy gen/ diffs on every data-model change.
  2. Dummy unused imports in host modules to satisfy generator runtime types.

Decision

  1. Two-Tier Content-Addressed Gen Cache (crates/tenon-compiler/src/gen/cache.rs):

    • Key every GEN_ABI 2 companion by its existing SHA-256 job.key (covering GEN_ABI, toolchain VERSION, 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 via fs_stage::write_atomic (best-effort on read-only directories; bypassed when TENON_NO_CACHE=1).
    • Every cache read is verified via gen::verify(job, &text) before acceptance; corrupted or mismatched entries are ignored and regenerated.
  2. Automatic Implicit Materialization During Module Loading (modules/graph.rs, modules/companions.rs, gen.rs):

    • During LoadMode::Ordinary and LoadMode::Canonical (covering tenon check, test, run, interp, build, eval, write, edit, deps, and hashes), the loader preserves its zero-overhead fast path when on-disk gen/ companions exist and pass gen::verify.
    • Whenever a derive target has a missing (E0968) or stale (E0969) on-disk companion — and no corrupted/hand-edited companion (E0970) — the loader invokes gen::materialize_companions and reloads once with the materialized companions supplied as in-memory overlays without creating or mutating on-disk gen/.
    • Setting TENON_STRICT_GEN=1 disables implicit materialization for strict CI environments that require committed on-disk gen/ files.
    • tenon gen --check continues to verify on-disk gen/ freshness against the materialized outputs, ensuring 100% backward compatibility with repositories that commit gen/.
    • tenon doc --card (crates/tenon/src/doc_card/extract.rs) also merges implicit cached companions for modules declaring derive items when on-disk gen/ is absent or stale.
  3. Hygienic Generator Runtime Imports (crates/tenon-compiler/src/gen.rs):

    • Allow GEN_ABI 2 generator output to import modules from any package declared in the generator package's own tenon.toml dependencies (packages.dependencies(gen_pkg)), in addition to std.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 ActionSpec solely for derive Actions). Arbitrary undeclared third-party package imports remain rejected with E0967.
  4. 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-disk gen/.
  5. Reserved Diagnostic Code:

    • E1160: Implicit derive materialization tool failure (e.g. I/O or internal staging error during implicit load).

Alternatives Considered

Consequences