Skip to content

fix(sidebar): stop a drifted virtualizer offset pinning the wrong project header - #18662

Closed
ccueto36 wants to merge 2 commits into
stablyai:mainfrom
ccueto36:project-navigation-ui-render-issue
Closed

ccueto36 wants to merge 2 commits into
stablyai:mainfrom
ccueto36:project-navigation-ui-render-issue

Conversation

@ccueto36

@ccueto36 ccueto36 commented Sep 4, 2026 •

Copy link
Copy Markdown

ELI5

The sidebar keeps a note of how far it thinks it has scrolled. When a workspace card grew in a list too short to scroll, that note moved even though the list did not. The pinned project header is chosen from that note, so the wrong project got pinned over the first row and left a hole where it should have been. Now the note only moves for rows that are actually above the top of the list, and if it ever ends up somewhere the list cannot reach, it is snapped back to where the list really is.

What Changed

  • use-scroll-suppression.ts / use-virtualizer.ts: shouldAdjustScrollPositionOnItemSizeChange now applies TanStack's fold rule on top of the existing scrolling and suppression gates. A first measurement corrects only when the row's top is above the viewport top; a re-measurement only when the row is entirely above it and the scroll direction is not backward.
  • scroll-offset-reconcile.ts (new) + use-row-measurement.ts: after layout, a believed offset the element cannot reach (beyond scrollHeight - clientHeight) is snapped back to the element's scrollTop and the list re-renders so range and sticky slots follow the DOM. Only an unreachable belief is touched; a reachable difference can be an in-flight scroll write whose event has not landed yet. The effect depends on isScrolling so a check skipped mid-scroll reruns once scrolling settles.
  • Tests: scroll-adjustment.test.ts (fold-aware predicate), scroll-offset-reconcile.test.ts (reconcile rules, the reported scenario, the in-flight → settled transition), scroll-offset-drift.test.ts (drives the real Virtualizer against an unscrollable element and shows the old predicate drifting scrollOffset while the new one holds 0).

Why

The active project header renders sticky in normal flow at the top of the list and its own virtual slot is left empty. That is only right when the pinned header is the first row. Which header pins is derived from the virtualizer's remembered scrollOffset (getActiveStickyIndexesForScroll and the range extractor's startIndex).

TanStack Virtual 3.17 books every size correction into scrollOffset before the element scrolls (applyScrollAdjustment) and only re-reads the element on a scroll event. The sidebar predicate returned true for every measured resize, replacing TanStack's default "only rows above the fold" rule with "always" (its intent, per #2175, was only to stay quiet during wheel input). So a card growing under a project, in a list shorter than the viewport, moved the believed offset to roughly 70 px while the element stayed at 0, and no scroll event ever corrected it. The header at that offset pinned over row 0 and left its slot blank; expanding a project shifted the indices and a different header pinned, which reads as a reorder.

Two reviewer notes: the predicate change also removes a pre-existing visible jump when a card below the viewport grows in a scrollable list, and the reconcile writes virtualizer.scrollOffset directly, which is a public field but not a documented mutation path.

Linked Issue

Fixes #18667

Visual Proof

Before: 12s screen recording of the bug. Project names are redacted to "Project A" through "Project E"; Project A is the one being collapsed and expanded. Watch for the blank band under Project A while it is collapsed, and Project A jumping above Project B when it is expanded. There are six projects in the list; the sixth is never visible because the mis-pinned header sits over it.

Screencast_20260904_091046_redacted.webm

After: not captured on a packaged build. The behavior is pinned by scroll-offset-drift.test.ts, which runs the real TanStack Virtualizer against an unscrollable element and asserts the offset stays at 0 under the new predicate, and by the scenario test that shows index 0 pinning once the offset is reconciled.

Testing

