The MOCA Reader for Python: open a package from a directory, a .moca archive
or a host-supplied source; check it; compute its digest; verify attestations
and evidence; resolve members; answer structure operations; produce citation
records; and search with one entry point over lexical, dense-sidecar, store or
hybrid backends.
It implements the Reader class of the Reader contract, follows the Reader interface with Python names, passes the whole conformance corpus, and returns the same ranked results as the reference Reader, moca-reader-typescript.
pip install moca-readerPython 3.11 or later. Getting started takes you from install to a cited search result and a conformance run in five steps.
from moca_reader import Library, read_package
pkg = read_package("support-kb.moca", trust_root="trust-root.json")
print(pkg.valid, pkg.digest, pkg.capabilities)
library = Library().add(pkg)
for record in library.search("refund window", audiences=["public"]):
print(record["node"]["title"], record["node"]["ref"], record["score"])A malformed package never raises: pkg.valid is False and pkg.diagnostics
says why. The Reader never reaches the network, or any file outside the
package, because a package names it. Citation records are plain dicts, the
same JSON every MOCA Reader produces (citation-record.schema.json).
| Name | Purpose |
|---|---|
read_package(target, *, trust_root, resolve_member, strict, limits, known_profiles, now) |
Read and check a package: a path, a .moca/.zip path, archive bytes (bytes, bytearray or memoryview, read in place: do not change them during the read), or a host source with list() and read(path). The result's terms holds the creator's terms as written, their problems and their authority (contract §15.1). |
Library(clock=, overlays=, limits=) |
Holds verified packages, with bound sidecar chunks: add, citations, get, search, structure. |
Search(source, backend, audiences=, hooks=, max_candidates=, terms_limits=) |
One entry point over a backend: search(query, limit=, include_all=, locale=, concepts=, scope=, up_to=, terms=). The gate always runs last. terms is the host's terms request (contract §15.4): with it, the gate also removes records whose creator's terms do not allow the use, and each record carries its resolved terms; without it, terms remove nothing. terms_limits are the host's own floors. source is a Library, or a StoreScope(package_states, clock) for a store backend: package_state(result) for each loaded package, so every library- and trust-dependent field is recomputed (contract §9.4). |
map_time(record, from_source, to_source, t) |
The same moment on another media description of the record's node: switch between the audio and video versions of an episode (package spec §5.7). None when it is not there. |
LexicalBackend, DenseBackend, MemoryStoreBackend, HybridBackend |
BM25 over package text or sidecar chunks; sidecar vectors with a host embedder (S006, S007); a reference in-memory store; Reciprocal Rank Fusion of a lexical and a dense backend. |
Embedder |
The protocol a host embedder follows: name, version, dimensions, query_prefix, document_prefix, embed(texts, role). |
moca_reader.embedders |
MOCA's four standard embedding models run locally (standard_embedder("bge-small-en-v1.5")), with the onnx extra. |
SearchHooks, ontology_guided(), diversify(per_node=) |
Retrieval hooks: host code, never package content. |
bind_sidecar(target, pkg) |
Check a sidecar against a package and return usable chunks. |
directory_resolver(dirs) |
A simple member resolver over folders. |
TrustRoot |
A host trust root, from a path or a dict. |
flat_metadata(record) |
The consuming guide's flat moca_* fields for a citation record, for vector stores. |
terms_request(destination=, conditions=, terms=) |
A terms request for an app that hands records to a model: ai at provider unless it declares device or host. The framework bindings apply one to every search. |
library.structure |
concept, requires, required_by, parts, narrower, broader, related, sequence, descendants, nodes. |
The API is synchronous.
Separate packages, each a thin adapter over the Reader (ADR-0002):
| Package | What it gives you |
|---|---|
moca-reader-langchain |
MocaRetriever: a LangChain retriever returning Documents |
moca-reader-llamaindex |
MocaRetriever: a LlamaIndex retriever returning scored nodes |
Both wrap a Search you build, so the gate and your audience filter always
apply, with a terms request for a third party's model unless you declare
destination="device" or "host". Each result's metadata carries the whole
citation record and the consuming guide's flat moca_* fields. flat_metadata(record) gives you
the same fields for your own vector store.
With the onnx extra, moca_reader.embedders runs MOCA's four standard
embedding models locally, the way every MOCA tool does, so a query embedded
here matches an index built with moca-index --model:
pip install "moca-reader[onnx]"
npx @openmoca/moca-embed models add bge-small-en-v1.5 # once: download and check the modelfrom moca_reader import DenseBackend, HybridBackend, LexicalBackend
from moca_reader.embedders import standard_embedder
embedder = standard_embedder("bge-small-en-v1.5") # files checked against their pinned SHA-256
hybrid = HybridBackend(LexicalBackend(library), DenseBackend(library, embedder))Models are read from ~/.moca/models (or MOCA_MODELS). The Reader never
downloads them.
The built-in lexical backend is BM25 (k1 1.2, b 0.75) over node text, or over sidecar chunks when a sidecar is bound. Every MOCA Reader tokenises the same way, so they return the same results:
- NFKC-normalise, then lower-case each code point (simple case mapping).
- Letters, digits and combining marks make words; anything else separates them. Words in scripts with vowel signs, such as Devanagari, stay whole.
- Chinese, Japanese, Thai, Lao, Khmer and Myanmar text is split into each character (with its marks) and each pair of neighbouring characters.
- No stemming. A host that needs it supplies its own lexical backend.
- Without a locale, every language of a node is searched and its best match returned; with one, the representation package spec §5 selects.
Characters added in very recent Unicode versions can tokenise differently across Readers, because each language runtime ships its own Unicode data.
As Reader contract §14 asks:
| Class | Reader |
| Contract version | 0.7.0 |
| Implemented | Archives, host sources, DSSE attestations, sidecars (lexical and dense), the ontology profile, creators' terms and the terms gate |
| Limits | The contract §3 defaults (moca_reader.source.DEFAULT_LIMITS, set with limits=), with at most 256 attestation files of 1 MiB each. JSON and frontmatter nested more than 64 levels deep (fixed, contract §3) are malformed |
| Encryption | read_package(..., decryptor=AGE_DECRYPTOR, key_provider=...), with AGE_DECRYPTOR from moca_reader.age (on cryptography, already a dependency). Without a decryptor, an envelope is recognised and reported with E001, and nothing inside it is exposed. bind_envelope_sidecar opens an envelope's index.age the same way. Decrypted bytes stay in memory, and are zeroed once read, so an opened envelope's result.source is None |
| Sigstore | Offline only, with the sigstore extra (pip install moca-reader[sigstore]), against the host trust root's sigstore.trustedRoot. Without the extra, or without a trusted root, a *.sigstore.json attestation is unverifiable (A003). There is no online mode: refresh the trusted root yourself (moca-spec attestations §6) |
The conformance adapter ships with the package. From a moca-spec checkout:
node scripts/run-conformance.mjs --reader "python -m moca_reader.conformance"You need uv. The tests read moca-spec's examples
and run its conformance corpus. Check out
moca-spec beside this repository, or
set MOCA_SPEC_DIR. CI uses the tags pinned at the top of
.github/workflows/test.yml (MOCA_SPEC_REF and the like); check out the same
tags to match it.
uv sync
uv run pytest
uv run ruff check && uv run ruff format --check
uv run mypytests/parity/parity.mjs runs 18 search scenarios (87 queries) over
moca-spec's examples through this Reader and the reference Reader, and fails
on any difference in records, order, spans, text or scores. Dense sidecars use
a deterministic hashing embedder, so no model is needed. With
moca-reader-typescript and moca-tools (after npm ci) beside moca-spec:
node tests/parity/parity.mjs- Add a dated section to CHANGELOG.md and update
versioninpyproject.tomland__version__insrc/moca_reader/__init__.py. - Point the
MOCA_*_REFpins in.github/workflows/test.ymlat the release tags (moca-spec docs/releasing.md). - The tests, corpus and parity pass.
- Merge to main, tag
vX.Y.Zon main and push the tag. The release workflow (.github/workflows/release.yml) checks that every version matches the tag, builds the three packages, and publishes them to PyPI through trusted publishing:moca-readerfirst, then its bindings, which release at the same version. No PyPI token is kept anywhere. - Publish a GitHub release whose notes are the changelog section.
What this Reader uses, and how strongly. Follow means we implement it and the tests check it where they can; map to means only an informative mapping, in the specification.
| Standard | Where | How strongly |
|---|---|---|
Open Knowledge Format v0.2, pinned at commit ad30107 |
content/ is an OKF bundle |
follow (pinned) |
| YAML 1.2, core schema | frontmatter; aliases and tags outside the core schema are refused | follow |
| JSON Schema 2020-12 | the vendored schemas, byte-identical to moca-spec's | follow |
| Ed25519 (RFC 8032) and in-toto Statement v1 in DSSE envelopes | package and review attestations | follow |
| Sigstore bundles | the sigstore extra: offline only; issuer and subject compared exactly |
follow |
| age v1 (X25519 and scrypt recipients) | moca_reader.age: encrypted packages, decrypt only |
follow |
| SKOS and Dublin Core terms in RDF 1.1 Turtle | structure.ttl |
follow (the named terms) |
| WebVTT | transcripts that evidence cites | follow |
| BCP 47 | language, locales, search locales |
follow |
| RFC 3339 | dates, validity windows, datePackaged |
follow |
| SPDX licence expressions | license, and a node's licence in citation records |
follow (exposed as written) |
| MARC relator codes | credit roles |
follow (exposed as written) |
| schema.org | datePublished, contentRating, typicalAgeRange, copyright |
map to |
| ONNX via ONNX Runtime | the onnx extra: the standard embedding models |
follow |
Built with the help of AI assistants; every change is reviewed and accepted by the maintainer.
Apache-2.0. See LICENSE and NOTICE.
OpenMOCA™ is a trade mark of PI & Mash Solutions Ltd. The licence covers the code, not the name: see the trade mark policy.