Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(memory): der Markdown-Rundlauf verliert mehrzeiligen Inhalt nicht mehr

purplef0a4cafc958b
diff --git a/src/stores/__tests__/memory-markdown-roundtrip.test.ts b/src/stores/__tests__/memory-markdown-roundtrip.test.ts
new file mode 100644
index 00000000..c6c4fd6a
--- /dev/null
+++ b/src/stores/__tests__/memory-markdown-roundtrip.test.ts
@@ -0,0 +1,74 @@
+/**
+ * R2-25 (Logikkontrolle) / J (3.0.1-Liste): the Markdown export/import round
+ * trip lost everything past a memory's first line — the rest of a multi-line
+ * content, the source and the date — because the importer reads the export
+ * one physical line at a time and a real line break inside `entry.content`
+ * split ONE list item across several lines. See the comment on
+ * `escapeMdContent` in `../memoryStore.ts` for the full mechanism.
+ *
+ * Run: npx vitest run src/stores/__tests__/memory-markdown-roundtrip.test.ts
+ */
+import { describe, it, expect, beforeEach } from 'vitest'
+import { useMemoryStore } from '../memoryStore'
+
+function reset() {
+ useMemoryStore.setState({
+ entries: [], localEntries: [], accountCollections: {},
+ memorySyncBaselines: {}, memorySyncPending: {}, activeMemoryOwner: null,
+ memoryCollectionRevision: 0, lastSynced: 0,
+ })
+}
+
+describe('memory markdown round trip — multi-line content', () => {
+ beforeEach(reset)
+
+ it('a multi-line content survives export + import unchanged, with source and date', () => {
+ const multiline = 'Erste Zeile.\nZweite Zeile mit Details.\nDritte Zeile, Abschluss.'
+ useMemoryStore.getState().addMemory({
+ type: 'project',
+ title: 'Mehrzeiliger Eintrag',
+ description: multiline.slice(0, 120),
+ content: multiline,
+ tags: ['t1'],
+ source: 'unit-test',
+ })
+
+ const md = useMemoryStore.getState().exportAsMarkdown()
+ reset()
+ const result = useMemoryStore.getState().importFromMarkdown(md)
+
+ expect(result.added).toBe(1)
+ const back = useMemoryStore.getState().entries[0]
+ expect(back.content).toBe(multiline)
+ expect(back.title).toBe('Mehrzeiliger Eintrag')
+ expect(back.source).toBe('unit-test')
+ // The date round-trips too — a lost date falls back to "now" (today),
+ // which a same-day test cannot tell apart from a real bug. Assert the
+ // export actually carries a source/date suffix on the item's own line
+ // instead, which is the thing that was silently dropped before the fix.
+ expect(md).toMatch(/\*\(unit-test\)\*, \d{4}-\d{2}-\d{2}/)
+ })
+
+ it('a content with a literal backslash-n survives too, not mistaken for a line break', () => {
+ const literal = 'the path is C:\\nope\\here'
+ useMemoryStore.getState().addMemory({
+ type: 'project', title: 'Backslash', description: literal, content: literal,
+ tags: [], source: 'unit-test',
+ })
+ const md = useMemoryStore.getState().exportAsMarkdown()
+ reset()
+ useMemoryStore.getState().importFromMarkdown(md)
+ expect(useMemoryStore.getState().entries[0].content).toBe(literal)
+ })
+
+ it('the export is exactly one physical line per entry, even for multi-line content', () => {
+ const multiline = 'a\nb\nc'
+ useMemoryStore.getState().addMemory({
+ type: 'user', title: 'Line count', description: multiline, content: multiline,
+ tags: [], source: 'unit-test',
+ })
+ const md = useMemoryStore.getState().exportAsMarkdown()
+ const itemLines = md.split('\n').filter((l) => l.startsWith('- '))
+ expect(itemLines).toHaveLength(1)
+ })
+})
diff --git a/src/stores/memoryStore.ts b/src/stores/memoryStore.ts
index 0169f52e..89db5054 100644
--- a/src/stores/memoryStore.ts
+++ b/src/stores/memoryStore.ts
@@ -568,6 +568,31 @@ export function migrateMemoryState(persistedState: unknown, version: number): Me
const MD_ITEM =
/^-\s+(?:\*\*(.+?)\*\*\s*(?:,|[\u2013\u2014])\s*)?(.+?)(?:(?:\s+\[([^\]]*)\])?\s+\*\(([^)]+)\)\*(?:\s*(?:,|[\u2013\u2014])\s*(.+?))?)?$/
+/**
+ * R2-25 (Logikkontrolle, 3.0.1): a multi-line memory (several paragraphs, a
+ * pasted snippet with its own line breaks) went into `exportAsMarkdown` as
+ * literal newline CHARACTERS inside `entry.content`. Written straight into
+ * the file, that turned ONE list item into several physical lines: the first
+ * kept the `- **Title**,` prefix, the middle ones had no `- ` prefix at all,
+ * and the LAST one carried `*(source)*, date` but no leading dash. `importFromMarkdown`
+ * scans line by line with `MD_ITEM`'s `^...$` anchors, so only the first line
+ * matched anything \u2014 the rest of the content, the source and the date were
+ * silently dropped, not just truncated.
+ *
+ * The fix keeps every memory to exactly one physical line in the export,
+ * which is what the whole per-line importer assumes. A real line break in the
+ * content becomes the two-character escape `\n`; a literal backslash the
+ * content already contained is doubled first so it can never be misread as
+ * the start of that escape. `unescapeMdContent` reverses both in one pass.
+ */
+function escapeMdContent(s: string): string {
+ return s.replace(/\\/g, '\\\\').replace(/\n/g, '\\n')
+}
+
+function unescapeMdContent(s: string): string {
+ return s.replace(/\\\\|\\n/g, (m) => (m === '\\n' ? '\n' : '\\'))
+}
+
/**
* The date the export writes: `YYYY-MM-DD`, not a locale string.
*
@@ -988,7 +1013,12 @@ export const useMemoryStore = create<MemoryState>()(
md += `## ${typeTitles[type]}\n\n`
for (const entry of typeEntries) {
const date = isoTag(entry.updatedAt)
- md += `- **${entry.title}**, ${entry.content}`
+ // R2-25: escaped so a multi-line entry stays ONE physical line —
+ // see escapeMdContent. The bracket-ending check below still reads
+ // the RAW content: `\n`-escaping never adds or removes a
+ // trailing `]`, and checking the escaped form would be the same
+ // answer read through an extra step.
+ md += `- **${entry.title}**, ${escapeMdContent(entry.content)}`
if (entry.tags.length > 0) md += ` [${entry.tags.join(', ')}]`
// A content that ends in a bracket group would otherwise read back
// as a tag list on import. The empty group occupies the tag slot,
@@ -1026,7 +1056,10 @@ export const useMemoryStore = create<MemoryState>()(
const itemMatch = line.match(MD_ITEM)
if (itemMatch) {
const title = itemMatch[1] || itemMatch[2].substring(0, 60)
- const content = itemMatch[2].trim()
+ // R2-25: undo escapeMdContent's `\n`/backslash escaping so a
+ // multi-line memory comes back with its real line breaks instead
+ // of the literal two-character escape.
+ const content = unescapeMdContent(itemMatch[2].trim())
const tags = itemMatch[3] ? itemMatch[3].split(',').map(t => t.trim()).filter(Boolean) : []
const source = itemMatch[4] || 'import'
const stand = isoBack(itemMatch[5]) ?? Date.now()