Linux only. Automated, not a manual run of a packaged build:

  • tsc --noEmit -p config/tsconfig.tc.web.json clean

  • oxlint, oxfmt --check, and check-changed-code-quality.mjs clean

  • vitest over src/renderer/src/components/sidebar: 294 files, 2501 tests passing before the follow-up commit; viewport + WorktreeList suites re-run after it, 131 tests passing

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Diagnosed and implemented with Claude Code (Claude Fable 5.1): frame extraction from the screen recording, tracing the sticky-header and TanStack scroll-offset behaviour, the code changes, and the tests. The author reviewed the diagnosis and the diff, directed the fix and the review follow-up, and ran the verification listed above.

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Renderer-only change in the sidebar virtualizer; no platform, SSH, path, or wire-format surface is touched. Performance: the reconcile runs in an existing layout effect, reads three properties of the scroll element, and re-renders only when it corrects something.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…ject header

Collapsing and expanding projects left a header-sized gap under one project,
made another project appear to jump above its neighbour, and hid the true first
project row entirely.

The active project header is rendered in normal flow at the top of the list and
its own virtual slot is left empty, which is only correct when the pinned header
is the first row. Which header pins is derived from TanStack's remembered
scrollOffset. TanStack 3.17 books every size correction into that offset before
the element scrolls and only re-reads the element on a scroll event. Orca's
shouldAdjustScrollPositionOnItemSizeChange returned true for every card resize,
including rows below the fold, so a card growing under a project in a list
shorter than the viewport moved the believed offset (~70px) while the element
stayed at 0. The header at that offset then pinned over row 0 and left its slot
blank; expanding a project shifted the indices and a different header pinned.

- Apply TanStack's fold rule inside the sidebar predicate: correct only rows
  above the viewport top (first measure) or entirely above it (remeasure, not
  scrolling backward), on top of the existing input-quiet gates. This also
  removes the visible jump when a card below the viewport grows in a
  scrollable list.
- Reconcile a believed offset the element cannot reach back to the element
  after layout and re-render so range and sticky slots follow the DOM.
- Cover the predicate, the reconcile, and the drift itself against the real
  Virtualizer with an unscrollable element.
@ccueto36
ccueto36 force-pushed the project-navigation-ui-render-issue branch from 0568ca4 to 2403c49 Compare September 4, 2026 14:00
@coderabbitai

coderabbitai Bot commented Sep 4, 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: Team

Run ID: 26e2f580-f7cf-41f8-888c-a07b64453bff

📥 Commits

Reviewing files that changed from the base of the PR and between 0568ca49a92aeb8ccb5e9ebda4690088e296f2be and 2c59a69.

📒 Files selected for processing (3)
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-reconcile.test.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-reconcile.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/use-row-measurement.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/sidebar/worktree-list/viewport/use-row-measurement.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-reconcile.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


📝 Walkthrough

Walkthrough

The worktree sidebar now uses row geometry, measurement state, and scroll direction to decide when size changes can adjust scroll position. New helpers reconcile unreachable virtualizer offsets with the actual scroll element. The row measurement hook applies this correction during a layout effect and triggers a render when needed. Tests cover fold-aware adjustment, unscrollable sidebars, offset clamping, tolerance, and sticky-header restoration.

Merge Risk: ⚪ Minimal · up to 2c59a

This change keeps sidebar virtual scroll state aligned with the actual scroll position, preventing incorrect sticky project headers and layout gaps during project resizing. The targeted behavior and integration coverage indicate no remaining merge-blocking risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 7 files.
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.
Title check ✅ Passed The title clearly and concisely describes the primary fix: preventing a drifted virtualizer offset from pinning the wrong project header.
Description check ✅ Passed The description is detailed and covers the change, rationale, linked issue, testing, AI disclosure, scope, and checklist. It includes a before recording, but the required after visual proof is not att…

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: cec9b500-6254-42a1-a542-e30553b89152

📥 Commits

Reviewing files that changed from the base of the PR and between 0d2375a and 0568ca49a92aeb8ccb5e9ebda4690088e296f2be.

📒 Files selected for processing (7)
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-adjustment.test.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-drift.test.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-reconcile.test.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/scroll-offset-reconcile.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/use-row-measurement.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/use-scroll-suppression.ts
  • src/renderer/src/components/sidebar/worktree-list/viewport/use-virtualizer.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

