Proof libraries & OpenTheory

Operating a library

This page covers the library directory as a running system: what is in it, how readers and writers share it, how to recover from a crash, how space is reclaimed, and what each error means.

The directory

<library>/
  CURRENT                 the current manifest's hash: 64 lowercase hex digits and a newline
  LOCK                    present only while a writer or the collector holds the library
  manifests/<hash>.jlm    one manifest per version, never changed
  parts/<hash>.jlp        partitions, never changed, shared between versions

Every manifest and partition is named by the SHA-256 of its bytes, so sha256sum parts/<hash>.jlp checks a file by hand, and copying a library with any file tool keeps it valid. A reader refuses a file whose bytes do not hash to its name. Two sub-partitions with the same bytes are one file, which the manifest names at both positions, so parts/ can hold fewer files than the manifest has partitions. A writer's temporary files are named .<hash>.tmp and CURRENT.tmp. The writer never follows a symbolic link inside the library, and a reader refuses a CURRENT that is one.

The library format specifies the directory and the files for implementers; ST7.1 is the normative layout; ST7.3 and ST9 give the formats of the two kinds of file.

Readers and writers

Readers (Library.open, snapshot, recheck, profile_snapshot, and a session's statement-only import) take no lock. They read CURRENT once and then read only immutable files, so a reader sees one complete version whatever writers do at the same time. Any number of readers can run concurrently with each other and with a writer. Library.is_current says whether CURRENT still names the version a Library was opened at.

Writers (Library.create and LibraryChange.publish) take LOCK, created exclusively. One writer at a time: a second one is refused with LOCKED and changes nothing; retry it when the first has finished. A writer also states its base version, the version the change was opened at, and is refused with CONFLICT if CURRENT moved. Retry a CONFLICT with change.rebase() and publish again (changing a library).

A publication writes and flushes every new file before it replaces CURRENT with an atomic rename, so a crash at any point leaves a library that loads: either the old version or the new one, complete.

After a crash

A writer that crashes may leave:

  • unreferenced partition or manifest files, which are harmless;
  • temporary files, which a later writer of the same file replaces;
  • LOCK, which makes every later writer fail with LOCKED.

A stale LOCK must be removed by hand, because a writer that is merely slow cannot be told from a dead one. LOCK holds the writer's process id, start time and a random token. Remove it only when you know the process that held it has stopped: two live writers could both replace CURRENT (ST7.2).

Old versions and space

Each version keeps its manifest, and partitions that no version needs any more stay on disk. ST7.2 specifies a collector that reclaims them under LOCK: it keeps the current manifest, a chosen number of its ancestors, every further ancestor that was current within the last hour (a reader may still be opening it), and every partition those manifests name, and removes other manifests and partitions older than one hour and all temporary files. The Store implements the collector and unlock internally, and its public API does not export them. Without them, keep every file: a reader of an older version needs its manifest and partitions, and a partition is shared between versions. Readers and writers never remove anything but their own temporary files.

The cumulative withdrawal list of a version (manifest section 6) is carried into every later version.

Budgets and limits

Every load takes a termbank.Budget, a count of work steps that it spends and never exceeds. When the budget runs out, the operation raises LIMIT and changes nothing: no session state, no library file that a version names, no CURRENT.

Other bounds apply whatever the budget:

Bound Value
a manifest or partition file 64 MiB
one sub-partition's estimated row size, before it is cut 2²⁴ bytes (ST9.1)
article input and objects ArticleLimits (importing articles)

Readers never allocate from a count read from a file before checking it against the bytes that remain.

Errors

Every failure is a StoreError with a kind, a message and, for a failure inside an input, an optional byte offset. Match on error.kind against the StoreErrorKind constants, never on the message text:

try:
    _ = change.publish()
except error:
    if error.kind == StoreErrorKind.LOCKED:
        ...                                   # another writer is working: retry later
    elif error.kind == StoreErrorKind.CONFLICT:
        change.rebase()                       # move the change onto the new version
        _ = change.publish()
    else:
        raise error^

String(error.kind) gives the kind's name.

Kind What it means What to do
FORMAT malformed bytes, or a library rule broken: two providers of one statement (Rule L1), a section that differs from what the load recomputes, a library without CURRENT, a version that is not CURRENT or an ancestor of it for your own publication, fix the input; for a library you load, it is damaged or was written by a non-conforming writer
DIGEST a file's bytes do not hash to its name, a block's digest or a Merkle root differs, or a container checksum fails restore the file from a copy; never rename it
IDENTITY a declaration's bytes do not have its stated identity the file is damaged
KERNEL the checker refused a row of an article being added fix or drop the article
LIBRARY a row of a loaded version that the checker refuses; a library over another or an unknown profile; a refused change (a cited withdrawal, a replacement that cites its theorem, a name that is not a theorem name, articles mixed with session theorems, a second publish) read the message; after a checker or profile change, re-check the library
REPLAY a session theorem that does not replay, or one of its rows refused, at publication check the export
VERSION another termbank format or library format version, such as a library written before format 2.2; a platform other than Linux x86-64 or 64-bit MacOS write the library again from its articles or sessions with this build; for judgment_mathlib, fetch the pinned version
MISSING_DEPENDENCY no library directory; an unknown statement; a session theorem citing a declaration the base does not hold check the path and the key; add the article the theorem imports first
LIMIT the budget or an input limit ran out retry with a larger budget or limit, if the input is trusted
LOCKED another writer holds LOCK retry when it has finished; after a crash, see above
CONFLICT CURRENT is not your base, your LOCK was removed while you wrote, a session theorem's name names another statement, or create over an existing library rebase the change and publish again
IO an operating-system file operation failed, or a manifest or partition could not be read retry; check permissions, free space and missing files

A failed load, re-check or publication leaves your session and the library unchanged. A publication that fails after it replaced CURRENT says so in its message: the new version is current and complete.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub