Meet Jev: one multiple-choice question that stops a chat spawning nine agents
Misha's iterate mind is a git repository of code and the agents that code runs, and every message that reaches it has to be handed to one of those agents.
Nine messages in eight minutes made nine agents. Four pairs of them were working at the same time, three relearned the same API call from each other's logs, and one contradicted another. Everyone is building multi-agent systems at the moment; this is the failure that never makes it onto the architecture diagram. Nothing was wrong with any single agent. The mistake was treating each message as a separate job: until this change, every message started a fresh agent, which rebuilt what it needed from the chat's last few lines. That is simple and stateless, and it falls apart as soon as someone sends several short messages about the same thing.
The fix is called Jev.
The question
Jev is a small classification model (typesafe/jev, on the mind's AI binding), and it sits in front of the agents. For each incoming message it answers one multiple-choice question: which of these conversations does the new message belong to?
The options are built in code, not chosen by Jev:
- every agent that was active with the same audience in the last two hours, up to five;
- the agent whose message this one is a reply to (a chat reply, or a mail's
In-Reply-To), however old; NEW: it is about something none of these is about.
Each option is a few sentences of plain description: how long ago that agent was last active, whether it is waiting for an answer and what it asked, what it has been about, and its latest exchanges. Jev never sees an agent's path, only labels and words that a person could also use to decide.
function routingRequest(message: Message, active: Agent[], now: number) {
const options = active
.filter((a) => a.audience === message.audience && now - a.lastAt < TWO_HOURS)
.slice(0, 5);
const criteria: Record<string, string> = {};
options.forEach((a, i) => {
criteria[`agent_${i + 1}`] =
`Last active ${ago(now - a.lastAt)}${i === 0 ? " (the last to speak)" : ""}. ` +
(a.waitingFor ? `Waiting for an answer: ${a.waitingFor}. ` : "Waiting for nothing. ") +
`About: ${a.about.join("; ")}. Latest: ${a.exchanges}`;
});
criteria.NEW = "About something none of these is about: a new thread";
return { options, question: { type: "choice", criteria }, background: RULES };
}
The audience filter matters as much as the descriptions. A message in a private chat is only ever routed among that chat's conversations, never into one happening in a shared group, whatever Jev thinks.
NEW is the safe answer
The routing step can fail in several ways: no candidates, the model errors, the model answers something that is not one of the labels, or it takes too long. Every one of those resolves to NEW, which is exactly the old behaviour. With no candidates there is no call at all; with candidates, an answer that has not arrived within three seconds is NEW.
That gives the feature a floor. At worst it costs three seconds and behaves as before; it can never strand a message.
The decision is also stored before anything acts on it, keyed by the incoming event, and logged on the chosen agent with the options it was given and the probability of each. A redelivered event goes where the first delivery went, and when a message lands somewhere odd, the log shows what Jev saw and how sure it was.
Tuning the wording against cases
The rules in the question's background were chosen against real message sequences and against invented ones for situations the real ones lacked: a bare "yes" while two agents each have a question open, a reply that changes the subject, a question about the whole to-do list. Each rule is one sentence. For example, a short answer belongs to the conversation that is waiting for one, the one that asked most recently, unless it replies to another; and a question about the list as a whole is a new thread.
The same day, two more cases were measured and fixed by adding words, not code:
- A message that only makes sense against one conversation, such as picking one of two options an agent had offered, joined that conversation with a probability of only 0.54. One rule, that a message picking an option a conversation offered belongs to it, took it to 0.99.
- Thanks after two jobs had both finished split about evenly between them. A rule that thanks closes the conversation that most recently did something, plus marking the most recent option "the last to speak", moved it to the last one, from 0.60 to 0.75, while thanks that named the other job still went there.
Every earlier scenario was re-run with each change, and all still routed correctly. The probabilities are what make this kind of tuning possible: a correct answer at 0.54 is a bug waiting to happen, and you can see it before it happens.
The bookkeeping has to be consistent
Routing is only as good as what it reads. On its first live run, a "yes" found no conversation to join, though the agent it answered had just asked a question. The records that say which agents are active and what they are waiting for were kept in the mind's key-value store, which is eventually consistent between the places where intake and the relays run. A list answered empty, and kept answering empty for a minute after the write. A stale read had also dropped a line from a chat's history.
Within the hour, all of it moved into one Durable Object: where each agent answers, each chat's last lines, each agent's conversation record, which agent owns each message, and each input's routing. It answers one call at a time from storage of its own, so what one part writes, the next part reads. For data that several processes coordinate through, "eventually" turned out to mean "after the user's next message".
The same commit added a test input that is routed among test conversations only, so routing can be tried without posting into a real chat. A later fix made each test chat an audience of its own, because one run could otherwise join the previous run's conversation about the same thing.
What carries over
- When the question is "which of these", build the options in code and let the model only choose. It is easier to test and easier to explain afterwards.
- Describe each option in words, including the things a person would weigh: how recent, whether it is waiting, what it is about.
- Give the classifier a default that is the old behaviour, and fall back to it on errors and timeouts.
- Keep the decision and its probabilities. They are your regression suite and your debugger.