dsh-cross-memory 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +247 -2
- package/cordis.patch.yml +27 -0
- package/docs/DETAIL.md +13 -0
- package/docs/DETAIL.zh.md +17 -0
- package/icon.svg +1 -0
- package/lib/backfill.js +273 -0
- package/lib/citations.js +305 -0
- package/lib/client.js +1550 -0
- package/lib/config.js +736 -0
- package/lib/consolidate.js +1119 -0
- package/lib/extract.js +570 -0
- package/lib/gate.js +137 -0
- package/lib/git-origin.js +224 -0
- package/lib/index.js +4475 -0
- package/lib/log.js +260 -0
- package/lib/loops.js +186 -0
- package/lib/notices.js +195 -0
- package/lib/notify.js +33 -0
- package/lib/persona.js +112 -0
- package/lib/query.js +93 -0
- package/lib/raw-recall.js +286 -0
- package/lib/reflection.js +291 -0
- package/lib/remote.js +455 -0
- package/lib/render.js +678 -0
- package/lib/retention.js +64 -0
- package/lib/schedule.js +245 -0
- package/lib/search.js +1068 -0
- package/lib/skills.js +220 -0
- package/lib/state.js +701 -0
- package/lib/storage.js +1424 -0
- package/lib/sync-runtime.js +268 -0
- package/lib/sync.js +620 -0
- package/lib/test/activity.test.js +74 -0
- package/lib/test/archive.test.js +182 -0
- package/lib/test/backfill.test.js +144 -0
- package/lib/test/citations.test.js +91 -0
- package/lib/test/client-wiring.test.js +134 -0
- package/lib/test/consolidate-job.test.js +95 -0
- package/lib/test/consolidate.test.js +430 -0
- package/lib/test/consolidation-selection.test.js +152 -0
- package/lib/test/dedupe-keys.test.js +230 -0
- package/lib/test/delta.test.js +93 -0
- package/lib/test/derived.test.js +110 -0
- package/lib/test/disputes-panel.test.js +314 -0
- package/lib/test/eval.test.js +171 -0
- package/lib/test/expand.test.js +164 -0
- package/lib/test/extract.test.js +1020 -0
- package/lib/test/git-origin.test.js +189 -0
- package/lib/test/injection.test.js +440 -0
- package/lib/test/kind-filter.test.js +49 -0
- package/lib/test/legacy-state.test.js +69 -0
- package/lib/test/log.test.js +156 -0
- package/lib/test/loops.test.js +86 -0
- package/lib/test/memory.test.js +855 -0
- package/lib/test/notices.test.js +140 -0
- package/lib/test/persona.test.js +73 -0
- package/lib/test/prefilter.test.js +73 -0
- package/lib/test/preset.test.js +77 -0
- package/lib/test/raw-recall.test.js +186 -0
- package/lib/test/recall.test.js +433 -0
- package/lib/test/reflection.test.js +64 -0
- package/lib/test/remote.test.js +227 -0
- package/lib/test/retention.test.js +117 -0
- package/lib/test/retrieval.test.js +142 -0
- package/lib/test/rotation.test.js +228 -0
- package/lib/test/schedule.test.js +112 -0
- package/lib/test/settings-card.test.js +58 -0
- package/lib/test/skills.test.js +129 -0
- package/lib/test/state.test.js +212 -0
- package/lib/test/subagent-contract.test.js +139 -0
- package/lib/test/sweep.test.js +159 -0
- package/lib/test/sync.test.js +314 -0
- package/lib/test/time.test.js +171 -0
- package/lib/time.js +303 -0
- package/lib/tool.js +330 -0
- package/lib/types/backfill.d.ts +96 -0
- package/lib/types/citations.d.ts +105 -0
- package/lib/types/config.d.ts +811 -0
- package/lib/types/consolidate.d.ts +742 -0
- package/lib/types/extract.d.ts +352 -0
- package/lib/types/gate.d.ts +83 -0
- package/lib/types/git-origin.d.ts +23 -0
- package/lib/types/index.d.ts +1316 -0
- package/lib/types/log.d.ts +144 -0
- package/lib/types/loops.d.ts +82 -0
- package/lib/types/notices.d.ts +117 -0
- package/lib/types/notify.d.ts +12 -0
- package/lib/types/persona.d.ts +43 -0
- package/lib/types/query.d.ts +45 -0
- package/lib/types/raw-recall.d.ts +84 -0
- package/lib/types/reflection.d.ts +122 -0
- package/lib/types/remote.d.ts +233 -0
- package/lib/types/render.d.ts +286 -0
- package/lib/types/retention.d.ts +66 -0
- package/lib/types/schedule.d.ts +116 -0
- package/lib/types/search.d.ts +583 -0
- package/lib/types/skills.d.ts +71 -0
- package/lib/types/state.d.ts +294 -0
- package/lib/types/storage.d.ts +458 -0
- package/lib/types/sync-runtime.d.ts +103 -0
- package/lib/types/sync.d.ts +222 -0
- package/lib/types/test/activity.test.d.ts +1 -0
- package/lib/types/test/archive.test.d.ts +1 -0
- package/lib/types/test/backfill.test.d.ts +1 -0
- package/lib/types/test/citations.test.d.ts +1 -0
- package/lib/types/test/client-wiring.test.d.ts +1 -0
- package/lib/types/test/consolidate-job.test.d.ts +1 -0
- package/lib/types/test/consolidate.test.d.ts +1 -0
- package/lib/types/test/consolidation-selection.test.d.ts +1 -0
- package/lib/types/test/dedupe-keys.test.d.ts +1 -0
- package/lib/types/test/delta.test.d.ts +1 -0
- package/lib/types/test/derived.test.d.ts +1 -0
- package/lib/types/test/disputes-panel.test.d.ts +1 -0
- package/lib/types/test/eval.test.d.ts +1 -0
- package/lib/types/test/expand.test.d.ts +1 -0
- package/lib/types/test/extract.test.d.ts +1 -0
- package/lib/types/test/git-origin.test.d.ts +1 -0
- package/lib/types/test/injection.test.d.ts +1 -0
- package/lib/types/test/kind-filter.test.d.ts +1 -0
- package/lib/types/test/legacy-state.test.d.ts +1 -0
- package/lib/types/test/log.test.d.ts +1 -0
- package/lib/types/test/loops.test.d.ts +1 -0
- package/lib/types/test/memory.test.d.ts +1 -0
- package/lib/types/test/notices.test.d.ts +1 -0
- package/lib/types/test/persona.test.d.ts +1 -0
- package/lib/types/test/prefilter.test.d.ts +1 -0
- package/lib/types/test/preset.test.d.ts +1 -0
- package/lib/types/test/raw-recall.test.d.ts +1 -0
- package/lib/types/test/recall.test.d.ts +1 -0
- package/lib/types/test/reflection.test.d.ts +1 -0
- package/lib/types/test/remote.test.d.ts +1 -0
- package/lib/types/test/retention.test.d.ts +1 -0
- package/lib/types/test/retrieval.test.d.ts +1 -0
- package/lib/types/test/rotation.test.d.ts +1 -0
- package/lib/types/test/schedule.test.d.ts +1 -0
- package/lib/types/test/settings-card.test.d.ts +1 -0
- package/lib/types/test/skills.test.d.ts +1 -0
- package/lib/types/test/state.test.d.ts +1 -0
- package/lib/types/test/subagent-contract.test.d.ts +1 -0
- package/lib/types/test/sweep.test.d.ts +1 -0
- package/lib/types/test/sync.test.d.ts +1 -0
- package/lib/types/test/time.test.d.ts +1 -0
- package/lib/types/time.d.ts +67 -0
- package/lib/types/tool.d.ts +9 -0
- package/lib/types/types.d.ts +374 -0
- package/lib/types/workspace.d.ts +20 -0
- package/lib/types.js +47 -0
- package/lib/workspace.js +65 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +126 -4
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for git-origin and for the origin fields storage records.
|
|
3
|
+
*
|
|
4
|
+
* @module dsh-cross-memory/test/git-origin.test
|
|
5
|
+
*/
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
8
|
+
import { tmpdir } from 'node:os';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
import test from 'node:test';
|
|
11
|
+
import { gitOrigin, normalizeGitUrl } from '../git-origin.js';
|
|
12
|
+
import { MemoryStore, projectSlug } from '../storage.js';
|
|
13
|
+
/** Write a git config holding one `origin` url into `<dir>`. */
|
|
14
|
+
async function writeConfig(dir, url) {
|
|
15
|
+
await mkdir(dir, { recursive: true });
|
|
16
|
+
await writeFile(join(dir, 'config'), [
|
|
17
|
+
'[core]',
|
|
18
|
+
'\trepositoryformatversion = 0',
|
|
19
|
+
'[remote "origin"]',
|
|
20
|
+
`\turl = ${url}`,
|
|
21
|
+
'\tfetch = +refs/heads/*:refs/remotes/origin/*',
|
|
22
|
+
'',
|
|
23
|
+
].join('\n'), 'utf8');
|
|
24
|
+
}
|
|
25
|
+
/** Write the descriptor files one project slug has on disk. */
|
|
26
|
+
async function writeProjectFiles(memories, slug, projectOrigin, hosts) {
|
|
27
|
+
const dir = join(memories, 'projects', slug);
|
|
28
|
+
await mkdir(join(dir, 'hosts'), { recursive: true });
|
|
29
|
+
await writeFile(join(dir, 'project.json'), JSON.stringify({
|
|
30
|
+
root: join('/checkouts', slug),
|
|
31
|
+
name: slug,
|
|
32
|
+
slug,
|
|
33
|
+
updatedAt: 1,
|
|
34
|
+
...projectOrigin === undefined ? {} : { origin: projectOrigin },
|
|
35
|
+
}));
|
|
36
|
+
for (const host of hosts) {
|
|
37
|
+
await writeFile(join(dir, 'hosts', `${host.file}.json`), JSON.stringify({
|
|
38
|
+
host: host.file,
|
|
39
|
+
platform: 'linux',
|
|
40
|
+
root: join('/checkouts', slug, host.file),
|
|
41
|
+
firstSeen: 1,
|
|
42
|
+
lastSeen: 2,
|
|
43
|
+
...host.origin === undefined ? {} : { origin: host.origin },
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
test('normalizeGitUrl folds ssh, https, credentials and suffixes into one origin', () => {
|
|
48
|
+
const expected = 'github.com/owner/repo';
|
|
49
|
+
for (const raw of [
|
|
50
|
+
'git@github.com:owner/repo.git',
|
|
51
|
+
'github.com:owner/repo',
|
|
52
|
+
'ssh://git@github.com/owner/repo.git',
|
|
53
|
+
'ssh://git@github.com/owner/repo',
|
|
54
|
+
'git://github.com/owner/repo.git',
|
|
55
|
+
'http://github.com/owner/repo',
|
|
56
|
+
'https://github.com/owner/repo.git',
|
|
57
|
+
'https://user:secret@github.com/owner/repo.git',
|
|
58
|
+
'https://github.com/owner/repo/',
|
|
59
|
+
'https://github.com/owner/repo.git/',
|
|
60
|
+
'https://GitHub.com/owner/repo.git',
|
|
61
|
+
'https://github.com/owner/repo.git?ref=main#readme',
|
|
62
|
+
' git@github.com:owner/repo.git ',
|
|
63
|
+
]) {
|
|
64
|
+
assert.equal(normalizeGitUrl(raw), expected, raw);
|
|
65
|
+
}
|
|
66
|
+
// Different repositories, and a path's case, stay distinct.
|
|
67
|
+
assert.notEqual(normalizeGitUrl('https://github.com/owner/other.git'), expected);
|
|
68
|
+
assert.notEqual(normalizeGitUrl('https://github.com/Owner/Repo.git'), expected);
|
|
69
|
+
// Credentials are gone, so a token rotation does not look like a new project.
|
|
70
|
+
assert.equal(normalizeGitUrl('https://old-token@git.example.com:8443/team/svc.git'), 'git.example.com:8443/team/svc');
|
|
71
|
+
});
|
|
72
|
+
test('normalizeGitUrl rejects values that name no repository', () => {
|
|
73
|
+
for (const raw of [
|
|
74
|
+
'',
|
|
75
|
+
' ',
|
|
76
|
+
'github.com',
|
|
77
|
+
'/Users/you/Code/alpha',
|
|
78
|
+
'C:\\Users\\you\\Code\\alpha',
|
|
79
|
+
'file:///tmp/repo.git',
|
|
80
|
+
'https://github.com',
|
|
81
|
+
'git@github.com:',
|
|
82
|
+
'not a url',
|
|
83
|
+
]) {
|
|
84
|
+
assert.equal(normalizeGitUrl(raw), undefined, JSON.stringify(raw));
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
test('gitOrigin reads the origin remote from a .git directory', async (t) => {
|
|
88
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-origin-'));
|
|
89
|
+
t.after(() => rm(root, { recursive: true, force: true }));
|
|
90
|
+
await writeConfig(join(root, '.git'), 'git@github.com:owner/repo.git');
|
|
91
|
+
assert.equal(await gitOrigin(root), 'github.com/owner/repo');
|
|
92
|
+
});
|
|
93
|
+
test('gitOrigin follows a worktree .git file to the shared config through commondir', async (t) => {
|
|
94
|
+
const base = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-worktree-'));
|
|
95
|
+
t.after(() => rm(base, { recursive: true, force: true }));
|
|
96
|
+
const main = join(base, 'repo');
|
|
97
|
+
const worktree = join(base, 'repo-branch');
|
|
98
|
+
await writeConfig(join(main, '.git'), 'https://user:token@git.example.com/team/service.git');
|
|
99
|
+
// The linked worktree's gitdir has no config of its own: `commondir` points
|
|
100
|
+
// back at the main repository's `.git`, which holds the shared config.
|
|
101
|
+
const linked = join(main, '.git', 'worktrees', 'branch');
|
|
102
|
+
await mkdir(linked, { recursive: true });
|
|
103
|
+
await writeFile(join(linked, 'commondir'), '../..\n', 'utf8');
|
|
104
|
+
await mkdir(worktree, { recursive: true });
|
|
105
|
+
await writeFile(join(worktree, '.git'), `gitdir: ${join('..', 'repo', '.git', 'worktrees', 'branch')}\n`, 'utf8');
|
|
106
|
+
assert.equal(await gitOrigin(worktree), 'git.example.com/team/service');
|
|
107
|
+
});
|
|
108
|
+
test('gitOrigin falls back to the gitdir config when no commondir exists', async (t) => {
|
|
109
|
+
const base = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-submodule-'));
|
|
110
|
+
t.after(() => rm(base, { recursive: true, force: true }));
|
|
111
|
+
const root = join(base, 'sub');
|
|
112
|
+
const gitDir = join(base, 'modules', 'sub');
|
|
113
|
+
await writeConfig(gitDir, 'ssh://git@gitlab.example.com/group/sub.git');
|
|
114
|
+
await mkdir(root, { recursive: true });
|
|
115
|
+
await writeFile(join(root, '.git'), `gitdir: ${gitDir}\n`, 'utf8');
|
|
116
|
+
assert.equal(await gitOrigin(root), 'gitlab.example.com/group/sub');
|
|
117
|
+
});
|
|
118
|
+
test('gitOrigin returns undefined for a directory that is not a checkout', async (t) => {
|
|
119
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-plain-'));
|
|
120
|
+
t.after(() => rm(root, { recursive: true, force: true }));
|
|
121
|
+
assert.equal(await gitOrigin(root), undefined);
|
|
122
|
+
// A `.git` file without the gitdir directive is not a checkout either.
|
|
123
|
+
await writeFile(join(root, '.git'), 'not a gitdir\n', 'utf8');
|
|
124
|
+
assert.equal(await gitOrigin(root), undefined);
|
|
125
|
+
});
|
|
126
|
+
test('a checkout without an origin remote has no project origin', async (t) => {
|
|
127
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-no-remote-'));
|
|
128
|
+
t.after(() => rm(root, { recursive: true, force: true }));
|
|
129
|
+
await mkdir(join(root, '.git'), { recursive: true });
|
|
130
|
+
await writeFile(join(root, '.git', 'config'), '[core]\n\trepositoryformatversion = 0\n', 'utf8');
|
|
131
|
+
assert.equal(await gitOrigin(root), undefined);
|
|
132
|
+
});
|
|
133
|
+
test('writeProjectDescriptor records the checkout origin and omits it for a plain directory', async (t) => {
|
|
134
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-origin-project-'));
|
|
135
|
+
const plain = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-origin-plain-'));
|
|
136
|
+
const memories = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-origin-store-'));
|
|
137
|
+
t.after(() => rm(root, { recursive: true, force: true }));
|
|
138
|
+
t.after(() => rm(plain, { recursive: true, force: true }));
|
|
139
|
+
t.after(() => rm(memories, { recursive: true, force: true }));
|
|
140
|
+
await writeConfig(join(root, '.git'), 'git@github.com:owner/alpha.git');
|
|
141
|
+
const store = new MemoryStore(memories);
|
|
142
|
+
await store.writeProjectDescriptor(root, 1000);
|
|
143
|
+
const slug = projectSlug(root);
|
|
144
|
+
assert.equal((await store.readProjectDescriptor(slug))?.origin, 'github.com/owner/alpha');
|
|
145
|
+
const hosts = await store.listHostDescriptors(slug);
|
|
146
|
+
assert.equal(hosts.length, 1);
|
|
147
|
+
assert.equal(hosts[0]?.origin, 'github.com/owner/alpha');
|
|
148
|
+
// A directory that is not a git checkout records no origin at all, rather
|
|
149
|
+
// than an empty one that a reader could mistake for a known identity.
|
|
150
|
+
await store.writeProjectDescriptor(plain, 1000);
|
|
151
|
+
const descriptor = await store.readProjectDescriptor(projectSlug(plain));
|
|
152
|
+
assert.equal(descriptor?.origin, undefined);
|
|
153
|
+
assert.equal(descriptor !== undefined && 'origin' in descriptor, false);
|
|
154
|
+
});
|
|
155
|
+
test('projectOrigins merges the descriptor and every host file without duplicates', async (t) => {
|
|
156
|
+
const memories = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-origins-'));
|
|
157
|
+
t.after(() => rm(memories, { recursive: true, force: true }));
|
|
158
|
+
await writeProjectFiles(memories, 'alpha', 'github.com/owner/alpha', [
|
|
159
|
+
{ file: 'pc', origin: 'github.com/owner/alpha' },
|
|
160
|
+
{ file: 'mac', origin: 'github.com/other/alpha' },
|
|
161
|
+
// A host file written before origins existed contributes nothing.
|
|
162
|
+
{ file: 'legacy' },
|
|
163
|
+
]);
|
|
164
|
+
await writeProjectFiles(memories, 'beta', undefined, []);
|
|
165
|
+
const store = new MemoryStore(memories);
|
|
166
|
+
assert.deepEqual(await store.projectOrigins('alpha'), ['github.com/other/alpha', 'github.com/owner/alpha']);
|
|
167
|
+
assert.deepEqual(await store.projectOrigins('beta'), []);
|
|
168
|
+
assert.deepEqual(await store.projectOrigins('never-written'), []);
|
|
169
|
+
});
|
|
170
|
+
test('originConflicts reports only a slug shared by two different origins', async (t) => {
|
|
171
|
+
const memories = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-conflicts-'));
|
|
172
|
+
t.after(() => rm(memories, { recursive: true, force: true }));
|
|
173
|
+
await writeProjectFiles(memories, 'shared', 'github.com/owner/shared', [
|
|
174
|
+
{ file: 'pc', origin: 'github.com/owner/shared' },
|
|
175
|
+
{ file: 'mac', origin: 'github.com/other/shared' },
|
|
176
|
+
{ file: 'laptop', origin: 'github.com/other/shared' },
|
|
177
|
+
]);
|
|
178
|
+
// One repository checked out on two machines is the normal case, not a clash.
|
|
179
|
+
await writeProjectFiles(memories, 'single', 'github.com/owner/single', [
|
|
180
|
+
{ file: 'pc', origin: 'github.com/owner/single' },
|
|
181
|
+
{ file: 'mac', origin: 'github.com/owner/single' },
|
|
182
|
+
]);
|
|
183
|
+
// A directory with no origin at all cannot prove a collision either.
|
|
184
|
+
await writeProjectFiles(memories, 'none', undefined, [{ file: 'pc' }]);
|
|
185
|
+
const store = new MemoryStore(memories);
|
|
186
|
+
assert.deepEqual(await store.originConflicts(), [
|
|
187
|
+
{ slug: 'shared', origins: ['github.com/other/shared', 'github.com/owner/shared'] },
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injection-behaviour tests for {@link MemoriesRuntime}.
|
|
3
|
+
*
|
|
4
|
+
* These pin the once-per-conversation rule that keeps the summary from crowding
|
|
5
|
+
* out the conversation: the block enters the first step at which the store has
|
|
6
|
+
* something to say, never again afterwards, and `clear`/`compact` (which replace
|
|
7
|
+
* the conversation) are the only thing that re-arms it.
|
|
8
|
+
*
|
|
9
|
+
* @module dsh-cross-memory/test/injection.test
|
|
10
|
+
*/
|
|
11
|
+
import assert from 'node:assert/strict';
|
|
12
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
13
|
+
import { homedir, tmpdir } from 'node:os';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import test from 'node:test';
|
|
16
|
+
import { MemoriesRuntime, isCatchAllDirectory } from '../index.js';
|
|
17
|
+
import { writeLoops } from '../loops.js';
|
|
18
|
+
/** A context stand-in exposing only what the runtime touches. */
|
|
19
|
+
const stubContext = {
|
|
20
|
+
get: () => undefined,
|
|
21
|
+
logger: { info: () => undefined, warn: () => undefined, debug: () => undefined },
|
|
22
|
+
};
|
|
23
|
+
/** A session stand-in: header, id, header fold, and a controllable history. */
|
|
24
|
+
function stubSession(cwd, history = []) {
|
|
25
|
+
let seq = 1;
|
|
26
|
+
return {
|
|
27
|
+
id: 'test-session',
|
|
28
|
+
header: { version: 0, id: 'test-session', createdAt: 0, cwd, isSeeded: false },
|
|
29
|
+
get seq() { return seq += 1; },
|
|
30
|
+
requestHeader: () => undefined,
|
|
31
|
+
deriveMessages: () => [...history],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** An agent stand-in exposing only `session`. */
|
|
35
|
+
function stubAgent(session) {
|
|
36
|
+
return { id: 'test-session', session, status: 'idle' };
|
|
37
|
+
}
|
|
38
|
+
test('injection is once per conversation, not once per turn', async (t) => {
|
|
39
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-inj-'));
|
|
40
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
41
|
+
t.after(() => runtime.dispose());
|
|
42
|
+
const agent = stubAgent(stubSession(dir));
|
|
43
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'an empty store injects nothing');
|
|
44
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Prefer pnpm', body: 'Use pnpm, not npm.', tags: ['tooling'] }, 'tool');
|
|
45
|
+
const first = await runtime.injectionFor(agent);
|
|
46
|
+
assert.ok(first !== undefined, 'a non-empty store injects');
|
|
47
|
+
assert.match(JSON.stringify(first.content), /Prefer pnpm/u);
|
|
48
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'the same step does not re-inject');
|
|
49
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'nor a later step');
|
|
50
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'nor a later turn');
|
|
51
|
+
// A write inside the conversation is NOT a reason to re-inject: the model just
|
|
52
|
+
// made it, and every later recall goes through memory_search.
|
|
53
|
+
await runtime.write(agent.session, { scope: 'global', title: 'Answer in Chinese', body: 'The user writes Chinese.', tags: [] }, 'tool');
|
|
54
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'a write never re-injects');
|
|
55
|
+
// `clear`/`compact` replace the conversation, so a session that carries
|
|
56
|
+
// nothing (the stub's empty history) gets the block again.
|
|
57
|
+
runtime.resetInjection(agent.session);
|
|
58
|
+
const second = await runtime.injectionFor(agent);
|
|
59
|
+
assert.ok(second !== undefined, 'a reset re-arms injection');
|
|
60
|
+
assert.notEqual(second.id, first.id);
|
|
61
|
+
assert.match(JSON.stringify(second.content), /Answer in Chinese/u);
|
|
62
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
63
|
+
});
|
|
64
|
+
test('a restarted harness does not re-inject into a conversation that already carries the block', async (t) => {
|
|
65
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-restart-'));
|
|
66
|
+
const first = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
67
|
+
t.after(() => first.dispose());
|
|
68
|
+
const session = stubSession(dir);
|
|
69
|
+
await first.write(session, { scope: 'global', title: 'Prefer pnpm', body: 'Use pnpm, not npm.', tags: ['tooling'] }, 'tool');
|
|
70
|
+
const block = await first.injectionFor(stubAgent(session));
|
|
71
|
+
assert.ok(block !== undefined, 'the first process injects once');
|
|
72
|
+
// A fresh runtime models a restarted process. The block travelled with the
|
|
73
|
+
// durable history, so resuming that conversation must not add a second copy —
|
|
74
|
+
// which is exactly what an in-memory-only marker could not see.
|
|
75
|
+
const second = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
76
|
+
t.after(() => second.dispose());
|
|
77
|
+
assert.equal(await second.injectionFor(stubAgent(stubSession(dir, [block]))), undefined, 'a restored history already carries the block');
|
|
78
|
+
// A cleared conversation carries nothing, so the block legitimately returns.
|
|
79
|
+
assert.ok(await second.injectionFor(stubAgent(stubSession(dir))) !== undefined, 'a cleared conversation gets the block again');
|
|
80
|
+
// Both stores must be closed before the temp directory goes.
|
|
81
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
82
|
+
});
|
|
83
|
+
test('an empty store stays silent, and a session with nothing to say is not marked', async (t) => {
|
|
84
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-forget-'));
|
|
85
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
86
|
+
t.after(() => runtime.dispose());
|
|
87
|
+
const agent = stubAgent(stubSession(dir));
|
|
88
|
+
await runtime.write(agent.session, { scope: 'project', title: 'Windows only', body: 'Path separators differ.', tags: [] }, 'tool');
|
|
89
|
+
assert.equal(await runtime.forget(agent.session, 'project', 'windows-only'), true);
|
|
90
|
+
assert.equal(await runtime.injectionFor(agent), undefined, 'an emptied store has nothing to say');
|
|
91
|
+
// The first memory that arrives after the session started still gets injected:
|
|
92
|
+
// a silent step must not consume the one injection the conversation is owed.
|
|
93
|
+
await runtime.write(agent.session, { scope: 'project', title: 'Windows only', body: 'Path separators differ.', tags: [] }, 'tool');
|
|
94
|
+
assert.ok(await runtime.injectionFor(agent) !== undefined, 'the first non-empty step injects');
|
|
95
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
96
|
+
});
|
|
97
|
+
test('project scope follows the session cwd and stays isolated from other roots', async (t) => {
|
|
98
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-scope-'));
|
|
99
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
100
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
101
|
+
t.after(() => runtime.dispose());
|
|
102
|
+
const inside = stubAgent(stubSession(workspace));
|
|
103
|
+
const outside = stubAgent(stubSession(tmpdir()));
|
|
104
|
+
await runtime.write(inside.session, { scope: 'project', title: 'Repo convention', body: 'Tests live beside sources.', tags: [] }, 'tool');
|
|
105
|
+
const here = await runtime.summary(inside.session);
|
|
106
|
+
assert.ok(here !== undefined);
|
|
107
|
+
assert.match(here, /Repo convention/u);
|
|
108
|
+
const elsewhere = await runtime.summary(outside.session);
|
|
109
|
+
assert.equal(elsewhere, undefined, 'another workspace sees nothing of this project scope');
|
|
110
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
111
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
112
|
+
});
|
|
113
|
+
test('the injected summary never exceeds its configured byte budget', async (t) => {
|
|
114
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-budget-'));
|
|
115
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false, maxSummaryBytes: 700, maxSummaryEntries: 50 });
|
|
116
|
+
t.after(() => runtime.dispose());
|
|
117
|
+
const agent = stubAgent(stubSession(dir));
|
|
118
|
+
for (let index = 0; index < 20; index += 1) {
|
|
119
|
+
await runtime.write(agent.session, { scope: 'global', title: `Fact ${index}`, body: 'x'.repeat(200), tags: ['noise'] }, 'tool');
|
|
120
|
+
}
|
|
121
|
+
const summary = await runtime.summary(agent.session);
|
|
122
|
+
assert.ok(summary !== undefined);
|
|
123
|
+
assert.ok(Buffer.byteLength(summary, 'utf8') <= 700);
|
|
124
|
+
const injected = await runtime.injectionFor(agent);
|
|
125
|
+
assert.ok(injected !== undefined);
|
|
126
|
+
const rows = runtime.state.retentionRows().filter((row) => row.scope === 'global');
|
|
127
|
+
const entries = await runtime.store.list('global', undefined, { fresh: true });
|
|
128
|
+
const listed = entries.filter((entry) => summary.includes(`- ${entry.title} [`));
|
|
129
|
+
assert.ok(listed.length < entries.length, 'the byte budget hides some selected memories');
|
|
130
|
+
for (const entry of entries) {
|
|
131
|
+
const surfaced = rows.find((row) => row.id === entry.id)?.surfacedAt ?? 0;
|
|
132
|
+
assert.equal(surfaced > 0, listed.some((shown) => shown.id === entry.id), `${entry.title} is credited only if shown`);
|
|
133
|
+
}
|
|
134
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
135
|
+
});
|
|
136
|
+
test('the harness home is not a workspace, so its sessions have no project scope', async (t) => {
|
|
137
|
+
const home = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-home-'));
|
|
138
|
+
const dir = join(home, 'memories');
|
|
139
|
+
const runtime = new MemoriesRuntime(stubContext, { dshHome: home, memoriesDir: dir, autoExtract: false });
|
|
140
|
+
t.after(() => runtime.dispose());
|
|
141
|
+
t.after(() => rm(home, { recursive: true, force: true }));
|
|
142
|
+
const agent = stubAgent(stubSession(home));
|
|
143
|
+
assert.equal(await runtime.projectRoot(agent.session), undefined, 'the harness home has no project scope');
|
|
144
|
+
// A project draft from such a session is stored globally rather than dropped:
|
|
145
|
+
// the extractor cannot know the session has no workspace.
|
|
146
|
+
const stored = await runtime.write(agent.session, { scope: 'project', title: 'Mixed fact', body: 'From a home-directory session.', tags: [] }, 'tool');
|
|
147
|
+
assert.equal(stored.entry.scope, 'global');
|
|
148
|
+
assert.deepEqual(await runtime.store.listProjects(), [], 'no project bucket is created for the harness home');
|
|
149
|
+
// A subdirectory of the harness home is still the harness home.
|
|
150
|
+
const nested = stubAgent(stubSession(join(home, 'logs')));
|
|
151
|
+
assert.equal(await runtime.projectRoot(nested.session), undefined);
|
|
152
|
+
// A real repository underneath another root keeps its own scope.
|
|
153
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
154
|
+
const inside = stubAgent(stubSession(workspace));
|
|
155
|
+
assert.equal(await runtime.projectRoot(inside.session), workspace);
|
|
156
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
157
|
+
});
|
|
158
|
+
test('the global section is capped so it cannot bury the project scope', async (t) => {
|
|
159
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-cap-'));
|
|
160
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
161
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false, globalSummaryEntries: 2 });
|
|
162
|
+
t.after(() => runtime.dispose());
|
|
163
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
164
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
165
|
+
const agent = stubAgent(stubSession(workspace));
|
|
166
|
+
for (let index = 0; index < 12; index += 1) {
|
|
167
|
+
await runtime.write(agent.session, { scope: 'global', title: `Portable fact ${index}`, body: 'True everywhere.', tags: [] }, 'auto');
|
|
168
|
+
}
|
|
169
|
+
await runtime.write(agent.session, { scope: 'project', title: 'This repo is special', body: 'Only true here.', tags: [] }, 'tool');
|
|
170
|
+
const summary = await runtime.summary(agent.session);
|
|
171
|
+
assert.ok(summary !== undefined);
|
|
172
|
+
const [globalPart, projectPart] = summary.split('\n## Project');
|
|
173
|
+
const globalBullets = (globalPart ?? '').split('\n').filter((line) => line.startsWith('- ') && !line.startsWith('- …'));
|
|
174
|
+
assert.equal(globalBullets.length, 2, 'the global half stops at its own cap');
|
|
175
|
+
assert.match(projectPart ?? '', /This repo is special/u, 'and the project half still appears');
|
|
176
|
+
});
|
|
177
|
+
test('the global half is bounded in bytes, not only in entries', async (t) => {
|
|
178
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-gbytes-'));
|
|
179
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
180
|
+
const runtime = new MemoriesRuntime(stubContext, {
|
|
181
|
+
memoriesDir: dir,
|
|
182
|
+
autoExtract: false,
|
|
183
|
+
globalSummaryEntries: 12,
|
|
184
|
+
globalSummaryBytes: 700,
|
|
185
|
+
});
|
|
186
|
+
t.after(() => runtime.dispose());
|
|
187
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
188
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
189
|
+
const agent = stubAgent(stubSession(workspace));
|
|
190
|
+
// Chinese entries cost about three bytes per character, so a count cap alone
|
|
191
|
+
// still let the global half take most of a 4 KB block.
|
|
192
|
+
for (let index = 0; index < 12; index += 1) {
|
|
193
|
+
await runtime.write(agent.session, {
|
|
194
|
+
scope: 'global',
|
|
195
|
+
title: `本机环境事实 ${index}`,
|
|
196
|
+
body: `这是一条很长的中文背景说明,用来确认真实字节数而不是字符数决定了注入预算的分配。第 ${index} 条。`,
|
|
197
|
+
tags: [],
|
|
198
|
+
}, 'auto');
|
|
199
|
+
}
|
|
200
|
+
await runtime.write(agent.session, { scope: 'project', title: 'This repo is special', body: 'Only true here.', tags: [] }, 'tool');
|
|
201
|
+
const summary = await runtime.summary(agent.session);
|
|
202
|
+
assert.ok(summary !== undefined);
|
|
203
|
+
const [globalPart] = summary.split('\n## Project');
|
|
204
|
+
assert.ok(Buffer.byteLength(globalPart ?? '', 'utf8') < 1_100, 'the global section stays inside its byte budget');
|
|
205
|
+
assert.match(summary, /This repo is special/u, 'and the project half is still there');
|
|
206
|
+
});
|
|
207
|
+
test('a never-surfaced backlog cannot swallow the shared global half', async (t) => {
|
|
208
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-freshhalf-'));
|
|
209
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
210
|
+
// The shipped ratio: the reservation asks for 3, the global half renders 4.
|
|
211
|
+
const runtime = new MemoriesRuntime(stubContext, {
|
|
212
|
+
memoriesDir: dir, autoExtract: false, globalSummaryEntries: 4, summaryFreshSlots: 3,
|
|
213
|
+
});
|
|
214
|
+
t.after(() => runtime.dispose());
|
|
215
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
216
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
217
|
+
const agent = stubAgent(stubSession(workspace));
|
|
218
|
+
// Well-established global memories that have actually been read, so they win
|
|
219
|
+
// the ranking on merit. Without that history every entry ties on weight and
|
|
220
|
+
// the newest simply sorts first — which would make any fresh bullet below
|
|
221
|
+
// evidence of the ranking rather than of the reservation.
|
|
222
|
+
const established = [];
|
|
223
|
+
for (let index = 0; index < 12; index += 1) {
|
|
224
|
+
const written = await runtime.write(agent.session, {
|
|
225
|
+
scope: 'global', title: `Established rule ${index}`, body: 'Applies everywhere.', tags: [],
|
|
226
|
+
}, 'auto');
|
|
227
|
+
established.push(written.entry.id);
|
|
228
|
+
}
|
|
229
|
+
for (const id of established) {
|
|
230
|
+
for (let read = 0; read < 3; read += 1)
|
|
231
|
+
await runtime.read(agent.session, 'global', id);
|
|
232
|
+
}
|
|
233
|
+
// Then a backlog: three brand-new global memories, none of them about anything.
|
|
234
|
+
// Every new memory arrives never-surfaced, so this is what a busy week looks like.
|
|
235
|
+
for (const title of ['Newest note A', 'Newest note B', 'Newest note C']) {
|
|
236
|
+
await runtime.write(agent.session, { scope: 'global', title, body: 'Never surfaced yet.', tags: [] }, 'auto');
|
|
237
|
+
}
|
|
238
|
+
// One project memory makes the two halves compete for the shared budget, which
|
|
239
|
+
// is the only situation the global count cap exists to arbitrate.
|
|
240
|
+
await runtime.write(agent.session, { scope: 'project', title: 'Workspace fact', body: 'Only here.', tags: [] }, 'auto');
|
|
241
|
+
const summary = await runtime.summary(agent.session);
|
|
242
|
+
assert.ok(summary !== undefined);
|
|
243
|
+
const [globalPart] = summary.split('\n## Project');
|
|
244
|
+
const freshShown = (globalPart ?? '').split('\n').filter((line) => /^- (?:📌 )?Newest note/u.test(line)).length;
|
|
245
|
+
const bullets = (globalPart ?? '').split('\n').filter((line) => line.startsWith('- ') && !line.startsWith('- …')).length;
|
|
246
|
+
assert.equal(bullets, 4, 'the global half still fills its own cap');
|
|
247
|
+
// Sized against `maxSummaryEntries` this was 3 of 4, and every topically
|
|
248
|
+
// relevant global memory was gone — the half every session shares, spent on
|
|
249
|
+
// whatever happened to be written recently.
|
|
250
|
+
assert.ok(freshShown <= 1, `the reservation takes at most a third of the section, not ${freshShown} of 4`);
|
|
251
|
+
assert.match(globalPart ?? '', /Established rule/u, 'and the ranking still fills the rest');
|
|
252
|
+
});
|
|
253
|
+
test('with no project half, the global half may use the shared budget', async (t) => {
|
|
254
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-alone-'));
|
|
255
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
256
|
+
const runtime = new MemoriesRuntime(stubContext, {
|
|
257
|
+
memoriesDir: dir, autoExtract: false, globalSummaryEntries: 4, summaryFreshSlots: 3,
|
|
258
|
+
});
|
|
259
|
+
t.after(() => runtime.dispose());
|
|
260
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
261
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
262
|
+
const agent = stubAgent(stubSession(workspace));
|
|
263
|
+
for (let index = 0; index < 10; index += 1) {
|
|
264
|
+
await runtime.write(agent.session, {
|
|
265
|
+
scope: 'global', title: `Global rule ${index}`, body: 'Applies everywhere.', tags: [],
|
|
266
|
+
}, 'auto');
|
|
267
|
+
}
|
|
268
|
+
const summary = await runtime.summary(agent.session);
|
|
269
|
+
assert.ok(summary !== undefined);
|
|
270
|
+
const bullets = summary.split('\n').filter((line) => line.startsWith('- ') && !line.startsWith('- …')).length;
|
|
271
|
+
// Four was the only cap left in force, and 62% of the byte budget sat unused.
|
|
272
|
+
assert.ok(bullets > 4, `a project-less session should list more than the global cap, got ${bullets}`);
|
|
273
|
+
});
|
|
274
|
+
test('a directory named in projectRootOverrides owns a scope without a marker', async (t) => {
|
|
275
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ovr-'));
|
|
276
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-home-'));
|
|
277
|
+
const runtime = new MemoriesRuntime(stubContext, {
|
|
278
|
+
memoriesDir: dir, autoExtract: false, projectRootOverrides: { [workspace]: workspace },
|
|
279
|
+
});
|
|
280
|
+
t.after(() => runtime.dispose());
|
|
281
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
282
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
283
|
+
const agent = stubAgent(stubSession(workspace));
|
|
284
|
+
// No .git here: without the override this session has no workspace at all,
|
|
285
|
+
// which is exactly how plugin-development notes ended up in the global scope.
|
|
286
|
+
const written = await runtime.write(agent.session, {
|
|
287
|
+
scope: 'project', title: 'Harness note', body: 'About the plugin itself.', tags: [],
|
|
288
|
+
}, 'auto');
|
|
289
|
+
assert.equal(written.entry.scope, 'project');
|
|
290
|
+
assert.deepEqual((await runtime.read(agent.session, 'project', written.entry.id))?.title, 'Harness note');
|
|
291
|
+
});
|
|
292
|
+
test('a global draft that names a known workspace is filed with that workspace', async (t) => {
|
|
293
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-scopeguard-'));
|
|
294
|
+
const workspace = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-ws-'));
|
|
295
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
296
|
+
t.after(() => runtime.dispose());
|
|
297
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
298
|
+
t.after(() => rm(workspace, { recursive: true, force: true }));
|
|
299
|
+
const agent = stubAgent(stubSession(workspace));
|
|
300
|
+
// The project scope has to exist for the guard to know about this workspace.
|
|
301
|
+
await runtime.write(agent.session, { scope: 'project', title: 'Repo layout', body: 'Sources under src.', tags: [] }, 'tool');
|
|
302
|
+
// A global memory follows the user into every unrelated project, so a draft
|
|
303
|
+
// that quotes this workspace's own path belongs to this workspace instead.
|
|
304
|
+
// The path is spelled with the platform's own separator: the guard looks for
|
|
305
|
+
// a path this platform would recognize.
|
|
306
|
+
const misfiled = await runtime.write(agent.session, {
|
|
307
|
+
scope: 'global',
|
|
308
|
+
title: 'Invoice export rule',
|
|
309
|
+
body: `The exporter only reads ${join(workspace, 'invoices')}.`,
|
|
310
|
+
tags: [],
|
|
311
|
+
}, 'auto');
|
|
312
|
+
assert.equal(misfiled.entry.scope, 'project', 'the cited path decides the scope');
|
|
313
|
+
// A memory that names no workspace stays global.
|
|
314
|
+
const portable = await runtime.write(agent.session, {
|
|
315
|
+
scope: 'global',
|
|
316
|
+
title: 'Prefer pnpm',
|
|
317
|
+
body: 'Use pnpm rather than npm.',
|
|
318
|
+
tags: [],
|
|
319
|
+
}, 'tool');
|
|
320
|
+
assert.equal(portable.entry.scope, 'global');
|
|
321
|
+
});
|
|
322
|
+
test('the user home directory itself is not a workspace', () => {
|
|
323
|
+
assert.equal(isCatchAllDirectory(homedir(), 'C:\\harness-home'), true);
|
|
324
|
+
assert.equal(isCatchAllDirectory(join(homedir(), 'AppData', 'LocalLow', 'SomeGame'), 'C:\\harness-home'), false);
|
|
325
|
+
});
|
|
326
|
+
test('a global draft is never rerouted into a catch-all directory', async (t) => {
|
|
327
|
+
const home = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-home-'));
|
|
328
|
+
const dir = join(home, 'memories');
|
|
329
|
+
const runtime = new MemoriesRuntime(stubContext, { dshHome: home, memoriesDir: dir, autoExtract: false });
|
|
330
|
+
t.after(() => runtime.dispose());
|
|
331
|
+
t.after(() => rm(home, { recursive: true, force: true }));
|
|
332
|
+
// A descriptor for the harness home can outlive the guard that stopped new
|
|
333
|
+
// ones being created. A machine-level fact quoting a path there belongs in
|
|
334
|
+
// global: filing it under the home scope hides it from every other workspace.
|
|
335
|
+
const entry = await runtime.write(stubAgent(stubSession(home)).session, {
|
|
336
|
+
scope: 'global',
|
|
337
|
+
title: 'Helper lives in the harness home',
|
|
338
|
+
body: `The helper is ${home}\\restart.mjs, and its log sits beside it.`,
|
|
339
|
+
tags: [],
|
|
340
|
+
}, 'tool');
|
|
341
|
+
assert.equal(entry.entry.scope, 'global', 'a catch-all directory is never a reroute target');
|
|
342
|
+
assert.deepEqual(await runtime.store.listProjects(), [], 'and no scope is created for it');
|
|
343
|
+
});
|
|
344
|
+
test('a session with no recorded cwd has no project scope either', async (t) => {
|
|
345
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-nocwd-'));
|
|
346
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
347
|
+
t.after(() => runtime.dispose());
|
|
348
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
349
|
+
const session = stubSession(undefined);
|
|
350
|
+
assert.equal(await runtime.projectRoot(session), undefined, 'no cwd means no workspace, not "wherever the host runs"');
|
|
351
|
+
assert.match(await runtime.stats(session), /project:none: 0 memories/u);
|
|
352
|
+
});
|
|
353
|
+
test('an injected memory the turn is about earns a use', async (t) => {
|
|
354
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-helped-'));
|
|
355
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
356
|
+
t.after(() => runtime.dispose());
|
|
357
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
358
|
+
const agent = stubAgent(stubSession(dir, [{
|
|
359
|
+
role: 'user',
|
|
360
|
+
source: { kind: 'user' },
|
|
361
|
+
content: [{ type: 'text', text: '发票 OCR 手写字段要怎么补摘?' }],
|
|
362
|
+
}]));
|
|
363
|
+
const stored = await runtime.write(agent.session, {
|
|
364
|
+
scope: 'global',
|
|
365
|
+
title: '发票 OCR 手写字段补摘',
|
|
366
|
+
body: '按版面语义抽字段,不要用首个正则命中。',
|
|
367
|
+
tags: ['ocr'],
|
|
368
|
+
}, 'tool');
|
|
369
|
+
assert.equal(stored.entry.uses, 0);
|
|
370
|
+
assert.ok(await runtime.injectionFor(agent) !== undefined, 'the block is injected first');
|
|
371
|
+
await runtime.creditInjectedUse(agent);
|
|
372
|
+
const after = await runtime.read(agent.session, 'global', stored.entry.id, false);
|
|
373
|
+
assert.ok((after?.uses ?? 0) >= 1, 'the conversation used the memory it was handed');
|
|
374
|
+
// Once per conversation: later steps must not keep inflating the count.
|
|
375
|
+
await runtime.creditInjectedUse(agent);
|
|
376
|
+
const again = await runtime.read(agent.session, 'global', stored.entry.id, false);
|
|
377
|
+
assert.equal(again?.uses, after?.uses);
|
|
378
|
+
});
|
|
379
|
+
test('a turn that only brushes past an injected memory earns nothing', async (t) => {
|
|
380
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-helped2-'));
|
|
381
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
382
|
+
t.after(() => runtime.dispose());
|
|
383
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
384
|
+
const agent = stubAgent(stubSession(dir, [{
|
|
385
|
+
role: 'user',
|
|
386
|
+
source: { kind: 'user' },
|
|
387
|
+
content: [{ type: 'text', text: '把这次改动提交一下,然后继续说别的。' }],
|
|
388
|
+
}]));
|
|
389
|
+
const stored = await runtime.write(agent.session, {
|
|
390
|
+
scope: 'global',
|
|
391
|
+
title: '发票 OCR 手写字段补摘',
|
|
392
|
+
body: '按版面语义抽字段,不要用首个正则命中。',
|
|
393
|
+
tags: ['ocr'],
|
|
394
|
+
}, 'tool');
|
|
395
|
+
await runtime.injectionFor(agent);
|
|
396
|
+
await runtime.creditInjectedUse(agent);
|
|
397
|
+
const after = await runtime.read(agent.session, 'global', stored.entry.id, false);
|
|
398
|
+
assert.equal(after?.uses, 0, 'a shared common word must not count as use');
|
|
399
|
+
});
|
|
400
|
+
test('a scope that has filled up still shows a memory written after it filled', async (t) => {
|
|
401
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-saturated-'));
|
|
402
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
403
|
+
t.after(() => runtime.dispose());
|
|
404
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
405
|
+
const agent = stubAgent(stubSession(dir));
|
|
406
|
+
const now = Date.now();
|
|
407
|
+
// Twenty long-standing, well-read entries: each one outranks anything new.
|
|
408
|
+
for (let index = 0; index < 20; index += 1) {
|
|
409
|
+
const { entry } = await runtime.write(agent.session, { scope: 'global', title: `Old fact ${index}`, body: 'Been here for months.', tags: [] }, 'auto');
|
|
410
|
+
await runtime.store.writeCounters(entry, undefined, { uses: 6, lastUsedAt: now, surfacedAt: now });
|
|
411
|
+
}
|
|
412
|
+
// The correction arrives last, reads zero, and has never been listed.
|
|
413
|
+
await runtime.write(agent.session, { scope: 'global', title: 'The correction', body: 'This supersedes one of the old facts.', tags: [] }, 'tool');
|
|
414
|
+
const summary = await runtime.summary(agent.session);
|
|
415
|
+
assert.ok(summary !== undefined);
|
|
416
|
+
assert.match(summary, /The correction/u, 'the reserved slot is what makes a new memory visible');
|
|
417
|
+
assert.match(summary, /… 9 more not shown|… \d+ more not shown/u, 'the block still says how much it hides');
|
|
418
|
+
});
|
|
419
|
+
test('an open loop is injected even when the memory store is empty', async (t) => {
|
|
420
|
+
const dir = await mkdtemp(join(tmpdir(), 'dsh-cross-memory-loops-inj-'));
|
|
421
|
+
const runtime = new MemoriesRuntime(stubContext, { memoriesDir: dir, autoExtract: false });
|
|
422
|
+
t.after(() => runtime.dispose());
|
|
423
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
424
|
+
const agent = stubAgent(stubSession(dir));
|
|
425
|
+
// The end-to-end benchmark caught this: the extractor filed "明天把登录页截图
|
|
426
|
+
// 补齐" as a loop, the loop ledger knew it, and the injected context did not.
|
|
427
|
+
await writeLoops(dir, {
|
|
428
|
+
version: 1,
|
|
429
|
+
loops: [
|
|
430
|
+
{ id: 'loop-shot', text: '明天把登录页的截图补齐', openedAt: Date.now(), state: 'open' },
|
|
431
|
+
{ id: 'loop-done', text: '已经发出去的报价', openedAt: Date.now(), state: 'done' },
|
|
432
|
+
],
|
|
433
|
+
});
|
|
434
|
+
const block = await runtime.injectionFor(agent);
|
|
435
|
+
assert.ok(block !== undefined, 'a loop alone is something to say');
|
|
436
|
+
assert.match(JSON.stringify(block.content), /登录页的截图/u);
|
|
437
|
+
assert.doesNotMatch(JSON.stringify(block.content), /已经发出去的报价/u, 'closed loops are history');
|
|
438
|
+
// And it stays out of the store: a loop is an obligation, not a memory fact.
|
|
439
|
+
assert.equal((await runtime.store.list('global', undefined, { fresh: true })).length, 0);
|
|
440
|
+
});
|