nebula-memory-mcp 1.3.0 → 1.3.1

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.3.0**(+ 外部写入监听开关) | **1.3.0** | 1.0.9(待同步) | 引擎与上游 2.3.0 对齐 |
156
+ | **2.4.0**(+ Sleep Cycle) | **1.3.1** | 1.0.10(待同步) | 引擎与上游 2.4.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.0',
48
+ version: '1.3.1',
49
49
  }
50
50
 
51
51
  // MCP 协议版本(当前规范)
package/memory/store.cjs CHANGED
@@ -86,6 +86,14 @@ const DEFAULTS = {
86
86
  // ⚠️ 只对多进程/多 agent 共用同一目录的场景有意义:
87
87
  // 单进程内的读写本来就是实时的,开不开都一样(只是多一次 stat)。
88
88
  watchExternalWrites: false,
89
+ // —— 睡眠整理(Sleep Cycle):纯规则保洁,0 token ——
90
+ sleepOnClose: true, // close() 时「距上次 > sleepIntervalHours 且节点 > sleepMinNodes」自动跑一次
91
+ sleepIntervalHours: 24,
92
+ sleepMinNodes: 5000, // 小库没必要整理,阈值以下不触发
93
+ sleepEdgeIdleDays: 90, // 边:超过这么久没被触发、权重又极低,才裁
94
+ sleepEdgeMinWeight: 0.05,
95
+ sleepJunkDays: 30, // 寒暄类节点:这么久没被访问 + importance 极低,才归档
96
+ sleepJunkMaxImportance: 0.05,
89
97
  consolidateThreshold: 0.85, // episode 聚合阈值
90
98
  consolidateMinCluster: 2,
91
99
  consolidateMaxCluster: 12,
@@ -755,6 +763,7 @@ class NebulaMemory {
755
763
  this._lastConsolidateAt = 0;
756
764
  this._lastSummaryRefreshAt = 0; // L3 画像摘要上次刷新的时刻(tick 节流用)
757
765
  this._lastTickAt = 0;
766
+ this._lastSleepAt = 0;
758
767
  this._observeTurns = 0;
759
768
  this._turnCounter = 0;
760
769
  /** 无向边 key 归一(dynamics.js 也通过 mem._edgeKey 使用) */
@@ -830,6 +839,7 @@ class NebulaMemory {
830
839
  if (this._indexMeta) {
831
840
  this._lastConsolidateAt = num(this._indexMeta.lastConsolidateAt, 0);
832
841
  this._lastTickAt = num(this._indexMeta.lastTickAt, 0);
842
+ this._lastSleepAt = num(this._indexMeta.lastSleepAt, 0);
833
843
  this._traceSeq = num(this._indexMeta.traceSeq, 0);
834
844
  this._recallTotal = num(this._indexMeta.recallTotal, 0);
835
845
  // 首次升级到带计数器的版本时,index meta 里没有 recallTotal;
@@ -864,6 +874,20 @@ class NebulaMemory {
864
874
  async close() {
865
875
  if (!this._ready) return;
866
876
  if (this._autoCompactTimer) { clearTimeout(this._autoCompactTimer); this._autoCompactTimer = null; }
877
+ // 睡眠整理:低频 + 大库才跑(默认开;opts.sleepOnClose=false 可关)
878
+ if (this.opts.sleepOnClose !== false) {
879
+ const interval = num(this.opts.sleepIntervalHours, 24) * 3600000;
880
+ if (
881
+ this.nodes.size > num(this.opts.sleepMinNodes, 5000) &&
882
+ Date.now() - num(this._lastSleepAt, 0) > interval
883
+ ) {
884
+ try {
885
+ await this.sleepCycle({ now: Date.now() });
886
+ } catch (e) {
887
+ this._log('warn', `sleepCycle failed (ignored): ${e.message}`);
888
+ }
889
+ }
890
+ }
867
891
  this._flushPending();
868
892
  this._maybeCompact();
869
893
  if (this._traceLines > num(this.opts.traceMax, 200)) this._compactTrace();
@@ -875,6 +899,101 @@ class NebulaMemory {
875
899
  this._closed = true;
876
900
  }
877
901
 
902
+ /**
903
+ * 睡眠整理(Sleep Cycle)—— 纯规则的本地保洁,0 token。
904
+ *
905
+ * 做三件事:① 裁掉「极低权重 + 从未触发 + 长期未动」的边;② 把长期没被访问、
906
+ * importance 极低的短寒暄节点标记 archived(**不删除**,随时可恢复);
907
+ * ③ 有改动就做一次物理压实(重复行合并 + JSONL 重写)。
908
+ *
909
+ * 护栏(这些一律不清理):importance >= 0.9、summary 层、profile 类型、
910
+ * meta 里带 hindsightSynced / synced / experience / observation 的节点。
911
+ *
912
+ * @param {{now?:number, dryRun?:boolean}} [opts]
913
+ * @returns {Promise<Object>} 统计(dryRun 时不落盘)
914
+ */
915
+ async sleepCycle(opts = {}) {
916
+ this._assertReady();
917
+ const now = num(opts.now, Date.now());
918
+ const dryRun = opts.dryRun === true;
919
+ const DAY = 86400000;
920
+ const t0 = Date.now();
921
+ const edgeIdle = num(this.opts.sleepEdgeIdleDays, 90) * DAY;
922
+ const edgeMinW = num(this.opts.sleepEdgeMinWeight, 0.05);
923
+ const junkIdle = num(this.opts.sleepJunkDays, 30) * DAY;
924
+ const junkMaxImp = num(this.opts.sleepJunkMaxImportance, 0.05);
925
+ const result = {
926
+ at: now,
927
+ dryRun,
928
+ edgesPruned: 0,
929
+ junkArchived: 0,
930
+ compacted: false,
931
+ durationMs: 0,
932
+ before: { nodes: this.nodes.size, edges: this.edgeMap.size },
933
+ after: null,
934
+ };
935
+
936
+ const isProtected = (n) => {
937
+ if (n.archived) return true;
938
+ if (num(n.importance, 0) >= 0.9) return true;
939
+ if (n.layer === 'summary') return true;
940
+ if (n.kind === 'profile') return true;
941
+ const m = n.meta || {};
942
+ if (m.hindsightSynced || m.synced === true || m.experience || m.observation) return true;
943
+ return false;
944
+ };
945
+
946
+ // ① 断边:只删边,不删节点
947
+ for (const e of [...this.edgeMap.values()]) {
948
+ if (num(e.fireCount, 0) > 0) continue;
949
+ if (num(e.w, 1) >= edgeMinW) continue;
950
+ if (now - num(e.lastFired, now) < edgeIdle) continue;
951
+ result.edgesPruned++;
952
+ if (!dryRun) this._removeEdge(e);
953
+ }
954
+
955
+ // ② 垃圾归档:只归档短寒暄,正文(哪怕很短)只要不是寒暄就不动
956
+ const JUNK = /^(嗯+|恩+|哦+|好|好的|行|可以|谢谢|多谢|收到|明白|了解|知道了|ok|okay|thanks|thank you|在吗)[。!?.!?~~]*$/i;
957
+ for (const n of [...this.nodes.values()]) {
958
+ if (isProtected(n)) continue;
959
+ if (num(n.importance, 0) > junkMaxImp) continue;
960
+ if (now - num(n.lastAccessAt, num(n.ts, now)) < junkIdle) continue;
961
+ const txt = String(n.text || '').trim();
962
+ if (!txt || txt.length > 12 || !JUNK.test(txt)) continue;
963
+ result.junkArchived++;
964
+ if (dryRun) continue;
965
+ this._unindexNode(n);
966
+ n.archived = true;
967
+ n.archivedAt = now;
968
+ this._indexNode(n);
969
+ this._appendNode(n);
970
+ }
971
+
972
+ // ③ 物理压实:有改动才做
973
+ if (!dryRun && (result.edgesPruned > 0 || result.junkArchived > 0)) {
974
+ this._flushPending();
975
+ this._compactNodesNow = true;
976
+ this._maybeCompact();
977
+ this._compactNodesNow = false;
978
+ this._writeIndexSnapshot();
979
+ this._writeThinIndex();
980
+ result.compacted = true;
981
+ }
982
+
983
+ if (!dryRun) {
984
+ this._lastSleepAt = now;
985
+ this._indexDirty = true;
986
+ this._writeIndexSnapshot();
987
+ }
988
+ result.after = { nodes: this.nodes.size, edges: this.edgeMap.size };
989
+ result.durationMs = Date.now() - t0;
990
+ this._log(
991
+ 'info',
992
+ `sleepCycle${dryRun ? ' (dryRun)' : ''}: edges-${result.edgesPruned} junk-${result.junkArchived} compacted=${result.compacted} ${result.durationMs}ms`
993
+ );
994
+ return result;
995
+ }
996
+
878
997
  /**
879
998
  * 手动压缩(实例保持可用):落 pending → 强制重写 nodes/edges jsonl → 落索引快照。
880
999
  * 只序列化内存里的存活集,规模再大也是亚秒级;收益是磁盘文件和下一次冷启动。
@@ -2123,6 +2242,7 @@ class NebulaMemory {
2123
2242
  traceSeq: payload.traceSeq,
2124
2243
  lastConsolidateAt: payload.lastConsolidateAt,
2125
2244
  lastTickAt: payload.lastTickAt,
2245
+ lastSleepAt: payload.lastSleepAt,
2126
2246
  };
2127
2247
  }
2128
2248
  const stale =
@@ -2337,6 +2457,7 @@ class NebulaMemory {
2337
2457
  recallTotal: payload.recallTotal,
2338
2458
  lastConsolidateAt: payload.lastConsolidateAt,
2339
2459
  lastTickAt: payload.lastTickAt,
2460
+ lastSleepAt: payload.lastSleepAt,
2340
2461
  };
2341
2462
  // 薄索引是权威,无需因为 index.json 陈旧就重写 19MB 快照
2342
2463
  this._indexDirty = false;
@@ -2532,6 +2653,7 @@ class NebulaMemory {
2532
2653
  recallTotal: this._recallTotal,
2533
2654
  lastConsolidateAt: this._lastConsolidateAt,
2534
2655
  lastTickAt: this._lastTickAt,
2656
+ lastSleepAt: this._lastSleepAt,
2535
2657
  nodes,
2536
2658
  edges,
2537
2659
  };
@@ -2836,6 +2958,7 @@ class NebulaMemory {
2836
2958
  recallTotal: this._recallTotal,
2837
2959
  lastConsolidateAt: this._lastConsolidateAt,
2838
2960
  lastTickAt: this._lastTickAt,
2961
+ lastSleepAt: this._lastSleepAt,
2839
2962
  postings,
2840
2963
  };
2841
2964
  atomicWrite(this.fileIndex, JSON.stringify(payload));
@@ -2853,6 +2976,7 @@ class NebulaMemory {
2853
2976
  recallTotal: payload.recallTotal,
2854
2977
  lastConsolidateAt: payload.lastConsolidateAt,
2855
2978
  lastTickAt: payload.lastTickAt,
2979
+ lastSleepAt: payload.lastSleepAt,
2856
2980
  };
2857
2981
  }
2858
2982
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nebula-memory-mcp",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
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",