Live streams
Snapshots over server-sent events, deltas, and why reconnecting needs no cursor.
The assistant is halfway through an answer when your laptop loses Wi-Fi. When the connection comes back, the answer jumps forward. The tab has caught up by asking for the chat as it looks now. The missing updates can stay missing.
In this chapter
- Follow a running chat from Pi’s watch to the browser’s event stream.
- Read the first event and apply an edit to the view it carries.
- Tell when the browser reconnects, asks you to sign in, or asks you to reload.
- Follow an older page of messages as it joins a moving transcript.
A new connection starts with the chat as it stands
| Time | On the wire | On screen |
|---|---|---|
| 0 ms | snapshotevent: snapshot
data: {"version":1,"snapshot":{"id":"9","title":"UTC time","profile":"chat","archived":false,"thinkingLevel":null,"model":"faux/faux-1","createdAt":1796112000000,"location":"none","messages":[{"id":"12.0","role":"user","text":"What time is it in UTC?","timestamp":1796112000000}],"contextWindow":{"used":8,"limit":128000,"source":"estimated"},"status":"running","runId":"14","partial":"","liveTools":[]}}
| Running |
| 500 ms | deltaevent: delta
data: {"version":1,"revision":1,"edits":[["set",["partial"],"I’ll check the clock for the current"]]}
| Running |
| 1100 ms | deltaevent: delta
data: {"version":1,"revision":2,"edits":[["slice",["partial"],0,36," UTC time."]]}
| Running |
| 1700 ms | deltaevent: delta
data: {"version":1,"revision":3,"edits":[["slice",["messages"],0,1,[{"id":"16.0","role":"assistant","text":"I’ll check the clock for the current UTC time.","timestamp":1796112000000,"calls":[{"id":"clock-1","name":"current_time","arguments":"{}"}],"failed":false}]],["set",["contextWindow","used"],22],["set",["partial"],""],["set",["liveTools"],[{"name":"current_time","callId":"clock-1","status":"running","output":""}]]]}
| Running |
| 2300 ms | dropConnection lost (browser marker) | Reconnecting… |
| 3400 ms | snapshot · reconnectedevent: snapshot
data: {"version":1,"snapshot":{"id":"9","title":"UTC time","profile":"chat","archived":false,"thinkingLevel":null,"model":"faux/faux-1","createdAt":1796112000000,"location":"none","messages":[{"id":"12.0","role":"user","text":"What time is it in UTC?","timestamp":1796112000000},{"id":"16.0","role":"assistant","text":"I’ll check the clock for the current UTC time.","timestamp":1796112000000,"calls":[{"id":"clock-1","name":"current_time","arguments":"{}"}],"failed":false},{"id":"18.0","role":"tool","name":"current_time","callId":"clock-1","text":"2026-12-01T08:00:00.000Z","timestamp":1796112000000,"failed":false}],"contextWindow":{"used":32,"limit":128000,"source":"estimated"},"status":"running","runId":"20","partial":"I checked the clock. The current time is","liveTools":[]}}
| Running |
| 4200 ms | deltaevent: delta
data: {"version":1,"revision":1,"edits":[["slice",["partial"],0,40," 08:00 UTC."]]}
| Running |
| 4800 ms | deltaevent: delta
data: {"version":1,"revision":2,"edits":[["delete",["runId"]],["slice",["messages"],0,3,[{"id":"21.0","role":"assistant","text":"I checked the clock. The current time is 08:00 UTC.","timestamp":1796112000000,"calls":[],"failed":false}]],["set",["contextWindow","used"],39],["set",["contextWindow","source"],"reported"],["set",["status"],"idle"],["set",["partial"],""]]}
| Idle |
On the wire
- snapshot
event: snapshot data: {"version":1,"snapshot":{"id":"9","title":"UTC time","profile":"chat","archived":false,"thinkingLevel":null,"model":"faux/faux-1","createdAt":1796112000000,"location":"none","messages":[{"id":"12.0","role":"user","text":"What time is it in UTC?","timestamp":1796112000000}],"contextWindow":{"used":8,"limit":128000,"source":"estimated"},"status":"running","runId":"14","partial":"","liveTools":[]}}
On screen
Running
When you open a chat, the browser makes a GET request to /api/v1/conversations/:id/events?delta=1 using EventSource. If the workspace is mounted under a base path, that path goes before /api/v1. The response stays open so Hono can send more events as the view changes. That format is called server-sent eventsA format for sending a series of named events from a server over one open HTTP response., or SSE. watch-conversation.ts api.ts
The first event carries a conversation snapshotThe JSON view Pipal assembles for the browser from saved chat state, recent messages, and live work.. Open the chat while an answer is being written and the current text from the live view arrives in partial, alongside the recent messages already saved. The same view can show tools already in flight. The data model traces each field back to its source. view.ts
On the wire, the clock call, live slot, and saved result have separate fields. The browser matches them into one activity row. If more calls follow without a visible row in between, the browser can group them without any group data on the stream. The Svelte workspace follows that display. contracts.ts transcript.svelte.ts
One watch combines several sources
Core’s watch() listens for changes to Pi’s conversation view, which includes recent saved entries and live run state. It also watches pipal.chat, the document that holds Pipal’s chat settings. A rename and a few new words in a reply can therefore wake the same loop. Pi’s watch can show a partial reply before that text reaches SQLite. index.ts CHANGELOG.md
A quiet stream can still have progress to report. While Pi waits to retry a model request, the transcript shows the scheduled local time, the number of the next attempt and a short reason. The notice clears when the next attempt starts. A summary of earlier messages has its own activity line, including whether it is running in the background. Reload during a summary and the new snapshot restores that line. status-text.ts run-status.spec.ts
Saving Settings → Workspace wakes open conversation watches too, as can a lost connection to a workspace. The snapshot carries the chat’s pinned locationThe place a chat uses for project tools, either the Pipal host, an account reached over SSH, or a Docker project.. For a chat pinned to a workspace, workspaceStatus() adds workspaceUnavailable: true when the binding can no longer be resolved. An open tab can learn that its project tools are unavailable on the same connection it already uses for replies. index.ts workspace-locations.ts
The parent chat can also show that a specialist is waiting for your approval. While its delegate call waits, core watches the specialist’s live tool state. It follows the same path for a running worker. A change confined to a child generates a new parent snapshot only if the approval requests it projects into the parent have changed. Delegation follows the specialist’s job. index.ts
Deep diveA blocked write leaves the watch free to fold changes together
The watch callbacks resolve a promise. They can do that while the stream is still sending the previous event. Hono awaits the write before it pulls another result, so several wake-ups can resolve the same promise. When the generator resumes, it can read the latest values once. In delta mode, Hono compares that new public JSON with the last snapshot it processed and advances the wire revision only when there are edits to send. A consecutive pair of revision numbers can therefore span several changes inside Pi. index.ts app.ts
// JSON escapes CR and LF, so the frame fits on one SSE data line.if (data !== undefined) await stream.write('event: ' + event + '\ndata: ' + data + '\n\n');previousMessages = update.value.messages;update = await snapshots.next();The watch reuses the projected message array as long as Pi reuses the same entries array. Delta mode can reuse the previous serialized messages too. With no query flags, Hono sends full snapshots throughout. With ?compact=1 it can send an update event without messages when it reuses that array. If both flags are set, delta mode takes precedence. A HEAD request reads one snapshot and returns the headers with no stream body or ongoing watch. index.ts app.ts sse-delta.test.ts
A delta edits one connection’s copy
event: delta
data: {"version":1,"revision":1,"edits":[["set",["partial"],"Real change"]]}
- Event name
EventSourcedispatches this to the listener fordelta. - Protocol version
The browser checks for version 1 before reading the edits.
- First revision
The browser starts its count at zero when it receives a snapshot. The next delta must have revision 1.
- An edit to partial text
Follow the path
["partial"]from the snapshot root and replace that field with the new string. - Blank line
The empty line ends this event. Line breaks inside the JSON strings are escaped on the wire.
data: {,,}
1 Event name
EventSource dispatches this to the listener for delta.
Once the browser has the first snapshot, Pipal can send a deltaAn ordered set of edits that turns the previous conversation snapshot into the next one on the same connection.. Each delta says what to change and which revision the result becomes. The browser applies it to a local copy called the replica. This saves sending the entire transcript window when the only change is a few more words. app.ts watch-conversation.ts
On the wire, snapshot carries {version, snapshot}. A delta carries {version, revision, edits}. An edit can replace a field, remove a field, or keep a slice of text or an array and append a tail. The revision belongs to the connection. Every full snapshot starts the count at zero in the replica, and its first delta has revision 1. app.ts watch-conversation.ts
Deep diveWhat the replica keeps and what makes it reject an edit
A set edit supplies a path and a value, and delete removes an existing object field. A slice supplies a path, an inclusive start, an exclusive end, and a tail of the same type. Paths use object keys and numeric array indexes and must contain at least one segment. String offsets count JavaScript’s UTF-16 code units. For text, the server looks for an overlap of at least 32 code units before using a slice. It can also keep the matching range of committed messages when the window moves. Other arrays of the same length are compared item by item. app.ts watch-conversation.ts
The browser requires a baseline, the next revision in order, and at least one edit. It copies containers along each edited path, checks the resulting snapshot’s known fields, and freezes the replica. Additional fields are allowed, and unchanged messages can keep their identity. If a frame leaves the snapshot invalid, none of its edits reach the displayed view. The tests change a message and set search data to the wrong type in the same frame, then check that the old view remains. watch-conversation.ts sse-delta.test.ts
The replica checks both workspace fields when present. It accepts local, ssh, docker, or none for location. The only accepted value for workspaceUnavailable is true. watch-conversation.ts contracts.ts
Reconnecting asks for another snapshot
- Browser EventSource
- Hono app.ts
- Core index.ts
Hono → Browser · delta
The tab is watching a reply
The browser has a snapshot and has applied edits on top of it.
Browser → Browser · error
The connection breaks
onerrorresets the replica. The last rendered view can stay on screen while the workspace reconnects.Core → Core · Release watch
Core can release the old watch
Pi can keep producing text or waiting for tools while the browser is disconnected.
Browser → Hono · GET events
EventSource retries
For a retryable drop,
EventSourcechooses when to retry the same URL. The request needs no position in the old stream.Hono → Core · watch()
Hono opens a new watch
The route asks for the first value before starting the event stream.
Core → Hono · Current view
Core assembles today’s view of the run
The snapshot contains the latest window of committed entries and the current partial reply, if one is in progress.
Hono → Browser · snapshot
The tab gets a new starting point
The full snapshot supplies all the fields needed to replace the live view. The replica starts its revision count at zero.
Hono → Browser · delta 1
The next edit belongs to this connection
Revision 1 applies to the new snapshot, regardless of the last revision seen before the drop.
Hono → Browser
The tab is watching a reply
The browser has a snapshot and has applied edits on top of it.
A network drop ends the old watch while the run can keep going. On a retryable stream error, EventSource handles the retry and the browser clears its replica. The next connection opens the same URL and receives a new snapshot. It can show the partial reply farther along, or a committed answer if the run finished during the gap. watch-conversation.ts app.test.ts
Even when the run has nothing new to show you, the server schedules a heartbeat after 15 seconds and schedules the next one after the write completes. In delta mode it sends event: ping with data: {}. Other modes send the SSE comment : heartbeat. contracts.ts app.ts
The browser starts a 45-second silence timer when it opens the stream. A snapshot or delta refreshes that clock. A ping counts only after the client has a snapshot, and a stream error returns it to waiting for that baseline. When the tab becomes hidden, Pipal closes the stream. When it becomes visible, it opens a fresh one. watch-conversation.ts sse-delta.test.ts
Deep diveWhich failures retry and which ask for a reload
A server error inside the stream loop, such as a serialization failure, can send event: resync with data: {"version":1} before closing. A request abort, session ending, or host shutdown closes the watch. app.ts sse-delta.test.ts
Pipal closes the source and schedules a fresh connection in 3 seconds after an in-band resync, invalid edits or a revision gap, a 45-second silence timeout, or an error that leaves EventSource closed. For retryable errors on the existing source, the browser handles retry timing while Pipal’s silence timer stays active. A visibility change can also replace a pending retry. watch-conversation.ts sse-delta.test.ts
A malformed full snapshot or a recognized version number other than 1 takes the incompatible path. The client stops watching, removes its visibility listener, and shows “Reload to use this server version.” in the main workspace. When the access check instead yields a 401, locking the workspace stops its watch and returns to the sign-in screen. watch-conversation.ts App.svelte
Older messages use a history cursor
The stream’s window starts with up to 24 saved entries. As new entries arrive, it can grow to 48. When the next entry would overflow that bound, Pi drops the oldest part of the window and keeps the newest 24. An entry is a saved transcript record and can supply several displayed messages. Keeping a small window limits how much the host loads, including any model-ready images stored in those entries. index.ts CHANGELOG.md
If the window has earlier history, its before field points to the entry immediately before it. This history cursorThe ID of the newest entry that belongs on the next page of older chat history. goes in GET /api/v1/conversations/:id/history?before=<entryId>. The server reads at or below that ID and returns the page oldest first. Core rejects a cursor unless it is a positive safe integer. The new before, if present, leads to the next page back. Meanwhile the stream can keep adding fresh messages at the other end. README.md index.ts
If you’re reading earlier messages when the live window moves, the main transcript can hold onto the messages that fall out. It also does this once you’ve requested an older page, including while the request is in flight. Transcript retains displaced messages when the cursor moves and the windows still share message IDs. If a new window shares no IDs with the previous nonempty one, there may be a gap. The browser drops the old pages and lets you page back from the new window. transcript.svelte.ts App.svelte
Deep diveWhy loading older messages can race with an incoming update
Core asks Pi for up to 25 entries, then projects up to 24 in chronological order. The extra entry, when present, becomes the next inclusive before cursor. The browser starts retaining displaced messages before the request leaves. When the page arrives, the workspace checks that the selection has not been reset, then prepend() checks that it still has a history page for the requested cursor. It filters page messages by ID against the entire loaded transcript before adding them at the top. The browser test holds an older page in flight while the live window advances and checks that the user messages appear in order. index.ts transcript.svelte.ts App.svelte chat.spec.ts
How does a saved entry turn into a message with an ID the browser can recognize across pages and connections? The data model follows the record across that boundary. The Svelte workspace shows how the browser renders the resulting view.
Recap
- Core combines the recent committed window with live run state, chat settings, workspace availability, and waiting approvals.
- SSE starts with a snapshot. In delta mode, numbered edits update the replica until the connection ends.
- Reconnecting starts a new replica. Paging backward uses an entry ID and merges older messages by message ID.
- Wi-Fi drops during revision 12. The assistant is still writing when the connection returns. How does the browser get the words it missed?
- It gets the latest partial reply in a new snapshot.
- It requests deltas starting at revision 13.
- It pages backward using the last message ID.
Answer and explanation
It gets the latest partial reply in a new snapshot.
The new stream opens a fresh watch. Its first snapshot already contains the current partial reply.
- The browser gets revision 4 after applying revision 2. What should it do?
- Apply revision 4 to the current replica.
- Keep waiting for revision 3 on the same stream.
- Start a fresh stream for a full snapshot.
Answer and explanation
Start a fresh stream for a full snapshot.
Each delta assumes the previous revision. A gap makes the edits unsafe to apply and triggers a reconnect.
- You return to a busy chat after a long disconnect. The new window shares no message IDs with the old one. Where does Load earlier messages start?
- At the old history cursor.
- At the new snapshot’s history cursor.
- At the first missed stream revision.
Answer and explanation
At the new snapshot’s history cursor.
A gap may separate the old pages from the new window. Transcript discards the old pages and pages from the new window so the next page joins it.
Wi-Fi drops during revision 12. The assistant is still writing when the connection returns. How does the browser get the words it missed?
The browser gets revision 4 after applying revision 2. What should it do?
You return to a busy chat after a long disconnect. The new window shares no message IDs with the old one. Where does Load earlier messages start?