Delegates
A delegate hands a task to a sub-agent, which runs in a child session of the caller's, in the same instance. The call holds no process while it waits: it parks until the child's run ends, then returns the sub-agent's answer as its result.
import { defineAgent, defineDelegate } from "@chia1104/motoko-core";
const researcher = defineAgent<NotesPorts>({
kind: "researcher",
instructions: "You find what the user's notes say about a topic, and answer in one paragraph.",
tools: [readNote],
model: { provider: "anthropic", modelId: "claude-haiku-5" }, // absent: the parent session's model
});
const research = defineDelegate({
name: "research",
description: "Hand a research task to the researcher. It sees only the task you write.",
tier: "read",
agent: researcher,
});
export const notesAgent = defineAgent<NotesPorts>({
kind: "notes",
instructions: "You manage the user's notes.",
tools: [readNote, deleteNote, research],
});The model calls research with { task }. In one commit the call creates the child session, owned
by the call, and configures it to run researcher alone: its tools, its instructions, and its model
when it names one. The call then submits the task and parks. A rerun after a crash finds the same
child and the same submission, so no crash makes a second of either. The result is the sub-agent's
answer. A child whose run ends without an answer (aborted, failed) gives an error result that says
why, and the parent's run goes on.
- A delegate is a tool like any other: the agent's policy and the session's rule judge its tier. A call that started is never gated again, whatever the rule says by then.
- Each session sees only its own agent's tools and instructions. The runtime sets pi-durable's default
extension selection (
settings.extensions) to the instance's agent, after the app extensions that select it, so a top-level session, also one made before its agent had delegates, never sees a sub-agent's. A child session selects the app extensions that name its sub-agent's kind, then the sub-agent's own: by default none of the app's (App extensions). - Sub-agents have no instances of their own.
createAgentRuntimethrows aTypeErrorfor two definitions of one kind among its agents and the sub-agents their delegates reach. A definition is frozen once made, so delegates never loop.
Child sessions
A child session is followed, decided in and aborted like any session, under its sub-agent:
- From its start, the delegate's call carries
{ sessionId, agent }in its details. Wire'sdelegateOf(item)reads them, so a UI follows the child withinstance.session(sessionId).events(). The parent's call staysrunningin its view while the child works. - A call the sub-agent's policy gates waits in the child: approve it with the child session's
approve(). The parent shows no question, and itsapprove()refuses the delegate's call. The child'ssettings()lists the sub-agent's tools. - Only its delegate submits to it: the child's
submit(),continue(),configure(),compact()anddelete()reject withSessionOwnedError(session_owned).listSessions()leaves it out. - Aborting the parent aborts the child, through pi-durable's ownership. Aborting the child ends its run, and the parent's call returns an error result.
- Deleting the parent deletes its child sessions in the same commit: their readers end, their calls
reject with
SessionNotFoundError, and the host erases them with the parent.
The sessions record names a child's sub-agent (agent) and its parent (parent: the session and the
call). A conversation document, runtime.calls, records each delegate's call in the commit that makes
its child: a call listed there has started, so when it sleeps, it waits on its child, not at the gate.