|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import type { DatabaseSync } from "node:sqlite"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | +import type { OpenClawConfig } from "../config/config.js"; |
| 7 | +import { resetEmbeddingMocks } from "./embedding.test-mocks.js"; |
| 8 | +import type { MemoryIndexManager } from "./index.js"; |
| 9 | +import { getRequiredMemoryIndexManager } from "./test-manager-helpers.js"; |
| 10 | + |
| 11 | +describe("memory manager readonly recovery", () => { |
| 12 | + let workspaceDir = ""; |
| 13 | + let indexPath = ""; |
| 14 | + let manager: MemoryIndexManager | null = null; |
| 15 | + |
| 16 | + beforeEach(async () => { |
| 17 | + resetEmbeddingMocks(); |
| 18 | + workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-mem-readonly-")); |
| 19 | + indexPath = path.join(workspaceDir, "index.sqlite"); |
| 20 | + await fs.mkdir(path.join(workspaceDir, "memory"), { recursive: true }); |
| 21 | + await fs.writeFile(path.join(workspaceDir, "MEMORY.md"), "Hello memory."); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + if (manager) { |
| 26 | + await manager.close(); |
| 27 | + manager = null; |
| 28 | + } |
| 29 | + await fs.rm(workspaceDir, { recursive: true, force: true }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("reopens sqlite and retries once when sync hits SQLITE_READONLY", async () => { |
| 33 | + const cfg = { |
| 34 | + agents: { |
| 35 | + defaults: { |
| 36 | + workspace: workspaceDir, |
| 37 | + memorySearch: { |
| 38 | + provider: "openai", |
| 39 | + model: "mock-embed", |
| 40 | + store: { path: indexPath }, |
| 41 | + sync: { watch: false, onSessionStart: false, onSearch: false }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + list: [{ id: "main", default: true }], |
| 45 | + }, |
| 46 | + } as OpenClawConfig; |
| 47 | + |
| 48 | + manager = await getRequiredMemoryIndexManager({ cfg, agentId: "main" }); |
| 49 | + |
| 50 | + const runSyncSpy = vi.spyOn( |
| 51 | + manager as unknown as { |
| 52 | + runSync: (params?: { reason?: string; force?: boolean }) => Promise<void>; |
| 53 | + }, |
| 54 | + "runSync", |
| 55 | + ); |
| 56 | + runSyncSpy |
| 57 | + .mockRejectedValueOnce(new Error("attempt to write a readonly database")) |
| 58 | + .mockResolvedValueOnce(undefined); |
| 59 | + const openDatabaseSpy = vi.spyOn( |
| 60 | + manager as unknown as { openDatabase: () => DatabaseSync }, |
| 61 | + "openDatabase", |
| 62 | + ); |
| 63 | + |
| 64 | + await manager.sync({ reason: "test" }); |
| 65 | + |
| 66 | + expect(runSyncSpy).toHaveBeenCalledTimes(2); |
| 67 | + expect(openDatabaseSpy).toHaveBeenCalledTimes(1); |
| 68 | + expect(manager.status().custom?.readonlyRecovery).toEqual({ |
| 69 | + attempts: 1, |
| 70 | + successes: 1, |
| 71 | + failures: 0, |
| 72 | + lastError: "attempt to write a readonly database", |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("reopens sqlite and retries when readonly appears in error code", async () => { |
| 77 | + const cfg = { |
| 78 | + agents: { |
| 79 | + defaults: { |
| 80 | + workspace: workspaceDir, |
| 81 | + memorySearch: { |
| 82 | + provider: "openai", |
| 83 | + model: "mock-embed", |
| 84 | + store: { path: indexPath }, |
| 85 | + sync: { watch: false, onSessionStart: false, onSearch: false }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + list: [{ id: "main", default: true }], |
| 89 | + }, |
| 90 | + } as OpenClawConfig; |
| 91 | + |
| 92 | + manager = await getRequiredMemoryIndexManager({ cfg, agentId: "main" }); |
| 93 | + |
| 94 | + const runSyncSpy = vi.spyOn( |
| 95 | + manager as unknown as { |
| 96 | + runSync: (params?: { reason?: string; force?: boolean }) => Promise<void>; |
| 97 | + }, |
| 98 | + "runSync", |
| 99 | + ); |
| 100 | + runSyncSpy |
| 101 | + .mockRejectedValueOnce({ message: "write failed", code: "SQLITE_READONLY" }) |
| 102 | + .mockResolvedValueOnce(undefined); |
| 103 | + const openDatabaseSpy = vi.spyOn( |
| 104 | + manager as unknown as { openDatabase: () => DatabaseSync }, |
| 105 | + "openDatabase", |
| 106 | + ); |
| 107 | + |
| 108 | + await manager.sync({ reason: "test" }); |
| 109 | + |
| 110 | + expect(runSyncSpy).toHaveBeenCalledTimes(2); |
| 111 | + expect(openDatabaseSpy).toHaveBeenCalledTimes(1); |
| 112 | + expect(manager.status().custom?.readonlyRecovery).toEqual({ |
| 113 | + attempts: 1, |
| 114 | + successes: 1, |
| 115 | + failures: 0, |
| 116 | + lastError: "write failed", |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("does not retry non-readonly sync errors", async () => { |
| 121 | + const cfg = { |
| 122 | + agents: { |
| 123 | + defaults: { |
| 124 | + workspace: workspaceDir, |
| 125 | + memorySearch: { |
| 126 | + provider: "openai", |
| 127 | + model: "mock-embed", |
| 128 | + store: { path: indexPath }, |
| 129 | + sync: { watch: false, onSessionStart: false, onSearch: false }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + list: [{ id: "main", default: true }], |
| 133 | + }, |
| 134 | + } as OpenClawConfig; |
| 135 | + |
| 136 | + manager = await getRequiredMemoryIndexManager({ cfg, agentId: "main" }); |
| 137 | + |
| 138 | + const runSyncSpy = vi.spyOn( |
| 139 | + manager as unknown as { |
| 140 | + runSync: (params?: { reason?: string; force?: boolean }) => Promise<void>; |
| 141 | + }, |
| 142 | + "runSync", |
| 143 | + ); |
| 144 | + runSyncSpy.mockRejectedValueOnce(new Error("embedding timeout")); |
| 145 | + const openDatabaseSpy = vi.spyOn( |
| 146 | + manager as unknown as { openDatabase: () => DatabaseSync }, |
| 147 | + "openDatabase", |
| 148 | + ); |
| 149 | + |
| 150 | + await expect(manager.sync({ reason: "test" })).rejects.toThrow("embedding timeout"); |
| 151 | + expect(runSyncSpy).toHaveBeenCalledTimes(1); |
| 152 | + expect(openDatabaseSpy).toHaveBeenCalledTimes(0); |
| 153 | + }); |
| 154 | +}); |
0 commit comments