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,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the notice queue: the rules that keep a proactive assistant from
|
|
3
|
+
* becoming a nag.
|
|
4
|
+
*
|
|
5
|
+
* @module dsh-cross-memory/test/notices.test
|
|
6
|
+
*/
|
|
7
|
+
import assert from 'node:assert/strict';
|
|
8
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
9
|
+
import { tmpdir } from 'node:os';
|
|
10
|
+
import { join } from 'node:path';
|
|
11
|
+
import test from 'node:test';
|
|
12
|
+
import { ackNotice, admitNotice, dueNotices, pruneNotices, quietHoursEnd, readNotices, withNotice, writeNotices, NOTICE_TTL_MS } from '../notices.js';
|
|
13
|
+
import { gateNotices, parseGateReply } from '../gate.js';
|
|
14
|
+
const NOW = Date.parse('2026-10-23T12:00:00Z');
|
|
15
|
+
const policy = { now: NOW, quietHours: '', dailyCap: 3, minSpacingHours: 6 };
|
|
16
|
+
const CANDIDATE = {
|
|
17
|
+
kind: 'digest',
|
|
18
|
+
createdAt: NOW,
|
|
19
|
+
dueAt: NOW,
|
|
20
|
+
title: '昨天我整理了 21 条记忆',
|
|
21
|
+
body: '新增 5、退役 9',
|
|
22
|
+
evidence: ['digest'],
|
|
23
|
+
dedupeKey: 'digest:daily',
|
|
24
|
+
};
|
|
25
|
+
test('a notice without evidence is refused', () => {
|
|
26
|
+
const verdict = admitNotice({ version: 1, notices: [] }, { ...CANDIDATE, evidence: [] }, policy);
|
|
27
|
+
assert.equal(verdict.ok, false);
|
|
28
|
+
assert.match(verdict.ok === false ? verdict.reason : '', /evidence/u);
|
|
29
|
+
});
|
|
30
|
+
test('the same subject is never repeated inside the spacing window', () => {
|
|
31
|
+
const first = admitNotice({ version: 1, notices: [] }, CANDIDATE, policy);
|
|
32
|
+
assert.equal(first.ok, true);
|
|
33
|
+
if (!first.ok)
|
|
34
|
+
return;
|
|
35
|
+
const queue = { version: 1, notices: [first.notice] };
|
|
36
|
+
const at1h = { ...policy, now: NOW + 3_600_000 };
|
|
37
|
+
const again = admitNotice(queue, { ...CANDIDATE, createdAt: NOW + 3_600_000 }, at1h);
|
|
38
|
+
assert.equal(again.ok, false, 'an hour later is still too soon');
|
|
39
|
+
const later = admitNotice(queue, { ...CANDIDATE, createdAt: NOW + 7 * 3_600_000 }, { ...policy, now: NOW + 7 * 3_600_000 });
|
|
40
|
+
assert.equal(later.ok, true, 'after the window it may speak again');
|
|
41
|
+
});
|
|
42
|
+
test('a dismissal doubles the silence', () => {
|
|
43
|
+
const first = admitNotice({ version: 1, notices: [] }, CANDIDATE, policy);
|
|
44
|
+
if (!first.ok)
|
|
45
|
+
return;
|
|
46
|
+
const dismissed = { ...first.notice, state: 'acked', outcome: 'dismissed', ackedAt: NOW + 1000 };
|
|
47
|
+
const queue = { version: 1, notices: [dismissed] };
|
|
48
|
+
const at7h = admitNotice(queue, { ...CANDIDATE, createdAt: NOW + 7 * 3_600_000 }, { ...policy, now: NOW + 7 * 3_600_000 });
|
|
49
|
+
assert.equal(at7h.ok, false, 'dismissed subjects wait twice as long');
|
|
50
|
+
const at13h = admitNotice(queue, { ...CANDIDATE, createdAt: NOW + 13 * 3_600_000 }, { ...policy, now: NOW + 13 * 3_600_000 });
|
|
51
|
+
assert.equal(at13h.ok, true);
|
|
52
|
+
});
|
|
53
|
+
test('the daily cap is a guard rail against a runaway trigger', () => {
|
|
54
|
+
const notices = [1, 2, 3].map((index) => ({
|
|
55
|
+
...CANDIDATE,
|
|
56
|
+
id: 'n' + index,
|
|
57
|
+
dedupeKey: 'other:' + index,
|
|
58
|
+
state: 'delivered',
|
|
59
|
+
}));
|
|
60
|
+
const verdict = admitNotice({ version: 1, notices }, { ...CANDIDATE, dedupeKey: 'fresh' }, policy);
|
|
61
|
+
assert.equal(verdict.ok, false);
|
|
62
|
+
assert.match(verdict.ok === false ? verdict.reason : '', /guard rail/u);
|
|
63
|
+
});
|
|
64
|
+
test('quiet hours defer a notice instead of dropping it', () => {
|
|
65
|
+
// Local-time constructors: quiet hours use the host clock, so an absolute UTC
|
|
66
|
+
// timestamp would only test the machine's timezone (and fail on UTC CI).
|
|
67
|
+
const at22 = new Date(2026, 9, 23, 22, 0, 0).getTime();
|
|
68
|
+
const due = quietHoursEnd(at22, '22:00-08:00');
|
|
69
|
+
assert.ok(due > at22, 'held until the window ends');
|
|
70
|
+
assert.equal(quietHoursEnd(at22, ''), at22, 'no window means no delay');
|
|
71
|
+
const daytime = new Date(2026, 9, 23, 10, 0, 0).getTime();
|
|
72
|
+
assert.equal(quietHoursEnd(daytime, '22:00-08:00'), daytime, 'outside the window it fires now');
|
|
73
|
+
});
|
|
74
|
+
test('the queue round-trips, delivers once, and expires', async (t) => {
|
|
75
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-notices-'));
|
|
76
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
77
|
+
const admitted = admitNotice({ version: 1, notices: [] }, CANDIDATE, policy);
|
|
78
|
+
assert.equal(admitted.ok, true);
|
|
79
|
+
if (!admitted.ok)
|
|
80
|
+
return;
|
|
81
|
+
await writeNotices(dir, { version: 1, notices: [admitted.notice] });
|
|
82
|
+
const loaded = await readNotices(dir);
|
|
83
|
+
assert.equal(loaded.notices.length, 1);
|
|
84
|
+
assert.deepEqual(dueNotices(loaded, NOW + 1000).map((notice) => notice.id), [admitted.notice.id]);
|
|
85
|
+
const delivered = withNotice(loaded, { ...admitted.notice, state: 'delivered', deliveredAt: NOW });
|
|
86
|
+
assert.deepEqual(dueNotices(delivered, NOW + 1000), [], 'a delivered notice is not due again');
|
|
87
|
+
const stale = pruneNotices({ version: 1, notices: [admitted.notice] }, NOW + NOTICE_TTL_MS + 1);
|
|
88
|
+
assert.equal(stale.notices[0]?.state, 'expired', 'nothing sits in the queue forever');
|
|
89
|
+
});
|
|
90
|
+
test('the gate only authorises ids it was shown, and fails to silence', async () => {
|
|
91
|
+
const candidates = [
|
|
92
|
+
{ id: 'loop-a', title: '还挂着:把报销单交给财务', detail: '说了 4 天' },
|
|
93
|
+
{ id: 'loop-b', title: '还挂着:看看部署脚本', detail: '说了 9 天' },
|
|
94
|
+
];
|
|
95
|
+
const parsed = parseGateReply('{"deliver":["loop-a","loop-made-up"],"reason":"a 有硬期限"}', candidates);
|
|
96
|
+
assert.equal(parsed.failed, false);
|
|
97
|
+
assert.deepEqual([...parsed.deliver], ['loop-a'], 'an invented id is not permission');
|
|
98
|
+
assert.equal(parsed.reason, 'a 有硬期限');
|
|
99
|
+
assert.equal(parseGateReply('not json at all', candidates).failed, true, 'garbage is not permission');
|
|
100
|
+
const fenced = parseGateReply('```json\n{"deliver":[],"reason":"都可以等"}\n```', candidates);
|
|
101
|
+
assert.equal(fenced.deliver.size, 0);
|
|
102
|
+
assert.equal(fenced.reason, '都可以等');
|
|
103
|
+
const noLlm = await gateNotices({ llm: undefined, session: undefined, candidates, timeoutMs: 1000, signal: new AbortController().signal });
|
|
104
|
+
assert.equal(noLlm.deliver.size, 0);
|
|
105
|
+
assert.equal(noLlm.skipped, 'no-llm');
|
|
106
|
+
const nothing = await gateNotices({ llm: undefined, session: undefined, candidates: [], timeoutMs: 1000, signal: new AbortController().signal });
|
|
107
|
+
assert.equal(nothing.deliver.size, 0);
|
|
108
|
+
});
|
|
109
|
+
test('answering a notice records the outcome, and only once', () => {
|
|
110
|
+
const first = admitNotice({ version: 1, notices: [] }, CANDIDATE, policy);
|
|
111
|
+
assert.equal(first.ok, true);
|
|
112
|
+
if (!first.ok)
|
|
113
|
+
return;
|
|
114
|
+
const queue = { version: 1, notices: [{ ...first.notice, state: 'delivered', deliveredAt: NOW }] };
|
|
115
|
+
const answered = ackNotice(queue, first.notice.id.slice(0, 12), 'dismissed', NOW + 1000);
|
|
116
|
+
assert.equal(answered.notice?.id, first.notice.id, 'a unique prefix is enough to answer');
|
|
117
|
+
const stored = answered.queue.notices[0];
|
|
118
|
+
assert.equal(stored?.state, 'acked');
|
|
119
|
+
assert.equal(stored?.outcome, 'dismissed');
|
|
120
|
+
assert.equal(stored?.ackedAt, NOW + 1000);
|
|
121
|
+
const again = ackNotice(answered.queue, first.notice.id, 'acted', NOW + 2000);
|
|
122
|
+
assert.equal(again.notice, undefined, 'an answered notice is not answered twice');
|
|
123
|
+
assert.match(again.reason ?? '', /no unacknowledged notice/u);
|
|
124
|
+
});
|
|
125
|
+
test('an ambiguous or unknown prefix is refused instead of guessing', () => {
|
|
126
|
+
const one = admitNotice({ version: 1, notices: [] }, CANDIDATE, policy);
|
|
127
|
+
const two = admitNotice({ version: 1, notices: [] }, { ...CANDIDATE, dedupeKey: 'other' }, policy);
|
|
128
|
+
if (!one.ok || !two.ok)
|
|
129
|
+
return;
|
|
130
|
+
const queue = {
|
|
131
|
+
version: 1,
|
|
132
|
+
notices: [
|
|
133
|
+
{ ...one.notice, state: 'delivered', deliveredAt: NOW },
|
|
134
|
+
{ ...two.notice, state: 'delivered', deliveredAt: NOW },
|
|
135
|
+
],
|
|
136
|
+
};
|
|
137
|
+
const ambiguous = ackNotice(queue, 'digest-', 'acted', NOW);
|
|
138
|
+
assert.equal(ambiguous.notice, undefined);
|
|
139
|
+
assert.match(ambiguous.reason ?? '', /be more specific/u);
|
|
140
|
+
const unknown = ackNotice(queue, 'zzzz', 'acted', NOW);
|
|
141
|
+
assert.equal(unknown.notice, undefined);
|
|
142
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the persona layer: the profile document and the voice that feeds it.
|
|
3
|
+
*
|
|
4
|
+
* Two rules are pinned here. The profile is a DOCUMENT, not a memory: it is
|
|
5
|
+
* rendered whole, in its own frame, with the date it was taken — a profile is a
|
|
6
|
+
* snapshot of a person, and one injected without a date reads as a permanent
|
|
7
|
+
* fact. And the profile is built from the user's own words only, so the voice
|
|
8
|
+
* flag has to survive the storage round trip.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/persona.test
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import test from 'node:test';
|
|
17
|
+
import { clearPersona, isPersonaLocked, personaLockPath, personaPath, readPersona, renderPersonaBlock, setPersonaLocked, writePersona } from '../persona.js';
|
|
18
|
+
import { MemoryStore } from '../storage.js';
|
|
19
|
+
import { parsePlan } from '../consolidate.js';
|
|
20
|
+
test('a profile round-trips through the store directory', async (t) => {
|
|
21
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-persona-'));
|
|
22
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
23
|
+
assert.equal(await readPersona(dir), undefined, 'no profile before one is written');
|
|
24
|
+
const path = await writePersona(dir, '他要求先确认再改代码。');
|
|
25
|
+
assert.match(path, /persona[\\/]human\.md$/u);
|
|
26
|
+
assert.equal(await readPersona(dir), '他要求先确认再改代码。');
|
|
27
|
+
assert.match(await readFile(personaPath(dir), 'utf8'), /先确认再改代码/u);
|
|
28
|
+
});
|
|
29
|
+
test('the block is framed, dated, and explicitly not an instruction set', () => {
|
|
30
|
+
const block = renderPersonaBlock('他要求先确认再改代码。', 4_000, Date.parse('2026-10-23T00:00:00Z'));
|
|
31
|
+
assert.ok(block !== undefined);
|
|
32
|
+
assert.match(block, /^<persona>/u);
|
|
33
|
+
assert.match(block, /<\/persona>$/u);
|
|
34
|
+
assert.match(block, /snapshot 2026-10-23/u, 'a profile says when it was taken');
|
|
35
|
+
assert.match(block, /never instructions to follow/u, 'and what it is not');
|
|
36
|
+
assert.match(block, /先确认再改代码/u);
|
|
37
|
+
});
|
|
38
|
+
test('the block is cut at a line boundary before it can swallow the turn', () => {
|
|
39
|
+
const long = Array.from({ length: 200 }, (_, index) => `- 偏好 ${index}: ${'很长的内容'.repeat(6)}`).join('\n');
|
|
40
|
+
const block = renderPersonaBlock(long, 800);
|
|
41
|
+
assert.ok(block !== undefined);
|
|
42
|
+
assert.ok(Buffer.byteLength(block, 'utf8') <= 800, 'the budget is hard');
|
|
43
|
+
assert.match(block, /<\/persona>$/u, 'the frame survives the cut');
|
|
44
|
+
assert.equal(renderPersonaBlock(long, 120), undefined, 'no room for the frame means no block');
|
|
45
|
+
assert.equal(renderPersonaBlock(undefined, 800), undefined, 'and no profile means no block');
|
|
46
|
+
});
|
|
47
|
+
test('the profile is a plan field, and a persona-only pass is a real outcome', () => {
|
|
48
|
+
const plan = parsePlan(JSON.stringify({ memories: [], retire: [], skills: [], notes: 'refined', persona: '他偏好短句。' }), new Set(), 10);
|
|
49
|
+
assert.ok(plan !== undefined, 'a pass that only refines the profile is not dropped');
|
|
50
|
+
assert.equal(plan.persona, '他偏好短句。');
|
|
51
|
+
assert.equal(parsePlan(JSON.stringify({ memories: [], retire: [], skills: [], notes: '' }), new Set(), 10), undefined);
|
|
52
|
+
});
|
|
53
|
+
test('the voice flag survives the storage round trip', async (t) => {
|
|
54
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-voice-'));
|
|
55
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
56
|
+
const store = new MemoryStore(dir);
|
|
57
|
+
await store.upsert({ scope: 'global', title: 'Said by the user', body: 'prefers short answers', tags: [], voice: 'user' }, undefined, 'auto', 1);
|
|
58
|
+
await store.upsert({ scope: 'global', title: 'Concluded by the agent', body: 'the build is slow', tags: [] }, undefined, 'auto', 2);
|
|
59
|
+
const listed = await store.list('global', undefined, { fresh: true });
|
|
60
|
+
const byId = new Map(listed.map((entry) => [entry.id, entry]));
|
|
61
|
+
assert.equal(byId.get('said-by-the-user')?.voice, 'user');
|
|
62
|
+
assert.equal(byId.get('concluded-by-the-agent')?.voice, undefined, 'the default is stored as absent');
|
|
63
|
+
// A rewrite that says nothing about voice keeps it, the way pinned does.
|
|
64
|
+
await store.upsert({ scope: 'global', title: 'Said by the user', body: 'prefers short answers, always', tags: [] }, undefined, 'auto', 3);
|
|
65
|
+
const reworded = (await store.list('global', undefined, { fresh: true })).find((entry) => entry.id === 'said-by-the-user');
|
|
66
|
+
assert.equal(reworded?.voice, 'user');
|
|
67
|
+
});
|
|
68
|
+
test('a legacy single-file profile is still read as the human block', async (t) => {
|
|
69
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-legacy-'));
|
|
70
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
71
|
+
await writeFile(join(dir, 'persona.md'), '旧的单文件画像\n', 'utf8');
|
|
72
|
+
assert.equal(await readPersona(dir), '旧的单文件画像', 'upgrading does not lose the profile');
|
|
73
|
+
});
|
|
74
|
+
test('the manual-edit lock is a timestamped marker, and clearing removes the profile', async (t) => {
|
|
75
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-lock-'));
|
|
76
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
77
|
+
assert.equal(await isPersonaLocked(dir), false, 'an untouched store is unlocked');
|
|
78
|
+
await writePersona(dir, '他先确认再改代码。');
|
|
79
|
+
await setPersonaLocked(dir, true);
|
|
80
|
+
assert.equal(await isPersonaLocked(dir), true, 'the marker locks the profile');
|
|
81
|
+
assert.match(await readFile(personaLockPath(dir), 'utf8'), /\d{4}-\d{2}-\d{2}T/u, 'the marker records when it was locked');
|
|
82
|
+
await clearPersona(dir);
|
|
83
|
+
assert.equal(await readPersona(dir), undefined, 'clearing removes the profile the injector reads');
|
|
84
|
+
await setPersonaLocked(dir, false);
|
|
85
|
+
assert.equal(await isPersonaLocked(dir), false, 'unlocking removes the marker');
|
|
86
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The prefilter must never lose a memory the full scan would have recalled.
|
|
3
|
+
*
|
|
4
|
+
* That is the whole contract: the index may make recall cheaper, it may return a
|
|
5
|
+
* superset of candidates, but the set of entries that pass `matchCredit` has to
|
|
6
|
+
* be identical to what an unfiltered scan finds. The property is checked directly
|
|
7
|
+
* here rather than inferred from the eval probes, because the eval only covers
|
|
8
|
+
* ten hand-picked queries and this is a correctness boundary.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/prefilter
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
import test from 'node:test';
|
|
14
|
+
import { matchCredit, prefilterEntries, termIndex } from '../search.js';
|
|
15
|
+
const base = {
|
|
16
|
+
id: 'x',
|
|
17
|
+
scope: 'global',
|
|
18
|
+
kind: 'fact',
|
|
19
|
+
tags: [],
|
|
20
|
+
keys: [],
|
|
21
|
+
uses: 0,
|
|
22
|
+
createdAt: 0,
|
|
23
|
+
updatedAt: 0,
|
|
24
|
+
lastUsedAt: 0,
|
|
25
|
+
lastSurfacedAt: 0,
|
|
26
|
+
source: 'auto',
|
|
27
|
+
};
|
|
28
|
+
const entries = [
|
|
29
|
+
{ ...base, id: 'a', title: '合并任务需要会话 idle 才会跑', body: '抽取收尾触发 consolidateIfDue。', appliesTo: '排查合并不跑的时候' },
|
|
30
|
+
{ ...base, id: 'b', title: '桌宠气泡需要 surface 参数', body: '不传 surface 会回 bad-request。', tags: ['dsh-pet'] },
|
|
31
|
+
{ ...base, id: 'c', title: 'Python 项目一律用 uv', body: '不用 pyenv 或 conda。', tags: ['python'] },
|
|
32
|
+
{ ...base, id: 'd', title: '重启 DSH 必须 quit 进程', body: '关窗口不算重启,pgrep 确认。', keys: ['restart'] },
|
|
33
|
+
{ ...base, id: 'e', title: '花枝招展杯看板发布流程', body: '构建后推到 gh-pages。', tags: ['hzb-cup'] },
|
|
34
|
+
{ ...base, id: 'f', title: '日志轮转脚本', body: 'logging 目录按天切分。', tags: ['logging'] },
|
|
35
|
+
{ ...base, id: 'g', title: '邮箱签名用顺颂商祺', body: '对外邮件结尾。', tags: ['mail'] },
|
|
36
|
+
{ ...base, id: 'h', title: '内存数据库用 WAL', body: 'state.db 开启 WAL 模式。', tags: ['sqlite'] },
|
|
37
|
+
{ ...base, id: 'i', title: '合并策略:保留更新的那条', body: '冲突时看 updatedAt。', tags: ['merge'] },
|
|
38
|
+
{ ...base, id: 'j', title: '桌宠配置在 main-config.jsonc', body: 'whisperPrompt 是替换语义。', tags: ['dsh-pet'] },
|
|
39
|
+
{ ...base, id: 'k', title: 'uv 安装后不必再建 venv', body: 'uv sync 即可。', tags: ['python', 'uv'] },
|
|
40
|
+
{ ...base, id: 'l', title: 'pgrep 用来确认进程真的退了', body: 'osascript quit 之后检查。', tags: ['mac'] },
|
|
41
|
+
{ ...base, id: 'm', title: 'bad-request 要看 message', body: 'surface 必填藏在失败分支。', tags: ['dsh-pet'] },
|
|
42
|
+
];
|
|
43
|
+
test('the index is a superset of every entry the full scan accepts', () => {
|
|
44
|
+
const queries = [
|
|
45
|
+
'重启后会话为什么没加载',
|
|
46
|
+
'桌宠气泡不回话 bad-request',
|
|
47
|
+
'python 环境用 uv 还是 pyenv',
|
|
48
|
+
'合并任务不跑',
|
|
49
|
+
'logging 按天切分',
|
|
50
|
+
'顺颂商祺 邮件结尾',
|
|
51
|
+
'WAL 模式',
|
|
52
|
+
'单字:退',
|
|
53
|
+
];
|
|
54
|
+
for (const query of queries) {
|
|
55
|
+
const accepted = entries.filter((entry) => matchCredit(entry, query, 9, 2).about);
|
|
56
|
+
const candidates = new Set(prefilterEntries(entries, query).map((entry) => entry.id));
|
|
57
|
+
for (const entry of accepted) {
|
|
58
|
+
assert.ok(candidates.has(entry.id), `prefilter lost "${entry.id}" for query "${query}"`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
test('a narrow candidate set falls back to the full scan instead of guessing', () => {
|
|
63
|
+
// Two entries share one rare bigram; the floor is higher than that, so the
|
|
64
|
+
// prefilter must decline to narrow at all.
|
|
65
|
+
const narrowed = prefilterEntries(entries, '顺颂商祺');
|
|
66
|
+
assert.equal(narrowed.length, entries.length, 'below the floor the full list is returned');
|
|
67
|
+
});
|
|
68
|
+
test('the index maps terms to every entry that contains them', () => {
|
|
69
|
+
const index = termIndex(entries);
|
|
70
|
+
const python = index.get('python') ?? [];
|
|
71
|
+
const ids = python.map((at) => entries[at]?.id);
|
|
72
|
+
assert.deepEqual(ids.sort(), ['c', 'k']);
|
|
73
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The consolidation child's composition.
|
|
3
|
+
*
|
|
4
|
+
* Since 0.2.1-rc.2 a subagent takes its tool set from the Agent Preset it
|
|
5
|
+
* names, and a child that names none inherits the parent's composition — the
|
|
6
|
+
* parent's tools, which is the one thing the consolidation pass must never
|
|
7
|
+
* have. This test pins the two halves of that contract: the plugin registers a
|
|
8
|
+
* preset of its own, and that preset carries no tool rows at all.
|
|
9
|
+
*
|
|
10
|
+
* @module dsh-cross-memory/test/preset.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 { Context } from '@deepseek-ai/cordis';
|
|
18
|
+
import * as plugin from '../index.js';
|
|
19
|
+
import { CONSOLIDATE_PRESET_ID } from '../consolidate.js';
|
|
20
|
+
import { MEMORIES_SETTINGS_DEFAULTS } from '../config.js';
|
|
21
|
+
/**
|
|
22
|
+
* Remove a test's temp directory.
|
|
23
|
+
*
|
|
24
|
+
* The plugin holds its SQLite state store open until its fiber is disposed,
|
|
25
|
+
* and Windows refuses to unlink an open file (`EBUSY`) where POSIX does not —
|
|
26
|
+
* so the fibers are disposed first, and the removal retries anyway.
|
|
27
|
+
*
|
|
28
|
+
* @param ctx - the context the plugin was mounted under.
|
|
29
|
+
* @param dir - the directory to remove.
|
|
30
|
+
*/
|
|
31
|
+
async function removeDir(ctx, dir) {
|
|
32
|
+
for (const fiber of [...(ctx.registry.get(plugin)?.fibers ?? [])])
|
|
33
|
+
await fiber.dispose();
|
|
34
|
+
await rm(dir, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
|
|
35
|
+
}
|
|
36
|
+
test('the plugin registers a tool-free preset for the consolidation child', async () => {
|
|
37
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-preset-'));
|
|
38
|
+
const root = new Context();
|
|
39
|
+
const ctx = root.extend({ name: 'preset-test' });
|
|
40
|
+
const registered = [];
|
|
41
|
+
ctx.provide('tools', { register: () => () => undefined, schemas: () => [] });
|
|
42
|
+
ctx.provide('commands', { register: () => () => undefined });
|
|
43
|
+
ctx.provide('llm', { stream: async function* () { } });
|
|
44
|
+
ctx.provide('agentPresets', {
|
|
45
|
+
register: async (definition) => {
|
|
46
|
+
registered.push(definition);
|
|
47
|
+
return async () => { registered.splice(registered.indexOf(definition), 1); };
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
const config = { memoriesDir: dir, logFile: '', ...MEMORIES_SETTINGS_DEFAULTS, autoExtract: false };
|
|
51
|
+
ctx.plugin(plugin, config);
|
|
52
|
+
// The preset is registered through an injection, so it lands a tick after the
|
|
53
|
+
// plugin is mounted.
|
|
54
|
+
await new Promise((settle) => setTimeout(settle, 50));
|
|
55
|
+
assert.equal(registered.length, 1, 'exactly one preset is registered');
|
|
56
|
+
assert.equal(registered[0]?.id, CONSOLIDATE_PRESET_ID);
|
|
57
|
+
// No plugin rows at all: the child gets no tools, so it can only answer.
|
|
58
|
+
assert.deepEqual(registered[0]?.plugins, [], 'the preset must carry no tool rows');
|
|
59
|
+
assert.ok((registered[0]?.name ?? '').length > 0, 'the preset names itself for the settings surface');
|
|
60
|
+
await removeDir(ctx, dir);
|
|
61
|
+
});
|
|
62
|
+
test('a deployment without a preset registry still loads the plugin', async () => {
|
|
63
|
+
// The preset registry is optional: a headless deployment composes no
|
|
64
|
+
// browser plane and no roster, and the plugin must keep every other surface
|
|
65
|
+
// and simply run its consolidation pass as a plain model call.
|
|
66
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-preset-'));
|
|
67
|
+
const root = new Context();
|
|
68
|
+
const ctx = root.extend({ name: 'preset-test-no-registry' });
|
|
69
|
+
ctx.provide('tools', { register: () => () => undefined, schemas: () => [] });
|
|
70
|
+
ctx.provide('commands', { register: () => () => undefined });
|
|
71
|
+
const config = { memoriesDir: dir, logFile: '', ...MEMORIES_SETTINGS_DEFAULTS, autoExtract: false };
|
|
72
|
+
ctx.plugin(plugin, config);
|
|
73
|
+
await new Promise((settle) => setTimeout(settle, 50));
|
|
74
|
+
const fibers = [...(ctx.registry.get(plugin)?.fibers ?? [])];
|
|
75
|
+
assert.equal(fibers.length, 1, 'the plugin is mounted');
|
|
76
|
+
await removeDir(ctx, dir);
|
|
77
|
+
});
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for raw-turn recall.
|
|
3
|
+
*
|
|
4
|
+
* The excerpt selector is the whole feature: it decides which original turn is
|
|
5
|
+
* worth showing next to a memory, must scrub credentials, and must stay inside
|
|
6
|
+
* the byte budget. Reading the transcript from the persisted logs is tested with
|
|
7
|
+
* a synthetic zstd log, the same shape the backfill tests use.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-cross-memory/test/raw-recall.test
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
|
|
13
|
+
import { tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import test from 'node:test';
|
|
16
|
+
import { zstdCompressSync } from 'node:zlib';
|
|
17
|
+
import { normalizeSettings } from '../config.js';
|
|
18
|
+
import { renderRecall } from '../render.js';
|
|
19
|
+
import { collectExcerpts, readTranscript, selectExcerpts, windowAroundQuote } from '../raw-recall.js';
|
|
20
|
+
import { MemoriesRuntime } from '../index.js';
|
|
21
|
+
import { zstdAvailable } from '../backfill.js';
|
|
22
|
+
/** One message, as the transcript reader yields it. */
|
|
23
|
+
function message(seq, role, text) {
|
|
24
|
+
return { seq, role, text };
|
|
25
|
+
}
|
|
26
|
+
/** One JSONL log line. */
|
|
27
|
+
function line(event) {
|
|
28
|
+
return `${JSON.stringify(event)}\n`;
|
|
29
|
+
}
|
|
30
|
+
/** A synthetic session log with prose, an injected block and a tool call. */
|
|
31
|
+
function sampleLog(sessionId) {
|
|
32
|
+
return [
|
|
33
|
+
line({ type: 'session', seq: 1, id: sessionId, cwd: '/tmp/work', delegationDepth: 0 }),
|
|
34
|
+
line({ type: 'user/message', seq: 2, data: { role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: '部署脚本的日志路径是 /var/log/deploy/app.log,出问题先看这个文件。' }] } }),
|
|
35
|
+
line({ type: 'user/message', seq: 3, data: { role: 'user', source: { kind: 'plugin:memories' }, content: [{ type: 'text', text: '<memory-context>injected</memory-context>' }] } }),
|
|
36
|
+
line({ type: 'assistant/message', seq: 4, data: { message: { role: 'assistant', content: [{ type: 'text', text: '收到,部署失败时优先检查 /var/log/deploy/app.log。' }] } } }),
|
|
37
|
+
line({ type: 'tool/call', seq: 5, data: { name: 'bash', arguments: '{}' } }),
|
|
38
|
+
].join('');
|
|
39
|
+
}
|
|
40
|
+
test('raw recall is off by default and bounded by its own knobs', () => {
|
|
41
|
+
const settings = normalizeSettings({});
|
|
42
|
+
assert.equal(settings.rawRecall, false, 'injecting prose from other sessions is opt-in');
|
|
43
|
+
assert.equal(settings.rawRecallBytes, 2_400);
|
|
44
|
+
assert.equal(settings.rawRecallExcerpts, 3);
|
|
45
|
+
const tuned = normalizeSettings({ rawRecall: true, rawRecallBytes: -10, rawRecallExcerpts: 0 });
|
|
46
|
+
assert.equal(tuned.rawRecall, true);
|
|
47
|
+
assert.equal(tuned.rawRecallBytes, 0);
|
|
48
|
+
assert.equal(tuned.rawRecallExcerpts, 0);
|
|
49
|
+
});
|
|
50
|
+
test('selectExcerpts keeps the turn that shares real evidence and drops generic overlap', () => {
|
|
51
|
+
const messages = [
|
|
52
|
+
message(1, 'user', '今天天气不错,我们随便聊聊。'),
|
|
53
|
+
message(2, 'user', '部署脚本的日志在 /var/log/deploy/app.log,部署失败先看它。'),
|
|
54
|
+
message(3, 'assistant', '好的,部署失败先查 /var/log/deploy/app.log。'),
|
|
55
|
+
message(4, 'user', '另外那个前端页面颜色不太对。'),
|
|
56
|
+
];
|
|
57
|
+
const excerpts = selectExcerpts(messages, '部署失败了应该看哪个日志?');
|
|
58
|
+
assert.ok(excerpts.length >= 1);
|
|
59
|
+
assert.ok(excerpts.some((excerpt) => excerpt.text.includes('/var/log/deploy/app.log')));
|
|
60
|
+
assert.ok(excerpts.every((excerpt) => !excerpt.text.includes('天气')));
|
|
61
|
+
});
|
|
62
|
+
test('selectExcerpts scrubs credentials and respects the count/character caps', () => {
|
|
63
|
+
const messages = [
|
|
64
|
+
message(1, 'user', '部署脚本里 api_key = "supersecretvalue" 别提交,日志看 /var/log/deploy/app.log。'),
|
|
65
|
+
message(2, 'user', '部署日志路径 /var/log/deploy/app.log 出问题就看。'),
|
|
66
|
+
message(3, 'user', '部署失败时也看 /var/log/deploy/worker.log。'),
|
|
67
|
+
];
|
|
68
|
+
const excerpts = selectExcerpts(messages, '部署日志在哪里,api_key 怎么处理?', { maxExcerpts: 2, maxChars: 60 });
|
|
69
|
+
assert.ok(excerpts.length <= 2);
|
|
70
|
+
assert.ok(excerpts.every((excerpt) => excerpt.text.length <= 60));
|
|
71
|
+
assert.ok(excerpts.every((excerpt) => !excerpt.text.includes('supersecretvalue')));
|
|
72
|
+
assert.ok(excerpts.every((excerpt) => !excerpt.text.includes('[redacted]') || !excerpt.text.includes('sk-')));
|
|
73
|
+
});
|
|
74
|
+
test('selectExcerpts returns nothing for a query with no usable tokens', () => {
|
|
75
|
+
assert.deepEqual(selectExcerpts([message(1, 'user', '部署日志在 /var/log/deploy/app.log')], '嗯'), []);
|
|
76
|
+
assert.deepEqual(selectExcerpts([], '部署日志在哪里'), []);
|
|
77
|
+
});
|
|
78
|
+
test('windowAroundQuote reproduces the sentence a memory was written from', () => {
|
|
79
|
+
const filler = '无关的背景说明。'.repeat(200);
|
|
80
|
+
const quote = '部署脚本的日志路径是 /var/log/deploy/app.log,出问题先看这个文件。';
|
|
81
|
+
const messages = [
|
|
82
|
+
message(1, 'user', `${filler}${quote}${filler}`),
|
|
83
|
+
message(2, 'assistant', '收到。'),
|
|
84
|
+
];
|
|
85
|
+
const excerpt = windowAroundQuote(messages, quote, 600);
|
|
86
|
+
assert.ok(excerpt !== undefined);
|
|
87
|
+
assert.ok(excerpt.text.includes('var/log/deploy/app.log'), 'the quote is inside the window, not cut off');
|
|
88
|
+
assert.ok(excerpt.text.length <= 600);
|
|
89
|
+
assert.equal(windowAroundQuote(messages, '这段原文已经不在会话里了', 600), undefined);
|
|
90
|
+
});
|
|
91
|
+
test('readTranscript finds the session log by directory name and returns prose only', async (t) => {
|
|
92
|
+
if (!zstdAvailable())
|
|
93
|
+
return;
|
|
94
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-raw-'));
|
|
95
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
96
|
+
const sessionId = 'session-raw-1';
|
|
97
|
+
const logDir = join(dir, sessionId);
|
|
98
|
+
await mkdir(logDir, { recursive: true });
|
|
99
|
+
await writeFile(join(logDir, 'session.v4.jsonl.zstd'), zstdCompressSync(Buffer.from(sampleLog(sessionId), 'utf8')));
|
|
100
|
+
const messages = await readTranscript(dir, sessionId);
|
|
101
|
+
assert.ok(messages !== undefined);
|
|
102
|
+
assert.deepEqual(messages.map((item) => item.text), [
|
|
103
|
+
'部署脚本的日志路径是 /var/log/deploy/app.log,出问题先看这个文件。',
|
|
104
|
+
'收到,部署失败时优先检查 /var/log/deploy/app.log。',
|
|
105
|
+
]);
|
|
106
|
+
assert.equal(await readTranscript(dir, 'session-missing'), undefined);
|
|
107
|
+
});
|
|
108
|
+
test('collectExcerpts reads at most two source sessions and tags each excerpt', async (t) => {
|
|
109
|
+
if (!zstdAvailable())
|
|
110
|
+
return;
|
|
111
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-raw-'));
|
|
112
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
113
|
+
for (const sessionId of ['session-a', 'session-b']) {
|
|
114
|
+
const logDir = join(dir, sessionId);
|
|
115
|
+
await mkdir(logDir, { recursive: true });
|
|
116
|
+
await writeFile(join(logDir, 'session.v4.jsonl.zstd'), zstdCompressSync(Buffer.from(sampleLog(sessionId), 'utf8')));
|
|
117
|
+
}
|
|
118
|
+
const excerpts = await collectExcerpts(dir, [
|
|
119
|
+
{ sessionId: 'session-a' },
|
|
120
|
+
{ sessionId: 'session-b' },
|
|
121
|
+
{ sessionId: 'session-missing' },
|
|
122
|
+
], '部署日志在哪里', { maxExcerpts: 2 });
|
|
123
|
+
assert.ok(excerpts.length >= 1);
|
|
124
|
+
assert.ok(excerpts.every((excerpt) => excerpt.sessionId === 'session-a' || excerpt.sessionId === 'session-b'));
|
|
125
|
+
});
|
|
126
|
+
test('renderRecall puts raw excerpts inside the frame and keeps the byte budget', () => {
|
|
127
|
+
const text = renderRecall([], 2_000, [], [
|
|
128
|
+
{ sessionId: 'session-a', seq: 2, role: 'user', text: '部署脚本的日志在 /var/log/deploy/app.log。' },
|
|
129
|
+
]);
|
|
130
|
+
assert.ok(text !== undefined);
|
|
131
|
+
assert.match(text, /<memory-recall>/u);
|
|
132
|
+
assert.match(text, /Original conversation excerpts/u);
|
|
133
|
+
assert.match(text, /session-a/u);
|
|
134
|
+
assert.ok(Buffer.byteLength(text, 'utf8') <= 2_000);
|
|
135
|
+
// A block with only excerpts is still a block.
|
|
136
|
+
assert.equal(renderRecall([], 100, [], []), undefined);
|
|
137
|
+
});
|
|
138
|
+
/** A minimal context the runtime can start under. */
|
|
139
|
+
const stubContext = { get: () => undefined, logger: { info: () => undefined, warn: () => undefined, debug: () => undefined, error: () => undefined } };
|
|
140
|
+
/** A session with one user turn, which is what the recall path reads. */
|
|
141
|
+
function recallSession(userText) {
|
|
142
|
+
return {
|
|
143
|
+
id: 'session-raw-int',
|
|
144
|
+
header: { version: 0, id: 'session-raw-int', createdAt: 0, isSeeded: false },
|
|
145
|
+
seq: 1,
|
|
146
|
+
requestHeader: () => undefined,
|
|
147
|
+
deriveMessages: () => [{ role: 'user', source: { kind: 'user' }, content: [{ type: 'text', text: userText }] }],
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
function recallAgent(session) {
|
|
151
|
+
return { id: session.id, session, status: 'idle' };
|
|
152
|
+
}
|
|
153
|
+
test('an on-demand recall carries original excerpts when rawRecall is on', async (t) => {
|
|
154
|
+
if (!zstdAvailable())
|
|
155
|
+
return;
|
|
156
|
+
const home = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-raw-home-'));
|
|
157
|
+
t.after(() => rm(home, { recursive: true, force: true }));
|
|
158
|
+
const memoriesDir = join(home, 'memories');
|
|
159
|
+
const sessionId = 'session-raw-int';
|
|
160
|
+
const logDir = join(home, 'sessions', sessionId);
|
|
161
|
+
await mkdir(logDir, { recursive: true });
|
|
162
|
+
await writeFile(join(logDir, 'session.v4.jsonl.zstd'), zstdCompressSync(Buffer.from(sampleLog(sessionId), 'utf8')));
|
|
163
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir, dshHome: home, autoExtract: false, rawRecall: true });
|
|
164
|
+
t.after(() => runtime.dispose());
|
|
165
|
+
const session = recallSession('部署失败了应该看哪个日志?');
|
|
166
|
+
await runtime.write(session, {
|
|
167
|
+
scope: 'global',
|
|
168
|
+
title: '部署日志路径',
|
|
169
|
+
body: '部署失败时先看 /var/log/deploy/app.log。',
|
|
170
|
+
tags: ['部署'],
|
|
171
|
+
sourceSession: sessionId,
|
|
172
|
+
// The stored quote is what the excerpt is grounded on; it must come from the
|
|
173
|
+
// same source session.
|
|
174
|
+
quote: '部署脚本的日志路径是 /var/log/deploy/app.log,出问题先看这个文件。',
|
|
175
|
+
evidenceSeq: 2,
|
|
176
|
+
}, 'tool');
|
|
177
|
+
const message = await runtime.recallFor(recallAgent(session));
|
|
178
|
+
assert.ok(message !== undefined, 'the memory matches, so a recall block is emitted');
|
|
179
|
+
const text = message.content
|
|
180
|
+
.filter((block) => block.type === 'text').map((block) => block.text ?? '').join('');
|
|
181
|
+
assert.match(text, /<memory-recall>/u);
|
|
182
|
+
assert.match(text, /Original conversation excerpts/u, 'the raw half rides with the memory');
|
|
183
|
+
assert.match(text, /session-raw-int/u, 'each excerpt names its source session');
|
|
184
|
+
assert.match(text, /var\/log\/deploy\/app\.log/u);
|
|
185
|
+
assert.match(text, /出问题先看这个文件/u, 'the excerpt is the exact source sentence');
|
|
186
|
+
});
|