whale-memory-core 0.26.1 → 0.26.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/lib/archive.js +11 -0
- package/lib/erase.js +22 -3
- package/lib/sqlite-store.js +4 -1
- package/package.json +1 -1
package/lib/archive.js
CHANGED
|
@@ -358,6 +358,17 @@ export function importArchive(fromDir, memDir, { force = false } = {}) {
|
|
|
358
358
|
throw new Error('Export changed during import: ' + f.name + '(导入期间备份内容发生变化)')
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
|
+
// 恢复旧备份会把擦除"撤销":备份是擦除前打的,里面没有墓碑。导入时把现有目录的
|
|
362
|
+
// 墓碑与审计账本并进新目录,否则记录回来了、撤回记录没了(验收抓到的)。
|
|
363
|
+
for (const name of ['tombstones.jsonl', 'audit.jsonl']) {
|
|
364
|
+
const live = path.join(target, name)
|
|
365
|
+
if (!fs.existsSync(live)) continue
|
|
366
|
+
const readLines = (file) => fs.readFileSync(file, 'utf8').split('\n').filter((line) => line.trim())
|
|
367
|
+
const staged = fs.existsSync(path.join(stage, name)) ? readLines(path.join(stage, name)) : []
|
|
368
|
+
const seen = new Set(staged)
|
|
369
|
+
const merged = staged.concat(readLines(live).filter((line) => !seen.has(line)))
|
|
370
|
+
fs.writeFileSync(path.join(stage, name), merged.join('\n') + (merged.length ? '\n' : ''), 'utf8')
|
|
371
|
+
}
|
|
361
372
|
const loaded = loadAll(stage, { allowSealCorrupt: true })
|
|
362
373
|
|
|
363
374
|
// 动手改目录之前先落盘日志:从这一刻起任何崩溃都能自动找回旧数据
|
package/lib/erase.js
CHANGED
|
@@ -203,7 +203,17 @@ export function eraseExperience(dir, { id, reason = '', actor = '', now = new Da
|
|
|
203
203
|
const formerIds = [...new Set(doomed.map((i) => (i.row && i.row.formerId ? String(i.row.formerId) : '')).filter(Boolean))]
|
|
204
204
|
// 墓碑记下这一条用过的所有 id(当前 id / 旧 id):按旧 id 撤回时,封存事件挂在当前 id 上,
|
|
205
205
|
// 体检要靠 ids 把两边对上(复审抓到的:只记 key 会让 doctor 永远红灯)。
|
|
206
|
-
|
|
206
|
+
// 共用旧 id:同一个旧 id 被两条以上经历用过,擦掉其中一条之后,剩下那条会"独占"这个旧 id,
|
|
207
|
+
// 旧后记就可能被算到它头上。旧数据里没有区分手段,所以这里把这种情况记进墓碑,让 doctor 提示。
|
|
208
|
+
const legacyCount = new Map()
|
|
209
|
+
for (const info of analysed.infos) {
|
|
210
|
+
if (!info.row || !info.rawFormerId) continue
|
|
211
|
+
legacyCount.set(info.rawFormerId, (legacyCount.get(info.rawFormerId) || 0) + 1)
|
|
212
|
+
}
|
|
213
|
+
const ambiguousIds = [...new Set(doomed.map((i) => i.rawFormerId).filter(Boolean))]
|
|
214
|
+
.filter((oldId) => (legacyCount.get(oldId) || 0) > 1)
|
|
215
|
+
|
|
216
|
+
const tombIds = [...new Set([key, ...analysed.target, ...formerIds])]
|
|
207
217
|
const eventIds = new Set(tombIds)
|
|
208
218
|
|
|
209
219
|
const journal = []
|
|
@@ -225,12 +235,21 @@ export function eraseExperience(dir, { id, reason = '', actor = '', now = new Da
|
|
|
225
235
|
removed.sqlite = Boolean(sq.present)
|
|
226
236
|
|
|
227
237
|
// 7) 什么都没找到:这次是"已经没了"(崩溃后重跑也走这里:顺手把残留事件清掉)
|
|
228
|
-
if (!doomed.length)
|
|
238
|
+
if (!doomed.length) {
|
|
239
|
+
// 记录行已经没了、但这次确实清掉了残留事件(崩溃后重跑会走这里):
|
|
240
|
+
// 既然真的删了东西,就要在审计里留一条,否则事后查不到任何痕迹(验收抓到的)。
|
|
241
|
+
const cleaned = Number(removed.records || 0) + Number(removed.sqliteRecords || 0) + Number(removed.sqliteEvents || 0) +
|
|
242
|
+
EVENT_FILES.reduce((n, name) => n + Number(removed[shortName(name)] || 0), 0)
|
|
243
|
+
if (cleaned > 0) {
|
|
244
|
+
try { audit.record(dir, { actor: actorText, action: 'erase', ids: [key], note: 'already-gone-cleanup' }) } catch { /* 审计失败不改变"已经删掉了"这个事实 */ }
|
|
245
|
+
}
|
|
246
|
+
return { ok: true, id: key, alreadyGone: true, at: '', tombstone: false, removed }
|
|
247
|
+
}
|
|
229
248
|
|
|
230
249
|
// 8) 墓碑:删除成功之后才写(墓碑不会早于删除出现,谎报"已撤回")
|
|
231
250
|
// ids 记下"这一条经历在库里用过的所有 id"(当前 id + 旧 id),体检核对封存事件时用
|
|
232
251
|
try {
|
|
233
|
-
tombstones.append(dir, { v: 1, id: key, ids: tombIds, at, reason: String(reason).trim(), actor: actorText, hash })
|
|
252
|
+
tombstones.append(dir, { v: 1, id: key, ids: tombIds, at, reason: String(reason).trim(), actor: actorText, hash, ...(ambiguousIds.length ? { ambiguousIds } : {}) })
|
|
234
253
|
} catch (error) {
|
|
235
254
|
throw coded('墓碑写失败:' + String(error && error.message ? error.message : error) + '。(tombstone write failed)', 'ERASE_TOMBSTONE_FAILED', { cause: error })
|
|
236
255
|
}
|
package/lib/sqlite-store.js
CHANGED
|
@@ -312,7 +312,8 @@ export function readRecords(db) {
|
|
|
312
312
|
// 2. eraseFromStore():一个事务里删 records + events(seal 留着),提交后按需刷新 sources。
|
|
313
313
|
// 墓碑写失败不回滚删除,这个取舍写在 docs/erase-audit-zh.md 里。
|
|
314
314
|
//
|
|
315
|
-
// 注意:SQLite
|
|
315
|
+
// 注意:SQLite 删除行之后,旧字节可能还留在文件页里。擦除路径会先开 secure_delete(见 eraseFromStore);
|
|
316
|
+
// 其余删除路径不保证,所以"按原始字节能不能搜到正文"只对擦除做承诺。
|
|
316
317
|
// 这不是"没擦掉",是"磁盘上的碎片不追溯"——和备份里的历史副本一样,文档里写明不承诺。
|
|
317
318
|
|
|
318
319
|
// 库文件本身的存在性/形状:不存在 = null;是目录 / 0 字节 = 抛(不能当"没有库"糊弄过去)
|
|
@@ -388,6 +389,8 @@ export function eraseFromStore(dir, ids, { refreshSources = false } = {}) {
|
|
|
388
389
|
try {
|
|
389
390
|
let records = 0
|
|
390
391
|
let events = 0
|
|
392
|
+
// 擦除要把旧页里的字节也抹掉:否则被删的正文还留在库文件里(验收抓到的)
|
|
393
|
+
db.exec('PRAGMA secure_delete = ON')
|
|
391
394
|
db.exec('BEGIN IMMEDIATE')
|
|
392
395
|
try {
|
|
393
396
|
const delRecord = db.prepare('DELETE FROM records WHERE id = ?')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.2",
|
|
4
4
|
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|