Storage¶
pivot.storage keeps Pivot's state: StateDB, the per-stage lock and listing files, the
content-addressed cache, restores, the output-ownership checks and pivot gc. This page maps each
rule that spans modules to the code that keeps it. The short list of rules a change must keep is
packages/pivot/src/pivot/storage/AGENTS.md. What users see is in
Caching & Skip Detection, Outputs and
Dependencies; a rule inside one function is in its docstring.
StateDB key prefixes¶
StateDB (storage/state.py) is one LMDB environment per state directory,
<state dir>/state.lmdb, with a 10 GiB virtual map (_MAP_SIZE). Keys and values are bytes.
Each kind of entry has a prefix constant at the top of state.py:
| Prefix | Key → value |
|---|---|
hash: |
the absolute path as given (os.fsencode(os.path.abspath(path))) → (mtime_ns, size, dev, inode) from a stat() that follows symlinks, and the content hash |
inode: |
(st_dev, st_ino) → the hash: key of one path with that identity; served only after the stat and the recorded path's device, inode and change time match |
gen: |
artifact identity (stage or stage:key) → output generation counter |
dep: |
stage:dep_identity → the dependency's generation when the stage ran; a file of a directory output records its directory's identity |
runcache: |
stage:input_hash → the run's output hashes (run_history.RunCacheEntry) |
run: |
run id → run history manifest |
sm: |
fingerprint manifest cache (stage manifests and source maps); best-effort, never needed for correctness |
fp: |
AST fingerprint and hash cache |
remote: |
remote_name:hash → present when the remote is known to hold the object |
remote_url: |
remote name → the URL its remote: entries were recorded for |
nocommit: |
stage → types.NoCommitRecord: what the stage's outputs hold that no lock records yet |
A file-hash key costs no system call to build; the stat in its value decides whether the cached hash still applies, so a link re-pointed at another file no longer matches. Generation keys use the artifact identity, so they stay the same wherever an output's path points.
A nocommit: record holds a --no-commit run's code manifest, output hashes and reads, for
pivot commit, and the *.pvt members Pivot wrote (pvt_members). Each writer replaces only its
own keys, in one write transaction; a run replaces all three of its keys, so an earlier run's
reads never stays beside a later run's outputs. The reader validates each key on its own and
drops only a malformed one. Only a written lock removes the record
(DeferredWrites.clear_no_commit_record).
A write waits for StateDB._write_transaction up to the StateDB's write_timeout (30 s by
default); a best-effort hash-cache save waits _WRITE_BACK_TIMEOUT (2 s). The worker/coordinator
split and the generation counters are in Execution Model.
File-hash write-back¶
Every command that hashes, commits or restores a workspace file saves its hash, so the next
pivot status --explain or pivot verify reads no file (the commands are listed in
File Change Detection).
- A caller collects entries with
pending=(cache.hash_file,cache.hash_directory) orrecorded=(cache.restore_from_cache) and saves them withstate.save_file_hashesorstate.FileHashBatch, which writes once per state directory. A stage output's entry goes to the StateDB of the output stage's state directory; any other path's to the configured one. - A worker returns its entries in
DeferredWrites.file_hash_entries(cache.deferred_file_hash_entries). In a normal run they share the run's own transaction with the stage's generations; under--no-committhe coordinator saves them best-effort. - Every
hash:write also writes theinode:entries, after the hash entries. Which filesystems the identity index trusts is decided instorage/mounts.py. - The
hash:cache holds one content hash per file, shared by filtered and whole directory walks; no directory hash is cached by path.
Directory inputs and .pivotignore¶
WorkspaceStore.hash_artifactpassesignoredtocache.hash_directoryfor apipeline.inputdirectory, and nothing else does.- A directory input's committed listing (
listing.input_listing_path) holds only the members its hash counts;listing.parserefuses any other listing for that hash. Onlycli/verify.py, the engine's git-ignored warning andstatus.py's orphaned-listing report read it. A restore takes members from an output's listing, a.pvt.listingor the cache object at the recorded hash. - The listing object of a filtered hash is written only to the cache of the machine that ran the
stage (
StageLock.write), and only an untargetedpivot pushsends it. So for a directory input held by tracked data,verify --allow-missingasks for what restores that data (verify._tracked_input_objects), never that object. - An input hash taken from a record (a tracked directory's
.pvt, in explain's--allow-missingfallback) goes throughWorkspaceStore.recorded_input_hash, which filters the whole listing withcache.filter_listing. When that listing is missing, the.pvt's whole hash stands. - A dependency on a stage's directory output is the producer's whole output hash
(
explain.producer_output_hashes). push,fetchandpullof a target transfer one set,sync.get_target_hashes: whatpivot checkoutof the target restores, the objects its lock lists underreads, and a mapped stage instance's producer listing.ignore.pyports git 2.52'sadd_patterns_from_buffer,match_basename,match_pathnameanddowild.packages/pivot/tests/storage/test_input_ignore_matches_git.pycompares the filtered walk,filter_listingandcheck_ignorewithgit ls-fileson generated trees (setPIVOT_IGNORE_GIT_CASESfor more cases).
.pvt discovery¶
track.discover_pvt_files walks the whole project as os.walk does and reads every .pvt it
finds. The repro/run pre-flight (executor/core.py verify_tracked_files) records a memo: for
each directory that changed at least 2 s before the walk, its subdirectory and .pvt names under
its stamp (st_dev, st_ino, st_mtime_ns, st_ctime_ns). A later walk still stats each
directory and lists only those whose stamp changed, or changed in the last 2 s. A stamp whose
mtime and ctime are both whole seconds, or whose time does not fit signed 64-bit nanoseconds, is
never trusted. The memo is used only where storage/mounts.py allows it: ext2/3/4, XFS, Btrfs,
F2FS, ZFS, and tmpfs mounted with inode64.
- Only the pre-flight writes the memo, through its descriptor under an
flock, emptying it first: a walk that reads it meanwhile sees a prefix, which never parses. Every other caller only reads it; a memo that cannot be read means every directory is listed. It holds no.pvtcontent, and one recorded under another project root is not used. - The memo is a file of the running account's own,
state.lmdb/pvt-memo-<uid>in the state directory, mode 0600, because other accounts can read StateDB. A walk opens it without following a symlink and uses it only when anfstatshows a regular file on an allowed filesystem, owned by the running account, with no group or other bits and one link; otherwise it lists every directory and writes nothing. The next pre-flight replaces a memo its owner loosened; another account's file is never touched. - A listing in which an entry's type could not be read is never recorded.
A .pvt inside a stage's directory output can be the stage's own data rather than a tracked
file. Every command that discovers .pvt files passes member_skip.own_member_skip. The
command's own stages decide from the registry; any other .pvt is decided from the locks other
stages left (lock.recorded_directories, cached per lock file by state_dir.recorded_locks) and
their nocommit: records (ownership.read_stage_claims). pivot track refuses a path whose
sidecar a stage no pipeline declares records as its member (member_skip.orphaned_member).
remote/sync.py applies the same rule to the one .pvt it reads by path
(_get_file_hash_from_pvt).
Paths in both forms¶
A missing tracked path meets a stage's dependencies and outputs in both its forms, its absolute
normalized path and its resolved path (os.path.realpath), and Path.parts in a pygtrie.Trie
decides containment. storage/path_forms.py keeps the forms, the tries and the questions
(enclosing, held, overlapping). Its callers are engine/graph.py (_tracked_file_errors,
_restore_creates, tracked_overlap) and ownership.tracked_output_refusal; each docstring
says its order of forms and questions.
Lock and listing files¶
- A lock records a directory as
{listing, size, nfiles}on disk, not underhash, which a released pivot reads as one file;types.StorageLockDataholds the listing's hash underhash(lock._from_document,lock._to_document). A lock that records a directory keeps every entry underrecordsand leavesdepsandoutsempty (lock._shaped), so a released pivot reads it as recording nothing. Run-cache keys holdrun_history._DIRECTORY_RECORDfor the same reason; an older entry is found underworker.older_pivot_input_hash. - A lock whose
schema_versionis belowlock.SCHEMA_VERSIONis read as it is, with one warning per process; a newer pivot's lock reads as missing. - A restart proof (
lock.restart_proof_path) beside a lock an older pivot wrote means that pivot may have restarted an incremental output;lock.restart_proofsrefuses the stage inexplain.get_stage_explanation(status, verify) and in the engine before it schedules. - Every listing read follows
listing._resolve: a committed listing is taken only when it hashes to the recorded hash and equals the cache's copy, if there is one.listing.OnStalechooses what a stale one does:REFUSEforpivot verify,QUIETfor a stage the command does not run. Where a cache was read and could not give the listing, the refusal isListingUnavailableError(its docstring says where), which a restore underrepro --all --keep-goingtolerates (executor/tracked_restore.py); every other refusal is a plainCorruptListingError.
Objects read by hash¶
A lock lists the objects its run read with pivot.read_by_hash (pivot/reads.py) under a
top-level reads key, as do the run-cache entry (RunCacheEntry.reads) and the nocommit:
record.
- Every skip decision passes
reads.lock_unavailabletoskip.check_stage(status, explain, the engine's pre-check, the worker's tier 2; tier 1 declines on it). The check is local: an object the cache lacks counts as on the remote only whenremote:records it for the remote's URL. - A run that fetched an object returns it in
DeferredWrites.remote_hashes(reads.Recording.fetched), applied in the run's transaction. - When only unavailable reads make a stage stale and a remote may hold them
(
types.UnavailableReads.pullable),check_stagesetsRecovery.PULL_READS;pivot statussuggestspivot pullfrom that field. - A read names nothing until a lock does, so each commit fetches again any read object the cache
lost (
reads.fetch_missing) and, inside its naming unit, refuses to write a lock naming one that is still missing (reads.require_cached).
Output ownership¶
Pivot never removes a tracked file, an import or a .pvt sidecar to replace a stage output. The
user-facing rules and their limits are under pivot track.
- Checked before anything is removed: a stage write (a stage with several outputs checks them all
first,
worker._write_outputs_with_store→WorkspaceStore.refuse_output_replacements; thenWorkspaceStore.prepare_outputchecks each one just before clearing it), generation, lock and run-cache restores (WorkspaceStore.restore_from_cache), commits (WorkspaceStore.commit), an incremental output's restore,checkout,pull,get --forceand the presentation layer. - A stage write clears its old output with
cache.remove_output_checked, which renames a directory aside, checks it there (ownership.swap_refusal) and puts it back when the check refuses. ThenWorkspaceStore.refuse_output_replacement_at_writeruns before the loader writes. A symlink-mode commit does the same throughcache.store_hashed_output. - A forced restore makes one decision,
ownership.forced_restore_refusal, over targets built once (checkout._restore_targets,restore._restore_targets), and checks each target three times: with every target before any changes, under its restore lock before its replacement is built (checkout._restore_path_locked,restore._replace_checked), and at its swap (ownership.swap_refusal, passed tocache.restore_from_cacheasbefore_swap). - A swap's backup cleanup (
cache._remove_backup_left) catches onlyOutputOwnershipError, so every swap check raises that error itself (checkout._refuse_swap). - A stage's own
*.pvtmembers:ownership.stage_membersdecides from the lock, the hash a restore writes, what this run writes and thenocommit:record, read once per command (ownership.StageClaims). A run stages each entry that brings one beside the output (worker._staging_path), records it (ownership.record_members, which writesStateDB.add_no_commit_pvt_membersfrom the worker and raisesMemberRecordErrorwhen it cannot), then renames it into place (worker._save_recorded). A restore records the members of a hash the lock does not record (ownership.record_restored).
Cache objects¶
- Every file object is published by
cache.publish_cache_objectand every temporary entry made bycache.staged_cache_fileorcache._staging_in; the sweep of.pivot_tmp_*entries runs at most once per_CACHE_SWEEP_INTERVALper prefix directory. - A whole-directory tree (
cache.tree_path) is used unchecked only when its.content-checkedmarker holds the listing's bytes. Restores andcache.missing_cache_objectscheck it withrepair=False; only the commit path repairs (cache._ensure_tree_complete). - A directory restore starts from that tree only where it links the tree whole
(
cache.restore_tree: SYMLINK first, and links into the cache allowed or no COPY among the modes);cache.missing_cache_objects, whichpivot getandcheckout --forceask, reads the same rule, so where links are refused and COPY is among the modes it looks up each member's object, as the restore that copies it does. - A listing is also a cache object, at the directory's hash (
listing.store_object).
The naming lock and the register¶
What Keeps a Cache Object Alive
describes the protocol and the register as users see them. The writer side is
storage/naming.py; the collector, storage/gc.py, imports it. Writers import only naming, so
a worker never pays to import the collector.
- Naming-unit sites (
naming.naming_unit):executor/worker.py(commit and run-cache skip),executor/commit.py,cli/track.py,storage/restore.py(restore from a revision) andnaming.register_use.WorkspaceStore.prepare_commithashes and publishes before the unit (cache.publish_hashed_output);WorkspaceStore.finish_commitre-checks and links inside it. - The register and the lock files belong to the object store:
naming.store_layoutandnaming.register_rootfind them, and writers refuse anunsharedlayout (naming.unshared_store_problem) asgc.resolve_scopesdoes. Engine._orchestrate_executionregisters the project (naming.register_use) before any stage runs, so a project is in the register while its stages run. A restore that links objects outside a unit registers first, in the cache it restores from:config.register_cache_useforcheckoutandpull,restore_outputs_to_hashes(register=…)for a skipped stage, andverify_tracked_filesfor--checkout-missing. A restore inside a unit registers nothing more: a nested unit could wait on a collection that waits on the outer one.- Where a registration fails with
EROFS, the process makes no link into that cache (cache.refuse_links_from); a restore copies instead, or refuses when copy is not a checkout mode (cache.refuse_unregistered_link). - A collection reads the register with
gc.read_registerand checks every other cache a project lists withgc._other_caches_reason. It removes nothing through a path:gc.store_snapshotholds each store directory open, andgc.execute_planremoves through descriptors thatgc._open_directory_atchecked. - A record gc cannot fully interpret refuses the collection (
gc._lock_keys,gc._pvt_keysandgc._run_cache_keysraise_UninterpretableRecordError;gc.collect_rootsturns it intoProjectUnreadableError). The one exception is a.pvta stage wrote into its own output.