Skip to Content
Motoko

Events

EventFieldsMeaning
run:startThe agent started working on an input, or carrying on a run (continue).
usermessageId, text, at?, steer?A user message as stored; steer: true when it joined the run going on.
assistant:startmessageIdA reply began streaming, under a stream id.
assistant:deltamessageId, channel (text/thinking), deltaMore of the streaming reply.
assistant:endmessageId, text, thinking?, stopReason, error?, usage?, at?The reply as stored, under its entry id; replaces the streamed one.
tool:drafttoolCallId, toolNameThe streaming reply began a call.
tool:starttoolCallId, toolName, label, tier, argsThe model called a tool (from the stored reply).
tool:updatetoolCallId, output?, details?Progress of a running call; only changed fields.
tool:endtoolCallId, toolName, status (ok/error/aborted), error?, details?How the call ended.
approval:requesttoolCallId, toolName, tier, args, parentToolCallId?The call waits for a decision on args, the arguments it runs with.
approval:resolvedtoolCallId, approved, comment?The decision.
reply:waittoolCallId, until?A deferred call is parked until its reply comes, or its deadline until passes.
retry:wait / retry:enderror, at /A model request failed and waits to be retried / the wait is over.
compaction:startreason (manual/threshold/overflow)A compaction is going on: older messages are being summarized.
compaction:enderror?No compaction goes on any more; error: the newest one failed.
session:compactedmessageId, summary, at?A summary replaced older messages in what the model reads.
queueitems: [{ submissionId, text, whenBusy }]The inputs waiting for a run, all of them, whenever that changes.
run:endreason (done/aborted/error/interrupted), code?, error?The run ended; code is the engine's reason.
wrapup:startThe run answered; the session still runs its afterRun tasks and takes no input yet.
wrapup:endThe afterRun tasks ended: the session takes a new input.
stream:resetTransport-level: the cursor cannot be honoured; discard the view, a full replay follows.

StreamItem is { cursor, event }; cursor is the position right after event. A call's tier is UNKNOWN_TIER ("unknown") when it is not known: the agent has no such tool, or the view shows the call before its tool:start.

An approval:request with parentToolCallId is a nested call's: one a tool call made through pi-durable's executeTool(), such as a code mode script's, under the model's call it names. A nested call has no tool:start or tool:end of its own; its question ends with the call it was made under.

Steering and the queue

An input submitted while a run is going waits for it, as submit's whenBusy says: queue (the default) waits for the run's answer and then starts a run of its own; steer joins the run going on once its current tool round has its results, so the model reads it before its next reply. A run starts at an input and holds every input that joined it, each sent as a user event with steer: true, under that run's run:start and run:end. A steer that comes after the run's last tool round waits for the answer like a queued input, and starts the next run with the inputs waiting with it.

While inputs wait, queue lists them in submission order, each with its submissionId and whenBusy (queue or steer), and it is sent again, whole, whenever an input joins it or leaves it: a run took it (its user event comes first), withdraw took it back, or an aborted run dropped it. Its last list is the queue; queue with no items empties it. A stream resumed from a cursor it cannot continue exactly states the queue first, as it is then, since inputs a client saw waiting may have left it meanwhile. A client that does not know queue skips it and folds every other event as before.

Compactions

A compaction summarizes a session's older messages into what the model reads from then on; the thread keeps them. One starts when the app asks (compact, reason: "manual"), when the context nears the model's window (threshold), or when a request overflows it (overflow), and it may run while the session is idle or alongside a run. compaction:start says one is going on, and is sent again when another, of another reason, goes on instead. compaction:end says none goes on any more: after a summary (whose session:compacted comes first when the session is idle, else at the run's next boundary), or with the newest one's error when it failed, which pi-durable keeps until a later compaction succeeds. A compaction that finds nothing to summarize sends no session:compacted, and leaves an earlier failure standing. Both come from pi.live, so a stream that replays the session shows only the compaction state as it is now.

Wrap-ups

A run whose session selects an app's afterRun hook (see core's App extensions) answers, and its run:end goes out, before the tasks the hook asks for run, such as naming the session. wrapup:start follows that run:end, and wrapup:end comes once the tasks end, before the next run's run:start. In between the view's status is idle, but the session takes no new input: submit with whenBusy: "reject" is refused (session_busy), and an input that queues shows in queue and starts its run after wrapup:end. How to show that, and whether to let a user type meanwhile, is the client's choice.

