Skip to content

Commit b7d6291

Browse files
authored
fix(memory): preserve Unicode in QMD snippets (#103996)
1 parent 920b29b commit b7d6291

4 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎extensions/memory-core/src/memory/qmd-manager.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,6 +4395,75 @@ describe("QmdMemoryManager", () => {
43954395
await manager.close();
43964396
});
43974397

4398+
it("keeps per-result and aggregate QMD snippet limits UTF-16 safe", async () => {
4399+
const expectedDocId = "unicode-boundary";
4400+
const snippet = "@@ -1,1\nabc😀tail";
4401+
spawnMock.mockImplementation((cmd: string, args: string[]) => {
4402+
const child = createMockChild({ autoClose: false });
4403+
if (isMcporterCommand(cmd) && args[0] === "call") {
4404+
emitAndClose(
4405+
child,
4406+
"stdout",
4407+
JSON.stringify({
4408+
results: [
4409+
{
4410+
docid: expectedDocId,
4411+
score: 0.91,
4412+
collection: "workspace-main",
4413+
snippet,
4414+
},
4415+
],
4416+
}),
4417+
);
4418+
return child;
4419+
}
4420+
emitAndClose(child, "stdout", "[]");
4421+
return child;
4422+
});
4423+
4424+
const searchWithLimits = async (limits: {
4425+
maxSnippetChars: number;
4426+
maxInjectedChars: number;
4427+
}) => {
4428+
const testConfig = {
4429+
...cfg,
4430+
memory: {
4431+
backend: "qmd",
4432+
qmd: {
4433+
includeDefaultMemory: false,
4434+
searchMode: "query",
4435+
update: { interval: "0s", debounceMs: 60_000, onBoot: false },
4436+
paths: [{ path: workspaceDir, pattern: "**/*.md", name: "workspace" }],
4437+
limits,
4438+
mcporter: { enabled: true, serverName: "qmd", startDaemon: false },
4439+
},
4440+
},
4441+
} as OpenClawConfig;
4442+
const { manager } = await createManager({ cfg: testConfig });
4443+
const inner = manager as unknown as {
4444+
db: { prepare: () => { all: () => unknown }; close: () => void };
4445+
};
4446+
inner.db = {
4447+
prepare: () => ({
4448+
all: () => [{ collection: "workspace-main", path: "notes/unicode.md" }],
4449+
}),
4450+
close: () => {},
4451+
};
4452+
const results = await manager.search("unicode", {
4453+
sessionKey: "agent:main:slack:dm:u123",
4454+
});
4455+
await manager.close();
4456+
return results;
4457+
};
4458+
4459+
await expect(searchWithLimits({ maxSnippetChars: 12, maxInjectedChars: 100 })).resolves.toEqual(
4460+
[expect.objectContaining({ snippet: "@@ -1,1\nabc" })],
4461+
);
4462+
await expect(searchWithLimits({ maxSnippetChars: 100, maxInjectedChars: 12 })).resolves.toEqual(
4463+
[expect.objectContaining({ snippet: "@@ -1,1\nabc" })],
4464+
);
4465+
});
4466+
43984467
it("uses snippet header width when mcporter only returns a start line", async () => {
43994468
const expectedDocId = "line-456";
44004469
cfg = {

‎extensions/memory-core/src/memory/qmd-manager.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
resolveAgentWorkspaceDir,
1818
resolveGlobalSingleton,
1919
resolveStateDir,
20+
truncateUtf16Safe,
2021
type OpenClawConfig,
2122
} from "openclaw/plugin-sdk/memory-core-host-engine-foundation";
2223
import {
@@ -1719,7 +1720,7 @@ export class QmdMemoryManager implements MemorySearchManager {
17191720
if (!doc) {
17201721
continue;
17211722
}
1722-
const snippet = entry.snippet?.slice(0, this.qmd.limits.maxSnippetChars) ?? "";
1723+
const snippet = truncateUtf16Safe(entry.snippet ?? "", this.qmd.limits.maxSnippetChars);
17231724
const lines = this.resolveSnippetLines(entry, snippet);
17241725
const score = typeof entry.score === "number" ? entry.score : 0;
17251726
const minScore = opts?.minScore ?? 0;
@@ -3526,7 +3527,7 @@ export class QmdMemoryManager implements MemorySearchManager {
35263527
clamped.push(entry);
35273528
remaining -= snippet.length;
35283529
} else {
3529-
const trimmed = snippet.slice(0, Math.max(0, remaining));
3530+
const trimmed = truncateUtf16Safe(snippet, remaining);
35303531
clamped.push(copyQmdSessionArtifactHit(entry, { ...entry, snippet: trimmed }));
35313532
break;
35323533
}

‎extensions/memory-core/src/tools.citations.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,25 @@ describe("memory search citations", () => {
121121

122122
it("clamps decorated snippets to qmd injected budget", async () => {
123123
setMemoryBackend("qmd");
124+
setMemorySearchImpl(async () => [
125+
{
126+
path: "MEMORY.md",
127+
startLine: 5,
128+
endLine: 7,
129+
score: 0.9,
130+
snippet: "abc😀tail",
131+
source: "memory" as const,
132+
},
133+
]);
124134
const cfg = asOpenClawConfig({
125-
memory: { citations: "on", backend: "qmd", qmd: { limits: { maxInjectedChars: 20 } } },
135+
memory: { citations: "on", backend: "qmd", qmd: { limits: { maxInjectedChars: 4 } } },
126136
agents: { list: [{ id: "main", default: true }] },
127137
});
128138
const tool = createMemorySearchToolOrThrow({ config: cfg });
129139
const result = await tool.execute("call_citations_qmd", { query: "notes" });
130140
const details = result.details as { results: Array<{ snippet: string; citation?: string }> };
131141
const firstResult = expectFirstMemoryResult(details);
132-
expect(firstResult.snippet.length).toBeLessThanOrEqual(20);
142+
expect(firstResult.snippet).toBe("abc");
133143
});
134144

135145
it("honors auto mode for direct chats", async () => {

‎extensions/memory-core/src/tools.citations.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from "openclaw/plugin-sdk/memory-core-host-runtime-core";
77
import type { MemorySearchResult } from "openclaw/plugin-sdk/memory-core-host-runtime-files";
88
import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
9+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
910

1011
export function resolveMemoryCitationsMode(cfg: OpenClawConfig): MemoryCitationsMode {
1112
const mode = cfg.memory?.citations;
@@ -55,7 +56,7 @@ export function clampResultsByInjectedChars(
5556
clamped.push(entry);
5657
remaining -= snippet.length;
5758
} else {
58-
const trimmed = snippet.slice(0, Math.max(0, remaining));
59+
const trimmed = truncateUtf16Safe(snippet, remaining);
5960
clamped.push({ ...entry, snippet: trimmed });
6061
break;
6162
}

0 commit comments

Comments
 (0)