docs/tui-stream-chunking-review.md
This document explains how stream chunking in the TUI works and why it is implemented this way.
Streaming output can arrive faster than a one-line-per-tick animation can show it. If commit speed stays fixed while arrival speed spikes, queued lines grow and visible output lags behind received output.
codex-rs/tui/src/streaming/chunking.rs
codex-rs/tui/src/streaming/commit_tick.rs
codex-rs/tui/src/streaming/controller.rs
codex-rs/tui/src/chatwidget.rs
On each commit tick:
queued_lines: total queued lines.oldest_age: max age of the oldest queued line across controllers.HistoryCells for insertion by the caller.In CatchUpOnly scope, policy state still advances, but draining is skipped
unless mode is currently CatchUp.
Two modes are used:
Smooth
tui/src/app.rs:COMMIT_ANIMATION_TICK (~8.3ms, ~120fps).CatchUp
Batch(queued_lines).Entry and exit use hysteresis:
CatchUp when queue depth or queue age exceeds enter thresholds.EXIT_HOLD).This prevents oscillation when load hovers near thresholds.
These are the current values in streaming/chunking.rs plus the baseline
commit tick in tui/src/app.rs. They are
experimental and may change as we gather more trace data.
~8.3ms (COMMIT_ANIMATION_TICK in app.rs)queued_lines >= 8 OR oldest_age >= 120msqueued_lines <= 2 AND oldest_age <= 40msCatchUp -> Smooth): 250ms250msqueued_lines >= 64 OR oldest_age >= 300msIn Smooth, plan is always Single.
In CatchUp, plan is Batch(queued_lines), which drains the currently queued
backlog for immediate convergence.
This keeps normal animation semantics intact, while making backlog behavior adaptive:
Smooth.CatchUp exits only after sustained low pressure.CatchUp.Trace events are emitted from commit-tick orchestration:
stream chunking commit tick
mode, queued_lines, oldest_queued_age_ms, drain_plan,
has_controller, all_idlestream chunking mode transition
prior_mode, new_mode, queued_lines, oldest_queued_age_ms,
entered_catch_upThese events are intended to explain display lag by showing queue pressure, selected drain behavior, and mode transitions over time.