diff --git a/src/renderer/src/components/right-sidebar/AiVaultPanel.tsx b/src/renderer/src/components/right-sidebar/AiVaultPanel.tsx index 810ecdc89..aa3d3d6fb 100644 --- a/src/renderer/src/components/right-sidebar/AiVaultPanel.tsx +++ b/src/renderer/src/components/right-sidebar/AiVaultPanel.tsx @@ -18,8 +18,9 @@ import { groupAiVaultSessions } from './ai-vault-session-filters' import { - normalizeAiVaultScopeForContext, - shouldRestoreAiVaultProjectScope + DEFAULT_AI_VAULT_SCOPE, + getRestorableAiVaultScope, + normalizeAiVaultScopeForContext } from './ai-vault-scope-state' import { buildAiVaultProjectContext } from './ai-vault-session-projects' import { @@ -47,7 +48,7 @@ export default function AiVaultPanel(): React.JSX.Element { const projectHostSetupProjection = useProjectHostSetupProjection() const agentCmdOverrides = useAppStore((s) => s.settings?.agentCmdOverrides ?? {}) const [query, setQuery] = useState('') - const [scope, setScope] = useState('project') + const [scope, setScope] = useState(DEFAULT_AI_VAULT_SCOPE) const [sort, setSort] = useState('updated') const [group, setGroup] = useState('project') const [hideEmptySessions, setHideEmptySessions] = useState(true) @@ -61,6 +62,7 @@ export default function AiVaultPanel(): React.JSX.Element { const refreshInFlightRef = useRef(false) const mountedRef = useRef(true) const userChangedScopeRef = useRef(false) + const preferredScopeRef = useRef(DEFAULT_AI_VAULT_SCOPE) const isRemoteWorktree = Boolean(activeWorktreeRepo?.connectionId) const activeWorktreePath = activeWorktree?.path ?? null @@ -91,8 +93,7 @@ export default function AiVaultPanel(): React.JSX.Element { (group === 'project' ? 0 : 1) + (hideEmptySessions ? 0 : 1) - // Project scope depends on active project context, but should come back after - // transient context loss unless the user intentionally chose another scope. + // Workspace is the preferred default, but unavailable context still falls back to All. useEffect(() => { const normalizedScope = normalizeAiVaultScopeForContext({ scope, @@ -105,16 +106,17 @@ export default function AiVaultPanel(): React.JSX.Element { }, [activeProjectKey, activeWorktreePath, scope]) useEffect(() => { - if ( - shouldRestoreAiVaultProjectScope({ - scope, - activeProjectKey, - userChangedScope: userChangedScopeRef.current - }) - ) { - setScope('project') + const restorableScope = getRestorableAiVaultScope({ + scope, + activeProjectKey, + activeWorktreePath, + preferredScope: preferredScopeRef.current, + userChangedScope: userChangedScopeRef.current + }) + if (restorableScope) { + setScope(restorableScope) } - }, [activeProjectKey, scope]) + }, [activeProjectKey, activeWorktreePath, scope]) const refresh = useCallback(async (args: { force?: boolean } = {}): Promise => { if (refreshInFlightRef.current) { @@ -284,7 +286,8 @@ export default function AiVaultPanel(): React.JSX.Element { }, []) const handleScopeChange = useCallback((nextScope: AiVaultScope) => { - userChangedScopeRef.current = nextScope !== 'project' + preferredScopeRef.current = nextScope + userChangedScopeRef.current = nextScope !== DEFAULT_AI_VAULT_SCOPE setScope(nextScope) }, []) diff --git a/src/renderer/src/components/right-sidebar/AiVaultPanelControls.tsx b/src/renderer/src/components/right-sidebar/AiVaultPanelControls.tsx index f9feda064..0b42428f6 100644 --- a/src/renderer/src/components/right-sidebar/AiVaultPanelControls.tsx +++ b/src/renderer/src/components/right-sidebar/AiVaultPanelControls.tsx @@ -36,8 +36,11 @@ import { translate } from '@/i18n/i18n' const VAULT_HEADER_CONTROL_CLASS = 'size-6 shrink-0' -const VAULT_SCOPE_TOGGLE_ITEM_CLASS = - 'h-7 min-h-7 min-w-0 flex-1 basis-0 shrink border border-transparent bg-transparent px-2.5 text-[11px] font-medium leading-none text-foreground shadow-none hover:bg-sidebar-accent hover:text-sidebar-accent-foreground aria-[checked=true]:border-foreground/20 aria-[checked=true]:bg-foreground/10 aria-[checked=true]:text-foreground aria-[checked=true]:shadow-xs aria-[checked=true]:hover:bg-foreground/15 aria-[checked=true]:hover:text-foreground data-[state=on]:border-foreground/20 data-[state=on]:bg-foreground/10 data-[state=on]:text-foreground data-[state=on]:shadow-xs data-[state=on]:hover:bg-foreground/15 data-[state=on]:hover:text-foreground @max-[300px]/ai-vault:px-1.5' +// Why: match ToggleGroup's spacing+outline qualifiers so selected edges out-specify its border-l-0 collapse. +const VAULT_SCOPE_SELECTED_EDGE_CLASS = + 'data-[spacing=0]:data-[variant=outline]:aria-[checked=true]:border-l data-[spacing=0]:data-[variant=outline]:data-[state=on]:border-l' + +const VAULT_SCOPE_TOGGLE_ITEM_CLASS = `h-7 min-h-7 min-w-0 flex-1 basis-0 shrink border border-transparent bg-transparent px-2.5 text-[11px] font-medium leading-none text-foreground shadow-none hover:bg-sidebar-accent hover:text-sidebar-accent-foreground aria-[checked=true]:border-foreground/20 aria-[checked=true]:bg-foreground/10 aria-[checked=true]:text-foreground aria-[checked=true]:shadow-xs aria-[checked=true]:hover:bg-foreground/15 aria-[checked=true]:hover:text-foreground data-[state=on]:border-foreground/20 data-[state=on]:bg-foreground/10 data-[state=on]:text-foreground data-[state=on]:shadow-xs data-[state=on]:hover:bg-foreground/15 data-[state=on]:hover:text-foreground ${VAULT_SCOPE_SELECTED_EDGE_CLASS} @max-[300px]/ai-vault:px-1.5` export function VaultGroupHeader({ group, diff --git a/src/renderer/src/components/right-sidebar/ai-vault-scope-state.test.ts b/src/renderer/src/components/right-sidebar/ai-vault-scope-state.test.ts index 72be0b95b..dfeedfcfb 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault-scope-state.test.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault-scope-state.test.ts @@ -1,9 +1,17 @@ import { describe, expect, it } from 'vitest' import { + DEFAULT_AI_VAULT_SCOPE, + getRestorableAiVaultScope, normalizeAiVaultScopeForContext, - shouldRestoreAiVaultProjectScope + shouldRestoreDefaultAiVaultScope } from './ai-vault-scope-state' +describe('DEFAULT_AI_VAULT_SCOPE', () => { + it('defaults the session history scope to workspace', () => { + expect(DEFAULT_AI_VAULT_SCOPE).toBe('workspace') + }) +}) + describe('normalizeAiVaultScopeForContext', () => { it('falls back from project to all when no active project is available', () => { expect( @@ -44,24 +52,97 @@ describe('normalizeAiVaultScopeForContext', () => { }) }) -describe('shouldRestoreAiVaultProjectScope', () => { - it('restores project after automatic fallback when a project becomes available', () => { +describe('shouldRestoreDefaultAiVaultScope', () => { + it('restores workspace after automatic fallback when a workspace becomes available', () => { expect( - shouldRestoreAiVaultProjectScope({ + shouldRestoreDefaultAiVaultScope({ scope: 'all', activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', userChangedScope: false }) ).toBe(true) }) - it('does not restore project after the user manually changed scope', () => { + it('does not restore workspace while the workspace is unavailable', () => { expect( - shouldRestoreAiVaultProjectScope({ + shouldRestoreDefaultAiVaultScope({ scope: 'all', activeProjectKey: 'project:orca', + activeWorktreePath: null, + userChangedScope: false + }) + ).toBe(false) + }) + + it('does not restore workspace after the user manually chose project or all', () => { + expect( + shouldRestoreDefaultAiVaultScope({ + scope: 'all', + activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', userChangedScope: true }) ).toBe(false) }) + + it('can restore a project default only when a project becomes available', () => { + expect( + shouldRestoreDefaultAiVaultScope({ + scope: 'all', + activeProjectKey: null, + activeWorktreePath: '/repo', + userChangedScope: false, + defaultScope: 'project' + }) + ).toBe(false) + + expect( + shouldRestoreDefaultAiVaultScope({ + scope: 'all', + activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', + userChangedScope: false, + defaultScope: 'project' + }) + ).toBe(true) + }) +}) + +describe('getRestorableAiVaultScope', () => { + it('restores the workspace preference after automatic fallback', () => { + expect( + getRestorableAiVaultScope({ + scope: 'all', + activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', + preferredScope: 'workspace', + userChangedScope: false + }) + ).toBe('workspace') + }) + + it('restores a manual project preference when a project becomes available again', () => { + expect( + getRestorableAiVaultScope({ + scope: 'all', + activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', + preferredScope: 'project', + userChangedScope: true + }) + ).toBe('project') + }) + + it('keeps a manual all preference sticky', () => { + expect( + getRestorableAiVaultScope({ + scope: 'all', + activeProjectKey: 'project:orca', + activeWorktreePath: '/repo', + preferredScope: 'all', + userChangedScope: true + }) + ).toBeNull() + }) }) diff --git a/src/renderer/src/components/right-sidebar/ai-vault-scope-state.ts b/src/renderer/src/components/right-sidebar/ai-vault-scope-state.ts index 5f564cdcd..5890fcf97 100644 --- a/src/renderer/src/components/right-sidebar/ai-vault-scope-state.ts +++ b/src/renderer/src/components/right-sidebar/ai-vault-scope-state.ts @@ -1,5 +1,7 @@ import type { AiVaultScope } from '../../../../shared/ai-vault-types' +export const DEFAULT_AI_VAULT_SCOPE: AiVaultScope = 'workspace' + export function normalizeAiVaultScopeForContext(args: { scope: AiVaultScope activeProjectKey: string | null @@ -14,10 +16,65 @@ export function normalizeAiVaultScopeForContext(args: { return args.scope } -export function shouldRestoreAiVaultProjectScope(args: { +function isAiVaultScopeAvailable(args: { scope: AiVaultScope activeProjectKey: string | null - userChangedScope: boolean + activeWorktreePath: string | null }): boolean { - return Boolean(args.activeProjectKey && args.scope === 'all' && !args.userChangedScope) + return normalizeAiVaultScopeForContext(args) === args.scope +} + +export function shouldRestoreDefaultAiVaultScope(args: { + scope: AiVaultScope + activeProjectKey: string | null + activeWorktreePath: string | null + userChangedScope: boolean + defaultScope?: AiVaultScope +}): boolean { + const defaultScope = args.defaultScope ?? DEFAULT_AI_VAULT_SCOPE + + return ( + args.scope === 'all' && + !args.userChangedScope && + isAiVaultScopeAvailable({ + scope: defaultScope, + activeProjectKey: args.activeProjectKey, + activeWorktreePath: args.activeWorktreePath + }) + ) +} + +export function getRestorableAiVaultScope(args: { + scope: AiVaultScope + activeProjectKey: string | null + activeWorktreePath: string | null + preferredScope: AiVaultScope + userChangedScope: boolean + defaultScope?: AiVaultScope +}): AiVaultScope | null { + const defaultScope = args.defaultScope ?? DEFAULT_AI_VAULT_SCOPE + + if (args.preferredScope === defaultScope) { + return shouldRestoreDefaultAiVaultScope({ + scope: args.scope, + activeProjectKey: args.activeProjectKey, + activeWorktreePath: args.activeWorktreePath, + userChangedScope: args.userChangedScope, + defaultScope + }) + ? defaultScope + : null + } + + if (args.scope !== 'all' || args.preferredScope === 'all') { + return null + } + + return isAiVaultScopeAvailable({ + scope: args.preferredScope, + activeProjectKey: args.activeProjectKey, + activeWorktreePath: args.activeWorktreePath + }) + ? args.preferredScope + : null }