@modusensus/dsh-mneme 0.2.8 → 0.2.10
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 +3 -3
- package/lib/config.js +8 -1
- package/lib/dream.js +60 -6
- package/lib/mirror.js +14 -2
- package/lib/service.js +23 -4
- package/lib/store.js +96 -0
- package/package.json +1 -1
- package/src/config.js +8 -1
- package/src/dream.js +60 -6
- package/src/mirror.js +14 -2
- package/src/service.js +23 -4
- package/src/store.js +96 -0
- package/test/conflict-freeze.test.js +290 -0
- package/test/dream.test.js +126 -0
- package/test/mirror-edit-digest.test.js +185 -0
- package/test/store.test.js +67 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@modusensus/dsh-mneme)
|
|
6
6
|
[](LICENSE)
|
|
7
7
|
[](https://github.com/awesome-dsh-plugin/awesome-dsh-plugin)
|
|
8
|
-
[](https://github.com/modusensus/dsh-mneme)
|
|
9
9
|
|
|
10
10
|
> 给 DeepSeek Harness 的跨会话记忆插件:让 Agent 记住你、记住项目、自动整理记忆。**Mneme**(Μνήμη)——希腊记忆女神 Mnemosyne 之名,掌管记忆与梦境,正如 autoDream 在后台巩固记忆。
|
|
11
11
|
|
|
@@ -225,7 +225,7 @@ src/
|
|
|
225
225
|
lib/
|
|
226
226
|
├── client.js # Web 面板(手写 ModuleLoader bundle)
|
|
227
227
|
└── *.js # src 的同步分发产物
|
|
228
|
-
test/ #
|
|
228
|
+
test/ # 363 个 node:test 测试(含审计与三轴线压测不变量)
|
|
229
229
|
scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压测 · sync-lib.js 同步
|
|
230
230
|
```
|
|
231
231
|
|
|
@@ -234,7 +234,7 @@ scripts/ # e2e-dsh.js 端到端演示 · stress-dsh.js 三轴线压
|
|
|
234
234
|
```bash
|
|
235
235
|
cd dsh-mneme
|
|
236
236
|
npm install # 安装 peer 依赖(以 devDependencies 形式,用于本地测试)
|
|
237
|
-
npm test # 运行
|
|
237
|
+
npm test # 运行 363 个测试
|
|
238
238
|
npm run stress # 三轴线压测:长会话检索 / 冲突仲裁 / 多 Agent 并发(离线 mock LLM)
|
|
239
239
|
npm run sync # 把 src/ 同步到 lib/(发布时由 prepack 钩子自动执行)
|
|
240
240
|
```
|
package/lib/config.js
CHANGED
|
@@ -67,5 +67,12 @@ export const Config = z.object({
|
|
|
67
67
|
reflectionUpdateEnabled: z.boolean().default(true),
|
|
68
68
|
reflectionFailureTracking: z.boolean().default(true),
|
|
69
69
|
reflectionUpdateMaxPerRun: z.natural().min(0).max(5).default(2),
|
|
70
|
-
reflectionUpdateMinAgeHours: z.natural().min(0).max(168).default(24)
|
|
70
|
+
reflectionUpdateMinAgeHours: z.natural().min(0).max(168).default(24),
|
|
71
|
+
|
|
72
|
+
// --- conflict freeze: manual review for conflicting memories (v0.2.1) ---
|
|
73
|
+
// Opt-in by default: when true, conflicting memories are not auto-merged
|
|
74
|
+
// and are marked as pending manual review instead.
|
|
75
|
+
conflictFreezeEnabled: z.boolean().default(false),
|
|
76
|
+
// Maximum number of frozen conflicts to keep pending for manual review.
|
|
77
|
+
conflictFreezeMaxPending: z.natural().min(1).max(1000).default(100),
|
|
71
78
|
});
|
package/lib/dream.js
CHANGED
|
@@ -350,6 +350,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
350
350
|
const route = resolveRoute(ctx, config, logger);
|
|
351
351
|
const runId = randomUUID();
|
|
352
352
|
const snapshotHash = hashSnapshot([...snapshot.values()]);
|
|
353
|
+
// Conflict freeze (opt-in): when enabled, conflict decisions are parked for
|
|
354
|
+
// manual review instead of auto-adjudicated. Read once up front so the
|
|
355
|
+
// prompt hint and the apply-split agree on the same gate.
|
|
356
|
+
const freezeEnabled = config.conflictFreezeEnabled === true;
|
|
353
357
|
// Every exit (success or failure) funnels through `finish`, which writes
|
|
354
358
|
// the audit row + receipt. A record failure is logged, never thrown —
|
|
355
359
|
// auditing must not break the consolidation path. Failed runs still
|
|
@@ -440,6 +444,12 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
440
444
|
).join("\n");
|
|
441
445
|
}
|
|
442
446
|
|
|
447
|
+
// Freeze-aware prompt: in freeze mode the conflict branch still outputs
|
|
448
|
+
// winner/loser (validation requires them) but they are treated as tentative
|
|
449
|
+
// candidates — the human makes the final call, not the model.
|
|
450
|
+
const consolidationPrompt = freezeEnabled
|
|
451
|
+
? CONSOLIDATION_PROMPT + `\n\n当前为「冲突冻结」模式:检测到内容矛盾的条目时,仍请输出 conflict,并以 winner/loser 作为候选、reason 说明理由;冲突不会被自动裁决,而会冻结待人工确认。`
|
|
452
|
+
: CONSOLIDATION_PROMPT;
|
|
443
453
|
let decisionText;
|
|
444
454
|
try {
|
|
445
455
|
decisionText = await streamText(ctx, {
|
|
@@ -448,7 +458,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
448
458
|
purpose: "compaction",
|
|
449
459
|
maxTokens: config.dreamMaxTokens ?? 4096,
|
|
450
460
|
messages: [
|
|
451
|
-
{ role: "system", content: [{ type: "text", text:
|
|
461
|
+
{ role: "system", content: [{ type: "text", text: consolidationPrompt }] },
|
|
452
462
|
{ role: "user", content: [{ type: "text", text: listText }] }
|
|
453
463
|
]
|
|
454
464
|
});
|
|
@@ -492,10 +502,46 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
492
502
|
}
|
|
493
503
|
}
|
|
494
504
|
|
|
505
|
+
// Conflict freeze (opt-in): when enabled, conflict decisions are not
|
|
506
|
+
// auto-adjudicated — no winner kept, no loser archived. The pair is parked
|
|
507
|
+
// in conflict_pending for human review instead. Best-effort: a store
|
|
508
|
+
// failure here must never block the run (fail-safe — the memories are left
|
|
509
|
+
// untouched and nothing is arbitrated). The cap (conflictFreezeMaxPending)
|
|
510
|
+
// bounds the review queue; overflow is skipped with a warning.
|
|
511
|
+
let frozenCount = 0;
|
|
512
|
+
const frozenIds = [];
|
|
513
|
+
const applyList = freezeEnabled ? decisions.filter((d) => d.action !== "conflict") : decisions;
|
|
514
|
+
if (freezeEnabled) {
|
|
515
|
+
const conflictsToFreeze = decisions.filter((d) => d.action === "conflict");
|
|
516
|
+
if (conflictsToFreeze.length > 0) {
|
|
517
|
+
try {
|
|
518
|
+
const maxPending = Number.isInteger(config.conflictFreezeMaxPending) ? config.conflictFreezeMaxPending : 100;
|
|
519
|
+
const pendingNow = service.countConflictPending();
|
|
520
|
+
const budget = Math.max(0, maxPending - pendingNow);
|
|
521
|
+
const toFreeze = conflictsToFreeze.slice(0, budget);
|
|
522
|
+
if (conflictsToFreeze.length > budget) {
|
|
523
|
+
logger?.warn?.(`dsh-mneme dream: conflict freeze queue full (${pendingNow}/${maxPending}), skipped ${conflictsToFreeze.length - budget} conflict(s)`);
|
|
524
|
+
}
|
|
525
|
+
for (const d of toFreeze) {
|
|
526
|
+
try {
|
|
527
|
+
service.saveConflictPending({ run_id: runId, memory_a: d.winner, memory_b: d.loser, reason: d.reason });
|
|
528
|
+
frozenCount++;
|
|
529
|
+
frozenIds.push(d.winner, d.loser);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
logger?.warn?.(`dsh-mneme dream: failed to freeze conflict ${d.winner}/${d.loser}: ${String(error)}`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
} catch (error) {
|
|
535
|
+
logger?.warn?.(`dsh-mneme dream: conflict freeze lookup failed: ${String(error)}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
495
540
|
// CAS-guarded, per-decision-transactional apply against the run snapshot:
|
|
496
541
|
// a target changed during the LLM call is skipped and reported as a
|
|
497
|
-
// conflict instead of being overwritten (item ①).
|
|
498
|
-
|
|
542
|
+
// conflict instead of being overwritten (item ①). Frozen conflicts are
|
|
543
|
+
// excluded from this list (they are parked, not applied).
|
|
544
|
+
const { applied, conflicts, failures, committed } = applyDecisions(applyList, service, logger, snapshot);
|
|
499
545
|
// Per-record receipt chain: one row per actually-committed merge/conflict/
|
|
500
546
|
// update verdict, stamped with the decision-basis digest + idempotency
|
|
501
547
|
// counters (count_before → count_after). Written here, before the run audit
|
|
@@ -521,18 +567,25 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
521
567
|
// claim "merge-archived" (item ②). Conflicts/failures ride along so the
|
|
522
568
|
// audit row records why the run diverged.
|
|
523
569
|
const outcome = { ...buildOutcome(committed), conflicts, failures };
|
|
570
|
+
// Frozen conflicts were not adjudicated: mark both sides pending in the
|
|
571
|
+
// per-id outcome so the audit row shows they were parked, not skipped.
|
|
572
|
+
if (frozenIds.length) {
|
|
573
|
+
for (const id of frozenIds) outcome.byId[id] = "conflict-pending";
|
|
574
|
+
}
|
|
524
575
|
// Decisions validated but not fully committed → reconcile (not ok).
|
|
525
576
|
const partial = conflicts.length > 0 || failures.length > 0;
|
|
526
577
|
// No decision landed (all-keep, or every decision skipped as an idempotent
|
|
527
578
|
// replay) → nothing substantive changed. Distinct from a success: such a
|
|
528
579
|
// run must never be reported as ok, or the audit claims work that never
|
|
529
580
|
// happened and the scheduler refreshes the baseline on a false positive.
|
|
530
|
-
|
|
581
|
+
// Frozen conflicts are substantive output (parked for review), so a run
|
|
582
|
+
// that only froze conflicts is not a noop.
|
|
583
|
+
const noChange = frozenCount === 0 && applied === 0 && committed.every((c) => c.action === "keep");
|
|
531
584
|
|
|
532
585
|
// Keep the vector index consistent with the post-dream store state.
|
|
533
586
|
if (semantic?.embedder && semantic?.vectorIndex) {
|
|
534
587
|
try {
|
|
535
|
-
await maintainIndexAfterDream(
|
|
588
|
+
await maintainIndexAfterDream(applyList, service, semantic);
|
|
536
589
|
} catch (error) {
|
|
537
590
|
logger?.warn?.(`[dsh-mneme] dream index maintenance failed: ${String(error)}`);
|
|
538
591
|
}
|
|
@@ -554,7 +607,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
554
607
|
});
|
|
555
608
|
} catch (error) {
|
|
556
609
|
logger?.warn?.(`dsh-mneme dream: summary llm call failed: ${String(error)}`);
|
|
557
|
-
return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, summary: false });
|
|
610
|
+
return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, frozen: frozenCount, summary: false });
|
|
558
611
|
}
|
|
559
612
|
let summaryStored = false;
|
|
560
613
|
if (summaryText !== undefined && summaryText.trim()) {
|
|
@@ -601,6 +654,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
601
654
|
outcome,
|
|
602
655
|
conflicts,
|
|
603
656
|
failures,
|
|
657
|
+
frozen: frozenCount,
|
|
604
658
|
summary: summaryStored
|
|
605
659
|
});
|
|
606
660
|
}
|
package/lib/mirror.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
|
|
4
5
|
export const TYPE_FILE = {
|
|
@@ -21,6 +22,13 @@ function unescape(text) {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
function renderMemory(m) {
|
|
25
|
+
// last-rendered digest baseline: sha256(title \x00 content). service.js
|
|
26
|
+
// compares the file hash against this to tell "untouched by a human" (machine
|
|
27
|
+
// write wins) apart from a real human edit, so a not-yet-re-rendered store
|
|
28
|
+
// update is not misread as a concurrent human edit.
|
|
29
|
+
const digest = createHash("sha256")
|
|
30
|
+
.update(`${m.title}\x00${m.content}`)
|
|
31
|
+
.digest("hex");
|
|
24
32
|
const lines = [];
|
|
25
33
|
lines.push(`## ${esc(m.title)}`);
|
|
26
34
|
lines.push("");
|
|
@@ -31,6 +39,7 @@ function renderMemory(m) {
|
|
|
31
39
|
lines.push(`- **更新时间**: ${m.updated_at}`);
|
|
32
40
|
if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
|
|
33
41
|
lines.push("");
|
|
42
|
+
lines.push(`<!-- mirror-digest: ${digest} -->`);
|
|
34
43
|
lines.push(m.content);
|
|
35
44
|
lines.push("");
|
|
36
45
|
lines.push("---");
|
|
@@ -86,7 +95,8 @@ export function createMirror(dir) {
|
|
|
86
95
|
let body = text
|
|
87
96
|
.slice(blockStart, blockEnd)
|
|
88
97
|
.replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
|
|
89
|
-
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
98
|
+
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
99
|
+
.replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
|
|
90
100
|
const separators = [...body.matchAll(/^---\s*$/gm)];
|
|
91
101
|
const lastSep = separators[separators.length - 1];
|
|
92
102
|
if (lastSep) body = body.slice(0, lastSep.index);
|
|
@@ -97,11 +107,13 @@ export function createMirror(dir) {
|
|
|
97
107
|
// during a three-way merge of human edits (see service.syncMirror).
|
|
98
108
|
const block = text.slice(blockStart, blockEnd);
|
|
99
109
|
const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
|
|
110
|
+
const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
|
|
100
111
|
edits.push({
|
|
101
112
|
id: anchor[1],
|
|
102
113
|
title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
|
|
103
114
|
content: body,
|
|
104
|
-
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined
|
|
115
|
+
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
|
|
116
|
+
digest: digestMatch ? digestMatch[1] : undefined
|
|
105
117
|
});
|
|
106
118
|
|
|
107
119
|
const lineEnd = text.indexOf("\n", blockStart);
|
package/lib/service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
|
|
4
4
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
@@ -349,8 +349,21 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
349
349
|
const humanChanged = (typeof edit.title === "string" && edit.title !== m.title)
|
|
350
350
|
|| (typeof edit.content === "string" && edit.content !== m.content);
|
|
351
351
|
if (!humanChanged) { result.push(m); continue; }
|
|
352
|
-
//
|
|
353
|
-
//
|
|
352
|
+
// 判断文件是否被人工动过:digest 存在且匹配则无人触碰,否则视为人工动过。
|
|
353
|
+
// digest 是渲染时对 sha256(title \x00 content) 的记录;机器 store 更新后
|
|
354
|
+
// 镜像还没重渲染时读到旧内容,digest 仍匹配 → 机器 wins,不会误判为
|
|
355
|
+
// 并发人工编辑导致机器写丢失 + 伪冲突标记。
|
|
356
|
+
const digestMatches = typeof edit.digest === "string"
|
|
357
|
+
&& typeof edit.title === "string"
|
|
358
|
+
&& typeof edit.content === "string"
|
|
359
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
360
|
+
if (digestMatches) {
|
|
361
|
+
// 无人触碰,机器 wins,走原样
|
|
362
|
+
result.push(m);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
// 人工动过(digest 不存在=老文件/手工文件保守视为人工动过),走三方合并
|
|
366
|
+
// (保留现有 storeChanged 逻辑)
|
|
354
367
|
const storeChanged = edit.updated_at !== undefined && m.updated_at !== edit.updated_at;
|
|
355
368
|
if (storeChanged) {
|
|
356
369
|
const marker = `\n\n> ⚠️ 并发冲突:人工编辑 vs 记忆库并发更新(${m.updated_at})\n> 记忆库版本:${m.content}`;
|
|
@@ -479,6 +492,12 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
479
492
|
// audit write, never a write-hook-triggering memory mutation).
|
|
480
493
|
saveReceipt: (r) => store.saveReceipt(r),
|
|
481
494
|
getReceipt: (id) => store.getReceipt(id),
|
|
482
|
-
listReceipts: (opts) => store.listReceipts(opts)
|
|
495
|
+
listReceipts: (opts) => store.listReceipts(opts),
|
|
496
|
+
// Conflict freeze bookkeeping (same semantics as the audit passthroughs
|
|
497
|
+
// above: an audit write, never a write-hook-triggering memory mutation).
|
|
498
|
+
saveConflictPending: (r) => store.saveConflictPending(r),
|
|
499
|
+
listConflictPending: (opts) => store.listConflictPending(opts),
|
|
500
|
+
resolveConflictPending: (id, o) => store.resolveConflictPending(id, o),
|
|
501
|
+
countConflictPending: () => store.countConflictPending()
|
|
483
502
|
};
|
|
484
503
|
}
|
package/lib/store.js
CHANGED
|
@@ -100,6 +100,24 @@ CREATE TABLE IF NOT EXISTS receipt_chain (
|
|
|
100
100
|
);
|
|
101
101
|
CREATE INDEX IF NOT EXISTS idx_receipt_chain_record ON receipt_chain(record_id);
|
|
102
102
|
CREATE INDEX IF NOT EXISTS idx_receipt_chain_run ON receipt_chain(run_id);
|
|
103
|
+
|
|
104
|
+
-- conflict_pending: conflicts parked for manual review (conflict freeze mode,
|
|
105
|
+
-- opt-in via config.conflictFreezeEnabled). When enabled, the dream layer does
|
|
106
|
+
-- NOT auto-adjudicate winner/loser — the conflicting pair is parked here until
|
|
107
|
+
-- a human reviews it. resolveConflictPending stamps resolved_at (plus the chosen
|
|
108
|
+
-- winner) so the review action stays auditable. Like the other audit tables this
|
|
109
|
+
-- is bookkeeping: it never triggers write hooks.
|
|
110
|
+
CREATE TABLE IF NOT EXISTS conflict_pending (
|
|
111
|
+
id TEXT PRIMARY KEY,
|
|
112
|
+
run_id TEXT,
|
|
113
|
+
memory_a TEXT NOT NULL,
|
|
114
|
+
memory_b TEXT NOT NULL,
|
|
115
|
+
reason TEXT,
|
|
116
|
+
created_at TEXT NOT NULL,
|
|
117
|
+
resolved_at TEXT,
|
|
118
|
+
resolved_winner TEXT
|
|
119
|
+
);
|
|
120
|
+
CREATE INDEX IF NOT EXISTS idx_conflict_pending_unresolved ON conflict_pending(resolved_at);
|
|
103
121
|
`;
|
|
104
122
|
|
|
105
123
|
const TYPES = new Set(["preference", "project", "decision", "history", "summary"]);
|
|
@@ -183,6 +201,20 @@ function toReceipt(row) {
|
|
|
183
201
|
};
|
|
184
202
|
}
|
|
185
203
|
|
|
204
|
+
function toConflictPending(row) {
|
|
205
|
+
if (!row) return undefined;
|
|
206
|
+
return {
|
|
207
|
+
id: row.id,
|
|
208
|
+
run_id: row.run_id ?? undefined,
|
|
209
|
+
memory_a: row.memory_a,
|
|
210
|
+
memory_b: row.memory_b,
|
|
211
|
+
reason: row.reason ?? undefined,
|
|
212
|
+
created_at: row.created_at,
|
|
213
|
+
resolved_at: row.resolved_at ?? undefined,
|
|
214
|
+
resolved_winner: row.resolved_winner ?? undefined
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
186
218
|
function toRecallRun(row) {
|
|
187
219
|
if (!row) return undefined;
|
|
188
220
|
return {
|
|
@@ -702,6 +734,66 @@ export function createStore(path) {
|
|
|
702
734
|
return db.prepare("DELETE FROM failure_memories WHERE created_at < ?").run(before).changes;
|
|
703
735
|
}
|
|
704
736
|
|
|
737
|
+
// --- conflict freeze: pending manual review ------------------------------
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
* Park a detected conflict for human review (conflict freeze mode). The pair
|
|
741
|
+
* order is normalized (sorted by id) so the same two memories are only ever
|
|
742
|
+
* pending once — a re-detection in a later dream run is a no-op, never a
|
|
743
|
+
* duplicate queue entry. Returns the pending row (freshly inserted, or the
|
|
744
|
+
* existing unresolved row when the pair is already pending).
|
|
745
|
+
*/
|
|
746
|
+
function saveConflictPending({ run_id, memory_a, memory_b, reason }) {
|
|
747
|
+
const [a, b] = [memory_a, memory_b].sort();
|
|
748
|
+
const existing = db.prepare(
|
|
749
|
+
"SELECT * FROM conflict_pending WHERE memory_a = ? AND memory_b = ? AND resolved_at IS NULL LIMIT 1"
|
|
750
|
+
).get(a, b);
|
|
751
|
+
if (existing) return toConflictPending(existing);
|
|
752
|
+
const id = randomUUID();
|
|
753
|
+
const now = nowIso();
|
|
754
|
+
db.prepare(
|
|
755
|
+
`INSERT INTO conflict_pending (id, run_id, memory_a, memory_b, reason, created_at)
|
|
756
|
+
VALUES (?, ?, ?, ?, ?, ?)`
|
|
757
|
+
).run(id, run_id ?? null, a, b, reason ?? null, now);
|
|
758
|
+
return toConflictPending(db.prepare("SELECT * FROM conflict_pending WHERE id = ?").get(id));
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
/**
|
|
762
|
+
* List pending conflicts, newest first. Unresolved rows only by default;
|
|
763
|
+
* pass includeResolved to include resolved ones (audit view).
|
|
764
|
+
*/
|
|
765
|
+
function listConflictPending({ limit = 50, offset = 0, includeResolved = false } = {}) {
|
|
766
|
+
const { limit: lim, offset: off } = sanitizePage(limit, offset, 50);
|
|
767
|
+
const clauses = [];
|
|
768
|
+
const params = [];
|
|
769
|
+
if (!includeResolved) clauses.push("resolved_at IS NULL");
|
|
770
|
+
const where = clauses.length ? `WHERE ${clauses.join(" AND ")}` : "";
|
|
771
|
+
const rows = db.prepare(
|
|
772
|
+
`SELECT * FROM conflict_pending ${where} ORDER BY created_at DESC, id LIMIT ? OFFSET ?`
|
|
773
|
+
).all(...params, lim, off);
|
|
774
|
+
return rows.map(toConflictPending);
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Mark a pending conflict as reviewed. winner (optional) records which side
|
|
779
|
+
* the human chose, keeping the resolution auditable. Returns the updated row,
|
|
780
|
+
* or undefined for an unknown id.
|
|
781
|
+
*/
|
|
782
|
+
function resolveConflictPending(id, { winner } = {}) {
|
|
783
|
+
const row = db.prepare("SELECT * FROM conflict_pending WHERE id = ?").get(id);
|
|
784
|
+
if (!row) return undefined;
|
|
785
|
+
db.prepare("UPDATE conflict_pending SET resolved_at = ?, resolved_winner = ? WHERE id = ?")
|
|
786
|
+
.run(nowIso(), winner ?? null, id);
|
|
787
|
+
return toConflictPending(db.prepare("SELECT * FROM conflict_pending WHERE id = ?").get(id));
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/** Number of unresolved (awaiting review) pending conflicts. */
|
|
791
|
+
function countConflictPending() {
|
|
792
|
+
return db.prepare(
|
|
793
|
+
"SELECT count(*) AS c FROM conflict_pending WHERE resolved_at IS NULL"
|
|
794
|
+
).get().c;
|
|
795
|
+
}
|
|
796
|
+
|
|
705
797
|
function getFailureStats({ since } = {}) {
|
|
706
798
|
const clause = since ? "WHERE created_at >= ?" : "";
|
|
707
799
|
const params = since ? [since] : [];
|
|
@@ -744,6 +836,10 @@ export function createStore(path) {
|
|
|
744
836
|
listFailures,
|
|
745
837
|
getFailureStats,
|
|
746
838
|
deleteOldFailures,
|
|
839
|
+
saveConflictPending,
|
|
840
|
+
listConflictPending,
|
|
841
|
+
resolveConflictPending,
|
|
842
|
+
countConflictPending,
|
|
747
843
|
close() {
|
|
748
844
|
db.close();
|
|
749
845
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modusensus/dsh-mneme",
|
|
3
3
|
"description": "Cross-session memory plugin for DeepSeek Harness with autoDream consolidation: SQLite store, Markdown mirrors, 6 model tools, automatic injection, session summarization, user profile/rules, custom slash commands, vector (semantic) search, and a Web GUI panel",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.10",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "lib/index.js",
|
package/src/config.js
CHANGED
|
@@ -67,5 +67,12 @@ export const Config = z.object({
|
|
|
67
67
|
reflectionUpdateEnabled: z.boolean().default(true),
|
|
68
68
|
reflectionFailureTracking: z.boolean().default(true),
|
|
69
69
|
reflectionUpdateMaxPerRun: z.natural().min(0).max(5).default(2),
|
|
70
|
-
reflectionUpdateMinAgeHours: z.natural().min(0).max(168).default(24)
|
|
70
|
+
reflectionUpdateMinAgeHours: z.natural().min(0).max(168).default(24),
|
|
71
|
+
|
|
72
|
+
// --- conflict freeze: manual review for conflicting memories (v0.2.1) ---
|
|
73
|
+
// Opt-in by default: when true, conflicting memories are not auto-merged
|
|
74
|
+
// and are marked as pending manual review instead.
|
|
75
|
+
conflictFreezeEnabled: z.boolean().default(false),
|
|
76
|
+
// Maximum number of frozen conflicts to keep pending for manual review.
|
|
77
|
+
conflictFreezeMaxPending: z.natural().min(1).max(1000).default(100),
|
|
71
78
|
});
|
package/src/dream.js
CHANGED
|
@@ -350,6 +350,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
350
350
|
const route = resolveRoute(ctx, config, logger);
|
|
351
351
|
const runId = randomUUID();
|
|
352
352
|
const snapshotHash = hashSnapshot([...snapshot.values()]);
|
|
353
|
+
// Conflict freeze (opt-in): when enabled, conflict decisions are parked for
|
|
354
|
+
// manual review instead of auto-adjudicated. Read once up front so the
|
|
355
|
+
// prompt hint and the apply-split agree on the same gate.
|
|
356
|
+
const freezeEnabled = config.conflictFreezeEnabled === true;
|
|
353
357
|
// Every exit (success or failure) funnels through `finish`, which writes
|
|
354
358
|
// the audit row + receipt. A record failure is logged, never thrown —
|
|
355
359
|
// auditing must not break the consolidation path. Failed runs still
|
|
@@ -440,6 +444,12 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
440
444
|
).join("\n");
|
|
441
445
|
}
|
|
442
446
|
|
|
447
|
+
// Freeze-aware prompt: in freeze mode the conflict branch still outputs
|
|
448
|
+
// winner/loser (validation requires them) but they are treated as tentative
|
|
449
|
+
// candidates — the human makes the final call, not the model.
|
|
450
|
+
const consolidationPrompt = freezeEnabled
|
|
451
|
+
? CONSOLIDATION_PROMPT + `\n\n当前为「冲突冻结」模式:检测到内容矛盾的条目时,仍请输出 conflict,并以 winner/loser 作为候选、reason 说明理由;冲突不会被自动裁决,而会冻结待人工确认。`
|
|
452
|
+
: CONSOLIDATION_PROMPT;
|
|
443
453
|
let decisionText;
|
|
444
454
|
try {
|
|
445
455
|
decisionText = await streamText(ctx, {
|
|
@@ -448,7 +458,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
448
458
|
purpose: "compaction",
|
|
449
459
|
maxTokens: config.dreamMaxTokens ?? 4096,
|
|
450
460
|
messages: [
|
|
451
|
-
{ role: "system", content: [{ type: "text", text:
|
|
461
|
+
{ role: "system", content: [{ type: "text", text: consolidationPrompt }] },
|
|
452
462
|
{ role: "user", content: [{ type: "text", text: listText }] }
|
|
453
463
|
]
|
|
454
464
|
});
|
|
@@ -492,10 +502,46 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
492
502
|
}
|
|
493
503
|
}
|
|
494
504
|
|
|
505
|
+
// Conflict freeze (opt-in): when enabled, conflict decisions are not
|
|
506
|
+
// auto-adjudicated — no winner kept, no loser archived. The pair is parked
|
|
507
|
+
// in conflict_pending for human review instead. Best-effort: a store
|
|
508
|
+
// failure here must never block the run (fail-safe — the memories are left
|
|
509
|
+
// untouched and nothing is arbitrated). The cap (conflictFreezeMaxPending)
|
|
510
|
+
// bounds the review queue; overflow is skipped with a warning.
|
|
511
|
+
let frozenCount = 0;
|
|
512
|
+
const frozenIds = [];
|
|
513
|
+
const applyList = freezeEnabled ? decisions.filter((d) => d.action !== "conflict") : decisions;
|
|
514
|
+
if (freezeEnabled) {
|
|
515
|
+
const conflictsToFreeze = decisions.filter((d) => d.action === "conflict");
|
|
516
|
+
if (conflictsToFreeze.length > 0) {
|
|
517
|
+
try {
|
|
518
|
+
const maxPending = Number.isInteger(config.conflictFreezeMaxPending) ? config.conflictFreezeMaxPending : 100;
|
|
519
|
+
const pendingNow = service.countConflictPending();
|
|
520
|
+
const budget = Math.max(0, maxPending - pendingNow);
|
|
521
|
+
const toFreeze = conflictsToFreeze.slice(0, budget);
|
|
522
|
+
if (conflictsToFreeze.length > budget) {
|
|
523
|
+
logger?.warn?.(`dsh-mneme dream: conflict freeze queue full (${pendingNow}/${maxPending}), skipped ${conflictsToFreeze.length - budget} conflict(s)`);
|
|
524
|
+
}
|
|
525
|
+
for (const d of toFreeze) {
|
|
526
|
+
try {
|
|
527
|
+
service.saveConflictPending({ run_id: runId, memory_a: d.winner, memory_b: d.loser, reason: d.reason });
|
|
528
|
+
frozenCount++;
|
|
529
|
+
frozenIds.push(d.winner, d.loser);
|
|
530
|
+
} catch (error) {
|
|
531
|
+
logger?.warn?.(`dsh-mneme dream: failed to freeze conflict ${d.winner}/${d.loser}: ${String(error)}`);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
} catch (error) {
|
|
535
|
+
logger?.warn?.(`dsh-mneme dream: conflict freeze lookup failed: ${String(error)}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
495
540
|
// CAS-guarded, per-decision-transactional apply against the run snapshot:
|
|
496
541
|
// a target changed during the LLM call is skipped and reported as a
|
|
497
|
-
// conflict instead of being overwritten (item ①).
|
|
498
|
-
|
|
542
|
+
// conflict instead of being overwritten (item ①). Frozen conflicts are
|
|
543
|
+
// excluded from this list (they are parked, not applied).
|
|
544
|
+
const { applied, conflicts, failures, committed } = applyDecisions(applyList, service, logger, snapshot);
|
|
499
545
|
// Per-record receipt chain: one row per actually-committed merge/conflict/
|
|
500
546
|
// update verdict, stamped with the decision-basis digest + idempotency
|
|
501
547
|
// counters (count_before → count_after). Written here, before the run audit
|
|
@@ -521,18 +567,25 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
521
567
|
// claim "merge-archived" (item ②). Conflicts/failures ride along so the
|
|
522
568
|
// audit row records why the run diverged.
|
|
523
569
|
const outcome = { ...buildOutcome(committed), conflicts, failures };
|
|
570
|
+
// Frozen conflicts were not adjudicated: mark both sides pending in the
|
|
571
|
+
// per-id outcome so the audit row shows they were parked, not skipped.
|
|
572
|
+
if (frozenIds.length) {
|
|
573
|
+
for (const id of frozenIds) outcome.byId[id] = "conflict-pending";
|
|
574
|
+
}
|
|
524
575
|
// Decisions validated but not fully committed → reconcile (not ok).
|
|
525
576
|
const partial = conflicts.length > 0 || failures.length > 0;
|
|
526
577
|
// No decision landed (all-keep, or every decision skipped as an idempotent
|
|
527
578
|
// replay) → nothing substantive changed. Distinct from a success: such a
|
|
528
579
|
// run must never be reported as ok, or the audit claims work that never
|
|
529
580
|
// happened and the scheduler refreshes the baseline on a false positive.
|
|
530
|
-
|
|
581
|
+
// Frozen conflicts are substantive output (parked for review), so a run
|
|
582
|
+
// that only froze conflicts is not a noop.
|
|
583
|
+
const noChange = frozenCount === 0 && applied === 0 && committed.every((c) => c.action === "keep");
|
|
531
584
|
|
|
532
585
|
// Keep the vector index consistent with the post-dream store state.
|
|
533
586
|
if (semantic?.embedder && semantic?.vectorIndex) {
|
|
534
587
|
try {
|
|
535
|
-
await maintainIndexAfterDream(
|
|
588
|
+
await maintainIndexAfterDream(applyList, service, semantic);
|
|
536
589
|
} catch (error) {
|
|
537
590
|
logger?.warn?.(`[dsh-mneme] dream index maintenance failed: ${String(error)}`);
|
|
538
591
|
}
|
|
@@ -554,7 +607,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
554
607
|
});
|
|
555
608
|
} catch (error) {
|
|
556
609
|
logger?.warn?.(`dsh-mneme dream: summary llm call failed: ${String(error)}`);
|
|
557
|
-
return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, summary: false });
|
|
610
|
+
return finish({ ok: false, error: "llm failed", applied, decisions: auditDecisions, outcome, frozen: frozenCount, summary: false });
|
|
558
611
|
}
|
|
559
612
|
let summaryStored = false;
|
|
560
613
|
if (summaryText !== undefined && summaryText.trim()) {
|
|
@@ -601,6 +654,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
|
|
|
601
654
|
outcome,
|
|
602
655
|
conflicts,
|
|
603
656
|
failures,
|
|
657
|
+
frozen: frozenCount,
|
|
604
658
|
summary: summaryStored
|
|
605
659
|
});
|
|
606
660
|
}
|
package/src/mirror.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { mkdirSync, readFileSync, writeFileSync, existsSync, rmSync } from "node:fs";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
|
|
4
5
|
export const TYPE_FILE = {
|
|
@@ -21,6 +22,13 @@ function unescape(text) {
|
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
function renderMemory(m) {
|
|
25
|
+
// last-rendered digest baseline: sha256(title \x00 content). service.js
|
|
26
|
+
// compares the file hash against this to tell "untouched by a human" (machine
|
|
27
|
+
// write wins) apart from a real human edit, so a not-yet-re-rendered store
|
|
28
|
+
// update is not misread as a concurrent human edit.
|
|
29
|
+
const digest = createHash("sha256")
|
|
30
|
+
.update(`${m.title}\x00${m.content}`)
|
|
31
|
+
.digest("hex");
|
|
24
32
|
const lines = [];
|
|
25
33
|
lines.push(`## ${esc(m.title)}`);
|
|
26
34
|
lines.push("");
|
|
@@ -31,6 +39,7 @@ function renderMemory(m) {
|
|
|
31
39
|
lines.push(`- **更新时间**: ${m.updated_at}`);
|
|
32
40
|
if (m.source) lines.push(`- **来源**: ${esc(m.source)}`);
|
|
33
41
|
lines.push("");
|
|
42
|
+
lines.push(`<!-- mirror-digest: ${digest} -->`);
|
|
34
43
|
lines.push(m.content);
|
|
35
44
|
lines.push("");
|
|
36
45
|
lines.push("---");
|
|
@@ -86,7 +95,8 @@ export function createMirror(dir) {
|
|
|
86
95
|
let body = text
|
|
87
96
|
.slice(blockStart, blockEnd)
|
|
88
97
|
.replace(/^- \*\*ID\*\*: `[^`]+`\n?/, "")
|
|
89
|
-
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
98
|
+
.replace(/^(- \*\*(类型|重要性|标签|更新时间|来源)\*\*:.*\n?)+/, "")
|
|
99
|
+
.replace(/^<!-- mirror-digest: [a-f0-9]+ -->\n?/m, "");
|
|
90
100
|
const separators = [...body.matchAll(/^---\s*$/gm)];
|
|
91
101
|
const lastSep = separators[separators.length - 1];
|
|
92
102
|
if (lastSep) body = body.slice(0, lastSep.index);
|
|
@@ -97,11 +107,13 @@ export function createMirror(dir) {
|
|
|
97
107
|
// during a three-way merge of human edits (see service.syncMirror).
|
|
98
108
|
const block = text.slice(blockStart, blockEnd);
|
|
99
109
|
const updatedMatch = block.match(/- \*\*更新时间\*\*: ([^\n]+)/);
|
|
110
|
+
const digestMatch = block.match(/<!-- mirror-digest: ([a-f0-9]+) -->/);
|
|
100
111
|
edits.push({
|
|
101
112
|
id: anchor[1],
|
|
102
113
|
title: titleMatch ? unescape(titleMatch[1]).trim() : undefined,
|
|
103
114
|
content: body,
|
|
104
|
-
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined
|
|
115
|
+
updated_at: updatedMatch ? updatedMatch[1].trim() : undefined,
|
|
116
|
+
digest: digestMatch ? digestMatch[1] : undefined
|
|
105
117
|
});
|
|
106
118
|
|
|
107
119
|
const lineEnd = text.indexOf("\n", blockStart);
|
package/src/service.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { TYPE_FILE } from "./mirror.js";
|
|
3
3
|
|
|
4
4
|
const INJECT_TYPES = new Set(["preference", "project", "decision", "summary"]);
|
|
@@ -349,8 +349,21 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
349
349
|
const humanChanged = (typeof edit.title === "string" && edit.title !== m.title)
|
|
350
350
|
|| (typeof edit.content === "string" && edit.content !== m.content);
|
|
351
351
|
if (!humanChanged) { result.push(m); continue; }
|
|
352
|
-
//
|
|
353
|
-
//
|
|
352
|
+
// 判断文件是否被人工动过:digest 存在且匹配则无人触碰,否则视为人工动过。
|
|
353
|
+
// digest 是渲染时对 sha256(title \x00 content) 的记录;机器 store 更新后
|
|
354
|
+
// 镜像还没重渲染时读到旧内容,digest 仍匹配 → 机器 wins,不会误判为
|
|
355
|
+
// 并发人工编辑导致机器写丢失 + 伪冲突标记。
|
|
356
|
+
const digestMatches = typeof edit.digest === "string"
|
|
357
|
+
&& typeof edit.title === "string"
|
|
358
|
+
&& typeof edit.content === "string"
|
|
359
|
+
&& createHash("sha256").update(`${edit.title}\x00${edit.content}`).digest("hex") === edit.digest;
|
|
360
|
+
if (digestMatches) {
|
|
361
|
+
// 无人触碰,机器 wins,走原样
|
|
362
|
+
result.push(m);
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
// 人工动过(digest 不存在=老文件/手工文件保守视为人工动过),走三方合并
|
|
366
|
+
// (保留现有 storeChanged 逻辑)
|
|
354
367
|
const storeChanged = edit.updated_at !== undefined && m.updated_at !== edit.updated_at;
|
|
355
368
|
if (storeChanged) {
|
|
356
369
|
const marker = `\n\n> ⚠️ 并发冲突:人工编辑 vs 记忆库并发更新(${m.updated_at})\n> 记忆库版本:${m.content}`;
|
|
@@ -479,6 +492,12 @@ export function createService({ store, mirror, config, onWrite }) {
|
|
|
479
492
|
// audit write, never a write-hook-triggering memory mutation).
|
|
480
493
|
saveReceipt: (r) => store.saveReceipt(r),
|
|
481
494
|
getReceipt: (id) => store.getReceipt(id),
|
|
482
|
-
listReceipts: (opts) => store.listReceipts(opts)
|
|
495
|
+
listReceipts: (opts) => store.listReceipts(opts),
|
|
496
|
+
// Conflict freeze bookkeeping (same semantics as the audit passthroughs
|
|
497
|
+
// above: an audit write, never a write-hook-triggering memory mutation).
|
|
498
|
+
saveConflictPending: (r) => store.saveConflictPending(r),
|
|
499
|
+
listConflictPending: (opts) => store.listConflictPending(opts),
|
|
500
|
+
resolveConflictPending: (id, o) => store.resolveConflictPending(id, o),
|
|
501
|
+
countConflictPending: () => store.countConflictPending()
|
|
483
502
|
};
|
|
484
503
|
}
|