|
1 | 1 | // Memory Core tests cover manager search orchestration behavior. |
2 | 2 | import fs from "node:fs/promises"; |
3 | 3 | import path from "node:path"; |
| 4 | +import type { DatabaseSync } from "node:sqlite"; |
4 | 5 | import { describe, expect, it, vi } from "vitest"; |
5 | 6 | import { |
6 | 7 | createManagerIndexFixture, |
@@ -195,6 +196,81 @@ describe("memory index", () => { |
195 | 196 | expect(close).not.toHaveBeenCalled(); |
196 | 197 | }); |
197 | 198 |
|
| 199 | + it("rejects caller cancellation during hybrid fallback scanning", async () => { |
| 200 | + const manager = await getPersistentManager( |
| 201 | + createCfg({ |
| 202 | + minScore: 0, |
| 203 | + hybrid: { enabled: true, vectorWeight: 0.5, textWeight: 0.5 }, |
| 204 | + }), |
| 205 | + ); |
| 206 | + await manager.sync({ reason: "test" }); |
| 207 | + |
| 208 | + const fields = manager as unknown as { |
| 209 | + db: DatabaseSync; |
| 210 | + ensureVectorReady: (dimensions?: number) => Promise<boolean>; |
| 211 | + }; |
| 212 | + fields.ensureVectorReady = async () => false; |
| 213 | + const insertChunk = fields.db.prepare( |
| 214 | + "INSERT INTO memory_index_chunks (id, path, source, start_line, end_line, hash, model, text, embedding, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", |
| 215 | + ); |
| 216 | + for (let index = 0; index < 4096; index += 1) { |
| 217 | + insertChunk.run( |
| 218 | + `cancel-scan-${index}`, |
| 219 | + `memory/cancel-scan-${index}.md`, |
| 220 | + "memory", |
| 221 | + 1, |
| 222 | + 1, |
| 223 | + `cancel-scan-hash-${index}`, |
| 224 | + "mock-embed", |
| 225 | + `fallback scan row ${index}`, |
| 226 | + JSON.stringify([0, 1, 0, 0]), |
| 227 | + index, |
| 228 | + ); |
| 229 | + } |
| 230 | + |
| 231 | + const originalPrepare = fields.db.prepare.bind(fields.db); |
| 232 | + let scannedBatches = 0; |
| 233 | + const prepareSpy = vi.spyOn(fields.db, "prepare").mockImplementation((sql: string) => { |
| 234 | + const statement = originalPrepare(sql); |
| 235 | + if (!sql.includes("SELECT rowid, id, path")) { |
| 236 | + return statement; |
| 237 | + } |
| 238 | + return { |
| 239 | + all: (...args: Parameters<typeof statement.all>) => { |
| 240 | + scannedBatches += 1; |
| 241 | + return statement.all(...args); |
| 242 | + }, |
| 243 | + } as unknown as typeof statement; |
| 244 | + }); |
| 245 | + |
| 246 | + try { |
| 247 | + const caller = new AbortController(); |
| 248 | + const abortReason = new Error("caller stopped hybrid memory search"); |
| 249 | + const pending = manager.search("alpha", { signal: caller.signal }); |
| 250 | + setImmediate(() => caller.abort(abortReason)); |
| 251 | + |
| 252 | + await expect(pending).rejects.toBe(abortReason); |
| 253 | + await new Promise<void>((resolve) => { |
| 254 | + setImmediate(resolve); |
| 255 | + }); |
| 256 | + await new Promise<void>((resolve) => { |
| 257 | + setImmediate(resolve); |
| 258 | + }); |
| 259 | + expect(scannedBatches).toBe(1); |
| 260 | + |
| 261 | + const healthyResults = await manager.search("alpha"); |
| 262 | + expect(healthyResults.some((result) => result.path === "memory/2026-01-12.md")).toBe(true); |
| 263 | + |
| 264 | + fields.ensureVectorReady = async () => { |
| 265 | + throw new Error("vector store unavailable"); |
| 266 | + }; |
| 267 | + const degradedResults = await manager.search("alpha"); |
| 268 | + expect(degradedResults.some((result) => result.path === "memory/2026-01-12.md")).toBe(true); |
| 269 | + } finally { |
| 270 | + prepareSpy.mockRestore(); |
| 271 | + } |
| 272 | + }); |
| 273 | + |
198 | 274 | it("supplements thin strict FTS results for conversational queries", async () => { |
199 | 275 | const cases = [ |
200 | 276 | { |
|
0 commit comments