Expand description
Content-addressed on-disk cache for compiled .harn pipelines.
Cold-start harn run re-parses, type-checks, and compiles the entry
pipeline before the VM gets a single instruction to execute. For short
Harn subcommands that wrap a few llm_calls in a small pipeline, that
compile cost dominates wall-clock time.
This module persists Chunk bytecode under
$HARN_CACHE_DIR/<source-hash>.harnbc (XDG-aware). The cache key is
derived from the source plus its compilation context. Entry chunks include
the content of every transitively-imported user file because they compile
the complete program. Module artifacts compile exactly one file and retain
unresolved import specs, so their context includes compiler and embedded
stdlib identity but deliberately excludes user dependencies. Any change to
an artifact’s actual compilation inputs flips the key and recompiles it.
File layout — little-endian throughout:
magic : [u8; 8] = "HARNBC\0\0"
schema_ver : u32 = SCHEMA_VERSION
version_len : u32
harn_version : [u8; version_len]
compiler_tag : u8 bitmask of active CompilerOptions
kind : u8 1 = entry chunk, 2 = module artifact
source_hash : [u8; 32]
context_hash : [u8; 32]
payload : postcard-serialized payload for `kind`The header lets a stale binary detect a future-version artifact
without crashing: a magic mismatch, schema mismatch, or version
mismatch is returned as Ok(None) so the caller transparently
recompiles. Real I/O errors propagate.
Concurrency: writes go through crate::atomic_io (write-tmp, fsync,
rename, fsync parent dir), and parallel invocations on a cache miss race
safely — the last writer wins, but every reader observes a consistent file
because the rename is atomic on every supported filesystem.
Structs§
- Cache
Key - Cache key components for a single pipeline source. Equality of all fields is necessary and sufficient for cache reuse.
- Lookup
Outcome - Result of a cache lookup. Carries the precomputed key so the caller can write it back on a miss without rehashing.
- Module
Lookup Outcome - Result of a
load_modulelookup. Carries the precomputed key so the caller can write it back on a miss without rehashing.
Constants§
- CACHE_
DIR_ ENV - Environment override for the cache directory. When set, takes precedence over the XDG and home-directory fallbacks.
- CACHE_
ENABLED_ ENV - Environment override that turns the cache off entirely. Setting this
to
0,false,no, oroffskips both reads and writes; useful when debugging compiler changes. - CACHE_
EXTENSION - Conventional extension for entry-chunk cache files.
- CODEGEN_
FINGERPRINT - Build-time fingerprint of the compiler front-end — the lexer, parser, IR,
and code generator — computed in
build.rsfrom those crates’ source and baked in viacargo:rustc-env. Folded into the cache key so a compiler change that alters emitted bytecode for unchanged source invalidates stale entries automatically, within a single version, with no manual cache wipe.HARN_VERSIONonly busts the cache across release bumps; this closes the same gap for the within-version compiler edits that masked #2610. See #2621. - HARN_
VERSION - Compile-time Harn release. Cache files written by a different release are rejected on load.
- MAGIC
- Header magic for all bytecode-cache artifact families.
- MODULE_
CACHE_ EXTENSION - Conventional extension for module-artifact cache files. Distinct from
CACHE_EXTENSIONso the same.harnsource can have both shipped adjacent if needed (e.g. when a file is both an executable entry and imported by other files). - SCHEMA_
VERSION - On-disk format version. Bump when
CachedChunkor the header layout changes in a backwards-incompatible way. v5:ModuleArtifactgainedpublic_type_names(pub typeexports). v6: payload encoding replaced with postcard. v7: exported type schemas moved from eager JSON strings to an initializer chunk that resolves imported aliases in the module environment.
Functions§
- adjacent_
cache_ path - Path to the adjacent precompiled entry-chunk artifact for
source_path.foo.harn→foo.harnbc. - adjacent_
module_ cache_ path - Path to the adjacent precompiled module-artifact for
source_path.foo.harn→foo.harnmod. - cache_
dir - Returns the directory the shared cache lives in. Honors
$HARN_CACHE_DIR, then$XDG_CACHE_HOME/harn/bytecode, then$HOME/.cache/harn/bytecode. The directory is not created here —storecreates it lazily on write so read-only environments don’t pay an mkdir cost. - cache_
enabled - True when the cache is enabled by the current environment.
- load
- Try to load a cached chunk for
source_pathwhose contents aresource. Returns the key alongside the (optional) chunk so callers avoid recomputing the key on miss. - load_
module - Look up the
ModuleArtifactforsource_path(whose contents aresource). Mirrorsloadbut for the.harnmodfamily. - packs_
cache_ dir - Root for
.harnpackarchives unpacked byharn run <bundle.harnpack>. Each verified bundle is replayed into<root>/<sanitized-bundle-hash>/so re-runs reuse the unpacked tree. Honors$HARN_CACHE_DIR/packswhen set, otherwise XDG /$HOME/.cache/harn/packs. - serialize_
chunk_ artifact - Serialize an entry-chunk artifact (header + payload) to bytes. The
resulting buffer is byte-identical to the file
store_atwould have written for the same(key, chunk). Use this when packaging artifacts into a container (e.g.harn pack) without going through the filesystem. - serialize_
module_ artifact - Serialize a module artifact (header + payload) to bytes. Companion
to
serialize_chunk_artifactfor the.harnmodfamily. - store
- Persist
chunkto the shared cache directory underkey. Atomic: a temp file is written then renamed into place. Concurrent invocations on the same key race safely. - store_
at - Write a precompiled entry-chunk artifact to an explicit path, for
use by the
harn precompilesubcommand. The header still records the key, so adjacent artifacts shipped with source are validated like any other cache hit. - store_
module - Persist
artifactto the shared cache underkey. Atomic; concurrent invocations race safely. - store_
module_ at - Write a module artifact to an explicit path.