How the plugin works where the reasoning is not obvious from the code. The
README describes what the plugin does; AGENTS.md states the rules a change has
to honour. This file explains why a few of those rules look the way they do,
for a reader who wants to understand the design before changing it.
A Chatto thread gets its own Hermes session, and that session starts empty. Meanwhile two kinds of message never become a turn of their own:
So a thread can contain messages that no session holds. The reported case: a cron job posts a build report into a channel, its echo is filtered, and an hour later someone replies to it. The thread it roots is new, its session is empty, and the agent would answer a message it cannot read.
The fix is to hand the thread's earlier messages to the session — but only the ones it has not seen. Re-delivering the whole thread on every turn would fill the context with a conversation the session already holds.
That "only what it has not seen" boundary is a cursor: one opaque message ID per thread, meaning the session holds this thread's history up to here. Two facts about it drive everything else:
[Earlier in this thread] line beside a conversation that
already contains it.after=<cursor>) returns the
oldest messages past the cursor, walking forward. So the window and the
thing being delivered are the same window, and a thread that outgrew
THREAD_CONTEXT_MESSAGE_LIMIT keeps answering — where an unanchored page put
the cursor outside the window, left no tail to find inside it, and muted the
thread for good.Two consequences ride along. The root is not part of an anchored page (the
server sets IncludeRoot false whenever after is set), and it is not needed
either — everything up to the cursor is already in the session — so the extra
root fetch is skipped: one RPC fewer per turn. And a backlog bigger than the
window drains over turns instead of arriving at once: each turn takes the next
slice and stops there, so nothing is skipped and nothing repeats.
The cursor lives twice: in _seeded_threads for the running process, and on the
session (chatto.thread_seed_cursor) so it survives a restart.
An earlier version of this code did not persist it, on the grounds that
set_session_metadata rewrites the whole routing index per call. Instead it
rebuilt the partition from the fetch the turn makes anyway:
the newest message that is not ours is the last one the session certainly holds, because every other person's message arrives as its own turn
That premise is false, and the fetch it reasons over is the shape a thread reply always produces: the window ends with the turn's own trigger — a person's message that became this turn seconds ago. So the rebuilt partition lands on the trigger, the tail comes out empty, and every bot message before it is skipped. After a restart the agent stops seeing its own output in a thread, which is the one thing the whole feature exists to prevent.
Excluding the trigger does not rescue it. The previous turn's trigger is just as much a person's message that became a turn, so the next restart skips again. A session's frontier is not a function of the message list at all: every candidate message is either a turn the session holds (person) or one it does not (bot), and the list alone cannot tell a past trigger from a bot message — only the stored frontier can.
Persisting costs a full routing-index rewrite, so the write is spent only where it buys something — which is not every turn:
_unpersisted_threads and retried, because a cursor that never
lands is exactly the cursor a restart needs.One case has no cursor anywhere. A thread's first turn seeded nothing — a reply that opens a fresh thread has no earlier messages — so there was nothing to write; there was no session to write it to either; and the process restarted before the second turn. The thread is blind exactly once.
The blind pass re-delivers the bot's own messages, leaves people's messages to their own turns, and records the frontier so no later process is guessing. It can duplicate one block, in the rarer variant where that first seed did carry own output. Losing the output instead is the common case and permanent, so the bounded duplicate is the cheaper error.
The adapter derives session keys of its own — to find the cursor, to find the
turn an edit should cancel — and a derived key is worthless unless it is
byte-identical to the one the gateway built. So it delegates to
_source_session_key instead of calling build_session_key itself. That matters
more than it looks: ingress runs before the runner stamps the profile onto the
event, so a key built without the profile namespace puts every bot of a
multiplexed gateway into agent:main: and names a session nobody is running.
get_thread_events, capped at
20 messages. Quiet threads skip it — the cursor means there is nothing to
fetch. It used to be two: the unanchored page omits the thread root, so the
root was fetched separately on top.[Earlier in this thread] is reference text, not trusted input: only the
trigger passes the auth gate.AGENTS.md owns the rules here — one field, three coordinated names, every path parses by kind. What is not obvious from them is which configuration a tool call sees.
The gateway multiplexes several serving profiles through one process, so a
process-wide cache would hand one profile another profile's credentials. Cached
configurations are keyed by the profile scope Hermes itself uses, and secrets come
from the scope the gateway installed rather than from os.environ — under
multiplexing the environment holds the launch profile's values, not the serving
one. A tool call that resolved its config from the environment would quietly answer
with the wrong bot's token, which reads as a permissions error on the server.
That is also why a configuration that resolved without a token is not cached at all. Caching it would pin "token not configured" for the life of the process, so a later call in a properly scoped context would keep being served the degraded configuration long after its scope came back — a failure that persists exactly as long as nobody restarts the gateway.
_Mentions answers one question — did this address us, or somebody else — and both
admission gates read only that. The interesting part is where the answer comes from,
because the two inbound paths are not equally informed.
The posted path asks the server. Chatto resolves mentions when a message is
created and reports the targets on the event (MessagePostedEvent.mentions).
includes_viewer is the server's own answer to "did this target include me", fixed
at post time and unaffected by membership since. Three properties make this better
than anything we could compute:
all/here/role entry,
never one per recipient — so recognising them needs no room roster at all.@name as plain text" is
therefore not a rule we implement; it is what an empty list already means.@moderator reaches a
bot that holds the moderator role, and there is no handle to match it against.The web frontend does the same thing in packages/chatto-client
(addressedMessage: message.mentions.some(m => m.includesViewer)), so this is
the platform's own answer rather than a second opinion.
The edited path reads the text. MessageEditedEvent carries only room_id
and message_event_id, and the hydrated Message has no mention field — the
mention resolution was deliberately kept out of the message resource. There is no
payload to read, so the fallback re-derives candidates from the body with the same
regex as before.
Why re-gate at all, when Chatto does not? FDR-006 decision 4 states that
mentions are post-time facts and that editing never re-resolves them. The frontend
honours that by treating an edit as a timeline reconciliation — store.ts refreshes
the window and returns, and only messagePosted feeds addressedMessage. We
deliberately do not: a user who types a message, forgets the @mention, and adds it
by editing expects an answer, and that is a promise this plugin makes. Chatto's rule
is uncontroversial in a client that has no turn for an edit to open.
What that costs. The fallback is a text reading, not a resolution, so it inherits
the regex's blind spots: a @bob inside a URL, a blockquote, a link label or a link
destination reads as a mention. The same text posted rather than edited reaches the
server path and is classified correctly. Two paths that answer the same question
differently is exactly the drift risk this design usually avoids; the containment is
that they converge on one type, and the only place that fills it differently is the
one place where no better information exists.
A user cannot tell whether the agent replied on its own or through a tool, so the
two paths must not behave differently. Both go through _deliver_chunks, which
owns the split at the 9900-character limit, the routing of a split answer as one
answer (the first chunk opens or joins a thread, later chunks carry that root),
and the stop-at-first-failure.
The callers differ only in what they do with the result: send() shapes it into a
SendResult, _post_chunks into a refusal that names which chunk failed and
lists the IDs that went out — so a retry repeats only what is missing instead of
duplicating a message the room already has.