Skip to content

Commit 51ebfab

Browse files
fix(assistant): remove stale localStorage cache read in session launcher
1 parent cc89a52 commit 51ebfab

2 files changed

Lines changed: 1 addition & 81 deletions

File tree

frontend/src/hooks/useAssistantSessionLauncher.test.tsx

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { initializeAssistantMode } from '@/api/repos'
66

77
const mocks = vi.hoisted(() => ({
88
listSessions: vi.fn(),
9-
getSession: vi.fn(),
109
createSession: vi.fn(),
1110
sendPromptAsync: vi.fn(),
1211
initializeAssistantMode: vi.fn(),
@@ -19,7 +18,6 @@ vi.mock('@/api/repos', () => ({
1918
vi.mock('@/api/opencode', () => ({
2019
OpenCodeClient: vi.fn(() => ({
2120
listSessions: mocks.listSessions,
22-
getSession: mocks.getSession,
2321
createSession: mocks.createSession,
2422
sendPromptAsync: mocks.sendPromptAsync,
2523
})),
@@ -36,25 +34,6 @@ describe('useAssistantSessionLauncher', () => {
3634
mocks.initializeAssistantMode.mockResolvedValue({ directory: '/assistant' })
3735
})
3836

39-
it('opens the cached assistant session without listing sessions', async () => {
40-
localStorage.setItem('ocm:assistant:last-session:123:/assistant', 'cached')
41-
mocks.getSession.mockResolvedValue({ id: 'cached', directory: '/assistant', time: { updated: 20 } })
42-
const onNavigate = vi.fn()
43-
const { result } = renderHook(() => useAssistantSessionLauncher({
44-
repoId: 123,
45-
opcodeUrl: 'http://localhost:5551',
46-
onNavigate,
47-
}))
48-
49-
await act(async () => {
50-
await result.current.openAssistant()
51-
})
52-
53-
expect(mocks.getSession).toHaveBeenCalledWith('cached')
54-
expect(mocks.listSessions).not.toHaveBeenCalled()
55-
expect(onNavigate).toHaveBeenCalledWith('cached')
56-
})
57-
5837
it('opens the latest root session in the assistant directory', async () => {
5938
mocks.listSessions.mockResolvedValue([
6039
{ id: 'older', directory: '/assistant', time: { updated: 10 } },
@@ -82,29 +61,6 @@ describe('useAssistantSessionLauncher', () => {
8261
expect(mocks.sendPromptAsync).not.toHaveBeenCalled()
8362
})
8463

85-
it('falls back to latest lookup when the cached session is stale', async () => {
86-
localStorage.setItem('ocm:assistant:last-session:123:/assistant', 'stale')
87-
mocks.getSession.mockRejectedValueOnce(new Error('not found'))
88-
mocks.listSessions.mockResolvedValue([
89-
{ id: 'latest', directory: '/assistant', time: { updated: 30 } },
90-
])
91-
const onNavigate = vi.fn()
92-
const { result } = renderHook(() => useAssistantSessionLauncher({
93-
repoId: 123,
94-
opcodeUrl: 'http://localhost:5551',
95-
onNavigate,
96-
}))
97-
98-
await act(async () => {
99-
await result.current.openAssistant()
100-
})
101-
102-
expect(mocks.getSession).toHaveBeenCalledWith('stale')
103-
expect(mocks.listSessions).toHaveBeenCalledWith({ limit: 1, roots: true })
104-
expect(onNavigate).toHaveBeenCalledWith('latest')
105-
expect(localStorage.getItem('ocm:assistant:last-session:123:/assistant')).toBe('latest')
106-
})
107-
10864
it('notifies an existing assistant session when some generated updates were preserved', async () => {
10965
mocks.initializeAssistantMode.mockResolvedValue({
11066
directory: '/assistant',

frontend/src/hooks/useAssistantSessionLauncher.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ function getLastAssistantSessionKey(repoId: number, directory: string): string {
1818
return `${LAST_ASSISTANT_SESSION_KEY_PREFIX}:${repoId}:${directory}`
1919
}
2020

21-
function getCachedAssistantSessionId(repoId: number, directory: string): string | undefined {
22-
try {
23-
return localStorage.getItem(getLastAssistantSessionKey(repoId, directory)) || undefined
24-
} catch {
25-
return undefined
26-
}
27-
}
28-
2921
function setCachedAssistantSessionId(repoId: number, directory: string, sessionId: string): void {
3022
try {
3123
localStorage.setItem(getLastAssistantSessionKey(repoId, directory), sessionId)
@@ -34,14 +26,6 @@ function setCachedAssistantSessionId(repoId: number, directory: string, sessionI
3426
}
3527
}
3628

37-
function removeCachedAssistantSessionId(repoId: number, directory: string): void {
38-
try {
39-
localStorage.removeItem(getLastAssistantSessionKey(repoId, directory))
40-
} catch {
41-
return
42-
}
43-
}
44-
4529
function isAssistantRootSession(session: OpenCodeSession, assistantDirectory: string): boolean {
4630
return !session.parentID && session.directory === assistantDirectory
4731
}
@@ -52,25 +36,6 @@ function findNewestRootAssistantSession(sessions: OpenCodeSession[], assistantDi
5236
.sort((a, b) => b.time.updated - a.time.updated)[0]
5337
}
5438

55-
async function getCachedAssistantSession(
56-
client: OpenCodeClient,
57-
repoId: number,
58-
assistantDirectory: string,
59-
): Promise<OpenCodeSession | undefined> {
60-
const cachedSessionId = getCachedAssistantSessionId(repoId, assistantDirectory)
61-
if (!cachedSessionId) return undefined
62-
63-
try {
64-
const session = await client.getSession(cachedSessionId)
65-
if (isAssistantRootSession(session, assistantDirectory)) return session
66-
removeCachedAssistantSessionId(repoId, assistantDirectory)
67-
} catch {
68-
removeCachedAssistantSessionId(repoId, assistantDirectory)
69-
}
70-
71-
return undefined
72-
}
73-
7439
async function getLatestAssistantSession(
7540
client: OpenCodeClient,
7641
assistantDirectory: string,
@@ -153,8 +118,7 @@ export function useAssistantSessionLauncher({
153118
const client = new OpenCodeClient(opcodeUrl, assistant.directory)
154119
const assistantDirectory = assistant.directory
155120

156-
const newest = await getCachedAssistantSession(client, repoId, assistantDirectory)
157-
?? await getLatestAssistantSession(client, assistantDirectory)
121+
const newest = await getLatestAssistantSession(client, assistantDirectory)
158122

159123
if (newest) {
160124
setCachedAssistantSessionId(repoId, assistantDirectory, newest.id)

0 commit comments

Comments
 (0)