diff --git a/packages/ui/src/App.tsx b/packages/ui/src/App.tsx index 65dc973..d837ce1 100644 --- a/packages/ui/src/App.tsx +++ b/packages/ui/src/App.tsx @@ -45,6 +45,7 @@ import { clearActiveParentSession, createSession, fetchSessions, + flushSessionPersistence, updateSessionAgent, updateSessionModel, } from "./stores/sessions" @@ -245,6 +246,12 @@ const App: Component = () => { return } + try { + await flushSessionPersistence(instanceId) + } catch (error) { + log.error("Failed to flush session persistence before closing", error) + } + clearActiveParentSession(instanceId) try { diff --git a/packages/ui/src/stores/session-state.ts b/packages/ui/src/stores/session-state.ts index 17657c4..6d243c4 100644 --- a/packages/ui/src/stores/session-state.ts +++ b/packages/ui/src/stores/session-state.ts @@ -171,6 +171,15 @@ function schedulePersist(instanceId: string) { persistTimers.set(instanceId, timer) } +async function flushSessionPersistence(instanceId: string) { + const existing = persistTimers.get(instanceId) + if (existing) { + clearTimeout(existing) + persistTimers.delete(instanceId) + } + await persistSessionTasks(instanceId) +} + async function persistSessionTasks(instanceId: string) { try { const instanceSessions = sessions().get(instanceId) @@ -439,6 +448,7 @@ export { pruneDraftPrompts, withSession, persistSessionTasks, + flushSessionPersistence, setSessionCompactionState, setSessionPendingPermission, setActiveSession,