Repository navigation
Fix Codex pane output tearing after app occlusion - #7173
Conversation
Track the latest PTY seq accepted by hidden snapshot restore or foreground renderer writes, and drop hidden/background chunks already covered by that seq. This prevents late background stateful-query chunks from mutating xterm after a newer snapshot rebuilt the renderer buffer. Co-authored-by: Orca <[email protected]>
📝 WalkthroughWalkthroughThis change adds per-PTY renderer output ordering in ChangesRelated issues: None provided
Sequence Diagram(s)sequenceDiagram
participant PTY
participant pty-connection.ts
participant getChunkDataAfterSnapshot
participant xterm
PTY->>pty-connection.ts: onData(chunk, meta.seq)
pty-connection.ts->>pty-connection.ts: observeRendererOrderedSeqRegression(meta)
pty-connection.ts->>getChunkDataAfterSnapshot: getHiddenRendererDataAfterOrderedSeq(data, meta)
getChunkDataAfterSnapshot-->>pty-connection.ts: orderedRendererData or null
alt orderedRendererData is available
pty-connection.ts->>xterm: write orderedRendererData
else orderedRendererData is null
pty-connection.ts->>pty-connection.ts: mark hidden restore needed
pty-connection.ts->>pty-connection.ts: schedule pending startup delivery
end
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)
7024-7102: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueThe
seqmath (staleSeq < currentSeq, snapshot baseline =currentSeq) correctly models an out-of-order delayed background chunk, and the primary assertion (writesnever containsstaleQueryFrame) directly validates the drop.Minor note (optional): because every frame is prefixed with
\r\x1b[2K(carriage-return + erase-line), all three frames collapse onto the same buffer line, soreferenceBufferreduces to just the final frame's content. ThetoEqual(referenceBuffer)comparison is therefore a fairly weak buffer-state check — thenot.toContainassertion is doing the real work. If you want the equality check to also guard against a mis-sliced partial write landing on screen, consider frames that render to distinct lines (e.g. without the leading erase) so residual stale bytes would actually diverge the buffers.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 56f14c45-c147-438d-ab80-77a79651f037
📒 Files selected for processing (2)
src/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/components/terminal-pane/pty-connection.ts
Review of the ordered-seq filter found one real data-loss hole: the baseline outlives the pty stream that produced it. Main deletes its per-pty byte counter on runtime.onPtyExit, so a revived session that reuses the same ptyId (agent-hibernation wake, kill + cold-restore in a still-mounted pane) restarts seq near zero while the renderer still holds the old high-water mark. Every hidden/background chunk from the revived stream then reads as "already covered" and is dropped without marking a snapshot restore, permanently freezing hidden output for that pane; foreground writes can only ratchet the stale baseline upward. Two guards close it, each pinned by a regression test that fails with the guard removed: - Exit reset: any observed pty exit clears the ordered baseline for the exiting ptyId, since the exit ends that ptyId's seq domain. This covers all kill/revive flows that deliver pty:exit to the renderer, including suppressed-exit restarts that rebind the same id. - Channel-seq regression reset: pty:data delivery is FIFO per pty and main assigns seq from a monotonic counter, so an in-order arrival below the last seen seq can only mean the counter restarted without an observed exit (e.g. a failed shutdown that already tore down runtime state). Detecting that regression drops the stale baseline instead of letting it cover the new stream's bytes. The existing stale-query regression test was also reworked to deliver chunks in the only order the FIFO channel can produce (snapshot RPC outruns in-flight chunks; the stale query frame then arrives in order after the restore), instead of delivering a newer-seq chunk before an older one, which the regression detector now correctly treats as a stream restart. The reworked test still fails against the pre-fix renderer, so the original coverage is preserved. Both guards are constant-time comparisons on the existing per-chunk path: no allocations, polling, buffering, or PTY resizes. Co-authored-by: Orca <[email protected]>
Co-authored-by: Orca <[email protected]>
Summary
Fixes renderer-buffer tearing in Codex terminal panes after the app has been occluded while hidden-output snapshot recovery is active.
The failing layer is the renderer terminal buffer: raw PTY bytes and main-owned snapshots can be clean, while a late renderer delivery can still mutate the pane after a newer restore. This PR does not use
orca terminal readas evidence because its retention-tail normalization is known to flatten spinner frames.Root cause: after hidden snapshot restore or newer foreground bytes advanced the renderer to a later PTY seq, a delayed background-origin Codex chunk containing a stateful terminal query could still take the hidden live-write path when
hiddenRendererStateDirtywas false. That allowed an older redraw/query chunk to be written into the renderer after a newer snapshot rebuilt the buffer.Fix: track the latest PTY seq accepted by the renderer for the current PTY through hidden snapshot restore and foreground drains/writes. Hidden/background renderer data is filtered against that ordered seq before it can reach the terminal. Fully covered stale chunks are dropped; unmappable overlap falls back to main snapshot recovery instead of risking duplicate/stale renderer bytes.
Review hardening
Review coverage for this PR: an independent adversarial agent pass over the seq-filter machinery and its lifecycle, maintainer diff review, and CodeRabbit. (A separate terminal-based reviewer agent was also attempted but could not run due to model-gateway errors.)
The adversarial pass found one real hole in the original fix, closed in the follow-up commit:
runtime.onPtyExit— including suppressed/synthetic kills — so a revived session that reuses the same ptyId (agent-hibernation wake, kill + cold-restore into a still-mounted pane) restarts seq near zero while the renderer still holds the old high-water mark. Every hidden/background chunk from the revived stream was then judged "already covered" and dropped without marking a snapshot restore, permanently freezing hidden output for that pane; foreground writes could only ratchet the stale baseline upward. Two constant-time guards close it: any observed pty exit clears the baseline for the exiting ptyId, and a per-pty channel seq-regression detector (pty:datais FIFO per pty with a monotonic counter, so an in-order arrival below the last seen seq can only mean a restarted counter) covers restarts whose exit never reached the renderer. Each guard has a regression test verified to fail with that guard removed.Findings triaged and rejected (no code change):
markHiddenOutputRestoreNeeded) always has a snapshot source available: seq metadata is only attached by the local and SSH-relay data channels, both snapshot-capable; the remote-runtime transport sends no seq metadata, which leaves the filter inert there by design.Reliability proof
terminal-session.snapshot-freshness/ renderer hidden-output ordering. A hidden/background chunk must not mutate the renderer terminal buffer after the renderer has already accepted a snapshot or foreground stream position that covers that chunk's PTY seq — and that acceptance mark must not outlive the pty stream that produced it.config/reliability-gates.jsoncexists in this worktree, so there is no manifest entry to promote. Treat this as a partial gate carried bypty-connection.test.ts.Validation
pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts- 288 passedpnpm run typecheck- passedoxlinton changed files - cleanMade with Orca 🐋