Repository navigation
fix(sidebar): let the scroll anchor follow a re-keyed row - #11543
Conversation
Creating a child worktree folds the parent and its children into one lineage-group render row, which rewrites the parent's virtual row key. A scroll anchor recorded under the old key could no longer find it, fell through to a fallback key, and pinned a different row to the viewport top -- a measured +180px jump with no sidebar reveal involved. Give restore an optional old-key -> new-key map so an anchor can follow its own row. The within-row offset transfers for a followed key (same row, new name) but is still dropped for a genuine fallback (different row).
Greptile SummaryFixes a second independent cause of the left sidebar jumping by teaching the scroll anchor restore to follow a row that was re-keyed in place rather than mistaking it for a deletion. When a workspace gains its first child,
Confidence Score: 5/5Safe to merge. The change is well-contained: the new resolution branch is only reached when a caller explicitly supplies rekeyedRowKeys, and the sole caller (WorktreeList) derives that map from the same renderRows it already passes. All existing callers without the map degrade byte-for-byte to the prior two-step logic. The fix is minimal (~40 lines of logic), every branch in the restore function is exercised by the new tests including exact-key, followed-key, genuine-delete, stale-map, and dissolve-clamp paths, and the regression scenario (scrollTop 700 vs 520) is reproduced and verified green in both DOM and measured paths. The key construction in buildLineageRowRekeyMap is consistent with getRenderRowKey and buildRenderableRows. No new layout effects, no changes to the scroll-listener path, and CombinedDiffViewer is explicitly covered by a regression test. Files Needing Attention: No files require special attention. The core logic change in virtualized-scroll-anchor-restore.ts is thoroughly tested, and worktree-list-virtual-rows.ts gains its new export alongside matching unit tests.
|
| Filename | Overview |
|---|---|
| src/renderer/src/hooks/virtualized-scroll-anchor-restore.ts | Core fix: adds a "followed key" resolution step (old key → new key via rekeyedRowKeys) between exact-key and fallback paths, with offset transfer and height-clamp logic for the followed branch. |
| src/renderer/src/components/sidebar/worktree-list-virtual-rows.ts | getRenderRowKey moved here from WorktreeList.tsx; new buildLineageRowRekeyMap builds the bidirectional fold/dissolve lookup table used by the restore fix. |
| src/renderer/src/components/sidebar/WorktreeList.tsx | Net -19 lines: getRenderRowKey re-exported from its new home; lineageRowRekeys memo added and wired into useVirtualizedScrollAnchor as rekeyedRowKeys. |
| src/renderer/src/hooks/useVirtualizedScrollAnchor.ts | rekeyedRowKeys option added to the options type, destructured, passed through to runVirtualizedScrollAnchorRestore, and included in the restore effect's deps array. |
| src/renderer/src/hooks/useVirtualizedScrollAnchor.listener-deps.test.ts | New test suite directly exercises runVirtualizedScrollAnchorRestore with fold/dissolve/delete/stale-map scenarios and a full hook integration test; existing listener-deps tests are unmodified. |
| src/renderer/src/components/sidebar/worktree-list-virtual-rows.test.ts | Six new tests for buildLineageRowRekeyMap covering fold, dissolve, round-trip, cross-section isolation, non-lineage rows, and empty input. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["anchor.key"] --> B{rowIndexByKey\nhas exact key?}
B -- yes --> E["resolvedKey = anchor.key\noffset = anchor.offset\nno clamp"]
B -- no --> C{rekeyedRowKeys\nhas anchor.key?}
C -- no --> D{fallbackKeys\nhas a rendered key?}
C -- yes --> F{followedKey\nrendered this pass?}
F -- no --> D
F -- yes --> G["resolvedKey = followedKey\noffset = anchor.offset\nclamped to row height"]
D -- yes --> H["resolvedKey = fallbackKey\noffset = 0\nnew row semantics"]
D -- no --> I["unresolvable:\npendingRestoreRef = false\nreturn"]
E --> J["restoreFromDomElement\nor restoreFromMeasuredItem\nor scrollToIndex"]
G --> J
H --> J
Reviews (1): Last reviewed commit: "fix(sidebar): let the scroll anchor foll..." | Re-trigger Greptile
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughThe change centralizes render-row key generation and adds lineage row re-key mappings for fold, unfold, and dissolve transitions. The virtualized scroll-anchor hook and restore function now resolve changed row keys and clamp offsets to current row heights. The sidebar supplies computed mappings while preserving the existing key export. Tests cover mapping behavior, fallback restoration, stale targets, offset clamping, and integrated sidebar anchor updates. 🚥 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 |
Description
Second, independent cause of "the left sidebar scrolls by itself". #11530 fixed the reveal path; this one has nothing to do with reveal.
Creating a child worktree makes
buildRenderableRowsfold the parent and its descendants into a singlelineage-grouprender row, which rewrites the parent's virtual row key. A scroll anchor recorded under the old key could no longer find its row, fell through to a fallback key — a different row — and pinned that to the viewport top.Measured: scrollTop 520 → 700, a +180px jump, with no
revealWorktreeInSidebarinvolved.ELI5
The sidebar remembers your place with a bookmark: "the row at the top is this one, and you're 20px into it."
When a workspace gets its first child, Orca merges the parent and its children into one combined row — and in doing so gives it a new name. The bookmark still said the old name, and nothing answered to it anymore. So the sidebar reached for its backup plan: "if the bookmarked row is gone, it must have been deleted, so pin whatever slid up into its place." But the row wasn't deleted — it was standing right there under a new name. The sidebar pinned the next row instead, and everything lurched 180px.
Now the list hands restore a small "this row was renamed to that" note, so the bookmark can follow its own row. The backup plan still applies to genuinely deleted rows.
Root cause
getRenderRowKeygives the same workspace two different keys depending on whether it currently has children:runVirtualizedScrollAnchorRestoreresolvedanchor.key, thenanchor.fallbackKeys. Those fallbacks encode a specific assumption — "the anchored row was removed and the row below slid up" — which is only sound for deletions. A re-key is not a deletion, so the fallback silently pinned a neighbour and forced the within-row offset to 0.The reproduction's own decomposition proves the fix and rules out the obvious alternative:
lineage-groupkey, original offsetSo the defect was never the
offset = 0line; it was that the anchor had no way to follow a row whose identity was rewritten.Design
A caller-supplied
rekeyedRowKeys?: ReadonlyMap<string, string>. Resolution becomes: exactanchor.key→ followed (re-keyed) key, if it names a row in this render →anchor.fallbackKeys.The offset transfers for a followed key (same row, new name) but is still dropped for a genuine fallback (different row) — the deletion semantics are preserved exactly. On the followed branch only, the offset is clamped to the resolved row's height, mirroring the record-time clamp, so a group dissolving back into a single card cannot overshoot.
WorktreeListowns the key scheme, so it builds the map; the hook stays generic.getRenderRowKeymoved toworktree-list-virtual-rows.tsalongside the other row helpers (still re-exported fromWorktreeListfor existing importers) —WorktreeList.tsxnet shrinks by 19 lines.What this deliberately does not do
An earlier design added a general "stable row identity" subsystem — a new public anchor field, a new hook option, a record-time transform, a resolver type, and 3 new test files. An adversarial review rejected it: the generality was inert for 6 of 7 row shapes, and it introduced a bug where an unclamped offset was written back into the anchor. This ships ~40 lines instead. If
pending → realor pin/unpin are ever shown to jump in practice, they extend the same map with one entry each — on evidence.The sidebar's missing
restoreSignalis not bundled. It is verifiably non-curative here: at the moment the child lands no user scroll has happened, so the divergence guard passes while the structural gate opens, and restore still lands at 700. It is a real gap, but a separate PR.Proof of regression
Reverting only the resolution change (back to exact-key → fallbacks):
Exactly the reported +180px. I reproduced this red independently of the agent that wrote the tests. The three control tests stayed green under the revert, confirming they are not coupled to the follow branch.
Proof of fix
pnpm run typecheck:web: passed.pnpm run check:code-quality:changed: 0 new findings across 6 files.oxlint+oxfmt --checkclean.Proof of no regression
CombinedDiffVieweris provably unaffected — it is the only other consumer of this hook, never passesrekeyedRowKeys, sofollowedKeyis alwaysundefinedand resolution degenerates byte-for-byte to today's two-step logic. Covered by explicit "no rekey map supplied" tests asserting the old 700 behavior still happens.rekeyedRowKeysis read only in the restore effect. No newuseLayoutEffect.getRenderRowKeyoutput is unchanged, so the reveal path (findPreferredRenderRowIndexForWorktree,revealMountedWorktreeElement), drag, context-menu, andelementsCachepruning all keep their existing key namespace.Known limitation
The jump requires the anchored top-visible row to be exactly the parent gaining its first child (or losing its last). If that parent sits above or below the anchor row, the anchor key survives and restore already held position correctly. Scrolling within an already-formed lineage group as it grows is a separate composite-row concern and is not addressed here.
Made with Orca 🐋