dsh-memory-huixiang 0.1.2 → 0.1.4
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 +2 -2
- package/index.js +53 -18
- package/lib/autodream.js +3 -1
- package/lib/dream.js +23 -3
- package/lib/heat.js +3 -0
- package/lib/lock.js +34 -0
- package/lib/parse.js +6 -3
- package/lib/prompt.js +19 -5
- package/lib/recall.js +2 -1
- package/lib/store.js +76 -68
- package/lib/stream.js +9 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -155,7 +155,7 @@ dsh plugin --profile web add link:~/.dsh/plugin-src/dsh-memory-huixiang
|
|
|
155
155
|
以下命令**只在源码树里跑**——npm 包不带 `test/`、`demo/`、`eval/`(包只含 `index.js`、`lib/`、`cordis.patch.yml`、`README.md`、`NOTICE`、`LICENSE`)。
|
|
156
156
|
|
|
157
157
|
```bash
|
|
158
|
-
node --test #
|
|
158
|
+
node --test # 244 个测试全过
|
|
159
159
|
node demo/simulate.mjs # 10 项演示,用临时库,不碰真库
|
|
160
160
|
node eval/run.mjs --golden <你的题库.jsonl> --dir <你的库目录>
|
|
161
161
|
```
|
|
@@ -173,7 +173,7 @@ node eval/run.mjs --golden <你的题库.jsonl> --dir <你的库目录>
|
|
|
173
173
|
- 召回是词面驱动的:换个说法(「家在何方」对「住哪」)可能翻不到。
|
|
174
174
|
- 单机单进程假设:多进程靠 `O_EXCL` 文件锁,不是分布式锁。
|
|
175
175
|
- `stream.jsonl` 会存每一轮原话(单条截断 4000 字)。这是特性也是风险,见「隐私」。
|
|
176
|
-
-
|
|
176
|
+
- 244 个测试覆盖的是核心路径,不是全覆盖。
|
|
177
177
|
|
|
178
178
|
## 隐私
|
|
179
179
|
|
package/index.js
CHANGED
|
@@ -164,7 +164,7 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
164
164
|
// 加载标记(抄 dsh-hippocampus 0.3.1 的 load.log,2026-11-16):重启后 tail 一下就知道
|
|
165
165
|
// 跑的是哪版、读的是哪份库、卡片几张;写不进去不拦插件——标记是给自己看的,不是功能。
|
|
166
166
|
try {
|
|
167
|
-
appendFileSync(
|
|
167
|
+
appendFileSync(join(dataDir, 'load.log'), `${new Date().toISOString()} loaded v${PKG_VERSION} cards=${store.all().length} dataDir=${dataDir} pid=${process.pid}\n`)
|
|
168
168
|
} catch { /* 标记失败不拦插件 */ }
|
|
169
169
|
// 每日体检补偿(2026-11-14 审查:04:30 定时任务的投递记录在,脚本却没留下记录——手机那时多半不在线)。
|
|
170
170
|
// 启动后补跑一次:只认真库、只认「今天还没有记录」,测试进程一概不碰。
|
|
@@ -198,8 +198,19 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
198
198
|
timer.unref?.()
|
|
199
199
|
return () => clearTimeout(timer)
|
|
200
200
|
}, 'cyber-hippocampus: health catch-up')
|
|
201
|
+
// 卸载/热重载后,setImmediate 里排队的做梦不该再去碰已经放开的 store(2026-11-21 #12)。
|
|
202
|
+
let disposed = false
|
|
203
|
+
ctx.effect(() => () => { disposed = true }, 'cyber-hippocampus: dispose flag')
|
|
201
204
|
// trace 使用侧:每个会话最近几次注入各记一条(各自带 iid 和被引用集合),别整体覆盖。
|
|
202
205
|
const injectedBySid = new Map() // sid -> [{ iid, ids, used: Set }]
|
|
206
|
+
// 这几个 Map 只增不删,长跑会随会话数一直涨(2026-11-21 #7):超过 200 个就丢最旧的一批。
|
|
207
|
+
function trimSidMap(map, max = 200) {
|
|
208
|
+
if (map.size <= max) return
|
|
209
|
+
for (const k of map.keys()) {
|
|
210
|
+
if (map.size <= Math.floor(max * 0.8)) break
|
|
211
|
+
map.delete(k)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
203
214
|
const INJECT_HISTORY = 5
|
|
204
215
|
let injectSeq = 0
|
|
205
216
|
let lastSessionId = '' // 事件里最后一次见到的会话;工具执行拿得到 session 时只用它兜底
|
|
@@ -581,6 +592,7 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
581
592
|
// 判死跟 cue 路径同口径:valid_until 写纯日期时按本地日界算,别拿 Date.parse 当 UTC 零点(2026-11-21 B3)。
|
|
582
593
|
const isDeadCard = (c) => isDead(c, Date.now())
|
|
583
594
|
const picked = []
|
|
595
|
+
const seenIds = new Set()
|
|
584
596
|
const missed = []
|
|
585
597
|
const ambiguous = []
|
|
586
598
|
const cueOf = new Map()
|
|
@@ -591,7 +603,8 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
591
603
|
const hit = allIds.includes(w) ? [w] : (tail4.get(w) || [])
|
|
592
604
|
if (hit.length === 1) {
|
|
593
605
|
const card = store.get(hit[0])
|
|
594
|
-
|
|
606
|
+
// 同一张卡在 ids 里出现两次时只取一次(否则重复 touch、返回值也重复,2026-11-21 #2)。
|
|
607
|
+
if (card && !seenIds.has(card.id)) { seenIds.add(card.id); picked.push(card); cueOf.set(card.id, raw) }
|
|
595
608
|
else missed.push(w)
|
|
596
609
|
} else if (hit.length > 1) {
|
|
597
610
|
ambiguous.push({ 短id: w, 候选: hit.slice(0, 4) })
|
|
@@ -758,8 +771,10 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
758
771
|
const r = store.addSelfNote(args.write)
|
|
759
772
|
return { ok: true, ...r }
|
|
760
773
|
}
|
|
761
|
-
|
|
762
|
-
|
|
774
|
+
// 传负数/0/怪值时别让 slice 跑偏,也别为读一次笔记扫两遍盘(2026-11-21 #4)。
|
|
775
|
+
const last = Math.min(40, Math.max(1, Math.trunc(Number(args.last) || 3)))
|
|
776
|
+
const all = store.selfNotes()
|
|
777
|
+
return { ok: true, count: all.length, notes: all.slice(-last) }
|
|
763
778
|
},
|
|
764
779
|
},
|
|
765
780
|
{
|
|
@@ -794,7 +809,11 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
794
809
|
},
|
|
795
810
|
output: TEXT({ type: 'object' }),
|
|
796
811
|
isConcurrencySafe: () => false,
|
|
797
|
-
execute: async (args) =>
|
|
812
|
+
execute: async (args) => {
|
|
813
|
+
// 0 会被 || 改写成 14,负数会让窗口取反(2026-11-21 #5)。
|
|
814
|
+
const days = Math.min(365, Math.max(1, Math.trunc(Number(args.lookbackDays) || 14)))
|
|
815
|
+
return { ok: true, ...dreamPass(store, { dryRun: args.dryRun !== false, lookbackDays: days }) }
|
|
816
|
+
},
|
|
798
817
|
},
|
|
799
818
|
{
|
|
800
819
|
name: 'hc_status',
|
|
@@ -835,32 +854,44 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
835
854
|
for (const def of definitions) ctx.effect(() => ctx.tools.register(def), `cyber-hippocampus: ${def.name}`)
|
|
836
855
|
|
|
837
856
|
// 一轮压缩会被拆成好多条 prune,先攒着,等 end/summary 时记成一笔。
|
|
838
|
-
|
|
857
|
+
// 按会话分开:两个窗口可能同时在压自己的历史,共用一个变量会串账、丢账(2026-11-21 #6)。
|
|
858
|
+
const foldings = new Map()
|
|
859
|
+
const FOLD_TTL_MS = 60 * 60 * 1000 // 只见 start 不见 end 的僵尸账,1 小时后自清,别越攒越多
|
|
860
|
+
const foldOf = (key, now = Date.now()) => {
|
|
861
|
+
for (const [k, v] of foldings) if (now - v.at > FOLD_TTL_MS) foldings.delete(k)
|
|
862
|
+
let f = foldings.get(key)
|
|
863
|
+
if (!f) {
|
|
864
|
+
f = { tokens: 0, seqStart: null, seqEnd: null, at: now }
|
|
865
|
+
foldings.set(key, f)
|
|
866
|
+
}
|
|
867
|
+
return f
|
|
868
|
+
}
|
|
839
869
|
|
|
840
870
|
ctx.on('session/event', (session, event) => {
|
|
841
871
|
try {
|
|
842
872
|
if (session?.id) lastSessionId = session.id
|
|
843
873
|
const type = event?.type || event?.kind
|
|
844
874
|
const data = event?.data ?? event ?? {}
|
|
875
|
+
const foldKey = String(session?.id || '')
|
|
845
876
|
|
|
846
877
|
if (type === 'compaction/start') {
|
|
847
|
-
|
|
878
|
+
foldings.set(foldKey, { tokens: 0, seqStart: null, seqEnd: null, at: Date.now() })
|
|
848
879
|
return
|
|
849
880
|
}
|
|
850
881
|
if (type === 'compaction/prune') {
|
|
851
|
-
|
|
852
|
-
|
|
882
|
+
const f = foldOf(foldKey)
|
|
883
|
+
f.at = Date.now()
|
|
884
|
+
f.tokens += Number(data.shadowedTokenCount) || 0
|
|
853
885
|
const start = data?.shadowedRange?.start
|
|
854
886
|
const end = data?.shadowedRange?.end
|
|
855
|
-
if (Number.isFinite(start))
|
|
856
|
-
if (Number.isFinite(end))
|
|
887
|
+
if (Number.isFinite(start)) f.seqStart = f.seqStart === null ? start : Math.min(f.seqStart, start)
|
|
888
|
+
if (Number.isFinite(end)) f.seqEnd = f.seqEnd === null ? end : Math.max(f.seqEnd, end)
|
|
857
889
|
return
|
|
858
890
|
}
|
|
859
891
|
if (type === 'compaction/summary' || type === 'compaction/end') {
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
}
|
|
892
|
+
const f = foldings.get(foldKey)
|
|
893
|
+
if (f && f.tokens > 0) store.recordCompaction(f)
|
|
894
|
+
foldings.delete(foldKey)
|
|
864
895
|
return
|
|
865
896
|
}
|
|
866
897
|
|
|
@@ -874,11 +905,13 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
874
905
|
const who = type === 'user/message' ? 'owner' : 'self'
|
|
875
906
|
// B 级「用上了」证据先记:写链/写流失败不该连带把 use 行也丢了。
|
|
876
907
|
if (type === 'assistant/message') recordTextUse(session?.id || '', body)
|
|
877
|
-
|
|
878
|
-
store.
|
|
908
|
+
// 两条流水各写各的:一条失败不该把另一条也带走,也别静默吞掉(2026-11-21 #13)。
|
|
909
|
+
try { store.appendChain(who, body) } catch (err) { console.warn('[cyber-hippocampus] 因果链写失败:', err?.message || err) }
|
|
910
|
+
try { store.appendStream({ role: who, text: body, sessionId: session?.id || '' }) } catch (err) { console.warn('[cyber-hippocampus] 原话流水写失败:', err?.message || err) }
|
|
879
911
|
// 按量做梦:攒够就后台归一次,别在事件钩子里同步跑(做梦要读全库、写盘)。
|
|
880
912
|
if (autoDream.enabled) {
|
|
881
913
|
setImmediate(() => {
|
|
914
|
+
if (disposed) return
|
|
882
915
|
try {
|
|
883
916
|
const r = maybeDream(store, autoDream)
|
|
884
917
|
if (r.ran) console.log('[cyber-hippocampus] 按量做梦:整理了', r.wrote, '条,stream 到第', r.total, '行')
|
|
@@ -941,10 +974,12 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
941
974
|
const ok = store.trace({ at: new Date(now).toISOString(), ev: 'inject', sid, ids, n: ids.length, iid })
|
|
942
975
|
if (ok) {
|
|
943
976
|
lastInject.set(sid, { key, at: now })
|
|
977
|
+
trimSidMap(lastInject)
|
|
944
978
|
const hist = injectedBySid.get(sid) || []
|
|
945
979
|
hist.push({ iid, ids, used: new Set() })
|
|
946
980
|
if (hist.length > INJECT_HISTORY) hist.shift()
|
|
947
981
|
injectedBySid.set(sid, hist)
|
|
982
|
+
trimSidMap(injectedBySid)
|
|
948
983
|
}
|
|
949
984
|
else console.warn('[cyber-hippocampus] trace 写入失败,这一条注入流水漏记')
|
|
950
985
|
} catch {
|
|
@@ -964,7 +999,7 @@ const catalogLimit = countOr(config.catalogLimit, 130)
|
|
|
964
999
|
const now = Date.now()
|
|
965
1000
|
const prev = lastBrief.get(sid)
|
|
966
1001
|
if (prev && prev.key === key && now - prev.at < INJECT_DEDUPE_MS) return
|
|
967
|
-
if (store.trace({ at: new Date(now).toISOString(), ev: 'brief', sid, ids, n: ids.length })) lastBrief.set(sid, { key, at: now })
|
|
1002
|
+
if (store.trace({ at: new Date(now).toISOString(), ev: 'brief', sid, ids, n: ids.length })) { lastBrief.set(sid, { key, at: now }); trimSidMap(lastBrief) }
|
|
968
1003
|
} catch {
|
|
969
1004
|
// 记不上不能影响对话。
|
|
970
1005
|
}
|
package/lib/autodream.js
CHANGED
|
@@ -29,7 +29,9 @@ export function maybeDream(store, opts = {}, now = Date.now()) {
|
|
|
29
29
|
const fresh = total - last
|
|
30
30
|
if (fresh < cfg.everyLines) return { ran: false, reason: 'below-threshold', fresh, total, last }
|
|
31
31
|
const lastAt = st?.at ? Date.parse(st.at) : 0
|
|
32
|
-
|
|
32
|
+
// 时间戳落在未来(手动改时间/NTP 回拨)时不进冷却:否则要一直等到那个「未来」才肯再梦(2026-11-21 #32)。
|
|
33
|
+
const lastAtUsable = Number.isFinite(lastAt) && lastAt > 0 && lastAt <= now + 60_000
|
|
34
|
+
if (lastAtUsable && now - lastAt < cfg.cooldownMs) {
|
|
33
35
|
return { ran: false, reason: 'cooldown', fresh, total, last, waitMs: cfg.cooldownMs - (now - lastAt) }
|
|
34
36
|
}
|
|
35
37
|
const result = dreamPass(store, { dryRun: cfg.dryRun })
|
package/lib/dream.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// 梦游整理(BV1dDxC67Ep3):闲下来时按主题把散的事件归成一句话,
|
|
2
2
|
// 但**保留来源、保留矛盾**,不把两条冲突的记忆强行合成一个说法。
|
|
3
3
|
// 这里故意不调模型:先把结构跑通,谁来写「概括」以后可以换成本鲸自己。
|
|
4
|
+
import { join } from 'node:path'
|
|
4
5
|
import { tokenize, isDead } from './recall.js'
|
|
6
|
+
import { withLock } from './lock.js'
|
|
5
7
|
|
|
6
8
|
const DAY = 86_400_000
|
|
7
9
|
|
|
@@ -94,6 +96,12 @@ export function buildInsightBody(tag, list, conflicts, budget) {
|
|
|
94
96
|
}
|
|
95
97
|
|
|
96
98
|
export function dreamPass(store, opts = {}) {
|
|
99
|
+
// 整趟做梦互斥:两个窗口同时触发时,各自读到同一份「这个主题还没有归纳卡」的快照,
|
|
100
|
+
// 会为同一主题各写一张同质卡(2026-11-21 实测两进程同时做梦归出 2 张)。
|
|
101
|
+
return withLock(join(store.dir, 'dream'), () => dreamPassLocked(store, opts))
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function dreamPassLocked(store, opts = {}) {
|
|
97
105
|
const now = opts.now ?? Date.now()
|
|
98
106
|
const lookbackDays = opts.lookbackDays ?? 14
|
|
99
107
|
const minGroup = opts.minGroup ?? 2
|
|
@@ -102,7 +110,12 @@ export function dreamPass(store, opts = {}) {
|
|
|
102
110
|
const allCards = store.all()
|
|
103
111
|
const insightsAll = allCards.filter((c) => c.kind === 'insight')
|
|
104
112
|
// 死卡不进归纳:被接替/已过期的旧说法若经 insight 复活,等于绕过全部死卡闸门(2026-11-21 B2)。
|
|
105
|
-
const cards = allCards.filter((c) =>
|
|
113
|
+
const cards = allCards.filter((c) => {
|
|
114
|
+
if (c.kind !== 'experience' || isDead(c, now) || !c.created) return false
|
|
115
|
+
// created 坏掉的卡(手改、旧版写坏)永远算不进窗口,别让一张坏卡把整趟做梦拖死(2026-11-21 #40)。
|
|
116
|
+
const t = Date.parse(c.created)
|
|
117
|
+
return Number.isFinite(t) && now - t <= lookbackDays * DAY
|
|
118
|
+
})
|
|
106
119
|
|
|
107
120
|
const groups = new Map()
|
|
108
121
|
for (const card of cards) {
|
|
@@ -143,7 +156,8 @@ export function dreamPass(store, opts = {}) {
|
|
|
143
156
|
continue
|
|
144
157
|
}
|
|
145
158
|
const insight = {
|
|
146
|
-
|
|
159
|
+
// 同一毫秒对同一 tag 梦两次会撞 id(后写覆盖前写),加 4 位随机尾巴(2026-11-21 #41)。
|
|
160
|
+
id: `ins-${tag.replace(/[^\w\u4e00-\u9fff]+/g, '')}-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`,
|
|
147
161
|
kind: 'insight',
|
|
148
162
|
source: 'self',
|
|
149
163
|
tags: [tag, '梦游整理'],
|
|
@@ -181,7 +195,13 @@ export function dreamPass(store, opts = {}) {
|
|
|
181
195
|
continue
|
|
182
196
|
}
|
|
183
197
|
if (!dryRun) {
|
|
184
|
-
|
|
198
|
+
try {
|
|
199
|
+
store.put(insight)
|
|
200
|
+
} catch (err) {
|
|
201
|
+
// 一张卡写不进(磁盘满/权限/坏 id)不能把整趟做梦带走,记进 skipped 让调用方看得见(2026-11-21 #38)。
|
|
202
|
+
skipped.push({ tag, reason: `落盘失败:${err?.message || err}`, from: ids })
|
|
203
|
+
continue
|
|
204
|
+
}
|
|
185
205
|
// 这一趟新写的也要进快照,否则同一趟里后面的标签组撞不出孪生,会再写一张同质卡(13:20 复核实测 0.951)。
|
|
186
206
|
// dryRun 下没落盘,就不该进快照(2026-11-21 #37)。
|
|
187
207
|
insightsAll.push(insight)
|
package/lib/heat.js
CHANGED
|
@@ -95,6 +95,9 @@ export function expiryMs(validUntil) {
|
|
|
95
95
|
if (day) {
|
|
96
96
|
// 按本机时区算「那天 23:59:59.999」,别用 Date.parse(它把纯日期当 UTC 零点)。
|
|
97
97
|
const d = new Date(Number(day[1]), Number(day[2]) - 1, Number(day[3]), 23, 59, 59, 999)
|
|
98
|
+
// 2 月 30 日这种不存在的日期,Date 会悄悄滚到 3 月 2 日,卡就比声明的多活几天(2026-11-21 #15)。
|
|
99
|
+
const rolled = d.getFullYear() !== Number(day[1]) || d.getMonth() !== Number(day[2]) - 1 || d.getDate() !== Number(day[3])
|
|
100
|
+
if (rolled) return 0
|
|
98
101
|
return Number.isFinite(d.getTime()) ? d.getTime() : 0
|
|
99
102
|
}
|
|
100
103
|
const t = Date.parse(s)
|
package/lib/lock.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// 文件写锁:同一份库可能被工具调用与夜间整理同时写(BV14qxL6WEGZ 评论区那条「两个进程抢写」的坑)。
|
|
2
|
+
// 用 O_EXCL 建锁文件,拿不到就短暂自旋;超过 10 秒的锁视为前任崩了,直接清掉。
|
|
3
|
+
// 原来长在 store.js 里,2026-11-21 抽出来给 stream.js 的 appendStream 也用(#29 多进程重号)。
|
|
4
|
+
import { rmSync, statSync, writeFileSync } from 'node:fs'
|
|
5
|
+
|
|
6
|
+
export const LOCK_WAIT_MS = 3000
|
|
7
|
+
export const LOCK_STALE_MS = 10_000
|
|
8
|
+
|
|
9
|
+
export function withLock(path, fn) {
|
|
10
|
+
const lock = `${path}.lock`
|
|
11
|
+
const start = Date.now()
|
|
12
|
+
for (;;) {
|
|
13
|
+
try {
|
|
14
|
+
writeFileSync(lock, String(process.pid), { flag: 'wx' })
|
|
15
|
+
break
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (err.code !== 'EEXIST') throw err
|
|
18
|
+
try {
|
|
19
|
+
if (Date.now() - statSync(lock).mtimeMs > LOCK_STALE_MS) {
|
|
20
|
+
rmSync(lock, { force: true })
|
|
21
|
+
continue
|
|
22
|
+
}
|
|
23
|
+
} catch {}
|
|
24
|
+
if (Date.now() - start > LOCK_WAIT_MS) throw new Error(`写锁超时:${lock}`)
|
|
25
|
+
// 原来的同步空转会把整个 agent 循环卡住;用 Atomics.wait 真睡 20ms。
|
|
26
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
return fn()
|
|
31
|
+
} finally {
|
|
32
|
+
rmSync(lock, { force: true })
|
|
33
|
+
}
|
|
34
|
+
}
|
package/lib/parse.js
CHANGED
|
@@ -46,9 +46,12 @@ export function parseCue(cue, now = Date.now()) {
|
|
|
46
46
|
const d = new Date(now)
|
|
47
47
|
d.setMonth(Number(m[1]) - 1, Number(m[2]))
|
|
48
48
|
d.setHours(0, 0, 0, 0)
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
// 2 月 30 日会滚成 3 月 2 日:不存在的日期就当没说,别拿错的一天去搜(2026-11-21 #31)。
|
|
50
|
+
if (d.getMonth() === Number(m[1]) - 1 && d.getDate() === Number(m[2])) {
|
|
51
|
+
out.since = d.getTime()
|
|
52
|
+
out.until = out.since + DAY_MS
|
|
53
|
+
out.when = `${m[1]}月${m[2]}日`
|
|
54
|
+
}
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
|
package/lib/prompt.js
CHANGED
|
@@ -11,6 +11,17 @@ import { expiryMs } from './heat.js'
|
|
|
11
11
|
const DEFAULT_COMPRESSION_LOG = join(process.env.DSH_HOME || join(homedir(), '.dsh'), 'notes', 'compression.log')
|
|
12
12
|
const COMPRESSION_LOG = process.env.HC_COMPRESSION_LOG || DEFAULT_COMPRESSION_LOG
|
|
13
13
|
|
|
14
|
+
/** 按码点截断:text.slice 可能正好切在 emoji 的代理对中间,吐半个字符(2026-11-21 #35)。 */
|
|
15
|
+
export function safeCut(text, n) {
|
|
16
|
+
const s = String(text ?? '')
|
|
17
|
+
const k = Math.max(0, Math.trunc(Number(n) || 0))
|
|
18
|
+
if (k <= 0) return ''
|
|
19
|
+
if (k >= s.length) return s
|
|
20
|
+
const code = s.charCodeAt(k - 1)
|
|
21
|
+
// 落点前一个是「高代理」=正切在一个字符的中间,退一位
|
|
22
|
+
return s.slice(0, code >= 0xd800 && code <= 0xdbff ? k - 1 : k)
|
|
23
|
+
}
|
|
24
|
+
|
|
14
25
|
/** 上一次被折叠的记录:让本鲸知道自己刚被压过,好去自检。 */
|
|
15
26
|
export function lastCompression(logPath = COMPRESSION_LOG) {
|
|
16
27
|
try {
|
|
@@ -63,8 +74,11 @@ export function renderSnapshot({ store, hot = [], briefs = [], catalog = [], cat
|
|
|
63
74
|
const lines = hot.map((c) => {
|
|
64
75
|
const 来源 = c.source === 'owner' ? '主人亲口' : '本鲸推的'
|
|
65
76
|
const 价 = c.conflict ? ' ⚠与旧说法冲突' : ''
|
|
77
|
+
// 0 = 「没写有效期 / 日期读不懂」(heat.js:93/100 的既有约定),不能当「1970 年就过期了」:
|
|
78
|
+
// 否则同一张卡 isDead 放行、这里却标「已过期」,两处口径打架(2026-11-21 只读复核 #15)。
|
|
79
|
+
const 期终点 = expiryMs(c.valid_until)
|
|
66
80
|
const 期 = c.valid_until
|
|
67
|
-
?
|
|
81
|
+
? 期终点 > 0 && 期终点 < Date.now()
|
|
68
82
|
? '(已过期,别再当现在的事实用)'
|
|
69
83
|
: `(有效期到 ${c.valid_until})`
|
|
70
84
|
: ''
|
|
@@ -95,7 +109,7 @@ export function renderSnapshot({ store, hot = [], briefs = [], catalog = [], cat
|
|
|
95
109
|
const 无证据 = c.source_type === 'no-evidence' ? '〔无证据〕' : ''
|
|
96
110
|
let line = `- ${短id}${truncate(c.title || c.body, cap)}(重量 ${c.live_weight?.toFixed(2) ?? '?'}${期}${价})${无证据}`
|
|
97
111
|
// 只有截短阶段才允许硬切;正常渲染一个字节都不动(老卡/现有输出不变)。
|
|
98
|
-
if (cut && line.length > 60) line = `${line
|
|
112
|
+
if (cut && line.length > 60) line = `${safeCut(line, 59)}…`
|
|
99
113
|
return line
|
|
100
114
|
}
|
|
101
115
|
const renderCatalogItem = (x) => `${x.pinned ? '★' : ''}${x.id ? String(x.id).slice(-4) + '· ' : ''}${x.title}`
|
|
@@ -199,7 +213,7 @@ export function renderSnapshot({ store, hot = [], briefs = [], catalog = [], cat
|
|
|
199
213
|
if (budget <= marker.length) {
|
|
200
214
|
text = ''
|
|
201
215
|
} else {
|
|
202
|
-
text = `${text
|
|
216
|
+
text = `${safeCut(text, budget - marker.length).trimEnd()}${marker}`
|
|
203
217
|
}
|
|
204
218
|
degraded = 'truncate'
|
|
205
219
|
}
|
|
@@ -212,12 +226,12 @@ export function renderSnapshot({ store, hot = [], briefs = [], catalog = [], cat
|
|
|
212
226
|
const allowed = MAX_CHARS - GUARD_NOTE.length - SEP.length - warn.length - SEP.length
|
|
213
227
|
if (allowed >= 0 && text.length > allowed) {
|
|
214
228
|
const marker = tail.length > kept.length ? '…(超长截断,因果链已让位)' : '…(超长截断)'
|
|
215
|
-
text = allowed > marker.length ? `${text
|
|
229
|
+
text = allowed > marker.length ? `${safeCut(text, allowed - marker.length).trimEnd()}${marker}` : ''
|
|
216
230
|
degraded = 'truncate'
|
|
217
231
|
}
|
|
218
232
|
}
|
|
219
233
|
|
|
220
|
-
const out = `${text
|
|
234
|
+
const out = `${safeCut(text, Math.max(0, budget))}${SEP}${GUARD_NOTE}${warn ? SEP + warn : ''}`
|
|
221
235
|
if (st) {
|
|
222
236
|
st.briefsWanted = briefs.length
|
|
223
237
|
st.briefsRendered = briefState.length
|
package/lib/recall.js
CHANGED
|
@@ -593,7 +593,8 @@ export function keywordFallback(store, cue, opts = {}) {
|
|
|
593
593
|
excerpt: excerptAround(c.body, hit[0]),
|
|
594
594
|
coverage: Number((hit.length / rare.length).toFixed(2)),
|
|
595
595
|
matched: hit.slice(0, 6),
|
|
596
|
-
|
|
596
|
+
// 同 isDead / 上面 :359 的口径:0 是「没写 / 读不懂」,不是「过期了」(2026-11-21 只读复核 #15)。
|
|
597
|
+
expired: Boolean(c.valid_until && expiryMs(c.valid_until) > 0 && expiryMs(c.valid_until) < now) || Boolean(c.superseded_by),
|
|
597
598
|
lowConfidence: true,
|
|
598
599
|
})
|
|
599
600
|
}
|
package/lib/store.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
// 外文库:一条记忆 = 磁盘上一个纯文本 .md,索引只是一份 JSON。
|
|
2
2
|
// 不依赖向量库,打包走人就能带走(BV121xg6kEqx 的第一层)。
|
|
3
|
-
import { appendFileSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from 'node:fs'
|
|
3
|
+
import { appendFileSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, readSync, renameSync, rmSync, statSync, writeFileSync, writeSync } from 'node:fs'
|
|
4
4
|
import { join } from 'node:path'
|
|
5
5
|
import { homedir } from 'node:os'
|
|
6
6
|
import { execFileSync } from 'node:child_process'
|
|
7
7
|
import { DEFAULT_HEAT, liveWeightOf, rehearse } from './heat.js'
|
|
8
8
|
import { appendStream, localDay, searchStream, streamSize, streamFirstAt, streamFirstKey } from './stream.js'
|
|
9
9
|
import { isDead, tokenize, tokenWeight } from './recall.js'
|
|
10
|
+
import { withLock } from './lock.js'
|
|
10
11
|
|
|
11
12
|
// 数据目录(2026-11-20 改名 dsh-memory-huixiang):新目录叫 huixiang。老用户的库在 cyber-hippocampus/ 下,
|
|
12
13
|
// 不能因为改名就「记忆消失」——新目录还没有、旧目录里已经有卡,就继续用旧目录。
|
|
@@ -86,38 +87,8 @@ const readJson = (path, fallback) => {
|
|
|
86
87
|
return fallback
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const LOCK_WAIT_MS = 3000
|
|
92
|
-
const LOCK_STALE_MS = 10_000
|
|
93
|
-
const withLock = (path, fn) => {
|
|
94
|
-
const lock = `${path}.lock`
|
|
95
|
-
const start = Date.now()
|
|
96
|
-
for (;;) {
|
|
97
|
-
try {
|
|
98
|
-
writeFileSync(lock, String(process.pid), { flag: 'wx' })
|
|
99
|
-
break
|
|
100
|
-
} catch (err) {
|
|
101
|
-
if (err.code !== 'EEXIST') throw err
|
|
102
|
-
try {
|
|
103
|
-
if (Date.now() - statSync(lock).mtimeMs > LOCK_STALE_MS) {
|
|
104
|
-
rmSync(lock, { force: true })
|
|
105
|
-
continue
|
|
106
|
-
}
|
|
107
|
-
} catch {}
|
|
108
|
-
if (Date.now() - start > LOCK_WAIT_MS) throw new Error(`写锁超时:${lock}`)
|
|
109
|
-
// 原来的同步空转会把整个 agent 循环卡住;用 Atomics.wait 真睡 20ms。
|
|
110
|
-
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20)
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
try {
|
|
114
|
-
return fn()
|
|
115
|
-
} finally {
|
|
116
|
-
rmSync(lock, { force: true })
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const writeAtomic = (path, text) => withLock(path, () => {
|
|
90
|
+
/** 无锁的原子写:调用方必须已经持有 path 的锁(put 要把「读 prev + 合并」也包进同一把锁)。 */
|
|
91
|
+
const writeFileUnlocked = (path, text) => {
|
|
121
92
|
const tmp = `${path}.tmp-${process.pid}`
|
|
122
93
|
// 先 fsync 再 rename:否则断电/掉电时 rename 可能指向一个还没落盘的空文件(2026-11-21 B12)。
|
|
123
94
|
const fd = openSync(tmp, 'w', 0o600)
|
|
@@ -128,7 +99,8 @@ const writeAtomic = (path, text) => withLock(path, () => {
|
|
|
128
99
|
closeSync(fd)
|
|
129
100
|
}
|
|
130
101
|
renameSync(tmp, path)
|
|
131
|
-
}
|
|
102
|
+
}
|
|
103
|
+
const writeAtomic = (path, text) => withLock(path, () => writeFileUnlocked(path, text))
|
|
132
104
|
|
|
133
105
|
/** 极简 frontmatter 解析:只认 key: value 一行一个,够用。 */
|
|
134
106
|
function parseCard(id, raw, mtimeMs = 0) {
|
|
@@ -410,23 +382,29 @@ export class HippocampusStore {
|
|
|
410
382
|
if (!card.id) throw new Error('card.id 必填')
|
|
411
383
|
card = { ...card, id: assertSafeId(card.id) }
|
|
412
384
|
const path = join(this.notesDir, `${card.id}.md`)
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
385
|
+
// 「读 prev → 合并 → 写」必须在同一把锁里:原来 get 在锁外、写才进锁,
|
|
386
|
+
// 两个进程各读到旧卡,后写的那个会把先写那个刚加的字段覆盖掉(2026-11-21 #17 实测 40/40 次丢 tags)。
|
|
387
|
+
return withLock(path, () => {
|
|
388
|
+
const prev = this.get(card.id)
|
|
389
|
+
const merged = {
|
|
390
|
+
...prev,
|
|
391
|
+
...card,
|
|
392
|
+
// tags/sourceIds/links 是数组字段;模型偶尔会传字符串,归一成数组,别让 renderCard 的 .map 崩掉。
|
|
393
|
+
tags: Array.isArray(card.tags) ? card.tags : (card.tags == null ? (prev?.tags || []) : [String(card.tags)]),
|
|
394
|
+
// ?? 只跳过 null/undefined,NaN 会原样穿透(写进 frontmatter 变成 "weight: NaN"):
|
|
395
|
+
// 缓存了一路的 NaN 最后会让这张卡既不算热也不算冷(2026-11-21 #25)。
|
|
396
|
+
weight: Number.isFinite(card.weight) ? card.weight : (Number.isFinite(prev?.weight) ? prev.weight : this.heat.initialWeight),
|
|
397
|
+
last_hit: Number.isFinite(card.last_hit) ? card.last_hit : (Number.isFinite(prev?.last_hit) ? prev.last_hit : Date.now()),
|
|
398
|
+
created: prev?.created || nowIso(),
|
|
399
|
+
// 只有明确传了 last_write 才算「写过」;touch/召回路径不传,窗口不会被召回延长。
|
|
400
|
+
last_write: card.last_write ?? prev?.last_write ?? Date.now(),
|
|
401
|
+
}
|
|
402
|
+
// 覆盖前留旧版(2026-11-11 抄自「事件流水」思路):只比内容字段,
|
|
403
|
+
// touch 改的 weight/last_hit 不算改写,不会把 history 刷满。
|
|
404
|
+
if (prev && contentChanged(prev, merged)) this.saveHistory(prev)
|
|
405
|
+
writeFileUnlocked(path, renderCard(merged))
|
|
406
|
+
return merged
|
|
407
|
+
})
|
|
430
408
|
}
|
|
431
409
|
|
|
432
410
|
/** 把旧版落进 history/<id>/<时间>.md,误合并/误覆盖时能翻回去(trash 只救删除,救不了覆盖)。 */
|
|
@@ -434,8 +412,9 @@ export class HippocampusStore {
|
|
|
434
412
|
try {
|
|
435
413
|
const dir = join(this.dir, 'history', String(card.id))
|
|
436
414
|
mkdirSync(dir, { recursive: true })
|
|
437
|
-
|
|
438
|
-
|
|
415
|
+
// 同一毫秒存两次会撞名、后一份把前一份盖掉(2026-11-21 #26):加 3 位随机尾巴,顺手 0600。
|
|
416
|
+
const stamp = `${new Date().toISOString().replace(/[:.]/g, '-')}-${Math.random().toString(36).slice(2, 5)}`
|
|
417
|
+
writeFileSync(join(dir, `${stamp}.md`), renderCard(card), { mode: 0o600 })
|
|
439
418
|
// 每张卡最多留 20 个旧版,超了从最老的开始删。
|
|
440
419
|
const files = readdirSync(dir).filter((f) => f.endsWith('.md')).sort()
|
|
441
420
|
for (const f of files.slice(0, Math.max(0, files.length - 20))) rmSync(join(dir, f), { force: true })
|
|
@@ -488,6 +467,10 @@ export class HippocampusStore {
|
|
|
488
467
|
// 窗口内不重复加成、也不重复记账。直接调 touch 不带参数时行为不变。
|
|
489
468
|
if (dedupeMs > 0) {
|
|
490
469
|
this._lastTouch ||= new Map()
|
|
470
|
+
// 只增不删会随「碰过的卡数」一直涨;超过 512 条时清掉 24 小时前的(2026-11-21 #22)。
|
|
471
|
+
if (this._lastTouch.size > 512) {
|
|
472
|
+
for (const [k, at] of this._lastTouch) if (now - at > 86_400_000) this._lastTouch.delete(k)
|
|
473
|
+
}
|
|
491
474
|
const prev = this._lastTouch.get(id) || 0
|
|
492
475
|
if (now - prev < dedupeMs) return card
|
|
493
476
|
this._lastTouch.set(id, now)
|
|
@@ -622,6 +605,25 @@ export class HippocampusStore {
|
|
|
622
605
|
// 传 -5 原本返回除前 5 行外的全部,注入会撑爆(2026-11-21 #34)。
|
|
623
606
|
const n = Number.isFinite(Number(lines)) ? Math.max(0, Math.trunc(Number(lines))) : 20
|
|
624
607
|
if (n === 0) return []
|
|
608
|
+
// 每轮注入都要取一次因果链尾;长跑几年这台文件会到几 MB,只拿最后 20 行却读全文件
|
|
609
|
+
// 会随年月线性变慢(2026-11-21 #23)。先读尾部 64KB,行数不够再退回全读。
|
|
610
|
+
const TAIL = 64 * 1024
|
|
611
|
+
try {
|
|
612
|
+
const size = statSync(this.chainPath).size
|
|
613
|
+
if (size > TAIL) {
|
|
614
|
+
const fd = openSync(this.chainPath, 'r')
|
|
615
|
+
let rows
|
|
616
|
+
try {
|
|
617
|
+
const buf = Buffer.alloc(TAIL)
|
|
618
|
+
readSync(fd, buf, 0, TAIL, size - TAIL)
|
|
619
|
+
rows = buf.toString('utf8').split('\n').filter(Boolean)
|
|
620
|
+
} finally {
|
|
621
|
+
closeSync(fd)
|
|
622
|
+
}
|
|
623
|
+
// 第一行多半是从字节中间切开的半截,丢掉;够 n 行就不回头读全文。
|
|
624
|
+
if (rows.length - 1 >= n) return rows.slice(-n)
|
|
625
|
+
}
|
|
626
|
+
} catch { /* 读尾部失败(权限/竞态)就退回全读 */ }
|
|
625
627
|
const all = readFileSync(this.chainPath, 'utf8').split('\n').filter(Boolean)
|
|
626
628
|
return all.slice(-n)
|
|
627
629
|
}
|
|
@@ -634,12 +636,15 @@ export class HippocampusStore {
|
|
|
634
636
|
// ── 自我笔记(限 40 条,满了挤掉最旧的)──────────────────────────────────
|
|
635
637
|
addSelfNote(text, at = nowIso()) {
|
|
636
638
|
this.ensure()
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
639
|
+
// 读全文件 → 追加 → 整文件重写,必须整体在锁里:两个进程各读到旧文件时会互相覆盖(2026-11-21 #20)。
|
|
640
|
+
return withLock(this.selfNotePath, () => {
|
|
641
|
+
const all = this.selfNotes()
|
|
642
|
+
all.push({ at, text: String(text).trim() })
|
|
643
|
+
const kept = all.slice(-SELF_NOTE_CAP)
|
|
644
|
+
// 整文件重写必须原子:写一半被杀,下次 selfNotes() 解析就炸,而它每轮都会被注入。
|
|
645
|
+
writeFileUnlocked(this.selfNotePath, kept.map((n) => JSON.stringify(n)).join('\n') + '\n')
|
|
646
|
+
return { kept: kept.length, cap: SELF_NOTE_CAP }
|
|
647
|
+
})
|
|
643
648
|
}
|
|
644
649
|
|
|
645
650
|
selfNotes() {
|
|
@@ -677,14 +682,17 @@ export class HippocampusStore {
|
|
|
677
682
|
}
|
|
678
683
|
|
|
679
684
|
setPersona({ base, state, note = '' } = {}) {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
if (
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
685
|
+
// 读 → 改 → 写整体加锁:两个窗口同时改人格时,后写的会把前一次的状态连同它的历史一起盖掉(2026-11-21 #21)。
|
|
686
|
+
return withLock(this.personaPath, () => {
|
|
687
|
+
const p = this.persona()
|
|
688
|
+
if (typeof base === 'string' && base.trim()) p.base = base.trim()
|
|
689
|
+
if (typeof state === 'string' && state.trim() && state.trim() !== p.state) {
|
|
690
|
+
if (p.state) p.history.push({ at: nowIso(), state: p.state, note: String(note).slice(0, 200) })
|
|
691
|
+
p.state = state.trim()
|
|
692
|
+
}
|
|
693
|
+
p.history = (p.history || []).slice(-30)
|
|
694
|
+
writeFileUnlocked(this.personaPath, JSON.stringify(p, null, 1))
|
|
695
|
+
return p
|
|
696
|
+
})
|
|
689
697
|
}
|
|
690
698
|
}
|
package/lib/stream.js
CHANGED
|
@@ -4,6 +4,7 @@ import { appendFileSync, closeSync, openSync, readFileSync, readSync, statSync,
|
|
|
4
4
|
import { createHash } from 'node:crypto'
|
|
5
5
|
import { join } from 'node:path'
|
|
6
6
|
import { tokenize, truncate, tokenWeight, containsToken } from './recall.js'
|
|
7
|
+
import { withLock } from './lock.js'
|
|
7
8
|
|
|
8
9
|
export const STREAM_FILE = 'stream.jsonl'
|
|
9
10
|
const COUNT_FILE = 'stream.count'
|
|
@@ -47,10 +48,14 @@ export function appendStream(store, { role = 'owner', text = '', sessionId = '',
|
|
|
47
48
|
const clean = String(text || '').replace(/\r/g, '').trim()
|
|
48
49
|
if (!clean) return 0
|
|
49
50
|
const body = clean.length > MAX_CHARS ? `${clean.slice(0, MAX_CHARS)}…(截断)` : clean
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
// 「读尾部算下一个号 + 追加」必须是一个原子动作:几个进程同时写时,各自读到的最后一个号一样,
|
|
52
|
+
// 落盘就会重号(2026-11-21 #29 实测 8 进程 200 行只用了 88 个号,hc_evidence 按号回查会指错行)。
|
|
53
|
+
return withLock(pathOf(store), () => {
|
|
54
|
+
const n = nextNo(store)
|
|
55
|
+
const line = JSON.stringify({ n, at: new Date(at).toISOString(), role, sid: String(sessionId || ''), text: body })
|
|
56
|
+
appendFileSync(pathOf(store), `${line}\n`, { mode: 0o600 })
|
|
57
|
+
return n
|
|
58
|
+
})
|
|
54
59
|
}
|
|
55
60
|
|
|
56
61
|
export function streamLines(store) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-memory-huixiang",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "回响 HuiXiang:给 AI 一个记得住人的本地记忆库。纯文本外文库 + 记忆重量 + 因果链 + 触发词召回,零依赖、不上云。",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|