dsh-cross-memory 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +253 -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 +1660 -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 +4482 -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 +163 -0
- package/lib/query.js +93 -0
- package/lib/raw-recall.js +286 -0
- package/lib/reflection.js +291 -0
- package/lib/remote.js +490 -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 +341 -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 +142 -0
- package/lib/test/persona.test.js +86 -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 +238 -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 +76 -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 +246 -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,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the one-hop expansion: following the keys and tags the direct hits
|
|
3
|
+
* are filed under.
|
|
4
|
+
*
|
|
5
|
+
* The corpus is built so that the related entries share *no word* with the
|
|
6
|
+
* query — if they did, they would be ordinary hits and the expansion would be
|
|
7
|
+
* untestable. That is also the property the feature has to have: a hop must
|
|
8
|
+
* never be presented as a match.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/expand.test
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import test from 'node:test';
|
|
14
|
+
import { EXPAND_FACTOR, EXPAND_MAX, relatedHits, searchMemories } from '../search.js';
|
|
15
|
+
import { renderHit } from '../render.js';
|
|
16
|
+
/** Fixed clock so decay never decides these assertions. */
|
|
17
|
+
const NOW = new Date(2026, 8, 29, 19, 0, 0).getTime();
|
|
18
|
+
/** One corpus entry. */
|
|
19
|
+
function entry(id, title, body, extra = {}, kind = 'fact') {
|
|
20
|
+
return {
|
|
21
|
+
id,
|
|
22
|
+
scope: 'global',
|
|
23
|
+
kind,
|
|
24
|
+
title,
|
|
25
|
+
body,
|
|
26
|
+
tags: [],
|
|
27
|
+
keys: [],
|
|
28
|
+
createdAt: NOW - 86_400_000,
|
|
29
|
+
updatedAt: NOW - 86_400_000,
|
|
30
|
+
uses: 0,
|
|
31
|
+
lastUsedAt: 0,
|
|
32
|
+
lastSurfacedAt: 0,
|
|
33
|
+
source: 'auto',
|
|
34
|
+
...extra,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** The seed, plus two entries filed under the same keys and one under a single key. */
|
|
38
|
+
const SEED = entry('state-db', 'SQLite 状态库在哪', '状态库放在 DSH_HOME 下。', { keys: ['state.db', 'sqlite'] });
|
|
39
|
+
const NEIGHBOUR = entry('leases', '任务租约与水位线', '水位线和任务租约都记在那个库里,重启后仍然有效。', { keys: ['state.db', 'sqlite'], updatedAt: NOW - 3_600_000 });
|
|
40
|
+
const NEIGHBOUR_BY_TAG = entry('watermarks', '后台抽取的水位线', '每个会话一条水位线,记录抽到哪一条消息。', { tags: ['state.db', 'sqlite'] });
|
|
41
|
+
const WEAK_LINK = entry('weak', '只共用一个 key 的记忆', '它只提到那个库一次。', { keys: ['state.db'] });
|
|
42
|
+
/**
|
|
43
|
+
* Entries carrying links of their own and nothing else.
|
|
44
|
+
*
|
|
45
|
+
* They exist to give the corpus a realistic size. A link is only an edge when
|
|
46
|
+
* it is not on most of the scope, and a four-entry corpus makes every link look
|
|
47
|
+
* ubiquitous — which is the real behaviour, measured, not a test artefact: a
|
|
48
|
+
* link on 3 of 4 entries really does mean nothing.
|
|
49
|
+
*/
|
|
50
|
+
const FILLERS = Array.from({ length: 12 }, (_, index) => entry(`filler-${index}`, `无关条目 ${index}`, '另一件事的正文。', { keys: [`unique-${index}`] }));
|
|
51
|
+
const GROUPS = [{ scope: 'global', label: 'global', entries: [SEED, NEIGHBOUR, NEIGHBOUR_BY_TAG, WEAK_LINK, ...FILLERS] }];
|
|
52
|
+
test('a hop needs two shared links, not one', () => {
|
|
53
|
+
const hits = searchMemories(GROUPS, '状态库', { expand: true, now: NOW, limit: 10 });
|
|
54
|
+
const ids = hits.map((hit) => hit.entry.id);
|
|
55
|
+
assert.equal(ids[0], 'state-db', 'the direct hit still comes first');
|
|
56
|
+
assert.ok(ids.includes('leases'), 'two shared keys are a link');
|
|
57
|
+
assert.ok(ids.includes('watermarks'), 'two shared tags are a link too');
|
|
58
|
+
assert.ok(!ids.includes('weak'), 'one shared key is not');
|
|
59
|
+
});
|
|
60
|
+
test('an unexpanded search returns only what matched', () => {
|
|
61
|
+
assert.deepEqual(searchMemories(GROUPS, '状态库', { now: NOW, limit: 10 }).map((hit) => hit.entry.id), ['state-db']);
|
|
62
|
+
});
|
|
63
|
+
test('every related hit ranks below every direct hit', () => {
|
|
64
|
+
// The search surface orders by relevance, so this compares *places* rather
|
|
65
|
+
// than numbers: the hop's exact value is asserted where it is computed, on
|
|
66
|
+
// `relatedHits`, whose seeds carry explicit scores.
|
|
67
|
+
const hits = searchMemories(GROUPS, '状态库', { expand: true, now: NOW, limit: 10 });
|
|
68
|
+
const direct = hits.filter((hit) => hit.via === undefined);
|
|
69
|
+
const related = hits.filter((hit) => hit.via !== undefined);
|
|
70
|
+
assert.deepEqual(direct.map((hit) => hit.entry.id), ['state-db']);
|
|
71
|
+
assert.ok(related.length > 0);
|
|
72
|
+
const lastDirect = hits.findLastIndex((hit) => hit.via === undefined);
|
|
73
|
+
const firstRelated = hits.findIndex((hit) => hit.via !== undefined);
|
|
74
|
+
assert.ok(firstRelated > lastDirect, `a hop came before a direct hit: ${hits.map((hit) => `${hit.entry.id}${hit.via === undefined ? '' : '*'}`).join(', ')}`);
|
|
75
|
+
for (const hit of related)
|
|
76
|
+
assert.equal(hit.via, 'state-db', 'the seed it came from is reported');
|
|
77
|
+
});
|
|
78
|
+
test('a link on most of the scope is not an edge', () => {
|
|
79
|
+
// The measured case: in the plugin's own project scope `dsh-cross-memory` sits on
|
|
80
|
+
// 22 of 29 entries, and 18 of its 48 hop-able pairs shared nothing but such
|
|
81
|
+
// links. Here the seed and its neighbour share two links that everything else
|
|
82
|
+
// carries too, so the hop must not happen at all.
|
|
83
|
+
const everywhere = ['common-a', 'common-b'];
|
|
84
|
+
const corpus = [
|
|
85
|
+
entry('seed', '状态库在哪', '状态库放在这里。', { keys: everywhere }),
|
|
86
|
+
entry('neighbour', '另一条', '也说到了状态库。', { keys: everywhere }),
|
|
87
|
+
...Array.from({ length: 6 }, (_, index) => entry(`filler-${index}`, `无关 ${index}`, '无关正文。', { keys: everywhere })),
|
|
88
|
+
];
|
|
89
|
+
const groups = [{ scope: 'global', label: 'global', entries: corpus }];
|
|
90
|
+
const hits = searchMemories(groups, '状态库', { expand: true, now: NOW, limit: 10 });
|
|
91
|
+
assert.ok(hits.every((hit) => hit.via === undefined), 'no hop fired');
|
|
92
|
+
assert.ok(hits.some((hit) => hit.entry.id === 'neighbour'), 'the other direct match is still there');
|
|
93
|
+
});
|
|
94
|
+
test('expansion never grows the result past the limit', () => {
|
|
95
|
+
const crowded = [SEED];
|
|
96
|
+
for (let index = 0; index < 6; index += 1) {
|
|
97
|
+
crowded.push(entry(`n${index}`, `邻居 ${index}`, '同样是那个库的记录。', { keys: ['state.db', 'sqlite'] }));
|
|
98
|
+
}
|
|
99
|
+
const groups = [{ scope: 'global', label: 'global', entries: [...crowded, ...FILLERS] }];
|
|
100
|
+
const hits = searchMemories(groups, '状态库', { expand: true, now: NOW, limit: 10 });
|
|
101
|
+
assert.equal(hits.length, 1 + EXPAND_MAX);
|
|
102
|
+
});
|
|
103
|
+
test('expansion respects the scope and kind filters it was given', () => {
|
|
104
|
+
const projectCopy = entry('project-lease', '项目里的租约', '同样的东西,不同的作用域。', { scope: 'project', keys: ['state.db', 'sqlite'] });
|
|
105
|
+
const groups = [
|
|
106
|
+
{ scope: 'global', label: 'global', entries: [SEED, NEIGHBOUR, ...FILLERS] },
|
|
107
|
+
{ scope: 'project', label: 'project:x', entries: [projectCopy] },
|
|
108
|
+
];
|
|
109
|
+
const scoped = searchMemories(groups, '状态库', { expand: true, now: NOW, limit: 10, scopes: ['global'] });
|
|
110
|
+
assert.deepEqual(scoped.map((hit) => hit.entry.id), ['state-db', 'leases']);
|
|
111
|
+
const kinds = searchMemories(groups, '状态库', { expand: true, now: NOW, limit: 10, kinds: ['procedure'] });
|
|
112
|
+
assert.deepEqual(kinds, [], 'a filtered search does not smuggle entries back in');
|
|
113
|
+
});
|
|
114
|
+
test('relatedHits reports the seed and can be bounded directly', () => {
|
|
115
|
+
const seeds = [{ entry: SEED, score: 100 }];
|
|
116
|
+
const related = relatedHits(GROUPS, seeds, { limit: 1 });
|
|
117
|
+
assert.equal(related.length, 1);
|
|
118
|
+
assert.equal(related[0]?.entry.id, 'leases', 'the strongest hop wins the single slot');
|
|
119
|
+
assert.equal(related[0]?.via, 'state-db');
|
|
120
|
+
assert.deepEqual(relatedHits(GROUPS, []), [], 'no seeds, no hops');
|
|
121
|
+
});
|
|
122
|
+
test('a related hit is marked in the rendering', () => {
|
|
123
|
+
const direct = renderHit({ entry: SEED, score: 10 }, 0);
|
|
124
|
+
const hop = renderHit({ entry: NEIGHBOUR, score: 4, via: 'state-db' }, 1);
|
|
125
|
+
assert.ok(!direct.includes('via='));
|
|
126
|
+
assert.ok(hop.includes('via=state-db'), hop);
|
|
127
|
+
});
|
|
128
|
+
test('a hop is scored from the seed that lit it up, not from the weakest seed', () => {
|
|
129
|
+
// The real-store eval forced this: pegging every hop to the weakest of the top
|
|
130
|
+
// three put the best match's neighbours below entries that shared one word
|
|
131
|
+
// with the turn. Activation spreads from the node that fired.
|
|
132
|
+
const strong = entry('strong', '强命中', '正文。', { keys: ['k1', 'k2'] });
|
|
133
|
+
const weak = entry('weak-seed', '弱命中', '正文。', { keys: ['k3', 'k4'] });
|
|
134
|
+
const nearStrong = entry('near-strong', '强种子的邻居', '正文。', { keys: ['k1', 'k2'] });
|
|
135
|
+
const nearWeak = entry('near-weak', '弱种子的邻居', '正文。', { keys: ['k3', 'k4'] });
|
|
136
|
+
const groups = [{ scope: 'global', label: 'global', entries: [nearStrong, nearWeak] }];
|
|
137
|
+
const related = relatedHits(groups, [{ entry: strong, score: 100 }, { entry: weak, score: 20 }], {});
|
|
138
|
+
assert.deepEqual(related.map((hit) => [hit.entry.id, hit.score, hit.via]), [
|
|
139
|
+
['near-strong', 100 * EXPAND_FACTOR * 0.5, 'strong'],
|
|
140
|
+
['near-weak', 20 * EXPAND_FACTOR * 0.5, 'weak-seed'],
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
test('a weak direct hit is upgraded by the hop, and never marked as a hop', () => {
|
|
144
|
+
// The eval found this: the eighth direct hit is exactly the entry a hop can
|
|
145
|
+
// lift, and "already a direct hit" must not disqualify it — but it did match
|
|
146
|
+
// the query, so it must not be labelled `via` either. The lift is asserted on
|
|
147
|
+
// the number (`relevance`), because whether it also changes the *place*
|
|
148
|
+
// depends on who else is in the corpus.
|
|
149
|
+
const keys = ['k1', 'k2', 'k3', 'k4'];
|
|
150
|
+
const seed = entry('seed', '状态库放在哪里', '状态库的位置记在这里。', { keys });
|
|
151
|
+
const weakHit = entry('weak-hit', '另有一条', '正文里提了一句状态库。', { keys });
|
|
152
|
+
// Three entries that outrank the weak hit, so it is not itself one of the
|
|
153
|
+
// seeds: a seed is excluded from the hop, and with a two-entry corpus the
|
|
154
|
+
// thing under test would have been its own seed.
|
|
155
|
+
const fillers = [1, 2, 3].map((index) => entry(`filler-${index}`, `状态库相关 ${index}`, '无关正文。'));
|
|
156
|
+
const groups = [{ scope: 'global', label: 'global', entries: [seed, weakHit, ...fillers] }];
|
|
157
|
+
const plain = searchMemories(groups, '状态库', { now: NOW, limit: 5 });
|
|
158
|
+
const expanded = searchMemories(groups, '状态库', { now: NOW, limit: 5, expand: true });
|
|
159
|
+
const before = plain.find((hit) => hit.entry.id === 'weak-hit');
|
|
160
|
+
const after = expanded.find((hit) => hit.entry.id === 'weak-hit');
|
|
161
|
+
assert.ok(before !== undefined && after !== undefined);
|
|
162
|
+
assert.ok((after.relevance ?? 0) > (before.relevance ?? 0), `the hop did not lift it: ${before.relevance} -> ${after.relevance}`);
|
|
163
|
+
assert.equal(after.via, undefined, 'it matched the query, so it is not a hop result');
|
|
164
|
+
});
|