Default session history to the current workspace (#6211)

This commit is contained in:
Brennan Benson
2026-06-23 16:22:26 -07:00
committed by GitHub
parent b053e1ac60
commit 4f6616ae6c
4 changed files with 170 additions and 26 deletions
@@ -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<AiVaultScope>('project')
const [scope, setScope] = useState<AiVaultScope>(DEFAULT_AI_VAULT_SCOPE)
const [sort, setSort] = useState<AiVaultSort>('updated')
const [group, setGroup] = useState<AiVaultGroup>('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<AiVaultScope>(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<void> => {
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)
}, [])
@@ -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,
@@ -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()
})
})
@@ -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
}