Skip to content

Commit aef52d8

Browse files
committed
fix(memory): preserve cancellation through hybrid search
1 parent 016472d commit aef52d8

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎extensions/memory-core/src/memory/manager-search-orchestration.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Memory Core tests cover manager search orchestration behavior.
22
import fs from "node:fs/promises";
33
import path from "node:path";
4+
import type { DatabaseSync } from "node:sqlite";
45
import { describe, expect, it, vi } from "vitest";
56
import {
67
createManagerIndexFixture,
@@ -195,6 +196,81 @@ describe("memory index", () => {
195196
expect(close).not.toHaveBeenCalled();
196197
});
197198

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+
198274
it("supplements thin strict FTS results for conversational queries", async () => {
199275
const cases = [
200276
{

‎extensions/memory-core/src/memory/manager-search-orchestration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ export abstract class MemorySearchOrchestration extends MemoryKeywordRetrieval {
366366
vectorProviderIdentity,
367367
opts?.signal,
368368
).catch((err: unknown) => {
369+
opts?.signal?.throwIfAborted();
369370
log.warn(`memory search: vector query failed: ${formatErrorMessage(err)}`);
370371
return [];
371372
})

0 commit comments

Comments
 (0)