diff --git a/src/shared/worktree-ownership.test.ts b/src/shared/worktree-ownership.test.ts index fff8995396a..2a37e49e1b0 100644 --- a/src/shared/worktree-ownership.test.ts +++ b/src/shared/worktree-ownership.test.ts @@ -10,6 +10,8 @@ import { EXTERNAL_WORKTREE_VISIBILITY_ROLLOUT_AT } from './worktree-ownership' +const LARGE_WORKSPACE_HISTORY_COUNT = 150_000 + function makeRepo(overrides: Partial = {}): Repo { return { id: 'repo-1', @@ -172,6 +174,31 @@ describe('worktree ownership classification', () => { ).toBe('orca-managed') }) + it('builds known layouts from large workspace history lists', () => { + const repo = makeRepo() + const workspaceDirHistory = Array.from( + { length: LARGE_WORKSPACE_HISTORY_COUNT }, + (_, index) => ({ + path: `/history/workspaces-${index}`, + nestWorkspaces: index % 2 === 0 + }) + ) + const settings = makeSettings({ + workspaceDir: '/new/workspaces', + workspaceDirHistory + }) + + const layouts = buildKnownOrcaWorkspaceLayouts(settings, repo) + + expect(layouts).toHaveLength(LARGE_WORKSPACE_HISTORY_COUNT + 1) + expect(layouts[0]).toEqual({ path: '/new/workspaces', nestWorkspaces: true }) + expect(layouts[1]).toEqual({ path: '/history/workspaces-0', nestWorkspaces: true }) + expect(layouts.at(-1)).toEqual({ + path: `/history/workspaces-${LARGE_WORKSPACE_HISTORY_COUNT - 1}`, + nestWorkspaces: false + }) + }) + it('handles Windows drive casing and separators', () => { const repo = makeRepo({ path: 'C:\\repos\\App' }) const settings = makeSettings({ workspaceDir: 'C:\\Orca\\Workspaces' }) diff --git a/src/shared/worktree-ownership.ts b/src/shared/worktree-ownership.ts index c628ed14202..813accb7f5c 100644 --- a/src/shared/worktree-ownership.ts +++ b/src/shared/worktree-ownership.ts @@ -49,11 +49,11 @@ export function buildKnownOrcaWorkspaceLayouts( const layouts: OrcaWorkspaceLayout[] = [] if (!repo?.connectionId && settings.workspaceDir) { layouts.push({ path: settings.workspaceDir, nestWorkspaces: settings.nestWorkspaces }) - layouts.push(...(settings.workspaceDirHistory ?? [])) + appendWorkspaceLayouts(layouts, settings.workspaceDirHistory ?? []) } const wslLayouts = repo ? buildWslWorkspaceLayouts(repo.path, settings) : [] - layouts.push(...wslLayouts) + appendWorkspaceLayouts(layouts, wslLayouts) const seen = new Set() return layouts.filter((layout) => { @@ -66,6 +66,17 @@ export function buildKnownOrcaWorkspaceLayouts( }) } +function appendWorkspaceLayouts( + target: OrcaWorkspaceLayout[], + source: readonly OrcaWorkspaceLayout[] +): void { + // Why: workspace history is persisted user data and can grow large enough + // for `push(...source)` to exceed the JavaScript call argument limit. + for (const layout of source) { + target.push(layout) + } +} + function buildWslWorkspaceLayouts( repoPath: string, settings: Pick