Skip to content

fix(terminal): stop phantom pinned-viewport pins from freezing follow-output - #8625

Merged
Jinwoo-H merged 1 commit into
mainfrom
Jinwoo-H/weird-terminal-behavior
Jul 13, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
Jinwoo-H/weird-terminal-behavior

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Symptoms

While an agent TUI (e.g. Codex inline mode) generates output:

  • The terminal stops following output; committed rows stay static while the live TUI region visibly scrolls "underneath" them.
  • After a long generation, the scroll position ends up near the top of the scrollback.
  • Switching back to a workspace/terminal sometimes lands the viewport near the top.

Root cause

The scroll-intent system (#6319) can latch pinnedViewport while the viewport is actually at the bottom, and per-write-batch enforcement then actively freezes the viewport at that absolute buffer line:

  1. Phantom pins. onWheel pins on any upward delta and preservePinnedAtBottom keeps the pin even when the viewport provably never moved — sub-row trackpad deltas, wheels consumed by a mouse-reporting TUI (pane-terminal-tui-wheel-reports), and plain PageUp/Home (delivered to the PTY app, never scrolling xterm) all latch a pin at the bottom.
  2. One-way sticky freeze. Every write batch does capture → write → enforce; for a pinned intent, enforcement yanks the viewport back to the pre-write line and re-latches pinned. Nothing on the output path (including typing) could ever flip pinned back to follow. The frozen viewport straddles the scrollback/live-screen boundary — that's the "content scrolls underneath static rows" artifact — and as thousands of lines append below, the frozen absolute line becomes "near the top".
  3. Raw row-index restores across rebuilds. Snapshot replay (applyMainBufferSnapshot) discards and rebuilds the buffer, and safeFit reflows can renumber every row; restoring the pre-rebuild numeric viewportY afterwards points at arbitrary content.

Fix

  • syncTerminalScrollIntentSoon's final settle tick reclassifies from the real viewport (drops preservePinnedAtBottom), so a wheel/keypress the viewport never followed cannot latch a pin.
  • captureTerminalWriteScrollIntent treats a pinned intent whose live viewport is at the bottom as followOutput — a latched phantom pin can no longer freeze streaming (defense-in-depth for any latch source, including stale durable records).
  • enforceTerminalCurrentScrollIntent (visibility resume) follows output for pins recorded at the bottom, and restores by bottom offset when the live buffer is shorter than the stored intent (buffer was rebuilt).
  • applyMainBufferSnapshot restores by bottom offset via the new restoreBy: 'bottomOffset' enforce option.
  • safeFit restores pinned panes through an xterm marker (same pattern as pane-scroll.ts), so reflow renumbering cannot misplace the restore; follow panes keep scroll-to-bottom.

Genuine pins are untouched: scrolling up and staying above the bottom still pins, and output/fit/replay still preserve that position (existing tests cover this; new guard test added).

Deterministic reproduction harness

  • Unit (terminal-scroll-intent.test.ts, pane-tree-ops.test.ts): 5 new behavior tests were red on the old code — phantom-pin settle, pinned-at-bottom write freeze (two batches), resume-follow for bottom pins, bottom-offset restore across a shrunk rebuild, marker restore across fit reflow — plus a guard test (green before and after) that a real pin still sticks.
  • E2E (terminal-scroll-intent-follow.spec.ts + streaming-scrollback-fixture.cjs): a stdin-phase-gated streaming fixture makes the failure order-deterministic. Test 1: a sub-row (−2px) wheel-up that never moves the viewport, then phase-2 streaming. Test 2: a plain Home keypress mid-stream (its escape bytes double as the phase-2 release). Both fail without the fix and pass with it (verified by stashing the src changes and re-running).

Verification

  • vitest pane-manager + terminal-pane suites: 206 files, 2492 tests pass.
  • Full unit suite: only 4 pre-existing failures remain (sidebar/PR-comment suites — verified failing on clean HEAD too, unrelated).
  • pnpm typecheck and pnpm lint pass.
  • New e2e spec: 2/2 pass on fix, 2/2 fail without it.

Made with Orca 🐋

…-output

A pinnedViewport intent could latch while the viewport was still at the
bottom (sub-row trackpad wheel, wheel consumed by a mouse-reporting TUI,
plain PageUp/Home delivered to the app). Per-write-batch enforcement then
froze the viewport at that absolute line: during agent generation the live
TUI region visibly scrolled "underneath" static committed rows, and after a
long generation the scroll position sat near the top. Rebuild paths
(snapshot replay, fit reflow) additionally restored pinned positions by raw
pre-rebuild row numbers into renumbered buffers.

- The wheel/keyboard settle tick reclassifies intent from the real viewport
  instead of preserving a pin the viewport never followed.
- Write capture treats a pinned intent whose live viewport is at the bottom
  as followOutput, so a latched phantom pin can never freeze streaming.
- Visibility-resume enforcement follows output for pins recorded at the
  bottom and restores by bottom offset when the buffer was rebuilt shorter.
- Snapshot replay restores by bottom offset; safeFit restores pinned panes
  through an xterm marker so reflow renumbering cannot misplace them.

Deterministic repro harness: unit tests red on the old code for each latch/
freeze/restore shape, plus a stdin-phase-gated streaming e2e fixture — both
e2e tests fail without this fix and pass with it.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1b55723b-0110-44ef-95b2-d7651ce6f9f7

📥 Commits

Reviewing files that changed from the base of the PR and between 469018e and e45331f.

📒 Files selected for processing (7)
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/lib/pane-manager/pane-tree-ops.test.ts
  • src/renderer/src/lib/pane-manager/pane-tree-ops.ts
  • src/renderer/src/lib/pane-manager/terminal-scroll-intent.test.ts
  • src/renderer/src/lib/pane-manager/terminal-scroll-intent.ts
  • tests/e2e/fixtures/streaming-scrollback-fixture.cjs
  • tests/e2e/terminal-scroll-intent-follow.spec.ts

📝 Walkthrough

Walkthrough

Updates terminal scroll-intent handling to distinguish viewport-line and bottom-offset restoration, avoid phantom pins, and track buffer base positions. safeFit now preserves pinned viewport state across reflow and synchronizes restored intent. Snapshot replay uses bottom-offset restoration. Unit tests cover wheel, visibility, fitting, and buffer-rebuild cases, while new end-to-end tests verify continued follow-output behavior during streamed scrollback.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is detailed but does not follow the required template and omits screenshots, testing, AI review, security, and notes sections. Rewrite it using the repo template and add Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main fix: preventing phantom pinned-viewport states from freezing follow-output.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H
Jinwoo-H merged commit 83588b4 into main Jul 13, 2026
5 checks passed
Jinwoo-H added a commit that referenced this pull request Jul 14, 2026
…ing snapshot restores

Returning to a worktree whose agent kept streaming (Codex "Working…")
replays a main-buffer snapshot: the buffer is cleared and rewritten, but
xterm parses those bytes asynchronously. Scroll-intent capture/enforce that
raced the parse — live stream batches, safeFit, visibility resume, and the
restore's own synchronous enforcement — read the half-cleared buffer and
re-latched the intent as "pinned at line 0", parking the returned pane at
the very top of the scrollback (#8625 does not cover this).

- Bracket the snapshot replay with a per-terminal rebuild marker
  (terminal-scroll-intent-rebuild.ts); while it is in flight, intent
  capture returns null and enforcement no-ops, so no racer can latch
  transient coordinates.
- Re-apply the pre-replay intent once, in a FIFO-ordered empty-write
  completion after the replay bytes have parsed (bottom-offset restore),
  with a timeout fallback so a wedged pipeline cannot suppress intent
  forever.
- Defense-in-depth for unbracketed rebuilds: enforcement no longer
  re-latches a pinned intent from a scrollback shorter than the stored
  pin's, and capture only normalizes pinned-at-bottom to follow when the
  scrollback is at least as long as the pin's.
- Split the DOM wheel/pointer tracking into
  terminal-scroll-intent-dom-tracking.ts (max-lines).

Deterministic e2e repro: a synchronized-output streaming fixture, pinned
six rows above bottom, worktree switch away and back — settled at
viewportY=0 (baseY=284) before this fix, restores near the pin after.

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant