Skip to content

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.

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.ts
index 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 = null
fixture.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.ts
index 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 impor
withMemorySyncSession: 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.ts
index 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?: AbortSignal
guard(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.ts
index 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 = latestValue
return 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.ts
index 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, allowSensitive
expectedEntries = state.entries
expectedBaselines = state.memorySyncBaselines
expectedPending = 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()