Skip to content

Commit 6d8f0a1

Browse files
authored
fix(memory-wiki): protect notes with damaged human markers (#117104)
1 parent d0b6a92 commit 6d8f0a1

6 files changed

Lines changed: 387 additions & 10 deletions

File tree

‎extensions/memory-wiki/src/ingest-human-notes.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,51 @@ describe("ingestMemoryWikiSource human notes", () => {
3939
expect(after).toContain(userNote);
4040
});
4141

42+
it.each([
43+
{
44+
name: "closing",
45+
malformedNotes: "<!-- openclaw:human:start -->\nHANDWRITTEN NOTE MUST SURVIVE",
46+
missingMarker: /openclaw:human:end/i,
47+
},
48+
{
49+
name: "opening",
50+
malformedNotes: "HANDWRITTEN NOTE MUST SURVIVE\n<!-- openclaw:human:end -->",
51+
missingMarker: /openclaw:human:start/i,
52+
},
53+
])(
54+
"preserves the source page when handwritten Notes are missing the $name marker",
55+
async ({ malformedNotes, missingMarker }) => {
56+
const rootDir = await createTempDir("memory-wiki-malformed-notes-");
57+
const inputPath = path.join(rootDir, "roadmap.txt");
58+
const { config } = await createVault({ rootDir: path.join(rootDir, "vault") });
59+
60+
await fs.writeFile(inputPath, "original source content\n", "utf8");
61+
await ingestMemoryWikiSource({
62+
config,
63+
inputPath,
64+
nowMs: Date.UTC(2026, 3, 5, 12, 0, 0),
65+
});
66+
67+
const pagePath = path.join(config.vault.path, "sources", "roadmap.md");
68+
const existingPage = (await fs.readFile(pagePath, "utf8")).replace(
69+
"<!-- openclaw:human:start -->\n<!-- openclaw:human:end -->",
70+
malformedNotes,
71+
);
72+
await fs.writeFile(pagePath, existingPage, "utf8");
73+
await fs.writeFile(inputPath, "updated source content\n", "utf8");
74+
75+
await expect(
76+
ingestMemoryWikiSource({
77+
config,
78+
inputPath,
79+
nowMs: Date.UTC(2026, 3, 6, 12, 0, 0),
80+
}),
81+
).rejects.toThrow(missingMarker);
82+
83+
await expect(fs.readFile(pagePath, "utf8")).resolves.toBe(existingPage);
84+
},
85+
);
86+
4287
it("preserves notes without corrupting source content that contains human markers", async () => {
4388
const rootDir = await createTempDir("memory-wiki-markers-");
4489
const inputPath = path.join(rootDir, "notes.txt");

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import path from "node:path";
44
import { describe, expect, it } from "vitest";
55
import {
66
createWikiPageFilename,
7+
extractHumanNotesBlock,
78
parseWikiMarkdown,
9+
preserveHumanNotesBlock,
810
renderWikiMarkdown,
911
scanWikiPageSummary,
1012
slugifyWikiSegment,
@@ -65,6 +67,112 @@ describe("slugifyWikiSegment", () => {
6567
});
6668
});
6769

70+
describe("human Notes blocks", () => {
71+
const startMarker = "<!-- openclaw:human:start -->";
72+
const endMarker = "<!-- openclaw:human:end -->";
73+
const rendered = ["# Source", "", "## Notes", startMarker, endMarker, ""].join("\n");
74+
75+
it("extracts and preserves complete human Notes blocks", () => {
76+
const existing = rendered.replace(
77+
`${startMarker}\n${endMarker}`,
78+
`${startMarker}\nDurable human annotation\n${endMarker}`,
79+
);
80+
81+
expect(extractHumanNotesBlock(existing)).toBe(
82+
`${startMarker}\nDurable human annotation\n${endMarker}`,
83+
);
84+
expect(preserveHumanNotesBlock(rendered, existing)).toBe(existing);
85+
expect(extractHumanNotesBlock(rendered)).toBeNull();
86+
});
87+
88+
it("leaves pages without human Notes markers unchanged", () => {
89+
const existing = "# Source\n\nGenerated content only.\n";
90+
91+
expect(extractHumanNotesBlock(existing)).toBeNull();
92+
expect(preserveHumanNotesBlock(rendered, existing)).toBe(rendered);
93+
expect(preserveHumanNotesBlock(existing, rendered)).toBe(existing);
94+
});
95+
96+
it.each([
97+
{
98+
name: "closing",
99+
lines: [startMarker, "Durable human annotation"],
100+
missingMarker: endMarker,
101+
},
102+
{
103+
name: "opening",
104+
lines: ["Durable human annotation", endMarker],
105+
missingMarker: startMarker,
106+
},
107+
])(
108+
"rejects a missing $name marker before extracting or replacing Notes",
109+
({ lines, missingMarker }) => {
110+
const malformed = ["# Source", "", "## Notes", ...lines, ""].join("\n");
111+
const expectedError = `Memory Wiki human Notes are missing ${missingMarker}; restore the missing marker before updating or removing this page`;
112+
113+
expect(() => extractHumanNotesBlock(malformed)).toThrow(expectedError);
114+
expect(() => preserveHumanNotesBlock(rendered, malformed)).toThrow(expectedError);
115+
expect(() => preserveHumanNotesBlock(malformed, rendered)).toThrow(expectedError);
116+
},
117+
);
118+
119+
it.each([startMarker, endMarker])(
120+
"ignores a standalone marker inside fenced source content",
121+
(marker) => {
122+
const existing = ["# Source", "", "## Content", "```text", marker, "```", ""].join("\n");
123+
124+
expect(extractHumanNotesBlock(existing)).toBeNull();
125+
expect(preserveHumanNotesBlock(rendered, existing)).toBe(rendered);
126+
},
127+
);
128+
129+
it("preserves marker comments embedded in complete human Notes", () => {
130+
const notes = [
131+
"Before copied markers",
132+
startMarker,
133+
"Between copied markers",
134+
endMarker,
135+
"After copied markers",
136+
].join("\n");
137+
const existing = rendered.replace(
138+
`${startMarker}\n${endMarker}`,
139+
`${startMarker}\n${notes}\n${endMarker}`,
140+
);
141+
142+
expect(extractHumanNotesBlock(existing)).toBe(`${startMarker}\n${notes}\n${endMarker}`);
143+
expect(preserveHumanNotesBlock(rendered, existing)).toBe(existing);
144+
});
145+
146+
it("ignores source-body marker pairs when extracting and preserving actual Notes", () => {
147+
const sourceWithMarkers = [
148+
"# Source",
149+
"",
150+
"## Content",
151+
"```text",
152+
startMarker,
153+
"Generated source annotation",
154+
endMarker,
155+
"```",
156+
"",
157+
"## Notes",
158+
startMarker,
159+
"Durable human annotation",
160+
endMarker,
161+
"",
162+
].join("\n");
163+
164+
expect(extractHumanNotesBlock(sourceWithMarkers)).toBe(
165+
`${startMarker}\nDurable human annotation\n${endMarker}`,
166+
);
167+
expect(preserveHumanNotesBlock(rendered, sourceWithMarkers)).toBe(
168+
rendered.replace(
169+
`${startMarker}\n${endMarker}`,
170+
`${startMarker}\nDurable human annotation\n${endMarker}`,
171+
),
172+
);
173+
});
174+
});
175+
68176
describe("toWikiPageSummary", () => {
69177
it("marks raw and generated source body metadata", () => {
70178
const rawSource = toWikiPageSummary({

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,16 @@ function afterSourceContentFence(page: string): number {
530530
function findNotesHumanBlock(page: string): { start: number; end: number } | null {
531531
const searchFrom = afterSourceContentFence(page);
532532
const start = page.indexOf(HUMAN_START_MARKER, searchFrom);
533-
if (start === -1) {
534-
return null;
535-
}
536533
const endMarker = page.lastIndexOf(HUMAN_END_MARKER);
537-
if (endMarker < start) {
534+
if (start === -1 && endMarker < searchFrom) {
538535
return null;
539536
}
537+
if (start === -1 || endMarker < start) {
538+
const missingMarker = start === -1 ? HUMAN_START_MARKER : HUMAN_END_MARKER;
539+
throw new Error(
540+
`Memory Wiki human Notes are missing ${missingMarker}; restore the missing marker before updating or removing this page`,
541+
);
542+
}
540543
return { start, end: endMarker + HUMAN_END_MARKER.length };
541544
}
542545

‎extensions/memory-wiki/src/source-page-shared.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,72 @@ describe("writeImportedSourcePage", () => {
220220
expect(after).toContain(userNote);
221221
});
222222

223+
it.each([
224+
{ group: "bridge" as const, missingMarker: "opening" as const },
225+
{ group: "bridge" as const, missingMarker: "closing" as const },
226+
{ group: "unsafe-local" as const, missingMarker: "opening" as const },
227+
{ group: "unsafe-local" as const, missingMarker: "closing" as const },
228+
])(
229+
"preserves a $group page and tracked state when its human Notes $missingMarker marker is missing",
230+
async ({ group, missingMarker }) => {
231+
const sourcePath = path.join(suiteRoot, "imported-malformed.txt");
232+
const pagePath = "sources/imported-malformed.md";
233+
const syncKey = `${group}:imported-malformed`;
234+
const state: Parameters<typeof writeImportedSourcePage>[0]["state"] = {
235+
entries: {},
236+
version: 1,
237+
};
238+
const firstSource = "first imported body";
239+
240+
await fs.writeFile(sourcePath, firstSource, "utf8");
241+
await writeImportedSourcePage({
242+
vaultRoot: suiteRoot,
243+
syncKey,
244+
sourcePath,
245+
sourceUpdatedAtMs: Date.UTC(2026, 4, 1),
246+
sourceSize: Buffer.byteLength(firstSource),
247+
renderFingerprint: "fp-1",
248+
pagePath,
249+
group,
250+
state,
251+
buildRendered: buildSourcePage,
252+
});
253+
254+
const absPage = path.join(suiteRoot, pagePath);
255+
const userNote = `DURABLE ${group.toUpperCase()} ANNOTATION`;
256+
const malformedNotes =
257+
missingMarker === "opening"
258+
? `${userNote}\n<!-- openclaw:human:end -->`
259+
: `<!-- openclaw:human:start -->\n${userNote}`;
260+
const existing = (await fs.readFile(absPage, "utf8")).replace(
261+
"<!-- openclaw:human:start -->\n<!-- openclaw:human:end -->",
262+
malformedNotes,
263+
);
264+
await fs.writeFile(absPage, existing, "utf8");
265+
const previousState = structuredClone(state);
266+
const secondSource = "second imported body must not overwrite human Notes";
267+
await fs.writeFile(sourcePath, secondSource, "utf8");
268+
269+
await expect(
270+
writeImportedSourcePage({
271+
vaultRoot: suiteRoot,
272+
syncKey,
273+
sourcePath,
274+
sourceUpdatedAtMs: Date.UTC(2026, 4, 2),
275+
sourceSize: Buffer.byteLength(secondSource),
276+
renderFingerprint: "fp-2",
277+
pagePath,
278+
group,
279+
state,
280+
buildRendered: buildSourcePage,
281+
}),
282+
).rejects.toThrow(/human Notes.*missing|missing.*human Notes|openclaw:human:(start|end)/i);
283+
284+
await expect(fs.readFile(absPage, "utf8")).resolves.toBe(existing);
285+
expect(state).toEqual(previousState);
286+
},
287+
);
288+
223289
it("preserves CRLF human notes without copying marker comments from existing imported content", async () => {
224290
const sourcePath = path.join(suiteRoot, "imported-crlf.txt");
225291
const pagePath = "sources/imported-crlf.md";

0 commit comments

Comments
 (0)