fix(assistant): remove stale localStorage cache read in session launcher

This commit is contained in:
Chris Scott committed 2026-06-05 18:27:36 -04:00
1 parent cc89a524c4
commit 51ebfabb3d
2 files changed
+1 -81

No files matched your search

@@ -6,7 +6,6 @@ import { initializeAssistantMode } from '@/api/repos'
const mocks = vi.hoisted(() => ({
listSessions: vi.fn(),
getSession: vi.fn(),
createSession: vi.fn(),
sendPromptAsync: vi.fn(),
initializeAssistantMode: vi.fn(),
@@ -19,7 +18,6 @@ vi.mock('@/api/repos', () => ({
vi.mock('@/api/opencode', () => ({
OpenCodeClient: vi.fn(() => ({
listSessions: mocks.listSessions,
getSession: mocks.getSession,
createSession: mocks.createSession,
sendPromptAsync: mocks.sendPromptAsync,
})),
@@ -36,25 +34,6 @@ describe('useAssistantSessionLauncher', () => {
mocks.initializeAssistantMode.mockResolvedValue({ directory: '/assistant' })
})
it('opens the cached assistant session without listing sessions', async () => {
localStorage.setItem('ocm:assistant:last-session:123:/assistant', 'cached')
mocks.getSession.mockResolvedValue({ id: 'cached', directory: '/assistant', time: { updated: 20 } })
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
repoId: 123,
opcodeUrl: 'http://localhost:5551',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(mocks.getSession).toHaveBeenCalledWith('cached')
expect(mocks.listSessions).not.toHaveBeenCalled()
expect(onNavigate).toHaveBeenCalledWith('cached')
})
it('opens the latest root session in the assistant directory', async () => {
mocks.listSessions.mockResolvedValue([
{ id: 'older', directory: '/assistant', time: { updated: 10 } },
@@ -82,29 +61,6 @@ describe('useAssistantSessionLauncher', () => {
expect(mocks.sendPromptAsync).not.toHaveBeenCalled()
})
it('falls back to latest lookup when the cached session is stale', async () => {
localStorage.setItem('ocm:assistant:last-session:123:/assistant', 'stale')
mocks.getSession.mockRejectedValueOnce(new Error('not found'))
mocks.listSessions.mockResolvedValue([
{ id: 'latest', directory: '/assistant', time: { updated: 30 } },
])
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
repoId: 123,
opcodeUrl: 'http://localhost:5551',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(mocks.getSession).toHaveBeenCalledWith('stale')
expect(mocks.listSessions).toHaveBeenCalledWith({ limit: 1, roots: true })
expect(onNavigate).toHaveBeenCalledWith('latest')
expect(localStorage.getItem('ocm:assistant:last-session:123:/assistant')).toBe('latest')
})
it('notifies an existing assistant session when some generated updates were preserved', async () => {
mocks.initializeAssistantMode.mockResolvedValue({
directory: '/assistant',
@@ -18,14 +18,6 @@ function getLastAssistantSessionKey(repoId: number, directory: string): string {
return `${LAST_ASSISTANT_SESSION_KEY_PREFIX}:${repoId}:${directory}`
}
function getCachedAssistantSessionId(repoId: number, directory: string): string | undefined {
try {
return localStorage.getItem(getLastAssistantSessionKey(repoId, directory)) || undefined
} catch {
return undefined
}
}
function setCachedAssistantSessionId(repoId: number, directory: string, sessionId: string): void {
try {
localStorage.setItem(getLastAssistantSessionKey(repoId, directory), sessionId)
@@ -34,14 +26,6 @@ function setCachedAssistantSessionId(repoId: number, directory: string, sessionI
}
}
function removeCachedAssistantSessionId(repoId: number, directory: string): void {
try {
localStorage.removeItem(getLastAssistantSessionKey(repoId, directory))
} catch {
return
}
}
function isAssistantRootSession(session: OpenCodeSession, assistantDirectory: string): boolean {
return !session.parentID && session.directory === assistantDirectory
}
@@ -52,25 +36,6 @@ function findNewestRootAssistantSession(sessions: OpenCodeSession[], assistantDi
.sort((a, b) => b.time.updated - a.time.updated)[0]
}
async function getCachedAssistantSession(
client: OpenCodeClient,
repoId: number,
assistantDirectory: string,
): Promise<OpenCodeSession | undefined> {
const cachedSessionId = getCachedAssistantSessionId(repoId, assistantDirectory)
if (!cachedSessionId) return undefined
try {
const session = await client.getSession(cachedSessionId)
if (isAssistantRootSession(session, assistantDirectory)) return session
removeCachedAssistantSessionId(repoId, assistantDirectory)
} catch {
removeCachedAssistantSessionId(repoId, assistantDirectory)
}
return undefined
}
async function getLatestAssistantSession(
client: OpenCodeClient,
assistantDirectory: string,
@@ -153,8 +118,7 @@ export function useAssistantSessionLauncher({
const client = new OpenCodeClient(opcodeUrl, assistant.directory)
const assistantDirectory = assistant.directory
const newest = await getCachedAssistantSession(client, repoId, assistantDirectory)
?? await getLatestAssistantSession(client, assistantDirectory)
const newest = await getLatestAssistantSession(client, assistantDirectory)
if (newest) {
setCachedAssistantSessionId(repoId, assistantDirectory, newest.id)