Repository navigation
fix(browser): bound retained webview guests across worktree switches #12194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
136f826
fix(browser): bound retained webview guests across worktree switches
brennanb2025 d393388
fix(browser): merge duplicate imports flagged by import/no-duplicates
brennanb2025 402c19e
fix(browser): evict guests without unmounting the worktree surface
brennanb2025 6bf0942
fix(browser): veto eviction for downloading pages; keep zoom; add kil…
brennanb2025 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
src/renderer/src/components/browser-pane/browser-guest-worktree-retention.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import type { BrowserPage, BrowserWorkspace } from '../../../../shared/types' | ||
| import { | ||
| BROWSER_GUEST_HIDDEN_WORKTREE_RETENTION_LIMIT, | ||
| browserTabVisibilityPageIds, | ||
| selectBrowserGuestEvictionWorktreeIds, | ||
| touchBrowserGuestWorktreeRecency, | ||
| worktreeHoldsLiveBrowserGuests | ||
| } from './browser-guest-worktree-retention' | ||
|
|
||
| function browserTab( | ||
| id: string, | ||
| pageIds: string[] = [], | ||
| activePageId: string | null = null | ||
| ): BrowserWorkspace { | ||
| return { | ||
| id, | ||
| worktreeId: 'wt-1', | ||
| label: id, | ||
| sessionProfileId: null, | ||
| pageIds, | ||
| activePageId, | ||
| url: 'about:blank', | ||
| title: id, | ||
| loading: false, | ||
| faviconUrl: null, | ||
| canGoBack: false, | ||
| canGoForward: false, | ||
| loadError: null, | ||
| createdAt: 1 | ||
| } | ||
| } | ||
|
|
||
| function page(id: string, workspaceId: string): BrowserPage { | ||
| return { | ||
| id, | ||
| workspaceId, | ||
| worktreeId: 'wt-1', | ||
| url: 'about:blank', | ||
| title: id, | ||
| loading: false, | ||
| faviconUrl: null, | ||
| canGoBack: false, | ||
| canGoForward: false, | ||
| loadError: null, | ||
| createdAt: 1 | ||
| } | ||
| } | ||
|
|
||
| type SelectionOverrides = Partial<Parameters<typeof selectBrowserGuestEvictionWorktreeIds>[0]> | ||
|
|
||
| function selectEvicted(overrides: SelectionOverrides): string[] { | ||
| return selectBrowserGuestEvictionWorktreeIds({ | ||
| orderedWorktreeIds: [], | ||
| activeWorktreeId: null, | ||
| isRetained: () => true, | ||
| holdsLiveGuests: () => true, | ||
| isEvictable: () => true, | ||
| ...overrides | ||
| }) | ||
| } | ||
|
|
||
| describe('selectBrowserGuestEvictionWorktreeIds', () => { | ||
| const sixWorktrees = ['wt-1', 'wt-2', 'wt-3', 'wt-4', 'wt-5', 'wt-6'] | ||
|
|
||
| it('is a no-op while retained guest-holding worktrees fit the budget', () => { | ||
| expect( | ||
| selectEvicted({ | ||
| orderedWorktreeIds: sixWorktrees.slice(0, BROWSER_GUEST_HIDDEN_WORKTREE_RETENTION_LIMIT) | ||
| }) | ||
| ).toEqual([]) | ||
| }) | ||
|
|
||
| it('evicts the least-recently-activated worktrees beyond the budget', () => { | ||
| expect(selectEvicted({ orderedWorktreeIds: sixWorktrees })).toEqual(['wt-5', 'wt-6']) | ||
| }) | ||
|
|
||
| it('never evicts or counts the active worktree', () => { | ||
| // Active most-recent: five hidden holders remain, so only the LRU one goes. | ||
| expect(selectEvicted({ orderedWorktreeIds: sixWorktrees, activeWorktreeId: 'wt-1' })).toEqual([ | ||
| 'wt-6' | ||
| ]) | ||
| // Active in LRU position: it is spared even though it ranks past the budget. | ||
| expect(selectEvicted({ orderedWorktreeIds: sixWorktrees, activeWorktreeId: 'wt-6' })).toEqual([ | ||
| 'wt-5' | ||
| ]) | ||
| }) | ||
|
|
||
| it('counts only worktrees that actually hold live guests', () => { | ||
| const holders = new Set(['wt-5', 'wt-6']) | ||
| expect( | ||
| selectEvicted({ | ||
| orderedWorktreeIds: sixWorktrees, | ||
| holdsLiveGuests: (worktreeId) => holders.has(worktreeId) | ||
| }) | ||
| ).toEqual([]) | ||
| }) | ||
|
|
||
| it('skips worktrees that are no longer retained', () => { | ||
| expect( | ||
| selectEvicted({ | ||
| orderedWorktreeIds: sixWorktrees, | ||
| isRetained: (worktreeId) => worktreeId !== 'wt-1' && worktreeId !== 'wt-2' | ||
| }) | ||
| ).toEqual([]) | ||
| }) | ||
|
|
||
| it('keeps a non-evictable worktree retained over budget instead of evicting it', () => { | ||
| expect( | ||
| selectEvicted({ | ||
| orderedWorktreeIds: sixWorktrees, | ||
| isEvictable: (worktreeId) => worktreeId !== 'wt-5' | ||
| }) | ||
| ).toEqual(['wt-6']) | ||
| }) | ||
|
|
||
| it('lets a protected worktree within the budget occupy a retained slot', () => { | ||
| expect( | ||
| selectEvicted({ | ||
| orderedWorktreeIds: ['wt-1', 'wt-2', 'wt-3', 'wt-4', 'wt-5'], | ||
| isEvictable: (worktreeId) => worktreeId !== 'wt-3' | ||
| }) | ||
| ).toEqual(['wt-5']) | ||
| }) | ||
|
|
||
| it('counts duplicated recency entries once', () => { | ||
| expect( | ||
| selectEvicted({ orderedWorktreeIds: ['wt-1', 'wt-1', 'wt-2', 'wt-3', 'wt-4', 'wt-5'] }) | ||
| ).toEqual(['wt-5']) | ||
| }) | ||
|
|
||
| it('honors a custom limit', () => { | ||
| expect(selectEvicted({ orderedWorktreeIds: sixWorktrees.slice(0, 3), limit: 1 })).toEqual([ | ||
| 'wt-2', | ||
| 'wt-3' | ||
| ]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('worktreeHoldsLiveBrowserGuests', () => { | ||
| it('detects live guests keyed by page id', () => { | ||
| const tabs = [browserTab('workspace-1', ['page-1'])] | ||
| const pages = { 'workspace-1': [page('page-1', 'workspace-1')] } | ||
|
|
||
| expect(worktreeHoldsLiveBrowserGuests(tabs, pages, (id) => id === 'page-1')).toBe(true) | ||
| expect(worktreeHoldsLiveBrowserGuests(tabs, pages, () => false)).toBe(false) | ||
| }) | ||
|
|
||
| it('falls back to the workspace tab id for legacy sessions without pages', () => { | ||
| const tabs = [browserTab('legacy-workspace')] | ||
|
|
||
| expect(worktreeHoldsLiveBrowserGuests(tabs, {}, (id) => id === 'legacy-workspace')).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('browserTabVisibilityPageIds', () => { | ||
| it('mirrors the overlay slot derivation: pageIds, then activePageId, then tab id', () => { | ||
| expect(browserTabVisibilityPageIds(browserTab('tab-1', ['page-1', 'page-2']))).toEqual([ | ||
| 'page-1', | ||
| 'page-2' | ||
| ]) | ||
| expect(browserTabVisibilityPageIds(browserTab('tab-1', [], 'page-3'))).toEqual(['page-3']) | ||
| expect(browserTabVisibilityPageIds(browserTab('tab-1'))).toEqual(['tab-1']) | ||
| }) | ||
| }) | ||
|
|
||
| describe('touchBrowserGuestWorktreeRecency', () => { | ||
| it('moves a re-activated worktree to the front without duplicating it', () => { | ||
| const recency = ['wt-2', 'wt-1'] | ||
|
|
||
| touchBrowserGuestWorktreeRecency(recency, 'wt-1') | ||
| expect(recency).toEqual(['wt-1', 'wt-2']) | ||
|
|
||
| touchBrowserGuestWorktreeRecency(recency, 'wt-3') | ||
| expect(recency).toEqual(['wt-3', 'wt-1', 'wt-2']) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.