module

Arcana::Mail

Arcana as a machine's mail carrier (the dot-ai spec's Mail, in the user's dot.ai repository): what dot-ai's local carrier does, through the same files and locks (the spec's "The carrier's files"), so either can be the machine's carrier and a change between them moves nothing. Agents keep using dot-ai mail, which hands the carrier's part to arcana mail.

Which carrier carries a person's mail is git config's ai.carrier, read from the person's own config only (person_config): arcana makes Arcana the carrier; under any other (local, dot-ai itself, the default) Arcana stays out of mail, and writes no mail files (carries?). Each run reads it afresh.

What it prints matches dot-ai line for line, so dot.ai's tests (tests/dot-ai.sh, with CARRIER=arcana) check both carriers. Where it names a command, it names dot-ai's when dot-ai is installed (dot_ai), and its own otherwise: Arcana never needs dot-ai.

Where it goes further: a message for an address no project on this machine has goes over the Arcana bus (bus_sender), to an agent registered there.

Constants

AGENT = /\A@[a-z][a-z0-9-]*\z/
DOT_AI_SCRIPT = File.join("skills", "dot-ai", "scripts", "dot-ai")

dot-ai's command, below a version of its Claude Code or Codex plugin, or below their home for a skill installed by hand.

HEADER = /\A(?:By|Held|Re|Sent|Id|Post): /
MAX_DEPTH = 4

How far below a search root a project may sit.

NAME = "arcana"

This carrier's name: ai.carrier's value for it, and what Via: and Sent: … via say.

OUTBOUND = /\A[0-9]{8}-[0-9]{6}Z-[@#].*\.md\z/
PERSON_KEYS = {"ai.carrier", "ai.path"}

The settings that are a person's, not a project's (the dot-ai spec's Config).

POST_FILE = /\A[0-9]+-.*\.md\z/
STALE_LOCK = 2.minutes

A lock older than this is a crashed run's.

TOPIC_NAME = /\A[a-z][a-z0-9-]*(?:\.[a-z][a-z0-9-]*)*\z/

Class methods

address_of(dir : String) : String | Nil
Source
ai_paths
Source
bus_sender(root : String) : BusSend

Sends mail from the project at root over the bus at ARCANA_URL, as the MCP bridge's arcana_deliver would: from the project's handle, to be read when the agent gets to it, with the message's id and Re:, the request signed with the project's key and the message too, so its recipient can check who sent it. The subject is the outbound name's topic.

Source
carrier

The person's carrier, local when unset.

Source
carrier?

Whether Arcana is the person's carrier.

Source
carries?

Whether Arcana carries mail, and so writes mail files (inbox files, sent copies): where it's the carrier, or where there's no dot-ai to carry it (dot_ai?). Only the carrier writes them.

Source
configured?(dir : String) : Bool

A project's address: @ plus its config's handle, or its directory's name folded (AgentConfig). Nil when the config can't be read or its handle isn't one. Whether the project at DIR is set up for an agent: its .ai/ has a config. Without one, whatever folders its .ai/ holds, no agent is there to read mail, so none is delivered to it (dot-ai's spec, Mail: Delivery).

Source
deliver(root : String, io : IO, warn : IO | Nil = nil, bus : BusSend | Nil = bus_sender(root)) : Nil

Delivers the outbox of the project at root, one line per message to io: delivered (posted, for a topic), or left in the outbox and why (the spec's Delivery, under the outbox's lock). A message for an address no project here has goes to bus.

Source
dot_ai

How an agent runs dot-ai (0.5 or later), for what Arcana tells it: dot-ai when it's on the PATH (dot-ai install), else the newest copy the dot-ai plugin installed for Claude Code or Codex, or one installed by hand as a skill, run with sh. Nil when there's none, and Arcana names its own commands instead.

Source
dot_ai?

Whether dot-ai is installed at all, in any version: dot_ai, or an older one (dot-ai-mail) installed for Claude Code or Codex. Without it, nothing but Arcana carries mail between projects.

Source
follows(root : String) : Array(String)

The topics the project at root follows: mail.follow in its config.

Source
has_id?(dir : String, id : String) : Bool

Files with a name ending in -<id>.md in dir, hidden ones aside.

Source
inbox(root : String) : Array(String)

The inbox of the project at root, oldest first.

Source
inbox_text(m : Outgoing, post : Int32 | Nil = nil) : String

An inbox file's or post's text: header lines, a blank line, then the message.

Source
layout(text : String) : Tuple(Array(String), String)

An outbound file's header lines, each with its newline, and its message: what follows them and the blank line that ends them.

Source
lock(path : String) : Bool

Takes the lock path, a directory, so making it is atomic; one older than STALE_LOCK is a crashed run's, and is cleared.

Source
lookup(map : Array(Tuple(String | Nil, String)), address : String) : Tuple(String | Nil, String | Nil)

The project with address: {directory, nil}, or {nil, why not}.

Source
message_id(sender : String, name : String, message : String) : String

A message's id: the first 8 hex digits of the SHA-256 of the sender's address, the outbound file's name and the message.

Source
person_config(key : String, path : Bool = false) : Array(String)

A per-person setting's values: from the person's own git config (--global, which takes in $XDG_CONFIG_HOME/git/config), else the system's, never a repository's, which a plain git config --get would let win. With path, ~ expanded.

Source
place_of(root : String, topic : String) : Int32

The project at root's place in topic: the last post it read.

Source
post_number(name : String) : Int32

A post's number, from its file name.

Source
posts_after(dir : String, n : Int32) : Array(String)

The posts in the topic folder dir after number n, oldest first.

Source
project_root(dir : String) : String | Nil

The project at or above dir: the nearest directory, dir included, holding .ai/ (through a link, too). Nil when there's none.

Source
re_ok?(re : String) : Bool

Whether a Re: line names something: a message's id (8 or more hex digits) or a topic's post (#topic:number).

Source
receipts(root : String) : Array(String)

The receipts for the mail the project at root sent: a line for each message read, or one that can't be any more, whose line is dropped. A line whose project doesn't start with / is another carrier's, for a recipient elsewhere, and is left. Left for next time while a delivery holds the outbox.

Source
repo_settings(dir : String) : Array(String)

What a repository at or above dir sets of the per-person keys, which is ignored: a warning line for each.

Source
scan(root : String, warn : IO | Nil = nil) : Array(Tuple(String | Nil, String))

Every project under the search roots, as {address, directory}, sorted by directory; the address is nil when the project's handle can't be read. Symbolic links aren't followed, and hidden directories and node_modules/ aren't searched. A root that doesn't exist is reported to warn.

Source
search_roots(root : String) : Array(String)

Where to look for projects: ai.path, else the outermost project enclosing root's, else root's parent directory.

Source
set_place(root : String, topic : String, n : Int32) : Nil

Moves the project at root's place in topic to n.

Source
topic_home(root : String) : Tuple(String | Nil, String | Nil)

Where this machine's topics are kept: .ai/topics/ of the topic home, the first ai.path directory, else the outermost project at or above root. {path, nil}, or {nil, why there's none}.

Source
topic_ok?(name : String) : Bool
Source
unlock(path : String) : Nil
Source
write_hidden(dir : String, name : String, text : String) : Bool

Writes text to dir/name through a hidden name, renamed into place, so no one reads half of it. False when it can't.

Source

Nested types