dsh-cross-memory 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +247 -2
- package/cordis.patch.yml +27 -0
- package/docs/DETAIL.md +13 -0
- package/docs/DETAIL.zh.md +17 -0
- package/icon.svg +1 -0
- package/lib/backfill.js +273 -0
- package/lib/citations.js +305 -0
- package/lib/client.js +1550 -0
- package/lib/config.js +736 -0
- package/lib/consolidate.js +1119 -0
- package/lib/extract.js +570 -0
- package/lib/gate.js +137 -0
- package/lib/git-origin.js +224 -0
- package/lib/index.js +4475 -0
- package/lib/log.js +260 -0
- package/lib/loops.js +186 -0
- package/lib/notices.js +195 -0
- package/lib/notify.js +33 -0
- package/lib/persona.js +112 -0
- package/lib/query.js +93 -0
- package/lib/raw-recall.js +286 -0
- package/lib/reflection.js +291 -0
- package/lib/remote.js +455 -0
- package/lib/render.js +678 -0
- package/lib/retention.js +64 -0
- package/lib/schedule.js +245 -0
- package/lib/search.js +1068 -0
- package/lib/skills.js +220 -0
- package/lib/state.js +701 -0
- package/lib/storage.js +1424 -0
- package/lib/sync-runtime.js +268 -0
- package/lib/sync.js +620 -0
- package/lib/test/activity.test.js +74 -0
- package/lib/test/archive.test.js +182 -0
- package/lib/test/backfill.test.js +144 -0
- package/lib/test/citations.test.js +91 -0
- package/lib/test/client-wiring.test.js +134 -0
- package/lib/test/consolidate-job.test.js +95 -0
- package/lib/test/consolidate.test.js +430 -0
- package/lib/test/consolidation-selection.test.js +152 -0
- package/lib/test/dedupe-keys.test.js +230 -0
- package/lib/test/delta.test.js +93 -0
- package/lib/test/derived.test.js +110 -0
- package/lib/test/disputes-panel.test.js +314 -0
- package/lib/test/eval.test.js +171 -0
- package/lib/test/expand.test.js +164 -0
- package/lib/test/extract.test.js +1020 -0
- package/lib/test/git-origin.test.js +189 -0
- package/lib/test/injection.test.js +440 -0
- package/lib/test/kind-filter.test.js +49 -0
- package/lib/test/legacy-state.test.js +69 -0
- package/lib/test/log.test.js +156 -0
- package/lib/test/loops.test.js +86 -0
- package/lib/test/memory.test.js +855 -0
- package/lib/test/notices.test.js +140 -0
- package/lib/test/persona.test.js +73 -0
- package/lib/test/prefilter.test.js +73 -0
- package/lib/test/preset.test.js +77 -0
- package/lib/test/raw-recall.test.js +186 -0
- package/lib/test/recall.test.js +433 -0
- package/lib/test/reflection.test.js +64 -0
- package/lib/test/remote.test.js +227 -0
- package/lib/test/retention.test.js +117 -0
- package/lib/test/retrieval.test.js +142 -0
- package/lib/test/rotation.test.js +228 -0
- package/lib/test/schedule.test.js +112 -0
- package/lib/test/settings-card.test.js +58 -0
- package/lib/test/skills.test.js +129 -0
- package/lib/test/state.test.js +212 -0
- package/lib/test/subagent-contract.test.js +139 -0
- package/lib/test/sweep.test.js +159 -0
- package/lib/test/sync.test.js +314 -0
- package/lib/test/time.test.js +171 -0
- package/lib/time.js +303 -0
- package/lib/tool.js +330 -0
- package/lib/types/backfill.d.ts +96 -0
- package/lib/types/citations.d.ts +105 -0
- package/lib/types/config.d.ts +811 -0
- package/lib/types/consolidate.d.ts +742 -0
- package/lib/types/extract.d.ts +352 -0
- package/lib/types/gate.d.ts +83 -0
- package/lib/types/git-origin.d.ts +23 -0
- package/lib/types/index.d.ts +1316 -0
- package/lib/types/log.d.ts +144 -0
- package/lib/types/loops.d.ts +82 -0
- package/lib/types/notices.d.ts +117 -0
- package/lib/types/notify.d.ts +12 -0
- package/lib/types/persona.d.ts +43 -0
- package/lib/types/query.d.ts +45 -0
- package/lib/types/raw-recall.d.ts +84 -0
- package/lib/types/reflection.d.ts +122 -0
- package/lib/types/remote.d.ts +233 -0
- package/lib/types/render.d.ts +286 -0
- package/lib/types/retention.d.ts +66 -0
- package/lib/types/schedule.d.ts +116 -0
- package/lib/types/search.d.ts +583 -0
- package/lib/types/skills.d.ts +71 -0
- package/lib/types/state.d.ts +294 -0
- package/lib/types/storage.d.ts +458 -0
- package/lib/types/sync-runtime.d.ts +103 -0
- package/lib/types/sync.d.ts +222 -0
- package/lib/types/test/activity.test.d.ts +1 -0
- package/lib/types/test/archive.test.d.ts +1 -0
- package/lib/types/test/backfill.test.d.ts +1 -0
- package/lib/types/test/citations.test.d.ts +1 -0
- package/lib/types/test/client-wiring.test.d.ts +1 -0
- package/lib/types/test/consolidate-job.test.d.ts +1 -0
- package/lib/types/test/consolidate.test.d.ts +1 -0
- package/lib/types/test/consolidation-selection.test.d.ts +1 -0
- package/lib/types/test/dedupe-keys.test.d.ts +1 -0
- package/lib/types/test/delta.test.d.ts +1 -0
- package/lib/types/test/derived.test.d.ts +1 -0
- package/lib/types/test/disputes-panel.test.d.ts +1 -0
- package/lib/types/test/eval.test.d.ts +1 -0
- package/lib/types/test/expand.test.d.ts +1 -0
- package/lib/types/test/extract.test.d.ts +1 -0
- package/lib/types/test/git-origin.test.d.ts +1 -0
- package/lib/types/test/injection.test.d.ts +1 -0
- package/lib/types/test/kind-filter.test.d.ts +1 -0
- package/lib/types/test/legacy-state.test.d.ts +1 -0
- package/lib/types/test/log.test.d.ts +1 -0
- package/lib/types/test/loops.test.d.ts +1 -0
- package/lib/types/test/memory.test.d.ts +1 -0
- package/lib/types/test/notices.test.d.ts +1 -0
- package/lib/types/test/persona.test.d.ts +1 -0
- package/lib/types/test/prefilter.test.d.ts +1 -0
- package/lib/types/test/preset.test.d.ts +1 -0
- package/lib/types/test/raw-recall.test.d.ts +1 -0
- package/lib/types/test/recall.test.d.ts +1 -0
- package/lib/types/test/reflection.test.d.ts +1 -0
- package/lib/types/test/remote.test.d.ts +1 -0
- package/lib/types/test/retention.test.d.ts +1 -0
- package/lib/types/test/retrieval.test.d.ts +1 -0
- package/lib/types/test/rotation.test.d.ts +1 -0
- package/lib/types/test/schedule.test.d.ts +1 -0
- package/lib/types/test/settings-card.test.d.ts +1 -0
- package/lib/types/test/skills.test.d.ts +1 -0
- package/lib/types/test/state.test.d.ts +1 -0
- package/lib/types/test/subagent-contract.test.d.ts +1 -0
- package/lib/types/test/sweep.test.d.ts +1 -0
- package/lib/types/test/sync.test.d.ts +1 -0
- package/lib/types/test/time.test.d.ts +1 -0
- package/lib/types/time.d.ts +67 -0
- package/lib/types/tool.d.ts +9 -0
- package/lib/types/types.d.ts +374 -0
- package/lib/types/workspace.d.ts +20 -0
- package/lib/types.js +47 -0
- package/lib/workspace.js +65 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +126 -4
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for consolidation's input selection.
|
|
3
|
+
*
|
|
4
|
+
* The pass used to receive the newest N memories, which meant an old entry was
|
|
5
|
+
* never looked at again — the only mechanism that could retire a stale fact
|
|
6
|
+
* could not reach one. Selection now prefers what was never reviewed and then
|
|
7
|
+
* what was reviewed longest ago, so nothing starves while new material and
|
|
8
|
+
* material the model actually uses still go first.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/consolidation-selection.test
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import test from 'node:test';
|
|
14
|
+
import { applyVerdicts, classifyPlan, conflictDisputes, parsePlan, selectForConsolidation } from '../consolidate.js';
|
|
15
|
+
/** Build one entry. */
|
|
16
|
+
function entry(id, updatedAt, uses = 0, scope = 'global') {
|
|
17
|
+
return {
|
|
18
|
+
id,
|
|
19
|
+
scope,
|
|
20
|
+
kind: 'fact',
|
|
21
|
+
title: id,
|
|
22
|
+
body: 'body',
|
|
23
|
+
tags: [],
|
|
24
|
+
keys: [],
|
|
25
|
+
createdAt: 0,
|
|
26
|
+
updatedAt,
|
|
27
|
+
uses,
|
|
28
|
+
lastUsedAt: 0,
|
|
29
|
+
lastSurfacedAt: 0,
|
|
30
|
+
source: 'auto',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** The review marks a pass records, keyed the way the runtime keys them. */
|
|
34
|
+
function reviewed(marks) {
|
|
35
|
+
return new Map(Object.entries(marks).map(([key, value]) => [key.replace(':', '\u0000'), value]));
|
|
36
|
+
}
|
|
37
|
+
test('never-reviewed entries come first, most used first', () => {
|
|
38
|
+
const selected = selectForConsolidation([entry('a', 100), entry('b', 1, 5), entry('c', 50)], reviewed({ 'global:c': 10 }), 2);
|
|
39
|
+
assert.deepEqual(selected.map((item) => item.id), ['b', 'a']);
|
|
40
|
+
});
|
|
41
|
+
test('a reviewed entry is chosen by how long it has waited, never starved', () => {
|
|
42
|
+
const entries = [entry('a', 100), entry('b', 90), entry('c', 80)];
|
|
43
|
+
const marks = reviewed({ 'global:a': 900, 'global:b': 500, 'global:c': 100 });
|
|
44
|
+
assert.deepEqual(selectForConsolidation(entries, marks, 2).map((item) => item.id), ['c', 'b']);
|
|
45
|
+
// The one that waited longest last time is first this time.
|
|
46
|
+
assert.deepEqual(selectForConsolidation(entries, reviewed({ 'global:a': 900, 'global:b': 500, 'global:c': 100 }), 1).map((item) => item.id), ['c']);
|
|
47
|
+
});
|
|
48
|
+
test('fresh material keeps its place ahead of re-review', () => {
|
|
49
|
+
const entries = [entry('fresh', 1), entry('old', 900)];
|
|
50
|
+
const marks = reviewed({ 'global:old': 1 });
|
|
51
|
+
assert.deepEqual(selectForConsolidation(entries, marks, 2).map((item) => item.id), ['fresh', 'old']);
|
|
52
|
+
assert.deepEqual(selectForConsolidation(entries, marks, 1).map((item) => item.id), ['fresh']);
|
|
53
|
+
});
|
|
54
|
+
test('ties break on recency of the write and then on id, so a pass is reproducible', () => {
|
|
55
|
+
const entries = [entry('b', 5), entry('a', 5), entry('c', 9)];
|
|
56
|
+
assert.deepEqual(selectForConsolidation(entries, new Map(), 3).map((item) => item.id), ['c', 'a', 'b']);
|
|
57
|
+
assert.deepEqual(selectForConsolidation(entries, reviewed({ 'global:a': 7, 'global:b': 7 }), 3).map((item) => item.id), ['c', 'a', 'b']);
|
|
58
|
+
});
|
|
59
|
+
test('scopes are distinct, and a limit of 0 selects nothing', () => {
|
|
60
|
+
const entries = [entry('shared', 1), entry('shared', 2, 0, 'project')];
|
|
61
|
+
const marks = reviewed({ 'global:shared': 1 });
|
|
62
|
+
// The project copy has never been reviewed, so it goes first even though the
|
|
63
|
+
// global one shares its id.
|
|
64
|
+
assert.deepEqual(selectForConsolidation(entries, marks, 1).map((item) => item.scope), ['project']);
|
|
65
|
+
assert.deepEqual(selectForConsolidation(entries, marks, 0), []);
|
|
66
|
+
});
|
|
67
|
+
test('a permanent memory is never rewritten or retired unattended', () => {
|
|
68
|
+
const rule = { ...entry('rule', 100), source: 'tool', permanent: true, body: 'the standing rule' };
|
|
69
|
+
const plan = {
|
|
70
|
+
upserts: [{ id: 'rule', scope: 'global', kind: 'fact', title: 'rule', body: 'a shorter take', tags: [] }],
|
|
71
|
+
retire: ['rule'],
|
|
72
|
+
skills: [],
|
|
73
|
+
notes: '',
|
|
74
|
+
};
|
|
75
|
+
const { safe, disputes } = classifyPlan(plan, { entries: [rule] });
|
|
76
|
+
assert.equal(safe.upserts.length, 0, 'a permanent memory is not rewritten without a person');
|
|
77
|
+
assert.deepEqual(safe.retire, [], 'and it is not retired either');
|
|
78
|
+
assert.equal(disputes.length, 2);
|
|
79
|
+
for (const dispute of disputes)
|
|
80
|
+
assert.match(dispute.reason, /永久保留/u);
|
|
81
|
+
});
|
|
82
|
+
test('a retirement covered by its successor is applied without asking a person', () => {
|
|
83
|
+
const old = { ...entry('old', 100), body: 'keep /a/b.md and the caveat', uses: 4 };
|
|
84
|
+
const plan = {
|
|
85
|
+
upserts: [{ id: 'new', scope: 'global', kind: 'fact', title: 'new', body: 'keep /a/b.md and the caveat, plus more detail', tags: [], derivedFrom: ['old'] }],
|
|
86
|
+
retire: ['old'],
|
|
87
|
+
skills: [],
|
|
88
|
+
notes: '',
|
|
89
|
+
};
|
|
90
|
+
const { safe, disputes } = classifyPlan(plan, { entries: [old] });
|
|
91
|
+
assert.deepEqual(safe.retire, ['old'], 'the predecessor is folded into its successor');
|
|
92
|
+
assert.equal(disputes.length, 0, 'nothing is asked: the content survives');
|
|
93
|
+
});
|
|
94
|
+
test('permanence stops the automation even when the content is covered', () => {
|
|
95
|
+
const old = { ...entry('old', 100), body: 'keep /a/b.md and the caveat', uses: 4, permanent: true };
|
|
96
|
+
const plan = {
|
|
97
|
+
upserts: [{ id: 'new', scope: 'global', kind: 'fact', title: 'new', body: 'keep /a/b.md and the caveat, plus more detail', tags: [], derivedFrom: ['old'] }],
|
|
98
|
+
retire: ['old'],
|
|
99
|
+
skills: [],
|
|
100
|
+
notes: '',
|
|
101
|
+
};
|
|
102
|
+
const { safe, disputes } = classifyPlan(plan, { entries: [old] });
|
|
103
|
+
assert.deepEqual(safe.retire, [], 'a promise is not automated away');
|
|
104
|
+
assert.equal(disputes.length, 1);
|
|
105
|
+
assert.equal(disputes[0]?.hard, true);
|
|
106
|
+
});
|
|
107
|
+
test('verdicts apply the accepted, drop the rejected, and keep the unheard', () => {
|
|
108
|
+
const plan = { upserts: [], retire: ['a', 'b', 'c'], skills: [], notes: '' };
|
|
109
|
+
const disputes = [
|
|
110
|
+
{ id: 'a', scope: 'global', action: 'retire', title: 'a', reason: 'r', before: 'x', after: '' },
|
|
111
|
+
{ id: 'b', scope: 'global', action: 'retire', title: 'b', reason: 'r', before: 'x', after: '' },
|
|
112
|
+
{ id: 'c', scope: 'global', action: 'retire', title: 'c', reason: 'r', before: 'x', after: '' },
|
|
113
|
+
];
|
|
114
|
+
const safe = { upserts: [], retire: [] };
|
|
115
|
+
const remaining = applyVerdicts(safe, plan, disputes, [
|
|
116
|
+
{ id: 'a', accept: true, confidence: 0.9, reason: 'carried over' },
|
|
117
|
+
{ id: 'b', accept: false, confidence: 0.8, reason: 'no replacement visible' },
|
|
118
|
+
{ id: 'c', accept: true, confidence: 0.3, reason: 'unsure' },
|
|
119
|
+
], 0.7);
|
|
120
|
+
assert.deepEqual(safe.retire, ['a'], 'only the confident accept is applied');
|
|
121
|
+
assert.deepEqual(remaining.map((row) => row.id), ['c'], 'a low-confidence verdict is not an answer');
|
|
122
|
+
});
|
|
123
|
+
test('a contradiction is reported as a question for the user, never resolved silently', () => {
|
|
124
|
+
const entries = [
|
|
125
|
+
{ ...entry('old-rule', 100), body: 'never push to main', uses: 3 },
|
|
126
|
+
{ ...entry('new-rule', 200), body: 'push to main when the suite is green', uses: 1, voice: 'user' },
|
|
127
|
+
];
|
|
128
|
+
const plan = parsePlan(JSON.stringify({
|
|
129
|
+
memories: [], retire: [], skills: [], notes: '',
|
|
130
|
+
conflicts: [{ keep: 'new-rule', drop: 'old-rule', reason: '推送策略反了' }],
|
|
131
|
+
}), new Set(['old-rule', 'new-rule']), 10);
|
|
132
|
+
assert.ok(plan?.conflicts !== undefined);
|
|
133
|
+
assert.deepEqual(plan.conflicts, [{ keep: 'new-rule', drop: 'old-rule', reason: '推送策略反了' }]);
|
|
134
|
+
const disputes = conflictDisputes(plan.conflicts ?? [], entries);
|
|
135
|
+
assert.equal(disputes.length, 1, 'the losing entry becomes one question');
|
|
136
|
+
assert.equal(disputes[0]?.id, 'old-rule');
|
|
137
|
+
assert.match(disputes[0]?.reason ?? '', /与 new-rule 矛盾/u, 'the question names what it conflicts with');
|
|
138
|
+
assert.equal(disputes[0]?.hard, true, 'intent is never adjudicated by a machine');
|
|
139
|
+
});
|
|
140
|
+
test('a conflict naming ids outside the page, or the same id twice, is dropped', () => {
|
|
141
|
+
const known = new Set(['a', 'b']);
|
|
142
|
+
const plan = parsePlan(JSON.stringify({
|
|
143
|
+
memories: [], retire: [], skills: [], notes: '',
|
|
144
|
+
conflicts: [
|
|
145
|
+
{ keep: 'a', drop: 'a', reason: 'self' },
|
|
146
|
+
{ keep: 'a', drop: 'missing', reason: 'unknown' },
|
|
147
|
+
{ keep: 'a', drop: 'b', reason: 'real' },
|
|
148
|
+
{ keep: 'b', drop: 'b', reason: 'duplicate loser' },
|
|
149
|
+
],
|
|
150
|
+
}), known, 10);
|
|
151
|
+
assert.deepEqual(plan?.conflicts, [{ keep: 'a', drop: 'b', reason: 'real' }]);
|
|
152
|
+
});
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for near-duplicate superseding and key expansion.
|
|
3
|
+
*
|
|
4
|
+
* Both exist to keep recall sharp as the store grows: a re-worded lesson must
|
|
5
|
+
* replace its predecessor instead of competing with it, and a memory must be
|
|
6
|
+
* findable by the words a future session would use, not only by the words the
|
|
7
|
+
* extractor happened to write.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-cross-memory/test/dedupe-keys.test
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import { mkdtemp, readFile, readdir, rename, rm } from 'node:fs/promises';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import test from 'node:test';
|
|
16
|
+
import { MemoryStore, isNearDuplicate, normalizeKey, overlap } from '../storage.js';
|
|
17
|
+
import { relevanceOf, searchMemories } from '../search.js';
|
|
18
|
+
/** A store and its temporary directory. */
|
|
19
|
+
async function tempStore() {
|
|
20
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-dedupe-'));
|
|
21
|
+
return { store: new MemoryStore(dir), dir };
|
|
22
|
+
}
|
|
23
|
+
test('normalizeKey lowercases, drops commas, and caps the length', () => {
|
|
24
|
+
assert.equal(normalizeKey(' Monorepo '), 'monorepo');
|
|
25
|
+
assert.equal(normalizeKey('pnpm, npm'), 'pnpm npm');
|
|
26
|
+
assert.equal(normalizeKey('x'.repeat(80)).length, 48);
|
|
27
|
+
});
|
|
28
|
+
test('overlap is the Jaccard ratio and is 0 when either side is empty', () => {
|
|
29
|
+
assert.equal(overlap(new Set(['a', 'b']), new Set(['a', 'b'])), 1);
|
|
30
|
+
assert.equal(overlap(new Set(['a', 'b']), new Set(['b', 'c'])), 1 / 3);
|
|
31
|
+
assert.equal(overlap(new Set(), new Set(['a'])), 0);
|
|
32
|
+
});
|
|
33
|
+
test('isNearDuplicate needs both halves to agree', () => {
|
|
34
|
+
const title = 'Run tests with node test runner';
|
|
35
|
+
const other = 'Run tests with node test runner';
|
|
36
|
+
assert.equal(isNearDuplicate({ title, body: 'same body' }, { title: other, body: 'same body' }, 0.7), true);
|
|
37
|
+
// A different body is a different lesson that happens to share a name.
|
|
38
|
+
assert.equal(isNearDuplicate({ title, body: 'one body' }, { title: other, body: 'another body entirely' }, 0.7), false);
|
|
39
|
+
// Overlapping but re-worded titles do not reach the threshold.
|
|
40
|
+
assert.equal(isNearDuplicate({ title: 'Prefer pnpm', body: 'same' }, { title: 'Answer in Chinese', body: 'same' }, 0.7), false);
|
|
41
|
+
assert.equal(isNearDuplicate({ title, body: 'same' }, { title: other, body: 'same' }, 0), false);
|
|
42
|
+
});
|
|
43
|
+
test('a re-worded memory supersedes its predecessor instead of joining it', async (t) => {
|
|
44
|
+
const { store, dir } = await tempStore();
|
|
45
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
46
|
+
store.similarityLimit = () => 0.7;
|
|
47
|
+
const first = await store.upsert({ scope: 'global', title: 'Run tests with node --test', body: 'The suite runs with node --test.', tags: [] }, undefined, 'auto', 1_000);
|
|
48
|
+
const second = await store.upsert({ scope: 'global', title: 'Run tests with node test runner', body: 'The suite runs with node --test.', tags: [] }, undefined, 'auto', 2_000);
|
|
49
|
+
assert.notEqual(first.entry.id, second.entry.id);
|
|
50
|
+
assert.equal(second.entry.supersedes, first.entry.id);
|
|
51
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
52
|
+
assert.deepEqual(listed.map((entry) => entry.id), [second.entry.id]);
|
|
53
|
+
assert.equal(await store.read('global', undefined, first.entry.id), undefined);
|
|
54
|
+
// The predecessor is archived for the same reason a merged loser is: an
|
|
55
|
+
// explicit rewrite is not a better reason to destroy a memory than a guess is.
|
|
56
|
+
assert.deepEqual((await store.listArchived('global', undefined)).map((entry) => entry.id), [first.entry.id]);
|
|
57
|
+
});
|
|
58
|
+
test('Chinese memories are comparable, so a paraphrase is recognised', () => {
|
|
59
|
+
const left = { title: '规格型号比对只比实质内容', body: '去空格、去全半角标点、转大写之后再比较。' };
|
|
60
|
+
const same = { title: '规格型号比较只比实质内容', body: '去空格、去全半角标点、转大写之后再比较。' };
|
|
61
|
+
const other = { title: '未支付轮询分档', body: '未支付订单的轮询改成两档加抖动。' };
|
|
62
|
+
// Splitting CJK on non-letters left a whole sentence as one token, which made
|
|
63
|
+
// every pair of Chinese memories incomparable. Character shingles fix that.
|
|
64
|
+
assert.equal(isNearDuplicate(left, same, 0.7), true);
|
|
65
|
+
assert.equal(isNearDuplicate(left, other, 0.7), false);
|
|
66
|
+
});
|
|
67
|
+
test('one title is one memory, and the loser is archived rather than deleted', async (t) => {
|
|
68
|
+
const { store, dir } = await tempStore();
|
|
69
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
70
|
+
store.similarityLimit = () => 0.7;
|
|
71
|
+
// Character-for-character identical titles with bodies in different languages:
|
|
72
|
+
// measured on a real store this pair survived for months, because the body
|
|
73
|
+
// check vetoed the merge that the titles were asking for.
|
|
74
|
+
const first = await store.upsert({
|
|
75
|
+
scope: 'global',
|
|
76
|
+
title: '手写摘取规则',
|
|
77
|
+
body: '同形归一必须带左侧词边界,否则机打税号会被抢走。形态闸门不能要求成本中心是固定位数。',
|
|
78
|
+
tags: [],
|
|
79
|
+
}, undefined, 'auto', 1_000);
|
|
80
|
+
const second = await store.upsert({
|
|
81
|
+
scope: 'global',
|
|
82
|
+
title: '手写摘取规则',
|
|
83
|
+
body: 'Lookalike normalization needs a left word boundary, or printed tax ids are hijacked.',
|
|
84
|
+
tags: [],
|
|
85
|
+
}, undefined, 'auto', 2_000, 'handwriting-crop-third');
|
|
86
|
+
assert.notEqual(first.entry.id, second.entry.id, 'two files exist to begin with');
|
|
87
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
88
|
+
assert.equal(listed.length, 1, 'one title is one memory');
|
|
89
|
+
// A judgement made without a model's help must be reversible: the loser is
|
|
90
|
+
// archived, so `/memories restore` can bring back a false merge.
|
|
91
|
+
const archived = await store.listArchived('global', undefined);
|
|
92
|
+
assert.equal(archived.length, 1, 'the loser is archived, not destroyed');
|
|
93
|
+
});
|
|
94
|
+
test('an id that ends in a dash is still addressable', async (t) => {
|
|
95
|
+
const { store, dir } = await tempStore();
|
|
96
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
97
|
+
const created = await store.upsert({
|
|
98
|
+
scope: 'global',
|
|
99
|
+
title: 'Dashed entry',
|
|
100
|
+
body: 'A body.',
|
|
101
|
+
tags: [],
|
|
102
|
+
}, undefined, 'auto', 1_000);
|
|
103
|
+
// A collision suffix can leave a trailing `-`, which slugify strips — so the
|
|
104
|
+
// canonical spelling does not exist and every id-addressed call misses it.
|
|
105
|
+
await rename(join(dir, 'entries', `${created.entry.id}.md`), join(dir, 'entries', `${created.entry.id}-.md`));
|
|
106
|
+
const dashed = `${created.entry.id}-`;
|
|
107
|
+
assert.ok(await store.read('global', undefined, dashed) !== undefined, 'the exact file name is found');
|
|
108
|
+
// A rewrite must land in that same file, not beside it: writing `${id}.md`
|
|
109
|
+
// would silently turn an update into a duplicate.
|
|
110
|
+
await store.upsert({
|
|
111
|
+
scope: 'global',
|
|
112
|
+
title: 'Dashed entry renamed',
|
|
113
|
+
body: 'A rewritten body.',
|
|
114
|
+
tags: [],
|
|
115
|
+
}, undefined, 'auto', 2_000, dashed);
|
|
116
|
+
const names = (await readdir(join(dir, 'entries'))).filter((name) => name.endsWith('.md'));
|
|
117
|
+
assert.deepEqual(names, [`${created.entry.id}-.md`], 'one file, updated in place');
|
|
118
|
+
assert.equal(await store.archive('global', undefined, dashed), true, 'and it can be archived');
|
|
119
|
+
assert.equal((await store.list('global', undefined, { fresh: true })).length, 0);
|
|
120
|
+
assert.equal((await store.listArchived('global', undefined)).length, 1);
|
|
121
|
+
});
|
|
122
|
+
test('the near-duplicate rule is off until it is configured', async (t) => {
|
|
123
|
+
const { store, dir } = await tempStore();
|
|
124
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
125
|
+
// The default `similarityLimit` of 0 keeps only the exact-match rule, so two
|
|
126
|
+
// entries that share most of their words but not their whole title both live.
|
|
127
|
+
await store.upsert({ scope: 'global', title: 'Run tests with node --test', body: 'The suite runs with node --test.', tags: [] }, undefined, 'auto', 1_000);
|
|
128
|
+
await store.upsert({ scope: 'global', title: 'Run tests with node test runner', body: 'The suite runs with node --test.', tags: [] }, undefined, 'auto', 2_000);
|
|
129
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
130
|
+
assert.equal(listed.length, 2);
|
|
131
|
+
});
|
|
132
|
+
test('a rewrite that omits appliesTo keeps the existing one', async (t) => {
|
|
133
|
+
const { store, dir } = await tempStore();
|
|
134
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
135
|
+
const first = await store.upsert({
|
|
136
|
+
scope: 'global',
|
|
137
|
+
title: 'Alpha fact',
|
|
138
|
+
body: 'The first body.',
|
|
139
|
+
tags: [],
|
|
140
|
+
appliesTo: '准备推送代码之前',
|
|
141
|
+
}, undefined, 'auto', 1_000);
|
|
142
|
+
assert.equal(first.entry.appliesTo, '准备推送代码之前');
|
|
143
|
+
// A consolidation rewrite often returns only title/body. Dropping the trigger
|
|
144
|
+
// phrase silently removes the field on-demand recall gates on, and nothing
|
|
145
|
+
// downstream can tell that from a deliberate removal.
|
|
146
|
+
const rewritten = await store.upsert({
|
|
147
|
+
scope: 'global',
|
|
148
|
+
title: 'Alpha fact',
|
|
149
|
+
body: 'A sharper body.',
|
|
150
|
+
tags: [],
|
|
151
|
+
}, undefined, 'auto', 2_000, 'alpha-fact');
|
|
152
|
+
assert.equal(rewritten.entry.appliesTo, '准备推送代码之前', 'an omitted appliesTo is carried over');
|
|
153
|
+
const cleared = await store.upsert({
|
|
154
|
+
scope: 'global',
|
|
155
|
+
title: 'Alpha fact',
|
|
156
|
+
body: 'A sharper body still.',
|
|
157
|
+
tags: [],
|
|
158
|
+
appliesTo: '',
|
|
159
|
+
}, undefined, 'auto', 3_000, 'alpha-fact');
|
|
160
|
+
assert.equal(cleared.entry.appliesTo, undefined, 'an explicit empty string removes it');
|
|
161
|
+
});
|
|
162
|
+
test('an explicit supersedes still wins over the similarity search', async (t) => {
|
|
163
|
+
const { store, dir } = await tempStore();
|
|
164
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
165
|
+
store.similarityLimit = () => 0.7;
|
|
166
|
+
await store.upsert({ scope: 'global', title: 'Alpha fact', body: 'alpha body', tags: [] }, undefined, 'auto', 1_000);
|
|
167
|
+
await store.upsert({ scope: 'global', title: 'Beta fact', body: 'beta body', tags: [] }, undefined, 'auto', 2_000);
|
|
168
|
+
const third = await store.upsert({ scope: 'global', title: 'Gamma fact', body: 'gamma body', tags: [], supersedes: 'alpha-fact' }, undefined, 'auto', 3_000);
|
|
169
|
+
assert.equal(third.entry.supersedes, 'alpha-fact');
|
|
170
|
+
assert.deepEqual((await store.list('global', undefined, { fresh: true })).map((entry) => entry.id).sort(), ['beta-fact', 'gamma-fact']);
|
|
171
|
+
});
|
|
172
|
+
test('keys round-trip through the file and are found by search', async (t) => {
|
|
173
|
+
const { store, dir } = await tempStore();
|
|
174
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
175
|
+
const stored = await store.upsert({
|
|
176
|
+
scope: 'global',
|
|
177
|
+
title: 'Prefer pnpm workspaces',
|
|
178
|
+
body: 'The repository is one pnpm workspace with several packages.',
|
|
179
|
+
tags: [],
|
|
180
|
+
keys: ['Monorepo', ' monorepo ', 'workspace layout'],
|
|
181
|
+
}, undefined, 'auto', 1_000);
|
|
182
|
+
assert.deepEqual([...stored.entry.keys], ['monorepo', 'workspace layout']);
|
|
183
|
+
const file = await readFile(join(dir, 'entries', 'prefer-pnpm-workspaces.md'), 'utf8');
|
|
184
|
+
assert.match(file, /^keys: monorepo, workspace layout$/mu);
|
|
185
|
+
const parsed = await store.read('global', undefined, 'prefer-pnpm-workspaces');
|
|
186
|
+
assert.deepEqual([...parsed?.keys ?? []], ['monorepo', 'workspace layout']);
|
|
187
|
+
// The alias is what a future session would type; it must win the ranking even
|
|
188
|
+
// though the word appears nowhere in the title or body.
|
|
189
|
+
const groups = [{ scope: 'global', label: 'global', entries: [parsed] }];
|
|
190
|
+
assert.equal(searchMemories(groups, 'monorepo')[0]?.entry.id, 'prefer-pnpm-workspaces');
|
|
191
|
+
assert.ok(relevanceOf(parsed, 'monorepo') > 20, 'the alias clears the relevance floor');
|
|
192
|
+
});
|
|
193
|
+
test('memories differing only by a single-character token stay separate', async (t) => {
|
|
194
|
+
const { store, dir } = await tempStore();
|
|
195
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
196
|
+
store.similarityLimit = () => 0.7;
|
|
197
|
+
await store.upsert({ scope: 'global', title: 'Old fact 1', body: 'Been here for months.', tags: [] }, undefined, 'auto', 1_000);
|
|
198
|
+
// Dropping one-character tokens would reduce both titles to {old, fact} and
|
|
199
|
+
// make this a duplicate of the first — a merge that deletes a real memory.
|
|
200
|
+
const second = await store.upsert({ scope: 'global', title: 'Old fact 2', body: 'Been here for months.', tags: [] }, undefined, 'auto', 2_000);
|
|
201
|
+
assert.equal(second.entry.supersedes, undefined, 'the digit is what tells the two apart');
|
|
202
|
+
assert.equal((await store.list('global', undefined, { fresh: true })).length, 2);
|
|
203
|
+
assert.equal(isNearDuplicate({ title: 'Old fact 1', body: 'same' }, { title: 'Old fact 2', body: 'same' }, 0.7), false);
|
|
204
|
+
});
|
|
205
|
+
test('a numbered family of memories is not collapsed by substring containment', async (t) => {
|
|
206
|
+
const { store, dir } = await tempStore();
|
|
207
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
208
|
+
// "Old fact 19" contains "Old fact 1", and the collision rule deletes its
|
|
209
|
+
// loser, so a raw substring test turned four distinct memories into two.
|
|
210
|
+
for (const title of ['Old fact 1', 'Old fact 19', 'Old fact 190', 'The correction']) {
|
|
211
|
+
await store.upsert({ scope: 'global', title, body: 'Been here for months.', tags: [] }, undefined, 'auto');
|
|
212
|
+
}
|
|
213
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
214
|
+
assert.deepEqual(listed.map((entry) => entry.id).sort(), ['old-fact-1', 'old-fact-19', 'old-fact-190', 'the-correction']);
|
|
215
|
+
});
|
|
216
|
+
test('a longer restatement still collapses into the shorter one', async (t) => {
|
|
217
|
+
const { store, dir } = await tempStore();
|
|
218
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
219
|
+
await store.upsert({ scope: 'global', title: 'Use pnpm', body: 'The repo uses pnpm.', tags: [] }, undefined, 'auto', 1_000);
|
|
220
|
+
await store.upsert({ scope: 'global', title: 'Use pnpm for this repo', body: 'The repo uses pnpm. Never npm.', tags: [] }, undefined, 'auto', 2_000);
|
|
221
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
222
|
+
assert.deepEqual(listed.map((entry) => entry.id), ['use-pnpm-for-this-repo'], 'the fuller statement wins and the shorthand goes');
|
|
223
|
+
});
|
|
224
|
+
test('a rewrite that omits keys keeps the stored ones', async (t) => {
|
|
225
|
+
const { store, dir } = await tempStore();
|
|
226
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
227
|
+
await store.upsert({ scope: 'global', title: 'Ship it', body: 'Run the release.', tags: [], keys: ['deploy'] }, undefined, 'auto', 1_000);
|
|
228
|
+
const updated = await store.upsert({ scope: 'global', title: 'Ship it', body: 'Run the release twice.', tags: [] }, undefined, 'auto', 2_000);
|
|
229
|
+
assert.deepEqual([...updated.entry.keys], ['deploy']);
|
|
230
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Late-injection tests for {@link MemoriesRuntime}.
|
|
3
|
+
*
|
|
4
|
+
* The summary is a snapshot, so a memory written after it must reach the model
|
|
5
|
+
* without waiting for the next session — once per write burst, bounded, and
|
|
6
|
+
* never repeating an entry the conversation already carries.
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-cross-memory/test/delta.test
|
|
9
|
+
*/
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
12
|
+
import { tmpdir } from 'node:os';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import test from 'node:test';
|
|
15
|
+
import { MemoriesRuntime } from '../index.js';
|
|
16
|
+
/** A context stand-in exposing only what the runtime touches. */
|
|
17
|
+
const stubContext = {
|
|
18
|
+
get: () => undefined,
|
|
19
|
+
logger: { info: () => undefined, warn: () => undefined, debug: () => undefined },
|
|
20
|
+
};
|
|
21
|
+
/** A session stand-in: id, header cwd, and an empty history. */
|
|
22
|
+
function stubSession(cwd, id = 'session-delta') {
|
|
23
|
+
return {
|
|
24
|
+
id,
|
|
25
|
+
header: { version: 0, id, createdAt: 0, cwd, isSeeded: false },
|
|
26
|
+
requestHeader: () => undefined,
|
|
27
|
+
deriveMessages: () => [],
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** An agent stand-in exposing only `session`. */
|
|
31
|
+
function stubAgent(session) {
|
|
32
|
+
return { id: session.id, session, status: 'idle' };
|
|
33
|
+
}
|
|
34
|
+
/** Text of one user message, for assertions on the injected block. */
|
|
35
|
+
function textOf(message) {
|
|
36
|
+
return JSON.stringify(message.content);
|
|
37
|
+
}
|
|
38
|
+
test('a memory written after the summary is announced once, and only once', async (t) => {
|
|
39
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-delta-'));
|
|
40
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
41
|
+
t.after(() => runtime.dispose());
|
|
42
|
+
const agent = stubAgent(stubSession(dir));
|
|
43
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Prefer pnpm', body: 'Use pnpm, not npm.', tags: [] }, 'tool');
|
|
44
|
+
const summary = await runtime.injectionFor(agent);
|
|
45
|
+
assert.ok(summary !== undefined);
|
|
46
|
+
assert.equal(await runtime.deltaFor(agent), undefined, 'nothing was written after the summary yet');
|
|
47
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Node 24 required', body: 'This repo needs Node 24 or newer.', tags: [] }, 'tool');
|
|
48
|
+
const first = await runtime.deltaFor(agent);
|
|
49
|
+
assert.ok(first !== undefined, 'a late write is announced');
|
|
50
|
+
assert.match(textOf(first), /Node 24 required/u);
|
|
51
|
+
assert.doesNotMatch(textOf(first), /Prefer pnpm/u, 'the summary entries are not repeated');
|
|
52
|
+
assert.equal(await runtime.deltaFor(agent), undefined, 'the same entry is not announced twice');
|
|
53
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Tests beside sources', body: 'Keep tests next to the code.', tags: [] }, 'tool');
|
|
54
|
+
const second = await runtime.deltaFor(agent);
|
|
55
|
+
assert.ok(second !== undefined, 'a second burst is allowed');
|
|
56
|
+
assert.match(textOf(second), /Tests beside sources/u);
|
|
57
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Third late memory', body: 'Should not be announced.', tags: [] }, 'tool');
|
|
58
|
+
assert.equal(await runtime.deltaFor(agent), undefined, 'the per-conversation cap holds');
|
|
59
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
60
|
+
});
|
|
61
|
+
test('the delta baseline survives a restart', async (t) => {
|
|
62
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-delta-restart-'));
|
|
63
|
+
const first = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
64
|
+
t.after(() => first.dispose());
|
|
65
|
+
const session = stubSession(dir, 'session-restart');
|
|
66
|
+
await first.write(session, { scope: 'global', title: 'Alpha', body: 'First memory.', tags: [] }, 'tool');
|
|
67
|
+
assert.ok(await first.injectionFor(stubAgent(session)) !== undefined);
|
|
68
|
+
await first.write(session, { scope: 'global', title: 'Beta', body: 'Second memory.', tags: [] }, 'tool');
|
|
69
|
+
assert.ok(await first.deltaFor(stubAgent(session)) !== undefined);
|
|
70
|
+
// A fresh runtime models a restarted process: the durable record says Alpha
|
|
71
|
+
// and Beta were already shown, so only Gamma may be announced.
|
|
72
|
+
const second = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
73
|
+
t.after(() => second.dispose());
|
|
74
|
+
const resumed = stubSession(dir, 'session-restart');
|
|
75
|
+
await second.write(resumed, { scope: 'global', title: 'Gamma', body: 'Third memory.', tags: [] }, 'tool');
|
|
76
|
+
const late = await second.deltaFor(stubAgent(resumed));
|
|
77
|
+
assert.ok(late !== undefined);
|
|
78
|
+
assert.match(textOf(late), /Gamma/u);
|
|
79
|
+
assert.doesNotMatch(textOf(late), /Alpha/u);
|
|
80
|
+
assert.doesNotMatch(textOf(late), /Beta/u);
|
|
81
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
82
|
+
});
|
|
83
|
+
test('injectDelta: false keeps the summary the only automatic block', async (t) => {
|
|
84
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-delta-off-'));
|
|
85
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false, injectDelta: false });
|
|
86
|
+
t.after(() => runtime.dispose());
|
|
87
|
+
const agent = stubAgent(stubSession(dir));
|
|
88
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Alpha', body: 'First memory.', tags: [] }, 'tool');
|
|
89
|
+
assert.ok(await runtime.injectionFor(agent) !== undefined);
|
|
90
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Beta', body: 'Second memory.', tags: [] }, 'tool');
|
|
91
|
+
assert.equal(await runtime.deltaFor(agent), undefined);
|
|
92
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
93
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the merge trail: which entries a memory was built from.
|
|
3
|
+
*
|
|
4
|
+
* The property that matters is that the trail survives — a consolidation that
|
|
5
|
+
* folds three memories into one archives the sources, so the list on the
|
|
6
|
+
* survivor is the only record of what it replaced. Everything here checks that
|
|
7
|
+
* the list is written, carried through rewrites, and never allowed to name an
|
|
8
|
+
* id nobody can open.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/derived.test
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import test from 'node:test';
|
|
17
|
+
import { MemoryStore, formatEntry, parseEntry } from '../storage.js';
|
|
18
|
+
import { parsePlan } from '../consolidate.js';
|
|
19
|
+
/** Create a temporary memories directory. */
|
|
20
|
+
async function tempStore() {
|
|
21
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-derived-'));
|
|
22
|
+
return { store: new MemoryStore(dir), dir };
|
|
23
|
+
}
|
|
24
|
+
/** One entry with every required field. */
|
|
25
|
+
function entry(extra = {}) {
|
|
26
|
+
return {
|
|
27
|
+
id: 'merged',
|
|
28
|
+
scope: 'global',
|
|
29
|
+
kind: 'fact',
|
|
30
|
+
title: '合并后的记忆',
|
|
31
|
+
body: '三条记忆合并成的一条。',
|
|
32
|
+
tags: [],
|
|
33
|
+
keys: [],
|
|
34
|
+
createdAt: 1,
|
|
35
|
+
updatedAt: 1,
|
|
36
|
+
uses: 0,
|
|
37
|
+
lastUsedAt: 0,
|
|
38
|
+
lastSurfacedAt: 0,
|
|
39
|
+
source: 'auto',
|
|
40
|
+
...extra,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
test('the merge trail round-trips through the file format', () => {
|
|
44
|
+
const text = formatEntry(entry({ derivedFrom: ['alpha', 'beta'] }));
|
|
45
|
+
assert.match(text, /^derivedFrom: alpha, beta$/mu);
|
|
46
|
+
const parsed = parseEntry(text, 'global', 'merged');
|
|
47
|
+
assert.deepEqual(parsed?.derivedFrom, ['alpha', 'beta']);
|
|
48
|
+
});
|
|
49
|
+
test('an entry with no merge trail carries no field and parses without one', () => {
|
|
50
|
+
const text = formatEntry(entry());
|
|
51
|
+
assert.ok(!text.includes('derivedFrom'));
|
|
52
|
+
assert.equal(parseEntry(text, 'global', 'merged')?.derivedFrom, undefined);
|
|
53
|
+
});
|
|
54
|
+
test('a rewrite keeps the trail it was given and gains the entry it supersedes', async () => {
|
|
55
|
+
const { store, dir } = await tempStore();
|
|
56
|
+
try {
|
|
57
|
+
await store.upsert({ scope: 'global', title: '甲', body: '第一条。', tags: [], keys: [] }, undefined, 'auto');
|
|
58
|
+
await store.upsert({ scope: 'global', title: '乙', body: '第二条。', tags: [], keys: [] }, undefined, 'auto');
|
|
59
|
+
const first = await store.upsert({ scope: 'global', title: '合并后的记忆', body: '甲和乙合起来。', tags: [], keys: [], derivedFrom: ['乙'] }, undefined, 'auto');
|
|
60
|
+
assert.deepEqual(first.entry.derivedFrom, ['乙'], 'an explicit trail is stored');
|
|
61
|
+
const kept = first.entry.id;
|
|
62
|
+
// A later rewrite that says nothing about provenance must not erase it.
|
|
63
|
+
const second = await store.upsert({ scope: 'global', title: '合并后的记忆', body: '甲和乙合成的,措辞更紧。', tags: [], keys: [] }, undefined, 'auto', Date.now(), kept);
|
|
64
|
+
assert.deepEqual(second.entry.derivedFrom, ['乙'], 'the trail survives a rewrite that omits it');
|
|
65
|
+
// A supersede is itself evidence: the entry it replaces was one of its sources.
|
|
66
|
+
const third = await store.upsert({ scope: 'global', title: '合并后的记忆', body: '再次改写。', tags: [], keys: [], supersedes: '甲' }, undefined, 'auto', Date.now(), kept);
|
|
67
|
+
assert.deepEqual(third.entry.derivedFrom, ['乙', '甲']);
|
|
68
|
+
assert.ok(!third.entry.derivedFrom?.includes(kept), 'an entry is never its own source');
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
await rm(dir, { recursive: true, force: true });
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
test('the plan parser keeps only ids the store can resolve, and never the entry itself', () => {
|
|
75
|
+
const known = new Set(['alpha', 'beta', 'merged']);
|
|
76
|
+
const reply = JSON.stringify({
|
|
77
|
+
memories: [{
|
|
78
|
+
id: 'merged',
|
|
79
|
+
scope: 'global',
|
|
80
|
+
kind: 'fact',
|
|
81
|
+
title: '合并后的记忆',
|
|
82
|
+
body: '内容。',
|
|
83
|
+
appliesTo: '任何时候',
|
|
84
|
+
derivedFrom: ['alpha', 'alpha', 'ghost', 'merged', 'beta'],
|
|
85
|
+
}],
|
|
86
|
+
retire: ['alpha', 'beta'],
|
|
87
|
+
notes: 'merged',
|
|
88
|
+
});
|
|
89
|
+
const plan = parsePlan(reply, known, 10);
|
|
90
|
+
assert.ok(plan !== undefined);
|
|
91
|
+
assert.deepEqual(plan.upserts[0]?.derivedFrom, ['alpha', 'beta']);
|
|
92
|
+
});
|
|
93
|
+
test('a plan with no derivedFrom leaves the field off entirely', () => {
|
|
94
|
+
const reply = JSON.stringify({
|
|
95
|
+
memories: [{ id: null, scope: 'global', kind: 'fact', title: '新条目', body: '内容。', appliesTo: '任何时候' }],
|
|
96
|
+
notes: 'added',
|
|
97
|
+
});
|
|
98
|
+
const plan = parsePlan(reply, new Set(['alpha']), 10);
|
|
99
|
+
assert.ok(plan !== undefined);
|
|
100
|
+
assert.equal(plan.upserts[0]?.derivedFrom, undefined);
|
|
101
|
+
});
|
|
102
|
+
test('a merge trail naming only unknown ids is dropped rather than stored as a dead reference', () => {
|
|
103
|
+
const reply = JSON.stringify({
|
|
104
|
+
memories: [{ id: 'merged', scope: 'global', kind: 'fact', title: '合并后的记忆', body: '内容。', appliesTo: '任何时候', derivedFrom: ['ghost'] }],
|
|
105
|
+
notes: 'merged',
|
|
106
|
+
});
|
|
107
|
+
const plan = parsePlan(reply, new Set(['merged']), 10);
|
|
108
|
+
assert.ok(plan !== undefined);
|
|
109
|
+
assert.equal(plan.upserts[0]?.derivedFrom, undefined);
|
|
110
|
+
});
|