docs/internal/frontend/stream.md
Frontend stream assembly
internal/frontend/stream assembles cumulative text from byte-offset chunks for
frontend consumers. It knows no leaf, turn, content type, lifecycle, network, or
presentation policy. For socket connection lifecycle, see frontend;
for wire frames and message_update, see protocol and the
JSONL API catalog. For consumer behavior, see the
CLI or TUI; providers are covered by
service/agent.
What are the core types and operations?
| Role | Surface | Use it for |
|---|---|---|
| Feed one chunk | Apply | apply an offset/text update; appends, queues, or reports a conflict |
| Stateful assembler | Assembler | contiguous content plus deferred future chunks |
| Input and outcome | Chunk, Result | an offset/text update and its append, queue, gap, or conflict outcome |
| Recovery/reset | Rebase, Reset, Generation | replace with a snapshot while retaining future work; reset increments generation |
| Diagnostics | ConflictKind | distinguish invalid ranges, contained mismatches, and overlap mismatches |
Apply accepts only an offset and text. String, HasGap, and Pending expose
state; Pending is an independent snapshot rather than mutable assembler storage.
Which rules must an editor preserve?
- Offsets are byte indexes, including for UTF-8. Upstream producers maintain one byte counter per logical stream, advancing it by the byte length of each delta.
- An out-of-order (future-offset) chunk is queued, not dropped. When missing content
arrives, draining may append several now-contiguous chunks;
Result.Appendedis only that new material. - Matching duplicates and contained text do not append. A matching overlap appends its new suffix. Conflicting contained or partial overlaps report their distinct conflict without changing established content.
- Negative or overflowing ranges conflict without mutating content or pending work. Negative offsets are not an append convention in this package.
Rebase(snapshot)replaces contiguous content, keeps future pending chunks, and drains any now-covered work. It does not declare a snapshot authoritative; callers own that decision.Resetclears state and incrementsGeneration.
Who supplies identity and recovery policy?
The assembler deliberately does not decide logical-stream identity or recovery.
The CLI keys one assembler by {leafID, turnID, deltaType}, then owns admission,
lifecycle races, ordered output, grace windows, and canonical completion. The TUI
keys by turn and box kind, uses assembled String() for box content, and turns
gaps or conflicts into debounced canonical-turn recovery. Keep those policies out
of this package, and do not share an assembler across logical streams.
Providers emit offset-bearing updates; the protocol defines their identity and
meaning. The transport carries those offsets unchanged. The core consumes only
(offset, delta text).
Where does the exact behavior live?
| Need | Authority |
|---|---|
| API and algorithm | internal/frontend/stream/assembler.go |
| Complete edge-case matrix | internal/frontend/stream/assembler_test.go (TestAssemblerConformance) |
| Offset field contract | pkg/protocol/agent.go (MessageUpdate) |
| Offset production | pkg/service/agent/loop.go (RunLoopWithOptions) |
| CLI policy | pkg/cli/stream_assembler.go, pkg/cli/prompt_stream.go; tests in pkg/cli/prompt_stream_test.go |
| TUI policy and recovery | pkg/tui/conversation_state.go, pkg/tui/app.go; recovery tests in pkg/tui/app_stream_gap_test.go |