Skip to Content
Motoko

Events from committed state

Each session's stream is derived from committed state by one transcriber, with one set of rules for history and for each new commit: the entries (pi.user, pi.assistant, pi.tool-result, pi.compaction, and the runtime's motoko.steer and motoko.continue), the pi.live document, the runtime's approval record, and the gate's memo of the arguments of a call waiting at it. Stored facts (messages, tool calls, results, decisions, run ends) come from entries and documents; what only pi.live shows (a streaming reply, drafts, running output, a retry wait, a parked question, a compaction going on or the newest one's failure) is superseded by the stored events that follow it.

Cursors

A cursor names a position in committed state: the session, the newest entry whose events the client has all folded, and the run it is in (the entry that started it: its input, or its continue's motoko.continue entry). It stays valid across a restart and in another process that opens the same storage. A cursor handed out from the live log also carries that log's place (epoch, seq). Resuming from a cursor:

  1. Exact, from the log. While the cursor's place is still in this process's log, the reader gets exactly the events after it, live included: no duplicates, no gaps.
  2. From committed state, anywhere else (after a restart, in another process, or once the cursor's run has left the log). The entries of the client's run up to its position are read silently to rebuild what the client knows (its open calls, whether it is in a run); the entries committed after the position are sent; then the current in-flight state is sent as events the reducer applies idempotently: the run going on, the question of each call waiting for approval, each decision of a call that left its wait, each deferred call's wait for its reply, running output, a retry wait, the reply streamed so far as one delta, drafted calls, the compaction going on or the newest one's failure (compaction:start or compaction:end), and, after the run's end, its wrap-up going on or none (wrapup:start or wrapup:end). The reader then joins the live log where it continues. The last step runs in the session's commit chain, so no commit lands between the catch-up and the log. The folded view equals that of a client connected all along; the events need not be the same ones.
  3. stream:reset and a full replay only for a cursor storage cannot honour (it names an entry the session does not hold, as after a restore from an older backup). A string that is not a cursor of this session rejects with InvalidCursorError.

events() without from replays the session from its start, a page of entries at a time, then joins the log the same way.

History read from storage shows how a run ended through its entries, except for a run that ended without an answer and left no entry saying why (faulted, no_model, turn_limit, ...), which pi.live.runFailure describes only until the next run starts. The runtime takes such a run's end from its input's settlement, or its continue's (unanswered, with the same reason and detail), which pi-durable keeps. pi-durable scans submissions only on the Storage, so the runtime reads the instance's own Storage for it.

What is held in memory

  • A session's stream (its observer, transcriber and log) exists only while it is needed: a reader is iterating, a call (submit, continue, approve, abort, withdraw, compact) is under way, or the session has work (a run, or queued input). A session with no reader and no run holds nothing; its stream closes and the next reader or call opens it again from committed state.
  • While it exists, the log holds the events since the current run's run:start, plus the previous run's events only while an attached reader has not read them yet. The transcriber holds the current run's open calls and questions. So what is held is bounded by the run, not by the session's history. A reader that falls behind the log carries on from committed state.
  • A catch-up reads history a page (200 entries) at a time and holds one page of events; rebuilding the state of a run (for a resumed client, or a stream that opens mid-run) reads that run's entries.
  • pi-durable's own view of the conversation is a window of at most 32 entries.

A reader keeps its session's stream open until it ends (the loop breaks or returns, or its signal aborts).