nebula-memory-mcp 1.3.1 → 1.3.2

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.
package/README.md CHANGED
@@ -153,7 +153,7 @@ agent-memory(引擎真源,9 个模块) ──人工对齐──▶ 本
153
153
 
154
154
  | 上游 `agent-memory` | **本仓库** | `nebula-workbuddy-mcp` | 备注 |
155
155
  |---|---|---|---|
156
- | **2.4.0**(+ Sleep Cycle) | **1.3.1** | 1.0.10(待同步) | 引擎与上游 2.4.0 对齐 |
156
+ | **2.5.0**(+ 冲突裁决) | **1.3.2** | 1.0.11(待同步) | 引擎与上游 2.5.0 对齐 |
157
157
 
158
158
  ## License
159
159
 
package/mcp-server.mjs CHANGED
@@ -45,7 +45,7 @@ const DATA_DIR = path.resolve(
45
45
  const SERVER_INFO = {
46
46
  name: 'agent-memory',
47
47
  // 与 package.json 的 version 保持一致(发版时必须一起改,见 README「引擎来历与同步」)
48
- version: '1.3.1',
48
+ version: '1.3.2',
49
49
  }
50
50
 
51
51
  // MCP 协议版本(当前规范)
@@ -207,12 +207,13 @@ const TOOLS = [
207
207
  },
208
208
  {
209
209
  name: 'recall',
210
- description: '召回与 query 相关的记忆。本地四臂检索,返回按相关度排序的记忆列表。',
210
+ description: '召回与 query 相关的记忆。本地四臂检索,返回按相关度排序的记忆列表。默认不返回「已被新陈述取代」的旧记忆(include_superseded=true 可查历史)。',
211
211
  inputSchema: {
212
212
  type: 'object',
213
213
  properties: {
214
214
  query: { type: 'string', description: '要检索的内容' },
215
215
  limit: { type: 'number', description: '最多返回条数,默认 6' },
216
+ include_superseded: { type: 'boolean', description: '是否包含已被新陈述取代的旧记忆(默认 false)' },
216
217
  namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库;填了只在该子库内检索。' },
217
218
  },
218
219
  required: ['query'],
@@ -275,7 +276,10 @@ async function callTool(name, args, defaultMem) {
275
276
  case 'recall': {
276
277
  const query = String(args.query || '')
277
278
  const limit = Math.max(1, Math.min(50, Number(args.limit) || 6))
278
- const hits = await mem.recall(query, { limit })
279
+ const hits = await mem.recall(query, {
280
+ limit,
281
+ includeSuperseded: args.include_superseded === true,
282
+ })
279
283
  const text = hits.length
280
284
  ? hits.map((h, i) => `${i + 1}. [${h.kind}] ${h.text} (score ${h.score})`).join('\n')
281
285
  : '(无匹配记忆)'
@@ -0,0 +1,121 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * conflict.cjs —— 谓词级冲突识别(纯规则,零依赖)
5
+ *
6
+ * 解决的是"记忆打架":昨天说「我喜欢 TypeScript」,今天说「我讨厌 TypeScript」,
7
+ * 两条都留在库里,召回时 AI 会精神分裂。
8
+ *
9
+ * 做法:从正文里抽一个极窄的「(主体, 谓词类, 极性)」三元组;只有
10
+ * - 主体归一化后完全相同
11
+ * - 谓词类相同
12
+ * - 极性相反(或新句是「改用/换成」这类更新语义)
13
+ * 才判定为冲突。**宁可漏判,不可误判** —— 误判会让一条真实记忆被静默降权。
14
+ *
15
+ * 已知不处理的(按设计):
16
+ * - 否定套否定(「我不是不喜欢」):TRAP 直接放弃整句
17
+ * - 无主语 / 主语是代词(「我喜欢它」):不提 claim
18
+ * - 中文之外的语言:词表只覆盖中英文常见极性表达
19
+ */
20
+
21
+ /** 否定套否定:这类句子语义歧义,直接不解析 */
22
+ const TRAP = /不是不|并非不|没有不|算不上不|不能说.*不/;
23
+
24
+ /**
25
+ * 抽取规则。顺序敏感:负极性必须在正极性之前匹配
26
+ * (否则「不喜欢」会被「喜欢」吃掉);「改用/换成」优先于泛化的「用」。
27
+ */
28
+ const RULES = [
29
+ {
30
+ p: 'preference',
31
+ pol: '-',
32
+ re: /我(?:很|挺|更|最|特别|一直|现在|平时|确实)?(?:不喜欢|不爱|讨厌|反感|受不了|不习惯|不常用)\s*(.+)/,
33
+ },
34
+ {
35
+ p: 'preference',
36
+ pol: '+',
37
+ re: /我(?:很|挺|更|最|特别|一直|现在|平时|确实)?(?:喜欢|偏爱|爱用|习惯用|习惯|常用|倾向于|支持)\s*(.+)/,
38
+ },
39
+ {
40
+ p: 'usage',
41
+ pol: 'upd',
42
+ // 只认「我把 X 改成 Y」这种**宾语在动词前**的写法(主体是 X)。
43
+ // 「我现在用 Y」「改用 Y」不认:宾语在动词后,整句会被当成主体(实测线上库误判 297 条)。
44
+ re: /我把\s*([^\s,。;,.;`]{1,20})\s*(?:改用|换成|改成|换为|改为)/,
45
+ },
46
+ {
47
+ p: 'usage',
48
+ pol: '-',
49
+ re: /我(?:不|不再|别)(?:用|使用)\s*(.+)/,
50
+ },
51
+ {
52
+ p: 'usage',
53
+ pol: '+',
54
+ re: /我(?:一直|平时|现在|通常|一般)?(?:用|使用)\s*(.+)/,
55
+ },
56
+ ];
57
+
58
+ /** 主体清洗:去引号/反引号、去尾部语气词与标点 */
59
+ function cleanSubject(raw) {
60
+ let s = String(raw || '')
61
+ .split(/[,。;!?、,.!?;::\n]/)[0]
62
+ .trim()
63
+ .replace(/^[`"'“”‘’《》「」【】\s]+/, '')
64
+ .replace(/[`"'“”‘’《》「」【】\s]+$/, '');
65
+ // 去掉尾部常见语气/助词;去掉开头误吞的「了/的/过」
66
+ s = s.replace(/(?:了|的|吧|呢|吗|啊|呀|哦|嘛)+$/, '').trim();
67
+ s = s.replace(/^[了的过]+/, '').trim();
68
+ return s;
69
+ }
70
+
71
+ /** 主体归一化:比较用,不用于展示 */
72
+ function norm(s) {
73
+ return String(s || '')
74
+ .toLowerCase()
75
+ .replace(/[\s`"'“”‘’《》「」【】]/g, '')
76
+ .replace(/(?:了|的)$/, '');
77
+ }
78
+
79
+ /** 主体是否可信:1~24 字,不能是代词/空壳 */
80
+ const BAD_SUBJECTS = new Set(['它', '他', '她', '这个', '那个', '这些', '那些', '这样', '那样', '这种', '那种']);
81
+
82
+ /**
83
+ * 从一条正文里抽 claim。
84
+ * @param {string} text
85
+ * @returns {{s:string, p:string, pol:string}|null}
86
+ */
87
+ function parseClaim(text) {
88
+ const t = String(text || '').trim();
89
+ if (!t || t.length > 400) return null;
90
+ if (TRAP.test(t)) return null;
91
+ for (const rule of RULES) {
92
+ const m = t.match(rule.re);
93
+ if (!m) continue;
94
+ const s = cleanSubject(m[1]);
95
+ // 时长/数量词不当主体(「我用了三天才搞定」不是在使用某个工具)
96
+ if (/^[0-9一二三四五六七八九十百千万两]+(?:天|日|小时|分钟|秒|周|星期|月|年|次|遍|个|位|岁)/.test(s)) continue;
97
+ if (!s || s.length > 24) continue;
98
+ if (BAD_SUBJECTS.has(s)) continue;
99
+ // 主体里必须至少有一个字母/数字/汉字,纯标点不算
100
+ if (!/[A-Za-z0-9\u4e00-\u9fa5]/.test(s)) continue;
101
+ // 引号残留 / 疑问词 / 情态词:说明抓到的是一段话而不是一个「东西」
102
+ if (/[」』》】"”’]/.test(s)) continue;
103
+ if (/(?:什么|怎么|为什么|是否|可以|应该|要不要|能|会|吗|呢)/.test(s)) continue;
104
+ return { s, p: rule.p, pol: rule.pol };
105
+ }
106
+ return null;
107
+ }
108
+
109
+ /**
110
+ * 两条 claim 是否冲突。
111
+ * @returns {boolean}
112
+ */
113
+ function isConflict(oldClaim, newClaim) {
114
+ if (!oldClaim || !newClaim) return false;
115
+ if (oldClaim.p !== newClaim.p) return false;
116
+ if (norm(oldClaim.s) !== norm(newClaim.s)) return false;
117
+ if (oldClaim.pol === newClaim.pol) return false;
118
+ return true;
119
+ }
120
+
121
+ module.exports = { parseClaim, isConflict, norm, cleanSubject, TRAP };
package/memory/recall.cjs CHANGED
@@ -61,6 +61,7 @@ function eligible(node, opts) {
61
61
  if (!node || node.deleted) return false;
62
62
  if (node.archived && !opts.includeArchived) return false;
63
63
  if (node.dormant && !opts.includeDormant && !opts.includeArchived) return false;
64
+ if (node.meta && node.meta.supersededBy && !opts.includeSuperseded) return false;
64
65
  return true;
65
66
  }
66
67
 
@@ -186,6 +187,7 @@ function runRecall(mem, query, options = {}) {
186
187
  const opts = {
187
188
  includeArchived: options.includeArchived === true,
188
189
  includeDormant: options.includeDormant === true,
190
+ includeSuperseded: options.includeSuperseded === true,
189
191
  hops: options.hops,
190
192
  alpha: options.alpha,
191
193
  gamma: options.gamma,
package/memory/store.cjs CHANGED
@@ -43,6 +43,7 @@ const recallSynonyms = require('./synonyms.cjs');
43
43
  const profileRecall = require('./profile-recall.cjs');
44
44
  const typoCorrect = require('./typo-fix.cjs');
45
45
  const pii = require('./pii.cjs');
46
+ const conflict = require('./conflict.cjs');
46
47
 
47
48
  const { LAYER, EDGE_KIND, SUMMARY_KIND } = dynamics;
48
49
 
@@ -94,6 +95,8 @@ const DEFAULTS = {
94
95
  sleepEdgeMinWeight: 0.05,
95
96
  sleepJunkDays: 30, // 寒暄类节点:这么久没被访问 + importance 极低,才归档
96
97
  sleepJunkMaxImportance: 0.05,
98
+ // 谓词级冲突裁决:写入时识别「同一主体、极性相反」的旧陈述并标 superseded(只降权不删)
99
+ conflictResolve: true,
97
100
  consolidateThreshold: 0.85, // episode 聚合阈值
98
101
  consolidateMinCluster: 2,
99
102
  consolidateMaxCluster: 12,
@@ -1102,6 +1105,37 @@ class NebulaMemory {
1102
1105
  return this._rememberOne({ ...input, text: raw, _redactTypes: redacted.types });
1103
1106
  }
1104
1107
 
1108
+ /**
1109
+ * 冲突裁决:新记录若与既有记录「同主体、同谓词类、极性相反」,把旧的标 superseded。
1110
+ * 铁律:只降权不删除;importance>=0.9 的永固节点豁免;已经 superseded 的不重复处理。
1111
+ * @returns {{id:string, claim:Object, supersededBy:string[]}|null}
1112
+ */
1113
+ _markConflicts(rec, now) {
1114
+ if (this.opts.conflictResolve === false) return null;
1115
+ const claim = conflict.parseClaim(rec.text);
1116
+ if (!claim) return null;
1117
+ rec.meta = { ...(rec.meta || {}), claim };
1118
+ const superseded = [];
1119
+ for (const old of this.nodes.values()) {
1120
+ if (old.id === rec.id) continue;
1121
+ const c = old.meta && old.meta.claim;
1122
+ if (!c) continue;
1123
+ if (!conflict.isConflict(c, claim)) continue;
1124
+ if (num(old.importance, 0) >= 0.9) continue; // 永固节点豁免
1125
+ if (old.meta.supersededBy) continue;
1126
+ this._unindexNode(old);
1127
+ old.meta = { ...old.meta, supersededBy: rec.id, supersededAt: now };
1128
+ old.importance = clamp(num(old.importance, 0.5) * 0.3, num(this.opts.importanceFloor, 0.02), 1);
1129
+ this._indexNode(old);
1130
+ this._appendNode(old);
1131
+ superseded.push(old.id);
1132
+ }
1133
+ if (superseded.length) {
1134
+ this._log('info', `conflict: "${claim.s}" 的新陈述取代了 ${superseded.length} 条旧记忆`);
1135
+ }
1136
+ return { id: rec.id, claim, supersededBy: superseded };
1137
+ }
1138
+
1105
1139
  /** 单条写入(被 remember 调用;超长已在调用方切块)。 */
1106
1140
  async _rememberOne(input = {}) {
1107
1141
  const rec = this._buildRecord(input);
@@ -1128,7 +1162,7 @@ class NebulaMemory {
1128
1162
  this._indexDirty = true;
1129
1163
  this._maybeFlushIndex();
1130
1164
  this._log('debug', `dedupe hit ${dup.id}`);
1131
- return { id: dup.id, deduped: true, node: this._publicNode(dup) };
1165
+ return { id: dup.id, deduped: true, node: this._publicNode(dup), superseded: [] };
1132
1166
  }
1133
1167
 
1134
1168
  this.nodes.set(rec.id, rec);
@@ -1138,7 +1172,14 @@ class NebulaMemory {
1138
1172
  this._dfVersion++;
1139
1173
  this._indexDirty = true;
1140
1174
  this._maybeFlushIndex();
1141
- return { id: rec.id, deduped: false, node: this._publicNode(rec) };
1175
+ // 冲突裁决:必须在新节点已落库之后跑(旧记录的 supersededBy 要指向真实 id)
1176
+ const conflictResult = this._markConflicts(rec, rec.ts);
1177
+ return {
1178
+ id: rec.id,
1179
+ deduped: false,
1180
+ node: this._publicNode(rec),
1181
+ superseded: conflictResult ? conflictResult.supersededBy : [],
1182
+ };
1142
1183
  }
1143
1184
 
1144
1185
  /**
@@ -1386,6 +1427,8 @@ class NebulaMemory {
1386
1427
  if (kinds && !kinds.includes(node.kind)) continue;
1387
1428
  if (options.tag && !node.tags.includes(options.tag)) continue;
1388
1429
  if (!includeArchived && node.archived) continue;
1430
+ // 冲突裁决:被新陈述取代的旧记忆默认不参与召回(includeSuperseded=true 可查历史)
1431
+ if (options.includeSuperseded !== true && node.meta && node.meta.supersededBy) continue;
1389
1432
  if (!includeDormant && node.dormant) continue;
1390
1433
 
1391
1434
  const lexical = this._tfidfCosine(node._tf, qv);
@@ -1802,6 +1845,7 @@ class NebulaMemory {
1802
1845
  topK,
1803
1846
  includeArchived,
1804
1847
  includeDormant,
1848
+ includeSuperseded: options.includeSuperseded === true,
1805
1849
  mmrLambda: num(options.mmrLambda, this.opts.mmrLambda),
1806
1850
  mmrThreshold: num(options.mmrThreshold, this.opts.mmrThreshold),
1807
1851
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nebula-memory-mcp",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "mcpName": "io.github.yb-zhai/nebula-memory",
5
5
  "description": "Zero-dependency local Agent memory MCP Server (stdio JSON-RPC). Works offline, no API keys, no database. Exposes remember/recall/stats/forget to Claude Desktop, Cursor, ChatGPT and any MCP client.",
6
6
  "type": "module",