Skip to content

Commit 2eb4242

Browse files
committed
fix(memory): preserve LanceDB index identity
1 parent b8122bd commit 2eb4242

5 files changed

Lines changed: 92 additions & 43 deletions

File tree

‎docs/plugins/memory-lancedb.md‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ with the current memory provider.
6565
}
6666
```
6767

68-
Restart the Gateway after changing plugin config, then verify it loaded:
68+
Restart the Gateway after installation, then verify it loaded:
6969

7070
```bash
7171
openclaw gateway restart
@@ -81,8 +81,8 @@ defaults to `openai`; `model` defaults to `text-embedding-3-small`.
8181
| ---------------------- | ------------- | ------------------------------------------------------------------------ |
8282
| `embedding.provider` | string | Adapter id, e.g. `openai`, `github-copilot`, `ollama`. Default `openai`. |
8383
| `embedding.model` | string | Default `text-embedding-3-small`. |
84-
| `embedding.apiKey` | string | Optional; supports `${ENV_VAR}` expansion. |
85-
| `embedding.baseUrl` | string | Optional; supports `${ENV_VAR}` expansion. |
84+
| `embedding.apiKey` | string | Optional; supports `${ENV_VAR}` expansion and live credential rotation. |
85+
| `embedding.baseUrl` | string | Optional; supports `${ENV_VAR}` expansion and live endpoint rotation. |
8686
| `embedding.dimensions` | integer (>=1) | Required for models not in the built-in table (see below). |
8787

8888
Two request paths exist:
@@ -98,6 +98,18 @@ Two request paths exist:
9898
for a raw OpenAI-compatible embeddings endpoint that has no bundled provider
9999
adapter.
100100

101+
`embedding.apiKey` and `embedding.baseUrl` are re-read from live plugin config
102+
for the next memory operation, as long as `provider`, `model`, and `dimensions`
103+
remain unchanged.
104+
105+
<Warning>
106+
`embedding.provider`, `embedding.model`, and `embedding.dimensions` define the
107+
persisted LanceDB index identity and do not change live. Before restarting with
108+
a new identity, plan a LanceDB re-embedding or rebuild so every stored row uses
109+
the new vector space and dimensions. The plugin does not re-embed existing rows
110+
automatically.
111+
</Warning>
112+
101113
OpenAI Codex / ChatGPT OAuth is not an OpenAI Platform embeddings credential.
102114
For OpenAI embeddings use an OpenAI API key auth profile, `OPENAI_API_KEY`, or
103115
`models.providers.openai.apiKey`. OAuth-only users should pick another
@@ -191,21 +203,23 @@ local server returns context-length errors.
191203

192204
## Recall and capture limits
193205

194-
| Setting | Default | Range | Applies to |
195-
| ----------------- | ------- | --------------------------- | ---------------------------------------------------------- |
196-
| `recallMaxChars` | `1000` | 100-10000 | Text sent to the embedding API for recall. |
197-
| `captureMaxChars` | `500` | 100-10000 | Message length eligible for auto-capture. |
198-
| `customTriggers` | `[]` | 0-50 items, each ≤100 chars | Literal phrases that make auto-capture consider a message. |
206+
| Setting | Default | Range | Applies to |
207+
| ----------------- | ------- | --------------------------- | ----------------------------------------------------------------- |
208+
| `recallMaxChars` | `1000` | 100-10000 | Recall query length and each escaped model-visible recalled item. |
209+
| `captureMaxChars` | `500` | 100-10000 | `memory_store` input limit and auto-capture eligibility. |
210+
| `customTriggers` | `[]` | 0-50 items, each ≤100 chars | Literal phrases that make auto-capture consider a message. |
199211

200212
`recallMaxChars` bounds the `before_prompt_build` auto-recall query, the
201213
`memory_recall` tool, the `memory_forget` query path, and `openclaw ltm search`.
202214
Auto-recall embeds the latest user message from the turn and falls back to the
203215
full prompt only when no user message is present, keeping channel metadata and
204-
large prompt blocks out of the embedding request.
216+
large prompt blocks out of the embedding request. It also bounds each recalled
217+
item after prompt escaping before that text reaches the model.
205218

