module

Arcana::Hook

arcana hook <event> [--agent claude|codex]: what the Claude Code and Codex session hooks run, so both agents register, check mail and sign off the same way. Reads the hook's JSON payload on stdin.

  • session-start: registers the project's handle (from .ai/config.yml in the nearest directory with .ai/, see AgentConfig.project_root) under its owner token, and tells the agent, in its context, who it is on the bus and that a "held" handle is its own.
  • stop: asks the agent to read waiting mail before it stops, once per change in the count.
  • prompt: as the user's prompt goes to the agent.
  • At stop and prompt, a session whose project now has another handle than the one it registered (a new .ai/ appeared above where it started), or that registered none (the bus was down at session start, say), registers the project's handle and tells the agent (follow_project).
  • session-end: moves unread mail into .ai/mail/inbox/ as notes (when the project has .ai/), then marks the handle offline. The handle stays registered, so mail sent between sessions waits for the next agent.

Mail between projects is the person's carrier's (git config's ai.carrier, see Mail). Where Arcana is the carrier, the hooks also do what dot-ai's do (Mail::Notices): deliver the outbox, tell of the inbox, followed topics' new posts and receipts, and keep the session's record (Mail::Sessions). Where Arcana carries mail (Mail.carries?), mail from agents still waiting on the bus is written into the project's inbox as inbox files (drain_mail), so the agent meets all mail the same way, through the inbox. Under another carrier, only that carrier writes mail files: messages on the bus stay there, and the Stop hook says how many are waiting.

A hook must never break the session: a bus that is down, or any other error, means no output and exit 0.

Constants

EVENTS = {"session-start", "stop", "prompt", "session-end"}

Class methods

drain(bus : Rest, cfg : AgentConfig) : Array(String)

Move unread mail into .ai/mail/inbox/, one note per message (see the dot-ai skill's mail conventions). Returns the paths written. Only in a project set up for an agent (Mail.configured?): in one without a config, nobody would read the files, so the mail stays on the bus for the next session.

Source
drain_mail(bus : Rest, cfg : AgentConfig) : Array(String)

Mail from agents waiting on the bus for the project (not service replies or topic notices, which an agent may be waiting to take itself) is written into its .ai/mail/inbox/ as inbox files, one at a time, so whatever brought a message, the agent meets it in the inbox. Returns the paths written.

Source
follow_project(bus : Rest, cfg : AgentConfig, session : String | Nil) : String | Nil

When this session's project has a handle other than the one it registered, as when it started below a folder that had no .ai/ yet and so registered an enclosing project's, or the session registered none (the bus was down at its start, or its config couldn't be read): registers the project's own handle, under its owner token and key (made if need be, as at session start), and returns what to tell the agent. Once per change; nil when nothing changed or the bus can't be reached (tried again next time).

Source
mail_body(payload : JSON::Any | Nil) : String | Nil

A message as a mail file holds it, after the header lines and their blank line: its text, then anything else the payload carries as a JSON block. Nil for an empty payload. The sender's copy in sent/ (MCP#write_sent_file) and the recipient's inbox file hold the same.

Source
mail_flag(handle : String) : String
Source
mail_root(start : String) : String | Nil

The project whose mail Arcana carries: the one at or above start (Mail.project_root), when Arcana is the machine's carrier and the project has .ai/mail/. Nil otherwise.

Source
mail_topic(subject : String | Nil) : String | Nil

A subject as a file name's topic (the dot-ai spec's File names), as dot-ai makes one: lowercased, each run of anything but a-z and 0-9 turned into one -, except a . between digits (landing-v0.8.2), trimmed of -, and cut to 50 characters. Nil when nothing is left.

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

A message's id (dot-ai): the first 8 hex digits of the SHA-256 of the sender's address, the outbound file's name and the message as its file holds it (mail_body), joined by newlines. The same on the sender's copy and the recipient's, whatever carries the message, so anyone with the sent copy can compute it again.

Source
prompt(bus : Rest, project_dir : String, agent : String, output : IO, mail : String | Nil = nil, session : String | Nil = nil) : Int32

As the user's prompt goes to the agent: a new handle, when the session's project changed (follow_project), and with mail (mail_root) the receipts (Mail::Notices), added to its context. Receipts never keep the agent going on their own, so they wait for the next turn.

Source
record_session(id : String | Nil, root : String | Nil, agent : String, state : String, start : Bool = false) : Nil

Writes the session id's record, where Arcana is the carrier of the project at root (mail_root).

Source
run(args : Array(String), input : IO = STDIN, output : IO = STDOUT, error : IO = STDERR, base_url : String = ENV["ARCANA_URL"]? || "http://127.0.0.1:19118") : Int32

Returns the process exit code.

Source
sent_name(mail_dir : String | Nil, to : String, subject : String | Nil, time : Time) : String

The name a message sent over the bus has in sent/ (dot-ai): the time it was written in UTC, the recipient and the subject as a topic (20261017-120000Z-@ipck--message-ids.md), or <time>-<address>.md without a subject. A name already in use in mail_dir's drafts, outbox or sent gets -2, -3 and so on. Without mail_dir (no mail/ to file in), the name the copy would have, from which the id is still taken.

Source
session_end(bus : Rest, project_dir : String, payload : JSON::Any, mail : String | Nil = nil, session : String | Nil = nil) : Int32
Source
stop(bus : Rest, project_dir : String, agent : String, payload : JSON::Any, output : IO, error : IO, mail : String | Nil = nil, session : String | Nil = nil, id : String | Nil = nil) : Int32

Keeps the agent going once per change in the count, so mail it leaves for later never loops it: Claude by exit 2 (stderr is shown to the agent), Codex by decision: block.

Where Arcana carries mail (Mail.carries?), a project with .ai/mail/inbox/ has its bus mail written there instead (drain_mail). Then, with mail (mail_root), Arcana tells what dot-ai's Stop hook would (Mail::Notices) and keeps the session's record, and otherwise nothing: the carrier tells of the inbox.

Source
write_note(inbox : String, env : JSON::Any) : String

A mail file: header lines, a blank line, then the message. Named time first (dot-ai), by the time it was sent in UTC, the sender, the subject as a topic, and the message's id: 20261013-190539Z-@ipck--dot-ai-0.3-b6c3b174.md, or <time>-<sender>--<id>.md without a subject. The id is the one the sender carried with the message (message_id); only a message that came without one, from an older Arcana or another client, is given the first 8 characters of its correlation id. A reply's Re: comes along. Written here at session end, and by the MCP bridge's arcana_receive (see MCP#write_inbox_files). write_sent files the sender's copy.

Source
write_sent(sent : String, name : String, message : String, time : Time, id : String, re : String | Nil = nil) : String

The sender's copy of a message sent over the bus, filed in sent/ under name (sent_name) as local delivery files the outbox's: a Re: line if it answers something, Sent: and Id:, a blank line, then the message (mail_body).

Source

Nested types