Chat Streaming
QDash translates runtime events into Server-Sent Events (SSE) while saving each turn independently of the browser connection.
Responsibility Boundary
| Layer | Responsibility |
|---|---|
| Agent Runtime | Durable model and tool execution |
pi_chat_service.py | HTTP bridge, event translation, and final response |
chat_turn_runs.py | Background stream and server-side chat recording |
| Chat UI | Live rendering, reconnecting, and explicit stop requests |
Event Translation
Text, reasoning, tool arguments, tool results, and rendered blocks stream as they become available. ViewEventTranslator reads durable conversation revisions, including result entries, so skipped intermediate updates do not lose tool output. Preserve event identity and ordering when changing translation; the final response must agree with the streamed blocks.
Text before or between tool calls becomes work history; text after the last tool call becomes the final answer. agent-runtime/src/prompt.ts requests concise progress updates at meaningful milestones, separately from thinking. These updates use the existing text events, so their frequency depends on the model. Keep the answer boundary consistent between ui/src/lib/agentChatStream.ts and src/qdash/api/services/chat_transcript.py.
On failure, both layers retain streamed content as an assistant message marked interrupted before the separate Error: … message. The last message remains the error for retry and status detection. Preserve this ordering and the partial content when changing recovery behavior.
Durable Turns and Cancellation
Closing a browser stream does not cancel a turn. Stop sends turn_request_id through POST /agent/chat/stop to the runtime's abort route. A late request cannot stop a different turn. The API records progress and completion even when no browser watches.
Reattaching to a Turn
Reconnect with the same request ID and reattach: true; never resubmit the prompt to recover a stream. The UI stores bounded per-user turn pointers in session storage. Other windows discover an active turn and follow it without submitting or saving another turn.
A restart or unavailable turn must produce an explicit recovery state, not silently dispatch the same tools again. Keep cancellation, transport loss, and completion distinct.
Branches
A branch forks a Pi conversation at a selected answer in the same SQLite store. It does not copy the database or reconstruct a transcript. Branches share the current workspace files, not a file snapshot from the fork point. Deleting a chat removes shared state only when no remaining chat uses it.
Project Sharing
agent_chat_share_service.py copies persisted chat messages into agent_chat_share for read-only project access. It projects text, chart, and composed UI blocks, excluding tool traces, runtime identifiers, attachments, and interactive cards. Publishing again replaces the snapshot while preserving its link; it never subscribes to the original chat's stream.
Only the chat owner with an Editor or Owner role can publish. Every read checks project membership and scope. The chat owner or a project Owner can revoke a snapshot; deleting the source chat removes its snapshots. Recorded turns from another project and running answers prevent publication. Legacy messages without turn records rely on the owner's review.
SharedChatPageContent renders snapshots without the live session provider's editing or runtime controls. Dashboard and chat share VisibilitySelector, VisibilityLabel, and CopyLinkButton from components/ui/. Test authorization, snapshot independence, revocation, and excluded blocks in tests/qdash/api/routers/test_agent_chat_share.py, plus the chat sharing UI tests. For the procedure, see Agent.
Calibration Session State in the Chat
Session links come from the chat's successful or running session-scoped tool calls, not model text or failed calls. Read the linked session through the active project's API before displaying its current status. Notifications describe chat events; they do not authorize calibration work.
Change and Test Streaming
Start with these files:
| File | Responsibility |
|---|---|
agent-runtime/src/server.ts | Runtime stream and abort routes |
agent-runtime/src/view-events.ts | Durable-view translation |
src/qdash/api/services/pi_chat_service.py | Runtime-to-SSE bridge |
src/qdash/api/services/chat_turn_runs.py | Server recording |
ui/src/lib/agentTurnStore.ts | Reload recovery pointers |
Run task ci-agent-runtime and the affected API/UI chat tests. Cover disconnect, reattach, late cancellation, reload, branch sharing, tool arguments, and final-response parity.