Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(userMemories,database): bm25 should escape more characters like `<` and `>` (#13843)

Nekoe5be1801a16e
diff --git a/packages/database/src/utils/bm25.test.ts b/packages/database/src/utils/bm25.test.ts
index d525171d2d1..418911e9643 100644
--- a/packages/database/src/utils/bm25.test.ts
+++ b/packages/database/src/utils/bm25.test.ts
@@ -33,6 +33,9 @@ describe('sanitizeBm25Query', () => {
it('should escape multiple special characters and join with AND', () => {
expect(sanitizeBm25Query('(a+b) & c!')).toBe('\\(a\\+b\\) AND \\& AND c\\!');
expect(sanitizeBm25Query('react-component')).toBe('react AND component');
+ expect(sanitizeBm25Query('<https://lobehub.com/skills/openclaw>')).toBe(
+ '\\<https\\:\\/\\/lobehub.com\\/skills\\/openclaw\\>',
+ );
});
it('should trim whitespace', () => {
diff --git a/packages/database/src/utils/bm25.ts b/packages/database/src/utils/bm25.ts
index 7621448bd8f..590ca2b043e 100644
--- a/packages/database/src/utils/bm25.ts
+++ b/packages/database/src/utils/bm25.ts
@@ -28,7 +28,11 @@ export function sanitizeBm25Query(query: string, options: SanitizeBm25QueryOptio
.split(/\s+/)
.map((word) => word.trim())
.filter((word) => !dropBooleanOperators || !BM25_BOOLEAN_OPERATORS.has(word.toUpperCase()))
- .map((word) => word.replaceAll(/[+&|!(){}[\]^"~*?:\\/]/g, '\\$&'))
+ // NOTICE:
+ // Keep `<` and `>` in this escape set. Angle-bracket wrapped tokens can be
+ // interpreted as range-query boundaries by the BM25 parser and may trigger
+ // parse failures when the boundary contains multiple terms.
+ .map((word) => word.replaceAll(/[+&|!(){}[\]^"~*?:\\/<>]/g, '\\$&'))
.filter(Boolean);
if (terms.length === 0) throw new Error('Query is empty after sanitization');