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?

RoleSurfaceUse it for
Feed one chunkApplyapply an offset/text update; appends, queues, or reports a conflict
Stateful assemblerAssemblercontiguous content plus deferred future chunks
Input and outcomeChunk, Resultan offset/text update and its append, queue, gap, or conflict outcome
Recovery/resetRebase, Reset, Generationreplace with a snapshot while retaining future work; reset increments generation
DiagnosticsConflictKinddistinguish 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.Appended is 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. Reset clears state and increments Generation.

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?

NeedAuthority
API and algorithminternal/frontend/stream/assembler.go
Complete edge-case matrixinternal/frontend/stream/assembler_test.go (TestAssemblerConformance)
Offset field contractpkg/protocol/agent.go (MessageUpdate)
Offset productionpkg/service/agent/loop.go (RunLoopWithOptions)
CLI policypkg/cli/stream_assembler.go, pkg/cli/prompt_stream.go; tests in pkg/cli/prompt_stream_test.go
TUI policy and recoverypkg/tui/conversation_state.go, pkg/tui/app.go; recovery tests in pkg/tui/app_stream_gap_test.go