Skip to content

Companion

The team bot's host has two agents and one workflow. Alice's pull last week's churn belongs to the analyst. Carol's what are competitors charging for a team plan? belongs to the researcher. run the weekly metrics digest is a workflow, and what can you do? needs no agent at all. The Slack bot posts every one of these to the same host and never names an agent.

Every event to a host starts on the built-in companion, which picks where the message goes. This post covers what a host is made of and how the companion routes.

The host

A deployment is one host: the agents and workflows the app registers on the builder.

host = (
    HostBuilder()
    .agent(AnalystAgent(), metadata=AgentMetadata(
        display_name="Analyst",
        description="Answers product metrics questions from the analytics replica.",
        capabilities=["signups", "churn", "revenue", "SQL"],
        usage_guidance="Questions about the product's own numbers.",
    ))
    .agent(ResearchAgent(), metadata=AgentMetadata(...))
    .workflow(WEEKLY_METRICS)
    .build()
)

Every agent and workflow added this way is a member: something the companion can route to. A host with one agent is how an app talks to that agent. The builder also registers companion, an agent the app never writes, which routes and is never a route itself. enable_evals() adds its two workflows as members. Its agents, and Cortex's, are registered without being members.

Each agent runs in its own process, and each of those processes builds the same host from the app's code. The companion's process reads the member list from its own copy, so the host and the companion always agree on what can be routed to. A deploy that changes the agents changes the list for every event after it.

The classifier

The companion runs no agent loop. Its request makes one structured output call whose only field is an enum:

# src/mash/agents/companion/classifier.py
def classifier_schema(choices: list[str]) -> dict[str, Any]:
    return {
        "type": "object",
        "properties": {"route": {"type": "string", "enum": list(choices)}},
        "required": ["route"],
        "additionalProperties": False,
    }

For the team bot the choices are analyst, researcher, weekly-metrics, about and unclear. The model can only answer with one of them, so a route always names something the host has.

The input has four sections: AGENTS, one line per agent with its display name and description, then its capabilities and usage guidance; WORKFLOWS, each with its display_name and description from the workflow's metadata; BOARD, the conversation's entries so far; and MESSAGE. The prompt tells the model that a follow-up on an earlier entry goes to the agent that wrote that entry. When Bob replies break it down by plan, the board shows the churn entry with analyst as its author, and the follow-up goes there.

Routing quality depends on the metadata. Two agents with overlapping descriptions produce unclear, and vague usage_guidance produces vague routes. The analyst's guidance says "the product's own numbers" and the researcher's says "anything outside the product's data", which keeps the two apart.

The classifier runs on the companion's own model, picked like masher's agents from whichever provider key is set. COMPANION_AGENT_<PROVIDER>_MODEL overrides it.

Routing to an agent

When the choice is an agent, the companion starts that agent's request and ends. The routed request runs in the event's session with the message unchanged and the event's context, metadata and structured_output. Its parent names the companion's request and records the choice:

# src/mash/runtime/engine/route.py
route = {"choice": agent_id, "picked_by": picked_by}
parent = {
    "kind": "conversation",
    "request_id": run.request_id,
    "agent_id": self.companion_id,
    "session_id": run.session_id,
    ...
    "route": route,
}

Each agent runs in its own process, so starting the request is an HTTP call. The companion's process posts it to the host's /h2a relay with its agent token. The relay accepts a conversation parent only from the companion, only in the parent request's session, and only for one of the host's member agents. It copies the parent's app and policy onto the routed request. The companion cannot reach past the host's members or act for another app or policy.

The companion's request ends with routed in its response metadata, naming the agent and the request. The host's EventService sees it and follows the routed request to its end. The analyst's answer, its approval prompt and its questions reach Alice on the event's stream directly, with no model call in between, and the event's outcome is the analyst's.

sequenceDiagram
    participant Bot as Slack bot
    participant Host as host (EventService)
    participant Comp as companion process
    participant Relay as host /h2a relay
    participant An as analyst process

    Bot->>Host: POST .../events
    Host->>Comp: start the companion's request
    Comp->>Comp: read board, classify → analyst
    Comp->>Relay: start routed request (parent: conversation)
    Relay->>An: request, with the parent's app and policy
    Comp-->>Host: end, routed {agent_id, request_id}
    Host->>An: follow the routed request
    An-->>Bot: frames on the event's stream

The other routes

about answers from the host's agent and workflow descriptions with no model call: "I pass each message to the agent or workflow that fits it", then the list.

unclear asks the user to pick one of the host's agents with a choice interaction, then routes to the one picked, with picked_by set to user.

A workflow choice makes a second structured output call. Its schema is the workflow's input schema with every field made nullable, and its prompt says to use only values the message or the board states. For run the weekly metrics digest, week_start comes back null, so the companion asks: "To run Weekly metrics digest I need week_start: The Monday the week starts on, as YYYY-MM-DD". The answer is coerced to the field's JSON type. The run starts under the event's app and policy and runs standalone; its steps never read or write the board. The companion waits for it, replies with the run's status, input and result, and writes one board entry with that reply. A failed run's error goes to the user, and nothing retries it.

Recovery

Every step of the companion's request records its output as an event with a dedupe key: the board position it read, the classifier's choice, the workflow input, each answer. A companion request recovered after a crash reads its choice back and makes no second model call.

The routed request's id is a UUID5 of the companion's request id:

# src/mash/runtime/engine/route.py
def routed_request_id(request_id: str) -> str:
    """The id of the agent request the companion's ``request_id`` routes to."""
    return str(uuid.uuid5(ROUTE_NAMESPACE, f"{request_id}:route"))

Resubmitting it returns the request already started, so a crash between starting the analyst's request and ending the companion's starts nothing twice. Cancelling the event cancels the companion's request and the request it routed to.

Where agents meet

A workflow's agent step runs on its agent directly, without the companion, and the routed request is the only path from the companion into an agent. Each agent in the host is a separate process with its own model, tools and sandbox folder, and none of them calls another. What they share is the conversation: the analyst and the researcher answer in the same thread, and each reads what the other said. That shared record is the board, and it's the next post.

Next: Shared Board.