Live agent data arrives over WebSockets. In Exact a stream is still a resource: the source returns a fetch that never settles, with an exactStream mapper that turns each socket event into the resource's next value. The agents package has three such sources in src/agents/sources.ts. The app never polls a running conversation.
The exactStream mechanism
Exact's rule (reference.md): "An answer that keeps coming is a fetch with exactStream, returned as the answer". A ws: or wss: URL opens a WebSocket under the net.websocket grant. For TypeScript sources the mapper receives (event, socket), where event.type is open, message, close or error, and "must return a shaped snapshot synchronously". Each returned value commits as the resource's answer. Letting the resource go (its arguments change, or refresh) closes the socket.
Each source follows the same outline:
Return an empty or initial value immediately when disabled.
Pre-encode a signed Subscribe envelope with encodeAgentEnvelope, timed with readTime.
return fetch(agentEndpoint(base, 'ws'), {exactStream: mapper}).
On open, socket.send(body): the binary envelope is the only client message.
On message, parse the JSON frame with parseFrame, which returns {} for anything that is not a JSON object, so a bad frame never throws out of the mapper.
The three streams
Source | Subscribe key | Initial value | Resource in the root |
|---|---|---|---|
| The HTTP snapshot |
| |
| The root run card from |
| |
|
|
|
Frames
Frame | Session stream | Run stream | Agent stream |
|---|---|---|---|
| note | ignored | note |
|
|
| ignored |
| upsert | ignored |
|
| append | ignored | ignored |
| take | upsert the run record, rebuild the root card |
|
|
| error = | error = |
socket |
| "Run updates disconnected. Reconnect to refresh." | "Live agent updates disconnected." |
The session stream's connection field moves Loaded (HTTP) to Connecting (socket open) to Live (subscribed), or to Disconnected.
Snapshot guards
The stream starts only from a matching snapshot. agentsSessionStream returns initial untouched unless initial.connection === 'Loaded' and its owner and sessionId match the arguments. A snapshot for another session or account never seeds a socket.
Replay is idempotent. upsertEvent replaces an event with the same id and sorts by seq, and afterSeq asks the server to replay only newer events.
Runs only move forward. Both agentsLoadRuns and the run stream write to the shared runSnapshots map and keep a record unless the incoming updatedAt is at least as new. A parity test holds an HTTP reply back while a change frame completes the run and asserts the late reply still shows Done.
The root merges by version. agentsRuns uses the live card in place of the HTTP card for the root run only when owner and rootId match, agentsLiveRuns.version >= agentsRunData.version, and the live list is not empty. version is one module counter (runRevision) incremented by every snapshot of either source.
Reconnect and backoff
Reconnection is driven by root tasks, not by the source (lines 199-206 and 447-463):
agentsReconnect is active while a visible session has agentsThread.connection == "Disconnected" or a run stream error, or a visible agent has a live error. It is keyed on agentsReconnectAttempt and runs agentsMaybeReconnect every second.
agentsMaybeReconnect calls agentsRetryConnection once performanceNow() >= agentsReconnectAt.
agentsRetryConnection sets the next allowed time to now plus agentsReconnectDelay, increments the attempt, doubles the delay up to 10 000 ms, and calls agentsRefresh, which restamps both clocks and refreshes all six resources. The fresh HTTP snapshot then seeds a new socket.
agentsVisit (keyed on top(nav).id) resets the delay to 1 000 ms and the attempt to 0 on every screen visit. The header's Reconnect button calls agentsRefresh directly.
sequenceDiagram
participant Root as app.contract
participant Src as sources.ts
participant Srv as agents server
Root->>Src: agentsSession
Src->>Srv: POST GetSession
Srv-->>Src: events and session
Src-->>Root: thread, connection Loaded
Root->>Src: agentsSessionStream with the snapshot
Src->>Srv: open WebSocket
Src->>Srv: signed Subscribe sessions/id afterSeq
Srv-->>Src: connected, subscribed
Src-->>Root: connection Live
Srv-->>Src: appendPartial text deltas
Src-->>Root: streaming text
Srv-->>Src: append durable event
Src-->>Root: rows, revision + 1
Srv--xSrc: socket closes
Src-->>Root: connection Disconnected
Root->>Root: agentsReconnect task, backoff 1 s doubling to 10 s
Root->>Src: refresh all resources
Src->>Srv: POST GetSession, then a new socketTests
review.test.ts: a non-JSON frame leaves the state intact; server time from frames affects reads only.
parity.test.ts: "a delayed HTTP tree cannot overwrite a newer completed run from the stream".
agents.test.contract: "a dropped stream reconnects and recovers the offline message once" (18 rows, no duplicate) and "read clocks advance on an idle session without reopening any stream".
Unit tests replace globalThis.fetch and capture the exactStream mapper to drive it with synthetic events.
Related
Agents: reads, commands and the clock: the HTTP snapshots and readTime
Agents: chat rows and run cards: chatRows, runRow, upsertEvent
Agents: screens and components: how text, phase and status are drawn
Agents server: the subscribe protocol from the server's side
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime