Portal: invalidate the split-divider hit-test cache on nested subview insertion

Merged
EJ
4mo ago
+1574-44

What you'd hit

After an NSSplitView is inserted, removed, replaced, reordered, or changed inside a nested container, terminal and browser portal hit testing can keep using stale divider regions until an unrelated geometry change refreshes the cache.

Root cause

The original cache validity key covered only the content root's direct children. Nested mutations do not change that list, and NSView.subviews is not documented as KVO-compliant. A later full-tree digest restored correctness but moved an O(view-count) traversal onto every pointer lookup, which is the latency-sensitive path this cache exists to protect.

A generation index also needs a provenance boundary: a subtree indexed as containing no split views can be detached, mutate while no window tracker can observe it, and then be reattached with a stale proof.

Fix

Both portal hosts now retain the weak identity of the root that produced their cached regions and validate that cache through one window-owned PortalViewHierarchyMutationTracker:

  • PortalSplitDividerCacheInvalidator installs one process-wide hook for every supported public NSView hierarchy mutation entrypoint: both addSubview forms, the subviews setter, both removal methods, replacement, and sortSubviews.
  • A cache miss records per-view split-presence state and the current window generation. Cache hits validate root identity, window identity, generation, root registration, and live divider regions without walking the hierarchy.
  • Mutations touching a split-bearing indexed branch, an inserted split, an unknown nonempty subtree, or a detached/cross-window subtree advance the generation and fail closed.
  • Indexed no-split state is reused only across continuous same-window moves, where every intervening hierarchy mutation passed through the same tracker. New leaves remain a constant-time fast path.
  • sortSubviews records identity order only for split-bearing indexed parents and invalidates only when the order changes. Browser interaction-layer sorting now skips already ordered subviews.
  • Existing frame, bounds, and visibility observation still handles divider geometry; structural correctness no longer depends on subview KVO.

The hook methods are resolved as a complete set before any implementation exchange, so installation cannot leave a partially hooked mutation surface.

Tests and performance proof

PortalHitTestingPerformanceTests covers nested/deep insertion, removal, replacement, content-root changes, detached root and nested-subtree mutation, hidden-to-visible transitions, moves, no-op and real sorting, and pointer-cache reuse.

  • Exact-SHA red proof at 342b48d2c18f1e44a1625c5bdf257f47bf7a3ba0: 14 tests executed; the new detached nested-subtree regression was the sole failure.
  • Exact-SHA green proof at 053695f2d7c88f8abdd6e90d9de5d3e59988f95c: all 14 tests passed.
  • Current-main exact-head revalidation at cab9967802bf8e002cf5fbf54b4b61f5b67a313e: all 14 tests passed after merging origin/main at 6089fa04d3effd27e43c5c6104a4eada62fe859f.
  • The bounded-work test compares insertion against AppKit's own identical no-cache attachment baseline across a 1,000-view prebuilt subtree and 128 active portal caches. Portal tracking adds only constant work, and cache hits do not re-walk 1,000 unrelated containers.

Project normalization, test wiring (654 files), workspace/package policy, app-host isolation/retry tests, script syntax, and git diff --check pass locally. No warning-budget or file-length-budget file is changed.

Required PR CI is waiting for the contributor fork to fast-forward from stale PR head 7346268746cff72113c85d9bc5b09f7d8cdc357b to canonical head cab9967802bf8e002cf5fbf54b4b61f5b67a313e. Current main CI independently reproduces the unrelated app-host/session failures seen in the earlier exact-branch full run; their owning repair PRs remain upstream.

Review

Canonical branch autoreview at cab9967802bf8e002cf5fbf54b4b61f5b67a313e reports no accepted/actionable findings (patch is correct, 0.9 confidence). Its merge-conflict gate against current origin/main and the cmux policy gate are clean. All three actual inline review threads are resolved.

Localization audit

No user-facing strings were added or changed.


[!NOTE]
High Risk
Process-wide method swizzling on NSView/NSSplitView affects every hierarchy mutation in the app; incorrect hook or generation logic could cause stale divider hit-testing or extra cache churn on the pointer path.

Overview
Fixes stale split-divider hit-test regions when NSSplitView layout changes in nested containers, after detach/reattach, or via arranged-subview APIs—without walking the full view tree on every pointer move.

Terminal and browser portal hosts now tie cache validity to the weak cached root plus PortalSplitDividerCacheInvalidator.isHierarchyCurrent, replacing checks on direct child identity lists.

New window-owned tracking (PortalViewHierarchyMutationTracker, registration tokens, per-view node state) records split-presence at cache build and bumps a generation when divider-relevant structure changes. Process-wide AppKit hooks on NSView/NSSplitView mutation entry points feed that tracker; subview KVO for structure is removed. Detached or cross-window mutations and unknown nonempty subtrees fail closed; split-free same-window moves can stay on a fast path.

PortalSplitDividerRegion.collect now returns hierarchy nodes for indexing instead of structure-only observation lists. The browser slot view skips sortSubviews when interaction-layer priorities are already ordered.

Tests cover cache reuse, deep insertion, arranged-subview changes, content-root swaps, detached proofs, and bounded work across many caches.

Reviewed by Cursor Bugbot for commit dfe3fd79d5a96a0e58c6cedd250d3e1e9d3639bd. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Performance

    • Improved split-divider detection by reusing cached hierarchy information when the window structure remains unchanged.
    • Reduced unnecessary view hierarchy traversal and interaction-layer reordering.
  • Bug Fixes

    • Cache results now refresh reliably after view insertion, removal, replacement, reordering, detachment, or reattachment.
    • Improved handling of nested split views, content-root changes, and inactive windows.
  • Tests

    • Added comprehensive coverage for cache reuse, hierarchy mutations, deep view insertion, reordering, and detached subtrees.