Skip to content

Commit 355a9cb

Browse files
committed
fix(memory): fall back to sqlite-vec platform variant
1 parent 6d7eb9b commit 355a9cb

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

packages/memory-host-sdk/src/host/sqlite-vec.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ function mockMissingSqliteVecPackage(): void {
1111
});
1212
}
1313

14+
function mockFailingSqliteVecPackage(): void {
15+
vi.doMock("sqlite-vec", () => ({
16+
getLoadablePath: () => "/install/node_modules/sqlite-vec-linux-x64/vec0.so",
17+
load: () => {
18+
throw new Error("bundled sqlite-vec load failed");
19+
},
20+
}));
21+
}
22+
1423
function mockPlatformVariantResolver(
1524
value: { pkg: string; extensionPath: string } | undefined,
1625
): void {
@@ -140,6 +149,27 @@ describe("loadSqliteVecExtension", () => {
140149
expect(prepare).toHaveBeenCalledWith("SELECT vec_version() AS version");
141150
});
142151

152+
it("falls back to the platform variant when bundled sqlite-vec load fails", async () => {
153+
mockFailingSqliteVecPackage();
154+
mockPlatformVariantResolver({
155+
pkg: "sqlite-vec-linux-x64",
156+
extensionPath: "/install/node_modules/sqlite-vec-linux-x64/vec0.so",
157+
});
158+
const { loadSqliteVecExtension } = await importLoader();
159+
const { db, prepare } = createDbMock();
160+
161+
const result = await loadSqliteVecExtension({ db: db as never });
162+
163+
expect(result).toEqual({
164+
ok: true,
165+
extensionPath: "/install/node_modules/sqlite-vec-linux-x64/vec0.so",
166+
});
167+
expect(db.loadExtension).toHaveBeenCalledWith(
168+
"/install/node_modules/sqlite-vec-linux-x64/vec0.so",
169+
);
170+
expect(prepare).toHaveBeenCalledWith("SELECT vec_version() AS version");
171+
});
172+
143173
it("resolves the installed platform variant through its exported vec0 subpath", async () => {
144174
const entry = CURRENT_PLATFORM_VARIANTS[`${process.platform}-${process.arch}`];
145175
if (!entry) {
@@ -204,4 +234,25 @@ describe("loadSqliteVecExtension", () => {
204234
"sqlite-vec platform variant sqlite-vec-linux-x64 failed to load from /install/node_modules/sqlite-vec-linux-x64/vec0.so. Set agents.defaults.memorySearch.store.vector.extensionPath, or an agent-specific memorySearch.store.vector.extensionPath, to a sqlite-vec loadable extension path. Original error: sqlite-vec health check failed after loading /install/node_modules/sqlite-vec-linux-x64/vec0.so | no such function: vec_version",
205235
});
206236
});
237+
238+
it("preserves bundled sqlite-vec and platform variant errors when both fail", async () => {
239+
mockFailingSqliteVecPackage();
240+
mockPlatformVariantResolver({
241+
pkg: "sqlite-vec-linux-x64",
242+
extensionPath: "/install/node_modules/sqlite-vec-linux-x64/vec0.so",
243+
});
244+
const { loadSqliteVecExtension } = await importLoader();
245+
const { db } = createDbMock();
246+
db.loadExtension.mockImplementation(() => {
247+
throw new Error("platform variant failed");
248+
});
249+
250+
const result = await loadSqliteVecExtension({ db: db as never });
251+
252+
expect(result).toEqual({
253+
ok: false,
254+
error:
255+
"sqlite-vec package failed to load, and platform variant sqlite-vec-linux-x64 failed to load from /install/node_modules/sqlite-vec-linux-x64/vec0.so. Set agents.defaults.memorySearch.store.vector.extensionPath, or an agent-specific memorySearch.store.vector.extensionPath, to a sqlite-vec loadable extension path. Package error: bundled sqlite-vec load failed. Variant error: platform variant failed",
256+
});
257+
});
207258
});

packages/memory-host-sdk/src/host/sqlite-vec.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ export async function loadSqliteVecExtension(params: {
6565
assertSqliteVecAvailable(params.db, extensionPath);
6666
return { ok: true, extensionPath };
6767
} catch (err) {
68-
if (!isMissingSqliteVecPackageError(err)) {
69-
throw err;
70-
}
7168
// Optional-dep installs sometimes land only the platform-specific variant
7269
// (e.g. sqlite-vec-linux-x64) without the meta sqlite-vec package. Load
7370
// the loadable extension straight from the variant when we can find it.
71+
// Bundled runtimes can also fail the meta-package import while the native
72+
// variant is still present, so try the concrete extension before failing.
7473
const variant = resolveSqliteVecPlatformVariant();
7574
if (!variant) {
75+
if (!isMissingSqliteVecPackageError(err)) {
76+
throw err;
77+
}
7678
const message = formatErrorMessage(err);
7779
return {
7880
ok: false,
@@ -84,6 +86,13 @@ export async function loadSqliteVecExtension(params: {
8486
return { ok: true, extensionPath: variant.extensionPath };
8587
} catch (variantErr) {
8688
const message = formatErrorMessage(variantErr);
89+
if (!isMissingSqliteVecPackageError(err)) {
90+
const packageMessage = formatErrorMessage(err);
91+
return {
92+
ok: false,
93+
error: `sqlite-vec package failed to load, and platform variant ${variant.pkg} failed to load from ${variant.extensionPath}. ${SQLITE_VEC_CONFIG_HINT} Package error: ${packageMessage}. Variant error: ${message}`,
94+
};
95+
}
8796
return {
8897
ok: false,
8998
error: `sqlite-vec platform variant ${variant.pkg} failed to load from ${variant.extensionPath}. ${SQLITE_VEC_CONFIG_HINT} Original error: ${message}`,

0 commit comments

Comments
 (0)