On this page

0318. Implicit Cache-Backed gen/ Derive Materialization (F47)

Context

ADR-0295 introduced two-tier implicit cached derive materialization (in-memory BTreeMap + package-local .tenon/cache/gen/<sha256>/<Target>.<Gen>.tn) for packages with a tenon.toml.

Two remaining gaps blocked zero-friction workflows for standalone scripts and agent inspection:

  1. Standalone scripts without tenon.toml: gen::cache fell back to <script_dir>/.tenon/cache/gen/..., cluttering the script's directory with .tenon/ artifacts.
  2. Ergonomic inspection and explicit write flags: Agents and users expect tenon view <file.tn> --generated alongside --expanded, tenon gen --print <file.tn> to inspect materialized derived code on demand without writing gen/, and tenon gen --write as an explicit counterpart to tenon gen --check.

Decision

  1. Package-Local and Standalone Global Gen Cache (crates/tenon-compiler/src/gen/cache.rs, gen.rs):

    • When the deriving module belongs to a package with tenon.toml, Tier 2 disk caching uses <package_dir>/.tenon/cache/gen/<job.key>/<Target>.<Gen>.tn.
    • When the deriving module has no enclosing tenon.toml (such as a standalone #! script under ADR-0319), Tier 2 disk caching routes to $TENON_CACHE_DIR/gen/<job.key>/<Target>.<Gen>.tn (falling back to $XDG_CACHE_HOME/tenon/gen or ~/.cache/tenon/gen), leaving zero .tenon/ or gen/ files in the script's directory.
    • Every read is verified via gen::verify(job, &text); TENON_NO_CACHE=1 disables disk caching cleanly.
  2. Full Backward Compatibility with Committed gen/:

    • Existing on-disk gen/*.tn files remain Priority 1 whenever fresh (gen::verify passes).
    • tenon gen [path] and tenon gen --write [path] publish fresh companions to gen/.
    • tenon gen --check [path] verifies on-disk gen/ freshness; TENON_STRICT_GEN=1 disables implicit fallback during loads.
  3. On-Demand Derived Code Inspection (crates/tenon/src/surface_cmd.rs, crates/tenon/src/cli/pkg.rs):

    • tenon view <file.tn> --generated is accepted as an alias for tenon view <file.tn> --expanded.
    • tenon gen --print <file.tn> (with optional --json) materializes and prints derived companions in readable ordinary Tenon syntax without modifying gen/.

Consequences