annals.store#
The document store: a flat set of documents, tags, groups and a recycle bin, as plain files.
Layout under the data root (local or remote, see annals.target):
docs/<id>/meta.json one document: metadata
docs/<id>/<main file> its content (index.html, report.md, ...), plus any assets
docs/<id>/.versions/<n>/ an earlier version, whole (its own meta.json and files)
docs/<id>.r<stamp>-<hex>/ a replacement not applied yet (``publish(replace=<id>)``)
trash/<id>/... the same shape; the recycle bin is a move
groups/<gid>.json a named, ordered list of document ids with its own URL
Flat on purpose: agents from many corpora do not share a hierarchy, and every placement
decision costs tokens. Tags and groups carry the structure. Ids are time-sortable and
readable (20261003-203301-starwars-theme-report-3f9a), so a listing is already in
publication order and a URL says what it points to.
Plain files, written whole, because the same tree is written by rsync from another machine and read by the server; there is no index to keep consistent.
Living documents (one link that stays current) are a replace: the new files are written as
a pending replacement next to the document, never over it, and whoever reads the store
applies it (DocStore.settle()): the current files move under .versions/<n>/ and
the replacement takes the id. Writing beside rather than over is what lets a replace travel
through an outbox (a directory another machine pulls and empties): the pending directory is
just another new directory to pull, and the store on the reading side applies it. Applying
is a sequence of renames, each claimed atomically, so two readers never apply the same
replacement twice and nothing is ever deleted.
Module Attributes
how much of a file is read to find its title and excerpt |
|
hidden files, caches, compiled python |
|
a published folder's own content skips hidden names, and a document that carries one anyway is refused) |
|
|
|
|
|
|
|
its work is finished or undone |
|
how long a group write waits for another writer of the same group, and how often it looks |
|
a group lock is held for milliseconds, so one older than this is a dead holder's (the lock's note carries it, so recovery on another machine knows) |
|
the longest title a group may have (a longer one is refused, not cut) |
|
a reader that misses a document being swapped waits for it this long, in steps |
|
a reader that misses a document being swapped waits for it this long, in steps |
|
a replacement is written under this hidden name, then renamed into place whole |
|
the store's bookkeeping lives there |
|
the interactive version of a page lives there, and the document's page links out to it |
|
where the page's thumbnails are cached, under the data root (beside the documents, never inside one) |
|
timed text published beside an audio file, named |
|
|
|
everything a reader's page needs, and nothing else, so a field added later stays out until named here |
Functions
|
|
|
Raise |
|
A title as the page shows it: control and invisible format characters dropped, whitespace collapsed. |
|
True when |
|
The rendering kind of a file, from its extension. |
|
|
|
|
|
UTC timestamp with second precision, the format every meta.json uses. |
|
Only the fields of |
|
Lowercase ascii words joined by hyphens; empty input becomes |
|
|
|
A title (markdown |
|
|
Classes
|
Publish, list, read, trash, restore and group documents on a |
|
The documents of |
|
The documents of |
- annals.store.ARTIFACT_HOSTS = ('claude.ai',)#
the interactive version of a page lives there, and the document’s page links out to it
- Type:
hosts an artifact link (
publish(artifact=...)) may point at
- annals.store.DFLT_EXCLUDE = ('.*', '__pycache__', '*.pyc', '*.pyo')#
hidden files, caches, compiled python
- Type:
names never published from a directory
- class annals.store.DocStore(target, *, settle=True)[source]#
Bases:
objectPublish, list, read, trash, restore and group documents on a
Target.- add_to_group(gid, doc_ids)[source]#
Append documents to an existing group, keeping order and skipping ones it has.
Each document must exist and not be in the bin;
KeyErrorfor a group that is not there.- Return type:
- doc_rel(doc_id)[source]#
The directory of a document (or of a version,
<id>@<n>) under the data root.- Return type:
- group_docs(gid)[source]#
A group and the meta of each of its documents, in its order (missing or binned ones left out).
- holds(ref_id)[source]#
Whether a document (or a version of one) or a group with this id is here, in the bin or not.
- Return type:
- list(*, trash=False)[source]#
Every document’s meta, last changed first (
updated, elsecreated).Pending replacements are applied first (when this store settles). One whose document is not here is listed under its own id, so it is never out of sight.
- make_group(title, doc_ids, *, gid=None, live_only=True)[source]#
Create (or overwrite) a group: a titled, ordered list of documents.
The title is cleaned (control characters dropped, whitespace collapsed, at most
MAX_GROUP_TITLElong, not blank); repeats are dropped, keeping the first; each document must exist and not be in the bin unlesslive_only=False(a bundle that keeps a document later binned).ValueErrorsays which rule failed. An explicitgidrewrites that group, under its lock.- Return type:
- meta(doc_id, *, settle=True)[source]#
One document’s meta;
in_trashsays which side of the bin it is on.<id>@<n>is versionn: its own meta, withversion_ofnaming the document. A pending replacement, read by its own id, sayspending.settle=Falseskips applying pending replacements (for a hot path such as a raw file).- Return type:
- pending(doc_id=None, *, names=None)[source]#
Pending replacements (directory names), oldest first; of one document if given.
names: the listing ofdocs/when the caller already has it.
- publish(*sources, title=None, tags=(), source=None, text=None, filename='document.md', exclude=('.*', '__pycache__', '*.pyc', '*.pyo'), replace=None, artifact=None, pdf=True, pdf_renderer='default')[source]#
Publish files (or a directory, or
text) as ONE document; return its meta.One markdown or html file is the common case. A directory is published whole, with
index.html(or the first renderable file) as the page shown.textpublishes a string asfilenameinstead of reading sources.excludelists glob patterns skipped inside a directory (hidden files and caches by default;()keeps all).replace(a document id) publishes a new version of that document instead of a new document: same id, same link, the current files kept as a version. It is written as a pending replacement and applied at once when this store settles (otherwise where the store is read). The returned meta sayspendinguntil then.artifact(a claude.ai artifact link, seecheck_artifact_url()) is the interactive version of the page: the document’s page links out to it. A replacement that names none keeps the document’s current one.An html page gets its own Download PDF button, and
<stem>.pdfprinted beside it bypdf_rendererunless it has one (annals.pagepdf, which says how a page declares or opts out).pdf=Falseleaves the page as it is.- Return type:
- read_text(doc_id, rel=None)[source]#
A document’s main file (or one of its files) as text.
- Return type:
- rename(doc_id, title)[source]#
Give a document a title of the owner’s choosing; a later version keeps it.
The title is cleaned as a group’s is (
clean_title()). A version or a pending replacement is refused (rename the document). Works in the bin too. Onlytitleandrenamedchange in the stored meta, which is read again under the document’s lock: a document that moved meanwhile (trashed, restored, purged) is aRuntimeError, never a meta written where its files are not. Returns its meta.- Return type:
- rename_group(gid, title)[source]#
Give a group a new title (cleaned as at creation); its documents stay as they are.
- Return type:
- search(query, *, trash=False)[source]#
Case-insensitive match of every query word against title, tags, excerpt, source.
- annals.store.GROUP_LOCK_STALE_S = 30#
a group lock is held for milliseconds, so one older than this is a dead holder’s (the lock’s note carries it, so recovery on another machine knows)
- annals.store.GROUP_LOCK_WAIT_S = 10#
how long a group write waits for another writer of the same group, and how often it looks
- annals.store.HEAD_CHARS = 4000#
how much of a file is read to find its title and excerpt
- class annals.store.HidingStore(store, *, hide)[source]#
Bases:
DocStoreThe documents of
storewithout thosehide(meta)is true of, on the same target.A hidden document is absent, not refused: it is not listed or found,
meta()raisesKeyErrorfor it, its versions and its pending replacements (so whatever reads a document through the store, its page, files, versions or a share, answers as for a missing one), and groups read through this store leave it out. An API built over this store therefore cannot show it anywhere, whichever route asks;hides()lets what keeps records of its own about documents (shares) hide those records too. Reads go throughstore, so a subclass’s reading still applies; writes reach the same target, and a group is always rewritten from its whole list, so hiding never deletes anything.- add_to_group(gid, doc_ids)[source]#
Append to the group’s whole list (the hidden ids stay in it); a hidden document is refused as a missing one.
- Return type:
- hides(doc_id)[source]#
Whether
doc_idis a document of the whole store that this one hides.- Return type:
- list(*, trash=False)[source]#
Every document’s meta, last changed first (
updated, elsecreated).Pending replacements are applied first (when this store settles). One whose document is not here is listed under its own id, so it is never out of sight.
- meta(doc_id, *, settle=True)[source]#
One document’s meta;
in_trashsays which side of the bin it is on.<id>@<n>is versionn: its own meta, withversion_ofnaming the document. A pending replacement, read by its own id, sayspending.settle=Falseskips applying pending replacements (for a hot path such as a raw file).- Return type:
- annals.store.INCOMING_PREFIX = '.incoming-'#
a replacement is written under this hidden name, then renamed into place whole
- annals.store.LOCK_PREFIX = '.lock-'#
<root>/.lock-<id>/is held while a replacement of<id>is applied; it is also the working directory, holdingnext(the replacement) andold(the version being archived) between renames, andlock.json(what a recovery needs to know)
- annals.store.LOCK_STALE_S = 600#
its work is finished or undone
- Type:
a lock older than this was left by a crash
- annals.store.MAX_GROUP_TITLE = 200#
the longest title a group may have (a longer one is refused, not cut)
- annals.store.PENDING_SEP = '.r'#
<id>.r<stamp>-<hex>is a pending replacement of<id>
- annals.store.PROVENANCE_KEYS = ('source', 'artifact', 'sessions')#
source(the publisher’s host, cwd, session and--metakeys),artifact(a claude.ai link only the owner can open) andsessions(the sessions that made it, with their claude.ai links:annals.sessions).copyto a room drops them from what it writes.- Type:
what a document’s meta says about where it came from
- annals.store.RESERVED = ('.versions',)#
the store’s bookkeeping lives there
- Type:
names a document’s own files may not use
- annals.store.ROOM_META_KEYS = frozenset({'created', 'excerpt', 'files', 'id', 'in_trash', 'kind', 'main', 'size', 'sizes', 'tags', 'title', 'updated', 'version', 'version_of', 'versions'})#
everything a reader’s page needs, and nothing else, so a field added later stays out until named here
- Type:
the fields of a document’s meta an outside audience may see (a room)
- class annals.store.RoomStore(store, *, keys=frozenset({'created', 'excerpt', 'files', 'id', 'in_trash', 'kind', 'main', 'size', 'sizes', 'tags', 'title', 'updated', 'version', 'version_of', 'versions'}))[source]#
Bases:
HidingStoreThe documents of
storeas an outside audience reads them (a room).Nothing in the bin: a binned document is absent, as in
HidingStore, so the owner withdraws a document from a room by binning it; a group left with no document to show is absent too (its title included). And only the fields inROOM_META_KEYS: every meta read through this store (one, a listing, a search, a group’s) comes without the rest, so whatever an API builds from it cannot show or match them. Writes go tostoreunchanged. Reading may still settle a pending replacement the publisher left, as the owner’s store does.- list(*, trash=False)[source]#
Every document’s meta, last changed first (
updated, elsecreated).Pending replacements are applied first (when this store settles). One whose document is not here is listed under its own id, so it is never out of sight.
- meta(doc_id, *, settle=True)[source]#
One document’s meta;
in_trashsays which side of the bin it is on.<id>@<n>is versionn: its own meta, withversion_ofnaming the document. A pending replacement, read by its own id, sayspending.settle=Falseskips applying pending replacements (for a hot path such as a raw file).- Return type:
- annals.store.SIDECAR_SUFFIXES = ('.cues.json', '.chapters.vtt', '.vtt', '.srt')#
timed text published beside an audio file, named
<stem><suffix>in the same folder (the suffix in any case): a transcript (.cues.json,.vttor.srt) and chapters (.chapters.vtt). The page plays the audio with them (app.js’sSIDECARS); a document of one audio file and its sidecars is an audio document, not a folder. (Video is left out until its page shows them.)
- annals.store.SWAP_STEP_S = 0.02#
a reader that misses a document being swapped waits for it this long, in steps
- annals.store.SWAP_WAIT_S = 2.0#
a reader that misses a document being swapped waits for it this long, in steps
- annals.store.THUMB_CACHE = 'cache/thumbs'#
where the page’s thumbnails are cached, under the data root (beside the documents, never inside one)
- annals.store.VERSIONS = '.versions'#
a published folder’s own content skips hidden names, and a document that carries one anyway is refused)
- Type:
where a document keeps its earlier versions (hidden
- annals.store.VERSION_SEP = '@'#
<id>@<n>names versionnof a document;@is not an id character
- annals.store.check_artifact_url(url, *, hosts=('claude.ai',))[source]#
urlif it is anhttpslink to one ofhosts(or a subdomain), else raise.The link is shown on the document’s page as a button, so only a plain https URL to a known artifact host is accepted: no other scheme, no credentials, no port, a host of dns labels only, printable ascii without quotes, angle brackets or backslashes.
- Return type:
- annals.store.check_id(doc_id)[source]#
Raise
ValueErroron an id that could not have come frommk_id().- Return type:
- annals.store.clean_title(title)[source]#
A title as the page shows it: control and invisible format characters dropped, whitespace collapsed.
ValueErrorwhen blank or longer thanMAX_GROUP_TITLE.- Return type:
- annals.store.is_sidecar_of(path, media)[source]#
True when
pathis timed text formedia:talk.vttortalk.cues.jsonfortalk.mp3, in the same folder (paths compare whole), the suffix in any case.>>> is_sidecar_of("a/talk.CHAPTERS.vtt", "a/talk.mp3"), is_sidecar_of("b/talk.vtt", "a/talk.mp3") (True, False)
- Return type:
- annals.store.kind_of(filename)[source]#
The rendering kind of a file, from its extension.
- Return type:
- annals.store.mk_id(title, *, when=None)[source]#
YYYYMMDD-HHMMSS-<slug>-<4 hex>: sortable by time, readable in a URL.- Return type:
- annals.store.mk_pending_id(doc_id, *, when=None)[source]#
<id>.r<YYYYMMDDHHMMSSffffff>-<hex>: sorts in publication order.- Return type:
- annals.store.now_iso()[source]#
UTC timestamp with second precision, the format every meta.json uses.
- Return type:
- annals.store.room_meta(meta, *, keys=frozenset({'created', 'excerpt', 'files', 'id', 'in_trash', 'kind', 'main', 'size', 'sizes', 'tags', 'title', 'updated', 'version', 'version_of', 'versions'}))[source]#
Only the fields of
metaa room shows (ROOM_META_KEYS).>>> room_meta({"id": "a", "source": {"host": "h"}, "from_pending": "p", "versions": [{"n": 1, "from_pending": "p"}]}) {'id': 'a', 'versions': [{'n': 1}]}
- Return type:
- annals.store.slugify(text, *, max_len=48)[source]#
Lowercase ascii words joined by hyphens; empty input becomes
doc.- Return type:
- annals.store.split_version(doc_id)[source]#
("<id>", n)from<id>@<n>(versionn),("<id>", None)from an id.