The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(memory): Abbruchgrund kommt aus flushMemoryPersist heraus (R2-38)
purplecbf794f97517
diff --git a/src/lib/__tests__/memory-persistence.test.ts b/src/lib/__tests__/memory-persistence.test.tsindex 498648dc..565bbe1b 100644--- a/src/lib/__tests__/memory-persistence.test.ts+++ b/src/lib/__tests__/memory-persistence.test.ts@@ -14,7 +14,13 @@ beforeEach(() => {vi.resetModules()fixture.value = nullfixture.write.mockReset().mockImplementation((value: string) => { fixture.value = value })- fixture.confirm.mockReset().mockImplementation((expected: string, _value: string, current: () => boolean) => fixture.value === expected && current())+ // Mirrors idbStorage's real compareAndSetIdbItem, which swallows a throw from+ // `current` inside its own try/catch (transaction abort) rather than letting+ // it escape: a caller that hands over a THROWING isCurrent must not rely on+ // this mock to pass it through for free.+ fixture.confirm.mockReset().mockImplementation((expected: string, _value: string, current: () => boolean) => {+ try { return fixture.value === expected && current() } catch { return false }+ })})it('serializes normal writes and confirms the requested snapshot before a later write', async () => {@@ -49,6 +55,12 @@ it('rejects a revoked confirmation without failing subsequent writes', async ()memoryPersistence.setItem(KEY, 'later')expect(await flushMemoryPersist()).toBe('later')})+it('R2-38: a thrown cancellation reason from isCurrent comes out of flushMemoryPersist, not a generic message', async () => {+ const { memoryPersistence, flushMemoryPersist } = await import('../memory-persistence')+ memoryPersistence.setItem(KEY, 'saved')+ const cancelled = new Error('Memory synchronization cancelled. Some changes may already be saved.')+ await expect(flushMemoryPersist(() => { throw cancelled })).rejects.toBe(cancelled)+})it('rehydration accepts a new authoritative persisted snapshot without stale expectations', async () => {const { memoryPersistence, flushMemoryPersist } = await import('../memory-persistence')memoryPersistence.setItem(KEY, 'old')diff --git a/src/lib/__tests__/memory-sync-coordinator.test.ts b/src/lib/__tests__/memory-sync-coordinator.test.tsindex 29ad8b2b..225d713a 100644--- a/src/lib/__tests__/memory-sync-coordinator.test.ts+++ b/src/lib/__tests__/memory-sync-coordinator.test.ts@@ -9,7 +9,7 @@ vi.mock('../../api/cloud/memory-sync', async importOriginal => ({ ...await imporwithMemorySyncSession: async (_owner: string, work: (session: unknown) => Promise<unknown>) =>work({ assertCurrent: () => {}, pull: fixture.pull, write: fixture.write }) }))vi.mock('../memory-persistence', async importOriginal => ({- ...await importOriginal<typeof import('../memory-persistence')>(), flushMemoryPersist: (guard: () => boolean) => fixture.flush(guard),+ ...await importOriginal<typeof import('../memory-persistence')>(), flushMemoryPersist: (guard: () => void) => fixture.flush(guard),}))const { synchronizeMemoryCollection } = await import('../memory-sync')const memory: MemoryFile = { id: 'one', type: 'user', title: 'Fact', content: 'Original fact', description: '', tags: [], source: 'manual', createdAt: 1, updatedAt: 1 }@@ -22,7 +22,10 @@ beforeEach(() => {useMemoryStore.getState().selectMemoryCollection('A')remote = []fixture.pull.mockReset().mockImplementation(async () => remote)- fixture.flush.mockReset().mockImplementation(async (guard: () => boolean) => { if (!guard()) throw new Error('Fixture stale'); return 'confirmed' })+ // R2-38: `guard` throws its OWN specific error (assertCurrent/check) rather+ // than returning false; the mock lets that error surface directly instead+ // of converting it to a generic "Fixture stale" rejection.+ fixture.flush.mockReset().mockImplementation(async (guard: () => void) => { guard(); return 'confirmed' })fixture.write.mockReset().mockImplementation(async (id: string, revision: number, payload: Record<string, unknown> | null) => {expect(Object.hasOwn(useMemoryStore.getState().memorySyncPending.A, id)).toBe(true)const saved = { memory_id: id, revision: revision + 1, payload, deleted: payload === null, updated_at: '2026-09-09T00:00:00Z' }diff --git a/src/lib/memory-legacy.ts b/src/lib/memory-legacy.tsindex 27064003..17ea5a14 100644--- a/src/lib/memory-legacy.ts+++ b/src/lib/memory-legacy.ts@@ -53,7 +53,7 @@ export async function reviewPreviousMemories(owner: string, signal?: AbortSignalguard(captured)return withMemorySyncSession(owner, async session => {const current = () => { session.assertCurrent(); guard(captured) }- await flushMemoryPersist(() => { try { current(); return true } catch { return false } })+ await flushMemoryPersist(current)const previous = await session.pullLegacy()current()const ids = previous.map(item => {diff --git a/src/lib/memory-persistence.ts b/src/lib/memory-persistence.tsindex 4125c9c9..2c63d22b 100644--- a/src/lib/memory-persistence.ts+++ b/src/lib/memory-persistence.ts@@ -42,16 +42,34 @@ export const memoryPersistence: StateStorage = {/** Confirm the snapshot offered before this call, never a later queued write.* A successful result proves an IndexedDB commit, not merely fallback success.- * Sync callers must keep entries and their baselines in the same store value. */-export function flushMemoryPersist(isCurrent: () => boolean = () => true): Promise<string> {+ * Sync callers must keep entries and their baselines in the same store value.+ *+ * `isCurrent` may throw instead of returning false (every caller in this repo+ * does: it is a guard that either passes silently or throws a specific+ * cancellation/account-changed error). R2-38: that error used to be lost, a+ * caller-side `catch { return false }` turned it into a plain boolean, and+ * this function then always threw its own generic "Memories changed" message+ * regardless of the real reason. The catch now lives here, and the original+ * error is what comes out. */+export function flushMemoryPersist(isCurrent: () => boolean | void = () => true): Promise<string> {const expected = latestValuereturn enqueue(async () => {const raw = await idbStorage.getItem(KEY)if (typeof raw !== 'string' || (expected !== undefined && raw !== expected)) {throw new Error('Could not confirm saved memories')}- if (!await compareAndSetIdbItem(KEY, raw, raw, isCurrent)) {- throw new Error('Memories changed while saving. Try again.')+ let abortReason: unknown+ const guarded = (): boolean => {+ try {+ const result = isCurrent()+ return result === false ? false : true+ } catch (e) {+ abortReason = e+ return false+ }+ }+ if (!await compareAndSetIdbItem(KEY, raw, raw, guarded)) {+ throw abortReason ?? new Error('Memories changed while saving. Try again.')}return raw})diff --git a/src/lib/memory-sync.ts b/src/lib/memory-sync.tsindex 3f914593..7a86bdf0 100644--- a/src/lib/memory-sync.ts+++ b/src/lib/memory-sync.ts@@ -71,11 +71,11 @@ export async function synchronizeMemoryCollection(owner: string, allowSensitiveexpectedEntries = state.entriesexpectedBaselines = state.memorySyncBaselinesexpectedPending = state.memorySyncPending- await flushMemoryPersist(() => { try { guard(); return true } catch { return false } })+ await flushMemoryPersist(guard)guard()}guard()- await flushMemoryPersist(() => { try { guard(); return true } catch { return false } })+ await flushMemoryPersist(guard)guard()const remote = await session.pull()guard()