@modusensus/dsh-mneme 0.6.7 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/.github/workflows/test.yml +32 -0
  2. package/.release-notes-v0.6.9.md +13 -0
  3. package/CHANGELOG.md +89 -0
  4. package/LICENSE +21 -21
  5. package/README.md +219 -463
  6. package/SECURITY.md +544 -0
  7. package/docs/devlog/2026-08-14-dsh-mneme-dev-log.md +247 -0
  8. package/docs/devlog/2026-08-15-dsh-mneme-audit-stress-dev-log.md +145 -0
  9. package/docs/devlog/2026-08-15-dsh-mneme-pipeline-dev-log.md +56 -0
  10. package/docs/devlog/2026-08-15-dsh-mneme-reflection-dev-log.md +77 -0
  11. package/docs/devlog/2026-08-15-dsh-mneme-review-fixes-dev-log.md +64 -0
  12. package/docs/devlog/2026-08-15-dsh-mneme-semantic-dev-log.md +90 -0
  13. package/dsh-mneme/CHANGELOG.md +248 -0
  14. package/dsh-mneme/LICENSE +21 -0
  15. package/dsh-mneme/README.md +465 -0
  16. package/{cordis.patch.yml → dsh-mneme/cordis.patch.yml} +15 -15
  17. package/dsh-mneme/docs/AGENT_MEMORY_RESEARCH.md +183 -0
  18. package/dsh-mneme/docs/ENTITIES.md +245 -0
  19. package/dsh-mneme/docs/LOCAL_MODEL.md +141 -0
  20. package/dsh-mneme/docs/MIGRATION.md +127 -0
  21. package/dsh-mneme/docs/SEMANTIC.md +256 -0
  22. package/dsh-mneme/docs/SLEEP.md +163 -0
  23. package/{lib → dsh-mneme/lib}/api.js +783 -783
  24. package/{lib → dsh-mneme/lib}/client.js +1757 -1757
  25. package/{src → dsh-mneme/lib}/commands.js +64 -64
  26. package/{lib → dsh-mneme/lib}/config.js +298 -288
  27. package/{lib → dsh-mneme/lib}/dream/clustering.js +118 -118
  28. package/{src → dsh-mneme/lib}/dream/decisions.js +488 -439
  29. package/{lib → dsh-mneme/lib}/dream/sleep.js +561 -554
  30. package/{src → dsh-mneme/lib}/dream/tag-extractor.js +156 -156
  31. package/{lib → dsh-mneme/lib}/dream.js +958 -929
  32. package/{src → dsh-mneme/lib}/embedding.js +154 -154
  33. package/{src → dsh-mneme/lib}/entities/extractor.js +242 -242
  34. package/{src → dsh-mneme/lib}/hot-memory.js +53 -53
  35. package/{lib → dsh-mneme/lib}/index.js +361 -361
  36. package/{src → dsh-mneme/lib}/inject.js +208 -208
  37. package/{lib → dsh-mneme/lib}/local-embedder.js +282 -282
  38. package/{lib → dsh-mneme/lib}/mirror.js +170 -170
  39. package/{lib → dsh-mneme/lib}/parser/tag.js +59 -59
  40. package/{lib → dsh-mneme/lib}/parser/wiki-link.js +38 -38
  41. package/{src → dsh-mneme/lib}/quality-filter.js +123 -123
  42. package/{lib → dsh-mneme/lib}/reranker.js +218 -218
  43. package/{lib → dsh-mneme/lib}/search/adaptive.js +22 -22
  44. package/{lib → dsh-mneme/lib}/search/bm25.js +96 -96
  45. package/{src → dsh-mneme/lib}/search/tag-boost.js +61 -61
  46. package/{src → dsh-mneme/lib}/service.js +1726 -1726
  47. package/{lib → dsh-mneme/lib}/settings.js +172 -172
  48. package/{lib → dsh-mneme/lib}/store.js +2238 -2238
  49. package/{lib → dsh-mneme/lib}/summarize.js +236 -236
  50. package/{lib → dsh-mneme/lib}/tools.js +290 -290
  51. package/{lib → dsh-mneme/lib}/vector-index.js +116 -116
  52. package/dsh-mneme/package-lock.json +1936 -0
  53. package/dsh-mneme/package.json +80 -0
  54. package/{scripts → dsh-mneme/scripts}/benchmark-embed.js +201 -201
  55. package/{scripts → dsh-mneme/scripts}/benchmark-recall.js +133 -133
  56. package/{scripts → dsh-mneme/scripts}/benchmark-rerank.js +166 -166
  57. package/{scripts → dsh-mneme/scripts}/e2e-dsh.js +218 -218
  58. package/{scripts → dsh-mneme/scripts}/stress-dsh.js +255 -255
  59. package/{scripts → dsh-mneme/scripts}/sync-lib.js +52 -52
  60. package/{src → dsh-mneme/src}/api.js +783 -783
  61. package/{lib → dsh-mneme/src}/commands.js +64 -64
  62. package/{src → dsh-mneme/src}/config.js +298 -288
  63. package/{src → dsh-mneme/src}/dream/clustering.js +118 -118
  64. package/{lib → dsh-mneme/src}/dream/decisions.js +488 -439
  65. package/{src → dsh-mneme/src}/dream/sleep.js +561 -554
  66. package/{lib → dsh-mneme/src}/dream/tag-extractor.js +156 -156
  67. package/{src → dsh-mneme/src}/dream.js +958 -929
  68. package/{lib → dsh-mneme/src}/embedding.js +154 -154
  69. package/{lib → dsh-mneme/src}/entities/extractor.js +242 -242
  70. package/{lib → dsh-mneme/src}/hot-memory.js +53 -53
  71. package/{src → dsh-mneme/src}/index.js +361 -361
  72. package/{lib → dsh-mneme/src}/inject.js +208 -208
  73. package/{src → dsh-mneme/src}/local-embedder.js +282 -282
  74. package/{src → dsh-mneme/src}/mirror.js +170 -170
  75. package/{src → dsh-mneme/src}/parser/tag.js +59 -59
  76. package/{src → dsh-mneme/src}/parser/wiki-link.js +38 -38
  77. package/{lib → dsh-mneme/src}/quality-filter.js +123 -123
  78. package/{src → dsh-mneme/src}/reranker.js +218 -218
  79. package/{src → dsh-mneme/src}/search/adaptive.js +22 -22
  80. package/{src → dsh-mneme/src}/search/bm25.js +96 -96
  81. package/{lib → dsh-mneme/src}/search/tag-boost.js +61 -61
  82. package/{lib → dsh-mneme/src}/service.js +1726 -1726
  83. package/{src → dsh-mneme/src}/settings.js +172 -172
  84. package/{src → dsh-mneme/src}/store.js +2238 -2238
  85. package/{src → dsh-mneme/src}/summarize.js +236 -236
  86. package/{src → dsh-mneme/src}/tools.js +290 -290
  87. package/{src → dsh-mneme/src}/vector-index.js +116 -116
  88. package/{test → dsh-mneme/test}/api.test.js +594 -594
  89. package/{test → dsh-mneme/test}/audit.test.js +448 -448
  90. package/{test → dsh-mneme/test}/benchmark.test.js +35 -35
  91. package/{test → dsh-mneme/test}/boundary-v0625.test.js +82 -82
  92. package/{test → dsh-mneme/test}/client.test.js +368 -368
  93. package/{test → dsh-mneme/test}/clustering.test.js +100 -100
  94. package/{test → dsh-mneme/test}/commands.test.js +69 -69
  95. package/{test → dsh-mneme/test}/config.test.js +50 -50
  96. package/{test → dsh-mneme/test}/conflict-freeze.test.js +290 -290
  97. package/{test → dsh-mneme/test}/directory.test.js +134 -134
  98. package/{test → dsh-mneme/test}/dream.test.js +1060 -901
  99. package/{test → dsh-mneme/test}/entities.test.js +522 -522
  100. package/{test → dsh-mneme/test}/epistemic.test.js +298 -298
  101. package/{test → dsh-mneme/test}/fnew-0112.test.js +311 -311
  102. package/{test → dsh-mneme/test}/fnew-03.test.js +422 -422
  103. package/{test → dsh-mneme/test}/graph-api.test.js +175 -175
  104. package/{test → dsh-mneme/test}/helpers/dream-mock.js +82 -82
  105. package/{test → dsh-mneme/test}/hot-memory.test.js +174 -174
  106. package/{test → dsh-mneme/test}/inject.test.js +103 -103
  107. package/{test → dsh-mneme/test}/llm-audit.test.js +279 -279
  108. package/{test → dsh-mneme/test}/local-embedder.test.js +227 -227
  109. package/{test → dsh-mneme/test}/mirror-dirty.test.js +424 -424
  110. package/{test → dsh-mneme/test}/mirror-edit-digest.test.js +187 -187
  111. package/{test → dsh-mneme/test}/mirror-generation.test.js +499 -499
  112. package/{test → dsh-mneme/test}/mirror.test.js +249 -249
  113. package/{test → dsh-mneme/test}/normalize-decisions.test.js +120 -120
  114. package/{test → dsh-mneme/test}/peer-blockers.test.js +190 -190
  115. package/{test → dsh-mneme/test}/policy-epoch.test.js +259 -259
  116. package/{test → dsh-mneme/test}/provenance.test.js +103 -103
  117. package/{test → dsh-mneme/test}/quality-filter.test.js +118 -118
  118. package/{test → dsh-mneme/test}/reasoning-effort.test.js +199 -199
  119. package/{test → dsh-mneme/test}/recall-evals.test.js +235 -235
  120. package/{test → dsh-mneme/test}/recall-layer.test.js +315 -315
  121. package/{test → dsh-mneme/test}/receipt-chain.test.js +451 -451
  122. package/{test → dsh-mneme/test}/reflection.test.js +226 -226
  123. package/{test → dsh-mneme/test}/reranker.test.js +240 -240
  124. package/{test → dsh-mneme/test}/search-fusion.test.js +90 -90
  125. package/{test → dsh-mneme/test}/semantic.test.js +124 -124
  126. package/{test → dsh-mneme/test}/service-search.test.js +199 -199
  127. package/{test → dsh-mneme/test}/service.test.js +435 -435
  128. package/{test → dsh-mneme/test}/settings.test.js +118 -118
  129. package/{test → dsh-mneme/test}/sleep.test.js +365 -365
  130. package/{test → dsh-mneme/test}/store.test.js +436 -436
  131. package/{test → dsh-mneme/test}/stress.test.js +209 -209
  132. package/{test → dsh-mneme/test}/summarize.test.js +191 -191
  133. package/{test → dsh-mneme/test}/tag-boost.test.js +125 -125
  134. package/{test → dsh-mneme/test}/tag.test.js +312 -312
  135. package/{test → dsh-mneme/test}/tools.test.js +285 -285
  136. package/{test → dsh-mneme/test}/vector-index.test.js +221 -221
  137. package/{test → dsh-mneme/test}/wiki-link.test.js +332 -332
  138. package/package.json +18 -40
  139. package//346/250/252/345/271/205.png +0 -0
