Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe('SourceControl virtualized changed-files list', () => {
expect(row('src/file-000.ts')?.className).toContain('bg-accent/60')
})

it('collapses and re-expands a virtualized section', () => {
it('retains a collapsed virtualized section across panel remount', () => {
resetState({
gitStatusByWorktree: { [mocks.activeWorktree.id]: manyEntries(500) }
})
Expand All @@ -346,8 +346,17 @@ describe('SourceControl virtualized changed-files list', () => {
expect(mountedRows().length).toBe(0)
expect(virtualList()).toBeNull()

act(() => root.unmount())
root = createRoot(container)
renderSourceControl()
expect(mountedRows().length).toBe(0)
expect(virtualList()).toBeNull()

const remountedHeader = Array.from(container.querySelectorAll('button')).find((button) =>
button.textContent?.includes('Changes')
)
act(() => {
changesHeader?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
remountedHeader?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
})
expect(mountedRows().length).toBeGreaterThan(0)
expect(mountedRows().length).toBeLessThanOrEqual(MAX_MOUNTED_ROWS)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @vitest-environment happy-dom

import { act } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useSourceControlPanelViewState } from './use-panel-view-state'

let container: HTMLDivElement
let root: Root
let toggle: (section: string) => void = () => {}
let collapsed = new Set<string>()

function Probe({ worktreeId }: { worktreeId: string | null }) {
const viewState = useSourceControlPanelViewState({
activeWorktreeId: worktreeId,
settings: null,
updateSettings: vi.fn()
})
toggle = viewState.toggleSection
collapsed = viewState.collapsedSections
return null
}

function render(worktreeId: string | null): void {
act(() => root.render(<Probe worktreeId={worktreeId} />))
}

function remount(worktreeId: string | null): void {
act(() => root.unmount())
root = createRoot(container)
render(worktreeId)
}

beforeEach(() => {
globalThis.IS_REACT_ACT_ENVIRONMENT = true
container = document.createElement('div')
root = createRoot(container)
})

afterEach(() => {
act(() => root.unmount())
})

describe('useSourceControlPanelViewState section disclosure', () => {
it('keeps a worktree choice across remount without leaking it to another worktree', () => {
render('wt-a')
act(() => toggle('unstaged'))
act(() => toggle('history'))
expect([...collapsed].sort()).toEqual(['unstaged'])

remount('wt-a')
expect([...collapsed].sort()).toEqual(['unstaged'])

render('wt-b')
expect([...collapsed].sort()).toEqual(['history'])

remount('wt-b')
expect([...collapsed].sort()).toEqual(['history'])

render('wt-a')
expect([...collapsed].sort()).toEqual(['unstaged'])
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useMemo, useRef, useState } from 'react'
import { BoundedMap } from '../../../../../../shared/bounded-map'
import { resolveSourceControlGroupOrder } from '../listing/section-order'
import { getNextSourceControlViewMode } from './header-toolbar'
import { normalizeSourceControlViewMode } from '../commit/commit-drafts'
Expand All @@ -11,6 +12,16 @@ function createDefaultCollapsedSections(): Set<string> {
return new Set(DEFAULT_COLLAPSED_SECTIONS)
}

// Why: the panel unmounts on sidebar tab switch; retain per-worktree disclosure for this renderer session.
const collapsedSectionsByWorktree = new BoundedMap<string, Set<string>>({ maxEntries: 512 })

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.

🎯 Functional Correctness | Minor | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '1,180p' src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts
sed -n '1,220p' src/renderer/src/components/right-sidebar/source-control/panel/use-panel-state.ts
rg -n 'activeWorktreeInstanceId|useSourceControlPanelViewState|worktreeId' src/renderer/src/components/right-sidebar/source-control/panel

Repository: stablyai/orca

Length of output: 11878


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- identity references ---'
rg -n -F --glob '*.{ts,tsx}' -e 'activeWorktreeInstanceId' -e 'executionHost' -e 'ownerId' -e 'owner' -e 'worktreeInstanceId' -e 'instanceId' src/renderer/src/components/right-sidebar/source-control src/renderer/src | head -n 240
printf '%s\n' '--- context declaration and related types ---'
rg -n -F --glob '*.{ts,tsx}' -e 'useSourceControlWorktreeContext' -e 'SourceControlWorktreeContext' -e 'activeWorktreeInstanceId' src/renderer/src/components/right-sidebar/source-control src/renderer/src/store src/shared | head -n 240
printf '%s\n' '--- replacement and locator references ---'
rg -n -F --glob '*.{ts,tsx}' -e 'worktree replacement' -e 'replaceWorktree' -e 'recreated' -e 'locator' -e 'worktreeInstance' -e 'executionHostId' src/renderer/src src/shared | head -n 300
printf '%s\n' '--- relevant diff ---'
git diff --find-renames 92ccacd7373147a07cd26ab81ac296b5b102912a 39dd7a464a6876c3d119fcf5f0ada414be7f639e -- src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.ts src/renderer/src/components/right-sidebar/source-control/panel/use-panel-state.ts

Repository: stablyai/orca

Length of output: 41439


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- source-control worktree context ---'
sed -n '1,280p' src/renderer/src/components/right-sidebar/source-control/listing/use-worktree-context.ts
printf '%s\n' '--- context imports and worktree types ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'instanceId?:' -e 'instanceId:' -e 'interface Worktree' -e 'type Worktree' -e 'executionHostId' src/shared src/renderer/src/store src/renderer/src/lib src/renderer/src/components/right-sidebar/source-control/listing | head -n 320
printf '%s\n' '--- exact instance-id definitions and consumers ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'activeWorktree?.instanceId' -e 'instanceId' src/renderer/src/components/right-sidebar/source-control/listing src/renderer/src/store/slices src/shared | head -n 320
printf '%s\n' '--- locator and identity helpers ---'
rg -n -i --glob '*.ts' --glob '*.tsx' 'host.?qualified|qualified.?locator|locator.*fallback|fallback.*locator|worktree.*locator|locator.*worktree|worktree.*identity|identity.*worktree' src | head -n 320
printf '%s\n' '--- tests for the changed hook and replacement behavior ---'
sed -n '1,260p' src/renderer/src/components/right-sidebar/source-control/panel/use-panel-view-state.test.tsx
rg -n -i --glob '*.{ts,tsx}' 'recreat|replace.*worktree|worktree.*replace|same.*worktree.*host|same.*id.*host|instanceId' src/renderer/src/components/right-sidebar/source-control src/renderer/src/store | head -n 320

Repository: stablyai/orca

Length of output: 45661


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- worktree declarations ---'
rg -n -U --glob '*.ts' --glob '*.tsx' 'export (interface|type) Worktree|interface Worktree|type Worktree\\s*=' src | head -n 80
printf '%s\n' '--- instance ID lifecycle ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'instanceId: crypto' -e 'instanceId: random' -e 'instanceId: uuid' -e 'instanceId: generate' -e 'instanceId ??' -e 'instanceId ||' -e 'worktree.instanceId' -e 'instanceId = ' src/shared src/renderer/src/store src/main | head -n 240
printf '%s\n' '--- same-ID host tests ---'
sed -n '1,150p' src/renderer/src/store/slices/worktree-catalog-reconciliation.test.ts
sed -n '210,270p' src/renderer/src/store/slices/purge-stale-runtime-host-state.test.ts
sed -n '200,250p' src/renderer/src/store/slices/worktree-listing-branch-switch.test.ts
printf '%s\n' '--- worktree ID construction and recreation comments ---'
rg -n -i --glob '*.ts' --glob '*.tsx' 'recreated at the same path|worktree ids are recycled|id.*repo.*path|repo.*path.*id|new.*instance' src/shared src/renderer/src/store src/main | head -n 240
printf '%s\n' '--- disclosure/fallback terms in full repository ---'
rg -n -i --glob '*.ts' --glob '*.tsx' 'collapsedSectionsByWorktree|host-qualified locator|locator fallback|occupant metadata|checkout occupant|section disclosure|disclosure state' . | head -n 240

Repository: stablyai/orca

Length of output: 41753


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- checkout identity contract ---'
sed -n '1,220p' src/shared/worktree/identity.ts
printf '%s\n' '--- persisted locator aliases ---'
sed -n '55,125p' src/shared/persisted-state-types.ts
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'Host-qualified locator aliases' -e 'worktreeLocatorAliases' -e 'locatorAliases' -e 'canonical identity keys' src | head -n 160
printf '%s\n' '--- alias declarations and consumers ---'
rg -n -i --glob '*.ts' --glob '*.tsx' 'locator.*alias|alias.*locator|canonical.*identity|identity.*key' src/shared src/renderer/src/store src/renderer/src/lib | head -n 240
printf '%s\n' '--- worktree ID and host construction ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'repo.id}::' -e 'repoId}::' -e 'hostId:' -e 'id: worktreeId' -e 'worktreeId = ' src/main src/shared src/renderer/src/store | head -n 260
printf '%s\n' '--- exact source-control owner key patterns ---'
sed -n '35,75p' src/renderer/src/components/right-sidebar/source-control/sync/use-git-history.ts
sed -n '145,180p' src/renderer/src/components/right-sidebar/source-control/sync/use-git-history.ts
sed -n '1,140p' src/renderer/src/components/right-sidebar/source-control/worktree-git-owner-target.ts

Repository: stablyai/orca

Length of output: 41649


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- canonical identity consumers ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'canonicalWorktreeIdentity' -e 'createWorktreeIdentity' -e 'WorktreeIdentityRef' -e 'worktreeIdentityAliases' src
printf '%s\n' '--- identity helper modules ---'
find src/shared/worktree -maxdepth 2 -type f -print | sort
for f in src/shared/worktree/id.ts src/shared/sidebar-list/workspace-rows.ts src/shared/sidebar-list/paired-server-rows.ts; do
  if test -f "$f"; then
    printf '%s\n' "--- $f ---"
    sed -n '1,240p' "$f"
  fi
done
printf '%s\n' '--- active worktree host and type evidence ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'activeWorktree.hostId' -e 'activeWorktree?.hostId' -e 'hostId: activeWorktree' -e 'Pick<Worktree' src/renderer/src src/shared | head -n 220
printf '%s\n' '--- worktree model imports ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'from.*types' -e 'from.*models' src/shared | grep -E 'Worktree|worktree' | head -n 120

Repository: stablyai/orca

Length of output: 41930


🏁 Script executed:

set -o pipefail
printf '%s\n' '--- host-qualified identity contract ---'
sed -n '1,180p' src/shared/worktree/host-qualified-identity.ts
printf '%s\n' '--- active worktree host and execution-host alias ---'
sed -n '1,140p' src/renderer/src/lib/worktree-execution-host-alias.ts
sed -n '285,315p' src/shared/execution-host.ts
printf '%s\n' '--- canonical alias projection ---'
sed -n '1,125p' src/main/persistence/loading-store/worktree-meta-alias-projection.ts
sed -n '1,125p' src/main/persistence/loading-store/worktree-identity-metadata.ts

Repository: stablyai/orca

Length of output: 17183


Key disclosure state by checkout occupant and execution host.

activeWorktreeId is only a repoId::path locator. It is not host-qualified. The hook must key state by the canonical occupant identity: the active worktree’s execution host and activeWorktreeInstanceId. Use composeWorktreeHostIdentity(executionHostId, activeWorktreeId) only when instance metadata is unavailable. Use the same key for the cache lookup, write, and switch comparison.

A recreated checkout receives a new durable instance ID, but the current hook ignores it. A same-ID checkout on another host also shares the current key. The host-qualified fallback matches the repository contract for legacy rows, but it cannot distinguish two occupants that both lack instance metadata on the same host.


function readCollapsedSections(worktreeId: string | null): Set<string> {
return (
(worktreeId ? collapsedSectionsByWorktree.get(worktreeId) : undefined) ??
createDefaultCollapsedSections()
)
}

/**
* Local presentation state for the Source Control panel: filter, collapsed sections/directories,
* list-vs-tree mode and the base-ref dialog, plus the per-worktree reset that keeps a worktree
Expand All @@ -30,8 +41,8 @@ export function useSourceControlPanelViewState({
const [fileListScrollElement, setFileListScrollElement] = useState<HTMLDivElement | null>(null)
const isMac = useMemo(() => navigator.userAgent.includes('Mac'), [])
const [filterExpanded, setFilterExpanded] = useState(false)
const [collapsedSections, setCollapsedSections] = useState<Set<string>>(
createDefaultCollapsedSections
const [collapsedSections, setCollapsedSections] = useState<Set<string>>(() =>
readCollapsedSections(activeWorktreeId)
)
const persistedSourceControlViewMode = normalizeSourceControlViewMode(
settings?.sourceControlViewMode
Expand All @@ -57,25 +68,29 @@ export function useSourceControlPanelViewState({
if (viewStateWorktreeId !== activeWorktreeId) {
setViewStateWorktreeId(activeWorktreeId)
setFilterExpanded(false)
setCollapsedSections(createDefaultCollapsedSections())
setCollapsedSections(readCollapsedSections(activeWorktreeId))
setCollapsedTreeDirs(new Set())
setBaseRefDialogOpen(false)
// Why: don't reset defaultBaseRef here — it's repo-scoped (resolved on activeRepo change); resetting would clobber non-main defaults.
setFilterQuery('')
// Why: don't reset commit-in-flight state — it's per-worktree; resetting would re-enable Commit for an incoming worktree mid-commit.
}

const toggleSection = useCallback((section: string) => {
setCollapsedSections((prev) => {
const next = new Set(prev)
const toggleSection = useCallback(
(section: string) => {
const next = new Set(collapsedSections)
if (next.has(section)) {
next.delete(section)
} else {
next.add(section)
}
return next
})
}, [])
setCollapsedSections(next)
if (activeWorktreeId) {
collapsedSectionsByWorktree.set(activeWorktreeId, next)
}
},
[activeWorktreeId, collapsedSections]
)

const toggleTreeDir = useCallback((key: string) => {
setCollapsedTreeDirs((prev) => {
Expand Down
Loading