fix(session-history): serve scope-only from a cached full list and NFC-fold bucket names

All and Project send the same paths, so a cached All list answers Project without a rescan. Bucket names and scope prefixes are compared NFC-folded, since CodeBuddy and Pi keep the cwd's own Unicode form.
This commit is contained in:
Kelvin Amoaba
2026-11-08 21:17:48 +00:00
parent fd13c174f7
commit 66018882d7
4 changed files with 67 additions and 4 deletions
@@ -0,0 +1,37 @@
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
const { scanAiVaultSessionsInService } = vi.hoisted(() => ({
scanAiVaultSessionsInService: vi.fn()
}))
vi.mock('./scan-service/session-scanner-service-spawn', async (importOriginal) => ({
...(await importOriginal<object>()),
scanAiVaultSessionsInService
}))
import { listAiVaultSessions, resetAiVaultSessionListCacheForTests } from './cached-session-list'
beforeEach(() => {
vi.spyOn(process, 'platform', 'get').mockReturnValue('darwin')
resetAiVaultSessionListCacheForTests()
scanAiVaultSessionsInService.mockResolvedValue({ sessions: [], issues: [], scannedAt: 'scan' })
})
afterEach(() => {
resetAiVaultSessionListCacheForTests()
vi.restoreAllMocks()
scanAiVaultSessionsInService.mockReset()
})
it('answers a scope-only request from a cached full list for the same paths, never the reverse', async () => {
const scopePaths = ['/repo/app']
await listAiVaultSessions({ scopePaths })
await listAiVaultSessions({ scopePaths, scopeOnly: true })
expect(scanAiVaultSessionsInService).toHaveBeenCalledTimes(1)
resetAiVaultSessionListCacheForTests()
await listAiVaultSessions({ scopePaths, scopeOnly: true })
await listAiVaultSessions({ scopePaths })
expect(scanAiVaultSessionsInService).toHaveBeenCalledTimes(3)
})
+3 -1
View File
@@ -145,9 +145,11 @@ export async function listAiVaultSessions(
if (args?.force === true) {
clearAiVaultBackgroundRestartCircuit()
}
// A full list for the same paths holds every row a scope-only one would, so All -> Project needs no rescan.
const fullKey = args?.scopeOnly ? aiVaultLocalScanScopeKey({ ...args, scopeOnly: false }) : key
if (
args?.force !== true &&
cachedList?.key === key &&
(cachedList?.key === key || cachedList?.key === fullKey) &&
cachedList.expiresAt > now &&
aiVaultCachedDepthServes(cachedList.depth, depth, args?.scopePaths)
) {
@@ -77,7 +77,7 @@ export function cwdBucketScopePrefixes(
for (const scopePath of scopePaths) {
for (const candidate of scopePathCandidates(scopePath)) {
for (const prefix of layout.encodeScopePrefixes(candidate)) {
prefixes.add(prefix.toLowerCase())
prefixes.add(prefix.normalize('NFC').toLowerCase())
}
}
}
@@ -111,11 +111,12 @@ export async function listProjectDirs(
recordRefusal(issues, rootDir, err, layout.agent)
return []
}
// Case-folded: Windows cwds differ in case, and each bucket's cwd is still checked afterwards.
// Case- and NFC-folded: Windows cwds differ in case, macOS names can be NFD, and each bucket's cwd is still checked.
return entries
.filter(
(entry) =>
entry.isDirectory() && layout.isDirInScope(entry.name.toLowerCase(), scopeProjectPrefixes)
entry.isDirectory() &&
layout.isDirInScope(entry.name.normalize('NFC').toLowerCase(), scopeProjectPrefixes)
)
.map((entry) => join(rootDir, entry.name))
}
@@ -149,6 +149,29 @@ describe('scanAiVaultSessions scopeOnly', () => {
await expect(workspaceSessionIds(root, scopePaths, { scopeOnly: true })).resolves.toEqual(main)
})
it('matches a CodeBuddy bucket whose name uses the other Unicode form', async () => {
const root = await tempRoot()
const cwd = '/repo/cafe\u0301'
const dir = join(isolatedScanRoots(root).codebuddyProjectsDir, 'repo-cafe\u0301')
await mkdir(dir, { recursive: true })
await writeFile(
join(dir, 'nfd.jsonl'),
JSON.stringify({
type: 'message',
timestamp: '2026-06-01T00:00:00.000Z',
sessionId: 'nfd',
cwd,
role: 'user',
content: [{ type: 'input_text', text: 'hi' }]
})
)
const scopePaths = [cwd.normalize('NFC')]
const main = await workspaceSessionIds(root, scopePaths, {})
expect(main).toEqual(['nfd'])
await expect(workspaceSessionIds(root, scopePaths, { scopeOnly: true })).resolves.toEqual(main)
})
it('keeps every in-scope session main shows when the scope holds more than the limit', async () => {
const root = await tempRoot()
const claudeRoot = isolatedScanRoots(root).claudeProjectsDir