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,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the Host→browser Remote surface.
|
|
3
|
+
*
|
|
4
|
+
* Two contracts matter here. The manifest must satisfy the registry's own
|
|
5
|
+
* validation rules — a descriptor the registry rejects means the Settings page
|
|
6
|
+
* silently never appears, and the failure would otherwise surface only in a
|
|
7
|
+
* running browser. The service must keep every path it is handed inside the
|
|
8
|
+
* store: a project is addressed by a slug the store already knows, never by a
|
|
9
|
+
* caller-supplied path.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-cross-memory/test/remote.test
|
|
12
|
+
*/
|
|
13
|
+
import assert from 'node:assert/strict';
|
|
14
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
15
|
+
import { tmpdir } from 'node:os';
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
import test from 'node:test';
|
|
18
|
+
import { REMOTE_CONTRIBUTION, REMOTE_INVOCATION_DATA, REMOTE_NAMESPACE, REMOTE_PACKAGE, REMOTE_SERVICE, createRemoteService, expandInvocations } from '../remote.js';
|
|
19
|
+
import { MemoryStore } from '../storage.js';
|
|
20
|
+
import { writeDraft } from '../skills.js';
|
|
21
|
+
/** A temp memory store plus a temp harness home. */
|
|
22
|
+
async function roots(t) {
|
|
23
|
+
const memoriesDir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-remote-'));
|
|
24
|
+
const dshHome = await mkdtemp(join(tmpdir(), 'dsh-home-remote-'));
|
|
25
|
+
t.after(async () => {
|
|
26
|
+
await rm(memoriesDir, { recursive: true, force: true });
|
|
27
|
+
await rm(dshHome, { recursive: true, force: true });
|
|
28
|
+
});
|
|
29
|
+
return { memoriesDir, dshHome, store: new MemoryStore(memoriesDir) };
|
|
30
|
+
}
|
|
31
|
+
/** The registry's grammar for one wire segment. */
|
|
32
|
+
const WIRE = /^[A-Za-z0-9_$.-]+$/u;
|
|
33
|
+
test('every invocation satisfies the registry validation rules', () => {
|
|
34
|
+
const endpoints = new Set();
|
|
35
|
+
const ids = new Set();
|
|
36
|
+
for (const descriptor of REMOTE_CONTRIBUTION.invocations) {
|
|
37
|
+
assert.ok(descriptor.id.length > 0);
|
|
38
|
+
assert.ok(descriptor.service.length > 0 && !descriptor.service.includes('#'));
|
|
39
|
+
assert.match(descriptor.namespace, WIRE);
|
|
40
|
+
assert.match(descriptor.method, WIRE);
|
|
41
|
+
const endpoint = `${descriptor.namespace}/${descriptor.method}`;
|
|
42
|
+
assert.equal(endpoints.has(endpoint), false, `duplicate endpoint ${endpoint}`);
|
|
43
|
+
assert.equal(ids.has(descriptor.id), false, `duplicate id ${descriptor.id}`);
|
|
44
|
+
endpoints.add(endpoint);
|
|
45
|
+
ids.add(descriptor.id);
|
|
46
|
+
// Strict codecs need a nonempty type symbol and a create() factory whose
|
|
47
|
+
// schema parses; the registry rejects any other strict codec shape.
|
|
48
|
+
const codecs = [descriptor.result, ...descriptor.parameters.map((parameter) => parameter.codec)];
|
|
49
|
+
for (const codec of codecs) {
|
|
50
|
+
assert.equal(codec.mode, 'strict');
|
|
51
|
+
assert.ok(codec.typeSymbol.length > 0);
|
|
52
|
+
assert.equal(typeof codec.create, 'function');
|
|
53
|
+
assert.equal(typeof codec.create().parse, 'function');
|
|
54
|
+
}
|
|
55
|
+
const wires = new Set();
|
|
56
|
+
for (const parameter of descriptor.parameters) {
|
|
57
|
+
assert.match(parameter.name, WIRE);
|
|
58
|
+
assert.match(parameter.wire, WIRE);
|
|
59
|
+
assert.equal(parameter.source, 'json');
|
|
60
|
+
assert.equal(wires.has(parameter.wire), false, `duplicate wire field ${parameter.wire}`);
|
|
61
|
+
wires.add(parameter.wire);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
assert.equal(REMOTE_CONTRIBUTION.package, REMOTE_PACKAGE);
|
|
65
|
+
assert.equal(REMOTE_CONTRIBUTION.face, 'host');
|
|
66
|
+
assert.deepEqual(REMOTE_CONTRIBUTION.schemas, []);
|
|
67
|
+
});
|
|
68
|
+
test('the wire table and the expanded descriptors agree', () => {
|
|
69
|
+
const seen = new Set();
|
|
70
|
+
const expanded = expandInvocations(REMOTE_INVOCATION_DATA, (shape) => {
|
|
71
|
+
seen.add(shape);
|
|
72
|
+
return { mode: 'strict', typeSymbol: shape, create: () => ({ parse: (value) => value }) };
|
|
73
|
+
});
|
|
74
|
+
assert.equal(expanded.length, REMOTE_INVOCATION_DATA.length);
|
|
75
|
+
assert.deepEqual(expanded.map((descriptor) => descriptor.method), REMOTE_INVOCATION_DATA.map((invocation) => invocation.method));
|
|
76
|
+
for (const [index, invocation] of REMOTE_INVOCATION_DATA.entries()) {
|
|
77
|
+
const descriptor = expanded[index];
|
|
78
|
+
assert.equal(descriptor?.service, REMOTE_SERVICE);
|
|
79
|
+
assert.equal(descriptor?.namespace, REMOTE_NAMESPACE);
|
|
80
|
+
assert.equal(descriptor?.id, `${REMOTE_PACKAGE}#${REMOTE_NAMESPACE}/${invocation.method}`);
|
|
81
|
+
assert.deepEqual(descriptor?.parameters.map((parameter) => parameter.wire), invocation.parameters.map((parameter) => parameter.wire));
|
|
82
|
+
// Optional parameters must accept a missing wire field; the gateway
|
|
83
|
+
// otherwise rejects the call as arguments-invalid.
|
|
84
|
+
assert.deepEqual(descriptor?.parameters.map((parameter) => parameter.acceptsUndefined === true), invocation.parameters.map((parameter) => parameter.optional === true));
|
|
85
|
+
}
|
|
86
|
+
// Every shape the browser renders is covered by a codec factory call.
|
|
87
|
+
assert.ok(seen.has('overview') && seen.has('entries') && seen.has('entry') && seen.has('removed'));
|
|
88
|
+
assert.ok(seen.has('skill') && seen.has('discard') && seen.has('text') && seen.has('number'));
|
|
89
|
+
assert.ok(seen.has('persona') && seen.has('flag'));
|
|
90
|
+
});
|
|
91
|
+
test('the host codecs validate instead of passing values through', () => {
|
|
92
|
+
const byMethod = new Map(REMOTE_CONTRIBUTION.invocations.map((descriptor) => [descriptor.method, descriptor]));
|
|
93
|
+
const overview = byMethod.get('overview');
|
|
94
|
+
assert.ok(overview !== undefined);
|
|
95
|
+
assert.throws(() => overview.result.create().parse({}), /storePath must be a string/u);
|
|
96
|
+
assert.throws(() => overview.result.create().parse({ storePath: 'x', globalCount: 1, projects: [], drafts: [], kinds: 'nope' }), /must be an array/u);
|
|
97
|
+
const list = byMethod.get('list');
|
|
98
|
+
assert.ok(list !== undefined);
|
|
99
|
+
const textCodec = list.parameters.find((parameter) => parameter.wire === 'query')?.codec;
|
|
100
|
+
const numberCodec = list.parameters.find((parameter) => parameter.wire === 'limit')?.codec;
|
|
101
|
+
assert.equal(textCodec?.create().parse('hello'), 'hello');
|
|
102
|
+
assert.throws(() => textCodec?.create().parse(7), /must be a string/u);
|
|
103
|
+
assert.equal(numberCodec?.create().parse(10), 10);
|
|
104
|
+
assert.throws(() => numberCodec?.create().parse('10'), /must be a number/u);
|
|
105
|
+
});
|
|
106
|
+
test('the settings page can read, save and lock the persona profile', async (t) => {
|
|
107
|
+
const { store, dshHome } = await roots(t);
|
|
108
|
+
const service = createRemoteService({ store, dshHome });
|
|
109
|
+
assert.deepEqual(await service.persona(), { text: '', locked: false }, 'an empty store has no profile');
|
|
110
|
+
const saved = await service.savePersona('他先确认再改代码。', true);
|
|
111
|
+
assert.deepEqual(saved, { text: '他先确认再改代码。', locked: true });
|
|
112
|
+
assert.deepEqual(await service.persona(), saved, 'the saved document and its lock are read back');
|
|
113
|
+
await service.savePersona('', false);
|
|
114
|
+
assert.deepEqual(await service.persona(), { text: '', locked: false }, 'saving an empty document clears it');
|
|
115
|
+
});
|
|
116
|
+
test('the settings page can list and resolve the decisions waiting for a person', async (t) => {
|
|
117
|
+
const { store, dshHome } = await roots(t);
|
|
118
|
+
const seen = [];
|
|
119
|
+
// A store without a runtime has no decisions: the page must render, not fail.
|
|
120
|
+
const bare = createRemoteService({ store, dshHome });
|
|
121
|
+
assert.deepEqual(await bare.disputes(), { disputes: [] });
|
|
122
|
+
assert.match(await bare.resolveDispute('x', 'accept'), /No decisions are waiting/u);
|
|
123
|
+
const service = createRemoteService({
|
|
124
|
+
store,
|
|
125
|
+
dshHome,
|
|
126
|
+
disputes: async () => [{
|
|
127
|
+
id: 'a', scope: 'global', action: 'retire', title: 'A', reason: '它被读到过 3 次', before: 'body', after: '',
|
|
128
|
+
}],
|
|
129
|
+
resolveDispute: async (id, decision) => {
|
|
130
|
+
seen.push({ id, decision });
|
|
131
|
+
return `Kept “${id}”.`;
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
const listed = await service.disputes();
|
|
135
|
+
assert.equal(listed.disputes[0]?.reason, '它被读到过 3 次');
|
|
136
|
+
assert.equal(listed.disputes[0]?.action, 'retire');
|
|
137
|
+
assert.equal(await service.resolveDispute('a', 'reject'), 'Kept “a”.');
|
|
138
|
+
assert.deepEqual(seen, [{ id: 'a', decision: 'reject' }]);
|
|
139
|
+
// A missing id, or an unknown decision word, must not reach the runtime as a
|
|
140
|
+
// destructive default.
|
|
141
|
+
assert.match(await service.resolveDispute('', 'accept'), /No decision id/u);
|
|
142
|
+
await service.resolveDispute('a', 'delete-everything');
|
|
143
|
+
assert.equal(seen[1]?.decision, 'accept', 'anything that is not reject is treated as accept');
|
|
144
|
+
});
|
|
145
|
+
test('the service carries the typertRemote binding the gateway validates', async (t) => {
|
|
146
|
+
const { store, dshHome } = await roots(t);
|
|
147
|
+
const service = createRemoteService({ store, dshHome });
|
|
148
|
+
const binding = service.typertRemote;
|
|
149
|
+
assert.equal(binding?.service, service);
|
|
150
|
+
assert.equal(binding?.serviceKey, REMOTE_SERVICE);
|
|
151
|
+
assert.equal(binding?.namespace, REMOTE_NAMESPACE);
|
|
152
|
+
// Non-enumerable: the binding must not become part of the service surface.
|
|
153
|
+
assert.equal(Object.keys(service).includes('typertRemote'), false);
|
|
154
|
+
});
|
|
155
|
+
test('overview reports both scopes and the staged drafts', async (t) => {
|
|
156
|
+
const { store, dshHome } = await roots(t);
|
|
157
|
+
await store.upsert({ scope: 'global', title: 'Global rule', body: 'Applies everywhere.', tags: ['rule'] }, undefined, 'user');
|
|
158
|
+
await store.upsert({ scope: 'project', title: 'Project rule', body: 'Applies here.', tags: [] }, join(t.name), 'user');
|
|
159
|
+
await writeDraft(store.memoriesDir, { name: 'demo-skill', description: 'A demo', steps: ['Do it.'] });
|
|
160
|
+
const service = createRemoteService({ store, dshHome });
|
|
161
|
+
const view = await service.overview();
|
|
162
|
+
assert.equal(view.storePath, store.memoriesDir);
|
|
163
|
+
assert.equal(view.globalCount, 1);
|
|
164
|
+
assert.equal(view.projects.length, 1);
|
|
165
|
+
assert.equal(view.projects[0]?.count, 1);
|
|
166
|
+
assert.equal(view.drafts.length, 1);
|
|
167
|
+
assert.equal(view.drafts[0]?.name, 'demo-skill');
|
|
168
|
+
assert.equal(view.drafts[0]?.promoted, false);
|
|
169
|
+
assert.ok(view.kinds.includes('preference'));
|
|
170
|
+
});
|
|
171
|
+
test('every service method takes one positional parameter per descriptor parameter', async (t) => {
|
|
172
|
+
const { store, dshHome } = await roots(t);
|
|
173
|
+
const service = createRemoteService({ store, dshHome });
|
|
174
|
+
for (const descriptor of REMOTE_CONTRIBUTION.invocations) {
|
|
175
|
+
const method = service[descriptor.method];
|
|
176
|
+
assert.ok(typeof method === 'function', `${descriptor.method} is missing from the service`);
|
|
177
|
+
// The gateway applies the resolved arguments positionally, so an object-shaped
|
|
178
|
+
// signature would receive the first string as its whole argument.
|
|
179
|
+
assert.equal(method.length, descriptor.parameters.length, `${descriptor.method} arity`);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
test('list browses, searches, and filters without leaving the store', async (t) => {
|
|
183
|
+
const { store, dshHome } = await roots(t);
|
|
184
|
+
const root = join(t.name, 'workspace');
|
|
185
|
+
await store.upsert({ scope: 'global', title: 'Commit in Chinese', body: 'Write commit messages in Chinese.', tags: ['git'], kind: 'preference' }, undefined, 'user');
|
|
186
|
+
await store.upsert({ scope: 'project', title: 'Build with tsc', body: 'Compile with tsc before tests.', tags: ['build'], kind: 'procedure' }, root, 'user');
|
|
187
|
+
const service = createRemoteService({ store, dshHome });
|
|
188
|
+
const slug = (await store.listProjects())[0];
|
|
189
|
+
assert.ok(slug !== undefined);
|
|
190
|
+
const all = await service.list(slug, '', '', '', 50);
|
|
191
|
+
assert.equal(all.total, 2);
|
|
192
|
+
assert.equal(all.entries.length, 2);
|
|
193
|
+
const globalOnly = await service.list(slug, 'global', '', '', 50);
|
|
194
|
+
assert.deepEqual(globalOnly.entries.map((entry) => entry.title), ['Commit in Chinese']);
|
|
195
|
+
const projectOnly = await service.list(slug, 'project', '', '', 50);
|
|
196
|
+
assert.deepEqual(projectOnly.entries.map((entry) => entry.title), ['Build with tsc']);
|
|
197
|
+
assert.equal(projectOnly.entries[0]?.appliesTo, '');
|
|
198
|
+
const ranked = await service.list(slug, '', 'tsc', '', 50);
|
|
199
|
+
assert.deepEqual(ranked.entries.map((entry) => entry.title), ['Build with tsc']);
|
|
200
|
+
const byKind = await service.list(slug, '', '', 'preference', 50);
|
|
201
|
+
assert.deepEqual(byKind.entries.map((entry) => entry.title), ['Commit in Chinese']);
|
|
202
|
+
// An unknown slug is an empty project scope, never a path the caller chose.
|
|
203
|
+
const unknown = await service.list('../../etc', 'project', '', '', 50);
|
|
204
|
+
assert.deepEqual(unknown.entries, []);
|
|
205
|
+
});
|
|
206
|
+
test('add writes through the store and rejects what it cannot store', async (t) => {
|
|
207
|
+
const { store, dshHome } = await roots(t);
|
|
208
|
+
const service = createRemoteService({ store, dshHome });
|
|
209
|
+
const created = await service.add(undefined, 'global', 'preference', 'Prefer pnpm', 'Use pnpm, never npm.', 'tooling, packages');
|
|
210
|
+
assert.equal(created.entry.id, 'prefer-pnpm');
|
|
211
|
+
assert.equal(created.entry.kind, 'preference');
|
|
212
|
+
assert.deepEqual(created.entry.tags, ['tooling', 'packages']);
|
|
213
|
+
assert.equal(created.entry.source, 'user');
|
|
214
|
+
assert.equal((await store.list('global', undefined)).length, 1);
|
|
215
|
+
await assert.rejects(() => service.add(undefined, 'global', 'fact', '', 'x', ''), /title and a body/u);
|
|
216
|
+
await assert.rejects(() => service.add('nope', 'project', 'fact', 'T', 'B', ''), /no project scope matches/u);
|
|
217
|
+
});
|
|
218
|
+
test('forget removes one entry and reports the truth', async (t) => {
|
|
219
|
+
const { store, dshHome } = await roots(t);
|
|
220
|
+
await store.upsert({ scope: 'global', title: 'Temporary', body: 'Gone soon.', tags: [] }, undefined, 'user');
|
|
221
|
+
const service = createRemoteService({ store, dshHome });
|
|
222
|
+
assert.deepEqual(await service.forget(undefined, 'global', 'temporary'), { removed: true });
|
|
223
|
+
assert.deepEqual(await service.forget(undefined, 'global', 'temporary'), { removed: false });
|
|
224
|
+
// A project-scoped forget with an unknown slug cannot touch anything.
|
|
225
|
+
assert.deepEqual(await service.forget('missing', 'project', 'temporary'), { removed: false });
|
|
226
|
+
});
|
|
227
|
+
test('skill promotion is explicit and idempotent over the Remote surface', async (t) => {
|
|
228
|
+
const { store, dshHome } = await roots(t);
|
|
229
|
+
await writeDraft(store.memoriesDir, { name: 'demo-skill', description: 'A demo', steps: ['Do it.'] });
|
|
230
|
+
const service = createRemoteService({ store, dshHome });
|
|
231
|
+
const first = await service.promoteSkill('demo-skill');
|
|
232
|
+
assert.equal(first.path, join(dshHome, 'skills', 'demo-skill', 'SKILL.md'));
|
|
233
|
+
assert.equal((await service.overview()).drafts[0]?.promoted, true);
|
|
234
|
+
assert.equal((await service.promoteSkill('demo-skill')).name, 'demo-skill');
|
|
235
|
+
await assert.rejects(() => service.promoteSkill('nope'), /no staged skill draft/u);
|
|
236
|
+
assert.deepEqual(await service.discardSkill('demo-skill'), { name: 'demo-skill', removed: true });
|
|
237
|
+
assert.deepEqual(await service.discardSkill('demo-skill'), { name: 'demo-skill', removed: false });
|
|
238
|
+
});
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for deterministic retention.
|
|
3
|
+
*
|
|
4
|
+
* These pin the rule that makes "unused memories eventually archive" true: age
|
|
5
|
+
* is measured from the last time an entry was read, surfaced, or written by a
|
|
6
|
+
* person — never from a consolidation rewrite — and a memory somebody typed is
|
|
7
|
+
* never chosen at all.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-cross-memory/test/retention.test
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import test from 'node:test';
|
|
13
|
+
import { lastAttention, planRetention, retentionKey } from '../retention.js';
|
|
14
|
+
/** One day in milliseconds. */
|
|
15
|
+
const DAY = 86_400_000;
|
|
16
|
+
/** A clock far enough from the epoch that ages read naturally. */
|
|
17
|
+
const NOW = 10_000 * DAY;
|
|
18
|
+
/** Build one entry from the fields a case cares about. */
|
|
19
|
+
function entry(partial) {
|
|
20
|
+
return {
|
|
21
|
+
scope: 'global',
|
|
22
|
+
kind: 'fact',
|
|
23
|
+
title: partial.id,
|
|
24
|
+
body: 'body',
|
|
25
|
+
tags: [],
|
|
26
|
+
keys: [],
|
|
27
|
+
createdAt: 0,
|
|
28
|
+
updatedAt: 0,
|
|
29
|
+
uses: 0,
|
|
30
|
+
lastUsedAt: 0,
|
|
31
|
+
lastSurfacedAt: 0,
|
|
32
|
+
source: 'auto',
|
|
33
|
+
...partial,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** Build one usage row. */
|
|
37
|
+
function row(partial) {
|
|
38
|
+
return { scope: 'global', uses: 0, lastUsedAt: 0, surfacedAt: 0, consolidatedAt: 0, ...partial };
|
|
39
|
+
}
|
|
40
|
+
test('a snapshot expires on when it was measured, not on when it was read', () => {
|
|
41
|
+
const snapshot = entry({
|
|
42
|
+
id: 'measured',
|
|
43
|
+
durability: 'snapshot',
|
|
44
|
+
asOf: NOW - 70 * DAY,
|
|
45
|
+
createdAt: NOW - 70 * DAY,
|
|
46
|
+
lastUsedAt: NOW - 1 * DAY,
|
|
47
|
+
lastSurfacedAt: NOW - 1 * DAY,
|
|
48
|
+
});
|
|
49
|
+
const fresh = entry({ id: 'recent', durability: 'snapshot', asOf: NOW - 5 * DAY, createdAt: NOW - 5 * DAY });
|
|
50
|
+
// Read yesterday, but the reading is 70 days old: being read does not make a
|
|
51
|
+
// number right again, which is why snapshots do not use the unused clock.
|
|
52
|
+
const decisions = planRetention([snapshot, fresh], [row({ id: 'measured', lastUsedAt: NOW - DAY })], {
|
|
53
|
+
maxUnusedDays: 90,
|
|
54
|
+
snapshotMaxAgeDays: 60,
|
|
55
|
+
now: NOW,
|
|
56
|
+
});
|
|
57
|
+
assert.deepEqual(decisions.map((decision) => decision.id), ['measured']);
|
|
58
|
+
});
|
|
59
|
+
test('a snapshot expiry of zero keeps snapshots forever', () => {
|
|
60
|
+
const snapshot = entry({ id: 'measured', durability: 'snapshot', asOf: NOW - 500 * DAY, createdAt: NOW - 500 * DAY, lastUsedAt: NOW });
|
|
61
|
+
const decisions = planRetention([snapshot], [], { maxUnusedDays: 90, snapshotMaxAgeDays: 0, now: NOW });
|
|
62
|
+
assert.deepEqual(decisions, []);
|
|
63
|
+
});
|
|
64
|
+
test('a pinned memory is never archived for being unused', () => {
|
|
65
|
+
const pinned = entry({ id: 'pinned', pinned: true, createdAt: NOW - 400 * DAY });
|
|
66
|
+
const decisions = planRetention([pinned], [], { maxUnusedDays: 90, snapshotMaxAgeDays: 60, now: NOW });
|
|
67
|
+
assert.deepEqual(decisions, [], 'a pin is a decision a schedule may not overrule');
|
|
68
|
+
});
|
|
69
|
+
test('an entry unused past the limit is archived, and its age is reported', () => {
|
|
70
|
+
const decisions = planRetention([entry({ id: 'old', createdAt: NOW - 100 * DAY })], [], { maxUnusedDays: 90, now: NOW });
|
|
71
|
+
assert.equal(decisions.length, 1);
|
|
72
|
+
assert.equal(decisions[0]?.id, 'old');
|
|
73
|
+
assert.equal(Math.round(decisions[0]?.ageDays ?? 0), 100);
|
|
74
|
+
});
|
|
75
|
+
test('an entry exactly at the limit is kept', () => {
|
|
76
|
+
assert.deepEqual(planRetention([entry({ id: 'edge', createdAt: NOW - 90 * DAY })], [], { maxUnusedDays: 90, now: NOW }), []);
|
|
77
|
+
});
|
|
78
|
+
test('the state database can protect an entry the file says was never read', () => {
|
|
79
|
+
const entries = [entry({ id: 'read', createdAt: NOW - 400 * DAY })];
|
|
80
|
+
// The file predates the read, or a concurrent session has the newer counter;
|
|
81
|
+
// either way the state database is the authority.
|
|
82
|
+
const rows = [row({ id: 'read', uses: 3, lastUsedAt: NOW - 10 * DAY })];
|
|
83
|
+
assert.deepEqual(planRetention(entries, rows, { maxUnusedDays: 90, now: NOW }), []);
|
|
84
|
+
});
|
|
85
|
+
test('being surfaced counts as attention, so a memory read out of the summary survives', () => {
|
|
86
|
+
const entries = [entry({ id: 'surfaced', createdAt: NOW - 200 * DAY, lastSurfacedAt: NOW - 2 * DAY })];
|
|
87
|
+
assert.deepEqual(planRetention(entries, [], { maxUnusedDays: 90, now: NOW }), []);
|
|
88
|
+
});
|
|
89
|
+
test('a consolidation rewrite is not attention: a memory cannot be kept alive by rewriting it', () => {
|
|
90
|
+
const entries = [entry({ id: 'rewritten', createdAt: NOW - 300 * DAY, updatedAt: NOW })];
|
|
91
|
+
const decisions = planRetention(entries, [], { maxUnusedDays: 90, now: NOW });
|
|
92
|
+
assert.deepEqual(decisions.map((decision) => decision.id), ['rewritten']);
|
|
93
|
+
});
|
|
94
|
+
test('a memory a person wrote is never chosen, and 0 disables retention', () => {
|
|
95
|
+
const handwritten = [entry({ id: 'handwritten', createdAt: NOW - 900 * DAY, source: 'user' })];
|
|
96
|
+
assert.deepEqual(planRetention(handwritten, [], { maxUnusedDays: 90, now: NOW }), []);
|
|
97
|
+
const stale = [entry({ id: 'stale', createdAt: 0 })];
|
|
98
|
+
assert.deepEqual(planRetention(stale, [], { maxUnusedDays: 0, now: NOW }), []);
|
|
99
|
+
});
|
|
100
|
+
test('the longest-unused entry is chosen first', () => {
|
|
101
|
+
const decisions = planRetention([
|
|
102
|
+
entry({ id: 'recent', createdAt: NOW - 100 * DAY }),
|
|
103
|
+
entry({ id: 'ancient', createdAt: NOW - 500 * DAY }),
|
|
104
|
+
], [], { maxUnusedDays: 90, now: NOW });
|
|
105
|
+
assert.deepEqual(decisions.map((decision) => decision.id), ['ancient', 'recent']);
|
|
106
|
+
});
|
|
107
|
+
test('usage rows are joined per scope, so another workspace cannot protect an entry', () => {
|
|
108
|
+
const entries = [entry({ id: 'shared', scope: 'project', createdAt: NOW - 400 * DAY })];
|
|
109
|
+
const rows = [row({ id: 'shared', scope: 'global', uses: 5, lastUsedAt: NOW })];
|
|
110
|
+
assert.equal(planRetention(entries, rows, { maxUnusedDays: 90, now: NOW }).length, 1);
|
|
111
|
+
});
|
|
112
|
+
test('lastAttention takes the newest signal from either view', () => {
|
|
113
|
+
const value = entry({ id: 'x', createdAt: 5, lastUsedAt: 10 });
|
|
114
|
+
assert.equal(lastAttention(value, undefined), 10);
|
|
115
|
+
assert.equal(lastAttention(value, row({ id: 'x', surfacedAt: 42 })), 42);
|
|
116
|
+
assert.equal(retentionKey('project', 'x'), 'project\u0000x');
|
|
117
|
+
});
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for retrieval: how a turn becomes queries, and how a memory scores.
|
|
3
|
+
*
|
|
4
|
+
* The failure these pin down is specific and was measured on a real store. A
|
|
5
|
+
* Chinese question has no spaces, so under a whitespace tokenizer it is one
|
|
6
|
+
* indivisible term and only a whole-question substring could ever match — which
|
|
7
|
+
* meant every Chinese-only turn recalled nothing, while a turn that happened to
|
|
8
|
+
* contain an ASCII word recalled a crowd. The corpus here is written the way
|
|
9
|
+
* real memories are: Chinese titles, Chinese bodies, occasional English terms.
|
|
10
|
+
*
|
|
11
|
+
* @module dsh-cross-memory/test/retrieval.test
|
|
12
|
+
*/
|
|
13
|
+
import assert from 'node:assert/strict';
|
|
14
|
+
import test from 'node:test';
|
|
15
|
+
import { tokenize, termWeight, relevanceOf, relevanceOfOne, searchMemories, FIELD_WEIGHTS, TERM_WEIGHTS } from '../search.js';
|
|
16
|
+
import { isSubstantiveTurn, queryMessages } from '../query.js';
|
|
17
|
+
/** Build one corpus entry. */
|
|
18
|
+
function entry(id, title, body, extra = {}, kind = 'fact') {
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
scope: 'global',
|
|
22
|
+
kind,
|
|
23
|
+
title,
|
|
24
|
+
body,
|
|
25
|
+
tags: extra.tags ?? [],
|
|
26
|
+
keys: extra.keys ?? [],
|
|
27
|
+
createdAt: 0,
|
|
28
|
+
updatedAt: 0,
|
|
29
|
+
uses: 0,
|
|
30
|
+
lastUsedAt: 0,
|
|
31
|
+
lastSurfacedAt: 0,
|
|
32
|
+
source: 'auto',
|
|
33
|
+
...extra.appliesTo === undefined ? {} : { appliesTo: extra.appliesTo },
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/** The corpus the queries below are ranked against. */
|
|
37
|
+
const CORPUS = [
|
|
38
|
+
entry('prefer-pnpm', 'Prefer pnpm workspaces', 'The repository is one pnpm workspace; do not run npm install.', { tags: ['tooling'], keys: ['monorepo'] }),
|
|
39
|
+
entry('plugin-logs', '插件日志查看方式', '插件的日志写在 dsh-cross-memory.log 里,超过 2MB 轮转一代。'),
|
|
40
|
+
entry('plugin-order', '插件注册顺序', '插件在启动时注册,日志级别由配置决定。'),
|
|
41
|
+
entry('restart-web', '重启 dsh web', '用 dshmarket 的 restart 端点重启。', { tags: ['dsh'], keys: ['dshmarket'] }),
|
|
42
|
+
entry('push-after-commit', '提交后主动推送', '本项目的约定是提交后主动推送,不要留在本地。', { appliesTo: '准备推送代码之前' }),
|
|
43
|
+
entry('commit-f', 'pwsh 下多行提交消息要用 -F 文件', '在 PowerShell 里用 git commit -F 读文件,heredoc 和 -m 都会出错。'),
|
|
44
|
+
entry('always-origin', '始终使用 origin 远端', '推送时远端固定为 origin,不要推到别的名字。', { appliesTo: '推送代码之前' }),
|
|
45
|
+
];
|
|
46
|
+
/** The corpus in the shape the search functions take. */
|
|
47
|
+
const GROUPS = [{ scope: 'global', label: 'global', entries: CORPUS }];
|
|
48
|
+
/** Ids of the top `limit` hits for one query. */
|
|
49
|
+
function rank(query, limit = 3) {
|
|
50
|
+
return searchMemories(GROUPS, query, { limit }).map((hit) => hit.entry.id);
|
|
51
|
+
}
|
|
52
|
+
test('tokenize keeps ASCII words whole and expands CJK into bigrams', () => {
|
|
53
|
+
assert.deepEqual(tokenize('git commit -F'), ['git', 'commit', 'f']);
|
|
54
|
+
// The whole point: a space-free Chinese run must produce more than itself.
|
|
55
|
+
const terms = tokenize('该插件是否有日志');
|
|
56
|
+
assert.ok(terms.includes('插件'), 'a bigram is reachable');
|
|
57
|
+
assert.ok(terms.includes('日志'), 'including the one that carries the meaning');
|
|
58
|
+
assert.ok(terms.length > 3, 'the run expands rather than staying indivisible');
|
|
59
|
+
assert.deepEqual(tokenize('日志'), ['日志'], 'a two-character run is already its own bigram');
|
|
60
|
+
assert.deepEqual(tokenize('看'), ['看'], 'a lone ideograph contributes itself');
|
|
61
|
+
});
|
|
62
|
+
test('a CJK bigram is a weaker claim than a Latin word', () => {
|
|
63
|
+
assert.equal(termWeight('logging'), 1);
|
|
64
|
+
assert.ok(termWeight('日志') < 1, 'a pair of characters can appear in unrelated text');
|
|
65
|
+
});
|
|
66
|
+
test('queryMessages splits a turn, keeps the whole, and is deterministic', () => {
|
|
67
|
+
const turn = '先看日志。然后把插件重启,再确认一下。';
|
|
68
|
+
const messages = queryMessages(turn);
|
|
69
|
+
assert.equal(messages[0], turn, 'the whole turn is still a candidate');
|
|
70
|
+
assert.ok(messages.some((message) => message.includes('先看日志')), 'sentences are scored on their own');
|
|
71
|
+
assert.ok(messages.some((message) => message.includes('把插件重启')), 'and so are clauses');
|
|
72
|
+
assert.deepEqual(queryMessages(turn), messages, 'the same turn always yields the same list');
|
|
73
|
+
assert.equal(new Set(messages).size, messages.length, 'no duplicates');
|
|
74
|
+
assert.deepEqual(queryMessages(' '), [], 'whitespace carries no query');
|
|
75
|
+
});
|
|
76
|
+
test('queryMessages bounds how much of a long turn reaches the scorer', () => {
|
|
77
|
+
const long = Array.from({ length: 60 }, (_, index) => `第${index}句关于打包与部署的说明文字。`).join('');
|
|
78
|
+
const messages = queryMessages(long);
|
|
79
|
+
assert.ok(messages.length <= 1 + 12, 'a long turn cannot fan out without bound');
|
|
80
|
+
assert.ok(messages.every((message) => message.length <= 240), 'no candidate is unbounded');
|
|
81
|
+
});
|
|
82
|
+
test('a Latin query still finds its memory', () => {
|
|
83
|
+
assert.equal(rank('pnpm')[0], 'prefer-pnpm');
|
|
84
|
+
assert.equal(rank('monorepo')[0], 'prefer-pnpm', 'a key is a first-class way in');
|
|
85
|
+
assert.equal(rank('dshmarket')[0], 'restart-web');
|
|
86
|
+
});
|
|
87
|
+
test('a space-free Chinese turn finds the memory it shares a phrase with', () => {
|
|
88
|
+
assert.equal(rank('这个插件的日志在哪里?')[0], 'plugin-logs');
|
|
89
|
+
assert.equal(rank('插件日志')[0], 'plugin-logs');
|
|
90
|
+
});
|
|
91
|
+
test('sharing common characters is not the same as sharing a phrase', () => {
|
|
92
|
+
const byPhrase = relevanceOf(CORPUS.find((item) => item.id === 'plugin-logs'), '这个插件的日志在哪里?');
|
|
93
|
+
const byPair = relevanceOf(CORPUS.find((item) => item.id === 'plugin-order'), '这个插件的日志在哪里?');
|
|
94
|
+
assert.ok(byPhrase > byPair, 'the memory sharing 插件日 outranks the one sharing only 插件');
|
|
95
|
+
});
|
|
96
|
+
test('generic character overlap does not outrank a real match', () => {
|
|
97
|
+
const hits = rank('该插件是否有日志', 7);
|
|
98
|
+
assert.ok(hits.includes('plugin-logs') || hits.includes('plugin-order'), 'the plugin memories rank above an unrelated one');
|
|
99
|
+
// The commit memory shares nothing with the question and must not appear.
|
|
100
|
+
assert.ok(!hits.includes('commit-f'));
|
|
101
|
+
});
|
|
102
|
+
test('appliesTo contributes, and is why a paraphrased turn still finds the memory', () => {
|
|
103
|
+
const push = CORPUS.find((item) => item.id === 'push-after-commit');
|
|
104
|
+
// `exactOptionalPropertyTypes` is on, so the field is removed by destructuring
|
|
105
|
+
// rather than by assigning `undefined` back to it.
|
|
106
|
+
const { appliesTo: _dropped, ...withoutAppliesTo } = push;
|
|
107
|
+
const withField = relevanceOf(push, '准备推送代码之前');
|
|
108
|
+
const withoutField = relevanceOf(withoutAppliesTo, '准备推送代码之前');
|
|
109
|
+
assert.ok(withField > withoutField, 'the appliesTo phrase adds score');
|
|
110
|
+
assert.ok(withoutField > 0, 'the body alone still matches, just less strongly');
|
|
111
|
+
});
|
|
112
|
+
test('the same phrase is worth more in the title than in the body', () => {
|
|
113
|
+
const phrase = 'prefer pnpm workspaces';
|
|
114
|
+
const inTitle = entry('in-title', phrase, 'A note.');
|
|
115
|
+
const inBody = entry('in-body', 'Package manager notes', `${phrase} over npm.`);
|
|
116
|
+
assert.ok(relevanceOfOne(inTitle, phrase) > relevanceOfOne(inBody, phrase), 'the title is the claim; the body is supporting text');
|
|
117
|
+
for (const field of ['title', 'keys', 'tags', 'appliesTo', 'body']) {
|
|
118
|
+
assert.ok(FIELD_WEIGHTS[field] > TERM_WEIGHTS[field], `${field}: the phrase outweighs a term`);
|
|
119
|
+
}
|
|
120
|
+
// A shorter query that matches the whole title still outranks a longer query
|
|
121
|
+
// that only matches its parts — the reason the field weight is per field.
|
|
122
|
+
assert.ok(relevanceOfOne(inTitle, 'prefer pnpm workspaces') > 0);
|
|
123
|
+
});
|
|
124
|
+
test('ranking is a total order: score, then recency, then title', () => {
|
|
125
|
+
const older = entry('older', 'Same title', 'body');
|
|
126
|
+
const newer = entry('newer', 'Same title', 'body');
|
|
127
|
+
const groups = [{ scope: 'global', label: 'global', entries: [{ ...older, updatedAt: 1 }, { ...newer, updatedAt: 2 }] }];
|
|
128
|
+
const hits = searchMemories(groups, 'same title', { limit: 5 });
|
|
129
|
+
assert.deepEqual(hits.map((hit) => hit.entry.id), ['newer', 'older']);
|
|
130
|
+
});
|
|
131
|
+
test('search filters and browse still behave', () => {
|
|
132
|
+
assert.equal(searchMemories(GROUPS, 'pnpm', { tags: ['nope'] }).length, 0);
|
|
133
|
+
assert.equal(searchMemories(GROUPS, '').length, 0);
|
|
134
|
+
assert.deepEqual(searchMemories(GROUPS, 'pnpm', { kinds: ['fact'] }).map((hit) => hit.entry.id), ['prefer-pnpm']);
|
|
135
|
+
assert.equal(searchMemories(GROUPS, 'pnpm', { kinds: ['failure'] }).length, 0);
|
|
136
|
+
});
|
|
137
|
+
test('a turn with no letters, digits, or ideographs is not worth a scan', () => {
|
|
138
|
+
assert.equal(isSubstantiveTurn('好'), true);
|
|
139
|
+
assert.equal(isSubstantiveTurn('ok'), true);
|
|
140
|
+
assert.equal(isSubstantiveTurn(' ... '), false);
|
|
141
|
+
assert.equal(isSubstantiveTurn(''), false);
|
|
142
|
+
});
|