nebula-memory-mcp 1.3.0 → 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 +1 -1
- package/mcp-server.mjs +7 -3
- package/memory/conflict.cjs +121 -0
- package/memory/recall.cjs +2 -0
- package/memory/store.cjs +170 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -153,7 +153,7 @@ agent-memory(引擎真源,9 个模块) ──人工对齐──▶ 本
|
|
|
153
153
|
|
|
154
154
|
| 上游 `agent-memory` | **本仓库** | `nebula-workbuddy-mcp` | 备注 |
|
|
155
155
|
|---|---|---|---|
|
|
156
|
-
| **2.
|
|
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.
|
|
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, {
|
|
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
|
|
|
@@ -86,6 +87,16 @@ const DEFAULTS = {
|
|
|
86
87
|
// ⚠️ 只对多进程/多 agent 共用同一目录的场景有意义:
|
|
87
88
|
// 单进程内的读写本来就是实时的,开不开都一样(只是多一次 stat)。
|
|
88
89
|
watchExternalWrites: false,
|
|
90
|
+
// —— 睡眠整理(Sleep Cycle):纯规则保洁,0 token ——
|
|
91
|
+
sleepOnClose: true, // close() 时「距上次 > sleepIntervalHours 且节点 > sleepMinNodes」自动跑一次
|
|
92
|
+
sleepIntervalHours: 24,
|
|
93
|
+
sleepMinNodes: 5000, // 小库没必要整理,阈值以下不触发
|
|
94
|
+
sleepEdgeIdleDays: 90, // 边:超过这么久没被触发、权重又极低,才裁
|
|
95
|
+
sleepEdgeMinWeight: 0.05,
|
|
96
|
+
sleepJunkDays: 30, // 寒暄类节点:这么久没被访问 + importance 极低,才归档
|
|
97
|
+
sleepJunkMaxImportance: 0.05,
|
|
98
|
+
// 谓词级冲突裁决:写入时识别「同一主体、极性相反」的旧陈述并标 superseded(只降权不删)
|
|
99
|
+
conflictResolve: true,
|
|
89
100
|
consolidateThreshold: 0.85, // episode 聚合阈值
|
|
90
101
|
consolidateMinCluster: 2,
|
|
91
102
|
consolidateMaxCluster: 12,
|
|
@@ -755,6 +766,7 @@ class NebulaMemory {
|
|
|
755
766
|
this._lastConsolidateAt = 0;
|
|
756
767
|
this._lastSummaryRefreshAt = 0; // L3 画像摘要上次刷新的时刻(tick 节流用)
|
|
757
768
|
this._lastTickAt = 0;
|
|
769
|
+
this._lastSleepAt = 0;
|
|
758
770
|
this._observeTurns = 0;
|
|
759
771
|
this._turnCounter = 0;
|
|
760
772
|
/** 无向边 key 归一(dynamics.js 也通过 mem._edgeKey 使用) */
|
|
@@ -830,6 +842,7 @@ class NebulaMemory {
|
|
|
830
842
|
if (this._indexMeta) {
|
|
831
843
|
this._lastConsolidateAt = num(this._indexMeta.lastConsolidateAt, 0);
|
|
832
844
|
this._lastTickAt = num(this._indexMeta.lastTickAt, 0);
|
|
845
|
+
this._lastSleepAt = num(this._indexMeta.lastSleepAt, 0);
|
|
833
846
|
this._traceSeq = num(this._indexMeta.traceSeq, 0);
|
|
834
847
|
this._recallTotal = num(this._indexMeta.recallTotal, 0);
|
|
835
848
|
// 首次升级到带计数器的版本时,index meta 里没有 recallTotal;
|
|
@@ -864,6 +877,20 @@ class NebulaMemory {
|
|
|
864
877
|
async close() {
|
|
865
878
|
if (!this._ready) return;
|
|
866
879
|
if (this._autoCompactTimer) { clearTimeout(this._autoCompactTimer); this._autoCompactTimer = null; }
|
|
880
|
+
// 睡眠整理:低频 + 大库才跑(默认开;opts.sleepOnClose=false 可关)
|
|
881
|
+
if (this.opts.sleepOnClose !== false) {
|
|
882
|
+
const interval = num(this.opts.sleepIntervalHours, 24) * 3600000;
|
|
883
|
+
if (
|
|
884
|
+
this.nodes.size > num(this.opts.sleepMinNodes, 5000) &&
|
|
885
|
+
Date.now() - num(this._lastSleepAt, 0) > interval
|
|
886
|
+
) {
|
|
887
|
+
try {
|
|
888
|
+
await this.sleepCycle({ now: Date.now() });
|
|
889
|
+
} catch (e) {
|
|
890
|
+
this._log('warn', `sleepCycle failed (ignored): ${e.message}`);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
}
|
|
867
894
|
this._flushPending();
|
|
868
895
|
this._maybeCompact();
|
|
869
896
|
if (this._traceLines > num(this.opts.traceMax, 200)) this._compactTrace();
|
|
@@ -875,6 +902,101 @@ class NebulaMemory {
|
|
|
875
902
|
this._closed = true;
|
|
876
903
|
}
|
|
877
904
|
|
|
905
|
+
/**
|
|
906
|
+
* 睡眠整理(Sleep Cycle)—— 纯规则的本地保洁,0 token。
|
|
907
|
+
*
|
|
908
|
+
* 做三件事:① 裁掉「极低权重 + 从未触发 + 长期未动」的边;② 把长期没被访问、
|
|
909
|
+
* importance 极低的短寒暄节点标记 archived(**不删除**,随时可恢复);
|
|
910
|
+
* ③ 有改动就做一次物理压实(重复行合并 + JSONL 重写)。
|
|
911
|
+
*
|
|
912
|
+
* 护栏(这些一律不清理):importance >= 0.9、summary 层、profile 类型、
|
|
913
|
+
* meta 里带 hindsightSynced / synced / experience / observation 的节点。
|
|
914
|
+
*
|
|
915
|
+
* @param {{now?:number, dryRun?:boolean}} [opts]
|
|
916
|
+
* @returns {Promise<Object>} 统计(dryRun 时不落盘)
|
|
917
|
+
*/
|
|
918
|
+
async sleepCycle(opts = {}) {
|
|
919
|
+
this._assertReady();
|
|
920
|
+
const now = num(opts.now, Date.now());
|
|
921
|
+
const dryRun = opts.dryRun === true;
|
|
922
|
+
const DAY = 86400000;
|
|
923
|
+
const t0 = Date.now();
|
|
924
|
+
const edgeIdle = num(this.opts.sleepEdgeIdleDays, 90) * DAY;
|
|
925
|
+
const edgeMinW = num(this.opts.sleepEdgeMinWeight, 0.05);
|
|
926
|
+
const junkIdle = num(this.opts.sleepJunkDays, 30) * DAY;
|
|
927
|
+
const junkMaxImp = num(this.opts.sleepJunkMaxImportance, 0.05);
|
|
928
|
+
const result = {
|
|
929
|
+
at: now,
|
|
930
|
+
dryRun,
|
|
931
|
+
edgesPruned: 0,
|
|
932
|
+
junkArchived: 0,
|
|
933
|
+
compacted: false,
|
|
934
|
+
durationMs: 0,
|
|
935
|
+
before: { nodes: this.nodes.size, edges: this.edgeMap.size },
|
|
936
|
+
after: null,
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
const isProtected = (n) => {
|
|
940
|
+
if (n.archived) return true;
|
|
941
|
+
if (num(n.importance, 0) >= 0.9) return true;
|
|
942
|
+
if (n.layer === 'summary') return true;
|
|
943
|
+
if (n.kind === 'profile') return true;
|
|
944
|
+
const m = n.meta || {};
|
|
945
|
+
if (m.hindsightSynced || m.synced === true || m.experience || m.observation) return true;
|
|
946
|
+
return false;
|
|
947
|
+
};
|
|
948
|
+
|
|
949
|
+
// ① 断边:只删边,不删节点
|
|
950
|
+
for (const e of [...this.edgeMap.values()]) {
|
|
951
|
+
if (num(e.fireCount, 0) > 0) continue;
|
|
952
|
+
if (num(e.w, 1) >= edgeMinW) continue;
|
|
953
|
+
if (now - num(e.lastFired, now) < edgeIdle) continue;
|
|
954
|
+
result.edgesPruned++;
|
|
955
|
+
if (!dryRun) this._removeEdge(e);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// ② 垃圾归档:只归档短寒暄,正文(哪怕很短)只要不是寒暄就不动
|
|
959
|
+
const JUNK = /^(嗯+|恩+|哦+|好|好的|行|可以|谢谢|多谢|收到|明白|了解|知道了|ok|okay|thanks|thank you|在吗)[。!?.!?~~]*$/i;
|
|
960
|
+
for (const n of [...this.nodes.values()]) {
|
|
961
|
+
if (isProtected(n)) continue;
|
|
962
|
+
if (num(n.importance, 0) > junkMaxImp) continue;
|
|
963
|
+
if (now - num(n.lastAccessAt, num(n.ts, now)) < junkIdle) continue;
|
|
964
|
+
const txt = String(n.text || '').trim();
|
|
965
|
+
if (!txt || txt.length > 12 || !JUNK.test(txt)) continue;
|
|
966
|
+
result.junkArchived++;
|
|
967
|
+
if (dryRun) continue;
|
|
968
|
+
this._unindexNode(n);
|
|
969
|
+
n.archived = true;
|
|
970
|
+
n.archivedAt = now;
|
|
971
|
+
this._indexNode(n);
|
|
972
|
+
this._appendNode(n);
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// ③ 物理压实:有改动才做
|
|
976
|
+
if (!dryRun && (result.edgesPruned > 0 || result.junkArchived > 0)) {
|
|
977
|
+
this._flushPending();
|
|
978
|
+
this._compactNodesNow = true;
|
|
979
|
+
this._maybeCompact();
|
|
980
|
+
this._compactNodesNow = false;
|
|
981
|
+
this._writeIndexSnapshot();
|
|
982
|
+
this._writeThinIndex();
|
|
983
|
+
result.compacted = true;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (!dryRun) {
|
|
987
|
+
this._lastSleepAt = now;
|
|
988
|
+
this._indexDirty = true;
|
|
989
|
+
this._writeIndexSnapshot();
|
|
990
|
+
}
|
|
991
|
+
result.after = { nodes: this.nodes.size, edges: this.edgeMap.size };
|
|
992
|
+
result.durationMs = Date.now() - t0;
|
|
993
|
+
this._log(
|
|
994
|
+
'info',
|
|
995
|
+
`sleepCycle${dryRun ? ' (dryRun)' : ''}: edges-${result.edgesPruned} junk-${result.junkArchived} compacted=${result.compacted} ${result.durationMs}ms`
|
|
996
|
+
);
|
|
997
|
+
return result;
|
|
998
|
+
}
|
|
999
|
+
|
|
878
1000
|
/**
|
|
879
1001
|
* 手动压缩(实例保持可用):落 pending → 强制重写 nodes/edges jsonl → 落索引快照。
|
|
880
1002
|
* 只序列化内存里的存活集,规模再大也是亚秒级;收益是磁盘文件和下一次冷启动。
|
|
@@ -983,6 +1105,37 @@ class NebulaMemory {
|
|
|
983
1105
|
return this._rememberOne({ ...input, text: raw, _redactTypes: redacted.types });
|
|
984
1106
|
}
|
|
985
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
|
+
|
|
986
1139
|
/** 单条写入(被 remember 调用;超长已在调用方切块)。 */
|
|
987
1140
|
async _rememberOne(input = {}) {
|
|
988
1141
|
const rec = this._buildRecord(input);
|
|
@@ -1009,7 +1162,7 @@ class NebulaMemory {
|
|
|
1009
1162
|
this._indexDirty = true;
|
|
1010
1163
|
this._maybeFlushIndex();
|
|
1011
1164
|
this._log('debug', `dedupe hit ${dup.id}`);
|
|
1012
|
-
return { id: dup.id, deduped: true, node: this._publicNode(dup) };
|
|
1165
|
+
return { id: dup.id, deduped: true, node: this._publicNode(dup), superseded: [] };
|
|
1013
1166
|
}
|
|
1014
1167
|
|
|
1015
1168
|
this.nodes.set(rec.id, rec);
|
|
@@ -1019,7 +1172,14 @@ class NebulaMemory {
|
|
|
1019
1172
|
this._dfVersion++;
|
|
1020
1173
|
this._indexDirty = true;
|
|
1021
1174
|
this._maybeFlushIndex();
|
|
1022
|
-
|
|
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
|
+
};
|
|
1023
1183
|
}
|
|
1024
1184
|
|
|
1025
1185
|
/**
|
|
@@ -1267,6 +1427,8 @@ class NebulaMemory {
|
|
|
1267
1427
|
if (kinds && !kinds.includes(node.kind)) continue;
|
|
1268
1428
|
if (options.tag && !node.tags.includes(options.tag)) continue;
|
|
1269
1429
|
if (!includeArchived && node.archived) continue;
|
|
1430
|
+
// 冲突裁决:被新陈述取代的旧记忆默认不参与召回(includeSuperseded=true 可查历史)
|
|
1431
|
+
if (options.includeSuperseded !== true && node.meta && node.meta.supersededBy) continue;
|
|
1270
1432
|
if (!includeDormant && node.dormant) continue;
|
|
1271
1433
|
|
|
1272
1434
|
const lexical = this._tfidfCosine(node._tf, qv);
|
|
@@ -1683,6 +1845,7 @@ class NebulaMemory {
|
|
|
1683
1845
|
topK,
|
|
1684
1846
|
includeArchived,
|
|
1685
1847
|
includeDormant,
|
|
1848
|
+
includeSuperseded: options.includeSuperseded === true,
|
|
1686
1849
|
mmrLambda: num(options.mmrLambda, this.opts.mmrLambda),
|
|
1687
1850
|
mmrThreshold: num(options.mmrThreshold, this.opts.mmrThreshold),
|
|
1688
1851
|
});
|
|
@@ -2123,6 +2286,7 @@ class NebulaMemory {
|
|
|
2123
2286
|
traceSeq: payload.traceSeq,
|
|
2124
2287
|
lastConsolidateAt: payload.lastConsolidateAt,
|
|
2125
2288
|
lastTickAt: payload.lastTickAt,
|
|
2289
|
+
lastSleepAt: payload.lastSleepAt,
|
|
2126
2290
|
};
|
|
2127
2291
|
}
|
|
2128
2292
|
const stale =
|
|
@@ -2337,6 +2501,7 @@ class NebulaMemory {
|
|
|
2337
2501
|
recallTotal: payload.recallTotal,
|
|
2338
2502
|
lastConsolidateAt: payload.lastConsolidateAt,
|
|
2339
2503
|
lastTickAt: payload.lastTickAt,
|
|
2504
|
+
lastSleepAt: payload.lastSleepAt,
|
|
2340
2505
|
};
|
|
2341
2506
|
// 薄索引是权威,无需因为 index.json 陈旧就重写 19MB 快照
|
|
2342
2507
|
this._indexDirty = false;
|
|
@@ -2532,6 +2697,7 @@ class NebulaMemory {
|
|
|
2532
2697
|
recallTotal: this._recallTotal,
|
|
2533
2698
|
lastConsolidateAt: this._lastConsolidateAt,
|
|
2534
2699
|
lastTickAt: this._lastTickAt,
|
|
2700
|
+
lastSleepAt: this._lastSleepAt,
|
|
2535
2701
|
nodes,
|
|
2536
2702
|
edges,
|
|
2537
2703
|
};
|
|
@@ -2836,6 +3002,7 @@ class NebulaMemory {
|
|
|
2836
3002
|
recallTotal: this._recallTotal,
|
|
2837
3003
|
lastConsolidateAt: this._lastConsolidateAt,
|
|
2838
3004
|
lastTickAt: this._lastTickAt,
|
|
3005
|
+
lastSleepAt: this._lastSleepAt,
|
|
2839
3006
|
postings,
|
|
2840
3007
|
};
|
|
2841
3008
|
atomicWrite(this.fileIndex, JSON.stringify(payload));
|
|
@@ -2853,6 +3020,7 @@ class NebulaMemory {
|
|
|
2853
3020
|
recallTotal: payload.recallTotal,
|
|
2854
3021
|
lastConsolidateAt: payload.lastConsolidateAt,
|
|
2855
3022
|
lastTickAt: payload.lastTickAt,
|
|
3023
|
+
lastSleepAt: payload.lastSleepAt,
|
|
2856
3024
|
};
|
|
2857
3025
|
}
|
|
2858
3026
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nebula-memory-mcp",
|
|
3
|
-
"version": "1.3.
|
|
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",
|