dsh-experience 0.0.0-stage → 1.0.4
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/CHANGELOG.md +225 -0
- package/CONTRIBUTING.md +87 -0
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/USER-GUIDE.md +92 -0
- package/config.example.json +13 -0
- package/cordis.patch.yml +4 -0
- package/index.js +1207 -0
- package/lib/capture.js +663 -0
- package/lib/decay.js +253 -0
- package/lib/evidence.js +206 -0
- package/lib/lookup.js +53 -0
- package/package.json +60 -4
- package/store.example.json +10 -0
- package/tools/e2e.mjs +342 -0
- package/tools/selftest.mjs +925 -0
package/tools/e2e.mjs
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
// End-to-end harness: drives index.js through a fake Cordis context.
|
|
2
|
+
// Everything runs against a throwaway DSH_HOME so the real library is untouched.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
8
|
+
|
|
9
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const src = path.resolve(here, '..');
|
|
11
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'dsh-exp-e2e-'));
|
|
12
|
+
process.env.DSH_HOME = home;
|
|
13
|
+
|
|
14
|
+
const results = [];
|
|
15
|
+
function test(name, fn) {
|
|
16
|
+
return Promise.resolve()
|
|
17
|
+
.then(fn)
|
|
18
|
+
.then(() => results.push({ name, ok: true }))
|
|
19
|
+
.catch((error) => results.push({ name, ok: false, error: String(error && error.message) }));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const mod = await import(pathToFileURL(path.join(src, 'index.js')).href);
|
|
23
|
+
|
|
24
|
+
const handlers = new Map();
|
|
25
|
+
const tools = new Map();
|
|
26
|
+
const commands = new Map();
|
|
27
|
+
const effects = [];
|
|
28
|
+
const agents = [];
|
|
29
|
+
|
|
30
|
+
function on(event, fn) {
|
|
31
|
+
if (!handlers.has(event)) handlers.set(event, []);
|
|
32
|
+
handlers.get(event).push(fn);
|
|
33
|
+
return () => {};
|
|
34
|
+
}
|
|
35
|
+
async function fire(event, ...args) {
|
|
36
|
+
for (const fn of handlers.get(event) || []) await fn(...args);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function trackContext(list) {
|
|
40
|
+
return (def) => {
|
|
41
|
+
list.push(def);
|
|
42
|
+
return () => {
|
|
43
|
+
const index = list.indexOf(def);
|
|
44
|
+
if (index >= 0) list.splice(index, 1);
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const rootContexts = [];
|
|
50
|
+
const ctx = {
|
|
51
|
+
effect(fn) {
|
|
52
|
+
const dispose = fn();
|
|
53
|
+
effects.push(dispose);
|
|
54
|
+
return dispose;
|
|
55
|
+
},
|
|
56
|
+
on,
|
|
57
|
+
tools: { register(def) { tools.set(def.name, def); return () => tools.delete(def.name); } },
|
|
58
|
+
commands: { register(def) { commands.set(def.name, def); return () => commands.delete(def.name); } },
|
|
59
|
+
agents: { list: () => agents, currentInitiator: () => agents[0] || null },
|
|
60
|
+
systemPrompt: { context: trackContext(rootContexts) },
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
function makeAgent(id) {
|
|
64
|
+
const scoped = [];
|
|
65
|
+
const agent = {
|
|
66
|
+
id,
|
|
67
|
+
sessionId: id,
|
|
68
|
+
session: { id, cwd: 'D:\\work\\acme-app' },
|
|
69
|
+
ctx: { systemPrompt: { context: trackContext(scoped) } },
|
|
70
|
+
scoped,
|
|
71
|
+
};
|
|
72
|
+
agents.push(agent);
|
|
73
|
+
return agent;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// A real profile has live agents before the plugin loads.
|
|
77
|
+
const A = makeAgent('session-e2e');
|
|
78
|
+
let B;
|
|
79
|
+
let otherAppId = null;
|
|
80
|
+
mod.apply(ctx);
|
|
81
|
+
|
|
82
|
+
const defsOf = (agent) =>
|
|
83
|
+
[...rootContexts, ...agent.scoped].filter((def) => def.name === 'experience-recall-v2');
|
|
84
|
+
const lastDef = (agent) => defsOf(agent).at(-1);
|
|
85
|
+
|
|
86
|
+
let callSeq = 0;
|
|
87
|
+
function call(agent, name, args, { ok = true, error = '' } = {}) {
|
|
88
|
+
callSeq += 1;
|
|
89
|
+
return fire('tools/result', { callId: `c${callSeq}`, name, agent, arguments: args }, {
|
|
90
|
+
isError: !ok,
|
|
91
|
+
value: ok ? 'done' : error,
|
|
92
|
+
content: [{ type: 'text', text: ok ? 'done' : error }],
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async function enter(agent, turn, stepNo, text) {
|
|
96
|
+
return fire(
|
|
97
|
+
'agent/pre-step',
|
|
98
|
+
{ agent, turn, step: stepNo, messages: [{ role: 'user', content: text }], modelSelection: {} },
|
|
99
|
+
async () => ({ kind: 'enter', messages: [] }),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
function cmd(line, agent = A) {
|
|
103
|
+
return commands.get('memory').handler({
|
|
104
|
+
commandId: 'c1',
|
|
105
|
+
agent,
|
|
106
|
+
rawInput: line,
|
|
107
|
+
attachments: [],
|
|
108
|
+
signal: undefined,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const storePath = () => path.join(home, 'experience', 'store.json');
|
|
112
|
+
const readStore = () => JSON.parse(fs.readFileSync(storePath(), 'utf8'));
|
|
113
|
+
const readStoreConfig = () => JSON.parse(fs.readFileSync(path.join(home, 'experience', 'config.json'), 'utf8'));
|
|
114
|
+
|
|
115
|
+
await test('T40 apply registers five tools, the command and a scoped context', () => {
|
|
116
|
+
for (const name of ['experience_record', 'experience_lookup', 'experience_forget', 'experience_debug', 'experience_review']) {
|
|
117
|
+
assert.ok(tools.has(name), `missing tool ${name}`);
|
|
118
|
+
}
|
|
119
|
+
assert.ok(commands.has('memory'));
|
|
120
|
+
assert.equal(defsOf(A).length, 1, 'live agent bound at apply time');
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
await test('T41 cold start recalls nothing and explains why', async () => {
|
|
124
|
+
await enter(A, 1, 1, '新增 pad 函数据用例');
|
|
125
|
+
assert.equal(lastDef(A).text(), '', 'empty library must inject nothing');
|
|
126
|
+
const out = await tools.get('experience_debug').execute({}, { agent: A });
|
|
127
|
+
assert.match(out, /\[1 存储\]/);
|
|
128
|
+
assert.match(out, /档案为空|experiences: 0/);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
await test('T42 a real recipe is captured as a candidate', async () => {
|
|
132
|
+
await enter(A, 2, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
133
|
+
await call(A, 'read', { file_path: 'D:\\work\\acme-app\\package.json' });
|
|
134
|
+
await call(A, 'write', { file_path: 'D:\\work\\acme-app\\src\\pad.js' });
|
|
135
|
+
await call(A, 'write', { file_path: 'D:\\work\\acme-app\\test\\pad.test.mjs' });
|
|
136
|
+
await call(A, 'pwsh', { command: 'node test\\pad.test.mjs' });
|
|
137
|
+
await fire('agent/turn-stopping', { agent: A, turn: 2, signal: undefined });
|
|
138
|
+
const store = readStore();
|
|
139
|
+
assert.equal(store.experiences.length, 1, 'one candidate captured');
|
|
140
|
+
const exp = store.experiences[0];
|
|
141
|
+
assert.equal(exp.status, 'candidate');
|
|
142
|
+
assert.match(exp.title, /新增 pad/, `title should name the artifact, got: ${exp.title}`);
|
|
143
|
+
assert.ok(exp.steps.length >= 2 && exp.steps.length <= 8);
|
|
144
|
+
assert.ok(!exp.steps.some((step) => /Get-Content|Get-ChildItem|package\.json/.test(step)), 'exploration leaked in');
|
|
145
|
+
if (process.env.E2E_VERBOSE) console.log('T42 fp:', JSON.stringify(exp.fingerprint), 'scope:', JSON.stringify(exp.scope));
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
await test('T43 scaffolding-only turn is rejected by G7', async () => {
|
|
149
|
+
await enter(A, 3, 1, '看看目录里有什么');
|
|
150
|
+
await call(A, 'pwsh', { command: '$roots = @("D:\\work\\acme-app")' });
|
|
151
|
+
await call(A, 'pwsh', { command: 'Get-ChildItem -Force' });
|
|
152
|
+
await call(A, 'pwsh', { command: 'Test-Path D:\\work\\acme-app' });
|
|
153
|
+
await fire('agent/turn-stopping', { agent: A, turn: 3, signal: undefined });
|
|
154
|
+
const store = readStore();
|
|
155
|
+
assert.equal(store.experiences.length, 1, 'no new candidate');
|
|
156
|
+
const turn = store.turns.at(-1);
|
|
157
|
+
assert.match(String(turn.capture && turn.capture.reason), /G7/, `expected G7, got ${JSON.stringify(turn.capture)}`);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
await test('T44 approve then recall injects a rendered block', async () => {
|
|
161
|
+
const approve = await cmd('approve exp-1');
|
|
162
|
+
assert.equal(approve.kind, 'success', approve.text);
|
|
163
|
+
await enter(A, 4, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
164
|
+
const text = lastDef(A).text();
|
|
165
|
+
assert.match(text, /\[AUTO-RECALL/);
|
|
166
|
+
assert.match(text, /exp-1/);
|
|
167
|
+
const nonceMatch = /nonce=(exp-[0-9a-f]{6})/.exec(text);
|
|
168
|
+
assert.ok(nonceMatch, 'nonce present');
|
|
169
|
+
const store = readStore();
|
|
170
|
+
const inj = store.injections.at(-1);
|
|
171
|
+
assert.equal(inj.rendered, true, 'text() must mark the injection rendered');
|
|
172
|
+
assert.equal(inj.nonce, nonceMatch[1]);
|
|
173
|
+
const debug = await tools.get('experience_debug').execute({}, { agent: A });
|
|
174
|
+
assert.match(debug, /\[4 注入\] PASS/);
|
|
175
|
+
assert.match(debug, new RegExp(nonceMatch[1]));
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
await test('T45 lookup reports the ids it could not find', async () => {
|
|
179
|
+
const out = await tools.get('experience_lookup').execute({ ids: ['exp-1', 'exp-999'] }, { agent: A });
|
|
180
|
+
assert.match(out, /exp-1/);
|
|
181
|
+
assert.match(out, /未找到:exp-999/);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
await test('T46 decay report is read-only and restore refuses unknown ids', async () => {
|
|
185
|
+
const before = readStore().experiences.length;
|
|
186
|
+
const report = await cmd('decay --age 400');
|
|
187
|
+
assert.equal(report.kind, 'success', report.text);
|
|
188
|
+
assert.equal(readStore().experiences.length, before, 'report must not delete anything');
|
|
189
|
+
assert.equal(readStore().archive, undefined, 'nothing moved to archive');
|
|
190
|
+
const restore = await cmd('restore exp-1');
|
|
191
|
+
assert.equal(restore.kind, 'error');
|
|
192
|
+
assert.match(restore.text, /archive 里没有/);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
await test('T47 teach pins the record so decay cannot fade it', async () => {
|
|
196
|
+
const out = await cmd('teach 发布前跑全量单测 || 跑 node tools/selftest.mjs; 看 pass N / fail 0');
|
|
197
|
+
assert.equal(out.kind, 'success', out.text);
|
|
198
|
+
const taught = readStore().experiences.find((e) => e.source && e.source.origin === 'human');
|
|
199
|
+
assert.ok(taught, 'taught record stored');
|
|
200
|
+
assert.equal(taught.status, 'verified');
|
|
201
|
+
const report = await cmd('decay --age 9999');
|
|
202
|
+
assert.match(report.text, /pinned/i);
|
|
203
|
+
assert.doesNotMatch(report.text, new RegExp(`${taught.id}[^\\n]*可归档`));
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
await test('T48 agent/created binds a late agent and agent/disposed releases it', async () => {
|
|
207
|
+
B = makeAgent('session-e2e-2');
|
|
208
|
+
await fire('agent/created', { agent: B, source: 'test', signal: undefined });
|
|
209
|
+
assert.equal(defsOf(B).length, 1, 'late agent bound');
|
|
210
|
+
await fire('agent/disposed', { agent: B });
|
|
211
|
+
assert.equal(defsOf(B).length, 0, 'dispose removes the scoped context');
|
|
212
|
+
await fire('agent/created', { agent: B, source: 'test', signal: undefined });
|
|
213
|
+
assert.equal(defsOf(B).length, 1, 're-created agent binds again');
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
await test('T49 repeat recipe merges and thickens reverse_examples', async () => {
|
|
217
|
+
await enter(A, 5, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
218
|
+
await call(A, 'write', { file_path: 'D:\\work\\acme-app\\src\\pad.js' });
|
|
219
|
+
await call(A, 'pwsh', { command: 'node test\\pad.test.mjs' }, { ok: false, error: 'TypeError: pad is not a function' });
|
|
220
|
+
await call(A, 'pwsh', { command: 'node test\\pad.test.mjs' });
|
|
221
|
+
await fire('agent/turn-stopping', { agent: A, turn: 5, signal: undefined });
|
|
222
|
+
const store = readStore();
|
|
223
|
+
const exp = store.experiences.find((item) => item.id === 'exp-1');
|
|
224
|
+
assert.equal(store.experiences.filter((item) => (item.scope || []).includes('acme-app')).length, 1, 'one record per project');
|
|
225
|
+
assert.equal(store.turns.at(-1).capture.merged, 'exp-1', 'fingerprint merged into exp-1');
|
|
226
|
+
assert.ok(exp.occurrences >= 2, `occurrences ${exp.occurrences}`);
|
|
227
|
+
assert.ok((exp.reverse_examples || []).length >= 1, 'new failure recorded as a reverse example');
|
|
228
|
+
assert.match((exp.reverse_examples || []).join(' '), /pad is not a function/);
|
|
229
|
+
assert.equal(exp.status, 'verified', 'a merge must not reset the human approval');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
await test('T50 same recipe in a different directory starts a new candidate', async () => {
|
|
233
|
+
await enter(A, 6, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
234
|
+
await call(A, 'write', { file_path: 'D:\\work\\other-app\\src\\pad.js' });
|
|
235
|
+
await call(A, 'pwsh', { command: 'node test\\pad.test.mjs' });
|
|
236
|
+
await fire('agent/turn-stopping', { agent: A, turn: 6, signal: undefined });
|
|
237
|
+
const store = readStore();
|
|
238
|
+
const fresh = store.experiences.filter((item) => (item.scope || []).includes('other-app'));
|
|
239
|
+
assert.equal(fresh.length, 1, 'scope gate keeps the records apart');
|
|
240
|
+
assert.equal(fresh[0].status, 'candidate');
|
|
241
|
+
assert.match(String(store.turns.at(-1).capture.reason), /scope|按新任务/);
|
|
242
|
+
otherAppId = fresh[0].id;
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
await test('T51 unrelated query injects nothing even when verified records exist', async () => {
|
|
246
|
+
await enter(A, 7, 1, '帮我写一首关于秋天的诗');
|
|
247
|
+
assert.equal(lastDef(A).text(), '', 'no injection below threshold');
|
|
248
|
+
const debug = await tools.get('experience_debug').execute({}, { agent: A });
|
|
249
|
+
assert.match(debug, /\[3 检索\] FAIL/);
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
await test('T52 experience_forget refuses verified records and deletes candidates', async () => {
|
|
253
|
+
const tool = tools.get('experience_forget');
|
|
254
|
+
const refused = await tool.execute({ id: 'exp-1' }, { agent: A });
|
|
255
|
+
assert.match(refused, /verified|拒绝|人工/);
|
|
256
|
+
const before = readStore().experiences.length;
|
|
257
|
+
const removed = await tool.execute({ id: otherAppId }, { agent: A });
|
|
258
|
+
assert.match(removed, new RegExp(otherAppId));
|
|
259
|
+
assert.equal(readStore().experiences.length, before - 1);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await test('T53 experience_review is read-only and lists what needs a human', async () => {
|
|
263
|
+
const out = await tools.get('experience_review').execute({}, { agent: A });
|
|
264
|
+
assert.doesNotMatch(out, /已升级|approved/);
|
|
265
|
+
assert.match(out, /人工|approve|批准/);
|
|
266
|
+
assert.equal(readStore().experiences[0].status, 'verified');
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
await test('T54 config set coerces types and rejects unknown keys', async () => {
|
|
270
|
+
const set = await cmd('config set minEffectiveSteps 3');
|
|
271
|
+
assert.equal(set.kind, 'success', set.text);
|
|
272
|
+
assert.equal(readStoreConfig().minEffectiveSteps, 3);
|
|
273
|
+
const bad = await cmd('config set captureBudgetMs twenty');
|
|
274
|
+
assert.equal(bad.kind, 'error');
|
|
275
|
+
assert.match(bad.text, /captureBudgetMs/);
|
|
276
|
+
const unknown = await cmd('config set nope 1');
|
|
277
|
+
assert.equal(unknown.kind, 'error');
|
|
278
|
+
assert.match(unknown.text, /nope/);
|
|
279
|
+
await cmd('config set minEffectiveSteps 2');
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
await test('T55 two agents keep separate traces and injections', async () => {
|
|
283
|
+
await enter(B, 8, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
284
|
+
await enter(A, 8, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
285
|
+
const before = readStore().injections.length;
|
|
286
|
+
const textB = lastDef(B).text();
|
|
287
|
+
const textA = lastDef(A).text();
|
|
288
|
+
assert.match(textA, /AUTO-RECALL/);
|
|
289
|
+
assert.match(textB, /AUTO-RECALL/);
|
|
290
|
+
assert.notEqual(textA, textB, 'nonces must differ per injection');
|
|
291
|
+
assert.equal(readStore().injections.length, before, 'later recall must not leak into another turn');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
await test('T56 the durable store never keeps an absolute path', () => {
|
|
295
|
+
const raw = fs.readFileSync(storePath(), 'utf8');
|
|
296
|
+
assert.doesNotMatch(raw, /[A-Za-z]:\\work/i, 'raw drive path leaked');
|
|
297
|
+
assert.doesNotMatch(raw, /acme-app\\src/i, 'raw project path leaked');
|
|
298
|
+
const turn = readStore().turns.at(-1);
|
|
299
|
+
assert.ok(turn.calls.every((call) => !/[A-Za-z]:\\work/i.test(call.hint)), 'trace hint leaked a path');
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
await test('T57 a new capture explains itself in one line', async () => {
|
|
303
|
+
const capture = readStore().turns.find((turn) => turn.capture && turn.capture.id)?.capture;
|
|
304
|
+
assert.ok(capture, 'expected at least one created capture');
|
|
305
|
+
assert.match(String(capture.reason), /^新建 exp-\d+(scope .+;指纹 .+)/);
|
|
306
|
+
});
|
|
307
|
+
await test('T58 superseded is never recalled and never re-captured', async () => {
|
|
308
|
+
const sup = await cmd('supersede exp-1 与 AGENTS.md 冲突:沙箱禁用 node --test');
|
|
309
|
+
assert.equal(sup.kind, 'success', sup.text);
|
|
310
|
+
const rec = readStore().experiences.find((e) => e.id === 'exp-1');
|
|
311
|
+
assert.equal(rec.status, 'superseded');
|
|
312
|
+
assert.match(rec.supersede.rule, /AGENTS\.md/);
|
|
313
|
+
|
|
314
|
+
await enter(A, 20, 1, '在 src 里新增 pad 函数并配 node:test 用例');
|
|
315
|
+
assert.equal(lastDef(A).text(), '', 'superseded must not be injected');
|
|
316
|
+
|
|
317
|
+
await call(A, 'write', { file_path: 'D:\\work\\acme-app\\src\\pad.js' });
|
|
318
|
+
await call(A, 'write', { file_path: 'D:\\work\\acme-app\\test\\pad.test.mjs' });
|
|
319
|
+
await call(A, 'pwsh', { command: 'node test\\pad.test.mjs' });
|
|
320
|
+
await fire('agent/turn-stopping', { agent: A, turn: 20, signal: undefined });
|
|
321
|
+
const turn = readStore().turns.at(-1);
|
|
322
|
+
assert.match(String(turn.capture && turn.capture.reason), /G8/, `expected G8, got ${JSON.stringify(turn.capture)}`);
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
console.log('');
|
|
326
|
+
let pass = 0;
|
|
327
|
+
results.forEach((item, index) => {
|
|
328
|
+
if (item.ok) {
|
|
329
|
+
pass += 1;
|
|
330
|
+
console.log(`ok ${40 + index} - ${item.name}`);
|
|
331
|
+
} else {
|
|
332
|
+
console.log(`not ok ${40 + index} - ${item.name}\n ${item.error}`);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
console.log(`\nE2E: ${pass} pass / ${results.length - pass} fail (of ${results.length})`);
|
|
336
|
+
if (pass !== results.length) {
|
|
337
|
+
console.log('store dump:', JSON.stringify(readStore().experiences.map((e) => ({
|
|
338
|
+
id: e.id, status: e.status, scope: e.scope, fp: e.fingerprint, occ: e.occurrences,
|
|
339
|
+
})), null, 1));
|
|
340
|
+
}
|
|
341
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
342
|
+
process.exit(pass === results.length ? 0 : 1);
|