206219
`captureMaxChars` gates whether a user message from the turn's `agent_end`
207-
event is short enough to be considered for auto-capture; it does not affect
208-
recall queries.
220+
event is short enough to be considered for auto-capture. `memory_store` rejects
221+
longer text before embedding or storage; the setting does not affect recall
222+
queries.
209223

210224
`customTriggers` adds literal auto-capture phrases without regex. Built-in
211225
triggers cover common English, Czech, Chinese, Japanese, and Korean memory
@@ -341,7 +355,7 @@ The embedding model rejected the recall query:
341355
memory-lancedb: recall failed: Error: 400 the input length exceeds the context length
342356
```
343357

344-
Lower `recallMaxChars`, then restart the Gateway:
358+
Lower `recallMaxChars`; the new limit applies to the next memory operation:
345359

346360
```json5
347361
{

‎extensions/memory-lancedb/embeddings.lifecycle.test.ts‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -482,41 +482,44 @@ describe("memory-lancedb provider lifecycle", () => {
482482
});
483483
const closeOldProvider = vi.fn(async () => {});
484484
const closeReplacementProvider = vi.fn(async () => {});
485-
const createProvider = vi.fn(async (options: { provider: string; model: string }) => {
486-
const isOld = options.provider === "fixture-old-provider";
487-
return {
488-
provider: {
489-
id: options.provider,
490-
model: options.model,
491-
embedQuery: vi.fn(async () => {
492-
if (isOld) {
493-
oldEmbeddingStarted();
494-
await oldEmbeddingGate;
495-
}
496-
return isOld ? [0.1] : [0.2];
497-
}),
498-
embedBatch: vi.fn(async () => [[0.1]]),
499-
close: isOld ? closeOldProvider : closeReplacementProvider,
500-
},
501-
};
502-
});
485+
const createProvider = vi.fn(
486+
async (options: { provider: string; model: string; remote?: { apiKey?: string } }) => {
487+
const isOld = options.remote?.apiKey === "fixture-old-key";
488+
return {
489+
provider: {
490+
id: options.provider,
491+
model: options.model,
492+
embedQuery: vi.fn(async () => {
493+
if (isOld) {
494+
oldEmbeddingStarted();
495+
await oldEmbeddingGate;
496+
}
497+
return isOld ? [0.1] : [0.2];
498+
}),
499+
embedBatch: vi.fn(async () => [[0.1]]),
500+
close: isOld ? closeOldProvider : closeReplacementProvider,
501+
},
502+
};
503+
},
504+
);
503505
providerMocks.getMemoryEmbeddingProvider.mockReturnValue({
504506
id: "fixture-provider",
505507
create: createProvider,
506508
});
509+
const embeddingIdentity = {
510+
provider: "fixture-provider",
511+
model: "fixture-model",
512+
dimensions: 3,
513+
} as const;
507514
const oldConfig = {
508-
provider: "fixture-old-provider",
509-
model: "fixture-old-model",
515+
...embeddingIdentity,
510516
apiKey: "fixture-old-key",
511517
baseUrl: "https://old.example.test/v1",
512-
dimensions: 3,
513518
} satisfies MemoryConfig["embedding"];
514519
const newConfig = {
515-
provider: "fixture-new-provider",
516-
model: "fixture-new-model",
520+
...embeddingIdentity,
517521
apiKey: "fixture-new-key",
518522
baseUrl: "https://new.example.test/v1",
519-
dimensions: 4,
520523
} satisfies MemoryConfig["embedding"];
521524
const embeddings = createEmbeddings(createApi());
522525

‎extensions/memory-lancedb/index.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,12 @@ describe("memory plugin e2e", () => {
908908
loadLanceDbModule,
909909
run: async () => {
910910
const pluginConfig = {
911-
embedding: { apiKey: "fixture-old-key", model: "text-embedding-3-small" },
911+
embedding: {
912+
apiKey: "fixture-old-key",
913+
baseUrl: "https://old.example.test/v1",
914+
model: "fixture-startup-model",
915+
dimensions: 3,
916+
},
912917
dbPath: getDbPath(),
913918
autoCapture: false,
914919
autoRecall: false,
@@ -939,12 +944,18 @@ describe("memory plugin e2e", () => {
939944
]);
940945

941946
expect(embeddingsCreate).toHaveBeenCalledWith({
942-
model: "text-embedding-3-small",
947+
model: "fixture-startup-model",
943948
input: "private shared-key query",
949+
dimensions: 3,
944950
});
945951
expect(embeddingsCreate).toHaveBeenCalledWith({
946-
model: "text-embedding-3-small",
952+
model: "fixture-startup-model",
947953
input: "main shared-key query",
954+
dimensions: 3,
955+
});
956+
expect(moduleMocks.createOpenAiClient).toHaveBeenNthCalledWith(1, {
957+
apiKey: "fixture-old-key",
958+
baseURL: "https://old.example.test/v1",
948959
});
949960
expect(moduleMocks.createOpenAiClient).toHaveBeenCalledOnce();
950961

@@ -957,8 +968,8 @@ describe("memory plugin e2e", () => {
957968
embedding: {
958969
apiKey: "fixture-new-key",
959970
baseUrl: "https://new.example.test/v1",
960-
model: "fixture-new-model",
961-
dimensions: 3,
971+
model: "fixture-ignored-live-model",
972+
dimensions: 4,
962973
},
963974
},
964975
},
@@ -974,7 +985,7 @@ describe("memory plugin e2e", () => {
974985
baseURL: "https://new.example.test/v1",
975986
});
976987
expect(embeddingsCreate).toHaveBeenCalledWith({
977-
model: "fixture-new-model",
988+
model: "fixture-startup-model",
978989
input: "rotated direct query",
979990
dimensions: 3,
980991
});

‎extensions/memory-lancedb/index.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default definePluginEntry({
174174
if (!runtimePluginConfig) {
175175
return disabledHookCfg;
176176
}
177-
return memoryConfigSchema.parse({
177+
const currentCfg = memoryConfigSchema.parse({
178178
embedding: {
179179
provider: cfg.embedding.provider,
180180
apiKey: cfg.embedding.apiKey,
@@ -194,6 +194,10 @@ export default definePluginEntry({
194194
...(cfg.storageOptions ? { storageOptions: cfg.storageOptions } : {}),
195195
...asOptionalRecord(runtimePluginConfig),
196196
});
197+
const { apiKey, baseUrl } = currentCfg.embedding;
198+
// LanceDB's fixed-size persisted vectors keep semantic identity startup-stable;
199+
// changing provider/model/dimensions without re-embedding corrupts search compatibility.
200+
return { ...currentCfg, embedding: { ...cfg.embedding, apiKey, baseUrl } };
197201
};
198202
const embeddings = createEmbeddings(api);
199203
const readMemoryRecallCooldown = (agentId: string): { error: string } | undefined => {

‎extensions/memory-lancedb/lancedb-store.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ describe("MemoryDB agent isolation", () => {
6868
reopened.close();
6969
});
7070

71+
test("rejects search when a persisted table is reopened with a different vector dimension", async () => {
72+
const db = new MemoryDB(getDbPath(), 2);
73+
await db.store("main", {
74+
text: "fixed-size vector",
75+
vector: [1, 0],
76+
importance: 0.7,
77+
category: "fact",
78+
});
79+
db.close();
80+
81+
const incompatible = new MemoryDB(getDbPath(), 3);
82+
await expect(incompatible.search("main", [1, 0, 0], 5, 0)).rejects.toThrow(
83+
"No vector column found to match with the query vector dimension: 3",
84+
);
85+
incompatible.close();
86+
});
87+
7188
test("refuses an unscoped legacy table until doctor migrates it", async () => {
7289
const connection = await lancedb.connect(getDbPath());
7390
const table = await connection.createTable("memories", [

0 commit comments

Comments
 (0)