# annals

annals: an in-tray for AI agents. Publish documents and media to a private page; get a link.

Agents call [`annals.tools.publish()`](annals.tools.html.md#annals.tools.publish) (or `annals publish file.md` from a shell) and hand
the printed URL to their human, who opens it on a phone. The documents are plain files in a
directory, local or on another machine over ssh; `annals.api.mk_app()` serves that
directory as a private page with search, sort, groups and a recycle bin.

### Functions

| [`configure`](#annals.configure)(\*[, target, base_url, to, outbox, ...])   | Write the publisher config (where to publish, what link to print) and show it.                                                                                    |
|-------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`group`](#annals.group)(title, doc_ids, \*[, add_to, target, ...])     | Make a group (one URL for a set of documents) from existing document ids.                                                                                         |
| [`groups`](#annals.groups)(\*[, target, base_url, to])                   | List groups, newest first.                                                                                                                                        |
| [`load_settings`](#annals.load_settings)(\*[, target, base_url, to, ...])       | Resolve settings: arguments, then `to`, env, the config file, then local defaults.                                                                                |
| [`ls`](#annals.ls)([q, trash, limit, target, base_url, to, ...])     | List documents (newest first), optionally filtered by words or showing the bin.                                                                                   |
| [`parse_target`](#annals.parse_target)(spec)                                   | `host:/path` becomes an [`SshTarget`](#annals.SshTarget); anything else a [`LocalTarget`](#annals.LocalTarget). |
| [`publish`](#annals.publish)(paths, \*[, title, tags, group, ...])        | Publish one document and print its link; several paths become several documents.                                                                                  |
| [`restore`](#annals.restore)(doc_ids, \*[, target, to])                   | Bring documents back from the recycle bin.                                                                                                                        |
| [`show`](#annals.show)(doc_id, \*[, target, base_url, to])             | A document's metadata, link, the groups it is in, and the annals links in its text.                                                                               |
| [`trash`](#annals.trash)(doc_ids, \*[, target, to])                     | Move documents to the recycle bin (restorable).                                                                                                                   |

### Classes

| [`DocStore`](#annals.DocStore)(target, \*[, settle])                | Publish, list, read, trash, restore and group documents on a `Target`.      |
|------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
| [`LocalTarget`](#annals.LocalTarget)(root)                             | A directory on this machine.                                                |
| [`Settings`](#annals.Settings)(target, base_url[, configured, ...]) | Where to publish and what link to print.                                    |
| [`SshTarget`](#annals.SshTarget)(host, root)                         | `host:path` on another machine, via the system ssh and rsync in batch mode. |

### *class* annals.DocStore(target, , settle=True)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

Publish, list, read, trash, restore and group documents on a `Target`.

#### add_to_group(gid, doc_ids)

Append documents to an existing group, keeping order and skipping ones it has.

Each document must exist and not be in the bin; `KeyError` for a group that is not
there.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### doc_rel(doc_id)

The directory of a document (or of a version, `<id>@<n>`) under the data root.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

#### group(gid)

One group, by id.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### group_docs(gid)

A group and the meta of each of its documents, in its order (missing or binned ones left out).

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]]

#### groups_of(doc_id)

The groups a document is in, newest first, each as `{"id", "title", "n"}`.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### holds(ref_id)

Whether a document (or a version of one) or a group with this id is here, in the bin or not.

* **Return type:**
  [`bool`](https://docs.python.org/3/builtins/functions.html#bool)

#### list(, trash=False)

Every document’s meta, last changed first (`updated`, else `created`).

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.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### list_groups()

Every group, newest first.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### make_group(title, doc_ids, , gid=None, live_only=True)

Create (or overwrite) a group: a titled, ordered list of documents.

The title is cleaned (control characters dropped, whitespace collapsed, at most
`MAX_GROUP_TITLE` long, not blank); repeats are dropped, keeping the first; each
document must exist and not be in the bin unless `live_only=False` (a bundle that
keeps a document later binned). `ValueError` says which rule failed. An explicit
`gid` rewrites that group, under its lock.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### meta(doc_id, , settle=True)

One document’s meta; `in_trash` says which side of the bin it is on.

`<id>@<n>` is version `n`: its own meta, with `version_of` naming the document.
A pending replacement, read by its own id, says `pending`. `settle=False`
skips applying pending replacements (for a hot path such as a raw file).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### pending(doc_id=None, , names=None)

Pending replacements (directory names), oldest first; of one document if given.

`names`: the listing of `docs/` when the caller already has it.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

#### 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')

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. `text` publishes a
string as `filename` instead of reading sources. `exclude` lists 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 says `pending` until then.

`artifact` (a claude.ai artifact link, see `check_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>.pdf` printed beside
it by `pdf_renderer` unless it has one ([`annals.pagepdf`](annals.pagepdf.html.md#module-annals.pagepdf), which says how a
page declares or opts out). `pdf=False` leaves the page as it is.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### purge(doc_id)

Delete a document that is in the bin. Refuses one that is not.

* **Return type:**
  [`None`](https://docs.python.org/3/builtins/constants.html#None)

#### read_text(doc_id, rel=None)

A document’s main file (or one of its files) as text.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

#### rename(doc_id, title)

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. Only `title`
and `renamed` change in the stored meta, which is read again under the document’s
lock: a document that moved meanwhile (trashed, restored, purged) is a
`RuntimeError`, never a meta written where its files are not. Returns its meta.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### rename_group(gid, title)

Give a group a new title (cleaned as at creation); its documents stay as they are.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### restore(doc_id)

Move a document out of the bin (idempotent).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### search(query, , trash=False)

Case-insensitive match of every query word against title, tags, excerpt, source.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

#### settle(doc_id=None)

Apply pending replacements (of one document, or all); return the ids replaced.

A replacement whose document is not here yet (it is still on its way, or the
document was purged) stays pending and is listed under its own id.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

#### trash(doc_id)

Move a document into the bin (idempotent).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### versions(doc_id)

Every version of a document, oldest first, the current one last.

* **Return type:**
  [`list`](#annals.DocStore.list)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]

### *class* annals.LocalTarget(root)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

A directory on this machine.

`read_metas()` keeps each meta it parsed, keyed by its file’s inode, mtime, ctime
and size (every write replaces the file, so a new inode), and resolves the root once
per listing, not twice per file: listing a thousand documents costs a `stat` per
document, not a parse. It hands back copies, so a caller may change what it gets.

#### local_path(rel)

`rel` under the root, resolved; `None` if it escapes the root (a symlink).

* **Return type:**
  [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path) | [`None`](https://docs.python.org/3/builtins/constants.html#None)

### *class* annals.Settings(target, base_url, configured=True, outbox=False, share_base_url='', room=False)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

Where to publish and what link to print. See the module docstring for precedence.

#### as_dict()

Plain dict, for the CLI and MCP surfaces.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

#### configured *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= True*

publishing then lands
in the local data dir and the link points at a local server, which is rarely meant

* **Type:**
  False when nothing (argument, env, config file) named a target

#### outbox *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= False*

the target is pulled and emptied by another machine; replacements apply there

#### room *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= False*

what is written to it carries no provenance

* **Type:**
  an outside audience reads this target

#### share_base_url *: [str](https://docs.python.org/3/builtins/stdtypes.html#str)* *= ''*

where share links point (`<this>/<token>/`); `<base_url>/shared` by default

### *class* annals.SshTarget(host, root)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

`host:path` on another machine, via the system ssh and rsync in batch mode.

### annals.configure(, target=None, base_url=None, to=None, outbox=False, share_base_url=None, room=False)

Write the publisher config (where to publish, what link to print) and show it.

### Example

`annals configure --target tw:/root/.local/share/annals --base-url https://apps.example.com/annals`.
`--to NAME` saves the target and base url as a named target (`annals publish --to NAME`)
and leaves the default alone. `--outbox` marks the target as an outbox: a directory
the server pulls and empties (it cannot be written to from here), so replacements are
left for the server to apply; it is kept by later changes (`ANNALS_OUTBOX=0` or an
edit of the file turns it off). `--share-base-url` is where share links point when
the share server is not at `<base-url>/shared` (a platform running it as its own
public app). `--room` marks a target an outside audience reads (a client’s room): what
is published or copied to it carries no provenance; kept by later changes, like
`--outbox`. With no arguments, shows the resolved settings without writing.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.group(title, doc_ids, , add_to=None, target=None, base_url=None, to=None)

Make a group (one URL for a set of documents) from existing document ids.

With `add_to` (a group id) the documents are added to that group instead, and `title`
is ignored. The documents must exist and not be in the bin; the title is cleaned (see
[`annals.store.clean_title()`](annals.store.html.md#annals.store.clean_title)).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.groups(, target=None, base_url=None, to=None)

List groups, newest first.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.load_settings(, target=None, base_url=None, to=None, outbox=None, share_base_url=None, room=None, config_path=None)

Resolve settings: arguments, then `to`, env, the config file, then local defaults.

`to` names a `[targets.NAME]` table of the config file; it replaces the environment
and the file’s default target for this call (explicit arguments still win).

* **Return type:**
  [`Settings`](annals.config.html.md#annals.config.Settings)

### annals.ls(q='', , trash=False, limit=50, target=None, base_url=None, to=None, since=None, session=None, tag=None, meta=None)

List documents (newest first), optionally filtered by words or showing the bin.

`to` lists a named target instead of the default one. The rest are exact filters,
all of which must hold, for tools that poll: `since` (an ISO date or time, or an age
like `6h` or `2d`: published or replaced at or after it), `session` (the publishing session’s
name, whole, case-sensitive), `tag` (one tag, whole: the owner’s own tags, set by `annals tag`, win over the publisher’s) and `meta` (`key=value`,
several allowed, against the document’s `source`: `corpus=annals`). A row is
`{id, title, kind, tags, created, updated, source, url}`; with `--json` that is the contract
(`misc/docs/design.md`), and `total` counts the matches before `limit`.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.parse_target(spec)

`host:/path` becomes an [`SshTarget`](#annals.SshTarget); anything else a [`LocalTarget`](#annals.LocalTarget).

* **Return type:**
  [`Target`](annals.target.html.md#annals.target.Target)

### annals.publish(paths, , title=None, tags='', group=None, add_to=None, session=None, session_url=None, all_files=False, target=None, base_url=None, to=None, replace=None, artifact=None, no_link_check=False, no_pdf=False, meta=None, upsert=None)

Publish one document and print its link; several paths become several documents.

An audio or video file takes its timed text along as one document: `talk.mp3
talk.vtt talk.chapters.vtt` (or `talk.cues.json`) is one audio document whose page
shows the transcript and chapters (`annals.store.SIDECAR_SUFFIXES`).

`paths`: files of any kind (markdown, html, images, video, audio, pdf, text), a
directory (one document: an `index.html` or single page with its assets, else a
gallery of everything in it), or `-` for stdin (markdown). `tags` is comma separated. `group`
names a group to create from the published documents; the reply then carries
`group_url` too. `add_to` appends them to an existing group instead (its id, as
printed in a group link), so a second batch lands under the link the owner already has. `session` records who published (the source shown on the page), and `session_url`
the claude.ai link that opens that session; by default the link of the Claude Code session
this runs in (`CLAUDE_CODE_BRIDGE_SESSION_ID`), `none` for no link. The document’s page
shows each session that made it, linked, under its title; a share or a room never does
([`annals.sessions`](annals.sessions.html.md#module-annals.sessions)). A directory skips
hidden files and caches (`.*`, `__pycache__`, `*.pyc`) unless `all_files`.
`to` publishes to a named target of the config file (`[targets.NAME]`, see
`annals configure --to`) instead of the default one. `replace` (a document’s link or
id) publishes ONE path as a new version of that document: the link stays the same, the
page shows the new version and offers the earlier ones. That is how a living page (a
guide, a status page) keeps one link. `artifact` (a claude.ai artifact link) is the
interactive version of the page: the document’s page shows an “Open the interactive
version” button. Publish the Artifact first, then the same page here with its link.
A markdown or text document that mentions an issue or a Slack/email/chat message
without linking to it gets a warning on stderr ([`annals.linkcheck`](annals.linkcheck.html.md#module-annals.linkcheck)); it is
published either way. `no_link_check` turns that check off.

An html page gets its own Download PDF button, inside the page, so it survives “open
full page”, raw links and shares, and `<stem>.pdf` printed beside it (headless
Chromium, the `pdf` extra; without it the button opens the print dialog). A page that
already has its PDF says so with `<link rel="alternate" type="application/pdf"
href="...">`; one with its own button marks it `data-annals-pdf`; `no_pdf` (or
`data-annals-pdf="off"` on `<html>`/`<body>`) leaves the page as it is
([`annals.pagepdf`](annals.pagepdf.html.md#module-annals.pagepdf)).

`meta` (`--meta key=value`, one flag per pair: `--meta corpus=x --meta issue=3`) records what the document belongs to, next
to `host`, `cwd` and `session` in its `source`: by convention `corpus` (the
repository or body of work), `repo`, `issue` (the issue it answers), `item` (a
desk item) and `kind` (report, brief, overview, render); other keys are kept as given.
`ls --session/--meta` finds them again. The CLI’s `--json` prints the result below
(`id`, `title`, `url`, and with a group `group_id` and `group_url`; several
paths give `docs`) as JSON and nothing else on stdout; that shape is a contract
(`misc/docs/design.md`).

`upsert` (`--upsert key=value`) makes the call idempotent for a living document
keyed by a meta value: it publishes ONE path as a new version of the newest live
document whose `source.<key>` equals the value (as `replace` would), or creates
one with that `key=value` in its `source` when there is none. The reply then says
`"created": true` or `false`, and `group` / `add_to` apply only when the
document is new. It takes the place of `replace`; not together.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.restore(doc_ids, , target=None, to=None)

Bring documents back from the recycle bin.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.show(doc_id, , target=None, base_url=None, to=None)

A document’s metadata, link, the groups it is in, and the annals links in its text.

`doc_id` may be the document’s link, on any annals this machine is configured for: the
link finds its own place (`to` names one explicitly). A group’s link gives the group
and its documents’ links. `links` are the links to other annals documents and groups
found in the document’s text files, on any annals this machine can read: follow them
with `annals show` or `annals aggregate`. To read the text, use `annals aggregate <link>`.

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### annals.trash(doc_ids, , target=None, to=None)

Move documents to the recycle bin (restorable).

* **Return type:**
  [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)

### Modules

| [`aggregate`](annals.aggregate.html.md#module-annals.aggregate)       | Many markdown files as ONE markdown document: a title, a table of contents, a section per file.                                                    |
|------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------|
| [`auth`](annals.auth.html.md#module-annals.auth)                 | The auth seam: who may read the annals.                                                                                                            |
| [`bulk`](annals.bulk.html.md#module-annals.bulk)                 | Bulk actions: one action over a selection of items, in one request.                                                                                |
| [`catalog`](annals.catalog.html.md#module-annals.catalog)           | The catalog: every source's items as one list, with search and the owner's tags.                                                                   |
| [`combine`](annals.combine.html.md#module-annals.combine)           | Many files of many kinds as ONE document: a Markdown, html or PDF file.                                                                            |
| [`config`](annals.config.html.md#module-annals.config)             | Settings: where documents are published and what link to print.                                                                                    |
| [`docfilter`](annals.docfilter.html.md#module-annals.docfilter)       | Exact filters over a document's meta, for a caller that polls: since, session, tag, source.                                                        |
| [`highlights`](annals.highlights.html.md#module-annals.highlights)     | Each reader's highlights: passages of documents kept with a name, tags and a note.                                                                 |
| [`jsonstore`](annals.jsonstore.html.md#module-annals.jsonstore)       | A `MutableMapping` kept in one JSON file, safe with several server processes.                                                                      |
| [`linkcheck`](annals.linkcheck.html.md#module-annals.linkcheck)       | Find what a document mentions without linking to it: the owner reads on a phone and clicks through.                                                |
| [`listening`](annals.listening.html.md#module-annals.listening)       | What each reader has heard of each audio document: percent heard, listens, where they stopped.                                                     |
| [`mcp`](annals.mcp.html.md#module-annals.mcp)                   | MCP surface: the same operations as the CLI, from [`annals.tools`](annals.tools.html.md#module-annals.tools), via `py2mcp`. |
| [`media`](annals.media.html.md#module-annals.media)               | The media relay for published html pages: what a framed page plays, told to the page around it.                                                    |
| [`notebook`](annals.notebook.html.md#module-annals.notebook)         | A share's documents as separate notebook sources: one plain web page per text file.                                                                |
| [`originals`](annals.originals.html.md#module-annals.originals)       | Originals: a pick's files, exactly as published, as one streamed `.zip`.                                                                           |
| [`overview`](annals.overview.html.md#module-annals.overview)         | Spoken overviews of a document: the request a page makes, and the worker that fulfils it.                                                          |
| [`pagepdf`](annals.pagepdf.html.md#module-annals.pagepdf)           | Every published html page carries its own Download PDF button, and the PDF beside it.                                                              |
| [`prefs`](annals.prefs.html.md#module-annals.prefs)               | Each reader's page preferences: a few named sections of JSON, kept on the server.                                                                  |
| [`records`](annals.records.html.md#module-annals.records)           | The published documents as records for a retrieval index, so an agent can find one by meaning.                                                     |
| [`references`](annals.references.html.md#module-annals.references)     | The references of documents, extracted and combined, in three forms.                                                                               |
| [`render`](annals.render.html.md#module-annals.render)             | Render markdown (and other page-like files) to html and PDF, with their images inside.                                                             |
| [`requirements`](annals.requirements.html.md#module-annals.requirements) | Optional dependencies, checked where they are needed and explained when they are missing.                                                          |
| [`search`](annals.search.html.md#module-annals.search)             | How the catalog answers a query: the search strategies.                                                                                            |
| [`sessions`](annals.sessions.html.md#module-annals.sessions)         | The sessions that made a document: each one's name and the link that opens it.                                                                     |
| [`share`](annals.share.html.md#module-annals.share)               | Share links: one document (or a group) behind a public, unguessable, revocable link.                                                               |
| [`share_page`](annals.share_page.html.md#module-annals.share_page)     | A share link's page with its text in it, and no script, so every reader can read it.                                                               |
| [`sources`](annals.sources.html.md#module-annals.sources)           | Item sources: everything the page lists, wherever it lives, as rows of one shape.                                                                  |
| [`store`](annals.store.html.md#module-annals.store)               | The document store: a flat set of documents, tags, groups and a recycle bin, as plain files.                                                       |
| [`tables`](annals.tables.html.md#module-annals.tables)             | Scrolling, zooming tables for published html pages.                                                                                                |
| [`tags`](annals.tags.html.md#module-annals.tags)                 | The owner's tags: a `MutableMapping` from item key to tag list, kept apart from the items.                                                         |
| [`target`](annals.target.html.md#module-annals.target)             | The transport seam: a document tree lands in a directory, here or on another machine.                                                              |
| [`titles`](annals.titles.html.md#module-annals.titles)             | A title for a file whose name says nothing: `overview.mp3`, `output.html`, `index.md`.                                                             |
| [`tokens`](annals.tokens.html.md#module-annals.tokens)             | Read tokens: a credential for an agent that cannot reach the store (a cloud session).                                                              |
| [`tools`](annals.tools.html.md#module-annals.tools)               | The operations, as plain functions: JSON-able arguments in, JSON-able dicts out.                                                                   |
