TUI paste failure during streaming can hide assistant message prefix
What version of Codex CLI is running?
codex-cli 0.146.0
What subscription do you have?
Not relevant to local TUI transcript rendering.
Which model were you using?
The issue reproduces in the client-side transcript path and is independent of the model.
What platform is your computer?
Microsoft Windows NT 10.0.19045.0 x64
What terminal emulator and version are you using (if applicable)?
Windows Terminal with PowerShell 7.6.4.
Codex doctor report
{
"overallStatus": "ok",
"codexVersion": "0.146.0",
"platform": "windows-x86_64",
"terminal": "Windows Terminal",
"os": "Windows 10.0.19045 (Windows 10 Pro) [64-bit]"
}
What issue are you seeing?
If an image-paste attempt fails while an assistant response is streaming, the visible TUI transcript can lose the prefix of the finalized assistant message. The raw rollout still contains the complete assistant message.
In a real session, the paste error was rendered between provisional assistant
cells. The exported visible transcript retained the numbered body after the
error, but omitted the introductory line that was present in the corresponding
raw response_item.
This is a transcript consolidation problem rather than model-output loss.
ChatWidget immediately commits the paste error as a history cell. At response
completion, handle_consolidate_agent_message only consolidates the trailing
contiguous run of AgentMessageCells. The interleaved error cell splits that
run, so the earlier provisional assistant cell is left outside the canonical
AgentMarkdownCell replacement.
Relevant source locations on current main:
codex-rs/tui/src/chatwidget/interaction.rs: theCtrl+V/Alt+Verror path callsadd_to_history(new_error_event(...))immediately.codex-rs/tui/src/app/agent_message_consolidation.rs:handle_consolidate_agent_messageuses a contiguous trailing-run search before replacing provisional cells with the finalized markdown cell.
What steps can reproduce the bug?
- Start a Codex TUI task that produces a response long enough to stream several lines.
- Put plain text, not an image, on the clipboard.
- While the assistant response is still streaming, press
Alt+Vto triggerFailed to paste image: no image on clipboard. - Let the response complete.
- Inspect or export the visible transcript and compare the finalized assistant message with the session rollout JSONL.
A deterministic app-level regression can model the same ordering with these committed cells before consolidation:
AgentMessageCell(first line)
PlainHistoryCell(paste error)
AgentMessageCell(stream continuation)
On current main, consolidation leaves four transcript cells where the correct
result has three: prior history, one canonical AgentMarkdownCell, and the
preserved paste error. The failure reproduces consistently in the focused test.
What is the expected behavior?
Finalizing a streamed assistant message must preserve its complete canonical markdown source even when a local history event is committed during the stream. The local paste error should remain visible without splitting, replacing, or hiding any part of the assistant response.
Additional information
A minimal local patch changes agent-message consolidation to find the active
stream across interleaved local history cells, replace all of its provisional
AgentMessageCells with one canonical AgentMarkdownCell, and then preserve
the interleaved cells after that finalized message in their original relative
order. A guard prevents consolidation from crossing a non-stream transcript
tail.
Local validation:
- Regression before the patch: failed consistently with
left: 4,right: 3. - Focused consolidation tests after the patch: 6 passed.
just fix -p codex-tui: passed with no clippy diagnostics.- Human TUI validation on the patched Windows binary: two complete
BEGIN/ 1-through-30 /ENDresponses remained intact in both normal history and the Ctrl+T transcript after four paste-failure events. No response prefix, body item, or final line was hidden. - Full
just test -p codex-tui: 3,327 passed; 7 unrelatedapp::tests::safety_bufferingtests timed out waiting for mock Responses API requests on this Windows environment. The 7 tests fail identically when run in isolation and do not touch transcript consolidation.
Related but distinct reports:
- #17103 covers text/image routing for
Ctrl+V; this bug remains possible for any legitimate image-paste failure during streaming. - #16801 covers missing reasoning summaries and stream event ordering.
- #15001 covered older subagent replay corruption, not main-TUI history cells interleaved during agent-message consolidation.
The repository currently accepts external pull requests by invitation only. A focused commit with the regression test is ready locally. Would maintainers like this submitted as an invited PR, or prefer a different consolidation invariant?
adding a second symptom that looks like the same root cause: the finished answer shows up twice after a terminal resize when a warning lands mid-stream.
how we found it: reading the code, then a regression test in our local patch (warning_inside_stream_does_not_duplicate_answer_on_resize) that fails on unpatched code and passes with the fix. we have not reproduced it live in a real session yet. the same code is still on rust-v0.160.0 (agent_message_consolidation.rs:56) and on main at b741e480e2 (agent_message_consolidation.rs:52).
trigger is any history cell committed while an answer is streaming. two common ones on main: usage warnings, since chatwidget/rate_limits.rs:393 calls add_to_history(new_usage_warning_event(..)) right away, and server or guardian warnings via on_warning in chatwidget/turn_runtime.rs:554. rate-limit reset hints are already deferred while the tail is an AgentMessageCell; usage warnings are not.
likely repro, not yet confirmed live: get a warning to land while a multi-line answer is streaming, let the answer finish, then resize the terminal narrower and back. expected is the answer once with the warning before or after it. per the test, what the code produces is the streamed lines from before the warning, then the warning, then the full consolidated answer repeating those lines. it looks fine until the resize, so it's easy to miss.
why: handle_consolidate_agent_message finds the cells to replace with trailing_run_start::<AgentMessageCell> (app/resize_reflow.rs:62), which only walks back over the contiguous trailing run. the warning breaks the run, so the first-line AgentMessageCell and any continuation cells before it stay in transcript_cells, and the new AgentMarkdownCell is added next to them. source-backed reflow on resize renders both. in #36577 the same split loses the prefix; on resize it duplicates.
the fix we carry on 0.159.3, covered by that test: find the start at the most recent first-line AgentMessageCell (is_stream_continuation() false), consolidate only the AgentMessageCells in that range into the AgentMarkdownCell, and re-insert the other cells (the notices) right after the answer in their original order. we also have a port to main, untested as a whole, which adds two pieces beyond the 0.159.3 fix: the backward search stops at the last UserHistoryCell and falls back to trailing_run_start, and when notices move it rebuilds the overlay with replace_cells and uses ConsolidationScrollbackReflow::Required so scrollback shows the new order right away.
test shape for the port (not run yet): transcript of earlier history, a 3-line first AgentMessageCell, a usage warning, then a continuation with "DONE", plus a variant with the warning after the last chunk. after consolidation, render at widths 120, 60, 120 through render_transcript_lines_for_reflow and the Ctrl+T overlay and assert each line shows exactly once with the warning after "DONE".
possibly related: #14805 (same response shown twice, no repro given).
happy to share the main diff (about 60 lines plus the test) if it helps. would only open a PR if you want one.
Thanks for the bug report. This will be addressed in an upcoming release.