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 entry source plus the content hash of every
transitively-imported user file; stdlib imports are covered by the
embedded harn_version field in the header. Any change to any input
flips the key and the next run recompiles.
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]
import_hash : [u8; 32]
payload : bincode-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 are atomic (write-tmp-then-rename), 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.
- 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.
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.