Both come from pi.live, like the compaction state: history keeps no wrap-up, so a stream that replays the session shows only a wrap-up going on now. A stream resumed from a cursor it cannot continue exactly states the wrap-up as it is, since one a client saw may be over: before the next run:start it replays, else at the end of its catch-up. So a resumed stream may send one wrapup:end, even for an agent with no afterRun hook; a stream read from the session's start sends none for such an agent.

Cursors

A cursor names a position in committed state, so it outlives the process that handed it out:

<session>.<entry>.<run>[.<epoch>.<seq>]      e.g. 7.42.40.0b6c7a8e1f2d3c4b.17
  • entry: the newest stored entry whose events the client has all folded (0 before the first).
  • run: the run the client is in, by the entry that started it: its user message's, or the marker of the continue that started it; * for a run that started without one; - between runs.
  • epoch, seq: where the event sits in the server's in-memory log of the current run, present on events of that log.

encodeCursor and decodeCursor convert between the string and a CursorPosition; clients treat cursors as opaque. Resuming from a cursor whose log place the server still holds yields exactly the events after it, no duplicates and no gaps. Any other cursor (after a restart, from another process on the same storage, or once its run has left the log) yields the queue and the compaction state as they are (a queue event, then compaction:start or compaction:end), the events of what was committed after entry, then the current in-flight state (see below) and the wrap-up state (see Wrap-ups), then the live stream; no stream:reset, no replay of the session. The resumed events need not be the ones a connected client got, but they fold into the same view. Only a cursor naming an entry the session's storage does not hold gets stream:reset and a full replay.

The view

AgentSessionView holds items (user and assistant messages, tool calls with their status and approval, compaction notes, notices for runs that did not finish), pendingApprovals (tool call ids), status (idle, running, awaiting_approval), retrying, queued, the inputs waiting for a run, absent while none waits, and compaction, { status: "running", reason } while a compaction goes on or { status: "failed", error } after one failed, absent otherwise, and wrappingUp, true from wrapup:start to wrapup:end, absent otherwise; run:start and run:end leave it as it is. A user message that steered its run has steer: true. Every event that names an item upserts it. An item stays where it was placed: a later event changes the newest items or appends, and only stream:reset clears the list, so an item's index is a stable key for the items that have no id of their own, such as notices.

Two questions every UI asks of a view:

  • isBusy(view): a run is going, waits for a decision, or wraps up (wrappingUp, while status is already idle). An input submitted now waits, and submit with whenBusy: "reject" and continue are refused. A compaction going on does not count.
  • canContinue(view): the session is not busy and its newest item, past compaction notes, is a notice, so continue would carry that run on. A notice with code: "nothing_to_answer" does not count, since carrying on would end the same way. A run the user stopped (reason: "aborted") does, and whether to offer it then is the UI's choice.

A tool call's args are those the model wrote (tool:start) until its approval:request, whose args replace them: the arguments the call runs with, which an app's hook may have rewritten before the gate.

A nested call's question is in nestedApprovals of the call it was made under, in the order they asked, with its toolName, tier, args, and approval once decided; its id is in pendingApprovals until then. The call it was made under runs on meanwhile. Its tool:end, or the run's end, drops them, decided or not, and a question for a call the view does not hold, or that ended, changes nothing.

A tool call's status is drafting, running, awaiting_approval, awaiting_reply or how it ended (ok, error, aborted). awaiting_reply is a deferred call parked on its reply, from its reply:wait, with the deadline as replyBy. Its tool:update puts it back to running, without replyBy, once a reply or the deadline woke it, and its tool:end ends it. The run stays running meanwhile: nothing waits for an operator.

A resumed stream sends in-flight state again rather than replaying it, and the reducer applies what a client may already hold idempotently: a stored message or call it has, approval:request for a call still waiting (and none for a call that ended or was decided), nested calls' too, reply:wait for a call still parked on its reply, tool:update only for a call still running, the reply streamed so far as assistant:start and one delta, retry:wait or retry:end, queue, compaction:start or compaction:end, wrapup:start or wrapup:end, and run:end only while the view shows a run.

Following a delegate's child

A delegate's call runs a sub-agent in a child session of its own. From its start, the call's details are { sessionId, agent } (delegateDetailsSchema): the child session and the sub-agent's kind. delegateOf(item) reads them off a tool item, and returns undefined for any other call. The call stays running in the parent's view while the child works, so a UI follows the child as it follows any session:

for (const item of view.items) {
  if (item.kind !== "tool") continue;
  const child = delegateOf(item);
  if (child !== undefined) follow(child.sessionId); // the sub-agent's own messages, calls and questions
}

A call the sub-agent's policy gates waits in the child session, and is decided there.