Skip to content

Commit f5553a8

Browse files
authored
fix(memory-wiki): ignore structural markers in query text (#128105)
1 parent c03c12c commit f5553a8

2 files changed

Lines changed: 173 additions & 4 deletions

File tree

‎extensions/memory-wiki/src/query.test.ts‎

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,169 @@ describe("searchMemoryWiki", () => {
345345
expect(results.map((result) => result.path)).toEqual(["entities/needle-person.md"]);
346346
});
347347

348+
it("filters standalone structural markers from matching, snippets, and ranking", async () => {
349+
const { rootDir, config } = await createQueryVault({
350+
initialize: true,
351+
});
352+
await Promise.all([
353+
fs.writeFile(
354+
path.join(rootDir, "entities", "marker-only.md"),
355+
renderWikiMarkdown({
356+
frontmatter: {
357+
pageType: "entity",
358+
id: "entity.marker-only",
359+
title: "Marker Only",
360+
},
361+
body: [
362+
"<!-- openclaw:wiki:generated:start -->",
363+
"<!-- openclaw:wiki:generated:end -->",
364+
"<!-- openclaw:human:start -->",
365+
"<!-- openclaw:human:end -->",
366+
"<!-- openclaw:wiki:raw-source -->",
367+
"",
368+
].join("\n"),
369+
}),
370+
"utf8",
371+
),
372+
fs.writeFile(
373+
path.join(rootDir, "entities", "evidence.md"),
374+
renderWikiMarkdown({
375+
frontmatter: {
376+
pageType: "entity",
377+
id: "entity.evidence",
378+
title: "Evidence Page",
379+
description: "openclaw release evidence",
380+
claims: [
381+
{
382+
id: "claim.evidence",
383+
text: "Neutral release note.",
384+
status: "supported",
385+
confidence: 0.8,
386+
evidence: [{ note: "supporting reference" }],
387+
},
388+
],
389+
},
390+
body: [
391+
"<!-- openclaw:human:start -->",
392+
"# Evidence Page",
393+
"",
394+
"Readable release evidence summary.",
395+
"<!-- openclaw:human:end -->",
396+
"",
397+
].join("\n"),
398+
}),
399+
"utf8",
400+
),
401+
fs.writeFile(
402+
path.join(rootDir, "entities", "marker-heavy.md"),
403+
renderWikiMarkdown({
404+
frontmatter: {
405+
pageType: "entity",
406+
id: "entity.marker-heavy",
407+
title: "Marker Heavy",
408+
},
409+
body: [
410+
"# Marker Heavy",
411+
"",
412+
"<!-- openclaw:wiki:generated:start -->",
413+
"openclaw body reference",
414+
"<!-- openclaw:wiki:generated:end -->",
415+
"<!-- openclaw:human:start -->",
416+
"<!-- openclaw:human:end -->",
417+
"<!-- openclaw:wiki:raw-source -->",
418+
"",
419+
].join("\n"),
420+
}),
421+
"utf8",
422+
),
423+
fs.writeFile(
424+
path.join(rootDir, "entities", "clean.md"),
425+
renderWikiMarkdown({
426+
frontmatter: {
427+
pageType: "entity",
428+
id: "entity.clean",
429+
title: "Clean",
430+
},
431+
body: "# Clean\n\nopenclaw openclaw body reference\n",
432+
}),
433+
"utf8",
434+
),
435+
]);
436+
437+
const evidenceResults = await searchMemoryWiki({
438+
config,
439+
query: "openclaw release",
440+
maxResults: 10,
441+
});
442+
expect(evidenceResults.map((result) => result.path)).toEqual(["entities/evidence.md"]);
443+
expect(evidenceResults[0]?.snippet).toBe("Readable release evidence summary.");
444+
445+
const openClawResults = await searchMemoryWiki({
446+
config,
447+
query: "openclaw",
448+
maxResults: 10,
449+
});
450+
const paths = openClawResults.map((result) => result.path);
451+
expect(paths).not.toContain("entities/marker-only.md");
452+
expect(
453+
openClawResults.find((result) => result.path === "entities/clean.md")?.score,
454+
).toBeGreaterThan(
455+
openClawResults.find((result) => result.path === "entities/marker-heavy.md")?.score ?? 0,
456+
);
457+
});
458+
459+
it("keeps managed-block content and inline marker prose searchable", async () => {
460+
const { rootDir, config } = await createQueryVault({
461+
initialize: true,
462+
});
463+
await Promise.all([
464+
fs.writeFile(
465+
path.join(rootDir, "entities", "managed-content.md"),
466+
renderWikiMarkdown({
467+
frontmatter: {
468+
pageType: "entity",
469+
id: "entity.managed-content",
470+
title: "Managed Content",
471+
},
472+
body: [
473+
"# Managed Content",
474+
"",
475+
"<!-- openclaw:wiki:generated:start -->",
476+
"Cobalt content remains searchable.",
477+
"<!-- openclaw:wiki:generated:end -->",
478+
"",
479+
].join("\n"),
480+
}),
481+
"utf8",
482+
),
483+
fs.writeFile(
484+
path.join(rootDir, "entities", "inline-marker.md"),
485+
renderWikiMarkdown({
486+
frontmatter: {
487+
pageType: "entity",
488+
id: "entity.inline-marker",
489+
title: "Inline Marker",
490+
},
491+
body: [
492+
"# Inline Marker",
493+
"",
494+
"The literal <!-- openclaw:wiki:generated:start --> inline-needle stays searchable.",
495+
"",
496+
].join("\n"),
497+
}),
498+
"utf8",
499+
),
500+
]);
501+
502+
const managedResults = await searchMemoryWiki({ config, query: "cobalt" });
503+
expect(managedResults.map((result) => result.path)).toEqual(["entities/managed-content.md"]);
504+
expect(managedResults[0]?.snippet).toBe("Cobalt content remains searchable.");
505+
506+
const inlineResults = await searchMemoryWiki({ config, query: "inline-needle" });
507+
expect(inlineResults.map((result) => result.path)).toEqual(["entities/inline-marker.md"]);
508+
expect(inlineResults[0]?.snippet).toContain("<!-- openclaw:wiki:generated:start -->");
509+
});
510+
348511
it("matches pages when all query terms appear without an exact phrase", async () => {
349512
const { rootDir, config } = await createQueryVault({
350513
initialize: true,

‎extensions/memory-wiki/src/query.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const WIKI_SNIPPET_MAX_CHARS = 700;
3535
const RELATED_BLOCK_PATTERN =
3636
/<!-- openclaw:wiki:related:start -->[\s\S]*?<!-- openclaw:wiki:related:end -->/g;
3737
const MARKDOWN_FRONTMATTER_PATTERN = /^\s*---\r?\n[\s\S]*?\r?\n---\r?\n?/;
38+
const STRUCTURAL_MARKER_LINE_PATTERN = /^\s*<!--\s*openclaw:(?:wiki|human):[^>]*-->\s*$/;
3839
const ROUTE_QUESTION_STOP_WORDS = new Set([
3940
"a",
4041
"about",
@@ -256,7 +257,7 @@ async function readQueryDigestBundle(
256257
function buildSnippet(raw: string, query: string): string {
257258
const queryLower = normalizeLowercaseStringOrEmpty(query);
258259
const queryTokens = buildQueryTokens(queryLower);
259-
const searchable = buildSnippetSearchText(raw);
260+
const searchable = buildSearchableBody(raw);
260261
const lines = searchable.split(/\r?\n/).filter((line) => line.trim().length > 0);
261262
const matchingLine =
262263
lines.find((line) =>
@@ -278,6 +279,7 @@ function buildPageSearchText(page: QueryableWikiPage): string {
278279
page.title,
279280
page.relativePath,
280281
page.id ?? "",
282+
JSON.stringify(parseWikiMarkdown(page.raw).frontmatter),
281283
page.pageType ?? "",
282284
page.entityType ?? "",
283285
page.canonicalId ?? "",
@@ -331,8 +333,12 @@ function stripGeneratedRelatedBlock(raw: string): string {
331333
return raw.replace(RELATED_BLOCK_PATTERN, "");
332334
}
333335

334-
function buildSnippetSearchText(raw: string): string {
335-
return stripGeneratedRelatedBlock(raw).replace(MARKDOWN_FRONTMATTER_PATTERN, "");
336+
function buildSearchableBody(raw: string): string {
337+
return stripGeneratedRelatedBlock(raw)
338+
.replace(MARKDOWN_FRONTMATTER_PATTERN, "")
339+
.split(/\r?\n/)
340+
.filter((line) => !STRUCTURAL_MARKER_LINE_PATTERN.test(line))
341+
.join("\n");
336342
}
337343

338344
function buildQueryTokens(queryLower: string): string[] {
@@ -834,7 +840,7 @@ function scorePage(page: QueryableWikiPage, query: string, mode: WikiSearchMode)
834840
const pathLower = normalizeLowercaseStringOrEmpty(page.relativePath);
835841
const idLower = normalizeLowercaseStringOrEmpty(page.id);
836842
const metadataLower = normalizeLowercaseStringOrEmpty(buildPageSearchText(page));
837-
const rawLower = normalizeLowercaseStringOrEmpty(stripGeneratedRelatedBlock(page.raw));
843+
const rawLower = normalizeLowercaseStringOrEmpty(buildSearchableBody(page.raw));
838844
const combinedLower = [titleLower, pathLower, idLower, metadataLower, rawLower].join("\n");
839845
const hasExactMatch =
840846
titleLower.includes(queryLower) ||

0 commit comments

Comments
 (0)