Repository navigation
Commit 2ceb44b
feat(memory-lancedb): scope memory_recall and memory_forget + configurable thresholds
In a shared LanceDB memory store, the explicit memory tools operate across the
whole store: memory_recall selects the top-K nearest vectors globally, and
memory_forget (query mode) runs an unfiltered search that can auto-delete the
wrong context's copy once the same fact exists in more than one context. Add an
optional per-row scope column and a native LanceDB .where() prefilter in both
tools, constraining results to the requested scope before the vector search
returns — no separate store per context needed. .where() is already used on the
--filter CLI path but not on these tools.
Scope is an opaque partition key, not a fixed concept: it is whatever the caller
partitions by (a project, a person, a channel, a tenant). A memory is tagged by
prefixing its text with [SCOPE:<value>], and both tools take a scope parameter.
The tag is a routing prefix: it is parsed into the scope column and stripped from
the text before embedding and storing (both the explicit store tool and the
auto-capture path), so the vector reflects the fact rather than the synthetic
prefix and the tag is never echoed back on recall. A scope key is a slug
([A-Za-z0-9_-]+); a tag or scope argument whose key is not a valid slug is rejected
rather than silently transformed, so a punctuated key (e.g. a raw channel/room id)
can never be canonicalized into another partition nor silently stored global. A tag
with no fact after it ([SCOPE:<value>] alone, or only whitespace) is rejected on
store and skipped on auto-capture, so the control tag is never embedded or persisted
as a memory on its own. Auto-capture strips the tag before its capture heuristics
run, so a long routing tag cannot change a message's eligibility (neither pushing
short content over the length floor nor valid content past captureMaxChars) — the
decision is made on the fact, not the prefix. OpenClaw builds the .where() clause
internally from the validated slug — no raw filter expression is accepted from the
caller.
Scope is enforced as a partition, not an optional label: a tagged memory is hidden
from any operation that does not ask for its scope. A scoped memory_recall returns
that scope's matches first with global rows filling the rest (scope and global are
retrieved in separate ANN passes, each with its own candidate budget, so strong
global neighbors cannot crowd the scope out of a shared top-K); an unscoped recall
is global-only, so scoped rows never bleed into a plain recall. memory_forget is
symmetric: a scoped forget is restricted to that exact scope, and an unscoped
forget is global-only — and this holds for both delete paths, the query path
(scope filter) and the memoryId path (fenced by checking the target row's scope
before deleting), so a known id from another partition cannot bypass the fence. The
automatic before-prompt recall is also global-only — it has no active-scope
signal, so it injects only global/untagged memories and never auto-recalls a
scoped row into an unrelated session. The ltm search CLI mirrors the tools:
unscoped it is global-only (never a table-wide scan that would expose scoped rows),
and an opt-in --scope <slug> restricts the vector search to a single partition.
Make duplicate detection scope-aware: the scope is applied as a .where() prefilter
before the near-exact top-K, so a duplicate only counts within the same scope and a
same-scope duplicate is never pushed out of the candidate window by other scopes.
The same fact can coexist under different scopes; same-scope and global-vs-global
writes still dedup. A self-healing migration adds the column to pre-existing
tables, preserving rows and vectors.
Surface recall selectivity as configuration (recallMinScore, recallResultCap,
autoRecallOverfetch), with matching manifest uiHints, and defaults equal to the
previously hardcoded values. These fields are threaded through the live-config
merge so operator values take effect at the before-prompt hook, not only at
startup.
Deferred (non-goal): this does not target the automatic before-prompt injection at
a specific scope — the hook has no active-scope signal, so it cannot know which
partition the current turn belongs to. Rather than leak, it fails safe by injecting
only global/untagged memories. Scope-targeted auto-injection is future work; the
scope column and .where() prefilter added here are the foundation for it.
Behavior-preserving until a memory is tagged: every pre-existing row has an empty
scope, so it stays global and is returned by unscoped recall and the automatic
injection exactly as before; thresholds default to today's values. A memory becomes
partitioned only when explicitly tagged [SCOPE:<value>], so installs that never use
scope tags see no change.
Add regression tests: the [SCOPE:<value>] tag survives hyphens and underscores
(no truncation/mis-scoping) and is stripped from the embedded input and stored
text; dedup is scope-aware and prefilters by scope before the top-K; scoped
memory_recall prioritizes its scope over many strong global matches; operator
recall thresholds survive the live-config merge into auto-recall; an unscoped
memory_recall returns only global rows; an unscoped memory_forget cannot delete a
scoped row; auto-recall injects only global memories; a scoped memory_forget
deletes only within its scope; a store tag with a non-slug key is rejected (no
silent global fallback); a memoryId forget is fenced to the row's scope; a tag-only
[SCOPE:<value>] store is rejected (and auto-capture skips it) rather than persisting
the bare tag; ltm search is global-only unless an explicit --scope slug is given; and
auto-capture eligibility is evaluated on the scope-stripped text, so a long routing
tag cannot flip a message's memory-worthiness.
Document the scope contract (tag/slug shape, global-vs-scoped recall and forget,
memoryId fence, global-only auto-recall) and the recall-selectivity settings in
docs/plugins/memory-lancedb.md, and regenerate docs/docs_map.md for the new
headings. The added tests are lint-clean under the type-aware oxlint config CI runs
(--tsconfig: restrict-template-expressions, no-unnecessary-type-conversion) and carry
no unchecked `any` casts, so the extensions lint and type-suppression-inventory gates
stay green.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>1 parent 4e78d91 commit 2ceb44b
6 files changed
Lines changed: 1983 additions & 79 deletions
File tree
- docs
- plugins
- extensions/memory-lancedb
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5915 | 5915 | | |
5916 | 5916 | | |
5917 | 5917 | | |
| 5918 | + | |
5918 | 5919 | | |
| 5920 | + | |
5919 | 5921 | | |
5920 | 5922 | | |
5921 | 5923 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
206 | 206 | | |
207 | 207 | | |
208 | 208 | | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
209 | 223 | | |
210 | 224 | | |
211 | 225 | | |
212 | 226 | | |
213 | 227 | | |
214 | 228 | | |
215 | 229 | | |
216 | | - | |
| 230 | + | |
217 | 231 | | |
218 | 232 | | |
219 | 233 | | |
| |||
233 | 247 | | |
234 | 248 | | |
235 | 249 | | |
236 | | - | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
237 | 253 | | |
238 | 254 | | |
| 255 | + | |
239 | 256 | | |
240 | | - | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
241 | 297 | | |
242 | 298 | | |
243 | 299 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
| |||
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
155 | 161 | | |
156 | 162 | | |
157 | 163 | | |
| |||
186 | 192 | | |
187 | 193 | | |
188 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
189 | 220 | | |
190 | 221 | | |
191 | 222 | | |
| |||
251 | 282 | | |
252 | 283 | | |
253 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
254 | 288 | | |
255 | 289 | | |
256 | 290 | | |
| |||
314 | 348 | | |
315 | 349 | | |
316 | 350 | | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
317 | 369 | | |
318 | 370 | | |
319 | 371 | | |
| |||
0 commit comments