The reconcile is skipped while a scroll is in flight, but the effect had no
dependency that would rerun it when isScrolling flips back, so a belief that
went unreachable mid-scroll could survive until the next structural change.
Extract the body into reconcileVirtualizerScrollOffset, depend on isScrolling,
and cover the in-flight to settled transition.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Fold-aware scroll-adjustment predicate — shouldAdjustWorktreeSidebarMeasuredRowScroll now layers TanStack Virtual's fold rule over the existing isScrolling/suppressUntil gates, so a row at or below the viewport top no longer books a scroll write the element can't honor.
  • Offset reconciliation — a new getReconciledVirtualScrollOffset plus a useLayoutEffect in use-row-measurement.ts snap an unreachable believed scrollOffset back to the element's scrollTop and re-render so range and sticky-header math follow the DOM.
  • Tests — fold-rule predicate cases, reconcile rules, and a real-Virtualizer drift harness that proves the old predicate drifts scrollOffset while the new one holds 0.

I traced the predicate against the vendored @tanstack/virtual-core 3.17.8 source (resizeItem → defaultShouldAdjust). The first-measure branch (itemStart < scrollOffsetWithAdj) and re-measure branch (itemStart + itemSize <= scrollOffsetWithAdj && scrollDirection !== 'backward') match the PR's itemStart/itemEnd conditions exactly, including the itemSize === item.end - item.start equivalence for the lazy measurementsCache view. The reconcile's guard (believedOffset <= scrollHeight - clientHeight + 1) correctly leaves reachable in-flight scroll writes alone and only touches beliefs the element provably cannot reach, so a legitimate scrollToIndex (whose target is clamped to getMaxScrollOffset()) can't be falsely corrected. The re-render path (useReducer + updated scrollOffsetRef) reads the corrected offset on the next getVirtualItems()/sticky-index pass, and the effect's deps (totalSize, renderRowKeySignature) mean it fires whenever a size correction books an offset but doesn't loop. The direct virtualizer.scrollOffset/scrollAdjustments write is undocumented, but there is no public setter, and the PR documents it honestly.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes (since 0568ca4)

  • reconcileVirtualizerScrollOffset — the mutation is extracted into a testable helper that separates the pure getReconciledVirtualScrollOffset math from the scrollOffset/scrollAdjustments/scrollOffsetRef writes and returns whether anything changed.
  • Re-run on scroll settle — virtualizer.isScrolling is read during render (isVirtualizerScrolling) and added to the useLayoutEffect deps, so a reconcile skipped mid-scroll runs again once scrolling settles (TanStack's debounced observeElementOffset fallback flips isScrolling → false and fires maybeNotify, which triggers the re-render that re-arms the effect).
  • Tests — the new reconcileVirtualizerScrollOffset suite covers the in-flight → settled transition and the already-correct no-op case, both with exact assertions.

The fix is sound: the isScrolling transition is observable (it feeds maybeNotify's memo deps), so the effect re-runs without polling, and the scrollOffset write doesn't change any of the effect's deps, so the useReducer-driven re-render can't loop. No further concerns.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker

Copy link
Copy Markdown
Contributor

Thanks @ccueto36 for the thorough investigation into the drifted virtualizer offset! This is already fixed on main by #20883 (3af09f8): shouldAdjustWorktreeSidebarMeasuredRowScroll now applies TanStack's fold rule beneath the existing scrolling/suppression gate (first measurements adjust only when the row starts above the anchor, remeasurements only when the row ends above it and the direction isn't backward), so a card growing in an unscrollable list no longer invents a scroll offset that pins the wrong project header. If you still see a header pinned over the first row or a gap on the latest build, please comment or reopen.

@nwparker nwparker closed this Oct 26, 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.

Sidebar: collapsed project leaves a gap and headers jump/pin over the first row (Recent order, unscrollable list)

2 participants