@@ -1,170 +1,170 @@
1
- import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
2
- import { createHash } from "node:crypto";
3
- import { join } from "node:path";
4
-
5
- export const TYPE_FILE = {
6
- preference: "preferences.md",
7
- project: "projects.md",
8
- decision: "decisions.md",
9
- history: "history.md",
10
- summary: "summary.md"
11
- };
12
-
13
- const ESCAPE = /([\\`*_[\]{}()#+.!|>~-])/g;
14
- const UNESCAPE = new RegExp("\\\\" + ESCAPE.source, "g");
15
-
16
- function esc(text) {
17
- return String(text).replace(ESCAPE, "\\$1");
18
- }
19
-
20
- function unescape(text) {
21
- return String(text).replace(UNESCAPE, "$1");
22
- }
23
-
24
- function renderMemory(m) {
25
- // last-rendered digest baseline: sha256(title \x00 content). service.js
26
- // compares the file hash against this to tell "untouched by a human" (machine
27
- // write wins) apart from a real human edit, so a not-yet-re-rendered store
28
- // update is not misread as a concurrent human edit.
29
- const digest = createHash("sha256")
30
- .update(`${m.title}\x00${m.content}`)
31
- .digest("hex");
32
- const lines = [];
33
- lines.push(`## ${esc(m.title)}`);
34
- // v0.6.2 tag line: entity_attrs-backed tags (attached by the service as
35
- // `entityTags`) rendered as `#tag` space-separated under the title. No tags
36
- // → no line. Legacy `- **标签**:` metadata below keeps the memories.tags
37
- // column (still written by save/update) readable.
38
- const entityTags = Array.isArray(m.entityTags) ? m.entityTags : [];
39
- if (entityTags.length) {
40
- lines.push("");
41
- lines.push(entityTags.map((t) => `#${esc(t)}`).join(" "));
42
- }
43
- lines.push("");
44
- lines.push(`- **ID**: \`${m.id}\``);
45
- lines.push(`- **类型**: ${m.type}`);
46
- lines.push(`- **重要性**: ${m.importance}`);
47
- lines.push(`- **标签**: ${m.tags.map((t) => `\`${esc(t)}\``).join(" ")}`);
48
- lines.push(`- **更新时间**: ${m.updated_at}`);
49
- if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
50
- lines.push("");
51
- lines.push(`<!-- mirror-digest: ${digest} -->`);
52
- lines.push(m.content);
53
- lines.push("");
54
- lines.push("---");
55
- lines.push("");
56
- return lines.join("\n");
57
- }
58
-
59
- export function createMirror(dir) {
60
- mkdirSync(dir, { recursive: true });
61
-
62
- function filePath(type) {
63
- const name = TYPE_FILE[type];
64
- return name ? join(dir, name) : undefined;
65
- }
66
-
67
- /**
68
- * Parse a mirror file back into {id, title, content} entries for human edits.
69
- * Entries are anchored on "- **ID**: `...`" lines that are followed by the
70
- * "- **类型**:" metadata line (structural entry head): each entry's block
71
- * spans from its ID line up to the next ID line (or end of file). The block
72
- * head (the ID line plus the generated metadata run) and the trailing
73
- * structural "---" separator are stripped; everything in between is the entry
74
- * body, so user content containing "---", metadata-like lines, or even a
75
- * machine-format "- **ID**: `x`" line is preserved. The title is the "## "
76
- * heading preceding the ID line.
77
- */
78
- function readHumanEdits(type = undefined) {
79
- const types = type ? [type] : Object.keys(TYPE_FILE);
80
- const edits = [];
81
- for (const t of types) {
82
- const file = filePath(t);
83
- if (!file || !existsSync(file)) continue;
84
- const text = readFileSync(file, "utf8").replace(/\r\n/g, "\n");
85
- // Anchor on the ID line only when it is a structural entry head: the
86
- // machine-rendered ID line is always followed by the "- **类型**:" line.
87
- // A body line like "- **ID**: `x`" is not, so it never splits the block
88
- // or produces a ghost entry.
89
- const anchors = [...text.matchAll(/^- \*\*ID\*\*: `([^`]+)`\n- \*\*类型\*\*:/gm)];
90
- let prevEnd = 0;
91
- for (let i = 0; i < anchors.length; i++) {
92
- const anchor = anchors[i];
93
- const blockStart = anchor.index;
94
- const blockEnd = i + 1 < anchors.length ? anchors[i + 1].index : text.length;
95
-
96
- // Title: last "## " heading before this ID line (file header region /
97
- // previous block tail). Body headings of earlier entries come before
98
- // the structural "---" + "## " of this entry, so the last match wins.
99
- const titleMatches = [...text.slice(prevEnd, blockStart).matchAll(/^## (.+)$/gm)];
100
- const titleMatch = titleMatches[titleMatches.length - 1];
101
-
102
- // Body: the ID line and the generated metadata run are structural head;
103
- // everything after them up to the trailing "---" separator is the body.
104
- let body = text
105
- .slice(blockStart, blockEnd)
106
- .replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
107
- .replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
108
- .replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
109
- const separators = [...body.matchAll(/^---\s*$/gm)];
110
- const lastSep = separators[separators.length - 1];
111
- if (lastSep) body = body.slice(0, lastSep.index);
112
- body = body.trim();
113
-
114
- // The machine-written "更新时间" line records the store's updated_at at
115
- // render time — the version token for detecting a concurrent store write
116
- // during a three-way merge of human edits (see service.syncMirror).
117
- const block = text.slice(blockStart, blockEnd);
118
- const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
119
- const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
120
- edits.push({
121
- id: anchor[1],
122
- title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
123
- content: body,
124
- updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
125
- digest: digestMatch ? digestMatch[1] : undefined
126
- });
127
-
128
- const lineEnd = text.indexOf("\n", blockStart);
129
- prevEnd = lineEnd === -1 ? text.length : lineEnd + 1;
130
- }
131
- }
132
- return edits;
133
- }
134
-
135
- function sync(memories) {
136
- const byType = {};
137
- for (const m of memories) {
138
- (byType[m.type] ??= []).push(m);
139
- }
140
- // Per-type physical outcomes (audit peer D): a failed write for one type
141
- // must not abort the whole render. Each type is written (or pruned) in its
142
- // own try/catch and the result reported so the caller can persist per-type
143
- // committed/failed receipts — a file that was already written is a real
144
- // physical commit even when a sibling type errors.
145
- const results = {};
146
- for (const type of Object.keys(TYPE_FILE)) {
147
- try {
148
- const file = filePath(type);
149
- const items = (byType[type] ?? [])
150
- .slice()
151
- .sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1));
152
- if (items.length === 0) {
153
- // no memories of this type: drop any stale mirror file so deleted
154
- // memories do not "resurrect" via readHumanEdits
155
- rmSync(file, { force: true });
156
- } else {
157
- const header = `# ${TYPE_FILE[type]} — dsh-mneme 镜像\n\n<!-- 手工编辑此文件会被合并回记忆库(人工优先)。 -->\n\n`;
158
- const body = items.map(renderMemory).join("\n");
159
- writeFileSync(file, header + body, "utf8");
160
- }
161
- results[type] = { ok: true };
162
- } catch (error) {
163
- results[type] = { ok: false, error: error?.message ?? String(error) };
164
- }
165
- }
166
- return results;
167
- }
168
-
169
- return { filePath, sync, readHumanEdits };
170
- }
1
+ import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
2
+ import { createHash } from "node:crypto";
3
+ import { join } from "node:path";
4
+
5
+ export const TYPE_FILE = {
6
+ preference: "preferences.md",
7
+ project: "projects.md",
8
+ decision: "decisions.md",
9
+ history: "history.md",
10
+ summary: "summary.md"
11
+ };
12
+
13
+ const ESCAPE = /([\\`*_[\]{}()#+.!|>~-])/g;
14
+ const UNESCAPE = new RegExp("\\\\" + ESCAPE.source, "g");
15
+
16
+ function esc(text) {
17
+ return String(text).replace(ESCAPE, "\\$1");
18
+ }
19
+
20
+ function unescape(text) {
21
+ return String(text).replace(UNESCAPE, "$1");
22
+ }
23
+
24
+ function renderMemory(m) {
25
+ // last-rendered digest baseline: sha256(title \x00 content). service.js
26
+ // compares the file hash against this to tell "untouched by a human" (machine
27
+ // write wins) apart from a real human edit, so a not-yet-re-rendered store
28
+ // update is not misread as a concurrent human edit.
29
+ const digest = createHash("sha256")
30
+ .update(`${m.title}\x00${m.content}`)
31
+ .digest("hex");
32
+ const lines = [];
33
+ lines.push(`## ${esc(m.title)}`);
34
+ // v0.6.2 tag line: entity_attrs-backed tags (attached by the service as
35
+ // `entityTags`) rendered as `#tag` space-separated under the title. No tags
36
+ // → no line. Legacy `- **标签**:` metadata below keeps the memories.tags
37
+ // column (still written by save/update) readable.
38
+ const entityTags = Array.isArray(m.entityTags) ? m.entityTags : [];
39
+ if (entityTags.length) {
40
+ lines.push("");
41
+ lines.push(entityTags.map((t) => `#${esc(t)}`).join(" "));
42
+ }
43
+ lines.push("");
44
+ lines.push(`- **ID**: \`${m.id}\``);
45
+ lines.push(`- **类型**: ${m.type}`);
46
+ lines.push(`- **重要性**: ${m.importance}`);
47
+ lines.push(`- **标签**: ${m.tags.map((t) => `\`${esc(t)}\``).join(" ")}`);
48
+ lines.push(`- **更新时间**: ${m.updated_at}`);
49
+ if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
50
+ lines.push("");
51
+ lines.push(`<!-- mirror-digest: ${digest} -->`);
52
+ lines.push(m.content);
53
+ lines.push("");
54
+ lines.push("---");
55
+ lines.push("");
56
+ return lines.join("\n");
57
+ }
58
+
59
+ export function createMirror(dir) {
60
+ mkdirSync(dir, { recursive: true });
61
+
62
+ function filePath(type) {
63
+ const name = TYPE_FILE[type];
64
+ return name ? join(dir, name) : undefined;
65
+ }
66
+
67
+ /**
68
+ * Parse a mirror file back into {id, title, content} entries for human edits.
69
+ * Entries are anchored on "- **ID**: `...`" lines that are followed by the
70
+ * "- **类型**:" metadata line (structural entry head): each entry's block
71
+ * spans from its ID line up to the next ID line (or end of file). The block
72
+ * head (the ID line plus the generated metadata run) and the trailing
73
+ * structural "---" separator are stripped; everything in between is the entry
74
+ * body, so user content containing "---", metadata-like lines, or even a
75
+ * machine-format "- **ID**: `x`" line is preserved. The title is the "## "
76
+ * heading preceding the ID line.
77
+ */
78
+ function readHumanEdits(type = undefined) {
79
+ const types = type ? [type] : Object.keys(TYPE_FILE);
80
+ const edits = [];
81
+ for (const t of types) {
82
+ const file = filePath(t);
83
+ if (!file || !existsSync(file)) continue;
84
+ const text = readFileSync(file, "utf8").replace(/\r\n/g, "\n");
85
+ // Anchor on the ID line only when it is a structural entry head: the
86
+ // machine-rendered ID line is always followed by the "- **类型**:" line.
87
+ // A body line like "- **ID**: `x`" is not, so it never splits the block
88
+ // or produces a ghost entry.
89
+ const anchors = [...text.matchAll(/^- \*\*ID\*\*: `([^`]+)`\n- \*\*类型\*\*:/gm)];
90
+ let prevEnd = 0;
91
+ for (let i = 0; i < anchors.length; i++) {
92
+ const anchor = anchors[i];
93
+ const blockStart = anchor.index;
94
+ const blockEnd = i + 1 < anchors.length ? anchors[i + 1].index : text.length;
95
+
96
+ // Title: last "## " heading before this ID line (file header region /
97
+ // previous block tail). Body headings of earlier entries come before
98
+ // the structural "---" + "## " of this entry, so the last match wins.
99
+ const titleMatches = [...text.slice(prevEnd, blockStart).matchAll(/^## (.+)$/gm)];
100
+ const titleMatch = titleMatches[titleMatches.length - 1];
101
+
102
+ // Body: the ID line and the generated metadata run are structural head;
103
+ // everything after them up to the trailing "---" separator is the body.
104
+ let body = text
105
+ .slice(blockStart, blockEnd)
106
+ .replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
107
+ .replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
108
+ .replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
109
+ const separators = [...body.matchAll(/^---\s*$/gm)];
110
+ const lastSep = separators[separators.length - 1];
111
+ if (lastSep) body = body.slice(0, lastSep.index);
112
+ body = body.trim();
113
+
114
+ // The machine-written "更新时间" line records the store's updated_at at
115
+ // render time — the version token for detecting a concurrent store write
116
+ // during a three-way merge of human edits (see service.syncMirror).
117
+ const block = text.slice(blockStart, blockEnd);
118
+ const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
119
+ const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
120
+ edits.push({
121
+ id: anchor[1],
122
+ title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
123
+ content: body,
124
+ updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
125
+ digest: digestMatch ? digestMatch[1] : undefined
126
+ });
127
+
128
+ const lineEnd = text.indexOf("\n", blockStart);
129
+ prevEnd = lineEnd === -1 ? text.length : lineEnd + 1;
130
+ }
131
+ }
132
+ return edits;
133
+ }
134
+
135
+ function sync(memories) {
136
+ const byType = {};
137
+ for (const m of memories) {
138
+ (byType[m.type] ??= []).push(m);
139
+ }
140
+ // Per-type physical outcomes (audit peer D): a failed write for one type
141
+ // must not abort the whole render. Each type is written (or pruned) in its
142
+ // own try/catch and the result reported so the caller can persist per-type
143
+ // committed/failed receipts — a file that was already written is a real
144
+ // physical commit even when a sibling type errors.
145
+ const results = {};
146
+ for (const type of Object.keys(TYPE_FILE)) {
147
+ try {
148
+ const file = filePath(type);
149
+ const items = (byType[type] ?? [])
150
+ .slice()
151
+ .sort((a, b) => (a.updated_at < b.updated_at ? 1 : -1));
152
+ if (items.length === 0) {
153
+ // no memories of this type: drop any stale mirror file so deleted
154
+ // memories do not "resurrect" via readHumanEdits
155
+ rmSync(file, { force: true });
156
+ } else {
157
+ const header = `# ${TYPE_FILE[type]} — dsh-mneme 镜像\n\n<!-- 手工编辑此文件会被合并回记忆库(人工优先)。 -->\n\n`;
158
+ const body = items.map(renderMemory).join("\n");
159
+ writeFileSync(file, header + body, "utf8");
160
+ }
161
+ results[type] = { ok: true };
162
+ } catch (error) {
163
+ results[type] = { ok: false, error: error?.message ?? String(error) };
164
+ }
165
+ }
166
+ return results;
167
+ }
168
+
169
+ return { filePath, sync, readHumanEdits };
170
+ }
@@ -1,59 +1,59 @@
1
- /**
2
- * Tag parser (v0.6.2). Given a memory's content, extract explicit hashtags of
3
- * the form `#标签`. The allowed character set is `[a-zA-Z0-9_一-龥-]+` (ASCII
4
- * letters/digits/underscore, CJK characters, and hyphen), so `#linux`,
5
- * `#deepseek` and `#考研` are all valid tags.
6
- *
7
- * Rules:
8
- * - multiple tags on one line are all extracted, in source order;
9
- * - duplicates collapse to the first occurrence;
10
- * - tags longer than MAX_TAG_LENGTH (20) characters are dropped as illegal;
11
- * - edge hyphens (leading/trailing, e.g. a sentence-terminating `#tag-`) are
12
- * stripped; a tag that collapses to empty is dropped;
13
- * - markdown `## heading` never matches (the second `#`/space is not in the
14
- * allowed set).
15
- *
16
- * Pure module: no store, no side effects — persistence (store.setMemoryTags)
17
- * is a separate step. sanitizeTags shares the same validation so LLM-extracted
18
- * tag arrays (autoDream tag-extractor) are filtered identically.
19
- */
20
-
21
- export const MAX_TAG_LENGTH = 20;
22
-
23
- // `一` (U+4E00) .. `龥` (U+9FA5) is the CJK Unified Ideographs block (the
24
- // rule's `一-龥`). Matching requires a tag character right after `#` so a bare
25
- // `#` and markdown `## heading` are naturally excluded (#/space not in set).
26
- const TAG_RE = /#[a-zA-Z0-9_一-龥-]+/g;
27
- const EDGE_HYPHEN = /^-+|-+$/g;
28
- const VALID_TAG = /^[a-zA-Z0-9_一-龥-]+$/;
29
-
30
- /** Validate + dedupe an array of tag strings. Non-strings, blanks, over-long
31
- * and illegal-character tags are dropped (fail-safe). A leading `#` is
32
- * tolerated so raw LLM output can be passed through directly. */
33
- export function sanitizeTags(tags) {
34
- if (!Array.isArray(tags)) return [];
35
- const seen = new Set();
36
- const out = [];
37
- for (const t of tags) {
38
- if (typeof t !== "string") continue;
39
- let tag = t.trim();
40
- if (tag.startsWith("#")) tag = tag.slice(1);
41
- tag = tag.replace(EDGE_HYPHEN, "");
42
- if (!tag || tag.length > MAX_TAG_LENGTH) continue;
43
- if (!VALID_TAG.test(tag)) continue;
44
- if (seen.has(tag)) continue; // 去重(保留首次出现顺序)
45
- seen.add(tag);
46
- out.push(tag);
47
- }
48
- return out;
49
- }
50
-
51
- /** @returns {string[]} validated tags in source order, deduplicated. */
52
- export function parseTags(content) {
53
- if (typeof content !== "string" || content.length === 0) return [];
54
- const raw = [];
55
- TAG_RE.lastIndex = 0;
56
- let m;
57
- while ((m = TAG_RE.exec(content)) !== null) raw.push(m[0]);
58
- return sanitizeTags(raw);
59
- }
1
+ /**
2
+ * Tag parser (v0.6.2). Given a memory's content, extract explicit hashtags of
3
+ * the form `#标签`. The allowed character set is `[a-zA-Z0-9_一-龥-]+` (ASCII
4
+ * letters/digits/underscore, CJK characters, and hyphen), so `#linux`,
5
+ * `#deepseek` and `#考研` are all valid tags.
6
+ *
7
+ * Rules:
8
+ * - multiple tags on one line are all extracted, in source order;
9
+ * - duplicates collapse to the first occurrence;
10
+ * - tags longer than MAX_TAG_LENGTH (20) characters are dropped as illegal;
11
+ * - edge hyphens (leading/trailing, e.g. a sentence-terminating `#tag-`) are
12
+ * stripped; a tag that collapses to empty is dropped;
13
+ * - markdown `## heading` never matches (the second `#`/space is not in the
14
+ * allowed set).
15
+ *
16
+ * Pure module: no store, no side effects — persistence (store.setMemoryTags)
17
+ * is a separate step. sanitizeTags shares the same validation so LLM-extracted
18
+ * tag arrays (autoDream tag-extractor) are filtered identically.
19
+ */
20
+
21
+ export const MAX_TAG_LENGTH = 20;
22
+
23
+ // `一` (U+4E00) .. `龥` (U+9FA5) is the CJK Unified Ideographs block (the
24
+ // rule's `一-龥`). Matching requires a tag character right after `#` so a bare
25
+ // `#` and markdown `## heading` are naturally excluded (#/space not in set).
26
+ const TAG_RE = /#[a-zA-Z0-9_一-龥-]+/g;
27
+ const EDGE_HYPHEN = /^-+|-+$/g;
28
+ const VALID_TAG = /^[a-zA-Z0-9_一-龥-]+$/;
29
+
30
+ /** Validate + dedupe an array of tag strings. Non-strings, blanks, over-long
31
+ * and illegal-character tags are dropped (fail-safe). A leading `#` is
32
+ * tolerated so raw LLM output can be passed through directly. */
33
+ export function sanitizeTags(tags) {
34
+ if (!Array.isArray(tags)) return [];
35
+ const seen = new Set();
36
+ const out = [];
37
+ for (const t of tags) {
38
+ if (typeof t !== "string") continue;
39
+ let tag = t.trim();
40
+ if (tag.startsWith("#")) tag = tag.slice(1);
41
+ tag = tag.replace(EDGE_HYPHEN, "");
42
+ if (!tag || tag.length > MAX_TAG_LENGTH) continue;
43
+ if (!VALID_TAG.test(tag)) continue;
44
+ if (seen.has(tag)) continue; // 去重(保留首次出现顺序)
45
+ seen.add(tag);
46
+ out.push(tag);
47
+ }
48
+ return out;
49
+ }
50
+
51
+ /** @returns {string[]} validated tags in source order, deduplicated. */
52
+ export function parseTags(content) {
53
+ if (typeof content !== "string" || content.length === 0) return [];
54
+ const raw = [];
55
+ TAG_RE.lastIndex = 0;
56
+ let m;
57
+ while ((m = TAG_RE.exec(content)) !== null) raw.push(m[0]);
58
+ return sanitizeTags(raw);
59
+ }
@@ -1,38 +1,38 @@
1
- /**
2
- * Wiki-Link parser (v0.6.1). Given a memory's content, extract explicit
3
- * cross-memory links of the form [[target]] or [[显示|target]]:
4
- * [[target]] → { display: "target", target: "target" }
5
- * [[显示|target]] → { display: "显示", target: "target" }
6
- *
7
- * Unclosed / empty-target / multi-pipe / bracket-nested markers are treated as
8
- * illegal and ignored. Pure module: no store, no side effects — resolution is a
9
- * separate step (resolveWikiLink) that needs a store handle.
10
- */
11
-
12
- /** @returns {{display: string, target: string}[]} in source order. */
13
- export function parseWikiLinks(content) {
14
- if (typeof content !== "string" || content.length === 0) return [];
15
- const links = [];
16
- // [^\[\]]* keeps a single match from crossing `]]`; an unclosed `[[` never
17
- // matches, and `[[a [[b]] c]]` only yields the inner `[[b]]`.
18
- const re = /\[\[([^\[\]]*)\]\]/g;
19
- let m;
20
- while ((m = re.exec(content)) !== null) {
21
- const inner = m[1];
22
- const parts = inner.split("|");
23
- if (parts.length > 2) continue; // 多管道 → 非法,忽略
24
- const rawTarget = (parts.length === 2 ? parts[1] : parts[0]).trim();
25
- if (!rawTarget) continue; // 空目标 → 非法,忽略
26
- const rawDisplay = parts[0].trim();
27
- links.push({ display: rawDisplay || rawTarget, target: rawTarget });
28
- }
29
- return links;
30
- }
31
-
32
- /** Resolve a wiki-link target title to a memory row via a case-insensitive
33
- * exact title match (store.findByTitle). Returns undefined when absent or the
34
- * store exposes no such lookup. */
35
- export function resolveWikiLink(store, title) {
36
- if (!store || typeof title !== "string" || !title.trim()) return undefined;
37
- return store.findByTitle?.(title.trim());
38
- }
1
+ /**
2
+ * Wiki-Link parser (v0.6.1). Given a memory's content, extract explicit
3
+ * cross-memory links of the form [[target]] or [[显示|target]]:
4
+ * [[target]] → { display: "target", target: "target" }
5
+ * [[显示|target]] → { display: "显示", target: "target" }
6
+ *
7
+ * Unclosed / empty-target / multi-pipe / bracket-nested markers are treated as
8
+ * illegal and ignored. Pure module: no store, no side effects — resolution is a
9
+ * separate step (resolveWikiLink) that needs a store handle.
10
+ */
11
+
12
+ /** @returns {{display: string, target: string}[]} in source order. */
13
+ export function parseWikiLinks(content) {
14
+ if (typeof content !== "string" || content.length === 0) return [];
15
+ const links = [];
16
+ // [^\[\]]* keeps a single match from crossing `]]`; an unclosed `[[` never
17
+ // matches, and `[[a [[b]] c]]` only yields the inner `[[b]]`.
18
+ const re = /\[\[([^\[\]]*)\]\]/g;
19
+ let m;
20
+ while ((m = re.exec(content)) !== null) {
21
+ const inner = m[1];
22
+ const parts = inner.split("|");
23
+ if (parts.length > 2) continue; // 多管道 → 非法,忽略
24
+ const rawTarget = (parts.length === 2 ? parts[1] : parts[0]).trim();
25
+ if (!rawTarget) continue; // 空目标 → 非法,忽略
26
+ const rawDisplay = parts[0].trim();
27
+ links.push({ display: rawDisplay || rawTarget, target: rawTarget });
28
+ }
29
+ return links;
30
+ }
31
+
32
+ /** Resolve a wiki-link target title to a memory row via a case-insensitive
33
+ * exact title match (store.findByTitle). Returns undefined when absent or the
34
+ * store exposes no such lookup. */
35
+ export function resolveWikiLink(store, title) {
36
+ if (!store || typeof title !== "string" || !title.trim()) return undefined;
37
+ return store.findByTitle?.(title.trim());
38
+ }